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

ltucker at codespeak.net ltucker at codespeak.net
Tue Jul 3 21:42:43 CEST 2007


Author: ltucker
Date: Tue Jul  3 21:42:41 2007
New Revision: 44707

Modified:
   z3/deliverance/trunk/deliverance/resource_fetcher.py
   z3/deliverance/trunk/deliverance/wsgimiddleware.py
Log:
allow slightly broader definition of what an internal resource is

Modified: z3/deliverance/trunk/deliverance/resource_fetcher.py
==============================================================================
--- z3/deliverance/trunk/deliverance/resource_fetcher.py	(original)
+++ z3/deliverance/trunk/deliverance/resource_fetcher.py	Tue Jul  3 21:42:41 2007
@@ -21,23 +21,29 @@
         else:
             self.environ = in_environ.copy()
             
-        if not self.uri.startswith('/'):
-            self.uri = '/' + self.uri
-
+        base_url = in_environ['deliverance.base-url']
+        if not base_url.endswith('/'):
+            base_url += '/'
+        uri = urlparse.urljoin(base_url, uri)
         uri_parts = urlparse.urlparse(uri)
 
-        self.environ['PATH_INFO'] = urllib.unquote(uri_parts[2])
+        if uri.startswith(base_url):
+            script_name = urlparse.urlparse(base_url)[2]
+            path_info = uri_parts[2][len(script_name):]
+            if not path_info.startswith('/'):
+                path_info = '/%s' % path_info
+                
+            self.environ['SCRIPT_NAME'] = urllib.unquote(script_name)
+            self.environ['PATH_INFO'] = urllib.unquote(path_info)
+        else:
+            self.environ['SCRIPT_NAME'] = ''
+            self.environ['PATH_INFO'] = urllib.unquote(uri_parts[2])
+                            
         if len(uri_parts[4]) > 0: 
             self.environ['QUERY_STRING'] = uri_parts[4] + '&notheme'
         else: 
             self.environ['QUERY_STRING'] = 'notheme'
 
-        base_url = in_environ['deliverance.base-url']
-        if base_url is not None:
-            self.environ['SCRIPT_NAME'] = urllib.unquote(urlparse.urlparse(base_url)[2])
-        else: 
-            self.environ['SCRIPT_NAME'] = ''
-
         if headers_only: 
             self.environ['REQUEST_METHOD'] = 'HEAD'
         else: 
@@ -130,7 +136,6 @@
     def __init__(self, in_environ, uri, headers_only=False): 
         self.uri = uri 
         
-        url_chunks = urlparse.urlsplit(uri)
         loc = urlparse.urlsplit(uri) 
         
         self.environ = in_environ.copy() 

Modified: z3/deliverance/trunk/deliverance/wsgimiddleware.py
==============================================================================
--- z3/deliverance/trunk/deliverance/wsgimiddleware.py	(original)
+++ z3/deliverance/trunk/deliverance/wsgimiddleware.py	Tue Jul  3 21:42:41 2007
@@ -39,7 +39,9 @@
     tranformation as a WSGI middleware component. 
     """
 
-    def __init__(self, app, theme_uri, rule_uri, renderer='py', merge_cache_control=False):
+    def __init__(self, app, theme_uri, rule_uri,
+                 renderer='py', merge_cache_control=False,
+                 is_internal_uri=None):
         """
         initializer
         
@@ -50,9 +52,14 @@
           performing transformations, may be 'py' or 'xslt' or a
           Renderer class
         merge_cache_control: if set to True, the cache-control header will 
-        be calculated from the cache-control headers of all component pages 
-        during rendering. If set to False, the requested content's 
-        cache-control headers will be used. (does not affect etag merging)
+          be calculated from the cache-control headers of all component pages 
+          during rendering. If set to False, the requested content's 
+          cache-control headers will be used. (does not affect etag merging)
+        is_internal_uri: an optional predicate accepting a uri and
+          a wsgi environment. This should return true if the uri
+          should be considered 'internal'(passed to the
+          subapplication) and false if the requestshould be send
+          over the network. 
         """
         self.app = app
         self.theme_uri = theme_uri
@@ -70,6 +77,8 @@
         else:
             self._rendererType = renderer
 
+        self._is_internal_uri = is_internal_uri
+
     def get_renderer(self, environ):
         return self.create_renderer(environ)
 
@@ -322,6 +331,7 @@
         uses cache if possible. throws exception if 
         response is not 200 
         """
+        
         if uri in environ[DELIVERANCE_CACHE]: 
             response = environ[DELIVERANCE_CACHE][uri]
             if response[0].startswith('200'): 
@@ -355,21 +365,39 @@
         environ[DELIVERANCE_CACHE][uri] = (status, headers, body)
 
         return body
+
+
+    def is_internal_uri(self, uri, environ):
+        if self._is_internal_uri:
+            # specified in constructor 
+            return self._is_internal_uri(uri, environ)
+        else:
+            # default
+            internalBaseURL = environ.get(DELIVERANCE_BASE_URL)
             
+            test_uri = urlparse.urljoin(internalBaseURL, uri)
+
+            if test_uri.startswith(internalBaseURL):
+                return True
+            else:
+                return False
 
     def get_fetcher(self, environ, uri): 
         """
         retrieve an object which is appropriate for fetching the 
         uri specified. 
         """
-        internalBaseURL = environ.get(DELIVERANCE_BASE_URL,None)
-        uri = urlparse.urljoin(internalBaseURL, uri)        
-
+        
         if urlparse.urlparse(uri)[0] == 'file':
             return FileResourceFetcher(environ, uri)
 
-        elif  internalBaseURL and uri.startswith(internalBaseURL):
-            return InternalResourceFetcher(environ, uri[len(internalBaseURL):],
+        elif self.is_internal_uri(uri, environ):
+            # make it absolute
+            internalBaseURL = environ.get(DELIVERANCE_BASE_URL)
+            uri = urlparse.urljoin(internalBaseURL, uri)
+
+            return InternalResourceFetcher(environ,
+                                           uri,
                                            self.app)
         else:
 	    out_environ = self.cleaned_environ(environ)


More information about the z3-checkins mailing list