request.response.setHeader() should convert unicode to str

Bug #284756 reported by Marius Gedminas
4
Affects Status Importance Assigned to Milestone
Zope 3
Won't Fix
Undecided
Unassigned
zope.publisher
Invalid
Undecided
Unassigned

Bug Description

I had a view that did essentially:

  class MyView(BrowserView):
      def __call__(self):
          filename = u'someunicodestring' # in real life this was self.context.__name__, which was a unicode string with ascii-only characters
          self.request.response.setHeader('Content-Disposition', 'attachment; filename="%s"' % filename)
          return '<someXml/>'

and I got a UnicodeDecodeError: 'ascii' codec can't decode byte 0x8b in position 1: ordinal not in range(128) in /zope/server/buffers.py", line 185, in append:
    self.strbuf = strbuf + s

Turns out zope.server was trying to concatenate a unicode string containing HTTP response headers with a byte string containing my XML, compressed with gzip.

I assume this problem would also occur if I tried to serve binary data (say, a PNG file with the proper content-type) and had a unicode string in one of the response headers.

HTTPRequest.setHeader() should convert unicode to str and give me a failure early (or silently succeed if my unicode string contains only ASCII).

Tres Seaver (tseaver)
Changed in zope3:
status: New → Won't Fix
Revision history for this message
Colin Watson (cjwatson) wrote :

The zope.publisher project on Launchpad has been archived at the request of the Zope developers (see https://answers.launchpad.net/launchpad/+question/683589 and https://answers.launchpad.net/launchpad/+question/685285). If this bug is still relevant, please refile it at https://github.com/zopefoundation/zope.publisher.

Changed in zope.publisher:
status: New → Invalid
To post a comment you must log in.
This report contains Public information  Edit
Everyone can see this information.

Other bug subscribers