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) {
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,

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.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());