18 #ifndef CIVETSERVER_HEADER_INCLUDED
19 #define CIVETSERVER_HEADER_INCLUDED
28 #ifndef CIVETWEB_CXX_API
30 #if defined(CIVETWEB_CXX_DLL_EXPORTS)
31 #define CIVETWEB_CXX_API __declspec(dllexport)
32 #elif defined(CIVETWEB_CXX_DLL_IMPORTS)
33 #define CIVETWEB_CXX_API __declspec(dllimport)
35 #define CIVETWEB_CXX_API
38 #define CIVETWEB_CXX_API __attribute__((visibility("default")))
40 #define CIVETWEB_CXX_API
50 class CIVETWEB_CXX_API CivetException :
public std::runtime_error
53 CivetException(
const std::string &msg) :
std::runtime_error(msg)
62 class CIVETWEB_CXX_API CivetHandler
68 virtual ~CivetHandler()
79 virtual bool handleGet(CivetServer *server,
struct mg_connection *conn);
89 virtual bool handleGet(CivetServer *server,
90 struct mg_connection *conn,
100 virtual bool handlePost(CivetServer *server,
struct mg_connection *conn);
110 virtual bool handlePost(CivetServer *server,
111 struct mg_connection *conn,
121 virtual bool handleHead(CivetServer *server,
struct mg_connection *conn);
131 virtual bool handleHead(CivetServer *server,
132 struct mg_connection *conn,
142 virtual bool handlePut(CivetServer *server,
struct mg_connection *conn);
152 virtual bool handlePut(CivetServer *server,
153 struct mg_connection *conn,
163 virtual bool handleDelete(CivetServer *server,
struct mg_connection *conn);
173 virtual bool handleDelete(CivetServer *server,
174 struct mg_connection *conn,
184 virtual bool handleOptions(CivetServer *server,
struct mg_connection *conn);
194 virtual bool handleOptions(CivetServer *server,
195 struct mg_connection *conn,
205 virtual bool handlePatch(CivetServer *server,
struct mg_connection *conn);
215 virtual bool handlePatch(CivetServer *server,
216 struct mg_connection *conn,
224 class CIVETWEB_CXX_API CivetAuthHandler
230 virtual ~CivetAuthHandler()
242 virtual bool authorize(CivetServer *server,
struct mg_connection *conn) = 0;
249 class CIVETWEB_CXX_API CivetWebSocketHandler
255 virtual ~CivetWebSocketHandler()
267 virtual bool handleConnection(CivetServer *server,
268 const struct mg_connection *conn);
277 virtual void handleReadyState(CivetServer *server,
278 struct mg_connection *conn);
290 virtual bool handleData(CivetServer *server,
291 struct mg_connection *conn,
302 virtual void handleClose(CivetServer *server,
303 const struct mg_connection *conn);
311 struct CIVETWEB_CXX_API CivetCallbacks :
public mg_callbacks {
320 class CIVETWEB_CXX_API CivetServer
340 CivetServer(
const char **options,
341 const struct CivetCallbacks *callbacks = 0,
342 const void *UserContext = 0);
343 CivetServer(
const std::vector<std::string> &options,
344 const struct CivetCallbacks *callbacks = 0,
345 const void *UserContext = 0);
350 virtual ~CivetServer();
364 const struct mg_context *
381 void addHandler(
const std::string &uri, CivetHandler *handler);
384 addHandler(
const std::string &uri, CivetHandler &handler)
386 addHandler(uri, &handler);
401 void addWebSocketHandler(
const std::string &uri,
402 CivetWebSocketHandler *handler);
405 addWebSocketHandler(
const std::string &uri, CivetWebSocketHandler &handler)
407 addWebSocketHandler(uri, &handler);
417 void removeHandler(
const std::string &uri);
426 void removeWebSocketHandler(
const std::string &uri);
439 void addAuthHandler(
const std::string &uri, CivetAuthHandler *handler);
442 addAuthHandler(
const std::string &uri, CivetAuthHandler &handler)
444 addAuthHandler(uri, &handler);
454 void removeAuthHandler(
const std::string &uri);
464 std::vector<int> getListeningPorts();
474 std::vector<struct mg_server_port> getListeningPortsFull();
488 static int getCookie(
struct mg_connection *conn,
489 const std::string &cookieName,
490 std::string &cookieValue);
498 static const char *getHeader(
struct mg_connection *conn,
499 const std::string &headerName);
506 static const char *getMethod(
struct mg_connection *conn);
531 static bool getParam(
struct mg_connection *conn,
534 size_t occurrence = 0);
551 getParam(
const std::string &
data,
554 size_t occurrence = 0)
556 return getParam(
data.c_str(),
data.length(), name, dst, occurrence);
574 static bool getParam(
const char *
data,
578 size_t occurrence = 0);
590 static std::string getPostData(
struct mg_connection *conn);
603 urlDecode(
const std::string &src,
605 bool is_form_url_encoded =
true)
607 urlDecode(src.c_str(), src.length(), dst, is_form_url_encoded);
621 static void urlDecode(
const char *src,
624 bool is_form_url_encoded =
true);
636 static void urlDecode(
const char *src,
638 bool is_form_url_encoded =
true);
648 urlEncode(
const std::string &src, std::string &dst,
bool append =
false)
650 urlEncode(src.c_str(), src.length(), dst,
append);
661 urlEncode(
const char *src, std::string &dst,
bool append =
false);
671 static void urlEncode(
const char *src,
679 getUserContext()
const
685 class CivetConnection
688 std::vector<char> postData;
692 std::map<const struct mg_connection *, CivetConnection> connections;
696 const void *UserContext;
708 static int requestHandler(
struct mg_connection *conn,
void *cbdata);
710 static int webSocketConnectionHandler(
const struct mg_connection *conn,
712 static void webSocketReadyHandler(
struct mg_connection *conn,
void *cbdata);
713 static int webSocketDataHandler(
struct mg_connection *conn,
718 static void webSocketCloseHandler(
const struct mg_connection *conn,
729 static int authHandler(
struct mg_connection *conn,
void *cbdata);
738 static void closeHandler(
const struct mg_connection *conn);
743 void (*userCloseHandler)(
const struct mg_connection *conn);