1.1 --- a/WebStack/Resources/ResourceMap.py Sat Jan 06 20:29:51 2007 +0000
1.2 +++ b/WebStack/Resources/ResourceMap.py Sat Jan 06 20:30:32 2007 +0000
1.3 @@ -3,7 +3,7 @@
1.4 """
1.5 Mapping from names to resources.
1.6
1.7 -Copyright (C) 2004, 2005 Paul Boddie <paul@boddie.org.uk>
1.8 +Copyright (C) 2004, 2005, 2007 Paul Boddie <paul@boddie.org.uk>
1.9
1.10 This library is free software; you can redistribute it and/or
1.11 modify it under the terms of the GNU Lesser General Public
1.12 @@ -26,7 +26,9 @@
1.13
1.14 "A resource mapping names to other resources."
1.15
1.16 - def __init__(self, mapping, pass_through=0, directory_redirects=1, urlencoding="utf-8"):
1.17 + path_encoding = "utf-8"
1.18 +
1.19 + def __init__(self, mapping, pass_through=0, directory_redirects=1, path_encoding=None, urlencoding=None):
1.20
1.21 """
1.22 Initialise the resource with a 'mapping' of names to resources. The
1.23 @@ -38,7 +40,9 @@
1.24 mapping is {"mammals" : ..., "reptiles" : ..., None : ...}
1.25
1.26 /mammals/cat -> matches "mammals"
1.27 +
1.28 /reptiles/python -> matches "reptiles"
1.29 +
1.30 /creatures/goblin -> no match, handled by None
1.31
1.32 When this resource matches a name in the virtual path info to one of the
1.33 @@ -67,21 +71,22 @@
1.34 resource is set as its own "catch all" resource. For example:
1.35
1.36 map_resource = MapResource(...)
1.37 +
1.38 map_resource.mapping[None] = map_resource
1.39
1.40 The optional 'directory_redirects' parameter, if set to a true value (as
1.41 is the default setting), causes a redirect adding a trailing "/"
1.42 character if the request path does not end with such a character.
1.43
1.44 - The optional 'urlencoding' is used to decode "URL encoded" character
1.45 - values in the request path, and overrides the default encoding wherever
1.46 - possible.
1.47 + The optional 'path_encoding' (for which 'urlencoding' is a synonym) is
1.48 + used to decode "URL encoded" character values in the request path, and
1.49 + overrides the default encoding wherever possible.
1.50 """
1.51
1.52 self.mapping = mapping
1.53 self.pass_through = pass_through
1.54 self.directory_redirects = directory_redirects
1.55 - self.urlencoding = urlencoding
1.56 + self.path_encoding = path_encoding or urlencoding or self.path_encoding
1.57
1.58 def respond(self, trans):
1.59
1.60 @@ -92,7 +97,7 @@
1.61
1.62 # Get the path info.
1.63
1.64 - parts = trans.get_virtual_path_info(self.urlencoding).split("/")
1.65 + parts = trans.get_virtual_path_info(self.path_encoding).split("/")
1.66
1.67 # Where the published resource has a path info value defined (ie. its
1.68 # path info consists of a "/" character plus some other text), the first
1.69 @@ -150,7 +155,7 @@
1.70 trans.set_response_code(404)
1.71 trans.set_content_type(WebStack.Generic.ContentType("text/plain"))
1.72 out = trans.get_response_stream()
1.73 - out.write("Resource '%s' not found." % trans.get_path_info(self.urlencoding))
1.74 + out.write("Resource '%s' not found." % trans.get_path_info(self.path_encoding))
1.75
1.76 def send_redirect(self, trans):
1.77
1.78 @@ -159,10 +164,10 @@
1.79 end of the request path.
1.80 """
1.81
1.82 - path_without_query = trans.get_path_without_query(self.urlencoding)
1.83 + path_without_query = trans.get_path_without_query(self.path_encoding)
1.84 query_string = trans.get_query_string()
1.85 if query_string:
1.86 query_string = "?" + query_string
1.87 - trans.redirect(trans.encode_path(path_without_query, self.urlencoding) + "/" + query_string)
1.88 + trans.redirect(trans.encode_path(path_without_query, self.path_encoding) + "/" + query_string)
1.89
1.90 # vim: tabstop=4 expandtab shiftwidth=4