WPILibC++ 2023.4.3-108-ge5452e3
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::initializer_list<std::string_view> protocols) {
70 return MatchProtocol({protocols.begin(), protocols.end()});
71 }
72
73 /**
74 * Accept the upgrade. Disconnect other readers (such as the HttpParser
75 * reader) before calling this. See also WebSocket::CreateServer().
76 * @param stream Connection stream
77 * @param protocol The subprotocol to send to the client
78 */
79 std::shared_ptr<WebSocket> Accept(uv::Stream& stream,
80 std::string_view protocol = {}) {
81 return WebSocket::CreateServer(stream, m_key, m_version, protocol);
82 }
83
84 bool IsUpgrade() const { return m_gotHost && m_websocket; }
85
86 /**
87 * Upgrade event. Call Accept() to accept the upgrade.
88 */
90
91 private:
92 bool m_gotHost = false;
93 bool m_websocket = false;
95 SmallString<64> m_key;
96 SmallString<16> m_version;
97};
98
99/**
100 * Dedicated WebSocket server.
101 */
102class WebSocketServer : public std::enable_shared_from_this<WebSocketServer> {
103 struct private_init {};
104
105 public:
106 /**
107 * Server options.
108 */
110 /**
111 * Checker for URL. Return true if URL should be accepted. By default all
112 * URLs are accepted.
113 */
114 std::function<bool(std::string_view)> checkUrl;
115
116 /**
117 * Checker for Host header. Return true if Host should be accepted. By
118 * default all hosts are accepted.
119 */
120 std::function<bool(std::string_view)> checkHost;
121 };
122
123 /**
124 * Private constructor.
125 */
127 std::span<const std::string_view> protocols,
128 ServerOptions options, const private_init&);
129
130 /**
131 * Starts a dedicated WebSocket server on the provided connection. The
132 * connection should be an accepted client stream.
133 * This also sets the stream user data to the socket server.
134 * A connected event is emitted when the connection is opened.
135 * @param stream Connection stream
136 * @param protocols Acceptable subprotocols
137 * @param options Handshake options
138 */
139 static std::shared_ptr<WebSocketServer> Create(
140 uv::Stream& stream, std::span<const std::string_view> protocols = {},
141 const ServerOptions& options = {});
142
143 /**
144 * Starts a dedicated WebSocket server on the provided connection. The
145 * connection should be an accepted client stream.
146 * This also sets the stream user data to the socket server.
147 * A connected event is emitted when the connection is opened.
148 * @param stream Connection stream
149 * @param protocols Acceptable subprotocols
150 * @param options Handshake options
151 */
152 static std::shared_ptr<WebSocketServer> Create(
153 uv::Stream& stream, std::initializer_list<std::string_view> protocols,
154 const ServerOptions& options = {}) {
155 return Create(stream, {protocols.begin(), protocols.end()}, options);
156 }
157
158 /**
159 * Connected event. First parameter is the URL, second is the websocket.
160 */
162
163 private:
164 uv::Stream& m_stream;
166 WebSocketServerHelper m_helper;
167 SmallVector<std::string, 2> m_protocols;
168 ServerOptions m_options;
169 bool m_aborted = false;
170 sig::ScopedConnection m_dataConn;
171 sig::ScopedConnection m_errorConn;
172 sig::ScopedConnection m_endConn;
173
174 void Abort(uint16_t code, std::string_view reason);
175};
176
177} // namespace wpi
178
179#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 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:110
HTTP protocol parser.
Definition: HttpParser.h:24
@ kRequest
Definition: HttpParser.h:27
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:89
WebSocketServerHelper(HttpParser &req)
Constructor.
std::shared_ptr< WebSocket > Accept(uv::Stream &stream, std::string_view protocol={})
Accept the upgrade.
Definition: WebSocketServer.h:79
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::initializer_list< std::string_view > protocols)
Try to find a match to the list of sub-protocols provided by the client.
Definition: WebSocketServer.h:68
bool IsUpgrade() const
Definition: WebSocketServer.h:84
Dedicated WebSocket server.
Definition: WebSocketServer.h:102
sig::Signal< std::string_view, WebSocket & > connected
Connected event.
Definition: WebSocketServer.h:161
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:152
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
basic_string_view< char > string_view
Definition: core.h:520
::uint16_t uint16_t
Definition: Meta.h:54
Definition: AprilTagFieldLayout.h:18
Server options.
Definition: WebSocketServer.h:109
std::function< bool(std::string_view)> checkUrl
Checker for URL.
Definition: WebSocketServer.h:114
std::function< bool(std::string_view)> checkHost
Checker for Host header.
Definition: WebSocketServer.h:120