在 HTTP/1.1 协议中增加了如下关于 chunk 的内容:
对于支持 HTTP/1.1 协议的客户端,要求能够处理以 chunked 形式组织的 response ,具体如下:
【Chunked Transfer-Encoding】
如果服务器想要在不知道 response 总体长度的情况下进行应答(就像使 long script output 那样),其需要使用简单的 chunked transfer-encoding 方式,即将完整的 response 拆分成更小的 chunk 后连续发送。你可以很容易辨认出这种类型的 response ,因为其包含了 Transfer-Encoding: chunked 头。所有支持 HTTP/1.1 协议的客户端都必须能够接收并处理 chunked 消息。
一条 chunked 消息由一组 chunk 构成,随后是由 "0" 构成的行,然后是多个可选的 footer ,最后是空行。每一块 chunk 有两部分构成:
上述 chunked response 样子如下:
需要注意的是,最后一个 footer 后面还需要有空行。当前文本数据的长度是 42 个字节(十六机制 1a + 10),而数据本身为 abcdefghijklmnopqrstuvwxyz1234567890abcdef 。而 footer 要以 header 来看待,就好像它们是直接被放在 response 头部一样。
chunk 数据可以包含任意二进制数据,并且很可能包含了很大的数据。表明 chunk 长度的 size 值很好被直接使用,但你需要确保程序中正确的忽略掉它们。footer 也很少被使用,但是它们会被用于 checksum 或数字签名的场景中。
作为对比,下面提供了一个等价于上面 response 的另一个例子,但是没有采用 chunked 编码形式:
而对于支持 HTTP/1.1 协议的服务器,同样需要能够接受以 chunked 数据格式发送来的 request 。
Faster response for dynamically-generated pages, by supporting chunked encoding, which allows a response to be sent before its total length is known.
对于支持 HTTP/1.1 协议的客户端,要求能够处理以 chunked 形式组织的 response ,具体如下:
【Chunked Transfer-Encoding】
如果服务器想要在不知道 response 总体长度的情况下进行应答(就像使 long script output 那样),其需要使用简单的 chunked transfer-encoding 方式,即将完整的 response 拆分成更小的 chunk 后连续发送。你可以很容易辨认出这种类型的 response ,因为其包含了 Transfer-Encoding: chunked 头。所有支持 HTTP/1.1 协议的客户端都必须能够接收并处理 chunked 消息。
一条 chunked 消息由一组 chunk 构成,随后是由 "0" 构成的行,然后是多个可选的 footer ,最后是空行。每一块 chunk 有两部分构成:
- 用于表示 chunk 数据长度的、以十六进制表示的、独占一行的数值。其后可能还有可以忽略的分号和额外参数(目前这些内容都未成为标准),以及结束标志 CRLF 。
- 数据本身,以 CRLF 结尾。
上述 chunked response 样子如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
|
HTTP
/1
.1 200 OK
Date: Fri, 31 Dec 1999 23:59:59 GMT
Content-Type: text
/plain
Transfer-Encoding: chunked
1a; ignore-stuff-here
abcdefghijklmnopqrstuvwxyz
10
1234567890abcdef
0
some-footer: some-value
another-footer: another-value
[blank line here]
|
需要注意的是,最后一个 footer 后面还需要有空行。当前文本数据的长度是 42 个字节(十六机制 1a + 10),而数据本身为 abcdefghijklmnopqrstuvwxyz1234567890abcdef 。而 footer 要以 header 来看待,就好像它们是直接被放在 response 头部一样。
chunk 数据可以包含任意二进制数据,并且很可能包含了很大的数据。表明 chunk 长度的 size 值很好被直接使用,但你需要确保程序中正确的忽略掉它们。footer 也很少被使用,但是它们会被用于 checksum 或数字签名的场景中。
作为对比,下面提供了一个等价于上面 response 的另一个例子,但是没有采用 chunked 编码形式:
1
2
3
4
5
6
7
8
|
HTTP
/1
.1 200 OK
Date: Fri, 31 Dec 1999 23:59:59 GMT
Content-Type: text
/plain
Content-Length: 42
some-footer: some-value
another-footer: another-value
abcdefghijklmnopqrstuvwxyz1234567890abcdef
|
Just as HTTP 1.1 clients must accept chunked responses, servers must accept chunked requests (an unlikely scenario, but possible). Servers aren't required to generate chunked messages; they just have to be able to receive them.