CORSSettingAttribute.cpp 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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. namespace Web::HTML {
  8. // https://html.spec.whatwg.org/multipage/urls-and-fetching.html#cors-settings-attributes
  9. CORSSettingAttribute cors_setting_attribute_from_keyword(Optional<String> const& keyword)
  10. {
  11. if (!keyword.has_value()) {
  12. // its missing value default is the No CORS state
  13. return CORSSettingAttribute::NoCORS;
  14. }
  15. if (keyword->bytes_as_string_view().equals_ignoring_ascii_case("use-credentials"sv)) {
  16. return CORSSettingAttribute::UseCredentials;
  17. }
  18. // The attribute's invalid value default is the Anonymous state
  19. return CORSSettingAttribute::Anonymous;
  20. }
  21. // https://html.spec.whatwg.org/multipage/urls-and-fetching.html#cors-settings-attribute-credentials-mode
  22. Fetch::Infrastructure::Request::CredentialsMode cors_settings_attribute_credentials_mode(CORSSettingAttribute attribute)
  23. {
  24. switch (attribute) {
  25. // -> No CORS
  26. // -> Anonymous
  27. case CORSSettingAttribute::NoCORS:
  28. case CORSSettingAttribute::Anonymous:
  29. // "same-origin"
  30. return Fetch::Infrastructure::Request::CredentialsMode::SameOrigin;
  31. // -> Use Credentials
  32. case CORSSettingAttribute::UseCredentials:
  33. // "include"
  34. return Fetch::Infrastructure::Request::CredentialsMode::Include;
  35. }
  36. VERIFY_NOT_REACHED();
  37. }
  38. }