为什么说HTTP2是二进制协议? [英] Why is it said that HTTP2 is a binary protocol?

查看:1105
本文介绍了为什么说HTTP2是二进制协议?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚读了一篇有关 http1 http2 之间的区别的文章,但是我的主要问题是当它说 http2是二进制文件时协议,但 http1是文本协议.

I've just read a article about differences between http1 and http2, but the main question that I have is when it says that http2 is a binary protocol but http1 is a textual protocol.

也许我是错的,但是我知道任何数据,文本或任何可能的格式在内存中都具有二进制表示形式,即使通过TCP/IP网络进行传输,数据也会按照层的格式进行拆分(OSI模型或TCP/IP模型表示形式),这意味着技术上文本格式在通过网络进行数据传输的上下文中不存在.

Maybe I'm wrong but I know that any data is, text or whatever format it can be has a binary representation form in memory, and even when transfer through TCP/IP network the data is splitted in a format according the layer(OSI model or TCP/IP model representation) which means that technically textual format doesn't exist in the context of data transfer through network.

所以我无法真正理解http2和http1之间的区别,请您帮我更好的解释吗?

So I cannot really understand this different between http2 and http1, can you help me please with a better explanation?

推荐答案

二进制可能是一个令人困惑的术语-在计算机的某个点上,一切最终都是二进制的!

Binary is probably a confusing term - everything is ultimately binary at some point in computers!

HTTP/2具有高度结构化的格式,其中HTTP消息被格式化为数据包(称为帧),并且每个帧都分配给一个流. HTTP/2帧具有特定格式,包括在以下位置声明的长度:每个帧的开始以及帧头中的其他各个字段.在许多方面,它就像一个TCP数据包.读取HTTP/2帧可以遵循已定义的过程(前24位是该数据包的长度,其后是8位,用于定义帧类型...等).帧头到达有效负载后(例如HTTP头或主体有效负载),它们也将采用预先已知的特定格式.可以在一个或多个帧中发送HTTP/2消息.

HTTP/2 has a highly structured format where HTTP messages are formatted into packets (called frames) and where each frame is assigned to a stream. HTTP/2 frames have a specific format, including a length which is declared at the beginning of each frame and various other fields in the frame header. In many ways it’s like a TCP packet. Reading an HTTP/2 frame can follow a defined process (the first 24 bits are the length of this packet, followed by 8 bits which define the frame type... etc.). After the frame header comes the payload (e.g. HTTP Headers, or the Body payload) and these will also be in a specific format that is known in advance. An HTTP/2 message can be sent in one or more frames.

相比之下,HTTP/1.1是一种非结构化格式,由ASCII编码的文本行组成-因此,是的,它最终以二进制形式传输,但是它基本上是字符流,而不是专门分成单独的片段/帧(其他比线).通过一次读取一个字符来解析HTTP/1.1消息(或至少第一个HTTP Request/Response行和HTTP标头),直到到达新的行字符为止.这有点麻烦,因为您事先不知道每行多长时间,因此您必须逐个字符地对其进行处理.在HTTP/1.1中,HTTP正文的长度处理方式略有不同,这通常是事先已知的,因为内容长度HTTP标头将对此进行定义. HTTP/1.1消息必须作为一个连续的数据流完整地发送,并且该连接只能用于传输该消息,直到完成为止.

By contrast HTTP/1.1 is an unstructured format made up of lines of text in ASCII encoding - so yes this is transmitted as binary ultimately, but it’s basically a stream of characters rather than being specifically broken into separate pieces/frames (other than lines). HTTP/1.1 messages (or at least the first HTTP Request/Response line and HTTP Headers) are parsed by reading in characters one at a time, until a new line character is reached. This is kind of messy as you don’t know in advance how long each line is so you must process it character by character. In HTTP/1.1 the HTTP Body’s length is handled slightly different as typically is known in advance as a content-length HTTP header will define this. An HTTP/1.1 message must be sent in its entirety as one continuous stream of data and the connection can not be used for anything else but transmitting that message until it is completed.

HTTP/2带来的优点是,通过将消息打包到特定的帧中,我们可以将消息混合在一起:这是请求1的一部分,这里是请求2的一部分,这里是请求1的更多...等等.在HTTP/1.1中,这是不可能的,因为HTTP消息没有包装到带有ID的数据包/帧中,该ID属于该请求属于哪个请求.

The advantage that HTTP/2 brings is that, by packaging messages into specific frames we can intermingle the messages: here’s a bit of request 1, here’s a bit of request 2, here’s some more of request 1... etc. In HTTP/1.1 this is not possible as the HTTP message is not wrapped into packets/frames tagged with an id as to which request this belongs to.

我有一个图

I’ve a diagram here and an animated version here that help conceptualise this better.

这篇关于为什么说HTTP2是二进制协议?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆