Fix URL parse pwd failed when the pwd contains '#' (#13631)

* Fix URL parse pwd failed when the pwd contains #

Signed-off-by: crazyhzm <crazyhzm@gmail.com>

* Parse url pwd

Signed-off-by: crazyhzm <crazyhzm@gmail.com>

* Fix npe

Signed-off-by: crazyhzm <crazyhzm@gmail.com>

* Remove username and pwd from parameter

Signed-off-by: crazyhzm <crazyhzm@gmail.com>

* Add ut

Signed-off-by: crazyhzm <crazyhzm@gmail.com>

---------

Signed-off-by: crazyhzm <crazyhzm@gmail.com>
This commit is contained in:
huazhongming 2024-01-13 01:58:58 +08:00 committed by GitHub
parent 1d9535518e
commit df0de49b8c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 4 deletions

View File

@ -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) {

View File

@ -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");