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 void trimx(struct hthead *req)
46 while(i < req->noheaders) {
47 if(!strncasecmp(req->headers[i][0], "x-ash-", 6)) {
48 free(req->headers[i][0]);
49 free(req->headers[i][1]);
50 free(req->headers[i]);
51 memmove(req->headers + i, req->headers + i + 1, sizeof(*req->headers) * (--req->noheaders - i));
58 static struct hthead *parsereq(FILE *in)
61 struct charbuf method, url, ver;
72 } else if((c == EOF) || (c < 32) || (c >= 128)) {
82 } else if((c == EOF) || (c < 32)) {
93 } else if((c == EOF) || (c < 32) || (c >= 128)) {
102 req = mkreq(method.b, url.b, ver.b);
103 if(parseheaders(req, in))
120 static struct hthead *parseresp(FILE *in)
124 struct charbuf ver, msg;
135 } else if((c == EOF) || (c < 32) || (c >= 128)) {
145 } else if((c == EOF) || (c < '0') || (c > '9')) {
148 code = (code * 10) + (c - '0');
156 } else if((c == EOF) || (c < 32)) {
164 req = mkresp(code, msg.b, ver.b);
165 if(parseheaders(req, in))
180 static off_t passdata(FILE *in, FILE *out, off_t max)
187 while(!feof(in) && ((max < 0) || (total < max))) {
190 read = min(max - total, read);
191 read = fread(buf, 1, read, in);
194 if(fwrite(buf, 1, read, out) != read)
201 static int passchunks(FILE *in, FILE *out)
207 read = fread(buf, 1, sizeof(buf), in);
210 fprintf(out, "%zx\r\n", read);
211 if(fwrite(buf, 1, read, out) != read)
213 fprintf(out, "\r\n");
218 static int hasheader(struct hthead *head, char *name, char *val)
222 if((hd = getheader(head, name)) == NULL)
224 return(!strcasecmp(hd, val));
227 void serve(FILE *in, struct conn *conn)
231 struct hthead *req, *resp;
238 if((req = parsereq(in)) == NULL)
240 replrest(req, req->url);
241 if(req->rest[0] == '/')
242 replrest(req, req->rest + 1);
243 if((p = strchr(req->rest, '?')) != NULL)
246 if((conn->initreq != NULL) && conn->initreq(conn, req))
249 if(block(plex, EV_WRITE, 60) <= 0)
251 if(socketpair(PF_UNIX, SOCK_STREAM, 0, pfds))
253 if(sendreq(plex, req, pfds[0]))
256 out = mtstdopen(pfds[1], 1, 600, "r+");
258 if((hd = getheader(req, "content-length")) != NULL) {
261 if(passdata(in, out, dlen) != dlen)
267 /* Make sure to send EOF */
268 shutdown(pfds[1], SHUT_WR);
270 if((resp = parseresp(out)) == NULL)
272 replstr(&resp->ver, req->ver);
274 if(!strcmp(req->ver, "HTTP/1.0")) {
277 if((hd = getheader(resp, "content-length")) != NULL) {
278 dlen = passdata(out, in, -1);
281 if(!hasheader(req, "connection", "keep-alive"))
284 passdata(out, in, -1);
287 if(hasheader(req, "connection", "close") || hasheader(resp, "connection", "close"))
289 } else if(!strcmp(req->ver, "HTTP/1.1")) {
290 if((hd = getheader(resp, "content-length")) != NULL) {
293 dlen = passdata(out, in, -1);
296 } else if(!getheader(resp, "transfer-encoding")) {
297 headappheader(resp, "Transfer-Encoding", "chunked");
300 if(passchunks(out, in))
305 passdata(out, in, -1);
308 if(hasheader(req, "connection", "close") || hasheader(resp, "connection", "close"))
330 static void plexwatch(struct muth *muth, va_list args)
337 block(fd, EV_READ, 0);
338 buf = smalloc(65536);
339 ret = recv(fd, buf, 65536, 0);
341 flog(LOG_WARNING, "received error on rootplex read channel: %s", strerror(errno));
343 } else if(ret == 0) {
346 /* Maybe I'd like to implement some protocol in this direction
352 static void usage(FILE *out)
354 fprintf(out, "usage: htparser [-hSf] [-u USER] [-r ROOT] PORTSPEC... -- ROOT [ARGS...]\n");
355 fprintf(out, "\twhere PORTSPEC is HANDLER[:PAR[=VAL][(,PAR[=VAL])...]] (try HANDLER:help)\n");
356 fprintf(out, "\tavailable handlers are `plain' and `ssl'.\n");
359 static void addport(char *spec)
361 char *nm, *p, *p2, *n;
362 struct charvbuf pars, vals;
366 if((p = strchr(spec, ':')) == NULL) {
372 if((n = strchr(p, ',')) != NULL)
374 if((p2 = strchr(p, '=')) != NULL)
385 } while((p = n) != NULL);
388 /* XXX: It would be nice to decentralize this, but, meh... */
389 if(!strcmp(nm, "plain")) {
390 handleplain(pars.d, pars.b, vals.b);
392 } else if(!strcmp(nm, "ssl")) {
393 handlegnussl(pars.d, pars.b, vals.b);
396 flog(LOG_ERR, "htparser: unknown port handler `%s'", nm);
404 int main(int argc, char **argv)
408 int daemonize, logsys;
410 struct passwd *pwent;
412 daemonize = logsys = 0;
415 while((c = getopt(argc, argv, "+hSfu:r:")) >= 0) {
427 if((pwent = getpwnam(optarg)) == NULL) {
428 flog(LOG_ERR, "could not find user %s", optarg);
441 for(i = optind; i < argc; i++) {
442 if(!strcmp(argv[i], "--"))
447 if(!s1 || (i == argc)) {
451 if((plex = stdmkchild(argv + ++i)) < 0) {
452 flog(LOG_ERR, "could not spawn root multiplexer: %s", strerror(errno));
455 mustart(plexwatch, plex);
460 flog(LOG_ERR, "could not chroot to %s: %s", root, strerror(errno));
465 if(setgid(pwent->pw_gid)) {
466 flog(LOG_ERR, "could not switch group to %i: %s", (int)pwent->pw_gid, strerror(errno));
469 if(setuid(pwent->pw_uid)) {
470 flog(LOG_ERR, "could not switch user to %i: %s", (int)pwent->pw_uid, strerror(errno));