diff --git a/dubbo-common/src/main/java/org/apache/dubbo/common/URLStrParser.java b/dubbo-common/src/main/java/org/apache/dubbo/common/URLStrParser.java index 592cdc5e614..329b0b669c1 100644 --- a/dubbo-common/src/main/java/org/apache/dubbo/common/URLStrParser.java +++ b/dubbo-common/src/main/java/org/apache/dubbo/common/URLStrParser.java @@ -95,6 +95,12 @@ private static Map parseDecodedParams(String str, int from) { */ private static URL parseURLBody(String fullURLStr, String decodedBody, Map parameters) { int starIdx = 0, endIdx = decodedBody.length(); + // ignore the url content following '#' + int poundIndex = decodedBody.indexOf('#'); + if (poundIndex != -1) { + endIdx = poundIndex; + } + String protocol = null; int protoEndIdx = decodedBody.indexOf("://"); if (protoEndIdx >= 0) { @@ -118,7 +124,7 @@ private static URL parseURLBody(String fullURLStr, String decodedBody, Map= 0) { - path = decodedBody.substring(pathStartIdx + 1); + path = decodedBody.substring(pathStartIdx + 1, endIdx); endIdx = pathStartIdx; } diff --git a/dubbo-common/src/test/java/org/apache/dubbo/common/URLTest.java b/dubbo-common/src/test/java/org/apache/dubbo/common/URLTest.java index d5381f54269..922a3487d4c 100644 --- a/dubbo-common/src/test/java/org/apache/dubbo/common/URLTest.java +++ b/dubbo-common/src/test/java/org/apache/dubbo/common/URLTest.java @@ -17,8 +17,8 @@ package org.apache.dubbo.common; import org.apache.dubbo.common.utils.CollectionUtils; - import org.apache.dubbo.common.utils.StringUtils; + import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; @@ -33,11 +33,11 @@ import static org.hamcrest.CoreMatchers.equalTo; import static org.hamcrest.MatcherAssert.assertThat; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertNotEquals; import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertSame; import static org.junit.jupiter.api.Assertions.assertTrue; -import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.fail; public class URLTest { @@ -339,6 +339,34 @@ public void test_valueOf_Exception_noProtocol() throws Exception { } } + @Test + public void test_ignore_pond() { + URL url = URL.valueOf("dubbo://admin:hello1234@10.20.130.230:20880/context/path#index?version=1.0.0&id=org.apache.dubbo.config.RegistryConfig#0"); + URL urlFromDecoder = URLStrParser.parseDecodedStr("dubbo://admin:hello1234@10.20.130.230:20880/context/path#index?version=1.0.0&id=org.apache.dubbo.config.RegistryConfig#0"); + + assertURLStrDecoder(url); + + assertEquals("dubbo", url.getProtocol()); + assertEquals("admin", url.getUsername()); + assertEquals("hello1234", url.getPassword()); + assertEquals("10.20.130.230", url.getHost()); + assertEquals("10.20.130.230:20880", url.getAddress()); + assertEquals(20880, url.getPort()); + assertEquals("context/path", url.getPath()); + assertEquals(2, url.getParameters().size()); + assertEquals("org.apache.dubbo.config.RegistryConfig#0", url.getParameter("id")); + + assertEquals("dubbo", urlFromDecoder.getProtocol()); + assertEquals("admin", urlFromDecoder.getUsername()); + assertEquals("hello1234", urlFromDecoder.getPassword()); + assertEquals("10.20.130.230", urlFromDecoder.getHost()); + assertEquals("10.20.130.230:20880", urlFromDecoder.getAddress()); + assertEquals(20880, urlFromDecoder.getPort()); + assertEquals("context/path", urlFromDecoder.getPath()); + assertEquals(2, urlFromDecoder.getParameters().size()); + assertEquals("org.apache.dubbo.config.RegistryConfig#0", urlFromDecoder.getParameter("id")); + } + @Test public void test_getAddress() throws Exception { URL url1 = URL.valueOf("dubbo://admin:hello1234@10.20.130.230:20880/context/path?version=1.0.0&application=morgan");