HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
NET_UrlHandler.h
Go to the documentation of this file.
1 /*
2  * PROPRIETARY INFORMATION. This software is proprietary to
3  * Side Effects Software Inc., and is not to be reproduced,
4  * transmitted, or disclosed in any way without written permission.
5  *
6  * NAME: NET_UrlHandler.h
7  *
8  * COMMENTS:
9  *
10  */
11 
12 #ifndef __NET_URLHANDLER_H__
13 #define __NET_URLHANDLER_H__
14 
15 #include "NET_API.h"
16 
17 #include "NET_WebAPIHandler.h"
18 #include "NET_WebAPI.h"
19 
20 #include <UT/UT_ArrayStringMap.h>
21 #include <UT/UT_StringArray.h>
22 #include <UT/UT_StringHolder.h>
23 #include <UT/UT_UniquePtr.h>
24 
25 class NET_HTTPRequest;
26 
27 // This is the base class used to handle HTTP requests.
28 class NET_API NET_BaseUrlHandler
29 {
30 public:
31  NET_BaseUrlHandler(const UT_StringRef &path, bool allows_partial = false);
32  NET_BaseUrlHandler(const UT_StringRef &path,
33  const UT_StringArray &methods,
34  bool allows_partial = false);
35  virtual ~NET_BaseUrlHandler() = default;
36 
37  // Handle a request.
38  virtual void handle(NET_HTTPRequest &req, NET_WebResponse &resp) = 0;
39 
40  const UT_StringHolder &path() const { return myPath; }
41 
42  // This makes no attempt at checking if the method is already added.
43  void addMethod(const UT_StringRef &method);
44  void removeMethod(const UT_StringRef &method);
45 
46  bool canHandleMethod(const UT_StringRef &method) const;
47 
48  // This tells the server if it allows partial prefix paths.
50 
51 private:
52  // This is the path the handler will try to handle the request on.
53  UT_StringHolder myPath;
54  // All the methods this handler will try to handle.
55  UT_StringArray myMethods;
56 };
57 
58 // This is a generic URL request handler. Use this if you want to do something
59 // on a specific route.
60 class NET_API NET_UrlHandler : public NET_BaseUrlHandler
61 {
62 public:
63  using Callback = std::function<NET_WebResponse(NET_HTTPRequest &req)>;
64 
66  const UT_StringArray &methods,
67  bool allows_partial = false);
69  const UT_StringArray &methods,
70  Callback clb,
71  bool allows_partial = false);
72  void registerCallback(Callback callback);
73 
74  void handle(NET_HTTPRequest &req, NET_WebResponse &resp) override;
75 
76 private:
77  Callback myCallback;
78 };
79 
80 // This is a generic API URL request handler. Use this if you want to handle
81 // multiple requests on a single API route.
82 class NET_API NET_APIUrlHandler : public NET_BaseUrlHandler
83 {
84 public:
86  const UT_StringArray &methods,
87  bool allows_partial = false,
88  unsigned api_version = 1);
89 
90  NET_APIUrlHandler(const NET_APIUrlHandler &) = delete;
91  NET_APIUrlHandler &operator=(const NET_APIUrlHandler &) = delete;
92 
93  void handle(NET_HTTPRequest &req, NET_WebResponse &resp) override;
94 
95  template <typename Cls, typename... Args>
96  void registerFunction(const UT_StringRef &name,
98  Args...),
99  Cls *owner,
100  const NET_WebAPIHandler::ParamArray &parr);
101 
102  /// Adds a method to get the API version details for this path.
103  NET_WebResponse cmd_api_info(NET_HTTPRequest &req, bool include_func);
104 protected:
105  bool handleAPI(NET_HTTPRequest &req, NET_WebResponse &resp);
106 
107 private:
108  unsigned myAPIVersion;
110 };
111 
112 template <typename Cls, typename... Args>
113 inline void
115  const UT_StringRef &name,
116  NET_WebResponse (Cls::*func)(NET_HTTPRequest &req, Args...),
117  Cls *owner,
118  const NET_WebAPIHandler::ParamArray &parr)
119 {
120  myFunctions.emplace(
121  name,
123  name, parr,
124  [func, owner](NET_HTTPRequest &req, Args... args) -> NET_WebResponse {
125  return (owner->*func)(req, std::forward<Args>(args)...);
126  }));
127 }
128 
129 // This handles static files
131 {
132 public:
133  NET_UrlFileHandler(const UT_StringRef &url_prefix,
134  const char* directory,
135  bool allows_partial = true);
136 
137  void handle(NET_HTTPRequest &req, NET_WebResponse &resp) override;
138 
139  /// Construct a valid file path using the request information. Returns true
140  /// it was able to construct a valid path.
141  static bool buildValidFilePath(const UT_StringHolder& req_path,
142  const UT_StringHolder &prefix_path,
143  const UT_StringHolder &directory,
144  NET_WebResponse& resp,
145  UT_StringHolder &valid_path);
146 private:
147  UT_StringHolder myDirectory;
148 };
149 
150 #endif // __NET_URLHANDLER_H__
151 
Response object used for responding to request in the server.
GLuint const GLchar * name
Definition: glew.h:1814
const Args & args
Definition: printf.h:628
const UT_StringHolder & path() const
void registerFunction(const UT_StringRef &name, NET_WebResponse(Cls::*func)(NET_HTTPRequest &req, Args...), Cls *owner, const NET_WebAPIHandler::ParamArray &parr)
virtual void handle(NET_HTTPRequest &req, NET_WebResponse &resp)=0
std::enable_if< !std::is_array< T >::value, UT_UniquePtr< T >>::type UTmakeUnique(REST &&...args)
Definition: UT_UniquePtr.h:46
GLsizei const GLchar *const * path
Definition: glew.h:6461
GLenum func
Definition: glcorearb.h:782
std::pair< iterator, bool > emplace(Args &&...args)
Definition: UT_ArraySet.h:1025
std::function< NET_WebResponse(NET_HTTPRequest &req)> Callback