是什么使http.server使用HTTP / 2?

I have made two Go programs, each of them tries to send a request to each other over TLS. In other words, two programs act as both client and server. However, first client host tries to connect to the server host using HTTP/1.1, whereas server host listens using HTTP/2, which leads to an error.

Program A

client := &http.Client{
    Transport: &http.Transport{
        TLSClientConfig: tlsConfig,
    },
}
mux := httprouter.New()
mux.GET("/test", test)
serverTLS := &http.Server{
    Addr:      "127.0.0.1:8081",
    Handler:   mux,
    TLSConfig: tlsConfig,
}

go func() {
    serverTLS.ListenAndServeTLS("", "")
}()

fmt.Println("test")
r, err := client.Get("https://127.0.0.1:8080/test")
if err != nil {
    log.Fatalln("Failed to connect testService over TLS : ", err)
}
defer r.Body.Close()
b, _ := ioutil.ReadAll(r.Body)
fmt.Println(string(b))

time.Sleep(time.Second * 15)

Program B

client := &http.Client{
    Transport: &http.Transport{
        TLSClientConfig: tlsConfig,
    },
}

mux := httprouter.New()
mux.GET("/test", test)
serverTLS := &http.Server{
    Addr:      "127.0.0.1:8080",
    Handler:   mux,
    TLSConfig: tlsConfig,
}

go func() {
    serverTLS.ListenAndServeTLS("", "")
}()

time.Sleep(time.Second * 10)

r, err := client.Get("https://127.0.0.1:8081/test")
if err != nil {
    log.Fatalln("Failed to connect testService over TLS : ", err)
}
defer r.Body.Close()
b, _ := ioutil.ReadAll(r.Body)
fmt.Println(string(b))

error message from program B (server)

http2: server: error reading preface from client 127.0.0.1:34854: bogus greeting "GET /test HTTP/1.1
Host"

Then, to specify bugs, I made two programs shorter by limiting their jobs to either of client or server.

Program A' (Server)

//setup server
mux := httprouter.New()
mux.GET("/test", test)
serverTLS := &http.Server{
    Addr:      "127.0.0.1:8080",
    Handler:   mux,
    TLSConfig: tlsConfig,
}

go func() {
    serverTLS.ListenAndServeTLS("", "")
}()

time.Sleep(time.Second * 10)

Program B' (Client)

//setup client
client := &http.Client{
    Transport: &http.Transport{
        TLSClientConfig: tlsConfig,
    },
}

fmt.Println("test")
r, err := client.Get("https://127.0.0.1:8080/test")
if err != nil {
    log.Fatalln("Failed to connect testService over TLS : ", err)
}
defer r.Body.Close()
b, _ := ioutil.ReadAll(r.Body)
fmt.Println(string(b))

This time, they work without an error. The communication between two hosts are done by HTTP/1.1. Could someone explain what changed the situation? What did force the Program B to use HTTP/2?

dourenzhou8740
dourenzhou8740 您未在此处列出的tlsConfig可能是问题的一部分。客户端可能通过ALPN宣布要说HTTP/2,而实际上却不这样做。除此之外,该代码看起来没有任何问题。
大约一年之前 回复

1个回答



根据 net / http :</ p>


从Go 1.6开始,使用HTTPS时,http包对HTTP / 2协议具有透明支持。 必须禁用HTTP / 2的程序可以通过将Transport.TLSNextProto(对于客户端)或Server.TLSNextProto(对于服务器)设置为非空的空映射来实现。 或者,当前支持以下GODEBUG环境变量:</ p>
</ blockquote>
</ div>

展开原文

原文

As per net/http:

Starting with Go 1.6, the http package has transparent support for the HTTP/2 protocol when using HTTPS. Programs that must disable HTTP/2 can do so by setting Transport.TLSNextProto (for clients) or Server.TLSNextProto (for servers) to a non-nil, empty map. Alternatively, the following GODEBUG environment variables are currently supported:

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐