Skip to content

Commit

Permalink
Correct case-folding of permanent URLs
Browse files Browse the repository at this point in the history
Fixes #87
  • Loading branch information
fluffy-critter committed Aug 22, 2020
1 parent b327a99 commit 4349412
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
10 changes: 8 additions & 2 deletions authl/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import logging
import os.path
import typing
import urllib.parse

import requests

Expand Down Expand Up @@ -46,14 +47,19 @@ def permanent_url(response: requests.Response) -> str:
""" Given a requests.Response object, determine what the permanent URL
for it is from the response history """

def normalize(url):
# normalize the netloc to lowercase
parsed = urllib.parse.urlparse(url)
return urllib.parse.urlunparse(parsed._replace(netloc=parsed.netloc.lower()))

for item in response.history:
if item.status_code in (301, 308):
# permanent redirect means we continue on to the next URL in the
# redirection change
continue
# Any other status code is assumed to be a temporary redirect, so this
# is the last permanent URL
return item.url
return normalize(item.url)

# Last history item was a permanent redirect, or there was no history
return response.url
return normalize(response.url)
4 changes: 4 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ def test_permanent_url(requests_mock):
req = requests.get('https://make-secure.example/final')
assert utils.permanent_url(req) == 'https://make-secure.example/final'

# correct case folding
req = requests.get('Https://Make-SecuRE.Example/final')
assert utils.permanent_url(req) == 'https://make-secure.example/final'

# ensure 308 redirect works too
requests_mock.get('http://perm-308.example', status_code=308,
headers={'Location': 'https://make-secure.example/308'})
Expand Down

0 comments on commit 4349412

Please sign in to comment.