From e3ef80c984742126be1cd91590bc6841c20801cf Mon Sep 17 00:00:00 2001 From: Chris Dent Date: Wed, 29 Jun 2016 13:53:45 -0400 Subject: [PATCH] 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 --- test/test_wsgi_compliance.py | 5 ++++- wsgi_intercept/__init__.py | 6 +++++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/test/test_wsgi_compliance.py b/test/test_wsgi_compliance.py index faf7dab..654b23c 100644 --- a/test/test_wsgi_compliance.py +++ b/test/test_wsgi_compliance.py @@ -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: diff --git a/wsgi_intercept/__init__.py b/wsgi_intercept/__init__.py index 6e20744..3ad0a86 100644 --- a/wsgi_intercept/__init__.py +++ b/wsgi_intercept/__init__.py @@ -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