FetchMethod.cpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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/DOM/AbortSignal.h>
  11. #include <LibWeb/Fetch/FetchMethod.h>
  12. #include <LibWeb/Fetch/Fetching/Fetching.h>
  13. #include <LibWeb/Fetch/Fetching/RefCountedFlag.h>
  14. #include <LibWeb/Fetch/Infrastructure/FetchAlgorithms.h>
  15. #include <LibWeb/Fetch/Infrastructure/FetchController.h>
  16. #include <LibWeb/Fetch/Infrastructure/HTTP/Requests.h>
  17. #include <LibWeb/Fetch/Infrastructure/HTTP/Responses.h>
  18. #include <LibWeb/Fetch/Request.h>
  19. #include <LibWeb/Fetch/Response.h>
  20. #include <LibWeb/HTML/Scripting/TemporaryExecutionContext.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. GC::Ref<WebIDL::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 promise_capability;
  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 promise_capability;
  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. GC::Ptr<Response> response_object;
  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. GC::Ptr<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, &relevant_realm](GC::Ref<Infrastructure::Response> response) mutable {
  68. // 1. If locallyAborted is true, then abort these steps.
  69. if (locally_aborted->value())
  70. return;
  71. // AD-HOC: An execution context is required for Promise functions.
  72. HTML::TemporaryExecutionContext execution_context { relevant_realm };
  73. // 2. If response’s aborted flag is set, then:
  74. if (response->aborted()) {
  75. // FIXME: 1. Let deserializedError be the result of deserialize a serialized abort reason given controller’s
  76. // serialized abort reason and relevantRealm.
  77. auto deserialized_error = JS::js_undefined();
  78. // 2. Abort the fetch() call with p, request, responseObject, and deserializedError.
  79. abort_fetch(relevant_realm, promise_capability, request, response_object, deserialized_error);
  80. // 3. Abort these steps.
  81. return;
  82. }
  83. // 3. If response is a network error, then reject p with a TypeError and abort these steps.
  84. if (response->is_network_error()) {
  85. auto message = response->network_error_message().value_or("Response is a network error"sv);
  86. WebIDL::reject_promise(relevant_realm, promise_capability, JS::TypeError::create(relevant_realm, message));
  87. return;
  88. }
  89. // 4. Set responseObject to the result of creating a Response object, given response, "immutable", and
  90. // relevantRealm.
  91. response_object = Response::create(relevant_realm, response, Headers::Guard::Immutable);
  92. // 5. Resolve p with responseObject.
  93. WebIDL::resolve_promise(relevant_realm, promise_capability, response_object);
  94. };
  95. controller = MUST(Fetching::fetch(
  96. realm,
  97. request,
  98. Infrastructure::FetchAlgorithms::create(vm,
  99. {
  100. .process_request_body_chunk_length = {},
  101. .process_request_end_of_body = {},
  102. .process_early_hints_response = {},
  103. .process_response = move(process_response),
  104. .process_response_end_of_body = {},
  105. .process_response_consume_body = {},
  106. })));
  107. // 11. Add the following abort steps to requestObject’s signal:
  108. request_object->signal()->add_abort_algorithm([locally_aborted, request, controller, promise_capability, request_object, response_object, &relevant_realm] {
  109. dbgln_if(WEB_FETCH_DEBUG, "Fetch: Request object signal's abort algorithm called");
  110. // 1. Set locallyAborted to true.
  111. locally_aborted->set_value(true);
  112. // 2. Assert: controller is non-null.
  113. VERIFY(controller);
  114. // 3. Abort controller with requestObject’s signal’s abort reason.
  115. controller->abort(relevant_realm, request_object->signal()->reason());
  116. // AD-HOC: An execution context is required for Promise functions.
  117. HTML::TemporaryExecutionContext execution_context { relevant_realm };
  118. // 4. Abort the fetch() call with p, request, responseObject, and requestObject’s signal’s abort reason.
  119. abort_fetch(relevant_realm, *promise_capability, request, response_object, request_object->signal()->reason());
  120. });
  121. // 13. Return p.
  122. return promise_capability;
  123. }
  124. // https://fetch.spec.whatwg.org/#abort-fetch
  125. void abort_fetch(JS::Realm& realm, WebIDL::Promise const& promise, GC::Ref<Infrastructure::Request> request, GC::Ptr<Response> response_object, JS::Value error)
  126. {
  127. dbgln_if(WEB_FETCH_DEBUG, "Fetch: Aborting fetch with: request @ {}, error = {}", request.ptr(), error);
  128. // 1. Reject promise with error.
  129. // NOTE: This is a no-op if promise has already fulfilled.
  130. WebIDL::reject_promise(realm, promise, error);
  131. // 2. If request’s body is non-null and is readable, then cancel request’s body with error.
  132. if (auto* body = request->body().get_pointer<GC::Ref<Infrastructure::Body>>(); body != nullptr && (*body)->stream()->is_readable()) {
  133. // TODO: Implement cancelling streams
  134. (void)error;
  135. }
  136. // 3. If responseObject is null, then return.
  137. if (response_object == nullptr)
  138. return;
  139. // 4. Let response be responseObject’s response.
  140. auto response = response_object->response();
  141. // 5. If response’s body is non-null and is readable, then error response’s body with error.
  142. if (response->body()) {
  143. auto stream = response->body()->stream();
  144. if (stream->is_readable()) {
  145. // TODO: Implement erroring streams
  146. (void)error;
  147. }
  148. }
  149. }
  150. }