Skip to content

armgit / solr

Public Imported from GitHub
0

Apache Solr open-source search software

SOLR-17427: Fix url building in cli (#2678)

Houston Putmane35207268b8b
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 dba8357aac0..76c6aa6f60f 100644
--- a/solr/core/src/java/org/apache/solr/cli/ApiTool.java
+++ b/solr/core/src/java/org/apache/solr/cli/ApiTool.java
@@ -125,7 +125,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 e46b2a38d99..9d2296d63bb 100644
--- a/solr/core/src/java/org/apache/solr/cli/PostTool.java
+++ b/solr/core/src/java/org/apache/solr/cli/PostTool.java
@@ -379,37 +379,32 @@ public class PostTool extends ToolBase {
private void doWebMode() {
reset();
int numPagesPosted;
- try {
- if (type != null) {
- throw new IllegalArgumentException(
- "Specifying content-type with \"--mode=web\" is not supported");
- }
+ if (type != null) {
+ throw new IllegalArgumentException(
+ "Specifying content-type with \"--mode=web\" is not supported");
+ }
- // Set Extracting handler as default
- solrUpdateUrl = appendUrlPath(solrUpdateUrl, "/extract");
+ // Set Extracting handler as default
+ solrUpdateUrl = appendUrlPath(solrUpdateUrl, "/extract");
- info("Posting web pages to Solr url " + solrUpdateUrl);
- auto = true;
- info(
- "Entering auto mode. Indexing pages with content-types corresponding to file endings "
- + fileTypes);
- if (recursive > 0) {
- if (recursive > MAX_WEB_DEPTH) {
- recursive = MAX_WEB_DEPTH;
- warn("Too large recursion depth for web mode, limiting to " + MAX_WEB_DEPTH + "...");
- }
- if (delay < DEFAULT_WEB_DELAY) {
- warn(
- "Never crawl an external web site faster than every 10 seconds, your IP will probably be blocked");
- }
- info("Entering recursive mode, depth=" + recursive + ", delay=" + delay + "s");
+ info("Posting web pages to Solr url " + solrUpdateUrl);
+ auto = true;
+ info(
+ "Entering auto mode. Indexing pages with content-types corresponding to file endings "
+ + fileTypes);
+ if (recursive > 0) {
+ if (recursive > MAX_WEB_DEPTH) {
+ recursive = MAX_WEB_DEPTH;
+ warn("Too large recursion depth for web mode, limiting to " + MAX_WEB_DEPTH + "...");
}
- numPagesPosted = postWebPages(args, 0, out);
- info(numPagesPosted + " web pages indexed.");
-
- } catch (URISyntaxException e) {
- warn("Wrong URL trying to append /extract to " + solrUpdateUrl);
+ if (delay < DEFAULT_WEB_DELAY) {
+ warn(
+ "Never crawl an external web site faster than every 10 seconds, your IP will probably be blocked");
+ }
+ info("Entering recursive mode, depth=" + recursive + ", delay=" + delay + "s");
}
+ numPagesPosted = postWebPages(args, 0, out);
+ info(numPagesPosted + " web pages indexed.");
}
private void doStdinMode() {
@@ -533,7 +528,7 @@ public class PostTool extends ToolBase {
postFile(srcFile, out, type);
Thread.sleep(delay * 1000L);
filesPosted++;
- } catch (InterruptedException | URISyntaxException e) {
+ } catch (InterruptedException | MalformedURLException | URISyntaxException e) {
throw new RuntimeException(e);
}
}
@@ -699,13 +694,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
@@ -715,10 +711,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);
@@ -806,7 +804,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 URISyntaxException {
+ public void postFile(File file, OutputStream output, String type)
+ throws MalformedURLException, URISyntaxException {
InputStream is = null;
URI uri = solrUpdateUrl;
@@ -884,9 +883,20 @@ public class PostTool extends ToolBase {
* @param append the path to append
* @return the final URL version
*/
- protected static URI appendUrlPath(URI uri, String append) throws URISyntaxException {
- var newPath = uri.getPath() + append;
- return new URI(uri.getScheme(), uri.getAuthority(), newPath, uri.getQuery(), uri.getFragment());
+ protected static URI appendUrlPath(URI uri, String append) {
+ if (append == null || append.isEmpty()) {
+ return uri;
+ }
+ if (append.startsWith("/")) {
+ append = append.substring(1);
+ }
+ if (uri.getQuery() != null && !uri.getQuery().isEmpty()) {
+ append += "?" + uri.getQuery();
+ }
+ if (!uri.getPath().endsWith("/")) {
+ append = uri.getPath() + "/" + append;
+ }
+ return uri.resolve(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 05488e7d8e3..e02f81a0abc 100755
--- a/solr/core/src/java/org/apache/solr/cli/SolrCLI.java
+++ b/solr/core/src/java/org/apache/solr/cli/SolrCLI.java
@@ -636,8 +636,11 @@ public class SolrCLI implements CLIO {
*/
public static String normalizeSolrUrl(String solrUrl, boolean logUrlFormatWarning) {
if (solrUrl != null) {
- if (solrUrl.contains("/solr")) { //
- String newSolrUrl = solrUrl.substring(0, solrUrl.indexOf("/solr"));
+ URI uri = URI.create(solrUrl);
+ String urlPath = uri.getRawPath();
+ if (urlPath != null && urlPath.contains("/solr")) {
+ String newSolrUrl =
+ uri.resolve(urlPath.substring(0, urlPath.lastIndexOf("/solr") + 1)).toString();
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 ["
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 db04f60f0ef..bec194ea808 100644
--- a/solr/core/src/test/org/apache/solr/cli/ApiToolTest.java
+++ b/solr/core/src/test/org/apache/solr/cli/ApiToolTest.java
@@ -99,4 +99,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 6f7d167e106..0f8b9f1559e 100644
--- a/solr/core/src/test/org/apache/solr/cli/PostToolTest.java
+++ b/solr/core/src/test/org/apache/solr/cli/PostToolTest.java
@@ -211,30 +211,25 @@ 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(URI.create("http://[ff01::114]/").toURL(), "/index.html"));
+ PostTool.computeFullUrl(URI.create("http://[ff01::114]/").toURL(), "/index.html"));
assertEquals(
"http://[ff01::114]/index.html",
- webPostTool.computeFullUrl(
- URI.create("http://[ff01::114]/foo/bar/").toURL(), "/index.html"));
+ PostTool.computeFullUrl(URI.create("http://[ff01::114]/foo/bar/").toURL(), "/index.html"));
assertEquals(
"http://[ff01::114]/fil.html",
- webPostTool.computeFullUrl(
+ PostTool.computeFullUrl(
URI.create("http://[ff01::114]/foo.htm?baz#hello").toURL(), "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(URI.create("http://[ff01::114]/").toURL(), "fil.jpg"));
+ assertNull(PostTool.computeFullUrl(URI.create("http://[ff01::114]/").toURL(), "fil.jpg"));
assertNull(
- webPostTool.computeFullUrl(
- URI.create("http://[ff01::114]/").toURL(), "mailto:[email protected]"));
+ PostTool.computeFullUrl(URI.create("http://[ff01::114]/").toURL(), "mailto:[email protected]"));
assertNull(
- webPostTool.computeFullUrl(URI.create("http://[ff01::114]/").toURL(), "ftp://server/file"));
+ PostTool.computeFullUrl(URI.create("http://[ff01::114]/").toURL(), "ftp://server/file"));
}
@Test
@@ -265,6 +260,21 @@ public class PostToolTest extends SolrCloudTestCase {
assertEquals(
URI.create("http://[ff01::114]/a?foo=bar"),
PostTool.appendUrlPath(URI.create("http://[ff01::114]?foo=bar"), "/a"));
+ assertEquals(
+ URI.create("http://[ff01::114]/a?foo=bar"),
+ PostTool.appendUrlPath(URI.create("http://[ff01::114]/?foo=bar"), "/a"));
+ assertEquals(
+ URI.create("http://[ff01::114]/a/b?foo=bar"),
+ PostTool.appendUrlPath(URI.create("http://[ff01::114]/a?foo=bar"), "/b"));
+ assertEquals(
+ URI.create("http://[ff01::114]/a/b?foo=bar"),
+ PostTool.appendUrlPath(URI.create("http://[ff01::114]/a/?foo=bar"), "/b"));
+ assertEquals(
+ URI.create("http://[ff01::114]/a/b?foo=bar"),
+ PostTool.appendUrlPath(URI.create("http://[ff01::114]/a?foo=bar"), "b"));
+ assertEquals(
+ URI.create("http://[ff01::114]/a/b?foo=bar"),
+ PostTool.appendUrlPath(URI.create("http://[ff01::114]/a/?foo=bar"), "b"));
}
@Test