本想找个好用方便简单高效的c++ http服务器框架,看来看去也没看到有非常合适的,后面也就懒得纠结了,用libevent自带的http server功能吧,不过看了看接口,c语言的接口用起来还是不太方便,如果用户是用c++,还要管理大量的c层面的指针,buffer,结构体等,于是就想着封装成c++形式的,而且利用c++11以后的一些特性,可以完全屏蔽c的这些细节。
原博客格式更友好:http://www.straka.cn/blog/cpp-wrapped-http-server-based-on-libevent/
如下c++ http框架、库摘自awesome-cpp: https://github.com/fffaraz/awesome-cpp#regular-expression
- ACE - An OO Network Programming Toolkit in C++. [?MIT?]
- Boost.Asio - A cross-platform C++ library for network and low-level I/O programming. [Boost]
- C++ REST SDK - C++ REST SDK (previously named Casablanca). [Apache2]
- Restbed - C++11 Asynchronous RESTful framework. [AGPL]
- Proxygen - Facebook's collection of C++ HTTP libraries including an easy to use HTTP server. [BSD]
- Muduo - A C++ non-blocking network library for multi-threaded server in Linux. [BSD]
- cpr - A modern C++ HTTP requests library with a simple but powerful interface. Modeled after the Python Requests module. [MIT] website
- Mongoose - Extremely lightweight webserver. [GPL2]
- libcurl - Multiprotocol file transfer library. [MIT/X derivate license]
- curlcpp - An object oriented C++ wrapper for CURL(libcurl). [MIT]
- Boost.Beast - HTTP and WebSocket built on Boost.Asio in C++11. [BSL-1.0] website
- Breep - Event based, high-level C++14 peer-to-peer library. [EUPL-1.1 (OSI approved)]
- libhttpserver - C++ library for creating an embedded Rest HTTP server (and more). [LGPL2.1]
- uWebSockets - µWS is one of the most lightweight, efficient & scalable WebSocket & HTTP server implementations available. [Zlib]
- restclient-cpp - Simple REST client for C++. It wraps libcurl for HTTP requests. [MIT]
- Seasocks - Simple, small, C++ embeddable webserver with WebSockets support. [BSD]
- Silicon - A high performance, middleware oriented C++14 http web framework. [MIT]
- nghttp2 - HTTP/2 C Library. [MIT] website
- Onion - HTTP server library in C designed to be lightweight and easy to use. [Apache2/GPL2]
- PicoHTTPParser - A tiny, primitive, fast HTTP request/response parser. [MIT]
- evpp - C++ high performance networking with TCP/UDP/HTTP protocols. [BSD]
- H2O - An optimized HTTP server with support for HTTP/1.x and HTTP/2. It can also be used as a library. [MIT]
- HTTP Parser - A http request/response parser for C. [MIT]
- Restinio - A header-only C++14 library that gives you an embedded HTTP/Websocket server. [BSD]
- cpp-httplib - A single file C++11 header-only HTTP/HTTPS sever library. [MIT]
- cpp-netlib - A collection of open-source libraries for high level network programming. [Boost]
我们先看下没有封装的,原生libevent的使用方法:
void http_handler(struct evhttp_request *req, void *arg) {
//get uri
const char *uri = evhttp_request_uri(req);
char *decoded_uri = evhttp_decode_uri(uri);
//get parameter
struct evkeyvalq params;
evhttp_parse_query(decoded_uri, ¶ms);
evhttp_find_header(¶ms, "key");
free(decoded_uri);
//get body
char *post_data = (char *) EVBUFFER_DATA(req->input_buffer);
...
//some other business logic
...
evhttp_add_header(req->output_headers, "Content-Type", "text/plain; charset=UTF-8");
struct evbuffer *buf = evbuffer_new();
evbuffer_add_printf(buf, "Hello world!\n%s\n", output);
evhttp_send_reply(req, HTTP_OK, "OK", buf);
evbuffer_free(buf);
}
int main(){
char * serv_addr = "0.0.0.0";
short port = 80;
int timeout = 10;
event_init();
struct evhttp *serv = evhttp_start(serv_addr, port);
evhttp_set_gencb(serv, http_handler, NULL);
...
//do some setting like evhttp_set_timeout(serv, timeout);
...
event_dispatch();
evhttp_free(serv);
return 0;
}
可以看到原生的libevent封装的c风格的http接口,用起来不是很方便,初始化步骤比较多,服务处理句柄(http_handler)必须包含strucrt evhttp_request *,处理函数内也有大量和libevent相关的结构体和方法,也就是说要使用libevent原生的http功能,需要对libevent的结构体使用有一定程度的了解,而这些c风格的接口使用起来并不友好,到处是危险的指针,还要了解libevent本身对资源的管理,否则容易造成泄漏或者空指针引用,接口本身直接暴露了内部实现,而非针对用户的使用习惯设计。
那么我就考虑将其封装为c++风格,以使其方便使用,接口用户友好,不用关心libevent的实现,而且可以完全按照c++风格编写。
对于使用者而言,一个http的服务框架,最基础的使用就是设置监听地址、端口、超时时间这些基本的,然后就是注册处理回调函数,然后编写回调函数就ok了,回调函数中能很方便的拿到http报文中的url、header、body,这就基本满足使用者需要,本文也致力于满足这样的一个需要,但不会把这个做的特别完善,对libevent已经提供的部分http功能做简单的封装,不会过多考虑http协议支持的完备性(如果大家觉得有必要,就慢慢改进吧)。
要把上述代码封装成c++风格的,总体而言没多大难度,其中一个小障碍是注册回调函数的c++风格化,要完全屏蔽libevent的底层实现,势必要把回调函数的签名给改了,那么就存在两种做法:
一种是在libevent中注册默认处理回调函数,也就是evhttp_set_gencb(serv, http_handler, NULL);,然后在http_handler中自己做路由路径的匹配,再分发到其他的处理函数中,这样回调处理函数就可以以任意自定义方式实现,但是这给封装带来了额外的工作,毕竟libevent已经带有请求路径路由了。
另一种就是采用c++ function的一些特性,把用户定义的回调函数转换成libevent支持的回调函数方法,这也就是本文的实现方法。
接下来看代码:
bool EvHttpServ::RegistHandler(std::string const &strUrl, HandlerFunc func){
if(!func){ return false; }
typedef void (*handle_t)(EvHttpRequest *);
//#TODO add middleware support
auto TransFunc = [] (struct evhttp_request *req, void *arg) {
if(NULL == req){
Utilis::LogWarn("Evhttp Request handler is NULL\n");
return;
}
EvHttpRequest httpReq(req);
try{
handle_t f=reinterpret_cast<handle_t>(arg);
f(&httpReq);
}catch(EvHttpServRTEXCP rtExcp){
if(NULL != req){ /// Judge to prevent req has already been destroied
httpReq.RespError(rtExcp.GetCode(),"Http hander throws error ");
}
}catch(std::exception e){
}
};
handle_t* pph = func.target<handle_t>();
typedef void (*func_t)(struct evhttp_request *,void *);
if(pph != nullptr ){
/// O SUCCESS,-1 ALREADY_EXIST,-2 FAILURE
return (-2 != evhttp_set_cb(evHttp_, strUrl.c_str(), TransFunc,reinterpret_cast<void*>(*pph)));
}else{
return false;
}
}
void testHandler(EvHttpRequest *req){
...
}
Serv.RegistHandler("/hi/test", testHandler);
其中利用了lambda函数完成了c++处理函数的统一,把本来用于给回调函数传递额外参数的void *arg用来传递对应的处理函数。
其余部分的封装结构还是比较简单和明确的,两个类,EvHttpServ、EvHttpRequest, 前者封装了http服务的相关工作,比如构造函数里的相关初始化工作,参数设置,另外提供了服务开启停止,处理函数注册、注销的接口。后者封装了请求本身相关的操作,毕竟同一个服务对应着并发存在的数个请求,所以分成两个类去完成。
原博客地址:
http://www.straka.cn/blog/cpp-wrapped-http-server-based-on-libevent/
代码仓库地址:
https://github.com/atp798/EvHttp