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