Skip to content

Commit 2f75212

Browse files
committedSep 8, 2020
Avoid unnecessary parsing of path params
Closes gh-25690

File tree

4 files changed

+22
-25
lines changed

4 files changed

+22
-25
lines changed
 

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

+1-12
Original file line numberDiff line numberDiff line change
@@ -521,8 +521,7 @@ protected String determineEncoding(HttpServletRequest request) {
521521
* @return the updated URI string
522522
*/
523523
public String removeSemicolonContent(String requestUri) {
524-
return (this.removeSemicolonContent ?
525-
removeSemicolonContentInternal(requestUri) : removeJsessionid(requestUri));
524+
return (this.removeSemicolonContent ? removeSemicolonContentInternal(requestUri) : requestUri);
526525
}
527526

528527
private String removeSemicolonContentInternal(String requestUri) {
@@ -536,16 +535,6 @@ private String removeSemicolonContentInternal(String requestUri) {
536535
return requestUri;
537536
}
538537

539-
private String removeJsessionid(String requestUri) {
540-
int startIndex = requestUri.toLowerCase().indexOf(";jsessionid=");
541-
if (startIndex != -1) {
542-
int endIndex = requestUri.indexOf(';', startIndex + 12);
543-
String start = requestUri.substring(0, startIndex);
544-
requestUri = (endIndex != -1) ? start + requestUri.substring(endIndex) : start;
545-
}
546-
return requestUri;
547-
}
548-
549538
/**
550539
* Decode the given URI path variables via {@link #decodeRequestString} unless
551540
* {@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-2018 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.
@@ -774,6 +774,9 @@ public static MultiValueMap<String, String> parseMatrixVariables(String matrixVa
774774
int index = pair.indexOf('=');
775775
if (index != -1) {
776776
String name = pair.substring(0, index);
777+
if (name.equalsIgnoreCase("jsessionid")) {
778+
continue;
779+
}
777780
String rawValue = pair.substring(index + 1);
778781
for (String value : StringUtils.commaDelimitedListToStringArray(rawValue)) {
779782
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-2015 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
assertEquals("/foo;a=b;c=d", helper.getRequestUri(request));
134134

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

147139
@Test

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

+14-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.
@@ -111,6 +111,19 @@ public void parseMatrixVariablesString() {
111111
variables = WebUtils.parseMatrixVariables("colors=red;colors=blue;colors=green");
112112
assertEquals(1, variables.size());
113113
assertEquals(Arrays.asList("red", "blue", "green"), variables.get("colors"));
114+
115+
variables = WebUtils.parseMatrixVariables("jsessionid=c0o7fszeb1");
116+
assertTrue(variables.isEmpty());
117+
118+
variables = WebUtils.parseMatrixVariables("a=b;jsessionid=c0o7fszeb1;c=d");
119+
assertEquals(2, variables.size());
120+
assertEquals(Collections.singletonList("b"), variables.get("a"));
121+
assertEquals(Collections.singletonList("d"), variables.get("c"));
122+
123+
variables = WebUtils.parseMatrixVariables("a=b;jsessionid=c0o7fszeb1;c=d");
124+
assertEquals(2, variables.size());
125+
assertEquals(Collections.singletonList("b"), variables.get("a"));
126+
assertEquals(Collections.singletonList("d"), variables.get("c"));
114127
}
115128

116129
@Test

0 commit comments

Comments
 (0)
Please sign in to comment.