Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit fc01794f authored by Kenny Root's avatar Kenny Root Committed by Jean-Baptiste Queru
Browse files

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)
parent f9d9cf1e
Loading
Loading
Loading
Loading
+6 −4
Original line number Diff line number Diff line
@@ -1028,7 +1028,7 @@ public abstract class Uri implements Parcelable, Comparable<Uri> {
        }

        private String parseHost() {
            String authority = getAuthority();
            String authority = getEncodedAuthority();
            if (authority == null) {
                return null;
            }
@@ -1037,9 +1037,11 @@ public abstract class Uri implements Parcelable, Comparable<Uri> {
            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 @@ public abstract class Uri implements Parcelable, Comparable<Uri> {
        }

        private int parsePort() {
            String authority = getAuthority();
            String authority = getEncodedAuthority();
            if (authority == null) {
                return -1;
            }
@@ -1065,7 +1067,7 @@ public abstract class Uri implements Parcelable, Comparable<Uri> {
                return -1;
            }

            String portString = authority.substring(portSeparator + 1);
            String portString = decode(authority.substring(portSeparator + 1));
            try {
                return Integer.parseInt(portString);
            } catch (NumberFormatException e) {
+5 −0
Original line number Diff line number Diff line
@@ -171,6 +171,11 @@ public class UriTest extends TestCase {
        assertEquals("bob lee", uri.getUserInfo());
        assertEquals("bob%20lee", uri.getEncodedUserInfo());

        uri = Uri.parse("http://bob%40lee%3ajr@local%68ost:4%32");
        assertEquals("bob@lee:jr", uri.getUserInfo());
        assertEquals("localhost", uri.getHost());
        assertEquals(42, uri.getPort());

        uri = Uri.parse("http://localhost");
        assertEquals("localhost", uri.getHost());
        assertEquals(-1, uri.getPort());