http - HTTP/2 是无状态协议(protocol)吗?

标签 http architecture protocols http2

据我了解,HTTP/2 带有一个称为 HPACK 的有状态 header 压缩。它不会改变 HTTP 协议(protocol)的无状态语义吗? Web 应用程序将 HTTP/2 视为无状态协议(protocol)是否安全?最后,HTTP/2 是否与现有的负载均衡器兼容?

最佳答案

HTTP/2 是无状态的。

原始的 HTTP 是一个无状态的协议(protocol),这意味着每个请求消息都可以被孤立地理解。这意味着每个请求都需要携带服务器为该请求提供服务所需的尽可能多的详细信息,而服务器不必存储来自先前请求的大量信息和元数据。

由于 HTTP/2 没有改变这种范式,它必须以同样的方式工作,无状态。

official RFCs可以清楚地看到以及。据称:

The Hypertext Transfer Protocol (HTTP) is an application-level protocol for distributed, collaborative, hypermedia information systems. It is a generic, stateless, protocol which can be used for many tasks...

HTTP/2的定义说:

This specification describes an optimized expression of the semantics of the Hypertext Transfer Protocol (HTTP), referred to as HTTP version 2 (HTTP/2)... This specification is an alternative to, but does not obsolete, the HTTP/1.1 message syntax. HTTP's existing semantics remain unchanged.

结论

HTTP/2 协议(protocol)在设计上是无状态的,因为与原始 HTTP 相比语义保持不变。


困惑可能从何而来

HTTP/2 连接是运行在 TCP 连接之上的应用层协议(protocol)(顺便说一句,没有什么能阻止你使用 HTTP over UDP,例如,这是可能的,但不使用 UDP,因为它不是“可靠的”运输”)。不要将它与 session 层和传输层混合。 HTTP 协议(protocol)在设计上是无状态的。 HTTP 通过加密的 SSL/TLS 连接,也没有改变此声明,因为 HTTPS 中的 S 与传输有关,而不是协议(protocol)本身。

HPACK , Header Compression for HTTP/2,是一种专为 HTTP/2 header 设计的压缩格式,它在 separate internet draft 中指定。 .它不会更改 HTTP/2 本身,因此不会更改语义。

RFC for HTTP/2在关于 HPACK 的部分中,他们声明:

Header compression is stateful. One compression context and one decompression context are used for the entire connection.

这就是为什么来自 HPACK's RFC :

2.2. Encoding and Decoding Contexts

To decompress header blocks, a decoder only needs to maintain a dynamic table (see Section 2.3.2) as a decoding context. No other dynamic state is needed.

When used for bidirectional communication, such as in HTTP, the encoding and decoding dynamic tables maintained by an endpoint are completely independent, i.e., the request and response dynamic tables are separate.


HPACK reduces the length of header field encoding by exploiting the redundancy inherent in protocols like HTTP. The ultimate goal of this is to reduce the amount of data that is required to send HTTP requests or responses.

HPACK 实现不可能是完全无状态的,因为完全独立的编码和解码表必须由端点维护。

同时,还有库试图解决 HPACK 问题,例如无状态事件驱动的 HPACK 编解码器 CASHPACK :

An HPACK implementation cannot be completely stateless, because a dynamic table needs to be maintained. Relying on the assumption that HTTP/2 will always decode complete HPACK sequences, statelessness is achieved using an event-driven API.

关于http - HTTP/2 是无状态协议(protocol)吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36178447/

相关文章:

swift - MKAnnotation协议(protocol)继承

swift - 在 Swift 中发布 Feed 数据建模

Perl - 创建多个监听不同端口的 HTTP 服务器

http - 在 onModuleLoad 中完成的请求根本不会被缓存。主要在 FireFox 中

Python项目架构

c# - 在 StructureMap 中使用 Registry 创建可插拔架构

windows - 获取Windows体系结构(32/64位版本)

java - Retrofit/OkHttp 删除 Transfer-Encoding header

java - 为什么 java 没有允许从流中读取 String 和 byte [] 的类?

javascript - 什么是hm ://protocol?