NavigatorBeacon.cpp 4.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /*
  2. * Copyright (c) 2023, Bastiaan van der Plaat <bastiaan.v.d.plaat@gmail.com>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibWeb/DOM/Document.h>
  7. #include <LibWeb/Fetch/Fetching/Fetching.h>
  8. #include <LibWeb/Fetch/Infrastructure/FetchAlgorithms.h>
  9. #include <LibWeb/HTML/Navigator.h>
  10. #include <LibWeb/HTML/NavigatorBeacon.h>
  11. #include <LibWeb/HTML/Scripting/Environments.h>
  12. #include <LibWeb/HTML/Window.h>
  13. namespace Web::HTML {
  14. // https://w3c.github.io/beacon/#sendbeacon-method
  15. WebIDL::ExceptionOr<bool> NavigatorBeaconMixin::send_beacon(String const& url, Optional<Fetch::BodyInit> const& data)
  16. {
  17. auto& navigator = verify_cast<Navigator>(*this);
  18. auto& realm = navigator.realm();
  19. auto& vm = realm.vm();
  20. auto& relevant_settings_object = HTML::relevant_settings_object(navigator);
  21. // 1. Set base to this's relevant settings object's API base URL.
  22. auto base_url = relevant_settings_object.api_base_url();
  23. // 2. Set origin to this's relevant settings object's origin.
  24. auto origin = relevant_settings_object.origin();
  25. // 3. Set parsedUrl to the result of the URL parser steps with url and base. If the algorithm returns an error, or if parsedUrl's scheme is not "http" or "https", throw a "TypeError" exception and terminate these steps.
  26. auto parsed_url = URLParser::basic_parse(url, base_url);
  27. if (!parsed_url.is_valid())
  28. return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, MUST(String::formatted("Beacon URL {} is invalid.", url)) };
  29. if (parsed_url.scheme() != "http" && parsed_url.scheme() != "https")
  30. return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, MUST(String::formatted("Beacon URL {} must be either http:// or https://.", url)) };
  31. // 4. Let headerList be an empty list.
  32. auto header_list = Fetch::Infrastructure::HeaderList::create(vm);
  33. // 5. Let corsMode be "no-cors".
  34. auto cors_mode = Fetch::Infrastructure::Request::Mode::NoCORS;
  35. // 6. If data is not null:
  36. Optional<JS::NonnullGCPtr<Fetch::Infrastructure::Body>> transmitted_data;
  37. if (data.has_value()) {
  38. // 6.1 Set transmittedData and contentType to the result of extracting data's byte stream with the keepalive flag set.
  39. auto body_with_type = TRY(Fetch::extract_body(realm, data.value(), true));
  40. transmitted_data = body_with_type.body;
  41. auto& content_type = body_with_type.type;
  42. // 6.2 If the amount of data that can be queued to be sent by keepalive enabled requests is exceeded by the size of transmittedData (as defined in HTTP-network-or-cache fetch), set the return value to false and terminate these steps.
  43. // FIXME: We don't have a size limit in Fetching::fetch
  44. // 6.3 If contentType is not null:
  45. if (content_type.has_value()) {
  46. // Set corsMode to "cors".
  47. cors_mode = Fetch::Infrastructure::Request::Mode::CORS;
  48. // If contentType value is a CORS-safelisted request-header value for the Content-Type header, set corsMode to "no-cors".
  49. auto content_type_header = MUST(Fetch::Infrastructure::Header::from_string_pair("Content-Type"sv, content_type.value()));
  50. if (Fetch::Infrastructure::is_cors_safelisted_request_header(content_type_header))
  51. cors_mode = Fetch::Infrastructure::Request::Mode::NoCORS;
  52. // Append a Content-Type header with value contentType to headerList.
  53. MUST(header_list->append(content_type_header));
  54. }
  55. }
  56. // FIXME: 7. Set the return value to true, return the sendBeacon() call, and continue to run the following steps in parallel:
  57. // 7.1 Let req be a new request, initialized as follows:
  58. auto req = Fetch::Infrastructure::Request::create(vm);
  59. req->set_method(MUST(ByteBuffer::copy("POST"sv.bytes()))); // method: POST
  60. req->set_client(&relevant_settings_object); // client: this's relevant settings object
  61. req->set_url_list({ parsed_url }); // url: parsedUrl
  62. req->set_header_list(header_list); // header list: headerList
  63. req->set_origin(origin); // origin: origin
  64. req->set_keepalive(true); // keepalive: true
  65. if (transmitted_data.has_value())
  66. req->set_body(transmitted_data.value()); // body: transmittedData
  67. req->set_mode(cors_mode); // mode: corsMode
  68. req->set_credentials_mode(Fetch::Infrastructure::Request::CredentialsMode::Include); // credentials mode: include
  69. req->set_initiator_type(Fetch::Infrastructure::Request::InitiatorType::Beacon); // initiator type: "beacon"
  70. // 7.2 Fetch req.
  71. (void)Fetch::Fetching::fetch(realm, req, Fetch::Infrastructure::FetchAlgorithms::create(vm, {}));
  72. return true;
  73. }
  74. }