Browse Source

Expose scheme in ReactorServerHttpRequest URI

This commit determines fixes ReactorServerHttpRequest.getUri() so that
it includes a URL scheme.

Issue: SPR-15931
pull/1519/head
Arjen Poutsma 7 years ago
parent
commit
ec6475a24c
  1. 15
      spring-web/src/main/java/org/springframework/http/server/reactive/ReactorServerHttpRequest.java
  2. 3
      spring-web/src/test/java/org/springframework/http/server/reactive/RxNettyServerHttpRequest.java
  3. 6
      spring-web/src/test/java/org/springframework/http/server/reactive/ServerHttpRequestIntegrationTests.java

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

@ -20,8 +20,10 @@ import java.net.InetSocketAddress; @@ -20,8 +20,10 @@ import java.net.InetSocketAddress;
import java.net.URI;
import java.net.URISyntaxException;
import io.netty.channel.ChannelPipeline;
import io.netty.handler.codec.http.HttpHeaderNames;
import io.netty.handler.codec.http.cookie.Cookie;
import io.netty.handler.ssl.SslHandler;
import reactor.core.publisher.Flux;
import reactor.ipc.netty.http.server.HttpServerRequest;
@ -62,6 +64,7 @@ public class ReactorServerHttpRequest extends AbstractServerHttpRequest { @@ -62,6 +64,7 @@ public class ReactorServerHttpRequest extends AbstractServerHttpRequest {
}
private static URI resolveBaseUrl(HttpServerRequest request) throws URISyntaxException {
String scheme = getScheme(request);
String header = request.requestHeaders().get(HttpHeaderNames.HOST);
if (header != null) {
final int portIndex;
@ -73,7 +76,7 @@ public class ReactorServerHttpRequest extends AbstractServerHttpRequest { @@ -73,7 +76,7 @@ public class ReactorServerHttpRequest extends AbstractServerHttpRequest {
}
if (portIndex != -1) {
try {
return new URI(null, null, header.substring(0, portIndex),
return new URI(scheme, null, header.substring(0, portIndex),
Integer.parseInt(header.substring(portIndex + 1)), null, null, null);
}
catch (NumberFormatException ex) {
@ -81,16 +84,22 @@ public class ReactorServerHttpRequest extends AbstractServerHttpRequest { @@ -81,16 +84,22 @@ public class ReactorServerHttpRequest extends AbstractServerHttpRequest {
}
}
else {
return new URI(null, header, null, null);
return new URI(scheme, header, null, null);
}
}
else {
InetSocketAddress localAddress = (InetSocketAddress) request.context().channel().localAddress();
return new URI(null, null, localAddress.getHostString(),
return new URI(scheme, null, localAddress.getHostString(),
localAddress.getPort(), null, null, null);
}
}
private static String getScheme(HttpServerRequest request) {
ChannelPipeline pipeline = request.context().channel().pipeline();
boolean ssl = pipeline.get(SslHandler.class) != null;
return ssl ? "https" : "http";
}
private static HttpHeaders initHeaders(HttpServerRequest channel) {
HttpHeaders headers = new HttpHeaders();
for (String name : channel.requestHeaders().names()) {

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

@ -74,7 +74,8 @@ public class RxNettyServerHttpRequest extends AbstractServerHttpRequest { @@ -74,7 +74,8 @@ public class RxNettyServerHttpRequest extends AbstractServerHttpRequest {
}
private static URI createUrl(InetSocketAddress address, String requestUri) throws URISyntaxException {
URI baseUrl = new URI(null, null, address.getHostString(), address.getPort(), null, null, null);
// TODO: determine scheme
URI baseUrl = new URI("http", null, address.getHostString(), address.getPort(), null, null, null);
return new URI(baseUrl.toString() + requestUri);
}

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

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2016 the original author or authors.
* Copyright 2002-2017 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.
@ -18,7 +18,6 @@ package org.springframework.http.server.reactive; @@ -18,7 +18,6 @@ package org.springframework.http.server.reactive;
import java.net.URI;
import static org.junit.Assert.*;
import org.junit.Test;
import reactor.core.publisher.Mono;
@ -27,6 +26,8 @@ import org.springframework.http.RequestEntity; @@ -27,6 +26,8 @@ import org.springframework.http.RequestEntity;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;
import static org.junit.Assert.*;
public class ServerHttpRequestIntegrationTests extends AbstractHttpHandlerIntegrationTests {
@Override
@ -48,6 +49,7 @@ public class ServerHttpRequestIntegrationTests extends AbstractHttpHandlerIntegr @@ -48,6 +49,7 @@ public class ServerHttpRequestIntegrationTests extends AbstractHttpHandlerIntegr
@Override
public Mono<Void> handle(ServerHttpRequest request, ServerHttpResponse response) {
URI uri = request.getURI();
assertEquals("http", uri.getScheme());
assertNotNull(uri.getHost());
assertNotEquals(-1, uri.getPort());
assertNotNull(request.getRemoteAddress());

Loading…
Cancel
Save