CivetServer.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. /**
  32. * Callback method for POST request.
  33. *
  34. * @param server - the calling server
  35. * @param conn - the connection information
  36. * @returns true if implemented, false otherwise
  37. */
  38. virtual bool handlePost(CivetServer *server, struct mg_connection *conn);
  39. /**
  40. * Callback method for PUT 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 handlePut(CivetServer *server, struct mg_connection *conn);
  47. /**
  48. * Callback method for DELETE 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 handleDelete(CivetServer *server, struct mg_connection *conn);
  55. };
  56. /**
  57. * CivetServer
  58. *
  59. * Basic class for embedded web server. This has a URL mapping built-in.
  60. */
  61. class CivetServer {
  62. public:
  63. /**
  64. * Constructor
  65. *
  66. * This automatically starts the sever.
  67. * It is good practice to call getContext() after this in case there
  68. * were errors starting the server.
  69. *
  70. * @param options - the web server options.
  71. * @param callbacks - optional web server callback methods.
  72. * Note that this class overrides begin_request callback.
  73. */
  74. CivetServer(const char **options, const struct mg_callbacks *callbacks = 0);
  75. /**
  76. * Destructor
  77. */
  78. virtual ~CivetServer();
  79. /**
  80. * close()
  81. *
  82. * Stops server and frees resources.
  83. */
  84. void close();
  85. /**
  86. * getContext()
  87. *
  88. * @return the context or 0 if not running.
  89. */
  90. const struct mg_context *getContext() const {
  91. return context;
  92. }
  93. /**
  94. * addHandler(const std::string &, CivetHandler *)
  95. *
  96. * Adds a URI handler. If there is existing URI handler, it will
  97. * be replaced with this one. The handler is "owned" by this server
  98. * and will be deallocated with it.
  99. *
  100. * URI's are ordered and partcial URI's are supported. For example,
  101. * consider two URIs in order: /a/b and /a; /a matches
  102. * /a, /a/b matches /a/b, /a/c matches /a. Reversing the order to
  103. * /a and /a/b; /a matches /a/b, /a/b matches /a. /a/c matches /a.
  104. *
  105. * @param uri - URI to match.
  106. * @param handler - handler instance to use. This will be free'ed
  107. * when the server closes and instances cannot be reused.
  108. */
  109. void addHandler(const std::string &uri, CivetHandler *handler);
  110. /**
  111. * removeHandler(const std::string &)
  112. *
  113. * Removes a handler, deleting it if found.
  114. *
  115. * @param - the exact URL used in addHandler().
  116. */
  117. void removeHandler(const std::string &uri);
  118. /**
  119. * getHandler(const std::string &uri)
  120. *
  121. * @param uri - the URI
  122. * @returns the handler that matches the requested URI or 0 if none were found.
  123. */
  124. CivetHandler *getHandler(const std::string &uri) const {
  125. return getHandler(uri.data(), uri.length());
  126. }
  127. /**
  128. * getHandler(const char *uri, unsigned urilen)
  129. *
  130. * @param uri - the URI
  131. * @param urilen - the length of the URI
  132. * @returns the handler that matches the requested URI or 0 if none were found.
  133. */
  134. CivetHandler *getHandler(const char *uri, unsigned urilen) const;
  135. protected:
  136. /**
  137. * handleRequest(struct mg_connection *)
  138. *
  139. * Handles the incomming request.
  140. *
  141. * @param conn - the connection information
  142. * @returns true if implemented, false otherwise
  143. */
  144. virtual bool handleRequest(struct mg_connection *conn);
  145. /**
  146. * Returns the index of the handler that matches the
  147. * URI exactly.
  148. *
  149. * @param uri - the url to match
  150. */
  151. int getIndex(const std::string &uri) const;
  152. std::vector<std::string> uris;
  153. std::vector<CivetHandler *> handlers;
  154. struct mg_context *context;
  155. private:
  156. static int begin_request_callback(struct mg_connection *conn);
  157. };
  158. #endif /* __cplusplus */
  159. #endif /* _CIVETWEB_SERVER_H_ */