Fixed #90: X-Forwarded-For now does v6 address properly

It also now ignores ports, as I can't find a good example of them being put into the XFF header.
This commit is contained in:
Andrew Godwin 2017-02-25 18:18:17 -08:00
parent 360a445f68
commit c55bc8a94b
2 changed files with 7 additions and 15 deletions

View File

@ -31,13 +31,13 @@ class TestXForwardedForHttpParsing(TestCase):
['10.1.2.3', 0] ['10.1.2.3', 0]
) )
def test_port_in_address(self): def test_v6_address(self):
headers = Headers({ headers = Headers({
b'X-Forwarded-For': [b'10.1.2.3:5123'], b'X-Forwarded-For': [b'1043::a321:0001, 10.0.5.6'],
}) })
self.assertEqual( self.assertEqual(
parse_x_forwarded_for(headers), parse_x_forwarded_for(headers),
['10.1.2.3', 5123] ['1043::a321:0001', 0]
) )
def test_multiple_proxys(self): def test_multiple_proxys(self):
@ -85,13 +85,13 @@ class TestXForwardedForWsParsing(TestCase):
['10.1.2.3', 0] ['10.1.2.3', 0]
) )
def test_port_in_address(self): def test_v6_address(self):
headers = { headers = {
b'X-Forwarded-For': b'10.1.2.3:5123', b'X-Forwarded-For': [b'1043::a321:0001, 10.0.5.6'],
} }
self.assertEqual( self.assertEqual(
parse_x_forwarded_for(headers), parse_x_forwarded_for(headers),
['10.1.2.3', 5123] ['1043::a321:0001', 0]
) )
def test_multiple_proxys(self): def test_multiple_proxys(self):

View File

@ -39,15 +39,7 @@ def parse_x_forwarded_for(headers,
if ',' in address_value: if ',' in address_value:
address_value = address_value.split(",")[0].strip() address_value = address_value.split(",")[0].strip()
if ':' in address_value: result = [address_value, 0]
address_host, address_port = address_value.split(':')
result = [address_host, 0]
try:
result[1] = int(address_port)
except ValueError:
pass
else:
result = [address_value, 0]
if port_header_name: if port_header_name:
# We only want to parse the X-Forwarded-Port header if we also parsed the X-Forwarded-For # We only want to parse the X-Forwarded-Port header if we also parsed the X-Forwarded-For