CivetServer.h 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. /*
  2. * Copyright (c) 2013 No Face Press, LLC
  3. * License http://opensource.org/licenses/mit-license.php MIT License
  4. */
  5. #ifndef _CIVETWEB_SERVER_H_
  6. #define _CIVETWEB_SERVER_H_
  7. #ifdef __cplusplus
  8. #include "civetweb.h"
  9. #include <vector>
  10. #include <string>
  11. class CivetServer; // forward declaration
  12. /**
  13. * Basic interface for a URI request handler. Handlers implementations
  14. * must be reentrant.
  15. */
  16. class CivetHandler
  17. {
  18. public:
  19. /**
  20. * Destructor
  21. */
  22. virtual ~CivetHandler() {
  23. }
  24. /**
  25. * Callback method for GET request.
  26. *
  27. * @param server - the calling server
  28. * @param conn - the connection information
  29. * @returns true if implemented, false otherwise
  30. */
  31. virtual bool handleGet(CivetServer *server, struct mg_connection *conn);
  32. /**
  33. * Callback method for POST request.
  34. *
  35. * @param server - the calling server
  36. * @param conn - the connection information
  37. * @returns true if implemented, false otherwise
  38. */
  39. virtual bool handlePost(CivetServer *server, struct mg_connection *conn);
  40. /**
  41. * Callback method for PUT request.
  42. *
  43. * @param server - the calling server
  44. * @param conn - the connection information
  45. * @returns true if implemented, false otherwise
  46. */
  47. virtual bool handlePut(CivetServer *server, struct mg_connection *conn);
  48. /**
  49. * Callback method for DELETE request.
  50. *
  51. * @param server - the calling server
  52. * @param conn - the connection information
  53. * @returns true if implemented, false otherwise
  54. */
  55. virtual bool handleDelete(CivetServer *server, struct mg_connection *conn);
  56. };
  57. /**
  58. * CivetServer
  59. *
  60. * Basic class for embedded web server. This has a URL mapping built-in.
  61. */
  62. class CivetServer
  63. {
  64. public:
  65. /**
  66. * Constructor
  67. *
  68. * This automatically starts the sever.
  69. * It is good practice to call getContext() after this in case there
  70. * were errors starting the server.
  71. *
  72. * @param options - the web server options.
  73. * @param callbacks - optional web server callback methods.
  74. */
  75. CivetServer(const char **options, const struct mg_callbacks *callbacks = 0);
  76. /**
  77. * Destructor
  78. */
  79. virtual ~CivetServer();
  80. /**
  81. * close()
  82. *
  83. * Stops server and frees resources.
  84. */
  85. void close();
  86. /**
  87. * getContext()
  88. *
  89. * @return the context or 0 if not running.
  90. */
  91. const struct mg_context *getContext() const {
  92. return context;
  93. }
  94. /**
  95. * addHandler(const std::string &, CivetHandler *)
  96. *
  97. * Adds a URI handler. If there is existing URI handler, it will
  98. * be replaced with this one.
  99. *
  100. * URI's are ordered and prefix (REST) URI's are supported.
  101. *
  102. * @param uri - URI to match.
  103. * @param handler - handler instance to use. This will be free'ed
  104. * when the server closes and instances cannot be reused.
  105. */
  106. void addHandler(const std::string &uri, CivetHandler *handler);
  107. /**
  108. * removeHandler(const std::string &)
  109. *
  110. * Removes a handler.
  111. *
  112. * @param - the exact URL used in addHandler().
  113. */
  114. void removeHandler(const std::string &uri);
  115. /**
  116. * getCookie(struct mg_connection *conn, const std::string &cookieName, std::string &cookieValue)
  117. * @param conn - the connection information
  118. * @param cookieName - cookie name to get the value from
  119. * @param cookieValue - cookie value is returned using thiis reference
  120. * @puts the cookie value string that matches the cookie name in the _cookieValue string.
  121. * @returns the size of the cookie value string read.
  122. */
  123. static int getCookie(struct mg_connection *conn, const std::string &cookieName, std::string &cookieValue);
  124. /**
  125. * getHeader(struct mg_connection *conn, const std::string &headerName)
  126. * @param conn - the connection information
  127. * @param headerName - header name to get the value from
  128. * @returns a char array whcih contains the header value as string
  129. */
  130. static const char* getHeader(struct mg_connection *conn, const std::string &headerName);
  131. /**
  132. * getParam(struct mg_connection *conn, const char *, std::string &, size_t)
  133. *
  134. * Returns a query paramter contained in the supplied buffer. The
  135. * occurance value is a zero-based index of a particular key name. This
  136. * should nto be confused with the index over all of the keys.
  137. *
  138. * @param data the query string
  139. * @param name the key to search for
  140. * @param the destination string
  141. * @param occurrence the occurrence of the selected name in the query (0 based).
  142. * @return true of key was found
  143. */
  144. static bool getParam(struct mg_connection *conn, const char *name,
  145. std::string &dst, size_t occurrence=0);
  146. /**
  147. * getParam(const std::string &, const char *, std::string &, size_t)
  148. *
  149. * Returns a query paramter contained in the supplied buffer. The
  150. * occurance value is a zero-based index of a particular key name. This
  151. * should nto be confused with the index over all of the keys.
  152. *
  153. * @param data the query string
  154. * @param name the key to search for
  155. * @param the destination string
  156. * @param occurrence the occurrence of the selected name in the query (0 based).
  157. * @return true of key was found
  158. */
  159. static bool getParam(const std::string &data, const char *name,
  160. std::string &dst, size_t occurrence=0) {
  161. return getParam(data.c_str(), data.length(), name, dst, occurrence);
  162. }
  163. /**
  164. * getParam(const char *, size_t, 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 nto be confused with the index over all of the keys.
  169. *
  170. * @param data the query string
  171. * @param length length of the query string
  172. * @param name the key to search for
  173. * @param the destination string
  174. * @param occurrence the occurrence of the selected name in the query (0 based).
  175. * @return true of key was found
  176. */
  177. static bool getParam(const char *data, size_t data_len, const char *name,
  178. std::string &dst, size_t occurrence=0);
  179. /**
  180. * urlDecode(const std::string &, std::string &, bool)
  181. *
  182. * @param src string to be decoded
  183. * @param dst destination string
  184. * @is_form_url_encoded true if form url encoded
  185. * form-url-encoded data differs from URI encoding in a way that it
  186. * uses '+' as character for space, see RFC 1866 section 8.2.1
  187. * http://ftp.ics.uci.edu/pub/ietf/html/rfc1866.txt
  188. */
  189. static void urlDecode(const std::string &src, std::string &dst, bool is_form_url_encoded=true) {
  190. urlDecode(src.c_str(), src.length(), dst, is_form_url_encoded);
  191. }
  192. /**
  193. * urlDecode(const char *, size_t, std::string &, bool)
  194. *
  195. * @param src buffer to be decoded
  196. * @param src_len length of buffer to be decoded
  197. * @param dst destination string
  198. * @is_form_url_encoded true if form url encoded
  199. * form-url-encoded data differs from URI encoding in a way that it
  200. * uses '+' as character for space, see RFC 1866 section 8.2.1
  201. * http://ftp.ics.uci.edu/pub/ietf/html/rfc1866.txt
  202. */
  203. static void urlDecode(const char *src, size_t src_len, std::string &dst, bool is_form_url_encoded=true);
  204. /**
  205. * urlDecode(const char *, std::string &, bool)
  206. *
  207. * @param src buffer to be decoded (0 terminated)
  208. * @param dst destination string
  209. * @is_form_url_encoded true if form url encoded
  210. * form-url-encoded data differs from URI encoding in a way that it
  211. * uses '+' as character for space, see RFC 1866 section 8.2.1
  212. * http://ftp.ics.uci.edu/pub/ietf/html/rfc1866.txt
  213. */
  214. static void urlDecode(const char *src, std::string &dst, bool is_form_url_encoded=true);
  215. /**
  216. * urlEncode(const std::string &, std::string &, bool)
  217. *
  218. * @param src buffer to be encoded
  219. * @param dst destination string
  220. * @append true if string should not be cleared before encoding.
  221. */
  222. static void urlEncode(const std::string &src, std::string &dst, bool append=false) {
  223. urlEncode(src.c_str(), src.length(), dst, append);
  224. }
  225. /**
  226. * urlEncode(const char *, size_t, std::string &, bool)
  227. *
  228. * @param src buffer to be encoded (0 terminated)
  229. * @param dst destination string
  230. * @append true if string should not be cleared before encoding.
  231. */
  232. static void urlEncode(const char *src, std::string &dst, bool append=false);
  233. /**
  234. * urlEncode(const char *, size_t, std::string &, bool)
  235. *
  236. * @param src buffer to be encoded
  237. * @param src_len length of buffer to be decoded
  238. * @param dst destination string
  239. * @append true if string should not be cleared before encoding.
  240. */
  241. static void urlEncode(const char *src, size_t src_len, std::string &dst, bool append=false);
  242. protected:
  243. struct mg_context *context;
  244. private:
  245. /**
  246. * requestHandler(struct mg_connection *, void *cbdata)
  247. *
  248. * Handles the incomming request.
  249. *
  250. * @param conn - the connection information
  251. * @param cbdata - pointer to the CivetHandler instance.
  252. * @returns 0 if implemented, false otherwise
  253. */
  254. static int requestHandler(struct mg_connection *conn, void *cbdata);
  255. };
  256. #endif /* __cplusplus */
  257. #endif /* _CIVETWEB_SERVER_H_ */