This is an automated email from the ASF dual-hosted git repository.
houston pushed a commit to branch branch_9_7
in repository https://gitbox.apache.org/repos/asf/solr.git
The following commit(s) were added to refs/heads/branch_9_7 by this push:
new 36f3622ea6f SOLR-17427: Fix url building in cli (#2678)
36f3622ea6f is described below
commit 36f3622ea6f0abf6567a52662db854c693b67e43
Author: Houston Putman <[email protected]>
AuthorDate: Fri Aug 30 10:40:37 2024 -0500
SOLR-17427: Fix url building in cli (#2678)
(cherry picked from commit e35207268b8b71559b9bdd8a164e9bc1f2018dfb)
---
.../core/src/java/org/apache/solr/cli/ApiTool.java | 2 +-
.../src/java/org/apache/solr/cli/PostTool.java | 41 ++++++++++++-------
.../java/org/apache/solr/cli/SimplePostTool.java | 47 ++++------------------
.../core/src/java/org/apache/solr/cli/SolrCLI.java | 22 ++++------
.../src/test/org/apache/solr/cli/ApiToolTest.java | 16 ++++++++
.../src/test/org/apache/solr/cli/PostToolTest.java | 34 +++++++++++-----
.../org/apache/solr/cli/SimplePostToolTest.java | 19 ++++++++-
7 files changed, 98 insertions(+), 83 deletions(-)
diff --git a/solr/core/src/java/org/apache/solr/cli/ApiTool.java
b/solr/core/src/java/org/apache/solr/cli/ApiTool.java
index e6b6627cbfd..1c412062ec8 100644
--- a/solr/core/src/java/org/apache/solr/cli/ApiTool.java
+++ b/solr/core/src/java/org/apache/solr/cli/ApiTool.java
@@ -120,7 +120,7 @@ public class ApiTool extends ToolBase {
* @return Solr base url with port and root (from above example
http://localhost:8983/solr)
*/
public static String getSolrUrlFromUri(URI uri) {
- return uri.getScheme() + "://" + uri.getAuthority() + "/" +
uri.getPath().split("/")[1];
+ return uri.resolve("/" + uri.getPath().split("/")[1]).toString();
}
public static ModifiableSolrParams getSolrParamsFromUri(URI uri) {
diff --git a/solr/core/src/java/org/apache/solr/cli/PostTool.java
b/solr/core/src/java/org/apache/solr/cli/PostTool.java
index 26e9b2999c3..9b9b5d3225d 100644
--- a/solr/core/src/java/org/apache/solr/cli/PostTool.java
+++ b/solr/core/src/java/org/apache/solr/cli/PostTool.java
@@ -403,7 +403,7 @@ public class PostTool extends ToolBase {
numPagesPosted = postWebPages(args, 0, out);
info(numPagesPosted + " web pages indexed.");
- } catch (MalformedURLException e) {
+ } catch (MalformedURLException | URISyntaxException e) {
warn("Wrong URL trying to append /extract to " + solrUpdateUrl);
}
}
@@ -529,7 +529,7 @@ public class PostTool extends ToolBase {
postFile(srcFile, out, type);
Thread.sleep(delay * 1000L);
filesPosted++;
- } catch (InterruptedException | MalformedURLException e) {
+ } catch (InterruptedException | MalformedURLException |
URISyntaxException e) {
throw new RuntimeException(e);
}
}
@@ -695,13 +695,14 @@ public class PostTool extends ToolBase {
* @param link the absolute or relative link
* @return the string version of the full URL
*/
- protected String computeFullUrl(URL baseUrl, String link) {
+ protected static String computeFullUrl(URL baseUrl, String link)
+ throws MalformedURLException, URISyntaxException {
if (link == null || link.length() == 0) {
return null;
}
if (!link.startsWith("http")) {
if (link.startsWith("/")) {
- link = baseUrl.getProtocol() + "://" + baseUrl.getAuthority() + link;
+ link = baseUrl.toURI().resolve(link).toString();
} else {
if (link.contains(":")) {
return null; // Skip non-relative URLs
@@ -711,10 +712,12 @@ public class PostTool extends ToolBase {
int sep = path.lastIndexOf('/');
String file = path.substring(sep + 1);
if (file.contains(".") || file.contains("?")) {
- path = path.substring(0, sep);
+ path = path.substring(0, sep + 1);
+ } else {
+ path = path + "/";
}
}
- link = baseUrl.getProtocol() + "://" + baseUrl.getAuthority() + path +
"/" + link;
+ link = baseUrl.toURI().resolve(path + link).toString();
}
}
link = normalizeUrlEnding(link);
@@ -814,7 +817,8 @@ public class PostTool extends ToolBase {
}
/** Opens the file and posts its contents to the solrUrl, writes to response
to output. */
- public void postFile(File file, OutputStream output, String type) throws
MalformedURLException {
+ public void postFile(File file, OutputStream output, String type)
+ throws MalformedURLException, URISyntaxException {
InputStream is = null;
URL url = solrUpdateUrl;
@@ -892,14 +896,21 @@ public class PostTool extends ToolBase {
* @param append the path to append
* @return the final URL version
*/
- protected static URL appendUrlPath(URL url, String append) throws
MalformedURLException {
- return new URL(
- url.getProtocol()
- + "://"
- + url.getAuthority()
- + url.getPath()
- + append
- + (url.getQuery() != null ? "?" + url.getQuery() : ""));
+ protected static URL appendUrlPath(URL url, String append)
+ throws URISyntaxException, MalformedURLException {
+ if (append == null || append.isEmpty()) {
+ return url;
+ }
+ if (append.startsWith("/")) {
+ append = append.substring(1);
+ }
+ if (url.getQuery() != null && !url.getQuery().isEmpty()) {
+ append += "?" + url.getQuery();
+ }
+ if (!url.getPath().endsWith("/")) {
+ append = url.getPath() + "/" + append;
+ }
+ return url.toURI().resolve(append).toURL();
}
/**
diff --git a/solr/core/src/java/org/apache/solr/cli/SimplePostTool.java
b/solr/core/src/java/org/apache/solr/cli/SimplePostTool.java
index 1c4ceaba9e8..954146e170b 100644
--- a/solr/core/src/java/org/apache/solr/cli/SimplePostTool.java
+++ b/solr/core/src/java/org/apache/solr/cli/SimplePostTool.java
@@ -406,7 +406,7 @@ public class SimplePostTool {
}
numPagesPosted = postWebPages(args, 0, out);
info(numPagesPosted + " web pages indexed.");
- } catch (MalformedURLException e) {
+ } catch (URISyntaxException | MalformedURLException e) {
fatal("Wrong URL trying to append /extract to " + solrUrl);
}
}
@@ -766,35 +766,9 @@ public class SimplePostTool {
* @param link the absolute or relative link
* @return the string version of the full URL
*/
- protected String computeFullUrl(URL baseUrl, String link) {
- if (link == null || link.length() == 0) {
- return null;
- }
- if (!link.startsWith("http")) {
- if (link.startsWith("/")) {
- link = baseUrl.getProtocol() + "://" + baseUrl.getAuthority() + link;
- } else {
- if (link.contains(":")) {
- return null; // Skip non-relative URLs
- }
- String path = baseUrl.getPath();
- if (!path.endsWith("/")) {
- int sep = path.lastIndexOf('/');
- String file = path.substring(sep + 1);
- if (file.contains(".") || file.contains("?")) {
- path = path.substring(0, sep);
- }
- }
- link = baseUrl.getProtocol() + "://" + baseUrl.getAuthority() + path +
"/" + link;
- }
- }
- link = normalizeUrlEnding(link);
- String l = link.toLowerCase(Locale.ROOT);
- // Simple brute force skip images
- if (l.endsWith(".jpg") || l.endsWith(".jpeg") || l.endsWith(".png") ||
l.endsWith(".gif")) {
- return null; // Skip images
- }
- return link;
+ protected String computeFullUrl(URL baseUrl, String link)
+ throws MalformedURLException, URISyntaxException {
+ return PostTool.computeFullUrl(baseUrl, link);
}
/**
@@ -924,7 +898,7 @@ public class SimplePostTool {
+ (mockMode ? " MOCK!" : ""));
is = new FileInputStream(file);
postData(is, file.length(), output, type, url);
- } catch (IOException e) {
+ } catch (IOException | URISyntaxException e) {
warn("Can't open/read file: " + file);
} finally {
try {
@@ -944,14 +918,9 @@ public class SimplePostTool {
* @param append the path to append
* @return the final URL version
*/
- protected static URL appendUrlPath(URL url, String append) throws
MalformedURLException {
- return new URL(
- url.getProtocol()
- + "://"
- + url.getAuthority()
- + url.getPath()
- + append
- + (url.getQuery() != null ? "?" + url.getQuery() : ""));
+ protected static URL appendUrlPath(URL url, String append)
+ throws MalformedURLException, URISyntaxException {
+ return PostTool.appendUrlPath(url, append);
}
/**
diff --git a/solr/core/src/java/org/apache/solr/cli/SolrCLI.java
b/solr/core/src/java/org/apache/solr/cli/SolrCLI.java
index 19e50f3d27d..c585e4f55d2 100755
--- a/solr/core/src/java/org/apache/solr/cli/SolrCLI.java
+++ b/solr/core/src/java/org/apache/solr/cli/SolrCLI.java
@@ -613,21 +613,13 @@ public class SolrCLI implements CLIO {
// Only consider URI path component when normalizing hostContext
if (urlPath.contains(hostContext)) {
String newSolrUrl =
- String.format(
- Locale.ROOT,
- "%s://%s:%s%s",
- uri.getScheme(),
- uri.getHost(),
- uri.getPort(),
- urlPath.substring(0, urlPath.indexOf(hostContext)));
- if (logUrlFormatWarning) {
- CLIO.out(
- "WARNING: URLs provided to this tool needn't include Solr's
context-root (e.g. \"/solr\"). Such URLs are deprecated and support for them
will be removed in a future release. Correcting from ["
- + solrUrl
- + "] to ["
- + newSolrUrl
- + "].");
- }
+ uri.resolve(urlPath.substring(0, urlPath.indexOf(hostContext)) +
"/").toString();
+ CLIO.out(
+ "WARNING: URLs provided to this tool needn't include Solr's
context-root (e.g. \"/solr\"). Such URLs are deprecated and support for them
will be removed in a future release. Correcting from ["
+ + solrUrl
+ + "] to ["
+ + newSolrUrl
+ + "].");
solrUrl = newSolrUrl;
}
if (solrUrl.endsWith("/")) {
diff --git a/solr/core/src/test/org/apache/solr/cli/ApiToolTest.java
b/solr/core/src/test/org/apache/solr/cli/ApiToolTest.java
index d817845854a..b013fec9de1 100644
--- a/solr/core/src/test/org/apache/solr/cli/ApiToolTest.java
+++ b/solr/core/src/test/org/apache/solr/cli/ApiToolTest.java
@@ -102,4 +102,20 @@ public class ApiToolTest extends SolrCloudTestCase {
String.format(Locale.ROOT, "Could not find string %s in response:
\n%s", find, json),
json.contains(find));
}
+
+ @Test
+ public void testSolrUrlParsing() throws Exception {
+ assertEquals(
+ "https://test-host.solr:8983/solr",
+
ApiTool.getSolrUrlFromUri(URI.create("https://test-host.solr:8983/solr")));
+ assertEquals(
+ "https://test-host.solr:8983/solr",
+
ApiTool.getSolrUrlFromUri(URI.create("https://test-host.solr:8983/solr/test/api")));
+ assertEquals(
+ "https://test-host.solr/solr",
+
ApiTool.getSolrUrlFromUri(URI.create("https://test-host.solr/solr/test/api")));
+ assertEquals(
+ "http://test-host.solr/solr",
+
ApiTool.getSolrUrlFromUri(URI.create("http://test-host.solr/solr/test/api")));
+ }
}
diff --git a/solr/core/src/test/org/apache/solr/cli/PostToolTest.java
b/solr/core/src/test/org/apache/solr/cli/PostToolTest.java
index d8f02363e47..f1d2949a6dc 100644
--- a/solr/core/src/test/org/apache/solr/cli/PostToolTest.java
+++ b/solr/core/src/test/org/apache/solr/cli/PostToolTest.java
@@ -194,25 +194,22 @@ public class PostToolTest extends SolrCloudTestCase {
}
@Test
- public void testComputeFullUrl() throws IOException {
-
- PostTool webPostTool = new PostTool();
-
+ public void testComputeFullUrl() throws IOException, URISyntaxException {
assertEquals(
"http://[ff01::114]/index.html",
- webPostTool.computeFullUrl(new URL("http://[ff01::114]/"),
"/index.html"));
+ PostTool.computeFullUrl(new URL("http://[ff01::114]/"),
"/index.html"));
assertEquals(
"http://[ff01::114]/index.html",
- webPostTool.computeFullUrl(new URL("http://[ff01::114]/foo/bar/"),
"/index.html"));
+ PostTool.computeFullUrl(new URL("http://[ff01::114]/foo/bar/"),
"/index.html"));
assertEquals(
"http://[ff01::114]/fil.html",
- webPostTool.computeFullUrl(new
URL("http://[ff01::114]/foo.htm?baz#hello"), "fil.html"));
+ PostTool.computeFullUrl(new
URL("http://[ff01::114]/foo.htm?baz#hello"), "fil.html"));
// TODO: How to know what is the base if URL path ends with "foo"??
// assertEquals("http://[ff01::114]/fil.html", t_web.computeFullUrl(new
// URL("http://[ff01::114]/foo?baz#hello"), "fil.html"));
- assertNull(webPostTool.computeFullUrl(new URL("http://[ff01::114]/"),
"fil.jpg"));
- assertNull(webPostTool.computeFullUrl(new URL("http://[ff01::114]/"),
"mailto:[email protected]"));
- assertNull(webPostTool.computeFullUrl(new URL("http://[ff01::114]/"),
"ftp://server/file"));
+ assertNull(PostTool.computeFullUrl(new URL("http://[ff01::114]/"),
"fil.jpg"));
+ assertNull(PostTool.computeFullUrl(new URL("http://[ff01::114]/"),
"mailto:[email protected]"));
+ assertNull(PostTool.computeFullUrl(new URL("http://[ff01::114]/"),
"ftp://server/file"));
}
@Test
@@ -239,10 +236,25 @@ public class PostToolTest extends SolrCloudTestCase {
}
@Test
- public void testAppendUrlPath() throws MalformedURLException {
+ public void testAppendUrlPath() throws MalformedURLException,
URISyntaxException {
assertEquals(
new URL("http://[ff01::114]/a?foo=bar"),
PostTool.appendUrlPath(new URL("http://[ff01::114]?foo=bar"), "/a"));
+ assertEquals(
+ new URL("http://[ff01::114]/a?foo=bar"),
+ PostTool.appendUrlPath(new URL("http://[ff01::114]/?foo=bar"), "/a"));
+ assertEquals(
+ new URL("http://[ff01::114]/a/b?foo=bar"),
+ PostTool.appendUrlPath(new URL("http://[ff01::114]/a?foo=bar"), "/b"));
+ assertEquals(
+ new URL("http://[ff01::114]/a/b?foo=bar"),
+ PostTool.appendUrlPath(new URL("http://[ff01::114]/a/?foo=bar"),
"/b"));
+ assertEquals(
+ new URL("http://[ff01::114]/a/b?foo=bar"),
+ PostTool.appendUrlPath(new URL("http://[ff01::114]/a?foo=bar"), "b"));
+ assertEquals(
+ new URL("http://[ff01::114]/a/b?foo=bar"),
+ PostTool.appendUrlPath(new URL("http://[ff01::114]/a/?foo=bar"), "b"));
}
@Test
diff --git a/solr/core/src/test/org/apache/solr/cli/SimplePostToolTest.java
b/solr/core/src/test/org/apache/solr/cli/SimplePostToolTest.java
index fdd58149a82..acbb0909610 100644
--- a/solr/core/src/test/org/apache/solr/cli/SimplePostToolTest.java
+++ b/solr/core/src/test/org/apache/solr/cli/SimplePostToolTest.java
@@ -106,7 +106,7 @@ public class SimplePostToolTest extends SolrTestCaseJ4 {
}
@Test
- public void testComputeFullUrl() throws MalformedURLException {
+ public void testComputeFullUrl() throws MalformedURLException,
URISyntaxException {
assertEquals(
"http://[ff01::114]/index.html",
t_web.computeFullUrl(new URL("http://[ff01::114]/"), "/index.html"));
@@ -153,10 +153,25 @@ public class SimplePostToolTest extends SolrTestCaseJ4 {
}
@Test
- public void testAppendUrlPath() throws MalformedURLException {
+ public void testAppendUrlPath() throws MalformedURLException,
URISyntaxException {
assertEquals(
new URL("http://[ff01::114]/a?foo=bar"),
SimplePostTool.appendUrlPath(new URL("http://[ff01::114]?foo=bar"),
"/a"));
+ assertEquals(
+ new URL("http://[ff01::114]/a?foo=bar"),
+ SimplePostTool.appendUrlPath(new URL("http://[ff01::114]/?foo=bar"),
"/a"));
+ assertEquals(
+ new URL("http://[ff01::114]/a/b?foo=bar"),
+ SimplePostTool.appendUrlPath(new URL("http://[ff01::114]/a?foo=bar"),
"/b"));
+ assertEquals(
+ new URL("http://[ff01::114]/a/b?foo=bar"),
+ SimplePostTool.appendUrlPath(new URL("http://[ff01::114]/a/?foo=bar"),
"/b"));
+ assertEquals(
+ new URL("http://[ff01::114]/a/b?foo=bar"),
+ SimplePostTool.appendUrlPath(new URL("http://[ff01::114]/a?foo=bar"),
"b"));
+ assertEquals(
+ new URL("http://[ff01::114]/a/b?foo=bar"),
+ SimplePostTool.appendUrlPath(new URL("http://[ff01::114]/a/?foo=bar"),
"b"));
}
@Test