[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> |
[email protected] | 9686820 | 2014-01-09 10:38:04 | [diff] [blame] | 11 | #include <string> |
davidben | 1e912ea | 2016-04-20 19:17:07 | [diff] [blame] | 12 | #include <unordered_set> |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 13 | #include <utility> |
[email protected] | cd48ed1 | 2014-01-22 14:34:22 | [diff] [blame] | 14 | #include <vector> |
[email protected] | d51365e | 2013-11-27 10:46:52 | [diff] [blame] | 15 | |
| 16 | #include "base/base64.h" |
[email protected] | d51365e | 2013-11-27 10:46:52 | [diff] [blame] | 17 | #include "base/bind.h" |
yhirano | 27b2b57 | 2014-10-30 11:23:44 | [diff] [blame] | 18 | #include "base/compiler_specific.h" |
[email protected] | 8aba017 | 2014-07-03 12:09:53 | [diff] [blame] | 19 | #include "base/logging.h" |
asvitkine | c3c9372 | 2015-06-17 14:48:37 | [diff] [blame] | 20 | #include "base/metrics/histogram_macros.h" |
[email protected] | f7e98ca | 2014-06-19 12:05:43 | [diff] [blame] | 21 | #include "base/metrics/sparse_histogram.h" |
[email protected] | d51365e | 2013-11-27 10:46:52 | [diff] [blame] | 22 | #include "base/stl_util.h" |
[email protected] | 0be9392 | 2014-01-29 00:42:45 | [diff] [blame] | 23 | #include "base/strings/string_number_conversions.h" |
[email protected] | 69d7a49 | 2014-02-19 08:36:32 | [diff] [blame] | 24 | #include "base/strings/string_piece.h" |
[email protected] | d51365e | 2013-11-27 10:46:52 | [diff] [blame] | 25 | #include "base/strings/string_util.h" |
[email protected] | 9686820 | 2014-01-09 10:38:04 | [diff] [blame] | 26 | #include "base/strings/stringprintf.h" |
[email protected] | cd48ed1 | 2014-01-22 14:34:22 | [diff] [blame] | 27 | #include "base/time/time.h" |
[email protected] | d51365e | 2013-11-27 10:46:52 | [diff] [blame] | 28 | #include "crypto/random.h" |
halton.huo | 299e200 | 2014-12-02 04:39:24 | [diff] [blame] | 29 | #include "net/base/io_buffer.h" |
[email protected] | d51365e | 2013-11-27 10:46:52 | [diff] [blame] | 30 | #include "net/http/http_request_headers.h" |
| 31 | #include "net/http/http_request_info.h" |
| 32 | #include "net/http/http_response_body_drainer.h" |
| 33 | #include "net/http/http_response_headers.h" |
| 34 | #include "net/http/http_status_code.h" |
| 35 | #include "net/http/http_stream_parser.h" |
| 36 | #include "net/socket/client_socket_handle.h" |
[email protected] | 65486614 | 2014-06-24 22:53:31 | [diff] [blame] | 37 | #include "net/socket/websocket_transport_client_socket_pool.h" |
[email protected] | d51365e | 2013-11-27 10:46:52 | [diff] [blame] | 38 | #include "net/websockets/websocket_basic_stream.h" |
yhirano | 8387aee | 2015-09-14 05:46:49 | [diff] [blame] | 39 | #include "net/websockets/websocket_deflate_parameters.h" |
[email protected] | 0be9392 | 2014-01-29 00:42:45 | [diff] [blame] | 40 | #include "net/websockets/websocket_deflate_predictor.h" |
| 41 | #include "net/websockets/websocket_deflate_predictor_impl.h" |
| 42 | #include "net/websockets/websocket_deflate_stream.h" |
| 43 | #include "net/websockets/websocket_deflater.h" |
[email protected] | 9686820 | 2014-01-09 10:38:04 | [diff] [blame] | 44 | #include "net/websockets/websocket_extension_parser.h" |
ricea | 11bdcd0 | 2014-11-20 09:57:07 | [diff] [blame] | 45 | #include "net/websockets/websocket_handshake_challenge.h" |
[email protected] | d51365e | 2013-11-27 10:46:52 | [diff] [blame] | 46 | #include "net/websockets/websocket_handshake_constants.h" |
[email protected] | cd48ed1 | 2014-01-22 14:34:22 | [diff] [blame] | 47 | #include "net/websockets/websocket_handshake_request_info.h" |
| 48 | #include "net/websockets/websocket_handshake_response_info.h" |
[email protected] | d51365e | 2013-11-27 10:46:52 | [diff] [blame] | 49 | #include "net/websockets/websocket_stream.h" |
| 50 | |
| 51 | namespace net { |
[email protected] | 0be9392 | 2014-01-29 00:42:45 | [diff] [blame] | 52 | |
yhirano | 27b2b57 | 2014-10-30 11:23:44 | [diff] [blame] | 53 | namespace { |
| 54 | |
ricea | 23c3f94 | 2015-02-02 13:35:13 | [diff] [blame] | 55 | const char kConnectionErrorStatusLine[] = "HTTP/1.1 503 Connection Error"; |
| 56 | |
yhirano | 27b2b57 | 2014-10-30 11:23:44 | [diff] [blame] | 57 | } // namespace |
| 58 | |
[email protected] | 0be9392 | 2014-01-29 00:42:45 | [diff] [blame] | 59 | // TODO(ricea): If more extensions are added, replace this with a more general |
| 60 | // mechanism. |
| 61 | struct WebSocketExtensionParams { |
yhirano | 8387aee | 2015-09-14 05:46:49 | [diff] [blame] | 62 | bool deflate_enabled = false; |
| 63 | WebSocketDeflateParameters deflate_parameters; |
[email protected] | 0be9392 | 2014-01-29 00:42:45 | [diff] [blame] | 64 | }; |
| 65 | |
[email protected] | d51365e | 2013-11-27 10:46:52 | [diff] [blame] | 66 | namespace { |
| 67 | |
[email protected] | 9686820 | 2014-01-09 10:38:04 | [diff] [blame] | 68 | enum GetHeaderResult { |
| 69 | GET_HEADER_OK, |
| 70 | GET_HEADER_MISSING, |
| 71 | GET_HEADER_MULTIPLE, |
| 72 | }; |
| 73 | |
| 74 | std::string MissingHeaderMessage(const std::string& header_name) { |
| 75 | return std::string("'") + header_name + "' header is missing"; |
| 76 | } |
| 77 | |
| 78 | std::string MultipleHeaderValuesMessage(const std::string& header_name) { |
| 79 | return |
| 80 | std::string("'") + |
| 81 | header_name + |
| 82 | "' header must not appear more than once in a response"; |
| 83 | } |
| 84 | |
[email protected] | d51365e | 2013-11-27 10:46:52 | [diff] [blame] | 85 | std::string GenerateHandshakeChallenge() { |
| 86 | std::string raw_challenge(websockets::kRawChallengeLength, '\0'); |
skyostil | b8f60ca | 2016-08-12 12:34:43 | [diff] [blame] | 87 | crypto::RandBytes(base::string_as_array(&raw_challenge), |
| 88 | raw_challenge.length()); |
[email protected] | d51365e | 2013-11-27 10:46:52 | [diff] [blame] | 89 | std::string encoded_challenge; |
[email protected] | 33fca12 | 2013-12-11 01:48:50 | [diff] [blame] | 90 | base::Base64Encode(raw_challenge, &encoded_challenge); |
[email protected] | d51365e | 2013-11-27 10:46:52 | [diff] [blame] | 91 | return encoded_challenge; |
| 92 | } |
| 93 | |
| 94 | void AddVectorHeaderIfNonEmpty(const char* name, |
| 95 | const std::vector<std::string>& value, |
| 96 | HttpRequestHeaders* headers) { |
| 97 | if (value.empty()) |
| 98 | return; |
brettw | d94a2214 | 2015-07-15 05:19:26 | [diff] [blame] | 99 | headers->SetHeader(name, base::JoinString(value, ", ")); |
[email protected] | d51365e | 2013-11-27 10:46:52 | [diff] [blame] | 100 | } |
| 101 | |
[email protected] | 9686820 | 2014-01-09 10:38:04 | [diff] [blame] | 102 | GetHeaderResult GetSingleHeaderValue(const HttpResponseHeaders* headers, |
| 103 | const base::StringPiece& name, |
| 104 | std::string* value) { |
olli.raula | ee489a5 | 2016-01-25 08:37:10 | [diff] [blame] | 105 | size_t iter = 0; |
[email protected] | 9686820 | 2014-01-09 10:38:04 | [diff] [blame] | 106 | size_t num_values = 0; |
| 107 | std::string temp_value; |
olli.raula | ee489a5 | 2016-01-25 08:37:10 | [diff] [blame] | 108 | while (headers->EnumerateHeader(&iter, name, &temp_value)) { |
[email protected] | 9686820 | 2014-01-09 10:38:04 | [diff] [blame] | 109 | if (++num_values > 1) |
| 110 | return GET_HEADER_MULTIPLE; |
| 111 | *value = temp_value; |
[email protected] | d51365e | 2013-11-27 10:46:52 | [diff] [blame] | 112 | } |
[email protected] | 9686820 | 2014-01-09 10:38:04 | [diff] [blame] | 113 | return num_values > 0 ? GET_HEADER_OK : GET_HEADER_MISSING; |
| 114 | } |
| 115 | |
| 116 | bool ValidateHeaderHasSingleValue(GetHeaderResult result, |
| 117 | const std::string& header_name, |
| 118 | std::string* failure_message) { |
| 119 | if (result == GET_HEADER_MISSING) { |
| 120 | *failure_message = MissingHeaderMessage(header_name); |
| 121 | return false; |
| 122 | } |
| 123 | if (result == GET_HEADER_MULTIPLE) { |
| 124 | *failure_message = MultipleHeaderValuesMessage(header_name); |
| 125 | return false; |
| 126 | } |
| 127 | DCHECK_EQ(result, GET_HEADER_OK); |
| 128 | return true; |
| 129 | } |
| 130 | |
| 131 | bool ValidateUpgrade(const HttpResponseHeaders* headers, |
| 132 | std::string* failure_message) { |
| 133 | std::string value; |
| 134 | GetHeaderResult result = |
| 135 | GetSingleHeaderValue(headers, websockets::kUpgrade, &value); |
| 136 | if (!ValidateHeaderHasSingleValue(result, |
| 137 | websockets::kUpgrade, |
| 138 | failure_message)) { |
| 139 | return false; |
| 140 | } |
| 141 | |
brettw | bc17d2c8 | 2015-06-09 22:39:08 | [diff] [blame] | 142 | if (!base::LowerCaseEqualsASCII(value, websockets::kWebSocketLowercase)) { |
[email protected] | 9686820 | 2014-01-09 10:38:04 | [diff] [blame] | 143 | *failure_message = |
| 144 | "'Upgrade' header value is not 'WebSocket': " + value; |
| 145 | return false; |
| 146 | } |
| 147 | return true; |
| 148 | } |
| 149 | |
| 150 | bool ValidateSecWebSocketAccept(const HttpResponseHeaders* headers, |
| 151 | const std::string& expected, |
| 152 | std::string* failure_message) { |
| 153 | std::string actual; |
| 154 | GetHeaderResult result = |
| 155 | GetSingleHeaderValue(headers, websockets::kSecWebSocketAccept, &actual); |
| 156 | if (!ValidateHeaderHasSingleValue(result, |
| 157 | websockets::kSecWebSocketAccept, |
| 158 | failure_message)) { |
| 159 | return false; |
| 160 | } |
| 161 | |
| 162 | if (expected != actual) { |
| 163 | *failure_message = "Incorrect 'Sec-WebSocket-Accept' header value"; |
| 164 | return false; |
| 165 | } |
| 166 | return true; |
| 167 | } |
| 168 | |
| 169 | bool ValidateConnection(const HttpResponseHeaders* headers, |
| 170 | std::string* failure_message) { |
| 171 | // Connection header is permitted to contain other tokens. |
| 172 | if (!headers->HasHeader(HttpRequestHeaders::kConnection)) { |
| 173 | *failure_message = MissingHeaderMessage(HttpRequestHeaders::kConnection); |
| 174 | return false; |
| 175 | } |
| 176 | if (!headers->HasHeaderValue(HttpRequestHeaders::kConnection, |
| 177 | websockets::kUpgrade)) { |
| 178 | *failure_message = "'Connection' header value must contain 'Upgrade'"; |
| 179 | return false; |
| 180 | } |
| 181 | return true; |
[email protected] | d51365e | 2013-11-27 10:46:52 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | bool ValidateSubProtocol( |
[email protected] | 9686820 | 2014-01-09 10:38:04 | [diff] [blame] | 185 | const HttpResponseHeaders* headers, |
[email protected] | d51365e | 2013-11-27 10:46:52 | [diff] [blame] | 186 | const std::vector<std::string>& requested_sub_protocols, |
[email protected] | 9686820 | 2014-01-09 10:38:04 | [diff] [blame] | 187 | std::string* sub_protocol, |
| 188 | std::string* failure_message) { |
olli.raula | ee489a5 | 2016-01-25 08:37:10 | [diff] [blame] | 189 | size_t iter = 0; |
[email protected] | 9686820 | 2014-01-09 10:38:04 | [diff] [blame] | 190 | std::string value; |
davidben | 1e912ea | 2016-04-20 19:17:07 | [diff] [blame] | 191 | std::unordered_set<std::string> requested_set(requested_sub_protocols.begin(), |
| 192 | requested_sub_protocols.end()); |
[email protected] | 9686820 | 2014-01-09 10:38:04 | [diff] [blame] | 193 | int count = 0; |
| 194 | bool has_multiple_protocols = false; |
| 195 | bool has_invalid_protocol = false; |
[email protected] | d51365e | 2013-11-27 10:46:52 | [diff] [blame] | 196 | |
[email protected] | 9686820 | 2014-01-09 10:38:04 | [diff] [blame] | 197 | while (!has_invalid_protocol || !has_multiple_protocols) { |
| 198 | std::string temp_value; |
olli.raula | ee489a5 | 2016-01-25 08:37:10 | [diff] [blame] | 199 | if (!headers->EnumerateHeader(&iter, websockets::kSecWebSocketProtocol, |
| 200 | &temp_value)) |
[email protected] | 9686820 | 2014-01-09 10:38:04 | [diff] [blame] | 201 | break; |
| 202 | value = temp_value; |
| 203 | if (requested_set.count(value) == 0) |
| 204 | has_invalid_protocol = true; |
| 205 | if (++count > 1) |
| 206 | has_multiple_protocols = true; |
[email protected] | d51365e | 2013-11-27 10:46:52 | [diff] [blame] | 207 | } |
[email protected] | 9686820 | 2014-01-09 10:38:04 | [diff] [blame] | 208 | |
| 209 | if (has_multiple_protocols) { |
| 210 | *failure_message = |
| 211 | MultipleHeaderValuesMessage(websockets::kSecWebSocketProtocol); |
| 212 | return false; |
| 213 | } else if (count > 0 && requested_sub_protocols.size() == 0) { |
| 214 | *failure_message = |
| 215 | std::string("Response must not include 'Sec-WebSocket-Protocol' " |
| 216 | "header if not present in request: ") |
| 217 | + value; |
| 218 | return false; |
| 219 | } else if (has_invalid_protocol) { |
| 220 | *failure_message = |
| 221 | "'Sec-WebSocket-Protocol' header value '" + |
| 222 | value + |
| 223 | "' in response does not match any of sent values"; |
| 224 | return false; |
| 225 | } else if (requested_sub_protocols.size() > 0 && count == 0) { |
| 226 | *failure_message = |
| 227 | "Sent non-empty 'Sec-WebSocket-Protocol' header " |
| 228 | "but no response was received"; |
| 229 | return false; |
| 230 | } |
| 231 | *sub_protocol = value; |
| 232 | return true; |
[email protected] | d51365e | 2013-11-27 10:46:52 | [diff] [blame] | 233 | } |
| 234 | |
[email protected] | 9686820 | 2014-01-09 10:38:04 | [diff] [blame] | 235 | bool ValidateExtensions(const HttpResponseHeaders* headers, |
tyoshino | 38ee68c | 2015-04-01 05:52:52 | [diff] [blame] | 236 | std::string* accepted_extensions_descriptor, |
[email protected] | 0be9392 | 2014-01-29 00:42:45 | [diff] [blame] | 237 | std::string* failure_message, |
| 238 | WebSocketExtensionParams* params) { |
olli.raula | ee489a5 | 2016-01-25 08:37:10 | [diff] [blame] | 239 | size_t iter = 0; |
tyoshino | 38ee68c | 2015-04-01 05:52:52 | [diff] [blame] | 240 | std::string header_value; |
| 241 | std::vector<std::string> header_values; |
[email protected] | 0be9392 | 2014-01-29 00:42:45 | [diff] [blame] | 242 | // TODO(ricea): If adding support for additional extensions, generalise this |
| 243 | // code. |
| 244 | bool seen_permessage_deflate = false; |
olli.raula | ee489a5 | 2016-01-25 08:37:10 | [diff] [blame] | 245 | while (headers->EnumerateHeader(&iter, websockets::kSecWebSocketExtensions, |
tyoshino | 38ee68c | 2015-04-01 05:52:52 | [diff] [blame] | 246 | &header_value)) { |
[email protected] | 9686820 | 2014-01-09 10:38:04 | [diff] [blame] | 247 | WebSocketExtensionParser parser; |
tyoshino | d90d293 | 2015-04-13 16:53:32 | [diff] [blame] | 248 | if (!parser.Parse(header_value)) { |
[email protected] | 9686820 | 2014-01-09 10:38:04 | [diff] [blame] | 249 | // TODO(yhirano) Set appropriate failure message. |
| 250 | *failure_message = |
| 251 | "'Sec-WebSocket-Extensions' header value is " |
| 252 | "rejected by the parser: " + |
tyoshino | 38ee68c | 2015-04-01 05:52:52 | [diff] [blame] | 253 | header_value; |
[email protected] | 9686820 | 2014-01-09 10:38:04 | [diff] [blame] | 254 | return false; |
| 255 | } |
tyoshino | 38ee68c | 2015-04-01 05:52:52 | [diff] [blame] | 256 | |
| 257 | const std::vector<WebSocketExtension>& extensions = parser.extensions(); |
| 258 | for (const auto& extension : extensions) { |
| 259 | if (extension.name() == "permessage-deflate") { |
| 260 | if (seen_permessage_deflate) { |
| 261 | *failure_message = "Received duplicate permessage-deflate response"; |
| 262 | return false; |
| 263 | } |
| 264 | seen_permessage_deflate = true; |
yhirano | 8387aee | 2015-09-14 05:46:49 | [diff] [blame] | 265 | auto& deflate_parameters = params->deflate_parameters; |
| 266 | if (!deflate_parameters.Initialize(extension, failure_message) || |
| 267 | !deflate_parameters.IsValidAsResponse(failure_message)) { |
| 268 | *failure_message = "Error in permessage-deflate: " + *failure_message; |
tyoshino | 38ee68c | 2015-04-01 05:52:52 | [diff] [blame] | 269 | return false; |
| 270 | } |
yhirano | 8387aee | 2015-09-14 05:46:49 | [diff] [blame] | 271 | // Note that we don't have to check the request-response compatibility |
| 272 | // here because we send a request compatible with any valid responses. |
| 273 | // TODO(yhirano): Place a DCHECK here. |
| 274 | |
tyoshino | 38ee68c | 2015-04-01 05:52:52 | [diff] [blame] | 275 | header_values.push_back(header_value); |
| 276 | } else { |
| 277 | *failure_message = "Found an unsupported extension '" + |
| 278 | extension.name() + |
| 279 | "' in 'Sec-WebSocket-Extensions' header"; |
[email protected] | 0be9392 | 2014-01-29 00:42:45 | [diff] [blame] | 280 | return false; |
| 281 | } |
[email protected] | 0be9392 | 2014-01-29 00:42:45 | [diff] [blame] | 282 | } |
[email protected] | d51365e | 2013-11-27 10:46:52 | [diff] [blame] | 283 | } |
brettw | d94a2214 | 2015-07-15 05:19:26 | [diff] [blame] | 284 | *accepted_extensions_descriptor = base::JoinString(header_values, ", "); |
yhirano | 8387aee | 2015-09-14 05:46:49 | [diff] [blame] | 285 | params->deflate_enabled = seen_permessage_deflate; |
[email protected] | d51365e | 2013-11-27 10:46:52 | [diff] [blame] | 286 | return true; |
| 287 | } |
| 288 | |
| 289 | } // namespace |
| 290 | |
| 291 | WebSocketBasicHandshakeStream::WebSocketBasicHandshakeStream( |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 292 | std::unique_ptr<ClientSocketHandle> connection, |
[email protected] | cd48ed1 | 2014-01-22 14:34:22 | [diff] [blame] | 293 | WebSocketStream::ConnectDelegate* connect_delegate, |
[email protected] | d51365e | 2013-11-27 10:46:52 | [diff] [blame] | 294 | bool using_proxy, |
| 295 | std::vector<std::string> requested_sub_protocols, |
[email protected] | 8aba017 | 2014-07-03 12:09:53 | [diff] [blame] | 296 | std::vector<std::string> requested_extensions, |
tyoshino | ccfcfde | 2016-07-21 14:06:55 | [diff] [blame] | 297 | WebSocketStreamRequest* request) |
mmenke | a7da6da | 2016-09-01 21:56:52 | [diff] [blame] | 298 | : state_(std::move(connection), |
| 299 | using_proxy, |
| 300 | false /* http_09_on_non_default_ports_enabled */), |
[email protected] | cd48ed1 | 2014-01-22 14:34:22 | [diff] [blame] | 301 | connect_delegate_(connect_delegate), |
yhirano | a7e05bb | 2014-11-06 05:40:39 | [diff] [blame] | 302 | http_response_info_(nullptr), |
[email protected] | d51365e | 2013-11-27 10:46:52 | [diff] [blame] | 303 | requested_sub_protocols_(requested_sub_protocols), |
[email protected] | 8aba017 | 2014-07-03 12:09:53 | [diff] [blame] | 304 | requested_extensions_(requested_extensions), |
tyoshino | ccfcfde | 2016-07-21 14:06:55 | [diff] [blame] | 305 | stream_request_(request) { |
[email protected] | 8aba017 | 2014-07-03 12:09:53 | [diff] [blame] | 306 | DCHECK(connect_delegate); |
tyoshino | ccfcfde | 2016-07-21 14:06:55 | [diff] [blame] | 307 | DCHECK(request); |
[email protected] | 8aba017 | 2014-07-03 12:09:53 | [diff] [blame] | 308 | } |
[email protected] | d51365e | 2013-11-27 10:46:52 | [diff] [blame] | 309 | |
| 310 | WebSocketBasicHandshakeStream::~WebSocketBasicHandshakeStream() {} |
| 311 | |
| 312 | int WebSocketBasicHandshakeStream::InitializeStream( |
| 313 | const HttpRequestInfo* request_info, |
| 314 | RequestPriority priority, |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 315 | const NetLogWithSource& net_log, |
[email protected] | d51365e | 2013-11-27 10:46:52 | [diff] [blame] | 316 | const CompletionCallback& callback) { |
[email protected] | cd48ed1 | 2014-01-22 14:34:22 | [diff] [blame] | 317 | url_ = request_info->url; |
[email protected] | d51365e | 2013-11-27 10:46:52 | [diff] [blame] | 318 | state_.Initialize(request_info, priority, net_log, callback); |
| 319 | return OK; |
| 320 | } |
| 321 | |
| 322 | int WebSocketBasicHandshakeStream::SendRequest( |
| 323 | const HttpRequestHeaders& headers, |
| 324 | HttpResponseInfo* response, |
| 325 | const CompletionCallback& callback) { |
| 326 | DCHECK(!headers.HasHeader(websockets::kSecWebSocketKey)); |
| 327 | DCHECK(!headers.HasHeader(websockets::kSecWebSocketProtocol)); |
| 328 | DCHECK(!headers.HasHeader(websockets::kSecWebSocketExtensions)); |
| 329 | DCHECK(headers.HasHeader(HttpRequestHeaders::kOrigin)); |
| 330 | DCHECK(headers.HasHeader(websockets::kUpgrade)); |
| 331 | DCHECK(headers.HasHeader(HttpRequestHeaders::kConnection)); |
| 332 | DCHECK(headers.HasHeader(websockets::kSecWebSocketVersion)); |
| 333 | DCHECK(parser()); |
| 334 | |
| 335 | http_response_info_ = response; |
| 336 | |
| 337 | // Create a copy of the headers object, so that we can add the |
| 338 | // Sec-WebSockey-Key header. |
| 339 | HttpRequestHeaders enriched_headers; |
| 340 | enriched_headers.CopyFrom(headers); |
[email protected] | a31ecc0 | 2013-12-05 08:30:55 | [diff] [blame] | 341 | std::string handshake_challenge; |
| 342 | if (handshake_challenge_for_testing_) { |
| 343 | handshake_challenge = *handshake_challenge_for_testing_; |
| 344 | handshake_challenge_for_testing_.reset(); |
| 345 | } else { |
| 346 | handshake_challenge = GenerateHandshakeChallenge(); |
| 347 | } |
[email protected] | d51365e | 2013-11-27 10:46:52 | [diff] [blame] | 348 | enriched_headers.SetHeader(websockets::kSecWebSocketKey, handshake_challenge); |
| 349 | |
[email protected] | d51365e | 2013-11-27 10:46:52 | [diff] [blame] | 350 | AddVectorHeaderIfNonEmpty(websockets::kSecWebSocketExtensions, |
| 351 | requested_extensions_, |
| 352 | &enriched_headers); |
[email protected] | 0be9392 | 2014-01-29 00:42:45 | [diff] [blame] | 353 | AddVectorHeaderIfNonEmpty(websockets::kSecWebSocketProtocol, |
| 354 | requested_sub_protocols_, |
| 355 | &enriched_headers); |
[email protected] | d51365e | 2013-11-27 10:46:52 | [diff] [blame] | 356 | |
ricea | 11bdcd0 | 2014-11-20 09:57:07 | [diff] [blame] | 357 | handshake_challenge_response_ = |
| 358 | ComputeSecWebSocketAccept(handshake_challenge); |
[email protected] | d51365e | 2013-11-27 10:46:52 | [diff] [blame] | 359 | |
[email protected] | cd48ed1 | 2014-01-22 14:34:22 | [diff] [blame] | 360 | DCHECK(connect_delegate_); |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 361 | std::unique_ptr<WebSocketHandshakeRequestInfo> request( |
[email protected] | cd48ed1 | 2014-01-22 14:34:22 | [diff] [blame] | 362 | new WebSocketHandshakeRequestInfo(url_, base::Time::Now())); |
| 363 | request->headers.CopyFrom(enriched_headers); |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 364 | connect_delegate_->OnStartOpeningHandshake(std::move(request)); |
[email protected] | cd48ed1 | 2014-01-22 14:34:22 | [diff] [blame] | 365 | |
[email protected] | d51365e | 2013-11-27 10:46:52 | [diff] [blame] | 366 | return parser()->SendRequest( |
| 367 | state_.GenerateRequestLine(), enriched_headers, response, callback); |
| 368 | } |
| 369 | |
| 370 | int WebSocketBasicHandshakeStream::ReadResponseHeaders( |
| 371 | const CompletionCallback& callback) { |
| 372 | // HttpStreamParser uses a weak pointer when reading from the |
| 373 | // socket, so it won't be called back after being destroyed. The |
| 374 | // HttpStreamParser is owned by HttpBasicState which is owned by this object, |
| 375 | // so this use of base::Unretained() is safe. |
| 376 | int rv = parser()->ReadResponseHeaders( |
| 377 | base::Bind(&WebSocketBasicHandshakeStream::ReadResponseHeadersCallback, |
| 378 | base::Unretained(this), |
| 379 | callback)); |
[email protected] | cd48ed1 | 2014-01-22 14:34:22 | [diff] [blame] | 380 | if (rv == ERR_IO_PENDING) |
| 381 | return rv; |
ricea | 24c195f | 2015-02-26 12:18:55 | [diff] [blame] | 382 | return ValidateResponse(rv); |
[email protected] | d51365e | 2013-11-27 10:46:52 | [diff] [blame] | 383 | } |
| 384 | |
[email protected] | d51365e | 2013-11-27 10:46:52 | [diff] [blame] | 385 | int WebSocketBasicHandshakeStream::ReadResponseBody( |
| 386 | IOBuffer* buf, |
| 387 | int buf_len, |
| 388 | const CompletionCallback& callback) { |
| 389 | return parser()->ReadResponseBody(buf, buf_len, callback); |
| 390 | } |
| 391 | |
| 392 | void WebSocketBasicHandshakeStream::Close(bool not_reusable) { |
| 393 | // This class ignores the value of |not_reusable| and never lets the socket be |
| 394 | // re-used. |
| 395 | if (parser()) |
| 396 | parser()->Close(true); |
| 397 | } |
| 398 | |
| 399 | bool WebSocketBasicHandshakeStream::IsResponseBodyComplete() const { |
| 400 | return parser()->IsResponseBodyComplete(); |
| 401 | } |
| 402 | |
[email protected] | d51365e | 2013-11-27 10:46:52 | [diff] [blame] | 403 | bool WebSocketBasicHandshakeStream::IsConnectionReused() const { |
| 404 | return parser()->IsConnectionReused(); |
| 405 | } |
| 406 | |
| 407 | void WebSocketBasicHandshakeStream::SetConnectionReused() { |
| 408 | parser()->SetConnectionReused(); |
| 409 | } |
| 410 | |
mmenke | bd84c39 | 2015-09-02 14:12:34 | [diff] [blame] | 411 | bool WebSocketBasicHandshakeStream::CanReuseConnection() const { |
[email protected] | d51365e | 2013-11-27 10:46:52 | [diff] [blame] | 412 | return false; |
| 413 | } |
| 414 | |
sclittle | 4de1bab9 | 2015-09-22 21:28:24 | [diff] [blame] | 415 | int64_t WebSocketBasicHandshakeStream::GetTotalReceivedBytes() const { |
[email protected] | bc92bc97 | 2013-12-13 08:32:59 | [diff] [blame] | 416 | return 0; |
| 417 | } |
| 418 | |
sclittle | be1ccf6 | 2015-09-02 19:40:36 | [diff] [blame] | 419 | int64_t WebSocketBasicHandshakeStream::GetTotalSentBytes() const { |
| 420 | return 0; |
| 421 | } |
| 422 | |
rch | cd37901 | 2017-04-12 21:53:32 | [diff] [blame^] | 423 | bool WebSocketBasicHandshakeStream::GetAlternativeService( |
| 424 | AlternativeService* alternative_service) const { |
| 425 | return false; |
| 426 | } |
| 427 | |
[email protected] | d51365e | 2013-11-27 10:46:52 | [diff] [blame] | 428 | bool WebSocketBasicHandshakeStream::GetLoadTimingInfo( |
| 429 | LoadTimingInfo* load_timing_info) const { |
| 430 | return state_.connection()->GetLoadTimingInfo(IsConnectionReused(), |
| 431 | load_timing_info); |
| 432 | } |
| 433 | |
| 434 | void WebSocketBasicHandshakeStream::GetSSLInfo(SSLInfo* ssl_info) { |
| 435 | parser()->GetSSLInfo(ssl_info); |
| 436 | } |
| 437 | |
| 438 | void WebSocketBasicHandshakeStream::GetSSLCertRequestInfo( |
| 439 | SSLCertRequestInfo* cert_request_info) { |
| 440 | parser()->GetSSLCertRequestInfo(cert_request_info); |
| 441 | } |
| 442 | |
ttuttle | d9dbc65 | 2015-09-29 20:00:59 | [diff] [blame] | 443 | bool WebSocketBasicHandshakeStream::GetRemoteEndpoint(IPEndPoint* endpoint) { |
| 444 | if (!state_.connection() || !state_.connection()->socket()) |
| 445 | return false; |
| 446 | |
| 447 | return state_.connection()->socket()->GetPeerAddress(endpoint) == OK; |
| 448 | } |
| 449 | |
zhongyi | ca364fbb | 2015-12-12 03:39:12 | [diff] [blame] | 450 | void WebSocketBasicHandshakeStream::PopulateNetErrorDetails( |
| 451 | NetErrorDetails* /*details*/) { |
| 452 | return; |
| 453 | } |
| 454 | |
nharper | 78e6d2b | 2016-09-21 05:42:35 | [diff] [blame] | 455 | Error WebSocketBasicHandshakeStream::GetTokenBindingSignature( |
nharper | b7441ef | 2016-01-25 23:54:14 | [diff] [blame] | 456 | crypto::ECPrivateKey* key, |
nharper | 78e6d2b | 2016-09-21 05:42:35 | [diff] [blame] | 457 | TokenBindingType tb_type, |
nharper | b7441ef | 2016-01-25 23:54:14 | [diff] [blame] | 458 | std::vector<uint8_t>* out) { |
| 459 | NOTREACHED(); |
| 460 | return ERR_NOT_IMPLEMENTED; |
| 461 | } |
| 462 | |
[email protected] | d51365e | 2013-11-27 10:46:52 | [diff] [blame] | 463 | void WebSocketBasicHandshakeStream::Drain(HttpNetworkSession* session) { |
| 464 | HttpResponseBodyDrainer* drainer = new HttpResponseBodyDrainer(this); |
| 465 | drainer->Start(session); |
| 466 | // |drainer| will delete itself. |
| 467 | } |
| 468 | |
| 469 | void WebSocketBasicHandshakeStream::SetPriority(RequestPriority priority) { |
| 470 | // TODO(ricea): See TODO comment in HttpBasicStream::SetPriority(). If it is |
| 471 | // gone, then copy whatever has happened there over here. |
| 472 | } |
| 473 | |
yhirano | a7e05bb | 2014-11-06 05:40:39 | [diff] [blame] | 474 | HttpStream* WebSocketBasicHandshakeStream::RenewStreamForAuth() { |
| 475 | // Return null because we don't support renewing the stream. |
| 476 | return nullptr; |
| 477 | } |
| 478 | |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 479 | std::unique_ptr<WebSocketStream> WebSocketBasicHandshakeStream::Upgrade() { |
[email protected] | d51365e | 2013-11-27 10:46:52 | [diff] [blame] | 480 | // The HttpStreamParser object has a pointer to our ClientSocketHandle. Make |
| 481 | // sure it does not touch it again before it is destroyed. |
| 482 | state_.DeleteParser(); |
[email protected] | 65486614 | 2014-06-24 22:53:31 | [diff] [blame] | 483 | WebSocketTransportClientSocketPool::UnlockEndpoint(state_.connection()); |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 484 | std::unique_ptr<WebSocketStream> basic_stream( |
| 485 | new WebSocketBasicStream(state_.ReleaseConnection(), state_.read_buf(), |
| 486 | sub_protocol_, extensions_)); |
[email protected] | 0be9392 | 2014-01-29 00:42:45 | [diff] [blame] | 487 | DCHECK(extension_params_.get()); |
| 488 | if (extension_params_->deflate_enabled) { |
[email protected] | 9c50b04 | 2014-04-28 06:40:15 | [diff] [blame] | 489 | UMA_HISTOGRAM_ENUMERATION( |
| 490 | "Net.WebSocket.DeflateMode", |
yhirano | 8387aee | 2015-09-14 05:46:49 | [diff] [blame] | 491 | extension_params_->deflate_parameters.client_context_take_over_mode(), |
[email protected] | 9c50b04 | 2014-04-28 06:40:15 | [diff] [blame] | 492 | WebSocketDeflater::NUM_CONTEXT_TAKEOVER_MODE_TYPES); |
| 493 | |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 494 | return std::unique_ptr<WebSocketStream>(new WebSocketDeflateStream( |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 495 | std::move(basic_stream), extension_params_->deflate_parameters, |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 496 | std::unique_ptr<WebSocketDeflatePredictor>( |
yhirano | 8387aee | 2015-09-14 05:46:49 | [diff] [blame] | 497 | new WebSocketDeflatePredictorImpl))); |
[email protected] | 0be9392 | 2014-01-29 00:42:45 | [diff] [blame] | 498 | } else { |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 499 | return basic_stream; |
[email protected] | 0be9392 | 2014-01-29 00:42:45 | [diff] [blame] | 500 | } |
[email protected] | d51365e | 2013-11-27 10:46:52 | [diff] [blame] | 501 | } |
| 502 | |
[email protected] | a31ecc0 | 2013-12-05 08:30:55 | [diff] [blame] | 503 | void WebSocketBasicHandshakeStream::SetWebSocketKeyForTesting( |
| 504 | const std::string& key) { |
| 505 | handshake_challenge_for_testing_.reset(new std::string(key)); |
| 506 | } |
| 507 | |
[email protected] | d51365e | 2013-11-27 10:46:52 | [diff] [blame] | 508 | void WebSocketBasicHandshakeStream::ReadResponseHeadersCallback( |
| 509 | const CompletionCallback& callback, |
| 510 | int result) { |
ricea | 24c195f | 2015-02-26 12:18:55 | [diff] [blame] | 511 | callback.Run(ValidateResponse(result)); |
[email protected] | d51365e | 2013-11-27 10:46:52 | [diff] [blame] | 512 | } |
| 513 | |
[email protected] | cd48ed1 | 2014-01-22 14:34:22 | [diff] [blame] | 514 | void WebSocketBasicHandshakeStream::OnFinishOpeningHandshake() { |
[email protected] | cd48ed1 | 2014-01-22 14:34:22 | [diff] [blame] | 515 | DCHECK(http_response_info_); |
[email protected] | e69c1cd | 2014-07-29 07:42:29 | [diff] [blame] | 516 | WebSocketDispatchOnFinishOpeningHandshake(connect_delegate_, |
| 517 | url_, |
| 518 | http_response_info_->headers, |
| 519 | http_response_info_->response_time); |
[email protected] | cd48ed1 | 2014-01-22 14:34:22 | [diff] [blame] | 520 | } |
| 521 | |
ricea | 24c195f | 2015-02-26 12:18:55 | [diff] [blame] | 522 | int WebSocketBasicHandshakeStream::ValidateResponse(int rv) { |
[email protected] | d51365e | 2013-11-27 10:46:52 | [diff] [blame] | 523 | DCHECK(http_response_info_); |
[email protected] | f7e98ca | 2014-06-19 12:05:43 | [diff] [blame] | 524 | // Most net errors happen during connection, so they are not seen by this |
| 525 | // method. The histogram for error codes is created in |
| 526 | // Delegate::OnResponseStarted in websocket_stream.cc instead. |
[email protected] | e5760f52 | 2014-02-05 12:28:50 | [diff] [blame] | 527 | if (rv >= 0) { |
[email protected] | f7e98ca | 2014-06-19 12:05:43 | [diff] [blame] | 528 | const HttpResponseHeaders* headers = http_response_info_->headers.get(); |
| 529 | const int response_code = headers->response_code(); |
| 530 | UMA_HISTOGRAM_SPARSE_SLOWLY("Net.WebSocket.ResponseCode", response_code); |
| 531 | switch (response_code) { |
[email protected] | e5760f52 | 2014-02-05 12:28:50 | [diff] [blame] | 532 | case HTTP_SWITCHING_PROTOCOLS: |
| 533 | OnFinishOpeningHandshake(); |
| 534 | return ValidateUpgradeResponse(headers); |
[email protected] | d51365e | 2013-11-27 10:46:52 | [diff] [blame] | 535 | |
[email protected] | e5760f52 | 2014-02-05 12:28:50 | [diff] [blame] | 536 | // We need to pass these through for authentication to work. |
| 537 | case HTTP_UNAUTHORIZED: |
| 538 | case HTTP_PROXY_AUTHENTICATION_REQUIRED: |
| 539 | return OK; |
[email protected] | d51365e | 2013-11-27 10:46:52 | [diff] [blame] | 540 | |
[email protected] | e5760f52 | 2014-02-05 12:28:50 | [diff] [blame] | 541 | // Other status codes are potentially risky (see the warnings in the |
| 542 | // WHATWG WebSocket API spec) and so are dropped by default. |
| 543 | default: |
[email protected] | aeb640d | 2014-02-21 11:03:18 | [diff] [blame] | 544 | // A WebSocket server cannot be using HTTP/0.9, so if we see version |
| 545 | // 0.9, it means the response was garbage. |
| 546 | // Reporting "Unexpected response code: 200" in this case is not |
| 547 | // helpful, so use a different error message. |
| 548 | if (headers->GetHttpVersion() == HttpVersion(0, 9)) { |
tyoshino | ccfcfde | 2016-07-21 14:06:55 | [diff] [blame] | 549 | OnFailure("Error during WebSocket handshake: Invalid status line"); |
[email protected] | aeb640d | 2014-02-21 11:03:18 | [diff] [blame] | 550 | } else { |
tyoshino | ccfcfde | 2016-07-21 14:06:55 | [diff] [blame] | 551 | OnFailure(base::StringPrintf( |
[email protected] | aeb640d | 2014-02-21 11:03:18 | [diff] [blame] | 552 | "Error during WebSocket handshake: Unexpected response code: %d", |
[email protected] | 8aba017 | 2014-07-03 12:09:53 | [diff] [blame] | 553 | headers->response_code())); |
[email protected] | aeb640d | 2014-02-21 11:03:18 | [diff] [blame] | 554 | } |
[email protected] | e5760f52 | 2014-02-05 12:28:50 | [diff] [blame] | 555 | OnFinishOpeningHandshake(); |
| 556 | return ERR_INVALID_RESPONSE; |
| 557 | } |
| 558 | } else { |
[email protected] | 3efc08f | 2014-02-07 09:33:34 | [diff] [blame] | 559 | if (rv == ERR_EMPTY_RESPONSE) { |
tyoshino | ccfcfde | 2016-07-21 14:06:55 | [diff] [blame] | 560 | OnFailure("Connection closed before receiving a handshake response"); |
[email protected] | 3efc08f | 2014-02-07 09:33:34 | [diff] [blame] | 561 | return rv; |
| 562 | } |
tyoshino | ccfcfde | 2016-07-21 14:06:55 | [diff] [blame] | 563 | OnFailure(std::string("Error during WebSocket handshake: ") + |
| 564 | ErrorToString(rv)); |
[email protected] | e5760f52 | 2014-02-05 12:28:50 | [diff] [blame] | 565 | OnFinishOpeningHandshake(); |
ricea | 23c3f94 | 2015-02-02 13:35:13 | [diff] [blame] | 566 | // Some error codes (for example ERR_CONNECTION_CLOSED) get changed to OK at |
| 567 | // higher levels. To prevent an unvalidated connection getting erroneously |
| 568 | // upgraded, don't pass through the status code unchanged if it is |
| 569 | // HTTP_SWITCHING_PROTOCOLS. |
| 570 | if (http_response_info_->headers && |
| 571 | http_response_info_->headers->response_code() == |
| 572 | HTTP_SWITCHING_PROTOCOLS) { |
| 573 | http_response_info_->headers->ReplaceStatusLine( |
| 574 | kConnectionErrorStatusLine); |
| 575 | } |
[email protected] | e5760f52 | 2014-02-05 12:28:50 | [diff] [blame] | 576 | return rv; |
[email protected] | d51365e | 2013-11-27 10:46:52 | [diff] [blame] | 577 | } |
| 578 | } |
| 579 | |
| 580 | int WebSocketBasicHandshakeStream::ValidateUpgradeResponse( |
[email protected] | e5760f52 | 2014-02-05 12:28:50 | [diff] [blame] | 581 | const HttpResponseHeaders* headers) { |
[email protected] | 0be9392 | 2014-01-29 00:42:45 | [diff] [blame] | 582 | extension_params_.reset(new WebSocketExtensionParams); |
[email protected] | 8aba017 | 2014-07-03 12:09:53 | [diff] [blame] | 583 | std::string failure_message; |
| 584 | if (ValidateUpgrade(headers, &failure_message) && |
| 585 | ValidateSecWebSocketAccept( |
| 586 | headers, handshake_challenge_response_, &failure_message) && |
| 587 | ValidateConnection(headers, &failure_message) && |
[email protected] | e5760f52 | 2014-02-05 12:28:50 | [diff] [blame] | 588 | ValidateSubProtocol(headers, |
[email protected] | 9686820 | 2014-01-09 10:38:04 | [diff] [blame] | 589 | requested_sub_protocols_, |
| 590 | &sub_protocol_, |
[email protected] | 8aba017 | 2014-07-03 12:09:53 | [diff] [blame] | 591 | &failure_message) && |
[email protected] | e5760f52 | 2014-02-05 12:28:50 | [diff] [blame] | 592 | ValidateExtensions(headers, |
[email protected] | 9686820 | 2014-01-09 10:38:04 | [diff] [blame] | 593 | &extensions_, |
[email protected] | 8aba017 | 2014-07-03 12:09:53 | [diff] [blame] | 594 | &failure_message, |
[email protected] | 0be9392 | 2014-01-29 00:42:45 | [diff] [blame] | 595 | extension_params_.get())) { |
[email protected] | d51365e | 2013-11-27 10:46:52 | [diff] [blame] | 596 | return OK; |
| 597 | } |
tyoshino | ccfcfde | 2016-07-21 14:06:55 | [diff] [blame] | 598 | OnFailure("Error during WebSocket handshake: " + failure_message); |
[email protected] | d51365e | 2013-11-27 10:46:52 | [diff] [blame] | 599 | return ERR_INVALID_RESPONSE; |
| 600 | } |
| 601 | |
tyoshino | ccfcfde | 2016-07-21 14:06:55 | [diff] [blame] | 602 | void WebSocketBasicHandshakeStream::OnFailure(const std::string& message) { |
| 603 | stream_request_->OnFailure(message); |
[email protected] | 8aba017 | 2014-07-03 12:09:53 | [diff] [blame] | 604 | } |
| 605 | |
[email protected] | d51365e | 2013-11-27 10:46:52 | [diff] [blame] | 606 | } // namespace net |