From ff2bdc55b912cc1c1d42cd1b48953e04142b5663 Mon Sep 17 00:00:00 2001 From: Thomas NOEL Date: Tue, 3 May 2016 16:59:27 +0200 Subject: [PATCH] misc: support variadic URL with trailing / (#10813) --- tests/test_variadic_url.py | 12 ++++++++++++ wcs/qommon/misc.py | 7 ++++--- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/tests/test_variadic_url.py b/tests/test_variadic_url.py index 0c395d3..70d7f09 100644 --- a/tests/test_variadic_url.py +++ b/tests/test_variadic_url.py @@ -9,6 +9,12 @@ def test_url_scheme(): {'https': 's'}) == 'https://www.example.net/foobar' assert get_variadic_url('http[https]://www.example.net/foobar', {'https': ''}) == 'http://www.example.net/foobar' + assert get_variadic_url('http[https]://www.example.net/foobar/', + {'https': ''}) == 'http://www.example.net/foobar/' + assert get_variadic_url('http[https]://www.example.net/foo/bar/', + {'https': ''}) == 'http://www.example.net/foo/bar/' + assert get_variadic_url('http[https]://www.example.net/foo/bar', + {'https': ''}) == 'http://www.example.net/foo/bar' def test_url_netloc(): assert get_variadic_url('http://[hostname]/foobar', @@ -67,6 +73,12 @@ def test_url_server(): {'url': 'http://www.example.net/'}) == 'http://www.example.net/foobar' assert get_variadic_url('[url]foobar', {'url': 'http://www.example.net/'}) == 'http://www.example.net/foobar' + assert get_variadic_url('[url]foo/bar/', + {'url': 'http://www.example.net/'}) == 'http://www.example.net/foo/bar/' + assert get_variadic_url('[url]foobar/', + {'url': 'http://www.example.net/'}) == 'http://www.example.net/foobar/' + assert get_variadic_url('[url]foo/bar', + {'url': 'http://www.example.net/'}) == 'http://www.example.net/foo/bar' def test_url_server_qs(): assert get_variadic_url('[url]?foo=bar', diff --git a/wcs/qommon/misc.py b/wcs/qommon/misc.py index 28de418..4c02ecf 100644 --- a/wcs/qommon/misc.py +++ b/wcs/qommon/misc.py @@ -316,15 +316,16 @@ def get_variadic_url(url, variables, encode_query=True): # (ex: http[https]://www.example.net) or because the value starts # with a variable name (ex: [url]); in that situation we do not # quote at all. - if path.count('/') >= 2: + if path.count('//') == 1: # there were no / in the original path (the two / comes from - # the scheme/netloc separation, this means there is no path + # the scheme/netloc separation, this means there is no path) before_path = ezt_substitute(path, variables) p2 = urlparse.urlsplit(before_path) scheme, netloc, path = p2.scheme, p2.netloc, p2.path else: # there is a path, we need to get back to the original URL and # split it on the last /, to isolate the path part. + lastslash = '/' if path.endswith('/') else '' if '/' in path: before_path, path = path.rsplit('/', 1) else: @@ -334,7 +335,7 @@ def get_variadic_url(url, variables, encode_query=True): scheme, netloc = p2.scheme, p2.netloc if p2.path: if not path: - path, query2 = p2.path, p2.query + path, query2 = p2.path + lastslash, p2.query else: path, query2 = p2.path + '/' + path, p2.query if query and query2: -- 2.8.1