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/>.
38 #include <attr/xattr.h>
41 static magic_t cookie = NULL;
43 static void passdata(int in, int out)
50 len = read(in, buf, 65536);
52 flog(LOG_ERR, "sendfile: could not read input: %s", strerror(errno));
57 for(off = 0; off < len; off += ret) {
58 ret = write(out, buf + off, len - off);
60 flog(LOG_ERR, "sendfile: could not write output: %s", strerror(errno));
68 static char *attrmimetype(char *file)
71 static char buf[1024];
75 if((sz = getxattr(file, "user.ash-mime-type", buf, sizeof(buf) - 1)) > 0)
77 if((sz = getxattr(file, "user.mime-type", buf, sizeof(buf) - 1)) > 0)
79 if((sz = getxattr(file, "user.mime_type", buf, sizeof(buf) - 1)) > 0)
81 if((sz = getxattr(file, "user.Content-Type", buf, sizeof(buf) - 1)) > 0)
85 for(i = 0; i < sz; i++) {
86 if((buf[sz] < 32) || (buf[sz] >= 128))
96 static const char *getmimetype(char *file, struct stat *sb)
100 if((ret = attrmimetype(file)) != NULL)
103 cookie = magic_open(MAGIC_MIME_TYPE);
104 magic_load(cookie, NULL);
106 if((ret = magic_file(cookie, file)) != NULL)
108 return("application/octet-stream");
111 static void checkcache(char *file, struct stat *sb)
115 if((hdr = getenv("REQ_IF_MODIFIED_SINCE")) != NULL) {
116 if(parsehttpdate(hdr) < sb->st_mtime)
118 printf("HTTP/1.1 304 Not Modified\n");
119 printf("Date: %s\n", fmthttpdate(time(NULL)));
120 printf("Content-Length: 0\n");
126 static void usage(void)
128 flog(LOG_ERR, "usage: sendfile [-c CONTENT-TYPE] METHOD URL REST");
131 int main(int argc, char **argv)
140 while((c = getopt(argc, argv, "c:")) >= 0) {
151 if(argc - optind < 3) {
155 if((file = getenv("REQ_X_ASH_FILE")) == NULL) {
156 flog(LOG_ERR, "sendfile: needs to be called with the X-Ash-File header");
159 if(*argv[optind + 2]) {
160 simpleerror(1, 404, "Not Found", "The requested URL has no corresponding resource.");
163 if(stat(file, &sb) || ((fd = open(file, O_RDONLY)) < 0)) {
164 flog(LOG_ERR, "sendfile: could not stat input file %s: %s", file, strerror(errno));
165 simpleerror(1, 500, "Internal Error", "The server could not access its own data.");
169 contype = getmimetype(file, &sb);
171 checkcache(file, &sb);
173 printf("HTTP/1.1 200 OK\n");
174 printf("Content-Type: %s\n", contype);
175 printf("Content-Length: %ji\n", (intmax_t)sb.st_size);
176 printf("Last-Modified: %s\n", fmthttpdate(sb.st_mtime));
177 printf("Date: %s\n", fmthttpdate(time(NULL)));
180 if(strcasecmp(argv[optind], "head"))