// Licensed to the Software Freedom Conservancy (SFC) under one
// or more contributor license agreements.  See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership.  The SFC licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License.  You may obtain a copy of the License at
//
//   http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied.  See the License for the
// specific language governing permissions and limitations
// under the License.

package org.openqa.selenium.netty.server;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.Channel;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;
import io.netty.handler.ssl.SslContext;
import io.netty.handler.ssl.SslContextBuilder;
import io.netty.handler.ssl.util.SelfSignedCertificate;
import io.netty.util.internal.logging.InternalLoggerFactory;
import io.netty.util.internal.logging.JdkLoggerFactory;
import org.openqa.selenium.remote.AddWebDriverSpecHeaders;
import org.openqa.selenium.grid.server.BaseServerOptions;
import org.openqa.selenium.grid.server.Server;
import org.openqa.selenium.remote.ErrorFilter;
import org.openqa.selenium.internal.Require;
import org.openqa.selenium.remote.http.HttpHandler;
import org.openqa.selenium.remote.http.Message;

import javax.net.ssl.SSLException;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.net.BindException;
import java.net.MalformedURLException;
import java.net.URL;
import java.security.cert.CertificateException;
import java.util.Optional;
import java.util.function.BiFunction;
import java.util.function.Consumer;

public class NettyServer implements Server<NettyServer> {

  private final EventLoopGroup bossGroup;
  private final EventLoopGroup workerGroup;
  private final int port;
  private final URL externalUrl;
  private final HttpHandler handler;
  private final BiFunction<String, Consumer<Message>, Optional<Consumer<Message>>> websocketHandler;
  private final SslContext sslCtx;
  private final boolean allowCors;

  private Channel channel;

  public NettyServer(
    BaseServerOptions options,
    HttpHandler handler) {
    this(options, handler, (str, sink) -> Optional.empty());
  }

  public NettyServer(
    BaseServerOptions options,
    HttpHandler handler,
    BiFunction<String, Consumer<Message>, Optional<Consumer<Message>>> websocketHandler) {
    Require.nonNull("Server options", options);
    Require.nonNull("Handler", handler);
    this.websocketHandler = Require.nonNull("Factory for websocket connections", websocketHandler);

    InternalLoggerFactory.setDefaultFactory(JdkLoggerFactory.INSTANCE);

    boolean secure = options.isSecure();
    if (secure) {
      try {
        sslCtx = SslContextBuilder.forServer(options.getCertificate(), options.getPrivateKey())
          .build();
      } catch (SSLException e) {
        throw new UncheckedIOException(new IOException("Certificate problem.", e));
      }
    } else if (options.isSelfSigned()) {
      try {
        SelfSignedCertificate cert = new SelfSignedCertificate();
        sslCtx = SslContextBuilder.forServer(cert.certificate(), cert.privateKey())
          .build();
      } catch (CertificateException | SSLException e) {
        throw new UncheckedIOException(new IOException("Self-signed certificate problem.", e));
      }
    } else {
      sslCtx = null;
    }

    this.handler = handler.with(new ErrorFilter().andThen(new AddWebDriverSpecHeaders()));

    bossGroup = new NioEventLoopGroup(1);
    workerGroup = new NioEventLoopGroup();

    port = options.getPort();
    allowCors = options.getAllowCORS();

    try {
      externalUrl = options.getExternalUri().toURL();
    } catch (MalformedURLException e) {
      throw new UncheckedIOException("Server URI is not a valid URL: " + options.getExternalUri(), e);
    }
  }

  @Override
  public boolean isStarted() {
    return channel != null;
  }

  @Override
  public URL getUrl() {
    return externalUrl;
  }

  @Override
  public void stop() {
    try {
      bossGroup.shutdownGracefully().sync();
      workerGroup.shutdownGracefully().sync();

      channel.closeFuture().sync();
    } catch (InterruptedException e) {
      Thread.currentThread().interrupt();
      throw new UncheckedIOException(new IOException("Shutdown interrupted", e));
    } finally {
      channel = null;
    }
  }

  @SuppressWarnings("ConstantConditions")
  public NettyServer start() {
    ServerBootstrap b = new ServerBootstrap();

    b.group(bossGroup, workerGroup)
      .channel(NioServerSocketChannel.class)
      .handler(new LoggingHandler(LogLevel.DEBUG))
      .childHandler(new SeleniumHttpInitializer(sslCtx, handler, websocketHandler, allowCors));

    try {
      channel = b.bind(port).sync().channel();
    } catch (InterruptedException e) {
      Thread.currentThread().interrupt();
      throw new UncheckedIOException(new IOException("Start up interrupted", e));
    } catch (Exception e) {
      if (e instanceof BindException) {
        throw new UncheckedIOException(new IOException(String.format("Port %s already in use", port), e));
      }
      throw e;
    }

    return this;
  }
}

@@ -840,16 +840,51 @@
 erver;%0A%0A
+import java.net.InetSocketAddress;%0A
 import i
@@ -918,16 +918,16 @@
 tstrap;%0A
-
 import i
@@ -2316,16 +2316,45 @@
 t port;%0A
+  private final String host;%0A
   privat
@@ -4133,24 +4133,76 @@
 .getPort();%0A
+    host = options.getHostname().orElse(%220.0.0.0%22);%0A
     allowCor
@@ -5358,21 +5358,50 @@
  b.bind(
+new InetSocketAddress(host, 
 port)
+)
 .sync().

Fix host option is not working issue (#9981)