1 # -*- coding: utf-8 -*-
4 HTTP-related utility functions.
7 from __future__
import with_statement
10 import cStringIO
as StringIO
15 def _gzip_decode(gzip_data
):
17 Unzip gzipped data and return them.
19 :param gzip_data: Gzipped data.
20 :returns: The actual data.
23 gzip_data
= StringIO
.StringIO(gzip_data
)
24 gzip_file
= gzip
.GzipFile(fileobj
=gzip_data
)
25 data
= gzip_file
.read()
31 def http_response(response
):
33 Return the response of a HTTP request.
35 If the response has been gzipped, gunzip it first.
37 :param response: The raw response of a HTTP request.
38 :returns: A response suitable to be used by clients.
40 metadata
= response
.info()
41 data
= response
.read()
43 if metadata
.get('content-encoding') == 'gzip':
44 return _gzip_decode(data
)