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