From: Fredrik Tolf Date: Sat, 18 Jun 2011 13:29:33 +0000 (+0200) Subject: Do If-Modified-Since caching. X-Git-Url: http://dolda2000.com/gitweb/?p=statserve.git;a=commitdiff_plain;h=86d0fd88c918f7cded4c864f646b6ff34c7bab9c Do If-Modified-Since caching. --- diff --git a/statserve.c b/statserve.c index 9bee73b..1801b95 100644 --- a/statserve.c +++ b/statserve.c @@ -23,6 +23,18 @@ static struct source *parsesource(char *arg) } } +static int cached(struct hthead *req, struct fileinfo f) +{ + char *hdr; + time_t cdate; + + if((hdr = getheader(req, "If-Modified-Since")) != NULL) { + cdate = parsehttpdate(hdr); + return((cdate > 0) && !(cdate < f.mtime)); + } + return(0); +} + static void serve(struct muth *muth, va_list args) { vavar(struct hthead *, req); @@ -42,12 +54,18 @@ static void serve(struct muth *muth, va_list args) goto out; } out = mtstdopen(fd, 1, 60, "r+"); - fprintf(out, "HTTP/1.1 200 OK\n"); - fprintf(out, "Content-Type: %s\n", f.ctype); - fprintf(out, "Content-Length: %zi\n", f.sz); - fprintf(out, "Last-Modified: %s\n", fmthttpdate(f.mtime)); - fprintf(out, "\n"); - fwrite(f.data, 1, f.sz, out); + if(cached(req, f)) { + fprintf(out, "HTTP/1.1 304 Not Modified\n"); + fprintf(out, "Content-Length: 0\n"); + fprintf(out, "\n"); + } else { + fprintf(out, "HTTP/1.1 200 OK\n"); + fprintf(out, "Content-Type: %s\n", f.ctype); + fprintf(out, "Content-Length: %zi\n", f.sz); + fprintf(out, "Last-Modified: %s\n", fmthttpdate(f.mtime)); + fprintf(out, "\n"); + fwrite(f.data, 1, f.sz, out); + } free(f.data); out: