Commit f10a1b06 authored by Claude Paroz's avatar Claude Paroz
Browse files

[py3] Fixed Python 3 compatibility of http handling

* Using str() when Python 2 expects bytes and Python 3 Unicode
* Fixed reraise-ing syntax
* Fixed slicing of byte strings
parent 22527a82
Loading
Loading
Loading
Loading
+4 −4
Original line number Diff line number Diff line
@@ -61,14 +61,14 @@ else:
        if not _cookie_allows_colon_in_names:
            def load(self, rawdata):
                self.bad_cookies = set()
                super(SimpleCookie, self).load(smart_bytes(rawdata))
                super(SimpleCookie, self).load(str(rawdata))
                for key in self.bad_cookies:
                    del self[key]

            # override private __set() method:
            # (needed for using our Morsel, and for laxness with CookieError
            def _BaseCookie__set(self, key, real_value, coded_value):
                key = smart_bytes(key)
                key = str(key)
                try:
                    M = self.get(key, Morsel())
                    M.set(key, real_value, coded_value)
@@ -137,7 +137,7 @@ def build_request_repr(request, path_override=None, GET_override=None,
    except:
        meta = '<could not parse>'
    path = path_override if path_override is not None else request.path
    return smart_bytes('<%s\npath:%s,\nGET:%s,\nPOST:%s,\nCOOKIES:%s,\nMETA:%s>' %
    return str('<%s\npath:%s,\nGET:%s,\nPOST:%s,\nCOOKIES:%s,\nMETA:%s>' %
                     (request.__class__.__name__,
                      path,
                      six.text_type(get),
@@ -294,7 +294,7 @@ class HttpRequest(object):
            try:
                self._body = self.read()
            except IOError as e:
                six.reraise(UnreadablePostError, e, sys.exc_traceback)
                six.reraise(UnreadablePostError, UnreadablePostError(*tuple(e.args)), sys.exc_info()[2])
            self._stream = BytesIO(self._body)
        return self._body

+5 −3
Original line number Diff line number Diff line
@@ -507,9 +507,11 @@ class BoundaryIter(object):
            end = index
            next = index + len(self._boundary)
            # backup over CRLF
            if data[max(0,end-1)] == b'\n':
            last = max(0, end-1)
            if data[last:last+1] == b'\n':
                end -= 1
            if data[max(0,end-1)] == b'\r':
            last = max(0, end-1)
            if data[last:last+1] == b'\r':
                end -= 1
            return end, next

@@ -613,7 +615,7 @@ def parse_header(line):
        if i >= 0:
            name = p[:i].strip().lower().decode('ascii')
            value = p[i+1:].strip()
            if len(value) >= 2 and value[0] == value[-1] == b'"':
            if len(value) >= 2 and value[:1] == value[-1:] == b'"':
                value = value[1:-1]
                value = value.replace(b'\\\\', b'\\').replace(b'\\"', b'"')
            pdict[name] = value