# HG changeset patch # User paulb # Date 1094247338 0 # Node ID 9e59e8a3d2707514e185e7bacbda2f5442828167 # Parent c1f805f34772b75a391cff033efbf1c8693385e7 [project @ 2004-09-03 21:35:38 by paulb] Fixed session handling. Improved multipart handling (and hopefully introduced proper conversion from character sets to Unicode in each part). Removed some commentary from the Twisted and Zope modules. diff -r c1f805f34772 -r 9e59e8a3d270 WebStack/JavaServlet.py --- a/WebStack/JavaServlet.py Fri Sep 03 21:34:29 2004 +0000 +++ b/WebStack/JavaServlet.py Fri Sep 03 21:35:38 2004 +0000 @@ -229,7 +229,9 @@ """ # There may not be a reliable means of extracting only the fields from - # the path using the API. + # the path using the API. Moreover, any access to the request parameters + # disrupts the proper extraction and decoding of the request parameters + # which originated in the request body. return get_fields_from_query_string(self.get_query_string(), java.net.URLDecoder().decode) @@ -247,6 +249,11 @@ or a plain string (representing a file upload form field, for example). """ + # Override the default encoding if requested. + + if encoding is not None: + self.request.setCharacterEncoding(encoding) + # Where the content type is "multipart/form-data", we use javax.mail # functionality. Otherwise, we use the Servlet API's parameter access # methods. @@ -255,11 +262,6 @@ fields = self._get_fields_from_message() else: - # Override the default encoding if requested. - - if encoding is not None: - self.request.setCharacterEncoding(encoding) - # There may not be a reliable means of extracting only the fields # the message body using the API. Remove fields originating from the # path in the mixture provided by the API. @@ -280,7 +282,6 @@ else: field_from_path_values = [] - # Filter out path values. fields[field_name] = [] @@ -436,7 +437,11 @@ (where appropriate) and returned. """ - return self.request.getSession(create) + session = self.request.getSession(create) + if session: + return Session(session) + else: + return None def expire_session(self): @@ -445,7 +450,7 @@ transaction. """ - session = self.get_session(create=0) + session = self.request.getSession(0) if session: session.invalidate() @@ -506,26 +511,32 @@ # Should get: form-data; name="x" - disposition = part.getHeader("Content-Disposition")[0].split(";") + disposition = part.getHeader("Content-Disposition")[0] + name = self._get_header_attribute(disposition, "name") - # Locate: name="x" + # Store and optionally convert the field. - if len(disposition) > 1: - for attribute in disposition[1:]: - t = attribute.split("=") - if len(t) == 2 and t[0].strip() == "name": + if name is not None: + if not fields.has_key(name): + fields[name] = [] + fields[name].append(subcontent) - # Locate: "x" -> Locate: x + # Otherwise, descend deeper into the multipart hierarchy. - name = t[1].strip()[1:-1] - if not fields.has_key(name): - fields[name] = [] - fields[name].append(subcontent) else: fields.update(self._get_fields_from_multipart(subcontent)) return fields + def _get_header_attribute(self, header_value, attribute_name): + parts = header_value.split(";") + if len(parts) > 1: + for attribute in parts[1:]: + t = attribute.split("=") + if len(t) == 2 and t[0].strip() == attribute_name: + return t[1].strip()[1:-1] + return None + class Session: """ diff -r c1f805f34772 -r 9e59e8a3d270 WebStack/Twisted.py --- a/WebStack/Twisted.py Fri Sep 03 21:34:29 2004 +0000 +++ b/WebStack/Twisted.py Fri Sep 03 21:35:38 2004 +0000 @@ -159,8 +159,6 @@ single value is associated with any given field name). Each value is either a Unicode object (representing a simple form field, for example) or a plain string (representing a file upload form field, for example). - - NOTE: This still does not seem to work with UTF-16. """ # Fix the inclusion of path fields since this prevents Unicode conversion. diff -r c1f805f34772 -r 9e59e8a3d270 WebStack/Zope.py --- a/WebStack/Zope.py Fri Sep 03 21:34:29 2004 +0000 +++ b/WebStack/Zope.py Fri Sep 03 21:35:38 2004 +0000 @@ -173,8 +173,6 @@ single value is associated with any given field name). Each value is either a Unicode object (representing a simple form field, for example) or a plain string (representing a file upload form field, for example). - - NOTE: This still does not seem to work with UTF-16. """ # Fix the inclusion of path fields since this prevents Unicode conversion.