在开发golang http应用程序时,我使用了很多。 http.Request
当访问请求主机地址时,我会使用 req.Host
, 但我发现req.URL.Host
字段, 但是当我打印它时,它是空的。
func handler(w http.ResponseWriter, r *http.Request) {
fmt.Println("uri Host: " + r.URL.Host + " Scheme: " + r.URL.Scheme)
fmt.Println("Host: " + r.Host)
}
http.Request 的文档给出以下注释,而net/url
net/url没有给出太多提示。
// For server requests Host specifies the host on which the
// URL is sought. Per RFC 2616, this is either the value of
// the "Host" header or the host name given in the URL itself.
// It may be of the form "host:port". For international domain
// names, Host may be in Punycode or Unicode form. Use
// golang.org/x/net/idna to convert it to either format if
// needed.
//
// For client requests Host optionally overrides the Host
// header to send. If empty, the Request.Write method uses
// the value of URL.Host. Host may contain an international
// domain name.
Host string
在我看来,在一个请求中有两个主机值:uri行和 host header,就像:
GET http://localhost:8080/ HTTP/1.1
Host: localhost:8080
但这并没有解决很多问题,它所带来的麻烦是:
Host
字段? 我的意思是这不是重复的吗?要是带有真实HTTP请求示例的答案是最好的。 提前致谢!
The r.URL
field is created by parsing the HTTP request URI.
The r.Host
field is the value of the Host request header. It's the same value as calling r.Header.Get("Host")
.
If the HTTP request on the wire is:
GET /pub/WWW/TheProject.html HTTP/1.1
Host: www.example.org:8080
then r.URL.Host
is "" and r.Host
is www.example.org:8080
.
The value of r.URL.Host
and r.Host
are almost always different. On a proxy server, r.URL.Host
is the host of the target server and r.Host
is the host of the proxy server itself. When not connecting through a proxy, the client does not specify a host in the request URI. In this scenario, r.URL.Host
is the empty string.
If you are not implementing a proxy, then you should use r.Host
to determine the host.
Essentially http.Request.Host
is for convenience.
r.Host
is much easier to call than r.Header.Get("Host")
or r.URL.Host
.
Also to be noted that some routers strip the host from http.Request.URL
so http.Request.Host
is useful in those cases as well.
Hence it can be considered that req.Host provides the Host value even when the request header or url has been modified elsewhere.