CivetServer.h 11 KB

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