Skip to content

Commit aec3a4c

Browse files
committed
Avoid unnecessary parsing of path params
Closes gh-25690
1 parent 26141b0 commit aec3a4c

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

Lines changed: 1 addition & 12 deletions
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

Lines changed: 4 additions & 1 deletion
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.
@@ -661,6 +661,9 @@ public static MultiValueMap<String, String> parseMatrixVariables(String matrixVa
661661
int index = pair.indexOf('=');
662662
if (index != -1) {
663663
String name = pair.substring(0, index);
664+
if (name.equalsIgnoreCase("jsessionid")) {
665+
continue;
666+
}
664667
String rawValue = pair.substring(index + 1);
665668
for (String value : StringUtils.commaDelimitedListToStringArray(rawValue)) {
666669
result.add(name, value);

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

Lines changed: 3 additions & 11 deletions
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

Lines changed: 14 additions & 1 deletion
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.
@@ -86,6 +86,19 @@ public void parseMatrixVariablesString() {
8686
variables = WebUtils.parseMatrixVariables("colors=red;colors=blue;colors=green");
8787
assertEquals(1, variables.size());
8888
assertEquals(Arrays.asList("red", "blue", "green"), variables.get("colors"));
89+
90+
variables = WebUtils.parseMatrixVariables("jsessionid=c0o7fszeb1");
91+
assertTrue(variables.isEmpty());
92+
93+
variables = WebUtils.parseMatrixVariables("a=b;jsessionid=c0o7fszeb1;c=d");
94+
assertEquals(2, variables.size());
95+
assertEquals(Collections.singletonList("b"), variables.get("a"));
96+
assertEquals(Collections.singletonList("d"), variables.get("c"));
97+
98+
variables = WebUtils.parseMatrixVariables("a=b;jsessionid=c0o7fszeb1;c=d");
99+
assertEquals(2, variables.size());
100+
assertEquals(Collections.singletonList("b"), variables.get("a"));
101+
assertEquals(Collections.singletonList("d"), variables.get("c"));
89102
}
90103

91104
@Test

0 commit comments

Comments
 (0)