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)
412 if(block(fd, EV_READ, 0) == 0)
414 buf = smalloc(65536);
415 ret = recv(fd, buf, 65536, 0);
417 flog(LOG_WARNING, "received error on rootplex read channel: %s", strerror(errno));
419 } else if(ret == 0) {
424 /* Maybe I'd like to implement some protocol in this direction
428 shutdown(plex, SHUT_RDWR);
429 for(i = 0; i < listeners.d; i++) {
430 if(listeners.b[i] == muth)
431 bufdel(listeners, i);
434 flog(LOG_INFO, "root handler exited, so shutting down listening...");
435 while(listeners.d > 0)
436 resume(listeners.b[0], 0);
440 static void initroot(void *uu)
447 if((fd = open("/dev/null", O_RDWR)) >= 0) {
455 putenv("ASHD_USESYSLOG=1");
457 unsetenv("ASHD_USESYSLOG");
460 static void usage(FILE *out)
462 fprintf(out, "usage: htparser [-hSf] [-u USER] [-r ROOT] [-p PIDFILE] PORTSPEC... -- ROOT [ARGS...]\n");
463 fprintf(out, "\twhere PORTSPEC is HANDLER[:PAR[=VAL][(,PAR[=VAL])...]] (try HANDLER:help)\n");
464 fprintf(out, "\tavailable handlers are `plain' and `ssl'.\n");
467 static void addport(char *spec)
469 char *nm, *p, *p2, *n;
470 struct charvbuf pars, vals;
474 if((p = strchr(spec, ':')) == NULL) {
480 if((n = strchr(p, ',')) != NULL)
482 if((p2 = strchr(p, '=')) != NULL)
493 } while((p = n) != NULL);
496 /* XXX: It would be nice to decentralize this, but, meh... */
497 if(!strcmp(nm, "plain")) {
498 handleplain(pars.d, pars.b, vals.b);
500 } else if(!strcmp(nm, "ssl")) {
501 handlegnussl(pars.d, pars.b, vals.b);
504 flog(LOG_ERR, "htparser: unknown port handler `%s'", nm);
512 static void sighandler(int sig)
517 int main(int argc, char **argv)
523 struct passwd *pwent;
525 daemonize = usesyslog = 0;
528 while((c = getopt(argc, argv, "+hSfu:r:p:")) >= 0) {
540 if((pwent = getpwnam(optarg)) == NULL) {
541 flog(LOG_ERR, "could not find user %s", optarg);
557 for(i = optind; i < argc; i++) {
558 if(!strcmp(argv[i], "--"))
563 if(!s1 || (i == argc)) {
567 if((plex = stdmkchild(argv + ++i, initroot, NULL)) < 0) {
568 flog(LOG_ERR, "could not spawn root multiplexer: %s", strerror(errno));
571 bufadd(listeners, mustart(plexwatch, plex));
573 if(pidfile != NULL) {
574 if((pidout = fopen(pidfile, "w")) == NULL) {
575 flog(LOG_ERR, "could not open %s for writing: %s", pidfile, strerror(errno));
582 if(chdir(root) || chroot(root)) {
583 flog(LOG_ERR, "could not chroot to %s: %s", root, strerror(errno));
588 if(setgid(pwent->pw_gid)) {
589 flog(LOG_ERR, "could not switch group to %i: %s", (int)pwent->pw_gid, strerror(errno));
592 if(setuid(pwent->pw_uid)) {
593 flog(LOG_ERR, "could not switch user to %i: %s", (int)pwent->pw_uid, strerror(errno));
597 signal(SIGPIPE, SIG_IGN);
598 signal(SIGCHLD, SIG_IGN);
599 signal(SIGINT, sighandler);
600 signal(SIGTERM, sighandler);
605 fprintf(pidout, "%i\n", getpid());
615 if(listeners.d > 0) {
616 while(listeners.d > 0)
617 resume(listeners.b[0], 0);
618 flog(LOG_INFO, "no longer listening");