Skip to content

Commit a78385f

Browse files
chemicLrstoyanchev
authored andcommitted
Handle X-Forwarded-Prefix parsed by Reactor Netty
See gh-33638
1 parent f9f025d commit a78385f

File tree

4 files changed

+36
-3
lines changed

4 files changed

+36
-3
lines changed

framework-platform/framework-platform.gradle

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ dependencies {
1111
api(platform("io.micrometer:micrometer-bom:1.14.0-M3"))
1212
api(platform("io.netty:netty-bom:4.1.113.Final"))
1313
api(platform("io.netty:netty5-bom:5.0.0.Alpha5"))
14-
api(platform("io.projectreactor:reactor-bom:2024.0.0-M6"))
14+
api(platform("io.projectreactor:reactor-bom:2024.0.0-SNAPSHOT"))
1515
api(platform("io.rsocket:rsocket-bom:1.1.3"))
1616
api(platform("org.apache.groovy:groovy-bom:4.0.23"))
1717
api(platform("org.apache.logging.log4j:log4j-bom:2.21.1"))
@@ -50,6 +50,7 @@ dependencies {
5050
api("io.micrometer:context-propagation:1.1.1")
5151
api("io.mockk:mockk:1.13.4")
5252
api("io.projectreactor.netty:reactor-netty5-http:2.0.0-M3")
53+
api("io.projectreactor.netty:reactor-netty-http")
5354
api("io.projectreactor.tools:blockhound:1.0.8.RELEASE")
5455
api("io.r2dbc:r2dbc-h2:1.0.0.RELEASE")
5556
api("io.r2dbc:r2dbc-spi-test:1.0.0.RELEASE")

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,8 @@ class ReactorServerHttpRequest extends AbstractServerHttpRequest {
6565
public ReactorServerHttpRequest(HttpServerRequest request, NettyDataBufferFactory bufferFactory)
6666
throws URISyntaxException {
6767

68-
super(HttpMethod.valueOf(request.method().name()), ReactorUriHelper.createUri(request), "",
68+
super(HttpMethod.valueOf(request.method().name()),
69+
ReactorUriHelper.createUri(request), request.forwardedPrefix(),
6970
new Netty4HeadersAdapter(request.requestHeaders()));
7071
Assert.notNull(bufferFactory, "DataBufferFactory must not be null");
7172
this.request = request;

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,12 @@ public static URI createUri(HttpServerRequest request) throws URISyntaxException
4848
builder.append(port);
4949
}
5050

51-
appendRequestUri(request, builder);
51+
String prefix = request.forwardedPrefix();
52+
if (prefix != null && !prefix.isEmpty()) {
53+
builder.append(prefix);
54+
}
5255

56+
appendRequestUri(request, builder);
5357
return new URI(builder.toString());
5458
}
5559

spring-web/src/test/java/org/springframework/http/server/reactive/ReactorUriHelperTests.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
import java.net.URISyntaxException;
2121

2222
import org.junit.jupiter.api.Test;
23+
import org.junit.jupiter.params.ParameterizedTest;
24+
import org.junit.jupiter.params.provider.CsvSource;
2325
import reactor.netty.http.server.HttpServerRequest;
2426

2527
import static org.assertj.core.api.Assertions.assertThat;
@@ -49,4 +51,29 @@ void hostnameWithZoneId() throws URISyntaxException {
4951

5052
}
5153

54+
@ParameterizedTest(name = "{displayName}({arguments})")
55+
@CsvSource(delimiter='|', value = {
56+
"/prefix | /prefix/",
57+
"/prefix1/prefix2 | /prefix1/prefix2/",
58+
" | /",
59+
"'' | /",
60+
})
61+
void forwardedPrefix(String prefixHeader, String expectedPath) throws URISyntaxException {
62+
HttpServerRequest nettyRequest = mock();
63+
64+
given(nettyRequest.scheme()).willReturn("https");
65+
given(nettyRequest.hostName()).willReturn("localhost");
66+
given(nettyRequest.hostPort()).willReturn(443);
67+
given(nettyRequest.uri()).willReturn("/");
68+
given(nettyRequest.forwardedPrefix()).willReturn(prefixHeader);
69+
70+
URI uri = ReactorUriHelper.createUri(nettyRequest);
71+
assertThat(uri).hasScheme("https")
72+
.hasHost("localhost")
73+
.hasPort(-1)
74+
.hasPath(expectedPath)
75+
.hasToString("https://localhost" + expectedPath);
76+
}
77+
78+
5279
}

0 commit comments

Comments
 (0)