Sane C++ Libraries
C++ Platform Abstraction Libraries
SC::HttpServer Struct Reference

Async Http server. More...

#include <HttpServer.h>

Public Member Functions

 HttpServer (const HttpServer &)=delete
 
HttpServeroperator= (const HttpServer &)=delete
 
 HttpServer (HttpServer &&)=delete
 
HttpServeroperator= (HttpServer &&)=delete
 
Result start (AsyncEventLoop &loop, uint32_t maxConcurrentRequests, StringView address, uint16_t port)
 Starts the http server on the given AsyncEventLoop, address and port. More...
 
Result stopAsync ()
 Stops http server asyncronously pushing cancel and close requests for next SC::AsyncEventLoop::runOnce. More...
 
Result stopSync ()
 Stops http server synchronously waiting for SC::AsyncEventLoop::runNoWait to cancel or close all requests. More...
 
bool isStarted () const
 Check if the server is started. More...
 
HttpRequestgetRequest (ArenaMapKey< HttpServerClient > key) const
 Obtain client request (or a nullptr if it doesn't exists) with the key returned by SC::HttpResponse::getClientKey. More...
 
HttpResponsegetResponse (ArenaMapKey< HttpServerClient > key) const
 Obtain client response (or a nullptr if it doesn't exists) with the key returned by SC::HttpResponse::getClientKey. More...
 
SocketDescriptorgetSocket (ArenaMapKey< HttpServerClient > key) const
 Obtain client socket (or a nullptr if it doesn't exists) with the key returned by SC::HttpResponse::getClientKey. More...
 
uint32_t getMaxConcurrentRequests () const
 Return maximum number of concurrent requests, corresponding to size of clients arena. More...
 

Public Attributes

Function< void(HttpRequest &, HttpResponse &)> onRequest
 Called after enough data from a newly connected client has arrived, causing all headers to be parsed. More...
 

Detailed Description

Async Http server.

Usage:

See also
SC::HttpWebServer
HttpServer server;
SC_TEST_EXPECT(server.start(eventLoop, 10, "127.0.0.1", 6152));
struct ServerContext
{
int numRequests;
HttpServer& server;
} serverContext = {0, server};
server.onRequest = [this, &serverContext](HttpRequest& request, HttpResponse& response)
{
if (request.getParser().method != HttpParser::Method::HttpGET)
{
SC_TEST_EXPECT(response.startResponse(405));
SC_TEST_EXPECT(response.end(""));
return;
}
if (request.getURL() != "/index.html" and request.getURL() != "/")
{
SC_TEST_EXPECT(response.startResponse(404));
SC_TEST_EXPECT(response.end(""));
return;
}
serverContext.numRequests++;
SC_TEST_EXPECT(response.startResponse(200));
SC_TEST_EXPECT(response.addHeader("Connection", "Closed"));
SC_TEST_EXPECT(response.addHeader("Content-Type", "text/html"));
SC_TEST_EXPECT(response.addHeader("Server", "SC"));
SC_TEST_EXPECT(response.addHeader("Date", "Mon, 27 Aug 2023 16:37:00 GMT"));
SC_TEST_EXPECT(response.addHeader("Last-Modified", "Wed, 27 Aug 2023 16:37:00 GMT"));
String str;
StringBuilder sb(str);
const char sampleHtml[] = "<html>\r\n"
"<body bgcolor=\"#000000\" text=\"#ffffff\">\r\n"
"<h1>This is a title {}!</h1>\r\n"
"We must start from somewhere\r\n"
"</body>\r\n"
"</html>\r\n";
SC_TEST_EXPECT(sb.format(sampleHtml, serverContext.numRequests));
SC_TEST_EXPECT(response.end(str.view().toCharSpan()));
};
#define SC_TEST_EXPECT(e)
Records a test expectation (eventually aborting or breaking o n failed test)
Definition: Testing.h:113

Member Function Documentation

◆ getMaxConcurrentRequests()

uint32_t SC::HttpServer::getMaxConcurrentRequests ( ) const

Return maximum number of concurrent requests, corresponding to size of clients arena.

◆ getRequest()

HttpRequest * SC::HttpServer::getRequest ( ArenaMapKey< HttpServerClient >  key) const

Obtain client request (or a nullptr if it doesn't exists) with the key returned by SC::HttpResponse::getClientKey.

◆ getResponse()

HttpResponse * SC::HttpServer::getResponse ( ArenaMapKey< HttpServerClient >  key) const

Obtain client response (or a nullptr if it doesn't exists) with the key returned by SC::HttpResponse::getClientKey.

◆ getSocket()

SocketDescriptor * SC::HttpServer::getSocket ( ArenaMapKey< HttpServerClient >  key) const

Obtain client socket (or a nullptr if it doesn't exists) with the key returned by SC::HttpResponse::getClientKey.

◆ isStarted()

bool SC::HttpServer::isStarted ( ) const

Check if the server is started.

◆ start()

Result SC::HttpServer::start ( AsyncEventLoop loop,
uint32_t  maxConcurrentRequests,
StringView  address,
uint16_t  port 
)

Starts the http server on the given AsyncEventLoop, address and port.

Parameters
loopThe event loop to be used, where to add the listening socket
maxConcurrentRequestsMaximum number of concurrent requests
addressThe address of local interface where to listen to
portThe local port where to start listening to
Returns
Valid Result if http listening has been started successfully

◆ stopAsync()

Result SC::HttpServer::stopAsync ( )

Stops http server asyncronously pushing cancel and close requests for next SC::AsyncEventLoop::runOnce.

◆ stopSync()

Result SC::HttpServer::stopSync ( )

Stops http server synchronously waiting for SC::AsyncEventLoop::runNoWait to cancel or close all requests.

Member Data Documentation

◆ onRequest

Function<void(HttpRequest&, HttpResponse&)> SC::HttpServer::onRequest

Called after enough data from a newly connected client has arrived, causing all headers to be parsed.

Warning
Both references can be invalidated in later stages of the http request lifetime. If necessary, store the client key returned by SC::HttpResponse::getClientKey and use it with SC::HttpServer::getRequest, SC::HttpServer::getResponse or SC::HttpServer::getSocket

The documentation for this struct was generated from the following file: