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 25c715b63344d29b7440f6c6d6d4d6674cf05bf7 Author: Michael Osipov <[email protected]> AuthorDate: Wed Oct 16 22:42:06 2019 +0200 [WAGON-569] Inconsistent encoding behavior for repository URLs with spaces --- .../wagon/shared/http/AbstractHttpClientWagon.java | 18 +++++-- .../maven/wagon/shared/http/EncodingUtil.java | 27 +++++----- .../maven/wagon/shared/http/EncodingUtilTest.java | 59 +++++++++++++++++++++- 3 files changed, 86 insertions(+), 18 deletions(-) diff --git a/wagon-providers/wagon-http-shared/src/main/java/org/apache/maven/wagon/shared/http/AbstractHttpClientWagon.java b/wagon-providers/wagon-http-shared/src/main/java/org/apache/maven/wagon/shared/http/AbstractHttpClientWagon.java index 1d25664..4c50a9d 100755 --- a/wagon-providers/wagon-http-shared/src/main/java/org/apache/maven/wagon/shared/http/AbstractHttpClientWagon.java +++ b/wagon-providers/wagon-http-shared/src/main/java/org/apache/maven/wagon/shared/http/AbstractHttpClientWagon.java @@ -660,9 +660,19 @@ public abstract class AbstractHttpClientWagon */ private String buildUrl( Resource resource ) { - return EncodingUtil.encodeURLToString( getRepository().getUrl(), resource.getName() ); + return buildUrl( resource.getName() ); } + /** + * Builds a complete URL string from the repository URL and the relative path of the resource passed. + * + * @param resourceName the resourcerelative path + * @return the complete URL + */ + private String buildUrl( String resourceName ) + { + return EncodingUtil.encodeURLToString( getRepository().getUrl(), resourceName ); + } private void put( Resource resource, File source, HttpEntity httpEntity, String url ) throws TransferFailedException, AuthorizationException, ResourceDoesNotExistException @@ -813,8 +823,7 @@ public abstract class AbstractHttpClientWagon private boolean resourceExists( int wait, String resourceName ) throws TransferFailedException, AuthorizationException { - String repositoryUrl = getRepository().getUrl(); - String url = repositoryUrl + ( repositoryUrl.endsWith( "/" ) ? "" : "/" ) + resourceName; + String url = buildUrl( resourceName ); HttpHead headMethod = new HttpHead( url ); try { @@ -1099,8 +1108,7 @@ public abstract class AbstractHttpClientWagon { Resource resource = inputData.getResource(); - String repositoryUrl = getRepository().getUrl(); - String url = repositoryUrl + ( repositoryUrl.endsWith( "/" ) ? "" : "/" ) + resource.getName(); + String url = buildUrl( resource ); HttpGet getMethod = new HttpGet( url ); long timestamp = resource.getLastModified(); if ( timestamp > 0 ) 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..c302ab7 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,8 @@ import java.net.URI; import java.net.URISyntaxException; import java.net.URL; +import org.apache.http.client.utils.URLEncodedUtils; + /** * Encoding utility. * @@ -38,7 +40,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 +65,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 +83,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 +92,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("/") && !path.isEmpty()) + 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 ); + } }
