* attempt to fix pecl Bug #16826 http_date strange behavior and crash
[m6w6/ext-http] / http_date_api.c
index 6770c0270edb2dc5efb34fb324b99d0547d89fe1..e6fd511552eef3a0404dd0a84b76a893ef1c5f16 100644 (file)
@@ -1,33 +1,26 @@
 /*
-   +----------------------------------------------------------------------+
-   | PECL :: http                                                         |
-   +----------------------------------------------------------------------+
-   | This source file is subject to version 3.0 of the PHP license, that  |
-   | is bundled with this package in the file LICENSE, and is available   |
-   | through the world-wide-web at http://www.php.net/license/3_0.txt.    |
-   | If you did not receive a copy of the PHP license and are unable to   |
-   | obtain it through the world-wide-web, please send a note to          |
-   | license@php.net so we can mail you a copy immediately.               |
-   +----------------------------------------------------------------------+
-   | Copyright (c) 2004-2005 Michael Wallner <mike@php.net>               |
-   +----------------------------------------------------------------------+
+    +--------------------------------------------------------------------+
+    | PECL :: http                                                       |
+    +--------------------------------------------------------------------+
+    | Redistribution and use in source and binary forms, with or without |
+    | modification, are permitted provided that the conditions mentioned |
+    | in the accompanying LICENSE file are met.                          |
+    +--------------------------------------------------------------------+
+    | Copyright (c) 2004-2007, Michael Wallner <mike@php.net>            |
+    +--------------------------------------------------------------------+
 */
 
 /* $Id$ */
 
-#ifdef HAVE_CONFIG_H
-#      include "config.h"
-#endif
-#include "php.h"
-
 #include "php_http.h"
-#include "php_http_std_defs.h"
 
-#include <ctype.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[] = {
@@ -49,6 +42,7 @@ enum assume_next {
        DATE_YEAR,
        DATE_TIME
 };
+#define DS -60
 static const struct time_zone {
        const char *name;
        const int offset;
@@ -56,22 +50,22 @@ static const struct time_zone {
     {"GMT", 0},     /* Greenwich Mean */
     {"UTC", 0},     /* Universal (Coordinated) */
     {"WET", 0},     /* Western European */
-    {"BST", 0},     /* British Summer */
+    {"BST", 0 DS}, /* British Summer */
     {"WAT", 60},    /* West Africa */
     {"AST", 240},   /* Atlantic Standard */
-    {"ADT", 240},   /* Atlantic Daylight */
+    {"ADT", 240 DS},/* Atlantic Daylight */
     {"EST", 300},   /* Eastern Standard */
-    {"EDT", 300},   /* Eastern Daylight */
+    {"EDT", 300 DS},/* Eastern Daylight */
     {"CST", 360},   /* Central Standard */
-    {"CDT", 360},   /* Central Daylight */
+    {"CDT", 360 DS},/* Central Daylight */
     {"MST", 420},   /* Mountain Standard */
-    {"MDT", 420},   /* Mountain Daylight */
+    {"MDT", 420 DS},/* Mountain Daylight */
     {"PST", 480},   /* Pacific Standard */
-    {"PDT", 480},   /* Pacific Daylight */
+    {"PDT", 480 DS},/* Pacific Daylight */
     {"YST", 540},   /* Yukon Standard */
-    {"YDT", 540},   /* Yukon Daylight */
+    {"YDT", 540 DS},/* Yukon Daylight */
     {"HST", 600},   /* Hawaii Standard */
-    {"HDT", 600},   /* Hawaii Daylight */
+    {"HDT", 600 DS},/* Hawaii Daylight */
     {"CAT", 600},   /* Central Alaska */
     {"AHST", 600},  /* Alaska-Hawaii Standard */
     {"NT",  660},   /* Nome */
@@ -79,29 +73,29 @@ static const struct time_zone {
     {"CET", -60},   /* Central European */
     {"MET", -60},   /* Middle European */
     {"MEWT", -60},  /* Middle European Winter */
-    {"MEST", -120}, /* Middle European Summer */
-    {"CEST", -120}, /* Central European Summer */
-    {"MESZ", -60},  /* Middle European Summer */
+    {"MEST", -60 DS},/* Middle European Summer */
+    {"CEST", -60 DS},/* Central European Summer */
+    {"MESZ", -60 DS},/* Middle European Summer */
     {"FWT", -60},   /* French Winter */
-    {"FST", -60},   /* French Summer */
+    {"FST", -60 DS},/* French Summer */
     {"EET", -120},  /* Eastern Europe, USSR Zone 1 */
     {"WAST", -420}, /* West Australian Standard */
-    {"WADT", -420}, /* West Australian Daylight */
+    {"WADT", -420 DS},/* West Australian Daylight */
     {"CCT", -480},  /* China Coast, USSR Zone 7 */
     {"JST", -540},  /* Japan Standard, USSR Zone 8 */
     {"EAST", -600}, /* Eastern Australian Standard */
-    {"EADT", -600}, /* Eastern Australian Daylight */
+    {"EADT", -600 DS},/* Eastern Australian Daylight */
     {"GST", -600},  /* Guam Standard, USSR Zone 9 */
     {"NZT", -720},  /* New Zealand */
     {"NZST", -720}, /* New Zealand Standard */
-    {"NZDT", -720}, /* New Zealand Daylight */
+    {"NZDT", -720 DS},/* New Zealand Daylight */
     {"IDLE", -720}, /* International Date Line East */
 };
 /* }}} */
 
 /* {{{ Day/Month/TZ checks for http_parse_date()
        Originally by libcurl, Copyright (C) 1998 - 2004, Daniel Stenberg, <daniel@haxx.se>, 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];
@@ -114,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];
@@ -130,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;
@@ -147,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;
 
-       if (gmtime = php_gmtime_r(&t, &tmbuf)) {
-               char *date = ecalloc(1, 31);
-               snprintf(date, 30,
+       memset(&tmbuf, 0, sizeof(tmbuf));
+       if ((gmtime = php_gmtime_r(&t, &tmbuf))) {
+               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, <daniel@haxx.se>, 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,
@@ -180,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;
@@ -218,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;
@@ -315,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. */