From 582deec1f3c8f51b431bb2ad685c9217ffd9eaad Mon Sep 17 00:00:00 2001 From: Steve Block Date: Mon, 6 Sep 2010 16:16:39 +0100 Subject: [PATCH] Fixes a problem with URL parsing when the host ends with a dot The WebAddress class provides a lenient parser for URLs. Currently, it identifies the host portion with the regex []+(\.[]+)* where is the set of characters valid for the host name. This pattern excludes the case where the host ends with a dot, which is valid possibility. As a result, any trailing dot is pushed into the path component. Since we add a leading slash to the path if one is missing, the result is a path that begins with '/.'. This fix changes the host regex to []+[\.]* which allows trailing dots and fixes the problem. Bug: 2337042 Change-Id: I310512531787e0f742988f5d815ad944fd39e059 --- core/java/android/net/WebAddress.java | 2 +- .../coretests/src/android/net/UriTest.java | 7 +++++ .../src/android/net/WebAddressTest.java | 31 +++++++++++++++++++ 3 files changed, 39 insertions(+), 1 deletion(-) create mode 100644 core/tests/coretests/src/android/net/WebAddressTest.java diff --git a/core/java/android/net/WebAddress.java b/core/java/android/net/WebAddress.java index 4101ab489308d..9c4d6e882b595 100644 --- a/core/java/android/net/WebAddress.java +++ b/core/java/android/net/WebAddress.java @@ -56,7 +56,7 @@ public class WebAddress { static Pattern sAddressPattern = Pattern.compile( /* scheme */ "(?:(http|https|file)\\:\\/\\/)?" + /* authority */ "(?:([-A-Za-z0-9$_.+!*'(),;?&=]+(?:\\:[-A-Za-z0-9$_.+!*'(),;?&=]+)?)@)?" + - /* host */ "([-" + GOOD_IRI_CHAR + "%_]+(?:\\.[-" + GOOD_IRI_CHAR + "%_]+)*|\\[[0-9a-fA-F:\\.]+\\])?" + + /* host */ "([" + GOOD_IRI_CHAR + "%_-][" + GOOD_IRI_CHAR + "%_\\.-]*|\\[[0-9a-fA-F:\\.]+\\])?" + /* port */ "(?:\\:([0-9]*))?" + /* path */ "(\\/?[^#]*)?" + /* anchor */ ".*", Pattern.CASE_INSENSITIVE); diff --git a/core/tests/coretests/src/android/net/UriTest.java b/core/tests/coretests/src/android/net/UriTest.java index fe608b5f31e4b..60c3c76c01aab 100644 --- a/core/tests/coretests/src/android/net/UriTest.java +++ b/core/tests/coretests/src/android/net/UriTest.java @@ -285,6 +285,13 @@ public class UriTest extends TestCase { assertEquals("d", uri.getQueryParameter("c")); } + @SmallTest + public void testHostWithTrailingDot() { + Uri uri = Uri.parse("http://google.com./b/c/g"); + assertEquals("google.com.", uri.getHost()); + assertEquals("/b/c/g", uri.getPath()); + } + @SmallTest public void testSchemeOnly() { Uri uri = Uri.parse("empty:"); diff --git a/core/tests/coretests/src/android/net/WebAddressTest.java b/core/tests/coretests/src/android/net/WebAddressTest.java new file mode 100644 index 0000000000000..7ca1e6246618b --- /dev/null +++ b/core/tests/coretests/src/android/net/WebAddressTest.java @@ -0,0 +1,31 @@ +/* + * Copyright (C) 2010 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package android.net; + +import android.net.WebAddress; +import android.test.suitebuilder.annotation.SmallTest; +import junit.framework.TestCase; + +public class WebAddressTest extends TestCase { + + @SmallTest + public void testHostWithTrailingDot() { + WebAddress webAddress = new WebAddress("http://google.com./b/c/g"); + assertEquals("google.com.", webAddress.mHost); + assertEquals("/b/c/g", webAddress.mPath); + } +}