[转] HTTP协议之Chunked解析

Posted by gavinkwoe

转至: http://hi.baidu.com/zkheartboy/blog/item/9216a0fd05591e1508244d74.html

在网上找了好一会,始终没发现有解析编码的文章,那就自己写一个吧,呵呵。
网上使用编码的网站似乎并不是很多,除了那些使用GZip压缩的网站,例:google.com,还有就是大部分打开GZip压缩的PHP论坛。
根据本人的理解,使用编码的主要好处就在于一些程序的运算出过程中,可以动态的输出内容。
例如,要在后台处理一个小时的运算,但又不希望用户等一个小时才能看到结果。这时就可采用编码将内容分块输出,用户随时都可以接收到最新的处理结果。
ASP关闭了缓存的输出模式,就是编码的。(Response.Buffer = false)
而每一次的Response.Write,都是一个,所以不要使用的太频繁哦,否则Chunk数量太多,额外的数据太浪费空间了。
若想了解的具体编码结构,用ASP关闭缓存调试蛮方便的。:)

我们先来看看RFC2616中对的定义:
-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- CRLF)

我们来模拟一下数据结构:
[Chunk大小][回车][Chunk数据体][回车][Chunk大小][回车][Chunk数据体][回车][0][回车]

注意chunk-size是以十六进制的ASCII码表示的,比如86AE(实际的十六进制应该是:38366165),计算成长度应该是:34478,表示从回车之后有连续的34478字节的数据。
跟踪了www.yahoo.com的返回数据,发现在chunk-size中,还会多一些空格。可能是固定长度为7个字节,不满7个字节的,就以空格补足,空格的ASCII码是0×20。

以下是解码过程的伪代码:
length := 0//用来记录解码后的数据体长度
read chunk-size, chunk-extension (if any) and CRLF//第一次读取块大小
while (chunk-size > 0) {//一直循环,直到读取的块大小为0
read chunk-data and CRLF//读取块数据体,以回车结束
append chunk-data to entity-body//添加块数据体到解码后实体数据
length := length + chunk-size//更新解码后的实体长度
read chunk-size and CRLF//读取新的块大小
}
read entity-//以下代码读取全部的头标记
while (entity- not empty) {
append entity- to existing fields
read entity-
}
Content-Length := length//头标记中添加内容长度
Remove “” from -//头标记中移除-

有空再研究一下GZip+是如何编码的,估计是每个Chunk块进行一次GZip独立压缩。

使用了,自然会在性能上稍微打点折扣,因为比正常的数据体多出了一些额外的消耗。
但是有一些情况下,必需要使用分块输出,这也是不得已而为之~^_^


[转] Chunked编码

Posted by gavinkwoe

转至: http://www.21andy.com/blog/20071109/660.html

有时候,Web服务器生成 Response是无法在就确定消息大小的,这时一般来说服务器将不会提供Content-Length的头信息,而采用编码动态的提供body内容的长度。

进行编码传输的 Response会在消息头部设置:

表示Content Body将用编码传输内容。

编码使用若干个Chunk串连而成,由一个标明长度为0的chunk标示结束。每个Chunk分为头部和正文两部分,头部内容指定下一段正文的字符总数(十六进制的数字)和数量单位(一般不写),正文部分就是指定长度的实际内容,两部分之间用回车换行(CRLF)隔开。在最后一个长度为0的Chunk中的内容是称为footer的内容,是一些附加的信息(通常可以直接忽略)。具体的Chunk编码格式如下:

  -Body = *chunk
         ”0″ CRLF
         footer
         CRLF
  chunk = chunk-size [ chunk-ext ] CRLF
      chunk-data CRLF

  hex-no-zero = <HEX excluding “0″>

  chunk-size = hex-no-zero *HEX
  chunk-ext = *( “;” chunk-ext-name [ "=" chunk-ext-value ] )
  chunk-ext-name = token
  chunk-ext-val = token | quoted-string
  chunk-data = chunk-size(OCTET)

  footer = *entity-

RFC文档中的解码过程如下:

  length := 0
  read chunk-size, chunk-ext (if any) and CRLF
  while (chunk-size > 0) {
  read chunk-data and CRLF
  append chunk-data to entity-body
  length := length + chunk-size
  read chunk-size and CRLF
  }
  read entity-
  while (entity- not empty) {
  append entity- to existing fields
  read entity-
  }
  Content-Length := length
  Remove “” from -

最后提供一段PHP版本的解码代码:

$chunk_size = (integer)hexdec(fgets( $socket_fd, 4096 ) );
while(!feof($socket_fd) && $chunk_size > 0) {
    
$bodyContent .= fread( $socket_fd, $chunk_size );
    
fread( $socket_fd, 2 ); // skip \r\n
    
$chunk_size = (integer)hexdec(fgets( $socket_fd, 4096 ) );
}