From 899761f0a8890353dc01f10d2af96ae192f3e655 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 See gh-25690 --- .../web/util/UrlPathHelper.java | 13 +------ .../springframework/web/util/WebUtils.java | 5 ++- .../web/util/UrlPathHelperTests.java | 14 ++------ .../web/util/WebUtilsTests.java | 34 +++++++++++++------ 4 files changed, 31 insertions(+), 35 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 79673637dbc..8e6118c1a51 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 @@ -556,8 +556,7 @@ public class UrlPathHelper { * @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) { @@ -571,16 +570,6 @@ public class UrlPathHelper { 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 fec0cfdbe87..8a0e76127ae 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-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. @@ -733,6 +733,9 @@ public abstract class WebUtils { 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 82a069ae128..0797120c32d 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-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. @@ -126,22 +126,14 @@ public class UrlPathHelperTests { } @Test - public void getRequestKeepSemicolonContent() throws UnsupportedEncodingException { + public void getRequestKeepSemicolonContent() { helper.setRemoveSemicolonContent(false); request.setRequestURI("/foo;a=b;c=d"); assertThat(helper.getRequestUri(request)).isEqualTo("/foo;a=b;c=d"); request.setRequestURI("/foo;jsessionid=c0o7fszeb1"); - assertThat(helper.getRequestUri(request)).as("jsessionid should always be removed").isEqualTo("/foo"); - - request.setRequestURI("/foo;a=b;jsessionid=c0o7fszeb1;c=d"); - assertThat(helper.getRequestUri(request)).as("jsessionid should always be removed").isEqualTo("/foo;a=b;c=d"); - - // SPR-10398 - - request.setRequestURI("/foo;a=b;JSESSIONID=c0o7fszeb1;c=d"); - assertThat(helper.getRequestUri(request)).as("JSESSIONID should always be removed").isEqualTo("/foo;a=b;c=d"); + assertThat(helper.getRequestUri(request)).isEqualTo("/foo;jsessionid=c0o7fszeb1"); } @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 68226877b16..998af1c9eb1 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. @@ -16,7 +16,6 @@ package org.springframework.web.util; -import java.util.Arrays; import java.util.Collections; import java.util.HashMap; import java.util.List; @@ -65,29 +64,42 @@ public class WebUtilsTests { MultiValueMap variables; variables = WebUtils.parseMatrixVariables(null); - assertThat(variables.size()).isEqualTo(0); + assertThat(variables).hasSize(0); variables = WebUtils.parseMatrixVariables("year"); - assertThat(variables.size()).isEqualTo(1); + assertThat(variables).hasSize(1); assertThat(variables.getFirst("year")).isEqualTo(""); variables = WebUtils.parseMatrixVariables("year=2012"); - assertThat(variables.size()).isEqualTo(1); + assertThat(variables).hasSize(1); assertThat(variables.getFirst("year")).isEqualTo("2012"); variables = WebUtils.parseMatrixVariables("year=2012;colors=red,blue,green"); - assertThat(variables.size()).isEqualTo(2); - assertThat(variables.get("colors")).isEqualTo(Arrays.asList("red", "blue", "green")); + assertThat(variables).hasSize(2); + assertThat(variables.get("colors")).containsExactly("red", "blue", "green"); assertThat(variables.getFirst("year")).isEqualTo("2012"); variables = WebUtils.parseMatrixVariables(";year=2012;colors=red,blue,green;"); - assertThat(variables.size()).isEqualTo(2); - assertThat(variables.get("colors")).isEqualTo(Arrays.asList("red", "blue", "green")); + assertThat(variables).hasSize(2); + assertThat(variables.get("colors")).containsExactly("red", "blue", "green"); assertThat(variables.getFirst("year")).isEqualTo("2012"); variables = WebUtils.parseMatrixVariables("colors=red;colors=blue;colors=green"); - assertThat(variables.size()).isEqualTo(1); - assertThat(variables.get("colors")).isEqualTo(Arrays.asList("red", "blue", "green")); + assertThat(variables).hasSize(1); + assertThat(variables.get("colors")).containsExactly("red", "blue", "green"); + + variables = WebUtils.parseMatrixVariables("jsessionid=c0o7fszeb1"); + assertThat(variables).isEmpty(); + + variables = WebUtils.parseMatrixVariables("a=b;jsessionid=c0o7fszeb1;c=d"); + assertThat(variables).hasSize(2); + assertThat(variables.get("a")).containsExactly("b"); + assertThat(variables.get("c")).containsExactly("d"); + + variables = WebUtils.parseMatrixVariables("a=b;jsessionid=c0o7fszeb1;c=d"); + assertThat(variables).hasSize(2); + assertThat(variables.get("a")).containsExactly("b"); + assertThat(variables.get("c")).containsExactly("d"); } @Test