NVIDIA DeepStream SDK API Reference

9.2 Release
includes/CivetServer.h
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
3  * SPDX-License-Identifier: Apache-2.0
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 #ifndef CIVETSERVER_HEADER_INCLUDED
19 #define CIVETSERVER_HEADER_INCLUDED
20 #ifdef __cplusplus
21 
22 #include "civetweb.h"
23 #include <map>
24 #include <stdexcept>
25 #include <string>
26 #include <vector>
27 
28 #ifndef CIVETWEB_CXX_API
29 #if defined(_WIN32)
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)
34 #else
35 #define CIVETWEB_CXX_API
36 #endif
37 #elif __GNUC__ >= 4
38 #define CIVETWEB_CXX_API __attribute__((visibility("default")))
39 #else
40 #define CIVETWEB_CXX_API
41 #endif
42 #endif
43 
44 // forward declaration
45 class CivetServer;
46 
50 class CIVETWEB_CXX_API CivetException : public std::runtime_error
51 {
52  public:
53  CivetException(const std::string &msg) : std::runtime_error(msg)
54  {
55  }
56 };
57 
62 class CIVETWEB_CXX_API CivetHandler
63 {
64  public:
68  virtual ~CivetHandler()
69  {
70  }
71 
79  virtual bool handleGet(CivetServer *server, struct mg_connection *conn);
80 
89  virtual bool handleGet(CivetServer *server,
90  struct mg_connection *conn,
91  int *status_code);
92 
100  virtual bool handlePost(CivetServer *server, struct mg_connection *conn);
101 
110  virtual bool handlePost(CivetServer *server,
111  struct mg_connection *conn,
112  int *status_code);
113 
121  virtual bool handleHead(CivetServer *server, struct mg_connection *conn);
122 
131  virtual bool handleHead(CivetServer *server,
132  struct mg_connection *conn,
133  int *status_code);
134 
142  virtual bool handlePut(CivetServer *server, struct mg_connection *conn);
143 
152  virtual bool handlePut(CivetServer *server,
153  struct mg_connection *conn,
154  int *status_code);
155 
163  virtual bool handleDelete(CivetServer *server, struct mg_connection *conn);
164 
173  virtual bool handleDelete(CivetServer *server,
174  struct mg_connection *conn,
175  int *status_code);
176 
184  virtual bool handleOptions(CivetServer *server, struct mg_connection *conn);
185 
194  virtual bool handleOptions(CivetServer *server,
195  struct mg_connection *conn,
196  int *status_code);
197 
205  virtual bool handlePatch(CivetServer *server, struct mg_connection *conn);
206 
215  virtual bool handlePatch(CivetServer *server,
216  struct mg_connection *conn,
217  int *status_code);
218 };
219 
224 class CIVETWEB_CXX_API CivetAuthHandler
225 {
226  public:
230  virtual ~CivetAuthHandler()
231  {
232  }
233 
242  virtual bool authorize(CivetServer *server, struct mg_connection *conn) = 0;
243 };
244 
249 class CIVETWEB_CXX_API CivetWebSocketHandler
250 {
251  public:
255  virtual ~CivetWebSocketHandler()
256  {
257  }
258 
267  virtual bool handleConnection(CivetServer *server,
268  const struct mg_connection *conn);
269 
277  virtual void handleReadyState(CivetServer *server,
278  struct mg_connection *conn);
279 
290  virtual bool handleData(CivetServer *server,
291  struct mg_connection *conn,
292  int bits,
293  char *data,
294  size_t data_len);
295 
302  virtual void handleClose(CivetServer *server,
303  const struct mg_connection *conn);
304 };
305 
311 struct CIVETWEB_CXX_API CivetCallbacks : public mg_callbacks {
312  CivetCallbacks();
313 };
314 
320 class CIVETWEB_CXX_API CivetServer
321 {
322  public:
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);
346 
350  virtual ~CivetServer();
351 
357  void close();
358 
364  const struct mg_context *
365  getContext() const
366  {
367  return context;
368  }
369 
381  void addHandler(const std::string &uri, CivetHandler *handler);
382 
383  void
384  addHandler(const std::string &uri, CivetHandler &handler)
385  {
386  addHandler(uri, &handler);
387  }
388 
401  void addWebSocketHandler(const std::string &uri,
402  CivetWebSocketHandler *handler);
403 
404  void
405  addWebSocketHandler(const std::string &uri, CivetWebSocketHandler &handler)
406  {
407  addWebSocketHandler(uri, &handler);
408  }
409 
417  void removeHandler(const std::string &uri);
418 
426  void removeWebSocketHandler(const std::string &uri);
427 
439  void addAuthHandler(const std::string &uri, CivetAuthHandler *handler);
440 
441  void
442  addAuthHandler(const std::string &uri, CivetAuthHandler &handler)
443  {
444  addAuthHandler(uri, &handler);
445  }
446 
454  void removeAuthHandler(const std::string &uri);
455 
464  std::vector<int> getListeningPorts();
465 
474  std::vector<struct mg_server_port> getListeningPortsFull();
475 
488  static int getCookie(struct mg_connection *conn,
489  const std::string &cookieName,
490  std::string &cookieValue);
491 
498  static const char *getHeader(struct mg_connection *conn,
499  const std::string &headerName);
500 
506  static const char *getMethod(struct mg_connection *conn);
507 
531  static bool getParam(struct mg_connection *conn,
532  const char *name,
533  std::string &dst,
534  size_t occurrence = 0);
535 
550  static bool
551  getParam(const std::string &data,
552  const char *name,
553  std::string &dst,
554  size_t occurrence = 0)
555  {
556  return getParam(data.c_str(), data.length(), name, dst, occurrence);
557  }
558 
574  static bool getParam(const char *data,
575  size_t data_len,
576  const char *name,
577  std::string &dst,
578  size_t occurrence = 0);
579 
590  static std::string getPostData(struct mg_connection *conn);
591 
602  static void
603  urlDecode(const std::string &src,
604  std::string &dst,
605  bool is_form_url_encoded = true)
606  {
607  urlDecode(src.c_str(), src.length(), dst, is_form_url_encoded);
608  }
609 
621  static void urlDecode(const char *src,
622  size_t src_len,
623  std::string &dst,
624  bool is_form_url_encoded = true);
625 
636  static void urlDecode(const char *src,
637  std::string &dst,
638  bool is_form_url_encoded = true);
639 
647  static void
648  urlEncode(const std::string &src, std::string &dst, bool append = false)
649  {
650  urlEncode(src.c_str(), src.length(), dst, append);
651  }
652 
660  static void
661  urlEncode(const char *src, std::string &dst, bool append = false);
662 
671  static void urlEncode(const char *src,
672  size_t src_len,
673  std::string &dst,
674  bool append = false);
675 
676  // generic user context which can be set/read,
677  // the server does nothing with this apart from keep it.
678  const void *
679  getUserContext() const
680  {
681  return UserContext;
682  }
683 
684  protected:
685  class CivetConnection
686  {
687  public:
688  std::vector<char> postData;
689  };
690 
691  struct mg_context *context;
692  std::map<const struct mg_connection *, CivetConnection> connections;
693 
694  // generic user context which can be set/read,
695  // the server does nothing with this apart from keep it.
696  const void *UserContext;
697 
698  private:
708  static int requestHandler(struct mg_connection *conn, void *cbdata);
709 
710  static int webSocketConnectionHandler(const struct mg_connection *conn,
711  void *cbdata);
712  static void webSocketReadyHandler(struct mg_connection *conn, void *cbdata);
713  static int webSocketDataHandler(struct mg_connection *conn,
714  int bits,
715  char *data,
716  size_t data_len,
717  void *cbdata);
718  static void webSocketCloseHandler(const struct mg_connection *conn,
719  void *cbdata);
729  static int authHandler(struct mg_connection *conn, void *cbdata);
730 
738  static void closeHandler(const struct mg_connection *conn);
739 
743  void (*userCloseHandler)(const struct mg_connection *conn);
744 };
745 
746 #endif /* __cplusplus */
747 #endif /* CIVETSERVER_HEADER_INCLUDED */
context
Definition: includes/opentelemetry/context/context.h:16
pydeepstreamdoc::nvmeta::BatchMetaDoc::append
constexpr const char * append
Definition: service-maker/sources/python/src/bindings/docstrings/metadatadoc.h:63
std
Extensions to the C++ standard library.
Definition: includes/opentelemetry/nostd/string_view.h:212
mg_callbacks
Definition: includes/civetweb.h:235
data
JSAMPIMAGE data
Definition: sources/libs/nvll_batch_jpeg_encode/oss/jpeglib.h:967