4#include "HttpConnection.h"
12template <
typename Base,
typename Derived>
15 static constexpr bool value = __is_base_of(Base, Derived);
21template <
int ReadQueue,
int WriteQueue,
int HeaderBytes,
int StreamBytes>
23 :
public HttpStaticConnection<ReadQueue, WriteQueue, HeaderBytes, StreamBytes, 8, HttpConnection>
37 Function<void(Result)> complete;
38 Function<void(Result)> fail;
57 typename =
typename TypeTraits::EnableIf<TypeTraits::IsBaseOf<HttpConnection, T>::value>::type>
58 Result
init(Span<T> clients)
60 return initInternal({clients.data(), clients.sizeInElements(),
sizeof(T)});
64 typename =
typename TypeTraits::EnableIf<TypeTraits::IsBaseOf<HttpConnection, T>::value>::type>
65 Result resize(Span<T> clients)
67 return resizeInternal({clients.data(), clients.sizeInElements(),
sizeof(T)});
117 transportShutdown = move(shutdown);
125 transportShutdown = {};
130 [[nodiscard]]
bool isStarted()
const {
return state == State::Started; }
158 uint32_t maxHeaderSize = 8 * 1024;
162 Function<Result(
HttpConnection&, Function<
void(Result)>)> transportShutdown;
172 State state = State::Stopped;
174 bool defaultKeepAlive =
true;
175 uint32_t maxRequestsPerConnection = 0;
177 void onNewClient(AsyncSocketAccept::Result& result);
178 void closeAsync(HttpConnection& requestClient);
179 void shutdownTransport(HttpConnection& requestClient);
180 void tryDeactivateConnection(HttpConnection& requestClient);
181 void deactivateConnection(HttpConnection& requestClient);
182 Result beginHttpConnection(HttpConnection& client);
183 Result beginTransportConnection(HttpConnection& client);
184 void onStreamReceive(HttpConnection& client, AsyncBufferView::ID bufferID);
185 void onRequestBodyData(HttpConnection& client, AsyncBufferView::ID bufferID);
186 void onTransportSetupComplete(HttpConnection& client, Result result);
188 Result waitForStopToFinish();
189 Result initInternal(SpanWithStride<HttpConnection> connections);
190 Result resizeInternal(SpanWithStride<HttpConnection> connections);
192 AsyncEventLoop* eventLoop =
nullptr;
193 SocketDescriptor serverSocket;
194 AsyncSocketAccept asyncServerAccept;
195 bool externalListener =
false;
197 struct EventDataListener;
198 struct EventBodyDataListener;
199 struct EventEndListener;
200 struct EventCloseListener;
Asynchronous I/O (files, sockets, timers, processes, fs events, threads wake-up) (see Async) AsyncEve...
Definition Async.h:1506
Async source abstraction emitting data events in caller provided byte buffers.
Definition AsyncStreams.h:228
Async destination abstraction where bytes can be written to.
Definition AsyncStreams.h:364
Adds compile-time configurable read and write queues to HttpConnection.
Definition HttpAsyncServer.h:24
Mutable transport setup view used by HttpAsyncServer after accepting a TCP socket.
Definition HttpAsyncServer.h:33
Async Http Server.
Definition HttpAsyncServer.h:54
void setTransportShutdown(Function< Result(HttpConnection &, Function< void(Result)>)> &&shutdown)
Sets an optional asynchronous hook that drains a terminal response before closing its socket.
Definition HttpAsyncServer.h:115
void setTransportSetup(Function< Result(HttpAsyncServerTransportSetup &)> &&setup)
Sets an optional transport setup hook invoked after accepting TCP and before HTTP reads request bytes...
Definition HttpAsyncServer.h:105
Result start(AsyncEventLoop &loop, StringSpan address, uint16_t port)
Starts the http server on the given AsyncEventLoop, address and port.
uint32_t getMaxRequestsPerConnection() const
Get the maximum requests per connection.
Definition HttpAsyncServer.h:153
Result stop()
Stops http server asynchronously pushing cancel and close requests to the event loop.
Result init(Span< T > clients)
Initializes the async server with all needed memory buffers.
Definition HttpAsyncServer.h:58
bool getDefaultKeepAlive() const
Get the default keep-alive setting.
Definition HttpAsyncServer.h:146
Result startExternal(AsyncEventLoop &loop)
Starts the HTTP state machine without creating a native listener.
void setDefaultKeepAlive(bool enabled)
Set default keep-alive behavior for all connections.
Definition HttpAsyncServer.h:143
Function< void(HttpConnection &)> onRequest
Called after enough data from a newly connected client has arrived, causing all headers to be parsed.
Definition HttpAsyncServer.h:135
Function< void(Result)> onError
Called on accept, parse, protocol, or streaming errors before the affected connection is closed.
Definition HttpAsyncServer.h:138
void setMaxRequestsPerConnection(uint32_t maxRequests)
Set maximum requests per keep-alive connection.
Definition HttpAsyncServer.h:150
bool isStarted() const
Returns true if the server has been started.
Definition HttpAsyncServer.h:130
void setMaxHeaderSize(uint32_t bytes)
Sets the maximum accepted request-header size in bytes.
Definition HttpAsyncServer.h:99
uint32_t getMaxHeaderSize() const
Gets the maximum accepted request-header size in bytes.
Definition HttpAsyncServer.h:102
void setTransportReuse(Function< Result(HttpConnection &)> &&reuse)
Sets an optional hook that reopens an alternate writable transport for the next keep-alive response.
Definition HttpAsyncServer.h:112
void clearTransportSetup()
Clears optional transport setup, reuse, shutdown, and teardown hooks.
Definition HttpAsyncServer.h:121
void setTransportClose(Function< void(HttpConnection &)> &&close)
Sets an optional transport teardown hook invoked before HTTP destroys the installed transport streams...
Definition HttpAsyncServer.h:109
Result acceptExternalConnection(HttpConnection &connection, AsyncReadableStream &readable, AsyncWritableStream &writable)
Activates a specific connection slot for externally accepted plaintext streams.
Result close()
Closes the server, removing references to the memory buffers passed during init.
Http connection abstraction holding both the incoming and outgoing messages in an HTTP transaction.
Definition HttpConnection.h:490
A pool of HttpConnection that can be active or inactive.
Definition HttpConnection.h:589
Adds compile-time configurable read and write queues to any class subclassing HttpConnectionBase.
Definition HttpConnection.h:657
IsBaseOf evaluates to true if the type Base is a base class of Derived, false otherwise.
Definition HttpAsyncServer.h:14