From a34f8c444bfbba2d723b00a7659284da1ef4ed15 Mon Sep 17 00:00:00 2001 From: Tobias Christiansen Date: Sat, 15 Oct 2022 19:22:20 +0200 Subject: [PATCH] WebDriver: Implement `GET /session/{id}/cookie/{name}` endpoint --- .../Browser/WebDriverConnection.cpp | 14 +++++++++++ .../Browser/WebDriverConnection.h | 1 + .../Browser/WebDriverSessionClient.ipc | 2 ++ Userland/Services/WebDriver/Client.cpp | 13 ++++++++++ Userland/Services/WebDriver/Client.h | 1 + Userland/Services/WebDriver/Session.cpp | 24 +++++++++++++++++++ Userland/Services/WebDriver/Session.h | 1 + 7 files changed, 56 insertions(+) diff --git a/Userland/Applications/Browser/WebDriverConnection.cpp b/Userland/Applications/Browser/WebDriverConnection.cpp index 8bcdcb76e7..aefae0f910 100644 --- a/Userland/Applications/Browser/WebDriverConnection.cpp +++ b/Userland/Applications/Browser/WebDriverConnection.cpp @@ -81,4 +81,18 @@ Messages::WebDriverSessionClient::GetAllCookiesResponse WebDriverConnection::get return { {} }; } +Messages::WebDriverSessionClient::GetNamedCookieResponse WebDriverConnection::get_named_cookie(String const& name) +{ + dbgln("WebDriverConnection: get_named_cookie"); + if (auto browser_window = m_browser_window.strong_ref()) { + if (browser_window->active_tab().on_get_cookies_entries) { + for (auto cookie : browser_window->active_tab().on_get_cookies_entries()) { + if (cookie.name == name) + return { cookie }; + } + } + } + return { {} }; +} + } diff --git a/Userland/Applications/Browser/WebDriverConnection.h b/Userland/Applications/Browser/WebDriverConnection.h index 17f394ef1c..d5ec25e689 100644 --- a/Userland/Applications/Browser/WebDriverConnection.h +++ b/Userland/Applications/Browser/WebDriverConnection.h @@ -44,6 +44,7 @@ public: virtual void back() override; virtual void forward() override; virtual Messages::WebDriverSessionClient::GetAllCookiesResponse get_all_cookies() override; + virtual Messages::WebDriverSessionClient::GetNamedCookieResponse get_named_cookie(String const& name) override; private: WebDriverConnection(NonnullOwnPtr socket, NonnullRefPtr browser_window); diff --git a/Userland/Applications/Browser/WebDriverSessionClient.ipc b/Userland/Applications/Browser/WebDriverSessionClient.ipc index 974fc1cc49..291a74beb6 100644 --- a/Userland/Applications/Browser/WebDriverSessionClient.ipc +++ b/Userland/Applications/Browser/WebDriverSessionClient.ipc @@ -12,4 +12,6 @@ endpoint WebDriverSessionClient { back() =| forward() =| get_all_cookies() => (Vector cookies) + get_named_cookie(String name) => (Optional cookie) + } diff --git a/Userland/Services/WebDriver/Client.cpp b/Userland/Services/WebDriver/Client.cpp index 8974cdd568..be39c7656d 100644 --- a/Userland/Services/WebDriver/Client.cpp +++ b/Userland/Services/WebDriver/Client.cpp @@ -31,6 +31,7 @@ Vector Client::s_routes = { { HTTP::HttpRequest::Method::POST, { "session", ":session_id", "back" }, &Client::handle_back }, { 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 }, }; Client::Client(NonnullOwnPtr socket, Core::Object* parent) @@ -501,4 +502,16 @@ ErrorOr Client::handle_get_all_cookies(Vector return make_json_value(cookies); } +// GET /session/{session id}/cookie/{name} https://w3c.github.io/webdriver/#dfn-get-named-cookie +ErrorOr Client::handle_get_named_cookie(Vector parameters, JsonValue const&) +{ + dbgln_if(WEBDRIVER_DEBUG, "Handling GET /session//cookie/"); + Session* session = TRY(find_session_with_id(parameters[0])); + + // NOTE: Spec steps handled in Session::get_all_cookies(). + auto cookies = TRY(session->get_named_cookie(parameters[1])); + + return make_json_value(cookies); +} + } diff --git a/Userland/Services/WebDriver/Client.h b/Userland/Services/WebDriver/Client.h index b8d5ba9ad2..ea01437024 100644 --- a/Userland/Services/WebDriver/Client.h +++ b/Userland/Services/WebDriver/Client.h @@ -57,6 +57,7 @@ private: ErrorOr handle_back(Vector, JsonValue const& payload); 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 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 02a4c32951..75d13b0261 100644 --- a/Userland/Services/WebDriver/Session.cpp +++ b/Userland/Services/WebDriver/Session.cpp @@ -273,4 +273,28 @@ ErrorOr Session::get_all_cookies() return JsonValue(cookies); } +// GET /session/{session id}/cookie/{name} https://w3c.github.io/webdriver/#dfn-get-named-cookie +ErrorOr Session::get_named_cookie(String const& name) +{ + + // 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. If the url variable name is equal to a cookie’s cookie name amongst all associated cookies of the + // current browsing context’s active document, return success with the serialized cookie as data. + auto maybe_cookie = m_browser_connection->get_named_cookie(name); + if (maybe_cookie.has_value()) { + auto cookie = maybe_cookie.release_value(); + auto serialized_cookie = serialize_cookie(cookie); + return JsonValue(serialized_cookie); + } + + // 4. Otherwise, return error with error code no such cookie. + return HttpError { 404, "no such cookie", "Cookie not found" }; +} + } diff --git a/Userland/Services/WebDriver/Session.h b/Userland/Services/WebDriver/Session.h index f34bc37b11..f15e48e3e0 100644 --- a/Userland/Services/WebDriver/Session.h +++ b/Userland/Services/WebDriver/Session.h @@ -41,6 +41,7 @@ public: ErrorOr back(); ErrorOr forward(); ErrorOr get_all_cookies(); + ErrorOr get_named_cookie(String const& name); private: NonnullRefPtr m_client;