mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-09-20 01:37:39 +03:00
LibWeb: Move extract_body() into Fetch
This commit is contained in:
parent
f98ce156c4
commit
571f4e3ebb
Notes:
sideshowbarker
2024-07-17 06:42:30 +09:00
Author: https://github.com/linusg Commit: https://github.com/SerenityOS/serenity/commit/571f4e3ebb Pull-request: https://github.com/SerenityOS/serenity/pull/15323 Reviewed-by: https://github.com/davidot ✅ Reviewed-by: https://github.com/kennethmyhra ✅
@ -123,6 +123,7 @@ set(SOURCES
|
||||
Dump.cpp
|
||||
Encoding/TextDecoder.cpp
|
||||
Encoding/TextEncoder.cpp
|
||||
Fetch/BodyInit.cpp
|
||||
Fetch/Headers.cpp
|
||||
Fetch/HeadersIterator.cpp
|
||||
Fetch/Infrastructure/HTTP.cpp
|
||||
|
76
Userland/Libraries/LibWeb/Fetch/BodyInit.cpp
Normal file
76
Userland/Libraries/LibWeb/Fetch/BodyInit.cpp
Normal file
@ -0,0 +1,76 @@
|
||||
/*
|
||||
* Copyright (c) 2022, Linus Groh <linusg@serenityos.org>
|
||||
* Copyright (c) 2022, Kenneth Myhra <kennethmyhra@serenityos.org>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#include <LibWeb/Bindings/IDLAbstractOperations.h>
|
||||
#include <LibWeb/Fetch/BodyInit.h>
|
||||
#include <LibWeb/Fetch/Infrastructure/HTTP/Bodies.h>
|
||||
#include <LibWeb/HTML/Window.h>
|
||||
#include <LibWeb/URL/URLSearchParams.h>
|
||||
|
||||
namespace Web::Fetch {
|
||||
|
||||
// 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'.
|
||||
ErrorOr<Infrastructure::BodyWithType> extract_body(JS::Realm& realm, XMLHttpRequestBodyInit const& body_init)
|
||||
{
|
||||
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.
|
||||
auto* stream = realm.heap().allocate<Streams::ReadableStream>(realm, window);
|
||||
// FIXME: 2. Let action be null.
|
||||
// 3. Let source be null.
|
||||
Infrastructure::Body::SourceType source {};
|
||||
// 4. Let length be null.
|
||||
Optional<u64> length {};
|
||||
// 5. Let type be null.
|
||||
Optional<ByteBuffer> type {};
|
||||
|
||||
// 6. Switch on object.
|
||||
// FIXME: Still need to support BufferSource and FormData
|
||||
TRY(body_init.visit(
|
||||
[&](JS::Handle<FileAPI::Blob> const& blob) -> ErrorOr<void> {
|
||||
// FIXME: Set action to this step: read object.
|
||||
// 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 {};
|
||||
},
|
||||
[&](JS::Handle<JS::Object> const& buffer_source) -> ErrorOr<void> {
|
||||
// Set source to a copy of the bytes held by object.
|
||||
source = TRY(Bindings::IDL::get_buffer_source_copy(*buffer_source.cell()));
|
||||
return {};
|
||||
},
|
||||
[&](JS::Handle<URL::URLSearchParams> const& url_search_params) -> ErrorOr<void> {
|
||||
// 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`.
|
||||
type = TRY(ByteBuffer::copy("application/x-www-form-urlencoded;charset=UTF-8"sv.bytes()));
|
||||
return {};
|
||||
},
|
||||
[&](String const& scalar_value_string) -> ErrorOr<void> {
|
||||
// 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`.
|
||||
type = TRY(ByteBuffer::copy("text/plain;charset=UTF-8"sv.bytes()));
|
||||
return {};
|
||||
}));
|
||||
|
||||
// FIXME: 7. If source is a byte sequence, then set action to a step that returns source and length to source’s length.
|
||||
// FIXME: 8. If action is non-null, then run these steps in in parallel:
|
||||
|
||||
// 9. 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) };
|
||||
// 10. Return (body, type).
|
||||
return Infrastructure::BodyWithType { .body = move(body), .type = move(type) };
|
||||
}
|
||||
|
||||
}
|
@ -16,4 +16,6 @@ namespace Web::Fetch {
|
||||
// https://fetch.spec.whatwg.org/#typedefdef-xmlhttprequestbodyinit
|
||||
using XMLHttpRequestBodyInit = Variant<JS::Handle<FileAPI::Blob>, JS::Handle<JS::Object>, JS::Handle<URL::URLSearchParams>, String>;
|
||||
|
||||
ErrorOr<Infrastructure::BodyWithType> extract_body(JS::Realm&, XMLHttpRequestBodyInit const&);
|
||||
|
||||
}
|
||||
|
@ -189,6 +189,7 @@ class HeadersIterator;
|
||||
|
||||
namespace Web::Fetch::Infrastructure {
|
||||
class Body;
|
||||
struct BodyWithType;
|
||||
struct Header;
|
||||
class HeaderList;
|
||||
class Request;
|
||||
|
@ -16,7 +16,6 @@
|
||||
#include <LibJS/Runtime/FunctionObject.h>
|
||||
#include <LibJS/Runtime/GlobalObject.h>
|
||||
#include <LibTextCodec/Decoder.h>
|
||||
#include <LibWeb/Bindings/IDLAbstractOperations.h>
|
||||
#include <LibWeb/Bindings/XMLHttpRequestPrototype.h>
|
||||
#include <LibWeb/DOM/DOMException.h>
|
||||
#include <LibWeb/DOM/Document.h>
|
||||
@ -24,6 +23,7 @@
|
||||
#include <LibWeb/DOM/EventDispatcher.h>
|
||||
#include <LibWeb/DOM/ExceptionOr.h>
|
||||
#include <LibWeb/DOM/IDLEventListener.h>
|
||||
#include <LibWeb/Fetch/BodyInit.h>
|
||||
#include <LibWeb/Fetch/Infrastructure/HTTP.h>
|
||||
#include <LibWeb/Fetch/Infrastructure/HTTP/Bodies.h>
|
||||
#include <LibWeb/Fetch/Infrastructure/HTTP/Methods.h>
|
||||
@ -268,66 +268,6 @@ Optional<StringView> XMLHttpRequest::get_final_encoding() const
|
||||
return encoding;
|
||||
}
|
||||
|
||||
// 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'.
|
||||
static ErrorOr<Fetch::Infrastructure::BodyWithType> extract_body(JS::Realm& realm, Fetch::XMLHttpRequestBodyInit const& body_init)
|
||||
{
|
||||
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.
|
||||
auto* stream = realm.heap().allocate<Streams::ReadableStream>(realm, window);
|
||||
// FIXME: 2. Let action be null.
|
||||
// 3. Let source be null.
|
||||
Fetch::Infrastructure::Body::SourceType source {};
|
||||
// 4. Let length be null.
|
||||
Optional<u64> length {};
|
||||
// 5. Let type be null.
|
||||
Optional<ByteBuffer> type {};
|
||||
|
||||
// 6. Switch on object.
|
||||
// FIXME: Still need to support BufferSource and FormData
|
||||
TRY(body_init.visit(
|
||||
[&](JS::Handle<FileAPI::Blob> const& blob) -> ErrorOr<void> {
|
||||
// FIXME: Set action to this step: read object.
|
||||
// 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 {};
|
||||
},
|
||||
[&](JS::Handle<JS::Object> const& buffer_source) -> ErrorOr<void> {
|
||||
// Set source to a copy of the bytes held by object.
|
||||
source = TRY(Bindings::IDL::get_buffer_source_copy(*buffer_source.cell()));
|
||||
return {};
|
||||
},
|
||||
[&](JS::Handle<URL::URLSearchParams> const& url_search_params) -> ErrorOr<void> {
|
||||
// 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`.
|
||||
type = TRY(ByteBuffer::copy("application/x-www-form-urlencoded;charset=UTF-8"sv.bytes()));
|
||||
return {};
|
||||
},
|
||||
[&](String const& scalar_value_string) -> ErrorOr<void> {
|
||||
// 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`.
|
||||
type = TRY(ByteBuffer::copy("text/plain;charset=UTF-8"sv.bytes()));
|
||||
return {};
|
||||
}));
|
||||
|
||||
// FIXME: 7. If source is a byte sequence, then set action to a step that returns source and length to source’s length.
|
||||
// FIXME: 8. If action is non-null, then run these steps in in parallel:
|
||||
|
||||
// 9. Let body be a body whose stream is stream, source is source, and length is length.
|
||||
auto body = Fetch::Infrastructure::Body { JS::make_handle(stream), move(source), move(length) };
|
||||
// 10. Return (body, type).
|
||||
return Fetch::Infrastructure::BodyWithType { .body = move(body), .type = move(type) };
|
||||
}
|
||||
|
||||
// https://xhr.spec.whatwg.org/#dom-xmlhttprequest-setrequestheader
|
||||
DOM::ExceptionOr<void> XMLHttpRequest::set_request_header(String const& name_string, String const& value_string)
|
||||
{
|
||||
@ -470,7 +410,7 @@ DOM::ExceptionOr<void> XMLHttpRequest::send(Optional<Fetch::XMLHttpRequestBodyIn
|
||||
if (m_method.is_one_of("GET"sv, "HEAD"sv))
|
||||
body = {};
|
||||
|
||||
auto body_with_type = body.has_value() ? TRY_OR_RETURN_OOM(global_object(), extract_body(realm, body.value())) : Optional<Fetch::Infrastructure::BodyWithType> {};
|
||||
auto body_with_type = body.has_value() ? TRY_OR_RETURN_OOM(global_object(), Fetch::extract_body(realm, body.value())) : Optional<Fetch::Infrastructure::BodyWithType> {};
|
||||
|
||||
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);
|
||||
|
Loading…
Reference in New Issue
Block a user