Skip to content

Commit

Permalink
Ensuring the base server can have a host name set
Browse files Browse the repository at this point in the history
  • Loading branch information
shs96c committed Sep 24, 2018
1 parent 15b2b44 commit ae8eff9
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 12 deletions.
32 changes: 22 additions & 10 deletions java/server/src/org/openqa/selenium/grid/server/BaseServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -56,20 +57,23 @@
public class BaseServer implements Server<BaseServer> {

private final org.seleniumhq.jetty9.server.Server server;
private final Map<Predicate<HttpRequest>, BiFunction<Injector, HttpRequest, CommandHandler>> handlers;
private final Map<Predicate<HttpRequest>, BiFunction<Injector, HttpRequest, CommandHandler>>
handlers;
private final ServletContextHandler servletContextHandler;
private final Injector injector;
private final URL url;

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) {
Expand Down Expand Up @@ -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");
Expand All @@ -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), "/*");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -28,6 +30,10 @@ public BaseServerOptions(Config config) {
this.config = config;
}

public Optional<String> getHostname() {
return config.get("server", "hostname");
}

public int getPort() {
int port = config.getInt("server", "port")
.orElse(0);
Expand Down

0 comments on commit ae8eff9

Please sign in to comment.