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> | 7 #include <string> |
8 | 8 |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/stl_util.h" | 10 #include "base/stl_util.h" |
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
86 const BoundNetLog& net_log) { | 86 const BoundNetLog& net_log) { |
87 Request* request = new Request(request_info.url, | 87 Request* request = new Request(request_info.url, |
88 this, | 88 this, |
89 delegate, | 89 delegate, |
90 websocket_handshake_stream_create_helper, | 90 websocket_handshake_stream_create_helper, |
91 net_log); | 91 net_log); |
92 Job* job = new Job(this, session_, request_info, priority, server_ssl_config, | 92 Job* job = new Job(this, session_, request_info, priority, server_ssl_config, |
93 proxy_ssl_config, net_log.net_log()); | 93 proxy_ssl_config, net_log.net_log()); |
94 request->AttachJob(job); | 94 request->AttachJob(job); |
95 | 95 |
96 AlternativeService alternative_service = | 96 const AlternativeServiceVector alternative_service_vector = |
97 GetAlternativeServiceFor(request_info.url); | 97 GetAlternativeServicesFor(request_info.url); |
98 if (alternative_service.protocol != UNINITIALIZED_ALTERNATE_PROTOCOL) { | 98 if (!alternative_service_vector.empty()) { |
| 99 // TODO(bnc): Pass on multiple alternative services to Job. |
| 100 const AlternativeService& alternative_service = |
| 101 alternative_service_vector[0]; |
99 // Never share connection with other jobs for FTP requests. | 102 // Never share connection with other jobs for FTP requests. |
100 DCHECK(!request_info.url.SchemeIs("ftp")); | 103 DCHECK(!request_info.url.SchemeIs("ftp")); |
101 | 104 |
102 Job* alternative_job = | 105 Job* alternative_job = |
103 new Job(this, session_, request_info, priority, server_ssl_config, | 106 new Job(this, session_, request_info, priority, server_ssl_config, |
104 proxy_ssl_config, alternative_service, net_log.net_log()); | 107 proxy_ssl_config, alternative_service, net_log.net_log()); |
105 request->AttachJob(alternative_job); | 108 request->AttachJob(alternative_job); |
106 | 109 |
107 job->WaitFor(alternative_job); | 110 job->WaitFor(alternative_job); |
108 // Make sure to wait until we call WaitFor(), before starting | 111 // Make sure to wait until we call WaitFor(), before starting |
109 // |alternative_job|, otherwise |alternative_job| will not notify |job| | 112 // |alternative_job|, otherwise |alternative_job| will not notify |job| |
110 // appropriately. | 113 // appropriately. |
111 alternative_job->Start(request); | 114 alternative_job->Start(request); |
112 } | 115 } |
113 | 116 |
114 // Even if |alternative_job| has already finished, it will not have notified | 117 // Even if |alternative_job| has already finished, it will not have notified |
115 // the request yet, since we defer that to the next iteration of the | 118 // the request yet, since we defer that to the next iteration of the |
116 // MessageLoop, so starting |job| is always safe. | 119 // MessageLoop, so starting |job| is always safe. |
117 job->Start(request); | 120 job->Start(request); |
118 return request; | 121 return request; |
119 } | 122 } |
120 | 123 |
121 void HttpStreamFactoryImpl::PreconnectStreams( | 124 void HttpStreamFactoryImpl::PreconnectStreams( |
122 int num_streams, | 125 int num_streams, |
123 const HttpRequestInfo& request_info, | 126 const HttpRequestInfo& request_info, |
124 RequestPriority priority, | 127 RequestPriority priority, |
125 const SSLConfig& server_ssl_config, | 128 const SSLConfig& server_ssl_config, |
126 const SSLConfig& proxy_ssl_config) { | 129 const SSLConfig& proxy_ssl_config) { |
127 DCHECK(!for_websockets_); | 130 DCHECK(!for_websockets_); |
128 AlternativeService alternative_service = | 131 AlternativeService alternative_service; |
129 GetAlternativeServiceFor(request_info.url); | 132 AlternativeServiceVector alternative_service_vector = |
| 133 GetAlternativeServicesFor(request_info.url); |
| 134 if (!alternative_service_vector.empty()) { |
| 135 // TODO(bnc): Pass on multiple alternative services to Job. |
| 136 alternative_service = alternative_service_vector[0]; |
| 137 } |
130 Job* job = | 138 Job* job = |
131 new Job(this, session_, request_info, priority, server_ssl_config, | 139 new Job(this, session_, request_info, priority, server_ssl_config, |
132 proxy_ssl_config, alternative_service, session_->net_log()); | 140 proxy_ssl_config, alternative_service, session_->net_log()); |
133 preconnect_job_set_.insert(job); | 141 preconnect_job_set_.insert(job); |
134 job->Preconnect(num_streams); | 142 job->Preconnect(num_streams); |
135 } | 143 } |
136 | 144 |
137 const HostMappingRules* HttpStreamFactoryImpl::GetHostMappingRules() const { | 145 const HostMappingRules* HttpStreamFactoryImpl::GetHostMappingRules() const { |
138 return session_->params().host_mapping_rules; | 146 return session_->params().host_mapping_rules; |
139 } | 147 } |
140 | 148 |
141 AlternativeService HttpStreamFactoryImpl::GetAlternativeServiceFor( | 149 AlternativeServiceVector HttpStreamFactoryImpl::GetAlternativeServicesFor( |
142 const GURL& original_url) { | 150 const GURL& original_url) { |
143 const AlternativeService kNoAlternativeService; | |
144 | |
145 if (!session_->params().use_alternate_protocols) | 151 if (!session_->params().use_alternate_protocols) |
146 return kNoAlternativeService; | 152 return AlternativeServiceVector(); |
147 | 153 |
148 if (original_url.SchemeIs("ftp")) | 154 if (original_url.SchemeIs("ftp")) |
149 return kNoAlternativeService; | 155 return AlternativeServiceVector(); |
150 | 156 |
151 HostPortPair origin = HostPortPair::FromURL(original_url); | 157 HostPortPair origin = HostPortPair::FromURL(original_url); |
152 HttpServerProperties& http_server_properties = | 158 HttpServerProperties& http_server_properties = |
153 *session_->http_server_properties(); | 159 *session_->http_server_properties(); |
154 const AlternativeService alternative_service = | 160 const AlternativeServiceVector alternative_service_vector = |
155 http_server_properties.GetAlternativeService(origin); | 161 http_server_properties.GetAlternativeServices(origin); |
| 162 if (alternative_service_vector.empty()) |
| 163 return AlternativeServiceVector(); |
156 | 164 |
157 if (alternative_service.protocol == UNINITIALIZED_ALTERNATE_PROTOCOL) | 165 AlternativeServiceVector enabled_alternative_service_vector; |
158 return kNoAlternativeService; | 166 for (const AlternativeService& alternative_service : |
159 if (http_server_properties.IsAlternativeServiceBroken(alternative_service)) { | 167 alternative_service_vector) { |
160 HistogramAlternateProtocolUsage(ALTERNATE_PROTOCOL_USAGE_BROKEN); | 168 DCHECK(IsAlternateProtocolValid(alternative_service.protocol)); |
161 return kNoAlternativeService; | 169 if (http_server_properties.IsAlternativeServiceBroken( |
| 170 alternative_service)) { |
| 171 HistogramAlternateProtocolUsage(ALTERNATE_PROTOCOL_USAGE_BROKEN); |
| 172 continue; |
| 173 } |
| 174 |
| 175 // Some shared unix systems may have user home directories (like |
| 176 // http://foo.com/~mike) which allow users to emit headers. This is a bad |
| 177 // idea already, but with Alternate-Protocol, it provides the ability for a |
| 178 // single user on a multi-user system to hijack the alternate protocol. |
| 179 // These systems also enforce ports <1024 as restricted ports. So don't |
| 180 // allow protocol upgrades to user-controllable ports. |
| 181 const int kUnrestrictedPort = 1024; |
| 182 if (!session_->params().enable_user_alternate_protocol_ports && |
| 183 (alternative_service.port >= kUnrestrictedPort && |
| 184 origin.port() < kUnrestrictedPort)) |
| 185 continue; |
| 186 |
| 187 origin.set_port(alternative_service.port); |
| 188 if (alternative_service.protocol >= NPN_SPDY_MINIMUM_VERSION && |
| 189 alternative_service.protocol <= NPN_SPDY_MAXIMUM_VERSION) { |
| 190 if (!HttpStreamFactory::spdy_enabled()) |
| 191 continue; |
| 192 |
| 193 if (session_->HasSpdyExclusion(origin)) |
| 194 continue; |
| 195 |
| 196 enabled_alternative_service_vector.push_back(alternative_service); |
| 197 continue; |
| 198 } |
| 199 |
| 200 DCHECK_EQ(QUIC, alternative_service.protocol); |
| 201 if (!session_->params().enable_quic) |
| 202 continue; |
| 203 |
| 204 if (session_->quic_stream_factory()->IsQuicDisabled(origin.port())) |
| 205 continue; |
| 206 |
| 207 if (session_->params().disable_insecure_quic && |
| 208 !original_url.SchemeIs("https")) { |
| 209 continue; |
| 210 } |
| 211 |
| 212 enabled_alternative_service_vector.push_back(alternative_service); |
162 } | 213 } |
163 if (!IsAlternateProtocolValid(alternative_service.protocol)) { | 214 return enabled_alternative_service_vector; |
164 NOTREACHED(); | |
165 return kNoAlternativeService; | |
166 } | |
167 | |
168 // Some shared unix systems may have user home directories (like | |
169 // http://foo.com/~mike) which allow users to emit headers. This is a bad | |
170 // idea already, but with Alternate-Protocol, it provides the ability for a | |
171 // single user on a multi-user system to hijack the alternate protocol. | |
172 // These systems also enforce ports <1024 as restricted ports. So don't | |
173 // allow protocol upgrades to user-controllable ports. | |
174 const int kUnrestrictedPort = 1024; | |
175 if (!session_->params().enable_user_alternate_protocol_ports && | |
176 (alternative_service.port >= kUnrestrictedPort && | |
177 origin.port() < kUnrestrictedPort)) | |
178 return kNoAlternativeService; | |
179 | |
180 origin.set_port(alternative_service.port); | |
181 if (alternative_service.protocol >= NPN_SPDY_MINIMUM_VERSION && | |
182 alternative_service.protocol <= NPN_SPDY_MAXIMUM_VERSION) { | |
183 if (!HttpStreamFactory::spdy_enabled()) | |
184 return kNoAlternativeService; | |
185 | |
186 if (session_->HasSpdyExclusion(origin)) | |
187 return kNoAlternativeService; | |
188 | |
189 return alternative_service; | |
190 } | |
191 | |
192 DCHECK_EQ(QUIC, alternative_service.protocol); | |
193 if (!session_->params().enable_quic) | |
194 return kNoAlternativeService; | |
195 | |
196 if (session_->quic_stream_factory()->IsQuicDisabled(origin.port())) | |
197 return kNoAlternativeService; | |
198 | |
199 if (session_->params().disable_insecure_quic && | |
200 !original_url.SchemeIs("https")) { | |
201 return kNoAlternativeService; | |
202 } | |
203 | |
204 return alternative_service; | |
205 } | 215 } |
206 | 216 |
207 void HttpStreamFactoryImpl::OrphanJob(Job* job, const Request* request) { | 217 void HttpStreamFactoryImpl::OrphanJob(Job* job, const Request* request) { |
208 DCHECK(ContainsKey(request_map_, job)); | 218 DCHECK(ContainsKey(request_map_, job)); |
209 DCHECK_EQ(request_map_[job], request); | 219 DCHECK_EQ(request_map_[job], request); |
210 DCHECK(!ContainsKey(orphaned_job_set_, job)); | 220 DCHECK(!ContainsKey(orphaned_job_set_, job)); |
211 | 221 |
212 request_map_.erase(job); | 222 request_map_.erase(job); |
213 | 223 |
214 orphaned_job_set_.insert(job); | 224 orphaned_job_set_.insert(job); |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
260 delete job; | 270 delete job; |
261 } | 271 } |
262 | 272 |
263 void HttpStreamFactoryImpl::OnPreconnectsComplete(const Job* job) { | 273 void HttpStreamFactoryImpl::OnPreconnectsComplete(const Job* job) { |
264 preconnect_job_set_.erase(job); | 274 preconnect_job_set_.erase(job); |
265 delete job; | 275 delete job; |
266 OnPreconnectsCompleteInternal(); | 276 OnPreconnectsCompleteInternal(); |
267 } | 277 } |
268 | 278 |
269 } // namespace net | 279 } // namespace net |
OLD | NEW |