# HG changeset patch # User paulb # Date 1094153542 0 # Node ID 5f9836c6dd9d085e8d16540dccde66adcdc7664e # Parent 642bd8f2e98628d62aa108b566eac9b180891ba0 [project @ 2004-09-02 19:32:22 by paulb] Added separation of path and body fields to those frameworks which merge the values into a single source. Certain charset/encoding issues may remain. diff -r 642bd8f2e986 -r 5f9836c6dd9d WebStack/JavaServlet.py --- a/WebStack/JavaServlet.py Thu Sep 02 19:31:31 2004 +0000 +++ b/WebStack/JavaServlet.py Thu Sep 02 19:32:22 2004 +0000 @@ -6,7 +6,7 @@ import Generic from StringIO import StringIO -from Helpers.Request import Cookie, get_body_fields, get_storage_items +from Helpers.Request import Cookie, get_body_fields, get_storage_items, get_fields_from_query_string import javax.servlet.http.Cookie # Form data decoding. @@ -14,6 +14,7 @@ import javax.mail.internet.MimeMessage import javax.mail.Session import java.util.Properties +import java.net.URLDecoder class Stream: @@ -212,7 +213,7 @@ Returns the query string from the path in the request. """ - return self.request.getQueryString() + return self.request.getQueryString() or "" # Higher level request-related methods. @@ -225,12 +226,12 @@ Returns a dictionary mapping field names to lists of values (even if a single value is associated with any given field name). - - NOTE: There may not be a reliable means of extracting only the fields - NOTE: from the path. """ - return self.get_fields_from_body() + # There may not be a reliable means of extracting only the fields from + # the path using the API. + + return get_fields_from_query_string(self.get_query_string(), java.net.URLDecoder().decode) def get_fields_from_body(self, encoding=None): @@ -244,25 +245,53 @@ single value is associated with any given field name). Each value is either a Unicode object (representing a simple form field, for example) or a plain string (representing a file upload form field, for example). - - NOTE: There may not be a reliable means of extracting only the fields - NOTE: from the message body. Moreover, the encoding of the fields may - NOTE: not be pertinent. """ # Where the content type is "multipart/form-data", we use javax.mail # functionality. Otherwise, we use the Servlet API's parameter access # methods. - if self.get_content_type() and self.get_content_type().content_type == "multipart/form-data": + if self.get_content_type() and self.get_content_type().media_type == "multipart/form-data": fields = self._get_fields_from_message() else: + # Override the default encoding if requested. + + if encoding is not None: + self.request.setCharacterEncoding(encoding) + + # There may not be a reliable means of extracting only the fields + # the message body using the API. Remove fields originating from the + # path in the mixture provided by the API. + + fields_from_path = self.get_fields_from_path() + fields = {} parameter_map = self.request.getParameterMap() if parameter_map: - for key in parameter_map.keySet(): - fields[key] = parameter_map[key] + for field_name in parameter_map.keySet(): + + # Find path values for this field. + + if fields_from_path.has_key(field_name): + field_from_path_values = fields_from_path[field_name] + if type(field_from_path_values) != type([]): + field_from_path_values = [field_from_path_values] + else: + field_from_path_values = [] + + + # Filter out path values. + + fields[field_name] = [] + for field_value in parameter_map[field_name]: + if field_value not in field_from_path_values: + fields[field_name].append(field_value) + + # Remove filtered entries. + + if fields[field_name] == []: + del fields[field_name] return fields @@ -308,7 +337,7 @@ Returns the response stream for the transaction. """ - return self.response.getOutputStream() + return self.response.getWriter() def get_response_code(self): @@ -343,7 +372,7 @@ Sets the 'content_type' for the response. """ - return self.response.setHeader("Content-Type", str(content_type)) + self.response.setContentType(str(content_type)) # Higher level response-related methods. diff -r 642bd8f2e986 -r 5f9836c6dd9d WebStack/Twisted.py --- a/WebStack/Twisted.py Thu Sep 02 19:31:31 2004 +0000 +++ b/WebStack/Twisted.py Thu Sep 02 19:32:22 2004 +0000 @@ -159,22 +159,48 @@ single value is associated with any given field name). Each value is either a Unicode object (representing a simple form field, for example) or a plain string (representing a file upload form field, for example). + + NOTE: This still does not seem to work with UTF-16. """ - # NOTE: Fix the inclusion of path fields since this prevents Unicode conversion. + # Fix the inclusion of path fields since this prevents Unicode conversion. + + fields_from_path = self.get_fields_from_path() encoding = encoding or self.get_content_type().charset or self.default_charset fields = {} for field_name, field_values in self.trans.args.items(): + + # Find the path values for this field (for filtering below). + + if fields_from_path.has_key(field_name): + field_from_path_values = fields_from_path[field_name] + if type(field_from_path_values) != type([]): + field_from_path_values = [field_from_path_values] + else: + field_from_path_values = [] + + # Find the body values. + if type(field_values) == type([]): fields[field_name] = [] # Twisted stores plain strings. for field_str in field_values: - fields[field_name].append(get_body_field(field_str, encoding)) + # Filter path values. + if field_str not in field_from_path_values: + fields[field_name].append(get_body_field(field_str, encoding)) + + # Remove filtered fields. + + if fields[field_name] == []: + del fields[field_name] else: - fields[field_name] = get_body_field(field_values, encoding) + # Filter path values. + if field_values not in field_from_path_values: + fields[field_name] = get_body_field(field_values, encoding) + return fields def get_user(self): diff -r 642bd8f2e986 -r 5f9836c6dd9d WebStack/Zope.py --- a/WebStack/Zope.py Thu Sep 02 19:31:31 2004 +0000 +++ b/WebStack/Zope.py Thu Sep 02 19:32:22 2004 +0000 @@ -11,6 +11,7 @@ from Helpers.Request import Cookie, get_body_field from Helpers.Response import ConvertingStream from ZPublisher.HTTPRequest import FileUpload +import cgi class Transaction(Generic.Transaction): @@ -158,13 +159,7 @@ single value is associated with any given field name). """ - fields = {} - for key, value in self.request.form.items(): - if type(value) == type([]): - fields[key] = value - else: - fields[key] = [value] - return fields + return cgi.parse_qs(self.get_query_string()) def get_fields_from_body(self, encoding=None): @@ -179,20 +174,44 @@ either a Unicode object (representing a simple form field, for example) or a plain string (representing a file upload form field, for example). - NOTE: Zope doesn't distinguish between path and body fields. + NOTE: This still does not seem to work with UTF-16. """ - # NOTE: Conversion to Unicode may be inappropriate. + # Fix the inclusion of path fields since this prevents Unicode conversion. + + fields_from_path = self.get_fields_from_path() encoding = encoding or self.get_content_type().charset or self.default_charset fields = {} - for field_name, field_values in self.get_fields_from_path().items(): + for field_name, field_values in self.request.form.items(): + + # Find the path values for this field (for filtering below). + + if fields_from_path.has_key(field_name): + field_from_path_values = fields_from_path[field_name] + if type(field_from_path_values) != type([]): + field_from_path_values = [field_from_path_values] + else: + field_from_path_values = [] + + # Find the body values. + if type(field_values) == type([]): fields[field_name] = [] for field_str in field_values: - fields[field_name].append(get_body_field(field_str, encoding)) + # Filter path values. + if field_str not in field_from_path_values: + fields[field_name].append(get_body_field(field_str, encoding)) + + # Remove filtered fields. + + if fields[field_name] == []: + del fields[field_name] else: - fields[field_name] = [get_body_field(field_values, encoding)] + # Filter path values. + if field_values not in field_from_path_values: + fields[field_name] = [get_body_field(field_values, encoding)] + return fields def get_user(self):