diff --git a/Userland/Libraries/LibWeb/Fetch/BodyInit.cpp b/Userland/Libraries/LibWeb/Fetch/BodyInit.cpp index 076711867f..b116de67f3 100644 --- a/Userland/Libraries/LibWeb/Fetch/BodyInit.cpp +++ b/Userland/Libraries/LibWeb/Fetch/BodyInit.cpp @@ -1,6 +1,6 @@ /* * Copyright (c) 2022-2023, Linus Groh - * Copyright (c) 2022, Kenneth Myhra + * Copyright (c) 2022-2023, Kenneth Myhra * * SPDX-License-Identifier: BSD-2-Clause */ @@ -8,9 +8,11 @@ #include #include #include +#include #include #include #include +#include namespace Web::Fetch { @@ -66,7 +68,7 @@ WebIDL::ExceptionOr extract_body(JS::Realm& realm, Optional type {}; // 10. Switch on object. - // FIXME: Still need to support BufferSource and FormData + // FIXME: Still need to support BufferSource TRY(object.visit( [&](JS::Handle const& blob) -> WebIDL::ExceptionOr { // Set source to object. @@ -88,6 +90,16 @@ WebIDL::ExceptionOr extract_body(JS::Realm& realm, source = TRY_OR_THROW_OOM(vm, WebIDL::get_buffer_source_copy(*buffer_source.cell())); return {}; }, + [&](JS::Handle const& form_data) -> WebIDL::ExceptionOr { + // Set action to this step: run the multipart/form-data encoding algorithm, with object’s entry list and UTF-8. + auto serialized_form_data = TRY_OR_THROW_OOM(vm, HTML::serialize_to_multipart_form_data(form_data->entry_list())); + // Set source to object. + source = serialized_form_data.serialized_data; + // FIXME: Set length to unclear, see html/6424 for improving this. + // Set type to `multipart/form-data; boundary=`, followed by the multipart/form-data boundary string generated by the multipart/form-data encoding algorithm. + type = TRY_OR_THROW_OOM(vm, ByteBuffer::copy(TRY_OR_THROW_OOM(vm, String::formatted("multipart/form-data; boundary={}"sv, serialized_form_data.boundary)).bytes())); + return {}; + }, [&](JS::Handle const& url_search_params) -> WebIDL::ExceptionOr { // Set source to the result of running the application/x-www-form-urlencoded serializer with object’s list. auto search_params_bytes = TRY(url_search_params->to_string()).bytes(); diff --git a/Userland/Libraries/LibWeb/Fetch/BodyInit.h b/Userland/Libraries/LibWeb/Fetch/BodyInit.h index f22deb9c7b..787c19ffcb 100644 --- a/Userland/Libraries/LibWeb/Fetch/BodyInit.h +++ b/Userland/Libraries/LibWeb/Fetch/BodyInit.h @@ -14,9 +14,9 @@ namespace Web::Fetch { // https://fetch.spec.whatwg.org/#bodyinit -using BodyInit = Variant, JS::Handle, JS::Handle, JS::Handle, String>; +using BodyInit = Variant, JS::Handle, JS::Handle, JS::Handle, JS::Handle, String>; -using BodyInitOrReadableBytes = Variant, JS::Handle, JS::Handle, JS::Handle, String, ReadonlyBytes>; +using BodyInitOrReadableBytes = Variant, JS::Handle, JS::Handle, JS::Handle, JS::Handle, String, ReadonlyBytes>; WebIDL::ExceptionOr safely_extract_body(JS::Realm&, BodyInitOrReadableBytes const&); WebIDL::ExceptionOr extract_body(JS::Realm&, BodyInitOrReadableBytes const&, bool keepalive = false); diff --git a/Userland/Libraries/LibWeb/Fetch/BodyInit.idl b/Userland/Libraries/LibWeb/Fetch/BodyInit.idl index a1040b46ee..4eb8c7cb25 100644 --- a/Userland/Libraries/LibWeb/Fetch/BodyInit.idl +++ b/Userland/Libraries/LibWeb/Fetch/BodyInit.idl @@ -1,9 +1,10 @@ #import #import #import +#import // https://fetch.spec.whatwg.org/#typedefdef-xmlhttprequestbodyinit -typedef (Blob or BufferSource or URLSearchParams or USVString) XMLHttpRequestBodyInit; +typedef (Blob or BufferSource or FormData or URLSearchParams or USVString) XMLHttpRequestBodyInit; // https://fetch.spec.whatwg.org/#bodyinit typedef (ReadableStream or XMLHttpRequestBodyInit) BodyInit; diff --git a/Userland/Libraries/LibWeb/XHR/FormData.idl b/Userland/Libraries/LibWeb/XHR/FormData.idl index 269f15801e..caffdc16b9 100644 --- a/Userland/Libraries/LibWeb/XHR/FormData.idl +++ b/Userland/Libraries/LibWeb/XHR/FormData.idl @@ -1,6 +1,10 @@ #import #import -#import +// FIXME: This #import currently gives the following error after XHR/FormData.idl was #imported in Fetch/BodyInit.idl: +// "LibWeb/HTML/Window.idl:114: error: Mixin 'WindowOrWorkerGlobalScope' was never defined." +// XHR/FormData.idl needs to be #imported in Fetch/BodyInit.idl while removing #import HTML/HTMLFormElement.idl +// currently makes no difference. +// #import typedef (File or USVString) FormDataEntryValue; diff --git a/Userland/Libraries/LibWeb/XHR/XMLHttpRequest.h b/Userland/Libraries/LibWeb/XHR/XMLHttpRequest.h index bcb8a48865..d37701e6ec 100644 --- a/Userland/Libraries/LibWeb/XHR/XMLHttpRequest.h +++ b/Userland/Libraries/LibWeb/XHR/XMLHttpRequest.h @@ -26,7 +26,7 @@ namespace Web::XHR { // https://fetch.spec.whatwg.org/#typedefdef-xmlhttprequestbodyinit -using DocumentOrXMLHttpRequestBodyInit = Variant, JS::Handle, JS::Handle, JS::Handle, AK::String>; +using DocumentOrXMLHttpRequestBodyInit = Variant, JS::Handle, JS::Handle, JS::Handle, JS::Handle, AK::String>; class XMLHttpRequest final : public XMLHttpRequestEventTarget { WEB_PLATFORM_OBJECT(XMLHttpRequest, XMLHttpRequestEventTarget);