Skip to content

Commit

Permalink
Improve efficiency of UrlPathHelper#getSanitizedPath
Browse files Browse the repository at this point in the history
  • Loading branch information
happyWilliam0 authored and rstoyanchev committed Nov 9, 2021
1 parent c5de5c9 commit b574396
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 9 deletions.
Expand Up @@ -405,16 +405,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 @@ -532,7 +533,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

0 comments on commit b574396

Please sign in to comment.