feat: migrate protocol module to NetworkService (Part 6) (#18223)

* fix: start node strem asyncly

* fix: headers value may be a list

* fix: simply destruct on finish/error

* fix: class may destruct immediately after subscribing "data"

* fix: send meaningful error

* fix: must always provide a response body

* fix: handle the case when one write can not write all data

* fix: handle connection error
This commit is contained in:
Cheng Zhao 2019-05-11 15:15:01 +09:00 committed by GitHub
parent 85c24c0b47
commit 326215e1f1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 140 additions and 84 deletions

View file

@ -103,10 +103,21 @@ network::ResourceResponseHead ToResponseHead(const mate::Dictionary& dict) {
base::DictionaryValue headers;
if (dict.Get("headers", &headers)) {
for (const auto& iter : headers.DictItems()) {
head.headers->AddHeader(iter.first + ": " + iter.second.GetString());
if (iter.second.is_string()) {
// key: value
head.headers->AddHeader(iter.first + ": " + iter.second.GetString());
} else if (iter.second.is_list()) {
// key: [values...]
for (const auto& item : iter.second.GetList()) {
if (item.is_string())
head.headers->AddHeader(iter.first + ": " + item.GetString());
}
} else {
continue;
}
// Some apps are passing content-type via headers, which is not accepted
// in NetworkService.
if (iter.first == "content-type")
if (iter.first == "content-type" && iter.second.is_string())
head.mime_type = iter.second.GetString();
}
}
@ -339,7 +350,19 @@ void AtomURLLoaderFactory::StartLoadingStream(
} else if (stream->IsNullOrUndefined()) {
// "data" was explicitly passed as null or undefined, assume the user wants
// to send an empty body.
//
// Note that We must submit a empty body otherwise NetworkService would
// crash.
client->OnReceiveResponse(head);
mojo::ScopedDataPipeProducerHandle producer;
mojo::ScopedDataPipeConsumerHandle consumer;
if (mojo::CreateDataPipe(nullptr, &producer, &consumer) != MOJO_RESULT_OK) {
client->OnComplete(
network::URLLoaderCompletionStatus(net::ERR_INSUFFICIENT_RESOURCES));
return;
}
producer.reset(); // The data pipe is empty.
client->OnStartLoadingResponseBody(std::move(consumer));
client->OnComplete(network::URLLoaderCompletionStatus(net::OK));
return;
} else if (!stream->IsObject()) {