summaryrefslogtreecommitdiff
path: root/src/http_date.c
blob: 2018e6d3ce1d6668aa1bfe83430ae2139a2e39d5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
/*
 * 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"
#include <string.h>     /* strlen() */

#include "buffer.h"     /* light_isdigit() */
#include "log.h"        /* log_epoch_secs */

/**
 * 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"
 */


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.
     */
    static unix_time64_t tm_year_last_check;
    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;
        if (NULL != gmtime64_r(&log_epoch_secs, &tm_cur)) {
            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;

    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;

    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 *
http_date_str_to_tm (const char * const s, const uint32_t len,
                     struct tm * const tm)
{

    /* attempt strptime() using multiple date formats
     * support RFC 822,1123,7231; RFC 850; and ANSI C asctime() date strings,
     * 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.
     */

    /* 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
     * - 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 */
    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);
}


uint32_t
http_date_time_to_str (char * const s, const size_t sz, const unix_time64_t t)
{
    /*('max' is expected to be >= 30 (IMF-fixdate is 29 chars + '\0'))*/
    struct tm tm;
  #ifdef __MINGW32__
    const char fmt[] = "%a, %d %b %Y %H:%M:%S GMT"; /*IMF-fixdate fmt*/
  #else
    const char fmt[] = "%a, %d %b %Y %T GMT";       /*IMF-fixdate fmt*/
  #endif
    return (__builtin_expect( (NULL != gmtime64_r(&t, &tm)), 1))
      ? (uint32_t)strftime(s, sz, fmt, &tm)
      : 0;
}


int
http_date_if_modified_since (const char * const ifmod, const uint32_t ifmodlen,
                             const unix_time64_t lmtime)
{
    struct tm ifmodtm;
    if (NULL == http_date_str_to_tm(ifmod, ifmodlen, &ifmodtm))
        return 1; /* date parse error */
    const time_t ifmtime = timegm(&ifmodtm);
  #if HAS_TIME_BITS64
    return (lmtime > ifmtime);
  #else
    return (TIME64_CAST(lmtime) > TIME64_CAST(ifmtime) || ifmtime==(time_t)-1);
  #endif
    /* returns 0 if not modified since,
     * returns 1 if modified since or date parse error */
}