[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 | |
| 7 | #include "base/logging.h" |
[email protected] | efa9e73 | 2013-11-29 02:55:05 | [diff] [blame] | 8 | #include "base/memory/scoped_ptr.h" |
[email protected] | 1e0a16a | 2014-04-04 16:18:04 | [diff] [blame] | 9 | #include "base/metrics/histogram.h" |
[email protected] | d759912 | 2014-05-24 03:37:23 | [diff] [blame] | 10 | #include "net/base/load_flags.h" |
[email protected] | efa9e73 | 2013-11-29 02:55:05 | [diff] [blame] | 11 | #include "net/http/http_request_headers.h" |
| 12 | #include "net/http/http_status_code.h" |
| 13 | #include "net/url_request/url_request.h" |
| 14 | #include "net/url_request/url_request_context.h" |
| 15 | #include "net/websockets/websocket_errors.h" |
| 16 | #include "net/websockets/websocket_handshake_constants.h" |
| 17 | #include "net/websockets/websocket_handshake_stream_base.h" |
| 18 | #include "net/websockets/websocket_handshake_stream_create_helper.h" |
| 19 | #include "net/websockets/websocket_test_util.h" |
| 20 | #include "url/gurl.h" |
[email protected] | 7824cf8 | 2014-03-13 10:22:57 | [diff] [blame] | 21 | #include "url/origin.h" |
[email protected] | 473282d | 2013-07-02 11:57:07 | [diff] [blame] | 22 | |
| 23 | namespace net { |
[email protected] | efa9e73 | 2013-11-29 02:55:05 | [diff] [blame] | 24 | namespace { |
| 25 | |
| 26 | class StreamRequestImpl; |
| 27 | |
| 28 | class Delegate : public URLRequest::Delegate { |
| 29 | public: |
[email protected] | 1e0a16a | 2014-04-04 16:18:04 | [diff] [blame] | 30 | enum HandshakeResult { |
| 31 | INCOMPLETE, |
| 32 | CONNECTED, |
| 33 | FAILED, |
| 34 | NUM_HANDSHAKE_RESULT_TYPES, |
| 35 | }; |
| 36 | |
| 37 | explicit Delegate(StreamRequestImpl* owner) |
| 38 | : owner_(owner), result_(INCOMPLETE) {} |
| 39 | virtual ~Delegate() { |
| 40 | UMA_HISTOGRAM_ENUMERATION( |
| 41 | "Net.WebSocket.HandshakeResult", result_, NUM_HANDSHAKE_RESULT_TYPES); |
| 42 | } |
[email protected] | efa9e73 | 2013-11-29 02:55:05 | [diff] [blame] | 43 | |
| 44 | // Implementation of URLRequest::Delegate methods. |
| 45 | virtual void OnResponseStarted(URLRequest* request) OVERRIDE; |
| 46 | |
| 47 | virtual void OnAuthRequired(URLRequest* request, |
| 48 | AuthChallengeInfo* auth_info) OVERRIDE; |
| 49 | |
| 50 | virtual void OnCertificateRequested(URLRequest* request, |
| 51 | SSLCertRequestInfo* cert_request_info) |
| 52 | OVERRIDE; |
| 53 | |
| 54 | virtual void OnSSLCertificateError(URLRequest* request, |
| 55 | const SSLInfo& ssl_info, |
| 56 | bool fatal) OVERRIDE; |
| 57 | |
| 58 | virtual void OnReadCompleted(URLRequest* request, int bytes_read) OVERRIDE; |
| 59 | |
| 60 | private: |
| 61 | StreamRequestImpl* owner_; |
[email protected] | 1e0a16a | 2014-04-04 16:18:04 | [diff] [blame] | 62 | HandshakeResult result_; |
[email protected] | efa9e73 | 2013-11-29 02:55:05 | [diff] [blame] | 63 | }; |
| 64 | |
| 65 | class StreamRequestImpl : public WebSocketStreamRequest { |
| 66 | public: |
| 67 | StreamRequestImpl( |
| 68 | const GURL& url, |
| 69 | const URLRequestContext* context, |
[email protected] | 490e38f4 | 2014-05-26 23:44:16 | [diff] [blame^] | 70 | const url::Origin& origin, |
[email protected] | efa9e73 | 2013-11-29 02:55:05 | [diff] [blame] | 71 | scoped_ptr<WebSocketStream::ConnectDelegate> connect_delegate, |
[email protected] | 490e38f4 | 2014-05-26 23:44:16 | [diff] [blame^] | 72 | scoped_ptr<WebSocketHandshakeStreamCreateHelper> create_helper) |
[email protected] | efa9e73 | 2013-11-29 02:55:05 | [diff] [blame] | 73 | : delegate_(new Delegate(this)), |
| 74 | url_request_(url, DEFAULT_PRIORITY, delegate_.get(), context), |
| 75 | connect_delegate_(connect_delegate.Pass()), |
[email protected] | 490e38f4 | 2014-05-26 23:44:16 | [diff] [blame^] | 76 | create_helper_(create_helper.release()) { |
| 77 | HttpRequestHeaders headers; |
| 78 | headers.SetHeader(websockets::kUpgrade, websockets::kWebSocketLowercase); |
| 79 | headers.SetHeader(HttpRequestHeaders::kConnection, websockets::kUpgrade); |
| 80 | headers.SetHeader(HttpRequestHeaders::kOrigin, origin.string()); |
| 81 | headers.SetHeader(websockets::kSecWebSocketVersion, |
| 82 | websockets::kSupportedVersion); |
| 83 | url_request_.SetExtraRequestHeaders(headers); |
| 84 | |
| 85 | // This passes the ownership of |create_helper_| to |url_request_|. |
| 86 | url_request_.SetUserData( |
| 87 | WebSocketHandshakeStreamBase::CreateHelper::DataKey(), |
| 88 | create_helper_); |
| 89 | url_request_.SetLoadFlags(LOAD_DISABLE_CACHE | |
| 90 | LOAD_BYPASS_CACHE | |
| 91 | LOAD_DO_NOT_PROMPT_FOR_LOGIN); |
| 92 | } |
[email protected] | efa9e73 | 2013-11-29 02:55:05 | [diff] [blame] | 93 | |
| 94 | // Destroying this object destroys the URLRequest, which cancels the request |
| 95 | // and so terminates the handshake if it is incomplete. |
| 96 | virtual ~StreamRequestImpl() {} |
| 97 | |
[email protected] | 490e38f4 | 2014-05-26 23:44:16 | [diff] [blame^] | 98 | void Start() { |
| 99 | url_request_.Start(); |
| 100 | } |
[email protected] | efa9e73 | 2013-11-29 02:55:05 | [diff] [blame] | 101 | |
| 102 | void PerformUpgrade() { |
| 103 | connect_delegate_->OnSuccess(create_helper_->stream()->Upgrade()); |
| 104 | } |
| 105 | |
| 106 | void ReportFailure() { |
[email protected] | 9686820 | 2014-01-09 10:38:04 | [diff] [blame] | 107 | std::string failure_message; |
| 108 | if (create_helper_->stream()) { |
| 109 | failure_message = create_helper_->stream()->GetFailureMessage(); |
| 110 | } else { |
| 111 | switch (url_request_.status().status()) { |
| 112 | case URLRequestStatus::SUCCESS: |
| 113 | case URLRequestStatus::IO_PENDING: |
| 114 | break; |
| 115 | case URLRequestStatus::CANCELED: |
| 116 | failure_message = "WebSocket opening handshake was canceled"; |
| 117 | break; |
| 118 | case URLRequestStatus::FAILED: |
| 119 | failure_message = |
| 120 | std::string("Error in connection establishment: ") + |
| 121 | ErrorToString(url_request_.status().error()); |
| 122 | break; |
| 123 | } |
| 124 | } |
| 125 | connect_delegate_->OnFailure(failure_message); |
[email protected] | efa9e73 | 2013-11-29 02:55:05 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | private: |
| 129 | // |delegate_| needs to be declared before |url_request_| so that it gets |
| 130 | // initialised first. |
| 131 | scoped_ptr<Delegate> delegate_; |
| 132 | |
| 133 | // Deleting the StreamRequestImpl object deletes this URLRequest object, |
| 134 | // cancelling the whole connection. |
| 135 | URLRequest url_request_; |
| 136 | |
| 137 | scoped_ptr<WebSocketStream::ConnectDelegate> connect_delegate_; |
| 138 | |
| 139 | // Owned by the URLRequest. |
| 140 | WebSocketHandshakeStreamCreateHelper* create_helper_; |
| 141 | }; |
| 142 | |
| 143 | void Delegate::OnResponseStarted(URLRequest* request) { |
| 144 | switch (request->GetResponseCode()) { |
| 145 | case HTTP_SWITCHING_PROTOCOLS: |
[email protected] | 1e0a16a | 2014-04-04 16:18:04 | [diff] [blame] | 146 | result_ = CONNECTED; |
[email protected] | efa9e73 | 2013-11-29 02:55:05 | [diff] [blame] | 147 | owner_->PerformUpgrade(); |
| 148 | return; |
| 149 | |
| 150 | case HTTP_UNAUTHORIZED: |
| 151 | case HTTP_PROXY_AUTHENTICATION_REQUIRED: |
| 152 | return; |
| 153 | |
| 154 | default: |
[email protected] | 1e0a16a | 2014-04-04 16:18:04 | [diff] [blame] | 155 | result_ = FAILED; |
[email protected] | efa9e73 | 2013-11-29 02:55:05 | [diff] [blame] | 156 | owner_->ReportFailure(); |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | void Delegate::OnAuthRequired(URLRequest* request, |
| 161 | AuthChallengeInfo* auth_info) { |
| 162 | request->CancelAuth(); |
| 163 | } |
| 164 | |
| 165 | void Delegate::OnCertificateRequested(URLRequest* request, |
| 166 | SSLCertRequestInfo* cert_request_info) { |
| 167 | request->ContinueWithCertificate(NULL); |
| 168 | } |
| 169 | |
| 170 | void Delegate::OnSSLCertificateError(URLRequest* request, |
| 171 | const SSLInfo& ssl_info, |
| 172 | bool fatal) { |
| 173 | request->Cancel(); |
| 174 | } |
| 175 | |
| 176 | void Delegate::OnReadCompleted(URLRequest* request, int bytes_read) { |
| 177 | NOTREACHED(); |
| 178 | } |
| 179 | |
[email protected] | efa9e73 | 2013-11-29 02:55:05 | [diff] [blame] | 180 | } // namespace |
[email protected] | 473282d | 2013-07-02 11:57:07 | [diff] [blame] | 181 | |
| 182 | WebSocketStreamRequest::~WebSocketStreamRequest() {} |
| 183 | |
| 184 | WebSocketStream::WebSocketStream() {} |
| 185 | WebSocketStream::~WebSocketStream() {} |
| 186 | |
| 187 | WebSocketStream::ConnectDelegate::~ConnectDelegate() {} |
| 188 | |
[email protected] | 473282d | 2013-07-02 11:57:07 | [diff] [blame] | 189 | scoped_ptr<WebSocketStreamRequest> WebSocketStream::CreateAndConnectStream( |
| 190 | const GURL& socket_url, |
| 191 | const std::vector<std::string>& requested_subprotocols, |
[email protected] | 7824cf8 | 2014-03-13 10:22:57 | [diff] [blame] | 192 | const url::Origin& origin, |
[email protected] | 473282d | 2013-07-02 11:57:07 | [diff] [blame] | 193 | URLRequestContext* url_request_context, |
| 194 | const BoundNetLog& net_log, |
| 195 | scoped_ptr<ConnectDelegate> connect_delegate) { |
[email protected] | efa9e73 | 2013-11-29 02:55:05 | [diff] [blame] | 196 | scoped_ptr<WebSocketHandshakeStreamCreateHelper> create_helper( |
[email protected] | cd48ed1 | 2014-01-22 14:34:22 | [diff] [blame] | 197 | new WebSocketHandshakeStreamCreateHelper(connect_delegate.get(), |
| 198 | requested_subprotocols)); |
[email protected] | 490e38f4 | 2014-05-26 23:44:16 | [diff] [blame^] | 199 | scoped_ptr<StreamRequestImpl> request( |
| 200 | new StreamRequestImpl(socket_url, |
| 201 | url_request_context, |
| 202 | origin, |
| 203 | connect_delegate.Pass(), |
| 204 | create_helper.Pass())); |
| 205 | request->Start(); |
| 206 | return request.PassAs<WebSocketStreamRequest>(); |
[email protected] | efa9e73 | 2013-11-29 02:55:05 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | // This is declared in websocket_test_util.h. |
| 210 | scoped_ptr<WebSocketStreamRequest> CreateAndConnectStreamForTesting( |
[email protected] | 7824cf8 | 2014-03-13 10:22:57 | [diff] [blame] | 211 | const GURL& socket_url, |
| 212 | scoped_ptr<WebSocketHandshakeStreamCreateHelper> create_helper, |
| 213 | const url::Origin& origin, |
| 214 | URLRequestContext* url_request_context, |
| 215 | const BoundNetLog& net_log, |
| 216 | scoped_ptr<WebSocketStream::ConnectDelegate> connect_delegate) { |
[email protected] | 490e38f4 | 2014-05-26 23:44:16 | [diff] [blame^] | 217 | scoped_ptr<StreamRequestImpl> request( |
| 218 | new StreamRequestImpl(socket_url, |
| 219 | url_request_context, |
| 220 | origin, |
| 221 | connect_delegate.Pass(), |
| 222 | create_helper.Pass())); |
| 223 | request->Start(); |
| 224 | return request.PassAs<WebSocketStreamRequest>(); |
[email protected] | 473282d | 2013-07-02 11:57:07 | [diff] [blame] | 225 | } |
| 226 | |
[email protected] | 473282d | 2013-07-02 11:57:07 | [diff] [blame] | 227 | } // namespace net |