mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 02:27:43 +00:00
LibWeb: Move extract_body() towards spec compliance
This commit is contained in:
parent
bc4ccadcac
commit
9b3da5a142
3 changed files with 43 additions and 19 deletions
|
@ -54,6 +54,8 @@ private:
|
||||||
|
|
||||||
ByteBuffer m_byte_buffer {};
|
ByteBuffer m_byte_buffer {};
|
||||||
String m_type {};
|
String m_type {};
|
||||||
|
|
||||||
|
friend class XHR::XMLHttpRequest;
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -25,6 +25,7 @@
|
||||||
#include <LibWeb/DOM/ExceptionOr.h>
|
#include <LibWeb/DOM/ExceptionOr.h>
|
||||||
#include <LibWeb/DOM/IDLEventListener.h>
|
#include <LibWeb/DOM/IDLEventListener.h>
|
||||||
#include <LibWeb/Fetch/Infrastructure/HTTP.h>
|
#include <LibWeb/Fetch/Infrastructure/HTTP.h>
|
||||||
|
#include <LibWeb/Fetch/Infrastructure/HTTP/Bodies.h>
|
||||||
#include <LibWeb/Fetch/Infrastructure/HTTP/Methods.h>
|
#include <LibWeb/Fetch/Infrastructure/HTTP/Methods.h>
|
||||||
#include <LibWeb/FileAPI/Blob.h>
|
#include <LibWeb/FileAPI/Blob.h>
|
||||||
#include <LibWeb/HTML/EventHandler.h>
|
#include <LibWeb/HTML/EventHandler.h>
|
||||||
|
@ -311,19 +312,43 @@ Optional<MimeSniff::MimeType> XMLHttpRequest::extract_mime_type(Fetch::HeaderLis
|
||||||
}
|
}
|
||||||
|
|
||||||
// https://fetch.spec.whatwg.org/#concept-bodyinit-extract
|
// https://fetch.spec.whatwg.org/#concept-bodyinit-extract
|
||||||
static XMLHttpRequest::BodyWithType extract_body(XMLHttpRequestBodyInit const& body)
|
// FIXME: The parameter 'body_init' should be 'typedef (ReadableStream or XMLHttpRequestBodyInit) BodyInit'. For now we just let it be 'XMLHttpRequestBodyInit'.
|
||||||
|
static Fetch::BodyWithType extract_body(XMLHttpRequestBodyInit const& body_init)
|
||||||
{
|
{
|
||||||
XMLHttpRequest::BodyWithType body_with_type {};
|
// FIXME: 1. Let stream be object if object is a ReadableStream object. Otherwise, let stream be a new ReadableStream, and set up stream.
|
||||||
body.visit(
|
Fetch::Body::ReadableStreamDummy stream {};
|
||||||
|
// FIXME: 2. Let action be null.
|
||||||
|
// 3. Let source be null.
|
||||||
|
Fetch::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 Blob, BufferSource and FormData
|
||||||
|
body_init.visit(
|
||||||
[&](NonnullRefPtr<URL::URLSearchParams> const& url_search_params) {
|
[&](NonnullRefPtr<URL::URLSearchParams> const& url_search_params) {
|
||||||
body_with_type.body = url_search_params->to_string().to_byte_buffer();
|
// Set source to the result of running the application/x-www-form-urlencoded serializer with object’s list.
|
||||||
body_with_type.type = "application/x-www-form-urlencoded;charset=UTF-8";
|
source = url_search_params->to_string().to_byte_buffer();
|
||||||
|
// Set type to `application/x-www-form-urlencoded;charset=UTF-8`.
|
||||||
|
type = MUST(ByteBuffer::copy("application/x-www-form-urlencoded;charset=UTF-8"sv.bytes()));
|
||||||
},
|
},
|
||||||
[&](String const& string) {
|
[&](String const& scalar_value_string) {
|
||||||
body_with_type.body = string.to_byte_buffer();
|
// NOTE: AK::String is always UTF-8.
|
||||||
body_with_type.type = "text/plain;charset=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 = MUST(ByteBuffer::copy("text/plain;charset=UTF-8"sv.bytes()));
|
||||||
});
|
});
|
||||||
return body_with_type;
|
|
||||||
|
// 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::Body { move(stream), move(source), move(length) };
|
||||||
|
// 10. Return (body, type).
|
||||||
|
return { .body = move(body), .type = move(type) };
|
||||||
}
|
}
|
||||||
|
|
||||||
// https://xhr.spec.whatwg.org/#dom-xmlhttprequest-setrequestheader
|
// https://xhr.spec.whatwg.org/#dom-xmlhttprequest-setrequestheader
|
||||||
|
@ -465,7 +490,7 @@ DOM::ExceptionOr<void> XMLHttpRequest::send(Optional<XMLHttpRequestBodyInit> bod
|
||||||
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() ? extract_body(body.value()) : XMLHttpRequest::BodyWithType {};
|
auto body_with_type = body.has_value() ? extract_body(body.value()) : Optional<Fetch::BodyWithType> {};
|
||||||
|
|
||||||
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);
|
||||||
|
@ -486,10 +511,12 @@ DOM::ExceptionOr<void> XMLHttpRequest::send(Optional<XMLHttpRequestBodyInit> bod
|
||||||
|
|
||||||
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.body.is_empty()) {
|
if (body_with_type.has_value()) {
|
||||||
request.set_body(body_with_type.body);
|
body_with_type->body.source().visit(
|
||||||
if (!body_with_type.type.is_empty())
|
[&](ByteBuffer const& buffer) { request.set_body(buffer); },
|
||||||
request.set_header("Content-Type", body_with_type.type);
|
[](auto&) {});
|
||||||
|
if (body_with_type->type.has_value())
|
||||||
|
request.set_header("Content-Type", String { body_with_type->type->span() });
|
||||||
}
|
}
|
||||||
for (auto& it : m_request_headers)
|
for (auto& it : m_request_headers)
|
||||||
request.set_header(it.key, it.value);
|
request.set_header(it.key, it.value);
|
||||||
|
|
|
@ -38,11 +38,6 @@ public:
|
||||||
Done = 4,
|
Done = 4,
|
||||||
};
|
};
|
||||||
|
|
||||||
struct BodyWithType {
|
|
||||||
ByteBuffer body;
|
|
||||||
String type;
|
|
||||||
};
|
|
||||||
|
|
||||||
using WrapperType = Bindings::XMLHttpRequestWrapper;
|
using WrapperType = Bindings::XMLHttpRequestWrapper;
|
||||||
|
|
||||||
static NonnullRefPtr<XMLHttpRequest> create(HTML::Window& window)
|
static NonnullRefPtr<XMLHttpRequest> create(HTML::Window& window)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue