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>
43 static char *pidfile = NULL;
44 static int daemonize, usesyslog;
45 struct mtbuf listeners;
47 static void trimx(struct hthead *req)
52 while(i < req->noheaders) {
53 if(!strncasecmp(req->headers[i][0], "x-ash-", 6)) {
54 free(req->headers[i][0]);
55 free(req->headers[i][1]);
56 free(req->headers[i]);
57 memmove(req->headers + i, req->headers + i + 1, sizeof(*req->headers) * (--req->noheaders - i));
64 static struct hthead *parsereq(struct bufio *in)
67 struct charbuf method, url, ver;
78 } else if((c == EOF) || (c < 32) || (c >= 128)) {
90 } else if((c == EOF) || (c < 32)) {
103 } else if((c == EOF) || (c < 32) || (c >= 128)) {
114 req = mkreq(method.b, url.b, ver.b);
115 if(parseheadersb(req, in))
132 static off_t passdata(struct bufio *in, struct bufio *out, off_t max)
138 while(!bioeof(in) && ((max < 0) || (total < max))) {
139 if((read = biordata(in)) > 0) {
141 read = min(max - total, read);
142 if((read = biowritesome(out, in->rbuf.b + in->rh, read)) < 0)
147 if(biorspace(in) && ((max < 0) || (biordata(in) < max - total)) && (biofillsome(in) < 0))
153 static int recvchunks(struct bufio *in, struct bufio *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 if((read = biordata(in)) > 0) {
189 if((read = biowritesome(out, in->rbuf.b + in->rh, min(read, chlen))) < 0)
194 if(biorspace(in) && (biordata(in) < chlen) && (biofillsome(in) <= 0))
197 if((biogetc(in) != 13) || (biogetc(in) != 10))
200 /* XXX: Technically, there may be trailers to be read, but that's
201 * just about as likely as chunk extensions. */
202 if((biogetc(in) != 13) || (biogetc(in) != 10))
207 static int passchunks(struct bufio *in, struct bufio *out)
212 if((read = biordata(in)) > 0) {
213 bioprintf(out, "%zx\r\n", read);
214 if(biowrite(out, in->rbuf.b + in->rh, read) != read)
217 bioprintf(out, "\r\n");
218 if(bioflush(out) < 0)
221 if(biorspace(in) && (biofillsome(in) < 0))
224 bioprintf(out, "0\r\n\r\n");
228 static int hasheader(struct hthead *head, char *name, char *val)
232 if((hd = getheader(head, name)) == NULL)
234 return(!strcasecmp(hd, val));
237 static int canonreq(struct hthead *req)
242 if(req->url[0] == '/') {
243 replrest(req, req->url + 1);
244 if((p = strchr(req->rest, '?')) != NULL)
248 if((p = strstr(req->url, "://")) != NULL) {
250 if(((n == 4) && !strncasecmp(req->url, "http", 4)) ||
251 ((n == 5) && !strncasecmp(req->url, "https", 5))) {
252 if(getheader(req, "host"))
255 if((p2 = strchr(p, '/')) == NULL) {
256 headappheader(req, "Host", p);
258 req->url = sstrdup("/");
262 headappheader(req, "Host", p);
266 replrest(req, req->url + 1);
267 if((p = strchr(req->rest, '?')) != NULL)
275 static int http10keep(struct hthead *req, struct hthead *resp)
279 fc = hasheader(resp, "connection", "close");
280 headrmheader(resp, "connection");
281 if(!fc && hasheader(req, "connection", "keep-alive")) {
282 headappheader(resp, "Connection", "Keep-Alive");
289 static char *connid(void)
291 static struct charbuf cur;
295 for(i = 0; i < cur.d; i++) {
296 if((++cur.b[i]) > 'Z')
303 ret = memcpy(smalloc(cur.d + 1), cur.b, cur.d);
308 void serve(struct bufio *in, struct conn *conn)
312 struct hthead *req, *resp;
322 if((req = parsereq(in)) == NULL)
327 headappheader(req, "X-Ash-Connection-ID", id);
328 if((conn->initreq != NULL) && conn->initreq(conn, req))
331 if((plex < 0) || block(plex, EV_WRITE, 60) <= 0)
333 if(socketpair(PF_UNIX, SOCK_STREAM, 0, pfds))
335 if(sendreq(plex, req, pfds[0]))
338 out = mtbioopen(pfds[1], 1, 600, "r+", NULL);
340 if(getheader(req, "content-type") != NULL) {
341 if((hd = getheader(req, "content-length")) != NULL) {
344 if(passdata(in, out, dlen) != dlen)
347 } else if(((hd = getheader(req, "transfer-encoding")) != NULL) && !strcasecmp(hd, "chunked")) {
348 if(recvchunks(in, out))
351 /* Ignore rather than abort, to be kinder to broken clients. */
352 headrmheader(req, "content-type");
357 /* Make sure to send EOF */
358 shutdown(pfds[1], SHUT_WR);
360 if((resp = parseresponseb(out)) == NULL)
362 replstr(&resp->ver, req->ver);
364 if(!getheader(resp, "server"))
365 headappheader(resp, "Server", sprintf3("ashd/%s", VERSION));
367 if(!strcasecmp(req->ver, "HTTP/1.0")) {
368 if(!strcasecmp(req->method, "head")) {
369 keep = http10keep(req, resp);
370 writerespb(in, resp);
371 bioprintf(in, "\r\n");
372 } else if((hd = getheader(resp, "content-length")) != NULL) {
373 keep = http10keep(req, resp);
375 writerespb(in, resp);
376 bioprintf(in, "\r\n");
377 if(passdata(out, in, dlen) != dlen)
380 headrmheader(resp, "connection");
381 writerespb(in, resp);
382 bioprintf(in, "\r\n");
383 passdata(out, in, -1);
388 } else if(!strcasecmp(req->ver, "HTTP/1.1")) {
389 if(!strcasecmp(req->method, "head")) {
390 writerespb(in, resp);
391 bioprintf(in, "\r\n");
392 } else if((hd = getheader(resp, "content-length")) != NULL) {
393 writerespb(in, resp);
394 bioprintf(in, "\r\n");
396 if(passdata(out, in, dlen) != dlen)
398 } else if(!getheader(resp, "transfer-encoding")) {
399 headappheader(resp, "Transfer-Encoding", "chunked");
400 writerespb(in, resp);
401 bioprintf(in, "\r\n");
402 if(passchunks(out, in))
405 writerespb(in, resp);
406 bioprintf(in, "\r\n");
407 passdata(out, in, -1);
410 if(hasheader(req, "connection", "close") || hasheader(resp, "connection", "close"))
433 static void plexwatch(struct muth *muth, va_list args)
441 if(block(fd, EV_READ, 0) == 0)
443 buf = smalloc(65536);
444 ret = recv(fd, buf, 65536, 0);
446 flog(LOG_WARNING, "received error on rootplex read channel: %s", strerror(errno));
448 } else if(ret == 0) {
453 /* Maybe I'd like to implement some protocol in this direction
457 shutdown(plex, SHUT_RDWR);
458 for(i = 0; i < listeners.d; i++) {
459 if(listeners.b[i] == muth)
460 bufdel(listeners, i);
463 flog(LOG_INFO, "root handler exited, so shutting down listening...");
464 while(listeners.d > 0)
465 resume(listeners.b[0], 0);
469 static void initroot(void *uu)
476 if((fd = open("/dev/null", O_RDWR)) >= 0) {
484 putenv("ASHD_USESYSLOG=1");
486 unsetenv("ASHD_USESYSLOG");
489 static void usage(FILE *out)
491 fprintf(out, "usage: htparser [-hSf] [-u USER] [-r ROOT] [-p PIDFILE] PORTSPEC... -- ROOT [ARGS...]\n");
492 fprintf(out, "\twhere PORTSPEC is HANDLER[:PAR[=VAL][(,PAR[=VAL])...]] (try HANDLER:help)\n");
493 fprintf(out, "\tavailable handlers are `plain' and `ssl'.\n");
496 static void addport(char *spec)
498 char *nm, *p, *p2, *n;
499 struct charvbuf pars, vals;
503 if((p = strchr(spec, ':')) == NULL) {
509 if((n = strchr(p, ',')) != NULL)
511 if((p2 = strchr(p, '=')) != NULL)
522 } while((p = n) != NULL);
525 /* XXX: It would be nice to decentralize this, but, meh... */
526 if(!strcmp(nm, "plain")) {
527 handleplain(pars.d, pars.b, vals.b);
529 } else if(!strcmp(nm, "ssl")) {
530 handlegnussl(pars.d, pars.b, vals.b);
533 flog(LOG_ERR, "htparser: unknown port handler `%s'", nm);
541 static void sighandler(int sig)
546 int main(int argc, char **argv)
552 struct passwd *pwent;
554 daemonize = usesyslog = 0;
557 while((c = getopt(argc, argv, "+hSfu:r:p:")) >= 0) {
569 if((pwent = getpwnam(optarg)) == NULL) {
570 flog(LOG_ERR, "could not find user %s", optarg);
586 for(i = optind; i < argc; i++) {
587 if(!strcmp(argv[i], "--"))
592 if(!s1 || (i == argc)) {
596 if((plex = stdmkchild(argv + ++i, initroot, NULL)) < 0) {
597 flog(LOG_ERR, "could not spawn root multiplexer: %s", strerror(errno));
600 bufadd(listeners, mustart(plexwatch, plex));
602 if(pidfile != NULL) {
603 if((pidout = fopen(pidfile, "w")) == NULL) {
604 flog(LOG_ERR, "could not open %s for writing: %s", pidfile, strerror(errno));
611 if(chdir(root) || chroot(root)) {
612 flog(LOG_ERR, "could not chroot to %s: %s", root, strerror(errno));
617 if(setgid(pwent->pw_gid)) {
618 flog(LOG_ERR, "could not switch group to %i: %s", (int)pwent->pw_gid, strerror(errno));
621 if(setuid(pwent->pw_uid)) {
622 flog(LOG_ERR, "could not switch user to %i: %s", (int)pwent->pw_uid, strerror(errno));
626 signal(SIGPIPE, SIG_IGN);
627 signal(SIGCHLD, SIG_IGN);
628 signal(SIGINT, sighandler);
629 signal(SIGTERM, sighandler);
634 fprintf(pidout, "%i\n", getpid());
644 if(listeners.d > 0) {
645 while(listeners.d > 0)
646 resume(listeners.b[0], 0);
647 flog(LOG_INFO, "no longer listening");