CivetServer.h 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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 partcial URI's are supported. For example,
  101. * consider two URIs in order: /a/b and /a; /a matches
  102. * /a, /a/b matches /a/b, /a/c matches /a. Reversing the order to
  103. * /a and /a/b; /a matches /a/b, /a/b matches /a. /a/c matches /a.
  104. *
  105. * @param uri - URI to match.
  106. * @param handler - handler instance to use. This will be free'ed
  107. * when the server closes and instances cannot be reused.
  108. */
  109. void addHandler(const std::string &uri, CivetHandler *handler);
  110. /**
  111. * removeHandler(const std::string &)
  112. *
  113. * Removes a handler.
  114. *
  115. * @param - the exact URL used in addHandler().
  116. */
  117. void removeHandler(const std::string &uri);
  118. /**
  119. * getCookie(struct mg_connection *conn, const std::string &cookieName, std::string &cookieValue)
  120. * @param conn - the connection information
  121. * @param cookieName - cookie name to get the value from
  122. * @param cookieValue - cookie value is returned using thiis reference
  123. * @puts the cookie value string that matches the cookie name in the _cookieValue string.
  124. * @returns the size of the cookie value string read.
  125. */
  126. static int getCookie(struct mg_connection *conn, const std::string &cookieName, std::string &cookieValue);
  127. /**
  128. * getHeader(struct mg_connection *conn, const std::string &headerName)
  129. * @param conn - the connection information
  130. * @param headerName - header name to get the value from
  131. * @returns a char array whcih contains the header value as string
  132. */
  133. static const char* getHeader(struct mg_connection *conn, const std::string &headerName);
  134. /**
  135. * getParam(struct mg_connection *conn, const char *, std::string &, size_t)
  136. *
  137. * Returns a query paramter contained in the supplied buffer. The
  138. * occurance value is a zero-based index of a particular key name. This
  139. * should nto be confused with the index over all of the keys.
  140. *
  141. * @param data the query string
  142. * @param name the key to search for
  143. * @param the destination string
  144. * @param occurrence the occurrence of the selected name in the query (0 based).
  145. * @return true of key was found
  146. */
  147. static bool getParam(struct mg_connection *conn, const char *name,
  148. std::string &dst, size_t occurrence=0);
  149. /**
  150. * getParam(const std::string &, const char *, std::string &, size_t)
  151. *
  152. * Returns a query paramter contained in the supplied buffer. The
  153. * occurance value is a zero-based index of a particular key name. This
  154. * should nto be confused with the index over all of the keys.
  155. *
  156. * @param data the query string
  157. * @param name the key to search for
  158. * @param the destination string
  159. * @param occurrence the occurrence of the selected name in the query (0 based).
  160. * @return true of key was found
  161. */
  162. static bool getParam(const std::string &data, const char *name,
  163. std::string &dst, size_t occurrence=0) {
  164. return getParam(data.c_str(), data.length(), name, dst, occurrence);
  165. }
  166. /**
  167. * getParam(const char *, size_t, const char *, std::string &, size_t)
  168. *
  169. * Returns a query paramter contained in the supplied buffer. The
  170. * occurance value is a zero-based index of a particular key name. This
  171. * should nto be confused with the index over all of the keys.
  172. *
  173. * @param data the query string
  174. * @param length length of the query string
  175. * @param name the key to search for
  176. * @param the destination string
  177. * @param occurrence the occurrence of the selected name in the query (0 based).
  178. * @return true of key was found
  179. */
  180. static bool getParam(const char *data, size_t data_len, const char *name,
  181. std::string &dst, size_t occurrence=0);
  182. /**
  183. * urlDecode(const std::string &, std::string &, bool)
  184. *
  185. * @param src string to be decoded
  186. * @param dst destination string
  187. * @is_form_url_encoded true if form url encoded
  188. * form-url-encoded data differs from URI encoding in a way that it
  189. * uses '+' as character for space, see RFC 1866 section 8.2.1
  190. * http://ftp.ics.uci.edu/pub/ietf/html/rfc1866.txt
  191. */
  192. static void urlDecode(const std::string &src, std::string &dst, bool is_form_url_encoded=true) {
  193. urlDecode(src.c_str(), src.length(), dst, is_form_url_encoded);
  194. }
  195. /**
  196. * urlDecode(const char *, size_t, std::string &, bool)
  197. *
  198. * @param src buffer to be decoded
  199. * @param src_len length of buffer to be decoded
  200. * @param dst destination string
  201. * @is_form_url_encoded true if form url encoded
  202. * form-url-encoded data differs from URI encoding in a way that it
  203. * uses '+' as character for space, see RFC 1866 section 8.2.1
  204. * http://ftp.ics.uci.edu/pub/ietf/html/rfc1866.txt
  205. */
  206. static void urlDecode(const char *src, size_t src_len, std::string &dst, bool is_form_url_encoded=true);
  207. /**
  208. * urlDecode(const char *, std::string &, bool)
  209. *
  210. * @param src buffer to be decoded (0 terminated)
  211. * @param dst destination string
  212. * @is_form_url_encoded true if form url encoded
  213. * form-url-encoded data differs from URI encoding in a way that it
  214. * uses '+' as character for space, see RFC 1866 section 8.2.1
  215. * http://ftp.ics.uci.edu/pub/ietf/html/rfc1866.txt
  216. */
  217. static void urlDecode(const char *src, std::string &dst, bool is_form_url_encoded=true);
  218. /**
  219. * urlEncode(const std::string &, std::string &, bool)
  220. *
  221. * @param src buffer to be encoded
  222. * @param dst destination string
  223. * @append true if string should not be cleared before encoding.
  224. */
  225. static void urlEncode(const std::string &src, std::string &dst, bool append=false) {
  226. urlEncode(src.c_str(), src.length(), dst, append);
  227. }
  228. /**
  229. * urlEncode(const char *, size_t, std::string &, bool)
  230. *
  231. * @param src buffer to be encoded (0 terminated)
  232. * @param dst destination string
  233. * @append true if string should not be cleared before encoding.
  234. */
  235. static void urlEncode(const char *src, std::string &dst, bool append=false);
  236. /**
  237. * urlEncode(const char *, size_t, std::string &, bool)
  238. *
  239. * @param src buffer to be encoded
  240. * @param src_len length of buffer to be decoded
  241. * @param dst destination string
  242. * @append true if string should not be cleared before encoding.
  243. */
  244. static void urlEncode(const char *src, size_t src_len, std::string &dst, bool append=false);
  245. protected:
  246. struct mg_context *context;
  247. private:
  248. /**
  249. * requestHandler(struct mg_connection *, void *cbdata)
  250. *
  251. * Handles the incomming request.
  252. *
  253. * @param conn - the connection information
  254. * @param cbdata - pointer to the CivetHandler instance.
  255. * @returns 0 if implemented, false otherwise
  256. */
  257. static int requestHandler(struct mg_connection *conn, void *cbdata);
  258. };
  259. #endif /* __cplusplus */
  260. #endif /* _CIVETWEB_SERVER_H_ */