Skip to content

Instantly share code, notes, and snippets.

@hjzheng
Last active July 31, 2019 03:31
Show Gist options
  • Save hjzheng/33c9cb4330afccac2646e2f5937d3ee3 to your computer and use it in GitHub Desktop.
Save hjzheng/33c9cb4330afccac2646e2f5937d3ee3 to your computer and use it in GitHub Desktop.
http2 协议

HTTP/2 抓包

  • 使用 TLS/SSL 加密,都是随着连接生成有变化的(???)

     Mac 下
     $ export SSLKEYLOGFILE=/Users/username/sslkeylogs/output.log 
     $ open -a Google\ Chrome
    
     打开 wireshark
     Preferences -> Protocols -> ssl 或 tls -> Master-Srcret log filename
    
     亲测有效
     注意这个 /Users/username/sslkeylogs/output.log 要提前建立好
    

HTTP/2 协议

  • HTTP/2 协议既可以运行在 TCP 协议之上,也可以运行在 TLS/SSL 协议之上,但是浏览器要求必须运行在 TLS/SSL 之上

IETF 标准不要求必须基于TLS/SSL协议

  • 浏览器要求必须基于TLS/SSL协议
  • 在 TLS 层 ALPN (Application Layer Protocol Negotiation)扩展做协商,只认 HTTP/1.x 的代理服务器不会干扰 HTTP/2
  • shema:http://和 https:// 默认基于 80 和 443 端口
  • h2:基于 TLS 协议运行的 HTTP/2 被称为 h2
  • h2c:直接在 TCP 协议之上运行的 HTTP/2 被称为 h2c

在 TCP 上从 http/1 升级到 http/2

  • 类似于 websocket,发送 get 请求加入 Connection:upgrade,HTTP2-setting 要求升级协议,当服务器返回 101

在 TLS 上从 http/1 升级到 http/2

  • TLS 建立连接时进行协商,通过扩展字段 Application-Layer Protocol Negotiation Extension (client Hello, server Hello)

HTTP/2 核心概念

  • 连接 Connection: 1 个 TCP 连接,包含一个和多个 Stream(多路复用的关键)
  • 数据流 Stream: 一个双向通讯数据流,包含 1条或多条 Message
  • 消息 Message: 对应 HTTP/1 中的请求或者响应,包含一条或者多条 Frame
  • 数据帧 Frame: 最小单位,以二进制压缩格式存放 HTTP/1 中的内容

消息 Message 由 HEADER 帧 与 DATA 帧组成

同一个 Stream 必须是有序传输,跨 stream 可以无序

HTTP/2 帧格式

Stream ID 的作用

  • 实现多路复用的关键
    • 接收端的实现,可据此并发组装消息
    • 同一 stream 内部的 frame 必须是有序的
    • SETTINGS_MAX_CONCURRENT_STREAMS 控制着并发 stream 数
    • 推送依赖性请求的关键
      • 客户端建立的流必须是奇数
      • 服务器建立的流必须是偶数
    • 流状态管理的约束性规定
      • 新建立的流 ID 必须大于曾经建立过的状态为 opened 或者 reserved 的流 ID
      • 在新建立的流上发送帧时,意味着将更小 ID 且为 idle 状态的流置为 closed 状态
      • Stream ID 不能复用,长连接耗尽 ID 应创建新连接
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment