HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
NET_HTTPTaskHandler.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_HTTPTaskHandler.h
7  *
8  * COMMENTS:
9  *
10  */
11 
12 #ifndef __NET_HTTPTASKHANDLER_H__
13 #define __NET_HTTPTASKHANDLER_H__
14 
15 #include "NET_WebSocket.h"
16 #include "NET_WebTaskHandler.h"
17 
18 #include "NET_RadixTree.h"
19 
20 #include <UT/UT_NonCopyable.h>
21 
22 #include <functional>
23 
25 
26 /// This is a generic handler for serving only http requests
28 {
29 public:
31 
32  // Make sure we can determine the protocol id by the type
34  {
36  }
37 
38  const char* className() const override
39  {
40  return "NET_HTTPWebTaskHandler";
41  }
42 
43 protected:
44  void onProcess() override;
45 };
46 
47 /// This is the base web socket handler. This handler will handle http and
48 /// web socket connections. Please keep in mind that once upgraded to a
49 /// websocket the handler will stick to being a websocket for its entire
50 /// lifetime.
51 ///
52 /// NOTE: This assumes its used on the server side. Which means that writing
53 /// does NOT add a mask which in some implementations causes an error
54 /// (i.e. pythong). Do not add a mask when writing without being
55 /// certain who initiated the connection.
57 {
58 public:
60 
61  int64 calcElapsedTime(time_t) const override
62  {
63  return 0;
64  }
65 
66  const char* className() const override
67  {
68  return "NET_WebSocketHandler";
69  }
70 
71  class NET_API Router : public UT_NonCopyable
72  {
73  public:
74  using CreateCallback =
75  std::function<NET_WebTaskHandlerPtr(NET_WebEndpoint &)>;
76 
77  static Router &instance();
78 
79  template <typename... Args>
80  void emplace(Args &&... args)
81  {
82  myPathTree.insert(std::forward<Args>(args)...);
83  }
84 
85  NET_WebTaskHandlerPtr create(const char* url, NET_WebEndpoint& ep) const
86  {
87  auto node = myPathTree.match(url);
88  if (node == nullptr || !node->value().get())
89  return nullptr;
90  return node->value().get()(ep);
91  }
92 
93  bool contains(const char* url) const
94  {
95  return myPathTree.contains(url);
96  }
97  private:
99  };
100 protected:
101  void onProcess() override;
102 
103  // Callback for a message -- Either utf8 or binary
104  virtual void onMessage(const UT_StringRef &data, bool is_binary) {}
105  // Ping message
106  virtual void onPing(const UT_StringRef &data);
107  // Pong message
108  virtual void onPong(const UT_StringRef &data) {}
109 
110  // TODO (mattw): properly reply that we are terminating if the client did
111  // not initiate the closure.
112 
113  void write(const UT_StringRef &message);
114 
115 private:
116  uint32 myMaxFrameSize;
117  NET_WebSocket::Buffer myReadBuffer;
118 };
119 
121 {
122 public:
124 
125  void onMessage(const UT_StringRef& data, bool is_binary) override;
126 };
127 
128 /// This is used to generate only http web task handlers.
130 
131 /// Factory that supports both http request and routable websockets
133 {
134 public:
135 
137  NET_WebEndpoint &ep,
139  const char *url = nullptr) const override
140  {
142  return UTmakeUnique<NET_HTTPWebTaskHandler>(ep);
143  else if (id == NET_WebTaskFactory::WSProtocolId)
144  {
145  if (url == nullptr)
146  return nullptr;
148  }
149  return nullptr;
150  }
151 
153  const char* path = nullptr) const override
154  {
155  if (id == HttpProtocolId)
156  return true;
157  if (id == WSProtocolId)
158  {
159  // Check that the router has a path for that websocket. If you only
160  // want one websocket throughout when you insert the creator
161  // into the path tree just use allows partial prefix at the root
162  // path so everything matches that path.
164  }
165  return false;
166  }
167 };
168 
169 #endif // __NET_HTTPTASKHANDLER_H__
170 
NET_WebTaskHandler(NET_WebEndpoint &ep)
Holds information about the port. In HTTP case this will hold any handlers that are bound to that por...
Definition: NET_WebServer.h:85
const Args & args
Definition: printf.h:628
int64 calcElapsedTime(time_t) const override
static NET_WebTaskFactory::ProtocolId protocolId()
UT_UniquePtr< NET_WebTaskHandler > NET_WebTaskHandlerPtr
static constexpr ProtocolId HttpProtocolId
std::function< NET_WebTaskHandlerPtr(NET_WebEndpoint &)> CreateCallback
NET_WebSocketHandler(NET_WebEndpoint &ep)
static constexpr ProtocolId WSProtocolId
virtual void onMessage(const UT_StringRef &data, bool is_binary)
bool contains(const char *url) const
GLint GLenum GLsizei GLint GLsizei const void * data
Definition: glew.h:1379
This is a generic handler for serving only http requests.
Factory that supports both http request and routable websockets.
GLsizei GLenum GLuint GLuint GLsizei GLchar * message
Definition: glew.h:2581
long long int64
Definition: SYS_Types.h:116
bool supportsProtocol(NET_WebTaskFactory::ProtocolId id, const char *path=nullptr) const override
NET_WebTaskHandlerPtr createByProtocol(NET_WebEndpoint &ep, NET_WebTaskFactory::ProtocolId id, const char *url=nullptr) const override
GLsizei const GLchar *const * path
Definition: glew.h:6461
NET_WebTaskHandlerPtr create(const char *url, NET_WebEndpoint &ep) const
const char * className() const override
This is used to tell the web server what kind of task to create.
const char * className() const override
virtual void onPong(const UT_StringRef &data)
unsigned int uint32
Definition: SYS_Types.h:40
void write(T &out, bool v)
Definition: ImfXdr.h:332
static Router & instance()
virtual void onProcess()