6#include "../Async/Async.h"
7#include "../AsyncStreams/AsyncRequestStreams.h"
8#include "../Foundation/Function.h"
9#include "../Foundation/StringSpan.h"
15#pragma warning(disable : 4251)
17struct SC_COMPILER_EXPORT HttpServer;
18struct SC_COMPILER_EXPORT HttpServerClient;
21struct SC_COMPILER_EXPORT HttpHeaderOffset
23 HttpParser::Token token = HttpParser::Token::Method;
57 using HttpHeaderOffset = detail::HttpHeaderOffset;
62 bool headersEndReceived =
false;
63 bool parsedSuccessfully =
true;
68 static constexpr size_t MaxNumHeaders = 64;
70 HttpHeaderOffset headerOffsets[MaxNumHeaders];
71 size_t numHeaders = 0;
100 size_t responseHeadersCapacity = 0;
102 bool headersSent =
false;
129 char debugName[16] = {0};
131 State state = State::Free;
156 IGrowableBuffer& headersMemory;
172 [[nodiscard]]
size_t getNumClients()
const {
return numClients; }
174 [[nodiscard]]
bool canAcceptMoreClients()
const {
return numClients < clients.sizeInElements(); }
175 [[nodiscard]]
bool allocateClient(
size_t& idx);
176 [[nodiscard]]
bool deallocateClient(HttpServerClient& client);
181 void closeAsync(HttpServerClient& requestClient);
183 IGrowableBuffer* headersMemory =
nullptr;
184 size_t numClients = 0;
#define SC_COMPILER_EXTERN
Define compiler-specific export macros for DLL visibility.
Definition Compiler.h:74
unsigned int uint32_t
Platform independent (4) bytes unsigned int.
Definition PrimitiveTypes.h:38
Async destination abstraction where bytes can be written to.
Definition AsyncStreams.h:266
Wraps function pointers, member functions and lambdas without ever allocating.
Definition Function.h:19
Definition HttpAsyncServer.h:12
Incremental HTTP request or response parser.
Definition HttpParser.h:17
Token
One possible Token reported by the parser.
Definition HttpParser.h:42
Http request received from a client.
Definition HttpServer.h:35
const HttpParser & getParser() const
Gets the associated HttpParser.
Definition HttpServer.h:43
StringSpan getURL() const
Gets the request URL.
Definition HttpServer.h:46
void reset()
Resets this object for it to be re-usable.
Result parse(const uint32_t maxHeaderSize, Span< const char > readData)
Parses an incoming slice of data (must be slice of availableHeader)
bool find(HttpParser::Token token, StringSpan &res) const
Finds a specific HttpParser::Result in the list of parsed header.
Http response that will be sent to a client.
Definition HttpServer.h:76
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.
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.
AsyncWritableStream & getWritableStream()
Obtain writable stream to write content.
Definition HttpServer.h:93
Definition HttpServer.h:108
Definition HttpServer.h:155
Async Http server.
Definition HttpServer.h:153
Result start(Memory &memory)
Starts the http server.
Function< void(HttpRequest &, HttpResponse &)> onRequest
Called after enough data from a newly connected client has arrived, causing all headers to be parsed.
Definition HttpServer.h:168
Uses an SC::AsyncFileWrite to stream data from a socket.
Definition AsyncRequestStreams.h:77
An ascii string used as boolean result. SC_TRY macro forwards errors to caller.
Definition Result.h:12
Low-level OS socket handle.
Definition Socket.h:153
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
Uses an SC::AsyncFileWrite to stream data to a socket.
Definition AsyncRequestStreams.h:84