CivetServer.h 14 KB

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