doc: Fixed typo in dirplex manpage.
[ashd.git] / src / htparser.c
... / ...
CommitLineData
1/*
2 ashd - A Sane HTTP Daemon
3 Copyright (C) 2008 Fredrik Tolf <fredrik@dolda2000.com>
4
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.
9
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.
14
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/>.
17*/
18
19#include <stdlib.h>
20#include <unistd.h>
21#include <stdio.h>
22#include <string.h>
23#include <sys/socket.h>
24#include <pwd.h>
25#include <sys/signal.h>
26#include <errno.h>
27
28#ifdef HAVE_CONFIG_H
29#include <config.h>
30#endif
31#include <utils.h>
32#include <mt.h>
33#include <mtio.h>
34#include <log.h>
35#include <req.h>
36#include <proc.h>
37
38#include "htparser.h"
39
40static int plex;
41static char *pidfile = NULL;
42
43static void trimx(struct hthead *req)
44{
45 int i;
46
47 i = 0;
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));
54 } else {
55 i++;
56 }
57 }
58}
59
60static struct hthead *parsereq(FILE *in)
61{
62 struct hthead *req;
63 struct charbuf method, url, ver;
64 int c;
65
66 req = NULL;
67 bufinit(method);
68 bufinit(url);
69 bufinit(ver);
70 while(1) {
71 c = getc(in);
72 if(c == ' ') {
73 break;
74 } else if((c == EOF) || (c < 32) || (c >= 128)) {
75 goto fail;
76 } else {
77 bufadd(method, c);
78 }
79 }
80 while(1) {
81 c = getc(in);
82 if(c == ' ') {
83 break;
84 } else if((c == EOF) || (c < 32)) {
85 goto fail;
86 } else {
87 bufadd(url, c);
88 }
89 }
90 while(1) {
91 c = getc(in);
92 if(c == 10) {
93 break;
94 } else if(c == 13) {
95 } else if((c == EOF) || (c < 32) || (c >= 128)) {
96 goto fail;
97 } else {
98 bufadd(ver, c);
99 }
100 }
101 bufadd(method, 0);
102 bufadd(url, 0);
103 bufadd(ver, 0);
104 req = mkreq(method.b, url.b, ver.b);
105 if(parseheaders(req, in))
106 goto fail;
107 trimx(req);
108 goto out;
109
110fail:
111 if(req != NULL) {
112 freehthead(req);
113 req = NULL;
114 }
115out:
116 buffree(method);
117 buffree(url);
118 buffree(ver);
119 return(req);
120}
121
122static struct hthead *parseresp(FILE *in)
123{
124 struct hthead *req;
125 int code;
126 struct charbuf ver, msg;
127 int c;
128
129 req = NULL;
130 bufinit(ver);
131 bufinit(msg);
132 code = 0;
133 while(1) {
134 c = getc(in);
135 if(c == ' ') {
136 break;
137 } else if((c == EOF) || (c < 32) || (c >= 128)) {
138 goto fail;
139 } else {
140 bufadd(ver, c);
141 }
142 }
143 while(1) {
144 c = getc(in);
145 if(c == ' ') {
146 break;
147 } else if((c == EOF) || (c < '0') || (c > '9')) {
148 goto fail;
149 } else {
150 code = (code * 10) + (c - '0');
151 }
152 }
153 while(1) {
154 c = getc(in);
155 if(c == 10) {
156 break;
157 } else if(c == 13) {
158 } else if((c == EOF) || (c < 32)) {
159 goto fail;
160 } else {
161 bufadd(msg, c);
162 }
163 }
164 bufadd(msg, 0);
165 bufadd(ver, 0);
166 req = mkresp(code, msg.b, ver.b);
167 if(parseheaders(req, in))
168 goto fail;
169 goto out;
170
171fail:
172 if(req != NULL) {
173 freehthead(req);
174 req = NULL;
175 }
176out:
177 buffree(msg);
178 buffree(ver);
179 return(req);
180}
181
182static off_t passdata(FILE *in, FILE *out, off_t max)
183{
184 size_t read;
185 off_t total;
186 char buf[8192];
187
188 total = 0;
189 while(!feof(in) && ((max < 0) || (total < max))) {
190 read = sizeof(buf);
191 if(max >= 0)
192 read = min(max - total, read);
193 read = fread(buf, 1, read, in);
194 if(ferror(in))
195 return(-1);
196 if(fwrite(buf, 1, read, out) != read)
197 return(-1);
198 total += read;
199 }
200 return(total);
201}
202
203static int passchunks(FILE *in, FILE *out)
204{
205 char buf[8192];
206 size_t read;
207
208 do {
209 read = fread(buf, 1, sizeof(buf), in);
210 if(ferror(in))
211 return(-1);
212 fprintf(out, "%zx\r\n", read);
213 if(fwrite(buf, 1, read, out) != read)
214 return(-1);
215 fprintf(out, "\r\n");
216 } while(read > 0);
217 return(0);
218}
219
220static int hasheader(struct hthead *head, char *name, char *val)
221{
222 char *hd;
223
224 if((hd = getheader(head, name)) == NULL)
225 return(0);
226 return(!strcasecmp(hd, val));
227}
228
229void serve(FILE *in, struct conn *conn)
230{
231 int pfds[2];
232 FILE *out;
233 struct hthead *req, *resp;
234 char *hd, *p;
235 off_t dlen;
236
237 out = NULL;
238 req = resp = NULL;
239 while(1) {
240 if((req = parsereq(in)) == NULL)
241 break;
242 replrest(req, req->url);
243 if(req->rest[0] == '/')
244 replrest(req, req->rest + 1);
245 if((p = strchr(req->rest, '?')) != NULL)
246 *p = 0;
247
248 if((conn->initreq != NULL) && conn->initreq(conn, req))
249 break;
250
251 if(block(plex, EV_WRITE, 60) <= 0)
252 break;
253 if(socketpair(PF_UNIX, SOCK_STREAM, 0, pfds))
254 break;
255 if(sendreq(plex, req, pfds[0]))
256 break;
257 close(pfds[0]);
258 out = mtstdopen(pfds[1], 1, 600, "r+");
259
260 if((hd = getheader(req, "content-length")) != NULL) {
261 dlen = atoo(hd);
262 if(dlen > 0) {
263 if(passdata(in, out, dlen) != dlen)
264 break;
265 }
266 }
267 if(fflush(out))
268 break;
269 /* Make sure to send EOF */
270 shutdown(pfds[1], SHUT_WR);
271
272 if((resp = parseresp(out)) == NULL)
273 break;
274 replstr(&resp->ver, req->ver);
275
276 if(!strcmp(req->ver, "HTTP/1.0")) {
277 writeresp(in, resp);
278 fprintf(in, "\r\n");
279 if((hd = getheader(resp, "content-length")) != NULL) {
280 dlen = passdata(out, in, -1);
281 if(dlen != atoo(hd))
282 break;
283 if(!hasheader(req, "connection", "keep-alive"))
284 break;
285 } else {
286 passdata(out, in, -1);
287 break;
288 }
289 if(hasheader(req, "connection", "close") || hasheader(resp, "connection", "close"))
290 break;
291 } else if(!strcmp(req->ver, "HTTP/1.1")) {
292 if((hd = getheader(resp, "content-length")) != NULL) {
293 writeresp(in, resp);
294 fprintf(in, "\r\n");
295 dlen = passdata(out, in, -1);
296 if(dlen != atoo(hd))
297 break;
298 } else if(!getheader(resp, "transfer-encoding")) {
299 headappheader(resp, "Transfer-Encoding", "chunked");
300 writeresp(in, resp);
301 fprintf(in, "\r\n");
302 if(passchunks(out, in))
303 break;
304 } else {
305 writeresp(in, resp);
306 fprintf(in, "\r\n");
307 passdata(out, in, -1);
308 break;
309 }
310 if(hasheader(req, "connection", "close") || hasheader(resp, "connection", "close"))
311 break;
312 } else {
313 break;
314 }
315
316 fclose(out);
317 out = NULL;
318 freehthead(req);
319 freehthead(resp);
320 req = resp = NULL;
321 }
322
323 if(out != NULL)
324 fclose(out);
325 if(req != NULL)
326 freehthead(req);
327 if(resp != NULL)
328 freehthead(resp);
329 fclose(in);
330}
331
332static void plexwatch(struct muth *muth, va_list args)
333{
334 vavar(int, fd);
335 char *buf;
336 int ret;
337
338 while(1) {
339 block(fd, EV_READ, 0);
340 buf = smalloc(65536);
341 ret = recv(fd, buf, 65536, 0);
342 if(ret < 0) {
343 flog(LOG_WARNING, "received error on rootplex read channel: %s", strerror(errno));
344 exit(1);
345 } else if(ret == 0) {
346 exit(0);
347 }
348 /* Maybe I'd like to implement some protocol in this direction
349 * some day... */
350 free(buf);
351 }
352}
353
354static void usage(FILE *out)
355{
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");
359}
360
361static void addport(char *spec)
362{
363 char *nm, *p, *p2, *n;
364 struct charvbuf pars, vals;
365
366 bufinit(pars);
367 bufinit(vals);
368 if((p = strchr(spec, ':')) == NULL) {
369 nm = spec;
370 } else {
371 nm = spec;
372 *(p++) = 0;
373 do {
374 if((n = strchr(p, ',')) != NULL)
375 *(n++) = 0;
376 if((p2 = strchr(p, '=')) != NULL)
377 *(p2++) = 0;
378 if(!*p) {
379 usage(stderr);
380 exit(1);
381 }
382 bufadd(pars, p);
383 if(p2)
384 bufadd(vals, p2);
385 else
386 bufadd(vals, "");
387 } while((p = n) != NULL);
388 }
389
390 /* XXX: It would be nice to decentralize this, but, meh... */
391 if(!strcmp(nm, "plain")) {
392 handleplain(pars.d, pars.b, vals.b);
393#ifdef HAVE_GNUTLS
394 } else if(!strcmp(nm, "ssl")) {
395 handlegnussl(pars.d, pars.b, vals.b);
396#endif
397 } else {
398 flog(LOG_ERR, "htparser: unknown port handler `%s'", nm);
399 exit(1);
400 }
401
402 buffree(pars);
403 buffree(vals);
404}
405
406int main(int argc, char **argv)
407{
408 int c;
409 int i, s1;
410 int daemonize, logsys;
411 char *root;
412 FILE *pidout;
413 struct passwd *pwent;
414
415 daemonize = logsys = 0;
416 root = NULL;
417 pwent = NULL;
418 while((c = getopt(argc, argv, "+hSfu:r:p:")) >= 0) {
419 switch(c) {
420 case 'h':
421 usage(stdout);
422 exit(0);
423 case 'f':
424 daemonize = 1;
425 break;
426 case 'S':
427 logsys = 1;
428 break;
429 case 'u':
430 if((pwent = getpwnam(optarg)) == NULL) {
431 flog(LOG_ERR, "could not find user %s", optarg);
432 exit(1);
433 }
434 break;
435 case 'r':
436 root = optarg;
437 break;
438 case 'p':
439 pidfile = optarg;
440 break;
441 default:
442 usage(stderr);
443 exit(1);
444 }
445 }
446 s1 = 0;
447 for(i = optind; i < argc; i++) {
448 if(!strcmp(argv[i], "--"))
449 break;
450 s1 = 1;
451 addport(argv[i]);
452 }
453 if(!s1 || (i == argc)) {
454 usage(stderr);
455 exit(1);
456 }
457 if((plex = stdmkchild(argv + ++i, NULL, NULL)) < 0) {
458 flog(LOG_ERR, "could not spawn root multiplexer: %s", strerror(errno));
459 return(1);
460 }
461 mustart(plexwatch, plex);
462 pidout = NULL;
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));
466 return(1);
467 }
468 }
469 if(logsys)
470 opensyslog();
471 if(root) {
472 if(chroot(root)) {
473 flog(LOG_ERR, "could not chroot to %s: %s", root, strerror(errno));
474 exit(1);
475 }
476 }
477 if(pwent) {
478 if(setgid(pwent->pw_gid)) {
479 flog(LOG_ERR, "could not switch group to %i: %s", (int)pwent->pw_gid, strerror(errno));
480 exit(1);
481 }
482 if(setuid(pwent->pw_uid)) {
483 flog(LOG_ERR, "could not switch user to %i: %s", (int)pwent->pw_uid, strerror(errno));
484 exit(1);
485 }
486 }
487 signal(SIGPIPE, SIG_IGN);
488 if(daemonize) {
489 daemon(0, 0);
490 }
491 if(pidout != NULL) {
492 fprintf(pidout, "%i\n", getpid());
493 fclose(pidout);
494 }
495 ioloop();
496 return(0);
497}