NVIDIA DeepStream SDK API Reference

7.0 Release
CivetServer.h
Go to the documentation of this file.
1 /* Copyright (c) 2013-2017 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 
7 #ifndef CIVETSERVER_HEADER_INCLUDED
8 #define CIVETSERVER_HEADER_INCLUDED
9 #ifdef __cplusplus
10 
11 #include "civetweb.h"
12 #include <map>
13 #include <stdexcept>
14 #include <string>
15 #include <vector>
16 
17 #ifndef CIVETWEB_CXX_API
18 #if defined(_WIN32)
19 #if defined(CIVETWEB_CXX_DLL_EXPORTS)
20 #define CIVETWEB_CXX_API __declspec(dllexport)
21 #elif defined(CIVETWEB_CXX_DLL_IMPORTS)
22 #define CIVETWEB_CXX_API __declspec(dllimport)
23 #else
24 #define CIVETWEB_CXX_API
25 #endif
26 #elif __GNUC__ >= 4
27 #define CIVETWEB_CXX_API __attribute__((visibility("default")))
28 #else
29 #define CIVETWEB_CXX_API
30 #endif
31 #endif
32 
33 // forward declaration
34 class CivetServer;
35 
39 class CIVETWEB_CXX_API CivetException : public std::runtime_error
40 {
41  public:
42  CivetException(const std::string &msg) : std::runtime_error(msg)
43  {
44  }
45 };
46 
51 class CIVETWEB_CXX_API CivetHandler
52 {
53  public:
57  virtual ~CivetHandler()
58  {
59  }
60 
68  virtual bool handleGet(CivetServer *server, struct mg_connection *conn);
69 
78  virtual bool handleGet(CivetServer *server,
79  struct mg_connection *conn,
80  int *status_code);
81 
89  virtual bool handlePost(CivetServer *server, struct mg_connection *conn);
90 
99  virtual bool handlePost(CivetServer *server,
100  struct mg_connection *conn,
101  int *status_code);
102 
110  virtual bool handleHead(CivetServer *server, struct mg_connection *conn);
111 
120  virtual bool handleHead(CivetServer *server,
121  struct mg_connection *conn,
122  int *status_code);
123 
131  virtual bool handlePut(CivetServer *server, struct mg_connection *conn);
132 
141  virtual bool handlePut(CivetServer *server,
142  struct mg_connection *conn,
143  int *status_code);
144 
152  virtual bool handleDelete(CivetServer *server, struct mg_connection *conn);
153 
162  virtual bool handleDelete(CivetServer *server,
163  struct mg_connection *conn,
164  int *status_code);
165 
173  virtual bool handleOptions(CivetServer *server, struct mg_connection *conn);
174 
183  virtual bool handleOptions(CivetServer *server,
184  struct mg_connection *conn,
185  int *status_code);
186 
194  virtual bool handlePatch(CivetServer *server, struct mg_connection *conn);
195 
204  virtual bool handlePatch(CivetServer *server,
205  struct mg_connection *conn,
206  int *status_code);
207 };
208 
213 class CIVETWEB_CXX_API CivetAuthHandler
214 {
215  public:
219  virtual ~CivetAuthHandler()
220  {
221  }
222 
231  virtual bool authorize(CivetServer *server, struct mg_connection *conn) = 0;
232 };
233 
238 class CIVETWEB_CXX_API CivetWebSocketHandler
239 {
240  public:
244  virtual ~CivetWebSocketHandler()
245  {
246  }
247 
256  virtual bool handleConnection(CivetServer *server,
257  const struct mg_connection *conn);
258 
266  virtual void handleReadyState(CivetServer *server,
267  struct mg_connection *conn);
268 
279  virtual bool handleData(CivetServer *server,
280  struct mg_connection *conn,
281  int bits,
282  char *data,
283  size_t data_len);
284 
291  virtual void handleClose(CivetServer *server,
292  const struct mg_connection *conn);
293 };
294 
300 struct CIVETWEB_CXX_API CivetCallbacks : public mg_callbacks {
301  CivetCallbacks();
302 };
303 
309 class CIVETWEB_CXX_API CivetServer
310 {
311  public:
329  CivetServer(const char **options,
330  const struct CivetCallbacks *callbacks = 0,
331  const void *UserContext = 0);
332  CivetServer(const std::vector<std::string> &options,
333  const struct CivetCallbacks *callbacks = 0,
334  const void *UserContext = 0);
335 
339  virtual ~CivetServer();
340 
346  void close();
347 
353  const struct mg_context *
354  getContext() const
355  {
356  return context;
357  }
358 
370  void addHandler(const std::string &uri, CivetHandler *handler);
371 
372  void
373  addHandler(const std::string &uri, CivetHandler &handler)
374  {
375  addHandler(uri, &handler);
376  }
377 
390  void addWebSocketHandler(const std::string &uri,
391  CivetWebSocketHandler *handler);
392 
393  void
394  addWebSocketHandler(const std::string &uri, CivetWebSocketHandler &handler)
395  {
396  addWebSocketHandler(uri, &handler);
397  }
398 
406  void removeHandler(const std::string &uri);
407 
415  void removeWebSocketHandler(const std::string &uri);
416 
428  void addAuthHandler(const std::string &uri, CivetAuthHandler *handler);
429 
430  void
431  addAuthHandler(const std::string &uri, CivetAuthHandler &handler)
432  {
433  addAuthHandler(uri, &handler);
434  }
435 
443  void removeAuthHandler(const std::string &uri);
444 
453  std::vector<int> getListeningPorts();
454 
463  std::vector<struct mg_server_port> getListeningPortsFull();
464 
477  static int getCookie(struct mg_connection *conn,
478  const std::string &cookieName,
479  std::string &cookieValue);
480 
487  static const char *getHeader(struct mg_connection *conn,
488  const std::string &headerName);
489 
495  static const char *getMethod(struct mg_connection *conn);
496 
520  static bool getParam(struct mg_connection *conn,
521  const char *name,
522  std::string &dst,
523  size_t occurrence = 0);
524 
539  static bool
540  getParam(const std::string &data,
541  const char *name,
542  std::string &dst,
543  size_t occurrence = 0)
544  {
545  return getParam(data.c_str(), data.length(), name, dst, occurrence);
546  }
547 
563  static bool getParam(const char *data,
564  size_t data_len,
565  const char *name,
566  std::string &dst,
567  size_t occurrence = 0);
568 
579  static std::string getPostData(struct mg_connection *conn);
580 
591  static void
592  urlDecode(const std::string &src,
593  std::string &dst,
594  bool is_form_url_encoded = true)
595  {
596  urlDecode(src.c_str(), src.length(), dst, is_form_url_encoded);
597  }
598 
610  static void urlDecode(const char *src,
611  size_t src_len,
612  std::string &dst,
613  bool is_form_url_encoded = true);
614 
625  static void urlDecode(const char *src,
626  std::string &dst,
627  bool is_form_url_encoded = true);
628 
636  static void
637  urlEncode(const std::string &src, std::string &dst, bool append = false)
638  {
639  urlEncode(src.c_str(), src.length(), dst, append);
640  }
641 
649  static void
650  urlEncode(const char *src, std::string &dst, bool append = false);
651 
660  static void urlEncode(const char *src,
661  size_t src_len,
662  std::string &dst,
663  bool append = false);
664 
665  // generic user context which can be set/read,
666  // the server does nothing with this apart from keep it.
667  const void *
668  getUserContext() const
669  {
670  return UserContext;
671  }
672 
673  protected:
674  class CivetConnection
675  {
676  public:
677  std::vector<char> postData;
678  };
679 
680  struct mg_context *context;
681  std::map<const struct mg_connection *, CivetConnection> connections;
682 
683  // generic user context which can be set/read,
684  // the server does nothing with this apart from keep it.
685  const void *UserContext;
686 
687  private:
697  static int requestHandler(struct mg_connection *conn, void *cbdata);
698 
699  static int webSocketConnectionHandler(const struct mg_connection *conn,
700  void *cbdata);
701  static void webSocketReadyHandler(struct mg_connection *conn, void *cbdata);
702  static int webSocketDataHandler(struct mg_connection *conn,
703  int bits,
704  char *data,
705  size_t data_len,
706  void *cbdata);
707  static void webSocketCloseHandler(const struct mg_connection *conn,
708  void *cbdata);
718  static int authHandler(struct mg_connection *conn, void *cbdata);
719 
727  static void closeHandler(const struct mg_connection *conn);
728 
732  void (*userCloseHandler)(const struct mg_connection *conn);
733 };
734 
735 #endif /* __cplusplus */
736 #endif /* CIVETSERVER_HEADER_INCLUDED */
mg_callbacks
Definition: civetweb.h:218
civetweb.h