blob: 69e14b8b28e5a85d9f98dd54557086049bd4fbbd [file] [log] [blame]
[email protected]d51365e2013-11-27 10:46:521// Copyright 2013 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_BASIC_HANDSHAKE_STREAM_H_
6#define NET_WEBSOCKETS_WEBSOCKET_BASIC_HANDSHAKE_STREAM_H_
7
sclittlebe1ccf62015-09-02 19:40:368#include <stdint.h>
9
danakj9c5cab52016-04-16 00:54:3310#include <memory>
[email protected]d51365e2013-11-27 10:46:5211#include <string>
12#include <vector>
13
tfarinaea94afc232015-10-20 04:23:3614#include "base/macros.h"
Bence Béky7d0c74d2018-03-05 08:31:0915#include "base/optional.h"
Bence Békya25e3f72018-02-13 21:13:3916#include "net/base/completion_once_callback.h"
[email protected]d51365e2013-11-27 10:46:5217#include "net/base/net_export.h"
18#include "net/http/http_basic_state.h"
19#include "net/websockets/websocket_handshake_stream_base.h"
[email protected]cd48ed12014-01-22 14:34:2220#include "url/gurl.h"
[email protected]d51365e2013-11-27 10:46:5221
22namespace net {
23
24class ClientSocketHandle;
25class HttpResponseHeaders;
26class HttpResponseInfo;
27class HttpStreamParser;
Bence Békyda280c62018-04-12 15:08:3728class WebSocketEndpointLockManager;
[email protected]0be93922014-01-29 00:42:4529struct WebSocketExtensionParams;
Adam Rice6f75c0f2018-06-04 08:00:0530class WebSocketStreamRequestAPI;
[email protected]0be93922014-01-29 00:42:4531
Adam Ricec786ad8a2018-05-22 09:49:1532class NET_EXPORT_PRIVATE WebSocketBasicHandshakeStream final
[email protected]d51365e2013-11-27 10:46:5233 : public WebSocketHandshakeStreamBase {
34 public:
[email protected]8aba0172014-07-03 12:09:5335 // |connect_delegate| and |failure_message| must out-live this object.
[email protected]d51365e2013-11-27 10:46:5236 WebSocketBasicHandshakeStream(
danakj9c5cab52016-04-16 00:54:3337 std::unique_ptr<ClientSocketHandle> connection,
[email protected]cd48ed12014-01-22 14:34:2238 WebSocketStream::ConnectDelegate* connect_delegate,
[email protected]d51365e2013-11-27 10:46:5239 bool using_proxy,
40 std::vector<std::string> requested_sub_protocols,
[email protected]8aba0172014-07-03 12:09:5341 std::vector<std::string> requested_extensions,
Adam Rice6f75c0f2018-06-04 08:00:0542 WebSocketStreamRequestAPI* request,
Bence Békyda280c62018-04-12 15:08:3743 WebSocketEndpointLockManager* websocket_endpoint_lock_manager);
[email protected]d51365e2013-11-27 10:46:5244
dchengb03027d2014-10-21 12:00:2045 ~WebSocketBasicHandshakeStream() override;
[email protected]d51365e2013-11-27 10:46:5246
47 // HttpStreamBase methods
dchengb03027d2014-10-21 12:00:2048 int InitializeStream(const HttpRequestInfo* request_info,
Steven Valdezb4ff0412018-01-18 22:39:2749 bool can_send_early,
dchengb03027d2014-10-21 12:00:2050 RequestPriority priority,
tfarina428341112016-09-22 13:38:2051 const NetLogWithSource& net_log,
Bence Békya25e3f72018-02-13 21:13:3952 CompletionOnceCallback callback) override;
dchengb03027d2014-10-21 12:00:2053 int SendRequest(const HttpRequestHeaders& request_headers,
54 HttpResponseInfo* response,
Bence Békya25e3f72018-02-13 21:13:3955 CompletionOnceCallback callback) override;
56 int ReadResponseHeaders(CompletionOnceCallback callback) override;
dchengb03027d2014-10-21 12:00:2057 int ReadResponseBody(IOBuffer* buf,
58 int buf_len,
Bence Békya25e3f72018-02-13 21:13:3959 CompletionOnceCallback callback) override;
dchengb03027d2014-10-21 12:00:2060 void Close(bool not_reusable) override;
61 bool IsResponseBodyComplete() const override;
dchengb03027d2014-10-21 12:00:2062 bool IsConnectionReused() const override;
63 void SetConnectionReused() override;
mmenkebd84c392015-09-02 14:12:3464 bool CanReuseConnection() const override;
sclittle4de1bab92015-09-22 21:28:2465 int64_t GetTotalReceivedBytes() const override;
sclittlebe1ccf62015-09-02 19:40:3666 int64_t GetTotalSentBytes() const override;
rchcd379012017-04-12 21:53:3267 bool GetAlternativeService(
68 AlternativeService* alternative_service) const override;
dchengb03027d2014-10-21 12:00:2069 bool GetLoadTimingInfo(LoadTimingInfo* load_timing_info) const override;
70 void GetSSLInfo(SSLInfo* ssl_info) override;
71 void GetSSLCertRequestInfo(SSLCertRequestInfo* cert_request_info) override;
ttuttled9dbc652015-09-29 20:00:5972 bool GetRemoteEndpoint(IPEndPoint* endpoint) override;
dchengb03027d2014-10-21 12:00:2073 void Drain(HttpNetworkSession* session) override;
74 void SetPriority(RequestPriority priority) override;
zhongyica364fbb2015-12-12 03:39:1275 void PopulateNetErrorDetails(NetErrorDetails* details) override;
yhiranoa7e05bb2014-11-06 05:40:3976 HttpStream* RenewStreamForAuth() override;
77
[email protected]d51365e2013-11-27 10:46:5278
79 // This is called from the top level once correct handshake response headers
80 // have been received. It creates an appropriate subclass of WebSocketStream
81 // depending on what extensions were negotiated. This object is unusable after
82 // Upgrade() has been called and should be disposed of as soon as possible.
danakj9c5cab52016-04-16 00:54:3383 std::unique_ptr<WebSocketStream> Upgrade() override;
[email protected]d51365e2013-11-27 10:46:5284
Bence Békyca0da432019-01-24 15:03:2085 base::WeakPtr<WebSocketHandshakeStreamBase> GetWeakPtr() override;
86
[email protected]a31ecc02013-12-05 08:30:5587 // Set the value used for the next Sec-WebSocket-Key header
88 // deterministically. The key is only used once, and then discarded.
89 // For tests only.
90 void SetWebSocketKeyForTesting(const std::string& key);
91
[email protected]d51365e2013-11-27 10:46:5292 private:
93 // A wrapper for the ReadResponseHeaders callback that checks whether or not
94 // the connection has been accepted.
Bence Békya25e3f72018-02-13 21:13:3995 void ReadResponseHeadersCallback(CompletionOnceCallback callback, int result);
[email protected]d51365e2013-11-27 10:46:5296
[email protected]cd48ed12014-01-22 14:34:2297 void OnFinishOpeningHandshake();
98
[email protected]e5760f522014-02-05 12:28:5099 // Validates the response and sends the finished handshake event.
ricea24c195f2015-02-26 12:18:55100 int ValidateResponse(int rv);
[email protected]d51365e2013-11-27 10:46:52101
102 // Check that the headers are well-formed for a 101 response, and returns
103 // OK if they are, otherwise returns ERR_INVALID_RESPONSE.
[email protected]e5760f522014-02-05 12:28:50104 int ValidateUpgradeResponse(const HttpResponseHeaders* headers);
[email protected]d51365e2013-11-27 10:46:52105
tyoshinoccfcfde2016-07-21 14:06:55106 void OnFailure(const std::string& message);
[email protected]d51365e2013-11-27 10:46:52107
tyoshinoccfcfde2016-07-21 14:06:55108 HttpStreamParser* parser() const { return state_.parser(); }
[email protected]8aba0172014-07-03 12:09:53109
Bence Békyde0be312018-03-13 17:51:58110 HandshakeResult result_;
111
[email protected]cd48ed12014-01-22 14:34:22112 // The request URL.
113 GURL url_;
114
[email protected]d51365e2013-11-27 10:46:52115 // HttpBasicState holds most of the handshake-related state.
116 HttpBasicState state_;
117
[email protected]cd48ed12014-01-22 14:34:22118 // Owned by another object.
119 // |connect_delegate| will live during the lifetime of this object.
Adam Rice8b382c602018-06-04 12:36:39120 WebSocketStream::ConnectDelegate* const connect_delegate_;
[email protected]cd48ed12014-01-22 14:34:22121
[email protected]d51365e2013-11-27 10:46:52122 // This is stored in SendRequest() for use by ReadResponseHeaders().
123 HttpResponseInfo* http_response_info_;
124
[email protected]a31ecc02013-12-05 08:30:55125 // The key to be sent in the next Sec-WebSocket-Key header. Usually NULL (the
126 // key is generated on the fly).
Bence Béky7d0c74d2018-03-05 08:31:09127 base::Optional<std::string> handshake_challenge_for_testing_;
[email protected]a31ecc02013-12-05 08:30:55128
[email protected]d51365e2013-11-27 10:46:52129 // The required value for the Sec-WebSocket-Accept header.
130 std::string handshake_challenge_response_;
131
132 // The sub-protocols we requested.
133 std::vector<std::string> requested_sub_protocols_;
134
135 // The extensions we requested.
136 std::vector<std::string> requested_extensions_;
137
138 // The sub-protocol selected by the server.
139 std::string sub_protocol_;
140
141 // The extension(s) selected by the server.
142 std::string extensions_;
143
[email protected]0be93922014-01-29 00:42:45144 // The extension parameters. The class is defined in the implementation file
145 // to avoid including extension-related header files here.
danakj9c5cab52016-04-16 00:54:33146 std::unique_ptr<WebSocketExtensionParams> extension_params_;
[email protected]0be93922014-01-29 00:42:45147
Adam Rice6f75c0f2018-06-04 08:00:05148 WebSocketStreamRequestAPI* const stream_request_;
Bence Békyda280c62018-04-12 15:08:37149
150 WebSocketEndpointLockManager* const websocket_endpoint_lock_manager_;
[email protected]96868202014-01-09 10:38:04151
Bence Békyca0da432019-01-24 15:03:20152 base::WeakPtrFactory<WebSocketBasicHandshakeStream> weak_ptr_factory_;
153
[email protected]d51365e2013-11-27 10:46:52154 DISALLOW_COPY_AND_ASSIGN(WebSocketBasicHandshakeStream);
155};
156
157} // namespace net
158
159#endif // NET_WEBSOCKETS_WEBSOCKET_BASIC_HANDSHAKE_STREAM_H_