summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibWeb/Fetch/Headers.h
blob: c366b5fcbf8a03e8efd56cdba3898f5c972549f6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/*
 * Copyright (c) 2022-2023, Linus Groh <linusg@serenityos.org>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#pragma once

#include <AK/DeprecatedString.h>
#include <AK/HashMap.h>
#include <AK/Variant.h>
#include <AK/Vector.h>
#include <LibJS/Forward.h>
#include <LibJS/Heap/GCPtr.h>
#include <LibWeb/Bindings/PlatformObject.h>
#include <LibWeb/Fetch/Infrastructure/HTTP/Headers.h>
#include <LibWeb/WebIDL/ExceptionOr.h>

namespace Web::Fetch {

using HeadersInit = Variant<Vector<Vector<DeprecatedString>>, OrderedHashMap<DeprecatedString, DeprecatedString>>;

// https://fetch.spec.whatwg.org/#headers-class
class Headers final : public Bindings::PlatformObject {
    WEB_PLATFORM_OBJECT(Headers, Bindings::PlatformObject);

public:
    enum class Guard {
        Immutable,
        Request,
        RequestNoCORS,
        Response,
        None,
    };

    static WebIDL::ExceptionOr<JS::NonnullGCPtr<Headers>> construct_impl(JS::Realm& realm, Optional<HeadersInit> const& init);

    virtual ~Headers() override;

    [[nodiscard]] JS::NonnullGCPtr<Infrastructure::HeaderList> header_list() const { return m_header_list; }
    void set_header_list(JS::NonnullGCPtr<Infrastructure::HeaderList> header_list) { m_header_list = header_list; }

    [[nodiscard]] Guard guard() const { return m_guard; }
    void set_guard(Guard guard) { m_guard = guard; }

    WebIDL::ExceptionOr<void> fill(HeadersInit const&);

    // JS API functions
    WebIDL::ExceptionOr<void> append(Infrastructure::Header);
    WebIDL::ExceptionOr<void> append(DeprecatedString const& name, DeprecatedString const& value);
    WebIDL::ExceptionOr<void> delete_(DeprecatedString const& name);
    WebIDL::ExceptionOr<DeprecatedString> get(DeprecatedString const& name);
    WebIDL::ExceptionOr<Vector<DeprecatedString>> get_set_cookie();
    WebIDL::ExceptionOr<bool> has(DeprecatedString const& name);
    WebIDL::ExceptionOr<void> set(DeprecatedString const& name, DeprecatedString const& value);

    using ForEachCallback = Function<JS::ThrowCompletionOr<void>(DeprecatedString const&, DeprecatedString const&)>;
    JS::ThrowCompletionOr<void> for_each(ForEachCallback);

private:
    friend class HeadersIterator;

    Headers(JS::Realm&, JS::NonnullGCPtr<Infrastructure::HeaderList>);

    virtual JS::ThrowCompletionOr<void> initialize(JS::Realm&) override;
    virtual void visit_edges(JS::Cell::Visitor&) override;

    WebIDL::ExceptionOr<bool> validate(Infrastructure::Header const&) const;
    void remove_privileged_no_cors_request_headers();

    // https://fetch.spec.whatwg.org/#concept-headers-header-list
    // A Headers object has an associated header list (a header list), which is initially empty.
    JS::NonnullGCPtr<Infrastructure::HeaderList> m_header_list;

    // https://fetch.spec.whatwg.org/#concept-headers-guard
    // A Headers object also has an associated guard, which is a headers guard. A headers guard is "immutable", "request", "request-no-cors", "response" or "none".
    Guard m_guard { Guard::None };
};

}