+void serve(struct bufio *in, int infd, struct conn *conn)
+{
+ int pfds[2];
+ struct bufio *out, *dout;
+ struct stdiofd *outi;
+ struct hthead *req, *resp;
+ char *hd, *id;
+ off_t dlen;
+ int keep, duplex;
+
+ id = connid();
+ out = NULL;
+ req = resp = NULL;
+ while(plex >= 0) {
+ bioflush(in);
+ if((req = parsereq(in)) == NULL)
+ break;
+ if(!canonreq(req))
+ break;
+
+ headappheader(req, "X-Ash-Connection-ID", id);
+ if((conn->initreq != NULL) && conn->initreq(conn, req))
+ break;
+
+ if((plex < 0) || 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 = mtbioopen(pfds[1], 1, 600, "r+", &outi);
+
+ if(getheader(req, "content-type") != NULL) {
+ if((hd = getheader(req, "content-length")) != NULL) {
+ dlen = atoo(hd);
+ if(dlen > 0) {
+ if(passdata(in, out, dlen) != dlen)
+ break;
+ }
+ } else if(((hd = getheader(req, "transfer-encoding")) != NULL) && !strcasecmp(hd, "chunked")) {
+ if(recvchunks(in, out))
+ break;
+ } else {
+ /* Ignore rather than abort, to be kinder to broken clients. */
+ headrmheader(req, "content-type");
+ }
+ }
+ if(bioflush(out))
+ break;
+ /* Make sure to send EOF */
+ shutdown(pfds[1], SHUT_WR);
+
+ if((resp = parseresponseb(out)) == NULL)
+ break;
+ replstr(&resp->ver, req->ver);
+
+ if(!getheader(resp, "server"))
+ headappheader(resp, "Server", sprintf3("ashd/%s", VERSION));
+ duplex = hasheader(resp, "x-ash-switch", "duplex");
+ trimx(resp);
+
+ if(duplex) {
+ if(outi->rights < 0)
+ break;
+ writerespb(in, resp);
+ bioprintf(in, "\r\n");
+ dout = mtbioopen(outi->rights, 1, 600, "r+", NULL);
+ passduplex(in, infd, dout, outi->rights);
+ outi->rights = -1;
+ bioclose(dout);
+ break;
+ } else if(!strcasecmp(req->ver, "HTTP/1.0")) {
+ if(!strcasecmp(req->method, "head")) {
+ keep = http10keep(req, resp);
+ writerespb(in, resp);
+ bioprintf(in, "\r\n");
+ } else if((hd = getheader(resp, "content-length")) != NULL) {
+ keep = http10keep(req, resp);
+ dlen = atoo(hd);
+ writerespb(in, resp);
+ bioprintf(in, "\r\n");
+ if(passdata(out, in, dlen) != dlen)
+ break;
+ } else {
+ headrmheader(resp, "connection");
+ writerespb(in, resp);
+ bioprintf(in, "\r\n");
+ passdata(out, in, -1);
+ break;
+ }
+ if(!keep)
+ break;
+ } else if(!strcasecmp(req->ver, "HTTP/1.1")) {
+ if(!strcasecmp(req->method, "head")) {
+ writerespb(in, resp);
+ bioprintf(in, "\r\n");
+ } else if((hd = getheader(resp, "content-length")) != NULL) {
+ writerespb(in, resp);
+ bioprintf(in, "\r\n");
+ dlen = atoo(hd);
+ if(passdata(out, in, dlen) != dlen)
+ break;
+ } else if(!getheader(resp, "transfer-encoding")) {
+ headappheader(resp, "Transfer-Encoding", "chunked");
+ writerespb(in, resp);
+ bioprintf(in, "\r\n");
+ if(passchunks(out, in))
+ break;
+ } else {
+ writerespb(in, resp);
+ bioprintf(in, "\r\n");
+ passdata(out, in, -1);
+ break;
+ }
+ if(hasheader(req, "connection", "close") || hasheader(resp, "connection", "close"))
+ break;
+ } else {
+ break;
+ }
+
+ bioclose(out);
+ out = NULL;
+ freehthead(req);
+ freehthead(resp);
+ req = resp = NULL;
+ }
+
+ if(out != NULL)
+ bioclose(out);
+ if(req != NULL)
+ freehthead(req);
+ if(resp != NULL)
+ freehthead(resp);
+ bioclose(in);
+ free(id);
+}
+
+static void plexwatch(struct muth *muth, va_list args)
+{
+ vavar(int, fd);
+ char *buf;
+ int i, s, ret;
+
+ s = 0;
+ while(1) {
+ if(block(fd, EV_READ, 0) == 0)
+ break;
+ buf = smalloc(65536);
+ ret = recv(fd, buf, 65536, 0);
+ if(ret < 0) {
+ flog(LOG_WARNING, "received error on rootplex read channel: %s", strerror(errno));
+ exit(1);
+ } else if(ret == 0) {
+ s = 1;
+ free(buf);
+ break;
+ }
+ /* Maybe I'd like to implement some protocol in this direction
+ * some day... */
+ free(buf);
+ }
+ shutdown(plex, SHUT_RDWR);
+ for(i = 0; i < listeners.d; i++) {
+ if(listeners.b[i] == muth)
+ bufdel(listeners, i);
+ }
+ if(s) {
+ flog(LOG_INFO, "root handler exited, so shutting down listening...");
+ while(listeners.d > 0)
+ resume(listeners.b[0], 0);
+ }
+}
+
+static void initroot(void *uu)