2021-04-11 10:43:54 -04:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2021, Tim Flynn <trflynn89@pm.me>
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright notice, this
|
|
|
|
* list of conditions and the following disclaimer.
|
|
|
|
*
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer in the documentation
|
|
|
|
* and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
|
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
|
|
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
|
|
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#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-11 10:43:54 -04:00
|
|
|
|
|
|
|
namespace Browser {
|
|
|
|
|
|
|
|
struct Cookie {
|
|
|
|
String name;
|
|
|
|
String value;
|
2021-04-12 23:16:27 -04:00
|
|
|
Core::DateTime creation_time {};
|
|
|
|
Core::DateTime last_access_time {};
|
2021-04-11 23:40:49 -04:00
|
|
|
Core::DateTime expiry_time {};
|
2021-04-11 23:47:43 -04:00
|
|
|
String domain {};
|
2021-04-11 23:48:59 -04:00
|
|
|
String path {};
|
2021-04-11 23:49:33 -04:00
|
|
|
bool secure { false };
|
|
|
|
bool http_only { false };
|
2021-04-12 23:16:27 -04:00
|
|
|
bool host_only { false };
|
|
|
|
bool persistent { false };
|
|
|
|
};
|
|
|
|
|
|
|
|
struct ParsedCookie;
|
|
|
|
|
|
|
|
struct CookieStorageKey {
|
|
|
|
bool operator==(const CookieStorageKey&) const = default;
|
|
|
|
|
|
|
|
String name;
|
|
|
|
String domain;
|
|
|
|
String path;
|
2021-04-11 10:43:54 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
class CookieJar {
|
|
|
|
public:
|
2021-04-13 12:28:39 -04:00
|
|
|
String get_cookie(const URL& url);
|
2021-04-11 10:43:54 -04:00
|
|
|
void set_cookie(const URL& url, const String& cookie);
|
2021-04-12 12:12:13 -04:00
|
|
|
void dump_cookies() const;
|
2021-04-11 10:43:54 -04:00
|
|
|
|
|
|
|
private:
|
|
|
|
static Optional<String> canonicalize_domain(const URL& url);
|
2021-04-11 23:48:59 -04:00
|
|
|
static String default_path(const URL& url);
|
2021-04-12 23:16:27 -04:00
|
|
|
static Optional<ParsedCookie> parse_cookie(const String& cookie_string);
|
|
|
|
static void parse_attributes(ParsedCookie& parsed_cookie, StringView unparsed_attributes);
|
|
|
|
static void process_attribute(ParsedCookie& parsed_cookie, StringView attribute_name, StringView attribute_value);
|
|
|
|
static void on_expires_attribute(ParsedCookie& parsed_cookie, StringView attribute_value);
|
|
|
|
static void on_max_age_attribute(ParsedCookie& parsed_cookie, StringView attribute_value);
|
|
|
|
static void on_domain_attribute(ParsedCookie& parsed_cookie, StringView attribute_value);
|
|
|
|
static void on_path_attribute(ParsedCookie& parsed_cookie, StringView attribute_value);
|
|
|
|
static void on_secure_attribute(ParsedCookie& parsed_cookie);
|
|
|
|
static void on_http_only_attribute(ParsedCookie& parsed_cookie);
|
2021-04-12 11:38:48 -04:00
|
|
|
static Optional<Core::DateTime> parse_date_time(StringView date_string);
|
2021-04-12 23:16:27 -04:00
|
|
|
static bool domain_matches(const String& string, const String& domain_string);
|
|
|
|
|
|
|
|
void store_cookie(ParsedCookie& parsed_cookie, const URL& url, String canonicalized_domain);
|
2021-04-13 12:28:39 -04:00
|
|
|
void purge_expired_cookies();
|
2021-04-12 23:16:27 -04:00
|
|
|
|
|
|
|
HashMap<CookieStorageKey, Cookie> m_cookies;
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
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> {
|
|
|
|
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 10:43:54 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|