1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373
|
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/test/chromedriver/net/websocket.h"
#include <stddef.h>
#include <stdint.h>
#include <string.h>
#include <memory>
#include <string_view>
#include <utility>
#include <vector>
#include "base/base64.h"
#include "base/containers/span.h"
#include "base/functional/bind.h"
#include "base/functional/callback_helpers.h"
#include "base/hash/sha1.h"
#include "base/json/json_writer.h"
#include "base/rand_util.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_view_util.h"
#include "base/strings/stringprintf.h"
#include "base/values.h"
#include "build/build_config.h"
#include "net/base/address_list.h"
#include "net/base/io_buffer.h"
#include "net/base/ip_address.h"
#include "net/base/ip_endpoint.h"
#include "net/base/net_errors.h"
#include "net/base/sys_addrinfo.h"
#include "net/http/http_response_headers.h"
#include "net/http/http_util.h"
#include "net/log/net_log_source.h"
#include "net/traffic_annotation/network_traffic_annotation_test_helper.h"
#include "net/websockets/websocket_frame.h"
#if BUILDFLAG(IS_WIN)
#include <Winsock2.h>
#endif
namespace {
bool ResolveHost(const std::string& host,
uint16_t port,
net::AddressList* address_list) {
struct addrinfo hints = {};
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
struct addrinfo* result;
if (getaddrinfo(host.c_str(), nullptr, &hints, &result))
return false;
auto list = net::AddressList::CreateFromAddrinfo(result);
*address_list = net::AddressList::CopyWithPort(list, port);
freeaddrinfo(result);
return !address_list->empty();
}
} // namespace
WebSocket::WebSocket(const GURL& url,
WebSocketListener* listener,
size_t read_buffer_size)
: url_(url),
listener_(listener),
state_(INITIALIZED),
write_buffer_(base::MakeRefCounted<net::DrainableIOBuffer>(
base::MakeRefCounted<net::IOBufferWithSize>(),
0)),
read_buffer_(
base::MakeRefCounted<net::IOBufferWithSize>(read_buffer_size)) {}
WebSocket::~WebSocket() {
CHECK(thread_checker_.CalledOnValidThread());
}
void WebSocket::Connect(net::CompletionOnceCallback callback) {
CHECK(thread_checker_.CalledOnValidThread());
CHECK_EQ(INITIALIZED, state_);
net::IPAddress address;
net::AddressList addresses;
uint16_t port = static_cast<uint16_t>(url_.EffectiveIntPort());
if (ParseURLHostnameToAddress(url_.host(), &address)) {
addresses = net::AddressList::CreateFromIPAddress(address, port);
} else {
if (!ResolveHost(url_.HostNoBrackets(), port, &addresses)) {
std::move(callback).Run(net::ERR_ADDRESS_UNREACHABLE);
return;
}
base::Value::List endpoints;
for (auto endpoint : addresses)
endpoints.Append(endpoint.ToStringWithoutPort());
std::string json;
CHECK(base::JSONWriter::Write(endpoints, &json));
VLOG(0) << "resolved " << url_.HostNoBracketsPiece() << " to " << json;
}
if (url_.host() == "localhost") {
// Ensure that both localhost addresses are included.
// See https://bugs.chromium.org/p/chromedriver/issues/detail?id=3316.
// Put IPv4 address at front, followed by IPv6 address, since that is
// the ordering used by DevTools.
addresses.endpoints().insert(
addresses.begin(),
{net::IPEndPoint(net::IPAddress::IPv4Localhost(), port),
net::IPEndPoint(net::IPAddress::IPv6Localhost(), port)});
addresses.Deduplicate();
}
net::NetLogSource source;
socket_ = std::make_unique<net::TCPClientSocket>(addresses, nullptr, nullptr,
nullptr, source);
state_ = CONNECTING;
connect_callback_ = std::move(callback);
int code = socket_->Connect(
base::BindOnce(&WebSocket::OnSocketConnect, base::Unretained(this)));
VLOG(4) << "WebSocket::Connect code=" << net::ErrorToShortString(code);
if (code != net::ERR_IO_PENDING)
OnSocketConnect(code);
}
bool WebSocket::Send(const std::string& message) {
VLOG(4) << "WebSocket::Send " << message;
CHECK(thread_checker_.CalledOnValidThread());
if (state_ != OPEN)
return false;
net::WebSocketFrameHeader header(net::WebSocketFrameHeader::kOpCodeText);
header.final = true;
header.masked = true;
header.payload_length = message.length();
size_t header_size = net::GetWebSocketFrameHeaderSize(header);
net::WebSocketMaskingKey masking_key = net::GenerateWebSocketMaskingKey();
std::string header_str;
header_str.resize(header_size);
CHECK_EQ(header_size,
base::checked_cast<size_t>(net::WriteWebSocketFrameHeader(
header, &masking_key, base::as_writable_byte_span(header_str))));
std::string masked_message = message;
net::MaskWebSocketFramePayload(masking_key, 0,
base::as_writable_byte_span(masked_message));
Write(header_str + masked_message);
return true;
}
void WebSocket::OnSocketConnect(int code) {
VLOG(4) << "WebSocket::OnSocketConnect code="
<< net::ErrorToShortString(code);
if (code != net::OK) {
VLOG(1) << "failed to connect to " << url_.HostNoBracketsPiece()
<< " (error " << code << ")";
Close(code);
return;
}
sec_key_ = base::Base64Encode(base::RandBytesAsVector(16));
std::string handshake = base::StringPrintf(
"GET %s HTTP/1.1\r\n"
"Host: %s\r\n"
"Upgrade: websocket\r\n"
"Connection: Upgrade\r\n"
"Sec-WebSocket-Key: %s\r\n"
"Sec-WebSocket-Version: 13\r\n"
"Pragma: no-cache\r\n"
"Cache-Control: no-cache\r\n"
"\r\n",
url_.path().c_str(),
url_.host().c_str(),
sec_key_.c_str());
VLOG(4) << "WebSocket::OnSocketConnect handshake\n" << handshake;
Write(handshake);
if (state_ == CLOSED) {
// The call to Write() above would call Close() if it encounters an error,
// in which case it's no longer safe to do anything else. Close() has
// already called the callback function, if any.
return;
}
Read();
}
void WebSocket::Write(const std::string& data) {
pending_write_ += data;
if (!write_buffer_->BytesRemaining())
ContinueWritingIfNecessary();
}
void WebSocket::OnWrite(int code) {
if (!socket_->IsConnected()) {
// Supposedly if |StreamSocket| is closed, the error code may be undefined.
Close(net::ERR_FAILED);
return;
}
if (code < 0) {
Close(code);
return;
}
write_buffer_->DidConsume(code);
ContinueWritingIfNecessary();
}
void WebSocket::ContinueWritingIfNecessary() {
if (!write_buffer_->BytesRemaining()) {
if (pending_write_.empty())
return;
const size_t pending_write_length = pending_write_.length();
write_buffer_ = base::MakeRefCounted<net::DrainableIOBuffer>(
base::MakeRefCounted<net::StringIOBuffer>(std::move(pending_write_)),
pending_write_length);
pending_write_.clear();
}
int code = socket_->Write(
write_buffer_.get(), write_buffer_->BytesRemaining(),
base::BindOnce(&WebSocket::OnWrite, base::Unretained(this)),
TRAFFIC_ANNOTATION_FOR_TESTS);
if (code != net::ERR_IO_PENDING)
OnWrite(code);
}
void WebSocket::Read() {
while (true) {
int code = socket_->Read(
read_buffer_.get(), read_buffer_->size(),
base::BindOnce(&WebSocket::OnRead, base::Unretained(this), true));
if (code == net::ERR_IO_PENDING)
break;
OnRead(false, code);
if (state_ == CLOSED)
break;
}
}
void WebSocket::OnRead(bool read_again, int code) {
if (code == 0) {
// On POSIX and FUCHSIA:
// code >= 0 is the result of POSIX read function call:
// code > 0 denotes the amount of successfully read bytes
// code == 0 means that we have reached EOF.
// The latter one is issued upon receiving a FIN packet.
// code < 0 is an error code
// On Windows:
// code >=0 is the result of recv function call (winsocks.h)
// It has the same semantic as in the POSIX case
// code < 0 is an error code
code = net::ERR_CONNECTION_CLOSED;
}
if (code < 0) {
VLOG(4) << "WebSocket::OnRead error " << net::ErrorToShortString(code);
Close(code);
return;
}
if (state_ == CONNECTING) {
OnReadDuringHandshake(
read_buffer_->span().first(base::checked_cast<size_t>(code)));
} else if (state_ == OPEN) {
OnReadDuringOpen(
read_buffer_->span().first(base::checked_cast<size_t>(code)));
}
// If we were called by the event loop due to arrival of data, call Read()
// again to read more data. If we were called by Read(), however, simply
// return to Read() and let it call socket_->Read() to read more data, and
// potentially call OnRead() again. This is necessary to avoid mutual
// recursion between Read and OnRead, which can cause stack overflow (e.g.,
// see https://crbug.com/877105).
if (read_again && state_ != CLOSED)
Read();
}
void WebSocket::OnReadDuringHandshake(base::span<const uint8_t> data_span) {
VLOG(4) << "WebSocket::OnReadDuringHandshake\n"
<< base::as_string_view(data_span);
handshake_response_ += base::as_string_view(data_span);
size_t headers_end = net::HttpUtil::LocateEndOfHeaders(
base::as_byte_span(handshake_response_), 0);
if (headers_end == std::string::npos)
return;
const char kMagicKey[] = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
std::string websocket_accept =
base::Base64Encode(base::SHA1HashString(sec_key_ + kMagicKey));
auto headers = base::MakeRefCounted<net::HttpResponseHeaders>(
net::HttpUtil::AssembleRawHeaders(
std::string_view(handshake_response_.data(), headers_end)));
if (headers->response_code() != 101 ||
!headers->HasHeaderValue("Upgrade", "WebSocket") ||
!headers->HasHeaderValue("Connection", "Upgrade") ||
!headers->HasHeaderValue("Sec-WebSocket-Accept", websocket_accept)) {
Close(net::ERR_FAILED);
return;
}
std::string leftover_message = handshake_response_.substr(headers_end);
handshake_response_.clear();
sec_key_.clear();
state_ = OPEN;
InvokeConnectCallback(net::OK);
if (!leftover_message.empty()) {
OnReadDuringOpen(base::as_writable_byte_span(leftover_message));
}
}
void WebSocket::OnReadDuringOpen(base::span<uint8_t> data_span) {
std::vector<std::unique_ptr<net::WebSocketFrameChunk>> frame_chunks;
// Call the parser's Decode method
CHECK(parser_.Decode(data_span, &frame_chunks));
for (size_t i = 0; i < frame_chunks.size(); ++i) {
const auto& header = frame_chunks[i]->header;
if (header) {
DCHECK_EQ(0u, current_frame_offset_);
is_current_frame_masked_ = header->masked;
current_masking_key_ = header->masking_key;
switch (header->opcode) {
case net::WebSocketFrameHeader::kOpCodeText:
is_current_message_opcode_text_ = true;
break;
case net::WebSocketFrameHeader::kOpCodeContinuation:
// This doesn't change the opcode of the current message.
break;
default:
is_current_message_opcode_text_ = false;
break;
}
}
if (!is_current_message_opcode_text_) {
continue;
}
auto& buffer = frame_chunks[i]->payload;
std::vector<char> payload(buffer.begin(), buffer.end());
if (is_current_frame_masked_) {
MaskWebSocketFramePayload(current_masking_key_, current_frame_offset_,
base::as_writable_byte_span(payload));
}
next_message_ += std::string(payload.data(), payload.size());
current_frame_offset_ += payload.size();
if (frame_chunks[i]->final_chunk) {
VLOG(4) << "WebSocket::OnReadDuringOpen " << next_message_;
listener_->OnMessageReceived(next_message_);
next_message_.clear();
current_frame_offset_ = 0;
}
}
}
void WebSocket::InvokeConnectCallback(int code) {
CHECK(!connect_callback_.is_null());
std::move(connect_callback_).Run(code);
}
void WebSocket::Close(int code) {
socket_->Disconnect();
if (!connect_callback_.is_null())
InvokeConnectCallback(code);
if (state_ == OPEN)
listener_->OnClose();
state_ = CLOSED;
}
|