From c79815f3bf552dd3b176cef04127914409fd1ccc Mon Sep 17 00:00:00 2001 From: Rob Bygrave Date: Sun, 1 Dec 2024 10:05:25 +1300 Subject: [PATCH] Restore binding to any local address (rather than loopback address) (#104) --- .../main/java/io/avaje/jex/DJexConfig.java | 15 ++++++---- .../src/main/java/io/avaje/jex/JexConfig.java | 5 +++- .../java/io/avaje/jex/jdk/JdkServerStart.java | 28 ++++++++++--------- 3 files changed, 29 insertions(+), 19 deletions(-) diff --git a/avaje-jex/src/main/java/io/avaje/jex/DJexConfig.java b/avaje-jex/src/main/java/io/avaje/jex/DJexConfig.java index c6d14b29..ee0cb116 100644 --- a/avaje-jex/src/main/java/io/avaje/jex/DJexConfig.java +++ b/avaje-jex/src/main/java/io/avaje/jex/DJexConfig.java @@ -86,8 +86,8 @@ public Executor executor() { } @Override - public JexConfig executor(Executor factory) { - this.executor = factory; + public JexConfig executor(Executor executor) { + this.executor = executor; return this; } @@ -131,14 +131,19 @@ public Map renderers() { return renderers; } + @Override + public String scheme() { + return httpsConfig == null ? "http" : "https"; + } + @Override public HttpsConfigurator httpsConfig() { - return this.httpsConfig; + return httpsConfig; } @Override - public JexConfig httpsConfig(HttpsConfigurator ssl) { - this.httpsConfig = ssl; + public JexConfig httpsConfig(HttpsConfigurator httpsConfig) { + this.httpsConfig = httpsConfig; return this; } diff --git a/avaje-jex/src/main/java/io/avaje/jex/JexConfig.java b/avaje-jex/src/main/java/io/avaje/jex/JexConfig.java index b19e429c..c36c42e3 100644 --- a/avaje-jex/src/main/java/io/avaje/jex/JexConfig.java +++ b/avaje-jex/src/main/java/io/avaje/jex/JexConfig.java @@ -21,7 +21,7 @@ public sealed interface JexConfig permits DJexConfig { /** - * Set the host on which the HttpServer will bind to. + * Set the host on which the HttpServer will bind to. Defaults to any local address. * * @param host The host. */ @@ -122,6 +122,9 @@ public sealed interface JexConfig permits DJexConfig { /** Return the {@link HttpsConfigurator} if https is enabled. */ HttpsConfigurator httpsConfig(); + /** Return the schema as http or https. */ + String scheme(); + /** Returns the configured compression settings. */ CompressionConfig compression(); diff --git a/avaje-jex/src/main/java/io/avaje/jex/jdk/JdkServerStart.java b/avaje-jex/src/main/java/io/avaje/jex/jdk/JdkServerStart.java index c350e74d..9af850fd 100644 --- a/avaje-jex/src/main/java/io/avaje/jex/jdk/JdkServerStart.java +++ b/avaje-jex/src/main/java/io/avaje/jex/jdk/JdkServerStart.java @@ -4,6 +4,7 @@ import java.io.UncheckedIOException; import java.net.InetAddress; import java.net.InetSocketAddress; +import java.net.UnknownHostException; import com.sun.net.httpserver.HttpServer; import com.sun.net.httpserver.HttpsServer; @@ -11,6 +12,7 @@ import io.avaje.applog.AppLog; import io.avaje.jex.AppLifecycle; import io.avaje.jex.Jex; +import io.avaje.jex.JexConfig; import io.avaje.jex.core.SpiServiceManager; import io.avaje.jex.routes.SpiRoutes; @@ -23,25 +25,20 @@ public final class JdkServerStart { public Jex.Server start(Jex jex, SpiRoutes routes, SpiServiceManager serviceManager) { try { final var config = jex.config(); - final var port = config.port(); - final var host = InetAddress.getByName(config.host()); - final var socket = new InetSocketAddress(host, config.port()); - final var contextPath = config.contextPath(); + final var socketAddress = createSocketAddress(config); final var https = config.httpsConfig(); - final var backlog = config.socketBacklog(); final HttpServer server; - final String scheme; if (https != null) { - var httpsServer = HttpsServer.create(socket, backlog); + var httpsServer = HttpsServer.create(socketAddress, config.socketBacklog()); httpsServer.setHttpsConfigurator(https); server = httpsServer; - scheme = "https"; } else { - scheme = "http"; - server = HttpServer.create(socket, backlog); + server = HttpServer.create(socketAddress, config.socketBacklog()); } + final var scheme = config.scheme(); + final var contextPath = config.contextPath(); final var manager = new CtxServiceManager(serviceManager, scheme, contextPath); final var handler = new RoutingHandler(routes, manager, config.compression()); @@ -51,12 +48,17 @@ public Jex.Server start(Jex jex, SpiRoutes routes, SpiServiceManager serviceMana jex.lifecycle().status(AppLifecycle.Status.STARTED); log.log( - INFO, - "started com.sun.net.httpserver.HttpServer on port %s://%s:%s" - .formatted(scheme, host.getHostName(), port)); + INFO, + "started com.sun.net.httpserver.HttpServer on port {0}://{1}", + scheme, socketAddress); return new JdkJexServer(server, jex.lifecycle(), handler); } catch (IOException e) { throw new UncheckedIOException(e); } } + + private static InetSocketAddress createSocketAddress(JexConfig config) throws UnknownHostException { + final var inetAddress = config.host() == null ? null : InetAddress.getByName(config.host()); + return new InetSocketAddress(inetAddress, config.port()); + } }