CivetServer.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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. This will be free'ed
  126. * when the server closes and instances cannot be reused.
  127. */
  128. void addHandler(const std::string &uri, CivetHandler *handler);
  129. /**
  130. * removeHandler(const std::string &)
  131. *
  132. * Removes a handler.
  133. *
  134. * @param uri - the exact URL used in addHandler().
  135. */
  136. void removeHandler(const std::string &uri);
  137. /**
  138. * getListeningPorts()
  139. *
  140. * Returns a list of ports that are listening
  141. *
  142. * @return A vector of ports
  143. */
  144. std::vector<int> getListeningPorts();
  145. /**
  146. * getCookie(struct mg_connection *conn, const std::string &cookieName, std::string &cookieValue)
  147. *
  148. * Puts the cookie value string that matches the cookie name in the cookieValue destinaton string.
  149. *
  150. * @param conn - the connection information
  151. * @param cookieName - cookie name to get the value from
  152. * @param cookieValue - cookie value is returned using thiis reference
  153. * @returns the size of the cookie value string read.
  154. */
  155. static int getCookie(struct mg_connection *conn, const std::string &cookieName, 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, const std::string &headerName);
  163. /**
  164. * getParam(struct mg_connection *conn, const char *, std::string &, size_t)
  165. *
  166. * Returns a query paramter contained in the supplied buffer. The
  167. * occurance value is a zero-based index of a particular key name. This
  168. * should not be confused with the index over all of the keys. Note that this
  169. * function assumes that parameters are sent as text in http query string
  170. * format, which is the default for web forms. This function will work for
  171. * html forms with method="GET" and method="POST" attributes. In other cases,
  172. * you may use a getParam version that directly takes the data instead of the
  173. * connection as a first argument.
  174. *
  175. * @param conn - parameters are read from the data sent through this connection
  176. * @param name - the key to search for
  177. * @param dst - the destination string
  178. * @param occurrence - the occurrence of the selected name in the query (0 based).
  179. * @return true if key was found
  180. */
  181. static bool getParam(struct mg_connection *conn, const char *name,
  182. std::string &dst, size_t occurrence=0);
  183. /**
  184. * getParam(const std::string &, const char *, std::string &, size_t)
  185. *
  186. * Returns a query paramter contained in the supplied buffer. The
  187. * occurance value is a zero-based index of a particular key name. This
  188. * should not be confused with the index over all of the keys.
  189. *
  190. * @param data - the query string (text)
  191. * @param name - the key to search for
  192. * @param dst - the destination string
  193. * @param occurrence - the occurrence of the selected name in the query (0 based).
  194. * @return true if key was found
  195. */
  196. static bool getParam(const std::string &data, const char *name,
  197. std::string &dst, size_t occurrence=0) {
  198. return getParam(data.c_str(), data.length(), name, dst, occurrence);
  199. }
  200. /**
  201. * getParam(const char *, size_t, const char *, std::string &, size_t)
  202. *
  203. * Returns a query paramter contained in the supplied buffer. The
  204. * occurance value is a zero-based index of a particular key name. This
  205. * should not be confused with the index over all of the keys.
  206. *
  207. * @param data the - query string (text)
  208. * @param data_len - length of the query string
  209. * @param name - the key to search for
  210. * @param dst - the destination string
  211. * @param occurrence - the occurrence of the selected name in the query (0 based).
  212. * @return true if key was found
  213. */
  214. static bool getParam(const char *data, size_t data_len, const char *name,
  215. std::string &dst, size_t occurrence=0);
  216. /**
  217. * urlDecode(const std::string &, std::string &, bool)
  218. *
  219. * @param src - string to be decoded
  220. * @param dst - destination string
  221. * @param is_form_url_encoded - true if form url encoded
  222. * form-url-encoded data differs from URI encoding in a way that it
  223. * uses '+' as character for space, see RFC 1866 section 8.2.1
  224. * http://ftp.ics.uci.edu/pub/ietf/html/rfc1866.txt
  225. */
  226. static void urlDecode(const std::string &src, std::string &dst, bool is_form_url_encoded=true) {
  227. urlDecode(src.c_str(), src.length(), dst, is_form_url_encoded);
  228. }
  229. /**
  230. * urlDecode(const char *, size_t, std::string &, bool)
  231. *
  232. * @param src - buffer to be decoded
  233. * @param src_len - length of buffer to be decoded
  234. * @param dst - destination string
  235. * @param is_form_url_encoded - true if form url encoded
  236. * form-url-encoded data differs from URI encoding in a way that it
  237. * uses '+' as character for space, see RFC 1866 section 8.2.1
  238. * http://ftp.ics.uci.edu/pub/ietf/html/rfc1866.txt
  239. */
  240. static void urlDecode(const char *src, size_t src_len, std::string &dst, bool is_form_url_encoded=true);
  241. /**
  242. * urlDecode(const char *, std::string &, bool)
  243. *
  244. * @param src - buffer to be decoded (0 terminated)
  245. * @param dst - destination string
  246. * @param is_form_url_encoded true - if form url encoded
  247. * form-url-encoded data differs from URI encoding in a way that it
  248. * uses '+' as character for space, see RFC 1866 section 8.2.1
  249. * http://ftp.ics.uci.edu/pub/ietf/html/rfc1866.txt
  250. */
  251. static void urlDecode(const char *src, std::string &dst, bool is_form_url_encoded=true);
  252. /**
  253. * urlEncode(const std::string &, std::string &, bool)
  254. *
  255. * @param src - buffer to be encoded
  256. * @param dst - destination string
  257. * @param append - true if string should not be cleared before encoding.
  258. */
  259. static void urlEncode(const std::string &src, std::string &dst, bool append=false) {
  260. urlEncode(src.c_str(), src.length(), dst, append);
  261. }
  262. /**
  263. * urlEncode(const char *, size_t, std::string &, bool)
  264. *
  265. * @param src - buffer to be encoded (0 terminated)
  266. * @param dst - destination string
  267. * @param append - true if string should not be cleared before encoding.
  268. */
  269. static void urlEncode(const char *src, std::string &dst, bool append=false);
  270. /**
  271. * urlEncode(const char *, size_t, std::string &, bool)
  272. *
  273. * @param src - buffer to be encoded
  274. * @param src_len - length of buffer to be decoded
  275. * @param dst - destination string
  276. * @param append - true if string should not be cleared before encoding.
  277. */
  278. static void urlEncode(const char *src, size_t src_len, std::string &dst, bool append=false);
  279. protected:
  280. class CivetConnection {
  281. public:
  282. char * postData;
  283. unsigned long postDataLen;
  284. CivetConnection();
  285. ~CivetConnection();
  286. };
  287. struct mg_context *context;
  288. std::map<struct mg_connection *, class CivetConnection> connections;
  289. private:
  290. /**
  291. * requestHandler(struct mg_connection *, void *cbdata)
  292. *
  293. * Handles the incomming request.
  294. *
  295. * @param conn - the connection information
  296. * @param cbdata - pointer to the CivetHandler instance.
  297. * @returns 0 if implemented, false otherwise
  298. */
  299. static int requestHandler(struct mg_connection *conn, void *cbdata);
  300. /**
  301. * closeHandler(struct mg_connection *)
  302. *
  303. * Handles closing a request (internal handler)
  304. *
  305. * @param conn - the connection information
  306. */
  307. static void closeHandler(struct mg_connection *conn);
  308. /**
  309. * Stores the user provided close handler
  310. */
  311. void (*userCloseHandler)(struct mg_connection *conn);
  312. };
  313. #endif /* __cplusplus */
  314. #endif /* _CIVETWEB_SERVER_H_ */