htparser: Handle HEAD requests properly.
[ashd.git] / src / htparser.c
CommitLineData
f0bbedf7
FT
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>
f4cdf919 22#include <string.h>
f4cdf919 23#include <sys/socket.h>
f0cbd8d7 24#include <pwd.h>
15fa3fe8 25#include <sys/signal.h>
f4cdf919 26#include <errno.h>
f0bbedf7
FT
27
28#ifdef HAVE_CONFIG_H
29#include <config.h>
30#endif
31#include <utils.h>
f4cdf919 32#include <mt.h>
83723896 33#include <mtio.h>
f4cdf919 34#include <log.h>
66987955 35#include <req.h>
9d87a119 36#include <proc.h>
f4cdf919 37
8774c31b 38#include "htparser.h"
f4cdf919 39
8774c31b 40static int plex;
43c58ba2 41static char *pidfile = NULL;
f4cdf919 42
df431d1d
FT
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
5fc1bf9f 60static struct hthead *parsereq(FILE *in)
66987955 61{
5fc1bf9f
FT
62 struct hthead *req;
63 struct charbuf method, url, ver;
64 int c;
66987955 65
5fc1bf9f
FT
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);
66987955 78 }
c9955b14 79 }
c9955b14 80 while(1) {
5fc1bf9f
FT
81 c = getc(in);
82 if(c == ' ') {
c9955b14 83 break;
5fc1bf9f
FT
84 } else if((c == EOF) || (c < 32)) {
85 goto fail;
86 } else {
87 bufadd(url, c);
66987955
FT
88 }
89 }
66987955 90 while(1) {
5fc1bf9f
FT
91 c = getc(in);
92 if(c == 10) {
66987955 93 break;
5fc1bf9f
FT
94 } else if(c == 13) {
95 } else if((c == EOF) || (c < 32) || (c >= 128)) {
c9955b14 96 goto fail;
5fc1bf9f
FT
97 } else {
98 bufadd(ver, c);
99 }
66987955 100 }
5fc1bf9f
FT
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;
df431d1d 107 trimx(req);
5fc1bf9f 108 goto out;
c9955b14
FT
109
110fail:
5fc1bf9f
FT
111 if(req != NULL) {
112 freehthead(req);
113 req = NULL;
114 }
115out:
116 buffree(method);
117 buffree(url);
118 buffree(ver);
119 return(req);
66987955
FT
120}
121
5fc1bf9f 122static struct hthead *parseresp(FILE *in)
9d87a119 123{
5fc1bf9f 124 struct hthead *req;
9d87a119 125 int code;
5fc1bf9f
FT
126 struct charbuf ver, msg;
127 int c;
9d87a119 128
5fc1bf9f
FT
129 req = NULL;
130 bufinit(ver);
131 bufinit(msg);
132 code = 0;
9d87a119 133 while(1) {
5fc1bf9f
FT
134 c = getc(in);
135 if(c == ' ') {
9d87a119 136 break;
5fc1bf9f 137 } else if((c == EOF) || (c < 32) || (c >= 128)) {
9d87a119 138 goto fail;
5fc1bf9f
FT
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')) {
9d87a119 148 goto fail;
5fc1bf9f
FT
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)) {
9d87a119 159 goto fail;
5fc1bf9f
FT
160 } else {
161 bufadd(msg, c);
162 }
9d87a119 163 }
5fc1bf9f
FT
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;
9d87a119
FT
170
171fail:
5fc1bf9f
FT
172 if(req != NULL) {
173 freehthead(req);
174 req = NULL;
175 }
176out:
177 buffree(msg);
178 buffree(ver);
179 return(req);
9d87a119
FT
180}
181
5fc1bf9f 182static off_t passdata(FILE *in, FILE *out, off_t max)
9d87a119 183{
5fc1bf9f
FT
184 size_t read;
185 off_t total;
186 char buf[8192];
187
188 total = 0;
f9255ddd 189 while(!feof(in) && ((max < 0) || (total < max))) {
5fc1bf9f
FT
190 read = sizeof(buf);
191 if(max >= 0)
a701d7b7 192 read = min(max - total, read);
5fc1bf9f
FT
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;
9d87a119 199 }
5fc1bf9f
FT
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);
f9255ddd 212 fprintf(out, "%zx\r\n", read);
5fc1bf9f
FT
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));
9d87a119
FT
227}
228
8774c31b 229void serve(FILE *in, struct conn *conn)
66987955 230{
af34331c 231 int pfds[2];
8774c31b 232 FILE *out;
9d87a119 233 struct hthead *req, *resp;
5fc1bf9f
FT
234 char *hd, *p;
235 off_t dlen;
66987955 236
5fc1bf9f 237 out = NULL;
3c296bd4 238 req = resp = NULL;
66987955 239 while(1) {
5fc1bf9f
FT
240 if((req = parsereq(in)) == NULL)
241 break;
242 replrest(req, req->url);
9e9eca79
FT
243 if(req->rest[0] == '/')
244 replrest(req, req->rest + 1);
edad3c6a
FT
245 if((p = strchr(req->rest, '?')) != NULL)
246 *p = 0;
9d87a119 247
8774c31b
FT
248 if((conn->initreq != NULL) && conn->initreq(conn, req))
249 break;
250
46c3d430 251 if(block(plex, EV_WRITE, 60) <= 0)
5fc1bf9f 252 break;
af34331c 253 if(socketpair(PF_UNIX, SOCK_STREAM, 0, pfds))
5fc1bf9f 254 break;
af34331c 255 if(sendreq(plex, req, pfds[0]))
5fc1bf9f 256 break;
af34331c 257 close(pfds[0]);
5fc1bf9f 258 out = mtstdopen(pfds[1], 1, 600, "r+");
a0327573 259
a0327573
FT
260 if((hd = getheader(req, "content-length")) != NULL) {
261 dlen = atoo(hd);
a06a2fbd 262 if(dlen > 0) {
5fc1bf9f
FT
263 if(passdata(in, out, dlen) != dlen)
264 break;
a06a2fbd 265 }
a0327573 266 }
5fc1bf9f
FT
267 if(fflush(out))
268 break;
d93d9a05 269 /* Make sure to send EOF */
5fc1bf9f 270 shutdown(pfds[1], SHUT_WR);
9d87a119 271
f9255ddd
FT
272 if((resp = parseresp(out)) == NULL)
273 break;
5fc1bf9f
FT
274 replstr(&resp->ver, req->ver);
275
276 if(!strcmp(req->ver, "HTTP/1.0")) {
277 writeresp(in, resp);
278 fprintf(in, "\r\n");
18fb436d
FT
279 if(!strcasecmp(req->method, "head")) {
280 if(!hasheader(req, "connection", "keep-alive"))
281 break;
282 } else if((hd = getheader(resp, "content-length")) != NULL) {
5fc1bf9f
FT
283 dlen = passdata(out, in, -1);
284 if(dlen != atoo(hd))
285 break;
286 if(!hasheader(req, "connection", "keep-alive"))
9d87a119 287 break;
5fc1bf9f
FT
288 } else {
289 passdata(out, in, -1);
290 break;
9d87a119 291 }
5fc1bf9f
FT
292 if(hasheader(req, "connection", "close") || hasheader(resp, "connection", "close"))
293 break;
294 } else if(!strcmp(req->ver, "HTTP/1.1")) {
18fb436d
FT
295 if(!strcasecmp(req->method, "head")) {
296 writeresp(in, resp);
297 fprintf(in, "\r\n");
298 } else if((hd = getheader(resp, "content-length")) != NULL) {
5fc1bf9f
FT
299 writeresp(in, resp);
300 fprintf(in, "\r\n");
301 dlen = passdata(out, in, -1);
302 if(dlen != atoo(hd))
303 break;
304 } else if(!getheader(resp, "transfer-encoding")) {
305 headappheader(resp, "Transfer-Encoding", "chunked");
306 writeresp(in, resp);
307 fprintf(in, "\r\n");
308 if(passchunks(out, in))
309 break;
310 } else {
311 writeresp(in, resp);
312 fprintf(in, "\r\n");
313 passdata(out, in, -1);
9d87a119 314 break;
5fc1bf9f
FT
315 }
316 if(hasheader(req, "connection", "close") || hasheader(resp, "connection", "close"))
9d87a119 317 break;
5fc1bf9f
FT
318 } else {
319 break;
9d87a119 320 }
5fc1bf9f
FT
321
322 fclose(out);
323 out = NULL;
9d87a119 324 freehthead(req);
9d87a119 325 freehthead(resp);
5fc1bf9f 326 req = resp = NULL;
66987955
FT
327 }
328
5fc1bf9f
FT
329 if(out != NULL)
330 fclose(out);
9d87a119
FT
331 if(req != NULL)
332 freehthead(req);
333 if(resp != NULL)
334 freehthead(resp);
5fc1bf9f 335 fclose(in);
66987955
FT
336}
337
32e24c19
FT
338static void plexwatch(struct muth *muth, va_list args)
339{
340 vavar(int, fd);
341 char *buf;
342 int ret;
343
344 while(1) {
345 block(fd, EV_READ, 0);
346 buf = smalloc(65536);
347 ret = recv(fd, buf, 65536, 0);
348 if(ret < 0) {
349 flog(LOG_WARNING, "received error on rootplex read channel: %s", strerror(errno));
350 exit(1);
351 } else if(ret == 0) {
352 exit(0);
353 }
354 /* Maybe I'd like to implement some protocol in this direction
355 * some day... */
356 free(buf);
357 }
358}
359
8774c31b
FT
360static void usage(FILE *out)
361{
43c58ba2 362 fprintf(out, "usage: htparser [-hSf] [-u USER] [-r ROOT] [-p PIDFILE] PORTSPEC... -- ROOT [ARGS...]\n");
8774c31b 363 fprintf(out, "\twhere PORTSPEC is HANDLER[:PAR[=VAL][(,PAR[=VAL])...]] (try HANDLER:help)\n");
6ca53b2e 364 fprintf(out, "\tavailable handlers are `plain' and `ssl'.\n");
8774c31b
FT
365}
366
367static void addport(char *spec)
368{
369 char *nm, *p, *p2, *n;
370 struct charvbuf pars, vals;
371
372 bufinit(pars);
373 bufinit(vals);
374 if((p = strchr(spec, ':')) == NULL) {
375 nm = spec;
376 } else {
377 nm = spec;
378 *(p++) = 0;
379 do {
380 if((n = strchr(p, ',')) != NULL)
381 *(n++) = 0;
382 if((p2 = strchr(p, '=')) != NULL)
383 *(p2++) = 0;
384 if(!*p) {
385 usage(stderr);
386 exit(1);
387 }
388 bufadd(pars, p);
389 if(p2)
390 bufadd(vals, p2);
391 else
392 bufadd(vals, "");
393 } while((p = n) != NULL);
394 }
395
396 /* XXX: It would be nice to decentralize this, but, meh... */
397 if(!strcmp(nm, "plain")) {
398 handleplain(pars.d, pars.b, vals.b);
6ca53b2e
FT
399#ifdef HAVE_GNUTLS
400 } else if(!strcmp(nm, "ssl")) {
401 handlegnussl(pars.d, pars.b, vals.b);
402#endif
8774c31b
FT
403 } else {
404 flog(LOG_ERR, "htparser: unknown port handler `%s'", nm);
405 exit(1);
406 }
407
408 buffree(pars);
409 buffree(vals);
410}
411
f0bbedf7
FT
412int main(int argc, char **argv)
413{
8774c31b
FT
414 int c;
415 int i, s1;
f0cbd8d7
FT
416 int daemonize, logsys;
417 char *root;
43c58ba2 418 FILE *pidout;
f0cbd8d7 419 struct passwd *pwent;
f4cdf919 420
f0cbd8d7
FT
421 daemonize = logsys = 0;
422 root = NULL;
423 pwent = NULL;
43c58ba2 424 while((c = getopt(argc, argv, "+hSfu:r:p:")) >= 0) {
8774c31b
FT
425 switch(c) {
426 case 'h':
427 usage(stdout);
428 exit(0);
f0cbd8d7
FT
429 case 'f':
430 daemonize = 1;
431 break;
432 case 'S':
433 logsys = 1;
434 break;
435 case 'u':
436 if((pwent = getpwnam(optarg)) == NULL) {
437 flog(LOG_ERR, "could not find user %s", optarg);
438 exit(1);
439 }
440 break;
441 case 'r':
442 root = optarg;
443 break;
43c58ba2
FT
444 case 'p':
445 pidfile = optarg;
446 break;
8774c31b
FT
447 default:
448 usage(stderr);
449 exit(1);
450 }
451 }
8774c31b
FT
452 s1 = 0;
453 for(i = optind; i < argc; i++) {
454 if(!strcmp(argv[i], "--"))
455 break;
456 s1 = 1;
457 addport(argv[i]);
9d87a119 458 }
8774c31b
FT
459 if(!s1 || (i == argc)) {
460 usage(stderr);
461 exit(1);
f4cdf919 462 }
6a7a868e 463 if((plex = stdmkchild(argv + ++i, NULL, NULL)) < 0) {
8774c31b
FT
464 flog(LOG_ERR, "could not spawn root multiplexer: %s", strerror(errno));
465 return(1);
f4cdf919 466 }
32e24c19 467 mustart(plexwatch, plex);
43c58ba2
FT
468 pidout = NULL;
469 if(pidfile != NULL) {
470 if((pidout = fopen(pidfile, "w")) == NULL) {
471 flog(LOG_ERR, "could not open %s for writing: %s", pidfile, strerror(errno));
472 return(1);
473 }
474 }
f0cbd8d7
FT
475 if(logsys)
476 opensyslog();
477 if(root) {
478 if(chroot(root)) {
479 flog(LOG_ERR, "could not chroot to %s: %s", root, strerror(errno));
480 exit(1);
481 }
482 }
483 if(pwent) {
484 if(setgid(pwent->pw_gid)) {
485 flog(LOG_ERR, "could not switch group to %i: %s", (int)pwent->pw_gid, strerror(errno));
486 exit(1);
487 }
488 if(setuid(pwent->pw_uid)) {
489 flog(LOG_ERR, "could not switch user to %i: %s", (int)pwent->pw_uid, strerror(errno));
490 exit(1);
491 }
492 }
15fa3fe8 493 signal(SIGPIPE, SIG_IGN);
f0cbd8d7
FT
494 if(daemonize) {
495 daemon(0, 0);
496 }
ee036f74 497 if(pidout != NULL) {
43c58ba2 498 fprintf(pidout, "%i\n", getpid());
ee036f74
FT
499 fclose(pidout);
500 }
f4cdf919
FT
501 ioloop();
502 return(0);
f0bbedf7 503}