Skip to content

Facebook

Proxygen – Getting started with Facebook’s HTTP Server

So recently I have been writing a tool for querying data from the fog. I needed a high thru put ingest engine that could handle a large amount of data while synchronizing ad-hoc data pipes (I know that sounds like buzz word drivel but it is what I needed). Since I have been courting her for a while, I thought I would finally ask Proxygen out for coffee to help me solve this problem. After we went out for our cup of coffee, I was saddened to learn that the Proxygen is a closed book of opensource software. The code is there free to read but to say that the documentation is lacking would be generous. Folly, a Proxygen dependency has a nice level of documentation here. Realistically, the examples and the doxygen docs are enough to get started.

I already wrote a post on getting started with a Proxygen HTTP Client. Now this one is about getting the server up and running. There is an example in the Proxygen documentation samples echo server. The echo example falls short in describing how to customize your own server implementation. As I describe rolling your own server, I will be leaving out specifics to my employers domain. That should leave some easily filled gaps for the reader. The ultimate goal is to have a ASP.NET style web api and a file server that I can use to create a full web solution. To create an easy API, the pieces of Proxygen must be utilized properly.

Server

In the echo server examples, there is an EchoServer.cpp source file that showcases how to setup a Proxygen http server. In this file, utilities are setup and the server is launched. Most of the file reads easily enough but the current goal is customization. To customize the execution pipeline, a handler factory must be placed in the Request handler chain found:

options.handlerFactories = RequestHandlerChain()
      .addThen<EchoHandlerFactory>()
      .build();

In the code above, replace “EchoHandlerFactory” with an HandlerFactory that fits the needs of the project. The next section has an overview of custom HandlerFactory creation.

Handler Factory

Handler factories allow for the creation of handlers (explained below). Each request will go through the handler factory to create a handler for that request. To treat this like ASP.NET, the Handler Factory works like the route config and the dependency injector all in one. With the EchoServer example, the EchoHandlerFactory is in the EchoServer.cpp source file. The EchoHandlerFactory handles the dependency resolution and if routing was available could use the HTTPMessage* to get the path and return the appropriate handler.

Handler

The Handler is going to handle the HTTP lifecycle and return an appropriate response. The Echo Handler sample has the handler source located in EchoHandler.cpp. In it, the HTTP lifecycle is handled. In it there are a few methods to make note of:

  • onRequest – This method can be used to determine authorization and authentication to the web resources. In it, the headers for the request are passed.
  • onBody – This is where the folly::IOBuf is built for the request. The buffer will be appended to during each read of the request and can be converted into a folly::string and then to a std::string.
  • onEOM – This is the method where you can respond to the request. The example shows how to us ResponseBuilder to put together an Ok response to the request.

 

Proxygen – Creating an HTTP Client

So in my newest endeavor I am using a Proxygen server to maximize efficiency and to allow for code sharing between the server project and the embedded projects. One of the first hurdles to overcome was getting the embedded devices a way to communicate with the server using Proxygen’s httpclient facilities. There is an example in the documentation that needs some love. I took that example and broke it out into a useful client. I needed two classes to make this happen:

  1. The curl client that would handle the lifecycle events of the http request.
  2. the http client that would create the easy to use interface for http requests.

NOTE: I am not an expert with the proxygen library. I am not an expert at C++. This is just a bit of code I got working the other night that I thought I would share.

If you have trouble compiling, check out this stack overflow post.

Twitter Auto Publish Powered By : XYZScripts.com