Browser: Add debug command to dump cookies

Using document.cookie only lets the test page see the name/value pair;
the value returned will not included the parsed attributes.
This commit is contained in:
Timothy Flynn 2021-04-12 12:12:13 -04:00 committed by Andreas Kling
parent fc03f8d959
commit 5496d71e4a
Notes: sideshowbarker 2024-07-18 20:27:45 +09:00
5 changed files with 37 additions and 0 deletions

View File

@ -27,6 +27,7 @@
#include "CookieJar.h"
#include <AK/AllOf.h>
#include <AK/NumericLimits.h>
#include <AK/StringBuilder.h>
#include <AK/URL.h>
#include <ctype.h>
@ -77,6 +78,32 @@ void CookieJar::set_cookie(const URL& url, const String& cookie_string)
it->value.append(move(*new_cookie));
}
void CookieJar::dump_cookies() const
{
static const char* url_color = "\033[34;1m";
static const char* cookie_color = "\033[31m";
static const char* attribute_color = "\033[33m";
static const char* no_color = "\033[0m";
StringBuilder builder;
builder.appendff("{} URLs with cookies\n", m_cookies.size());
for (const auto& url_and_cookies : m_cookies) {
builder.appendff("{}Cookies for:{} {}\n", url_color, no_color, url_and_cookies.key.is_empty() ? "file://" : url_and_cookies.key);
for (const auto& cookie : url_and_cookies.value) {
builder.appendff("\t{}{}{} = {}{}{}\n", cookie_color, cookie.name, no_color, cookie_color, cookie.value, no_color);
builder.appendff("\t\t{}Expiry{} = {}\n", attribute_color, no_color, cookie.expiry_time.to_string());
builder.appendff("\t\t{}Domain{} = {}\n", attribute_color, no_color, cookie.domain);
builder.appendff("\t\t{}Path{} = {}\n", attribute_color, no_color, cookie.path);
builder.appendff("\t\t{}Secure{} = {:s}\n", attribute_color, no_color, cookie.secure);
builder.appendff("\t\t{}HttpOnly{} = {:s}\n", attribute_color, no_color, cookie.http_only);
}
}
dbgln("{}", builder.build());
}
Optional<String> CookieJar::canonicalize_domain(const URL& url)
{
// https://tools.ietf.org/html/rfc6265#section-5.1.2

View File

@ -48,6 +48,7 @@ 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);

View File

@ -440,6 +440,10 @@ Tab::Tab(Type type)
debug_menu.add_action(GUI::Action::create("Dump &History", { Mod_Ctrl, Key_H }, [&](auto&) {
m_history.dump();
}));
debug_menu.add_action(GUI::Action::create("Dump C&ookies", [&](auto&) {
if (on_dump_cookies)
on_dump_cookies();
}));
debug_menu.add_separator();
auto line_box_borders_action = GUI::Action::create_checkable(
"&Line Box Borders", [this](auto& action) {

View File

@ -72,6 +72,7 @@ public:
Function<void(const Gfx::Bitmap&)> on_favicon_change;
Function<String(const URL& url)> on_get_cookie;
Function<void(const URL& url, const String& cookie)> on_set_cookie;
Function<void()> on_dump_cookies;
const String& title() const { return m_title; }
const Gfx::Bitmap* icon() const { return m_icon; }

View File

@ -227,6 +227,10 @@ int main(int argc, char** argv)
cookie_jar.set_cookie(url, cookie);
};
new_tab.on_dump_cookies = [&]() {
cookie_jar.dump_cookies();
};
new_tab.load(url);
dbgln("Added new tab {:p}, loading {}", &new_tab, url);