Sane C++ Libraries
C++ Platform Abstraction Libraries
Http

🟥 HTTP parser, client and server

Http library contains a hand-written http 1.1 parser, client and server.

Features

  • HTTP 1.1 Parser
  • HTTP 1.1 Client
  • HTTP 1.1 Server

Status

🟥 Draft
In current state the library is able to host simple static website but it cannot be used for any internet facing application.

Description

The HTTP parser is an incremental parser, that will emit events as soon as a valid element has been successfully parsed. This allows handling incomplete responses without needing holding it entirely in memory.

The HTTP client and server are for now just some basic implementations and are missing some important feature.

HttpServer

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

HttpWebServer

Http web server helps statically serves files from a directory.
It can be used in conjunction with SC::HttpServer, by calling SC::HttpWebServer::serveFile inside the SC::HttpServer::onRequest callback to statically serve files.

See also
SC::HttpServer
// Creates an HttpServer that serves files from application root directory
HttpServer server;
HttpWebServer webServer;
SC_TEST_EXPECT(server.start(eventLoop, 16, "127.0.0.1", 8090));
SC_TEST_EXPECT(webServer.init(report.applicationRootDirectory));
server.onRequest = [&](HttpRequest& req, HttpResponse& res) { webServer.serveFile(req, res); };

HttpClient

Http async client.

Examples

Roadmap

🟨 MVP

  • Server+Client: Support mostly used HTTP verbs / methods
  • Server+Client: HTTP 1.1 Chunked Encoding

🟩 Usable Features:

🟦 Complete Features:

  • HTTPS
  • Support all HTTP verbs / methods

💡 Unplanned Features:

  • Http 2.0
  • Http 3.0