EVHttpServer  1.0.0
A Lightweight http server base libevent and threadpool
Classes | Public Types | Public Member Functions | List of all members
EVHttpServer Class Reference

EVHttpServer is just an http server implemented by encapsulating libevent using c++, It provides: More...

#include <EVHttpServer.h>

Classes

struct  HttpKeyVal
 HttpKeyVal is a key-value pair used to represent the HTTP head and HTTP Query. More...
 
class  HttpReq
 HttpReq is an encapsulation class for http requests, which provides access to method, path, body, etc. It is not allowed to be created externally. Some components of the uri do not provide access functions. More...
 
class  HttpRes
 HttpRes is the encapsulation class of http response, which provides methods for setting body, head, code, etc. It is not allowed to be created externally. More...
 
struct  PathAndMethod
 Path and method are used to represent an HTTP request. More...
 

Public Types

enum  ReqType {
  REQ_GET = 1 << 0, REQ_POST = 1 << 1, REQ_HEAD = 1 << 2, REQ_PUT = 1 << 3,
  REQ_DELETE = 1 << 4, REQ_OPTIONS = 1 << 5, REQ_TRACE = 1 << 6, REQ_CONNECT = 1 << 7,
  REQ_PATCH = 1 << 8
}
 ReqType is the enumeration type of the http request method, refer to the evhttp_cmd_type type definition in libevent.
 
using ReqHandler = void(*)(const HttpReq &req, HttpRes &res, void *arg)
 

Public Member Functions

 EVHttpServer ()
 Construct EVHttpServer object.
 
virtual ~EVHttpServer ()
 destructor
 
bool init (const unsigned int port, const std::string &ip="0.0.0.0")
 initialize http server More...
 
bool init (const std::list< unsigned int > &portList, const std::string &ip="0.0.0.0")
 Initialize the http server with multiple ports. More...
 
bool start (const unsigned int threadNum=5)
 Start http server,run event dispatch thread. if threadNum > 0, the thread pool will be created and start threadNum threads. More...
 
bool stop ()
 Stop the event dispatch thread and destroy the thread pool. More...
 
bool addHandler (const PathAndMethod &reqArg, const ReqHandler &handler, void *arg=nullptr)
 Register the callback handler function corresponding to the http request, handlers can be registered both before and after the start function is called. More...
 
bool rmHandler (const PathAndMethod &reqArg)
 Remove the callback handler corresponding to the http request. More...
 
bool addRegHandler (const PathAndMethod &reqArg, const ReqHandler &handler, void *arg=nullptr)
 Add a handler for "regex path", handlers can be registered both before and after the start function is called. More...
 
bool rmRegHandler (const PathAndMethod &reqArg)
 Remove handlers for "regex path". More...
 

Detailed Description

EVHttpServer is just an http server implemented by encapsulating libevent using c++, It provides:

Member Function Documentation

◆ addHandler()

bool EVHttpServer::addHandler ( const PathAndMethod reqArg,
const ReqHandler &  handler,
void *  arg = nullptr 
)

Register the callback handler function corresponding to the http request, handlers can be registered both before and after the start function is called.

Parameters
[in]reqArg: http request parameters, including request method and path, see PathAndMethod for details
[in]handler: callback handler function
[in]arg: User-defined parameters
Return values
true: Add success
false: Add failed

◆ addRegHandler()

bool EVHttpServer::addRegHandler ( const PathAndMethod reqArg,
const ReqHandler &  handler,
void *  arg = nullptr 
)

Add a handler for "regex path", handlers can be registered both before and after the start function is called.

Parameters
[in]reqArg: http request parameters, including request method and path, see PathAndMethod for details
[in]handler: callback function
[in]arg: User-defined parameters
Return values
true: Add success
false: Add failed

◆ init() [1/2]

bool EVHttpServer::init ( const unsigned int  port,
const std::string &  ip = "0.0.0.0" 
)

initialize http server

Parameters
[in]port: http port to be listen
[in]ipIP address, default parameter "0.0.0.0"
Return values
true: success
false: failed

◆ init() [2/2]

bool EVHttpServer::init ( const std::list< unsigned int > &  portList,
const std::string &  ip = "0.0.0.0" 
)

Initialize the http server with multiple ports.

Parameters
[in]portList: http port list
[in]ipIP address, default parameter "0.0.0.0"
Return values
true: success
false: failed

◆ rmHandler()

bool EVHttpServer::rmHandler ( const PathAndMethod reqArg)

Remove the callback handler corresponding to the http request.

Parameters
[in]reqArg: http request parameters, including request method and path, see PathAndMethod for details
Return values
true: success
false: failed

◆ rmRegHandler()

bool EVHttpServer::rmRegHandler ( const PathAndMethod reqArg)

Remove handlers for "regex path".

Parameters
[in]reqArg: http request parameters, including request method and path, see PathAndMethod for details
Return values
true: success
false: failed

◆ start()

bool EVHttpServer::start ( const unsigned int  threadNum = 5)

Start http server,run event dispatch thread. if threadNum > 0, the thread pool will be created and start threadNum threads.

Parameters
[in]threadNum: When using ThreadPool, specify thenumber of threads in the thread pool
Return values
true: success
false: failed

◆ stop()

bool EVHttpServer::stop ( )

Stop the event dispatch thread and destroy the thread pool.

Return values
true: success
false: failed

The documentation for this class was generated from the following files: