CivetServer.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  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. #include <vector>
  13. #include <stdexcept>
  14. // forward declaration
  15. class CivetServer;
  16. /**
  17. * Exception class for thrown exceptions within the CivetHandler object.
  18. */
  19. class CIVETWEB_API CivetException : public std::runtime_error
  20. {
  21. public:
  22. CivetException(const std::string &msg) : std::runtime_error(msg)
  23. {
  24. }
  25. };
  26. /**
  27. * Basic interface for a URI request handler. Handlers implementations
  28. * must be reentrant.
  29. */
  30. class CIVETWEB_API CivetHandler
  31. {
  32. public:
  33. /**
  34. * Destructor
  35. */
  36. virtual ~CivetHandler()
  37. {
  38. }
  39. /**
  40. * Callback method for GET 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 handleGet(CivetServer *server, struct mg_connection *conn);
  47. /**
  48. * Callback method for POST 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 handlePost(CivetServer *server, struct mg_connection *conn);
  55. /**
  56. * Callback method for PUT request.
  57. *
  58. * @param server - the calling server
  59. * @param conn - the connection information
  60. * @returns true if implemented, false otherwise
  61. */
  62. virtual bool handlePut(CivetServer *server, struct mg_connection *conn);
  63. /**
  64. * Callback method for DELETE request.
  65. *
  66. * @param server - the calling server
  67. * @param conn - the connection information
  68. * @returns true if implemented, false otherwise
  69. */
  70. virtual bool handleDelete(CivetServer *server, struct mg_connection *conn);
  71. /**
  72. * Callback method for OPTIONS request.
  73. *
  74. * @param server - the calling server
  75. * @param conn - the connection information
  76. * @returns true if implemented, false otherwise
  77. */
  78. virtual bool handleOptions(CivetServer *server, struct mg_connection *conn);
  79. };
  80. /**
  81. * CivetServer
  82. *
  83. * Basic class for embedded web server. This has an URL mapping built-in.
  84. */
  85. class CIVETWEB_API CivetServer
  86. {
  87. public:
  88. /**
  89. * Constructor
  90. *
  91. * This automatically starts the sever.
  92. * It is good practice to call getContext() after this in case there
  93. * were errors starting the server.
  94. *
  95. * @param options - the web server options.
  96. * @param callbacks - optional web server callback methods.
  97. *
  98. * @throws CivetException
  99. */
  100. CivetServer(const char **options, const struct mg_callbacks *callbacks = 0);
  101. /**
  102. * Destructor
  103. */
  104. virtual ~CivetServer();
  105. /**
  106. * close()
  107. *
  108. * Stops server and frees resources.
  109. */
  110. void close();
  111. /**
  112. * getContext()
  113. *
  114. * @return the context or 0 if not running.
  115. */
  116. const struct mg_context *getContext() const
  117. {
  118. return context;
  119. }
  120. /**
  121. * addHandler(const std::string &, CivetHandler *)
  122. *
  123. * Adds a URI handler. If there is existing URI handler, it will
  124. * be replaced with this one.
  125. *
  126. * URI's are ordered and prefix (REST) URI's are supported.
  127. *
  128. * @param uri - URI to match.
  129. * @param handler - handler instance to use.
  130. */
  131. void addHandler(const std::string &uri, CivetHandler *handler);
  132. void addHandler(const std::string &uri, CivetHandler &handler)
  133. {
  134. addHandler(uri, &handler);
  135. }
  136. /**
  137. * removeHandler(const std::string &)
  138. *
  139. * Removes a handler.
  140. *
  141. * @param uri - the exact URL used in addHandler().
  142. */
  143. void removeHandler(const std::string &uri);
  144. /**
  145. * getListeningPorts()
  146. *
  147. * Returns a list of ports that are listening
  148. *
  149. * @return A vector of ports
  150. */
  151. std::vector<int> getListeningPorts();
  152. /**
  153. * getCookie(struct mg_connection *conn, const std::string &cookieName,
  154. *std::string &cookieValue)
  155. *
  156. * Puts the cookie value string that matches the cookie name in the
  157. *cookieValue destinaton string.
  158. *
  159. * @param conn - the connection information
  160. * @param cookieName - cookie name to get the value from
  161. * @param cookieValue - cookie value is returned using thiis reference
  162. * @returns the size of the cookie value string read.
  163. */
  164. static int getCookie(struct mg_connection *conn,
  165. const std::string &cookieName,
  166. std::string &cookieValue);
  167. /**
  168. * getHeader(struct mg_connection *conn, const std::string &headerName)
  169. * @param conn - the connection information
  170. * @param headerName - header name to get the value from
  171. * @returns a char array whcih contains the header value as string
  172. */
  173. static const char *getHeader(struct mg_connection *conn,
  174. const std::string &headerName);
  175. /**
  176. * getParam(struct mg_connection *conn, const char *, std::string &, size_t)
  177. *
  178. * Returns a query paramter contained in the supplied buffer. The
  179. * occurance value is a zero-based index of a particular key name. This
  180. * should not be confused with the index over all of the keys. Note that
  181. *this
  182. * function assumes that parameters are sent as text in http query string
  183. * format, which is the default for web forms. This function will work for
  184. * html forms with method="GET" and method="POST" attributes. In other
  185. *cases,
  186. * you may use a getParam version that directly takes the data instead of
  187. *the
  188. * connection as a first argument.
  189. *
  190. * @param conn - parameters are read from the data sent through this
  191. *connection
  192. * @param name - the key to search for
  193. * @param dst - the destination string
  194. * @param occurrence - the occurrence of the selected name in the query (0
  195. *based).
  196. * @return true if key was found
  197. */
  198. static bool getParam(struct mg_connection *conn,
  199. const char *name,
  200. std::string &dst,
  201. size_t occurrence = 0);
  202. /**
  203. * getParam(const std::string &, const char *, std::string &, size_t)
  204. *
  205. * Returns a query paramter contained in the supplied buffer. The
  206. * occurance value is a zero-based index of a particular key name. This
  207. * should not be confused with the index over all of the keys.
  208. *
  209. * @param data - the query string (text)
  210. * @param name - the key to search for
  211. * @param dst - the destination string
  212. * @param occurrence - the occurrence of the selected name in the query (0
  213. *based).
  214. * @return true if key was found
  215. */
  216. static bool getParam(const std::string &data,
  217. const char *name,
  218. std::string &dst,
  219. size_t occurrence = 0)
  220. {
  221. return getParam(data.c_str(), data.length(), name, dst, occurrence);
  222. }
  223. /**
  224. * getParam(const char *, size_t, const char *, std::string &, size_t)
  225. *
  226. * Returns a query paramter contained in the supplied buffer. The
  227. * occurance value is a zero-based index of a particular key name. This
  228. * should not be confused with the index over all of the keys.
  229. *
  230. * @param data the - query string (text)
  231. * @param data_len - length of the query string
  232. * @param name - the key to search for
  233. * @param dst - the destination string
  234. * @param occurrence - the occurrence of the selected name in the query (0
  235. *based).
  236. * @return true if key was found
  237. */
  238. static bool getParam(const char *data,
  239. size_t data_len,
  240. const char *name,
  241. std::string &dst,
  242. size_t occurrence = 0);
  243. /**
  244. * urlDecode(const std::string &, std::string &, bool)
  245. *
  246. * @param src - string to be decoded
  247. * @param dst - destination string
  248. * @param is_form_url_encoded - true if form url encoded
  249. * form-url-encoded data differs from URI encoding in a way that it
  250. * uses '+' as character for space, see RFC 1866 section 8.2.1
  251. * http://ftp.ics.uci.edu/pub/ietf/html/rfc1866.txt
  252. */
  253. static void urlDecode(const std::string &src,
  254. std::string &dst,
  255. bool is_form_url_encoded = true)
  256. {
  257. urlDecode(src.c_str(), src.length(), dst, is_form_url_encoded);
  258. }
  259. /**
  260. * urlDecode(const char *, size_t, std::string &, bool)
  261. *
  262. * @param src - buffer to be decoded
  263. * @param src_len - length of buffer to be decoded
  264. * @param dst - destination string
  265. * @param is_form_url_encoded - true if form url encoded
  266. * form-url-encoded data differs from URI encoding in a way that it
  267. * uses '+' as character for space, see RFC 1866 section 8.2.1
  268. * http://ftp.ics.uci.edu/pub/ietf/html/rfc1866.txt
  269. */
  270. static void urlDecode(const char *src,
  271. size_t src_len,
  272. std::string &dst,
  273. bool is_form_url_encoded = true);
  274. /**
  275. * urlDecode(const char *, std::string &, bool)
  276. *
  277. * @param src - buffer to be decoded (0 terminated)
  278. * @param dst - destination string
  279. * @param is_form_url_encoded true - if form url encoded
  280. * form-url-encoded data differs from URI encoding in a way that it
  281. * uses '+' as character for space, see RFC 1866 section 8.2.1
  282. * http://ftp.ics.uci.edu/pub/ietf/html/rfc1866.txt
  283. */
  284. static void urlDecode(const char *src,
  285. std::string &dst,
  286. bool is_form_url_encoded = true);
  287. /**
  288. * urlEncode(const std::string &, std::string &, bool)
  289. *
  290. * @param src - buffer to be encoded
  291. * @param dst - destination string
  292. * @param append - true if string should not be cleared before encoding.
  293. */
  294. static void
  295. urlEncode(const std::string &src, std::string &dst, bool append = false)
  296. {
  297. urlEncode(src.c_str(), src.length(), dst, append);
  298. }
  299. /**
  300. * urlEncode(const char *, size_t, std::string &, bool)
  301. *
  302. * @param src - buffer to be encoded (0 terminated)
  303. * @param dst - destination string
  304. * @param append - true if string should not be cleared before encoding.
  305. */
  306. static void
  307. urlEncode(const char *src, std::string &dst, bool append = false);
  308. /**
  309. * urlEncode(const char *, size_t, std::string &, bool)
  310. *
  311. * @param src - buffer to be encoded
  312. * @param src_len - length of buffer to be decoded
  313. * @param dst - destination string
  314. * @param append - true if string should not be cleared before encoding.
  315. */
  316. static void urlEncode(const char *src,
  317. size_t src_len,
  318. std::string &dst,
  319. bool append = false);
  320. protected:
  321. class CivetConnection
  322. {
  323. public:
  324. char *postData;
  325. unsigned long postDataLen;
  326. CivetConnection();
  327. ~CivetConnection();
  328. };
  329. struct mg_context *context;
  330. std::map<struct mg_connection *, class CivetConnection> connections;
  331. private:
  332. /**
  333. * requestHandler(struct mg_connection *, void *cbdata)
  334. *
  335. * Handles the incomming request.
  336. *
  337. * @param conn - the connection information
  338. * @param cbdata - pointer to the CivetHandler instance.
  339. * @returns 0 if implemented, false otherwise
  340. */
  341. static int requestHandler(struct mg_connection *conn, void *cbdata);
  342. /**
  343. * closeHandler(struct mg_connection *)
  344. *
  345. * Handles closing a request (internal handler)
  346. *
  347. * @param conn - the connection information
  348. */
  349. static void closeHandler(const struct mg_connection *conn);
  350. /**
  351. * Stores the user provided close handler
  352. */
  353. void (*userCloseHandler)(const struct mg_connection *conn);
  354. };
  355. #endif /* __cplusplus */
  356. #endif /* _CIVETWEB_SERVER_H_ */