[email protected] | 473282d | 2013-07-02 11:57:07 | [diff] [blame] | 1 | // Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "net/websockets/websocket_stream.h" |
| 6 | |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 7 | #include <memory> |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 8 | #include <utility> |
| 9 | |
[email protected] | 473282d | 2013-07-02 11:57:07 | [diff] [blame] | 10 | #include "base/logging.h" |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 11 | #include "base/memory/ptr_util.h" |
asvitkine | c3c9372 | 2015-06-17 14:48:37 | [diff] [blame] | 12 | #include "base/metrics/histogram_macros.h" |
[email protected] | f7e98ca | 2014-06-19 12:05:43 | [diff] [blame] | 13 | #include "base/metrics/sparse_histogram.h" |
ricea | 38fc268c | 2015-02-09 02:41:29 | [diff] [blame] | 14 | #include "base/strings/stringprintf.h" |
yhirano | 569e09ce | 2014-09-11 12:07:29 | [diff] [blame] | 15 | #include "base/time/time.h" |
| 16 | #include "base/timer/timer.h" |
[email protected] | d759912 | 2014-05-24 03:37:23 | [diff] [blame] | 17 | #include "net/base/load_flags.h" |
[email protected] | efa9e73 | 2013-11-29 02:55:05 | [diff] [blame] | 18 | #include "net/http/http_request_headers.h" |
[email protected] | e69c1cd | 2014-07-29 07:42:29 | [diff] [blame] | 19 | #include "net/http/http_response_headers.h" |
[email protected] | efa9e73 | 2013-11-29 02:55:05 | [diff] [blame] | 20 | #include "net/http/http_status_code.h" |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 21 | #include "net/url_request/redirect_info.h" |
[email protected] | efa9e73 | 2013-11-29 02:55:05 | [diff] [blame] | 22 | #include "net/url_request/url_request.h" |
| 23 | #include "net/url_request/url_request_context.h" |
| 24 | #include "net/websockets/websocket_errors.h" |
[email protected] | a6244952 | 2014-06-05 11:11:15 | [diff] [blame] | 25 | #include "net/websockets/websocket_event_interface.h" |
[email protected] | efa9e73 | 2013-11-29 02:55:05 | [diff] [blame] | 26 | #include "net/websockets/websocket_handshake_constants.h" |
| 27 | #include "net/websockets/websocket_handshake_stream_base.h" |
| 28 | #include "net/websockets/websocket_handshake_stream_create_helper.h" |
[email protected] | efa9e73 | 2013-11-29 02:55:05 | [diff] [blame] | 29 | #include "url/gurl.h" |
mkwst | 4997ce8 | 2015-07-25 12:00:05 | [diff] [blame] | 30 | #include "url/origin.h" |
[email protected] | 473282d | 2013-07-02 11:57:07 | [diff] [blame] | 31 | |
| 32 | namespace net { |
[email protected] | efa9e73 | 2013-11-29 02:55:05 | [diff] [blame] | 33 | namespace { |
| 34 | |
yhirano | 569e09ce | 2014-09-11 12:07:29 | [diff] [blame] | 35 | // The timeout duration of WebSocket handshake. |
| 36 | // It is defined as the same value as the TCP connection timeout value in |
| 37 | // net/socket/websocket_transport_client_socket_pool.cc to make it hard for |
| 38 | // JavaScript programs to recognize the timeout cause. |
| 39 | const int kHandshakeTimeoutIntervalInSeconds = 240; |
| 40 | |
tyoshino | ccfcfde | 2016-07-21 14:06:55 | [diff] [blame] | 41 | class WebSocketStreamRequestImpl; |
[email protected] | efa9e73 | 2013-11-29 02:55:05 | [diff] [blame] | 42 | |
| 43 | class Delegate : public URLRequest::Delegate { |
| 44 | public: |
[email protected] | 1e0a16a | 2014-04-04 16:18:04 | [diff] [blame] | 45 | enum HandshakeResult { |
| 46 | INCOMPLETE, |
| 47 | CONNECTED, |
| 48 | FAILED, |
| 49 | NUM_HANDSHAKE_RESULT_TYPES, |
| 50 | }; |
| 51 | |
tyoshino | ccfcfde | 2016-07-21 14:06:55 | [diff] [blame] | 52 | explicit Delegate(WebSocketStreamRequestImpl* owner) |
[email protected] | 1e0a16a | 2014-04-04 16:18:04 | [diff] [blame] | 53 | : owner_(owner), result_(INCOMPLETE) {} |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 54 | ~Delegate() override { |
[email protected] | 1e0a16a | 2014-04-04 16:18:04 | [diff] [blame] | 55 | UMA_HISTOGRAM_ENUMERATION( |
| 56 | "Net.WebSocket.HandshakeResult", result_, NUM_HANDSHAKE_RESULT_TYPES); |
| 57 | } |
[email protected] | efa9e73 | 2013-11-29 02:55:05 | [diff] [blame] | 58 | |
| 59 | // Implementation of URLRequest::Delegate methods. |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 60 | void OnReceivedRedirect(URLRequest* request, |
| 61 | const RedirectInfo& redirect_info, |
Adam Rice | cb76ac6 | 2015-02-20 05:33:25 | [diff] [blame] | 62 | bool* defer_redirect) override; |
[email protected] | a6244952 | 2014-06-05 11:11:15 | [diff] [blame] | 63 | |
mmenke | fe5d0b11 | 2016-09-06 20:46:58 | [diff] [blame^] | 64 | void OnResponseStarted(URLRequest* request) override; |
[email protected] | efa9e73 | 2013-11-29 02:55:05 | [diff] [blame] | 65 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 66 | void OnAuthRequired(URLRequest* request, |
| 67 | AuthChallengeInfo* auth_info) override; |
[email protected] | efa9e73 | 2013-11-29 02:55:05 | [diff] [blame] | 68 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 69 | void OnCertificateRequested(URLRequest* request, |
| 70 | SSLCertRequestInfo* cert_request_info) override; |
[email protected] | efa9e73 | 2013-11-29 02:55:05 | [diff] [blame] | 71 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 72 | void OnSSLCertificateError(URLRequest* request, |
| 73 | const SSLInfo& ssl_info, |
| 74 | bool fatal) override; |
[email protected] | efa9e73 | 2013-11-29 02:55:05 | [diff] [blame] | 75 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 76 | void OnReadCompleted(URLRequest* request, int bytes_read) override; |
[email protected] | efa9e73 | 2013-11-29 02:55:05 | [diff] [blame] | 77 | |
| 78 | private: |
tyoshino | ccfcfde | 2016-07-21 14:06:55 | [diff] [blame] | 79 | WebSocketStreamRequestImpl* owner_; |
[email protected] | 1e0a16a | 2014-04-04 16:18:04 | [diff] [blame] | 80 | HandshakeResult result_; |
[email protected] | efa9e73 | 2013-11-29 02:55:05 | [diff] [blame] | 81 | }; |
| 82 | |
tyoshino | ccfcfde | 2016-07-21 14:06:55 | [diff] [blame] | 83 | class WebSocketStreamRequestImpl : public WebSocketStreamRequest { |
[email protected] | efa9e73 | 2013-11-29 02:55:05 | [diff] [blame] | 84 | public: |
tyoshino | ccfcfde | 2016-07-21 14:06:55 | [diff] [blame] | 85 | WebSocketStreamRequestImpl( |
[email protected] | efa9e73 | 2013-11-29 02:55:05 | [diff] [blame] | 86 | const GURL& url, |
| 87 | const URLRequestContext* context, |
mkwst | 4997ce8 | 2015-07-25 12:00:05 | [diff] [blame] | 88 | const url::Origin& origin, |
tyoshino | 8572d57 | 2016-07-13 06:29:48 | [diff] [blame] | 89 | const GURL& first_party_for_cookies, |
allada | cef397d | 2016-06-29 17:52:23 | [diff] [blame] | 90 | const std::string& additional_headers, |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 91 | std::unique_ptr<WebSocketStream::ConnectDelegate> connect_delegate, |
| 92 | std::unique_ptr<WebSocketHandshakeStreamCreateHelper> create_helper) |
[email protected] | efa9e73 | 2013-11-29 02:55:05 | [diff] [blame] | 93 | : delegate_(new Delegate(this)), |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 94 | url_request_( |
| 95 | context->CreateRequest(url, DEFAULT_PRIORITY, delegate_.get())), |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 96 | connect_delegate_(std::move(connect_delegate)), |
tyoshino | ccfcfde | 2016-07-21 14:06:55 | [diff] [blame] | 97 | handshake_stream_create_helper_(create_helper.release()), |
| 98 | handshake_stream_(nullptr) { |
| 99 | handshake_stream_create_helper_->set_stream_request(this); |
[email protected] | 490e38f4 | 2014-05-26 23:44:16 | [diff] [blame] | 100 | HttpRequestHeaders headers; |
| 101 | headers.SetHeader(websockets::kUpgrade, websockets::kWebSocketLowercase); |
| 102 | headers.SetHeader(HttpRequestHeaders::kConnection, websockets::kUpgrade); |
mkwst | 4997ce8 | 2015-07-25 12:00:05 | [diff] [blame] | 103 | headers.SetHeader(HttpRequestHeaders::kOrigin, origin.Serialize()); |
[email protected] | 490e38f4 | 2014-05-26 23:44:16 | [diff] [blame] | 104 | headers.SetHeader(websockets::kSecWebSocketVersion, |
| 105 | websockets::kSupportedVersion); |
allada | cef397d | 2016-06-29 17:52:23 | [diff] [blame] | 106 | |
| 107 | headers.AddHeadersFromString(additional_headers); |
| 108 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 109 | url_request_->SetExtraRequestHeaders(headers); |
tyoshino | 8572d57 | 2016-07-13 06:29:48 | [diff] [blame] | 110 | url_request_->set_initiator(origin); |
| 111 | url_request_->set_first_party_for_cookies(first_party_for_cookies); |
[email protected] | 490e38f4 | 2014-05-26 23:44:16 | [diff] [blame] | 112 | |
tyoshino | ccfcfde | 2016-07-21 14:06:55 | [diff] [blame] | 113 | // This passes the ownership of |handshake_stream_create_helper_| to |
| 114 | // |url_request_|. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 115 | url_request_->SetUserData( |
[email protected] | 490e38f4 | 2014-05-26 23:44:16 | [diff] [blame] | 116 | WebSocketHandshakeStreamBase::CreateHelper::DataKey(), |
tyoshino | ccfcfde | 2016-07-21 14:06:55 | [diff] [blame] | 117 | handshake_stream_create_helper_); |
baranovich | 1b32ffb | 2015-01-15 14:44:52 | [diff] [blame] | 118 | url_request_->SetLoadFlags(LOAD_DISABLE_CACHE | LOAD_BYPASS_CACHE); |
[email protected] | 490e38f4 | 2014-05-26 23:44:16 | [diff] [blame] | 119 | } |
[email protected] | efa9e73 | 2013-11-29 02:55:05 | [diff] [blame] | 120 | |
| 121 | // Destroying this object destroys the URLRequest, which cancels the request |
| 122 | // and so terminates the handshake if it is incomplete. |
tyoshino | ccfcfde | 2016-07-21 14:06:55 | [diff] [blame] | 123 | ~WebSocketStreamRequestImpl() override {} |
| 124 | |
| 125 | void OnHandshakeStreamCreated( |
| 126 | WebSocketHandshakeStreamBase* handshake_stream) override { |
| 127 | handshake_stream_ = handshake_stream; |
| 128 | } |
| 129 | |
| 130 | void OnFailure(const std::string& message) override { |
| 131 | failure_message_ = message; |
| 132 | } |
[email protected] | efa9e73 | 2013-11-29 02:55:05 | [diff] [blame] | 133 | |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 134 | void Start(std::unique_ptr<base::Timer> timer) { |
yhirano | 569e09ce | 2014-09-11 12:07:29 | [diff] [blame] | 135 | DCHECK(timer); |
brettw | bc44c0a9 | 2015-02-20 22:30:39 | [diff] [blame] | 136 | base::TimeDelta timeout(base::TimeDelta::FromSeconds( |
yhirano | 569e09ce | 2014-09-11 12:07:29 | [diff] [blame] | 137 | kHandshakeTimeoutIntervalInSeconds)); |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 138 | timer_ = std::move(timer); |
yhirano | 569e09ce | 2014-09-11 12:07:29 | [diff] [blame] | 139 | timer_->Start(FROM_HERE, timeout, |
tyoshino | ccfcfde | 2016-07-21 14:06:55 | [diff] [blame] | 140 | base::Bind(&WebSocketStreamRequestImpl::OnTimeout, |
yhirano | 569e09ce | 2014-09-11 12:07:29 | [diff] [blame] | 141 | base::Unretained(this))); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 142 | url_request_->Start(); |
[email protected] | 490e38f4 | 2014-05-26 23:44:16 | [diff] [blame] | 143 | } |
[email protected] | efa9e73 | 2013-11-29 02:55:05 | [diff] [blame] | 144 | |
| 145 | void PerformUpgrade() { |
yhirano | 569e09ce | 2014-09-11 12:07:29 | [diff] [blame] | 146 | DCHECK(timer_); |
tyoshino | ccfcfde | 2016-07-21 14:06:55 | [diff] [blame] | 147 | DCHECK(handshake_stream_); |
| 148 | |
yhirano | 569e09ce | 2014-09-11 12:07:29 | [diff] [blame] | 149 | timer_->Stop(); |
tyoshino | ccfcfde | 2016-07-21 14:06:55 | [diff] [blame] | 150 | |
| 151 | WebSocketHandshakeStreamBase* handshake_stream = handshake_stream_; |
| 152 | handshake_stream_ = nullptr; |
| 153 | connect_delegate_->OnSuccess(handshake_stream->Upgrade()); |
[email protected] | efa9e73 | 2013-11-29 02:55:05 | [diff] [blame] | 154 | } |
| 155 | |
mmenke | fe5d0b11 | 2016-09-06 20:46:58 | [diff] [blame^] | 156 | std::string FailureMessageFromNetError() { |
| 157 | int error = url_request_->status().error(); |
| 158 | if (error == ERR_TUNNEL_CONNECTION_FAILED) { |
ricea | 38fc268c | 2015-02-09 02:41:29 | [diff] [blame] | 159 | // This error is common and confusing, so special-case it. |
| 160 | // TODO(ricea): Include the HostPortPair of the selected proxy server in |
| 161 | // the error message. This is not currently possible because it isn't set |
| 162 | // in HttpResponseInfo when a ERR_TUNNEL_CONNECTION_FAILED error happens. |
| 163 | return "Establishing a tunnel via proxy server failed."; |
| 164 | } else { |
| 165 | return std::string("Error in connection establishment: ") + |
mmenke | fe5d0b11 | 2016-09-06 20:46:58 | [diff] [blame^] | 166 | ErrorToString(url_request_->status().error()); |
ricea | 38fc268c | 2015-02-09 02:41:29 | [diff] [blame] | 167 | } |
| 168 | } |
| 169 | |
mmenke | fe5d0b11 | 2016-09-06 20:46:58 | [diff] [blame^] | 170 | void ReportFailure() { |
yhirano | 569e09ce | 2014-09-11 12:07:29 | [diff] [blame] | 171 | DCHECK(timer_); |
| 172 | timer_->Stop(); |
[email protected] | 8aba017 | 2014-07-03 12:09:53 | [diff] [blame] | 173 | if (failure_message_.empty()) { |
mmenke | fe5d0b11 | 2016-09-06 20:46:58 | [diff] [blame^] | 174 | switch (url_request_->status().status()) { |
| 175 | case URLRequestStatus::SUCCESS: |
| 176 | case URLRequestStatus::IO_PENDING: |
[email protected] | 9686820 | 2014-01-09 10:38:04 | [diff] [blame] | 177 | break; |
mmenke | fe5d0b11 | 2016-09-06 20:46:58 | [diff] [blame^] | 178 | case URLRequestStatus::CANCELED: |
| 179 | if (url_request_->status().error() == ERR_TIMED_OUT) |
| 180 | failure_message_ = "WebSocket opening handshake timed out"; |
| 181 | else |
| 182 | failure_message_ = "WebSocket opening handshake was canceled"; |
[email protected] | 9686820 | 2014-01-09 10:38:04 | [diff] [blame] | 183 | break; |
mmenke | fe5d0b11 | 2016-09-06 20:46:58 | [diff] [blame^] | 184 | case URLRequestStatus::FAILED: |
| 185 | failure_message_ = FailureMessageFromNetError(); |
[email protected] | 9686820 | 2014-01-09 10:38:04 | [diff] [blame] | 186 | break; |
| 187 | } |
| 188 | } |
[email protected] | e69c1cd | 2014-07-29 07:42:29 | [diff] [blame] | 189 | ReportFailureWithMessage(failure_message_); |
| 190 | } |
| 191 | |
| 192 | void ReportFailureWithMessage(const std::string& failure_message) { |
| 193 | connect_delegate_->OnFailure(failure_message); |
| 194 | } |
| 195 | |
| 196 | void OnFinishOpeningHandshake() { |
| 197 | WebSocketDispatchOnFinishOpeningHandshake(connect_delegate(), |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 198 | url_request_->url(), |
| 199 | url_request_->response_headers(), |
| 200 | url_request_->response_time()); |
[email protected] | efa9e73 | 2013-11-29 02:55:05 | [diff] [blame] | 201 | } |
| 202 | |
[email protected] | a6244952 | 2014-06-05 11:11:15 | [diff] [blame] | 203 | WebSocketStream::ConnectDelegate* connect_delegate() const { |
| 204 | return connect_delegate_.get(); |
| 205 | } |
| 206 | |
yhirano | 569e09ce | 2014-09-11 12:07:29 | [diff] [blame] | 207 | void OnTimeout() { |
| 208 | url_request_->CancelWithError(ERR_TIMED_OUT); |
| 209 | } |
| 210 | |
[email protected] | efa9e73 | 2013-11-29 02:55:05 | [diff] [blame] | 211 | private: |
| 212 | // |delegate_| needs to be declared before |url_request_| so that it gets |
| 213 | // initialised first. |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 214 | std::unique_ptr<Delegate> delegate_; |
[email protected] | efa9e73 | 2013-11-29 02:55:05 | [diff] [blame] | 215 | |
tyoshino | ccfcfde | 2016-07-21 14:06:55 | [diff] [blame] | 216 | // Deleting the WebSocketStreamRequestImpl object deletes this URLRequest |
| 217 | // object, cancelling the whole connection. |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 218 | std::unique_ptr<URLRequest> url_request_; |
[email protected] | efa9e73 | 2013-11-29 02:55:05 | [diff] [blame] | 219 | |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 220 | std::unique_ptr<WebSocketStream::ConnectDelegate> connect_delegate_; |
[email protected] | efa9e73 | 2013-11-29 02:55:05 | [diff] [blame] | 221 | |
| 222 | // Owned by the URLRequest. |
tyoshino | ccfcfde | 2016-07-21 14:06:55 | [diff] [blame] | 223 | WebSocketHandshakeStreamCreateHelper* handshake_stream_create_helper_; |
| 224 | |
| 225 | // This is owned by the caller of CreateBaseStream() or |
| 226 | // CreateSpdyStream() of WebsocketHandshakeStreamCreateHelper. Both the |
| 227 | // stream and this object will be destroyed during the destruction of the |
| 228 | // URLRequest object associated with the handshake. This is only guaranteed |
| 229 | // to be a valid pointer if the handshake succeeded. |
| 230 | WebSocketHandshakeStreamBase* handshake_stream_; |
[email protected] | 8aba017 | 2014-07-03 12:09:53 | [diff] [blame] | 231 | |
| 232 | // The failure message supplied by WebSocketBasicHandshakeStream, if any. |
| 233 | std::string failure_message_; |
yhirano | 569e09ce | 2014-09-11 12:07:29 | [diff] [blame] | 234 | |
| 235 | // A timer for handshake timeout. |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 236 | std::unique_ptr<base::Timer> timer_; |
[email protected] | efa9e73 | 2013-11-29 02:55:05 | [diff] [blame] | 237 | }; |
| 238 | |
[email protected] | a6244952 | 2014-06-05 11:11:15 | [diff] [blame] | 239 | class SSLErrorCallbacks : public WebSocketEventInterface::SSLErrorCallbacks { |
| 240 | public: |
| 241 | explicit SSLErrorCallbacks(URLRequest* url_request) |
| 242 | : url_request_(url_request) {} |
| 243 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 244 | void CancelSSLRequest(int error, const SSLInfo* ssl_info) override { |
[email protected] | a6244952 | 2014-06-05 11:11:15 | [diff] [blame] | 245 | if (ssl_info) { |
| 246 | url_request_->CancelWithSSLError(error, *ssl_info); |
| 247 | } else { |
| 248 | url_request_->CancelWithError(error); |
| 249 | } |
| 250 | } |
| 251 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 252 | void ContinueSSLRequest() override { |
[email protected] | a6244952 | 2014-06-05 11:11:15 | [diff] [blame] | 253 | url_request_->ContinueDespiteLastError(); |
| 254 | } |
| 255 | |
| 256 | private: |
| 257 | URLRequest* url_request_; |
| 258 | }; |
| 259 | |
Adam Rice | cb76ac6 | 2015-02-20 05:33:25 | [diff] [blame] | 260 | void Delegate::OnReceivedRedirect(URLRequest* request, |
| 261 | const RedirectInfo& redirect_info, |
| 262 | bool* defer_redirect) { |
| 263 | // This code should never be reached for externally generated redirects, |
| 264 | // as WebSocketBasicHandshakeStream is responsible for filtering out |
| 265 | // all response codes besides 101, 401, and 407. As such, the URLRequest |
| 266 | // should never see a redirect sent over the network. However, internal |
| 267 | // redirects also result in this method being called, such as those |
| 268 | // caused by HSTS. |
| 269 | // Because it's security critical to prevent externally-generated |
| 270 | // redirects in WebSockets, perform additional checks to ensure this |
| 271 | // is only internal. |
| 272 | GURL::Replacements replacements; |
| 273 | replacements.SetSchemeStr("wss"); |
| 274 | GURL expected_url = request->original_url().ReplaceComponents(replacements); |
| 275 | if (redirect_info.new_method != "GET" || |
| 276 | redirect_info.new_url != expected_url) { |
| 277 | // This should not happen. |
| 278 | DLOG(FATAL) << "Unauthorized WebSocket redirect to " |
| 279 | << redirect_info.new_method << " " |
| 280 | << redirect_info.new_url.spec(); |
| 281 | request->Cancel(); |
| 282 | } |
| 283 | } |
| 284 | |
mmenke | fe5d0b11 | 2016-09-06 20:46:58 | [diff] [blame^] | 285 | void Delegate::OnResponseStarted(URLRequest* request) { |
[email protected] | f7e98ca | 2014-06-19 12:05:43 | [diff] [blame] | 286 | // All error codes, including OK and ABORTED, as with |
| 287 | // Net.ErrorCodesForMainFrame3 |
mmenke | fe5d0b11 | 2016-09-06 20:46:58 | [diff] [blame^] | 288 | UMA_HISTOGRAM_SPARSE_SLOWLY("Net.WebSocket.ErrorCodes", |
| 289 | -request->status().error()); |
| 290 | if (!request->status().is_success()) { |
[email protected] | a6244952 | 2014-06-05 11:11:15 | [diff] [blame] | 291 | DVLOG(3) << "OnResponseStarted (request failed)"; |
mmenke | fe5d0b11 | 2016-09-06 20:46:58 | [diff] [blame^] | 292 | owner_->ReportFailure(); |
[email protected] | a6244952 | 2014-06-05 11:11:15 | [diff] [blame] | 293 | return; |
| 294 | } |
[email protected] | f7e98ca | 2014-06-19 12:05:43 | [diff] [blame] | 295 | const int response_code = request->GetResponseCode(); |
| 296 | DVLOG(3) << "OnResponseStarted (response code " << response_code << ")"; |
| 297 | switch (response_code) { |
[email protected] | efa9e73 | 2013-11-29 02:55:05 | [diff] [blame] | 298 | case HTTP_SWITCHING_PROTOCOLS: |
[email protected] | 1e0a16a | 2014-04-04 16:18:04 | [diff] [blame] | 299 | result_ = CONNECTED; |
[email protected] | efa9e73 | 2013-11-29 02:55:05 | [diff] [blame] | 300 | owner_->PerformUpgrade(); |
| 301 | return; |
| 302 | |
| 303 | case HTTP_UNAUTHORIZED: |
[email protected] | e69c1cd | 2014-07-29 07:42:29 | [diff] [blame] | 304 | result_ = FAILED; |
| 305 | owner_->OnFinishOpeningHandshake(); |
| 306 | owner_->ReportFailureWithMessage( |
| 307 | "HTTP Authentication failed; no valid credentials available"); |
| 308 | return; |
| 309 | |
[email protected] | efa9e73 | 2013-11-29 02:55:05 | [diff] [blame] | 310 | case HTTP_PROXY_AUTHENTICATION_REQUIRED: |
[email protected] | e69c1cd | 2014-07-29 07:42:29 | [diff] [blame] | 311 | result_ = FAILED; |
| 312 | owner_->OnFinishOpeningHandshake(); |
| 313 | owner_->ReportFailureWithMessage("Proxy authentication failed"); |
[email protected] | efa9e73 | 2013-11-29 02:55:05 | [diff] [blame] | 314 | return; |
| 315 | |
| 316 | default: |
[email protected] | 1e0a16a | 2014-04-04 16:18:04 | [diff] [blame] | 317 | result_ = FAILED; |
mmenke | fe5d0b11 | 2016-09-06 20:46:58 | [diff] [blame^] | 318 | owner_->ReportFailure(); |
[email protected] | efa9e73 | 2013-11-29 02:55:05 | [diff] [blame] | 319 | } |
| 320 | } |
| 321 | |
| 322 | void Delegate::OnAuthRequired(URLRequest* request, |
| 323 | AuthChallengeInfo* auth_info) { |
[email protected] | a6244952 | 2014-06-05 11:11:15 | [diff] [blame] | 324 | // This should only be called if credentials are not already stored. |
[email protected] | efa9e73 | 2013-11-29 02:55:05 | [diff] [blame] | 325 | request->CancelAuth(); |
| 326 | } |
| 327 | |
| 328 | void Delegate::OnCertificateRequested(URLRequest* request, |
| 329 | SSLCertRequestInfo* cert_request_info) { |
[email protected] | a6244952 | 2014-06-05 11:11:15 | [diff] [blame] | 330 | // This method is called when a client certificate is requested, and the |
| 331 | // request context does not already contain a client certificate selection for |
| 332 | // the endpoint. In this case, a main frame resource request would pop-up UI |
| 333 | // to permit selection of a client certificate, but since WebSockets are |
| 334 | // sub-resources they should not pop-up UI and so there is nothing more we can |
| 335 | // do. |
| 336 | request->Cancel(); |
[email protected] | efa9e73 | 2013-11-29 02:55:05 | [diff] [blame] | 337 | } |
| 338 | |
| 339 | void Delegate::OnSSLCertificateError(URLRequest* request, |
| 340 | const SSLInfo& ssl_info, |
| 341 | bool fatal) { |
[email protected] | a6244952 | 2014-06-05 11:11:15 | [diff] [blame] | 342 | owner_->connect_delegate()->OnSSLCertificateError( |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 343 | std::unique_ptr<WebSocketEventInterface::SSLErrorCallbacks>( |
[email protected] | a6244952 | 2014-06-05 11:11:15 | [diff] [blame] | 344 | new SSLErrorCallbacks(request)), |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 345 | ssl_info, fatal); |
[email protected] | efa9e73 | 2013-11-29 02:55:05 | [diff] [blame] | 346 | } |
| 347 | |
| 348 | void Delegate::OnReadCompleted(URLRequest* request, int bytes_read) { |
| 349 | NOTREACHED(); |
| 350 | } |
| 351 | |
[email protected] | efa9e73 | 2013-11-29 02:55:05 | [diff] [blame] | 352 | } // namespace |
[email protected] | 473282d | 2013-07-02 11:57:07 | [diff] [blame] | 353 | |
| 354 | WebSocketStreamRequest::~WebSocketStreamRequest() {} |
| 355 | |
| 356 | WebSocketStream::WebSocketStream() {} |
| 357 | WebSocketStream::~WebSocketStream() {} |
| 358 | |
| 359 | WebSocketStream::ConnectDelegate::~ConnectDelegate() {} |
| 360 | |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 361 | std::unique_ptr<WebSocketStreamRequest> WebSocketStream::CreateAndConnectStream( |
[email protected] | 473282d | 2013-07-02 11:57:07 | [diff] [blame] | 362 | const GURL& socket_url, |
tyoshino | ccfcfde | 2016-07-21 14:06:55 | [diff] [blame] | 363 | std::unique_ptr<WebSocketHandshakeStreamCreateHelper> create_helper, |
mkwst | 4997ce8 | 2015-07-25 12:00:05 | [diff] [blame] | 364 | const url::Origin& origin, |
tyoshino | 8572d57 | 2016-07-13 06:29:48 | [diff] [blame] | 365 | const GURL& first_party_for_cookies, |
allada | cef397d | 2016-06-29 17:52:23 | [diff] [blame] | 366 | const std::string& additional_headers, |
[email protected] | 473282d | 2013-07-02 11:57:07 | [diff] [blame] | 367 | URLRequestContext* url_request_context, |
| 368 | const BoundNetLog& net_log, |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 369 | std::unique_ptr<ConnectDelegate> connect_delegate) { |
tyoshino | ccfcfde | 2016-07-21 14:06:55 | [diff] [blame] | 370 | std::unique_ptr<WebSocketStreamRequestImpl> request( |
| 371 | new WebSocketStreamRequestImpl( |
| 372 | socket_url, url_request_context, origin, first_party_for_cookies, |
| 373 | additional_headers, std::move(connect_delegate), |
| 374 | std::move(create_helper))); |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 375 | request->Start(std::unique_ptr<base::Timer>(new base::Timer(false, false))); |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 376 | return std::move(request); |
[email protected] | efa9e73 | 2013-11-29 02:55:05 | [diff] [blame] | 377 | } |
| 378 | |
tyoshino | ccfcfde | 2016-07-21 14:06:55 | [diff] [blame] | 379 | std::unique_ptr<WebSocketStreamRequest> |
| 380 | WebSocketStream::CreateAndConnectStreamForTesting( |
[email protected] | 7824cf8 | 2014-03-13 10:22:57 | [diff] [blame] | 381 | const GURL& socket_url, |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 382 | std::unique_ptr<WebSocketHandshakeStreamCreateHelper> create_helper, |
mkwst | 4997ce8 | 2015-07-25 12:00:05 | [diff] [blame] | 383 | const url::Origin& origin, |
tyoshino | 8572d57 | 2016-07-13 06:29:48 | [diff] [blame] | 384 | const GURL& first_party_for_cookies, |
allada | cef397d | 2016-06-29 17:52:23 | [diff] [blame] | 385 | const std::string& additional_headers, |
[email protected] | 7824cf8 | 2014-03-13 10:22:57 | [diff] [blame] | 386 | URLRequestContext* url_request_context, |
| 387 | const BoundNetLog& net_log, |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 388 | std::unique_ptr<WebSocketStream::ConnectDelegate> connect_delegate, |
| 389 | std::unique_ptr<base::Timer> timer) { |
tyoshino | ccfcfde | 2016-07-21 14:06:55 | [diff] [blame] | 390 | std::unique_ptr<WebSocketStreamRequestImpl> request( |
| 391 | new WebSocketStreamRequestImpl( |
| 392 | socket_url, url_request_context, origin, first_party_for_cookies, |
| 393 | additional_headers, std::move(connect_delegate), |
| 394 | std::move(create_helper))); |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 395 | request->Start(std::move(timer)); |
| 396 | return std::move(request); |
[email protected] | 473282d | 2013-07-02 11:57:07 | [diff] [blame] | 397 | } |
| 398 | |
[email protected] | e69c1cd | 2014-07-29 07:42:29 | [diff] [blame] | 399 | void WebSocketDispatchOnFinishOpeningHandshake( |
| 400 | WebSocketStream::ConnectDelegate* connect_delegate, |
| 401 | const GURL& url, |
| 402 | const scoped_refptr<HttpResponseHeaders>& headers, |
| 403 | base::Time response_time) { |
| 404 | DCHECK(connect_delegate); |
dcheng | b206dc41 | 2014-08-26 19:46:23 | [diff] [blame] | 405 | if (headers.get()) { |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 406 | connect_delegate->OnFinishOpeningHandshake( |
| 407 | base::WrapUnique(new WebSocketHandshakeResponseInfo( |
| 408 | url, headers->response_code(), headers->GetStatusText(), headers, |
| 409 | response_time))); |
[email protected] | e69c1cd | 2014-07-29 07:42:29 | [diff] [blame] | 410 | } |
| 411 | } |
| 412 | |
[email protected] | 473282d | 2013-07-02 11:57:07 | [diff] [blame] | 413 | } // namespace net |