X-Git-Url: http://dolda2000.com/gitweb/?a=blobdiff_plain;f=statserve.c;h=1801b95b3e632392870b4fb5a779971a02f1febf;hb=86d0fd88c918f7cded4c864f646b6ff34c7bab9c;hp=ab3b3467bc0db6aba64b4207a20a4b116c0633e2;hpb=ea4e0b7174a8372eaa3b9519e7ca98e6ba78c6d7;p=statserve.git diff --git a/statserve.c b/statserve.c index ab3b346..1801b95 100644 --- a/statserve.c +++ b/statserve.c @@ -3,6 +3,7 @@ #include #include #include +#include #include #include #include @@ -22,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); @@ -41,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: @@ -84,6 +103,21 @@ static void listenloop(struct muth *muth, va_list args) } } +static void sigterm(int sig) +{ + shutdown(0, SHUT_RDWR); +} + +static void closeall(void) +{ + struct source *src; + + for(src = sources; src != NULL; src = src->next) { + if(src->close) + src->close(src); + } +} + int main(int argc, char **argv) { int c; @@ -104,7 +138,10 @@ int main(int argc, char **argv) } last = NULL; while(optind < argc) { - src = parsesource(argv[optind++]); + if((src = parsesource(argv[optind++])) == NULL) { + closeall(); + return(1); + } if(!sources) sources = src; if(last) @@ -113,9 +150,13 @@ int main(int argc, char **argv) } if(!sources) { usage(stderr); + closeall(); return(1); } mustart(listenloop, 0); + signal(SIGINT, sigterm); + signal(SIGTERM, sigterm); ioloop(); + closeall(); return(0); }