解HTTP協議中的”Transfer-Encoding: chunked”

來源:互聯網
上載者:User

   通常,HTTP協議中使用Content-Length這個頭來告知資料的長度。然後,在資料下行的過程中,Content-Length的方式要預先在伺服器中緩衝所有資料,然後所有資料再一股腦兒地發給用戶端。
    如果要一邊產生資料,一邊發給用戶端,WEB 伺服器就需要使用"Transfer-Encoding: chunked"這樣的方式來代替Content-Length。

    "Transfer-Encoding: chunked"是這樣編碼的:
HTTP頭
/r/n
/r/n      --連續的兩個/r/n之後就是HTTP體了
16進位值代表的資料長度
/r/n
上面所指的資料長度
/r/n    --每段資料結束後,以/r/n標識

16進位代表的第二段資料
/r/n
XX長度的資料
/r/n

………… (反覆通過這樣的方式表示每次傳輸的資料長度)

0      --資料結束部分用0表示,然後是連續的兩個/r/n
/r/n
/r/n

      下面的代碼示範和如何解析"Transfer-Encoding: chunked"的資料:
//test_chunked.cpp<br />#include <stdio.h><br />#include <string.h></p><p>int Hex2Int(const char* str)<br />{<br /> int nResult = 0;<br /> while (*str!='/0')<br /> {<br /> switch (*str)<br /> {<br /> case '0'...'9':<br /> nResult = nResult*16 + *str-'0';<br /> break;<br /> case 'a'...'f':<br /> nResult = nResult*16 + *str-'a'+10;<br /> break;<br /> case 'A'...'F':<br /> nResult = nResult*16 + *str-'A'+10;<br /> break;<br /> default:<br /> return -1;<br /> break;<br /> }<br /> str++;<br /> }<br /> return nResult;<br />}</p><p>#define COPY_STRING(dst, src, src_len) do{memcpy((dst), (src), (src_len)); dst[(src_len)]='/0';}while(0);</p><p>void test(const char* file)<br />{<br /> //<br /> const int BUFFER_SIZE = 1024*10;<br /> char* buf = new char[BUFFER_SIZE];<br /> FILE* fp = fopen(file, "rb");<br /> if (NULL==fp)<br /> {<br /> printf("open file error/n");<br /> return;<br /> }<br /> int nLen = fread(buf, 1, BUFFER_SIZE, fp);<br /> fclose(fp);<br /> fp = NULL;<br /> buf[nLen] = '/0';<br /> //<br /> char* pBody = strstr(buf, "/r/n/r/n");<br /> if (NULL==pBody)<br /> {<br /> return;<br /> }<br /> pBody += 4;<br /> FILE* fDst = fopen("result.txt.gz", "ab");<br /> //下面開始解析<br /> int nBytes;<br /> char* pStart = pBody;<br /> char* pTemp;<br /> char temp[10];<br /> do<br /> {<br /> pTemp = strchr(pStart, '/r');<br /> if (NULL==pTemp)<br /> {<br /> printf("格式錯誤!/n");<br /> break;<br /> }<br /> nLen = pTemp-pStart;<br /> COPY_STRING(temp, pStart, nLen);<br /> nBytes = Hex2Int(temp);<br /> pStart = pTemp + 2;<br /> //下面寫入到另一個檔案<br /> if (nBytes>0)<br /> {<br /> if (nBytes!=fwrite(pStart, 1, nBytes, fDst))<br /> {<br /> printf("write error!/n");<br /> break;<br /> }<br /> pStart += nBytes + 2;<br /> }<br /> } while(nBytes>0);<br /> fclose(fDst);<br /> fDst = NULL;<br /> delete[] buf;<br /> buf = NULL;<br />}</p><p>int main()<br />{<br /> test("chunked.txt");<br /> return 1;<br />}<br />

聯繫我們

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