WebDriver: Implement GET /session/{id}/cookie endpoint

This commit is contained in:
Tobias Christiansen 2022-10-15 14:08:07 +02:00 committed by Linus Groh
parent 2c808958b9
commit 001699f2d6
Notes: sideshowbarker 2024-07-17 05:44:08 +09:00
8 changed files with 82 additions and 3 deletions

View file

@ -1,12 +1,15 @@
/*
* Copyright (c) 2022, Florent Castelli <florent.castelli@gmail.com>
* Copyright (c) 2022, Sam Atkins <atkinssj@serenityos.org>
* Copyright (c) 2022, Tobias Christiansen <tobyase@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include "WebDriverConnection.h"
#include "BrowserWindow.h"
#include <AK/Vector.h>
#include <LibWeb/Cookie/Cookie.h>
namespace Browser {
@ -67,4 +70,15 @@ void WebDriverConnection::forward()
browser_window->active_tab().go_forward();
}
Messages::WebDriverSessionClient::GetAllCookiesResponse WebDriverConnection::get_all_cookies()
{
dbgln("WebDriverConnection: get_cookies");
if (auto browser_window = m_browser_window.strong_ref()) {
if (browser_window->active_tab().on_get_cookies_entries) {
return { browser_window->active_tab().on_get_cookies_entries() };
}
}
return { {} };
}
}

View file

@ -1,6 +1,7 @@
/*
* Copyright (c) 2022, Florent Castelli <florent.castelli@gmail.com>
* Copyright (c) 2022, Sam Atkins <atkinssj@serenityos.org>
* Copyright (c) 2022, Tobias Christiansen <tobyase@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
@ -42,6 +43,7 @@ public:
virtual void refresh() override;
virtual void back() override;
virtual void forward() override;
virtual Messages::WebDriverSessionClient::GetAllCookiesResponse get_all_cookies() override;
private:
WebDriverConnection(NonnullOwnPtr<Core::Stream::LocalSocket> socket, NonnullRefPtr<BrowserWindow> browser_window);

View file

@ -1,4 +1,6 @@
#include <AK/URL.h>
#include <AK/Vector.h>
#include <LibWeb/Cookie/Cookie.h>
endpoint WebDriverSessionClient {
quit() =|
@ -9,4 +11,5 @@ endpoint WebDriverSessionClient {
refresh() =|
back() =|
forward() =|
get_all_cookies() => (Vector<Web::Cookie::Cookie> cookies)
}

View file

@ -8,12 +8,12 @@ set(SOURCES
Client.cpp
Session.cpp
main.cpp
)
)
set(GENERATED_SOURCES
../../Applications/Browser/WebDriverSessionClientEndpoint.h
../../Applications/Browser/WebDriverSessionServerEndpoint.h
)
)
serenity_bin(WebDriver)
target_link_libraries(WebDriver LibCore LibHTTP LibMain LibIPC)
target_link_libraries(WebDriver LibCore LibHTTP LibMain LibIPC LibWeb)

View file

@ -1,6 +1,7 @@
/*
* Copyright (c) 2022, Florent Castelli <florent.castelli@gmail.com>
* Copyright (c) 2022, Sam Atkins <atkinssj@serenityos.org>
* Copyright (c) 2022, Tobias Christiansen <tobyase@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
@ -29,6 +30,7 @@ Vector<Client::Route> Client::s_routes = {
{ HTTP::HttpRequest::Method::POST, { "session", ":session_id", "refresh" }, &Client::handle_refresh },
{ 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 },
};
Client::Client(NonnullOwnPtr<Core::Stream::BufferedTCPSocket> socket, Core::Object* parent)
@ -487,4 +489,16 @@ ErrorOr<JsonValue, HttpError> Client::handle_forward(Vector<StringView> paramete
return make_json_value(result);
}
// GET /session/{session id}/cookie https://w3c.github.io/webdriver/#dfn-get-all-cookies
ErrorOr<JsonValue, HttpError> Client::handle_get_all_cookies(Vector<StringView> parameters, JsonValue const&)
{
dbgln_if(WEBDRIVER_DEBUG, "Handling GET /session/<session_id>/cookie");
Session* session = TRY(find_session_with_id(parameters[0]));
// NOTE: Spec steps handled in Session::get_all_cookies().
auto cookies = TRY(session->get_all_cookies());
return make_json_value(cookies);
}
}

View file

@ -56,6 +56,7 @@ private:
ErrorOr<JsonValue, HttpError> handle_refresh(Vector<StringView>, JsonValue const& payload);
ErrorOr<JsonValue, HttpError> handle_back(Vector<StringView>, JsonValue const& payload);
ErrorOr<JsonValue, HttpError> handle_forward(Vector<StringView>, JsonValue const& payload);
ErrorOr<JsonValue, HttpError> handle_get_all_cookies(Vector<StringView>, JsonValue const& payload);
ErrorOr<Session*, HttpError> find_session_with_id(StringView session_id);
JsonValue make_json_value(JsonValue const&);

View file

@ -1,6 +1,7 @@
/*
* Copyright (c) 2022, Florent Castelli <florent.castelli@gmail.com>
* Copyright (c) 2022, Sam Atkins <atkinssj@serenityos.org>
* Copyright (c) 2022, Tobias Christiansen <tobyase@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
@ -11,6 +12,7 @@
#include <LibCore/LocalServer.h>
#include <LibCore/Stream.h>
#include <LibCore/System.h>
#include <LibWeb/Cookie/Cookie.h>
#include <unistd.h>
namespace WebDriver {
@ -229,4 +231,46 @@ ErrorOr<JsonValue, HttpError> Session::forward()
return JsonValue();
}
// https://w3c.github.io/webdriver/#dfn-serialized-cookie
static JsonObject serialize_cookie(Web::Cookie::Cookie const& cookie)
{
JsonObject serialized_cookie = {};
serialized_cookie.set("name", cookie.name);
serialized_cookie.set("value", cookie.value);
serialized_cookie.set("path", cookie.path);
serialized_cookie.set("domain", cookie.domain);
serialized_cookie.set("secure", cookie.secure);
serialized_cookie.set("httpOnly", cookie.http_only);
serialized_cookie.set("expiry", cookie.expiry_time.timestamp());
// FIXME: Add sameSite to Cookie and serialize it here too.
return serialized_cookie;
}
// GET /session/{session id}/cookie https://w3c.github.io/webdriver/#dfn-get-all-cookies
ErrorOr<JsonValue, HttpError> Session::get_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. Let cookies be a new JSON List.
JsonArray cookies = {};
// 4. For each cookie in all associated cookies of the current browsing contexts active document:
for (auto const& cookie : m_browser_connection->get_all_cookies()) {
// 1. Let serialized cookie be the result of serializing cookie.
auto serialized_cookie = serialize_cookie(cookie);
// 2. Append serialized cookie to cookies
cookies.append(serialized_cookie);
}
// 5. Return success with data cookies.
return JsonValue(cookies);
}
}

View file

@ -40,6 +40,7 @@ public:
ErrorOr<JsonValue, HttpError> refresh();
ErrorOr<JsonValue, HttpError> back();
ErrorOr<JsonValue, HttpError> forward();
ErrorOr<JsonValue, HttpError> get_all_cookies();
private:
NonnullRefPtr<Client> m_client;