# HG changeset patch # User paulb # Date 1121539066 0 # Node ID 370deefd28f4e27fbc32da2edb1a5502ff86dab2 # Parent eee8cee71b66f03a1e31c99550eb1cf39b828a8e [project @ 2005-07-16 18:37:44 by paulb] Added directory redirects to the ResourceMap.MapResource. diff -r eee8cee71b66 -r 370deefd28f4 README.txt --- a/README.txt Sat Jul 16 15:01:34 2005 +0000 +++ b/README.txt Sat Jul 16 18:37:46 2005 +0000 @@ -61,6 +61,8 @@ the CGI and Webware notes. Changed the mod_python server name method to use the server object rather than the connection object. +Added a parameter to the ResourceMap.MapResource class to permit automatic +redirects into resource hierarchies when no trailing "/" was given in the URL. New in WebStack 0.9 (Changes since WebStack 0.8) ------------------------------------------------ diff -r eee8cee71b66 -r 370deefd28f4 WebStack/Resources/ResourceMap.py --- a/WebStack/Resources/ResourceMap.py Sat Jul 16 15:01:34 2005 +0000 +++ b/WebStack/Resources/ResourceMap.py Sat Jul 16 18:37:46 2005 +0000 @@ -8,7 +8,7 @@ "A resource mapping names to other resources." - def __init__(self, mapping): + def __init__(self, mapping, directory_redirects=1): """ Initialise the resource with a 'mapping' of names to resources. The @@ -16,9 +16,14 @@ without "/" characters; the special value None is used where no name corresponds to that used in the request path and may be used to map to a "catch all" resource. + + The optional 'directory_redirects' parameter, if set to a true value (as + is the default setting), causes a redirect adding a trailing "/" + character if the request path does not end with such a character. """ self.mapping = mapping + self.directory_redirects = directory_redirects def respond(self, trans): @@ -31,10 +36,18 @@ parts = trans.get_virtual_path_info().split("/") - # The first part should always be empty, and there should always be a - # second part. + # Where the published resource has a path info value defined (ie. its + # path info consists of a "/" character plus some other text), the first + # part should always be empty and there should always be a second part. + # Where the published resource has no path info defined, there will only + # be one part. In the latter case, we define the name to be None. - name = parts[1] + if len(parts) > 1: + name = parts[1] + elif self.directory_redirects: + self.send_redirect(trans) + else: + name = None # Get the mapped resource. @@ -70,4 +83,19 @@ out = trans.get_response_stream() out.write("Resource '%s' not found." % trans.get_path_info()) + def send_redirect(self, trans): + + """ + Send a redirect using the given 'trans', adding a "/" character to the + end of the request path. + """ + + query_string = trans.get_query_string() + if query_string: + query_string = "?" + query_string + + trans.set_response_code(302) + trans.set_header_value("Location", trans.get_path_without_query() + "/" + query_string) + raise WebStack.Generic.EndOfResponse + # vim: tabstop=4 expandtab shiftwidth=4