Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

8267840: Improve URLStreamHandler.parseURL() #4526

Closed
wants to merge 4 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
39 changes: 17 additions & 22 deletions src/java.base/share/classes/java/net/URLStreamHandler.java
Expand Up @@ -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
Expand Down Expand Up @@ -158,13 +153,12 @@ protected void parseURL(URL u, String spec, int start, int limit) {
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) == '/') &&
Expand Down Expand Up @@ -249,7 +243,7 @@ protected void parseURL(URL u, String spec, int start, int limit) {
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 = "";
}

Expand All @@ -259,26 +253,27 @@ protected void parseURL(URL u, String spec, int start, int limit) {

// 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 = "";
Expand All @@ -299,7 +294,7 @@ protected void parseURL(URL u, String spec, int start, int limit) {
*/
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;
Expand Down