Avoid creating unnecessary garbage during URL normalization

Closes gh-11207
pull/11378/head
Andy Wilkinson 7 years ago
parent 5fd529279d
commit a491727b7c

@ -48,6 +48,10 @@ public class Handler extends URLStreamHandler {
private static final String SEPARATOR = "!/"; private static final String SEPARATOR = "!/";
private static final String CURRENT_DIR = "/./";
private static final String PARENT_DIR = "/../";
private static final String[] FALLBACK_HANDLERS = { private static final String[] FALLBACK_HANDLERS = {
"sun.net.www.protocol.jar.Handler" }; "sun.net.www.protocol.jar.Handler" };
@ -207,6 +211,9 @@ public class Handler extends URLStreamHandler {
} }
private String normalize(String file) { private String normalize(String file) {
if (!file.contains(CURRENT_DIR) && !file.contains(PARENT_DIR)) {
return file;
}
int afterLastSeparatorIndex = file.lastIndexOf(SEPARATOR) + SEPARATOR.length(); int afterLastSeparatorIndex = file.lastIndexOf(SEPARATOR) + SEPARATOR.length();
String afterSeparator = file.substring(afterLastSeparatorIndex); String afterSeparator = file.substring(afterLastSeparatorIndex);
afterSeparator = replaceParentDir(afterSeparator); afterSeparator = replaceParentDir(afterSeparator);
@ -216,7 +223,7 @@ public class Handler extends URLStreamHandler {
private String replaceParentDir(String file) { private String replaceParentDir(String file) {
int parentDirIndex; int parentDirIndex;
while ((parentDirIndex = file.indexOf("/../")) >= 0) { while ((parentDirIndex = file.indexOf(PARENT_DIR)) >= 0) {
int precedingSlashIndex = file.lastIndexOf('/', parentDirIndex - 1); int precedingSlashIndex = file.lastIndexOf('/', parentDirIndex - 1);
if (precedingSlashIndex >= 0) { if (precedingSlashIndex >= 0) {
file = file.substring(0, precedingSlashIndex) file = file.substring(0, precedingSlashIndex)
@ -230,7 +237,7 @@ public class Handler extends URLStreamHandler {
} }
private String replaceCurrentDir(String file) { private String replaceCurrentDir(String file) {
return file.replace("/./", "/"); return file.replace(CURRENT_DIR, "/");
} }
@Override @Override

Loading…
Cancel
Save