Path: blob/master/src/java.base/share/classes/sun/net/util/URLUtil.java
41159 views
/*1* Copyright (c) 2009, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package sun.net.util;2627import java.io.IOException;28import java.net.URL;29import java.net.URLPermission;30import java.security.Permission;3132/**33* URL Utility class.34*/35public class URLUtil {36/**37* Returns a string form of the url suitable for use as a key in HashMap/Sets.38*39* The string form should be behave in the same manner as the URL when40* compared for equality in a HashMap/Set, except that no nameservice41* lookup is done on the hostname (only string comparison), and the fragment42* is not considered.43*44* @see java.net.URLStreamHandler.sameFile(java.net.URL)45*/46public static String urlNoFragString(URL url) {47StringBuilder strForm = new StringBuilder();4849String protocol = url.getProtocol();50if (protocol != null) {51/* protocol is compared case-insensitive, so convert to lowercase */52protocol = protocol.toLowerCase();53strForm.append(protocol);54strForm.append("://");55}5657String host = url.getHost();58if (host != null) {59/* host is compared case-insensitive, so convert to lowercase */60host = host.toLowerCase();61strForm.append(host);6263int port = url.getPort();64if (port == -1) {65/* if no port is specificed then use the protocols66* default, if there is one */67port = url.getDefaultPort();68}69if (port != -1) {70strForm.append(":").append(port);71}72}7374String file = url.getFile();75if (file != null) {76strForm.append(file);77}7879return strForm.toString();80}8182public static Permission getConnectPermission(URL url) throws IOException {83String urlStringLowerCase = url.toString().toLowerCase();84if (urlStringLowerCase.startsWith("http:") || urlStringLowerCase.startsWith("https:")) {85return getURLConnectPermission(url);86} else if (urlStringLowerCase.startsWith("jar:http:") || urlStringLowerCase.startsWith("jar:https:")) {87String urlString = url.toString();88int bangPos = urlString.indexOf("!/");89urlString = urlString.substring(4, bangPos > -1 ? bangPos : urlString.length());90URL u = new URL(urlString);91return getURLConnectPermission(u);92// If protocol is HTTP or HTTPS than use URLPermission object93} else {94return url.openConnection().getPermission();95}96}9798private static Permission getURLConnectPermission(URL url) {99String urlString = url.getProtocol() + "://" + url.getAuthority() + url.getPath();100return new URLPermission(urlString);101}102}103104105106