使用zlib解压缩来自HTTPS流量的gzip,不正确的标头检查

问题描述 投票:0回答:1

我正在尝试使用python创建代理,它也会读取请求和响应的内容,我正在使用它来执行此操作:https://github.com/inaz2/proxy2/blob/python3/proxy2.py

但由于某种原因,我无法解压缩任何gzip压缩的有效负载。到目前为止我尝试过的:

@staticmethod
def decode_content_body(data, encoding):
    print(encoding) # -> 'gzip'
    if not data:
        return None

    if encoding == 'identity':
        text = data
    elif encoding in ('gzip', 'x-gzip'):
        try:
            data = data.encoded('latin_1')
            # data = str(data) # no luck
            # data = data.encoded() # no luck
            compressed_stream = StringIO(data)
            gzipper = gzip.GzipFile(fileobj=compressed_stream)
            text = gzipper.read() # -> TypeError: can't concat str to bytes

        except:
            # data has to be bytes like object, says zlib
            # text = zlib.decompress(data.encode()) # -> zlib.error: Error -3 while decompressing data: incorrect header check
            text = zlib.decompress(data.encode(), -zlib.MAX_WBITS) # -> zlib.error: Error -3 while decompressing data: invalid block type

    elif encoding == 'deflate':
        try:
            text = zlib.decompress(data)
        except zlib.error:
            text = zlib.decompress(data, -zlib.MAX_WBITS)

    else:
        raise Exception("Unknown Content-Encoding: {}".format(encoding))
    return text

数据不是人类可读的格式,所以它显然是用某些东西压缩的。代理正在使用使用HTTPS的站点。

python python-3.x https decode zlib
1个回答
0
投票

问题可能是data传递给你的函数。

它的类型为str,因此您用来读取请求数据的代码已经尝试将它收到的bytes数据解码为str。两件事之一可能出错了:

  1. 它不希望bytes包含压缩的数据
  2. 它希望使用与现实中使用的机制不同的机制来压缩数据

因此:str包含胡言乱语!

使用stomp.py包时遇到了同样的问题!在我的例子中,解决方案是在打开Stomp连接时显式设置auto_decode=False参数。

希望有所帮助!

PS:关于我的特定问题如何解决的更多信息:https://groups.google.com/forum/#!topic/openraildata-talk/IsO206F5US8

© www.soinside.com 2019 - 2024. All rights reserved.