X-Git-Url: http://www.dolda2000.com/gitweb/?a=blobdiff_plain;f=src%2Fsendfile.c;h=b3797aaca88027cbd3776707b10a2dafc2807f18;hb=df96b2220cf1f738fdfc26236df1e91d5c89e078;hp=4ec25a60eaf2df89b041b6d8cad4998efc767884;hpb=8f728a255f0e887b2a4f6eb814a061c124381aa2;p=ashd.git diff --git a/src/sendfile.c b/src/sendfile.c index 4ec25a6..b3797aa 100644 --- a/src/sendfile.c +++ b/src/sendfile.c @@ -26,6 +26,8 @@ #include #include #include +#include +#include #ifdef HAVE_CONFIG_H #include @@ -34,6 +36,10 @@ #include #include +#ifdef HAVE_XATTR +#include +#endif + static magic_t cookie = NULL; static void passdata(int in, int out) @@ -61,17 +67,42 @@ 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 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); + cookie = magic_open(MAGIC_MIME_TYPE | MAGIC_SYMLINK); magic_load(cookie, NULL); } if((ret = magic_file(cookie, file)) != NULL) @@ -79,6 +110,15 @@ static const char *getmimetype(char *file, struct stat *sb) 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; @@ -86,10 +126,10 @@ static void checkcache(char *file, struct stat *sb) 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"); + printf("HTTP/1.1 304 Not Modified\n"); + printf("Date: %s\n", fmthttpdate(time(NULL))); + printf("Content-Length: 0\n"); + printf("\n"); exit(0); } } @@ -107,6 +147,7 @@ int main(int argc, char **argv) int fd; const char *contype; + setlocale(LC_ALL, ""); contype = NULL; while((c = getopt(argc, argv, "c:")) >= 0) { switch(c) { @@ -138,15 +179,16 @@ int main(int argc, char **argv) } 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", 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"); + 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[optind], "head")) passdata(fd, 1);