From e973049c6552483685eb53c1fc245a619f6f48f6 Mon Sep 17 00:00:00 2001 From: roy Date: Mon, 15 Oct 2018 01:23:43 +0800 Subject: [PATCH] Rest-High-Level-Client:fix uri encode bug when url path start with '/' . Relates to issues #34433 --- .../client/RequestConverters.java | 4 ++- .../client/RequestConvertersTests.java | 32 +++++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/RequestConverters.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/RequestConverters.java index 9c461a404cf8f..add88c51bd97b 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/RequestConverters.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/RequestConverters.java @@ -996,7 +996,9 @@ private static String encodePart(String pathPart) { //encode each part (e.g. index, type and id) separately before merging them into the path //we prepend "/" to the path part to make this path absolute, otherwise there can be issues with //paths that start with `-` or contain `:` - URI uri = new URI(null, null, null, -1, "/" + pathPart, null, null); + //the authority must be an empty string and not null, else paths that being with slashes could have them + //misinterpreted as part of the authority. + URI uri = new URI(null, "", "/" + pathPart, null, null); //manually encode any slash that each part may contain return uri.getRawPath().substring(1).replaceAll("/", "%2F"); } catch (URISyntaxException e) { diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/RequestConvertersTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/RequestConvertersTests.java index 3801dfe71de9c..f75f6bf074608 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/RequestConvertersTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/RequestConvertersTests.java @@ -1411,6 +1411,38 @@ public void testEndpointBuilderEncodeParts() { .addPathPartAsIs("cache/clear"); assertEquals("/index1,index2/cache/clear", endpointBuilder.build()); } + { + EndpointBuilder endpointBuilder = new EndpointBuilder().addPathPart("/foo"); + assertEquals("/%2Ffoo", endpointBuilder.build()); + } + { + EndpointBuilder endpointBuilder = new EndpointBuilder().addPathPart("//foo"); + assertEquals("/%2F%2Ffoo", endpointBuilder.build()); + } + { + EndpointBuilder endpointBuilder = new EndpointBuilder().addPathPart("///foo"); + assertEquals("/%2F%2F%2Ffoo", endpointBuilder.build()); + } + { + EndpointBuilder endpointBuilder = new EndpointBuilder().addPathPart("/foo/bar"); + assertEquals("/%2Ffoo%2Fbar", endpointBuilder.build()); + } + { + EndpointBuilder endpointBuilder = new EndpointBuilder().addPathPart("//foo/bar"); + assertEquals("/%2F%2Ffoo%2Fbar", endpointBuilder.build()); + } + { + EndpointBuilder endpointBuilder = new EndpointBuilder().addPathPart("/foo@bar"); + assertEquals("/%2Ffoo@bar", endpointBuilder.build()); + } + { + EndpointBuilder endpointBuilder = new EndpointBuilder().addPathPart("//foo@bar"); + assertEquals("/%2F%2Ffoo@bar", endpointBuilder.build()); + } + { + EndpointBuilder endpointBuilder = new EndpointBuilder().addPathPart("/part1").addPathPart("//part2").addPathPart("///part3"); + assertEquals("/%2Fpart1/%2F%2Fpart2/%2F%2F%2Fpart3", endpointBuilder.build()); + } } public void testEndpoint() {