You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@dubbo.apache.org by je...@apache.org on 2018/08/07 02:31:59 UTC

[incubator-dubbo] branch master updated: 解析用户定义的URL时,当前太过粗暴,做下优雅调整 (#2077)

This is an automated email from the ASF dual-hosted git repository.

jerrick pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-dubbo.git


The following commit(s) were added to refs/heads/master by this push:
     new 547947a  解析用户定义的URL时,当前太过粗暴,做下优雅调整 (#2077)
547947a is described below

commit 547947a85719ec11eb43ee23773349c5f30ace27
Author: 王虹凯 <wh...@gmail.com>
AuthorDate: Tue Aug 7 10:31:56 2018 +0800

    解析用户定义的URL时,当前太过粗暴,做下优雅调整 (#2077)
---
 .../src/main/java/org/apache/dubbo/common/utils/UrlUtils.java | 11 ++++++++---
 .../test/java/org/apache/dubbo/common/utils/UrlUtilsTest.java |  6 ++++++
 2 files changed, 14 insertions(+), 3 deletions(-)

diff --git a/dubbo-common/src/main/java/org/apache/dubbo/common/utils/UrlUtils.java b/dubbo-common/src/main/java/org/apache/dubbo/common/utils/UrlUtils.java
index 408cac7..a7b3301 100644
--- a/dubbo-common/src/main/java/org/apache/dubbo/common/utils/UrlUtils.java
+++ b/dubbo-common/src/main/java/org/apache/dubbo/common/utils/UrlUtils.java
@@ -27,12 +27,17 @@ import java.util.Set;
 
 public class UrlUtils {
 
+    /**
+     *  in the url string,mark the param begin
+     */
+    private final static String URL_PARAM_STARTING_SYMBOL = "?";
+
     public static URL parseURL(String address, Map<String, String> defaults) {
         if (address == null || address.length() == 0) {
             return null;
         }
         String url;
-        if (address.indexOf("://") >= 0) {
+        if (address.contains("://") || address.contains(URL_PARAM_STARTING_SYMBOL)) {
             url = address;
         } else {
             String[] addresses = Constants.COMMA_SPLIT_PATTERN.split(address);
@@ -45,7 +50,7 @@ public class UrlUtils {
                     }
                     backup.append(addresses[i]);
                 }
-                url += "?" + Constants.BACKUP_KEY + "=" + backup.toString();
+                url += URL_PARAM_STARTING_SYMBOL + Constants.BACKUP_KEY + "=" + backup.toString();
             }
         }
         String defaultProtocol = defaults == null ? null : defaults.get("protocol");
@@ -335,7 +340,7 @@ public class UrlUtils {
             version = service.substring(i + 1);
             service = service.substring(0, i);
         }
-        return URL.valueOf(Constants.EMPTY_PROTOCOL + "://0.0.0.0/" + service + "?"
+        return URL.valueOf(Constants.EMPTY_PROTOCOL + "://0.0.0.0/" + service + URL_PARAM_STARTING_SYMBOL
                 + Constants.CATEGORY_KEY + "=" + category
                 + (group == null ? "" : "&" + Constants.GROUP_KEY + "=" + group)
                 + (version == null ? "" : "&" + Constants.VERSION_KEY + "=" + version));
diff --git a/dubbo-common/src/test/java/org/apache/dubbo/common/utils/UrlUtilsTest.java b/dubbo-common/src/test/java/org/apache/dubbo/common/utils/UrlUtilsTest.java
index bdb2a9d..7141c96 100644
--- a/dubbo-common/src/test/java/org/apache/dubbo/common/utils/UrlUtilsTest.java
+++ b/dubbo-common/src/test/java/org/apache/dubbo/common/utils/UrlUtilsTest.java
@@ -57,6 +57,12 @@ public class UrlUtilsTest {
     }
 
     @Test
+    public void testParseURLWithSpecial() {
+        String address = "127.0.0.1:2181?backup=127.0.0.1:2182,127.0.0.1:2183";
+        assertEquals("dubbo://" + address,UrlUtils.parseURL(address, null).toString());
+    }
+
+    @Test
     public void testDefaultUrl() {
         String address = "127.0.0.1";
         URL url = UrlUtils.parseURL(address, null);