| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "net/websockets/websocket_stream.h" | 5 #include "net/websockets/websocket_stream.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "base/memory/scoped_ptr.h" | 8 #include "base/memory/scoped_ptr.h" |
| 9 #include "base/metrics/histogram.h" | 9 #include "base/metrics/histogram.h" |
| 10 #include "base/metrics/sparse_histogram.h" | 10 #include "base/metrics/sparse_histogram.h" |
| 11 #include "base/time/time.h" |
| 12 #include "base/timer/timer.h" |
| 11 #include "net/base/load_flags.h" | 13 #include "net/base/load_flags.h" |
| 12 #include "net/http/http_request_headers.h" | 14 #include "net/http/http_request_headers.h" |
| 13 #include "net/http/http_response_headers.h" | 15 #include "net/http/http_response_headers.h" |
| 14 #include "net/http/http_status_code.h" | 16 #include "net/http/http_status_code.h" |
| 15 #include "net/url_request/redirect_info.h" | 17 #include "net/url_request/redirect_info.h" |
| 16 #include "net/url_request/url_request.h" | 18 #include "net/url_request/url_request.h" |
| 17 #include "net/url_request/url_request_context.h" | 19 #include "net/url_request/url_request_context.h" |
| 18 #include "net/websockets/websocket_errors.h" | 20 #include "net/websockets/websocket_errors.h" |
| 19 #include "net/websockets/websocket_event_interface.h" | 21 #include "net/websockets/websocket_event_interface.h" |
| 20 #include "net/websockets/websocket_handshake_constants.h" | 22 #include "net/websockets/websocket_handshake_constants.h" |
| 21 #include "net/websockets/websocket_handshake_stream_base.h" | 23 #include "net/websockets/websocket_handshake_stream_base.h" |
| 22 #include "net/websockets/websocket_handshake_stream_create_helper.h" | 24 #include "net/websockets/websocket_handshake_stream_create_helper.h" |
| 23 #include "net/websockets/websocket_test_util.h" | 25 #include "net/websockets/websocket_test_util.h" |
| 24 #include "url/gurl.h" | 26 #include "url/gurl.h" |
| 25 #include "url/origin.h" | 27 #include "url/origin.h" |
| 26 | 28 |
| 27 namespace net { | 29 namespace net { |
| 28 namespace { | 30 namespace { |
| 29 | 31 |
| 32 // The timeout duration of WebSocket handshake. |
| 33 // It is defined as the same value as the TCP connection timeout value in |
| 34 // net/socket/websocket_transport_client_socket_pool.cc to make it hard for |
| 35 // JavaScript programs to recognize the timeout cause. |
| 36 const int kHandshakeTimeoutIntervalInSeconds = 240; |
| 37 |
| 30 class StreamRequestImpl; | 38 class StreamRequestImpl; |
| 31 | 39 |
| 32 class Delegate : public URLRequest::Delegate { | 40 class Delegate : public URLRequest::Delegate { |
| 33 public: | 41 public: |
| 34 enum HandshakeResult { | 42 enum HandshakeResult { |
| 35 INCOMPLETE, | 43 INCOMPLETE, |
| 36 CONNECTED, | 44 CONNECTED, |
| 37 FAILED, | 45 FAILED, |
| 38 NUM_HANDSHAKE_RESULT_TYPES, | 46 NUM_HANDSHAKE_RESULT_TYPES, |
| 39 }; | 47 }; |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 105 create_helper_); | 113 create_helper_); |
| 106 url_request_->SetLoadFlags(LOAD_DISABLE_CACHE | | 114 url_request_->SetLoadFlags(LOAD_DISABLE_CACHE | |
| 107 LOAD_BYPASS_CACHE | | 115 LOAD_BYPASS_CACHE | |
| 108 LOAD_DO_NOT_PROMPT_FOR_LOGIN); | 116 LOAD_DO_NOT_PROMPT_FOR_LOGIN); |
| 109 } | 117 } |
| 110 | 118 |
| 111 // Destroying this object destroys the URLRequest, which cancels the request | 119 // Destroying this object destroys the URLRequest, which cancels the request |
| 112 // and so terminates the handshake if it is incomplete. | 120 // and so terminates the handshake if it is incomplete. |
| 113 virtual ~StreamRequestImpl() {} | 121 virtual ~StreamRequestImpl() {} |
| 114 | 122 |
| 115 void Start() { | 123 void Start(scoped_ptr<base::Timer> timer) { |
| 124 DCHECK(timer); |
| 125 TimeDelta timeout(TimeDelta::FromSeconds( |
| 126 kHandshakeTimeoutIntervalInSeconds)); |
| 127 timer_ = timer.Pass(); |
| 128 timer_->Start(FROM_HERE, timeout, |
| 129 base::Bind(&StreamRequestImpl::OnTimeout, |
| 130 base::Unretained(this))); |
| 116 url_request_->Start(); | 131 url_request_->Start(); |
| 117 } | 132 } |
| 118 | 133 |
| 119 void PerformUpgrade() { | 134 void PerformUpgrade() { |
| 135 DCHECK(timer_); |
| 136 timer_->Stop(); |
| 120 connect_delegate_->OnSuccess(create_helper_->Upgrade()); | 137 connect_delegate_->OnSuccess(create_helper_->Upgrade()); |
| 121 } | 138 } |
| 122 | 139 |
| 123 void ReportFailure() { | 140 void ReportFailure() { |
| 141 DCHECK(timer_); |
| 142 timer_->Stop(); |
| 124 if (failure_message_.empty()) { | 143 if (failure_message_.empty()) { |
| 125 switch (url_request_->status().status()) { | 144 switch (url_request_->status().status()) { |
| 126 case URLRequestStatus::SUCCESS: | 145 case URLRequestStatus::SUCCESS: |
| 127 case URLRequestStatus::IO_PENDING: | 146 case URLRequestStatus::IO_PENDING: |
| 128 break; | 147 break; |
| 129 case URLRequestStatus::CANCELED: | 148 case URLRequestStatus::CANCELED: |
| 130 failure_message_ = "WebSocket opening handshake was canceled"; | 149 if (url_request_->status().error() == ERR_TIMED_OUT) |
| 150 failure_message_ = "WebSocket opening handshake timed out"; |
| 151 else |
| 152 failure_message_ = "WebSocket opening handshake was canceled"; |
| 131 break; | 153 break; |
| 132 case URLRequestStatus::FAILED: | 154 case URLRequestStatus::FAILED: |
| 133 failure_message_ = | 155 failure_message_ = |
| 134 std::string("Error in connection establishment: ") + | 156 std::string("Error in connection establishment: ") + |
| 135 ErrorToString(url_request_->status().error()); | 157 ErrorToString(url_request_->status().error()); |
| 136 break; | 158 break; |
| 137 } | 159 } |
| 138 } | 160 } |
| 139 ReportFailureWithMessage(failure_message_); | 161 ReportFailureWithMessage(failure_message_); |
| 140 } | 162 } |
| 141 | 163 |
| 142 void ReportFailureWithMessage(const std::string& failure_message) { | 164 void ReportFailureWithMessage(const std::string& failure_message) { |
| 143 connect_delegate_->OnFailure(failure_message); | 165 connect_delegate_->OnFailure(failure_message); |
| 144 } | 166 } |
| 145 | 167 |
| 146 void OnFinishOpeningHandshake() { | 168 void OnFinishOpeningHandshake() { |
| 147 WebSocketDispatchOnFinishOpeningHandshake(connect_delegate(), | 169 WebSocketDispatchOnFinishOpeningHandshake(connect_delegate(), |
| 148 url_request_->url(), | 170 url_request_->url(), |
| 149 url_request_->response_headers(), | 171 url_request_->response_headers(), |
| 150 url_request_->response_time()); | 172 url_request_->response_time()); |
| 151 } | 173 } |
| 152 | 174 |
| 153 WebSocketStream::ConnectDelegate* connect_delegate() const { | 175 WebSocketStream::ConnectDelegate* connect_delegate() const { |
| 154 return connect_delegate_.get(); | 176 return connect_delegate_.get(); |
| 155 } | 177 } |
| 156 | 178 |
| 179 void OnTimeout() { |
| 180 url_request_->CancelWithError(ERR_TIMED_OUT); |
| 181 } |
| 182 |
| 157 private: | 183 private: |
| 158 // |delegate_| needs to be declared before |url_request_| so that it gets | 184 // |delegate_| needs to be declared before |url_request_| so that it gets |
| 159 // initialised first. | 185 // initialised first. |
| 160 scoped_ptr<Delegate> delegate_; | 186 scoped_ptr<Delegate> delegate_; |
| 161 | 187 |
| 162 // Deleting the StreamRequestImpl object deletes this URLRequest object, | 188 // Deleting the StreamRequestImpl object deletes this URLRequest object, |
| 163 // cancelling the whole connection. | 189 // cancelling the whole connection. |
| 164 scoped_ptr<URLRequest> url_request_; | 190 scoped_ptr<URLRequest> url_request_; |
| 165 | 191 |
| 166 scoped_ptr<WebSocketStream::ConnectDelegate> connect_delegate_; | 192 scoped_ptr<WebSocketStream::ConnectDelegate> connect_delegate_; |
| 167 | 193 |
| 168 // Owned by the URLRequest. | 194 // Owned by the URLRequest. |
| 169 WebSocketHandshakeStreamCreateHelper* create_helper_; | 195 WebSocketHandshakeStreamCreateHelper* create_helper_; |
| 170 | 196 |
| 171 // The failure message supplied by WebSocketBasicHandshakeStream, if any. | 197 // The failure message supplied by WebSocketBasicHandshakeStream, if any. |
| 172 std::string failure_message_; | 198 std::string failure_message_; |
| 199 |
| 200 // A timer for handshake timeout. |
| 201 scoped_ptr<base::Timer> timer_; |
| 173 }; | 202 }; |
| 174 | 203 |
| 175 class SSLErrorCallbacks : public WebSocketEventInterface::SSLErrorCallbacks { | 204 class SSLErrorCallbacks : public WebSocketEventInterface::SSLErrorCallbacks { |
| 176 public: | 205 public: |
| 177 explicit SSLErrorCallbacks(URLRequest* url_request) | 206 explicit SSLErrorCallbacks(URLRequest* url_request) |
| 178 : url_request_(url_request) {} | 207 : url_request_(url_request) {} |
| 179 | 208 |
| 180 virtual void CancelSSLRequest(int error, const SSLInfo* ssl_info) OVERRIDE { | 209 virtual void CancelSSLRequest(int error, const SSLInfo* ssl_info) OVERRIDE { |
| 181 if (ssl_info) { | 210 if (ssl_info) { |
| 182 url_request_->CancelWithSSLError(error, *ssl_info); | 211 url_request_->CancelWithSSLError(error, *ssl_info); |
| (...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 279 scoped_ptr<ConnectDelegate> connect_delegate) { | 308 scoped_ptr<ConnectDelegate> connect_delegate) { |
| 280 scoped_ptr<WebSocketHandshakeStreamCreateHelper> create_helper( | 309 scoped_ptr<WebSocketHandshakeStreamCreateHelper> create_helper( |
| 281 new WebSocketHandshakeStreamCreateHelper(connect_delegate.get(), | 310 new WebSocketHandshakeStreamCreateHelper(connect_delegate.get(), |
| 282 requested_subprotocols)); | 311 requested_subprotocols)); |
| 283 scoped_ptr<StreamRequestImpl> request( | 312 scoped_ptr<StreamRequestImpl> request( |
| 284 new StreamRequestImpl(socket_url, | 313 new StreamRequestImpl(socket_url, |
| 285 url_request_context, | 314 url_request_context, |
| 286 origin, | 315 origin, |
| 287 connect_delegate.Pass(), | 316 connect_delegate.Pass(), |
| 288 create_helper.Pass())); | 317 create_helper.Pass())); |
| 289 request->Start(); | 318 request->Start(scoped_ptr<base::Timer>(new base::Timer(false, false))); |
| 290 return request.PassAs<WebSocketStreamRequest>(); | 319 return request.PassAs<WebSocketStreamRequest>(); |
| 291 } | 320 } |
| 292 | 321 |
| 293 // This is declared in websocket_test_util.h. | 322 // This is declared in websocket_test_util.h. |
| 294 scoped_ptr<WebSocketStreamRequest> CreateAndConnectStreamForTesting( | 323 scoped_ptr<WebSocketStreamRequest> CreateAndConnectStreamForTesting( |
| 295 const GURL& socket_url, | 324 const GURL& socket_url, |
| 296 scoped_ptr<WebSocketHandshakeStreamCreateHelper> create_helper, | 325 scoped_ptr<WebSocketHandshakeStreamCreateHelper> create_helper, |
| 297 const url::Origin& origin, | 326 const url::Origin& origin, |
| 298 URLRequestContext* url_request_context, | 327 URLRequestContext* url_request_context, |
| 299 const BoundNetLog& net_log, | 328 const BoundNetLog& net_log, |
| 300 scoped_ptr<WebSocketStream::ConnectDelegate> connect_delegate) { | 329 scoped_ptr<WebSocketStream::ConnectDelegate> connect_delegate, |
| 330 scoped_ptr<base::Timer> timer) { |
| 301 scoped_ptr<StreamRequestImpl> request( | 331 scoped_ptr<StreamRequestImpl> request( |
| 302 new StreamRequestImpl(socket_url, | 332 new StreamRequestImpl(socket_url, |
| 303 url_request_context, | 333 url_request_context, |
| 304 origin, | 334 origin, |
| 305 connect_delegate.Pass(), | 335 connect_delegate.Pass(), |
| 306 create_helper.Pass())); | 336 create_helper.Pass())); |
| 307 request->Start(); | 337 request->Start(timer.Pass()); |
| 308 return request.PassAs<WebSocketStreamRequest>(); | 338 return request.PassAs<WebSocketStreamRequest>(); |
| 309 } | 339 } |
| 310 | 340 |
| 311 void WebSocketDispatchOnFinishOpeningHandshake( | 341 void WebSocketDispatchOnFinishOpeningHandshake( |
| 312 WebSocketStream::ConnectDelegate* connect_delegate, | 342 WebSocketStream::ConnectDelegate* connect_delegate, |
| 313 const GURL& url, | 343 const GURL& url, |
| 314 const scoped_refptr<HttpResponseHeaders>& headers, | 344 const scoped_refptr<HttpResponseHeaders>& headers, |
| 315 base::Time response_time) { | 345 base::Time response_time) { |
| 316 DCHECK(connect_delegate); | 346 DCHECK(connect_delegate); |
| 317 if (headers.get()) { | 347 if (headers.get()) { |
| 318 connect_delegate->OnFinishOpeningHandshake(make_scoped_ptr( | 348 connect_delegate->OnFinishOpeningHandshake(make_scoped_ptr( |
| 319 new WebSocketHandshakeResponseInfo(url, | 349 new WebSocketHandshakeResponseInfo(url, |
| 320 headers->response_code(), | 350 headers->response_code(), |
| 321 headers->GetStatusText(), | 351 headers->GetStatusText(), |
| 322 headers, | 352 headers, |
| 323 response_time))); | 353 response_time))); |
| 324 } | 354 } |
| 325 } | 355 } |
| 326 | 356 |
| 327 } // namespace net | 357 } // namespace net |
| OLD | NEW |