mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 08:57:34 +00:00
LibWeb: Update extract_body() to use BodyInit
This commit is contained in:
parent
8f5620b5d9
commit
54a987ad45
3 changed files with 31 additions and 14 deletions
|
@ -6,6 +6,7 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <LibWeb/Bindings/IDLAbstractOperations.h>
|
#include <LibWeb/Bindings/IDLAbstractOperations.h>
|
||||||
|
#include <LibWeb/DOM/ExceptionOr.h>
|
||||||
#include <LibWeb/Fetch/BodyInit.h>
|
#include <LibWeb/Fetch/BodyInit.h>
|
||||||
#include <LibWeb/Fetch/Infrastructure/HTTP/Bodies.h>
|
#include <LibWeb/Fetch/Infrastructure/HTTP/Bodies.h>
|
||||||
#include <LibWeb/HTML/Window.h>
|
#include <LibWeb/HTML/Window.h>
|
||||||
|
@ -14,13 +15,18 @@
|
||||||
namespace Web::Fetch {
|
namespace Web::Fetch {
|
||||||
|
|
||||||
// https://fetch.spec.whatwg.org/#concept-bodyinit-extract
|
// https://fetch.spec.whatwg.org/#concept-bodyinit-extract
|
||||||
// FIXME: The parameter 'body_init' should be 'typedef (ReadableStream or XMLHttpRequestBodyInit) BodyInit'. For now we just let it be 'XMLHttpRequestBodyInit'.
|
DOM::ExceptionOr<Infrastructure::BodyWithType> extract_body(JS::Realm& realm, BodyInit const& object, bool keepalive)
|
||||||
ErrorOr<Infrastructure::BodyWithType> extract_body(JS::Realm& realm, XMLHttpRequestBodyInit const& body_init)
|
|
||||||
{
|
{
|
||||||
auto& window = verify_cast<HTML::Window>(realm.global_object());
|
auto& window = verify_cast<HTML::Window>(realm.global_object());
|
||||||
|
|
||||||
// FIXME: 1. Let stream be object if object is a ReadableStream object. Otherwise, let stream be a new ReadableStream, and set up stream.
|
// 1. Let stream be object if object is a ReadableStream object. Otherwise, let stream be a new ReadableStream, and set up stream.
|
||||||
auto* stream = realm.heap().allocate<Streams::ReadableStream>(realm, window);
|
Streams::ReadableStream* stream;
|
||||||
|
if (auto const* handle = object.get_pointer<JS::Handle<Streams::ReadableStream>>()) {
|
||||||
|
stream = const_cast<Streams::ReadableStream*>(handle->cell());
|
||||||
|
} else {
|
||||||
|
stream = realm.heap().allocate<Streams::ReadableStream>(realm, window);
|
||||||
|
}
|
||||||
|
|
||||||
// FIXME: 2. Let action be null.
|
// FIXME: 2. Let action be null.
|
||||||
// 3. Let source be null.
|
// 3. Let source be null.
|
||||||
Infrastructure::Body::SourceType source {};
|
Infrastructure::Body::SourceType source {};
|
||||||
|
@ -31,8 +37,8 @@ ErrorOr<Infrastructure::BodyWithType> extract_body(JS::Realm& realm, XMLHttpRequ
|
||||||
|
|
||||||
// 6. Switch on object.
|
// 6. Switch on object.
|
||||||
// FIXME: Still need to support BufferSource and FormData
|
// FIXME: Still need to support BufferSource and FormData
|
||||||
TRY(body_init.visit(
|
TRY(object.visit(
|
||||||
[&](JS::Handle<FileAPI::Blob> const& blob) -> ErrorOr<void> {
|
[&](JS::Handle<FileAPI::Blob> const& blob) -> DOM::ExceptionOr<void> {
|
||||||
// FIXME: Set action to this step: read object.
|
// FIXME: Set action to this step: read object.
|
||||||
// Set source to object.
|
// Set source to object.
|
||||||
source = blob;
|
source = blob;
|
||||||
|
@ -43,24 +49,35 @@ ErrorOr<Infrastructure::BodyWithType> extract_body(JS::Realm& realm, XMLHttpRequ
|
||||||
type = blob->type().to_byte_buffer();
|
type = blob->type().to_byte_buffer();
|
||||||
return {};
|
return {};
|
||||||
},
|
},
|
||||||
[&](JS::Handle<JS::Object> const& buffer_source) -> ErrorOr<void> {
|
[&](JS::Handle<JS::Object> const& buffer_source) -> DOM::ExceptionOr<void> {
|
||||||
// Set source to a copy of the bytes held by object.
|
// Set source to a copy of the bytes held by object.
|
||||||
source = TRY(Bindings::IDL::get_buffer_source_copy(*buffer_source.cell()));
|
source = TRY_OR_RETURN_OOM(window, Bindings::IDL::get_buffer_source_copy(*buffer_source.cell()));
|
||||||
return {};
|
return {};
|
||||||
},
|
},
|
||||||
[&](JS::Handle<URL::URLSearchParams> const& url_search_params) -> ErrorOr<void> {
|
[&](JS::Handle<URL::URLSearchParams> const& url_search_params) -> DOM::ExceptionOr<void> {
|
||||||
// Set source to the result of running the application/x-www-form-urlencoded serializer with object’s list.
|
// 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();
|
source = url_search_params->to_string().to_byte_buffer();
|
||||||
// Set type to `application/x-www-form-urlencoded;charset=UTF-8`.
|
// Set type to `application/x-www-form-urlencoded;charset=UTF-8`.
|
||||||
type = TRY(ByteBuffer::copy("application/x-www-form-urlencoded;charset=UTF-8"sv.bytes()));
|
type = TRY_OR_RETURN_OOM(window, ByteBuffer::copy("application/x-www-form-urlencoded;charset=UTF-8"sv.bytes()));
|
||||||
return {};
|
return {};
|
||||||
},
|
},
|
||||||
[&](String const& scalar_value_string) -> ErrorOr<void> {
|
[&](String const& scalar_value_string) -> DOM::ExceptionOr<void> {
|
||||||
// NOTE: AK::String is always UTF-8.
|
// NOTE: AK::String is always UTF-8.
|
||||||
// Set source to the UTF-8 encoding of object.
|
// Set source to the UTF-8 encoding of object.
|
||||||
source = scalar_value_string.to_byte_buffer();
|
source = scalar_value_string.to_byte_buffer();
|
||||||
// Set type to `text/plain;charset=UTF-8`.
|
// Set type to `text/plain;charset=UTF-8`.
|
||||||
type = TRY(ByteBuffer::copy("text/plain;charset=UTF-8"sv.bytes()));
|
type = TRY_OR_RETURN_OOM(window, ByteBuffer::copy("text/plain;charset=UTF-8"sv.bytes()));
|
||||||
|
return {};
|
||||||
|
},
|
||||||
|
[&](JS::Handle<Streams::ReadableStream> const& stream) -> DOM::ExceptionOr<void> {
|
||||||
|
// If keepalive is true, then throw a TypeError.
|
||||||
|
if (keepalive)
|
||||||
|
return DOM::SimpleException { DOM::SimpleExceptionType::TypeError, "Cannot extract body from stream when keepalive is set" };
|
||||||
|
|
||||||
|
// If object is disturbed or locked, then throw a TypeError.
|
||||||
|
if (stream->is_disturbed() || stream->is_locked())
|
||||||
|
return DOM::SimpleException { DOM::SimpleExceptionType::TypeError, "Cannot extract body from disturbed or locked stream" };
|
||||||
|
|
||||||
return {};
|
return {};
|
||||||
}));
|
}));
|
||||||
|
|
||||||
|
|
|
@ -19,6 +19,6 @@ using XMLHttpRequestBodyInit = Variant<JS::Handle<FileAPI::Blob>, JS::Handle<JS:
|
||||||
// https://fetch.spec.whatwg.org/#bodyinit
|
// https://fetch.spec.whatwg.org/#bodyinit
|
||||||
using BodyInit = Variant<JS::Handle<Streams::ReadableStream>, JS::Handle<FileAPI::Blob>, JS::Handle<JS::Object>, JS::Handle<URL::URLSearchParams>, String>;
|
using BodyInit = Variant<JS::Handle<Streams::ReadableStream>, JS::Handle<FileAPI::Blob>, JS::Handle<JS::Object>, JS::Handle<URL::URLSearchParams>, String>;
|
||||||
|
|
||||||
ErrorOr<Infrastructure::BodyWithType> extract_body(JS::Realm&, XMLHttpRequestBodyInit const&);
|
DOM::ExceptionOr<Infrastructure::BodyWithType> extract_body(JS::Realm&, BodyInit const&, bool keepalive = false);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -410,7 +410,7 @@ DOM::ExceptionOr<void> XMLHttpRequest::send(Optional<Fetch::XMLHttpRequestBodyIn
|
||||||
if (m_method.is_one_of("GET"sv, "HEAD"sv))
|
if (m_method.is_one_of("GET"sv, "HEAD"sv))
|
||||||
body = {};
|
body = {};
|
||||||
|
|
||||||
auto body_with_type = body.has_value() ? TRY_OR_RETURN_OOM(global_object(), Fetch::extract_body(realm, body.value())) : Optional<Fetch::Infrastructure::BodyWithType> {};
|
auto body_with_type = body.has_value() ? TRY(Fetch::extract_body(realm, body.value())) : Optional<Fetch::Infrastructure::BodyWithType> {};
|
||||||
|
|
||||||
AK::URL request_url = m_window->associated_document().parse_url(m_url.to_string());
|
AK::URL request_url = m_window->associated_document().parse_url(m_url.to_string());
|
||||||
dbgln("XHR send from {} to {}", m_window->associated_document().url(), request_url);
|
dbgln("XHR send from {} to {}", m_window->associated_document().url(), request_url);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue