mirror of
https://github.com/golang/go
synced 2024-11-18 02:54:47 -07:00
net/http/httputil: make DumpRequest use Request.RequestURI when available
Fixes #10912 Change-Id: If04e3205d5cc43ebfd6864bc59340c8697cbc0af Reviewed-on: https://go-review.googlesource.com/17592 Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Ian Lance Taylor <iant@golang.org>
This commit is contained in:
parent
4280ed84fd
commit
9b1068ad2f
@ -197,15 +197,29 @@ func DumpRequest(req *http.Request, body bool) (dump []byte, err error) {
|
|||||||
|
|
||||||
var b bytes.Buffer
|
var b bytes.Buffer
|
||||||
|
|
||||||
fmt.Fprintf(&b, "%s %s HTTP/%d.%d\r\n", valueOrDefault(req.Method, "GET"),
|
// By default, print out the unmodified req.RequestURI, which
|
||||||
req.URL.RequestURI(), req.ProtoMajor, req.ProtoMinor)
|
// is always set for incoming server requests. But because we
|
||||||
|
// previously used req.URL.RequestURI and the docs weren't
|
||||||
host := req.Host
|
// always so clear about when to use DumpRequest vs
|
||||||
if host == "" && req.URL != nil {
|
// DumpRequestOut, fall back to the old way if the caller
|
||||||
host = req.URL.Host
|
// provides a non-server Request.
|
||||||
|
reqURI := req.RequestURI
|
||||||
|
if reqURI == "" {
|
||||||
|
reqURI = req.URL.RequestURI()
|
||||||
}
|
}
|
||||||
if host != "" {
|
|
||||||
fmt.Fprintf(&b, "Host: %s\r\n", host)
|
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)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
chunked := len(req.TransferEncoding) > 0 && req.TransferEncoding[0] == "chunked"
|
chunked := len(req.TransferEncoding) > 0 && req.TransferEncoding[0] == "chunked"
|
||||||
|
@ -5,6 +5,7 @@
|
|||||||
package httputil
|
package httputil
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bufio"
|
||||||
"bytes"
|
"bytes"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
@ -135,6 +136,14 @@ var dumpTests = []dumpTest{
|
|||||||
"Accept-Encoding: gzip\r\n\r\n" +
|
"Accept-Encoding: gzip\r\n\r\n" +
|
||||||
strings.Repeat("a", 8193),
|
strings.Repeat("a", 8193),
|
||||||
},
|
},
|
||||||
|
|
||||||
|
{
|
||||||
|
Req: *mustReadRequest("GET http://foo.com/ HTTP/1.1\r\n" +
|
||||||
|
"User-Agent: blah\r\n\r\n"),
|
||||||
|
NoBody: true,
|
||||||
|
WantDump: "GET http://foo.com/ HTTP/1.1\r\n" +
|
||||||
|
"User-Agent: blah\r\n\r\n",
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestDumpRequest(t *testing.T) {
|
func TestDumpRequest(t *testing.T) {
|
||||||
@ -211,6 +220,14 @@ func mustNewRequest(method, url string, body io.Reader) *http.Request {
|
|||||||
return req
|
return req
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func mustReadRequest(s string) *http.Request {
|
||||||
|
req, err := http.ReadRequest(bufio.NewReader(strings.NewReader(s)))
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
return req
|
||||||
|
}
|
||||||
|
|
||||||
var dumpResTests = []struct {
|
var dumpResTests = []struct {
|
||||||
res *http.Response
|
res *http.Response
|
||||||
body bool
|
body bool
|
||||||
|
Loading…
Reference in New Issue
Block a user