mirror of
https://github.com/golang/go
synced 2024-11-22 04:24:39 -07:00
net/http/httputil: set HOST header always
According to RFC2616, all request messages must have HOST header. > A client MUST include a Host header field in all HTTP/1.1 request messages . https://www.rfc-editor.org/rfc/rfc2616#section-14.23
This commit is contained in:
parent
a343f4017b
commit
6909bca6b1
@ -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"
|
||||
|
@ -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)
|
||||
}
|
||||
|
@ -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() {
|
||||
|
Loading…
Reference in New Issue
Block a user