From 2fde1c36329b4bbc585b7f9d9c56210d349f2958 Mon Sep 17 00:00:00 2001 From: Brad Fitzpatrick Date: Wed, 29 Jan 2014 11:31:52 +0100 Subject: [PATCH] httputil: make CloseBody more robust Change-Id: If46c60a5088f57ada45b8aa91ae29c8f512604c7 --- pkg/httputil/httputil.go | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/pkg/httputil/httputil.go b/pkg/httputil/httputil.go index fc31deeed..08c30c581 100644 --- a/pkg/httputil/httputil.go +++ b/pkg/httputil/httputil.go @@ -318,7 +318,15 @@ func CloseBody(rc io.ReadCloser) { // 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. - // TODO: use a bytepool package somewhere for these two bytes. - rc.Read(make([]byte, 2)) + // TODO: use a bytepool package somewhere for this byte? + // 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() }