Make Uri.parseUserPart, parseHost, and parsePort symmetric
Currently parseUserPart uses the encoded authority to split the URI
into user and non-user parts, but the parseHost and parsePort uses
the decoded URI to split the URI into non-host, host, and port parts.
This gives unexpected results when %40 ('@') and %3a (':') is used
in a URI:
Uri test = Uri.parse("http://bob%40lee%3ajr@example.com:42/");
test.getUserInfo() => "bob@lee:jr"
test.getHost() => "lee:jr@example.com" (should be "example.com")
test.getPort() => -1 (should be 42)
diff --git a/core/java/android/net/Uri.java b/core/java/android/net/Uri.java
index 298be3b..9a1b65d 100644
--- a/core/java/android/net/Uri.java
+++ b/core/java/android/net/Uri.java
@@ -1028,7 +1028,7 @@
}
private String parseHost() {
- String authority = getAuthority();
+ String authority = getEncodedAuthority();
if (authority == null) {
return null;
}
@@ -1037,9 +1037,11 @@
int userInfoSeparator = authority.indexOf('@');
int portSeparator = authority.indexOf(':', userInfoSeparator);
- return portSeparator == NOT_FOUND
+ String encodedHost = portSeparator == NOT_FOUND
? authority.substring(userInfoSeparator + 1)
: authority.substring(userInfoSeparator + 1, portSeparator);
+
+ return decode(encodedHost);
}
private volatile int port = NOT_CALCULATED;
@@ -1051,7 +1053,7 @@
}
private int parsePort() {
- String authority = getAuthority();
+ String authority = getEncodedAuthority();
if (authority == null) {
return -1;
}
@@ -1065,7 +1067,7 @@
return -1;
}
- String portString = authority.substring(portSeparator + 1);
+ String portString = decode(authority.substring(portSeparator + 1));
try {
return Integer.parseInt(portString);
} catch (NumberFormatException e) {