1.1 --- a/WebStack/ModPython.py Fri Jun 20 22:23:58 2008 +0200
1.2 +++ b/WebStack/ModPython.py Sun Jun 06 15:13:39 2004 +0000
1.3 @@ -5,6 +5,7 @@
1.4 """
1.5
1.6 import Generic
1.7 +from Helpers.Response import ConvertingStream
1.8 from mod_python.util import parse_qs, FieldStorage
1.9 from mod_python import apache
1.10 try:
1.11 @@ -26,6 +27,7 @@
1.12 self.trans = trans
1.13 self.response_code = apache.OK
1.14 self.user = None
1.15 + self.content_type = None
1.16
1.17 # Cached information.
1.18
1.19 @@ -152,11 +154,14 @@
1.20
1.21 return parse_qs(self.get_query_string(), 1) # 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 @@ -165,6 +170,8 @@
1.37 body with fields found in the path.
1.38 """
1.39
1.40 + encoding = self.get_content_type().charset or encoding or "iso-8859-1"
1.41 +
1.42 if self.storage_body is None:
1.43 self.storage_body = FieldStorage(self.trans, keep_blank_values=1)
1.44
1.45 @@ -174,7 +181,7 @@
1.46 for field in self.storage_body.list:
1.47 if not fields.has_key(field.name):
1.48 fields[field.name] = []
1.49 - fields[field.name].append(field.value)
1.50 + fields[field.name].append(unicode(field.value, encoding))
1.51 return fields
1.52
1.53 def get_user(self):
1.54 @@ -229,7 +236,13 @@
1.55 the transaction.
1.56 """
1.57
1.58 - return self.trans
1.59 + # Unicode can upset this operation. Using either the specified charset,
1.60 + # the same charset as that used in the request, or a default encoding.
1.61 +
1.62 + encoding = self.get_content_type().charset or "utf-8"
1.63 + if self.content_type:
1.64 + encoding = self.content_type.charset or encoding
1.65 + return ConvertingStream(self.trans, encoding)
1.66
1.67 def get_response_code(self):
1.68
1.69 @@ -264,6 +277,9 @@
1.70 response.
1.71 """
1.72
1.73 + # Remember the content type for encoding purposes later.
1.74 +
1.75 + self.content_type = content_type
1.76 self.trans.content_type = self.format_content_type(content_type)
1.77
1.78 def set_cookie(self, cookie):