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