You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@libcloud.apache.org by pq...@apache.org on 2010/02/23 23:40:52 UTC

svn commit: r915562 - in /incubator/libcloud/trunk/libcloud: __init__.py base.py

Author: pquerna
Date: Tue Feb 23 22:40:52 2010
New Revision: 915562

URL: http://svn.apache.org/viewvc?rev=915562&view=rev
Log:
Improve support for debugging HTTPS connections, and expand the request logger to support HTTP in addition to HTTPS

Modified:
    incubator/libcloud/trunk/libcloud/__init__.py
    incubator/libcloud/trunk/libcloud/base.py

Modified: incubator/libcloud/trunk/libcloud/__init__.py
URL: http://svn.apache.org/viewvc/incubator/libcloud/trunk/libcloud/__init__.py?rev=915562&r1=915561&r2=915562&view=diff
==============================================================================
--- incubator/libcloud/trunk/libcloud/__init__.py (original)
+++ incubator/libcloud/trunk/libcloud/__init__.py Tue Feb 23 22:40:52 2010
@@ -32,9 +32,10 @@
     @type fo: File like object, only write operations are used.
     """
     import httplib
-    from libcloud.base import ConnectionKey,LoggingHTTPSConnection
+    from libcloud.base import ConnectionKey, LoggingHTTPConnection, LoggingHTTPSConnection
     LoggingHTTPSConnection.log = fo
-    ConnectionKey.conn_classes = (httplib.HTTPConnection, LoggingHTTPSConnection)
+    LoggingHTTPConnection.log = fo
+    ConnectionKey.conn_classes = (LoggingHTTPConnection, LoggingHTTPSConnection)
 
 def _init_once():
     import os

Modified: incubator/libcloud/trunk/libcloud/base.py
URL: http://svn.apache.org/viewvc/incubator/libcloud/trunk/libcloud/base.py?rev=915562&r1=915561&r2=915562&view=diff
==============================================================================
--- incubator/libcloud/trunk/libcloud/base.py (original)
+++ incubator/libcloud/trunk/libcloud/base.py Tue Feb 23 22:40:52 2010
@@ -197,7 +197,7 @@
         return self.status == httplib.OK or self.status == httplib.CREATED
 
 #TODO: Move this to a better location/package
-class LoggingHTTPSConnection(httplib.HTTPSConnection):
+class LoggingConnection():
     """
     Debug class to log all HTTP(s) requests as they could be made
     with the C{curl} command.
@@ -241,14 +241,6 @@
                % (id(self), id(r)))
         return (rr, rv)
 
-    def getresponse(self):
-        r = httplib.HTTPSConnection.getresponse(self)
-        if self.log is not None:
-            r, rv = self._log_response(r)
-            self.log.write(rv + "\n")
-            self.log.flush()
-        return r
-
     def _log_curl(self, method, url, body, headers):
         cmd = ["curl", "-i"]
 
@@ -264,7 +256,18 @@
         cmd.extend([pquote("https://%s:%d%s" % (self.host, self.port, url))])
         return " ".join(cmd)
 
+class LoggingHTTPSConnection(LoggingConnection, httplib.HTTPSConnection):
+
+    def getresponse(self):
+        r = httplib.HTTPSConnection.getresponse(self)
+        if self.log is not None:
+            r, rv = self._log_response(r)
+            self.log.write(rv + "\n")
+            self.log.flush()
+        return r
+
     def request(self, method, url, body=None, headers=None):
+        headers.update({'X-LC-Request-ID': str(id(self))})
         if self.log is not None:
             pre = "# -------- begin %d request ----------\n"  % id(self)
             self.log.write(pre +
@@ -273,6 +276,26 @@
         return httplib.HTTPSConnection.request(self, method, url,
                                                body, headers)
 
+class LoggingHTTPConnection(LoggingConnection, httplib.HTTPConnection):
+
+    def getresponse(self):
+        r = httplib.HTTPConnection.getresponse(self)
+        if self.log is not None:
+            r, rv = self._log_response(r)
+            self.log.write(rv + "\n")
+            self.log.flush()
+        return r
+
+    def request(self, method, url, body=None, headers=None):
+        headers.update({'X-LC-Request-ID': str(id(self))})
+        if self.log is not None:
+            pre = "# -------- begin %d request ----------\n"  % id(self)
+            self.log.write(pre +
+                           self._log_curl(method, url, body, headers) + "\n")
+            self.log.flush()
+        return httplib.HTTPConnection.request(self, method, url,
+                                               body, headers)
+
 class ConnectionKey(object):
     """
     A Base Connection class to derive from.
@@ -280,7 +303,7 @@
     interface.implementsOnly(IConnectionKey)
     interface.classProvides(IConnectionKeyFactory)
 
-    #conn_classes = (httplib.HTTPConnection, LoggingHTTPSConnection)
+    #conn_classes = (httplib.LoggingHTTPConnection, LoggingHTTPSConnection)
     conn_classes = (httplib.HTTPConnection, httplib.HTTPSConnection)
 
     responseCls = Response
@@ -315,6 +338,12 @@
         port = port or self.port[self.secure]
 
         connection = self.conn_classes[self.secure](host, port)
+        # You can uncoment this line, if you setup a reverse proxy server
+        # which proxies to your endpoint, and lets you easily capture
+        # connections in cleartext when you setup the proxy to do SSL
+        # for you
+        #connection = self.conn_classes[False]("127.0.0.1", 8080)
+
         self.connection = connection
 
     def _user_agent(self):