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 <attr/xattr.h>
48 static magic_t cookie;
50 static char *attrmimetype(char *file)
57 if((sz = getxattr(file, "user.ash-mime-type", buf, sizeof(buf) - 1)) > 0)
59 if((sz = getxattr(file, "user.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.Content-Type", buf, sizeof(buf) - 1)) > 0)
67 for(i = 0; i < sz; i++) {
68 if((buf[i] < 32) || (buf[i] >= 128))
78 static char *getmimetype(char *file, struct stat *sb)
83 if((ret = attrmimetype(file)) != NULL)
85 if((cret = magic_file(cookie, file)) != NULL)
86 return(sstrdup(cret));
87 return(sstrdup("application/octet-stream"));
90 /* XXX: This could be made far better and check for other attributes
91 * and stuff, but not now. */
92 static char *ckctype(char *ctype)
96 if(!strncmp(ctype, "text/", 5) && (strchr(ctype, ';') == NULL)) {
97 buf = sprintf2("%s; charset=%s", ctype, nl_langinfo(CODESET));
104 static int checkcache(struct hthead *req, FILE *out, char *file, struct stat *sb)
108 if((hdr = getheader(req, "If-Modified-Since")) != NULL) {
109 if(parsehttpdate(hdr) < sb->st_mtime)
111 fprintf(out, "HTTP/1.1 304 Not Modified\n");
112 fprintf(out, "Date: %s\n", fmthttpdate(time(NULL)));
113 fprintf(out, "Content-Length: 0\n");
120 static off_t passdata(FILE *in, FILE *out, off_t max)
127 while(!feof(in) && ((max < 0) || (total < max))) {
130 read = min(max - total, read);
131 read = fread(buf, 1, read, in);
134 if(fwrite(buf, 1, read, out) != read)
141 static void sendwhole(struct hthead *req, FILE *out, FILE *sfile, struct stat *sb, char *contype, int head)
143 fprintf(out, "HTTP/1.1 200 OK\n");
144 fprintf(out, "Content-Type: %s\n", contype);
145 fprintf(out, "Content-Length: %ji\n", (intmax_t)sb->st_size);
146 fprintf(out, "Last-Modified: %s\n", fmthttpdate(sb->st_mtime));
147 fprintf(out, "Date: %s\n", fmthttpdate(time(NULL)));
150 passdata(sfile, out, -1);
153 static void sendrange(struct hthead *req, FILE *out, FILE *sfile, struct stat *sb, char *contype, char *spec, int head)
155 char buf[strlen(spec) + 1];
159 if(strncmp(spec, "bytes=", 6))
161 strcpy(buf, spec + 6);
162 if((p = strchr(buf, '-')) == NULL)
168 start = end - strtoll(p + 1, &e, 10);
175 start = strtoll(buf, &e, 10);
179 end = strtoll(p, &e, 10) + 1;
186 if(start >= sb->st_size) {
187 fprintf(out, "HTTP/1.1 416 Not satisfiable\n");
188 fprintf(out, "Content-Range: */%ji\n", (intmax_t)sb->st_size);
189 fprintf(out, "Content-Length: 0\n");
190 fprintf(out, "Last-Modified: %s\n", fmthttpdate(sb->st_mtime));
191 fprintf(out, "Date: %s\n", fmthttpdate(time(NULL)));
195 if((start < 0) || (start >= end))
197 if(end > sb->st_size)
200 if(fseeko(sfile, start, SEEK_SET)) {
201 simpleerror2(out, 500, "Internal Error", "Could not seek properly to beginning of requested byte range.");
202 flog(LOG_ERR, "sendfile: could not seek properly when serving partial content: %s", strerror(errno));
205 fprintf(out, "HTTP/1.1 206 Partial content\n");
206 fprintf(out, "Content-Range: bytes %ji-%ji/%ji\n", (intmax_t)start, (intmax_t)(end - 1), (intmax_t)sb->st_size);
207 fprintf(out, "Content-Length: %ji\n", (intmax_t)(end - start));
208 fprintf(out, "Content-Type: %s\n", contype);
209 fprintf(out, "Last-Modified: %s\n", fmthttpdate(sb->st_mtime));
210 fprintf(out, "Date: %s\n", fmthttpdate(time(NULL)));
213 passdata(sfile, out, end - start);
217 sendwhole(req, out, sfile, sb, contype, head);
220 static void serve(struct muth *muth, va_list args)
222 vavar(struct hthead *, req);
226 char *file, *contype, *hdr;
231 out = mtstdopen(fd, 1, 60, "r+", NULL);
233 if((file = getheader(req, "X-Ash-File")) == NULL) {
234 flog(LOG_ERR, "psendfile: needs to be called with the X-Ash-File header");
235 simpleerror2(out, 500, "Internal Error", "The server is incorrectly configured.");
239 simpleerror2(out, 404, "Not Found", "The requested URL has no corresponding resource.");
242 if(((sfile = fopen(file, "r")) == NULL) || fstat(fileno(sfile), &sb)) {
243 flog(LOG_ERR, "psendfile: could not stat input file %s: %s", file, strerror(errno));
244 simpleerror2(out, 500, "Internal Error", "The server could not access its own data.");
247 if(!strcasecmp(req->method, "get")) {
249 } else if(!strcasecmp(req->method, "head")) {
252 simpleerror2(out, 405, "Method not allowed", "The requested method is not defined for this resource.");
255 if((hdr = getheader(req, "X-Ash-Content-Type")) == NULL)
256 contype = getmimetype(file, &sb);
258 contype = sstrdup(hdr);
259 contype = ckctype(contype);
261 if(checkcache(req, out, file, &sb))
264 if((hdr = getheader(req, "Range")) != NULL)
265 sendrange(req, out, sfile, &sb, contype, hdr, ishead);
267 sendwhole(req, out, sfile, &sb, contype, ishead);
278 static void listenloop(struct muth *muth, va_list args)
285 block(lfd, EV_READ, 0);
286 if((fd = recvreq(lfd, &req)) < 0) {
288 flog(LOG_ERR, "recvreq: %s", strerror(errno));
291 mustart(serve, req, fd);
295 static void sigterm(int sig)
297 shutdown(0, SHUT_RDWR);
300 static void usage(FILE *out)
302 fprintf(out, "usage: psendfile [-h]\n");
305 int main(int argc, char **argv)
309 setlocale(LC_ALL, "");
310 while((c = getopt(argc, argv, "h")) >= 0) {
320 cookie = magic_open(MAGIC_MIME_TYPE | MAGIC_SYMLINK);
321 magic_load(cookie, NULL);
322 mustart(listenloop, 0);
323 signal(SIGINT, sigterm);
324 signal(SIGTERM, sigterm);