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

Improve the efficiency of UrlPathHelper.getSanitizedPath() #27623

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
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
Expand Up @@ -404,16 +404,17 @@ else if (index1 == requestUri.length()) {
* </ul>
*/
private static String getSanitizedPath(final String path) {
int index = path.indexOf("//");
if (index >= 0) {
StringBuilder sanitized = new StringBuilder(path);
while (index != -1) {
sanitized.deleteCharAt(index);
index = sanitized.indexOf("//", index);
if (path.length() == 0) {
return path;
}
char[] arr = path.toCharArray();
int slowIndex = 0;
for (int fastIndex = 1; fastIndex < arr.length; fastIndex++) {
if (arr[fastIndex] != '/' || arr[slowIndex] != '/') {
arr[++slowIndex] = arr[fastIndex];
}
return sanitized.toString();
}
return path;
return new String(arr, 0, slowIndex + 1);
}

/**
Expand Down Expand Up @@ -531,7 +532,7 @@ public String getOriginatingServletPath(HttpServletRequest request) {
*/
public String getOriginatingQueryString(HttpServletRequest request) {
if ((request.getAttribute(WebUtils.FORWARD_REQUEST_URI_ATTRIBUTE) != null) ||
(request.getAttribute(WebUtils.ERROR_REQUEST_URI_ATTRIBUTE) != null)) {
(request.getAttribute(WebUtils.ERROR_REQUEST_URI_ATTRIBUTE) != null)) {
return (String) request.getAttribute(WebUtils.FORWARD_QUERY_STRING_ATTRIBUTE);
}
else {
Expand Down
Expand Up @@ -246,6 +246,12 @@ void removeDuplicateSlashesInPath() {
request.setRequestURI("/SPR-12372/foo/bar//");

assertThat(helper.getLookupPathForRequest(request)).isEqualTo("/foo/bar//");

// "enhance" case
request.setServletPath("/foo/bar//");
request.setRequestURI("/SPR-12372////////////////////////foo//////////////////bar////////////////////");

assertThat(helper.getLookupPathForRequest(request)).isEqualTo("/foo/bar//");
}

@Test
Expand Down