httputil: make CloseBody more robust

Change-Id: If46c60a5088f57ada45b8aa91ae29c8f512604c7
This commit is contained in:
Brad Fitzpatrick 2014-01-29 11:31:52 +01:00
parent 7c3afd2711
commit 2fde1c3632
1 changed files with 10 additions and 2 deletions

View File

@ -318,7 +318,15 @@ func CloseBody(rc io.ReadCloser) {
// Content-Length. Or maybe Go 1.3's Close itself would look // Content-Length. Or maybe Go 1.3's Close itself would look
// to see if we're at EOF even if it hasn't been Read. // to see if we're at EOF even if it hasn't been Read.
// TODO: use a bytepool package somewhere for these two bytes. // TODO: use a bytepool package somewhere for this byte?
rc.Read(make([]byte, 2)) // Justification for 3 byte reads: two for up to "\r\n" after
// a JSON/XML document, and then 1 to see EOF if we haven't yet.
buf := make([]byte, 1)
for i := 0; i < 3; i++ {
_, err := rc.Read(buf)
if err != nil {
break
}
}
rc.Close() rc.Close()
} }