From d11d26ef9170890900096b10bd196653209579b3 Mon Sep 17 00:00:00 2001 From: Kenneth Myhra Date: Thu, 10 Nov 2022 20:11:25 +0100 Subject: [PATCH] LibWeb: Implement support for DOM::Document in XHR::send() Co-authored-by: Linus Groh --- .../Libraries/LibWeb/XHR/XMLHttpRequest.cpp | 27 ++++++++++++++----- .../Libraries/LibWeb/XHR/XMLHttpRequest.h | 5 +++- .../Libraries/LibWeb/XHR/XMLHttpRequest.idl | 3 ++- 3 files changed, 26 insertions(+), 9 deletions(-) diff --git a/Userland/Libraries/LibWeb/XHR/XMLHttpRequest.cpp b/Userland/Libraries/LibWeb/XHR/XMLHttpRequest.cpp index a52b71330d..01a7f044b7 100644 --- a/Userland/Libraries/LibWeb/XHR/XMLHttpRequest.cpp +++ b/Userland/Libraries/LibWeb/XHR/XMLHttpRequest.cpp @@ -421,7 +421,7 @@ WebIDL::ExceptionOr XMLHttpRequest::open(String const& method_string, Stri } // https://xhr.spec.whatwg.org/#dom-xmlhttprequest-send -WebIDL::ExceptionOr XMLHttpRequest::send(Optional body) +WebIDL::ExceptionOr XMLHttpRequest::send(Optional body) { auto& vm = this->vm(); auto& realm = *vm.current_realm(); @@ -436,7 +436,14 @@ WebIDL::ExceptionOr XMLHttpRequest::send(Optional {}; + Optional body_with_type {}; + Optional serialized_document {}; + if (body.has_value()) { + if (body->has>()) + serialized_document = TRY(body->get>().cell()->serialize_fragment(DOMParsing::RequireWellFormed::No)); + else + body_with_type = TRY(Fetch::extract_body(realm, body->downcast())); + } 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); @@ -457,7 +464,9 @@ WebIDL::ExceptionOr XMLHttpRequest::send(Optionalpage()); 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( [&](ByteBuffer const& buffer) -> WebIDL::ExceptionOr { auto byte_buffer = TRY_OR_RETURN_OOM(realm, ByteBuffer::copy(buffer)); @@ -472,10 +481,14 @@ WebIDL::ExceptionOr XMLHttpRequest::send(Optional WebIDL::ExceptionOr { 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)) - request.set_header("Content-Type", String { body_with_type->type->span() }); + } + + // If 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 (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>()) { + request.set_header("Content-Type", "text/html;charset=UTF-8"); } } for (auto& it : m_request_headers) diff --git a/Userland/Libraries/LibWeb/XHR/XMLHttpRequest.h b/Userland/Libraries/LibWeb/XHR/XMLHttpRequest.h index e7cf3bc1c9..43182f323a 100644 --- a/Userland/Libraries/LibWeb/XHR/XMLHttpRequest.h +++ b/Userland/Libraries/LibWeb/XHR/XMLHttpRequest.h @@ -23,6 +23,9 @@ namespace Web::XHR { +// https://fetch.spec.whatwg.org/#typedefdef-xmlhttprequestbodyinit +using DocumentOrXMLHttpRequestBodyInit = Variant, JS::Handle, JS::Handle, JS::Handle, AK::String>; + class XMLHttpRequest final : public XMLHttpRequestEventTarget { WEB_PLATFORM_OBJECT(XMLHttpRequest, XMLHttpRequestEventTarget); @@ -47,7 +50,7 @@ public: WebIDL::ExceptionOr open(String const& method, String const& url); WebIDL::ExceptionOr open(String const& method, String const& url, bool async, String const& username = {}, String const& password = {}); - WebIDL::ExceptionOr send(Optional body); + WebIDL::ExceptionOr send(Optional body); WebIDL::ExceptionOr set_request_header(String const& header, String const& value); WebIDL::ExceptionOr set_response_type(Bindings::XMLHttpRequestResponseType); diff --git a/Userland/Libraries/LibWeb/XHR/XMLHttpRequest.idl b/Userland/Libraries/LibWeb/XHR/XMLHttpRequest.idl index 0bfc20f729..cf43329f4c 100644 --- a/Userland/Libraries/LibWeb/XHR/XMLHttpRequest.idl +++ b/Userland/Libraries/LibWeb/XHR/XMLHttpRequest.idl @@ -1,3 +1,4 @@ +#import #import #import #import @@ -33,7 +34,7 @@ interface XMLHttpRequest : XMLHttpRequestEventTarget { undefined open(DOMString method, DOMString url); undefined open(ByteString method, USVString url, boolean async, optional USVString? username = {}, optional USVString? password = {}); undefined setRequestHeader(DOMString name, DOMString value); - undefined send(optional XMLHttpRequestBodyInit? body = null); + undefined send(optional (Document or XMLHttpRequestBodyInit)? body = null); undefined abort(); ByteString? getResponseHeader(ByteString name);