X-Git-Url: http://www.dolda2000.com/gitweb/?a=blobdiff_plain;f=src%2Fsendfile.c;h=b3797aaca88027cbd3776707b10a2dafc2807f18;hb=19409e874c34c5e442dc4cefe2d08981d4412c6c;hp=76c0ad4576224def27512c6e6b16e21f67b38a3b;hpb=01b2ebcef044b1aaea39a3ec3b5d3ccf0f08e444;p=ashd.git diff --git a/src/sendfile.c b/src/sendfile.c index 76c0ad4..b3797aa 100644 --- a/src/sendfile.c +++ b/src/sendfile.c @@ -24,6 +24,10 @@ #include #include #include +#include +#include +#include +#include #ifdef HAVE_CONFIG_H #include @@ -32,6 +36,12 @@ #include #include +#ifdef HAVE_XATTR +#include +#endif + +static magic_t cookie = NULL; + static void passdata(int in, int out) { int ret, len, off; @@ -57,38 +67,108 @@ static void passdata(int in, int out) free(buf); } -static int strrcmp(char *str, char *end) +static char *attrmimetype(char *file) { - return(strcmp(str + strlen(str) - strlen(end), end)); +#ifdef HAVE_XATTR + static char buf[1024]; + int i; + ssize_t sz; + + if((sz = getxattr(file, "user.ash-mime-type", buf, sizeof(buf) - 1)) > 0) + goto found; + if((sz = getxattr(file, "user.mime-type", buf, sizeof(buf) - 1)) > 0) + goto found; + if((sz = getxattr(file, "user.mime_type", buf, sizeof(buf) - 1)) > 0) + goto found; + if((sz = getxattr(file, "user.Content-Type", buf, sizeof(buf) - 1)) > 0) + goto found; + return(NULL); +found: + for(i = 0; i < sz; i++) { + if((buf[sz] < 32) || (buf[sz] >= 128)) + return(NULL); + } + buf[sz] = 0; + return(buf); +#else + return(NULL); +#endif } -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((ret = attrmimetype(file)) != NULL) + return(ret); + if(cookie == NULL) { + cookie = magic_open(MAGIC_MIME_TYPE | MAGIC_SYMLINK); + magic_load(cookie, NULL); + } + if((ret = magic_file(cookie, file)) != NULL) + return(ret); return("application/octet-stream"); } +/* XXX: This could be made far better and check for other attributes + * and stuff, but not now. */ +static const char *ckctype(const char *ctype) +{ + if(!strncmp(ctype, "text/", 5) && (strchr(ctype, ';') == NULL)) + return(sprintf2("%s; charset=%s", ctype, nl_langinfo(CODESET))); + return(ctype); +} + +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\n"); + printf("Date: %s\n", fmthttpdate(time(NULL))); + printf("Content-Length: 0\n"); + printf("\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"); + setlocale(LC_ALL, ""); + 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 +177,20 @@ 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); + contype = ckctype(contype); + + checkcache(file, &sb); - printf("HTTP/1.1 200 OK\r\n"); - printf("Content-Type: %s\r\n", getmimetype(file, &sb)); - printf("Content-Length: %ji\r\n", (intmax_t)sb.st_size); - printf("\r\n"); + printf("HTTP/1.1 200 OK\n"); + printf("Content-Type: %s\n", contype); + printf("Content-Length: %ji\n", (intmax_t)sb.st_size); + printf("Last-Modified: %s\n", fmthttpdate(sb.st_mtime)); + printf("Date: %s\n", fmthttpdate(time(NULL))); + printf("\n"); fflush(stdout); - if(strcasecmp(argv[1], "head")) + if(strcasecmp(argv[optind], "head")) passdata(fd, 1); return(0); }