mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 05:47:35 +00:00
LibWeb: Add support for XMLHttpRequest request headers
Implement XMLHttpRequest.setRequestHeader() and include the headers in the outgoing HTTP request.
This commit is contained in:
parent
0639e77898
commit
c99e35485a
4 changed files with 33 additions and 1 deletions
|
@ -43,6 +43,7 @@ void XMLHttpRequestPrototype::initialize(JS::GlobalObject& global_object)
|
|||
Object::initialize(global_object);
|
||||
define_native_function("open", open, 2);
|
||||
define_native_function("send", send, 0);
|
||||
define_native_function("setRequestHeader", set_request_header, 2);
|
||||
define_native_property("readyState", ready_state_getter, nullptr, JS::Attribute::Enumerable | JS::Attribute::Configurable);
|
||||
define_native_property("responseText", response_text_getter, nullptr, JS::Attribute::Enumerable | JS::Attribute::Configurable);
|
||||
|
||||
|
@ -93,6 +94,21 @@ JS_DEFINE_NATIVE_FUNCTION(XMLHttpRequestPrototype::send)
|
|||
return JS::js_undefined();
|
||||
}
|
||||
|
||||
JS_DEFINE_NATIVE_FUNCTION(XMLHttpRequestPrototype::set_request_header)
|
||||
{
|
||||
auto* impl = impl_from(vm, global_object);
|
||||
if (!impl)
|
||||
return {};
|
||||
auto arg0 = vm.argument(0).to_string(global_object);
|
||||
if (vm.exception())
|
||||
return {};
|
||||
auto arg1 = vm.argument(1).to_string(global_object);
|
||||
if (vm.exception())
|
||||
return {};
|
||||
impl->set_request_header(arg0, arg1);
|
||||
return JS::js_undefined();
|
||||
}
|
||||
|
||||
JS_DEFINE_NATIVE_GETTER(XMLHttpRequestPrototype::ready_state_getter)
|
||||
{
|
||||
auto* impl = impl_from(vm, global_object);
|
||||
|
|
|
@ -41,6 +41,7 @@ public:
|
|||
private:
|
||||
JS_DECLARE_NATIVE_FUNCTION(open);
|
||||
JS_DECLARE_NATIVE_FUNCTION(send);
|
||||
JS_DECLARE_NATIVE_FUNCTION(set_request_header);
|
||||
|
||||
JS_DECLARE_NATIVE_GETTER(ready_state_getter);
|
||||
JS_DECLARE_NATIVE_GETTER(response_text_getter);
|
||||
|
|
|
@ -63,10 +63,16 @@ String XMLHttpRequest::response_text() const
|
|||
return String::copy(m_response);
|
||||
}
|
||||
|
||||
void XMLHttpRequest::set_request_header(const String& header, const String& value)
|
||||
{
|
||||
m_request_headers.set(header, value);
|
||||
}
|
||||
|
||||
void XMLHttpRequest::open(const String& method, const String& url)
|
||||
{
|
||||
m_method = method;
|
||||
m_url = url;
|
||||
m_request_headers.clear();
|
||||
set_ready_state(ReadyState::Opened);
|
||||
}
|
||||
|
||||
|
@ -88,10 +94,15 @@ void XMLHttpRequest::send()
|
|||
return;
|
||||
}
|
||||
|
||||
LoadRequest request;
|
||||
request.set_url(m_window->document().complete_url(m_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(
|
||||
m_window->document().complete_url(m_url),
|
||||
request,
|
||||
[weak_this = make_weak_ptr()](auto data, auto&) {
|
||||
if (!weak_this)
|
||||
return;
|
||||
|
|
|
@ -62,6 +62,8 @@ public:
|
|||
void open(const String& method, const String& url);
|
||||
void send();
|
||||
|
||||
void set_request_header(const String& header, const String& value);
|
||||
|
||||
private:
|
||||
virtual void ref_event_target() override { ref(); }
|
||||
virtual void unref_event_target() override { unref(); }
|
||||
|
@ -79,6 +81,8 @@ private:
|
|||
String m_method;
|
||||
String m_url;
|
||||
|
||||
HashMap<String, String, CaseInsensitiveStringTraits> m_request_headers;
|
||||
|
||||
ByteBuffer m_response;
|
||||
};
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue