1
0
mirror of https://github.com/esp8266/Arduino.git synced 2025-04-21 10:26:06 +03:00

* Use RFC1123 format for date/time.

* UTC/localtime issue with If-Modified-Since header.
* Expires header added.


git-svn-id: svn://svn.code.sf.net/p/axtls/code/trunk@214 9a5d90b5-6617-0410-8a86-bb477d3ed2e3
This commit is contained in:
cameronrich 2011-07-15 23:36:46 +00:00
parent 15ab963e13
commit 9c517f6351
2 changed files with 32 additions and 17 deletions

View File

@ -41,6 +41,7 @@
#define HTTP_VERSION "HTTP/1.1" #define HTTP_VERSION "HTTP/1.1"
static const char * index_file = "index.html"; static const char * index_file = "index.html";
static const char * rfc1123_format = "%a, %d %b %Y %H:%M:%S GMT";
static int special_read(struct connstruct *cn, void *buf, size_t count); static int special_read(struct connstruct *cn, void *buf, size_t count);
static int special_write(struct connstruct *cn, static int special_write(struct connstruct *cn,
@ -373,8 +374,11 @@ void procsendhead(struct connstruct *cn)
{ {
char buf[MAXREQUESTLENGTH]; char buf[MAXREQUESTLENGTH];
struct stat stbuf; struct stat stbuf;
time_t now = cn->timeout - CONFIG_HTTP_TIMEOUT; time_t t_time;
struct tm *ptm;
char date[32]; char date[32];
char last_modified[32];
char expires[32];
int file_exists; int file_exists;
/* are we trying to access a file over the HTTP connection instead of a /* are we trying to access a file over the HTTP connection instead of a
@ -439,18 +443,27 @@ void procsendhead(struct connstruct *cn)
return; return;
} }
strcpy(date, ctime(&now));
time(&t_time);
ptm = gmtime(&t_time);
strftime(date, sizeof(date), rfc1123_format, ptm);
/* has the file been read before? */ /* has the file been read before? */
if (cn->if_modified_since != -1 && (cn->if_modified_since == 0 || if (cn->if_modified_since != -1)
cn->if_modified_since >= stbuf.st_mtime))
{
ptm = gmtime(&stbuf.st_mtime);
t_time = mktime(ptm);
if (cn->if_modified_since >= t_time)
{ {
snprintf(buf, sizeof(buf), HTTP_VERSION" 304 Not Modified\nServer: " snprintf(buf, sizeof(buf), HTTP_VERSION" 304 Not Modified\nServer: "
"%s\nDate: %s\n", server_version, date); "%s\nDate: %s\n\n", server_version, date);
special_write(cn, buf, strlen(buf)); special_write(cn, buf, strlen(buf));
cn->state = STATE_WANT_TO_READ_HEAD; cn->state = STATE_WANT_TO_READ_HEAD;
return; return;
} }
}
if (cn->reqtype == TYPE_HEAD) if (cn->reqtype == TYPE_HEAD)
{ {
@ -471,11 +484,17 @@ void procsendhead(struct connstruct *cn)
return; return;
} }
ptm = gmtime(&stbuf.st_mtime);
strftime(last_modified, sizeof(last_modified), rfc1123_format, ptm);
t_time += CONFIG_HTTP_TIMEOUT;
ptm = gmtime(&t_time);
strftime(expires, sizeof(expires), rfc1123_format, ptm);
snprintf(buf, sizeof(buf), HTTP_VERSION" 200 OK\nServer: %s\n" snprintf(buf, sizeof(buf), HTTP_VERSION" 200 OK\nServer: %s\n"
"Content-Type: %s\nContent-Length: %ld\n" "Content-Type: %s\nContent-Length: %ld\n"
"Date: %sLast-Modified: %s\n", server_version, "Date: %s\nLast-Modified: %s\nExpires: %s\n\n", server_version,
getmimetype(cn->actualfile), (long) stbuf.st_size, getmimetype(cn->actualfile), (long) stbuf.st_size,
date, ctime(&stbuf.st_mtime)); /* ctime() has a \n on the end */ date, last_modified, expires);
special_write(cn, buf, strlen(buf)); special_write(cn, buf, strlen(buf));

View File

@ -89,13 +89,9 @@ time_t tdate_parse(const char* str)
memset(&tm, 0, sizeof(struct tm)); memset(&tm, 0, sizeof(struct tm));
/* wdy, DD mth YY HH:MM:SS GMT */ /* wdy, DD mth YY HH:MM:SS GMT */
if ((sscanf(str, "%3[a-zA-Z], %d %3[a-zA-Z] %d %d:%d:%d GMT", if (sscanf(str, "%3[a-zA-Z], %d %3[a-zA-Z] %d %d:%d:%d GMT",
str_wday, &tm_mday, str_mon, &tm_year, &tm_hour, &tm_min, str_wday, &tm_mday, str_mon, &tm_year, &tm_hour, &tm_min,
&tm_sec) == 7) || &tm_sec) == 7)
/* wdy mth DD HH:MM:SS YY */
(sscanf(str, "%3[a-zA-Z] %3[a-zA-Z] %d %d:%d:%d %d",
str_wday, str_mon, &tm_mday, &tm_hour, &tm_min, &tm_sec,
&tm_year) == 7))
{ {
int8_t tm_wday = day_mon_map_search(str_wday, wday_tab, int8_t tm_wday = day_mon_map_search(str_wday, wday_tab,
sizeof(wday_tab)/sizeof(struct day_mon_map)); sizeof(wday_tab)/sizeof(struct day_mon_map));