FetchMethod.cpp 8.2 KB

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