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