[z3-checkins] r42038 - in z3/deliverance/DeliveranceVHoster/trunk: docs dvhoster

ianb at codespeak.net ianb at codespeak.net
Fri Apr 13 21:22:58 CEST 2007


Author: ianb
Date: Fri Apr 13 21:22:56 2007
New Revision: 42038

Added:
   z3/deliverance/DeliveranceVHoster/trunk/dvhoster/uritemplate.py   (contents, props changed)
Modified:
   z3/deliverance/DeliveranceVHoster/trunk/docs/rest-api.txt
   z3/deliverance/DeliveranceVHoster/trunk/dvhoster/dispatcher.py
Log:
Allow URI templates for remote_uri

Modified: z3/deliverance/DeliveranceVHoster/trunk/docs/rest-api.txt
==============================================================================
--- z3/deliverance/DeliveranceVHoster/trunk/docs/rest-api.txt	(original)
+++ z3/deliverance/DeliveranceVHoster/trunk/docs/rest-api.txt	Fri Apr 13 21:22:56 2007
@@ -90,6 +90,32 @@
   can do this with a `hook <hook.html>`_, or otherwise all requests
   (except to this configuration API) will fail.
 
+  ``remote_uri`` may be a `URI Template
+  <http://bitworking.org/projects/URI-Templates/draft-gregorio-uritemplate-00.html>`_,
+  meaning that variables can be put in ``{varname}}.  These variables
+  are available:
+
+  ``{host}``:
+    The host *and* port of the request (e.g., ``foo.com:80``).
+
+  ``{domain}``:
+    The host without port
+
+  ``{port}``:
+    The port
+
+  ``{scheme}``:
+    The scheme, like ``http`` or ``https``
+
+  ``{path}``:
+    The full path of the request
+
+  ``{script_name}``:
+    The SCRIPT_NAME (typically empty)
+
+  ``{path_info}``:
+    The PATH_INFO (typically the same as ``{path}``)
+
 ``/.deliverance/remote_uris?{add or remove}``
 
   You can post to one of these resources to add or remove items from

Modified: z3/deliverance/DeliveranceVHoster/trunk/dvhoster/dispatcher.py
==============================================================================
--- z3/deliverance/DeliveranceVHoster/trunk/dvhoster/dispatcher.py	(original)
+++ z3/deliverance/DeliveranceVHoster/trunk/dvhoster/dispatcher.py	Fri Apr 13 21:22:56 2007
@@ -15,6 +15,7 @@
 from dvhoster.debuginterp import Renderer
 from dvhoster.util import load_func
 from dvhoster.logcreate import make_logger
+from dvhoster import uritemplate
 
 def norm_path(urlpath):
     if not urlpath:
@@ -112,6 +113,17 @@
                                          for r in remote_uris])))
             return exc(environ, start_response)
 
+        vars = {
+            'host': environ['HTTP_HOST'],
+            'domain': environ['HTTP_HOST'].split(':', 1)[0],
+            'scheme': environ['wsgi.url_scheme'],
+            'port': environ['HTTP_HOST'].split(':', 1)[1],
+            'path': environ['SCRIPT_NAME'] + environ['PATH_INFO'],
+            'script_name': environ['SCRIPT_NAME'],
+            'path_info': environ['PATH_INFO'],
+            }
+        remote_uri = uritemplate.sub_vars(remote_uri, vars)
+        
         environ['dvhoster.remote_uri'] = remote_uri
 
         should_theme_uri = True
@@ -163,6 +175,15 @@
             if self.clean_environ_headers_regex.search(key):
                 # @@: Should log this
                 del environ[key]
+        host = environ.get('HTTP_HOST')
+        if host is None:
+            host = environ['SERVER_NAME'] + ':' + environ['SERVER_PORT']
+        elif ':' not in host:
+            if environ['wsgi.url_scheme'] == 'https':
+                host += ':443'
+            else:
+                host += ':80'
+        environ['HTTP_HOST'] = host
 
     def get_domain_info(self, environ):
         """

Added: z3/deliverance/DeliveranceVHoster/trunk/dvhoster/uritemplate.py
==============================================================================
--- (empty file)
+++ z3/deliverance/DeliveranceVHoster/trunk/dvhoster/uritemplate.py	Fri Apr 13 21:22:56 2007
@@ -0,0 +1,26 @@
+"""
+Substitutes URL templates
+"""
+
+import re
+import urllib
+
+template_re = re.compile(r'[{](.*?)[}]')
+
+def sub_vars(uri, vars, default=None):
+    if default is None:
+        def subber(match):
+            try:
+                return urllib.quote(str(vars[match.group(1)]))
+            except KeyError:
+                raise KeyError(
+                    "Variable %s in template %r not found (from variables %s)"
+                    % (match.group(1) or '(empty)',
+                       uri,
+                       ', '.join(map(str, vars.keys()))))
+    else:
+        def subber(match):
+            return urllib.quote(str(vars.get(match.group(1), default)))
+    return template_re.sub(subber, uri)
+
+                


More information about the z3-checkins mailing list