blob: feec88f70205ee862b2bb1ef810fc87e093b8325 [file] [log] [blame]
Bence Béky46bfbc12018-02-22 19:28:201// Copyright 2018 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef NET_WEBSOCKETS_WEBSOCKET_HTTP2_HANDSHAKE_STREAM_H_
6#define NET_WEBSOCKETS_WEBSOCKET_HTTP2_HANDSHAKE_STREAM_H_
7
8#include <stdint.h>
9
10#include <memory>
Eric Orthac661912022-01-10 21:44:1711#include <set>
Bence Béky46bfbc12018-02-22 19:28:2012#include <string>
13#include <vector>
14
Keishi Hattori0e45c022021-11-27 09:25:5215#include "base/memory/raw_ptr.h"
Bence Béky46bfbc12018-02-22 19:28:2016#include "base/memory/weak_ptr.h"
Bence Béky334ddfc22021-03-12 15:18:1317#include "base/strings/string_piece.h"
Bence Béky46bfbc12018-02-22 19:28:2018#include "net/base/completion_once_callback.h"
19#include "net/base/io_buffer.h"
20#include "net/base/net_export.h"
21#include "net/base/request_priority.h"
22#include "net/log/net_log_with_source.h"
Bence Békyb7096dc2022-08-02 14:20:4023#include "net/third_party/quiche/src/quiche/spdy/core/http2_header_block.h"
Bence Béky46bfbc12018-02-22 19:28:2024#include "net/websockets/websocket_basic_stream_adapters.h"
25#include "net/websockets/websocket_handshake_stream_base.h"
26#include "net/websockets/websocket_stream.h"
Anton Bikineev068d2912021-05-15 20:43:5227#include "third_party/abseil-cpp/absl/types/optional.h"
Bence Béky46bfbc12018-02-22 19:28:2028
Bence Béky46bfbc12018-02-22 19:28:2029namespace net {
30
31struct LoadTimingInfo;
32class SSLInfo;
33class IOBuffer;
34class SSLCertRequestInfo;
35class IPEndPoint;
36class HttpNetworkSession;
37struct NetErrorDetails;
38class HttpStream;
39class HttpResponseHeaders;
40struct HttpRequestInfo;
41class HttpResponseInfo;
42class SpdySession;
43struct AlternativeService;
44class SpdyStreamRequest;
45struct WebSocketExtensionParams;
46
47class NET_EXPORT_PRIVATE WebSocketHttp2HandshakeStream
48 : public WebSocketHandshakeStreamBase,
49 public WebSocketSpdyStreamAdapter::Delegate {
50 public:
51 // |connect_delegate| and |request| must out-live this object.
52 WebSocketHttp2HandshakeStream(
53 base::WeakPtr<SpdySession> session,
54 WebSocketStream::ConnectDelegate* connect_delegate,
55 std::vector<std::string> requested_sub_protocols,
56 std::vector<std::string> requested_extensions,
Cammie Smith Barnes5191037b72021-03-01 22:06:5357 WebSocketStreamRequestAPI* request,
Eric Orthac661912022-01-10 21:44:1758 std::set<std::string> dns_aliases);
Bence Béky46bfbc12018-02-22 19:28:2059
Peter Boström293b1342021-09-22 17:31:4360 WebSocketHttp2HandshakeStream(const WebSocketHttp2HandshakeStream&) = delete;
61 WebSocketHttp2HandshakeStream& operator=(
62 const WebSocketHttp2HandshakeStream&) = delete;
63
Bence Béky46bfbc12018-02-22 19:28:2064 ~WebSocketHttp2HandshakeStream() override;
65
66 // HttpStream methods.
Ali Beyada0b1a1c2022-04-08 20:08:1467 void RegisterRequest(const HttpRequestInfo* request_info) override;
68 int InitializeStream(bool can_send_early,
Bence Béky46bfbc12018-02-22 19:28:2069 RequestPriority priority,
70 const NetLogWithSource& net_log,
71 CompletionOnceCallback callback) override;
72 int SendRequest(const HttpRequestHeaders& request_headers,
73 HttpResponseInfo* response,
74 CompletionOnceCallback callback) override;
75 int ReadResponseHeaders(CompletionOnceCallback callback) override;
76 int ReadResponseBody(IOBuffer* buf,
77 int buf_len,
78 CompletionOnceCallback callback) override;
79 void Close(bool not_reusable) override;
80 bool IsResponseBodyComplete() const override;
81 bool IsConnectionReused() const override;
82 void SetConnectionReused() override;
83 bool CanReuseConnection() const override;
84 int64_t GetTotalReceivedBytes() const override;
85 int64_t GetTotalSentBytes() const override;
86 bool GetAlternativeService(
87 AlternativeService* alternative_service) const override;
88 bool GetLoadTimingInfo(LoadTimingInfo* load_timing_info) const override;
89 void GetSSLInfo(SSLInfo* ssl_info) override;
90 void GetSSLCertRequestInfo(SSLCertRequestInfo* cert_request_info) override;
Titouan Rigoudy5cf08f12022-05-20 15:49:2291 int GetRemoteEndpoint(IPEndPoint* endpoint) override;
Bence Béky46bfbc12018-02-22 19:28:2092 void Drain(HttpNetworkSession* session) override;
93 void SetPriority(RequestPriority priority) override;
94 void PopulateNetErrorDetails(NetErrorDetails* details) override;
Tsuyoshi Horo34e23cc2022-07-11 02:04:0595 std::unique_ptr<HttpStream> RenewStreamForAuth() override;
Eric Orthac661912022-01-10 21:44:1796 const std::set<std::string>& GetDnsAliases() const override;
Bence Béky334ddfc22021-03-12 15:18:1397 base::StringPiece GetAcceptChViaAlps() const override;
Bence Béky46bfbc12018-02-22 19:28:2098
99 // WebSocketHandshakeStreamBase methods.
100
101 // This is called from the top level once correct handshake response headers
102 // have been received. It creates an appropriate subclass of WebSocketStream
103 // depending on what extensions were negotiated. This object is unusable after
104 // Upgrade() has been called and should be disposed of as soon as possible.
105 std::unique_ptr<WebSocketStream> Upgrade() override;
106
Bence Békyca0da432019-01-24 15:03:20107 base::WeakPtr<WebSocketHandshakeStreamBase> GetWeakPtr() override;
108
Bence Béky46bfbc12018-02-22 19:28:20109 // WebSocketSpdyStreamAdapter::Delegate methods.
110 void OnHeadersSent() override;
Ryan Hamilton0239aac2018-05-19 00:03:13111 void OnHeadersReceived(
Bence Béky4c325e52020-10-22 20:48:01112 const spdy::Http2HeaderBlock& response_headers) override;
Bence Béky46bfbc12018-02-22 19:28:20113 void OnClose(int status) override;
114
115 // Called by |spdy_stream_request_| when requested stream is ready.
116 void StartRequestCallback(int rv);
117
118 private:
119 // Validates the response and sends the finished handshake event.
120 int ValidateResponse();
121
122 // Check that the headers are well-formed and have a 200 status code,
123 // in which case returns OK, otherwise returns ERR_INVALID_RESPONSE.
124 int ValidateUpgradeResponse(const HttpResponseHeaders* headers);
125
Adam Langleya48b636a2020-11-12 23:42:52126 void OnFailure(const std::string& message,
127 int net_error,
Anton Bikineev068d2912021-05-15 20:43:52128 absl::optional<int> response_code);
Bence Béky46bfbc12018-02-22 19:28:20129
Tsuyoshi Horoa0b9c0f2022-06-09 01:41:51130 HandshakeResult result_ = HandshakeResult::HTTP2_INCOMPLETE;
Bence Békyde0be312018-03-13 17:51:58131
Bence Béky46bfbc12018-02-22 19:28:20132 // The connection to open the Websocket stream on.
133 base::WeakPtr<SpdySession> session_;
134
135 // Owned by another object.
136 // |connect_delegate| will live during the lifetime of this object.
Keishi Hattori0e45c022021-11-27 09:25:52137 const raw_ptr<WebSocketStream::ConnectDelegate> connect_delegate_;
Bence Béky46bfbc12018-02-22 19:28:20138
Tsuyoshi Horo432981d52022-06-09 09:50:13139 raw_ptr<HttpResponseInfo> http_response_info_ = nullptr;
Bence Béky46bfbc12018-02-22 19:28:20140
Bence Béky4c325e52020-10-22 20:48:01141 spdy::Http2HeaderBlock http2_request_headers_;
Bence Béky46bfbc12018-02-22 19:28:20142
143 // The sub-protocols we requested.
144 std::vector<std::string> requested_sub_protocols_;
145
146 // The extensions we requested.
147 std::vector<std::string> requested_extensions_;
148
Keishi Hattori0e45c022021-11-27 09:25:52149 const raw_ptr<WebSocketStreamRequestAPI> stream_request_;
Bence Béky46bfbc12018-02-22 19:28:20150
Tsuyoshi Horo432981d52022-06-09 09:50:13151 raw_ptr<const HttpRequestInfo> request_info_ = nullptr;
Bence Béky46bfbc12018-02-22 19:28:20152
153 RequestPriority priority_;
154
155 NetLogWithSource net_log_;
156
157 // SpdyStreamRequest that will create the stream.
158 std::unique_ptr<SpdyStreamRequest> spdy_stream_request_;
159
160 // SpdyStream corresponding to the request.
161 base::WeakPtr<SpdyStream> stream_;
162
163 // WebSocketSpdyStreamAdapter holding a WeakPtr to |stream_|.
164 // This can be passed on to WebSocketBasicStream when created.
165 std::unique_ptr<WebSocketSpdyStreamAdapter> stream_adapter_;
166
167 // True if |stream_| has been created then closed.
Tsuyoshi Horoa0b9c0f2022-06-09 01:41:51168 bool stream_closed_ = false;
Bence Béky46bfbc12018-02-22 19:28:20169
170 // The error code corresponding to the reason for closing the stream.
171 // Only meaningful if |stream_closed_| is true.
Tsuyoshi Horoa0b9c0f2022-06-09 01:41:51172 int stream_error_ = OK;
Bence Béky46bfbc12018-02-22 19:28:20173
174 // True if complete response headers have been received.
Tsuyoshi Horoa0b9c0f2022-06-09 01:41:51175 bool response_headers_complete_ = false;
Bence Béky46bfbc12018-02-22 19:28:20176
177 // Save callback provided in asynchronous HttpStream methods.
178 CompletionOnceCallback callback_;
179
180 // The sub-protocol selected by the server.
181 std::string sub_protocol_;
182
183 // The extension(s) selected by the server.
184 std::string extensions_;
185
186 // The extension parameters. The class is defined in the implementation file
187 // to avoid including extension-related header files here.
188 std::unique_ptr<WebSocketExtensionParams> extension_params_;
189
Eric Orthac661912022-01-10 21:44:17190 // Stores any DNS aliases for the remote endpoint. Includes all known aliases,
191 // e.g. from A, AAAA, or HTTPS, not just from the address used for the
192 // connection, in no particular order. These are stored in the stream instead
193 // of the session due to complications related to IP-pooling.
194 std::set<std::string> dns_aliases_;
Cammie Smith Barnes5191037b72021-03-01 22:06:53195
Jeremy Romand54000b22019-07-08 18:40:16196 base::WeakPtrFactory<WebSocketHttp2HandshakeStream> weak_ptr_factory_{this};
Bence Béky46bfbc12018-02-22 19:28:20197};
198
199} // namespace net
200
201#endif // NET_WEBSOCKETS_WEBSOCKET_HTTP2_HANDSHAKE_STREAM_H_