Polishing contribution

Closes gh-30137
This commit is contained in:
rstoyanchev
2023-09-20 18:43:33 +01:00
parent a1c4fb3840
commit cc296c5033
2 changed files with 25 additions and 12 deletions

View File

@@ -18,6 +18,7 @@ package org.springframework.web.server.adapter;
import java.net.InetSocketAddress;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Collections;
import java.util.Locale;
import java.util.Set;
@@ -30,6 +31,7 @@ import org.springframework.lang.Nullable;
import org.springframework.util.LinkedCaseInsensitiveMap;
import org.springframework.util.StringUtils;
import org.springframework.web.util.ForwardedHeaderUtils;
import org.springframework.web.util.UriComponents;
/**
* Extract values from "Forwarded" and "X-Forwarded-*" headers to override
@@ -102,7 +104,7 @@ public class ForwardedHeaderTransformer implements Function<ServerHttpRequest, S
if (!this.removeOnly) {
URI originalUri = request.getURI();
HttpHeaders headers = request.getHeaders();
URI uri = ForwardedHeaderUtils.adaptFromForwardedHeaders(originalUri, headers).build(true).toUri();
URI uri = adaptFromForwardedHeaders(originalUri, headers);
builder.uri(uri);
String prefix = getForwardedPrefix(request);
if (prefix != null) {
@@ -121,6 +123,17 @@ public class ForwardedHeaderTransformer implements Function<ServerHttpRequest, S
return request;
}
private static URI adaptFromForwardedHeaders(URI uri, HttpHeaders headers) {
// GH-30137: assume URI is encoded, but avoid build(true) for more lenient handling
UriComponents components = ForwardedHeaderUtils.adaptFromForwardedHeaders(uri, headers).build();
try {
return new URI(components.toUriString());
}
catch (URISyntaxException ex) {
throw new IllegalStateException("Could not create URI object: " + ex.getMessage(), ex);
}
}
/**
* Whether the request has any Forwarded headers.
* @param request the request