| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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/location.h" | 8 #include "base/location.h" |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/metrics/histogram_macros.h" | 10 #include "base/metrics/histogram_macros.h" |
| (...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 97 max_duration_ = timeout; | 97 max_duration_ = timeout; |
| 98 return prev; | 98 return prev; |
| 99 } | 99 } |
| 100 | 100 |
| 101 size_t ProxyScriptFetcherImpl::SetSizeConstraint(size_t size_bytes) { | 101 size_t ProxyScriptFetcherImpl::SetSizeConstraint(size_t size_bytes) { |
| 102 size_t prev = max_response_bytes_; | 102 size_t prev = max_response_bytes_; |
| 103 max_response_bytes_ = size_bytes; | 103 max_response_bytes_ = size_bytes; |
| 104 return prev; | 104 return prev; |
| 105 } | 105 } |
| 106 | 106 |
| 107 void ProxyScriptFetcherImpl::OnResponseCompleted(URLRequest* request) { | 107 void ProxyScriptFetcherImpl::OnResponseCompleted(URLRequest* request, |
| 108 int net_error) { |
| 108 DCHECK_EQ(request, cur_request_.get()); | 109 DCHECK_EQ(request, cur_request_.get()); |
| 109 | 110 |
| 110 // Use |result_code_| as the request's error if we have already set it to | 111 // Use |result_code_| as the request's error if we have already set it to |
| 111 // something specific. | 112 // something specific. |
| 112 if (result_code_ == OK && !request->status().is_success()) | 113 if (result_code_ == OK && net_error != OK) |
| 113 result_code_ = request->status().error(); | 114 result_code_ = net_error; |
| 114 | 115 |
| 115 FetchCompleted(); | 116 FetchCompleted(); |
| 116 } | 117 } |
| 117 | 118 |
| 118 int ProxyScriptFetcherImpl::Fetch( | 119 int ProxyScriptFetcherImpl::Fetch( |
| 119 const GURL& url, base::string16* text, const CompletionCallback& callback) { | 120 const GURL& url, base::string16* text, const CompletionCallback& callback) { |
| 120 // It is invalid to call Fetch() while a request is already in progress. | 121 // It is invalid to call Fetch() while a request is already in progress. |
| 121 DCHECK(!cur_request_.get()); | 122 DCHECK(!cur_request_.get()); |
| 122 DCHECK(!callback.is_null()); | 123 DCHECK(!callback.is_null()); |
| 123 DCHECK(text); | 124 DCHECK(text); |
| (...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 199 if (IsCertStatusMinorError(ssl_info.cert_status)) { | 200 if (IsCertStatusMinorError(ssl_info.cert_status)) { |
| 200 request->ContinueDespiteLastError(); | 201 request->ContinueDespiteLastError(); |
| 201 return; | 202 return; |
| 202 } | 203 } |
| 203 LOG(WARNING) << "SSL certificate error when fetching PAC script, aborting."; | 204 LOG(WARNING) << "SSL certificate error when fetching PAC script, aborting."; |
| 204 // Certificate errors are in same space as net errors. | 205 // Certificate errors are in same space as net errors. |
| 205 result_code_ = MapCertStatusToNetError(ssl_info.cert_status); | 206 result_code_ = MapCertStatusToNetError(ssl_info.cert_status); |
| 206 request->Cancel(); | 207 request->Cancel(); |
| 207 } | 208 } |
| 208 | 209 |
| 209 void ProxyScriptFetcherImpl::OnResponseStarted(URLRequest* request) { | 210 void ProxyScriptFetcherImpl::OnResponseStarted(URLRequest* request, |
| 211 int net_error) { |
| 210 DCHECK_EQ(request, cur_request_.get()); | 212 DCHECK_EQ(request, cur_request_.get()); |
| 213 DCHECK_NE(ERR_IO_PENDING, net_error); |
| 211 | 214 |
| 212 if (!request->status().is_success()) { | 215 if (net_error != OK) { |
| 213 OnResponseCompleted(request); | 216 OnResponseCompleted(request, net_error); |
| 214 return; | 217 return; |
| 215 } | 218 } |
| 216 | 219 |
| 217 // Require HTTP responses to have a success status code. | 220 // Require HTTP responses to have a success status code. |
| 218 if (request->url().SchemeIsHTTPOrHTTPS()) { | 221 if (request->url().SchemeIsHTTPOrHTTPS()) { |
| 219 // NOTE about status codes: We are like Firefox 3 in this respect. | 222 // NOTE about status codes: We are like Firefox 3 in this respect. |
| 220 // {IE 7, Safari 3, Opera 9.5} do not care about the status code. | 223 // {IE 7, Safari 3, Opera 9.5} do not care about the status code. |
| 221 if (request->GetResponseCode() != 200) { | 224 if (request->GetResponseCode() != 200) { |
| 222 VLOG(1) << "Fetched PAC script had (bad) status line: " | 225 VLOG(1) << "Fetched PAC script had (bad) status line: " |
| 223 << request->response_headers()->GetStatusLine(); | 226 << request->response_headers()->GetStatusLine(); |
| (...skipping 11 matching lines...) Expand all Loading... |
| 235 VLOG(1) << "Fetched PAC script does not have a proper mime type: " | 238 VLOG(1) << "Fetched PAC script does not have a proper mime type: " |
| 236 << mime_type; | 239 << mime_type; |
| 237 } | 240 } |
| 238 } | 241 } |
| 239 | 242 |
| 240 ReadBody(request); | 243 ReadBody(request); |
| 241 } | 244 } |
| 242 | 245 |
| 243 void ProxyScriptFetcherImpl::OnReadCompleted(URLRequest* request, | 246 void ProxyScriptFetcherImpl::OnReadCompleted(URLRequest* request, |
| 244 int num_bytes) { | 247 int num_bytes) { |
| 248 DCHECK_NE(ERR_IO_PENDING, num_bytes); |
| 249 |
| 245 DCHECK_EQ(request, cur_request_.get()); | 250 DCHECK_EQ(request, cur_request_.get()); |
| 246 if (ConsumeBytesRead(request, num_bytes)) { | 251 if (ConsumeBytesRead(request, num_bytes)) { |
| 247 // Keep reading. | 252 // Keep reading. |
| 248 ReadBody(request); | 253 ReadBody(request); |
| 249 } | 254 } |
| 250 } | 255 } |
| 251 | 256 |
| 252 void ProxyScriptFetcherImpl::ReadBody(URLRequest* request) { | 257 void ProxyScriptFetcherImpl::ReadBody(URLRequest* request) { |
| 253 // Read as many bytes as are available synchronously. | 258 // Read as many bytes as are available synchronously. |
| 254 while (true) { | 259 while (true) { |
| 255 int num_bytes; | 260 int num_bytes = request->Read(buf_.get(), kBufSize); |
| 256 if (!request->Read(buf_.get(), kBufSize, &num_bytes)) { | 261 if (num_bytes == ERR_IO_PENDING) |
| 257 // Check whether the read failed synchronously. | 262 return; |
| 258 if (!request->status().is_io_pending()) | 263 |
| 259 OnResponseCompleted(request); | 264 if (num_bytes < 0) { |
| 265 OnResponseCompleted(request, num_bytes); |
| 260 return; | 266 return; |
| 261 } | 267 } |
| 268 |
| 262 if (!ConsumeBytesRead(request, num_bytes)) | 269 if (!ConsumeBytesRead(request, num_bytes)) |
| 263 return; | 270 return; |
| 264 } | 271 } |
| 265 } | 272 } |
| 266 | 273 |
| 267 bool ProxyScriptFetcherImpl::ConsumeBytesRead(URLRequest* request, | 274 bool ProxyScriptFetcherImpl::ConsumeBytesRead(URLRequest* request, |
| 268 int num_bytes) { | 275 int num_bytes) { |
| 269 if (num_bytes <= 0) { | 276 if (num_bytes <= 0) { |
| 270 // Error while reading, or EOF. | 277 // Error while reading, or EOF. |
| 271 OnResponseCompleted(request); | 278 OnResponseCompleted(request, num_bytes); |
| 272 return false; | 279 return false; |
| 273 } | 280 } |
| 274 | 281 |
| 275 // Enforce maximum size bound. | 282 // Enforce maximum size bound. |
| 276 if (num_bytes + bytes_read_so_far_.size() > | 283 if (num_bytes + bytes_read_so_far_.size() > |
| 277 static_cast<size_t>(max_response_bytes_)) { | 284 static_cast<size_t>(max_response_bytes_)) { |
| 278 result_code_ = ERR_FILE_TOO_BIG; | 285 result_code_ = ERR_FILE_TOO_BIG; |
| 279 request->Cancel(); | 286 request->Cancel(); |
| 280 return false; | 287 return false; |
| 281 } | 288 } |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 331 // is still applicable. | 338 // is still applicable. |
| 332 if (cur_request_id_ != id) | 339 if (cur_request_id_ != id) |
| 333 return; | 340 return; |
| 334 | 341 |
| 335 DCHECK(cur_request_.get()); | 342 DCHECK(cur_request_.get()); |
| 336 result_code_ = ERR_TIMED_OUT; | 343 result_code_ = ERR_TIMED_OUT; |
| 337 cur_request_->Cancel(); | 344 cur_request_->Cancel(); |
| 338 } | 345 } |
| 339 | 346 |
| 340 } // namespace net | 347 } // namespace net |
| OLD | NEW |