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