CivetServer.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /*
  2. * Copyright (c) 2013 No Face Press, LLC
  3. * License http://opensource.org/licenses/mit-license.php MIT License
  4. */
  5. #ifndef _CIVETWEB_SERVER_H_
  6. #define _CIVETWEB_SERVER_H_
  7. #ifdef __cplusplus
  8. #include "civetweb.h"
  9. #include <vector>
  10. #include <string>
  11. class CivetServer; // forward declaration
  12. /**
  13. * Basic interface for a URI request handler. Handlers implementations
  14. * must be reentrant.
  15. */
  16. class CivetHandler {
  17. public:
  18. /**
  19. * Destructor
  20. */
  21. virtual ~CivetHandler() {
  22. }
  23. /**
  24. * Callback method for GET request.
  25. *
  26. * @param server - the calling server
  27. * @param conn - the connection information
  28. * @returns true if implemented, false otherwise
  29. */
  30. virtual bool handleGet(CivetServer *server, struct mg_connection *conn) {
  31. return false;
  32. }
  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. return false;
  42. }
  43. /**
  44. * Callback method for PUT request.
  45. *
  46. * @param server - the calling server
  47. * @param conn - the connection information
  48. * @returns true if implemented, false otherwise
  49. */
  50. virtual bool handlePut(CivetServer *server, struct mg_connection *conn) {
  51. return false;
  52. }
  53. /**
  54. * Callback method for DELETE request.
  55. *
  56. * @param server - the calling server
  57. * @param conn - the connection information
  58. * @returns true if implemented, false otherwise
  59. */
  60. virtual bool handleDelete(CivetServer *server, struct mg_connection *conn) {
  61. return false;
  62. }
  63. };
  64. /**
  65. * CivetServer
  66. *
  67. * Basic class for embedded web server. This has a URL mapping built-in.
  68. */
  69. class CivetServer {
  70. public:
  71. /**
  72. * Constructor
  73. *
  74. * This automatically starts the sever.
  75. * It is good practice to call getContext() after this in case there
  76. * were errors starting the server.
  77. *
  78. * @param options - the web server options.
  79. * @param callbacks - optional web server callback methods.
  80. * Note that this class overrides begin_request callback.
  81. */
  82. CivetServer(const char **options, const struct mg_callbacks *callbacks = 0);
  83. /**
  84. * Destructor
  85. */
  86. virtual ~CivetServer();
  87. /**
  88. * close()
  89. *
  90. * Stops server and frees resources.
  91. */
  92. void close();
  93. /**
  94. * getContext()
  95. *
  96. * @return the context or 0 if not running.
  97. */
  98. const struct mg_context *getContext() const {
  99. return context;
  100. }
  101. /**
  102. * addHandler(const std::string &, CivetHandler *)
  103. *
  104. * Adds a URI handler. If there is existing URI handler, it will
  105. * be replaced with this one. The handler is "owned" by this server
  106. * and will be deallocated with it.
  107. *
  108. * URI's are ordered and partcial URI's are supported. For example,
  109. * consider two URIs in order: /a/b and /a; /a matches
  110. * /a, /a/b matches /a/b, /a/c matches /a. Reversing the order to
  111. * /a and /a/b; /a matches /a/b, /a/b matches /a. /a/c matches /a.
  112. *
  113. * @param uri - URI to match.
  114. * @param handler - handler instance to use. This will be free'ed
  115. * when the server closes and instances cannot be reused.
  116. */
  117. void addHandler(const std::string &uri, CivetHandler *handler);
  118. /**
  119. * removeHandler(const std::string &)
  120. *
  121. * Removes a handler, deleting it if found.
  122. *
  123. * @param - the exact URL used in addHandler().
  124. */
  125. void removeHandler(const std::string &uri);
  126. /**
  127. * getHandler(const std::string &uri)
  128. *
  129. * @param uri - the URI
  130. * @returns the handler that matches the requested URI or 0 if none were found.
  131. */
  132. CivetHandler *getHandler(const std::string &uri) const {
  133. return getHandler(uri.data(), uri.length());
  134. }
  135. /**
  136. * getHandler(const char *uri, unsigned urilen)
  137. *
  138. * @param uri - the URI
  139. * @param urilen - the length of the URI
  140. * @returns the handler that matches the requested URI or 0 if none were found.
  141. */
  142. CivetHandler *getHandler(const char *uri, unsigned urilen) const;
  143. protected:
  144. /**
  145. * handleRequest(struct mg_connection *)
  146. *
  147. * Handles the incomming request.
  148. *
  149. * @param conn - the connection information
  150. * @returns true if implemented, false otherwise
  151. */
  152. virtual bool handleRequest(struct mg_connection *conn);
  153. /**
  154. * Returns the index of the handler that matches the
  155. * URI exactly.
  156. *
  157. * @param uri - the url to match
  158. */
  159. int getIndex(const std::string &uri) const;
  160. std::vector<std::string> uris;
  161. std::vector<CivetHandler *> handlers;
  162. struct mg_context *context;
  163. private:
  164. static int begin_request_callback(struct mg_connection *conn);
  165. };
  166. #endif /* __cplusplus */
  167. #endif /* _CIVETWEB_SERVER_H_ */