4 enum { bufsize = 8*1024 };
5 char buf[bufsize+1], addr[128], *proto, *host, *port, *path;
13 /* read all the headers */
18 if((r = read(0, buf+n, bufsize-n)) <= 0)
22 } while(strstr(buf, "\r\n\r\n") == nil);
24 /* remove keep alive headers */
25 if(p = cistrstr(buf, "\nConnection:"))
26 if(e = strchr(p+1, '\n'))
28 if(p = cistrstr(buf, "\nProxy-Connection:"))
29 if(e = strchr(p+1, '\n'))
32 /* crack first line of http request */
33 if(e = strchr(buf, '\n'))
35 r = tokenize(buf, f, 3);
41 if(p = strstr(proto, "://")){
50 if(p = strchr(host, '/')){
56 if(p = strrchr(host, ']')){
58 if(p = strrchr(p, ':'))
61 } else if(p = strrchr(host, ':')){
66 snprint(addr, sizeof(addr), "tcp!%s!%s", host, port);
69 fd = dial(addr, 0, 0, 0);
72 con = cistrcmp(f[0], "CONNECT") == 0;
75 print("%s 500 Connection Failed\r\n\r\n%r\n", f[2]);
77 print("%s 200 Connection Established\r\n\r\n", f[2]);
82 switch(rfork(RFPROC|RFFDG|RFNOWAIT)){
91 print("%s /%s %s\r\nConnection: close\r\n%s", f[0], path, f[2], e);
94 while((r = read(0, buf, sizeof(buf))) > 0)
95 if(write(1, buf, r) != r)
98 postnote(PNGROUP, getpid(), "kill");