[Cronet] Delay StartNetLog and StopNetLog until native request context is initialized
[chromium-blink-merge.git] / net / websockets / websocket_basic_handshake_stream.cc
blobecddd50fcfc850c88df9cd69a953773c971b1066
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.
5 #include "net/websockets/websocket_basic_handshake_stream.h"
7 #include <algorithm>
8 #include <iterator>
9 #include <set>
10 #include <string>
11 #include <vector>
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"
49 namespace net {
51 namespace {
53 const char kConnectionErrorStatusLine[] = "HTTP/1.1 503 Connection Error";
55 } // namespace
57 // TODO(ricea): If more extensions are added, replace this with a more general
58 // mechanism.
59 struct WebSocketExtensionParams {
60 WebSocketExtensionParams()
61 : deflate_enabled(false),
62 client_window_bits(15),
63 deflate_mode(WebSocketDeflater::TAKE_OVER_CONTEXT) {}
65 bool deflate_enabled;
66 int client_window_bits;
67 WebSocketDeflater::ContextTakeOverMode deflate_mode;
70 namespace {
72 enum GetHeaderResult {
73 GET_HEADER_OK,
74 GET_HEADER_MISSING,
75 GET_HEADER_MULTIPLE,
78 std::string MissingHeaderMessage(const std::string& header_name) {
79 return std::string("'") + header_name + "' header is missing";
82 std::string MultipleHeaderValuesMessage(const std::string& header_name) {
83 return
84 std::string("'") +
85 header_name +
86 "' header must not appear more than once in a response";
89 std::string GenerateHandshakeChallenge() {
90 std::string raw_challenge(websockets::kRawChallengeLength, '\0');
91 crypto::RandBytes(string_as_array(&raw_challenge), raw_challenge.length());
92 std::string encoded_challenge;
93 base::Base64Encode(raw_challenge, &encoded_challenge);
94 return encoded_challenge;
97 void AddVectorHeaderIfNonEmpty(const char* name,
98 const std::vector<std::string>& value,
99 HttpRequestHeaders* headers) {
100 if (value.empty())
101 return;
102 headers->SetHeader(name, JoinString(value, ", "));
105 GetHeaderResult GetSingleHeaderValue(const HttpResponseHeaders* headers,
106 const base::StringPiece& name,
107 std::string* value) {
108 void* state = nullptr;
109 size_t num_values = 0;
110 std::string temp_value;
111 while (headers->EnumerateHeader(&state, name, &temp_value)) {
112 if (++num_values > 1)
113 return GET_HEADER_MULTIPLE;
114 *value = temp_value;
116 return num_values > 0 ? GET_HEADER_OK : GET_HEADER_MISSING;
119 bool ValidateHeaderHasSingleValue(GetHeaderResult result,
120 const std::string& header_name,
121 std::string* failure_message) {
122 if (result == GET_HEADER_MISSING) {
123 *failure_message = MissingHeaderMessage(header_name);
124 return false;
126 if (result == GET_HEADER_MULTIPLE) {
127 *failure_message = MultipleHeaderValuesMessage(header_name);
128 return false;
130 DCHECK_EQ(result, GET_HEADER_OK);
131 return true;
134 bool ValidateUpgrade(const HttpResponseHeaders* headers,
135 std::string* failure_message) {
136 std::string value;
137 GetHeaderResult result =
138 GetSingleHeaderValue(headers, websockets::kUpgrade, &value);
139 if (!ValidateHeaderHasSingleValue(result,
140 websockets::kUpgrade,
141 failure_message)) {
142 return false;
145 if (!LowerCaseEqualsASCII(value, websockets::kWebSocketLowercase)) {
146 *failure_message =
147 "'Upgrade' header value is not 'WebSocket': " + value;
148 return false;
150 return true;
153 bool ValidateSecWebSocketAccept(const HttpResponseHeaders* headers,
154 const std::string& expected,
155 std::string* failure_message) {
156 std::string actual;
157 GetHeaderResult result =
158 GetSingleHeaderValue(headers, websockets::kSecWebSocketAccept, &actual);
159 if (!ValidateHeaderHasSingleValue(result,
160 websockets::kSecWebSocketAccept,
161 failure_message)) {
162 return false;
165 if (expected != actual) {
166 *failure_message = "Incorrect 'Sec-WebSocket-Accept' header value";
167 return false;
169 return true;
172 bool ValidateConnection(const HttpResponseHeaders* headers,
173 std::string* failure_message) {
174 // Connection header is permitted to contain other tokens.
175 if (!headers->HasHeader(HttpRequestHeaders::kConnection)) {
176 *failure_message = MissingHeaderMessage(HttpRequestHeaders::kConnection);
177 return false;
179 if (!headers->HasHeaderValue(HttpRequestHeaders::kConnection,
180 websockets::kUpgrade)) {
181 *failure_message = "'Connection' header value must contain 'Upgrade'";
182 return false;
184 return true;
187 bool ValidateSubProtocol(
188 const HttpResponseHeaders* headers,
189 const std::vector<std::string>& requested_sub_protocols,
190 std::string* sub_protocol,
191 std::string* failure_message) {
192 void* state = nullptr;
193 std::string value;
194 base::hash_set<std::string> requested_set(requested_sub_protocols.begin(),
195 requested_sub_protocols.end());
196 int count = 0;
197 bool has_multiple_protocols = false;
198 bool has_invalid_protocol = false;
200 while (!has_invalid_protocol || !has_multiple_protocols) {
201 std::string temp_value;
202 if (!headers->EnumerateHeader(
203 &state, websockets::kSecWebSocketProtocol, &temp_value))
204 break;
205 value = temp_value;
206 if (requested_set.count(value) == 0)
207 has_invalid_protocol = true;
208 if (++count > 1)
209 has_multiple_protocols = true;
212 if (has_multiple_protocols) {
213 *failure_message =
214 MultipleHeaderValuesMessage(websockets::kSecWebSocketProtocol);
215 return false;
216 } else if (count > 0 && requested_sub_protocols.size() == 0) {
217 *failure_message =
218 std::string("Response must not include 'Sec-WebSocket-Protocol' "
219 "header if not present in request: ")
220 + value;
221 return false;
222 } else if (has_invalid_protocol) {
223 *failure_message =
224 "'Sec-WebSocket-Protocol' header value '" +
225 value +
226 "' in response does not match any of sent values";
227 return false;
228 } else if (requested_sub_protocols.size() > 0 && count == 0) {
229 *failure_message =
230 "Sent non-empty 'Sec-WebSocket-Protocol' header "
231 "but no response was received";
232 return false;
234 *sub_protocol = value;
235 return true;
238 bool DeflateError(std::string* message, const base::StringPiece& piece) {
239 *message = "Error in permessage-deflate: ";
240 piece.AppendToString(message);
241 return false;
244 bool ValidatePerMessageDeflateExtension(const WebSocketExtension& extension,
245 std::string* failure_message,
246 WebSocketExtensionParams* params) {
247 static const char kClientPrefix[] = "client_";
248 static const char kServerPrefix[] = "server_";
249 static const char kNoContextTakeover[] = "no_context_takeover";
250 static const char kMaxWindowBits[] = "max_window_bits";
251 const size_t kPrefixLen = arraysize(kClientPrefix) - 1;
252 static_assert(kPrefixLen == arraysize(kServerPrefix) - 1,
253 "the strings server and client must be the same length");
254 typedef std::vector<WebSocketExtension::Parameter> ParameterVector;
256 DCHECK_EQ("permessage-deflate", extension.name());
257 const ParameterVector& parameters = extension.parameters();
258 std::set<std::string> seen_names;
259 for (ParameterVector::const_iterator it = parameters.begin();
260 it != parameters.end(); ++it) {
261 const std::string& name = it->name();
262 if (seen_names.count(name) != 0) {
263 return DeflateError(
264 failure_message,
265 "Received duplicate permessage-deflate extension parameter " + name);
267 seen_names.insert(name);
268 const std::string client_or_server(name, 0, kPrefixLen);
269 const bool is_client = (client_or_server == kClientPrefix);
270 if (!is_client && client_or_server != kServerPrefix) {
271 return DeflateError(
272 failure_message,
273 "Received an unexpected permessage-deflate extension parameter");
275 const std::string rest(name, kPrefixLen);
276 if (rest == kNoContextTakeover) {
277 if (it->HasValue()) {
278 return DeflateError(failure_message,
279 "Received invalid " + name + " parameter");
281 if (is_client)
282 params->deflate_mode = WebSocketDeflater::DO_NOT_TAKE_OVER_CONTEXT;
283 } else if (rest == kMaxWindowBits) {
284 if (!it->HasValue())
285 return DeflateError(failure_message, name + " must have value");
286 int bits = 0;
287 if (!base::StringToInt(it->value(), &bits) || bits < 8 || bits > 15 ||
288 it->value()[0] == '0' ||
289 it->value().find_first_not_of("0123456789") != std::string::npos) {
290 return DeflateError(failure_message,
291 "Received invalid " + name + " parameter");
293 if (is_client)
294 params->client_window_bits = bits;
295 } else {
296 return DeflateError(
297 failure_message,
298 "Received an unexpected permessage-deflate extension parameter");
301 params->deflate_enabled = true;
302 return true;
305 bool ValidateExtensions(const HttpResponseHeaders* headers,
306 const std::vector<std::string>& requested_extensions,
307 std::string* extensions,
308 std::string* failure_message,
309 WebSocketExtensionParams* params) {
310 void* state = nullptr;
311 std::string value;
312 std::vector<std::string> accepted_extensions;
313 // TODO(ricea): If adding support for additional extensions, generalise this
314 // code.
315 bool seen_permessage_deflate = false;
316 while (headers->EnumerateHeader(
317 &state, websockets::kSecWebSocketExtensions, &value)) {
318 WebSocketExtensionParser parser;
319 parser.Parse(value);
320 if (parser.has_error()) {
321 // TODO(yhirano) Set appropriate failure message.
322 *failure_message =
323 "'Sec-WebSocket-Extensions' header value is "
324 "rejected by the parser: " +
325 value;
326 return false;
328 if (parser.extension().name() == "permessage-deflate") {
329 if (seen_permessage_deflate) {
330 *failure_message = "Received duplicate permessage-deflate response";
331 return false;
333 seen_permessage_deflate = true;
334 if (!ValidatePerMessageDeflateExtension(
335 parser.extension(), failure_message, params))
336 return false;
337 } else {
338 *failure_message =
339 "Found an unsupported extension '" +
340 parser.extension().name() +
341 "' in 'Sec-WebSocket-Extensions' header";
342 return false;
344 accepted_extensions.push_back(value);
346 *extensions = JoinString(accepted_extensions, ", ");
347 return true;
350 } // namespace
352 WebSocketBasicHandshakeStream::WebSocketBasicHandshakeStream(
353 scoped_ptr<ClientSocketHandle> connection,
354 WebSocketStream::ConnectDelegate* connect_delegate,
355 bool using_proxy,
356 std::vector<std::string> requested_sub_protocols,
357 std::vector<std::string> requested_extensions,
358 std::string* failure_message)
359 : state_(connection.release(), using_proxy),
360 connect_delegate_(connect_delegate),
361 http_response_info_(nullptr),
362 requested_sub_protocols_(requested_sub_protocols),
363 requested_extensions_(requested_extensions),
364 failure_message_(failure_message) {
365 DCHECK(connect_delegate);
366 DCHECK(failure_message);
369 WebSocketBasicHandshakeStream::~WebSocketBasicHandshakeStream() {}
371 int WebSocketBasicHandshakeStream::InitializeStream(
372 const HttpRequestInfo* request_info,
373 RequestPriority priority,
374 const BoundNetLog& net_log,
375 const CompletionCallback& callback) {
376 url_ = request_info->url;
377 state_.Initialize(request_info, priority, net_log, callback);
378 return OK;
381 int WebSocketBasicHandshakeStream::SendRequest(
382 const HttpRequestHeaders& headers,
383 HttpResponseInfo* response,
384 const CompletionCallback& callback) {
385 DCHECK(!headers.HasHeader(websockets::kSecWebSocketKey));
386 DCHECK(!headers.HasHeader(websockets::kSecWebSocketProtocol));
387 DCHECK(!headers.HasHeader(websockets::kSecWebSocketExtensions));
388 DCHECK(headers.HasHeader(HttpRequestHeaders::kOrigin));
389 DCHECK(headers.HasHeader(websockets::kUpgrade));
390 DCHECK(headers.HasHeader(HttpRequestHeaders::kConnection));
391 DCHECK(headers.HasHeader(websockets::kSecWebSocketVersion));
392 DCHECK(parser());
394 http_response_info_ = response;
396 // Create a copy of the headers object, so that we can add the
397 // Sec-WebSockey-Key header.
398 HttpRequestHeaders enriched_headers;
399 enriched_headers.CopyFrom(headers);
400 std::string handshake_challenge;
401 if (handshake_challenge_for_testing_) {
402 handshake_challenge = *handshake_challenge_for_testing_;
403 handshake_challenge_for_testing_.reset();
404 } else {
405 handshake_challenge = GenerateHandshakeChallenge();
407 enriched_headers.SetHeader(websockets::kSecWebSocketKey, handshake_challenge);
409 AddVectorHeaderIfNonEmpty(websockets::kSecWebSocketExtensions,
410 requested_extensions_,
411 &enriched_headers);
412 AddVectorHeaderIfNonEmpty(websockets::kSecWebSocketProtocol,
413 requested_sub_protocols_,
414 &enriched_headers);
416 handshake_challenge_response_ =
417 ComputeSecWebSocketAccept(handshake_challenge);
419 DCHECK(connect_delegate_);
420 scoped_ptr<WebSocketHandshakeRequestInfo> request(
421 new WebSocketHandshakeRequestInfo(url_, base::Time::Now()));
422 request->headers.CopyFrom(enriched_headers);
423 connect_delegate_->OnStartOpeningHandshake(request.Pass());
425 return parser()->SendRequest(
426 state_.GenerateRequestLine(), enriched_headers, response, callback);
429 int WebSocketBasicHandshakeStream::ReadResponseHeaders(
430 const CompletionCallback& callback) {
431 // HttpStreamParser uses a weak pointer when reading from the
432 // socket, so it won't be called back after being destroyed. The
433 // HttpStreamParser is owned by HttpBasicState which is owned by this object,
434 // so this use of base::Unretained() is safe.
435 int rv = parser()->ReadResponseHeaders(
436 base::Bind(&WebSocketBasicHandshakeStream::ReadResponseHeadersCallback,
437 base::Unretained(this),
438 callback));
439 if (rv == ERR_IO_PENDING)
440 return rv;
441 return ValidateResponse(rv);
444 int WebSocketBasicHandshakeStream::ReadResponseBody(
445 IOBuffer* buf,
446 int buf_len,
447 const CompletionCallback& callback) {
448 return parser()->ReadResponseBody(buf, buf_len, callback);
451 void WebSocketBasicHandshakeStream::Close(bool not_reusable) {
452 // This class ignores the value of |not_reusable| and never lets the socket be
453 // re-used.
454 if (parser())
455 parser()->Close(true);
458 bool WebSocketBasicHandshakeStream::IsResponseBodyComplete() const {
459 return parser()->IsResponseBodyComplete();
462 bool WebSocketBasicHandshakeStream::CanFindEndOfResponse() const {
463 return parser() && parser()->CanFindEndOfResponse();
466 bool WebSocketBasicHandshakeStream::IsConnectionReused() const {
467 return parser()->IsConnectionReused();
470 void WebSocketBasicHandshakeStream::SetConnectionReused() {
471 parser()->SetConnectionReused();
474 bool WebSocketBasicHandshakeStream::IsConnectionReusable() const {
475 return false;
478 int64 WebSocketBasicHandshakeStream::GetTotalReceivedBytes() const {
479 return 0;
482 bool WebSocketBasicHandshakeStream::GetLoadTimingInfo(
483 LoadTimingInfo* load_timing_info) const {
484 return state_.connection()->GetLoadTimingInfo(IsConnectionReused(),
485 load_timing_info);
488 void WebSocketBasicHandshakeStream::GetSSLInfo(SSLInfo* ssl_info) {
489 parser()->GetSSLInfo(ssl_info);
492 void WebSocketBasicHandshakeStream::GetSSLCertRequestInfo(
493 SSLCertRequestInfo* cert_request_info) {
494 parser()->GetSSLCertRequestInfo(cert_request_info);
497 bool WebSocketBasicHandshakeStream::IsSpdyHttpStream() const { return false; }
499 void WebSocketBasicHandshakeStream::Drain(HttpNetworkSession* session) {
500 HttpResponseBodyDrainer* drainer = new HttpResponseBodyDrainer(this);
501 drainer->Start(session);
502 // |drainer| will delete itself.
505 void WebSocketBasicHandshakeStream::SetPriority(RequestPriority priority) {
506 // TODO(ricea): See TODO comment in HttpBasicStream::SetPriority(). If it is
507 // gone, then copy whatever has happened there over here.
510 UploadProgress WebSocketBasicHandshakeStream::GetUploadProgress() const {
511 return UploadProgress();
514 HttpStream* WebSocketBasicHandshakeStream::RenewStreamForAuth() {
515 // Return null because we don't support renewing the stream.
516 return nullptr;
519 scoped_ptr<WebSocketStream> WebSocketBasicHandshakeStream::Upgrade() {
520 // The HttpStreamParser object has a pointer to our ClientSocketHandle. Make
521 // sure it does not touch it again before it is destroyed.
522 state_.DeleteParser();
523 WebSocketTransportClientSocketPool::UnlockEndpoint(state_.connection());
524 scoped_ptr<WebSocketStream> basic_stream(
525 new WebSocketBasicStream(state_.ReleaseConnection(),
526 state_.read_buf(),
527 sub_protocol_,
528 extensions_));
529 DCHECK(extension_params_.get());
530 if (extension_params_->deflate_enabled) {
531 UMA_HISTOGRAM_ENUMERATION(
532 "Net.WebSocket.DeflateMode",
533 extension_params_->deflate_mode,
534 WebSocketDeflater::NUM_CONTEXT_TAKEOVER_MODE_TYPES);
536 return scoped_ptr<WebSocketStream>(
537 new WebSocketDeflateStream(basic_stream.Pass(),
538 extension_params_->deflate_mode,
539 extension_params_->client_window_bits,
540 scoped_ptr<WebSocketDeflatePredictor>(
541 new WebSocketDeflatePredictorImpl)));
542 } else {
543 return basic_stream.Pass();
547 void WebSocketBasicHandshakeStream::SetWebSocketKeyForTesting(
548 const std::string& key) {
549 handshake_challenge_for_testing_.reset(new std::string(key));
552 void WebSocketBasicHandshakeStream::ReadResponseHeadersCallback(
553 const CompletionCallback& callback,
554 int result) {
555 callback.Run(ValidateResponse(result));
558 void WebSocketBasicHandshakeStream::OnFinishOpeningHandshake() {
559 DCHECK(http_response_info_);
560 WebSocketDispatchOnFinishOpeningHandshake(connect_delegate_,
561 url_,
562 http_response_info_->headers,
563 http_response_info_->response_time);
566 int WebSocketBasicHandshakeStream::ValidateResponse(int rv) {
567 DCHECK(http_response_info_);
568 // Most net errors happen during connection, so they are not seen by this
569 // method. The histogram for error codes is created in
570 // Delegate::OnResponseStarted in websocket_stream.cc instead.
571 if (rv >= 0) {
572 const HttpResponseHeaders* headers = http_response_info_->headers.get();
573 const int response_code = headers->response_code();
574 UMA_HISTOGRAM_SPARSE_SLOWLY("Net.WebSocket.ResponseCode", response_code);
575 switch (response_code) {
576 case HTTP_SWITCHING_PROTOCOLS:
577 OnFinishOpeningHandshake();
578 return ValidateUpgradeResponse(headers);
580 // We need to pass these through for authentication to work.
581 case HTTP_UNAUTHORIZED:
582 case HTTP_PROXY_AUTHENTICATION_REQUIRED:
583 return OK;
585 // Other status codes are potentially risky (see the warnings in the
586 // WHATWG WebSocket API spec) and so are dropped by default.
587 default:
588 // A WebSocket server cannot be using HTTP/0.9, so if we see version
589 // 0.9, it means the response was garbage.
590 // Reporting "Unexpected response code: 200" in this case is not
591 // helpful, so use a different error message.
592 if (headers->GetHttpVersion() == HttpVersion(0, 9)) {
593 set_failure_message(
594 "Error during WebSocket handshake: Invalid status line");
595 } else {
596 set_failure_message(base::StringPrintf(
597 "Error during WebSocket handshake: Unexpected response code: %d",
598 headers->response_code()));
600 OnFinishOpeningHandshake();
601 return ERR_INVALID_RESPONSE;
603 } else {
604 if (rv == ERR_EMPTY_RESPONSE) {
605 set_failure_message(
606 "Connection closed before receiving a handshake response");
607 return rv;
609 set_failure_message(std::string("Error during WebSocket handshake: ") +
610 ErrorToString(rv));
611 OnFinishOpeningHandshake();
612 // Some error codes (for example ERR_CONNECTION_CLOSED) get changed to OK at
613 // higher levels. To prevent an unvalidated connection getting erroneously
614 // upgraded, don't pass through the status code unchanged if it is
615 // HTTP_SWITCHING_PROTOCOLS.
616 if (http_response_info_->headers &&
617 http_response_info_->headers->response_code() ==
618 HTTP_SWITCHING_PROTOCOLS) {
619 http_response_info_->headers->ReplaceStatusLine(
620 kConnectionErrorStatusLine);
622 return rv;
626 int WebSocketBasicHandshakeStream::ValidateUpgradeResponse(
627 const HttpResponseHeaders* headers) {
628 extension_params_.reset(new WebSocketExtensionParams);
629 std::string failure_message;
630 if (ValidateUpgrade(headers, &failure_message) &&
631 ValidateSecWebSocketAccept(
632 headers, handshake_challenge_response_, &failure_message) &&
633 ValidateConnection(headers, &failure_message) &&
634 ValidateSubProtocol(headers,
635 requested_sub_protocols_,
636 &sub_protocol_,
637 &failure_message) &&
638 ValidateExtensions(headers,
639 requested_extensions_,
640 &extensions_,
641 &failure_message,
642 extension_params_.get())) {
643 return OK;
645 set_failure_message("Error during WebSocket handshake: " + failure_message);
646 return ERR_INVALID_RESPONSE;
649 void WebSocketBasicHandshakeStream::set_failure_message(
650 const std::string& failure_message) {
651 *failure_message_ = failure_message;
654 } // namespace net