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>
24 #include <netinet/in.h>
25 #include <arpa/inet.h>
42 static struct hthead *parsereq(FILE *in)
45 struct charbuf method, url, ver;
56 } else if((c == EOF) || (c < 32) || (c >= 128)) {
66 } else if((c == EOF) || (c < 32)) {
77 } else if((c == EOF) || (c < 32) || (c >= 128)) {
86 req = mkreq(method.b, url.b, ver.b);
87 if(parseheaders(req, in))
103 static struct hthead *parseresp(FILE *in)
107 struct charbuf ver, msg;
118 } else if((c == EOF) || (c < 32) || (c >= 128)) {
128 } else if((c == EOF) || (c < '0') || (c > '9')) {
131 code = (code * 10) + (c - '0');
139 } else if((c == EOF) || (c < 32)) {
147 req = mkresp(code, msg.b, ver.b);
148 if(parseheaders(req, in))
163 static off_t passdata(FILE *in, FILE *out, off_t max)
170 while(!feof(in) && ((max < 0) || (total < max))) {
173 read = min(max - total, read);
174 read = fread(buf, 1, read, in);
177 if(fwrite(buf, 1, read, out) != read)
184 static int passchunks(FILE *in, FILE *out)
190 read = fread(buf, 1, sizeof(buf), in);
193 fprintf(out, "%zx\r\n", read);
194 if(fwrite(buf, 1, read, out) != read)
196 fprintf(out, "\r\n");
201 static int hasheader(struct hthead *head, char *name, char *val)
205 if((hd = getheader(head, name)) == NULL)
207 return(!strcasecmp(hd, val));
210 void serve(FILE *in, struct conn *conn)
214 struct hthead *req, *resp;
221 if((req = parsereq(in)) == NULL)
223 replrest(req, req->url);
224 if(req->rest[0] == '/')
225 replrest(req, req->rest + 1);
226 if((p = strchr(req->rest, '?')) != NULL)
229 if((conn->initreq != NULL) && conn->initreq(conn, req))
232 if(block(plex, EV_WRITE, 60) <= 0)
234 if(socketpair(PF_UNIX, SOCK_STREAM, 0, pfds))
236 if(sendreq(plex, req, pfds[0]))
239 out = mtstdopen(pfds[1], 1, 600, "r+");
241 if((hd = getheader(req, "content-length")) != NULL) {
244 if(passdata(in, out, dlen) != dlen)
250 /* Make sure to send EOF */
251 shutdown(pfds[1], SHUT_WR);
253 if((resp = parseresp(out)) == NULL)
255 replstr(&resp->ver, req->ver);
257 if(!strcmp(req->ver, "HTTP/1.0")) {
260 if((hd = getheader(resp, "content-length")) != NULL) {
261 dlen = passdata(out, in, -1);
264 if(!hasheader(req, "connection", "keep-alive"))
267 passdata(out, in, -1);
270 if(hasheader(req, "connection", "close") || hasheader(resp, "connection", "close"))
272 } else if(!strcmp(req->ver, "HTTP/1.1")) {
273 if((hd = getheader(resp, "content-length")) != NULL) {
276 dlen = passdata(out, in, -1);
279 } else if(!getheader(resp, "transfer-encoding")) {
280 headappheader(resp, "Transfer-Encoding", "chunked");
283 if(passchunks(out, in))
288 passdata(out, in, -1);
291 if(hasheader(req, "connection", "close") || hasheader(resp, "connection", "close"))
313 static void plexwatch(struct muth *muth, va_list args)
320 block(fd, EV_READ, 0);
321 buf = smalloc(65536);
322 ret = recv(fd, buf, 65536, 0);
324 flog(LOG_WARNING, "received error on rootplex read channel: %s", strerror(errno));
326 } else if(ret == 0) {
329 /* Maybe I'd like to implement some protocol in this direction
335 static void usage(FILE *out)
337 fprintf(out, "usage: htparser [-h] PORTSPEC... -- ROOT [ARGS...]\n");
338 fprintf(out, "\twhere PORTSPEC is HANDLER[:PAR[=VAL][(,PAR[=VAL])...]] (try HANDLER:help)\n");
339 fprintf(out, "\tavailable handlers are `plain'.\n");
342 static void addport(char *spec)
344 char *nm, *p, *p2, *n;
345 struct charvbuf pars, vals;
349 if((p = strchr(spec, ':')) == NULL) {
355 if((n = strchr(p, ',')) != NULL)
357 if((p2 = strchr(p, '=')) != NULL)
368 } while((p = n) != NULL);
371 /* XXX: It would be nice to decentralize this, but, meh... */
372 if(!strcmp(nm, "plain")) {
373 handleplain(pars.d, pars.b, vals.b);
375 flog(LOG_ERR, "htparser: unknown port handler `%s'", nm);
383 int main(int argc, char **argv)
388 while((c = getopt(argc, argv, "+h")) >= 0) {
398 if((argc - optind) < 3) {
403 for(i = optind; i < argc; i++) {
404 if(!strcmp(argv[i], "--"))
409 if(!s1 || (i == argc)) {
413 if((plex = stdmkchild(argv + ++i)) < 0) {
414 flog(LOG_ERR, "could not spawn root multiplexer: %s", strerror(errno));
417 mustart(plexwatch, plex);