Client.cpp 28 KB

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