<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Computer, Electron and Technology &#187; protocol</title>
	<atom:link href="http://www.donevii.com/post/tag/protocol/feed" rel="self" type="application/rss+xml" />
	<link>http://www.donevii.com</link>
	<description>关注技术、移动互联网以及一切 GEEK &#38; NERD 的事情</description>
	<lastBuildDate>Wed, 21 Dec 2011 10:49:54 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>[转] HTTP协议之Chunked解析</title>
		<link>http://www.donevii.com/post/468.html</link>
		<comments>http://www.donevii.com/post/468.html#comments</comments>
		<pubDate>Tue, 02 Sep 2008 13:59:00 +0000</pubDate>
		<dc:creator>gavinkwoe</dc:creator>
				<category><![CDATA[protocol]]></category>
		<category><![CDATA[chunked]]></category>
		<category><![CDATA[http]]></category>
		<category><![CDATA[协议]]></category>
		<category><![CDATA[编码]]></category>
		<category><![CDATA[网络]]></category>

		<guid isPermaLink="false">http://www.donevii.com/?p=468</guid>
		<description><![CDATA[转至: http://hi.baidu.com/zkheartboy/blog/item/9216a0fd05591e1508244d74.html 在网上找了好一会，始终没发现有解析Chunked编码的文章，那就自己写一个吧，呵呵。 网上使用Chunked编码的网站似乎并不是很多，除... ]]></description>
			<content:encoded><![CDATA[<p>转至: <a href="http://hi.baidu.com/zkheartboy/blog/item/9216a0fd05591e1508244d74.html">http://hi.baidu.com/zkheartboy/blog/item/9216a0fd05591e1508244d74.html</a></p>
<p>在网上找了好一会，始终没发现有解析Chunked编码的文章，那就自己写一个吧，呵呵。<br />
网上使用Chunked编码的网站似乎并不是很多，除了那些使用GZip压缩的网站，例：google.com，还有就是大部分打开GZip压缩的PHP论坛。<br />
根据本人的理解，使用Chunked编码的主要好处就在于一些程序的运算出过程中，可以动态的输出内容。<br />
例如，要在后台处理一个小时的运算，但又不希望用户等一个小时才能看到结果。这时就可采用Chunked编码将内容分块输出，用户随时都可以接收到最新的处理结果。<br />
ASP关闭了缓存的输出模式，就是Chunked编码的。(Response.Buffer = false)<br />
而每一次的Response.Write，都是一个Chunked，所以不要使用的太频繁哦，否则Chunk数量太多，额外的数据太浪费空间了。<br />
若想了解Chunked的具体编码结构，用ASP关闭缓存调试蛮方便的。:)</p>
<p>我们先来看看RFC2616中对Chunked的定义：<br />
<a href="http://www.donevii.com/post/tag/chunked" class="st_tag internal_tag" rel="tag" title="Posts tagged with chunked">Chunked</a>-Body = *chunk<br />
last-chunk<br />
trailer<br />
CRLF</p>
<p>chunk = chunk-size [ chunk-extension ] CRLF<br />
chunk-data CRLF<br />
chunk-size = 1*HEX<br />
last-chunk = 1*(&#8220;0&#8243;) [ chunk-extension ] CRLF</p>
<p>chunk-extension= *( &#8220;;&#8221; chunk-ext-name [ "=" chunk-ext-val ] )<br />
chunk-ext-name = token<br />
chunk-ext-val = token | quoted-string<br />
chunk-data = chunk-size(OCTET)<br />
trailer = *(entity-header CRLF)</p>
<p>我们来模拟一下数据结构：<br />
[Chunk大小][回车][Chunk数据体][回车][Chunk大小][回车][Chunk数据体][回车][0][回车]</p>
<p>注意chunk-size是以十六进制的ASCII码表示的，比如86AE（实际的十六进制应该是：38366165），计算成长度应该是：34478，表示从回车之后有连续的34478字节的数据。<br />
跟踪了www.yahoo.com的返回数据，发现在chunk-size中，还会多一些空格。可能是固定长度为7个字节，不满7个字节的，就以空格补足，空格的ASCII码是0&#215;20。</p>
<p>以下是解码过程的伪代码：<br />
length := 0//用来记录解码后的数据体长度<br />
read chunk-size, chunk-extension (if any) and CRLF//第一次读取块大小<br />
while (chunk-size &gt; 0) {//一直循环，直到读取的块大小为0<br />
read chunk-data and CRLF//读取块数据体，以回车结束<br />
append chunk-data to entity-body//添加块数据体到解码后实体数据<br />
length := length + chunk-size//更新解码后的实体长度<br />
read chunk-size and CRLF//读取新的块大小<br />
}<br />
read entity-header//以下代码读取全部的头标记<br />
while (entity-header not empty) {<br />
append entity-header to existing header fields<br />
read entity-header<br />
}<br />
Content-Length := length//头标记中添加内容长度<br />
Remove &#8220;chunked&#8221; from Transfer-Encoding//头标记中移除Transfer-Encoding</p>
<p>有空再研究一下GZip＋Chunked是如何编码的，估计是每个Chunk块进行一次GZip独立压缩。</p>
<p>使用了Chunked，自然会在性能上稍微打点折扣，因为比正常的数据体多出了一些额外的消耗。<br />
但是有一些情况下，必需要使用分块输出，这也是不得已而为之～^_^</p>
]]></content:encoded>
			<wfw:commentRss>http://www.donevii.com/post/468.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

