Check X-Forwarded-Proto when returning the url

Closes #31
This commit is contained in:
Kevin McCarthy
2014-06-21 14:16:42 -10:00
parent 5ee70566ec
commit f6d708ebe5
2 changed files with 20 additions and 2 deletions
+13 -1
View File
@@ -13,6 +13,7 @@ from hashlib import md5
from werkzeug.http import parse_authorization_header
from flask import request, make_response
from six.moves.urllib.parse import urlparse, urlunparse
from .structures import CaseInsensitiveDict
@@ -133,6 +134,17 @@ def semiflatten(multi):
else:
return multi
def get_url(request):
"""
Since we might be hosted behind a proxy, we need to check the
X-Forwarded-Proto header to find out what protocol was used to access us.
"""
if 'X-Forwarded-Proto' not in request.headers:
return request.url
url = list(urlparse(request.url))
url[0] = request.headers.get('X-Forwarded-Proto')
return urlunparse(url)
def get_dict(*keys, **extras):
"""Returns request dict of given keys."""
@@ -151,7 +163,7 @@ def get_dict(*keys, **extras):
_json = None
d = dict(
url=request.url,
url=get_url(request),
args=semiflatten(request.args),
form=form,
data=json_safe(data),
+7 -1
View File
@@ -215,7 +215,7 @@ class HttpbinTestCase(unittest.TestCase):
content_type='application/x-www-form-urlencoded'
)
form_data = json.loads(response.data.decode('utf-8'))['form']
self.assertEquals(form_data, {'name': 'kevin'})
self.assertEqual(form_data, {'name': 'kevin'})
def test_methods__to_status_endpoint(self):
methods = [
@@ -237,6 +237,12 @@ class HttpbinTestCase(unittest.TestCase):
response.headers.get('Content-Type'), 'application/xml'
)
def test_x_forwarded_proto(self):
response = self.app.get(path='/get', headers={
'X-Forwarded-Proto':'https'
})
assert json.loads(response.data.decode('utf-8'))['url'].startswith('https://')
if __name__ == '__main__':
unittest.main()