1.1 --- a/WebStack/Twisted.py Fri Jun 20 22:23:58 2008 +0200
1.2 +++ b/WebStack/Twisted.py Sun Jun 06 15:13:39 2004 +0000
1.3 @@ -7,6 +7,7 @@
1.4 import Generic
1.5 from Helpers.Auth import UserInfo
1.6 from Helpers.Request import Cookie
1.7 +from Helpers.Response import ConvertingStream
1.8 from cgi import parse_qs
1.9
1.10 class Transaction(Generic.Transaction):
1.11 @@ -21,6 +22,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 @@ -149,17 +151,26 @@
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 - return self.trans.args
1.39 + encoding = self.get_content_type().charset or encoding or "iso-8859-1"
1.40 + fields = {}
1.41 + for field_name, field_values in self.trans.args.items():
1.42 + fields[field_name] = []
1.43 + for field_value in field_values:
1.44 + fields[field_name].append(unicode(field_value, encoding))
1.45 + return fields
1.46
1.47 def get_user(self):
1.48
1.49 @@ -222,7 +233,13 @@
1.50 the transaction.
1.51 """
1.52
1.53 - return self.trans
1.54 + # Unicode can upset this operation. Using either the specified charset,
1.55 + # the same charset as that used in the request, or a default encoding.
1.56 +
1.57 + encoding = self.get_content_type().charset or "utf-8"
1.58 + if self.content_type:
1.59 + encoding = self.content_type.charset or encoding
1.60 + return ConvertingStream(self.trans, encoding)
1.61
1.62 def get_response_code(self):
1.63
1.64 @@ -259,6 +276,9 @@
1.65 response.
1.66 """
1.67
1.68 + # Remember the content type for encoding purposes later.
1.69 +
1.70 + self.content_type = content_type
1.71 self.trans.setHeader("Content-Type", self.format_content_type(content_type))
1.72
1.73 # Higher level response-related methods.