| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "net/proxy/proxy_script_fetcher_impl.h" | 5 #include "net/proxy/proxy_script_fetcher_impl.h" |
| 6 | 6 |
| 7 #include "base/compiler_specific.h" | 7 #include "base/compiler_specific.h" |
| 8 #include "base/i18n/icu_string_conversions.h" | 8 #include "base/i18n/icu_string_conversions.h" |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/message_loop.h" | 10 #include "base/message_loop.h" |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 79 cur_request_id_(0), | 79 cur_request_id_(0), |
| 80 callback_(NULL), | 80 callback_(NULL), |
| 81 result_code_(OK), | 81 result_code_(OK), |
| 82 result_text_(NULL), | 82 result_text_(NULL), |
| 83 max_response_bytes_(kDefaultMaxResponseBytes), | 83 max_response_bytes_(kDefaultMaxResponseBytes), |
| 84 max_duration_(base::TimeDelta::FromMilliseconds(kDefaultMaxDurationMs)) { | 84 max_duration_(base::TimeDelta::FromMilliseconds(kDefaultMaxDurationMs)) { |
| 85 DCHECK(url_request_context); | 85 DCHECK(url_request_context); |
| 86 } | 86 } |
| 87 | 87 |
| 88 ProxyScriptFetcherImpl::~ProxyScriptFetcherImpl() { | 88 ProxyScriptFetcherImpl::~ProxyScriptFetcherImpl() { |
| 89 // The URLRequest's destructor will cancel the outstanding request, and | 89 // The net::URLRequest's destructor will cancel the outstanding request, and |
| 90 // ensure that the delegate (this) is not called again. | 90 // ensure that the delegate (this) is not called again. |
| 91 } | 91 } |
| 92 | 92 |
| 93 int ProxyScriptFetcherImpl::Fetch(const GURL& url, | 93 int ProxyScriptFetcherImpl::Fetch(const GURL& url, |
| 94 string16* text, | 94 string16* text, |
| 95 CompletionCallback* callback) { | 95 CompletionCallback* callback) { |
| 96 // It is invalid to call Fetch() while a request is already in progress. | 96 // It is invalid to call Fetch() while a request is already in progress. |
| 97 DCHECK(!cur_request_.get()); | 97 DCHECK(!cur_request_.get()); |
| 98 | 98 |
| 99 DCHECK(callback); | 99 DCHECK(callback); |
| 100 DCHECK(text); | 100 DCHECK(text); |
| 101 | 101 |
| 102 cur_request_.reset(new URLRequest(url, this)); | 102 cur_request_.reset(new net::URLRequest(url, this)); |
| 103 cur_request_->set_context(url_request_context_); | 103 cur_request_->set_context(url_request_context_); |
| 104 cur_request_->set_method("GET"); | 104 cur_request_->set_method("GET"); |
| 105 | 105 |
| 106 // Make sure that the PAC script is downloaded using a direct connection, | 106 // Make sure that the PAC script is downloaded using a direct connection, |
| 107 // to avoid circular dependencies (fetching is a part of proxy resolution). | 107 // to avoid circular dependencies (fetching is a part of proxy resolution). |
| 108 // Also disable the use of the disk cache. The cache is disabled so that if | 108 // Also disable the use of the disk cache. The cache is disabled so that if |
| 109 // the user switches networks we don't potentially use the cached response | 109 // the user switches networks we don't potentially use the cached response |
| 110 // from old network when we should in fact be re-fetching on the new network. | 110 // from old network when we should in fact be re-fetching on the new network. |
| 111 cur_request_->set_load_flags(LOAD_BYPASS_PROXY | LOAD_DISABLE_CACHE); | 111 cur_request_->set_load_flags(LOAD_BYPASS_PROXY | LOAD_DISABLE_CACHE); |
| 112 | 112 |
| 113 // Save the caller's info for notification on completion. | 113 // Save the caller's info for notification on completion. |
| 114 callback_ = callback; | 114 callback_ = callback; |
| 115 result_text_ = text; | 115 result_text_ = text; |
| 116 | 116 |
| 117 bytes_read_so_far_.clear(); | 117 bytes_read_so_far_.clear(); |
| 118 | 118 |
| 119 // Post a task to timeout this request if it takes too long. | 119 // Post a task to timeout this request if it takes too long. |
| 120 cur_request_id_ = ++next_id_; | 120 cur_request_id_ = ++next_id_; |
| 121 MessageLoop::current()->PostDelayedTask(FROM_HERE, | 121 MessageLoop::current()->PostDelayedTask(FROM_HERE, |
| 122 task_factory_.NewRunnableMethod(&ProxyScriptFetcherImpl::OnTimeout, | 122 task_factory_.NewRunnableMethod(&ProxyScriptFetcherImpl::OnTimeout, |
| 123 cur_request_id_), | 123 cur_request_id_), |
| 124 static_cast<int>(max_duration_.InMilliseconds())); | 124 static_cast<int>(max_duration_.InMilliseconds())); |
| 125 | 125 |
| 126 // Start the request. | 126 // Start the request. |
| 127 cur_request_->Start(); | 127 cur_request_->Start(); |
| 128 return ERR_IO_PENDING; | 128 return ERR_IO_PENDING; |
| 129 } | 129 } |
| 130 | 130 |
| 131 void ProxyScriptFetcherImpl::Cancel() { | 131 void ProxyScriptFetcherImpl::Cancel() { |
| 132 // ResetCurRequestState will free the URLRequest, which will cause | 132 // ResetCurRequestState will free the net::URLRequest, which will cause |
| 133 // cancellation. | 133 // cancellation. |
| 134 ResetCurRequestState(); | 134 ResetCurRequestState(); |
| 135 } | 135 } |
| 136 | 136 |
| 137 URLRequestContext* ProxyScriptFetcherImpl::GetRequestContext() { | 137 URLRequestContext* ProxyScriptFetcherImpl::GetRequestContext() { |
| 138 return url_request_context_; | 138 return url_request_context_; |
| 139 } | 139 } |
| 140 | 140 |
| 141 void ProxyScriptFetcherImpl::OnAuthRequired(URLRequest* request, | 141 void ProxyScriptFetcherImpl::OnAuthRequired(net::URLRequest* request, |
| 142 AuthChallengeInfo* auth_info) { | 142 AuthChallengeInfo* auth_info) { |
| 143 DCHECK_EQ(request, cur_request_.get()); | 143 DCHECK_EQ(request, cur_request_.get()); |
| 144 // TODO(eroman): | 144 // TODO(eroman): |
| 145 LOG(WARNING) << "Auth required to fetch PAC script, aborting."; | 145 LOG(WARNING) << "Auth required to fetch PAC script, aborting."; |
| 146 result_code_ = ERR_NOT_IMPLEMENTED; | 146 result_code_ = ERR_NOT_IMPLEMENTED; |
| 147 request->CancelAuth(); | 147 request->CancelAuth(); |
| 148 } | 148 } |
| 149 | 149 |
| 150 void ProxyScriptFetcherImpl::OnSSLCertificateError(URLRequest* request, | 150 void ProxyScriptFetcherImpl::OnSSLCertificateError(net::URLRequest* request, |
| 151 int cert_error, | 151 int cert_error, |
| 152 X509Certificate* cert) { | 152 X509Certificate* cert) { |
| 153 DCHECK_EQ(request, cur_request_.get()); | 153 DCHECK_EQ(request, cur_request_.get()); |
| 154 LOG(WARNING) << "SSL certificate error when fetching PAC script, aborting."; | 154 LOG(WARNING) << "SSL certificate error when fetching PAC script, aborting."; |
| 155 // Certificate errors are in same space as net errors. | 155 // Certificate errors are in same space as net errors. |
| 156 result_code_ = cert_error; | 156 result_code_ = cert_error; |
| 157 request->Cancel(); | 157 request->Cancel(); |
| 158 } | 158 } |
| 159 | 159 |
| 160 void ProxyScriptFetcherImpl::OnResponseStarted(URLRequest* request) { | 160 void ProxyScriptFetcherImpl::OnResponseStarted(net::URLRequest* request) { |
| 161 DCHECK_EQ(request, cur_request_.get()); | 161 DCHECK_EQ(request, cur_request_.get()); |
| 162 | 162 |
| 163 if (!request->status().is_success()) { | 163 if (!request->status().is_success()) { |
| 164 OnResponseCompleted(request); | 164 OnResponseCompleted(request); |
| 165 return; | 165 return; |
| 166 } | 166 } |
| 167 | 167 |
| 168 // Require HTTP responses to have a success status code. | 168 // Require HTTP responses to have a success status code. |
| 169 if (request->url().SchemeIs("http") || request->url().SchemeIs("https")) { | 169 if (request->url().SchemeIs("http") || request->url().SchemeIs("https")) { |
| 170 // NOTE about status codes: We are like Firefox 3 in this respect. | 170 // NOTE about status codes: We are like Firefox 3 in this respect. |
| (...skipping 13 matching lines...) Expand all Loading... |
| 184 cur_request_->GetMimeType(&mime_type); | 184 cur_request_->GetMimeType(&mime_type); |
| 185 if (!IsPacMimeType(mime_type)) { | 185 if (!IsPacMimeType(mime_type)) { |
| 186 VLOG(1) << "Fetched PAC script does not have a proper mime type: " | 186 VLOG(1) << "Fetched PAC script does not have a proper mime type: " |
| 187 << mime_type; | 187 << mime_type; |
| 188 } | 188 } |
| 189 } | 189 } |
| 190 | 190 |
| 191 ReadBody(request); | 191 ReadBody(request); |
| 192 } | 192 } |
| 193 | 193 |
| 194 void ProxyScriptFetcherImpl::OnReadCompleted(URLRequest* request, | 194 void ProxyScriptFetcherImpl::OnReadCompleted(net::URLRequest* request, |
| 195 int num_bytes) { | 195 int num_bytes) { |
| 196 DCHECK_EQ(request, cur_request_.get()); | 196 DCHECK_EQ(request, cur_request_.get()); |
| 197 if (ConsumeBytesRead(request, num_bytes)) { | 197 if (ConsumeBytesRead(request, num_bytes)) { |
| 198 // Keep reading. | 198 // Keep reading. |
| 199 ReadBody(request); | 199 ReadBody(request); |
| 200 } | 200 } |
| 201 } | 201 } |
| 202 | 202 |
| 203 void ProxyScriptFetcherImpl::OnResponseCompleted(URLRequest* request) { | 203 void ProxyScriptFetcherImpl::OnResponseCompleted(net::URLRequest* request) { |
| 204 DCHECK_EQ(request, cur_request_.get()); | 204 DCHECK_EQ(request, cur_request_.get()); |
| 205 | 205 |
| 206 // Use |result_code_| as the request's error if we have already set it to | 206 // Use |result_code_| as the request's error if we have already set it to |
| 207 // something specific. | 207 // something specific. |
| 208 if (result_code_ == OK && !request->status().is_success()) | 208 if (result_code_ == OK && !request->status().is_success()) |
| 209 result_code_ = request->status().os_error(); | 209 result_code_ = request->status().os_error(); |
| 210 | 210 |
| 211 FetchCompleted(); | 211 FetchCompleted(); |
| 212 } | 212 } |
| 213 | 213 |
| 214 void ProxyScriptFetcherImpl::ReadBody(URLRequest* request) { | 214 void ProxyScriptFetcherImpl::ReadBody(net::URLRequest* request) { |
| 215 // Read as many bytes as are available synchronously. | 215 // Read as many bytes as are available synchronously. |
| 216 while (true) { | 216 while (true) { |
| 217 int num_bytes; | 217 int num_bytes; |
| 218 if (!request->Read(buf_, kBufSize, &num_bytes)) { | 218 if (!request->Read(buf_, kBufSize, &num_bytes)) { |
| 219 // Check whether the read failed synchronously. | 219 // Check whether the read failed synchronously. |
| 220 if (!request->status().is_io_pending()) | 220 if (!request->status().is_io_pending()) |
| 221 OnResponseCompleted(request); | 221 OnResponseCompleted(request); |
| 222 return; | 222 return; |
| 223 } | 223 } |
| 224 if (!ConsumeBytesRead(request, num_bytes)) | 224 if (!ConsumeBytesRead(request, num_bytes)) |
| 225 return; | 225 return; |
| 226 } | 226 } |
| 227 } | 227 } |
| 228 | 228 |
| 229 bool ProxyScriptFetcherImpl::ConsumeBytesRead(URLRequest* request, | 229 bool ProxyScriptFetcherImpl::ConsumeBytesRead(net::URLRequest* request, |
| 230 int num_bytes) { | 230 int num_bytes) { |
| 231 if (num_bytes <= 0) { | 231 if (num_bytes <= 0) { |
| 232 // Error while reading, or EOF. | 232 // Error while reading, or EOF. |
| 233 OnResponseCompleted(request); | 233 OnResponseCompleted(request); |
| 234 return false; | 234 return false; |
| 235 } | 235 } |
| 236 | 236 |
| 237 // Enforce maximum size bound. | 237 // Enforce maximum size bound. |
| 238 if (num_bytes + bytes_read_so_far_.size() > | 238 if (num_bytes + bytes_read_so_far_.size() > |
| 239 static_cast<size_t>(max_response_bytes_)) { | 239 static_cast<size_t>(max_response_bytes_)) { |
| (...skipping 30 matching lines...) Expand all Loading... |
| 270 | 270 |
| 271 void ProxyScriptFetcherImpl::ResetCurRequestState() { | 271 void ProxyScriptFetcherImpl::ResetCurRequestState() { |
| 272 cur_request_.reset(); | 272 cur_request_.reset(); |
| 273 cur_request_id_ = 0; | 273 cur_request_id_ = 0; |
| 274 callback_ = NULL; | 274 callback_ = NULL; |
| 275 result_code_ = OK; | 275 result_code_ = OK; |
| 276 result_text_ = NULL; | 276 result_text_ = NULL; |
| 277 } | 277 } |
| 278 | 278 |
| 279 void ProxyScriptFetcherImpl::OnTimeout(int id) { | 279 void ProxyScriptFetcherImpl::OnTimeout(int id) { |
| 280 // Timeout tasks may outlive the URLRequest they reference. Make sure it | 280 // Timeout tasks may outlive the net::URLRequest they reference. Make sure it |
| 281 // is still applicable. | 281 // is still applicable. |
| 282 if (cur_request_id_ != id) | 282 if (cur_request_id_ != id) |
| 283 return; | 283 return; |
| 284 | 284 |
| 285 DCHECK(cur_request_.get()); | 285 DCHECK(cur_request_.get()); |
| 286 result_code_ = ERR_TIMED_OUT; | 286 result_code_ = ERR_TIMED_OUT; |
| 287 cur_request_->Cancel(); | 287 cur_request_->Cancel(); |
| 288 } | 288 } |
| 289 | 289 |
| 290 base::TimeDelta ProxyScriptFetcherImpl::SetTimeoutConstraint( | 290 base::TimeDelta ProxyScriptFetcherImpl::SetTimeoutConstraint( |
| 291 base::TimeDelta timeout) { | 291 base::TimeDelta timeout) { |
| 292 base::TimeDelta prev = max_duration_; | 292 base::TimeDelta prev = max_duration_; |
| 293 max_duration_ = timeout; | 293 max_duration_ = timeout; |
| 294 return prev; | 294 return prev; |
| 295 } | 295 } |
| 296 | 296 |
| 297 size_t ProxyScriptFetcherImpl::SetSizeConstraint(size_t size_bytes) { | 297 size_t ProxyScriptFetcherImpl::SetSizeConstraint(size_t size_bytes) { |
| 298 size_t prev = max_response_bytes_; | 298 size_t prev = max_response_bytes_; |
| 299 max_response_bytes_ = size_bytes; | 299 max_response_bytes_ = size_bytes; |
| 300 return prev; | 300 return prev; |
| 301 } | 301 } |
| 302 | 302 |
| 303 } // namespace net | 303 } // namespace net |
| OLD | NEW |