Expose localAddress in WebFlux server
Closes gh-24174
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2018 the original author or authors.
|
||||
* Copyright 2002-2019 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.
|
||||
@@ -181,9 +181,6 @@ class DefaultServerHttpRequestBuilder implements ServerHttpRequest.Builder {
|
||||
|
||||
private final MultiValueMap<String, HttpCookie> cookies;
|
||||
|
||||
@Nullable
|
||||
private final InetSocketAddress remoteAddress;
|
||||
|
||||
@Nullable
|
||||
private final SslInfo sslInfo;
|
||||
|
||||
@@ -199,7 +196,6 @@ class DefaultServerHttpRequestBuilder implements ServerHttpRequest.Builder {
|
||||
super(uri, contextPath, headers);
|
||||
this.methodValue = methodValue;
|
||||
this.cookies = cookies;
|
||||
this.remoteAddress = originalRequest.getRemoteAddress();
|
||||
this.sslInfo = sslInfo != null ? sslInfo : originalRequest.getSslInfo();
|
||||
this.body = body;
|
||||
this.originalRequest = originalRequest;
|
||||
@@ -218,7 +214,13 @@ class DefaultServerHttpRequestBuilder implements ServerHttpRequest.Builder {
|
||||
@Nullable
|
||||
@Override
|
||||
public InetSocketAddress getRemoteAddress() {
|
||||
return this.remoteAddress;
|
||||
return this.originalRequest.getRemoteAddress();
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public InetSocketAddress getLocalAddress() {
|
||||
return this.originalRequest.getLocalAddress();
|
||||
}
|
||||
|
||||
@Nullable
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2018 the original author or authors.
|
||||
* Copyright 2002-2019 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.
|
||||
@@ -151,6 +151,11 @@ class ReactorServerHttpRequest extends AbstractServerHttpRequest {
|
||||
return this.request.remoteAddress();
|
||||
}
|
||||
|
||||
@Override
|
||||
public InetSocketAddress getLocalAddress() {
|
||||
return this.request.hostAddress();
|
||||
}
|
||||
|
||||
@Override
|
||||
@Nullable
|
||||
protected SslInfo initSslInfo() {
|
||||
|
||||
@@ -72,6 +72,15 @@ public interface ServerHttpRequest extends HttpRequest, ReactiveHttpInputMessage
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the local address the request was accepted on, if available.
|
||||
* 5.2.3
|
||||
*/
|
||||
@Nullable
|
||||
default InetSocketAddress getLocalAddress() {
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the SSL session information if the request has been transmitted
|
||||
* over a secure protocol including SSL certificates, if available.
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2018 the original author or authors.
|
||||
* Copyright 2002-2019 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.
|
||||
@@ -101,6 +101,11 @@ public class ServerHttpRequestDecorator implements ServerHttpRequest {
|
||||
return getDelegate().getRemoteAddress();
|
||||
}
|
||||
|
||||
@Override
|
||||
public InetSocketAddress getLocalAddress() {
|
||||
return getDelegate().getLocalAddress();
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public SslInfo getSslInfo() {
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2018 the original author or authors.
|
||||
* Copyright 2002-2019 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.
|
||||
@@ -178,6 +178,11 @@ class ServletServerHttpRequest extends AbstractServerHttpRequest {
|
||||
return new InetSocketAddress(this.request.getRemoteHost(), this.request.getRemotePort());
|
||||
}
|
||||
|
||||
@Override
|
||||
public InetSocketAddress getLocalAddress() {
|
||||
return new InetSocketAddress(this.request.getLocalAddr(), this.request.getLocalPort());
|
||||
}
|
||||
|
||||
@Override
|
||||
@Nullable
|
||||
protected SslInfo initSslInfo() {
|
||||
|
||||
@@ -102,6 +102,11 @@ class UndertowServerHttpRequest extends AbstractServerHttpRequest {
|
||||
return this.exchange.getSourceAddress();
|
||||
}
|
||||
|
||||
@Override
|
||||
public InetSocketAddress getLocalAddress() {
|
||||
return this.exchange.getDestinationAddress();
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
protected SslInfo initSslInfo() {
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2018 the original author or authors.
|
||||
* Copyright 2002-2019 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.
|
||||
@@ -52,7 +52,7 @@ import org.springframework.web.util.UriComponentsBuilder;
|
||||
* @author Rossen Stoyanchev
|
||||
* @since 5.0
|
||||
*/
|
||||
public class MockServerHttpRequest extends AbstractServerHttpRequest {
|
||||
public final class MockServerHttpRequest extends AbstractServerHttpRequest {
|
||||
|
||||
private final HttpMethod httpMethod;
|
||||
|
||||
@@ -61,6 +61,9 @@ public class MockServerHttpRequest extends AbstractServerHttpRequest {
|
||||
@Nullable
|
||||
private final InetSocketAddress remoteAddress;
|
||||
|
||||
@Nullable
|
||||
private final InetSocketAddress localAddress;
|
||||
|
||||
@Nullable
|
||||
private final SslInfo sslInfo;
|
||||
|
||||
@@ -69,13 +72,14 @@ public class MockServerHttpRequest extends AbstractServerHttpRequest {
|
||||
|
||||
private MockServerHttpRequest(HttpMethod httpMethod, URI uri, @Nullable String contextPath,
|
||||
HttpHeaders headers, MultiValueMap<String, HttpCookie> cookies,
|
||||
@Nullable InetSocketAddress remoteAddress, @Nullable SslInfo sslInfo,
|
||||
Publisher<? extends DataBuffer> body) {
|
||||
@Nullable InetSocketAddress remoteAddress, @Nullable InetSocketAddress localAddress,
|
||||
@Nullable SslInfo sslInfo, Publisher<? extends DataBuffer> body) {
|
||||
|
||||
super(uri, contextPath, headers);
|
||||
this.httpMethod = httpMethod;
|
||||
this.cookies = cookies;
|
||||
this.remoteAddress = remoteAddress;
|
||||
this.localAddress = localAddress;
|
||||
this.sslInfo = sslInfo;
|
||||
this.body = Flux.from(body);
|
||||
}
|
||||
@@ -97,6 +101,12 @@ public class MockServerHttpRequest extends AbstractServerHttpRequest {
|
||||
return this.remoteAddress;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public InetSocketAddress getLocalAddress() {
|
||||
return this.localAddress;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
protected SslInfo initSslInfo() {
|
||||
@@ -254,6 +264,12 @@ public class MockServerHttpRequest extends AbstractServerHttpRequest {
|
||||
*/
|
||||
B remoteAddress(InetSocketAddress remoteAddress);
|
||||
|
||||
/**
|
||||
* Set the local address to return.
|
||||
* @since 5.2.3
|
||||
*/
|
||||
B localAddress(InetSocketAddress localAddress);
|
||||
|
||||
/**
|
||||
* Set SSL session information and certificates.
|
||||
*/
|
||||
@@ -408,6 +424,9 @@ public class MockServerHttpRequest extends AbstractServerHttpRequest {
|
||||
@Nullable
|
||||
private InetSocketAddress remoteAddress;
|
||||
|
||||
@Nullable
|
||||
private InetSocketAddress localAddress;
|
||||
|
||||
@Nullable
|
||||
private SslInfo sslInfo;
|
||||
|
||||
@@ -441,6 +460,12 @@ public class MockServerHttpRequest extends AbstractServerHttpRequest {
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public BodyBuilder localAddress(InetSocketAddress localAddress) {
|
||||
this.localAddress = localAddress;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sslInfo(SslInfo sslInfo) {
|
||||
this.sslInfo = sslInfo;
|
||||
@@ -545,7 +570,7 @@ public class MockServerHttpRequest extends AbstractServerHttpRequest {
|
||||
public MockServerHttpRequest body(Publisher<? extends DataBuffer> body) {
|
||||
applyCookiesIfNecessary();
|
||||
return new MockServerHttpRequest(this.method, getUrlToUse(), this.contextPath,
|
||||
this.headers, this.cookies, this.remoteAddress, this.sslInfo, body);
|
||||
this.headers, this.cookies, this.remoteAddress, this.localAddress, this.sslInfo, body);
|
||||
}
|
||||
|
||||
private void applyCookiesIfNecessary() {
|
||||
|
||||
Reference in New Issue
Block a user