WPILibC++ 2027.0.0-alpha-4
Loading...
Searching...
No Matches
HttpWebSocketServerConnection.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 <initializer_list>
8#include <memory>
9#include <span>
10#include <string>
11#include <string_view>
12
14#include "wpi/net/WebSocket.hpp"
16#include "wpi/net/uv/Stream.hpp"
17#include "wpi/util/SmallVector.hpp"
18
19namespace wpi::net {
20
21/**
22 * A server-side HTTP connection that also accepts WebSocket upgrades.
23 *
24 * @tparam Derived derived class for std::enable_shared_from_this.
25 */
26template <typename Derived>
28 : public HttpServerConnection,
29 public std::enable_shared_from_this<Derived> {
30 public:
31 /**
32 * Constructor.
33 *
34 * @param stream network stream
35 * @param protocols Acceptable subprotocols
36 */
37 HttpWebSocketServerConnection(std::shared_ptr<uv::Stream> stream,
38 std::span<const std::string_view> protocols)
39 : HttpServerConnection{stream},
40 m_helper{m_request},
41 m_protocols{protocols.begin(), protocols.end()} {
42 // Handle upgrade event
43 m_helper.upgrade.connect([this] {
44 // Negotiate sub-protocol
45 wpi::util::SmallVector<std::string_view, 2> protocols{m_protocols.begin(),
46 m_protocols.end()};
47 std::string_view protocol = m_helper.MatchProtocol(protocols).second;
48
49 // Check that the upgrade is valid
50 if (!IsValidWsUpgrade(protocol)) {
51 return;
52 }
53
54 // Disconnect HttpServerConnection header reader
55 m_dataConn.disconnect();
56 m_messageCompleteConn.disconnect();
57
58 // Accepting the stream may destroy this (as it replaces the stream user
59 // data), so grab a shared pointer first.
60 auto self = this->shared_from_this();
61
62 // Accept the upgrade
63 auto ws = m_helper.Accept(m_stream, protocol);
64
65 // Set this as the websocket user data to keep it around
66 ws->SetData(self);
67
68 // Store in member
69 m_websocket = ws.get();
70
71 // Call derived class function
73 });
74 }
75
76 /**
77 * Constructor.
78 *
79 * @param stream network stream
80 * @param protocols Acceptable subprotocols
81 */
83 std::shared_ptr<uv::Stream> stream,
84 std::initializer_list<std::string_view> protocols)
86 {protocols.begin(), protocols.end()}) {}
87
88 protected:
89 /**
90 * Check that an incoming WebSocket upgrade is okay. This is called prior
91 * to accepting the upgrade (so prior to ProcessWsUpgrade()).
92 *
93 * The implementation should check other headers and return true if the
94 * WebSocket connection should be accepted.
95 *
96 * @param protocol negotiated subprotocol
97 */
98 virtual bool IsValidWsUpgrade(std::string_view protocol) { return true; }
99
100 /**
101 * Process an incoming WebSocket upgrade. This is called after the header
102 * reader has been disconnected and the websocket has been accepted.
103 *
104 * The implementation should set up appropriate callbacks on the websocket
105 * object to continue communication.
106 *
107 * @note When a WebSocket upgrade occurs, the stream user data is replaced
108 * with the websocket, and the websocket user data points to "this".
109 * Replace the websocket user data with caution!
110 */
111 virtual void ProcessWsUpgrade() = 0;
112
113 /**
114 * WebSocket connection; not valid until ProcessWsUpgrade is called.
115 */
117
118 private:
119 WebSocketServerHelper m_helper;
120 wpi::util::SmallVector<std::string, 2> m_protocols;
121};
122
123} // namespace wpi::net
uv::Stream & m_stream
The underlying stream for the connection.
Definition HttpServerConnection.hpp:140
wpi::util::sig::ScopedConnection m_dataConn
The header reader connection.
Definition HttpServerConnection.hpp:143
HttpParser m_request
The HTTP request.
Definition HttpServerConnection.hpp:131
HttpServerConnection(std::shared_ptr< uv::Stream > stream)
wpi::util::sig::Connection m_messageCompleteConn
The message complete connection.
Definition HttpServerConnection.hpp:149
WebSocket * m_websocket
WebSocket connection; not valid until ProcessWsUpgrade is called.
Definition HttpWebSocketServerConnection.hpp:116
HttpWebSocketServerConnection(std::shared_ptr< uv::Stream > stream, std::initializer_list< std::string_view > protocols)
Constructor.
Definition HttpWebSocketServerConnection.hpp:82
virtual void ProcessWsUpgrade()=0
Process an incoming WebSocket upgrade.
virtual bool IsValidWsUpgrade(std::string_view protocol)
Check that an incoming WebSocket upgrade is okay.
Definition HttpWebSocketServerConnection.hpp:98
HttpWebSocketServerConnection(std::shared_ptr< uv::Stream > stream, std::span< const std::string_view > protocols)
Constructor.
Definition HttpWebSocketServerConnection.hpp:37
RFC 6455 compliant WebSocket client and server implementation.
Definition WebSocket.hpp:32
WebSocket HTTP server helper.
Definition WebSocketServer.hpp:31
Definition raw_socket_ostream.hpp:9