You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@dubbo.apache.org by GitBox <gi...@apache.org> on 2018/08/13 02:39:53 UTC
[GitHub] lllichen closed pull request #2230: optimize the way of parsing
url(string)
lllichen closed pull request #2230: optimize the way of parsing url(string)
URL: https://github.com/apache/incubator-dubbo/pull/2230
This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:
As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):
diff --git a/dubbo-common/src/main/java/org/apache/dubbo/common/URL.java b/dubbo-common/src/main/java/org/apache/dubbo/common/URL.java
index a83ca91e22..90bdbcc3eb 100644
--- a/dubbo-common/src/main/java/org/apache/dubbo/common/URL.java
+++ b/dubbo-common/src/main/java/org/apache/dubbo/common/URL.java
@@ -169,87 +169,129 @@ public URL(String protocol, String username, String password, String host, int p
this.parameters = Collections.unmodifiableMap(parameters);
}
- /**
- * Parse url string
- *
- * @param url URL string
- * @return URL instance
- * @see URL
- */
- public static URL valueOf(String url) {
- if (url == null || (url = url.trim()).length() == 0) {
- throw new IllegalArgumentException("url == null");
- }
- String protocol = null;
- String username = null;
- String password = null;
- String host = null;
- int port = 0;
- String path = null;
- Map<String, String> parameters = null;
- int i = url.indexOf("?"); // seperator between body and parameters
- if (i >= 0) {
- String[] parts = url.substring(i + 1).split("\\&");
- parameters = new HashMap<String, String>();
- for (String part : parts) {
- part = part.trim();
- if (part.length() > 0) {
- int j = part.indexOf('=');
- if (j >= 0) {
- parameters.put(part.substring(0, j), part.substring(j + 1));
- } else {
- parameters.put(part, part);
+
+ private static class URLParser {
+
+ private String url = null;
+ private String protocol = null;
+ private String username = null;
+ private String password = null;
+ private String host = null;
+ private int port = 0;
+ private String path = null;
+ private Map<String, String> parameters = null;
+
+ public URLParser(String url) {
+ this.url = url;
+ this.parse();
+ }
+
+ private void parseParameters() {
+ int i = url.indexOf( "?" ); // separator between body and parameters
+ if (i >= 0) {
+ String[] parts = url.substring( i + 1 ).split( "\\&" );
+ Map<String, String> parameters = new HashMap<String, String>();
+ for (String part : parts) {
+ part = part.trim();
+ if (part.length() > 0) {
+ int j = part.indexOf( '=' );
+ if (j >= 0) {
+ parameters.put( part.substring( 0, j ), part.substring( j + 1 ) );
+ } else {
+ parameters.put( part, part );
+ }
}
}
+ url = url.substring( 0, i );
+ this.parameters = parameters;
}
- url = url.substring(0, i);
}
- i = url.indexOf("://");
- if (i >= 0) {
- if (i == 0) throw new IllegalStateException("url missing protocol: \"" + url + "\"");
- protocol = url.substring(0, i);
- url = url.substring(i + 3);
- } else {
- // case: file:/path/to/file.txt
- i = url.indexOf(":/");
+
+ private void parseProtocol() {
+ int i = url.indexOf( "://" );
if (i >= 0) {
- if (i == 0) throw new IllegalStateException("url missing protocol: \"" + url + "\"");
- protocol = url.substring(0, i);
- url = url.substring(i + 1);
+ if (i == 0) throw new IllegalStateException( "url missing protocol: \"" + url + "\"" );
+ protocol = url.substring( 0, i );
+ url = url.substring( i + 3 );
+ } else {
+ // case: file:/path/to/file.txt
+ i = url.indexOf( ":/" );
+ if (i >= 0) {
+ if (i == 0) throw new IllegalStateException( "url missing protocol: \"" + url + "\"" );
+ protocol = url.substring( 0, i );
+ url = url.substring( i + 1 );
+ }
}
}
- i = url.indexOf("/");
- if (i >= 0) {
- path = url.substring(i + 1);
- url = url.substring(0, i);
- }
- i = url.lastIndexOf("@");
- if (i >= 0) {
- username = url.substring(0, i);
- int j = username.indexOf(":");
- if (j >= 0) {
- password = username.substring(j + 1);
- username = username.substring(0, j);
+ private void parseUsernameAndPassword() {
+ int i = url.indexOf( "/" );
+ if (i >= 0) {
+ path = url.substring( i + 1 );
+ url = url.substring( 0, i );
}
- url = url.substring(i + 1);
- }
- i = url.lastIndexOf(":");
- if (i >= 0 && i < url.length() - 1) {
- if (url.lastIndexOf("%") > i) {
- // ipv6 address with scope id
- // e.g. fe80:0:0:0:894:aeec:f37d:23e1%en0
- // see https://howdoesinternetwork.com/2013/ipv6-zone-id
- // ignore
- } else {
- port = Integer.parseInt(url.substring(i+1));
- url = url.substring(0, i);
+ i = url.lastIndexOf( "@" );
+ if (i >= 0) {
+ username = url.substring( 0, i );
+ int j = username.indexOf( ":" );
+ if (j >= 0) {
+ password = username.substring( j + 1 );
+ username = username.substring( 0, j );
+ }
+ url = url.substring( i + 1 );
+ }
+ }
+
+ private void parsePort() {
+ int i = url.lastIndexOf( ":" );
+ if (i >= 0 && i < url.length() - 1) {
+ if (url.lastIndexOf( "%" ) > i) {
+ // ipv6 address with scope id
+ // e.g. fe80:0:0:0:894:aeec:f37d:23e1%en0
+ // see https://howdoesinternetwork.com/2013/ipv6-zone-id
+ // ignore
+ } else {
+ port = Integer.parseInt( url.substring( i + 1 ) );
+ url = url.substring( 0, i );
+ }
}
}
- if (url.length() > 0) host = url;
- return new URL(protocol, username, password, host, port, path, parameters);
+
+ private void parseHost() {
+ if (url.length() > 0)
+ host = url;
+ }
+
+ private void parse() {
+ this.parseParameters();
+ this.parseProtocol();
+ this.parseUsernameAndPassword();
+ this.parsePort();
+ this.parseHost();
+ }
+
+ private URL build() {
+ return new URL( protocol, username, password, host, port, path, parameters );
+ }
+ }
+
+
+
+ /**
+ * Parse url string
+ *
+ * @param url URL string
+ * @return URL instance
+ * @see URL
+ */
+ public static URL valueOf(String url) {
+ if (url == null || (url = url.trim()).length() == 0) {
+ throw new IllegalArgumentException("url is empty!");
+ }
+ return new URLParser(url).build();
}
+
public static String encode(String value) {
if (value == null || value.length() == 0) {
return "";
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
With regards,
Apache Git Services
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org
For additional commands, e-mail: notifications-help@dubbo.apache.org