Client.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600
  1. /*
  2. * Copyright (c) 2022, Florent Castelli <florent.castelli@gmail.com>
  3. * Copyright (c) 2022, Sam Atkins <atkinssj@serenityos.org>
  4. * Copyright (c) 2022, Tobias Christiansen <tobyase@serenityos.org>
  5. * Copyright (c) 2022, Linus Groh <linusg@serenityos.org>
  6. *
  7. * SPDX-License-Identifier: BSD-2-Clause
  8. */
  9. #include "Client.h"
  10. #include "Session.h"
  11. #include <AK/Debug.h>
  12. #include <AK/JsonParser.h>
  13. #include <LibCore/DateTime.h>
  14. #include <LibCore/MemoryStream.h>
  15. #include <LibHTTP/HttpRequest.h>
  16. #include <LibHTTP/HttpResponse.h>
  17. namespace WebDriver {
  18. Atomic<unsigned> Client::s_next_session_id;
  19. NonnullOwnPtrVector<Session> Client::s_sessions;
  20. Vector<Client::Route> Client::s_routes = {
  21. { HTTP::HttpRequest::Method::POST, { "session" }, &Client::handle_new_session },
  22. { HTTP::HttpRequest::Method::DELETE, { "session", ":session_id" }, &Client::handle_delete_session },
  23. { HTTP::HttpRequest::Method::GET, { "status" }, &Client::handle_get_status },
  24. { HTTP::HttpRequest::Method::POST, { "session", ":session_id", "url" }, &Client::handle_navigate_to },
  25. { HTTP::HttpRequest::Method::GET, { "session", ":session_id", "url" }, &Client::handle_get_current_url },
  26. { HTTP::HttpRequest::Method::POST, { "session", ":session_id", "back" }, &Client::handle_back },
  27. { HTTP::HttpRequest::Method::POST, { "session", ":session_id", "forward" }, &Client::handle_forward },
  28. { HTTP::HttpRequest::Method::POST, { "session", ":session_id", "refresh" }, &Client::handle_refresh },
  29. { HTTP::HttpRequest::Method::GET, { "session", ":session_id", "title" }, &Client::handle_get_title },
  30. { HTTP::HttpRequest::Method::GET, { "session", ":session_id", "window" }, &Client::handle_get_window_handle },
  31. { HTTP::HttpRequest::Method::DELETE, { "session", ":session_id", "window" }, &Client::handle_close_window },
  32. { HTTP::HttpRequest::Method::POST, { "session", ":session_id", "element" }, &Client::handle_find_element },
  33. { HTTP::HttpRequest::Method::GET, { "session", ":session_id", "cookie" }, &Client::handle_get_all_cookies },
  34. { HTTP::HttpRequest::Method::GET, { "session", ":session_id", "cookie", ":name" }, &Client::handle_get_named_cookie },
  35. { HTTP::HttpRequest::Method::POST, { "session", ":session_id", "cookie" }, &Client::handle_add_cookie },
  36. { HTTP::HttpRequest::Method::DELETE, { "session", ":session_id", "cookie", ":name" }, &Client::handle_delete_cookie },
  37. { HTTP::HttpRequest::Method::DELETE, { "session", ":session_id", "cookie" }, &Client::handle_delete_all_cookies },
  38. };
  39. Client::Client(NonnullOwnPtr<Core::Stream::BufferedTCPSocket> socket, Core::Object* parent)
  40. : Core::Object(parent)
  41. , m_socket(move(socket))
  42. {
  43. }
  44. void Client::die()
  45. {
  46. m_socket->close();
  47. deferred_invoke([this] { remove_from_parent(); });
  48. }
  49. void Client::start()
  50. {
  51. m_socket->on_ready_to_read = [this] {
  52. StringBuilder builder;
  53. // FIXME: All this should be moved to LibHTTP and be made spec compliant
  54. auto maybe_buffer = ByteBuffer::create_uninitialized(m_socket->buffer_size());
  55. if (maybe_buffer.is_error()) {
  56. warnln("Could not create buffer for client: {}", maybe_buffer.error());
  57. die();
  58. return;
  59. }
  60. auto buffer = maybe_buffer.release_value();
  61. for (;;) {
  62. auto maybe_can_read = m_socket->can_read_without_blocking();
  63. if (maybe_can_read.is_error()) {
  64. warnln("Failed to get the blocking status for the socket: {}", maybe_can_read.error());
  65. die();
  66. return;
  67. }
  68. if (!maybe_can_read.value())
  69. break;
  70. auto maybe_data = m_socket->read(buffer);
  71. if (maybe_data.is_error()) {
  72. warnln("Failed to read data from the request: {}", maybe_data.error());
  73. die();
  74. return;
  75. }
  76. if (m_socket->is_eof()) {
  77. die();
  78. break;
  79. }
  80. builder.append(StringView(maybe_data.value()));
  81. }
  82. auto request = builder.to_byte_buffer();
  83. auto http_request_or_error = HTTP::HttpRequest::from_raw_request(request);
  84. if (!http_request_or_error.has_value())
  85. return;
  86. auto http_request = http_request_or_error.release_value();
  87. auto body_or_error = read_body_as_json(http_request);
  88. if (body_or_error.is_error()) {
  89. warnln("Failed to read the request body: {}", body_or_error.error());
  90. die();
  91. return;
  92. }
  93. auto maybe_did_handle = handle_request(http_request, body_or_error.value());
  94. if (maybe_did_handle.is_error()) {
  95. warnln("Failed to handle the request: {}", maybe_did_handle.error());
  96. }
  97. die();
  98. };
  99. }
  100. ErrorOr<JsonValue> Client::read_body_as_json(HTTP::HttpRequest const& request)
  101. {
  102. // If we received a multipart body here, this would fail badly.
  103. unsigned content_length = 0;
  104. for (auto const& header : request.headers()) {
  105. if (header.name.equals_ignoring_case("Content-Length"sv)) {
  106. content_length = header.value.to_int(TrimWhitespace::Yes).value_or(0);
  107. break;
  108. }
  109. }
  110. if (!content_length)
  111. return JsonValue();
  112. // FIXME: Check the Content-Type is actually application/json
  113. JsonParser json_parser(request.body());
  114. return json_parser.parse();
  115. }
  116. ErrorOr<bool> Client::handle_request(HTTP::HttpRequest const& request, JsonValue const& body)
  117. {
  118. if constexpr (WEBDRIVER_DEBUG) {
  119. dbgln("Got HTTP request: {} {}", request.method_name(), request.resource());
  120. if (!body.is_null())
  121. dbgln("Body: {}", body.to_string());
  122. }
  123. auto routing_result_match = match_route(request.method(), request.resource());
  124. if (routing_result_match.is_error()) {
  125. auto error = routing_result_match.release_error();
  126. dbgln_if(WEBDRIVER_DEBUG, "Failed to match route: {}", error);
  127. TRY(send_error_response(error, request));
  128. return false;
  129. }
  130. auto routing_result = routing_result_match.release_value();
  131. auto result = (this->*routing_result.handler)(routing_result.parameters, body);
  132. if (result.is_error()) {
  133. dbgln_if(WEBDRIVER_DEBUG, "Error in calling route handler: {}", result.error());
  134. TRY(send_error_response(result.release_error(), request));
  135. return false;
  136. }
  137. auto object = result.release_value();
  138. TRY(send_response(object.to_string(), request));
  139. return true;
  140. }
  141. // https://w3c.github.io/webdriver/#dfn-send-a-response
  142. ErrorOr<void> Client::send_response(StringView content, HTTP::HttpRequest const& request)
  143. {
  144. // FIXME: Implement to spec.
  145. StringBuilder builder;
  146. builder.append("HTTP/1.0 200 OK\r\n"sv);
  147. builder.append("Server: WebDriver (SerenityOS)\r\n"sv);
  148. builder.append("X-Frame-Options: SAMEORIGIN\r\n"sv);
  149. builder.append("X-Content-Type-Options: nosniff\r\n"sv);
  150. builder.append("Pragma: no-cache\r\n"sv);
  151. builder.append("Content-Type: application/json; charset=utf-8\r\n"sv);
  152. builder.appendff("Content-Length: {}\r\n", content.length());
  153. builder.append("\r\n"sv);
  154. auto builder_contents = builder.to_byte_buffer();
  155. TRY(m_socket->write(builder_contents));
  156. TRY(m_socket->write(content.bytes()));
  157. log_response(200, request);
  158. auto keep_alive = false;
  159. if (auto it = request.headers().find_if([](auto& header) { return header.name.equals_ignoring_case("Connection"sv); }); !it.is_end()) {
  160. if (it->value.trim_whitespace().equals_ignoring_case("keep-alive"sv))
  161. keep_alive = true;
  162. }
  163. if (!keep_alive)
  164. m_socket->close();
  165. return {};
  166. }
  167. // https://w3c.github.io/webdriver/#dfn-send-an-error
  168. ErrorOr<void> Client::send_error_response(HttpError const& error, HTTP::HttpRequest const& request)
  169. {
  170. // FIXME: Implement to spec.
  171. dbgln("send_error_response: {} {}: {}", error.http_status, error.error, error.message);
  172. auto reason_phrase = HTTP::HttpResponse::reason_phrase_for_code(error.http_status);
  173. auto result = JsonObject();
  174. result.set("error", error.error);
  175. result.set("message", error.message);
  176. result.set("stacktrace", "");
  177. StringBuilder content_builder;
  178. result.serialize(content_builder);
  179. StringBuilder header_builder;
  180. header_builder.appendff("HTTP/1.0 {} ", error.http_status);
  181. header_builder.append(reason_phrase);
  182. header_builder.append("\r\n"sv);
  183. header_builder.append("Content-Type: application/json; charset=UTF-8\r\n"sv);
  184. header_builder.appendff("Content-Length: {}\r\n", content_builder.length());
  185. header_builder.append("\r\n"sv);
  186. TRY(m_socket->write(header_builder.to_byte_buffer()));
  187. TRY(m_socket->write(content_builder.to_byte_buffer()));
  188. log_response(error.http_status, request);
  189. return {};
  190. }
  191. void Client::log_response(unsigned code, HTTP::HttpRequest const& request)
  192. {
  193. outln("{} :: {:03d} :: {} {}", Core::DateTime::now().to_string(), code, request.method_name(), request.resource());
  194. }
  195. // https://w3c.github.io/webdriver/#dfn-match-a-request
  196. ErrorOr<Client::RoutingResult, HttpError> Client::match_route(HTTP::HttpRequest::Method method, String const& resource)
  197. {
  198. // FIXME: Implement to spec.
  199. dbgln_if(WEBDRIVER_DEBUG, "match_route({}, {})", HTTP::to_string(method), resource);
  200. // https://w3c.github.io/webdriver/webdriver-spec.html#routing-requests
  201. if (!resource.starts_with(m_prefix))
  202. return HttpError { 404, "unknown command", "The resource doesn't start with the prefix." };
  203. Vector<StringView> resource_split = resource.substring_view(m_prefix.length()).split_view('/', true);
  204. Vector<StringView> parameters;
  205. bool matched_path = false;
  206. for (auto const& route : Client::s_routes) {
  207. dbgln_if(WEBDRIVER_DEBUG, "- Checking {} {}", HTTP::to_string(route.method), String::join("/"sv, route.path));
  208. if (resource_split.size() != route.path.size()) {
  209. dbgln_if(WEBDRIVER_DEBUG, "-> Discarding: Wrong length");
  210. continue;
  211. }
  212. bool match = true;
  213. for (size_t i = 0; i < route.path.size(); ++i) {
  214. if (route.path[i].starts_with(':')) {
  215. parameters.append(resource_split[i]);
  216. continue;
  217. }
  218. if (route.path[i] != resource_split[i]) {
  219. match = false;
  220. parameters.clear();
  221. dbgln_if(WEBDRIVER_DEBUG, "-> Discarding: Part `{}` does not match `{}`", route.path[i], resource_split[i]);
  222. break;
  223. }
  224. }
  225. if (match && route.method == method) {
  226. dbgln_if(WEBDRIVER_DEBUG, "-> Matched! :^)");
  227. return RoutingResult { route.handler, parameters };
  228. }
  229. matched_path = true;
  230. }
  231. // Matched a path, but didn't match a known method
  232. if (matched_path) {
  233. dbgln_if(WEBDRIVER_DEBUG, "- A path matched, but method didn't. :^(");
  234. return HttpError { 405, "unknown method", "The command matched a known URL but did not match a method for that URL." };
  235. }
  236. // Didn't have any match
  237. dbgln_if(WEBDRIVER_DEBUG, "- No matches. :^(");
  238. return HttpError { 404, "unknown command", "The command was not recognized." };
  239. }
  240. ErrorOr<Session*, HttpError> Client::find_session_with_id(StringView session_id)
  241. {
  242. auto session_id_or_error = session_id.to_uint<>();
  243. if (!session_id_or_error.has_value())
  244. return HttpError { 404, "invalid session id", "Invalid session id" };
  245. for (auto& session : Client::s_sessions) {
  246. if (session.session_id() == session_id_or_error.value())
  247. return &session;
  248. }
  249. return HttpError { 404, "invalid session id", "Invalid session id" };
  250. }
  251. void Client::close_session(unsigned session_id)
  252. {
  253. bool found = Client::s_sessions.remove_first_matching([&](auto const& it) {
  254. return it->session_id() == session_id;
  255. });
  256. if (found)
  257. dbgln_if(WEBDRIVER_DEBUG, "Shut down session {}", session_id);
  258. else
  259. dbgln_if(WEBDRIVER_DEBUG, "Unable to shut down session {}: Not found", session_id);
  260. }
  261. JsonValue Client::make_json_value(JsonValue const& value)
  262. {
  263. JsonObject result;
  264. result.set("value", value);
  265. return result;
  266. }
  267. // 8.1 New Session, https://w3c.github.io/webdriver/#dfn-new-sessions
  268. // POST /session
  269. ErrorOr<JsonValue, HttpError> Client::handle_new_session(Vector<StringView> const&, JsonValue const&)
  270. {
  271. dbgln_if(WEBDRIVER_DEBUG, "Handling POST /session");
  272. // FIXME: 1. If the maximum active sessions is equal to the length of the list of active sessions,
  273. // return error with error code session not created.
  274. // FIXME: 2. If the remote end is an intermediary node, take implementation-defined steps that either
  275. // result in returning an error with error code session not created, or in returning a
  276. // success with data that is isomorphic to that returned by remote ends according to the
  277. // rest of this algorithm. If an error is not returned, the intermediary node must retain a
  278. // reference to the session created on the upstream node as the associated session such
  279. // that commands may be forwarded to this associated session on subsequent commands.
  280. // FIXME: 3. If the maximum active sessions is equal to the length of the list of active sessions,
  281. // return error with error code session not created.
  282. // FIXME: 4. Let capabilities be the result of trying to process capabilities with parameters as an argument.
  283. // FIXME: 5. If capabilities’s is null, return error with error code session not created.
  284. // 6. Let session id be the result of generating a UUID.
  285. // FIXME: Actually create a UUID.
  286. auto session_id = Client::s_next_session_id++;
  287. // 7. Let session be a new session with the session ID of session id.
  288. NonnullOwnPtr<Session> session = make<Session>(session_id, *this);
  289. auto start_result = session->start();
  290. if (start_result.is_error()) {
  291. return HttpError { 500, "Failed to start session", start_result.error().string_literal() };
  292. }
  293. // FIXME: 8. Set the current session to session.
  294. // FIXME: 9. Run any WebDriver new session algorithm defined in external specifications,
  295. // with arguments session and capabilities.
  296. // 10. Append session to active sessions.
  297. Client::s_sessions.append(move(session));
  298. // 11. Let body be a JSON Object initialized with:
  299. JsonObject body;
  300. // "sessionId"
  301. // session id
  302. body.set("sessionId", String::number(session_id));
  303. // FIXME: "capabilities"
  304. // capabilities
  305. // FIXME: 12. Initialize the following from capabilities:
  306. // NOTE: See spec for steps
  307. // FIXME: 13. Set the webdriver-active flag to true.
  308. // FIXME: 14. Set the current top-level browsing context for session with the top-level browsing context
  309. // of the UA’s current browsing context.
  310. // FIXME: 15. Set the request queue to a new queue.
  311. // 16. Return success with data body.
  312. return make_json_value(body);
  313. }
  314. // 8.2 Delete Session, https://w3c.github.io/webdriver/#dfn-delete-session
  315. // DELETE /session/{session id}
  316. ErrorOr<JsonValue, HttpError> Client::handle_delete_session(Vector<StringView> const& parameters, JsonValue const&)
  317. {
  318. dbgln_if(WEBDRIVER_DEBUG, "Handling DELETE /session/<session_id>");
  319. // 1. If the current session is an active session, try to close the session.
  320. auto* session = TRY(find_session_with_id(parameters[0]));
  321. auto stop_result = session->stop();
  322. if (stop_result.is_error()) {
  323. return HttpError { 500, "unsupported operation", stop_result.error().string_literal() };
  324. }
  325. // 2. Return success with data null.
  326. return make_json_value(JsonValue());
  327. }
  328. // 8.3 Status, https://w3c.github.io/webdriver/#dfn-status
  329. // GET /status
  330. ErrorOr<JsonValue, HttpError> Client::handle_get_status(Vector<StringView> const&, JsonValue const&)
  331. {
  332. dbgln_if(WEBDRIVER_DEBUG, "Handling GET /status");
  333. // 1. Let body be a new JSON Object with the following properties:
  334. // "ready"
  335. // The remote end’s readiness state.
  336. // "message"
  337. // An implementation-defined string explaining the remote end’s readiness state.
  338. // FIXME: Report if we are somehow not ready.
  339. JsonObject body;
  340. body.set("ready", true);
  341. body.set("message", "Ready to start some sessions!");
  342. // 2. Return success with data body.
  343. return body;
  344. }
  345. // 10.1 Navigate To, https://w3c.github.io/webdriver/#dfn-navigate-to
  346. // POST /session/{session id}/url
  347. ErrorOr<JsonValue, HttpError> Client::handle_navigate_to(Vector<StringView> const& parameters, JsonValue const& payload)
  348. {
  349. dbgln_if(WEBDRIVER_DEBUG, "Handling POST /session/<session_id>/url");
  350. auto* session = TRY(find_session_with_id(parameters[0]));
  351. // NOTE: Spec steps handled in Session::navigate_to().
  352. auto result = TRY(session->navigate_to(payload));
  353. return make_json_value(result);
  354. }
  355. // 10.2 Get Current URL, https://w3c.github.io/webdriver/#dfn-get-current-url
  356. // GET /session/{session id}/url
  357. ErrorOr<JsonValue, HttpError> Client::handle_get_current_url(Vector<StringView> const& parameters, JsonValue const&)
  358. {
  359. dbgln_if(WEBDRIVER_DEBUG, "Handling GET /session/<session_id>/url");
  360. auto* session = TRY(find_session_with_id(parameters[0]));
  361. // NOTE: Spec steps handled in Session::get_current_url().
  362. auto result = TRY(session->get_current_url());
  363. return make_json_value(result);
  364. }
  365. // 10.3 Back, https://w3c.github.io/webdriver/#dfn-back
  366. // POST /session/{session id}/back
  367. ErrorOr<JsonValue, HttpError> Client::handle_back(Vector<StringView> const& parameters, JsonValue const&)
  368. {
  369. dbgln_if(WEBDRIVER_DEBUG, "Handling POST /session/<session_id>/back");
  370. auto* session = TRY(find_session_with_id(parameters[0]));
  371. // NOTE: Spec steps handled in Session::back().
  372. auto result = TRY(session->back());
  373. return make_json_value(result);
  374. }
  375. // 10.4 Forward, https://w3c.github.io/webdriver/#dfn-forward
  376. // POST /session/{session id}/forward
  377. ErrorOr<JsonValue, HttpError> Client::handle_forward(Vector<StringView> const& parameters, JsonValue const&)
  378. {
  379. dbgln_if(WEBDRIVER_DEBUG, "Handling POST /session/<session_id>/forward");
  380. auto* session = TRY(find_session_with_id(parameters[0]));
  381. // NOTE: Spec steps handled in Session::forward().
  382. auto result = TRY(session->forward());
  383. return make_json_value(result);
  384. }
  385. // 10.5 Refresh, https://w3c.github.io/webdriver/#dfn-refresh
  386. // POST /session/{session id}/refresh
  387. ErrorOr<JsonValue, HttpError> Client::handle_refresh(Vector<StringView> const& parameters, JsonValue const&)
  388. {
  389. dbgln_if(WEBDRIVER_DEBUG, "Handling POST /session/<session_id>/refresh");
  390. auto* session = TRY(find_session_with_id(parameters[0]));
  391. // NOTE: Spec steps handled in Session::refresh().
  392. auto result = TRY(session->refresh());
  393. return make_json_value(result);
  394. }
  395. // 10.6 Get Title, https://w3c.github.io/webdriver/#dfn-get-title
  396. // GET /session/{session id}/title
  397. ErrorOr<JsonValue, HttpError> Client::handle_get_title(Vector<StringView> const& parameters, JsonValue const&)
  398. {
  399. dbgln_if(WEBDRIVER_DEBUG, "Handling GET /session/<session_id>/title");
  400. auto* session = TRY(find_session_with_id(parameters[0]));
  401. // NOTE: Spec steps handled in Session::get_title().
  402. auto result = TRY(session->get_title());
  403. return make_json_value(result);
  404. }
  405. // 11.1 Get Window Handle, https://w3c.github.io/webdriver/#get-window-handle
  406. // GET /session/{session id}/window
  407. ErrorOr<JsonValue, HttpError> Client::handle_get_window_handle(Vector<StringView> const& parameters, JsonValue const&)
  408. {
  409. dbgln_if(WEBDRIVER_DEBUG, "Handling GET /session/<session_id>/window");
  410. auto* session = TRY(find_session_with_id(parameters[0]));
  411. // 1. If the current top-level browsing context is no longer open, return error with error code no such window.
  412. auto current_window = session->get_window_object();
  413. if (!current_window.has_value())
  414. return HttpError { 404, "no such window", "Window not found" };
  415. // 2. Return success with data being the window handle associated with the current top-level browsing context.
  416. return make_json_value(session->current_window_handle());
  417. }
  418. // 11.2 Close Window, https://w3c.github.io/webdriver/#dfn-close-window
  419. // DELETE /session/{session id}/window
  420. ErrorOr<JsonValue, HttpError> Client::handle_close_window(Vector<StringView> const& parameters, JsonValue const&)
  421. {
  422. dbgln_if(WEBDRIVER_DEBUG, "Handling DELETE /session/<session_id>/window");
  423. auto* session = TRY(find_session_with_id(parameters[0]));
  424. // NOTE: Spec steps handled in Session::close_window().
  425. TRY(unwrap_result(session->close_window()));
  426. return make_json_value(JsonValue());
  427. }
  428. // 12.3.2 Find Element, https://w3c.github.io/webdriver/#dfn-find-element
  429. // POST /session/{session id}/element
  430. ErrorOr<JsonValue, HttpError> Client::handle_find_element(Vector<StringView> const& parameters, JsonValue const& payload)
  431. {
  432. dbgln_if(WEBDRIVER_DEBUG, "Handling POST /session/<session_id>/element");
  433. auto* session = TRY(find_session_with_id(parameters[0]));
  434. // NOTE: Spec steps handled in Session::find_element().
  435. auto result = TRY(session->find_element(payload));
  436. return make_json_value(result);
  437. }
  438. // 14.1 Get All Cookies, https://w3c.github.io/webdriver/#dfn-get-all-cookies
  439. // GET /session/{session id}/cookie
  440. ErrorOr<JsonValue, HttpError> Client::handle_get_all_cookies(Vector<StringView> const& parameters, JsonValue const&)
  441. {
  442. dbgln_if(WEBDRIVER_DEBUG, "Handling GET /session/<session_id>/cookie");
  443. auto* session = TRY(find_session_with_id(parameters[0]));
  444. // NOTE: Spec steps handled in Session::get_all_cookies().
  445. auto cookies = TRY(session->get_all_cookies());
  446. return make_json_value(cookies);
  447. }
  448. // 14.2 Get Named Cookie, https://w3c.github.io/webdriver/#dfn-get-named-cookie
  449. // GET /session/{session id}/cookie/{name}
  450. ErrorOr<JsonValue, HttpError> Client::handle_get_named_cookie(Vector<StringView> const& parameters, JsonValue const&)
  451. {
  452. dbgln_if(WEBDRIVER_DEBUG, "Handling GET /session/<session_id>/cookie/<name>");
  453. auto* session = TRY(find_session_with_id(parameters[0]));
  454. // NOTE: Spec steps handled in Session::get_all_cookies().
  455. auto cookies = TRY(session->get_named_cookie(parameters[1]));
  456. return make_json_value(cookies);
  457. }
  458. // 14.3 Add Cookie, https://w3c.github.io/webdriver/#dfn-adding-a-cookie
  459. // POST /session/{session id}/cookie
  460. ErrorOr<JsonValue, HttpError> Client::handle_add_cookie(Vector<StringView> const& parameters, JsonValue const& payload)
  461. {
  462. dbgln_if(WEBDRIVER_DEBUG, "Handling POST /session/<session_id>/cookie");
  463. auto* session = TRY(find_session_with_id(parameters[0]));
  464. // NOTE: Spec steps handled in Session::add_cookie().
  465. auto result = TRY(session->add_cookie(payload));
  466. return make_json_value(result);
  467. }
  468. // 14.4 Delete Cookie, https://w3c.github.io/webdriver/#dfn-delete-cookie
  469. // DELETE /session/{session id}/cookie/{name}
  470. ErrorOr<JsonValue, HttpError> Client::handle_delete_cookie(Vector<StringView> const& parameters, JsonValue const&)
  471. {
  472. dbgln_if(WEBDRIVER_DEBUG, "Handling DELETE /session/<session_id>/cookie/<name>");
  473. auto* session = TRY(find_session_with_id(parameters[0]));
  474. // NOTE: Spec steps handled in Session::delete_cookie().
  475. auto result = TRY(session->delete_cookie(parameters[1]));
  476. return make_json_value(result);
  477. }
  478. // 14.5 Delete All Cookies, https://w3c.github.io/webdriver/#dfn-delete-all-cookies
  479. // DELETE /session/{session id}/cookie
  480. ErrorOr<JsonValue, HttpError> Client::handle_delete_all_cookies(Vector<StringView> const& parameters, JsonValue const&)
  481. {
  482. dbgln_if(WEBDRIVER_DEBUG, "Handling DELETE /session/<session_id>/cookie");
  483. auto* session = TRY(find_session_with_id(parameters[0]));
  484. // NOTE: Spec steps handled in Session::delete_all_cookies().
  485. auto result = TRY(session->delete_all_cookies());
  486. return make_json_value(result);
  487. }
  488. }