Improve efficiency of UrlPathHelper#getSanitizedPath
See gh-27623
This commit is contained in:
parent
c5de5c9939
commit
b5743966d6
|
|
@ -405,16 +405,17 @@ public class UrlPathHelper {
|
|||
* </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);
|
||||
}
|
||||
return sanitized.toString();
|
||||
if (path.length() == 0) {
|
||||
return path;
|
||||
}
|
||||
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 new String(arr, 0, slowIndex + 1);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -532,7 +533,7 @@ public class UrlPathHelper {
|
|||
*/
|
||||
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 {
|
||||
|
|
|
|||
|
|
@ -246,6 +246,12 @@ class UrlPathHelperTests {
|
|||
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
|
||||
|
|
|
|||
Loading…
Reference in New Issue