blob: c3f5b2e8b7ba9ba758be1807a8b39dcd651ba956 [file] [log] [blame]
Piotr Jastrzebski51b1b692015-02-16 15:01:09 +00001/*
Narayan Kamath2c87ad32015-12-21 13:53:32 +00002 * Copyright (C) 2014 The Android Open Source Project
Tobias Thierer7dbee732018-02-08 21:45:09 +00003 * Copyright (c) 1995, 2015, Oracle and/or its affiliates. All rights reserved.
Piotr Jastrzebski51b1b692015-02-16 15:01:09 +00004 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 *
6 * This code is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License version 2 only, as
8 * published by the Free Software Foundation. Oracle designates this
9 * particular file as subject to the "Classpath" exception as provided
10 * by Oracle in the LICENSE file that accompanied this code.
11 *
12 * This code is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 * version 2 for more details (a copy is included in the LICENSE file that
16 * accompanied this code).
17 *
18 * You should have received a copy of the GNU General Public License version
19 * 2 along with this work; if not, write to the Free Software Foundation,
20 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
21 *
22 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
23 * or visit www.oracle.com if you need additional information or have any
24 * questions.
25 */
26
27package java.net;
28
29import java.io.IOException;
30import java.io.InputStream;
Tobias Thierer7dbee732018-02-08 21:45:09 +000031import java.io.InvalidObjectException;
32import java.io.ObjectInputStream.GetField;
33import java.io.ObjectStreamException;
34import java.io.ObjectStreamField;
35import java.util.Collections;
36import java.util.HashSet;
Piotr Jastrzebski51b1b692015-02-16 15:01:09 +000037import java.util.Hashtable;
Tobias Thierer7dbee732018-02-08 21:45:09 +000038import java.util.Set;
Piotr Jastrzebski51b1b692015-02-16 15:01:09 +000039import java.util.StringTokenizer;
40import sun.security.util.SecurityConstants;
41
42/**
Yi Kong3a6411e2016-06-14 11:25:41 +010043 * Class {@code URL} represents a Uniform Resource
Piotr Jastrzebski51b1b692015-02-16 15:01:09 +000044 * Locator, a pointer to a "resource" on the World
45 * Wide Web. A resource can be something as simple as a file or a
46 * directory, or it can be a reference to a more complicated object,
47 * such as a query to a database or to a search engine. More
48 * information on the types of URLs and their formats can be found at:
Yi Kong3a6411e2016-06-14 11:25:41 +010049 * <a href=
50 * "http://web.archive.org/web/20051219043731/http://archive.ncsa.uiuc.edu/SDG/Software/Mosaic/Demo/url-primer.html">
51 * <i>Types of URL</i></a>
Piotr Jastrzebski51b1b692015-02-16 15:01:09 +000052 * <p>
Yi Kong3a6411e2016-06-14 11:25:41 +010053 * In general, a URL can be broken into several parts. Consider the
54 * following example:
55 * <blockquote><pre>
56 * http://www.example.com/docs/resource1.html
57 * </pre></blockquote>
58 * <p>
59 * The URL above indicates that the protocol to use is
60 * {@code http} (HyperText Transfer Protocol) and that the
Piotr Jastrzebski51b1b692015-02-16 15:01:09 +000061 * information resides on a host machine named
Yi Kong3a6411e2016-06-14 11:25:41 +010062 * {@code www.example.com}. The information on that host
63 * machine is named {@code /docs/resource1.html}. The exact
Piotr Jastrzebski51b1b692015-02-16 15:01:09 +000064 * meaning of this name on the host machine is both protocol
65 * dependent and host dependent. The information normally resides in
66 * a file, but it could be generated on the fly. This component of
67 * the URL is called the <i>path</i> component.
68 * <p>
69 * A URL can optionally specify a "port", which is the
70 * port number to which the TCP connection is made on the remote host
71 * machine. If the port is not specified, the default port for
72 * the protocol is used instead. For example, the default port for
Yi Kong3a6411e2016-06-14 11:25:41 +010073 * {@code http} is {@code 80}. An alternative port could be
Piotr Jastrzebski51b1b692015-02-16 15:01:09 +000074 * specified as:
75 * <blockquote><pre>
Yi Kong3a6411e2016-06-14 11:25:41 +010076 * http://www.example.com:1080/docs/resource1.html
Piotr Jastrzebski51b1b692015-02-16 15:01:09 +000077 * </pre></blockquote>
78 * <p>
Yi Kong3a6411e2016-06-14 11:25:41 +010079 * The syntax of {@code URL} is defined by <a
Piotr Jastrzebski51b1b692015-02-16 15:01:09 +000080 * href="http://www.ietf.org/rfc/rfc2396.txt"><i>RFC&nbsp;2396: Uniform
81 * Resource Identifiers (URI): Generic Syntax</i></a>, amended by <a
82 * href="http://www.ietf.org/rfc/rfc2732.txt"><i>RFC&nbsp;2732: Format for
83 * Literal IPv6 Addresses in URLs</i></a>. The Literal IPv6 address format
84 * also supports scope_ids. The syntax and usage of scope_ids is described
85 * <a href="Inet6Address.html#scoped">here</a>.
86 * <p>
87 * A URL may have appended to it a "fragment", also known
88 * as a "ref" or a "reference". The fragment is indicated by the sharp
89 * sign character "#" followed by more characters. For example,
90 * <blockquote><pre>
91 * http://java.sun.com/index.html#chapter1
92 * </pre></blockquote>
93 * <p>
94 * This fragment is not technically part of the URL. Rather, it
95 * indicates that after the specified resource is retrieved, the
96 * application is specifically interested in that part of the
Yi Kong3a6411e2016-06-14 11:25:41 +010097 * document that has the tag {@code chapter1} attached to it. The
Piotr Jastrzebski51b1b692015-02-16 15:01:09 +000098 * meaning of a tag is resource specific.
99 * <p>
100 * An application can also specify a "relative URL",
101 * which contains only enough information to reach the resource
102 * relative to another URL. Relative URLs are frequently used within
103 * HTML pages. For example, if the contents of the URL:
104 * <blockquote><pre>
105 * http://java.sun.com/index.html
106 * </pre></blockquote>
107 * contained within it the relative URL:
108 * <blockquote><pre>
109 * FAQ.html
110 * </pre></blockquote>
111 * it would be a shorthand for:
112 * <blockquote><pre>
113 * http://java.sun.com/FAQ.html
114 * </pre></blockquote>
115 * <p>
116 * The relative URL need not specify all the components of a URL. If
117 * the protocol, host name, or port number is missing, the value is
118 * inherited from the fully specified URL. The file component must be
119 * specified. The optional fragment is not inherited.
120 * <p>
121 * The URL class does not itself encode or decode any URL components
122 * according to the escaping mechanism defined in RFC2396. It is the
123 * responsibility of the caller to encode any fields, which need to be
124 * escaped prior to calling URL, and also to decode any escaped fields,
125 * that are returned from URL. Furthermore, because URL has no knowledge
126 * of URL escaping, it does not recognise equivalence between the encoded
127 * or decoded form of the same URL. For example, the two URLs:<br>
128 * <pre> http://foo.com/hello world/ and http://foo.com/hello%20world</pre>
129 * would be considered not equal to each other.
130 * <p>
131 * Note, the {@link java.net.URI} class does perform escaping of its
132 * component fields in certain circumstances. The recommended way
133 * to manage the encoding and decoding of URLs is to use {@link java.net.URI},
134 * and to convert between these two classes using {@link #toURI()} and
135 * {@link URI#toURL()}.
136 * <p>
137 * The {@link URLEncoder} and {@link URLDecoder} classes can also be
138 * used, but only for HTML form encoding, which is not the same
139 * as the encoding scheme defined in RFC2396.
140 *
141 * @author James Gosling
142 * @since JDK1.0
143 */
144public final class URL implements java.io.Serializable {
145
Tobias Thierer7dbee732018-02-08 21:45:09 +0000146 // Android-changed: Custom built-in URLStreamHandlers for http, https.
147 // static final String BUILTIN_HANDLERS_PREFIX = "sun.net.www.protocol";
148 private static final Set<String> BUILTIN_HANDLER_CLASS_NAMES = createBuiltinHandlerClassNames();
Piotr Jastrzebski51b1b692015-02-16 15:01:09 +0000149 static final long serialVersionUID = -7627629688361524110L;
150
151 /**
152 * The property which specifies the package prefix list to be scanned
153 * for protocol handlers. The value of this property (if any) should
154 * be a vertical bar delimited list of package names to search through
155 * for a protocol handler to load. The policy of this class is that
156 * all protocol handlers will be in a class called <protocolname>.Handler,
157 * and each package in the list is examined in turn for a matching
158 * handler. If none are found (or the property is not specified), the
159 * default package prefix, sun.net.www.protocol, is used. The search
160 * proceeds from the first package in the list to the last and stops
161 * when a match is found.
162 */
163 private static final String protocolPathProp = "java.protocol.handler.pkgs";
164
165 /**
166 * The protocol to use (ftp, http, nntp, ... etc.) .
167 * @serial
168 */
169 private String protocol;
170
171 /**
172 * The host name to connect to.
173 * @serial
174 */
175 private String host;
176
177 /**
178 * The protocol port to connect to.
179 * @serial
180 */
181 private int port = -1;
182
183 /**
Yi Kong3a6411e2016-06-14 11:25:41 +0100184 * The specified file name on that host. {@code file} is
185 * defined as {@code path[?query]}
Piotr Jastrzebski51b1b692015-02-16 15:01:09 +0000186 * @serial
187 */
188 private String file;
189
190 /**
191 * The query part of this URL.
192 */
193 private transient String query;
194
195 /**
196 * The authority part of this URL.
197 * @serial
198 */
199 private String authority;
200
201 /**
202 * The path part of this URL.
203 */
204 private transient String path;
205
206 /**
207 * The userinfo part of this URL.
208 */
209 private transient String userInfo;
210
211 /**
212 * # reference.
213 * @serial
214 */
215 private String ref;
216
217 /**
218 * The host's IP address, used in equals and hashCode.
219 * Computed on demand. An uninitialized or unknown hostAddress is null.
220 */
221 transient InetAddress hostAddress;
222
223 /**
224 * The URLStreamHandler for this URL.
225 */
226 transient URLStreamHandler handler;
227
228 /* Our hash code.
229 * @serial
230 */
Tobias Thierer7dbee732018-02-08 21:45:09 +0000231 private int hashCode = -1;
232
233 private transient UrlDeserializedState tempState;
Piotr Jastrzebski51b1b692015-02-16 15:01:09 +0000234
235 /**
Yi Kong3a6411e2016-06-14 11:25:41 +0100236 * Creates a {@code URL} object from the specified
237 * {@code protocol}, {@code host}, {@code port}
238 * number, and {@code file}.<p>
Piotr Jastrzebski51b1b692015-02-16 15:01:09 +0000239 *
Yi Kong3a6411e2016-06-14 11:25:41 +0100240 * {@code host} can be expressed as a host name or a literal
Piotr Jastrzebski51b1b692015-02-16 15:01:09 +0000241 * IP address. If IPv6 literal address is used, it should be
Yi Kong3a6411e2016-06-14 11:25:41 +0100242 * enclosed in square brackets ({@code '['} and {@code ']'}), as
Piotr Jastrzebski51b1b692015-02-16 15:01:09 +0000243 * specified by <a
244 * href="http://www.ietf.org/rfc/rfc2732.txt">RFC&nbsp;2732</a>;
245 * However, the literal IPv6 address format defined in <a
246 * href="http://www.ietf.org/rfc/rfc2373.txt"><i>RFC&nbsp;2373: IP
247 * Version 6 Addressing Architecture</i></a> is also accepted.<p>
248 *
Yi Kong3a6411e2016-06-14 11:25:41 +0100249 * Specifying a {@code port} number of {@code -1}
Piotr Jastrzebski51b1b692015-02-16 15:01:09 +0000250 * indicates that the URL should use the default port for the
251 * protocol.<p>
252 *
253 * If this is the first URL object being created with the specified
254 * protocol, a <i>stream protocol handler</i> object, an instance of
Yi Kong3a6411e2016-06-14 11:25:41 +0100255 * class {@code URLStreamHandler}, is created for that protocol:
Piotr Jastrzebski51b1b692015-02-16 15:01:09 +0000256 * <ol>
257 * <li>If the application has previously set up an instance of
Yi Kong3a6411e2016-06-14 11:25:41 +0100258 * {@code URLStreamHandlerFactory} as the stream handler factory,
259 * then the {@code createURLStreamHandler} method of that instance
Piotr Jastrzebski51b1b692015-02-16 15:01:09 +0000260 * is called with the protocol string as an argument to create the
261 * stream protocol handler.
Yi Kong3a6411e2016-06-14 11:25:41 +0100262 * <li>If no {@code URLStreamHandlerFactory} has yet been set up,
263 * or if the factory's {@code createURLStreamHandler} method
264 * returns {@code null}, then the constructor finds the
Piotr Jastrzebski51b1b692015-02-16 15:01:09 +0000265 * value of the system property:
266 * <blockquote><pre>
267 * java.protocol.handler.pkgs
268 * </pre></blockquote>
Yi Kong3a6411e2016-06-14 11:25:41 +0100269 * If the value of that system property is not {@code null},
Piotr Jastrzebski51b1b692015-02-16 15:01:09 +0000270 * it is interpreted as a list of packages separated by a vertical
Yi Kong3a6411e2016-06-14 11:25:41 +0100271 * slash character '{@code |}'. The constructor tries to load
Piotr Jastrzebski51b1b692015-02-16 15:01:09 +0000272 * the class named:
273 * <blockquote><pre>
274 * &lt;<i>package</i>&gt;.&lt;<i>protocol</i>&gt;.Handler
275 * </pre></blockquote>
276 * where &lt;<i>package</i>&gt; is replaced by the name of the package
277 * and &lt;<i>protocol</i>&gt; is replaced by the name of the protocol.
278 * If this class does not exist, or if the class exists but it is not
Yi Kong3a6411e2016-06-14 11:25:41 +0100279 * a subclass of {@code URLStreamHandler}, then the next package
Piotr Jastrzebski51b1b692015-02-16 15:01:09 +0000280 * in the list is tried.
281 * <li>If the previous step fails to find a protocol handler, then the
282 * constructor tries to load from a system default package.
283 * <blockquote><pre>
284 * &lt;<i>system default package</i>&gt;.&lt;<i>protocol</i>&gt;.Handler
285 * </pre></blockquote>
286 * If this class does not exist, or if the class exists but it is not a
Yi Kong3a6411e2016-06-14 11:25:41 +0100287 * subclass of {@code URLStreamHandler}, then a
288 * {@code MalformedURLException} is thrown.
Piotr Jastrzebski51b1b692015-02-16 15:01:09 +0000289 * </ol>
290 *
291 * <p>Protocol handlers for the following protocols are guaranteed
292 * to exist on the search path :-
293 * <blockquote><pre>
Yi Kong3a6411e2016-06-14 11:25:41 +0100294 * http, https, file, and jar
Piotr Jastrzebski51b1b692015-02-16 15:01:09 +0000295 * </pre></blockquote>
296 * Protocol handlers for additional protocols may also be
297 * available.
298 *
299 * <p>No validation of the inputs is performed by this constructor.
300 *
301 * @param protocol the name of the protocol to use.
302 * @param host the name of the host.
303 * @param port the port number on the host.
304 * @param file the file on the host
305 * @exception MalformedURLException if an unknown protocol is specified.
306 * @see java.lang.System#getProperty(java.lang.String)
307 * @see java.net.URL#setURLStreamHandlerFactory(
308 * java.net.URLStreamHandlerFactory)
309 * @see java.net.URLStreamHandler
310 * @see java.net.URLStreamHandlerFactory#createURLStreamHandler(
311 * java.lang.String)
312 */
313 public URL(String protocol, String host, int port, String file)
314 throws MalformedURLException
315 {
316 this(protocol, host, port, file, null);
317 }
318
319 /**
Yi Kong3a6411e2016-06-14 11:25:41 +0100320 * Creates a URL from the specified {@code protocol}
321 * name, {@code host} name, and {@code file} name. The
Piotr Jastrzebski51b1b692015-02-16 15:01:09 +0000322 * default port for the specified protocol is used.
323 * <p>
324 * This method is equivalent to calling the four-argument
Yi Kong3a6411e2016-06-14 11:25:41 +0100325 * constructor with the arguments being {@code protocol},
326 * {@code host}, {@code -1}, and {@code file}.
Piotr Jastrzebski51b1b692015-02-16 15:01:09 +0000327 *
328 * No validation of the inputs is performed by this constructor.
329 *
330 * @param protocol the name of the protocol to use.
331 * @param host the name of the host.
332 * @param file the file on the host.
333 * @exception MalformedURLException if an unknown protocol is specified.
334 * @see java.net.URL#URL(java.lang.String, java.lang.String,
335 * int, java.lang.String)
336 */
337 public URL(String protocol, String host, String file)
338 throws MalformedURLException {
339 this(protocol, host, -1, file);
340 }
341
342 /**
Yi Kong3a6411e2016-06-14 11:25:41 +0100343 * Creates a {@code URL} object from the specified
344 * {@code protocol}, {@code host}, {@code port}
345 * number, {@code file}, and {@code handler}. Specifying
346 * a {@code port} number of {@code -1} indicates that
Piotr Jastrzebski51b1b692015-02-16 15:01:09 +0000347 * the URL should use the default port for the protocol. Specifying
Yi Kong3a6411e2016-06-14 11:25:41 +0100348 * a {@code handler} of {@code null} indicates that the URL
Piotr Jastrzebski51b1b692015-02-16 15:01:09 +0000349 * should use a default stream handler for the protocol, as outlined
350 * for:
351 * java.net.URL#URL(java.lang.String, java.lang.String, int,
352 * java.lang.String)
353 *
354 * <p>If the handler is not null and there is a security manager,
Yi Kong3a6411e2016-06-14 11:25:41 +0100355 * the security manager's {@code checkPermission}
Piotr Jastrzebski51b1b692015-02-16 15:01:09 +0000356 * method is called with a
Yi Kong3a6411e2016-06-14 11:25:41 +0100357 * {@code NetPermission("specifyStreamHandler")} permission.
Piotr Jastrzebski51b1b692015-02-16 15:01:09 +0000358 * This may result in a SecurityException.
359 *
360 * No validation of the inputs is performed by this constructor.
361 *
362 * @param protocol the name of the protocol to use.
363 * @param host the name of the host.
364 * @param port the port number on the host.
365 * @param file the file on the host
366 * @param handler the stream handler for the URL.
367 * @exception MalformedURLException if an unknown protocol is specified.
368 * @exception SecurityException
369 * if a security manager exists and its
Yi Kong3a6411e2016-06-14 11:25:41 +0100370 * {@code checkPermission} method doesn't allow
Piotr Jastrzebski51b1b692015-02-16 15:01:09 +0000371 * specifying a stream handler explicitly.
372 * @see java.lang.System#getProperty(java.lang.String)
373 * @see java.net.URL#setURLStreamHandlerFactory(
374 * java.net.URLStreamHandlerFactory)
375 * @see java.net.URLStreamHandler
376 * @see java.net.URLStreamHandlerFactory#createURLStreamHandler(
377 * java.lang.String)
378 * @see SecurityManager#checkPermission
379 * @see java.net.NetPermission
380 */
381 public URL(String protocol, String host, int port, String file,
382 URLStreamHandler handler) throws MalformedURLException {
383 if (handler != null) {
384 SecurityManager sm = System.getSecurityManager();
385 if (sm != null) {
386 // check for permission to specify a handler
387 checkSpecifyHandler(sm);
388 }
389 }
390
391 protocol = protocol.toLowerCase();
392 this.protocol = protocol;
393 if (host != null) {
394
395 /**
396 * if host is a literal IPv6 address,
397 * we will make it conform to RFC 2732
398 */
399 if (host.indexOf(':') >= 0 && !host.startsWith("[")) {
400 host = "["+host+"]";
401 }
402 this.host = host;
403
404 if (port < -1) {
405 throw new MalformedURLException("Invalid port number :" +
406 port);
407 }
408 this.port = port;
409 authority = (port == -1) ? host : host + ":" + port;
410 }
411
Victor Change376c072018-01-04 14:27:34 +0000412 // Android-changed: App compat. Prepend '/' if host is null / empty
413 // Parts parts = new Parts(file);
Przemyslaw Szczepaniak06fc5c32015-07-21 17:20:27 +0100414 Parts parts = new Parts(file, host);
Piotr Jastrzebski51b1b692015-02-16 15:01:09 +0000415 path = parts.getPath();
416 query = parts.getQuery();
417
418 if (query != null) {
419 this.file = path + "?" + query;
420 } else {
421 this.file = path;
422 }
423 ref = parts.getRef();
424
425 // Note: we don't do validation of the URL here. Too risky to change
426 // right now, but worth considering for future reference. -br
427 if (handler == null &&
428 (handler = getURLStreamHandler(protocol)) == null) {
429 throw new MalformedURLException("unknown protocol: " + protocol);
430 }
431 this.handler = handler;
432 }
433
434 /**
Yi Kong3a6411e2016-06-14 11:25:41 +0100435 * Creates a {@code URL} object from the {@code String}
Piotr Jastrzebski51b1b692015-02-16 15:01:09 +0000436 * representation.
437 * <p>
438 * This constructor is equivalent to a call to the two-argument
Yi Kong3a6411e2016-06-14 11:25:41 +0100439 * constructor with a {@code null} first argument.
Piotr Jastrzebski51b1b692015-02-16 15:01:09 +0000440 *
Yi Kong3a6411e2016-06-14 11:25:41 +0100441 * @param spec the {@code String} to parse as a URL.
Piotr Jastrzebski51b1b692015-02-16 15:01:09 +0000442 * @exception MalformedURLException if no protocol is specified, or an
Yi Kong3a6411e2016-06-14 11:25:41 +0100443 * unknown protocol is found, or {@code spec} is {@code null}.
Piotr Jastrzebski51b1b692015-02-16 15:01:09 +0000444 * @see java.net.URL#URL(java.net.URL, java.lang.String)
445 */
446 public URL(String spec) throws MalformedURLException {
447 this(null, spec);
448 }
449
450 /**
451 * Creates a URL by parsing the given spec within a specified context.
452 *
453 * The new URL is created from the given context URL and the spec
454 * argument as described in
455 * RFC2396 &quot;Uniform Resource Identifiers : Generic * Syntax&quot; :
456 * <blockquote><pre>
457 * &lt;scheme&gt;://&lt;authority&gt;&lt;path&gt;?&lt;query&gt;#&lt;fragment&gt;
458 * </pre></blockquote>
459 * The reference is parsed into the scheme, authority, path, query and
460 * fragment parts. If the path component is empty and the scheme,
461 * authority, and query components are undefined, then the new URL is a
462 * reference to the current document. Otherwise, the fragment and query
463 * parts present in the spec are used in the new URL.
464 * <p>
465 * If the scheme component is defined in the given spec and does not match
466 * the scheme of the context, then the new URL is created as an absolute
467 * URL based on the spec alone. Otherwise the scheme component is inherited
468 * from the context URL.
469 * <p>
470 * If the authority component is present in the spec then the spec is
471 * treated as absolute and the spec authority and path will replace the
472 * context authority and path. If the authority component is absent in the
473 * spec then the authority of the new URL will be inherited from the
474 * context.
475 * <p>
476 * If the spec's path component begins with a slash character
477 * &quot;/&quot; then the
478 * path is treated as absolute and the spec path replaces the context path.
479 * <p>
480 * Otherwise, the path is treated as a relative path and is appended to the
481 * context path, as described in RFC2396. Also, in this case,
482 * the path is canonicalized through the removal of directory
Yi Kong3a6411e2016-06-14 11:25:41 +0100483 * changes made by occurrences of &quot;..&quot; and &quot;.&quot;.
Piotr Jastrzebski51b1b692015-02-16 15:01:09 +0000484 * <p>
485 * For a more detailed description of URL parsing, refer to RFC2396.
486 *
487 * @param context the context in which to parse the specification.
Yi Kong3a6411e2016-06-14 11:25:41 +0100488 * @param spec the {@code String} to parse as a URL.
Piotr Jastrzebski51b1b692015-02-16 15:01:09 +0000489 * @exception MalformedURLException if no protocol is specified, or an
Yi Kong3a6411e2016-06-14 11:25:41 +0100490 * unknown protocol is found, or {@code spec} is {@code null}.
Piotr Jastrzebski51b1b692015-02-16 15:01:09 +0000491 * @see java.net.URL#URL(java.lang.String, java.lang.String,
492 * int, java.lang.String)
493 * @see java.net.URLStreamHandler
494 * @see java.net.URLStreamHandler#parseURL(java.net.URL,
495 * java.lang.String, int, int)
496 */
497 public URL(URL context, String spec) throws MalformedURLException {
498 this(context, spec, null);
499 }
500
501 /**
502 * Creates a URL by parsing the given spec with the specified handler
503 * within a specified context. If the handler is null, the parsing
504 * occurs as with the two argument constructor.
505 *
506 * @param context the context in which to parse the specification.
Yi Kong3a6411e2016-06-14 11:25:41 +0100507 * @param spec the {@code String} to parse as a URL.
Piotr Jastrzebski51b1b692015-02-16 15:01:09 +0000508 * @param handler the stream handler for the URL.
509 * @exception MalformedURLException if no protocol is specified, or an
Yi Kong3a6411e2016-06-14 11:25:41 +0100510 * unknown protocol is found, or {@code spec} is {@code null}.
Piotr Jastrzebski51b1b692015-02-16 15:01:09 +0000511 * @exception SecurityException
512 * if a security manager exists and its
Yi Kong3a6411e2016-06-14 11:25:41 +0100513 * {@code checkPermission} method doesn't allow
Piotr Jastrzebski51b1b692015-02-16 15:01:09 +0000514 * specifying a stream handler.
515 * @see java.net.URL#URL(java.lang.String, java.lang.String,
516 * int, java.lang.String)
517 * @see java.net.URLStreamHandler
518 * @see java.net.URLStreamHandler#parseURL(java.net.URL,
519 * java.lang.String, int, int)
520 */
521 public URL(URL context, String spec, URLStreamHandler handler)
522 throws MalformedURLException
523 {
524 String original = spec;
525 int i, limit, c;
526 int start = 0;
527 String newProtocol = null;
528 boolean aRef=false;
529 boolean isRelative = false;
530
531 // Check for permission to specify a handler
532 if (handler != null) {
533 SecurityManager sm = System.getSecurityManager();
534 if (sm != null) {
535 checkSpecifyHandler(sm);
536 }
537 }
538
539 try {
540 limit = spec.length();
541 while ((limit > 0) && (spec.charAt(limit - 1) <= ' ')) {
542 limit--; //eliminate trailing whitespace
543 }
544 while ((start < limit) && (spec.charAt(start) <= ' ')) {
545 start++; // eliminate leading whitespace
546 }
547
548 if (spec.regionMatches(true, start, "url:", 0, 4)) {
549 start += 4;
550 }
551 if (start < spec.length() && spec.charAt(start) == '#') {
552 /* we're assuming this is a ref relative to the context URL.
553 * This means protocols cannot start w/ '#', but we must parse
554 * ref URL's like: "hello:there" w/ a ':' in them.
555 */
556 aRef=true;
557 }
558 for (i = start ; !aRef && (i < limit) &&
559 ((c = spec.charAt(i)) != '/') ; i++) {
560 if (c == ':') {
561
562 String s = spec.substring(start, i).toLowerCase();
563 if (isValidProtocol(s)) {
564 newProtocol = s;
565 start = i + 1;
566 }
567 break;
568 }
569 }
570
571 // Only use our context if the protocols match.
572 protocol = newProtocol;
573 if ((context != null) && ((newProtocol == null) ||
574 newProtocol.equalsIgnoreCase(context.protocol))) {
575 // inherit the protocol handler from the context
576 // if not specified to the constructor
577 if (handler == null) {
578 handler = context.handler;
579 }
580
581 // If the context is a hierarchical URL scheme and the spec
582 // contains a matching scheme then maintain backwards
583 // compatibility and treat it as if the spec didn't contain
584 // the scheme; see 5.2.3 of RFC2396
585 if (context.path != null && context.path.startsWith("/"))
586 newProtocol = null;
587
588 if (newProtocol == null) {
589 protocol = context.protocol;
590 authority = context.authority;
591 userInfo = context.userInfo;
592 host = context.host;
593 port = context.port;
594 file = context.file;
595 path = context.path;
596 isRelative = true;
597 }
598 }
599
600 if (protocol == null) {
601 throw new MalformedURLException("no protocol: "+original);
602 }
603
604 // Get the protocol handler if not specified or the protocol
605 // of the context could not be used
606 if (handler == null &&
607 (handler = getURLStreamHandler(protocol)) == null) {
608 throw new MalformedURLException("unknown protocol: "+protocol);
609 }
610
611 this.handler = handler;
612
613 i = spec.indexOf('#', start);
614 if (i >= 0) {
615 ref = spec.substring(i + 1, limit);
616 limit = i;
617 }
618
619 /*
620 * Handle special case inheritance of query and fragment
621 * implied by RFC2396 section 5.2.2.
622 */
623 if (isRelative && start == limit) {
624 query = context.query;
625 if (ref == null) {
626 ref = context.ref;
627 }
628 }
629
630 handler.parseURL(this, spec, start, limit);
631
632 } catch(MalformedURLException e) {
633 throw e;
634 } catch(Exception e) {
635 MalformedURLException exception = new MalformedURLException(e.getMessage());
636 exception.initCause(e);
637 throw exception;
638 }
639 }
640
641 /*
642 * Returns true if specified string is a valid protocol name.
643 */
644 private boolean isValidProtocol(String protocol) {
645 int len = protocol.length();
646 if (len < 1)
647 return false;
648 char c = protocol.charAt(0);
649 if (!Character.isLetter(c))
650 return false;
651 for (int i = 1; i < len; i++) {
652 c = protocol.charAt(i);
653 if (!Character.isLetterOrDigit(c) && c != '.' && c != '+' &&
654 c != '-') {
655 return false;
656 }
657 }
658 return true;
659 }
660
661 /*
662 * Checks for permission to specify a stream handler.
663 */
664 private void checkSpecifyHandler(SecurityManager sm) {
665 sm.checkPermission(SecurityConstants.SPECIFY_HANDLER_PERMISSION);
666 }
667
668 /**
669 * Sets the fields of the URL. This is not a public method so that
670 * only URLStreamHandlers can modify URL fields. URLs are
671 * otherwise constant.
672 *
673 * @param protocol the name of the protocol to use
674 * @param host the name of the host
675 @param port the port number on the host
676 * @param file the file on the host
677 * @param ref the internal reference in the URL
678 */
Yi Kong65184752016-08-15 14:42:20 +0100679 void set(String protocol, String host, int port,
680 String file, String ref) {
Piotr Jastrzebski51b1b692015-02-16 15:01:09 +0000681 synchronized (this) {
682 this.protocol = protocol;
683 this.host = host;
684 authority = port == -1 ? host : host + ":" + port;
685 this.port = port;
686 this.file = file;
687 this.ref = ref;
688 /* This is very important. We must recompute this after the
689 * URL has been changed. */
690 hashCode = -1;
691 hostAddress = null;
692 int q = file.lastIndexOf('?');
693 if (q != -1) {
694 query = file.substring(q+1);
695 path = file.substring(0, q);
696 } else
697 path = file;
698 }
699 }
700
701 /**
702 * Sets the specified 8 fields of the URL. This is not a public method so
703 * that only URLStreamHandlers can modify URL fields. URLs are otherwise
704 * constant.
705 *
706 * @param protocol the name of the protocol to use
707 * @param host the name of the host
708 * @param port the port number on the host
709 * @param authority the authority part for the url
710 * @param userInfo the username and password
711 * @param path the file on the host
712 * @param ref the internal reference in the URL
713 * @param query the query part of this URL
714 * @since 1.3
715 */
Yi Kong65184752016-08-15 14:42:20 +0100716 void set(String protocol, String host, int port,
717 String authority, String userInfo, String path,
718 String query, String ref) {
Piotr Jastrzebski51b1b692015-02-16 15:01:09 +0000719 synchronized (this) {
720 this.protocol = protocol;
721 this.host = host;
722 this.port = port;
Victor Change376c072018-01-04 14:27:34 +0000723 // Android-changed: App compat. Only include query part if it's nonempty.
724 // this.file = query == null ? path : path + "?" + query;
Przemyslaw Szczepaniake56459a2016-04-04 15:02:56 +0100725 this.file = (query == null || query.isEmpty()) ? path : path + "?" + query;
Piotr Jastrzebski51b1b692015-02-16 15:01:09 +0000726 this.userInfo = userInfo;
727 this.path = path;
728 this.ref = ref;
729 /* This is very important. We must recompute this after the
730 * URL has been changed. */
731 hashCode = -1;
732 hostAddress = null;
733 this.query = query;
734 this.authority = authority;
735 }
736 }
737
738 /**
Yi Kong3a6411e2016-06-14 11:25:41 +0100739 * Gets the query part of this {@code URL}.
Piotr Jastrzebski51b1b692015-02-16 15:01:09 +0000740 *
Yi Kong3a6411e2016-06-14 11:25:41 +0100741 * @return the query part of this {@code URL},
Piotr Jastrzebski51b1b692015-02-16 15:01:09 +0000742 * or <CODE>null</CODE> if one does not exist
743 * @since 1.3
744 */
745 public String getQuery() {
746 return query;
747 }
748
749 /**
Yi Kong3a6411e2016-06-14 11:25:41 +0100750 * Gets the path part of this {@code URL}.
Piotr Jastrzebski51b1b692015-02-16 15:01:09 +0000751 *
Yi Kong3a6411e2016-06-14 11:25:41 +0100752 * @return the path part of this {@code URL}, or an
Piotr Jastrzebski51b1b692015-02-16 15:01:09 +0000753 * empty string if one does not exist
754 * @since 1.3
755 */
756 public String getPath() {
757 return path;
758 }
759
760 /**
Yi Kong3a6411e2016-06-14 11:25:41 +0100761 * Gets the userInfo part of this {@code URL}.
Piotr Jastrzebski51b1b692015-02-16 15:01:09 +0000762 *
Yi Kong3a6411e2016-06-14 11:25:41 +0100763 * @return the userInfo part of this {@code URL}, or
Piotr Jastrzebski51b1b692015-02-16 15:01:09 +0000764 * <CODE>null</CODE> if one does not exist
765 * @since 1.3
766 */
767 public String getUserInfo() {
768 return userInfo;
769 }
770
771 /**
Yi Kong3a6411e2016-06-14 11:25:41 +0100772 * Gets the authority part of this {@code URL}.
Piotr Jastrzebski51b1b692015-02-16 15:01:09 +0000773 *
Yi Kong3a6411e2016-06-14 11:25:41 +0100774 * @return the authority part of this {@code URL}
Piotr Jastrzebski51b1b692015-02-16 15:01:09 +0000775 * @since 1.3
776 */
777 public String getAuthority() {
778 return authority;
779 }
780
781 /**
Yi Kong3a6411e2016-06-14 11:25:41 +0100782 * Gets the port number of this {@code URL}.
Piotr Jastrzebski51b1b692015-02-16 15:01:09 +0000783 *
784 * @return the port number, or -1 if the port is not set
785 */
786 public int getPort() {
787 return port;
788 }
789
790 /**
791 * Gets the default port number of the protocol associated
Yi Kong3a6411e2016-06-14 11:25:41 +0100792 * with this {@code URL}. If the URL scheme or the URLStreamHandler
Piotr Jastrzebski51b1b692015-02-16 15:01:09 +0000793 * for the URL do not define a default port number,
794 * then -1 is returned.
795 *
796 * @return the port number
797 * @since 1.4
798 */
799 public int getDefaultPort() {
800 return handler.getDefaultPort();
801 }
802
803 /**
Yi Kong3a6411e2016-06-14 11:25:41 +0100804 * Gets the protocol name of this {@code URL}.
Piotr Jastrzebski51b1b692015-02-16 15:01:09 +0000805 *
Yi Kong3a6411e2016-06-14 11:25:41 +0100806 * @return the protocol of this {@code URL}.
Piotr Jastrzebski51b1b692015-02-16 15:01:09 +0000807 */
808 public String getProtocol() {
809 return protocol;
810 }
811
812 /**
Yi Kong3a6411e2016-06-14 11:25:41 +0100813 * Gets the host name of this {@code URL}, if applicable.
Piotr Jastrzebski51b1b692015-02-16 15:01:09 +0000814 * The format of the host conforms to RFC 2732, i.e. for a
815 * literal IPv6 address, this method will return the IPv6 address
Yi Kong3a6411e2016-06-14 11:25:41 +0100816 * enclosed in square brackets ({@code '['} and {@code ']'}).
Piotr Jastrzebski51b1b692015-02-16 15:01:09 +0000817 *
Yi Kong3a6411e2016-06-14 11:25:41 +0100818 * @return the host name of this {@code URL}.
Piotr Jastrzebski51b1b692015-02-16 15:01:09 +0000819 */
820 public String getHost() {
821 return host;
822 }
823
824 /**
Yi Kong3a6411e2016-06-14 11:25:41 +0100825 * Gets the file name of this {@code URL}.
Piotr Jastrzebski51b1b692015-02-16 15:01:09 +0000826 * The returned file portion will be
827 * the same as <CODE>getPath()</CODE>, plus the concatenation of
828 * the value of <CODE>getQuery()</CODE>, if any. If there is
829 * no query portion, this method and <CODE>getPath()</CODE> will
830 * return identical results.
831 *
Yi Kong3a6411e2016-06-14 11:25:41 +0100832 * @return the file name of this {@code URL},
Piotr Jastrzebski51b1b692015-02-16 15:01:09 +0000833 * or an empty string if one does not exist
834 */
835 public String getFile() {
836 return file;
837 }
838
839 /**
840 * Gets the anchor (also known as the "reference") of this
Yi Kong3a6411e2016-06-14 11:25:41 +0100841 * {@code URL}.
Piotr Jastrzebski51b1b692015-02-16 15:01:09 +0000842 *
843 * @return the anchor (also known as the "reference") of this
Yi Kong3a6411e2016-06-14 11:25:41 +0100844 * {@code URL}, or <CODE>null</CODE> if one does not exist
Piotr Jastrzebski51b1b692015-02-16 15:01:09 +0000845 */
846 public String getRef() {
847 return ref;
848 }
849
Victor Change376c072018-01-04 14:27:34 +0000850 // Android-changed: Don't let URL.equals() attempt to resolve host names.
Piotr Jastrzebski51b1b692015-02-16 15:01:09 +0000851 /**
852 * Compares this URL for equality with another object.<p>
853 *
854 * If the given object is not a URL then this method immediately returns
Yi Kong3a6411e2016-06-14 11:25:41 +0100855 * {@code false}.<p>
Piotr Jastrzebski51b1b692015-02-16 15:01:09 +0000856 *
857 * Two URL objects are equal if they have the same protocol, reference
858 * equivalent hosts, have the same port number on the host, and the same
859 * file and fragment of the file.<p>
860 *
Narayan Kamathb93dbae2016-02-01 15:54:33 +0000861 * Returns true if this URL equals {@code o}. URLs are equal if they have
862 * the same protocol, host, port, file, and reference.
Piotr Jastrzebski51b1b692015-02-16 15:01:09 +0000863 *
Narayan Kamathb93dbae2016-02-01 15:54:33 +0000864 * <h3>Network I/O Warning</h3>
865 * <p>Some implementations of URL.equals() resolve host names over the
866 * network. This is problematic:
867 * <ul>
868 * <li><strong>The network may be slow.</strong> Many classes, including
869 * core collections like {@link java.util.Map Map} and {@link java.util.Set
870 * Set} expect that {@code equals} and {@code hashCode} will return quickly.
871 * By violating this assumption, this method posed potential performance
872 * problems.
873 * <li><strong>Equal IP addresses do not imply equal content.</strong>
874 * Virtual hosting permits unrelated sites to share an IP address. This
875 * method could report two otherwise unrelated URLs to be equal because
876 * they're hosted on the same server.</li>
877 * <li><strong>The network may not be available.</strong> Two URLs could be
878 * equal when a network is available and unequal otherwise.</li>
879 * <li><strong>The network may change.</strong> The IP address for a given
880 * host name varies by network and over time. This is problematic for mobile
881 * devices. Two URLs could be equal on some networks and unequal on
882 * others.</li>
883 * </ul>
884 * <p>This problem is fixed in Android 4.0 (Ice Cream Sandwich). In that
885 * release, URLs are only equal if their host names are equal (ignoring
886 * case).
Piotr Jastrzebski51b1b692015-02-16 15:01:09 +0000887 *
888 * @param obj the URL to compare against.
Yi Kong3a6411e2016-06-14 11:25:41 +0100889 * @return {@code true} if the objects are the same;
890 * {@code false} otherwise.
Piotr Jastrzebski51b1b692015-02-16 15:01:09 +0000891 */
892 public boolean equals(Object obj) {
893 if (!(obj instanceof URL))
894 return false;
895 URL u2 = (URL)obj;
896
897 return handler.equals(this, u2);
898 }
899
900 /**
901 * Creates an integer suitable for hash table indexing.<p>
902 *
903 * The hash code is based upon all the URL components relevant for URL
904 * comparison. As such, this operation is a blocking operation.<p>
905 *
Yi Kong3a6411e2016-06-14 11:25:41 +0100906 * @return a hash code for this {@code URL}.
Piotr Jastrzebski51b1b692015-02-16 15:01:09 +0000907 */
908 public synchronized int hashCode() {
909 if (hashCode != -1)
910 return hashCode;
911
912 hashCode = handler.hashCode(this);
913 return hashCode;
914 }
915
916 /**
917 * Compares two URLs, excluding the fragment component.<p>
918 *
Yi Kong3a6411e2016-06-14 11:25:41 +0100919 * Returns {@code true} if this {@code URL} and the
920 * {@code other} argument are equal without taking the
Piotr Jastrzebski51b1b692015-02-16 15:01:09 +0000921 * fragment component into consideration.
922 *
Yi Kong3a6411e2016-06-14 11:25:41 +0100923 * @param other the {@code URL} to compare against.
924 * @return {@code true} if they reference the same remote object;
925 * {@code false} otherwise.
Piotr Jastrzebski51b1b692015-02-16 15:01:09 +0000926 */
927 public boolean sameFile(URL other) {
928 return handler.sameFile(this, other);
929 }
930
931 /**
Yi Kong3a6411e2016-06-14 11:25:41 +0100932 * Constructs a string representation of this {@code URL}. The
933 * string is created by calling the {@code toExternalForm}
Piotr Jastrzebski51b1b692015-02-16 15:01:09 +0000934 * method of the stream protocol handler for this object.
935 *
936 * @return a string representation of this object.
937 * @see java.net.URL#URL(java.lang.String, java.lang.String, int,
938 * java.lang.String)
939 * @see java.net.URLStreamHandler#toExternalForm(java.net.URL)
940 */
941 public String toString() {
942 return toExternalForm();
943 }
944
945 /**
Yi Kong3a6411e2016-06-14 11:25:41 +0100946 * Constructs a string representation of this {@code URL}. The
947 * string is created by calling the {@code toExternalForm}
Piotr Jastrzebski51b1b692015-02-16 15:01:09 +0000948 * method of the stream protocol handler for this object.
949 *
950 * @return a string representation of this object.
951 * @see java.net.URL#URL(java.lang.String, java.lang.String,
952 * int, java.lang.String)
953 * @see java.net.URLStreamHandler#toExternalForm(java.net.URL)
954 */
955 public String toExternalForm() {
956 return handler.toExternalForm(this);
957 }
958
959 /**
960 * Returns a {@link java.net.URI} equivalent to this URL.
Yi Kong3a6411e2016-06-14 11:25:41 +0100961 * This method functions in the same way as {@code new URI (this.toString())}.
Piotr Jastrzebski51b1b692015-02-16 15:01:09 +0000962 * <p>Note, any URL instance that complies with RFC 2396 can be converted
963 * to a URI. However, some URLs that are not strictly in compliance
964 * can not be converted to a URI.
965 *
966 * @exception URISyntaxException if this URL is not formatted strictly according to
967 * to RFC2396 and cannot be converted to a URI.
968 *
969 * @return a URI instance equivalent to this URL.
970 * @since 1.5
971 */
972 public URI toURI() throws URISyntaxException {
973 return new URI (toString());
974 }
975
976 /**
977 * Returns a {@link java.net.URLConnection URLConnection} instance that
978 * represents a connection to the remote object referred to by the
979 * {@code URL}.
980 *
981 * <P>A new instance of {@linkplain java.net.URLConnection URLConnection} is
982 * created every time when invoking the
983 * {@linkplain java.net.URLStreamHandler#openConnection(URL)
984 * URLStreamHandler.openConnection(URL)} method of the protocol handler for
985 * this URL.</P>
986 *
987 * <P>It should be noted that a URLConnection instance does not establish
988 * the actual network connection on creation. This will happen only when
989 * calling {@linkplain java.net.URLConnection#connect() URLConnection.connect()}.</P>
990 *
991 * <P>If for the URL's protocol (such as HTTP or JAR), there
992 * exists a public, specialized URLConnection subclass belonging
993 * to one of the following packages or one of their subpackages:
994 * java.lang, java.io, java.util, java.net, the connection
995 * returned will be of that subclass. For example, for HTTP an
996 * HttpURLConnection will be returned, and for JAR a
997 * JarURLConnection will be returned.</P>
998 *
999 * @return a {@link java.net.URLConnection URLConnection} linking
1000 * to the URL.
1001 * @exception IOException if an I/O exception occurs.
1002 * @see java.net.URL#URL(java.lang.String, java.lang.String,
1003 * int, java.lang.String)
1004 */
1005 public URLConnection openConnection() throws java.io.IOException {
Stewart Chaof88008e2016-04-10 15:28:42 -04001006 java.util.SeempLog.record_str(91, "URL:"+query);
Piotr Jastrzebski51b1b692015-02-16 15:01:09 +00001007 return handler.openConnection(this);
1008 }
1009
1010 /**
1011 * Same as {@link #openConnection()}, except that the connection will be
1012 * made through the specified proxy; Protocol handlers that do not
1013 * support proxing will ignore the proxy parameter and make a
1014 * normal connection.
1015 *
1016 * Invoking this method preempts the system's default ProxySelector
1017 * settings.
1018 *
1019 * @param proxy the Proxy through which this connection
1020 * will be made. If direct connection is desired,
1021 * Proxy.NO_PROXY should be specified.
Yi Kong3a6411e2016-06-14 11:25:41 +01001022 * @return a {@code URLConnection} to the URL.
Piotr Jastrzebski51b1b692015-02-16 15:01:09 +00001023 * @exception IOException if an I/O exception occurs.
1024 * @exception SecurityException if a security manager is present
1025 * and the caller doesn't have permission to connect
1026 * to the proxy.
1027 * @exception IllegalArgumentException will be thrown if proxy is null,
1028 * or proxy has the wrong type
1029 * @exception UnsupportedOperationException if the subclass that
1030 * implements the protocol handler doesn't support
1031 * this method.
1032 * @see java.net.URL#URL(java.lang.String, java.lang.String,
1033 * int, java.lang.String)
1034 * @see java.net.URLConnection
1035 * @see java.net.URLStreamHandler#openConnection(java.net.URL,
1036 * java.net.Proxy)
1037 * @since 1.5
1038 */
1039 public URLConnection openConnection(Proxy proxy)
1040 throws java.io.IOException {
1041 if (proxy == null) {
1042 throw new IllegalArgumentException("proxy can not be null");
1043 }
1044
1045 // Create a copy of Proxy as a security measure
1046 Proxy p = proxy == Proxy.NO_PROXY ? Proxy.NO_PROXY : sun.net.ApplicationProxy.create(proxy);
1047 SecurityManager sm = System.getSecurityManager();
1048 if (p.type() != Proxy.Type.DIRECT && sm != null) {
1049 InetSocketAddress epoint = (InetSocketAddress) p.address();
1050 if (epoint.isUnresolved())
1051 sm.checkConnect(epoint.getHostName(), epoint.getPort());
1052 else
1053 sm.checkConnect(epoint.getAddress().getHostAddress(),
1054 epoint.getPort());
1055 }
1056 return handler.openConnection(this, p);
1057 }
1058
1059 /**
Yi Kong3a6411e2016-06-14 11:25:41 +01001060 * Opens a connection to this {@code URL} and returns an
1061 * {@code InputStream} for reading from that connection. This
Piotr Jastrzebski51b1b692015-02-16 15:01:09 +00001062 * method is a shorthand for:
1063 * <blockquote><pre>
1064 * openConnection().getInputStream()
1065 * </pre></blockquote>
1066 *
1067 * @return an input stream for reading from the URL connection.
1068 * @exception IOException if an I/O exception occurs.
1069 * @see java.net.URL#openConnection()
1070 * @see java.net.URLConnection#getInputStream()
1071 */
1072 public final InputStream openStream() throws java.io.IOException {
1073 return openConnection().getInputStream();
1074 }
1075
1076 /**
1077 * Gets the contents of this URL. This method is a shorthand for:
1078 * <blockquote><pre>
1079 * openConnection().getContent()
1080 * </pre></blockquote>
1081 *
1082 * @return the contents of this URL.
1083 * @exception IOException if an I/O exception occurs.
1084 * @see java.net.URLConnection#getContent()
1085 */
1086 public final Object getContent() throws java.io.IOException {
1087 return openConnection().getContent();
1088 }
1089
1090 /**
1091 * Gets the contents of this URL. This method is a shorthand for:
1092 * <blockquote><pre>
1093 * openConnection().getContent(Class[])
1094 * </pre></blockquote>
1095 *
1096 * @param classes an array of Java types
1097 * @return the content object of this URL that is the first match of
1098 * the types specified in the classes array.
1099 * null if none of the requested types are supported.
1100 * @exception IOException if an I/O exception occurs.
1101 * @see java.net.URLConnection#getContent(Class[])
1102 * @since 1.3
1103 */
1104 public final Object getContent(Class[] classes)
1105 throws java.io.IOException {
1106 return openConnection().getContent(classes);
1107 }
1108
1109 /**
1110 * The URLStreamHandler factory.
1111 */
1112 static URLStreamHandlerFactory factory;
1113
1114 /**
Yi Kong3a6411e2016-06-14 11:25:41 +01001115 * Sets an application's {@code URLStreamHandlerFactory}.
Piotr Jastrzebski51b1b692015-02-16 15:01:09 +00001116 * This method can be called at most once in a given Java Virtual
1117 * Machine.
1118 *
Yi Kong3a6411e2016-06-14 11:25:41 +01001119 *<p> The {@code URLStreamHandlerFactory} instance is used to
Piotr Jastrzebski51b1b692015-02-16 15:01:09 +00001120 *construct a stream protocol handler from a protocol name.
1121 *
1122 * <p> If there is a security manager, this method first calls
Yi Kong3a6411e2016-06-14 11:25:41 +01001123 * the security manager's {@code checkSetFactory} method
Piotr Jastrzebski51b1b692015-02-16 15:01:09 +00001124 * to ensure the operation is allowed.
1125 * This could result in a SecurityException.
1126 *
1127 * @param fac the desired factory.
1128 * @exception Error if the application has already set a factory.
1129 * @exception SecurityException if a security manager exists and its
Yi Kong3a6411e2016-06-14 11:25:41 +01001130 * {@code checkSetFactory} method doesn't allow
Piotr Jastrzebski51b1b692015-02-16 15:01:09 +00001131 * the operation.
1132 * @see java.net.URL#URL(java.lang.String, java.lang.String,
1133 * int, java.lang.String)
1134 * @see java.net.URLStreamHandlerFactory
1135 * @see SecurityManager#checkSetFactory
1136 */
1137 public static void setURLStreamHandlerFactory(URLStreamHandlerFactory fac) {
1138 synchronized (streamHandlerLock) {
1139 if (factory != null) {
1140 throw new Error("factory already defined");
1141 }
1142 SecurityManager security = System.getSecurityManager();
1143 if (security != null) {
1144 security.checkSetFactory();
1145 }
1146 handlers.clear();
1147 factory = fac;
1148 }
1149 }
1150
1151 /**
1152 * A table of protocol handlers.
1153 */
Yi Kong7f4b1b82016-08-11 14:02:43 +01001154 static Hashtable<String,URLStreamHandler> handlers = new Hashtable<>();
Piotr Jastrzebski51b1b692015-02-16 15:01:09 +00001155 private static Object streamHandlerLock = new Object();
1156
Piotr Jastrzebski51b1b692015-02-16 15:01:09 +00001157 /**
1158 * Returns the Stream Handler.
1159 * @param protocol the protocol to use
1160 */
1161 static URLStreamHandler getURLStreamHandler(String protocol) {
1162
Yi Kong7f4b1b82016-08-11 14:02:43 +01001163 URLStreamHandler handler = handlers.get(protocol);
Piotr Jastrzebski51b1b692015-02-16 15:01:09 +00001164 if (handler == null) {
1165
1166 boolean checkedWithFactory = false;
1167
1168 // Use the factory (if any)
1169 if (factory != null) {
1170 handler = factory.createURLStreamHandler(protocol);
1171 checkedWithFactory = true;
1172 }
1173
1174 // Try java protocol handler
1175 if (handler == null) {
Victor Change376c072018-01-04 14:27:34 +00001176 // Android-changed: Android doesn't need AccessController.
1177 // Remove unnecessary use of reflection for sun classes
1178 /*
1179 packagePrefixList
1180 = java.security.AccessController.doPrivileged(
1181 new sun.security.action.GetPropertyAction(
1182 protocolPathProp,""));
1183 if (packagePrefixList != "") {
1184 packagePrefixList += "|";
1185 }
1186
1187 // REMIND: decide whether to allow the "null" class prefix
1188 // or not.
1189 packagePrefixList += "sun.net.www.protocol";
1190 */
Narayan Kamathf861f1e2015-12-03 14:50:09 +00001191 final String packagePrefixList = System.getProperty(protocolPathProp,"");
Tobias Thierer7dbee732018-02-08 21:45:09 +00001192
1193 StringTokenizer packagePrefixIter =
1194 new StringTokenizer(packagePrefixList, "|");
Piotr Jastrzebski51b1b692015-02-16 15:01:09 +00001195
1196 while (handler == null &&
1197 packagePrefixIter.hasMoreTokens()) {
1198
Victor Change376c072018-01-04 14:27:34 +00001199 String packagePrefix =
1200 packagePrefixIter.nextToken().trim();
Piotr Jastrzebski51b1b692015-02-16 15:01:09 +00001201 try {
Yi Kong7f4b1b82016-08-11 14:02:43 +01001202 String clsName = packagePrefix + "." + protocol +
1203 ".Handler";
1204 Class<?> cls = null;
Piotr Jastrzebski51b1b692015-02-16 15:01:09 +00001205 try {
Piotr Jastrzebski51b1b692015-02-16 15:01:09 +00001206 ClassLoader cl = ClassLoader.getSystemClassLoader();
Victor Change376c072018-01-04 14:27:34 +00001207 // BEGIN Android-changed: Fall back to thread's contextClassLoader.
1208 // http://b/25897689
Narayan Kamathf861f1e2015-12-03 14:50:09 +00001209 cls = Class.forName(clsName, true, cl);
1210 } catch (ClassNotFoundException e) {
1211 ClassLoader contextLoader = Thread.currentThread().getContextClassLoader();
1212 if (contextLoader != null) {
1213 cls = Class.forName(clsName, true, contextLoader);
Piotr Jastrzebski51b1b692015-02-16 15:01:09 +00001214 }
Victor Change376c072018-01-04 14:27:34 +00001215 // END Android-changed: Fall back to thread's contextClassLoader.
Piotr Jastrzebski51b1b692015-02-16 15:01:09 +00001216 }
1217 if (cls != null) {
1218 handler =
1219 (URLStreamHandler)cls.newInstance();
1220 }
Narayan Kamathf861f1e2015-12-03 14:50:09 +00001221 } catch (ReflectiveOperationException ignored) {
Piotr Jastrzebski51b1b692015-02-16 15:01:09 +00001222 }
1223 }
1224 }
1225
Tobias Thierer7dbee732018-02-08 21:45:09 +00001226 // BEGIN Android-added: Custom built-in URLStreamHandlers for http, https.
Przemyslaw Szczepaniak320a3d32015-07-15 16:34:29 +01001227 // Fallback to built-in stream handler.
Przemyslaw Szczepaniak320a3d32015-07-15 16:34:29 +01001228 if (handler == null) {
1229 try {
Tobias Thierer7dbee732018-02-08 21:45:09 +00001230 handler = createBuiltinHandler(protocol);
Przemyslaw Szczepaniak320a3d32015-07-15 16:34:29 +01001231 } catch (Exception e) {
1232 throw new AssertionError(e);
1233 }
1234 }
Tobias Thierer7dbee732018-02-08 21:45:09 +00001235 // END Android-added: Custom built-in URLStreamHandlers for http, https.
Przemyslaw Szczepaniak320a3d32015-07-15 16:34:29 +01001236
Piotr Jastrzebski51b1b692015-02-16 15:01:09 +00001237 synchronized (streamHandlerLock) {
1238
1239 URLStreamHandler handler2 = null;
1240
1241 // Check again with hashtable just in case another
1242 // thread created a handler since we last checked
Yi Kong7f4b1b82016-08-11 14:02:43 +01001243 handler2 = handlers.get(protocol);
Piotr Jastrzebski51b1b692015-02-16 15:01:09 +00001244
1245 if (handler2 != null) {
1246 return handler2;
1247 }
1248
1249 // Check with factory if another thread set a
1250 // factory since our last check
1251 if (!checkedWithFactory && factory != null) {
1252 handler2 = factory.createURLStreamHandler(protocol);
1253 }
1254
1255 if (handler2 != null) {
1256 // The handler from the factory must be given more
1257 // importance. Discard the default handler that
1258 // this thread created.
1259 handler = handler2;
1260 }
1261
1262 // Insert this handler into the hashtable
1263 if (handler != null) {
1264 handlers.put(protocol, handler);
1265 }
1266
1267 }
1268 }
1269
1270 return handler;
1271
1272 }
1273
Tobias Thierer7dbee732018-02-08 21:45:09 +00001274 // BEGIN Android-added: Custom built-in URLStreamHandlers for http, https.
1275 /**
1276 * Returns an instance of the built-in handler for the given protocol, or null if none exists.
1277 */
1278 private static URLStreamHandler createBuiltinHandler(String protocol)
1279 throws ClassNotFoundException, InstantiationException, IllegalAccessException {
1280 URLStreamHandler handler = null;
1281 if (protocol.equals("file")) {
1282 handler = new sun.net.www.protocol.file.Handler();
1283 } else if (protocol.equals("ftp")) {
1284 handler = new sun.net.www.protocol.ftp.Handler();
1285 } else if (protocol.equals("jar")) {
1286 handler = new sun.net.www.protocol.jar.Handler();
1287 } else if (protocol.equals("http")) {
1288 handler = (URLStreamHandler)Class.
1289 forName("com.android.okhttp.HttpHandler").newInstance();
1290 } else if (protocol.equals("https")) {
1291 handler = (URLStreamHandler)Class.
1292 forName("com.android.okhttp.HttpsHandler").newInstance();
1293 }
1294 return handler;
1295 }
1296
1297 /** Names of implementation classes returned by {@link #createBuiltinHandler(String)}. */
1298 private static Set<String> createBuiltinHandlerClassNames() {
1299 Set<String> result = new HashSet<>();
1300 // Refer to class names rather than classes to avoid needlessly triggering <clinit>.
1301 result.add("sun.net.www.protocol.file.Handler");
1302 result.add("sun.net.www.protocol.ftp.Handler");
1303 result.add("sun.net.www.protocol.jar.Handler");
1304 result.add("com.android.okhttp.HttpHandler");
1305 result.add("com.android.okhttp.HttpsHandler");
1306 return Collections.unmodifiableSet(result);
1307 }
1308 // END Android-added: Custom built-in URLStreamHandlers for http, https.
1309
1310 /**
1311 * @serialField protocol String
1312 *
1313 * @serialField host String
1314 *
1315 * @serialField port int
1316 *
1317 * @serialField authority String
1318 *
1319 * @serialField file String
1320 *
1321 * @serialField ref String
1322 *
1323 * @serialField hashCode int
1324 *
1325 */
1326 private static final ObjectStreamField[] serialPersistentFields = {
1327 new ObjectStreamField("protocol", String.class),
1328 new ObjectStreamField("host", String.class),
1329 new ObjectStreamField("port", int.class),
1330 new ObjectStreamField("authority", String.class),
1331 new ObjectStreamField("file", String.class),
1332 new ObjectStreamField("ref", String.class),
1333 // Android-changed: App compat: hashCode should not be serialized.
1334 // new ObjectStreamField("hashCode", int.class), };
1335 };
1336
Piotr Jastrzebski51b1b692015-02-16 15:01:09 +00001337 /**
1338 * WriteObject is called to save the state of the URL to an
1339 * ObjectOutputStream. The handler is not saved since it is
1340 * specific to this system.
1341 *
1342 * @serialData the default write object value. When read back in,
1343 * the reader must ensure that calling getURLStreamHandler with
1344 * the protocol variable returns a valid URLStreamHandler and
1345 * throw an IOException if it does not.
1346 */
1347 private synchronized void writeObject(java.io.ObjectOutputStream s)
1348 throws IOException
1349 {
1350 s.defaultWriteObject(); // write the fields
1351 }
1352
1353 /**
1354 * readObject is called to restore the state of the URL from the
1355 * stream. It reads the components of the URL and finds the local
1356 * stream handler.
1357 */
1358 private synchronized void readObject(java.io.ObjectInputStream s)
Tobias Thierer7dbee732018-02-08 21:45:09 +00001359 throws IOException, ClassNotFoundException {
1360 GetField gf = s.readFields();
1361 String protocol = (String)gf.get("protocol", null);
1362 if (getURLStreamHandler(protocol) == null) {
Piotr Jastrzebski51b1b692015-02-16 15:01:09 +00001363 throw new IOException("unknown protocol: " + protocol);
1364 }
Tobias Thierer7dbee732018-02-08 21:45:09 +00001365 String host = (String)gf.get("host", null);
1366 int port = gf.get("port", -1);
1367 String authority = (String)gf.get("authority", null);
1368 String file = (String)gf.get("file", null);
1369 String ref = (String)gf.get("ref", null);
1370 // Android-changed: App compat: hashCode should not be serialized.
1371 // int hashCode = gf.get("hashCode", -1);
1372 final int hashCode = -1;
1373 if (authority == null
1374 && ((host != null && host.length() > 0) || port != -1)) {
1375 if (host == null)
1376 host = "";
1377 authority = (port == -1) ? host : host + ":" + port;
1378 }
1379 tempState = new UrlDeserializedState(protocol, host, port, authority,
1380 file, ref, hashCode);
1381 }
1382
1383 /**
1384 * Replaces the de-serialized object with an URL object.
1385 *
1386 * @return a newly created object from the deserialzed state.
1387 *
1388 * @throws ObjectStreamException if a new object replacing this
1389 * object could not be created
1390 */
1391
1392 private Object readResolve() throws ObjectStreamException {
1393
1394 URLStreamHandler handler = null;
1395 // already been checked in readObject
1396 handler = getURLStreamHandler(tempState.getProtocol());
1397
1398 URL replacementURL = null;
1399 if (isBuiltinStreamHandler(handler.getClass().getName())) {
1400 replacementURL = fabricateNewURL();
1401 } else {
1402 replacementURL = setDeserializedFields(handler);
1403 }
1404 return replacementURL;
1405 }
1406
1407 private URL setDeserializedFields(URLStreamHandler handler) {
1408 URL replacementURL;
1409 String userInfo = null;
1410 String protocol = tempState.getProtocol();
1411 String host = tempState.getHost();
1412 int port = tempState.getPort();
1413 String authority = tempState.getAuthority();
1414 String file = tempState.getFile();
1415 String ref = tempState.getRef();
1416 int hashCode = tempState.getHashCode();
1417
Piotr Jastrzebski51b1b692015-02-16 15:01:09 +00001418
1419 // Construct authority part
Tobias Thierer7dbee732018-02-08 21:45:09 +00001420 if (authority == null
1421 && ((host != null && host.length() > 0) || port != -1)) {
Piotr Jastrzebski51b1b692015-02-16 15:01:09 +00001422 if (host == null)
1423 host = "";
1424 authority = (port == -1) ? host : host + ":" + port;
1425
1426 // Handle hosts with userInfo in them
1427 int at = host.lastIndexOf('@');
1428 if (at != -1) {
1429 userInfo = host.substring(0, at);
1430 host = host.substring(at+1);
1431 }
1432 } else if (authority != null) {
1433 // Construct user info part
1434 int ind = authority.indexOf('@');
1435 if (ind != -1)
1436 userInfo = authority.substring(0, ind);
1437 }
1438
1439 // Construct path and query part
Tobias Thierer7dbee732018-02-08 21:45:09 +00001440 String path = null;
1441 String query = null;
Piotr Jastrzebski51b1b692015-02-16 15:01:09 +00001442 if (file != null) {
1443 // Fix: only do this if hierarchical?
1444 int q = file.lastIndexOf('?');
1445 if (q != -1) {
1446 query = file.substring(q+1);
1447 path = file.substring(0, q);
1448 } else
1449 path = file;
1450 }
Tobias Thierer7dbee732018-02-08 21:45:09 +00001451
1452 // Set the object fields.
1453 this.protocol = protocol;
1454 this.host = host;
1455 this.port = port;
1456 this.file = file;
1457 this.authority = authority;
1458 this.ref = ref;
1459 this.hashCode = hashCode;
1460 this.handler = handler;
1461 this.query = query;
1462 this.path = path;
1463 this.userInfo = userInfo;
1464 replacementURL = this;
1465 return replacementURL;
1466 }
1467
1468 private URL fabricateNewURL()
1469 throws InvalidObjectException {
1470 // create URL string from deserialized object
1471 URL replacementURL = null;
1472 String urlString = tempState.reconstituteUrlString();
1473
1474 try {
1475 replacementURL = new URL(urlString);
1476 } catch (MalformedURLException mEx) {
1477 resetState();
1478 InvalidObjectException invoEx = new InvalidObjectException(
1479 "Malformed URL: " + urlString);
1480 invoEx.initCause(mEx);
1481 throw invoEx;
1482 }
1483 replacementURL.setSerializedHashCode(tempState.getHashCode());
1484 resetState();
1485 return replacementURL;
1486 }
1487
1488 private boolean isBuiltinStreamHandler(String handlerClassName) {
1489 // Android-changed: Some built-in handlers (eg. HttpHandler) are not in sun.net.www.protocol.
1490 // return (handlerClassName.startsWith(BUILTIN_HANDLERS_PREFIX));
1491 return BUILTIN_HANDLER_CLASS_NAMES.contains(handlerClassName);
1492 }
1493
1494 private void resetState() {
1495 this.protocol = null;
1496 this.host = null;
1497 this.port = -1;
1498 this.file = null;
1499 this.authority = null;
1500 this.ref = null;
1501 this.hashCode = -1;
1502 this.handler = null;
1503 this.query = null;
1504 this.path = null;
1505 this.userInfo = null;
1506 this.tempState = null;
1507 }
1508
1509 private void setSerializedHashCode(int hc) {
1510 this.hashCode = hc;
Piotr Jastrzebski51b1b692015-02-16 15:01:09 +00001511 }
Piotr Jastrzebski51b1b692015-02-16 15:01:09 +00001512}
1513
1514class Parts {
1515 String path, query, ref;
1516
Victor Change376c072018-01-04 14:27:34 +00001517 // Android-changed: App compat. Prepend '/' if host is null / empty.
1518 // Parts(String file)
Przemyslaw Szczepaniak06fc5c32015-07-21 17:20:27 +01001519 Parts(String file, String host) {
Piotr Jastrzebski51b1b692015-02-16 15:01:09 +00001520 int ind = file.indexOf('#');
1521 ref = ind < 0 ? null: file.substring(ind + 1);
1522 file = ind < 0 ? file: file.substring(0, ind);
1523 int q = file.lastIndexOf('?');
1524 if (q != -1) {
1525 query = file.substring(q+1);
1526 path = file.substring(0, q);
1527 } else {
1528 path = file;
1529 }
Victor Change376c072018-01-04 14:27:34 +00001530 // BEGIN Android-changed: App compat. Prepend '/' if host is null / empty.
Przemyslaw Szczepaniak06fc5c32015-07-21 17:20:27 +01001531 if (path != null && path.length() > 0 && path.charAt(0) != '/' &&
1532 host != null && !host.isEmpty()) {
Przemyslaw Szczepaniaka53c8892015-07-21 09:39:07 +01001533 path = '/' + path;
1534 }
Victor Change376c072018-01-04 14:27:34 +00001535 // END Android-changed: App compat. Prepend '/' if host is null / empty.
Piotr Jastrzebski51b1b692015-02-16 15:01:09 +00001536 }
1537
1538 String getPath() {
1539 return path;
1540 }
1541
1542 String getQuery() {
1543 return query;
1544 }
1545
1546 String getRef() {
1547 return ref;
1548 }
1549}
Tobias Thierer7dbee732018-02-08 21:45:09 +00001550
1551final class UrlDeserializedState {
1552 private final String protocol;
1553 private final String host;
1554 private final int port;
1555 private final String authority;
1556 private final String file;
1557 private final String ref;
1558 private final int hashCode;
1559
1560 public UrlDeserializedState(String protocol,
1561 String host, int port,
1562 String authority, String file,
1563 String ref, int hashCode) {
1564 this.protocol = protocol;
1565 this.host = host;
1566 this.port = port;
1567 this.authority = authority;
1568 this.file = file;
1569 this.ref = ref;
1570 this.hashCode = hashCode;
1571 }
1572
1573 String getProtocol() {
1574 return protocol;
1575 }
1576
1577 String getHost() {
1578 return host;
1579 }
1580
1581 String getAuthority () {
1582 return authority;
1583 }
1584
1585 int getPort() {
1586 return port;
1587 }
1588
1589 String getFile () {
1590 return file;
1591 }
1592
1593 String getRef () {
1594 return ref;
1595 }
1596
1597 int getHashCode () {
1598 return hashCode;
1599 }
1600
1601 String reconstituteUrlString() {
1602
1603 // pre-compute length of StringBuilder
1604 int len = protocol.length() + 1;
1605 if (authority != null && authority.length() > 0)
1606 len += 2 + authority.length();
1607 if (file != null) {
1608 len += file.length();
1609 }
1610 if (ref != null)
1611 len += 1 + ref.length();
1612 StringBuilder result = new StringBuilder(len);
1613 result.append(protocol);
1614 result.append(":");
1615 if (authority != null && authority.length() > 0) {
1616 result.append("//");
1617 result.append(authority);
1618 }
1619 if (file != null) {
1620 result.append(file);
1621 }
1622 if (ref != null) {
1623 result.append("#");
1624 result.append(ref);
1625 }
1626 return result.toString();
1627 }
1628}