如何进行chunked解码?

O湛狼O 2012-05-28 05:55:11
HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Content-Type: application/json;charset=utf-8
Transfer-Encoding: chunked
Content-Encoding: gzip
Vary: Accept-Encoding
Date: Mon, 28 May 2012 09:56:44 GMT
Connection: close

a

如何对这种数据解析解码?有高手知道吗。
...全文
418 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
xuwei86924 2013-08-09
  • 打赏
  • 举报
回复
引用 2 楼 lijiantao923 的回复:
已经解决了。不用这么麻烦,还是谢谢你了。[Quote=引用 1 楼 的回复:] 我写的代理工具里刚好做了这个处理,代码见此处:https://github.com/AlloyTeam/Rythem/blob/master/proxy/rypipedata.cpp 工具介绍:http://www.alloyteam.com/2012/05/web-front-end-tool-rythem-1/ chunked格式为: size\r\n content …… [/Quote]
咋解决的啊
un_nu 2012-05-30
  • 打赏
  • 举报
回复
我写的代理工具里刚好做了这个处理,代码见此处:https://github.com/AlloyTeam/Rythem/blob/master/proxy/rypipedata.cpp

工具介绍:http://www.alloyteam.com/2012/05/web-front-end-tool-rythem-1/

chunked格式为:

size\r\n
content
size\r\n
content\r\n
...
0\r\n

size为16进制字符

相关代码:

int RyPipeData::readChunk(int chunkSize,QByteArray* chunkData,QByteArray* unChunkDes){
int sourceSize = (*chunkData).size();
if(sourceSize < chunkSize){
(*unChunkDes).append(*chunkData);
(*chunkData).clear();
return sourceSize;
}else{
QByteArray ba = (*chunkData).left(chunkSize);
(*unChunkDes).append(chunkData->left(chunkSize));
(*chunkData).remove(0,chunkSize);
return chunkSize;
}
}
int RyPipeData::getChunkSize(QByteArray* chunkedData){
QByteArray theBody = *chunkedData;
//qDebug()<<"_lastChunkSizeByteArray:"<<_lastChunkSizeByteArray;

int chunkEndNewLineSize = 2; // if using \r\n
int beginOfLength = 0;
int endOfLength = 0;

if(theBody.startsWith("\r\n")){
beginOfLength = 2;
}else if(theBody.startsWith("\n")){
beginOfLength = 1;
}

if(!_lastChunkSizeByteArray.isEmpty()){
if(beginOfLength==0){
//the cach chunksize bytes was whole bytes
// do nothing here
}else{
endOfLength = theBody.indexOf("\r\n");
if(endOfLength==-1){
// not ending with \r\n
// try \n
endOfLength = theBody.indexOf("\n");
chunkEndNewLineSize = 1; // using \n
}
if(endOfLength == -1){
// newLine not found
// append bytes to _lastChunkSizeByteArray
// and return -1 to identify not found
/*
qDebug()<<"A invalid getChunkSize\n"
<<chunkedData->size()
<<"\n<===>\n"
<<theBody.replace("\r","\\r").replace("\n","\\n")
<<"\n<===>\n"
<<beginOfLength;
qDebug()<<"\n_lastChunkSizeByteArray="<<_lastChunkSizeByteArray;
*/
/*
meet this condition:
the data doesn't contains whole chunksize bytes
*/
_lastChunkSizeByteArray.append(theBody);
(*chunkedData).clear();
return -1;
}else{
// found new line
// update _lastChunkSizeByteArray and calculate and clear it
_lastChunkSizeByteArray.append(theBody.left(endOfLength));
}
}
}else{
// has no chunksizeByte cache
//theBody.mid(beginOfLength,endOfLength-beginOfLength).trimmed()
endOfLength = theBody.indexOf("\r\n",beginOfLength);
if(endOfLength==-1){
endOfLength = theBody.indexOf("\n",beginOfLength);
chunkEndNewLineSize = 1; // using \n
}
if(endOfLength == -1){
// newLine not found
// cache bytes to _lastChunkSizeByteArray
// and return -1 to identify not found
_lastChunkSizeByteArray.append(chunkedData->mid(beginOfLength));
/*
qDebug()<<"B invalid getChunkSize\n"
<<chunkedData->size()
<<"\n<===>\n"
<<theBody.replace("\r","\\r").replace("\n","\\n")
<<"\n<===>\n"
<<beginOfLength;
qDebug()<<"\n_lastChunkSizeByteArray="<<_lastChunkSizeByteArray;
*/
(*chunkedData).clear();
return -1;
}else{
_lastChunkSizeByteArray = (*chunkedData).mid(beginOfLength,endOfLength-beginOfLength).trimmed();
}
}
bool isChunkValid;
//qDebug()<<"chunkSize ba="<<theBody.mid(beginOfLength,endOfLength-beginOfLength).trimmed();

_lastChunkSizeByteArray = _lastChunkSizeByteArray.trimmed();
int chunkSize = _lastChunkSizeByteArray.toULongLong(&isChunkValid,16);
_lastChunkSizeByteArray.clear();

if(!isChunkValid){
qDebug()<<"CHUNKED PARSE ERROR:invalid getChunkSize\n"
<<theBody.replace("\r","\\r").replace("\n","\\n")
<<"\n"<<_lastChunkSizeByteArray;
return -1;
}

(*chunkedData).remove(0,endOfLength+chunkEndNewLineSize);

return chunkSize;
}
O湛狼O 2012-05-30
  • 打赏
  • 举报
回复
已经解决了。不用这么麻烦,还是谢谢你了。[Quote=引用 1 楼 的回复:]

我写的代理工具里刚好做了这个处理,代码见此处:https://github.com/AlloyTeam/Rythem/blob/master/proxy/rypipedata.cpp

工具介绍:http://www.alloyteam.com/2012/05/web-front-end-tool-rythem-1/

chunked格式为:

size\r\n
content
……
[/Quote]

16,211

社区成员

发帖
与我相关
我的任务
社区描述
Qt 是一个跨平台应用程序框架。通过使用 Qt,您可以一次性开发应用程序和用户界面,然后将其部署到多个桌面和嵌入式操作系统,而无需重复编写源代码。
社区管理员
  • Qt
  • 亭台六七座
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧