From 1da3eddf56450c4e41d92796fc303fdd0315690d Mon Sep 17 00:00:00 2001 From: Fraser Tweedale Date: Tue, 29 May 2018 18:52:05 +1000 Subject: [PATCH] Handle compressed responses from Dogtag We currently accept compressed responses for some Dogtag resources, via an 'Accept: gzip, deflate' header. But we don't decompress the received data. Inspect the response Content-Encoding header and decompress the response body according to its value. The `gzip.decompress` function is only available on Python 3.2 or later. In earlier versions, it is necessary to use StringIO and treat the compressed data as a file. This commit avoids this complexity. Therefore it should only be included in Python 3 based releases. Fixes: https://pagure.io/freeipa/issue/7563 Reviewed-By: Christian Heimes --- ipapython/dogtag.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/ipapython/dogtag.py b/ipapython/dogtag.py index c19814458..00343bd1c 100644 --- a/ipapython/dogtag.py +++ b/ipapython/dogtag.py @@ -18,8 +18,10 @@ # import collections +import gzip import logging import xml.dom.minidom +import zlib import six # pylint: disable=import-error @@ -228,8 +230,15 @@ def _httplib_request( logger.debug("httplib request failed:", exc_info=True) raise NetworkError(uri=uri, error=str(e)) + encoding = res.getheader('Content-Encoding') + if encoding == 'gzip': + # note: gzip.decompress available in Python >= 3.2 + http_body = gzip.decompress(http_body) # pylint: disable=no-member + elif encoding == 'deflate': + http_body = zlib.decompress(http_body) + logger.debug('response status %d', http_status) logger.debug('response headers %s', http_headers) - logger.debug('response body %r', http_body) + logger.debug('response body (decoded): %r', http_body) return http_status, http_headers, http_body