Search code examples
pythonhttptornado

tornado web application fails to decode compressed http body


I am writing some simple prototype tornado web applications and found that tornado fails to decode the http request body with

Error -3 while decompressing: incorrect header check

From one of the tornado web application, I am sending http request by compressing the body using zlib.

http_body = zlib.compress(data)

And also added http header:

'Content-Encoding': 'gzip' 

However, when I receive this http request in another tornado web application, I see that it results in decompressing failure as mentioned above.

Sample Code to handle the http request:

class MyRequestHandler(tornado.web.RequestHandler):
    def post(self):
        global num
        message = self.request.body
        self.set_status(200)

I have also ensured that decompress_request=True when application listen.

I have checked the tornado documentation and earlier post and found nothing regarding compressed http body part or any example of it. The only thing mentioned is decompress_response parameter which just ensures compressed http response from a server.

Am I missing any settings here?


Solution

  • gzip and zlib are both based on the same underlying compression algorithm, but they are not the same thing. You must use gzip and not just zlib here:

    def post_gzip(self, body):
        bytesio = BytesIO()
        gzip_file = gzip.GzipFile(mode='w', fileobj=bytesio)
        gzip_file.write(utf8(body))
        gzip_file.close()
        compressed_body = bytesio.getvalue()
        return self.fetch('/', method='POST', body=compressed_body,
                          headers={'Content-Encoding': 'gzip'})
    

    Some zlib functions also take cryptic options that cause them to produce gzip-format output. These can be used with zlib.decompressobj and zlib.compressobj to do streaming compression and decompression.