Use libmagic for 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 static magic_t cookie = NULL;
38
39 static void passdata(int in, int out)
40 {
41     int ret, len, off;
42     char *buf;
43     
44     buf = smalloc(65536);
45     while(1) {
46         len = read(in, buf, 65536);
47         if(len < 0) {
48             flog(LOG_ERR, "sendfile: could not read input: %s", strerror(errno));
49             break;
50         }
51         if(len == 0)
52             break;
53         for(off = 0; off < len; off += ret) {
54             ret = write(out, buf + off, len - off);
55             if(ret < 0) {
56                 flog(LOG_ERR, "sendfile: could not write output: %s", strerror(errno));
57                 break;
58             }
59         }
60     }
61     free(buf);
62 }
63
64 static int strrcmp(char *str, char *end)
65 {
66     return(strcmp(str + strlen(str) - strlen(end), end));
67 }
68
69 static const char *getmimetype(char *file, struct stat *sb)
70 {
71     const char *ret;
72     
73     if(cookie == NULL) {
74         cookie = magic_open(MAGIC_MIME_TYPE);
75         magic_load(cookie, NULL);
76     }
77     if((ret = magic_file(cookie, file)) != NULL)
78         return(ret);
79     return("application/octet-stream");
80 }
81
82 static void checkcache(char *file, struct stat *sb)
83 {
84     char *hdr;
85     
86     if((hdr = getenv("REQ_IF_MODIFIED_SINCE")) != NULL) {
87         if(parsehttpdate(hdr) < sb->st_mtime)
88             return;
89         printf("HTTP/1.1 304 Not Modified\r\n");
90         printf("Date: %s\r\n", fmthttpdate(time(NULL)));
91         printf("Content-Length: 0\r\n");
92         printf("\r\n");
93         exit(0);
94     }
95 }
96
97 static void usage(void)
98 {
99     flog(LOG_ERR, "usage: sendfile [-c CONTENT-TYPE] METHOD URL REST");
100 }
101
102 int main(int argc, char **argv)
103 {
104     int c;
105     char *file;
106     struct stat sb;
107     int fd;
108     const char *contype;
109     
110     contype = NULL;
111     while((c = getopt(argc, argv, "c:")) >= 0) {
112         switch(c) {
113         case 'c':
114             contype = optarg;
115             break;
116         default:
117             usage();
118             exit(1);
119             break;
120         }
121     }
122     if(argc - optind < 3) {
123         usage();
124         exit(1);
125     }
126     if((file = getenv("REQ_X_ASH_FILE")) == NULL) {
127         flog(LOG_ERR, "sendfile: needs to be called with the X-Ash-File header");
128         exit(1);
129     }
130     if(*argv[optind + 2]) {
131         simpleerror(1, 404, "Not Found", "The requested URL has no corresponding resource.");
132         exit(0);
133     }
134     if(stat(file, &sb) || ((fd = open(file, O_RDONLY)) < 0)) {
135         flog(LOG_ERR, "sendfile: could not stat input file %s: %s", file, strerror(errno));
136         simpleerror(1, 500, "Internal Error", "The server could not access its own data.");
137         exit(1);
138     }
139     if(contype == NULL)
140         contype = getmimetype(file, &sb);
141     
142     checkcache(file, &sb);
143     
144     printf("HTTP/1.1 200 OK\r\n");
145     printf("Content-Type: %s\r\n", contype);
146     printf("Content-Length: %ji\r\n", (intmax_t)sb.st_size);
147     printf("Last-Modified: %s\r\n", fmthttpdate(sb.st_mtime));
148     printf("Date: %s\r\n", fmthttpdate(time(NULL)));
149     printf("\r\n");
150     fflush(stdout);
151     if(strcasecmp(argv[optind], "head"))
152         passdata(fd, 1);
153     return(0);
154 }