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

Information

RBTools
master
4d8867f...

Reviewers

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 httplib.py:

draft = draft.update(**update_fields)
  File "/mnt/vsl175-a/halvorlu/envs/reviewboard/lib/python2.7/site-packages/RBTools-0.7.2alpha0.dev-py2.7.egg/rbtools/api/resource.py", line 137, in <lambda>
    meth(resource, **kwargs)))
  File "/mnt/vsl175-a/halvorlu/envs/reviewboard/lib/python2.7/site-packages/RBTools-0.7.2alpha0.dev-py2.7.egg/rbtools/api/decorators.py", line 27, in request_method
    *args, **kwargs)
  File "/mnt/vsl175-a/halvorlu/envs/reviewboard/lib/python2.7/site-packages/RBTools-0.7.2alpha0.dev-py2.7.egg/rbtools/api/transport/sync.py", line 62, in execute_request_method
    return self._execute_request(request)
  File "/mnt/vsl175-a/halvorlu/envs/reviewboard/lib/python2.7/site-packages/RBTools-0.7.2alpha0.dev-py2.7.egg/rbtools/api/transport/sync.py", line 71, in _execute_request
    rsp = self.server.make_request(request)
  File "/mnt/vsl175-a/halvorlu/envs/reviewboard/lib/python2.7/site-packages/RBTools-0.7.2alpha0.dev-py2.7.egg/rbtools/api/request.py", line 525, in make_request
    rsp = self._urlopen(r)
  File "/mnt/vsl175-a/halvorlu/envs/reviewboard/lib/python2.7/site-packages/RBTools-0.7.2alpha0.dev-py2.7.egg/rbtools/api/cache.py", line 200, in make_request
    return self.urlopen(request)
  File "/usr/lib64/python2.7/urllib2.py", line 127, in urlopen
    return _opener.open(url, data, timeout)
  File "/usr/lib64/python2.7/urllib2.py", line 404, in open
    response = self._open(req, data)
  File "/usr/lib64/python2.7/urllib2.py", line 422, in _open
    '_open', req)
  File "/usr/lib64/python2.7/urllib2.py", line 382, in _call_chain
    result = func(*args)
  File "/usr/lib64/python2.7/urllib2.py", line 1214, in http_open
    return self.do_open(httplib.HTTPConnection, req)
  File "/usr/lib64/python2.7/urllib2.py", line 1181, in do_open
    h.request(req.get_method(), req.get_selector(), req.data, headers)
  File "/usr/lib64/python2.7/httplib.py", line 995, in request
    self._send_request(method, url, body, headers)
  File "/usr/lib64/python2.7/httplib.py", line 1029, in _send_request
    self.endheaders(body)
  File "/usr/lib64/python2.7/httplib.py", line 991, in endheaders
    self._send_output(message_body)
  File "/usr/lib64/python2.7/httplib.py", 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:

SERVER=http://localhost:8080/
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.