callcgi: Remember to check for POLLERR and POLLHUP on the input socket.
[ashd.git] / src / sendfile.c
index 601661b..b3797aa 100644 (file)
@@ -26,6 +26,8 @@
 #include <stdint.h>
 #include <time.h>
 #include <magic.h>
+#include <locale.h>
+#include <langinfo.h>
 
 #ifdef HAVE_CONFIG_H
 #include <config.h>
@@ -65,11 +67,6 @@ static void passdata(int in, int out)
     free(buf);
 }
 
-static int strrcmp(char *str, char *end)
-{
-    return(strcmp(str + strlen(str) - strlen(end), end));
-}
-
 static char *attrmimetype(char *file)
 {
 #ifdef HAVE_XATTR
@@ -105,7 +102,7 @@ static const char *getmimetype(char *file, struct stat *sb)
     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)
@@ -113,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;
@@ -120,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);
     }
 }
@@ -141,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) {
@@ -172,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);