Skip to content

Commit

Permalink
Merge pull request #382 from gratipay/flex-canonize
Browse files Browse the repository at this point in the history
make Canonizer more flexible
  • Loading branch information
pjz committed Sep 5, 2014
2 parents df08f7f + 5f8bbab commit eec2ed8
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions aspen/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,14 @@ def nice(t):
# Hostname canonicalization
# =========================

def Canonizer(expected):
def _extract_scheme(request):
return request.headers.get('X-Forwarded-Proto', 'http') # Heroku

def _extract_host(request):
return request.headers['Host'] # will 400 if missing

def Canonizer(expected, permanent_redirect=False, extract_scheme=_extract_scheme,
extract_host=_extract_host):
"""Takes a netloc such as http://localhost:8080 (no path part).
"""

Expand All @@ -400,8 +407,8 @@ def canonize(request):
"""Enforce a certain network location.
"""

scheme = request.headers.get('X-Forwarded-Proto', 'http') # XXX Heroku
host = request.headers['Host'] # will 400 if missing
scheme = extract_scheme(request)
host = extract_host(request)

actual = scheme + "://" + host

Expand All @@ -415,7 +422,7 @@ def canonize(request):
else:
# For non-idempotent methods, redirect to homepage.
uri += '/'
request.redirect(uri, permanent=True)
request.redirect(uri, permanent=permanent_redirect)

return expected and canonize or noop

Expand Down

0 comments on commit eec2ed8

Please sign in to comment.