[email protected] | fecef22 | 2012-01-05 02:26:15 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 4 | |
[email protected] | 175adac | 2008-07-30 17:28:04 | [diff] [blame] | 5 | #include "net/url_request/url_request_http_job.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 6 | |
Eric Lawrence | f814f5b2 | 2019-10-04 01:52:43 | [diff] [blame] | 7 | #include <algorithm> |
| 8 | #include <utility> |
mmenke | d3a02b3 | 2016-11-16 20:21:32 | [diff] [blame] | 9 | #include <vector> |
| 10 | |
[email protected] | 4ed2755f | 2008-12-15 09:01:33 | [diff] [blame] | 11 | #include "base/base_switches.h" |
[email protected] | 4f9e5c8 | 2011-11-17 16:04:56 | [diff] [blame] | 12 | #include "base/bind.h" |
[email protected] | 084262c | 2011-12-01 21:12:47 | [diff] [blame] | 13 | #include "base/bind_helpers.h" |
[email protected] | 4ed2755f | 2008-12-15 09:01:33 | [diff] [blame] | 14 | #include "base/command_line.h" |
[email protected] | 39ce5c0 | 2008-08-22 04:03:44 | [diff] [blame] | 15 | #include "base/compiler_specific.h" |
[email protected] | 6088942 | 2008-09-23 01:18:16 | [diff] [blame] | 16 | #include "base/file_version_info.h" |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 17 | #include "base/location.h" |
Avi Drissman | 13fc893 | 2015-12-20 04:40:46 | [diff] [blame] | 18 | #include "base/macros.h" |
[email protected] | 8684a881 | 2011-03-22 13:59:38 | [diff] [blame] | 19 | #include "base/metrics/field_trial.h" |
Devdeep Ray | 0c6966d | 2017-07-14 00:58:22 | [diff] [blame] | 20 | #include "base/metrics/histogram_functions.h" |
asvitkine | c3c9372 | 2015-06-17 14:48:37 | [diff] [blame] | 21 | #include "base/metrics/histogram_macros.h" |
Mike West | fbd4411 | 2019-05-08 18:23:37 | [diff] [blame] | 22 | #include "base/numerics/safe_conversions.h" |
Lily Chen | b851acc | 2019-08-07 15:54:44 | [diff] [blame] | 23 | #include "base/optional.h" |
[email protected] | 5b90b5d | 2009-04-30 23:06:01 | [diff] [blame] | 24 | #include "base/rand_util.h" |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 25 | #include "base/single_thread_task_runner.h" |
[email protected] | 4dc3ad4f | 2013-06-11 07:15:50 | [diff] [blame] | 26 | #include "base/strings/string_util.h" |
Devdeep Ray | 0c6966d | 2017-07-14 00:58:22 | [diff] [blame] | 27 | #include "base/strings/stringprintf.h" |
gab | f767595f | 2016-05-11 18:50:35 | [diff] [blame] | 28 | #include "base/threading/thread_task_runner_handle.h" |
[email protected] | f002abb | 2013-06-28 02:30:21 | [diff] [blame] | 29 | #include "base/time/time.h" |
ssid | 6d6b4010 | 2016-04-05 18:59:56 | [diff] [blame] | 30 | #include "base/trace_event/trace_event.h" |
estade | 5e5529d | 2015-05-21 20:59:11 | [diff] [blame] | 31 | #include "base/values.h" |
Mike West | fbd4411 | 2019-05-08 18:23:37 | [diff] [blame] | 32 | #include "net/base/features.h" |
[email protected] | 6d81b48 | 2011-02-22 19:47:19 | [diff] [blame] | 33 | #include "net/base/host_port_pair.h" |
Matt Menke | d732ea4 | 2019-03-08 12:05:00 | [diff] [blame] | 34 | #include "net/base/http_user_agent_settings.h" |
[email protected] | b843072 | 2008-09-17 20:05:44 | [diff] [blame] | 35 | #include "net/base/load_flags.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 36 | #include "net/base/net_errors.h" |
[email protected] | 636eccd | 2011-06-28 12:28:01 | [diff] [blame] | 37 | #include "net/base/network_delegate.h" |
Shivani Sharma | d81bdd4 | 2019-05-23 17:19:56 | [diff] [blame] | 38 | #include "net/base/network_isolation_key.h" |
mkwst | f71d0bd | 2016-03-21 14:15:24 | [diff] [blame] | 39 | #include "net/base/registry_controlled_domains/registry_controlled_domain.h" |
xunjieli | 0b7f5b6 | 2016-12-06 20:43:48 | [diff] [blame] | 40 | #include "net/base/trace_constants.h" |
tfarina | 7a4a7fd | 2016-01-20 14:23:44 | [diff] [blame] | 41 | #include "net/base/url_util.h" |
[email protected] | 6e7845ae | 2013-03-29 21:48:11 | [diff] [blame] | 42 | #include "net/cert/cert_status_flags.h" |
Emily Stark | 8b411de0 | 2017-11-23 20:21:27 | [diff] [blame] | 43 | #include "net/cert/ct_policy_status.h" |
Ryan Sleevi | 19a7bde | 2017-11-22 06:51:39 | [diff] [blame] | 44 | #include "net/cert/known_roots.h" |
Randy Smith | e23356c2 | 2017-11-14 01:41:50 | [diff] [blame] | 45 | #include "net/cookies/canonical_cookie.h" |
[email protected] | dc8313a | 2014-03-24 21:38:14 | [diff] [blame] | 46 | #include "net/cookies/cookie_store.h" |
Maks Orlovich | 64e2913d | 2019-03-05 14:42:56 | [diff] [blame] | 47 | #include "net/cookies/cookie_util.h" |
xunjieli | 28a1877 | 2016-10-25 15:30:06 | [diff] [blame] | 48 | #include "net/filter/brotli_source_stream.h" |
| 49 | #include "net/filter/filter_source_stream.h" |
| 50 | #include "net/filter/gzip_source_stream.h" |
xunjieli | 28a1877 | 2016-10-25 15:30:06 | [diff] [blame] | 51 | #include "net/filter/source_stream.h" |
[email protected] | 26219171 | 2014-03-22 00:46:57 | [diff] [blame] | 52 | #include "net/http/http_content_disposition.h" |
[email protected] | 7a299a9 | 2012-10-24 23:54:50 | [diff] [blame] | 53 | #include "net/http/http_network_session.h" |
[email protected] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame] | 54 | #include "net/http/http_request_headers.h" |
[email protected] | 319d9e6f | 2009-02-18 19:47:21 | [diff] [blame] | 55 | #include "net/http/http_response_headers.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 56 | #include "net/http/http_response_info.h" |
[email protected] | 9094b60 | 2012-02-27 21:44:58 | [diff] [blame] | 57 | #include "net/http/http_status_code.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 58 | #include "net/http/http_transaction.h" |
| 59 | #include "net/http/http_transaction_factory.h" |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 60 | #include "net/http/http_util.h" |
Ryan Hamilton | 0bed157 | 2018-03-02 19:58:21 | [diff] [blame] | 61 | #include "net/log/net_log.h" |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 62 | #include "net/log/net_log_event_type.h" |
Imam Mohammad Bokhary | 374b339a | 2019-09-27 16:35:19 | [diff] [blame] | 63 | #include "net/log/net_log_values.h" |
mikecirone | f22f981 | 2016-10-04 03:40:19 | [diff] [blame] | 64 | #include "net/log/net_log_with_source.h" |
tbansal | ca83c00 | 2016-04-28 20:56:28 | [diff] [blame] | 65 | #include "net/nqe/network_quality_estimator.h" |
Lily Houghton | 582d462 | 2018-01-22 22:43:40 | [diff] [blame] | 66 | #include "net/proxy_resolution/proxy_info.h" |
Lily Houghton | ffe89daa0 | 2018-03-09 18:30:03 | [diff] [blame] | 67 | #include "net/proxy_resolution/proxy_resolution_service.h" |
Lily Houghton | 582d462 | 2018-01-22 22:43:40 | [diff] [blame] | 68 | #include "net/proxy_resolution/proxy_retry_info.h" |
[email protected] | 536fd0b | 2013-03-14 17:41:57 | [diff] [blame] | 69 | #include "net/ssl/ssl_cert_request_info.h" |
| 70 | #include "net/ssl/ssl_config_service.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 71 | #include "net/url_request/url_request.h" |
[email protected] | 319d9e6f | 2009-02-18 19:47:21 | [diff] [blame] | 72 | #include "net/url_request/url_request_context.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 73 | #include "net/url_request/url_request_error_job.h" |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 74 | #include "net/url_request/url_request_http_job_histogram.h" |
[email protected] | e0f35c9 | 2013-05-08 16:04:34 | [diff] [blame] | 75 | #include "net/url_request/url_request_job_factory.h" |
[email protected] | 06965e0 | 2009-09-04 21:36:42 | [diff] [blame] | 76 | #include "net/url_request/url_request_redirect_job.h" |
[email protected] | 6b3f964 | 2010-11-25 02:29:06 | [diff] [blame] | 77 | #include "net/url_request/url_request_throttler_manager.h" |
Bence Béky | a5ec55e6 | 2018-03-07 14:23:28 | [diff] [blame] | 78 | #include "net/url_request/websocket_handshake_userdata_key.h" |
mkwst | 8241a12 | 2015-10-20 07:15:10 | [diff] [blame] | 79 | #include "url/origin.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 80 | |
mgersh | d21d6d14 | 2016-12-14 23:06:36 | [diff] [blame] | 81 | #if defined(OS_ANDROID) |
| 82 | #include "net/android/network_library.h" |
| 83 | #endif |
| 84 | |
mkwst | 202534e3 | 2016-01-15 16:07:15 | [diff] [blame] | 85 | namespace { |
| 86 | |
Eric Lawrence | f814f5b2 | 2019-10-04 01:52:43 | [diff] [blame] | 87 | base::Value CookieExcludedNetLogParams(const std::string& operation, |
| 88 | const std::string& cookie_name, |
| 89 | const std::string& exclusion_reason, |
| 90 | net::NetLogCaptureMode capture_mode) { |
| 91 | base::Value dict(base::Value::Type::DICTIONARY); |
| 92 | dict.SetStringKey("operation", operation); |
| 93 | dict.SetStringKey("exclusion_reason", exclusion_reason); |
| 94 | if (net::NetLogCaptureIncludesSensitive(capture_mode) && |
| 95 | !cookie_name.empty()) { |
| 96 | dict.SetStringKey("name", cookie_name); |
| 97 | } |
| 98 | return dict; |
| 99 | } |
| 100 | |
Ryan Sleevi | 19a7bde | 2017-11-22 06:51:39 | [diff] [blame] | 101 | // Records details about the most-specific trust anchor in |spki_hashes|, |
| 102 | // which is expected to be ordered with the leaf cert first and the root cert |
| 103 | // last. This complements the per-verification histogram |
| 104 | // Net.Certificate.TrustAnchor.Verify |
| 105 | void LogTrustAnchor(const net::HashValueVector& spki_hashes) { |
Ryan Sleevi | d756c2d | 2017-12-01 02:07:48 | [diff] [blame] | 106 | // Don't record metrics if there are no hashes; this is true if the HTTP |
| 107 | // load did not come from an active network connection, such as the disk |
| 108 | // cache or a synthesized response. |
| 109 | if (spki_hashes.empty()) |
| 110 | return; |
| 111 | |
Ryan Sleevi | 19a7bde | 2017-11-22 06:51:39 | [diff] [blame] | 112 | int32_t id = 0; |
| 113 | for (const auto& hash : spki_hashes) { |
| 114 | id = net::GetNetTrustAnchorHistogramIdForSPKI(hash); |
| 115 | if (id != 0) |
| 116 | break; |
| 117 | } |
Ilya Sherman | 0eb3980 | 2017-12-08 20:58:18 | [diff] [blame] | 118 | base::UmaHistogramSparse("Net.Certificate.TrustAnchor.Request", id); |
Ryan Sleevi | 19a7bde | 2017-11-22 06:51:39 | [diff] [blame] | 119 | } |
| 120 | |
Emily Stark | 8b411de0 | 2017-11-23 20:21:27 | [diff] [blame] | 121 | // Records per-request histograms relating to Certificate Transparency |
| 122 | // compliance. |
| 123 | void RecordCTHistograms(const net::SSLInfo& ssl_info) { |
Emily Stark | 9676ea8 | 2017-11-29 08:10:08 | [diff] [blame] | 124 | if (ssl_info.ct_policy_compliance == |
| 125 | net::ct::CTPolicyCompliance::CT_POLICY_COMPLIANCE_DETAILS_NOT_AVAILABLE) { |
Emily Stark | 8b411de0 | 2017-11-23 20:21:27 | [diff] [blame] | 126 | return; |
Emily Stark | 9676ea8 | 2017-11-29 08:10:08 | [diff] [blame] | 127 | } |
Emily Stark | 8b411de0 | 2017-11-23 20:21:27 | [diff] [blame] | 128 | if (!ssl_info.is_issued_by_known_root) |
| 129 | return; |
| 130 | |
| 131 | // Connections with major errors other than CERTIFICATE_TRANSPARENCY_REQUIRED |
| 132 | // would have failed anyway, so do not record these histograms for such |
| 133 | // requests. |
| 134 | net::CertStatus other_errors = |
| 135 | ssl_info.cert_status & |
| 136 | ~net::CERT_STATUS_CERTIFICATE_TRANSPARENCY_REQUIRED; |
Matt Mueller | 93651144 | 2019-09-03 18:15:12 | [diff] [blame] | 137 | if (net::IsCertStatusError(other_errors)) |
Emily Stark | 8b411de0 | 2017-11-23 20:21:27 | [diff] [blame] | 138 | return; |
Emily Stark | 8b411de0 | 2017-11-23 20:21:27 | [diff] [blame] | 139 | |
| 140 | // Record the CT compliance of each request, to give a picture of the |
| 141 | // percentage of overall requests that are CT-compliant. |
| 142 | UMA_HISTOGRAM_ENUMERATION( |
| 143 | "Net.CertificateTransparency.RequestComplianceStatus", |
Emily Stark | 627238f | 2017-11-29 03:29:54 | [diff] [blame] | 144 | ssl_info.ct_policy_compliance, |
Kunihiko Sakamoto | 3646973 | 2018-09-27 03:33:45 | [diff] [blame] | 145 | net::ct::CTPolicyCompliance::CT_POLICY_COUNT); |
Emily Stark | 8b411de0 | 2017-11-23 20:21:27 | [diff] [blame] | 146 | // Record the CT compliance of each request which was required to be CT |
| 147 | // compliant. This gives a picture of the sites that are supposed to be |
| 148 | // compliant and how well they do at actually being compliant. |
| 149 | if (ssl_info.ct_policy_compliance_required) { |
| 150 | UMA_HISTOGRAM_ENUMERATION( |
| 151 | "Net.CertificateTransparency.CTRequiredRequestComplianceStatus", |
Emily Stark | 627238f | 2017-11-29 03:29:54 | [diff] [blame] | 152 | ssl_info.ct_policy_compliance, |
Kunihiko Sakamoto | 3646973 | 2018-09-27 03:33:45 | [diff] [blame] | 153 | net::ct::CTPolicyCompliance::CT_POLICY_COUNT); |
Emily Stark | 8b411de0 | 2017-11-23 20:21:27 | [diff] [blame] | 154 | } |
| 155 | } |
| 156 | |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 157 | net::CookieNetworkSecurity HistogramEntryForCookie( |
| 158 | const net::CanonicalCookie& cookie, |
| 159 | const net::URLRequest& request, |
| 160 | const net::HttpRequestInfo& request_info) { |
| 161 | if (!request_info.url.SchemeIsCryptographic()) { |
| 162 | return net::CookieNetworkSecurity::k1pNonsecureConnection; |
| 163 | } |
| 164 | |
| 165 | if (cookie.IsSecure()) { |
| 166 | return net::CookieNetworkSecurity::k1pSecureAttribute; |
| 167 | } |
| 168 | |
| 169 | net::TransportSecurityState* transport_security_state = |
| 170 | request.context()->transport_security_state(); |
| 171 | net::TransportSecurityState::STSState sts_state; |
| 172 | const std::string cookie_domain = |
| 173 | cookie.IsHostCookie() ? request.url().host() : cookie.Domain().substr(1); |
| 174 | const bool hsts = |
| 175 | transport_security_state->GetSTSState(cookie_domain, &sts_state) && |
| 176 | sts_state.ShouldUpgradeToSSL(); |
| 177 | if (!hsts) { |
| 178 | return net::CookieNetworkSecurity::k1pSecureConnection; |
| 179 | } |
| 180 | |
| 181 | if (cookie.IsHostCookie()) { |
| 182 | if (cookie.IsPersistent() && sts_state.expiry >= cookie.ExpiryDate()) { |
| 183 | return net::CookieNetworkSecurity::k1pHSTSHostCookie; |
| 184 | } else { |
| 185 | // Session cookies are assumed to live forever. |
| 186 | return net::CookieNetworkSecurity::k1pExpiringHSTSHostCookie; |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | // Domain cookies require HSTS to include subdomains to prevent spoofing. |
| 191 | if (sts_state.include_subdomains) { |
| 192 | if (cookie.IsPersistent() && sts_state.expiry >= cookie.ExpiryDate()) { |
| 193 | return net::CookieNetworkSecurity::k1pHSTSSubdomainsIncluded; |
| 194 | } else { |
| 195 | // Session cookies are assumed to live forever. |
| 196 | return net::CookieNetworkSecurity::k1pExpiringHSTSSubdomainsIncluded; |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | return net::CookieNetworkSecurity::k1pHSTSSpoofable; |
| 201 | } |
| 202 | |
Thiemo Nagel | 6c783dbf | 2018-03-21 13:53:30 | [diff] [blame] | 203 | void LogCookieUMA(const net::CookieList& cookie_list, |
| 204 | const net::URLRequest& request, |
| 205 | const net::HttpRequestInfo& request_info) { |
| 206 | const bool secure_request = request_info.url.SchemeIsCryptographic(); |
| 207 | const bool same_site = net::registry_controlled_domains::SameDomainOrHost( |
| 208 | request.url(), request.site_for_cookies(), |
| 209 | net::registry_controlled_domains::INCLUDE_PRIVATE_REGISTRIES); |
Mike West | 8614988 | 2017-07-28 10:41:49 | [diff] [blame] | 210 | |
Thiemo Nagel | 6c783dbf | 2018-03-21 13:53:30 | [diff] [blame] | 211 | const base::Time now = base::Time::Now(); |
| 212 | base::Time oldest = base::Time::Max(); |
| 213 | for (const auto& cookie : cookie_list) { |
| 214 | const std::string histogram_name = |
| 215 | std::string("Cookie.AllAgesFor") + |
| 216 | (secure_request ? "Secure" : "NonSecure") + |
| 217 | (same_site ? "SameSite" : "CrossSite") + "Request"; |
| 218 | const int age_in_days = (now - cookie.CreationDate()).InDays(); |
| 219 | base::UmaHistogramCounts1000(histogram_name, age_in_days); |
Mohsen Izadi | ad6f759 | 2018-04-18 15:57:10 | [diff] [blame] | 220 | oldest = std::min(cookie.CreationDate(), oldest); |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 221 | |
| 222 | net::CookieNetworkSecurity entry = |
| 223 | HistogramEntryForCookie(cookie, request, request_info); |
| 224 | if (!same_site) { |
| 225 | entry = |
| 226 | static_cast<net::CookieNetworkSecurity>(static_cast<int>(entry) | 1); |
| 227 | } |
| 228 | UMA_HISTOGRAM_ENUMERATION("Cookie.NetworkSecurity", entry, |
| 229 | net::CookieNetworkSecurity::kCount); |
Mike West | 8614988 | 2017-07-28 10:41:49 | [diff] [blame] | 230 | } |
Thiemo Nagel | 6c783dbf | 2018-03-21 13:53:30 | [diff] [blame] | 231 | |
| 232 | const std::string histogram_name = |
| 233 | std::string("Cookie.AgeFor") + (secure_request ? "Secure" : "NonSecure") + |
| 234 | (same_site ? "SameSite" : "CrossSite") + "Request"; |
| 235 | const int age_in_days = (now - oldest).InDays(); |
| 236 | base::UmaHistogramCounts1000(histogram_name, age_in_days); |
Mike West | 8614988 | 2017-07-28 10:41:49 | [diff] [blame] | 237 | } |
| 238 | |
mkwst | 202534e3 | 2016-01-15 16:07:15 | [diff] [blame] | 239 | } // namespace |
| 240 | |
[email protected] | 4f5656c6 | 2010-12-13 10:47:09 | [diff] [blame] | 241 | namespace net { |
| 242 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 243 | // TODO(darin): make sure the port blocking code is not lost |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 244 | // static |
[email protected] | 4f5656c6 | 2010-12-13 10:47:09 | [diff] [blame] | 245 | URLRequestJob* URLRequestHttpJob::Factory(URLRequest* request, |
[email protected] | 9f17046 | 2012-08-24 01:06:58 | [diff] [blame] | 246 | NetworkDelegate* network_delegate, |
[email protected] | 4f5656c6 | 2010-12-13 10:47:09 | [diff] [blame] | 247 | const std::string& scheme) { |
[email protected] | f4533ba | 2013-11-28 09:35:41 | [diff] [blame] | 248 | DCHECK(scheme == "http" || scheme == "https" || scheme == "ws" || |
| 249 | scheme == "wss"); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 250 | |
[email protected] | 81293f48 | 2012-08-13 19:35:45 | [diff] [blame] | 251 | if (!request->context()->http_transaction_factory()) { |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 252 | NOTREACHED() << "requires a valid context"; |
[email protected] | 9f17046 | 2012-08-24 01:06:58 | [diff] [blame] | 253 | return new URLRequestErrorJob( |
| 254 | request, network_delegate, ERR_INVALID_ARGUMENT); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 255 | } |
| 256 | |
mgersh | d21d6d14 | 2016-12-14 23:06:36 | [diff] [blame] | 257 | const GURL& url = request->url(); |
| 258 | |
| 259 | // Check for reasons not to return a URLRequestHttpJob. These don't apply to |
| 260 | // https and wss requests. |
| 261 | if (!url.SchemeIsCryptographic()) { |
| 262 | // Check for HSTS upgrade. |
| 263 | TransportSecurityState* hsts = |
| 264 | request->context()->transport_security_state(); |
| 265 | if (hsts && hsts->ShouldUpgradeToSSL(url.host())) { |
| 266 | GURL::Replacements replacements; |
| 267 | replacements.SetSchemeStr( |
Mike West | 8614988 | 2017-07-28 10:41:49 | [diff] [blame] | 268 | |
mgersh | d21d6d14 | 2016-12-14 23:06:36 | [diff] [blame] | 269 | url.SchemeIs(url::kHttpScheme) ? url::kHttpsScheme : url::kWssScheme); |
| 270 | return new URLRequestRedirectJob( |
| 271 | request, network_delegate, url.ReplaceComponents(replacements), |
| 272 | // Use status code 307 to preserve the method, so POST requests work. |
| 273 | URLRequestRedirectJob::REDIRECT_307_TEMPORARY_REDIRECT, "HSTS"); |
| 274 | } |
| 275 | |
| 276 | #if defined(OS_ANDROID) |
| 277 | // Check whether the app allows cleartext traffic to this host, and return |
mgersh | 072720a | 2016-12-15 16:33:34 | [diff] [blame] | 278 | // ERR_CLEARTEXT_NOT_PERMITTED if not. |
mgersh | d21d6d14 | 2016-12-14 23:06:36 | [diff] [blame] | 279 | if (request->context()->check_cleartext_permitted() && |
| 280 | !android::IsCleartextPermitted(url.host())) { |
| 281 | return new URLRequestErrorJob(request, network_delegate, |
| 282 | ERR_CLEARTEXT_NOT_PERMITTED); |
| 283 | } |
| 284 | #endif |
| 285 | } |
mkwst | 549a5c63 | 2016-06-20 14:17:03 | [diff] [blame] | 286 | |
[email protected] | ee4c30d | 2012-11-07 15:08:43 | [diff] [blame] | 287 | return new URLRequestHttpJob(request, |
| 288 | network_delegate, |
| 289 | request->context()->http_user_agent_settings()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 290 | } |
| 291 | |
[email protected] | ee4c30d | 2012-11-07 15:08:43 | [diff] [blame] | 292 | URLRequestHttpJob::URLRequestHttpJob( |
| 293 | URLRequest* request, |
| 294 | NetworkDelegate* network_delegate, |
| 295 | const HttpUserAgentSettings* http_user_agent_settings) |
[email protected] | 9f17046 | 2012-08-24 01:06:58 | [diff] [blame] | 296 | : URLRequestJob(request, network_delegate), |
Aaron Tagliaboschi | 028009e | 2019-03-07 16:28:06 | [diff] [blame] | 297 | num_cookie_lines_left_(0), |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 298 | priority_(DEFAULT_PRIORITY), |
mmenke | 54e82af | 2016-02-16 23:06:30 | [diff] [blame] | 299 | response_info_(nullptr), |
[email protected] | 4f5656c6 | 2010-12-13 10:47:09 | [diff] [blame] | 300 | proxy_auth_state_(AUTH_STATE_DONT_NEED_AUTH), |
| 301 | server_auth_state_(AUTH_STATE_DONT_NEED_AUTH), |
[email protected] | 3589e55 | 2008-08-20 23:11:34 | [diff] [blame] | 302 | read_in_progress_(false), |
mmenke | 54e82af | 2016-02-16 23:06:30 | [diff] [blame] | 303 | throttling_entry_(nullptr), |
[email protected] | bbaea8f | 2011-06-24 00:11:01 | [diff] [blame] | 304 | done_(false), |
[email protected] | 5a07c19 | 2012-07-30 20:18:22 | [diff] [blame] | 305 | awaiting_callback_(false), |
[email protected] | 0981210 | 2014-05-24 00:04:11 | [diff] [blame] | 306 | http_user_agent_settings_(http_user_agent_settings), |
sclittle | ce72c48 | 2015-08-24 20:20:59 | [diff] [blame] | 307 | total_received_bytes_from_previous_transactions_(0), |
Jeremy Roman | d54000b2 | 2019-07-08 18:40:16 | [diff] [blame] | 308 | total_sent_bytes_from_previous_transactions_(0) { |
[email protected] | a73a280 | 2012-05-02 19:20:15 | [diff] [blame] | 309 | URLRequestThrottlerManager* manager = request->context()->throttler_manager(); |
| 310 | if (manager) |
| 311 | throttling_entry_ = manager->RegisterRequestUrl(request->url()); |
| 312 | |
[email protected] | ec23f52 | 2011-02-22 21:01:38 | [diff] [blame] | 313 | ResetTimer(); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 314 | } |
| 315 | |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 316 | URLRequestHttpJob::~URLRequestHttpJob() { |
| 317 | CHECK(!awaiting_callback_); |
| 318 | |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 319 | DoneWithRequest(ABORTED); |
| 320 | } |
| 321 | |
| 322 | void URLRequestHttpJob::SetPriority(RequestPriority priority) { |
| 323 | priority_ = priority; |
| 324 | if (transaction_) |
| 325 | transaction_->SetPriority(priority_); |
| 326 | } |
| 327 | |
| 328 | void URLRequestHttpJob::Start() { |
| 329 | DCHECK(!transaction_.get()); |
| 330 | |
[email protected] | 99ecf6e | 2013-04-10 22:46:13 | [diff] [blame] | 331 | // URLRequest::SetReferrer ensures that we do not send username and password |
| 332 | // fields in the referrer. |
| 333 | GURL referrer(request_->referrer()); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 334 | |
| 335 | request_info_.url = request_->url(); |
| 336 | request_info_.method = request_->method(); |
Shivani Sharma | d81bdd4 | 2019-05-23 17:19:56 | [diff] [blame] | 337 | |
Shivani Sharma | 11c23f55 | 2019-06-11 18:18:39 | [diff] [blame] | 338 | request_info_.network_isolation_key = request_->network_isolation_key(); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 339 | request_info_.load_flags = request_->load_flags(); |
dalyk | 7084d3cd | 2019-10-15 19:49:36 | [diff] [blame] | 340 | request_info_.disable_secure_dns = request_->disable_secure_dns(); |
Ramin Halavati | b5e433e6 | 2018-02-07 07:41:10 | [diff] [blame] | 341 | request_info_.traffic_annotation = |
| 342 | net::MutableNetworkTrafficAnnotationTag(request_->traffic_annotation()); |
Paul Jensen | 60e4553 | 2018-02-19 16:04:18 | [diff] [blame] | 343 | request_info_.socket_tag = request_->socket_tag(); |
Douglas Creager | ef5eecdc | 2018-11-09 20:50:36 | [diff] [blame] | 344 | #if BUILDFLAG(ENABLE_REPORTING) |
| 345 | request_info_.reporting_upload_depth = request_->reporting_upload_depth(); |
| 346 | #endif |
Ramin Halavati | b5e433e6 | 2018-02-07 07:41:10 | [diff] [blame] | 347 | |
mkwst | e3e9599 | 2016-02-23 17:45:41 | [diff] [blame] | 348 | // Privacy mode could still be disabled in SetCookieHeaderAndStart if we are |
| 349 | // going to send previously saved cookies. |
Matt Menke | 7ad78edc | 2018-11-21 19:22:57 | [diff] [blame] | 350 | request_info_.privacy_mode = privacy_mode(); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 351 | |
| 352 | // Strip Referer from request_info_.extra_headers to prevent, e.g., plugins |
| 353 | // from overriding headers that are controlled using other means. Otherwise a |
| 354 | // plugin could set a referrer although sending the referrer is inhibited. |
| 355 | request_info_.extra_headers.RemoveHeader(HttpRequestHeaders::kReferer); |
| 356 | |
rdsmith | 81f60756 | 2014-11-21 18:35:16 | [diff] [blame] | 357 | // Our consumer should have made sure that this is a safe referrer. See for |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 358 | // instance WebCore::FrameLoader::HideReferrer. |
| 359 | if (referrer.is_valid()) { |
Mike West | fbd4411 | 2019-05-08 18:23:37 | [diff] [blame] | 360 | std::string referer_value = referrer.spec(); |
| 361 | UMA_HISTOGRAM_COUNTS_10000("Referrer.HeaderLength", referer_value.length()); |
| 362 | if (base::FeatureList::IsEnabled(features::kCapRefererHeaderLength) && |
| 363 | base::saturated_cast<int>(referer_value.length()) > |
| 364 | features::kMaxRefererHeaderLength.Get()) { |
| 365 | // Strip the referrer down to its origin, but ensure that it's serialized |
| 366 | // as a URL (e.g. retaining a trailing `/` character). |
| 367 | referer_value = url::Origin::Create(referrer).GetURL().spec(); |
| 368 | } |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 369 | request_info_.extra_headers.SetHeader(HttpRequestHeaders::kReferer, |
Mike West | fbd4411 | 2019-05-08 18:23:37 | [diff] [blame] | 370 | referer_value); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 371 | } |
| 372 | |
| 373 | request_info_.extra_headers.SetHeaderIfMissing( |
| 374 | HttpRequestHeaders::kUserAgent, |
| 375 | http_user_agent_settings_ ? |
[email protected] | aa05127 | 2014-03-10 05:56:56 | [diff] [blame] | 376 | http_user_agent_settings_->GetUserAgent() : std::string()); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 377 | |
| 378 | AddExtraHeaders(); |
| 379 | AddCookieHeaderAndStart(); |
| 380 | } |
| 381 | |
| 382 | void URLRequestHttpJob::Kill() { |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 383 | weak_factory_.InvalidateWeakPtrs(); |
mmenke | 2281f376 | 2015-11-02 20:38:17 | [diff] [blame] | 384 | if (transaction_) |
| 385 | DestroyTransaction(); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 386 | URLRequestJob::Kill(); |
| 387 | } |
| 388 | |
ttuttle | 3ae0692 | 2015-05-11 23:41:52 | [diff] [blame] | 389 | void URLRequestHttpJob::GetConnectionAttempts(ConnectionAttempts* out) const { |
| 390 | if (transaction_) |
| 391 | transaction_->GetConnectionAttempts(out); |
| 392 | else |
| 393 | out->clear(); |
| 394 | } |
| 395 | |
ryansturm | 49a8cb1 | 2016-06-15 16:51:09 | [diff] [blame] | 396 | void URLRequestHttpJob::NotifyBeforeSendHeadersCallback( |
[email protected] | 1252d42f | 2014-07-01 21:20:20 | [diff] [blame] | 397 | const ProxyInfo& proxy_info, |
| 398 | HttpRequestHeaders* request_headers) { |
| 399 | DCHECK(request_headers); |
[email protected] | 597a1ab | 2014-06-26 08:12:27 | [diff] [blame] | 400 | DCHECK_NE(URLRequestStatus::CANCELED, GetStatus().status()); |
Tarun Bansal | 4dcebd4 | 2017-10-12 20:12:30 | [diff] [blame] | 401 | if (proxy_info.is_empty()) { |
| 402 | SetProxyServer(ProxyServer::Direct()); |
| 403 | } else { |
| 404 | SetProxyServer(proxy_info.proxy_server()); |
| 405 | } |
[email protected] | 597a1ab | 2014-06-26 08:12:27 | [diff] [blame] | 406 | if (network_delegate()) { |
ryansturm | 49a8cb1 | 2016-06-15 16:51:09 | [diff] [blame] | 407 | network_delegate()->NotifyBeforeSendHeaders( |
| 408 | request_, proxy_info, |
Lily Houghton | 8c2f97d | 2018-01-22 05:06:59 | [diff] [blame] | 409 | request_->context()->proxy_resolution_service()->proxy_retry_info(), |
[email protected] | 1252d42f | 2014-07-01 21:20:20 | [diff] [blame] | 410 | request_headers); |
[email protected] | 597a1ab | 2014-06-26 08:12:27 | [diff] [blame] | 411 | } |
| 412 | } |
| 413 | |
[email protected] | 175adac | 2008-07-30 17:28:04 | [diff] [blame] | 414 | void URLRequestHttpJob::NotifyHeadersComplete() { |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 415 | DCHECK(!response_info_); |
Aaron Tagliaboschi | 028009e | 2019-03-07 16:28:06 | [diff] [blame] | 416 | DCHECK_EQ(0, num_cookie_lines_left_); |
Maks Orlovich | 1571423 | 2019-06-16 23:14:09 | [diff] [blame] | 417 | DCHECK(request_->maybe_stored_cookies().empty()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 418 | |
| 419 | response_info_ = transaction_->GetResponseInfo(); |
| 420 | |
Matt Menke | 5dca1bb | 2019-09-13 20:43:12 | [diff] [blame] | 421 | if (!response_info_->was_cached && throttling_entry_.get()) |
xunjieli | 041e939 | 2015-05-19 21:51:33 | [diff] [blame] | 422 | throttling_entry_->UpdateWithResponse(GetResponseCode()); |
[email protected] | 6b3f964 | 2010-11-25 02:29:06 | [diff] [blame] | 423 | |
[email protected] | fecef22 | 2012-01-05 02:26:15 | [diff] [blame] | 424 | // The ordering of these calls is not important. |
[email protected] | 77f6fb43 | 2009-09-05 14:21:09 | [diff] [blame] | 425 | ProcessStrictTransportSecurityHeader(); |
estark | 1614475f | 2016-03-10 03:46:47 | [diff] [blame] | 426 | ProcessExpectCTHeader(); |
[email protected] | a9cea754 | 2009-05-20 04:30:23 | [diff] [blame] | 427 | |
Lily Chen | f53dfbcd | 2019-08-30 01:42:10 | [diff] [blame] | 428 | // Clear |set_cookie_status_list_| after any processing in case |
Aaron Tagliaboschi | 028009e | 2019-03-07 16:28:06 | [diff] [blame] | 429 | // SaveCookiesAndNotifyHeadersComplete is called again. |
Lily Chen | f53dfbcd | 2019-08-30 01:42:10 | [diff] [blame] | 430 | request_->set_maybe_stored_cookies(std::move(set_cookie_status_list_)); |
Aaron Tagliaboschi | 028009e | 2019-03-07 16:28:06 | [diff] [blame] | 431 | |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 432 | // The HTTP transaction may be restarted several times for the purposes |
| 433 | // of sending authorization information. Each time it restarts, we get |
| 434 | // notified of the headers completion so that we can update the cookie store. |
| 435 | if (transaction_->IsReadyToRestartForAuth()) { |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 436 | // TODO(battre): This breaks the webrequest API for |
| 437 | // URLRequestTestHTTP.BasicAuthWithCookies |
ryansturm | 2343cb6 | 2016-06-15 01:09:00 | [diff] [blame] | 438 | // where OnBeforeStartTransaction -> OnStartTransaction -> |
| 439 | // OnBeforeStartTransaction occurs. |
[email protected] | f3cf980 | 2011-10-28 18:44:58 | [diff] [blame] | 440 | RestartTransactionWithAuth(AuthCredentials()); |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 441 | return; |
| 442 | } |
| 443 | |
[email protected] | 4f5656c6 | 2010-12-13 10:47:09 | [diff] [blame] | 444 | URLRequestJob::NotifyHeadersComplete(); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 445 | } |
| 446 | |
[email protected] | 175adac | 2008-07-30 17:28:04 | [diff] [blame] | 447 | void URLRequestHttpJob::DestroyTransaction() { |
[email protected] | c6a4eb9 | 2010-03-03 23:51:19 | [diff] [blame] | 448 | DCHECK(transaction_.get()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 449 | |
[email protected] | bbaea8f | 2011-06-24 00:11:01 | [diff] [blame] | 450 | DoneWithRequest(ABORTED); |
sclittle | ce72c48 | 2015-08-24 20:20:59 | [diff] [blame] | 451 | |
| 452 | total_received_bytes_from_previous_transactions_ += |
| 453 | transaction_->GetTotalReceivedBytes(); |
sclittle | fb24989 | 2015-09-10 21:33:22 | [diff] [blame] | 454 | total_sent_bytes_from_previous_transactions_ += |
| 455 | transaction_->GetTotalSentBytes(); |
[email protected] | af4876d | 2008-10-21 23:10:57 | [diff] [blame] | 456 | transaction_.reset(); |
David Benjamin | 0bda12f | 2018-02-07 19:58:45 | [diff] [blame] | 457 | response_info_ = nullptr; |
| 458 | override_response_headers_ = nullptr; |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 459 | receive_headers_end_ = base::TimeTicks(); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 460 | } |
| 461 | |
[email protected] | 175adac | 2008-07-30 17:28:04 | [diff] [blame] | 462 | void URLRequestHttpJob::StartTransaction() { |
[email protected] | cc05edc | 2013-03-08 18:04:41 | [diff] [blame] | 463 | if (network_delegate()) { |
David Benjamin | 9776ca2 | 2018-06-13 00:00:15 | [diff] [blame] | 464 | OnCallToDelegate( |
| 465 | NetLogEventType::NETWORK_DELEGATE_BEFORE_START_TRANSACTION); |
mmenke | d3a02b3 | 2016-11-16 20:21:32 | [diff] [blame] | 466 | // The NetworkDelegate must watch for OnRequestDestroyed and not modify |
Matt Mueller | 22372d43 | 2019-04-12 20:45:24 | [diff] [blame] | 467 | // |extra_headers| after it's called. |
| 468 | // TODO(mattm): change the API to remove the out-params and take the |
| 469 | // results as params of the callback. |
ryansturm | 2343cb6 | 2016-06-15 01:09:00 | [diff] [blame] | 470 | int rv = network_delegate()->NotifyBeforeStartTransaction( |
mmenke | d3a02b3 | 2016-11-16 20:21:32 | [diff] [blame] | 471 | request_, |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 472 | base::BindOnce(&URLRequestHttpJob::NotifyBeforeStartTransactionCallback, |
Matt Mueller | 22372d43 | 2019-04-12 20:45:24 | [diff] [blame] | 473 | weak_factory_.GetWeakPtr()), |
[email protected] | 636eccd | 2011-06-28 12:28:01 | [diff] [blame] | 474 | &request_info_.extra_headers); |
| 475 | // If an extension blocks the request, we rely on the callback to |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 476 | // MaybeStartTransactionInternal(). |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 477 | if (rv == ERR_IO_PENDING) |
[email protected] | 636eccd | 2011-06-28 12:28:01 | [diff] [blame] | 478 | return; |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 479 | MaybeStartTransactionInternal(rv); |
| 480 | return; |
[email protected] | 636eccd | 2011-06-28 12:28:01 | [diff] [blame] | 481 | } |
| 482 | StartTransactionInternal(); |
| 483 | } |
| 484 | |
ryansturm | 2343cb6 | 2016-06-15 01:09:00 | [diff] [blame] | 485 | void URLRequestHttpJob::NotifyBeforeStartTransactionCallback(int result) { |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 486 | // Check that there are no callbacks to already canceled requests. |
| 487 | DCHECK_NE(URLRequestStatus::CANCELED, GetStatus().status()); |
| 488 | |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 489 | MaybeStartTransactionInternal(result); |
| 490 | } |
| 491 | |
| 492 | void URLRequestHttpJob::MaybeStartTransactionInternal(int result) { |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 493 | OnCallToDelegateComplete(); |
[email protected] | 636eccd | 2011-06-28 12:28:01 | [diff] [blame] | 494 | if (result == OK) { |
| 495 | StartTransactionInternal(); |
| 496 | } else { |
Eric Roman | 06bd974 | 2019-07-13 15:19:13 | [diff] [blame] | 497 | request_->net_log().AddEventWithStringParams(NetLogEventType::CANCELLED, |
| 498 | "source", "delegate"); |
jam | d3f5a3c | 2016-10-27 18:52:02 | [diff] [blame] | 499 | // Don't call back synchronously to the delegate. |
| 500 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 501 | FROM_HERE, |
kylechar | f4fe517 | 2019-02-15 18:53:49 | [diff] [blame] | 502 | base::BindOnce(&URLRequestHttpJob::NotifyStartError, |
| 503 | weak_factory_.GetWeakPtr(), |
| 504 | URLRequestStatus(URLRequestStatus::FAILED, result))); |
[email protected] | 636eccd | 2011-06-28 12:28:01 | [diff] [blame] | 505 | } |
| 506 | } |
| 507 | |
| 508 | void URLRequestHttpJob::StartTransactionInternal() { |
mmenke | 4600b60 | 2015-10-28 18:52:17 | [diff] [blame] | 509 | // This should only be called while the request's status is IO_PENDING. |
mmenke | 2281f376 | 2015-11-02 20:38:17 | [diff] [blame] | 510 | DCHECK_EQ(URLRequestStatus::IO_PENDING, request_->status().status()); |
David Benjamin | 0bda12f | 2018-02-07 19:58:45 | [diff] [blame] | 511 | DCHECK(!override_response_headers_); |
mmenke | 4600b60 | 2015-10-28 18:52:17 | [diff] [blame] | 512 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 513 | // NOTE: This method assumes that request_info_ is already setup properly. |
| 514 | |
[email protected] | 3460228 | 2010-02-03 22:14:15 | [diff] [blame] | 515 | // If we already have a transaction, then we should restart the transaction |
[email protected] | f3cf980 | 2011-10-28 18:44:58 | [diff] [blame] | 516 | // with auth provided by auth_credentials_. |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 517 | |
[email protected] | 99c0790 | 2010-08-17 18:59:52 | [diff] [blame] | 518 | int rv; |
[email protected] | 6b3f964 | 2010-11-25 02:29:06 | [diff] [blame] | 519 | |
tbansal | 80a5216 | 2016-05-20 17:55:04 | [diff] [blame] | 520 | // Notify NetworkQualityEstimator. |
| 521 | NetworkQualityEstimator* network_quality_estimator = |
| 522 | request()->context()->network_quality_estimator(); |
| 523 | if (network_quality_estimator) |
| 524 | network_quality_estimator->NotifyStartTransaction(*request_); |
| 525 | |
[email protected] | 3460228 | 2010-02-03 22:14:15 | [diff] [blame] | 526 | if (transaction_.get()) { |
mmenke | d3a02b3 | 2016-11-16 20:21:32 | [diff] [blame] | 527 | rv = transaction_->RestartWithAuth( |
Yannic Bonenberger | cc716d4 | 2019-09-04 17:05:36 | [diff] [blame] | 528 | auth_credentials_, base::BindOnce(&URLRequestHttpJob::OnStartCompleted, |
| 529 | base::Unretained(this))); |
[email protected] | f3cf980 | 2011-10-28 18:44:58 | [diff] [blame] | 530 | auth_credentials_ = AuthCredentials(); |
[email protected] | 3460228 | 2010-02-03 22:14:15 | [diff] [blame] | 531 | } else { |
[email protected] | 3460228 | 2010-02-03 22:14:15 | [diff] [blame] | 532 | DCHECK(request_->context()->http_transaction_factory()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 533 | |
[email protected] | 99c0790 | 2010-08-17 18:59:52 | [diff] [blame] | 534 | rv = request_->context()->http_transaction_factory()->CreateTransaction( |
[email protected] | 027bd85a | 2013-12-27 22:39:10 | [diff] [blame] | 535 | priority_, &transaction_); |
[email protected] | f4533ba | 2013-11-28 09:35:41 | [diff] [blame] | 536 | |
| 537 | if (rv == OK && request_info_.url.SchemeIsWSOrWSS()) { |
Bence Béky | a5ec55e6 | 2018-03-07 14:23:28 | [diff] [blame] | 538 | base::SupportsUserData::Data* data = |
| 539 | request_->GetUserData(kWebSocketHandshakeUserDataKey); |
[email protected] | f4533ba | 2013-11-28 09:35:41 | [diff] [blame] | 540 | if (data) { |
| 541 | transaction_->SetWebSocketHandshakeStreamCreateHelper( |
| 542 | static_cast<WebSocketHandshakeStreamBase::CreateHelper*>(data)); |
| 543 | } else { |
| 544 | rv = ERR_DISALLOWED_URL_SCHEME; |
| 545 | } |
| 546 | } |
| 547 | |
[email protected] | 4f5656c6 | 2010-12-13 10:47:09 | [diff] [blame] | 548 | if (rv == OK) { |
ryansturm | 49a8cb1 | 2016-06-15 16:51:09 | [diff] [blame] | 549 | transaction_->SetBeforeHeadersSentCallback( |
| 550 | base::Bind(&URLRequestHttpJob::NotifyBeforeSendHeadersCallback, |
[email protected] | 597a1ab | 2014-06-26 08:12:27 | [diff] [blame] | 551 | base::Unretained(this))); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 552 | transaction_->SetRequestHeadersCallback(request_headers_callback_); |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 553 | transaction_->SetResponseHeadersCallback(response_headers_callback_); |
[email protected] | a45840b | 2014-01-10 15:40:22 | [diff] [blame] | 554 | |
dcheng | c2e01e8 | 2014-08-27 00:24:42 | [diff] [blame] | 555 | if (!throttling_entry_.get() || |
xunjieli | 41edcdd | 2015-06-24 14:26:40 | [diff] [blame] | 556 | !throttling_entry_->ShouldRejectRequest(*request_)) { |
[email protected] | 227b0e8 | 2011-03-25 21:11:53 | [diff] [blame] | 557 | rv = transaction_->Start( |
Yannic Bonenberger | cc716d4 | 2019-09-04 17:05:36 | [diff] [blame] | 558 | &request_info_, |
| 559 | base::BindOnce(&URLRequestHttpJob::OnStartCompleted, |
| 560 | base::Unretained(this)), |
mmenke | d3a02b3 | 2016-11-16 20:21:32 | [diff] [blame] | 561 | request_->net_log()); |
[email protected] | bbaea8f | 2011-06-24 00:11:01 | [diff] [blame] | 562 | start_time_ = base::TimeTicks::Now(); |
[email protected] | 227b0e8 | 2011-03-25 21:11:53 | [diff] [blame] | 563 | } else { |
| 564 | // Special error code for the exponential back-off module. |
| 565 | rv = ERR_TEMPORARILY_THROTTLED; |
| 566 | } |
[email protected] | 3460228 | 2010-02-03 22:14:15 | [diff] [blame] | 567 | } |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 568 | } |
| 569 | |
[email protected] | 4f5656c6 | 2010-12-13 10:47:09 | [diff] [blame] | 570 | if (rv == ERR_IO_PENDING) |
[email protected] | 3460228 | 2010-02-03 22:14:15 | [diff] [blame] | 571 | return; |
| 572 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 573 | // The transaction started synchronously, but we need to notify the |
[email protected] | 4f5656c6 | 2010-12-13 10:47:09 | [diff] [blame] | 574 | // URLRequest delegate via the message loop. |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 575 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
kylechar | f4fe517 | 2019-02-15 18:53:49 | [diff] [blame] | 576 | FROM_HERE, base::BindOnce(&URLRequestHttpJob::OnStartCompleted, |
| 577 | weak_factory_.GetWeakPtr(), rv)); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 578 | } |
| 579 | |
[email protected] | 175adac | 2008-07-30 17:28:04 | [diff] [blame] | 580 | void URLRequestHttpJob::AddExtraHeaders() { |
[email protected] | c7bef94c | 2011-06-21 18:05:51 | [diff] [blame] | 581 | if (!request_info_.extra_headers.HasHeader( |
Helen Li | 7dd8083 | 2017-09-12 18:29:17 | [diff] [blame] | 582 | HttpRequestHeaders::kAcceptEncoding)) { |
Aaron Tagliaboschi | 6c652aa | 2018-11-30 22:13:41 | [diff] [blame] | 583 | // If a range is specifically requested, set the "Accepted Encoding" header |
| 584 | // to "identity" |
| 585 | if (request_info_.extra_headers.HasHeader(HttpRequestHeaders::kRange)) { |
| 586 | request_info_.extra_headers.SetHeader(HttpRequestHeaders::kAcceptEncoding, |
| 587 | "identity"); |
| 588 | } else { |
| 589 | // Advertise "br" encoding only if transferred data is opaque to proxy. |
| 590 | bool advertise_brotli = false; |
| 591 | if (request()->context()->enable_brotli()) { |
| 592 | if (request()->url().SchemeIsCryptographic() || |
| 593 | IsLocalhost(request()->url())) { |
| 594 | advertise_brotli = true; |
| 595 | } |
eustas | 7ff8968 | 2016-07-07 20:23:39 | [diff] [blame] | 596 | } |
eustas | fbec913 | 2015-12-30 14:56:51 | [diff] [blame] | 597 | |
Aaron Tagliaboschi | 6c652aa | 2018-11-30 22:13:41 | [diff] [blame] | 598 | // Supply Accept-Encoding headers first so that it is more likely that |
| 599 | // they will be in the first transmitted packet. This can sometimes make |
| 600 | // it easier to filter and analyze the streams to assure that a proxy has |
| 601 | // not damaged these headers. Some proxies deliberately corrupt |
| 602 | // Accept-Encoding headers. |
| 603 | std::string advertised_encodings = "gzip, deflate"; |
| 604 | if (advertise_brotli) |
| 605 | advertised_encodings += ", br"; |
| 606 | // Tell the server what compression formats are supported. |
| 607 | request_info_.extra_headers.SetHeader(HttpRequestHeaders::kAcceptEncoding, |
| 608 | advertised_encodings); |
| 609 | } |
[email protected] | 423041b | 2008-10-27 17:39:28 | [diff] [blame] | 610 | } |
| 611 | |
[email protected] | ee4c30d | 2012-11-07 15:08:43 | [diff] [blame] | 612 | if (http_user_agent_settings_) { |
[email protected] | 84f0543 | 2013-03-15 01:00:12 | [diff] [blame] | 613 | // Only add default Accept-Language if the request didn't have it |
| 614 | // specified. |
[email protected] | ee4c30d | 2012-11-07 15:08:43 | [diff] [blame] | 615 | std::string accept_language = |
| 616 | http_user_agent_settings_->GetAcceptLanguage(); |
Yao Xiao | 90833e5b | 2019-05-09 16:19:41 | [diff] [blame] | 617 | if (base::FeatureList::IsEnabled(features::kAcceptLanguageHeader) && |
| 618 | !accept_language.empty()) { |
[email protected] | ee4c30d | 2012-11-07 15:08:43 | [diff] [blame] | 619 | request_info_.extra_headers.SetHeaderIfMissing( |
| 620 | HttpRequestHeaders::kAcceptLanguage, |
| 621 | accept_language); |
| 622 | } |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 623 | } |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 624 | } |
| 625 | |
[email protected] | 3460228 | 2010-02-03 22:14:15 | [diff] [blame] | 626 | void URLRequestHttpJob::AddCookieHeaderAndStart() { |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 627 | CookieStore* cookie_store = request_->context()->cookie_store(); |
[email protected] | 1a6fff5 | 2011-10-20 21:00:16 | [diff] [blame] | 628 | if (cookie_store && !(request_info_.load_flags & LOAD_DO_NOT_SEND_COOKIES)) { |
mkwst | e3e9599 | 2016-02-23 17:45:41 | [diff] [blame] | 629 | CookieOptions options; |
Aaron Tagliaboschi | fd4f3f01 | 2019-05-30 23:05:54 | [diff] [blame] | 630 | options.set_return_excluded_cookies(); |
mkwst | e3e9599 | 2016-02-23 17:45:41 | [diff] [blame] | 631 | options.set_include_httponly(); |
Maks Orlovich | 64e2913d | 2019-03-05 14:42:56 | [diff] [blame] | 632 | options.set_same_site_cookie_context( |
| 633 | net::cookie_util::ComputeSameSiteContextForRequest( |
| 634 | request_->method(), request_->url(), request_->site_for_cookies(), |
| 635 | request_->initiator(), request_->attach_same_site_cookies())); |
mkwst | e3e9599 | 2016-02-23 17:45:41 | [diff] [blame] | 636 | cookie_store->GetCookieListWithOptionsAsync( |
| 637 | request_->url(), options, |
Yannic Bonenberger | cc716d4 | 2019-09-04 17:05:36 | [diff] [blame] | 638 | base::BindOnce(&URLRequestHttpJob::SetCookieHeaderAndStart, |
| 639 | weak_factory_.GetWeakPtr(), options)); |
[email protected] | 03d845f | 2011-07-29 19:06:26 | [diff] [blame] | 640 | } else { |
mmenke | d3a02b3 | 2016-11-16 20:21:32 | [diff] [blame] | 641 | StartTransaction(); |
[email protected] | 03d845f | 2011-07-29 19:06:26 | [diff] [blame] | 642 | } |
| 643 | } |
| 644 | |
Aaron Tagliaboschi | a4c64b5 | 2019-01-25 03:28:49 | [diff] [blame] | 645 | void URLRequestHttpJob::SetCookieHeaderAndStart( |
Lily Chen | 3ac7cf4 | 2019-05-30 23:29:29 | [diff] [blame] | 646 | const CookieOptions& options, |
Lily Chen | f068a76 | 2019-08-21 21:10:50 | [diff] [blame] | 647 | const CookieStatusList& cookies_with_status_list, |
Aaron Tagliaboschi | a4c64b5 | 2019-01-25 03:28:49 | [diff] [blame] | 648 | const CookieStatusList& excluded_list) { |
Maks Orlovich | 1571423 | 2019-06-16 23:14:09 | [diff] [blame] | 649 | DCHECK(request_->maybe_sent_cookies().empty()); |
Aaron Tagliaboschi | fd4f3f01 | 2019-05-30 23:05:54 | [diff] [blame] | 650 | |
Lily Chen | f53dfbcd | 2019-08-30 01:42:10 | [diff] [blame] | 651 | // TODO(chlily): This is just for passing to CanGetCookies(), however the |
| 652 | // CookieList parameter of CanGetCookies(), which eventually gets passed to |
| 653 | // the NetworkDelegate, never actually gets used anywhere except in tests. The |
| 654 | // parameter should be removed. |
Lily Chen | f068a76 | 2019-08-21 21:10:50 | [diff] [blame] | 655 | CookieList cookie_list = |
| 656 | net::cookie_util::StripStatuses(cookies_with_status_list); |
| 657 | |
Lily Chen | f53dfbcd | 2019-08-30 01:42:10 | [diff] [blame] | 658 | bool can_get_cookies = CanGetCookies(cookie_list); |
| 659 | if (!cookies_with_status_list.empty() && can_get_cookies) { |
Maks Orlovich | 1571423 | 2019-06-16 23:14:09 | [diff] [blame] | 660 | LogCookieUMA(cookie_list, *request_, request_info_); |
Mike West | 8614988 | 2017-07-28 10:41:49 | [diff] [blame] | 661 | |
Lily Chen | f53dfbcd | 2019-08-30 01:42:10 | [diff] [blame] | 662 | std::string cookie_line = |
| 663 | CanonicalCookie::BuildCookieLine(cookies_with_status_list); |
Maks Orlovich | 1571423 | 2019-06-16 23:14:09 | [diff] [blame] | 664 | UMA_HISTOGRAM_COUNTS_10000("Cookie.HeaderLength", cookie_line.length()); |
| 665 | request_info_.extra_headers.SetHeader(HttpRequestHeaders::kCookie, |
| 666 | cookie_line); |
Mike West | 7c8c2ad | 2018-02-10 07:08:49 | [diff] [blame] | 667 | |
Maks Orlovich | 1571423 | 2019-06-16 23:14:09 | [diff] [blame] | 668 | // Disable privacy mode as we are sending cookies anyway. |
| 669 | request_info_.privacy_mode = PRIVACY_MODE_DISABLED; |
[email protected] | 54f4c936 | 2011-07-25 21:54:46 | [diff] [blame] | 670 | } |
Aaron Tagliaboschi | 41bfbec | 2019-03-12 19:23:48 | [diff] [blame] | 671 | |
Lily Chen | f53dfbcd | 2019-08-30 01:42:10 | [diff] [blame] | 672 | // Report status for things in |excluded_list| and |cookies_with_status_list| |
| 673 | // after the delegate got a chance to block them. |
| 674 | CookieStatusList maybe_sent_cookies = excluded_list; |
| 675 | // CanGetCookies only looks at the fields of the URLRequest, not the cookies |
| 676 | // it is passed, so if CanGetCookies(cookie_list) is false, then |
| 677 | // CanGetCookies(excluded_list) would also be false, so tag also the |
| 678 | // excluded cookies as having been blocked by user preferences. |
| 679 | if (!can_get_cookies) { |
| 680 | for (CookieStatusList::iterator it = maybe_sent_cookies.begin(); |
| 681 | it != maybe_sent_cookies.end(); ++it) { |
| 682 | it->status.AddExclusionReason( |
| 683 | CanonicalCookie::CookieInclusionStatus::EXCLUDE_USER_PREFERENCES); |
Lily Chen | 3ac7cf4 | 2019-05-30 23:29:29 | [diff] [blame] | 684 | } |
| 685 | } |
Lily Chen | f53dfbcd | 2019-08-30 01:42:10 | [diff] [blame] | 686 | for (const auto& cookie_with_status : cookies_with_status_list) { |
| 687 | CanonicalCookie::CookieInclusionStatus status = cookie_with_status.status; |
| 688 | if (!can_get_cookies) { |
| 689 | status.AddExclusionReason( |
| 690 | CanonicalCookie::CookieInclusionStatus::EXCLUDE_USER_PREFERENCES); |
| 691 | } |
| 692 | maybe_sent_cookies.push_back({cookie_with_status.cookie, status}); |
| 693 | } |
Lily Chen | 3ac7cf4 | 2019-05-30 23:29:29 | [diff] [blame] | 694 | |
Lily Chen | 0e079b73 | 2019-10-01 19:48:40 | [diff] [blame] | 695 | if (request_->net_log().IsCapturing()) { |
| 696 | for (const auto& cookie_and_status : maybe_sent_cookies) { |
| 697 | if (!cookie_and_status.status.IsInclude()) { |
Eric Lawrence | f814f5b2 | 2019-10-04 01:52:43 | [diff] [blame] | 698 | request_->net_log().AddEvent( |
| 699 | NetLogEventType::COOKIE_INCLUSION_STATUS, |
| 700 | [&](NetLogCaptureMode capture_mode) { |
| 701 | return CookieExcludedNetLogParams( |
| 702 | "send", cookie_and_status.cookie.Name(), |
| 703 | cookie_and_status.status.GetDebugString(), capture_mode); |
| 704 | }); |
Lily Chen | 0e079b73 | 2019-10-01 19:48:40 | [diff] [blame] | 705 | } |
| 706 | } |
| 707 | } |
| 708 | |
Maks Orlovich | 1571423 | 2019-06-16 23:14:09 | [diff] [blame] | 709 | request_->set_maybe_sent_cookies(std::move(maybe_sent_cookies)); |
Aaron Tagliaboschi | fd4f3f01 | 2019-05-30 23:05:54 | [diff] [blame] | 710 | |
mmenke | d3a02b3 | 2016-11-16 20:21:32 | [diff] [blame] | 711 | StartTransaction(); |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 712 | } |
| 713 | |
[email protected] | ea8141e | 2011-10-05 13:12:51 | [diff] [blame] | 714 | void URLRequestHttpJob::SaveCookiesAndNotifyHeadersComplete(int result) { |
Lily Chen | f53dfbcd | 2019-08-30 01:42:10 | [diff] [blame] | 715 | DCHECK(set_cookie_status_list_.empty()); |
Aaron Tagliaboschi | 028009e | 2019-03-07 16:28:06 | [diff] [blame] | 716 | DCHECK_EQ(0, num_cookie_lines_left_); |
| 717 | |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 718 | // End of the call started in OnStartCompleted. |
| 719 | OnCallToDelegateComplete(); |
| 720 | |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 721 | if (result != OK) { |
Eric Roman | 06bd974 | 2019-07-13 15:19:13 | [diff] [blame] | 722 | request_->net_log().AddEventWithStringParams(NetLogEventType::CANCELLED, |
| 723 | "source", "delegate"); |
[email protected] | ea8141e | 2011-10-05 13:12:51 | [diff] [blame] | 724 | NotifyStartError(URLRequestStatus(URLRequestStatus::FAILED, result)); |
| 725 | return; |
| 726 | } |
| 727 | |
Aaron Tagliaboschi | 028009e | 2019-03-07 16:28:06 | [diff] [blame] | 728 | if ((request_info_.load_flags & LOAD_DO_NOT_SAVE_COOKIES) || |
| 729 | !request_->context()->cookie_store()) { |
| 730 | NotifyHeadersComplete(); |
| 731 | return; |
| 732 | } |
| 733 | |
mmenke | 54e82af | 2016-02-16 23:06:30 | [diff] [blame] | 734 | base::Time response_date; |
Lily Chen | b851acc | 2019-08-07 15:54:44 | [diff] [blame] | 735 | base::Optional<base::Time> server_time = base::nullopt; |
| 736 | if (GetResponseHeaders()->GetDateValue(&response_date)) |
| 737 | server_time = base::make_optional(response_date); |
[email protected] | 263163f | 2012-06-14 22:40:34 | [diff] [blame] | 738 | |
Aaron Tagliaboschi | 028009e | 2019-03-07 16:28:06 | [diff] [blame] | 739 | CookieOptions options; |
| 740 | options.set_include_httponly(); |
Maks Orlovich | fdbc8be | 2019-03-18 18:34:52 | [diff] [blame] | 741 | options.set_same_site_cookie_context( |
Maks Orlovich | 3637afd2 | 2019-05-06 20:37:20 | [diff] [blame] | 742 | net::cookie_util::ComputeSameSiteContextForResponse( |
| 743 | request_->url(), request_->site_for_cookies(), |
| 744 | request_->initiator())); |
jww | 79aceda | 2015-12-07 01:56:34 | [diff] [blame] | 745 | |
Aaron Tagliaboschi | fd4f3f01 | 2019-05-30 23:05:54 | [diff] [blame] | 746 | options.set_return_excluded_cookies(); |
| 747 | |
Aaron Tagliaboschi | 028009e | 2019-03-07 16:28:06 | [diff] [blame] | 748 | // Set all cookies, without waiting for them to be set. Any subsequent read |
| 749 | // will see the combined result of all cookie operation. |
| 750 | const base::StringPiece name("Set-Cookie"); |
| 751 | std::string cookie_string; |
| 752 | size_t iter = 0; |
| 753 | HttpResponseHeaders* headers = GetResponseHeaders(); |
| 754 | |
| 755 | // NotifyHeadersComplete needs to be called once and only once after the |
| 756 | // list has been fully processed, and it can either be called in the |
| 757 | // callback or after the loop is called, depending on how the last element |
| 758 | // was handled. |num_cookie_lines_left_| keeps track of how many async |
| 759 | // callbacks are currently out (starting from 1 to make sure the loop runs all |
| 760 | // the way through before trying to exit). If there are any callbacks still |
| 761 | // waiting when the loop ends, then NotifyHeadersComplete will be called when |
| 762 | // it reaches 0 in the callback itself. |
| 763 | num_cookie_lines_left_ = 1; |
| 764 | while (headers->EnumerateHeader(&iter, name, &cookie_string)) { |
| 765 | CanonicalCookie::CookieInclusionStatus returned_status; |
| 766 | |
| 767 | num_cookie_lines_left_++; |
| 768 | |
| 769 | std::unique_ptr<CanonicalCookie> cookie = net::CanonicalCookie::Create( |
Lily Chen | b851acc | 2019-08-07 15:54:44 | [diff] [blame] | 770 | request_->url(), cookie_string, base::Time::Now(), server_time, |
Aaron Tagliaboschi | 028009e | 2019-03-07 16:28:06 | [diff] [blame] | 771 | &returned_status); |
| 772 | |
Lily Chen | f53dfbcd | 2019-08-30 01:42:10 | [diff] [blame] | 773 | base::Optional<CanonicalCookie> cookie_to_return = base::nullopt; |
| 774 | if (returned_status.IsInclude()) { |
| 775 | DCHECK(cookie); |
| 776 | // Make a copy of the cookie if we successfully made one. |
| 777 | cookie_to_return = *cookie; |
| 778 | } |
| 779 | if (cookie && !CanSetCookie(*cookie, &options)) { |
| 780 | returned_status.AddExclusionReason( |
| 781 | CanonicalCookie::CookieInclusionStatus::EXCLUDE_USER_PREFERENCES); |
| 782 | } |
| 783 | if (!returned_status.IsInclude()) { |
| 784 | OnSetCookieResult(options, cookie_to_return, std::move(cookie_string), |
Aaron Tagliaboschi | fd4f3f01 | 2019-05-30 23:05:54 | [diff] [blame] | 785 | returned_status); |
Aaron Tagliaboschi | 028009e | 2019-03-07 16:28:06 | [diff] [blame] | 786 | continue; |
[email protected] | 263163f | 2012-06-14 22:40:34 | [diff] [blame] | 787 | } |
Lily Chen | 0f208ea | 2019-08-08 23:37:53 | [diff] [blame] | 788 | |
| 789 | request_->context()->cookie_store()->SetCanonicalCookieAsync( |
| 790 | std::move(cookie), request_->url().scheme(), options, |
Lily Chen | 3ac7cf4 | 2019-05-30 23:29:29 | [diff] [blame] | 791 | base::BindOnce(&URLRequestHttpJob::OnSetCookieResult, |
Lily Chen | f53dfbcd | 2019-08-30 01:42:10 | [diff] [blame] | 792 | weak_factory_.GetWeakPtr(), options, cookie_to_return, |
Lily Chen | 3ac7cf4 | 2019-05-30 23:29:29 | [diff] [blame] | 793 | cookie_string)); |
Aaron Tagliaboschi | 028009e | 2019-03-07 16:28:06 | [diff] [blame] | 794 | } |
| 795 | // Removing the 1 that |num_cookie_lines_left| started with, signifing that |
| 796 | // loop has been exited. |
| 797 | num_cookie_lines_left_--; |
| 798 | |
| 799 | if (num_cookie_lines_left_ == 0) |
| 800 | NotifyHeadersComplete(); |
| 801 | } |
| 802 | |
| 803 | void URLRequestHttpJob::OnSetCookieResult( |
Lily Chen | 3ac7cf4 | 2019-05-30 23:29:29 | [diff] [blame] | 804 | const CookieOptions& options, |
Aaron Tagliaboschi | fd4f3f01 | 2019-05-30 23:05:54 | [diff] [blame] | 805 | base::Optional<CanonicalCookie> cookie, |
Aaron Tagliaboschi | 028009e | 2019-03-07 16:28:06 | [diff] [blame] | 806 | std::string cookie_string, |
| 807 | CanonicalCookie::CookieInclusionStatus status) { |
Lily Chen | 0e079b73 | 2019-10-01 19:48:40 | [diff] [blame] | 808 | if (!status.IsInclude() && request_->net_log().IsCapturing()) { |
Eric Lawrence | f814f5b2 | 2019-10-04 01:52:43 | [diff] [blame] | 809 | request_->net_log().AddEvent(NetLogEventType::COOKIE_INCLUSION_STATUS, |
| 810 | [&](NetLogCaptureMode capture_mode) { |
| 811 | return CookieExcludedNetLogParams( |
| 812 | "store", |
| 813 | cookie ? cookie.value().Name() : "", |
| 814 | status.GetDebugString(), capture_mode); |
| 815 | }); |
Lily Chen | 0e079b73 | 2019-10-01 19:48:40 | [diff] [blame] | 816 | } |
Lily Chen | f53dfbcd | 2019-08-30 01:42:10 | [diff] [blame] | 817 | set_cookie_status_list_.emplace_back(std::move(cookie), |
| 818 | std::move(cookie_string), status); |
Aaron Tagliaboschi | 028009e | 2019-03-07 16:28:06 | [diff] [blame] | 819 | |
| 820 | num_cookie_lines_left_--; |
| 821 | |
Lily Chen | f53dfbcd | 2019-08-30 01:42:10 | [diff] [blame] | 822 | // If all the cookie lines have been handled, |set_cookie_status_list_| now |
| 823 | // reflects the result of all Set-Cookie lines, and the request can be |
| 824 | // continued. |
Aaron Tagliaboschi | 028009e | 2019-03-07 16:28:06 | [diff] [blame] | 825 | if (num_cookie_lines_left_ == 0) |
| 826 | NotifyHeadersComplete(); |
[email protected] | 3460228 | 2010-02-03 22:14:15 | [diff] [blame] | 827 | } |
| 828 | |
[email protected] | 77f6fb43 | 2009-09-05 14:21:09 | [diff] [blame] | 829 | void URLRequestHttpJob::ProcessStrictTransportSecurityHeader() { |
[email protected] | a9cea754 | 2009-05-20 04:30:23 | [diff] [blame] | 830 | DCHECK(response_info_); |
[email protected] | 6ed72be | 2013-01-08 22:07:33 | [diff] [blame] | 831 | TransportSecurityState* security_state = |
| 832 | request_->context()->transport_security_state(); |
[email protected] | e88006f | 2012-01-11 06:15:07 | [diff] [blame] | 833 | const SSLInfo& ssl_info = response_info_->ssl_info; |
[email protected] | a9cea754 | 2009-05-20 04:30:23 | [diff] [blame] | 834 | |
[email protected] | 6ed72be | 2013-01-08 22:07:33 | [diff] [blame] | 835 | // Only accept HSTS headers on HTTPS connections that have no |
| 836 | // certificate errors. |
[email protected] | e88006f | 2012-01-11 06:15:07 | [diff] [blame] | 837 | if (!ssl_info.is_valid() || IsCertStatusError(ssl_info.cert_status) || |
mmenke | d3a02b3 | 2016-11-16 20:21:32 | [diff] [blame] | 838 | !security_state) { |
[email protected] | e88006f | 2012-01-11 06:15:07 | [diff] [blame] | 839 | return; |
mmenke | d3a02b3 | 2016-11-16 20:21:32 | [diff] [blame] | 840 | } |
[email protected] | 326e679 | 2009-12-11 21:04:42 | [diff] [blame] | 841 | |
estark | a5da7670 | 2015-04-09 04:00:16 | [diff] [blame] | 842 | // Don't accept HSTS headers when the hostname is an IP address. |
| 843 | if (request_info_.url.HostIsIPAddress()) |
| 844 | return; |
| 845 | |
[email protected] | 242d856 | 2012-10-30 21:20:46 | [diff] [blame] | 846 | // http://tools.ietf.org/html/draft-ietf-websec-strict-transport-sec: |
| 847 | // |
| 848 | // If a UA receives more than one STS header field in a HTTP response |
| 849 | // message over secure transport, then the UA MUST process only the |
| 850 | // first such header field. |
[email protected] | 6ed72be | 2013-01-08 22:07:33 | [diff] [blame] | 851 | HttpResponseHeaders* headers = GetResponseHeaders(); |
| 852 | std::string value; |
olli.raula | ee489a5 | 2016-01-25 08:37:10 | [diff] [blame] | 853 | if (headers->EnumerateHeader(nullptr, "Strict-Transport-Security", &value)) |
[email protected] | 6ed72be | 2013-01-08 22:07:33 | [diff] [blame] | 854 | security_state->AddHSTSHeader(request_info_.url.host(), value); |
[email protected] | a9cea754 | 2009-05-20 04:30:23 | [diff] [blame] | 855 | } |
[email protected] | 4f5656c6 | 2010-12-13 10:47:09 | [diff] [blame] | 856 | |
estark | 1614475f | 2016-03-10 03:46:47 | [diff] [blame] | 857 | void URLRequestHttpJob::ProcessExpectCTHeader() { |
| 858 | DCHECK(response_info_); |
| 859 | TransportSecurityState* security_state = |
| 860 | request_->context()->transport_security_state(); |
| 861 | const SSLInfo& ssl_info = response_info_->ssl_info; |
| 862 | |
| 863 | // Only accept Expect CT headers on HTTPS connections that have no |
| 864 | // certificate errors. |
| 865 | if (!ssl_info.is_valid() || IsCertStatusError(ssl_info.cert_status) || |
| 866 | !security_state) { |
| 867 | return; |
| 868 | } |
| 869 | |
estark | 1614475f | 2016-03-10 03:46:47 | [diff] [blame] | 870 | HttpResponseHeaders* headers = GetResponseHeaders(); |
| 871 | std::string value; |
Emily Stark | 4cfecf07 | 2017-08-08 01:05:51 | [diff] [blame] | 872 | if (headers->GetNormalizedHeader("Expect-CT", &value)) { |
estark | 1614475f | 2016-03-10 03:46:47 | [diff] [blame] | 873 | security_state->ProcessExpectCTHeader( |
| 874 | value, HostPortPair::FromURL(request_info_.url), ssl_info); |
| 875 | } |
| 876 | } |
| 877 | |
[email protected] | 5394e42 | 2011-01-20 22:07:43 | [diff] [blame] | 878 | void URLRequestHttpJob::OnStartCompleted(int result) { |
Alexandr Ilin | 3312663 | 2018-11-14 14:48:17 | [diff] [blame] | 879 | TRACE_EVENT0(NetTracingCategory(), "URLRequestHttpJob::OnStartCompleted"); |
[email protected] | ec23f52 | 2011-02-22 21:01:38 | [diff] [blame] | 880 | RecordTimer(); |
| 881 | |
[email protected] | 80abdad | 2014-03-15 00:20:54 | [diff] [blame] | 882 | // If the job is done (due to cancellation), can just ignore this |
| 883 | // notification. |
| 884 | if (done_) |
[email protected] | 5394e42 | 2011-01-20 22:07:43 | [diff] [blame] | 885 | return; |
| 886 | |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 887 | receive_headers_end_ = base::TimeTicks::Now(); |
| 888 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 889 | const URLRequestContext* context = request_->context(); |
| 890 | |
Ryan Sleevi | 19a7bde | 2017-11-22 06:51:39 | [diff] [blame] | 891 | if (transaction_ && transaction_->GetResponseInfo()) { |
| 892 | const SSLInfo& ssl_info = transaction_->GetResponseInfo()->ssl_info; |
Matt Mueller | 93651144 | 2019-09-03 18:15:12 | [diff] [blame] | 893 | if (!IsCertificateError(result)) { |
Ryan Sleevi | 19a7bde | 2017-11-22 06:51:39 | [diff] [blame] | 894 | LogTrustAnchor(ssl_info.public_key_hashes); |
| 895 | } |
Emily Stark | 8b411de0 | 2017-11-23 20:21:27 | [diff] [blame] | 896 | |
| 897 | RecordCTHistograms(ssl_info); |
Ryan Sleevi | 19a7bde | 2017-11-22 06:51:39 | [diff] [blame] | 898 | } |
| 899 | |
Wojciech Dzierżanowski | abdeeaf | 2019-04-01 20:16:22 | [diff] [blame] | 900 | if (transaction_ && transaction_->GetResponseInfo()) { |
| 901 | SetProxyServer(transaction_->GetResponseInfo()->proxy_server); |
| 902 | } |
| 903 | |
[email protected] | 5394e42 | 2011-01-20 22:07:43 | [diff] [blame] | 904 | if (result == OK) { |
[email protected] | ea8141e | 2011-10-05 13:12:51 | [diff] [blame] | 905 | scoped_refptr<HttpResponseHeaders> headers = GetResponseHeaders(); |
hiroshige | cb76caa4c5 | 2015-12-22 07:50:25 | [diff] [blame] | 906 | |
[email protected] | cc05edc | 2013-03-08 18:04:41 | [diff] [blame] | 907 | if (network_delegate()) { |
[email protected] | ea8141e | 2011-10-05 13:12:51 | [diff] [blame] | 908 | // Note that |this| may not be deleted until |
mmenke | d3a02b3 | 2016-11-16 20:21:32 | [diff] [blame] | 909 | // |URLRequestHttpJob::OnHeadersReceivedCallback()| or |
[email protected] | ea8141e | 2011-10-05 13:12:51 | [diff] [blame] | 910 | // |NetworkDelegate::URLRequestDestroyed()| has been called. |
David Benjamin | 9776ca2 | 2018-06-13 00:00:15 | [diff] [blame] | 911 | OnCallToDelegate(NetLogEventType::NETWORK_DELEGATE_HEADERS_RECEIVED); |
Matt Menke | eed1a24 | 2019-10-23 21:26:06 | [diff] [blame^] | 912 | preserve_fragment_on_redirect_url_ = base::nullopt; |
Yutaka Hirano | 832c890 | 2019-10-16 10:04:34 | [diff] [blame] | 913 | IPEndPoint endpoint; |
| 914 | if (transaction_) |
| 915 | transaction_->GetRemoteEndpoint(&endpoint); |
mmenke | d3a02b3 | 2016-11-16 20:21:32 | [diff] [blame] | 916 | // The NetworkDelegate must watch for OnRequestDestroyed and not modify |
Matt Mueller | be9d8e8 | 2019-03-15 21:38:44 | [diff] [blame] | 917 | // any of the arguments after it's called. |
| 918 | // TODO(mattm): change the API to remove the out-params and take the |
| 919 | // results as params of the callback. |
[email protected] | cc05edc | 2013-03-08 18:04:41 | [diff] [blame] | 920 | int error = network_delegate()->NotifyHeadersReceived( |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 921 | request_, |
| 922 | base::BindOnce(&URLRequestHttpJob::OnHeadersReceivedCallback, |
Matt Mueller | be9d8e8 | 2019-03-15 21:38:44 | [diff] [blame] | 923 | weak_factory_.GetWeakPtr()), |
Yutaka Hirano | 832c890 | 2019-10-16 10:04:34 | [diff] [blame] | 924 | headers.get(), &override_response_headers_, endpoint, |
Matt Menke | eed1a24 | 2019-10-23 21:26:06 | [diff] [blame^] | 925 | &preserve_fragment_on_redirect_url_); |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 926 | if (error != OK) { |
| 927 | if (error == ERR_IO_PENDING) { |
[email protected] | ea8141e | 2011-10-05 13:12:51 | [diff] [blame] | 928 | awaiting_callback_ = true; |
[email protected] | ea8141e | 2011-10-05 13:12:51 | [diff] [blame] | 929 | } else { |
Eric Roman | 06bd974 | 2019-07-13 15:19:13 | [diff] [blame] | 930 | request_->net_log().AddEventWithStringParams( |
| 931 | NetLogEventType::CANCELLED, "source", "delegate"); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 932 | OnCallToDelegateComplete(); |
[email protected] | ea8141e | 2011-10-05 13:12:51 | [diff] [blame] | 933 | NotifyStartError(URLRequestStatus(URLRequestStatus::FAILED, error)); |
| 934 | } |
| 935 | return; |
| 936 | } |
| 937 | } |
| 938 | |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 939 | SaveCookiesAndNotifyHeadersComplete(OK); |
[email protected] | e5624f0 | 2011-09-27 19:43:53 | [diff] [blame] | 940 | } else if (IsCertificateError(result)) { |
[email protected] | 6061c14 | 2013-10-21 15:13:34 | [diff] [blame] | 941 | // We encountered an SSL certificate error. |
rsleevi | 9545d34 | 2016-06-21 03:17:37 | [diff] [blame] | 942 | // Maybe overridable, maybe not. Ask the delegate to decide. |
| 943 | TransportSecurityState* state = context->transport_security_state(); |
| 944 | NotifySSLCertificateError( |
Emily Stark | 79fba584 | 2019-04-25 04:59:36 | [diff] [blame] | 945 | result, transaction_->GetResponseInfo()->ssl_info, |
rsleevi | 9545d34 | 2016-06-21 03:17:37 | [diff] [blame] | 946 | state->ShouldSSLErrorsBeFatal(request_info_.url.host())); |
[email protected] | 5394e42 | 2011-01-20 22:07:43 | [diff] [blame] | 947 | } else if (result == ERR_SSL_CLIENT_AUTH_CERT_NEEDED) { |
[email protected] | a83dd33 | 2011-07-13 10:41:01 | [diff] [blame] | 948 | NotifyCertificateRequested( |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 949 | transaction_->GetResponseInfo()->cert_request_info.get()); |
[email protected] | 5394e42 | 2011-01-20 22:07:43 | [diff] [blame] | 950 | } else { |
[email protected] | 419704c | 2014-01-14 11:18:06 | [diff] [blame] | 951 | // Even on an error, there may be useful information in the response |
| 952 | // info (e.g. whether there's a cached copy). |
| 953 | if (transaction_.get()) |
| 954 | response_info_ = transaction_->GetResponseInfo(); |
[email protected] | 5394e42 | 2011-01-20 22:07:43 | [diff] [blame] | 955 | NotifyStartError(URLRequestStatus(URLRequestStatus::FAILED, result)); |
| 956 | } |
| 957 | } |
| 958 | |
[email protected] | ea8141e | 2011-10-05 13:12:51 | [diff] [blame] | 959 | void URLRequestHttpJob::OnHeadersReceivedCallback(int result) { |
[email protected] | ea8141e | 2011-10-05 13:12:51 | [diff] [blame] | 960 | awaiting_callback_ = false; |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 961 | |
| 962 | // Check that there are no callbacks to already canceled requests. |
| 963 | DCHECK_NE(URLRequestStatus::CANCELED, GetStatus().status()); |
| 964 | |
[email protected] | ea8141e | 2011-10-05 13:12:51 | [diff] [blame] | 965 | SaveCookiesAndNotifyHeadersComplete(result); |
| 966 | } |
| 967 | |
[email protected] | 5394e42 | 2011-01-20 22:07:43 | [diff] [blame] | 968 | void URLRequestHttpJob::OnReadCompleted(int result) { |
Alexandr Ilin | 3312663 | 2018-11-14 14:48:17 | [diff] [blame] | 969 | TRACE_EVENT0(NetTracingCategory(), "URLRequestHttpJob::OnReadCompleted"); |
[email protected] | 5394e42 | 2011-01-20 22:07:43 | [diff] [blame] | 970 | read_in_progress_ = false; |
| 971 | |
xunjieli | 26ede96 | 2015-11-23 19:39:13 | [diff] [blame] | 972 | DCHECK_NE(ERR_IO_PENDING, result); |
| 973 | |
[email protected] | f001bd6a | 2011-12-08 04:31:37 | [diff] [blame] | 974 | if (ShouldFixMismatchedContentLength(result)) |
[email protected] | 5543cbb | 2012-04-20 16:35:23 | [diff] [blame] | 975 | result = OK; |
[email protected] | f001bd6a | 2011-12-08 04:31:37 | [diff] [blame] | 976 | |
xunjieli | 26ede96 | 2015-11-23 19:39:13 | [diff] [blame] | 977 | // EOF or error, done with this job. |
| 978 | if (result <= 0) |
| 979 | DoneWithRequest(FINISHED); |
[email protected] | 5394e42 | 2011-01-20 22:07:43 | [diff] [blame] | 980 | |
xunjieli | 26ede96 | 2015-11-23 19:39:13 | [diff] [blame] | 981 | ReadRawDataComplete(result); |
[email protected] | 5394e42 | 2011-01-20 22:07:43 | [diff] [blame] | 982 | } |
| 983 | |
[email protected] | 5394e42 | 2011-01-20 22:07:43 | [diff] [blame] | 984 | void URLRequestHttpJob::RestartTransactionWithAuth( |
[email protected] | f3cf980 | 2011-10-28 18:44:58 | [diff] [blame] | 985 | const AuthCredentials& credentials) { |
| 986 | auth_credentials_ = credentials; |
[email protected] | 5394e42 | 2011-01-20 22:07:43 | [diff] [blame] | 987 | |
| 988 | // These will be reset in OnStartCompleted. |
David Benjamin | 0bda12f | 2018-02-07 19:58:45 | [diff] [blame] | 989 | response_info_ = nullptr; |
| 990 | override_response_headers_ = nullptr; // See https://crbug.com/801237. |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 991 | receive_headers_end_ = base::TimeTicks(); |
[email protected] | 5394e42 | 2011-01-20 22:07:43 | [diff] [blame] | 992 | |
[email protected] | ec23f52 | 2011-02-22 21:01:38 | [diff] [blame] | 993 | ResetTimer(); |
| 994 | |
[email protected] | 5394e42 | 2011-01-20 22:07:43 | [diff] [blame] | 995 | // Update the cookies, since the cookie store may have been updated from the |
| 996 | // headers in the 401/407. Since cookies were already appended to |
| 997 | // extra_headers, we need to strip them out before adding them again. |
[email protected] | ea8141e | 2011-10-05 13:12:51 | [diff] [blame] | 998 | request_info_.extra_headers.RemoveHeader(HttpRequestHeaders::kCookie); |
[email protected] | 5394e42 | 2011-01-20 22:07:43 | [diff] [blame] | 999 | |
Aaron Tagliaboschi | fd4f3f01 | 2019-05-30 23:05:54 | [diff] [blame] | 1000 | // TODO(https://crbug.com/968327/): This is weird, as all other clearing is at |
| 1001 | // the URLRequest layer. Should this call into URLRequest so it can share |
| 1002 | // logic at that layer with SetAuth()? |
Maks Orlovich | 1571423 | 2019-06-16 23:14:09 | [diff] [blame] | 1003 | request_->set_maybe_sent_cookies({}); |
| 1004 | request_->set_maybe_stored_cookies({}); |
Aaron Tagliaboschi | fd4f3f01 | 2019-05-30 23:05:54 | [diff] [blame] | 1005 | |
[email protected] | 5394e42 | 2011-01-20 22:07:43 | [diff] [blame] | 1006 | AddCookieHeaderAndStart(); |
| 1007 | } |
| 1008 | |
[email protected] | 0736d9e | 2012-11-28 19:50:40 | [diff] [blame] | 1009 | void URLRequestHttpJob::SetUpload(UploadDataStream* upload) { |
[email protected] | 5394e42 | 2011-01-20 22:07:43 | [diff] [blame] | 1010 | DCHECK(!transaction_.get()) << "cannot change once started"; |
[email protected] | 0736d9e | 2012-11-28 19:50:40 | [diff] [blame] | 1011 | request_info_.upload_data_stream = upload; |
[email protected] | 5394e42 | 2011-01-20 22:07:43 | [diff] [blame] | 1012 | } |
| 1013 | |
| 1014 | void URLRequestHttpJob::SetExtraRequestHeaders( |
| 1015 | const HttpRequestHeaders& headers) { |
| 1016 | DCHECK(!transaction_.get()) << "cannot change once started"; |
| 1017 | request_info_.extra_headers.CopyFrom(headers); |
| 1018 | } |
| 1019 | |
[email protected] | 5394e42 | 2011-01-20 22:07:43 | [diff] [blame] | 1020 | LoadState URLRequestHttpJob::GetLoadState() const { |
| 1021 | return transaction_.get() ? |
| 1022 | transaction_->GetLoadState() : LOAD_STATE_IDLE; |
| 1023 | } |
| 1024 | |
[email protected] | 5394e42 | 2011-01-20 22:07:43 | [diff] [blame] | 1025 | bool URLRequestHttpJob::GetMimeType(std::string* mime_type) const { |
| 1026 | DCHECK(transaction_.get()); |
| 1027 | |
| 1028 | if (!response_info_) |
| 1029 | return false; |
| 1030 | |
ellyjones | 0e9d5e8 | 2015-02-17 23:06:28 | [diff] [blame] | 1031 | HttpResponseHeaders* headers = GetResponseHeaders(); |
| 1032 | if (!headers) |
| 1033 | return false; |
| 1034 | return headers->GetMimeType(mime_type); |
[email protected] | 5394e42 | 2011-01-20 22:07:43 | [diff] [blame] | 1035 | } |
| 1036 | |
| 1037 | bool URLRequestHttpJob::GetCharset(std::string* charset) { |
| 1038 | DCHECK(transaction_.get()); |
| 1039 | |
| 1040 | if (!response_info_) |
| 1041 | return false; |
| 1042 | |
[email protected] | ea8141e | 2011-10-05 13:12:51 | [diff] [blame] | 1043 | return GetResponseHeaders()->GetCharset(charset); |
[email protected] | 5394e42 | 2011-01-20 22:07:43 | [diff] [blame] | 1044 | } |
| 1045 | |
| 1046 | void URLRequestHttpJob::GetResponseInfo(HttpResponseInfo* info) { |
[email protected] | ea8141e | 2011-10-05 13:12:51 | [diff] [blame] | 1047 | if (response_info_) { |
[email protected] | 419704c | 2014-01-14 11:18:06 | [diff] [blame] | 1048 | DCHECK(transaction_.get()); |
| 1049 | |
[email protected] | 5394e42 | 2011-01-20 22:07:43 | [diff] [blame] | 1050 | *info = *response_info_; |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 1051 | if (override_response_headers_.get()) |
[email protected] | ea8141e | 2011-10-05 13:12:51 | [diff] [blame] | 1052 | info->headers = override_response_headers_; |
| 1053 | } |
[email protected] | 5394e42 | 2011-01-20 22:07:43 | [diff] [blame] | 1054 | } |
| 1055 | |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 1056 | void URLRequestHttpJob::GetLoadTimingInfo( |
| 1057 | LoadTimingInfo* load_timing_info) const { |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 1058 | // If haven't made it far enough to receive any headers, don't return |
rdsmith | 81f60756 | 2014-11-21 18:35:16 | [diff] [blame] | 1059 | // anything. This makes for more consistent behavior in the case of errors. |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 1060 | if (!transaction_ || receive_headers_end_.is_null()) |
| 1061 | return; |
| 1062 | if (transaction_->GetLoadTimingInfo(load_timing_info)) |
| 1063 | load_timing_info->receive_headers_end = receive_headers_end_; |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 1064 | } |
| 1065 | |
Tsuyoshi Horo | 01faed6 | 2019-02-20 22:11:37 | [diff] [blame] | 1066 | bool URLRequestHttpJob::GetTransactionRemoteEndpoint( |
| 1067 | IPEndPoint* endpoint) const { |
ttuttle | d9dbc65 | 2015-09-29 20:00:59 | [diff] [blame] | 1068 | if (!transaction_) |
| 1069 | return false; |
| 1070 | |
| 1071 | return transaction_->GetRemoteEndpoint(endpoint); |
| 1072 | } |
| 1073 | |
[email protected] | 5394e42 | 2011-01-20 22:07:43 | [diff] [blame] | 1074 | int URLRequestHttpJob::GetResponseCode() const { |
| 1075 | DCHECK(transaction_.get()); |
| 1076 | |
| 1077 | if (!response_info_) |
| 1078 | return -1; |
| 1079 | |
[email protected] | ea8141e | 2011-10-05 13:12:51 | [diff] [blame] | 1080 | return GetResponseHeaders()->response_code(); |
[email protected] | 5394e42 | 2011-01-20 22:07:43 | [diff] [blame] | 1081 | } |
| 1082 | |
zhongyi | 48704c18 | 2015-12-07 07:52:02 | [diff] [blame] | 1083 | void URLRequestHttpJob::PopulateNetErrorDetails( |
| 1084 | NetErrorDetails* details) const { |
| 1085 | if (!transaction_) |
| 1086 | return; |
| 1087 | return transaction_->PopulateNetErrorDetails(details); |
| 1088 | } |
| 1089 | |
xunjieli | 28a1877 | 2016-10-25 15:30:06 | [diff] [blame] | 1090 | std::unique_ptr<SourceStream> URLRequestHttpJob::SetUpSourceStream() { |
[email protected] | 5394e42 | 2011-01-20 22:07:43 | [diff] [blame] | 1091 | DCHECK(transaction_.get()); |
| 1092 | if (!response_info_) |
Bacek | 8f37155 | 2016-05-12 01:13:49 | [diff] [blame] | 1093 | return nullptr; |
[email protected] | 5394e42 | 2011-01-20 22:07:43 | [diff] [blame] | 1094 | |
xunjieli | 28a1877 | 2016-10-25 15:30:06 | [diff] [blame] | 1095 | std::unique_ptr<SourceStream> upstream = URLRequestJob::SetUpSourceStream(); |
[email protected] | ea8141e | 2011-10-05 13:12:51 | [diff] [blame] | 1096 | HttpResponseHeaders* headers = GetResponseHeaders(); |
xunjieli | 28a1877 | 2016-10-25 15:30:06 | [diff] [blame] | 1097 | std::string type; |
| 1098 | std::vector<SourceStream::SourceType> types; |
olli.raula | ee489a5 | 2016-01-25 08:37:10 | [diff] [blame] | 1099 | size_t iter = 0; |
xunjieli | 28a1877 | 2016-10-25 15:30:06 | [diff] [blame] | 1100 | while (headers->EnumerateHeader(&iter, "Content-Encoding", &type)) { |
eustas | c7d27da | 2017-04-06 10:33:20 | [diff] [blame] | 1101 | SourceStream::SourceType source_type = |
| 1102 | FilterSourceStream::ParseEncodingType(type); |
eustas | c7d27da | 2017-04-06 10:33:20 | [diff] [blame] | 1103 | switch (source_type) { |
| 1104 | case SourceStream::TYPE_BROTLI: |
| 1105 | case SourceStream::TYPE_DEFLATE: |
| 1106 | case SourceStream::TYPE_GZIP: |
eustas | c7d27da | 2017-04-06 10:33:20 | [diff] [blame] | 1107 | types.push_back(source_type); |
| 1108 | break; |
| 1109 | case SourceStream::TYPE_NONE: |
| 1110 | // Identity encoding type. Pass through raw response body. |
| 1111 | return upstream; |
Helen Li | 666c068 | 2017-09-15 19:57:05 | [diff] [blame] | 1112 | case SourceStream::TYPE_UNKNOWN: |
eustas | c7d27da | 2017-04-06 10:33:20 | [diff] [blame] | 1113 | // Unknown encoding type. Pass through raw response body. |
Paul Jensen | e85e815 | 2019-07-01 15:38:00 | [diff] [blame] | 1114 | // Request will not be canceled; though |
eustas | c7d27da | 2017-04-06 10:33:20 | [diff] [blame] | 1115 | // it is expected that user will see malformed / garbage response. |
eustas | c7d27da | 2017-04-06 10:33:20 | [diff] [blame] | 1116 | return upstream; |
Helen Li | 666c068 | 2017-09-15 19:57:05 | [diff] [blame] | 1117 | case SourceStream::TYPE_GZIP_FALLBACK_DEPRECATED: |
| 1118 | case SourceStream::TYPE_SDCH_DEPRECATED: |
| 1119 | case SourceStream::TYPE_SDCH_POSSIBLE_DEPRECATED: |
| 1120 | case SourceStream::TYPE_REJECTED: |
| 1121 | case SourceStream::TYPE_INVALID: |
| 1122 | case SourceStream::TYPE_MAX: |
| 1123 | NOTREACHED(); |
| 1124 | return nullptr; |
eustas | c7d27da | 2017-04-06 10:33:20 | [diff] [blame] | 1125 | } |
[email protected] | 5394e42 | 2011-01-20 22:07:43 | [diff] [blame] | 1126 | } |
| 1127 | |
jdoerrie | 22a91d8b9 | 2018-10-05 08:43:26 | [diff] [blame] | 1128 | for (auto r_iter = types.rbegin(); r_iter != types.rend(); ++r_iter) { |
xunjieli | 28a1877 | 2016-10-25 15:30:06 | [diff] [blame] | 1129 | std::unique_ptr<FilterSourceStream> downstream; |
| 1130 | SourceStream::SourceType type = *r_iter; |
| 1131 | switch (type) { |
| 1132 | case SourceStream::TYPE_BROTLI: |
| 1133 | downstream = CreateBrotliSourceStream(std::move(upstream)); |
| 1134 | break; |
xunjieli | 28a1877 | 2016-10-25 15:30:06 | [diff] [blame] | 1135 | case SourceStream::TYPE_GZIP: |
| 1136 | case SourceStream::TYPE_DEFLATE: |
xunjieli | 28a1877 | 2016-10-25 15:30:06 | [diff] [blame] | 1137 | downstream = GzipSourceStream::Create(std::move(upstream), type); |
| 1138 | break; |
Helen Li | 666c068 | 2017-09-15 19:57:05 | [diff] [blame] | 1139 | case SourceStream::TYPE_GZIP_FALLBACK_DEPRECATED: |
| 1140 | case SourceStream::TYPE_SDCH_DEPRECATED: |
| 1141 | case SourceStream::TYPE_SDCH_POSSIBLE_DEPRECATED: |
xunjieli | 28a1877 | 2016-10-25 15:30:06 | [diff] [blame] | 1142 | case SourceStream::TYPE_NONE: |
| 1143 | case SourceStream::TYPE_INVALID: |
eustas | c7d27da | 2017-04-06 10:33:20 | [diff] [blame] | 1144 | case SourceStream::TYPE_REJECTED: |
| 1145 | case SourceStream::TYPE_UNKNOWN: |
xunjieli | 28a1877 | 2016-10-25 15:30:06 | [diff] [blame] | 1146 | case SourceStream::TYPE_MAX: |
| 1147 | NOTREACHED(); |
| 1148 | return nullptr; |
| 1149 | } |
| 1150 | if (downstream == nullptr) |
| 1151 | return nullptr; |
| 1152 | upstream = std::move(downstream); |
| 1153 | } |
| 1154 | |
| 1155 | return upstream; |
[email protected] | 5394e42 | 2011-01-20 22:07:43 | [diff] [blame] | 1156 | } |
| 1157 | |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 1158 | bool URLRequestHttpJob::CopyFragmentOnRedirect(const GURL& location) const { |
| 1159 | // Allow modification of reference fragments by default, unless |
Matt Menke | eed1a24 | 2019-10-23 21:26:06 | [diff] [blame^] | 1160 | // |preserve_fragment_on_redirect_url_| is set and equal to the redirect URL. |
| 1161 | return !preserve_fragment_on_redirect_url_.has_value() || |
| 1162 | preserve_fragment_on_redirect_url_ != location; |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 1163 | } |
| 1164 | |
[email protected] | 5394e42 | 2011-01-20 22:07:43 | [diff] [blame] | 1165 | bool URLRequestHttpJob::IsSafeRedirect(const GURL& location) { |
[email protected] | e0f35c9 | 2013-05-08 16:04:34 | [diff] [blame] | 1166 | // HTTP is always safe. |
| 1167 | // TODO(pauljensen): Remove once crbug.com/146591 is fixed. |
| 1168 | if (location.is_valid() && |
| 1169 | (location.scheme() == "http" || location.scheme() == "https")) { |
[email protected] | 5394e42 | 2011-01-20 22:07:43 | [diff] [blame] | 1170 | return true; |
[email protected] | 5394e42 | 2011-01-20 22:07:43 | [diff] [blame] | 1171 | } |
[email protected] | e0f35c9 | 2013-05-08 16:04:34 | [diff] [blame] | 1172 | // Query URLRequestJobFactory as to whether |location| would be safe to |
| 1173 | // redirect to. |
| 1174 | return request_->context()->job_factory() && |
| 1175 | request_->context()->job_factory()->IsSafeRedirectTarget(location); |
[email protected] | 5394e42 | 2011-01-20 22:07:43 | [diff] [blame] | 1176 | } |
| 1177 | |
| 1178 | bool URLRequestHttpJob::NeedsAuth() { |
| 1179 | int code = GetResponseCode(); |
| 1180 | if (code == -1) |
| 1181 | return false; |
| 1182 | |
rdsmith | 81f60756 | 2014-11-21 18:35:16 | [diff] [blame] | 1183 | // Check if we need either Proxy or WWW Authentication. This could happen |
[email protected] | 5394e42 | 2011-01-20 22:07:43 | [diff] [blame] | 1184 | // because we either provided no auth info, or provided incorrect info. |
| 1185 | switch (code) { |
| 1186 | case 407: |
| 1187 | if (proxy_auth_state_ == AUTH_STATE_CANCELED) |
| 1188 | return false; |
| 1189 | proxy_auth_state_ = AUTH_STATE_NEED_AUTH; |
| 1190 | return true; |
| 1191 | case 401: |
| 1192 | if (server_auth_state_ == AUTH_STATE_CANCELED) |
| 1193 | return false; |
| 1194 | server_auth_state_ = AUTH_STATE_NEED_AUTH; |
| 1195 | return true; |
| 1196 | } |
| 1197 | return false; |
| 1198 | } |
| 1199 | |
Emily Stark | f2c9bbd | 2019-04-09 17:08:58 | [diff] [blame] | 1200 | std::unique_ptr<AuthChallengeInfo> URLRequestHttpJob::GetAuthChallengeInfo() { |
[email protected] | 5394e42 | 2011-01-20 22:07:43 | [diff] [blame] | 1201 | DCHECK(transaction_.get()); |
| 1202 | DCHECK(response_info_); |
| 1203 | |
| 1204 | // sanity checks: |
| 1205 | DCHECK(proxy_auth_state_ == AUTH_STATE_NEED_AUTH || |
| 1206 | server_auth_state_ == AUTH_STATE_NEED_AUTH); |
[email protected] | 9094b60 | 2012-02-27 21:44:58 | [diff] [blame] | 1207 | DCHECK((GetResponseHeaders()->response_code() == HTTP_UNAUTHORIZED) || |
| 1208 | (GetResponseHeaders()->response_code() == |
| 1209 | HTTP_PROXY_AUTHENTICATION_REQUIRED)); |
[email protected] | 5394e42 | 2011-01-20 22:07:43 | [diff] [blame] | 1210 | |
Emily Stark | f2c9bbd | 2019-04-09 17:08:58 | [diff] [blame] | 1211 | if (!response_info_->auth_challenge.has_value()) |
| 1212 | return nullptr; |
| 1213 | return std::make_unique<AuthChallengeInfo>( |
| 1214 | response_info_->auth_challenge.value()); |
[email protected] | 5394e42 | 2011-01-20 22:07:43 | [diff] [blame] | 1215 | } |
| 1216 | |
[email protected] | f3cf980 | 2011-10-28 18:44:58 | [diff] [blame] | 1217 | void URLRequestHttpJob::SetAuth(const AuthCredentials& credentials) { |
[email protected] | 5394e42 | 2011-01-20 22:07:43 | [diff] [blame] | 1218 | DCHECK(transaction_.get()); |
| 1219 | |
| 1220 | // Proxy gets set first, then WWW. |
| 1221 | if (proxy_auth_state_ == AUTH_STATE_NEED_AUTH) { |
| 1222 | proxy_auth_state_ = AUTH_STATE_HAVE_AUTH; |
| 1223 | } else { |
[email protected] | dd29bcd7 | 2011-03-24 00:03:44 | [diff] [blame] | 1224 | DCHECK_EQ(server_auth_state_, AUTH_STATE_NEED_AUTH); |
[email protected] | 5394e42 | 2011-01-20 22:07:43 | [diff] [blame] | 1225 | server_auth_state_ = AUTH_STATE_HAVE_AUTH; |
| 1226 | } |
| 1227 | |
[email protected] | f3cf980 | 2011-10-28 18:44:58 | [diff] [blame] | 1228 | RestartTransactionWithAuth(credentials); |
[email protected] | 5394e42 | 2011-01-20 22:07:43 | [diff] [blame] | 1229 | } |
| 1230 | |
| 1231 | void URLRequestHttpJob::CancelAuth() { |
[email protected] | 5394e42 | 2011-01-20 22:07:43 | [diff] [blame] | 1232 | if (proxy_auth_state_ == AUTH_STATE_NEED_AUTH) { |
| 1233 | proxy_auth_state_ = AUTH_STATE_CANCELED; |
| 1234 | } else { |
[email protected] | dd29bcd7 | 2011-03-24 00:03:44 | [diff] [blame] | 1235 | DCHECK_EQ(server_auth_state_, AUTH_STATE_NEED_AUTH); |
[email protected] | 5394e42 | 2011-01-20 22:07:43 | [diff] [blame] | 1236 | server_auth_state_ = AUTH_STATE_CANCELED; |
| 1237 | } |
| 1238 | |
Matt Menke | 0743e34 | 2019-06-11 20:48:10 | [diff] [blame] | 1239 | // The above lines should ensure this is the case. |
| 1240 | DCHECK(!NeedsAuth()); |
[email protected] | 5394e42 | 2011-01-20 22:07:43 | [diff] [blame] | 1241 | |
Matt Menke | 0743e34 | 2019-06-11 20:48:10 | [diff] [blame] | 1242 | // Let the consumer read the HTTP error page. NeedsAuth() should now return |
| 1243 | // false, so NotifyHeadersComplete() should not request auth from the client |
| 1244 | // again. |
[email protected] | 5394e42 | 2011-01-20 22:07:43 | [diff] [blame] | 1245 | // |
Matt Menke | 0743e34 | 2019-06-11 20:48:10 | [diff] [blame] | 1246 | // Have to do this via PostTask to avoid re-entrantly calling into the |
| 1247 | // consumer. |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 1248 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
Matt Menke | 0743e34 | 2019-06-11 20:48:10 | [diff] [blame] | 1249 | FROM_HERE, base::BindOnce(&URLRequestHttpJob::NotifyFinalHeadersReceived, |
| 1250 | weak_factory_.GetWeakPtr())); |
[email protected] | 5394e42 | 2011-01-20 22:07:43 | [diff] [blame] | 1251 | } |
| 1252 | |
| 1253 | void URLRequestHttpJob::ContinueWithCertificate( |
mattm | 436ccfe | 2017-06-19 20:24:08 | [diff] [blame] | 1254 | scoped_refptr<X509Certificate> client_cert, |
| 1255 | scoped_refptr<SSLPrivateKey> client_private_key) { |
David Benjamin | 0bda12f | 2018-02-07 19:58:45 | [diff] [blame] | 1256 | DCHECK(transaction_); |
[email protected] | 5394e42 | 2011-01-20 22:07:43 | [diff] [blame] | 1257 | |
| 1258 | DCHECK(!response_info_) << "should not have a response yet"; |
David Benjamin | 0bda12f | 2018-02-07 19:58:45 | [diff] [blame] | 1259 | DCHECK(!override_response_headers_); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 1260 | receive_headers_end_ = base::TimeTicks(); |
[email protected] | 5394e42 | 2011-01-20 22:07:43 | [diff] [blame] | 1261 | |
[email protected] | ec23f52 | 2011-02-22 21:01:38 | [diff] [blame] | 1262 | ResetTimer(); |
| 1263 | |
mmenke | d3a02b3 | 2016-11-16 20:21:32 | [diff] [blame] | 1264 | int rv = transaction_->RestartWithCertificate( |
mattm | 436ccfe | 2017-06-19 20:24:08 | [diff] [blame] | 1265 | std::move(client_cert), std::move(client_private_key), |
Yannic Bonenberger | cc716d4 | 2019-09-04 17:05:36 | [diff] [blame] | 1266 | base::BindOnce(&URLRequestHttpJob::OnStartCompleted, |
| 1267 | base::Unretained(this))); |
[email protected] | 5394e42 | 2011-01-20 22:07:43 | [diff] [blame] | 1268 | if (rv == ERR_IO_PENDING) |
| 1269 | return; |
| 1270 | |
| 1271 | // The transaction started synchronously, but we need to notify the |
| 1272 | // URLRequest delegate via the message loop. |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 1273 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
kylechar | f4fe517 | 2019-02-15 18:53:49 | [diff] [blame] | 1274 | FROM_HERE, base::BindOnce(&URLRequestHttpJob::OnStartCompleted, |
| 1275 | weak_factory_.GetWeakPtr(), rv)); |
[email protected] | 5394e42 | 2011-01-20 22:07:43 | [diff] [blame] | 1276 | } |
| 1277 | |
| 1278 | void URLRequestHttpJob::ContinueDespiteLastError() { |
| 1279 | // If the transaction was destroyed, then the job was cancelled. |
| 1280 | if (!transaction_.get()) |
| 1281 | return; |
| 1282 | |
| 1283 | DCHECK(!response_info_) << "should not have a response yet"; |
David Benjamin | 0bda12f | 2018-02-07 19:58:45 | [diff] [blame] | 1284 | DCHECK(!override_response_headers_); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 1285 | receive_headers_end_ = base::TimeTicks(); |
[email protected] | 5394e42 | 2011-01-20 22:07:43 | [diff] [blame] | 1286 | |
[email protected] | ec23f52 | 2011-02-22 21:01:38 | [diff] [blame] | 1287 | ResetTimer(); |
| 1288 | |
Yannic Bonenberger | cc716d4 | 2019-09-04 17:05:36 | [diff] [blame] | 1289 | int rv = transaction_->RestartIgnoringLastError(base::BindOnce( |
| 1290 | &URLRequestHttpJob::OnStartCompleted, base::Unretained(this))); |
[email protected] | 5394e42 | 2011-01-20 22:07:43 | [diff] [blame] | 1291 | if (rv == ERR_IO_PENDING) |
| 1292 | return; |
| 1293 | |
| 1294 | // The transaction started synchronously, but we need to notify the |
| 1295 | // URLRequest delegate via the message loop. |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 1296 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
kylechar | f4fe517 | 2019-02-15 18:53:49 | [diff] [blame] | 1297 | FROM_HERE, base::BindOnce(&URLRequestHttpJob::OnStartCompleted, |
| 1298 | weak_factory_.GetWeakPtr(), rv)); |
[email protected] | 5394e42 | 2011-01-20 22:07:43 | [diff] [blame] | 1299 | } |
| 1300 | |
[email protected] | f001bd6a | 2011-12-08 04:31:37 | [diff] [blame] | 1301 | bool URLRequestHttpJob::ShouldFixMismatchedContentLength(int rv) const { |
| 1302 | // Some servers send the body compressed, but specify the content length as |
rdsmith | 81f60756 | 2014-11-21 18:35:16 | [diff] [blame] | 1303 | // the uncompressed size. Although this violates the HTTP spec we want to |
[email protected] | f001bd6a | 2011-12-08 04:31:37 | [diff] [blame] | 1304 | // support it (as IE and FireFox do), but *only* for an exact match. |
| 1305 | // See http://crbug.com/79694. |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 1306 | if (rv == ERR_CONTENT_LENGTH_MISMATCH || |
| 1307 | rv == ERR_INCOMPLETE_CHUNKED_ENCODING) { |
mmenke | d3a02b3 | 2016-11-16 20:21:32 | [diff] [blame] | 1308 | if (request_->response_headers()) { |
Avi Drissman | 13fc893 | 2015-12-20 04:40:46 | [diff] [blame] | 1309 | int64_t expected_length = |
| 1310 | request_->response_headers()->GetContentLength(); |
pkasting | f527948 | 2016-07-27 02:18:20 | [diff] [blame] | 1311 | VLOG(1) << __func__ << "() \"" << request_->url().spec() << "\"" |
[email protected] | f001bd6a | 2011-12-08 04:31:37 | [diff] [blame] | 1312 | << " content-length = " << expected_length |
| 1313 | << " pre total = " << prefilter_bytes_read() |
| 1314 | << " post total = " << postfilter_bytes_read(); |
| 1315 | if (postfilter_bytes_read() == expected_length) { |
| 1316 | // Clear the error. |
| 1317 | return true; |
| 1318 | } |
| 1319 | } |
| 1320 | } |
| 1321 | return false; |
| 1322 | } |
| 1323 | |
xunjieli | 26ede96 | 2015-11-23 19:39:13 | [diff] [blame] | 1324 | int URLRequestHttpJob::ReadRawData(IOBuffer* buf, int buf_size) { |
[email protected] | 5394e42 | 2011-01-20 22:07:43 | [diff] [blame] | 1325 | DCHECK_NE(buf_size, 0); |
[email protected] | 5394e42 | 2011-01-20 22:07:43 | [diff] [blame] | 1326 | DCHECK(!read_in_progress_); |
| 1327 | |
Yannic Bonenberger | cc716d4 | 2019-09-04 17:05:36 | [diff] [blame] | 1328 | int rv = |
| 1329 | transaction_->Read(buf, buf_size, |
| 1330 | base::BindOnce(&URLRequestHttpJob::OnReadCompleted, |
| 1331 | base::Unretained(this))); |
[email protected] | 85c1dce | 2011-07-06 12:01:29 | [diff] [blame] | 1332 | |
[email protected] | f001bd6a | 2011-12-08 04:31:37 | [diff] [blame] | 1333 | if (ShouldFixMismatchedContentLength(rv)) |
xunjieli | 26ede96 | 2015-11-23 19:39:13 | [diff] [blame] | 1334 | rv = OK; |
[email protected] | f001bd6a | 2011-12-08 04:31:37 | [diff] [blame] | 1335 | |
xunjieli | 26ede96 | 2015-11-23 19:39:13 | [diff] [blame] | 1336 | if (rv == 0 || (rv < 0 && rv != ERR_IO_PENDING)) |
| 1337 | DoneWithRequest(FINISHED); |
[email protected] | 5394e42 | 2011-01-20 22:07:43 | [diff] [blame] | 1338 | |
xunjieli | 26ede96 | 2015-11-23 19:39:13 | [diff] [blame] | 1339 | if (rv == ERR_IO_PENDING) |
[email protected] | 5394e42 | 2011-01-20 22:07:43 | [diff] [blame] | 1340 | read_in_progress_ = true; |
[email protected] | 5394e42 | 2011-01-20 22:07:43 | [diff] [blame] | 1341 | |
xunjieli | 26ede96 | 2015-11-23 19:39:13 | [diff] [blame] | 1342 | return rv; |
[email protected] | 5394e42 | 2011-01-20 22:07:43 | [diff] [blame] | 1343 | } |
| 1344 | |
Avi Drissman | 13fc893 | 2015-12-20 04:40:46 | [diff] [blame] | 1345 | int64_t URLRequestHttpJob::GetTotalReceivedBytes() const { |
sclittle | ce72c48 | 2015-08-24 20:20:59 | [diff] [blame] | 1346 | int64_t total_received_bytes = |
| 1347 | total_received_bytes_from_previous_transactions_; |
| 1348 | if (transaction_) |
| 1349 | total_received_bytes += transaction_->GetTotalReceivedBytes(); |
| 1350 | return total_received_bytes; |
[email protected] | 71e5ff8f | 2014-01-13 09:44:04 | [diff] [blame] | 1351 | } |
| 1352 | |
sclittle | fb24989 | 2015-09-10 21:33:22 | [diff] [blame] | 1353 | int64_t URLRequestHttpJob::GetTotalSentBytes() const { |
| 1354 | int64_t total_sent_bytes = total_sent_bytes_from_previous_transactions_; |
| 1355 | if (transaction_) |
| 1356 | total_sent_bytes += transaction_->GetTotalSentBytes(); |
| 1357 | return total_sent_bytes; |
| 1358 | } |
| 1359 | |
[email protected] | 5c04f72 | 2011-08-12 17:52:47 | [diff] [blame] | 1360 | void URLRequestHttpJob::DoneReading() { |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 1361 | if (transaction_) { |
[email protected] | 5c04f72 | 2011-08-12 17:52:47 | [diff] [blame] | 1362 | transaction_->DoneReading(); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 1363 | } |
| 1364 | DoneWithRequest(FINISHED); |
| 1365 | } |
| 1366 | |
| 1367 | void URLRequestHttpJob::DoneReadingRedirectResponse() { |
| 1368 | if (transaction_) { |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 1369 | if (transaction_->GetResponseInfo()->headers->IsRedirect(nullptr)) { |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 1370 | // If the original headers indicate a redirect, go ahead and cache the |
| 1371 | // response, even if the |override_response_headers_| are a redirect to |
| 1372 | // another location. |
| 1373 | transaction_->DoneReading(); |
| 1374 | } else { |
| 1375 | // Otherwise, |override_response_headers_| must be non-NULL and contain |
| 1376 | // bogus headers indicating a redirect. |
dcheng | c2e01e8 | 2014-08-27 00:24:42 | [diff] [blame] | 1377 | DCHECK(override_response_headers_.get()); |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 1378 | DCHECK(override_response_headers_->IsRedirect(nullptr)); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 1379 | transaction_->StopCaching(); |
| 1380 | } |
| 1381 | } |
[email protected] | 5c04f72 | 2011-08-12 17:52:47 | [diff] [blame] | 1382 | DoneWithRequest(FINISHED); |
| 1383 | } |
| 1384 | |
Tsuyoshi Horo | 01faed6 | 2019-02-20 22:11:37 | [diff] [blame] | 1385 | IPEndPoint URLRequestHttpJob::GetResponseRemoteEndpoint() const { |
| 1386 | return response_info_ ? response_info_->remote_endpoint : IPEndPoint(); |
[email protected] | 6d81b48 | 2011-02-22 19:47:19 | [diff] [blame] | 1387 | } |
| 1388 | |
[email protected] | ec23f52 | 2011-02-22 21:01:38 | [diff] [blame] | 1389 | void URLRequestHttpJob::RecordTimer() { |
| 1390 | if (request_creation_time_.is_null()) { |
| 1391 | NOTREACHED() |
| 1392 | << "The same transaction shouldn't start twice without new timing."; |
| 1393 | return; |
| 1394 | } |
| 1395 | |
[email protected] | 320a29f1 | 2011-03-21 14:47:41 | [diff] [blame] | 1396 | base::TimeDelta to_start = base::Time::Now() - request_creation_time_; |
[email protected] | ec23f52 | 2011-02-22 21:01:38 | [diff] [blame] | 1397 | request_creation_time_ = base::Time(); |
[email protected] | 8684a881 | 2011-03-22 13:59:38 | [diff] [blame] | 1398 | |
[email protected] | 5c68d69 | 2011-08-24 04:59:41 | [diff] [blame] | 1399 | UMA_HISTOGRAM_MEDIUM_TIMES("Net.HttpTimeToFirstByte", to_start); |
[email protected] | ec23f52 | 2011-02-22 21:01:38 | [diff] [blame] | 1400 | } |
| 1401 | |
| 1402 | void URLRequestHttpJob::ResetTimer() { |
| 1403 | if (!request_creation_time_.is_null()) { |
| 1404 | NOTREACHED() |
| 1405 | << "The timer was reset before it was recorded."; |
| 1406 | return; |
| 1407 | } |
| 1408 | request_creation_time_ = base::Time::Now(); |
| 1409 | } |
| 1410 | |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 1411 | void URLRequestHttpJob::SetRequestHeadersCallback( |
| 1412 | RequestHeadersCallback callback) { |
| 1413 | DCHECK(!transaction_); |
| 1414 | DCHECK(!request_headers_callback_); |
| 1415 | request_headers_callback_ = std::move(callback); |
| 1416 | } |
| 1417 | |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 1418 | void URLRequestHttpJob::SetResponseHeadersCallback( |
| 1419 | ResponseHeadersCallback callback) { |
| 1420 | DCHECK(!transaction_); |
| 1421 | DCHECK(!response_headers_callback_); |
| 1422 | response_headers_callback_ = std::move(callback); |
| 1423 | } |
| 1424 | |
[email protected] | bbaea8f | 2011-06-24 00:11:01 | [diff] [blame] | 1425 | void URLRequestHttpJob::RecordPerfHistograms(CompletionCause reason) { |
| 1426 | if (start_time_.is_null()) |
| 1427 | return; |
| 1428 | |
| 1429 | base::TimeDelta total_time = base::TimeTicks::Now() - start_time_; |
| 1430 | UMA_HISTOGRAM_TIMES("Net.HttpJob.TotalTime", total_time); |
| 1431 | |
| 1432 | if (reason == FINISHED) { |
Devdeep Ray | 0c6966d | 2017-07-14 00:58:22 | [diff] [blame] | 1433 | UmaHistogramTimes( |
| 1434 | base::StringPrintf("Net.HttpJob.TotalTimeSuccess.Priority%d", |
| 1435 | request()->priority()), |
| 1436 | total_time); |
[email protected] | bbaea8f | 2011-06-24 00:11:01 | [diff] [blame] | 1437 | UMA_HISTOGRAM_TIMES("Net.HttpJob.TotalTimeSuccess", total_time); |
| 1438 | } else { |
| 1439 | UMA_HISTOGRAM_TIMES("Net.HttpJob.TotalTimeCancel", total_time); |
| 1440 | } |
| 1441 | |
[email protected] | 4b4d2024 | 2012-02-23 18:27:46 | [diff] [blame] | 1442 | if (response_info_) { |
rtenneti | e55c4b7 | 2015-08-03 21:48:35 | [diff] [blame] | 1443 | // QUIC (by default) supports https scheme only, thus track https URLs only |
| 1444 | // for QUIC. |
| 1445 | bool is_https_google = request() && request()->url().SchemeIs("https") && |
| 1446 | HasGoogleHost(request()->url()); |
rtenneti | 72928bb | 2015-04-28 18:28:13 | [diff] [blame] | 1447 | bool used_quic = response_info_->DidUseQuic(); |
rtenneti | e55c4b7 | 2015-08-03 21:48:35 | [diff] [blame] | 1448 | if (is_https_google) { |
rtenneti | 72928bb | 2015-04-28 18:28:13 | [diff] [blame] | 1449 | if (used_quic) { |
rtenneti | e55c4b7 | 2015-08-03 21:48:35 | [diff] [blame] | 1450 | UMA_HISTOGRAM_MEDIUM_TIMES("Net.HttpJob.TotalTime.Secure.Quic", |
| 1451 | total_time); |
rtenneti | 72928bb | 2015-04-28 18:28:13 | [diff] [blame] | 1452 | } |
| 1453 | } |
jkarlin | ded0021b | 2016-12-05 16:01:06 | [diff] [blame] | 1454 | |
| 1455 | UMA_HISTOGRAM_CUSTOM_COUNTS("Net.HttpJob.PrefilterBytesRead", |
| 1456 | prefilter_bytes_read(), 1, 50000000, 50); |
[email protected] | 4b4d2024 | 2012-02-23 18:27:46 | [diff] [blame] | 1457 | if (response_info_->was_cached) { |
| 1458 | UMA_HISTOGRAM_TIMES("Net.HttpJob.TotalTimeCached", total_time); |
jkarlin | ded0021b | 2016-12-05 16:01:06 | [diff] [blame] | 1459 | UMA_HISTOGRAM_CUSTOM_COUNTS("Net.HttpJob.PrefilterBytesRead.Cache", |
| 1460 | prefilter_bytes_read(), 1, 50000000, 50); |
| 1461 | |
jkarlin | cc6943d | 2016-04-25 19:25:35 | [diff] [blame] | 1462 | if (response_info_->unused_since_prefetch) |
Steven Holte | 6143e64 | 2017-06-28 19:47:38 | [diff] [blame] | 1463 | UMA_HISTOGRAM_COUNTS_1M("Net.Prefetch.HitBytes", |
| 1464 | prefilter_bytes_read()); |
jkarlin | cc6943d | 2016-04-25 19:25:35 | [diff] [blame] | 1465 | } else { |
[email protected] | 4b4d2024 | 2012-02-23 18:27:46 | [diff] [blame] | 1466 | UMA_HISTOGRAM_TIMES("Net.HttpJob.TotalTimeNotCached", total_time); |
jkarlin | ded0021b | 2016-12-05 16:01:06 | [diff] [blame] | 1467 | UMA_HISTOGRAM_CUSTOM_COUNTS("Net.HttpJob.PrefilterBytesRead.Net", |
| 1468 | prefilter_bytes_read(), 1, 50000000, 50); |
| 1469 | |
| 1470 | if (request_info_.load_flags & LOAD_PREFETCH) { |
Steven Holte | 6143e64 | 2017-06-28 19:47:38 | [diff] [blame] | 1471 | UMA_HISTOGRAM_COUNTS_1M("Net.Prefetch.PrefilterBytesReadFromNetwork", |
| 1472 | prefilter_bytes_read()); |
jkarlin | ded0021b | 2016-12-05 16:01:06 | [diff] [blame] | 1473 | } |
rtenneti | e55c4b7 | 2015-08-03 21:48:35 | [diff] [blame] | 1474 | if (is_https_google) { |
rtenneti | 72928bb | 2015-04-28 18:28:13 | [diff] [blame] | 1475 | if (used_quic) { |
rtenneti | e55c4b7 | 2015-08-03 21:48:35 | [diff] [blame] | 1476 | UMA_HISTOGRAM_MEDIUM_TIMES( |
| 1477 | "Net.HttpJob.TotalTimeNotCached.Secure.Quic", total_time); |
rtenneti | 72928bb | 2015-04-28 18:28:13 | [diff] [blame] | 1478 | } else { |
rtenneti | e55c4b7 | 2015-08-03 21:48:35 | [diff] [blame] | 1479 | UMA_HISTOGRAM_MEDIUM_TIMES( |
| 1480 | "Net.HttpJob.TotalTimeNotCached.Secure.NotQuic", total_time); |
rtenneti | 72928bb | 2015-04-28 18:28:13 | [diff] [blame] | 1481 | } |
| 1482 | } |
[email protected] | b73656ca | 2011-07-22 17:42:17 | [diff] [blame] | 1483 | } |
[email protected] | bbaea8f | 2011-06-24 00:11:01 | [diff] [blame] | 1484 | } |
| 1485 | |
| 1486 | start_time_ = base::TimeTicks(); |
| 1487 | } |
| 1488 | |
| 1489 | void URLRequestHttpJob::DoneWithRequest(CompletionCause reason) { |
| 1490 | if (done_) |
| 1491 | return; |
| 1492 | done_ = true; |
tbansal | 79ed5cd | 2015-08-10 18:53:56 | [diff] [blame] | 1493 | |
| 1494 | // Notify NetworkQualityEstimator. |
mmenke | 7ce675a | 2016-11-07 17:08:30 | [diff] [blame] | 1495 | NetworkQualityEstimator* network_quality_estimator = |
| 1496 | request()->context()->network_quality_estimator(); |
| 1497 | if (network_quality_estimator) { |
| 1498 | network_quality_estimator->NotifyRequestCompleted( |
| 1499 | *request(), request_->status().error()); |
tbansal | 79ed5cd | 2015-08-10 18:53:56 | [diff] [blame] | 1500 | } |
| 1501 | |
[email protected] | bbaea8f | 2011-06-24 00:11:01 | [diff] [blame] | 1502 | RecordPerfHistograms(reason); |
mmenke | 7ce675a | 2016-11-07 17:08:30 | [diff] [blame] | 1503 | request()->set_received_response_content_length(prefilter_bytes_read()); |
[email protected] | bbaea8f | 2011-06-24 00:11:01 | [diff] [blame] | 1504 | } |
| 1505 | |
[email protected] | ea8141e | 2011-10-05 13:12:51 | [diff] [blame] | 1506 | HttpResponseHeaders* URLRequestHttpJob::GetResponseHeaders() const { |
| 1507 | DCHECK(transaction_.get()); |
| 1508 | DCHECK(transaction_->GetResponseInfo()); |
| 1509 | return override_response_headers_.get() ? |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 1510 | override_response_headers_.get() : |
| 1511 | transaction_->GetResponseInfo()->headers.get(); |
[email protected] | ea8141e | 2011-10-05 13:12:51 | [diff] [blame] | 1512 | } |
| 1513 | |
| 1514 | void URLRequestHttpJob::NotifyURLRequestDestroyed() { |
| 1515 | awaiting_callback_ = false; |
tbansal | 80a5216 | 2016-05-20 17:55:04 | [diff] [blame] | 1516 | |
| 1517 | // Notify NetworkQualityEstimator. |
mmenke | 7ce675a | 2016-11-07 17:08:30 | [diff] [blame] | 1518 | NetworkQualityEstimator* network_quality_estimator = |
| 1519 | request()->context()->network_quality_estimator(); |
| 1520 | if (network_quality_estimator) |
| 1521 | network_quality_estimator->NotifyURLRequestDestroyed(*request()); |
[email protected] | ea8141e | 2011-10-05 13:12:51 | [diff] [blame] | 1522 | } |
| 1523 | |
[email protected] | 4f5656c6 | 2010-12-13 10:47:09 | [diff] [blame] | 1524 | } // namespace net |