1.1 --- a/WebStack/BaseHTTPRequestHandler.py Fri Jun 20 22:23:58 2008 +0200
1.2 +++ b/WebStack/BaseHTTPRequestHandler.py Sun Jun 06 15:13:39 2004 +0000
1.3 @@ -6,6 +6,7 @@
1.4
1.5 import Generic
1.6 from Helpers.Request import MessageBodyStream
1.7 +from Helpers.Response import ConvertingStream
1.8 from Helpers.Auth import UserInfo
1.9 from cgi import parse_qs, FieldStorage
1.10 import Cookie
1.11 @@ -192,16 +193,21 @@
1.12
1.13 return parse_qs(self.get_query_string(), keep_blank_values=1)
1.14
1.15 - def get_fields_from_body(self):
1.16 + def get_fields_from_body(self, encoding=None):
1.17
1.18 """
1.19 A framework-specific method which extracts the form fields from the
1.20 - message body in the transaction.
1.21 + message body in the transaction. The optional 'encoding' parameter
1.22 + specifies the character encoding of the message body for cases where no
1.23 + such information is available, but where the default encoding is to be
1.24 + overridden.
1.25
1.26 Returns a dictionary mapping field names to lists of values (even if a
1.27 single value is associated with any given field name).
1.28 """
1.29
1.30 + encoding = self.get_content_type().charset or encoding or "iso-8859-1"
1.31 +
1.32 if self.storage_body is None:
1.33 self.storage_body = FieldStorage(fp=self.get_request_stream(), headers=self.get_headers(),
1.34 environ={"REQUEST_METHOD" : self.get_request_method()}, keep_blank_values=1)
1.35 @@ -215,7 +221,9 @@
1.36 # Traverse the storage, finding each field value.
1.37
1.38 for field_name in self.storage_body.keys():
1.39 - fields[field_name] = self.storage_body.getlist(field_name)
1.40 + fields[field_name] = []
1.41 + for field_value in self.storage_body.getlist(field_name):
1.42 + fields[field_name].append(unicode(field_value, encoding))
1.43 return fields
1.44
1.45 def get_user(self):
1.46 @@ -269,8 +277,13 @@
1.47 """
1.48
1.49 # Return a stream which is later emptied into the real stream.
1.50 + # Unicode can upset this operation. Using either the specified charset,
1.51 + # the same charset as that used in the request, or a default encoding.
1.52
1.53 - return self.content
1.54 + encoding = self.get_content_type().charset or "utf-8"
1.55 + if self.content_type:
1.56 + encoding = self.content_type.charset or encoding
1.57 + return ConvertingStream(self.content, encoding)
1.58
1.59 def get_response_code(self):
1.60
2.1 --- a/WebStack/CGI.py Fri Jun 20 22:23:58 2008 +0200
2.2 +++ b/WebStack/CGI.py Sun Jun 06 15:13:39 2004 +0000
2.3 @@ -7,6 +7,7 @@
2.4 import Generic
2.5 import os, sys
2.6 from Helpers.Request import MessageBodyStream
2.7 +from Helpers.Response import ConvertingStream
2.8 from Helpers.Auth import UserInfo
2.9 from Helpers import Environment
2.10 from cgi import parse_qs, FieldStorage
2.11 @@ -196,16 +197,21 @@
2.12
2.13 return parse_qs(self.get_query_string(), keep_blank_values=1)
2.14
2.15 - def get_fields_from_body(self):
2.16 + def get_fields_from_body(self, encoding=None):
2.17
2.18 """
2.19 A framework-specific method which extracts the form fields from the
2.20 - message body in the transaction.
2.21 + message body in the transaction. The optional 'encoding' parameter
2.22 + specifies the character encoding of the message body for cases where no
2.23 + such information is available, but where the default encoding is to be
2.24 + overridden.
2.25
2.26 Returns a dictionary mapping field names to lists of values (even if a
2.27 single value is associated with any given field name).
2.28 """
2.29
2.30 + encoding = self.get_content_type().charset or encoding or "iso-8859-1"
2.31 +
2.32 if self.storage_body is None:
2.33 self.storage_body = FieldStorage(fp=self.get_request_stream(), keep_blank_values=1)
2.34
2.35 @@ -218,7 +224,9 @@
2.36 # Traverse the storage, finding each field value.
2.37
2.38 for field_name in self.storage_body.keys():
2.39 - fields[field_name] = self.storage_body.getlist(field_name)
2.40 + fields[field_name] = []
2.41 + for field_value in self.storage_body.getlist(field_name):
2.42 + fields[field_name].append(unicode(field_value, encoding))
2.43 return fields
2.44
2.45 def get_user(self):
2.46 @@ -268,8 +276,13 @@
2.47 """
2.48
2.49 # Return a stream which is later emptied into the real stream.
2.50 + # Unicode can upset this operation. Using either the specified charset,
2.51 + # the same charset as that used in the request, or a default encoding.
2.52
2.53 - return self.content
2.54 + encoding = self.get_content_type().charset or "utf-8"
2.55 + if self.content_type:
2.56 + encoding = self.content_type.charset or encoding
2.57 + return ConvertingStream(self.content, encoding)
2.58
2.59 def get_response_code(self):
2.60
3.1 --- a/WebStack/Generic.py Fri Jun 20 22:23:58 2008 +0200
3.2 +++ b/WebStack/Generic.py Sun Jun 06 15:13:39 2004 +0000
3.3 @@ -238,11 +238,14 @@
3.4
3.5 raise NotImplementedError, "get_fields_from_path"
3.6
3.7 - def get_fields_from_body(self):
3.8 + def get_fields_from_body(self, encoding=None):
3.9
3.10 """
3.11 A framework-specific method which extracts the form fields from the
3.12 - message body in the transaction.
3.13 + message body in the transaction. The optional 'encoding' parameter
3.14 + specifies the character encoding of the message body for cases where no
3.15 + such information is available, but where the default encoding is to be
3.16 + overridden.
3.17
3.18 Returns a dictionary mapping field names to lists of values (even if a
3.19 single value is associated with any given field name).
4.1 --- a/WebStack/JavaServlet.py Fri Jun 20 22:23:58 2008 +0200
4.2 +++ b/WebStack/JavaServlet.py Sun Jun 06 15:13:39 2004 +0000
4.3 @@ -222,17 +222,21 @@
4.4
4.5 return self.get_fields_from_body()
4.6
4.7 - def get_fields_from_body(self):
4.8 + def get_fields_from_body(self, encoding=None):
4.9
4.10 """
4.11 A framework-specific method which extracts the form fields from the
4.12 - message body in the transaction.
4.13 + message body in the transaction. The optional 'encoding' parameter
4.14 + specifies the character encoding of the message body for cases where no
4.15 + such information is available, but where the default encoding is to be
4.16 + overridden.
4.17
4.18 Returns a dictionary mapping field names to lists of values (even if a
4.19 single value is associated with any given field name).
4.20
4.21 NOTE: There may not be a reliable means of extracting only the fields
4.22 - NOTE: from the message body.
4.23 + NOTE: from the message body. Moreover, the encoding of the fields may
4.24 + NOTE: not be pertinent.
4.25 """
4.26
4.27 parameter_map = self.request.getParameterMap()
5.1 --- a/WebStack/ModPython.py Fri Jun 20 22:23:58 2008 +0200
5.2 +++ b/WebStack/ModPython.py Sun Jun 06 15:13:39 2004 +0000
5.3 @@ -5,6 +5,7 @@
5.4 """
5.5
5.6 import Generic
5.7 +from Helpers.Response import ConvertingStream
5.8 from mod_python.util import parse_qs, FieldStorage
5.9 from mod_python import apache
5.10 try:
5.11 @@ -26,6 +27,7 @@
5.12 self.trans = trans
5.13 self.response_code = apache.OK
5.14 self.user = None
5.15 + self.content_type = None
5.16
5.17 # Cached information.
5.18
5.19 @@ -152,11 +154,14 @@
5.20
5.21 return parse_qs(self.get_query_string(), 1) # keep_blank_values=1
5.22
5.23 - def get_fields_from_body(self):
5.24 + def get_fields_from_body(self, encoding=None):
5.25
5.26 """
5.27 A framework-specific method which extracts the form fields from the
5.28 - message body in the transaction.
5.29 + message body in the transaction. The optional 'encoding' parameter
5.30 + specifies the character encoding of the message body for cases where no
5.31 + such information is available, but where the default encoding is to be
5.32 + overridden.
5.33
5.34 Returns a dictionary mapping field names to lists of values (even if a
5.35 single value is associated with any given field name).
5.36 @@ -165,6 +170,8 @@
5.37 body with fields found in the path.
5.38 """
5.39
5.40 + encoding = self.get_content_type().charset or encoding or "iso-8859-1"
5.41 +
5.42 if self.storage_body is None:
5.43 self.storage_body = FieldStorage(self.trans, keep_blank_values=1)
5.44
5.45 @@ -174,7 +181,7 @@
5.46 for field in self.storage_body.list:
5.47 if not fields.has_key(field.name):
5.48 fields[field.name] = []
5.49 - fields[field.name].append(field.value)
5.50 + fields[field.name].append(unicode(field.value, encoding))
5.51 return fields
5.52
5.53 def get_user(self):
5.54 @@ -229,7 +236,13 @@
5.55 the transaction.
5.56 """
5.57
5.58 - return self.trans
5.59 + # Unicode can upset this operation. Using either the specified charset,
5.60 + # the same charset as that used in the request, or a default encoding.
5.61 +
5.62 + encoding = self.get_content_type().charset or "utf-8"
5.63 + if self.content_type:
5.64 + encoding = self.content_type.charset or encoding
5.65 + return ConvertingStream(self.trans, encoding)
5.66
5.67 def get_response_code(self):
5.68
5.69 @@ -264,6 +277,9 @@
5.70 response.
5.71 """
5.72
5.73 + # Remember the content type for encoding purposes later.
5.74 +
5.75 + self.content_type = content_type
5.76 self.trans.content_type = self.format_content_type(content_type)
5.77
5.78 def set_cookie(self, cookie):
6.1 --- a/WebStack/Twisted.py Fri Jun 20 22:23:58 2008 +0200
6.2 +++ b/WebStack/Twisted.py Sun Jun 06 15:13:39 2004 +0000
6.3 @@ -7,6 +7,7 @@
6.4 import Generic
6.5 from Helpers.Auth import UserInfo
6.6 from Helpers.Request import Cookie
6.7 +from Helpers.Response import ConvertingStream
6.8 from cgi import parse_qs
6.9
6.10 class Transaction(Generic.Transaction):
6.11 @@ -21,6 +22,7 @@
6.12
6.13 self.trans = trans
6.14 self.user = None
6.15 + self.content_type = None
6.16
6.17 # Request-related methods.
6.18
6.19 @@ -149,17 +151,26 @@
6.20
6.21 return parse_qs(self.get_query_string(), keep_blank_values=1)
6.22
6.23 - def get_fields_from_body(self):
6.24 + def get_fields_from_body(self, encoding=None):
6.25
6.26 """
6.27 A framework-specific method which extracts the form fields from the
6.28 - message body in the transaction.
6.29 + message body in the transaction. The optional 'encoding' parameter
6.30 + specifies the character encoding of the message body for cases where no
6.31 + such information is available, but where the default encoding is to be
6.32 + overridden.
6.33
6.34 Returns a dictionary mapping field names to lists of values (even if a
6.35 single value is associated with any given field name).
6.36 """
6.37
6.38 - return self.trans.args
6.39 + encoding = self.get_content_type().charset or encoding or "iso-8859-1"
6.40 + fields = {}
6.41 + for field_name, field_values in self.trans.args.items():
6.42 + fields[field_name] = []
6.43 + for field_value in field_values:
6.44 + fields[field_name].append(unicode(field_value, encoding))
6.45 + return fields
6.46
6.47 def get_user(self):
6.48
6.49 @@ -222,7 +233,13 @@
6.50 the transaction.
6.51 """
6.52
6.53 - return self.trans
6.54 + # Unicode can upset this operation. Using either the specified charset,
6.55 + # the same charset as that used in the request, or a default encoding.
6.56 +
6.57 + encoding = self.get_content_type().charset or "utf-8"
6.58 + if self.content_type:
6.59 + encoding = self.content_type.charset or encoding
6.60 + return ConvertingStream(self.trans, encoding)
6.61
6.62 def get_response_code(self):
6.63
6.64 @@ -259,6 +276,9 @@
6.65 response.
6.66 """
6.67
6.68 + # Remember the content type for encoding purposes later.
6.69 +
6.70 + self.content_type = content_type
6.71 self.trans.setHeader("Content-Type", self.format_content_type(content_type))
6.72
6.73 # Higher level response-related methods.
7.1 --- a/WebStack/Webware.py Fri Jun 20 22:23:58 2008 +0200
7.2 +++ b/WebStack/Webware.py Sun Jun 06 15:13:39 2004 +0000
7.3 @@ -9,6 +9,7 @@
7.4 import StringIO
7.5 from Helpers import Environment
7.6 from Helpers.Request import Cookie
7.7 +from Helpers.Response import ConvertingStream
7.8
7.9 class Transaction(Generic.Transaction):
7.10
7.11 @@ -22,6 +23,7 @@
7.12
7.13 self.trans = trans
7.14 self.user = None
7.15 + self.content_type = None
7.16
7.17 # Request-related methods.
7.18
7.19 @@ -165,24 +167,31 @@
7.20
7.21 return parse_qs(self.get_query_string(), keep_blank_values=1)
7.22
7.23 - def get_fields_from_body(self):
7.24 + def get_fields_from_body(self, encoding=None):
7.25
7.26 """
7.27 A framework-specific method which extracts the form fields from the
7.28 - message body in the transaction.
7.29 + message body in the transaction. The optional 'encoding' parameter
7.30 + specifies the character encoding of the message body for cases where no
7.31 + such information is available, but where the default encoding is to be
7.32 + overridden.
7.33
7.34 Returns a dictionary mapping field names to lists of values (even if a
7.35 single value is associated with any given field name).
7.36 """
7.37
7.38 + encoding = self.get_content_type().charset or encoding or "iso-8859-1"
7.39 +
7.40 # Fix the non-list results.
7.41
7.42 fields = {}
7.43 - for field_name, field_value in self.trans.request().fields().items():
7.44 - if type(field_value) == type([]):
7.45 - fields[field_name] = field_value
7.46 + for field_name, field_values in self.trans.request().fields().items():
7.47 + if type(field_values) == type([]):
7.48 + fields[field_name] = []
7.49 + for field_value in field_values:
7.50 + fields[field_name].append(unicode(field_value, encoding))
7.51 else:
7.52 - fields[field_name] = [field_value]
7.53 + fields[field_name] = [unicode(field_values, encoding)]
7.54 return fields
7.55
7.56 def get_user(self):
7.57 @@ -244,7 +253,13 @@
7.58 the transaction.
7.59 """
7.60
7.61 - return self.trans.response()
7.62 + # Unicode can upset this operation. Using either the specified charset,
7.63 + # the same charset as that used in the request, or a default encoding.
7.64 +
7.65 + encoding = self.get_content_type().charset or "utf-8"
7.66 + if self.content_type:
7.67 + encoding = self.content_type.charset or encoding
7.68 + return ConvertingStream(self.trans.response(), encoding)
7.69
7.70 def get_response_code(self):
7.71
7.72 @@ -288,6 +303,9 @@
7.73 response.
7.74 """
7.75
7.76 + # Remember the content type for encoding purposes later.
7.77 +
7.78 + self.content_type = content_type
7.79 return self.trans.response().setHeader("Content-Type", self.format_content_type(content_type))
7.80
7.81 # Higher level response-related methods.