🟥 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;
struct ServerContext
{
int numRequests;
HttpServer& server;
} serverContext = {0, server};
server.onRequest = [this, &serverContext](HttpRequest& request, HttpResponse& response)
{
if (request.getParser().method != HttpParser::Method::HttpGET)
{
return;
}
if (request.getURL() != "/index.html" and request.getURL() != "/")
{
return;
}
serverContext.numRequests++;
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";
};
#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
HttpServer server;
HttpWebServer webServer;
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: