1.1 --- a/WebStack/CGI.py Mon Sep 06 21:29:59 2004 +0000
1.2 +++ b/WebStack/CGI.py Mon Sep 06 21:31:04 2004 +0000
1.3 @@ -192,23 +192,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 @@ -219,7 +226,10 @@
1.43 encoding = encoding or self.get_content_type().charset or self.default_charset
1.44
1.45 if self.storage_body is None:
1.46 - self.storage_body = FieldStorage(fp=self.get_request_stream(), keep_blank_values=1)
1.47 + self.storage_body = FieldStorage(fp=self.get_request_stream(),
1.48 + headers={"content-type" : str(self.get_content_type())},
1.49 + environ={"REQUEST_METHOD" : self.get_request_method()},
1.50 + keep_blank_values=1)
1.51
1.52 # Avoid strange design issues with FieldStorage by checking the internal
1.53 # field list directly.
1.54 @@ -233,6 +243,36 @@
1.55
1.56 return fields
1.57
1.58 + def get_fields(self, encoding=None):
1.59 +
1.60 + """
1.61 + Extracts fields (or request parameters) from both the path specified in
1.62 + the transaction as well as the message body. The optional 'encoding'
1.63 + parameter specifies the character encoding of the message body for cases
1.64 + where no such information is available, but where the default encoding
1.65 + is to be overridden.
1.66 +
1.67 + Returns a dictionary mapping field names to lists of values (even if a
1.68 + single value is associated with any given field name). Each value is
1.69 + either a Unicode object (representing a simple form field, for example)
1.70 + or a plain string (representing a file upload form field, for example).
1.71 +
1.72 + Where a given field name is used in both the path and message body to
1.73 + specify values, the values from both sources will be combined into a
1.74 + single list associated with that field name.
1.75 + """
1.76 +
1.77 + # Combine the two sources.
1.78 +
1.79 + fields = {}
1.80 + fields.update(self.get_fields_from_path())
1.81 + for name, values in self.get_fields_from_body(encoding).items():
1.82 + if not fields.has_key(name):
1.83 + fields[name] = values
1.84 + else:
1.85 + fields[name] += values
1.86 + return fields
1.87 +
1.88 def get_user(self):
1.89
1.90 """