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/>.
24 #include <sys/socket.h>
26 #include <sys/signal.h>
42 static char *pidfile = NULL;
43 static int daemonize, usesyslog;
45 static void trimx(struct hthead *req)
50 while(i < req->noheaders) {
51 if(!strncasecmp(req->headers[i][0], "x-ash-", 6)) {
52 free(req->headers[i][0]);
53 free(req->headers[i][1]);
54 free(req->headers[i]);
55 memmove(req->headers + i, req->headers + i + 1, sizeof(*req->headers) * (--req->noheaders - i));
62 static struct hthead *parsereq(FILE *in)
65 struct charbuf method, url, ver;
76 } else if((c == EOF) || (c < 32) || (c >= 128)) {
88 } else if((c == EOF) || (c < 32)) {
101 } else if((c == EOF) || (c < 32) || (c >= 128)) {
112 req = mkreq(method.b, url.b, ver.b);
113 if(parseheaders(req, in))
130 static off_t passdata(FILE *in, FILE *out, off_t max)
137 while(!feof(in) && ((max < 0) || (total < max))) {
140 read = min(max - total, read);
141 read = fread(buf, 1, read, in);
144 if(fwrite(buf, 1, read, out) != read)
151 static int passchunks(FILE *in, FILE *out)
157 read = fread(buf, 1, sizeof(buf), in);
160 fprintf(out, "%zx\r\n", read);
161 if(fwrite(buf, 1, read, out) != read)
163 fprintf(out, "\r\n");
168 static int hasheader(struct hthead *head, char *name, char *val)
172 if((hd = getheader(head, name)) == NULL)
174 return(!strcasecmp(hd, val));
177 static int canonreq(struct hthead *req)
182 if(req->url[0] == '/') {
183 replrest(req, req->url + 1);
184 if((p = strchr(req->rest, '?')) != NULL)
188 if((p = strstr(req->url, "://")) != NULL) {
190 if(((n == 4) && !strncasecmp(req->url, "http", 4)) ||
191 ((n == 5) && !strncasecmp(req->url, "https", 5))) {
192 if(getheader(req, "host"))
195 if((p2 = strchr(p, '/')) == NULL) {
196 headappheader(req, "Host", p);
198 req->url = sstrdup("/");
202 headappheader(req, "Host", p);
206 replrest(req, req->url + 1);
207 if((p = strchr(req->rest, '?')) != NULL)
215 static int http10keep(struct hthead *req, struct hthead *resp)
219 fc = hasheader(resp, "connection", "close");
220 headrmheader(resp, "connection");
221 if(!fc && hasheader(req, "connection", "keep-alive")) {
222 headappheader(resp, "Connection", "Keep-Alive");
229 void serve(FILE *in, struct conn *conn)
233 struct hthead *req, *resp;
241 if((req = parsereq(in)) == NULL)
246 if((conn->initreq != NULL) && conn->initreq(conn, req))
249 if(block(plex, EV_WRITE, 60) <= 0)
251 if(socketpair(PF_UNIX, SOCK_STREAM, 0, pfds))
253 if(sendreq(plex, req, pfds[0]))
256 out = mtstdopen(pfds[1], 1, 600, "r+");
258 if((hd = getheader(req, "content-length")) != NULL) {
261 if(passdata(in, out, dlen) != dlen)
267 /* Make sure to send EOF */
268 shutdown(pfds[1], SHUT_WR);
270 if((resp = parseresponse(out)) == NULL)
272 replstr(&resp->ver, req->ver);
274 if(!getheader(resp, "server"))
275 headappheader(resp, "Server", sprintf3("ashd/%s", VERSION));
277 if(!strcmp(req->ver, "HTTP/1.0")) {
278 if(!strcasecmp(req->method, "head")) {
279 keep = http10keep(req, resp);
282 } else if((hd = getheader(resp, "content-length")) != NULL) {
283 keep = http10keep(req, resp);
287 if(passdata(out, in, dlen) != dlen)
290 headrmheader(resp, "connection");
293 passdata(out, in, -1);
298 } else if(!strcmp(req->ver, "HTTP/1.1")) {
299 if(!strcasecmp(req->method, "head")) {
302 } else if((hd = getheader(resp, "content-length")) != NULL) {
306 if(passdata(out, in, dlen) != dlen)
308 } else if(!getheader(resp, "transfer-encoding")) {
309 headappheader(resp, "Transfer-Encoding", "chunked");
312 if(passchunks(out, in))
317 passdata(out, in, -1);
320 if(hasheader(req, "connection", "close") || hasheader(resp, "connection", "close"))
342 static void plexwatch(struct muth *muth, va_list args)
349 block(fd, EV_READ, 0);
350 buf = smalloc(65536);
351 ret = recv(fd, buf, 65536, 0);
353 flog(LOG_WARNING, "received error on rootplex read channel: %s", strerror(errno));
355 } else if(ret == 0) {
358 /* Maybe I'd like to implement some protocol in this direction
364 static void initroot(void *uu)
371 if((fd = open("/dev/null", O_RDWR)) >= 0) {
379 putenv("ASHD_USESYSLOG=1");
381 unsetenv("ASHD_USESYSLOG");
384 static void usage(FILE *out)
386 fprintf(out, "usage: htparser [-hSf] [-u USER] [-r ROOT] [-p PIDFILE] PORTSPEC... -- ROOT [ARGS...]\n");
387 fprintf(out, "\twhere PORTSPEC is HANDLER[:PAR[=VAL][(,PAR[=VAL])...]] (try HANDLER:help)\n");
388 fprintf(out, "\tavailable handlers are `plain' and `ssl'.\n");
391 static void addport(char *spec)
393 char *nm, *p, *p2, *n;
394 struct charvbuf pars, vals;
398 if((p = strchr(spec, ':')) == NULL) {
404 if((n = strchr(p, ',')) != NULL)
406 if((p2 = strchr(p, '=')) != NULL)
417 } while((p = n) != NULL);
420 /* XXX: It would be nice to decentralize this, but, meh... */
421 if(!strcmp(nm, "plain")) {
422 handleplain(pars.d, pars.b, vals.b);
424 } else if(!strcmp(nm, "ssl")) {
425 handlegnussl(pars.d, pars.b, vals.b);
428 flog(LOG_ERR, "htparser: unknown port handler `%s'", nm);
436 int main(int argc, char **argv)
442 struct passwd *pwent;
444 daemonize = usesyslog = 0;
447 while((c = getopt(argc, argv, "+hSfu:r:p:")) >= 0) {
459 if((pwent = getpwnam(optarg)) == NULL) {
460 flog(LOG_ERR, "could not find user %s", optarg);
476 for(i = optind; i < argc; i++) {
477 if(!strcmp(argv[i], "--"))
482 if(!s1 || (i == argc)) {
486 if((plex = stdmkchild(argv + ++i, initroot, NULL)) < 0) {
487 flog(LOG_ERR, "could not spawn root multiplexer: %s", strerror(errno));
490 mustart(plexwatch, plex);
492 if(pidfile != NULL) {
493 if((pidout = fopen(pidfile, "w")) == NULL) {
494 flog(LOG_ERR, "could not open %s for writing: %s", pidfile, strerror(errno));
501 if(chdir(root) || chroot(root)) {
502 flog(LOG_ERR, "could not chroot to %s: %s", root, strerror(errno));
507 if(setgid(pwent->pw_gid)) {
508 flog(LOG_ERR, "could not switch group to %i: %s", (int)pwent->pw_gid, strerror(errno));
511 if(setuid(pwent->pw_uid)) {
512 flog(LOG_ERR, "could not switch user to %i: %s", (int)pwent->pw_uid, strerror(errno));
516 signal(SIGPIPE, SIG_IGN);
521 fprintf(pidout, "%i\n", getpid());