Make sure headers are always correctly encoded

WebSocket headers were not correctly encoding as bytestrings.
This commit is contained in:
Thomas Steen Rasmussen 2017-10-12 20:06:18 +02:00 committed by Andrew Godwin
parent d24fd06460
commit f9233d4b47

View File

@ -5,7 +5,10 @@ def header_value(headers, header_name):
value = headers[header_name] value = headers[header_name]
if isinstance(value, list): if isinstance(value, list):
value = value[0] value = value[0]
return value.decode("utf-8") # decode to urf-8 if value is bytes
if isinstance(value, bytes):
value = value.decode("utf-8")
return value
def parse_x_forwarded_for(headers, def parse_x_forwarded_for(headers,
@ -28,12 +31,14 @@ def parse_x_forwarded_for(headers,
if not address_header_name: if not address_header_name:
return (original_addr, original_scheme) return (original_addr, original_scheme)
# Convert twisted-style headers into dicts
if isinstance(headers, Headers): if isinstance(headers, Headers):
# Convert twisted-style headers into a dict
headers = dict(headers.getAllRawHeaders()) headers = dict(headers.getAllRawHeaders())
# Lowercase all header keys
# Lowercase all header names in the dict headers = {name.lower(): values for name, values in headers.items()}
headers = {name.lower(): values for name, values in headers.items()} else:
# Lowercase (and encode to utf-8 where needed) non-twisted header keys
headers = {name.lower() if isinstance(name, bytes) else name.lower().encode("utf-8"): values for name, values in headers.items()}
address_header_name = address_header_name.lower().encode("utf-8") address_header_name = address_header_name.lower().encode("utf-8")
result_addr = original_addr result_addr = original_addr