| OLD | NEW |
| (Empty) |
| 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 | |
| 7 #include <algorithm> | |
| 8 #include <iterator> | |
| 9 #include <set> | |
| 10 #include <string> | |
| 11 #include <vector> | |
| 12 | |
| 13 #include "base/base64.h" | |
| 14 #include "base/basictypes.h" | |
| 15 #include "base/bind.h" | |
| 16 #include "base/compiler_specific.h" | |
| 17 #include "base/containers/hash_tables.h" | |
| 18 #include "base/logging.h" | |
| 19 #include "base/metrics/histogram.h" | |
| 20 #include "base/metrics/sparse_histogram.h" | |
| 21 #include "base/stl_util.h" | |
| 22 #include "base/strings/string_number_conversions.h" | |
| 23 #include "base/strings/string_piece.h" | |
| 24 #include "base/strings/string_util.h" | |
| 25 #include "base/strings/stringprintf.h" | |
| 26 #include "base/time/time.h" | |
| 27 #include "crypto/random.h" | |
| 28 #include "net/base/io_buffer.h" | |
| 29 #include "net/http/http_request_headers.h" | |
| 30 #include "net/http/http_request_info.h" | |
| 31 #include "net/http/http_response_body_drainer.h" | |
| 32 #include "net/http/http_response_headers.h" | |
| 33 #include "net/http/http_status_code.h" | |
| 34 #include "net/http/http_stream_parser.h" | |
| 35 #include "net/socket/client_socket_handle.h" | |
| 36 #include "net/socket/websocket_transport_client_socket_pool.h" | |
| 37 #include "net/websockets/websocket_basic_stream.h" | |
| 38 #include "net/websockets/websocket_deflate_predictor.h" | |
| 39 #include "net/websockets/websocket_deflate_predictor_impl.h" | |
| 40 #include "net/websockets/websocket_deflate_stream.h" | |
| 41 #include "net/websockets/websocket_deflater.h" | |
| 42 #include "net/websockets/websocket_extension_parser.h" | |
| 43 #include "net/websockets/websocket_handshake_challenge.h" | |
| 44 #include "net/websockets/websocket_handshake_constants.h" | |
| 45 #include "net/websockets/websocket_handshake_request_info.h" | |
| 46 #include "net/websockets/websocket_handshake_response_info.h" | |
| 47 #include "net/websockets/websocket_stream.h" | |
| 48 | |
| 49 namespace net { | |
| 50 | |
| 51 namespace { | |
| 52 | |
| 53 const char kConnectionErrorStatusLine[] = "HTTP/1.1 503 Connection Error"; | |
| 54 | |
| 55 // TODO(yhirano): Remove these functions once http://crbug.com/399535 is fixed. | |
| 56 NOINLINE void RunCallbackWithOk(const CompletionCallback& callback, | |
| 57 int result) { | |
| 58 DCHECK_EQ(result, OK); | |
| 59 callback.Run(OK); | |
| 60 } | |
| 61 | |
| 62 NOINLINE void RunCallbackWithInvalidResponseCausedByRedirect( | |
| 63 const CompletionCallback& callback, | |
| 64 int result) { | |
| 65 DCHECK_EQ(result, ERR_INVALID_RESPONSE); | |
| 66 callback.Run(ERR_INVALID_RESPONSE); | |
| 67 } | |
| 68 | |
| 69 NOINLINE void RunCallbackWithInvalidResponse( | |
| 70 const CompletionCallback& callback, | |
| 71 int result) { | |
| 72 DCHECK_EQ(result, ERR_INVALID_RESPONSE); | |
| 73 callback.Run(ERR_INVALID_RESPONSE); | |
| 74 } | |
| 75 | |
| 76 NOINLINE void RunCallback(const CompletionCallback& callback, int result) { | |
| 77 callback.Run(result); | |
| 78 } | |
| 79 | |
| 80 } // namespace | |
| 81 | |
| 82 // TODO(ricea): If more extensions are added, replace this with a more general | |
| 83 // mechanism. | |
| 84 struct WebSocketExtensionParams { | |
| 85 WebSocketExtensionParams() | |
| 86 : deflate_enabled(false), | |
| 87 client_window_bits(15), | |
| 88 deflate_mode(WebSocketDeflater::TAKE_OVER_CONTEXT) {} | |
| 89 | |
| 90 bool deflate_enabled; | |
| 91 int client_window_bits; | |
| 92 WebSocketDeflater::ContextTakeOverMode deflate_mode; | |
| 93 }; | |
| 94 | |
| 95 namespace { | |
| 96 | |
| 97 enum GetHeaderResult { | |
| 98 GET_HEADER_OK, | |
| 99 GET_HEADER_MISSING, | |
| 100 GET_HEADER_MULTIPLE, | |
| 101 }; | |
| 102 | |
| 103 std::string MissingHeaderMessage(const std::string& header_name) { | |
| 104 return std::string("'") + header_name + "' header is missing"; | |
| 105 } | |
| 106 | |
| 107 std::string MultipleHeaderValuesMessage(const std::string& header_name) { | |
| 108 return | |
| 109 std::string("'") + | |
| 110 header_name + | |
| 111 "' header must not appear more than once in a response"; | |
| 112 } | |
| 113 | |
| 114 std::string GenerateHandshakeChallenge() { | |
| 115 std::string raw_challenge(websockets::kRawChallengeLength, '\0'); | |
| 116 crypto::RandBytes(string_as_array(&raw_challenge), raw_challenge.length()); | |
| 117 std::string encoded_challenge; | |
| 118 base::Base64Encode(raw_challenge, &encoded_challenge); | |
| 119 return encoded_challenge; | |
| 120 } | |
| 121 | |
| 122 void AddVectorHeaderIfNonEmpty(const char* name, | |
| 123 const std::vector<std::string>& value, | |
| 124 HttpRequestHeaders* headers) { | |
| 125 if (value.empty()) | |
| 126 return; | |
| 127 headers->SetHeader(name, JoinString(value, ", ")); | |
| 128 } | |
| 129 | |
| 130 GetHeaderResult GetSingleHeaderValue(const HttpResponseHeaders* headers, | |
| 131 const base::StringPiece& name, | |
| 132 std::string* value) { | |
| 133 void* state = nullptr; | |
| 134 size_t num_values = 0; | |
| 135 std::string temp_value; | |
| 136 while (headers->EnumerateHeader(&state, name, &temp_value)) { | |
| 137 if (++num_values > 1) | |
| 138 return GET_HEADER_MULTIPLE; | |
| 139 *value = temp_value; | |
| 140 } | |
| 141 return num_values > 0 ? GET_HEADER_OK : GET_HEADER_MISSING; | |
| 142 } | |
| 143 | |
| 144 bool ValidateHeaderHasSingleValue(GetHeaderResult result, | |
| 145 const std::string& header_name, | |
| 146 std::string* failure_message) { | |
| 147 if (result == GET_HEADER_MISSING) { | |
| 148 *failure_message = MissingHeaderMessage(header_name); | |
| 149 return false; | |
| 150 } | |
| 151 if (result == GET_HEADER_MULTIPLE) { | |
| 152 *failure_message = MultipleHeaderValuesMessage(header_name); | |
| 153 return false; | |
| 154 } | |
| 155 DCHECK_EQ(result, GET_HEADER_OK); | |
| 156 return true; | |
| 157 } | |
| 158 | |
| 159 bool ValidateUpgrade(const HttpResponseHeaders* headers, | |
| 160 std::string* failure_message) { | |
| 161 std::string value; | |
| 162 GetHeaderResult result = | |
| 163 GetSingleHeaderValue(headers, websockets::kUpgrade, &value); | |
| 164 if (!ValidateHeaderHasSingleValue(result, | |
| 165 websockets::kUpgrade, | |
| 166 failure_message)) { | |
| 167 return false; | |
| 168 } | |
| 169 | |
| 170 if (!LowerCaseEqualsASCII(value, websockets::kWebSocketLowercase)) { | |
| 171 *failure_message = | |
| 172 "'Upgrade' header value is not 'WebSocket': " + value; | |
| 173 return false; | |
| 174 } | |
| 175 return true; | |
| 176 } | |
| 177 | |
| 178 bool ValidateSecWebSocketAccept(const HttpResponseHeaders* headers, | |
| 179 const std::string& expected, | |
| 180 std::string* failure_message) { | |
| 181 std::string actual; | |
| 182 GetHeaderResult result = | |
| 183 GetSingleHeaderValue(headers, websockets::kSecWebSocketAccept, &actual); | |
| 184 if (!ValidateHeaderHasSingleValue(result, | |
| 185 websockets::kSecWebSocketAccept, | |
| 186 failure_message)) { | |
| 187 return false; | |
| 188 } | |
| 189 | |
| 190 if (expected != actual) { | |
| 191 *failure_message = "Incorrect 'Sec-WebSocket-Accept' header value"; | |
| 192 return false; | |
| 193 } | |
| 194 return true; | |
| 195 } | |
| 196 | |
| 197 bool ValidateConnection(const HttpResponseHeaders* headers, | |
| 198 std::string* failure_message) { | |
| 199 // Connection header is permitted to contain other tokens. | |
| 200 if (!headers->HasHeader(HttpRequestHeaders::kConnection)) { | |
| 201 *failure_message = MissingHeaderMessage(HttpRequestHeaders::kConnection); | |
| 202 return false; | |
| 203 } | |
| 204 if (!headers->HasHeaderValue(HttpRequestHeaders::kConnection, | |
| 205 websockets::kUpgrade)) { | |
| 206 *failure_message = "'Connection' header value must contain 'Upgrade'"; | |
| 207 return false; | |
| 208 } | |
| 209 return true; | |
| 210 } | |
| 211 | |
| 212 bool ValidateSubProtocol( | |
| 213 const HttpResponseHeaders* headers, | |
| 214 const std::vector<std::string>& requested_sub_protocols, | |
| 215 std::string* sub_protocol, | |
| 216 std::string* failure_message) { | |
| 217 void* state = nullptr; | |
| 218 std::string value; | |
| 219 base::hash_set<std::string> requested_set(requested_sub_protocols.begin(), | |
| 220 requested_sub_protocols.end()); | |
| 221 int count = 0; | |
| 222 bool has_multiple_protocols = false; | |
| 223 bool has_invalid_protocol = false; | |
| 224 | |
| 225 while (!has_invalid_protocol || !has_multiple_protocols) { | |
| 226 std::string temp_value; | |
| 227 if (!headers->EnumerateHeader( | |
| 228 &state, websockets::kSecWebSocketProtocol, &temp_value)) | |
| 229 break; | |
| 230 value = temp_value; | |
| 231 if (requested_set.count(value) == 0) | |
| 232 has_invalid_protocol = true; | |
| 233 if (++count > 1) | |
| 234 has_multiple_protocols = true; | |
| 235 } | |
| 236 | |
| 237 if (has_multiple_protocols) { | |
| 238 *failure_message = | |
| 239 MultipleHeaderValuesMessage(websockets::kSecWebSocketProtocol); | |
| 240 return false; | |
| 241 } else if (count > 0 && requested_sub_protocols.size() == 0) { | |
| 242 *failure_message = | |
| 243 std::string("Response must not include 'Sec-WebSocket-Protocol' " | |
| 244 "header if not present in request: ") | |
| 245 + value; | |
| 246 return false; | |
| 247 } else if (has_invalid_protocol) { | |
| 248 *failure_message = | |
| 249 "'Sec-WebSocket-Protocol' header value '" + | |
| 250 value + | |
| 251 "' in response does not match any of sent values"; | |
| 252 return false; | |
| 253 } else if (requested_sub_protocols.size() > 0 && count == 0) { | |
| 254 *failure_message = | |
| 255 "Sent non-empty 'Sec-WebSocket-Protocol' header " | |
| 256 "but no response was received"; | |
| 257 return false; | |
| 258 } | |
| 259 *sub_protocol = value; | |
| 260 return true; | |
| 261 } | |
| 262 | |
| 263 bool DeflateError(std::string* message, const base::StringPiece& piece) { | |
| 264 *message = "Error in permessage-deflate: "; | |
| 265 piece.AppendToString(message); | |
| 266 return false; | |
| 267 } | |
| 268 | |
| 269 bool ValidatePerMessageDeflateExtension(const WebSocketExtension& extension, | |
| 270 std::string* failure_message, | |
| 271 WebSocketExtensionParams* params) { | |
| 272 static const char kClientPrefix[] = "client_"; | |
| 273 static const char kServerPrefix[] = "server_"; | |
| 274 static const char kNoContextTakeover[] = "no_context_takeover"; | |
| 275 static const char kMaxWindowBits[] = "max_window_bits"; | |
| 276 const size_t kPrefixLen = arraysize(kClientPrefix) - 1; | |
| 277 static_assert(kPrefixLen == arraysize(kServerPrefix) - 1, | |
| 278 "the strings server and client must be the same length"); | |
| 279 typedef std::vector<WebSocketExtension::Parameter> ParameterVector; | |
| 280 | |
| 281 DCHECK_EQ("permessage-deflate", extension.name()); | |
| 282 const ParameterVector& parameters = extension.parameters(); | |
| 283 std::set<std::string> seen_names; | |
| 284 for (ParameterVector::const_iterator it = parameters.begin(); | |
| 285 it != parameters.end(); ++it) { | |
| 286 const std::string& name = it->name(); | |
| 287 if (seen_names.count(name) != 0) { | |
| 288 return DeflateError( | |
| 289 failure_message, | |
| 290 "Received duplicate permessage-deflate extension parameter " + name); | |
| 291 } | |
| 292 seen_names.insert(name); | |
| 293 const std::string client_or_server(name, 0, kPrefixLen); | |
| 294 const bool is_client = (client_or_server == kClientPrefix); | |
| 295 if (!is_client && client_or_server != kServerPrefix) { | |
| 296 return DeflateError( | |
| 297 failure_message, | |
| 298 "Received an unexpected permessage-deflate extension parameter"); | |
| 299 } | |
| 300 const std::string rest(name, kPrefixLen); | |
| 301 if (rest == kNoContextTakeover) { | |
| 302 if (it->HasValue()) { | |
| 303 return DeflateError(failure_message, | |
| 304 "Received invalid " + name + " parameter"); | |
| 305 } | |
| 306 if (is_client) | |
| 307 params->deflate_mode = WebSocketDeflater::DO_NOT_TAKE_OVER_CONTEXT; | |
| 308 } else if (rest == kMaxWindowBits) { | |
| 309 if (!it->HasValue()) | |
| 310 return DeflateError(failure_message, name + " must have value"); | |
| 311 int bits = 0; | |
| 312 if (!base::StringToInt(it->value(), &bits) || bits < 8 || bits > 15 || | |
| 313 it->value()[0] == '0' || | |
| 314 it->value().find_first_not_of("0123456789") != std::string::npos) { | |
| 315 return DeflateError(failure_message, | |
| 316 "Received invalid " + name + " parameter"); | |
| 317 } | |
| 318 if (is_client) | |
| 319 params->client_window_bits = bits; | |
| 320 } else { | |
| 321 return DeflateError( | |
| 322 failure_message, | |
| 323 "Received an unexpected permessage-deflate extension parameter"); | |
| 324 } | |
| 325 } | |
| 326 params->deflate_enabled = true; | |
| 327 return true; | |
| 328 } | |
| 329 | |
| 330 bool ValidateExtensions(const HttpResponseHeaders* headers, | |
| 331 const std::vector<std::string>& requested_extensions, | |
| 332 std::string* extensions, | |
| 333 std::string* failure_message, | |
| 334 WebSocketExtensionParams* params) { | |
| 335 void* state = nullptr; | |
| 336 std::string value; | |
| 337 std::vector<std::string> accepted_extensions; | |
| 338 // TODO(ricea): If adding support for additional extensions, generalise this | |
| 339 // code. | |
| 340 bool seen_permessage_deflate = false; | |
| 341 while (headers->EnumerateHeader( | |
| 342 &state, websockets::kSecWebSocketExtensions, &value)) { | |
| 343 WebSocketExtensionParser parser; | |
| 344 parser.Parse(value); | |
| 345 if (parser.has_error()) { | |
| 346 // TODO(yhirano) Set appropriate failure message. | |
| 347 *failure_message = | |
| 348 "'Sec-WebSocket-Extensions' header value is " | |
| 349 "rejected by the parser: " + | |
| 350 value; | |
| 351 return false; | |
| 352 } | |
| 353 if (parser.extension().name() == "permessage-deflate") { | |
| 354 if (seen_permessage_deflate) { | |
| 355 *failure_message = "Received duplicate permessage-deflate response"; | |
| 356 return false; | |
| 357 } | |
| 358 seen_permessage_deflate = true; | |
| 359 if (!ValidatePerMessageDeflateExtension( | |
| 360 parser.extension(), failure_message, params)) | |
| 361 return false; | |
| 362 } else { | |
| 363 *failure_message = | |
| 364 "Found an unsupported extension '" + | |
| 365 parser.extension().name() + | |
| 366 "' in 'Sec-WebSocket-Extensions' header"; | |
| 367 return false; | |
| 368 } | |
| 369 accepted_extensions.push_back(value); | |
| 370 } | |
| 371 *extensions = JoinString(accepted_extensions, ", "); | |
| 372 return true; | |
| 373 } | |
| 374 | |
| 375 } // namespace | |
| 376 | |
| 377 WebSocketBasicHandshakeStream::WebSocketBasicHandshakeStream( | |
| 378 scoped_ptr<ClientSocketHandle> connection, | |
| 379 WebSocketStream::ConnectDelegate* connect_delegate, | |
| 380 bool using_proxy, | |
| 381 std::vector<std::string> requested_sub_protocols, | |
| 382 std::vector<std::string> requested_extensions, | |
| 383 std::string* failure_message) | |
| 384 : state_(connection.release(), using_proxy), | |
| 385 connect_delegate_(connect_delegate), | |
| 386 http_response_info_(nullptr), | |
| 387 requested_sub_protocols_(requested_sub_protocols), | |
| 388 requested_extensions_(requested_extensions), | |
| 389 failure_message_(failure_message) { | |
| 390 DCHECK(connect_delegate); | |
| 391 DCHECK(failure_message); | |
| 392 } | |
| 393 | |
| 394 WebSocketBasicHandshakeStream::~WebSocketBasicHandshakeStream() {} | |
| 395 | |
| 396 int WebSocketBasicHandshakeStream::InitializeStream( | |
| 397 const HttpRequestInfo* request_info, | |
| 398 RequestPriority priority, | |
| 399 const BoundNetLog& net_log, | |
| 400 const CompletionCallback& callback) { | |
| 401 url_ = request_info->url; | |
| 402 state_.Initialize(request_info, priority, net_log, callback); | |
| 403 return OK; | |
| 404 } | |
| 405 | |
| 406 int WebSocketBasicHandshakeStream::SendRequest( | |
| 407 const HttpRequestHeaders& headers, | |
| 408 HttpResponseInfo* response, | |
| 409 const CompletionCallback& callback) { | |
| 410 DCHECK(!headers.HasHeader(websockets::kSecWebSocketKey)); | |
| 411 DCHECK(!headers.HasHeader(websockets::kSecWebSocketProtocol)); | |
| 412 DCHECK(!headers.HasHeader(websockets::kSecWebSocketExtensions)); | |
| 413 DCHECK(headers.HasHeader(HttpRequestHeaders::kOrigin)); | |
| 414 DCHECK(headers.HasHeader(websockets::kUpgrade)); | |
| 415 DCHECK(headers.HasHeader(HttpRequestHeaders::kConnection)); | |
| 416 DCHECK(headers.HasHeader(websockets::kSecWebSocketVersion)); | |
| 417 DCHECK(parser()); | |
| 418 | |
| 419 http_response_info_ = response; | |
| 420 | |
| 421 // Create a copy of the headers object, so that we can add the | |
| 422 // Sec-WebSockey-Key header. | |
| 423 HttpRequestHeaders enriched_headers; | |
| 424 enriched_headers.CopyFrom(headers); | |
| 425 std::string handshake_challenge; | |
| 426 if (handshake_challenge_for_testing_) { | |
| 427 handshake_challenge = *handshake_challenge_for_testing_; | |
| 428 handshake_challenge_for_testing_.reset(); | |
| 429 } else { | |
| 430 handshake_challenge = GenerateHandshakeChallenge(); | |
| 431 } | |
| 432 enriched_headers.SetHeader(websockets::kSecWebSocketKey, handshake_challenge); | |
| 433 | |
| 434 AddVectorHeaderIfNonEmpty(websockets::kSecWebSocketExtensions, | |
| 435 requested_extensions_, | |
| 436 &enriched_headers); | |
| 437 AddVectorHeaderIfNonEmpty(websockets::kSecWebSocketProtocol, | |
| 438 requested_sub_protocols_, | |
| 439 &enriched_headers); | |
| 440 | |
| 441 handshake_challenge_response_ = | |
| 442 ComputeSecWebSocketAccept(handshake_challenge); | |
| 443 | |
| 444 DCHECK(connect_delegate_); | |
| 445 scoped_ptr<WebSocketHandshakeRequestInfo> request( | |
| 446 new WebSocketHandshakeRequestInfo(url_, base::Time::Now())); | |
| 447 request->headers.CopyFrom(enriched_headers); | |
| 448 connect_delegate_->OnStartOpeningHandshake(request.Pass()); | |
| 449 | |
| 450 return parser()->SendRequest( | |
| 451 state_.GenerateRequestLine(), enriched_headers, response, callback); | |
| 452 } | |
| 453 | |
| 454 int WebSocketBasicHandshakeStream::ReadResponseHeaders( | |
| 455 const CompletionCallback& callback) { | |
| 456 // HttpStreamParser uses a weak pointer when reading from the | |
| 457 // socket, so it won't be called back after being destroyed. The | |
| 458 // HttpStreamParser is owned by HttpBasicState which is owned by this object, | |
| 459 // so this use of base::Unretained() is safe. | |
| 460 int rv = parser()->ReadResponseHeaders( | |
| 461 base::Bind(&WebSocketBasicHandshakeStream::ReadResponseHeadersCallback, | |
| 462 base::Unretained(this), | |
| 463 callback)); | |
| 464 if (rv == ERR_IO_PENDING) | |
| 465 return rv; | |
| 466 bool is_redirect = false; | |
| 467 return ValidateResponse(rv, &is_redirect); | |
| 468 } | |
| 469 | |
| 470 int WebSocketBasicHandshakeStream::ReadResponseBody( | |
| 471 IOBuffer* buf, | |
| 472 int buf_len, | |
| 473 const CompletionCallback& callback) { | |
| 474 return parser()->ReadResponseBody(buf, buf_len, callback); | |
| 475 } | |
| 476 | |
| 477 void WebSocketBasicHandshakeStream::Close(bool not_reusable) { | |
| 478 // This class ignores the value of |not_reusable| and never lets the socket be | |
| 479 // re-used. | |
| 480 if (parser()) | |
| 481 parser()->Close(true); | |
| 482 } | |
| 483 | |
| 484 bool WebSocketBasicHandshakeStream::IsResponseBodyComplete() const { | |
| 485 return parser()->IsResponseBodyComplete(); | |
| 486 } | |
| 487 | |
| 488 bool WebSocketBasicHandshakeStream::CanFindEndOfResponse() const { | |
| 489 return parser() && parser()->CanFindEndOfResponse(); | |
| 490 } | |
| 491 | |
| 492 bool WebSocketBasicHandshakeStream::IsConnectionReused() const { | |
| 493 return parser()->IsConnectionReused(); | |
| 494 } | |
| 495 | |
| 496 void WebSocketBasicHandshakeStream::SetConnectionReused() { | |
| 497 parser()->SetConnectionReused(); | |
| 498 } | |
| 499 | |
| 500 bool WebSocketBasicHandshakeStream::IsConnectionReusable() const { | |
| 501 return false; | |
| 502 } | |
| 503 | |
| 504 int64 WebSocketBasicHandshakeStream::GetTotalReceivedBytes() const { | |
| 505 return 0; | |
| 506 } | |
| 507 | |
| 508 bool WebSocketBasicHandshakeStream::GetLoadTimingInfo( | |
| 509 LoadTimingInfo* load_timing_info) const { | |
| 510 return state_.connection()->GetLoadTimingInfo(IsConnectionReused(), | |
| 511 load_timing_info); | |
| 512 } | |
| 513 | |
| 514 void WebSocketBasicHandshakeStream::GetSSLInfo(SSLInfo* ssl_info) { | |
| 515 parser()->GetSSLInfo(ssl_info); | |
| 516 } | |
| 517 | |
| 518 void WebSocketBasicHandshakeStream::GetSSLCertRequestInfo( | |
| 519 SSLCertRequestInfo* cert_request_info) { | |
| 520 parser()->GetSSLCertRequestInfo(cert_request_info); | |
| 521 } | |
| 522 | |
| 523 bool WebSocketBasicHandshakeStream::IsSpdyHttpStream() const { return false; } | |
| 524 | |
| 525 void WebSocketBasicHandshakeStream::Drain(HttpNetworkSession* session) { | |
| 526 HttpResponseBodyDrainer* drainer = new HttpResponseBodyDrainer(this); | |
| 527 drainer->Start(session); | |
| 528 // |drainer| will delete itself. | |
| 529 } | |
| 530 | |
| 531 void WebSocketBasicHandshakeStream::SetPriority(RequestPriority priority) { | |
| 532 // TODO(ricea): See TODO comment in HttpBasicStream::SetPriority(). If it is | |
| 533 // gone, then copy whatever has happened there over here. | |
| 534 } | |
| 535 | |
| 536 UploadProgress WebSocketBasicHandshakeStream::GetUploadProgress() const { | |
| 537 return UploadProgress(); | |
| 538 } | |
| 539 | |
| 540 HttpStream* WebSocketBasicHandshakeStream::RenewStreamForAuth() { | |
| 541 // Return null because we don't support renewing the stream. | |
| 542 return nullptr; | |
| 543 } | |
| 544 | |
| 545 scoped_ptr<WebSocketStream> WebSocketBasicHandshakeStream::Upgrade() { | |
| 546 // The HttpStreamParser object has a pointer to our ClientSocketHandle. Make | |
| 547 // sure it does not touch it again before it is destroyed. | |
| 548 state_.DeleteParser(); | |
| 549 WebSocketTransportClientSocketPool::UnlockEndpoint(state_.connection()); | |
| 550 scoped_ptr<WebSocketStream> basic_stream( | |
| 551 new WebSocketBasicStream(state_.ReleaseConnection(), | |
| 552 state_.read_buf(), | |
| 553 sub_protocol_, | |
| 554 extensions_)); | |
| 555 DCHECK(extension_params_.get()); | |
| 556 if (extension_params_->deflate_enabled) { | |
| 557 UMA_HISTOGRAM_ENUMERATION( | |
| 558 "Net.WebSocket.DeflateMode", | |
| 559 extension_params_->deflate_mode, | |
| 560 WebSocketDeflater::NUM_CONTEXT_TAKEOVER_MODE_TYPES); | |
| 561 | |
| 562 return scoped_ptr<WebSocketStream>( | |
| 563 new WebSocketDeflateStream(basic_stream.Pass(), | |
| 564 extension_params_->deflate_mode, | |
| 565 extension_params_->client_window_bits, | |
| 566 scoped_ptr<WebSocketDeflatePredictor>( | |
| 567 new WebSocketDeflatePredictorImpl))); | |
| 568 } else { | |
| 569 return basic_stream.Pass(); | |
| 570 } | |
| 571 } | |
| 572 | |
| 573 void WebSocketBasicHandshakeStream::SetWebSocketKeyForTesting( | |
| 574 const std::string& key) { | |
| 575 handshake_challenge_for_testing_.reset(new std::string(key)); | |
| 576 } | |
| 577 | |
| 578 void WebSocketBasicHandshakeStream::ReadResponseHeadersCallback( | |
| 579 const CompletionCallback& callback, | |
| 580 int result) { | |
| 581 bool is_redirect = false; | |
| 582 int rv = ValidateResponse(result, &is_redirect); | |
| 583 | |
| 584 // TODO(yhirano): Simplify this statement once http://crbug.com/399535 is | |
| 585 // fixed. | |
| 586 switch (rv) { | |
| 587 case OK: | |
| 588 RunCallbackWithOk(callback, rv); | |
| 589 break; | |
| 590 case ERR_INVALID_RESPONSE: | |
| 591 if (is_redirect) | |
| 592 RunCallbackWithInvalidResponseCausedByRedirect(callback, rv); | |
| 593 else | |
| 594 RunCallbackWithInvalidResponse(callback, rv); | |
| 595 break; | |
| 596 default: | |
| 597 RunCallback(callback, rv); | |
| 598 break; | |
| 599 } | |
| 600 } | |
| 601 | |
| 602 void WebSocketBasicHandshakeStream::OnFinishOpeningHandshake() { | |
| 603 DCHECK(http_response_info_); | |
| 604 WebSocketDispatchOnFinishOpeningHandshake(connect_delegate_, | |
| 605 url_, | |
| 606 http_response_info_->headers, | |
| 607 http_response_info_->response_time); | |
| 608 } | |
| 609 | |
| 610 int WebSocketBasicHandshakeStream::ValidateResponse(int rv, | |
| 611 bool* is_redirect) { | |
| 612 DCHECK(http_response_info_); | |
| 613 *is_redirect = false; | |
| 614 // Most net errors happen during connection, so they are not seen by this | |
| 615 // method. The histogram for error codes is created in | |
| 616 // Delegate::OnResponseStarted in websocket_stream.cc instead. | |
| 617 if (rv >= 0) { | |
| 618 const HttpResponseHeaders* headers = http_response_info_->headers.get(); | |
| 619 const int response_code = headers->response_code(); | |
| 620 *is_redirect = HttpResponseHeaders::IsRedirectResponseCode(response_code); | |
| 621 UMA_HISTOGRAM_SPARSE_SLOWLY("Net.WebSocket.ResponseCode", response_code); | |
| 622 switch (response_code) { | |
| 623 case HTTP_SWITCHING_PROTOCOLS: | |
| 624 OnFinishOpeningHandshake(); | |
| 625 return ValidateUpgradeResponse(headers); | |
| 626 | |
| 627 // We need to pass these through for authentication to work. | |
| 628 case HTTP_UNAUTHORIZED: | |
| 629 case HTTP_PROXY_AUTHENTICATION_REQUIRED: | |
| 630 return OK; | |
| 631 | |
| 632 // Other status codes are potentially risky (see the warnings in the | |
| 633 // WHATWG WebSocket API spec) and so are dropped by default. | |
| 634 default: | |
| 635 // A WebSocket server cannot be using HTTP/0.9, so if we see version | |
| 636 // 0.9, it means the response was garbage. | |
| 637 // Reporting "Unexpected response code: 200" in this case is not | |
| 638 // helpful, so use a different error message. | |
| 639 if (headers->GetHttpVersion() == HttpVersion(0, 9)) { | |
| 640 set_failure_message( | |
| 641 "Error during WebSocket handshake: Invalid status line"); | |
| 642 } else { | |
| 643 set_failure_message(base::StringPrintf( | |
| 644 "Error during WebSocket handshake: Unexpected response code: %d", | |
| 645 headers->response_code())); | |
| 646 } | |
| 647 OnFinishOpeningHandshake(); | |
| 648 return ERR_INVALID_RESPONSE; | |
| 649 } | |
| 650 } else { | |
| 651 if (rv == ERR_EMPTY_RESPONSE) { | |
| 652 set_failure_message( | |
| 653 "Connection closed before receiving a handshake response"); | |
| 654 return rv; | |
| 655 } | |
| 656 set_failure_message(std::string("Error during WebSocket handshake: ") + | |
| 657 ErrorToString(rv)); | |
| 658 OnFinishOpeningHandshake(); | |
| 659 // Some error codes (for example ERR_CONNECTION_CLOSED) get changed to OK at | |
| 660 // higher levels. To prevent an unvalidated connection getting erroneously | |
| 661 // upgraded, don't pass through the status code unchanged if it is | |
| 662 // HTTP_SWITCHING_PROTOCOLS. | |
| 663 if (http_response_info_->headers && | |
| 664 http_response_info_->headers->response_code() == | |
| 665 HTTP_SWITCHING_PROTOCOLS) { | |
| 666 http_response_info_->headers->ReplaceStatusLine( | |
| 667 kConnectionErrorStatusLine); | |
| 668 } | |
| 669 return rv; | |
| 670 } | |
| 671 } | |
| 672 | |
| 673 int WebSocketBasicHandshakeStream::ValidateUpgradeResponse( | |
| 674 const HttpResponseHeaders* headers) { | |
| 675 extension_params_.reset(new WebSocketExtensionParams); | |
| 676 std::string failure_message; | |
| 677 if (ValidateUpgrade(headers, &failure_message) && | |
| 678 ValidateSecWebSocketAccept( | |
| 679 headers, handshake_challenge_response_, &failure_message) && | |
| 680 ValidateConnection(headers, &failure_message) && | |
| 681 ValidateSubProtocol(headers, | |
| 682 requested_sub_protocols_, | |
| 683 &sub_protocol_, | |
| 684 &failure_message) && | |
| 685 ValidateExtensions(headers, | |
| 686 requested_extensions_, | |
| 687 &extensions_, | |
| 688 &failure_message, | |
| 689 extension_params_.get())) { | |
| 690 return OK; | |
| 691 } | |
| 692 set_failure_message("Error during WebSocket handshake: " + failure_message); | |
| 693 return ERR_INVALID_RESPONSE; | |
| 694 } | |
| 695 | |
| 696 void WebSocketBasicHandshakeStream::set_failure_message( | |
| 697 const std::string& failure_message) { | |
| 698 *failure_message_ = failure_message; | |
| 699 } | |
| 700 | |
| 701 } // namespace net | |
| OLD | NEW |