Permalink
Browse files

Make sure headers are always correctly encoded

WebSocket headers were not correctly encoding as bytestrings.
  • Loading branch information...
tykling authored and andrewgodwin committed Oct 12, 2017
1 parent d24fd06 commit f9233d4b47eecb55ffe5941914fea58bdeaf2f1d
Showing with 10 additions and 5 deletions.
  1. +10 −5 daphne/utils.py
@@ -5,7 +5,10 @@ def header_value(headers, header_name):
value = headers[header_name]
if isinstance(value, list):
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,
@@ -28,12 +31,14 @@ def parse_x_forwarded_for(headers,
if not address_header_name:
return (original_addr, original_scheme)

# Convert twisted-style headers into dicts
if isinstance(headers, Headers):
# Convert twisted-style headers into a dict
headers = dict(headers.getAllRawHeaders())

# Lowercase all header names in the dict
headers = {name.lower(): values for name, values in headers.items()}
# Lowercase all header keys
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")
result_addr = original_addr

0 comments on commit f9233d4

Please sign in to comment.