From 0cf818826c445703453cf6b9788831edd4ec6bd3 Mon Sep 17 00:00:00 2001 From: Chen Yufei Date: Sun, 28 Jul 2013 16:56:59 +0800 Subject: [PATCH] Fix bug for close connection response with no body. cow should add content-length: 0 header to indicate the end of close connection responses with no body, add chunked encoding for those has body. Safari will wait for body when cow incorrectly adds the chunked header. As Safari limits number of concurrent connections to the proxy, it will block on such erroneous responses. --- http.go | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/http.go b/http.go index 29935759..414d8714 100644 --- a/http.go +++ b/http.go @@ -602,8 +602,20 @@ func parseResponse(sv *serverConn, r *Request, rp *Response) (err error) { // Connection close, no content length specification // Use chunked encoding to pass content back to client if !rp.ConnectionKeepAlive && !rp.Chunking && rp.ContLen == -1 { - rp.raw.WriteString("Transfer-Encoding: chunked\r\n") + if rp.hasBody(r.Method) { + debug.Println("add chunked encoding to close connection response", r, rp) + rp.raw.WriteString("Transfer-Encoding: chunked\r\n") + } else { + debug.Println("add content-length 0 to close connection response", r, rp) + rp.raw.WriteString("Content-Length: 0\r\n") + } } + // Check for invalid response + if !rp.hasBody(r.Method) && (rp.Chunking || rp.ContLen != -1) { + errl.Printf("response has no body, but with chunked/content-length set\n%s", + rp.Verbose()) + } + // Whether COW should respond with keep-alive depends on client request, // not server response. if r.ConnectionKeepAlive {