分块传输编码 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 OK
Content-Type: text/plain
Transfer-Encoding: chunked25
This is the data in the first chunk1C
and this is the second one3
con8
sequence0

编码应答的解释

前两个块的数据中包含有显式的\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 chunk
and this is the second one
consequence

下面是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

Http Chunked Transfer Coding相关推荐

  1. php transfer-encoding: chunked,php – 使用chunked transfer encoding和gzip

    我最近开始在我的网站上使用gzip,它在除Opera之外的所有浏览器上都起到了一种魅力作用,它给出了一个错误,即由于数据损坏而无法解压缩内容.从我可以从测试和谷歌搜索中收集到的,使用gzip和chun ...

  2. 《图解HTTP》读书笔记--第3章HTTP报文内的HTTP信息

    写在前面:本文仅供个人学习使用,如有侵权,请联系删除.文章中所用图片绝大多数来源于<图解HTTP>,请读者支持原版. 文章目录 第3章 HTTP报文内的HTTP信息 3.1 HTTP报文 ...

  3. 计算机网络是如何通信的「三」

    一.Cookie和Session机制:无状态下的会话 产生背景: HTTP 是一个无状态协议,无状态是指协议对于事务处理没有记忆能力,服务器不知道客户端是什么状态.即我们给服务器发送 HTTP 请求之 ...

  4. 如果传输的文件过大怎么办

    服务器上返回的资源文件比较大,比如有些js文件大小可能就有几兆.文件过大就会影响传输的效率,同时也会带来带宽的消耗.怎么办呢? 1. 常见的手段是,对文件进行压缩,减少文件大小.那压缩和解压缩的流程怎 ...

  5. Apple 低延迟HLS分析

    HLS协议 HTTP Live Streaming(HLS)是Apple公司主导提出并实现的基于HTTP的自适应码率流媒体通信协议(RFC8216),作为其产品QuickTime,Safari,OS ...

  6. 《图解 HTTP》读书笔记(未完待续)

    ARP 协议(Address Resolution Protocol)一种以解析地址的协议,根据通信双方的 IP 地址就可以查出对应的 MAC 地址. MAC( Media Access Contro ...

  7. HTTP(2)----图文解析HTTP

    先扒一扒HTTP协议背景? HTTP(HyperText Transfer Protocol) 即超文本传输协议,现在基本上所有web项目都遵从HTTP协议(协议就是一种人为的规范). 目前绝大部分使 ...

  8. http状态码 以及请求响应头相关

    1xx消息[编辑] 这一类型的状态码,代表请求已被接受,需要继续处理.这类响应是临时响应,只包含状态行和某些可选的响应头信息,并以空行结束.由于HTTP/1.0协议中没有定义任何1xx状态码,所以除非 ...

  9. [WebKit]浏览器的加载与页面性能优化

    非常棒.非常系统的一份资料,值得阅读! 原文来自百度泛用户体验. 作者:nwind 本文将探讨浏览器渲染的loading过程,主要有2个目的: 了解浏览器在loading过程中的实现细节,具体都做了什 ...

最新文章

  1. 反汇编算法介绍和应用——线性扫描算法分析
  2. 线程局部存储(很有局限性,但有时候可以用一下)
  3. Java遗传算法并行多机调度程序
  4. ansible调用callbacks插件实现结果nosql输出回调
  5. [Objective-C]编程艺术 笔记整理
  6. dijkstra算法代码_数据科学家需要知道的5种图算法(附代码)
  7. java--自动装箱,拆箱
  8. C语言 error C4996: This function or variable may be unsafe - C语言零基础入门教程
  9. Q91:真实地模拟透明材质(Realistic Transparency)(2)——Triangle Meshes
  10. 简单的http客户端
  11. 二分法求函数的解(函数零点)
  12. 单片机七阶音符_单片机教程二十七:单片机音乐程序设计(含源程序)
  13. java实现中国象棋3:走棋规则的实现
  14. Guava基本工具包Base:Null和字符串分割(Splitter)、拼接(Joiner)处理
  15. Android系统的系统运行库层,Android系统框架
  16. 02 LED的闪烁、模拟交通灯
  17. 测试适合染发颜色的软件,5秒测出你适合染什么颜色的头发!准到爆!
  18. ubuntu上打开md文件_Linux_查看.md
  19. 爬取豆瓣电影Top250影片信息
  20. Javaweb学习之中文乱码问题

热门文章

  1. tomcat 连接oracle重连,JSP+Tomcat连接Oracle数据库
  2. 【c语言】求最大最小值
  3. JAVA秒杀mysql层实现_Java高并发秒杀API之web层
  4. Ctrl + z 和 Linux jobs
  5. Linux中iptraf命令详解(IP局域网监控工具)
  6. 圆桌讨论:Cloud 2.0时代的工业互联网与智能制造 1
  7. Django 实现分库
  8. 深入解析Android关机
  9. 通过javascript把图片转化为字符画
  10. 修改数据库中group_concat的返回结果的长度限制