This is an automated email from the ASF dual-hosted git repository.
kimmking pushed a commit to branch 3.2
in repository https://gitbox.apache.org/repos/asf/dubbo.git
The following commit(s) were added to refs/heads/3.2 by this push:
new df0de49b8c Fix URL parse pwd failed when the pwd contains '#' (#13631)
df0de49b8c is described below
commit df0de49b8c19cae9b9d1d1e4f6e3a37111c6f7ec
Author: huazhongming <[email protected]>
AuthorDate: Sat Jan 13 01:58:58 2024 +0800
Fix URL parse pwd failed when the pwd contains '#' (#13631)
* Fix URL parse pwd failed when the pwd contains #
Signed-off-by: crazyhzm <[email protected]>
* Parse url pwd
Signed-off-by: crazyhzm <[email protected]>
* Fix npe
Signed-off-by: crazyhzm <[email protected]>
* Remove username and pwd from parameter
Signed-off-by: crazyhzm <[email protected]>
* Add ut
Signed-off-by: crazyhzm <[email protected]>
---------
Signed-off-by: crazyhzm <[email protected]>
---
.../java/org/apache/dubbo/common/URLStrParser.java | 21 +++++++++++++++++----
.../org/apache/dubbo/common/URLStrParserTest.java | 1 +
2 files changed, 18 insertions(+), 4 deletions(-)
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 3b1761e2ff..c8ee6337e4 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
@@ -18,12 +18,15 @@ package org.apache.dubbo.common;
import org.apache.dubbo.common.url.component.ServiceConfigURL;
import org.apache.dubbo.common.url.component.URLItemCache;
+import org.apache.dubbo.common.utils.StringUtils;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import static
org.apache.dubbo.common.constants.CommonConstants.DEFAULT_KEY_PREFIX;
+import static org.apache.dubbo.common.constants.CommonConstants.PASSWORD_KEY;
+import static org.apache.dubbo.common.constants.CommonConstants.USERNAME_KEY;
import static org.apache.dubbo.common.utils.StringUtils.EMPTY_STRING;
import static org.apache.dubbo.common.utils.StringUtils.decodeHexByte;
import static org.apache.dubbo.common.utils.Utf8Utils.decodeUtf8;
@@ -42,8 +45,8 @@ public final class URLStrParser {
}
/**
- * @param decodedURLStr : after {@link URL#decode} string
- * decodedURLStr format:
protocol://username:password@host:port/path?k1=v1&k2=v2
+ * @param decodedURLStr : after {@link URL#decode} string decodedURLStr
format:
+ *
protocol://username:password@host:port/path?k1=v1&k2=v2
*
[protocol://][username:password@][host:port]/[path][?k1=v1&k2=v2]
*/
public static URL parseDecodedStr(String decodedURLStr) {
@@ -103,6 +106,7 @@ public final class URLStrParser {
int starIdx = 0, endIdx = decodedBody.length();
// ignore the url content following '#'
int poundIndex = decodedBody.indexOf('#');
+
if (poundIndex != -1) {
endIdx = poundIndex;
}
@@ -147,6 +151,15 @@ public final class URLStrParser {
}
starIdx = pwdEndIdx + 1;
}
+ if (username == null && parameters != null &&
StringUtils.isNotEmpty(parameters.get(USERNAME_KEY))) {
+ username = parameters.get(USERNAME_KEY);
+ parameters.remove(USERNAME_KEY);
+ }
+
+ if (password == null && parameters != null &&
StringUtils.isNotEmpty(parameters.get(PASSWORD_KEY))) {
+ password = parameters.get(PASSWORD_KEY);
+ parameters.remove(PASSWORD_KEY);
+ }
String host = null;
int port = 0;
@@ -203,8 +216,8 @@ public final class URLStrParser {
}
/**
- * @param encodedURLStr : after {@link URL#encode(String)} string
- * encodedURLStr after decode format:
protocol://username:password@host:port/path?k1=v1&k2=v2
+ * @param encodedURLStr : after {@link URL#encode(String)} string
encodedURLStr after decode format:
+ *
protocol://username:password@host:port/path?k1=v1&k2=v2
*
[protocol://][username:password@][host:port]/[path][?k1=v1&k2=v2]
*/
public static URL parseEncodedStr(String encodedURLStr) {
diff --git
a/dubbo-common/src/test/java/org/apache/dubbo/common/URLStrParserTest.java
b/dubbo-common/src/test/java/org/apache/dubbo/common/URLStrParserTest.java
index 4c4c218c35..01364faab9 100644
--- a/dubbo-common/src/test/java/org/apache/dubbo/common/URLStrParserTest.java
+++ b/dubbo-common/src/test/java/org/apache/dubbo/common/URLStrParserTest.java
@@ -48,6 +48,7 @@ class URLStrParserTest {
testCases.add("nacos://192.168.1.1:8848?username=&password=");
testCases.add("dubbo://127.0.0.1?timeout=1234&default.timeout=5678");
testCases.add("dubbo://127.0.0.1?default.timeout=5678");
+
testCases.add("zookeeper://test10.301.216.302:2181?username=t1#&password=t2#");
errorDecodedCases.add("dubbo:192.168.1.1");
errorDecodedCases.add("://192.168.1.1");