Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8217364

Custom URLStreamHandler for jrt or file protocol can override default handler.

XMLWordPrintable

    • b20
    • x86_64
    • windows_10

        ADDITIONAL SYSTEM INFORMATION :
        JDK 11.0.2

        A DESCRIPTION OF THE PROBLEM :
        java.net.URL.isOverrideable(String) for "jrt" or "file" returns false,
        but custom URLStreamHandlerFactory can override URLStreamHandler for jrt or file protocol
        providing the default URLStreamHandlerFactory.

        In java.net.URL#getURLStreamHandler,
        custom URLStreamHandler's createURLStreamHandler is invoked twice.
        But the 2nd invocation does not check by isOverrideable(protocol).

        So, custom URLStreamHandlerFactory can override URLStreamHandler for jrt or file protocol
        providing the default URLStreamHandlerFactory.

                ==== the 1st invocation ====
                if (isOverrideable(protocol) && jdk.internal.misc.VM.isBooted()) {
                    // Use the factory (if any). Volatile read makes
                    // URLStreamHandlerFactory appear fully initialized to current thread.
                    fac = factory;
                    if (fac != null) {
                        handler = fac.createURLStreamHandler(protocol);
                        checkedWithFactory = true;
                    }

                    if (handler == null && !protocol.equalsIgnoreCase("jar")) {
                        handler = lookupViaProviders(protocol);
                    }

                    if (handler == null) {
                        handler = lookupViaProperty(protocol);
                    }
                }

                ==== the 2nd invocation ====
                    // Check with factory if another thread set a
                    // factory since our last check
                    if (!checkedWithFactory && (fac = factory) != null) {
                        handler2 = fac.createURLStreamHandler(protocol);
                    }

        REGRESSION : Last worked in version 11.0.1

        STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
        Execute the following test case to reproduce

        java CustomURLStreamHandlerFactory jrt:/foo

        EXPECTED VERSUS ACTUAL BEHAVIOR :
        EXPECTED -
        [output]
        Default URLStreamHandler used for jrt:/foo
        ACTUAL -
        [output]
        java.lang.RuntimeException: CustomURLStreamHandler used for jrt:/foo
                at CustomURLStreamHandlerFactory$CustomURLStreamHandler.openConnection(CustomURLStreamHandlerFactory.java:13)
                at java.base/java.net.URL.openConnection(URL.java:1051)
                at CustomURLStreamHandlerFactory.main(CustomURLStreamHandlerFactory.java:22)

        ---------- BEGIN SOURCE ----------
        import java.io.*;
        import java.net.*;

        public class CustomURLStreamHandlerFactory implements URLStreamHandlerFactory {
            @Override
            public URLStreamHandler createURLStreamHandler(String protocol) {
                return new CustomURLStreamHandler();
            }

            private static class CustomURLStreamHandler extends URLStreamHandler {
                @Override
                protected URLConnection openConnection(URL u) throws IOException {
                    throw new RuntimeException("CustomURLStreamHandler used for " + u);
                }
            }

            public static void main(String[] args) {
                URL.setURLStreamHandlerFactory(new CustomURLStreamHandlerFactory());

                for (String url : args) {
                    try {
                        new URL(url).openConnection();
                        System.out.println("Default URLStreamHandler used for " + url);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        }
        ---------- END SOURCE ----------

        FREQUENCY : always


              coffeys Sean Coffey
              webbuggrp Webbug Group
              Votes:
              0 Vote for this issue
              Watchers:
              6 Start watching this issue

                Created:
                Updated:
                Resolved: