Added xattr mime-type detection to sendfile.
[ashd.git] / src / sendfile.c
1 /*
2     ashd - A Sane HTTP Daemon
3     Copyright (C) 2008  Fredrik Tolf <fredrik@dolda2000.com>
4
5     This program is free software: you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation, either version 3 of the License, or
8     (at your option) any later version.
9
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14
15     You should have received a copy of the GNU General Public License
16     along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 #include <stdlib.h>
20 #include <string.h>
21 #include <stdio.h>
22 #include <unistd.h>
23 #include <fcntl.h>
24 #include <errno.h>
25 #include <sys/stat.h>
26 #include <stdint.h>
27 #include <time.h>
28 #include <magic.h>
29
30 #ifdef HAVE_CONFIG_H
31 #include <config.h>
32 #endif
33 #include <utils.h>
34 #include <log.h>
35 #include <resp.h>
36
37 #ifdef HAVE_XATTR
38 #include <attr/xattr.h>
39 #endif
40
41 static magic_t cookie = NULL;
42
43 static void passdata(int in, int out)
44 {
45     int ret, len, off;
46     char *buf;
47     
48     buf = smalloc(65536);
49     while(1) {
50         len = read(in, buf, 65536);
51         if(len < 0) {
52             flog(LOG_ERR, "sendfile: could not read input: %s", strerror(errno));
53             break;
54         }
55         if(len == 0)
56             break;
57         for(off = 0; off < len; off += ret) {
58             ret = write(out, buf + off, len - off);
59             if(ret < 0) {
60                 flog(LOG_ERR, "sendfile: could not write output: %s", strerror(errno));
61                 break;
62             }
63         }
64     }
65     free(buf);
66 }
67
68 static int strrcmp(char *str, char *end)
69 {
70     return(strcmp(str + strlen(str) - strlen(end), end));
71 }
72
73 static char *attrmimetype(char *file)
74 {
75 #ifdef HAVE_XATTR
76     static char buf[1024];
77     int i;
78     ssize_t sz;
79     
80     if((sz = getxattr(file, "user.ash-mime-type", buf, sizeof(buf) - 1)) > 0)
81         goto found;
82     if((sz = getxattr(file, "user.mime-type", buf, sizeof(buf) - 1)) > 0)
83         goto found;
84     if((sz = getxattr(file, "user.mime_type", buf, sizeof(buf) - 1)) > 0)
85         goto found;
86     if((sz = getxattr(file, "user.Content-Type", buf, sizeof(buf) - 1)) > 0)
87         goto found;
88     return(NULL);
89 found:
90     for(i = 0; i < sz; i++) {
91         if((buf[sz] < 32) || (buf[sz] >= 128))
92             return(NULL);
93     }
94     buf[sz] = 0;
95     return(buf);
96 #else
97     return(NULL);
98 #endif
99 }
100
101 static const char *getmimetype(char *file, struct stat *sb)
102 {
103     const char *ret;
104     
105     if((ret = attrmimetype(file)) != NULL)
106         return(ret);
107     if(cookie == NULL) {
108         cookie = magic_open(MAGIC_MIME_TYPE);
109         magic_load(cookie, NULL);
110     }
111     if((ret = magic_file(cookie, file)) != NULL)
112         return(ret);
113     return("application/octet-stream");
114 }
115
116 static void checkcache(char *file, struct stat *sb)
117 {
118     char *hdr;
119     
120     if((hdr = getenv("REQ_IF_MODIFIED_SINCE")) != NULL) {
121         if(parsehttpdate(hdr) < sb->st_mtime)
122             return;
123         printf("HTTP/1.1 304 Not Modified\r\n");
124         printf("Date: %s\r\n", fmthttpdate(time(NULL)));
125         printf("Content-Length: 0\r\n");
126         printf("\r\n");
127         exit(0);
128     }
129 }
130
131 static void usage(void)
132 {
133     flog(LOG_ERR, "usage: sendfile [-c CONTENT-TYPE] METHOD URL REST");
134 }
135
136 int main(int argc, char **argv)
137 {
138     int c;
139     char *file;
140     struct stat sb;
141     int fd;
142     const char *contype;
143     
144     contype = NULL;
145     while((c = getopt(argc, argv, "c:")) >= 0) {
146         switch(c) {
147         case 'c':
148             contype = optarg;
149             break;
150         default:
151             usage();
152             exit(1);
153             break;
154         }
155     }
156     if(argc - optind < 3) {
157         usage();
158         exit(1);
159     }
160     if((file = getenv("REQ_X_ASH_FILE")) == NULL) {
161         flog(LOG_ERR, "sendfile: needs to be called with the X-Ash-File header");
162         exit(1);
163     }
164     if(*argv[optind + 2]) {
165         simpleerror(1, 404, "Not Found", "The requested URL has no corresponding resource.");
166         exit(0);
167     }
168     if(stat(file, &sb) || ((fd = open(file, O_RDONLY)) < 0)) {
169         flog(LOG_ERR, "sendfile: could not stat input file %s: %s", file, strerror(errno));
170         simpleerror(1, 500, "Internal Error", "The server could not access its own data.");
171         exit(1);
172     }
173     if(contype == NULL)
174         contype = getmimetype(file, &sb);
175     
176     checkcache(file, &sb);
177     
178     printf("HTTP/1.1 200 OK\r\n");
179     printf("Content-Type: %s\r\n", contype);
180     printf("Content-Length: %ji\r\n", (intmax_t)sb.st_size);
181     printf("Last-Modified: %s\r\n", fmthttpdate(sb.st_mtime));
182     printf("Date: %s\r\n", fmthttpdate(time(NULL)));
183     printf("\r\n");
184     fflush(stdout);
185     if(strcasecmp(argv[optind], "head"))
186         passdata(fd, 1);
187     return(0);
188 }