]> Cypherpunks repositories - gostls13.git/commitdiff
net/http/httputil: DumpRequest dumps Content-Length if set in header
authorEmmanuel Odeke <emm.odeke@gmail.com>
Sat, 9 Apr 2016 07:34:18 +0000 (00:34 -0700)
committerBrad Fitzpatrick <bradfitz@golang.org>
Sat, 9 Apr 2016 23:36:28 +0000 (23:36 +0000)
Fixes #7215

Change-Id: I108171ef18cac66d0dc11ce3553c26fc49529807
Reviewed-on: https://go-review.googlesource.com/21790
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>

src/net/http/httputil/dump.go
src/net/http/httputil/dump_test.go
src/net/http/httputil/example_test.go

index ddde11a0e403c60be090be382b0764a5abb73d54..692ab62c9b718f26dc8847744c5355294610978f 100644 (file)
@@ -163,7 +163,6 @@ func valueOrDefault(value, def string) string {
 
 var reqWriteExcludeHeaderDump = map[string]bool{
        "Host":              true, // not in Header map anyway
-       "Content-Length":    true,
        "Transfer-Encoding": true,
        "Trailer":           true,
 }
index fc884347a6e945d592c04577332123b7539499a3..2e980d39f8af924aa7daefcf38d29a8d58ffb04a 100644 (file)
@@ -122,6 +122,10 @@ var dumpTests = []dumpTest{
                                Host:   "post.tld",
                                Path:   "/",
                        },
+                       Header: http.Header{
+                               "Content-Length": []string{"8193"},
+                       },
+
                        ContentLength: 8193,
                        ProtoMajor:    1,
                        ProtoMinor:    1,
@@ -135,6 +139,10 @@ var dumpTests = []dumpTest{
                        "Content-Length: 8193\r\n" +
                        "Accept-Encoding: gzip\r\n\r\n" +
                        strings.Repeat("a", 8193),
+               WantDump: "POST / HTTP/1.1\r\n" +
+                       "Host: post.tld\r\n" +
+                       "Content-Length: 8193\r\n\r\n" +
+                       strings.Repeat("a", 8193),
        },
 
        {
@@ -144,6 +152,38 @@ var dumpTests = []dumpTest{
                WantDump: "GET http://foo.com/ HTTP/1.1\r\n" +
                        "User-Agent: blah\r\n\r\n",
        },
+
+       // Issue #7215. DumpRequest should return the "Content-Length" when set
+       {
+               Req: *mustReadRequest("POST /v2/api/?login HTTP/1.1\r\n" +
+                       "Host: passport.myhost.com\r\n" +
+                       "Content-Length: 3\r\n" +
+                       "\r\nkey1=name1&key2=name2"),
+               WantDump: "POST /v2/api/?login HTTP/1.1\r\n" +
+                       "Host: passport.myhost.com\r\n" +
+                       "Content-Length: 3\r\n" +
+                       "\r\nkey",
+       },
+
+       // Issue #7215. DumpRequest should return the "Content-Length" in ReadRequest
+       {
+               Req: *mustReadRequest("POST /v2/api/?login HTTP/1.1\r\n" +
+                       "Host: passport.myhost.com\r\n" +
+                       "Content-Length: 0\r\n" +
+                       "\r\nkey1=name1&key2=name2"),
+               WantDump: "POST /v2/api/?login HTTP/1.1\r\n" +
+                       "Host: passport.myhost.com\r\n" +
+                       "Content-Length: 0\r\n\r\n",
+       },
+
+       // Issue #7215. DumpRequest should not return the "Content-Length" if unset
+       {
+               Req: *mustReadRequest("POST /v2/api/?login HTTP/1.1\r\n" +
+                       "Host: passport.myhost.com\r\n" +
+                       "\r\nkey1=name1&key2=name2"),
+               WantDump: "POST /v2/api/?login HTTP/1.1\r\n" +
+                       "Host: passport.myhost.com\r\n\r\n",
+       },
 }
 
 func TestDumpRequest(t *testing.T) {
index f85613574298154f39e272d4a6c5fbb9cb576af3..619160367453ad9180857b4438536863ae7d7acf 100644 (file)
@@ -47,7 +47,7 @@ func ExampleDumpRequest() {
        fmt.Printf("%s", b)
 
        // Output:
-       // "POST / HTTP/1.1\r\nHost: www.example.org\r\nAccept-Encoding: gzip\r\nUser-Agent: Go-http-client/1.1\r\n\r\nGo is a general-purpose language designed with systems programming in mind."
+       // "POST / HTTP/1.1\r\nHost: www.example.org\r\nAccept-Encoding: gzip\r\nContent-Length: 75\r\nUser-Agent: Go-http-client/1.1\r\n\r\nGo is a general-purpose language designed with systems programming in mind."
 }
 
 func ExampleDumpRequestOut() {