1 /* 2 * Copyright (c) 1995, 2013, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package java.net; 27 28 import java.io.IOException; 29 import java.io.InputStream; 30 import java.security.AccessController; 31 import java.security.PrivilegedAction; 32 import java.util.ArrayList; 33 import java.util.Collection; 34 import java.util.Collections; 35 import java.util.Hashtable; 36 import java.util.List; 37 import java.util.Objects; 38 import java.util.ServiceLoader; 39 import java.util.Set; 40 import java.util.concurrent.CopyOnWriteArrayList; 41 42 import sun.security.util.SecurityConstants; 43 44 /** 45 * Class {@code URL} represents a Uniform Resource 46 * Locator, a pointer to a "resource" on the World 47 * Wide Web. A resource can be something as simple as a file or a 48 * directory, or it can be a reference to a more complicated object, 49 * such as a query to a database or to a search engine. More 50 * information on the types of URLs and their formats can be found at: 51 * <a href= 52 * "http://web.archive.org/web/20051219043731/http://archive.ncsa.uiuc.edu/SDG/Software/Mosaic/Demo/url-primer.html"> 53 * <i>Types of URL</i></a> 54 * <p> 55 * In general, a URL can be broken into several parts. Consider the 56 * following example: 57 * <blockquote><pre> 58 * http://www.example.com/docs/resource1.html 59 * </pre></blockquote> 60 * <p> 61 * The URL above indicates that the protocol to use is 62 * {@code http} (HyperText Transfer Protocol) and that the 63 * information resides on a host machine named 64 * {@code www.example.com}. The information on that host 65 * machine is named {@code /docs/resource1.html}. The exact 66 * meaning of this name on the host machine is both protocol 67 * dependent and host dependent. The information normally resides in 68 * a file, but it could be generated on the fly. This component of 69 * the URL is called the <i>path</i> component. 70 * <p> 71 * A URL can optionally specify a "port", which is the 72 * port number to which the TCP connection is made on the remote host 73 * machine. If the port is not specified, the default port for 74 * the protocol is used instead. For example, the default port for 75 * {@code http} is {@code 80}. An alternative port could be 76 * specified as: 77 * <blockquote><pre> 78 * http://www.example.com:1080/docs/resource1.html 79 * </pre></blockquote> 80 * <p> 81 * The syntax of {@code URL} is defined by <a 82 * href="http://www.ietf.org/rfc/rfc2396.txt"><i>RFC 2396: Uniform 83 * Resource Identifiers (URI): Generic Syntax</i></a>, amended by <a 84 * href="http://www.ietf.org/rfc/rfc2732.txt"><i>RFC 2732: Format for 85 * Literal IPv6 Addresses in URLs</i></a>. The Literal IPv6 address format 86 * also supports scope_ids. The syntax and usage of scope_ids is described 87 * <a href="Inet6Address.html#scoped">here</a>. 88 * <p> 89 * A URL may have appended to it a "fragment", also known 90 * as a "ref" or a "reference". The fragment is indicated by the sharp 91 * sign character "#" followed by more characters. For example, 92 * <blockquote><pre> 93 * http://java.sun.com/index.html#chapter1 94 * </pre></blockquote> 95 * <p> 96 * This fragment is not technically part of the URL. Rather, it 97 * indicates that after the specified resource is retrieved, the 98 * application is specifically interested in that part of the 99 * document that has the tag {@code chapter1} attached to it. The 100 * meaning of a tag is resource specific. 101 * <p> 102 * An application can also specify a "relative URL", 103 * which contains only enough information to reach the resource 104 * relative to another URL. Relative URLs are frequently used within 105 * HTML pages. For example, if the contents of the URL: 106 * <blockquote><pre> 107 * http://java.sun.com/index.html 108 * </pre></blockquote> 109 * contained within it the relative URL: 110 * <blockquote><pre> 111 * FAQ.html 112 * </pre></blockquote> 113 * it would be a shorthand for: 114 * <blockquote><pre> 115 * http://java.sun.com/FAQ.html 116 * </pre></blockquote> 117 * <p> 118 * The relative URL need not specify all the components of a URL. If 119 * the protocol, host name, or port number is missing, the value is 120 * inherited from the fully specified URL. The file component must be 121 * specified. The optional fragment is not inherited. 122 * <p> 123 * The URL class does not itself encode or decode any URL components 124 * according to the escaping mechanism defined in RFC2396. It is the 125 * responsibility of the caller to encode any fields, which need to be 126 * escaped prior to calling URL, and also to decode any escaped fields, 127 * that are returned from URL. Furthermore, because URL has no knowledge 128 * of URL escaping, it does not recognise equivalence between the encoded 129 * or decoded form of the same URL. For example, the two URLs:<br> 130 * <pre> http://foo.com/hello world/ and http://foo.com/hello%20world</pre> 131 * would be considered not equal to each other. 132 * <p> 133 * Note, the {@link java.net.URI} class does perform escaping of its 134 * component fields in certain circumstances. The recommended way 135 * to manage the encoding and decoding of URLs is to use {@link java.net.URI}, 136 * and to convert between these two classes using {@link #toURI()} and 137 * {@link URI#toURL()}. 138 * <p> 139 * The {@link URLEncoder} and {@link URLDecoder} classes can also be 140 * used, but only for HTML form encoding, which is not the same 141 * as the encoding scheme defined in RFC2396. 142 * 143 * @author James Gosling 144 * @since 1.0 145 */ 146 public final class URL implements java.io.Serializable { 147 148 static final long serialVersionUID = -7627629688361524110L; 149 150 /** 151 * The property which specifies the package prefix list to be scanned 152 * for protocol handlers. The value of this property (if any) should 153 * be a vertical bar delimited list of package names to search through 154 * for a protocol handler to load. The policy of this class is that 155 * all protocol handlers will be in a class called <protocolname>.Handler, 156 * and each package in the list is examined in turn for a matching 157 * handler. If none are found (or the property is not specified), the 158 * default package prefix, sun.net.www.protocol, is used. The search 159 * proceeds from the first package in the list to the last and stops 160 * when a match is found. 161 */ 162 private static final String protocolPathProp = "java.protocol.handler.pkgs"; 163 164 /** 165 * The protocol to use (ftp, http, nntp, ... etc.) . 166 * @serial 167 */ 168 private String protocol; 169 170 /** 171 * The host name to connect to. 172 * @serial 173 */ 174 private String host; 175 176 /** 177 * The protocol port to connect to. 178 * @serial 179 */ 180 private int port = -1; 181 182 /** 183 * The specified file name on that host. {@code file} is 184 * defined as {@code path[?query]} 185 * @serial 186 */ 187 private String file; 188 189 /** 190 * The query part of this URL. 191 */ 192 private transient String query; 193 194 /** 195 * The authority part of this URL. 196 * @serial 197 */ 198 private String authority; 199 200 /** 201 * The path part of this URL. 202 */ 203 private transient String path; 204 205 /** 206 * The userinfo part of this URL. 207 */ 208 private transient String userInfo; 209 210 /** 211 * # reference. 212 * @serial 213 */ 214 private String ref; 215 216 /** 217 * The host's IP address, used in equals and hashCode. 218 * Computed on demand. An uninitialized or unknown hostAddress is null. 219 */ 220 transient InetAddress hostAddress; 221 222 /** 223 * The URLStreamHandler for this URL. 224 */ 225 transient URLStreamHandler handler; 226 227 /* Our hash code. 228 * @serial 229 */ 230 private int hashCode = -1; 231 232 /** 233 * Creates a {@code URL} object from the specified 234 * {@code protocol}, {@code host}, {@code port} 235 * number, and {@code file}.<p> 236 * 237 * {@code host} can be expressed as a host name or a literal 238 * IP address. If IPv6 literal address is used, it should be 239 * enclosed in square brackets ({@code '['} and {@code ']'}), as 240 * specified by <a 241 * href="http://www.ietf.org/rfc/rfc2732.txt">RFC 2732</a>; 242 * However, the literal IPv6 address format defined in <a 243 * href="http://www.ietf.org/rfc/rfc2373.txt"><i>RFC 2373: IP 244 * Version 6 Addressing Architecture</i></a> is also accepted.<p> 245 * 246 * Specifying a {@code port} number of {@code -1} 247 * indicates that the URL should use the default port for the 248 * protocol.<p> 249 * 250 * If this is the first URL object being created with the specified 251 * protocol, a <i>stream protocol handler</i> object, an instance of 252 * class {@code URLStreamHandler}, is created for that protocol: 253 * <ol> 254 * <li>If the application has previously set up an instance of 255 * {@code URLStreamHandlerFactory}, through {@linkplain 256 * #setURLStreamHandlerFactory(URLStreamHandlerFactory) 257 * setURLStreamHandlerFactory}, as the stream handler factory, 258 * then the {@code createURLStreamHandler} method of that instance 259 * is called with the protocol string as an argument to create the 260 * stream protocol handler. 261 * <li>If no {@code URLStreamHandlerFactory} has yet been set up, 262 * or if the factory's {@code createURLStreamHandler} method 263 * returns {@code null}, then the list of factories, set through 264 * {@linkplain #addURLStreamHandlerFactory(URLStreamHandlerFactory) 265 * addURLStreamHandlerFactory} is consulted. The {@code 266 * createURLStreamHandler} method of each factory is invoked, in 267 * registration order, with the protocol string, until a factory returns 268 * non-null, or all the factories in the list have been exhausted. 269 * <li>If the previous step fails to find a protocol handler, then the 270 * {@linkplain java.util.ServiceLoader ServiceLoader} mechanism is used 271 * to locate a {@code URLStreamHandlerFactory} provider using the system 272 * class loader. The ordering that providers are located is 273 * implementation specific, and an implementation is free to cache the 274 * located providers. A {@linkplain java.util.ServiceConfigurationError 275 * ServiceConfigurationError}, {@code Error} or {@code RuntimeException} 276 * thrown from the {@code createURLStreamHandler}, if encountered, will 277 * be propagated to the calling thread. The {@code 278 * createURLStreamHandler} method of each provider, if instantiated, is 279 * invoked, with the protocol string, until a provider returns non-null, 280 * or all providers have been exhausted. 281 * <li>If the previous step fails to find a protocol handler, then the 282 * constructor tries to load a built-in protocol handler. 283 * If this class does not exist, or if the class exists but it is not a 284 * subclass of {@code URLStreamHandler}, then a 285 * {@code MalformedURLException} is thrown. 286 * </ol> 287 * 288 * <p>Protocol handlers for the following protocols are guaranteed 289 * to exist on the search path :- 290 * <blockquote><pre> 291 * http, https, file, and jar 292 * </pre></blockquote> 293 * Protocol handlers for additional protocols may also be available. Some 294 * protocols, that are fundamental to the platform, may have restrictions 295 * around when, or if, their built-in handlers can be overridden. 296 * 297 * <p>No validation of the inputs is performed by this constructor. 298 * 299 * @param protocol the name of the protocol to use. 300 * @param host the name of the host. 301 * @param port the port number on the host. 302 * @param file the file on the host 303 * @exception MalformedURLException if an unknown protocol is specified. 304 * @see java.lang.System#getProperty(java.lang.String) 305 * @see java.net.URL#setURLStreamHandlerFactory( 306 * java.net.URLStreamHandlerFactory) 307 * @see java.net.URLStreamHandler 308 * @see java.net.URLStreamHandlerFactory#createURLStreamHandler( 309 * java.lang.String) 310 */ 311 public URL(String protocol, String host, int port, String file) 312 throws MalformedURLException 313 { 314 this(protocol, host, port, file, null); 315 } 316 317 /** 318 * Creates a URL from the specified {@code protocol} 319 * name, {@code host} name, and {@code file} name. The 320 * default port for the specified protocol is used. 321 * <p> 322 * This method is equivalent to calling the four-argument 323 * constructor with the arguments being {@code protocol}, 324 * {@code host}, {@code -1}, and {@code file}. 325 * 326 * No validation of the inputs is performed by this constructor. 327 * 328 * @param protocol the name of the protocol to use. 329 * @param host the name of the host. 330 * @param file the file on the host. 331 * @exception MalformedURLException if an unknown protocol is specified. 332 * @see java.net.URL#URL(java.lang.String, java.lang.String, 333 * int, java.lang.String) 334 */ 335 public URL(String protocol, String host, String file) 336 throws MalformedURLException { 337 this(protocol, host, -1, file); 338 } 339 340 /** 341 * Creates a {@code URL} object from the specified 342 * {@code protocol}, {@code host}, {@code port} 343 * number, {@code file}, and {@code handler}. Specifying 344 * a {@code port} number of {@code -1} indicates that 345 * the URL should use the default port for the protocol. Specifying 346 * a {@code handler} of {@code null} indicates that the URL 347 * should use a default stream handler for the protocol, as outlined 348 * for: 349 * java.net.URL#URL(java.lang.String, java.lang.String, int, 350 * java.lang.String) 351 * 352 * <p>If the handler is not null and there is a security manager, 353 * the security manager's {@code checkPermission} 354 * method is called with a 355 * {@code NetPermission("specifyStreamHandler")} permission. 356 * This may result in a SecurityException. 357 * 358 * No validation of the inputs is performed by this constructor. 359 * 360 * @param protocol the name of the protocol to use. 361 * @param host the name of the host. 362 * @param port the port number on the host. 363 * @param file the file on the host 364 * @param handler the stream handler for the URL. 365 * @exception MalformedURLException if an unknown protocol is specified. 366 * @exception SecurityException 367 * if a security manager exists and its 368 * {@code checkPermission} method doesn't allow 369 * specifying a stream handler explicitly. 370 * @see java.lang.System#getProperty(java.lang.String) 371 * @see java.net.URL#setURLStreamHandlerFactory( 372 * java.net.URLStreamHandlerFactory) 373 * @see java.net.URLStreamHandler 374 * @see java.net.URLStreamHandlerFactory#createURLStreamHandler( 375 * java.lang.String) 376 * @see SecurityManager#checkPermission 377 * @see java.net.NetPermission 378 */ 379 public URL(String protocol, String host, int port, String file, 380 URLStreamHandler handler) throws MalformedURLException { 381 if (handler != null) { 382 SecurityManager sm = System.getSecurityManager(); 383 if (sm != null) { 384 // check for permission to specify a handler 385 checkSpecifyHandler(sm); 386 } 387 } 388 389 protocol = protocol.toLowerCase(); 390 this.protocol = protocol; 391 if (host != null) { 392 393 /** 394 * if host is a literal IPv6 address, 395 * we will make it conform to RFC 2732 396 */ 397 if (host.indexOf(':') >= 0 && !host.startsWith("[")) { 398 host = "["+host+"]"; 399 } 400 this.host = host; 401 402 if (port < -1) { 403 throw new MalformedURLException("Invalid port number :" + 404 port); 405 } 406 this.port = port; 407 authority = (port == -1) ? host : host + ":" + port; 408 } 409 410 Parts parts = new Parts(file); 411 path = parts.getPath(); 412 query = parts.getQuery(); 413 414 if (query != null) { 415 this.file = path + "?" + query; 416 } else { 417 this.file = path; 418 } 419 ref = parts.getRef(); 420 421 // Note: we don't do validation of the URL here. Too risky to change 422 // right now, but worth considering for future reference. -br 423 if (handler == null && 424 (handler = getURLStreamHandler(protocol)) == null) { 425 throw new MalformedURLException("unknown protocol: " + protocol); 426 } 427 this.handler = handler; 428 } 429 430 /** 431 * Creates a {@code URL} object from the {@code String} 432 * representation. 433 * <p> 434 * This constructor is equivalent to a call to the two-argument 435 * constructor with a {@code null} first argument. 436 * 437 * @param spec the {@code String} to parse as a URL. 438 * @exception MalformedURLException if no protocol is specified, or an 439 * unknown protocol is found, or {@code spec} is {@code null}. 440 * @see java.net.URL#URL(java.net.URL, java.lang.String) 441 */ 442 public URL(String spec) throws MalformedURLException { 443 this(null, spec); 444 } 445 446 /** 447 * Creates a URL by parsing the given spec within a specified context. 448 * 449 * The new URL is created from the given context URL and the spec 450 * argument as described in 451 * RFC2396 "Uniform Resource Identifiers : Generic * Syntax" : 452 * <blockquote><pre> 453 * <scheme>://<authority><path>?<query>#<fragment> 454 * </pre></blockquote> 455 * The reference is parsed into the scheme, authority, path, query and 456 * fragment parts. If the path component is empty and the scheme, 457 * authority, and query components are undefined, then the new URL is a 458 * reference to the current document. Otherwise, the fragment and query 459 * parts present in the spec are used in the new URL. 460 * <p> 461 * If the scheme component is defined in the given spec and does not match 462 * the scheme of the context, then the new URL is created as an absolute 463 * URL based on the spec alone. Otherwise the scheme component is inherited 464 * from the context URL. 465 * <p> 466 * If the authority component is present in the spec then the spec is 467 * treated as absolute and the spec authority and path will replace the 468 * context authority and path. If the authority component is absent in the 469 * spec then the authority of the new URL will be inherited from the 470 * context. 471 * <p> 472 * If the spec's path component begins with a slash character 473 * "/" then the 474 * path is treated as absolute and the spec path replaces the context path. 475 * <p> 476 * Otherwise, the path is treated as a relative path and is appended to the 477 * context path, as described in RFC2396. Also, in this case, 478 * the path is canonicalized through the removal of directory 479 * changes made by occurrences of ".." and ".". 480 * <p> 481 * For a more detailed description of URL parsing, refer to RFC2396. 482 * 483 * @param context the context in which to parse the specification. 484 * @param spec the {@code String} to parse as a URL. 485 * @exception MalformedURLException if no protocol is specified, or an 486 * unknown protocol is found, or {@code spec} is {@code null}. 487 * @see java.net.URL#URL(java.lang.String, java.lang.String, 488 * int, java.lang.String) 489 * @see java.net.URLStreamHandler 490 * @see java.net.URLStreamHandler#parseURL(java.net.URL, 491 * java.lang.String, int, int) 492 */ 493 public URL(URL context, String spec) throws MalformedURLException { 494 this(context, spec, null); 495 } 496 497 /** 498 * Creates a URL by parsing the given spec with the specified handler 499 * within a specified context. If the handler is null, the parsing 500 * occurs as with the two argument constructor. 501 * 502 * @param context the context in which to parse the specification. 503 * @param spec the {@code String} to parse as a URL. 504 * @param handler the stream handler for the URL. 505 * @exception MalformedURLException if no protocol is specified, or an 506 * unknown protocol is found, or {@code spec} is {@code null}. 507 * @exception SecurityException 508 * if a security manager exists and its 509 * {@code checkPermission} method doesn't allow 510 * specifying a stream handler. 511 * @see java.net.URL#URL(java.lang.String, java.lang.String, 512 * int, java.lang.String) 513 * @see java.net.URLStreamHandler 514 * @see java.net.URLStreamHandler#parseURL(java.net.URL, 515 * java.lang.String, int, int) 516 */ 517 public URL(URL context, String spec, URLStreamHandler handler) 518 throws MalformedURLException 519 { 520 String original = spec; 521 int i, limit, c; 522 int start = 0; 523 String newProtocol = null; 524 boolean aRef=false; 525 boolean isRelative = false; 526 527 // Check for permission to specify a handler 528 if (handler != null) { 529 SecurityManager sm = System.getSecurityManager(); 530 if (sm != null) { 531 checkSpecifyHandler(sm); 532 } 533 } 534 535 try { 536 limit = spec.length(); 537 while ((limit > 0) && (spec.charAt(limit - 1) <= ' ')) { 538 limit--; //eliminate trailing whitespace 539 } 540 while ((start < limit) && (spec.charAt(start) <= ' ')) { 541 start++; // eliminate leading whitespace 542 } 543 544 if (spec.regionMatches(true, start, "url:", 0, 4)) { 545 start += 4; 546 } 547 if (start < spec.length() && spec.charAt(start) == '#') { 548 /* we're assuming this is a ref relative to the context URL. 549 * This means protocols cannot start w/ '#', but we must parse 550 * ref URL's like: "hello:there" w/ a ':' in them. 551 */ 552 aRef=true; 553 } 554 for (i = start ; !aRef && (i < limit) && 555 ((c = spec.charAt(i)) != '/') ; i++) { 556 if (c == ':') { 557 558 String s = spec.substring(start, i).toLowerCase(); 559 if (isValidProtocol(s)) { 560 newProtocol = s; 561 start = i + 1; 562 } 563 break; 564 } 565 } 566 567 // Only use our context if the protocols match. 568 protocol = newProtocol; 569 if ((context != null) && ((newProtocol == null) || 570 newProtocol.equalsIgnoreCase(context.protocol))) { 571 // inherit the protocol handler from the context 572 // if not specified to the constructor 573 if (handler == null) { 574 handler = context.handler; 575 } 576 577 // If the context is a hierarchical URL scheme and the spec 578 // contains a matching scheme then maintain backwards 579 // compatibility and treat it as if the spec didn't contain 580 // the scheme; see 5.2.3 of RFC2396 581 if (context.path != null && context.path.startsWith("/")) 582 newProtocol = null; 583 584 if (newProtocol == null) { 585 protocol = context.protocol; 586 authority = context.authority; 587 userInfo = context.userInfo; 588 host = context.host; 589 port = context.port; 590 file = context.file; 591 path = context.path; 592 isRelative = true; 593 } 594 } 595 596 if (protocol == null) { 597 throw new MalformedURLException("no protocol: "+original); 598 } 599 600 // Get the protocol handler if not specified or the protocol 601 // of the context could not be used 602 if (handler == null && 603 (handler = getURLStreamHandler(protocol)) == null) { 604 throw new MalformedURLException("unknown protocol: "+protocol); 605 } 606 607 this.handler = handler; 608 609 i = spec.indexOf('#', start); 610 if (i >= 0) { 611 ref = spec.substring(i + 1, limit); 612 limit = i; 613 } 614 615 /* 616 * Handle special case inheritance of query and fragment 617 * implied by RFC2396 section 5.2.2. 618 */ 619 if (isRelative && start == limit) { 620 query = context.query; 621 if (ref == null) { 622 ref = context.ref; 623 } 624 } 625 626 handler.parseURL(this, spec, start, limit); 627 628 } catch(MalformedURLException e) { 629 throw e; 630 } catch(Exception e) { 631 MalformedURLException exception = new MalformedURLException(e.getMessage()); 632 exception.initCause(e); 633 throw exception; 634 } 635 } 636 637 /* 638 * Returns true if specified string is a valid protocol name. 639 */ 640 private boolean isValidProtocol(String protocol) { 641 int len = protocol.length(); 642 if (len < 1) 643 return false; 644 char c = protocol.charAt(0); 645 if (!Character.isLetter(c)) 646 return false; 647 for (int i = 1; i < len; i++) { 648 c = protocol.charAt(i); 649 if (!Character.isLetterOrDigit(c) && c != '.' && c != '+' && 650 c != '-') { 651 return false; 652 } 653 } 654 return true; 655 } 656 657 /* 658 * Checks for permission to specify a stream handler. 659 */ 660 private void checkSpecifyHandler(SecurityManager sm) { 661 sm.checkPermission(SecurityConstants.SPECIFY_HANDLER_PERMISSION); 662 } 663 664 /** 665 * Sets the fields of the URL. This is not a public method so that 666 * only URLStreamHandlers can modify URL fields. URLs are 667 * otherwise constant. 668 * 669 * @param protocol the name of the protocol to use 670 * @param host the name of the host 671 @param port the port number on the host 672 * @param file the file on the host 673 * @param ref the internal reference in the URL 674 */ 675 void set(String protocol, String host, int port, 676 String file, String ref) { 677 synchronized (this) { 678 this.protocol = protocol; 679 this.host = host; 680 authority = port == -1 ? host : host + ":" + port; 681 this.port = port; 682 this.file = file; 683 this.ref = ref; 684 /* This is very important. We must recompute this after the 685 * URL has been changed. */ 686 hashCode = -1; 687 hostAddress = null; 688 int q = file.lastIndexOf('?'); 689 if (q != -1) { 690 query = file.substring(q+1); 691 path = file.substring(0, q); 692 } else 693 path = file; 694 } 695 } 696 697 /** 698 * Sets the specified 8 fields of the URL. This is not a public method so 699 * that only URLStreamHandlers can modify URL fields. URLs are otherwise 700 * constant. 701 * 702 * @param protocol the name of the protocol to use 703 * @param host the name of the host 704 * @param port the port number on the host 705 * @param authority the authority part for the url 706 * @param userInfo the username and password 707 * @param path the file on the host 708 * @param ref the internal reference in the URL 709 * @param query the query part of this URL 710 * @since 1.3 711 */ 712 void set(String protocol, String host, int port, 713 String authority, String userInfo, String path, 714 String query, String ref) { 715 synchronized (this) { 716 this.protocol = protocol; 717 this.host = host; 718 this.port = port; 719 this.file = query == null ? path : path + "?" + query; 720 this.userInfo = userInfo; 721 this.path = path; 722 this.ref = ref; 723 /* This is very important. We must recompute this after the 724 * URL has been changed. */ 725 hashCode = -1; 726 hostAddress = null; 727 this.query = query; 728 this.authority = authority; 729 } 730 } 731 732 /** 733 * Gets the query part of this {@code URL}. 734 * 735 * @return the query part of this {@code URL}, 736 * or <CODE>null</CODE> if one does not exist 737 * @since 1.3 738 */ 739 public String getQuery() { 740 return query; 741 } 742 743 /** 744 * Gets the path part of this {@code URL}. 745 * 746 * @return the path part of this {@code URL}, or an 747 * empty string if one does not exist 748 * @since 1.3 749 */ 750 public String getPath() { 751 return path; 752 } 753 754 /** 755 * Gets the userInfo part of this {@code URL}. 756 * 757 * @return the userInfo part of this {@code URL}, or 758 * <CODE>null</CODE> if one does not exist 759 * @since 1.3 760 */ 761 public String getUserInfo() { 762 return userInfo; 763 } 764 765 /** 766 * Gets the authority part of this {@code URL}. 767 * 768 * @return the authority part of this {@code URL} 769 * @since 1.3 770 */ 771 public String getAuthority() { 772 return authority; 773 } 774 775 /** 776 * Gets the port number of this {@code URL}. 777 * 778 * @return the port number, or -1 if the port is not set 779 */ 780 public int getPort() { 781 return port; 782 } 783 784 /** 785 * Gets the default port number of the protocol associated 786 * with this {@code URL}. If the URL scheme or the URLStreamHandler 787 * for the URL do not define a default port number, 788 * then -1 is returned. 789 * 790 * @return the port number 791 * @since 1.4 792 */ 793 public int getDefaultPort() { 794 return handler.getDefaultPort(); 795 } 796 797 /** 798 * Gets the protocol name of this {@code URL}. 799 * 800 * @return the protocol of this {@code URL}. 801 */ 802 public String getProtocol() { 803 return protocol; 804 } 805 806 /** 807 * Gets the host name of this {@code URL}, if applicable. 808 * The format of the host conforms to RFC 2732, i.e. for a 809 * literal IPv6 address, this method will return the IPv6 address 810 * enclosed in square brackets ({@code '['} and {@code ']'}). 811 * 812 * @return the host name of this {@code URL}. 813 */ 814 public String getHost() { 815 return host; 816 } 817 818 /** 819 * Gets the file name of this {@code URL}. 820 * The returned file portion will be 821 * the same as <CODE>getPath()</CODE>, plus the concatenation of 822 * the value of <CODE>getQuery()</CODE>, if any. If there is 823 * no query portion, this method and <CODE>getPath()</CODE> will 824 * return identical results. 825 * 826 * @return the file name of this {@code URL}, 827 * or an empty string if one does not exist 828 */ 829 public String getFile() { 830 return file; 831 } 832 833 /** 834 * Gets the anchor (also known as the "reference") of this 835 * {@code URL}. 836 * 837 * @return the anchor (also known as the "reference") of this 838 * {@code URL}, or <CODE>null</CODE> if one does not exist 839 */ 840 public String getRef() { 841 return ref; 842 } 843 844 /** 845 * Compares this URL for equality with another object.<p> 846 * 847 * If the given object is not a URL then this method immediately returns 848 * {@code false}.<p> 849 * 850 * Two URL objects are equal if they have the same protocol, reference 851 * equivalent hosts, have the same port number on the host, and the same 852 * file and fragment of the file.<p> 853 * 854 * Two hosts are considered equivalent if both host names can be resolved 855 * into the same IP addresses; else if either host name can't be 856 * resolved, the host names must be equal without regard to case; or both 857 * host names equal to null.<p> 858 * 859 * Since hosts comparison requires name resolution, this operation is a 860 * blocking operation. <p> 861 * 862 * Note: The defined behavior for {@code equals} is known to 863 * be inconsistent with virtual hosting in HTTP. 864 * 865 * @param obj the URL to compare against. 866 * @return {@code true} if the objects are the same; 867 * {@code false} otherwise. 868 */ 869 public boolean equals(Object obj) { 870 if (!(obj instanceof URL)) 871 return false; 872 URL u2 = (URL)obj; 873 874 return handler.equals(this, u2); 875 } 876 877 /** 878 * Creates an integer suitable for hash table indexing.<p> 879 * 880 * The hash code is based upon all the URL components relevant for URL 881 * comparison. As such, this operation is a blocking operation. 882 * 883 * @return a hash code for this {@code URL}. 884 */ 885 public synchronized int hashCode() { 886 if (hashCode != -1) 887 return hashCode; 888 889 hashCode = handler.hashCode(this); 890 return hashCode; 891 } 892 893 /** 894 * Compares two URLs, excluding the fragment component.<p> 895 * 896 * Returns {@code true} if this {@code URL} and the 897 * {@code other} argument are equal without taking the 898 * fragment component into consideration. 899 * 900 * @param other the {@code URL} to compare against. 901 * @return {@code true} if they reference the same remote object; 902 * {@code false} otherwise. 903 */ 904 public boolean sameFile(URL other) { 905 return handler.sameFile(this, other); 906 } 907 908 /** 909 * Constructs a string representation of this {@code URL}. The 910 * string is created by calling the {@code toExternalForm} 911 * method of the stream protocol handler for this object. 912 * 913 * @return a string representation of this object. 914 * @see java.net.URL#URL(java.lang.String, java.lang.String, int, 915 * java.lang.String) 916 * @see java.net.URLStreamHandler#toExternalForm(java.net.URL) 917 */ 918 public String toString() { 919 return toExternalForm(); 920 } 921 922 /** 923 * Constructs a string representation of this {@code URL}. The 924 * string is created by calling the {@code toExternalForm} 925 * method of the stream protocol handler for this object. 926 * 927 * @return a string representation of this object. 928 * @see java.net.URL#URL(java.lang.String, java.lang.String, 929 * int, java.lang.String) 930 * @see java.net.URLStreamHandler#toExternalForm(java.net.URL) 931 */ 932 public String toExternalForm() { 933 return handler.toExternalForm(this); 934 } 935 936 /** 937 * Returns a {@link java.net.URI} equivalent to this URL. 938 * This method functions in the same way as {@code new URI (this.toString())}. 939 * <p>Note, any URL instance that complies with RFC 2396 can be converted 940 * to a URI. However, some URLs that are not strictly in compliance 941 * can not be converted to a URI. 942 * 943 * @exception URISyntaxException if this URL is not formatted strictly according to 944 * to RFC2396 and cannot be converted to a URI. 945 * 946 * @return a URI instance equivalent to this URL. 947 * @since 1.5 948 */ 949 public URI toURI() throws URISyntaxException { 950 return new URI (toString()); 951 } 952 953 /** 954 * Returns a {@link java.net.URLConnection URLConnection} instance that 955 * represents a connection to the remote object referred to by the 956 * {@code URL}. 957 * 958 * <P>A new instance of {@linkplain java.net.URLConnection URLConnection} is 959 * created every time when invoking the 960 * {@linkplain java.net.URLStreamHandler#openConnection(URL) 961 * URLStreamHandler.openConnection(URL)} method of the protocol handler for 962 * this URL.</P> 963 * 964 * <P>It should be noted that a URLConnection instance does not establish 965 * the actual network connection on creation. This will happen only when 966 * calling {@linkplain java.net.URLConnection#connect() URLConnection.connect()}.</P> 967 * 968 * <P>If for the URL's protocol (such as HTTP or JAR), there 969 * exists a public, specialized URLConnection subclass belonging 970 * to one of the following packages or one of their subpackages: 971 * java.lang, java.io, java.util, java.net, the connection 972 * returned will be of that subclass. For example, for HTTP an 973 * HttpURLConnection will be returned, and for JAR a 974 * JarURLConnection will be returned.</P> 975 * 976 * @return a {@link java.net.URLConnection URLConnection} linking 977 * to the URL. 978 * @exception IOException if an I/O exception occurs. 979 * @see java.net.URL#URL(java.lang.String, java.lang.String, 980 * int, java.lang.String) 981 */ 982 public URLConnection openConnection() throws java.io.IOException { 983 return handler.openConnection(this); 984 } 985 986 /** 987 * Same as {@link #openConnection()}, except that the connection will be 988 * made through the specified proxy; Protocol handlers that do not 989 * support proxing will ignore the proxy parameter and make a 990 * normal connection. 991 * 992 * Invoking this method preempts the system's default 993 * {@link java.net.ProxySelector ProxySelector} settings. 994 * 995 * @param proxy the Proxy through which this connection 996 * will be made. If direct connection is desired, 997 * Proxy.NO_PROXY should be specified. 998 * @return a {@code URLConnection} to the URL. 999 * @exception IOException if an I/O exception occurs. 1000 * @exception SecurityException if a security manager is present 1001 * and the caller doesn't have permission to connect 1002 * to the proxy. 1003 * @exception IllegalArgumentException will be thrown if proxy is null, 1004 * or proxy has the wrong type 1005 * @exception UnsupportedOperationException if the subclass that 1006 * implements the protocol handler doesn't support 1007 * this method. 1008 * @see java.net.URL#URL(java.lang.String, java.lang.String, 1009 * int, java.lang.String) 1010 * @see java.net.URLConnection 1011 * @see java.net.URLStreamHandler#openConnection(java.net.URL, 1012 * java.net.Proxy) 1013 * @since 1.5 1014 */ 1015 public URLConnection openConnection(Proxy proxy) 1016 throws java.io.IOException { 1017 if (proxy == null) { 1018 throw new IllegalArgumentException("proxy can not be null"); 1019 } 1020 1021 // Create a copy of Proxy as a security measure 1022 Proxy p = proxy == Proxy.NO_PROXY ? Proxy.NO_PROXY : sun.net.ApplicationProxy.create(proxy); 1023 SecurityManager sm = System.getSecurityManager(); 1024 if (p.type() != Proxy.Type.DIRECT && sm != null) { 1025 InetSocketAddress epoint = (InetSocketAddress) p.address(); 1026 if (epoint.isUnresolved()) 1027 sm.checkConnect(epoint.getHostName(), epoint.getPort()); 1028 else 1029 sm.checkConnect(epoint.getAddress().getHostAddress(), 1030 epoint.getPort()); 1031 } 1032 return handler.openConnection(this, p); 1033 } 1034 1035 /** 1036 * Opens a connection to this {@code URL} and returns an 1037 * {@code InputStream} for reading from that connection. This 1038 * method is a shorthand for: 1039 * <blockquote><pre> 1040 * openConnection().getInputStream() 1041 * </pre></blockquote> 1042 * 1043 * @return an input stream for reading from the URL connection. 1044 * @exception IOException if an I/O exception occurs. 1045 * @see java.net.URL#openConnection() 1046 * @see java.net.URLConnection#getInputStream() 1047 */ 1048 public final InputStream openStream() throws java.io.IOException { 1049 return openConnection().getInputStream(); 1050 } 1051 1052 /** 1053 * Gets the contents of this URL. This method is a shorthand for: 1054 * <blockquote><pre> 1055 * openConnection().getContent() 1056 * </pre></blockquote> 1057 * 1058 * @return the contents of this URL. 1059 * @exception IOException if an I/O exception occurs. 1060 * @see java.net.URLConnection#getContent() 1061 */ 1062 public final Object getContent() throws java.io.IOException { 1063 return openConnection().getContent(); 1064 } 1065 1066 /** 1067 * Gets the contents of this URL. This method is a shorthand for: 1068 * <blockquote><pre> 1069 * openConnection().getContent(classes) 1070 * </pre></blockquote> 1071 * 1072 * @param classes an array of Java types 1073 * @return the content object of this URL that is the first match of 1074 * the types specified in the classes array. 1075 * null if none of the requested types are supported. 1076 * @exception IOException if an I/O exception occurs. 1077 * @see java.net.URLConnection#getContent(Class[]) 1078 * @since 1.3 1079 */ 1080 public final Object getContent(Class<?>[] classes) 1081 throws java.io.IOException { 1082 return openConnection().getContent(classes); 1083 } 1084 1085 /** 1086 * The Application URLStreamHandler factory. 1087 */ 1088 private static volatile URLStreamHandlerFactory factory; 1089 1090 /** The list of factories. */ 1091 private static final CopyOnWriteArrayList<URLStreamHandlerFactory> factoryList = 1092 new CopyOnWriteArrayList<>(); 1093 1094 /** 1095 * Sets an application's {@code URLStreamHandlerFactory}. 1096 * This method can be called at most once in a given Java Virtual 1097 * Machine. 1098 * 1099 *<p> The {@code URLStreamHandlerFactory} instance is used to 1100 *construct a stream protocol handler from a protocol name. 1101 * 1102 * <p> If there is a security manager, this method first calls 1103 * the security manager's {@code checkSetFactory} method 1104 * to ensure the operation is allowed. 1105 * This could result in a SecurityException. 1106 * 1107 * @param fac the desired factory. 1108 * @exception Error if the application has already set a factory. 1109 * @exception SecurityException if a security manager exists and its 1110 * {@code checkSetFactory} method doesn't allow 1111 * the operation. 1112 * @see java.net.URL#URL(java.lang.String, java.lang.String, 1113 * int, java.lang.String) 1114 * @see java.net.URLStreamHandlerFactory 1115 * @see SecurityManager#checkSetFactory 1116 */ 1117 public static void setURLStreamHandlerFactory(URLStreamHandlerFactory fac) { 1118 synchronized (streamHandlerLock) { 1119 if (factory != null) { 1120 throw new Error("factory already defined"); 1121 } 1122 SecurityManager security = System.getSecurityManager(); 1123 if (security != null) { 1124 security.checkSetFactory(); 1125 } 1126 handlers.clear(); 1127 1128 // safe publication of URLStreamHandlerFactory with volatile write 1129 factory = fac; 1130 } 1131 } 1132 1133 /** 1134 * Adds a {@linkplain URLStreamHandlerFactory}. This method can be called 1135 * multiple times in order to create an effective list of factories. 1136 * 1137 * <p> Factories, added through a call to this method, will have their 1138 * {@code createURLStreamHandler} method called, in the order in which they 1139 * were added, to construct a stream protocol handler from a protocol name. 1140 * A factory that cannot construct a stream protocol handler for a 1141 * particular protocol name should return {@code null}. The next factory in 1142 * the list, if there is one, will then be consulted. 1143 * 1144 * <p> If there is a security manager, this method first calls 1145 * the security manager's {@code checkSetFactory} method 1146 * to ensure the operation is allowed. 1147 * This could result in a SecurityException. 1148 * 1149 * @apiNote 1150 * This method is intended to be used by long running applications, to 1151 * support adding additional system-wide protocol handlers, beyond that of 1152 * the built-in handlers. It can be used in some situations to override 1153 * built-in handlers, that may not be possible to locate using services, 1154 * like {@code jar} for example. 1155 * 1156 * @param factory the factory 1157 * @throws SecurityException if a security manager exists and its {@link 1158 * SecurityManager#checkSetFactory} method doesn't allow the operation 1159 * @throws NullPointerException if {@code factory} is null 1160 * @since 1.9 1161 */ 1162 public static void addURLStreamHandlerFactory(URLStreamHandlerFactory factory) { 1163 Objects.requireNonNull(factory); 1164 synchronized (streamHandlerLock) { 1165 SecurityManager security = System.getSecurityManager(); 1166 if (security != null) { 1167 security.checkSetFactory(); 1168 } 1169 handlers.clear(); 1170 1171 factoryList.add(factory); 1172 } 1173 } 1174 1175 private static final URLStreamHandlerFactory defaultFactory = new DefaultFactory(); 1176 1177 private static class DefaultFactory implements URLStreamHandlerFactory { 1178 private static String PREFIX = "sun.net.www.protocol"; 1179 1180 public URLStreamHandler createURLStreamHandler(String protocol) { 1181 String name = PREFIX + "." + protocol + ".Handler"; 1182 try { 1183 Class<?> c = Class.forName(name); 1184 return (URLStreamHandler)c.newInstance(); 1185 } catch (ClassNotFoundException x) { 1186 // ignore 1187 } catch (Exception e) { 1188 // For compatibility, all Exceptions are ignored. 1189 // any number of exceptions can get thrown here 1190 } 1191 return null; 1192 } 1193 } 1194 1195 private static final URLStreamHandler NULL_HANDLER = new URLStreamHandler() { 1196 public URLConnection openConnection(URL u) { return null; } 1197 public URLConnection openConnection(URL u, Proxy p) { return null; } 1198 }; 1199 1200 /** Returns a handler instance for the given protocol, null if the factories 1201 * could not create a handler, or NULL_HANDLER if there are no factories. 1202 */ 1203 private static URLStreamHandler handlerFromSettableFactory(String protocol) { 1204 URLStreamHandler handler = NULL_HANDLER; 1205 // Use the factory (if any). Volatile read makes 1206 // URLStreamHandlerFactory appear fully initialized to current thread. 1207 URLStreamHandlerFactory fac = factory; 1208 if (fac != null) 1209 handler = fac.createURLStreamHandler(protocol); 1210 1211 if (handler == NULL_HANDLER || handler == null) { 1212 for (URLStreamHandlerFactory f : factoryList) { 1213 handler = f.createURLStreamHandler(protocol); 1214 if (handler != null) 1215 return handler; 1216 } 1217 } 1218 return handler; 1219 } 1220 1221 // installed service providers 1222 private static volatile List<URLStreamHandlerFactory> installedProviders; 1223 private static final Object providersLock = new Object(); 1224 private static boolean loadingProviders = false; 1225 1226 private static List<URLStreamHandlerFactory> loadInstalledProviders() { 1227 List<URLStreamHandlerFactory> list = new ArrayList<>(); 1228 ServiceLoader<URLStreamHandlerFactory> sl = ServiceLoader 1229 .load(URLStreamHandlerFactory.class, ClassLoader.getSystemClassLoader()); 1230 // ServiceConfigurationError may be throw here 1231 for (URLStreamHandlerFactory provider: sl) 1232 list.add(provider); 1233 1234 return list; 1235 } 1236 1237 private static List<URLStreamHandlerFactory> installedProviders() { 1238 if (installedProviders == null) { 1239 synchronized (providersLock) { 1240 if (installedProviders == null) { 1241 if (loadingProviders) { 1242 throw new Error( 1243 "Circular loading of URL stream handler providers detected"); 1244 } 1245 loadingProviders = true; 1246 1247 // do not use a lambda, or method ref here. 1248 List<URLStreamHandlerFactory> list = AccessController 1249 .doPrivileged(new PrivilegedAction<List<URLStreamHandlerFactory>>() { 1250 public List<URLStreamHandlerFactory> run() { 1251 return URL.loadInstalledProviders(); 1252 } 1253 }); 1254 1255 installedProviders = Collections.unmodifiableList(list); 1256 } 1257 } 1258 } 1259 return installedProviders; 1260 } 1261 1262 private static final String SL_SKIP_PROTOCOL = "jar"; 1263 1264 private static URLStreamHandler handlerFromInstalledProviders(String protocol) { 1265 if (protocol.equalsIgnoreCase(SL_SKIP_PROTOCOL)) 1266 return null; 1267 1268 URLStreamHandler handler = null; 1269 for (URLStreamHandlerFactory f : installedProviders()) { 1270 handler = f.createURLStreamHandler(protocol); 1271 if (handler != null) 1272 return handler; 1273 } 1274 return handler; 1275 } 1276 1277 private static final String[] UNOVERRIDEABLE_PROTOCOLS = {"file", "jrt"}; 1278 private static boolean overrideable(String protocol) { 1279 for (String p : UNOVERRIDEABLE_PROTOCOLS) 1280 if (protocol.equalsIgnoreCase(p)) 1281 return false; 1282 return true; 1283 } 1284 1285 /** 1286 * A table of protocol handlers. 1287 */ 1288 static Hashtable<String,URLStreamHandler> handlers = new Hashtable<>(); 1289 private static final Object streamHandlerLock = new Object(); 1290 1291 /** 1292 * Returns the Stream Handler. 1293 * @param protocol the protocol to use 1294 */ 1295 static URLStreamHandler getURLStreamHandler(String protocol) { 1296 1297 URLStreamHandler handler = handlers.get(protocol); 1298 1299 if (handler != null) 1300 return handler; 1301 1302 boolean checkedWithFactory = false; 1303 1304 if (overrideable(protocol)) { 1305 handler = handlerFromSettableFactory(protocol); 1306 if (handler != NULL_HANDLER) 1307 checkedWithFactory = true; 1308 1309 if (handler == null || handler == NULL_HANDLER) { 1310 handler = handlerFromInstalledProviders(protocol); 1311 } 1312 } 1313 if (handler == null) { 1314 // Try the built-in protocol handler 1315 handler = defaultFactory.createURLStreamHandler(protocol); 1316 } 1317 1318 synchronized (streamHandlerLock) { 1319 1320 URLStreamHandler handler2 = null; 1321 1322 // Check again with hashtable just in case another 1323 // thread created a handler since we last checked 1324 handler2 = handlers.get(protocol); 1325 1326 if (handler2 != null) { 1327 return handler2; 1328 } 1329 1330 // Check with factory if another thread set a 1331 // factory since our last check 1332 if (!checkedWithFactory) { 1333 handler2 = handlerFromSettableFactory(protocol); 1334 } 1335 1336 if (!(handler2 == null || handler2 == NULL_HANDLER)) { 1337 // The handler from the factory must be given more 1338 // importance. Discard the default handler that 1339 // this thread created. 1340 handler = handler2; 1341 } 1342 1343 // Insert this handler into the hashtable 1344 if (handler != null) { 1345 handlers.put(protocol, handler); 1346 } 1347 1348 } 1349 1350 return handler; 1351 } 1352 1353 /** 1354 * WriteObject is called to save the state of the URL to an 1355 * ObjectOutputStream. The handler is not saved since it is 1356 * specific to this system. 1357 * 1358 * @serialData the default write object value. When read back in, 1359 * the reader must ensure that calling getURLStreamHandler with 1360 * the protocol variable returns a valid URLStreamHandler and 1361 * throw an IOException if it does not. 1362 */ 1363 private synchronized void writeObject(java.io.ObjectOutputStream s) 1364 throws IOException 1365 { 1366 s.defaultWriteObject(); // write the fields 1367 } 1368 1369 /** 1370 * readObject is called to restore the state of the URL from the 1371 * stream. It reads the components of the URL and finds the local 1372 * stream handler. 1373 */ 1374 private synchronized void readObject(java.io.ObjectInputStream s) 1375 throws IOException, ClassNotFoundException 1376 { 1377 s.defaultReadObject(); // read the fields 1378 if ((handler = getURLStreamHandler(protocol)) == null) { 1379 throw new IOException("unknown protocol: " + protocol); 1380 } 1381 1382 // Construct authority part 1383 if (authority == null && 1384 ((host != null && host.length() > 0) || port != -1)) { 1385 if (host == null) 1386 host = ""; 1387 authority = (port == -1) ? host : host + ":" + port; 1388 1389 // Handle hosts with userInfo in them 1390 int at = host.lastIndexOf('@'); 1391 if (at != -1) { 1392 userInfo = host.substring(0, at); 1393 host = host.substring(at+1); 1394 } 1395 } else if (authority != null) { 1396 // Construct user info part 1397 int ind = authority.indexOf('@'); 1398 if (ind != -1) 1399 userInfo = authority.substring(0, ind); 1400 } 1401 1402 // Construct path and query part 1403 path = null; 1404 query = null; 1405 if (file != null) { 1406 // Fix: only do this if hierarchical? 1407 int q = file.lastIndexOf('?'); 1408 if (q != -1) { 1409 query = file.substring(q+1); 1410 path = file.substring(0, q); 1411 } else 1412 path = file; 1413 } 1414 } 1415 } 1416 1417 class Parts { 1418 String path, query, ref; 1419 1420 Parts(String file) { 1421 int ind = file.indexOf('#'); 1422 ref = ind < 0 ? null: file.substring(ind + 1); 1423 file = ind < 0 ? file: file.substring(0, ind); 1424 int q = file.lastIndexOf('?'); 1425 if (q != -1) { 1426 query = file.substring(q+1); 1427 path = file.substring(0, q); 1428 } else { 1429 path = file; 1430 } 1431 } 1432 1433 String getPath() { 1434 return path; 1435 } 1436 1437 String getQuery() { 1438 return query; 1439 } 1440 1441 String getRef() { 1442 return ref; 1443 } 1444 } --- EOF ---