1.1 --- a/WebStack/Webware.py Fri Jun 20 22:23:58 2008 +0200
1.2 +++ b/WebStack/Webware.py Sun Jun 06 15:13:39 2004 +0000
1.3 @@ -9,6 +9,7 @@
1.4 import StringIO
1.5 from Helpers import Environment
1.6 from Helpers.Request import Cookie
1.7 +from Helpers.Response import ConvertingStream
1.8
1.9 class Transaction(Generic.Transaction):
1.10
1.11 @@ -22,6 +23,7 @@
1.12
1.13 self.trans = trans
1.14 self.user = None
1.15 + self.content_type = None
1.16
1.17 # Request-related methods.
1.18
1.19 @@ -165,24 +167,31 @@
1.20
1.21 return parse_qs(self.get_query_string(), keep_blank_values=1)
1.22
1.23 - def get_fields_from_body(self):
1.24 + def get_fields_from_body(self, encoding=None):
1.25
1.26 """
1.27 A framework-specific method which extracts the form fields from the
1.28 - message body in the transaction.
1.29 + message body in the transaction. The optional 'encoding' parameter
1.30 + specifies the character encoding of the message body for cases where no
1.31 + such information is available, but where the default encoding is to be
1.32 + overridden.
1.33
1.34 Returns a dictionary mapping field names to lists of values (even if a
1.35 single value is associated with any given field name).
1.36 """
1.37
1.38 + encoding = self.get_content_type().charset or encoding or "iso-8859-1"
1.39 +
1.40 # Fix the non-list results.
1.41
1.42 fields = {}
1.43 - for field_name, field_value in self.trans.request().fields().items():
1.44 - if type(field_value) == type([]):
1.45 - fields[field_name] = field_value
1.46 + for field_name, field_values in self.trans.request().fields().items():
1.47 + if type(field_values) == type([]):
1.48 + fields[field_name] = []
1.49 + for field_value in field_values:
1.50 + fields[field_name].append(unicode(field_value, encoding))
1.51 else:
1.52 - fields[field_name] = [field_value]
1.53 + fields[field_name] = [unicode(field_values, encoding)]
1.54 return fields
1.55
1.56 def get_user(self):
1.57 @@ -244,7 +253,13 @@
1.58 the transaction.
1.59 """
1.60
1.61 - return self.trans.response()
1.62 + # Unicode can upset this operation. Using either the specified charset,
1.63 + # the same charset as that used in the request, or a default encoding.
1.64 +
1.65 + encoding = self.get_content_type().charset or "utf-8"
1.66 + if self.content_type:
1.67 + encoding = self.content_type.charset or encoding
1.68 + return ConvertingStream(self.trans.response(), encoding)
1.69
1.70 def get_response_code(self):
1.71
1.72 @@ -288,6 +303,9 @@
1.73 response.
1.74 """
1.75
1.76 + # Remember the content type for encoding purposes later.
1.77 +
1.78 + self.content_type = content_type
1.79 return self.trans.response().setHeader("Content-Type", self.format_content_type(content_type))
1.80
1.81 # Higher level response-related methods.