WebSocketClient.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /*
  2. * Copyright (c) 2021, Dex♪ <dexes.ttp@gmail.com>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibProtocol/WebSocket.h>
  7. #include <LibProtocol/WebSocketClient.h>
  8. namespace Protocol {
  9. WebSocketClient::WebSocketClient(NonnullOwnPtr<Core::LocalSocket> socket)
  10. : IPC::ConnectionToServer<WebSocketClientEndpoint, WebSocketServerEndpoint>(*this, move(socket))
  11. {
  12. }
  13. RefPtr<WebSocket> WebSocketClient::connect(const URL& url, ByteString const& origin, Vector<ByteString> const& protocols, Vector<ByteString> const& extensions, HashMap<ByteString, ByteString> const& request_headers)
  14. {
  15. auto headers_or_error = request_headers.clone();
  16. if (headers_or_error.is_error())
  17. return nullptr;
  18. auto connection_id = IPCProxy::connect(url, origin, protocols, extensions, headers_or_error.release_value());
  19. if (connection_id < 0)
  20. return nullptr;
  21. auto connection = WebSocket::create_from_id({}, *this, connection_id);
  22. m_connections.set(connection_id, connection);
  23. return connection;
  24. }
  25. u32 WebSocketClient::ready_state(Badge<WebSocket>, WebSocket& connection)
  26. {
  27. if (!m_connections.contains(connection.id()))
  28. return (u32)WebSocket::ReadyState::Closed;
  29. return IPCProxy::ready_state(connection.id());
  30. }
  31. ByteString WebSocketClient::subprotocol_in_use(Badge<WebSocket>, WebSocket& connection)
  32. {
  33. if (!m_connections.contains(connection.id()))
  34. return ByteString::empty();
  35. return IPCProxy::subprotocol_in_use(connection.id());
  36. }
  37. void WebSocketClient::send(Badge<WebSocket>, WebSocket& connection, ByteBuffer data, bool is_text)
  38. {
  39. if (!m_connections.contains(connection.id()))
  40. return;
  41. async_send(connection.id(), is_text, move(data));
  42. }
  43. void WebSocketClient::close(Badge<WebSocket>, WebSocket& connection, u16 code, ByteString message)
  44. {
  45. if (!m_connections.contains(connection.id()))
  46. return;
  47. async_close(connection.id(), code, move(message));
  48. }
  49. bool WebSocketClient::set_certificate(Badge<WebSocket>, WebSocket& connection, ByteString certificate, ByteString key)
  50. {
  51. if (!m_connections.contains(connection.id()))
  52. return false;
  53. return IPCProxy::set_certificate(connection.id(), move(certificate), move(key));
  54. }
  55. void WebSocketClient::connected(i32 connection_id)
  56. {
  57. auto maybe_connection = m_connections.get(connection_id);
  58. if (maybe_connection.has_value())
  59. maybe_connection.value()->did_open({});
  60. }
  61. void WebSocketClient::received(i32 connection_id, bool is_text, ByteBuffer const& data)
  62. {
  63. auto maybe_connection = m_connections.get(connection_id);
  64. if (maybe_connection.has_value())
  65. maybe_connection.value()->did_receive({}, data, is_text);
  66. }
  67. void WebSocketClient::errored(i32 connection_id, i32 message)
  68. {
  69. auto maybe_connection = m_connections.get(connection_id);
  70. if (maybe_connection.has_value())
  71. maybe_connection.value()->did_error({}, message);
  72. }
  73. void WebSocketClient::closed(i32 connection_id, u16 code, ByteString const& reason, bool clean)
  74. {
  75. auto maybe_connection = m_connections.get(connection_id);
  76. if (maybe_connection.has_value())
  77. maybe_connection.value()->did_close({}, code, reason, clean);
  78. }
  79. void WebSocketClient::certificate_requested(i32 connection_id)
  80. {
  81. auto maybe_connection = m_connections.get(connection_id);
  82. if (maybe_connection.has_value())
  83. maybe_connection.value()->did_request_certificates({});
  84. }
  85. }