Fix uninitialized value in BufferedResourceHandler unittest
[chromium-blink-merge.git] / net / proxy / proxy_script_fetcher_impl.cc
blob4e57121f281a0c0629c6d927bd120b2e14209198
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "net/proxy/proxy_script_fetcher_impl.h"
7 #include "base/compiler_specific.h"
8 #include "base/logging.h"
9 #include "base/message_loop/message_loop.h"
10 #include "base/profiler/scoped_tracker.h"
11 #include "base/strings/string_util.h"
12 #include "net/base/data_url.h"
13 #include "net/base/io_buffer.h"
14 #include "net/base/load_flags.h"
15 #include "net/base/net_errors.h"
16 #include "net/base/net_string_util.h"
17 #include "net/base/request_priority.h"
18 #include "net/cert/cert_status_flags.h"
19 #include "net/http/http_response_headers.h"
20 #include "net/url_request/url_request_context.h"
22 // TODO(eroman):
23 // - Support auth-prompts (http://crbug.com/77366)
25 namespace net {
27 namespace {
29 // The maximum size (in bytes) allowed for a PAC script. Responses exceeding
30 // this will fail with ERR_FILE_TOO_BIG.
31 const int kDefaultMaxResponseBytes = 1048576; // 1 megabyte
33 // The maximum duration (in milliseconds) allowed for fetching the PAC script.
34 // Responses exceeding this will fail with ERR_TIMED_OUT.
35 const int kDefaultMaxDurationMs = 300000; // 5 minutes
37 // Returns true if |mime_type| is one of the known PAC mime type.
38 bool IsPacMimeType(const std::string& mime_type) {
39 static const char * const kSupportedPacMimeTypes[] = {
40 "application/x-ns-proxy-autoconfig",
41 "application/x-javascript-config",
43 for (size_t i = 0; i < arraysize(kSupportedPacMimeTypes); ++i) {
44 if (LowerCaseEqualsASCII(mime_type, kSupportedPacMimeTypes[i]))
45 return true;
47 return false;
50 // Converts |bytes| (which is encoded by |charset|) to UTF16, saving the resul
51 // to |*utf16|.
52 // If |charset| is empty, then we don't know what it was and guess.
53 void ConvertResponseToUTF16(const std::string& charset,
54 const std::string& bytes,
55 base::string16* utf16) {
56 const char* codepage;
58 if (charset.empty()) {
59 // Assume ISO-8859-1 if no charset was specified.
60 codepage = kCharsetLatin1;
61 } else {
62 // Otherwise trust the charset that was provided.
63 codepage = charset.c_str();
66 // Be generous in the conversion -- if any characters lie outside of |charset|
67 // (i.e. invalid), then substitute them with U+FFFD rather than failing.
68 ConvertToUTF16WithSubstitutions(bytes, codepage, utf16);
71 } // namespace
73 ProxyScriptFetcherImpl::ProxyScriptFetcherImpl(
74 URLRequestContext* url_request_context)
75 : url_request_context_(url_request_context),
76 buf_(new IOBuffer(kBufSize)),
77 next_id_(0),
78 cur_request_id_(0),
79 result_code_(OK),
80 result_text_(NULL),
81 max_response_bytes_(kDefaultMaxResponseBytes),
82 max_duration_(base::TimeDelta::FromMilliseconds(kDefaultMaxDurationMs)),
83 weak_factory_(this) {
84 DCHECK(url_request_context);
87 ProxyScriptFetcherImpl::~ProxyScriptFetcherImpl() {
88 // The URLRequest's destructor will cancel the outstanding request, and
89 // ensure that the delegate (this) is not called again.
92 base::TimeDelta ProxyScriptFetcherImpl::SetTimeoutConstraint(
93 base::TimeDelta timeout) {
94 base::TimeDelta prev = max_duration_;
95 max_duration_ = timeout;
96 return prev;
99 size_t ProxyScriptFetcherImpl::SetSizeConstraint(size_t size_bytes) {
100 size_t prev = max_response_bytes_;
101 max_response_bytes_ = size_bytes;
102 return prev;
105 void ProxyScriptFetcherImpl::OnResponseCompleted(URLRequest* request) {
106 DCHECK_EQ(request, cur_request_.get());
108 // Use |result_code_| as the request's error if we have already set it to
109 // something specific.
110 if (result_code_ == OK && !request->status().is_success())
111 result_code_ = request->status().error();
113 FetchCompleted();
116 int ProxyScriptFetcherImpl::Fetch(
117 const GURL& url, base::string16* text, const CompletionCallback& callback) {
118 // It is invalid to call Fetch() while a request is already in progress.
119 DCHECK(!cur_request_.get());
120 DCHECK(!callback.is_null());
121 DCHECK(text);
123 // Handle base-64 encoded data-urls that contain custom PAC scripts.
124 if (url.SchemeIs("data")) {
125 std::string mime_type;
126 std::string charset;
127 std::string data;
128 if (!DataURL::Parse(url, &mime_type, &charset, &data))
129 return ERR_FAILED;
131 ConvertResponseToUTF16(charset, data, text);
132 return OK;
135 cur_request_ =
136 url_request_context_->CreateRequest(url, DEFAULT_PRIORITY, this, NULL);
137 cur_request_->set_method("GET");
139 // Make sure that the PAC script is downloaded using a direct connection,
140 // to avoid circular dependencies (fetching is a part of proxy resolution).
141 // Also disable the use of the disk cache. The cache is disabled so that if
142 // the user switches networks we don't potentially use the cached response
143 // from old network when we should in fact be re-fetching on the new network.
144 // If the PAC script is hosted on an HTTPS server we bypass revocation
145 // checking in order to avoid a circular dependency when attempting to fetch
146 // the OCSP response or CRL. We could make the revocation check go direct but
147 // the proxy might be the only way to the outside world.
148 cur_request_->SetLoadFlags(LOAD_BYPASS_PROXY | LOAD_DISABLE_CACHE |
149 LOAD_DISABLE_CERT_REVOCATION_CHECKING);
151 // Save the caller's info for notification on completion.
152 callback_ = callback;
153 result_text_ = text;
155 bytes_read_so_far_.clear();
157 // Post a task to timeout this request if it takes too long.
158 cur_request_id_ = ++next_id_;
159 base::MessageLoop::current()->PostDelayedTask(
160 FROM_HERE,
161 base::Bind(&ProxyScriptFetcherImpl::OnTimeout,
162 weak_factory_.GetWeakPtr(),
163 cur_request_id_),
164 max_duration_);
166 // Start the request.
167 cur_request_->Start();
168 return ERR_IO_PENDING;
171 void ProxyScriptFetcherImpl::Cancel() {
172 // ResetCurRequestState will free the URLRequest, which will cause
173 // cancellation.
174 ResetCurRequestState();
177 URLRequestContext* ProxyScriptFetcherImpl::GetRequestContext() const {
178 return url_request_context_;
181 void ProxyScriptFetcherImpl::OnAuthRequired(URLRequest* request,
182 AuthChallengeInfo* auth_info) {
183 DCHECK_EQ(request, cur_request_.get());
184 // TODO(eroman): http://crbug.com/77366
185 LOG(WARNING) << "Auth required to fetch PAC script, aborting.";
186 result_code_ = ERR_NOT_IMPLEMENTED;
187 request->CancelAuth();
190 void ProxyScriptFetcherImpl::OnSSLCertificateError(URLRequest* request,
191 const SSLInfo& ssl_info,
192 bool fatal) {
193 DCHECK_EQ(request, cur_request_.get());
194 // Revocation check failures are not fatal.
195 if (IsCertStatusMinorError(ssl_info.cert_status)) {
196 request->ContinueDespiteLastError();
197 return;
199 LOG(WARNING) << "SSL certificate error when fetching PAC script, aborting.";
200 // Certificate errors are in same space as net errors.
201 result_code_ = MapCertStatusToNetError(ssl_info.cert_status);
202 request->Cancel();
205 void ProxyScriptFetcherImpl::OnResponseStarted(URLRequest* request) {
206 // TODO(vadimt): Remove ScopedTracker below once crbug.com/423948 is fixed.
207 tracked_objects::ScopedTracker tracking_profile(
208 FROM_HERE_WITH_EXPLICIT_FUNCTION(
209 "423948 ProxyScriptFetcherImpl::OnResponseStarted"));
211 DCHECK_EQ(request, cur_request_.get());
213 if (!request->status().is_success()) {
214 OnResponseCompleted(request);
215 return;
218 // Require HTTP responses to have a success status code.
219 if (request->url().SchemeIsHTTPOrHTTPS()) {
220 // NOTE about status codes: We are like Firefox 3 in this respect.
221 // {IE 7, Safari 3, Opera 9.5} do not care about the status code.
222 if (request->GetResponseCode() != 200) {
223 VLOG(1) << "Fetched PAC script had (bad) status line: "
224 << request->response_headers()->GetStatusLine();
225 result_code_ = ERR_PAC_STATUS_NOT_OK;
226 request->Cancel();
227 return;
230 // NOTE about mime types: We do not enforce mime types on PAC files.
231 // This is for compatibility with {IE 7, Firefox 3, Opera 9.5}. We will
232 // however log mismatches to help with debugging.
233 std::string mime_type;
234 cur_request_->GetMimeType(&mime_type);
235 if (!IsPacMimeType(mime_type)) {
236 VLOG(1) << "Fetched PAC script does not have a proper mime type: "
237 << mime_type;
241 ReadBody(request);
244 void ProxyScriptFetcherImpl::OnReadCompleted(URLRequest* request,
245 int num_bytes) {
246 // TODO(vadimt): Remove ScopedTracker below once crbug.com/423948 is fixed.
247 tracked_objects::ScopedTracker tracking_profile(
248 FROM_HERE_WITH_EXPLICIT_FUNCTION(
249 "423948 ProxyScriptFetcherImpl::OnReadCompleted"));
251 DCHECK_EQ(request, cur_request_.get());
252 if (ConsumeBytesRead(request, num_bytes)) {
253 // Keep reading.
254 ReadBody(request);
258 void ProxyScriptFetcherImpl::ReadBody(URLRequest* request) {
259 // Read as many bytes as are available synchronously.
260 while (true) {
261 int num_bytes;
262 if (!request->Read(buf_.get(), kBufSize, &num_bytes)) {
263 // Check whether the read failed synchronously.
264 if (!request->status().is_io_pending())
265 OnResponseCompleted(request);
266 return;
268 if (!ConsumeBytesRead(request, num_bytes))
269 return;
273 bool ProxyScriptFetcherImpl::ConsumeBytesRead(URLRequest* request,
274 int num_bytes) {
275 if (num_bytes <= 0) {
276 // Error while reading, or EOF.
277 OnResponseCompleted(request);
278 return false;
281 // Enforce maximum size bound.
282 if (num_bytes + bytes_read_so_far_.size() >
283 static_cast<size_t>(max_response_bytes_)) {
284 result_code_ = ERR_FILE_TOO_BIG;
285 request->Cancel();
286 return false;
289 bytes_read_so_far_.append(buf_->data(), num_bytes);
290 return true;
293 void ProxyScriptFetcherImpl::FetchCompleted() {
294 if (result_code_ == OK) {
295 // The caller expects the response to be encoded as UTF16.
296 std::string charset;
297 cur_request_->GetCharset(&charset);
298 ConvertResponseToUTF16(charset, bytes_read_so_far_, result_text_);
299 } else {
300 // On error, the caller expects empty string for bytes.
301 result_text_->clear();
304 int result_code = result_code_;
305 CompletionCallback callback = callback_;
307 ResetCurRequestState();
309 callback.Run(result_code);
312 void ProxyScriptFetcherImpl::ResetCurRequestState() {
313 cur_request_.reset();
314 cur_request_id_ = 0;
315 callback_.Reset();
316 result_code_ = OK;
317 result_text_ = NULL;
320 void ProxyScriptFetcherImpl::OnTimeout(int id) {
321 // Timeout tasks may outlive the URLRequest they reference. Make sure it
322 // is still applicable.
323 if (cur_request_id_ != id)
324 return;
326 DCHECK(cur_request_.get());
327 result_code_ = ERR_TIMED_OUT;
328 cur_request_->Cancel();
331 } // namespace net