6#include "../AsyncStreams/AsyncStreams.h"
7#include "../Foundation/StringSpan.h"
43 [[nodiscard]]
size_t getHeadersLength()
const;
45 struct SC_COMPILER_EXPORT HttpHeaderOffset
55 bool headersEndReceived =
false;
56 bool parsedSuccessfully =
true;
61 static constexpr size_t MaxNumHeaders = 64;
63 HttpHeaderOffset headerOffsets[MaxNumHeaders];
64 size_t numHeaders = 0;
107 size_t responseHeadersCapacity = 0;
109 bool headersSent =
false;
111 bool keepAlive =
true;
112 bool keepAliveExplicitlySet =
false;
113 bool connectionHeaderAdded =
false;
123 struct SC_COMPILER_EXPORT
ID
125 size_t getIndex()
const {
return index; }
153 friend struct HttpConnectionsPool;
154 friend struct HttpAsyncServer;
156 State state = State::Inactive;
159 Span<char> headerMemory;
163template <
typename Type>
173 constexpr SpanWithStride(
void* data,
size_t sizeInElements,
size_t strideInBytes)
174 : data(data), sizeElements(sizeInElements), strideInBytes(strideInBytes)
177 template <
typename U>
179 : data(span.data()), sizeElements(span.sizeInElements()), strideInBytes(sizeof(U))
182 [[nodiscard]]
constexpr size_t sizeInElements()
const {
return sizeElements; }
183 [[nodiscard]]
constexpr bool empty()
const {
return sizeElements == 0; }
185 template <
typename U>
186 SpanWithStride<U> castTo()
188 return {
static_cast<U*
>(
reinterpret_cast<Type*
>(data)), sizeElements, strideInBytes};
191 [[nodiscard]] Type& operator[](
size_t idx)
193 return *
reinterpret_cast<Type*
>(
reinterpret_cast<char*
>(data) + idx * strideInBytes);
196 [[nodiscard]]
const Type& operator[](
size_t idx)
const
198 return *
reinterpret_cast<const Type*
>(
reinterpret_cast<const char*
>(data) + idx * strideInBytes);
204 size_t strideInBytes;
228 return connections[connectionID.index];
243 size_t numConnections = 0;
244 size_t highestActiveConnection = 0;
unsigned int uint32_t
Platform independent (4) bytes unsigned int.
Definition PrimitiveTypes.h:38
Definition AsyncStreams.h:52
Holds a Span of AsyncBufferView (allocated by user) holding available memory for the streams.
Definition AsyncStreams.h:160
Pipes read data from SC::AsyncReadableStream, forwarding them to SC::AsyncWritableStream.
Definition AsyncStreams.h:457
Async source abstraction emitting data events in caller provided byte buffers.
Definition AsyncStreams.h:211
Async destination abstraction where bytes can be written to.
Definition AsyncStreams.h:311
Async Http Server.
Definition HttpAsyncServer.h:93
Definition HttpConnection.h:124
Http connection abstraction holding both the incoming and outgoing messages in an HTTP transaction.
Definition HttpConnection.h:120
void reset()
Prepare this client for re-use, marking it as Inactive.
ID getConnectionID() const
The ID used to find this client in HttpConnectionsPool.
Definition HttpConnection.h:139
void setHeaderMemory(Span< char > memory)
Sets memory for the header.
Definition HttpConnection.h:136
A pool of HttpConnection that can be active or inactive.
Definition HttpConnection.h:209
size_t getNumActiveConnections() const
Returns only the number of active connections.
Definition HttpConnection.h:217
size_t getHighestActiveConnection() const
Returns only the number of active connections.
Definition HttpConnection.h:223
HttpConnection & getConnectionAt(size_t idx)
Returns a connection in the [0, getNumTotalConnections] range.
Definition HttpConnection.h:232
Result init(SpanWithStride< HttpConnection > connectionsStorage)
Initializes the server with memory buffers for connections and headers.
bool activateNew(HttpConnection::ID &connectionID)
Finds an available connection (if any), activates it and returns its ID to use with getConnection(id)
HttpConnection & getConnection(HttpConnection::ID connectionID)
Returns a connection by ID.
Definition HttpConnection.h:226
size_t getNumTotalConnections() const
Returns the total number of connections (active + inactive)
Definition HttpConnection.h:220
Result close()
Closes the server, removing references to the memory buffers passed during init.
bool deactivate(HttpConnection::ID connectionID)
De-activates a connection previously returned by activateNew.
Incremental HTTP request or response parser.
Definition HttpParser.h:14
Token
One possible Token reported by the parser.
Definition HttpParser.h:40
Incoming message from the perspective of the participants of an HTTP transaction.
Definition HttpConnection.h:16
const HttpParser & getParser() const
Gets the associated HttpParser.
Definition HttpConnection.h:18
StringSpan getURL() const
Gets the request URL.
Definition HttpConnection.h:21
void reset()
Resets this object for it to be re-usable.
Outgoing message from the perspective of the participants of an HTTP transaction.
Definition HttpConnection.h:69
Result startResponse(int httpCode)
Starts the response with a http standard code (200 OK, 404 NOT FOUND etc.)
Result sendHeaders()
Start sending response headers, before sending any data.
void setKeepAlive(bool keepAlive)
Sets whether to keep the connection alive after this response.
Result addHeader(StringSpan headerName, StringSpan headerValue)
Writes an http header to this response.
Result end()
Finalizes the writable stream after sending all in progress writes.
void reset()
Resets this object for it to be re-usable.
bool isKeepAliveExplicitlySet() const
Returns true if setKeepAlive was explicitly called.
Definition HttpConnection.h:97
AsyncWritableStream & getWritableStream()
Obtain writable stream for sending content back to connected client.
Definition HttpConnection.h:86
bool getKeepAlive() const
Gets whether the connection should be kept alive after this response.
Definition HttpConnection.h:94
An ascii string used as boolean result. SC_TRY macro forwards errors to caller.
Definition Result.h:13
View over a contiguous sequence of items with a custom stride between elements.
Definition HttpConnection.h:165
constexpr SpanWithStride(void *data, size_t sizeInElements, size_t strideInBytes)
Builds a SpanWithStride from data, size, and stride.
Definition HttpConnection.h:173
constexpr SpanWithStride()
Builds an empty SpanWithStride.
Definition HttpConnection.h:167
View over a contiguous sequence of items (pointer + size in elements).
Definition Span.h:29
An read-only view over a string (to avoid including Strings library when parsing is not needed).
Definition StringSpan.h:37