mirror of
https://github.com/Grasscutters/mitmproxy.git
synced 2024-11-23 00:01:36 +00:00
Merge pull request #1563 from mhils/fix-1562
Raise TypeError on invalid header assignment, fix #1562
This commit is contained in:
commit
d5427c7298
@ -14,6 +14,7 @@ if six.PY2: # pragma: no cover
|
||||
return x
|
||||
|
||||
def _always_bytes(x):
|
||||
strutils.always_bytes(x, "utf-8", "replace") # raises a TypeError if x != str/bytes/None.
|
||||
return x
|
||||
else:
|
||||
# While headers _should_ be ASCII, it's not uncommon for certain headers to be utf-8 encoded.
|
||||
|
@ -8,7 +8,10 @@ import six
|
||||
def always_bytes(unicode_or_bytes, *encode_args):
|
||||
if isinstance(unicode_or_bytes, six.text_type):
|
||||
return unicode_or_bytes.encode(*encode_args)
|
||||
return unicode_or_bytes
|
||||
elif isinstance(unicode_or_bytes, bytes) or unicode_or_bytes is None:
|
||||
return unicode_or_bytes
|
||||
else:
|
||||
raise TypeError("Expected str or bytes, but got {}.".format(type(unicode_or_bytes).__name__))
|
||||
|
||||
|
||||
def native(s, *encoding_opts):
|
||||
|
@ -189,7 +189,7 @@ class Response(_HTTP2Message):
|
||||
|
||||
resp = http.Response(
|
||||
b'HTTP/2.0',
|
||||
self.status_code.string(),
|
||||
int(self.status_code.string()),
|
||||
b'',
|
||||
headers,
|
||||
body,
|
||||
|
@ -6,7 +6,7 @@ import time
|
||||
import hyperframe.frame
|
||||
from hpack.hpack import Encoder, Decoder
|
||||
|
||||
from netlib import utils, strutils
|
||||
from netlib import utils
|
||||
from netlib.http import http2
|
||||
import netlib.http.headers
|
||||
import netlib.http.response
|
||||
@ -201,7 +201,7 @@ class HTTP2StateProtocol(object):
|
||||
headers = response.headers.copy()
|
||||
|
||||
if ':status' not in headers:
|
||||
headers.insert(0, b':status', strutils.always_bytes(response.status_code))
|
||||
headers.insert(0, b':status', str(response.status_code).encode())
|
||||
|
||||
if hasattr(response, 'stream_id'):
|
||||
stream_id = response.stream_id
|
||||
|
@ -43,6 +43,15 @@ class TestHeaders(object):
|
||||
with raises(TypeError):
|
||||
Headers([[b"Host", u"not-bytes"]])
|
||||
|
||||
def test_set(self):
|
||||
headers = Headers()
|
||||
headers[u"foo"] = u"1"
|
||||
headers[b"bar"] = b"2"
|
||||
headers["baz"] = b"3"
|
||||
with raises(TypeError):
|
||||
headers["foobar"] = 42
|
||||
assert len(headers) == 3
|
||||
|
||||
def test_bytes(self):
|
||||
headers = Headers(Host="example.com")
|
||||
assert bytes(headers) == b"Host: example.com\r\n"
|
||||
|
@ -8,6 +8,8 @@ def test_always_bytes():
|
||||
assert strutils.always_bytes("foo") == b"foo"
|
||||
with tutils.raises(ValueError):
|
||||
strutils.always_bytes(u"\u2605", "ascii")
|
||||
with tutils.raises(TypeError):
|
||||
strutils.always_bytes(42, "ascii")
|
||||
|
||||
|
||||
def test_native():
|
||||
|
Loading…
Reference in New Issue
Block a user