X-Git-Url: http://dolda2000.com/gitweb/?a=blobdiff_plain;f=statserve.c;h=1801b95b3e632392870b4fb5a779971a02f1febf;hb=86d0fd88c918f7cded4c864f646b6ff34c7bab9c;hp=ed4938822e93c60d9acfe95f82ae5cc839e22055;hpb=34d725a595138f81ba84d67d7b84766c3a270d9f;p=statserve.git diff --git a/statserve.c b/statserve.c index ed49388..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: @@ -59,7 +78,7 @@ out: static void usage(FILE *out) { - fprintf(out, "usage: statserve [-h] SOURCE...\n"); + fprintf(out, "usage: statserve [-h] [-P PAGESIZE] SOURCE...\n"); } static void listenloop(struct muth *muth, va_list args) @@ -84,13 +103,31 @@ 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; struct source *last, *src; - while((c = getopt(argc, argv, "+h")) >= 0) { + while((c = getopt(argc, argv, "+hP:")) >= 0) { switch(c) { + case 'P': + dbpagesize = atoi(optarg); + break; case 'h': usage(stdout); return(0); @@ -101,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) @@ -110,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); }