fix for separate bind URL / header-base URL

This commit is contained in:
Jörg Prante 2020-08-04 06:42:48 +02:00
parent 849a77aeec
commit d42f928113
2 changed files with 24 additions and 18 deletions

View file

@ -206,25 +206,28 @@ public final class Server implements AutoCloseable {
} }
public URL getBaseURL(HttpHeaders headers) { public URL getBaseURL(HttpHeaders headers) {
URL bindURL = serverConfig.getDefaultDomain().getHttpAddress().base(); String scheme = null;
String scheme = headers != null ? headers.get("x-forwarded-proto") : null; String host = 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 port = null; String port = null;
if (host != null) { if (headers == null) {
host = stripPort(host); URL bindURL = serverConfig.getDefaultDomain().getHttpAddress().base();
port = extractPort(host); scheme = bindURL.getScheme();
if (port == null) { host = bindURL.getHost();
port = bindURL.getPort() != null ? Integer.toString(bindURL.getPort()) : null; 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); URL.Builder builder = URL.builder().scheme(scheme).host(host);
if (port != null) { if (port != null) {
@ -338,7 +341,7 @@ public final class Server implements AutoCloseable {
} }
private static String hostAndPort(URL url) { 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, private HttpChannelInitializer findChannelInitializer(int majorVersion,

View file

@ -4,6 +4,7 @@ import io.netty.handler.codec.http.DefaultFullHttpRequest;
import io.netty.handler.codec.http.HttpMethod; import io.netty.handler.codec.http.HttpMethod;
import io.netty.handler.codec.http.HttpVersion; import io.netty.handler.codec.http.HttpVersion;
import static org.junit.jupiter.api.Assertions.assertEquals; 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.Test;
import org.junit.jupiter.api.extension.ExtendWith; import org.junit.jupiter.api.extension.ExtendWith;
import org.xbib.net.URL; import org.xbib.net.URL;
@ -56,6 +57,7 @@ public class ContextURLTest {
String contextPath1 = serverRequest1.getContextPath(); String contextPath1 = serverRequest1.getContextPath();
assertEquals("/one", contextPath1); assertEquals("/one", contextPath1);
URL url1 = serverRequest1.getContextURL(); URL url1 = serverRequest1.getContextURL();
assertNotNull(url1);
assertEquals("domain.one", url1.getHost()); assertEquals("domain.one", url1.getHost());
assertEquals("/one/", url1.getPath()); assertEquals("/one/", url1.getPath());
@ -65,6 +67,7 @@ public class ContextURLTest {
String contextPath2 = serverRequest2.getContextPath(); String contextPath2 = serverRequest2.getContextPath();
assertEquals("/two", contextPath2); assertEquals("/two", contextPath2);
URL url2 = serverRequest2.getContextURL(); URL url2 = serverRequest2.getContextURL();
assertNotNull(url2);
assertEquals("domain.two", url2.getHost()); assertEquals("domain.two", url2.getHost());
assertEquals("/two/", url2.getPath()); assertEquals("/two/", url2.getPath());