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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值