Make sure request.method is encoded to bytestring to avoid error in httplib

Review Request #6964 — Created Feb. 19, 2015 and submitted — Latest diff uploaded




Make sure request.method is encoded to bytestring to avoid error in httplib

Non-ASCII letters like æøåÆØÅ seemed to give problems when part of summary or description. They cause a UnicodeDecodeError to be raised in

draft = draft.update(**update_fields)
  File "/mnt/vsl175-a/halvorlu/envs/reviewboard/lib/python2.7/site-packages/", line 137, in <lambda>
    meth(resource, **kwargs)))
  File "/mnt/vsl175-a/halvorlu/envs/reviewboard/lib/python2.7/site-packages/", line 27, in request_method
    *args, **kwargs)
  File "/mnt/vsl175-a/halvorlu/envs/reviewboard/lib/python2.7/site-packages/", line 62, in execute_request_method
    return self._execute_request(request)
  File "/mnt/vsl175-a/halvorlu/envs/reviewboard/lib/python2.7/site-packages/", line 71, in _execute_request
    rsp = self.server.make_request(request)
  File "/mnt/vsl175-a/halvorlu/envs/reviewboard/lib/python2.7/site-packages/", line 525, in make_request
    rsp = self._urlopen(r)
  File "/mnt/vsl175-a/halvorlu/envs/reviewboard/lib/python2.7/site-packages/", line 200, in make_request
    return self.urlopen(request)
  File "/usr/lib64/python2.7/", line 127, in urlopen
    return, data, timeout)
  File "/usr/lib64/python2.7/", line 404, in open
    response = self._open(req, data)
  File "/usr/lib64/python2.7/", line 422, in _open
    '_open', req)
  File "/usr/lib64/python2.7/", line 382, in _call_chain
    result = func(*args)
  File "/usr/lib64/python2.7/", line 1214, in http_open
    return self.do_open(httplib.HTTPConnection, req)
  File "/usr/lib64/python2.7/", line 1181, in do_open
    h.request(req.get_method(), req.get_selector(),, headers)
  File "/usr/lib64/python2.7/", line 995, in request
    self._send_request(method, url, body, headers)
  File "/usr/lib64/python2.7/", line 1029, in _send_request
  File "/usr/lib64/python2.7/", line 991, in endheaders
  File "/usr/lib64/python2.7/", line 842, in _send_output
    msg += message_body
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 94: ordinal not in range(128)

The error seems to arise from the fact that request.method may be a Unicode string, whereas all the other parts of the HTTP message are bytestrings, hence they cannot be concatenated.

The behavior can be tested by setting up a ReviewBoard server with a testrepo (path /home/user/repo) and running the following bash script:

hg clone /home/user/repo clientrepo
cd clientrepo
echo "æøå" > tmp.txt
hg add tmp.txt
hg commit -m "ÆØÅæøå" -u "User æøå"
rbt post --server $SERVER --repository testrepo --username testuser --password password --debug

With the requested change the behavior is as expected: The review request is uploaded without any errors.