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