}
}
+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);
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: