CivetServer.h 11 KB

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