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
2.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
2.2 +++ b/WebStack/Resources/Selectors.py Sat Jan 06 20:30:32 2007 +0000
2.3 @@ -0,0 +1,71 @@
2.4 +#!/usr/bin/env python
2.5 +
2.6 +"""
2.7 +Resources which "select" other resources, sometimes causing desirable
2.8 +side-effects.
2.9 +
2.10 +Copyright (C) 2007 Paul Boddie <paul@boddie.org.uk>
2.11 +
2.12 +This library is free software; you can redistribute it and/or
2.13 +modify it under the terms of the GNU Lesser General Public
2.14 +License as published by the Free Software Foundation; either
2.15 +version 2.1 of the License, or (at your option) any later version.
2.16 +
2.17 +This library is distributed in the hope that it will be useful,
2.18 +but WITHOUT ANY WARRANTY; without even the implied warranty of
2.19 +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
2.20 +Lesser General Public License for more details.
2.21 +
2.22 +You should have received a copy of the GNU Lesser General Public
2.23 +License along with this library; if not, write to the Free Software
2.24 +Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
2.25 +"""
2.26 +
2.27 +class PathSelector:
2.28 +
2.29 + "Set a request's current path and processed path info on an attribute."
2.30 +
2.31 + def __init__(self, resource, add_slash=1, attribute_name="root", path_encoding="utf-8"):
2.32 +
2.33 + """
2.34 + Initialise the selector with a 'resource' (to which all requests shall
2.35 + be forwarded), specifying whether a "/" character shall be added to
2.36 + stored paths using the optional 'add_slash' parameter (default is true),
2.37 + along with an optional 'attribute_name' (indicating the name of the
2.38 + attribute on which the path information shall be stored), and the
2.39 + optional 'path_encoding' for interpreting URL-encoded path values.
2.40 + """
2.41 +
2.42 + self.resource = resource
2.43 + self.add_slash = add_slash
2.44 + self.attribute_name = attribute_name
2.45 + self.path_encoding = path_encoding
2.46 +
2.47 + def _slash(self):
2.48 + if self.add_slash:
2.49 + return "/"
2.50 + else:
2.51 + return ""
2.52 +
2.53 + def respond(self, trans):
2.54 +
2.55 + """
2.56 + Respond to the transaction 'trans' by storing the current path and
2.57 + processed virtual path info on the named transaction attribute.
2.58 + """
2.59 +
2.60 + pwi = trans.get_path_without_info(self.path_encoding)
2.61 +
2.62 + # Make a note of the path given the following general rule:
2.63 + # path_without_info + path_info
2.64 + # == path_without_info + processed_virtual_path_info + virtual_path_info
2.65 +
2.66 + attributes = trans.get_attributes()
2.67 + attributes[self.attribute_name] = trans.encode_path(
2.68 + pwi + trans.get_processed_virtual_path_info(self.path_encoding) + self._slash(),
2.69 + self.path_encoding
2.70 + )
2.71 +
2.72 + self.resource.respond(trans)
2.73 +
2.74 +# vim: tabstop=4 expandtab shiftwidth=4