CivetServer.h 14 KB

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