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