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/>.
31 #include <sys/socket.h>
44 #include <attr/xattr.h>
47 static magic_t cookie;
49 static char *attrmimetype(char *file)
56 if((sz = getxattr(file, "user.ash-mime-type", buf, sizeof(buf) - 1)) > 0)
58 if((sz = getxattr(file, "user.mime-type", buf, sizeof(buf) - 1)) > 0)
60 if((sz = getxattr(file, "user.mime_type", buf, sizeof(buf) - 1)) > 0)
62 if((sz = getxattr(file, "user.Content-Type", buf, sizeof(buf) - 1)) > 0)
66 for(i = 0; i < sz; i++) {
67 if((buf[i] < 32) || (buf[i] >= 128))
77 static char *getmimetype(char *file, struct stat *sb)
82 if((ret = attrmimetype(file)) != NULL)
84 if((cret = magic_file(cookie, file)) != NULL)
85 return(sstrdup(cret));
86 return(sstrdup("application/octet-stream"));
89 /* XXX: This could be made far better and check for other attributes
90 * and stuff, but not now. */
91 static char *ckctype(char *ctype)
95 if(!strncmp(ctype, "text/", 5) && (strchr(ctype, ';') == NULL)) {
96 buf = sprintf2("%s; charset=%s", ctype, nl_langinfo(CODESET));
103 static int checkcache(struct hthead *req, FILE *out, char *file, struct stat *sb)
107 if((hdr = getheader(req, "If-Modified-Since")) != NULL) {
108 if(parsehttpdate(hdr) < sb->st_mtime)
110 fprintf(out, "HTTP/1.1 304 Not Modified\n");
111 fprintf(out, "Date: %s\n", fmthttpdate(time(NULL)));
112 fprintf(out, "Content-Length: 0\n");
119 static off_t passdata(FILE *in, FILE *out, off_t max)
126 while(!feof(in) && ((max < 0) || (total < max))) {
129 read = min(max - total, read);
130 read = fread(buf, 1, read, in);
133 if(fwrite(buf, 1, read, out) != read)
140 static void sendwhole(struct hthead *req, FILE *out, FILE *sfile, struct stat *sb, char *contype, int head)
142 fprintf(out, "HTTP/1.1 200 OK\n");
143 fprintf(out, "Content-Type: %s\n", contype);
144 fprintf(out, "Content-Length: %ji\n", (intmax_t)sb->st_size);
145 fprintf(out, "Last-Modified: %s\n", fmthttpdate(sb->st_mtime));
146 fprintf(out, "Date: %s\n", fmthttpdate(time(NULL)));
149 passdata(sfile, out, -1);
152 static void sendrange(struct hthead *req, FILE *out, FILE *sfile, struct stat *sb, char *contype, char *spec, int head)
154 char buf[strlen(spec) + 1];
158 if(strncmp(spec, "bytes=", 6))
160 strcpy(buf, spec + 6);
161 if((p = strchr(buf, '-')) == NULL)
167 start = end - strtoll(p + 1, &e, 10);
174 start = strtoll(buf, &e, 10);
178 end = strtoll(p, &e, 10) + 1;
185 if(start >= sb->st_size) {
186 fprintf(out, "HTTP/1.1 416 Not satisfiable\n");
187 fprintf(out, "Content-Range: */%ji\n", (intmax_t)sb->st_size);
188 fprintf(out, "Content-Length: 0\n");
189 fprintf(out, "Last-Modified: %s\n", fmthttpdate(sb->st_mtime));
190 fprintf(out, "Date: %s\n", fmthttpdate(time(NULL)));
194 if((start < 0) || (start >= end))
196 if(end > sb->st_size)
199 if(fseeko(sfile, start, SEEK_SET)) {
200 simpleerror2(out, 500, "Internal Error", "Could not seek properly to beginning of requested byte range.");
201 flog(LOG_ERR, "sendfile: could not seek properly when serving partial content: %s", strerror(errno));
204 fprintf(out, "HTTP/1.1 206 Partial content\n");
205 fprintf(out, "Content-Range: bytes %ji-%ji/%ji\n", (intmax_t)start, (intmax_t)(end - 1), (intmax_t)sb->st_size);
206 fprintf(out, "Content-Length: %ji\n", (intmax_t)(end - start));
207 fprintf(out, "Content-Type: %s\n", contype);
208 fprintf(out, "Last-Modified: %s\n", fmthttpdate(sb->st_mtime));
209 fprintf(out, "Date: %s\n", fmthttpdate(time(NULL)));
212 passdata(sfile, out, end - start);
216 sendwhole(req, out, sfile, sb, contype, head);
219 static void serve(struct muth *muth, va_list args)
221 vavar(struct hthead *, req);
225 char *file, *contype, *hdr;
230 out = mtstdopen(fd, 1, 60, "r+");
232 if((file = getheader(req, "X-Ash-File")) == NULL) {
233 flog(LOG_ERR, "psendfile: needs to be called with the X-Ash-File header");
234 simpleerror2(out, 500, "Internal Error", "The server is incorrectly configured.");
238 simpleerror2(out, 404, "Not Found", "The requested URL has no corresponding resource.");
241 if(((sfile = fopen(file, "r")) < 0) || fstat(fileno(sfile), &sb)) {
242 flog(LOG_ERR, "psendfile: could not stat input file %s: %s", file, strerror(errno));
243 simpleerror2(out, 500, "Internal Error", "The server could not access its own data.");
246 if(!strcasecmp(req->method, "get")) {
248 } else if(!strcasecmp(req->method, "head")) {
251 simpleerror2(out, 405, "Method not allowed", "The requested method is not defined for this resource.");
254 if((hdr = getheader(req, "X-Ash-Content-Type")) == NULL)
255 contype = getmimetype(file, &sb);
257 contype = sstrdup(hdr);
258 contype = ckctype(contype);
260 if(checkcache(req, out, file, &sb))
263 if((hdr = getheader(req, "Range")) != NULL)
264 sendrange(req, out, sfile, &sb, contype, hdr, ishead);
266 sendwhole(req, out, sfile, &sb, contype, ishead);
277 static void listenloop(struct muth *muth, va_list args)
284 block(lfd, EV_READ, 0);
285 if((fd = recvreq(lfd, &req)) < 0) {
287 flog(LOG_ERR, "recvreq: %s", strerror(errno));
290 mustart(serve, req, fd);
294 static void sigterm(int sig)
296 shutdown(0, SHUT_RDWR);
299 static void usage(FILE *out)
301 fprintf(out, "usage: psendfile [-h]\n");
304 int main(int argc, char **argv)
308 setlocale(LC_ALL, "");
309 while((c = getopt(argc, argv, "h")) >= 0) {
319 cookie = magic_open(MAGIC_MIME_TYPE | MAGIC_SYMLINK);
320 magic_load(cookie, NULL);
321 mustart(listenloop, 0);
322 signal(SIGINT, sigterm);
323 signal(SIGTERM, sigterm);