2020-06-01 21:58:29 +02:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
|
*
|
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>
|
2020-06-01 21:58:29 +02:00
|
|
|
#include <AK/URL.h>
|
2021-09-11 21:15:15 -07:00
|
|
|
#include <LibCore/ElapsedTimer.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:
|
|
|
|
|
LoadRequest()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-13 00:33:23 +03:00
|
|
|
static LoadRequest create_for_url_on_page(const AK::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; }
|
|
|
|
|
|
2020-06-01 21:58:29 +02:00
|
|
|
bool is_valid() const { return m_url.is_valid(); }
|
|
|
|
|
|
2021-09-13 00:33:23 +03:00
|
|
|
const AK::URL& url() const { return m_url; }
|
|
|
|
|
void set_url(const AK::URL& url) { m_url = url; }
|
2020-06-01 21:58:29 +02:00
|
|
|
|
2022-12-04 18:02:33 +00:00
|
|
|
DeprecatedString const& method() const { return m_method; }
|
|
|
|
|
void set_method(DeprecatedString 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(); }
|
2023-03-13 16:30:34 +01:00
|
|
|
Duration load_time() const { return m_load_timer.elapsed_time(); }
|
2021-09-11 21:15:15 -07:00
|
|
|
|
2023-12-11 12:54:32 +01:00
|
|
|
JS::GCPtr<Page> page() { 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());
|
2022-12-06 01:12:49 +00:00
|
|
|
auto url_and_method_hash = pair_int_hash(m_url.to_deprecated_string().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
|
|
|
}
|
|
|
|
|
|
2022-12-04 18:02:33 +00:00
|
|
|
void set_header(DeprecatedString const& name, DeprecatedString const& value) { m_headers.set(name, value); }
|
|
|
|
|
DeprecatedString header(DeprecatedString const& name) const { return m_headers.get(name).value_or({}); }
|
2020-09-28 11:52:59 +02:00
|
|
|
|
2022-12-04 18:02:33 +00:00
|
|
|
HashMap<DeprecatedString, DeprecatedString, CaseInsensitiveStringTraits> const& headers() const { return m_headers; }
|
2020-09-28 11:52:59 +02:00
|
|
|
|
2020-06-01 21:58:29 +02:00
|
|
|
private:
|
2021-09-13 00:33:23 +03:00
|
|
|
AK::URL m_url;
|
2022-12-04 18:02:33 +00:00
|
|
|
DeprecatedString m_method { "GET" };
|
|
|
|
|
HashMap<DeprecatedString, DeprecatedString, 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;
|
2023-12-11 12:54:32 +01:00
|
|
|
JS::Handle<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
|
|
|
};
|
|
|
|
|
|
|
|
|
|
}
|