X-Git-Url: http://git.dolda2000.com/gitweb/?a=blobdiff_plain;f=src%2Fsendfile.c;h=601661b0656ad650ec56ea08111d36f5f729ea36;hb=0328ac04990bf22e635930aa37abb8c2128a17f1;hp=1bcc932349a382962222bfff0547e77068a0a8e6;hpb=289c63fce39e82409004c8e5a7aa7757cc5ad3b6;p=ashd.git diff --git a/src/sendfile.c b/src/sendfile.c index 1bcc932..601661b 100644 --- a/src/sendfile.c +++ b/src/sendfile.c @@ -25,6 +25,7 @@ #include #include #include +#include #ifdef HAVE_CONFIG_H #include @@ -33,6 +34,12 @@ #include #include +#ifdef HAVE_XATTR +#include +#endif + +static magic_t cookie = NULL; + static void passdata(int in, int out) { int ret, len, off; @@ -63,21 +70,46 @@ static int strrcmp(char *str, char *end) return(strcmp(str + strlen(str) - strlen(end), end)); } -static char *getmimetype(char *file, struct stat *sb) +static char *attrmimetype(char *file) { - /* 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"); - if(!strrcmp(file, ".css")) - return("text/css"); - if(!strrcmp(file, ".py")) - return("text/plain"); - if(!strrcmp(file, ".c")) - return("text/plain"); +#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 const char *getmimetype(char *file, struct stat *sb) +{ + const char *ret; + + if((ret = attrmimetype(file)) != NULL) + return(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"); } @@ -107,7 +139,7 @@ int main(int argc, char **argv) char *file; struct stat sb; int fd; - char *contype; + const char *contype; contype = NULL; while((c = getopt(argc, argv, "c:")) >= 0) {