upload http
[bottlenecks.git] / rubbos / app / httpd-2.0.64 / srclib / apr-util / misc / apr_date.c
1 /* Copyright 2000-2005 The Apache Software Foundation or its licensors, as
2  * applicable.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 /*
18  * apr_date.c: date parsing utility routines
19  *     These routines are (hopefully) platform independent.
20  * 
21  * 27 Oct 1996  Roy Fielding
22  *     Extracted (with many modifications) from mod_proxy.c and
23  *     tested with over 50,000 randomly chosen valid date strings
24  *     and several hundred variations of invalid date strings.
25  * 
26  */
27
28 #include "apr.h"
29 #include "apr_lib.h"
30
31 #define APR_WANT_STRFUNC
32 #include "apr_want.h"
33
34 #if APR_HAVE_STDLIB_H
35 #include <stdlib.h>
36 #endif
37
38 #if APR_HAVE_CTYPE_H
39 #include <ctype.h>
40 #endif
41
42 #include "apr_date.h"
43
44 /*
45  * Compare a string to a mask
46  * Mask characters (arbitrary maximum is 256 characters, just in case):
47  *   @ - uppercase letter
48  *   $ - lowercase letter
49  *   & - hex digit
50  *   # - digit
51  *   ~ - digit or space
52  *   * - swallow remaining characters 
53  *  <x> - exact match for any other character
54  */
55 APU_DECLARE(int) apr_date_checkmask(const char *data, const char *mask)
56 {
57     int i;
58     char d;
59
60     for (i = 0; i < 256; i++) {
61         d = data[i];
62         switch (mask[i]) {
63         case '\0':
64             return (d == '\0');
65
66         case '*':
67             return 1;
68
69         case '@':
70             if (!apr_isupper(d))
71                 return 0;
72             break;
73         case '$':
74             if (!apr_islower(d))
75                 return 0;
76             break;
77         case '#':
78             if (!apr_isdigit(d))
79                 return 0;
80             break;
81         case '&':
82             if (!apr_isxdigit(d))
83                 return 0;
84             break;
85         case '~':
86             if ((d != ' ') && !apr_isdigit(d))
87                 return 0;
88             break;
89         default:
90             if (mask[i] != d)
91                 return 0;
92             break;
93         }
94     }
95     return 0;          /* We only get here if mask is corrupted (exceeds 256) */
96 }
97
98 /*
99  * Parses an HTTP date in one of three standard forms:
100  *
101  *     Sun, 06 Nov 1994 08:49:37 GMT  ; RFC 822, updated by RFC 1123
102  *     Sunday, 06-Nov-94 08:49:37 GMT ; RFC 850, obsoleted by RFC 1036
103  *     Sun Nov  6 08:49:37 1994       ; ANSI C's asctime() format
104  *
105  * and returns the apr_time_t number of microseconds since 1 Jan 1970 GMT, 
106  * or APR_DATE_BAD if this would be out of range or if the date is invalid.
107  *
108  * The restricted HTTP syntax is
109  * 
110  *     HTTP-date    = rfc1123-date | rfc850-date | asctime-date
111  *
112  *     rfc1123-date = wkday "," SP date1 SP time SP "GMT"
113  *     rfc850-date  = weekday "," SP date2 SP time SP "GMT"
114  *     asctime-date = wkday SP date3 SP time SP 4DIGIT
115  *
116  *     date1        = 2DIGIT SP month SP 4DIGIT
117  *                    ; day month year (e.g., 02 Jun 1982)
118  *     date2        = 2DIGIT "-" month "-" 2DIGIT
119  *                    ; day-month-year (e.g., 02-Jun-82)
120  *     date3        = month SP ( 2DIGIT | ( SP 1DIGIT ))
121  *                    ; month day (e.g., Jun  2)
122  *
123  *     time         = 2DIGIT ":" 2DIGIT ":" 2DIGIT
124  *                    ; 00:00:00 - 23:59:59
125  *
126  *     wkday        = "Mon" | "Tue" | "Wed"
127  *                  | "Thu" | "Fri" | "Sat" | "Sun"
128  *
129  *     weekday      = "Monday" | "Tuesday" | "Wednesday"
130  *                  | "Thursday" | "Friday" | "Saturday" | "Sunday"
131  *
132  *     month        = "Jan" | "Feb" | "Mar" | "Apr"
133  *                  | "May" | "Jun" | "Jul" | "Aug"
134  *                  | "Sep" | "Oct" | "Nov" | "Dec"
135  *
136  * However, for the sake of robustness (and Netscapeness), we ignore the
137  * weekday and anything after the time field (including the timezone).
138  *
139  * This routine is intended to be very fast; 10x faster than using sscanf.
140  *
141  * Originally from Andrew Daviel <andrew@vancouver-webpages.com>, 29 Jul 96
142  * but many changes since then.
143  *
144  */
145 APU_DECLARE(apr_time_t) apr_date_parse_http(const char *date)
146 {
147     apr_time_exp_t ds;
148     apr_time_t result;
149     int mint, mon;
150     const char *monstr, *timstr;
151     static const int months[12] =
152     {
153     ('J' << 16) | ('a' << 8) | 'n', ('F' << 16) | ('e' << 8) | 'b',
154     ('M' << 16) | ('a' << 8) | 'r', ('A' << 16) | ('p' << 8) | 'r',
155     ('M' << 16) | ('a' << 8) | 'y', ('J' << 16) | ('u' << 8) | 'n',
156     ('J' << 16) | ('u' << 8) | 'l', ('A' << 16) | ('u' << 8) | 'g',
157     ('S' << 16) | ('e' << 8) | 'p', ('O' << 16) | ('c' << 8) | 't',
158     ('N' << 16) | ('o' << 8) | 'v', ('D' << 16) | ('e' << 8) | 'c'};
159
160     if (!date)
161         return APR_DATE_BAD;
162
163     while (*date && apr_isspace(*date))    /* Find first non-whitespace char */
164         ++date;
165
166     if (*date == '\0') 
167         return APR_DATE_BAD;
168
169     if ((date = strchr(date, ' ')) == NULL)       /* Find space after weekday */
170         return APR_DATE_BAD;
171
172     ++date;        /* Now pointing to first char after space, which should be */
173
174     /* start of the actual date information for all 4 formats. */
175
176     if (apr_date_checkmask(date, "## @$$ #### ##:##:## *")) {
177         /* RFC 1123 format with two days */
178         ds.tm_year = ((date[7] - '0') * 10 + (date[8] - '0') - 19) * 100;
179         if (ds.tm_year < 0)
180             return APR_DATE_BAD;
181
182         ds.tm_year += ((date[9] - '0') * 10) + (date[10] - '0');
183
184         ds.tm_mday = ((date[0] - '0') * 10) + (date[1] - '0');
185
186         monstr = date + 3;
187         timstr = date + 12;
188     }
189     else if (apr_date_checkmask(date, "##-@$$-## ##:##:## *")) { 
190         /* RFC 850 format */
191         ds.tm_year = ((date[7] - '0') * 10) + (date[8] - '0');
192         if (ds.tm_year < 70)
193             ds.tm_year += 100;
194
195         ds.tm_mday = ((date[0] - '0') * 10) + (date[1] - '0');
196
197         monstr = date + 3;
198         timstr = date + 10;
199     }
200     else if (apr_date_checkmask(date, "@$$ ~# ##:##:## ####*")) {
201         /* asctime format */
202         ds.tm_year = ((date[16] - '0') * 10 + (date[17] - '0') - 19) * 100;
203         if (ds.tm_year < 0) 
204             return APR_DATE_BAD;
205
206         ds.tm_year += ((date[18] - '0') * 10) + (date[19] - '0');
207
208         if (date[4] == ' ')
209             ds.tm_mday = 0;
210         else
211             ds.tm_mday = (date[4] - '0') * 10;
212
213         ds.tm_mday += (date[5] - '0');
214
215         monstr = date;
216         timstr = date + 7;
217     }
218     else if (apr_date_checkmask(date, "# @$$ #### ##:##:## *")) {
219         /* RFC 1123 format with one day */
220         ds.tm_year = ((date[6] - '0') * 10 + (date[7] - '0') - 19) * 100;
221         if (ds.tm_year < 0)
222             return APR_DATE_BAD;
223
224         ds.tm_year += ((date[8] - '0') * 10) + (date[9] - '0');
225
226         ds.tm_mday = (date[0] - '0');
227
228         monstr = date + 2;
229         timstr = date + 11;
230     }
231     else
232         return APR_DATE_BAD;
233
234     if (ds.tm_mday <= 0 || ds.tm_mday > 31)
235         return APR_DATE_BAD;
236
237     ds.tm_hour = ((timstr[0] - '0') * 10) + (timstr[1] - '0');
238     ds.tm_min = ((timstr[3] - '0') * 10) + (timstr[4] - '0');
239     ds.tm_sec = ((timstr[6] - '0') * 10) + (timstr[7] - '0');
240
241     if ((ds.tm_hour > 23) || (ds.tm_min > 59) || (ds.tm_sec > 61)) 
242         return APR_DATE_BAD;
243
244     mint = (monstr[0] << 16) | (monstr[1] << 8) | monstr[2];
245     for (mon = 0; mon < 12; mon++)
246         if (mint == months[mon])
247             break;
248
249     if (mon == 12)
250         return APR_DATE_BAD;
251
252     if ((ds.tm_mday == 31) && (mon == 3 || mon == 5 || mon == 8 || mon == 10))
253         return APR_DATE_BAD;
254
255     /* February gets special check for leapyear */
256     if ((mon == 1) &&
257         ((ds.tm_mday > 29) || 
258         ((ds.tm_mday == 29)
259         && ((ds.tm_year & 3)
260         || (((ds.tm_year % 100) == 0)
261         && (((ds.tm_year % 400) != 100)))))))
262         return APR_DATE_BAD;
263
264     ds.tm_mon = mon;
265
266     /* ap_mplode_time uses tm_usec and tm_gmtoff fields, but they haven't 
267      * been set yet. 
268      * It should be safe to just zero out these values.
269      * tm_usec is the number of microseconds into the second.  HTTP only
270      * cares about second granularity.
271      * tm_gmtoff is the number of seconds off of GMT the time is.  By
272      * definition all times going through this function are in GMT, so this
273      * is zero. 
274      */
275     ds.tm_usec = 0;
276     ds.tm_gmtoff = 0;
277     if (apr_time_exp_get(&result, &ds) != APR_SUCCESS) 
278         return APR_DATE_BAD;
279     
280     return result;
281 }
282
283 /*
284  * Parses a string resembling an RFC 822 date.  This is meant to be
285  * leinent in its parsing of dates.  Hence, this will parse a wider 
286  * range of dates than apr_date_parse_http.
287  *
288  * The prominent mailer (or poster, if mailer is unknown) that has
289  * been seen in the wild is included for the unknown formats.
290  *
291  *     Sun, 06 Nov 1994 08:49:37 GMT  ; RFC 822, updated by RFC 1123
292  *     Sunday, 06-Nov-94 08:49:37 GMT ; RFC 850, obsoleted by RFC 1036
293  *     Sun Nov  6 08:49:37 1994       ; ANSI C's asctime() format
294  *     Sun, 6 Nov 1994 08:49:37 GMT   ; RFC 822, updated by RFC 1123
295  *     Sun, 06 Nov 94 08:49:37 GMT    ; RFC 822
296  *     Sun, 6 Nov 94 08:49:37 GMT     ; RFC 822
297  *     Sun, 06 Nov 94 08:49 GMT       ; Unknown [drtr@ast.cam.ac.uk] 
298  *     Sun, 6 Nov 94 08:49 GMT        ; Unknown [drtr@ast.cam.ac.uk]
299  *     Sun, 06 Nov 94 8:49:37 GMT     ; Unknown [Elm 70.85]
300  *     Sun, 6 Nov 94 8:49:37 GMT      ; Unknown [Elm 70.85] 
301  *     Mon,  7 Jan 2002 07:21:22 GMT  ; Unknown [Postfix]
302  *     Sun, 06-Nov-1994 08:49:37 GMT  ; RFC 850 with four digit years
303  *
304  */
305
306 #define TIMEPARSE(ds,hr10,hr1,min10,min1,sec10,sec1)        \
307     {                                                       \
308         ds.tm_hour = ((hr10 - '0') * 10) + (hr1 - '0');     \
309         ds.tm_min = ((min10 - '0') * 10) + (min1 - '0');    \
310         ds.tm_sec = ((sec10 - '0') * 10) + (sec1 - '0');    \
311     }
312 #define TIMEPARSE_STD(ds,timstr)                            \
313     {                                                       \
314         TIMEPARSE(ds, timstr[0],timstr[1],                  \
315                       timstr[3],timstr[4],                  \
316                       timstr[6],timstr[7]);                 \
317     }
318
319 APU_DECLARE(apr_time_t) apr_date_parse_rfc(const char *date)
320 {
321     apr_time_exp_t ds;
322     apr_time_t result;
323     int mint, mon;
324     const char *monstr, *timstr, *gmtstr;
325     static const int months[12] =
326     {
327     ('J' << 16) | ('a' << 8) | 'n', ('F' << 16) | ('e' << 8) | 'b',
328     ('M' << 16) | ('a' << 8) | 'r', ('A' << 16) | ('p' << 8) | 'r',
329     ('M' << 16) | ('a' << 8) | 'y', ('J' << 16) | ('u' << 8) | 'n',
330     ('J' << 16) | ('u' << 8) | 'l', ('A' << 16) | ('u' << 8) | 'g',
331     ('S' << 16) | ('e' << 8) | 'p', ('O' << 16) | ('c' << 8) | 't',
332     ('N' << 16) | ('o' << 8) | 'v', ('D' << 16) | ('e' << 8) | 'c' };
333
334     if (!date)
335         return APR_DATE_BAD;
336
337     /* Not all dates have text months at the beginning. */
338     if (!apr_isdigit(date[0]))
339     {
340         while (*date && apr_isspace(*date)) /* Find first non-whitespace char */
341             ++date;
342
343         if (*date == '\0') 
344             return APR_DATE_BAD;
345
346         if ((date = strchr(date, ' ')) == NULL)   /* Find space after weekday */
347             return APR_DATE_BAD;
348
349         ++date;    /* Now pointing to first char after space, which should be */    }
350
351     /* start of the actual date information for all 11 formats. */
352     if (apr_date_checkmask(date, "## @$$ #### ##:##:## *")) {   /* RFC 1123 format */
353         ds.tm_year = ((date[7] - '0') * 10 + (date[8] - '0') - 19) * 100;
354
355         if (ds.tm_year < 0)
356             return APR_DATE_BAD;
357
358         ds.tm_year += ((date[9] - '0') * 10) + (date[10] - '0');
359
360         ds.tm_mday = ((date[0] - '0') * 10) + (date[1] - '0');
361
362         monstr = date + 3;
363         timstr = date + 12;
364         gmtstr = date + 20;
365
366         TIMEPARSE_STD(ds, timstr);
367     }
368     else if (apr_date_checkmask(date, "##-@$$-## ##:##:## *")) {/* RFC 850 format  */
369         ds.tm_year = ((date[7] - '0') * 10) + (date[8] - '0');
370
371         if (ds.tm_year < 70)
372             ds.tm_year += 100;
373
374         ds.tm_mday = ((date[0] - '0') * 10) + (date[1] - '0');
375
376         monstr = date + 3;
377         timstr = date + 10;
378         gmtstr = date + 19;
379
380         TIMEPARSE_STD(ds, timstr);
381     }
382     else if (apr_date_checkmask(date, "@$$ ~# ##:##:## ####*")) {
383         /* asctime format */
384         ds.tm_year = ((date[16] - '0') * 10 + (date[17] - '0') - 19) * 100;
385         if (ds.tm_year < 0) 
386             return APR_DATE_BAD;
387
388         ds.tm_year += ((date[18] - '0') * 10) + (date[19] - '0');
389
390         if (date[4] == ' ')
391             ds.tm_mday = 0;
392         else
393             ds.tm_mday = (date[4] - '0') * 10;
394
395         ds.tm_mday += (date[5] - '0');
396
397         monstr = date;
398         timstr = date + 7;
399         gmtstr = NULL;
400
401         TIMEPARSE_STD(ds, timstr);
402     }
403     else if (apr_date_checkmask(date, "# @$$ #### ##:##:## *")) {
404         /* RFC 1123 format*/
405         ds.tm_year = ((date[6] - '0') * 10 + (date[7] - '0') - 19) * 100;
406
407         if (ds.tm_year < 0)
408             return APR_DATE_BAD;
409
410         ds.tm_year += ((date[8] - '0') * 10) + (date[9] - '0');
411         ds.tm_mday = (date[0] - '0');
412
413         monstr = date + 2;
414         timstr = date + 11;
415         gmtstr = date + 20;
416
417         TIMEPARSE_STD(ds, timstr);
418     }
419     else if (apr_date_checkmask(date, "## @$$ ## ##:##:## *")) {
420         /* This is the old RFC 1123 date format - many many years ago, people
421          * used two-digit years.  Oh, how foolish.  */
422         ds.tm_year = ((date[7] - '0') * 10) + (date[8] - '0');
423
424         if (ds.tm_year < 70)
425             ds.tm_year += 100;
426
427         ds.tm_mday = ((date[0] - '0') * 10) + (date[1] - '0');
428
429         monstr = date + 3;
430         timstr = date + 10;
431         gmtstr = date + 19;
432
433         TIMEPARSE_STD(ds, timstr);
434     } 
435     else if (apr_date_checkmask(date, "# @$$ ## ##:##:## *")) {
436         /* This is the old RFC 1123 date format - many many years ago, people
437          * used two-digit years.  Oh, how foolish.  */
438         ds.tm_year = ((date[6] - '0') * 10) + (date[7] - '0');
439
440         if (ds.tm_year < 70)
441             ds.tm_year += 100;
442
443         ds.tm_mday = (date[0] - '0');
444
445         monstr = date + 2;
446         timstr = date + 9;
447         gmtstr = date + 18;
448
449         TIMEPARSE_STD(ds, timstr);
450     } 
451     else if (apr_date_checkmask(date, "## @$$ ## ##:## *")) {
452         /* Loser format.  This is quite bogus.  */
453         ds.tm_year = ((date[7] - '0') * 10) + (date[8] - '0');
454
455         if (ds.tm_year < 70)
456             ds.tm_year += 100;
457
458         ds.tm_mday = ((date[0] - '0') * 10) + (date[1] - '0');
459
460         monstr = date + 3;
461         timstr = date + 10;
462         gmtstr = NULL;
463
464         TIMEPARSE(ds, timstr[0],timstr[1], timstr[3],timstr[4], '0','0');
465     } 
466     else if (apr_date_checkmask(date, "# @$$ ## ##:## *")) {
467         /* Loser format.  This is quite bogus.  */
468         ds.tm_year = ((date[6] - '0') * 10) + (date[7] - '0');
469
470         if (ds.tm_year < 70)
471             ds.tm_year += 100;
472
473         ds.tm_mday = (date[0] - '0');
474
475         monstr = date + 2;
476         timstr = date + 9;
477         gmtstr = NULL;
478
479         TIMEPARSE(ds, timstr[0],timstr[1], timstr[3],timstr[4], '0','0');
480     }
481     else if (apr_date_checkmask(date, "## @$$ ## #:##:## *")) {
482         /* Loser format.  This is quite bogus.  */
483         ds.tm_year = ((date[7] - '0') * 10) + (date[8] - '0');
484
485         if (ds.tm_year < 70)
486             ds.tm_year += 100;
487
488         ds.tm_mday = ((date[0] - '0') * 10) + (date[1] - '0');
489
490         monstr = date + 3;
491         timstr = date + 9;
492         gmtstr = date + 18;
493
494         TIMEPARSE(ds, '0',timstr[1], timstr[3],timstr[4], timstr[6],timstr[7]);
495     }
496     else if (apr_date_checkmask(date, "# @$$ ## #:##:## *")) {
497          /* Loser format.  This is quite bogus.  */
498         ds.tm_year = ((date[6] - '0') * 10) + (date[7] - '0');
499
500         if (ds.tm_year < 70)
501             ds.tm_year += 100;
502
503         ds.tm_mday = (date[0] - '0');
504
505         monstr = date + 2;
506         timstr = date + 8;
507         gmtstr = date + 17;
508
509         TIMEPARSE(ds, '0',timstr[1], timstr[3],timstr[4], timstr[6],timstr[7]);
510     }
511     else if (apr_date_checkmask(date, " # @$$ #### ##:##:## *")) {   
512         /* RFC 1123 format with a space instead of a leading zero. */
513         ds.tm_year = ((date[7] - '0') * 10 + (date[8] - '0') - 19) * 100;
514
515         if (ds.tm_year < 0)
516             return APR_DATE_BAD;
517
518         ds.tm_year += ((date[9] - '0') * 10) + (date[10] - '0');
519
520         ds.tm_mday = (date[1] - '0');
521
522         monstr = date + 3;
523         timstr = date + 12;
524         gmtstr = date + 20;
525
526         TIMEPARSE_STD(ds, timstr);
527     }
528     else if (apr_date_checkmask(date, "##-@$$-#### ##:##:## *")) {
529        /* RFC 1123 with dashes instead of spaces between date/month/year
530         * This also looks like RFC 850 with four digit years.
531         */
532         ds.tm_year = ((date[7] - '0') * 10 + (date[8] - '0') - 19) * 100;
533         if (ds.tm_year < 0)
534             return APR_DATE_BAD;
535
536         ds.tm_year += ((date[9] - '0') * 10) + (date[10] - '0');
537
538         ds.tm_mday = ((date[0] - '0') * 10) + (date[1] - '0');
539
540         monstr = date + 3;
541         timstr = date + 12;
542         gmtstr = date + 21;
543
544         TIMEPARSE_STD(ds, timstr);
545     }
546     else
547         return APR_DATE_BAD;
548
549     if (ds.tm_mday <= 0 || ds.tm_mday > 31)
550         return APR_DATE_BAD;
551
552     if ((ds.tm_hour > 23) || (ds.tm_min > 59) || (ds.tm_sec > 61)) 
553         return APR_DATE_BAD;
554
555     mint = (monstr[0] << 16) | (monstr[1] << 8) | monstr[2];
556     for (mon = 0; mon < 12; mon++)
557         if (mint == months[mon])
558             break;
559
560     if (mon == 12)
561         return APR_DATE_BAD;
562
563     if ((ds.tm_mday == 31) && (mon == 3 || mon == 5 || mon == 8 || mon == 10))
564         return APR_DATE_BAD;
565
566     /* February gets special check for leapyear */
567
568     if ((mon == 1) &&
569         ((ds.tm_mday > 29)
570         || ((ds.tm_mday == 29)
571         && ((ds.tm_year & 3)
572         || (((ds.tm_year % 100) == 0)
573         && (((ds.tm_year % 400) != 100)))))))
574         return APR_DATE_BAD;
575
576     ds.tm_mon = mon;
577
578     /* tm_gmtoff is the number of seconds off of GMT the time is.
579      *
580      * We only currently support: [+-]ZZZZ where Z is the offset in
581      * hours from GMT.
582      *
583      * If there is any confusion, tm_gmtoff will remain 0.
584      */
585     ds.tm_gmtoff = 0;
586     if (gmtstr && *gmtstr != '\0') {
587         /* Do we have a GMT? */
588         if (*(++gmtstr) != '\0') {
589             int offset;
590             switch (*(gmtstr++)) {
591             case '-':
592                 offset = atoi(gmtstr);
593                 ds.tm_gmtoff -= (offset / 100) * 60 * 60;
594                 ds.tm_gmtoff -= (offset % 100) * 60;
595                 break;
596             case '+':
597                 offset = atoi(gmtstr);
598                 ds.tm_gmtoff += (offset / 100) * 60 * 60;
599                 ds.tm_gmtoff += (offset % 100) * 60;
600                 break;
601             }
602         }
603     }
604
605     /* apr_time_exp_get uses tm_usec field, but it hasn't been set yet. 
606      * It should be safe to just zero out this value.
607      * tm_usec is the number of microseconds into the second.  HTTP only
608      * cares about second granularity.
609      */
610     ds.tm_usec = 0;
611
612     if (apr_time_exp_gmt_get(&result, &ds) != APR_SUCCESS) 
613         return APR_DATE_BAD;
614     
615     return result;
616 }