From ae8eff97c4c802e2100093ffa6a090a5d9d9f958 Mon Sep 17 00:00:00 2001 From: Simon Stewart Date: Mon, 24 Sep 2018 12:31:41 +0100 Subject: [PATCH] Ensuring the base server can have a host name set --- .../selenium/grid/server/BaseServer.java | 32 +++++++++++++------ .../selenium/grid/server/BaseServerFlags.java | 10 ++++-- .../grid/server/BaseServerOptions.java | 6 ++++ 3 files changed, 36 insertions(+), 12 deletions(-) diff --git a/java/server/src/org/openqa/selenium/grid/server/BaseServer.java b/java/server/src/org/openqa/selenium/grid/server/BaseServer.java index 787fac2f2392a..1d51678360a1f 100644 --- a/java/server/src/org/openqa/selenium/grid/server/BaseServer.java +++ b/java/server/src/org/openqa/selenium/grid/server/BaseServer.java @@ -34,6 +34,7 @@ import org.openqa.selenium.remote.http.HttpRequest; import org.seleniumhq.jetty9.security.ConstraintMapping; import org.seleniumhq.jetty9.security.ConstraintSecurityHandler; +import org.seleniumhq.jetty9.server.Connector; import org.seleniumhq.jetty9.server.HttpConfiguration; import org.seleniumhq.jetty9.server.HttpConnectionFactory; import org.seleniumhq.jetty9.server.ServerConnector; @@ -56,7 +57,8 @@ public class BaseServer implements Server { private final org.seleniumhq.jetty9.server.Server server; - private final Map, BiFunction> handlers; + private final Map, BiFunction> + handlers; private final ServletContextHandler servletContextHandler; private final Injector injector; private final URL url; @@ -64,12 +66,14 @@ public class BaseServer implements Server { public BaseServer(BaseServerOptions options) { int port = options.getPort() == 0 ? PortProber.findFreePort() : options.getPort(); - String host; - try { - host = new NetworkUtils().getNonLoopbackAddressOfThisMachine(); - } catch (WebDriverException ignored) { - host = "localhost"; - } + String host = options.getHostname().orElseGet(() -> { + try { + return new NetworkUtils().getNonLoopbackAddressOfThisMachine(); + } catch (WebDriverException ignored) { + return "localhost"; + } + }); + try { this.url = new URL("http", host, port, ""); } catch (MalformedURLException e) { @@ -102,7 +106,9 @@ public BaseServer(BaseServerOptions options) { }); this.servletContextHandler = new ServletContextHandler(ServletContextHandler.SECURITY); - ConstraintSecurityHandler securityHandler = (ConstraintSecurityHandler) servletContextHandler.getSecurityHandler(); + ConstraintSecurityHandler + securityHandler = + (ConstraintSecurityHandler) servletContextHandler.getSecurityHandler(); Constraint disableTrace = new Constraint(); disableTrace.setName("Disable TRACE"); @@ -117,16 +123,22 @@ public BaseServer(BaseServerOptions options) { enableOther.setName("Enable everything but TRACE"); ConstraintMapping enableOtherMapping = new ConstraintMapping(); enableOtherMapping.setConstraint(enableOther); - enableOtherMapping.setMethodOmissions(new String[] {"TRACE"}); + enableOtherMapping.setMethodOmissions(new String[]{"TRACE"}); enableOtherMapping.setPathSpec("/"); securityHandler.addConstraintMapping(enableOtherMapping); server.setHandler(servletContextHandler); HttpConfiguration httpConfig = new HttpConfiguration(); + httpConfig.setSecureScheme("https"); + ServerConnector http = new ServerConnector(server, new HttpConnectionFactory(httpConfig)); + http.setHost(getUrl().getHost()); http.setPort(getUrl().getPort()); - server.addConnector(http); + + http.setIdleTimeout(500000); + + server.setConnectors(new Connector[]{http}); addServlet(new CommandHandlerServlet(injector, handlers), "/*"); } diff --git a/java/server/src/org/openqa/selenium/grid/server/BaseServerFlags.java b/java/server/src/org/openqa/selenium/grid/server/BaseServerFlags.java index 7a58cb4aa92db..dc959bd84c837 100644 --- a/java/server/src/org/openqa/selenium/grid/server/BaseServerFlags.java +++ b/java/server/src/org/openqa/selenium/grid/server/BaseServerFlags.java @@ -23,11 +23,17 @@ public class BaseServerFlags { - @Parameter(description = "Port to listen on", names = {"-p", "--port"}) + @Parameter( + names = {"--host"}, + description = "IP or hostname : usually determined automatically.") + @ConfigValue(section = "server", name = "hostname") + private String host; + + @Parameter(description = "Port to listen on.", names = {"-p", "--port"}) @ConfigValue(section = "server", name = "port") private int port = 0; - @Parameter(description = "Maximum number of listener threads", names = "--max-threads") + @Parameter(description = "Maximum number of listener threads.", names = "--max-threads") @ConfigValue(section = "server", name = "max-threads") private int maxThreads = Runtime.getRuntime().availableProcessors() * 3; diff --git a/java/server/src/org/openqa/selenium/grid/server/BaseServerOptions.java b/java/server/src/org/openqa/selenium/grid/server/BaseServerOptions.java index ca623eee6470f..af3ff69f2cd7c 100644 --- a/java/server/src/org/openqa/selenium/grid/server/BaseServerOptions.java +++ b/java/server/src/org/openqa/selenium/grid/server/BaseServerOptions.java @@ -20,6 +20,8 @@ import org.openqa.selenium.grid.config.Config; import org.openqa.selenium.grid.config.ConfigException; +import java.util.Optional; + public class BaseServerOptions { private final Config config; @@ -28,6 +30,10 @@ public BaseServerOptions(Config config) { this.config = config; } + public Optional getHostname() { + return config.get("server", "hostname"); + } + public int getPort() { int port = config.getInt("server", "port") .orElse(0);