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/url_request/url_fetcher_core.h" | 5 #include "net/url_request/url_fetcher_core.h" |
6 | 6 |
7 #include <stdint.h> | 7 #include <stdint.h> |
8 #include <utility> | 8 #include <utility> |
9 | 9 |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
(...skipping 389 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
400 bool* defer_redirect) { | 400 bool* defer_redirect) { |
401 DCHECK_EQ(request, request_.get()); | 401 DCHECK_EQ(request, request_.get()); |
402 DCHECK(network_task_runner_->BelongsToCurrentThread()); | 402 DCHECK(network_task_runner_->BelongsToCurrentThread()); |
403 if (stop_on_redirect_) { | 403 if (stop_on_redirect_) { |
404 stopped_on_redirect_ = true; | 404 stopped_on_redirect_ = true; |
405 url_ = redirect_info.new_url; | 405 url_ = redirect_info.new_url; |
406 response_code_ = request_->GetResponseCode(); | 406 response_code_ = request_->GetResponseCode(); |
407 was_fetched_via_proxy_ = request_->was_fetched_via_proxy(); | 407 was_fetched_via_proxy_ = request_->was_fetched_via_proxy(); |
408 was_cached_ = request_->was_cached(); | 408 was_cached_ = request_->was_cached(); |
409 total_received_bytes_ += request_->GetTotalReceivedBytes(); | 409 total_received_bytes_ += request_->GetTotalReceivedBytes(); |
410 request->Cancel(); | 410 int result = request->Cancel(); |
411 OnReadCompleted(request, 0); | 411 OnReadCompleted(request, result); |
412 } | 412 } |
413 } | 413 } |
414 | 414 |
415 void URLFetcherCore::OnResponseStarted(URLRequest* request) { | 415 void URLFetcherCore::OnResponseStarted(URLRequest* request, int net_error) { |
416 DCHECK_EQ(request, request_.get()); | 416 DCHECK_EQ(request, request_.get()); |
417 DCHECK(network_task_runner_->BelongsToCurrentThread()); | 417 DCHECK(network_task_runner_->BelongsToCurrentThread()); |
418 if (request_->status().is_success()) { | 418 DCHECK_NE(ERR_IO_PENDING, net_error); |
| 419 |
| 420 if (net_error == OK) { |
419 response_code_ = request_->GetResponseCode(); | 421 response_code_ = request_->GetResponseCode(); |
420 response_headers_ = request_->response_headers(); | 422 response_headers_ = request_->response_headers(); |
421 socket_address_ = request_->GetSocketAddress(); | 423 socket_address_ = request_->GetSocketAddress(); |
422 was_fetched_via_proxy_ = request_->was_fetched_via_proxy(); | 424 was_fetched_via_proxy_ = request_->was_fetched_via_proxy(); |
423 was_cached_ = request_->was_cached(); | 425 was_cached_ = request_->was_cached(); |
424 total_response_bytes_ = request_->GetExpectedContentSize(); | 426 total_response_bytes_ = request_->GetExpectedContentSize(); |
425 } | 427 } |
426 | 428 |
427 ReadResponse(); | 429 ReadResponse(); |
428 } | 430 } |
(...skipping 17 matching lines...) Expand all Loading... |
446 DCHECK(network_task_runner_->BelongsToCurrentThread()); | 448 DCHECK(network_task_runner_->BelongsToCurrentThread()); |
447 | 449 |
448 if (!stopped_on_redirect_) | 450 if (!stopped_on_redirect_) |
449 url_ = request->url(); | 451 url_ = request->url(); |
450 URLRequestThrottlerManager* throttler_manager = | 452 URLRequestThrottlerManager* throttler_manager = |
451 request->context()->throttler_manager(); | 453 request->context()->throttler_manager(); |
452 if (throttler_manager) | 454 if (throttler_manager) |
453 url_throttler_entry_ = throttler_manager->RegisterRequestUrl(url_); | 455 url_throttler_entry_ = throttler_manager->RegisterRequestUrl(url_); |
454 | 456 |
455 do { | 457 do { |
456 if (!request_->status().is_success() || bytes_read <= 0) | 458 if (bytes_read <= 0) |
457 break; | 459 break; |
458 | 460 |
459 current_response_bytes_ += bytes_read; | 461 current_response_bytes_ += bytes_read; |
460 InformDelegateDownloadProgress(); | 462 InformDelegateDownloadProgress(); |
461 | 463 |
462 const int result = | 464 const int result = |
463 WriteBuffer(new DrainableIOBuffer(buffer_.get(), bytes_read)); | 465 WriteBuffer(new DrainableIOBuffer(buffer_.get(), bytes_read)); |
464 if (result < 0) { | 466 if (result < 0) { |
465 // Write failed or waiting for write completion. | 467 // Write failed or waiting for write completion. |
466 return; | 468 return; |
467 } | 469 } |
468 } while (request_->Read(buffer_.get(), kBufferSize, &bytes_read)); | 470 bytes_read = request_->Read(buffer_.get(), kBufferSize); |
469 | 471 } while (bytes_read > 0); |
470 const URLRequestStatus status = request_->status(); | |
471 | 472 |
472 // See comments re: HEAD requests in ReadResponse(). | 473 // See comments re: HEAD requests in ReadResponse(). |
473 if (!status.is_io_pending() || request_type_ == URLFetcher::HEAD) { | 474 if (bytes_read != ERR_IO_PENDING || request_type_ == URLFetcher::HEAD) { |
474 status_ = status; | 475 status_ = URLRequestStatus::FromError(bytes_read); |
475 received_response_content_length_ = | 476 received_response_content_length_ = |
476 request_->received_response_content_length(); | 477 request_->received_response_content_length(); |
477 total_received_bytes_ += request_->GetTotalReceivedBytes(); | 478 total_received_bytes_ += request_->GetTotalReceivedBytes(); |
478 ReleaseRequest(); | 479 ReleaseRequest(); |
479 | 480 |
480 // No more data to write. | 481 // No more data to write. |
481 const int result = response_writer_->Finish( | 482 const int result = response_writer_->Finish( |
482 base::Bind(&URLFetcherCore::DidFinishWriting, this)); | 483 base::Bind(&URLFetcherCore::DidFinishWriting, this)); |
483 if (result != ERR_IO_PENDING) | 484 if (result != ERR_IO_PENDING) |
484 DidFinishWriting(result); | 485 DidFinishWriting(result); |
(...skipping 394 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
879 if (request_.get()) | 880 if (request_.get()) |
880 ReadResponse(); | 881 ReadResponse(); |
881 } | 882 } |
882 | 883 |
883 void URLFetcherCore::ReadResponse() { | 884 void URLFetcherCore::ReadResponse() { |
884 // Some servers may treat HEAD requests as GET requests. To free up the | 885 // Some servers may treat HEAD requests as GET requests. To free up the |
885 // network connection as soon as possible, signal that the request has | 886 // network connection as soon as possible, signal that the request has |
886 // completed immediately, without trying to read any data back (all we care | 887 // completed immediately, without trying to read any data back (all we care |
887 // about is the response code and headers, which we already have). | 888 // about is the response code and headers, which we already have). |
888 int bytes_read = 0; | 889 int bytes_read = 0; |
889 if (request_->status().is_success() && | 890 if (request_type_ != URLFetcher::HEAD) |
890 (request_type_ != URLFetcher::HEAD)) { | 891 bytes_read = request_->Read(buffer_.get(), kBufferSize); |
891 if (!request_->Read(buffer_.get(), kBufferSize, &bytes_read)) | 892 |
892 bytes_read = -1; // Match OnReadCompleted() interface contract. | |
893 } | |
894 OnReadCompleted(request_.get(), bytes_read); | 893 OnReadCompleted(request_.get(), bytes_read); |
895 } | 894 } |
896 | 895 |
897 void URLFetcherCore::InformDelegateUploadProgress() { | 896 void URLFetcherCore::InformDelegateUploadProgress() { |
898 DCHECK(network_task_runner_->BelongsToCurrentThread()); | 897 DCHECK(network_task_runner_->BelongsToCurrentThread()); |
899 if (request_.get()) { | 898 if (request_.get()) { |
900 int64_t current = request_->GetUploadProgress().position(); | 899 int64_t current = request_->GetUploadProgress().position(); |
901 if (current_upload_bytes_ != current) { | 900 if (current_upload_bytes_ != current) { |
902 current_upload_bytes_ = current; | 901 current_upload_bytes_ = current; |
903 int64_t total = -1; | 902 int64_t total = -1; |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
949 } | 948 } |
950 | 949 |
951 void URLFetcherCore::AssertHasNoUploadData() const { | 950 void URLFetcherCore::AssertHasNoUploadData() const { |
952 DCHECK(!upload_content_set_); | 951 DCHECK(!upload_content_set_); |
953 DCHECK(upload_content_.empty()); | 952 DCHECK(upload_content_.empty()); |
954 DCHECK(upload_file_path_.empty()); | 953 DCHECK(upload_file_path_.empty()); |
955 DCHECK(upload_stream_factory_.is_null()); | 954 DCHECK(upload_stream_factory_.is_null()); |
956 } | 955 } |
957 | 956 |
958 } // namespace net | 957 } // namespace net |
OLD | NEW |