http 編碼chunked

來源:互聯網
上載者:User

標籤:

之前針對這個也不是很關注,這個chunked在nginx上是預設開啟的,但是在apache上沒有,所以當切換伺服器的時候,同時遇到了這個問題,探索資料內容存在亂碼。

定位我就知道應該是http header的問題,但是具體是什麼也不是很清楚。


仔細查閱發現,這個chunked編碼。

http以trunked編碼方式傳輸的資料表示規則

一般HTTP通訊時會使用是Content-Length頭資訊性來指定小,但是有時候無法確定資訊大小,就要使用trunked編碼動態提供body內容的長度。

進行Chunked編碼傳輸的HTTP資料要在訊息頭部設定:

Transfer-Encoding: chunked

表示Content Body將用chunked編碼傳輸內容。

Chunked編碼一般使用若干個chunk串聯而成,最後由一個標明長度為0的chunk標示結束。
每個chunk分為頭部本文兩部分,
頭部內容指定下一段本文的字元總數(非零開頭的十六進位的數字)和數量單位(一般不寫,表示位元組).
本文部分就是指定長度的實際內容,兩部分之間用斷行符號換行(CRLF)隔開。
在最後一個長度為0的chunk中的內容是稱為footer的內容,是一些附加的Header資訊(通常可以直接忽略)。具體的chunk編碼格式如下:

  1.   chunked-body =*chunk
  2.          "0" CRLF
  3.          footer
  4.          CRLF
  5.   chunk = chunk-size[ chunk-ext ] CRLF
  6.        chunk-data CRLF
  7.   hex-no-zero = <HEX excluding "0">
  8.   chunk-size = hex-no-zero*HEX
  9.   chunk-ext = *( ";" chunk-ext-name[ "=" chunk-ext-value] )
  10.   chunk-ext-name= token
  11.   chunk-ext-val= token | quoted-string
  12.   chunk-data = chunk-size(OCTET)
  13.   footer = *entity-header

RFC文檔中的chunked解碼過程如下:

  1.   length := 0
  2.   read chunk-size, chunk-ext(if any) and CRLF
  3.   while (chunk-size >0) {
  4.       read chunk-data and CRLF
  5.       append chunk-data to entity-body
  6.       length := length + chunk-size
  7.       read chunk-size and CRLF
  8.   }
  9.   read entity-header
  10.   while (entity-header not empty){
  11.       append entity-header to existing header fields
  12.       read entity-header
  13.   }
  14.   Content-Length := length
  15.   Remove "chunked" from Transfer-Encoding

nginx伺服器關閉chunked編碼方式:

chunked_transfer_encoding off;


參考連結:

http://www.6san.com/759/

http://www.sunnyu.com/?p=175

http 編碼chunked

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.