8223892: Improved handling of jar files

Reviewed-by: dfuchs, chegar, michaelm, rhalade, ahgross
This commit is contained in:
Aleksei Efimov 2019-06-25 00:07:47 +01:00
parent 8e2b10070e
commit d6304e88e6
2 changed files with 24 additions and 1 deletions

View file

@ -484,6 +484,16 @@ public final class URL implements java.io.Serializable {
throw new MalformedURLException(s); 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);
}
}
}
} }
/** /**

View file

@ -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. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -121,6 +121,13 @@ public class Handler extends java.net.URLStreamHandler {
return h; 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 @Override
@SuppressWarnings("deprecation") @SuppressWarnings("deprecation")
@ -146,6 +153,12 @@ public class Handler extends java.net.URLStreamHandler {
: false; : false;
spec = spec.substring(start, limit); 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) { if (absoluteSpec) {
file = parseAbsoluteSpec(spec); file = parseAbsoluteSpec(spec);
} else if (!refOnly) { } else if (!refOnly) {