2020-06-01 21:58:29 +02:00
|
|
|
/*
|
2024-10-04 13:19:50 +02:00
|
|
|
* Copyright (c) 2020, Andreas Kling <andreas@ladybird.org>
|
2020-06-01 21:58:29 +02:00
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-06-01 21:58:29 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
2020-09-28 11:52:59 +02:00
|
|
|
#include <AK/ByteBuffer.h>
|
|
|
|
|
#include <AK/HashMap.h>
|
2021-09-11 21:15:15 -07:00
|
|
|
#include <AK/Time.h>
|
|
|
|
|
#include <LibCore/ElapsedTimer.h>
|
2024-03-18 16:22:27 +13:00
|
|
|
#include <LibURL/URL.h>
|
2020-06-01 21:58:29 +02:00
|
|
|
#include <LibWeb/Forward.h>
|
2022-02-26 17:50:31 +01:00
|
|
|
#include <LibWeb/Page/Page.h>
|
2020-06-01 21:58:29 +02:00
|
|
|
|
|
|
|
|
namespace Web {
|
|
|
|
|
|
|
|
|
|
class LoadRequest {
|
|
|
|
|
public:
|
2024-05-26 07:52:39 -04:00
|
|
|
LoadRequest();
|
2020-06-01 21:58:29 +02:00
|
|
|
|
2024-03-18 16:22:27 +13:00
|
|
|
static LoadRequest create_for_url_on_page(const URL::URL& url, Page* page);
|
2021-04-14 10:34:18 -04:00
|
|
|
|
2023-03-15 12:28:33 +01:00
|
|
|
// The main resource is the file being displayed in a frame (unlike subresources like images, scripts, etc.)
|
|
|
|
|
// If a main resource fails with an HTTP error, we may still display its content if non-empty, e.g a custom 404 page.
|
|
|
|
|
bool is_main_resource() const { return m_main_resource; }
|
|
|
|
|
void set_main_resource(bool b) { m_main_resource = b; }
|
|
|
|
|
|
2025-04-19 16:59:36 +12:00
|
|
|
bool is_valid() const { return m_url.has_value(); }
|
2020-06-01 21:58:29 +02:00
|
|
|
|
2024-05-26 07:52:39 -04:00
|
|
|
int id() const { return m_id; }
|
|
|
|
|
|
2025-04-19 16:59:36 +12:00
|
|
|
Optional<URL::URL> const& url() const { return m_url; }
|
|
|
|
|
void set_url(Optional<URL::URL> url) { m_url = move(url); }
|
2020-06-01 21:58:29 +02:00
|
|
|
|
2023-12-16 17:49:34 +03:30
|
|
|
ByteString const& method() const { return m_method; }
|
|
|
|
|
void set_method(ByteString const& method) { m_method = method; }
|
2020-09-28 11:52:59 +02:00
|
|
|
|
2022-04-01 20:58:27 +03:00
|
|
|
ByteBuffer const& body() const { return m_body; }
|
2022-10-15 21:49:31 +02:00
|
|
|
void set_body(ByteBuffer body) { m_body = move(body); }
|
2020-09-28 11:52:59 +02:00
|
|
|
|
2023-07-07 22:48:11 -04:00
|
|
|
void start_timer() { m_load_timer.start(); }
|
2024-07-16 23:45:44 -06:00
|
|
|
AK::Duration load_time() const { return m_load_timer.elapsed_time(); }
|
2021-09-11 21:15:15 -07:00
|
|
|
|
2024-11-15 04:01:23 +13:00
|
|
|
GC::Ptr<Page> page() const { return m_page.ptr(); }
|
2022-02-26 17:50:31 +01:00
|
|
|
void set_page(Page& page) { m_page = page; }
|
|
|
|
|
|
2020-09-28 11:52:59 +02:00
|
|
|
unsigned hash() const
|
|
|
|
|
{
|
2022-04-01 20:58:27 +03:00
|
|
|
auto body_hash = string_hash((char const*)m_body.data(), m_body.size());
|
2021-09-11 20:18:53 -07:00
|
|
|
auto body_and_headers_hash = pair_int_hash(body_hash, m_headers.hash());
|
2025-04-19 16:59:36 +12:00
|
|
|
auto url_hash = m_url.has_value() ? m_url->to_byte_string().hash() : 0;
|
|
|
|
|
auto url_and_method_hash = pair_int_hash(url_hash, m_method.hash());
|
2021-09-11 20:18:53 -07:00
|
|
|
return pair_int_hash(body_and_headers_hash, url_and_method_hash);
|
2020-09-28 11:52:59 +02:00
|
|
|
}
|
2020-06-01 21:58:29 +02:00
|
|
|
|
2022-04-01 20:58:27 +03:00
|
|
|
bool operator==(LoadRequest const& other) const
|
2020-06-01 21:58:29 +02:00
|
|
|
{
|
2020-09-28 11:52:59 +02:00
|
|
|
if (m_headers.size() != other.m_headers.size())
|
|
|
|
|
return false;
|
2022-10-14 04:57:12 +08:00
|
|
|
for (auto const& it : m_headers) {
|
2020-09-28 11:52:59 +02:00
|
|
|
auto jt = other.m_headers.find(it.key);
|
|
|
|
|
if (jt == other.m_headers.end())
|
|
|
|
|
return false;
|
2020-09-28 17:34:51 +02:00
|
|
|
if (it.value != jt->value)
|
|
|
|
|
return false;
|
2020-09-28 11:52:59 +02:00
|
|
|
}
|
|
|
|
|
return m_url == other.m_url && m_method == other.m_method && m_body == other.m_body;
|
2020-06-01 21:58:29 +02:00
|
|
|
}
|
|
|
|
|
|
2023-12-16 17:49:34 +03:30
|
|
|
void set_header(ByteString const& name, ByteString const& value) { m_headers.set(name, value); }
|
|
|
|
|
ByteString header(ByteString const& name) const { return m_headers.get(name).value_or({}); }
|
2020-09-28 11:52:59 +02:00
|
|
|
|
2023-12-16 17:49:34 +03:30
|
|
|
HashMap<ByteString, ByteString, CaseInsensitiveStringTraits> const& headers() const { return m_headers; }
|
2020-09-28 11:52:59 +02:00
|
|
|
|
2020-06-01 21:58:29 +02:00
|
|
|
private:
|
2024-05-26 07:52:39 -04:00
|
|
|
int m_id { 0 };
|
2025-04-19 16:59:36 +12:00
|
|
|
Optional<URL::URL> m_url;
|
2023-12-16 17:49:34 +03:30
|
|
|
ByteString m_method { "GET" };
|
|
|
|
|
HashMap<ByteString, ByteString, CaseInsensitiveStringTraits> m_headers;
|
2020-09-28 11:52:59 +02:00
|
|
|
ByteBuffer m_body;
|
2021-09-11 21:15:15 -07:00
|
|
|
Core::ElapsedTimer m_load_timer;
|
2024-11-15 04:01:23 +13:00
|
|
|
GC::Root<Page> m_page;
|
2023-03-15 12:28:33 +01:00
|
|
|
bool m_main_resource { false };
|
2020-06-01 21:58:29 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
namespace AK {
|
|
|
|
|
|
|
|
|
|
template<>
|
2023-11-08 20:29:12 +01:00
|
|
|
struct Traits<Web::LoadRequest> : public DefaultTraits<Web::LoadRequest> {
|
2022-04-01 20:58:27 +03:00
|
|
|
static unsigned hash(Web::LoadRequest const& request) { return request.hash(); }
|
2020-06-01 21:58:29 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
}
|