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[i] < 32) || (buf[i] >= 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 sendwhole(int fd, struct stat *sb, const char *contype, int head)
146 printf("HTTP/1.1 200 OK\n");
147 printf("Content-Type: %s\n", contype);
148 printf("Content-Length: %ji\n", (intmax_t)sb->st_size);
149 printf("Last-Modified: %s\n", fmthttpdate(sb->st_mtime));
150 printf("Date: %s\n", fmthttpdate(time(NULL)));
157 static void sendrange(int fd, struct stat *sb, const char *contype, 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 printf("HTTP/1.1 416 Not satisfiable\n");
192 printf("Content-Range: */%ji\n", (intmax_t)sb->st_size);
193 printf("Content-Length: 0\n");
194 printf("Last-Modified: %s\n", fmthttpdate(sb->st_mtime));
195 printf("Date: %s\n", fmthttpdate(time(NULL)));
199 if((start < 0) || (start >= end))
201 if(end > sb->st_size)
204 if(lseek(fd, start, SEEK_SET) != start) {
205 simpleerror(1, 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 printf("HTTP/1.1 206 Partial content\n");
210 printf("Content-Range: bytes %ji-%ji/%ji\n", (intmax_t)start, (intmax_t)(end - 1), (intmax_t)sb->st_size);
211 printf("Content-Length: %ji\n", (intmax_t)(end - start));
212 printf("Content-Type: %s\n", contype);
213 printf("Last-Modified: %s\n", fmthttpdate(sb->st_mtime));
214 printf("Date: %s\n", fmthttpdate(time(NULL)));
218 passdata(fd, 1, end - start);
222 sendwhole(fd, sb, contype, head);
225 static void usage(void)
227 flog(LOG_ERR, "usage: sendfile [-c CONTENT-TYPE] [-f FILE] METHOD URL REST");
230 int main(int argc, char **argv)
235 int fd, ishead, ignrest;
238 setlocale(LC_ALL, "");
242 while((c = getopt(argc, argv, "c:f:")) >= 0) {
257 if(argc - optind < 3) {
261 if((file == NULL) && ((file = getenv("REQ_X_ASH_FILE")) == NULL)) {
262 flog(LOG_ERR, "sendfile: needs to be called with either the X-Ash-File header or the -f option");
265 if(!ignrest && *argv[optind + 2]) {
266 simpleerror(1, 404, "Not Found", "The requested URL has no corresponding resource.");
269 if(stat(file, &sb) || ((fd = open(file, O_RDONLY)) < 0)) {
270 flog(LOG_ERR, "sendfile: could not stat input file %s: %s", file, strerror(errno));
271 simpleerror(1, 500, "Internal Error", "The server could not access its own data.");
274 if(!strcasecmp(argv[optind], "get")) {
276 } else if(!strcasecmp(argv[optind], "head")) {
279 simpleerror(1, 405, "Method not allowed", "The requested method is not defined for this resource.");
282 if(contype == NULL) {
283 if((hdr = getenv("REQ_X_ASH_CONTENT_TYPE")) != NULL)
286 contype = getmimetype(file, &sb);
288 contype = ckctype(contype);
290 checkcache(file, &sb);
292 if((hdr = getenv("REQ_RANGE")) != NULL)
293 sendrange(fd, &sb, contype, hdr, ishead);
295 sendwhole(fd, &sb, contype, ishead);