CORSSettingAttribute.cpp 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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->is_empty() || keyword->bytes_as_string_view().equals_ignoring_ascii_case("anonymous"sv)) {
  16. return CORSSettingAttribute::Anonymous;
  17. }
  18. if (keyword->bytes_as_string_view().equals_ignoring_ascii_case("use-credentials"sv)) {
  19. return CORSSettingAttribute::UseCredentials;
  20. }
  21. // The attribute's invalid value default is the Anonymous state
  22. return CORSSettingAttribute::Anonymous;
  23. }
  24. // https://html.spec.whatwg.org/multipage/urls-and-fetching.html#cors-settings-attribute-credentials-mode
  25. Fetch::Infrastructure::Request::CredentialsMode cors_settings_attribute_credentials_mode(CORSSettingAttribute attribute)
  26. {
  27. switch (attribute) {
  28. // -> No CORS
  29. // -> Anonymous
  30. case CORSSettingAttribute::NoCORS:
  31. case CORSSettingAttribute::Anonymous:
  32. // "same-origin"
  33. return Fetch::Infrastructure::Request::CredentialsMode::SameOrigin;
  34. // -> Use Credentials
  35. case CORSSettingAttribute::UseCredentials:
  36. // "include"
  37. return Fetch::Infrastructure::Request::CredentialsMode::Include;
  38. }
  39. VERIFY_NOT_REACHED();
  40. }
  41. }