douba9425
2017-03-21 08:17
采纳率: 100%
浏览 982
已采纳

golang`http.Request`的`Host`和`URL.Host`之间有什么区别?

在开发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

但这并没有解决很多问题,它所带来的麻烦是:

  1. 为什么请求中有两个不同的 Host 字段? 我的意思是这不是重复的吗?
  2. 两个主机字段在同一个请求中可以不同吗?
  3. 在什么情况下应该使用哪一个?

要是带有真实HTTP请求示例的答案是最好的。 提前致谢!

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • douxiajia6720 2017-03-21 11:43
    已采纳

    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.

    打赏 评论
  • doumeng3345 2017-03-21 08:30

    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.

    打赏 评论

相关推荐 更多相似问题