htparser: Use bufio instead of stdio for greater responsiveness.
[ashd.git] / src / htparser.c
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 <fcntl.h>
23 #include <string.h>
24 #include <sys/socket.h>
25 #include <pwd.h>
26 #include <sys/signal.h>
27 #include <errno.h>
28
29 #ifdef HAVE_CONFIG_H
30 #include <config.h>
31 #endif
32 #include <utils.h>
33 #include <mt.h>
34 #include <mtio.h>
35 #include <log.h>
36 #include <req.h>
37 #include <proc.h>
38 #include <bufio.h>
39
40 #include "htparser.h"
41
42 static int plex;
43 static char *pidfile = NULL;
44 static int daemonize, usesyslog;
45 struct mtbuf listeners;
46
47 static void trimx(struct hthead *req)
48 {
49     int i;
50     
51     i = 0;
52     while(i < req->noheaders) {
53         if(!strncasecmp(req->headers[i][0], "x-ash-", 6)) {
54             free(req->headers[i][0]);
55             free(req->headers[i][1]);
56             free(req->headers[i]);
57             memmove(req->headers + i, req->headers + i + 1, sizeof(*req->headers) * (--req->noheaders - i));
58         } else {
59             i++;
60         }
61     }
62 }
63
64 static struct hthead *parsereq(struct bufio *in)
65 {
66     struct hthead *req;
67     struct charbuf method, url, ver;
68     int c;
69     
70     req = NULL;
71     bufinit(method);
72     bufinit(url);
73     bufinit(ver);
74     while(1) {
75         c = biogetc(in);
76         if(c == ' ') {
77             break;
78         } else if((c == EOF) || (c < 32) || (c >= 128)) {
79             goto fail;
80         } else {
81             bufadd(method, c);
82             if(method.d >= 128)
83                 goto fail;
84         }
85     }
86     while(1) {
87         c = biogetc(in);
88         if(c == ' ') {
89             break;
90         } else if((c == EOF) || (c < 32)) {
91             goto fail;
92         } else {
93             bufadd(url, c);
94             if(url.d >= 65536)
95                 goto fail;
96         }
97     }
98     while(1) {
99         c = biogetc(in);
100         if(c == 10) {
101             break;
102         } else if(c == 13) {
103         } else if((c == EOF) || (c < 32) || (c >= 128)) {
104             goto fail;
105         } else {
106             bufadd(ver, c);
107             if(ver.d >= 128)
108                 goto fail;
109         }
110     }
111     bufadd(method, 0);
112     bufadd(url, 0);
113     bufadd(ver, 0);
114     req = mkreq(method.b, url.b, ver.b);
115     if(parseheadersb(req, in))
116         goto fail;
117     trimx(req);
118     goto out;
119     
120 fail:
121     if(req != NULL) {
122         freehthead(req);
123         req = NULL;
124     }
125 out:
126     buffree(method);
127     buffree(url);
128     buffree(ver);
129     return(req);
130 }
131
132 static off_t passdata(struct bufio *in, struct bufio *out, off_t max)
133 {
134     ssize_t read;
135     off_t total;
136     
137     total = 0;
138     while(!bioeof(in) && ((max < 0) || (total < max))) {
139         if((read = biordata(in)) > 0) {
140             if(max >= 0)
141                 read = min(max - total, read);
142             if((read = biowritesome(out, in->rbuf.b + in->rh, read)) < 0)
143                 return(-1);
144             in->rh += read;
145             total += read;
146         }
147         if(biorspace(in) && ((max < 0) || (biordata(in) < max - total)) && (biofillsome(in) < 0))
148             return(-1);
149     }
150     return(total);
151 }
152
153 static int recvchunks(struct bufio *in, struct bufio *out)
154 {
155     size_t read, chlen;
156     int c, r;
157     
158     while(1) {
159         chlen = 0;
160         r = 0;
161         while(1) {
162             c = biogetc(in);
163             if(c == 10) {
164                 if(!r)
165                     return(-1);
166                 break;
167             } else if(c == 13) {
168             } else if((c >= '0') && (c <= '9')) {
169                 chlen = (chlen << 4) + (c - '0');
170                 r = 1;
171             } else if((c >= 'A') && (c <= 'F')) {
172                 chlen = (chlen << 4) + (c + 10 - 'A');
173                 r = 1;
174             } else if((c >= 'a') && (c <= 'f')) {
175                 chlen = (chlen << 4) + (c + 10 - 'a');
176                 r = 1;
177             } else {
178                 /* XXX: Technically, there may be chunk extensions to
179                  * be read, but since that will likely never actually
180                  * happen in practice, I can just as well add support
181                  * for that if it actually does become relevant. */
182                 return(-1);
183             }
184         }
185         if(chlen == 0)
186             break;
187         while(chlen > 0) {
188             if((read = biordata(in)) > 0) {
189                 if((read = biowritesome(out, in->rbuf.b + in->rh, min(read, chlen))) < 0)
190                     return(-1);
191                 in->rh += read;
192                 chlen -= read;
193             }
194             if(biorspace(in) && (biordata(in) < chlen) && (biofillsome(in) <= 0))
195                 return(-1);
196         }
197         if((biogetc(in) != 13) || (biogetc(in) != 10))
198             return(-1);
199     }
200     /* XXX: Technically, there may be trailers to be read, but that's
201      * just about as likely as chunk extensions. */
202     if((biogetc(in) != 13) || (biogetc(in) != 10))
203         return(-1);
204     return(0);
205 }
206
207 static int passchunks(struct bufio *in, struct bufio *out)
208 {
209     size_t read;
210     
211     while(!bioeof(in)) {
212         if((read = biordata(in)) > 0) {
213             bioprintf(out, "%zx\r\n", read);
214             if(biowrite(out, in->rbuf.b + in->rh, read) != read)
215                 return(-1);
216             in->rh += read;
217             bioprintf(out, "\r\n");
218             if(bioflush(out) < 0)
219                 return(-1);
220         }
221         if(biorspace(in) && (biofillsome(in) < 0))
222             return(-1);
223     }
224     bioprintf(out, "0\r\n\r\n");
225     return(0);
226 }
227
228 static int hasheader(struct hthead *head, char *name, char *val)
229 {
230     char *hd;
231     
232     if((hd = getheader(head, name)) == NULL)
233         return(0);
234     return(!strcasecmp(hd, val));
235 }
236
237 static int canonreq(struct hthead *req)
238 {
239     char *p, *p2, *r;
240     int n;
241
242     if(req->url[0] == '/') {
243         replrest(req, req->url + 1);
244         if((p = strchr(req->rest, '?')) != NULL)
245             *p = 0;
246         return(1);
247     }
248     if((p = strstr(req->url, "://")) != NULL) {
249         n = p - req->url;
250         if(((n == 4) && !strncasecmp(req->url, "http", 4)) ||
251            ((n == 5) && !strncasecmp(req->url, "https", 5))) {
252             if(getheader(req, "host"))
253                 return(0);
254             p += 3;
255             if((p2 = strchr(p, '/')) == NULL) {
256                 headappheader(req, "Host", p);
257                 free(req->url);
258                 req->url = sstrdup("/");
259             } else {
260                 r = sstrdup(p2);
261                 *(p2++) = 0;
262                 headappheader(req, "Host", p);
263                 free(req->url);
264                 req->url = r;
265             }
266             replrest(req, req->url + 1);
267             if((p = strchr(req->rest, '?')) != NULL)
268                 *p = 0;
269             return(1);
270         }
271     }
272     return(0);
273 }
274
275 static int http10keep(struct hthead *req, struct hthead *resp)
276 {
277     int fc;
278     
279     fc = hasheader(resp, "connection", "close");
280     headrmheader(resp, "connection");
281     if(!fc && hasheader(req, "connection", "keep-alive")) {
282         headappheader(resp, "Connection", "Keep-Alive");
283         return(1);
284     } else {
285         return(0);
286     }
287 }
288
289 static char *connid(void)
290 {
291     static struct charbuf cur;
292     int i;
293     char *ret;
294     
295     for(i = 0; i < cur.d; i++) {
296         if((++cur.b[i]) > 'Z')
297             cur.b[i] = 'A';
298         else
299             goto done;
300     }
301     bufadd(cur, 'A');
302 done:
303     ret = memcpy(smalloc(cur.d + 1), cur.b, cur.d);
304     ret[cur.d] = 0;
305     return(ret);
306 }
307
308 void serve(struct bufio *in, struct conn *conn)
309 {
310     int pfds[2];
311     struct bufio *out;
312     struct hthead *req, *resp;
313     char *hd, *id;
314     off_t dlen;
315     int keep;
316     
317     id = connid();
318     out = NULL;
319     req = resp = NULL;
320     while(plex >= 0) {
321         bioflush(in);
322         if((req = parsereq(in)) == NULL)
323             break;
324         if(!canonreq(req))
325             break;
326         
327         headappheader(req, "X-Ash-Connection-ID", id);
328         if((conn->initreq != NULL) && conn->initreq(conn, req))
329             break;
330         
331         if((plex < 0) || block(plex, EV_WRITE, 60) <= 0)
332             break;
333         if(socketpair(PF_UNIX, SOCK_STREAM, 0, pfds))
334             break;
335         if(sendreq(plex, req, pfds[0]))
336             break;
337         close(pfds[0]);
338         out = mtbioopen(pfds[1], 1, 600, "r+", NULL);
339
340         if(getheader(req, "content-type") != NULL) {
341             if((hd = getheader(req, "content-length")) != NULL) {
342                 dlen = atoo(hd);
343                 if(dlen > 0) {
344                     if(passdata(in, out, dlen) != dlen)
345                         break;
346                 }
347             } else if(((hd = getheader(req, "transfer-encoding")) != NULL) && !strcasecmp(hd, "chunked")) {
348                 if(recvchunks(in, out))
349                     break;
350             } else {
351                 /* Ignore rather than abort, to be kinder to broken clients. */
352                 headrmheader(req, "content-type");
353             }
354         }
355         if(bioflush(out))
356             break;
357         /* Make sure to send EOF */
358         shutdown(pfds[1], SHUT_WR);
359         
360         if((resp = parseresponseb(out)) == NULL)
361             break;
362         replstr(&resp->ver, req->ver);
363         
364         if(!getheader(resp, "server"))
365             headappheader(resp, "Server", sprintf3("ashd/%s", VERSION));
366
367         if(!strcasecmp(req->ver, "HTTP/1.0")) {
368             if(!strcasecmp(req->method, "head")) {
369                 keep = http10keep(req, resp);
370                 writerespb(in, resp);
371                 bioprintf(in, "\r\n");
372             } else if((hd = getheader(resp, "content-length")) != NULL) {
373                 keep = http10keep(req, resp);
374                 dlen = atoo(hd);
375                 writerespb(in, resp);
376                 bioprintf(in, "\r\n");
377                 if(passdata(out, in, dlen) != dlen)
378                     break;
379             } else {
380                 headrmheader(resp, "connection");
381                 writerespb(in, resp);
382                 bioprintf(in, "\r\n");
383                 passdata(out, in, -1);
384                 break;
385             }
386             if(!keep)
387                 break;
388         } else if(!strcasecmp(req->ver, "HTTP/1.1")) {
389             if(!strcasecmp(req->method, "head")) {
390                 writerespb(in, resp);
391                 bioprintf(in, "\r\n");
392             } else if((hd = getheader(resp, "content-length")) != NULL) {
393                 writerespb(in, resp);
394                 bioprintf(in, "\r\n");
395                 dlen = atoo(hd);
396                 if(passdata(out, in, dlen) != dlen)
397                     break;
398             } else if(!getheader(resp, "transfer-encoding")) {
399                 headappheader(resp, "Transfer-Encoding", "chunked");
400                 writerespb(in, resp);
401                 bioprintf(in, "\r\n");
402                 if(passchunks(out, in))
403                     break;
404             } else {
405                 writerespb(in, resp);
406                 bioprintf(in, "\r\n");
407                 passdata(out, in, -1);
408                 break;
409             }
410             if(hasheader(req, "connection", "close") || hasheader(resp, "connection", "close"))
411                 break;
412         } else {
413             break;
414         }
415
416         bioclose(out);
417         out = NULL;
418         freehthead(req);
419         freehthead(resp);
420         req = resp = NULL;
421     }
422     
423     if(out != NULL)
424         bioclose(out);
425     if(req != NULL)
426         freehthead(req);
427     if(resp != NULL)
428         freehthead(resp);
429     bioclose(in);
430     free(id);
431 }
432
433 static void plexwatch(struct muth *muth, va_list args)
434 {
435     vavar(int, fd);
436     char *buf;
437     int i, s, ret;
438     
439     s = 0;
440     while(1) {
441         if(block(fd, EV_READ, 0) == 0)
442             break;
443         buf = smalloc(65536);
444         ret = recv(fd, buf, 65536, 0);
445         if(ret < 0) {
446             flog(LOG_WARNING, "received error on rootplex read channel: %s", strerror(errno));
447             exit(1);
448         } else if(ret == 0) {
449             s = 1;
450             free(buf);
451             break;
452         }
453         /* Maybe I'd like to implement some protocol in this direction
454          * some day... */
455         free(buf);
456     }
457     shutdown(plex, SHUT_RDWR);
458     for(i = 0; i < listeners.d; i++) {
459         if(listeners.b[i] == muth)
460             bufdel(listeners, i);
461     }
462     if(s) {
463         flog(LOG_INFO, "root handler exited, so shutting down listening...");
464         while(listeners.d > 0)
465             resume(listeners.b[0], 0);
466     }
467 }
468
469 static void initroot(void *uu)
470 {
471     int fd;
472     
473     setsid();
474     if(daemonize) {
475         chdir("/");
476         if((fd = open("/dev/null", O_RDWR)) >= 0) {
477             dup2(fd, 0);
478             dup2(fd, 1);
479             dup2(fd, 2);
480             close(fd);
481         }
482     }
483     if(usesyslog)
484         putenv("ASHD_USESYSLOG=1");
485     else
486         unsetenv("ASHD_USESYSLOG");
487 }
488
489 static void usage(FILE *out)
490 {
491     fprintf(out, "usage: htparser [-hSf] [-u USER] [-r ROOT] [-p PIDFILE] PORTSPEC... -- ROOT [ARGS...]\n");
492     fprintf(out, "\twhere PORTSPEC is HANDLER[:PAR[=VAL][(,PAR[=VAL])...]] (try HANDLER:help)\n");
493     fprintf(out, "\tavailable handlers are `plain' and `ssl'.\n");
494 }
495
496 static void addport(char *spec)
497 {
498     char *nm, *p, *p2, *n;
499     struct charvbuf pars, vals;
500     
501     bufinit(pars);
502     bufinit(vals);
503     if((p = strchr(spec, ':')) == NULL) {
504         nm = spec;
505     } else {
506         nm = spec;
507         *(p++) = 0;
508         do {
509             if((n = strchr(p, ',')) != NULL)
510                 *(n++) = 0;
511             if((p2 = strchr(p, '=')) != NULL)
512                 *(p2++) = 0;
513             if(!*p) {
514                 usage(stderr);
515                 exit(1);
516             }
517             bufadd(pars, p);
518             if(p2)
519                 bufadd(vals, p2);
520             else
521                 bufadd(vals, "");
522         } while((p = n) != NULL);
523     }
524     
525     /* XXX: It would be nice to decentralize this, but, meh... */
526     if(!strcmp(nm, "plain")) {
527         handleplain(pars.d, pars.b, vals.b);
528 #ifdef HAVE_GNUTLS
529     } else if(!strcmp(nm, "ssl")) {
530         handlegnussl(pars.d, pars.b, vals.b);
531 #endif
532     } else {
533         flog(LOG_ERR, "htparser: unknown port handler `%s'", nm);
534         exit(1);
535     }
536     
537     buffree(pars);
538     buffree(vals);
539 }
540
541 static void sighandler(int sig)
542 {
543     exitioloop(1);
544 }
545
546 int main(int argc, char **argv)
547 {
548     int c, d;
549     int i, s1;
550     char *root;
551     FILE *pidout;
552     struct passwd *pwent;
553     
554     daemonize = usesyslog = 0;
555     root = NULL;
556     pwent = NULL;
557     while((c = getopt(argc, argv, "+hSfu:r:p:")) >= 0) {
558         switch(c) {
559         case 'h':
560             usage(stdout);
561             exit(0);
562         case 'f':
563             daemonize = 1;
564             break;
565         case 'S':
566             usesyslog = 1;
567             break;
568         case 'u':
569             if((pwent = getpwnam(optarg)) == NULL) {
570                 flog(LOG_ERR, "could not find user %s", optarg);
571                 exit(1);
572             }
573             break;
574         case 'r':
575             root = optarg;
576             break;
577         case 'p':
578             pidfile = optarg;
579             break;
580         default:
581             usage(stderr);
582             exit(1);
583         }
584     }
585     s1 = 0;
586     for(i = optind; i < argc; i++) {
587         if(!strcmp(argv[i], "--"))
588             break;
589         s1 = 1;
590         addport(argv[i]);
591     }
592     if(!s1 || (i == argc)) {
593         usage(stderr);
594         exit(1);
595     }
596     if((plex = stdmkchild(argv + ++i, initroot, NULL)) < 0) {
597         flog(LOG_ERR, "could not spawn root multiplexer: %s", strerror(errno));
598         return(1);
599     }
600     bufadd(listeners, mustart(plexwatch, plex));
601     pidout = NULL;
602     if(pidfile != NULL) {
603         if((pidout = fopen(pidfile, "w")) == NULL) {
604             flog(LOG_ERR, "could not open %s for writing: %s", pidfile, strerror(errno));
605             return(1);
606         }
607     }
608     if(usesyslog)
609         opensyslog();
610     if(root) {
611         if(chdir(root) || chroot(root)) {
612             flog(LOG_ERR, "could not chroot to %s: %s", root, strerror(errno));
613             exit(1);
614         }
615     }
616     if(pwent) {
617         if(setgid(pwent->pw_gid)) {
618             flog(LOG_ERR, "could not switch group to %i: %s", (int)pwent->pw_gid, strerror(errno));
619             exit(1);
620         }
621         if(setuid(pwent->pw_uid)) {
622             flog(LOG_ERR, "could not switch user to %i: %s", (int)pwent->pw_uid, strerror(errno));
623             exit(1);
624         }
625     }
626     signal(SIGPIPE, SIG_IGN);
627     signal(SIGCHLD, SIG_IGN);
628     signal(SIGINT, sighandler);
629     signal(SIGTERM, sighandler);
630     if(daemonize) {
631         daemon(0, 0);
632     }
633     if(pidout != NULL) {
634         fprintf(pidout, "%i\n", getpid());
635         fclose(pidout);
636     }
637     d = 0;
638     while(!d) {
639         switch(ioloop()) {
640         case 0:
641             d = 1;
642             break;
643         case 1:
644             if(listeners.d > 0) {
645                 while(listeners.d > 0)
646                     resume(listeners.b[0], 0);
647                 flog(LOG_INFO, "no longer listening");
648             } else {
649                 d = 1;
650             }
651             break;
652         }
653     }
654     return(0);
655 }