This is an automated email from the ASF dual-hosted git repository. michaelo pushed a commit to branch WAGON-569 in repository https://gitbox.apache.org/repos/asf/maven-wagon.git
commit 8a212190b6a7c031d11cc18f17e8774888b94b3d Author: Michael Osipov <[email protected]> AuthorDate: Wed Oct 16 22:42:06 2019 +0200 [WAGON-569] Inconsistent encoding behavior for repository URLs with spaces --- .../maven/wagon/shared/http/EncodingUtil.java | 28 +++++----- .../maven/wagon/shared/http/EncodingUtilTest.java | 59 +++++++++++++++++++++- 2 files changed, 74 insertions(+), 13 deletions(-) diff --git a/wagon-providers/wagon-http-shared/src/main/java/org/apache/maven/wagon/shared/http/EncodingUtil.java b/wagon-providers/wagon-http-shared/src/main/java/org/apache/maven/wagon/shared/http/EncodingUtil.java index 1794288..6885a86 100644 --- a/wagon-providers/wagon-http-shared/src/main/java/org/apache/maven/wagon/shared/http/EncodingUtil.java +++ b/wagon-providers/wagon-http-shared/src/main/java/org/apache/maven/wagon/shared/http/EncodingUtil.java @@ -24,6 +24,9 @@ import java.net.URI; import java.net.URISyntaxException; import java.net.URL; +import org.apache.http.client.utils.URLEncodedUtils; +import org.codehaus.plexus.util.StringUtils; + /** * Encoding utility. * @@ -38,7 +41,9 @@ public class EncodingUtil * @return Parsed/encoded {@link URI} that represents the string form URL passed in. * @throws MalformedURLException * @throws URISyntaxException + * @deprecated to be removed with 4.0.0 */ + @Deprecated public static URI encodeURL( String url ) throws MalformedURLException, URISyntaxException { @@ -61,7 +66,9 @@ public class EncodingUtil * @param url Raw/decoded string form of a URL to parse/encode. * @return Parsed/encoded URI (as string) that represents the * @throws IllegalArgumentException in case the URL string is invalid. + * @deprecated To be remvoed with 4.0.0 */ + @Deprecated public static String encodeURLToString( String url ) { try @@ -77,8 +84,8 @@ public class EncodingUtil /** * Parses and returns an encoded version of the given URL string alongside the given paths. * - * @param baseUrl Base URL to use when constructing the final URL, ie: scheme://authority/initial.path. - * @param paths Additional path(s) to append at the end of the base path. + * @param baseUrl Base URL to use when constructing the final URL. This has to be a valid URL already. + * @param paths Additional unencoded path(s) to append at the end of the base path. * @return Composed URL (base + paths) already encoded, separating the individual path components by "/". * @since TODO */ @@ -86,20 +93,17 @@ public class EncodingUtil { StringBuilder url = new StringBuilder( baseUrl ); - String[] parts = paths == null ? // + String[] segments = paths == null ? // new String[0] : // paths.length == 1 ? paths[0].split( "/" ) : paths; - for ( String part : parts ) - { - if ( !url.toString().endsWith( "/" ) ) - { - url.append( '/' ); - } + String path = URLEncodedUtils.formatSegments(segments); - url.append( part ); - } + if (url.toString().endsWith("/") && StringUtils.isNotEmpty(path)) + url.deleteCharAt(url.length() - 1); + + url.append(path); - return encodeURLToString( url.toString() ); + return url.toString(); } } diff --git a/wagon-providers/wagon-http-shared/src/test/java/org/apache/maven/wagon/shared/http/EncodingUtilTest.java b/wagon-providers/wagon-http-shared/src/test/java/org/apache/maven/wagon/shared/http/EncodingUtilTest.java index f20f35f..e7fab98 100644 --- a/wagon-providers/wagon-http-shared/src/test/java/org/apache/maven/wagon/shared/http/EncodingUtilTest.java +++ b/wagon-providers/wagon-http-shared/src/test/java/org/apache/maven/wagon/shared/http/EncodingUtilTest.java @@ -46,8 +46,65 @@ public class EncodingUtilTest public void testEncodeURLWithSpacesInBothBaseAndPath() throws URISyntaxException, MalformedURLException { - String encodedURL = EncodingUtil.encodeURLToString( "file://host:1/with a", "path with spaces" ); + String encodedURL = EncodingUtil.encodeURLToString( "file://host:1/with%20a", "path with spaces" ); assertEquals( "file://host:1/with%20a/path%20with%20spaces", encodedURL ); } + + public void testEncodeURLWithSlashes1() + throws URISyntaxException, MalformedURLException + { + String encodedURL = EncodingUtil.encodeURLToString( "file://host:1/basePath", new String[] {"a", "b", "c" } ); + + assertEquals( "file://host:1/basePath/a/b/c", encodedURL ); + } + + public void testEncodeURLWithSlashes2() + throws URISyntaxException, MalformedURLException + { + String encodedURL = EncodingUtil.encodeURLToString( "file://host:1/basePath/", new String[] {"a", "b", "c" } ); + + assertEquals( "file://host:1/basePath/a/b/c", encodedURL ); + } + + public void testEncodeURLWithSlashes3() + throws URISyntaxException, MalformedURLException + { + String encodedURL = EncodingUtil.encodeURLToString( "file://host:1/basePath/", new String[0] ); + + assertEquals( "file://host:1/basePath/", encodedURL ); + } + + public void testEncodeURLWithSlashes4() + throws URISyntaxException, MalformedURLException + { + String encodedURL = EncodingUtil.encodeURLToString( "file://host:1/basePath", new String[0] ); + + assertEquals( "file://host:1/basePath", encodedURL ); + } + + public void testEncodeURLWithSlashes5() + throws URISyntaxException, MalformedURLException + { + String encodedURL = EncodingUtil.encodeURLToString( "file://host:1/basePath", + new String[] {"a/1", "b/1", "c/1" } ); + + assertEquals( "file://host:1/basePath/a%2F1/b%2F1/c%2F1", encodedURL ); + } + + public void testEncodeURLWithSlashes6() + throws URISyntaxException, MalformedURLException + { + String encodedURL = EncodingUtil.encodeURLToString( "file://host:1/", new String[0] ); + + assertEquals( "file://host:1/", encodedURL ); + } + + public void testEncodeURLWithSlashes7() + throws URISyntaxException, MalformedURLException + { + String encodedURL = EncodingUtil.encodeURLToString( "file://host:1", new String[0] ); + + assertEquals( "file://host:1", encodedURL ); + } }
