如何解压http
How to decompress http?
我试图读取包含http压缩消息的tcp数据包,但它失败了'在zlib解压缩期间异常:(-3)不正确的头检查'。我的代码出了什么问题,或者有一个库可以为我做这些?
std::string decompress_string(const std::string& str) {
z_stream zs; // z_stream is zlib's control structure
memset(&zs, 0, sizeof(zs));
if (inflateInit(&zs) != Z_OK)
throw(std::runtime_error("inflateInit failed while decompressing."));
zs.next_in = (Bytef*)str.data();
zs.avail_in = str.size();
int ret;
char outbuffer[32768];
std::string outstring;
// get the decompressed bytes blockwise using repeated calls to inflate
do {
zs.next_out = reinterpret_cast<Bytef*>(outbuffer);
zs.avail_out = sizeof(outbuffer);
ret = inflate(&zs, 0);
if (outstring.size() < zs.total_out) {
outstring.append(outbuffer,
zs.total_out - outstring.size());
}
} while (ret == Z_OK);
inflateEnd(&zs);
if (ret != Z_STREAM_END) { // an error occurred that was not EOF
qDebug() << "Exception during zlib decompression: (" << ret << ") " << zs.msg;
return "";
}
return outstring;
}
std::string parseHttp(std::string payload) {
size_t index = payload.find("rnrn");
if (index == std::string::npos) {
qDebug() << "http body not found, dropped.";
return "";
}
std::string body = payload.substr(index + 4);
if (payload.find("Content-Encoding: gzip") == std::string::npos){
return body;
} else {
return decompress_string(body);
}
}
可能是gzip格式。尝试使用inflateInit2()
, wbits
设置为31
来解码gzip格式。gzip数据以1f 8b 08
开头
相关文章:
- 函数何时会在c++中包含stack_Unwind_Resume调用
- 如何在boost beast http请求中设置http头
- Python中的for循环与C++有何不同
- TSP递归解的迭代形式
- 在多个核心中处理一个HTTP请求
- SFML library: http request
- 我应该在锁定TBitmap画布后解锁它吗
- 使用C++进行运行长度解压缩
- c++fork,不需要等待,即可解压execl
- 解压数据从平铺json文件与zlib c++
- 在JPG解压期间缩小JPG
- 解压到指定目录
- 使用Boost gzip_decompressor解压内存中的数据
- 如何使用miniz创建一个可以通过gzip解压的压缩文件
- C/ c++解压GZIP时任意输出大小
- 在Redhat上从c++解压文件:system()的替代方案
- 使用boost将多个文件解压为一个文件
- C/C++:轻松解压到内存
- 如何解压http
- 哈夫曼压缩/解压