mirror of
https://github.com/RGBCube/serenity
synced 2025-07-19 05:37:36 +00:00
LibWeb: Implement XMLHttpRequest.overrideMimeType
This allows you to ignore the Content-Type returned by the server and always parse the content as if it's the given MIME type. This will currently be used for allowing you to override the charset of text responses.
This commit is contained in:
parent
8cfeca5261
commit
4ccade42b7
3 changed files with 24 additions and 0 deletions
|
@ -291,4 +291,21 @@ String XMLHttpRequest::get_all_response_headers() const
|
|||
return builder.to_string();
|
||||
}
|
||||
|
||||
// https://xhr.spec.whatwg.org/#dom-xmlhttprequest-overridemimetype
|
||||
DOM::ExceptionOr<void> XMLHttpRequest::override_mime_type(String const& mime)
|
||||
{
|
||||
// 1. If this’s state is loading or done, then throw an "InvalidStateError" DOMException.
|
||||
if (m_ready_state == ReadyState::Loading || m_ready_state == ReadyState::Done)
|
||||
return DOM::InvalidStateError::create("Cannot override MIME type when state is Loading or Done.");
|
||||
|
||||
// 2. Set this’s override MIME type to the result of parsing mime.
|
||||
m_override_mime_type = MimeSniff::MimeType::from_string(mime);
|
||||
|
||||
// 3. If this’s override MIME type is failure, then set this’s override MIME type to application/octet-stream.
|
||||
if (!m_override_mime_type.has_value())
|
||||
m_override_mime_type = MimeSniff::MimeType("application"sv, "octet-stream"sv);
|
||||
|
||||
return {};
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -14,6 +14,7 @@
|
|||
#include <LibWeb/Bindings/Wrappable.h>
|
||||
#include <LibWeb/DOM/EventTarget.h>
|
||||
#include <LibWeb/DOM/ExceptionOr.h>
|
||||
#include <LibWeb/MimeSniff/MimeType.h>
|
||||
#include <LibWeb/XHR/XMLHttpRequestEventTarget.h>
|
||||
|
||||
namespace Web::XHR {
|
||||
|
@ -62,6 +63,8 @@ public:
|
|||
Bindings::CallbackType* onreadystatechange();
|
||||
void set_onreadystatechange(Optional<Bindings::CallbackType>);
|
||||
|
||||
DOM::ExceptionOr<void> override_mime_type(String const& mime);
|
||||
|
||||
private:
|
||||
virtual void ref_event_target() override { ref(); }
|
||||
virtual void unref_event_target() override { unref(); }
|
||||
|
@ -91,6 +94,9 @@ private:
|
|||
bool m_timed_out { false };
|
||||
|
||||
ByteBuffer m_response_object;
|
||||
|
||||
// https://xhr.spec.whatwg.org/#override-mime-type
|
||||
Optional<MimeSniff::MimeType> m_override_mime_type;
|
||||
};
|
||||
|
||||
}
|
||||
|
|
|
@ -18,6 +18,7 @@ interface XMLHttpRequest : XMLHttpRequestEventTarget {
|
|||
|
||||
ByteString? getResponseHeader(ByteString name);
|
||||
ByteString getAllResponseHeaders();
|
||||
undefined overrideMimeType(DOMString mime);
|
||||
|
||||
attribute EventHandler onreadystatechange;
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue