2020-11-21 14:51:40 +00:00
|
|
|
/*
|
|
|
|
* http_date - HTTP date manipulation
|
|
|
|
*
|
|
|
|
* Copyright(c) 2015 Glenn Strauss gstrauss()gluelogic.com All rights reserved
|
|
|
|
* License: BSD 3-clause (same as lighttpd)
|
|
|
|
*/
|
|
|
|
#include "http_date.h"
|
|
|
|
|
|
|
|
#include "sys-time.h"
|
2020-11-21 15:42:10 +00:00
|
|
|
#include <string.h> /* strlen() */
|
|
|
|
|
|
|
|
#include "buffer.h" /* light_isdigit() */
|
|
|
|
#include "log.h" /* log_epoch_secs */
|
2020-11-21 14:51:40 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* https://tools.ietf.org/html/rfc7231
|
|
|
|
* [RFC7231] 7.1.1.1 Date/Time Formats
|
|
|
|
* Prior to 1995, there were three different formats commonly used by
|
|
|
|
* servers to communicate timestamps. For compatibility with old
|
|
|
|
* implementations, all three are defined here. The preferred format is
|
|
|
|
* a fixed-length and single-zone subset of the date and time
|
|
|
|
* specification used by the Internet Message Format [RFC5322].
|
|
|
|
* HTTP-date = IMF-fixdate / obs-date
|
|
|
|
* An example of the preferred format is
|
|
|
|
* Sun, 06 Nov 1994 08:49:37 GMT ; IMF-fixdate
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* (intended for use with strftime() and strptime())
|
|
|
|
* "%a, %d %b %Y %T GMT"
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2020-11-21 15:42:10 +00:00
|
|
|
static const char datestrs[] =
|
|
|
|
/*0 10 20 30 40 50 60 70 80 90*/
|
|
|
|
"\0\x0A\x14\x1E\x28\x32\x3c\x46\x50\x5A"
|
|
|
|
"SunMonTueWedThuFriSat"
|
|
|
|
"JanFebMarAprMayJunJulAugSepOctNovDec";
|
|
|
|
|
|
|
|
|
|
|
|
__attribute_cold__
|
|
|
|
static const char *
|
|
|
|
http_date_parse_RFC_850 (const char *s, struct tm * const tm)
|
|
|
|
{
|
|
|
|
/* RFC 7231 7.1.1.1.
|
|
|
|
* Recipients of a timestamp value in rfc850-date format, which uses a
|
|
|
|
* two-digit year, MUST interpret a timestamp that appears to be more
|
|
|
|
* than 50 years in the future as representing the most recent year in
|
|
|
|
* the past that had the same last two digits.
|
|
|
|
*/
|
2021-07-12 18:46:49 +00:00
|
|
|
static unix_time64_t tm_year_last_check;
|
2020-11-21 15:42:10 +00:00
|
|
|
static int tm_year_cur;
|
|
|
|
static int tm_year_base;
|
|
|
|
/* (log_epoch_secs is a global variable, maintained elsewhere) */
|
|
|
|
/* (optimization: check for year change no more than once per min) */
|
|
|
|
if (log_epoch_secs >= tm_year_last_check + 60) {
|
|
|
|
struct tm tm_cur;
|
2021-07-12 18:46:49 +00:00
|
|
|
if (NULL != gmtime64_r(&log_epoch_secs, &tm_cur)) {
|
2020-11-21 15:42:10 +00:00
|
|
|
tm_year_last_check = log_epoch_secs;
|
|
|
|
if (tm_cur.tm_year != tm_year_cur) {
|
|
|
|
tm_year_cur = tm_cur.tm_year;
|
|
|
|
tm_year_base = tm_year_cur - (tm_year_cur % 100);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Note: does not validate numerical ranges of
|
|
|
|
* tm_mday, tm_hour, tm_min, tm_sec */
|
|
|
|
/* Note: does not validate tm_wday beyond first three chars */
|
|
|
|
|
|
|
|
tm->tm_isdst = 0;
|
|
|
|
tm->tm_yday = 0;
|
|
|
|
tm->tm_wday = 0;
|
|
|
|
tm->tm_mon = 0;
|
|
|
|
|
|
|
|
const char * const tens = datestrs;
|
|
|
|
|
|
|
|
const char *p = tens + 10;
|
|
|
|
do {
|
|
|
|
if (s[0] == p[0] && s[1] == p[1] && s[2] == p[2]) break;
|
|
|
|
p += 3;
|
|
|
|
} while (++tm->tm_wday < 7);
|
|
|
|
if (7 == tm->tm_wday) return NULL;
|
|
|
|
|
|
|
|
s += 3;
|
|
|
|
while (*s != ',' && *s != '\0') ++s;
|
|
|
|
|
|
|
|
if (s[0] != ',' || s[1] != ' '
|
|
|
|
|| !light_isdigit(s[2]) || !light_isdigit(s[3]))
|
|
|
|
return NULL;
|
|
|
|
tm->tm_mday = tens[(s[2]-'0')] + (s[3]-'0');
|
|
|
|
|
|
|
|
if ( s[4] != '-') return NULL;
|
|
|
|
p = tens + 10 + sizeof("SunMonTueWedThuFriSat")-1;
|
|
|
|
do {
|
|
|
|
if (s[5] == p[0] && s[6] == p[1] && s[7] == p[2]) break;
|
|
|
|
p += 3;
|
|
|
|
} while (++tm->tm_mon < 12);
|
|
|
|
if (12 == tm->tm_mon) return NULL;
|
2020-11-21 14:51:40 +00:00
|
|
|
|
2020-11-21 15:42:10 +00:00
|
|
|
if (s[8] != '-' || !light_isdigit(s[9]) || !light_isdigit(s[10]))
|
|
|
|
return NULL;
|
|
|
|
tm->tm_year = tens[(s[9]-'0')] + (s[10]-'0') + tm_year_base;
|
|
|
|
if (tm->tm_year > tm_year_cur + 50) tm->tm_year -= 100;
|
2020-11-21 14:51:40 +00:00
|
|
|
|
2020-11-21 15:42:10 +00:00
|
|
|
if (s[11] != ' ' || !light_isdigit(s[12]) || !light_isdigit(s[13]))
|
|
|
|
return NULL;
|
|
|
|
tm->tm_hour = tens[(s[12]-'0')] + (s[13]-'0');
|
|
|
|
|
|
|
|
if (s[14] != ':' || !light_isdigit(s[15]) || !light_isdigit(s[16]))
|
|
|
|
return NULL;
|
|
|
|
tm->tm_min = tens[(s[15]-'0')] + (s[16]-'0');
|
|
|
|
|
|
|
|
if (s[17] != ':' || !light_isdigit(s[18]) || !light_isdigit(s[19]))
|
|
|
|
return NULL;
|
|
|
|
tm->tm_sec = tens[(s[18]-'0')] + (s[19]-'0');
|
|
|
|
|
|
|
|
if (s[20] != ' ' || s[21] != 'G' || s[22] != 'M' || s[23] != 'T')
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return s+24; /*(24 chars from ',' following the variable len wday)*/
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
__attribute_cold__
|
|
|
|
static const char *
|
|
|
|
http_date_parse_asctime (const char * const s, struct tm * const tm)
|
|
|
|
{
|
|
|
|
/* Note: does not validate numerical ranges of
|
|
|
|
* tm_mday, tm_hour, tm_min, tm_sec */
|
|
|
|
|
|
|
|
tm->tm_isdst = 0;
|
|
|
|
tm->tm_yday = 0;
|
|
|
|
tm->tm_wday = 0;
|
|
|
|
tm->tm_mon = 0;
|
|
|
|
|
|
|
|
const char * const tens = datestrs;
|
|
|
|
|
|
|
|
const char *p = tens + 10;
|
|
|
|
do {
|
|
|
|
if (s[0] == p[0] && s[1] == p[1] && s[2] == p[2]) break;
|
|
|
|
p += 3;
|
|
|
|
} while (++tm->tm_wday < 7);
|
|
|
|
if (7 == tm->tm_wday) return NULL;
|
|
|
|
|
|
|
|
if (s[3] != ' ') return NULL;
|
|
|
|
p = tens + 10 + sizeof("SunMonTueWedThuFriSat")-1;
|
|
|
|
do {
|
|
|
|
if (s[4] == p[0] && s[5] == p[1] && s[6] == p[2]) break;
|
|
|
|
p += 3;
|
|
|
|
} while (++tm->tm_mon < 12);
|
|
|
|
if (12 == tm->tm_mon) return NULL;
|
|
|
|
|
|
|
|
if (s[7] != ' ' || (s[8] != ' ' && !light_isdigit(s[8]))
|
|
|
|
|| !light_isdigit(s[9]))
|
|
|
|
return NULL;
|
|
|
|
tm->tm_mday = (s[8] == ' ' ? 0 : tens[(s[8]-'0')]) + (s[9]-'0');
|
|
|
|
|
|
|
|
if (s[10] != ' ' || !light_isdigit(s[11]) || !light_isdigit(s[12]))
|
|
|
|
return NULL;
|
|
|
|
tm->tm_hour = tens[(s[11]-'0')] + (s[12]-'0');
|
|
|
|
|
|
|
|
if (s[13] != ':' || !light_isdigit(s[14]) || !light_isdigit(s[15]))
|
|
|
|
return NULL;
|
|
|
|
tm->tm_min = tens[(s[14]-'0')] + (s[15]-'0');
|
|
|
|
|
|
|
|
if (s[16] != ':' || !light_isdigit(s[17]) || !light_isdigit(s[18]))
|
|
|
|
return NULL;
|
|
|
|
tm->tm_sec = tens[(s[17]-'0')] + (s[18]-'0');
|
|
|
|
|
|
|
|
if (s[19] != ' ' || !light_isdigit(s[20]) || !light_isdigit(s[21])
|
|
|
|
|| !light_isdigit(s[22]) || !light_isdigit(s[23])) return NULL;
|
|
|
|
tm->tm_year =(tens[(s[20]-'0')] + (s[21]-'0'))*100
|
|
|
|
+ tens[(s[22]-'0')] + (s[23]-'0') - 1900;
|
|
|
|
|
|
|
|
return s+24;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static const char *
|
|
|
|
http_date_parse_IMF_fixdate (const char * const s, struct tm * const tm)
|
|
|
|
{
|
|
|
|
/* Note: does not validate numerical ranges of
|
|
|
|
* tm_mday, tm_hour, tm_min, tm_sec */
|
|
|
|
|
|
|
|
tm->tm_isdst = 0;
|
|
|
|
tm->tm_yday = 0;
|
|
|
|
tm->tm_wday = 0;
|
|
|
|
tm->tm_mon = 0;
|
|
|
|
|
|
|
|
const char * const tens = datestrs;
|
|
|
|
|
|
|
|
const char *p = tens + 10;
|
|
|
|
do {
|
|
|
|
if (s[0] == p[0] && s[1] == p[1] && s[2] == p[2]) break;
|
|
|
|
p += 3;
|
|
|
|
} while (++tm->tm_wday < 7);
|
|
|
|
if (7 == tm->tm_wday) return NULL;
|
|
|
|
|
|
|
|
if (s[3] != ',' || s[4] != ' '
|
|
|
|
|| !light_isdigit(s[5]) || !light_isdigit(s[6]))
|
|
|
|
return NULL;
|
|
|
|
tm->tm_mday = tens[(s[5]-'0')] + (s[6]-'0');
|
|
|
|
|
|
|
|
if ( s[7] != ' ') return NULL;
|
|
|
|
p = tens + 10 + sizeof("SunMonTueWedThuFriSat")-1;
|
|
|
|
do {
|
|
|
|
if (s[8] == p[0] && s[9] == p[1] && s[10] == p[2]) break;
|
|
|
|
p += 3;
|
|
|
|
} while (++tm->tm_mon < 12);
|
|
|
|
if (12 == tm->tm_mon) return NULL;
|
|
|
|
|
|
|
|
if (s[11] != ' ' || !light_isdigit(s[12]) || !light_isdigit(s[13])
|
|
|
|
|| !light_isdigit(s[14]) || !light_isdigit(s[15])) return NULL;
|
|
|
|
tm->tm_year =(tens[(s[12]-'0')] + (s[13]-'0'))*100
|
|
|
|
+ tens[(s[14]-'0')] + (s[15]-'0') - 1900;
|
|
|
|
|
|
|
|
if (s[16] != ' ' || !light_isdigit(s[17]) || !light_isdigit(s[18]))
|
|
|
|
return NULL;
|
|
|
|
tm->tm_hour = tens[(s[17]-'0')] + (s[18]-'0');
|
|
|
|
|
|
|
|
if (s[19] != ':' || !light_isdigit(s[20]) || !light_isdigit(s[21]))
|
|
|
|
return NULL;
|
|
|
|
tm->tm_min = tens[(s[20]-'0')] + (s[21]-'0');
|
|
|
|
|
|
|
|
if (s[22] != ':' || !light_isdigit(s[23]) || !light_isdigit(s[24]))
|
|
|
|
return NULL;
|
|
|
|
tm->tm_sec = tens[(s[23]-'0')] + (s[24]-'0');
|
|
|
|
|
|
|
|
if (s[25] != ' ' || s[26] != 'G' || s[27] != 'M' || s[28] != 'T')
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return s+29;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static const char *
|
2020-11-22 07:41:11 +00:00
|
|
|
http_date_str_to_tm (const char * const s, const uint32_t len,
|
|
|
|
struct tm * const tm)
|
2020-11-21 14:51:40 +00:00
|
|
|
{
|
2020-11-22 07:41:11 +00:00
|
|
|
|
2020-11-21 14:51:40 +00:00
|
|
|
/* attempt strptime() using multiple date formats
|
2020-11-21 15:42:10 +00:00
|
|
|
* support RFC 822,1123,7231; RFC 850; and ANSI C asctime() date strings,
|
2020-11-21 14:51:40 +00:00
|
|
|
* as required by [RFC7231] https://tools.ietf.org/html/rfc7231#section-7.1
|
|
|
|
* [RFC7231] 7.1.1.1 Date/Time Formats
|
|
|
|
* HTTP-date = IMF-fixdate / obs-date
|
|
|
|
* [...]
|
|
|
|
* A recipient that parses a timestamp value in an HTTP header field
|
|
|
|
* MUST accept all three HTTP-date formats.
|
|
|
|
*/
|
|
|
|
|
2020-11-21 15:42:10 +00:00
|
|
|
/* employ specialized strptime()
|
|
|
|
* - HTTP expected date formats are known, so not needed as input param
|
|
|
|
* - HTTP expected date string content is in C locale and is case-sensitive
|
|
|
|
* - returns (const char *) instead of strptime() (char *) return type
|
2020-11-22 07:41:11 +00:00
|
|
|
* - returns NULL if error (if date string could not be parsed)
|
|
|
|
* Note: internal implementation requires '\0'-terminated string, or at
|
|
|
|
* least one valid char after partial match of RFC 850 or asctime formats */
|
2020-11-21 15:42:10 +00:00
|
|
|
if (len == 29)
|
|
|
|
return http_date_parse_IMF_fixdate(s, tm);
|
|
|
|
else if (len > 29)
|
|
|
|
return http_date_parse_RFC_850(s, tm);
|
|
|
|
else /* len < 29 */
|
|
|
|
return http_date_parse_asctime(s, tm);
|
2020-11-21 14:51:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-11-22 07:41:11 +00:00
|
|
|
uint32_t
|
2021-07-12 18:46:49 +00:00
|
|
|
http_date_time_to_str (char * const s, const size_t sz, const unix_time64_t t)
|
2020-11-21 14:51:40 +00:00
|
|
|
{
|
|
|
|
/*('max' is expected to be >= 30 (IMF-fixdate is 29 chars + '\0'))*/
|
|
|
|
struct tm tm;
|
2020-11-22 07:41:11 +00:00
|
|
|
const char fmt[] = "%a, %d %b %Y %T GMT"; /*IMF-fixdate fmt*/
|
2021-07-12 18:46:49 +00:00
|
|
|
return (__builtin_expect( (NULL != gmtime64_r(&t, &tm)), 1))
|
2020-11-22 07:41:11 +00:00
|
|
|
? (uint32_t)strftime(s, sz, fmt, &tm)
|
2020-11-21 14:51:40 +00:00
|
|
|
: 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
2020-11-22 07:41:11 +00:00
|
|
|
http_date_if_modified_since (const char * const ifmod, const uint32_t ifmodlen,
|
2021-07-12 18:46:49 +00:00
|
|
|
const unix_time64_t lmtime)
|
2020-11-21 14:51:40 +00:00
|
|
|
{
|
|
|
|
struct tm ifmodtm;
|
2020-11-22 07:41:11 +00:00
|
|
|
if (NULL == http_date_str_to_tm(ifmod, ifmodlen, &ifmodtm))
|
2020-11-21 14:51:40 +00:00
|
|
|
return 1; /* date parse error */
|
2021-04-26 16:50:20 +00:00
|
|
|
const time_t ifmtime = timegm(&ifmodtm);
|
2021-07-12 18:46:49 +00:00
|
|
|
#if HAS_TIME_BITS64
|
2020-11-21 14:51:40 +00:00
|
|
|
return (lmtime > ifmtime);
|
2021-07-12 18:46:49 +00:00
|
|
|
#else
|
|
|
|
return (TIME64_CAST(lmtime) > TIME64_CAST(ifmtime) || ifmtime==(time_t)-1);
|
|
|
|
#endif
|
2020-11-21 14:51:40 +00:00
|
|
|
/* returns 0 if not modified since,
|
|
|
|
* returns 1 if modified since or date parse error */
|
|
|
|
}
|