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