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 <cheimes@redhat.com>
This commit is contained in:
Fraser Tweedale 2018-05-29 18:52:05 +10:00 committed by Christian Heimes
parent 59b3eb0433
commit 1da3eddf56

View File

@ -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