CivetServer.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. // Copyright (c) 2013 Thomas Davis
  2. //
  3. // Permission is hereby granted, free of charge, to any person obtaining a copy
  4. // of this software and associated documentation files (the "Software"), to deal
  5. // in the Software without restriction, including without limitation the rights
  6. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. // copies of the Software, and to permit persons to whom the Software is
  8. // furnished to do so, subject to the following conditions:
  9. //
  10. // The above copyright notice and this permission notice shall be included in
  11. // all copies or substantial portions of the Software.
  12. //
  13. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. // THE SOFTWARE
  20. #ifndef _CIVETWEB_SERVER_H_
  21. #define _CIVETWEB_SERVER_H_
  22. #ifdef __cplusplus
  23. #include <civetweb.h>
  24. #include <vector>
  25. #include <string>
  26. class CivetServer; // forward declaration
  27. /**
  28. * Basic interface for a URI request handler. Handlers implementations
  29. * must be reentrant.
  30. */
  31. class CivetHandler {
  32. public:
  33. /**
  34. * Destructor
  35. */
  36. virtual ~CivetHandler() {
  37. }
  38. /**
  39. * Callback method for GET request.
  40. *
  41. * @param server - the calling server
  42. * @param conn - the connection information
  43. * @returns true if implemented, false otherwise
  44. */
  45. virtual bool handleGet(CivetServer *server, struct mg_connection *conn) {
  46. return false;
  47. }
  48. /**
  49. * Callback method for POST 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 handlePost(CivetServer *server, struct mg_connection *conn) {
  56. return false;
  57. }
  58. /**
  59. * Callback method for PUT 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 handlePut(CivetServer *server, struct mg_connection *conn) {
  66. return false;
  67. }
  68. /**
  69. * Callback method for DELETE request.
  70. *
  71. * @param server - the calling server
  72. * @param conn - the connection information
  73. * @returns true if implemented, false otherwise
  74. */
  75. virtual bool handleDelete(CivetServer *server, struct mg_connection *conn) {
  76. return false;
  77. }
  78. };
  79. /**
  80. * CivetServer
  81. *
  82. * Basic class for embedded web server. This has a URL mapping built-in.
  83. */
  84. class CivetServer {
  85. public:
  86. /**
  87. * Constructor
  88. *
  89. * This automatically starts the sever.
  90. * It is good practice to call getContext() after this in case there
  91. * were errors starting the server.
  92. *
  93. * @param options - the web server options.
  94. * @param callbacks - optional web server callback methods.
  95. * Note that this class overrides begin_request callback.
  96. */
  97. CivetServer(const char **options, const struct mg_callbacks *callbacks = 0);
  98. /**
  99. * Destructor
  100. */
  101. virtual ~CivetServer();
  102. /**
  103. * close()
  104. *
  105. * Stops server and frees resources.
  106. */
  107. void close();
  108. /**
  109. * getContext()
  110. *
  111. * @return the context or 0 if not running.
  112. */
  113. const struct mg_context *getContext() const {
  114. return context;
  115. }
  116. /**
  117. * addHandler(const std::string &, CivetHandler *)
  118. *
  119. * Adds a URI handler. If there is existing URI handler, it will
  120. * be replaced with this one. The handler is "owned" by this server
  121. * and will be deallocated with it.
  122. *
  123. * URI's are ordered and partcial URI's are supported. For example,
  124. * consider two URIs in order: /a/b and /a; /a matches
  125. * /a, /a/b matches /a/b, /a/c matches /a. Reversing the order to
  126. * /a and /a/b; /a matches /a/b, /a/b matches /a. /a/c matches /a.
  127. *
  128. * @param uri - URI to match.
  129. * @param handler - handler instance to use. This will be free'ed
  130. * when the server closes and instances cannot be reused.
  131. */
  132. void addHandler(const std::string &uri, CivetHandler *handler);
  133. /**
  134. * removeHandler(const std::string &)
  135. *
  136. * Removes a handler, deleting it if found.
  137. *
  138. * @param - the exact URL used in addHandler().
  139. */
  140. void removeHandler(const std::string &uri);
  141. /**
  142. * getHandler(const std::string &uri)
  143. *
  144. * @param uri - the URI
  145. * @returns the handler that matches the requested URI or 0 if none were found.
  146. */
  147. CivetHandler *getHandler(const std::string &uri) const {
  148. return getHandler(uri.data(), uri.length());
  149. }
  150. /**
  151. * getHandler(const char *uri, unsigned urilen)
  152. *
  153. * @param uri - the URI
  154. * @param urilen - the length of the URI
  155. * @returns the handler that matches the requested URI or 0 if none were found.
  156. */
  157. CivetHandler *getHandler(const char *uri, unsigned urilen) const;
  158. protected:
  159. /**
  160. * handleRequest(struct mg_connection *)
  161. *
  162. * Handles the incomming request.
  163. *
  164. * @param conn - the connection information
  165. * @returns true if implemented, false otherwise
  166. */
  167. virtual bool handleRequest(struct mg_connection *conn);
  168. /**
  169. * Returns the index of the handler that matches the
  170. * URI exactly.
  171. *
  172. * @param uri - the url to match
  173. */
  174. int getIndex(const std::string &uri) const;
  175. std::vector<std::string> uris;
  176. std::vector<CivetHandler *> handlers;
  177. struct mg_context *context;
  178. private:
  179. static int begin_request_callback(struct mg_connection *conn);
  180. };
  181. #endif /* __cplusplus */
  182. #endif /* _CIVETWEB_SERVER_H_ */