From a2ed17f6531fe862790ddb04f015798f0f1cda21 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 | 25 +++++++++++++++++++------ wcs/qommon/misc.py | 7 ++++--- 2 files changed, 23 insertions(+), 9 deletions(-) diff --git a/tests/test_variadic_url.py b/tests/test_variadic_url.py index 0c395d3..6c52991 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', @@ -61,12 +67,19 @@ def test_url_whole(): {'url': 'http://www.example.net/foobar'}) == 'http://www.example.net/foobar' def test_url_server(): - assert get_variadic_url('[url]/foobar', - {'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]foobar', - {'url': 'http://www.example.net/'}) == 'http://www.example.net/foobar' + for url in ('http://www.example.net', 'http://www.example.net/'): + assert get_variadic_url('[url]/foobar', + {'url': url}) == 'http://www.example.net/foobar' + assert get_variadic_url('[url]/foobar', + {'url': url}) == 'http://www.example.net/foobar' + assert get_variadic_url('[url]/foobar/', + {'url': url}) == '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 dedd3b4..30c282c 100644 --- a/wcs/qommon/misc.py +++ b/wcs/qommon/misc.py @@ -332,15 +332,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: @@ -350,7 +351,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