2016-08-28 05:46:31 +00:00
|
|
|
#pragma once
|
2016-09-15 08:29:08 +00:00
|
|
|
#include <boost/algorithm/string/predicate.hpp>
|
2017-09-17 03:52:37 +00:00
|
|
|
#include <boost/array.hpp>
|
2021-11-06 03:06:18 +00:00
|
|
|
#include "crow/logging.h"
|
2016-09-21 14:11:06 +00:00
|
|
|
#include "crow/socket_adaptors.h"
|
|
|
|
#include "crow/http_request.h"
|
|
|
|
#include "crow/TinySHA1.hpp"
|
2016-08-28 05:46:31 +00:00
|
|
|
|
|
|
|
namespace crow
|
|
|
|
{
|
|
|
|
namespace websocket
|
|
|
|
{
|
|
|
|
enum class WebSocketReadState
|
|
|
|
{
|
|
|
|
MiniHeader,
|
|
|
|
Len16,
|
|
|
|
Len64,
|
|
|
|
Mask,
|
|
|
|
Payload,
|
|
|
|
};
|
|
|
|
|
2021-11-25 11:45:38 +00:00
|
|
|
/// A base class for websocket connection.
|
|
|
|
struct connection
|
|
|
|
{
|
|
|
|
virtual void send_binary(const std::string& msg) = 0;
|
|
|
|
virtual void send_text(const std::string& msg) = 0;
|
|
|
|
virtual void send_ping(const std::string& msg) = 0;
|
|
|
|
virtual void send_pong(const std::string& msg) = 0;
|
2016-08-28 05:46:31 +00:00
|
|
|
virtual void close(const std::string& msg = "quit") = 0;
|
2021-11-25 11:45:38 +00:00
|
|
|
virtual std::string get_remote_ip() = 0;
|
|
|
|
virtual ~connection() {}
|
2016-09-09 17:39:22 +00:00
|
|
|
|
|
|
|
void userdata(void* u) { userdata_ = u; }
|
|
|
|
void* userdata() { return userdata_; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
void* userdata_;
|
2021-11-25 11:45:38 +00:00
|
|
|
};
|
2016-08-28 05:46:31 +00:00
|
|
|
|
2021-11-22 14:39:48 +00:00
|
|
|
// Modified version of the illustration in RFC6455 Section-5.2
|
|
|
|
//
|
|
|
|
//
|
2020-11-09 01:41:55 +00:00
|
|
|
// 0 1 2 3 -byte
|
|
|
|
// 0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7 -bit
|
2020-11-04 03:20:09 +00:00
|
|
|
// +-+-+-+-+-------+-+-------------+-------------------------------+
|
|
|
|
// |F|R|R|R| opcode|M| Payload len | Extended payload length |
|
|
|
|
// |I|S|S|S| (4) |A| (7) | (16/64) |
|
|
|
|
// |N|V|V|V| |S| | (if payload len==126/127) |
|
|
|
|
// | |1|2|3| |K| | |
|
|
|
|
// +-+-+-+-+-------+-+-------------+ - - - - - - - - - - - - - - - +
|
|
|
|
// | Extended payload length continued, if payload len == 127 |
|
|
|
|
// + - - - - - - - - - - - - - - - +-------------------------------+
|
|
|
|
// | |Masking-key, if MASK set to 1 |
|
|
|
|
// +-------------------------------+-------------------------------+
|
|
|
|
// | Masking-key (continued) | Payload Data |
|
|
|
|
// +-------------------------------- - - - - - - - - - - - - - - - +
|
|
|
|
// : Payload Data continued ... :
|
|
|
|
// + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +
|
|
|
|
// | Payload Data continued ... |
|
|
|
|
// +---------------------------------------------------------------+
|
2021-11-22 14:39:48 +00:00
|
|
|
//
|
2020-11-04 03:20:09 +00:00
|
|
|
|
2020-11-09 14:36:03 +00:00
|
|
|
/// A websocket connection.
|
2022-05-14 15:56:39 +00:00
|
|
|
|
2022-05-06 17:02:43 +00:00
|
|
|
template<typename Adaptor, typename Handler>
|
2016-08-28 05:46:31 +00:00
|
|
|
class Connection : public connection
|
|
|
|
{
|
2021-11-25 11:45:38 +00:00
|
|
|
public:
|
|
|
|
/// Constructor for a connection.
|
2021-12-03 03:39:23 +00:00
|
|
|
|
2021-11-25 11:45:38 +00:00
|
|
|
///
|
|
|
|
/// Requires a request with an "Upgrade: websocket" header.<br>
|
|
|
|
/// Automatically handles the handshake.
|
2022-05-15 08:01:24 +00:00
|
|
|
Connection(const crow::request& req, Adaptor&& adaptor, Handler* handler, uint64_t max_payload,
|
2021-11-25 11:45:38 +00:00
|
|
|
std::function<void(crow::websocket::connection&)> open_handler,
|
|
|
|
std::function<void(crow::websocket::connection&, const std::string&, bool)> message_handler,
|
|
|
|
std::function<void(crow::websocket::connection&, const std::string&)> close_handler,
|
|
|
|
std::function<void(crow::websocket::connection&)> error_handler,
|
2022-05-27 12:06:12 +00:00
|
|
|
std::function<bool(const crow::request&, void**)> accept_handler):
|
2021-11-25 11:45:38 +00:00
|
|
|
adaptor_(std::move(adaptor)),
|
2022-05-06 17:02:43 +00:00
|
|
|
handler_(handler),
|
2022-05-16 16:14:51 +00:00
|
|
|
max_payload_bytes_(max_payload),
|
2022-05-06 17:02:43 +00:00
|
|
|
open_handler_(std::move(open_handler)),
|
|
|
|
message_handler_(std::move(message_handler)),
|
|
|
|
close_handler_(std::move(close_handler)),
|
|
|
|
error_handler_(std::move(error_handler)),
|
2022-05-20 11:14:55 +00:00
|
|
|
accept_handler_(std::move(accept_handler))
|
2021-11-25 11:45:38 +00:00
|
|
|
{
|
|
|
|
if (!boost::iequals(req.get_header_value("upgrade"), "websocket"))
|
2016-08-28 05:46:31 +00:00
|
|
|
{
|
2021-11-25 11:45:38 +00:00
|
|
|
adaptor.close();
|
2022-05-20 11:14:55 +00:00
|
|
|
handler_->remove_websocket(this);
|
2021-11-25 11:45:38 +00:00
|
|
|
delete this;
|
|
|
|
return;
|
2016-08-28 05:46:31 +00:00
|
|
|
}
|
|
|
|
|
2021-11-25 11:45:38 +00:00
|
|
|
if (accept_handler_)
|
2016-08-28 05:46:31 +00:00
|
|
|
{
|
2022-05-27 12:06:12 +00:00
|
|
|
void* ud = nullptr;
|
|
|
|
if (!accept_handler_(req, &ud))
|
2021-11-25 11:45:38 +00:00
|
|
|
{
|
|
|
|
adaptor.close();
|
2022-05-20 11:14:55 +00:00
|
|
|
handler_->remove_websocket(this);
|
2021-11-25 11:45:38 +00:00
|
|
|
delete this;
|
|
|
|
return;
|
|
|
|
}
|
2022-05-27 12:06:12 +00:00
|
|
|
userdata(ud);
|
2016-08-28 05:46:31 +00:00
|
|
|
}
|
|
|
|
|
2021-11-25 11:45:38 +00:00
|
|
|
// Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==
|
|
|
|
// Sec-WebSocket-Version: 13
|
|
|
|
std::string magic = req.get_header_value("Sec-WebSocket-Key") + "258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
|
|
|
|
sha1::SHA1 s;
|
|
|
|
s.processBytes(magic.data(), magic.size());
|
|
|
|
uint8_t digest[20];
|
|
|
|
s.getDigestBytes(digest);
|
2022-05-14 22:15:39 +00:00
|
|
|
|
2021-11-25 11:45:38 +00:00
|
|
|
start(crow::utility::base64encode((unsigned char*)digest, 20));
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Send data through the socket.
|
|
|
|
template<typename CompletionHandler>
|
|
|
|
void dispatch(CompletionHandler handler)
|
|
|
|
{
|
|
|
|
adaptor_.get_io_service().dispatch(handler);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Send data through the socket and return immediately.
|
|
|
|
template<typename CompletionHandler>
|
|
|
|
void post(CompletionHandler handler)
|
|
|
|
{
|
|
|
|
adaptor_.get_io_service().post(handler);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Send a "Ping" message.
|
2021-12-03 03:39:23 +00:00
|
|
|
|
2021-11-25 11:45:38 +00:00
|
|
|
///
|
|
|
|
/// Usually invoked to check if the other point is still online.
|
|
|
|
void send_ping(const std::string& msg) override
|
|
|
|
{
|
2021-11-27 12:28:50 +00:00
|
|
|
dispatch([this, msg] {
|
2021-11-27 17:44:51 +00:00
|
|
|
auto header = build_header(0x9, msg.size());
|
|
|
|
write_buffers_.emplace_back(std::move(header));
|
|
|
|
write_buffers_.emplace_back(msg);
|
|
|
|
do_write();
|
|
|
|
});
|
2021-11-25 11:45:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Send a "Pong" message.
|
2021-12-03 03:39:23 +00:00
|
|
|
|
2021-11-25 11:45:38 +00:00
|
|
|
///
|
|
|
|
/// Usually automatically invoked as a response to a "Ping" message.
|
|
|
|
void send_pong(const std::string& msg) override
|
|
|
|
{
|
2021-11-27 12:28:50 +00:00
|
|
|
dispatch([this, msg] {
|
2021-11-27 17:44:51 +00:00
|
|
|
auto header = build_header(0xA, msg.size());
|
|
|
|
write_buffers_.emplace_back(std::move(header));
|
|
|
|
write_buffers_.emplace_back(msg);
|
|
|
|
do_write();
|
|
|
|
});
|
2021-11-25 11:45:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Send a binary encoded message.
|
|
|
|
void send_binary(const std::string& msg) override
|
|
|
|
{
|
2021-11-27 12:28:50 +00:00
|
|
|
dispatch([this, msg] {
|
2021-11-27 17:44:51 +00:00
|
|
|
auto header = build_header(2, msg.size());
|
|
|
|
write_buffers_.emplace_back(std::move(header));
|
|
|
|
write_buffers_.emplace_back(msg);
|
|
|
|
do_write();
|
|
|
|
});
|
2021-11-25 11:45:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Send a plaintext message.
|
|
|
|
void send_text(const std::string& msg) override
|
|
|
|
{
|
2021-11-27 12:28:50 +00:00
|
|
|
dispatch([this, msg] {
|
2021-11-27 17:44:51 +00:00
|
|
|
auto header = build_header(1, msg.size());
|
|
|
|
write_buffers_.emplace_back(std::move(header));
|
|
|
|
write_buffers_.emplace_back(msg);
|
|
|
|
do_write();
|
|
|
|
});
|
2021-11-25 11:45:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Send a close signal.
|
2021-12-03 03:39:23 +00:00
|
|
|
|
2021-11-25 11:45:38 +00:00
|
|
|
///
|
|
|
|
/// Sets a flag to destroy the object once the message is sent.
|
|
|
|
void close(const std::string& msg) override
|
|
|
|
{
|
2021-11-27 12:28:50 +00:00
|
|
|
dispatch([this, msg] {
|
2021-11-27 17:44:51 +00:00
|
|
|
has_sent_close_ = true;
|
|
|
|
if (has_recv_close_ && !is_close_handler_called_)
|
|
|
|
{
|
|
|
|
is_close_handler_called_ = true;
|
|
|
|
if (close_handler_)
|
|
|
|
close_handler_(*this, msg);
|
|
|
|
}
|
|
|
|
auto header = build_header(0x8, msg.size());
|
|
|
|
write_buffers_.emplace_back(std::move(header));
|
|
|
|
write_buffers_.emplace_back(msg);
|
|
|
|
do_write();
|
|
|
|
});
|
2021-11-25 11:45:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string get_remote_ip() override
|
|
|
|
{
|
|
|
|
return adaptor_.remote_endpoint().address().to_string();
|
|
|
|
}
|
|
|
|
|
2022-05-15 00:51:51 +00:00
|
|
|
void set_max_payload_size(uint64_t payload)
|
|
|
|
{
|
|
|
|
max_payload_bytes_ = payload;
|
|
|
|
}
|
|
|
|
|
2021-11-25 11:45:38 +00:00
|
|
|
protected:
|
|
|
|
/// Generate the websocket headers using an opcode and the message size (in bytes).
|
|
|
|
std::string build_header(int opcode, size_t size)
|
|
|
|
{
|
|
|
|
char buf[2 + 8] = "\x80\x00";
|
|
|
|
buf[0] += opcode;
|
|
|
|
if (size < 126)
|
2021-11-01 20:57:21 +00:00
|
|
|
{
|
2021-11-25 11:45:38 +00:00
|
|
|
buf[1] += static_cast<char>(size);
|
|
|
|
return {buf, buf + 2};
|
2021-11-01 20:57:21 +00:00
|
|
|
}
|
2021-11-25 11:45:38 +00:00
|
|
|
else if (size < 0x10000)
|
2016-08-28 05:46:31 +00:00
|
|
|
{
|
2021-11-25 11:45:38 +00:00
|
|
|
buf[1] += 126;
|
|
|
|
*(uint16_t*)(buf + 2) = htons(static_cast<uint16_t>(size));
|
|
|
|
return {buf, buf + 4};
|
2016-08-28 05:46:31 +00:00
|
|
|
}
|
2021-11-25 11:45:38 +00:00
|
|
|
else
|
2016-08-28 05:46:31 +00:00
|
|
|
{
|
2021-11-25 11:45:38 +00:00
|
|
|
buf[1] += 127;
|
|
|
|
*reinterpret_cast<uint64_t*>(buf + 2) = ((1 == htonl(1)) ? static_cast<uint64_t>(size) : (static_cast<uint64_t>(htonl((size)&0xFFFFFFFF)) << 32) | htonl(static_cast<uint64_t>(size) >> 32));
|
|
|
|
return {buf, buf + 10};
|
2016-08-28 05:46:31 +00:00
|
|
|
}
|
2021-11-25 11:45:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Send the HTTP upgrade response.
|
2021-12-03 03:39:23 +00:00
|
|
|
|
2021-11-25 11:45:38 +00:00
|
|
|
///
|
|
|
|
/// Finishes the handshake process, then starts reading messages from the socket.
|
|
|
|
void start(std::string&& hello)
|
|
|
|
{
|
|
|
|
static std::string header = "HTTP/1.1 101 Switching Protocols\r\n"
|
|
|
|
"Upgrade: websocket\r\n"
|
|
|
|
"Connection: Upgrade\r\n"
|
|
|
|
"Sec-WebSocket-Accept: ";
|
|
|
|
write_buffers_.emplace_back(header);
|
|
|
|
write_buffers_.emplace_back(std::move(hello));
|
|
|
|
write_buffers_.emplace_back(crlf);
|
|
|
|
write_buffers_.emplace_back(crlf);
|
|
|
|
do_write();
|
|
|
|
if (open_handler_)
|
|
|
|
open_handler_(*this);
|
|
|
|
do_read();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Read a websocket message.
|
2021-12-03 03:39:23 +00:00
|
|
|
|
2021-11-25 11:45:38 +00:00
|
|
|
///
|
|
|
|
/// Involves:<br>
|
|
|
|
/// Handling headers (opcodes, size).<br>
|
|
|
|
/// Unmasking the payload.<br>
|
|
|
|
/// Reading the actual payload.<br>
|
|
|
|
void do_read()
|
|
|
|
{
|
|
|
|
is_reading = true;
|
|
|
|
switch (state_)
|
2016-08-28 05:46:31 +00:00
|
|
|
{
|
2021-11-25 11:45:38 +00:00
|
|
|
case WebSocketReadState::MiniHeader:
|
2016-08-28 05:46:31 +00:00
|
|
|
{
|
2021-11-25 11:45:38 +00:00
|
|
|
mini_header_ = 0;
|
|
|
|
//boost::asio::async_read(adaptor_.socket(), boost::asio::buffer(&mini_header_, 1),
|
|
|
|
adaptor_.socket().async_read_some(
|
|
|
|
boost::asio::buffer(&mini_header_, 2),
|
|
|
|
[this](const boost::system::error_code& ec, std::size_t
|
2017-09-17 17:58:53 +00:00
|
|
|
#ifdef CROW_ENABLE_DEBUG
|
2021-11-25 11:45:38 +00:00
|
|
|
bytes_transferred
|
2017-09-17 17:58:53 +00:00
|
|
|
#endif
|
2021-11-25 11:45:38 +00:00
|
|
|
)
|
2017-09-17 17:58:53 +00:00
|
|
|
|
2021-11-25 11:45:38 +00:00
|
|
|
{
|
|
|
|
is_reading = false;
|
|
|
|
mini_header_ = ntohs(mini_header_);
|
2016-08-28 05:46:31 +00:00
|
|
|
#ifdef CROW_ENABLE_DEBUG
|
2021-11-25 11:45:38 +00:00
|
|
|
|
|
|
|
if (!ec && bytes_transferred != 2)
|
|
|
|
{
|
|
|
|
throw std::runtime_error("WebSocket:MiniHeader:async_read fail:asio bug?");
|
|
|
|
}
|
2016-08-28 05:46:31 +00:00
|
|
|
#endif
|
|
|
|
|
2021-11-25 11:45:38 +00:00
|
|
|
if (!ec)
|
|
|
|
{
|
|
|
|
if ((mini_header_ & 0x80) == 0x80)
|
|
|
|
has_mask_ = true;
|
|
|
|
else //if the websocket specification is enforced and the message isn't masked, terminate the connection
|
|
|
|
{
|
2021-11-22 14:39:48 +00:00
|
|
|
#ifndef CROW_ENFORCE_WS_SPEC
|
2021-11-25 11:45:38 +00:00
|
|
|
has_mask_ = false;
|
2021-11-22 14:39:48 +00:00
|
|
|
#else
|
2021-11-25 11:45:38 +00:00
|
|
|
close_connection_ = true;
|
2022-05-18 09:28:51 +00:00
|
|
|
adaptor_.shutdown_readwrite();
|
2021-11-25 11:45:38 +00:00
|
|
|
adaptor_.close();
|
|
|
|
if (error_handler_)
|
|
|
|
error_handler_(*this);
|
|
|
|
check_destroy();
|
2021-11-22 14:39:48 +00:00
|
|
|
#endif
|
2021-11-25 11:45:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ((mini_header_ & 0x7f) == 127)
|
|
|
|
{
|
|
|
|
state_ = WebSocketReadState::Len64;
|
|
|
|
}
|
|
|
|
else if ((mini_header_ & 0x7f) == 126)
|
|
|
|
{
|
|
|
|
state_ = WebSocketReadState::Len16;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
remaining_length_ = mini_header_ & 0x7f;
|
|
|
|
state_ = WebSocketReadState::Mask;
|
|
|
|
}
|
|
|
|
do_read();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
close_connection_ = true;
|
2022-05-18 09:28:51 +00:00
|
|
|
adaptor_.shutdown_readwrite();
|
2021-11-25 11:45:38 +00:00
|
|
|
adaptor_.close();
|
|
|
|
if (error_handler_)
|
|
|
|
error_handler_(*this);
|
|
|
|
check_destroy();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case WebSocketReadState::Len16:
|
|
|
|
{
|
|
|
|
remaining_length_ = 0;
|
|
|
|
remaining_length16_ = 0;
|
|
|
|
boost::asio::async_read(
|
|
|
|
adaptor_.socket(), boost::asio::buffer(&remaining_length16_, 2),
|
|
|
|
[this](const boost::system::error_code& ec, std::size_t
|
2017-09-17 17:58:53 +00:00
|
|
|
#ifdef CROW_ENABLE_DEBUG
|
2021-11-25 11:45:38 +00:00
|
|
|
bytes_transferred
|
2017-09-17 17:58:53 +00:00
|
|
|
#endif
|
2021-11-27 12:28:50 +00:00
|
|
|
) {
|
2021-11-25 11:45:38 +00:00
|
|
|
is_reading = false;
|
|
|
|
remaining_length16_ = ntohs(remaining_length16_);
|
|
|
|
remaining_length_ = remaining_length16_;
|
2016-08-28 05:46:31 +00:00
|
|
|
#ifdef CROW_ENABLE_DEBUG
|
2021-11-25 11:45:38 +00:00
|
|
|
if (!ec && bytes_transferred != 2)
|
|
|
|
{
|
|
|
|
throw std::runtime_error("WebSocket:Len16:async_read fail:asio bug?");
|
|
|
|
}
|
2016-08-28 05:46:31 +00:00
|
|
|
#endif
|
|
|
|
|
2021-11-25 11:45:38 +00:00
|
|
|
if (!ec)
|
|
|
|
{
|
|
|
|
state_ = WebSocketReadState::Mask;
|
|
|
|
do_read();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
close_connection_ = true;
|
2022-05-18 09:28:51 +00:00
|
|
|
adaptor_.shutdown_readwrite();
|
2021-11-25 11:45:38 +00:00
|
|
|
adaptor_.close();
|
|
|
|
if (error_handler_)
|
|
|
|
error_handler_(*this);
|
|
|
|
check_destroy();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case WebSocketReadState::Len64:
|
|
|
|
{
|
|
|
|
boost::asio::async_read(
|
|
|
|
adaptor_.socket(), boost::asio::buffer(&remaining_length_, 8),
|
|
|
|
[this](const boost::system::error_code& ec, std::size_t
|
2017-09-17 17:58:53 +00:00
|
|
|
#ifdef CROW_ENABLE_DEBUG
|
2021-11-25 11:45:38 +00:00
|
|
|
bytes_transferred
|
2017-09-17 17:58:53 +00:00
|
|
|
#endif
|
2021-11-27 12:28:50 +00:00
|
|
|
) {
|
2021-11-25 11:45:38 +00:00
|
|
|
is_reading = false;
|
|
|
|
remaining_length_ = ((1 == ntohl(1)) ? (remaining_length_) : (static_cast<uint64_t>(ntohl((remaining_length_)&0xFFFFFFFF)) << 32) | ntohl((remaining_length_) >> 32));
|
2016-08-28 05:46:31 +00:00
|
|
|
#ifdef CROW_ENABLE_DEBUG
|
2021-11-25 11:45:38 +00:00
|
|
|
if (!ec && bytes_transferred != 8)
|
|
|
|
{
|
|
|
|
throw std::runtime_error("WebSocket:Len16:async_read fail:asio bug?");
|
|
|
|
}
|
2016-08-28 05:46:31 +00:00
|
|
|
#endif
|
|
|
|
|
2021-11-25 11:45:38 +00:00
|
|
|
if (!ec)
|
|
|
|
{
|
|
|
|
state_ = WebSocketReadState::Mask;
|
|
|
|
do_read();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
close_connection_ = true;
|
2022-05-18 09:28:51 +00:00
|
|
|
adaptor_.shutdown_readwrite();
|
2021-11-25 11:45:38 +00:00
|
|
|
adaptor_.close();
|
|
|
|
if (error_handler_)
|
|
|
|
error_handler_(*this);
|
|
|
|
check_destroy();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case WebSocketReadState::Mask:
|
2022-04-27 03:10:44 +00:00
|
|
|
if (remaining_length_ > max_payload_bytes_)
|
|
|
|
{
|
|
|
|
close_connection_ = true;
|
|
|
|
adaptor_.close();
|
|
|
|
if (error_handler_)
|
2022-04-27 03:33:10 +00:00
|
|
|
error_handler_(*this);
|
2022-04-27 03:10:44 +00:00
|
|
|
check_destroy();
|
|
|
|
}
|
|
|
|
else if (has_mask_)
|
2021-11-25 11:45:38 +00:00
|
|
|
{
|
|
|
|
boost::asio::async_read(
|
|
|
|
adaptor_.socket(), boost::asio::buffer((char*)&mask_, 4),
|
|
|
|
[this](const boost::system::error_code& ec, std::size_t
|
2016-08-28 05:46:31 +00:00
|
|
|
#ifdef CROW_ENABLE_DEBUG
|
2021-11-25 11:45:38 +00:00
|
|
|
bytes_transferred
|
2020-11-04 04:16:03 +00:00
|
|
|
#endif
|
2021-11-27 12:28:50 +00:00
|
|
|
) {
|
2021-11-25 11:45:38 +00:00
|
|
|
is_reading = false;
|
2020-11-04 04:16:03 +00:00
|
|
|
#ifdef CROW_ENABLE_DEBUG
|
2021-11-25 11:45:38 +00:00
|
|
|
if (!ec && bytes_transferred != 4)
|
|
|
|
{
|
|
|
|
throw std::runtime_error("WebSocket:Mask:async_read fail:asio bug?");
|
|
|
|
}
|
2016-08-28 05:46:31 +00:00
|
|
|
#endif
|
|
|
|
|
2021-11-25 11:45:38 +00:00
|
|
|
if (!ec)
|
|
|
|
{
|
|
|
|
state_ = WebSocketReadState::Payload;
|
|
|
|
do_read();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
close_connection_ = true;
|
|
|
|
if (error_handler_)
|
|
|
|
error_handler_(*this);
|
2022-05-18 09:28:51 +00:00
|
|
|
adaptor_.shutdown_readwrite();
|
2021-11-25 11:45:38 +00:00
|
|
|
adaptor_.close();
|
2022-05-20 11:14:55 +00:00
|
|
|
check_destroy();
|
2021-11-25 11:45:38 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
state_ = WebSocketReadState::Payload;
|
|
|
|
do_read();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case WebSocketReadState::Payload:
|
|
|
|
{
|
|
|
|
auto to_read = static_cast<std::uint64_t>(buffer_.size());
|
|
|
|
if (remaining_length_ < to_read)
|
|
|
|
to_read = remaining_length_;
|
|
|
|
adaptor_.socket().async_read_some(
|
|
|
|
boost::asio::buffer(buffer_, static_cast<std::size_t>(to_read)),
|
2021-11-27 12:28:50 +00:00
|
|
|
[this](const boost::system::error_code& ec, std::size_t bytes_transferred) {
|
2021-11-25 11:45:38 +00:00
|
|
|
is_reading = false;
|
|
|
|
|
|
|
|
if (!ec)
|
|
|
|
{
|
|
|
|
fragment_.insert(fragment_.end(), buffer_.begin(), buffer_.begin() + bytes_transferred);
|
|
|
|
remaining_length_ -= bytes_transferred;
|
|
|
|
if (remaining_length_ == 0)
|
|
|
|
{
|
|
|
|
handle_fragment();
|
|
|
|
state_ = WebSocketReadState::MiniHeader;
|
|
|
|
do_read();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
do_read();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
close_connection_ = true;
|
|
|
|
if (error_handler_)
|
|
|
|
error_handler_(*this);
|
2022-05-18 09:28:51 +00:00
|
|
|
adaptor_.shutdown_readwrite();
|
2021-11-25 11:45:38 +00:00
|
|
|
adaptor_.close();
|
2022-05-20 11:14:55 +00:00
|
|
|
check_destroy();
|
2021-11-25 11:45:38 +00:00
|
|
|
}
|
|
|
|
});
|
2016-08-28 05:46:31 +00:00
|
|
|
}
|
2021-11-25 11:45:38 +00:00
|
|
|
break;
|
2016-08-28 05:46:31 +00:00
|
|
|
}
|
2021-11-25 11:45:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Check if the FIN bit is set.
|
|
|
|
bool is_FIN()
|
|
|
|
{
|
|
|
|
return mini_header_ & 0x8000;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Extract the opcode from the header.
|
|
|
|
int opcode()
|
|
|
|
{
|
|
|
|
return (mini_header_ & 0x0f00) >> 8;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Process the payload fragment.
|
2021-12-03 03:39:23 +00:00
|
|
|
|
2021-11-25 11:45:38 +00:00
|
|
|
///
|
|
|
|
/// Unmasks the fragment, checks the opcode, merges fragments into 1 message body, and calls the appropriate handler.
|
|
|
|
void handle_fragment()
|
|
|
|
{
|
|
|
|
if (has_mask_)
|
2016-08-28 05:46:31 +00:00
|
|
|
{
|
2021-11-25 11:45:38 +00:00
|
|
|
for (decltype(fragment_.length()) i = 0; i < fragment_.length(); i++)
|
|
|
|
{
|
|
|
|
fragment_[i] ^= ((char*)&mask_)[i % 4];
|
|
|
|
}
|
2016-08-28 05:46:31 +00:00
|
|
|
}
|
2021-11-25 11:45:38 +00:00
|
|
|
switch (opcode())
|
2016-08-28 05:46:31 +00:00
|
|
|
{
|
2021-11-25 11:45:38 +00:00
|
|
|
case 0: // Continuation
|
2016-08-28 05:46:31 +00:00
|
|
|
{
|
2021-11-25 11:45:38 +00:00
|
|
|
message_ += fragment_;
|
|
|
|
if (is_FIN())
|
2020-11-04 04:16:03 +00:00
|
|
|
{
|
2021-11-25 11:45:38 +00:00
|
|
|
if (message_handler_)
|
|
|
|
message_handler_(*this, message_, is_binary_);
|
|
|
|
message_.clear();
|
2020-11-04 04:16:03 +00:00
|
|
|
}
|
2016-08-28 05:46:31 +00:00
|
|
|
}
|
2021-11-25 11:45:38 +00:00
|
|
|
break;
|
|
|
|
case 1: // Text
|
2016-08-28 05:46:31 +00:00
|
|
|
{
|
2021-11-25 11:45:38 +00:00
|
|
|
is_binary_ = false;
|
|
|
|
message_ += fragment_;
|
|
|
|
if (is_FIN())
|
|
|
|
{
|
|
|
|
if (message_handler_)
|
|
|
|
message_handler_(*this, message_, is_binary_);
|
|
|
|
message_.clear();
|
|
|
|
}
|
2016-08-28 05:46:31 +00:00
|
|
|
}
|
2021-11-25 11:45:38 +00:00
|
|
|
break;
|
|
|
|
case 2: // Binary
|
|
|
|
{
|
|
|
|
is_binary_ = true;
|
|
|
|
message_ += fragment_;
|
|
|
|
if (is_FIN())
|
|
|
|
{
|
|
|
|
if (message_handler_)
|
|
|
|
message_handler_(*this, message_, is_binary_);
|
|
|
|
message_.clear();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 0x8: // Close
|
2016-08-28 05:46:31 +00:00
|
|
|
{
|
2021-11-25 11:45:38 +00:00
|
|
|
has_recv_close_ = true;
|
|
|
|
if (!has_sent_close_)
|
2016-08-28 05:46:31 +00:00
|
|
|
{
|
2021-11-25 11:45:38 +00:00
|
|
|
close(fragment_);
|
2016-08-28 05:46:31 +00:00
|
|
|
}
|
2021-11-25 11:45:38 +00:00
|
|
|
else
|
|
|
|
{
|
2022-05-18 09:28:51 +00:00
|
|
|
adaptor_.shutdown_readwrite();
|
2021-11-25 11:45:38 +00:00
|
|
|
adaptor_.close();
|
|
|
|
close_connection_ = true;
|
|
|
|
if (!is_close_handler_called_)
|
2016-08-28 05:46:31 +00:00
|
|
|
{
|
2021-11-25 11:45:38 +00:00
|
|
|
if (close_handler_)
|
|
|
|
close_handler_(*this, fragment_);
|
|
|
|
is_close_handler_called_ = true;
|
|
|
|
}
|
|
|
|
check_destroy();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 0x9: // Ping
|
|
|
|
{
|
|
|
|
send_pong(fragment_);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 0xA: // Pong
|
|
|
|
{
|
|
|
|
pong_received_ = true;
|
2016-08-28 05:46:31 +00:00
|
|
|
}
|
2021-11-25 11:45:38 +00:00
|
|
|
break;
|
2016-08-28 05:46:31 +00:00
|
|
|
}
|
|
|
|
|
2021-11-25 11:45:38 +00:00
|
|
|
fragment_.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Send the buffers' data through the socket.
|
2021-12-03 03:39:23 +00:00
|
|
|
|
2021-11-25 11:45:38 +00:00
|
|
|
///
|
|
|
|
/// Also destroyes the object if the Close flag is set.
|
|
|
|
void do_write()
|
|
|
|
{
|
|
|
|
if (sending_buffers_.empty())
|
2016-08-28 05:46:31 +00:00
|
|
|
{
|
2021-11-25 11:45:38 +00:00
|
|
|
sending_buffers_.swap(write_buffers_);
|
|
|
|
std::vector<boost::asio::const_buffer> buffers;
|
|
|
|
buffers.reserve(sending_buffers_.size());
|
|
|
|
for (auto& s : sending_buffers_)
|
|
|
|
{
|
|
|
|
buffers.emplace_back(boost::asio::buffer(s));
|
|
|
|
}
|
|
|
|
boost::asio::async_write(
|
|
|
|
adaptor_.socket(), buffers,
|
2021-11-27 12:28:50 +00:00
|
|
|
[&](const boost::system::error_code& ec, std::size_t /*bytes_transferred*/) {
|
2021-11-25 11:45:38 +00:00
|
|
|
sending_buffers_.clear();
|
|
|
|
if (!ec && !close_connection_)
|
|
|
|
{
|
|
|
|
if (!write_buffers_.empty())
|
|
|
|
do_write();
|
|
|
|
if (has_sent_close_)
|
|
|
|
close_connection_ = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
close_connection_ = true;
|
|
|
|
check_destroy();
|
|
|
|
}
|
|
|
|
});
|
2016-08-28 05:46:31 +00:00
|
|
|
}
|
2021-11-25 11:45:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Destroy the Connection.
|
|
|
|
void check_destroy()
|
|
|
|
{
|
|
|
|
//if (has_sent_close_ && has_recv_close_)
|
|
|
|
if (!is_close_handler_called_)
|
|
|
|
if (close_handler_)
|
|
|
|
close_handler_(*this, "uncleanly");
|
2022-05-20 11:14:55 +00:00
|
|
|
handler_->remove_websocket(this);
|
2021-11-25 11:45:38 +00:00
|
|
|
if (sending_buffers_.empty() && !is_reading)
|
|
|
|
delete this;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
Adaptor adaptor_;
|
2022-05-06 17:02:43 +00:00
|
|
|
Handler* handler_;
|
2021-11-25 11:45:38 +00:00
|
|
|
|
|
|
|
std::vector<std::string> sending_buffers_;
|
|
|
|
std::vector<std::string> write_buffers_;
|
|
|
|
|
|
|
|
boost::array<char, 4096> buffer_;
|
|
|
|
bool is_binary_;
|
|
|
|
std::string message_;
|
|
|
|
std::string fragment_;
|
|
|
|
WebSocketReadState state_{WebSocketReadState::MiniHeader};
|
|
|
|
uint16_t remaining_length16_{0};
|
|
|
|
uint64_t remaining_length_{0};
|
2022-04-27 03:10:44 +00:00
|
|
|
uint64_t max_payload_bytes_{UINT64_MAX};
|
2021-11-25 11:45:38 +00:00
|
|
|
bool close_connection_{false};
|
|
|
|
bool is_reading{false};
|
|
|
|
bool has_mask_{false};
|
|
|
|
uint32_t mask_;
|
|
|
|
uint16_t mini_header_;
|
|
|
|
bool has_sent_close_{false};
|
|
|
|
bool has_recv_close_{false};
|
|
|
|
bool error_occured_{false};
|
|
|
|
bool pong_received_{false};
|
|
|
|
bool is_close_handler_called_{false};
|
|
|
|
|
|
|
|
std::function<void(crow::websocket::connection&)> open_handler_;
|
|
|
|
std::function<void(crow::websocket::connection&, const std::string&, bool)> message_handler_;
|
|
|
|
std::function<void(crow::websocket::connection&, const std::string&)> close_handler_;
|
|
|
|
std::function<void(crow::websocket::connection&)> error_handler_;
|
2022-05-27 12:06:12 +00:00
|
|
|
std::function<bool(const crow::request&, void**)> accept_handler_;
|
2016-08-28 05:46:31 +00:00
|
|
|
};
|
2021-11-25 11:45:38 +00:00
|
|
|
} // namespace websocket
|
|
|
|
} // namespace crow
|