Commit 2815045a authored by Brad Fitzpatrick's avatar Brad Fitzpatrick

net/http/httputil: make DumpRequest and DumpRequestOut recognize http.NoBody

Fixes #18506

Change-Id: I6b0b107296311178938609e878e1ef47a30a463f
Reviewed-on: https://go-review.googlesource.com/34814Reviewed-by: 's avatarIan Lance Taylor <iant@golang.org>
parent ecac8275
...@@ -18,11 +18,16 @@ import ( ...@@ -18,11 +18,16 @@ import (
"time" "time"
) )
// One of the copies, say from b to r2, could be avoided by using a more // drainBody reads all of b to memory and then returns two equivalent
// elaborate trick where the other copy is made during Request/Response.Write. // ReadClosers yielding the same bytes.
// This would complicate things too much, given that these functions are for //
// debugging only. // It returns an error if the initial slurp of all bytes fails. It does not attempt
// to make the returned ReadClosers have identical error-matching behavior.
func drainBody(b io.ReadCloser) (r1, r2 io.ReadCloser, err error) { func drainBody(b io.ReadCloser) (r1, r2 io.ReadCloser, err error) {
if b == http.NoBody {
// No copying needed. Preserve the magic sentinel meaning of NoBody.
return http.NoBody, http.NoBody, nil
}
var buf bytes.Buffer var buf bytes.Buffer
if _, err = buf.ReadFrom(b); err != nil { if _, err = buf.ReadFrom(b); err != nil {
return nil, b, err return nil, b, err
......
...@@ -184,6 +184,18 @@ var dumpTests = []dumpTest{ ...@@ -184,6 +184,18 @@ var dumpTests = []dumpTest{
WantDump: "POST /v2/api/?login HTTP/1.1\r\n" + WantDump: "POST /v2/api/?login HTTP/1.1\r\n" +
"Host: passport.myhost.com\r\n\r\n", "Host: passport.myhost.com\r\n\r\n",
}, },
// Issue 18506: make drainBody recognize NoBody. Otherwise
// this was turning into a chunked request.
{
Req: *mustNewRequest("POST", "http://example.com/foo", http.NoBody),
WantDumpOut: "POST /foo HTTP/1.1\r\n" +
"Host: example.com\r\n" +
"User-Agent: Go-http-client/1.1\r\n" +
"Content-Length: 0\r\n" +
"Accept-Encoding: gzip\r\n\r\n",
},
} }
func TestDumpRequest(t *testing.T) { func TestDumpRequest(t *testing.T) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment