HTTP1.X、HTTP/2、websocket、http.Hijacker golang

豆瓣奶茶 · · 1200 次点击 · · 开始浏览    
这是一个创建于 的文章,其中的信息可能已经有所发展或是发生改变。

  • 从Go 1.6开始,net/http下提供的Server在调用ListenAndServeTLS函数启动https服务的情况下会自动支持HTTP/2。其会根据与客户端TLS握手阶段的ALPN扩展判断客户端是否支持HTTP/2(h2),若支持,在TLS握手结束后会直接使用HTTP/2进行通讯。

  • 若需要使用HTTPS但不想开启HTTP/2可以有以下两种方法:

    • 初始化Server结构体时,将TLSNextProto字段置为一个非nil的空map

      // 栗子
      server := http.Server{
          Addr:         ":8080",
          TLSNextProto: make(map[string]func(*http.Server, *tls.Conn, http.Handler)),
      }
      log.Fatal(server.ListenAndServeTLS("./ssl/ca.crt", "./ssl/ca.key"))
      
    • 使用GODEBUG环境变量

      GODEBUG=http2client=0  # disable HTTP/2 client support
      GODEBUG=http2server=0  # disable HTTP/2 server support
      GODEBUG=http2debug=1   # enable verbose HTTP/2 debug logs
      GODEBUG=http2debug=2   # ... even more verbose, with frame dumps
      
      

  • 也就是说默认提供的http.Server仅在启用https时才会支持HTTP/2,也就是只支持h2模式。若要使用h2c模式,需要使用golang.org/x/net/http2中提供的API。(P.S:目前大多数浏览器仅支持h2模式)

  • WebSocket和HTTP/2不兼容,如果想让WebSocket跑在TLS上,需要用上面的方法禁用HTTP/2

    • 原因分析:

      • 在HTTP1.X中,一个请求和回复对应在一个tcp连接上,在websocket握手结束后,该tcp链接升级为websocket协议。而在HTTP/2中,多个请求和回复会复用一个tcp链接,无法实现上述的过程。

      • 对应在Go的代码上,以github.com/gorilla/websocket的WebSocket实现为例。其会在握手阶段将http.ResponseWriter断言为http.Hijacker接口并调用其中的Hijack()方法,拿到原始tcp链接对象并进行接管。而在使用HTTP/2时,http.ResponseWriter无法断言为http.Hijacker

        github.com/gorilla/websocket

        h, ok := w.(http.Hijacker)
        if !ok {
          return u.returnError(w, r, http.StatusInternalServerError, "websocket: response does not implement http.Hijacker")
        }
        var brw *bufio.ReadWriter
        netConn, brw, err = h.Hijack()
        if err != nil {
          return u.returnError(w, r, http.StatusInternalServerError, err.Error())
        }
        
        

        http.Hijacker

        // The Hijacker interface is implemented by ResponseWriters that allow
        // an HTTP handler to take over the connection.
        //
        // The default ResponseWriter for HTTP/1.x connections supports
        // Hijacker, but HTTP/2 connections intentionally do not.
        // ResponseWriter wrappers may also not support Hijacker. Handlers
        // should always test for this ability at runtime.
        type Hijacker interface {
          // Hijack lets the caller take over the connection.
          // After a call to Hijack the HTTP server library
          // will not do anything else with the connection.
          //
          // It becomes the caller's responsibility to manage
          // and close the connection.
          //
          // The returned net.Conn may have read or write deadlines
          // already set, depending on the configuration of the
          // Server. It is the caller's responsibility to set
          // or clear those deadlines as needed.
          //
          // The returned bufio.Reader may contain unprocessed buffered
          // data from the client.
          //
          // After a call to Hijack, the original Request.Body should
          // not be used.
          Hijack() (net.Conn, *bufio.ReadWriter, error)
        }
        
        
    • 有一个WebSocket over HTTP/2的草案,不过并没有什么用,看样子这问题暂时无解。

  • 关于HTTP/2的Server Push,其主要用途是提前推送web资源以减少延时。无法像WebSocket一样作为实时的消息推送手段


    image.png

有疑问加站长微信联系(非本文作者)

本文来自:简书

感谢作者:豆瓣奶茶

查看原文:HTTP1.X、HTTP/2、websocket、http.Hijacker golang

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

1200 次点击  
加入收藏 微博
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传