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/>.
23 #include <sys/socket.h>
25 #include <sys/signal.h>
41 static char *pidfile = NULL;
43 static void trimx(struct hthead *req)
48 while(i < req->noheaders) {
49 if(!strncasecmp(req->headers[i][0], "x-ash-", 6)) {
50 free(req->headers[i][0]);
51 free(req->headers[i][1]);
52 free(req->headers[i]);
53 memmove(req->headers + i, req->headers + i + 1, sizeof(*req->headers) * (--req->noheaders - i));
60 static struct hthead *parsereq(FILE *in)
63 struct charbuf method, url, ver;
74 } else if((c == EOF) || (c < 32) || (c >= 128)) {
84 } else if((c == EOF) || (c < 32)) {
95 } else if((c == EOF) || (c < 32) || (c >= 128)) {
104 req = mkreq(method.b, url.b, ver.b);
105 if(parseheaders(req, in))
122 static struct hthead *parseresp(FILE *in)
126 struct charbuf ver, msg;
137 } else if((c == EOF) || (c < 32) || (c >= 128)) {
147 } else if((c == EOF) || (c < '0') || (c > '9')) {
150 code = (code * 10) + (c - '0');
158 } else if((c == EOF) || (c < 32)) {
166 req = mkresp(code, msg.b, ver.b);
167 if(parseheaders(req, in))
182 static off_t passdata(FILE *in, FILE *out, off_t max)
189 while(!feof(in) && ((max < 0) || (total < max))) {
192 read = min(max - total, read);
193 read = fread(buf, 1, read, in);
196 if(fwrite(buf, 1, read, out) != read)
203 static int passchunks(FILE *in, FILE *out)
209 read = fread(buf, 1, sizeof(buf), in);
212 fprintf(out, "%zx\r\n", read);
213 if(fwrite(buf, 1, read, out) != read)
215 fprintf(out, "\r\n");
220 static int hasheader(struct hthead *head, char *name, char *val)
224 if((hd = getheader(head, name)) == NULL)
226 return(!strcasecmp(hd, val));
229 void serve(FILE *in, struct conn *conn)
233 struct hthead *req, *resp;
240 if((req = parsereq(in)) == NULL)
242 replrest(req, req->url);
243 if(req->rest[0] == '/')
244 replrest(req, req->rest + 1);
245 if((p = strchr(req->rest, '?')) != NULL)
248 if((conn->initreq != NULL) && conn->initreq(conn, req))
251 if(block(plex, EV_WRITE, 60) <= 0)
253 if(socketpair(PF_UNIX, SOCK_STREAM, 0, pfds))
255 if(sendreq(plex, req, pfds[0]))
258 out = mtstdopen(pfds[1], 1, 600, "r+");
260 if((hd = getheader(req, "content-length")) != NULL) {
263 if(passdata(in, out, dlen) != dlen)
269 /* Make sure to send EOF */
270 shutdown(pfds[1], SHUT_WR);
272 if((resp = parseresp(out)) == NULL)
274 replstr(&resp->ver, req->ver);
276 if(!strcmp(req->ver, "HTTP/1.0")) {
279 if((hd = getheader(resp, "content-length")) != NULL) {
280 dlen = passdata(out, in, -1);
283 if(!hasheader(req, "connection", "keep-alive"))
286 passdata(out, in, -1);
289 if(hasheader(req, "connection", "close") || hasheader(resp, "connection", "close"))
291 } else if(!strcmp(req->ver, "HTTP/1.1")) {
292 if((hd = getheader(resp, "content-length")) != NULL) {
295 dlen = passdata(out, in, -1);
298 } else if(!getheader(resp, "transfer-encoding")) {
299 headappheader(resp, "Transfer-Encoding", "chunked");
302 if(passchunks(out, in))
307 passdata(out, in, -1);
310 if(hasheader(req, "connection", "close") || hasheader(resp, "connection", "close"))
332 static void plexwatch(struct muth *muth, va_list args)
339 block(fd, EV_READ, 0);
340 buf = smalloc(65536);
341 ret = recv(fd, buf, 65536, 0);
343 flog(LOG_WARNING, "received error on rootplex read channel: %s", strerror(errno));
345 } else if(ret == 0) {
348 /* Maybe I'd like to implement some protocol in this direction
354 static void usage(FILE *out)
356 fprintf(out, "usage: htparser [-hSf] [-u USER] [-r ROOT] [-p PIDFILE] PORTSPEC... -- ROOT [ARGS...]\n");
357 fprintf(out, "\twhere PORTSPEC is HANDLER[:PAR[=VAL][(,PAR[=VAL])...]] (try HANDLER:help)\n");
358 fprintf(out, "\tavailable handlers are `plain' and `ssl'.\n");
361 static void addport(char *spec)
363 char *nm, *p, *p2, *n;
364 struct charvbuf pars, vals;
368 if((p = strchr(spec, ':')) == NULL) {
374 if((n = strchr(p, ',')) != NULL)
376 if((p2 = strchr(p, '=')) != NULL)
387 } while((p = n) != NULL);
390 /* XXX: It would be nice to decentralize this, but, meh... */
391 if(!strcmp(nm, "plain")) {
392 handleplain(pars.d, pars.b, vals.b);
394 } else if(!strcmp(nm, "ssl")) {
395 handlegnussl(pars.d, pars.b, vals.b);
398 flog(LOG_ERR, "htparser: unknown port handler `%s'", nm);
406 int main(int argc, char **argv)
410 int daemonize, logsys;
413 struct passwd *pwent;
415 daemonize = logsys = 0;
418 while((c = getopt(argc, argv, "+hSfu:r:p:")) >= 0) {
430 if((pwent = getpwnam(optarg)) == NULL) {
431 flog(LOG_ERR, "could not find user %s", optarg);
447 for(i = optind; i < argc; i++) {
448 if(!strcmp(argv[i], "--"))
453 if(!s1 || (i == argc)) {
457 if((plex = stdmkchild(argv + ++i, NULL, NULL)) < 0) {
458 flog(LOG_ERR, "could not spawn root multiplexer: %s", strerror(errno));
461 mustart(plexwatch, plex);
463 if(pidfile != NULL) {
464 if((pidout = fopen(pidfile, "w")) == NULL) {
465 flog(LOG_ERR, "could not open %s for writing: %s", pidfile, strerror(errno));
473 flog(LOG_ERR, "could not chroot to %s: %s", root, strerror(errno));
478 if(setgid(pwent->pw_gid)) {
479 flog(LOG_ERR, "could not switch group to %i: %s", (int)pwent->pw_gid, strerror(errno));
482 if(setuid(pwent->pw_uid)) {
483 flog(LOG_ERR, "could not switch user to %i: %s", (int)pwent->pw_uid, strerror(errno));
487 signal(SIGPIPE, SIG_IGN);
492 fprintf(pidout, "%i\n", getpid());