1.1 --- a/README.txt Sat Jul 16 15:01:34 2005 +0000
1.2 +++ b/README.txt Sat Jul 16 18:37:46 2005 +0000
1.3 @@ -61,6 +61,8 @@
1.4 the CGI and Webware notes.
1.5 Changed the mod_python server name method to use the server object rather than
1.6 the connection object.
1.7 +Added a parameter to the ResourceMap.MapResource class to permit automatic
1.8 +redirects into resource hierarchies when no trailing "/" was given in the URL.
1.9
1.10 New in WebStack 0.9 (Changes since WebStack 0.8)
1.11 ------------------------------------------------
2.1 --- a/WebStack/Resources/ResourceMap.py Sat Jul 16 15:01:34 2005 +0000
2.2 +++ b/WebStack/Resources/ResourceMap.py Sat Jul 16 18:37:46 2005 +0000
2.3 @@ -8,7 +8,7 @@
2.4
2.5 "A resource mapping names to other resources."
2.6
2.7 - def __init__(self, mapping):
2.8 + def __init__(self, mapping, directory_redirects=1):
2.9
2.10 """
2.11 Initialise the resource with a 'mapping' of names to resources. The
2.12 @@ -16,9 +16,14 @@
2.13 without "/" characters; the special value None is used where no name
2.14 corresponds to that used in the request path and may be used to map to
2.15 a "catch all" resource.
2.16 +
2.17 + The optional 'directory_redirects' parameter, if set to a true value (as
2.18 + is the default setting), causes a redirect adding a trailing "/"
2.19 + character if the request path does not end with such a character.
2.20 """
2.21
2.22 self.mapping = mapping
2.23 + self.directory_redirects = directory_redirects
2.24
2.25 def respond(self, trans):
2.26
2.27 @@ -31,10 +36,18 @@
2.28
2.29 parts = trans.get_virtual_path_info().split("/")
2.30
2.31 - # The first part should always be empty, and there should always be a
2.32 - # second part.
2.33 + # Where the published resource has a path info value defined (ie. its
2.34 + # path info consists of a "/" character plus some other text), the first
2.35 + # part should always be empty and there should always be a second part.
2.36 + # Where the published resource has no path info defined, there will only
2.37 + # be one part. In the latter case, we define the name to be None.
2.38
2.39 - name = parts[1]
2.40 + if len(parts) > 1:
2.41 + name = parts[1]
2.42 + elif self.directory_redirects:
2.43 + self.send_redirect(trans)
2.44 + else:
2.45 + name = None
2.46
2.47 # Get the mapped resource.
2.48
2.49 @@ -70,4 +83,19 @@
2.50 out = trans.get_response_stream()
2.51 out.write("Resource '%s' not found." % trans.get_path_info())
2.52
2.53 + def send_redirect(self, trans):
2.54 +
2.55 + """
2.56 + Send a redirect using the given 'trans', adding a "/" character to the
2.57 + end of the request path.
2.58 + """
2.59 +
2.60 + query_string = trans.get_query_string()
2.61 + if query_string:
2.62 + query_string = "?" + query_string
2.63 +
2.64 + trans.set_response_code(302)
2.65 + trans.set_header_value("Location", trans.get_path_without_query() + "/" + query_string)
2.66 + raise WebStack.Generic.EndOfResponse
2.67 +
2.68 # vim: tabstop=4 expandtab shiftwidth=4