[email protected] | 26b997396 | 2012-01-28 00:57:00 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 9b9ae955 | 2010-07-01 22:20:50 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
[email protected] | 677c9057 | 2008-12-10 09:03:15 | [diff] [blame] | 4 | |
Lily Houghton | 582d462 | 2018-01-22 22:43:40 | [diff] [blame] | 5 | #include "net/proxy_resolution/pac_file_fetcher_impl.h" |
[email protected] | 677c9057 | 2008-12-10 09:03:15 | [diff] [blame] | 6 | |
Sebastien Marchand | 6d0558fd | 2019-01-25 16:49:37 | [diff] [blame] | 7 | #include "base/bind.h" |
[email protected] | 677c9057 | 2008-12-10 09:03:15 | [diff] [blame] | 8 | #include "base/compiler_specific.h" |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 9 | #include "base/location.h" |
[email protected] | 13a279e | 2009-04-13 17:32:37 | [diff] [blame] | 10 | #include "base/logging.h" |
asvitkine | 3033081 | 2016-08-30 04:01:08 | [diff] [blame] | 11 | #include "base/metrics/histogram_macros.h" |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 12 | #include "base/single_thread_task_runner.h" |
Avi Drissman | 4365a478 | 2018-12-28 19:26:24 | [diff] [blame] | 13 | #include "base/stl_util.h" |
[email protected] | fc9be580 | 2013-06-11 10:56:51 | [diff] [blame] | 14 | #include "base/strings/string_util.h" |
gab | f767595f | 2016-05-11 18:50:35 | [diff] [blame] | 15 | #include "base/threading/thread_task_runner_handle.h" |
[email protected] | d9d71e08 | 2011-02-16 11:44:28 | [diff] [blame] | 16 | #include "net/base/data_url.h" |
[email protected] | 9dea9e1f | 2009-01-29 00:30:47 | [diff] [blame] | 17 | #include "net/base/io_buffer.h" |
[email protected] | 677c9057 | 2008-12-10 09:03:15 | [diff] [blame] | 18 | #include "net/base/load_flags.h" |
[email protected] | 597cf6e | 2009-05-29 09:43:26 | [diff] [blame] | 19 | #include "net/base/net_errors.h" |
[email protected] | de36240 | 2014-05-10 18:32:47 | [diff] [blame] | 20 | #include "net/base/net_string_util.h" |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 21 | #include "net/base/request_priority.h" |
[email protected] | 6e7845ae | 2013-03-29 21:48:11 | [diff] [blame] | 22 | #include "net/cert/cert_status_flags.h" |
[email protected] | e0ef2c2 | 2009-06-03 23:54:44 | [diff] [blame] | 23 | #include "net/http/http_response_headers.h" |
Eric Roman | ab9fc65 | 2018-05-08 01:59:53 | [diff] [blame] | 24 | #include "net/url_request/redirect_info.h" |
[email protected] | 8693361 | 2010-10-16 23:10:33 | [diff] [blame] | 25 | #include "net/url_request/url_request_context.h" |
[email protected] | 677c9057 | 2008-12-10 09:03:15 | [diff] [blame] | 26 | |
[email protected] | f0a51fb5 | 2009-03-05 12:46:38 | [diff] [blame] | 27 | // TODO(eroman): |
[email protected] | 33abb68 | 2011-03-29 03:58:42 | [diff] [blame] | 28 | // - Support auth-prompts (http://crbug.com/77366) |
[email protected] | 677c9057 | 2008-12-10 09:03:15 | [diff] [blame] | 29 | |
| 30 | namespace net { |
| 31 | |
| 32 | namespace { |
| 33 | |
| 34 | // The maximum size (in bytes) allowed for a PAC script. Responses exceeding |
| 35 | // this will fail with ERR_FILE_TOO_BIG. |
[email protected] | 8693361 | 2010-10-16 23:10:33 | [diff] [blame] | 36 | const int kDefaultMaxResponseBytes = 1048576; // 1 megabyte |
[email protected] | 677c9057 | 2008-12-10 09:03:15 | [diff] [blame] | 37 | |
| 38 | // The maximum duration (in milliseconds) allowed for fetching the PAC script. |
| 39 | // Responses exceeding this will fail with ERR_TIMED_OUT. |
Eric Roman | cdb1d1c | 2018-01-03 21:17:08 | [diff] [blame] | 40 | // |
| 41 | // This timeout applies to both scripts fetched in the course of WPAD, as well |
| 42 | // as explicitly configured ones. |
| 43 | // |
| 44 | // If the default timeout is too high, auto-detect can stall for a long time, |
| 45 | // and if it is too low then slow loading scripts may be skipped. |
| 46 | // |
| 47 | // 30 seconds is a compromise between those competing goals. This value also |
| 48 | // appears to match Microsoft Edge (based on testing). |
| 49 | constexpr base::TimeDelta kDefaultMaxDuration = |
| 50 | base::TimeDelta::FromSeconds(30); |
[email protected] | 677c9057 | 2008-12-10 09:03:15 | [diff] [blame] | 51 | |
[email protected] | 13a279e | 2009-04-13 17:32:37 | [diff] [blame] | 52 | // Returns true if |mime_type| is one of the known PAC mime type. |
| 53 | bool IsPacMimeType(const std::string& mime_type) { |
Lily Houghton | 9844d32 | 2018-01-20 05:44:01 | [diff] [blame] | 54 | static const char* const kSupportedPacMimeTypes[] = { |
| 55 | "application/x-ns-proxy-autoconfig", "application/x-javascript-config", |
[email protected] | 13a279e | 2009-04-13 17:32:37 | [diff] [blame] | 56 | }; |
Avi Drissman | 4365a478 | 2018-12-28 19:26:24 | [diff] [blame] | 57 | for (size_t i = 0; i < base::size(kSupportedPacMimeTypes); ++i) { |
brettw | bc17d2c8 | 2015-06-09 22:39:08 | [diff] [blame] | 58 | if (base::LowerCaseEqualsASCII(mime_type, kSupportedPacMimeTypes[i])) |
[email protected] | 13a279e | 2009-04-13 17:32:37 | [diff] [blame] | 59 | return true; |
| 60 | } |
| 61 | return false; |
| 62 | } |
| 63 | |
Eric Roman | 598760f | 2018-10-12 01:30:41 | [diff] [blame] | 64 | struct BomMapping { |
| 65 | base::StringPiece prefix; |
| 66 | const char* charset; |
| 67 | }; |
| 68 | |
| 69 | const BomMapping kBomMappings[] = { |
| 70 | {"\xFE\xFF", "utf-16be"}, |
| 71 | {"\xFF\xFE", "utf-16le"}, |
| 72 | {"\xEF\xBB\xBF", "utf-8"}, |
| 73 | }; |
| 74 | |
[email protected] | 9b9ae955 | 2010-07-01 22:20:50 | [diff] [blame] | 75 | // Converts |bytes| (which is encoded by |charset|) to UTF16, saving the resul |
| 76 | // to |*utf16|. |
[email protected] | 8f3c9634 | 2009-09-22 03:06:54 | [diff] [blame] | 77 | // If |charset| is empty, then we don't know what it was and guess. |
[email protected] | 9b9ae955 | 2010-07-01 22:20:50 | [diff] [blame] | 78 | void ConvertResponseToUTF16(const std::string& charset, |
| 79 | const std::string& bytes, |
[email protected] | 42cba2fb | 2013-03-29 19:58:57 | [diff] [blame] | 80 | base::string16* utf16) { |
[email protected] | 8f3c9634 | 2009-09-22 03:06:54 | [diff] [blame] | 81 | if (charset.empty()) { |
Eric Roman | 598760f | 2018-10-12 01:30:41 | [diff] [blame] | 82 | // Guess the charset by looking at the BOM. |
| 83 | base::StringPiece bytes_str(bytes); |
| 84 | for (const auto& bom : kBomMappings) { |
| 85 | if (bytes_str.starts_with(bom.prefix)) { |
| 86 | return ConvertResponseToUTF16( |
| 87 | bom.charset, |
| 88 | // Strip the BOM in the converted response. |
| 89 | bytes.substr(bom.prefix.size()), utf16); |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | // Otherwise assume ISO-8859-1 if no charset was specified. |
| 94 | return ConvertResponseToUTF16(kCharsetLatin1, bytes, utf16); |
[email protected] | 8f3c9634 | 2009-09-22 03:06:54 | [diff] [blame] | 95 | } |
| 96 | |
Eric Roman | 598760f | 2018-10-12 01:30:41 | [diff] [blame] | 97 | DCHECK(!charset.empty()); |
| 98 | |
[email protected] | de36240 | 2014-05-10 18:32:47 | [diff] [blame] | 99 | // Be generous in the conversion -- if any characters lie outside of |charset| |
| 100 | // (i.e. invalid), then substitute them with U+FFFD rather than failing. |
Eric Roman | 598760f | 2018-10-12 01:30:41 | [diff] [blame] | 101 | ConvertToUTF16WithSubstitutions(bytes, charset.c_str(), utf16); |
[email protected] | 8f3c9634 | 2009-09-22 03:06:54 | [diff] [blame] | 102 | } |
| 103 | |
[email protected] | 677c9057 | 2008-12-10 09:03:15 | [diff] [blame] | 104 | } // namespace |
| 105 | |
Eric Roman | ab9fc65 | 2018-05-08 01:59:53 | [diff] [blame] | 106 | std::unique_ptr<PacFileFetcherImpl> PacFileFetcherImpl::Create( |
| 107 | URLRequestContext* url_request_context) { |
Matt Menke | 27e91ae4 | 2019-09-10 16:10:48 | [diff] [blame^] | 108 | return base::WrapUnique(new PacFileFetcherImpl(url_request_context)); |
[email protected] | 677c9057 | 2008-12-10 09:03:15 | [diff] [blame] | 109 | } |
| 110 | |
Lily Houghton | 9959786 | 2018-03-07 16:40:42 | [diff] [blame] | 111 | PacFileFetcherImpl::~PacFileFetcherImpl() { |
[email protected] | da968bc | 2011-01-19 11:48:19 | [diff] [blame] | 112 | // The URLRequest's destructor will cancel the outstanding request, and |
[email protected] | 677c9057 | 2008-12-10 09:03:15 | [diff] [blame] | 113 | // ensure that the delegate (this) is not called again. |
| 114 | } |
| 115 | |
Lily Houghton | 9959786 | 2018-03-07 16:40:42 | [diff] [blame] | 116 | base::TimeDelta PacFileFetcherImpl::SetTimeoutConstraint( |
[email protected] | 7aefb15 | 2011-01-21 23:46:49 | [diff] [blame] | 117 | base::TimeDelta timeout) { |
| 118 | base::TimeDelta prev = max_duration_; |
| 119 | max_duration_ = timeout; |
| 120 | return prev; |
| 121 | } |
| 122 | |
Lily Houghton | 9959786 | 2018-03-07 16:40:42 | [diff] [blame] | 123 | size_t PacFileFetcherImpl::SetSizeConstraint(size_t size_bytes) { |
[email protected] | 7aefb15 | 2011-01-21 23:46:49 | [diff] [blame] | 124 | size_t prev = max_response_bytes_; |
| 125 | max_response_bytes_ = size_bytes; |
| 126 | return prev; |
| 127 | } |
| 128 | |
Lily Houghton | 9959786 | 2018-03-07 16:40:42 | [diff] [blame] | 129 | void PacFileFetcherImpl::OnResponseCompleted(URLRequest* request, |
| 130 | int net_error) { |
[email protected] | 7aefb15 | 2011-01-21 23:46:49 | [diff] [blame] | 131 | DCHECK_EQ(request, cur_request_.get()); |
| 132 | |
| 133 | // Use |result_code_| as the request's error if we have already set it to |
| 134 | // something specific. |
maksim.sisov | bf794e2 | 2016-09-13 09:20:34 | [diff] [blame] | 135 | if (result_code_ == OK && net_error != OK) |
| 136 | result_code_ = net_error; |
[email protected] | 7aefb15 | 2011-01-21 23:46:49 | [diff] [blame] | 137 | |
| 138 | FetchCompleted(); |
| 139 | } |
| 140 | |
Ramin Halavati | bb8c4d8 | 2018-03-16 08:04:31 | [diff] [blame] | 141 | int PacFileFetcherImpl::Fetch( |
| 142 | const GURL& url, |
| 143 | base::string16* text, |
Bence Béky | cc5b88a | 2018-05-25 20:24:17 | [diff] [blame] | 144 | CompletionOnceCallback callback, |
Ramin Halavati | bb8c4d8 | 2018-03-16 08:04:31 | [diff] [blame] | 145 | const NetworkTrafficAnnotationTag traffic_annotation) { |
[email protected] | 677c9057 | 2008-12-10 09:03:15 | [diff] [blame] | 146 | // It is invalid to call Fetch() while a request is already in progress. |
| 147 | DCHECK(!cur_request_.get()); |
[email protected] | 23578681 | 2011-12-20 02:15:31 | [diff] [blame] | 148 | DCHECK(!callback.is_null()); |
[email protected] | 9b9ae955 | 2010-07-01 22:20:50 | [diff] [blame] | 149 | DCHECK(text); |
[email protected] | 677c9057 | 2008-12-10 09:03:15 | [diff] [blame] | 150 | |
mmenke | ed8d7e43 | 2017-05-03 16:48:33 | [diff] [blame] | 151 | if (!url_request_context_) |
| 152 | return ERR_CONTEXT_SHUT_DOWN; |
| 153 | |
Eric Roman | ab9fc65 | 2018-05-08 01:59:53 | [diff] [blame] | 154 | if (!IsUrlSchemeAllowed(url)) |
| 155 | return ERR_DISALLOWED_URL_SCHEME; |
| 156 | |
[email protected] | d9d71e08 | 2011-02-16 11:44:28 | [diff] [blame] | 157 | // Handle base-64 encoded data-urls that contain custom PAC scripts. |
| 158 | if (url.SchemeIs("data")) { |
| 159 | std::string mime_type; |
| 160 | std::string charset; |
| 161 | std::string data; |
| 162 | if (!DataURL::Parse(url, &mime_type, &charset, &data)) |
| 163 | return ERR_FAILED; |
| 164 | |
| 165 | ConvertResponseToUTF16(charset, data, text); |
| 166 | return OK; |
| 167 | } |
| 168 | |
cbentzel | 27d2e5e5 | 2015-07-10 17:39:56 | [diff] [blame] | 169 | DCHECK(fetch_start_time_.is_null()); |
cbentzel | e45ccba | 2015-08-21 18:23:14 | [diff] [blame] | 170 | fetch_start_time_ = base::TimeTicks::Now(); |
cbentzel | 27d2e5e5 | 2015-07-10 17:39:56 | [diff] [blame] | 171 | |
mmenke | ed8d7e43 | 2017-05-03 16:48:33 | [diff] [blame] | 172 | // Use highest priority, so if socket pools are being used for other types of |
| 173 | // requests, PAC requests are aren't blocked on them. |
rhalavati | de8bf4e | 2017-05-16 06:09:11 | [diff] [blame] | 174 | cur_request_ = url_request_context_->CreateRequest(url, MAXIMUM_PRIORITY, |
| 175 | this, traffic_annotation); |
Matt Menke | 510ceaa1 | 2018-01-09 17:52:26 | [diff] [blame] | 176 | cur_request_->set_is_pac_request(true); |
[email protected] | 677c9057 | 2008-12-10 09:03:15 | [diff] [blame] | 177 | |
| 178 | // Make sure that the PAC script is downloaded using a direct connection, |
| 179 | // to avoid circular dependencies (fetching is a part of proxy resolution). |
[email protected] | e0ef2c2 | 2009-06-03 23:54:44 | [diff] [blame] | 180 | // Also disable the use of the disk cache. The cache is disabled so that if |
| 181 | // the user switches networks we don't potentially use the cached response |
| 182 | // from old network when we should in fact be re-fetching on the new network. |
[email protected] | 6fbac16 | 2011-06-20 00:29:04 | [diff] [blame] | 183 | // If the PAC script is hosted on an HTTPS server we bypass revocation |
| 184 | // checking in order to avoid a circular dependency when attempting to fetch |
| 185 | // the OCSP response or CRL. We could make the revocation check go direct but |
mmenke | ed8d7e43 | 2017-05-03 16:48:33 | [diff] [blame] | 186 | // the proxy might be the only way to the outside world. IGNORE_LIMITS is |
| 187 | // used to avoid blocking proxy resolution on other network requests. |
[email protected] | bb1c466 | 2013-11-14 00:00:07 | [diff] [blame] | 188 | cur_request_->SetLoadFlags(LOAD_BYPASS_PROXY | LOAD_DISABLE_CACHE | |
Ryan Sleevi | 24fe268 | 2018-08-16 21:33:46 | [diff] [blame] | 189 | LOAD_DISABLE_CERT_NETWORK_FETCHES | |
mmenke | ed8d7e43 | 2017-05-03 16:48:33 | [diff] [blame] | 190 | LOAD_IGNORE_LIMITS); |
[email protected] | 677c9057 | 2008-12-10 09:03:15 | [diff] [blame] | 191 | |
| 192 | // Save the caller's info for notification on completion. |
Bence Béky | cc5b88a | 2018-05-25 20:24:17 | [diff] [blame] | 193 | callback_ = std::move(callback); |
[email protected] | 9b9ae955 | 2010-07-01 22:20:50 | [diff] [blame] | 194 | result_text_ = text; |
| 195 | |
| 196 | bytes_read_so_far_.clear(); |
[email protected] | 677c9057 | 2008-12-10 09:03:15 | [diff] [blame] | 197 | |
| 198 | // Post a task to timeout this request if it takes too long. |
| 199 | cur_request_id_ = ++next_id_; |
eroman | e186724 | 2015-04-23 23:07:44 | [diff] [blame] | 200 | |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 201 | base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
Lily Houghton | 9844d32 | 2018-01-20 05:44:01 | [diff] [blame] | 202 | FROM_HERE, |
kylechar | f4fe517 | 2019-02-15 18:53:49 | [diff] [blame] | 203 | base::BindOnce(&PacFileFetcherImpl::OnTimeout, weak_factory_.GetWeakPtr(), |
| 204 | cur_request_id_), |
[email protected] | 26b997396 | 2012-01-28 00:57:00 | [diff] [blame] | 205 | max_duration_); |
[email protected] | 677c9057 | 2008-12-10 09:03:15 | [diff] [blame] | 206 | |
| 207 | // Start the request. |
| 208 | cur_request_->Start(); |
[email protected] | 620f571 | 2009-08-04 22:43:12 | [diff] [blame] | 209 | return ERR_IO_PENDING; |
[email protected] | 677c9057 | 2008-12-10 09:03:15 | [diff] [blame] | 210 | } |
| 211 | |
Lily Houghton | 9959786 | 2018-03-07 16:40:42 | [diff] [blame] | 212 | void PacFileFetcherImpl::Cancel() { |
[email protected] | da968bc | 2011-01-19 11:48:19 | [diff] [blame] | 213 | // ResetCurRequestState will free the URLRequest, which will cause |
[email protected] | 677c9057 | 2008-12-10 09:03:15 | [diff] [blame] | 214 | // cancellation. |
| 215 | ResetCurRequestState(); |
| 216 | } |
| 217 | |
Lily Houghton | 9959786 | 2018-03-07 16:40:42 | [diff] [blame] | 218 | URLRequestContext* PacFileFetcherImpl::GetRequestContext() const { |
[email protected] | 20d296ddc | 2009-11-18 23:07:08 | [diff] [blame] | 219 | return url_request_context_; |
| 220 | } |
| 221 | |
Lily Houghton | 9959786 | 2018-03-07 16:40:42 | [diff] [blame] | 222 | void PacFileFetcherImpl::OnShutdown() { |
mmenke | ed8d7e43 | 2017-05-03 16:48:33 | [diff] [blame] | 223 | url_request_context_ = nullptr; |
| 224 | |
| 225 | if (cur_request_) { |
| 226 | result_code_ = ERR_CONTEXT_SHUT_DOWN; |
| 227 | FetchCompleted(); |
| 228 | } |
| 229 | } |
| 230 | |
Eric Roman | ab9fc65 | 2018-05-08 01:59:53 | [diff] [blame] | 231 | void PacFileFetcherImpl::OnReceivedRedirect(URLRequest* request, |
| 232 | const RedirectInfo& redirect_info, |
| 233 | bool* defer_redirect) { |
| 234 | int error = OK; |
| 235 | |
| 236 | // Redirection to file:// is never OK. Ordinarily this is handled lower in the |
| 237 | // stack (|FileProtocolHandler::IsSafeRedirectTarget|), but this is reachable |
| 238 | // when built without file:// suppport. Return the same error for consistency. |
| 239 | if (redirect_info.new_url.SchemeIsFile()) { |
| 240 | error = ERR_UNSAFE_REDIRECT; |
| 241 | } else if (!IsUrlSchemeAllowed(redirect_info.new_url)) { |
| 242 | error = ERR_DISALLOWED_URL_SCHEME; |
| 243 | } |
| 244 | |
| 245 | if (error != OK) { |
| 246 | // Fail the redirect. |
| 247 | request->CancelWithError(error); |
| 248 | OnResponseCompleted(request, error); |
| 249 | } |
| 250 | } |
| 251 | |
Lily Houghton | 9959786 | 2018-03-07 16:40:42 | [diff] [blame] | 252 | void PacFileFetcherImpl::OnAuthRequired(URLRequest* request, |
Emily Stark | f2c9bbd | 2019-04-09 17:08:58 | [diff] [blame] | 253 | const AuthChallengeInfo& auth_info) { |
[email protected] | 096724916 | 2010-11-20 00:13:32 | [diff] [blame] | 254 | DCHECK_EQ(request, cur_request_.get()); |
[email protected] | 33abb68 | 2011-03-29 03:58:42 | [diff] [blame] | 255 | // TODO(eroman): http://crbug.com/77366 |
[email protected] | 13a279e | 2009-04-13 17:32:37 | [diff] [blame] | 256 | LOG(WARNING) << "Auth required to fetch PAC script, aborting."; |
[email protected] | 677c9057 | 2008-12-10 09:03:15 | [diff] [blame] | 257 | result_code_ = ERR_NOT_IMPLEMENTED; |
| 258 | request->CancelAuth(); |
| 259 | } |
| 260 | |
Lily Houghton | 9959786 | 2018-03-07 16:40:42 | [diff] [blame] | 261 | void PacFileFetcherImpl::OnSSLCertificateError(URLRequest* request, |
Emily Stark | 79fba584 | 2019-04-25 04:59:36 | [diff] [blame] | 262 | int net_error, |
Lily Houghton | 9959786 | 2018-03-07 16:40:42 | [diff] [blame] | 263 | const SSLInfo& ssl_info, |
| 264 | bool fatal) { |
[email protected] | 096724916 | 2010-11-20 00:13:32 | [diff] [blame] | 265 | DCHECK_EQ(request, cur_request_.get()); |
[email protected] | 13a279e | 2009-04-13 17:32:37 | [diff] [blame] | 266 | LOG(WARNING) << "SSL certificate error when fetching PAC script, aborting."; |
[email protected] | 677c9057 | 2008-12-10 09:03:15 | [diff] [blame] | 267 | // Certificate errors are in same space as net errors. |
Emily Stark | 79fba584 | 2019-04-25 04:59:36 | [diff] [blame] | 268 | result_code_ = net_error; |
[email protected] | 677c9057 | 2008-12-10 09:03:15 | [diff] [blame] | 269 | request->Cancel(); |
| 270 | } |
| 271 | |
Lily Houghton | 9959786 | 2018-03-07 16:40:42 | [diff] [blame] | 272 | void PacFileFetcherImpl::OnResponseStarted(URLRequest* request, int net_error) { |
[email protected] | 096724916 | 2010-11-20 00:13:32 | [diff] [blame] | 273 | DCHECK_EQ(request, cur_request_.get()); |
maksim.sisov | bf794e2 | 2016-09-13 09:20:34 | [diff] [blame] | 274 | DCHECK_NE(ERR_IO_PENDING, net_error); |
[email protected] | 677c9057 | 2008-12-10 09:03:15 | [diff] [blame] | 275 | |
maksim.sisov | bf794e2 | 2016-09-13 09:20:34 | [diff] [blame] | 276 | if (net_error != OK) { |
| 277 | OnResponseCompleted(request, net_error); |
[email protected] | 677c9057 | 2008-12-10 09:03:15 | [diff] [blame] | 278 | return; |
| 279 | } |
| 280 | |
| 281 | // Require HTTP responses to have a success status code. |
[email protected] | 91f568903 | 2013-08-22 01:43:33 | [diff] [blame] | 282 | if (request->url().SchemeIsHTTPOrHTTPS()) { |
[email protected] | f0a51fb5 | 2009-03-05 12:46:38 | [diff] [blame] | 283 | // NOTE about status codes: We are like Firefox 3 in this respect. |
[email protected] | 677c9057 | 2008-12-10 09:03:15 | [diff] [blame] | 284 | // {IE 7, Safari 3, Opera 9.5} do not care about the status code. |
| 285 | if (request->GetResponseCode() != 200) { |
[email protected] | b30a3f5 | 2010-10-16 01:05:46 | [diff] [blame] | 286 | VLOG(1) << "Fetched PAC script had (bad) status line: " |
| 287 | << request->response_headers()->GetStatusLine(); |
Eric Roman | a22b1f7 | 2019-09-05 19:35:04 | [diff] [blame] | 288 | result_code_ = ERR_HTTP_RESPONSE_CODE_FAILURE; |
[email protected] | 677c9057 | 2008-12-10 09:03:15 | [diff] [blame] | 289 | request->Cancel(); |
| 290 | return; |
| 291 | } |
[email protected] | 13a279e | 2009-04-13 17:32:37 | [diff] [blame] | 292 | |
| 293 | // NOTE about mime types: We do not enforce mime types on PAC files. |
| 294 | // This is for compatibility with {IE 7, Firefox 3, Opera 9.5}. We will |
| 295 | // however log mismatches to help with debugging. |
[email protected] | e0ef2c2 | 2009-06-03 23:54:44 | [diff] [blame] | 296 | std::string mime_type; |
| 297 | cur_request_->GetMimeType(&mime_type); |
| 298 | if (!IsPacMimeType(mime_type)) { |
[email protected] | b30a3f5 | 2010-10-16 01:05:46 | [diff] [blame] | 299 | VLOG(1) << "Fetched PAC script does not have a proper mime type: " |
| 300 | << mime_type; |
[email protected] | 13a279e | 2009-04-13 17:32:37 | [diff] [blame] | 301 | } |
[email protected] | 677c9057 | 2008-12-10 09:03:15 | [diff] [blame] | 302 | } |
| 303 | |
| 304 | ReadBody(request); |
| 305 | } |
| 306 | |
Lily Houghton | 9959786 | 2018-03-07 16:40:42 | [diff] [blame] | 307 | void PacFileFetcherImpl::OnReadCompleted(URLRequest* request, int num_bytes) { |
maksim.sisov | bf794e2 | 2016-09-13 09:20:34 | [diff] [blame] | 308 | DCHECK_NE(ERR_IO_PENDING, num_bytes); |
| 309 | |
[email protected] | 096724916 | 2010-11-20 00:13:32 | [diff] [blame] | 310 | DCHECK_EQ(request, cur_request_.get()); |
| 311 | if (ConsumeBytesRead(request, num_bytes)) { |
| 312 | // Keep reading. |
[email protected] | 677c9057 | 2008-12-10 09:03:15 | [diff] [blame] | 313 | ReadBody(request); |
[email protected] | 677c9057 | 2008-12-10 09:03:15 | [diff] [blame] | 314 | } |
| 315 | } |
| 316 | |
Matt Menke | 27e91ae4 | 2019-09-10 16:10:48 | [diff] [blame^] | 317 | PacFileFetcherImpl::PacFileFetcherImpl(URLRequestContext* url_request_context) |
Eric Roman | ab9fc65 | 2018-05-08 01:59:53 | [diff] [blame] | 318 | : url_request_context_(url_request_context), |
Victor Costan | 9c7302b | 2018-08-27 16:39:44 | [diff] [blame] | 319 | buf_(base::MakeRefCounted<IOBuffer>(kBufSize)), |
Eric Roman | ab9fc65 | 2018-05-08 01:59:53 | [diff] [blame] | 320 | next_id_(0), |
| 321 | cur_request_id_(0), |
| 322 | result_code_(OK), |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 323 | result_text_(nullptr), |
Eric Roman | ab9fc65 | 2018-05-08 01:59:53 | [diff] [blame] | 324 | max_response_bytes_(kDefaultMaxResponseBytes), |
Matt Menke | 27e91ae4 | 2019-09-10 16:10:48 | [diff] [blame^] | 325 | max_duration_(kDefaultMaxDuration) { |
Eric Roman | ab9fc65 | 2018-05-08 01:59:53 | [diff] [blame] | 326 | DCHECK(url_request_context); |
| 327 | } |
| 328 | |
| 329 | bool PacFileFetcherImpl::IsUrlSchemeAllowed(const GURL& url) const { |
| 330 | // Always allow http://, https://, data:, and ftp://. |
| 331 | if (url.SchemeIsHTTPOrHTTPS() || url.SchemeIs("ftp") || url.SchemeIs("data")) |
| 332 | return true; |
| 333 | |
Eric Roman | ab9fc65 | 2018-05-08 01:59:53 | [diff] [blame] | 334 | // Disallow any other URL scheme. |
| 335 | return false; |
| 336 | } |
| 337 | |
Lily Houghton | 9959786 | 2018-03-07 16:40:42 | [diff] [blame] | 338 | void PacFileFetcherImpl::ReadBody(URLRequest* request) { |
[email protected] | 096724916 | 2010-11-20 00:13:32 | [diff] [blame] | 339 | // Read as many bytes as are available synchronously. |
| 340 | while (true) { |
maksim.sisov | bf794e2 | 2016-09-13 09:20:34 | [diff] [blame] | 341 | int num_bytes = request->Read(buf_.get(), kBufSize); |
| 342 | if (num_bytes == ERR_IO_PENDING) |
| 343 | return; |
| 344 | |
| 345 | if (num_bytes < 0) { |
| 346 | OnResponseCompleted(request, num_bytes); |
[email protected] | 096724916 | 2010-11-20 00:13:32 | [diff] [blame] | 347 | return; |
| 348 | } |
maksim.sisov | bf794e2 | 2016-09-13 09:20:34 | [diff] [blame] | 349 | |
[email protected] | 096724916 | 2010-11-20 00:13:32 | [diff] [blame] | 350 | if (!ConsumeBytesRead(request, num_bytes)) |
| 351 | return; |
[email protected] | 677c9057 | 2008-12-10 09:03:15 | [diff] [blame] | 352 | } |
| 353 | } |
| 354 | |
Lily Houghton | 9959786 | 2018-03-07 16:40:42 | [diff] [blame] | 355 | bool PacFileFetcherImpl::ConsumeBytesRead(URLRequest* request, int num_bytes) { |
Eric Roman | f417485 | 2018-02-16 22:47:13 | [diff] [blame] | 356 | if (fetch_time_to_first_byte_.is_null()) |
| 357 | fetch_time_to_first_byte_ = base::TimeTicks::Now(); |
| 358 | |
[email protected] | 096724916 | 2010-11-20 00:13:32 | [diff] [blame] | 359 | if (num_bytes <= 0) { |
| 360 | // Error while reading, or EOF. |
maksim.sisov | bf794e2 | 2016-09-13 09:20:34 | [diff] [blame] | 361 | OnResponseCompleted(request, num_bytes); |
[email protected] | 096724916 | 2010-11-20 00:13:32 | [diff] [blame] | 362 | return false; |
| 363 | } |
| 364 | |
| 365 | // Enforce maximum size bound. |
| 366 | if (num_bytes + bytes_read_so_far_.size() > |
| 367 | static_cast<size_t>(max_response_bytes_)) { |
| 368 | result_code_ = ERR_FILE_TOO_BIG; |
| 369 | request->Cancel(); |
| 370 | return false; |
| 371 | } |
| 372 | |
| 373 | bytes_read_so_far_.append(buf_->data(), num_bytes); |
| 374 | return true; |
| 375 | } |
| 376 | |
Lily Houghton | 9959786 | 2018-03-07 16:40:42 | [diff] [blame] | 377 | void PacFileFetcherImpl::FetchCompleted() { |
[email protected] | 8f3c9634 | 2009-09-22 03:06:54 | [diff] [blame] | 378 | if (result_code_ == OK) { |
Lily Houghton | 9959786 | 2018-03-07 16:40:42 | [diff] [blame] | 379 | // Calculate duration of time for PAC file fetch to complete. |
cbentzel | 27d2e5e5 | 2015-07-10 17:39:56 | [diff] [blame] | 380 | DCHECK(!fetch_start_time_.is_null()); |
cbentzel | e45ccba | 2015-08-21 18:23:14 | [diff] [blame] | 381 | DCHECK(!fetch_time_to_first_byte_.is_null()); |
| 382 | UMA_HISTOGRAM_MEDIUM_TIMES("Net.ProxyScriptFetcher.SuccessDuration", |
| 383 | base::TimeTicks::Now() - fetch_start_time_); |
| 384 | UMA_HISTOGRAM_MEDIUM_TIMES("Net.ProxyScriptFetcher.FirstByteDuration", |
| 385 | fetch_time_to_first_byte_ - fetch_start_time_); |
cbentzel | 27d2e5e5 | 2015-07-10 17:39:56 | [diff] [blame] | 386 | |
[email protected] | 9b9ae955 | 2010-07-01 22:20:50 | [diff] [blame] | 387 | // The caller expects the response to be encoded as UTF16. |
[email protected] | 8f3c9634 | 2009-09-22 03:06:54 | [diff] [blame] | 388 | std::string charset; |
| 389 | cur_request_->GetCharset(&charset); |
[email protected] | 9b9ae955 | 2010-07-01 22:20:50 | [diff] [blame] | 390 | ConvertResponseToUTF16(charset, bytes_read_so_far_, result_text_); |
[email protected] | 8f3c9634 | 2009-09-22 03:06:54 | [diff] [blame] | 391 | } else { |
| 392 | // On error, the caller expects empty string for bytes. |
[email protected] | 9b9ae955 | 2010-07-01 22:20:50 | [diff] [blame] | 393 | result_text_->clear(); |
[email protected] | 8f3c9634 | 2009-09-22 03:06:54 | [diff] [blame] | 394 | } |
[email protected] | 677c9057 | 2008-12-10 09:03:15 | [diff] [blame] | 395 | |
| 396 | int result_code = result_code_; |
Bence Béky | cc5b88a | 2018-05-25 20:24:17 | [diff] [blame] | 397 | CompletionOnceCallback callback = std::move(callback_); |
[email protected] | 677c9057 | 2008-12-10 09:03:15 | [diff] [blame] | 398 | |
| 399 | ResetCurRequestState(); |
| 400 | |
Bence Béky | cc5b88a | 2018-05-25 20:24:17 | [diff] [blame] | 401 | std::move(callback).Run(result_code); |
[email protected] | 677c9057 | 2008-12-10 09:03:15 | [diff] [blame] | 402 | } |
| 403 | |
Lily Houghton | 9959786 | 2018-03-07 16:40:42 | [diff] [blame] | 404 | void PacFileFetcherImpl::ResetCurRequestState() { |
[email protected] | 677c9057 | 2008-12-10 09:03:15 | [diff] [blame] | 405 | cur_request_.reset(); |
| 406 | cur_request_id_ = 0; |
[email protected] | 23578681 | 2011-12-20 02:15:31 | [diff] [blame] | 407 | callback_.Reset(); |
[email protected] | 677c9057 | 2008-12-10 09:03:15 | [diff] [blame] | 408 | result_code_ = OK; |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 409 | result_text_ = nullptr; |
cbentzel | e45ccba | 2015-08-21 18:23:14 | [diff] [blame] | 410 | fetch_start_time_ = base::TimeTicks(); |
| 411 | fetch_time_to_first_byte_ = base::TimeTicks(); |
[email protected] | 677c9057 | 2008-12-10 09:03:15 | [diff] [blame] | 412 | } |
| 413 | |
Lily Houghton | 9959786 | 2018-03-07 16:40:42 | [diff] [blame] | 414 | void PacFileFetcherImpl::OnTimeout(int id) { |
[email protected] | da968bc | 2011-01-19 11:48:19 | [diff] [blame] | 415 | // Timeout tasks may outlive the URLRequest they reference. Make sure it |
[email protected] | 677c9057 | 2008-12-10 09:03:15 | [diff] [blame] | 416 | // is still applicable. |
| 417 | if (cur_request_id_ != id) |
| 418 | return; |
| 419 | |
| 420 | DCHECK(cur_request_.get()); |
| 421 | result_code_ = ERR_TIMED_OUT; |
mmenke | ed8d7e43 | 2017-05-03 16:48:33 | [diff] [blame] | 422 | FetchCompleted(); |
[email protected] | 677c9057 | 2008-12-10 09:03:15 | [diff] [blame] | 423 | } |
| 424 | |
[email protected] | 677c9057 | 2008-12-10 09:03:15 | [diff] [blame] | 425 | } // namespace net |