我最近正在设置各种测试环境,在这个cas中,我不需要读取和解码来自HTTP服务器的gzip响应。我知道我到目前为止所做的工作,因为我已经用wireshark和硬编码数据进行了测试,如下所述,我的问题是,我处理来自HTTP服务器的gizzped数据的方式有什么问题?
下面是我正在使用的:
在这个线程http://www.qtcentre.org/threads/30031-qUncompress-data-from-gzip中,我对提供的数据使用了gzipDecopress函数,并查看了它的工作情况。
QByteArray gzipDecompress( QByteArray compressData )
{
//Hardcode sample data
const char dat[40] = {
0x1F, 0x8B, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0xAA, 0x2E, 0x2E, 0x49, 0x2C, 0x29,
0x2D, 0xB6, 0x4A, 0x4B, 0xCC, 0x29, 0x4E, 0xAD, 0x05, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0x03, 0x00,
0x2A, 0x63, 0x18, 0xC5, 0x0E, 0x00, 0x00, 0x00};
compressData = QByteArray::fromRawData( dat, 40);
//decompress GZIP data
//strip header and trailer
compressData.remove(0, 10);
compressData.chop(12);
const int buffersize = 16384;
quint8 buffer[buffersize];
z_stream cmpr_stream;
cmpr_stream.next_in = (unsigned char *)compressData.data();
cmpr_stream.avail_in = compressData.size();
cmpr_stream.total_in = 0;
cmpr_stream.next_out = buffer;
cmpr_stream.avail_out = buffersize;
cmpr_stream.total_out = 0;
cmpr_stream.zalloc = Z_NULL;
cmpr_stream.zalloc = Z_NULL;
if( inflateInit2(&cmpr_stream, -8 ) != Z_OK) {
qDebug() << "cmpr_stream error!";
}
QByteArray uncompressed;
do {
int status = inflate( &cmpr_stream, Z_SYNC_FLUSH );
if(status == Z_OK || status == Z_STREAM_END) {
uncompressed.append(QByteArray::fromRawData((char *)buffer, buffersize - cmpr_stream.avail_out));
cmpr_stream.next_out = buffer;
cmpr_stream.avail_out = buffersize;
} else {
inflateEnd(&cmpr_stream);
}
if(status == Z_STREAM_END) {
inflateEnd(&cmpr_stream);
break;
}
}while(cmpr_stream.avail_out == 0);
return uncompressed;}
当数据像该示例中那样被硬编码时,字符串被解压缩。但是,当我从HTTP服务器读取响应并将其存储在QByteArray中时,它无法解压缩。我读了下面的回复,当我在wireshark上比较结果时,我可以看到它是有效的
//Read that length of encoded data
char EncodedData[ LengthToRead ];
memset( EncodedData, 0, LengthToRead );
recv( socketDesc, EncodedData, LengthToRead, 0 );
EndOfData = true;
//EncodedDataBytes = QByteArray((char*)EncodedData);
EncodedDataBytes = QByteArray::fromRawData(EncodedData, LengthToRead );我假设我在读取响应时遗漏了一些头部或字节顺序,但目前不知道是什么。欢迎任何帮助!!
编辑:上周末我一直在研究这个问题,目前我正在尝试测试给定十六进制字符串的编码和解码,它在纯文本中是"{status:false}“。我曾尝试使用在线gzip编码器,例如http://www.txtwizard.net/compression,但它返回的一些ascii文本与上述代码中的十六进制字符串不匹配。当我使用PHPs gzcompress( "{status:false}",1)函数时,它会给出非ascii值,因为它们是ascii值,所以无法复制/粘贴到测试中。所以我想知道是否有任何gzip编码/解码的标准参考?它肯定不是某种特殊的编码,因为firefox和wireshark都可以解码数据包,但我的软件不能。
发布于 2018-03-26 20:56:52
所以问题出在我的gzip函数上,我在这个链接上找到了正确的函数:uncompress error when using zlib
正如玉米杆上面提到的,infalteInit2函数需要将MAX_WBITS+16作为其最大位大小,我认为这就是问题所在。如果任何人知道任何库或插件来处理这个问题,请在这里发布它们!令我惊讶的是,当HTTP客户端/服务器如此普遍地使用它时,它却不得不手动编码。
https://stackoverflow.com/questions/49324336
复制相似问题