blob: fb86f5de49bdaed91104ee0ad499537f3ec52cff [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
tfarinaea94afc232015-10-20 04:23:367#include <stddef.h>
[email protected]d51365e2013-11-27 10:46:528#include <algorithm>
9#include <iterator>
[email protected]0be93922014-01-29 00:42:4510#include <set>
[email protected]96868202014-01-09 10:38:0411#include <string>
dchengc7eeda422015-12-26 03:56:4812#include <utility>
[email protected]cd48ed12014-01-22 14:34:2213#include <vector>
[email protected]d51365e2013-11-27 10:46:5214
15#include "base/base64.h"
[email protected]d51365e2013-11-27 10:46:5216#include "base/bind.h"
yhirano27b2b572014-10-30 11:23:4417#include "base/compiler_specific.h"
[email protected]8aba0172014-07-03 12:09:5318#include "base/logging.h"
Ilya Sherman0eb39802017-12-08 20:58:1819#include "base/metrics/histogram_functions.h"
[email protected]d51365e2013-11-27 10:46:5220#include "base/stl_util.h"
[email protected]0be93922014-01-29 00:42:4521#include "base/strings/string_number_conversions.h"
[email protected]69d7a492014-02-19 08:36:3222#include "base/strings/string_piece.h"
[email protected]d51365e2013-11-27 10:46:5223#include "base/strings/string_util.h"
[email protected]96868202014-01-09 10:38:0424#include "base/strings/stringprintf.h"
[email protected]cd48ed12014-01-22 14:34:2225#include "base/time/time.h"
[email protected]d51365e2013-11-27 10:46:5226#include "crypto/random.h"
halton.huo299e2002014-12-02 04:39:2427#include "net/base/io_buffer.h"
[email protected]d51365e2013-11-27 10:46:5228#include "net/http/http_request_headers.h"
29#include "net/http/http_request_info.h"
30#include "net/http/http_response_body_drainer.h"
31#include "net/http/http_response_headers.h"
32#include "net/http/http_status_code.h"
33#include "net/http/http_stream_parser.h"
34#include "net/socket/client_socket_handle.h"
Bence Béky3cb271d2018-03-29 22:00:4835#include "net/socket/ssl_client_socket.h"
Bence Békyda280c62018-04-12 15:08:3736#include "net/socket/websocket_endpoint_lock_manager.h"
[email protected]654866142014-06-24 22:53:3137#include "net/socket/websocket_transport_client_socket_pool.h"
[email protected]d51365e2013-11-27 10:46:5238#include "net/websockets/websocket_basic_stream.h"
Bence Béky7294fc22018-02-08 14:26:1739#include "net/websockets/websocket_basic_stream_adapters.h"
yhirano8387aee2015-09-14 05:46:4940#include "net/websockets/websocket_deflate_parameters.h"
[email protected]0be93922014-01-29 00:42:4541#include "net/websockets/websocket_deflate_predictor.h"
42#include "net/websockets/websocket_deflate_predictor_impl.h"
43#include "net/websockets/websocket_deflate_stream.h"
44#include "net/websockets/websocket_deflater.h"
ricea11bdcd02014-11-20 09:57:0745#include "net/websockets/websocket_handshake_challenge.h"
[email protected]d51365e2013-11-27 10:46:5246#include "net/websockets/websocket_handshake_constants.h"
[email protected]cd48ed12014-01-22 14:34:2247#include "net/websockets/websocket_handshake_request_info.h"
48#include "net/websockets/websocket_handshake_response_info.h"
[email protected]d51365e2013-11-27 10:46:5249#include "net/websockets/websocket_stream.h"
50
51namespace net {
[email protected]0be93922014-01-29 00:42:4552
yhirano27b2b572014-10-30 11:23:4453namespace {
54
ricea23c3f942015-02-02 13:35:1355const char kConnectionErrorStatusLine[] = "HTTP/1.1 503 Connection Error";
56
yhirano27b2b572014-10-30 11:23:4457} // namespace
58
[email protected]d51365e2013-11-27 10:46:5259namespace {
60
[email protected]96868202014-01-09 10:38:0461enum GetHeaderResult {
62 GET_HEADER_OK,
63 GET_HEADER_MISSING,
64 GET_HEADER_MULTIPLE,
65};
66
67std::string MissingHeaderMessage(const std::string& header_name) {
68 return std::string("'") + header_name + "' header is missing";
69}
70
[email protected]d51365e2013-11-27 10:46:5271std::string GenerateHandshakeChallenge() {
72 std::string raw_challenge(websockets::kRawChallengeLength, '\0');
Ryan Sleevi972b2ff2018-05-14 15:45:1073 crypto::RandBytes(base::data(raw_challenge), raw_challenge.length());
[email protected]d51365e2013-11-27 10:46:5274 std::string encoded_challenge;
[email protected]33fca122013-12-11 01:48:5075 base::Base64Encode(raw_challenge, &encoded_challenge);
[email protected]d51365e2013-11-27 10:46:5276 return encoded_challenge;
77}
78
[email protected]96868202014-01-09 10:38:0479GetHeaderResult GetSingleHeaderValue(const HttpResponseHeaders* headers,
80 const base::StringPiece& name,
81 std::string* value) {
olli.raulaee489a52016-01-25 08:37:1082 size_t iter = 0;
[email protected]96868202014-01-09 10:38:0483 size_t num_values = 0;
84 std::string temp_value;
olli.raulaee489a52016-01-25 08:37:1085 while (headers->EnumerateHeader(&iter, name, &temp_value)) {
[email protected]96868202014-01-09 10:38:0486 if (++num_values > 1)
87 return GET_HEADER_MULTIPLE;
88 *value = temp_value;
[email protected]d51365e2013-11-27 10:46:5289 }
[email protected]96868202014-01-09 10:38:0490 return num_values > 0 ? GET_HEADER_OK : GET_HEADER_MISSING;
91}
92
93bool ValidateHeaderHasSingleValue(GetHeaderResult result,
94 const std::string& header_name,
95 std::string* failure_message) {
96 if (result == GET_HEADER_MISSING) {
97 *failure_message = MissingHeaderMessage(header_name);
98 return false;
99 }
100 if (result == GET_HEADER_MULTIPLE) {
Bence Békyb28709c22018-03-06 13:03:44101 *failure_message =
102 WebSocketHandshakeStreamBase::MultipleHeaderValuesMessage(header_name);
[email protected]96868202014-01-09 10:38:04103 return false;
104 }
105 DCHECK_EQ(result, GET_HEADER_OK);
106 return true;
107}
108
109bool ValidateUpgrade(const HttpResponseHeaders* headers,
110 std::string* failure_message) {
111 std::string value;
112 GetHeaderResult result =
113 GetSingleHeaderValue(headers, websockets::kUpgrade, &value);
114 if (!ValidateHeaderHasSingleValue(result,
115 websockets::kUpgrade,
116 failure_message)) {
117 return false;
118 }
119
brettwbc17d2c82015-06-09 22:39:08120 if (!base::LowerCaseEqualsASCII(value, websockets::kWebSocketLowercase)) {
[email protected]96868202014-01-09 10:38:04121 *failure_message =
122 "'Upgrade' header value is not 'WebSocket': " + value;
123 return false;
124 }
125 return true;
126}
127
128bool ValidateSecWebSocketAccept(const HttpResponseHeaders* headers,
129 const std::string& expected,
130 std::string* failure_message) {
131 std::string actual;
132 GetHeaderResult result =
133 GetSingleHeaderValue(headers, websockets::kSecWebSocketAccept, &actual);
134 if (!ValidateHeaderHasSingleValue(result,
135 websockets::kSecWebSocketAccept,
136 failure_message)) {
137 return false;
138 }
139
140 if (expected != actual) {
141 *failure_message = "Incorrect 'Sec-WebSocket-Accept' header value";
142 return false;
143 }
144 return true;
145}
146
147bool ValidateConnection(const HttpResponseHeaders* headers,
148 std::string* failure_message) {
149 // Connection header is permitted to contain other tokens.
150 if (!headers->HasHeader(HttpRequestHeaders::kConnection)) {
151 *failure_message = MissingHeaderMessage(HttpRequestHeaders::kConnection);
152 return false;
153 }
154 if (!headers->HasHeaderValue(HttpRequestHeaders::kConnection,
155 websockets::kUpgrade)) {
156 *failure_message = "'Connection' header value must contain 'Upgrade'";
157 return false;
158 }
159 return true;
[email protected]d51365e2013-11-27 10:46:52160}
161
[email protected]d51365e2013-11-27 10:46:52162} // namespace
163
Adam Ricec786ad8a2018-05-22 09:49:15164const base::Feature
165 WebSocketBasicHandshakeStream::kWebSocketHandshakeReuseConnection{
166 "WebSocketHandshakeReuseConnection", base::FEATURE_DISABLED_BY_DEFAULT};
167
[email protected]d51365e2013-11-27 10:46:52168WebSocketBasicHandshakeStream::WebSocketBasicHandshakeStream(
danakj9c5cab52016-04-16 00:54:33169 std::unique_ptr<ClientSocketHandle> connection,
[email protected]cd48ed12014-01-22 14:34:22170 WebSocketStream::ConnectDelegate* connect_delegate,
[email protected]d51365e2013-11-27 10:46:52171 bool using_proxy,
172 std::vector<std::string> requested_sub_protocols,
[email protected]8aba0172014-07-03 12:09:53173 std::vector<std::string> requested_extensions,
Bence Békyda280c62018-04-12 15:08:37174 WebSocketStreamRequest* request,
175 WebSocketEndpointLockManager* websocket_endpoint_lock_manager)
Bence Békyde0be312018-03-13 17:51:58176 : result_(HandshakeResult::INCOMPLETE),
177 state_(std::move(connection),
mmenkea7da6da2016-09-01 21:56:52178 using_proxy,
179 false /* http_09_on_non_default_ports_enabled */),
[email protected]cd48ed12014-01-22 14:34:22180 connect_delegate_(connect_delegate),
yhiranoa7e05bb2014-11-06 05:40:39181 http_response_info_(nullptr),
[email protected]d51365e2013-11-27 10:46:52182 requested_sub_protocols_(requested_sub_protocols),
[email protected]8aba0172014-07-03 12:09:53183 requested_extensions_(requested_extensions),
Bence Békyda280c62018-04-12 15:08:37184 stream_request_(request),
185 websocket_endpoint_lock_manager_(websocket_endpoint_lock_manager) {
[email protected]8aba0172014-07-03 12:09:53186 DCHECK(connect_delegate);
tyoshinoccfcfde2016-07-21 14:06:55187 DCHECK(request);
[email protected]8aba0172014-07-03 12:09:53188}
[email protected]d51365e2013-11-27 10:46:52189
Bence Békyde0be312018-03-13 17:51:58190WebSocketBasicHandshakeStream::~WebSocketBasicHandshakeStream() {
191 RecordHandshakeResult(result_);
192}
[email protected]d51365e2013-11-27 10:46:52193
194int WebSocketBasicHandshakeStream::InitializeStream(
195 const HttpRequestInfo* request_info,
Steven Valdezb4ff0412018-01-18 22:39:27196 bool can_send_early,
[email protected]d51365e2013-11-27 10:46:52197 RequestPriority priority,
tfarina42834112016-09-22 13:38:20198 const NetLogWithSource& net_log,
Bence Békya25e3f72018-02-13 21:13:39199 CompletionOnceCallback callback) {
Ramin Halavati20e949f2018-02-14 20:14:32200 DCHECK(request_info->traffic_annotation.is_valid());
[email protected]cd48ed12014-01-22 14:34:22201 url_ = request_info->url;
Bence Békyd5d65a912018-02-11 04:28:00202 state_.Initialize(request_info, can_send_early, priority, net_log);
[email protected]d51365e2013-11-27 10:46:52203 return OK;
204}
205
206int WebSocketBasicHandshakeStream::SendRequest(
207 const HttpRequestHeaders& headers,
208 HttpResponseInfo* response,
Bence Békya25e3f72018-02-13 21:13:39209 CompletionOnceCallback callback) {
[email protected]d51365e2013-11-27 10:46:52210 DCHECK(!headers.HasHeader(websockets::kSecWebSocketKey));
211 DCHECK(!headers.HasHeader(websockets::kSecWebSocketProtocol));
212 DCHECK(!headers.HasHeader(websockets::kSecWebSocketExtensions));
213 DCHECK(headers.HasHeader(HttpRequestHeaders::kOrigin));
214 DCHECK(headers.HasHeader(websockets::kUpgrade));
215 DCHECK(headers.HasHeader(HttpRequestHeaders::kConnection));
216 DCHECK(headers.HasHeader(websockets::kSecWebSocketVersion));
217 DCHECK(parser());
218
219 http_response_info_ = response;
220
221 // Create a copy of the headers object, so that we can add the
222 // Sec-WebSockey-Key header.
223 HttpRequestHeaders enriched_headers;
224 enriched_headers.CopyFrom(headers);
[email protected]a31ecc02013-12-05 08:30:55225 std::string handshake_challenge;
Bence Béky7d0c74d2018-03-05 08:31:09226 if (handshake_challenge_for_testing_.has_value()) {
227 handshake_challenge = handshake_challenge_for_testing_.value();
[email protected]a31ecc02013-12-05 08:30:55228 handshake_challenge_for_testing_.reset();
229 } else {
230 handshake_challenge = GenerateHandshakeChallenge();
231 }
[email protected]d51365e2013-11-27 10:46:52232 enriched_headers.SetHeader(websockets::kSecWebSocketKey, handshake_challenge);
233
[email protected]d51365e2013-11-27 10:46:52234 AddVectorHeaderIfNonEmpty(websockets::kSecWebSocketExtensions,
235 requested_extensions_,
236 &enriched_headers);
[email protected]0be93922014-01-29 00:42:45237 AddVectorHeaderIfNonEmpty(websockets::kSecWebSocketProtocol,
238 requested_sub_protocols_,
239 &enriched_headers);
[email protected]d51365e2013-11-27 10:46:52240
ricea11bdcd02014-11-20 09:57:07241 handshake_challenge_response_ =
242 ComputeSecWebSocketAccept(handshake_challenge);
[email protected]d51365e2013-11-27 10:46:52243
[email protected]cd48ed12014-01-22 14:34:22244 DCHECK(connect_delegate_);
Bence Béky65623972018-03-05 15:31:56245 auto request =
246 std::make_unique<WebSocketHandshakeRequestInfo>(url_, base::Time::Now());
[email protected]cd48ed12014-01-22 14:34:22247 request->headers.CopyFrom(enriched_headers);
dchengc7eeda422015-12-26 03:56:48248 connect_delegate_->OnStartOpeningHandshake(std::move(request));
[email protected]cd48ed12014-01-22 14:34:22249
Ramin Halavati20e949f2018-02-14 20:14:32250 return parser()->SendRequest(
251 state_.GenerateRequestLine(), enriched_headers,
252 NetworkTrafficAnnotationTag(state_.traffic_annotation()), response,
253 std::move(callback));
[email protected]d51365e2013-11-27 10:46:52254}
255
256int WebSocketBasicHandshakeStream::ReadResponseHeaders(
Bence Békya25e3f72018-02-13 21:13:39257 CompletionOnceCallback callback) {
[email protected]d51365e2013-11-27 10:46:52258 // HttpStreamParser uses a weak pointer when reading from the
259 // socket, so it won't be called back after being destroyed. The
260 // HttpStreamParser is owned by HttpBasicState which is owned by this object,
261 // so this use of base::Unretained() is safe.
Bence Békya25e3f72018-02-13 21:13:39262 int rv = parser()->ReadResponseHeaders(base::BindOnce(
263 &WebSocketBasicHandshakeStream::ReadResponseHeadersCallback,
264 base::Unretained(this), std::move(callback)));
[email protected]cd48ed12014-01-22 14:34:22265 if (rv == ERR_IO_PENDING)
266 return rv;
ricea24c195f2015-02-26 12:18:55267 return ValidateResponse(rv);
[email protected]d51365e2013-11-27 10:46:52268}
269
[email protected]d51365e2013-11-27 10:46:52270int WebSocketBasicHandshakeStream::ReadResponseBody(
271 IOBuffer* buf,
272 int buf_len,
Bence Békya25e3f72018-02-13 21:13:39273 CompletionOnceCallback callback) {
274 return parser()->ReadResponseBody(buf, buf_len, std::move(callback));
[email protected]d51365e2013-11-27 10:46:52275}
276
277void WebSocketBasicHandshakeStream::Close(bool not_reusable) {
278 // This class ignores the value of |not_reusable| and never lets the socket be
279 // re-used.
280 if (parser())
281 parser()->Close(true);
282}
283
284bool WebSocketBasicHandshakeStream::IsResponseBodyComplete() const {
285 return parser()->IsResponseBodyComplete();
286}
287
[email protected]d51365e2013-11-27 10:46:52288bool WebSocketBasicHandshakeStream::IsConnectionReused() const {
289 return parser()->IsConnectionReused();
290}
291
292void WebSocketBasicHandshakeStream::SetConnectionReused() {
293 parser()->SetConnectionReused();
294}
295
mmenkebd84c392015-09-02 14:12:34296bool WebSocketBasicHandshakeStream::CanReuseConnection() const {
Adam Ricec786ad8a2018-05-22 09:49:15297 if (!base::FeatureList::IsEnabled(kWebSocketHandshakeReuseConnection))
298 return false;
299
300 return parser() && parser()->CanReuseConnection();
[email protected]d51365e2013-11-27 10:46:52301}
302
sclittle4de1bab92015-09-22 21:28:24303int64_t WebSocketBasicHandshakeStream::GetTotalReceivedBytes() const {
[email protected]bc92bc972013-12-13 08:32:59304 return 0;
305}
306
sclittlebe1ccf62015-09-02 19:40:36307int64_t WebSocketBasicHandshakeStream::GetTotalSentBytes() const {
308 return 0;
309}
310
rchcd379012017-04-12 21:53:32311bool WebSocketBasicHandshakeStream::GetAlternativeService(
312 AlternativeService* alternative_service) const {
313 return false;
314}
315
[email protected]d51365e2013-11-27 10:46:52316bool WebSocketBasicHandshakeStream::GetLoadTimingInfo(
317 LoadTimingInfo* load_timing_info) const {
318 return state_.connection()->GetLoadTimingInfo(IsConnectionReused(),
319 load_timing_info);
320}
321
322void WebSocketBasicHandshakeStream::GetSSLInfo(SSLInfo* ssl_info) {
323 parser()->GetSSLInfo(ssl_info);
324}
325
326void WebSocketBasicHandshakeStream::GetSSLCertRequestInfo(
327 SSLCertRequestInfo* cert_request_info) {
328 parser()->GetSSLCertRequestInfo(cert_request_info);
329}
330
ttuttled9dbc652015-09-29 20:00:59331bool WebSocketBasicHandshakeStream::GetRemoteEndpoint(IPEndPoint* endpoint) {
332 if (!state_.connection() || !state_.connection()->socket())
333 return false;
334
335 return state_.connection()->socket()->GetPeerAddress(endpoint) == OK;
336}
337
zhongyica364fbb2015-12-12 03:39:12338void WebSocketBasicHandshakeStream::PopulateNetErrorDetails(
339 NetErrorDetails* /*details*/) {
340 return;
341}
342
nharper78e6d2b2016-09-21 05:42:35343Error WebSocketBasicHandshakeStream::GetTokenBindingSignature(
nharperb7441ef2016-01-25 23:54:14344 crypto::ECPrivateKey* key,
nharper78e6d2b2016-09-21 05:42:35345 TokenBindingType tb_type,
nharperb7441ef2016-01-25 23:54:14346 std::vector<uint8_t>* out) {
Bence Béky3cb271d2018-03-29 22:00:48347 DCHECK(url_.SchemeIsCryptographic());
348
Bence Békydae8af5f2018-04-13 08:53:17349 return state_.connection()->socket()->GetTokenBindingSignature(key, tb_type,
350 out);
nharperb7441ef2016-01-25 23:54:14351}
352
[email protected]d51365e2013-11-27 10:46:52353void WebSocketBasicHandshakeStream::Drain(HttpNetworkSession* session) {
354 HttpResponseBodyDrainer* drainer = new HttpResponseBodyDrainer(this);
355 drainer->Start(session);
356 // |drainer| will delete itself.
357}
358
359void WebSocketBasicHandshakeStream::SetPriority(RequestPriority priority) {
360 // TODO(ricea): See TODO comment in HttpBasicStream::SetPriority(). If it is
361 // gone, then copy whatever has happened there over here.
362}
363
yhiranoa7e05bb2014-11-06 05:40:39364HttpStream* WebSocketBasicHandshakeStream::RenewStreamForAuth() {
365 // Return null because we don't support renewing the stream.
366 return nullptr;
367}
368
danakj9c5cab52016-04-16 00:54:33369std::unique_ptr<WebSocketStream> WebSocketBasicHandshakeStream::Upgrade() {
[email protected]d51365e2013-11-27 10:46:52370 // The HttpStreamParser object has a pointer to our ClientSocketHandle. Make
371 // sure it does not touch it again before it is destroyed.
372 state_.DeleteParser();
Bence Békyda280c62018-04-12 15:08:37373 WebSocketTransportClientSocketPool::UnlockEndpoint(
374 state_.connection(), websocket_endpoint_lock_manager_);
Bence Béky7294fc22018-02-08 14:26:17375 std::unique_ptr<WebSocketStream> basic_stream =
376 std::make_unique<WebSocketBasicStream>(
377 std::make_unique<WebSocketClientSocketHandleAdapter>(
378 state_.ReleaseConnection()),
379 state_.read_buf(), sub_protocol_, extensions_);
[email protected]0be93922014-01-29 00:42:45380 DCHECK(extension_params_.get());
381 if (extension_params_->deflate_enabled) {
Bence Békyde0be312018-03-13 17:51:58382 RecordDeflateMode(
383 extension_params_->deflate_parameters.client_context_take_over_mode());
[email protected]9c50b042014-04-28 06:40:15384
Bence Béky65623972018-03-05 15:31:56385 return std::make_unique<WebSocketDeflateStream>(
dchengc7eeda422015-12-26 03:56:48386 std::move(basic_stream), extension_params_->deflate_parameters,
Bence Béky65623972018-03-05 15:31:56387 std::make_unique<WebSocketDeflatePredictorImpl>());
[email protected]0be93922014-01-29 00:42:45388 } else {
dchengc7eeda422015-12-26 03:56:48389 return basic_stream;
[email protected]0be93922014-01-29 00:42:45390 }
[email protected]d51365e2013-11-27 10:46:52391}
392
[email protected]a31ecc02013-12-05 08:30:55393void WebSocketBasicHandshakeStream::SetWebSocketKeyForTesting(
394 const std::string& key) {
Bence Béky7d0c74d2018-03-05 08:31:09395 handshake_challenge_for_testing_ = key;
[email protected]a31ecc02013-12-05 08:30:55396}
397
[email protected]d51365e2013-11-27 10:46:52398void WebSocketBasicHandshakeStream::ReadResponseHeadersCallback(
Bence Békya25e3f72018-02-13 21:13:39399 CompletionOnceCallback callback,
[email protected]d51365e2013-11-27 10:46:52400 int result) {
Bence Békya25e3f72018-02-13 21:13:39401 std::move(callback).Run(ValidateResponse(result));
[email protected]d51365e2013-11-27 10:46:52402}
403
[email protected]cd48ed12014-01-22 14:34:22404void WebSocketBasicHandshakeStream::OnFinishOpeningHandshake() {
[email protected]cd48ed12014-01-22 14:34:22405 DCHECK(http_response_info_);
Yutaka Hirano36c94952018-05-30 21:33:33406 WebSocketDispatchOnFinishOpeningHandshake(
407 connect_delegate_, url_, http_response_info_->headers,
408 http_response_info_->socket_address, http_response_info_->response_time);
[email protected]cd48ed12014-01-22 14:34:22409}
410
ricea24c195f2015-02-26 12:18:55411int WebSocketBasicHandshakeStream::ValidateResponse(int rv) {
[email protected]d51365e2013-11-27 10:46:52412 DCHECK(http_response_info_);
[email protected]f7e98ca2014-06-19 12:05:43413 // Most net errors happen during connection, so they are not seen by this
414 // method. The histogram for error codes is created in
415 // Delegate::OnResponseStarted in websocket_stream.cc instead.
[email protected]e5760f522014-02-05 12:28:50416 if (rv >= 0) {
[email protected]f7e98ca2014-06-19 12:05:43417 const HttpResponseHeaders* headers = http_response_info_->headers.get();
418 const int response_code = headers->response_code();
Ilya Sherman0eb39802017-12-08 20:58:18419 base::UmaHistogramSparse("Net.WebSocket.ResponseCode", response_code);
[email protected]f7e98ca2014-06-19 12:05:43420 switch (response_code) {
[email protected]e5760f522014-02-05 12:28:50421 case HTTP_SWITCHING_PROTOCOLS:
422 OnFinishOpeningHandshake();
423 return ValidateUpgradeResponse(headers);
[email protected]d51365e2013-11-27 10:46:52424
[email protected]e5760f522014-02-05 12:28:50425 // We need to pass these through for authentication to work.
426 case HTTP_UNAUTHORIZED:
427 case HTTP_PROXY_AUTHENTICATION_REQUIRED:
428 return OK;
[email protected]d51365e2013-11-27 10:46:52429
[email protected]e5760f522014-02-05 12:28:50430 // Other status codes are potentially risky (see the warnings in the
431 // WHATWG WebSocket API spec) and so are dropped by default.
432 default:
[email protected]aeb640d2014-02-21 11:03:18433 // A WebSocket server cannot be using HTTP/0.9, so if we see version
434 // 0.9, it means the response was garbage.
435 // Reporting "Unexpected response code: 200" in this case is not
436 // helpful, so use a different error message.
437 if (headers->GetHttpVersion() == HttpVersion(0, 9)) {
tyoshinoccfcfde2016-07-21 14:06:55438 OnFailure("Error during WebSocket handshake: Invalid status line");
[email protected]aeb640d2014-02-21 11:03:18439 } else {
tyoshinoccfcfde2016-07-21 14:06:55440 OnFailure(base::StringPrintf(
[email protected]aeb640d2014-02-21 11:03:18441 "Error during WebSocket handshake: Unexpected response code: %d",
[email protected]8aba0172014-07-03 12:09:53442 headers->response_code()));
[email protected]aeb640d2014-02-21 11:03:18443 }
[email protected]e5760f522014-02-05 12:28:50444 OnFinishOpeningHandshake();
Bence Békyde0be312018-03-13 17:51:58445 result_ = HandshakeResult::INVALID_STATUS;
[email protected]e5760f522014-02-05 12:28:50446 return ERR_INVALID_RESPONSE;
447 }
448 } else {
[email protected]3efc08f2014-02-07 09:33:34449 if (rv == ERR_EMPTY_RESPONSE) {
tyoshinoccfcfde2016-07-21 14:06:55450 OnFailure("Connection closed before receiving a handshake response");
Bence Békyde0be312018-03-13 17:51:58451 result_ = HandshakeResult::EMPTY_RESPONSE;
[email protected]3efc08f2014-02-07 09:33:34452 return rv;
453 }
tyoshinoccfcfde2016-07-21 14:06:55454 OnFailure(std::string("Error during WebSocket handshake: ") +
455 ErrorToString(rv));
[email protected]e5760f522014-02-05 12:28:50456 OnFinishOpeningHandshake();
ricea23c3f942015-02-02 13:35:13457 // Some error codes (for example ERR_CONNECTION_CLOSED) get changed to OK at
458 // higher levels. To prevent an unvalidated connection getting erroneously
459 // upgraded, don't pass through the status code unchanged if it is
460 // HTTP_SWITCHING_PROTOCOLS.
461 if (http_response_info_->headers &&
462 http_response_info_->headers->response_code() ==
463 HTTP_SWITCHING_PROTOCOLS) {
464 http_response_info_->headers->ReplaceStatusLine(
465 kConnectionErrorStatusLine);
Bence Békyde0be312018-03-13 17:51:58466 result_ = HandshakeResult::FAILED_SWITCHING_PROTOCOLS;
467 return rv;
ricea23c3f942015-02-02 13:35:13468 }
Bence Békyde0be312018-03-13 17:51:58469 result_ = HandshakeResult::FAILED;
[email protected]e5760f522014-02-05 12:28:50470 return rv;
[email protected]d51365e2013-11-27 10:46:52471 }
472}
473
474int WebSocketBasicHandshakeStream::ValidateUpgradeResponse(
[email protected]e5760f522014-02-05 12:28:50475 const HttpResponseHeaders* headers) {
Bence Béky65623972018-03-05 15:31:56476 extension_params_ = std::make_unique<WebSocketExtensionParams>();
[email protected]8aba0172014-07-03 12:09:53477 std::string failure_message;
Bence Békyde0be312018-03-13 17:51:58478 if (!ValidateUpgrade(headers, &failure_message)) {
479 result_ = HandshakeResult::FAILED_UPGRADE;
480 } else if (!ValidateSecWebSocketAccept(headers, handshake_challenge_response_,
481 &failure_message)) {
482 result_ = HandshakeResult::FAILED_ACCEPT;
483 } else if (!ValidateConnection(headers, &failure_message)) {
484 result_ = HandshakeResult::FAILED_CONNECTION;
485 } else if (!ValidateSubProtocol(headers, requested_sub_protocols_,
486 &sub_protocol_, &failure_message)) {
487 result_ = HandshakeResult::FAILED_SUBPROTO;
488 } else if (!ValidateExtensions(headers, &extensions_, &failure_message,
489 extension_params_.get())) {
490 result_ = HandshakeResult::FAILED_EXTENSIONS;
491 } else {
492 result_ = HandshakeResult::CONNECTED;
[email protected]d51365e2013-11-27 10:46:52493 return OK;
494 }
tyoshinoccfcfde2016-07-21 14:06:55495 OnFailure("Error during WebSocket handshake: " + failure_message);
[email protected]d51365e2013-11-27 10:46:52496 return ERR_INVALID_RESPONSE;
497}
498
tyoshinoccfcfde2016-07-21 14:06:55499void WebSocketBasicHandshakeStream::OnFailure(const std::string& message) {
500 stream_request_->OnFailure(message);
[email protected]8aba0172014-07-03 12:09:53501}
502
[email protected]d51365e2013-11-27 10:46:52503} // namespace net