duandong2562 2019-08-02 00:22
浏览 201

是什么使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?

  • 写回答

1条回答 默认 最新

  • dongzhenshen7435 2019-08-15 00:59
    关注

    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:

    评论

报告相同问题?

悬赏问题

  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊