[Inteproxy-commits] r277 - in trunk: . inteproxy

scm-commit@wald.intevation.org scm-commit at wald.intevation.org
Tue Sep 21 20:09:16 CEST 2010


Author: iweinzierl
Date: 2010-09-21 20:09:15 +0200 (Tue, 21 Sep 2010)
New Revision: 277

Modified:
   trunk/ChangeLog
   trunk/inteproxy/proxycore.py
Log:
Some refactoring: extracted http and https connection creation in proxycore.py into own methods.

Modified: trunk/ChangeLog
===================================================================
--- trunk/ChangeLog	2010-09-20 19:46:56 UTC (rev 276)
+++ trunk/ChangeLog	2010-09-21 18:09:15 UTC (rev 277)
@@ -1,3 +1,11 @@
+2010-09-21  Ingo Weinzierl <ingo.weinzierl at intevation.de>
+
+	Some refactoring
+
+	* inteproxy/proxycore.py (open_http_connection): Renamed this method
+	into 'perform_http_request' and moved the http and https connection into
+	own methods 'open_http_connection' and open_https_connection'.
+
 2010-09-20  Bernhard Herzog  <bh at intevation.de>
 
 	* inteproxy/proxyconnection.py: Removed.  It's no longer used.

Modified: trunk/inteproxy/proxycore.py
===================================================================
--- trunk/inteproxy/proxycore.py	2010-09-20 19:46:56 UTC (rev 276)
+++ trunk/inteproxy/proxycore.py	2010-09-21 18:09:15 UTC (rev 277)
@@ -85,7 +85,7 @@
         self.log_debug("Converted url: %r", remote_url)
         response = None
         try:
-            response = self.open_http_connection(remote_url, client_request)
+            response = self.perform_http_request(remote_url, client_request)
         except socket.error, exc:
             # some low level error occurred (e.g. the connection to an
             # upstream proxy could not be established).  Send a Bad
@@ -117,7 +117,48 @@
                                 "Basic %s" % userpass))
         return headers
 
-    def open_http_connection(self, remote_url, client_request):
+
+    def open_https_connection(self, remote_address):
+        """Open a HTTPS connection to remote_address
+
+        This method handles proxies as well. Its return value is a socket like
+        object using SSL to encrypt the data."""
+        self.log_debug("Open HTTPS connection to %r" % remote_address[0])
+        sock = None
+
+        proxy = self.server.https_proxy
+        if proxy:
+            sock = connect_tcp(proxy.host, proxy.port,
+                               log_debug=self.log_debug)
+            sock = connect_http_connect(sock, remote_address[0],
+                                        remote_address[1],
+                                        self.create_proxy_headers(proxy),
+                                        debuglevel=self.debug_level)
+        else:
+            sock = connect_tcp(remote_address[0], remote_address[1],
+                               log_debug=self.log_debug)
+        return connect_ssl(sock, log_debug=self.log_debug)
+
+
+    def open_http_connection(self, remote_url, remote_address, request_uri,
+                             extra_headers):
+        """Open a HTTP connection to remote_address
+
+        This method handles proxies as well. Its return value is a socket
+        object."""
+        self.log_debug("Open HTTP connection to %r" % remote_address[0])
+
+        proxy = self.server.http_proxy
+        if proxy:
+            extra_headers.extend(self.create_proxy_headers(proxy))
+            return connect_tcp(proxy.host, proxy.port,
+                               log_debug=self.log_debug), remote_url
+        else:
+            return connect_tcp(remote_address[0], remote_address[1],
+                               log_debug=self.log_debug), request_uri
+
+
+    def perform_http_request(self, remote_url, client_request):
         """Open a HTTP connection to remote_url and send client_request
 
         This method handles both http and https URLs as well as proxies
@@ -131,34 +172,17 @@
         # but with schem and netloc removed.
         request_uri = urlparse.urlunsplit(("", "", path, query, fragment))
 
-        proxy = None
         extra_headers = [("Host", "%s:%d" % remote_address)]
 
         sock = None
 
-        if scheme == "https":
-            proxy = self.server.https_proxy
-            if proxy:
-                sock = connect_tcp(proxy.host, proxy.port,
-                                   log_debug=self.log_debug)
-                sock = connect_http_connect(sock, remote_address[0],
-                                            remote_address[1],
-                                            self.create_proxy_headers(proxy),
-                                            debuglevel=self.debug_level)
-            else:
-                sock = connect_tcp(remote_address[0], remote_address[1],
-                                   log_debug=self.log_debug)
-            sock = connect_ssl(sock, log_debug=self.log_debug)
-        elif scheme == "http":
-            proxy = self.server.http_proxy
-            if proxy:
-                sock = connect_tcp(proxy.host, proxy.port,
-                                   log_debug=self.log_debug)
-                request_uri = remote_url
-                extra_headers.extend(self.create_proxy_headers(proxy))
-            else:
-                sock = connect_tcp(remote_address[0], remote_address[1],
-                                   log_debug=self.log_debug)
+        if scheme == "http":
+            sock, request_uri = self.open_http_connection(remote_url,
+                                                          remote_address,
+                                                          request_uri,
+                                                          extra_headers)
+        elif scheme == "https":
+            sock = self.open_https_connection(remote_address)
 
         connection = SocketHTTPConnection(sock, *remote_address)
         if self.debug_level > 1:



More information about the Inteproxy-commits mailing list