CivetServer.h 11 KB

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