OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/http/http_stream_factory_impl.h" | 5 #include "net/http/http_stream_factory_impl.h" |
6 | 6 |
7 #include <string> | |
8 | |
7 #include "base/string_number_conversions.h" | 9 #include "base/string_number_conversions.h" |
8 #include "base/stl_util.h" | 10 #include "base/stl_util.h" |
9 #include "googleurl/src/gurl.h" | 11 #include "googleurl/src/gurl.h" |
10 #include "net/base/net_log.h" | 12 #include "net/base/net_log.h" |
11 #include "net/base/net_util.h" | 13 #include "net/base/net_util.h" |
12 #include "net/http/http_network_session.h" | 14 #include "net/http/http_network_session.h" |
13 #include "net/http/http_pipelined_connection.h" | 15 #include "net/http/http_pipelined_connection.h" |
14 #include "net/http/http_pipelined_host.h" | 16 #include "net/http/http_pipelined_host.h" |
15 #include "net/http/http_pipelined_stream.h" | 17 #include "net/http/http_pipelined_stream.h" |
16 #include "net/http/http_server_properties.h" | 18 #include "net/http/http_server_properties.h" |
17 #include "net/http/http_stream_factory_impl_job.h" | 19 #include "net/http/http_stream_factory_impl_job.h" |
18 #include "net/http/http_stream_factory_impl_request.h" | 20 #include "net/http/http_stream_factory_impl_request.h" |
21 #include "net/socket/client_socket_pool_manager.h" | |
mmenke
2013/06/10 20:26:03
nit: I don't believe this is used.
yhirano
2013/06/11 11:13:16
Done.
| |
19 #include "net/spdy/spdy_http_stream.h" | 22 #include "net/spdy/spdy_http_stream.h" |
20 | 23 |
21 namespace net { | 24 namespace net { |
22 | 25 |
23 namespace { | 26 namespace { |
24 | 27 |
25 const PortAlternateProtocolPair kNoAlternateProtocol = { | 28 const PortAlternateProtocolPair kNoAlternateProtocol = { |
26 0, UNINITIALIZED_ALTERNATE_PROTOCOL | 29 0, UNINITIALIZED_ALTERNATE_PROTOCOL |
27 }; | 30 }; |
28 | 31 |
29 GURL UpgradeUrlToHttps(const GURL& original_url, int port) { | 32 GURL UpgradeUrlToHttps(const GURL& original_url, int port) { |
30 GURL::Replacements replacements; | 33 GURL::Replacements replacements; |
31 // new_sheme and new_port need to be in scope here because GURL::Replacements | 34 // new_sheme and new_port need to be in scope here because GURL::Replacements |
32 // references the memory contained by them directly. | 35 // references the memory contained by them directly. |
33 const std::string new_scheme = "https"; | 36 const std::string new_scheme = "https"; |
34 const std::string new_port = base::IntToString(port); | 37 const std::string new_port = base::IntToString(port); |
35 replacements.SetSchemeStr(new_scheme); | 38 replacements.SetSchemeStr(new_scheme); |
36 replacements.SetPortStr(new_port); | 39 replacements.SetPortStr(new_port); |
37 return original_url.ReplaceComponents(replacements); | 40 return original_url.ReplaceComponents(replacements); |
38 } | 41 } |
39 | 42 |
40 } // namespace | 43 } // namespace |
41 | 44 |
42 HttpStreamFactoryImpl::HttpStreamFactoryImpl(HttpNetworkSession* session) | 45 HttpStreamFactoryImpl::HttpStreamFactoryImpl(HttpNetworkSession* session, |
46 bool for_websockets) | |
43 : session_(session), | 47 : session_(session), |
44 http_pipelined_host_pool_(this, NULL, | 48 http_pipelined_host_pool_(this, NULL, |
45 session_->http_server_properties(), | 49 session_->http_server_properties(), |
46 session_->force_http_pipelining()) {} | 50 session_->force_http_pipelining()), |
51 for_websockets_(for_websockets) {} | |
47 | 52 |
48 HttpStreamFactoryImpl::~HttpStreamFactoryImpl() { | 53 HttpStreamFactoryImpl::~HttpStreamFactoryImpl() { |
49 DCHECK(request_map_.empty()); | 54 DCHECK(request_map_.empty()); |
50 DCHECK(spdy_session_request_map_.empty()); | 55 DCHECK(spdy_session_request_map_.empty()); |
51 DCHECK(http_pipelining_request_map_.empty()); | 56 DCHECK(http_pipelining_request_map_.empty()); |
52 | 57 |
53 std::set<const Job*> tmp_job_set; | 58 std::set<const Job*> tmp_job_set; |
54 tmp_job_set.swap(orphaned_job_set_); | 59 tmp_job_set.swap(orphaned_job_set_); |
55 STLDeleteContainerPointers(tmp_job_set.begin(), tmp_job_set.end()); | 60 STLDeleteContainerPointers(tmp_job_set.begin(), tmp_job_set.end()); |
56 DCHECK(orphaned_job_set_.empty()); | 61 DCHECK(orphaned_job_set_.empty()); |
57 | 62 |
58 tmp_job_set.clear(); | 63 tmp_job_set.clear(); |
59 tmp_job_set.swap(preconnect_job_set_); | 64 tmp_job_set.swap(preconnect_job_set_); |
60 STLDeleteContainerPointers(tmp_job_set.begin(), tmp_job_set.end()); | 65 STLDeleteContainerPointers(tmp_job_set.begin(), tmp_job_set.end()); |
61 DCHECK(preconnect_job_set_.empty()); | 66 DCHECK(preconnect_job_set_.empty()); |
62 } | 67 } |
63 | 68 |
64 HttpStreamRequest* HttpStreamFactoryImpl::RequestStream( | 69 HttpStreamRequest* HttpStreamFactoryImpl::RequestStream( |
65 const HttpRequestInfo& request_info, | 70 const HttpRequestInfo& request_info, |
66 RequestPriority priority, | 71 RequestPriority priority, |
67 const SSLConfig& server_ssl_config, | 72 const SSLConfig& server_ssl_config, |
68 const SSLConfig& proxy_ssl_config, | 73 const SSLConfig& proxy_ssl_config, |
69 HttpStreamRequest::Delegate* delegate, | 74 HttpStreamRequest::Delegate* delegate, |
70 const BoundNetLog& net_log) { | 75 const BoundNetLog& net_log) { |
71 Request* request = new Request(request_info.url, this, delegate, net_log); | 76 DCHECK(!for_websockets_); |
77 return RequestStreamInternal(request_info, | |
78 priority, | |
79 server_ssl_config, | |
80 proxy_ssl_config, | |
81 delegate, | |
82 NULL, | |
83 net_log); | |
84 } | |
85 | |
86 HttpStreamRequest* HttpStreamFactoryImpl::RequestWebSocketStream( | |
87 const HttpRequestInfo& request_info, | |
88 RequestPriority priority, | |
89 const SSLConfig& server_ssl_config, | |
90 const SSLConfig& proxy_ssl_config, | |
91 HttpStreamRequest::Delegate* delegate, | |
92 WebSocketStreamBase::Factory* factory, | |
93 const BoundNetLog& net_log) { | |
94 DCHECK(for_websockets_); | |
95 return RequestStreamInternal(request_info, | |
96 priority, | |
97 server_ssl_config, | |
98 proxy_ssl_config, | |
99 delegate, | |
100 factory, | |
101 net_log); | |
102 } | |
103 | |
104 HttpStreamRequest* HttpStreamFactoryImpl::RequestStreamInternal( | |
105 const HttpRequestInfo& request_info, | |
106 RequestPriority priority, | |
107 const SSLConfig& server_ssl_config, | |
108 const SSLConfig& proxy_ssl_config, | |
109 HttpStreamRequest::Delegate* delegate, | |
110 WebSocketStreamBase::Factory* websocket_stream_factory, | |
111 const BoundNetLog& net_log) { | |
112 Request* request = | |
113 new Request(request_info.url, | |
114 this, | |
115 delegate, | |
116 websocket_stream_factory, | |
117 net_log); | |
72 | 118 |
73 GURL alternate_url; | 119 GURL alternate_url; |
74 PortAlternateProtocolPair alternate = | 120 PortAlternateProtocolPair alternate = |
75 GetAlternateProtocolRequestFor(request_info.url, &alternate_url); | 121 GetAlternateProtocolRequestFor(request_info.url, &alternate_url); |
76 Job* alternate_job = NULL; | 122 Job* alternate_job = NULL; |
77 if (alternate.protocol != UNINITIALIZED_ALTERNATE_PROTOCOL) { | 123 if (alternate.protocol != UNINITIALIZED_ALTERNATE_PROTOCOL) { |
78 // Never share connection with other jobs for FTP requests. | 124 // Never share connection with other jobs for FTP requests. |
79 DCHECK(!request_info.url.SchemeIs("ftp")); | 125 DCHECK(!request_info.url.SchemeIs("ftp")); |
80 | 126 |
81 HttpRequestInfo alternate_request_info = request_info; | 127 HttpRequestInfo alternate_request_info = request_info; |
(...skipping 24 matching lines...) Expand all Loading... | |
106 job->Start(request); | 152 job->Start(request); |
107 return request; | 153 return request; |
108 } | 154 } |
109 | 155 |
110 void HttpStreamFactoryImpl::PreconnectStreams( | 156 void HttpStreamFactoryImpl::PreconnectStreams( |
111 int num_streams, | 157 int num_streams, |
112 const HttpRequestInfo& request_info, | 158 const HttpRequestInfo& request_info, |
113 RequestPriority priority, | 159 RequestPriority priority, |
114 const SSLConfig& server_ssl_config, | 160 const SSLConfig& server_ssl_config, |
115 const SSLConfig& proxy_ssl_config) { | 161 const SSLConfig& proxy_ssl_config) { |
162 DCHECK(!for_websockets_); | |
116 GURL alternate_url; | 163 GURL alternate_url; |
117 PortAlternateProtocolPair alternate = | 164 PortAlternateProtocolPair alternate = |
118 GetAlternateProtocolRequestFor(request_info.url, &alternate_url); | 165 GetAlternateProtocolRequestFor(request_info.url, &alternate_url); |
119 Job* job = NULL; | 166 Job* job = NULL; |
120 if (alternate.protocol != UNINITIALIZED_ALTERNATE_PROTOCOL) { | 167 if (alternate.protocol != UNINITIALIZED_ALTERNATE_PROTOCOL) { |
121 HttpRequestInfo alternate_request_info = request_info; | 168 HttpRequestInfo alternate_request_info = request_info; |
122 alternate_request_info.url = alternate_url; | 169 alternate_request_info.url = alternate_url; |
123 job = new Job(this, session_, alternate_request_info, priority, | 170 job = new Job(this, session_, alternate_request_info, priority, |
124 server_ssl_config, proxy_ssl_config, session_->net_log()); | 171 server_ssl_config, proxy_ssl_config, session_->net_log()); |
125 job->MarkAsAlternate(request_info.url, alternate); | 172 job->MarkAsAlternate(request_info.url, alternate); |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
191 } else { | 238 } else { |
192 DCHECK_EQ(QUIC, alternate.protocol); | 239 DCHECK_EQ(QUIC, alternate.protocol); |
193 if (!session_->params().enable_quic || | 240 if (!session_->params().enable_quic || |
194 !original_url.SchemeIs("http")) | 241 !original_url.SchemeIs("http")) |
195 return kNoAlternateProtocol; | 242 return kNoAlternateProtocol; |
196 // TODO(rch): Figure out how to make QUIC iteract with PAC | 243 // TODO(rch): Figure out how to make QUIC iteract with PAC |
197 // scripts. By not re-writing the URL, we will query the PAC script | 244 // scripts. By not re-writing the URL, we will query the PAC script |
198 // for the proxy to use to reach the original URL via TCP. But | 245 // for the proxy to use to reach the original URL via TCP. But |
199 // the alternate request will be going via UDP to a different port. | 246 // the alternate request will be going via UDP to a different port. |
200 *alternate_url = original_url; | 247 *alternate_url = original_url; |
201 } | 248 } |
202 return alternate; | 249 return alternate; |
203 } | 250 } |
204 | 251 |
205 void HttpStreamFactoryImpl::OrphanJob(Job* job, const Request* request) { | 252 void HttpStreamFactoryImpl::OrphanJob(Job* job, const Request* request) { |
206 DCHECK(ContainsKey(request_map_, job)); | 253 DCHECK(ContainsKey(request_map_, job)); |
207 DCHECK_EQ(request_map_[job], request); | 254 DCHECK_EQ(request_map_[job], request); |
208 DCHECK(!ContainsKey(orphaned_job_set_, job)); | 255 DCHECK(!ContainsKey(orphaned_job_set_, job)); |
209 | 256 |
210 request_map_.erase(job); | 257 request_map_.erase(job); |
211 | 258 |
212 orphaned_job_set_.insert(job); | 259 orphaned_job_set_.insert(job); |
213 job->Orphan(request); | 260 job->Orphan(request); |
214 } | 261 } |
215 | 262 |
216 void HttpStreamFactoryImpl::OnSpdySessionReady( | 263 void HttpStreamFactoryImpl::OnNewSpdySessionReady( |
217 scoped_refptr<SpdySession> spdy_session, | 264 scoped_refptr<SpdySession> spdy_session, |
218 bool direct, | 265 bool direct, |
219 const SSLConfig& used_ssl_config, | 266 const SSLConfig& used_ssl_config, |
220 const ProxyInfo& used_proxy_info, | 267 const ProxyInfo& used_proxy_info, |
221 bool was_npn_negotiated, | 268 bool was_npn_negotiated, |
222 NextProto protocol_negotiated, | 269 NextProto protocol_negotiated, |
223 bool using_spdy, | 270 bool using_spdy, |
224 const BoundNetLog& net_log) { | 271 const BoundNetLog& net_log) { |
225 const SpdySessionKey& spdy_session_key = | 272 const SpdySessionKey& spdy_session_key = |
226 spdy_session->spdy_session_key(); | 273 spdy_session->spdy_session_key(); |
227 while (!spdy_session->IsClosed()) { | 274 while (!spdy_session->IsClosed()) { |
228 // Each iteration may empty out the RequestSet for |spdy_session_key_ in | 275 // Each iteration may empty out the RequestSet for |spdy_session_key_ in |
229 // |spdy_session_request_map_|. So each time, check for RequestSet and use | 276 // |spdy_session_request_map_|. So each time, check for RequestSet and use |
230 // the first one. | 277 // the first one. |
231 // | 278 // |
232 // TODO(willchan): If it's important, switch RequestSet out for a FIFO | 279 // TODO(willchan): If it's important, switch RequestSet out for a FIFO |
233 // pqueue (Order by priority first, then FIFO within same priority). Unclear | 280 // pqueue (Order by priority first, then FIFO within same priority). Unclear |
234 // that it matters here. | 281 // that it matters here. |
235 if (!ContainsKey(spdy_session_request_map_, spdy_session_key)) | 282 if (!ContainsKey(spdy_session_request_map_, spdy_session_key)) |
236 break; | 283 break; |
237 Request* request = *spdy_session_request_map_[spdy_session_key].begin(); | 284 Request* request = *spdy_session_request_map_[spdy_session_key].begin(); |
238 request->Complete(was_npn_negotiated, | 285 request->Complete(was_npn_negotiated, |
239 protocol_negotiated, | 286 protocol_negotiated, |
240 using_spdy, | 287 using_spdy, |
241 net_log); | 288 net_log); |
242 bool use_relative_url = direct || request->url().SchemeIs("https"); | 289 if (for_websockets_) { |
243 request->OnStreamReady( | 290 WebSocketStreamBase::Factory* factory = |
244 NULL, | 291 request->websocket_stream_factory(); |
245 used_ssl_config, | 292 DCHECK(factory); |
246 used_proxy_info, | 293 bool use_relative_url = direct || request->url().SchemeIs("wss"); |
247 new SpdyHttpStream(spdy_session.get(), use_relative_url)); | 294 request->OnWebSocketStreamReady( |
295 NULL, used_ssl_config, used_proxy_info, | |
296 factory->CreateSpdyStream(spdy_session, use_relative_url)); | |
297 } else { | |
298 bool use_relative_url = direct || request->url().SchemeIs("https"); | |
299 request->OnStreamReady(NULL, used_ssl_config, used_proxy_info, | |
300 new SpdyHttpStream(spdy_session, | |
301 use_relative_url)); | |
302 } | |
248 } | 303 } |
249 // TODO(mbelshe): Alert other valid requests. | 304 // TODO(mbelshe): Alert other valid requests. |
250 } | 305 } |
251 | 306 |
252 void HttpStreamFactoryImpl::OnOrphanedJobComplete(const Job* job) { | 307 void HttpStreamFactoryImpl::OnOrphanedJobComplete(const Job* job) { |
253 orphaned_job_set_.erase(job); | 308 orphaned_job_set_.erase(job); |
254 delete job; | 309 delete job; |
255 } | 310 } |
256 | 311 |
257 void HttpStreamFactoryImpl::OnPreconnectsComplete(const Job* job) { | 312 void HttpStreamFactoryImpl::OnPreconnectsComplete(const Job* job) { |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
290 it != requests_to_fail.end(); ++it) { | 345 it != requests_to_fail.end(); ++it) { |
291 Request* request = *it; | 346 Request* request = *it; |
292 if (request == request_map_[job]) { | 347 if (request == request_map_[job]) { |
293 continue; | 348 continue; |
294 } | 349 } |
295 request->OnStreamFailed(NULL, status, used_ssl_config); | 350 request->OnStreamFailed(NULL, status, used_ssl_config); |
296 } | 351 } |
297 } | 352 } |
298 | 353 |
299 } // namespace net | 354 } // namespace net |
OLD | NEW |