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