CivetServer.h 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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 <vector>
  11. #include <string>
  12. class CivetServer; // forward declaration
  13. /**
  14. * Basic interface for a URI request handler. Handlers implementations
  15. * must be reentrant.
  16. */
  17. class CivetHandler
  18. {
  19. public:
  20. /**
  21. * Destructor
  22. */
  23. virtual ~CivetHandler() {
  24. }
  25. /**
  26. * Callback method for GET request.
  27. *
  28. * @param server - the calling server
  29. * @param conn - the connection information
  30. * @returns true if implemented, false otherwise
  31. */
  32. virtual bool handleGet(CivetServer *server, struct mg_connection *conn);
  33. /**
  34. * Callback method for POST request.
  35. *
  36. * @param server - the calling server
  37. * @param conn - the connection information
  38. * @returns true if implemented, false otherwise
  39. */
  40. virtual bool handlePost(CivetServer *server, struct mg_connection *conn);
  41. /**
  42. * Callback method for PUT request.
  43. *
  44. * @param server - the calling server
  45. * @param conn - the connection information
  46. * @returns true if implemented, false otherwise
  47. */
  48. virtual bool handlePut(CivetServer *server, struct mg_connection *conn);
  49. /**
  50. * Callback method for DELETE request.
  51. *
  52. * @param server - the calling server
  53. * @param conn - the connection information
  54. * @returns true if implemented, false otherwise
  55. */
  56. virtual bool handleDelete(CivetServer *server, struct mg_connection *conn);
  57. };
  58. /**
  59. * CivetServer
  60. *
  61. * Basic class for embedded web server. This has a URL mapping built-in.
  62. */
  63. class CivetServer
  64. {
  65. public:
  66. /**
  67. * Constructor
  68. *
  69. * This automatically starts the sever.
  70. * It is good practice to call getContext() after this in case there
  71. * were errors starting the server.
  72. *
  73. * @param options - the web server options.
  74. * @param callbacks - optional web server callback methods.
  75. */
  76. CivetServer(const char **options, const struct mg_callbacks *callbacks = 0);
  77. /**
  78. * Destructor
  79. */
  80. virtual ~CivetServer();
  81. /**
  82. * close()
  83. *
  84. * Stops server and frees resources.
  85. */
  86. void close();
  87. /**
  88. * getContext()
  89. *
  90. * @return the context or 0 if not running.
  91. */
  92. const struct mg_context *getContext() const {
  93. return context;
  94. }
  95. /**
  96. * addHandler(const std::string &, CivetHandler *)
  97. *
  98. * Adds a URI handler. If there is existing URI handler, it will
  99. * be replaced with this one.
  100. *
  101. * URI's are ordered and prefix (REST) URI's are supported.
  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. char * postData;
  246. private:
  247. /**
  248. * requestHandler(struct mg_connection *, void *cbdata)
  249. *
  250. * Handles the incomming request.
  251. *
  252. * @param conn - the connection information
  253. * @param cbdata - pointer to the CivetHandler instance.
  254. * @returns 0 if implemented, false otherwise
  255. */
  256. static int requestHandler(struct mg_connection *conn, void *cbdata);
  257. /**
  258. * closeHandler(struct mg_connection *)
  259. *
  260. * Handles closing a request (internal handler)
  261. *
  262. * @param conn - the connection information
  263. */
  264. static void closeHandler(struct mg_connection *conn);
  265. /**
  266. * Stores the user provided close handler
  267. */
  268. void (*userCloseHandler)(struct mg_connection *conn);
  269. };
  270. #endif /* __cplusplus */
  271. #endif /* _CIVETWEB_SERVER_H_ */