2022-07-12 18:04:24 +01:00
|
|
|
/*
|
2023-03-02 23:26:35 +00:00
|
|
|
* Copyright (c) 2022-2023, Linus Groh <linusg@serenityos.org>
|
2022-07-12 18:04:24 +01:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <AK/ByteBuffer.h>
|
|
|
|
#include <AK/Forward.h>
|
2022-07-18 08:09:11 +01:00
|
|
|
#include <AK/NonnullRefPtr.h>
|
2022-07-12 18:04:24 +01:00
|
|
|
#include <AK/Optional.h>
|
|
|
|
#include <AK/Variant.h>
|
2024-11-15 04:01:23 +13:00
|
|
|
#include <LibGC/Ptr.h>
|
|
|
|
#include <LibGC/Root.h>
|
2023-02-28 18:12:44 +00:00
|
|
|
#include <LibWeb/Fetch/Infrastructure/Task.h>
|
2022-07-18 08:09:11 +01:00
|
|
|
#include <LibWeb/FileAPI/Blob.h>
|
2022-09-21 23:54:04 +01:00
|
|
|
#include <LibWeb/Streams/ReadableStream.h>
|
2023-02-13 11:58:39 +01:00
|
|
|
#include <LibWeb/WebIDL/Promise.h>
|
2022-07-12 18:04:24 +01:00
|
|
|
|
2022-07-17 23:52:02 +01:00
|
|
|
namespace Web::Fetch::Infrastructure {
|
2022-07-12 18:04:24 +01:00
|
|
|
|
|
|
|
// https://fetch.spec.whatwg.org/#concept-body
|
2023-08-18 19:38:13 +02:00
|
|
|
class Body final : public JS::Cell {
|
2024-11-15 04:01:23 +13:00
|
|
|
GC_CELL(Body, JS::Cell);
|
|
|
|
GC_DECLARE_ALLOCATOR(Body);
|
2023-08-18 19:38:13 +02:00
|
|
|
|
2022-07-12 18:04:24 +01:00
|
|
|
public:
|
2024-11-15 04:01:23 +13:00
|
|
|
using SourceType = Variant<Empty, ByteBuffer, GC::Root<FileAPI::Blob>>;
|
2023-05-13 12:44:46 +01:00
|
|
|
// processBody must be an algorithm accepting a byte sequence.
|
2024-11-15 04:01:23 +13:00
|
|
|
using ProcessBodyCallback = GC::Ref<GC::Function<void(ByteBuffer)>>;
|
2023-05-13 12:44:46 +01:00
|
|
|
// processBodyError must be an algorithm optionally accepting an exception.
|
2024-11-15 04:01:23 +13:00
|
|
|
using ProcessBodyErrorCallback = GC::Ref<GC::Function<void(JS::Value)>>;
|
2024-05-17 22:06:27 +02:00
|
|
|
// processBodyChunk must be an algorithm accepting a byte sequence.
|
2024-11-15 04:01:23 +13:00
|
|
|
using ProcessBodyChunkCallback = GC::Ref<GC::Function<void(ByteBuffer)>>;
|
2024-05-17 22:06:27 +02:00
|
|
|
// processEndOfBody must be an algorithm accepting no arguments
|
2024-11-15 04:01:23 +13:00
|
|
|
using ProcessEndOfBodyCallback = GC::Ref<GC::Function<void()>>;
|
2022-07-12 18:04:24 +01:00
|
|
|
|
2024-11-15 04:01:23 +13:00
|
|
|
[[nodiscard]] static GC::Ref<Body> create(JS::VM&, GC::Ref<Streams::ReadableStream>);
|
|
|
|
[[nodiscard]] static GC::Ref<Body> create(JS::VM&, GC::Ref<Streams::ReadableStream>, SourceType, Optional<u64>);
|
2022-07-12 18:04:24 +01:00
|
|
|
|
2024-11-15 04:01:23 +13:00
|
|
|
[[nodiscard]] GC::Ref<Streams::ReadableStream> stream() const { return *m_stream; }
|
|
|
|
void set_stream(GC::Ref<Streams::ReadableStream> value) { m_stream = value; }
|
2022-07-12 18:04:24 +01:00
|
|
|
[[nodiscard]] SourceType const& source() const { return m_source; }
|
|
|
|
[[nodiscard]] Optional<u64> const& length() const { return m_length; }
|
|
|
|
|
2024-11-15 04:01:23 +13:00
|
|
|
[[nodiscard]] GC::Ref<Body> clone(JS::Realm&);
|
2022-09-25 19:25:53 +01:00
|
|
|
|
2025-07-16 12:29:26 +02:00
|
|
|
void fully_read(JS::Realm&, ProcessBodyCallback process_body, ProcessBodyErrorCallback process_body_error, TaskDestination) const;
|
|
|
|
void incrementally_read(ProcessBodyChunkCallback process_body_chunk, ProcessEndOfBodyCallback process_end_of_body, ProcessBodyErrorCallback process_body_error, TaskDestination);
|
|
|
|
void incrementally_read_loop(Streams::ReadableStreamDefaultReader& reader, TaskDestination, ProcessBodyChunkCallback process_body_chunk, ProcessEndOfBodyCallback process_end_of_body, ProcessBodyErrorCallback process_body_error);
|
2022-09-25 19:27:02 +01:00
|
|
|
|
2023-08-18 19:38:13 +02:00
|
|
|
virtual void visit_edges(JS::Cell::Visitor&) override;
|
|
|
|
|
2022-07-12 18:04:24 +01:00
|
|
|
private:
|
2024-11-15 04:01:23 +13:00
|
|
|
explicit Body(GC::Ref<Streams::ReadableStream>);
|
|
|
|
Body(GC::Ref<Streams::ReadableStream>, SourceType, Optional<u64>);
|
2023-08-18 19:38:13 +02:00
|
|
|
|
2022-07-12 18:04:24 +01:00
|
|
|
// https://fetch.spec.whatwg.org/#concept-body-stream
|
|
|
|
// A stream (a ReadableStream object).
|
2024-11-15 04:01:23 +13:00
|
|
|
GC::Ref<Streams::ReadableStream> m_stream;
|
2022-07-12 18:04:24 +01:00
|
|
|
|
|
|
|
// https://fetch.spec.whatwg.org/#concept-body-source
|
|
|
|
// A source (null, a byte sequence, a Blob object, or a FormData object), initially null.
|
|
|
|
SourceType m_source;
|
|
|
|
|
|
|
|
// https://fetch.spec.whatwg.org/#concept-body-total-bytes
|
|
|
|
// A length (null or an integer), initially null.
|
|
|
|
Optional<u64> m_length;
|
|
|
|
};
|
|
|
|
|
|
|
|
// https://fetch.spec.whatwg.org/#body-with-type
|
|
|
|
// A body with type is a tuple that consists of a body (a body) and a type (a header value or null).
|
|
|
|
struct BodyWithType {
|
2024-11-15 04:01:23 +13:00
|
|
|
GC::Ref<Body> body;
|
2022-07-12 18:04:24 +01:00
|
|
|
Optional<ByteBuffer> type;
|
|
|
|
};
|
|
|
|
|
2024-11-04 16:06:01 +01:00
|
|
|
GC::Ref<Body> byte_sequence_as_body(JS::Realm&, ReadonlyBytes);
|
2022-10-30 14:39:32 +00:00
|
|
|
|
2022-07-12 18:04:24 +01:00
|
|
|
}
|