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 {
|
|
|
|
bool operator==(const CookieStorageKey&) const = default;
|
|
|
|
|
|
|
|
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);
|
2021-04-15 14:36:20 +00:00
|
|
|
void set_cookie(const URL& url, const Web::Cookie::ParsedCookie& parsed_cookie, Web::Cookie::Source source);
|
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);
|
2021-04-13 03:16:27 +00:00
|
|
|
static bool domain_matches(const String& string, const String& domain_string);
|
2021-04-14 14:18:13 +00:00
|
|
|
static bool path_matches(const String& request_path, const String& 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
|
|
|
|
2021-04-15 14:36:20 +00:00
|
|
|
void store_cookie(const Web::Cookie::ParsedCookie& parsed_cookie, const URL& url, String canonicalized_domain, Web::Cookie::Source source);
|
2021-06-08 15:06:27 +00:00
|
|
|
Vector<Web::Cookie::Cookie&> get_matching_cookies(const URL& url, const String& 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> {
|
|
|
|
static unsigned hash(const Browser::CookieStorageKey& 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;
|
|
|
|
}
|
2021-04-11 14:43:54 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|