[z3-checkins] r40784 - z3/deliverance/trunk/deliverance

ianb at codespeak.net ianb at codespeak.net
Mon Mar 19 18:09:42 CET 2007


Author: ianb
Date: Mon Mar 19 18:09:40 2007
New Revision: 40784

Modified:
   z3/deliverance/trunk/deliverance/proxyapp.py
Log:
Added a better / more complete entry point for setting up the proxy

Modified: z3/deliverance/trunk/deliverance/proxyapp.py
==============================================================================
--- z3/deliverance/trunk/deliverance/proxyapp.py	(original)
+++ z3/deliverance/trunk/deliverance/proxyapp.py	Mon Mar 19 18:09:40 2007
@@ -3,7 +3,12 @@
 passing the request to another HTTP server
 """
 
+import os
+import urlparse
 from paste.proxy import TransparentProxy
+from paste.urlmap import URLMap
+from paste.urlparser import StaticURLParser
+from paste.exceptions import errormiddleware
 from deliverance.wsgimiddleware import DeliveranceMiddleware
 from deliverance.relocateresponse import RelocateMiddleware
 
@@ -53,6 +58,25 @@
             environ, start_response)
     
 
+class ProxyMountedDeliveranceApp(ProxyDeliveranceApp):
+
+    def __init__(self, *args, **kw):
+        try:
+            mount_points = kw.pop('mount_points')
+        except KeyError:
+            mount_points = {}
+        self.mount_points = mount_points
+        ProxyDeliveranceApp.__init__(self, *args, **kw)
+
+    def make_app(self):
+        normal_app = ProxyDeliveranceApp.make_app(self)
+        from paste.urlmap import URLMap
+        urlmap = URLMap()
+        for name, value in self.mount_points.items():
+            urlmap[name] = value
+        urlmap['/'] = normal_app
+        return urlmap
+
 class DebugHeaders(object):
 
     translate_keys = {'CONTENT_LENGTH': 'HTTP_CONTENT_LENGTH',
@@ -77,3 +101,55 @@
                 print '  %s: %s' % (name.title(), value)
             start_response(status, headers, exc_info)
         return self.app(environ, repl_start_response)
+
+
+def make_proxy(global_conf,
+               wrap_href, theme_uri, rule_uri,
+               renderer='py', transparent=False, debug_headers=False,
+               relocate_content=False,
+               merge_cache_control=False,
+               **kw):
+    from paste.deploy.converters import asbool
+    mount_points = {}
+    for name, value in kw.items():
+        if name.startswith('mount '):
+            path = name[len('mount '):].strip()
+            if not path:
+                raise ValueError('Bad path: %r (in %r)' % (path, name))
+            mount_points[path] = StaticURLParser(os.path.abspath(value))
+        else:
+            raise ValueError(
+                "Unexpected configuration key: %r" % name)
+    if not wrap_href.startswith('http:') or wrap_href.startswith('https:'):
+        wrap_href = 'http://' + wrap_href.lstrip('/')
+    parts = urlparse.urlsplit(wrap_href)
+    scheme, netloc, path, query, fragment = parts
+    scheme = scheme.lower()
+    if scheme not in ['http', 'https']:
+        raise ValueError(
+            "I don't know how to proxy to the scheme %r (from wrap_href=%s)"
+            % (scheme, wrap_href))
+    if fragment:
+        raise ValueError(
+            "You cannot use a fragment (%r) in wrap_href=%s"
+            % (fragment, wrap_href))
+    if query:
+        raise ValueError(
+            "You cannot use a query string ?%s (from wrap_href=%s)"
+            % (query, wrap_href))
+    if path and path != '/':
+        raise ValueError(
+            "Proxying to a path on a server is not currently supported "
+            "(path=%r from wrap_href=%s)"
+            % (path, wrap_href))
+    app = ProxyMountedDeliveranceApp(
+        theme_uri=theme_uri,
+        rule_uri=rule_uri,
+        proxy=netloc,
+        transparent=asbool(transparent),
+        debug_headers=asbool(debug_headers),
+        relocate_content=asbool(relocate_content),
+        renderer=renderer,
+        mount_points=mount_points)
+    app = errormiddleware.make_error_middleware(app, global_conf)
+    return app


More information about the z3-checkins mailing list