Avi Drissman | 6459548 | 2022-09-14 20:52:29 | [diff] [blame] | 1 | // Copyright 2013 The Chromium Authors |
[email protected] | d51365e | 2013-11-27 10:46:52 | [diff] [blame] | 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_basic_handshake_stream.h" |
| 6 | |
tfarina | ea94afc23 | 2015-10-20 04:23:36 | [diff] [blame] | 7 | #include <stddef.h> |
[email protected] | d51365e | 2013-11-27 10:46:52 | [diff] [blame] | 8 | #include <algorithm> |
| 9 | #include <iterator> |
[email protected] | 0be9392 | 2014-01-29 00:42:45 | [diff] [blame] | 10 | #include <set> |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 11 | #include <utility> |
[email protected] | d51365e | 2013-11-27 10:46:52 | [diff] [blame] | 12 | |
| 13 | #include "base/base64.h" |
[email protected] | d51365e | 2013-11-27 10:46:52 | [diff] [blame] | 14 | #include "base/bind.h" |
Hans Wennborg | 0924470b | 2020-04-27 21:08:05 | [diff] [blame] | 15 | #include "base/check_op.h" |
yhirano | 27b2b57 | 2014-10-30 11:23:44 | [diff] [blame] | 16 | #include "base/compiler_specific.h" |
Ilya Sherman | 0eb3980 | 2017-12-08 20:58:18 | [diff] [blame] | 17 | #include "base/metrics/histogram_functions.h" |
[email protected] | 0be9392 | 2014-01-29 00:42:45 | [diff] [blame] | 18 | #include "base/strings/string_number_conversions.h" |
[email protected] | 69d7a49 | 2014-02-19 08:36:32 | [diff] [blame] | 19 | #include "base/strings/string_piece.h" |
[email protected] | d51365e | 2013-11-27 10:46:52 | [diff] [blame] | 20 | #include "base/strings/string_util.h" |
[email protected] | 9686820 | 2014-01-09 10:38:04 | [diff] [blame] | 21 | #include "base/strings/stringprintf.h" |
[email protected] | cd48ed1 | 2014-01-22 14:34:22 | [diff] [blame] | 22 | #include "base/time/time.h" |
[email protected] | d51365e | 2013-11-27 10:46:52 | [diff] [blame] | 23 | #include "crypto/random.h" |
halton.huo | 299e200 | 2014-12-02 04:39:24 | [diff] [blame] | 24 | #include "net/base/io_buffer.h" |
Tsuyoshi Horo | 01faed6 | 2019-02-20 22:11:37 | [diff] [blame] | 25 | #include "net/base/ip_endpoint.h" |
Tsuyoshi Horo | b074f876 | 2022-07-22 23:25:14 | [diff] [blame] | 26 | #include "net/http/http_network_session.h" |
[email protected] | d51365e | 2013-11-27 10:46:52 | [diff] [blame] | 27 | #include "net/http/http_request_headers.h" |
| 28 | #include "net/http/http_request_info.h" |
| 29 | #include "net/http/http_response_body_drainer.h" |
| 30 | #include "net/http/http_response_headers.h" |
| 31 | #include "net/http/http_status_code.h" |
| 32 | #include "net/http/http_stream_parser.h" |
| 33 | #include "net/socket/client_socket_handle.h" |
Bence Béky | 3cb271d | 2018-03-29 22:00:48 | [diff] [blame] | 34 | #include "net/socket/ssl_client_socket.h" |
Bence Béky | da280c6 | 2018-04-12 15:08:37 | [diff] [blame] | 35 | #include "net/socket/websocket_endpoint_lock_manager.h" |
[email protected] | 65486614 | 2014-06-24 22:53:31 | [diff] [blame] | 36 | #include "net/socket/websocket_transport_client_socket_pool.h" |
Matt Menke | f171ff8b | 2019-02-05 20:06:13 | [diff] [blame] | 37 | #include "net/ssl/ssl_cert_request_info.h" |
| 38 | #include "net/ssl/ssl_info.h" |
[email protected] | d51365e | 2013-11-27 10:46:52 | [diff] [blame] | 39 | #include "net/websockets/websocket_basic_stream.h" |
Bence Béky | 7294fc2 | 2018-02-08 14:26:17 | [diff] [blame] | 40 | #include "net/websockets/websocket_basic_stream_adapters.h" |
yhirano | 8387aee | 2015-09-14 05:46:49 | [diff] [blame] | 41 | #include "net/websockets/websocket_deflate_parameters.h" |
[email protected] | 0be9392 | 2014-01-29 00:42:45 | [diff] [blame] | 42 | #include "net/websockets/websocket_deflate_predictor.h" |
| 43 | #include "net/websockets/websocket_deflate_predictor_impl.h" |
| 44 | #include "net/websockets/websocket_deflate_stream.h" |
| 45 | #include "net/websockets/websocket_deflater.h" |
ricea | 11bdcd0 | 2014-11-20 09:57:07 | [diff] [blame] | 46 | #include "net/websockets/websocket_handshake_challenge.h" |
[email protected] | d51365e | 2013-11-27 10:46:52 | [diff] [blame] | 47 | #include "net/websockets/websocket_handshake_constants.h" |
[email protected] | cd48ed1 | 2014-01-22 14:34:22 | [diff] [blame] | 48 | #include "net/websockets/websocket_handshake_request_info.h" |
| 49 | #include "net/websockets/websocket_handshake_response_info.h" |
[email protected] | d51365e | 2013-11-27 10:46:52 | [diff] [blame] | 50 | #include "net/websockets/websocket_stream.h" |
| 51 | |
| 52 | namespace net { |
[email protected] | 0be9392 | 2014-01-29 00:42:45 | [diff] [blame] | 53 | |
yhirano | 27b2b57 | 2014-10-30 11:23:44 | [diff] [blame] | 54 | namespace { |
| 55 | |
ricea | 23c3f94 | 2015-02-02 13:35:13 | [diff] [blame] | 56 | const char kConnectionErrorStatusLine[] = "HTTP/1.1 503 Connection Error"; |
| 57 | |
yhirano | 27b2b57 | 2014-10-30 11:23:44 | [diff] [blame] | 58 | } // namespace |
| 59 | |
[email protected] | d51365e | 2013-11-27 10:46:52 | [diff] [blame] | 60 | namespace { |
| 61 | |
[email protected] | 9686820 | 2014-01-09 10:38:04 | [diff] [blame] | 62 | enum GetHeaderResult { |
| 63 | GET_HEADER_OK, |
| 64 | GET_HEADER_MISSING, |
| 65 | GET_HEADER_MULTIPLE, |
| 66 | }; |
| 67 | |
| 68 | std::string MissingHeaderMessage(const std::string& header_name) { |
| 69 | return std::string("'") + header_name + "' header is missing"; |
| 70 | } |
| 71 | |
[email protected] | d51365e | 2013-11-27 10:46:52 | [diff] [blame] | 72 | std::string GenerateHandshakeChallenge() { |
| 73 | std::string raw_challenge(websockets::kRawChallengeLength, '\0'); |
Daniel Cheng | 5feb16f | 2022-02-28 06:52:07 | [diff] [blame] | 74 | crypto::RandBytes(std::data(raw_challenge), raw_challenge.length()); |
[email protected] | d51365e | 2013-11-27 10:46:52 | [diff] [blame] | 75 | std::string encoded_challenge; |
[email protected] | 33fca12 | 2013-12-11 01:48:50 | [diff] [blame] | 76 | base::Base64Encode(raw_challenge, &encoded_challenge); |
[email protected] | d51365e | 2013-11-27 10:46:52 | [diff] [blame] | 77 | return encoded_challenge; |
| 78 | } |
| 79 | |
[email protected] | 9686820 | 2014-01-09 10:38:04 | [diff] [blame] | 80 | GetHeaderResult GetSingleHeaderValue(const HttpResponseHeaders* headers, |
David Benjamin | b6c2dd16 | 2022-10-24 10:46:06 | [diff] [blame] | 81 | base::StringPiece name, |
[email protected] | 9686820 | 2014-01-09 10:38:04 | [diff] [blame] | 82 | std::string* value) { |
olli.raula | ee489a5 | 2016-01-25 08:37:10 | [diff] [blame] | 83 | size_t iter = 0; |
[email protected] | 9686820 | 2014-01-09 10:38:04 | [diff] [blame] | 84 | size_t num_values = 0; |
| 85 | std::string temp_value; |
olli.raula | ee489a5 | 2016-01-25 08:37:10 | [diff] [blame] | 86 | while (headers->EnumerateHeader(&iter, name, &temp_value)) { |
[email protected] | 9686820 | 2014-01-09 10:38:04 | [diff] [blame] | 87 | if (++num_values > 1) |
| 88 | return GET_HEADER_MULTIPLE; |
| 89 | *value = temp_value; |
[email protected] | d51365e | 2013-11-27 10:46:52 | [diff] [blame] | 90 | } |
[email protected] | 9686820 | 2014-01-09 10:38:04 | [diff] [blame] | 91 | return num_values > 0 ? GET_HEADER_OK : GET_HEADER_MISSING; |
| 92 | } |
| 93 | |
| 94 | bool ValidateHeaderHasSingleValue(GetHeaderResult result, |
| 95 | const std::string& header_name, |
| 96 | std::string* failure_message) { |
| 97 | if (result == GET_HEADER_MISSING) { |
| 98 | *failure_message = MissingHeaderMessage(header_name); |
| 99 | return false; |
| 100 | } |
| 101 | if (result == GET_HEADER_MULTIPLE) { |
Bence Béky | b28709c2 | 2018-03-06 13:03:44 | [diff] [blame] | 102 | *failure_message = |
| 103 | WebSocketHandshakeStreamBase::MultipleHeaderValuesMessage(header_name); |
[email protected] | 9686820 | 2014-01-09 10:38:04 | [diff] [blame] | 104 | return false; |
| 105 | } |
| 106 | DCHECK_EQ(result, GET_HEADER_OK); |
| 107 | return true; |
| 108 | } |
| 109 | |
| 110 | bool ValidateUpgrade(const HttpResponseHeaders* headers, |
| 111 | std::string* failure_message) { |
| 112 | std::string value; |
| 113 | GetHeaderResult result = |
| 114 | GetSingleHeaderValue(headers, websockets::kUpgrade, &value); |
| 115 | if (!ValidateHeaderHasSingleValue(result, |
| 116 | websockets::kUpgrade, |
| 117 | failure_message)) { |
| 118 | return false; |
| 119 | } |
| 120 | |
Dan McArdle | 80e9dc8 | 2022-05-23 01:43:12 | [diff] [blame] | 121 | if (!base::EqualsCaseInsensitiveASCII(value, |
| 122 | websockets::kWebSocketLowercase)) { |
[email protected] | 9686820 | 2014-01-09 10:38:04 | [diff] [blame] | 123 | *failure_message = |
| 124 | "'Upgrade' header value is not 'WebSocket': " + value; |
| 125 | return false; |
| 126 | } |
| 127 | return true; |
| 128 | } |
| 129 | |
| 130 | bool ValidateSecWebSocketAccept(const HttpResponseHeaders* headers, |
| 131 | const std::string& expected, |
| 132 | std::string* failure_message) { |
| 133 | std::string actual; |
| 134 | GetHeaderResult result = |
| 135 | GetSingleHeaderValue(headers, websockets::kSecWebSocketAccept, &actual); |
| 136 | if (!ValidateHeaderHasSingleValue(result, |
| 137 | websockets::kSecWebSocketAccept, |
| 138 | failure_message)) { |
| 139 | return false; |
| 140 | } |
| 141 | |
| 142 | if (expected != actual) { |
| 143 | *failure_message = "Incorrect 'Sec-WebSocket-Accept' header value"; |
| 144 | return false; |
| 145 | } |
| 146 | return true; |
| 147 | } |
| 148 | |
| 149 | bool ValidateConnection(const HttpResponseHeaders* headers, |
| 150 | std::string* failure_message) { |
| 151 | // Connection header is permitted to contain other tokens. |
| 152 | if (!headers->HasHeader(HttpRequestHeaders::kConnection)) { |
| 153 | *failure_message = MissingHeaderMessage(HttpRequestHeaders::kConnection); |
| 154 | return false; |
| 155 | } |
| 156 | if (!headers->HasHeaderValue(HttpRequestHeaders::kConnection, |
| 157 | websockets::kUpgrade)) { |
| 158 | *failure_message = "'Connection' header value must contain 'Upgrade'"; |
| 159 | return false; |
| 160 | } |
| 161 | return true; |
[email protected] | d51365e | 2013-11-27 10:46:52 | [diff] [blame] | 162 | } |
| 163 | |
Nanami Mikiya | ca3588c | 2022-01-31 03:22:25 | [diff] [blame] | 164 | base::Value NetLogFailureParam(int net_error, const std::string& message) { |
Travis Skare | adae7ff | 2022-07-27 16:23:33 | [diff] [blame] | 165 | base::Value::Dict dict; |
| 166 | dict.Set("net_error", net_error); |
| 167 | dict.Set("message", message); |
| 168 | return base::Value(std::move(dict)); |
Nanami Mikiya | ca3588c | 2022-01-31 03:22:25 | [diff] [blame] | 169 | } |
| 170 | |
[email protected] | d51365e | 2013-11-27 10:46:52 | [diff] [blame] | 171 | } // namespace |
| 172 | |
| 173 | WebSocketBasicHandshakeStream::WebSocketBasicHandshakeStream( |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 174 | std::unique_ptr<ClientSocketHandle> connection, |
[email protected] | cd48ed1 | 2014-01-22 14:34:22 | [diff] [blame] | 175 | WebSocketStream::ConnectDelegate* connect_delegate, |
[email protected] | d51365e | 2013-11-27 10:46:52 | [diff] [blame] | 176 | bool using_proxy, |
| 177 | std::vector<std::string> requested_sub_protocols, |
[email protected] | 8aba017 | 2014-07-03 12:09:53 | [diff] [blame] | 178 | std::vector<std::string> requested_extensions, |
Adam Rice | 6f75c0f | 2018-06-04 08:00:05 | [diff] [blame] | 179 | WebSocketStreamRequestAPI* request, |
Bence Béky | da280c6 | 2018-04-12 15:08:37 | [diff] [blame] | 180 | WebSocketEndpointLockManager* websocket_endpoint_lock_manager) |
Tsuyoshi Horo | a0b9c0f | 2022-06-09 01:41:51 | [diff] [blame] | 181 | : state_(std::move(connection), using_proxy), |
[email protected] | cd48ed1 | 2014-01-22 14:34:22 | [diff] [blame] | 182 | connect_delegate_(connect_delegate), |
Adam Rice | 8b382c60 | 2018-06-04 12:36:39 | [diff] [blame] | 183 | requested_sub_protocols_(std::move(requested_sub_protocols)), |
| 184 | requested_extensions_(std::move(requested_extensions)), |
Bence Béky | da280c6 | 2018-04-12 15:08:37 | [diff] [blame] | 185 | stream_request_(request), |
Jeremy Roman | d54000b2 | 2019-07-08 18:40:16 | [diff] [blame] | 186 | websocket_endpoint_lock_manager_(websocket_endpoint_lock_manager) { |
[email protected] | 8aba017 | 2014-07-03 12:09:53 | [diff] [blame] | 187 | DCHECK(connect_delegate); |
tyoshino | ccfcfde | 2016-07-21 14:06:55 | [diff] [blame] | 188 | DCHECK(request); |
[email protected] | 8aba017 | 2014-07-03 12:09:53 | [diff] [blame] | 189 | } |
[email protected] | d51365e | 2013-11-27 10:46:52 | [diff] [blame] | 190 | |
Bence Béky | de0be31 | 2018-03-13 17:51:58 | [diff] [blame] | 191 | WebSocketBasicHandshakeStream::~WebSocketBasicHandshakeStream() { |
Adam Rice | 8b382c60 | 2018-06-04 12:36:39 | [diff] [blame] | 192 | // Some members are "stolen" by RenewStreamForAuth() and should not be touched |
| 193 | // here. Particularly |connect_delegate_|, |stream_request_|, and |
| 194 | // |websocket_endpoint_lock_manager_|. |
| 195 | |
| 196 | // TODO(ricea): What's the right thing to do here if we renewed the stream for |
| 197 | // auth? Currently we record it as INCOMPLETE. |
Bence Béky | de0be31 | 2018-03-13 17:51:58 | [diff] [blame] | 198 | RecordHandshakeResult(result_); |
| 199 | } |
[email protected] | d51365e | 2013-11-27 10:46:52 | [diff] [blame] | 200 | |
Ali Beyad | a0b1a1c | 2022-04-08 20:08:14 | [diff] [blame] | 201 | void WebSocketBasicHandshakeStream::RegisterRequest( |
| 202 | const HttpRequestInfo* request_info) { |
| 203 | DCHECK(request_info); |
| 204 | DCHECK(request_info->traffic_annotation.is_valid()); |
| 205 | request_info_ = request_info; |
| 206 | } |
| 207 | |
[email protected] | d51365e | 2013-11-27 10:46:52 | [diff] [blame] | 208 | int WebSocketBasicHandshakeStream::InitializeStream( |
Steven Valdez | b4ff041 | 2018-01-18 22:39:27 | [diff] [blame] | 209 | bool can_send_early, |
[email protected] | d51365e | 2013-11-27 10:46:52 | [diff] [blame] | 210 | RequestPriority priority, |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 211 | const NetLogWithSource& net_log, |
Bence Béky | a25e3f7 | 2018-02-13 21:13:39 | [diff] [blame] | 212 | CompletionOnceCallback callback) { |
Ali Beyad | a0b1a1c | 2022-04-08 20:08:14 | [diff] [blame] | 213 | url_ = request_info_->url; |
Nanami Mikiya | ca3588c | 2022-01-31 03:22:25 | [diff] [blame] | 214 | net_log_ = net_log; |
Steven Valdez | 1c185917 | 2019-04-10 15:33:28 | [diff] [blame] | 215 | // The WebSocket may receive a socket in the early data state from |
| 216 | // HttpNetworkTransaction, which means it must call ConfirmHandshake() for |
| 217 | // requests that need replay protection. However, the first request on any |
| 218 | // WebSocket stream is a GET with an idempotent request |
| 219 | // (https://tools.ietf.org/html/rfc6455#section-1.3), so there is no need to |
| 220 | // call ConfirmHandshake(). |
| 221 | // |
| 222 | // Data after the WebSockets handshake may not be replayable, but the |
| 223 | // handshake is guaranteed to be confirmed once the HTTP response is received. |
| 224 | DCHECK(can_send_early); |
Ali Beyad | a0b1a1c | 2022-04-08 20:08:14 | [diff] [blame] | 225 | state_.Initialize(request_info_, priority, net_log); |
| 226 | // RequestInfo is no longer needed after this point. |
| 227 | request_info_ = nullptr; |
[email protected] | d51365e | 2013-11-27 10:46:52 | [diff] [blame] | 228 | return OK; |
| 229 | } |
| 230 | |
| 231 | int WebSocketBasicHandshakeStream::SendRequest( |
| 232 | const HttpRequestHeaders& headers, |
| 233 | HttpResponseInfo* response, |
Bence Béky | a25e3f7 | 2018-02-13 21:13:39 | [diff] [blame] | 234 | CompletionOnceCallback callback) { |
[email protected] | d51365e | 2013-11-27 10:46:52 | [diff] [blame] | 235 | DCHECK(!headers.HasHeader(websockets::kSecWebSocketKey)); |
| 236 | DCHECK(!headers.HasHeader(websockets::kSecWebSocketProtocol)); |
| 237 | DCHECK(!headers.HasHeader(websockets::kSecWebSocketExtensions)); |
| 238 | DCHECK(headers.HasHeader(HttpRequestHeaders::kOrigin)); |
| 239 | DCHECK(headers.HasHeader(websockets::kUpgrade)); |
| 240 | DCHECK(headers.HasHeader(HttpRequestHeaders::kConnection)); |
| 241 | DCHECK(headers.HasHeader(websockets::kSecWebSocketVersion)); |
| 242 | DCHECK(parser()); |
| 243 | |
| 244 | http_response_info_ = response; |
| 245 | |
| 246 | // Create a copy of the headers object, so that we can add the |
| 247 | // Sec-WebSockey-Key header. |
| 248 | HttpRequestHeaders enriched_headers; |
| 249 | enriched_headers.CopyFrom(headers); |
[email protected] | a31ecc0 | 2013-12-05 08:30:55 | [diff] [blame] | 250 | std::string handshake_challenge; |
Bence Béky | 7d0c74d | 2018-03-05 08:31:09 | [diff] [blame] | 251 | if (handshake_challenge_for_testing_.has_value()) { |
| 252 | handshake_challenge = handshake_challenge_for_testing_.value(); |
[email protected] | a31ecc0 | 2013-12-05 08:30:55 | [diff] [blame] | 253 | handshake_challenge_for_testing_.reset(); |
| 254 | } else { |
| 255 | handshake_challenge = GenerateHandshakeChallenge(); |
| 256 | } |
[email protected] | d51365e | 2013-11-27 10:46:52 | [diff] [blame] | 257 | enriched_headers.SetHeader(websockets::kSecWebSocketKey, handshake_challenge); |
| 258 | |
[email protected] | d51365e | 2013-11-27 10:46:52 | [diff] [blame] | 259 | AddVectorHeaderIfNonEmpty(websockets::kSecWebSocketExtensions, |
| 260 | requested_extensions_, |
| 261 | &enriched_headers); |
[email protected] | 0be9392 | 2014-01-29 00:42:45 | [diff] [blame] | 262 | AddVectorHeaderIfNonEmpty(websockets::kSecWebSocketProtocol, |
| 263 | requested_sub_protocols_, |
| 264 | &enriched_headers); |
[email protected] | d51365e | 2013-11-27 10:46:52 | [diff] [blame] | 265 | |
ricea | 11bdcd0 | 2014-11-20 09:57:07 | [diff] [blame] | 266 | handshake_challenge_response_ = |
| 267 | ComputeSecWebSocketAccept(handshake_challenge); |
[email protected] | d51365e | 2013-11-27 10:46:52 | [diff] [blame] | 268 | |
[email protected] | cd48ed1 | 2014-01-22 14:34:22 | [diff] [blame] | 269 | DCHECK(connect_delegate_); |
Bence Béky | 6562397 | 2018-03-05 15:31:56 | [diff] [blame] | 270 | auto request = |
| 271 | std::make_unique<WebSocketHandshakeRequestInfo>(url_, base::Time::Now()); |
[email protected] | cd48ed1 | 2014-01-22 14:34:22 | [diff] [blame] | 272 | request->headers.CopyFrom(enriched_headers); |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 273 | connect_delegate_->OnStartOpeningHandshake(std::move(request)); |
[email protected] | cd48ed1 | 2014-01-22 14:34:22 | [diff] [blame] | 274 | |
Ramin Halavati | 20e949f | 2018-02-14 20:14:32 | [diff] [blame] | 275 | return parser()->SendRequest( |
| 276 | state_.GenerateRequestLine(), enriched_headers, |
| 277 | NetworkTrafficAnnotationTag(state_.traffic_annotation()), response, |
| 278 | std::move(callback)); |
[email protected] | d51365e | 2013-11-27 10:46:52 | [diff] [blame] | 279 | } |
| 280 | |
| 281 | int WebSocketBasicHandshakeStream::ReadResponseHeaders( |
Bence Béky | a25e3f7 | 2018-02-13 21:13:39 | [diff] [blame] | 282 | CompletionOnceCallback callback) { |
[email protected] | d51365e | 2013-11-27 10:46:52 | [diff] [blame] | 283 | // HttpStreamParser uses a weak pointer when reading from the |
| 284 | // socket, so it won't be called back after being destroyed. The |
| 285 | // HttpStreamParser is owned by HttpBasicState which is owned by this object, |
| 286 | // so this use of base::Unretained() is safe. |
Bence Béky | a25e3f7 | 2018-02-13 21:13:39 | [diff] [blame] | 287 | int rv = parser()->ReadResponseHeaders(base::BindOnce( |
| 288 | &WebSocketBasicHandshakeStream::ReadResponseHeadersCallback, |
| 289 | base::Unretained(this), std::move(callback))); |
[email protected] | cd48ed1 | 2014-01-22 14:34:22 | [diff] [blame] | 290 | if (rv == ERR_IO_PENDING) |
| 291 | return rv; |
ricea | 24c195f | 2015-02-26 12:18:55 | [diff] [blame] | 292 | return ValidateResponse(rv); |
[email protected] | d51365e | 2013-11-27 10:46:52 | [diff] [blame] | 293 | } |
| 294 | |
[email protected] | d51365e | 2013-11-27 10:46:52 | [diff] [blame] | 295 | int WebSocketBasicHandshakeStream::ReadResponseBody( |
| 296 | IOBuffer* buf, |
| 297 | int buf_len, |
Bence Béky | a25e3f7 | 2018-02-13 21:13:39 | [diff] [blame] | 298 | CompletionOnceCallback callback) { |
| 299 | return parser()->ReadResponseBody(buf, buf_len, std::move(callback)); |
[email protected] | d51365e | 2013-11-27 10:46:52 | [diff] [blame] | 300 | } |
| 301 | |
| 302 | void WebSocketBasicHandshakeStream::Close(bool not_reusable) { |
| 303 | // This class ignores the value of |not_reusable| and never lets the socket be |
| 304 | // re-used. |
Matt Menke | f171ff8b | 2019-02-05 20:06:13 | [diff] [blame] | 305 | if (!parser()) |
| 306 | return; |
| 307 | StreamSocket* socket = state_.connection()->socket(); |
| 308 | if (socket) |
| 309 | socket->Disconnect(); |
| 310 | state_.connection()->Reset(); |
[email protected] | d51365e | 2013-11-27 10:46:52 | [diff] [blame] | 311 | } |
| 312 | |
| 313 | bool WebSocketBasicHandshakeStream::IsResponseBodyComplete() const { |
| 314 | return parser()->IsResponseBodyComplete(); |
| 315 | } |
| 316 | |
[email protected] | d51365e | 2013-11-27 10:46:52 | [diff] [blame] | 317 | bool WebSocketBasicHandshakeStream::IsConnectionReused() const { |
Matt Menke | f171ff8b | 2019-02-05 20:06:13 | [diff] [blame] | 318 | return state_.IsConnectionReused(); |
[email protected] | d51365e | 2013-11-27 10:46:52 | [diff] [blame] | 319 | } |
| 320 | |
| 321 | void WebSocketBasicHandshakeStream::SetConnectionReused() { |
Matt Menke | f171ff8b | 2019-02-05 20:06:13 | [diff] [blame] | 322 | state_.connection()->set_reuse_type(ClientSocketHandle::REUSED_IDLE); |
[email protected] | d51365e | 2013-11-27 10:46:52 | [diff] [blame] | 323 | } |
| 324 | |
mmenke | bd84c39 | 2015-09-02 14:12:34 | [diff] [blame] | 325 | bool WebSocketBasicHandshakeStream::CanReuseConnection() const { |
Matt Menke | f171ff8b | 2019-02-05 20:06:13 | [diff] [blame] | 326 | return parser() && state_.connection()->socket() && |
| 327 | parser()->CanReuseConnection(); |
[email protected] | d51365e | 2013-11-27 10:46:52 | [diff] [blame] | 328 | } |
| 329 | |
sclittle | 4de1bab9 | 2015-09-22 21:28:24 | [diff] [blame] | 330 | int64_t WebSocketBasicHandshakeStream::GetTotalReceivedBytes() const { |
[email protected] | bc92bc97 | 2013-12-13 08:32:59 | [diff] [blame] | 331 | return 0; |
| 332 | } |
| 333 | |
sclittle | be1ccf6 | 2015-09-02 19:40:36 | [diff] [blame] | 334 | int64_t WebSocketBasicHandshakeStream::GetTotalSentBytes() const { |
| 335 | return 0; |
| 336 | } |
| 337 | |
rch | cd37901 | 2017-04-12 21:53:32 | [diff] [blame] | 338 | bool WebSocketBasicHandshakeStream::GetAlternativeService( |
| 339 | AlternativeService* alternative_service) const { |
| 340 | return false; |
| 341 | } |
| 342 | |
[email protected] | d51365e | 2013-11-27 10:46:52 | [diff] [blame] | 343 | bool WebSocketBasicHandshakeStream::GetLoadTimingInfo( |
| 344 | LoadTimingInfo* load_timing_info) const { |
| 345 | return state_.connection()->GetLoadTimingInfo(IsConnectionReused(), |
| 346 | load_timing_info); |
| 347 | } |
| 348 | |
| 349 | void WebSocketBasicHandshakeStream::GetSSLInfo(SSLInfo* ssl_info) { |
Matt Menke | f171ff8b | 2019-02-05 20:06:13 | [diff] [blame] | 350 | if (!state_.connection()->socket()) { |
| 351 | ssl_info->Reset(); |
| 352 | return; |
| 353 | } |
[email protected] | d51365e | 2013-11-27 10:46:52 | [diff] [blame] | 354 | parser()->GetSSLInfo(ssl_info); |
| 355 | } |
| 356 | |
| 357 | void WebSocketBasicHandshakeStream::GetSSLCertRequestInfo( |
| 358 | SSLCertRequestInfo* cert_request_info) { |
Matt Menke | f171ff8b | 2019-02-05 20:06:13 | [diff] [blame] | 359 | if (!state_.connection()->socket()) { |
| 360 | cert_request_info->Reset(); |
| 361 | return; |
| 362 | } |
[email protected] | d51365e | 2013-11-27 10:46:52 | [diff] [blame] | 363 | parser()->GetSSLCertRequestInfo(cert_request_info); |
| 364 | } |
| 365 | |
Titouan Rigoudy | 5cf08f1 | 2022-05-20 15:49:22 | [diff] [blame] | 366 | int WebSocketBasicHandshakeStream::GetRemoteEndpoint(IPEndPoint* endpoint) { |
ttuttle | d9dbc65 | 2015-09-29 20:00:59 | [diff] [blame] | 367 | if (!state_.connection() || !state_.connection()->socket()) |
Titouan Rigoudy | 5cf08f1 | 2022-05-20 15:49:22 | [diff] [blame] | 368 | return ERR_SOCKET_NOT_CONNECTED; |
ttuttle | d9dbc65 | 2015-09-29 20:00:59 | [diff] [blame] | 369 | |
Titouan Rigoudy | 5cf08f1 | 2022-05-20 15:49:22 | [diff] [blame] | 370 | return state_.connection()->socket()->GetPeerAddress(endpoint); |
ttuttle | d9dbc65 | 2015-09-29 20:00:59 | [diff] [blame] | 371 | } |
| 372 | |
zhongyi | ca364fbb | 2015-12-12 03:39:12 | [diff] [blame] | 373 | void WebSocketBasicHandshakeStream::PopulateNetErrorDetails( |
| 374 | NetErrorDetails* /*details*/) { |
| 375 | return; |
| 376 | } |
| 377 | |
[email protected] | d51365e | 2013-11-27 10:46:52 | [diff] [blame] | 378 | void WebSocketBasicHandshakeStream::Drain(HttpNetworkSession* session) { |
Tsuyoshi Horo | b074f876 | 2022-07-22 23:25:14 | [diff] [blame] | 379 | session->StartResponseDrainer( |
| 380 | std::make_unique<HttpResponseBodyDrainer>(this)); |
[email protected] | d51365e | 2013-11-27 10:46:52 | [diff] [blame] | 381 | // |drainer| will delete itself. |
| 382 | } |
| 383 | |
| 384 | void WebSocketBasicHandshakeStream::SetPriority(RequestPriority priority) { |
| 385 | // TODO(ricea): See TODO comment in HttpBasicStream::SetPriority(). If it is |
| 386 | // gone, then copy whatever has happened there over here. |
| 387 | } |
| 388 | |
Tsuyoshi Horo | 34e23cc | 2022-07-11 02:04:05 | [diff] [blame] | 389 | std::unique_ptr<HttpStream> |
| 390 | WebSocketBasicHandshakeStream::RenewStreamForAuth() { |
Adam Rice | 8b382c60 | 2018-06-04 12:36:39 | [diff] [blame] | 391 | DCHECK(IsResponseBodyComplete()); |
| 392 | DCHECK(!parser()->IsMoreDataBuffered()); |
| 393 | // The HttpStreamParser object still has a pointer to the connection. Just to |
| 394 | // be extra-sure it doesn't touch the connection again, delete it here rather |
| 395 | // than leaving it until the destructor is called. |
| 396 | state_.DeleteParser(); |
| 397 | |
| 398 | auto handshake_stream = std::make_unique<WebSocketBasicHandshakeStream>( |
| 399 | state_.ReleaseConnection(), connect_delegate_, state_.using_proxy(), |
| 400 | std::move(requested_sub_protocols_), std::move(requested_extensions_), |
| 401 | stream_request_, websocket_endpoint_lock_manager_); |
| 402 | |
| 403 | stream_request_->OnBasicHandshakeStreamCreated(handshake_stream.get()); |
| 404 | |
Tsuyoshi Horo | 34e23cc | 2022-07-11 02:04:05 | [diff] [blame] | 405 | return handshake_stream; |
yhirano | a7e05bb | 2014-11-06 05:40:39 | [diff] [blame] | 406 | } |
| 407 | |
Eric Orth | ac66191 | 2022-01-10 21:44:17 | [diff] [blame] | 408 | const std::set<std::string>& WebSocketBasicHandshakeStream::GetDnsAliases() |
Cammie Smith Barnes | 4a5d72f | 2020-12-17 21:47:04 | [diff] [blame] | 409 | const { |
| 410 | return state_.GetDnsAliases(); |
| 411 | } |
| 412 | |
Bence Béky | 334ddfc2 | 2021-03-12 15:18:13 | [diff] [blame] | 413 | base::StringPiece WebSocketBasicHandshakeStream::GetAcceptChViaAlps() const { |
| 414 | return {}; |
| 415 | } |
| 416 | |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 417 | std::unique_ptr<WebSocketStream> WebSocketBasicHandshakeStream::Upgrade() { |
[email protected] | d51365e | 2013-11-27 10:46:52 | [diff] [blame] | 418 | // The HttpStreamParser object has a pointer to our ClientSocketHandle. Make |
| 419 | // sure it does not touch it again before it is destroyed. |
| 420 | state_.DeleteParser(); |
Bence Béky | da280c6 | 2018-04-12 15:08:37 | [diff] [blame] | 421 | WebSocketTransportClientSocketPool::UnlockEndpoint( |
| 422 | state_.connection(), websocket_endpoint_lock_manager_); |
Bence Béky | 7294fc2 | 2018-02-08 14:26:17 | [diff] [blame] | 423 | std::unique_ptr<WebSocketStream> basic_stream = |
| 424 | std::make_unique<WebSocketBasicStream>( |
| 425 | std::make_unique<WebSocketClientSocketHandleAdapter>( |
| 426 | state_.ReleaseConnection()), |
Nanami Mikiya | d6b837e | 2022-02-01 05:58:57 | [diff] [blame] | 427 | state_.read_buf(), sub_protocol_, extensions_, net_log_); |
[email protected] | 0be9392 | 2014-01-29 00:42:45 | [diff] [blame] | 428 | DCHECK(extension_params_.get()); |
| 429 | if (extension_params_->deflate_enabled) { |
Bence Béky | 6562397 | 2018-03-05 15:31:56 | [diff] [blame] | 430 | return std::make_unique<WebSocketDeflateStream>( |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 431 | std::move(basic_stream), extension_params_->deflate_parameters, |
Bence Béky | 6562397 | 2018-03-05 15:31:56 | [diff] [blame] | 432 | std::make_unique<WebSocketDeflatePredictorImpl>()); |
[email protected] | 0be9392 | 2014-01-29 00:42:45 | [diff] [blame] | 433 | } |
Adam Rice | b023728 | 2019-06-20 14:14:02 | [diff] [blame] | 434 | |
| 435 | return basic_stream; |
[email protected] | d51365e | 2013-11-27 10:46:52 | [diff] [blame] | 436 | } |
| 437 | |
Bence Béky | ca0da43 | 2019-01-24 15:03:20 | [diff] [blame] | 438 | base::WeakPtr<WebSocketHandshakeStreamBase> |
| 439 | WebSocketBasicHandshakeStream::GetWeakPtr() { |
| 440 | return weak_ptr_factory_.GetWeakPtr(); |
| 441 | } |
| 442 | |
[email protected] | a31ecc0 | 2013-12-05 08:30:55 | [diff] [blame] | 443 | void WebSocketBasicHandshakeStream::SetWebSocketKeyForTesting( |
| 444 | const std::string& key) { |
Bence Béky | 7d0c74d | 2018-03-05 08:31:09 | [diff] [blame] | 445 | handshake_challenge_for_testing_ = key; |
[email protected] | a31ecc0 | 2013-12-05 08:30:55 | [diff] [blame] | 446 | } |
| 447 | |
[email protected] | d51365e | 2013-11-27 10:46:52 | [diff] [blame] | 448 | void WebSocketBasicHandshakeStream::ReadResponseHeadersCallback( |
Bence Béky | a25e3f7 | 2018-02-13 21:13:39 | [diff] [blame] | 449 | CompletionOnceCallback callback, |
[email protected] | d51365e | 2013-11-27 10:46:52 | [diff] [blame] | 450 | int result) { |
Bence Béky | a25e3f7 | 2018-02-13 21:13:39 | [diff] [blame] | 451 | std::move(callback).Run(ValidateResponse(result)); |
[email protected] | d51365e | 2013-11-27 10:46:52 | [diff] [blame] | 452 | } |
| 453 | |
ricea | 24c195f | 2015-02-26 12:18:55 | [diff] [blame] | 454 | int WebSocketBasicHandshakeStream::ValidateResponse(int rv) { |
[email protected] | d51365e | 2013-11-27 10:46:52 | [diff] [blame] | 455 | DCHECK(http_response_info_); |
[email protected] | f7e98ca | 2014-06-19 12:05:43 | [diff] [blame] | 456 | // Most net errors happen during connection, so they are not seen by this |
| 457 | // method. The histogram for error codes is created in |
| 458 | // Delegate::OnResponseStarted in websocket_stream.cc instead. |
[email protected] | e5760f52 | 2014-02-05 12:28:50 | [diff] [blame] | 459 | if (rv >= 0) { |
[email protected] | f7e98ca | 2014-06-19 12:05:43 | [diff] [blame] | 460 | const HttpResponseHeaders* headers = http_response_info_->headers.get(); |
| 461 | const int response_code = headers->response_code(); |
Ilya Sherman | 0eb3980 | 2017-12-08 20:58:18 | [diff] [blame] | 462 | base::UmaHistogramSparse("Net.WebSocket.ResponseCode", response_code); |
[email protected] | f7e98ca | 2014-06-19 12:05:43 | [diff] [blame] | 463 | switch (response_code) { |
[email protected] | e5760f52 | 2014-02-05 12:28:50 | [diff] [blame] | 464 | case HTTP_SWITCHING_PROTOCOLS: |
[email protected] | e5760f52 | 2014-02-05 12:28:50 | [diff] [blame] | 465 | return ValidateUpgradeResponse(headers); |
[email protected] | d51365e | 2013-11-27 10:46:52 | [diff] [blame] | 466 | |
[email protected] | e5760f52 | 2014-02-05 12:28:50 | [diff] [blame] | 467 | // We need to pass these through for authentication to work. |
| 468 | case HTTP_UNAUTHORIZED: |
| 469 | case HTTP_PROXY_AUTHENTICATION_REQUIRED: |
| 470 | return OK; |
[email protected] | d51365e | 2013-11-27 10:46:52 | [diff] [blame] | 471 | |
[email protected] | e5760f52 | 2014-02-05 12:28:50 | [diff] [blame] | 472 | // Other status codes are potentially risky (see the warnings in the |
| 473 | // WHATWG WebSocket API spec) and so are dropped by default. |
| 474 | default: |
[email protected] | aeb640d | 2014-02-21 11:03:18 | [diff] [blame] | 475 | // A WebSocket server cannot be using HTTP/0.9, so if we see version |
| 476 | // 0.9, it means the response was garbage. |
| 477 | // Reporting "Unexpected response code: 200" in this case is not |
| 478 | // helpful, so use a different error message. |
| 479 | if (headers->GetHttpVersion() == HttpVersion(0, 9)) { |
Adam Langley | a48b636a | 2020-11-12 23:42:52 | [diff] [blame] | 480 | OnFailure("Error during WebSocket handshake: Invalid status line", |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 481 | ERR_FAILED, absl::nullopt); |
[email protected] | aeb640d | 2014-02-21 11:03:18 | [diff] [blame] | 482 | } else { |
Adam Langley | a48b636a | 2020-11-12 23:42:52 | [diff] [blame] | 483 | OnFailure(base::StringPrintf("Error during WebSocket handshake: " |
| 484 | "Unexpected response code: %d", |
| 485 | headers->response_code()), |
| 486 | ERR_FAILED, headers->response_code()); |
[email protected] | aeb640d | 2014-02-21 11:03:18 | [diff] [blame] | 487 | } |
Bence Béky | de0be31 | 2018-03-13 17:51:58 | [diff] [blame] | 488 | result_ = HandshakeResult::INVALID_STATUS; |
[email protected] | e5760f52 | 2014-02-05 12:28:50 | [diff] [blame] | 489 | return ERR_INVALID_RESPONSE; |
| 490 | } |
| 491 | } else { |
[email protected] | 3efc08f | 2014-02-07 09:33:34 | [diff] [blame] | 492 | if (rv == ERR_EMPTY_RESPONSE) { |
Adam Langley | a48b636a | 2020-11-12 23:42:52 | [diff] [blame] | 493 | OnFailure("Connection closed before receiving a handshake response", rv, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 494 | absl::nullopt); |
Bence Béky | de0be31 | 2018-03-13 17:51:58 | [diff] [blame] | 495 | result_ = HandshakeResult::EMPTY_RESPONSE; |
[email protected] | 3efc08f | 2014-02-07 09:33:34 | [diff] [blame] | 496 | return rv; |
| 497 | } |
Adam Langley | a48b636a | 2020-11-12 23:42:52 | [diff] [blame] | 498 | OnFailure( |
| 499 | std::string("Error during WebSocket handshake: ") + ErrorToString(rv), |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 500 | rv, absl::nullopt); |
ricea | 23c3f94 | 2015-02-02 13:35:13 | [diff] [blame] | 501 | // Some error codes (for example ERR_CONNECTION_CLOSED) get changed to OK at |
| 502 | // higher levels. To prevent an unvalidated connection getting erroneously |
| 503 | // upgraded, don't pass through the status code unchanged if it is |
| 504 | // HTTP_SWITCHING_PROTOCOLS. |
| 505 | if (http_response_info_->headers && |
| 506 | http_response_info_->headers->response_code() == |
| 507 | HTTP_SWITCHING_PROTOCOLS) { |
| 508 | http_response_info_->headers->ReplaceStatusLine( |
| 509 | kConnectionErrorStatusLine); |
Bence Béky | de0be31 | 2018-03-13 17:51:58 | [diff] [blame] | 510 | result_ = HandshakeResult::FAILED_SWITCHING_PROTOCOLS; |
| 511 | return rv; |
ricea | 23c3f94 | 2015-02-02 13:35:13 | [diff] [blame] | 512 | } |
Bence Béky | de0be31 | 2018-03-13 17:51:58 | [diff] [blame] | 513 | result_ = HandshakeResult::FAILED; |
[email protected] | e5760f52 | 2014-02-05 12:28:50 | [diff] [blame] | 514 | return rv; |
[email protected] | d51365e | 2013-11-27 10:46:52 | [diff] [blame] | 515 | } |
| 516 | } |
| 517 | |
| 518 | int WebSocketBasicHandshakeStream::ValidateUpgradeResponse( |
[email protected] | e5760f52 | 2014-02-05 12:28:50 | [diff] [blame] | 519 | const HttpResponseHeaders* headers) { |
Bence Béky | 6562397 | 2018-03-05 15:31:56 | [diff] [blame] | 520 | extension_params_ = std::make_unique<WebSocketExtensionParams>(); |
[email protected] | 8aba017 | 2014-07-03 12:09:53 | [diff] [blame] | 521 | std::string failure_message; |
Bence Béky | de0be31 | 2018-03-13 17:51:58 | [diff] [blame] | 522 | if (!ValidateUpgrade(headers, &failure_message)) { |
| 523 | result_ = HandshakeResult::FAILED_UPGRADE; |
| 524 | } else if (!ValidateSecWebSocketAccept(headers, handshake_challenge_response_, |
| 525 | &failure_message)) { |
| 526 | result_ = HandshakeResult::FAILED_ACCEPT; |
| 527 | } else if (!ValidateConnection(headers, &failure_message)) { |
| 528 | result_ = HandshakeResult::FAILED_CONNECTION; |
| 529 | } else if (!ValidateSubProtocol(headers, requested_sub_protocols_, |
| 530 | &sub_protocol_, &failure_message)) { |
| 531 | result_ = HandshakeResult::FAILED_SUBPROTO; |
| 532 | } else if (!ValidateExtensions(headers, &extensions_, &failure_message, |
| 533 | extension_params_.get())) { |
| 534 | result_ = HandshakeResult::FAILED_EXTENSIONS; |
| 535 | } else { |
| 536 | result_ = HandshakeResult::CONNECTED; |
[email protected] | d51365e | 2013-11-27 10:46:52 | [diff] [blame] | 537 | return OK; |
| 538 | } |
Adam Langley | a48b636a | 2020-11-12 23:42:52 | [diff] [blame] | 539 | OnFailure("Error during WebSocket handshake: " + failure_message, ERR_FAILED, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 540 | absl::nullopt); |
[email protected] | d51365e | 2013-11-27 10:46:52 | [diff] [blame] | 541 | return ERR_INVALID_RESPONSE; |
| 542 | } |
| 543 | |
Adam Langley | a48b636a | 2020-11-12 23:42:52 | [diff] [blame] | 544 | void WebSocketBasicHandshakeStream::OnFailure( |
| 545 | const std::string& message, |
| 546 | int net_error, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 547 | absl::optional<int> response_code) { |
Nanami Mikiya | ca3588c | 2022-01-31 03:22:25 | [diff] [blame] | 548 | net_log_.AddEvent(net::NetLogEventType::WEBSOCKET_UPGRADE_FAILURE, |
| 549 | [&] { return NetLogFailureParam(net_error, message); }); |
Adam Rice | d4596a8e | 2018-07-13 08:06:17 | [diff] [blame] | 550 | // Avoid connection reuse if auth did not happen. |
| 551 | state_.connection()->socket()->Disconnect(); |
Adam Langley | a48b636a | 2020-11-12 23:42:52 | [diff] [blame] | 552 | stream_request_->OnFailure(message, net_error, response_code); |
[email protected] | 8aba017 | 2014-07-03 12:09:53 | [diff] [blame] | 553 | } |
| 554 | |
[email protected] | d51365e | 2013-11-27 10:46:52 | [diff] [blame] | 555 | } // namespace net |