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 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 21 | import java.util.regex.Matcher; |
| 22 | import java.util.regex.Pattern; |
| 23 | |
| 24 | /** |
| 25 | * {@hide} |
| 26 | * |
| 27 | * Web Address Parser |
| 28 | * |
| 29 | * This is called WebAddress, rather than URL or URI, because it |
| 30 | * attempts to parse the stuff that a user will actually type into a |
| 31 | * browser address widget. |
| 32 | * |
| 33 | * Unlike java.net.uri, this parser will not choke on URIs missing |
| 34 | * schemes. It will only throw a ParseException if the input is |
| 35 | * really hosed. |
| 36 | * |
| 37 | * If given an https scheme but no port, fills in port |
| 38 | * |
| 39 | */ |
| 40 | public class WebAddress { |
| 41 | |
| 42 | private final static String LOGTAG = "http"; |
| 43 | |
| 44 | public String mScheme; |
| 45 | public String mHost; |
| 46 | public int mPort; |
| 47 | public String mPath; |
| 48 | public String mAuthInfo; |
| 49 | |
| 50 | static final int MATCH_GROUP_SCHEME = 1; |
| 51 | static final int MATCH_GROUP_AUTHORITY = 2; |
| 52 | static final int MATCH_GROUP_HOST = 3; |
| 53 | static final int MATCH_GROUP_PORT = 4; |
| 54 | static final int MATCH_GROUP_PATH = 5; |
| 55 | |
| 56 | static Pattern sAddressPattern = Pattern.compile( |
Patrick Scott | 9253446 | 2010-03-31 13:35:24 -0400 | [diff] [blame] | 57 | /* scheme */ "(?:(http|https|file)\\:\\/\\/)?" + |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 58 | /* authority */ "(?:([-A-Za-z0-9$_.+!*'(),;?&=]+(?:\\:[-A-Za-z0-9$_.+!*'(),;?&=]+)?)@)?" + |
Shimeng (Simon) Wang | 51c02db | 2010-02-11 14:07:44 -0800 | [diff] [blame] | 59 | /* host */ "([-" + GOOD_IRI_CHAR + "%_]+(?:\\.[-" + GOOD_IRI_CHAR + "%_]+)*|\\[[0-9a-fA-F:\\.]+\\])?" + |
Steve Block | 2257440 | 2010-03-08 17:52:02 +0000 | [diff] [blame] | 60 | /* port */ "(?:\\:([0-9]*))?" + |
Patrick Scott | fa4039e4 | 2010-02-16 16:15:55 -0500 | [diff] [blame] | 61 | /* path */ "(\\/?[^#]*)?" + |
Patrick Scott | 9253446 | 2010-03-31 13:35:24 -0400 | [diff] [blame] | 62 | /* anchor */ ".*", Pattern.CASE_INSENSITIVE); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 63 | |
| 64 | /** parses given uriString. */ |
| 65 | public WebAddress(String address) throws ParseException { |
| 66 | if (address == null) { |
| 67 | throw new NullPointerException(); |
| 68 | } |
| 69 | |
| 70 | // android.util.Log.d(LOGTAG, "WebAddress: " + address); |
| 71 | |
| 72 | mScheme = ""; |
| 73 | mHost = ""; |
| 74 | mPort = -1; |
| 75 | mPath = "/"; |
| 76 | mAuthInfo = ""; |
| 77 | |
| 78 | Matcher m = sAddressPattern.matcher(address); |
| 79 | String t; |
| 80 | if (m.matches()) { |
| 81 | t = m.group(MATCH_GROUP_SCHEME); |
Patrick Scott | 9253446 | 2010-03-31 13:35:24 -0400 | [diff] [blame] | 82 | if (t != null) mScheme = t.toLowerCase(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 83 | t = m.group(MATCH_GROUP_AUTHORITY); |
| 84 | if (t != null) mAuthInfo = t; |
| 85 | t = m.group(MATCH_GROUP_HOST); |
| 86 | if (t != null) mHost = t; |
| 87 | t = m.group(MATCH_GROUP_PORT); |
Steve Block | 2257440 | 2010-03-08 17:52:02 +0000 | [diff] [blame] | 88 | if (t != null && t.length() > 0) { |
| 89 | // The ':' character is not returned by the regex. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 90 | try { |
| 91 | mPort = Integer.parseInt(t); |
| 92 | } catch (NumberFormatException ex) { |
| 93 | throw new ParseException("Bad port"); |
| 94 | } |
| 95 | } |
| 96 | t = m.group(MATCH_GROUP_PATH); |
| 97 | if (t != null && t.length() > 0) { |
| 98 | /* handle busted myspace frontpage redirect with |
| 99 | missing initial "/" */ |
| 100 | if (t.charAt(0) == '/') { |
| 101 | mPath = t; |
| 102 | } else { |
| 103 | mPath = "/" + t; |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | } else { |
| 108 | // nothing found... outa here |
| 109 | throw new ParseException("Bad address"); |
| 110 | } |
| 111 | |
| 112 | /* Get port from scheme or scheme from port, if necessary and |
| 113 | possible */ |
| 114 | if (mPort == 443 && mScheme.equals("")) { |
| 115 | mScheme = "https"; |
| 116 | } else if (mPort == -1) { |
| 117 | if (mScheme.equals("https")) |
| 118 | mPort = 443; |
| 119 | else |
| 120 | mPort = 80; // default |
| 121 | } |
| 122 | if (mScheme.equals("")) mScheme = "http"; |
| 123 | } |
| 124 | |
| 125 | public String toString() { |
| 126 | String port = ""; |
| 127 | if ((mPort != 443 && mScheme.equals("https")) || |
| 128 | (mPort != 80 && mScheme.equals("http"))) { |
| 129 | port = ":" + Integer.toString(mPort); |
| 130 | } |
| 131 | String authInfo = ""; |
| 132 | if (mAuthInfo.length() > 0) { |
| 133 | authInfo = mAuthInfo + "@"; |
| 134 | } |
| 135 | |
| 136 | return mScheme + "://" + authInfo + mHost + port + mPath; |
| 137 | } |
| 138 | } |