Fetch.cpp 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. /*
  2. * Copyright (c) 2024, Sam Atkins <sam@ladybird.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibURL/Parser.h>
  7. #include <LibWeb/CSS/CSSStyleSheet.h>
  8. #include <LibWeb/CSS/Fetch.h>
  9. #include <LibWeb/Fetch/Fetching/Fetching.h>
  10. namespace Web::CSS {
  11. // https://drafts.csswg.org/css-values-4/#fetch-a-style-resource
  12. void fetch_a_style_resource(String const& url_value, CSSStyleSheet const& sheet, Fetch::Infrastructure::Request::Destination destination, CorsMode cors_mode, Fetch::Infrastructure::FetchAlgorithms::ProcessResponseConsumeBodyFunction process_response)
  13. {
  14. auto& vm = sheet.vm();
  15. // 1. Let environmentSettings be sheet’s relevant settings object.
  16. auto& environment_settings = HTML::relevant_settings_object(sheet);
  17. // 2. Let base be sheet’s stylesheet base URL if it is not null, otherwise environmentSettings’s API base URL. [CSSOM]
  18. auto base = sheet.base_url().value_or(environment_settings.api_base_url());
  19. // 3. Let parsedUrl be the result of the URL parser steps with urlValue’s url and base. If the algorithm returns an error, return.
  20. auto parsed_url = URL::Parser::basic_parse(url_value, base);
  21. if (!parsed_url.is_valid())
  22. return;
  23. // 4. Let req be a new request whose url is parsedUrl, whose destination is destination, mode is corsMode,
  24. // origin is environmentSettings’s origin, credentials mode is "same-origin", use-url-credentials flag is set,
  25. // client is environmentSettings, and whose referrer is environmentSettings’s API base URL.
  26. auto request = Fetch::Infrastructure::Request::create(vm);
  27. request->set_url(parsed_url);
  28. request->set_destination(destination);
  29. request->set_mode(cors_mode == CorsMode::Cors ? Fetch::Infrastructure::Request::Mode::CORS : Fetch::Infrastructure::Request::Mode::NoCORS);
  30. request->set_origin(environment_settings.origin());
  31. request->set_credentials_mode(Fetch::Infrastructure::Request::CredentialsMode::SameOrigin);
  32. request->set_use_url_credentials(true);
  33. request->set_client(&environment_settings);
  34. request->set_referrer(environment_settings.api_base_url());
  35. // 5. Apply any URL request modifier steps that apply to this request.
  36. // FIXME: No specs seem to define these yet. When they do, implement them.
  37. // 6. If req’s mode is "cors", set req’s referrer to sheet’s location. [CSSOM]
  38. if (request->mode() == Fetch::Infrastructure::Request::Mode::CORS) {
  39. // FIXME: sheet's location is an optional string, what do we do here?
  40. }
  41. // 7. If sheet’s origin-clean flag is set, set req’s initiator type to "css". [CSSOM]
  42. if (sheet.is_origin_clean())
  43. request->set_initiator_type(Fetch::Infrastructure::Request::InitiatorType::CSS);
  44. // 8. Fetch req, with processresponseconsumebody set to processResponse.
  45. Fetch::Infrastructure::FetchAlgorithms::Input fetch_algorithms_input {};
  46. fetch_algorithms_input.process_response_consume_body = move(process_response);
  47. (void)Fetch::Fetching::fetch(environment_settings.realm(), request, Fetch::Infrastructure::FetchAlgorithms::create(vm, move(fetch_algorithms_input)));
  48. }
  49. }