Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/sun/net/URLCanonicalizer.java
38829 views
/*1* Copyright (c) 1996, 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;2627/**28* Helper class to map URL "abbreviations" to real URLs.29* The default implementation supports the following mappings:30* ftp.mumble.bar/... => ftp://ftp.mumble.bar/...31* gopher.mumble.bar/... => gopher://gopher.mumble.bar/...32* other.name.dom/... => http://other.name.dom/...33* /foo/... => file:/foo/...34*35* Full URLs (those including a protocol name) are passed through unchanged.36*37* Subclassers can override or extend this behavior to support different38* or additional canonicalization policies.39*40* @author Steve Byrne41*/4243public class URLCanonicalizer {44/**45* Creates the default canonicalizer instance.46*/47public URLCanonicalizer() { }4849/**50* Given a possibly abbreviated URL (missing a protocol name, typically),51* this method's job is to transform that URL into a canonical form,52* by including a protocol name and additional syntax, if necessary.53*54* For a correctly formed URL, this method should just return its argument.55*/56public String canonicalize(String simpleURL) {57String resultURL = simpleURL;58if (simpleURL.startsWith("ftp.")) {59resultURL = "ftp://" + simpleURL;60} else if (simpleURL.startsWith("gopher.")) {61resultURL = "gopher://" + simpleURL;62} else if (simpleURL.startsWith("/")) {63resultURL = "file:" + simpleURL;64} else if (!hasProtocolName(simpleURL)) {65if (isSimpleHostName(simpleURL)) {66simpleURL = "www." + simpleURL + ".com";67}68resultURL = "http://" + simpleURL;69}7071return resultURL;72}7374/**75* Given a possibly abbreviated URL, this predicate function returns76* true if it appears that the URL contains a protocol name77*/78public boolean hasProtocolName(String url) {79int index = url.indexOf(':');80if (index <= 0) { // treat ":foo" as not having a protocol spec81return false;82}8384for (int i = 0; i < index; i++) {85char c = url.charAt(i);8687// REMIND: this is a guess at legal characters in a protocol --88// need to be verified89if ((c >= 'A' && c <= 'Z')90|| (c >= 'a' && c <= 'z')91|| (c == '-')) {92continue;93}9495// found an illegal character96return false;97}9899return true;100}101102/**103* Returns true if the URL is just a single name, no periods or104* slashes, false otherwise105**/106protected boolean isSimpleHostName(String url) {107108for (int i = 0; i < url.length(); i++) {109char c = url.charAt(i);110111// REMIND: this is a guess at legal characters in a protocol --112// need to be verified113if ((c >= 'A' && c <= 'Z')114|| (c >= 'a' && c <= 'z')115|| (c >= '0' && c <= '9')116|| (c == '-')) {117continue;118}119120// found an illegal character121return false;122}123124return true;125}126}127128129