1.1 --- a/WebStack/JavaServlet.py Thu Sep 02 19:31:31 2004 +0000
1.2 +++ b/WebStack/JavaServlet.py Thu Sep 02 19:32:22 2004 +0000
1.3 @@ -6,7 +6,7 @@
1.4
1.5 import Generic
1.6 from StringIO import StringIO
1.7 -from Helpers.Request import Cookie, get_body_fields, get_storage_items
1.8 +from Helpers.Request import Cookie, get_body_fields, get_storage_items, get_fields_from_query_string
1.9 import javax.servlet.http.Cookie
1.10
1.11 # Form data decoding.
1.12 @@ -14,6 +14,7 @@
1.13 import javax.mail.internet.MimeMessage
1.14 import javax.mail.Session
1.15 import java.util.Properties
1.16 +import java.net.URLDecoder
1.17
1.18 class Stream:
1.19
1.20 @@ -212,7 +213,7 @@
1.21 Returns the query string from the path in the request.
1.22 """
1.23
1.24 - return self.request.getQueryString()
1.25 + return self.request.getQueryString() or ""
1.26
1.27 # Higher level request-related methods.
1.28
1.29 @@ -225,12 +226,12 @@
1.30
1.31 Returns a dictionary mapping field names to lists of values (even if a
1.32 single value is associated with any given field name).
1.33 -
1.34 - NOTE: There may not be a reliable means of extracting only the fields
1.35 - NOTE: from the path.
1.36 """
1.37
1.38 - return self.get_fields_from_body()
1.39 + # There may not be a reliable means of extracting only the fields from
1.40 + # the path using the API.
1.41 +
1.42 + return get_fields_from_query_string(self.get_query_string(), java.net.URLDecoder().decode)
1.43
1.44 def get_fields_from_body(self, encoding=None):
1.45
1.46 @@ -244,25 +245,53 @@
1.47 single value is associated with any given field name). Each value is
1.48 either a Unicode object (representing a simple form field, for example)
1.49 or a plain string (representing a file upload form field, for example).
1.50 -
1.51 - NOTE: There may not be a reliable means of extracting only the fields
1.52 - NOTE: from the message body. Moreover, the encoding of the fields may
1.53 - NOTE: not be pertinent.
1.54 """
1.55
1.56 # Where the content type is "multipart/form-data", we use javax.mail
1.57 # functionality. Otherwise, we use the Servlet API's parameter access
1.58 # methods.
1.59
1.60 - if self.get_content_type() and self.get_content_type().content_type == "multipart/form-data":
1.61 + if self.get_content_type() and self.get_content_type().media_type == "multipart/form-data":
1.62 fields = self._get_fields_from_message()
1.63
1.64 else:
1.65 + # Override the default encoding if requested.
1.66 +
1.67 + if encoding is not None:
1.68 + self.request.setCharacterEncoding(encoding)
1.69 +
1.70 + # There may not be a reliable means of extracting only the fields
1.71 + # the message body using the API. Remove fields originating from the
1.72 + # path in the mixture provided by the API.
1.73 +
1.74 + fields_from_path = self.get_fields_from_path()
1.75 +
1.76 fields = {}
1.77 parameter_map = self.request.getParameterMap()
1.78 if parameter_map:
1.79 - for key in parameter_map.keySet():
1.80 - fields[key] = parameter_map[key]
1.81 + for field_name in parameter_map.keySet():
1.82 +
1.83 + # Find path values for this field.
1.84 +
1.85 + if fields_from_path.has_key(field_name):
1.86 + field_from_path_values = fields_from_path[field_name]
1.87 + if type(field_from_path_values) != type([]):
1.88 + field_from_path_values = [field_from_path_values]
1.89 + else:
1.90 + field_from_path_values = []
1.91 +
1.92 +
1.93 + # Filter out path values.
1.94 +
1.95 + fields[field_name] = []
1.96 + for field_value in parameter_map[field_name]:
1.97 + if field_value not in field_from_path_values:
1.98 + fields[field_name].append(field_value)
1.99 +
1.100 + # Remove filtered entries.
1.101 +
1.102 + if fields[field_name] == []:
1.103 + del fields[field_name]
1.104
1.105 return fields
1.106
1.107 @@ -308,7 +337,7 @@
1.108 Returns the response stream for the transaction.
1.109 """
1.110
1.111 - return self.response.getOutputStream()
1.112 + return self.response.getWriter()
1.113
1.114 def get_response_code(self):
1.115
1.116 @@ -343,7 +372,7 @@
1.117 Sets the 'content_type' for the response.
1.118 """
1.119
1.120 - return self.response.setHeader("Content-Type", str(content_type))
1.121 + self.response.setContentType(str(content_type))
1.122
1.123 # Higher level response-related methods.
1.124
2.1 --- a/WebStack/Twisted.py Thu Sep 02 19:31:31 2004 +0000
2.2 +++ b/WebStack/Twisted.py Thu Sep 02 19:32:22 2004 +0000
2.3 @@ -159,22 +159,48 @@
2.4 single value is associated with any given field name). Each value is
2.5 either a Unicode object (representing a simple form field, for example)
2.6 or a plain string (representing a file upload form field, for example).
2.7 +
2.8 + NOTE: This still does not seem to work with UTF-16.
2.9 """
2.10
2.11 - # NOTE: Fix the inclusion of path fields since this prevents Unicode conversion.
2.12 + # Fix the inclusion of path fields since this prevents Unicode conversion.
2.13 +
2.14 + fields_from_path = self.get_fields_from_path()
2.15
2.16 encoding = encoding or self.get_content_type().charset or self.default_charset
2.17 fields = {}
2.18 for field_name, field_values in self.trans.args.items():
2.19 +
2.20 + # Find the path values for this field (for filtering below).
2.21 +
2.22 + if fields_from_path.has_key(field_name):
2.23 + field_from_path_values = fields_from_path[field_name]
2.24 + if type(field_from_path_values) != type([]):
2.25 + field_from_path_values = [field_from_path_values]
2.26 + else:
2.27 + field_from_path_values = []
2.28 +
2.29 + # Find the body values.
2.30 +
2.31 if type(field_values) == type([]):
2.32 fields[field_name] = []
2.33
2.34 # Twisted stores plain strings.
2.35
2.36 for field_str in field_values:
2.37 - fields[field_name].append(get_body_field(field_str, encoding))
2.38 + # Filter path values.
2.39 + if field_str not in field_from_path_values:
2.40 + fields[field_name].append(get_body_field(field_str, encoding))
2.41 +
2.42 + # Remove filtered fields.
2.43 +
2.44 + if fields[field_name] == []:
2.45 + del fields[field_name]
2.46 else:
2.47 - fields[field_name] = get_body_field(field_values, encoding)
2.48 + # Filter path values.
2.49 + if field_values not in field_from_path_values:
2.50 + fields[field_name] = get_body_field(field_values, encoding)
2.51 +
2.52 return fields
2.53
2.54 def get_user(self):
3.1 --- a/WebStack/Zope.py Thu Sep 02 19:31:31 2004 +0000
3.2 +++ b/WebStack/Zope.py Thu Sep 02 19:32:22 2004 +0000
3.3 @@ -11,6 +11,7 @@
3.4 from Helpers.Request import Cookie, get_body_field
3.5 from Helpers.Response import ConvertingStream
3.6 from ZPublisher.HTTPRequest import FileUpload
3.7 +import cgi
3.8
3.9 class Transaction(Generic.Transaction):
3.10
3.11 @@ -158,13 +159,7 @@
3.12 single value is associated with any given field name).
3.13 """
3.14
3.15 - fields = {}
3.16 - for key, value in self.request.form.items():
3.17 - if type(value) == type([]):
3.18 - fields[key] = value
3.19 - else:
3.20 - fields[key] = [value]
3.21 - return fields
3.22 + return cgi.parse_qs(self.get_query_string())
3.23
3.24 def get_fields_from_body(self, encoding=None):
3.25
3.26 @@ -179,20 +174,44 @@
3.27 either a Unicode object (representing a simple form field, for example)
3.28 or a plain string (representing a file upload form field, for example).
3.29
3.30 - NOTE: Zope doesn't distinguish between path and body fields.
3.31 + NOTE: This still does not seem to work with UTF-16.
3.32 """
3.33
3.34 - # NOTE: Conversion to Unicode may be inappropriate.
3.35 + # Fix the inclusion of path fields since this prevents Unicode conversion.
3.36 +
3.37 + fields_from_path = self.get_fields_from_path()
3.38
3.39 encoding = encoding or self.get_content_type().charset or self.default_charset
3.40 fields = {}
3.41 - for field_name, field_values in self.get_fields_from_path().items():
3.42 + for field_name, field_values in self.request.form.items():
3.43 +
3.44 + # Find the path values for this field (for filtering below).
3.45 +
3.46 + if fields_from_path.has_key(field_name):
3.47 + field_from_path_values = fields_from_path[field_name]
3.48 + if type(field_from_path_values) != type([]):
3.49 + field_from_path_values = [field_from_path_values]
3.50 + else:
3.51 + field_from_path_values = []
3.52 +
3.53 + # Find the body values.
3.54 +
3.55 if type(field_values) == type([]):
3.56 fields[field_name] = []
3.57 for field_str in field_values:
3.58 - fields[field_name].append(get_body_field(field_str, encoding))
3.59 + # Filter path values.
3.60 + if field_str not in field_from_path_values:
3.61 + fields[field_name].append(get_body_field(field_str, encoding))
3.62 +
3.63 + # Remove filtered fields.
3.64 +
3.65 + if fields[field_name] == []:
3.66 + del fields[field_name]
3.67 else:
3.68 - fields[field_name] = [get_body_field(field_values, encoding)]
3.69 + # Filter path values.
3.70 + if field_values not in field_from_path_values:
3.71 + fields[field_name] = [get_body_field(field_values, encoding)]
3.72 +
3.73 return fields
3.74
3.75 def get_user(self):