2022-09-22 00:04:06 +01:00
|
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2022, Linus Groh <linusg@serenityos.org>
|
|
|
|
|
* Copyright (c) 2022, Kenneth Myhra <kennethmyhra@serenityos.org>
|
|
|
|
|
*
|
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include <LibWeb/Fetch/BodyInit.h>
|
|
|
|
|
#include <LibWeb/Fetch/Infrastructure/HTTP/Bodies.h>
|
|
|
|
|
#include <LibWeb/URL/URLSearchParams.h>
|
2022-09-24 16:14:37 +01:00
|
|
|
|
#include <LibWeb/WebIDL/AbstractOperations.h>
|
2022-09-25 17:03:42 +01:00
|
|
|
|
#include <LibWeb/WebIDL/ExceptionOr.h>
|
2022-09-22 00:04:06 +01:00
|
|
|
|
|
|
|
|
|
namespace Web::Fetch {
|
|
|
|
|
|
2022-10-13 19:11:42 +02:00
|
|
|
|
// https://fetch.spec.whatwg.org/#bodyinit-safely-extract
|
|
|
|
|
WebIDL::ExceptionOr<Infrastructure::BodyWithType> safely_extract_body(JS::Realm& realm, BodyInitOrReadbleBytes const& object)
|
|
|
|
|
{
|
|
|
|
|
// 1. If object is a ReadableStream object, then:
|
|
|
|
|
if (auto const* stream = object.get_pointer<JS::Handle<Streams::ReadableStream>>()) {
|
|
|
|
|
// 1. Assert: object is neither disturbed nor locked.
|
|
|
|
|
VERIFY(!((*stream)->is_disturbed() || (*stream)->is_locked()));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 2. Return the result of extracting object.
|
|
|
|
|
return extract_body(realm, object);
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-22 00:04:06 +01:00
|
|
|
|
// https://fetch.spec.whatwg.org/#concept-bodyinit-extract
|
2022-09-25 19:15:35 +01:00
|
|
|
|
WebIDL::ExceptionOr<Infrastructure::BodyWithType> extract_body(JS::Realm& realm, BodyInitOrReadbleBytes const& object, bool keepalive)
|
2022-09-22 00:04:06 +01:00
|
|
|
|
{
|
2022-10-30 14:37:38 +00:00
|
|
|
|
// 1. Let stream be null.
|
|
|
|
|
JS::GCPtr<Streams::ReadableStream> stream;
|
|
|
|
|
|
|
|
|
|
// 2. If object is a ReadableStream object, then set stream to object.
|
|
|
|
|
if (auto const* stream_handle = object.get_pointer<JS::Handle<Streams::ReadableStream>>()) {
|
|
|
|
|
stream = const_cast<Streams::ReadableStream*>(stream_handle->cell());
|
|
|
|
|
}
|
|
|
|
|
// 3. Otherwise, if object is a Blob object, set stream to the result of running object’s get stream.
|
|
|
|
|
else if (auto const* blob_handle = object.get_pointer<JS::Handle<FileAPI::Blob>>()) {
|
|
|
|
|
// FIXME: "set stream to the result of running object’s get stream"
|
|
|
|
|
(void)blob_handle;
|
LibWeb: Remove unecessary dependence on Window from assorted classes
These classes only needed Window to get at its realm. Pass a realm
directly to construct Crypto, Encoding, HRT, IntersectionObserver,
NavigationTiming, Page, RequestIdleCallback, Selection, Streams, URL,
and XML classes.
2022-09-25 18:11:21 -06:00
|
|
|
|
stream = realm.heap().allocate<Streams::ReadableStream>(realm, realm);
|
2022-09-22 17:30:54 +01:00
|
|
|
|
}
|
2022-10-30 14:37:38 +00:00
|
|
|
|
// 4. Otherwise, set stream to a new ReadableStream object, and set up stream.
|
|
|
|
|
else {
|
|
|
|
|
// FIXME: "set up stream"
|
|
|
|
|
stream = realm.heap().allocate<Streams::ReadableStream>(realm, realm);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 5. Assert: stream is a ReadableStream object.
|
|
|
|
|
VERIFY(stream);
|
2022-09-22 17:30:54 +01:00
|
|
|
|
|
2022-10-30 14:37:38 +00:00
|
|
|
|
// FIXME: 6. Let action be null.
|
|
|
|
|
|
|
|
|
|
// 7. Let source be null.
|
2022-09-22 00:04:06 +01:00
|
|
|
|
Infrastructure::Body::SourceType source {};
|
2022-10-30 14:37:38 +00:00
|
|
|
|
|
|
|
|
|
// 8. Let length be null.
|
2022-09-22 00:04:06 +01:00
|
|
|
|
Optional<u64> length {};
|
2022-10-30 14:37:38 +00:00
|
|
|
|
|
|
|
|
|
// 9. Let type be null.
|
2022-09-22 00:04:06 +01:00
|
|
|
|
Optional<ByteBuffer> type {};
|
|
|
|
|
|
2022-10-30 14:37:38 +00:00
|
|
|
|
// 10. Switch on object.
|
2022-09-22 00:04:06 +01:00
|
|
|
|
// FIXME: Still need to support BufferSource and FormData
|
2022-09-22 17:30:54 +01:00
|
|
|
|
TRY(object.visit(
|
2022-09-25 17:03:42 +01:00
|
|
|
|
[&](JS::Handle<FileAPI::Blob> const& blob) -> WebIDL::ExceptionOr<void> {
|
2022-09-22 00:04:06 +01:00
|
|
|
|
// Set source to object.
|
|
|
|
|
source = blob;
|
|
|
|
|
// Set length to object’s size.
|
|
|
|
|
length = blob->size();
|
|
|
|
|
// If object’s type attribute is not the empty byte sequence, set type to its value.
|
|
|
|
|
if (!blob->type().is_empty())
|
|
|
|
|
type = blob->type().to_byte_buffer();
|
|
|
|
|
return {};
|
|
|
|
|
},
|
2022-09-25 19:15:35 +01:00
|
|
|
|
[&](ReadonlyBytes bytes) -> WebIDL::ExceptionOr<void> {
|
|
|
|
|
// Set source to object.
|
2022-09-25 18:08:29 -06:00
|
|
|
|
source = TRY_OR_RETURN_OOM(realm, ByteBuffer::copy(bytes));
|
2022-09-25 19:15:35 +01:00
|
|
|
|
return {};
|
|
|
|
|
},
|
2022-09-25 17:03:42 +01:00
|
|
|
|
[&](JS::Handle<JS::Object> const& buffer_source) -> WebIDL::ExceptionOr<void> {
|
2022-09-22 00:04:06 +01:00
|
|
|
|
// Set source to a copy of the bytes held by object.
|
2022-09-25 18:08:29 -06:00
|
|
|
|
source = TRY_OR_RETURN_OOM(realm, WebIDL::get_buffer_source_copy(*buffer_source.cell()));
|
2022-09-22 00:04:06 +01:00
|
|
|
|
return {};
|
|
|
|
|
},
|
2022-09-25 17:03:42 +01:00
|
|
|
|
[&](JS::Handle<URL::URLSearchParams> const& url_search_params) -> WebIDL::ExceptionOr<void> {
|
2022-09-22 00:04:06 +01:00
|
|
|
|
// Set source to the result of running the application/x-www-form-urlencoded serializer with object’s list.
|
|
|
|
|
source = url_search_params->to_string().to_byte_buffer();
|
|
|
|
|
// Set type to `application/x-www-form-urlencoded;charset=UTF-8`.
|
2022-09-25 18:08:29 -06:00
|
|
|
|
type = TRY_OR_RETURN_OOM(realm, ByteBuffer::copy("application/x-www-form-urlencoded;charset=UTF-8"sv.bytes()));
|
2022-09-22 00:04:06 +01:00
|
|
|
|
return {};
|
|
|
|
|
},
|
2022-09-25 17:03:42 +01:00
|
|
|
|
[&](String const& scalar_value_string) -> WebIDL::ExceptionOr<void> {
|
2022-09-22 00:04:06 +01:00
|
|
|
|
// NOTE: AK::String is always UTF-8.
|
|
|
|
|
// Set source to the UTF-8 encoding of object.
|
|
|
|
|
source = scalar_value_string.to_byte_buffer();
|
|
|
|
|
// Set type to `text/plain;charset=UTF-8`.
|
2022-09-25 18:08:29 -06:00
|
|
|
|
type = TRY_OR_RETURN_OOM(realm, ByteBuffer::copy("text/plain;charset=UTF-8"sv.bytes()));
|
2022-09-22 17:30:54 +01:00
|
|
|
|
return {};
|
|
|
|
|
},
|
2022-09-25 17:03:42 +01:00
|
|
|
|
[&](JS::Handle<Streams::ReadableStream> const& stream) -> WebIDL::ExceptionOr<void> {
|
2022-09-22 17:30:54 +01:00
|
|
|
|
// If keepalive is true, then throw a TypeError.
|
|
|
|
|
if (keepalive)
|
2022-10-27 23:18:16 +01:00
|
|
|
|
return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, "Cannot extract body from stream when keepalive is set"sv };
|
2022-09-22 17:30:54 +01:00
|
|
|
|
|
|
|
|
|
// If object is disturbed or locked, then throw a TypeError.
|
|
|
|
|
if (stream->is_disturbed() || stream->is_locked())
|
2022-10-27 23:18:16 +01:00
|
|
|
|
return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, "Cannot extract body from disturbed or locked stream"sv };
|
2022-09-22 17:30:54 +01:00
|
|
|
|
|
2022-09-22 00:04:06 +01:00
|
|
|
|
return {};
|
|
|
|
|
}));
|
|
|
|
|
|
2022-10-30 14:37:38 +00:00
|
|
|
|
// FIXME: 11. If source is a byte sequence, then set action to a step that returns source and length to source’s length.
|
2022-11-08 10:02:01 -05:00
|
|
|
|
// FIXME: 12. If action is non-null, then run these steps in parallel:
|
2022-10-30 14:37:38 +00:00
|
|
|
|
|
|
|
|
|
// 13. Let body be a body whose stream is stream, source is source, and length is length.
|
|
|
|
|
auto body = Infrastructure::Body { JS::make_handle(*stream), move(source), move(length) };
|
2022-09-22 00:04:06 +01:00
|
|
|
|
|
2022-10-30 14:37:38 +00:00
|
|
|
|
// 14. Return (body, type).
|
2022-09-22 00:04:06 +01:00
|
|
|
|
return Infrastructure::BodyWithType { .body = move(body), .type = move(type) };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|