如何解压http?
How to decompress http?
我正在尝试读取包含 HTTP 压缩消息的 TCP 数据包,但失败并显示 'Exception during zlib decompression: ( -3 ) incorrect header check'。我的代码有什么问题,或者是否有一个库可以帮我解决这个问题?
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("\r\n\r\n");
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
.
开头
我正在尝试读取包含 HTTP 压缩消息的 TCP 数据包,但失败并显示 'Exception during zlib decompression: ( -3 ) incorrect header check'。我的代码有什么问题,或者是否有一个库可以帮我解决这个问题?
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("\r\n\r\n");
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
.