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