Skip to content

Commit a63c888

Browse files
committed
Avoid unnecessary parsing of path params
Closes gh-25690
1 parent 2281e42 commit a63c888

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
@@ -522,8 +522,7 @@ protected String determineEncoding(HttpServletRequest request) {
522522
* @return the updated URI string
523523
*/
524524
public String removeSemicolonContent(String requestUri) {
525-
return (this.removeSemicolonContent ?
526-
removeSemicolonContentInternal(requestUri) : removeJsessionid(requestUri));
525+
return (this.removeSemicolonContent ? removeSemicolonContentInternal(requestUri) : requestUri);
527526
}
528527

529528
private String removeSemicolonContentInternal(String requestUri) {
@@ -537,16 +536,6 @@ private String removeSemicolonContentInternal(String requestUri) {
537536
return requestUri;
538537
}
539538

540-
private String removeJsessionid(String requestUri) {
541-
int startIndex = requestUri.toLowerCase().indexOf(";jsessionid=");
542-
if (startIndex != -1) {
543-
int endIndex = requestUri.indexOf(';', startIndex + 12);
544-
String start = requestUri.substring(0, startIndex);
545-
requestUri = (endIndex != -1) ? start + requestUri.substring(endIndex) : start;
546-
}
547-
return requestUri;
548-
}
549-
550539
/**
551540
* Decode the given URI path variables via {@link #decodeRequestString} unless
552541
* {@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.
@@ -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

Lines changed: 3 additions & 11 deletions
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.
@@ -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.
@@ -88,6 +88,19 @@ public void parseMatrixVariablesString() {
8888
variables = WebUtils.parseMatrixVariables("colors=red;colors=blue;colors=green");
8989
assertEquals(1, variables.size());
9090
assertEquals(Arrays.asList("red", "blue", "green"), variables.get("colors"));
91+
92+
variables = WebUtils.parseMatrixVariables("jsessionid=c0o7fszeb1");
93+
assertTrue(variables.isEmpty());
94+
95+
variables = WebUtils.parseMatrixVariables("a=b;jsessionid=c0o7fszeb1;c=d");
96+
assertEquals(2, variables.size());
97+
assertEquals(Collections.singletonList("b"), variables.get("a"));
98+
assertEquals(Collections.singletonList("d"), variables.get("c"));
99+
100+
variables = WebUtils.parseMatrixVariables("a=b;jsessionid=c0o7fszeb1;c=d");
101+
assertEquals(2, variables.size());
102+
assertEquals(Collections.singletonList("b"), variables.get("a"));
103+
assertEquals(Collections.singletonList("d"), variables.get("c"));
91104
}
92105

93106
@Test

0 commit comments

Comments
 (0)