blob: 8f728f790f83328d8e0eab6ebc4db492eb58e433 [file] [log] [blame]
[email protected]26b9973962012-01-28 00:57:001// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]9b9ae9552010-07-01 22:20:502// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
[email protected]677c90572008-12-10 09:03:154
[email protected]86933612010-10-16 23:10:335#include "net/proxy/proxy_script_fetcher_impl.h"
[email protected]677c90572008-12-10 09:03:156
7#include "base/compiler_specific.h"
skyostil4891b25b2015-06-11 11:43:458#include "base/location.h"
[email protected]13a279e2009-04-13 17:32:379#include "base/logging.h"
cbentzel27d2e5e52015-07-10 17:39:5610#include "base/metrics/histogram.h"
skyostil4891b25b2015-06-11 11:43:4511#include "base/single_thread_task_runner.h"
[email protected]fc9be5802013-06-11 10:56:5112#include "base/strings/string_util.h"
skyostil4891b25b2015-06-11 11:43:4513#include "base/thread_task_runner_handle.h"
[email protected]d9d71e082011-02-16 11:44:2814#include "net/base/data_url.h"
[email protected]9dea9e1f2009-01-29 00:30:4715#include "net/base/io_buffer.h"
[email protected]677c90572008-12-10 09:03:1516#include "net/base/load_flags.h"
[email protected]597cf6e2009-05-29 09:43:2617#include "net/base/net_errors.h"
[email protected]de362402014-05-10 18:32:4718#include "net/base/net_string_util.h"
[email protected]2ca01e52013-10-31 22:05:1919#include "net/base/request_priority.h"
[email protected]6e7845ae2013-03-29 21:48:1120#include "net/cert/cert_status_flags.h"
[email protected]e0ef2c22009-06-03 23:54:4421#include "net/http/http_response_headers.h"
[email protected]86933612010-10-16 23:10:3322#include "net/url_request/url_request_context.h"
[email protected]677c90572008-12-10 09:03:1523
[email protected]f0a51fb52009-03-05 12:46:3824// TODO(eroman):
[email protected]33abb682011-03-29 03:58:4225// - Support auth-prompts (http://crbug.com/77366)
[email protected]677c90572008-12-10 09:03:1526
27namespace net {
28
29namespace {
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]86933612010-10-16 23:10:3333const int kDefaultMaxResponseBytes = 1048576; // 1 megabyte
[email protected]677c90572008-12-10 09:03:1534
35// The maximum duration (in milliseconds) allowed for fetching the PAC script.
36// Responses exceeding this will fail with ERR_TIMED_OUT.
[email protected]86933612010-10-16 23:10:3337const int kDefaultMaxDurationMs = 300000; // 5 minutes
[email protected]677c90572008-12-10 09:03:1538
[email protected]13a279e2009-04-13 17:32:3739// Returns true if |mime_type| is one of the known PAC mime type.
40bool IsPacMimeType(const std::string& mime_type) {
41 static const char * const kSupportedPacMimeTypes[] = {
42 "application/x-ns-proxy-autoconfig",
43 "application/x-javascript-config",
44 };
45 for (size_t i = 0; i < arraysize(kSupportedPacMimeTypes); ++i) {
brettwbc17d2c82015-06-09 22:39:0846 if (base::LowerCaseEqualsASCII(mime_type, kSupportedPacMimeTypes[i]))
[email protected]13a279e2009-04-13 17:32:3747 return true;
48 }
49 return false;
50}
51
[email protected]9b9ae9552010-07-01 22:20:5052// Converts |bytes| (which is encoded by |charset|) to UTF16, saving the resul
53// to |*utf16|.
[email protected]8f3c96342009-09-22 03:06:5454// If |charset| is empty, then we don't know what it was and guess.
[email protected]9b9ae9552010-07-01 22:20:5055void ConvertResponseToUTF16(const std::string& charset,
56 const std::string& bytes,
[email protected]42cba2fb2013-03-29 19:58:5757 base::string16* utf16) {
[email protected]8f3c96342009-09-22 03:06:5458 const char* codepage;
59
60 if (charset.empty()) {
61 // Assume ISO-8859-1 if no charset was specified.
[email protected]de362402014-05-10 18:32:4762 codepage = kCharsetLatin1;
[email protected]8f3c96342009-09-22 03:06:5463 } else {
64 // Otherwise trust the charset that was provided.
65 codepage = charset.c_str();
66 }
67
[email protected]de362402014-05-10 18:32:4768 // Be generous in the conversion -- if any characters lie outside of |charset|
69 // (i.e. invalid), then substitute them with U+FFFD rather than failing.
70 ConvertToUTF16WithSubstitutions(bytes, codepage, utf16);
[email protected]8f3c96342009-09-22 03:06:5471}
72
[email protected]677c90572008-12-10 09:03:1573} // namespace
74
[email protected]677c90572008-12-10 09:03:1575ProxyScriptFetcherImpl::ProxyScriptFetcherImpl(
[email protected]da968bc2011-01-19 11:48:1976 URLRequestContext* url_request_context)
kulkarni.acd7b4462014-08-28 07:41:3477 : url_request_context_(url_request_context),
[email protected]da968bc2011-01-19 11:48:1978 buf_(new IOBuffer(kBufSize)),
[email protected]677c90572008-12-10 09:03:1579 next_id_(0),
[email protected]677c90572008-12-10 09:03:1580 cur_request_id_(0),
[email protected]677c90572008-12-10 09:03:1581 result_code_(OK),
[email protected]86933612010-10-16 23:10:3382 result_text_(NULL),
83 max_response_bytes_(kDefaultMaxResponseBytes),
kulkarni.acd7b4462014-08-28 07:41:3484 max_duration_(base::TimeDelta::FromMilliseconds(kDefaultMaxDurationMs)),
85 weak_factory_(this) {
[email protected]677c90572008-12-10 09:03:1586 DCHECK(url_request_context);
87}
88
89ProxyScriptFetcherImpl::~ProxyScriptFetcherImpl() {
[email protected]da968bc2011-01-19 11:48:1990 // The URLRequest's destructor will cancel the outstanding request, and
[email protected]677c90572008-12-10 09:03:1591 // ensure that the delegate (this) is not called again.
92}
93
[email protected]7aefb152011-01-21 23:46:4994base::TimeDelta ProxyScriptFetcherImpl::SetTimeoutConstraint(
95 base::TimeDelta timeout) {
96 base::TimeDelta prev = max_duration_;
97 max_duration_ = timeout;
98 return prev;
99}
100
101size_t ProxyScriptFetcherImpl::SetSizeConstraint(size_t size_bytes) {
102 size_t prev = max_response_bytes_;
103 max_response_bytes_ = size_bytes;
104 return prev;
105}
106
107void ProxyScriptFetcherImpl::OnResponseCompleted(URLRequest* request) {
108 DCHECK_EQ(request, cur_request_.get());
109
110 // Use |result_code_| as the request's error if we have already set it to
111 // something specific.
112 if (result_code_ == OK && !request->status().is_success())
[email protected]d0cc35b2011-09-08 12:02:05113 result_code_ = request->status().error();
[email protected]7aefb152011-01-21 23:46:49114
115 FetchCompleted();
116}
117
[email protected]235786812011-12-20 02:15:31118int ProxyScriptFetcherImpl::Fetch(
[email protected]42cba2fb2013-03-29 19:58:57119 const GURL& url, base::string16* text, const CompletionCallback& callback) {
[email protected]677c90572008-12-10 09:03:15120 // It is invalid to call Fetch() while a request is already in progress.
121 DCHECK(!cur_request_.get());
[email protected]235786812011-12-20 02:15:31122 DCHECK(!callback.is_null());
[email protected]9b9ae9552010-07-01 22:20:50123 DCHECK(text);
[email protected]677c90572008-12-10 09:03:15124
[email protected]d9d71e082011-02-16 11:44:28125 // Handle base-64 encoded data-urls that contain custom PAC scripts.
126 if (url.SchemeIs("data")) {
127 std::string mime_type;
128 std::string charset;
129 std::string data;
130 if (!DataURL::Parse(url, &mime_type, &charset, &data))
131 return ERR_FAILED;
132
133 ConvertResponseToUTF16(charset, data, text);
134 return OK;
135 }
136
cbentzel27d2e5e52015-07-10 17:39:56137 DCHECK(fetch_start_time_.is_null());
cbentzele45ccba2015-08-21 18:23:14138 fetch_start_time_ = base::TimeTicks::Now();
cbentzel27d2e5e52015-07-10 17:39:56139
[email protected]2ca01e52013-10-31 22:05:19140 cur_request_ =
davidben151423e2015-03-23 18:48:36141 url_request_context_->CreateRequest(url, DEFAULT_PRIORITY, this);
[email protected]677c90572008-12-10 09:03:15142 cur_request_->set_method("GET");
143
144 // Make sure that the PAC script is downloaded using a direct connection,
145 // to avoid circular dependencies (fetching is a part of proxy resolution).
[email protected]e0ef2c22009-06-03 23:54:44146 // Also disable the use of the disk cache. The cache is disabled so that if
147 // the user switches networks we don't potentially use the cached response
148 // from old network when we should in fact be re-fetching on the new network.
[email protected]6fbac162011-06-20 00:29:04149 // If the PAC script is hosted on an HTTPS server we bypass revocation
150 // checking in order to avoid a circular dependency when attempting to fetch
151 // the OCSP response or CRL. We could make the revocation check go direct but
152 // the proxy might be the only way to the outside world.
[email protected]bb1c4662013-11-14 00:00:07153 cur_request_->SetLoadFlags(LOAD_BYPASS_PROXY | LOAD_DISABLE_CACHE |
154 LOAD_DISABLE_CERT_REVOCATION_CHECKING);
[email protected]677c90572008-12-10 09:03:15155
156 // Save the caller's info for notification on completion.
157 callback_ = callback;
[email protected]9b9ae9552010-07-01 22:20:50158 result_text_ = text;
159
160 bytes_read_so_far_.clear();
[email protected]677c90572008-12-10 09:03:15161
162 // Post a task to timeout this request if it takes too long.
163 cur_request_id_ = ++next_id_;
eromane1867242015-04-23 23:07:44164
skyostil4891b25b2015-06-11 11:43:45165 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
166 FROM_HERE, base::Bind(&ProxyScriptFetcherImpl::OnTimeout,
167 weak_factory_.GetWeakPtr(), cur_request_id_),
[email protected]26b9973962012-01-28 00:57:00168 max_duration_);
[email protected]677c90572008-12-10 09:03:15169
170 // Start the request.
171 cur_request_->Start();
[email protected]620f5712009-08-04 22:43:12172 return ERR_IO_PENDING;
[email protected]677c90572008-12-10 09:03:15173}
174
175void ProxyScriptFetcherImpl::Cancel() {
[email protected]da968bc2011-01-19 11:48:19176 // ResetCurRequestState will free the URLRequest, which will cause
[email protected]677c90572008-12-10 09:03:15177 // cancellation.
178 ResetCurRequestState();
179}
180
[email protected]7258def2011-05-17 19:53:00181URLRequestContext* ProxyScriptFetcherImpl::GetRequestContext() const {
[email protected]20d296ddc2009-11-18 23:07:08182 return url_request_context_;
183}
184
[email protected]da968bc2011-01-19 11:48:19185void ProxyScriptFetcherImpl::OnAuthRequired(URLRequest* request,
[email protected]677c90572008-12-10 09:03:15186 AuthChallengeInfo* auth_info) {
[email protected]0967249162010-11-20 00:13:32187 DCHECK_EQ(request, cur_request_.get());
[email protected]33abb682011-03-29 03:58:42188 // TODO(eroman): http://crbug.com/77366
[email protected]13a279e2009-04-13 17:32:37189 LOG(WARNING) << "Auth required to fetch PAC script, aborting.";
[email protected]677c90572008-12-10 09:03:15190 result_code_ = ERR_NOT_IMPLEMENTED;
191 request->CancelAuth();
192}
193
[email protected]da968bc2011-01-19 11:48:19194void ProxyScriptFetcherImpl::OnSSLCertificateError(URLRequest* request,
[email protected]e5624f02011-09-27 19:43:53195 const SSLInfo& ssl_info,
[email protected]46d117e2012-01-18 01:53:14196 bool fatal) {
[email protected]0967249162010-11-20 00:13:32197 DCHECK_EQ(request, cur_request_.get());
[email protected]5db5a73d2011-10-12 16:19:36198 // Revocation check failures are not fatal.
199 if (IsCertStatusMinorError(ssl_info.cert_status)) {
200 request->ContinueDespiteLastError();
201 return;
202 }
[email protected]13a279e2009-04-13 17:32:37203 LOG(WARNING) << "SSL certificate error when fetching PAC script, aborting.";
[email protected]677c90572008-12-10 09:03:15204 // Certificate errors are in same space as net errors.
[email protected]e5624f02011-09-27 19:43:53205 result_code_ = MapCertStatusToNetError(ssl_info.cert_status);
[email protected]677c90572008-12-10 09:03:15206 request->Cancel();
207}
208
[email protected]da968bc2011-01-19 11:48:19209void ProxyScriptFetcherImpl::OnResponseStarted(URLRequest* request) {
[email protected]0967249162010-11-20 00:13:32210 DCHECK_EQ(request, cur_request_.get());
[email protected]677c90572008-12-10 09:03:15211
212 if (!request->status().is_success()) {
213 OnResponseCompleted(request);
214 return;
215 }
216
217 // Require HTTP responses to have a success status code.
[email protected]91f5689032013-08-22 01:43:33218 if (request->url().SchemeIsHTTPOrHTTPS()) {
[email protected]f0a51fb52009-03-05 12:46:38219 // NOTE about status codes: We are like Firefox 3 in this respect.
[email protected]677c90572008-12-10 09:03:15220 // {IE 7, Safari 3, Opera 9.5} do not care about the status code.
221 if (request->GetResponseCode() != 200) {
[email protected]b30a3f52010-10-16 01:05:46222 VLOG(1) << "Fetched PAC script had (bad) status line: "
223 << request->response_headers()->GetStatusLine();
[email protected]677c90572008-12-10 09:03:15224 result_code_ = ERR_PAC_STATUS_NOT_OK;
225 request->Cancel();
226 return;
227 }
[email protected]13a279e2009-04-13 17:32:37228
229 // NOTE about mime types: We do not enforce mime types on PAC files.
230 // This is for compatibility with {IE 7, Firefox 3, Opera 9.5}. We will
231 // however log mismatches to help with debugging.
[email protected]e0ef2c22009-06-03 23:54:44232 std::string mime_type;
233 cur_request_->GetMimeType(&mime_type);
234 if (!IsPacMimeType(mime_type)) {
[email protected]b30a3f52010-10-16 01:05:46235 VLOG(1) << "Fetched PAC script does not have a proper mime type: "
236 << mime_type;
[email protected]13a279e2009-04-13 17:32:37237 }
[email protected]677c90572008-12-10 09:03:15238 }
239
240 ReadBody(request);
241}
242
[email protected]da968bc2011-01-19 11:48:19243void ProxyScriptFetcherImpl::OnReadCompleted(URLRequest* request,
[email protected]677c90572008-12-10 09:03:15244 int num_bytes) {
[email protected]0967249162010-11-20 00:13:32245 DCHECK_EQ(request, cur_request_.get());
246 if (ConsumeBytesRead(request, num_bytes)) {
247 // Keep reading.
[email protected]677c90572008-12-10 09:03:15248 ReadBody(request);
[email protected]677c90572008-12-10 09:03:15249 }
250}
251
[email protected]da968bc2011-01-19 11:48:19252void ProxyScriptFetcherImpl::ReadBody(URLRequest* request) {
[email protected]0967249162010-11-20 00:13:32253 // Read as many bytes as are available synchronously.
254 while (true) {
255 int num_bytes;
[email protected]90499482013-06-01 00:39:50256 if (!request->Read(buf_.get(), kBufSize, &num_bytes)) {
[email protected]0967249162010-11-20 00:13:32257 // Check whether the read failed synchronously.
258 if (!request->status().is_io_pending())
259 OnResponseCompleted(request);
260 return;
261 }
262 if (!ConsumeBytesRead(request, num_bytes))
263 return;
[email protected]677c90572008-12-10 09:03:15264 }
265}
266
[email protected]da968bc2011-01-19 11:48:19267bool ProxyScriptFetcherImpl::ConsumeBytesRead(URLRequest* request,
[email protected]0967249162010-11-20 00:13:32268 int num_bytes) {
269 if (num_bytes <= 0) {
270 // Error while reading, or EOF.
271 OnResponseCompleted(request);
272 return false;
273 }
274
275 // Enforce maximum size bound.
276 if (num_bytes + bytes_read_so_far_.size() >
277 static_cast<size_t>(max_response_bytes_)) {
278 result_code_ = ERR_FILE_TOO_BIG;
279 request->Cancel();
280 return false;
281 }
282
cbentzele45ccba2015-08-21 18:23:14283 if (bytes_read_so_far_.empty()) {
284 DCHECK(fetch_time_to_first_byte_.is_null());
285 fetch_time_to_first_byte_ = base::TimeTicks::Now();
286 }
287
[email protected]0967249162010-11-20 00:13:32288 bytes_read_so_far_.append(buf_->data(), num_bytes);
289 return true;
290}
291
[email protected]677c90572008-12-10 09:03:15292void ProxyScriptFetcherImpl::FetchCompleted() {
[email protected]8f3c96342009-09-22 03:06:54293 if (result_code_ == OK) {
cbentzel27d2e5e52015-07-10 17:39:56294 // Calculate duration of time for proxy script fetch to complete.
295 DCHECK(!fetch_start_time_.is_null());
cbentzele45ccba2015-08-21 18:23:14296 DCHECK(!fetch_time_to_first_byte_.is_null());
297 UMA_HISTOGRAM_MEDIUM_TIMES("Net.ProxyScriptFetcher.SuccessDuration",
298 base::TimeTicks::Now() - fetch_start_time_);
299 UMA_HISTOGRAM_MEDIUM_TIMES("Net.ProxyScriptFetcher.FirstByteDuration",
300 fetch_time_to_first_byte_ - fetch_start_time_);
cbentzel27d2e5e52015-07-10 17:39:56301
[email protected]9b9ae9552010-07-01 22:20:50302 // The caller expects the response to be encoded as UTF16.
[email protected]8f3c96342009-09-22 03:06:54303 std::string charset;
304 cur_request_->GetCharset(&charset);
[email protected]9b9ae9552010-07-01 22:20:50305 ConvertResponseToUTF16(charset, bytes_read_so_far_, result_text_);
[email protected]8f3c96342009-09-22 03:06:54306 } else {
307 // On error, the caller expects empty string for bytes.
[email protected]9b9ae9552010-07-01 22:20:50308 result_text_->clear();
[email protected]8f3c96342009-09-22 03:06:54309 }
[email protected]677c90572008-12-10 09:03:15310
311 int result_code = result_code_;
[email protected]235786812011-12-20 02:15:31312 CompletionCallback callback = callback_;
[email protected]677c90572008-12-10 09:03:15313
314 ResetCurRequestState();
315
[email protected]235786812011-12-20 02:15:31316 callback.Run(result_code);
[email protected]677c90572008-12-10 09:03:15317}
318
319void ProxyScriptFetcherImpl::ResetCurRequestState() {
320 cur_request_.reset();
321 cur_request_id_ = 0;
[email protected]235786812011-12-20 02:15:31322 callback_.Reset();
[email protected]677c90572008-12-10 09:03:15323 result_code_ = OK;
[email protected]9b9ae9552010-07-01 22:20:50324 result_text_ = NULL;
cbentzele45ccba2015-08-21 18:23:14325 fetch_start_time_ = base::TimeTicks();
326 fetch_time_to_first_byte_ = base::TimeTicks();
[email protected]677c90572008-12-10 09:03:15327}
328
329void ProxyScriptFetcherImpl::OnTimeout(int id) {
[email protected]da968bc2011-01-19 11:48:19330 // Timeout tasks may outlive the URLRequest they reference. Make sure it
[email protected]677c90572008-12-10 09:03:15331 // is still applicable.
332 if (cur_request_id_ != id)
333 return;
334
335 DCHECK(cur_request_.get());
336 result_code_ = ERR_TIMED_OUT;
337 cur_request_->Cancel();
338}
339
[email protected]677c90572008-12-10 09:03:15340} // namespace net