Skip to content

Commit ec6475a

Browse files
committed
Expose scheme in ReactorServerHttpRequest URI
This commit determines fixes ReactorServerHttpRequest.getUri() so that it includes a URL scheme. Issue: SPR-15931
1 parent 15ab0ad commit ec6475a

File tree

3 files changed

+18
-6
lines changed

3 files changed

+18
-6
lines changed

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

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,10 @@
2020
import java.net.URI;
2121
import java.net.URISyntaxException;
2222

23+
import io.netty.channel.ChannelPipeline;
2324
import io.netty.handler.codec.http.HttpHeaderNames;
2425
import io.netty.handler.codec.http.cookie.Cookie;
26+
import io.netty.handler.ssl.SslHandler;
2527
import reactor.core.publisher.Flux;
2628
import reactor.ipc.netty.http.server.HttpServerRequest;
2729

@@ -62,6 +64,7 @@ private static URI initUri(HttpServerRequest request) throws URISyntaxException
6264
}
6365

6466
private static URI resolveBaseUrl(HttpServerRequest request) throws URISyntaxException {
67+
String scheme = getScheme(request);
6568
String header = request.requestHeaders().get(HttpHeaderNames.HOST);
6669
if (header != null) {
6770
final int portIndex;
@@ -73,24 +76,30 @@ private static URI resolveBaseUrl(HttpServerRequest request) throws URISyntaxExc
7376
}
7477
if (portIndex != -1) {
7578
try {
76-
return new URI(null, null, header.substring(0, portIndex),
79+
return new URI(scheme, null, header.substring(0, portIndex),
7780
Integer.parseInt(header.substring(portIndex + 1)), null, null, null);
7881
}
7982
catch (NumberFormatException ex) {
8083
throw new URISyntaxException(header, "Unable to parse port", portIndex);
8184
}
8285
}
8386
else {
84-
return new URI(null, header, null, null);
87+
return new URI(scheme, header, null, null);
8588
}
8689
}
8790
else {
8891
InetSocketAddress localAddress = (InetSocketAddress) request.context().channel().localAddress();
89-
return new URI(null, null, localAddress.getHostString(),
92+
return new URI(scheme, null, localAddress.getHostString(),
9093
localAddress.getPort(), null, null, null);
9194
}
9295
}
9396

97+
private static String getScheme(HttpServerRequest request) {
98+
ChannelPipeline pipeline = request.context().channel().pipeline();
99+
boolean ssl = pipeline.get(SslHandler.class) != null;
100+
return ssl ? "https" : "http";
101+
}
102+
94103
private static HttpHeaders initHeaders(HttpServerRequest channel) {
95104
HttpHeaders headers = new HttpHeaders();
96105
for (String name : channel.requestHeaders().names()) {

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,8 @@ private static URI initUri(HttpServerRequest<ByteBuf> request, InetSocketAddress
7474
}
7575

7676
private static URI createUrl(InetSocketAddress address, String requestUri) throws URISyntaxException {
77-
URI baseUrl = new URI(null, null, address.getHostString(), address.getPort(), null, null, null);
77+
// TODO: determine scheme
78+
URI baseUrl = new URI("http", null, address.getHostString(), address.getPort(), null, null, null);
7879
return new URI(baseUrl.toString() + requestUri);
7980
}
8081

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2016 the original author or authors.
2+
* Copyright 2002-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -18,7 +18,6 @@
1818

1919
import java.net.URI;
2020

21-
import static org.junit.Assert.*;
2221
import org.junit.Test;
2322
import reactor.core.publisher.Mono;
2423

@@ -27,6 +26,8 @@
2726
import org.springframework.http.ResponseEntity;
2827
import org.springframework.web.client.RestTemplate;
2928

29+
import static org.junit.Assert.*;
30+
3031
public class ServerHttpRequestIntegrationTests extends AbstractHttpHandlerIntegrationTests {
3132

3233
@Override
@@ -48,6 +49,7 @@ public static class CheckRequestHandler implements HttpHandler {
4849
@Override
4950
public Mono<Void> handle(ServerHttpRequest request, ServerHttpResponse response) {
5051
URI uri = request.getURI();
52+
assertEquals("http", uri.getScheme());
5153
assertNotNull(uri.getHost());
5254
assertNotEquals(-1, uri.getPort());
5355
assertNotNull(request.getRemoteAddress());

0 commit comments

Comments
 (0)