http協議之chunk編碼

來源:互聯網
上載者:User

在有時伺服器產生HTTP回應是無法確定訊息大小的,這時用Content-Length就無法事先寫入長度,而需要即時產生訊息長度,這時伺服器一般採用Chunked編碼。  
    在進行Chunked編碼傳輸時,在回複訊息的頭部有transfer-coding並定為Chunked,表示將用Chunked編碼傳輸內容。採用以下方式編碼:  
    Chunked-Body=*chunk  
           "0"CRLF  
           footer  
           CRLF  
    chunk=chunk-size[chunk-ext]CRLF  
        chunk-dataCRLF  
   
    hex-no-zero=<HEXexcluding"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-header  
 
  編碼使用若干個Chunk組成,由一個標明長度為0的chunk結束,每個Chunk有兩部分組成,第一部分是該Chunk的長度和長度單位(一般不
寫),第二部分就是指定長度的內容,每個部分用CRLF隔開。在最後一個長度為0的Chunk中的內容是稱為footer的內容,是一些沒有寫的頭部內
容。  
    下面給出一個Chunked的解碼過程(RFC文檔中有)  
    length:=0  
    readchunk-size,chunk-ext(ifany)andCRLF  
    while(chunk-size>0){  
    readchunk-dataandCRLF  
    appendchunk-datatoentity-body  
    length:=length+chunk-size  
    readchunk-sizeandCRLF  
    }  
    readentity-header  
    while(entity-headernotempty){  
    appendentity-headertoexistingheaderfields  
    readentity-header  
    }  
    Content-Length:=length  
    Remove"chunked"fromTransfer-Encoding

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.