CivetServer.h 11 KB

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