Strip the leading slash of the rest string.
[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
FT
22#include <string.h>
23#include <sys/select.h>
24#include <sys/socket.h>
25#include <netinet/in.h>
9d87a119 26#include <arpa/inet.h>
f4cdf919 27#include <errno.h>
9d87a119 28#include <time.h>
f0bbedf7
FT
29
30#ifdef HAVE_CONFIG_H
31#include <config.h>
32#endif
33#include <utils.h>
f4cdf919
FT
34#include <mt.h>
35#include <log.h>
66987955 36#include <req.h>
9d87a119 37#include <proc.h>
f4cdf919
FT
38
39#define EV_READ 1
40#define EV_WRITE 2
41
42struct blocker {
43 struct blocker *n, *p;
44 int fd;
45 int ev;
9d87a119 46 time_t to;
f4cdf919
FT
47 struct muth *th;
48};
49
50static struct blocker *blockers;
9d87a119 51int plex;
f4cdf919 52
9d87a119 53static int block(int fd, int ev, time_t to)
f4cdf919
FT
54{
55 struct blocker *bl;
56 int rv;
57
58 omalloc(bl);
59 bl->fd = fd;
60 bl->ev = ev;
9d87a119
FT
61 if(to > 0)
62 bl->to = time(NULL) + to;
f4cdf919
FT
63 bl->th = current;
64 bl->n = blockers;
65 if(blockers)
66 blockers->p = bl;
67 blockers = bl;
68 rv = yield();
69 if(bl->n)
70 bl->n->p = bl->p;
71 if(bl->p)
72 bl->p->n = bl->n;
73 if(bl == blockers)
74 blockers = bl->n;
75 return(rv);
76}
77
78static int listensock4(int port)
79{
80 struct sockaddr_in name;
81 int fd;
82 int valbuf;
83
84 memset(&name, 0, sizeof(name));
85 name.sin_family = AF_INET;
86 name.sin_port = htons(port);
87 if((fd = socket(PF_INET, SOCK_STREAM, 0)) < 0)
88 return(-1);
89 valbuf = 1;
90 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &valbuf, sizeof(valbuf));
91 if(bind(fd, (struct sockaddr *)&name, sizeof(name))) {
92 close(fd);
93 return(-1);
94 }
95 if(listen(fd, 16) < 0) {
96 close(fd);
97 return(-1);
98 }
99 return(fd);
100}
101
102static int listensock6(int port)
103{
104 struct sockaddr_in6 name;
105 int fd;
106 int valbuf;
107
108 memset(&name, 0, sizeof(name));
109 name.sin6_family = AF_INET6;
110 name.sin6_port = htons(port);
111 if((fd = socket(PF_INET6, SOCK_STREAM, 0)) < 0)
112 return(-1);
113 valbuf = 1;
114 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &valbuf, sizeof(valbuf));
115 if(bind(fd, (struct sockaddr *)&name, sizeof(name))) {
116 close(fd);
117 return(-1);
118 }
119 if(listen(fd, 16) < 0) {
120 close(fd);
121 return(-1);
122 }
123 return(fd);
124}
125
c9955b14 126static size_t readhead(int fd, struct charbuf *buf)
66987955 127{
66987955 128 int nl;
c9955b14 129 size_t off;
66987955 130
c9955b14
FT
131 int get1(void)
132 {
133 int ret;
134
135 while(!(off < buf->d)) {
136 sizebuf(*buf, buf->d + 1024);
137 ret = recv(fd, buf->b + buf->d, buf->s - buf->d, MSG_DONTWAIT);
138 if(ret <= 0) {
139 if((ret < 0) && (errno == EAGAIN)) {
9d87a119
FT
140 if(block(fd, EV_READ, 60) <= 0)
141 return(-1);
c9955b14
FT
142 continue;
143 }
144 return(-1);
66987955 145 }
c9955b14 146 buf->d += ret;
66987955 147 }
c9955b14
FT
148 return(buf->b[off++]);
149 }
150
151 nl = 0;
152 off = 0;
153 while(1) {
154 switch(get1()) {
155 case '\n':
66987955 156 if(nl)
c9955b14 157 return(off);
66987955 158 nl = 1;
c9955b14
FT
159 break;
160 case '\r':
161 break;
162 case -1:
163 return(-1);
164 default:
66987955 165 nl = 0;
c9955b14 166 break;
66987955
FT
167 }
168 }
66987955
FT
169}
170
171#define SKIPNL(ptr) ({ \
172 int __buf__; \
173 if(*(ptr) == '\r') \
174 *((ptr)++) = 0; \
175 if(*(ptr) != '\n') { \
176 __buf__ = 0; \
177 } else { \
178 *((ptr)++) = 0; \
179 __buf__ = 1; \
180 } \
181 __buf__;})
9d87a119 182static struct hthead *parserawreq(char *buf)
66987955
FT
183{
184 char *p, *p2, *nl;
185 char *method, *url, *ver;
9d87a119 186 struct hthead *req;
66987955
FT
187
188 if((nl = strchr(buf, '\n')) == NULL)
189 return(NULL);
190 if(((p = strchr(buf, ' ')) == NULL) || (p > nl))
191 return(NULL);
192 method = buf;
193 *(p++) = 0;
194 if(((p2 = strchr(p, ' ')) == NULL) || (p2 > nl))
195 return(NULL);
196 url = p;
197 p = p2;
198 *(p++) = 0;
199 if(strncmp(p, "HTTP/", 5))
200 return(NULL);
201 ver = (p += 5);
202 for(; ((*p >= '0') && (*p <= '9')) || (*p == '.'); p++);
203 if(!SKIPNL(p))
204 return(NULL);
205
206 req = mkreq(method, url, ver);
207 while(1) {
208 if(SKIPNL(p)) {
209 if(*p)
c9955b14 210 goto fail;
66987955
FT
211 break;
212 }
213 if((nl = strchr(p, '\n')) == NULL)
c9955b14 214 goto fail;
66987955 215 if(((p2 = strchr(p, ':')) == NULL) || (p2 > nl))
c9955b14 216 goto fail;
66987955
FT
217 *(p2++) = 0;
218 for(; (*p2 == ' ') || (*p2 == '\t'); p2++);
219 for(nl = p2; (*nl != '\r') && (*nl != '\n'); nl++);
220 if(!SKIPNL(nl))
c9955b14 221 goto fail;
9d87a119
FT
222 if(strncasecmp(p, "x-ash-", 6))
223 headappheader(req, p, p2);
66987955
FT
224 p = nl;
225 }
226 return(req);
c9955b14
FT
227
228fail:
9d87a119 229 freehthead(req);
c9955b14 230 return(NULL);
66987955
FT
231}
232
9d87a119
FT
233static struct hthead *parserawresp(char *buf)
234{
235 char *p, *p2, *nl;
236 char *msg, *ver;
237 int code;
238 struct hthead *resp;
239
240 if((nl = strchr(buf, '\n')) == NULL)
241 return(NULL);
242 p = strchr(buf, '\r');
243 if((p != NULL) && (p < nl))
244 nl = p;
245 if(strncmp(buf, "HTTP/", 5))
246 return(NULL);
247 ver = p = buf + 5;
248 for(; ((*p >= '0') && (*p <= '9')) || (*p == '.'); p++);
249 if(*p != ' ')
250 return(NULL);
251 *(p++) = 0;
252 if(((p2 = strchr(p, ' ')) == NULL) || (p2 > nl))
253 return(NULL);
254 *(p2++) = 0;
255 code = atoi(p);
256 if((code < 100) || (code >= 600))
257 return(NULL);
258 if(p2 >= nl)
259 return(NULL);
260 msg = p2;
261 p = nl;
262 if(!SKIPNL(p))
263 return(NULL);
264
265 resp = mkresp(code, msg, ver);
266 while(1) {
267 if(SKIPNL(p)) {
268 if(*p)
269 goto fail;
270 break;
271 }
272 if((nl = strchr(p, '\n')) == NULL)
273 goto fail;
274 if(((p2 = strchr(p, ':')) == NULL) || (p2 > nl))
275 goto fail;
276 *(p2++) = 0;
277 for(; (*p2 == ' ') || (*p2 == '\t'); p2++);
278 for(nl = p2; (*nl != '\r') && (*nl != '\n'); nl++);
279 if(!SKIPNL(nl))
280 goto fail;
281 headappheader(resp, p, p2);
282 p = nl;
283 }
284 return(resp);
285
286fail:
287 freehthead(resp);
288 return(NULL);
289}
290
291static off_t passdata(int src, int dst, struct charbuf *buf, off_t max)
292{
293 size_t dataoff, smax;
294 off_t sent;
295 int eof, ret;
296
297 sent = 0;
298 eof = 0;
299 while(!eof || (buf->d > 0)) {
300 if(!eof && (buf->d < buf->s) && ((max < 0) || (sent + buf->d < max))) {
301 while(1) {
302 ret = recv(src, buf->b + buf->d, buf->s - buf->d, MSG_DONTWAIT);
303 if((ret < 0) && (errno == EAGAIN)) {
304 } else if(ret < 0) {
305 return(-1);
306 } else if(ret == 0) {
307 eof = 1;
308 break;
309 } else {
310 buf->d += ret;
311 break;
312 }
313 if(buf->d > 0)
314 break;
315 if(block(src, EV_READ, 0) <= 0)
316 return(-1);
317 }
318 }
319 for(dataoff = 0; (dataoff < buf->d) && ((max < 0) || (sent < max));) {
320 if(block(dst, EV_WRITE, 120) <= 0)
321 return(-1);
322 smax = buf->d - dataoff;
323 if(sent + smax > max)
324 smax = max - sent;
325 ret = send(dst, buf->b + dataoff, smax, MSG_NOSIGNAL | MSG_DONTWAIT);
326 if(ret < 0)
327 return(-1);
328 dataoff += ret;
329 sent += ret;
330 }
331 bufeat(*buf, dataoff);
332 }
333 return(sent);
334}
335
66987955
FT
336static void serve(struct muth *muth, va_list args)
337{
338 vavar(int, fd);
9d87a119
FT
339 vavar(struct sockaddr_storage, name);
340 int cfd;
341 char old;
342 char *hd;
343 struct charbuf inbuf, outbuf;
344 struct hthead *req, *resp;
a0327573 345 off_t dlen, sent;
c9955b14 346 size_t headoff;
9d87a119 347 char nmbuf[256];
66987955 348
9d87a119
FT
349 bufinit(inbuf);
350 bufinit(outbuf);
351 cfd = -1;
352 req = NULL;
66987955 353 while(1) {
9d87a119
FT
354 /*
355 * First, find and decode the header:
356 */
357 if((headoff = readhead(fd, &inbuf)) < 0)
358 goto out;
359 if(headoff > 65536) {
360 /* We cannot handle arbitrarily large headers, as they
361 * need to fit within a single Unix datagram. This is
362 * probably a safe limit, and larger packets than this are
363 * most likely erroneous (or malicious) anyway. */
364 goto out;
365 }
366 old = inbuf.b[headoff];
367 inbuf.b[headoff] = 0;
368 if((req = parserawreq(inbuf.b)) == NULL)
66987955 369 goto out;
9d87a119
FT
370 inbuf.b[headoff] = old;
371 bufeat(inbuf, headoff);
9e9eca79
FT
372 /* We strip off the leading slash from the rest string, so
373 * that multiplexers can parse coherently. */
374 if(req->rest[0] == '/')
375 replrest(req, req->rest + 1);
9d87a119
FT
376
377 /*
378 * Add metainformation and then send the request to the root
379 * multiplexer:
380 */
381 if(name.ss_family == AF_INET) {
382 headappheader(req, "X-Ash-Address", inet_ntop(AF_INET, &((struct sockaddr_in *)&name)->sin_addr, nmbuf, sizeof(nmbuf)));
383 headappheader(req, "X-Ash-Port", sprintf3("%i", ntohs(((struct sockaddr_in *)&name)->sin_port)));
384 } else if(name.ss_family == AF_INET6) {
385 headappheader(req, "X-Ash-Address", inet_ntop(AF_INET6, &((struct sockaddr_in6 *)&name)->sin6_addr, nmbuf, sizeof(nmbuf)));
386 headappheader(req, "X-Ash-Port", sprintf3("%i", ntohs(((struct sockaddr_in6 *)&name)->sin6_port)));
387 }
388 cfd = sendreq(plex, req);
a0327573
FT
389
390 /*
391 * If there is message data, pass it:
392 */
393 if((hd = getheader(req, "content-length")) != NULL) {
394 dlen = atoo(hd);
395 if(dlen > 0)
396 passdata(fd, cfd, &inbuf, dlen);
397 }
9d87a119
FT
398
399 /*
400 * Find and decode the response header:
401 */
402 outbuf.d = 0;
403 headoff = readhead(cfd, &outbuf);
404 hd = memcpy(smalloc(headoff + 1), outbuf.b, headoff);
405 hd[headoff] = 0;
406 if((resp = parserawresp(hd)) == NULL)
66987955 407 goto out;
9d87a119
FT
408
409 /*
410 * Pass the actual output:
411 */
412 sizebuf(outbuf, 65536);
413 sent = passdata(cfd, fd, &outbuf, -1);
414 sent -= headoff;
415
416 /*
417 * Check for connection expiry
418 */
419 if(strcasecmp(req->method, "head")) {
420 if((hd = getheader(resp, "content-length")) != NULL) {
421 if(sent != atoo(hd)) {
422 /* Exit because of error */
423 goto out;
424 }
425 } else {
426 if(((hd = getheader(resp, "transfer-encoding")) == NULL) || !strcasecmp(hd, "identity"))
427 break;
428 }
429 if(((hd = getheader(req, "connection")) != NULL) && !strcasecmp(hd, "close"))
430 break;
431 if(((hd = getheader(resp, "connection")) != NULL) && !strcasecmp(hd, "close"))
432 break;
433 }
434
435 close(cfd);
436 cfd = -1;
437 freehthead(req);
438 req = NULL;
439 freehthead(resp);
440 resp = NULL;
66987955
FT
441 }
442
443out:
9d87a119
FT
444 if(cfd >= 0)
445 close(cfd);
446 if(req != NULL)
447 freehthead(req);
448 if(resp != NULL)
449 freehthead(resp);
450 buffree(inbuf);
451 buffree(outbuf);
66987955
FT
452 close(fd);
453}
454
f4cdf919
FT
455static void listenloop(struct muth *muth, va_list args)
456{
457 vavar(int, ss);
458 int ns;
459 struct sockaddr_storage name;
460 socklen_t namelen;
461
462 while(1) {
463 namelen = sizeof(name);
9d87a119 464 block(ss, EV_READ, 0);
f4cdf919 465 ns = accept(ss, (struct sockaddr *)&name, &namelen);
66987955
FT
466 if(ns < 0) {
467 flog(LOG_ERR, "accept: %s", strerror(errno));
468 goto out;
469 }
9d87a119 470 mustart(serve, ns, name);
f4cdf919 471 }
66987955
FT
472
473out:
474 close(ss);
f4cdf919
FT
475}
476
477static void ioloop(void)
478{
479 int ret;
480 fd_set rfds, wfds, efds;
481 struct blocker *bl, *nbl;
9d87a119
FT
482 struct timeval toval;
483 time_t now, timeout;
f4cdf919
FT
484 int maxfd;
485 int ev;
486
66987955 487 while(blockers != NULL) {
f4cdf919
FT
488 FD_ZERO(&rfds);
489 FD_ZERO(&wfds);
490 FD_ZERO(&efds);
491 maxfd = 0;
9d87a119
FT
492 now = time(NULL);
493 timeout = 0;
f4cdf919
FT
494 for(bl = blockers; bl; bl = bl->n) {
495 if(bl->ev & EV_READ)
496 FD_SET(bl->fd, &rfds);
497 if(bl->ev & EV_WRITE)
498 FD_SET(bl->fd, &wfds);
499 FD_SET(bl->fd, &efds);
500 if(bl->fd > maxfd)
501 maxfd = bl->fd;
9d87a119
FT
502 if((bl->to != 0) && ((timeout == 0) || (timeout > bl->to)))
503 timeout = bl->to;
f4cdf919 504 }
9d87a119
FT
505 toval.tv_sec = timeout - now;
506 toval.tv_usec = 0;
507 ret = select(maxfd + 1, &rfds, &wfds, &efds, timeout?(&toval):NULL);
f4cdf919
FT
508 if(ret < 0) {
509 if(errno != EINTR) {
510 flog(LOG_CRIT, "ioloop: select errored out: %s", strerror(errno));
511 /* To avoid CPU hogging in case it's bad, which it
512 * probably is. */
513 sleep(1);
514 }
515 }
9d87a119 516 now = time(NULL);
f4cdf919
FT
517 for(bl = blockers; bl; bl = nbl) {
518 nbl = bl->n;
519 ev = 0;
520 if(FD_ISSET(bl->fd, &rfds))
521 ev |= EV_READ;
522 if(FD_ISSET(bl->fd, &wfds))
523 ev |= EV_WRITE;
524 if(FD_ISSET(bl->fd, &efds))
525 ev = -1;
66987955
FT
526 if(ev != 0)
527 resume(bl->th, ev);
9d87a119
FT
528 else if((bl->to != 0) && (bl->to <= now))
529 resume(bl->th, 0);
f4cdf919
FT
530 }
531 }
532}
f0bbedf7
FT
533
534int main(int argc, char **argv)
535{
f4cdf919
FT
536 int fd;
537
9d87a119
FT
538 if(argc < 2) {
539 fprintf(stderr, "usage: htparser ROOT [ARGS...]\n");
540 exit(1);
541 }
542 if((plex = stdmkchild(argv + 1)) < 0) {
543 flog(LOG_ERR, "could not spawn root multiplexer: %s", strerror(errno));
544 return(1);
545 }
f4cdf919
FT
546 if((fd = listensock6(8080)) < 0) {
547 flog(LOG_ERR, "could not listen on IPv6: %s", strerror(errno));
548 return(1);
549 }
550 mustart(listenloop, fd);
551 if((fd = listensock4(8080)) < 0) {
552 if(errno != EADDRINUSE) {
553 flog(LOG_ERR, "could not listen on IPv4: %s", strerror(errno));
554 return(1);
555 }
556 } else {
557 mustart(listenloop, fd);
558 }
559 ioloop();
560 return(0);
f0bbedf7 561}