# HG changeset patch # User paulb # Date 1124820510 0 # Node ID 5c5389988b5c44d7b70fc87ea20ec563e93d1f54 # Parent bad3608128f43f0be0a47538a0740882a23b0a4a [project @ 2005-08-23 18:08:25 by paulb] Changed get_fields_from_path and added convenience decoding/encoding methods. diff -r bad3608128f4 -r 5c5389988b5c README.txt --- a/README.txt Tue Aug 23 18:06:18 2005 +0000 +++ b/README.txt Tue Aug 23 18:08:30 2005 +0000 @@ -55,6 +55,8 @@ New in WebStack 1.0 (Changes since WebStack 0.10) ------------------------------------------------- +Changed the behaviour of get_fields_from_path to return Unicode data decoded +using the optional encoding parameter or a common default encoding. Added Debian package support. Added automatic session directory creation for the WebStack sessions implementation. @@ -67,8 +69,9 @@ from Scott Robinson). Fixed ResourceMap.MapResource to handle non-existent resources properly (where the virtual path info is only one component in length). -Added a convenience method to Transaction for the decoding of path values and -the production of Unicode objects - see the get_readable_path method. +Added convenience methods to Transaction for the decoding and encoding of path +values (to and from Unicode objects) - see the decode_path and encode_path +methods. New in WebStack 0.10 (Changes since WebStack 0.9) ------------------------------------------------- diff -r bad3608128f4 -r 5c5389988b5c WebStack/BaseHTTPRequestHandler.py --- a/WebStack/BaseHTTPRequestHandler.py Tue Aug 23 18:06:18 2005 +0000 +++ b/WebStack/BaseHTTPRequestHandler.py Tue Aug 23 18:08:30 2005 +0000 @@ -233,12 +233,14 @@ # Higher level request-related methods. - def get_fields_from_path(self): + def get_fields_from_path(self, encoding=None): """ Extracts fields (or request parameters) from the path specified in the transaction. The underlying framework may refuse to supply fields from - the path if handling a POST transaction. + the path if handling a POST transaction. The optional 'encoding' + parameter specifies the character encoding of the query string for cases + where the default encoding is to be overridden. Returns a dictionary mapping field names to lists of values (even if a single value is associated with any given field name). @@ -248,9 +250,11 @@ fields = {} for name, values in parse_qs(self.get_query_string(), keep_blank_values=1).items(): + name = self.decode_path(name, encoding) fields[name] = [] for value in values: - fields[name].append(unicode(value, "iso-8859-1")) + value = self.decode_path(value, encoding) + fields[name].append(value) return fields def get_fields_from_body(self, encoding=None): diff -r bad3608128f4 -r 5c5389988b5c WebStack/CGI.py --- a/WebStack/CGI.py Tue Aug 23 18:06:18 2005 +0000 +++ b/WebStack/CGI.py Tue Aug 23 18:08:30 2005 +0000 @@ -219,24 +219,26 @@ # Higher level request-related methods. - def get_fields_from_path(self): + def get_fields_from_path(self, encoding=None): """ Extracts fields (or request parameters) from the path specified in the transaction. The underlying framework may refuse to supply fields from - the path if handling a POST transaction. + the path if handling a POST transaction. The optional 'encoding' + parameter specifies the character encoding of the query string for cases + where the default encoding is to be overridden. Returns a dictionary mapping field names to lists of values (even if a single value is associated with any given field name). """ - # NOTE: Support at best ISO-8859-1 values. - fields = {} for name, values in parse_qs(self.get_query_string(), keep_blank_values=1).items(): + name = self.decode_path(name, encoding) fields[name] = [] for value in values: - fields[name].append(unicode(value, "iso-8859-1")) + value = self.decode_path(value, encoding) + fields[name].append(value) return fields def get_fields_from_body(self, encoding=None): diff -r bad3608128f4 -r 5c5389988b5c WebStack/Generic.py --- a/WebStack/Generic.py Tue Aug 23 18:06:18 2005 +0000 +++ b/WebStack/Generic.py Tue Aug 23 18:08:30 2005 +0000 @@ -257,18 +257,37 @@ # Public utility methods. - def get_readable_path(self, path, encoding): + def decode_path(self, path, encoding=None): """ - From the given 'path', use the given 'encoding' to decode the information and - convert it to Unicode. Upon success, return the 'path' as a Unicode value. Upon - failure, return None. + From the given 'path', use the optional 'encoding' (if specified) to decode the + information and convert it to Unicode. Upon failure for a specified 'encoding' + or where 'encoding' is not specified, use the default character encoding to + perform the conversion. + + Returns the 'path' as a Unicode value without "URL encoded" character values. """ - try: - return urllib.unquote(path).decode(encoding) - except UnicodeError: - return None + unquoted_path = urllib.unquote(path) + if encoding is not None: + try: + return unquoted_path.decode(encoding) + except UnicodeError: + pass + return unquoted_path.decode(self.default_charset) + + def encode_path(self, path, encoding=None): + + """ + Encode the given 'path', using the optional 'encoding' (if specified) or the + default encoding where 'encoding' is not specified, and produce a suitable "URL + encoded" string. + """ + + if encoding is not None: + return urllib.quote(path.encode(encoding)) + else: + return urllib.quote(path.encode(self.default_charset)) # Server-related methods. @@ -381,12 +400,14 @@ # Higher level request-related methods. - def get_fields_from_path(self): + def get_fields_from_path(self, encoding=None): """ Extracts fields (or request parameters) from the path specified in the transaction. The underlying framework may refuse to supply fields from - the path if handling a POST transaction. + the path if handling a POST transaction. The optional 'encoding' + parameter specifies the character encoding of the query string for cases + where the default encoding is to be overridden. Returns a dictionary mapping field names to lists of values (even if a single value is associated with any given field name). diff -r bad3608128f4 -r 5c5389988b5c WebStack/JavaServlet.py --- a/WebStack/JavaServlet.py Tue Aug 23 18:06:18 2005 +0000 +++ b/WebStack/JavaServlet.py Tue Aug 23 18:08:30 2005 +0000 @@ -253,12 +253,14 @@ # Higher level request-related methods. - def get_fields_from_path(self): + def get_fields_from_path(self, encoding=None): """ Extracts fields (or request parameters) from the path specified in the transaction. The underlying framework may refuse to supply fields from - the path if handling a POST transaction. + the path if handling a POST transaction. The optional 'encoding' + parameter specifies the character encoding of the query string for cases + where the default encoding is to be overridden. Returns a dictionary mapping field names to lists of values (even if a single value is associated with any given field name). diff -r bad3608128f4 -r 5c5389988b5c WebStack/ModPython.py --- a/WebStack/ModPython.py Tue Aug 23 18:06:18 2005 +0000 +++ b/WebStack/ModPython.py Tue Aug 23 18:08:30 2005 +0000 @@ -210,24 +210,26 @@ # Higher level request-related methods. - def get_fields_from_path(self): + def get_fields_from_path(self, encoding=None): """ Extracts fields (or request parameters) from the path specified in the transaction. The underlying framework may refuse to supply fields from - the path if handling a POST transaction. + the path if handling a POST transaction. The optional 'encoding' + parameter specifies the character encoding of the query string for cases + where the default encoding is to be overridden. Returns a dictionary mapping field names to lists of values (even if a single value is associated with any given field name). """ - # NOTE: Support at best ISO-8859-1 values. - fields = {} for name, values in parse_qs(self.get_query_string(), 1).items(): # keep_blank_values=1 + name = self.decode_path(name, encoding) fields[name] = [] for value in values: - fields[name].append(unicode(value, "iso-8859-1")) + value = self.decode_path(value, encoding) + fields[name].append(value) return fields def get_fields_from_body(self, encoding=None): diff -r bad3608128f4 -r 5c5389988b5c WebStack/Twisted.py --- a/WebStack/Twisted.py Tue Aug 23 18:06:18 2005 +0000 +++ b/WebStack/Twisted.py Tue Aug 23 18:08:30 2005 +0000 @@ -182,24 +182,26 @@ # Higher level request-related methods. - def get_fields_from_path(self): + def get_fields_from_path(self, encoding=None): """ Extracts fields (or request parameters) from the path specified in the transaction. The underlying framework may refuse to supply fields from - the path if handling a POST transaction. + the path if handling a POST transaction. The optional 'encoding' + parameter specifies the character encoding of the query string for cases + where the default encoding is to be overridden. Returns a dictionary mapping field names to lists of values (even if a single value is associated with any given field name). """ - # NOTE: Support at best ISO-8859-1 values. - fields = {} for name, values in parse_qs(self.get_query_string(), keep_blank_values=1).items(): + name = self.decode_path(name, encoding) fields[name] = [] for value in values: - fields[name].append(unicode(value, "iso-8859-1")) + value = self.decode_path(value, encoding) + fields[name].append(value) return fields def get_fields_from_body(self, encoding=None): diff -r bad3608128f4 -r 5c5389988b5c WebStack/WSGI.py --- a/WebStack/WSGI.py Tue Aug 23 18:06:18 2005 +0000 +++ b/WebStack/WSGI.py Tue Aug 23 18:08:30 2005 +0000 @@ -223,24 +223,26 @@ # Higher level request-related methods. - def get_fields_from_path(self): + def get_fields_from_path(self, encoding=None): """ Extracts fields (or request parameters) from the path specified in the transaction. The underlying framework may refuse to supply fields from - the path if handling a POST transaction. + the path if handling a POST transaction. The optional 'encoding' + parameter specifies the character encoding of the query string for cases + where the default encoding is to be overridden. Returns a dictionary mapping field names to lists of values (even if a single value is associated with any given field name). """ - # NOTE: Support at best ISO-8859-1 values. - fields = {} for name, values in parse_qs(self.get_query_string(), keep_blank_values=1).items(): + name = self.decode_path(name, encoding) fields[name] = [] for value in values: - fields[name].append(unicode(value, "iso-8859-1")) + value = self.decode_path(value, encoding) + fields[name].append(value) return fields def get_fields_from_body(self, encoding=None): diff -r bad3608128f4 -r 5c5389988b5c WebStack/Webware.py --- a/WebStack/Webware.py Tue Aug 23 18:06:18 2005 +0000 +++ b/WebStack/Webware.py Tue Aug 23 18:08:30 2005 +0000 @@ -190,24 +190,26 @@ # Higher level request-related methods. - def get_fields_from_path(self): + def get_fields_from_path(self, encoding=None): """ Extracts fields (or request parameters) from the path specified in the transaction. The underlying framework may refuse to supply fields from - the path if handling a POST transaction. + the path if handling a POST transaction. The optional 'encoding' + parameter specifies the character encoding of the query string for cases + where the default encoding is to be overridden. Returns a dictionary mapping field names to lists of values (even if a single value is associated with any given field name). """ - # NOTE: Support at best ISO-8859-1 values. - fields = {} for name, values in parse_qs(self.get_query_string(), keep_blank_values=1).items(): + name = self.decode_path(name, encoding) fields[name] = [] for value in values: - fields[name].append(unicode(value, "iso-8859-1")) + value = self.decode_path(value, encoding) + fields[name].append(value) return fields def get_fields_from_body(self, encoding=None): diff -r bad3608128f4 -r 5c5389988b5c WebStack/Zope.py --- a/WebStack/Zope.py Tue Aug 23 18:06:18 2005 +0000 +++ b/WebStack/Zope.py Tue Aug 23 18:08:30 2005 +0000 @@ -195,24 +195,26 @@ # Higher level request-related methods. - def get_fields_from_path(self): + def get_fields_from_path(self, encoding=None): """ Extracts fields (or request parameters) from the path specified in the transaction. The underlying framework may refuse to supply fields from - the path if handling a POST transaction. + the path if handling a POST transaction. The optional 'encoding' + parameter specifies the character encoding of the query string for cases + where the default encoding is to be overridden. Returns a dictionary mapping field names to lists of values (even if a single value is associated with any given field name). """ - # NOTE: Support at best ISO-8859-1 values. - fields = {} for name, values in cgi.parse_qs(self.get_query_string()).items(): + name = self.decode_path(name, encoding) fields[name] = [] for value in values: - fields[name].append(unicode(value, "iso-8859-1")) + value = self.decode_path(value, encoding) + fields[name].append(value) return fields def get_fields_from_body(self, encoding=None):