Retry only for https protocol
[elinks.git] / src / protocol / date.c
blobf75489ede75f0688d35eeee0c0cb16b452680f16
1 /* Parser of HTTP date */
3 #ifdef HAVE_CONFIG_H
4 #include "config.h"
5 #endif
7 #include <stdlib.h>
8 #include <string.h>
10 #ifdef TIME_WITH_SYS_TIME
11 #ifdef HAVE_SYS_TIME_H
12 #include <sys/time.h>
13 #endif
14 #ifdef HAVE_TIME_H
15 #include <time.h>
16 #endif
17 #else
18 #if defined(TM_IN_SYS_TIME) && defined(HAVE_SYS_TIME_H)
19 #include <sys/time.h>
20 #elif defined(HAVE_TIME_H)
21 #include <time.h>
22 #endif
23 #endif
25 #include "elinks.h"
27 #include "protocol/date.h"
28 #include "util/conv.h"
29 #include "util/time.h"
32 * Sun, 06 Nov 1994 08:49:37 GMT ; RFC 822, updated by RFC 1123
33 * Sunday, 06-Nov-94 08:49:37 GMT ; RFC 850, obsoleted by RFC 1036
34 * Sun Nov 6 08:49:37 1994 ; ANSI C's asctime() format
37 int
38 parse_year(const unsigned char **date_p, unsigned char *end)
40 const unsigned char *date = *date_p;
41 int year;
43 if ((end && date + 1 >= end)
44 || !isdigit(date[0])
45 || !isdigit(date[1]))
46 return -1;
48 year = (date[0] - '0') * 10 + date[1] - '0';
50 if ((!end || date + 3 < end)
51 && isdigit(date[2])
52 && isdigit(date[3])) {
53 /* Four digits date */
54 year = year * 10 + date[2] - '0';
55 year = year * 10 + date[3] - '0' - 1900;
56 date += 4;
58 } else if (year < 70) {
59 /* Assuming the epoch starting at 1.1.1970 so it's already next
60 * century. --wget */
61 year += 100;
62 date += 2;
65 *date_p = date;
66 return year;
69 int
70 parse_month(const unsigned char **buf, unsigned char *end)
72 const unsigned char *month = *buf;
73 int monthnum;
75 /* Check that the buffer has atleast 3 chars. */
76 if ((end && month + 2 > end)
77 || !month[0] || !month[1] || !month[2])
78 return -1;
80 monthnum = month2num(month);
82 if (monthnum != -1)
83 *buf += 3;
85 return monthnum;
89 /* Return day number. */
90 int
91 parse_day(const unsigned char **date_p, unsigned char *end)
93 const unsigned char *date = *date_p;
94 int day;
96 if ((end && date >= end) || !isdigit(*date))
97 return 32;
98 day = *date++ - '0';
100 if ((!end || date < end) && isdigit(*date)) {
101 day = day * 10 + *date++ - '0';
104 *date_p = date;
105 return day;
109 parse_time(const unsigned char **time, struct tm *tm, unsigned char *end)
111 unsigned char h1, h2, m1, m2;
112 const unsigned char *date = *time;
114 #define check_time(tm) \
115 ((tm)->tm_hour <= 23 && (tm)->tm_min <= 59 && (tm)->tm_sec <= 59)
117 /* Eat HH:MM */
118 if (end && date + 5 > end)
119 return 0;
121 h1 = *date++; if (!isdigit(h1)) return 0;
122 h2 = *date++; if (!isdigit(h2)) return 0;
124 if (*date++ != ':') return 0;
126 m1 = *date++; if (!isdigit(m1)) return 0;
127 m2 = *date++; if (!isdigit(m2)) return 0;
129 tm->tm_hour = (h1 - '0') * 10 + h2 - '0';
130 tm->tm_min = (m1 - '0') * 10 + m2 - '0';
131 tm->tm_sec = 0;
133 /* Eat :SS or [PA]M or nothing */
134 if (end && date + 2 >= end) {
135 *time = date;
136 return check_time(tm);
139 if (*date == ':') {
140 unsigned char s1, s2;
142 date++;
144 if (end && date + 2 >= end)
145 return 0;
147 s1 = *date++; if (!isdigit(s1)) return 0;
148 s2 = *date++; if (!isdigit(s2)) return 0;
150 tm->tm_sec = (s1 - '0') * 10 + s2 - '0';
152 } else if (*date == 'A' || *date == 'P') {
153 /* Adjust hour from AM/PM. The sequence goes 11:00AM, 12:00PM,
154 * 01:00PM ... 11:00PM, 12:00AM, 01:00AM. --wget */
155 if (tm->tm_hour == 12)
156 tm->tm_hour = 0;
158 if (*date++ == 'P')
159 tm->tm_hour += 12;
161 if (*date++ != 'M')
162 return 0;
165 *time = date;
167 return check_time(tm);
171 static time_t
172 my_timegm(struct tm *tm)
174 time_t t = 0;
176 /* Okay, the next part of the code is somehow problematic. Now, we use
177 * own code for calculating the number of seconds from 1.1.1970,
178 * brought here by SC from w3m. I don't like it a lot, but it's 100%
179 * portable, it's faster and it's shorter. --pasky */
180 #if 0
181 #ifdef HAVE_TIMEGM
182 t = timegm(tm);
183 #else
184 /* Since mktime thinks we have localtime, we need a wrapper
185 * to handle GMT. */
186 /* FIXME: It was reported that it doesn't work somewhere :/. */
188 unsigned char *tz = getenv("TZ");
190 if (tz && *tz) {
191 /* Temporary disable timezone in-place. */
192 unsigned char tmp = *tz;
194 *tz = '\0';
195 tzset();
197 t = mktime(tm);
199 *tz = tmp;
200 tzset();
202 } else {
203 /* Already GMT, cool! */
204 t = mktime(tm);
207 #endif
208 #else
209 /* Following code was borrowed from w3m, and its developers probably
210 * borrowed it from somewhere else as well, altough they didn't bother
211 * to mention that. */ /* Actually, same code appears to be in lynx as
212 * well.. oh well. :) */
213 /* See README.timegm for explanation about how this works. */
214 tm->tm_mon -= 2;
215 if (tm->tm_mon < 0) {
216 tm->tm_mon += 12;
217 tm->tm_year--;
219 tm->tm_mon *= 153; tm->tm_mon += 2;
220 tm->tm_year -= 68;
222 /* Bug 924: Assumes all years divisible by 4 are leap years,
223 * even though e.g. 2100 is not. */
224 tm->tm_mday += tm->tm_year * 1461 / 4;
225 tm->tm_mday += ((tm->tm_mon / 5) - 672);
227 t = ((tm->tm_mday * 60 * 60 * 24) +
228 (tm->tm_hour * 60 * 60) +
229 (tm->tm_min * 60) +
230 tm->tm_sec);
231 #endif
233 if (t == (time_t) -1) return 0;
235 return t;
239 time_t
240 parse_date(unsigned char **date_pos, unsigned char *end,
241 int update_pos, int skip_week_day)
243 #define skip_time_sep(date, end) \
244 do { \
245 const unsigned char *start = (date); \
246 while ((!(end) || (date) < (end)) \
247 && (*(date) == ' ' || *(date) == '-')) \
248 (date)++; \
249 if (date == start) return 0; \
250 } while (0)
252 struct tm tm;
253 const unsigned char *date = (const unsigned char *) *date_pos;
255 if (!date) return 0;
257 if (skip_week_day) {
258 /* Skip day-of-week */
259 for (; (!end || date < end) && *date != ' '; date++)
260 if (!*date) return 0;
262 /* As pasky said who cares if we allow '-'s here? */
263 skip_time_sep(date, end);
266 if (isdigit(*date)) {
267 /* RFC 1036 / RFC 1123 */
269 /* Eat day */
271 /* date++; */
272 tm.tm_mday = parse_day(&date, end);
273 if (tm.tm_mday > 31) return 0;
275 skip_time_sep(date, end);
277 /* Eat month */
279 tm.tm_mon = parse_month(&date, end);
280 if (tm.tm_mon < 0) return 0;
282 skip_time_sep(date, end);
284 /* Eat year */
286 tm.tm_year = parse_year(&date, end);
287 if (tm.tm_year < 0) return 0;
289 skip_time_sep(date, end);
291 /* Eat time */
293 if (!parse_time(&date, &tm, end)) return 0;
295 } else {
296 /* ANSI C's asctime() format */
298 /* Eat month */
300 tm.tm_mon = parse_month(&date, end);
301 if (tm.tm_mon < 0) return 0;
303 /* I know, we shouldn't allow '-', but who cares ;). --pasky */
304 skip_time_sep(date, end);
306 /* Eat day */
308 tm.tm_mday = parse_day(&date, end);
309 if (tm.tm_mday > 31) return 0;
311 skip_time_sep(date, end);
313 /* Eat time */
315 if (!parse_time(&date, &tm, end)) return 0;
317 skip_time_sep(date, end);
319 /* Eat year */
321 tm.tm_year = parse_year(&date, end);
322 if (tm.tm_year < 0) return 0;
324 #undef skip_time_sep
326 if (update_pos)
327 *date_pos = (unsigned char *) date;
329 return (time_t) my_timegm(&tm);