網上使用chunked編碼的網站似乎并不是很多,除了那些使用gzip壓縮的網站,例:google.com,還有就是大部分打開gzip壓縮的php論壇。
根據本人的理解,使用chunked編碼的主要好處就在于一些程序的運算出過程中,可以動態的輸出內容。
例如,要在后臺處理一個小時的運算,但又不希望用戶等一個小時才能看到結果。這時就可采用chunked編碼將內容分塊輸出,用戶隨時都可以接收到最新的處理結果。
asp關閉了緩存的輸出模式,就是chunked編碼的。(response.buffer = false)
而每一次的response.write,都是一個chunked,所以不要使用的太頻繁哦,否則chunk數量太多,額外的數據太浪費空間了。
若想了解chunked的具體編碼結構,用asp關閉緩存調試蠻方便的。:)
我們先來看看rfc2616中對chunked的定義:
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)
我們來模擬一下數據結構:
[chunk大小][回車][chunk數據體][回車][chunk大小][回車][chunk數據體][回車][0][回車]
注意chunk-size是以十六進制的ascii碼表示的,比如86ae(實際的十六進制應該是:38366165),計算成長度應該是:34478,表示從回車之后有連續的34478字節的數據。
跟蹤了的返回數據,發現在chunk-size中,還會多一些空格。可能是固定長度為7個字節,不滿7個字節的,就以空格補足,空格的ascii碼是0x20。
以下是解碼過程的偽代碼:
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-header//以下代碼讀取全部的頭標記
while (entity-header not empty) {
append entity-header to existing header fields
read entity-header
}
content-length := length//頭標記中添加內容長度
remove chunked from transfer-encoding//頭標記中移除transfer-encoding
有空再研究一下gzip+chunked是如何編碼的,估計是每個chunk塊進行一次gzip獨立壓縮。
使用了chunked,自然會在性能上稍微打點折扣,因為比正常的數據體多出了一些額外的消耗。
但是有一些情況下,必需要使用分塊輸出,這也是不得已而為之.