1.1 --- a/WebStack/BaseHTTPRequestHandler.py Fri Aug 27 23:51:02 2004 +0000
1.2 +++ b/WebStack/BaseHTTPRequestHandler.py Fri Aug 27 23:51:51 2004 +0000
1.3 @@ -207,7 +207,10 @@
1.4 the default encoding is to be overridden.
1.5
1.6 Returns a dictionary mapping field names to lists of values (even if a
1.7 - single value is associated with any given field name).
1.8 + single value is associated with any given field name). Each value is
1.9 + either a Unicode object (representing a simple form field, for example)
1.10 + or a file-like object (representing a file upload form field, for
1.11 + example).
1.12 """
1.13
1.14 encoding = self.get_content_type().charset or encoding or "iso-8859-1"
1.15 @@ -227,7 +230,10 @@
1.16 for field_name in self.storage_body.keys():
1.17 fields[field_name] = []
1.18 for field_value in self.storage_body.getlist(field_name):
1.19 - fields[field_name].append(unicode(field_value, encoding))
1.20 + if hasattr(field_value, "file"):
1.21 + fields[field_name].append(field_value.file)
1.22 + else:
1.23 + fields[field_name].append(unicode(field_value, encoding))
1.24 return fields
1.25
1.26 def get_user(self):
2.1 --- a/WebStack/CGI.py Fri Aug 27 23:51:02 2004 +0000
2.2 +++ b/WebStack/CGI.py Fri Aug 27 23:51:51 2004 +0000
2.3 @@ -201,7 +201,10 @@
2.4 the default encoding is to be overridden.
2.5
2.6 Returns a dictionary mapping field names to lists of values (even if a
2.7 - single value is associated with any given field name).
2.8 + single value is associated with any given field name). Each value is
2.9 + either a Unicode object (representing a simple form field, for example)
2.10 + or a file-like object (representing a file upload form field, for
2.11 + example).
2.12 """
2.13
2.14 encoding = self.get_content_type().charset or encoding or "iso-8859-1"
2.15 @@ -220,7 +223,10 @@
2.16 for field_name in self.storage_body.keys():
2.17 fields[field_name] = []
2.18 for field_value in self.storage_body.getlist(field_name):
2.19 - fields[field_name].append(unicode(field_value, encoding))
2.20 + if hasattr(field_value, "file"):
2.21 + fields[field_name].append(field_value.file)
2.22 + else:
2.23 + fields[field_name].append(unicode(field_value, encoding))
2.24 return fields
2.25
2.26 def get_user(self):
3.1 --- a/WebStack/Generic.py Fri Aug 27 23:51:02 2004 +0000
3.2 +++ b/WebStack/Generic.py Fri Aug 27 23:51:51 2004 +0000
3.3 @@ -19,9 +19,10 @@
3.4
3.5 "A container for content type information."
3.6
3.7 - def __init__(self, content_type, charset=None):
3.8 + def __init__(self, content_type, charset=None, attributes=None):
3.9 self.content_type = content_type
3.10 self.charset = charset
3.11 + self.attributes = attributes
3.12
3.13 class Transaction:
3.14
3.15 @@ -51,11 +52,22 @@
3.16 if content_type_field is None:
3.17 return ContentType(None, "iso-8859-1")
3.18
3.19 - t = content_type_field.split("; charset=")
3.20 - if len(t) == 1:
3.21 - return ContentType(t[0], "iso-8859-1")
3.22 - else:
3.23 - return ContentType(t[0], t[1])
3.24 + l = content_type_field.split(";")
3.25 + content_type_attributes = []
3.26 + content_type_charset = "iso-8859-1"
3.27 +
3.28 + # Find the charset and remember all other attributes.
3.29 +
3.30 + content_type_str, attributes = l[0].strip(), l[1:]
3.31 +
3.32 + for attribute in attributes:
3.33 + t = attribute.strip().split("=")
3.34 + if t[0] == "charset" and len(t) > 1:
3.35 + content_type_charset = t[1]
3.36 + else:
3.37 + content_type_attributes.append(t)
3.38 +
3.39 + return ContentType(content_type_str, content_type_charset, content_type_attributes)
3.40
3.41 def format_content_type(self, content_type):
3.42
3.43 @@ -242,7 +254,10 @@
3.44 the default encoding is to be overridden.
3.45
3.46 Returns a dictionary mapping field names to lists of values (even if a
3.47 - single value is associated with any given field name).
3.48 + single value is associated with any given field name). Each value is
3.49 + either a Unicode object (representing a simple form field, for example)
3.50 + or a file-like object (representing a file upload form field, for
3.51 + example).
3.52 """
3.53
3.54 raise NotImplementedError, "get_fields_from_body"
4.1 --- a/WebStack/JavaServlet.py Fri Aug 27 23:51:02 2004 +0000
4.2 +++ b/WebStack/JavaServlet.py Fri Aug 27 23:51:51 2004 +0000
4.3 @@ -8,6 +8,7 @@
4.4 from StringIO import StringIO
4.5 from Helpers.Request import Cookie
4.6 import javax.servlet.http.Cookie
4.7 +from cgi import FieldStorage
4.8
4.9 class Stream:
4.10
4.11 @@ -65,6 +66,10 @@
4.12 cookie_name = cookie.getName()
4.13 self.cookies_in[cookie_name] = Cookie(cookie_name, cookie.getValue())
4.14
4.15 + # Cached information.
4.16 +
4.17 + self.storage_body = None
4.18 +
4.19 def commit(self):
4.20
4.21 """
4.22 @@ -231,18 +236,63 @@
4.23 the default encoding is to be overridden.
4.24
4.25 Returns a dictionary mapping field names to lists of values (even if a
4.26 - single value is associated with any given field name).
4.27 + single value is associated with any given field name). Each value is
4.28 + either a Unicode object (representing a simple form field, for example)
4.29 + or a file-like object (representing a file upload form field, for
4.30 + example).
4.31
4.32 NOTE: There may not be a reliable means of extracting only the fields
4.33 NOTE: from the message body. Moreover, the encoding of the fields may
4.34 NOTE: not be pertinent.
4.35 """
4.36
4.37 - parameter_map = self.request.getParameterMap()
4.38 fields = {}
4.39 - if parameter_map:
4.40 - for key in parameter_map.keySet():
4.41 - fields[key] = parameter_map[key]
4.42 +
4.43 + # Where the content type is "multipart/form-data", we use the
4.44 + # FieldStorage class from the standard library. Otherwise, we use the
4.45 + # Servlet API's parameter access methods.
4.46 +
4.47 + if self.get_content_type() and self.get_content_type().content_type == "multipart/form-data":
4.48 +
4.49 + # NOTE: Taken from WebStack.CGI.get_fields_from_body.
4.50 +
4.51 + if self.storage_body is None:
4.52 + self.storage_body = FieldStorage(fp=self.get_request_stream(), headers=self.get_headers(),
4.53 + environ={"REQUEST_METHOD" : self.get_request_method()}, keep_blank_values=1)
4.54 +
4.55 + # Avoid strange design issues with FieldStorage by checking the internal
4.56 + # field list directly.
4.57 +
4.58 + if self.storage_body.list is not None:
4.59 +
4.60 + # Traverse the storage, finding each field value.
4.61 +
4.62 + for field_name in self.storage_body.keys():
4.63 +
4.64 + # NOTE: Jython 2.1 does not have a cgi module with a
4.65 + # NOTE: FieldStorage.getlist implementation.
4.66 +
4.67 + field_values = self.storage_body.getvalue(field_name)
4.68 +
4.69 + if type(field_values) == type([]):
4.70 + fields[field_name] = []
4.71 + for field_value in field_values:
4.72 + if hasattr(field_value, "file"):
4.73 + fields[field_name].append(field_value.file)
4.74 + else:
4.75 + fields[field_name].append(field_value)
4.76 + else:
4.77 + if hasattr(field_values, "file"):
4.78 + fields[field_name] = [field_values.file]
4.79 + else:
4.80 + fields[field_name] = [field_values]
4.81 +
4.82 + else:
4.83 + parameter_map = self.request.getParameterMap()
4.84 + if parameter_map:
4.85 + for key in parameter_map.keySet():
4.86 + fields[key] = parameter_map[key]
4.87 +
4.88 return fields
4.89
4.90 def get_user(self):
5.1 --- a/WebStack/ModPython.py Fri Aug 27 23:51:02 2004 +0000
5.2 +++ b/WebStack/ModPython.py Fri Aug 27 23:51:51 2004 +0000
5.3 @@ -161,7 +161,10 @@
5.4 the default encoding is to be overridden.
5.5
5.6 Returns a dictionary mapping field names to lists of values (even if a
5.7 - single value is associated with any given field name).
5.8 + single value is associated with any given field name). Each value is
5.9 + either a Unicode object (representing a simple form field, for example)
5.10 + or a file-like object (representing a file upload form field, for
5.11 + example).
5.12
5.13 The mod_python.util.FieldStorage class may augment the fields from the
5.14 body with fields found in the path.
5.15 @@ -178,7 +181,10 @@
5.16 for field in self.storage_body.list:
5.17 if not fields.has_key(field.name):
5.18 fields[field.name] = []
5.19 - fields[field.name].append(unicode(field.value, encoding))
5.20 + if hasattr(field.value, "file"):
5.21 + fields[field.name].append(field.value.file)
5.22 + else:
5.23 + fields[field.name].append(unicode(field.value, encoding))
5.24 return fields
5.25
5.26 def get_user(self):
6.1 --- a/WebStack/Twisted.py Fri Aug 27 23:51:02 2004 +0000
6.2 +++ b/WebStack/Twisted.py Fri Aug 27 23:51:51 2004 +0000
6.3 @@ -156,7 +156,10 @@
6.4 the default encoding is to be overridden.
6.5
6.6 Returns a dictionary mapping field names to lists of values (even if a
6.7 - single value is associated with any given field name).
6.8 + single value is associated with any given field name). Each value is
6.9 + either a Unicode object (representing a simple form field, for example)
6.10 + or a file-like object (representing a file upload form field, for
6.11 + example).
6.12 """
6.13
6.14 encoding = self.get_content_type().charset or encoding or "iso-8859-1"
6.15 @@ -164,7 +167,10 @@
6.16 for field_name, field_values in self.trans.args.items():
6.17 fields[field_name] = []
6.18 for field_value in field_values:
6.19 - fields[field_name].append(unicode(field_value, encoding))
6.20 + if hasattr(field_value, "file"):
6.21 + fields[field_name].append(field_value.file)
6.22 + else:
6.23 + fields[field_name].append(unicode(field_value, encoding))
6.24 return fields
6.25
6.26 def get_user(self):
7.1 --- a/WebStack/Webware.py Fri Aug 27 23:51:02 2004 +0000
7.2 +++ b/WebStack/Webware.py Fri Aug 27 23:51:51 2004 +0000
7.3 @@ -172,7 +172,10 @@
7.4 the default encoding is to be overridden.
7.5
7.6 Returns a dictionary mapping field names to lists of values (even if a
7.7 - single value is associated with any given field name).
7.8 + single value is associated with any given field name). Each value is
7.9 + either a Unicode object (representing a simple form field, for example)
7.10 + or a file-like object (representing a file upload form field, for
7.11 + example).
7.12 """
7.13
7.14 encoding = self.get_content_type().charset or encoding or "iso-8859-1"
7.15 @@ -184,9 +187,15 @@
7.16 if type(field_values) == type([]):
7.17 fields[field_name] = []
7.18 for field_value in field_values:
7.19 - fields[field_name].append(unicode(field_value, encoding))
7.20 + if hasattr(field_value, "file"):
7.21 + fields[field_name].append(field_value.file)
7.22 + else:
7.23 + fields[field_name].append(unicode(field_value, encoding))
7.24 else:
7.25 - fields[field_name] = [unicode(field_values, encoding)]
7.26 + if hasattr(field_values, "file"):
7.27 + fields[field_name] = [field_values.file]
7.28 + else:
7.29 + fields[field_name] = [unicode(field_values, encoding)]
7.30 return fields
7.31
7.32 def get_user(self):
8.1 --- a/WebStack/Zope.py Fri Aug 27 23:51:02 2004 +0000
8.2 +++ b/WebStack/Zope.py Fri Aug 27 23:51:51 2004 +0000
8.3 @@ -10,6 +10,7 @@
8.4 from Helpers import Environment
8.5 from Helpers.Request import Cookie
8.6 from Helpers.Response import ConvertingStream
8.7 +from ZPublisher.HTTPRequest import FileUpload
8.8
8.9 class Transaction(Generic.Transaction):
8.10
8.11 @@ -174,7 +175,10 @@
8.12 the default encoding is to be overridden.
8.13
8.14 Returns a dictionary mapping field names to lists of values (even if a
8.15 - single value is associated with any given field name).
8.16 + single value is associated with any given field name). Each value is
8.17 + either a Unicode object (representing a simple form field, for example)
8.18 + or a file-like object (representing a file upload form field, for
8.19 + example).
8.20
8.21 NOTE: Zope doesn't distinguish between path and body fields.
8.22 """
8.23 @@ -186,7 +190,10 @@
8.24 for field_name, field_values in self.get_fields_from_path().items():
8.25 fields[field_name] = []
8.26 for field_value in field_values:
8.27 - fields[field_name].append(unicode(field_value, encoding))
8.28 + if isinstance(field_value, FileUpload):
8.29 + fields[field_name].append(field_value)
8.30 + else:
8.31 + fields[field_name].append(unicode(field_value, encoding))
8.32 return fields
8.33
8.34 def get_user(self):