]> git.ipfire.org Git - thirdparty/squid.git/blob - lib/rfc1123.c
DW:
[thirdparty/squid.git] / lib / rfc1123.c
1
2 /*
3 * $Id: rfc1123.c,v 1.26 2000/11/07 23:37:35 wessels Exp $
4 *
5 * DEBUG:
6 * AUTHOR: Harvest Derived
7 *
8 * SQUID Internet Object Cache http://squid.nlanr.net/Squid/
9 * ----------------------------------------------------------
10 *
11 * Squid is the result of efforts by numerous individuals from the
12 * Internet community. Development is led by Duane Wessels of the
13 * National Laboratory for Applied Network Research and funded by the
14 * National Science Foundation. Squid is Copyrighted (C) 1998 by
15 * the Regents of the University of California. Please see the
16 * COPYRIGHT file for full details. Squid incorporates software
17 * developed and/or copyrighted by other sources. Please see the
18 * CREDITS file for full details.
19 *
20 * This program is free software; you can redistribute it and/or modify
21 * it under the terms of the GNU General Public License as published by
22 * the Free Software Foundation; either version 2 of the License, or
23 * (at your option) any later version.
24 *
25 * This program is distributed in the hope that it will be useful,
26 * but WITHOUT ANY WARRANTY; without even the implied warranty of
27 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28 * GNU General Public License for more details.
29 *
30 * You should have received a copy of the GNU General Public License
31 * along with this program; if not, write to the Free Software
32 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
33 *
34 */
35
36 #include "config.h"
37
38
39 /*
40 * Adapted from HTSUtils.c in CERN httpd 3.0 (http://info.cern.ch/httpd/)
41 * by Darren Hardy <hardy@cs.colorado.edu>, November 1994.
42 */
43 #if HAVE_STDIO_H
44 #include <stdio.h>
45 #endif
46 #if HAVE_STDLIB_H
47 #include <stdlib.h>
48 #endif
49 #if HAVE_STRING_H
50 #include <string.h>
51 #endif
52 #if HAVE_CTYPE_H
53 #include <ctype.h>
54 #endif
55 #if HAVE_SYS_TYPES_H
56 #include <sys/types.h>
57 #endif
58 #if HAVE_TIME_H
59 #include <time.h>
60 #endif
61 #if HAVE_SYS_TIME_H
62 #include <sys/time.h>
63 #endif
64 #include "assert.h"
65
66 #include "util.h"
67 #include "snprintf.h"
68
69 #define RFC850_STRFTIME "%A, %d-%b-%y %H:%M:%S GMT"
70 #define RFC1123_STRFTIME "%a, %d %b %Y %H:%M:%S GMT"
71
72 static const char *const w_space = " \t\r\n";
73
74 static int make_month(const char *s);
75 static int make_num(const char *s);
76
77 static char *month_names[12] =
78 {
79 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
80 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
81 };
82
83
84 static int
85 make_num(const char *s)
86 {
87 if (*s >= '0' && *s <= '9')
88 return 10 * (*s - '0') + *(s + 1) - '0';
89 else
90 return *(s + 1) - '0';
91 }
92
93 static int
94 make_month(const char *s)
95 {
96 int i;
97 char month[3];
98
99 month[0] = xtoupper(*s);
100 month[1] = xtolower(*(s + 1));
101 month[2] = xtolower(*(s + 2));
102
103 for (i = 0; i < 12; i++)
104 if (!strncmp(month_names[i], month, 3))
105 return i;
106 return 0;
107 }
108
109 static int
110 tmSaneValues(struct tm *tm)
111 {
112 if (tm->tm_sec < 0 || tm->tm_sec > 59)
113 return 0;
114 if (tm->tm_min < 0 || tm->tm_min > 59)
115 return 0;
116 if (tm->tm_hour < 0 || tm->tm_hour > 23)
117 return 0;
118 if (tm->tm_mday < 1 || tm->tm_mday > 31)
119 return 0;
120 if (tm->tm_mon < 0 || tm->tm_mon > 11)
121 return 0;
122 if (tm->tm_year < 70 || tm->tm_year > 120)
123 return 0;
124 return 1;
125 }
126
127 static struct tm *
128 parse_date1(const char *str)
129 {
130 /* Thursday, 10-Jun-93 01:29:59 GMT */
131 const char *s;
132 static struct tm tm;
133 assert(NULL != str);
134 memset(&tm, '\0', sizeof(struct tm));
135 s = strchr(str, ',');
136 if (NULL == s)
137 return NULL;
138 while (*s == ' ')
139 s++;
140 /* backup if month is only one digit */
141 if (xisdigit(*s) && !xisdigit(*(s + 1)))
142 s--;
143 if (!strchr(s, '-'))
144 return NULL;
145 if ((int) strlen(s) < 18)
146 return NULL;
147 memset(&tm, '\0', sizeof(tm));
148 tm.tm_mday = make_num(s);
149 tm.tm_mon = make_month(s + 3);
150 tm.tm_year = make_num(s + 7);
151 /*
152 * Y2K: Arjan de Vet <Arjan.deVet@adv.IAEhv.nl>
153 * if tm.tm_year < 70, assume it's after the year 2000.
154 */
155 if (tm.tm_year < 70)
156 tm.tm_year += 100;
157 tm.tm_hour = make_num(s + 10);
158 tm.tm_min = make_num(s + 13);
159 tm.tm_sec = make_num(s + 16);
160 return tmSaneValues(&tm) ? &tm : NULL;
161 }
162
163 static struct tm *
164 parse_date2(const char *str)
165 {
166 /* Thu, 10 Jan 1993 01:29:59 GMT */
167 const char *s;
168 struct tm tm;
169 assert(NULL != str);
170 memset(&tm, '\0', sizeof(struct tm));
171 s = strchr(str, ',');
172 if (NULL == s)
173 return NULL;
174 while (*s == ' ')
175 s++;
176 /* backup if month is only one digit */
177 if (xisdigit(*s) && !xisdigit(*(s + 1)))
178 s--;
179 if (strchr(s, '-'))
180 return NULL;
181 if ((int) strlen(s) < 20)
182 return NULL;
183 memset(&tm, '\0', sizeof(tm));
184 tm.tm_mday = make_num(s);
185 tm.tm_mon = make_month(s + 3);
186 tm.tm_year = (100 * make_num(s + 7) - 1900) + make_num(s + 9);
187 tm.tm_hour = make_num(s + 12);
188 tm.tm_min = make_num(s + 15);
189 tm.tm_sec = make_num(s + 18);
190 return tmSaneValues(&tm) ? &tm : NULL;
191 }
192
193 static struct tm *
194 parse_date3(const char *str)
195 {
196 /* Wed Jun 9 01:29:59 1993 GMT */
197 static struct tm tm;
198 char *s;
199 static char buf[128];
200 while (*str && *str == ' ')
201 str++;
202 xstrncpy(buf, str, 128);
203 if (NULL == (s = strtok(buf, w_space)))
204 return NULL;
205 if (NULL == (s = strtok(NULL, w_space)))
206 return NULL;
207 tm.tm_mon = make_month(s);
208 if (NULL == (s = strtok(NULL, w_space)))
209 return NULL;
210 tm.tm_mday = atoi(s);
211 if (NULL == (s = strtok(NULL, ":")))
212 return NULL;
213 tm.tm_hour = atoi(s);
214 if (NULL == (s = strtok(NULL, ":")))
215 return NULL;
216 tm.tm_min = atoi(s);
217 if (NULL == (s = strtok(NULL, w_space)))
218 return NULL;
219 tm.tm_sec = atoi(s);
220 if (NULL == (s = strtok(NULL, w_space)))
221 return NULL;
222 /* Y2K fix, richard.kettlewell@kewill.com */
223 tm.tm_year = atoi(s) - 1900;
224 return tmSaneValues(&tm) ? &tm : NULL;
225 }
226
227 time_t
228 parse_rfc1123(const char *str)
229 {
230 struct tm *tm;
231 time_t t;
232 if (NULL == str)
233 return -1;
234 tm = parse_date1(str);
235 if (NULL == tm) {
236 tm = parse_date2(str);
237 if (NULL == tm) {
238 tm = parse_date3(str);
239 if (NULL == tm)
240 return -1;
241 }
242 }
243 tm->tm_isdst = -1;
244 #ifdef HAVE_TIMEGM
245 t = timegm(tm);
246 #elif HAVE_TM_GMTOFF
247 t = mktime(tm);
248 {
249 struct tm *local = localtime(&t);
250 t += local->tm_gmtoff;
251 }
252 #else
253 /* some systems do not have tm_gmtoff so we fake it */
254 t = mktime(tm);
255 {
256 time_t dst = 0;
257 #if defined (_TIMEZONE)
258 #elif defined (_timezone)
259 #elif defined(_SQUID_AIX_)
260 #elif defined(_SQUID_CYGWIN_)
261 #else
262 extern time_t timezone;
263 #endif
264 /*
265 * The following assumes a fixed DST offset of 1 hour,
266 * which is probably wrong.
267 */
268 if (tm->tm_isdst > 0)
269 dst = -3600;
270 #if defined ( _timezone) || defined(_SQUID_CYGWIN_)
271 t -= (_timezone + dst);
272 #else
273 t -= (timezone + dst);
274 #endif
275 }
276 #endif
277 return t;
278 }
279
280 const char *
281 mkrfc1123(time_t t)
282 {
283 static char buf[128];
284
285 struct tm *gmt = gmtime(&t);
286
287 buf[0] = '\0';
288 strftime(buf, 127, RFC1123_STRFTIME, gmt);
289 return buf;
290 }
291
292 const char *
293 mkhttpdlogtime(const time_t * t)
294 {
295 static char buf[128];
296
297 struct tm *gmt = gmtime(t);
298
299 #ifndef USE_GMT
300 int gmt_min, gmt_hour, gmt_yday, day_offset;
301 size_t len;
302 struct tm *lt;
303 int min_offset;
304
305 /* localtime & gmtime may use the same static data */
306 gmt_min = gmt->tm_min;
307 gmt_hour = gmt->tm_hour;
308 gmt_yday = gmt->tm_yday;
309
310 lt = localtime(t);
311
312 day_offset = lt->tm_yday - gmt_yday;
313 /* wrap round on end of year */
314 if (day_offset > 1)
315 day_offset = -1;
316 else if (day_offset < -1)
317 day_offset = 1;
318
319 min_offset = day_offset * 1440 + (lt->tm_hour - gmt_hour) * 60
320 + (lt->tm_min - gmt_min);
321
322 len = strftime(buf, 127 - 5, "%d/%b/%Y:%H:%M:%S ", lt);
323 snprintf(buf + len, 128 - len, "%+03d%02d",
324 (min_offset / 60) % 24,
325 min_offset % 60);
326 #else /* USE_GMT */
327 buf[0] = '\0';
328 strftime(buf, 127, "%d/%b/%Y:%H:%M:%S -000", gmt);
329 #endif /* USE_GMT */
330
331 return buf;
332 }
333
334 #if 0
335 int
336 main()
337 {
338 char *x;
339 time_t t, pt;
340
341 t = time(NULL);
342 x = mkrfc1123(t);
343 printf("HTTP Time: %s\n", x);
344
345 pt = parse_rfc1123(x);
346 printf("Parsed: %d vs. %d\n", pt, t);
347 }
348
349 #endif