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 "net/base/load_flags.h" | 10 #include "net/base/load_flags.h" |
11 #include "net/http/http_request_headers.h" | 11 #include "net/http/http_request_headers.h" |
12 #include "net/http/http_status_code.h" | 12 #include "net/http/http_status_code.h" |
13 #include "net/url_request/url_request.h" | 13 #include "net/url_request/url_request.h" |
14 #include "net/url_request/url_request_context.h" | 14 #include "net/url_request/url_request_context.h" |
15 #include "net/websockets/websocket_errors.h" | 15 #include "net/websockets/websocket_errors.h" |
16 #include "net/websockets/websocket_event_interface.h" | |
16 #include "net/websockets/websocket_handshake_constants.h" | 17 #include "net/websockets/websocket_handshake_constants.h" |
17 #include "net/websockets/websocket_handshake_stream_base.h" | 18 #include "net/websockets/websocket_handshake_stream_base.h" |
18 #include "net/websockets/websocket_handshake_stream_create_helper.h" | 19 #include "net/websockets/websocket_handshake_stream_create_helper.h" |
19 #include "net/websockets/websocket_test_util.h" | 20 #include "net/websockets/websocket_test_util.h" |
20 #include "url/gurl.h" | 21 #include "url/gurl.h" |
21 #include "url/origin.h" | 22 #include "url/origin.h" |
22 | 23 |
23 namespace net { | 24 namespace net { |
24 namespace { | 25 namespace { |
25 | 26 |
26 class StreamRequestImpl; | 27 class StreamRequestImpl; |
27 | 28 |
28 class Delegate : public URLRequest::Delegate { | 29 class Delegate : public URLRequest::Delegate { |
29 public: | 30 public: |
30 enum HandshakeResult { | 31 enum HandshakeResult { |
31 INCOMPLETE, | 32 INCOMPLETE, |
32 CONNECTED, | 33 CONNECTED, |
33 FAILED, | 34 FAILED, |
34 NUM_HANDSHAKE_RESULT_TYPES, | 35 NUM_HANDSHAKE_RESULT_TYPES, |
35 }; | 36 }; |
36 | 37 |
37 explicit Delegate(StreamRequestImpl* owner) | 38 explicit Delegate(StreamRequestImpl* owner) |
38 : owner_(owner), result_(INCOMPLETE) {} | 39 : owner_(owner), result_(INCOMPLETE) {} |
39 virtual ~Delegate() { | 40 virtual ~Delegate() { |
40 UMA_HISTOGRAM_ENUMERATION( | 41 UMA_HISTOGRAM_ENUMERATION( |
41 "Net.WebSocket.HandshakeResult", result_, NUM_HANDSHAKE_RESULT_TYPES); | 42 "Net.WebSocket.HandshakeResult", result_, NUM_HANDSHAKE_RESULT_TYPES); |
42 } | 43 } |
43 | 44 |
44 // Implementation of URLRequest::Delegate methods. | 45 // Implementation of URLRequest::Delegate methods. |
46 virtual void OnReceivedRedirect(URLRequest* request, | |
tyoshino (SeeGerritForStatus)
2014/06/03 12:11:59
write a comment to roughly explain why we never re
Adam Rice
2014/06/03 12:59:51
Done.
| |
47 const GURL& new_url, | |
48 bool* defer_redirect) OVERRIDE { | |
49 NOTREACHED(); | |
50 } | |
51 | |
45 virtual void OnResponseStarted(URLRequest* request) OVERRIDE; | 52 virtual void OnResponseStarted(URLRequest* request) OVERRIDE; |
46 | 53 |
47 virtual void OnAuthRequired(URLRequest* request, | 54 virtual void OnAuthRequired(URLRequest* request, |
48 AuthChallengeInfo* auth_info) OVERRIDE; | 55 AuthChallengeInfo* auth_info) OVERRIDE; |
49 | 56 |
50 virtual void OnCertificateRequested(URLRequest* request, | 57 virtual void OnCertificateRequested(URLRequest* request, |
51 SSLCertRequestInfo* cert_request_info) | 58 SSLCertRequestInfo* cert_request_info) |
52 OVERRIDE; | 59 OVERRIDE; |
53 | 60 |
54 virtual void OnSSLCertificateError(URLRequest* request, | 61 virtual void OnSSLCertificateError(URLRequest* request, |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
118 case URLRequestStatus::FAILED: | 125 case URLRequestStatus::FAILED: |
119 failure_message = | 126 failure_message = |
120 std::string("Error in connection establishment: ") + | 127 std::string("Error in connection establishment: ") + |
121 ErrorToString(url_request_.status().error()); | 128 ErrorToString(url_request_.status().error()); |
122 break; | 129 break; |
123 } | 130 } |
124 } | 131 } |
125 connect_delegate_->OnFailure(failure_message); | 132 connect_delegate_->OnFailure(failure_message); |
126 } | 133 } |
127 | 134 |
135 WebSocketStream::ConnectDelegate* connect_delegate() const { | |
136 return connect_delegate_.get(); | |
137 } | |
138 | |
128 private: | 139 private: |
129 // |delegate_| needs to be declared before |url_request_| so that it gets | 140 // |delegate_| needs to be declared before |url_request_| so that it gets |
130 // initialised first. | 141 // initialised first. |
131 scoped_ptr<Delegate> delegate_; | 142 scoped_ptr<Delegate> delegate_; |
132 | 143 |
133 // Deleting the StreamRequestImpl object deletes this URLRequest object, | 144 // Deleting the StreamRequestImpl object deletes this URLRequest object, |
134 // cancelling the whole connection. | 145 // cancelling the whole connection. |
135 URLRequest url_request_; | 146 URLRequest url_request_; |
136 | 147 |
137 scoped_ptr<WebSocketStream::ConnectDelegate> connect_delegate_; | 148 scoped_ptr<WebSocketStream::ConnectDelegate> connect_delegate_; |
138 | 149 |
139 // Owned by the URLRequest. | 150 // Owned by the URLRequest. |
140 WebSocketHandshakeStreamCreateHelper* create_helper_; | 151 WebSocketHandshakeStreamCreateHelper* create_helper_; |
141 }; | 152 }; |
142 | 153 |
154 class SSLErrorCallbacks : public WebSocketEventInterface::SSLErrorCallbacks { | |
155 public: | |
156 explicit SSLErrorCallbacks(URLRequest* url_request) | |
157 : url_request_(url_request) {} | |
158 | |
159 virtual void CancelSSLRequest(int error, const SSLInfo* ssl_info) OVERRIDE { | |
160 if (ssl_info) { | |
161 url_request_->CancelWithSSLError(error, *ssl_info); | |
162 } else { | |
163 url_request_->CancelWithError(error); | |
164 } | |
165 } | |
166 | |
167 virtual void ContinueSSLRequest() OVERRIDE { | |
168 url_request_->ContinueDespiteLastError(); | |
169 } | |
170 | |
171 private: | |
172 URLRequest* url_request_; | |
173 }; | |
174 | |
143 void Delegate::OnResponseStarted(URLRequest* request) { | 175 void Delegate::OnResponseStarted(URLRequest* request) { |
176 if (!request->status().is_success()) { | |
177 DVLOG(3) << "OnResponseStarted (request failed)"; | |
178 owner_->ReportFailure(); | |
179 return; | |
180 } | |
181 DVLOG(3) << "OnResponseStarted (response code " << request->GetResponseCode() | |
182 << ")"; | |
144 switch (request->GetResponseCode()) { | 183 switch (request->GetResponseCode()) { |
145 case HTTP_SWITCHING_PROTOCOLS: | 184 case HTTP_SWITCHING_PROTOCOLS: |
146 result_ = CONNECTED; | 185 result_ = CONNECTED; |
147 owner_->PerformUpgrade(); | 186 owner_->PerformUpgrade(); |
148 return; | 187 return; |
149 | 188 |
150 case HTTP_UNAUTHORIZED: | 189 case HTTP_UNAUTHORIZED: |
151 case HTTP_PROXY_AUTHENTICATION_REQUIRED: | 190 case HTTP_PROXY_AUTHENTICATION_REQUIRED: |
152 return; | 191 return; |
153 | 192 |
154 default: | 193 default: |
155 result_ = FAILED; | 194 result_ = FAILED; |
156 owner_->ReportFailure(); | 195 owner_->ReportFailure(); |
157 } | 196 } |
158 } | 197 } |
159 | 198 |
160 void Delegate::OnAuthRequired(URLRequest* request, | 199 void Delegate::OnAuthRequired(URLRequest* request, |
161 AuthChallengeInfo* auth_info) { | 200 AuthChallengeInfo* auth_info) { |
201 // This should only be called if credentials are not already stored. | |
162 request->CancelAuth(); | 202 request->CancelAuth(); |
163 } | 203 } |
164 | 204 |
165 void Delegate::OnCertificateRequested(URLRequest* request, | 205 void Delegate::OnCertificateRequested(URLRequest* request, |
166 SSLCertRequestInfo* cert_request_info) { | 206 SSLCertRequestInfo* cert_request_info) { |
167 request->ContinueWithCertificate(NULL); | 207 // URLRequest should handle the non-interactive cases, so there is nothing |
208 // more for us to do here. | |
tyoshino (SeeGerritForStatus)
2014/06/03 12:11:59
could you please elaborate this comment?
Adam Rice
2014/06/03 12:59:51
Done.
| |
209 request->Cancel(); | |
168 } | 210 } |
169 | 211 |
170 void Delegate::OnSSLCertificateError(URLRequest* request, | 212 void Delegate::OnSSLCertificateError(URLRequest* request, |
171 const SSLInfo& ssl_info, | 213 const SSLInfo& ssl_info, |
172 bool fatal) { | 214 bool fatal) { |
173 request->Cancel(); | 215 owner_->connect_delegate()->OnSSLCertificateError( |
216 scoped_ptr<WebSocketEventInterface::SSLErrorCallbacks>( | |
217 new SSLErrorCallbacks(request)), | |
218 ssl_info, | |
219 fatal); | |
174 } | 220 } |
175 | 221 |
176 void Delegate::OnReadCompleted(URLRequest* request, int bytes_read) { | 222 void Delegate::OnReadCompleted(URLRequest* request, int bytes_read) { |
177 NOTREACHED(); | 223 NOTREACHED(); |
178 } | 224 } |
179 | 225 |
180 } // namespace | 226 } // namespace |
181 | 227 |
182 WebSocketStreamRequest::~WebSocketStreamRequest() {} | 228 WebSocketStreamRequest::~WebSocketStreamRequest() {} |
183 | 229 |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
218 new StreamRequestImpl(socket_url, | 264 new StreamRequestImpl(socket_url, |
219 url_request_context, | 265 url_request_context, |
220 origin, | 266 origin, |
221 connect_delegate.Pass(), | 267 connect_delegate.Pass(), |
222 create_helper.Pass())); | 268 create_helper.Pass())); |
223 request->Start(); | 269 request->Start(); |
224 return request.PassAs<WebSocketStreamRequest>(); | 270 return request.PassAs<WebSocketStreamRequest>(); |
225 } | 271 } |
226 | 272 |
227 } // namespace net | 273 } // namespace net |
OLD | NEW |