Interface HttpServer
-
- All Superinterfaces:
Measured
public interface HttpServer extends Measured
An HTTP and WebSockets server.You receive HTTP requests by providing a
requestHandler(io.vertx.core.Handler<io.vertx.core.http.HttpServerRequest>)
. As requests arrive on the server the handler will be called with the requests.You receive WebSockets by providing a
webSocketHandler(io.vertx.core.Handler<io.vertx.core.http.ServerWebSocket>)
. As WebSocket connections arrive on the server, the WebSocket is passed to the handler.- Author:
- Tim Fox
-
-
Method Summary
All Methods Instance Methods Abstract Methods Default Methods Modifier and Type Method Description int
actualPort()
The actual port the server is listening on.default Future<Void>
close()
Close the server.HttpServer
connectionHandler(Handler<HttpConnection> handler)
Set a connection handler for the server.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.HttpServer
invalidRequestHandler(Handler<HttpServerRequest> handler)
Set ahandler
for handling invalid requests.Future<HttpServer>
listen()
Tell the server to start listening.default Future<HttpServer>
listen(int port)
Likelisten(int, String)
but the server will listen on host "0.0.0.0" and port specified here ignoring any value in theHttpServerOptions
that was used when creating the server.default Future<HttpServer>
listen(int port, String host)
Tell the server to start listening.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).Handler<HttpServerRequest>
requestHandler()
HttpServer
requestHandler(Handler<HttpServerRequest> handler)
Set the request handler for the server torequestHandler
.default Future<Void>
shutdown()
Shutdown with a 30 seconds timeout (shutdown(30, TimeUnit.SECONDS)
).Future<Void>
shutdown(long timeout, TimeUnit unit)
Initiate the server shutdown sequence.default Future<Boolean>
updateSSLOptions(ServerSSLOptions options)
Update the server with new SSLoptions
, the update happens if the options object is valid and different from the existing options object.Future<Boolean>
updateSSLOptions(ServerSSLOptions options, boolean force)
Update the server with new SSLoptions
, the update happens if the options object is valid and different from the existing options object.void
updateTrafficShapingOptions(TrafficShapingOptions options)
Update traffic shaping optionsoptions
, the update happens if valid values are passed for traffic shaping options.Handler<ServerWebSocket>
webSocketHandler()
HttpServer
webSocketHandler(Handler<ServerWebSocket> handler)
Set the WebSocket handler for the server towsHandler
.HttpServer
webSocketHandshakeHandler(Handler<ServerWebSocketHandshake> handler)
Set a handler for WebSocket handshake.-
Methods inherited from interface io.vertx.core.metrics.Measured
isMetricsEnabled
-
-
-
-
Method Detail
-
requestHandler
HttpServer requestHandler(Handler<HttpServerRequest> handler)
Set the request handler for the server torequestHandler
. As HTTP requests are received by the server, instances ofHttpServerRequest
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 ahandler
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()
orServerWebSocketHandshake.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 towsHandler
. If a WebSocket connect handshake is successful a newServerWebSocket
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 SSLoptions
, 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 itsequals
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, settingforce
totrue
forces the update.The boolean succeeded future result indicates whether the update occurred.
- Parameters:
options
- the new SSL optionsforce
- force the update when options are equals- Returns:
- a future signaling the update success
-
updateTrafficShapingOptions
void updateTrafficShapingOptions(TrafficShapingOptions options)
Update traffic shaping optionsoptions
, the update happens if valid values are passed for traffic shaping options. This update happens synchronously and at best effort for rate update to take effect immediately.- Parameters:
options
- the new traffic shaping options
-
listen
Future<HttpServer> listen()
Tell the server to start listening. The server will listen on the port and host specified in theHttpServerOptions
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 theHttpServerOptions
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 onhost
- 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)
Likelisten(int, String)
but the server will listen on host "0.0.0.0" and port specified here ignoring any value in theHttpServerOptions
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(30, TimeUnit.SECONDS)
).- Returns:
- a future completed when shutdown has completed
-
shutdown
Future<Void> shutdown(long timeout, TimeUnit unit)
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 closedunit
- the of the timeout- 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.
-
-