Browse Source

Prefer local hostAddress in ReactorServerHttpRequest

Closes gh-28601
pull/30101/head
rstoyanchev 3 years ago
parent
commit
a2b7a907ec
  1. 16
      spring-web/src/main/java/org/springframework/http/server/reactive/ReactorServerHttpRequest.java

16
spring-web/src/main/java/org/springframework/http/server/reactive/ReactorServerHttpRequest.java

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2022 the original author or authors. * Copyright 2002-2023 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -82,6 +82,12 @@ class ReactorServerHttpRequest extends AbstractServerHttpRequest {
private static URI resolveBaseUrl(HttpServerRequest request) throws URISyntaxException { private static URI resolveBaseUrl(HttpServerRequest request) throws URISyntaxException {
String scheme = getScheme(request); String scheme = getScheme(request);
InetSocketAddress hostAddress = request.hostAddress();
if (hostAddress != null) {
return new URI(scheme, null, hostAddress.getHostString(), hostAddress.getPort(), null, null, null);
}
String header = request.requestHeaders().get(HttpHeaderNames.HOST); String header = request.requestHeaders().get(HttpHeaderNames.HOST);
if (header != null) { if (header != null) {
final int portIndex; final int portIndex;
@ -104,12 +110,8 @@ class ReactorServerHttpRequest extends AbstractServerHttpRequest {
return new URI(scheme, header, null, null); return new URI(scheme, header, null, null);
} }
} }
else {
InetSocketAddress localAddress = request.hostAddress(); throw new IllegalStateException("Neither local hostAddress nor HOST header available");
Assert.state(localAddress != null, "No host address available");
return new URI(scheme, null, localAddress.getHostString(),
localAddress.getPort(), null, null, null);
}
} }
private static String getScheme(HttpServerRequest request) { private static String getScheme(HttpServerRequest request) {

Loading…
Cancel
Save