WPILibC++  2020.3.2
WebSocketServer.h
1 /*----------------------------------------------------------------------------*/
2 /* Copyright (c) 2018-2019 FIRST. All Rights Reserved. */
3 /* Open Source Software - may be modified and shared by FRC teams. The code */
4 /* must be accompanied by the FIRST BSD license file in the root directory of */
5 /* the project. */
6 /*----------------------------------------------------------------------------*/
7 
8 #ifndef WPIUTIL_WPI_WEBSOCKETSERVER_H_
9 #define WPIUTIL_WPI_WEBSOCKETSERVER_H_
10 
11 #include <functional>
12 #include <initializer_list>
13 #include <memory>
14 #include <string>
15 #include <utility>
16 
17 #include "wpi/ArrayRef.h"
18 #include "wpi/HttpParser.h"
19 #include "wpi/Signal.h"
20 #include "wpi/SmallString.h"
21 #include "wpi/SmallVector.h"
22 #include "wpi/StringRef.h"
23 #include "wpi/WebSocket.h"
24 
25 namespace wpi {
26 
27 namespace uv {
28 class Stream;
29 } // namespace uv
30 
36  public:
41  explicit WebSocketServerHelper(HttpParser& req);
42 
47  bool IsWebsocket() const { return m_websocket; }
48 
58  std::pair<bool, StringRef> MatchProtocol(ArrayRef<StringRef> protocols);
59 
69  std::pair<bool, StringRef> MatchProtocol(
70  std::initializer_list<StringRef> protocols) {
71  return MatchProtocol(makeArrayRef(protocols.begin(), protocols.end()));
72  }
73 
80  std::shared_ptr<WebSocket> Accept(uv::Stream& stream,
81  StringRef protocol = StringRef{}) {
82  return WebSocket::CreateServer(stream, m_key, m_version, protocol);
83  }
84 
85  bool IsUpgrade() const { return m_gotHost && m_websocket; }
86 
91 
92  private:
93  bool m_gotHost = false;
94  bool m_websocket = false;
95  SmallVector<std::string, 2> m_protocols;
96  SmallString<64> m_key;
97  SmallString<16> m_version;
98 };
99 
103 class WebSocketServer : public std::enable_shared_from_this<WebSocketServer> {
104  struct private_init {};
105 
106  public:
110  struct ServerOptions {
115  std::function<bool(StringRef)> checkUrl;
116 
121  std::function<bool(StringRef)> checkHost;
122  };
123 
128  const ServerOptions& options, const private_init&);
129 
139  static std::shared_ptr<WebSocketServer> Create(
140  uv::Stream& stream, ArrayRef<StringRef> protocols = ArrayRef<StringRef>{},
141  const ServerOptions& options = ServerOptions{});
142 
152  static std::shared_ptr<WebSocketServer> Create(
153  uv::Stream& stream, std::initializer_list<StringRef> protocols,
154  const ServerOptions& options = ServerOptions{}) {
155  return Create(stream, makeArrayRef(protocols.begin(), protocols.end()),
156  options);
157  }
158 
163 
164  private:
165  uv::Stream& m_stream;
166  HttpParser m_req{HttpParser::kRequest};
167  WebSocketServerHelper m_helper;
168  SmallVector<std::string, 2> m_protocols;
169  ServerOptions m_options;
170  bool m_aborted = false;
171  sig::ScopedConnection m_dataConn;
172  sig::ScopedConnection m_errorConn;
173  sig::ScopedConnection m_endConn;
174 
175  void Abort(uint16_t code, StringRef reason);
176 };
177 
178 } // namespace wpi
179 
180 #endif // WPIUTIL_WPI_WEBSOCKETSERVER_H_
wpi::WebSocketServer::Create
static std::shared_ptr< WebSocketServer > Create(uv::Stream &stream, std::initializer_list< StringRef > protocols, const ServerOptions &options=ServerOptions{})
Starts a dedicated WebSocket server on the provided connection.
Definition: WebSocketServer.h:152
wpi::uv::Stream
Stream handle.
Definition: Stream.h:69
wpi::WebSocketServer::ServerOptions
Server options.
Definition: WebSocketServer.h:110
wpi::WebSocketServerHelper
WebSocket HTTP server helper.
Definition: WebSocketServer.h:35
wpi::WebSocketServerHelper::WebSocketServerHelper
WebSocketServerHelper(HttpParser &req)
Constructor.
wpi::WebSocketServer::Create
static std::shared_ptr< WebSocketServer > Create(uv::Stream &stream, ArrayRef< StringRef > protocols=ArrayRef< StringRef >{}, const ServerOptions &options=ServerOptions{})
Starts a dedicated WebSocket server on the provided connection.
wpi::WebSocket::CreateServer
static std::shared_ptr< WebSocket > CreateServer(uv::Stream &stream, StringRef key, StringRef version, StringRef protocol=StringRef{})
Starts a server connection by performing the initial server side handshake.
wpi::ArrayRef
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:42
wpi::WebSocketServerHelper::MatchProtocol
std::pair< bool, StringRef > MatchProtocol(std::initializer_list< StringRef > protocols)
Try to find a match to the list of sub-protocols provided by the client.
Definition: WebSocketServer.h:69
wpi::WebSocketServerHelper::upgrade
sig::Signal upgrade
Upgrade event.
Definition: WebSocketServer.h:90
wpi::WebSocketServerHelper::IsWebsocket
bool IsWebsocket() const
Get whether or not this was a websocket upgrade.
Definition: WebSocketServer.h:47
wpi::HttpParser
HTTP protocol parser.
Definition: HttpParser.h:25
wpi::makeArrayRef
ArrayRef< T > makeArrayRef(const T &OneElt)
Construct an ArrayRef from a single element.
Definition: ArrayRef.h:447
wpi::WebSocketServerHelper::MatchProtocol
std::pair< bool, StringRef > MatchProtocol(ArrayRef< StringRef > protocols)
Try to find a match to the list of sub-protocols provided by the client.
wpi::WebSocketServer::connected
sig::Signal< StringRef, WebSocket & > connected
Connected event.
Definition: WebSocketServer.h:162
wpi
WPILib C++ utilities (wpiutil) namespace.
Definition: EventLoopRunner.h:17
wpi::SmallString< 64 >
wpi::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
wpi::WebSocketServer::ServerOptions::checkUrl
std::function< bool(StringRef)> checkUrl
Checker for URL.
Definition: WebSocketServer.h:115
wpi::WebSocketServerHelper::Accept
std::shared_ptr< WebSocket > Accept(uv::Stream &stream, StringRef protocol=StringRef{})
Accept the upgrade.
Definition: WebSocketServer.h:80
wpi::sig::ScopedConnection
ScopedConnection is a RAII version of Connection It disconnects the slot from the signal upon destruc...
Definition: Signal.h:257
wpi::WebSocketServer
Dedicated WebSocket server.
Definition: WebSocketServer.h:103
wpi::WebSocketServer::ServerOptions::checkHost
std::function< bool(StringRef)> checkHost
Checker for Host header.
Definition: WebSocketServer.h:121
wpi::SmallVector< std::string, 2 >
wpi::WebSocketServer::WebSocketServer
WebSocketServer(uv::Stream &stream, ArrayRef< StringRef > protocols, const ServerOptions &options, const private_init &)
Private constructor.
wpi::sig::SignalBase
SignalBase is an implementation of the observer pattern, through the use of an emitting object and sl...
Definition: Signal.h:495