X-Git-Url: https://git.m6w6.name/?p=m6w6%2Fext-http;a=blobdiff_plain;f=http_date_api.c;h=78a5da1938da7935a83acf587c4b6ccfe1ecaffd;hp=91c16766aba994136783995584ccfcb27a77d341;hb=ad5f896b03adaa073134a00108a9cdf00720673a;hpb=edc84b40eb2c5be04492fa98fec5833a030782eb diff --git a/http_date_api.c b/http_date_api.c index 91c1676..78a5da1 100644 --- a/http_date_api.c +++ b/http_date_api.c @@ -6,22 +6,21 @@ | modification, are permitted provided that the conditions mentioned | | in the accompanying LICENSE file are met. | +--------------------------------------------------------------------+ - | Copyright (c) 2004-2005, Michael Wallner | + | Copyright (c) 2004-2010, Michael Wallner | +--------------------------------------------------------------------+ */ /* $Id$ */ -#ifdef HAVE_CONFIG_H -# include "config.h" -#endif - #include "php_http.h" + +#include "php_http_api.h" #include "php_http_date_api.h" -static int check_day(char *day, size_t len); -static int check_month(char *month); -static int check_tzone(char *tzone); +static inline int check_day(const char *day, size_t len); +static inline int check_month(const char *month); +static inline int check_tzone(const char *tzone); +static inline time_t parse_date(const char *month); /* {{{ day/month names */ static const char *days[] = { @@ -96,7 +95,7 @@ static const struct time_zone { /* {{{ Day/Month/TZ checks for http_parse_date() Originally by libcurl, Copyright (C) 1998 - 2004, Daniel Stenberg, , et al. */ -static int check_day(char *day, size_t len) +static inline int check_day(const char *day, size_t len) { int i; const char * const *check = (len > 3) ? &weekdays[0] : &wkdays[0]; @@ -109,7 +108,7 @@ static int check_day(char *day, size_t len) return -1; } -static int check_month(char *month) +static inline int check_month(const char *month) { int i; const char * const *check = &months[0]; @@ -125,7 +124,7 @@ static int check_month(char *month) /* return the time zone offset between GMT and the input one, in number of seconds or -1 if the timezone wasn't found/legal */ -static int check_tzone(char *tzone) +static inline int check_tzone(const char *tzone) { unsigned i; const struct time_zone *check = time_zones; @@ -142,26 +141,39 @@ static int check_tzone(char *tzone) /* {{{ char *http_date(time_t) */ PHP_HTTP_API char *_http_date(time_t t TSRMLS_DC) { - struct tm *gmtime, tmbuf; + char *date = NULL; + struct tm *gmtime = NULL, tmbuf; + memset(&tmbuf, 0, sizeof(tmbuf)); if ((gmtime = php_gmtime_r(&t, &tmbuf))) { - char *date = ecalloc(1, 31); - snprintf(date, 30, + spprintf(&date, 0, "%s, %02d %s %04d %02d:%02d:%02d GMT", days[gmtime->tm_wday], gmtime->tm_mday, months[gmtime->tm_mon], gmtime->tm_year + 1900, gmtime->tm_hour, gmtime->tm_min, gmtime->tm_sec ); - return date; } - return NULL; + return date; +} +/* }}} */ + +/* {{{ time_t http_parse_date(char *) */ +PHP_HTTP_API time_t _http_parse_date_ex(const char *date, zend_bool silent TSRMLS_DC) +{ + time_t t = parse_date(date); + + if (-1 == t && !silent) { + http_error_ex(HE_NOTICE, HTTP_E_RUNTIME, "Could not parse date: %s", date); + } + + return t; } /* }}} */ -/* {{{ time_t http_parse_date(char *) +/* time_t parse_date(char *) Originally by libcurl, Copyright (C) 1998 - 2004, Daniel Stenberg, , et al. */ -PHP_HTTP_API time_t _http_parse_date(const char *date) +static inline time_t parse_date(const char *date) { time_t t = 0; int tz_offset = -1, year = -1, month = -1, monthday = -1, weekday = -1, @@ -175,11 +187,11 @@ PHP_HTTP_API time_t _http_parse_date(const char *date) while (*date && (part < 6)) { int found = 0; - while (*date && !isalnum(*date)) { + while (*date && !HTTP_IS_CTYPE(alnum, *date)) { date++; } - if (isalpha(*date)) { + if (HTTP_IS_CTYPE(alpha, *date)) { /* a name coming up */ char buf[32] = ""; size_t len; @@ -213,7 +225,7 @@ PHP_HTTP_API time_t _http_parse_date(const char *date) } date += len; } - else if (isdigit(*date)) { + else if (HTTP_IS_CTYPE(digit, *date)) { /* a digit */ int val; char *end; @@ -310,11 +322,13 @@ PHP_HTTP_API time_t _http_parse_date(const char *date) long delta; time_t t2; - if(!(gmt = php_gmtime_r(&t, &keeptime2))) { + if((gmt = php_gmtime_r(&t, &keeptime2))) { + tm = *gmt; /* MSVC quirks */ + } else { return -1; /* illegal date/time */ } - t2 = mktime(gmt); + t2 = mktime(&tm); /* Add the time zone diff (between the given timezone and GMT) and the diff between the local time zone and GMT. */