• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2013 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "net/websockets/websocket_stream.h"
6 
7 #include <optional>
8 #include <ostream>
9 #include <utility>
10 
11 #include "base/check.h"
12 #include "base/check_op.h"
13 #include "base/functional/bind.h"
14 #include "base/location.h"
15 #include "base/logging.h"
16 #include "base/memory/raw_ptr.h"
17 #include "base/memory/weak_ptr.h"
18 #include "base/metrics/histogram_functions.h"
19 #include "base/notreached.h"
20 #include "base/strings/strcat.h"
21 #include "base/time/time.h"
22 #include "base/timer/timer.h"
23 #include "net/base/auth.h"
24 #include "net/base/isolation_info.h"
25 #include "net/base/load_flags.h"
26 #include "net/base/net_errors.h"
27 #include "net/base/request_priority.h"
28 #include "net/base/url_util.h"
29 #include "net/cookies/cookie_util.h"
30 #include "net/http/http_request_headers.h"
31 #include "net/http/http_response_headers.h"
32 #include "net/http/http_response_info.h"
33 #include "net/http/http_status_code.h"
34 #include "net/storage_access_api/status.h"
35 #include "net/traffic_annotation/network_traffic_annotation.h"
36 #include "net/url_request/redirect_info.h"
37 #include "net/url_request/url_request.h"
38 #include "net/url_request/url_request_context.h"
39 #include "net/url_request/websocket_handshake_userdata_key.h"
40 #include "net/websockets/websocket_basic_handshake_stream.h"
41 #include "net/websockets/websocket_event_interface.h"
42 #include "net/websockets/websocket_handshake_constants.h"
43 #include "net/websockets/websocket_handshake_response_info.h"
44 #include "net/websockets/websocket_handshake_stream_base.h"
45 #include "net/websockets/websocket_handshake_stream_create_helper.h"
46 #include "net/websockets/websocket_http2_handshake_stream.h"
47 #include "net/websockets/websocket_http3_handshake_stream.h"
48 #include "url/gurl.h"
49 #include "url/origin.h"
50 
51 namespace net {
52 class SSLCertRequestInfo;
53 class SSLInfo;
54 class SiteForCookies;
55 
56 namespace {
57 
58 // The timeout duration of WebSocket handshake.
59 // It is defined as the same value as the TCP connection timeout value in
60 // net/socket/websocket_transport_client_socket_pool.cc to make it hard for
61 // JavaScript programs to recognize the timeout cause.
62 constexpr int kHandshakeTimeoutIntervalInSeconds = 240;
63 
64 class WebSocketStreamRequestImpl;
65 
66 class Delegate : public URLRequest::Delegate {
67  public:
Delegate(WebSocketStreamRequestImpl * owner)68   explicit Delegate(WebSocketStreamRequestImpl* owner) : owner_(owner) {}
69   ~Delegate() override = default;
70 
71   // Implementation of URLRequest::Delegate methods.
72   int OnConnected(URLRequest* request,
73                   const TransportInfo& info,
74                   CompletionOnceCallback callback) override;
75 
76   void OnReceivedRedirect(URLRequest* request,
77                           const RedirectInfo& redirect_info,
78                           bool* defer_redirect) override;
79 
80   void OnResponseStarted(URLRequest* request, int net_error) override;
81 
82   void OnAuthRequired(URLRequest* request,
83                       const AuthChallengeInfo& auth_info) override;
84 
85   void OnCertificateRequested(URLRequest* request,
86                               SSLCertRequestInfo* cert_request_info) override;
87 
88   void OnSSLCertificateError(URLRequest* request,
89                              int net_error,
90                              const SSLInfo& ssl_info,
91                              bool fatal) override;
92 
93   void OnReadCompleted(URLRequest* request, int bytes_read) override;
94 
95  private:
96   void OnAuthRequiredComplete(URLRequest* request,
97                               const AuthCredentials* auth_credentials);
98 
99   raw_ptr<WebSocketStreamRequestImpl> owner_;
100 };
101 
102 class WebSocketStreamRequestImpl : public WebSocketStreamRequestAPI {
103  public:
WebSocketStreamRequestImpl(const GURL & url,const std::vector<std::string> & requested_subprotocols,const URLRequestContext * context,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,std::unique_ptr<WebSocketStream::ConnectDelegate> connect_delegate,std::unique_ptr<WebSocketStreamRequestAPI> api_delegate)104   WebSocketStreamRequestImpl(
105       const GURL& url,
106       const std::vector<std::string>& requested_subprotocols,
107       const URLRequestContext* context,
108       const url::Origin& origin,
109       const SiteForCookies& site_for_cookies,
110       StorageAccessApiStatus storage_access_api_status,
111       const IsolationInfo& isolation_info,
112       const HttpRequestHeaders& additional_headers,
113       NetworkTrafficAnnotationTag traffic_annotation,
114       std::unique_ptr<WebSocketStream::ConnectDelegate> connect_delegate,
115       std::unique_ptr<WebSocketStreamRequestAPI> api_delegate)
116       : delegate_(this),
117         connect_delegate_(std::move(connect_delegate)),
118         url_request_(context->CreateRequest(url,
119                                             DEFAULT_PRIORITY,
120                                             &delegate_,
121                                             traffic_annotation,
122                                             /*is_for_websockets=*/true)),
123         api_delegate_(std::move(api_delegate)) {
124     DCHECK_EQ(IsolationInfo::RequestType::kOther,
125               isolation_info.request_type());
126 
127     HttpRequestHeaders headers = additional_headers;
128     headers.SetHeader(websockets::kUpgrade, websockets::kWebSocketLowercase);
129     headers.SetHeader(HttpRequestHeaders::kConnection, websockets::kUpgrade);
130     headers.SetHeader(HttpRequestHeaders::kOrigin, origin.Serialize());
131     headers.SetHeader(websockets::kSecWebSocketVersion,
132                       websockets::kSupportedVersion);
133 
134     // Remove HTTP headers that are important to websocket connections: they
135     // will be added later.
136     headers.RemoveHeader(websockets::kSecWebSocketExtensions);
137     headers.RemoveHeader(websockets::kSecWebSocketKey);
138     headers.RemoveHeader(websockets::kSecWebSocketProtocol);
139 
140     url_request_->SetExtraRequestHeaders(headers);
141     url_request_->set_initiator(origin);
142     url_request_->set_site_for_cookies(site_for_cookies);
143     url_request_->set_isolation_info(isolation_info);
144 
145     cookie_util::AddOrRemoveStorageAccessApiOverride(
146         url, storage_access_api_status, url_request_->initiator(),
147         url_request_->cookie_setting_overrides());
148 
149     auto create_helper = std::make_unique<WebSocketHandshakeStreamCreateHelper>(
150         connect_delegate_.get(), requested_subprotocols, this);
151     url_request_->SetUserData(kWebSocketHandshakeUserDataKey,
152                               std::move(create_helper));
153     url_request_->SetLoadFlags(LOAD_DISABLE_CACHE | LOAD_BYPASS_CACHE);
154     connect_delegate_->OnCreateRequest(url_request_.get());
155   }
156 
157   // Destroying this object destroys the URLRequest, which cancels the request
158   // and so terminates the handshake if it is incomplete.
~WebSocketStreamRequestImpl()159   ~WebSocketStreamRequestImpl() override {
160     if (ws_upgrade_success_) {
161       CHECK(url_request_);
162       // "Cancel" the request with an error code indicating the upgrade
163       // succeeded.
164       url_request_->CancelWithError(ERR_WS_UPGRADE);
165     }
166   }
167 
OnBasicHandshakeStreamCreated(WebSocketBasicHandshakeStream * handshake_stream)168   void OnBasicHandshakeStreamCreated(
169       WebSocketBasicHandshakeStream* handshake_stream) override {
170     if (api_delegate_) {
171       api_delegate_->OnBasicHandshakeStreamCreated(handshake_stream);
172     }
173     OnHandshakeStreamCreated(handshake_stream);
174   }
175 
OnHttp2HandshakeStreamCreated(WebSocketHttp2HandshakeStream * handshake_stream)176   void OnHttp2HandshakeStreamCreated(
177       WebSocketHttp2HandshakeStream* handshake_stream) override {
178     if (api_delegate_) {
179       api_delegate_->OnHttp2HandshakeStreamCreated(handshake_stream);
180     }
181     OnHandshakeStreamCreated(handshake_stream);
182   }
183 
OnHttp3HandshakeStreamCreated(WebSocketHttp3HandshakeStream * handshake_stream)184   void OnHttp3HandshakeStreamCreated(
185       WebSocketHttp3HandshakeStream* handshake_stream) override {
186     if (api_delegate_) {
187       api_delegate_->OnHttp3HandshakeStreamCreated(handshake_stream);
188     }
189     OnHandshakeStreamCreated(handshake_stream);
190   }
191 
OnFailure(const std::string & message,int net_error,std::optional<int> response_code)192   void OnFailure(const std::string& message,
193                  int net_error,
194                  std::optional<int> response_code) override {
195     if (api_delegate_)
196       api_delegate_->OnFailure(message, net_error, response_code);
197     failure_message_ = message;
198     failure_net_error_ = net_error;
199     failure_response_code_ = response_code;
200   }
201 
Start(std::unique_ptr<base::OneShotTimer> timer)202   void Start(std::unique_ptr<base::OneShotTimer> timer) {
203     DCHECK(timer);
204     base::TimeDelta timeout(base::Seconds(kHandshakeTimeoutIntervalInSeconds));
205     timer_ = std::move(timer);
206     timer_->Start(FROM_HERE, timeout,
207                   base::BindOnce(&WebSocketStreamRequestImpl::OnTimeout,
208                                  base::Unretained(this)));
209     url_request_->Start();
210   }
211 
PerformUpgrade()212   void PerformUpgrade() {
213     DCHECK(timer_);
214     DCHECK(connect_delegate_);
215 
216     timer_->Stop();
217 
218     if (!handshake_stream_) {
219       ReportFailureWithMessage(
220           "No handshake stream has been created or handshake stream is already "
221           "destroyed.",
222           ERR_FAILED, std::nullopt);
223       return;
224     }
225 
226     if (!handshake_stream_->CanReadFromStream()) {
227       ReportFailureWithMessage("Handshake stream is not readable.",
228                                ERR_CONNECTION_CLOSED, std::nullopt);
229       return;
230     }
231 
232     ws_upgrade_success_ = true;
233     WebSocketHandshakeStreamBase* handshake_stream = handshake_stream_.get();
234     handshake_stream_.reset();
235     auto handshake_response_info =
236         std::make_unique<WebSocketHandshakeResponseInfo>(
237             url_request_->url(), url_request_->response_headers(),
238             url_request_->GetResponseRemoteEndpoint(),
239             url_request_->response_time());
240     connect_delegate_->OnSuccess(handshake_stream->Upgrade(),
241                                  std::move(handshake_response_info));
242   }
243 
FailureMessageFromNetError(int net_error)244   std::string FailureMessageFromNetError(int net_error) {
245     if (net_error == ERR_TUNNEL_CONNECTION_FAILED) {
246       // This error is common and confusing, so special-case it.
247       // TODO(ricea): Include the HostPortPair of the selected proxy server in
248       // the error message. This is not currently possible because it isn't set
249       // in HttpResponseInfo when a ERR_TUNNEL_CONNECTION_FAILED error happens.
250       return "Establishing a tunnel via proxy server failed.";
251     } else {
252       return base::StrCat(
253           {"Error in connection establishment: ", ErrorToString(net_error)});
254     }
255   }
256 
ReportFailure(int net_error,std::optional<int> response_code)257   void ReportFailure(int net_error, std::optional<int> response_code) {
258     DCHECK(timer_);
259     timer_->Stop();
260     if (failure_message_.empty()) {
261       switch (net_error) {
262         case OK:
263         case ERR_IO_PENDING:
264           break;
265         case ERR_ABORTED:
266           failure_message_ = "WebSocket opening handshake was canceled";
267           break;
268         case ERR_TIMED_OUT:
269           failure_message_ = "WebSocket opening handshake timed out";
270           break;
271         default:
272           failure_message_ = FailureMessageFromNetError(net_error);
273           break;
274       }
275     }
276 
277     ReportFailureWithMessage(
278         failure_message_, failure_net_error_.value_or(net_error),
279         failure_response_code_ ? failure_response_code_ : response_code);
280   }
281 
ReportFailureWithMessage(const std::string & failure_message,int net_error,std::optional<int> response_code)282   void ReportFailureWithMessage(const std::string& failure_message,
283                                 int net_error,
284                                 std::optional<int> response_code) {
285     connect_delegate_->OnFailure(failure_message, net_error, response_code);
286   }
287 
connect_delegate() const288   WebSocketStream::ConnectDelegate* connect_delegate() const {
289     return connect_delegate_.get();
290   }
291 
OnTimeout()292   void OnTimeout() {
293     url_request_->CancelWithError(ERR_TIMED_OUT);
294   }
295 
296  private:
OnHandshakeStreamCreated(WebSocketHandshakeStreamBase * handshake_stream)297   void OnHandshakeStreamCreated(
298       WebSocketHandshakeStreamBase* handshake_stream) {
299     DCHECK(handshake_stream);
300 
301     handshake_stream_ = handshake_stream->GetWeakPtr();
302   }
303 
304   // |delegate_| needs to be declared before |url_request_| so that it gets
305   // initialised first and destroyed second.
306   Delegate delegate_;
307 
308   std::unique_ptr<WebSocketStream::ConnectDelegate> connect_delegate_;
309 
310   // Deleting the WebSocketStreamRequestImpl object deletes this URLRequest
311   // object, cancelling the whole connection. Must be destroyed before
312   // `delegate_`, since `url_request_` has a pointer to it, and before
313   // `connect_delegate_`, because there may be a pointer to it further down the
314   // stack.
315   const std::unique_ptr<URLRequest> url_request_;
316 
317   // This is owned by the caller of
318   // WebsocketHandshakeStreamCreateHelper::CreateBasicStream() or
319   // CreateHttp2Stream() or CreateHttp3Stream().  Both the stream and this
320   // object will be destroyed during the destruction of the URLRequest object
321   // associated with the handshake. This is only guaranteed to be a valid
322   // pointer if the handshake succeeded.
323   base::WeakPtr<WebSocketHandshakeStreamBase> handshake_stream_;
324 
325   // The failure information supplied by WebSocketBasicHandshakeStream, if any.
326   std::string failure_message_;
327   std::optional<int> failure_net_error_;
328   std::optional<int> failure_response_code_;
329 
330   // A timer for handshake timeout.
331   std::unique_ptr<base::OneShotTimer> timer_;
332 
333   // Set to true if the websocket upgrade succeeded.
334   bool ws_upgrade_success_ = false;
335 
336   // A delegate for On*HandshakeCreated and OnFailure calls.
337   std::unique_ptr<WebSocketStreamRequestAPI> api_delegate_;
338 };
339 
340 class SSLErrorCallbacks : public WebSocketEventInterface::SSLErrorCallbacks {
341  public:
SSLErrorCallbacks(URLRequest * url_request)342   explicit SSLErrorCallbacks(URLRequest* url_request)
343       : url_request_(url_request->GetWeakPtr()) {}
344 
CancelSSLRequest(int error,const SSLInfo * ssl_info)345   void CancelSSLRequest(int error, const SSLInfo* ssl_info) override {
346     if (!url_request_)
347       return;
348 
349     if (ssl_info) {
350       url_request_->CancelWithSSLError(error, *ssl_info);
351     } else {
352       url_request_->CancelWithError(error);
353     }
354   }
355 
ContinueSSLRequest()356   void ContinueSSLRequest() override {
357     if (url_request_)
358       url_request_->ContinueDespiteLastError();
359   }
360 
361  private:
362   base::WeakPtr<URLRequest> url_request_;
363 };
364 
OnConnected(URLRequest * request,const TransportInfo & info,CompletionOnceCallback callback)365 int Delegate::OnConnected(URLRequest* request,
366                           const TransportInfo& info,
367                           CompletionOnceCallback callback) {
368   owner_->connect_delegate()->OnURLRequestConnected(request, info);
369   return OK;
370 }
371 
OnReceivedRedirect(URLRequest * request,const RedirectInfo & redirect_info,bool * defer_redirect)372 void Delegate::OnReceivedRedirect(URLRequest* request,
373                                   const RedirectInfo& redirect_info,
374                                   bool* defer_redirect) {
375   // This code should never be reached for externally generated redirects,
376   // as WebSocketBasicHandshakeStream is responsible for filtering out
377   // all response codes besides 101, 401, and 407. As such, the URLRequest
378   // should never see a redirect sent over the network. However, internal
379   // redirects also result in this method being called, such as those
380   // caused by HSTS.
381   // Because it's security critical to prevent externally-generated
382   // redirects in WebSockets, perform additional checks to ensure this
383   // is only internal.
384   GURL::Replacements replacements;
385   replacements.SetSchemeStr("wss");
386   GURL expected_url = request->original_url().ReplaceComponents(replacements);
387   if (redirect_info.new_method != "GET" ||
388       redirect_info.new_url != expected_url) {
389     // This should not happen.
390     DLOG(FATAL) << "Unauthorized WebSocket redirect to "
391                 << redirect_info.new_method << " "
392                 << redirect_info.new_url.spec();
393     request->Cancel();
394   }
395 }
396 
OnResponseStarted(URLRequest * request,int net_error)397 void Delegate::OnResponseStarted(URLRequest* request, int net_error) {
398   DCHECK_NE(ERR_IO_PENDING, net_error);
399 
400   const bool is_http2 =
401       request->response_info().connection_info == HttpConnectionInfo::kHTTP2;
402 
403   // All error codes, including OK and ABORTED, as with
404   // Net.ErrorCodesForMainFrame4
405   base::UmaHistogramSparse("Net.WebSocket.ErrorCodes", -net_error);
406   if (is_http2) {
407     base::UmaHistogramSparse("Net.WebSocket.ErrorCodes.Http2", -net_error);
408   }
409   if (net::IsLocalhost(request->url())) {
410     base::UmaHistogramSparse("Net.WebSocket.ErrorCodes_Localhost", -net_error);
411   } else {
412     base::UmaHistogramSparse("Net.WebSocket.ErrorCodes_NotLocalhost",
413                              -net_error);
414   }
415 
416   if (net_error != OK) {
417     DVLOG(3) << "OnResponseStarted (request failed)";
418     owner_->ReportFailure(net_error, std::nullopt);
419     return;
420   }
421   const int response_code = request->GetResponseCode();
422   DVLOG(3) << "OnResponseStarted (response code " << response_code << ")";
423 
424   if (is_http2) {
425     if (response_code == HTTP_OK) {
426       owner_->PerformUpgrade();
427       return;
428     }
429 
430     owner_->ReportFailure(net_error, std::nullopt);
431     return;
432   }
433 
434   switch (response_code) {
435     case HTTP_SWITCHING_PROTOCOLS:
436       owner_->PerformUpgrade();
437       return;
438 
439     case HTTP_UNAUTHORIZED:
440       owner_->ReportFailureWithMessage(
441           "HTTP Authentication failed; no valid credentials available",
442           net_error, response_code);
443       return;
444 
445     case HTTP_PROXY_AUTHENTICATION_REQUIRED:
446       owner_->ReportFailureWithMessage("Proxy authentication failed", net_error,
447                                        response_code);
448       return;
449 
450     default:
451       owner_->ReportFailure(net_error, response_code);
452   }
453 }
454 
OnAuthRequired(URLRequest * request,const AuthChallengeInfo & auth_info)455 void Delegate::OnAuthRequired(URLRequest* request,
456                               const AuthChallengeInfo& auth_info) {
457   std::optional<AuthCredentials> credentials;
458   // This base::Unretained(this) relies on an assumption that |callback| can
459   // be called called during the opening handshake.
460   int rv = owner_->connect_delegate()->OnAuthRequired(
461       auth_info, request->response_headers(),
462       request->GetResponseRemoteEndpoint(),
463       base::BindOnce(&Delegate::OnAuthRequiredComplete, base::Unretained(this),
464                      request),
465       &credentials);
466   request->LogBlockedBy("WebSocketStream::Delegate::OnAuthRequired");
467   if (rv == ERR_IO_PENDING)
468     return;
469   if (rv != OK) {
470     request->LogUnblocked();
471     owner_->ReportFailure(rv, std::nullopt);
472     return;
473   }
474   OnAuthRequiredComplete(request, nullptr);
475 }
476 
OnAuthRequiredComplete(URLRequest * request,const AuthCredentials * credentials)477 void Delegate::OnAuthRequiredComplete(URLRequest* request,
478                                       const AuthCredentials* credentials) {
479   request->LogUnblocked();
480   if (!credentials) {
481     request->CancelAuth();
482     return;
483   }
484   request->SetAuth(*credentials);
485 }
486 
OnCertificateRequested(URLRequest * request,SSLCertRequestInfo * cert_request_info)487 void Delegate::OnCertificateRequested(URLRequest* request,
488                                       SSLCertRequestInfo* cert_request_info) {
489   // This method is called when a client certificate is requested, and the
490   // request context does not already contain a client certificate selection for
491   // the endpoint. In this case, a main frame resource request would pop-up UI
492   // to permit selection of a client certificate, but since WebSockets are
493   // sub-resources they should not pop-up UI and so there is nothing more we can
494   // do.
495   request->Cancel();
496 }
497 
OnSSLCertificateError(URLRequest * request,int net_error,const SSLInfo & ssl_info,bool fatal)498 void Delegate::OnSSLCertificateError(URLRequest* request,
499                                      int net_error,
500                                      const SSLInfo& ssl_info,
501                                      bool fatal) {
502   owner_->connect_delegate()->OnSSLCertificateError(
503       std::make_unique<SSLErrorCallbacks>(request), net_error, ssl_info, fatal);
504 }
505 
OnReadCompleted(URLRequest * request,int bytes_read)506 void Delegate::OnReadCompleted(URLRequest* request, int bytes_read) {
507   NOTREACHED();
508 }
509 
510 }  // namespace
511 
512 WebSocketStreamRequest::~WebSocketStreamRequest() = default;
513 
514 WebSocketStream::WebSocketStream() = default;
515 WebSocketStream::~WebSocketStream() = default;
516 
517 WebSocketStream::ConnectDelegate::~ConnectDelegate() = default;
518 
CreateAndConnectStream(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,URLRequestContext * url_request_context,const NetLogWithSource & net_log,NetworkTrafficAnnotationTag traffic_annotation,std::unique_ptr<ConnectDelegate> connect_delegate)519 std::unique_ptr<WebSocketStreamRequest> WebSocketStream::CreateAndConnectStream(
520     const GURL& socket_url,
521     const std::vector<std::string>& requested_subprotocols,
522     const url::Origin& origin,
523     const SiteForCookies& site_for_cookies,
524     StorageAccessApiStatus storage_access_api_status,
525     const IsolationInfo& isolation_info,
526     const HttpRequestHeaders& additional_headers,
527     URLRequestContext* url_request_context,
528     const NetLogWithSource& net_log,
529     NetworkTrafficAnnotationTag traffic_annotation,
530     std::unique_ptr<ConnectDelegate> connect_delegate) {
531   auto request = std::make_unique<WebSocketStreamRequestImpl>(
532       socket_url, requested_subprotocols, url_request_context, origin,
533       site_for_cookies, storage_access_api_status, isolation_info,
534       additional_headers, traffic_annotation, std::move(connect_delegate),
535       nullptr);
536   request->Start(std::make_unique<base::OneShotTimer>());
537   return std::move(request);
538 }
539 
540 std::unique_ptr<WebSocketStreamRequest>
CreateAndConnectStreamForTesting(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,URLRequestContext * url_request_context,const NetLogWithSource & net_log,NetworkTrafficAnnotationTag traffic_annotation,std::unique_ptr<WebSocketStream::ConnectDelegate> connect_delegate,std::unique_ptr<base::OneShotTimer> timer,std::unique_ptr<WebSocketStreamRequestAPI> api_delegate)541 WebSocketStream::CreateAndConnectStreamForTesting(
542     const GURL& socket_url,
543     const std::vector<std::string>& requested_subprotocols,
544     const url::Origin& origin,
545     const SiteForCookies& site_for_cookies,
546     StorageAccessApiStatus storage_access_api_status,
547     const IsolationInfo& isolation_info,
548     const HttpRequestHeaders& additional_headers,
549     URLRequestContext* url_request_context,
550     const NetLogWithSource& net_log,
551     NetworkTrafficAnnotationTag traffic_annotation,
552     std::unique_ptr<WebSocketStream::ConnectDelegate> connect_delegate,
553     std::unique_ptr<base::OneShotTimer> timer,
554     std::unique_ptr<WebSocketStreamRequestAPI> api_delegate) {
555   auto request = std::make_unique<WebSocketStreamRequestImpl>(
556       socket_url, requested_subprotocols, url_request_context, origin,
557       site_for_cookies, storage_access_api_status, isolation_info,
558       additional_headers, traffic_annotation, std::move(connect_delegate),
559       std::move(api_delegate));
560   request->Start(std::move(timer));
561   return std::move(request);
562 }
563 
564 }  // namespace net
565