[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" |
[email protected] | d51365e | 2013-11-27 10:46:52 | [diff] [blame] | 26 | #include "net/http/http_request_headers.h" |
| 27 | #include "net/http/http_request_info.h" |
| 28 | #include "net/http/http_response_body_drainer.h" |
| 29 | #include "net/http/http_response_headers.h" |
| 30 | #include "net/http/http_status_code.h" |
| 31 | #include "net/http/http_stream_parser.h" |
| 32 | #include "net/socket/client_socket_handle.h" |
Bence Béky | 3cb271d | 2018-03-29 22:00:48 | [diff] [blame] | 33 | #include "net/socket/ssl_client_socket.h" |
Bence Béky | da280c6 | 2018-04-12 15:08:37 | [diff] [blame] | 34 | #include "net/socket/websocket_endpoint_lock_manager.h" |
[email protected] | 65486614 | 2014-06-24 22:53:31 | [diff] [blame] | 35 | #include "net/socket/websocket_transport_client_socket_pool.h" |
[email protected] | d51365e | 2013-11-27 10:46:52 | [diff] [blame] | 36 | #include "net/websockets/websocket_basic_stream.h" |
Bence Béky | 7294fc2 | 2018-02-08 14:26:17 | [diff] [blame] | 37 | #include "net/websockets/websocket_basic_stream_adapters.h" |
yhirano | 8387aee | 2015-09-14 05:46:49 | [diff] [blame] | 38 | #include "net/websockets/websocket_deflate_parameters.h" |
[email protected] | 0be9392 | 2014-01-29 00:42:45 | [diff] [blame] | 39 | #include "net/websockets/websocket_deflate_predictor.h" |
| 40 | #include "net/websockets/websocket_deflate_predictor_impl.h" |
| 41 | #include "net/websockets/websocket_deflate_stream.h" |
| 42 | #include "net/websockets/websocket_deflater.h" |
ricea | 11bdcd0 | 2014-11-20 09:57:07 | [diff] [blame] | 43 | #include "net/websockets/websocket_handshake_challenge.h" |
[email protected] | d51365e | 2013-11-27 10:46:52 | [diff] [blame] | 44 | #include "net/websockets/websocket_handshake_constants.h" |
[email protected] | cd48ed1 | 2014-01-22 14:34:22 | [diff] [blame] | 45 | #include "net/websockets/websocket_handshake_request_info.h" |
| 46 | #include "net/websockets/websocket_handshake_response_info.h" |
[email protected] | d51365e | 2013-11-27 10:46:52 | [diff] [blame] | 47 | #include "net/websockets/websocket_stream.h" |
| 48 | |
| 49 | namespace net { |
[email protected] | 0be9392 | 2014-01-29 00:42:45 | [diff] [blame] | 50 | |
yhirano | 27b2b57 | 2014-10-30 11:23:44 | [diff] [blame] | 51 | namespace { |
| 52 | |
ricea | 23c3f94 | 2015-02-02 13:35:13 | [diff] [blame] | 53 | const char kConnectionErrorStatusLine[] = "HTTP/1.1 503 Connection Error"; |
| 54 | |
yhirano | 27b2b57 | 2014-10-30 11:23:44 | [diff] [blame] | 55 | } // namespace |
| 56 | |
[email protected] | d51365e | 2013-11-27 10:46:52 | [diff] [blame] | 57 | namespace { |
| 58 | |
[email protected] | 9686820 | 2014-01-09 10:38:04 | [diff] [blame] | 59 | enum GetHeaderResult { |
| 60 | GET_HEADER_OK, |
| 61 | GET_HEADER_MISSING, |
| 62 | GET_HEADER_MULTIPLE, |
| 63 | }; |
| 64 | |
| 65 | std::string MissingHeaderMessage(const std::string& header_name) { |
| 66 | return std::string("'") + header_name + "' header is missing"; |
| 67 | } |
| 68 | |
[email protected] | d51365e | 2013-11-27 10:46:52 | [diff] [blame] | 69 | std::string GenerateHandshakeChallenge() { |
| 70 | std::string raw_challenge(websockets::kRawChallengeLength, '\0'); |
Ryan Sleevi | 972b2ff | 2018-05-14 15:45:10 | [diff] [blame] | 71 | crypto::RandBytes(base::data(raw_challenge), raw_challenge.length()); |
[email protected] | d51365e | 2013-11-27 10:46:52 | [diff] [blame] | 72 | std::string encoded_challenge; |
[email protected] | 33fca12 | 2013-12-11 01:48:50 | [diff] [blame] | 73 | base::Base64Encode(raw_challenge, &encoded_challenge); |
[email protected] | d51365e | 2013-11-27 10:46:52 | [diff] [blame] | 74 | return encoded_challenge; |
| 75 | } |
| 76 | |
[email protected] | 9686820 | 2014-01-09 10:38:04 | [diff] [blame] | 77 | GetHeaderResult GetSingleHeaderValue(const HttpResponseHeaders* headers, |
| 78 | const base::StringPiece& name, |
| 79 | std::string* value) { |
olli.raula | ee489a5 | 2016-01-25 08:37:10 | [diff] [blame] | 80 | size_t iter = 0; |
[email protected] | 9686820 | 2014-01-09 10:38:04 | [diff] [blame] | 81 | size_t num_values = 0; |
| 82 | std::string temp_value; |
olli.raula | ee489a5 | 2016-01-25 08:37:10 | [diff] [blame] | 83 | while (headers->EnumerateHeader(&iter, name, &temp_value)) { |
[email protected] | 9686820 | 2014-01-09 10:38:04 | [diff] [blame] | 84 | if (++num_values > 1) |
| 85 | return GET_HEADER_MULTIPLE; |
| 86 | *value = temp_value; |
[email protected] | d51365e | 2013-11-27 10:46:52 | [diff] [blame] | 87 | } |
[email protected] | 9686820 | 2014-01-09 10:38:04 | [diff] [blame] | 88 | return num_values > 0 ? GET_HEADER_OK : GET_HEADER_MISSING; |
| 89 | } |
| 90 | |
| 91 | bool ValidateHeaderHasSingleValue(GetHeaderResult result, |
| 92 | const std::string& header_name, |
| 93 | std::string* failure_message) { |
| 94 | if (result == GET_HEADER_MISSING) { |
| 95 | *failure_message = MissingHeaderMessage(header_name); |
| 96 | return false; |
| 97 | } |
| 98 | if (result == GET_HEADER_MULTIPLE) { |
Bence Béky | b28709c2 | 2018-03-06 13:03:44 | [diff] [blame] | 99 | *failure_message = |
| 100 | WebSocketHandshakeStreamBase::MultipleHeaderValuesMessage(header_name); |
[email protected] | 9686820 | 2014-01-09 10:38:04 | [diff] [blame] | 101 | return false; |
| 102 | } |
| 103 | DCHECK_EQ(result, GET_HEADER_OK); |
| 104 | return true; |
| 105 | } |
| 106 | |
| 107 | bool ValidateUpgrade(const HttpResponseHeaders* headers, |
| 108 | std::string* failure_message) { |
| 109 | std::string value; |
| 110 | GetHeaderResult result = |
| 111 | GetSingleHeaderValue(headers, websockets::kUpgrade, &value); |
| 112 | if (!ValidateHeaderHasSingleValue(result, |
| 113 | websockets::kUpgrade, |
| 114 | failure_message)) { |
| 115 | return false; |
| 116 | } |
| 117 | |
brettw | bc17d2c8 | 2015-06-09 22:39:08 | [diff] [blame] | 118 | if (!base::LowerCaseEqualsASCII(value, websockets::kWebSocketLowercase)) { |
[email protected] | 9686820 | 2014-01-09 10:38:04 | [diff] [blame] | 119 | *failure_message = |
| 120 | "'Upgrade' header value is not 'WebSocket': " + value; |
| 121 | return false; |
| 122 | } |
| 123 | return true; |
| 124 | } |
| 125 | |
| 126 | bool ValidateSecWebSocketAccept(const HttpResponseHeaders* headers, |
| 127 | const std::string& expected, |
| 128 | std::string* failure_message) { |
| 129 | std::string actual; |
| 130 | GetHeaderResult result = |
| 131 | GetSingleHeaderValue(headers, websockets::kSecWebSocketAccept, &actual); |
| 132 | if (!ValidateHeaderHasSingleValue(result, |
| 133 | websockets::kSecWebSocketAccept, |
| 134 | failure_message)) { |
| 135 | return false; |
| 136 | } |
| 137 | |
| 138 | if (expected != actual) { |
| 139 | *failure_message = "Incorrect 'Sec-WebSocket-Accept' header value"; |
| 140 | return false; |
| 141 | } |
| 142 | return true; |
| 143 | } |
| 144 | |
| 145 | bool ValidateConnection(const HttpResponseHeaders* headers, |
| 146 | std::string* failure_message) { |
| 147 | // Connection header is permitted to contain other tokens. |
| 148 | if (!headers->HasHeader(HttpRequestHeaders::kConnection)) { |
| 149 | *failure_message = MissingHeaderMessage(HttpRequestHeaders::kConnection); |
| 150 | return false; |
| 151 | } |
| 152 | if (!headers->HasHeaderValue(HttpRequestHeaders::kConnection, |
| 153 | websockets::kUpgrade)) { |
| 154 | *failure_message = "'Connection' header value must contain 'Upgrade'"; |
| 155 | return false; |
| 156 | } |
| 157 | return true; |
[email protected] | d51365e | 2013-11-27 10:46:52 | [diff] [blame] | 158 | } |
| 159 | |
[email protected] | d51365e | 2013-11-27 10:46:52 | [diff] [blame] | 160 | } // namespace |
| 161 | |
Adam Rice | c786ad8a | 2018-05-22 09:49:15 | [diff] [blame] | 162 | const base::Feature |
| 163 | WebSocketBasicHandshakeStream::kWebSocketHandshakeReuseConnection{ |
Adam Rice | 6612adc | 2018-10-16 18:08:47 | [diff] [blame] | 164 | "WebSocketHandshakeReuseConnection", base::FEATURE_ENABLED_BY_DEFAULT}; |
Adam Rice | c786ad8a | 2018-05-22 09:49:15 | [diff] [blame] | 165 | |
[email protected] | d51365e | 2013-11-27 10:46:52 | [diff] [blame] | 166 | WebSocketBasicHandshakeStream::WebSocketBasicHandshakeStream( |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 167 | std::unique_ptr<ClientSocketHandle> connection, |
[email protected] | cd48ed1 | 2014-01-22 14:34:22 | [diff] [blame] | 168 | WebSocketStream::ConnectDelegate* connect_delegate, |
[email protected] | d51365e | 2013-11-27 10:46:52 | [diff] [blame] | 169 | bool using_proxy, |
| 170 | std::vector<std::string> requested_sub_protocols, |
[email protected] | 8aba017 | 2014-07-03 12:09:53 | [diff] [blame] | 171 | std::vector<std::string> requested_extensions, |
Adam Rice | 6f75c0f | 2018-06-04 08:00:05 | [diff] [blame] | 172 | WebSocketStreamRequestAPI* request, |
Bence Béky | da280c6 | 2018-04-12 15:08:37 | [diff] [blame] | 173 | WebSocketEndpointLockManager* websocket_endpoint_lock_manager) |
Bence Béky | de0be31 | 2018-03-13 17:51:58 | [diff] [blame] | 174 | : result_(HandshakeResult::INCOMPLETE), |
| 175 | state_(std::move(connection), |
mmenke | a7da6da | 2016-09-01 21:56:52 | [diff] [blame] | 176 | using_proxy, |
| 177 | false /* http_09_on_non_default_ports_enabled */), |
[email protected] | cd48ed1 | 2014-01-22 14:34:22 | [diff] [blame] | 178 | connect_delegate_(connect_delegate), |
yhirano | a7e05bb | 2014-11-06 05:40:39 | [diff] [blame] | 179 | http_response_info_(nullptr), |
Adam Rice | 8b382c60 | 2018-06-04 12:36:39 | [diff] [blame] | 180 | requested_sub_protocols_(std::move(requested_sub_protocols)), |
| 181 | requested_extensions_(std::move(requested_extensions)), |
Bence Béky | da280c6 | 2018-04-12 15:08:37 | [diff] [blame] | 182 | stream_request_(request), |
| 183 | websocket_endpoint_lock_manager_(websocket_endpoint_lock_manager) { |
[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 | 42834111 | 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; |
Bence Béky | d5d65a91 | 2018-02-11 04:28:00 | [diff] [blame] | 206 | state_.Initialize(request_info, can_send_early, priority, net_log); |
[email protected] | d51365e | 2013-11-27 10:46:52 | [diff] [blame] | 207 | return OK; |
| 208 | } |
| 209 | |
| 210 | int WebSocketBasicHandshakeStream::SendRequest( |
| 211 | const HttpRequestHeaders& headers, |
| 212 | HttpResponseInfo* response, |
Bence Béky | a25e3f7 | 2018-02-13 21:13:39 | [diff] [blame] | 213 | CompletionOnceCallback callback) { |
[email protected] | d51365e | 2013-11-27 10:46:52 | [diff] [blame] | 214 | DCHECK(!headers.HasHeader(websockets::kSecWebSocketKey)); |
| 215 | DCHECK(!headers.HasHeader(websockets::kSecWebSocketProtocol)); |
| 216 | DCHECK(!headers.HasHeader(websockets::kSecWebSocketExtensions)); |
| 217 | DCHECK(headers.HasHeader(HttpRequestHeaders::kOrigin)); |
| 218 | DCHECK(headers.HasHeader(websockets::kUpgrade)); |
| 219 | DCHECK(headers.HasHeader(HttpRequestHeaders::kConnection)); |
| 220 | DCHECK(headers.HasHeader(websockets::kSecWebSocketVersion)); |
| 221 | DCHECK(parser()); |
| 222 | |
| 223 | http_response_info_ = response; |
| 224 | |
| 225 | // Create a copy of the headers object, so that we can add the |
| 226 | // Sec-WebSockey-Key header. |
| 227 | HttpRequestHeaders enriched_headers; |
| 228 | enriched_headers.CopyFrom(headers); |
[email protected] | a31ecc0 | 2013-12-05 08:30:55 | [diff] [blame] | 229 | std::string handshake_challenge; |
Bence Béky | 7d0c74d | 2018-03-05 08:31:09 | [diff] [blame] | 230 | if (handshake_challenge_for_testing_.has_value()) { |
| 231 | handshake_challenge = handshake_challenge_for_testing_.value(); |
[email protected] | a31ecc0 | 2013-12-05 08:30:55 | [diff] [blame] | 232 | handshake_challenge_for_testing_.reset(); |
| 233 | } else { |
| 234 | handshake_challenge = GenerateHandshakeChallenge(); |
| 235 | } |
[email protected] | d51365e | 2013-11-27 10:46:52 | [diff] [blame] | 236 | enriched_headers.SetHeader(websockets::kSecWebSocketKey, handshake_challenge); |
| 237 | |
[email protected] | d51365e | 2013-11-27 10:46:52 | [diff] [blame] | 238 | AddVectorHeaderIfNonEmpty(websockets::kSecWebSocketExtensions, |
| 239 | requested_extensions_, |
| 240 | &enriched_headers); |
[email protected] | 0be9392 | 2014-01-29 00:42:45 | [diff] [blame] | 241 | AddVectorHeaderIfNonEmpty(websockets::kSecWebSocketProtocol, |
| 242 | requested_sub_protocols_, |
| 243 | &enriched_headers); |
[email protected] | d51365e | 2013-11-27 10:46:52 | [diff] [blame] | 244 | |
ricea | 11bdcd0 | 2014-11-20 09:57:07 | [diff] [blame] | 245 | handshake_challenge_response_ = |
| 246 | ComputeSecWebSocketAccept(handshake_challenge); |
[email protected] | d51365e | 2013-11-27 10:46:52 | [diff] [blame] | 247 | |
[email protected] | cd48ed1 | 2014-01-22 14:34:22 | [diff] [blame] | 248 | DCHECK(connect_delegate_); |
Bence Béky | 6562397 | 2018-03-05 15:31:56 | [diff] [blame] | 249 | auto request = |
| 250 | std::make_unique<WebSocketHandshakeRequestInfo>(url_, base::Time::Now()); |
[email protected] | cd48ed1 | 2014-01-22 14:34:22 | [diff] [blame] | 251 | request->headers.CopyFrom(enriched_headers); |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 252 | connect_delegate_->OnStartOpeningHandshake(std::move(request)); |
[email protected] | cd48ed1 | 2014-01-22 14:34:22 | [diff] [blame] | 253 | |
Ramin Halavati | 20e949f | 2018-02-14 20:14:32 | [diff] [blame] | 254 | return parser()->SendRequest( |
| 255 | state_.GenerateRequestLine(), enriched_headers, |
| 256 | NetworkTrafficAnnotationTag(state_.traffic_annotation()), response, |
| 257 | std::move(callback)); |
[email protected] | d51365e | 2013-11-27 10:46:52 | [diff] [blame] | 258 | } |
| 259 | |
| 260 | int WebSocketBasicHandshakeStream::ReadResponseHeaders( |
Bence Béky | a25e3f7 | 2018-02-13 21:13:39 | [diff] [blame] | 261 | CompletionOnceCallback callback) { |
[email protected] | d51365e | 2013-11-27 10:46:52 | [diff] [blame] | 262 | // HttpStreamParser uses a weak pointer when reading from the |
| 263 | // socket, so it won't be called back after being destroyed. The |
| 264 | // HttpStreamParser is owned by HttpBasicState which is owned by this object, |
| 265 | // so this use of base::Unretained() is safe. |
Bence Béky | a25e3f7 | 2018-02-13 21:13:39 | [diff] [blame] | 266 | int rv = parser()->ReadResponseHeaders(base::BindOnce( |
| 267 | &WebSocketBasicHandshakeStream::ReadResponseHeadersCallback, |
| 268 | base::Unretained(this), std::move(callback))); |
[email protected] | cd48ed1 | 2014-01-22 14:34:22 | [diff] [blame] | 269 | if (rv == ERR_IO_PENDING) |
| 270 | return rv; |
ricea | 24c195f | 2015-02-26 12:18:55 | [diff] [blame] | 271 | return ValidateResponse(rv); |
[email protected] | d51365e | 2013-11-27 10:46:52 | [diff] [blame] | 272 | } |
| 273 | |
[email protected] | d51365e | 2013-11-27 10:46:52 | [diff] [blame] | 274 | int WebSocketBasicHandshakeStream::ReadResponseBody( |
| 275 | IOBuffer* buf, |
| 276 | int buf_len, |
Bence Béky | a25e3f7 | 2018-02-13 21:13:39 | [diff] [blame] | 277 | CompletionOnceCallback callback) { |
| 278 | return parser()->ReadResponseBody(buf, buf_len, std::move(callback)); |
[email protected] | d51365e | 2013-11-27 10:46:52 | [diff] [blame] | 279 | } |
| 280 | |
| 281 | void WebSocketBasicHandshakeStream::Close(bool not_reusable) { |
| 282 | // This class ignores the value of |not_reusable| and never lets the socket be |
| 283 | // re-used. |
| 284 | if (parser()) |
| 285 | parser()->Close(true); |
| 286 | } |
| 287 | |
| 288 | bool WebSocketBasicHandshakeStream::IsResponseBodyComplete() const { |
| 289 | return parser()->IsResponseBodyComplete(); |
| 290 | } |
| 291 | |
[email protected] | d51365e | 2013-11-27 10:46:52 | [diff] [blame] | 292 | bool WebSocketBasicHandshakeStream::IsConnectionReused() const { |
| 293 | return parser()->IsConnectionReused(); |
| 294 | } |
| 295 | |
| 296 | void WebSocketBasicHandshakeStream::SetConnectionReused() { |
| 297 | parser()->SetConnectionReused(); |
| 298 | } |
| 299 | |
mmenke | bd84c39 | 2015-09-02 14:12:34 | [diff] [blame] | 300 | bool WebSocketBasicHandshakeStream::CanReuseConnection() const { |
Adam Rice | c786ad8a | 2018-05-22 09:49:15 | [diff] [blame] | 301 | if (!base::FeatureList::IsEnabled(kWebSocketHandshakeReuseConnection)) |
| 302 | return false; |
| 303 | |
| 304 | return parser() && parser()->CanReuseConnection(); |
[email protected] | d51365e | 2013-11-27 10:46:52 | [diff] [blame] | 305 | } |
| 306 | |
sclittle | 4de1bab9 | 2015-09-22 21:28:24 | [diff] [blame] | 307 | int64_t WebSocketBasicHandshakeStream::GetTotalReceivedBytes() const { |
[email protected] | bc92bc97 | 2013-12-13 08:32:59 | [diff] [blame] | 308 | return 0; |
| 309 | } |
| 310 | |
sclittle | be1ccf6 | 2015-09-02 19:40:36 | [diff] [blame] | 311 | int64_t WebSocketBasicHandshakeStream::GetTotalSentBytes() const { |
| 312 | return 0; |
| 313 | } |
| 314 | |
rch | cd37901 | 2017-04-12 21:53:32 | [diff] [blame] | 315 | bool WebSocketBasicHandshakeStream::GetAlternativeService( |
| 316 | AlternativeService* alternative_service) const { |
| 317 | return false; |
| 318 | } |
| 319 | |
[email protected] | d51365e | 2013-11-27 10:46:52 | [diff] [blame] | 320 | bool WebSocketBasicHandshakeStream::GetLoadTimingInfo( |
| 321 | LoadTimingInfo* load_timing_info) const { |
| 322 | return state_.connection()->GetLoadTimingInfo(IsConnectionReused(), |
| 323 | load_timing_info); |
| 324 | } |
| 325 | |
| 326 | void WebSocketBasicHandshakeStream::GetSSLInfo(SSLInfo* ssl_info) { |
| 327 | parser()->GetSSLInfo(ssl_info); |
| 328 | } |
| 329 | |
| 330 | void WebSocketBasicHandshakeStream::GetSSLCertRequestInfo( |
| 331 | SSLCertRequestInfo* cert_request_info) { |
| 332 | parser()->GetSSLCertRequestInfo(cert_request_info); |
| 333 | } |
| 334 | |
ttuttle | d9dbc65 | 2015-09-29 20:00:59 | [diff] [blame] | 335 | bool WebSocketBasicHandshakeStream::GetRemoteEndpoint(IPEndPoint* endpoint) { |
| 336 | if (!state_.connection() || !state_.connection()->socket()) |
| 337 | return false; |
| 338 | |
| 339 | return state_.connection()->socket()->GetPeerAddress(endpoint) == OK; |
| 340 | } |
| 341 | |
zhongyi | ca364fbb | 2015-12-12 03:39:12 | [diff] [blame] | 342 | void WebSocketBasicHandshakeStream::PopulateNetErrorDetails( |
| 343 | NetErrorDetails* /*details*/) { |
| 344 | return; |
| 345 | } |
| 346 | |
[email protected] | d51365e | 2013-11-27 10:46:52 | [diff] [blame] | 347 | void WebSocketBasicHandshakeStream::Drain(HttpNetworkSession* session) { |
| 348 | HttpResponseBodyDrainer* drainer = new HttpResponseBodyDrainer(this); |
| 349 | drainer->Start(session); |
| 350 | // |drainer| will delete itself. |
| 351 | } |
| 352 | |
| 353 | void WebSocketBasicHandshakeStream::SetPriority(RequestPriority priority) { |
| 354 | // TODO(ricea): See TODO comment in HttpBasicStream::SetPriority(). If it is |
| 355 | // gone, then copy whatever has happened there over here. |
| 356 | } |
| 357 | |
yhirano | a7e05bb | 2014-11-06 05:40:39 | [diff] [blame] | 358 | HttpStream* WebSocketBasicHandshakeStream::RenewStreamForAuth() { |
Adam Rice | 8b382c60 | 2018-06-04 12:36:39 | [diff] [blame] | 359 | if (!base::FeatureList::IsEnabled(kWebSocketHandshakeReuseConnection)) |
| 360 | return nullptr; |
| 361 | |
| 362 | DCHECK(IsResponseBodyComplete()); |
| 363 | DCHECK(!parser()->IsMoreDataBuffered()); |
| 364 | // The HttpStreamParser object still has a pointer to the connection. Just to |
| 365 | // be extra-sure it doesn't touch the connection again, delete it here rather |
| 366 | // than leaving it until the destructor is called. |
| 367 | state_.DeleteParser(); |
| 368 | |
| 369 | auto handshake_stream = std::make_unique<WebSocketBasicHandshakeStream>( |
| 370 | state_.ReleaseConnection(), connect_delegate_, state_.using_proxy(), |
| 371 | std::move(requested_sub_protocols_), std::move(requested_extensions_), |
| 372 | stream_request_, websocket_endpoint_lock_manager_); |
| 373 | |
| 374 | stream_request_->OnBasicHandshakeStreamCreated(handshake_stream.get()); |
| 375 | |
| 376 | return handshake_stream.release(); |
yhirano | a7e05bb | 2014-11-06 05:40:39 | [diff] [blame] | 377 | } |
| 378 | |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 379 | std::unique_ptr<WebSocketStream> WebSocketBasicHandshakeStream::Upgrade() { |
[email protected] | d51365e | 2013-11-27 10:46:52 | [diff] [blame] | 380 | // The HttpStreamParser object has a pointer to our ClientSocketHandle. Make |
| 381 | // sure it does not touch it again before it is destroyed. |
| 382 | state_.DeleteParser(); |
Bence Béky | da280c6 | 2018-04-12 15:08:37 | [diff] [blame] | 383 | WebSocketTransportClientSocketPool::UnlockEndpoint( |
| 384 | state_.connection(), websocket_endpoint_lock_manager_); |
Bence Béky | 7294fc2 | 2018-02-08 14:26:17 | [diff] [blame] | 385 | std::unique_ptr<WebSocketStream> basic_stream = |
| 386 | std::make_unique<WebSocketBasicStream>( |
| 387 | std::make_unique<WebSocketClientSocketHandleAdapter>( |
| 388 | state_.ReleaseConnection()), |
| 389 | state_.read_buf(), sub_protocol_, extensions_); |
[email protected] | 0be9392 | 2014-01-29 00:42:45 | [diff] [blame] | 390 | DCHECK(extension_params_.get()); |
| 391 | if (extension_params_->deflate_enabled) { |
Bence Béky | de0be31 | 2018-03-13 17:51:58 | [diff] [blame] | 392 | RecordDeflateMode( |
| 393 | extension_params_->deflate_parameters.client_context_take_over_mode()); |
[email protected] | 9c50b04 | 2014-04-28 06:40:15 | [diff] [blame] | 394 | |
Bence Béky | 6562397 | 2018-03-05 15:31:56 | [diff] [blame] | 395 | return std::make_unique<WebSocketDeflateStream>( |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 396 | std::move(basic_stream), extension_params_->deflate_parameters, |
Bence Béky | 6562397 | 2018-03-05 15:31:56 | [diff] [blame] | 397 | std::make_unique<WebSocketDeflatePredictorImpl>()); |
[email protected] | 0be9392 | 2014-01-29 00:42:45 | [diff] [blame] | 398 | } else { |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 399 | return basic_stream; |
[email protected] | 0be9392 | 2014-01-29 00:42:45 | [diff] [blame] | 400 | } |
[email protected] | d51365e | 2013-11-27 10:46:52 | [diff] [blame] | 401 | } |
| 402 | |
[email protected] | a31ecc0 | 2013-12-05 08:30:55 | [diff] [blame] | 403 | void WebSocketBasicHandshakeStream::SetWebSocketKeyForTesting( |
| 404 | const std::string& key) { |
Bence Béky | 7d0c74d | 2018-03-05 08:31:09 | [diff] [blame] | 405 | handshake_challenge_for_testing_ = key; |
[email protected] | a31ecc0 | 2013-12-05 08:30:55 | [diff] [blame] | 406 | } |
| 407 | |
[email protected] | d51365e | 2013-11-27 10:46:52 | [diff] [blame] | 408 | void WebSocketBasicHandshakeStream::ReadResponseHeadersCallback( |
Bence Béky | a25e3f7 | 2018-02-13 21:13:39 | [diff] [blame] | 409 | CompletionOnceCallback callback, |
[email protected] | d51365e | 2013-11-27 10:46:52 | [diff] [blame] | 410 | int result) { |
Bence Béky | a25e3f7 | 2018-02-13 21:13:39 | [diff] [blame] | 411 | std::move(callback).Run(ValidateResponse(result)); |
[email protected] | d51365e | 2013-11-27 10:46:52 | [diff] [blame] | 412 | } |
| 413 | |
[email protected] | cd48ed1 | 2014-01-22 14:34:22 | [diff] [blame] | 414 | void WebSocketBasicHandshakeStream::OnFinishOpeningHandshake() { |
[email protected] | cd48ed1 | 2014-01-22 14:34:22 | [diff] [blame] | 415 | DCHECK(http_response_info_); |
Yutaka Hirano | 36c9495 | 2018-05-30 21:33:33 | [diff] [blame] | 416 | WebSocketDispatchOnFinishOpeningHandshake( |
| 417 | connect_delegate_, url_, http_response_info_->headers, |
| 418 | http_response_info_->socket_address, http_response_info_->response_time); |
[email protected] | cd48ed1 | 2014-01-22 14:34:22 | [diff] [blame] | 419 | } |
| 420 | |
ricea | 24c195f | 2015-02-26 12:18:55 | [diff] [blame] | 421 | int WebSocketBasicHandshakeStream::ValidateResponse(int rv) { |
[email protected] | d51365e | 2013-11-27 10:46:52 | [diff] [blame] | 422 | DCHECK(http_response_info_); |
[email protected] | f7e98ca | 2014-06-19 12:05:43 | [diff] [blame] | 423 | // Most net errors happen during connection, so they are not seen by this |
| 424 | // method. The histogram for error codes is created in |
| 425 | // Delegate::OnResponseStarted in websocket_stream.cc instead. |
[email protected] | e5760f52 | 2014-02-05 12:28:50 | [diff] [blame] | 426 | if (rv >= 0) { |
[email protected] | f7e98ca | 2014-06-19 12:05:43 | [diff] [blame] | 427 | const HttpResponseHeaders* headers = http_response_info_->headers.get(); |
| 428 | const int response_code = headers->response_code(); |
Ilya Sherman | 0eb3980 | 2017-12-08 20:58:18 | [diff] [blame] | 429 | base::UmaHistogramSparse("Net.WebSocket.ResponseCode", response_code); |
[email protected] | f7e98ca | 2014-06-19 12:05:43 | [diff] [blame] | 430 | switch (response_code) { |
[email protected] | e5760f52 | 2014-02-05 12:28:50 | [diff] [blame] | 431 | case HTTP_SWITCHING_PROTOCOLS: |
| 432 | OnFinishOpeningHandshake(); |
| 433 | return ValidateUpgradeResponse(headers); |
[email protected] | d51365e | 2013-11-27 10:46:52 | [diff] [blame] | 434 | |
[email protected] | e5760f52 | 2014-02-05 12:28:50 | [diff] [blame] | 435 | // We need to pass these through for authentication to work. |
| 436 | case HTTP_UNAUTHORIZED: |
| 437 | case HTTP_PROXY_AUTHENTICATION_REQUIRED: |
| 438 | return OK; |
[email protected] | d51365e | 2013-11-27 10:46:52 | [diff] [blame] | 439 | |
[email protected] | e5760f52 | 2014-02-05 12:28:50 | [diff] [blame] | 440 | // Other status codes are potentially risky (see the warnings in the |
| 441 | // WHATWG WebSocket API spec) and so are dropped by default. |
| 442 | default: |
[email protected] | aeb640d | 2014-02-21 11:03:18 | [diff] [blame] | 443 | // A WebSocket server cannot be using HTTP/0.9, so if we see version |
| 444 | // 0.9, it means the response was garbage. |
| 445 | // Reporting "Unexpected response code: 200" in this case is not |
| 446 | // helpful, so use a different error message. |
| 447 | if (headers->GetHttpVersion() == HttpVersion(0, 9)) { |
tyoshino | ccfcfde | 2016-07-21 14:06:55 | [diff] [blame] | 448 | OnFailure("Error during WebSocket handshake: Invalid status line"); |
[email protected] | aeb640d | 2014-02-21 11:03:18 | [diff] [blame] | 449 | } else { |
tyoshino | ccfcfde | 2016-07-21 14:06:55 | [diff] [blame] | 450 | OnFailure(base::StringPrintf( |
[email protected] | aeb640d | 2014-02-21 11:03:18 | [diff] [blame] | 451 | "Error during WebSocket handshake: Unexpected response code: %d", |
[email protected] | 8aba017 | 2014-07-03 12:09:53 | [diff] [blame] | 452 | headers->response_code())); |
[email protected] | aeb640d | 2014-02-21 11:03:18 | [diff] [blame] | 453 | } |
[email protected] | e5760f52 | 2014-02-05 12:28:50 | [diff] [blame] | 454 | OnFinishOpeningHandshake(); |
Bence Béky | de0be31 | 2018-03-13 17:51:58 | [diff] [blame] | 455 | result_ = HandshakeResult::INVALID_STATUS; |
[email protected] | e5760f52 | 2014-02-05 12:28:50 | [diff] [blame] | 456 | return ERR_INVALID_RESPONSE; |
| 457 | } |
| 458 | } else { |
[email protected] | 3efc08f | 2014-02-07 09:33:34 | [diff] [blame] | 459 | if (rv == ERR_EMPTY_RESPONSE) { |
tyoshino | ccfcfde | 2016-07-21 14:06:55 | [diff] [blame] | 460 | OnFailure("Connection closed before receiving a handshake response"); |
Bence Béky | de0be31 | 2018-03-13 17:51:58 | [diff] [blame] | 461 | result_ = HandshakeResult::EMPTY_RESPONSE; |
[email protected] | 3efc08f | 2014-02-07 09:33:34 | [diff] [blame] | 462 | return rv; |
| 463 | } |
tyoshino | ccfcfde | 2016-07-21 14:06:55 | [diff] [blame] | 464 | OnFailure(std::string("Error during WebSocket handshake: ") + |
| 465 | ErrorToString(rv)); |
[email protected] | e5760f52 | 2014-02-05 12:28:50 | [diff] [blame] | 466 | OnFinishOpeningHandshake(); |
ricea | 23c3f94 | 2015-02-02 13:35:13 | [diff] [blame] | 467 | // Some error codes (for example ERR_CONNECTION_CLOSED) get changed to OK at |
| 468 | // higher levels. To prevent an unvalidated connection getting erroneously |
| 469 | // upgraded, don't pass through the status code unchanged if it is |
| 470 | // HTTP_SWITCHING_PROTOCOLS. |
| 471 | if (http_response_info_->headers && |
| 472 | http_response_info_->headers->response_code() == |
| 473 | HTTP_SWITCHING_PROTOCOLS) { |
| 474 | http_response_info_->headers->ReplaceStatusLine( |
| 475 | kConnectionErrorStatusLine); |
Bence Béky | de0be31 | 2018-03-13 17:51:58 | [diff] [blame] | 476 | result_ = HandshakeResult::FAILED_SWITCHING_PROTOCOLS; |
| 477 | return rv; |
ricea | 23c3f94 | 2015-02-02 13:35:13 | [diff] [blame] | 478 | } |
Bence Béky | de0be31 | 2018-03-13 17:51:58 | [diff] [blame] | 479 | result_ = HandshakeResult::FAILED; |
[email protected] | e5760f52 | 2014-02-05 12:28:50 | [diff] [blame] | 480 | return rv; |
[email protected] | d51365e | 2013-11-27 10:46:52 | [diff] [blame] | 481 | } |
| 482 | } |
| 483 | |
| 484 | int WebSocketBasicHandshakeStream::ValidateUpgradeResponse( |
[email protected] | e5760f52 | 2014-02-05 12:28:50 | [diff] [blame] | 485 | const HttpResponseHeaders* headers) { |
Bence Béky | 6562397 | 2018-03-05 15:31:56 | [diff] [blame] | 486 | extension_params_ = std::make_unique<WebSocketExtensionParams>(); |
[email protected] | 8aba017 | 2014-07-03 12:09:53 | [diff] [blame] | 487 | std::string failure_message; |
Bence Béky | de0be31 | 2018-03-13 17:51:58 | [diff] [blame] | 488 | if (!ValidateUpgrade(headers, &failure_message)) { |
| 489 | result_ = HandshakeResult::FAILED_UPGRADE; |
| 490 | } else if (!ValidateSecWebSocketAccept(headers, handshake_challenge_response_, |
| 491 | &failure_message)) { |
| 492 | result_ = HandshakeResult::FAILED_ACCEPT; |
| 493 | } else if (!ValidateConnection(headers, &failure_message)) { |
| 494 | result_ = HandshakeResult::FAILED_CONNECTION; |
| 495 | } else if (!ValidateSubProtocol(headers, requested_sub_protocols_, |
| 496 | &sub_protocol_, &failure_message)) { |
| 497 | result_ = HandshakeResult::FAILED_SUBPROTO; |
| 498 | } else if (!ValidateExtensions(headers, &extensions_, &failure_message, |
| 499 | extension_params_.get())) { |
| 500 | result_ = HandshakeResult::FAILED_EXTENSIONS; |
| 501 | } else { |
| 502 | result_ = HandshakeResult::CONNECTED; |
[email protected] | d51365e | 2013-11-27 10:46:52 | [diff] [blame] | 503 | return OK; |
| 504 | } |
tyoshino | ccfcfde | 2016-07-21 14:06:55 | [diff] [blame] | 505 | OnFailure("Error during WebSocket handshake: " + failure_message); |
[email protected] | d51365e | 2013-11-27 10:46:52 | [diff] [blame] | 506 | return ERR_INVALID_RESPONSE; |
| 507 | } |
| 508 | |
tyoshino | ccfcfde | 2016-07-21 14:06:55 | [diff] [blame] | 509 | void WebSocketBasicHandshakeStream::OnFailure(const std::string& message) { |
Adam Rice | d4596a8e | 2018-07-13 08:06:17 | [diff] [blame] | 510 | // Avoid connection reuse if auth did not happen. |
| 511 | state_.connection()->socket()->Disconnect(); |
tyoshino | ccfcfde | 2016-07-21 14:06:55 | [diff] [blame] | 512 | stream_request_->OnFailure(message); |
[email protected] | 8aba017 | 2014-07-03 12:09:53 | [diff] [blame] | 513 | } |
| 514 | |
[email protected] | d51365e | 2013-11-27 10:46:52 | [diff] [blame] | 515 | } // namespace net |