public abstract class HttpServer
extends java.lang.Object
bind(TcpServer) is ultimately called.
Internally, materialization happens in three phases, first tcpConfiguration() is called to retrieve a ready to use TcpServer, then
tcpConfiguration() ()} retrieve a usable TcpServer for the final
bind(TcpServer) is called.
Examples:
HttpServer.create()
.host("0.0.0.0")
.handle((req, res) -> res.sendString(Flux.just("hello"))
.bind()
.block();
| Constructor and Description |
|---|
HttpServer() |
| Modifier and Type | Method and Description |
|---|---|
Mono<? extends DisposableServer> |
bind()
|
protected abstract Mono<? extends DisposableServer> |
bind(TcpServer b)
|
DisposableServer |
bindNow()
Start a Server in a blocking fashion, and wait for it to finish initializing.
|
DisposableServer |
bindNow(java.time.Duration timeout)
Start a Server in a blocking fashion, and wait for it to finish initializing.
|
void |
bindUntilJavaShutdown(java.time.Duration timeout,
java.util.function.Consumer<DisposableServer> onStart)
Start a Server in a fully blocking fashion, not only waiting for it to initialize
but also blocking during the full lifecycle of the client/server.
|
HttpServer |
compress()
Enable GZip response compression if the client request presents accept encoding
headers.
|
HttpServer |
compress(java.util.function.BiPredicate<HttpServerRequest,HttpServerResponse> predicate)
Enable GZip response compression if the client request presents accept encoding
headers and the passed
Predicate matches. |
HttpServer |
compress(int minResponseSize)
Enable GZip response compression if the client request presents accept encoding
headers AND the response reaches a minimum threshold
|
static HttpServer |
create()
Prepare a
HttpServer |
HttpServer |
forwarded()
Enable support for the
"Forwarded" and "X-Forwarded-*"
HTTP request headers for deriving information about the connection. |
static HttpServer |
from(TcpServer tcpServer)
Prepare a
HttpServer |
HttpServer |
handle(java.util.function.BiFunction<? super HttpServerRequest,? super HttpServerResponse,? extends Publisher<java.lang.Void>> handler)
Attach an IO handler to react on connected server
|
HttpServer |
host(java.lang.String host)
The host to which this server should bind.
|
HttpServer |
httpRequestDecoder(java.util.function.Function<HttpRequestDecoderSpec,HttpRequestDecoderSpec> requestDecoderOptions)
Configure the
HttpServerCodec's request decoding options. |
HttpServer |
noCompression()
Disable gzip compression
|
HttpServer |
noForwarded()
Disable support for the
"Forwarded" and "X-Forwarded-*"
HTTP request headers. |
HttpServer |
observe(ConnectionObserver observer)
Setup all lifecycle callbacks called on or after
each child
Channel
has been connected and after it has been disconnected. |
HttpServer |
port(int port)
The port to which this server should bind.
|
HttpServer |
protocol(HttpProtocol... supportedProtocols)
The HTTP protocol to support.
|
HttpServer |
route(java.util.function.Consumer<? super HttpServerRoutes> routesBuilder)
Define routes for the server through the provided
HttpServerRoutes builder. |
HttpServer |
secure(java.util.function.Consumer<? super SslProvider.SslContextSpec> sslProviderBuilder)
Apply an SSL configuration customization via the passed builder.
|
protected TcpServer |
tcpConfiguration()
Materialize a TcpServer from the parent
HttpServer chain to use with
bind(TcpServer) or separately |
HttpServer |
tcpConfiguration(java.util.function.Function<? super TcpServer,? extends TcpServer> tcpMapper)
Apply
ServerBootstrap configuration given mapper taking currently
configured one and returning a new one to be ultimately used for socket binding. |
HttpServer |
wiretap()
Apply a wire logger configuration using
HttpServer category
and DEBUG logger level |
public static HttpServer create()
HttpServerHttpServerpublic static HttpServer from(TcpServer tcpServer)
HttpServerHttpServerpublic final Mono<? extends DisposableServer> bind()
HttpServer and return a Mono of DisposableServer. If
Mono is cancelled, the underlying binding will be aborted. Once the DisposableServer has been emitted and is not necessary anymore, disposing main server
loop must be done by the user via DisposableChannel.dispose().
If update configuration phase fails, a Mono.error(Throwable) will be returnedMono of DisposableServerpublic final DisposableServer bindNow()
DisposableServer offers simple server API, including to DisposableChannel.disposeNow() shut it down in a blocking fashion.Connectionpublic final DisposableServer bindNow(java.time.Duration timeout)
DisposableServer offers simple server API, including to DisposableChannel.disposeNow() shut it down in a blocking fashion.timeout - max startup timeoutDisposableServerpublic final void bindUntilJavaShutdown(java.time.Duration timeout,
@Nullable
java.util.function.Consumer<DisposableServer> onStart)
Note that a JVM shutdown hook is added by
this method in order to properly disconnect the client/server upon receiving a
sigkill signal.
timeout - a timeout for server shutdownonStart - an optional callback on server startpublic final HttpServer compress()
HttpServerpublic final HttpServer compress(int minResponseSize)
minResponseSize - compression is performed once response size exceeds given
value in byteHttpServerpublic final HttpServer compress(java.util.function.BiPredicate<HttpServerRequest,HttpServerResponse> predicate)
Predicate matches.
note the passed HttpServerRequest and HttpServerResponse
should be considered read-only and the implement SHOULD NOT consume or
write the request/response in this predicate.
predicate - that returns true to compress the response.HttpServerpublic final HttpServer forwarded()
"Forwarded" and "X-Forwarded-*"
HTTP request headers for deriving information about the connection.HttpServerpublic final HttpServer host(java.lang.String host)
host - The host to bind to.HttpServerpublic final HttpServer handle(java.util.function.BiFunction<? super HttpServerRequest,? super HttpServerResponse,? extends Publisher<java.lang.Void>> handler)
handler - an IO handler that can dispose underlying connection when Publisher terminates. Only the first registered handler will subscribe to the
returned Publisher while other will immediately cancel given a same
ConnectionHttpServerpublic final HttpServer httpRequestDecoder(java.util.function.Function<HttpRequestDecoderSpec,HttpRequestDecoderSpec> requestDecoderOptions)
HttpServerCodec's request decoding options.requestDecoderOptions - a function to mutate the provided Http request decoder optionsHttpServerpublic final HttpServer noCompression()
HttpServerpublic final HttpServer noForwarded()
"Forwarded" and "X-Forwarded-*"
HTTP request headers.HttpServerpublic final HttpServer observe(ConnectionObserver observer)
Channel
has been connected and after it has been disconnected.observer - a consumer observing state changesHttpServerpublic final HttpServer protocol(HttpProtocol... supportedProtocols)
HttpProtocol.HTTP11.supportedProtocols - The various HttpProtocol this server will supportHttpServerpublic final HttpServer port(int port)
port - The port to bind to.HttpServerpublic final HttpServer secure(java.util.function.Consumer<? super SslProvider.SslContextSpec> sslProviderBuilder)
SslContext to be passed to with a default value of
10 seconds handshake timeout unless the environment property reactor.netty.tcp.sslHandshakeTimeout is set.
If SelfSignedCertificate needs to be used, the sample below can be
used. Note that SelfSignedCertificate should not be used in production.
SelfSignedCertificate cert = new SelfSignedCertificate();
SslContextBuilder sslContextBuilder =
SslContextBuilder.forServer(cert.certificate(), cert.privateKey());
secure(sslContextSpec -> sslContextSpec.sslContext(sslContextBuilder));
sslProviderBuilder - builder callback for further customization of SslContext.HttpServerpublic final HttpServer route(java.util.function.Consumer<? super HttpServerRoutes> routesBuilder)
HttpServerRoutes builder.routesBuilder - provides a route builder to be mutated in order to define routes.HttpServer starting the router on subscribepublic final HttpServer tcpConfiguration(java.util.function.Function<? super TcpServer,? extends TcpServer> tcpMapper)
ServerBootstrap configuration given mapper taking currently
configured one and returning a new one to be ultimately used for socket binding.
Configuration will apply during tcpConfiguration() phase.
tcpMapper - A tcpServer mapping function to update tcp configuration and
return an enriched tcp server to use.HttpServerpublic final HttpServer wiretap()
HttpServer category
and DEBUG logger levelHttpServerprotected abstract Mono<? extends DisposableServer> bind(TcpServer b)
b - the TcpServer to bindMono of DisposableServerprotected TcpServer tcpConfiguration()
HttpServer chain to use with
bind(TcpServer) or separatelyTcpServer