2021-04-11 14:43:54 +00:00
|
|
|
/*
|
2022-01-31 18:07:22 +00:00
|
|
|
* Copyright (c) 2021, Tim Flynn <trflynn89@serenityos.org>
|
2021-04-11 14:43:54 +00:00
|
|
|
*
|
2021-04-22 08:24:48 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2021-04-11 14:43:54 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <AK/HashMap.h>
|
|
|
|
#include <AK/Optional.h>
|
|
|
|
#include <AK/String.h>
|
2021-04-13 03:16:27 +00:00
|
|
|
#include <AK/Traits.h>
|
2021-04-12 03:40:49 +00:00
|
|
|
#include <LibCore/DateTime.h>
|
2021-04-13 21:01:20 +00:00
|
|
|
#include <LibWeb/Cookie/Cookie.h>
|
2021-04-13 20:47:05 +00:00
|
|
|
#include <LibWeb/Forward.h>
|
2021-04-11 14:43:54 +00:00
|
|
|
|
|
|
|
namespace Browser {
|
|
|
|
|
2021-04-13 03:16:27 +00:00
|
|
|
struct CookieStorageKey {
|
2022-04-01 17:58:27 +00:00
|
|
|
bool operator==(CookieStorageKey const&) const = default;
|
2021-04-13 03:16:27 +00:00
|
|
|
|
|
|
|
String name;
|
|
|
|
String domain;
|
|
|
|
String path;
|
2021-04-11 14:43:54 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class CookieJar {
|
|
|
|
public:
|
2021-04-13 21:30:41 +00:00
|
|
|
String get_cookie(const URL& url, Web::Cookie::Source source);
|
2022-04-01 17:58:27 +00:00
|
|
|
void set_cookie(const URL& url, Web::Cookie::ParsedCookie const& parsed_cookie, Web::Cookie::Source source);
|
2022-10-16 17:48:19 +00:00
|
|
|
void update_cookie(URL const&, Web::Cookie::Cookie);
|
2021-04-12 16:12:13 +00:00
|
|
|
void dump_cookies() const;
|
2022-03-01 16:05:42 +00:00
|
|
|
Vector<Web::Cookie::Cookie> get_all_cookies() const;
|
2021-04-11 14:43:54 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
static Optional<String> canonicalize_domain(const URL& url);
|
2022-04-01 17:58:27 +00:00
|
|
|
static bool domain_matches(String const& string, String const& domain_string);
|
|
|
|
static bool path_matches(String const& request_path, String const& cookie_path);
|
2021-04-13 20:47:05 +00:00
|
|
|
static String default_path(const URL& url);
|
2021-04-13 03:16:27 +00:00
|
|
|
|
2022-04-01 17:58:27 +00:00
|
|
|
void store_cookie(Web::Cookie::ParsedCookie const& parsed_cookie, const URL& url, String canonicalized_domain, Web::Cookie::Source source);
|
|
|
|
Vector<Web::Cookie::Cookie&> get_matching_cookies(const URL& url, String const& canonicalized_domain, Web::Cookie::Source source);
|
2021-04-13 16:28:39 +00:00
|
|
|
void purge_expired_cookies();
|
2021-04-13 03:16:27 +00:00
|
|
|
|
2021-04-13 21:01:20 +00:00
|
|
|
HashMap<CookieStorageKey, Web::Cookie::Cookie> m_cookies;
|
2021-04-13 03:16:27 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace AK {
|
2021-04-11 14:43:54 +00:00
|
|
|
|
2021-04-13 03:16:27 +00:00
|
|
|
template<>
|
|
|
|
struct Traits<Browser::CookieStorageKey> : public GenericTraits<Browser::CookieStorageKey> {
|
2022-04-01 17:58:27 +00:00
|
|
|
static unsigned hash(Browser::CookieStorageKey const& key)
|
2021-04-13 03:16:27 +00:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
2021-04-11 14:43:54 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|