mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-04-25 14:05:15 +00:00
This commit un-deprecates DeprecatedString, and repurposes it as a byte string. As the null state has already been removed, there are no other particularly hairy blockers in repurposing this type as a byte string (what it _really_ is). This commit is auto-generated: $ xs=$(ack -l \bDeprecatedString\b\|deprecated_string AK Userland \ Meta Ports Ladybird Tests Kernel) $ perl -pie 's/\bDeprecatedString\b/ByteString/g; s/deprecated_string/byte_string/g' $xs $ clang-format --style=file -i \ $(git diff --name-only | grep \.cpp\|\.h) $ gn format $(git ls-files '*.gn' '*.gni')
100 lines
3.3 KiB
C++
100 lines
3.3 KiB
C++
/*
|
|
* Copyright (c) 2021, Dex♪ <dexes.ttp@gmail.com>
|
|
*
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
*/
|
|
|
|
#include <LibProtocol/WebSocket.h>
|
|
#include <LibProtocol/WebSocketClient.h>
|
|
|
|
namespace Protocol {
|
|
|
|
WebSocketClient::WebSocketClient(NonnullOwnPtr<Core::LocalSocket> socket)
|
|
: IPC::ConnectionToServer<WebSocketClientEndpoint, WebSocketServerEndpoint>(*this, move(socket))
|
|
{
|
|
}
|
|
|
|
RefPtr<WebSocket> WebSocketClient::connect(const URL& url, ByteString const& origin, Vector<ByteString> const& protocols, Vector<ByteString> const& extensions, HashMap<ByteString, ByteString> const& request_headers)
|
|
{
|
|
auto headers_or_error = request_headers.clone();
|
|
if (headers_or_error.is_error())
|
|
return nullptr;
|
|
auto connection_id = IPCProxy::connect(url, origin, protocols, extensions, headers_or_error.release_value());
|
|
if (connection_id < 0)
|
|
return nullptr;
|
|
auto connection = WebSocket::create_from_id({}, *this, connection_id);
|
|
m_connections.set(connection_id, connection);
|
|
return connection;
|
|
}
|
|
|
|
u32 WebSocketClient::ready_state(Badge<WebSocket>, WebSocket& connection)
|
|
{
|
|
if (!m_connections.contains(connection.id()))
|
|
return (u32)WebSocket::ReadyState::Closed;
|
|
return IPCProxy::ready_state(connection.id());
|
|
}
|
|
|
|
ByteString WebSocketClient::subprotocol_in_use(Badge<WebSocket>, WebSocket& connection)
|
|
{
|
|
if (!m_connections.contains(connection.id()))
|
|
return ByteString::empty();
|
|
return IPCProxy::subprotocol_in_use(connection.id());
|
|
}
|
|
|
|
void WebSocketClient::send(Badge<WebSocket>, WebSocket& connection, ByteBuffer data, bool is_text)
|
|
{
|
|
if (!m_connections.contains(connection.id()))
|
|
return;
|
|
async_send(connection.id(), is_text, move(data));
|
|
}
|
|
|
|
void WebSocketClient::close(Badge<WebSocket>, WebSocket& connection, u16 code, ByteString message)
|
|
{
|
|
if (!m_connections.contains(connection.id()))
|
|
return;
|
|
async_close(connection.id(), code, move(message));
|
|
}
|
|
|
|
bool WebSocketClient::set_certificate(Badge<WebSocket>, WebSocket& connection, ByteString certificate, ByteString key)
|
|
{
|
|
if (!m_connections.contains(connection.id()))
|
|
return false;
|
|
return IPCProxy::set_certificate(connection.id(), move(certificate), move(key));
|
|
}
|
|
|
|
void WebSocketClient::connected(i32 connection_id)
|
|
{
|
|
auto maybe_connection = m_connections.get(connection_id);
|
|
if (maybe_connection.has_value())
|
|
maybe_connection.value()->did_open({});
|
|
}
|
|
|
|
void WebSocketClient::received(i32 connection_id, bool is_text, ByteBuffer const& data)
|
|
{
|
|
auto maybe_connection = m_connections.get(connection_id);
|
|
if (maybe_connection.has_value())
|
|
maybe_connection.value()->did_receive({}, data, is_text);
|
|
}
|
|
|
|
void WebSocketClient::errored(i32 connection_id, i32 message)
|
|
{
|
|
auto maybe_connection = m_connections.get(connection_id);
|
|
if (maybe_connection.has_value())
|
|
maybe_connection.value()->did_error({}, message);
|
|
}
|
|
|
|
void WebSocketClient::closed(i32 connection_id, u16 code, ByteString const& reason, bool clean)
|
|
{
|
|
auto maybe_connection = m_connections.get(connection_id);
|
|
if (maybe_connection.has_value())
|
|
maybe_connection.value()->did_close({}, code, reason, clean);
|
|
}
|
|
|
|
void WebSocketClient::certificate_requested(i32 connection_id)
|
|
{
|
|
auto maybe_connection = m_connections.get(connection_id);
|
|
if (maybe_connection.has_value())
|
|
maybe_connection.value()->did_request_certificates({});
|
|
}
|
|
|
|
}
|