Skip to content

Commit dd011c9

Browse files
committed
Merge branch '5.2.x' into master
2 parents 59ecede + d616c66 commit dd011c9

File tree

11 files changed

+145
-92
lines changed

11 files changed

+145
-92
lines changed

spring-web/src/main/java/org/springframework/http/converter/json/AbstractJackson2HttpMessageConverter.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,15 @@ private Object readJavaType(JavaType javaType, HttpInputMessage inputMessage) th
285285
}
286286
}
287287

288-
private static Charset getCharset(@Nullable MediaType contentType) {
288+
/**
289+
* Return the charset to use for JSON input.
290+
* <p>By default this is either the charset from the input {@code MediaType}
291+
* or otherwise falling back on {@code UTF-8}.
292+
* @param contentType the content type of the HTTP input message
293+
* @return the charset to use
294+
* @since 5.1.18
295+
*/
296+
protected static Charset getCharset(@Nullable MediaType contentType) {
289297
if (contentType != null && contentType.getCharset() != null) {
290298
return contentType.getCharset();
291299
}

spring-web/src/main/java/org/springframework/web/filter/ForwardedHeaderFilter.java

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -81,20 +81,11 @@ public class ForwardedHeaderFilter extends OncePerRequestFilter {
8181
}
8282

8383

84-
private final UrlPathHelper pathHelper;
85-
8684
private boolean removeOnly;
8785

8886
private boolean relativeRedirects;
8987

9088

91-
public ForwardedHeaderFilter() {
92-
this.pathHelper = new UrlPathHelper();
93-
this.pathHelper.setUrlDecode(false);
94-
this.pathHelper.setRemoveSemicolonContent(false);
95-
}
96-
97-
9889
/**
9990
* Enables mode in which any "Forwarded" or "X-Forwarded-*" headers are
10091
* removed only and the information in them ignored.
@@ -151,7 +142,7 @@ protected void doFilterInternal(HttpServletRequest request, HttpServletResponse
151142
}
152143
else {
153144
HttpServletRequest wrappedRequest =
154-
new ForwardedHeaderExtractingRequest(request, this.pathHelper);
145+
new ForwardedHeaderExtractingRequest(request);
155146

156147
HttpServletResponse wrappedResponse = this.relativeRedirects ?
157148
RelativeRedirectResponseWrapper.wrapIfNecessary(response, HttpStatus.SEE_OTHER) :
@@ -235,7 +226,7 @@ private static class ForwardedHeaderExtractingRequest extends ForwardedHeaderRem
235226
private final ForwardedPrefixExtractor forwardedPrefixExtractor;
236227

237228

238-
ForwardedHeaderExtractingRequest(HttpServletRequest servletRequest, UrlPathHelper pathHelper) {
229+
ForwardedHeaderExtractingRequest(HttpServletRequest servletRequest) {
239230
super(servletRequest);
240231

241232
ServerHttpRequest request = new ServletServerHttpRequest(servletRequest);
@@ -251,7 +242,7 @@ private static class ForwardedHeaderExtractingRequest extends ForwardedHeaderRem
251242

252243
String baseUrl = this.scheme + "://" + this.host + (port == -1 ? "" : ":" + port);
253244
Supplier<HttpServletRequest> delegateRequest = () -> (HttpServletRequest) getRequest();
254-
this.forwardedPrefixExtractor = new ForwardedPrefixExtractor(delegateRequest, pathHelper, baseUrl);
245+
this.forwardedPrefixExtractor = new ForwardedPrefixExtractor(delegateRequest, baseUrl);
255246
}
256247

257248

@@ -320,8 +311,6 @@ private static class ForwardedPrefixExtractor {
320311

321312
private final Supplier<HttpServletRequest> delegate;
322313

323-
private final UrlPathHelper pathHelper;
324-
325314
private final String baseUrl;
326315

327316
private String actualRequestUri;
@@ -340,14 +329,10 @@ private static class ForwardedPrefixExtractor {
340329
* @param delegateRequest supplier for the current
341330
* {@link HttpServletRequestWrapper#getRequest() delegate request} which
342331
* may change during a forward (e.g. Tomcat.
343-
* @param pathHelper the path helper instance
344332
* @param baseUrl the host, scheme, and port based on forwarded headers
345333
*/
346-
public ForwardedPrefixExtractor(
347-
Supplier<HttpServletRequest> delegateRequest, UrlPathHelper pathHelper, String baseUrl) {
348-
334+
public ForwardedPrefixExtractor(Supplier<HttpServletRequest> delegateRequest, String baseUrl) {
349335
this.delegate = delegateRequest;
350-
this.pathHelper = pathHelper;
351336
this.baseUrl = baseUrl;
352337
this.actualRequestUri = delegateRequest.get().getRequestURI();
353338

@@ -384,7 +369,8 @@ private static String initForwardedPrefix(HttpServletRequest request) {
384369
@Nullable
385370
private String initRequestUri() {
386371
if (this.forwardedPrefix != null) {
387-
return this.forwardedPrefix + this.pathHelper.getPathWithinApplication(this.delegate.get());
372+
return this.forwardedPrefix +
373+
UrlPathHelper.rawPathInstance.getPathWithinApplication(this.delegate.get());
388374
}
389375
return null;
390376
}

spring-web/src/main/java/org/springframework/web/util/UriComponentsBuilder.java

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,13 @@ public static UriComponentsBuilder fromPath(String path) {
192192
}
193193

194194
/**
195-
* Create a builder that is initialized with the given {@code URI}.
195+
* Create a builder that is initialized from the given {@code URI}.
196+
* <p><strong>Note:</strong> the components in the resulting builder will be
197+
* in fully encoded (raw) form and further changes must also supply values
198+
* that are fully encoded, for example via methods in {@link UriUtils}.
199+
* In addition please use {@link #build(boolean)} with a value of "true" to
200+
* build the {@link UriComponents} instance in order to indicate that the
201+
* components are encoded.
196202
* @param uri the URI to initialize with
197203
* @return the new {@code UriComponentsBuilder}
198204
*/
@@ -439,11 +445,13 @@ public UriComponents build() {
439445
}
440446

441447
/**
442-
* Build a {@code UriComponents} instance from the various components
443-
* contained in this builder.
444-
* @param encoded whether all the components set in this builder are
445-
* encoded ({@code true}) or not ({@code false})
448+
* Variant of {@link #build()} to create a {@link UriComponents} instance
449+
* when components are already fully encoded. This is useful for example if
450+
* the builder was created via {@link UriComponentsBuilder#fromUri(URI)}.
451+
* @param encoded whether the components in this builder are already encoded
446452
* @return the URI components
453+
* @throws IllegalArgumentException if any of the components contain illegal
454+
* characters that should have been encoded.
447455
*/
448456
public UriComponents build(boolean encoded) {
449457
return buildInternal(encoded ?

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

Lines changed: 40 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,8 @@ public class UrlPathHelper {
8585

8686
private String defaultEncoding = WebUtils.DEFAULT_CHARACTER_ENCODING;
8787

88+
private boolean readOnly = false;
89+
8890

8991
/**
9092
* Whether URL lookups should always use the full path within the current
@@ -96,6 +98,7 @@ public class UrlPathHelper {
9698
* <p>By default this is set to "false".
9799
*/
98100
public void setAlwaysUseFullPath(boolean alwaysUseFullPath) {
101+
checkReadOnly();
99102
this.alwaysUseFullPath = alwaysUseFullPath;
100103
}
101104

@@ -118,6 +121,7 @@ public void setAlwaysUseFullPath(boolean alwaysUseFullPath) {
118121
* @see java.net.URLDecoder#decode(String, String)
119122
*/
120123
public void setUrlDecode(boolean urlDecode) {
124+
checkReadOnly();
121125
this.urlDecode = urlDecode;
122126
}
123127

@@ -134,13 +138,15 @@ public boolean isUrlDecode() {
134138
* <p>Default is "true".
135139
*/
136140
public void setRemoveSemicolonContent(boolean removeSemicolonContent) {
141+
checkReadOnly();
137142
this.removeSemicolonContent = removeSemicolonContent;
138143
}
139144

140145
/**
141146
* Whether configured to remove ";" (semicolon) content from the request URI.
142147
*/
143148
public boolean shouldRemoveSemicolonContent() {
149+
checkReadOnly();
144150
return this.removeSemicolonContent;
145151
}
146152

@@ -158,6 +164,7 @@ public boolean shouldRemoveSemicolonContent() {
158164
* @see WebUtils#DEFAULT_CHARACTER_ENCODING
159165
*/
160166
public void setDefaultEncoding(String defaultEncoding) {
167+
checkReadOnly();
161168
this.defaultEncoding = defaultEncoding;
162169
}
163170

@@ -168,6 +175,17 @@ protected String getDefaultEncoding() {
168175
return this.defaultEncoding;
169176
}
170177

178+
/**
179+
* Switch to read-only mode where further configuration changes are not allowed.
180+
*/
181+
private void setReadOnly() {
182+
this.readOnly = true;
183+
}
184+
185+
private void checkReadOnly() {
186+
Assert.isTrue(!this.readOnly, "This instance cannot be modified");
187+
}
188+
171189

172190
/**
173191
* {@link #getLookupPathForRequest Resolve} the lookupPath and cache it in a
@@ -590,8 +608,7 @@ protected String determineEncoding(HttpServletRequest request) {
590608
* @return the updated URI string
591609
*/
592610
public String removeSemicolonContent(String requestUri) {
593-
return (this.removeSemicolonContent ?
594-
removeSemicolonContentInternal(requestUri) : removeJsessionid(requestUri));
611+
return (this.removeSemicolonContent ? removeSemicolonContentInternal(requestUri) : requestUri);
595612
}
596613

597614
private String removeSemicolonContentInternal(String requestUri) {
@@ -605,16 +622,6 @@ private String removeSemicolonContentInternal(String requestUri) {
605622
return requestUri;
606623
}
607624

608-
private String removeJsessionid(String requestUri) {
609-
int startIndex = requestUri.toLowerCase().indexOf(";jsessionid=");
610-
if (startIndex != -1) {
611-
int endIndex = requestUri.indexOf(';', startIndex + 12);
612-
String start = requestUri.substring(0, startIndex);
613-
requestUri = (endIndex != -1) ? start + requestUri.substring(endIndex) : start;
614-
}
615-
return requestUri;
616-
}
617-
618625
/**
619626
* Decode the given URI path variables via {@link #decodeRequestString} unless
620627
* {@link #setUrlDecode} is set to {@code true} in which case it is assumed
@@ -695,35 +702,37 @@ private boolean shouldRemoveTrailingServletPathSlash(HttpServletRequest request)
695702

696703

697704
/**
698-
* Shared, read-only instance of {@code UrlPathHelper}. Uses default settings:
705+
* Shared, read-only instance with defaults. The following apply:
699706
* <ul>
700707
* <li>{@code alwaysUseFullPath=false}
701708
* <li>{@code urlDecode=true}
702709
* <li>{@code removeSemicolon=true}
703710
* <li>{@code defaultEncoding=}{@link WebUtils#DEFAULT_CHARACTER_ENCODING}
704711
* </ul>
705712
*/
706-
public static final UrlPathHelper defaultInstance = new UrlPathHelper() {
713+
public static final UrlPathHelper defaultInstance = new UrlPathHelper();
707714

708-
@Override
709-
public void setAlwaysUseFullPath(boolean alwaysUseFullPath) {
710-
throw new UnsupportedOperationException();
711-
}
715+
static {
716+
defaultInstance.setReadOnly();
717+
}
712718

713-
@Override
714-
public void setUrlDecode(boolean urlDecode) {
715-
throw new UnsupportedOperationException();
716-
}
717719

718-
@Override
719-
public void setRemoveSemicolonContent(boolean removeSemicolonContent) {
720-
throw new UnsupportedOperationException();
721-
}
720+
/**
721+
* Shared, read-only instance for the full, encoded path. The following apply:
722+
* <ul>
723+
* <li>{@code alwaysUseFullPath=true}
724+
* <li>{@code urlDecode=false}
725+
* <li>{@code removeSemicolon=false}
726+
* <li>{@code defaultEncoding=}{@link WebUtils#DEFAULT_CHARACTER_ENCODING}
727+
* </ul>
728+
*/
729+
public static final UrlPathHelper rawPathInstance = new UrlPathHelper();
722730

723-
@Override
724-
public void setDefaultEncoding(String defaultEncoding) {
725-
throw new UnsupportedOperationException();
726-
}
727-
};
731+
static {
732+
rawPathInstance.setAlwaysUseFullPath(true);
733+
rawPathInstance.setUrlDecode(false);
734+
rawPathInstance.setRemoveSemicolonContent(false);
735+
rawPathInstance.setReadOnly();
736+
}
728737

729738
}

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-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

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -126,14 +126,7 @@ public void getRequestKeepSemicolonContent() {
126126
assertThat(helper.getRequestUri(request)).isEqualTo("/foo;a=b;c=d");
127127

128128
request.setRequestURI("/foo;jsessionid=c0o7fszeb1");
129-
assertThat(helper.getRequestUri(request)).as("jsessionid should always be removed").isEqualTo("/foo");
130-
131-
request.setRequestURI("/foo;a=b;jsessionid=c0o7fszeb1;c=d");
132-
assertThat(helper.getRequestUri(request)).as("jsessionid should always be removed").isEqualTo("/foo;a=b;c=d");
133-
134-
// SPR-10398
135-
request.setRequestURI("/foo;a=b;JSESSIONID=c0o7fszeb1;c=d");
136-
assertThat(helper.getRequestUri(request)).as("JSESSIONID should always be removed").isEqualTo("/foo;a=b;c=d");
129+
assertThat(helper.getRequestUri(request)).isEqualTo("/foo;jsessionid=c0o7fszeb1");
137130
}
138131

139132
@Test

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

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