8267840: Improve URLStreamHandler.parseURL()

Reviewed-by: dfuchs, redestad
This commit is contained in:
Sergey Tsypanov 2021-08-05 14:55:00 +00:00 committed by Claes Redestad
parent 55bd52a142
commit d7fc9e4171

View File

@ -26,13 +26,8 @@
package java.net;
import java.io.IOException;
import java.io.InputStream;
import java.io.File;
import java.io.OutputStream;
import java.util.Hashtable;
import java.util.Objects;
import sun.net.util.IPAddressUtil;
import sun.net.www.ParseUtil;
/**
* The abstract class {@code URLStreamHandler} is the common
@ -158,13 +153,12 @@ public abstract class URLStreamHandler {
queryOnly = queryStart == start;
if ((queryStart != -1) && (queryStart < limit)) {
query = spec.substring(queryStart+1, limit);
if (limit > queryStart)
limit = queryStart;
limit = queryStart;
spec = spec.substring(0, queryStart);
}
}
int i = 0;
int i;
// Parse the authority part if any
boolean isUNCName = (start <= limit - 4) &&
(spec.charAt(start) == '/') &&
@ -249,7 +243,7 @@ public abstract class URLStreamHandler {
start = i;
// If the authority is defined then the path is defined by the
// spec only; See RFC 2396 Section 5.2.4.
if (authority != null && !authority.isEmpty())
if (!authority.isEmpty())
path = "";
}
@ -259,26 +253,27 @@ public abstract class URLStreamHandler {
// Parse the file path if any
if (start < limit) {
String specStr = spec.substring(start, limit);
if (spec.charAt(start) == '/') {
path = spec.substring(start, limit);
path = specStr;
} else if (path != null && !path.isEmpty()) {
isRelPath = true;
int ind = path.lastIndexOf('/');
String separator = "";
if (ind == -1 && authority != null)
separator = "/";
path = path.substring(0, ind + 1) + separator +
spec.substring(start, limit);
if (ind == -1 && authority != null) {
path = "/".concat(specStr);
} else {
path = path.substring(0, ind + 1).concat(specStr);
}
} else {
path = spec.substring(start, limit);
path = (authority != null) ? "/" + path : path;
path = (authority != null) ? "/".concat(specStr) : specStr;
}
} else if (queryOnly && path != null) {
int ind = path.lastIndexOf('/');
if (ind < 0)
ind = 0;
path = path.substring(0, ind) + "/";
if (ind < 0) {
path = "/";
} else {
path = path.substring(0, ind + 1);
}
}
if (path == null)
path = "";
@ -299,7 +294,7 @@ public abstract class URLStreamHandler {
*/
if (i > 0 && (limit = path.lastIndexOf('/', i - 1)) >= 0 &&
(path.indexOf("/../", limit) != 0)) {
path = path.substring(0, limit) + path.substring(i + 3);
path = path.substring(0, limit).concat(path.substring(i + 3));
i = 0;
} else {
i = i + 3;