HTTP/2前向兼容

HTTP/2如何向前兼容

@(協議學習)[http/2]html

  1. 如何兼容http/1.xgit

  2. 如何儘可能避免冗餘github

HTTP/2與HTTP/1.x兼容

1. ClearText over TCP

請求

支持HTTP/2協議的client會構造一個HTTP/1.x的請求,並在請求頭中添加HTTP/2中須要的頭信息:segmentfault

GET / HTTP/1.1
Host: server.example.com
Connection: Upgrade, HTTP2-Settings
Upgrade: h2c
HTTP2-Settings: <base64url encoding of HTTP/2 SETTINGS payload>

關於HTTP/2相關頭域的說明:學習

  • Connection:Upgrade,HTTP2-Settings:見下對Connection頭域做用的說明。this

  • Upgrade:h2c:Upgrade頭提供了一個簡單的機制在同一個連接上進行協議轉換(從HTTP/1.1轉換到其餘協議)。h2c爲一個HTTP/2版本標示,表示HTTP/2使用的明文(Cleartext)傳輸。url

  • HTTP2-Settings:<>:code

響應

HTTP/2響應
若是Server端支持HTTP/2,那麼將返回101(Switching Protocols)響應以表對HTTP/2的支持。server

HTTP/1.1 101 Switching Protocols
Connection: Upgrade
Upgrade: h2c

[ HTTP/2 connection ...

以空行結束101響應以後,server端能夠開始發送HTTP/2幀,發送的幀必須包含初始化upgrade請求的響應。htm

HTTP/1.x升級至HTTP/2的請求都須要包含一個HTTP2-Settings頭。

HTTP/1.1響應

若是Server不支持HTTP/2Request,那麼也能夠根據請求中HTTP/1.1相關信息作出響應。返回的響應爲典型的HTTP/1.1響應

HTTP/1.1 200 OK
Content-Length: 243
Content-Type: text/html

...

關於Connection頭域的說明
14 Header Field Definitions14.10關於Connection頭的描述有這樣的說明:

HTTP/1.1 proxies MUST parse the Connection header field before a message is forwarded and, for each connection-token in this field, remove any header field(s) from the message with the same name as the connection-token. Connection options are signaled by the presence of a connection-token in the Connection header field, not by any corresponding additional header field(s), since the additional header field may not be sent if there are no parameters associated with that connection option.

Proxy不支持HTTP/2的時候,能夠根據Connection將HTTP/2的頭信息去除,避免了進一步傳輸的開銷,轉發到Server的時候已經變成HTTP/1.1的一個請求。

Over TLS

TLS-ALPN該RFC定義了在TLS handshake階段的應用層協議協定。看起來是專門爲了HTTP/2而制定(修改)。
這樣在TLS handshake過程就完成了協議的約定,而後若是是HTTP/2就免去了HTTP/1.1下的試探的過程,能夠直接收發HTTP/2的消息幀。

參考文獻:

  1. Hypertext Transfer Protocol Version 2 (HTTP/2)

  2. http2-協議協商過程

相關文章
相關標籤/搜索