#ifdef UNSAFE_BUFFERS_BUILD
#pragma allow_unsafe_buffers
#endif
#include "net/websockets/websocket_channel.h"
#include <limits.h>
#include <stddef.h>
#include <string.h>
#include <algorithm>
#include <iterator>
#include <ostream>
#include <string_view>
#include <utility>
#include <vector>
#include "base/big_endian.h"
#include "base/check.h"
#include "base/check_op.h"
#include "base/functional/bind.h"
#include "base/location.h"
#include "base/logging.h"
#include "base/memory/raw_ptr.h"
#include "base/numerics/byte_conversions.h"
#include "base/numerics/safe_conversions.h"
#include "base/ranges/algorithm.h"
#include "base/strings/stringprintf.h"
#include "base/time/time.h"
#include "base/values.h"
#include "net/base/io_buffer.h"
#include "net/base/net_errors.h"
#include "net/http/http_response_headers.h"
#include "net/log/net_log_event_type.h"
#include "net/log/net_log_with_source.h"
#include "net/storage_access_api/status.h"
#include "net/traffic_annotation/network_traffic_annotation.h"
#include "net/websockets/websocket_errors.h"
#include "net/websockets/websocket_event_interface.h"
#include "net/websockets/websocket_frame.h"
#include "net/websockets/websocket_handshake_request_info.h"
#include "net/websockets/websocket_handshake_response_info.h"
#include "net/websockets/websocket_stream.h"
namespace net {
class AuthChallengeInfo;
class AuthCredentials;
class SSLInfo;
namespace {
StreamingUtf8Validator;
constexpr size_t kWebSocketCloseCodeLength = …;
constexpr int kClosingHandshakeTimeoutSeconds = …;
constexpr int kUnderlyingConnectionCloseTimeoutSeconds = …;
ChannelState;
constexpr size_t kMaximumCloseReasonLength = …;
bool IsStrictlyValidCloseStatusCode(int code) { … }
void GetFrameTypeForOpcode(WebSocketFrameHeader::OpCode opcode,
std::string* name) { … }
base::Value::Dict NetLogFailParam(uint16_t code,
std::string_view reason,
std::string_view message) { … }
class DependentIOBuffer : public WrappedIOBuffer { … };
}
class WebSocketChannel::SendBuffer { … };
void WebSocketChannel::SendBuffer::AddFrame(
std::unique_ptr<WebSocketFrame> frame,
scoped_refptr<IOBuffer> buffer) { … }
class WebSocketChannel::ConnectDelegate
: public WebSocketStream::ConnectDelegate { … };
WebSocketChannel::WebSocketChannel(
std::unique_ptr<WebSocketEventInterface> event_interface,
URLRequestContext* url_request_context)
: … { … }
WebSocketChannel::~WebSocketChannel() { … }
void WebSocketChannel::SendAddChannelRequest(
const GURL& socket_url,
const std::vector<std::string>& requested_subprotocols,
const url::Origin& origin,
const SiteForCookies& site_for_cookies,
StorageAccessApiStatus storage_access_api_status,
const IsolationInfo& isolation_info,
const HttpRequestHeaders& additional_headers,
NetworkTrafficAnnotationTag traffic_annotation) { … }
void WebSocketChannel::SetState(State new_state) { … }
bool WebSocketChannel::InClosingState() const { … }
WebSocketChannel::ChannelState WebSocketChannel::SendFrame(
bool fin,
WebSocketFrameHeader::OpCode op_code,
scoped_refptr<IOBuffer> buffer,
size_t buffer_size) { … }
ChannelState WebSocketChannel::StartClosingHandshake(
uint16_t code,
const std::string& reason) { … }
void WebSocketChannel::SendAddChannelRequestForTesting(
const GURL& socket_url,
const std::vector<std::string>& requested_subprotocols,
const url::Origin& origin,
const SiteForCookies& site_for_cookies,
StorageAccessApiStatus storage_access_api_status,
const IsolationInfo& isolation_info,
const HttpRequestHeaders& additional_headers,
NetworkTrafficAnnotationTag traffic_annotation,
WebSocketStreamRequestCreationCallback callback) { … }
void WebSocketChannel::SetClosingHandshakeTimeoutForTesting(
base::TimeDelta delay) { … }
void WebSocketChannel::SetUnderlyingConnectionCloseTimeoutForTesting(
base::TimeDelta delay) { … }
void WebSocketChannel::SendAddChannelRequestWithSuppliedCallback(
const GURL& socket_url,
const std::vector<std::string>& requested_subprotocols,
const url::Origin& origin,
const SiteForCookies& site_for_cookies,
StorageAccessApiStatus storage_access_api_status,
const IsolationInfo& isolation_info,
const HttpRequestHeaders& additional_headers,
NetworkTrafficAnnotationTag traffic_annotation,
WebSocketStreamRequestCreationCallback callback) { … }
void WebSocketChannel::OnCreateURLRequest(URLRequest* request) { … }
void WebSocketChannel::OnURLRequestConnected(URLRequest* request,
const TransportInfo& info) { … }
void WebSocketChannel::OnConnectSuccess(
std::unique_ptr<WebSocketStream> stream,
std::unique_ptr<WebSocketHandshakeResponseInfo> response) { … }
void WebSocketChannel::OnConnectFailure(const std::string& message,
int net_error,
std::optional<int> response_code) { … }
void WebSocketChannel::OnSSLCertificateError(
std::unique_ptr<WebSocketEventInterface::SSLErrorCallbacks>
ssl_error_callbacks,
int net_error,
const SSLInfo& ssl_info,
bool fatal) { … }
int WebSocketChannel::OnAuthRequired(
const AuthChallengeInfo& auth_info,
scoped_refptr<HttpResponseHeaders> response_headers,
const IPEndPoint& remote_endpoint,
base::OnceCallback<void(const AuthCredentials*)> callback,
std::optional<AuthCredentials>* credentials) { … }
void WebSocketChannel::OnStartOpeningHandshake(
std::unique_ptr<WebSocketHandshakeRequestInfo> request) { … }
ChannelState WebSocketChannel::WriteFrames() { … }
ChannelState WebSocketChannel::OnWriteDone(bool synchronous, int result) { … }
ChannelState WebSocketChannel::ReadFrames() { … }
ChannelState WebSocketChannel::OnReadDone(bool synchronous, int result) { … }
ChannelState WebSocketChannel::HandleFrame(
std::unique_ptr<WebSocketFrame> frame) { … }
ChannelState WebSocketChannel::HandleFrameByState(
const WebSocketFrameHeader::OpCode opcode,
bool final,
base::span<const char> payload) { … }
ChannelState WebSocketChannel::HandleDataFrame(
WebSocketFrameHeader::OpCode opcode,
bool final,
base::span<const char> payload) { … }
ChannelState WebSocketChannel::HandleCloseFrame(uint16_t code,
const std::string& reason) { … }
ChannelState WebSocketChannel::RespondToClosingHandshake() { … }
ChannelState WebSocketChannel::SendFrameInternal(
bool fin,
WebSocketFrameHeader::OpCode op_code,
scoped_refptr<IOBuffer> buffer,
uint64_t buffer_size) { … }
void WebSocketChannel::FailChannel(const std::string& message,
uint16_t code,
const std::string& reason) { … }
ChannelState WebSocketChannel::SendClose(uint16_t code,
const std::string& reason) { … }
bool WebSocketChannel::ParseClose(base::span<const char> payload,
uint16_t* code,
std::string* reason,
std::string* message) { … }
void WebSocketChannel::DoDropChannel(bool was_clean,
uint16_t code,
const std::string& reason) { … }
void WebSocketChannel::CloseTimeout() { … }
}