From: Fredrik Tolf Date: Mon, 14 Dec 2009 16:08:14 +0000 (+0100) Subject: Use libmagic for sendfile. X-Git-Tag: 0.1~100 X-Git-Url: http://www.dolda2000.com/gitweb/?p=ashd.git;a=commitdiff_plain;h=8f728a255f0e887b2a4f6eb814a061c124381aa2 Use libmagic for sendfile. --- diff --git a/src/Makefile.am b/src/Makefile.am index 6218087..db9f833 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -7,3 +7,5 @@ debugsink_SOURCES = debugsink.c LDADD = $(top_srcdir)/lib/libht.a AM_CPPFLAGS = -I$(top_srcdir)/lib + +sendfile_LDADD = $(LDADD) -lmagic diff --git a/src/sendfile.c b/src/sendfile.c index 1bcc932..4ec25a6 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,8 @@ #include #include +static magic_t cookie = NULL; + static void passdata(int in, int out) { int ret, len, off; @@ -63,21 +66,16 @@ 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"); - if(!strrcmp(file, ".css")) - return("text/css"); - if(!strrcmp(file, ".py")) - return("text/plain"); - if(!strrcmp(file, ".c")) - 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"); } @@ -107,7 +105,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) {