Skip to content

Commit

Permalink
Don't throw away port number when parsing the Forwarded header aio-li…
Browse files Browse the repository at this point in the history
  • Loading branch information
Gustavo Carneiro committed May 18, 2018
1 parent 1d3afb2 commit e382c67
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
6 changes: 4 additions & 2 deletions aiohttp/web_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class FileField:
qdtext=_QDTEXT, quoted_pair=_QUOTED_PAIR)

_FORWARDED_PAIR = (
r'({token})=({token}|{quoted_string})'.format(
r'({token})=({token}|{quoted_string})(:\d{{1,4}})?'.format(
token=_TOKEN,
quoted_string=_QUOTED_STRING))

Expand Down Expand Up @@ -247,11 +247,13 @@ def forwarded(self):
# bad syntax here, skip to next comma
pos = field_value.find(',', pos)
else:
(name, value) = match.groups()
(name, value, port) = match.groups()
if value[0] == '"':
# quoted string: remove quotes and unescape
value = _QUOTED_PAIR_REPLACE_RE.sub(r'\1',
value[1:-1])
if port:
value += port
elem[name.lower()] = value
pos += len(match.group(0))
need_separator = True
Expand Down
14 changes: 14 additions & 0 deletions tests/test_web_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,20 @@ def test_single_forwarded_header():
assert req.forwarded[0]['proto'] == 'identifier'


def test_forwarded_host_with_port():
header = 'for=1.2.3.4:1234'
req = make_mocked_request('GET', '/',
headers=CIMultiDict({'Forwarded': header}))
assert req.forwarded == ({'for': "1.2.3.4:1234"},)


def test_forwarded_quoted_host_with_port():
header = 'for="[2001:db8:cafe::17]:1234"'
req = make_mocked_request('GET', '/',
headers=CIMultiDict({'Forwarded': header}))
assert req.forwarded == ({'for': "[2001:db8:cafe::17]:1234"},)


def test_single_forwarded_header_camelcase():
header = 'bY=identifier;fOr=identifier;HOst=identifier;pRoTO=identifier'
req = make_mocked_request('GET', '/',
Expand Down

0 comments on commit e382c67

Please sign in to comment.