8197849: Misc improvements to jar resource handling

Reviewed-by: rriggs, dfuchs
This commit is contained in:
Claes Redestad 2018-02-14 19:03:12 +01:00
parent f74677e37e
commit abb7e3a52a
3 changed files with 91 additions and 20 deletions

View file

@ -44,7 +44,9 @@ import java.nio.charset.CodingErrorAction;
* @author Mike McCloskey
*/
public class ParseUtil {
public final class ParseUtil {
private ParseUtil() {}
/**
* Constructs an encoded version of the specified path string suitable
@ -80,10 +82,13 @@ public class ParseUtil {
int len = path.length();
for (int i = 0; i < len; i++) {
char c = path.charAt(i);
if (c == '/' || c == '.' ||
c >= 'a' && c <= 'z' ||
c >= 'A' && c <= 'Z' ||
c >= '0' && c <= '9') {
// Ordering in the following test is performance sensitive,
// and typically paths have most chars in the a-z range, then
// in the symbol range '&'-':' (includes '.', '/' and '0'-'9')
// and more rarely in the A-Z range.
if (c >= 'a' && c <= 'z' ||
c >= '&' && c <= ':' ||
c >= 'A' && c <= 'Z') {
continue;
} else if (c > 0x007F || match(c, L_ENCODED, H_ENCODED)) {
return i;
@ -219,9 +224,17 @@ public class ParseUtil {
/**
* Returns a canonical version of the specified string.
*/
public String canonizeString(String file) {
int i = 0;
int lim = file.length();
public static String canonizeString(String file) {
int len = file.length();
if (len == 0 || (file.indexOf("./") == -1 && file.charAt(len - 1) != '.')) {
return file;
} else {
return doCanonize(file);
}
}
private static String doCanonize(String file) {
int i, lim;
// Remove embedded /../
while ((i = file.indexOf("/../")) >= 0) {

View file

@ -141,10 +141,9 @@ public class Handler extends java.net.URLStreamHandler {
// 1. absolute (jar:)
// 2. relative (i.e. url + foo/bar/baz.ext)
// 3. anchor-only (i.e. url + #foo), which we already did (refOnly)
boolean absoluteSpec = false;
if (spec.length() >= 4) {
absoluteSpec = spec.substring(0, 4).equalsIgnoreCase("jar:");
}
boolean absoluteSpec = spec.length() >= 4
? spec.regionMatches(true, 0, "jar:", 0, 4)
: false;
spec = spec.substring(start, limit);
if (absoluteSpec) {
@ -156,16 +155,14 @@ public class Handler extends java.net.URLStreamHandler {
int bangSlash = indexOfBangSlash(file);
String toBangSlash = file.substring(0, bangSlash);
String afterBangSlash = file.substring(bangSlash);
sun.net.www.ParseUtil canonizer = new ParseUtil();
afterBangSlash = canonizer.canonizeString(afterBangSlash);
afterBangSlash = ParseUtil.canonizeString(afterBangSlash);
file = toBangSlash + afterBangSlash;
}
setURL(url, "jar", "", -1, file, ref);
}
private String parseAbsoluteSpec(String spec) {
URL url = null;
int index = -1;
int index;
// check for !/
if ((index = indexOfBangSlash(spec)) == -1) {
throw new NullPointerException("no !/ in spec");
@ -173,7 +170,7 @@ public class Handler extends java.net.URLStreamHandler {
// test the inner URL
try {
String innerSpec = spec.substring(0, index - 1);
url = new URL(innerSpec);
new URL(innerSpec);
} catch (MalformedURLException e) {
throw new NullPointerException("invalid url: " +
spec + " (" + e + ")");
@ -193,16 +190,16 @@ public class Handler extends java.net.URLStreamHandler {
": no !/");
}
ctxFile = ctxFile.substring(0, bangSlash);
}
if (!ctxFile.endsWith("/") && (!spec.startsWith("/"))){
} else {
// chop up the last component
int lastSlash = ctxFile.lastIndexOf('/');
if (lastSlash == -1) {
throw new NullPointerException("malformed " +
"context url:" +
url);
} else if (lastSlash < ctxFile.length() - 1) {
ctxFile = ctxFile.substring(0, lastSlash + 1);
}
ctxFile = ctxFile.substring(0, lastSlash + 1);
}
return (ctxFile + spec);
}