Skip to content

Commit

Permalink
Browser: Add debug command to dump cookies
Browse files Browse the repository at this point in the history
Using document.cookie only lets the test page see the name/value pair;
the value returned will not included the parsed attributes.
  • Loading branch information
trflynn89 authored and awesomekling committed Apr 12, 2021
1 parent fc03f8d commit 5496d71
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 0 deletions.
27 changes: 27 additions & 0 deletions Userland/Applications/Browser/CookieJar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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>

Expand Down Expand Up @@ -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:https://" : 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
Expand Down
1 change: 1 addition & 0 deletions Userland/Applications/Browser/CookieJar.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
4 changes: 4 additions & 0 deletions Userland/Applications/Browser/Tab.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
1 change: 1 addition & 0 deletions Userland/Applications/Browser/Tab.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ class Tab final : public GUI::Widget {
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; }
Expand Down
4 changes: 4 additions & 0 deletions Userland/Applications/Browser/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 5496d71

Please sign in to comment.