From c6ce65ef564dfb89944ae07dd3a2c25f28fc287f Mon Sep 17 00:00:00 2001 From: Rossen Stoyanchev Date: Tue, 9 Nov 2021 09:45:35 +0000 Subject: [PATCH] Polishing contribution Closes gh-27623 --- .../springframework/web/util/UrlPathHelper.java | 15 ++++++++------- .../web/util/UrlPathHelperTests.java | 4 ++-- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/spring-web/src/main/java/org/springframework/web/util/UrlPathHelper.java b/spring-web/src/main/java/org/springframework/web/util/UrlPathHelper.java index a4a85e707b..685f376b42 100644 --- a/spring-web/src/main/java/org/springframework/web/util/UrlPathHelper.java +++ b/spring-web/src/main/java/org/springframework/web/util/UrlPathHelper.java @@ -405,17 +405,18 @@ public class UrlPathHelper { * */ private static String getSanitizedPath(final String path) { - if (path.length() == 0) { + int start = path.indexOf("//"); + if (start == -1) { 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]; + char[] content = path.toCharArray(); + int slowIndex = start; + for (int fastIndex = start + 1; fastIndex < content.length; fastIndex++) { + if (content[fastIndex] != '/' || content[slowIndex] != '/') { + content[++slowIndex] = content[fastIndex]; } } - return new String(arr, 0, slowIndex + 1); + return new String(content, 0, slowIndex + 1); } /** diff --git a/spring-web/src/test/java/org/springframework/web/util/UrlPathHelperTests.java b/spring-web/src/test/java/org/springframework/web/util/UrlPathHelperTests.java index 49d8ed6a07..e7ef050837 100644 --- a/spring-web/src/test/java/org/springframework/web/util/UrlPathHelperTests.java +++ b/spring-web/src/test/java/org/springframework/web/util/UrlPathHelperTests.java @@ -232,12 +232,12 @@ class UrlPathHelperTests { request.setContextPath("/SPR-12372"); request.setPathInfo(null); request.setServletPath("/foo/bar/"); - request.setRequestURI("/SPR-12372/foo//bar/"); + request.setRequestURI("/SPR-12372/foo///bar/"); assertThat(helper.getLookupPathForRequest(request)).isEqualTo("/foo/bar/"); request.setServletPath("/foo/bar/"); - request.setRequestURI("/SPR-12372/foo/bar//"); + request.setRequestURI("////SPR-12372/foo/bar//"); assertThat(helper.getLookupPathForRequest(request)).isEqualTo("/foo/bar/");