# HG changeset patch # User paulb # Date 1124976580 0 # Node ID 310b3771e82fa91cf1d4ad55b1cbbb1c03a3cd68 # Parent b5ffb29b57647dce62d8f6705112121005aa7564 [project @ 2005-08-25 13:29:28 by paulb] Introduced an encoding parameter to the get_virtual_path_info and get_processed_virtual_path_info methods on Transaction. diff -r b5ffb29b5764 -r 310b3771e82f WebStack/Generic.py --- a/WebStack/Generic.py Wed Aug 24 23:42:32 2005 +0000 +++ b/WebStack/Generic.py Thu Aug 25 13:29:40 2005 +0000 @@ -640,26 +640,34 @@ self.path_info = path_info - def get_virtual_path_info(self): + def get_virtual_path_info(self, encoding=None): """ An application-specific method which either returns path info set in the 'set_virtual_path_info' method, or the normal path info found in the request. + + If the optional 'encoding' is set, use that in preference to the default + encoding to convert the path into a form not containing "URL encoded" + character values. """ if self.path_info is not None: return self.path_info else: - return self.get_path_info() + return self.get_path_info(encoding) - def get_processed_virtual_path_info(self): + def get_processed_virtual_path_info(self, encoding=None): """ An application-specific method which returns the virtual path info that is considered "processed"; that is, the part of the path info which is not included in the virtual path info. + If the optional 'encoding' is set, use that in preference to the default + encoding to convert the path into a form not containing "URL encoded" + character values. + Where the virtual path info is identical to the path info, an empty string is returned. @@ -670,8 +678,8 @@ of the path info, the entire path info is returned. """ - real_path_info = self.get_path_info() - virtual_path_info = self.get_virtual_path_info() + real_path_info = self.get_path_info(encoding) + virtual_path_info = self.get_virtual_path_info(encoding) if virtual_path_info == "": return real_path_info diff -r b5ffb29b5764 -r 310b3771e82f WebStack/Resources/ResourceMap.py --- a/WebStack/Resources/ResourceMap.py Wed Aug 24 23:42:32 2005 +0000 +++ b/WebStack/Resources/ResourceMap.py Thu Aug 25 13:29:40 2005 +0000 @@ -26,7 +26,7 @@ "A resource mapping names to other resources." - def __init__(self, mapping, pass_through=0, directory_redirects=1): + def __init__(self, mapping, pass_through=0, directory_redirects=1, urlencoding=None): """ Initialise the resource with a 'mapping' of names to resources. The @@ -72,11 +72,16 @@ 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. + + The optional 'urlencoding' is used to decode "URL encoded" character + values in the request path, and overrides the default encoding wherever + possible. """ self.mapping = mapping self.pass_through = pass_through self.directory_redirects = directory_redirects + self.urlencoding = urlencoding def respond(self, trans): @@ -87,7 +92,7 @@ # Get the path info. - parts = trans.get_virtual_path_info().split("/") + parts = trans.get_virtual_path_info(self.urlencoding).split("/") # Where the published resource has a path info value defined (ie. its # path info consists of a "/" character plus some other text), the first diff -r b5ffb29b5764 -r 310b3771e82f WebStack/Resources/Static.py --- a/WebStack/Resources/Static.py Wed Aug 24 23:42:32 2005 +0000 +++ b/WebStack/Resources/Static.py Thu Aug 25 13:29:40 2005 +0000 @@ -27,7 +27,7 @@ "A resource serving the contents of a filesystem directory." - def __init__(self, directory, media_types=None, unrecognised_media_type="application/data"): + def __init__(self, directory, media_types=None, unrecognised_media_type="application/data", urlencoding=None): """ Initialise the resource to serve files from the given 'directory'. @@ -45,17 +45,22 @@ Where no media type can be found for a resource, a predefined media type is set which can be overridden by specifying a value for the optional 'unrecognised_media_type' parameter. + + The optional 'urlencoding' is used to decode "URL encoded" character + values in the request path, and overrides the default encoding wherever + possible. """ self.directory = directory self.media_types = media_types or {} self.unrecognised_media_type = unrecognised_media_type + self.urlencoding = urlencoding def respond(self, trans): "Respond to the given transaction, 'trans', by serving a file." - parts = trans.get_virtual_path_info().split("/") + parts = trans.get_virtual_path_info(self.urlencoding).split("/") filename = parts[1] out = trans.get_response_stream() diff -r b5ffb29b5764 -r 310b3771e82f docs/path-info.html --- a/docs/path-info.html Wed Aug 24 23:42:32 2005 +0000 +++ b/docs/path-info.html Thu Aug 25 13:29:40 2005 +0000 @@ -4,7 +4,6 @@ Paths To and Within Applications -

Paths To and Within Applications

@@ -54,7 +53,9 @@ - the application itself decides the scope of the path and can set the "virtual path info" using the set_virtual_path_info method. The path should either contain a leading / -character optionally followed by other characters, or an empty string. +character optionally followed by other characters, or an empty string.
+ +An optional encoding parameter may be used to assist the process of converting the path to a Unicode object - see "Character Encodings" for more information.

Choosing the Right Path Value

diff -r b5ffb29b5764 -r 310b3771e82f examples/Common/Calendar/__init__.py --- a/examples/Common/Calendar/__init__.py Wed Aug 24 23:42:32 2005 +0000 +++ b/examples/Common/Calendar/__init__.py Thu Aug 25 13:29:40 2005 +0000 @@ -16,6 +16,7 @@ """ resource_dir = os.path.join(os.path.split(__file__)[0], "calendars") + urlencoding = "utf-8" def __init__(self): if not os.path.exists(self.resource_dir): @@ -40,7 +41,7 @@ # NOTE: We always assume that calendar files are being uploaded. content_type = trans.get_content_type() - calendar_name = trans.get_virtual_path_info().split("/")[-1] + calendar_name = trans.get_virtual_path_info(self.urlencoding).split("/")[-1] # Handle uploads. @@ -68,7 +69,7 @@ """) - if trans.get_virtual_path_info() == "/": + if trans.get_virtual_path_info(self.urlencoding) == "/": time_now = time.strftime("%Y-%m-%dT%TZ", time.gmtime(time.time())) out.write(""" diff -r b5ffb29b5764 -r 310b3771e82f examples/Common/Simple/__init__.py --- a/examples/Common/Simple/__init__.py Wed Aug 24 23:42:32 2005 +0000 +++ b/examples/Common/Simple/__init__.py Thu Aug 25 13:29:40 2005 +0000 @@ -90,8 +90,8 @@ trans.get_path("utf-8"), trans.get_path_without_query("utf-8"), trans.get_path_info("utf-8"), - trans.get_virtual_path_info(), - trans.get_processed_virtual_path_info(), + trans.get_virtual_path_info("utf-8"), + trans.get_processed_virtual_path_info("utf-8"), trans.get_query_string(), trans.decode_path(trans.get_query_string(), "utf-8"), trans.get_server_name(),