diff options
author | Maximilian Hils <git@maximilianhils.com> | 2015-09-21 18:34:43 +0200 |
---|---|---|
committer | Maximilian Hils <git@maximilianhils.com> | 2015-09-21 18:34:43 +0200 |
commit | 1ff8f294b459e03e113acb417678a6fd782c2685 (patch) | |
tree | 120fdcb30c706629c26dd46fbee08d9b8e8328e0 /netlib | |
parent | 9dea36e43913a642c7f379a55828e1fb7745ba6b (diff) | |
download | mitmproxy-1ff8f294b459e03e113acb417678a6fd782c2685.tar.gz mitmproxy-1ff8f294b459e03e113acb417678a6fd782c2685.tar.bz2 mitmproxy-1ff8f294b459e03e113acb417678a6fd782c2685.zip |
minor encoding fixes
Diffstat (limited to 'netlib')
-rw-r--r-- | netlib/utils.py | 6 | ||||
-rw-r--r-- | netlib/wsgi.py | 18 |
2 files changed, 12 insertions, 12 deletions
diff --git a/netlib/utils.py b/netlib/utils.py index 8d11bd5b..b9848038 100644 --- a/netlib/utils.py +++ b/netlib/utils.py @@ -26,7 +26,7 @@ def always_byte_args(*encode_args): return decorator -def native(s, encoding="latin-1"): +def native(s, *encoding_opts): """ Convert :py:class:`bytes` or :py:class:`unicode` to the native :py:class:`str` type, using latin1 encoding if conversion is necessary. @@ -37,10 +37,10 @@ def native(s, encoding="latin-1"): raise TypeError("%r is neither bytes nor unicode" % s) if six.PY3: if isinstance(s, six.binary_type): - return s.decode(encoding) + return s.decode(*encoding_opts) else: if isinstance(s, six.text_type): - return s.encode(encoding) + return s.encode(*encoding_opts) return s diff --git a/netlib/wsgi.py b/netlib/wsgi.py index 8fb09008..4fcd5178 100644 --- a/netlib/wsgi.py +++ b/netlib/wsgi.py @@ -55,38 +55,38 @@ class WSGIAdaptor(object): self.app, self.domain, self.port, self.sversion = app, domain, port, sversion def make_environ(self, flow, errsoc, **extra): - path = native(flow.request.path) + path = native(flow.request.path, "latin-1") if '?' in path: - path_info, query = native(path).split('?', 1) + path_info, query = native(path, "latin-1").split('?', 1) else: path_info = path query = '' environ = { 'wsgi.version': (1, 0), - 'wsgi.url_scheme': native(flow.request.scheme), + 'wsgi.url_scheme': native(flow.request.scheme, "latin-1"), 'wsgi.input': BytesIO(flow.request.body or b""), 'wsgi.errors': errsoc, 'wsgi.multithread': True, 'wsgi.multiprocess': False, 'wsgi.run_once': False, 'SERVER_SOFTWARE': self.sversion, - 'REQUEST_METHOD': native(flow.request.method), + 'REQUEST_METHOD': native(flow.request.method, "latin-1"), 'SCRIPT_NAME': '', 'PATH_INFO': urllib.parse.unquote(path_info), 'QUERY_STRING': query, - 'CONTENT_TYPE': native(flow.request.headers.get('Content-Type', '')), - 'CONTENT_LENGTH': native(flow.request.headers.get('Content-Length', '')), + 'CONTENT_TYPE': native(flow.request.headers.get('Content-Type', ''), "latin-1"), + 'CONTENT_LENGTH': native(flow.request.headers.get('Content-Length', ''), "latin-1"), 'SERVER_NAME': self.domain, 'SERVER_PORT': str(self.port), - 'SERVER_PROTOCOL': native(flow.request.http_version), + 'SERVER_PROTOCOL': native(flow.request.http_version, "latin-1"), } environ.update(extra) if flow.client_conn.address: - environ["REMOTE_ADDR"] = native(flow.client_conn.address.host) + environ["REMOTE_ADDR"] = native(flow.client_conn.address.host, "latin-1") environ["REMOTE_PORT"] = flow.client_conn.address.port for key, value in flow.request.headers.items(): - key = 'HTTP_' + native(key).upper().replace('-', '_') + key = 'HTTP_' + native(key, "latin-1").upper().replace('-', '_') if key not in ('HTTP_CONTENT_TYPE', 'HTTP_CONTENT_LENGTH'): environ[key] = value return environ |