From 86d0fd88c918f7cded4c864f646b6ff34c7bab9c Mon Sep 17 00:00:00 2001 From: Fredrik Tolf Date: Sat, 18 Jun 2011 15:29:33 +0200 Subject: [PATCH] Do If-Modified-Since caching. --- statserve.c | 30 ++++++++++++++++++++++++------ 1 file changed, 24 insertions(+), 6 deletions(-) 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: -- 2.11.0