Skip to content

Commit 203370a

Browse files
committed
Handle absolute URI in Reactor request.uri()
The request URI returned from HttpServerRequest.uri() typically contains contains an absolute path but could also contain an absolute URI. This commit adds handling for the latter, effectively taking only the absolute path portion. Issue: SPR-16243
1 parent b9a1168 commit 203370a

File tree

1 file changed

+24
-1
lines changed

1 file changed

+24
-1
lines changed

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

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import org.springframework.util.Assert;
3737
import org.springframework.util.LinkedMultiValueMap;
3838
import org.springframework.util.MultiValueMap;
39+
import org.springframework.util.StringUtils;
3940

4041
/**
4142
* Adapt {@link ServerHttpRequest} to the Reactor {@link HttpServerRequest}.
@@ -62,7 +63,7 @@ public ReactorServerHttpRequest(HttpServerRequest request, NettyDataBufferFactor
6263

6364
private static URI initUri(HttpServerRequest request) throws URISyntaxException {
6465
Assert.notNull(request, "'request' must not be null");
65-
return new URI(resolveBaseUrl(request).toString() + request.uri());
66+
return new URI(resolveBaseUrl(request).toString() + resolveRequestUri(request));
6667
}
6768

6869
private static URI resolveBaseUrl(HttpServerRequest request) throws URISyntaxException {
@@ -102,6 +103,28 @@ private static String getScheme(HttpServerRequest request) {
102103
return ssl ? "https" : "http";
103104
}
104105

106+
private static String resolveRequestUri(HttpServerRequest request) {
107+
String uri = request.uri();
108+
for (int i = 0; i < uri.length(); i++) {
109+
char c = uri.charAt(i);
110+
if (c == '/' || c == '?' || c == '#') {
111+
break;
112+
}
113+
if (c == ':' && (i + 2 < uri.length())) {
114+
if (uri.charAt(i + 1) == '/' && uri.charAt(i + 2) == '/') {
115+
for (int j = i + 3; j < uri.length(); j++) {
116+
c = uri.charAt(j);
117+
if (c == '/' || c == '?' || c == '#') {
118+
return uri.substring(j);
119+
}
120+
}
121+
return "";
122+
}
123+
}
124+
}
125+
return uri;
126+
}
127+
105128
private static HttpHeaders initHeaders(HttpServerRequest channel) {
106129
HttpHeaders headers = new HttpHeaders();
107130
for (String name : channel.requestHeaders().names()) {

0 commit comments

Comments
 (0)