Skip to content

Commit

Permalink
Fix URL parse pwd failed when the pwd contains #
Browse files Browse the repository at this point in the history
Signed-off-by: crazyhzm <[email protected]>
  • Loading branch information
CrazyHZM committed Jan 9, 2024
1 parent 1c6374c commit 259d482
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ private 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) {
Expand Down Expand Up @@ -103,7 +103,9 @@ private static URL parseURLBody(String fullURLStr, String decodedBody, Map<Strin
int starIdx = 0, endIdx = decodedBody.length();
// ignore the url content following '#'
int poundIndex = decodedBody.indexOf('#');
if (poundIndex != -1) {
int pwdEndIdx = lastIndexOf(decodedBody, '@', starIdx, endIdx);

if (poundIndex != -1 && poundIndex > pwdEndIdx) {
endIdx = poundIndex;
}

Expand Down Expand Up @@ -136,7 +138,7 @@ private static URL parseURLBody(String fullURLStr, String decodedBody, Map<Strin

String username = null;
String password = null;
int pwdEndIdx = lastIndexOf(decodedBody, '@', starIdx, endIdx);
pwdEndIdx = lastIndexOf(decodedBody, '@', starIdx, endIdx);
if (pwdEndIdx > 0) {
int passwordStartIdx = indexOf(decodedBody, ':', starIdx, pwdEndIdx);
if (passwordStartIdx != -1) { // tolerate incomplete user pwd input, like '1234@'
Expand Down Expand Up @@ -203,8 +205,8 @@ public static Map<String, String> parseParams(String rawParams, boolean encoded)
}

/**
* @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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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://test:test#[email protected]:2181");

errorDecodedCases.add("dubbo:192.168.1.1");
errorDecodedCases.add("://192.168.1.1");
Expand Down

0 comments on commit 259d482

Please sign in to comment.