/src/serenity/Userland/Libraries/LibWeb/Fetch/Request.h
Line | Count | Source |
1 | | /* |
2 | | * Copyright (c) 2022-2023, Linus Groh <linusg@serenityos.org> |
3 | | * |
4 | | * SPDX-License-Identifier: BSD-2-Clause |
5 | | */ |
6 | | |
7 | | #pragma once |
8 | | |
9 | | #include <AK/Forward.h> |
10 | | #include <LibJS/Forward.h> |
11 | | #include <LibJS/Heap/GCPtr.h> |
12 | | #include <LibWeb/Bindings/PlatformObject.h> |
13 | | #include <LibWeb/Bindings/RequestPrototype.h> |
14 | | #include <LibWeb/Fetch/Body.h> |
15 | | #include <LibWeb/Fetch/BodyInit.h> |
16 | | #include <LibWeb/Fetch/Headers.h> |
17 | | #include <LibWeb/Fetch/Infrastructure/HTTP/Requests.h> |
18 | | #include <LibWeb/Forward.h> |
19 | | |
20 | | namespace Web::Fetch { |
21 | | |
22 | | // https://fetch.spec.whatwg.org/#requestinfo |
23 | | using RequestInfo = Variant<JS::Handle<Request>, String>; |
24 | | |
25 | | // https://fetch.spec.whatwg.org/#requestinit |
26 | | struct RequestInit { |
27 | | Optional<String> method; |
28 | | Optional<HeadersInit> headers; |
29 | | Optional<Optional<BodyInit>> body; |
30 | | Optional<String> referrer; |
31 | | Optional<Bindings::ReferrerPolicy> referrer_policy; |
32 | | Optional<Bindings::RequestMode> mode; |
33 | | Optional<Bindings::RequestCredentials> credentials; |
34 | | Optional<Bindings::RequestCache> cache; |
35 | | Optional<Bindings::RequestRedirect> redirect; |
36 | | Optional<String> integrity; |
37 | | Optional<bool> keepalive; |
38 | | Optional<JS::GCPtr<DOM::AbortSignal>> signal; |
39 | | Optional<Bindings::RequestDuplex> duplex; |
40 | | Optional<Bindings::RequestPriority> priority; |
41 | | Optional<JS::Value> window; |
42 | | |
43 | | // https://infra.spec.whatwg.org/#map-is-empty |
44 | | bool is_empty() const |
45 | 0 | { |
46 | 0 | return !(method.has_value() |
47 | 0 | || headers.has_value() |
48 | 0 | || body.has_value() |
49 | 0 | || referrer.has_value() |
50 | 0 | || referrer_policy.has_value() |
51 | 0 | || mode.has_value() |
52 | 0 | || credentials.has_value() |
53 | 0 | || cache.has_value() |
54 | 0 | || redirect.has_value() |
55 | 0 | || integrity.has_value() |
56 | 0 | || keepalive.has_value() |
57 | 0 | || signal.has_value() |
58 | 0 | || duplex.has_value() |
59 | 0 | || priority.has_value() |
60 | 0 | || window.has_value()); |
61 | 0 | } |
62 | | }; |
63 | | |
64 | | // https://fetch.spec.whatwg.org/#request |
65 | | class Request final |
66 | | : public Bindings::PlatformObject |
67 | | , public BodyMixin { |
68 | | WEB_PLATFORM_OBJECT(Request, Bindings::PlatformObject); |
69 | | JS_DECLARE_ALLOCATOR(Request); |
70 | | |
71 | | public: |
72 | | [[nodiscard]] static JS::NonnullGCPtr<Request> create(JS::Realm&, JS::NonnullGCPtr<Infrastructure::Request>, Headers::Guard, JS::NonnullGCPtr<DOM::AbortSignal>); |
73 | | static WebIDL::ExceptionOr<JS::NonnullGCPtr<Request>> construct_impl(JS::Realm&, RequestInfo const& input, RequestInit const& init = {}); |
74 | | |
75 | | virtual ~Request() override; |
76 | | |
77 | | // ^BodyMixin |
78 | | virtual Optional<MimeSniff::MimeType> mime_type_impl() const override; |
79 | | virtual JS::GCPtr<Infrastructure::Body> body_impl() override; |
80 | | virtual JS::GCPtr<Infrastructure::Body const> body_impl() const override; |
81 | 0 | virtual Bindings::PlatformObject& as_platform_object() override { return *this; } |
82 | 0 | virtual Bindings::PlatformObject const& as_platform_object() const override { return *this; } |
83 | | |
84 | 0 | [[nodiscard]] JS::NonnullGCPtr<Infrastructure::Request> request() const { return m_request; } |
85 | | |
86 | | // JS API functions |
87 | | [[nodiscard]] String method() const; |
88 | | [[nodiscard]] String url() const; |
89 | | [[nodiscard]] JS::NonnullGCPtr<Headers> headers() const; |
90 | | [[nodiscard]] Bindings::RequestDestination destination() const; |
91 | | [[nodiscard]] String referrer() const; |
92 | | [[nodiscard]] Bindings::ReferrerPolicy referrer_policy() const; |
93 | | [[nodiscard]] Bindings::RequestMode mode() const; |
94 | | [[nodiscard]] Bindings::RequestCredentials credentials() const; |
95 | | [[nodiscard]] Bindings::RequestCache cache() const; |
96 | | [[nodiscard]] Bindings::RequestRedirect redirect() const; |
97 | | [[nodiscard]] String integrity() const; |
98 | | [[nodiscard]] bool keepalive() const; |
99 | | [[nodiscard]] bool is_reload_navigation() const; |
100 | | [[nodiscard]] bool is_history_navigation() const; |
101 | | [[nodiscard]] JS::NonnullGCPtr<DOM::AbortSignal> signal() const; |
102 | | [[nodiscard]] Bindings::RequestDuplex duplex() const; |
103 | | [[nodiscard]] WebIDL::ExceptionOr<JS::NonnullGCPtr<Request>> clone() const; |
104 | | |
105 | | private: |
106 | | Request(JS::Realm&, JS::NonnullGCPtr<Infrastructure::Request>); |
107 | | |
108 | | virtual void initialize(JS::Realm&) override; |
109 | | virtual void visit_edges(Cell::Visitor&) override; |
110 | | |
111 | | // https://fetch.spec.whatwg.org/#concept-request-request |
112 | | // A Request object has an associated request (a request). |
113 | | JS::NonnullGCPtr<Infrastructure::Request> m_request; |
114 | | |
115 | | // https://fetch.spec.whatwg.org/#request-headers |
116 | | // A Request object also has an associated headers (null or a Headers object), initially null. |
117 | | JS::GCPtr<Headers> m_headers; |
118 | | |
119 | | // https://fetch.spec.whatwg.org/#request-signal |
120 | | // A Request object has an associated signal (null or an AbortSignal object), initially null. |
121 | | JS::GCPtr<DOM::AbortSignal> m_signal; |
122 | | }; |
123 | | |
124 | | } |