10 #include <ashd/mtio.h>
12 #include "statserve.h"
14 static struct source *sources = NULL;
16 static struct source *parsesource(char *arg)
18 if(arg[strlen(arg) - 1] == '/') {
21 return(mkdbsrc(arg, NULL));
25 static void serve(struct muth *muth, va_list args)
27 vavar(struct hthead *, req);
34 for(src = sources; src != NULL; src = src->next) {
35 f = src->serve(src, req->rest);
40 simpleerror(fd, 404, "Resource not found", "The resource %s was not found", htmlquote(req->rest));
43 out = mtstdopen(fd, 1, 60, "r+");
44 fprintf(out, "HTTP/1.1 200 OK\n");
45 fprintf(out, "Content-Type: %s\n", f.ctype);
46 fprintf(out, "Content-Length: %zi\n", f.sz);
47 fprintf(out, "Last-Modified: %s\n", fmthttpdate(f.mtime));
49 fwrite(f.data, 1, f.sz, out);
60 static void usage(FILE *out)
62 fprintf(out, "usage: statserve [-h] [-P PAGESIZE] SOURCE...\n");
65 static void listenloop(struct muth *muth, va_list args)
74 if((fd = recvreq(lfd, &req)) < 0) {
76 flog(LOG_ERR, "recvreq: %s", strerror(errno));
79 mustart(serve, req, fd);
80 for(src = sources; src != NULL; src = src->next) {
87 int main(int argc, char **argv)
90 struct source *last, *src;
92 while((c = getopt(argc, argv, "+hP:")) >= 0) {
95 dbpagesize = atoi(optarg);
106 while(optind < argc) {
107 src = parsesource(argv[optind++]);
118 mustart(listenloop, 0);