Chromium Code Reviews| 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/url_request/url_request_http_job.h" | 5 #include "net/url_request/url_request_http_job.h" |
| 6 | 6 |
| 7 #include <vector> | 7 #include <vector> |
| 8 | 8 |
| 9 #include "base/base_switches.h" | 9 #include "base/base_switches.h" |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| (...skipping 172 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 183 | 183 |
| 184 GURL::Replacements replacements; | 184 GURL::Replacements replacements; |
| 185 replacements.SetSchemeStr(url.SchemeIs(url::kHttpScheme) ? url::kHttpsScheme | 185 replacements.SetSchemeStr(url.SchemeIs(url::kHttpScheme) ? url::kHttpsScheme |
| 186 : url::kWssScheme); | 186 : url::kWssScheme); |
| 187 return new net::URLRequestRedirectJob( | 187 return new net::URLRequestRedirectJob( |
| 188 request, network_delegate, url.ReplaceComponents(replacements), | 188 request, network_delegate, url.ReplaceComponents(replacements), |
| 189 // Use status code 307 to preserve the method, so POST requests work. | 189 // Use status code 307 to preserve the method, so POST requests work. |
| 190 net::URLRequestRedirectJob::REDIRECT_307_TEMPORARY_REDIRECT, "HSTS"); | 190 net::URLRequestRedirectJob::REDIRECT_307_TEMPORARY_REDIRECT, "HSTS"); |
| 191 } | 191 } |
| 192 | 192 |
| 193 // If |request|'s insecure request policy matches its URL, then upgrade it from | |
| 194 // a non-secure protocol to a secure protocol (e.g. "http" => "https"). See | |
| 195 // https://www.w3.org/TR/upgrade-insecure-requests/ for details. | |
| 196 // | |
| 197 // TODO(mkwst): HSTS is currently modeled as a redirect, which makes sense, | |
| 198 // given the web-exposed behavior developers currently rely upon. At some | |
| 199 // point, however, https://wicg.github.io/hsts-priming/ will change that | |
| 200 // expectation. Once those changes are in place, it would make sense to | |
| 201 // merge the HSTS logic from 'MaybeInternallyRedirect' into these functions. | |
| 202 bool ShouldUpgradeURLForRequest(const GURL& url, net::URLRequest* request) { | |
| 203 if (request->insecure_request_policy() == net::URLRequest::DO_NOT_UPGRADE_INSE CURE_REQUESTS || | |
| 204 url.SchemeIsCryptographic() || | |
| 205 (request->initiator() && | |
| 206 request->insecure_request_policy() == net::URLRequest::UPGRADE_SAME_HOST_ INSECURE_REQUESTS && | |
| 207 request->initiator()->host() != url.host())) { | |
| 208 return false; | |
| 209 } | |
| 210 | |
| 211 return true; | |
| 212 } | |
| 213 | |
| 214 GURL UpgradeURL(const GURL& url) { | |
| 215 DCHECK(url.SchemeIs(url::kHttpScheme) || url.SchemeIs(url::kWsScheme)); | |
| 216 GURL::Replacements replacements; | |
| 217 replacements.SetSchemeStr(url.SchemeIs(url::kHttpScheme) ? url::kHttpsScheme : url::kWssScheme); | |
| 218 return url.ReplaceComponents(replacements); | |
| 219 } | |
| 220 | |
| 221 void MaybeRewriteRequestURL(net::URLRequest* request) { | |
| 222 if (!ShouldUpgradeURLForRequest(request->url(), request)) | |
| 223 return; | |
| 224 | |
| 225 request->RewriteURL(UpgradeURL(request->url()), "Upgrade-Insecure-Requests"); | |
| 226 } | |
| 227 | |
| 193 } // namespace | 228 } // namespace |
| 194 | 229 |
| 195 namespace net { | 230 namespace net { |
| 196 | 231 |
| 197 // TODO(darin): make sure the port blocking code is not lost | 232 // TODO(darin): make sure the port blocking code is not lost |
| 198 // static | 233 // static |
| 199 URLRequestJob* URLRequestHttpJob::Factory(URLRequest* request, | 234 URLRequestJob* URLRequestHttpJob::Factory(URLRequest* request, |
| 200 NetworkDelegate* network_delegate, | 235 NetworkDelegate* network_delegate, |
| 201 const std::string& scheme) { | 236 const std::string& scheme) { |
| 202 DCHECK(scheme == "http" || scheme == "https" || scheme == "ws" || | 237 DCHECK(scheme == "http" || scheme == "https" || scheme == "ws" || |
| 203 scheme == "wss"); | 238 scheme == "wss"); |
| 204 | 239 |
| 205 if (!request->context()->http_transaction_factory()) { | 240 if (!request->context()->http_transaction_factory()) { |
| 206 NOTREACHED() << "requires a valid context"; | 241 NOTREACHED() << "requires a valid context"; |
| 207 return new URLRequestErrorJob( | 242 return new URLRequestErrorJob( |
| 208 request, network_delegate, ERR_INVALID_ARGUMENT); | 243 request, network_delegate, ERR_INVALID_ARGUMENT); |
| 209 } | 244 } |
| 210 | 245 |
| 246 MaybeRewriteRequestURL(request); | |
|
mmenke
2016/12/13 19:00:24
The redirect stuff all looks pretty reasonable to
| |
| 247 | |
| 211 URLRequestRedirectJob* redirect = | 248 URLRequestRedirectJob* redirect = |
| 212 MaybeInternallyRedirect(request, network_delegate); | 249 MaybeInternallyRedirect(request, network_delegate); |
| 213 if (redirect) | 250 if (redirect) |
| 214 return redirect; | 251 return redirect; |
| 215 | 252 |
| 216 return new URLRequestHttpJob(request, | 253 return new URLRequestHttpJob(request, |
| 217 network_delegate, | 254 network_delegate, |
| 218 request->context()->http_user_agent_settings()); | 255 request->context()->http_user_agent_settings()); |
| 219 } | 256 } |
| 220 | 257 |
| (...skipping 899 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1120 return nullptr; | 1157 return nullptr; |
| 1121 } | 1158 } |
| 1122 if (downstream == nullptr) | 1159 if (downstream == nullptr) |
| 1123 return nullptr; | 1160 return nullptr; |
| 1124 upstream = std::move(downstream); | 1161 upstream = std::move(downstream); |
| 1125 } | 1162 } |
| 1126 | 1163 |
| 1127 return upstream; | 1164 return upstream; |
| 1128 } | 1165 } |
| 1129 | 1166 |
| 1167 RedirectInfo URLRequestHttpJob::ComputeRedirectInfo(const GURL& location, int ht tp_status_code) { | |
| 1168 return URLRequestJob::ComputeRedirectInfo(ShouldUpgradeURLForRequest(location, request_) ? UpgradeURL(location) : location, http_status_code); | |
| 1169 } | |
| 1170 | |
| 1130 bool URLRequestHttpJob::CopyFragmentOnRedirect(const GURL& location) const { | 1171 bool URLRequestHttpJob::CopyFragmentOnRedirect(const GURL& location) const { |
| 1131 // Allow modification of reference fragments by default, unless | 1172 // Allow modification of reference fragments by default, unless |
| 1132 // |allowed_unsafe_redirect_url_| is set and equal to the redirect URL. | 1173 // |allowed_unsafe_redirect_url_| is set and equal to the redirect URL. |
| 1133 // When this is the case, we assume that the network delegate has set the | 1174 // When this is the case, we assume that the network delegate has set the |
| 1134 // desired redirect URL (with or without fragment), so it must not be changed | 1175 // desired redirect URL (with or without fragment), so it must not be changed |
| 1135 // any more. | 1176 // any more. |
| 1136 return !allowed_unsafe_redirect_url_.is_valid() || | 1177 return !allowed_unsafe_redirect_url_.is_valid() || |
| 1137 allowed_unsafe_redirect_url_ != location; | 1178 allowed_unsafe_redirect_url_ != location; |
| 1138 } | 1179 } |
| 1139 | 1180 |
| (...skipping 407 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1547 awaiting_callback_ = false; | 1588 awaiting_callback_ = false; |
| 1548 | 1589 |
| 1549 // Notify NetworkQualityEstimator. | 1590 // Notify NetworkQualityEstimator. |
| 1550 NetworkQualityEstimator* network_quality_estimator = | 1591 NetworkQualityEstimator* network_quality_estimator = |
| 1551 request()->context()->network_quality_estimator(); | 1592 request()->context()->network_quality_estimator(); |
| 1552 if (network_quality_estimator) | 1593 if (network_quality_estimator) |
| 1553 network_quality_estimator->NotifyURLRequestDestroyed(*request()); | 1594 network_quality_estimator->NotifyURLRequestDestroyed(*request()); |
| 1554 } | 1595 } |
| 1555 | 1596 |
| 1556 } // namespace net | 1597 } // namespace net |
| OLD | NEW |