WPILibC++ 2027.0.0-alpha-4
Loading...
Searching...
No Matches
WebSocketServer.hpp
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#pragma once
6
7#include <functional>
8#include <initializer_list>
9#include <memory>
10#include <span>
11#include <string>
12#include <string_view>
13#include <utility>
14
16#include "wpi/net/WebSocket.hpp"
17#include "wpi/util/Signal.h"
19#include "wpi/util/SmallVector.hpp"
20
21namespace wpi::net {
22
23namespace uv {
24class Stream;
25} // namespace uv
26
27/**
28 * WebSocket HTTP server helper. Handles websocket-specific headers. User
29 * must provide the HttpParser.
30 */
32 public:
33 /**
34 * Constructor.
35 * @param req HttpParser for request
36 */
38
39 /**
40 * Get whether or not this was a websocket upgrade.
41 * Only valid during and after the upgrade event.
42 */
43 bool IsWebsocket() const { return m_websocket; }
44
45 /**
46 * Try to find a match to the list of sub-protocols provided by the client.
47 * The list is priority ordered, so the first match wins.
48 * Only valid during and after the upgrade event.
49 * @param protocols Acceptable protocols
50 * @return Pair; first item is true if a match was made, false if not.
51 * Second item is the matched protocol if a match was made, otherwise
52 * is empty.
53 */
54 std::pair<bool, std::string_view> MatchProtocol(
55 std::span<const std::string_view> protocols);
56
57 /**
58 * Try to find a match to the list of sub-protocols provided by the client.
59 * The list is priority ordered, so the first match wins.
60 * Only valid during and after the upgrade event.
61 * @param protocols Acceptable protocols
62 * @return Pair; first item is true if a match was made, false if not.
63 * Second item is the matched protocol if a match was made, otherwise
64 * is empty.
65 */
66 std::pair<bool, std::string_view> MatchProtocol(
67 std::span<const std::string> protocols);
68
69 /**
70 * Try to find a match to the list of sub-protocols provided by the client.
71 * The list is priority ordered, so the first match wins.
72 * Only valid during and after the upgrade event.
73 * @param protocols Acceptable protocols
74 * @return Pair; first item is true if a match was made, false if not.
75 * Second item is the matched protocol if a match was made, otherwise
76 * is empty.
77 */
78 std::pair<bool, std::string_view> MatchProtocol(
79 std::initializer_list<std::string_view> protocols) {
80 return MatchProtocol({protocols.begin(), protocols.end()});
81 }
82
83 /**
84 * Accept the upgrade. Disconnect other readers (such as the HttpParser
85 * reader) before calling this. See also WebSocket::CreateServer().
86 * @param stream Connection stream
87 * @param protocol The subprotocol to send to the client
88 */
89 std::shared_ptr<WebSocket> Accept(uv::Stream& stream,
90 std::string_view protocol = {}) {
91 return WebSocket::CreateServer(stream, m_key, m_version, protocol);
92 }
93
94 bool IsUpgrade() const { return m_gotHost && m_websocket; }
95
96 /**
97 * Upgrade event. Call Accept() to accept the upgrade.
98 */
100
101 private:
102 bool m_gotHost = false;
103 bool m_websocket = false;
104 wpi::util::SmallVector<std::string, 2> m_protocols;
107};
108
109/**
110 * Dedicated WebSocket server.
111 */
112class WebSocketServer : public std::enable_shared_from_this<WebSocketServer> {
113 struct private_init {};
114
115 public:
116 /**
117 * Server options.
118 */
120 /**
121 * Checker for URL. Return true if URL should be accepted. By default all
122 * URLs are accepted.
123 */
124 std::function<bool(std::string_view)> checkUrl;
125
126 /**
127 * Checker for Host header. Return true if Host should be accepted. By
128 * default all hosts are accepted.
129 */
130 std::function<bool(std::string_view)> checkHost;
131 };
132
133 /**
134 * Private constructor.
135 */
137 std::span<const std::string_view> protocols,
138 ServerOptions options, const private_init&);
139
140 /**
141 * Starts a dedicated WebSocket server on the provided connection. The
142 * connection should be an accepted client stream.
143 * This also sets the stream user data to the socket server.
144 * A connected event is emitted when the connection is opened.
145 * @param stream Connection stream
146 * @param protocols Acceptable subprotocols
147 * @param options Handshake options
148 */
149 static std::shared_ptr<WebSocketServer> Create(
150 uv::Stream& stream, std::span<const std::string_view> protocols = {},
151 const ServerOptions& options = {});
152
153 /**
154 * Starts a dedicated WebSocket server on the provided connection. The
155 * connection should be an accepted client stream.
156 * This also sets the stream user data to the socket server.
157 * A connected event is emitted when the connection is opened.
158 * @param stream Connection stream
159 * @param protocols Acceptable subprotocols
160 * @param options Handshake options
161 */
162 static std::shared_ptr<WebSocketServer> Create(
163 uv::Stream& stream, std::initializer_list<std::string_view> protocols,
164 const ServerOptions& options = {}) {
165 return Create(stream, {protocols.begin(), protocols.end()}, options);
166 }
167
168 /**
169 * Connected event. First parameter is the URL, second is the websocket.
170 */
172
173 private:
174 uv::Stream& m_stream;
176 WebSocketServerHelper m_helper;
177 wpi::util::SmallVector<std::string, 2> m_protocols;
178 ServerOptions m_options;
179 bool m_aborted = false;
183
184 void Abort(uint16_t code, std::string_view reason);
185};
186
187} // namespace wpi::net
This file defines the SmallString 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.hpp:22
@ kRequest
Definition HttpParser.hpp:25
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.hpp:31
std::shared_ptr< WebSocket > Accept(uv::Stream &stream, std::string_view protocol={})
Accept the upgrade.
Definition WebSocketServer.hpp:89
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.hpp:78
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.
bool IsWebsocket() const
Get whether or not this was a websocket upgrade.
Definition WebSocketServer.hpp:43
wpi::util::sig::Signal upgrade
Upgrade event.
Definition WebSocketServer.hpp:99
WebSocketServerHelper(HttpParser &req)
Constructor.
bool IsUpgrade() const
Definition WebSocketServer.hpp:94
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.
WebSocketServer(uv::Stream &stream, std::span< const std::string_view > protocols, ServerOptions options, const private_init &)
Private constructor.
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.
wpi::util::sig::Signal< std::string_view, WebSocket & > connected
Connected event.
Definition WebSocketServer.hpp:171
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.hpp:162
Stream handle.
Definition Stream.hpp:66
SmallString - A SmallString is just a SmallVector with methods and accessors that make it work better...
Definition SmallString.hpp:27
ScopedConnection is a RAII version of Connection It disconnects the slot from the signal upon destruc...
Definition Signal.h:257
Definition Prepare.hpp:14
Definition raw_socket_ostream.hpp:9
SignalBase< detail::NullMutex, T... > Signal
Specialization of SignalBase to be used in single threaded contexts.
Definition Signal.h:809
Server options.
Definition WebSocketServer.hpp:119
std::function< bool(std::string_view)> checkHost
Checker for Host header.
Definition WebSocketServer.hpp:130
std::function< bool(std::string_view)> checkUrl
Checker for URL.
Definition WebSocketServer.hpp:124