def __init__(self, response, connection): """ :param response: HTTP response object. (optional) :type response: :class:`httplib.HTTPResponse` :param connection: Parent connection object. :type connection: :class:`.Connection` """ self.connection = connection # http.client In Python 3 doesn't automatically lowercase the header # names self.headers = lowercase_keys(dict(response.headers)) self.error = response.reason self.status = response.status_code self.request = response.request self.iter_content = response.iter_content self.body = response.text.strip() \ if response.text is not None and hasattr(response.text, 'strip') \ else '' if not self.success(): raise exception_from_message(code=self.status, message=self.parse_error(), headers=self.headers) self.object = self.parse_body()
def __init__(self, response, connection): """ :param response: HTTP response object. (optional) :type response: :class:`httplib.HTTPResponse` :param connection: Parent connection object. :type connection: :class:`.Connection` """ self.connection = connection # http.client In Python 3 doesn't automatically lowercase the header # names self.headers = lowercase_keys(dict(response.getheaders())) self.error = response.reason self.status = response.status # This attribute is set when using LoggingConnection. original_data = getattr(response, '_original_data', None) if original_data: # LoggingConnection already decompresses data so it can log it # which means we don't need to decompress it here. self.body = response._original_data else: self.body = self._decompress_response(body=response.read(), headers=self.headers) if PY3: self.body = b(self.body).decode('utf-8') if not self.success(): raise Exception(self.parse_error()) self.object = self.parse_body()
def __init__(self, response, connection): """ :param response: HTTP response object. (optional) :type response: :class:`httplib.HTTPResponse` :param connection: Parent connection object. :type connection: :class:`.Connection` """ self.connection = connection # http.client In Python 3 doesn't automatically lowercase the header # names self.headers = lowercase_keys(dict(response.headers)) self.error = response.reason self.status = response.status_code self.request = response.request self.iter_content = response.iter_content self.body = response.text.strip() \ if response.text is not None and hasattr(response.text, 'strip') \ else '' if not self.success(): raise exception_from_message(code=self.status, message=self.parse_error()) self.object = self.parse_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: 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 __init__(self, connection, response=None): """ :param connection: Parent connection object. :type connection: :class:`.Connection` """ self._status = None self._response = None self._headers = {} self._error = None self._reason = None self.connection = connection if response is not None: self.headers = lowercase_keys(dict(response.headers)) self.error = response.reason self.status = response.status_code self.request = response.request self.iter_content = response.iter_content
def __init__(self, connection, response=None): """ :param connection: Parent connection object. :type connection: :class:`.Connection` """ self._status = None self._response = None self._headers = {} self._error = None self._reason = None self.connection = connection if response: self.headers = lowercase_keys(dict(response.headers)) self.error = response.reason self.status = response.status_code self.request = response.request self.iter_content = response.iter_content
def __init__(self, response, connection): self.body = self._decompress_response(response=response) if PY3: self.body = b(self.body).decode('utf-8') self.status = response.status # http.client In Python 3 doesn't automatically lowercase the header # names self.headers = lowercase_keys(dict(response.getheaders())) self.error = response.reason self.connection = connection if not self.success(): raise Exception(self.parse_error()) self.object = self.parse_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" headers = lowercase_keys(dict(r.getheaders())) content_type = headers.get("content-type", None) pretty_print = os.environ.get("LIBCLOUD_DEBUG_PRETTY_PRINT_RESPONSE", False) if pretty_print and content_type == "application/json": try: body = json.loads(ensure_string(body)) body = json.dumps(body, sort_keys=True, indent=4) except Exception: # Invalid JSON or server is lying about content-type pass elif pretty_print and content_type in ["text/xml", "application/xml"]: try: elem = parseString(body.decode("utf-8")) body = elem.toprettyxml() except Exception: # Invalid XML pass ht += ensure_string(body) rv += ht rv += "\n# -------- end %d:%d response ----------\n" % (id(self), id(r)) return rv
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 headers(self): if not self._headers: self._headers = lowercase_keys(dict(self.response.getheaders())) return self._headers
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)