changeset 5466:8067bdeb4e31

7189567: java net obselete protocol Reviewed-by: alanb, ahgross
author chegar
date Wed, 22 Aug 2012 22:51:16 +0100
parents 4ed8c0b034c6
children 00f5a58b55fd
files src/share/classes/java/net/URL.java test/java/net/URL/Gopher.java test/java/net/URL/Test.java
diffstat 3 files changed, 101 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/src/share/classes/java/net/URL.java	Mon Aug 20 22:10:39 2012 +0100
+++ b/src/share/classes/java/net/URL.java	Wed Aug 22 22:51:16 2012 +0100
@@ -28,6 +28,8 @@
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
+import java.security.AccessController;
+import java.security.PrivilegedAction;
 import java.util.Hashtable;
 import java.util.StringTokenizer;
 import sun.security.util.SecurityConstants;
@@ -1116,6 +1118,22 @@
     static Hashtable handlers = new Hashtable();
     private static Object streamHandlerLock = new Object();
 
+    // special case the gopher protocol, disabled by default
+    private static final String GOPHER = "gopher";
+    private static final String ENABLE_GOPHER_PROP = "jdk.net.registerGopherProtocol";
+    private static final boolean enableGopher = AccessController.doPrivileged(
+                new PrivilegedAction<Boolean>() {
+                    @Override
+                    public Boolean run() {
+                        String prop = System.getProperty(ENABLE_GOPHER_PROP);
+                        return prop == null ? false :
+                                   (prop.equalsIgnoreCase("false") ? false : true);
+                    }
+                });
+
+    // package name of the JDK implementation protocol handlers
+    private static final String JDK_PACKAGE_PREFIX =  "sun.net.www.protocol";
+
     /**
      * Returns the Stream Handler.
      * @param protocol the protocol to use
@@ -1147,7 +1165,7 @@
 
                 // REMIND: decide whether to allow the "null" class prefix
                 // or not.
-                packagePrefixList += "sun.net.www.protocol";
+                packagePrefixList += JDK_PACKAGE_PREFIX;
 
                 StringTokenizer packagePrefixIter =
                     new StringTokenizer(packagePrefixList, "|");
@@ -1157,6 +1175,14 @@
 
                     String packagePrefix =
                       packagePrefixIter.nextToken().trim();
+
+                    // do not try to instantiate the JDK gopher handler
+                    // unless the system property had been explicitly set
+                    if (protocol.equalsIgnoreCase(GOPHER) &&
+                        packagePrefix.equals(JDK_PACKAGE_PREFIX) &&
+                        !enableGopher) {
+                            continue;
+                    }
                     try {
                         String clsName = packagePrefix + "." + protocol +
                           ".Handler";
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/java/net/URL/Gopher.java	Wed Aug 22 22:51:16 2012 +0100
@@ -0,0 +1,74 @@
+/*
+ * Copyright (c) 2012 Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+/*
+ * @test
+ * @bug 7189567
+ * @summary java net obselete protocol
+ * @run main Gopher
+ * @run main/othervm -Djdk.net.registerGopherProtocol Gopher enabled
+ * @run main/othervm -Djdk.net.registerGopherProtocol=false Gopher
+ */
+
+import java.net.MalformedURLException;
+import java.net.URL;
+
+public class Gopher {
+    static final String GOPHER_PROP = "jdk.net.registerGopherProtocol";
+
+    public static void main(String[] args) throws Exception {
+        boolean expectEnabled = false;
+        if (args.length >= 1 && args[0].equals("enabled"))
+            expectEnabled = true;
+
+        String prop = System.getProperty(GOPHER_PROP);
+        boolean gopherEnabled = prop == null ? false :
+                                   (prop.equalsIgnoreCase("false") ? false : true);
+
+        // Validate system property reading
+        if (expectEnabled && !gopherEnabled) {
+            System.err.println(GOPHER_PROP + ": " + gopherEnabled);
+            throw new RuntimeException(
+                    "Failed: expected system property to be enabled, but it is not");
+        }
+        if (!expectEnabled && gopherEnabled) {
+            System.err.println(GOPHER_PROP + ": " + gopherEnabled);
+            throw new RuntimeException(
+                    "Failed: expected system property to be disabled, but it is not");
+        }
+
+        try {
+            new URL("gopher://anyhost:70/[anydata]");
+            if (!gopherEnabled) {
+                System.err.println(GOPHER_PROP + ": " + gopherEnabled);
+                throw new RuntimeException("Failed: gopher should NOT be enabled");
+            }
+        } catch (MalformedURLException x) {
+            if (gopherEnabled) {
+                System.err.println(GOPHER_PROP + ": " + gopherEnabled);
+                x.printStackTrace();
+                throw new RuntimeException("Failed: gopher should be enabled");
+            }
+        }
+    }
+}
--- a/test/java/net/URL/Test.java	Mon Aug 20 22:10:39 2012 +0100
+++ b/test/java/net/URL/Test.java	Wed Aug 22 22:51:16 2012 +0100
@@ -322,10 +322,6 @@
         test("ftp://ftp.is.co.za/rfc/rfc1808.txt")
             .s("ftp").h("ftp.is.co.za").p("/rfc/rfc1808.txt").z();
 
-        test("gopher://spinaltap.micro.umn.edu/00/Weather/California/Los%20Angeles")
-            .s("gopher").h("spinaltap.micro.umn.edu")
-            .p("/00/Weather/California/Los%20Angeles").z();
-
         test("http://www.math.uio.no/faq/compression-faq/part1.html")
             .s("http").h("www.math.uio.no").p("/faq/compression-faq/part1.html").z();