Commits

Jason R. Coombs committed 6ac9f27

Extract 'encode_header_items' classmethod from HeaderMap.output. Now another function can prepare a header list the same way _cprequest does in .finalize.
Updated customheaders documentation to use the new .encode_header_items to ensure the example doesn't fail on Python 3 with a TypeError. Fixes #1177.

Comments (0)

Files changed (2)

cherrypy/lib/httputil.py

 
     def output(self):
         """Transform self into a list of (name, value) tuples."""
-        header_list = []
-        for k, v in self.items():
+        return list(self.encode_header_items(self.items()))
+
+    def encode_header_items(cls, header_items):
+        """
+        Prepare the sequence of name, value tuples into a form suitable for
+        transmitting on the wire for HTTP.
+        """
+        for k, v in header_items:
             if isinstance(k, unicodestr):
-                k = self.encode(k)
+                k = cls.encode(k)
 
             if not isinstance(v, basestring):
                 v = str(v)
 
             if isinstance(v, unicodestr):
-                v = self.encode(v)
+                v = cls.encode(v)
 
             # See header_translate_* constants above.
             # Replace only if you really know what you're doing.
             k = k.translate(header_translate_table, header_translate_deletechars)
             v = v.translate(header_translate_table, header_translate_deletechars)
 
-            header_list.append((k, v))
-        return header_list
+            yield (k, v)
+    encode_header_items = classmethod(encode_header_items)
 
-    def encode(self, v):
+    def encode(cls, v):
         """Return the given header name or value, encoded for HTTP output."""
-        for enc in self.encodings:
+        for enc in cls.encodings:
             try:
                 return v.encode(enc)
             except UnicodeEncodeError:
                 continue
 
-        if self.protocol == (1, 1) and self.use_rfc_2047:
+        if cls.protocol == (1, 1) and cls.use_rfc_2047:
             # Encode RFC-2047 TEXT
             # (e.g. u"\u8200" -> "=?utf-8?b?6IiA?=").
             # We do our own here instead of using the email module
 
         raise ValueError("Could not encode header part %r using "
                          "any of the encodings %r." %
-                         (v, self.encodings))
-
+                         (v, cls.encodings))
+    encode = classmethod(encode)
 
 class Host(object):
     """An internet address.

sphinx/source/progguide/customheaders.rst

     import cherrypy
 
     def multi_headers():
-        cherrypy.response.header_list.extend(cherrypy.response.multiheaders)
+        cherrypy.response.header_list.extend(
+            cherrypy.response.headers.encode_header_items(
+                cherrypy.response.multiheaders))
 
     cherrypy.tools.multiheaders = cherrypy.Tool('on_end_resource', multi_headers)
 
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.