]> git.dolda2000.com Git - ashd.git/blob - src/psendfile.c
ea00b3e4511fe182ba95bade75f9c77fd0f1621a
[ashd.git] / src / psendfile.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 <signal.h>
29 #include <magic.h>
30 #include <locale.h>
31 #include <langinfo.h>
32 #include <sys/socket.h>
33
34 #ifdef HAVE_CONFIG_H
35 #include <config.h>
36 #endif
37 #include <utils.h>
38 #include <log.h>
39 #include <req.h>
40 #include <resp.h>
41 #include <mt.h>
42 #include <mtio.h>
43
44 #ifdef HAVE_XATTR
45 #include <sys/xattr.h>
46 #endif
47
48 #include "compress.h"
49
50 static magic_t cookie;
51
52 static char *attrmimetype(char *file)
53 {
54 #ifdef HAVE_XATTR
55     char buf[1024];
56     int i;
57     ssize_t sz;
58     
59     if((sz = getxattr(file, "user.ash-mime-type", buf, sizeof(buf) - 1)) > 0)
60         goto found;
61     if((sz = getxattr(file, "user.mime-type", buf, sizeof(buf) - 1)) > 0)
62         goto found;
63     if((sz = getxattr(file, "user.mime_type", buf, sizeof(buf) - 1)) > 0)
64         goto found;
65     if((sz = getxattr(file, "user.Content-Type", buf, sizeof(buf) - 1)) > 0)
66         goto found;
67     return(NULL);
68 found:
69     for(i = 0; i < sz; i++) {
70         if((buf[i] < 32) || (buf[i] >= 128))
71             return(NULL);
72     }
73     buf[sz] = 0;
74     return(sstrdup(buf));
75 #else
76     return(NULL);
77 #endif
78 }
79
80 static char *getmimetype(char *file, struct stat *sb)
81 {
82     char *ret;
83     const char *cret;
84     
85     if((ret = attrmimetype(file)) != NULL)
86         return(ret);
87     if((cret = magic_file(cookie, file)) != NULL)
88         return(sstrdup(cret));
89     return(sstrdup("application/octet-stream"));
90 }
91
92 /* XXX: This could be made far better and check for other attributes
93  * and stuff, but not now. */
94 static char *ckctype(char *ctype)
95 {
96     char *buf;
97     
98     if(!strncmp(ctype, "text/", 5) && (strchr(ctype, ';') == NULL)) {
99         buf = sprintf2("%s; charset=%s", ctype, nl_langinfo(CODESET));
100         free(ctype);
101         return(buf);
102     }
103     return(ctype);
104 }
105
106 static int checkcache(struct hthead *req, FILE *out, char *file, struct stat *sb)
107 {
108     char *hdr;
109     
110     if((hdr = getheader(req, "If-Modified-Since")) != NULL) {
111         if(parsehttpdate(hdr) < sb->st_mtime)
112             return(0);
113         fprintf(out, "HTTP/1.1 304 Not Modified\n");
114         fprintf(out, "Date: %s\n", fmthttpdate(time(NULL)));
115         fprintf(out, "Content-Length: 0\n");
116         fprintf(out, "\n");
117         return(1);
118     }
119     return(0);
120 }
121
122 static off_t passdata(FILE *in, FILE *out, off_t max)
123 {
124     size_t read;
125     off_t total;
126     char buf[8192];
127     
128     total = 0;
129     while(!feof(in) && ((max < 0) || (total < max))) {
130         read = sizeof(buf);
131         if(max >= 0)
132             read = min(max - total, read);
133         read = fread(buf, 1, read, in);
134         if(ferror(in))
135             return(-1);
136         if(fwrite(buf, 1, read, out) != read)
137             return(-1);
138         total += read;
139     }
140     return(total);
141 }
142
143 static void sendwhole(struct hthead *req, FILE *out, FILE *sfile, struct stat *sb, char *contype, const char *enctype, int head)
144 {
145     fprintf(out, "HTTP/1.1 200 OK\n");
146     fprintf(out, "Content-Type: %s\n", contype);
147     if(enctype != NULL)
148         fprintf(out, "Content-Encoding: %s\n", enctype);
149     fprintf(out, "Content-Length: %ji\n", (intmax_t)sb->st_size);
150     fprintf(out, "Last-Modified: %s\n", fmthttpdate(sb->st_mtime));
151     fprintf(out, "Date: %s\n", fmthttpdate(time(NULL)));
152     fprintf(out, "\n");
153     if(!head)
154         passdata(sfile, out, -1);
155 }
156
157 static void sendrange(struct hthead *req, FILE *out, FILE *sfile, struct stat *sb, char *contype, const char *enctype, char *spec, int head)
158 {
159     char buf[strlen(spec) + 1];
160     char *p, *e;
161     off_t start, end;
162     
163     if(strncmp(spec, "bytes=", 6))
164         goto error;
165     strcpy(buf, spec + 6);
166     if((p = strchr(buf, '-')) == NULL)
167         goto error;
168     if(p == buf) {
169         if(!p[1])
170             goto error;
171         end = sb->st_size;
172         start = end - strtoll(p + 1, &e, 10);
173         if(*e)
174             goto error;
175         if(start < 0)
176             start = 0;
177     } else {
178         *(p++) = 0;
179         start = strtoll(buf, &e, 10);
180         if(*e)
181             goto error;
182         if(*p) {
183             end = strtoll(p, &e, 10) + 1;
184             if(*e)
185                 goto error;
186         } else {
187             end = sb->st_size;
188         }
189     }
190     if(start >= sb->st_size) {
191         fprintf(out, "HTTP/1.1 416 Not satisfiable\n");
192         fprintf(out, "Content-Range: */%ji\n", (intmax_t)sb->st_size);
193         fprintf(out, "Content-Length: 0\n");
194         fprintf(out, "Last-Modified: %s\n", fmthttpdate(sb->st_mtime));
195         fprintf(out, "Date: %s\n", fmthttpdate(time(NULL)));
196         fprintf(out, "\n");
197         return;
198     }
199     if((start < 0) || (start >= end))
200         goto error;
201     if(end > sb->st_size)
202         end = sb->st_size;
203     errno = 0;
204     if(fseeko(sfile, start, SEEK_SET)) {
205         simpleerror2(out, 500, "Internal Error", "Could not seek properly to beginning of requested byte range.");
206         flog(LOG_ERR, "sendfile: could not seek properly when serving partial content: %s", strerror(errno));
207         return;
208     }
209     fprintf(out, "HTTP/1.1 206 Partial content\n");
210     fprintf(out, "Content-Range: bytes %ji-%ji/%ji\n", (intmax_t)start, (intmax_t)(end - 1), (intmax_t)sb->st_size);
211     fprintf(out, "Content-Length: %ji\n", (intmax_t)(end - start));
212     fprintf(out, "Content-Type: %s\n", contype);
213     fprintf(out, "Last-Modified: %s\n", fmthttpdate(sb->st_mtime));
214     fprintf(out, "Date: %s\n", fmthttpdate(time(NULL)));
215     fprintf(out, "\n");
216     if(!head)
217         passdata(sfile, out, end - start);
218     return;
219     
220 error:
221     sendwhole(req, out, sfile, sb, contype, enctype, head);
222 }
223
224 static void serve(struct muth *muth, va_list args)
225 {
226     vavar(struct hthead *, req);
227     vavar(int, fd);
228     FILE *out, *sfile;
229     int ishead, sfd;
230     char *file, *contype, *hdr;
231     const char *enctype;
232     struct stat sb;
233     
234     sfile = NULL;
235     contype = NULL;
236     out = mtstdopen(fd, 1, 60, "r+", NULL);
237     
238     if((file = getheader(req, "X-Ash-File")) == NULL) {
239         flog(LOG_ERR, "psendfile: needs to be called with the X-Ash-File header");
240         simpleerror2(out, 500, "Internal Error", "The server is incorrectly configured.");
241         goto out;
242     }
243     if(*req->rest) {
244         simpleerror2(out, 404, "Not Found", "The requested URL has no corresponding resource.");
245         goto out;
246     }
247     hdr = getheader(req, "X-Ash-Compress") ? getheader(req, "Accept-Encoding") : "";
248     if(((sfd = ccopen(file, &sb, hdr, &enctype)) < 0) || ((sfile = fdopen(sfd, "r")) == NULL)) {
249         if(sfd >= 0)
250             close(sfd);
251         flog(LOG_ERR, "psendfile: could not open input file %s: %s", file, strerror(errno));
252         simpleerror2(out, 500, "Internal Error", "The server could not access its own data.");
253         goto out;
254     }
255     if(!strcasecmp(req->method, "get")) {
256         ishead = 0;
257     } else if(!strcasecmp(req->method, "head")) {
258         ishead = 1;
259     } else {
260         simpleerror2(out, 405, "Method not allowed", "The requested method is not defined for this resource.");
261         goto out;
262     }
263     if((hdr = getheader(req, "X-Ash-Content-Type")) == NULL)
264         contype = getmimetype(file, &sb);
265     else
266         contype = sstrdup(hdr);
267     contype = ckctype(contype);
268     
269     if(checkcache(req, out, file, &sb))
270         goto out;
271
272     if((hdr = getheader(req, "Range")) != NULL)
273         sendrange(req, out, sfile, &sb, contype, enctype, hdr, ishead);
274     else
275         sendwhole(req, out, sfile, &sb, contype, enctype, ishead);
276     
277 out:
278     if(sfile != NULL)
279         fclose(sfile);
280     if(contype != NULL)
281         free(contype);
282     fclose(out);
283     freehthead(req);
284 }
285
286 static void listenloop(struct muth *muth, va_list args)
287 {
288     vavar(int, lfd);
289     int fd;
290     struct hthead *req;
291     
292     while(1) {
293         block(lfd, EV_READ, 0);
294         if((fd = recvreq(lfd, &req)) < 0) {
295             if(errno != 0)
296                 flog(LOG_ERR, "recvreq: %s", strerror(errno));
297             break;
298         }
299         mustart(serve, req, fd);
300     }
301 }
302
303 static void sigterm(int sig)
304 {
305     shutdown(0, SHUT_RDWR);
306 }
307
308 static void usage(FILE *out)
309 {
310     fprintf(out, "usage: psendfile [-h]\n");
311 }
312
313 int main(int argc, char **argv)
314 {
315     int c;
316     
317     setlocale(LC_ALL, "");
318     while((c = getopt(argc, argv, "h")) >= 0) {
319         switch(c) {
320         case 'h':
321             usage(stdout);
322             exit(0);
323         default:
324             usage(stderr);
325             exit(1);
326         }
327     }
328     cookie = magic_open(MAGIC_MIME_TYPE | MAGIC_SYMLINK);
329     magic_load(cookie, NULL);
330     mustart(listenloop, 0);
331     signal(SIGINT, sigterm);
332     signal(SIGTERM, sigterm);
333     ioloop();
334     return(0);
335 }