2 ashd - A Sane HTTP Daemon
3 Copyright (C) 2008 Fredrik Tolf <fredrik@dolda2000.com>
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.
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.
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/>.
32 #include <sys/socket.h>
45 #include <sys/xattr.h>
50 static magic_t cookie;
52 static char *attrmimetype(char *file)
59 if((sz = getxattr(file, "user.ash-mime-type", buf, sizeof(buf) - 1)) > 0)
61 if((sz = getxattr(file, "user.mime-type", buf, sizeof(buf) - 1)) > 0)
63 if((sz = getxattr(file, "user.mime_type", buf, sizeof(buf) - 1)) > 0)
65 if((sz = getxattr(file, "user.Content-Type", buf, sizeof(buf) - 1)) > 0)
69 for(i = 0; i < sz; i++) {
70 if((buf[i] < 32) || (buf[i] >= 128))
80 static char *getmimetype(char *file, struct stat *sb)
85 if((ret = attrmimetype(file)) != NULL)
87 if((cret = magic_file(cookie, file)) != NULL)
88 return(sstrdup(cret));
89 return(sstrdup("application/octet-stream"));
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)
98 if(!strncmp(ctype, "text/", 5) && (strchr(ctype, ';') == NULL)) {
99 buf = sprintf2("%s; charset=%s", ctype, nl_langinfo(CODESET));
106 static int checkcache(struct hthead *req, FILE *out, char *file, struct stat *sb)
110 if((hdr = getheader(req, "If-Modified-Since")) != NULL) {
111 if(parsehttpdate(hdr) < sb->st_mtime)
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");
122 static off_t passdata(FILE *in, FILE *out, off_t max)
129 while(!feof(in) && ((max < 0) || (total < max))) {
132 read = min(max - total, read);
133 read = fread(buf, 1, read, in);
136 if(fwrite(buf, 1, read, out) != read)
143 static void sendwhole(struct hthead *req, FILE *out, FILE *sfile, struct stat *sb, char *contype, const char *enctype, int head)
145 fprintf(out, "HTTP/1.1 200 OK\n");
146 fprintf(out, "Content-Type: %s\n", contype);
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)));
154 passdata(sfile, out, -1);
157 static void sendrange(struct hthead *req, FILE *out, FILE *sfile, struct stat *sb, char *contype, const char *enctype, char *spec, int head)
159 char buf[strlen(spec) + 1];
163 if(strncmp(spec, "bytes=", 6))
165 strcpy(buf, spec + 6);
166 if((p = strchr(buf, '-')) == NULL)
172 start = end - strtoll(p + 1, &e, 10);
179 start = strtoll(buf, &e, 10);
183 end = strtoll(p, &e, 10) + 1;
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)));
199 if((start < 0) || (start >= end))
201 if(end > sb->st_size)
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));
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)));
217 passdata(sfile, out, end - start);
221 sendwhole(req, out, sfile, sb, contype, enctype, head);
224 static void serve(struct muth *muth, va_list args)
226 vavar(struct hthead *, req);
230 char *file, *contype, *hdr;
236 out = mtstdopen(fd, 1, 60, "r+", NULL);
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.");
244 simpleerror2(out, 404, "Not Found", "The requested URL has no corresponding resource.");
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)) {
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.");
255 if(!strcasecmp(req->method, "get")) {
257 } else if(!strcasecmp(req->method, "head")) {
260 simpleerror2(out, 405, "Method not allowed", "The requested method is not defined for this resource.");
263 if((hdr = getheader(req, "X-Ash-Content-Type")) == NULL)
264 contype = getmimetype(file, &sb);
266 contype = sstrdup(hdr);
267 contype = ckctype(contype);
269 if(checkcache(req, out, file, &sb))
272 if((hdr = getheader(req, "Range")) != NULL)
273 sendrange(req, out, sfile, &sb, contype, enctype, hdr, ishead);
275 sendwhole(req, out, sfile, &sb, contype, enctype, ishead);
286 static void listenloop(struct muth *muth, va_list args)
293 block(lfd, EV_READ, 0);
294 if((fd = recvreq(lfd, &req)) < 0) {
296 flog(LOG_ERR, "recvreq: %s", strerror(errno));
299 mustart(serve, req, fd);
303 static void sigterm(int sig)
305 shutdown(0, SHUT_RDWR);
308 static void usage(FILE *out)
310 fprintf(out, "usage: psendfile [-h]\n");
313 int main(int argc, char **argv)
317 setlocale(LC_ALL, "");
318 while((c = getopt(argc, argv, "h")) >= 0) {
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);