Expose scheme in ReactorServerHttpRequest URI
This commit determines fixes ReactorServerHttpRequest.getUri() so that it includes a URL scheme. Issue: SPR-15931
This commit is contained in:
		
							parent
							
								
									15ab0ad6e2
								
							
						
					
					
						commit
						ec6475a24c
					
				|  | @ -20,8 +20,10 @@ import java.net.InetSocketAddress; | ||||||
| import java.net.URI; | import java.net.URI; | ||||||
| import java.net.URISyntaxException; | import java.net.URISyntaxException; | ||||||
| 
 | 
 | ||||||
|  | import io.netty.channel.ChannelPipeline; | ||||||
| import io.netty.handler.codec.http.HttpHeaderNames; | import io.netty.handler.codec.http.HttpHeaderNames; | ||||||
| import io.netty.handler.codec.http.cookie.Cookie; | import io.netty.handler.codec.http.cookie.Cookie; | ||||||
|  | import io.netty.handler.ssl.SslHandler; | ||||||
| import reactor.core.publisher.Flux; | import reactor.core.publisher.Flux; | ||||||
| import reactor.ipc.netty.http.server.HttpServerRequest; | import reactor.ipc.netty.http.server.HttpServerRequest; | ||||||
| 
 | 
 | ||||||
|  | @ -62,6 +64,7 @@ public class ReactorServerHttpRequest extends AbstractServerHttpRequest { | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	private static URI resolveBaseUrl(HttpServerRequest request) throws URISyntaxException { | 	private static URI resolveBaseUrl(HttpServerRequest request) throws URISyntaxException { | ||||||
|  | 		String scheme = getScheme(request); | ||||||
| 		String header = request.requestHeaders().get(HttpHeaderNames.HOST); | 		String header = request.requestHeaders().get(HttpHeaderNames.HOST); | ||||||
| 		if (header != null) { | 		if (header != null) { | ||||||
| 			final int portIndex; | 			final int portIndex; | ||||||
|  | @ -73,7 +76,7 @@ public class ReactorServerHttpRequest extends AbstractServerHttpRequest { | ||||||
| 			} | 			} | ||||||
| 			if (portIndex != -1) { | 			if (portIndex != -1) { | ||||||
| 				try { | 				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); | 							Integer.parseInt(header.substring(portIndex + 1)), null, null, null); | ||||||
| 				} | 				} | ||||||
| 				catch (NumberFormatException ex) { | 				catch (NumberFormatException ex) { | ||||||
|  | @ -81,16 +84,22 @@ public class ReactorServerHttpRequest extends AbstractServerHttpRequest { | ||||||
| 				} | 				} | ||||||
| 			} | 			} | ||||||
| 			else { | 			else { | ||||||
| 				return new URI(null, header, null, null); | 				return new URI(scheme, header, null, null); | ||||||
| 			} | 			} | ||||||
| 		} | 		} | ||||||
| 		else { | 		else { | ||||||
| 			InetSocketAddress localAddress = (InetSocketAddress) request.context().channel().localAddress(); | 			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); | 					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) { | 	private static HttpHeaders initHeaders(HttpServerRequest channel) { | ||||||
| 		HttpHeaders headers = new HttpHeaders(); | 		HttpHeaders headers = new HttpHeaders(); | ||||||
| 		for (String name : channel.requestHeaders().names()) { | 		for (String name : channel.requestHeaders().names()) { | ||||||
|  |  | ||||||
|  | @ -74,7 +74,8 @@ public class RxNettyServerHttpRequest extends AbstractServerHttpRequest { | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	private static URI createUrl(InetSocketAddress address, String requestUri) throws URISyntaxException { | 	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); | 		return new URI(baseUrl.toString() + requestUri); | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
|  | @ -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"); |  * Licensed under the Apache License, Version 2.0 (the "License"); | ||||||
|  * you may not use this file except in compliance with the License. |  * you may not use this file except in compliance with the License. | ||||||
|  | @ -18,7 +18,6 @@ package org.springframework.http.server.reactive; | ||||||
| 
 | 
 | ||||||
| import java.net.URI; | import java.net.URI; | ||||||
| 
 | 
 | ||||||
| import static org.junit.Assert.*; |  | ||||||
| import org.junit.Test; | import org.junit.Test; | ||||||
| import reactor.core.publisher.Mono; | import reactor.core.publisher.Mono; | ||||||
| 
 | 
 | ||||||
|  | @ -27,6 +26,8 @@ import org.springframework.http.RequestEntity; | ||||||
| import org.springframework.http.ResponseEntity; | import org.springframework.http.ResponseEntity; | ||||||
| import org.springframework.web.client.RestTemplate; | import org.springframework.web.client.RestTemplate; | ||||||
| 
 | 
 | ||||||
|  | import static org.junit.Assert.*; | ||||||
|  | 
 | ||||||
| public class ServerHttpRequestIntegrationTests extends AbstractHttpHandlerIntegrationTests { | public class ServerHttpRequestIntegrationTests extends AbstractHttpHandlerIntegrationTests { | ||||||
| 
 | 
 | ||||||
| 	@Override | 	@Override | ||||||
|  | @ -48,6 +49,7 @@ public class ServerHttpRequestIntegrationTests extends AbstractHttpHandlerIntegr | ||||||
| 		@Override | 		@Override | ||||||
| 		public Mono<Void> handle(ServerHttpRequest request, ServerHttpResponse response) { | 		public Mono<Void> handle(ServerHttpRequest request, ServerHttpResponse response) { | ||||||
| 			URI uri = request.getURI(); | 			URI uri = request.getURI(); | ||||||
|  | 			assertEquals("http", uri.getScheme()); | ||||||
| 			assertNotNull(uri.getHost()); | 			assertNotNull(uri.getHost()); | ||||||
| 			assertNotEquals(-1, uri.getPort()); | 			assertNotEquals(-1, uri.getPort()); | ||||||
| 			assertNotNull(request.getRemoteAddress()); | 			assertNotNull(request.getRemoteAddress()); | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue