From f6d318a7828bf02ebd1d3322038fbe78095e60ad Mon Sep 17 00:00:00 2001 From: Alexander Reelsen Date: Fri, 23 Mar 2018 12:16:34 +0100 Subject: [PATCH] Watcher: Prevent question mark in HttpClient with empty params (elastic/x-pack-elasticsearch#4206) The HTTPClient in watcher always appended a question mark at the end of an URL, regardless if parameters were used or not. This commit adds a check to only pass valid parameters to the URI construction. Original commit: elastic/x-pack-elasticsearch@184f8f441ccaa61b7f6a238823ade3fb07c7709f --- .../xpack/watcher/common/http/HttpClient.java | 3 ++- .../xpack/watcher/common/http/HttpClientTests.java | 11 +++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/common/http/HttpClient.java b/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/common/http/HttpClient.java index 597425a2c23..95cef69c9d7 100644 --- a/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/common/http/HttpClient.java +++ b/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/common/http/HttpClient.java @@ -241,8 +241,9 @@ public class HttpClient extends AbstractComponent { try { List qparams = new ArrayList<>(request.params.size()); request.params.forEach((k, v) -> qparams.add(new BasicNameValuePair(k, v))); + String format = URLEncodedUtils.format(qparams, "UTF-8"); URI uri = URIUtils.createURI(request.scheme.scheme(), request.host, request.port, request.path, - URLEncodedUtils.format(qparams, "UTF-8"), null); + Strings.isNullOrEmpty(format) ? null : format, null); return uri; } catch (URISyntaxException e) { diff --git a/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/common/http/HttpClientTests.java b/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/common/http/HttpClientTests.java index 2cf657f0557..2a02c5300bd 100644 --- a/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/common/http/HttpClientTests.java +++ b/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/common/http/HttpClientTests.java @@ -595,4 +595,15 @@ public class HttpClientTests extends ESTestCase { assertThat(webServer.requests().get(0).getHeader(HttpHeaders.CONTENT_TYPE), is(XContentType.JSON.mediaType())); assertThat(webServer.requests().get(0).getBody(), is(body)); } + + public void testThatUrlDoesNotContainQuestionMarkAtTheEnd() throws Exception { + webServer.enqueue(new MockResponse().setResponseCode(200).setBody("whatever")); + + HttpRequest request = HttpRequest.builder("localhost", webServer.getPort()) + .path("foo") + .build(); + httpClient.execute(request); + assertThat(webServer.requests(), hasSize(1)); + assertThat(webServer.requests().get(0).getUri().getRawPath(), is("/foo")); + } }