gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 15:06:26 25 4
gpt4 key购买 nike

据我了解,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 onedecompression 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 adynamic table (see Section 2.3.2) as a decoding context. No otherdynamic state is needed.

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


HPACK reduces the length of header field encoding by exploiting theredundancy inherent in protocols like HTTP. The ultimate goal ofthis is to reduce the amount of data that is required to send HTTPrequests 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com