Simple HTTP Server using proxygen
In my previous article I talked about how to make the most simple HTTP client. This article will be about making the most simple HTTP server. In order to get most out of this article I suggest referencing code as you read. The code is available here:
https://gitlab.com/technologysoup/starter-kits/hello-proxygen/-/tree/main/src/echo_server
In order to get a working HTTP server we need only a handful of components:
HTTPServer(obviously)HTTPOptionsRequestHandlerandRequestHandlerFactory
And that is really it.
HTTPOptions object is used to configure HTTPServer. Settings you can change range from thing like number of threads used, idle timeout, TCP flow connection parameters, content compression, etc. To get most basic server working we only need to register a single request handler:
HTTPServerOptions options;
options.handlerFactories =
RequestHandlerChain().addThen<EchoHandlerFactory>().build();EchoHandlerFactory is an object that implements RequestHandlerFactory interface. This handler factory will be called every time a request is received and its job is to return an object to handle the request. In this case an EchoHandler.
EchoHandler implements RequestHandler interface. RequestHandler exposes various hooks in the request/response processing chain. The ones that are of interest to us, at this point, are:
onRequest -fires when all headers are receivedonBody -fires every time a part of body content is receivedonEOM -fires when whole body has been received
In order to start a server we will also need to tell it to which addresses to bind. to. HTTPServer::IPConfig structure is used to do that:
using IPConfig = HTTPServer::IPConfig
using Protocol = HTTPServer::Protocol
using vec = std::vector
vec<IPConfig> IPs = {
{SocketAddress("::1", 4567, true), Protocol::HTTP},
};Now we are ready to start the server:
HTTPServer server(std::move(options));
server.bind(IPs);
std::thread t([&]() { server.start(); });
Now you can compile the program and run it. You can than use curl or any other utility to test it.
In my next article I plan to talk some more about running a multi threaded server and libevent.
If there is something specific that you’d like to learn more about please let me know in the comments below.
Lastly, if you learned something useful please share this post and consider subscribing to get notified when my next article comes out. I try to publish on a weekly / bi-weely basis - depending on the topic and amount of “free” time I have.

