@(協議學習)[http/2]html
如何兼容http/1.xgit
如何儘可能避免冗餘github
支持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/2
Request,那麼也能夠根據請求中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
的一個請求。
TLS-ALPN該RFC定義了在TLS handshake階段的應用層協議協定。看起來是專門爲了HTTP/2
而制定(修改)。
這樣在TLS handshake過程就完成了協議的約定,而後若是是HTTP/2
就免去了HTTP/1.1
下的試探的過程,能夠直接收發HTTP/2
的消息幀。