Skip to content

Commit 4ded58b

Browse files
harshavardhanabradfitz
authored andcommitted
net/http/httputil: Keep response headers when response ContentLength is 0.
Current code does not print any response headers from httputil.DumpResponse(). PUT /miniocloud/new-file HTTP/1.1 Host: s3.amazonaws.com User-Agent: Go-http-client/1.1 Content-Length: 11 Accept-Encoding: gzip HTTP/1.1 200 OK With this fix we get an appropriate output for httputil.DumpResponse(). PUT /miniocloud/new-file HTTP/1.1 Host: s3.amazonaws.com User-Agent: Go-http-client/1.1 Content-Length: 11 Accept-Encoding: gzip HTTP/1.1 200 OK Content-Length: 0 Date: Thu, 14 Jan 2016 03:04:42 GMT Etag: "3e25960a79dbc69b674cd4ec67a72c62" Server: AmazonS3 X-Amz-Id-2: qnXyH6sknlovV0Myy3emFAXTNtI/sQIcu1ZXNq/6wd17K32tQ7WNGB1qb3nzCpW2DhfeZ/MbWfw= X-Amz-Request-Id: 8422EACB0CC492BD Fixes #13942 Change-Id: Ida063cc3524a96170d8a837893f7c9f49b6cf98e Reviewed-on: https://go-review.googlesource.com/18624 Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org> Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org>
1 parent 58ec583 commit 4ded58b

File tree

2 files changed

+34
-6
lines changed

2 files changed

+34
-6
lines changed

src/net/http/httputil/dump.go

+13-6
Original file line numberDiff line numberDiff line change
@@ -264,19 +264,20 @@ func DumpRequest(req *http.Request, body bool) (dump []byte, err error) {
264264
return
265265
}
266266

267-
// errNoBody is a sentinel error value used by failureToReadBody so we can detect
268-
// that the lack of body was intentional.
267+
// errNoBody is a sentinel error value used by failureToReadBody so we
268+
// can detect that the lack of body was intentional.
269269
var errNoBody = errors.New("sentinel error value")
270270

271271
// failureToReadBody is a io.ReadCloser that just returns errNoBody on
272-
// Read. It's swapped in when we don't actually want to consume the
273-
// body, but need a non-nil one, and want to distinguish the error
274-
// from reading the dummy body.
272+
// Read. It's swapped in when we don't actually want to consume
273+
// the body, but need a non-nil one, and want to distinguish the
274+
// error from reading the dummy body.
275275
type failureToReadBody struct{}
276276

277277
func (failureToReadBody) Read([]byte) (int, error) { return 0, errNoBody }
278278
func (failureToReadBody) Close() error { return nil }
279279

280+
// emptyBody is an instance of empty reader.
280281
var emptyBody = ioutil.NopCloser(strings.NewReader(""))
281282

282283
// DumpResponse is like DumpRequest but dumps a response.
@@ -286,7 +287,13 @@ func DumpResponse(resp *http.Response, body bool) (dump []byte, err error) {
286287
savecl := resp.ContentLength
287288

288289
if !body {
289-
resp.Body = failureToReadBody{}
290+
// For content length of zero. Make sure the body is an empty
291+
// reader, instead of returning error through failureToReadBody{}.
292+
if resp.ContentLength == 0 {
293+
resp.Body = emptyBody
294+
} else {
295+
resp.Body = failureToReadBody{}
296+
}
290297
} else if resp.Body == nil {
291298
resp.Body = emptyBody
292299
} else {

src/net/http/httputil/dump_test.go

+21
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,27 @@ Transfer-Encoding: chunked
288288
foo
289289
0`,
290290
},
291+
{
292+
res: &http.Response{
293+
Status: "200 OK",
294+
StatusCode: 200,
295+
Proto: "HTTP/1.1",
296+
ProtoMajor: 1,
297+
ProtoMinor: 1,
298+
ContentLength: 0,
299+
Header: http.Header{
300+
// To verify if headers are not filtered out.
301+
"Foo1": []string{"Bar1"},
302+
"Foo2": []string{"Bar2"},
303+
},
304+
Body: nil,
305+
},
306+
body: false, // to verify we see 0, not empty.
307+
want: `HTTP/1.1 200 OK
308+
Foo1: Bar1
309+
Foo2: Bar2
310+
Content-Length: 0`,
311+
},
291312
}
292313

293314
func TestDumpResponse(t *testing.T) {

0 commit comments

Comments
 (0)