Http Chunked Transfer Coding

来源:互联网 发布:linux 通配符 编辑:程序博客网 时间:2024/05/17 09:13

分块传输编码 Chunked Transfer Coding

是超文本传输协议(HTTP)中的一种数据传输机制,允许HTTP由網頁伺服器发送给客户端应用( 通常是网页浏览器)的数据可以分成多个部分。分块传输编码只在HTTP协议1.1版本(HTTP/1.1)中提供。

通常,HTTP应答消息中发送的数据是整个发送的,Content-Length消息头字段表示数据的长度。数据的长度很重要,因为客户端需要知道哪里是应答消息的结束,以及后续应答消息的开始。然而,使用分块传输编码,数据分解成一系列数据块,并以一个或多个块发送,这样服务器可以发送数据而不需要预先知道发送内容的总大小。通常数据块的大小是一致的,但也不总是这种情况。

原理

HTTP 1.1引入分块传输编码提供了以下几点好处:


HTTP分块传输编码允许服务器为动态生成的内容维持HTTP持久链接。通常,持久链接需要服务器在开始发送消息体前发送Content-Length消息头字段,但是对于动态生成的内容来说,在内容创建完之前是不可知的。[1]
分块传输编码允许服务器在最后发送消息头字段。对于那些头字段值在内容被生成之前无法知道的情形非常重要,例如消息的内容要使用散列进行签名,散列的结果通过HTTP消息头字段进行传输。没有分块传输编码时,服务器必须缓冲内容直到完成后计算头字段的值并在发送内容前发送这些头字段的值。

HTTP服务器有时使用压缩 (gzip或deflate)以缩短传输花费的时间。分块传输编码可以用来分隔压缩对象的多个部分。在这种情况下,块不是分别压缩的,而是整个负载进行压缩,压缩的输出使用本文描述的方案进行分块传输。在压缩的情形中,分块编码有利于一边进行压缩一边发送数据,而不是先完成压缩过程以得知压缩后数据的大小。


格式

如果一个HTTP消息(请求消息或应答消息)的Transfer-Encoding消息头的值为chunked,那么,消息体由数量未定的块组成,并以最后一个大小为0的块为结束。

每一个非空的块都以该块包含数据的字节数(字节数以十六进制表示)开始,跟随一个CRLF (回车及換行),然后是数据本身,最后块CRLF结束。在一些实现中,块大小和CRLF之间填充有白空格(0x20)。

最后一块是单行,由块大小(0),一些可选的填充白空格,以及CRLF。最后一块不再包含任何数据,但是可以发送可选的尾部,包括消息头字段。

消息最后以CRLF结尾。

例子

编码的应答

HTTP/1.1 200 OKContent-Type: text/plainTransfer-Encoding: chunked25This is the data in the first chunk1Cand this is the second one3con8sequence0

编码应答的解释

前两个块的数据中包含有显式的\r\n字符。

"This is the data in the first chunk\r\n"      (37 字符 => 十六进制: 0x25)"and this is the second one\r\n"               (28 字符 => 十六进制: 0x1C)"con"                                          (3  字符 => 十六进制: 0x03)"sequence"                                     (8  字符 => 十六进制: 0x08)

应答需要以0长度的块( "0\r\n\r\n".)结束。

解码的数据

This is the data in the first chunkand this is the second oneconsequence

下面是RFC中的说明:

附加解释:1、中括号表示可有可无的;2、星号是正则表达式的用法,表示任意多个;3、CRLF表示回车换行,英文为:Carriage-Return Line-Feed;

4、 chunk-ext-name = token  chunk-ext-val  = token | quoted-string,可以和hashMap中的key和value类比; 5、trailer        = *(entity-header CRLF)中的entity-header,指的是response的头信息,也是key-value形式的,比如Content-type:text/html 。


https://tools.ietf.org/html/rfc2616#section-3.6.1

Chunked Transfer Coding


   The chunked encoding modifies the body of a message in order to
   transfer it as a series of chunks, each with its own size indicator,
   followed by an OPTIONAL trailer containing entity-header fields. This
   allows dynamically produced content to be transferred along with the
   information necessary for the recipient to verify that it has
   received the full message.


       Chunked-Body   = *chunk
                        last-chunk
                        trailer
                        CRLF
       chunk          = chunk-size [ chunk-extension ] CRLF
                        chunk-data CRLF
       chunk-size     = 1*HEX
       last-chunk     = 1*("0") [ chunk-extension ] CRLF
       chunk-extension= *( ";" chunk-ext-name [ "=" chunk-ext-val ] )
       chunk-ext-name = token
       chunk-ext-val  = token | quoted-string
       chunk-data     = chunk-size(OCTET)
       trailer        = *(entity-header CRLF)

   The chunk-size field is a string of hex digits indicating the size of
   the chunk. The chunked encoding is ended by any chunk whose size is
   zero, followed by the trailer, which is terminated by an empty line.

   The trailer allows the sender to include additional HTTP header
   fields at the end of the message. The Trailer header field can be
   used to indicate which header fields are included in a trailer

下面是chunked中的trailer中的解释:


1、RFC中的解释:

Trailer

http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.40
The Trailer general field value indicates that the given set of header fields is present in the trailer of a message encoded with chunked transfer-coding.

       Trailer  = "Trailer" ":" 1#field-name
An HTTP/1.1 message SHOULD include a Trailer header field in a message using chunked transfer-coding with a non-empty trailer. Doing so allows the recipient to know which header fields to expect in the trailer.
If no Trailer header field is present, the trailer SHOULD NOT include any header fields. See section 3.6.1 for restrictions on the use of trailer fields in a "chunked" transfer-coding.
Message header fields listed in the Trailer header field MUST NOT include the following header fields:
      . Transfer-Encoding
      . Content-Length
      . Trailer


2、通俗的解释:

Trailer:
lists the headers that will be transmitted after the message body, in a trailer block. This allows servers to compute some values, like Content-MD5: while transmitting the data. 

大意是:在响应(response)消息的尾部列举将要传送的头信息。在传输消息的时候这个允许服务器计算一些值,比如MD5加密的内容。

Message header fields listed in the Trailer header field MUST NOT include the following header fields:
      . Transfer-Encoding
      . Content-Length
      . Trailer

Trailer中的header-entity不能包含.Transfer-Encoding、Content-Length、Trailer这三个header


部分内容引用自:https://zh.wikipedia.org/wiki/%E5%88%86%E5%9D%97%E4%BC%A0%E8%BE%93%E7%BC%96%E7%A0%81

0 0
原创粉丝点击