def _log_response(self, r): rv = "# -------- begin %d:%d response ----------\n" % (id(self), id(r)) ht = "" v = r.version if r.version == 10: v = "HTTP/1.0" if r.version == 11: v = "HTTP/1.1" ht += "%s %s %s\r\n" % (v, r.status, r.reason) body = r.read() for h in r.getheaders(): ht += "%s: %s\r\n" % (h[0].title(), h[1]) ht += "\r\n" # this is evil. laugh with me. ha arharhrhahahaha class fakesock: def __init__(self, s): self.s = s def makefile(self, *args, **kwargs): if PY3: from io import BytesIO cls = BytesIO else: cls = StringIO return cls(b(self.s)) rr = r original_data = body headers = lowercase_keys(dict(r.getheaders())) encoding = headers.get('content-encoding', None) if encoding in ['zlib', 'deflate']: body = decompress_data('zlib', body) elif encoding in ['gzip', 'x-gzip']: body = decompress_data('gzip', body) if r.chunked: ht += "%x\r\n" % (len(body)) ht += u(body) ht += "\r\n0\r\n" else: ht += u(body) if sys.version_info >= (2, 6) and sys.version_info < (2, 7): cls = HTTPResponse else: cls = httplib.HTTPResponse rr = cls(sock=fakesock(ht), method=r._method, debuglevel=r.debuglevel) rr.begin() rv += ht rv += ("\n# -------- end %d:%d response ----------\n" % (id(self), id(r))) rr._original_data = body return (rr, rv)
def _log_response(self, r): rv = "# -------- begin %d:%d response ----------\n" % (id(self), id(r)) ht = "" v = r.version if r.version == 10: v = "HTTP/1.0" if r.version == 11: v = "HTTP/1.1" ht += "%s %s %s\r\n" % (v, r.status, r.reason) body = r.read() for h in r.getheaders(): ht += "%s: %s\r\n" % (h[0].title(), h[1]) ht += "\r\n" headers = lowercase_keys(dict(r.getheaders())) encoding = headers.get('content-encoding', None) content_type = headers.get('content-type', None) if encoding in ['zlib', 'deflate']: body = decompress_data('zlib', body) elif encoding in ['gzip', 'x-gzip']: body = decompress_data('gzip', body) pretty_print = os.environ.get('LIBCLOUD_DEBUG_PRETTY_PRINT_RESPONSE', False) if pretty_print and content_type == 'application/json': try: body = json.loads(body.decode('utf-8')) body = json.dumps(body, sort_keys=True, indent=4) except: # Invalid JSON or server is lying about content-type pass elif pretty_print and content_type == 'text/xml': try: elem = parseString(body.decode('utf-8')) body = elem.toprettyxml() except Exception: # Invalid XML pass ht += u(body) rv += ht rv += ("\n# -------- end %d:%d response ----------\n" % (id(self), id(r))) return rv
def _decompress_response(self, body, headers): """ Decompress a response body if it is using deflate or gzip encoding. :param body: Response body. :type body: ``str`` :param headers: Response headers. :type headers: ``dict`` :return: Decompressed response :rtype: ``str`` """ encoding = headers.get('content-encoding', None) if encoding in ['zlib', 'deflate']: body = decompress_data('zlib', body) elif encoding in ['gzip', 'x-gzip']: body = decompress_data('gzip', body) else: body = body.strip() return body
def _decompress_response(self, response): """ Decompress a response body if it is using deflate or gzip encoding. @return: Decompressed response """ headers = lowercase_keys(dict(response.getheaders())) encoding = headers.get('content-encoding', None) original_data = getattr(response, '_original_data', None) if original_data is not None: return original_data body = response.read() if encoding in ['zlib', 'deflate']: body = decompress_data('zlib', body) elif encoding in ['gzip', 'x-gzip']: body = decompress_data('gzip', body) else: body = body.strip() return body
def _log_response(self, r): rv = "# -------- begin %d:%d response ----------\n" % (id(self), id(r)) ht = "" v = r.version if r.version == 10: v = "HTTP/1.0" if r.version == 11: v = "HTTP/1.1" ht += "%s %s %s\r\n" % (v, r.status, r.reason) body = r.read() for h in r.getheaders(): ht += "%s: %s\r\n" % (h[0].title(), h[1]) ht += "\r\n" # this is evil. laugh with me. ha arharhrhahahaha class fakesock(object): def __init__(self, s): self.s = s def makefile(self, *args, **kwargs): if PY3: from io import BytesIO cls = BytesIO else: cls = StringIO return cls(b(self.s)) rr = r headers = lowercase_keys(dict(r.getheaders())) encoding = headers.get('content-encoding', None) content_type = headers.get('content-type', None) if encoding in ['zlib', 'deflate']: body = decompress_data('zlib', body) elif encoding in ['gzip', 'x-gzip']: body = decompress_data('gzip', body) pretty_print = os.environ.get('LIBCLOUD_DEBUG_PRETTY_PRINT_RESPONSE', False) if r.chunked: ht += "%x\r\n" % (len(body)) ht += body.decode('utf-8') ht += "\r\n0\r\n" else: if pretty_print and content_type == 'application/json': try: body = json.loads(body.decode('utf-8')) body = json.dumps(body, sort_keys=True, indent=4) except: # Invalid JSON or server is lying about content-type pass elif pretty_print and content_type == 'text/xml': try: elem = xml.dom.minidom.parseString(body.decode('utf-8')) body = elem.toprettyxml() except Exception: # Invalid XML pass ht += u(body) if sys.version_info >= (2, 6) and sys.version_info < (2, 7): cls = HTTPResponse else: cls = httplib.HTTPResponse rr = cls(sock=fakesock(ht), method=r._method, debuglevel=r.debuglevel) rr.begin() rv += ht rv += ("\n# -------- end %d:%d response ----------\n" % (id(self), id(r))) rr._original_data = body return (rr, rv)