diff --git a/src/java.base/share/classes/sun/net/www/ParseUtil.java b/src/java.base/share/classes/sun/net/www/ParseUtil.java index 628eeb948ad..51d1b8398b6 100644 --- a/src/java.base/share/classes/sun/net/www/ParseUtil.java +++ b/src/java.base/share/classes/sun/net/www/ParseUtil.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 1998, 2024, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 1998, 2025, 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 @@ -507,6 +507,22 @@ public final class ParseUtil { } } + /** + * {@return true if the url is a file: URL for a 'local file' as defined by RFC 8089, Section 2} + * + * For unknown historical reasons, this method deviates from RFC 8089 + * by allowing "~" as an alias for 'localhost' + * + * @param url the URL which may be a local file URL + */ + public static boolean isLocalFileURL(URL url) { + if (url.getProtocol().equalsIgnoreCase("file")) { + String host = url.getHost(); + return host == null || host.isEmpty() || host.equals("~") || + host.equalsIgnoreCase("localhost"); + } + return false; + } // -- Character classes for parsing -- diff --git a/src/java.base/share/classes/sun/net/www/protocol/jar/JarFileFactory.java b/src/java.base/share/classes/sun/net/www/protocol/jar/JarFileFactory.java index 9afbea8bf76..4d695c8d4e8 100644 --- a/src/java.base/share/classes/sun/net/www/protocol/jar/JarFileFactory.java +++ b/src/java.base/share/classes/sun/net/www/protocol/jar/JarFileFactory.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 1999, 2024, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 1999, 2025, 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 @@ -34,6 +34,7 @@ import java.util.jar.JarFile; import jdk.internal.util.OperatingSystem; import sun.net.util.URLUtil; +import sun.net.www.ParseUtil; /* A factory for cached JAR file. This class is used to both retrieve * and cache Jar files. @@ -92,7 +93,7 @@ class JarFileFactory implements URLJarFile.URLJarFileCloseController { return get(url, false); } URL patched = urlFor(url); - if (!URLJarFile.isFileURL(patched)) { + if (!ParseUtil.isLocalFileURL(patched)) { // A temporary file will be created, we can prepopulate // the cache in this case. return get(url, useCaches); @@ -158,9 +159,10 @@ class JarFileFactory implements URLJarFile.URLJarFileCloseController { // Deal with UNC pathnames specially. See 4180841 String host = url.getHost(); - if (host != null && !host.isEmpty() && - !host.equalsIgnoreCase("localhost")) { - + // Subtly different from ParseUtil.isLocalFileURL, for historical reasons + boolean isLocalFile = ParseUtil.isLocalFileURL(url) && !"~".equals(host); + // For remote hosts, change 'file://host/folder/data.xml' to 'file:////host/folder/data.xml' + if (!isLocalFile) { @SuppressWarnings("deprecation") var _unused = url = new URL("file", "", "//" + host + url.getPath()); } diff --git a/src/java.base/share/classes/sun/net/www/protocol/jar/URLJarFile.java b/src/java.base/share/classes/sun/net/www/protocol/jar/URLJarFile.java index b3d4a223196..d30d18df9d6 100644 --- a/src/java.base/share/classes/sun/net/www/protocol/jar/URLJarFile.java +++ b/src/java.base/share/classes/sun/net/www/protocol/jar/URLJarFile.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2001, 2024, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2001, 2025, 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 @@ -49,7 +49,7 @@ public class URLJarFile extends JarFile { private Map superEntries; static JarFile getJarFile(URL url, URLJarFileCloseController closeController) throws IOException { - if (isFileURL(url)) { + if (ParseUtil.isLocalFileURL(url)) { Runtime.Version version = "runtime".equals(url.getRef()) ? JarFile.runtimeVersion() : JarFile.baseVersion(); @@ -71,20 +71,6 @@ public class URLJarFile extends JarFile { this.closeController = closeController; } - static boolean isFileURL(URL url) { - if (url.getProtocol().equalsIgnoreCase("file")) { - /* - * Consider this a 'file' only if it's a LOCAL file, because - * 'file:' URLs can be accessible through ftp. - */ - String host = url.getHost(); - if (host == null || host.isEmpty() || host.equals("~") || - host.equalsIgnoreCase("localhost")) - return true; - } - return false; - } - /** * Returns the ZipEntry for the given entry name or * null if not found. diff --git a/src/java.base/unix/classes/sun/net/www/protocol/file/Handler.java b/src/java.base/unix/classes/sun/net/www/protocol/file/Handler.java index 4b2c110b3e1..efde6a809e9 100644 --- a/src/java.base/unix/classes/sun/net/www/protocol/file/Handler.java +++ b/src/java.base/unix/classes/sun/net/www/protocol/file/Handler.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 1994, 2022, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 1994, 2025, 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 @@ -64,8 +64,7 @@ public class Handler extends URLStreamHandler { public URLConnection openConnection(URL u, Proxy p) throws IOException { String host = u.getHost(); - if (host == null || host.isEmpty() || host.equals("~") || - host.equalsIgnoreCase("localhost")) { + if (ParseUtil.isLocalFileURL(u)) { File file = new File(ParseUtil.decode(u.getPath())); return createFileURLConnection(u, file); } diff --git a/src/java.base/windows/classes/sun/net/www/protocol/file/Handler.java b/src/java.base/windows/classes/sun/net/www/protocol/file/Handler.java index 2865aa32a95..38ad16267e7 100644 --- a/src/java.base/windows/classes/sun/net/www/protocol/file/Handler.java +++ b/src/java.base/windows/classes/sun/net/www/protocol/file/Handler.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 1999, 2022, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 1999, 2025, 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 @@ -72,9 +72,7 @@ public class Handler extends URLStreamHandler { path = path.replace('/', '\\'); path = path.replace('|', ':'); - if ((host == null) || host.isEmpty() || - host.equalsIgnoreCase("localhost") || - host.equals("~")) { + if (ParseUtil.isLocalFileURL(url)) { return createFileURLConnection(url, new File(path)); }