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 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398
|
/*
* The contents of this file are subject to the AOLserver Public License
* Version 1.1 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
* http://aolserver.com/.
*
* Software distributed under the License is distributed on an "AS IS"
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
* the License for the specific language governing rights and limitations
* under the License.
*
* The Original Code is AOLserver Code and related documentation
* distributed by AOL.
*
* The Initial Developer of the Original Code is America Online,
* Inc. Portions created by AOL are Copyright (C) 1999 America Online,
* Inc. All Rights Reserved.
*
* Alternatively, the contents of this file may be used under the terms
* of the GNU General Public License (the "GPL"), in which case the
* provisions of GPL are applicable instead of those above. If you wish
* to allow use of your version of this file only under the terms of the
* GPL and not to allow others to use your version of this file under the
* License, indicate your decision by deleting the provisions above and
* replace them with the notice and other provisions required by the GPL.
* If you do not delete the provisions above, a recipient may use your
* version of this file under either the License or the GPL.
*/
static char *weekdays_names[7] =
{ "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
/*
* time.c --
*
* Manipulate times and dates; this is strongly influenced
* by HTSUtils.c from CERN. See also RFC 1123.
*/
static const char *RCSID = "@(#) $Header: /cvsroot/aolserver/aolserver/nsd/httptime.c,v 1.9 2003/01/18 19:24:20 jgdavidson Exp $, compiled: " __DATE__ " " __TIME__;
#include "nsd.h"
/*
* Local functions defined in this file
*/
static int MakeNum(char *s);
static int MakeMonth(char *s);
/*
* Static variables defined in this file
*/
static char *month_names[12] =
{
"Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
};
/*
*----------------------------------------------------------------------
*
* Ns_Httptime --
*
* Convert a time_t into a time/date format used in HTTP
* (see RFC 1123). If passed-in time is null, then the
* current time will be used.
*
* Results:
* The string time, or NULL if error.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
char *
Ns_HttpTime(Ns_DString *pds, time_t *when)
{
time_t now;
char buf[40];
struct tm *tmPtr;
if (when == NULL) {
now = time(0);
when = &now;
}
tmPtr = ns_gmtime(when);
if (tmPtr == NULL) {
return NULL;
}
/*
* Using snprintf instead of strftime to always use english names
* The format is RFC 1123: "Sun, 06 Nov 1997 09:12:45 GMT"
*/
snprintf(buf, 40, "%s, %d %s %d %02d:%02d:%02d GMT",
weekdays_names[tmPtr->tm_wday], tmPtr->tm_mday,
month_names[tmPtr->tm_mon], tmPtr->tm_year + 1900,
tmPtr->tm_hour, tmPtr->tm_min, tmPtr->tm_sec);
Ns_DStringAppend(pds, buf);
return pds->string;
}
/*
*----------------------------------------------------------------------
*
* Ns_ParseHttpTime --
*
* Take a time in one of three formats and convert it to a time_t.
* Formats are: "Thursday, 10-Jun-93 01:29:59 GMT", "Thu, 10
* Jan 1993 01:29:59 GMT", or "Wed Jun 9 01:29:59 1993 GMT"
*
* Results:
* 0 if error, or standard time_t.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
time_t
Ns_ParseHttpTime(char *str)
{
char *s;
struct tm tm;
time_t t;
if (str == NULL) {
return 0;
}
/*
* Find the comma after day-of-week
*
* Thursday, 10-Jun-93 01:29:59 GMT
* ^
* +-- s
*
* Thu, 10 Jan 1993 01:29:59 GMT
* ^
* +-- s
*/
s = strchr(str, ',');
if (s != NULL) {
/*
* Advance S to the first non-space after the comma
* which should be the first digit of the day.
*/
s++;
while (*s && *s == ' ') {
s++;
}
/*
* Figure out which format it is in. If there is a hyphen, then
* it must be the first format.
*/
if (strchr(s, '-') != NULL) {
if (strlen(s) < 18) {
return 0;
}
/*
* The format is:
*
* Thursday, 10-Jun-93 01:29:59 GMT
* ^
* +--s
*/
tm.tm_mday = MakeNum(s);
tm.tm_mon = MakeMonth(s + 3);
tm.tm_year = MakeNum(s + 7);
tm.tm_hour = MakeNum(s + 10);
tm.tm_min = MakeNum(s + 13);
tm.tm_sec = MakeNum(s + 16);
} else {
if ((int) strlen(s) < 20) {
return 0;
}
/*
* The format is:
*
* Thu, 10 Jan 1993 01:29:59 GMT
* ^
* +--s
*/
tm.tm_mday = MakeNum(s);
tm.tm_mon = MakeMonth(s + 3);
tm.tm_year = (100 * MakeNum(s + 7) - 1900) + MakeNum(s + 9);
tm.tm_hour = MakeNum(s + 12);
tm.tm_min = MakeNum(s + 15);
tm.tm_sec = MakeNum(s + 18);
}
} else {
/*
* No commas, so it must be the third, fixed field, format:
*
* Wed Jun 9 01:29:59 1993 GMT
*
* Advance s to the first letter of the month.
*/
s = str;
while (*s && *s == ' ') {
s++;
}
if ((int) strlen(s) < 24) {
return 0;
}
tm.tm_mday = MakeNum(s + 8);
tm.tm_mon = MakeMonth(s + 4);
tm.tm_year = MakeNum(s + 22);
tm.tm_hour = MakeNum(s + 11);
tm.tm_min = MakeNum(s + 14);
tm.tm_sec = MakeNum(s + 17);
}
/*
* If there are any impossible values, then return an error.
*/
if (tm.tm_sec < 0 || tm.tm_sec > 59 ||
tm.tm_min < 0 || tm.tm_min > 59 ||
tm.tm_hour < 0 || tm.tm_hour > 23 ||
tm.tm_mday < 1 || tm.tm_mday > 31 ||
tm.tm_mon < 0 || tm.tm_mon > 11 ||
tm.tm_year < 70 || tm.tm_year > 120) {
return 0;
}
tm.tm_isdst = 0;
#ifdef HAVE_TIMEGM
t = timegm(&tm);
#else
t = mktime(&tm) - timezone;
#endif
return t;
}
/*
*----------------------------------------------------------------------
*
* NsTclParseHttpTimeObjCmd --
*
* Implements ns_parsehttptime as obj command.
*
* Results:
* Tcl result.
*
* Side effects:
* See docs.
*
*----------------------------------------------------------------------
*/
int
NsTclParseHttpTimeObjCmd(ClientData arg, Tcl_Interp *interp, int objc, Tcl_Obj *CONST objv[])
{
time_t time;
if (objc != 2) {
Tcl_WrongNumArgs(interp, 1, objv, "httptime");
return TCL_ERROR;
}
time = Ns_ParseHttpTime(Tcl_GetString(objv[1]));
if (time == 0) {
Tcl_AppendResult(interp, "invalid time: ",
Tcl_GetString(objv[1]), NULL);
return TCL_ERROR;
}
Tcl_SetLongObj(Tcl_GetObjResult(interp), time);
return TCL_OK;
}
/*
*----------------------------------------------------------------------
*
* NsTclHttpTimeObjCmd --
*
* Implements ns_httptime as obj command.
*
* Results:
* Tcl result.
*
* Side effects:
* See docs.
*
*----------------------------------------------------------------------
*/
int
NsTclHttpTimeObjCmd(ClientData arg, Tcl_Interp *interp, int objc, Tcl_Obj *CONST objv[])
{
Ns_DString ds;
int itime;
time_t time;
if (objc != 2) {
Tcl_WrongNumArgs(interp, 1, objv, "time");
return TCL_ERROR;
}
if (Tcl_GetIntFromObj(interp, objv[1], &itime) != TCL_OK) {
return TCL_ERROR;
}
time = (time_t) itime;
Ns_DStringInit(&ds);
Ns_HttpTime(&ds, &time);
Tcl_SetResult(interp, Ns_DStringExport(&ds), (Tcl_FreeProc *) ns_free);
Ns_DStringFree(&ds);
return TCL_OK;
}
/*
*----------------------------------------------------------------------
*
* MakeNum --
*
* Convert a one or two-digit day into an integer, allowing a
* space in the first position.
*
* Results:
* An integer.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
static int
MakeNum(char *s)
{
if (*s >= '0' && *s <= '9') {
return (10 * (*s - '0')) + (*(s + 1) - '0');
} else {
return *(s + 1) - '0';
}
}
/*
*----------------------------------------------------------------------
*
* MakeMonth --
*
* Convert a three-digit abbreviated month name into a number;
* e.g., Jan=0, Feb=1, etc.
*
* Results:
* An integral month number.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
static int
MakeMonth(char *s)
{
int i;
/*
* Make sure it's capitalized like this:
* "Jan"
*/
*s = toupper(*s);
*(s + 1) = tolower(*(s + 1));
*(s + 2) = tolower(*(s + 2));
for (i = 0; i < 12; i++) {
if (!strncmp(month_names[i], s, 3)) {
return i;
}
}
return 0;
}
|