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
|
|
|
|
|
2022-12-04 18:02:33 +00:00
|
|
|
#include <AK/DeprecatedString.h>
|
2021-04-11 10:43:54 -04:00
|
|
|
#include <AK/HashMap.h>
|
|
|
|
#include <AK/Optional.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
|
|
|
|
2022-12-04 18:02:33 +00:00
|
|
|
DeprecatedString name;
|
|
|
|
DeprecatedString domain;
|
|
|
|
DeprecatedString path;
|
2021-04-11 10:43:54 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
class CookieJar {
|
|
|
|
public:
|
2022-12-04 18:02:33 +00:00
|
|
|
DeprecatedString 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-11-28 11:24:04 -05:00
|
|
|
void update_cookie(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;
|
2022-11-11 09:24:07 -05:00
|
|
|
Vector<Web::Cookie::Cookie> get_all_cookies(URL const& url);
|
2022-12-04 18:02:33 +00:00
|
|
|
Optional<Web::Cookie::Cookie> get_named_cookie(URL const& url, DeprecatedString const& name);
|
2021-04-11 10:43:54 -04:00
|
|
|
|
|
|
|
private:
|
2022-12-04 18:02:33 +00:00
|
|
|
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);
|
|
|
|
static DeprecatedString default_path(const URL& url);
|
2021-04-12 23:16:27 -04:00
|
|
|
|
2022-11-11 09:24:07 -05:00
|
|
|
enum class MatchingCookiesSpecMode {
|
|
|
|
RFC6265,
|
|
|
|
WebDriver,
|
|
|
|
};
|
|
|
|
|
2022-12-04 18:02:33 +00:00
|
|
|
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);
|
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
|
|
|
};
|
|
|
|
|
|
|
|
}
|