X-Git-Url: http://git.dolda2000.com/gitweb/?a=blobdiff_plain;f=src%2Fsendfile.c;h=4ec25a60eaf2df89b041b6d8cad4998efc767884;hb=8f728a255f0e887b2a4f6eb814a061c124381aa2;hp=76c0ad4576224def27512c6e6b16e21f67b38a3b;hpb=01b2ebcef044b1aaea39a3ec3b5d3ccf0f08e444;p=ashd.git diff --git a/src/sendfile.c b/src/sendfile.c index 76c0ad4..4ec25a6 100644 --- a/src/sendfile.c +++ b/src/sendfile.c @@ -24,6 +24,8 @@ #include #include #include +#include +#include #ifdef HAVE_CONFIG_H #include @@ -32,6 +34,8 @@ #include #include +static magic_t cookie = NULL; + static void passdata(int in, int out) { int ret, len, off; @@ -62,33 +66,68 @@ static int strrcmp(char *str, char *end) return(strcmp(str + strlen(str) - strlen(end), end)); } -static char *getmimetype(char *file, struct stat *sb) +static const char *getmimetype(char *file, struct stat *sb) { - /* Rewrite with libmagic. */ - if(!strrcmp(file, ".html")) - return("text/html"); - if(!strrcmp(file, ".xhtml")) - return("application/xhtml+xml"); - if(!strrcmp(file, ".txt")) - return("text/plain"); + const char *ret; + + if(cookie == NULL) { + cookie = magic_open(MAGIC_MIME_TYPE); + magic_load(cookie, NULL); + } + if((ret = magic_file(cookie, file)) != NULL) + return(ret); return("application/octet-stream"); } +static void checkcache(char *file, struct stat *sb) +{ + char *hdr; + + if((hdr = getenv("REQ_IF_MODIFIED_SINCE")) != NULL) { + if(parsehttpdate(hdr) < sb->st_mtime) + return; + printf("HTTP/1.1 304 Not Modified\r\n"); + printf("Date: %s\r\n", fmthttpdate(time(NULL))); + printf("Content-Length: 0\r\n"); + printf("\r\n"); + exit(0); + } +} + +static void usage(void) +{ + flog(LOG_ERR, "usage: sendfile [-c CONTENT-TYPE] METHOD URL REST"); +} + int main(int argc, char **argv) { + int c; char *file; struct stat sb; int fd; + const char *contype; - if(argc < 4) { - flog(LOG_ERR, "usage: sendfile METHOD URL REST"); + contype = NULL; + while((c = getopt(argc, argv, "c:")) >= 0) { + switch(c) { + case 'c': + contype = optarg; + break; + default: + usage(); + exit(1); + break; + } + } + if(argc - optind < 3) { + usage(); exit(1); } if((file = getenv("REQ_X_ASH_FILE")) == NULL) { flog(LOG_ERR, "sendfile: needs to be called with the X-Ash-File header"); exit(1); } - if(*argv[3]) { + if(*argv[optind + 2]) { simpleerror(1, 404, "Not Found", "The requested URL has no corresponding resource."); exit(0); } @@ -97,13 +136,19 @@ int main(int argc, char **argv) simpleerror(1, 500, "Internal Error", "The server could not access its own data."); exit(1); } + if(contype == NULL) + contype = getmimetype(file, &sb); + + checkcache(file, &sb); printf("HTTP/1.1 200 OK\r\n"); - printf("Content-Type: %s\r\n", getmimetype(file, &sb)); + printf("Content-Type: %s\r\n", contype); printf("Content-Length: %ji\r\n", (intmax_t)sb.st_size); + printf("Last-Modified: %s\r\n", fmthttpdate(sb.st_mtime)); + printf("Date: %s\r\n", fmthttpdate(time(NULL))); printf("\r\n"); fflush(stdout); - if(strcasecmp(argv[1], "head")) + if(strcasecmp(argv[optind], "head")) passdata(fd, 1); return(0); }