+static int canonreq(struct hthead *req)
+{
+ char *p, *p2, *r;
+ int n;
+
+ if(req->url[0] == '/') {
+ replrest(req, req->url + 1);
+ if((p = strchr(req->rest, '?')) != NULL)
+ *p = 0;
+ return(1);
+ }
+ if((p = strstr(req->url, "://")) != NULL) {
+ n = p - req->url;
+ if(((n == 4) && !strncasecmp(req->url, "http", 4)) ||
+ ((n == 5) && !strncasecmp(req->url, "https", 5))) {
+ if(getheader(req, "host"))
+ return(0);
+ p += 3;
+ if((p2 = strchr(p, '/')) == NULL) {
+ headappheader(req, "Host", p);
+ free(req->url);
+ req->url = sstrdup("/");
+ } else {
+ r = sstrdup(p2);
+ *(p2++) = 0;
+ headappheader(req, "Host", p);
+ free(req->url);
+ req->url = r;
+ }
+ replrest(req, req->url + 1);
+ if((p = strchr(req->rest, '?')) != NULL)
+ *p = 0;
+ return(1);
+ }
+ }
+ return(0);
+}
+
+static int http10keep(struct hthead *req, struct hthead *resp)
+{
+ int fc;
+
+ fc = hasheader(resp, "connection", "close");
+ headrmheader(resp, "connection");
+ if(!fc && hasheader(req, "connection", "keep-alive")) {
+ headappheader(resp, "Connection", "Keep-Alive");
+ return(1);
+ } else {
+ return(0);
+ }
+}
+
+static char *connid(void)
+{
+ static struct charbuf cur;
+ int i;
+ char *ret;
+
+ for(i = 0; i < cur.d; i++) {
+ if((++cur.b[i]) > 'Z')
+ cur.b[i] = 'A';
+ else
+ goto done;
+ }
+ bufadd(cur, 'A');
+done:
+ ret = memcpy(smalloc(cur.d + 1), cur.b, cur.d);
+ ret[cur.d] = 0;
+ return(ret);
+}
+
+static void passduplex(struct bufio *a, int afd, struct bufio *b, int bfd)
+{
+ struct selected pfd[4], sel;
+ struct bufio *sio;
+ int n, ev;
+
+ while(!bioeof(a) && !bioeof(b)) {
+ biocopybuf(b, a);
+ biocopybuf(a, b);
+ n = 0;
+ if(!a->eof) {
+ ev = 0;
+ if(biorspace(a))
+ ev |= EV_READ;
+ if(biowdata(a))
+ ev |= EV_WRITE;
+ if(ev)
+ pfd[n++] = (struct selected){.fd = afd, .ev = ev};
+ }
+ if(!b->eof) {
+ ev = 0;
+ if(!b->eof && biorspace(b))
+ ev |= EV_READ;
+ if(biowdata(b))
+ ev |= EV_WRITE;
+ if(ev)
+ pfd[n++] = (struct selected){.fd = bfd, .ev = ev};
+ }
+ sel = mblock(600, n, pfd);
+ if(sel.fd == afd)
+ sio = a;
+ else if(sel.fd == bfd)
+ sio = b;
+ else
+ break;
+ if((sel.ev & EV_READ) && (biofillsome(sio) < 0))
+ break;
+ if((sel.ev & EV_WRITE) && (bioflushsome(sio) < 0))
+ break;
+ }
+}
+
+void serve(struct bufio *in, int infd, struct conn *conn)