mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 21:17:44 +00:00
LibWeb: Move ExceptionOr from DOM/ to WebIDL/
This is a concept fully defined in the Web IDL spec and doesn't belong in the DOM directory/namespace - not even DOMException, despite the name :^)
This commit is contained in:
parent
c0eda77928
commit
ad04d7ac9b
107 changed files with 441 additions and 440 deletions
|
@ -5,17 +5,17 @@
|
|||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#include <LibWeb/DOM/ExceptionOr.h>
|
||||
#include <LibWeb/Fetch/BodyInit.h>
|
||||
#include <LibWeb/Fetch/Infrastructure/HTTP/Bodies.h>
|
||||
#include <LibWeb/HTML/Window.h>
|
||||
#include <LibWeb/URL/URLSearchParams.h>
|
||||
#include <LibWeb/WebIDL/AbstractOperations.h>
|
||||
#include <LibWeb/WebIDL/ExceptionOr.h>
|
||||
|
||||
namespace Web::Fetch {
|
||||
|
||||
// https://fetch.spec.whatwg.org/#concept-bodyinit-extract
|
||||
DOM::ExceptionOr<Infrastructure::BodyWithType> extract_body(JS::Realm& realm, BodyInit const& object, bool keepalive)
|
||||
WebIDL::ExceptionOr<Infrastructure::BodyWithType> extract_body(JS::Realm& realm, BodyInit const& object, bool keepalive)
|
||||
{
|
||||
auto& window = verify_cast<HTML::Window>(realm.global_object());
|
||||
|
||||
|
@ -38,7 +38,7 @@ DOM::ExceptionOr<Infrastructure::BodyWithType> extract_body(JS::Realm& realm, Bo
|
|||
// 6. Switch on object.
|
||||
// FIXME: Still need to support BufferSource and FormData
|
||||
TRY(object.visit(
|
||||
[&](JS::Handle<FileAPI::Blob> const& blob) -> DOM::ExceptionOr<void> {
|
||||
[&](JS::Handle<FileAPI::Blob> const& blob) -> WebIDL::ExceptionOr<void> {
|
||||
// FIXME: Set action to this step: read object.
|
||||
// Set source to object.
|
||||
source = blob;
|
||||
|
@ -49,19 +49,19 @@ DOM::ExceptionOr<Infrastructure::BodyWithType> extract_body(JS::Realm& realm, Bo
|
|||
type = blob->type().to_byte_buffer();
|
||||
return {};
|
||||
},
|
||||
[&](JS::Handle<JS::Object> const& buffer_source) -> DOM::ExceptionOr<void> {
|
||||
[&](JS::Handle<JS::Object> const& buffer_source) -> WebIDL::ExceptionOr<void> {
|
||||
// Set source to a copy of the bytes held by object.
|
||||
source = TRY_OR_RETURN_OOM(window, WebIDL::get_buffer_source_copy(*buffer_source.cell()));
|
||||
return {};
|
||||
},
|
||||
[&](JS::Handle<URL::URLSearchParams> const& url_search_params) -> DOM::ExceptionOr<void> {
|
||||
[&](JS::Handle<URL::URLSearchParams> const& url_search_params) -> WebIDL::ExceptionOr<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_OR_RETURN_OOM(window, ByteBuffer::copy("application/x-www-form-urlencoded;charset=UTF-8"sv.bytes()));
|
||||
return {};
|
||||
},
|
||||
[&](String const& scalar_value_string) -> DOM::ExceptionOr<void> {
|
||||
[&](String const& scalar_value_string) -> WebIDL::ExceptionOr<void> {
|
||||
// NOTE: AK::String is always UTF-8.
|
||||
// Set source to the UTF-8 encoding of object.
|
||||
source = scalar_value_string.to_byte_buffer();
|
||||
|
@ -69,14 +69,14 @@ DOM::ExceptionOr<Infrastructure::BodyWithType> extract_body(JS::Realm& realm, Bo
|
|||
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> {
|
||||
[&](JS::Handle<Streams::ReadableStream> const& stream) -> WebIDL::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" };
|
||||
return WebIDL::SimpleException { WebIDL::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 WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, "Cannot extract body from disturbed or locked stream" };
|
||||
|
||||
return {};
|
||||
}));
|
||||
|
|
|
@ -19,6 +19,6 @@ using XMLHttpRequestBodyInit = Variant<JS::Handle<FileAPI::Blob>, JS::Handle<JS:
|
|||
// 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>;
|
||||
|
||||
DOM::ExceptionOr<Infrastructure::BodyWithType> extract_body(JS::Realm&, BodyInit const&, bool keepalive = false);
|
||||
WebIDL::ExceptionOr<Infrastructure::BodyWithType> extract_body(JS::Realm&, BodyInit const&, bool keepalive = false);
|
||||
|
||||
}
|
||||
|
|
|
@ -11,7 +11,7 @@
|
|||
namespace Web::Fetch {
|
||||
|
||||
// https://fetch.spec.whatwg.org/#dom-headers
|
||||
DOM::ExceptionOr<JS::NonnullGCPtr<Headers>> Headers::create_with_global_object(HTML::Window& window, Optional<HeadersInit> const& init)
|
||||
WebIDL::ExceptionOr<JS::NonnullGCPtr<Headers>> Headers::create_with_global_object(HTML::Window& window, Optional<HeadersInit> const& init)
|
||||
{
|
||||
// The new Headers(init) constructor steps are:
|
||||
auto* headers = window.heap().allocate<Headers>(window.realm(), window);
|
||||
|
@ -35,7 +35,7 @@ Headers::Headers(HTML::Window& window)
|
|||
Headers::~Headers() = default;
|
||||
|
||||
// https://fetch.spec.whatwg.org/#dom-headers-append
|
||||
DOM::ExceptionOr<void> Headers::append(String const& name_string, String const& value_string)
|
||||
WebIDL::ExceptionOr<void> Headers::append(String const& name_string, String const& value_string)
|
||||
{
|
||||
// The append(name, value) method steps are to append (name, value) to this.
|
||||
auto header = Infrastructure::Header {
|
||||
|
@ -47,18 +47,18 @@ DOM::ExceptionOr<void> Headers::append(String const& name_string, String const&
|
|||
}
|
||||
|
||||
// https://fetch.spec.whatwg.org/#dom-headers-delete
|
||||
DOM::ExceptionOr<void> Headers::delete_(String const& name_string)
|
||||
WebIDL::ExceptionOr<void> Headers::delete_(String const& name_string)
|
||||
{
|
||||
// The delete(name) method steps are:
|
||||
auto name = name_string.bytes();
|
||||
|
||||
// 1. If name is not a header name, then throw a TypeError.
|
||||
if (!Infrastructure::is_header_name(name))
|
||||
return DOM::SimpleException { DOM::SimpleExceptionType::TypeError, "Invalid header name" };
|
||||
return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, "Invalid header name" };
|
||||
|
||||
// 2. If this’s guard is "immutable", then throw a TypeError.
|
||||
if (m_guard == Guard::Immutable)
|
||||
return DOM::SimpleException { DOM::SimpleExceptionType::TypeError, "Headers object is immutable" };
|
||||
return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, "Headers object is immutable" };
|
||||
|
||||
// 3. Otherwise, if this’s guard is "request" and name is a forbidden header name, return.
|
||||
if (m_guard == Guard::Request && Infrastructure::is_forbidden_header_name(name))
|
||||
|
@ -87,14 +87,14 @@ DOM::ExceptionOr<void> Headers::delete_(String const& name_string)
|
|||
}
|
||||
|
||||
// https://fetch.spec.whatwg.org/#dom-headers-get
|
||||
DOM::ExceptionOr<String> Headers::get(String const& name_string)
|
||||
WebIDL::ExceptionOr<String> Headers::get(String const& name_string)
|
||||
{
|
||||
// The get(name) method steps are:
|
||||
auto name = name_string.bytes();
|
||||
|
||||
// 1. If name is not a header name, then throw a TypeError.
|
||||
if (!Infrastructure::is_header_name(name))
|
||||
return DOM::SimpleException { DOM::SimpleExceptionType::TypeError, "Invalid header name" };
|
||||
return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, "Invalid header name" };
|
||||
|
||||
// 2. Return the result of getting name from this’s header list.
|
||||
auto byte_buffer = TRY_OR_RETURN_OOM(global_object(), m_header_list.get(name));
|
||||
|
@ -103,21 +103,21 @@ DOM::ExceptionOr<String> Headers::get(String const& name_string)
|
|||
}
|
||||
|
||||
// https://fetch.spec.whatwg.org/#dom-headers-has
|
||||
DOM::ExceptionOr<bool> Headers::has(String const& name_string)
|
||||
WebIDL::ExceptionOr<bool> Headers::has(String const& name_string)
|
||||
{
|
||||
// The has(name) method steps are:
|
||||
auto name = name_string.bytes();
|
||||
|
||||
// 1. If name is not a header name, then throw a TypeError.
|
||||
if (!Infrastructure::is_header_name(name))
|
||||
return DOM::SimpleException { DOM::SimpleExceptionType::TypeError, "Invalid header name" };
|
||||
return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, "Invalid header name" };
|
||||
|
||||
// 2. Return true if this’s header list contains name; otherwise false.
|
||||
return m_header_list.contains(name);
|
||||
}
|
||||
|
||||
// https://fetch.spec.whatwg.org/#dom-headers-set
|
||||
DOM::ExceptionOr<void> Headers::set(String const& name_string, String const& value_string)
|
||||
WebIDL::ExceptionOr<void> Headers::set(String const& name_string, String const& value_string)
|
||||
{
|
||||
// The set(name, value) method steps are:
|
||||
auto name = name_string.bytes();
|
||||
|
@ -133,13 +133,13 @@ DOM::ExceptionOr<void> Headers::set(String const& name_string, String const& val
|
|||
|
||||
// 2. If name is not a header name or value is not a header value, then throw a TypeError.
|
||||
if (!Infrastructure::is_header_name(name))
|
||||
return DOM::SimpleException { DOM::SimpleExceptionType::TypeError, "Invalid header name" };
|
||||
return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, "Invalid header name" };
|
||||
if (!Infrastructure::is_header_value(value))
|
||||
return DOM::SimpleException { DOM::SimpleExceptionType::TypeError, "Invalid header value" };
|
||||
return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, "Invalid header value" };
|
||||
|
||||
// 3. If this’s guard is "immutable", then throw a TypeError.
|
||||
if (m_guard == Guard::Immutable)
|
||||
return DOM::SimpleException { DOM::SimpleExceptionType::TypeError, "Headers object is immutable" };
|
||||
return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, "Headers object is immutable" };
|
||||
|
||||
// 4. Otherwise, if this’s guard is "request" and name is a forbidden header name, return.
|
||||
if (m_guard == Guard::Request && Infrastructure::is_forbidden_header_name(name))
|
||||
|
@ -201,7 +201,7 @@ JS::ThrowCompletionOr<void> Headers::for_each(ForEachCallback callback)
|
|||
}
|
||||
|
||||
// https://fetch.spec.whatwg.org/#concept-headers-append
|
||||
DOM::ExceptionOr<void> Headers::append(Infrastructure::Header header)
|
||||
WebIDL::ExceptionOr<void> Headers::append(Infrastructure::Header header)
|
||||
{
|
||||
// To append a header (name, value) to a Headers object headers, run these steps:
|
||||
auto& [name, value] = header;
|
||||
|
@ -211,13 +211,13 @@ DOM::ExceptionOr<void> Headers::append(Infrastructure::Header header)
|
|||
|
||||
// 2. If name is not a header name or value is not a header value, then throw a TypeError.
|
||||
if (!Infrastructure::is_header_name(name))
|
||||
return DOM::SimpleException { DOM::SimpleExceptionType::TypeError, "Invalid header name" };
|
||||
return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, "Invalid header name" };
|
||||
if (!Infrastructure::is_header_value(value))
|
||||
return DOM::SimpleException { DOM::SimpleExceptionType::TypeError, "Invalid header value" };
|
||||
return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, "Invalid header value" };
|
||||
|
||||
// 3. If headers’s guard is "immutable", then throw a TypeError.
|
||||
if (m_guard == Guard::Immutable)
|
||||
return DOM::SimpleException { DOM::SimpleExceptionType::TypeError, "Headers object is immutable" };
|
||||
return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, "Headers object is immutable" };
|
||||
|
||||
// 4. Otherwise, if headers’s guard is "request" and name is a forbidden header name, return.
|
||||
if (m_guard == Guard::Request && Infrastructure::is_forbidden_header_name(name))
|
||||
|
@ -264,16 +264,16 @@ DOM::ExceptionOr<void> Headers::append(Infrastructure::Header header)
|
|||
}
|
||||
|
||||
// https://fetch.spec.whatwg.org/#concept-headers-fill
|
||||
DOM::ExceptionOr<void> Headers::fill(HeadersInit const& object)
|
||||
WebIDL::ExceptionOr<void> Headers::fill(HeadersInit const& object)
|
||||
{
|
||||
// To fill a Headers object headers with a given object object, run these steps:
|
||||
return object.visit(
|
||||
// 1. If object is a sequence, then for each header in object:
|
||||
[this](Vector<Vector<String>> const& object) -> DOM::ExceptionOr<void> {
|
||||
[this](Vector<Vector<String>> const& object) -> WebIDL::ExceptionOr<void> {
|
||||
for (auto const& entry : object) {
|
||||
// 1. If header does not contain exactly two items, then throw a TypeError.
|
||||
if (entry.size() != 2)
|
||||
return DOM::SimpleException { DOM::SimpleExceptionType::TypeError, "Array must contain header key/value pair" };
|
||||
return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, "Array must contain header key/value pair" };
|
||||
|
||||
// 2. Append (header’s first item, header’s second item) to headers.
|
||||
auto header = Fetch::Infrastructure::Header {
|
||||
|
@ -285,7 +285,7 @@ DOM::ExceptionOr<void> Headers::fill(HeadersInit const& object)
|
|||
return {};
|
||||
},
|
||||
// 2. Otherwise, object is a record, then for each key → value in object, append (key, value) to headers.
|
||||
[this](OrderedHashMap<String, String> const& object) -> DOM::ExceptionOr<void> {
|
||||
[this](OrderedHashMap<String, String> const& object) -> WebIDL::ExceptionOr<void> {
|
||||
for (auto const& entry : object) {
|
||||
auto header = Fetch::Infrastructure::Header {
|
||||
.name = TRY_OR_RETURN_OOM(global_object(), ByteBuffer::copy(entry.key.bytes())),
|
||||
|
|
|
@ -11,8 +11,8 @@
|
|||
#include <AK/Variant.h>
|
||||
#include <AK/Vector.h>
|
||||
#include <LibWeb/Bindings/PlatformObject.h>
|
||||
#include <LibWeb/DOM/ExceptionOr.h>
|
||||
#include <LibWeb/Fetch/Infrastructure/HTTP/Headers.h>
|
||||
#include <LibWeb/WebIDL/ExceptionOr.h>
|
||||
|
||||
namespace Web::Fetch {
|
||||
|
||||
|
@ -31,16 +31,16 @@ public:
|
|||
None,
|
||||
};
|
||||
|
||||
static DOM::ExceptionOr<JS::NonnullGCPtr<Headers>> create_with_global_object(HTML::Window& window, Optional<HeadersInit> const& init);
|
||||
static WebIDL::ExceptionOr<JS::NonnullGCPtr<Headers>> create_with_global_object(HTML::Window& window, Optional<HeadersInit> const& init);
|
||||
|
||||
virtual ~Headers() override;
|
||||
|
||||
DOM::ExceptionOr<void> append(Infrastructure::Header);
|
||||
DOM::ExceptionOr<void> append(String const& name, String const& value);
|
||||
DOM::ExceptionOr<void> delete_(String const& name);
|
||||
DOM::ExceptionOr<String> get(String const& name);
|
||||
DOM::ExceptionOr<bool> has(String const& name);
|
||||
DOM::ExceptionOr<void> set(String const& name, String const& value);
|
||||
WebIDL::ExceptionOr<void> append(Infrastructure::Header);
|
||||
WebIDL::ExceptionOr<void> append(String const& name, String const& value);
|
||||
WebIDL::ExceptionOr<void> delete_(String const& name);
|
||||
WebIDL::ExceptionOr<String> get(String const& name);
|
||||
WebIDL::ExceptionOr<bool> has(String const& name);
|
||||
WebIDL::ExceptionOr<void> set(String const& name, String const& value);
|
||||
|
||||
using ForEachCallback = Function<JS::ThrowCompletionOr<void>(String const&, String const&)>;
|
||||
JS::ThrowCompletionOr<void> for_each(ForEachCallback);
|
||||
|
@ -50,7 +50,7 @@ private:
|
|||
|
||||
explicit Headers(HTML::Window&);
|
||||
|
||||
DOM::ExceptionOr<void> fill(HeadersInit const&);
|
||||
WebIDL::ExceptionOr<void> fill(HeadersInit const&);
|
||||
void remove_privileged_no_cors_headers();
|
||||
|
||||
// https://fetch.spec.whatwg.org/#concept-headers-header-list
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue