From d42f9281130b932cac68fbbf0a0c4429c2e0e5ba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=CC=88rg=20Prante?= Date: Tue, 4 Aug 2020 06:42:48 +0200 Subject: [PATCH] fix for separate bind URL / header-base URL --- .../org/xbib/netty/http/server/Server.java | 39 ++++++++++--------- .../http/server/test/ContextURLTest.java | 3 ++ 2 files changed, 24 insertions(+), 18 deletions(-) diff --git a/netty-http-server/src/main/java/org/xbib/netty/http/server/Server.java b/netty-http-server/src/main/java/org/xbib/netty/http/server/Server.java index 2a52871..1bfe1cd 100644 --- a/netty-http-server/src/main/java/org/xbib/netty/http/server/Server.java +++ b/netty-http-server/src/main/java/org/xbib/netty/http/server/Server.java @@ -206,25 +206,28 @@ public final class Server implements AutoCloseable { } public URL getBaseURL(HttpHeaders headers) { - URL bindURL = serverConfig.getDefaultDomain().getHttpAddress().base(); - String scheme = headers != null ? headers.get("x-forwarded-proto") : null; - if (scheme == null) { - scheme = bindURL.getScheme(); - } - String host = headers != null ? headers.get("x-forwarded-host") : null; - if (host == null) { - host = headers != null ? headers.get("host") : null; - if (host == null) { - host = bindURL.getHost(); - } - } + String scheme = null; + String host = null; String port = null; - if (host != null) { - host = stripPort(host); - port = extractPort(host); - if (port == null) { - port = bindURL.getPort() != null ? Integer.toString(bindURL.getPort()) : null; + if (headers == null) { + URL bindURL = serverConfig.getDefaultDomain().getHttpAddress().base(); + scheme = bindURL.getScheme(); + host = bindURL.getHost(); + port = bindURL.getPort() != null ? Integer.toString(bindURL.getPort()) : null; + } else if (headers.get("host") != null) { + // proxy proto, host + scheme = headers.get("x-forwarded-proto"); + if (scheme == null) { + scheme = "http"; } + host = headers.get("x-forwarded-host"); + if (host == null) { + host = headers.get("host"); + } + port = extractPort(host); + host = stripPort(host); + } else { + throw new IllegalArgumentException("no host header in " + headers); } URL.Builder builder = URL.builder().scheme(scheme).host(host); if (port != null) { @@ -338,7 +341,7 @@ public final class Server implements AutoCloseable { } private static String hostAndPort(URL url) { - return url == null ? null : url.getPort() != -1 ? url.getHost() + ":" + url.getPort() : url.getHost(); + return url == null ? null : url.getPort() != null && url.getPort() != -1 ? url.getHost() + ":" + url.getPort() : url.getHost(); } private HttpChannelInitializer findChannelInitializer(int majorVersion, diff --git a/netty-http-server/src/test/java/org/xbib/netty/http/server/test/ContextURLTest.java b/netty-http-server/src/test/java/org/xbib/netty/http/server/test/ContextURLTest.java index 8099f5a..02f4098 100644 --- a/netty-http-server/src/test/java/org/xbib/netty/http/server/test/ContextURLTest.java +++ b/netty-http-server/src/test/java/org/xbib/netty/http/server/test/ContextURLTest.java @@ -4,6 +4,7 @@ import io.netty.handler.codec.http.DefaultFullHttpRequest; import io.netty.handler.codec.http.HttpMethod; import io.netty.handler.codec.http.HttpVersion; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.xbib.net.URL; @@ -56,6 +57,7 @@ public class ContextURLTest { String contextPath1 = serverRequest1.getContextPath(); assertEquals("/one", contextPath1); URL url1 = serverRequest1.getContextURL(); + assertNotNull(url1); assertEquals("domain.one", url1.getHost()); assertEquals("/one/", url1.getPath()); @@ -65,6 +67,7 @@ public class ContextURLTest { String contextPath2 = serverRequest2.getContextPath(); assertEquals("/two", contextPath2); URL url2 = serverRequest2.getContextURL(); + assertNotNull(url2); assertEquals("domain.two", url2.getHost()); assertEquals("/two/", url2.getPath());