CivetServer.h 14 KB

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