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>
40 static char *pidfile = NULL;
42 static void trimx(struct hthead *req)
47 while(i < req->noheaders) {
48 if(!strncasecmp(req->headers[i][0], "x-ash-", 6)) {
49 free(req->headers[i][0]);
50 free(req->headers[i][1]);
51 free(req->headers[i]);
52 memmove(req->headers + i, req->headers + i + 1, sizeof(*req->headers) * (--req->noheaders - i));
59 static struct hthead *parsereq(FILE *in)
62 struct charbuf method, url, ver;
73 } else if((c == EOF) || (c < 32) || (c >= 128)) {
83 } else if((c == EOF) || (c < 32)) {
94 } else if((c == EOF) || (c < 32) || (c >= 128)) {
103 req = mkreq(method.b, url.b, ver.b);
104 if(parseheaders(req, in))
121 static struct hthead *parseresp(FILE *in)
125 struct charbuf ver, msg;
136 } else if((c == EOF) || (c < 32) || (c >= 128)) {
146 } else if((c == EOF) || (c < '0') || (c > '9')) {
149 code = (code * 10) + (c - '0');
157 } else if((c == EOF) || (c < 32)) {
165 req = mkresp(code, msg.b, ver.b);
166 if(parseheaders(req, in))
181 static off_t passdata(FILE *in, FILE *out, off_t max)
188 while(!feof(in) && ((max < 0) || (total < max))) {
191 read = min(max - total, read);
192 read = fread(buf, 1, read, in);
195 if(fwrite(buf, 1, read, out) != read)
202 static int passchunks(FILE *in, FILE *out)
208 read = fread(buf, 1, sizeof(buf), in);
211 fprintf(out, "%zx\r\n", read);
212 if(fwrite(buf, 1, read, out) != read)
214 fprintf(out, "\r\n");
219 static int hasheader(struct hthead *head, char *name, char *val)
223 if((hd = getheader(head, name)) == NULL)
225 return(!strcasecmp(hd, val));
228 void serve(FILE *in, struct conn *conn)
232 struct hthead *req, *resp;
239 if((req = parsereq(in)) == NULL)
241 replrest(req, req->url);
242 if(req->rest[0] == '/')
243 replrest(req, req->rest + 1);
244 if((p = strchr(req->rest, '?')) != NULL)
247 if((conn->initreq != NULL) && conn->initreq(conn, req))
250 if(block(plex, EV_WRITE, 60) <= 0)
252 if(socketpair(PF_UNIX, SOCK_STREAM, 0, pfds))
254 if(sendreq(plex, req, pfds[0]))
257 out = mtstdopen(pfds[1], 1, 600, "r+");
259 if((hd = getheader(req, "content-length")) != NULL) {
262 if(passdata(in, out, dlen) != dlen)
268 /* Make sure to send EOF */
269 shutdown(pfds[1], SHUT_WR);
271 if((resp = parseresp(out)) == NULL)
273 replstr(&resp->ver, req->ver);
275 if(!strcmp(req->ver, "HTTP/1.0")) {
278 if((hd = getheader(resp, "content-length")) != NULL) {
279 dlen = passdata(out, in, -1);
282 if(!hasheader(req, "connection", "keep-alive"))
285 passdata(out, in, -1);
288 if(hasheader(req, "connection", "close") || hasheader(resp, "connection", "close"))
290 } else if(!strcmp(req->ver, "HTTP/1.1")) {
291 if((hd = getheader(resp, "content-length")) != NULL) {
294 dlen = passdata(out, in, -1);
297 } else if(!getheader(resp, "transfer-encoding")) {
298 headappheader(resp, "Transfer-Encoding", "chunked");
301 if(passchunks(out, in))
306 passdata(out, in, -1);
309 if(hasheader(req, "connection", "close") || hasheader(resp, "connection", "close"))
331 static void plexwatch(struct muth *muth, va_list args)
338 block(fd, EV_READ, 0);
339 buf = smalloc(65536);
340 ret = recv(fd, buf, 65536, 0);
342 flog(LOG_WARNING, "received error on rootplex read channel: %s", strerror(errno));
344 } else if(ret == 0) {
347 /* Maybe I'd like to implement some protocol in this direction
353 static void usage(FILE *out)
355 fprintf(out, "usage: htparser [-hSf] [-u USER] [-r ROOT] [-p PIDFILE] PORTSPEC... -- ROOT [ARGS...]\n");
356 fprintf(out, "\twhere PORTSPEC is HANDLER[:PAR[=VAL][(,PAR[=VAL])...]] (try HANDLER:help)\n");
357 fprintf(out, "\tavailable handlers are `plain' and `ssl'.\n");
360 static void addport(char *spec)
362 char *nm, *p, *p2, *n;
363 struct charvbuf pars, vals;
367 if((p = strchr(spec, ':')) == NULL) {
373 if((n = strchr(p, ',')) != NULL)
375 if((p2 = strchr(p, '=')) != NULL)
386 } while((p = n) != NULL);
389 /* XXX: It would be nice to decentralize this, but, meh... */
390 if(!strcmp(nm, "plain")) {
391 handleplain(pars.d, pars.b, vals.b);
393 } else if(!strcmp(nm, "ssl")) {
394 handlegnussl(pars.d, pars.b, vals.b);
397 flog(LOG_ERR, "htparser: unknown port handler `%s'", nm);
405 int main(int argc, char **argv)
409 int daemonize, logsys;
412 struct passwd *pwent;
414 daemonize = logsys = 0;
417 while((c = getopt(argc, argv, "+hSfu:r:p:")) >= 0) {
429 if((pwent = getpwnam(optarg)) == NULL) {
430 flog(LOG_ERR, "could not find user %s", optarg);
446 for(i = optind; i < argc; i++) {
447 if(!strcmp(argv[i], "--"))
452 if(!s1 || (i == argc)) {
456 if((plex = stdmkchild(argv + ++i)) < 0) {
457 flog(LOG_ERR, "could not spawn root multiplexer: %s", strerror(errno));
460 mustart(plexwatch, plex);
462 if(pidfile != NULL) {
463 if((pidout = fopen(pidfile, "w")) == NULL) {
464 flog(LOG_ERR, "could not open %s for writing: %s", pidfile, strerror(errno));
472 flog(LOG_ERR, "could not chroot to %s: %s", root, strerror(errno));
477 if(setgid(pwent->pw_gid)) {
478 flog(LOG_ERR, "could not switch group to %i: %s", (int)pwent->pw_gid, strerror(errno));
481 if(setuid(pwent->pw_uid)) {
482 flog(LOG_ERR, "could not switch user to %i: %s", (int)pwent->pw_uid, strerror(errno));
490 fprintf(pidout, "%i\n", getpid());