Further fix for #78 and the shared util function

This commit is contained in:
Andrew Godwin 2017-02-10 10:01:55 -08:00
parent 630609fce7
commit 7d1123d39a

View File

@ -1,3 +1,4 @@
from twisted.web.http_headers import Headers
def parse_x_forwarded_for(headers, def parse_x_forwarded_for(headers,
@ -16,10 +17,17 @@ def parse_x_forwarded_for(headers,
if not address_header_name: if not address_header_name:
return original return original
# Convert twisted-style headers into dicts
if isinstance(headers, Headers):
headers = dict(headers.getAllRawHeaders())
# Lowercase all header names in the dict
headers = {name.lower(): 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 = original result = original
if headers.hasHeader(address_header_name): if address_header_name in headers:
address_value = headers.getRawHeaders(address_header_name)[0].decode("utf-8") address_value = headers[address_header_name].decode("utf-8")
if ',' in address_value: if ',' in address_value:
address_value = address_value.split(",")[-1].strip() address_value = address_value.split(",")[-1].strip()