1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 02:37:42 +00:00

LibWeb: Flesh out existing XHR methods a bit more

This makes open, send and setRequestHeader a bit more spec compliant and
adds a bunch of FIXMEs for unimplemented parts.
This commit is contained in:
Luke 2021-01-23 17:50:22 +00:00 committed by Andreas Kling
parent c68148efc5
commit 4f2e154dbe
8 changed files with 359 additions and 29 deletions

View file

@ -36,6 +36,8 @@
#include <LibWeb/HTML/EventNames.h>
#include <LibWeb/Loader/ResourceLoader.h>
#include <LibWeb/Origin.h>
#include <LibWeb/XHR/EventNames.h>
#include <LibWeb/XHR/ProgressEvent.h>
#include <LibWeb/XHR/XMLHttpRequest.h>
namespace Web::XHR {
@ -52,33 +54,147 @@ XMLHttpRequest::~XMLHttpRequest()
void XMLHttpRequest::set_ready_state(ReadyState ready_state)
{
// FIXME: call onreadystatechange once we have that
m_ready_state = ready_state;
dispatch_event(DOM::Event::create(EventNames::readystatechange));
}
void XMLHttpRequest::fire_progress_event(const String& event_name, u64 transmitted, u64 length)
{
dispatch_event(ProgressEvent::create(event_name, transmitted, length));
}
String XMLHttpRequest::response_text() const
{
if (m_response.is_null())
if (m_response_object.is_null())
return {};
return String::copy(m_response);
return String::copy(m_response_object);
}
// https://fetch.spec.whatwg.org/#forbidden-header-name
static bool is_forbidden_header_name(const String& header_name)
{
if (header_name.starts_with("Proxy-", CaseSensitivity::CaseInsensitive) || header_name.starts_with("Sec-", CaseSensitivity::CaseInsensitive))
return true;
auto lowercase_header_name = header_name.to_lowercase();
return lowercase_header_name.is_one_of("accept-charset", "accept-encoding", "access-control-request-headers", "access-control-request-method", "connection", "content-length", "cookie", "cookie2", "date", "dnt", "expect", "host", "keep-alive", "origin", "referer", "te", "trailer", "transfer-encoding", "upgrade", "via");
}
// https://fetch.spec.whatwg.org/#forbidden-method
static bool is_forbidden_method(const String& method)
{
auto lowercase_method = method.to_lowercase();
return lowercase_method.is_one_of("connect", "trace", "track");
}
// https://fetch.spec.whatwg.org/#concept-method-normalize
static String normalize_method(const String& method)
{
auto lowercase_method = method.to_lowercase();
if (lowercase_method.is_one_of("delete", "get", "head", "options", "post", "put"))
return method.to_uppercase();
return method;
}
// https://fetch.spec.whatwg.org/#concept-header-value-normalize
static String normalize_header_value(const String& header_value)
{
// FIXME: I'm not sure if this is the right trim, it should only be HTML whitespace bytes.
return header_value.trim_whitespace();
}
// https://xhr.spec.whatwg.org/#dom-xmlhttprequest-setrequestheader
void XMLHttpRequest::set_request_header(const String& header, const String& value)
{
m_request_headers.set(header, value);
if (m_ready_state != ReadyState::Opened) {
// FIXME: throw an "InvalidStateError" DOMException.
return;
}
if (m_send) {
// FIXME: throw an "InvalidStateError" DOMException.
return;
}
// FIXME: Check if name matches the name production.
// FIXME: Check if value matches the value production.
if (is_forbidden_header_name(header))
return;
// FIXME: Combine
m_request_headers.set(header, normalize_header_value(value));
}
void XMLHttpRequest::open(const String& method, const String& url)
{
m_method = method;
m_url = url;
// FIXME: Let settingsObject be thiss relevant settings object.
// FIXME: If settingsObject has a responsible document and it is not fully active, then throw an "InvalidStateError" DOMException.
// FIXME: Check that the method matches the method token production. https://tools.ietf.org/html/rfc7230#section-3.1.1
if (is_forbidden_method(method)) {
// FIXME: Throw a "SecurityError" DOMException.
return;
}
String normalized_method = normalize_method(method);
// FIXME: Pass in settingObject's API base URL and API URL character encoding.
URL parsed_url(url);
if (!parsed_url.is_valid()) {
// FIXME: Throw a "SyntaxError" DOMException.
return;
}
if (!parsed_url.host().is_null()) {
// FIXME: If the username argument is not null, set the username given parsedURL and username.
// FIXME: If the password argument is not null, set the password given parsedURL and password.
}
// FIXME: If async is false, the current global object is a Window object, and either thiss timeout is
// not 0 or thiss response type is not the empty string, then throw an "InvalidAccessError" DOMException.
// FIXME: If the async argument is omitted, set async to true, and set username and password to null.
// FIXME: Terminate the ongoing fetch operated by the XMLHttpRequest object.
m_send = false;
m_upload_listener = false;
m_method = normalized_method;
m_url = parsed_url;
// FIXME: Set thiss synchronous flag if async is false; otherwise unset thiss synchronous flag.
// (We're currently defaulting to async)
m_synchronous = false;
m_request_headers.clear();
set_ready_state(ReadyState::Opened);
// FIXME: Set thiss response to a network error.
// FIXME: Set thiss received bytes to the empty byte sequence.
m_response_object = {};
if (m_ready_state != ReadyState::Opened)
set_ready_state(ReadyState::Opened);
}
// https://xhr.spec.whatwg.org/#dom-xmlhttprequest-send
void XMLHttpRequest::send()
{
URL request_url = m_window->document().complete_url(m_url);
if (m_ready_state != ReadyState::Opened) {
// FIXME: throw an "InvalidStateError" DOMException.
return;
}
if (m_send) {
// FIXME: throw an "InvalidStateError" DOMException.
return;
}
// FIXME: If thiss request method is `GET` or `HEAD`, then set body to null.
// FIXME: If body is not null, then:
URL request_url = m_window->document().complete_url(m_url.to_string());
dbgln("XHR send from {} to {}", m_window->document().url(), request_url);
// TODO: Add support for preflight requests to support CORS requests
@ -95,28 +211,62 @@ void XMLHttpRequest::send()
}
LoadRequest request;
request.set_url(m_window->document().complete_url(m_url));
request.set_method(m_method);
request.set_url(request_url);
for (auto& it : m_request_headers)
request.set_header(it.key, it.value);
// FIXME: in order to properly set ReadyState::HeadersReceived and ReadyState::Loading,
// we need to make ResourceLoader give us more detailed updates than just "done" and "error".
ResourceLoader::the().load(
request,
[weak_this = make_weak_ptr()](auto data, auto&) {
if (!weak_this)
return;
const_cast<XMLHttpRequest&>(*weak_this).m_response = ByteBuffer::copy(data);
const_cast<XMLHttpRequest&>(*weak_this).set_ready_state(ReadyState::Done);
const_cast<XMLHttpRequest&>(*weak_this).dispatch_event(DOM::Event::create(HTML::EventNames::load));
},
[weak_this = make_weak_ptr()](auto& error) {
if (!weak_this)
return;
dbgln("XHR failed to load: {}", error);
const_cast<XMLHttpRequest&>(*weak_this).set_ready_state(ReadyState::Done);
const_cast<XMLHttpRequest&>(*weak_this).dispatch_event(DOM::Event::create(HTML::EventNames::error));
});
m_upload_complete = false;
m_timed_out = false;
// FIXME: If reqs body is null (which it always is currently)
m_upload_complete = true;
m_send = true;
if (!m_synchronous) {
fire_progress_event(EventNames::loadstart, 0, 0);
// FIXME: If thiss upload complete flag is unset and thiss upload listener flag is set,
// then fire a progress event named loadstart at thiss upload object with 0 and reqs bodys total bytes.
if (m_ready_state != ReadyState::Opened || !m_send)
return;
// FIXME: in order to properly set ReadyState::HeadersReceived and ReadyState::Loading,
// we need to make ResourceLoader give us more detailed updates than just "done" and "error".
ResourceLoader::the().load(
request,
[weak_this = make_weak_ptr()](auto data, auto&) {
if (!weak_this)
return;
auto& xhr = const_cast<XMLHttpRequest&>(*weak_this);
auto response_data = ByteBuffer::copy(data);
// FIXME: There's currently no difference between transmitted and length.
u64 transmitted = response_data.size();
u64 length = response_data.size();
if (!xhr.m_synchronous) {
xhr.m_response_object = response_data;
xhr.fire_progress_event(EventNames::progress, transmitted, length);
}
xhr.m_ready_state = ReadyState::Done;
xhr.m_send = false;
xhr.dispatch_event(DOM::Event::create(EventNames::readystatechange));
xhr.fire_progress_event(EventNames::load, transmitted, length);
xhr.fire_progress_event(EventNames::loadend, transmitted, length);
},
[weak_this = make_weak_ptr()](auto& error) {
if (!weak_this)
return;
dbgln("XHR failed to load: {}", error);
const_cast<XMLHttpRequest&>(*weak_this).set_ready_state(ReadyState::Done);
const_cast<XMLHttpRequest&>(*weak_this).dispatch_event(DOM::Event::create(HTML::EventNames::error));
});
} else {
TODO();
}
}
bool XMLHttpRequest::dispatch_event(NonnullRefPtr<DOM::Event> event)