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
|
// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef NET_WEBSOCKETS_WEBSOCKET_HTTP3_HANDSHAKE_STREAM_H_
#define NET_WEBSOCKETS_WEBSOCKET_HTTP3_HANDSHAKE_STREAM_H_
#include <stdint.h>
#include <memory>
#include <optional>
#include <set>
#include <string>
#include <string_view>
#include <vector>
#include "base/memory/raw_ptr.h"
#include "base/memory/weak_ptr.h"
#include "base/time/time.h"
#include "net/base/completion_once_callback.h"
#include "net/base/net_errors.h"
#include "net/base/net_export.h"
#include "net/base/request_priority.h"
#include "net/log/net_log_with_source.h"
#include "net/quic/quic_chromium_client_session.h"
#include "net/quic/quic_session_pool.h"
#include "net/third_party/quiche/src/quiche/common/http/http_header_block.h"
#include "net/websockets/websocket_basic_stream_adapters.h"
#include "net/websockets/websocket_handshake_stream_base.h"
#include "net/websockets/websocket_stream.h"
namespace net {
class HttpNetworkSession;
class HttpRequestHeaders;
class HttpResponseHeaders;
class HttpResponseInfo;
class HttpStream;
class IOBuffer;
class IPEndPoint;
class SSLInfo;
struct AlternativeService;
struct HttpRequestInfo;
struct LoadTimingInfo;
struct NetErrorDetails;
class NET_EXPORT_PRIVATE WebSocketHttp3HandshakeStream final
: public WebSocketHandshakeStreamBase,
public WebSocketQuicStreamAdapter::Delegate {
public:
WebSocketHttp3HandshakeStream(
std::unique_ptr<QuicChromiumClientSession::Handle> session,
WebSocketStream::ConnectDelegate* connect_delegate,
std::vector<std::string> requested_sub_protocols,
std::vector<std::string> requested_extensions,
WebSocketStreamRequestAPI* request,
std::set<std::string> dns_aliases);
WebSocketHttp3HandshakeStream(const WebSocketHttp3HandshakeStream&) = delete;
WebSocketHttp3HandshakeStream& operator=(
const WebSocketHttp3HandshakeStream&) = delete;
~WebSocketHttp3HandshakeStream() override;
// HttpStream methods.
void RegisterRequest(const HttpRequestInfo* request_info) override;
int InitializeStream(bool can_send_early,
RequestPriority priority,
const NetLogWithSource& net_log,
CompletionOnceCallback callback) override;
int SendRequest(const HttpRequestHeaders& request_headers,
HttpResponseInfo* response,
CompletionOnceCallback callback) override;
int ReadResponseHeaders(CompletionOnceCallback callback) override;
int ReadResponseBody(IOBuffer* buf,
int buf_len,
CompletionOnceCallback callback) override;
void Close(bool not_reusable) override;
bool IsResponseBodyComplete() const override;
bool IsConnectionReused() const override;
void SetConnectionReused() override;
bool CanReuseConnection() const override;
int64_t GetTotalReceivedBytes() const override;
int64_t GetTotalSentBytes() const override;
bool GetAlternativeService(
AlternativeService* alternative_service) const override;
bool GetLoadTimingInfo(LoadTimingInfo* load_timing_info) const override;
void GetSSLInfo(SSLInfo* ssl_info) override;
int GetRemoteEndpoint(IPEndPoint* endpoint) override;
void Drain(HttpNetworkSession* session) override;
void SetPriority(RequestPriority priority) override;
void PopulateNetErrorDetails(NetErrorDetails* details) override;
std::unique_ptr<HttpStream> RenewStreamForAuth() override;
const std::set<std::string>& GetDnsAliases() const override;
std::string_view GetAcceptChViaAlps() const override;
// WebSocketHandshakeStreamBase methods.
// This is called from the top level once correct handshake response headers
// have been received. It creates an appropriate subclass of WebSocketStream
// depending on what extensions were negotiated. This object is unusable after
// Upgrade() has been called and should be disposed of as soon as possible.
std::unique_ptr<WebSocketStream> Upgrade() override;
bool CanReadFromStream() const override;
base::WeakPtr<WebSocketHandshakeStreamBase> GetWeakPtr() override;
// WebSocketQuicStreamAdapter::Delegate methods.
void OnHeadersSent() override;
void OnHeadersReceived(
const quiche::HttpHeaderBlock& response_headers) override;
void OnClose(int status) override;
private:
void ReceiveAdapterAndStartRequest(
std::unique_ptr<WebSocketQuicStreamAdapter> adapter);
// Validates the response and sends the finished handshake event.
int ValidateResponse();
// Check that the headers are well-formed and have a 200 status code,
// in which case returns OK, otherwise returns ERR_INVALID_RESPONSE.
int ValidateUpgradeResponse(const HttpResponseHeaders* headers);
void OnFailure(const std::string& message,
int net_error,
std::optional<int> response_code);
void OnHandshakeConfirmed(CompletionOnceCallback callback, int rv);
HandshakeResult result_ = HandshakeResult::HTTP3_INCOMPLETE;
std::unique_ptr<WebSocketSpdyStreamAdapter> adapter_;
// True if `stream_` has been created then closed.
bool stream_closed_ = false;
// The error code corresponding to the reason for closing the stream.
// Only meaningful if `stream_closed_` is true.
int stream_error_ = OK;
// True if complete response headers have been received.
bool response_headers_complete_ = false;
// Time the request was issued.
base::Time request_time_;
std::unique_ptr<QuicChromiumClientSession::Handle> session_;
// Owned by another object.
// `connect_delegate` will live during the lifetime of this object.
const raw_ptr<WebSocketStream::ConnectDelegate> connect_delegate_;
raw_ptr<HttpResponseInfo> http_response_info_ = nullptr;
quiche::HttpHeaderBlock http3_request_headers_;
// The sub-protocols we requested.
std::vector<std::string> requested_sub_protocols_;
// The extensions we requested.
std::vector<std::string> requested_extensions_;
const raw_ptr<WebSocketStreamRequestAPI> stream_request_;
raw_ptr<const HttpRequestInfo> request_info_ = nullptr;
RequestPriority priority_;
NetLogWithSource net_log_;
// WebSocketQuicStreamAdapter holding a WeakPtr to `stream_`.
// This can be passed on to WebSocketBasicStream when created.
std::unique_ptr<WebSocketQuicStreamAdapter> stream_adapter_;
CompletionOnceCallback callback_;
// The sub-protocol selected by the server.
std::string sub_protocol_;
// The extension(s) selected by the server.
std::string extensions_;
// The extension parameters. The class is defined in the implementation file
// to avoid including extension-related header files here.
std::unique_ptr<WebSocketExtensionParams> extension_params_;
std::set<std::string> dns_aliases_;
base::WeakPtrFactory<WebSocketHttp3HandshakeStream> weak_ptr_factory_{this};
};
} // namespace net
#endif // NET_WEBSOCKETS_WEBSOCKET_HTTP3_HANDSHAKE_STREAM_H_
|