Commit b78c3508 authored by Arthur Sonzogni's avatar Arthur Sonzogni Committed by Commit Bot

MimeSniffingURLLoader: Always forward empty response's body.

This is similar to:
 * https://chromium-review.googlesource.com/c/chromium/src/+/1323109
 * https://chromium-review.googlesource.com/c/chromium/src/+/1329163
 * https://chromium-review.googlesource.com/c/chromium/src/+/1323070
 * https://chromium-review.googlesource.com/c/chromium/src/+/1323092

On request success, it makes the MimeSniffingURLLoader to always send a
response's body datapipe, even when it is empty.

It also adds a DCHECK to ensure the |source_url_loader| is also behaving
the same way.

Bug: 831155,826868
Change-Id: I468a24c31142cb0a8d646cd09928f7b03680d779
Reviewed-on: https://chromium-review.googlesource.com/c/1350874Reviewed-by: default avatarKinuko Yasuda <kinuko@chromium.org>
Commit-Queue: Arthur Sonzogni <arthursonzogni@chromium.org>
Cr-Commit-Position: refs/heads/master@{#612152}
parent 37ad5a5c
......@@ -299,6 +299,33 @@ TEST_F(MimeSniffingThrottleTest, NoBody) {
EXPECT_TRUE(delegate->is_intercepted());
// Call OnComplete() without sending body.
delegate->source_loader_client()->OnComplete(
network::URLLoaderCompletionStatus(net::ERR_FAILED));
delegate->destination_loader_client()->RunUntilComplete();
// The mime type should be updated to the default mime type ("text/plain").
EXPECT_TRUE(delegate->destination_loader_client()->has_received_response());
EXPECT_EQ("text/plain",
delegate->destination_loader_client()->response_head().mime_type);
}
TEST_F(MimeSniffingThrottleTest, EmptyBody) {
auto throttle = std::make_unique<MimeSniffingThrottle>();
auto delegate = std::make_unique<MockDelegate>();
throttle->set_delegate(delegate.get());
GURL response_url("https://example.com");
network::ResourceResponseHead response_head;
bool defer = false;
throttle->WillProcessResponse(response_url, &response_head, &defer);
EXPECT_TRUE(defer);
EXPECT_TRUE(delegate->is_intercepted());
mojo::DataPipe pipe;
delegate->source_loader_client()->OnStartLoadingResponseBody(
std::move(pipe.consumer_handle));
pipe.producer_handle.reset(); // The pipe is empty.
delegate->source_loader_client()->OnComplete(
network::URLLoaderCompletionStatus());
delegate->destination_loader_client()->RunUntilComplete();
......
......@@ -109,9 +109,8 @@ void MimeSniffingURLLoader::OnComplete(
DCHECK(!complete_status_.has_value());
switch (state_) {
case State::kWaitForBody:
// OnComplete() is called without OnStartLoadingResponseBody(). There is
// no response body in this case. Use |kDefaultMimeType| as its mime type
// even though it's empty.
// An error occured before receiving any data.
DCHECK_NE(net::OK, status.error_code);
state_ = State::kCompleted;
response_head_.mime_type = kDefaultMimeType;
if (!throttle_) {
......@@ -231,19 +230,9 @@ void MimeSniffingURLLoader::OnBodyWritable(MojoResult) {
void MimeSniffingURLLoader::CompleteSniffing() {
DCHECK_EQ(State::kSniffing, state_);
if (buffered_body_.empty()) {
// A data pipe for the body was received but no body was provided. Don't
// propagate OnStartLoadingResponseBody() in this case. We treat this
// situation as the same as when OnStartLoadingResponseBody() was not
// called.
//
// TODO(crbug.com/826868): Remove this once all loaders are aligned.
state_ = State::kWaitForBody;
if (complete_status_.has_value()) {
auto status = complete_status_.value();
complete_status_.reset();
OnComplete(status);
}
return;
// The URLLoader ended before sending any data. There is not enough
// informations to determine the MIME type.
response_head_.mime_type = kDefaultMimeType;
}
state_ = State::kSending;
......@@ -272,7 +261,13 @@ void MimeSniffingURLLoader::CompleteSniffing() {
// Call OnComplete() if OnComplete() has already been called.
if (complete_status_.has_value())
destination_url_loader_client_->OnComplete(complete_status_.value());
SendReceivedBodyToClient();
if (bytes_remaining_in_buffer_) {
SendReceivedBodyToClient();
return;
}
CompleteSending();
}
void MimeSniffingURLLoader::CompleteSending() {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment