Skip to content

Commit 899761f

Browse files
committedSep 7, 2020
Avoid unnecessary parsing of path params
See gh-25690
1 parent f88759c commit 899761f

File tree

4 files changed

+31
-35
lines changed

4 files changed

+31
-35
lines changed
 

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

+1-12
Original file line numberDiff line numberDiff line change
@@ -556,8 +556,7 @@ protected String determineEncoding(HttpServletRequest request) {
556556
* @return the updated URI string
557557
*/
558558
public String removeSemicolonContent(String requestUri) {
559-
return (this.removeSemicolonContent ?
560-
removeSemicolonContentInternal(requestUri) : removeJsessionid(requestUri));
559+
return (this.removeSemicolonContent ? removeSemicolonContentInternal(requestUri) : requestUri);
561560
}
562561

563562
private String removeSemicolonContentInternal(String requestUri) {
@@ -571,16 +570,6 @@ private String removeSemicolonContentInternal(String requestUri) {
571570
return requestUri;
572571
}
573572

574-
private String removeJsessionid(String requestUri) {
575-
int startIndex = requestUri.toLowerCase().indexOf(";jsessionid=");
576-
if (startIndex != -1) {
577-
int endIndex = requestUri.indexOf(';', startIndex + 12);
578-
String start = requestUri.substring(0, startIndex);
579-
requestUri = (endIndex != -1) ? start + requestUri.substring(endIndex) : start;
580-
}
581-
return requestUri;
582-
}
583-
584573
/**
585574
* Decode the given URI path variables via {@link #decodeRequestString} unless
586575
* {@link #setUrlDecode} is set to {@code true} in which case it is assumed

‎spring-web/src/main/java/org/springframework/web/util/WebUtils.java

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -733,6 +733,9 @@ public static MultiValueMap<String, String> parseMatrixVariables(String matrixVa
733733
int index = pair.indexOf('=');
734734
if (index != -1) {
735735
String name = pair.substring(0, index);
736+
if (name.equalsIgnoreCase("jsessionid")) {
737+
continue;
738+
}
736739
String rawValue = pair.substring(index + 1);
737740
for (String value : StringUtils.commaDelimitedListToStringArray(rawValue)) {
738741
result.add(name, value);

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

+3-11
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -126,22 +126,14 @@ public void getRequestRemoveSemicolonContent() throws UnsupportedEncodingExcepti
126126
}
127127

128128
@Test
129-
public void getRequestKeepSemicolonContent() throws UnsupportedEncodingException {
129+
public void getRequestKeepSemicolonContent() {
130130
helper.setRemoveSemicolonContent(false);
131131

132132
request.setRequestURI("/foo;a=b;c=d");
133133
assertThat(helper.getRequestUri(request)).isEqualTo("/foo;a=b;c=d");
134134

135135
request.setRequestURI("/foo;jsessionid=c0o7fszeb1");
136-
assertThat(helper.getRequestUri(request)).as("jsessionid should always be removed").isEqualTo("/foo");
137-
138-
request.setRequestURI("/foo;a=b;jsessionid=c0o7fszeb1;c=d");
139-
assertThat(helper.getRequestUri(request)).as("jsessionid should always be removed").isEqualTo("/foo;a=b;c=d");
140-
141-
// SPR-10398
142-
143-
request.setRequestURI("/foo;a=b;JSESSIONID=c0o7fszeb1;c=d");
144-
assertThat(helper.getRequestUri(request)).as("JSESSIONID should always be removed").isEqualTo("/foo;a=b;c=d");
136+
assertThat(helper.getRequestUri(request)).isEqualTo("/foo;jsessionid=c0o7fszeb1");
145137
}
146138

147139
@Test

‎spring-web/src/test/java/org/springframework/web/util/WebUtilsTests.java

+23-11
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,7 +16,6 @@
1616

1717
package org.springframework.web.util;
1818

19-
import java.util.Arrays;
2019
import java.util.Collections;
2120
import java.util.HashMap;
2221
import java.util.List;
@@ -65,29 +64,42 @@ public void parseMatrixVariablesString() {
6564
MultiValueMap<String, String> variables;
6665

6766
variables = WebUtils.parseMatrixVariables(null);
68-
assertThat(variables.size()).isEqualTo(0);
67+
assertThat(variables).hasSize(0);
6968

7069
variables = WebUtils.parseMatrixVariables("year");
71-
assertThat(variables.size()).isEqualTo(1);
70+
assertThat(variables).hasSize(1);
7271
assertThat(variables.getFirst("year")).isEqualTo("");
7372

7473
variables = WebUtils.parseMatrixVariables("year=2012");
75-
assertThat(variables.size()).isEqualTo(1);
74+
assertThat(variables).hasSize(1);
7675
assertThat(variables.getFirst("year")).isEqualTo("2012");
7776

7877
variables = WebUtils.parseMatrixVariables("year=2012;colors=red,blue,green");
79-
assertThat(variables.size()).isEqualTo(2);
80-
assertThat(variables.get("colors")).isEqualTo(Arrays.asList("red", "blue", "green"));
78+
assertThat(variables).hasSize(2);
79+
assertThat(variables.get("colors")).containsExactly("red", "blue", "green");
8180
assertThat(variables.getFirst("year")).isEqualTo("2012");
8281

8382
variables = WebUtils.parseMatrixVariables(";year=2012;colors=red,blue,green;");
84-
assertThat(variables.size()).isEqualTo(2);
85-
assertThat(variables.get("colors")).isEqualTo(Arrays.asList("red", "blue", "green"));
83+
assertThat(variables).hasSize(2);
84+
assertThat(variables.get("colors")).containsExactly("red", "blue", "green");
8685
assertThat(variables.getFirst("year")).isEqualTo("2012");
8786

8887
variables = WebUtils.parseMatrixVariables("colors=red;colors=blue;colors=green");
89-
assertThat(variables.size()).isEqualTo(1);
90-
assertThat(variables.get("colors")).isEqualTo(Arrays.asList("red", "blue", "green"));
88+
assertThat(variables).hasSize(1);
89+
assertThat(variables.get("colors")).containsExactly("red", "blue", "green");
90+
91+
variables = WebUtils.parseMatrixVariables("jsessionid=c0o7fszeb1");
92+
assertThat(variables).isEmpty();
93+
94+
variables = WebUtils.parseMatrixVariables("a=b;jsessionid=c0o7fszeb1;c=d");
95+
assertThat(variables).hasSize(2);
96+
assertThat(variables.get("a")).containsExactly("b");
97+
assertThat(variables.get("c")).containsExactly("d");
98+
99+
variables = WebUtils.parseMatrixVariables("a=b;jsessionid=c0o7fszeb1;c=d");
100+
assertThat(variables).hasSize(2);
101+
assertThat(variables.get("a")).containsExactly("b");
102+
assertThat(variables.get("c")).containsExactly("d");
91103
}
92104

93105
@Test

0 commit comments

Comments
 (0)