From 0d6dc2492d24a76be8f91b47f21a18d51850b660 Mon Sep 17 00:00:00 2001 From: Tobias Christiansen Date: Sun, 16 Oct 2022 19:55:01 +0200 Subject: [PATCH] WebDriver: Implement `DELETE /session/{id}/cookie` endpoint --- Userland/Services/WebDriver/Client.cpp | 12 +++++++++ Userland/Services/WebDriver/Client.h | 1 + Userland/Services/WebDriver/Session.cpp | 35 +++++++++++++++++++++++++ Userland/Services/WebDriver/Session.h | 3 +++ 4 files changed, 51 insertions(+) diff --git a/Userland/Services/WebDriver/Client.cpp b/Userland/Services/WebDriver/Client.cpp index be39c7656d..5105486b08 100644 --- a/Userland/Services/WebDriver/Client.cpp +++ b/Userland/Services/WebDriver/Client.cpp @@ -32,6 +32,7 @@ Vector Client::s_routes = { { HTTP::HttpRequest::Method::POST, { "session", ":session_id", "forward" }, &Client::handle_forward }, { HTTP::HttpRequest::Method::GET, { "session", ":session_id", "cookie" }, &Client::handle_get_all_cookies }, { HTTP::HttpRequest::Method::GET, { "session", ":session_id", "cookie", ":name" }, &Client::handle_get_named_cookie }, + { HTTP::HttpRequest::Method::DELETE, { "session", ":session_id", "cookie" }, &Client::handle_delete_all_cookies }, }; Client::Client(NonnullOwnPtr socket, Core::Object* parent) @@ -514,4 +515,15 @@ ErrorOr Client::handle_get_named_cookie(Vector return make_json_value(cookies); } +// DELETE /session/{session id}/cookie https://w3c.github.io/webdriver/#dfn-delete-all-cookies +ErrorOr Client::handle_delete_all_cookies(Vector parameters, JsonValue const&) +{ + dbgln_if(WEBDRIVER_DEBUG, "Handling DELETE /session//cookie"); + Session* session = TRY(find_session_with_id(parameters[0])); + + // NOTE: Spec steps handled in Session::delete_all_cookies(). + auto result = TRY(session->delete_all_cookies()); + return make_json_value(result); +} + } diff --git a/Userland/Services/WebDriver/Client.h b/Userland/Services/WebDriver/Client.h index ea01437024..88209927bc 100644 --- a/Userland/Services/WebDriver/Client.h +++ b/Userland/Services/WebDriver/Client.h @@ -58,6 +58,7 @@ private: ErrorOr handle_forward(Vector, JsonValue const& payload); ErrorOr handle_get_all_cookies(Vector, JsonValue const& payload); ErrorOr handle_get_named_cookie(Vector, JsonValue const& payload); + ErrorOr handle_delete_all_cookies(Vector, JsonValue const& payload); ErrorOr find_session_with_id(StringView session_id); JsonValue make_json_value(JsonValue const&); diff --git a/Userland/Services/WebDriver/Session.cpp b/Userland/Services/WebDriver/Session.cpp index 75d13b0261..a7dde357ae 100644 --- a/Userland/Services/WebDriver/Session.cpp +++ b/Userland/Services/WebDriver/Session.cpp @@ -297,4 +297,39 @@ ErrorOr Session::get_named_cookie(String const& name) return HttpError { 404, "no such cookie", "Cookie not found" }; } +// https://w3c.github.io/webdriver/#dfn-delete-cookies +void Session::delete_cookies(Optional const& name) +{ + // For each cookie among all associated cookies of the current browsing context’s active document, + // run the substeps of the first matching condition: + for (auto& cookie : m_browser_connection->get_all_cookies()) { + // -> name is undefined + // -> name is equal to cookie name + if (!name.has_value() || name.value() == cookie.name) { + // Set the cookie expiry time to a Unix timestamp in the past. + cookie.expiry_time = Core::DateTime::from_timestamp(0); + m_browser_connection->async_update_cookie(cookie); + } + // -> Otherwise + // Do nothing. + } +} + +// DELETE /session/{session id}/cookie https://w3c.github.io/webdriver/#dfn-delete-all-cookies +ErrorOr Session::delete_all_cookies() +{ + // 1. If the current browsing context is no longer open, return error with error code no such window. + auto current_window = get_window_object(); + if (!current_window.has_value()) + return HttpError { 404, "no such window", "Window not found" }; + + // FIXME: 2. Handle any user prompts, and return its value if it is an error. + + // 3. Delete cookies, giving no filtering argument. + delete_cookies(); + + // 4. Return success with data null. + return JsonValue(); +} + } diff --git a/Userland/Services/WebDriver/Session.h b/Userland/Services/WebDriver/Session.h index f15e48e3e0..802824e05a 100644 --- a/Userland/Services/WebDriver/Session.h +++ b/Userland/Services/WebDriver/Session.h @@ -42,8 +42,11 @@ public: ErrorOr forward(); ErrorOr get_all_cookies(); ErrorOr get_named_cookie(String const& name); + ErrorOr delete_all_cookies(); private: + void delete_cookies(Optional const& name = {}); + NonnullRefPtr m_client; bool m_started { false }; unsigned m_id { 0 };