[New-bugs-announce] [issue11898] Sending binary data with a POST request in httplib can cause Unicode exceptions

Bernhard Rosenkraenzer report at bugs.python.org
Thu Apr 21 15:42:34 CEST 2011


New submission from Bernhard Rosenkraenzer <bero at arklinux.org>:

Sending e.g. a JPEG file with a httplib POST request (e.g. through mechanize) can result in an error like this:

  File "/usr/lib64/python2.7/httplib.py", line 947, in request
    self._send_request(method, url, body, headers)
  File "/usr/lib64/python2.7/httplib.py", line 988, in _send_request
    self.endheaders(body)
  File "/usr/lib64/python2.7/httplib.py", line 941, in endheaders
    self._send_output(message_body)
  File "/usr/lib64/python2.7/httplib.py", line 802, in _send_output
    msg += message_body
UnicodeDecodeError: 'ascii' codec can't decode byte 0xff in position 2566: invalid start byte


The code triggering this is the attempt to merge the msg and message_body into a single request in httplib.py lines 791+

The patch I'm attaching treats an invalid string of unknown encoding (e.g. binary data wrapped as string) like something that isn't a string.

Works for me with the patch.

----------
components: Library (Lib)
files: python-2.7.1-fix-httplib-UnicodeDecodeError.patch
keywords: patch
messages: 134211
nosy: bero
priority: normal
severity: normal
status: open
title: Sending binary data with a POST request in httplib can cause Unicode exceptions
type: behavior
versions: Python 2.7
Added file: http://bugs.python.org/file21747/python-2.7.1-fix-httplib-UnicodeDecodeError.patch

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue11898>
_______________________________________


More information about the New-bugs-announce mailing list