b3797aaca88027cbd3776707b10a2dafc2807f18
[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 #include <locale.h>
30 #include <langinfo.h>
31
32 #ifdef HAVE_CONFIG_H
33 #include <config.h>
34 #endif
35 #include <utils.h>
36 #include <log.h>
37 #include <resp.h>
38
39 #ifdef HAVE_XATTR
40 #include <attr/xattr.h>
41 #endif
42
43 static magic_t cookie = NULL;
44
45 static void passdata(int in, int out)
46 {
47     int ret, len, off;
48     char *buf;
49     
50     buf = smalloc(65536);
51     while(1) {
52         len = read(in, buf, 65536);
53         if(len < 0) {
54             flog(LOG_ERR, "sendfile: could not read input: %s", strerror(errno));
55             break;
56         }
57         if(len == 0)
58             break;
59         for(off = 0; off < len; off += ret) {
60             ret = write(out, buf + off, len - off);
61             if(ret < 0) {
62                 flog(LOG_ERR, "sendfile: could not write output: %s", strerror(errno));
63                 break;
64             }
65         }
66     }
67     free(buf);
68 }
69
70 static char *attrmimetype(char *file)
71 {
72 #ifdef HAVE_XATTR
73     static char buf[1024];
74     int i;
75     ssize_t sz;
76     
77     if((sz = getxattr(file, "user.ash-mime-type", buf, sizeof(buf) - 1)) > 0)
78         goto found;
79     if((sz = getxattr(file, "user.mime-type", buf, sizeof(buf) - 1)) > 0)
80         goto found;
81     if((sz = getxattr(file, "user.mime_type", buf, sizeof(buf) - 1)) > 0)
82         goto found;
83     if((sz = getxattr(file, "user.Content-Type", buf, sizeof(buf) - 1)) > 0)
84         goto found;
85     return(NULL);
86 found:
87     for(i = 0; i < sz; i++) {
88         if((buf[sz] < 32) || (buf[sz] >= 128))
89             return(NULL);
90     }
91     buf[sz] = 0;
92     return(buf);
93 #else
94     return(NULL);
95 #endif
96 }
97
98 static const char *getmimetype(char *file, struct stat *sb)
99 {
100     const char *ret;
101     
102     if((ret = attrmimetype(file)) != NULL)
103         return(ret);
104     if(cookie == NULL) {
105         cookie = magic_open(MAGIC_MIME_TYPE | MAGIC_SYMLINK);
106         magic_load(cookie, NULL);
107     }
108     if((ret = magic_file(cookie, file)) != NULL)
109         return(ret);
110     return("application/octet-stream");
111 }
112
113 /* XXX: This could be made far better and check for other attributes
114  * and stuff, but not now. */
115 static const char *ckctype(const char *ctype)
116 {
117     if(!strncmp(ctype, "text/", 5) && (strchr(ctype, ';') == NULL))
118         return(sprintf2("%s; charset=%s", ctype, nl_langinfo(CODESET)));
119     return(ctype);
120 }
121
122 static void checkcache(char *file, struct stat *sb)
123 {
124     char *hdr;
125     
126     if((hdr = getenv("REQ_IF_MODIFIED_SINCE")) != NULL) {
127         if(parsehttpdate(hdr) < sb->st_mtime)
128             return;
129         printf("HTTP/1.1 304 Not Modified\n");
130         printf("Date: %s\n", fmthttpdate(time(NULL)));
131         printf("Content-Length: 0\n");
132         printf("\n");
133         exit(0);
134     }
135 }
136
137 static void usage(void)
138 {
139     flog(LOG_ERR, "usage: sendfile [-c CONTENT-TYPE] METHOD URL REST");
140 }
141
142 int main(int argc, char **argv)
143 {
144     int c;
145     char *file;
146     struct stat sb;
147     int fd;
148     const char *contype;
149     
150     setlocale(LC_ALL, "");
151     contype = NULL;
152     while((c = getopt(argc, argv, "c:")) >= 0) {
153         switch(c) {
154         case 'c':
155             contype = optarg;
156             break;
157         default:
158             usage();
159             exit(1);
160             break;
161         }
162     }
163     if(argc - optind < 3) {
164         usage();
165         exit(1);
166     }
167     if((file = getenv("REQ_X_ASH_FILE")) == NULL) {
168         flog(LOG_ERR, "sendfile: needs to be called with the X-Ash-File header");
169         exit(1);
170     }
171     if(*argv[optind + 2]) {
172         simpleerror(1, 404, "Not Found", "The requested URL has no corresponding resource.");
173         exit(0);
174     }
175     if(stat(file, &sb) || ((fd = open(file, O_RDONLY)) < 0)) {
176         flog(LOG_ERR, "sendfile: could not stat input file %s: %s", file, strerror(errno));
177         simpleerror(1, 500, "Internal Error", "The server could not access its own data.");
178         exit(1);
179     }
180     if(contype == NULL)
181         contype = getmimetype(file, &sb);
182     contype = ckctype(contype);
183     
184     checkcache(file, &sb);
185     
186     printf("HTTP/1.1 200 OK\n");
187     printf("Content-Type: %s\n", contype);
188     printf("Content-Length: %ji\n", (intmax_t)sb.st_size);
189     printf("Last-Modified: %s\n", fmthttpdate(sb.st_mtime));
190     printf("Date: %s\n", fmthttpdate(time(NULL)));
191     printf("\n");
192     fflush(stdout);
193     if(strcasecmp(argv[optind], "head"))
194         passdata(fd, 1);
195     return(0);
196 }