From: Takeaki Matsumoto Date: Tue, 25 Aug 2015 07:47:01 +0000 (+0900) Subject: Fix url in API response to get original X-Git-Url: https://review.fuel-infra.org/gitweb?a=commitdiff_plain;h=d348d557581966160f8843f6a18eda8845ae432d;p=openstack-build%2Fcinder-build.git Fix url in API response to get original This changes the url in the API response to get the original value of redirection if the header 'X-Forwarded-Host' exists. Change-Id: I10877522db5df66d14c01487de6297f1336cecaf Closes-Bug: 1488373 --- diff --git a/cinder/api/common.py b/cinder/api/common.py index 9dc7ed112..793da39aa 100644 --- a/cinder/api/common.py +++ b/cinder/api/common.py @@ -220,6 +220,17 @@ def get_sort_params(params, default_key='created_at', default_dir='desc'): return sort_keys, sort_dirs +def get_request_url(request): + url = request.application_url + headers = request.headers + forwarded = headers.get('X-Forwarded-Host') + if forwarded: + url_parts = list(urllib.parse.urlsplit(url)) + url_parts[1] = re.split(',\s?', forwarded)[-1] + url = urllib.parse.urlunsplit(url_parts).rstrip('/') + return url + + def remove_version_from_href(href): """Removes the first api version from the href. @@ -265,7 +276,7 @@ class ViewBuilder(object): """Return href string with proper limit and marker params.""" params = request.params.copy() params["marker"] = identifier - prefix = self._update_link_prefix(request.application_url, + prefix = self._update_link_prefix(get_request_url(request), CONF.osapi_volume_base_URL) url = os.path.join(prefix, request.environ["cinder.context"].project_id, @@ -274,7 +285,7 @@ class ViewBuilder(object): def _get_href_link(self, request, identifier): """Return an href string pointing to this object.""" - prefix = self._update_link_prefix(request.application_url, + prefix = self._update_link_prefix(get_request_url(request), CONF.osapi_volume_base_URL) return os.path.join(prefix, request.environ["cinder.context"].project_id, @@ -283,7 +294,7 @@ class ViewBuilder(object): def _get_bookmark_link(self, request, identifier): """Create a URL that refers to a specific resource.""" - base_url = remove_version_from_href(request.application_url) + base_url = remove_version_from_href(get_request_url(request)) base_url = self._update_link_prefix(base_url, CONF.osapi_volume_base_URL) return os.path.join(base_url, diff --git a/cinder/tests/unit/api/test_common.py b/cinder/tests/unit/api/test_common.py index ca7cbf1a7..f00bf84aa 100644 --- a/cinder/tests/unit/api/test_common.py +++ b/cinder/tests/unit/api/test_common.py @@ -550,3 +550,23 @@ class LinkPrefixTest(test.TestCase): "http://new.prefix.com:20455/new_extra_prefix") self.assertEqual("http://new.prefix.com:20455/new_extra_prefix/v1", result) + + +class RequestUrlTest(test.TestCase): + def test_get_request_url_no_forward(self): + app_url = 'http://127.0.0.1/v2;param?key=value#frag' + request = type('', (), { + 'application_url': app_url, + 'headers': {} + }) + result = common.get_request_url(request) + self.assertEqual(app_url, result) + + def test_get_request_url_forward(self): + request = type('', (), { + 'application_url': 'http://127.0.0.1/v2;param?key=value#frag', + 'headers': {'X-Forwarded-Host': '192.168.0.243:24'} + }) + result = common.get_request_url(request) + self.assertEqual('http://192.168.0.243:24/v2;param?key=value#frag', + result)