home *** CD-ROM | disk | FTP | other *** search
/ PC Online 1999 April / PCO0499.ISO / filesbbs / os2 / apach134.arj / APACH134.ZIP / src / main / util_date.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-01-01  |  10.1 KB  |  322 lines

  1. /* ====================================================================
  2.  * Copyright (c) 1996-1999 The Apache Group.  All rights reserved.
  3.  *
  4.  * Redistribution and use in source and binary forms, with or without
  5.  * modification, are permitted provided that the following conditions
  6.  * are met:
  7.  *
  8.  * 1. Redistributions of source code must retain the above copyright
  9.  *    notice, this list of conditions and the following disclaimer. 
  10.  *
  11.  * 2. Redistributions in binary form must reproduce the above copyright
  12.  *    notice, this list of conditions and the following disclaimer in
  13.  *    the documentation and/or other materials provided with the
  14.  *    distribution.
  15.  *
  16.  * 3. All advertising materials mentioning features or use of this
  17.  *    software must display the following acknowledgment:
  18.  *    "This product includes software developed by the Apache Group
  19.  *    for use in the Apache HTTP server project (http://www.apache.org/)."
  20.  *
  21.  * 4. The names "Apache Server" and "Apache Group" must not be used to
  22.  *    endorse or promote products derived from this software without
  23.  *    prior written permission. For written permission, please contact
  24.  *    apache@apache.org.
  25.  *
  26.  * 5. Products derived from this software may not be called "Apache"
  27.  *    nor may "Apache" appear in their names without prior written
  28.  *    permission of the Apache Group.
  29.  *
  30.  * 6. Redistributions of any form whatsoever must retain the following
  31.  *    acknowledgment:
  32.  *    "This product includes software developed by the Apache Group
  33.  *    for use in the Apache HTTP server project (http://www.apache.org/)."
  34.  *
  35.  * THIS SOFTWARE IS PROVIDED BY THE APACHE GROUP ``AS IS'' AND ANY
  36.  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  37.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  38.  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE APACHE GROUP OR
  39.  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  40.  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  41.  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  42.  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  43.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  44.  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  45.  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  46.  * OF THE POSSIBILITY OF SUCH DAMAGE.
  47.  * ====================================================================
  48.  *
  49.  * This software consists of voluntary contributions made by many
  50.  * individuals on behalf of the Apache Group and was originally based
  51.  * on public domain software written at the National Center for
  52.  * Supercomputing Applications, University of Illinois, Urbana-Champaign.
  53.  * For more information on the Apache Group and the Apache HTTP server
  54.  * project, please see <http://www.apache.org/>.
  55.  *
  56.  */
  57.  
  58. /*
  59.  * util_date.c: date parsing utility routines
  60.  *     These routines are (hopefully) platform-independent.
  61.  * 
  62.  * 27 Oct 1996  Roy Fielding
  63.  *     Extracted (with many modifications) from mod_proxy.c and
  64.  *     tested with over 50,000 randomly chosen valid date strings
  65.  *     and several hundred variations of invalid date strings.
  66.  * 
  67.  */
  68.  
  69. #include "ap_config.h"
  70. #include "util_date.h"
  71. #include <ctype.h>
  72. #include <string.h>
  73.  
  74. /*
  75.  * Compare a string to a mask
  76.  * Mask characters (arbitrary maximum is 256 characters, just in case):
  77.  *   @ - uppercase letter
  78.  *   $ - lowercase letter
  79.  *   & - hex digit
  80.  *   # - digit
  81.  *   ~ - digit or space
  82.  *   * - swallow remaining characters 
  83.  *  <x> - exact match for any other character
  84.  */
  85. API_EXPORT(int) ap_checkmask(const char *data, const char *mask)
  86. {
  87.     int i;
  88.     char d;
  89.  
  90.     for (i = 0; i < 256; i++) {
  91.     d = data[i];
  92.     switch (mask[i]) {
  93.     case '\0':
  94.         return (d == '\0');
  95.  
  96.     case '*':
  97.         return 1;
  98.  
  99.     case '@':
  100.         if (!ap_isupper(d))
  101.         return 0;
  102.         break;
  103.     case '$':
  104.         if (!ap_islower(d))
  105.         return 0;
  106.         break;
  107.     case '#':
  108.         if (!ap_isdigit(d))
  109.         return 0;
  110.         break;
  111.     case '&':
  112.         if (!isxdigit(d))
  113.         return 0;
  114.         break;
  115.     case '~':
  116.         if ((d != ' ') && !ap_isdigit(d))
  117.         return 0;
  118.         break;
  119.     default:
  120.         if (mask[i] != d)
  121.         return 0;
  122.         break;
  123.     }
  124.     }
  125.     return 0;            /* We only get here if mask is corrupted (exceeds 256) */
  126. }
  127.  
  128. /*
  129.  * tm2sec converts a GMT tm structure into the number of seconds since
  130.  * 1st January 1970 UT.  Note that we ignore tm_wday, tm_yday, and tm_dst.
  131.  * 
  132.  * The return value is always a valid time_t value -- (time_t)0 is returned
  133.  * if the input date is outside that capable of being represented by time(),
  134.  * i.e., before Thu, 01 Jan 1970 00:00:00 for all systems and 
  135.  * beyond 2038 for 32bit systems.
  136.  *
  137.  * This routine is intended to be very fast, much faster than mktime().
  138.  */
  139. API_EXPORT(time_t) ap_tm2sec(const struct tm * t)
  140. {
  141.     int year;
  142.     time_t days;
  143.     static const int dayoffset[12] =
  144.     {306, 337, 0, 31, 61, 92, 122, 153, 184, 214, 245, 275};
  145.  
  146.     year = t->tm_year;
  147.  
  148.     if (year < 70 || ((sizeof(time_t) <= 4) && (year >= 138)))
  149.     return BAD_DATE;
  150.  
  151.     /* shift new year to 1st March in order to make leap year calc easy */
  152.  
  153.     if (t->tm_mon < 2)
  154.     year--;
  155.  
  156.     /* Find number of days since 1st March 1900 (in the Gregorian calendar). */
  157.  
  158.     days = year * 365 + year / 4 - year / 100 + (year / 100 + 3) / 4;
  159.     days += dayoffset[t->tm_mon] + t->tm_mday - 1;
  160.     days -= 25508;        /* 1 jan 1970 is 25508 days since 1 mar 1900 */
  161.  
  162.     days = ((days * 24 + t->tm_hour) * 60 + t->tm_min) * 60 + t->tm_sec;
  163.  
  164.     if (days < 0)
  165.     return BAD_DATE;    /* must have overflowed */
  166.     else
  167.     return days;        /* must be a valid time */
  168. }
  169.  
  170. /*
  171.  * Parses an HTTP date in one of three standard forms:
  172.  *
  173.  *     Sun, 06 Nov 1994 08:49:37 GMT  ; RFC 822, updated by RFC 1123
  174.  *     Sunday, 06-Nov-94 08:49:37 GMT ; RFC 850, obsoleted by RFC 1036
  175.  *     Sun Nov  6 08:49:37 1994       ; ANSI C's asctime() format
  176.  *
  177.  * and returns the time_t number of seconds since 1 Jan 1970 GMT, or
  178.  * 0 if this would be out of range or if the date is invalid.
  179.  *
  180.  * The restricted HTTP syntax is
  181.  * 
  182.  *     HTTP-date    = rfc1123-date | rfc850-date | asctime-date
  183.  *
  184.  *     rfc1123-date = wkday "," SP date1 SP time SP "GMT"
  185.  *     rfc850-date  = weekday "," SP date2 SP time SP "GMT"
  186.  *     asctime-date = wkday SP date3 SP time SP 4DIGIT
  187.  *
  188.  *     date1        = 2DIGIT SP month SP 4DIGIT
  189.  *                    ; day month year (e.g., 02 Jun 1982)
  190.  *     date2        = 2DIGIT "-" month "-" 2DIGIT
  191.  *                    ; day-month-year (e.g., 02-Jun-82)
  192.  *     date3        = month SP ( 2DIGIT | ( SP 1DIGIT ))
  193.  *                    ; month day (e.g., Jun  2)
  194.  *
  195.  *     time         = 2DIGIT ":" 2DIGIT ":" 2DIGIT
  196.  *                    ; 00:00:00 - 23:59:59
  197.  *
  198.  *     wkday        = "Mon" | "Tue" | "Wed"
  199.  *                  | "Thu" | "Fri" | "Sat" | "Sun"
  200.  *
  201.  *     weekday      = "Monday" | "Tuesday" | "Wednesday"
  202.  *                  | "Thursday" | "Friday" | "Saturday" | "Sunday"
  203.  *
  204.  *     month        = "Jan" | "Feb" | "Mar" | "Apr"
  205.  *                  | "May" | "Jun" | "Jul" | "Aug"
  206.  *                  | "Sep" | "Oct" | "Nov" | "Dec"
  207.  *
  208.  * However, for the sake of robustness (and Netscapeness), we ignore the
  209.  * weekday and anything after the time field (including the timezone).
  210.  *
  211.  * This routine is intended to be very fast; 10x faster than using sscanf.
  212.  *
  213.  * Originally from Andrew Daviel <andrew@vancouver-webpages.com>, 29 Jul 96
  214.  * but many changes since then.
  215.  *
  216.  */
  217. API_EXPORT(time_t) ap_parseHTTPdate(const char *date)
  218. {
  219.     struct tm ds;
  220.     int mint, mon;
  221.     const char *monstr, *timstr;
  222.     static const int months[12] =
  223.     {
  224.     ('J' << 16) | ('a' << 8) | 'n', ('F' << 16) | ('e' << 8) | 'b',
  225.     ('M' << 16) | ('a' << 8) | 'r', ('A' << 16) | ('p' << 8) | 'r',
  226.     ('M' << 16) | ('a' << 8) | 'y', ('J' << 16) | ('u' << 8) | 'n',
  227.     ('J' << 16) | ('u' << 8) | 'l', ('A' << 16) | ('u' << 8) | 'g',
  228.     ('S' << 16) | ('e' << 8) | 'p', ('O' << 16) | ('c' << 8) | 't',
  229.     ('N' << 16) | ('o' << 8) | 'v', ('D' << 16) | ('e' << 8) | 'c'};
  230.  
  231.     if (!date)
  232.     return BAD_DATE;
  233.  
  234.     while (*date && ap_isspace(*date))    /* Find first non-whitespace char */
  235.     ++date;
  236.  
  237.     if (*date == '\0')
  238.     return BAD_DATE;
  239.  
  240.     if ((date = strchr(date, ' ')) == NULL)    /* Find space after weekday */
  241.     return BAD_DATE;
  242.  
  243.     ++date;            /* Now pointing to first char after space, which should be */
  244.     /* start of the actual date information for all 3 formats. */
  245.  
  246.     if (ap_checkmask(date, "## @$$ #### ##:##:## *")) {    /* RFC 1123 format */
  247.     ds.tm_year = ((date[7] - '0') * 10 + (date[8] - '0') - 19) * 100;
  248.     if (ds.tm_year < 0)
  249.         return BAD_DATE;
  250.  
  251.     ds.tm_year += ((date[9] - '0') * 10) + (date[10] - '0');
  252.  
  253.     ds.tm_mday = ((date[0] - '0') * 10) + (date[1] - '0');
  254.  
  255.     monstr = date + 3;
  256.     timstr = date + 12;
  257.     }
  258.     else if (ap_checkmask(date, "##-@$$-## ##:##:## *")) {        /* RFC 850 format  */
  259.     ds.tm_year = ((date[7] - '0') * 10) + (date[8] - '0');
  260.     if (ds.tm_year < 70)
  261.         ds.tm_year += 100;
  262.  
  263.     ds.tm_mday = ((date[0] - '0') * 10) + (date[1] - '0');
  264.  
  265.     monstr = date + 3;
  266.     timstr = date + 10;
  267.     }
  268.     else if (ap_checkmask(date, "@$$ ~# ##:##:## ####*")) {    /* asctime format  */
  269.     ds.tm_year = ((date[16] - '0') * 10 + (date[17] - '0') - 19) * 100;
  270.     if (ds.tm_year < 0)
  271.         return BAD_DATE;
  272.  
  273.     ds.tm_year += ((date[18] - '0') * 10) + (date[19] - '0');
  274.  
  275.     if (date[4] == ' ')
  276.         ds.tm_mday = 0;
  277.     else
  278.         ds.tm_mday = (date[4] - '0') * 10;
  279.  
  280.     ds.tm_mday += (date[5] - '0');
  281.  
  282.     monstr = date;
  283.     timstr = date + 7;
  284.     }
  285.     else
  286.     return BAD_DATE;
  287.  
  288.     if (ds.tm_mday <= 0 || ds.tm_mday > 31)
  289.     return BAD_DATE;
  290.  
  291.     ds.tm_hour = ((timstr[0] - '0') * 10) + (timstr[1] - '0');
  292.     ds.tm_min = ((timstr[3] - '0') * 10) + (timstr[4] - '0');
  293.     ds.tm_sec = ((timstr[6] - '0') * 10) + (timstr[7] - '0');
  294.  
  295.     if ((ds.tm_hour > 23) || (ds.tm_min > 59) || (ds.tm_sec > 61))
  296.     return BAD_DATE;
  297.  
  298.     mint = (monstr[0] << 16) | (monstr[1] << 8) | monstr[2];
  299.     for (mon = 0; mon < 12; mon++)
  300.     if (mint == months[mon])
  301.         break;
  302.     if (mon == 12)
  303.     return BAD_DATE;
  304.  
  305.     if ((ds.tm_mday == 31) && (mon == 3 || mon == 5 || mon == 8 || mon == 10))
  306.     return BAD_DATE;
  307.  
  308.     /* February gets special check for leapyear */
  309.  
  310.     if ((mon == 1) &&
  311.     ((ds.tm_mday > 29)
  312.      || ((ds.tm_mday == 29)
  313.          && ((ds.tm_year & 3)
  314.          || (((ds.tm_year % 100) == 0)
  315.              && (((ds.tm_year % 400) != 100)))))))
  316.     return BAD_DATE;
  317.  
  318.     ds.tm_mon = mon;
  319.  
  320.     return ap_tm2sec(&ds);
  321. }
  322.