Skip to content

Truly independent web browser

LibWeb: Proxy the body of the input Request in the Request constructor

Andreas Kling88338f2f099a
diff --git a/Libraries/LibWeb/Fetch/Request.cpp b/Libraries/LibWeb/Fetch/Request.cpp
index 64d56a974e7..9e9c4ebab44 100644
--- a/Libraries/LibWeb/Fetch/Request.cpp
+++ b/Libraries/LibWeb/Fetch/Request.cpp
@@ -15,10 +15,44 @@
#include <LibWeb/Fetch/Infrastructure/HTTP/Requests.h>
#include <LibWeb/Fetch/Request.h>
#include <LibWeb/HTML/Scripting/Environments.h>
+#include <LibWeb/Streams/ReadableStream.h>
+#include <LibWeb/Streams/TransformStream.h>
+#include <LibWeb/WebIDL/Promise.h>
#include <LibWebCommon/ReferrerPolicy/ReferrerPolicy.h>
namespace Web::Fetch {
+// https://streams.spec.whatwg.org/#create-an-identity-transformstream
+static GC::Ref<Streams::TransformStream> create_an_identity_transform_stream(HTML::EnvironmentSettingsObject& settings_object)
+{
+ auto& realm = settings_object.realm();
+
+ // 1. Let transformStream be a new TransformStream.
+ auto transform_stream = Streams::TransformStream::create(realm);
+
+ // 2. Set up transformStream with transformAlgorithm set to an algorithm which, given chunk, enqueues chunk in
+ // transformStream.
+ auto transform_algorithm = GC::create_function(GC::Heap::the(), [&realm, transform_stream](JS::Value chunk) -> GC::Ref<WebIDL::Promise> {
+ if (auto result = transform_stream->enqueue(chunk); result.is_error())
+ return WebIDL::create_rejected_promise_from_exception(realm, result.release_error());
+ return WebIDL::create_resolved_promise(realm, JS::js_undefined());
+ });
+ transform_stream->set_up(realm, transform_algorithm);
+
+ // 3. Return transformStream.
+ return transform_stream;
+}
+
+// https://streams.spec.whatwg.org/#readablestream-create-a-proxy
+static GC::Ref<Streams::ReadableStream> create_a_proxy(HTML::EnvironmentSettingsObject& settings_object, GC::Ref<Streams::ReadableStream> stream)
+{
+ // 1. Let identityTransform be the result of creating an identity TransformStream.
+ auto identity_transform = create_an_identity_transform_stream(settings_object);
+
+ // 2. Return the result of stream piped through identityTransform.
+ return stream->piped_through(identity_transform);
+}
+
static Bindings::ReferrerPolicy referrer_policy_to_bindings(ReferrerPolicy::ReferrerPolicy referrer_policy)
{
return static_cast<Bindings::ReferrerPolicy>(referrer_policy);
@@ -641,20 +675,26 @@ WebIDL::ExceptionOr<GC::Ref<Request>> Request::create_with_settings(HTML::Enviro
}
// 40. Let finalBody be inputOrInitBody.
- auto const& final_body = input_or_init_body;
+ Optional<Infrastructure::Request::BodyType> final_body;
+ if (input_or_init_body.has_value())
+ final_body = *input_or_init_body;
// 41. If initBody is null and inputBody is non-null, then:
if (!init_body.has_value() && input_body.has_value()) {
- // 2. If input is unusable, then throw a TypeError.
+ // 1. If inputBody is unusable, then throw a TypeError.
if (input.has<GC::Ref<Request>>() && input.get<GC::Ref<Request>>()->is_unusable())
return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, "Request is unusable"_utf16 };
- // FIXME: 2. Set finalBody to the result of creating a proxy for inputBody.
+ // 2. Set finalBody to the result of creating a proxy for inputBody.
+ // NB: The proxy is a new body whose stream is the proxy of inputBody's stream. It keeps inputBody's source and
+ // length, so that it can still be transmitted and recreated like inputBody.
+ if (auto const* body = input_body->get_pointer<GC::Ref<Infrastructure::Body>>())
+ final_body = Infrastructure::Body::create(create_a_proxy(relevant_settings_object, (*body)->stream()), (*body)->fetch_body());
}
// 42. Set this’s request’s body to finalBody.
if (final_body.has_value())
- request_object->request()->set_body(*final_body);
+ request_object->request()->set_body(final_body.release_value());
return GC::Ref { *request_object };
}
diff --git a/Tests/LibWeb/Text/expected/wpt-import/fetch/api/request/request-disturbed.any.sharedworker.txt b/Tests/LibWeb/Text/expected/wpt-import/fetch/api/request/request-disturbed.any.sharedworker.txt
index 0ceb6b39d56..0e748a4ad71 100644
--- a/Tests/LibWeb/Text/expected/wpt-import/fetch/api/request/request-disturbed.any.sharedworker.txt
+++ b/Tests/LibWeb/Text/expected/wpt-import/fetch/api/request/request-disturbed.any.sharedworker.txt
@@ -2,14 +2,13 @@ Harness status: OK
Found 9 tests
-7 Pass
-2 Fail
+9 Pass
Pass Request's body: initial state
Pass Request without body cannot be disturbed
Pass Check cloning a disturbed request
Pass Check creating a new request from a disturbed request
Pass Check creating a new request with a new body from a disturbed request
-Fail Input request used for creating new request became disturbed
+Pass Input request used for creating new request became disturbed
Pass Input request used for creating new request with a new body is not disturbed
Pass Check consuming a disturbed request
-Fail Request construction failure should not set "bodyUsed"
\ No newline at end of file
+Pass Request construction failure should not set "bodyUsed"
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/expected/wpt-import/fetch/api/request/request-disturbed.any.txt b/Tests/LibWeb/Text/expected/wpt-import/fetch/api/request/request-disturbed.any.txt
index 0ceb6b39d56..0e748a4ad71 100644
--- a/Tests/LibWeb/Text/expected/wpt-import/fetch/api/request/request-disturbed.any.txt
+++ b/Tests/LibWeb/Text/expected/wpt-import/fetch/api/request/request-disturbed.any.txt
@@ -2,14 +2,13 @@ Harness status: OK
Found 9 tests
-7 Pass
-2 Fail
+9 Pass
Pass Request's body: initial state
Pass Request without body cannot be disturbed
Pass Check cloning a disturbed request
Pass Check creating a new request from a disturbed request
Pass Check creating a new request with a new body from a disturbed request
-Fail Input request used for creating new request became disturbed
+Pass Input request used for creating new request became disturbed
Pass Input request used for creating new request with a new body is not disturbed
Pass Check consuming a disturbed request
-Fail Request construction failure should not set "bodyUsed"
\ No newline at end of file
+Pass Request construction failure should not set "bodyUsed"
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/expected/wpt-import/fetch/api/request/request-disturbed.any.worker.txt b/Tests/LibWeb/Text/expected/wpt-import/fetch/api/request/request-disturbed.any.worker.txt
index 0ceb6b39d56..0e748a4ad71 100644
--- a/Tests/LibWeb/Text/expected/wpt-import/fetch/api/request/request-disturbed.any.worker.txt
+++ b/Tests/LibWeb/Text/expected/wpt-import/fetch/api/request/request-disturbed.any.worker.txt
@@ -2,14 +2,13 @@ Harness status: OK
Found 9 tests
-7 Pass
-2 Fail
+9 Pass
Pass Request's body: initial state
Pass Request without body cannot be disturbed
Pass Check cloning a disturbed request
Pass Check creating a new request from a disturbed request
Pass Check creating a new request with a new body from a disturbed request
-Fail Input request used for creating new request became disturbed
+Pass Input request used for creating new request became disturbed
Pass Input request used for creating new request with a new body is not disturbed
Pass Check consuming a disturbed request
-Fail Request construction failure should not set "bodyUsed"
\ No newline at end of file
+Pass Request construction failure should not set "bodyUsed"
\ No newline at end of file