8223892: Improved handling of jar files
Reviewed-by: dfuchs, chegar, michaelm, rhalade, ahgross
--- 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) {