WebAddress.java revision 2269d1572e5fcfb725ea55f5764d8c3280d69f6d
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 17package android.net; 18 19import static android.util.Patterns.GOOD_IRI_CHAR; 20 21import java.util.regex.Matcher; 22import 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 */ 40public 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( 57 /* scheme */ "(?:(http|HTTP|https|HTTPS|file|FILE)\\:\\/\\/)?" + 58 /* authority */ "(?:([-A-Za-z0-9$_.+!*'(),;?&=]+(?:\\:[-A-Za-z0-9$_.+!*'(),;?&=]+)?)@)?" + 59 /* host */ "([-" + GOOD_IRI_CHAR + "%_]+(?:\\.[-" + GOOD_IRI_CHAR + "%_]+)*|\\[[0-9a-fA-F:\\.]+\\])?" + 60 /* port */ "(?:\\:([0-9]+))?" + 61 /* path */ "(\\/?[^#]*)?" + 62 /* anchor */ ".*"); 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); 82 if (t != null) mScheme = t; 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); 88 if (t != null) { 89 try { 90 mPort = Integer.parseInt(t); 91 } catch (NumberFormatException ex) { 92 throw new ParseException("Bad port"); 93 } 94 } 95 t = m.group(MATCH_GROUP_PATH); 96 if (t != null && t.length() > 0) { 97 /* handle busted myspace frontpage redirect with 98 missing initial "/" */ 99 if (t.charAt(0) == '/') { 100 mPath = t; 101 } else { 102 mPath = "/" + t; 103 } 104 } 105 106 } else { 107 // nothing found... outa here 108 throw new ParseException("Bad address"); 109 } 110 111 /* Get port from scheme or scheme from port, if necessary and 112 possible */ 113 if (mPort == 443 && mScheme.equals("")) { 114 mScheme = "https"; 115 } else if (mPort == -1) { 116 if (mScheme.equals("https")) 117 mPort = 443; 118 else 119 mPort = 80; // default 120 } 121 if (mScheme.equals("")) mScheme = "http"; 122 } 123 124 public String toString() { 125 String port = ""; 126 if ((mPort != 443 && mScheme.equals("https")) || 127 (mPort != 80 && mScheme.equals("http"))) { 128 port = ":" + Integer.toString(mPort); 129 } 130 String authInfo = ""; 131 if (mAuthInfo.length() > 0) { 132 authInfo = mAuthInfo + "@"; 133 } 134 135 return mScheme + "://" + authInfo + mHost + port + mPath; 136 } 137} 138