blob: 8cf6c7825d41a8b992aceb911f73b80ad50b721e [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"
[email protected]654866142014-06-24 22:53:3135#include "net/socket/websocket_transport_client_socket_pool.h"
[email protected]d51365e2013-11-27 10:46:5236#include "net/websockets/websocket_basic_stream.h"
Bence Béky7294fc22018-02-08 14:26:1737#include "net/websockets/websocket_basic_stream_adapters.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"
ricea11bdcd02014-11-20 09:57:0743#include "net/websockets/websocket_handshake_challenge.h"
[email protected]d51365e2013-11-27 10:46:5244#include "net/websockets/websocket_handshake_constants.h"
[email protected]cd48ed12014-01-22 14:34:2245#include "net/websockets/websocket_handshake_request_info.h"
46#include "net/websockets/websocket_handshake_response_info.h"
[email protected]d51365e2013-11-27 10:46:5247#include "net/websockets/websocket_stream.h"
48
49namespace net {
[email protected]0be93922014-01-29 00:42:4550
yhirano27b2b572014-10-30 11:23:4451namespace {
52
ricea23c3f942015-02-02 13:35:1353const char kConnectionErrorStatusLine[] = "HTTP/1.1 503 Connection Error";
54
yhirano27b2b572014-10-30 11:23:4455} // namespace
56
[email protected]d51365e2013-11-27 10:46:5257namespace {
58
[email protected]96868202014-01-09 10:38:0459enum GetHeaderResult {
60 GET_HEADER_OK,
61 GET_HEADER_MISSING,
62 GET_HEADER_MULTIPLE,
63};
64
65std::string MissingHeaderMessage(const std::string& header_name) {
66 return std::string("'") + header_name + "' header is missing";
67}
68
[email protected]d51365e2013-11-27 10:46:5269std::string GenerateHandshakeChallenge() {
70 std::string raw_challenge(websockets::kRawChallengeLength, '\0');
skyostilb8f60ca2016-08-12 12:34:4371 crypto::RandBytes(base::string_as_array(&raw_challenge),
72 raw_challenge.length());
[email protected]d51365e2013-11-27 10:46:5273 std::string encoded_challenge;
[email protected]33fca122013-12-11 01:48:5074 base::Base64Encode(raw_challenge, &encoded_challenge);
[email protected]d51365e2013-11-27 10:46:5275 return encoded_challenge;
76}
77
[email protected]96868202014-01-09 10:38:0478GetHeaderResult GetSingleHeaderValue(const HttpResponseHeaders* headers,
79 const base::StringPiece& name,
80 std::string* value) {
olli.raulaee489a52016-01-25 08:37:1081 size_t iter = 0;
[email protected]96868202014-01-09 10:38:0482 size_t num_values = 0;
83 std::string temp_value;
olli.raulaee489a52016-01-25 08:37:1084 while (headers->EnumerateHeader(&iter, name, &temp_value)) {
[email protected]96868202014-01-09 10:38:0485 if (++num_values > 1)
86 return GET_HEADER_MULTIPLE;
87 *value = temp_value;
[email protected]d51365e2013-11-27 10:46:5288 }
[email protected]96868202014-01-09 10:38:0489 return num_values > 0 ? GET_HEADER_OK : GET_HEADER_MISSING;
90}
91
92bool ValidateHeaderHasSingleValue(GetHeaderResult result,
93 const std::string& header_name,
94 std::string* failure_message) {
95 if (result == GET_HEADER_MISSING) {
96 *failure_message = MissingHeaderMessage(header_name);
97 return false;
98 }
99 if (result == GET_HEADER_MULTIPLE) {
Bence Békyb28709c22018-03-06 13:03:44100 *failure_message =
101 WebSocketHandshakeStreamBase::MultipleHeaderValuesMessage(header_name);
[email protected]96868202014-01-09 10:38:04102 return false;
103 }
104 DCHECK_EQ(result, GET_HEADER_OK);
105 return true;
106}
107
108bool ValidateUpgrade(const HttpResponseHeaders* headers,
109 std::string* failure_message) {
110 std::string value;
111 GetHeaderResult result =
112 GetSingleHeaderValue(headers, websockets::kUpgrade, &value);
113 if (!ValidateHeaderHasSingleValue(result,
114 websockets::kUpgrade,
115 failure_message)) {
116 return false;
117 }
118
brettwbc17d2c82015-06-09 22:39:08119 if (!base::LowerCaseEqualsASCII(value, websockets::kWebSocketLowercase)) {
[email protected]96868202014-01-09 10:38:04120 *failure_message =
121 "'Upgrade' header value is not 'WebSocket': " + value;
122 return false;
123 }
124 return true;
125}
126
127bool ValidateSecWebSocketAccept(const HttpResponseHeaders* headers,
128 const std::string& expected,
129 std::string* failure_message) {
130 std::string actual;
131 GetHeaderResult result =
132 GetSingleHeaderValue(headers, websockets::kSecWebSocketAccept, &actual);
133 if (!ValidateHeaderHasSingleValue(result,
134 websockets::kSecWebSocketAccept,
135 failure_message)) {
136 return false;
137 }
138
139 if (expected != actual) {
140 *failure_message = "Incorrect 'Sec-WebSocket-Accept' header value";
141 return false;
142 }
143 return true;
144}
145
146bool ValidateConnection(const HttpResponseHeaders* headers,
147 std::string* failure_message) {
148 // Connection header is permitted to contain other tokens.
149 if (!headers->HasHeader(HttpRequestHeaders::kConnection)) {
150 *failure_message = MissingHeaderMessage(HttpRequestHeaders::kConnection);
151 return false;
152 }
153 if (!headers->HasHeaderValue(HttpRequestHeaders::kConnection,
154 websockets::kUpgrade)) {
155 *failure_message = "'Connection' header value must contain 'Upgrade'";
156 return false;
157 }
158 return true;
[email protected]d51365e2013-11-27 10:46:52159}
160
[email protected]d51365e2013-11-27 10:46:52161} // namespace
162
163WebSocketBasicHandshakeStream::WebSocketBasicHandshakeStream(
danakj9c5cab52016-04-16 00:54:33164 std::unique_ptr<ClientSocketHandle> connection,
[email protected]cd48ed12014-01-22 14:34:22165 WebSocketStream::ConnectDelegate* connect_delegate,
[email protected]d51365e2013-11-27 10:46:52166 bool using_proxy,
167 std::vector<std::string> requested_sub_protocols,
[email protected]8aba0172014-07-03 12:09:53168 std::vector<std::string> requested_extensions,
tyoshinoccfcfde2016-07-21 14:06:55169 WebSocketStreamRequest* request)
Bence Békyde0be312018-03-13 17:51:58170 : result_(HandshakeResult::INCOMPLETE),
171 state_(std::move(connection),
mmenkea7da6da2016-09-01 21:56:52172 using_proxy,
173 false /* http_09_on_non_default_ports_enabled */),
[email protected]cd48ed12014-01-22 14:34:22174 connect_delegate_(connect_delegate),
yhiranoa7e05bb2014-11-06 05:40:39175 http_response_info_(nullptr),
[email protected]d51365e2013-11-27 10:46:52176 requested_sub_protocols_(requested_sub_protocols),
[email protected]8aba0172014-07-03 12:09:53177 requested_extensions_(requested_extensions),
tyoshinoccfcfde2016-07-21 14:06:55178 stream_request_(request) {
[email protected]8aba0172014-07-03 12:09:53179 DCHECK(connect_delegate);
tyoshinoccfcfde2016-07-21 14:06:55180 DCHECK(request);
[email protected]8aba0172014-07-03 12:09:53181}
[email protected]d51365e2013-11-27 10:46:52182
Bence Békyde0be312018-03-13 17:51:58183WebSocketBasicHandshakeStream::~WebSocketBasicHandshakeStream() {
184 RecordHandshakeResult(result_);
185}
[email protected]d51365e2013-11-27 10:46:52186
187int WebSocketBasicHandshakeStream::InitializeStream(
188 const HttpRequestInfo* request_info,
Steven Valdezb4ff0412018-01-18 22:39:27189 bool can_send_early,
[email protected]d51365e2013-11-27 10:46:52190 RequestPriority priority,
tfarina42834112016-09-22 13:38:20191 const NetLogWithSource& net_log,
Bence Békya25e3f72018-02-13 21:13:39192 CompletionOnceCallback callback) {
Ramin Halavati20e949f2018-02-14 20:14:32193 DCHECK(request_info->traffic_annotation.is_valid());
[email protected]cd48ed12014-01-22 14:34:22194 url_ = request_info->url;
Bence Békyd5d65a912018-02-11 04:28:00195 state_.Initialize(request_info, can_send_early, priority, net_log);
[email protected]d51365e2013-11-27 10:46:52196 return OK;
197}
198
199int WebSocketBasicHandshakeStream::SendRequest(
200 const HttpRequestHeaders& headers,
201 HttpResponseInfo* response,
Bence Békya25e3f72018-02-13 21:13:39202 CompletionOnceCallback callback) {
[email protected]d51365e2013-11-27 10:46:52203 DCHECK(!headers.HasHeader(websockets::kSecWebSocketKey));
204 DCHECK(!headers.HasHeader(websockets::kSecWebSocketProtocol));
205 DCHECK(!headers.HasHeader(websockets::kSecWebSocketExtensions));
206 DCHECK(headers.HasHeader(HttpRequestHeaders::kOrigin));
207 DCHECK(headers.HasHeader(websockets::kUpgrade));
208 DCHECK(headers.HasHeader(HttpRequestHeaders::kConnection));
209 DCHECK(headers.HasHeader(websockets::kSecWebSocketVersion));
210 DCHECK(parser());
211
212 http_response_info_ = response;
213
214 // Create a copy of the headers object, so that we can add the
215 // Sec-WebSockey-Key header.
216 HttpRequestHeaders enriched_headers;
217 enriched_headers.CopyFrom(headers);
[email protected]a31ecc02013-12-05 08:30:55218 std::string handshake_challenge;
Bence Béky7d0c74d2018-03-05 08:31:09219 if (handshake_challenge_for_testing_.has_value()) {
220 handshake_challenge = handshake_challenge_for_testing_.value();
[email protected]a31ecc02013-12-05 08:30:55221 handshake_challenge_for_testing_.reset();
222 } else {
223 handshake_challenge = GenerateHandshakeChallenge();
224 }
[email protected]d51365e2013-11-27 10:46:52225 enriched_headers.SetHeader(websockets::kSecWebSocketKey, handshake_challenge);
226
[email protected]d51365e2013-11-27 10:46:52227 AddVectorHeaderIfNonEmpty(websockets::kSecWebSocketExtensions,
228 requested_extensions_,
229 &enriched_headers);
[email protected]0be93922014-01-29 00:42:45230 AddVectorHeaderIfNonEmpty(websockets::kSecWebSocketProtocol,
231 requested_sub_protocols_,
232 &enriched_headers);
[email protected]d51365e2013-11-27 10:46:52233
ricea11bdcd02014-11-20 09:57:07234 handshake_challenge_response_ =
235 ComputeSecWebSocketAccept(handshake_challenge);
[email protected]d51365e2013-11-27 10:46:52236
[email protected]cd48ed12014-01-22 14:34:22237 DCHECK(connect_delegate_);
Bence Béky65623972018-03-05 15:31:56238 auto request =
239 std::make_unique<WebSocketHandshakeRequestInfo>(url_, base::Time::Now());
[email protected]cd48ed12014-01-22 14:34:22240 request->headers.CopyFrom(enriched_headers);
dchengc7eeda422015-12-26 03:56:48241 connect_delegate_->OnStartOpeningHandshake(std::move(request));
[email protected]cd48ed12014-01-22 14:34:22242
Ramin Halavati20e949f2018-02-14 20:14:32243 return parser()->SendRequest(
244 state_.GenerateRequestLine(), enriched_headers,
245 NetworkTrafficAnnotationTag(state_.traffic_annotation()), response,
246 std::move(callback));
[email protected]d51365e2013-11-27 10:46:52247}
248
249int WebSocketBasicHandshakeStream::ReadResponseHeaders(
Bence Békya25e3f72018-02-13 21:13:39250 CompletionOnceCallback callback) {
[email protected]d51365e2013-11-27 10:46:52251 // HttpStreamParser uses a weak pointer when reading from the
252 // socket, so it won't be called back after being destroyed. The
253 // HttpStreamParser is owned by HttpBasicState which is owned by this object,
254 // so this use of base::Unretained() is safe.
Bence Békya25e3f72018-02-13 21:13:39255 int rv = parser()->ReadResponseHeaders(base::BindOnce(
256 &WebSocketBasicHandshakeStream::ReadResponseHeadersCallback,
257 base::Unretained(this), std::move(callback)));
[email protected]cd48ed12014-01-22 14:34:22258 if (rv == ERR_IO_PENDING)
259 return rv;
ricea24c195f2015-02-26 12:18:55260 return ValidateResponse(rv);
[email protected]d51365e2013-11-27 10:46:52261}
262
[email protected]d51365e2013-11-27 10:46:52263int WebSocketBasicHandshakeStream::ReadResponseBody(
264 IOBuffer* buf,
265 int buf_len,
Bence Békya25e3f72018-02-13 21:13:39266 CompletionOnceCallback callback) {
267 return parser()->ReadResponseBody(buf, buf_len, std::move(callback));
[email protected]d51365e2013-11-27 10:46:52268}
269
270void WebSocketBasicHandshakeStream::Close(bool not_reusable) {
271 // This class ignores the value of |not_reusable| and never lets the socket be
272 // re-used.
273 if (parser())
274 parser()->Close(true);
275}
276
277bool WebSocketBasicHandshakeStream::IsResponseBodyComplete() const {
278 return parser()->IsResponseBodyComplete();
279}
280
[email protected]d51365e2013-11-27 10:46:52281bool WebSocketBasicHandshakeStream::IsConnectionReused() const {
282 return parser()->IsConnectionReused();
283}
284
285void WebSocketBasicHandshakeStream::SetConnectionReused() {
286 parser()->SetConnectionReused();
287}
288
mmenkebd84c392015-09-02 14:12:34289bool WebSocketBasicHandshakeStream::CanReuseConnection() const {
[email protected]d51365e2013-11-27 10:46:52290 return false;
291}
292
sclittle4de1bab92015-09-22 21:28:24293int64_t WebSocketBasicHandshakeStream::GetTotalReceivedBytes() const {
[email protected]bc92bc972013-12-13 08:32:59294 return 0;
295}
296
sclittlebe1ccf62015-09-02 19:40:36297int64_t WebSocketBasicHandshakeStream::GetTotalSentBytes() const {
298 return 0;
299}
300
rchcd379012017-04-12 21:53:32301bool WebSocketBasicHandshakeStream::GetAlternativeService(
302 AlternativeService* alternative_service) const {
303 return false;
304}
305
[email protected]d51365e2013-11-27 10:46:52306bool WebSocketBasicHandshakeStream::GetLoadTimingInfo(
307 LoadTimingInfo* load_timing_info) const {
308 return state_.connection()->GetLoadTimingInfo(IsConnectionReused(),
309 load_timing_info);
310}
311
312void WebSocketBasicHandshakeStream::GetSSLInfo(SSLInfo* ssl_info) {
313 parser()->GetSSLInfo(ssl_info);
314}
315
316void WebSocketBasicHandshakeStream::GetSSLCertRequestInfo(
317 SSLCertRequestInfo* cert_request_info) {
318 parser()->GetSSLCertRequestInfo(cert_request_info);
319}
320
ttuttled9dbc652015-09-29 20:00:59321bool WebSocketBasicHandshakeStream::GetRemoteEndpoint(IPEndPoint* endpoint) {
322 if (!state_.connection() || !state_.connection()->socket())
323 return false;
324
325 return state_.connection()->socket()->GetPeerAddress(endpoint) == OK;
326}
327
zhongyica364fbb2015-12-12 03:39:12328void WebSocketBasicHandshakeStream::PopulateNetErrorDetails(
329 NetErrorDetails* /*details*/) {
330 return;
331}
332
nharper78e6d2b2016-09-21 05:42:35333Error WebSocketBasicHandshakeStream::GetTokenBindingSignature(
nharperb7441ef2016-01-25 23:54:14334 crypto::ECPrivateKey* key,
nharper78e6d2b2016-09-21 05:42:35335 TokenBindingType tb_type,
nharperb7441ef2016-01-25 23:54:14336 std::vector<uint8_t>* out) {
337 NOTREACHED();
338 return ERR_NOT_IMPLEMENTED;
339}
340
[email protected]d51365e2013-11-27 10:46:52341void WebSocketBasicHandshakeStream::Drain(HttpNetworkSession* session) {
342 HttpResponseBodyDrainer* drainer = new HttpResponseBodyDrainer(this);
343 drainer->Start(session);
344 // |drainer| will delete itself.
345}
346
347void WebSocketBasicHandshakeStream::SetPriority(RequestPriority priority) {
348 // TODO(ricea): See TODO comment in HttpBasicStream::SetPriority(). If it is
349 // gone, then copy whatever has happened there over here.
350}
351
yhiranoa7e05bb2014-11-06 05:40:39352HttpStream* WebSocketBasicHandshakeStream::RenewStreamForAuth() {
353 // Return null because we don't support renewing the stream.
354 return nullptr;
355}
356
danakj9c5cab52016-04-16 00:54:33357std::unique_ptr<WebSocketStream> WebSocketBasicHandshakeStream::Upgrade() {
[email protected]d51365e2013-11-27 10:46:52358 // The HttpStreamParser object has a pointer to our ClientSocketHandle. Make
359 // sure it does not touch it again before it is destroyed.
360 state_.DeleteParser();
[email protected]654866142014-06-24 22:53:31361 WebSocketTransportClientSocketPool::UnlockEndpoint(state_.connection());
Bence Béky7294fc22018-02-08 14:26:17362 std::unique_ptr<WebSocketStream> basic_stream =
363 std::make_unique<WebSocketBasicStream>(
364 std::make_unique<WebSocketClientSocketHandleAdapter>(
365 state_.ReleaseConnection()),
366 state_.read_buf(), sub_protocol_, extensions_);
[email protected]0be93922014-01-29 00:42:45367 DCHECK(extension_params_.get());
368 if (extension_params_->deflate_enabled) {
Bence Békyde0be312018-03-13 17:51:58369 RecordDeflateMode(
370 extension_params_->deflate_parameters.client_context_take_over_mode());
[email protected]9c50b042014-04-28 06:40:15371
Bence Béky65623972018-03-05 15:31:56372 return std::make_unique<WebSocketDeflateStream>(
dchengc7eeda422015-12-26 03:56:48373 std::move(basic_stream), extension_params_->deflate_parameters,
Bence Béky65623972018-03-05 15:31:56374 std::make_unique<WebSocketDeflatePredictorImpl>());
[email protected]0be93922014-01-29 00:42:45375 } else {
dchengc7eeda422015-12-26 03:56:48376 return basic_stream;
[email protected]0be93922014-01-29 00:42:45377 }
[email protected]d51365e2013-11-27 10:46:52378}
379
[email protected]a31ecc02013-12-05 08:30:55380void WebSocketBasicHandshakeStream::SetWebSocketKeyForTesting(
381 const std::string& key) {
Bence Béky7d0c74d2018-03-05 08:31:09382 handshake_challenge_for_testing_ = key;
[email protected]a31ecc02013-12-05 08:30:55383}
384
[email protected]d51365e2013-11-27 10:46:52385void WebSocketBasicHandshakeStream::ReadResponseHeadersCallback(
Bence Békya25e3f72018-02-13 21:13:39386 CompletionOnceCallback callback,
[email protected]d51365e2013-11-27 10:46:52387 int result) {
Bence Békya25e3f72018-02-13 21:13:39388 std::move(callback).Run(ValidateResponse(result));
[email protected]d51365e2013-11-27 10:46:52389}
390
[email protected]cd48ed12014-01-22 14:34:22391void WebSocketBasicHandshakeStream::OnFinishOpeningHandshake() {
[email protected]cd48ed12014-01-22 14:34:22392 DCHECK(http_response_info_);
[email protected]e69c1cd2014-07-29 07:42:29393 WebSocketDispatchOnFinishOpeningHandshake(connect_delegate_,
394 url_,
395 http_response_info_->headers,
396 http_response_info_->response_time);
[email protected]cd48ed12014-01-22 14:34:22397}
398
ricea24c195f2015-02-26 12:18:55399int WebSocketBasicHandshakeStream::ValidateResponse(int rv) {
[email protected]d51365e2013-11-27 10:46:52400 DCHECK(http_response_info_);
[email protected]f7e98ca2014-06-19 12:05:43401 // Most net errors happen during connection, so they are not seen by this
402 // method. The histogram for error codes is created in
403 // Delegate::OnResponseStarted in websocket_stream.cc instead.
[email protected]e5760f522014-02-05 12:28:50404 if (rv >= 0) {
[email protected]f7e98ca2014-06-19 12:05:43405 const HttpResponseHeaders* headers = http_response_info_->headers.get();
406 const int response_code = headers->response_code();
Ilya Sherman0eb39802017-12-08 20:58:18407 base::UmaHistogramSparse("Net.WebSocket.ResponseCode", response_code);
[email protected]f7e98ca2014-06-19 12:05:43408 switch (response_code) {
[email protected]e5760f522014-02-05 12:28:50409 case HTTP_SWITCHING_PROTOCOLS:
410 OnFinishOpeningHandshake();
411 return ValidateUpgradeResponse(headers);
[email protected]d51365e2013-11-27 10:46:52412
[email protected]e5760f522014-02-05 12:28:50413 // We need to pass these through for authentication to work.
414 case HTTP_UNAUTHORIZED:
415 case HTTP_PROXY_AUTHENTICATION_REQUIRED:
416 return OK;
[email protected]d51365e2013-11-27 10:46:52417
[email protected]e5760f522014-02-05 12:28:50418 // Other status codes are potentially risky (see the warnings in the
419 // WHATWG WebSocket API spec) and so are dropped by default.
420 default:
[email protected]aeb640d2014-02-21 11:03:18421 // A WebSocket server cannot be using HTTP/0.9, so if we see version
422 // 0.9, it means the response was garbage.
423 // Reporting "Unexpected response code: 200" in this case is not
424 // helpful, so use a different error message.
425 if (headers->GetHttpVersion() == HttpVersion(0, 9)) {
tyoshinoccfcfde2016-07-21 14:06:55426 OnFailure("Error during WebSocket handshake: Invalid status line");
[email protected]aeb640d2014-02-21 11:03:18427 } else {
tyoshinoccfcfde2016-07-21 14:06:55428 OnFailure(base::StringPrintf(
[email protected]aeb640d2014-02-21 11:03:18429 "Error during WebSocket handshake: Unexpected response code: %d",
[email protected]8aba0172014-07-03 12:09:53430 headers->response_code()));
[email protected]aeb640d2014-02-21 11:03:18431 }
[email protected]e5760f522014-02-05 12:28:50432 OnFinishOpeningHandshake();
Bence Békyde0be312018-03-13 17:51:58433 result_ = HandshakeResult::INVALID_STATUS;
[email protected]e5760f522014-02-05 12:28:50434 return ERR_INVALID_RESPONSE;
435 }
436 } else {
[email protected]3efc08f2014-02-07 09:33:34437 if (rv == ERR_EMPTY_RESPONSE) {
tyoshinoccfcfde2016-07-21 14:06:55438 OnFailure("Connection closed before receiving a handshake response");
Bence Békyde0be312018-03-13 17:51:58439 result_ = HandshakeResult::EMPTY_RESPONSE;
[email protected]3efc08f2014-02-07 09:33:34440 return rv;
441 }
tyoshinoccfcfde2016-07-21 14:06:55442 OnFailure(std::string("Error during WebSocket handshake: ") +
443 ErrorToString(rv));
[email protected]e5760f522014-02-05 12:28:50444 OnFinishOpeningHandshake();
ricea23c3f942015-02-02 13:35:13445 // Some error codes (for example ERR_CONNECTION_CLOSED) get changed to OK at
446 // higher levels. To prevent an unvalidated connection getting erroneously
447 // upgraded, don't pass through the status code unchanged if it is
448 // HTTP_SWITCHING_PROTOCOLS.
449 if (http_response_info_->headers &&
450 http_response_info_->headers->response_code() ==
451 HTTP_SWITCHING_PROTOCOLS) {
452 http_response_info_->headers->ReplaceStatusLine(
453 kConnectionErrorStatusLine);
Bence Békyde0be312018-03-13 17:51:58454 result_ = HandshakeResult::FAILED_SWITCHING_PROTOCOLS;
455 return rv;
ricea23c3f942015-02-02 13:35:13456 }
Bence Békyde0be312018-03-13 17:51:58457 result_ = HandshakeResult::FAILED;
[email protected]e5760f522014-02-05 12:28:50458 return rv;
[email protected]d51365e2013-11-27 10:46:52459 }
460}
461
462int WebSocketBasicHandshakeStream::ValidateUpgradeResponse(
[email protected]e5760f522014-02-05 12:28:50463 const HttpResponseHeaders* headers) {
Bence Béky65623972018-03-05 15:31:56464 extension_params_ = std::make_unique<WebSocketExtensionParams>();
[email protected]8aba0172014-07-03 12:09:53465 std::string failure_message;
Bence Békyde0be312018-03-13 17:51:58466 if (!ValidateUpgrade(headers, &failure_message)) {
467 result_ = HandshakeResult::FAILED_UPGRADE;
468 } else if (!ValidateSecWebSocketAccept(headers, handshake_challenge_response_,
469 &failure_message)) {
470 result_ = HandshakeResult::FAILED_ACCEPT;
471 } else if (!ValidateConnection(headers, &failure_message)) {
472 result_ = HandshakeResult::FAILED_CONNECTION;
473 } else if (!ValidateSubProtocol(headers, requested_sub_protocols_,
474 &sub_protocol_, &failure_message)) {
475 result_ = HandshakeResult::FAILED_SUBPROTO;
476 } else if (!ValidateExtensions(headers, &extensions_, &failure_message,
477 extension_params_.get())) {
478 result_ = HandshakeResult::FAILED_EXTENSIONS;
479 } else {
480 result_ = HandshakeResult::CONNECTED;
[email protected]d51365e2013-11-27 10:46:52481 return OK;
482 }
tyoshinoccfcfde2016-07-21 14:06:55483 OnFailure("Error during WebSocket handshake: " + failure_message);
[email protected]d51365e2013-11-27 10:46:52484 return ERR_INVALID_RESPONSE;
485}
486
tyoshinoccfcfde2016-07-21 14:06:55487void WebSocketBasicHandshakeStream::OnFailure(const std::string& message) {
488 stream_request_->OnFailure(message);
[email protected]8aba0172014-07-03 12:09:53489}
490
[email protected]d51365e2013-11-27 10:46:52491} // namespace net