Support for SslInfo in ServerHttpRequest#mutate

Issue: SPR-16830
This commit is contained in:
Rossen Stoyanchev
2018-05-17 17:27:52 -04:00
parent ade2eab169
commit fbd12e9d16
3 changed files with 113 additions and 31 deletions

View File

@@ -57,6 +57,9 @@ class DefaultServerHttpRequestBuilder implements ServerHttpRequest.Builder {
@Nullable
private String contextPath;
@Nullable
private SslInfo sslInfo;
private Flux<DataBuffer> body;
private final ServerHttpRequest originalRequest;
@@ -97,6 +100,7 @@ class DefaultServerHttpRequestBuilder implements ServerHttpRequest.Builder {
@Override
public ServerHttpRequest.Builder path(String path) {
Assert.isTrue(path.startsWith("/"), "The path does not have a leading slash.");
this.uriPath = path;
return this;
}
@@ -120,10 +124,16 @@ class DefaultServerHttpRequestBuilder implements ServerHttpRequest.Builder {
return this;
}
@Override
public ServerHttpRequest.Builder sslInfo(SslInfo sslInfo) {
this.sslInfo = sslInfo;
return this;
}
@Override
public ServerHttpRequest build() {
return new DefaultServerHttpRequest(getUriToUse(), this.contextPath, this.httpHeaders,
this.httpMethodValue, this.cookies, this.body, this.originalRequest);
return new MutatedServerHttpRequest(getUriToUse(), this.contextPath, this.httpHeaders,
this.httpMethodValue, this.cookies, this.sslInfo, this.body, this.originalRequest);
}
private URI getUriToUse() {
@@ -165,7 +175,7 @@ class DefaultServerHttpRequestBuilder implements ServerHttpRequest.Builder {
}
private static class DefaultServerHttpRequest extends AbstractServerHttpRequest {
private static class MutatedServerHttpRequest extends AbstractServerHttpRequest {
private final String methodValue;
@@ -181,15 +191,16 @@ class DefaultServerHttpRequestBuilder implements ServerHttpRequest.Builder {
private final ServerHttpRequest originalRequest;
public DefaultServerHttpRequest(URI uri, @Nullable String contextPath,
public MutatedServerHttpRequest(URI uri, @Nullable String contextPath,
HttpHeaders headers, String methodValue, MultiValueMap<String, HttpCookie> cookies,
Flux<DataBuffer> body, ServerHttpRequest originalRequest) {
@Nullable SslInfo sslInfo, Flux<DataBuffer> body, ServerHttpRequest originalRequest) {
super(uri, contextPath, headers);
this.methodValue = methodValue;
this.cookies = cookies;
this.remoteAddress = originalRequest.getRemoteAddress();
this.sslInfo = originalRequest.getSslInfo();
this.sslInfo = sslInfo != null ? sslInfo : originalRequest.getSslInfo();
this.body = body;
this.originalRequest = originalRequest;
}

View File

@@ -95,18 +95,36 @@ public interface ServerHttpRequest extends HttpRequest, ReactiveHttpInputMessage
Builder method(HttpMethod httpMethod);
/**
* Set the URI to return.
* Set the URI to use with the following conditions:
* <ul>
* <li>If {@link #path(String) path} is also set, it overrides the path
* of the URI provided here.
* <li>If {@link #contextPath(String) contextPath} is also set, or
* already present, it must match the start of the path of the URI
* provided here.
* </ul>
*/
Builder uri(URI uri);
/**
* Set the path to use instead of the {@code "rawPath"} of
* {@link ServerHttpRequest#getURI()}.
* Set the path to use instead of the {@code "rawPath"} of the URI of
* the request with the following conditions:
* <ul>
* <li>If {@link #uri(URI) uri} is also set, the path given here
* overrides the path of the given URI.
* <li>If {@link #contextPath(String) contextPath} is also set, or
* already present, it must match the start of the path given here.
* <li>The given value must begin with a slash.
* </ul>
*/
Builder path(String path);
/**
* Set the contextPath to use.
* <p>The given value must be a valid {@link RequestPath#contextPath()
* contextPath} and it must match the start of the path of the URI of
* the request. That means changing the contextPath, implies also
* changing the path via {@link #path(String)}.
*/
Builder contextPath(String contextPath);
@@ -116,16 +134,22 @@ public interface ServerHttpRequest extends HttpRequest, ReactiveHttpInputMessage
Builder header(String key, String value);
/**
* Manipulate this request's headers with the given consumer. The
* headers provided to the consumer are "live", so that the consumer can be used to
* {@linkplain HttpHeaders#set(String, String) overwrite} existing header values,
* {@linkplain HttpHeaders#remove(Object) remove} values, or use any of the other
* {@link HttpHeaders} methods.
* @param headersConsumer a function that consumes the {@code HttpHeaders}
* @return this builder
* Manipulate request headers. The provided {@code HttpHeaders} contains
* current request headers, so that the {@code Consumer} can
* {@linkplain HttpHeaders#set(String, String) overwrite} or
* {@linkplain HttpHeaders#remove(Object) remove} existing values, or
* use any other {@link HttpHeaders} methods.
*/
Builder headers(Consumer<HttpHeaders> headersConsumer);
/**
* Set the SSL session information. This may be useful in environments
* where TLS termination is done at the router, but SSL information is
* made available in some other way such as through a header.
* @since 5.0.7
*/
Builder sslInfo(SslInfo sslInfo);
/**
* Build a {@link ServerHttpRequest} decorator with the mutated properties.
*/