mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 14:07:46 +00:00
LibWeb: Implement support for DOM::Document in XHR::send()
Co-authored-by: Linus Groh <mail@linusgroh.de>
This commit is contained in:
parent
1dc05fcc12
commit
d11d26ef91
3 changed files with 26 additions and 9 deletions
|
@ -421,7 +421,7 @@ WebIDL::ExceptionOr<void> XMLHttpRequest::open(String const& method_string, Stri
|
||||||
}
|
}
|
||||||
|
|
||||||
// https://xhr.spec.whatwg.org/#dom-xmlhttprequest-send
|
// https://xhr.spec.whatwg.org/#dom-xmlhttprequest-send
|
||||||
WebIDL::ExceptionOr<void> XMLHttpRequest::send(Optional<Fetch::XMLHttpRequestBodyInit> body)
|
WebIDL::ExceptionOr<void> XMLHttpRequest::send(Optional<DocumentOrXMLHttpRequestBodyInit> body)
|
||||||
{
|
{
|
||||||
auto& vm = this->vm();
|
auto& vm = this->vm();
|
||||||
auto& realm = *vm.current_realm();
|
auto& realm = *vm.current_realm();
|
||||||
|
@ -436,7 +436,14 @@ WebIDL::ExceptionOr<void> XMLHttpRequest::send(Optional<Fetch::XMLHttpRequestBod
|
||||||
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(Fetch::extract_body(realm, body.value())) : Optional<Fetch::Infrastructure::BodyWithType> {};
|
Optional<Fetch::Infrastructure::BodyWithType> body_with_type {};
|
||||||
|
Optional<String> serialized_document {};
|
||||||
|
if (body.has_value()) {
|
||||||
|
if (body->has<JS::Handle<DOM::Document>>())
|
||||||
|
serialized_document = TRY(body->get<JS::Handle<DOM::Document>>().cell()->serialize_fragment(DOMParsing::RequireWellFormed::No));
|
||||||
|
else
|
||||||
|
body_with_type = TRY(Fetch::extract_body(realm, body->downcast<Fetch::BodyInitOrReadableBytes>()));
|
||||||
|
}
|
||||||
|
|
||||||
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);
|
||||||
|
@ -457,7 +464,9 @@ WebIDL::ExceptionOr<void> XMLHttpRequest::send(Optional<Fetch::XMLHttpRequestBod
|
||||||
|
|
||||||
auto request = LoadRequest::create_for_url_on_page(request_url, m_window->page());
|
auto request = LoadRequest::create_for_url_on_page(request_url, m_window->page());
|
||||||
request.set_method(m_method);
|
request.set_method(m_method);
|
||||||
if (body_with_type.has_value()) {
|
if (serialized_document.has_value()) {
|
||||||
|
request.set_body(serialized_document->to_byte_buffer());
|
||||||
|
} else if (body_with_type.has_value()) {
|
||||||
TRY(body_with_type->body.source().visit(
|
TRY(body_with_type->body.source().visit(
|
||||||
[&](ByteBuffer const& buffer) -> WebIDL::ExceptionOr<void> {
|
[&](ByteBuffer const& buffer) -> WebIDL::ExceptionOr<void> {
|
||||||
auto byte_buffer = TRY_OR_RETURN_OOM(realm, ByteBuffer::copy(buffer));
|
auto byte_buffer = TRY_OR_RETURN_OOM(realm, ByteBuffer::copy(buffer));
|
||||||
|
@ -472,10 +481,14 @@ WebIDL::ExceptionOr<void> XMLHttpRequest::send(Optional<Fetch::XMLHttpRequestBod
|
||||||
[](auto&) -> WebIDL::ExceptionOr<void> {
|
[](auto&) -> WebIDL::ExceptionOr<void> {
|
||||||
return {};
|
return {};
|
||||||
}));
|
}));
|
||||||
if (body_with_type->type.has_value()) {
|
}
|
||||||
// If type is non-null and this’s headers’s header list does not contain `Content-Type`, then append (`Content-Type`, type) to this’s headers.
|
|
||||||
if (!m_request_headers.contains("Content-Type"sv))
|
// If this’s headers’s header list does not contain `Content-Type`, then append (`Content-Type`, type) to this’s headers.
|
||||||
request.set_header("Content-Type", String { body_with_type->type->span() });
|
if (!m_request_headers.contains("Content-Type"sv)) {
|
||||||
|
if (body_with_type.has_value() && body_with_type->type.has_value()) {
|
||||||
|
request.set_header("Content-Type", String { body_with_type->type->span() });
|
||||||
|
} else if (body.has_value() && body->has<JS::Handle<DOM::Document>>()) {
|
||||||
|
request.set_header("Content-Type", "text/html;charset=UTF-8");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for (auto& it : m_request_headers)
|
for (auto& it : m_request_headers)
|
||||||
|
|
|
@ -23,6 +23,9 @@
|
||||||
|
|
||||||
namespace Web::XHR {
|
namespace Web::XHR {
|
||||||
|
|
||||||
|
// https://fetch.spec.whatwg.org/#typedefdef-xmlhttprequestbodyinit
|
||||||
|
using DocumentOrXMLHttpRequestBodyInit = Variant<JS::Handle<Web::DOM::Document>, JS::Handle<Web::FileAPI::Blob>, JS::Handle<JS::Object>, JS::Handle<Web::URL::URLSearchParams>, AK::String>;
|
||||||
|
|
||||||
class XMLHttpRequest final : public XMLHttpRequestEventTarget {
|
class XMLHttpRequest final : public XMLHttpRequestEventTarget {
|
||||||
WEB_PLATFORM_OBJECT(XMLHttpRequest, XMLHttpRequestEventTarget);
|
WEB_PLATFORM_OBJECT(XMLHttpRequest, XMLHttpRequestEventTarget);
|
||||||
|
|
||||||
|
@ -47,7 +50,7 @@ public:
|
||||||
|
|
||||||
WebIDL::ExceptionOr<void> open(String const& method, String const& url);
|
WebIDL::ExceptionOr<void> open(String const& method, String const& url);
|
||||||
WebIDL::ExceptionOr<void> open(String const& method, String const& url, bool async, String const& username = {}, String const& password = {});
|
WebIDL::ExceptionOr<void> open(String const& method, String const& url, bool async, String const& username = {}, String const& password = {});
|
||||||
WebIDL::ExceptionOr<void> send(Optional<Fetch::XMLHttpRequestBodyInit> body);
|
WebIDL::ExceptionOr<void> send(Optional<DocumentOrXMLHttpRequestBodyInit> body);
|
||||||
|
|
||||||
WebIDL::ExceptionOr<void> set_request_header(String const& header, String const& value);
|
WebIDL::ExceptionOr<void> set_request_header(String const& header, String const& value);
|
||||||
WebIDL::ExceptionOr<void> set_response_type(Bindings::XMLHttpRequestResponseType);
|
WebIDL::ExceptionOr<void> set_response_type(Bindings::XMLHttpRequestResponseType);
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
|
#import <DOM/Document.idl>
|
||||||
#import <DOM/EventHandler.idl>
|
#import <DOM/EventHandler.idl>
|
||||||
#import <Fetch/BodyInit.idl>
|
#import <Fetch/BodyInit.idl>
|
||||||
#import <XHR/XMLHttpRequestEventTarget.idl>
|
#import <XHR/XMLHttpRequestEventTarget.idl>
|
||||||
|
@ -33,7 +34,7 @@ interface XMLHttpRequest : XMLHttpRequestEventTarget {
|
||||||
undefined open(DOMString method, DOMString url);
|
undefined open(DOMString method, DOMString url);
|
||||||
undefined open(ByteString method, USVString url, boolean async, optional USVString? username = {}, optional USVString? password = {});
|
undefined open(ByteString method, USVString url, boolean async, optional USVString? username = {}, optional USVString? password = {});
|
||||||
undefined setRequestHeader(DOMString name, DOMString value);
|
undefined setRequestHeader(DOMString name, DOMString value);
|
||||||
undefined send(optional XMLHttpRequestBodyInit? body = null);
|
undefined send(optional (Document or XMLHttpRequestBodyInit)? body = null);
|
||||||
undefined abort();
|
undefined abort();
|
||||||
|
|
||||||
ByteString? getResponseHeader(ByteString name);
|
ByteString? getResponseHeader(ByteString name);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue