Coverage Report

Created: 2025-09-05 06:52

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