1.1 --- a/WebStack/BaseHTTPRequestHandler.py Mon Sep 06 21:29:59 2004 +0000
1.2 +++ b/WebStack/BaseHTTPRequestHandler.py Mon Sep 06 21:31:04 2004 +0000
1.3 @@ -200,23 +200,30 @@
1.4 def get_fields_from_path(self):
1.5
1.6 """
1.7 - Extracts the form fields from the path specified in the transaction. The
1.8 - underlying framework may refuse to supply fields from the path if
1.9 - handling a POST transaction.
1.10 + Extracts fields (or request parameters) from the path specified in the
1.11 + transaction. The underlying framework may refuse to supply fields from
1.12 + the path if handling a POST transaction.
1.13
1.14 Returns a dictionary mapping field names to lists of values (even if a
1.15 single value is associated with any given field name).
1.16 """
1.17
1.18 - return parse_qs(self.get_query_string(), keep_blank_values=1)
1.19 + # NOTE: Support at best ISO-8859-1 values.
1.20 +
1.21 + fields = {}
1.22 + for name, values in parse_qs(self.get_query_string(), keep_blank_values=1).items():
1.23 + fields[name] = []
1.24 + for value in values:
1.25 + fields[name].append(unicode(value, "iso-8859-1"))
1.26 + return fields
1.27
1.28 def get_fields_from_body(self, encoding=None):
1.29
1.30 """
1.31 - Extracts the form fields from the message body in the transaction. The
1.32 - optional 'encoding' parameter specifies the character encoding of the
1.33 - message body for cases where no such information is available, but where
1.34 - the default encoding is to be overridden.
1.35 + Extracts fields (or request parameters) from the message body in the
1.36 + transaction. The optional 'encoding' parameter specifies the character
1.37 + encoding of the message body for cases where no such information is
1.38 + available, but where the default encoding is to be overridden.
1.39
1.40 Returns a dictionary mapping field names to lists of values (even if a
1.41 single value is associated with any given field name). Each value is
1.42 @@ -242,6 +249,36 @@
1.43
1.44 return fields
1.45
1.46 + def get_fields(self, encoding=None):
1.47 +
1.48 + """
1.49 + Extracts fields (or request parameters) from both the path specified in
1.50 + the transaction as well as the message body. The optional 'encoding'
1.51 + parameter specifies the character encoding of the message body for cases
1.52 + where no such information is available, but where the default encoding
1.53 + is to be overridden.
1.54 +
1.55 + Returns a dictionary mapping field names to lists of values (even if a
1.56 + single value is associated with any given field name). Each value is
1.57 + either a Unicode object (representing a simple form field, for example)
1.58 + or a plain string (representing a file upload form field, for example).
1.59 +
1.60 + Where a given field name is used in both the path and message body to
1.61 + specify values, the values from both sources will be combined into a
1.62 + single list associated with that field name.
1.63 + """
1.64 +
1.65 + # Combine the two sources.
1.66 +
1.67 + fields = {}
1.68 + fields.update(self.get_fields_from_path())
1.69 + for name, values in self.get_fields_from_body(encoding).items():
1.70 + if not fields.has_key(name):
1.71 + fields[name] = values
1.72 + else:
1.73 + fields[name] += values
1.74 + return fields
1.75 +
1.76 def get_user(self):
1.77
1.78 """
2.1 --- a/WebStack/CGI.py Mon Sep 06 21:29:59 2004 +0000
2.2 +++ b/WebStack/CGI.py Mon Sep 06 21:31:04 2004 +0000
2.3 @@ -192,23 +192,30 @@
2.4 def get_fields_from_path(self):
2.5
2.6 """
2.7 - Extracts the form fields from the path specified in the transaction. The
2.8 - underlying framework may refuse to supply fields from the path if
2.9 - handling a POST transaction.
2.10 + Extracts fields (or request parameters) from the path specified in the
2.11 + transaction. The underlying framework may refuse to supply fields from
2.12 + the path if handling a POST transaction.
2.13
2.14 Returns a dictionary mapping field names to lists of values (even if a
2.15 single value is associated with any given field name).
2.16 """
2.17
2.18 - return parse_qs(self.get_query_string(), keep_blank_values=1)
2.19 + # NOTE: Support at best ISO-8859-1 values.
2.20 +
2.21 + fields = {}
2.22 + for name, values in parse_qs(self.get_query_string(), keep_blank_values=1).items():
2.23 + fields[name] = []
2.24 + for value in values:
2.25 + fields[name].append(unicode(value, "iso-8859-1"))
2.26 + return fields
2.27
2.28 def get_fields_from_body(self, encoding=None):
2.29
2.30 """
2.31 - Extracts the form fields from the message body in the transaction. The
2.32 - optional 'encoding' parameter specifies the character encoding of the
2.33 - message body for cases where no such information is available, but where
2.34 - the default encoding is to be overridden.
2.35 + Extracts fields (or request parameters) from the message body in the
2.36 + transaction. The optional 'encoding' parameter specifies the character
2.37 + encoding of the message body for cases where no such information is
2.38 + available, but where the default encoding is to be overridden.
2.39
2.40 Returns a dictionary mapping field names to lists of values (even if a
2.41 single value is associated with any given field name). Each value is
2.42 @@ -219,7 +226,10 @@
2.43 encoding = encoding or self.get_content_type().charset or self.default_charset
2.44
2.45 if self.storage_body is None:
2.46 - self.storage_body = FieldStorage(fp=self.get_request_stream(), keep_blank_values=1)
2.47 + self.storage_body = FieldStorage(fp=self.get_request_stream(),
2.48 + headers={"content-type" : str(self.get_content_type())},
2.49 + environ={"REQUEST_METHOD" : self.get_request_method()},
2.50 + keep_blank_values=1)
2.51
2.52 # Avoid strange design issues with FieldStorage by checking the internal
2.53 # field list directly.
2.54 @@ -233,6 +243,36 @@
2.55
2.56 return fields
2.57
2.58 + def get_fields(self, encoding=None):
2.59 +
2.60 + """
2.61 + Extracts fields (or request parameters) from both the path specified in
2.62 + the transaction as well as the message body. The optional 'encoding'
2.63 + parameter specifies the character encoding of the message body for cases
2.64 + where no such information is available, but where the default encoding
2.65 + is to be overridden.
2.66 +
2.67 + Returns a dictionary mapping field names to lists of values (even if a
2.68 + single value is associated with any given field name). Each value is
2.69 + either a Unicode object (representing a simple form field, for example)
2.70 + or a plain string (representing a file upload form field, for example).
2.71 +
2.72 + Where a given field name is used in both the path and message body to
2.73 + specify values, the values from both sources will be combined into a
2.74 + single list associated with that field name.
2.75 + """
2.76 +
2.77 + # Combine the two sources.
2.78 +
2.79 + fields = {}
2.80 + fields.update(self.get_fields_from_path())
2.81 + for name, values in self.get_fields_from_body(encoding).items():
2.82 + if not fields.has_key(name):
2.83 + fields[name] = values
2.84 + else:
2.85 + fields[name] += values
2.86 + return fields
2.87 +
2.88 def get_user(self):
2.89
2.90 """
3.1 --- a/WebStack/Generic.py Mon Sep 06 21:29:59 2004 +0000
3.2 +++ b/WebStack/Generic.py Mon Sep 06 21:31:04 2004 +0000
3.3 @@ -281,9 +281,9 @@
3.4 def get_fields_from_path(self):
3.5
3.6 """
3.7 - Extracts the form fields from the path specified in the transaction. The
3.8 - underlying framework may refuse to supply fields from the path if
3.9 - handling a POST transaction.
3.10 + Extracts fields (or request parameters) from the path specified in the
3.11 + transaction. The underlying framework may refuse to supply fields from
3.12 + the path if handling a POST transaction.
3.13
3.14 Returns a dictionary mapping field names to lists of values (even if a
3.15 single value is associated with any given field name).
3.16 @@ -294,10 +294,10 @@
3.17 def get_fields_from_body(self, encoding=None):
3.18
3.19 """
3.20 - Extracts the form fields from the message body in the transaction. The
3.21 - optional 'encoding' parameter specifies the character encoding of the
3.22 - message body for cases where no such information is available, but where
3.23 - the default encoding is to be overridden.
3.24 + Extracts fields (or request parameters) from the message body in the
3.25 + transaction. The optional 'encoding' parameter specifies the character
3.26 + encoding of the message body for cases where no such information is
3.27 + available, but where the default encoding is to be overridden.
3.28
3.29 Returns a dictionary mapping field names to lists of values (even if a
3.30 single value is associated with any given field name). Each value is
3.31 @@ -307,6 +307,27 @@
3.32
3.33 raise NotImplementedError, "get_fields_from_body"
3.34
3.35 + def get_fields(self, encoding=None):
3.36 +
3.37 + """
3.38 + Extracts fields (or request parameters) from both the path specified in
3.39 + the transaction as well as the message body. The optional 'encoding'
3.40 + parameter specifies the character encoding of the message body for cases
3.41 + where no such information is available, but where the default encoding
3.42 + is to be overridden.
3.43 +
3.44 + Returns a dictionary mapping field names to lists of values (even if a
3.45 + single value is associated with any given field name). Each value is
3.46 + either a Unicode object (representing a simple form field, for example)
3.47 + or a plain string (representing a file upload form field, for example).
3.48 +
3.49 + Where a given field name is used in both the path and message body to
3.50 + specify values, the values from both sources will be combined into a
3.51 + single list associated with that field name.
3.52 + """
3.53 +
3.54 + raise NotImplementedError, "get_fields"
3.55 +
3.56 def get_user(self):
3.57
3.58 """
4.1 --- a/WebStack/JavaServlet.py Mon Sep 06 21:29:59 2004 +0000
4.2 +++ b/WebStack/JavaServlet.py Mon Sep 06 21:31:04 2004 +0000
4.3 @@ -6,7 +6,7 @@
4.4
4.5 import Generic
4.6 from StringIO import StringIO
4.7 -from Helpers.Request import Cookie, get_body_fields, get_storage_items, get_fields_from_query_string
4.8 +from Helpers.Request import Cookie, get_body_fields, get_storage_items, get_fields_from_query_string, filter_fields
4.9 import javax.servlet.http
4.10
4.11 # Form data decoding.
4.12 @@ -45,7 +45,11 @@
4.13
4.14 "Read a line from the stream, returning it as a string."
4.15
4.16 - return self.stream.readLine() + "\n"
4.17 + line = self.stream.readLine()
4.18 + if line is not None:
4.19 + return line + "\n"
4.20 + else:
4.21 + return ""
4.22
4.23 class Transaction(Generic.Transaction):
4.24
4.25 @@ -75,7 +79,7 @@
4.26
4.27 # Cached information.
4.28
4.29 - self.storage_body = None
4.30 + self.message_fields = None
4.31
4.32 def commit(self):
4.33
4.34 @@ -221,9 +225,9 @@
4.35 def get_fields_from_path(self):
4.36
4.37 """
4.38 - Extracts the form fields from the path specified in the transaction. The
4.39 - underlying framework may refuse to supply fields from the path if
4.40 - handling a POST transaction.
4.41 + Extracts fields (or request parameters) from the path specified in the
4.42 + transaction. The underlying framework may refuse to supply fields from
4.43 + the path if handling a POST transaction.
4.44
4.45 Returns a dictionary mapping field names to lists of values (even if a
4.46 single value is associated with any given field name).
4.47 @@ -239,10 +243,10 @@
4.48 def get_fields_from_body(self, encoding=None):
4.49
4.50 """
4.51 - Extracts the form fields from the message body in the transaction. The
4.52 - optional 'encoding' parameter specifies the character encoding of the
4.53 - message body for cases where no such information is available, but where
4.54 - the default encoding is to be overridden.
4.55 + Extracts fields (or request parameters) from the message body in the
4.56 + transaction. The optional 'encoding' parameter specifies the character
4.57 + encoding of the message body for cases where no such information is
4.58 + available, but where the default encoding is to be overridden.
4.59
4.60 Returns a dictionary mapping field names to lists of values (even if a
4.61 single value is associated with any given field name). Each value is
4.62 @@ -250,6 +254,16 @@
4.63 or a plain string (representing a file upload form field, for example).
4.64 """
4.65
4.66 + # There may not be a reliable means of extracting only the fields
4.67 + # the message body using the API. Remove fields originating from the
4.68 + # path in the mixture provided by the API.
4.69 +
4.70 + all_fields = self._get_fields(encoding)
4.71 + fields_from_path = self.get_fields_from_path()
4.72 + return filter_fields(all_fields, fields_from_path)
4.73 +
4.74 + def _get_fields(self, encoding=None):
4.75 +
4.76 # Override the default encoding if requested.
4.77
4.78 if encoding is not None:
4.79 @@ -260,41 +274,51 @@
4.80 # methods.
4.81
4.82 if self.get_content_type() and self.get_content_type().media_type == "multipart/form-data":
4.83 - fields = self._get_fields_from_message()
4.84 -
4.85 + if self.message_fields is not None:
4.86 + return self.message_fields
4.87 + else:
4.88 + fields = self.message_fields = self._get_fields_from_message()
4.89 else:
4.90 - # There may not be a reliable means of extracting only the fields
4.91 - # the message body using the API. Remove fields originating from the
4.92 - # path in the mixture provided by the API.
4.93 -
4.94 - fields_from_path = self.get_fields_from_path()
4.95 -
4.96 fields = {}
4.97 parameter_map = self.request.getParameterMap()
4.98 if parameter_map:
4.99 for field_name in parameter_map.keySet():
4.100 + fields[field_name] = parameter_map[field_name]
4.101
4.102 - # Find path values for this field.
4.103 + return fields
4.104 +
4.105 + def get_fields(self, encoding=None):
4.106
4.107 - if fields_from_path.has_key(field_name):
4.108 - field_from_path_values = fields_from_path[field_name]
4.109 - if type(field_from_path_values) != type([]):
4.110 - field_from_path_values = [field_from_path_values]
4.111 - else:
4.112 - field_from_path_values = []
4.113 + """
4.114 + Extracts fields (or request parameters) from both the path specified in
4.115 + the transaction as well as the message body. The optional 'encoding'
4.116 + parameter specifies the character encoding of the message body for cases
4.117 + where no such information is available, but where the default encoding
4.118 + is to be overridden.
4.119 +
4.120 + Returns a dictionary mapping field names to lists of values (even if a
4.121 + single value is associated with any given field name). Each value is
4.122 + either a Unicode object (representing a simple form field, for example)
4.123 + or a plain string (representing a file upload form field, for example).
4.124
4.125 - # Filter out path values.
4.126 + Where a given field name is used in both the path and message body to
4.127 + specify values, the values from both sources will be combined into a
4.128 + single list associated with that field name.
4.129 + """
4.130 +
4.131 + # NOTE: The Java Servlet API (like Zope) seems to provide only body
4.132 + # NOTE: fields upon POST requests.
4.133
4.134 - fields[field_name] = []
4.135 - for field_value in parameter_map[field_name]:
4.136 - if field_value not in field_from_path_values:
4.137 - fields[field_name].append(field_value)
4.138 -
4.139 - # Remove filtered entries.
4.140 -
4.141 - if fields[field_name] == []:
4.142 - del fields[field_name]
4.143 -
4.144 + if self.get_request_method() == "GET":
4.145 + return self._get_fields(encoding)
4.146 + else:
4.147 + fields = {}
4.148 + fields.update(self.get_fields_from_path())
4.149 + for name, values in self._get_fields(encoding).items():
4.150 + if not fields.has_key(name):
4.151 + fields[name] = values
4.152 + else:
4.153 + fields[name] += values
4.154 return fields
4.155
4.156 def get_user(self):
5.1 --- a/WebStack/ModPython.py Mon Sep 06 21:29:59 2004 +0000
5.2 +++ b/WebStack/ModPython.py Mon Sep 06 21:31:04 2004 +0000
5.3 @@ -5,7 +5,7 @@
5.4 """
5.5
5.6 import Generic
5.7 -from Helpers.Request import get_body_field
5.8 +from Helpers.Request import get_body_field, filter_fields
5.9 from Helpers.Response import ConvertingStream
5.10 from mod_python.util import parse_qs, FieldStorage
5.11 from mod_python import apache
5.12 @@ -175,23 +175,30 @@
5.13 def get_fields_from_path(self):
5.14
5.15 """
5.16 - Extracts the form fields from the path specified in the transaction. The
5.17 - underlying framework may refuse to supply fields from the path if
5.18 - handling a POST transaction.
5.19 + Extracts fields (or request parameters) from the path specified in the
5.20 + transaction. The underlying framework may refuse to supply fields from
5.21 + the path if handling a POST transaction.
5.22
5.23 Returns a dictionary mapping field names to lists of values (even if a
5.24 single value is associated with any given field name).
5.25 """
5.26
5.27 - return parse_qs(self.get_query_string(), 1) # keep_blank_values=1
5.28 + # NOTE: Support at best ISO-8859-1 values.
5.29 +
5.30 + fields = {}
5.31 + for name, values in parse_qs(self.get_query_string(), 1).items(): # keep_blank_values=1
5.32 + fields[name] = []
5.33 + for value in values:
5.34 + fields[name].append(unicode(value, "iso-8859-1"))
5.35 + return fields
5.36
5.37 def get_fields_from_body(self, encoding=None):
5.38
5.39 """
5.40 - Extracts the form fields from the message body in the transaction. The
5.41 - optional 'encoding' parameter specifies the character encoding of the
5.42 - message body for cases where no such information is available, but where
5.43 - the default encoding is to be overridden.
5.44 + Extracts fields (or request parameters) from the message body in the
5.45 + transaction. The optional 'encoding' parameter specifies the character
5.46 + encoding of the message body for cases where no such information is
5.47 + available, but where the default encoding is to be overridden.
5.48
5.49 Returns a dictionary mapping field names to lists of values (even if a
5.50 single value is associated with any given field name). Each value is
5.51 @@ -202,6 +209,11 @@
5.52 body with fields found in the path.
5.53 """
5.54
5.55 + all_fields = self._get_fields(encoding)
5.56 + fields_from_path = self.get_fields_from_path()
5.57 + return filter_fields(all_fields, fields_from_path)
5.58 +
5.59 + def _get_fields(self, encoding=None):
5.60 encoding = encoding or self.get_content_type().charset or self.default_charset
5.61
5.62 if self.storage_body is None:
5.63 @@ -216,6 +228,27 @@
5.64 fields[field.name].append(get_body_field(field.value, encoding))
5.65 return fields
5.66
5.67 + def get_fields(self, encoding=None):
5.68 +
5.69 + """
5.70 + Extracts fields (or request parameters) from both the path specified in
5.71 + the transaction as well as the message body. The optional 'encoding'
5.72 + parameter specifies the character encoding of the message body for cases
5.73 + where no such information is available, but where the default encoding
5.74 + is to be overridden.
5.75 +
5.76 + Returns a dictionary mapping field names to lists of values (even if a
5.77 + single value is associated with any given field name). Each value is
5.78 + either a Unicode object (representing a simple form field, for example)
5.79 + or a plain string (representing a file upload form field, for example).
5.80 +
5.81 + Where a given field name is used in both the path and message body to
5.82 + specify values, the values from both sources will be combined into a
5.83 + single list associated with that field name.
5.84 + """
5.85 +
5.86 + return self._get_fields(encoding)
5.87 +
5.88 def get_user(self):
5.89
5.90 """
6.1 --- a/WebStack/Twisted.py Mon Sep 06 21:29:59 2004 +0000
6.2 +++ b/WebStack/Twisted.py Mon Sep 06 21:31:04 2004 +0000
6.3 @@ -6,7 +6,7 @@
6.4
6.5 import Generic
6.6 from Helpers.Auth import UserInfo
6.7 -from Helpers.Request import Cookie, get_body_field
6.8 +from Helpers.Request import Cookie, get_body_field, filter_fields
6.9 from Helpers.Response import ConvertingStream
6.10 from Helpers.Session import SessionStore
6.11 from cgi import parse_qs
6.12 @@ -154,23 +154,30 @@
6.13 def get_fields_from_path(self):
6.14
6.15 """
6.16 - Extracts the form fields from the path specified in the transaction. The
6.17 - underlying framework may refuse to supply fields from the path if
6.18 - handling a POST transaction.
6.19 + Extracts fields (or request parameters) from the path specified in the
6.20 + transaction. The underlying framework may refuse to supply fields from
6.21 + the path if handling a POST transaction.
6.22
6.23 Returns a dictionary mapping field names to lists of values (even if a
6.24 single value is associated with any given field name).
6.25 """
6.26
6.27 - return parse_qs(self.get_query_string(), keep_blank_values=1)
6.28 + # NOTE: Support at best ISO-8859-1 values.
6.29 +
6.30 + fields = {}
6.31 + for name, values in parse_qs(self.get_query_string(), keep_blank_values=1).items():
6.32 + fields[name] = []
6.33 + for value in values:
6.34 + fields[name].append(unicode(value, "iso-8859-1"))
6.35 + return fields
6.36
6.37 def get_fields_from_body(self, encoding=None):
6.38
6.39 """
6.40 - Extracts the form fields from the message body in the transaction. The
6.41 - optional 'encoding' parameter specifies the character encoding of the
6.42 - message body for cases where no such information is available, but where
6.43 - the default encoding is to be overridden.
6.44 + Extracts fields (or request parameters) from the message body in the
6.45 + transaction. The optional 'encoding' parameter specifies the character
6.46 + encoding of the message body for cases where no such information is
6.47 + available, but where the default encoding is to be overridden.
6.48
6.49 Returns a dictionary mapping field names to lists of values (even if a
6.50 single value is associated with any given field name). Each value is
6.51 @@ -178,23 +185,19 @@
6.52 or a plain string (representing a file upload form field, for example).
6.53 """
6.54
6.55 - # Fix the inclusion of path fields since this prevents Unicode conversion.
6.56 + # There may not be a reliable means of extracting only the fields
6.57 + # the message body using the API. Remove fields originating from the
6.58 + # path in the mixture provided by the API.
6.59
6.60 + all_fields = self._get_fields(encoding)
6.61 fields_from_path = self.get_fields_from_path()
6.62 + return filter_fields(all_fields, fields_from_path)
6.63
6.64 + def _get_fields(self, encoding=None):
6.65 encoding = encoding or self.get_content_type().charset or self.default_charset
6.66 fields = {}
6.67 for field_name, field_values in self.trans.args.items():
6.68
6.69 - # Find the path values for this field (for filtering below).
6.70 -
6.71 - if fields_from_path.has_key(field_name):
6.72 - field_from_path_values = fields_from_path[field_name]
6.73 - if type(field_from_path_values) != type([]):
6.74 - field_from_path_values = [field_from_path_values]
6.75 - else:
6.76 - field_from_path_values = []
6.77 -
6.78 # Find the body values.
6.79
6.80 if type(field_values) == type([]):
6.81 @@ -203,21 +206,33 @@
6.82 # Twisted stores plain strings.
6.83
6.84 for field_str in field_values:
6.85 - # Filter path values.
6.86 - if field_str not in field_from_path_values:
6.87 - fields[field_name].append(get_body_field(field_str, encoding))
6.88 -
6.89 - # Remove filtered fields.
6.90 -
6.91 - if fields[field_name] == []:
6.92 - del fields[field_name]
6.93 + fields[field_name].append(get_body_field(field_str, encoding))
6.94 else:
6.95 - # Filter path values.
6.96 - if field_values not in field_from_path_values:
6.97 - fields[field_name] = get_body_field(field_values, encoding)
6.98 + fields[field_name] = get_body_field(field_values, encoding)
6.99
6.100 return fields
6.101
6.102 + def get_fields(self, encoding=None):
6.103 +
6.104 + """
6.105 + Extracts fields (or request parameters) from both the path specified in
6.106 + the transaction as well as the message body. The optional 'encoding'
6.107 + parameter specifies the character encoding of the message body for cases
6.108 + where no such information is available, but where the default encoding
6.109 + is to be overridden.
6.110 +
6.111 + Returns a dictionary mapping field names to lists of values (even if a
6.112 + single value is associated with any given field name). Each value is
6.113 + either a Unicode object (representing a simple form field, for example)
6.114 + or a plain string (representing a file upload form field, for example).
6.115 +
6.116 + Where a given field name is used in both the path and message body to
6.117 + specify values, the values from both sources will be combined into a
6.118 + single list associated with that field name.
6.119 + """
6.120 +
6.121 + return self._get_fields(encoding)
6.122 +
6.123 def get_user(self):
6.124
6.125 """
7.1 --- a/WebStack/Webware.py Mon Sep 06 21:29:59 2004 +0000
7.2 +++ b/WebStack/Webware.py Mon Sep 06 21:31:04 2004 +0000
7.3 @@ -8,7 +8,7 @@
7.4 from cgi import parse_qs
7.5 import StringIO
7.6 from Helpers import Environment
7.7 -from Helpers.Request import Cookie, get_body_field
7.8 +from Helpers.Request import Cookie, get_body_field, filter_fields
7.9 from Helpers.Response import ConvertingStream
7.10
7.11 class Transaction(Generic.Transaction):
7.12 @@ -158,23 +158,30 @@
7.13 def get_fields_from_path(self):
7.14
7.15 """
7.16 - Extracts the form fields from the path specified in the transaction. The
7.17 - underlying framework may refuse to supply fields from the path if
7.18 - handling a POST transaction.
7.19 + Extracts fields (or request parameters) from the path specified in the
7.20 + transaction. The underlying framework may refuse to supply fields from
7.21 + the path if handling a POST transaction.
7.22
7.23 Returns a dictionary mapping field names to lists of values (even if a
7.24 single value is associated with any given field name).
7.25 """
7.26
7.27 - return parse_qs(self.get_query_string(), keep_blank_values=1)
7.28 + # NOTE: Support at best ISO-8859-1 values.
7.29 +
7.30 + fields = {}
7.31 + for name, values in parse_qs(self.get_query_string(), keep_blank_values=1).items():
7.32 + fields[name] = []
7.33 + for value in values:
7.34 + fields[name].append(unicode(value, "iso-8859-1"))
7.35 + return fields
7.36
7.37 def get_fields_from_body(self, encoding=None):
7.38
7.39 """
7.40 - Extracts the form fields from the message body in the transaction. The
7.41 - optional 'encoding' parameter specifies the character encoding of the
7.42 - message body for cases where no such information is available, but where
7.43 - the default encoding is to be overridden.
7.44 + Extracts fields (or request parameters) from the message body in the
7.45 + transaction. The optional 'encoding' parameter specifies the character
7.46 + encoding of the message body for cases where no such information is
7.47 + available, but where the default encoding is to be overridden.
7.48
7.49 Returns a dictionary mapping field names to lists of values (even if a
7.50 single value is associated with any given field name). Each value is
7.51 @@ -182,6 +189,11 @@
7.52 or a plain string (representing a file upload form field, for example).
7.53 """
7.54
7.55 + all_fields = self._get_fields(encoding)
7.56 + fields_from_path = self.get_fields_from_path()
7.57 + return filter_fields(all_fields, fields_from_path)
7.58 +
7.59 + def _get_fields(self, encoding=None):
7.60 encoding = encoding or self.get_content_type().charset or self.default_charset
7.61 fields = {}
7.62 for field_name, field_values in self.trans.request().fields().items():
7.63 @@ -193,6 +205,27 @@
7.64 fields[field_name] = [get_body_field(field_values, encoding)]
7.65 return fields
7.66
7.67 + def get_fields(self, encoding=None):
7.68 +
7.69 + """
7.70 + Extracts fields (or request parameters) from both the path specified in
7.71 + the transaction as well as the message body. The optional 'encoding'
7.72 + parameter specifies the character encoding of the message body for cases
7.73 + where no such information is available, but where the default encoding
7.74 + is to be overridden.
7.75 +
7.76 + Returns a dictionary mapping field names to lists of values (even if a
7.77 + single value is associated with any given field name). Each value is
7.78 + either a Unicode object (representing a simple form field, for example)
7.79 + or a plain string (representing a file upload form field, for example).
7.80 +
7.81 + Where a given field name is used in both the path and message body to
7.82 + specify values, the values from both sources will be combined into a
7.83 + single list associated with that field name.
7.84 + """
7.85 +
7.86 + return self._get_fields(encoding)
7.87 +
7.88 def get_user(self):
7.89
7.90 """
8.1 --- a/WebStack/Zope.py Mon Sep 06 21:29:59 2004 +0000
8.2 +++ b/WebStack/Zope.py Mon Sep 06 21:31:04 2004 +0000
8.3 @@ -8,7 +8,7 @@
8.4
8.5 import Generic
8.6 from Helpers import Environment
8.7 -from Helpers.Request import Cookie, get_body_field
8.8 +from Helpers.Request import Cookie, get_body_field, filter_fields
8.9 from Helpers.Response import ConvertingStream
8.10 from ZPublisher.HTTPRequest import FileUpload
8.11 import cgi
8.12 @@ -151,23 +151,30 @@
8.13 def get_fields_from_path(self):
8.14
8.15 """
8.16 - Extracts the form fields from the path specified in the transaction. The
8.17 - underlying framework may refuse to supply fields from the path if
8.18 - handling a POST transaction.
8.19 + Extracts fields (or request parameters) from the path specified in the
8.20 + transaction. The underlying framework may refuse to supply fields from
8.21 + the path if handling a POST transaction.
8.22
8.23 Returns a dictionary mapping field names to lists of values (even if a
8.24 single value is associated with any given field name).
8.25 """
8.26
8.27 - return cgi.parse_qs(self.get_query_string())
8.28 + # NOTE: Support at best ISO-8859-1 values.
8.29 +
8.30 + fields = {}
8.31 + for name, values in cgi.parse_qs(self.get_query_string()).items():
8.32 + fields[name] = []
8.33 + for value in values:
8.34 + fields[name].append(unicode(value, "iso-8859-1"))
8.35 + return fields
8.36
8.37 def get_fields_from_body(self, encoding=None):
8.38
8.39 """
8.40 - Extracts the form fields from the message body in the transaction. The
8.41 - optional 'encoding' parameter specifies the character encoding of the
8.42 - message body for cases where no such information is available, but where
8.43 - the default encoding is to be overridden.
8.44 + Extracts fields (or request parameters) from the message body in the
8.45 + transaction. The optional 'encoding' parameter specifies the character
8.46 + encoding of the message body for cases where no such information is
8.47 + available, but where the default encoding is to be overridden.
8.48
8.49 Returns a dictionary mapping field names to lists of values (even if a
8.50 single value is associated with any given field name). Each value is
8.51 @@ -175,41 +182,57 @@
8.52 or a plain string (representing a file upload form field, for example).
8.53 """
8.54
8.55 - # Fix the inclusion of path fields since this prevents Unicode conversion.
8.56 + all_fields = self._get_fields(encoding)
8.57 + fields_from_path = self.get_fields_from_path()
8.58 + return filter_fields(all_fields, fields_from_path)
8.59
8.60 - fields_from_path = self.get_fields_from_path()
8.61 -
8.62 + def _get_fields(self, encoding=None):
8.63 encoding = encoding or self.get_content_type().charset or self.default_charset
8.64 fields = {}
8.65 for field_name, field_values in self.request.form.items():
8.66
8.67 - # Find the path values for this field (for filtering below).
8.68 -
8.69 - if fields_from_path.has_key(field_name):
8.70 - field_from_path_values = fields_from_path[field_name]
8.71 - if type(field_from_path_values) != type([]):
8.72 - field_from_path_values = [field_from_path_values]
8.73 - else:
8.74 - field_from_path_values = []
8.75 -
8.76 # Find the body values.
8.77
8.78 if type(field_values) == type([]):
8.79 fields[field_name] = []
8.80 for field_str in field_values:
8.81 - # Filter path values.
8.82 - if field_str not in field_from_path_values:
8.83 - fields[field_name].append(get_body_field(field_str, encoding))
8.84 + fields[field_name].append(get_body_field(field_str, encoding))
8.85 + else:
8.86 + fields[field_name] = [get_body_field(field_values, encoding)]
8.87 +
8.88 + return fields
8.89
8.90 - # Remove filtered fields.
8.91 + def get_fields(self, encoding=None):
8.92 +
8.93 + """
8.94 + Extracts fields (or request parameters) from both the path specified in
8.95 + the transaction as well as the message body. The optional 'encoding'
8.96 + parameter specifies the character encoding of the message body for cases
8.97 + where no such information is available, but where the default encoding
8.98 + is to be overridden.
8.99
8.100 - if fields[field_name] == []:
8.101 - del fields[field_name]
8.102 - else:
8.103 - # Filter path values.
8.104 - if field_values not in field_from_path_values:
8.105 - fields[field_name] = [get_body_field(field_values, encoding)]
8.106 + Returns a dictionary mapping field names to lists of values (even if a
8.107 + single value is associated with any given field name). Each value is
8.108 + either a Unicode object (representing a simple form field, for example)
8.109 + or a plain string (representing a file upload form field, for example).
8.110 +
8.111 + Where a given field name is used in both the path and message body to
8.112 + specify values, the values from both sources will be combined into a
8.113 + single list associated with that field name.
8.114 + """
8.115
8.116 + # NOTE: Zope seems to provide only body fields upon POST requests.
8.117 +
8.118 + if self.get_request_method() == "GET":
8.119 + return self._get_fields(encoding)
8.120 + else:
8.121 + fields = {}
8.122 + fields.update(self.get_fields_from_path())
8.123 + for name, values in self._get_fields(encoding).items():
8.124 + if not fields.has_key(name):
8.125 + fields[name] = values
8.126 + else:
8.127 + fields[name] += values
8.128 return fields
8.129
8.130 def get_user(self):