Added some caching capabilities to sendfile.
authorFredrik Tolf <fredrik@dolda2000.com>
Tue, 23 Dec 2008 00:35:19 +0000 (01:35 +0100)
committerFredrik Tolf <fredrik@dolda2000.com>
Tue, 23 Dec 2008 00:35:19 +0000 (01:35 +0100)
lib/resp.c
lib/resp.h
src/sendfile.c

index 1271d2c..b6c91ae 100644 (file)
@@ -20,6 +20,8 @@
 #include <string.h>
 #include <stdio.h>
 #include <stdarg.h>
+#include <time.h>
+#include <regex.h>
 
 #ifdef HAVE_CONFIG_H
 #include <config.h>
@@ -80,3 +82,82 @@ void simpleerror(int fd, int code, char *msg, char *fmt, ...)
     fclose(out);
     buffree(buf);
 }
+
+char *fmthttpdate(time_t time)
+{
+    /* I avoid using strftime, since it depends on locale settings. */
+    static char *days[] = {"Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"};
+    static char *months[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
+                            "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
+    struct tm *tm;
+    
+    tm = gmtime(&time);
+    return(sprintf3("%s, %i %s %i %02i:%02i:%02i GMT", days[(tm->tm_wday + 6) % 7], tm->tm_mday, months[tm->tm_mon], tm->tm_year + 1900, tm->tm_hour, tm->tm_min, tm->tm_sec));
+}
+
+time_t parsehttpdate(char *date)
+{
+    static regex_t *spec = NULL;
+    static char *months[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
+                            "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
+    int i;
+    regmatch_t g[11];
+    struct tm tm;
+    int tz;
+    
+    int gtoi(regmatch_t g)
+    {
+       int i, n;
+
+       for(i = g.rm_so, n = 0; i < g.rm_eo; i++)
+           n = (n * 10) + (date[i] - '0');
+       return(n);
+    }
+    
+    int gstrcmp(regmatch_t g, char *str) {
+       if(g.rm_eo - g.rm_so != strlen(str))
+           return(1);
+       return(strncasecmp(date + g.rm_so, str, g.rm_eo - g.rm_so));
+    }
+
+    if(spec == NULL) {
+       omalloc(spec);
+       if(regcomp(spec, "^[A-Z]{3}, +([0-9]+) +([A-Z]{3}) +([0-9]+) +([0-9]{2}):([0-9]{2}):([0-9]{2}) +(([A-Z]+)|[+-]([0-9]{2})([0-9]{2}))$", REG_EXTENDED | REG_ICASE)) {
+           free(spec);
+           spec = NULL;
+           return(0);
+       }
+    }
+    if(regexec(spec, date, 11, g, 0))
+       return(0);
+    tm.tm_mday = gtoi(g[1]);
+    tm.tm_year = gtoi(g[3]) - 1900;
+    tm.tm_hour = gtoi(g[4]);
+    tm.tm_min = gtoi(g[5]);
+    tm.tm_sec = gtoi(g[6]);
+    
+    tm.tm_mon = -1;
+    for(i = 0; i < 12; i++) {
+       if(!gstrcmp(g[2], months[i])) {
+           tm.tm_mon = i;
+           break;
+       }
+    }
+    if(tm.tm_mon < 0)
+       return(0);
+    
+    if(g[8].rm_so > 0) {
+       if(!gstrcmp(g[8], "GMT"))
+           tz = 0;
+       else
+           return(0);
+    } else if((g[9].rm_so > 0) && (g[10].rm_so > 0)) {
+       tz = gtoi(g[9]) * 3600 + gtoi(g[10]) * 60;
+       if(date[g[7].rm_so] == '-')
+           tz = -tz;
+    } else {
+       return(0);
+    }
+    
+    return(timegm(&tm) - tz);
+}
index a1f01f3..90771dc 100644 (file)
@@ -2,5 +2,7 @@
 #define _LIB_HTRESP_H
 
 void simpleerror(int fd, int code, char *msg, char *fmt, ...);
+char *fmthttpdate(time_t time);
+time_t parsehttpdate(char *date);
 
 #endif
index 76c0ad4..f12e33e 100644 (file)
@@ -24,6 +24,7 @@
 #include <errno.h>
 #include <sys/stat.h>
 #include <stdint.h>
+#include <time.h>
 
 #ifdef HAVE_CONFIG_H
 #include <config.h>
@@ -71,9 +72,28 @@ static char *getmimetype(char *file, struct stat *sb)
        return("application/xhtml+xml");
     if(!strrcmp(file, ".txt"))
        return("text/plain");
+    if(!strrcmp(file, ".py"))
+       return("text/plain");
+    if(!strrcmp(file, ".c"))
+       return("text/plain");
     return("application/octet-stream");
 }
 
+static void checkcache(char *file, struct stat *sb)
+{
+    char *hdr;
+    
+    if((hdr = getenv("REQ_IF_MODIFIED_SINCE")) != NULL) {
+       if(parsehttpdate(hdr) < sb->st_mtime)
+           return;
+       printf("HTTP/1.1 304 Not Modified\r\n");
+       printf("Date: %s\r\n", fmthttpdate(time(NULL)));
+       printf("Content-Length: 0\r\n");
+       printf("\r\n");
+       exit(0);
+    }
+}
+
 int main(int argc, char **argv)
 {
     char *file;
@@ -98,9 +118,13 @@ int main(int argc, char **argv)
        exit(1);
     }
     
+    checkcache(file, &sb);
+    
     printf("HTTP/1.1 200 OK\r\n");
     printf("Content-Type: %s\r\n", getmimetype(file, &sb));
     printf("Content-Length: %ji\r\n", (intmax_t)sb.st_size);
+    printf("Last-Modified: %s\r\n", fmthttpdate(sb.st_mtime));
+    printf("Date: %s\r\n", fmthttpdate(time(NULL)));
     printf("\r\n");
     fflush(stdout);
     if(strcasecmp(argv[1], "head"))