8223892: Improved handling of jar files
authoraefimov
Tue, 25 Jun 2019 00:07:47 +0100
changeset 58631 36c5e85b8597
parent 58630 6b93cc7741ba
child 58632 fbab568169c4
8223892: Improved handling of jar files Reviewed-by: dfuchs, chegar, michaelm, rhalade, ahgross
src/java.base/share/classes/java/net/URL.java
src/java.base/share/classes/sun/net/www/protocol/jar/Handler.java
--- a/src/java.base/share/classes/java/net/URL.java	Fri Jun 21 10:51:10 2019 +0100
+++ b/src/java.base/share/classes/java/net/URL.java	Tue Jun 25 00:07:47 2019 +0100
@@ -484,6 +484,16 @@
                 throw new MalformedURLException(s);
             }
         }
+        if ("jar".equalsIgnoreCase(protocol)) {
+            if (handler instanceof sun.net.www.protocol.jar.Handler) {
+                // URL.openConnection() would throw a confusing exception
+                // so generate a better exception here instead.
+                String s = ((sun.net.www.protocol.jar.Handler) handler).checkNestedProtocol(file);
+                if (s != null) {
+                    throw new MalformedURLException(s);
+                }
+            }
+        }
     }
 
     /**
--- a/src/java.base/share/classes/sun/net/www/protocol/jar/Handler.java	Fri Jun 21 10:51:10 2019 +0100
+++ b/src/java.base/share/classes/sun/net/www/protocol/jar/Handler.java	Tue Jun 25 00:07:47 2019 +0100
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1997, 2019, 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
@@ -121,6 +121,13 @@
         return h;
     }
 
+    public String checkNestedProtocol(String spec) {
+        if (spec.regionMatches(true, 0, "jar:", 0, 4)) {
+            return "Nested JAR URLs are not supported";
+        } else {
+            return null;
+        }
+    }
 
     @Override
     @SuppressWarnings("deprecation")
@@ -146,6 +153,12 @@
                 : false;
         spec = spec.substring(start, limit);
 
+        String exceptionMessage = checkNestedProtocol(spec);
+        if (exceptionMessage != null) {
+            // NPE will be transformed into MalformedURLException by the caller
+            throw new NullPointerException(exceptionMessage);
+        }
+
         if (absoluteSpec) {
             file = parseAbsoluteSpec(spec);
         } else if (!refOnly) {