]> Cypherpunks repositories - gostls13.git/commitdiff
net/http/httputil: make DumpRequest and DumpRequestOut recognize http.NoBody
authorBrad Fitzpatrick <bradfitz@golang.org>
Wed, 4 Jan 2017 21:23:00 +0000 (21:23 +0000)
committerBrad Fitzpatrick <bradfitz@golang.org>
Wed, 4 Jan 2017 23:02:08 +0000 (23:02 +0000)
Fixes #18506

Change-Id: I6b0b107296311178938609e878e1ef47a30a463f
Reviewed-on: https://go-review.googlesource.com/34814
Reviewed-by: Ian Lance Taylor <iant@golang.org>
src/net/http/httputil/dump.go
src/net/http/httputil/dump_test.go

index 15116816328693db674f02a85cd446188b91b720..7104c3745452ad42853fe00193231812263aa736 100644 (file)
@@ -18,11 +18,16 @@ import (
        "time"
 )
 
-// One of the copies, say from b to r2, could be avoided by using a more
-// elaborate trick where the other copy is made during Request/Response.Write.
-// This would complicate things too much, given that these functions are for
-// debugging only.
+// drainBody reads all of b to memory and then returns two equivalent
+// ReadClosers yielding the same bytes.
+//
+// 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) {
+       if b == http.NoBody {
+               // No copying needed. Preserve the magic sentinel meaning of NoBody.
+               return http.NoBody, http.NoBody, nil
+       }
        var buf bytes.Buffer
        if _, err = buf.ReadFrom(b); err != nil {
                return nil, b, err
index 2e980d39f8af924aa7daefcf38d29a8d58ffb04a..f881020fef7e57a341a4704d600bb7b79643ab86 100644 (file)
@@ -184,6 +184,18 @@ var dumpTests = []dumpTest{
                WantDump: "POST /v2/api/?login HTTP/1.1\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) {