Async Http Server. More...
#include <HttpAsyncServer.h>
Public Member Functions | |
| template<typename T , typename = typename TypeTraits::EnableIf<TypeTraits::IsBaseOf<HttpConnection, T>::value>::type> | |
| Result | init (Span< T > clients) |
| Initializes the async server with all needed memory buffers. | |
| template<typename T , typename = typename TypeTraits::EnableIf<TypeTraits::IsBaseOf<HttpConnection, T>::value>::type> | |
| Result | resize (Span< T > clients) |
| Result | close () |
| Closes the server, removing references to the memory buffers passed during init. | |
| Result | start (AsyncEventLoop &loop, StringSpan address, uint16_t port) |
| Starts the http server on the given AsyncEventLoop, address and port. | |
| Result | startExternal (AsyncEventLoop &loop) |
| Starts the HTTP state machine without creating a native listener. | |
| Result | acceptExternalConnection (HttpConnection &connection, AsyncReadableStream &readable, AsyncWritableStream &writable) |
| Activates a specific connection slot for externally accepted plaintext streams. | |
| Result | stop () |
| Stops http server asynchronously pushing cancel and close requests to the event loop. | |
| void | setMaxHeaderSize (uint32_t bytes) |
| Sets the maximum accepted request-header size in bytes. | |
| uint32_t | getMaxHeaderSize () const |
| Gets the maximum accepted request-header size in bytes. | |
| void | setTransportSetup (Function< Result(HttpAsyncServerTransportSetup &)> &&setup) |
| Sets an optional transport setup hook invoked after accepting TCP and before HTTP reads request bytes. | |
| void | setTransportClose (Function< void(HttpConnection &)> &&close) |
| Sets an optional transport teardown hook invoked before HTTP destroys the installed transport streams. | |
| void | setTransportReuse (Function< Result(HttpConnection &)> &&reuse) |
| Sets an optional hook that reopens an alternate writable transport for the next keep-alive response. | |
| void | setTransportShutdown (Function< Result(HttpConnection &, Function< void(Result)>)> &&shutdown) |
| Sets an optional asynchronous hook that drains a terminal response before closing its socket. | |
| void | clearTransportSetup () |
| Clears optional transport setup, reuse, shutdown, and teardown hooks. | |
| bool | isStarted () const |
| Returns true if the server has been started. | |
| const HttpConnectionsPool & | getConnections () const |
| void | setDefaultKeepAlive (bool enabled) |
| Set default keep-alive behavior for all connections. | |
| bool | getDefaultKeepAlive () const |
| Get the default keep-alive setting. | |
| void | setMaxRequestsPerConnection (uint32_t maxRequests) |
| Set maximum requests per keep-alive connection. | |
| uint32_t | getMaxRequestsPerConnection () const |
| Get the maximum requests per connection. | |
Public Attributes | |
| Function< void(HttpConnection &)> | onRequest |
| Called after enough data from a newly connected client has arrived, causing all headers to be parsed. | |
| Function< void(Result)> | onError |
| Called on accept, parse, protocol, or streaming errors before the affected connection is closed. | |
Async Http Server.
This class handles a fully asynchronous http server staying inside 5 fixed memory regions passed during init.
Usage:
| Result SC::HttpAsyncServer::acceptExternalConnection | ( | HttpConnection & | connection, |
| AsyncReadableStream & | readable, | ||
| AsyncWritableStream & | writable ) |
Activates a specific connection slot for externally accepted plaintext streams.
This must be called on the server event-loop thread. Both streams and the connection storage remain caller-owned until Http has destroyed the streams and deactivated the slot.
|
inline |
Clears optional transport setup, reuse, shutdown, and teardown hooks.
| Result SC::HttpAsyncServer::close | ( | ) |
Closes the server, removing references to the memory buffers passed during init.
|
inlinenodiscard |
Get the default keep-alive setting.
|
inlinenodiscard |
Gets the maximum accepted request-header size in bytes.
|
inlinenodiscard |
Get the maximum requests per connection.
|
inline |
Initializes the async server with all needed memory buffers.
|
inlinenodiscard |
Returns true if the server has been started.
|
inline |
Set default keep-alive behavior for all connections.
| enabled | true to keep connections alive by default (HTTP/1.1 default) |
|
inline |
Sets the maximum accepted request-header size in bytes.
The limit must fit inside the per-connection header storage passed to init().
|
inline |
Set maximum requests per keep-alive connection.
| maxRequests | Maximum requests before closing connection (0 = unlimited) |
|
inline |
Sets an optional transport teardown hook invoked before HTTP destroys the installed transport streams.
The hook may start asynchronous stream destruction but must not reset the connection's transport streams.
|
inline |
Sets an optional hook that reopens an alternate writable transport for the next keep-alive response.
|
inline |
Sets an optional transport setup hook invoked after accepting TCP and before HTTP reads request bytes.
|
inline |
Sets an optional asynchronous hook that drains a terminal response before closing its socket.
| Result SC::HttpAsyncServer::start | ( | AsyncEventLoop & | loop, |
| StringSpan | address, | ||
| uint16_t | port ) |
Starts the http server on the given AsyncEventLoop, address and port.
| loop | The event loop to be used, where to add the listening socket |
| address | The address of local interface where to listen to |
| port | The local port where to start listening to |
| Result SC::HttpAsyncServer::startExternal | ( | AsyncEventLoop & | loop | ) |
Starts the HTTP state machine without creating a native listener.
An external listener must inject accepted plaintext streams with acceptExternalConnection().
| Result SC::HttpAsyncServer::stop | ( | ) |
Stops http server asynchronously pushing cancel and close requests to the event loop.
| Function<void(Result)> SC::HttpAsyncServer::onError |
Called on accept, parse, protocol, or streaming errors before the affected connection is closed.
| Function<void(HttpConnection&)> SC::HttpAsyncServer::onRequest |
Called after enough data from a newly connected client has arrived, causing all headers to be parsed.