OLD | NEW |
---|---|
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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/cert_net/cert_net_fetcher_impl.h" | 5 #include "net/cert_net/cert_net_fetcher_impl.h" |
6 | 6 |
7 #include <tuple> | 7 #include <tuple> |
8 #include <utility> | 8 #include <utility> |
9 | 9 |
10 #include "base/callback_helpers.h" | 10 #include "base/callback_helpers.h" |
(...skipping 160 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
171 void StartURLRequest(URLRequestContext* context); | 171 void StartURLRequest(URLRequestContext* context); |
172 | 172 |
173 private: | 173 private: |
174 // The pointers in RequestList are not owned by the Job. | 174 // The pointers in RequestList are not owned by the Job. |
175 using RequestList = base::LinkedList<RequestImpl>; | 175 using RequestList = base::LinkedList<RequestImpl>; |
176 | 176 |
177 // Implementation of URLRequest::Delegate | 177 // Implementation of URLRequest::Delegate |
178 void OnReceivedRedirect(URLRequest* request, | 178 void OnReceivedRedirect(URLRequest* request, |
179 const RedirectInfo& redirect_info, | 179 const RedirectInfo& redirect_info, |
180 bool* defer_redirect) override; | 180 bool* defer_redirect) override; |
181 void OnResponseStarted(URLRequest* request) override; | 181 void OnResponseStarted(URLRequest* request, int net_error) override; |
182 void OnReadCompleted(URLRequest* request, int bytes_read) override; | 182 void OnReadCompleted(URLRequest* request, int bytes_read) override; |
183 | 183 |
184 // Clears the URLRequest and timer. Helper for doing work common to | 184 // Clears the URLRequest and timer. Helper for doing work common to |
185 // cancellation and job completion. | 185 // cancellation and job completion. |
186 void Stop(); | 186 void Stop(); |
187 | 187 |
188 // Reads as much data as available from |request|. | 188 // Reads as much data as available from |request|. |
189 void ReadBody(URLRequest* request); | 189 void ReadBody(URLRequest* request); |
190 | 190 |
191 // Helper to copy the partial bytes read from the read IOBuffer to an | 191 // Helper to copy the partial bytes read from the read IOBuffer to an |
192 // aggregated buffer. | 192 // aggregated buffer. |
193 bool ConsumeBytesRead(URLRequest* request, int num_bytes); | 193 bool ConsumeBytesRead(URLRequest* request, int num_bytes); |
194 | 194 |
195 // Called once the job has exceeded its deadline. | 195 // Called once the job has exceeded its deadline. |
196 void OnTimeout(); | 196 void OnTimeout(); |
197 | 197 |
198 // Called when the URLRequest has completed (either success or failure). | 198 // Called when the URLRequest has completed (either success or failure). |
199 void OnUrlRequestCompleted(URLRequest* request); | 199 void OnUrlRequestCompleted(URLRequest* request, int net_error); |
200 | 200 |
201 // Called when the Job has completed. The job may finish in response to a | 201 // Called when the Job has completed. The job may finish in response to a |
202 // timeout, an invalid URL, or the URLRequest completing. By the time this | 202 // timeout, an invalid URL, or the URLRequest completing. By the time this |
203 // method is called, the response variables have been assigned | 203 // method is called, the response variables have been assigned |
204 // (result_net_error_ and response_body_). | 204 // (result_net_error_ and response_body_). |
205 void OnJobCompleted(); | 205 void OnJobCompleted(); |
206 | 206 |
207 // The requests attached to this job. | 207 // The requests attached to this job. |
208 RequestList requests_; | 208 RequestList requests_; |
209 | 209 |
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
304 | 304 |
305 void CertNetFetcherImpl::Job::OnReceivedRedirect( | 305 void CertNetFetcherImpl::Job::OnReceivedRedirect( |
306 URLRequest* request, | 306 URLRequest* request, |
307 const RedirectInfo& redirect_info, | 307 const RedirectInfo& redirect_info, |
308 bool* defer_redirect) { | 308 bool* defer_redirect) { |
309 DCHECK_EQ(url_request_.get(), request); | 309 DCHECK_EQ(url_request_.get(), request); |
310 | 310 |
311 // Ensure that the new URL matches the policy. | 311 // Ensure that the new URL matches the policy. |
312 Error error = CanFetchUrl(redirect_info.new_url); | 312 Error error = CanFetchUrl(redirect_info.new_url); |
313 if (error != OK) { | 313 if (error != OK) { |
314 request->CancelWithError(error); | 314 int status = request->CancelWithError(error); |
mmenke
2016/08/30 22:13:22
Can we call these results, instead? net:Errors ar
maksims (do not use this acc)
2016/09/01 12:22:29
Done.
| |
315 OnUrlRequestCompleted(request); | 315 OnUrlRequestCompleted(request, status); |
316 return; | 316 return; |
317 } | 317 } |
318 } | 318 } |
319 | 319 |
320 void CertNetFetcherImpl::Job::OnResponseStarted(URLRequest* request) { | 320 void CertNetFetcherImpl::Job::OnResponseStarted(URLRequest* request, |
321 int net_error) { | |
321 DCHECK_EQ(url_request_.get(), request); | 322 DCHECK_EQ(url_request_.get(), request); |
323 DCHECK_NE(ERR_IO_PENDING, net_error); | |
322 | 324 |
323 if (!request->status().is_success()) { | 325 if (net_error != OK) { |
324 OnUrlRequestCompleted(request); | 326 OnUrlRequestCompleted(request, net_error); |
325 return; | 327 return; |
326 } | 328 } |
327 | 329 |
328 if (request->GetResponseCode() != 200) { | 330 if (request->GetResponseCode() != 200) { |
329 // TODO(eroman): Use a more specific error code. | 331 // TODO(eroman): Use a more specific error code. |
330 request->CancelWithError(ERR_FAILED); | 332 int status = request->CancelWithError(ERR_FAILED); |
331 OnUrlRequestCompleted(request); | 333 OnUrlRequestCompleted(request, status); |
332 return; | 334 return; |
333 } | 335 } |
334 | 336 |
335 ReadBody(request); | 337 ReadBody(request); |
336 } | 338 } |
337 | 339 |
338 void CertNetFetcherImpl::Job::OnReadCompleted(URLRequest* request, | 340 void CertNetFetcherImpl::Job::OnReadCompleted(URLRequest* request, |
339 int bytes_read) { | 341 int bytes_read) { |
340 DCHECK_EQ(url_request_.get(), request); | 342 DCHECK_EQ(url_request_.get(), request); |
343 DCHECK_NE(ERR_IO_PENDING, bytes_read); | |
341 | 344 |
342 // Keep reading the response body. | 345 // Keep reading the response body. |
343 if (ConsumeBytesRead(request, bytes_read)) | 346 if (ConsumeBytesRead(request, bytes_read)) |
344 ReadBody(request); | 347 ReadBody(request); |
345 } | 348 } |
346 | 349 |
347 void CertNetFetcherImpl::Job::Stop() { | 350 void CertNetFetcherImpl::Job::Stop() { |
348 timer_.Stop(); | 351 timer_.Stop(); |
349 url_request_.reset(); | 352 url_request_.reset(); |
350 } | 353 } |
351 | 354 |
352 void CertNetFetcherImpl::Job::ReadBody(URLRequest* request) { | 355 void CertNetFetcherImpl::Job::ReadBody(URLRequest* request) { |
353 // Read as many bytes as are available synchronously. | 356 // Read as many bytes as are available synchronously. |
354 int num_bytes; | 357 int num_bytes = 0; |
355 while ( | 358 while (num_bytes >= 0) { |
356 request->Read(read_buffer_.get(), kReadBufferSizeInBytes, &num_bytes)) { | 359 num_bytes = request->Read(read_buffer_.get(), kReadBufferSizeInBytes); |
357 if (!ConsumeBytesRead(request, num_bytes)) | 360 if (!ConsumeBytesRead(request, num_bytes)) |
358 return; | 361 return; |
359 } | 362 } |
360 | 363 |
361 // Check whether the read failed synchronously. | 364 // Check whether the read failed synchronously. |
362 if (!request->status().is_io_pending()) | 365 if (num_bytes != ERR_IO_PENDING) |
363 OnUrlRequestCompleted(request); | 366 OnUrlRequestCompleted(request, num_bytes); |
364 return; | 367 return; |
365 } | 368 } |
366 | 369 |
367 bool CertNetFetcherImpl::Job::ConsumeBytesRead(URLRequest* request, | 370 bool CertNetFetcherImpl::Job::ConsumeBytesRead(URLRequest* request, |
368 int num_bytes) { | 371 int num_bytes) { |
369 if (num_bytes <= 0) { | 372 if (num_bytes <= 0) { |
370 // Error while reading, or EOF. | 373 // Error while reading, or EOF. |
371 OnUrlRequestCompleted(request); | 374 OnUrlRequestCompleted(request, num_bytes); |
372 return false; | 375 return false; |
373 } | 376 } |
374 | 377 |
375 // Enforce maximum size bound. | 378 // Enforce maximum size bound. |
376 if (num_bytes + response_body_.size() > request_params_->max_response_bytes) { | 379 if (num_bytes + response_body_.size() > request_params_->max_response_bytes) { |
377 request->CancelWithError(ERR_FILE_TOO_BIG); | 380 int status = request->CancelWithError(ERR_FILE_TOO_BIG); |
378 OnUrlRequestCompleted(request); | 381 OnUrlRequestCompleted(request, status); |
379 return false; | 382 return false; |
380 } | 383 } |
381 | 384 |
382 // Append the data to |response_body_|. | 385 // Append the data to |response_body_|. |
383 response_body_.reserve(num_bytes); | 386 response_body_.reserve(num_bytes); |
384 response_body_.insert(response_body_.end(), read_buffer_->data(), | 387 response_body_.insert(response_body_.end(), read_buffer_->data(), |
385 read_buffer_->data() + num_bytes); | 388 read_buffer_->data() + num_bytes); |
386 return true; | 389 return true; |
387 } | 390 } |
388 | 391 |
389 void CertNetFetcherImpl::Job::OnTimeout() { | 392 void CertNetFetcherImpl::Job::OnTimeout() { |
mmenke
2016/08/30 22:13:22
Suggestion: Make this FailRequest(net::Error erro
maksims (do not use this acc)
2016/09/01 12:22:29
Done.
| |
390 result_net_error_ = ERR_TIMED_OUT; | 393 result_net_error_ = ERR_TIMED_OUT; |
391 url_request_->CancelWithError(result_net_error_); | 394 url_request_->CancelWithError(result_net_error_); |
392 OnJobCompleted(); | 395 OnJobCompleted(); |
393 } | 396 } |
394 | 397 |
395 void CertNetFetcherImpl::Job::OnUrlRequestCompleted(URLRequest* request) { | 398 void CertNetFetcherImpl::Job::OnUrlRequestCompleted(URLRequest* request, |
399 int net_error) { | |
396 DCHECK_EQ(request, url_request_.get()); | 400 DCHECK_EQ(request, url_request_.get()); |
397 | 401 |
398 if (request->status().is_success()) | 402 if (net_error == OK) |
399 result_net_error_ = OK; | 403 result_net_error_ = OK; |
400 else | 404 else |
401 result_net_error_ = static_cast<Error>(request->status().error()); | 405 result_net_error_ = static_cast<Error>(net_error); |
mmenke
2016/08/30 22:13:22
This whole thing is just:
result_net_error_ = sta
maksims (do not use this acc)
2016/09/01 12:22:29
Done.
| |
402 | 406 |
403 OnJobCompleted(); | 407 OnJobCompleted(); |
404 } | 408 } |
405 | 409 |
406 void CertNetFetcherImpl::Job::OnJobCompleted() { | 410 void CertNetFetcherImpl::Job::OnJobCompleted() { |
mmenke
2016/08/30 22:13:22
Suggestion: Make this OnJobCompleted(net::Error),
maksims (do not use this acc)
2016/09/01 12:22:29
Yes, you're right. Done!
| |
407 // Stop the timer and clear the URLRequest. | 411 // Stop the timer and clear the URLRequest. |
408 Stop(); | 412 Stop(); |
409 | 413 |
410 // Invoking the callbacks is subtle as state may be mutated while iterating | 414 // Invoking the callbacks is subtle as state may be mutated while iterating |
411 // through the callbacks: | 415 // through the callbacks: |
412 // | 416 // |
413 // * The parent CertNetFetcherImpl may be deleted | 417 // * The parent CertNetFetcherImpl may be deleted |
414 // * Requests in this job may be cancelled | 418 // * Requests in this job may be cancelled |
415 | 419 |
416 std::unique_ptr<Job> delete_this = parent_->RemoveJob(this); | 420 std::unique_ptr<Job> delete_this = parent_->RemoveJob(this); |
(...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
547 void CertNetFetcherImpl::ClearCurrentlyCompletingJob(Job* job) { | 551 void CertNetFetcherImpl::ClearCurrentlyCompletingJob(Job* job) { |
548 DCHECK_EQ(currently_completing_job_, job); | 552 DCHECK_EQ(currently_completing_job_, job); |
549 currently_completing_job_ = nullptr; | 553 currently_completing_job_ = nullptr; |
550 } | 554 } |
551 | 555 |
552 bool CertNetFetcherImpl::IsCurrentlyCompletingJob(Job* job) { | 556 bool CertNetFetcherImpl::IsCurrentlyCompletingJob(Job* job) { |
553 return job == currently_completing_job_; | 557 return job == currently_completing_job_; |
554 } | 558 } |
555 | 559 |
556 } // namespace net | 560 } // namespace net |
OLD | NEW |