NavigatorBeacon.cpp 4.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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 <LibURL/Parser.h>
  7. #include <LibWeb/DOM/Document.h>
  8. #include <LibWeb/Fetch/Fetching/Fetching.h>
  9. #include <LibWeb/Fetch/Infrastructure/FetchAlgorithms.h>
  10. #include <LibWeb/Fetch/Infrastructure/HTTP/Bodies.h>
  11. #include <LibWeb/HTML/Navigator.h>
  12. #include <LibWeb/HTML/NavigatorBeacon.h>
  13. #include <LibWeb/HTML/Scripting/Environments.h>
  14. #include <LibWeb/HTML/Window.h>
  15. namespace Web::HTML {
  16. // https://w3c.github.io/beacon/#sendbeacon-method
  17. WebIDL::ExceptionOr<bool> NavigatorBeaconMixin::send_beacon(String const& url, Optional<Fetch::BodyInit> const& data)
  18. {
  19. auto& navigator = verify_cast<Navigator>(*this);
  20. auto& realm = navigator.realm();
  21. auto& vm = realm.vm();
  22. auto& relevant_settings_object = HTML::relevant_settings_object(navigator);
  23. // 1. Set base to this's relevant settings object's API base URL.
  24. auto base_url = relevant_settings_object.api_base_url();
  25. // 2. Set origin to this's relevant settings object's origin.
  26. auto origin = relevant_settings_object.origin();
  27. // 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.
  28. auto parsed_url = URL::Parser::basic_parse(url, base_url);
  29. if (!parsed_url.is_valid())
  30. return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, MUST(String::formatted("Beacon URL {} is invalid.", url)) };
  31. if (parsed_url.scheme() != "http" && parsed_url.scheme() != "https")
  32. return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, MUST(String::formatted("Beacon URL {} must be either http:// or https://.", url)) };
  33. // 4. Let headerList be an empty list.
  34. auto header_list = Fetch::Infrastructure::HeaderList::create(vm);
  35. // 5. Let corsMode be "no-cors".
  36. auto cors_mode = Fetch::Infrastructure::Request::Mode::NoCORS;
  37. // 6. If data is not null:
  38. GC::Ptr<Fetch::Infrastructure::Body> transmitted_data;
  39. if (data.has_value()) {
  40. // 6.1 Set transmittedData and contentType to the result of extracting data's byte stream with the keepalive flag set.
  41. auto body_with_type = TRY(Fetch::extract_body(realm, data.value(), true));
  42. transmitted_data = body_with_type.body;
  43. auto& content_type = body_with_type.type;
  44. // 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.
  45. if (transmitted_data->length().has_value() && transmitted_data->length().value() > Fetch::Fetching::keepalive_maximum_size)
  46. return false;
  47. // 6.3 If contentType is not null:
  48. if (content_type.has_value()) {
  49. // Set corsMode to "cors".
  50. cors_mode = Fetch::Infrastructure::Request::Mode::CORS;
  51. // If contentType value is a CORS-safelisted request-header value for the Content-Type header, set corsMode to "no-cors".
  52. auto content_type_header = Fetch::Infrastructure::Header::from_string_pair("Content-Type"sv, content_type.value());
  53. if (Fetch::Infrastructure::is_cors_safelisted_request_header(content_type_header))
  54. cors_mode = Fetch::Infrastructure::Request::Mode::NoCORS;
  55. // Append a Content-Type header with value contentType to headerList.
  56. header_list->append(content_type_header);
  57. }
  58. }
  59. // FIXME: 7. Set the return value to true, return the sendBeacon() call, and continue to run the following steps in parallel:
  60. // 7.1 Let req be a new request, initialized as follows:
  61. auto req = Fetch::Infrastructure::Request::create(vm);
  62. req->set_method(MUST(ByteBuffer::copy("POST"sv.bytes()))); // method: POST
  63. req->set_client(&relevant_settings_object); // client: this's relevant settings object
  64. req->set_url_list({ parsed_url }); // url: parsedUrl
  65. req->set_header_list(header_list); // header list: headerList
  66. req->set_origin(origin); // origin: origin
  67. req->set_keepalive(true); // keepalive: true
  68. if (transmitted_data)
  69. req->set_body(GC::Ref<Fetch::Infrastructure::Body> { *transmitted_data }); // body: transmittedData
  70. req->set_mode(cors_mode); // mode: corsMode
  71. req->set_credentials_mode(Fetch::Infrastructure::Request::CredentialsMode::Include); // credentials mode: include
  72. req->set_initiator_type(Fetch::Infrastructure::Request::InitiatorType::Beacon); // initiator type: "beacon"
  73. // 7.2 Fetch req.
  74. (void)Fetch::Fetching::fetch(realm, req, Fetch::Infrastructure::FetchAlgorithms::create(vm, {}));
  75. return true;
  76. }
  77. }