From 7d1123d39add52572a59a0f02de2f9add75e37d9 Mon Sep 17 00:00:00 2001 From: Andrew Godwin Date: Fri, 10 Feb 2017 10:01:55 -0800 Subject: [PATCH] Further fix for #78 and the shared util function --- daphne/utils.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/daphne/utils.py b/daphne/utils.py index 98e6d71..e37703b 100644 --- a/daphne/utils.py +++ b/daphne/utils.py @@ -1,3 +1,4 @@ +from twisted.web.http_headers import Headers def parse_x_forwarded_for(headers, @@ -16,10 +17,17 @@ def parse_x_forwarded_for(headers, if not address_header_name: 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") result = original - if headers.hasHeader(address_header_name): - address_value = headers.getRawHeaders(address_header_name)[0].decode("utf-8") + if address_header_name in headers: + address_value = headers[address_header_name].decode("utf-8") if ',' in address_value: address_value = address_value.split(",")[-1].strip()