golang` http.Request`的`Host`和`URL.Host`有什么区别? [英] What is the difference between `Host` and `URL.Host` for golang `http.Request`?
问题描述
在开发golang http应用程序时,我使用 http.Request
很多。当访问请求主机地址时,我会使用 req.Host
,但是我发现有 req.URL.Host
字段,但是当我打印它时,它是空的。
func处理程序(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
没有提供太多线索。
//对于服务器请求,Host指定寻找
// URL的主机。根据RFC 2616,这是
的值//主机头或URL本身给出的主机名。
//它可能是host:port的形式。对于国际域
//名称,主机可能采用Punycode或Unicode形式。如果
//需要,可以使用
// golang.org/x/net/idna将其转换为任一格式。
//
//对于客户端请求,主机可以选择覆盖主机
//头部发送。如果为空,则Request.Write方法使用
// URL.Host的值。主机可能包含国际
//域名。
主机字符串
在我看来,请求中有两个主机值:uri line和 Host
标题,如:
GET http:// localhost :8080 / HTTP / 1.1
Host:localhost:8080
但它并不能解决很多问题问题比它创造的要多:
- 为什么请求中有两个不同的
Host
字段?我的意思是这不是重复的? - 两个
Host
字段可以在相同请求中有所不同吗? - 哪一个应该用于什么情况?
回答一个真正的HTTP请求示例是最好的。
r.URL
通过解析HTTP请求URI来创建。 r .Host
字段是主机请求的值头。这与调用 r.Header.Get(Host)
。
相同。电汇是:
GET /pub/WWW/TheProject.html HTTP / 1.1
主机:www.example。 org:8080
然后 r.URL.Host
是, r.Host
是 www.example.org:8080
。
r.URL.Host
和 r.Host
的值几乎总是不同的。在代理服务器上, r.URL.Host
是目标服务器的主机,而 r.Host
是主机的代理服务器本身。当不通过代理连接时,客户端不会在请求URI中指定主机。在这种情况下, r.URL.Host
是空字符串。 如果您没有实现代理,那么你应该使用 r.Host
来确定主机。
When developing golang http application, I use http.Request
a lot. When accessing request host address, I would use req.Host
, but I find that there is req.URL.Host
field, but when I print it, it's empty.
func handler(w http.ResponseWriter, r *http.Request) {
fmt.Println("uri Host: " + r.URL.Host + " Scheme: " + r.URL.Scheme)
fmt.Println("Host: " + r.Host)
}
The documentation of http.Request gives the following comments, while net/url
does not give much clue.
// 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
It seems to me that there are two host value in a request: uri line and Host
header, like:
GET http://localhost:8080/ HTTP/1.1
Host: localhost:8080
But it does not solve many problems than it creates:
- Why are there two different
Host
field in request? I mean isn't this a duplicate? - Can the two
Host
fields be different in the same request? - Which one should I use for what situation?
Answers with a real HTTP request example would be the best. Thanks in advance.
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.
这篇关于golang` http.Request`的`Host`和`URL.Host`有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!