CivetServer.h 10 KB

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