Sane C++ Libraries
C++ Platform Abstraction Libraries
Loading...
Searching...
No Matches
SC::HttpServer Struct Reference

Async Http server. More...

#include <HttpServer.h>

Classes

struct  Memory
 

Public Member Functions

Result start (AsyncEventLoop &loop, StringSpan address, uint16_t port, Memory &memory)
 Starts the http server on the given AsyncEventLoop, address and port.
 
Result stopAsync ()
 Stops http server asynchronously pushing cancel and close requests for next SC::AsyncEventLoop::runOnce.
 
Result stopSync ()
 Stops http server synchronously waiting for SC::AsyncEventLoop::runNoWait to cancel or close all requests.
 
bool isStarted () const
 Check if the server is started.
 

Public Attributes

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

Detailed Description

Async Http server.

Usage:

See also
SC::HttpWebServer
constexpr int NUM_CLIENTS = 3;
Buffer headersMemory;
SC_TEST_EXPECT(headersMemory.resize(NUM_CLIENTS * 8 * 1024));
Buffer requestsMemory;
SC_TEST_EXPECT(requestsMemory.resize(NUM_CLIENTS * 1024 * 2));
HttpServerClient clients[NUM_CLIENTS];
GrowableBuffer<Buffer> headers = {headersMemory};
HttpServer::Memory serverMemory = {headers, clients};
HttpServer server;
SC_TEST_EXPECT(server.start(eventLoop, "127.0.0.1", 6152, serverMemory));
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"));
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";
String str;
SC_TEST_EXPECT(StringBuilder::format(str, sampleHtml, serverContext.numRequests));
SC_TEST_EXPECT(response.end(str.view().toCharSpan()));
};

Member Function Documentation

◆ isStarted()

bool SC::HttpServer::isStarted ( ) const
inlinenodiscard

Check if the server is started.

◆ start()

Result SC::HttpServer::start ( AsyncEventLoop & loop,
StringSpan address,
uint16_t port,
Memory & memory )

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

Parameters
loopThe event loop to be used, where to add the listening socket
addressThe address of local interface where to listen to
portThe local port where to start listening to
memoryMemory buffers to be used by the http server
Returns
Valid Result if http listening has been started successfully

◆ stopAsync()

Result SC::HttpServer::stopAsync ( )

Stops http server asynchronously 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: