+ 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;