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