X-Git-Url: http://www.dolda2000.com/gitweb/?a=blobdiff_plain;f=src%2Fhtparser.c;h=9f02adea02fe44c7463fc22d9709229fc7f5ea87;hb=f9255ddd4966787957cb8eb676155d99883d7955;hp=3a788ac899bee541cb997ba729cf039955c2cc2e;hpb=f4cdf91946475f428f3861275ce9d07f2da73a93;p=ashd.git diff --git a/src/htparser.c b/src/htparser.c index 3a788ac..9f02ade 100644 --- a/src/htparser.c +++ b/src/htparser.c @@ -20,9 +20,9 @@ #include #include #include -#include #include #include +#include #include #ifdef HAVE_CONFIG_H @@ -30,42 +30,12 @@ #endif #include #include +#include #include +#include +#include -#define EV_READ 1 -#define EV_WRITE 2 - -struct blocker { - struct blocker *n, *p; - int fd; - int ev; - struct muth *th; -}; - -static struct blocker *blockers; - -static int block(int fd, int ev) -{ - struct blocker *bl; - int rv; - - omalloc(bl); - bl->fd = fd; - bl->ev = ev; - bl->th = current; - bl->n = blockers; - if(blockers) - blockers->p = bl; - blockers = bl; - rv = yield(); - if(bl->n) - bl->n->p = bl->p; - if(bl->p) - bl->p->n = bl->n; - if(bl == blockers) - blockers = bl->n; - return(rv); -} +static int plex; static int listensock4(int port) { @@ -115,6 +85,286 @@ static int listensock6(int port) return(fd); } +static struct hthead *parsereq(FILE *in) +{ + struct hthead *req; + struct charbuf method, url, ver; + int c; + + req = NULL; + bufinit(method); + bufinit(url); + bufinit(ver); + while(1) { + c = getc(in); + if(c == ' ') { + break; + } else if((c == EOF) || (c < 32) || (c >= 128)) { + goto fail; + } else { + bufadd(method, c); + } + } + while(1) { + c = getc(in); + if(c == ' ') { + break; + } else if((c == EOF) || (c < 32)) { + goto fail; + } else { + bufadd(url, c); + } + } + while(1) { + c = getc(in); + if(c == 10) { + break; + } else if(c == 13) { + } else if((c == EOF) || (c < 32) || (c >= 128)) { + goto fail; + } else { + bufadd(ver, c); + } + } + bufadd(method, 0); + bufadd(url, 0); + bufadd(ver, 0); + req = mkreq(method.b, url.b, ver.b); + if(parseheaders(req, in)) + goto fail; + goto out; + +fail: + if(req != NULL) { + freehthead(req); + req = NULL; + } +out: + buffree(method); + buffree(url); + buffree(ver); + return(req); +} + +static struct hthead *parseresp(FILE *in) +{ + struct hthead *req; + int code; + struct charbuf ver, msg; + int c; + + req = NULL; + bufinit(ver); + bufinit(msg); + code = 0; + while(1) { + c = getc(in); + if(c == ' ') { + break; + } else if((c == EOF) || (c < 32) || (c >= 128)) { + goto fail; + } else { + bufadd(ver, c); + } + } + while(1) { + c = getc(in); + if(c == ' ') { + break; + } else if((c == EOF) || (c < '0') || (c > '9')) { + goto fail; + } else { + code = (code * 10) + (c - '0'); + } + } + while(1) { + c = getc(in); + if(c == 10) { + break; + } else if(c == 13) { + } else if((c == EOF) || (c < 32)) { + goto fail; + } else { + bufadd(msg, c); + } + } + bufadd(msg, 0); + bufadd(ver, 0); + req = mkresp(code, msg.b, ver.b); + if(parseheaders(req, in)) + goto fail; + goto out; + +fail: + if(req != NULL) { + freehthead(req); + req = NULL; + } +out: + buffree(msg); + buffree(ver); + return(req); +} + +static off_t passdata(FILE *in, FILE *out, off_t max) +{ + size_t read; + off_t total; + char buf[8192]; + + total = 0; + while(!feof(in) && ((max < 0) || (total < max))) { + read = sizeof(buf); + if(max >= 0) + read = min(max - total, read); + read = fread(buf, 1, read, in); + if(ferror(in)) + return(-1); + if(fwrite(buf, 1, read, out) != read) + return(-1); + total += read; + } + return(total); +} + +static int passchunks(FILE *in, FILE *out) +{ + char buf[8192]; + size_t read; + + do { + read = fread(buf, 1, sizeof(buf), in); + if(ferror(in)) + return(-1); + fprintf(out, "%zx\r\n", read); + if(fwrite(buf, 1, read, out) != read) + return(-1); + fprintf(out, "\r\n"); + } while(read > 0); + return(0); +} + +static int hasheader(struct hthead *head, char *name, char *val) +{ + char *hd; + + if((hd = getheader(head, name)) == NULL) + return(0); + return(!strcasecmp(hd, val)); +} + +static void serve(struct muth *muth, va_list args) +{ + vavar(int, fd); + vavar(struct sockaddr_storage, name); + int pfds[2]; + FILE *in, *out; + struct hthead *req, *resp; + char nmbuf[256]; + char *hd, *p; + off_t dlen; + + in = mtstdopen(fd, 1, 60, "r+"); + out = NULL; + req = resp = NULL; + while(1) { + if((req = parsereq(in)) == NULL) + break; + replrest(req, req->url); + if(req->rest[0] == '/') + replrest(req, req->rest + 1); + if((p = strchr(req->rest, '?')) != NULL) + *p = 0; + + if(name.ss_family == AF_INET) { + headappheader(req, "X-Ash-Address", inet_ntop(AF_INET, &((struct sockaddr_in *)&name)->sin_addr, nmbuf, sizeof(nmbuf))); + headappheader(req, "X-Ash-Port", sprintf3("%i", ntohs(((struct sockaddr_in *)&name)->sin_port))); + } else if(name.ss_family == AF_INET6) { + headappheader(req, "X-Ash-Address", inet_ntop(AF_INET6, &((struct sockaddr_in6 *)&name)->sin6_addr, nmbuf, sizeof(nmbuf))); + headappheader(req, "X-Ash-Port", sprintf3("%i", ntohs(((struct sockaddr_in6 *)&name)->sin6_port))); + } + + if(block(plex, EV_WRITE, 60) <= 0) + break; + if(socketpair(PF_UNIX, SOCK_STREAM, 0, pfds)) + break; + if(sendreq(plex, req, pfds[0])) + break; + close(pfds[0]); + out = mtstdopen(pfds[1], 1, 600, "r+"); + + if((hd = getheader(req, "content-length")) != NULL) { + dlen = atoo(hd); + if(dlen > 0) { + if(passdata(in, out, dlen) != dlen) + break; + } + } + if(fflush(out)) + break; + /* Make sure to send EOF */ + shutdown(pfds[1], SHUT_WR); + + if((resp = parseresp(out)) == NULL) + break; + replstr(&resp->ver, req->ver); + + if(!strcmp(req->ver, "HTTP/1.0")) { + writeresp(in, resp); + fprintf(in, "\r\n"); + if((hd = getheader(resp, "content-length")) != NULL) { + dlen = passdata(out, in, -1); + if(dlen != atoo(hd)) + break; + if(!hasheader(req, "connection", "keep-alive")) + break; + } else { + passdata(out, in, -1); + break; + } + if(hasheader(req, "connection", "close") || hasheader(resp, "connection", "close")) + break; + } else if(!strcmp(req->ver, "HTTP/1.1")) { + if((hd = getheader(resp, "content-length")) != NULL) { + writeresp(in, resp); + fprintf(in, "\r\n"); + dlen = passdata(out, in, -1); + if(dlen != atoo(hd)) + break; + } else if(!getheader(resp, "transfer-encoding")) { + headappheader(resp, "Transfer-Encoding", "chunked"); + writeresp(in, resp); + fprintf(in, "\r\n"); + if(passchunks(out, in)) + break; + } else { + writeresp(in, resp); + fprintf(in, "\r\n"); + passdata(out, in, -1); + break; + } + if(hasheader(req, "connection", "close") || hasheader(resp, "connection", "close")) + break; + } else { + break; + } + + fclose(out); + out = NULL; + freehthead(req); + freehthead(resp); + req = resp = NULL; + } + + if(out != NULL) + fclose(out); + if(req != NULL) + freehthead(req); + if(resp != NULL) + freehthead(resp); + fclose(in); +} + static void listenloop(struct muth *muth, va_list args) { vavar(int, ss); @@ -124,56 +374,38 @@ static void listenloop(struct muth *muth, va_list args) while(1) { namelen = sizeof(name); - block(ss, EV_READ); + block(ss, EV_READ, 0); ns = accept(ss, (struct sockaddr *)&name, &namelen); - block(ns, EV_WRITE); - write(ns, "test\n", 5); - close(ns); + if(ns < 0) { + flog(LOG_ERR, "accept: %s", strerror(errno)); + goto out; + } + mustart(serve, ns, name); } + +out: + close(ss); } -static void ioloop(void) +static void plexwatch(struct muth *muth, va_list args) { + vavar(int, fd); + char *buf; int ret; - fd_set rfds, wfds, efds; - struct blocker *bl, *nbl; - int maxfd; - int ev; while(1) { - FD_ZERO(&rfds); - FD_ZERO(&wfds); - FD_ZERO(&efds); - maxfd = 0; - for(bl = blockers; bl; bl = bl->n) { - if(bl->ev & EV_READ) - FD_SET(bl->fd, &rfds); - if(bl->ev & EV_WRITE) - FD_SET(bl->fd, &wfds); - FD_SET(bl->fd, &efds); - if(bl->fd > maxfd) - maxfd = bl->fd; - } - ret = select(maxfd + 1, &rfds, &wfds, &efds, NULL); + block(fd, EV_READ, 0); + buf = smalloc(65536); + ret = recv(fd, buf, 65536, 0); if(ret < 0) { - if(errno != EINTR) { - flog(LOG_CRIT, "ioloop: select errored out: %s", strerror(errno)); - /* To avoid CPU hogging in case it's bad, which it - * probably is. */ - sleep(1); - } - } - for(bl = blockers; bl; bl = nbl) { - nbl = bl->n; - ev = 0; - if(FD_ISSET(bl->fd, &rfds)) - ev |= EV_READ; - if(FD_ISSET(bl->fd, &wfds)) - ev |= EV_WRITE; - if(FD_ISSET(bl->fd, &efds)) - ev = -1; - resume(bl->th, ev); + flog(LOG_WARNING, "received error on rootplex read channel: %s", strerror(errno)); + exit(1); + } else if(ret == 0) { + exit(0); } + /* Maybe I'd like to implement some protocol in this direction + * some day... */ + free(buf); } } @@ -181,6 +413,14 @@ int main(int argc, char **argv) { int fd; + if(argc < 2) { + fprintf(stderr, "usage: htparser ROOT [ARGS...]\n"); + exit(1); + } + if((plex = stdmkchild(argv + 1)) < 0) { + flog(LOG_ERR, "could not spawn root multiplexer: %s", strerror(errno)); + return(1); + } if((fd = listensock6(8080)) < 0) { flog(LOG_ERR, "could not listen on IPv6: %s", strerror(errno)); return(1); @@ -194,6 +434,7 @@ int main(int argc, char **argv) } else { mustart(listenloop, fd); } + mustart(plexwatch, plex); ioloop(); return(0); }