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)
52 len = read(in, buf, 65536);
54 flog(LOG_ERR, "sendfile: could not read input: %s", strerror(errno));
59 for(off = 0; off < len; off += ret) {
60 ret = write(out, buf + off, len - off);
62 flog(LOG_ERR, "sendfile: could not write output: %s", strerror(errno));
70 static char *attrmimetype(char *file)
73 static char buf[1024];
77 if((sz = getxattr(file, "user.ash-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.mime_type", buf, sizeof(buf) - 1)) > 0)
83 if((sz = getxattr(file, "user.Content-Type", buf, sizeof(buf) - 1)) > 0)
87 for(i = 0; i < sz; i++) {
88 if((buf[sz] < 32) || (buf[sz] >= 128))
98 static const char *getmimetype(char *file, struct stat *sb)
102 if((ret = attrmimetype(file)) != NULL)
105 cookie = magic_open(MAGIC_MIME_TYPE | MAGIC_SYMLINK);
106 magic_load(cookie, NULL);
108 if((ret = magic_file(cookie, file)) != NULL)
110 return("application/octet-stream");
113 /* XXX: This could be made far better and check for other attributes
114 * and stuff, but not now. */
115 static const char *ckctype(const char *ctype)
117 if(!strncmp(ctype, "text/", 5) && (strchr(ctype, ';') == NULL))
118 return(sprintf2("%s; charset=%s", ctype, nl_langinfo(CODESET)));
122 static void checkcache(char *file, struct stat *sb)
126 if((hdr = getenv("REQ_IF_MODIFIED_SINCE")) != NULL) {
127 if(parsehttpdate(hdr) < sb->st_mtime)
129 printf("HTTP/1.1 304 Not Modified\n");
130 printf("Date: %s\n", fmthttpdate(time(NULL)));
131 printf("Content-Length: 0\n");
137 static void usage(void)
139 flog(LOG_ERR, "usage: sendfile [-c CONTENT-TYPE] METHOD URL REST");
142 int main(int argc, char **argv)
150 setlocale(LC_ALL, "");
152 while((c = getopt(argc, argv, "c:")) >= 0) {
163 if(argc - optind < 3) {
167 if((file = getenv("REQ_X_ASH_FILE")) == NULL) {
168 flog(LOG_ERR, "sendfile: needs to be called with the X-Ash-File header");
171 if(*argv[optind + 2]) {
172 simpleerror(1, 404, "Not Found", "The requested URL has no corresponding resource.");
175 if(stat(file, &sb) || ((fd = open(file, O_RDONLY)) < 0)) {
176 flog(LOG_ERR, "sendfile: could not stat input file %s: %s", file, strerror(errno));
177 simpleerror(1, 500, "Internal Error", "The server could not access its own data.");
181 contype = getmimetype(file, &sb);
182 contype = ckctype(contype);
184 checkcache(file, &sb);
186 printf("HTTP/1.1 200 OK\n");
187 printf("Content-Type: %s\n", contype);
188 printf("Content-Length: %ji\n", (intmax_t)sb.st_size);
189 printf("Last-Modified: %s\n", fmthttpdate(sb.st_mtime));
190 printf("Date: %s\n", fmthttpdate(time(NULL)));
193 if(strcasecmp(argv[optind], "head"))