CivetServer.h 9.6 KB

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