FetchMethod.cpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /*
  2. * Copyright (c) 2022-2023, Linus Groh <linusg@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/Debug.h>
  7. #include <AK/TypeCasts.h>
  8. #include <LibJS/Runtime/PromiseCapability.h>
  9. #include <LibWeb/Bindings/ExceptionOrUtils.h>
  10. #include <LibWeb/Bindings/HostDefined.h>
  11. #include <LibWeb/DOM/AbortSignal.h>
  12. #include <LibWeb/Fetch/FetchMethod.h>
  13. #include <LibWeb/Fetch/Fetching/Fetching.h>
  14. #include <LibWeb/Fetch/Fetching/RefCountedFlag.h>
  15. #include <LibWeb/Fetch/Infrastructure/FetchAlgorithms.h>
  16. #include <LibWeb/Fetch/Infrastructure/FetchController.h>
  17. #include <LibWeb/Fetch/Infrastructure/HTTP/Requests.h>
  18. #include <LibWeb/Fetch/Infrastructure/HTTP/Responses.h>
  19. #include <LibWeb/Fetch/Request.h>
  20. #include <LibWeb/Fetch/Response.h>
  21. #include <LibWeb/WebIDL/ExceptionOr.h>
  22. #include <LibWeb/WebIDL/Promise.h>
  23. namespace Web::Fetch {
  24. // https://fetch.spec.whatwg.org/#dom-global-fetch
  25. JS::NonnullGCPtr<JS::Promise> fetch(JS::VM& vm, RequestInfo const& input, RequestInit const& init)
  26. {
  27. auto& realm = *vm.current_realm();
  28. // 1. Let p be a new promise.
  29. auto promise_capability = WebIDL::create_promise(realm);
  30. // 2. Let requestObject be the result of invoking the initial value of Request as constructor with input and init
  31. // as arguments. If this throws an exception, reject p with it and return p.
  32. auto exception_or_request_object = Request::construct_impl(realm, input, init);
  33. if (exception_or_request_object.is_exception()) {
  34. auto throw_completion = Bindings::dom_exception_to_throw_completion(vm, exception_or_request_object.exception());
  35. WebIDL::reject_promise(realm, promise_capability, *throw_completion.value());
  36. return verify_cast<JS::Promise>(*promise_capability->promise().ptr());
  37. }
  38. auto request_object = exception_or_request_object.release_value();
  39. // 3. Let request be requestObject’s request.
  40. auto request = request_object->request();
  41. // 4. If requestObject’s signal is aborted, then:
  42. if (request_object->signal()->aborted()) {
  43. // 1. Abort the fetch() call with p, request, null, and requestObject’s signal’s abort reason.
  44. abort_fetch(realm, promise_capability, request, nullptr, request_object->signal()->reason());
  45. // 2. Return p.
  46. return verify_cast<JS::Promise>(*promise_capability->promise().ptr());
  47. }
  48. // 5. Let globalObject be request’s client’s global object.
  49. auto& global_object = request->client()->global_object();
  50. // FIXME: 6. If globalObject is a ServiceWorkerGlobalScope object, then set request’s service-workers mode to "none".
  51. (void)global_object;
  52. // 7. Let responseObject be null.
  53. JS::Handle<Response> response_object_handle;
  54. // 8. Let relevantRealm be this’s relevant Realm.
  55. // NOTE: This assumes that the running execution context is for the fetch() function call.
  56. auto& relevant_realm = HTML::relevant_realm(*vm.running_execution_context().function);
  57. // 9. Let locallyAborted be false.
  58. // NOTE: This lets us reject promises with predictable timing, when the request to abort comes from the same thread
  59. // as the call to fetch.
  60. auto locally_aborted = Fetching::RefCountedFlag::create(false);
  61. // 10. Let controller be null.
  62. JS::GCPtr<Infrastructure::FetchController> controller;
  63. // NOTE: Step 11 is done out of order so that the controller is non-null when we capture the GCPtr by copy in the abort algorithm lambda.
  64. // This is not observable, AFAICT.
  65. // 12. Set controller to the result of calling fetch given request and processResponse given response being these
  66. // steps:
  67. auto process_response = [locally_aborted, promise_capability, request, response_object_handle, &relevant_realm](JS::NonnullGCPtr<Infrastructure::Response> response) mutable {
  68. // 1. If locallyAborted is true, then abort these steps.
  69. if (locally_aborted->value())
  70. return;
  71. // NOTE: Not part of the spec, but we need to have an execution context on the stack to call native functions.
  72. // (In this case, Promise functions)
  73. auto& environment_settings_object = Bindings::host_defined_environment_settings_object(relevant_realm);
  74. environment_settings_object.prepare_to_run_script();
  75. ScopeGuard guard = [&]() {
  76. // See above NOTE.
  77. environment_settings_object.clean_up_after_running_script();
  78. };
  79. // 2. If response’s aborted flag is set, then:
  80. if (response->aborted()) {
  81. // FIXME: 1. Let deserializedError be the result of deserialize a serialized abort reason given controller’s
  82. // serialized abort reason and relevantRealm.
  83. auto deserialized_error = JS::js_undefined();
  84. // 2. Abort the fetch() call with p, request, responseObject, and deserializedError.
  85. abort_fetch(relevant_realm, promise_capability, request, response_object_handle.cell(), deserialized_error);
  86. // 3. Abort these steps.
  87. return;
  88. }
  89. // 3. If response is a network error, then reject p with a TypeError and abort these steps.
  90. if (response->is_network_error()) {
  91. auto message = response->network_error_message().value_or("Response is a network error"sv);
  92. WebIDL::reject_promise(relevant_realm, promise_capability, JS::TypeError::create(relevant_realm, message).release_allocated_value_but_fixme_should_propagate_errors());
  93. return;
  94. }
  95. // 4. Set responseObject to the result of creating a Response object, given response, "immutable", and
  96. // relevantRealm.
  97. auto response_object = Response::create(relevant_realm, response, Headers::Guard::Immutable).release_value_but_fixme_should_propagate_errors();
  98. response_object_handle = JS::make_handle(response_object);
  99. // 5. Resolve p with responseObject.
  100. WebIDL::resolve_promise(relevant_realm, promise_capability, response_object);
  101. };
  102. controller = MUST(Fetching::fetch(
  103. realm,
  104. request,
  105. Infrastructure::FetchAlgorithms::create(vm,
  106. {
  107. .process_request_body_chunk_length = {},
  108. .process_request_end_of_body = {},
  109. .process_early_hints_response = {},
  110. .process_response = move(process_response),
  111. .process_response_end_of_body = {},
  112. .process_response_consume_body = {},
  113. })));
  114. // 11. Add the following abort steps to requestObject’s signal:
  115. request_object->signal()->add_abort_algorithm([locally_aborted, request, controller, promise_capability_handle = JS::make_handle(*promise_capability), request_object_handle = JS::make_handle(*request_object), response_object_handle, &relevant_realm] {
  116. dbgln_if(WEB_FETCH_DEBUG, "Fetch: Request object signal's abort algorithm called");
  117. auto& promise_capability = *promise_capability_handle;
  118. auto& request_object = *request_object_handle;
  119. JS::GCPtr<Response> response_object = response_object_handle.ptr();
  120. // 1. Set locallyAborted to true.
  121. locally_aborted->set_value(true);
  122. // 2. Assert: controller is non-null.
  123. VERIFY(controller);
  124. // 3. Abort controller with requestObject’s signal’s abort reason.
  125. controller->abort(relevant_realm, request_object.signal()->reason());
  126. // 4. Abort the fetch() call with p, request, responseObject, and requestObject’s signal’s abort reason.
  127. abort_fetch(relevant_realm, promise_capability, request, response_object, request_object.signal()->reason());
  128. });
  129. // 13. Return p.
  130. return verify_cast<JS::Promise>(*promise_capability->promise().ptr());
  131. }
  132. // https://fetch.spec.whatwg.org/#abort-fetch
  133. void abort_fetch(JS::Realm& realm, WebIDL::Promise const& promise, JS::NonnullGCPtr<Infrastructure::Request> request, JS::GCPtr<Response> response_object, JS::Value error)
  134. {
  135. dbgln_if(WEB_FETCH_DEBUG, "Fetch: Aborting fetch with: request @ {}, error = {}", request.ptr(), error);
  136. // 1. Reject promise with error.
  137. // NOTE: This is a no-op if promise has already fulfilled.
  138. WebIDL::reject_promise(realm, promise, error);
  139. // 2. If request’s body is non-null and is readable, then cancel request’s body with error.
  140. if (auto* body = request->body().get_pointer<Infrastructure::Body>(); body != nullptr && body->stream()->is_readable()) {
  141. // TODO: Implement cancelling streams
  142. (void)error;
  143. }
  144. // 3. If responseObject is null, then return.
  145. if (response_object == nullptr)
  146. return;
  147. // 4. Let response be responseObject’s response.
  148. auto response = response_object->response();
  149. // 5. If response’s body is non-null and is readable, then error response’s body with error.
  150. if (response->body().has_value()) {
  151. auto stream = response->body()->stream();
  152. if (stream->is_readable()) {
  153. // TODO: Implement erroring streams
  154. (void)error;
  155. }
  156. }
  157. }
  158. }