From 0fe771645978ebbd08b1af0095954c124ae81a41 Mon Sep 17 00:00:00 2001 From: Alexander Reelsen Date: Mon, 1 Feb 2016 10:37:44 +0100 Subject: [PATCH] HttpClient: Prevent NPE when no path is specified Original commit: elastic/x-pack-elasticsearch@47f26a58509cc955f9bdbdfae389e89e69df15c6 --- .../watcher/support/http/HttpClient.java | 2 +- .../watcher/support/http/HttpClientTests.java | 13 +++++++++---- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/elasticsearch/x-pack/watcher/src/main/java/org/elasticsearch/watcher/support/http/HttpClient.java b/elasticsearch/x-pack/watcher/src/main/java/org/elasticsearch/watcher/support/http/HttpClient.java index f9af31b5e90..17d531851a9 100644 --- a/elasticsearch/x-pack/watcher/src/main/java/org/elasticsearch/watcher/support/http/HttpClient.java +++ b/elasticsearch/x-pack/watcher/src/main/java/org/elasticsearch/watcher/support/http/HttpClient.java @@ -142,7 +142,7 @@ public class HttpClient extends AbstractLifecycleComponent { queryString = builder.toString(); } - String path = request.path; + String path = Strings.hasLength(request.path) ? request.path : ""; if (Strings.hasLength(queryString)) { path += "?" + queryString; } diff --git a/elasticsearch/x-pack/watcher/src/test/java/org/elasticsearch/watcher/support/http/HttpClientTests.java b/elasticsearch/x-pack/watcher/src/test/java/org/elasticsearch/watcher/support/http/HttpClientTests.java index 7837a92ab38..6fd00b73799 100644 --- a/elasticsearch/x-pack/watcher/src/test/java/org/elasticsearch/watcher/support/http/HttpClientTests.java +++ b/elasticsearch/x-pack/watcher/src/test/java/org/elasticsearch/watcher/support/http/HttpClientTests.java @@ -24,10 +24,7 @@ import org.junit.Before; import javax.net.ssl.SSLSocket; import javax.net.ssl.SSLSocketFactory; import java.io.IOException; -import java.net.BindException; -import java.net.InetAddress; -import java.net.Socket; -import java.net.UnknownHostException; +import java.net.*; import java.nio.charset.StandardCharsets; import java.nio.file.Path; import java.security.UnrecoverableKeyException; @@ -148,6 +145,14 @@ public class HttpClientTests extends ESTestCase { assertThat(recordedRequest.getHeader("Authorization"), equalTo("Basic dXNlcjpwYXNz")); } + public void testNoPathSpecified() throws Exception { + webServer.enqueue(new MockResponse().setResponseCode(200).setBody("doesntmatter")); + HttpRequest.Builder request = HttpRequest.builder("localhost", webPort).method(HttpMethod.GET); + httpClient.execute(request.build()); + RecordedRequest recordedRequest = webServer.takeRequest(); + assertThat(recordedRequest.getPath(), equalTo("/")); + } + public void testHttps() throws Exception { Path resource = getDataPath("/org/elasticsearch/shield/keystore/truststore-testnode-only.jks");