From 2f75212eb667a30fe2fa9b5aca8f22d5e255821f Mon Sep 17 00:00:00 2001 From: Rossen Stoyanchev Date: Thu, 3 Sep 2020 20:35:06 +0100 Subject: [PATCH] Avoid unnecessary parsing of path params Closes gh-25690 --- .../springframework/web/util/UrlPathHelper.java | 13 +------------ .../org/springframework/web/util/WebUtils.java | 5 ++++- .../web/util/UrlPathHelperTests.java | 14 +++----------- .../springframework/web/util/WebUtilsTests.java | 15 ++++++++++++++- 4 files changed, 22 insertions(+), 25 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 9f256fead228..14a20c7e0800 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 @@ -521,8 +521,7 @@ protected String determineEncoding(HttpServletRequest request) { * @return the updated URI string */ public String removeSemicolonContent(String requestUri) { - return (this.removeSemicolonContent ? - removeSemicolonContentInternal(requestUri) : removeJsessionid(requestUri)); + return (this.removeSemicolonContent ? removeSemicolonContentInternal(requestUri) : requestUri); } private String removeSemicolonContentInternal(String requestUri) { @@ -536,16 +535,6 @@ private String removeSemicolonContentInternal(String requestUri) { return requestUri; } - private String removeJsessionid(String requestUri) { - int startIndex = requestUri.toLowerCase().indexOf(";jsessionid="); - if (startIndex != -1) { - int endIndex = requestUri.indexOf(';', startIndex + 12); - String start = requestUri.substring(0, startIndex); - requestUri = (endIndex != -1) ? start + requestUri.substring(endIndex) : start; - } - return requestUri; - } - /** * Decode the given URI path variables via {@link #decodeRequestString} unless * {@link #setUrlDecode} is set to {@code true} in which case it is assumed diff --git a/spring-web/src/main/java/org/springframework/web/util/WebUtils.java b/spring-web/src/main/java/org/springframework/web/util/WebUtils.java index 49ba3894484a..08a1f5fa0b9a 100644 --- a/spring-web/src/main/java/org/springframework/web/util/WebUtils.java +++ b/spring-web/src/main/java/org/springframework/web/util/WebUtils.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2020 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -774,6 +774,9 @@ public static MultiValueMap parseMatrixVariables(String matrixVa int index = pair.indexOf('='); if (index != -1) { String name = pair.substring(0, index); + if (name.equalsIgnoreCase("jsessionid")) { + continue; + } String rawValue = pair.substring(index + 1); for (String value : StringUtils.commaDelimitedListToStringArray(rawValue)) { result.add(name, value); 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 b27ef6d8475f..361972f56aba 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 @@ -1,5 +1,5 @@ /* - * Copyright 2002-2015 the original author or authors. + * Copyright 2002-2020 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -126,22 +126,14 @@ public void getRequestRemoveSemicolonContent() throws UnsupportedEncodingExcepti } @Test - public void getRequestKeepSemicolonContent() throws UnsupportedEncodingException { + public void getRequestKeepSemicolonContent() { helper.setRemoveSemicolonContent(false); request.setRequestURI("/foo;a=b;c=d"); assertEquals("/foo;a=b;c=d", helper.getRequestUri(request)); request.setRequestURI("/foo;jsessionid=c0o7fszeb1"); - assertEquals("jsessionid should always be removed", "/foo", helper.getRequestUri(request)); - - request.setRequestURI("/foo;a=b;jsessionid=c0o7fszeb1;c=d"); - assertEquals("jsessionid should always be removed", "/foo;a=b;c=d", helper.getRequestUri(request)); - - // SPR-10398 - - request.setRequestURI("/foo;a=b;JSESSIONID=c0o7fszeb1;c=d"); - assertEquals("JSESSIONID should always be removed", "/foo;a=b;c=d", helper.getRequestUri(request)); + assertEquals("/foo;jsessionid=c0o7fszeb1", helper.getRequestUri(request)); } @Test diff --git a/spring-web/src/test/java/org/springframework/web/util/WebUtilsTests.java b/spring-web/src/test/java/org/springframework/web/util/WebUtilsTests.java index 9014cad727c4..ef5acc0ef529 100644 --- a/spring-web/src/test/java/org/springframework/web/util/WebUtilsTests.java +++ b/spring-web/src/test/java/org/springframework/web/util/WebUtilsTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2019 the original author or authors. + * Copyright 2002-2020 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -111,6 +111,19 @@ public void parseMatrixVariablesString() { variables = WebUtils.parseMatrixVariables("colors=red;colors=blue;colors=green"); assertEquals(1, variables.size()); assertEquals(Arrays.asList("red", "blue", "green"), variables.get("colors")); + + variables = WebUtils.parseMatrixVariables("jsessionid=c0o7fszeb1"); + assertTrue(variables.isEmpty()); + + variables = WebUtils.parseMatrixVariables("a=b;jsessionid=c0o7fszeb1;c=d"); + assertEquals(2, variables.size()); + assertEquals(Collections.singletonList("b"), variables.get("a")); + assertEquals(Collections.singletonList("d"), variables.get("c")); + + variables = WebUtils.parseMatrixVariables("a=b;jsessionid=c0o7fszeb1;c=d"); + assertEquals(2, variables.size()); + assertEquals(Collections.singletonList("b"), variables.get("a")); + assertEquals(Collections.singletonList("d"), variables.get("c")); } @Test