CivetServer.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  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. * CivetServer
  82. *
  83. * Basic class for embedded web server. This has an URL mapping built-in.
  84. */
  85. class CIVETWEB_API CivetServer
  86. {
  87. public:
  88. /**
  89. * Constructor
  90. *
  91. * This automatically starts the sever.
  92. * It is good practice to call getContext() after this in case there
  93. * were errors starting the server.
  94. *
  95. * @param options - the web server options.
  96. * @param callbacks - optional web server callback methods.
  97. *
  98. * @throws CivetException
  99. */
  100. CivetServer(const char **options, const struct mg_callbacks *callbacks = 0);
  101. /**
  102. * Destructor
  103. */
  104. virtual ~CivetServer();
  105. /**
  106. * close()
  107. *
  108. * Stops server and frees resources.
  109. */
  110. void close();
  111. /**
  112. * getContext()
  113. *
  114. * @return the context or 0 if not running.
  115. */
  116. const struct mg_context *
  117. getContext() const
  118. {
  119. return context;
  120. }
  121. /**
  122. * addHandler(const std::string &, CivetHandler *)
  123. *
  124. * Adds a URI handler. If there is existing URI handler, it will
  125. * be replaced with this one.
  126. *
  127. * URI's are ordered and prefix (REST) URI's are supported.
  128. *
  129. * @param uri - URI to match.
  130. * @param handler - handler instance to use.
  131. */
  132. void addHandler(const std::string &uri, CivetHandler *handler);
  133. void
  134. addHandler(const std::string &uri, CivetHandler &handler)
  135. {
  136. addHandler(uri, &handler);
  137. }
  138. /**
  139. * removeHandler(const std::string &)
  140. *
  141. * Removes a handler.
  142. *
  143. * @param uri - the exact URL used in addHandler().
  144. */
  145. void removeHandler(const std::string &uri);
  146. /**
  147. * getListeningPorts()
  148. *
  149. * Returns a list of ports that are listening
  150. *
  151. * @return A vector of ports
  152. */
  153. std::vector<int> getListeningPorts();
  154. /**
  155. * getCookie(struct mg_connection *conn, const std::string &cookieName,
  156. *std::string &cookieValue)
  157. *
  158. * Puts the cookie value string that matches the cookie name in the
  159. *cookieValue destinaton string.
  160. *
  161. * @param conn - the connection information
  162. * @param cookieName - cookie name to get the value from
  163. * @param cookieValue - cookie value is returned using thiis reference
  164. * @returns the size of the cookie value string read.
  165. */
  166. static int getCookie(struct mg_connection *conn,
  167. const std::string &cookieName,
  168. std::string &cookieValue);
  169. /**
  170. * getHeader(struct mg_connection *conn, const std::string &headerName)
  171. * @param conn - the connection information
  172. * @param headerName - header name to get the value from
  173. * @returns a char array whcih contains the header value as string
  174. */
  175. static const char *getHeader(struct mg_connection *conn,
  176. const std::string &headerName);
  177. /**
  178. * getParam(struct mg_connection *conn, const char *, std::string &, size_t)
  179. *
  180. * Returns a query paramter contained in the supplied buffer. The
  181. * occurance value is a zero-based index of a particular key name. This
  182. * should not be confused with the index over all of the keys. Note that
  183. *this
  184. * function assumes that parameters are sent as text in http query string
  185. * format, which is the default for web forms. This function will work for
  186. * html forms with method="GET" and method="POST" attributes. In other
  187. *cases,
  188. * you may use a getParam version that directly takes the data instead of
  189. *the
  190. * connection as a first argument.
  191. *
  192. * @param conn - parameters are read from the data sent through this
  193. *connection
  194. * @param name - the key to search for
  195. * @param dst - the destination string
  196. * @param occurrence - the occurrence of the selected name in the query (0
  197. *based).
  198. * @return true if key was found
  199. */
  200. static bool getParam(struct mg_connection *conn,
  201. const char *name,
  202. std::string &dst,
  203. size_t occurrence = 0);
  204. /**
  205. * getParam(const std::string &, const char *, std::string &, size_t)
  206. *
  207. * Returns a query paramter contained in the supplied buffer. The
  208. * occurance value is a zero-based index of a particular key name. This
  209. * should not be confused with the index over all of the keys.
  210. *
  211. * @param data - the query string (text)
  212. * @param name - the key to search for
  213. * @param dst - the destination string
  214. * @param occurrence - the occurrence of the selected name in the query (0
  215. *based).
  216. * @return true if key was found
  217. */
  218. static bool
  219. getParam(const std::string &data,
  220. const char *name,
  221. std::string &dst,
  222. size_t occurrence = 0)
  223. {
  224. return getParam(data.c_str(), data.length(), name, dst, occurrence);
  225. }
  226. /**
  227. * getParam(const char *, size_t, const char *, std::string &, size_t)
  228. *
  229. * Returns a query paramter contained in the supplied buffer. The
  230. * occurance value is a zero-based index of a particular key name. This
  231. * should not be confused with the index over all of the keys.
  232. *
  233. * @param data the - query string (text)
  234. * @param data_len - length of the query string
  235. * @param name - the key to search for
  236. * @param dst - the destination string
  237. * @param occurrence - the occurrence of the selected name in the query (0
  238. *based).
  239. * @return true if key was found
  240. */
  241. static bool getParam(const char *data,
  242. size_t data_len,
  243. const char *name,
  244. std::string &dst,
  245. size_t occurrence = 0);
  246. /**
  247. * urlDecode(const std::string &, std::string &, bool)
  248. *
  249. * @param src - string to be decoded
  250. * @param dst - destination string
  251. * @param is_form_url_encoded - true if form url encoded
  252. * form-url-encoded data differs from URI encoding in a way that it
  253. * uses '+' as character for space, see RFC 1866 section 8.2.1
  254. * http://ftp.ics.uci.edu/pub/ietf/html/rfc1866.txt
  255. */
  256. static void
  257. urlDecode(const std::string &src,
  258. std::string &dst,
  259. bool is_form_url_encoded = true)
  260. {
  261. urlDecode(src.c_str(), src.length(), dst, is_form_url_encoded);
  262. }
  263. /**
  264. * urlDecode(const char *, size_t, std::string &, bool)
  265. *
  266. * @param src - buffer to be decoded
  267. * @param src_len - length of buffer to be decoded
  268. * @param dst - destination string
  269. * @param is_form_url_encoded - true if form url encoded
  270. * form-url-encoded data differs from URI encoding in a way that it
  271. * uses '+' as character for space, see RFC 1866 section 8.2.1
  272. * http://ftp.ics.uci.edu/pub/ietf/html/rfc1866.txt
  273. */
  274. static void urlDecode(const char *src,
  275. size_t src_len,
  276. std::string &dst,
  277. bool is_form_url_encoded = true);
  278. /**
  279. * urlDecode(const char *, std::string &, bool)
  280. *
  281. * @param src - buffer to be decoded (0 terminated)
  282. * @param dst - destination string
  283. * @param is_form_url_encoded true - if form url encoded
  284. * form-url-encoded data differs from URI encoding in a way that it
  285. * uses '+' as character for space, see RFC 1866 section 8.2.1
  286. * http://ftp.ics.uci.edu/pub/ietf/html/rfc1866.txt
  287. */
  288. static void urlDecode(const char *src,
  289. std::string &dst,
  290. bool is_form_url_encoded = true);
  291. /**
  292. * urlEncode(const std::string &, std::string &, bool)
  293. *
  294. * @param src - buffer to be encoded
  295. * @param dst - destination string
  296. * @param append - true if string should not be cleared before encoding.
  297. */
  298. static void
  299. urlEncode(const std::string &src, std::string &dst, bool append = false)
  300. {
  301. urlEncode(src.c_str(), src.length(), dst, append);
  302. }
  303. /**
  304. * urlEncode(const char *, size_t, std::string &, bool)
  305. *
  306. * @param src - buffer to be encoded (0 terminated)
  307. * @param dst - destination string
  308. * @param append - true if string should not be cleared before encoding.
  309. */
  310. static void
  311. urlEncode(const char *src, std::string &dst, bool append = false);
  312. /**
  313. * urlEncode(const char *, size_t, std::string &, bool)
  314. *
  315. * @param src - buffer to be encoded
  316. * @param src_len - length of buffer to be decoded
  317. * @param dst - destination string
  318. * @param append - true if string should not be cleared before encoding.
  319. */
  320. static void urlEncode(const char *src,
  321. size_t src_len,
  322. std::string &dst,
  323. bool append = false);
  324. protected:
  325. class CivetConnection
  326. {
  327. public:
  328. char *postData;
  329. unsigned long postDataLen;
  330. CivetConnection();
  331. ~CivetConnection();
  332. };
  333. struct mg_context *context;
  334. std::map<struct mg_connection *, class CivetConnection> connections;
  335. private:
  336. /**
  337. * requestHandler(struct mg_connection *, void *cbdata)
  338. *
  339. * Handles the incomming request.
  340. *
  341. * @param conn - the connection information
  342. * @param cbdata - pointer to the CivetHandler instance.
  343. * @returns 0 if implemented, false otherwise
  344. */
  345. static int requestHandler(struct mg_connection *conn, void *cbdata);
  346. /**
  347. * closeHandler(struct mg_connection *)
  348. *
  349. * Handles closing a request (internal handler)
  350. *
  351. * @param conn - the connection information
  352. */
  353. static void closeHandler(const struct mg_connection *conn);
  354. /**
  355. * Stores the user provided close handler
  356. */
  357. void (*userCloseHandler)(const struct mg_connection *conn);
  358. };
  359. #endif /* __cplusplus */
  360. #endif /* _CIVETWEB_SERVER_H_ */