From a84a41fa436f88bdce6ce3d36187aa8f0d05e298 Mon Sep 17 00:00:00 2001 From: rstoyanchev Date: Tue, 8 Oct 2024 07:05:16 +0100 Subject: [PATCH] Polishing contribution Closes gh-33638 --- framework-platform/framework-platform.gradle | 1 - .../http/server/reactive/ReactorUriHelper.java | 8 ++++++-- .../http/server/reactive/ReactorUriHelperTests.java | 7 ++++--- 3 files changed, 10 insertions(+), 6 deletions(-) diff --git a/framework-platform/framework-platform.gradle b/framework-platform/framework-platform.gradle index c4b3f662f3..61648fa071 100644 --- a/framework-platform/framework-platform.gradle +++ b/framework-platform/framework-platform.gradle @@ -50,7 +50,6 @@ dependencies { api("io.micrometer:context-propagation:1.1.1") api("io.mockk:mockk:1.13.4") api("io.projectreactor.netty:reactor-netty5-http:2.0.0-M3") - api("io.projectreactor.netty:reactor-netty-http") api("io.projectreactor.tools:blockhound:1.0.8.RELEASE") api("io.r2dbc:r2dbc-h2:1.0.0.RELEASE") api("io.r2dbc:r2dbc-spi-test:1.0.0.RELEASE") diff --git a/spring-web/src/main/java/org/springframework/http/server/reactive/ReactorUriHelper.java b/spring-web/src/main/java/org/springframework/http/server/reactive/ReactorUriHelper.java index 9f31f3db59..b2544cc848 100644 --- a/spring-web/src/main/java/org/springframework/http/server/reactive/ReactorUriHelper.java +++ b/spring-web/src/main/java/org/springframework/http/server/reactive/ReactorUriHelper.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2023 the original author or authors. + * Copyright 2002-2024 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. @@ -24,7 +24,7 @@ import reactor.netty.http.server.HttpServerRequest; import org.springframework.util.Assert; /** - * Helper class for creating a {@link URI} from a reactor {@link HttpServerRequest}. + * Helper class to create {@link URI} from a Reactor Netty request. * * @author Arjen Poutsma * @since 6.0.8 @@ -48,12 +48,16 @@ abstract class ReactorUriHelper { builder.append(port); } + // Reactor Netty has config whether to extract and apply forwarded headers. + // We apply the prefix manually as it affects the contextPath too. + String prefix = request.forwardedPrefix(); if (prefix != null && !prefix.isEmpty()) { builder.append(prefix); } appendRequestUri(request, builder); + return new URI(builder.toString()); } diff --git a/spring-web/src/test/java/org/springframework/http/server/reactive/ReactorUriHelperTests.java b/spring-web/src/test/java/org/springframework/http/server/reactive/ReactorUriHelperTests.java index b275c96ace..f34ab6b1f4 100644 --- a/spring-web/src/test/java/org/springframework/http/server/reactive/ReactorUriHelperTests.java +++ b/spring-web/src/test/java/org/springframework/http/server/reactive/ReactorUriHelperTests.java @@ -29,6 +29,8 @@ import static org.mockito.BDDMockito.given; import static org.mockito.Mockito.mock; /** + * Unit tests for {@link ReactorUriHelper}. + * * @author Arjen Poutsma */ class ReactorUriHelperTests { @@ -58,14 +60,14 @@ class ReactorUriHelperTests { " | /", "'' | /", }) - void forwardedPrefix(String prefixHeader, String expectedPath) throws URISyntaxException { + void forwardedPrefix(String forwardedPrefixHeader, String expectedPath) throws URISyntaxException { HttpServerRequest nettyRequest = mock(); given(nettyRequest.scheme()).willReturn("https"); given(nettyRequest.hostName()).willReturn("localhost"); given(nettyRequest.hostPort()).willReturn(443); given(nettyRequest.uri()).willReturn("/"); - given(nettyRequest.forwardedPrefix()).willReturn(prefixHeader); + given(nettyRequest.forwardedPrefix()).willReturn(forwardedPrefixHeader); URI uri = ReactorUriHelper.createUri(nettyRequest); assertThat(uri).hasScheme("https") @@ -75,5 +77,4 @@ class ReactorUriHelperTests { .hasToString("https://localhost" + expectedPath); } - }