英文:
Why is the address of http.Request.URL.Host same for 2 different http.Request structures?
问题
这段代码是一个自包含的示例,用于尝试复制一个错误。当运行此程序时,&request.URL.Host
和&request1.URL.Host
的地址是相同的。为什么会这样?根据我的理解,这是两个不同的结构,所以URL.Host
不应该有相同的地址。
package main
import (
"crypto/tls"
"fmt"
"net/http"
"net/url"
)
func main() {
hostname := "www.google.com"
uri, err := url.Parse("http://www.google.com/")
if err != nil {
panic(err)
}
var tlsConfig *tls.Config
tlsConfig = &tls.Config{
ServerName: hostname,
InsecureSkipVerify: true,
}
client := &http.Client{
Transport: &http.Transport{
DisableKeepAlives: true,
TLSClientConfig: tlsConfig,
},
}
request1 := &http.Request{
Header: http.Header{"User-Agent": {"Foo"}},
Host: hostname,
Method: "GET",
URL: uri,
}
request2 := &http.Request{
Header: http.Header{"User-Agent": {"Foo"}},
Host: hostname,
Method: "GET",
URL: uri,
}
fmt.Printf("Address1: %s, Address2: %s\n", &request1.URL.Host, &request2.URL.Host)
resp, err := client.Do(request1)
if err != nil {
panic(err)
}
defer resp.Body.Close()
fmt.Printf("\nResponse: %s", resp)
}
希望这可以帮助你解决问题。
英文:
This code is a self-contained example from a large code base to try to replicate a bug. When this program is run, the address of both &request.URL.Host
and &request1.URL.Host
is same. Why? From my understanding, these are 2 different structures so URL.Host
should not have the same address.
package main
import (
"crypto/tls"
"fmt"
"net/http"
"net/url"
)
func main() {
hostname := "www.google.com"
uri, err := url.Parse("http://www.google.com/")
if err != nil {
panic(err)
}
var tlsConfig *tls.Config
tlsConfig = &tls.Config{
ServerName: hostname,
InsecureSkipVerify: true,
}
client := &http.Client{
Transport: &http.Transport{
DisableKeepAlives: true,
TLSClientConfig: tlsConfig,
},
}
request1 := &http.Request{
Header: http.Header{"User-Agent": {"Foo"}},
Host: hostname,
Method: "GET",
URL: uri,
}
request2 := &http.Request{
Header: http.Header{"User-Agent": {"Foo"}},
Host: hostname,
Method: "GET",
URL: uri,
}
fmt.Printf("Address1: %s, Address2: %s\n", &request1.URL.Host, &request2.URL.Host)
resp, err := client.Do(request1)
if err != nil {
panic(err)
}
defer resp.Body.Close()
fmt.Printf("\nResponse: %s", resp)
}
答案1
得分: 1
http.Request
是一个结构体,其中的 URL
字段是一个指针:
URL *url.URL
在你的代码中,你有一个名为 uri
的变量,它持有一个 *url.URL
类型的指针。
然后你创建了两个请求,将这些指针存储在 request1
和 request2
变量中,但是你给它们的 URL
字段赋予了相同的值,即相同的指针。
因此,存在一个单独的 url.URL
值,并且你将其地址分别赋给了 request1.URL
和 request2.URL
。然后你打印了 request1.URL.Host
和 request2.URL.Host
的地址,但是由于 request1.URL
和 request2.URL
都指向同一个 url.URL
(结构体)值,所以该结构体的 Host
字段的地址将是相同的。对于这两个请求结构体来说,并不存在不同的 url.URL
值。
英文:
http.Request
is a struct, whose URL
field is a pointer:
URL *url.URL
In your code you have a single uri
variable holding a pointer of type *url.URL
.
Then you create 2 requests, storing the pointers in request1
and request2
variables, but you assign the same value, the same pointer to their URL
field.
So there is a single url.URL
value, and you assign its address to both request1.URL
and request2.URL
. Then you print the addresses of request1.URL.Host
and request2.URL.Host
, but since both request1.URL
and request2.URL
point to the same and only url.URL
(struct) value, the address of the Host
field of that struct will be the same. There are no distinct url.URL
values for the 2 request structs.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论