1 #ifndef _EVHTTP_SERVER_H_ 2 #define _EVHTTP_SERVER_H_ 5 #include <unordered_map> 19 #define USE_LINUX_REGEX_API (1) 22 #define USE_LINUX_REGEX_API (0) 24 #include <sys/types.h> 40 struct evhttp_request;
95 std::string methodStr()
const;
96 std::string path()
const;
97 std::string body()
const;
98 std::unique_ptr<char[]> bodyRaw()
const;
99 std::string uri()
const;
100 std::string host()
const;
102 void headers(std::list<HttpKeyVal> & ret)
const;
103 bool findHeader(
const std::string & key, std::string & value)
const;
105 void querys(std::list<HttpKeyVal> & ret)
const;
106 bool findQuery(
const std::string & key, std::string & value)
const;
111 struct evhttp_request * m_request =
nullptr;
123 bool setBody(
const std::string & body);
125 bool addHeaders(
const std::list<HttpKeyVal> & list);
126 bool setHeaders(
const std::list<HttpKeyVal> & list);
128 void setCode(
const int & code);
129 void setReason(
const std::string & reason);
135 int getCode(
void)
const;
136 const char * getReason(
void)
const;
139 std::string m_reason;
140 struct evhttp_request * m_request =
nullptr;
141 bool m_initBody =
false;
145 using ReqHandler = void (*)(
const HttpReq & req,
HttpRes & res,
void * arg);
161 return ((this->method == info.method) && (this->path == info.path));
170 bool init(
const unsigned int port,
const std::string & ip =
"0.0.0.0");
171 bool init(
const std::list<unsigned int> & portList,
const std::string & ip =
"0.0.0.0");
172 bool start(
const unsigned int threadNum = 5);
180 static void handleHttpEvent(
struct evhttp_request * request,
void * arg);
181 static void * dispatchThread(
void * arg);
183 struct PathAndMethodHash
187 std::string key = std::to_string(t.method) + t.path;
188 std::hash<std::string> strHash;
200 #if USE_LINUX_REGEX_API 205 CallBackBind callbackBind;
208 static void dealTask(
struct evhttp_request * request,
const PathAndMethod & reqArg,
const CallBackBind & handleBind);
210 std::thread * m_thread =
nullptr;
211 volatile bool m_isInited =
false;
212 volatile bool m_isRunning =
false;
213 event_base * m_base =
nullptr;
214 std::list<void *> m_evhttpList;
216 std::unordered_map<PathAndMethod, CallBackBind, PathAndMethodHash> m_handlerMap;
217 std::list<RegNode> m_regList;
bool start(const unsigned int threadNum=5)
Start http server,run event dispatch thread. if threadNum > 0, the thread pool will be created and s...
Definition: EVHttpServer.cpp:190
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 i...
Definition: EVHttpServer.cpp:469
ReqType
ReqType is the enumeration type of the http request method, refer to the evhttp_cmd_type type definit...
Definition: EVHttpServer.h:63
bool rmRegHandler(const PathAndMethod &reqArg)
Remove handlers for "regex path".
Definition: EVHttpServer.cpp:530
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 ...
Definition: EVHttpServer.cpp:239
EVHttpServer is just an http server implemented by encapsulating libevent using c++, It provides:
Definition: EVHttpServer.h:56
bool stop()
Stop the event dispatch thread and destroy the thread pool.
Definition: EVHttpServer.cpp:389
Definition: ThreadPool.h:14
bool init(const unsigned int port, const std::string &ip="0.0.0.0")
initialize http server
Definition: EVHttpServer.cpp:34
bool rmHandler(const PathAndMethod &reqArg)
Remove the callback handler corresponding to the http request.
Definition: EVHttpServer.cpp:270
HttpKeyVal is a key-value pair used to represent the HTTP head and HTTP Query.
Definition: EVHttpServer.h:79
HttpRes is the encapsulation class of http response, which provides methods for setting body...
Definition: EVHttpServer.h:120
EVHttpServer()
Construct EVHttpServer object.
Definition: EVHttpServer.cpp:23
Path and method are used to represent an HTTP request.
Definition: EVHttpServer.h:150
virtual ~EVHttpServer()
destructor
Definition: EVHttpServer.cpp:446
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.
Definition: EVHttpServer.h:91