Merge branch '5.2.x' into master

This commit is contained in:
Rossen Stoyanchev
2020-09-07 21:41:30 +01:00
11 changed files with 145 additions and 92 deletions

View File

@@ -285,7 +285,15 @@ public abstract class AbstractJackson2HttpMessageConverter extends AbstractGener
}
}
private static Charset getCharset(@Nullable MediaType contentType) {
/**
* Return the charset to use for JSON input.
* <p>By default this is either the charset from the input {@code MediaType}
* or otherwise falling back on {@code UTF-8}.
* @param contentType the content type of the HTTP input message
* @return the charset to use
* @since 5.1.18
*/
protected static Charset getCharset(@Nullable MediaType contentType) {
if (contentType != null && contentType.getCharset() != null) {
return contentType.getCharset();
}

View File

@@ -81,20 +81,11 @@ public class ForwardedHeaderFilter extends OncePerRequestFilter {
}
private final UrlPathHelper pathHelper;
private boolean removeOnly;
private boolean relativeRedirects;
public ForwardedHeaderFilter() {
this.pathHelper = new UrlPathHelper();
this.pathHelper.setUrlDecode(false);
this.pathHelper.setRemoveSemicolonContent(false);
}
/**
* Enables mode in which any "Forwarded" or "X-Forwarded-*" headers are
* removed only and the information in them ignored.
@@ -151,7 +142,7 @@ public class ForwardedHeaderFilter extends OncePerRequestFilter {
}
else {
HttpServletRequest wrappedRequest =
new ForwardedHeaderExtractingRequest(request, this.pathHelper);
new ForwardedHeaderExtractingRequest(request);
HttpServletResponse wrappedResponse = this.relativeRedirects ?
RelativeRedirectResponseWrapper.wrapIfNecessary(response, HttpStatus.SEE_OTHER) :
@@ -235,7 +226,7 @@ public class ForwardedHeaderFilter extends OncePerRequestFilter {
private final ForwardedPrefixExtractor forwardedPrefixExtractor;
ForwardedHeaderExtractingRequest(HttpServletRequest servletRequest, UrlPathHelper pathHelper) {
ForwardedHeaderExtractingRequest(HttpServletRequest servletRequest) {
super(servletRequest);
ServerHttpRequest request = new ServletServerHttpRequest(servletRequest);
@@ -251,7 +242,7 @@ public class ForwardedHeaderFilter extends OncePerRequestFilter {
String baseUrl = this.scheme + "://" + this.host + (port == -1 ? "" : ":" + port);
Supplier<HttpServletRequest> delegateRequest = () -> (HttpServletRequest) getRequest();
this.forwardedPrefixExtractor = new ForwardedPrefixExtractor(delegateRequest, pathHelper, baseUrl);
this.forwardedPrefixExtractor = new ForwardedPrefixExtractor(delegateRequest, baseUrl);
}
@@ -320,8 +311,6 @@ public class ForwardedHeaderFilter extends OncePerRequestFilter {
private final Supplier<HttpServletRequest> delegate;
private final UrlPathHelper pathHelper;
private final String baseUrl;
private String actualRequestUri;
@@ -340,14 +329,10 @@ public class ForwardedHeaderFilter extends OncePerRequestFilter {
* @param delegateRequest supplier for the current
* {@link HttpServletRequestWrapper#getRequest() delegate request} which
* may change during a forward (e.g. Tomcat.
* @param pathHelper the path helper instance
* @param baseUrl the host, scheme, and port based on forwarded headers
*/
public ForwardedPrefixExtractor(
Supplier<HttpServletRequest> delegateRequest, UrlPathHelper pathHelper, String baseUrl) {
public ForwardedPrefixExtractor(Supplier<HttpServletRequest> delegateRequest, String baseUrl) {
this.delegate = delegateRequest;
this.pathHelper = pathHelper;
this.baseUrl = baseUrl;
this.actualRequestUri = delegateRequest.get().getRequestURI();
@@ -384,7 +369,8 @@ public class ForwardedHeaderFilter extends OncePerRequestFilter {
@Nullable
private String initRequestUri() {
if (this.forwardedPrefix != null) {
return this.forwardedPrefix + this.pathHelper.getPathWithinApplication(this.delegate.get());
return this.forwardedPrefix +
UrlPathHelper.rawPathInstance.getPathWithinApplication(this.delegate.get());
}
return null;
}

View File

@@ -192,7 +192,13 @@ public class UriComponentsBuilder implements UriBuilder, Cloneable {
}
/**
* Create a builder that is initialized with the given {@code URI}.
* Create a builder that is initialized from the given {@code URI}.
* <p><strong>Note:</strong> the components in the resulting builder will be
* in fully encoded (raw) form and further changes must also supply values
* that are fully encoded, for example via methods in {@link UriUtils}.
* In addition please use {@link #build(boolean)} with a value of "true" to
* build the {@link UriComponents} instance in order to indicate that the
* components are encoded.
* @param uri the URI to initialize with
* @return the new {@code UriComponentsBuilder}
*/
@@ -439,11 +445,13 @@ public class UriComponentsBuilder implements UriBuilder, Cloneable {
}
/**
* Build a {@code UriComponents} instance from the various components
* contained in this builder.
* @param encoded whether all the components set in this builder are
* encoded ({@code true}) or not ({@code false})
* Variant of {@link #build()} to create a {@link UriComponents} instance
* when components are already fully encoded. This is useful for example if
* the builder was created via {@link UriComponentsBuilder#fromUri(URI)}.
* @param encoded whether the components in this builder are already encoded
* @return the URI components
* @throws IllegalArgumentException if any of the components contain illegal
* characters that should have been encoded.
*/
public UriComponents build(boolean encoded) {
return buildInternal(encoded ?

View File

@@ -85,6 +85,8 @@ public class UrlPathHelper {
private String defaultEncoding = WebUtils.DEFAULT_CHARACTER_ENCODING;
private boolean readOnly = false;
/**
* Whether URL lookups should always use the full path within the current
@@ -96,6 +98,7 @@ public class UrlPathHelper {
* <p>By default this is set to "false".
*/
public void setAlwaysUseFullPath(boolean alwaysUseFullPath) {
checkReadOnly();
this.alwaysUseFullPath = alwaysUseFullPath;
}
@@ -118,6 +121,7 @@ public class UrlPathHelper {
* @see java.net.URLDecoder#decode(String, String)
*/
public void setUrlDecode(boolean urlDecode) {
checkReadOnly();
this.urlDecode = urlDecode;
}
@@ -134,6 +138,7 @@ public class UrlPathHelper {
* <p>Default is "true".
*/
public void setRemoveSemicolonContent(boolean removeSemicolonContent) {
checkReadOnly();
this.removeSemicolonContent = removeSemicolonContent;
}
@@ -141,6 +146,7 @@ public class UrlPathHelper {
* Whether configured to remove ";" (semicolon) content from the request URI.
*/
public boolean shouldRemoveSemicolonContent() {
checkReadOnly();
return this.removeSemicolonContent;
}
@@ -158,6 +164,7 @@ public class UrlPathHelper {
* @see WebUtils#DEFAULT_CHARACTER_ENCODING
*/
public void setDefaultEncoding(String defaultEncoding) {
checkReadOnly();
this.defaultEncoding = defaultEncoding;
}
@@ -168,6 +175,17 @@ public class UrlPathHelper {
return this.defaultEncoding;
}
/**
* Switch to read-only mode where further configuration changes are not allowed.
*/
private void setReadOnly() {
this.readOnly = true;
}
private void checkReadOnly() {
Assert.isTrue(!this.readOnly, "This instance cannot be modified");
}
/**
* {@link #getLookupPathForRequest Resolve} the lookupPath and cache it in a
@@ -590,8 +608,7 @@ public class UrlPathHelper {
* @return the updated URI string
*/
public String removeSemicolonContent(String requestUri) {
return (this.removeSemicolonContent ?
removeSemicolonContentInternal(requestUri) : removeJsessionid(requestUri));
return (this.removeSemicolonContent ? removeSemicolonContentInternal(requestUri) : requestUri);
}
private String removeSemicolonContentInternal(String requestUri) {
@@ -605,16 +622,6 @@ public class UrlPathHelper {
return requestUri;
}
private String removeJsessionid(String requestUri) {
int startIndex = requestUri.toLowerCase().indexOf(";jsessionid=");
if (startIndex != -1) {
int endIndex = requestUri.indexOf(';', startIndex + 12);
String start = requestUri.substring(0, startIndex);
requestUri = (endIndex != -1) ? start + requestUri.substring(endIndex) : start;
}
return requestUri;
}
/**
* Decode the given URI path variables via {@link #decodeRequestString} unless
* {@link #setUrlDecode} is set to {@code true} in which case it is assumed
@@ -695,7 +702,7 @@ public class UrlPathHelper {
/**
* Shared, read-only instance of {@code UrlPathHelper}. Uses default settings:
* Shared, read-only instance with defaults. The following apply:
* <ul>
* <li>{@code alwaysUseFullPath=false}
* <li>{@code urlDecode=true}
@@ -703,27 +710,29 @@ public class UrlPathHelper {
* <li>{@code defaultEncoding=}{@link WebUtils#DEFAULT_CHARACTER_ENCODING}
* </ul>
*/
public static final UrlPathHelper defaultInstance = new UrlPathHelper() {
public static final UrlPathHelper defaultInstance = new UrlPathHelper();
@Override
public void setAlwaysUseFullPath(boolean alwaysUseFullPath) {
throw new UnsupportedOperationException();
}
static {
defaultInstance.setReadOnly();
}
@Override
public void setUrlDecode(boolean urlDecode) {
throw new UnsupportedOperationException();
}
@Override
public void setRemoveSemicolonContent(boolean removeSemicolonContent) {
throw new UnsupportedOperationException();
}
/**
* Shared, read-only instance for the full, encoded path. The following apply:
* <ul>
* <li>{@code alwaysUseFullPath=true}
* <li>{@code urlDecode=false}
* <li>{@code removeSemicolon=false}
* <li>{@code defaultEncoding=}{@link WebUtils#DEFAULT_CHARACTER_ENCODING}
* </ul>
*/
public static final UrlPathHelper rawPathInstance = new UrlPathHelper();
@Override
public void setDefaultEncoding(String defaultEncoding) {
throw new UnsupportedOperationException();
}
};
static {
rawPathInstance.setAlwaysUseFullPath(true);
rawPathInstance.setUrlDecode(false);
rawPathInstance.setRemoveSemicolonContent(false);
rawPathInstance.setReadOnly();
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -733,6 +733,9 @@ public abstract class WebUtils {
int index = pair.indexOf('=');
if (index != -1) {
String name = pair.substring(0, index);
if (name.equalsIgnoreCase("jsessionid")) {
continue;
}
String rawValue = pair.substring(index + 1);
for (String value : StringUtils.commaDelimitedListToStringArray(rawValue)) {
result.add(name, value);