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;
44 struct mtbuf listeners;
46 static void trimx(struct hthead *req)
51 while(i < req->noheaders) {
52 if(!strncasecmp(req->headers[i][0], "x-ash-", 6)) {
53 free(req->headers[i][0]);
54 free(req->headers[i][1]);
55 free(req->headers[i]);
56 memmove(req->headers + i, req->headers + i + 1, sizeof(*req->headers) * (--req->noheaders - i));
63 static struct hthead *parsereq(FILE *in)
66 struct charbuf method, url, ver;
77 } else if((c == EOF) || (c < 32) || (c >= 128)) {
89 } else if((c == EOF) || (c < 32)) {
102 } else if((c == EOF) || (c < 32) || (c >= 128)) {
113 req = mkreq(method.b, url.b, ver.b);
114 if(parseheaders(req, in))
131 static off_t passdata(FILE *in, FILE *out, off_t max)
138 while(!feof(in) && ((max < 0) || (total < max))) {
141 read = min(max - total, read);
142 read = fread(buf, 1, read, in);
145 if(fwrite(buf, 1, read, out) != read)
152 static int recvchunks(FILE *in, FILE *out)
168 } else if((c >= '0') && (c <= '9')) {
169 chlen = (chlen << 4) + (c - '0');
171 } else if((c >= 'A') && (c <= 'F')) {
172 chlen = (chlen << 4) + (c + 10 - 'A');
174 } else if((c >= 'a') && (c <= 'f')) {
175 chlen = (chlen << 4) + (c + 10 - 'a');
178 /* XXX: Technically, there may be chunk extensions to
179 * be read, but since that will likely never actually
180 * happen in practice, I can just as well add support
181 * for that if it actually does become relevant. */
188 read = fread(buf, 1, min(sizeof(buf), chlen), in);
189 if(feof(in) || ferror(in))
191 if(fwrite(buf, 1, read, out) != read)
195 if((getc(in) != 13) || (getc(in) != 10))
198 /* XXX: Technically, there may be trailers to be read, but that's
199 * just about as likely as chunk extensions. */
200 if((getc(in) != 13) || (getc(in) != 10))
205 static int passchunks(FILE *in, FILE *out)
211 read = fread(buf, 1, sizeof(buf), in);
214 fprintf(out, "%zx\r\n", read);
215 if(fwrite(buf, 1, read, out) != read)
217 fprintf(out, "\r\n");
222 static int hasheader(struct hthead *head, char *name, char *val)
226 if((hd = getheader(head, name)) == NULL)
228 return(!strcasecmp(hd, val));
231 static int canonreq(struct hthead *req)
236 if(req->url[0] == '/') {
237 replrest(req, req->url + 1);
238 if((p = strchr(req->rest, '?')) != NULL)
242 if((p = strstr(req->url, "://")) != NULL) {
244 if(((n == 4) && !strncasecmp(req->url, "http", 4)) ||
245 ((n == 5) && !strncasecmp(req->url, "https", 5))) {
246 if(getheader(req, "host"))
249 if((p2 = strchr(p, '/')) == NULL) {
250 headappheader(req, "Host", p);
252 req->url = sstrdup("/");
256 headappheader(req, "Host", p);
260 replrest(req, req->url + 1);
261 if((p = strchr(req->rest, '?')) != NULL)
269 static int http10keep(struct hthead *req, struct hthead *resp)
273 fc = hasheader(resp, "connection", "close");
274 headrmheader(resp, "connection");
275 if(!fc && hasheader(req, "connection", "keep-alive")) {
276 headappheader(resp, "Connection", "Keep-Alive");
283 void serve(FILE *in, struct conn *conn)
287 struct hthead *req, *resp;
295 if((req = parsereq(in)) == NULL)
300 if((conn->initreq != NULL) && conn->initreq(conn, req))
303 if((plex < 0) || block(plex, EV_WRITE, 60) <= 0)
305 if(socketpair(PF_UNIX, SOCK_STREAM, 0, pfds))
307 if(sendreq(plex, req, pfds[0]))
310 out = mtstdopen(pfds[1], 1, 600, "r+");
312 if(getheader(req, "content-type") != NULL) {
313 if((hd = getheader(req, "content-length")) != NULL) {
316 if(passdata(in, out, dlen) != dlen)
319 } else if(((hd = getheader(req, "transfer-encoding")) != NULL) && !strcasecmp(hd, "chunked")) {
320 if(recvchunks(in, out))
323 /* Ignore rather than abort, to be kinder to broken clients. */
324 headrmheader(req, "content-type");
329 /* Make sure to send EOF */
330 shutdown(pfds[1], SHUT_WR);
332 if((resp = parseresponse(out)) == NULL)
334 replstr(&resp->ver, req->ver);
336 if(!getheader(resp, "server"))
337 headappheader(resp, "Server", sprintf3("ashd/%s", VERSION));
339 if(!strcasecmp(req->ver, "HTTP/1.0")) {
340 if(!strcasecmp(req->method, "head")) {
341 keep = http10keep(req, resp);
344 } else if((hd = getheader(resp, "content-length")) != NULL) {
345 keep = http10keep(req, resp);
349 if(passdata(out, in, dlen) != dlen)
352 headrmheader(resp, "connection");
355 passdata(out, in, -1);
360 } else if(!strcasecmp(req->ver, "HTTP/1.1")) {
361 if(!strcasecmp(req->method, "head")) {
364 } else if((hd = getheader(resp, "content-length")) != NULL) {
368 if(passdata(out, in, dlen) != dlen)
370 } else if(!getheader(resp, "transfer-encoding")) {
371 headappheader(resp, "Transfer-Encoding", "chunked");
374 if(passchunks(out, in))
379 passdata(out, in, -1);
382 if(hasheader(req, "connection", "close") || hasheader(resp, "connection", "close"))
404 static void plexwatch(struct muth *muth, va_list args)
411 if(block(fd, EV_READ, 0) == 0)
413 buf = smalloc(65536);
414 ret = recv(fd, buf, 65536, 0);
416 flog(LOG_WARNING, "received error on rootplex read channel: %s", strerror(errno));
418 } else if(ret == 0) {
422 /* Maybe I'd like to implement some protocol in this direction
428 for(i = 0; i < listeners.d; i++) {
429 if(listeners.b[i] == muth)
430 bufdel(listeners, i);
432 flog(LOG_INFO, "root handler exited, so shutting down listening...");
433 while(listeners.d > 0)
434 resume(listeners.b[0], 0);
437 static void initroot(void *uu)
444 if((fd = open("/dev/null", O_RDWR)) >= 0) {
452 putenv("ASHD_USESYSLOG=1");
454 unsetenv("ASHD_USESYSLOG");
457 static void usage(FILE *out)
459 fprintf(out, "usage: htparser [-hSf] [-u USER] [-r ROOT] [-p PIDFILE] PORTSPEC... -- ROOT [ARGS...]\n");
460 fprintf(out, "\twhere PORTSPEC is HANDLER[:PAR[=VAL][(,PAR[=VAL])...]] (try HANDLER:help)\n");
461 fprintf(out, "\tavailable handlers are `plain' and `ssl'.\n");
464 static void addport(char *spec)
466 char *nm, *p, *p2, *n;
467 struct charvbuf pars, vals;
471 if((p = strchr(spec, ':')) == NULL) {
477 if((n = strchr(p, ',')) != NULL)
479 if((p2 = strchr(p, '=')) != NULL)
490 } while((p = n) != NULL);
493 /* XXX: It would be nice to decentralize this, but, meh... */
494 if(!strcmp(nm, "plain")) {
495 handleplain(pars.d, pars.b, vals.b);
497 } else if(!strcmp(nm, "ssl")) {
498 handlegnussl(pars.d, pars.b, vals.b);
501 flog(LOG_ERR, "htparser: unknown port handler `%s'", nm);
509 static void sighandler(int sig)
514 int main(int argc, char **argv)
520 struct passwd *pwent;
522 daemonize = usesyslog = 0;
525 while((c = getopt(argc, argv, "+hSfu:r:p:")) >= 0) {
537 if((pwent = getpwnam(optarg)) == NULL) {
538 flog(LOG_ERR, "could not find user %s", optarg);
554 for(i = optind; i < argc; i++) {
555 if(!strcmp(argv[i], "--"))
560 if(!s1 || (i == argc)) {
564 if((plex = stdmkchild(argv + ++i, initroot, NULL)) < 0) {
565 flog(LOG_ERR, "could not spawn root multiplexer: %s", strerror(errno));
568 bufadd(listeners, mustart(plexwatch, plex));
570 if(pidfile != NULL) {
571 if((pidout = fopen(pidfile, "w")) == NULL) {
572 flog(LOG_ERR, "could not open %s for writing: %s", pidfile, strerror(errno));
579 if(chdir(root) || chroot(root)) {
580 flog(LOG_ERR, "could not chroot to %s: %s", root, strerror(errno));
585 if(setgid(pwent->pw_gid)) {
586 flog(LOG_ERR, "could not switch group to %i: %s", (int)pwent->pw_gid, strerror(errno));
589 if(setuid(pwent->pw_uid)) {
590 flog(LOG_ERR, "could not switch user to %i: %s", (int)pwent->pw_uid, strerror(errno));
594 signal(SIGPIPE, SIG_IGN);
595 signal(SIGCHLD, SIG_IGN);
596 signal(SIGINT, sighandler);
597 signal(SIGTERM, sighandler);
602 fprintf(pidout, "%i\n", getpid());
612 if(listeners.d > 0) {
613 while(listeners.d > 0)
614 resume(listeners.b[0], 0);
615 flog(LOG_INFO, "no longer listening");