1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 06:17:35 +00:00

ProtocolServer: Pass HTTP response headers to the client

We now store the response headers in a download object on the protocol
server side and pass it to the client when finishing up a download.

Response headers are passed as an IPC::Dictionary. :^)
This commit is contained in:
Andreas Kling 2020-05-03 22:20:49 +02:00
parent a83d74b38c
commit eb6e35a1be
18 changed files with 49 additions and 30 deletions

View file

@ -68,7 +68,7 @@ ImageStyleValue::ImageStyleValue(const URL& url, Document& document)
, m_document(document.make_weak_ptr())
{
NonnullRefPtr<ImageStyleValue> protector(*this);
ResourceLoader::the().load(url, [this, protector](auto& data) {
ResourceLoader::the().load(url, [this, protector](auto& data, auto&) {
if (!m_document)
return;
m_bitmap = Gfx::load_png_from_memory(data.data(), data.size());

View file

@ -53,7 +53,7 @@ void HTMLImageElement::parse_attribute(const FlyString& name, const String& valu
void HTMLImageElement::load_image(const String& src)
{
URL src_url = document().complete_url(src);
ResourceLoader::the().load(src_url, [this, weak_element = make_weak_ptr()](auto data) {
ResourceLoader::the().load(src_url, [this, weak_element = make_weak_ptr()](auto data, auto&) {
if (!weak_element) {
dbg() << "HTMLImageElement: Load completed after element destroyed.";
return;

View file

@ -47,7 +47,7 @@ void HTMLLinkElement::inserted_into(Node&)
{
if (rel() == "stylesheet") {
URL url = document().complete_url(href());
ResourceLoader::the().load(url, [&](auto data) {
ResourceLoader::the().load(url, [&](auto data, auto&) {
if (data.is_null()) {
dbg() << "HTMLLinkElement: Failed to load stylesheet: " << href();
return;

View file

@ -82,7 +82,7 @@ void HTMLScriptElement::inserted_into(Node& new_parent)
}
String source;
ResourceLoader::the().load_sync(src_url, [&](auto& data) {
ResourceLoader::the().load_sync(src_url, [&](auto& data, auto&) {
if (data.is_null()) {
dbg() << "HTMLScriptElement: Failed to load " << src;
return;

View file

@ -72,7 +72,7 @@ void XMLHttpRequest::send()
// 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),
[weak_this = make_weak_ptr()](auto& data) {
[weak_this = make_weak_ptr()](auto& data, auto&) {
if (!weak_this)
return;
const_cast<XMLHttpRequest&>(*weak_this).m_response = data;

View file

@ -360,7 +360,7 @@ void HtmlView::load(const URL& url)
ResourceLoader::the().load(
url,
[this, url](auto data) {
[this, url](auto data, auto& response_headers) {
if (data.is_null()) {
load_error_page(url, "No data");
return;
@ -390,7 +390,7 @@ void HtmlView::load(const URL& url)
ResourceLoader::the().load(
favicon_url,
[this, favicon_url](auto data) {
[this, favicon_url](auto data, auto&) {
dbg() << "Favicon downloaded, " << data.size() << " bytes from " << favicon_url.to_string();
auto decoder = Gfx::ImageDecoder::create(data.data(), data.size());
auto bitmap = decoder->bitmap();
@ -412,7 +412,7 @@ void HtmlView::load_error_page(const URL& failed_url, const String& error)
auto error_page_url = "file:///res/html/error.html";
ResourceLoader::the().load(
error_page_url,
[this, failed_url, error](auto data) {
[this, failed_url, error](auto data, auto&) {
ASSERT(!data.is_null());
auto html = String::format(
String::copy(data).characters(),

View file

@ -47,14 +47,14 @@ ResourceLoader::ResourceLoader()
{
}
void ResourceLoader::load_sync(const URL& url, Function<void(const ByteBuffer&)> success_callback, Function<void(const String&)> error_callback)
void ResourceLoader::load_sync(const URL& url, Function<void(const ByteBuffer&, const HashMap<String, String>& response_headers)> success_callback, Function<void(const String&)> error_callback)
{
Core::EventLoop loop;
load(
url,
[&](auto& data) {
success_callback(data);
[&](auto& data, auto& response_headers) {
success_callback(data, response_headers);
loop.quit(0);
},
[&](auto& string) {
@ -66,7 +66,7 @@ void ResourceLoader::load_sync(const URL& url, Function<void(const ByteBuffer&)>
loop.exec();
}
void ResourceLoader::load(const URL& url, Function<void(const ByteBuffer&)> success_callback, Function<void(const String&)> error_callback)
void ResourceLoader::load(const URL& url, Function<void(const ByteBuffer&, const HashMap<String, String>& response_headers)> success_callback, Function<void(const String&)> error_callback)
{
if (is_port_blocked(url.port())) {
dbg() << "ResourceLoader::load: Error: blocked port " << url.port() << " for URL: " << url;
@ -83,7 +83,7 @@ void ResourceLoader::load(const URL& url, Function<void(const ByteBuffer&)> succ
data = url.data_payload().to_byte_buffer();
deferred_invoke([data = move(data), success_callback = move(success_callback)](auto&) {
success_callback(data);
success_callback(data, {});
});
return;
}
@ -100,7 +100,7 @@ void ResourceLoader::load(const URL& url, Function<void(const ByteBuffer&)> succ
auto data = f->read_all();
deferred_invoke([data = move(data), success_callback = move(success_callback)](auto&) {
success_callback(data);
success_callback(data, {});
});
return;
}
@ -112,7 +112,7 @@ void ResourceLoader::load(const URL& url, Function<void(const ByteBuffer&)> succ
error_callback("Failed to initiate load");
return;
}
download->on_finish = [this, success_callback = move(success_callback), error_callback = move(error_callback)](bool success, const ByteBuffer& payload, auto) {
download->on_finish = [this, success_callback = move(success_callback), error_callback = move(error_callback)](bool success, const ByteBuffer& payload, auto, auto& response_headers) {
--m_pending_loads;
if (on_load_counter_change)
on_load_counter_change();
@ -121,7 +121,7 @@ void ResourceLoader::load(const URL& url, Function<void(const ByteBuffer&)> succ
error_callback("HTTP load failed");
return;
}
success_callback(ByteBuffer::copy(payload.data(), payload.size()));
success_callback(ByteBuffer::copy(payload.data(), payload.size()), response_headers);
};
++m_pending_loads;
if (on_load_counter_change)

View file

@ -41,8 +41,8 @@ class ResourceLoader : public Core::Object {
public:
static ResourceLoader& the();
void load(const URL&, Function<void(const ByteBuffer&)> success_callback, Function<void(const String&)> error_callback = nullptr);
void load_sync(const URL&, Function<void(const ByteBuffer&)> success_callback, Function<void(const String&)> error_callback = nullptr);
void load(const URL&, Function<void(const ByteBuffer&, const HashMap<String, String>& response_headers)> success_callback, Function<void(const String&)> error_callback = nullptr);
void load_sync(const URL&, Function<void(const ByteBuffer&, const HashMap<String, String>& response_headers)> success_callback, Function<void(const String&)> error_callback = nullptr);
Function<void()> on_load_counter_change;