Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 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. |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 4 | |
| 5 | #include "wpi/WebSocket.h" |
| 6 | |
| 7 | #include <random> |
| 8 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 9 | #include "fmt/format.h" |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 10 | #include "wpi/Base64.h" |
| 11 | #include "wpi/HttpParser.h" |
| 12 | #include "wpi/SmallString.h" |
| 13 | #include "wpi/SmallVector.h" |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 14 | #include "wpi/StringExtras.h" |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 15 | #include "wpi/raw_uv_ostream.h" |
| 16 | #include "wpi/sha1.h" |
| 17 | #include "wpi/uv/Stream.h" |
| 18 | |
| 19 | using namespace wpi; |
| 20 | |
| 21 | namespace { |
| 22 | class WebSocketWriteReq : public uv::WriteReq { |
| 23 | public: |
| 24 | explicit WebSocketWriteReq( |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 25 | std::function<void(span<uv::Buffer>, uv::Error)> callback) |
| 26 | : m_callback{std::move(callback)} { |
| 27 | finish.connect([this](uv::Error err) { |
| 28 | span<uv::Buffer> bufs{m_bufs}; |
| 29 | for (auto&& buf : bufs.subspan(0, m_startUser)) { |
| 30 | buf.Deallocate(); |
| 31 | } |
| 32 | m_callback(bufs.subspan(m_startUser), err); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 33 | }); |
| 34 | } |
| 35 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 36 | std::function<void(span<uv::Buffer>, uv::Error)> m_callback; |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 37 | SmallVector<uv::Buffer, 4> m_bufs; |
| 38 | size_t m_startUser; |
| 39 | }; |
| 40 | } // namespace |
| 41 | |
| 42 | class WebSocket::ClientHandshakeData { |
| 43 | public: |
| 44 | ClientHandshakeData() { |
| 45 | // key is a random nonce |
| 46 | static std::random_device rd; |
| 47 | static std::default_random_engine gen{rd()}; |
| 48 | std::uniform_int_distribution<unsigned int> dist(0, 255); |
| 49 | char nonce[16]; // the nonce sent to the server |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 50 | for (char& v : nonce) { |
| 51 | v = static_cast<char>(dist(gen)); |
| 52 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 53 | raw_svector_ostream os(key); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 54 | Base64Encode(os, {nonce, 16}); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 55 | } |
| 56 | ~ClientHandshakeData() { |
| 57 | if (auto t = timer.lock()) { |
| 58 | t->Stop(); |
| 59 | t->Close(); |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | SmallString<64> key; // the key sent to the server |
| 64 | SmallVector<std::string, 2> protocols; // valid protocols |
| 65 | HttpParser parser{HttpParser::kResponse}; // server response parser |
| 66 | bool hasUpgrade = false; |
| 67 | bool hasConnection = false; |
| 68 | bool hasAccept = false; |
| 69 | bool hasProtocol = false; |
| 70 | |
| 71 | std::weak_ptr<uv::Timer> timer; |
| 72 | }; |
| 73 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 74 | static std::string_view AcceptHash(std::string_view key, |
| 75 | SmallVectorImpl<char>& buf) { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 76 | SHA1 hash; |
| 77 | hash.Update(key); |
| 78 | hash.Update("258EAFA5-E914-47DA-95CA-C5AB0DC85B11"); |
| 79 | SmallString<64> hashBuf; |
| 80 | return Base64Encode(hash.RawFinal(hashBuf), buf); |
| 81 | } |
| 82 | |
| 83 | WebSocket::WebSocket(uv::Stream& stream, bool server, const private_init&) |
| 84 | : m_stream{stream}, m_server{server} { |
| 85 | // Connect closed and error signals to ourselves |
| 86 | m_stream.closed.connect([this]() { SetClosed(1006, "handle closed"); }); |
| 87 | m_stream.error.connect([this](uv::Error err) { |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 88 | Terminate(1006, fmt::format("stream error: {}", err.name())); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 89 | }); |
| 90 | |
| 91 | // Start reading |
| 92 | m_stream.StopRead(); // we may have been reading |
| 93 | m_stream.StartRead(); |
| 94 | m_stream.data.connect( |
| 95 | [this](uv::Buffer& buf, size_t size) { HandleIncoming(buf, size); }); |
| 96 | m_stream.end.connect( |
| 97 | [this]() { Terminate(1006, "remote end closed connection"); }); |
| 98 | } |
| 99 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 100 | WebSocket::~WebSocket() = default; |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 101 | |
| 102 | std::shared_ptr<WebSocket> WebSocket::CreateClient( |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 103 | uv::Stream& stream, std::string_view uri, std::string_view host, |
| 104 | span<const std::string_view> protocols, const ClientOptions& options) { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 105 | auto ws = std::make_shared<WebSocket>(stream, false, private_init{}); |
| 106 | stream.SetData(ws); |
| 107 | ws->StartClient(uri, host, protocols, options); |
| 108 | return ws; |
| 109 | } |
| 110 | |
| 111 | std::shared_ptr<WebSocket> WebSocket::CreateServer(uv::Stream& stream, |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 112 | std::string_view key, |
| 113 | std::string_view version, |
| 114 | std::string_view protocol) { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 115 | auto ws = std::make_shared<WebSocket>(stream, true, private_init{}); |
| 116 | stream.SetData(ws); |
| 117 | ws->StartServer(key, version, protocol); |
| 118 | return ws; |
| 119 | } |
| 120 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 121 | void WebSocket::Close(uint16_t code, std::string_view reason) { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 122 | SendClose(code, reason); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 123 | if (m_state != FAILED && m_state != CLOSED) { |
| 124 | m_state = CLOSING; |
| 125 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 126 | } |
| 127 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 128 | void WebSocket::Fail(uint16_t code, std::string_view reason) { |
| 129 | if (m_state == FAILED || m_state == CLOSED) { |
| 130 | return; |
| 131 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 132 | SendClose(code, reason); |
| 133 | SetClosed(code, reason, true); |
| 134 | Shutdown(); |
| 135 | } |
| 136 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 137 | void WebSocket::Terminate(uint16_t code, std::string_view reason) { |
| 138 | if (m_state == FAILED || m_state == CLOSED) { |
| 139 | return; |
| 140 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 141 | SetClosed(code, reason); |
| 142 | Shutdown(); |
| 143 | } |
| 144 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 145 | void WebSocket::StartClient(std::string_view uri, std::string_view host, |
| 146 | span<const std::string_view> protocols, |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 147 | const ClientOptions& options) { |
| 148 | // Create client handshake data |
| 149 | m_clientHandshake = std::make_unique<ClientHandshakeData>(); |
| 150 | |
| 151 | // Build client request |
| 152 | SmallVector<uv::Buffer, 4> bufs; |
| 153 | raw_uv_ostream os{bufs, 4096}; |
| 154 | |
| 155 | os << "GET " << uri << " HTTP/1.1\r\n"; |
| 156 | os << "Host: " << host << "\r\n"; |
| 157 | os << "Upgrade: websocket\r\n"; |
| 158 | os << "Connection: Upgrade\r\n"; |
| 159 | os << "Sec-WebSocket-Key: " << m_clientHandshake->key << "\r\n"; |
| 160 | os << "Sec-WebSocket-Version: 13\r\n"; |
| 161 | |
| 162 | // protocols (if provided) |
| 163 | if (!protocols.empty()) { |
| 164 | os << "Sec-WebSocket-Protocol: "; |
| 165 | bool first = true; |
| 166 | for (auto protocol : protocols) { |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 167 | if (!first) { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 168 | os << ", "; |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 169 | } else { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 170 | first = false; |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 171 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 172 | os << protocol; |
| 173 | // also save for later checking against server response |
| 174 | m_clientHandshake->protocols.emplace_back(protocol); |
| 175 | } |
| 176 | os << "\r\n"; |
| 177 | } |
| 178 | |
| 179 | // other headers |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 180 | for (auto&& header : options.extraHeaders) { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 181 | os << header.first << ": " << header.second << "\r\n"; |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 182 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 183 | |
| 184 | // finish headers |
| 185 | os << "\r\n"; |
| 186 | |
| 187 | // Send client request |
| 188 | m_stream.Write(bufs, [](auto bufs, uv::Error) { |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 189 | for (auto& buf : bufs) { |
| 190 | buf.Deallocate(); |
| 191 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 192 | }); |
| 193 | |
| 194 | // Set up client response handling |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 195 | m_clientHandshake->parser.status.connect([this](std::string_view status) { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 196 | unsigned int code = m_clientHandshake->parser.GetStatusCode(); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 197 | if (code != 101) { |
| 198 | Terminate(code, status); |
| 199 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 200 | }); |
| 201 | m_clientHandshake->parser.header.connect( |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 202 | [this](std::string_view name, std::string_view value) { |
| 203 | value = trim(value); |
| 204 | if (equals_lower(name, "upgrade")) { |
| 205 | if (!equals_lower(value, "websocket")) { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 206 | return Terminate(1002, "invalid upgrade response value"); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 207 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 208 | m_clientHandshake->hasUpgrade = true; |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 209 | } else if (equals_lower(name, "connection")) { |
| 210 | if (!equals_lower(value, "upgrade")) { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 211 | return Terminate(1002, "invalid connection response value"); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 212 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 213 | m_clientHandshake->hasConnection = true; |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 214 | } else if (equals_lower(name, "sec-websocket-accept")) { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 215 | // Check against expected response |
| 216 | SmallString<64> acceptBuf; |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 217 | if (!equals(value, AcceptHash(m_clientHandshake->key, acceptBuf))) { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 218 | return Terminate(1002, "invalid accept key"); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 219 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 220 | m_clientHandshake->hasAccept = true; |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 221 | } else if (equals_lower(name, "sec-websocket-extensions")) { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 222 | // No extensions are supported |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 223 | if (!value.empty()) { |
| 224 | return Terminate(1010, "unsupported extension"); |
| 225 | } |
| 226 | } else if (equals_lower(name, "sec-websocket-protocol")) { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 227 | // Make sure it was one of the provided protocols |
| 228 | bool match = false; |
| 229 | for (auto&& protocol : m_clientHandshake->protocols) { |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 230 | if (equals_lower(value, protocol)) { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 231 | match = true; |
| 232 | break; |
| 233 | } |
| 234 | } |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 235 | if (!match) { |
| 236 | return Terminate(1003, "unsupported protocol"); |
| 237 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 238 | m_clientHandshake->hasProtocol = true; |
| 239 | m_protocol = value; |
| 240 | } |
| 241 | }); |
| 242 | m_clientHandshake->parser.headersComplete.connect([this](bool) { |
| 243 | if (!m_clientHandshake->hasUpgrade || !m_clientHandshake->hasConnection || |
| 244 | !m_clientHandshake->hasAccept || |
| 245 | (!m_clientHandshake->hasProtocol && |
| 246 | !m_clientHandshake->protocols.empty())) { |
| 247 | return Terminate(1002, "invalid response"); |
| 248 | } |
| 249 | if (m_state == CONNECTING) { |
| 250 | m_state = OPEN; |
| 251 | open(m_protocol); |
| 252 | } |
| 253 | }); |
| 254 | |
| 255 | // Start handshake timer if a timeout was specified |
| 256 | if (options.handshakeTimeout != (uv::Timer::Time::max)()) { |
| 257 | auto timer = uv::Timer::Create(m_stream.GetLoopRef()); |
| 258 | timer->timeout.connect( |
| 259 | [this]() { Terminate(1006, "connection timed out"); }); |
| 260 | timer->Start(options.handshakeTimeout); |
| 261 | m_clientHandshake->timer = timer; |
| 262 | } |
| 263 | } |
| 264 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 265 | void WebSocket::StartServer(std::string_view key, std::string_view version, |
| 266 | std::string_view protocol) { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 267 | m_protocol = protocol; |
| 268 | |
| 269 | // Build server response |
| 270 | SmallVector<uv::Buffer, 4> bufs; |
| 271 | raw_uv_ostream os{bufs, 4096}; |
| 272 | |
| 273 | // Handle unsupported version |
| 274 | if (version != "13") { |
| 275 | os << "HTTP/1.1 426 Upgrade Required\r\n"; |
| 276 | os << "Upgrade: WebSocket\r\n"; |
| 277 | os << "Sec-WebSocket-Version: 13\r\n\r\n"; |
| 278 | m_stream.Write(bufs, [this](auto bufs, uv::Error) { |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 279 | for (auto& buf : bufs) { |
| 280 | buf.Deallocate(); |
| 281 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 282 | // XXX: Should we support sending a new handshake on the same connection? |
| 283 | // XXX: "this->" is required by GCC 5.5 (bug) |
| 284 | this->Terminate(1003, "unsupported protocol version"); |
| 285 | }); |
| 286 | return; |
| 287 | } |
| 288 | |
| 289 | os << "HTTP/1.1 101 Switching Protocols\r\n"; |
| 290 | os << "Upgrade: websocket\r\n"; |
| 291 | os << "Connection: Upgrade\r\n"; |
| 292 | |
| 293 | // accept hash |
| 294 | SmallString<64> acceptBuf; |
| 295 | os << "Sec-WebSocket-Accept: " << AcceptHash(key, acceptBuf) << "\r\n"; |
| 296 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 297 | if (!protocol.empty()) { |
| 298 | os << "Sec-WebSocket-Protocol: " << protocol << "\r\n"; |
| 299 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 300 | |
| 301 | // end headers |
| 302 | os << "\r\n"; |
| 303 | |
| 304 | // Send server response |
| 305 | m_stream.Write(bufs, [this](auto bufs, uv::Error) { |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 306 | for (auto& buf : bufs) { |
| 307 | buf.Deallocate(); |
| 308 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 309 | if (m_state == CONNECTING) { |
| 310 | m_state = OPEN; |
| 311 | open(m_protocol); |
| 312 | } |
| 313 | }); |
| 314 | } |
| 315 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 316 | void WebSocket::SendClose(uint16_t code, std::string_view reason) { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 317 | SmallVector<uv::Buffer, 4> bufs; |
| 318 | if (code != 1005) { |
| 319 | raw_uv_ostream os{bufs, 4096}; |
| 320 | const uint8_t codeMsb[] = {static_cast<uint8_t>((code >> 8) & 0xff), |
| 321 | static_cast<uint8_t>(code & 0xff)}; |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 322 | os << span{codeMsb}; |
| 323 | os << reason; |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 324 | } |
| 325 | Send(kFlagFin | kOpClose, bufs, [](auto bufs, uv::Error) { |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 326 | for (auto&& buf : bufs) { |
| 327 | buf.Deallocate(); |
| 328 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 329 | }); |
| 330 | } |
| 331 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 332 | void WebSocket::SetClosed(uint16_t code, std::string_view reason, bool failed) { |
| 333 | if (m_state == FAILED || m_state == CLOSED) { |
| 334 | return; |
| 335 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 336 | m_state = failed ? FAILED : CLOSED; |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 337 | closed(code, reason); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 338 | } |
| 339 | |
| 340 | void WebSocket::Shutdown() { |
| 341 | m_stream.Shutdown([this] { m_stream.Close(); }); |
| 342 | } |
| 343 | |
| 344 | void WebSocket::HandleIncoming(uv::Buffer& buf, size_t size) { |
| 345 | // ignore incoming data if we're failed or closed |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 346 | if (m_state == FAILED || m_state == CLOSED) { |
| 347 | return; |
| 348 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 349 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 350 | std::string_view data{buf.base, size}; |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 351 | |
| 352 | // Handle connecting state (mainly on client) |
| 353 | if (m_state == CONNECTING) { |
| 354 | if (m_clientHandshake) { |
| 355 | data = m_clientHandshake->parser.Execute(data); |
| 356 | // check for parser failure |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 357 | if (m_clientHandshake->parser.HasError()) { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 358 | return Terminate(1003, "invalid response"); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 359 | } |
| 360 | if (m_state != OPEN) { |
| 361 | return; // not done with handshake yet |
| 362 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 363 | |
| 364 | // we're done with the handshake, so release its memory |
| 365 | m_clientHandshake.reset(); |
| 366 | |
| 367 | // fall through to process additional data after handshake |
| 368 | } else { |
| 369 | return Terminate(1003, "got data on server before response"); |
| 370 | } |
| 371 | } |
| 372 | |
| 373 | // Message processing |
| 374 | while (!data.empty()) { |
| 375 | if (m_frameSize == UINT64_MAX) { |
| 376 | // Need at least two bytes to determine header length |
| 377 | if (m_header.size() < 2u) { |
| 378 | size_t toCopy = (std::min)(2u - m_header.size(), data.size()); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 379 | m_header.append(data.data(), data.data() + toCopy); |
| 380 | data.remove_prefix(toCopy); |
| 381 | if (m_header.size() < 2u) { |
| 382 | return; // need more data |
| 383 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 384 | |
| 385 | // Validate RSV bits are zero |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 386 | if ((m_header[0] & 0x70) != 0) { |
| 387 | return Fail(1002, "nonzero RSV"); |
| 388 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 389 | } |
| 390 | |
| 391 | // Once we have first two bytes, we can calculate the header size |
| 392 | if (m_headerSize == 0) { |
| 393 | m_headerSize = 2; |
| 394 | uint8_t len = m_header[1] & kLenMask; |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 395 | if (len == 126) { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 396 | m_headerSize += 2; |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 397 | } else if (len == 127) { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 398 | m_headerSize += 8; |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 399 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 400 | bool masking = (m_header[1] & kFlagMasking) != 0; |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 401 | if (masking) { |
| 402 | m_headerSize += 4; // masking key |
| 403 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 404 | // On server side, incoming messages MUST be masked |
| 405 | // On client side, incoming messages MUST NOT be masked |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 406 | if (m_server && !masking) { |
| 407 | return Fail(1002, "client data not masked"); |
| 408 | } |
| 409 | if (!m_server && masking) { |
| 410 | return Fail(1002, "server data masked"); |
| 411 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 412 | } |
| 413 | |
| 414 | // Need to complete header to calculate message size |
| 415 | if (m_header.size() < m_headerSize) { |
| 416 | size_t toCopy = (std::min)(m_headerSize - m_header.size(), data.size()); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 417 | m_header.append(data.data(), data.data() + toCopy); |
| 418 | data.remove_prefix(toCopy); |
| 419 | if (m_header.size() < m_headerSize) { |
| 420 | return; // need more data |
| 421 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 422 | } |
| 423 | |
| 424 | if (m_header.size() >= m_headerSize) { |
| 425 | // get payload length |
| 426 | uint8_t len = m_header[1] & kLenMask; |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 427 | if (len == 126) { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 428 | m_frameSize = (static_cast<uint16_t>(m_header[2]) << 8) | |
| 429 | static_cast<uint16_t>(m_header[3]); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 430 | } else if (len == 127) { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 431 | m_frameSize = (static_cast<uint64_t>(m_header[2]) << 56) | |
| 432 | (static_cast<uint64_t>(m_header[3]) << 48) | |
| 433 | (static_cast<uint64_t>(m_header[4]) << 40) | |
| 434 | (static_cast<uint64_t>(m_header[5]) << 32) | |
| 435 | (static_cast<uint64_t>(m_header[6]) << 24) | |
| 436 | (static_cast<uint64_t>(m_header[7]) << 16) | |
| 437 | (static_cast<uint64_t>(m_header[8]) << 8) | |
| 438 | static_cast<uint64_t>(m_header[9]); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 439 | } else { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 440 | m_frameSize = len; |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 441 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 442 | |
| 443 | // limit maximum size |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 444 | if ((m_payload.size() + m_frameSize) > m_maxMessageSize) { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 445 | return Fail(1009, "message too large"); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 446 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 447 | } |
| 448 | } |
| 449 | |
| 450 | if (m_frameSize != UINT64_MAX) { |
| 451 | size_t need = m_frameStart + m_frameSize - m_payload.size(); |
| 452 | size_t toCopy = (std::min)(need, data.size()); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 453 | m_payload.append(data.data(), data.data() + toCopy); |
| 454 | data.remove_prefix(toCopy); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 455 | need -= toCopy; |
| 456 | if (need == 0) { |
| 457 | // We have a complete frame |
| 458 | // If the message had masking, unmask it |
| 459 | if ((m_header[1] & kFlagMasking) != 0) { |
| 460 | uint8_t key[4] = { |
| 461 | m_header[m_headerSize - 4], m_header[m_headerSize - 3], |
| 462 | m_header[m_headerSize - 2], m_header[m_headerSize - 1]}; |
| 463 | int n = 0; |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 464 | for (uint8_t& ch : span{m_payload}.subspan(m_frameStart)) { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 465 | ch ^= key[n++]; |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 466 | if (n >= 4) { |
| 467 | n = 0; |
| 468 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 469 | } |
| 470 | } |
| 471 | |
| 472 | // Handle message |
| 473 | bool fin = (m_header[0] & kFlagFin) != 0; |
| 474 | uint8_t opcode = m_header[0] & kOpMask; |
| 475 | switch (opcode) { |
| 476 | case kOpCont: |
| 477 | switch (m_fragmentOpcode) { |
| 478 | case kOpText: |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 479 | if (!m_combineFragments || fin) { |
| 480 | text(std::string_view{reinterpret_cast<char*>( |
| 481 | m_payload.data()), |
| 482 | m_payload.size()}, |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 483 | fin); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 484 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 485 | break; |
| 486 | case kOpBinary: |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 487 | if (!m_combineFragments || fin) { |
| 488 | binary(m_payload, fin); |
| 489 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 490 | break; |
| 491 | default: |
| 492 | // no preceding message? |
| 493 | return Fail(1002, "invalid continuation message"); |
| 494 | } |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 495 | if (fin) { |
| 496 | m_fragmentOpcode = 0; |
| 497 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 498 | break; |
| 499 | case kOpText: |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 500 | if (m_fragmentOpcode != 0) { |
| 501 | return Fail(1002, "incomplete fragment"); |
| 502 | } |
| 503 | if (!m_combineFragments || fin) { |
| 504 | text(std::string_view{reinterpret_cast<char*>(m_payload.data()), |
| 505 | m_payload.size()}, |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 506 | fin); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 507 | } |
| 508 | if (!fin) { |
| 509 | m_fragmentOpcode = opcode; |
| 510 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 511 | break; |
| 512 | case kOpBinary: |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 513 | if (m_fragmentOpcode != 0) { |
| 514 | return Fail(1002, "incomplete fragment"); |
| 515 | } |
| 516 | if (!m_combineFragments || fin) { |
| 517 | binary(m_payload, fin); |
| 518 | } |
| 519 | if (!fin) { |
| 520 | m_fragmentOpcode = opcode; |
| 521 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 522 | break; |
| 523 | case kOpClose: { |
| 524 | uint16_t code; |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 525 | std::string_view reason; |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 526 | if (!fin) { |
| 527 | code = 1002; |
| 528 | reason = "cannot fragment control frames"; |
| 529 | } else if (m_payload.size() < 2) { |
| 530 | code = 1005; |
| 531 | } else { |
| 532 | code = (static_cast<uint16_t>(m_payload[0]) << 8) | |
| 533 | static_cast<uint16_t>(m_payload[1]); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 534 | reason = drop_front( |
| 535 | {reinterpret_cast<char*>(m_payload.data()), m_payload.size()}, |
| 536 | 2); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 537 | } |
| 538 | // Echo the close if we didn't previously send it |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 539 | if (m_state != CLOSING) { |
| 540 | SendClose(code, reason); |
| 541 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 542 | SetClosed(code, reason); |
| 543 | // If we're the server, shutdown the connection. |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 544 | if (m_server) { |
| 545 | Shutdown(); |
| 546 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 547 | break; |
| 548 | } |
| 549 | case kOpPing: |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 550 | if (!fin) { |
| 551 | return Fail(1002, "cannot fragment control frames"); |
| 552 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 553 | ping(m_payload); |
| 554 | break; |
| 555 | case kOpPong: |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 556 | if (!fin) { |
| 557 | return Fail(1002, "cannot fragment control frames"); |
| 558 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 559 | pong(m_payload); |
| 560 | break; |
| 561 | default: |
| 562 | return Fail(1002, "invalid message opcode"); |
| 563 | } |
| 564 | |
| 565 | // Prepare for next message |
| 566 | m_header.clear(); |
| 567 | m_headerSize = 0; |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 568 | if (!m_combineFragments || fin) { |
| 569 | m_payload.clear(); |
| 570 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 571 | m_frameStart = m_payload.size(); |
| 572 | m_frameSize = UINT64_MAX; |
| 573 | } |
| 574 | } |
| 575 | } |
| 576 | } |
| 577 | |
| 578 | void WebSocket::Send( |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 579 | uint8_t opcode, span<const uv::Buffer> data, |
| 580 | std::function<void(span<uv::Buffer>, uv::Error)> callback) { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 581 | // If we're not open, emit an error and don't send the data |
| 582 | if (m_state != OPEN) { |
| 583 | int err; |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 584 | if (m_state == CONNECTING) { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 585 | err = UV_EAGAIN; |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 586 | } else { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 587 | err = UV_ESHUTDOWN; |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 588 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 589 | SmallVector<uv::Buffer, 4> bufs{data.begin(), data.end()}; |
| 590 | callback(bufs, uv::Error{err}); |
| 591 | return; |
| 592 | } |
| 593 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 594 | auto req = std::make_shared<WebSocketWriteReq>(std::move(callback)); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 595 | raw_uv_ostream os{req->m_bufs, 4096}; |
| 596 | |
| 597 | // opcode (includes FIN bit) |
| 598 | os << static_cast<unsigned char>(opcode); |
| 599 | |
| 600 | // payload length |
| 601 | uint64_t size = 0; |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 602 | for (auto&& buf : data) { |
| 603 | size += buf.len; |
| 604 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 605 | if (size < 126) { |
| 606 | os << static_cast<unsigned char>((m_server ? 0x00 : kFlagMasking) | size); |
| 607 | } else if (size <= 0xffff) { |
| 608 | os << static_cast<unsigned char>((m_server ? 0x00 : kFlagMasking) | 126); |
| 609 | const uint8_t sizeMsb[] = {static_cast<uint8_t>((size >> 8) & 0xff), |
| 610 | static_cast<uint8_t>(size & 0xff)}; |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 611 | os << span{sizeMsb}; |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 612 | } else { |
| 613 | os << static_cast<unsigned char>((m_server ? 0x00 : kFlagMasking) | 127); |
| 614 | const uint8_t sizeMsb[] = {static_cast<uint8_t>((size >> 56) & 0xff), |
| 615 | static_cast<uint8_t>((size >> 48) & 0xff), |
| 616 | static_cast<uint8_t>((size >> 40) & 0xff), |
| 617 | static_cast<uint8_t>((size >> 32) & 0xff), |
| 618 | static_cast<uint8_t>((size >> 24) & 0xff), |
| 619 | static_cast<uint8_t>((size >> 16) & 0xff), |
| 620 | static_cast<uint8_t>((size >> 8) & 0xff), |
| 621 | static_cast<uint8_t>(size & 0xff)}; |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 622 | os << span{sizeMsb}; |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 623 | } |
| 624 | |
| 625 | // clients need to mask the input data |
| 626 | if (!m_server) { |
| 627 | // generate masking key |
| 628 | static std::random_device rd; |
| 629 | static std::default_random_engine gen{rd()}; |
| 630 | std::uniform_int_distribution<unsigned int> dist(0, 255); |
| 631 | uint8_t key[4]; |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 632 | for (uint8_t& v : key) { |
| 633 | v = dist(gen); |
| 634 | } |
| 635 | os << span<const uint8_t>{key, 4}; |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 636 | // copy and mask data |
| 637 | int n = 0; |
| 638 | for (auto&& buf : data) { |
| 639 | for (auto&& ch : buf.data()) { |
| 640 | os << static_cast<unsigned char>(static_cast<uint8_t>(ch) ^ key[n++]); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 641 | if (n >= 4) { |
| 642 | n = 0; |
| 643 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 644 | } |
| 645 | } |
| 646 | req->m_startUser = req->m_bufs.size(); |
| 647 | req->m_bufs.append(data.begin(), data.end()); |
| 648 | // don't send the user bufs as we copied their data |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 649 | m_stream.Write(span{req->m_bufs}.subspan(0, req->m_startUser), req); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 650 | } else { |
| 651 | // servers can just send the buffers directly without masking |
| 652 | req->m_startUser = req->m_bufs.size(); |
| 653 | req->m_bufs.append(data.begin(), data.end()); |
| 654 | m_stream.Write(req->m_bufs, req); |
| 655 | } |
| 656 | } |