1
0
mirror of https://github.com/moparisthebest/curl synced 2024-12-21 15:48:49 -05:00

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:
Daniel Stenberg 2011-02-07 15:00:48 +01:00
parent 2cacd82661
commit 54d9f060b4
6 changed files with 48 additions and 31 deletions

View File

@ -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
* 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;
if(fstated) {
const struct tm *tm;
time_t filetime = (time_t)statbuf.st_mtime;
#ifdef HAVE_GMTIME_R
struct tm buffer;
tm = (const struct tm *)gmtime_r(&filetime, &buffer);
#else
tm = gmtime(&filetime);
#endif
const struct tm *tm = &buffer;
result = Curl_gmtime(filetime, &buffer);
if(result)
return result;
/* format: "Tue, 15 Nov 1994 12:45:26 GMT" */
snprintf(buf, BUFSIZE-1,
"Last-Modified: %s, %02d %s %4d %02d:%02d:%02d GMT\r\n",

View File

@ -1841,14 +1841,14 @@ static CURLcode ftp_state_mdtm_resp(struct connectdata *conn,
ftpc->file &&
data->set.get_filetime &&
(data->info.filetime>=0) ) {
struct tm *tm;
time_t filetime = (time_t)data->info.filetime;
#ifdef HAVE_GMTIME_R
struct tm buffer;
tm = (struct tm *)gmtime_r(&filetime, &buffer);
#else
tm = gmtime(&filetime);
#endif
const struct tm *tm = &buffer;
result = Curl_gmtime(filetime, &buffer);
if(result)
return result;
/* format: "Tue, 15 Nov 1994 12:45:26" */
snprintf(buf, BUFSIZE-1,
"Last-Modified: %s, %02d %s %4d %02d:%02d:%02d GMT\r\n",

View File

@ -170,13 +170,12 @@ static void showtime(struct SessionHandle *data,
const char *text,
time_t stamp)
{
struct tm *tm;
#ifdef HAVE_GMTIME_R
struct tm buffer;
tm = (struct tm *)gmtime_r(&stamp, &buffer);
#else
tm = gmtime(&stamp);
#endif
const struct tm *tm = &buffer;
CURLcode result = Curl_gmtime(stamp, &buffer);
if(result)
return;
snprintf(data->state.buffer,
BUFSIZE,
"\t %s: %s, %02d %s %4d %02d:%02d:%02d GMT\n",

View File

@ -2052,9 +2052,17 @@ CURLcode Curl_add_custom_headers(struct connectdata *conn,
CURLcode Curl_add_timecondition(struct SessionHandle *data,
Curl_send_buffer *req_buffer)
{
struct tm *tm;
const struct tm *tm;
char *buf = data->state.buffer;
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
* 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).
*/
#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" */
snprintf(buf, BUFSIZE-1,
"%s, %02d %s %4d %02d:%02d:%02d GMT",
@ -2654,7 +2654,7 @@ CURLcode Curl_http(struct connectdata *conn, bool *done)
#endif
if(data->set.timecondition) {
result = Curl_add_timecondition(data, req_buffer);
result = Curl_add_timecondition(data, req_buffer);
if(result)
return result;
}

View File

@ -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
* 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 */
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;
}

View File

@ -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
* 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_SOONER 2
CURLcode Curl_gmtime(time_t intime, struct tm *store);
#endif