WPILibC++ 2027.0.0-alpha-2
Loading...
Searching...
No Matches
WebSocketServer.h
Go to the documentation of this file.
1// Copyright (c) FIRST and other WPILib contributors.
2// Open Source Software; you can modify and/or share it under the terms of
3// the WPILib BSD license file in the root directory of this project.
4
5#ifndef WPINET_WEBSOCKETSERVER_H_
6#define WPINET_WEBSOCKETSERVER_H_
7
8#include <functional>
9#include <initializer_list>
10#include <memory>
11#include <span>
12#include <string>
13#include <string_view>
14#include <utility>
15
16#include <wpi/Signal.h>
17#include <wpi/SmallString.h>
18#include <wpi/SmallVector.h>
19
20#include "wpinet/HttpParser.h"
21#include "wpinet/WebSocket.h"
22
23namespace wpi {
24
25namespace uv {
26class Stream;
27} // namespace uv
28
29/**
30 * WebSocket HTTP server helper. Handles websocket-specific headers. User
31 * must provide the HttpParser.
32 */
34 public:
35 /**
36 * Constructor.
37 * @param req HttpParser for request
38 */
40
41 /**
42 * Get whether or not this was a websocket upgrade.
43 * Only valid during and after the upgrade event.
44 */
45 bool IsWebsocket() const { return m_websocket; }
46
47 /**
48 * Try to find a match to the list of sub-protocols provided by the client.
49 * The list is priority ordered, so the first match wins.
50 * Only valid during and after the upgrade event.
51 * @param protocols Acceptable protocols
52 * @return Pair; first item is true if a match was made, false if not.
53 * Second item is the matched protocol if a match was made, otherwise
54 * is empty.
55 */
56 std::pair<bool, std::string_view> MatchProtocol(
57 std::span<const std::string_view> protocols);
58
59 /**
60 * Try to find a match to the list of sub-protocols provided by the client.
61 * The list is priority ordered, so the first match wins.
62 * Only valid during and after the upgrade event.
63 * @param protocols Acceptable protocols
64 * @return Pair; first item is true if a match was made, false if not.
65 * Second item is the matched protocol if a match was made, otherwise
66 * is empty.
67 */
68 std::pair<bool, std::string_view> MatchProtocol(
69 std::span<const std::string> protocols);
70
71 /**
72 * Try to find a match to the list of sub-protocols provided by the client.
73 * The list is priority ordered, so the first match wins.
74 * Only valid during and after the upgrade event.
75 * @param protocols Acceptable protocols
76 * @return Pair; first item is true if a match was made, false if not.
77 * Second item is the matched protocol if a match was made, otherwise
78 * is empty.
79 */
80 std::pair<bool, std::string_view> MatchProtocol(
81 std::initializer_list<std::string_view> protocols) {
82 return MatchProtocol({protocols.begin(), protocols.end()});
83 }
84
85 /**
86 * Accept the upgrade. Disconnect other readers (such as the HttpParser
87 * reader) before calling this. See also WebSocket::CreateServer().
88 * @param stream Connection stream
89 * @param protocol The subprotocol to send to the client
90 */
91 std::shared_ptr<WebSocket> Accept(uv::Stream& stream,
92 std::string_view protocol = {}) {
93 return WebSocket::CreateServer(stream, m_key, m_version, protocol);
94 }
95
96 bool IsUpgrade() const { return m_gotHost && m_websocket; }
97
98 /**
99 * Upgrade event. Call Accept() to accept the upgrade.
100 */
102
103 private:
104 bool m_gotHost = false;
105 bool m_websocket = false;
106 SmallVector<std::string, 2> m_protocols;
107 SmallString<64> m_key;
108 SmallString<16> m_version;
109};
110
111/**
112 * Dedicated WebSocket server.
113 */
114class WebSocketServer : public std::enable_shared_from_this<WebSocketServer> {
115 struct private_init {};
116
117 public:
118 /**
119 * Server options.
120 */
122 /**
123 * Checker for URL. Return true if URL should be accepted. By default all
124 * URLs are accepted.
125 */
126 std::function<bool(std::string_view)> checkUrl;
127
128 /**
129 * Checker for Host header. Return true if Host should be accepted. By
130 * default all hosts are accepted.
131 */
132 std::function<bool(std::string_view)> checkHost;
133 };
134
135 /**
136 * Private constructor.
137 */
139 std::span<const std::string_view> protocols,
140 ServerOptions options, const private_init&);
141
142 /**
143 * Starts a dedicated WebSocket server on the provided connection. The
144 * connection should be an accepted client stream.
145 * This also sets the stream user data to the socket server.
146 * A connected event is emitted when the connection is opened.
147 * @param stream Connection stream
148 * @param protocols Acceptable subprotocols
149 * @param options Handshake options
150 */
151 static std::shared_ptr<WebSocketServer> Create(
152 uv::Stream& stream, std::span<const std::string_view> protocols = {},
153 const ServerOptions& options = {});
154
155 /**
156 * Starts a dedicated WebSocket server on the provided connection. The
157 * connection should be an accepted client stream.
158 * This also sets the stream user data to the socket server.
159 * A connected event is emitted when the connection is opened.
160 * @param stream Connection stream
161 * @param protocols Acceptable subprotocols
162 * @param options Handshake options
163 */
164 static std::shared_ptr<WebSocketServer> Create(
165 uv::Stream& stream, std::initializer_list<std::string_view> protocols,
166 const ServerOptions& options = {}) {
167 return Create(stream, {protocols.begin(), protocols.end()}, options);
168 }
169
170 /**
171 * Connected event. First parameter is the URL, second is the websocket.
172 */
174
175 private:
176 uv::Stream& m_stream;
178 WebSocketServerHelper m_helper;
179 SmallVector<std::string, 2> m_protocols;
180 ServerOptions m_options;
181 bool m_aborted = false;
182 sig::ScopedConnection m_dataConn;
183 sig::ScopedConnection m_errorConn;
184 sig::ScopedConnection m_endConn;
185
186 void Abort(uint16_t code, std::string_view reason);
187};
188
189} // namespace wpi
190
191#endif // WPINET_WEBSOCKETSERVER_H_
This file defines the SmallString class.
This file defines the SmallVector class.
and restrictions which apply to each piece of software is included later in this file and or inside of the individual applicable source files The disclaimer of warranty in the WPILib license above applies to all code in and nothing in any of the other licenses gives permission to use the names of FIRST nor the names of the WPILib contributors to endorse or promote products derived from this software The following pieces of software have additional or alternate and or glfw and nanopb were modified for use in Google Inc All rights reserved Redistribution and use in source and binary with or without are permitted provided that the following conditions are this list of conditions and the following disclaimer *Redistributions in binary form must reproduce the above copyright this list of conditions and the following disclaimer in the documentation and or other materials provided with the distribution *Neither the name of Google Inc nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS AS IS AND ANY EXPRESS OR IMPLIED BUT NOT LIMITED THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY OR CONSEQUENTIAL WHETHER IN STRICT OR EVEN IF ADVISED OF THE POSSIBILITY OF SUCH January AND DISTRIBUTION Definitions License shall mean the terms and conditions for and distribution as defined by Sections through of this document Licensor shall mean the copyright owner or entity authorized by the copyright owner that is granting the License Legal Entity shall mean the union of the acting entity and all other entities that control are controlled by or are under common control with that entity For the purposes of this definition control direct or to cause the direction or management of such whether by contract or including but not limited to software source code
Definition ThirdPartyNotices.txt:122
HTTP protocol parser.
Definition HttpParser.h:24
@ kRequest
Definition HttpParser.h:27
SmallString - A SmallString is just a SmallVector with methods and accessors that make it work better...
Definition SmallString.h:27
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition SmallVector.h:1198
static std::shared_ptr< WebSocket > CreateServer(uv::Stream &stream, std::string_view key, std::string_view version, std::string_view protocol={})
Starts a server connection by performing the initial server side handshake.
WebSocket HTTP server helper.
Definition WebSocketServer.h:33
sig::Signal upgrade
Upgrade event.
Definition WebSocketServer.h:101
WebSocketServerHelper(HttpParser &req)
Constructor.
std::shared_ptr< WebSocket > Accept(uv::Stream &stream, std::string_view protocol={})
Accept the upgrade.
Definition WebSocketServer.h:91
bool IsWebsocket() const
Get whether or not this was a websocket upgrade.
Definition WebSocketServer.h:45
std::pair< bool, std::string_view > MatchProtocol(std::span< const std::string_view > protocols)
Try to find a match to the list of sub-protocols provided by the client.
std::pair< bool, std::string_view > MatchProtocol(std::span< const std::string > protocols)
Try to find a match to the list of sub-protocols provided by the client.
std::pair< bool, std::string_view > MatchProtocol(std::initializer_list< std::string_view > protocols)
Try to find a match to the list of sub-protocols provided by the client.
Definition WebSocketServer.h:80
bool IsUpgrade() const
Definition WebSocketServer.h:96
Dedicated WebSocket server.
Definition WebSocketServer.h:114
sig::Signal< std::string_view, WebSocket & > connected
Connected event.
Definition WebSocketServer.h:173
static std::shared_ptr< WebSocketServer > Create(uv::Stream &stream, std::initializer_list< std::string_view > protocols, const ServerOptions &options={})
Starts a dedicated WebSocket server on the provided connection.
Definition WebSocketServer.h:164
static std::shared_ptr< WebSocketServer > Create(uv::Stream &stream, std::span< const std::string_view > protocols={}, const ServerOptions &options={})
Starts a dedicated WebSocket server on the provided connection.
WebSocketServer(uv::Stream &stream, std::span< const std::string_view > protocols, ServerOptions options, const private_init &)
Private constructor.
ScopedConnection is a RAII version of Connection It disconnects the slot from the signal upon destruc...
Definition Signal.h:257
SignalBase is an implementation of the observer pattern, through the use of an emitting object and sl...
Definition Signal.h:495
Stream handle.
Definition Stream.h:68
Definition ntcore_cpp.h:26
Server options.
Definition WebSocketServer.h:121
std::function< bool(std::string_view)> checkUrl
Checker for URL.
Definition WebSocketServer.h:126
std::function< bool(std::string_view)> checkHost
Checker for Host header.
Definition WebSocketServer.h:132