CivetServer.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509
  1. /* Copyright (c) 2013-2014 the Civetweb developers
  2. * Copyright (c) 2013 No Face Press, LLC
  3. *
  4. * License http://opensource.org/licenses/mit-license.php MIT License
  5. */
  6. #ifndef _CIVETWEB_SERVER_H_
  7. #define _CIVETWEB_SERVER_H_
  8. #ifdef __cplusplus
  9. #include "civetweb.h"
  10. #include <map>
  11. #include <string>
  12. #include <vector>
  13. #include <stdexcept>
  14. // forward declaration
  15. class CivetServer;
  16. /**
  17. * Exception class for thrown exceptions within the CivetHandler object.
  18. */
  19. class CIVETWEB_API CivetException : public std::runtime_error
  20. {
  21. public:
  22. CivetException(const std::string &msg) : std::runtime_error(msg)
  23. {
  24. }
  25. };
  26. /**
  27. * Basic interface for a URI request handler. Handlers implementations
  28. * must be reentrant.
  29. */
  30. class CIVETWEB_API CivetHandler
  31. {
  32. public:
  33. /**
  34. * Destructor
  35. */
  36. virtual ~CivetHandler()
  37. {
  38. }
  39. /**
  40. * Callback method for GET request.
  41. *
  42. * @param server - the calling server
  43. * @param conn - the connection information
  44. * @returns true if implemented, false otherwise
  45. */
  46. virtual bool handleGet(CivetServer *server, struct mg_connection *conn);
  47. /**
  48. * Callback method for POST request.
  49. *
  50. * @param server - the calling server
  51. * @param conn - the connection information
  52. * @returns true if implemented, false otherwise
  53. */
  54. virtual bool handlePost(CivetServer *server, struct mg_connection *conn);
  55. /**
  56. * Callback method for PUT request.
  57. *
  58. * @param server - the calling server
  59. * @param conn - the connection information
  60. * @returns true if implemented, false otherwise
  61. */
  62. virtual bool handlePut(CivetServer *server, struct mg_connection *conn);
  63. /**
  64. * Callback method for DELETE request.
  65. *
  66. * @param server - the calling server
  67. * @param conn - the connection information
  68. * @returns true if implemented, false otherwise
  69. */
  70. virtual bool handleDelete(CivetServer *server, struct mg_connection *conn);
  71. /**
  72. * Callback method for OPTIONS request.
  73. *
  74. * @param server - the calling server
  75. * @param conn - the connection information
  76. * @returns true if implemented, false otherwise
  77. */
  78. virtual bool handleOptions(CivetServer *server, struct mg_connection *conn);
  79. /**
  80. * Callback method for PATCH request.
  81. *
  82. * @param server - the calling server
  83. * @param conn - the connection information
  84. * @returns true if implemented, false otherwise
  85. */
  86. virtual bool handlePatch(CivetServer *server, struct mg_connection *conn);
  87. };
  88. /**
  89. * Basic interface for a websocket handler. Handlers implementations
  90. * must be reentrant.
  91. */
  92. class CIVETWEB_API CivetWebSocketHandler
  93. {
  94. public:
  95. /**
  96. * Destructor
  97. */
  98. virtual ~CivetWebSocketHandler()
  99. {
  100. }
  101. /**
  102. * Callback method for when the client intends to establish a websocket
  103. *connection, before websocket handshake.
  104. *
  105. * @param server - the calling server
  106. * @param conn - the connection information
  107. * @returns true to keep socket open, false to close it
  108. */
  109. virtual bool handleConnection(CivetServer *server,
  110. const struct mg_connection *conn);
  111. /**
  112. * Callback method for when websocket handshake is successfully completed,
  113. *and connection is ready for data exchange.
  114. *
  115. * @param server - the calling server
  116. * @param conn - the connection information
  117. */
  118. virtual void handleReadyState(CivetServer *server,
  119. struct mg_connection *conn);
  120. /**
  121. * Callback method for when a data frame has been received from the client.
  122. *
  123. * @param server - the calling server
  124. * @param conn - the connection information
  125. * @bits: first byte of the websocket frame, see websocket RFC at
  126. *http://tools.ietf.org/html/rfc6455, section 5.2
  127. * @data, data_len: payload, with mask (if any) already applied.
  128. * @returns true to keep socket open, false to close it
  129. */
  130. virtual bool handleData(CivetServer *server,
  131. struct mg_connection *conn,
  132. int bits,
  133. char *data,
  134. size_t data_len);
  135. /**
  136. * Callback method for when the connection is closed.
  137. *
  138. * @param server - the calling server
  139. * @param conn - the connection information
  140. */
  141. virtual void handleClose(CivetServer *server,
  142. const struct mg_connection *conn);
  143. };
  144. /**
  145. * CivetServer
  146. *
  147. * Basic class for embedded web server. This has an URL mapping built-in.
  148. */
  149. class CIVETWEB_API CivetServer
  150. {
  151. public:
  152. /**
  153. * Constructor
  154. *
  155. * This automatically starts the sever.
  156. * It is good practice to call getContext() after this in case there
  157. * were errors starting the server.
  158. *
  159. * @param options - the web server options.
  160. * @param callbacks - optional web server callback methods.
  161. *
  162. * @throws CivetException
  163. */
  164. CivetServer(const char **options, const struct mg_callbacks *callbacks = 0);
  165. CivetServer(std::vector<std::string> options,
  166. const struct mg_callbacks *callbacks = 0);
  167. /**
  168. * Destructor
  169. */
  170. virtual ~CivetServer();
  171. /**
  172. * close()
  173. *
  174. * Stops server and frees resources.
  175. */
  176. void close();
  177. /**
  178. * getContext()
  179. *
  180. * @return the context or 0 if not running.
  181. */
  182. const struct mg_context *
  183. getContext() const
  184. {
  185. return context;
  186. }
  187. /**
  188. * addHandler(const std::string &, CivetHandler *)
  189. *
  190. * Adds a URI handler. If there is existing URI handler, it will
  191. * be replaced with this one.
  192. *
  193. * URI's are ordered and prefix (REST) URI's are supported.
  194. *
  195. * @param uri - URI to match.
  196. * @param handler - handler instance to use.
  197. */
  198. void addHandler(const std::string &uri, CivetHandler *handler);
  199. void
  200. addHandler(const std::string &uri, CivetHandler &handler)
  201. {
  202. addHandler(uri, &handler);
  203. }
  204. /**
  205. * addWebSocketHandler
  206. *
  207. * Adds a WebSocket handler for a specific URI. If there is existing URI
  208. *handler, it will
  209. * be replaced with this one.
  210. *
  211. * URI's are ordered and prefix (REST) URI's are supported.
  212. *
  213. * @param uri - URI to match.
  214. * @param handler - handler instance to use.
  215. */
  216. void addWebSocketHandler(const std::string &uri,
  217. CivetWebSocketHandler *handler);
  218. void
  219. addWebSocketHandler(const std::string &uri, CivetWebSocketHandler &handler)
  220. {
  221. addWebSocketHandler(uri, &handler);
  222. }
  223. /**
  224. * removeHandler(const std::string &)
  225. *
  226. * Removes a handler.
  227. *
  228. * @param uri - the exact URL used in addHandler().
  229. */
  230. void removeHandler(const std::string &uri);
  231. /**
  232. * removeWebSocketHandler(const std::string &)
  233. *
  234. * Removes a web socket handler.
  235. *
  236. * @param uri - the exact URL used in addWebSocketHandler().
  237. */
  238. void removeWebSocketHandler(const std::string &uri);
  239. /**
  240. * getListeningPorts()
  241. *
  242. * Returns a list of ports that are listening
  243. *
  244. * @return A vector of ports
  245. */
  246. std::vector<int> getListeningPorts();
  247. /**
  248. * getCookie(struct mg_connection *conn, const std::string &cookieName,
  249. *std::string &cookieValue)
  250. *
  251. * Puts the cookie value string that matches the cookie name in the
  252. *cookieValue destinaton string.
  253. *
  254. * @param conn - the connection information
  255. * @param cookieName - cookie name to get the value from
  256. * @param cookieValue - cookie value is returned using thiis reference
  257. * @returns the size of the cookie value string read.
  258. */
  259. static int getCookie(struct mg_connection *conn,
  260. const std::string &cookieName,
  261. std::string &cookieValue);
  262. /**
  263. * getHeader(struct mg_connection *conn, const std::string &headerName)
  264. * @param conn - the connection information
  265. * @param headerName - header name to get the value from
  266. * @returns a char array whcih contains the header value as string
  267. */
  268. static const char *getHeader(struct mg_connection *conn,
  269. const std::string &headerName);
  270. /**
  271. * getParam(struct mg_connection *conn, const char *, std::string &, size_t)
  272. *
  273. * Returns a query paramter contained in the supplied buffer. The
  274. * occurance value is a zero-based index of a particular key name. This
  275. * should not be confused with the index over all of the keys. Note that
  276. *this
  277. * function assumes that parameters are sent as text in http query string
  278. * format, which is the default for web forms. This function will work for
  279. * html forms with method="GET" and method="POST" attributes. In other
  280. *cases,
  281. * you may use a getParam version that directly takes the data instead of
  282. *the
  283. * connection as a first argument.
  284. *
  285. * @param conn - parameters are read from the data sent through this
  286. *connection
  287. * @param name - the key to search for
  288. * @param dst - the destination string
  289. * @param occurrence - the occurrence of the selected name in the query (0
  290. *based).
  291. * @return true if key was found
  292. */
  293. static bool getParam(struct mg_connection *conn,
  294. const char *name,
  295. std::string &dst,
  296. size_t occurrence = 0);
  297. /**
  298. * getParam(const std::string &, const char *, std::string &, size_t)
  299. *
  300. * Returns a query paramter contained in the supplied buffer. The
  301. * occurance value is a zero-based index of a particular key name. This
  302. * should not be confused with the index over all of the keys.
  303. *
  304. * @param data - the query string (text)
  305. * @param name - the key to search for
  306. * @param dst - the destination string
  307. * @param occurrence - the occurrence of the selected name in the query (0
  308. *based).
  309. * @return true if key was found
  310. */
  311. static bool
  312. getParam(const std::string &data,
  313. const char *name,
  314. std::string &dst,
  315. size_t occurrence = 0)
  316. {
  317. return getParam(data.c_str(), data.length(), name, dst, occurrence);
  318. }
  319. /**
  320. * getParam(const char *, size_t, const char *, std::string &, size_t)
  321. *
  322. * Returns a query paramter contained in the supplied buffer. The
  323. * occurance value is a zero-based index of a particular key name. This
  324. * should not be confused with the index over all of the keys.
  325. *
  326. * @param data the - query string (text)
  327. * @param data_len - length of the query string
  328. * @param name - the key to search for
  329. * @param dst - the destination string
  330. * @param occurrence - the occurrence of the selected name in the query (0
  331. *based).
  332. * @return true if key was found
  333. */
  334. static bool getParam(const char *data,
  335. size_t data_len,
  336. const char *name,
  337. std::string &dst,
  338. size_t occurrence = 0);
  339. /**
  340. * urlDecode(const std::string &, std::string &, bool)
  341. *
  342. * @param src - string to be decoded
  343. * @param dst - destination string
  344. * @param is_form_url_encoded - true if form url encoded
  345. * form-url-encoded data differs from URI encoding in a way that it
  346. * uses '+' as character for space, see RFC 1866 section 8.2.1
  347. * http://ftp.ics.uci.edu/pub/ietf/html/rfc1866.txt
  348. */
  349. static void
  350. urlDecode(const std::string &src,
  351. std::string &dst,
  352. bool is_form_url_encoded = true)
  353. {
  354. urlDecode(src.c_str(), src.length(), dst, is_form_url_encoded);
  355. }
  356. /**
  357. * urlDecode(const char *, size_t, std::string &, bool)
  358. *
  359. * @param src - buffer to be decoded
  360. * @param src_len - length of buffer to be decoded
  361. * @param dst - destination string
  362. * @param is_form_url_encoded - true if form url encoded
  363. * form-url-encoded data differs from URI encoding in a way that it
  364. * uses '+' as character for space, see RFC 1866 section 8.2.1
  365. * http://ftp.ics.uci.edu/pub/ietf/html/rfc1866.txt
  366. */
  367. static void urlDecode(const char *src,
  368. size_t src_len,
  369. std::string &dst,
  370. bool is_form_url_encoded = true);
  371. /**
  372. * urlDecode(const char *, std::string &, bool)
  373. *
  374. * @param src - buffer to be decoded (0 terminated)
  375. * @param dst - destination string
  376. * @param is_form_url_encoded true - if form url encoded
  377. * form-url-encoded data differs from URI encoding in a way that it
  378. * uses '+' as character for space, see RFC 1866 section 8.2.1
  379. * http://ftp.ics.uci.edu/pub/ietf/html/rfc1866.txt
  380. */
  381. static void urlDecode(const char *src,
  382. std::string &dst,
  383. bool is_form_url_encoded = true);
  384. /**
  385. * urlEncode(const std::string &, std::string &, bool)
  386. *
  387. * @param src - buffer to be encoded
  388. * @param dst - destination string
  389. * @param append - true if string should not be cleared before encoding.
  390. */
  391. static void
  392. urlEncode(const std::string &src, std::string &dst, bool append = false)
  393. {
  394. urlEncode(src.c_str(), src.length(), dst, append);
  395. }
  396. /**
  397. * urlEncode(const char *, size_t, std::string &, bool)
  398. *
  399. * @param src - buffer to be encoded (0 terminated)
  400. * @param dst - destination string
  401. * @param append - true if string should not be cleared before encoding.
  402. */
  403. static void
  404. urlEncode(const char *src, std::string &dst, bool append = false);
  405. /**
  406. * urlEncode(const char *, size_t, std::string &, bool)
  407. *
  408. * @param src - buffer to be encoded
  409. * @param src_len - length of buffer to be decoded
  410. * @param dst - destination string
  411. * @param append - true if string should not be cleared before encoding.
  412. */
  413. static void urlEncode(const char *src,
  414. size_t src_len,
  415. std::string &dst,
  416. bool append = false);
  417. protected:
  418. class CivetConnection
  419. {
  420. public:
  421. char *postData;
  422. unsigned long postDataLen;
  423. CivetConnection();
  424. ~CivetConnection();
  425. };
  426. struct mg_context *context;
  427. std::map<struct mg_connection *, class CivetConnection> connections;
  428. private:
  429. /**
  430. * requestHandler(struct mg_connection *, void *cbdata)
  431. *
  432. * Handles the incomming request.
  433. *
  434. * @param conn - the connection information
  435. * @param cbdata - pointer to the CivetHandler instance.
  436. * @returns 0 if implemented, false otherwise
  437. */
  438. static int requestHandler(struct mg_connection *conn, void *cbdata);
  439. static int webSocketConnectionHandler(const struct mg_connection *conn,
  440. void *cbdata);
  441. static void webSocketReadyHandler(struct mg_connection *conn, void *cbdata);
  442. static int webSocketDataHandler(struct mg_connection *conn,
  443. int bits,
  444. char *data,
  445. size_t data_len,
  446. void *cbdata);
  447. static void webSocketCloseHandler(const struct mg_connection *conn,
  448. void *cbdata);
  449. /**
  450. * closeHandler(struct mg_connection *)
  451. *
  452. * Handles closing a request (internal handler)
  453. *
  454. * @param conn - the connection information
  455. */
  456. static void closeHandler(const struct mg_connection *conn);
  457. /**
  458. * Stores the user provided close handler
  459. */
  460. void (*userCloseHandler)(const struct mg_connection *conn);
  461. };
  462. #endif /* __cplusplus */
  463. #endif /* _CIVETWEB_SERVER_H_ */