[Inteproxy-commits] r169 - in trunk: . inteproxy test

scm-commit@wald.intevation.org scm-commit at wald.intevation.org
Tue Nov 25 19:54:57 CET 2008


Author: bh
Date: 2008-11-25 19:54:56 +0100 (Tue, 25 Nov 2008)
New Revision: 169

Modified:
   trunk/ChangeLog
   trunk/inteproxy/main.py
   trunk/inteproxy/proxycore.py
   trunk/inteproxy/transcoder.py
   trunk/test/test_inteproxy.py
Log:
* inteproxy/proxycore.py (MasterWorkerServer.__init__): Rename the
proxy parameters and instance variables.  Their values are no
longer strings with urls but objects with attributes.
(InteProxyHTTPRequestHandler.open_http_connection): Adapt to the
server attribute renaming and new proxy description objects

* inteproxy/main.py (handle_proxy_config): New.  Handles proxy
configuration which may now come from a config file and from
environment variables.
(run_server): Read the configuration file using
inteproxy.config.read_config and pass it's hosts settings to the
transcoder map.  Also, use handle_proxy_config to create the proxy
settings to pass to the server object.

* inteproxy/transcoder.py (TranscoderMap.add_hosts): New.  Adds
host description from a list of objects as returned by the
inteproxy.config module

* test/test_inteproxy.py (ServerTest.setUp): Adapt to proxy config
changes


Modified: trunk/ChangeLog
===================================================================
--- trunk/ChangeLog	2008-11-25 16:59:04 UTC (rev 168)
+++ trunk/ChangeLog	2008-11-25 18:54:56 UTC (rev 169)
@@ -1,5 +1,28 @@
 2008-11-25  Bernhard Herzog  <bh at intevation.de>
 
+	* inteproxy/proxycore.py (MasterWorkerServer.__init__): Rename the
+	proxy parameters and instance variables.  Their values are no
+	longer strings with urls but objects with attributes.
+	(InteProxyHTTPRequestHandler.open_http_connection): Adapt to the
+	server attribute renaming and new proxy description objects
+
+	* inteproxy/main.py (handle_proxy_config): New.  Handles proxy
+	configuration which may now come from a config file and from
+	environment variables.
+	(run_server): Read the configuration file using
+	inteproxy.config.read_config and pass it's hosts settings to the
+	transcoder map.  Also, use handle_proxy_config to create the proxy
+	settings to pass to the server object.
+
+	* inteproxy/transcoder.py (TranscoderMap.add_hosts): New.  Adds
+	host description from a list of objects as returned by the
+	inteproxy.config module
+
+	* test/test_inteproxy.py (ServerTest.setUp): Adapt to proxy config
+	changes
+
+2008-11-25  Bernhard Herzog  <bh at intevation.de>
+
 	* inteproxy/config.py: New module to read the config file.  The
 	config file now contains optional proxy settings
 

Modified: trunk/inteproxy/main.py
===================================================================
--- trunk/inteproxy/main.py	2008-11-25 16:59:04 UTC (rev 168)
+++ trunk/inteproxy/main.py	2008-11-25 18:54:56 UTC (rev 169)
@@ -17,6 +17,7 @@
 import inteproxy.proxyconnection as proxyconnection
 import inteproxy.transcoder
 import inteproxy.resources
+import inteproxy.config
 from inteproxy.proxycore import (InteProxyHTTPRequestHandler,
                                  MasterWorkerServer,
                                  log_date_time_string)
@@ -76,7 +77,35 @@
             if isinstance(lang, str):
                 os.environ["LANG"] = lang
 
+def handle_proxy_config(config, proxy_type):
+    """Extracts the proxy settings for a given proxy type.
+    Parameters:
+        config -- The configuration returned by the read_config function
+        proxy_type -- Either 'http_proxy' or 'https_proxy'
 
+    If config's attribute named by proxy_type is not None, the function
+    returns that.  Otherwise, if the environment variable named by
+    proxy_type is set, extract the proxy settings from that and return
+    it as a proxy settings object.  Otherwise the function returns None.
+    """
+    proxy = getattr(config, proxy_type)
+
+    if proxy is None:
+        proxy_env = os.environ.get(proxy_type)
+        if proxy_env:
+            netloc = urlparse.urlsplit(proxy_env)[1]
+            if ":" in netloc:
+                hostname, port = netloc.split(":", 1)
+                port = int(port)
+            else:
+                hostname = netloc
+                port = 80
+            proxy = inteproxy.config.Struct(host=hostname, port=port,
+                                            username=None, password=None)
+
+    return proxy
+
+
 # The main application object.  It's a global variable so that worker
 # threads can easily get access to it.
 the_application = None
@@ -102,10 +131,12 @@
     parser.add_option("--config-file")
     opts, rest = parser.parse_args()
 
+    config = inteproxy.config.read_config(opts.config_file)
+
     HandlerClass.debug_level = opts.debug_level
 
     transcoder_map = inteproxy.transcoder.create_transcoder_map()
-    transcoder_map.read_config(opts.config_file)
+    transcoder_map.add_hosts(config.hosts)
 
     setup_language()
 
@@ -116,10 +147,10 @@
     sys.stderr.write("InteProxy Version %s\n" % inteproxy_version)
     sys.stderr.write("[%s] server starting up\n" % log_date_time_string())
 
-    http_proxy = os.environ.get("http_proxy")
-    https_proxy = os.environ.get("https_proxy")
     httpd = ServerClass(server_address, HandlerClass, opts.workers,
-                        http_proxy, https_proxy, transcoder_map=transcoder_map,
+                        handle_proxy_config(config, "http_proxy"),
+                        handle_proxy_config(config, "https_proxy"),
+                        transcoder_map=transcoder_map,
                         rewrite_urls=opts.rewrite_urls)
 
     # import the gtkapp here instead of at top-level to avoid loading

Modified: trunk/inteproxy/proxycore.py
===================================================================
--- trunk/inteproxy/proxycore.py	2008-11-25 16:59:04 UTC (rev 168)
+++ trunk/inteproxy/proxycore.py	2008-11-25 18:54:56 UTC (rev 169)
@@ -118,13 +118,15 @@
         request_uri = urlparse.urlunsplit(("", "", path, query, fragment))
         if scheme == "http":
             connection_class = httplib.HTTPConnection
-            if self.server.http_proxy_url:
-                netloc = urlparse.urlsplit(self.server.http_proxy_url)[1]
+            if self.server.http_proxy:
+                proxy = self.server.http_proxy
+                netloc = "%s:%d" % (proxy.host, proxy.port)
                 request_uri = remote_url
         elif scheme == "https":
             connection_class = httplib.HTTPSConnection
-            if self.server.https_proxy_url:
-                netloc = urlparse.urlsplit(self.server.https_proxy_url)[1]
+            if self.server.https_proxy:
+                proxy = self.server.https_proxy
+                netloc = "%s:%d" % (proxy.host, proxy.port)
                 request_uri = remote_url
                 connection_class = HTTPSProxyConnection
 
@@ -286,11 +288,11 @@
     do_shutdown = False
 
     def __init__(self, server_address, RequestHandlerClass, num_workers,
-                 http_proxy_url, https_proxy_url, transcoder_map,
+                 http_proxy, https_proxy, transcoder_map,
                  rewrite_urls=False):
         HTTPServer.__init__(self, server_address, RequestHandlerClass)
-        self.http_proxy_url = http_proxy_url
-        self.https_proxy_url = https_proxy_url
+        self.http_proxy = http_proxy
+        self.https_proxy = https_proxy
         self.transcoder_map = transcoder_map
         self.rewrite_urls = rewrite_urls
         self.thread_pool = ThreadPool(num_workers, lambda f: f())

Modified: trunk/inteproxy/transcoder.py
===================================================================
--- trunk/inteproxy/transcoder.py	2008-11-25 16:59:04 UTC (rev 168)
+++ trunk/inteproxy/transcoder.py	2008-11-25 18:54:56 UTC (rev 169)
@@ -191,6 +191,10 @@
         """
         self.hostmap[(host, path)] = classname
 
+    def add_hosts(self, hosts):
+        for entry in hosts:
+            self.add_host(entry.host, entry.path, entry.classname)
+
     def lookup(self, method, host, path):
         """Returns the python class implementing the transcoder for path on host
         """

Modified: trunk/test/test_inteproxy.py
===================================================================
--- trunk/test/test_inteproxy.py	2008-11-25 16:59:04 UTC (rev 168)
+++ trunk/test/test_inteproxy.py	2008-11-25 18:54:56 UTC (rev 169)
@@ -16,6 +16,7 @@
 
 from inteproxy.proxycore import MasterWorkerServer, InteProxyHTTPRequestHandler
 from inteproxy.transcoder import create_transcoder_map
+from inteproxy.config import Struct
 import inteproxy.httpserver as httpserver
 
 
@@ -43,14 +44,15 @@
         if http_proxy:
             self.http_proxy = httpserver.ServerThread(http_proxy)
             self.http_proxy.start(daemon=True)
-            self.http_proxy_url = "http://localhost:%d/" \
-                                  % (self.http_proxy.server_port,)
+            self.http_proxy_desc = Struct(host="localhost",
+                                          port=self.http_proxy.server_port,
+                                          username=None, password=None)
         else:
-            self.http_proxy = self.http_proxy_url = None
+            self.http_proxy = self.http_proxy_desc = None
 
         proxyserver = MasterWorkerServer(("127.0.0.1", 0),
                                          InteProxyHTTPRequestHandler, 5,
-                                         self.http_proxy_url, None,
+                                         self.http_proxy_desc, None,
                                    transcoder_map=self.create_transcoder_map(),
                                          rewrite_urls=self.rewrite_urls)
         self.server = httpserver.ServerThread(proxyserver)



More information about the Inteproxy-commits mailing list