Bodies.h 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. /*
  2. * Copyright (c) 2022, Linus Groh <linusg@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/ByteBuffer.h>
  8. #include <AK/Forward.h>
  9. #include <AK/NonnullRefPtr.h>
  10. #include <AK/Optional.h>
  11. #include <AK/Variant.h>
  12. #include <LibJS/Heap/Handle.h>
  13. #include <LibWeb/FileAPI/Blob.h>
  14. #include <LibWeb/Streams/ReadableStream.h>
  15. namespace Web::Fetch::Infrastructure {
  16. // https://fetch.spec.whatwg.org/#concept-body
  17. class Body final {
  18. public:
  19. using SourceType = Variant<Empty, ByteBuffer, JS::Handle<FileAPI::Blob>>;
  20. explicit Body(JS::Handle<Streams::ReadableStream>);
  21. Body(JS::Handle<Streams::ReadableStream>, SourceType, Optional<u64>);
  22. [[nodiscard]] JS::NonnullGCPtr<Streams::ReadableStream> stream() const { return *m_stream; }
  23. [[nodiscard]] SourceType const& source() const { return m_source; }
  24. [[nodiscard]] Optional<u64> const& length() const { return m_length; }
  25. [[nodiscard]] WebIDL::ExceptionOr<Body> clone() const;
  26. [[nodiscard]] JS::NonnullGCPtr<JS::PromiseCapability> fully_read_as_promise() const;
  27. private:
  28. // https://fetch.spec.whatwg.org/#concept-body-stream
  29. // A stream (a ReadableStream object).
  30. JS::Handle<Streams::ReadableStream> m_stream;
  31. // https://fetch.spec.whatwg.org/#concept-body-source
  32. // A source (null, a byte sequence, a Blob object, or a FormData object), initially null.
  33. SourceType m_source;
  34. // https://fetch.spec.whatwg.org/#concept-body-total-bytes
  35. // A length (null or an integer), initially null.
  36. Optional<u64> m_length;
  37. };
  38. // https://fetch.spec.whatwg.org/#body-with-type
  39. // A body with type is a tuple that consists of a body (a body) and a type (a header value or null).
  40. struct BodyWithType {
  41. Body body;
  42. Optional<ByteBuffer> type;
  43. };
  44. }