diff --git a/src/net/http/httputil/dump.go b/src/net/http/httputil/dump.go index 7affe5e61a9..8a42023b2a9 100644 --- a/src/net/http/httputil/dump.go +++ b/src/net/http/httputil/dump.go @@ -243,15 +243,9 @@ func DumpRequest(req *http.Request, body bool) ([]byte, error) { fmt.Fprintf(&b, "%s %s HTTP/%d.%d\r\n", valueOrDefault(req.Method, "GET"), reqURI, req.ProtoMajor, req.ProtoMinor) - absRequestURI := strings.HasPrefix(req.RequestURI, "http://") || strings.HasPrefix(req.RequestURI, "https://") - if !absRequestURI { - host := req.Host - if host == "" && req.URL != nil { - host = req.URL.Host - } - if host != "" { - fmt.Fprintf(&b, "Host: %s\r\n", host) - } + _, ok := req.Header["Host"] + if ok { + fmt.Fprintf(&b, "Host: %s\r\n", req.Header.Get("Host")) } chunked := len(req.TransferEncoding) > 0 && req.TransferEncoding[0] == "chunked" diff --git a/src/net/http/httputil/dump_test.go b/src/net/http/httputil/dump_test.go index c20c054865f..42404b7187d 100644 --- a/src/net/http/httputil/dump_test.go +++ b/src/net/http/httputil/dump_test.go @@ -18,6 +18,7 @@ import ( "strings" "testing" "time" + _ "unsafe" ) type eofReader struct{} @@ -57,7 +58,6 @@ var dumpTests = []dumpTest{ Body: []byte("abcdef"), WantDump: "GET /search HTTP/1.1\r\n" + - "Host: www.google.com\r\n" + "Transfer-Encoding: chunked\r\n\r\n" + chunk("abcdef") + chunk(""), }, @@ -151,7 +151,6 @@ var dumpTests = []dumpTest{ "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), }, @@ -383,8 +382,11 @@ func mustNewRequest(method, url string, body io.Reader) *http.Request { return req } +//go:linkname readRequest net/http.readRequest +func readRequest(b *bufio.Reader) (*http.Request, error) + func mustReadRequest(s string) *http.Request { - req, err := http.ReadRequest(bufio.NewReader(strings.NewReader(s))) + req, err := readRequest(bufio.NewReader(strings.NewReader(s))) if err != nil { panic(err) } diff --git a/src/net/http/httputil/example_test.go b/src/net/http/httputil/example_test.go index 6c107f83900..9c065562f3f 100644 --- a/src/net/http/httputil/example_test.go +++ b/src/net/http/httputil/example_test.go @@ -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\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." + // "POST / HTTP/1.1\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() {