浏览代码

LibWeb: Add definitions from '2.2.4. Bodies' in the Fetch spec

...at least the ones that don't require an implementation of the Streams
spec :^)
Linus Groh 3 年之前
父节点
当前提交
f507f68d4a

+ 1 - 0
Userland/Libraries/LibWeb/CMakeLists.txt

@@ -119,6 +119,7 @@ set(SOURCES
     Encoding/TextEncoder.cpp
     Fetch/Infrastructure/HTTP.cpp
     Fetch/Infrastructure/URL.cpp
+    Fetch/Infrastructure/HTTP/Bodies.cpp
     Fetch/Infrastructure/HTTP/Headers.cpp
     Fetch/Infrastructure/HTTP/Methods.cpp
     Fetch/Infrastructure/HTTP/Statuses.cpp

+ 23 - 0
Userland/Libraries/LibWeb/Fetch/Infrastructure/HTTP/Bodies.cpp

@@ -0,0 +1,23 @@
+/*
+ * Copyright (c) 2022, Linus Groh <linusg@serenityos.org>
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#include <LibWeb/Fetch/Infrastructure/HTTP/Bodies.h>
+
+namespace Web::Fetch {
+
+Body::Body(ReadableStreamDummy stream)
+    : m_stream(stream)
+{
+}
+
+Body::Body(ReadableStreamDummy stream, SourceType source, Optional<u64> length)
+    : m_stream(stream)
+    , m_source(move(source))
+    , m_length(move(length))
+{
+}
+
+}

+ 52 - 0
Userland/Libraries/LibWeb/Fetch/Infrastructure/HTTP/Bodies.h

@@ -0,0 +1,52 @@
+/*
+ * Copyright (c) 2022, Linus Groh <linusg@serenityos.org>
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#pragma once
+
+#include <AK/ByteBuffer.h>
+#include <AK/Forward.h>
+#include <AK/Optional.h>
+#include <AK/Variant.h>
+
+namespace Web::Fetch {
+
+// https://fetch.spec.whatwg.org/#concept-body
+class Body final {
+public:
+    using SourceType = Variant<Empty, ByteBuffer>;
+
+    struct ReadableStreamDummy { };
+
+    explicit Body(ReadableStreamDummy);
+    Body(ReadableStreamDummy, SourceType, Optional<u64>);
+
+    [[nodiscard]] ReadableStreamDummy const& stream() { return m_stream; }
+    [[nodiscard]] SourceType const& source() const { return m_source; }
+    [[nodiscard]] Optional<u64> const& length() const { return m_length; }
+
+private:
+    // https://fetch.spec.whatwg.org/#concept-body-stream
+    // A stream (a ReadableStream object).
+    // FIXME: Just a placeholder for now.
+    ReadableStreamDummy m_stream;
+
+    // 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 {
+    Body body;
+    Optional<ByteBuffer> type;
+};
+
+}

+ 1 - 0
Userland/Libraries/LibWeb/Forward.h

@@ -170,6 +170,7 @@ class TextEncoder;
 }
 
 namespace Web::Fetch {
+class Body;
 struct Header;
 class HeaderList;
 }