- fixed a gotcha in http_chunked_decode (-size_t is always > 0)
[m6w6/ext-http] / http_date_api.c
1 /*
2 +----------------------------------------------------------------------+
3 | PECL :: http |
4 +----------------------------------------------------------------------+
5 | This source file is subject to version 3.0 of the PHP license, that |
6 | is bundled with this package in the file LICENSE, and is available |
7 | through the world-wide-web at http://www.php.net/license/3_0.txt. |
8 | If you did not receive a copy of the PHP license and are unable to |
9 | obtain it through the world-wide-web, please send a note to |
10 | license@php.net so we can mail you a copy immediately. |
11 +----------------------------------------------------------------------+
12 | Copyright (c) 2004-2005 Michael Wallner <mike@php.net> |
13 +----------------------------------------------------------------------+
14 */
15
16 /* $Id$ */
17
18 #ifdef HAVE_CONFIG_H
19 # include "config.h"
20 #endif
21 #include "php.h"
22
23 #include "php_http.h"
24 #include "php_http_std_defs.h"
25
26 #include <ctype.h>
27
28 static int check_day(char *day, size_t len);
29 static int check_month(char *month);
30 static int check_tzone(char *tzone);
31
32 /* {{{ day/month names */
33 static const char *days[] = {
34 "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
35 };
36 static const char *wkdays[] = {
37 "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"
38 };
39 static const char *weekdays[] = {
40 "Monday", "Tuesday", "Wednesday",
41 "Thursday", "Friday", "Saturday", "Sunday"
42 };
43 static const char *months[] = {
44 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
45 "Jul", "Aug", "Sep", "Okt", "Nov", "Dec"
46 };
47 enum assume_next {
48 DATE_MDAY,
49 DATE_YEAR,
50 DATE_TIME
51 };
52 static const struct time_zone {
53 const char *name;
54 const int offset;
55 } time_zones[] = {
56 {"GMT", 0}, /* Greenwich Mean */
57 {"UTC", 0}, /* Universal (Coordinated) */
58 {"WET", 0}, /* Western European */
59 {"BST", 0}, /* British Summer */
60 {"WAT", 60}, /* West Africa */
61 {"AST", 240}, /* Atlantic Standard */
62 {"ADT", 240}, /* Atlantic Daylight */
63 {"EST", 300}, /* Eastern Standard */
64 {"EDT", 300}, /* Eastern Daylight */
65 {"CST", 360}, /* Central Standard */
66 {"CDT", 360}, /* Central Daylight */
67 {"MST", 420}, /* Mountain Standard */
68 {"MDT", 420}, /* Mountain Daylight */
69 {"PST", 480}, /* Pacific Standard */
70 {"PDT", 480}, /* Pacific Daylight */
71 {"YST", 540}, /* Yukon Standard */
72 {"YDT", 540}, /* Yukon Daylight */
73 {"HST", 600}, /* Hawaii Standard */
74 {"HDT", 600}, /* Hawaii Daylight */
75 {"CAT", 600}, /* Central Alaska */
76 {"AHST", 600}, /* Alaska-Hawaii Standard */
77 {"NT", 660}, /* Nome */
78 {"IDLW", 720}, /* International Date Line West */
79 {"CET", -60}, /* Central European */
80 {"MET", -60}, /* Middle European */
81 {"MEWT", -60}, /* Middle European Winter */
82 {"MEST", -120}, /* Middle European Summer */
83 {"CEST", -120}, /* Central European Summer */
84 {"MESZ", -60}, /* Middle European Summer */
85 {"FWT", -60}, /* French Winter */
86 {"FST", -60}, /* French Summer */
87 {"EET", -120}, /* Eastern Europe, USSR Zone 1 */
88 {"WAST", -420}, /* West Australian Standard */
89 {"WADT", -420}, /* West Australian Daylight */
90 {"CCT", -480}, /* China Coast, USSR Zone 7 */
91 {"JST", -540}, /* Japan Standard, USSR Zone 8 */
92 {"EAST", -600}, /* Eastern Australian Standard */
93 {"EADT", -600}, /* Eastern Australian Daylight */
94 {"GST", -600}, /* Guam Standard, USSR Zone 9 */
95 {"NZT", -720}, /* New Zealand */
96 {"NZST", -720}, /* New Zealand Standard */
97 {"NZDT", -720}, /* New Zealand Daylight */
98 {"IDLE", -720}, /* International Date Line East */
99 };
100 /* }}} */
101
102 /* {{{ Day/Month/TZ checks for http_parse_date()
103 Originally by libcurl, Copyright (C) 1998 - 2004, Daniel Stenberg, <daniel@haxx.se>, et al. */
104 static int check_day(char *day, size_t len)
105 {
106 int i;
107 const char * const *check = (len > 3) ? &weekdays[0] : &wkdays[0];
108 for (i = 0; i < 7; i++) {
109 if (!strcmp(day, check[0])) {
110 return i;
111 }
112 check++;
113 }
114 return -1;
115 }
116
117 static int check_month(char *month)
118 {
119 int i;
120 const char * const *check = &months[0];
121 for (i = 0; i < 12; i++) {
122 if (!strcmp(month, check[0])) {
123 return i;
124 }
125 check++;
126 }
127 return -1;
128 }
129
130 /* return the time zone offset between GMT and the input one, in number
131 of seconds or -1 if the timezone wasn't found/legal */
132
133 static int check_tzone(char *tzone)
134 {
135 unsigned i;
136 const struct time_zone *check = time_zones;
137 for (i = 0; i < sizeof(time_zones) / sizeof(time_zones[0]); i++) {
138 if (!strcmp(tzone, check->name)) {
139 return check->offset * 60;
140 }
141 check++;
142 }
143 return -1;
144 }
145 /* }}} */
146
147 /* {{{ char *http_date(time_t) */
148 PHP_HTTP_API char *_http_date(time_t t TSRMLS_DC)
149 {
150 struct tm *gmtime, tmbuf;
151
152 if (gmtime = php_gmtime_r(&t, &tmbuf)) {
153 char *date = ecalloc(1, 31);
154 snprintf(date, 30,
155 "%s, %02d %s %04d %02d:%02d:%02d GMT",
156 days[gmtime->tm_wday], gmtime->tm_mday,
157 months[gmtime->tm_mon], gmtime->tm_year + 1900,
158 gmtime->tm_hour, gmtime->tm_min, gmtime->tm_sec
159 );
160 return date;
161 }
162
163 return NULL;
164 }
165 /* }}} */
166
167 /* {{{ time_t http_parse_date(char *)
168 Originally by libcurl, Copyright (C) 1998 - 2004, Daniel Stenberg, <daniel@haxx.se>, et al. */
169 PHP_HTTP_API time_t _http_parse_date(const char *date)
170 {
171 time_t t = 0;
172 int tz_offset = -1, year = -1, month = -1, monthday = -1, weekday = -1,
173 hours = -1, minutes = -1, seconds = -1;
174 struct tm tm;
175 enum assume_next dignext = DATE_MDAY;
176 const char *indate = date;
177
178 int part = 0; /* max 6 parts */
179
180 while (*date && (part < 6)) {
181 int found = 0;
182
183 while (*date && !isalnum(*date)) {
184 date++;
185 }
186
187 if (isalpha(*date)) {
188 /* a name coming up */
189 char buf[32] = "";
190 size_t len;
191 sscanf(date, "%31[A-Za-z]", buf);
192 len = strlen(buf);
193
194 if (weekday == -1) {
195 weekday = check_day(buf, len);
196 if (weekday != -1) {
197 found = 1;
198 }
199 }
200
201 if (!found && (month == -1)) {
202 month = check_month(buf);
203 if (month != -1) {
204 found = 1;
205 }
206 }
207
208 if (!found && (tz_offset == -1)) {
209 /* this just must be a time zone string */
210 tz_offset = check_tzone(buf);
211 if (tz_offset != -1) {
212 found = 1;
213 }
214 }
215
216 if (!found) {
217 return -1; /* bad string */
218 }
219 date += len;
220 }
221 else if (isdigit(*date)) {
222 /* a digit */
223 int val;
224 char *end;
225 if ((seconds == -1) &&
226 (3 == sscanf(date, "%02d:%02d:%02d", &hours, &minutes, &seconds))) {
227 /* time stamp! */
228 date += 8;
229 found = 1;
230 }
231 else {
232 val = (int) strtol(date, &end, 10);
233
234 if ((tz_offset == -1) && ((end - date) == 4) && (val < 1300) &&
235 (indate < date) && ((date[-1] == '+' || date[-1] == '-'))) {
236 /* four digits and a value less than 1300 and it is preceeded with
237 a plus or minus. This is a time zone indication. */
238 found = 1;
239 tz_offset = (val / 100 * 60 + val % 100) * 60;
240
241 /* the + and - prefix indicates the local time compared to GMT,
242 this we need ther reversed math to get what we want */
243 tz_offset = date[-1] == '+' ? -tz_offset : tz_offset;
244 }
245
246 if (((end - date) == 8) && (year == -1) && (month == -1) && (monthday == -1)) {
247 /* 8 digits, no year, month or day yet. This is YYYYMMDD */
248 found = 1;
249 year = val / 10000;
250 month = (val % 10000) / 100 - 1; /* month is 0 - 11 */
251 monthday = val % 100;
252 }
253
254 if (!found && (dignext == DATE_MDAY) && (monthday == -1)) {
255 if ((val > 0) && (val < 32)) {
256 monthday = val;
257 found = 1;
258 }
259 dignext = DATE_YEAR;
260 }
261
262 if (!found && (dignext == DATE_YEAR) && (year == -1)) {
263 year = val;
264 found = 1;
265 if (year < 1900) {
266 year += year > 70 ? 1900 : 2000;
267 }
268 if(monthday == -1) {
269 dignext = DATE_MDAY;
270 }
271 }
272
273 if (!found) {
274 return -1;
275 }
276
277 date = end;
278 }
279 }
280
281 part++;
282 }
283
284 if (-1 == seconds) {
285 seconds = minutes = hours = 0; /* no time, make it zero */
286 }
287
288 if ((-1 == monthday) || (-1 == month) || (-1 == year)) {
289 /* lacks vital info, fail */
290 return -1;
291 }
292
293 if (sizeof(time_t) < 5) {
294 /* 32 bit time_t can only hold dates to the beginning of 2038 */
295 if (year > 2037) {
296 return 0x7fffffff;
297 }
298 }
299
300 tm.tm_sec = seconds;
301 tm.tm_min = minutes;
302 tm.tm_hour = hours;
303 tm.tm_mday = monthday;
304 tm.tm_mon = month;
305 tm.tm_year = year - 1900;
306 tm.tm_wday = 0;
307 tm.tm_yday = 0;
308 tm.tm_isdst = 0;
309
310 t = mktime(&tm);
311
312 /* time zone adjust */
313 if (t != -1) {
314 struct tm *gmt, keeptime2;
315 long delta;
316 time_t t2;
317
318 if(!(gmt = php_gmtime_r(&t, &keeptime2))) {
319 return -1; /* illegal date/time */
320 }
321
322 t2 = mktime(gmt);
323
324 /* Add the time zone diff (between the given timezone and GMT) and the
325 diff between the local time zone and GMT. */
326 delta = (tz_offset != -1 ? tz_offset : 0) + (t - t2);
327
328 if((delta > 0) && (t + delta < t)) {
329 return -1; /* time_t overflow */
330 }
331
332 t += delta;
333 }
334
335 return t;
336 }
337 /* }}} */
338
339
340 /*
341 * Local variables:
342 * tab-width: 4
343 * c-basic-offset: 4
344 * End:
345 * vim600: sw=4 ts=4 fdm=marker
346 * vim<600: sw=4 ts=4
347 */
348