Skip to content

Commit c6ce65e

Browse files
committed
Polishing contribution
Closes gh-27623
1 parent b574396 commit c6ce65e

File tree

2 files changed

+10
-9
lines changed

2 files changed

+10
-9
lines changed

spring-web/src/main/java/org/springframework/web/util/UrlPathHelper.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -405,17 +405,18 @@ else if (index1 == requestUri.length()) {
405405
* </ul>
406406
*/
407407
private static String getSanitizedPath(final String path) {
408-
if (path.length() == 0) {
408+
int start = path.indexOf("//");
409+
if (start == -1) {
409410
return path;
410411
}
411-
char[] arr = path.toCharArray();
412-
int slowIndex = 0;
413-
for (int fastIndex = 1; fastIndex < arr.length; fastIndex++) {
414-
if (arr[fastIndex] != '/' || arr[slowIndex] != '/') {
415-
arr[++slowIndex] = arr[fastIndex];
412+
char[] content = path.toCharArray();
413+
int slowIndex = start;
414+
for (int fastIndex = start + 1; fastIndex < content.length; fastIndex++) {
415+
if (content[fastIndex] != '/' || content[slowIndex] != '/') {
416+
content[++slowIndex] = content[fastIndex];
416417
}
417418
}
418-
return new String(arr, 0, slowIndex + 1);
419+
return new String(content, 0, slowIndex + 1);
419420
}
420421

421422
/**

spring-web/src/test/java/org/springframework/web/util/UrlPathHelperTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -232,12 +232,12 @@ void removeDuplicateSlashesInPath() {
232232
request.setContextPath("/SPR-12372");
233233
request.setPathInfo(null);
234234
request.setServletPath("/foo/bar/");
235-
request.setRequestURI("/SPR-12372/foo//bar/");
235+
request.setRequestURI("/SPR-12372/foo///bar/");
236236

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

239239
request.setServletPath("/foo/bar/");
240-
request.setRequestURI("/SPR-12372/foo/bar//");
240+
request.setRequestURI("////SPR-12372/foo/bar//");
241241

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

0 commit comments

Comments
 (0)