2020-12-26 14:12:05 -08:00
|
|
|
// Copyright (c) FIRST and other WPILib contributors.
|
|
|
|
|
// Open Source Software; you can modify and/or share it under the terms of
|
|
|
|
|
// the WPILib BSD license file in the root directory of this project.
|
2020-07-05 22:10:30 -07:00
|
|
|
|
2022-05-07 10:54:14 -07:00
|
|
|
#ifndef WPINET_HTTPWEBSOCKETSERVERCONNECTION_H_
|
|
|
|
|
#define WPINET_HTTPWEBSOCKETSERVERCONNECTION_H_
|
2020-07-05 22:10:30 -07:00
|
|
|
|
|
|
|
|
#include <initializer_list>
|
|
|
|
|
#include <memory>
|
2022-10-15 16:33:14 -07:00
|
|
|
#include <span>
|
2020-07-05 22:10:30 -07:00
|
|
|
#include <string>
|
2021-06-06 16:13:58 -07:00
|
|
|
#include <string_view>
|
2020-07-05 22:10:30 -07:00
|
|
|
|
2025-11-07 19:56:21 -05:00
|
|
|
#include "wpi/util/SmallVector.hpp"
|
2022-05-07 10:54:14 -07:00
|
|
|
|
2025-11-07 19:56:21 -05:00
|
|
|
#include "wpi/net/HttpServerConnection.hpp"
|
|
|
|
|
#include "wpi/net/WebSocket.hpp"
|
|
|
|
|
#include "wpi/net/WebSocketServer.hpp"
|
|
|
|
|
#include "wpi/net/uv/Stream.hpp"
|
2020-07-05 22:10:30 -07:00
|
|
|
|
|
|
|
|
namespace wpi {
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* A server-side HTTP connection that also accepts WebSocket upgrades.
|
|
|
|
|
*
|
|
|
|
|
* @tparam Derived derived class for std::enable_shared_from_this.
|
|
|
|
|
*/
|
|
|
|
|
template <typename Derived>
|
|
|
|
|
class HttpWebSocketServerConnection
|
|
|
|
|
: public HttpServerConnection,
|
|
|
|
|
public std::enable_shared_from_this<Derived> {
|
|
|
|
|
public:
|
|
|
|
|
/**
|
|
|
|
|
* Constructor.
|
|
|
|
|
*
|
|
|
|
|
* @param stream network stream
|
|
|
|
|
* @param protocols Acceptable subprotocols
|
|
|
|
|
*/
|
|
|
|
|
HttpWebSocketServerConnection(std::shared_ptr<uv::Stream> stream,
|
2024-10-15 23:42:57 -07:00
|
|
|
std::span<const std::string_view> protocols)
|
|
|
|
|
: HttpServerConnection{stream},
|
|
|
|
|
m_helper{m_request},
|
|
|
|
|
m_protocols{protocols.begin(), protocols.end()} {
|
|
|
|
|
// Handle upgrade event
|
|
|
|
|
m_helper.upgrade.connect([this] {
|
|
|
|
|
// Negotiate sub-protocol
|
|
|
|
|
SmallVector<std::string_view, 2> protocols{m_protocols.begin(),
|
|
|
|
|
m_protocols.end()};
|
|
|
|
|
std::string_view protocol = m_helper.MatchProtocol(protocols).second;
|
|
|
|
|
|
|
|
|
|
// Check that the upgrade is valid
|
|
|
|
|
if (!IsValidWsUpgrade(protocol)) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Disconnect HttpServerConnection header reader
|
|
|
|
|
m_dataConn.disconnect();
|
|
|
|
|
m_messageCompleteConn.disconnect();
|
|
|
|
|
|
|
|
|
|
// Accepting the stream may destroy this (as it replaces the stream user
|
|
|
|
|
// data), so grab a shared pointer first.
|
|
|
|
|
auto self = this->shared_from_this();
|
|
|
|
|
|
|
|
|
|
// Accept the upgrade
|
|
|
|
|
auto ws = m_helper.Accept(m_stream, protocol);
|
|
|
|
|
|
|
|
|
|
// Set this as the websocket user data to keep it around
|
|
|
|
|
ws->SetData(self);
|
|
|
|
|
|
|
|
|
|
// Store in member
|
|
|
|
|
m_websocket = ws.get();
|
|
|
|
|
|
|
|
|
|
// Call derived class function
|
|
|
|
|
ProcessWsUpgrade();
|
|
|
|
|
});
|
|
|
|
|
}
|
2020-07-05 22:10:30 -07:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Constructor.
|
|
|
|
|
*
|
|
|
|
|
* @param stream network stream
|
|
|
|
|
* @param protocols Acceptable subprotocols
|
|
|
|
|
*/
|
2021-06-06 16:13:58 -07:00
|
|
|
HttpWebSocketServerConnection(
|
|
|
|
|
std::shared_ptr<uv::Stream> stream,
|
|
|
|
|
std::initializer_list<std::string_view> protocols)
|
2021-06-06 19:51:14 -07:00
|
|
|
: HttpWebSocketServerConnection(stream,
|
|
|
|
|
{protocols.begin(), protocols.end()}) {}
|
2020-07-05 22:10:30 -07:00
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
/**
|
|
|
|
|
* Check that an incoming WebSocket upgrade is okay. This is called prior
|
|
|
|
|
* to accepting the upgrade (so prior to ProcessWsUpgrade()).
|
|
|
|
|
*
|
|
|
|
|
* The implementation should check other headers and return true if the
|
|
|
|
|
* WebSocket connection should be accepted.
|
|
|
|
|
*
|
|
|
|
|
* @param protocol negotiated subprotocol
|
|
|
|
|
*/
|
2021-06-06 16:13:58 -07:00
|
|
|
virtual bool IsValidWsUpgrade(std::string_view protocol) { return true; }
|
2020-07-05 22:10:30 -07:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Process an incoming WebSocket upgrade. This is called after the header
|
|
|
|
|
* reader has been disconnected and the websocket has been accepted.
|
|
|
|
|
*
|
|
|
|
|
* The implementation should set up appropriate callbacks on the websocket
|
|
|
|
|
* object to continue communication.
|
|
|
|
|
*
|
|
|
|
|
* @note When a WebSocket upgrade occurs, the stream user data is replaced
|
|
|
|
|
* with the websocket, and the websocket user data points to "this".
|
|
|
|
|
* Replace the websocket user data with caution!
|
|
|
|
|
*/
|
|
|
|
|
virtual void ProcessWsUpgrade() = 0;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* WebSocket connection; not valid until ProcessWsUpgrade is called.
|
|
|
|
|
*/
|
|
|
|
|
WebSocket* m_websocket = nullptr;
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
WebSocketServerHelper m_helper;
|
|
|
|
|
SmallVector<std::string, 2> m_protocols;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
} // namespace wpi
|
|
|
|
|
|
2022-05-07 10:54:14 -07:00
|
|
|
#endif // WPINET_HTTPWEBSOCKETSERVERCONNECTION_H_
|