fix libcloud connection class
Project: http://git-wip-us.apache.org/repos/asf/libcloud/repo Commit: http://git-wip-us.apache.org/repos/asf/libcloud/commit/1c5deaf5 Tree: http://git-wip-us.apache.org/repos/asf/libcloud/tree/1c5deaf5 Diff: http://git-wip-us.apache.org/repos/asf/libcloud/diff/1c5deaf5 Branch: refs/heads/trunk Commit: 1c5deaf5dc26685a5a5f46f18fef732269029507 Parents: 2d60ee2 Author: Anthony Shaw <anthonys...@apache.org> Authored: Wed Jan 11 10:03:44 2017 +1100 Committer: Anthony Shaw <anthonys...@apache.org> Committed: Wed Jan 11 10:03:44 2017 +1100 ---------------------------------------------------------------------- libcloud/utils/loggingconnection.py | 75 +++++++++----------------------- 1 file changed, 20 insertions(+), 55 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/libcloud/blob/1c5deaf5/libcloud/utils/loggingconnection.py ---------------------------------------------------------------------- diff --git a/libcloud/utils/loggingconnection.py b/libcloud/utils/loggingconnection.py index 3ee09ad..adea0ba 100644 --- a/libcloud/utils/loggingconnection.py +++ b/libcloud/utils/loggingconnection.py @@ -23,18 +23,11 @@ except: from pipes import quote as pquote from xml.dom.minidom import parseString -import sys import os from libcloud.common.base import (LibcloudConnection, - HTTPResponse, HttpLibResponseProxy) -from libcloud.utils.py3 import httplib -from libcloud.utils.py3 import PY3 -from libcloud.utils.py3 import StringIO from libcloud.utils.py3 import u -from libcloud.utils.py3 import b - from libcloud.utils.misc import lowercase_keys from libcloud.utils.compression import decompress_data @@ -68,20 +61,6 @@ class LoggingConnection(LibcloudConnection): 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) @@ -95,42 +74,28 @@ class LoggingConnection(LibcloudConnection): 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 = 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 + 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) - 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) + return rv def _log_curl(self, method, url, body, headers): cmd = ["curl"] @@ -173,12 +138,12 @@ class LoggingConnection(LibcloudConnection): return " ".join(cmd) def getresponse(self): - r = HttpLibResponseProxy(LibcloudConnection.getresponse(self)) + original_response = LibcloudConnection.getresponse(self) if self.log is not None: - r, rv = self._log_response(r) + rv = self._log_response(HttpLibResponseProxy(original_response)) self.log.write(rv + "\n") self.log.flush() - return r + return original_response def request(self, method, url, body=None, headers=None): headers.update({'X-LC-Request-ID': str(id(self))})