Coverage Report

Created: 2025-12-18 07:52

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/serenity/Userland/Libraries/LibWeb/Fetch/Response.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/Responses.h>
18
#include <LibWeb/Forward.h>
19
20
namespace Web::Fetch {
21
22
// https://fetch.spec.whatwg.org/#responseinit
23
struct ResponseInit {
24
    u16 status;
25
    String status_text;
26
    Optional<HeadersInit> headers;
27
};
28
29
// https://fetch.spec.whatwg.org/#response
30
class Response final
31
    : public Bindings::PlatformObject
32
    , public BodyMixin {
33
    WEB_PLATFORM_OBJECT(Response, Bindings::PlatformObject);
34
    JS_DECLARE_ALLOCATOR(Response);
35
36
public:
37
    [[nodiscard]] static JS::NonnullGCPtr<Response> create(JS::Realm&, JS::NonnullGCPtr<Infrastructure::Response>, Headers::Guard);
38
    static WebIDL::ExceptionOr<JS::NonnullGCPtr<Response>> construct_impl(JS::Realm&, Optional<BodyInit> const& body = {}, ResponseInit const& init = {});
39
40
    virtual ~Response() override;
41
42
    // ^BodyMixin
43
    virtual Optional<MimeSniff::MimeType> mime_type_impl() const override;
44
    virtual JS::GCPtr<Infrastructure::Body> body_impl() override;
45
    virtual JS::GCPtr<Infrastructure::Body const> body_impl() const override;
46
0
    virtual Bindings::PlatformObject& as_platform_object() override { return *this; }
47
0
    virtual Bindings::PlatformObject const& as_platform_object() const override { return *this; }
48
49
0
    [[nodiscard]] JS::NonnullGCPtr<Infrastructure::Response> response() const { return m_response; }
50
51
    // JS API functions
52
    [[nodiscard]] static JS::NonnullGCPtr<Response> error(JS::VM&);
53
    static WebIDL::ExceptionOr<JS::NonnullGCPtr<Response>> redirect(JS::VM&, String const& url, u16 status);
54
    static WebIDL::ExceptionOr<JS::NonnullGCPtr<Response>> json(JS::VM&, JS::Value data, ResponseInit const& init = {});
55
    [[nodiscard]] Bindings::ResponseType type() const;
56
    [[nodiscard]] String url() const;
57
    [[nodiscard]] bool redirected() const;
58
    [[nodiscard]] u16 status() const;
59
    [[nodiscard]] bool ok() const;
60
    [[nodiscard]] String status_text() const;
61
    [[nodiscard]] JS::NonnullGCPtr<Headers> headers() const;
62
    [[nodiscard]] WebIDL::ExceptionOr<JS::NonnullGCPtr<Response>> clone() const;
63
64
    // Pull in json() from the BodyMixin, which gets lost due to the static json() above
65
    using BodyMixin::json;
66
67
private:
68
    Response(JS::Realm&, JS::NonnullGCPtr<Infrastructure::Response>);
69
70
    virtual void initialize(JS::Realm&) override;
71
    virtual void visit_edges(Cell::Visitor&) override;
72
73
    WebIDL::ExceptionOr<void> initialize_response(ResponseInit const&, Optional<Infrastructure::BodyWithType> const&);
74
75
    // https://fetch.spec.whatwg.org/#concept-response-response
76
    // A Response object has an associated response (a response).
77
    JS::NonnullGCPtr<Infrastructure::Response> m_response;
78
79
    // https://fetch.spec.whatwg.org/#response-headers
80
    // A Response object also has an associated headers (null or a Headers object), initially null.
81
    JS::GCPtr<Headers> m_headers;
82
};
83
84
}