#include <sys/stat.h>
#include <stdint.h>
#include <time.h>
+#include <magic.h>
#ifdef HAVE_CONFIG_H
#include <config.h>
#include <log.h>
#include <resp.h>
+#ifdef HAVE_XATTR
+#include <attr/xattr.h>
+#endif
+
+static magic_t cookie = NULL;
+
static void passdata(int in, int out)
{
int ret, len, off;
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");
}
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);
}
}
char *file;
struct stat sb;
int fd;
- char *contype;
+ const char *contype;
contype = NULL;
while((c = getopt(argc, argv, "c:")) >= 0) {
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);