Parse HTTP 'Date' entries in the `C' locale rather than the user's.

Fix from [bin/42917] (with minor changes), from KAMADA Ken'ichi.
This commit is contained in:
lukem 2010-03-04 21:40:53 +00:00
parent 029e3bd476
commit cbd600cfd6
3 changed files with 38 additions and 20 deletions

View File

@ -1,4 +1,4 @@
/* $NetBSD: extern.h,v 1.77 2009/07/13 19:05:41 roy Exp $ */ /* $NetBSD: extern.h,v 1.78 2010/03/04 21:40:53 lukem Exp $ */
/*- /*-
* Copyright (c) 1996-2009 The NetBSD Foundation, Inc. * Copyright (c) 1996-2009 The NetBSD Foundation, Inc.
@ -166,6 +166,7 @@ const char *onoff(int);
void opts(int, char **); void opts(int, char **);
void newer(int, char **); void newer(int, char **);
void page(int, char **); void page(int, char **);
const char *parse_rfc2616time(struct tm *, const char *);
int parserate(int, char **, int); int parserate(int, char **, int);
char *prompt(void); char *prompt(void);
void proxabort(int); void proxabort(int);

View File

@ -1,4 +1,4 @@
/* $NetBSD: fetch.c,v 1.191 2009/08/17 09:08:16 christos Exp $ */ /* $NetBSD: fetch.c,v 1.192 2010/03/04 21:40:53 lukem Exp $ */
/*- /*-
* Copyright (c) 1997-2009 The NetBSD Foundation, Inc. * Copyright (c) 1997-2009 The NetBSD Foundation, Inc.
@ -34,7 +34,7 @@
#include <sys/cdefs.h> #include <sys/cdefs.h>
#ifndef lint #ifndef lint
__RCSID("$NetBSD: fetch.c,v 1.191 2009/08/17 09:08:16 christos Exp $"); __RCSID("$NetBSD: fetch.c,v 1.192 2010/03/04 21:40:53 lukem Exp $");
#endif /* not lint */ #endif /* not lint */
/* /*
@ -934,28 +934,18 @@ fetch_url(const char *url, const char *proxyenv, char *proxyauth, char *wwwauth)
} else if (match_token(&cp, "Last-Modified:")) { } else if (match_token(&cp, "Last-Modified:")) {
struct tm parsed; struct tm parsed;
char *t; const char *t;
memset(&parsed, 0, sizeof(parsed)); memset(&parsed, 0, sizeof(parsed));
/* RFC1123 */ t = parse_rfc2616time(&parsed, cp);
if ((t = strptime(cp, if (t != NULL) {
"%a, %d %b %Y %H:%M:%S GMT",
&parsed))
/* RFC0850 */
|| (t = strptime(cp,
"%a, %d-%b-%y %H:%M:%S GMT",
&parsed))
/* asctime */
|| (t = strptime(cp,
"%a, %b %d %H:%M:%S %Y",
&parsed))) {
parsed.tm_isdst = -1; parsed.tm_isdst = -1;
if (*t == '\0') if (*t == '\0')
mtime = timegm(&parsed); mtime = timegm(&parsed);
#ifndef NO_DEBUG #ifndef NO_DEBUG
if (ftp_debug && mtime != -1) { if (ftp_debug && mtime != -1) {
fprintf(ttyout, fprintf(ttyout,
"parsed date as: %s", "parsed time as: %s",
rfc2822time(localtime(&mtime))); rfc2822time(localtime(&mtime)));
} }
#endif #endif

View File

@ -1,4 +1,4 @@
/* $NetBSD: util.c,v 1.152 2009/07/13 19:05:41 roy Exp $ */ /* $NetBSD: util.c,v 1.153 2010/03/04 21:40:53 lukem Exp $ */
/*- /*-
* Copyright (c) 1997-2009 The NetBSD Foundation, Inc. * Copyright (c) 1997-2009 The NetBSD Foundation, Inc.
@ -64,7 +64,7 @@
#include <sys/cdefs.h> #include <sys/cdefs.h>
#ifndef lint #ifndef lint
__RCSID("$NetBSD: util.c,v 1.152 2009/07/13 19:05:41 roy Exp $"); __RCSID("$NetBSD: util.c,v 1.153 2010/03/04 21:40:53 lukem Exp $");
#endif /* not lint */ #endif /* not lint */
/* /*
@ -85,6 +85,7 @@ __RCSID("$NetBSD: util.c,v 1.152 2009/07/13 19:05:41 roy Exp $");
#include <signal.h> #include <signal.h>
#include <libgen.h> #include <libgen.h>
#include <limits.h> #include <limits.h>
#include <locale.h>
#include <netdb.h> #include <netdb.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
@ -756,7 +757,7 @@ remotemodtime(const char *file, int noisy)
else else
goto cleanup_parse_time; goto cleanup_parse_time;
} else { } else {
DPRINTF("remotemodtime: parsed date `%s' as " LLF DPRINTF("remotemodtime: parsed time `%s' as " LLF
", %s", ", %s",
timestr, (LLT)rtime, timestr, (LLT)rtime,
rfc2822time(localtime(&rtime))); rfc2822time(localtime(&rtime)));
@ -791,6 +792,32 @@ rfc2822time(const struct tm *tm)
return result; return result;
} }
/*
* Parse HTTP-date as per RFC 2616.
* Return a pointer to the next character of the consumed date string,
* or NULL if failed.
*/
const char *
parse_rfc2616time(struct tm *parsed, const char *httpdate)
{
const char *t;
const char *curlocale;
/* The representation of %a depends on the current locale. */
curlocale = setlocale(LC_TIME, NULL);
(void)setlocale(LC_TIME, "C");
/* RFC1123 */
if ((t = strptime(httpdate, "%a, %d %b %Y %H:%M:%S GMT", parsed)) ||
/* RFC0850 */
(t = strptime(httpdate, "%a, %d-%b-%y %H:%M:%S GMT", parsed)) ||
/* asctime */
(t = strptime(httpdate, "%a, %b %d %H:%M:%S %Y", parsed))) {
; /* do nothing */
}
(void)setlocale(LC_TIME, curlocale);
return t;
}
/* /*
* Update global `localcwd', which contains the state of the local cwd * Update global `localcwd', which contains the state of the local cwd
*/ */