Curl_gmtime: added a portable gmtime
Instead of polluting many places with #ifdefs, we create a single place for this function, and also check return code properly so that a NULL pointer returned won't cause problems.
This commit is contained in:
13
lib/file.c
13
lib/file.c
@@ -5,7 +5,7 @@
|
|||||||
* | (__| |_| | _ <| |___
|
* | (__| |_| | _ <| |___
|
||||||
* \___|\___/|_| \_\_____|
|
* \___|\___/|_| \_\_____|
|
||||||
*
|
*
|
||||||
* Copyright (C) 1998 - 2010, Daniel Stenberg, <daniel@haxx.se>, et al.
|
* Copyright (C) 1998 - 2011, Daniel Stenberg, <daniel@haxx.se>, et al.
|
||||||
*
|
*
|
||||||
* This software is licensed as described in the file COPYING, which
|
* This software is licensed as described in the file COPYING, which
|
||||||
* you should have received as part of this distribution. The terms
|
* you should have received as part of this distribution. The terms
|
||||||
@@ -487,14 +487,13 @@ static CURLcode file_do(struct connectdata *conn, bool *done)
|
|||||||
return result;
|
return result;
|
||||||
|
|
||||||
if(fstated) {
|
if(fstated) {
|
||||||
const struct tm *tm;
|
|
||||||
time_t filetime = (time_t)statbuf.st_mtime;
|
time_t filetime = (time_t)statbuf.st_mtime;
|
||||||
#ifdef HAVE_GMTIME_R
|
|
||||||
struct tm buffer;
|
struct tm buffer;
|
||||||
tm = (const struct tm *)gmtime_r(&filetime, &buffer);
|
const struct tm *tm = &buffer;
|
||||||
#else
|
result = Curl_gmtime(filetime, &buffer);
|
||||||
tm = gmtime(&filetime);
|
if(result)
|
||||||
#endif
|
return result;
|
||||||
|
|
||||||
/* format: "Tue, 15 Nov 1994 12:45:26 GMT" */
|
/* format: "Tue, 15 Nov 1994 12:45:26 GMT" */
|
||||||
snprintf(buf, BUFSIZE-1,
|
snprintf(buf, BUFSIZE-1,
|
||||||
"Last-Modified: %s, %02d %s %4d %02d:%02d:%02d GMT\r\n",
|
"Last-Modified: %s, %02d %s %4d %02d:%02d:%02d GMT\r\n",
|
||||||
|
12
lib/ftp.c
12
lib/ftp.c
@@ -1841,14 +1841,14 @@ static CURLcode ftp_state_mdtm_resp(struct connectdata *conn,
|
|||||||
ftpc->file &&
|
ftpc->file &&
|
||||||
data->set.get_filetime &&
|
data->set.get_filetime &&
|
||||||
(data->info.filetime>=0) ) {
|
(data->info.filetime>=0) ) {
|
||||||
struct tm *tm;
|
|
||||||
time_t filetime = (time_t)data->info.filetime;
|
time_t filetime = (time_t)data->info.filetime;
|
||||||
#ifdef HAVE_GMTIME_R
|
|
||||||
struct tm buffer;
|
struct tm buffer;
|
||||||
tm = (struct tm *)gmtime_r(&filetime, &buffer);
|
const struct tm *tm = &buffer;
|
||||||
#else
|
|
||||||
tm = gmtime(&filetime);
|
result = Curl_gmtime(filetime, &buffer);
|
||||||
#endif
|
if(result)
|
||||||
|
return result;
|
||||||
|
|
||||||
/* format: "Tue, 15 Nov 1994 12:45:26" */
|
/* format: "Tue, 15 Nov 1994 12:45:26" */
|
||||||
snprintf(buf, BUFSIZE-1,
|
snprintf(buf, BUFSIZE-1,
|
||||||
"Last-Modified: %s, %02d %s %4d %02d:%02d:%02d GMT\r\n",
|
"Last-Modified: %s, %02d %s %4d %02d:%02d:%02d GMT\r\n",
|
||||||
|
11
lib/gtls.c
11
lib/gtls.c
@@ -170,13 +170,12 @@ static void showtime(struct SessionHandle *data,
|
|||||||
const char *text,
|
const char *text,
|
||||||
time_t stamp)
|
time_t stamp)
|
||||||
{
|
{
|
||||||
struct tm *tm;
|
|
||||||
#ifdef HAVE_GMTIME_R
|
|
||||||
struct tm buffer;
|
struct tm buffer;
|
||||||
tm = (struct tm *)gmtime_r(&stamp, &buffer);
|
const struct tm *tm = &buffer;
|
||||||
#else
|
CURLcode result = Curl_gmtime(stamp, &buffer);
|
||||||
tm = gmtime(&stamp);
|
if(result)
|
||||||
#endif
|
return;
|
||||||
|
|
||||||
snprintf(data->state.buffer,
|
snprintf(data->state.buffer,
|
||||||
BUFSIZE,
|
BUFSIZE,
|
||||||
"\t %s: %s, %02d %s %4d %02d:%02d:%02d GMT\n",
|
"\t %s: %s, %02d %s %4d %02d:%02d:%02d GMT\n",
|
||||||
|
20
lib/http.c
20
lib/http.c
@@ -2052,9 +2052,17 @@ CURLcode Curl_add_custom_headers(struct connectdata *conn,
|
|||||||
CURLcode Curl_add_timecondition(struct SessionHandle *data,
|
CURLcode Curl_add_timecondition(struct SessionHandle *data,
|
||||||
Curl_send_buffer *req_buffer)
|
Curl_send_buffer *req_buffer)
|
||||||
{
|
{
|
||||||
struct tm *tm;
|
const struct tm *tm;
|
||||||
char *buf = data->state.buffer;
|
char *buf = data->state.buffer;
|
||||||
CURLcode result = CURLE_OK;
|
CURLcode result = CURLE_OK;
|
||||||
|
struct tm keeptime;
|
||||||
|
|
||||||
|
result = Curl_gmtime(data->set.timevalue, &keeptime);
|
||||||
|
if(result) {
|
||||||
|
failf(data, "Invalid TIMEVALUE\n");
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
tm = &keeptime;
|
||||||
|
|
||||||
/* The If-Modified-Since header family should have their times set in
|
/* The If-Modified-Since header family should have their times set in
|
||||||
* GMT as RFC2616 defines: "All HTTP date/time stamps MUST be
|
* GMT as RFC2616 defines: "All HTTP date/time stamps MUST be
|
||||||
@@ -2063,14 +2071,6 @@ CURLcode Curl_add_timecondition(struct SessionHandle *data,
|
|||||||
* Time)." (see page 20 of RFC2616).
|
* Time)." (see page 20 of RFC2616).
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#ifdef HAVE_GMTIME_R
|
|
||||||
/* thread-safe version */
|
|
||||||
struct tm keeptime;
|
|
||||||
tm = (struct tm *)gmtime_r(&data->set.timevalue, &keeptime);
|
|
||||||
#else
|
|
||||||
tm = gmtime(&data->set.timevalue);
|
|
||||||
#endif
|
|
||||||
|
|
||||||
/* format: "Tue, 15 Nov 1994 12:45:26 GMT" */
|
/* format: "Tue, 15 Nov 1994 12:45:26 GMT" */
|
||||||
snprintf(buf, BUFSIZE-1,
|
snprintf(buf, BUFSIZE-1,
|
||||||
"%s, %02d %s %4d %02d:%02d:%02d GMT",
|
"%s, %02d %s %4d %02d:%02d:%02d GMT",
|
||||||
@@ -2654,7 +2654,7 @@ CURLcode Curl_http(struct connectdata *conn, bool *done)
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
if(data->set.timecondition) {
|
if(data->set.timecondition) {
|
||||||
result = Curl_add_timecondition(data, req_buffer);
|
result = Curl_add_timecondition(data, req_buffer);
|
||||||
if(result)
|
if(result)
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
@@ -5,7 +5,7 @@
|
|||||||
* | (__| |_| | _ <| |___
|
* | (__| |_| | _ <| |___
|
||||||
* \___|\___/|_| \_\_____|
|
* \___|\___/|_| \_\_____|
|
||||||
*
|
*
|
||||||
* Copyright (C) 1998 - 2010, Daniel Stenberg, <daniel@haxx.se>, et al.
|
* Copyright (C) 1998 - 2011, Daniel Stenberg, <daniel@haxx.se>, et al.
|
||||||
*
|
*
|
||||||
* This software is licensed as described in the file COPYING, which
|
* This software is licensed as described in the file COPYING, which
|
||||||
* you should have received as part of this distribution. The terms
|
* you should have received as part of this distribution. The terms
|
||||||
@@ -514,3 +514,20 @@ time_t curl_getdate(const char *p, const time_t *now)
|
|||||||
/* everything else is fail */
|
/* everything else is fail */
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
CURLcode Curl_gmtime(time_t intime, struct tm *store)
|
||||||
|
{
|
||||||
|
const struct tm *tm;
|
||||||
|
#ifdef HAVE_GMTIME_R
|
||||||
|
/* thread-safe version */
|
||||||
|
tm = (struct tm *)(gmtime_r)(&intime, store);
|
||||||
|
#else
|
||||||
|
tm = (gmtime)(&intime);
|
||||||
|
if(tm)
|
||||||
|
*store = *tm; /* copy the pointed struct to the local copy */
|
||||||
|
#endif
|
||||||
|
|
||||||
|
if(!tm)
|
||||||
|
return CURLE_BAD_FUNCTION_ARGUMENT;
|
||||||
|
return CURLE_OK;
|
||||||
|
}
|
||||||
|
@@ -7,7 +7,7 @@
|
|||||||
* | (__| |_| | _ <| |___
|
* | (__| |_| | _ <| |___
|
||||||
* \___|\___/|_| \_\_____|
|
* \___|\___/|_| \_\_____|
|
||||||
*
|
*
|
||||||
* Copyright (C) 1998 - 2009, Daniel Stenberg, <daniel@haxx.se>, et al.
|
* Copyright (C) 1998 - 2011, Daniel Stenberg, <daniel@haxx.se>, et al.
|
||||||
*
|
*
|
||||||
* This software is licensed as described in the file COPYING, which
|
* This software is licensed as described in the file COPYING, which
|
||||||
* you should have received as part of this distribution. The terms
|
* you should have received as part of this distribution. The terms
|
||||||
@@ -43,4 +43,6 @@ int Curl_parsedate(const char *date, time_t *output);
|
|||||||
#define PARSEDATE_LATER 1
|
#define PARSEDATE_LATER 1
|
||||||
#define PARSEDATE_SOONER 2
|
#define PARSEDATE_SOONER 2
|
||||||
|
|
||||||
|
CURLcode Curl_gmtime(time_t intime, struct tm *store);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
Reference in New Issue
Block a user