PotentialCORSRequest.cpp 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. /*
  2. * Copyright (c) 2023, Srikavin Ramkumar <me@srikavin.me>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibWeb/HTML/CORSSettingAttribute.h>
  7. #include <LibWeb/HTML/PotentialCORSRequest.h>
  8. namespace Web::HTML {
  9. // https://html.spec.whatwg.org/multipage/urls-and-fetching.html#create-a-potential-cors-request
  10. JS::NonnullGCPtr<Fetch::Infrastructure::Request>
  11. create_potential_CORS_request(JS::VM& vm, AK::URL const& url, Optional<Fetch::Infrastructure::Request::Destination> destination, CORSSettingAttribute cors_attribute_state, SameOriginFallbackFlag same_origin_fallback_flag)
  12. {
  13. // 1. Let mode be "no-cors" if corsAttributeState is No CORS, and "cors" otherwise.
  14. auto mode = cors_attribute_state == CORSSettingAttribute::NoCORS
  15. ? Fetch::Infrastructure::Request::Mode::NoCORS
  16. : Fetch::Infrastructure::Request::Mode::CORS;
  17. // 2. If same-origin fallback flag is set and mode is "no-cors", set mode to "same-origin".
  18. if (same_origin_fallback_flag == SameOriginFallbackFlag::Yes && mode == Fetch::Infrastructure::Request::Mode::NoCORS)
  19. mode = Fetch::Infrastructure::Request::Mode::SameOrigin;
  20. // 3. Let credentialsMode be "include".
  21. auto credentials_mode = Fetch::Infrastructure::Request::CredentialsMode::Include;
  22. // 4. If corsAttributeState is Anonymous, set credentialsMode to "same-origin".
  23. if (cors_attribute_state == CORSSettingAttribute::Anonymous)
  24. credentials_mode = Fetch::Infrastructure::Request::CredentialsMode::SameOrigin;
  25. // 5. Let request be a new request whose URL is url, destination is destination, mode is mode, credentials mode is credentialsMode,
  26. // and whose use-URL-credentials flag is set.
  27. auto request = Fetch::Infrastructure::Request::create(vm);
  28. request->set_url(url);
  29. request->set_destination(destination);
  30. request->set_mode(mode);
  31. request->set_credentials_mode(credentials_mode);
  32. request->set_use_url_credentials(true);
  33. return request;
  34. }
  35. }