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>
41 static struct hthead *parsereq(FILE *in)
44 struct charbuf method, url, ver;
55 } else if((c == EOF) || (c < 32) || (c >= 128)) {
65 } else if((c == EOF) || (c < 32)) {
76 } else if((c == EOF) || (c < 32) || (c >= 128)) {
85 req = mkreq(method.b, url.b, ver.b);
86 if(parseheaders(req, in))
102 static struct hthead *parseresp(FILE *in)
106 struct charbuf ver, msg;
117 } else if((c == EOF) || (c < 32) || (c >= 128)) {
127 } else if((c == EOF) || (c < '0') || (c > '9')) {
130 code = (code * 10) + (c - '0');
138 } else if((c == EOF) || (c < 32)) {
146 req = mkresp(code, msg.b, ver.b);
147 if(parseheaders(req, in))
162 static off_t passdata(FILE *in, FILE *out, off_t max)
169 while(!feof(in) && ((max < 0) || (total < max))) {
172 read = min(max - total, read);
173 read = fread(buf, 1, read, in);
176 if(fwrite(buf, 1, read, out) != read)
183 static int passchunks(FILE *in, FILE *out)
189 read = fread(buf, 1, sizeof(buf), in);
192 fprintf(out, "%zx\r\n", read);
193 if(fwrite(buf, 1, read, out) != read)
195 fprintf(out, "\r\n");
200 static int hasheader(struct hthead *head, char *name, char *val)
204 if((hd = getheader(head, name)) == NULL)
206 return(!strcasecmp(hd, val));
209 void serve(FILE *in, struct conn *conn)
213 struct hthead *req, *resp;
220 if((req = parsereq(in)) == NULL)
222 replrest(req, req->url);
223 if(req->rest[0] == '/')
224 replrest(req, req->rest + 1);
225 if((p = strchr(req->rest, '?')) != NULL)
228 if((conn->initreq != NULL) && conn->initreq(conn, req))
231 if(block(plex, EV_WRITE, 60) <= 0)
233 if(socketpair(PF_UNIX, SOCK_STREAM, 0, pfds))
235 if(sendreq(plex, req, pfds[0]))
238 out = mtstdopen(pfds[1], 1, 600, "r+");
240 if((hd = getheader(req, "content-length")) != NULL) {
243 if(passdata(in, out, dlen) != dlen)
249 /* Make sure to send EOF */
250 shutdown(pfds[1], SHUT_WR);
252 if((resp = parseresp(out)) == NULL)
254 replstr(&resp->ver, req->ver);
256 if(!strcmp(req->ver, "HTTP/1.0")) {
259 if((hd = getheader(resp, "content-length")) != NULL) {
260 dlen = passdata(out, in, -1);
263 if(!hasheader(req, "connection", "keep-alive"))
266 passdata(out, in, -1);
269 if(hasheader(req, "connection", "close") || hasheader(resp, "connection", "close"))
271 } else if(!strcmp(req->ver, "HTTP/1.1")) {
272 if((hd = getheader(resp, "content-length")) != NULL) {
275 dlen = passdata(out, in, -1);
278 } else if(!getheader(resp, "transfer-encoding")) {
279 headappheader(resp, "Transfer-Encoding", "chunked");
282 if(passchunks(out, in))
287 passdata(out, in, -1);
290 if(hasheader(req, "connection", "close") || hasheader(resp, "connection", "close"))
312 static void plexwatch(struct muth *muth, va_list args)
319 block(fd, EV_READ, 0);
320 buf = smalloc(65536);
321 ret = recv(fd, buf, 65536, 0);
323 flog(LOG_WARNING, "received error on rootplex read channel: %s", strerror(errno));
325 } else if(ret == 0) {
328 /* Maybe I'd like to implement some protocol in this direction
334 static void usage(FILE *out)
336 fprintf(out, "usage: htparser [-hSf] [-u USER] [-r ROOT] PORTSPEC... -- ROOT [ARGS...]\n");
337 fprintf(out, "\twhere PORTSPEC is HANDLER[:PAR[=VAL][(,PAR[=VAL])...]] (try HANDLER:help)\n");
338 fprintf(out, "\tavailable handlers are `plain'.\n");
341 static void addport(char *spec)
343 char *nm, *p, *p2, *n;
344 struct charvbuf pars, vals;
348 if((p = strchr(spec, ':')) == NULL) {
354 if((n = strchr(p, ',')) != NULL)
356 if((p2 = strchr(p, '=')) != NULL)
367 } while((p = n) != NULL);
370 /* XXX: It would be nice to decentralize this, but, meh... */
371 if(!strcmp(nm, "plain")) {
372 handleplain(pars.d, pars.b, vals.b);
374 flog(LOG_ERR, "htparser: unknown port handler `%s'", nm);
382 int main(int argc, char **argv)
386 int daemonize, logsys;
388 struct passwd *pwent;
390 daemonize = logsys = 0;
393 while((c = getopt(argc, argv, "+hSfu:r:")) >= 0) {
405 if((pwent = getpwnam(optarg)) == NULL) {
406 flog(LOG_ERR, "could not find user %s", optarg);
418 if((argc - optind) < 3) {
423 for(i = optind; i < argc; i++) {
424 if(!strcmp(argv[i], "--"))
429 if(!s1 || (i == argc)) {
433 if((plex = stdmkchild(argv + ++i)) < 0) {
434 flog(LOG_ERR, "could not spawn root multiplexer: %s", strerror(errno));
437 mustart(plexwatch, plex);
442 flog(LOG_ERR, "could not chroot to %s: %s", root, strerror(errno));
447 if(setgid(pwent->pw_gid)) {
448 flog(LOG_ERR, "could not switch group to %i: %s", (int)pwent->pw_gid, strerror(errno));
451 if(setuid(pwent->pw_uid)) {
452 flog(LOG_ERR, "could not switch user to %i: %s", (int)pwent->pw_uid, strerror(errno));