Interface HttpServer

All Superinterfaces:
Measured

public interface HttpServer extends Measured
An HTTP and WebSockets server.

You receive HTTP requests by providing a requestHandler(Handler). As requests arrive on the server the handler will be called with the requests.

You receive WebSockets by providing a webSocketHandler(Handler). As WebSocket connections arrive on the server, the WebSocket is passed to the handler.

Author:
Tim Fox
  • Method Details

    • requestHandler

      HttpServer requestHandler(Handler<HttpServerRequest> handler)
      Set the request handler for the server to requestHandler. As HTTP requests are received by the server, instances of HttpServerRequest will be created and passed to this handler.
      Returns:
      a reference to this, so the API can be used fluently
    • requestHandler

      Handler<HttpServerRequest> requestHandler()
      Returns:
      the request handler
    • invalidRequestHandler

      HttpServer invalidRequestHandler(Handler<HttpServerRequest> handler)
      Set a handler for handling invalid requests. When an invalid request is received by the server this handler will be called with the request. The handler can send any HTTP response, when the response ends, the server shall close the connection. HttpServerRequest.decoderResult() can be used to obtain the Netty decoder result and the failure cause reported by the decoder.

      Currently this handler is only used for HTTP/1.x requests.

      When no specific handler is set, the HttpServerRequest.DEFAULT_INVALID_REQUEST_HANDLER is used.

      Returns:
      a reference to this, so the API can be used fluently
    • connectionHandler

      HttpServer connectionHandler(Handler<HttpConnection> handler)
      Set a connection handler for the server.
      The handler will always be called on the event-loop thread.
      Returns:
      a reference to this, so the API can be used fluently
    • webSocketHandshakeHandler

      HttpServer webSocketHandshakeHandler(Handler<ServerWebSocketHandshake> handler)
      Set a handler for WebSocket handshake.

      When an inbound HTTP request presents a WebSocket upgrade, this handler is called first. The handler can chose to ServerWebSocketHandshake.accept() or ServerWebSocketHandshake.reject() the request.

      Setting no handler, implicitly accepts any HTTP request connection presenting an upgrade header and upgrades it to a WebSocket.

    • exceptionHandler

      HttpServer exceptionHandler(Handler<Throwable> handler)
      Set an exception handler called for socket errors happening before the HTTP connection is established, e.g. during the TLS handshake.
      Parameters:
      handler - the handler to set
      Returns:
      a reference to this, so the API can be used fluently
    • webSocketHandler

      HttpServer webSocketHandler(Handler<ServerWebSocket> handler)
      Set the WebSocket handler for the server to wsHandler. If a WebSocket connect handshake is successful a new ServerWebSocket instance will be created and passed to the handler.
      Returns:
      a reference to this, so the API can be used fluently
    • webSocketHandler

      Handler<ServerWebSocket> webSocketHandler()
      Returns:
      the WebSocket handler
    • updateSSLOptions

      default Future<Boolean> updateSSLOptions(ServerSSLOptions options)
      Update the server with new SSL options, the update happens if the options object is valid and different from the existing options object.

      The boolean succeeded future result indicates whether the update occurred.

      Parameters:
      options - the new SSL options
      Returns:
      a future signaling the update success
    • updateSSLOptions

      Future<Boolean> updateSSLOptions(ServerSSLOptions options, boolean force)

      Update the server with new SSL options, the update happens if the options object is valid and different from the existing options object.

      The options object is compared using its equals method against the existing options to prevent an update when the objects are equals since loading options can be costly, this can happen for share TCP servers. When object are equals, setting force to true forces the update.

      The boolean succeeded future result indicates whether the update occurred.

      Parameters:
      options - the new SSL options
      force - force the update when options are equals
      Returns:
      a future signaling the update success
    • updateTrafficShapingOptions

      Future<Boolean> updateTrafficShapingOptions(TrafficShapingOptions options)

      Update the server with new traffic options, the update happens if the options object is valid and different from the existing options object.

      The options object is compared using its equals method against the existing options to prevent an update when the objects are equals since loading options can be costly, this can happen for share TCP servers. When object are equals, setting force to true forces the update.

      The boolean succeeded future result indicates whether the update occurred.

      Parameters:
      options - the new traffic shaping options
      Returns:
      a future signaling the update success
    • listen

      Future<HttpServer> listen()
      Tell the server to start listening. The server will listen on the port and host specified in the HttpServerOptions that was used when creating the server.

      The listen happens asynchronously and the server may not be listening until some time after the call has returned.

      Returns:
      a future completed with the listen operation result
    • listen

      default Future<HttpServer> listen(int port, String host)
      Tell the server to start listening. The server will listen on the port and host specified here, ignoring any value set in the HttpServerOptions that was used when creating the server.

      The listen happens asynchronously and the server may not be listening until some time after the call has returned.

      Parameters:
      port - the port to listen on
      host - the host to listen on
      Returns:
      a future completed with the listen operation result
    • listen

      Future<HttpServer> listen(SocketAddress address)
      Tell the server to start listening on the given address supplying a handler that will be called when the server is actually listening (or has failed).
      Parameters:
      address - the address to listen on
      Returns:
      a future completed with the listen operation result
    • listen

      default Future<HttpServer> listen(int port)
      Like listen(int, String) but the server will listen on host "0.0.0.0" and port specified here ignoring any value in the HttpServerOptions that was used when creating the server.
      Parameters:
      port - the port to listen on
      Returns:
      a future completed with the listen operation result
    • close

      default Future<Void> close()
      Close the server. Any open HTTP connections will be closed.

      The close happens asynchronously and the server may not be closed until some time after the call has returned.

      Returns:
      a future completed with the result
    • shutdown

      default Future<Void> shutdown()
      Shutdown with a 30 seconds timeout (shutdown(Duration.ofSeconds(30))).
      Returns:
      a future completed when shutdown has completed
    • shutdown

      default Future<Void> shutdown(long timeout, TimeUnit unit)
    • shutdown

      Future<Void> shutdown(Duration timeout)
      Initiate the server shutdown sequence.

      Connections are taken out of service and closed when all inflight requests are processed. When all connections are closed the server is closed. When the timeout expires, all unclosed connections are immediately closed.

      • HTTP/2 connections will send a go away frame immediately to signal the other side the connection will close
      • HTTP/1.x server connection will be closed after the current response is sent
      Parameters:
      timeout - the amount of time after which all resources are forcibly closed
      Returns:
      a future notified when the client is closed
    • actualPort

      int actualPort()
      The actual port the server is listening on. This is useful if you bound the server specifying 0 as port number signifying an ephemeral port
      Returns:
      the actual port the server is listening on.