WPILibC++  2018.4.1-1235-g11e5faf
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Modules Pages
WebSocketServer.h
1 /*----------------------------------------------------------------------------*/
2 /* Copyright (c) 2018 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 <memory>
13 #include <string>
14 #include <utility>
15 
16 #include "wpi/ArrayRef.h"
17 #include "wpi/HttpParser.h"
18 #include "wpi/Signal.h"
19 #include "wpi/SmallString.h"
20 #include "wpi/SmallVector.h"
21 #include "wpi/StringRef.h"
22 #include "wpi/WebSocket.h"
23 
24 namespace wpi {
25 
26 namespace uv {
27 class Stream;
28 } // namespace uv
29 
35  public:
40  explicit WebSocketServerHelper(HttpParser& req);
41 
46  bool IsWebsocket() const { return m_websocket; }
47 
57  std::pair<bool, StringRef> MatchProtocol(ArrayRef<StringRef> protocols);
58 
65  std::shared_ptr<WebSocket> Accept(uv::Stream& stream,
66  StringRef protocol = StringRef{}) {
67  return WebSocket::CreateServer(stream, m_key, m_version, protocol);
68  }
69 
70  bool IsUpgrade() const { return m_gotHost && m_websocket; }
71 
76 
77  private:
78  bool m_gotHost = false;
79  bool m_websocket = false;
80  SmallVector<std::string, 2> m_protocols;
81  SmallString<64> m_key;
82  SmallString<16> m_version;
83 };
84 
88 class WebSocketServer : public std::enable_shared_from_this<WebSocketServer> {
89  struct private_init {};
90 
91  public:
95  struct ServerOptions {
100  std::function<bool(StringRef)> checkUrl;
101 
106  std::function<bool(StringRef)> checkHost;
107  };
108 
113  const ServerOptions& options, const private_init&);
114 
124  static std::shared_ptr<WebSocketServer> Create(
125  uv::Stream& stream, ArrayRef<StringRef> protocols = ArrayRef<StringRef>{},
126  const ServerOptions& options = ServerOptions{});
127 
132 
133  private:
134  uv::Stream& m_stream;
135  HttpParser m_req{HttpParser::kRequest};
136  WebSocketServerHelper m_helper;
137  SmallVector<std::string, 2> m_protocols;
138  ServerOptions m_options;
139  bool m_aborted = false;
140  sig::Connection m_headerConn;
141 
142  void Abort(uint16_t code, StringRef reason);
143 };
144 
145 } // namespace wpi
146 
147 #endif // WPIUTIL_WPI_WEBSOCKETSERVER_H_
std::shared_ptr< WebSocket > Accept(uv::Stream &stream, StringRef protocol=StringRef{})
Accept the upgrade.
Definition: WebSocketServer.h:65
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.
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.
WebSocketServer(uv::Stream &stream, ArrayRef< StringRef > protocols, const ServerOptions &options, const private_init &)
Private constructor.
std::function< bool(StringRef)> checkHost
Checker for Host header.
Definition: WebSocketServer.h:106
Server options.
Definition: WebSocketServer.h:95
std::function< bool(StringRef)> checkUrl
Checker for URL.
Definition: WebSocketServer.h:100
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
Definition: ArrayRef.h:41
WPILib C++ utilities (wpiutil) namespace.
Definition: SmallString.h:21
Stream handle.
Definition: Stream.h:68
Dedicated WebSocket server.
Definition: WebSocketServer.h:88
A Connection object allows interaction with an ongoing slot connection.
Definition: Signal.h:198
WebSocketServerHelper(HttpParser &req)
Constructor.
sig::Signal< StringRef, WebSocket & > connected
Connected event.
Definition: WebSocketServer.h:131
sig::Signal upgrade
Upgrade event.
Definition: WebSocketServer.h:75
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
HTTP protocol parser.
Definition: HttpParser.h:25
SignalBase is an implementation of the observer pattern, through the use of an emitting object and sl...
Definition: Signal.h:495
bool IsWebsocket() const
Get whether or not this was a websocket upgrade.
Definition: WebSocketServer.h:46
std::pair< bool, StringRef > MatchProtocol(ArrayRef< StringRef > protocols)
Try to find a match to the list of sub-protocols provided by the client.
WebSocket HTTP server helper.
Definition: WebSocketServer.h:34