#include "net/websockets/websocket_basic_handshake_stream.h"
#include <stddef.h>
#include <array>
#include <set>
#include <string_view>
#include <type_traits>
#include <utility>
#include "base/base64.h"
#include "base/check.h"
#include "base/check_op.h"
#include "base/functional/bind.h"
#include "base/functional/callback.h"
#include "base/memory/scoped_refptr.h"
#include "base/metrics/histogram_functions.h"
#include "base/strings/strcat.h"
#include "base/strings/string_util.h"
#include "base/strings/stringprintf.h"
#include "base/time/time.h"
#include "base/values.h"
#include "crypto/random.h"
#include "net/base/net_errors.h"
#include "net/http/http_network_session.h"
#include "net/http/http_request_headers.h"
#include "net/http/http_request_info.h"
#include "net/http/http_response_body_drainer.h"
#include "net/http/http_response_headers.h"
#include "net/http/http_response_info.h"
#include "net/http/http_status_code.h"
#include "net/http/http_stream_parser.h"
#include "net/http/http_version.h"
#include "net/log/net_log_event_type.h"
#include "net/socket/client_socket_handle.h"
#include "net/socket/stream_socket.h"
#include "net/socket/websocket_transport_client_socket_pool.h"
#include "net/ssl/ssl_cert_request_info.h"
#include "net/ssl/ssl_info.h"
#include "net/traffic_annotation/network_traffic_annotation.h"
#include "net/websockets/websocket_basic_stream.h"
#include "net/websockets/websocket_basic_stream_adapters.h"
#include "net/websockets/websocket_deflate_predictor_impl.h"
#include "net/websockets/websocket_deflate_stream.h"
#include "net/websockets/websocket_handshake_challenge.h"
#include "net/websockets/websocket_handshake_constants.h"
#include "net/websockets/websocket_handshake_request_info.h"
#include "net/websockets/websocket_stream.h"
namespace net {
class HttpStream;
class IOBuffer;
class IPEndPoint;
struct AlternativeService;
struct LoadTimingInfo;
struct NetErrorDetails;
namespace {
constexpr char kConnectionErrorStatusLine[] = …;
}
namespace {
enum GetHeaderResult { … };
std::string MissingHeaderMessage(const std::string& header_name) { … }
std::string GenerateHandshakeChallenge() { … }
GetHeaderResult GetSingleHeaderValue(const HttpResponseHeaders* headers,
std::string_view name,
std::string* value) { … }
bool ValidateHeaderHasSingleValue(GetHeaderResult result,
const std::string& header_name,
std::string* failure_message) { … }
bool ValidateUpgrade(const HttpResponseHeaders* headers,
std::string* failure_message) { … }
bool ValidateSecWebSocketAccept(const HttpResponseHeaders* headers,
const std::string& expected,
std::string* failure_message) { … }
bool ValidateConnection(const HttpResponseHeaders* headers,
std::string* failure_message) { … }
base::Value::Dict NetLogFailureParam(int net_error,
const std::string& message) { … }
}
WebSocketBasicHandshakeStream::WebSocketBasicHandshakeStream(
std::unique_ptr<StreamSocketHandle> connection,
WebSocketStream::ConnectDelegate* connect_delegate,
bool is_for_get_to_http_proxy,
std::vector<std::string> requested_sub_protocols,
std::vector<std::string> requested_extensions,
WebSocketStreamRequestAPI* request,
WebSocketEndpointLockManager* websocket_endpoint_lock_manager)
: … { … }
WebSocketBasicHandshakeStream::~WebSocketBasicHandshakeStream() { … }
void WebSocketBasicHandshakeStream::RegisterRequest(
const HttpRequestInfo* request_info) { … }
int WebSocketBasicHandshakeStream::InitializeStream(
bool can_send_early,
RequestPriority priority,
const NetLogWithSource& net_log,
CompletionOnceCallback callback) { … }
int WebSocketBasicHandshakeStream::SendRequest(
const HttpRequestHeaders& headers,
HttpResponseInfo* response,
CompletionOnceCallback callback) { … }
int WebSocketBasicHandshakeStream::ReadResponseHeaders(
CompletionOnceCallback callback) { … }
int WebSocketBasicHandshakeStream::ReadResponseBody(
IOBuffer* buf,
int buf_len,
CompletionOnceCallback callback) { … }
void WebSocketBasicHandshakeStream::Close(bool not_reusable) { … }
bool WebSocketBasicHandshakeStream::IsResponseBodyComplete() const { … }
bool WebSocketBasicHandshakeStream::IsConnectionReused() const { … }
void WebSocketBasicHandshakeStream::SetConnectionReused() { … }
bool WebSocketBasicHandshakeStream::CanReuseConnection() const { … }
int64_t WebSocketBasicHandshakeStream::GetTotalReceivedBytes() const { … }
int64_t WebSocketBasicHandshakeStream::GetTotalSentBytes() const { … }
bool WebSocketBasicHandshakeStream::GetAlternativeService(
AlternativeService* alternative_service) const { … }
bool WebSocketBasicHandshakeStream::GetLoadTimingInfo(
LoadTimingInfo* load_timing_info) const { … }
void WebSocketBasicHandshakeStream::GetSSLInfo(SSLInfo* ssl_info) { … }
int WebSocketBasicHandshakeStream::GetRemoteEndpoint(IPEndPoint* endpoint) { … }
void WebSocketBasicHandshakeStream::PopulateNetErrorDetails(
NetErrorDetails* ) { … }
void WebSocketBasicHandshakeStream::Drain(HttpNetworkSession* session) { … }
void WebSocketBasicHandshakeStream::SetPriority(RequestPriority priority) { … }
std::unique_ptr<HttpStream>
WebSocketBasicHandshakeStream::RenewStreamForAuth() { … }
const std::set<std::string>& WebSocketBasicHandshakeStream::GetDnsAliases()
const { … }
std::string_view WebSocketBasicHandshakeStream::GetAcceptChViaAlps() const { … }
std::unique_ptr<WebSocketStream> WebSocketBasicHandshakeStream::Upgrade() { … }
bool WebSocketBasicHandshakeStream::CanReadFromStream() const { … }
base::WeakPtr<WebSocketHandshakeStreamBase>
WebSocketBasicHandshakeStream::GetWeakPtr() { … }
void WebSocketBasicHandshakeStream::SetWebSocketKeyForTesting(
const std::string& key) { … }
void WebSocketBasicHandshakeStream::ReadResponseHeadersCallback(
CompletionOnceCallback callback,
int result) { … }
int WebSocketBasicHandshakeStream::ValidateResponse(int rv) { … }
int WebSocketBasicHandshakeStream::ValidateUpgradeResponse(
const HttpResponseHeaders* headers) { … }
void WebSocketBasicHandshakeStream::OnFailure(
const std::string& message,
int net_error,
std::optional<int> response_code) { … }
}