解HTTP协议中的"Transfer-Encoding: chunked"
2010-01-24 15:05
537 查看
通常,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"的数据:
如果要一边产生数据,一边发给客户端,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 #include <stdio.h> #include <string.h> int Hex2Int(const char* str) { int nResult = 0; while (*str!='/0') { switch (*str) { case '0'...'9': nResult = nResult*16 + *str-'0'; break; case 'a'...'f': nResult = nResult*16 + *str-'a'+10; break; case 'A'...'F': nResult = nResult*16 + *str-'A'+10; break; default: return -1; break; } str++; } return nResult; } #define COPY_STRING(dst, src, src_len) do{memcpy((dst), (src), (src_len)); dst[(src_len)]='/0';}while(0); void test(const char* file) { // const int BUFFER_SIZE = 1024*10; char* buf = new char[BUFFER_SIZE]; FILE* fp = fopen(file, "rb"); if (NULL==fp) { printf("open file error/n"); return; } int nLen = fread(buf, 1, BUFFER_SIZE, fp); fclose(fp); fp = NULL; buf[nLen] = '/0'; // char* pBody = strstr(buf, "/r/n/r/n"); if (NULL==pBody) { return; } pBody += 4; FILE* fDst = fopen("result.txt.gz", "ab"); //下面开始解析 int nBytes; char* pStart = pBody; char* pTemp; char temp[10]; do { pTemp = strchr(pStart, '/r'); if (NULL==pTemp) { printf("格式错误!/n"); break; } nLen = pTemp-pStart; COPY_STRING(temp, pStart, nLen); nBytes = Hex2Int(temp); pStart = pTemp + 2; //下面写入到另一个文件 if (nBytes>0) { if (nBytes!=fwrite(pStart, 1, nBytes, fDst)) { printf("write error!/n"); break; } pStart += nBytes + 2; } } while(nBytes>0); fclose(fDst); fDst = NULL; delete[] buf; buf = NULL; } int main() { test("chunked.txt"); return 1; }
相关文章推荐
- HTTP协议中的"Transfer-Encoding: chunked"
- 理解HTTP协议中的"Transfer-Encoding: chunked"
- 理解HTTP协议中的"Transfer-Encoding: chunked"
- 理解HTTP协议中的"Transfer-Encoding: chunked"
- http 协议 通用信息头 Transfer-Encoding: chunked
- http协议content-encoding & transfer-encoding
- HTTP协议:分块传输编码(Chunked transfer encoding)
- HTTP协议扫盲(八 )响应报文之 Transfer-Encoding=chunked方式
- http协议中的【Transfer-Encoding: chunked】
- 关于Http中Transfer-Encoding: chunked问题
- MVC详解:了解真正所谓的"框架"(http://vipnews.csdn.net/newscontent.aspx?pointid=2009_02_01_144216610)
- HTTP 协议中的 Transfer-Encoding
- <meta http-equiv="X-UA-Compatible" content="IE=edge" /> 的说明
- <%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>标签再使用时报错
- "http://www.NGNr.net"介绍
- <meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7" />隐藏<!DOCTYPE HTML>
- <meta http-equiv="X-UA-Compatible" content="IE=edge" /> 的说明
- How to deal with "Incorrect NSStringEncoding value 0×0000 Error with ASIHTTPRequest" problem?
- HTTP 协议中的 Transfer-Encoding
- NSString asscii格式(2进制) 转 utf8格式——解决iOS自己处理http socket数据,遇到Transfer-Encoding: chunked时