Truly independent web browser
LibWeb: Read a request’s body stream when fetch() sends its source
sideshowbarkerf1c3a47b8c90
diff --git a/Libraries/LibWeb/Fetch/FetchMethod.cpp b/Libraries/LibWeb/Fetch/FetchMethod.cppindex c02de62d32a..eb9e58409cf 100644--- a/Libraries/LibWeb/Fetch/FetchMethod.cpp+++ b/Libraries/LibWeb/Fetch/FetchMethod.cpp@@ -164,10 +164,9 @@ GC::Ref<WebIDL::Promise> fetch(JS::Realm& realm, RequestInfo const& input, Bindi});Infrastructure::FetchAlgorithms::Input fetch_algorithms_input { Engine::BodyIntent::DeliverStreamToConsumerAgent };fetch_algorithms_input.process_response = process_response;- // AD-HOC: Request holds its body's source and length, and requestObject its stream, which fetching reads for a- // body without a source. Its reader is acquired here, not when fetching reads the stream, so that script- // cannot lock the stream first.- if (auto body = request_object->body_impl(); body && !body->source().has_value())+ // AD-HOC: Request holds its body's source and length, and requestObject its stream, which fetching reads. Its+ // reader is acquired here, not when fetching reads the stream, so that script cannot lock the stream first.+ if (auto body = request_object->body_impl())fetch_algorithms_input.request_body_reader = Infrastructure::FetchAlgorithms::RequestBodyReader { *body, MUST(body->stream()->get_a_reader()) };controller_holder->set_controller(Fetching::fetch(realm, request, Infrastructure::FetchAlgorithms::create(move(fetch_algorithms_input))));diff --git a/Libraries/LibWeb/Fetch/Fetching/Fetching.cpp b/Libraries/LibWeb/Fetch/Fetching/Fetching.cppindex 1fef5807345..7288b59da78 100644--- a/Libraries/LibWeb/Fetch/Fetching/Fetching.cpp+++ b/Libraries/LibWeb/Fetch/Fetching/Fetching.cpp@@ -3129,6 +3129,16 @@ GC::Ref<PendingResponse> nonstandard_resource_loader_file_or_http_network_fetch(HTML::TemporaryExecutionContext const execution_context { realm, HTML::TemporaryExecutionContext::CallbacksEnabled::Yes };request_body_reader->body->incrementally_read_loop(request_body_reader->reader, fetch_params.controller()->task_destination(), process_body_chunk_bytes, process_end_of_body, process_body_error);}++ // AD-HOC: A body with a source is sent from its source, but its stream has to be read all the same: A stream+ // teed from it (that of a clone of the Request, e.g.) can't finish, or be canceled, until it is.+ if (auto const& request_body_reader = fetch_params.controller()->algorithms()->request_body_reader(); source.has_value() && request_body_reader.has_value()) {+ auto ignore_chunk = GC::create_function(GC::Heap::the(), [](ByteBuffer) { });+ auto ignore_end_of_body = GC::create_function(GC::Heap::the(), [] { });+ auto ignore_error = GC::create_function(GC::Heap::the(), [](JS::Value) { });+ HTML::TemporaryExecutionContext const execution_context { realm, HTML::TemporaryExecutionContext::CallbacksEnabled::Yes };+ request_body_reader->body->incrementally_read_loop(request_body_reader->reader, fetch_params.controller()->task_destination(), ignore_chunk, ignore_end_of_body, ignore_error);+ }}return pending_response;diff --git a/Tests/LibWeb/Text/expected/Fetch/fetch-reads-request-body-stream.txt b/Tests/LibWeb/Text/expected/Fetch/fetch-reads-request-body-stream.txtnew file mode 100644index 00000000000..ea33eaa5abd--- /dev/null+++ b/Tests/LibWeb/Text/expected/Fetch/fetch-reads-request-body-stream.txt@@ -0,0 +1,4 @@+response: ok+request bodyUsed: true+canceling the clone's body: resolved+clone's body: the bodydiff --git a/Tests/LibWeb/Text/input/Fetch/fetch-reads-request-body-stream.html b/Tests/LibWeb/Text/input/Fetch/fetch-reads-request-body-stream.htmlnew file mode 100644index 00000000000..3233424cadd--- /dev/null+++ b/Tests/LibWeb/Text/input/Fetch/fetch-reads-request-body-stream.html@@ -0,0 +1,25 @@+<!DOCTYPE html>+<script src="../include.js"></script>+<script>+ asyncTest(async done => {+ const url = await httpTestServer().createEcho("POST", "/fetch-reads-request-body-stream", {+ status: 200,+ headers: { "Access-Control-Allow-Origin": "*" },+ body: "ok",+ });+ const timeout = () => new Promise(resolve => setTimeout(() => resolve("still pending"), 2000));++ const request = new Request(url, { method: "POST", body: "the body" });+ const clone = request.clone();+ const response = await fetch(request);+ println(`response: ${await response.text()}`);+ println(`request bodyUsed: ${request.bodyUsed}`);+ println(`canceling the clone's body: ${await Promise.race([clone.body.cancel().then(() => "resolved"), timeout()])}`);++ const unread = new Request(url, { method: "POST", body: "the body" });+ const unreadClone = unread.clone();+ await (await fetch(unread)).text();+ println(`clone's body: ${await Promise.race([unreadClone.text(), timeout()])}`);+ done();+ });+</script>