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/>.
40 #include <attr/xattr.h>
43 static magic_t cookie = NULL;
45 static void passdata(int in, int out, off_t maxlen)
53 if((maxlen > 0) && (len > maxlen))
55 len = read(in, buf, len);
57 flog(LOG_ERR, "sendfile: could not read input: %s", strerror(errno));
62 for(off = 0; off < len; off += ret) {
63 ret = write(out, buf + off, len - off);
65 flog(LOG_ERR, "sendfile: could not write output: %s", strerror(errno));
70 if((maxlen -= len) <= 0)
77 static char *attrmimetype(char *file)
80 static char buf[1024];
84 if((sz = getxattr(file, "user.ash-mime-type", buf, sizeof(buf) - 1)) > 0)
86 if((sz = getxattr(file, "user.mime-type", buf, sizeof(buf) - 1)) > 0)
88 if((sz = getxattr(file, "user.mime_type", buf, sizeof(buf) - 1)) > 0)
90 if((sz = getxattr(file, "user.Content-Type", buf, sizeof(buf) - 1)) > 0)
94 for(i = 0; i < sz; i++) {
95 if((buf[sz] < 32) || (buf[sz] >= 128))
105 static const char *getmimetype(char *file, struct stat *sb)
109 if((ret = attrmimetype(file)) != NULL)
112 cookie = magic_open(MAGIC_MIME_TYPE | MAGIC_SYMLINK);
113 magic_load(cookie, NULL);
115 if((ret = magic_file(cookie, file)) != NULL)
117 return("application/octet-stream");
120 /* XXX: This could be made far better and check for other attributes
121 * and stuff, but not now. */
122 static const char *ckctype(const char *ctype)
124 if(!strncmp(ctype, "text/", 5) && (strchr(ctype, ';') == NULL))
125 return(sprintf2("%s; charset=%s", ctype, nl_langinfo(CODESET)));
129 static void checkcache(char *file, struct stat *sb)
133 if((hdr = getenv("REQ_IF_MODIFIED_SINCE")) != NULL) {
134 if(parsehttpdate(hdr) < sb->st_mtime)
136 printf("HTTP/1.1 304 Not Modified\n");
137 printf("Date: %s\n", fmthttpdate(time(NULL)));
138 printf("Content-Length: 0\n");
144 static void usage(void)
146 flog(LOG_ERR, "usage: sendfile [-c CONTENT-TYPE] METHOD URL REST");
149 static void sendwhole(int fd, struct stat *sb, const char *contype, int head)
151 printf("HTTP/1.1 200 OK\n");
152 printf("Content-Type: %s\n", contype);
153 printf("Content-Length: %ji\n", (intmax_t)sb->st_size);
154 printf("Last-Modified: %s\n", fmthttpdate(sb->st_mtime));
155 printf("Date: %s\n", fmthttpdate(time(NULL)));
162 static void sendrange(int fd, struct stat *sb, const char *contype, char *spec, int head)
164 char buf[strlen(spec) + 1];
168 if(strncmp(spec, "bytes=", 6))
170 strcpy(buf, spec + 6);
171 if((p = strchr(buf, '-')) == NULL)
177 start = end - strtoll(p + 1, &e, 10);
184 start = strtoll(buf, &e, 10);
188 end = strtoll(p, &e, 10) + 1;
195 if(start >= sb->st_size) {
196 printf("HTTP/1.1 416 Not satisfiable\n");
197 printf("Content-Range: */%ji\n", (intmax_t)sb->st_size);
198 printf("Content-Length: 0\n");
199 printf("Last-Modified: %s\n", fmthttpdate(sb->st_mtime));
200 printf("Date: %s\n", fmthttpdate(time(NULL)));
204 if((start < 0) || (start >= end))
206 if(end > sb->st_size)
209 if(lseek(fd, start, SEEK_SET) != start) {
210 simpleerror(1, 500, "Internal Error", "Could not seek properly to beginning of requested byte range.");
211 flog(LOG_ERR, "sendfile: could not seek properly when serving partial content: %s", strerror(errno));
214 printf("HTTP/1.1 206 Partial content\n");
215 printf("Content-Range: bytes %ji-%ji/%ji\n", (intmax_t)start, (intmax_t)(end - 1), (intmax_t)sb->st_size);
216 printf("Content-Length: %ji\n", (intmax_t)(end - start));
217 printf("Content-Type: %s\n", contype);
218 printf("Last-Modified: %s\n", fmthttpdate(sb->st_mtime));
219 printf("Date: %s\n", fmthttpdate(time(NULL)));
223 passdata(fd, 1, end - start);
227 sendwhole(fd, sb, contype, head);
230 int main(int argc, char **argv)
238 setlocale(LC_ALL, "");
240 while((c = getopt(argc, argv, "c:")) >= 0) {
251 if(argc - optind < 3) {
255 if((file = getenv("REQ_X_ASH_FILE")) == NULL) {
256 flog(LOG_ERR, "sendfile: needs to be called with the X-Ash-File header");
259 if(*argv[optind + 2]) {
260 simpleerror(1, 404, "Not Found", "The requested URL has no corresponding resource.");
263 if(stat(file, &sb) || ((fd = open(file, O_RDONLY)) < 0)) {
264 flog(LOG_ERR, "sendfile: could not stat input file %s: %s", file, strerror(errno));
265 simpleerror(1, 500, "Internal Error", "The server could not access its own data.");
269 contype = getmimetype(file, &sb);
270 contype = ckctype(contype);
272 checkcache(file, &sb);
274 ishead = !strcasecmp(argv[optind], "head");
275 if((hdr = getenv("REQ_RANGE")) != NULL)
276 sendrange(fd, &sb, contype, hdr, ishead);
278 sendwhole(fd, &sb, contype, ishead);