1.1 --- a/WebStack/Zope.py Mon Sep 06 21:29:59 2004 +0000
1.2 +++ b/WebStack/Zope.py Mon Sep 06 21:31:04 2004 +0000
1.3 @@ -8,7 +8,7 @@
1.4
1.5 import Generic
1.6 from Helpers import Environment
1.7 -from Helpers.Request import Cookie, get_body_field
1.8 +from Helpers.Request import Cookie, get_body_field, filter_fields
1.9 from Helpers.Response import ConvertingStream
1.10 from ZPublisher.HTTPRequest import FileUpload
1.11 import cgi
1.12 @@ -151,23 +151,30 @@
1.13 def get_fields_from_path(self):
1.14
1.15 """
1.16 - Extracts the form fields from the path specified in the transaction. The
1.17 - underlying framework may refuse to supply fields from the path if
1.18 - handling a POST transaction.
1.19 + Extracts fields (or request parameters) from the path specified in the
1.20 + transaction. The underlying framework may refuse to supply fields from
1.21 + the path if handling a POST transaction.
1.22
1.23 Returns a dictionary mapping field names to lists of values (even if a
1.24 single value is associated with any given field name).
1.25 """
1.26
1.27 - return cgi.parse_qs(self.get_query_string())
1.28 + # NOTE: Support at best ISO-8859-1 values.
1.29 +
1.30 + fields = {}
1.31 + for name, values in cgi.parse_qs(self.get_query_string()).items():
1.32 + fields[name] = []
1.33 + for value in values:
1.34 + fields[name].append(unicode(value, "iso-8859-1"))
1.35 + return fields
1.36
1.37 def get_fields_from_body(self, encoding=None):
1.38
1.39 """
1.40 - Extracts the form fields from the message body in the transaction. The
1.41 - optional 'encoding' parameter specifies the character encoding of the
1.42 - message body for cases where no such information is available, but where
1.43 - the default encoding is to be overridden.
1.44 + Extracts fields (or request parameters) from the message body in the
1.45 + transaction. The optional 'encoding' parameter specifies the character
1.46 + encoding of the message body for cases where no such information is
1.47 + available, but where the default encoding is to be overridden.
1.48
1.49 Returns a dictionary mapping field names to lists of values (even if a
1.50 single value is associated with any given field name). Each value is
1.51 @@ -175,41 +182,57 @@
1.52 or a plain string (representing a file upload form field, for example).
1.53 """
1.54
1.55 - # Fix the inclusion of path fields since this prevents Unicode conversion.
1.56 + all_fields = self._get_fields(encoding)
1.57 + fields_from_path = self.get_fields_from_path()
1.58 + return filter_fields(all_fields, fields_from_path)
1.59
1.60 - fields_from_path = self.get_fields_from_path()
1.61 -
1.62 + def _get_fields(self, encoding=None):
1.63 encoding = encoding or self.get_content_type().charset or self.default_charset
1.64 fields = {}
1.65 for field_name, field_values in self.request.form.items():
1.66
1.67 - # Find the path values for this field (for filtering below).
1.68 -
1.69 - if fields_from_path.has_key(field_name):
1.70 - field_from_path_values = fields_from_path[field_name]
1.71 - if type(field_from_path_values) != type([]):
1.72 - field_from_path_values = [field_from_path_values]
1.73 - else:
1.74 - field_from_path_values = []
1.75 -
1.76 # Find the body values.
1.77
1.78 if type(field_values) == type([]):
1.79 fields[field_name] = []
1.80 for field_str in field_values:
1.81 - # Filter path values.
1.82 - if field_str not in field_from_path_values:
1.83 - fields[field_name].append(get_body_field(field_str, encoding))
1.84 + fields[field_name].append(get_body_field(field_str, encoding))
1.85 + else:
1.86 + fields[field_name] = [get_body_field(field_values, encoding)]
1.87 +
1.88 + return fields
1.89
1.90 - # Remove filtered fields.
1.91 + def get_fields(self, encoding=None):
1.92 +
1.93 + """
1.94 + Extracts fields (or request parameters) from both the path specified in
1.95 + the transaction as well as the message body. The optional 'encoding'
1.96 + parameter specifies the character encoding of the message body for cases
1.97 + where no such information is available, but where the default encoding
1.98 + is to be overridden.
1.99
1.100 - if fields[field_name] == []:
1.101 - del fields[field_name]
1.102 - else:
1.103 - # Filter path values.
1.104 - if field_values not in field_from_path_values:
1.105 - fields[field_name] = [get_body_field(field_values, encoding)]
1.106 + Returns a dictionary mapping field names to lists of values (even if a
1.107 + single value is associated with any given field name). Each value is
1.108 + either a Unicode object (representing a simple form field, for example)
1.109 + or a plain string (representing a file upload form field, for example).
1.110 +
1.111 + Where a given field name is used in both the path and message body to
1.112 + specify values, the values from both sources will be combined into a
1.113 + single list associated with that field name.
1.114 + """
1.115
1.116 + # NOTE: Zope seems to provide only body fields upon POST requests.
1.117 +
1.118 + if self.get_request_method() == "GET":
1.119 + return self._get_fields(encoding)
1.120 + else:
1.121 + fields = {}
1.122 + fields.update(self.get_fields_from_path())
1.123 + for name, values in self._get_fields(encoding).items():
1.124 + if not fields.has_key(name):
1.125 + fields[name] = values
1.126 + else:
1.127 + fields[name] += values
1.128 return fields
1.129
1.130 def get_user(self):