The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2006 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package android.net; |
| 18 | |
Dianne Hackborn | 2269d157 | 2010-02-24 19:54:22 -0800 | [diff] [blame] | 19 | import static android.util.Patterns.GOOD_IRI_CHAR; |
Shimeng (Simon) Wang | 51c02db | 2010-02-11 14:07:44 -0800 | [diff] [blame] | 20 | |
Aurimas Liutikas | 4d1699d | 2019-08-28 13:01:05 -0700 | [diff] [blame] | 21 | import android.annotation.NonNull; |
Ignacio Solla | 451e338 | 2014-11-10 10:35:54 +0000 | [diff] [blame] | 22 | import android.annotation.SystemApi; |
Artur Satayev | 2695800 | 2019-12-10 17:47:52 +0000 | [diff] [blame] | 23 | import android.compat.annotation.UnsupportedAppUsage; |
Mathew Inwood | 31755f9 | 2018-12-20 13:53:36 +0000 | [diff] [blame] | 24 | import android.os.Build; |
Ignacio Solla | 451e338 | 2014-11-10 10:35:54 +0000 | [diff] [blame] | 25 | |
Elliott Hughes | cb64d43 | 2013-08-02 10:00:44 -0700 | [diff] [blame] | 26 | import java.util.Locale; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 27 | import java.util.regex.Matcher; |
| 28 | import java.util.regex.Pattern; |
| 29 | |
| 30 | /** |
| 31 | * {@hide} |
| 32 | * |
| 33 | * Web Address Parser |
| 34 | * |
| 35 | * This is called WebAddress, rather than URL or URI, because it |
| 36 | * attempts to parse the stuff that a user will actually type into a |
| 37 | * browser address widget. |
| 38 | * |
| 39 | * Unlike java.net.uri, this parser will not choke on URIs missing |
| 40 | * schemes. It will only throw a ParseException if the input is |
| 41 | * really hosed. |
| 42 | * |
| 43 | * If given an https scheme but no port, fills in port |
| 44 | * |
| 45 | */ |
Ignacio Solla | 451e338 | 2014-11-10 10:35:54 +0000 | [diff] [blame] | 46 | // TODO(igsolla): remove WebAddress from the system SDK once the WebView apk does not |
| 47 | // longer need to be binary compatible with the API 21 version of the framework. |
| 48 | @SystemApi |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 49 | public class WebAddress { |
| 50 | |
Mathew Inwood | 53f089f | 2018-08-08 14:44:44 +0100 | [diff] [blame] | 51 | @UnsupportedAppUsage |
Bjorn Bringert | eb8be97 | 2010-10-12 16:24:55 +0100 | [diff] [blame] | 52 | private String mScheme; |
Mathew Inwood | 53f089f | 2018-08-08 14:44:44 +0100 | [diff] [blame] | 53 | @UnsupportedAppUsage |
Bjorn Bringert | eb8be97 | 2010-10-12 16:24:55 +0100 | [diff] [blame] | 54 | private String mHost; |
Mathew Inwood | 31755f9 | 2018-12-20 13:53:36 +0000 | [diff] [blame] | 55 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023) |
Bjorn Bringert | eb8be97 | 2010-10-12 16:24:55 +0100 | [diff] [blame] | 56 | private int mPort; |
Mathew Inwood | 53f089f | 2018-08-08 14:44:44 +0100 | [diff] [blame] | 57 | @UnsupportedAppUsage |
Bjorn Bringert | eb8be97 | 2010-10-12 16:24:55 +0100 | [diff] [blame] | 58 | private String mPath; |
| 59 | private String mAuthInfo; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 60 | |
| 61 | static final int MATCH_GROUP_SCHEME = 1; |
| 62 | static final int MATCH_GROUP_AUTHORITY = 2; |
| 63 | static final int MATCH_GROUP_HOST = 3; |
| 64 | static final int MATCH_GROUP_PORT = 4; |
| 65 | static final int MATCH_GROUP_PATH = 5; |
| 66 | |
| 67 | static Pattern sAddressPattern = Pattern.compile( |
Patrick Scott | 9253446 | 2010-03-31 13:35:24 -0400 | [diff] [blame] | 68 | /* scheme */ "(?:(http|https|file)\\:\\/\\/)?" + |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 69 | /* authority */ "(?:([-A-Za-z0-9$_.+!*'(),;?&=]+(?:\\:[-A-Za-z0-9$_.+!*'(),;?&=]+)?)@)?" + |
Steve Block | 582deec | 2010-09-06 16:16:39 +0100 | [diff] [blame] | 70 | /* host */ "([" + GOOD_IRI_CHAR + "%_-][" + GOOD_IRI_CHAR + "%_\\.-]*|\\[[0-9a-fA-F:\\.]+\\])?" + |
Steve Block | 2257440 | 2010-03-08 17:52:02 +0000 | [diff] [blame] | 71 | /* port */ "(?:\\:([0-9]*))?" + |
Patrick Scott | fa4039e4 | 2010-02-16 16:15:55 -0500 | [diff] [blame] | 72 | /* path */ "(\\/?[^#]*)?" + |
Patrick Scott | 9253446 | 2010-03-31 13:35:24 -0400 | [diff] [blame] | 73 | /* anchor */ ".*", Pattern.CASE_INSENSITIVE); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 74 | |
| 75 | /** parses given uriString. */ |
| 76 | public WebAddress(String address) throws ParseException { |
| 77 | if (address == null) { |
| 78 | throw new NullPointerException(); |
| 79 | } |
| 80 | |
| 81 | // android.util.Log.d(LOGTAG, "WebAddress: " + address); |
| 82 | |
| 83 | mScheme = ""; |
| 84 | mHost = ""; |
| 85 | mPort = -1; |
| 86 | mPath = "/"; |
| 87 | mAuthInfo = ""; |
| 88 | |
| 89 | Matcher m = sAddressPattern.matcher(address); |
| 90 | String t; |
| 91 | if (m.matches()) { |
| 92 | t = m.group(MATCH_GROUP_SCHEME); |
Elliott Hughes | cb64d43 | 2013-08-02 10:00:44 -0700 | [diff] [blame] | 93 | if (t != null) mScheme = t.toLowerCase(Locale.ROOT); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 94 | t = m.group(MATCH_GROUP_AUTHORITY); |
| 95 | if (t != null) mAuthInfo = t; |
| 96 | t = m.group(MATCH_GROUP_HOST); |
| 97 | if (t != null) mHost = t; |
| 98 | t = m.group(MATCH_GROUP_PORT); |
Steve Block | 2257440 | 2010-03-08 17:52:02 +0000 | [diff] [blame] | 99 | if (t != null && t.length() > 0) { |
| 100 | // The ':' character is not returned by the regex. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 101 | try { |
| 102 | mPort = Integer.parseInt(t); |
| 103 | } catch (NumberFormatException ex) { |
| 104 | throw new ParseException("Bad port"); |
| 105 | } |
| 106 | } |
| 107 | t = m.group(MATCH_GROUP_PATH); |
| 108 | if (t != null && t.length() > 0) { |
| 109 | /* handle busted myspace frontpage redirect with |
| 110 | missing initial "/" */ |
| 111 | if (t.charAt(0) == '/') { |
| 112 | mPath = t; |
| 113 | } else { |
| 114 | mPath = "/" + t; |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | } else { |
| 119 | // nothing found... outa here |
| 120 | throw new ParseException("Bad address"); |
| 121 | } |
| 122 | |
| 123 | /* Get port from scheme or scheme from port, if necessary and |
| 124 | possible */ |
| 125 | if (mPort == 443 && mScheme.equals("")) { |
| 126 | mScheme = "https"; |
| 127 | } else if (mPort == -1) { |
| 128 | if (mScheme.equals("https")) |
| 129 | mPort = 443; |
| 130 | else |
| 131 | mPort = 80; // default |
| 132 | } |
| 133 | if (mScheme.equals("")) mScheme = "http"; |
| 134 | } |
| 135 | |
Aurimas Liutikas | 4d1699d | 2019-08-28 13:01:05 -0700 | [diff] [blame] | 136 | @NonNull |
Bjorn Bringert | eb8be97 | 2010-10-12 16:24:55 +0100 | [diff] [blame] | 137 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 138 | public String toString() { |
| 139 | String port = ""; |
| 140 | if ((mPort != 443 && mScheme.equals("https")) || |
| 141 | (mPort != 80 && mScheme.equals("http"))) { |
| 142 | port = ":" + Integer.toString(mPort); |
| 143 | } |
| 144 | String authInfo = ""; |
| 145 | if (mAuthInfo.length() > 0) { |
| 146 | authInfo = mAuthInfo + "@"; |
| 147 | } |
| 148 | |
| 149 | return mScheme + "://" + authInfo + mHost + port + mPath; |
| 150 | } |
Bjorn Bringert | eb8be97 | 2010-10-12 16:24:55 +0100 | [diff] [blame] | 151 | |
Ignacio Solla | 451e338 | 2014-11-10 10:35:54 +0000 | [diff] [blame] | 152 | /** {@hide} */ |
Bjorn Bringert | eb8be97 | 2010-10-12 16:24:55 +0100 | [diff] [blame] | 153 | public void setScheme(String scheme) { |
| 154 | mScheme = scheme; |
| 155 | } |
| 156 | |
Ignacio Solla | 451e338 | 2014-11-10 10:35:54 +0000 | [diff] [blame] | 157 | /** {@hide} */ |
Mathew Inwood | 53f089f | 2018-08-08 14:44:44 +0100 | [diff] [blame] | 158 | @UnsupportedAppUsage |
Bjorn Bringert | eb8be97 | 2010-10-12 16:24:55 +0100 | [diff] [blame] | 159 | public String getScheme() { |
| 160 | return mScheme; |
| 161 | } |
| 162 | |
Ignacio Solla | 451e338 | 2014-11-10 10:35:54 +0000 | [diff] [blame] | 163 | /** {@hide} */ |
Mathew Inwood | 53f089f | 2018-08-08 14:44:44 +0100 | [diff] [blame] | 164 | @UnsupportedAppUsage |
Bjorn Bringert | eb8be97 | 2010-10-12 16:24:55 +0100 | [diff] [blame] | 165 | public void setHost(String host) { |
| 166 | mHost = host; |
| 167 | } |
| 168 | |
Ignacio Solla | 451e338 | 2014-11-10 10:35:54 +0000 | [diff] [blame] | 169 | /** {@hide} */ |
Mathew Inwood | 53f089f | 2018-08-08 14:44:44 +0100 | [diff] [blame] | 170 | @UnsupportedAppUsage |
Bjorn Bringert | eb8be97 | 2010-10-12 16:24:55 +0100 | [diff] [blame] | 171 | public String getHost() { |
| 172 | return mHost; |
| 173 | } |
| 174 | |
Ignacio Solla | 451e338 | 2014-11-10 10:35:54 +0000 | [diff] [blame] | 175 | /** {@hide} */ |
Bjorn Bringert | eb8be97 | 2010-10-12 16:24:55 +0100 | [diff] [blame] | 176 | public void setPort(int port) { |
| 177 | mPort = port; |
| 178 | } |
| 179 | |
Ignacio Solla | 451e338 | 2014-11-10 10:35:54 +0000 | [diff] [blame] | 180 | /** {@hide} */ |
Mathew Inwood | 53f089f | 2018-08-08 14:44:44 +0100 | [diff] [blame] | 181 | @UnsupportedAppUsage |
Bjorn Bringert | eb8be97 | 2010-10-12 16:24:55 +0100 | [diff] [blame] | 182 | public int getPort() { |
| 183 | return mPort; |
| 184 | } |
| 185 | |
Ignacio Solla | 451e338 | 2014-11-10 10:35:54 +0000 | [diff] [blame] | 186 | /** {@hide} */ |
Mathew Inwood | 53f089f | 2018-08-08 14:44:44 +0100 | [diff] [blame] | 187 | @UnsupportedAppUsage |
Bjorn Bringert | eb8be97 | 2010-10-12 16:24:55 +0100 | [diff] [blame] | 188 | public void setPath(String path) { |
| 189 | mPath = path; |
| 190 | } |
| 191 | |
Ignacio Solla | 451e338 | 2014-11-10 10:35:54 +0000 | [diff] [blame] | 192 | /** {@hide} */ |
Mathew Inwood | 53f089f | 2018-08-08 14:44:44 +0100 | [diff] [blame] | 193 | @UnsupportedAppUsage |
Bjorn Bringert | eb8be97 | 2010-10-12 16:24:55 +0100 | [diff] [blame] | 194 | public String getPath() { |
| 195 | return mPath; |
| 196 | } |
| 197 | |
Ignacio Solla | 451e338 | 2014-11-10 10:35:54 +0000 | [diff] [blame] | 198 | /** {@hide} */ |
Bjorn Bringert | eb8be97 | 2010-10-12 16:24:55 +0100 | [diff] [blame] | 199 | public void setAuthInfo(String authInfo) { |
| 200 | mAuthInfo = authInfo; |
| 201 | } |
| 202 | |
Ignacio Solla | 451e338 | 2014-11-10 10:35:54 +0000 | [diff] [blame] | 203 | /** {@hide} */ |
Mathew Inwood | 53f089f | 2018-08-08 14:44:44 +0100 | [diff] [blame] | 204 | @UnsupportedAppUsage |
Bjorn Bringert | eb8be97 | 2010-10-12 16:24:55 +0100 | [diff] [blame] | 205 | public String getAuthInfo() { |
| 206 | return mAuthInfo; |
| 207 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 208 | } |