FetchMethod.cpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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/HTML/Scripting/TemporaryExecutionContext.h>
  22. #include <LibWeb/WebIDL/ExceptionOr.h>
  23. #include <LibWeb/WebIDL/Promise.h>
  24. namespace Web::Fetch {
  25. // https://fetch.spec.whatwg.org/#dom-global-fetch
  26. JS::NonnullGCPtr<WebIDL::Promise> fetch(JS::VM& vm, RequestInfo const& input, RequestInit const& init)
  27. {
  28. auto& realm = *vm.current_realm();
  29. // 1. Let p be a new promise.
  30. auto promise_capability = WebIDL::create_promise(realm);
  31. // 2. Let requestObject be the result of invoking the initial value of Request as constructor with input and init
  32. // as arguments. If this throws an exception, reject p with it and return p.
  33. auto exception_or_request_object = Request::construct_impl(realm, input, init);
  34. if (exception_or_request_object.is_exception()) {
  35. auto throw_completion = Bindings::dom_exception_to_throw_completion(vm, exception_or_request_object.exception());
  36. WebIDL::reject_promise(realm, promise_capability, *throw_completion.value());
  37. return promise_capability;
  38. }
  39. auto request_object = exception_or_request_object.release_value();
  40. // 3. Let request be requestObject’s request.
  41. auto request = request_object->request();
  42. // 4. If requestObject’s signal is aborted, then:
  43. if (request_object->signal()->aborted()) {
  44. // 1. Abort the fetch() call with p, request, null, and requestObject’s signal’s abort reason.
  45. abort_fetch(realm, promise_capability, request, nullptr, request_object->signal()->reason());
  46. // 2. Return p.
  47. return promise_capability;
  48. }
  49. // 5. Let globalObject be request’s client’s global object.
  50. auto& global_object = request->client()->global_object();
  51. // FIXME: 6. If globalObject is a ServiceWorkerGlobalScope object, then set request’s service-workers mode to "none".
  52. (void)global_object;
  53. // 7. Let responseObject be null.
  54. JS::GCPtr<Response> response_object;
  55. // 8. Let relevantRealm be this’s relevant Realm.
  56. // NOTE: This assumes that the running execution context is for the fetch() function call.
  57. auto& relevant_realm = HTML::relevant_realm(*vm.running_execution_context().function);
  58. // 9. Let locallyAborted be false.
  59. // NOTE: This lets us reject promises with predictable timing, when the request to abort comes from the same thread
  60. // as the call to fetch.
  61. auto locally_aborted = Fetching::RefCountedFlag::create(false);
  62. // 10. Let controller be null.
  63. JS::GCPtr<Infrastructure::FetchController> controller;
  64. // 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.
  65. // This is not observable, AFAICT.
  66. // 12. Set controller to the result of calling fetch given request and processResponse given response being these
  67. // steps:
  68. auto process_response = [locally_aborted, promise_capability, request, response_object, &relevant_realm](JS::NonnullGCPtr<Infrastructure::Response> response) mutable {
  69. // 1. If locallyAborted is true, then abort these steps.
  70. if (locally_aborted->value())
  71. return;
  72. // AD-HOC: An execution context is required for Promise functions.
  73. HTML::TemporaryExecutionContext execution_context { relevant_realm };
  74. // 2. If response’s aborted flag is set, then:
  75. if (response->aborted()) {
  76. // FIXME: 1. Let deserializedError be the result of deserialize a serialized abort reason given controller’s
  77. // serialized abort reason and relevantRealm.
  78. auto deserialized_error = JS::js_undefined();
  79. // 2. Abort the fetch() call with p, request, responseObject, and deserializedError.
  80. abort_fetch(relevant_realm, promise_capability, request, response_object, deserialized_error);
  81. // 3. Abort these steps.
  82. return;
  83. }
  84. // 3. If response is a network error, then reject p with a TypeError and abort these steps.
  85. if (response->is_network_error()) {
  86. auto message = response->network_error_message().value_or("Response is a network error"sv);
  87. WebIDL::reject_promise(relevant_realm, promise_capability, JS::TypeError::create(relevant_realm, message));
  88. return;
  89. }
  90. // 4. Set responseObject to the result of creating a Response object, given response, "immutable", and
  91. // relevantRealm.
  92. response_object = Response::create(relevant_realm, response, Headers::Guard::Immutable);
  93. // 5. Resolve p with responseObject.
  94. WebIDL::resolve_promise(relevant_realm, promise_capability, response_object);
  95. };
  96. controller = MUST(Fetching::fetch(
  97. realm,
  98. request,
  99. Infrastructure::FetchAlgorithms::create(vm,
  100. {
  101. .process_request_body_chunk_length = {},
  102. .process_request_end_of_body = {},
  103. .process_early_hints_response = {},
  104. .process_response = move(process_response),
  105. .process_response_end_of_body = {},
  106. .process_response_consume_body = {},
  107. })));
  108. // 11. Add the following abort steps to requestObject’s signal:
  109. request_object->signal()->add_abort_algorithm([locally_aborted, request, controller, promise_capability, request_object, response_object, &relevant_realm] {
  110. dbgln_if(WEB_FETCH_DEBUG, "Fetch: Request object signal's abort algorithm called");
  111. // 1. Set locallyAborted to true.
  112. locally_aborted->set_value(true);
  113. // 2. Assert: controller is non-null.
  114. VERIFY(controller);
  115. // 3. Abort controller with requestObject’s signal’s abort reason.
  116. controller->abort(relevant_realm, request_object->signal()->reason());
  117. // AD-HOC: An execution context is required for Promise functions.
  118. HTML::TemporaryExecutionContext execution_context { relevant_realm };
  119. // 4. Abort the fetch() call with p, request, responseObject, and requestObject’s signal’s abort reason.
  120. abort_fetch(relevant_realm, *promise_capability, request, response_object, request_object->signal()->reason());
  121. });
  122. // 13. Return p.
  123. return promise_capability;
  124. }
  125. // https://fetch.spec.whatwg.org/#abort-fetch
  126. void abort_fetch(JS::Realm& realm, WebIDL::Promise const& promise, JS::NonnullGCPtr<Infrastructure::Request> request, JS::GCPtr<Response> response_object, JS::Value error)
  127. {
  128. dbgln_if(WEB_FETCH_DEBUG, "Fetch: Aborting fetch with: request @ {}, error = {}", request.ptr(), error);
  129. // 1. Reject promise with error.
  130. // NOTE: This is a no-op if promise has already fulfilled.
  131. WebIDL::reject_promise(realm, promise, error);
  132. // 2. If request’s body is non-null and is readable, then cancel request’s body with error.
  133. if (auto* body = request->body().get_pointer<JS::NonnullGCPtr<Infrastructure::Body>>(); body != nullptr && (*body)->stream()->is_readable()) {
  134. // TODO: Implement cancelling streams
  135. (void)error;
  136. }
  137. // 3. If responseObject is null, then return.
  138. if (response_object == nullptr)
  139. return;
  140. // 4. Let response be responseObject’s response.
  141. auto response = response_object->response();
  142. // 5. If response’s body is non-null and is readable, then error response’s body with error.
  143. if (response->body()) {
  144. auto stream = response->body()->stream();
  145. if (stream->is_readable()) {
  146. // TODO: Implement erroring streams
  147. (void)error;
  148. }
  149. }
  150. }
  151. }