CivetServer.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  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, const struct mg_callbacks *callbacks = 0);
  158. /**
  159. * Destructor
  160. */
  161. virtual ~CivetServer();
  162. /**
  163. * close()
  164. *
  165. * Stops server and frees resources.
  166. */
  167. void close();
  168. /**
  169. * getContext()
  170. *
  171. * @return the context or 0 if not running.
  172. */
  173. const struct mg_context *
  174. getContext() const
  175. {
  176. return context;
  177. }
  178. /**
  179. * addHandler(const std::string &, CivetHandler *)
  180. *
  181. * Adds a URI handler. If there is existing URI handler, it will
  182. * be replaced with this one.
  183. *
  184. * URI's are ordered and prefix (REST) URI's are supported.
  185. *
  186. * @param uri - URI to match.
  187. * @param handler - handler instance to use.
  188. */
  189. void addHandler(const std::string &uri, CivetHandler *handler);
  190. void
  191. addHandler(const std::string &uri, CivetHandler &handler)
  192. {
  193. addHandler(uri, &handler);
  194. }
  195. /**
  196. * addWebSocketHandler
  197. *
  198. * Adds a WebSocket handler for a specific URI. If there is existing URI
  199. *handler, it will
  200. * be replaced with this one.
  201. *
  202. * URI's are ordered and prefix (REST) URI's are supported.
  203. *
  204. * @param uri - URI to match.
  205. * @param handler - handler instance to use.
  206. */
  207. void addWebSocketHandler(const std::string &uri,
  208. CivetWebSocketHandler *handler);
  209. void
  210. addWebSocketHandler(const std::string &uri, CivetWebSocketHandler &handler)
  211. {
  212. addWebSocketHandler(uri, &handler);
  213. }
  214. /**
  215. * removeHandler(const std::string &)
  216. *
  217. * Removes a handler.
  218. *
  219. * @param uri - the exact URL used in addHandler().
  220. */
  221. void removeHandler(const std::string &uri);
  222. /**
  223. * removeWebSocketHandler(const std::string &)
  224. *
  225. * Removes a web socket handler.
  226. *
  227. * @param uri - the exact URL used in addWebSocketHandler().
  228. */
  229. void removeWebSocketHandler(const std::string &uri);
  230. /**
  231. * getListeningPorts()
  232. *
  233. * Returns a list of ports that are listening
  234. *
  235. * @return A vector of ports
  236. */
  237. std::vector<int> getListeningPorts();
  238. /**
  239. * getCookie(struct mg_connection *conn, const std::string &cookieName,
  240. *std::string &cookieValue)
  241. *
  242. * Puts the cookie value string that matches the cookie name in the
  243. *cookieValue destinaton string.
  244. *
  245. * @param conn - the connection information
  246. * @param cookieName - cookie name to get the value from
  247. * @param cookieValue - cookie value is returned using thiis reference
  248. * @returns the size of the cookie value string read.
  249. */
  250. static int getCookie(struct mg_connection *conn,
  251. const std::string &cookieName,
  252. std::string &cookieValue);
  253. /**
  254. * getHeader(struct mg_connection *conn, const std::string &headerName)
  255. * @param conn - the connection information
  256. * @param headerName - header name to get the value from
  257. * @returns a char array whcih contains the header value as string
  258. */
  259. static const char *getHeader(struct mg_connection *conn,
  260. const std::string &headerName);
  261. /**
  262. * getParam(struct mg_connection *conn, const char *, std::string &, size_t)
  263. *
  264. * Returns a query paramter contained in the supplied buffer. The
  265. * occurance value is a zero-based index of a particular key name. This
  266. * should not be confused with the index over all of the keys. Note that
  267. *this
  268. * function assumes that parameters are sent as text in http query string
  269. * format, which is the default for web forms. This function will work for
  270. * html forms with method="GET" and method="POST" attributes. In other
  271. *cases,
  272. * you may use a getParam version that directly takes the data instead of
  273. *the
  274. * connection as a first argument.
  275. *
  276. * @param conn - parameters are read from the data sent through this
  277. *connection
  278. * @param name - the key to search for
  279. * @param dst - the destination string
  280. * @param occurrence - the occurrence of the selected name in the query (0
  281. *based).
  282. * @return true if key was found
  283. */
  284. static bool getParam(struct mg_connection *conn,
  285. const char *name,
  286. std::string &dst,
  287. size_t occurrence = 0);
  288. /**
  289. * getParam(const std::string &, const char *, std::string &, size_t)
  290. *
  291. * Returns a query paramter contained in the supplied buffer. The
  292. * occurance value is a zero-based index of a particular key name. This
  293. * should not be confused with the index over all of the keys.
  294. *
  295. * @param data - the query string (text)
  296. * @param name - the key to search for
  297. * @param dst - the destination string
  298. * @param occurrence - the occurrence of the selected name in the query (0
  299. *based).
  300. * @return true if key was found
  301. */
  302. static bool
  303. getParam(const std::string &data,
  304. const char *name,
  305. std::string &dst,
  306. size_t occurrence = 0)
  307. {
  308. return getParam(data.c_str(), data.length(), name, dst, occurrence);
  309. }
  310. /**
  311. * getParam(const char *, size_t, const char *, std::string &, size_t)
  312. *
  313. * Returns a query paramter contained in the supplied buffer. The
  314. * occurance value is a zero-based index of a particular key name. This
  315. * should not be confused with the index over all of the keys.
  316. *
  317. * @param data the - query string (text)
  318. * @param data_len - length of the query string
  319. * @param name - the key to search for
  320. * @param dst - the destination string
  321. * @param occurrence - the occurrence of the selected name in the query (0
  322. *based).
  323. * @return true if key was found
  324. */
  325. static bool getParam(const char *data,
  326. size_t data_len,
  327. const char *name,
  328. std::string &dst,
  329. size_t occurrence = 0);
  330. /**
  331. * urlDecode(const std::string &, std::string &, bool)
  332. *
  333. * @param src - string to be decoded
  334. * @param dst - destination string
  335. * @param is_form_url_encoded - true if form url encoded
  336. * form-url-encoded data differs from URI encoding in a way that it
  337. * uses '+' as character for space, see RFC 1866 section 8.2.1
  338. * http://ftp.ics.uci.edu/pub/ietf/html/rfc1866.txt
  339. */
  340. static void
  341. urlDecode(const std::string &src,
  342. std::string &dst,
  343. bool is_form_url_encoded = true)
  344. {
  345. urlDecode(src.c_str(), src.length(), dst, is_form_url_encoded);
  346. }
  347. /**
  348. * urlDecode(const char *, size_t, std::string &, bool)
  349. *
  350. * @param src - buffer to be decoded
  351. * @param src_len - length of buffer to be decoded
  352. * @param dst - destination string
  353. * @param is_form_url_encoded - true if form url encoded
  354. * form-url-encoded data differs from URI encoding in a way that it
  355. * uses '+' as character for space, see RFC 1866 section 8.2.1
  356. * http://ftp.ics.uci.edu/pub/ietf/html/rfc1866.txt
  357. */
  358. static void urlDecode(const char *src,
  359. size_t src_len,
  360. std::string &dst,
  361. bool is_form_url_encoded = true);
  362. /**
  363. * urlDecode(const char *, std::string &, bool)
  364. *
  365. * @param src - buffer to be decoded (0 terminated)
  366. * @param dst - destination string
  367. * @param is_form_url_encoded true - if form url encoded
  368. * form-url-encoded data differs from URI encoding in a way that it
  369. * uses '+' as character for space, see RFC 1866 section 8.2.1
  370. * http://ftp.ics.uci.edu/pub/ietf/html/rfc1866.txt
  371. */
  372. static void urlDecode(const char *src,
  373. std::string &dst,
  374. bool is_form_url_encoded = true);
  375. /**
  376. * urlEncode(const std::string &, std::string &, bool)
  377. *
  378. * @param src - buffer to be encoded
  379. * @param dst - destination string
  380. * @param append - true if string should not be cleared before encoding.
  381. */
  382. static void
  383. urlEncode(const std::string &src, std::string &dst, bool append = false)
  384. {
  385. urlEncode(src.c_str(), src.length(), dst, append);
  386. }
  387. /**
  388. * urlEncode(const char *, size_t, std::string &, bool)
  389. *
  390. * @param src - buffer to be encoded (0 terminated)
  391. * @param dst - destination string
  392. * @param append - true if string should not be cleared before encoding.
  393. */
  394. static void
  395. urlEncode(const char *src, std::string &dst, bool append = false);
  396. /**
  397. * urlEncode(const char *, size_t, std::string &, bool)
  398. *
  399. * @param src - buffer to be encoded
  400. * @param src_len - length of buffer to be decoded
  401. * @param dst - destination string
  402. * @param append - true if string should not be cleared before encoding.
  403. */
  404. static void urlEncode(const char *src,
  405. size_t src_len,
  406. std::string &dst,
  407. bool append = false);
  408. protected:
  409. class CivetConnection
  410. {
  411. public:
  412. char *postData;
  413. unsigned long postDataLen;
  414. CivetConnection();
  415. ~CivetConnection();
  416. };
  417. struct mg_context *context;
  418. std::map<struct mg_connection *, class CivetConnection> connections;
  419. private:
  420. /**
  421. * requestHandler(struct mg_connection *, void *cbdata)
  422. *
  423. * Handles the incomming request.
  424. *
  425. * @param conn - the connection information
  426. * @param cbdata - pointer to the CivetHandler instance.
  427. * @returns 0 if implemented, false otherwise
  428. */
  429. static int requestHandler(struct mg_connection *conn, void *cbdata);
  430. static int webSocketConnectionHandler(const struct mg_connection *conn,
  431. void *cbdata);
  432. static void webSocketReadyHandler(struct mg_connection *conn, void *cbdata);
  433. static int webSocketDataHandler(struct mg_connection *conn,
  434. int bits,
  435. char *data,
  436. size_t data_len,
  437. void *cbdata);
  438. static void webSocketCloseHandler(const struct mg_connection *conn,
  439. void *cbdata);
  440. /**
  441. * closeHandler(struct mg_connection *)
  442. *
  443. * Handles closing a request (internal handler)
  444. *
  445. * @param conn - the connection information
  446. */
  447. static void closeHandler(const struct mg_connection *conn);
  448. /**
  449. * Stores the user provided close handler
  450. */
  451. void (*userCloseHandler)(const struct mg_connection *conn);
  452. };
  453. #endif /* __cplusplus */
  454. #endif /* _CIVETWEB_SERVER_H_ */