mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 14:37:45 +00:00
Browser: Store cookies in a LibSQL database :^)
Currently, cookies are ephemeral and only survive for the lifetime of Browser instance. This will make Browser instead store cookies in a SQL database for persisted access.
This commit is contained in:
parent
bf060adcf9
commit
fef255ee22
6 changed files with 513 additions and 121 deletions
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright (c) 2021, Tim Flynn <trflynn89@serenityos.org>
|
||||
* Copyright (c) 2021-2022, Tim Flynn <trflynn89@serenityos.org>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
@ -7,34 +7,42 @@
|
|||
#pragma once
|
||||
|
||||
#include <AK/DeprecatedString.h>
|
||||
#include <AK/Function.h>
|
||||
#include <AK/HashMap.h>
|
||||
#include <AK/Optional.h>
|
||||
#include <AK/Traits.h>
|
||||
#include <LibCore/DateTime.h>
|
||||
#include <LibSQL/Type.h>
|
||||
#include <LibWeb/Cookie/Cookie.h>
|
||||
#include <LibWeb/Forward.h>
|
||||
|
||||
namespace Browser {
|
||||
|
||||
struct CookieStorageKey {
|
||||
bool operator==(CookieStorageKey const&) const = default;
|
||||
|
||||
DeprecatedString name;
|
||||
DeprecatedString domain;
|
||||
DeprecatedString path;
|
||||
};
|
||||
class Database;
|
||||
|
||||
class CookieJar {
|
||||
struct Statements {
|
||||
SQL::StatementID create_table { 0 };
|
||||
SQL::StatementID insert_cookie { 0 };
|
||||
SQL::StatementID update_cookie { 0 };
|
||||
SQL::StatementID expire_cookie { 0 };
|
||||
SQL::StatementID select_cookie { 0 };
|
||||
SQL::StatementID select_all_cookies { 0 };
|
||||
};
|
||||
|
||||
public:
|
||||
static ErrorOr<CookieJar> create(Database& database);
|
||||
|
||||
DeprecatedString get_cookie(const URL& url, Web::Cookie::Source source);
|
||||
void set_cookie(const URL& url, Web::Cookie::ParsedCookie const& parsed_cookie, Web::Cookie::Source source);
|
||||
void update_cookie(Web::Cookie::Cookie);
|
||||
void dump_cookies() const;
|
||||
Vector<Web::Cookie::Cookie> get_all_cookies() const;
|
||||
void dump_cookies();
|
||||
Vector<Web::Cookie::Cookie> get_all_cookies();
|
||||
Vector<Web::Cookie::Cookie> get_all_cookies(URL const& url);
|
||||
Optional<Web::Cookie::Cookie> get_named_cookie(URL const& url, DeprecatedString const& name);
|
||||
|
||||
private:
|
||||
CookieJar(Database& database, Statements statements);
|
||||
|
||||
static Optional<DeprecatedString> canonicalize_domain(const URL& url);
|
||||
static bool domain_matches(DeprecatedString const& string, DeprecatedString const& domain_string);
|
||||
static bool path_matches(DeprecatedString const& request_path, DeprecatedString const& cookie_path);
|
||||
|
@ -46,26 +54,22 @@ private:
|
|||
};
|
||||
|
||||
void store_cookie(Web::Cookie::ParsedCookie const& parsed_cookie, const URL& url, DeprecatedString canonicalized_domain, Web::Cookie::Source source);
|
||||
Vector<Web::Cookie::Cookie&> get_matching_cookies(const URL& url, DeprecatedString const& canonicalized_domain, Web::Cookie::Source source, MatchingCookiesSpecMode mode = MatchingCookiesSpecMode::RFC6265);
|
||||
Vector<Web::Cookie::Cookie> get_matching_cookies(const URL& url, DeprecatedString const& canonicalized_domain, Web::Cookie::Source source, MatchingCookiesSpecMode mode = MatchingCookiesSpecMode::RFC6265);
|
||||
|
||||
void insert_cookie_into_database(Web::Cookie::Cookie const& cookie);
|
||||
void update_cookie_in_database(Web::Cookie::Cookie const& cookie);
|
||||
|
||||
using OnCookieFound = Function<void(Web::Cookie::Cookie&, Web::Cookie::Cookie)>;
|
||||
using OnCookieNotFound = Function<void(Web::Cookie::Cookie)>;
|
||||
void select_cookie_from_database(Web::Cookie::Cookie cookie, OnCookieFound on_result, OnCookieNotFound on_complete_without_results);
|
||||
|
||||
using OnSelectAllCookiesResult = Function<void(Web::Cookie::Cookie)>;
|
||||
void select_all_cookies_from_database(OnSelectAllCookiesResult on_result);
|
||||
|
||||
void purge_expired_cookies();
|
||||
|
||||
HashMap<CookieStorageKey, Web::Cookie::Cookie> m_cookies;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
namespace AK {
|
||||
|
||||
template<>
|
||||
struct Traits<Browser::CookieStorageKey> : public GenericTraits<Browser::CookieStorageKey> {
|
||||
static unsigned hash(Browser::CookieStorageKey const& key)
|
||||
{
|
||||
unsigned hash = 0;
|
||||
hash = pair_int_hash(hash, string_hash(key.name.characters(), key.name.length()));
|
||||
hash = pair_int_hash(hash, string_hash(key.domain.characters(), key.domain.length()));
|
||||
hash = pair_int_hash(hash, string_hash(key.path.characters(), key.path.length()));
|
||||
return hash;
|
||||
}
|
||||
Database& m_database;
|
||||
Statements m_statements;
|
||||
};
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue