ladybird/Userland/Applications/Browser/CookieJar.h

71 lines
2.8 KiB
C
Raw Normal View History

/*
* 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>
#include <AK/Vector.h>
#include <LibCore/DateTime.h>
namespace Browser {
struct Cookie {
String name;
String value;
Core::DateTime expiry_time {};
String domain {};
2021-04-11 23:48:59 -04:00
String path {};
bool secure { false };
bool http_only { false };
};
class CookieJar {
public:
String get_cookie(const URL& url) const;
void set_cookie(const URL& url, const String& cookie);
void dump_cookies() const;
private:
static Optional<String> canonicalize_domain(const URL& url);
2021-04-11 23:48:59 -04:00
static String default_path(const URL& url);
static Optional<Cookie> parse_cookie(const String& cookie_string, String default_domain, String default_path);
static void parse_attributes(Cookie& cookie, StringView unparsed_attributes);
static void process_attribute(Cookie& cookie, StringView attribute_name, StringView attribute_value);
static void on_expires_attribute(Cookie& cookie, StringView attribute_value);
static void on_max_age_attribute(Cookie& cookie, StringView attribute_value);
static void on_domain_attribute(Cookie& cookie, StringView attribute_value);
static void on_path_attribute(Cookie& cookie, StringView attribute_value);
static void on_secure_attribute(Cookie& cookie);
static void on_http_only_attribute(Cookie& cookie);
static Optional<Core::DateTime> parse_date_time(StringView date_string);
HashMap<String, Vector<Cookie>> m_cookies;
};
}