Ensure header values are native str

PEP 3333 requires that values in the environ that come from
header be native str objects decoded from ISO-8859-1 bytes.
This means that in Python 2 and 3 they are effectively
different things, but oh well, these are the rules.

One area where this causes a real issue is in the
SimpleCookie.load() method which checks for the type of the
string associated with HTTP_COOKIE.

Fixes #39
This commit is contained in:
Chris Dent
2016-06-29 13:53:45 -04:00
parent d34e64f764
commit e3ef80c984
2 changed files with 9 additions and 2 deletions

View File

@@ -50,7 +50,8 @@ def test_more_interesting():
resp, content = http.request(
'http://some_hopefully_nonexistant_domain' + expected_uri,
'GET',
headers={'Accept': 'application/json'})
headers={'Accept': 'application/json',
'Cookie': 'foo=bar'})
internal_env = app.get_internals()
expected_path_info = unquote(expected_uri.split('?')[0])
@@ -58,6 +59,8 @@ def test_more_interesting():
assert internal_env['RAW_URI'] == expected_uri
assert internal_env['QUERY_STRING'] == 'bar=baz%20zoom'
assert internal_env['HTTP_ACCEPT'] == 'application/json'
assert internal_env['HTTP_COOKIE'] == 'foo=bar'
assert type(internal_env['HTTP_COOKIE']) == type('')
# Do the rather painful wsgi encoding dance.
if sys.version_info[0] > 2:

View File

@@ -220,7 +220,11 @@ def make_environ(inp, host, port, script_name):
k, v = line.strip().split(b':', 1)
v = v.lstrip()
v = v.decode('ISO-8859-1')
# Make header value a "native" string. PEP 3333 requires that
# string-like things in headers be of type `str`. Much of the
# time this isn't a problem but the SimpleCookie library does
# type checking against `type("")`.
v = str(v.decode('ISO-8859-1'))
#
# take care of special headers, and for the rest, put them