blob: cdb432edf4f767b042d719a0c85deb6ad7987d84 [file] [log] [blame]
[email protected]d51365e2013-11-27 10:46:521// 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>
[email protected]0be93922014-01-29 00:42:459#include <set>
[email protected]96868202014-01-09 10:38:0410#include <string>
[email protected]cd48ed12014-01-22 14:34:2211#include <vector>
[email protected]d51365e2013-11-27 10:46:5212
13#include "base/base64.h"
14#include "base/basictypes.h"
15#include "base/bind.h"
yhirano27b2b572014-10-30 11:23:4416#include "base/compiler_specific.h"
[email protected]d51365e2013-11-27 10:46:5217#include "base/containers/hash_tables.h"
[email protected]8aba0172014-07-03 12:09:5318#include "base/logging.h"
asvitkinec3c93722015-06-17 14:48:3719#include "base/metrics/histogram_macros.h"
[email protected]f7e98ca2014-06-19 12:05:4320#include "base/metrics/sparse_histogram.h"
[email protected]d51365e2013-11-27 10:46:5221#include "base/stl_util.h"
[email protected]0be93922014-01-29 00:42:4522#include "base/strings/string_number_conversions.h"
[email protected]69d7a492014-02-19 08:36:3223#include "base/strings/string_piece.h"
[email protected]d51365e2013-11-27 10:46:5224#include "base/strings/string_util.h"
[email protected]96868202014-01-09 10:38:0425#include "base/strings/stringprintf.h"
[email protected]cd48ed12014-01-22 14:34:2226#include "base/time/time.h"
[email protected]d51365e2013-11-27 10:46:5227#include "crypto/random.h"
halton.huo299e2002014-12-02 04:39:2428#include "net/base/io_buffer.h"
[email protected]d51365e2013-11-27 10:46:5229#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"
[email protected]654866142014-06-24 22:53:3136#include "net/socket/websocket_transport_client_socket_pool.h"
[email protected]d51365e2013-11-27 10:46:5237#include "net/websockets/websocket_basic_stream.h"
yhirano8387aee2015-09-14 05:46:4938#include "net/websockets/websocket_deflate_parameters.h"
[email protected]0be93922014-01-29 00:42:4539#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"
[email protected]96868202014-01-09 10:38:0443#include "net/websockets/websocket_extension_parser.h"
ricea11bdcd02014-11-20 09:57:0744#include "net/websockets/websocket_handshake_challenge.h"
[email protected]d51365e2013-11-27 10:46:5245#include "net/websockets/websocket_handshake_constants.h"
[email protected]cd48ed12014-01-22 14:34:2246#include "net/websockets/websocket_handshake_request_info.h"
47#include "net/websockets/websocket_handshake_response_info.h"
[email protected]d51365e2013-11-27 10:46:5248#include "net/websockets/websocket_stream.h"
49
50namespace net {
[email protected]0be93922014-01-29 00:42:4551
yhirano27b2b572014-10-30 11:23:4452namespace {
53
ricea23c3f942015-02-02 13:35:1354const char kConnectionErrorStatusLine[] = "HTTP/1.1 503 Connection Error";
55
yhirano27b2b572014-10-30 11:23:4456} // namespace
57
[email protected]0be93922014-01-29 00:42:4558// TODO(ricea): If more extensions are added, replace this with a more general
59// mechanism.
60struct WebSocketExtensionParams {
yhirano8387aee2015-09-14 05:46:4961 bool deflate_enabled = false;
62 WebSocketDeflateParameters deflate_parameters;
[email protected]0be93922014-01-29 00:42:4563};
64
[email protected]d51365e2013-11-27 10:46:5265namespace {
66
[email protected]96868202014-01-09 10:38:0467enum GetHeaderResult {
68 GET_HEADER_OK,
69 GET_HEADER_MISSING,
70 GET_HEADER_MULTIPLE,
71};
72
73std::string MissingHeaderMessage(const std::string& header_name) {
74 return std::string("'") + header_name + "' header is missing";
75}
76
77std::string MultipleHeaderValuesMessage(const std::string& header_name) {
78 return
79 std::string("'") +
80 header_name +
81 "' header must not appear more than once in a response";
82}
83
[email protected]d51365e2013-11-27 10:46:5284std::string GenerateHandshakeChallenge() {
85 std::string raw_challenge(websockets::kRawChallengeLength, '\0');
86 crypto::RandBytes(string_as_array(&raw_challenge), raw_challenge.length());
87 std::string encoded_challenge;
[email protected]33fca122013-12-11 01:48:5088 base::Base64Encode(raw_challenge, &encoded_challenge);
[email protected]d51365e2013-11-27 10:46:5289 return encoded_challenge;
90}
91
92void AddVectorHeaderIfNonEmpty(const char* name,
93 const std::vector<std::string>& value,
94 HttpRequestHeaders* headers) {
95 if (value.empty())
96 return;
brettwd94a22142015-07-15 05:19:2697 headers->SetHeader(name, base::JoinString(value, ", "));
[email protected]d51365e2013-11-27 10:46:5298}
99
[email protected]96868202014-01-09 10:38:04100GetHeaderResult GetSingleHeaderValue(const HttpResponseHeaders* headers,
101 const base::StringPiece& name,
102 std::string* value) {
yhiranoa7e05bb2014-11-06 05:40:39103 void* state = nullptr;
[email protected]96868202014-01-09 10:38:04104 size_t num_values = 0;
105 std::string temp_value;
106 while (headers->EnumerateHeader(&state, name, &temp_value)) {
107 if (++num_values > 1)
108 return GET_HEADER_MULTIPLE;
109 *value = temp_value;
[email protected]d51365e2013-11-27 10:46:52110 }
[email protected]96868202014-01-09 10:38:04111 return num_values > 0 ? GET_HEADER_OK : GET_HEADER_MISSING;
112}
113
114bool ValidateHeaderHasSingleValue(GetHeaderResult result,
115 const std::string& header_name,
116 std::string* failure_message) {
117 if (result == GET_HEADER_MISSING) {
118 *failure_message = MissingHeaderMessage(header_name);
119 return false;
120 }
121 if (result == GET_HEADER_MULTIPLE) {
122 *failure_message = MultipleHeaderValuesMessage(header_name);
123 return false;
124 }
125 DCHECK_EQ(result, GET_HEADER_OK);
126 return true;
127}
128
129bool ValidateUpgrade(const HttpResponseHeaders* headers,
130 std::string* failure_message) {
131 std::string value;
132 GetHeaderResult result =
133 GetSingleHeaderValue(headers, websockets::kUpgrade, &value);
134 if (!ValidateHeaderHasSingleValue(result,
135 websockets::kUpgrade,
136 failure_message)) {
137 return false;
138 }
139
brettwbc17d2c82015-06-09 22:39:08140 if (!base::LowerCaseEqualsASCII(value, websockets::kWebSocketLowercase)) {
[email protected]96868202014-01-09 10:38:04141 *failure_message =
142 "'Upgrade' header value is not 'WebSocket': " + value;
143 return false;
144 }
145 return true;
146}
147
148bool ValidateSecWebSocketAccept(const HttpResponseHeaders* headers,
149 const std::string& expected,
150 std::string* failure_message) {
151 std::string actual;
152 GetHeaderResult result =
153 GetSingleHeaderValue(headers, websockets::kSecWebSocketAccept, &actual);
154 if (!ValidateHeaderHasSingleValue(result,
155 websockets::kSecWebSocketAccept,
156 failure_message)) {
157 return false;
158 }
159
160 if (expected != actual) {
161 *failure_message = "Incorrect 'Sec-WebSocket-Accept' header value";
162 return false;
163 }
164 return true;
165}
166
167bool ValidateConnection(const HttpResponseHeaders* headers,
168 std::string* failure_message) {
169 // Connection header is permitted to contain other tokens.
170 if (!headers->HasHeader(HttpRequestHeaders::kConnection)) {
171 *failure_message = MissingHeaderMessage(HttpRequestHeaders::kConnection);
172 return false;
173 }
174 if (!headers->HasHeaderValue(HttpRequestHeaders::kConnection,
175 websockets::kUpgrade)) {
176 *failure_message = "'Connection' header value must contain 'Upgrade'";
177 return false;
178 }
179 return true;
[email protected]d51365e2013-11-27 10:46:52180}
181
182bool ValidateSubProtocol(
[email protected]96868202014-01-09 10:38:04183 const HttpResponseHeaders* headers,
[email protected]d51365e2013-11-27 10:46:52184 const std::vector<std::string>& requested_sub_protocols,
[email protected]96868202014-01-09 10:38:04185 std::string* sub_protocol,
186 std::string* failure_message) {
yhiranoa7e05bb2014-11-06 05:40:39187 void* state = nullptr;
[email protected]96868202014-01-09 10:38:04188 std::string value;
[email protected]d51365e2013-11-27 10:46:52189 base::hash_set<std::string> requested_set(requested_sub_protocols.begin(),
190 requested_sub_protocols.end());
[email protected]96868202014-01-09 10:38:04191 int count = 0;
192 bool has_multiple_protocols = false;
193 bool has_invalid_protocol = false;
[email protected]d51365e2013-11-27 10:46:52194
[email protected]96868202014-01-09 10:38:04195 while (!has_invalid_protocol || !has_multiple_protocols) {
196 std::string temp_value;
197 if (!headers->EnumerateHeader(
198 &state, websockets::kSecWebSocketProtocol, &temp_value))
199 break;
200 value = temp_value;
201 if (requested_set.count(value) == 0)
202 has_invalid_protocol = true;
203 if (++count > 1)
204 has_multiple_protocols = true;
[email protected]d51365e2013-11-27 10:46:52205 }
[email protected]96868202014-01-09 10:38:04206
207 if (has_multiple_protocols) {
208 *failure_message =
209 MultipleHeaderValuesMessage(websockets::kSecWebSocketProtocol);
210 return false;
211 } else if (count > 0 && requested_sub_protocols.size() == 0) {
212 *failure_message =
213 std::string("Response must not include 'Sec-WebSocket-Protocol' "
214 "header if not present in request: ")
215 + value;
216 return false;
217 } else if (has_invalid_protocol) {
218 *failure_message =
219 "'Sec-WebSocket-Protocol' header value '" +
220 value +
221 "' in response does not match any of sent values";
222 return false;
223 } else if (requested_sub_protocols.size() > 0 && count == 0) {
224 *failure_message =
225 "Sent non-empty 'Sec-WebSocket-Protocol' header "
226 "but no response was received";
227 return false;
228 }
229 *sub_protocol = value;
230 return true;
[email protected]d51365e2013-11-27 10:46:52231}
232
[email protected]96868202014-01-09 10:38:04233bool ValidateExtensions(const HttpResponseHeaders* headers,
tyoshino38ee68c2015-04-01 05:52:52234 std::string* accepted_extensions_descriptor,
[email protected]0be93922014-01-29 00:42:45235 std::string* failure_message,
236 WebSocketExtensionParams* params) {
yhiranoa7e05bb2014-11-06 05:40:39237 void* state = nullptr;
tyoshino38ee68c2015-04-01 05:52:52238 std::string header_value;
239 std::vector<std::string> header_values;
[email protected]0be93922014-01-29 00:42:45240 // TODO(ricea): If adding support for additional extensions, generalise this
241 // code.
242 bool seen_permessage_deflate = false;
tyoshino38ee68c2015-04-01 05:52:52243 while (headers->EnumerateHeader(&state, websockets::kSecWebSocketExtensions,
244 &header_value)) {
[email protected]96868202014-01-09 10:38:04245 WebSocketExtensionParser parser;
tyoshinod90d2932015-04-13 16:53:32246 if (!parser.Parse(header_value)) {
[email protected]96868202014-01-09 10:38:04247 // TODO(yhirano) Set appropriate failure message.
248 *failure_message =
249 "'Sec-WebSocket-Extensions' header value is "
250 "rejected by the parser: " +
tyoshino38ee68c2015-04-01 05:52:52251 header_value;
[email protected]96868202014-01-09 10:38:04252 return false;
253 }
tyoshino38ee68c2015-04-01 05:52:52254
255 const std::vector<WebSocketExtension>& extensions = parser.extensions();
256 for (const auto& extension : extensions) {
257 if (extension.name() == "permessage-deflate") {
258 if (seen_permessage_deflate) {
259 *failure_message = "Received duplicate permessage-deflate response";
260 return false;
261 }
262 seen_permessage_deflate = true;
yhirano8387aee2015-09-14 05:46:49263 auto& deflate_parameters = params->deflate_parameters;
264 if (!deflate_parameters.Initialize(extension, failure_message) ||
265 !deflate_parameters.IsValidAsResponse(failure_message)) {
266 *failure_message = "Error in permessage-deflate: " + *failure_message;
tyoshino38ee68c2015-04-01 05:52:52267 return false;
268 }
yhirano8387aee2015-09-14 05:46:49269 // Note that we don't have to check the request-response compatibility
270 // here because we send a request compatible with any valid responses.
271 // TODO(yhirano): Place a DCHECK here.
272
tyoshino38ee68c2015-04-01 05:52:52273 header_values.push_back(header_value);
274 } else {
275 *failure_message = "Found an unsupported extension '" +
276 extension.name() +
277 "' in 'Sec-WebSocket-Extensions' header";
[email protected]0be93922014-01-29 00:42:45278 return false;
279 }
[email protected]0be93922014-01-29 00:42:45280 }
[email protected]d51365e2013-11-27 10:46:52281 }
brettwd94a22142015-07-15 05:19:26282 *accepted_extensions_descriptor = base::JoinString(header_values, ", ");
yhirano8387aee2015-09-14 05:46:49283 params->deflate_enabled = seen_permessage_deflate;
[email protected]d51365e2013-11-27 10:46:52284 return true;
285}
286
287} // namespace
288
289WebSocketBasicHandshakeStream::WebSocketBasicHandshakeStream(
290 scoped_ptr<ClientSocketHandle> connection,
[email protected]cd48ed12014-01-22 14:34:22291 WebSocketStream::ConnectDelegate* connect_delegate,
[email protected]d51365e2013-11-27 10:46:52292 bool using_proxy,
293 std::vector<std::string> requested_sub_protocols,
[email protected]8aba0172014-07-03 12:09:53294 std::vector<std::string> requested_extensions,
295 std::string* failure_message)
[email protected]d51365e2013-11-27 10:46:52296 : state_(connection.release(), using_proxy),
[email protected]cd48ed12014-01-22 14:34:22297 connect_delegate_(connect_delegate),
yhiranoa7e05bb2014-11-06 05:40:39298 http_response_info_(nullptr),
[email protected]d51365e2013-11-27 10:46:52299 requested_sub_protocols_(requested_sub_protocols),
[email protected]8aba0172014-07-03 12:09:53300 requested_extensions_(requested_extensions),
301 failure_message_(failure_message) {
302 DCHECK(connect_delegate);
303 DCHECK(failure_message);
304}
[email protected]d51365e2013-11-27 10:46:52305
306WebSocketBasicHandshakeStream::~WebSocketBasicHandshakeStream() {}
307
308int WebSocketBasicHandshakeStream::InitializeStream(
309 const HttpRequestInfo* request_info,
310 RequestPriority priority,
311 const BoundNetLog& net_log,
312 const CompletionCallback& callback) {
[email protected]cd48ed12014-01-22 14:34:22313 url_ = request_info->url;
[email protected]d51365e2013-11-27 10:46:52314 state_.Initialize(request_info, priority, net_log, callback);
315 return OK;
316}
317
318int WebSocketBasicHandshakeStream::SendRequest(
319 const HttpRequestHeaders& headers,
320 HttpResponseInfo* response,
321 const CompletionCallback& callback) {
322 DCHECK(!headers.HasHeader(websockets::kSecWebSocketKey));
323 DCHECK(!headers.HasHeader(websockets::kSecWebSocketProtocol));
324 DCHECK(!headers.HasHeader(websockets::kSecWebSocketExtensions));
325 DCHECK(headers.HasHeader(HttpRequestHeaders::kOrigin));
326 DCHECK(headers.HasHeader(websockets::kUpgrade));
327 DCHECK(headers.HasHeader(HttpRequestHeaders::kConnection));
328 DCHECK(headers.HasHeader(websockets::kSecWebSocketVersion));
329 DCHECK(parser());
330
331 http_response_info_ = response;
332
333 // Create a copy of the headers object, so that we can add the
334 // Sec-WebSockey-Key header.
335 HttpRequestHeaders enriched_headers;
336 enriched_headers.CopyFrom(headers);
[email protected]a31ecc02013-12-05 08:30:55337 std::string handshake_challenge;
338 if (handshake_challenge_for_testing_) {
339 handshake_challenge = *handshake_challenge_for_testing_;
340 handshake_challenge_for_testing_.reset();
341 } else {
342 handshake_challenge = GenerateHandshakeChallenge();
343 }
[email protected]d51365e2013-11-27 10:46:52344 enriched_headers.SetHeader(websockets::kSecWebSocketKey, handshake_challenge);
345
[email protected]d51365e2013-11-27 10:46:52346 AddVectorHeaderIfNonEmpty(websockets::kSecWebSocketExtensions,
347 requested_extensions_,
348 &enriched_headers);
[email protected]0be93922014-01-29 00:42:45349 AddVectorHeaderIfNonEmpty(websockets::kSecWebSocketProtocol,
350 requested_sub_protocols_,
351 &enriched_headers);
[email protected]d51365e2013-11-27 10:46:52352
ricea11bdcd02014-11-20 09:57:07353 handshake_challenge_response_ =
354 ComputeSecWebSocketAccept(handshake_challenge);
[email protected]d51365e2013-11-27 10:46:52355
[email protected]cd48ed12014-01-22 14:34:22356 DCHECK(connect_delegate_);
357 scoped_ptr<WebSocketHandshakeRequestInfo> request(
358 new WebSocketHandshakeRequestInfo(url_, base::Time::Now()));
359 request->headers.CopyFrom(enriched_headers);
360 connect_delegate_->OnStartOpeningHandshake(request.Pass());
361
[email protected]d51365e2013-11-27 10:46:52362 return parser()->SendRequest(
363 state_.GenerateRequestLine(), enriched_headers, response, callback);
364}
365
366int WebSocketBasicHandshakeStream::ReadResponseHeaders(
367 const CompletionCallback& callback) {
368 // HttpStreamParser uses a weak pointer when reading from the
369 // socket, so it won't be called back after being destroyed. The
370 // HttpStreamParser is owned by HttpBasicState which is owned by this object,
371 // so this use of base::Unretained() is safe.
372 int rv = parser()->ReadResponseHeaders(
373 base::Bind(&WebSocketBasicHandshakeStream::ReadResponseHeadersCallback,
374 base::Unretained(this),
375 callback));
[email protected]cd48ed12014-01-22 14:34:22376 if (rv == ERR_IO_PENDING)
377 return rv;
ricea24c195f2015-02-26 12:18:55378 return ValidateResponse(rv);
[email protected]d51365e2013-11-27 10:46:52379}
380
[email protected]d51365e2013-11-27 10:46:52381int WebSocketBasicHandshakeStream::ReadResponseBody(
382 IOBuffer* buf,
383 int buf_len,
384 const CompletionCallback& callback) {
385 return parser()->ReadResponseBody(buf, buf_len, callback);
386}
387
388void WebSocketBasicHandshakeStream::Close(bool not_reusable) {
389 // This class ignores the value of |not_reusable| and never lets the socket be
390 // re-used.
391 if (parser())
392 parser()->Close(true);
393}
394
395bool WebSocketBasicHandshakeStream::IsResponseBodyComplete() const {
396 return parser()->IsResponseBodyComplete();
397}
398
[email protected]d51365e2013-11-27 10:46:52399bool WebSocketBasicHandshakeStream::IsConnectionReused() const {
400 return parser()->IsConnectionReused();
401}
402
403void WebSocketBasicHandshakeStream::SetConnectionReused() {
404 parser()->SetConnectionReused();
405}
406
mmenkebd84c392015-09-02 14:12:34407bool WebSocketBasicHandshakeStream::CanReuseConnection() const {
[email protected]d51365e2013-11-27 10:46:52408 return false;
409}
410
[email protected]bc92bc972013-12-13 08:32:59411int64 WebSocketBasicHandshakeStream::GetTotalReceivedBytes() const {
412 return 0;
413}
414
sclittlebe1ccf62015-09-02 19:40:36415int64_t WebSocketBasicHandshakeStream::GetTotalSentBytes() const {
416 return 0;
417}
418
[email protected]d51365e2013-11-27 10:46:52419bool WebSocketBasicHandshakeStream::GetLoadTimingInfo(
420 LoadTimingInfo* load_timing_info) const {
421 return state_.connection()->GetLoadTimingInfo(IsConnectionReused(),
422 load_timing_info);
423}
424
425void WebSocketBasicHandshakeStream::GetSSLInfo(SSLInfo* ssl_info) {
426 parser()->GetSSLInfo(ssl_info);
427}
428
429void WebSocketBasicHandshakeStream::GetSSLCertRequestInfo(
430 SSLCertRequestInfo* cert_request_info) {
431 parser()->GetSSLCertRequestInfo(cert_request_info);
432}
433
[email protected]d51365e2013-11-27 10:46:52434void WebSocketBasicHandshakeStream::Drain(HttpNetworkSession* session) {
435 HttpResponseBodyDrainer* drainer = new HttpResponseBodyDrainer(this);
436 drainer->Start(session);
437 // |drainer| will delete itself.
438}
439
440void WebSocketBasicHandshakeStream::SetPriority(RequestPriority priority) {
441 // TODO(ricea): See TODO comment in HttpBasicStream::SetPriority(). If it is
442 // gone, then copy whatever has happened there over here.
443}
444
yhiranoa7e05bb2014-11-06 05:40:39445UploadProgress WebSocketBasicHandshakeStream::GetUploadProgress() const {
446 return UploadProgress();
447}
448
449HttpStream* WebSocketBasicHandshakeStream::RenewStreamForAuth() {
450 // Return null because we don't support renewing the stream.
451 return nullptr;
452}
453
[email protected]d51365e2013-11-27 10:46:52454scoped_ptr<WebSocketStream> WebSocketBasicHandshakeStream::Upgrade() {
[email protected]d51365e2013-11-27 10:46:52455 // The HttpStreamParser object has a pointer to our ClientSocketHandle. Make
456 // sure it does not touch it again before it is destroyed.
457 state_.DeleteParser();
[email protected]654866142014-06-24 22:53:31458 WebSocketTransportClientSocketPool::UnlockEndpoint(state_.connection());
[email protected]0be93922014-01-29 00:42:45459 scoped_ptr<WebSocketStream> basic_stream(
[email protected]d51365e2013-11-27 10:46:52460 new WebSocketBasicStream(state_.ReleaseConnection(),
461 state_.read_buf(),
462 sub_protocol_,
463 extensions_));
[email protected]0be93922014-01-29 00:42:45464 DCHECK(extension_params_.get());
465 if (extension_params_->deflate_enabled) {
[email protected]9c50b042014-04-28 06:40:15466 UMA_HISTOGRAM_ENUMERATION(
467 "Net.WebSocket.DeflateMode",
yhirano8387aee2015-09-14 05:46:49468 extension_params_->deflate_parameters.client_context_take_over_mode(),
[email protected]9c50b042014-04-28 06:40:15469 WebSocketDeflater::NUM_CONTEXT_TAKEOVER_MODE_TYPES);
470
yhirano8387aee2015-09-14 05:46:49471 return scoped_ptr<WebSocketStream>(new WebSocketDeflateStream(
472 basic_stream.Pass(), extension_params_->deflate_parameters,
473 scoped_ptr<WebSocketDeflatePredictor>(
474 new WebSocketDeflatePredictorImpl)));
[email protected]0be93922014-01-29 00:42:45475 } else {
476 return basic_stream.Pass();
477 }
[email protected]d51365e2013-11-27 10:46:52478}
479
[email protected]a31ecc02013-12-05 08:30:55480void WebSocketBasicHandshakeStream::SetWebSocketKeyForTesting(
481 const std::string& key) {
482 handshake_challenge_for_testing_.reset(new std::string(key));
483}
484
[email protected]d51365e2013-11-27 10:46:52485void WebSocketBasicHandshakeStream::ReadResponseHeadersCallback(
486 const CompletionCallback& callback,
487 int result) {
ricea24c195f2015-02-26 12:18:55488 callback.Run(ValidateResponse(result));
[email protected]d51365e2013-11-27 10:46:52489}
490
[email protected]cd48ed12014-01-22 14:34:22491void WebSocketBasicHandshakeStream::OnFinishOpeningHandshake() {
[email protected]cd48ed12014-01-22 14:34:22492 DCHECK(http_response_info_);
[email protected]e69c1cd2014-07-29 07:42:29493 WebSocketDispatchOnFinishOpeningHandshake(connect_delegate_,
494 url_,
495 http_response_info_->headers,
496 http_response_info_->response_time);
[email protected]cd48ed12014-01-22 14:34:22497}
498
ricea24c195f2015-02-26 12:18:55499int WebSocketBasicHandshakeStream::ValidateResponse(int rv) {
[email protected]d51365e2013-11-27 10:46:52500 DCHECK(http_response_info_);
[email protected]f7e98ca2014-06-19 12:05:43501 // Most net errors happen during connection, so they are not seen by this
502 // method. The histogram for error codes is created in
503 // Delegate::OnResponseStarted in websocket_stream.cc instead.
[email protected]e5760f522014-02-05 12:28:50504 if (rv >= 0) {
[email protected]f7e98ca2014-06-19 12:05:43505 const HttpResponseHeaders* headers = http_response_info_->headers.get();
506 const int response_code = headers->response_code();
507 UMA_HISTOGRAM_SPARSE_SLOWLY("Net.WebSocket.ResponseCode", response_code);
508 switch (response_code) {
[email protected]e5760f522014-02-05 12:28:50509 case HTTP_SWITCHING_PROTOCOLS:
510 OnFinishOpeningHandshake();
511 return ValidateUpgradeResponse(headers);
[email protected]d51365e2013-11-27 10:46:52512
[email protected]e5760f522014-02-05 12:28:50513 // We need to pass these through for authentication to work.
514 case HTTP_UNAUTHORIZED:
515 case HTTP_PROXY_AUTHENTICATION_REQUIRED:
516 return OK;
[email protected]d51365e2013-11-27 10:46:52517
[email protected]e5760f522014-02-05 12:28:50518 // Other status codes are potentially risky (see the warnings in the
519 // WHATWG WebSocket API spec) and so are dropped by default.
520 default:
[email protected]aeb640d2014-02-21 11:03:18521 // A WebSocket server cannot be using HTTP/0.9, so if we see version
522 // 0.9, it means the response was garbage.
523 // Reporting "Unexpected response code: 200" in this case is not
524 // helpful, so use a different error message.
525 if (headers->GetHttpVersion() == HttpVersion(0, 9)) {
[email protected]8aba0172014-07-03 12:09:53526 set_failure_message(
527 "Error during WebSocket handshake: Invalid status line");
[email protected]aeb640d2014-02-21 11:03:18528 } else {
[email protected]8aba0172014-07-03 12:09:53529 set_failure_message(base::StringPrintf(
[email protected]aeb640d2014-02-21 11:03:18530 "Error during WebSocket handshake: Unexpected response code: %d",
[email protected]8aba0172014-07-03 12:09:53531 headers->response_code()));
[email protected]aeb640d2014-02-21 11:03:18532 }
[email protected]e5760f522014-02-05 12:28:50533 OnFinishOpeningHandshake();
534 return ERR_INVALID_RESPONSE;
535 }
536 } else {
[email protected]3efc08f2014-02-07 09:33:34537 if (rv == ERR_EMPTY_RESPONSE) {
[email protected]8aba0172014-07-03 12:09:53538 set_failure_message(
539 "Connection closed before receiving a handshake response");
[email protected]3efc08f2014-02-07 09:33:34540 return rv;
541 }
[email protected]8aba0172014-07-03 12:09:53542 set_failure_message(std::string("Error during WebSocket handshake: ") +
543 ErrorToString(rv));
[email protected]e5760f522014-02-05 12:28:50544 OnFinishOpeningHandshake();
ricea23c3f942015-02-02 13:35:13545 // Some error codes (for example ERR_CONNECTION_CLOSED) get changed to OK at
546 // higher levels. To prevent an unvalidated connection getting erroneously
547 // upgraded, don't pass through the status code unchanged if it is
548 // HTTP_SWITCHING_PROTOCOLS.
549 if (http_response_info_->headers &&
550 http_response_info_->headers->response_code() ==
551 HTTP_SWITCHING_PROTOCOLS) {
552 http_response_info_->headers->ReplaceStatusLine(
553 kConnectionErrorStatusLine);
554 }
[email protected]e5760f522014-02-05 12:28:50555 return rv;
[email protected]d51365e2013-11-27 10:46:52556 }
557}
558
559int WebSocketBasicHandshakeStream::ValidateUpgradeResponse(
[email protected]e5760f522014-02-05 12:28:50560 const HttpResponseHeaders* headers) {
[email protected]0be93922014-01-29 00:42:45561 extension_params_.reset(new WebSocketExtensionParams);
[email protected]8aba0172014-07-03 12:09:53562 std::string failure_message;
563 if (ValidateUpgrade(headers, &failure_message) &&
564 ValidateSecWebSocketAccept(
565 headers, handshake_challenge_response_, &failure_message) &&
566 ValidateConnection(headers, &failure_message) &&
[email protected]e5760f522014-02-05 12:28:50567 ValidateSubProtocol(headers,
[email protected]96868202014-01-09 10:38:04568 requested_sub_protocols_,
569 &sub_protocol_,
[email protected]8aba0172014-07-03 12:09:53570 &failure_message) &&
[email protected]e5760f522014-02-05 12:28:50571 ValidateExtensions(headers,
[email protected]96868202014-01-09 10:38:04572 &extensions_,
[email protected]8aba0172014-07-03 12:09:53573 &failure_message,
[email protected]0be93922014-01-29 00:42:45574 extension_params_.get())) {
[email protected]d51365e2013-11-27 10:46:52575 return OK;
576 }
[email protected]8aba0172014-07-03 12:09:53577 set_failure_message("Error during WebSocket handshake: " + failure_message);
[email protected]d51365e2013-11-27 10:46:52578 return ERR_INVALID_RESPONSE;
579}
580
[email protected]8aba0172014-07-03 12:09:53581void WebSocketBasicHandshakeStream::set_failure_message(
582 const std::string& failure_message) {
583 *failure_message_ = failure_message;
584}
585
[email protected]d51365e2013-11-27 10:46:52586} // namespace net