6#include "../Containers/ArenaMapKey.h"
7#include "../Foundation/Function.h"
8#include "../Strings/SmallString.h"
18struct SocketDescriptor;
19struct HttpServerClient;
61 using HttpHeaderOffset = detail::HttpHeaderOffset;
63 bool headersEndReceived =
false;
64 bool parsedSuccessfully =
true;
95 [[nodiscard]]
bool mustBeFlushed()
const {
return responseEnded or outputBuffer.size() > highwaterMark; }
97 HttpServer* server =
nullptr;
99 ArenaMapKey<HttpServerClient> key;
101 Vector<char> outputBuffer;
103 bool responseEnded =
false;
104 size_t highwaterMark = 1024;
170#if SC_PLATFORM_WINDOWS
#define SC_COMPILER_EXPORT
Macro for symbol visibility in non-MSVC compilers.
Definition: Compiler.h:78
#define SC_COMPILER_EXTERN
Define compiler-specific export macros for DLL visibility.
Definition: Compiler.h:74
unsigned long long uint64_t
Platform independent (8) bytes unsigned int.
Definition: PrimitiveTypes.h:42
unsigned int uint32_t
Platform independent (4) bytes unsigned int.
Definition: PrimitiveTypes.h:38
unsigned short uint16_t
Platform independent (2) bytes unsigned int.
Definition: PrimitiveTypes.h:37
A sparse vector keeping objects at a stable memory location.
Definition: ArenaMapKey.h:22
Asynchronous I/O (files, sockets, timers, processes, fs events, threads wake-up) (see Async) AsyncEve...
Definition: Async.h:933
Wraps function pointers, member functions and lambdas without ever allocating.
Definition: Function.h:50
Incremental HTTP request or response parser.
Definition: HttpParser.h:17
Result
One possible Result reported by the parser.
Definition: HttpParser.h:42
@ Method
Http method has been found.
Http request received from a client.
Definition: HttpServer.h:46
const HttpParser & getParser() const
Gets the associated HttpParser.
Definition: HttpServer.h:54
bool find(HttpParser::Result result, StringView &res) const
Finds a specific HttpParser::Result in the list of parsed header.
StringView getURL() const
Gets the request URL.
Definition: HttpServer.h:57
Http response that will be sent to a client.
Definition: HttpServer.h:75
Result startResponse(int httpCode)
Starts the response with a http standard code (200 OK, 404 NOT FOUND etc.)
Result addHeader(StringView headerName, StringView headerValue)
Writes an http header to this response.
Result write(Span< const char > data)
Appends some data to the response.
Result end(Span< const char > data)
Finalizes response appending some data.
Async Http server.
Definition: HttpServer.h:122
Result stopAsync()
Stops http server asyncronously pushing cancel and close requests for next SC::AsyncEventLoop::runOnc...
HttpRequest * getRequest(ArenaMapKey< HttpServerClient > key) const
Obtain client request (or a nullptr if it doesn't exists) with the key returned by SC::HttpResponse::...
uint32_t getMaxConcurrentRequests() const
Return maximum number of concurrent requests, corresponding to size of clients arena.
SocketDescriptor * getSocket(ArenaMapKey< HttpServerClient > key) const
Obtain client socket (or a nullptr if it doesn't exists) with the key returned by SC::HttpResponse::g...
Result stopSync()
Stops http server synchronously waiting for SC::AsyncEventLoop::runNoWait to cancel or close all requ...
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:151
bool isStarted() const
Check if the server is started.
Result start(AsyncEventLoop &loop, uint32_t maxConcurrentRequests, StringView address, uint16_t port)
Starts the http server on the given AsyncEventLoop, address and port.
HttpResponse * getResponse(ArenaMapKey< HttpServerClient > key) const
Obtain client response (or a nullptr if it doesn't exists) with the key returned by SC::HttpResponse:...
An ascii string used as boolean result. SC_TRY macro forwards errors to caller.
Definition: Result.h:11
Low-level OS socket handle.
Definition: SocketDescriptor.h:154
View over a contiguous sequence of items (pointer + size in elements).
Definition: Span.h:20
Non-owning view over a range of characters with UTF Encoding.
Definition: StringView.h:47
A contiguous sequence of heap allocated elements.
Definition: Vector.h:51