HTTP/2前向兼容

HTTP/2如何向前兼容

@(协议学习)[http/2]

  1. 如何兼容http/1.x

  2. 如何尽量避免冗余

HTTP/2与HTTP/1.x兼容

1. ClearText over TCP

请求

支持HTTP/2协议的client会构造一个HTTP/1.x的请求,并在请求头中添加HTTP/2中需要的头信息:

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头域作用的说明。

  • Upgrade:h2c:Upgrade头提供了一个简单的机制在同一个链接上进行协议转换(从HTTP/1.1转换到其他协议)。h2c为一个HTTP/2版本标示,表示HTTP/2使用的明文(Cleartext)传输。

  • HTTP2-Settings:<>:

响应

HTTP/2响应
如果Server端支持HTTP/2,那么将返回101(Switching Protocols)响应以表对HTTP/2的支持。

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

[ HTTP/2 connection ...

以空行结束101响应之后,server端可以开始发送HTTP/2帧,发送的帧必须包含初始化upgrade请求的响应。

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-协议协商过程

    原文作者:HTTP
    原文地址: https://segmentfault.com/a/1190000002896785
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞