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)});
109 SC_TRY(checkTransportMutation(owner));
110 transportSetup = move(setup);
118 SC_TRY(checkTransportMutation(owner));
119 transportClose = move(close);
126 SC_TRY(checkTransportMutation(owner));
127 transportReuse = move(reuse);
133 const void* owner =
nullptr)
135 SC_TRY(checkTransportMutation(owner));
136 transportShutdown = move(shutdown);
153 [[nodiscard]]
bool isStarted()
const {
return state == State::Started; }
179 Result checkTransportMutation(
const void* owner)
const;
180 Result checkTransportQuiescence()
const;
184 uint32_t maxHeaderSize = 8 * 1024;
188 Function<Result(
HttpConnection&, Function<
void(Result)>)> transportShutdown;
191 const void* transportOwner =
nullptr;
193 uint32_t transportCallbackDepth = 0;
202 State state = State::Stopped;
204 bool defaultKeepAlive =
true;
205 uint32_t maxRequestsPerConnection = 0;
207 void onNewClient(AsyncSocketAccept::Result& result);
208 void closeAsync(HttpConnection& requestClient);
209 void shutdownTransport(HttpConnection& requestClient);
210 void tryDeactivateConnection(HttpConnection& requestClient);
211 void deactivateConnection(HttpConnection& requestClient);
212 Result beginHttpConnection(HttpConnection& client);
213 Result beginTransportConnection(HttpConnection& client);
214 void onStreamReceive(HttpConnection& client, AsyncBufferView::ID bufferID);
215 void onRequestBodyData(HttpConnection& client, AsyncBufferView::ID bufferID);
216 void onTransportSetupComplete(HttpConnection& client, Result result);
218 Result waitForStopToFinish();
219 Result initInternal(SpanWithStride<HttpConnection> connections);
220 Result resizeInternal(SpanWithStride<HttpConnection> connections);
222 AsyncEventLoop* eventLoop =
nullptr;
223 SocketDescriptor serverSocket;
224 AsyncSocketAccept asyncServerAccept;
225 bool externalListener =
false;
227 struct EventDataListener;
228 struct EventBodyDataListener;
229 struct EventEndListener;
230 struct EventCloseListener;
Asynchronous I/O (files, sockets, timers, processes, fs events, threads wake-up) (see Async) AsyncEve...
Definition Async.h:1521
Async source abstraction emitting data events in caller provided byte buffers.
Definition AsyncStreams.h:229
Async destination abstraction where bytes can be written to.
Definition AsyncStreams.h:365
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
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:176
Result setTransportClose(Function< void(HttpConnection &)> &&close, const void *owner=nullptr)
Sets an optional transport teardown hook invoked before HTTP destroys the installed transport streams...
Definition HttpAsyncServer.h:116
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
Result releaseTransport(const void *owner)
Clears hooks and releases only the matching owner's claim after complete server teardown.
Result clearTransportSetup(const void *owner=nullptr)
Clears optional transport setup, reuse, shutdown, and teardown hooks.
bool getDefaultKeepAlive() const
Get the default keep-alive setting.
Definition HttpAsyncServer.h:169
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:166
Function< void(HttpConnection &)> onRequest
Called after enough data from a newly connected client has arrived, causing all headers to be parsed.
Definition HttpAsyncServer.h:158
Function< void(Result)> onError
Called on accept, parse, protocol, or streaming errors before the affected connection is closed.
Definition HttpAsyncServer.h:161
void setMaxRequestsPerConnection(uint32_t maxRequests)
Set maximum requests per keep-alive connection.
Definition HttpAsyncServer.h:173
bool isStarted() const
Returns true if the server has been started.
Definition HttpAsyncServer.h:153
void setMaxHeaderSize(uint32_t bytes)
Sets the maximum accepted request-header size in bytes.
Definition HttpAsyncServer.h:101
uint32_t getMaxHeaderSize() const
Gets the maximum accepted request-header size in bytes.
Definition HttpAsyncServer.h:104
Result setTransportShutdown(Function< Result(HttpConnection &, Function< void(Result)>)> &&shutdown, const void *owner=nullptr)
Sets an optional asynchronous hook that drains a terminal response before closing its socket.
Definition HttpAsyncServer.h:132
Result setTransportSetup(Function< Result(HttpAsyncServerTransportSetup &)> &&setup, const void *owner=nullptr)
Sets an optional transport setup hook invoked after accepting TCP and before HTTP reads request bytes...
Definition HttpAsyncServer.h:107
Result acceptExternalConnection(HttpConnection &connection, AsyncReadableStream &readable, AsyncWritableStream &writable)
Activates a specific connection slot for externally accepted plaintext streams.
Result setTransportReuse(Function< Result(HttpConnection &)> &&reuse, const void *owner=nullptr)
Sets an optional hook that reopens an alternate writable transport for the next keep-alive response.
Definition HttpAsyncServer.h:124
Result claimTransport(const void *owner)
Claims the transport hook group with a non-null address-stable owner token.
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:489
A pool of HttpConnection that can be active or inactive.
Definition HttpConnection.h:588
Adds compile-time configurable read and write queues to any class subclassing HttpConnectionBase.
Definition HttpConnection.h:656
IsBaseOf evaluates to true if the type Base is a base class of Derived, false otherwise.
Definition HttpAsyncServer.h:14