Commits

Anonymous committed b9729d2

Backported CherryPy 3.2 encoding technique. Fixes #1035.

Comments (0)

Files changed (2)

cherrypy/_cprequest.py

         if cookie:
             for line in cookie.split("\n"):
                 name, value = line.split(": ", 1)
+                if isinstance(name, unicode):
+                    name = name.encode("ISO-8859-1")
+                if isinstance(value, unicode):
+                    value = headers.encode(value)
                 h.append((name, value))
     
     def check_timeout(self):

cherrypy/lib/http.py

         header_list = []
         for key, v in self.iteritems():
             if isinstance(v, unicode):
-                # HTTP/1.0 says, "Words of *TEXT may contain octets
-                # from character sets other than US-ASCII." and
-                # "Recipients of header field TEXT containing octets
-                # outside the US-ASCII character set may assume that
-                # they represent ISO-8859-1 characters."
-                try:
-                    v = v.encode("iso-8859-1")
-                except UnicodeEncodeError:
-                    if protocol >= (1, 1):
-                        # Encode RFC-2047 TEXT
-                        # (e.g. u"\u8200" -> "=?utf-8?b?6IiA?=").
-                        v = Header(v, 'utf-8').encode()
-                    else:
-                        raise
+                v = self.encode(v)
             else:
                 # This coercion should not take any time at all
                 # if value is already of type "str".
             header_list.append((key, v))
         return header_list
 
+    def encode(self, v):
+        # HTTP/1.0 says, "Words of *TEXT may contain octets
+        # from character sets other than US-ASCII." and
+        # "Recipients of header field TEXT containing octets
+        # outside the US-ASCII character set may assume that
+        # they represent ISO-8859-1 characters."
+        try:
+            v = v.encode("iso-8859-1")
+        except UnicodeEncodeError:
+            if protocol >= (1, 1):
+                # Encode RFC-2047 TEXT
+                # (e.g. u"\u8200" -> "=?utf-8?b?6IiA?=").
+                v = Header(v, 'utf-8').encode()
+            else:
+                raise
+        return v
+
 
 class MaxSizeExceeded(Exception):
     pass
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.