Skip to content

Commit 64bc3a0

Browse files
committed
SQL: fix URI path being lost in case of hosted ES scenario (elastic#44776)
(cherry picked from commit 06dea85)
1 parent da9087a commit 64bc3a0

File tree

3 files changed

+84
-1
lines changed

3 files changed

+84
-1
lines changed

x-pack/plugin/sql/sql-client/src/main/java/org/elasticsearch/xpack/sql/client/JreHttpUrlConnection.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import javax.sql.rowset.serial.SerialException;
3636

3737
import static java.util.Collections.emptyMap;
38+
import static org.elasticsearch.xpack.sql.client.UriUtils.appendSegmentToPath;
3839
import static org.elasticsearch.xpack.sql.proto.Protocol.SQL_QUERY_REST_ENDPOINT;
3940

4041
/**
@@ -52,7 +53,7 @@ public class JreHttpUrlConnection implements Closeable {
5253
+ "?error_trace] and method [POST], allowed:";
5354

5455
public static <R> R http(String path, String query, ConnectionConfiguration cfg, Function<JreHttpUrlConnection, R> handler) {
55-
final URI uriPath = cfg.baseUri().resolve(path); // update path if needed
56+
final URI uriPath = appendSegmentToPath(cfg.baseUri(), path); // update path if needed
5657
final String uriQuery = query == null ? uriPath.getQuery() : query; // update query if needed
5758
final URL url;
5859
try {

x-pack/plugin/sql/sql-client/src/main/java/org/elasticsearch/xpack/sql/client/UriUtils.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,4 +76,33 @@ public static URI removeQuery(URI uri, String connectionString, URI defaultURI)
7676
throw new IllegalArgumentException("Invalid connection configuration [" + connectionString + "]: " + e.getMessage(), e);
7777
}
7878
}
79+
80+
public static URI appendSegmentToPath(URI uri, String segment) {
81+
if (uri == null) {
82+
throw new IllegalArgumentException("URI must not be null");
83+
}
84+
if (segment == null || segment.isEmpty() || "/".equals(segment)) {
85+
return uri;
86+
}
87+
88+
String path = uri.getPath();
89+
String concatenatedPath = "";
90+
String cleanSegment = segment.startsWith("/") ? segment.substring(1) : segment;
91+
92+
if (path == null || path.isEmpty()) {
93+
path = "/";
94+
}
95+
96+
if (path.charAt(path.length() - 1) == '/') {
97+
concatenatedPath = path + cleanSegment;
98+
} else {
99+
concatenatedPath = path + "/" + cleanSegment;
100+
}
101+
try {
102+
return new URI(uri.getScheme(), uri.getUserInfo(), uri.getHost(), uri.getPort(), concatenatedPath,
103+
uri.getQuery(), uri.getFragment());
104+
} catch (URISyntaxException e) {
105+
throw new IllegalArgumentException("Invalid segment [" + segment + "] for URI [" + uri + "]: " + e.getMessage(), e);
106+
}
107+
}
79108
}

x-pack/plugin/sql/sql-client/src/test/java/org/elasticsearch/xpack/sql/client/UriUtilsTests.java

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
import java.net.URI;
1111

12+
import static org.elasticsearch.xpack.sql.client.UriUtils.appendSegmentToPath;
1213
import static org.elasticsearch.xpack.sql.client.UriUtils.parseURI;
1314
import static org.elasticsearch.xpack.sql.client.UriUtils.removeQuery;
1415

@@ -84,4 +85,56 @@ public void testRemoveQueryNoQuery() throws Exception {
8485
assertEquals(URI.create("http://server:9100"),
8586
removeQuery(URI.create("http://server:9100"), "http://server:9100", DEFAULT_URI));
8687
}
88+
89+
public void testAppendEmptySegmentToPath() throws Exception {
90+
assertEquals(URI.create("http://server:9100"),
91+
appendSegmentToPath(URI.create("http://server:9100"), ""));
92+
}
93+
94+
public void testAppendNullSegmentToPath() throws Exception {
95+
assertEquals(URI.create("http://server:9100"),
96+
appendSegmentToPath(URI.create("http://server:9100"), null));
97+
}
98+
99+
public void testAppendSegmentToNullPath() throws Exception {
100+
assertEquals(
101+
"URI must not be null",
102+
expectThrows(IllegalArgumentException.class, () -> appendSegmentToPath(null, "/_sql")).getMessage()
103+
);
104+
}
105+
106+
public void testAppendSegmentToEmptyPath() throws Exception {
107+
assertEquals(URI.create("/_sql"),
108+
appendSegmentToPath(URI.create(""), "/_sql"));
109+
}
110+
111+
public void testAppendSlashSegmentToPath() throws Exception {
112+
assertEquals(URI.create("http://server:9100"),
113+
appendSegmentToPath(URI.create("http://server:9100"), "/"));
114+
}
115+
116+
public void testAppendSqlSegmentToPath() throws Exception {
117+
assertEquals(URI.create("http://server:9100/_sql"),
118+
appendSegmentToPath(URI.create("http://server:9100"), "/_sql"));
119+
}
120+
121+
public void testAppendSqlSegmentNoSlashToPath() throws Exception {
122+
assertEquals(URI.create("http://server:9100/_sql"),
123+
appendSegmentToPath(URI.create("http://server:9100"), "_sql"));
124+
}
125+
126+
public void testAppendSegmentToPath() throws Exception {
127+
assertEquals(URI.create("http://server:9100/es_rest/_sql"),
128+
appendSegmentToPath(URI.create("http://server:9100/es_rest"), "/_sql"));
129+
}
130+
131+
public void testAppendSegmentNoSlashToPath() throws Exception {
132+
assertEquals(URI.create("http://server:9100/es_rest/_sql"),
133+
appendSegmentToPath(URI.create("http://server:9100/es_rest"), "_sql"));
134+
}
135+
136+
public void testAppendSegmentTwoSlashesToPath() throws Exception {
137+
assertEquals(URI.create("https://server:9100/es_rest/_sql"),
138+
appendSegmentToPath(URI.create("https://server:9100/es_rest/"), "/_sql"));
139+
}
87140
}

0 commit comments

Comments
 (0)