|
20 | 20 | import java.net.URI;
|
21 | 21 | import java.net.URISyntaxException;
|
22 | 22 |
|
| 23 | +import io.netty.handler.codec.http.HttpHeaderNames; |
23 | 24 | import io.netty.handler.codec.http.cookie.Cookie;
|
24 | 25 | import reactor.core.publisher.Flux;
|
25 | 26 | import reactor.ipc.netty.http.server.HttpServerRequest;
|
@@ -55,16 +56,39 @@ public ReactorServerHttpRequest(HttpServerRequest request, NettyDataBufferFactor
|
55 | 56 | this.bufferFactory = bufferFactory;
|
56 | 57 | }
|
57 | 58 |
|
58 |
| - private static URI initUri(HttpServerRequest channel) throws URISyntaxException { |
59 |
| - Assert.notNull(channel, "'channel' must not be null"); |
60 |
| - InetSocketAddress address = channel.remoteAddress(); |
61 |
| - String requestUri = channel.uri(); |
62 |
| - return (address != null ? createUrl(address, requestUri) : new URI(requestUri)); |
| 59 | + private static URI initUri(HttpServerRequest request) throws URISyntaxException { |
| 60 | + Assert.notNull(request, "'request' must not be null"); |
| 61 | + URI baseUri = resolveBaseUrl(request); |
| 62 | + String requestUri = request.uri(); |
| 63 | + return (baseUri != null ? new URI(baseUri.toString() + requestUri) : new URI(requestUri)); |
63 | 64 | }
|
64 | 65 |
|
65 |
| - private static URI createUrl(InetSocketAddress address, String requestUri) throws URISyntaxException { |
66 |
| - URI baseUrl = new URI(null, null, address.getHostString(), address.getPort(), null, null, null); |
67 |
| - return new URI(baseUrl.toString() + requestUri); |
| 66 | + private static URI resolveBaseUrl(HttpServerRequest request) throws URISyntaxException { |
| 67 | + String header = request.requestHeaders().get(HttpHeaderNames.HOST); |
| 68 | + if (header != null) { |
| 69 | + final int portIndex; |
| 70 | + if (header.startsWith("[")) { |
| 71 | + portIndex = header.indexOf(':', header.indexOf(']')); |
| 72 | + } else { |
| 73 | + portIndex = header.indexOf(':'); |
| 74 | + } |
| 75 | + if (portIndex != -1) { |
| 76 | + try { |
| 77 | + return new URI(null, null, header.substring(0, portIndex), |
| 78 | + Integer.parseInt(header.substring(portIndex + 1)), null, null, null); |
| 79 | + } catch (NumberFormatException ignore) { |
| 80 | + throw new URISyntaxException(header, "Unable to parse port", portIndex); |
| 81 | + } |
| 82 | + } |
| 83 | + else { |
| 84 | + return new URI(null, header, null, null); |
| 85 | + } |
| 86 | + } |
| 87 | + else { |
| 88 | + InetSocketAddress localAddress = (InetSocketAddress) request.context().channel().localAddress(); |
| 89 | + return new URI(null, null, localAddress.getHostString(), |
| 90 | + localAddress.getPort(), null, null, null); |
| 91 | + } |
68 | 92 | }
|
69 | 93 |
|
70 | 94 | private static HttpHeaders initHeaders(HttpServerRequest channel) {
|
|
0 commit comments