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

scm-commit@wald.intevation.org scm-commit at wald.intevation.org
Fri May 16 16:07:32 CEST 2008


Author: bernhard
Date: 2008-05-16 16:07:32 +0200 (Fri, 16 May 2008)
New Revision: 128

Modified:
   trunk/ChangeLog
   trunk/inteproxy/main.py
   trunk/inteproxy/proxyconnection.py
   trunk/inteproxy/proxycore.py
Log:
Now the inteproxy-build-in method for SSL Connect proxies
is also used in case of HTTP/1.0.

* inteproxy/proxycore.py: Removed classes HTTP10HTTPSConnection()
and HTTP10HTTPSHandler again. They are not needed for this level
and might be added to proxyconnection.py in the future.

(class InteProxyHTTPRequestHandler): New variables for 
urllib2.OpenDirector instances.

(handle_proxy_request()): Using new OpenDirector variables to "open".

* inteproxy/proxyconnection.py: New classes:
ProxyHTTPS10Connection(), ConnectHTTPS10Handler().

* inteproxy/main.py: New functions build_opener() and
eat_https_proxy_envvar() to replace now removed setup_urllib2().
Rational: We can only eat the environment variable ones.

(run_server()): Using the new functions to set the openers for
the HandlerClass.


Modified: trunk/ChangeLog
===================================================================
--- trunk/ChangeLog	2008-05-16 10:23:36 UTC (rev 127)
+++ trunk/ChangeLog	2008-05-16 14:07:32 UTC (rev 128)
@@ -1,5 +1,29 @@
 2008-05-16  Bernhard Reiter  <bernhard at intevation.de>
 
+	Now the inteproxy-build-in method for SSL Connect proxies
+	is also used in case of HTTP/1.0.
+
+	* inteproxy/proxycore.py: Removed classes HTTP10HTTPSConnection()
+	and HTTP10HTTPSHandler again. They are not needed for this level
+	and might be added to proxyconnection.py in the future.
+
+	(class InteProxyHTTPRequestHandler): New variables for 
+	urllib2.OpenDirector instances.
+
+	(handle_proxy_request()): Using new OpenDirector variables to "open".
+	
+	* inteproxy/proxyconnection.py: New classes:
+	ProxyHTTPS10Connection(), ConnectHTTPS10Handler().
+
+	* inteproxy/main.py: New functions build_opener() and
+	eat_https_proxy_envvar() to replace now removed setup_urllib2().
+	Rational: We can only eat the environment variable ones.
+
+	(run_server()): Using the new functions to set the openers for
+	the HandlerClass.
+
+2008-05-16  Bernhard Reiter  <bernhard at intevation.de>
+
 	* inteproxy/proxycore.py(handle_proxy_request()): Adding the 
 	urllib2.ProxyHandler to the used Handlers for the HTTP 1.0 case.
 

Modified: trunk/inteproxy/main.py
===================================================================
--- trunk/inteproxy/main.py	2008-05-16 10:23:36 UTC (rev 127)
+++ trunk/inteproxy/main.py	2008-05-16 14:07:32 UTC (rev 128)
@@ -46,23 +46,36 @@
     http_error_301 = http_error_303 = http_error_307 = http_error_302
 
 
-def setup_urllib2(debuglevel):
-    handlers = []
+def eat_https_proxy_envvar():
     if os.environ.has_key('https_proxy'):
         https_proxy = urlparse.urlsplit(os.getenv('https_proxy'))[1]
         del os.environ['https_proxy']
         sys.stderr.write("[%s] Using HTTPS proxy: %s\n"
                          % (log_date_time_string(), https_proxy))
-        proxy_handler = proxyconnection.ConnectHTTPSHandler(proxy=https_proxy,
-                                                            debuglevel=debuglevel)
+        return https_proxy
+    return None
+
+def build_opener(https_proxy = None, debuglevel = 0, http_vsn = 11):
+    print "called build_opener", https_proxy, debuglevel, http_vsn
+    handlers = []
+    if https_proxy:
+        sys.stderr.write("[%s] Using HTTPS proxy: %s\n"
+                         % (log_date_time_string(), https_proxy))
+        if http_vsn == 10:
+            proxy_handler = proxyconnection.ConnectHTTPS10Handler(
+                                    proxy=https_proxy, debuglevel=debuglevel)
+        else:
+            proxy_handler = proxyconnection.ConnectHTTPSHandler(
+                                    proxy=https_proxy, debuglevel=debuglevel)
         handlers.append(proxy_handler)
     else:
+        #FIXME: We would need HTTP10*Handlers here optionally to be complete
         httphandler = urllib2.HTTPHandler(debuglevel=debuglevel)
         httpshandler = urllib2.HTTPSHandler(debuglevel=debuglevel)
         handlers.extend((httphandler, httpshandler))
 
     handlers.append(HTTPRedirectHandler)
-    urllib2.install_opener(urllib2.build_opener(*handlers))
+    return(urllib2.build_opener(*handlers))
 
 
 def setup_logging(opts):
@@ -151,7 +164,9 @@
 
     setup_logging(opts)
 
-    setup_urllib2(opts.debug_level)
+    https_proxy = eat_https_proxy_envvar()
+    HandlerClass.httpopener = build_opener(https_proxy, opts.debug_level)
+    HandlerClass.http10opener = build_opener(https_proxy, opts.debug_level, 10)
 
     server_address = ('localhost', opts.port)
 

Modified: trunk/inteproxy/proxyconnection.py
===================================================================
--- trunk/inteproxy/proxyconnection.py	2008-05-16 10:23:36 UTC (rev 127)
+++ trunk/inteproxy/proxyconnection.py	2008-05-16 14:07:32 UTC (rev 128)
@@ -80,6 +80,9 @@
         ssl = socket.ssl(self.sock, self.key_file, self.cert_file)
         self.sock = httplib.FakeSocket(self.sock, ssl)
 
+class ProxyHTTPS10Connection(ProxyHTTPSConnection):
+    _http_vsn = 10
+    _http_vsn_str = 'HTTP/1.0'
 
 class ConnectHTTPHandler(urllib2.HTTPHandler):
 
@@ -104,7 +107,14 @@
             req.set_proxy(self.proxy, 'https')
         return urllib2.HTTPSHandler.do_open(self, ProxyHTTPSConnection, req)
 
+class ConnectHTTPS10Handler(ConnectHTTPSHandler):
 
+    def do_open(self, http_class, req):
+        if self.proxy is not None:
+            req.set_proxy(self.proxy, 'https')
+        return urllib2.HTTPSHandler.do_open(self, ProxyHTTPS10Connection, req)
+
+
 if __name__ == '__main__':
 
     import sys

Modified: trunk/inteproxy/proxycore.py
===================================================================
--- trunk/inteproxy/proxycore.py	2008-05-16 10:23:36 UTC (rev 127)
+++ trunk/inteproxy/proxycore.py	2008-05-16 14:07:32 UTC (rev 128)
@@ -32,23 +32,6 @@
         year, hh, mm, ss)
     return s
 
-class HTTP10HTTPSConnection(httplib.HTTPSConnection):
-    """Subclass of httplib.HTTPSConnection to force HTTP 1.0 protocol."""
-    _http_vsn = 10
-    _http_vsn_str = 'HTTP/1.0'
-
-    def __init_(self, host, port=None, key_file=None, cert_file=None,
-                     strict=None):
-        httplib.HTTPSConnection.__init__(self, host, port, key_file, cert_file,
-                        strict)
-
-
-class HTTP10HTTPSHandler(urllib2.HTTPSHandler):
-    """Subclass of HTTPSHandler forcing HTTP 1.0 protocol with httplib."""
-    def https_open(self, req):
-        return self.do_open(HTTP10HTTPSConnection, req)
-
-
 class InteProxyHTTPRequestHandler(BaseHTTPServer.BaseHTTPRequestHandler):
     # Whether the SHUTDOWN method is allowed.  If allowed, anybody who can
     # connect to the server can shutdown the server.
@@ -62,6 +45,10 @@
     #
     debug_level = 0
 
+    # urllib2.OpenDirector instances that we use to all
+    httpopener = None
+    http10opener = None
+
     def do_SHUTDOWN(self):
         """Shutdown the server if self.allow_shutdown is True (default False).
 
@@ -160,13 +147,11 @@
             # got asked as HTTP 1.0 from the client, so we need to use
             # derived classes
             if self.request_version == "HTTP/1.0":
-                self.log_debug("Trying my HTTP10HTTPSHandler.")
-                #FIXME: Do we need more handlers here? Maybe Referer?
-                opener = urllib2.build_opener(HTTP10HTTPSHandler,
-                                urllib2.ProxyHandler)
-                response = opener.open(request)
+                self.log_debug("Trying http10opener.")
+                response = self.http10opener.open(request)
             else:
-                response = urllib2.urlopen(request)
+                response = self.httpopener.open(request)
+
         except urllib2.HTTPError, err:
             # a HTTPError is a valid http response object, so we can
             # treat it like a normal response.



More information about the Inteproxy-commits mailing list