ladybird/Userland/Libraries/LibWebSocket/Impl/TLSv12WebSocketConnectionImpl.cpp
Ali Mohammad Pur 51c2c69357 AK+Everywhere: Disallow constructing Functions from incompatible types
Previously, AK::Function would accept _any_ callable type, and try to
call it when called, first with the given set of arguments, then with
zero arguments, and if all of those failed, it would simply not call the
function and **return a value-constructed Out type**.
This lead to many, many, many hard to debug situations when someone
forgot a `const` in their lambda argument types, and many cases of
people taking zero arguments in their lambdas to ignore them.
This commit reworks the Function interface to not include any such
surprising behaviour, if your function instance is not callable with
the declared argument set of the Function, it can simply not be
assigned to that Function instance, end of story.
2021-06-06 00:27:30 +04:30

96 lines
2.3 KiB
C++

/*
* Copyright (c) 2021, Dex♪ <dexes.ttp@gmail.com>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibWebSocket/Impl/TLSv12WebSocketConnectionImpl.h>
namespace WebSocket {
TLSv12WebSocketConnectionImpl::TLSv12WebSocketConnectionImpl(Core::Object* parent)
: AbstractWebSocketImpl(parent)
{
}
TLSv12WebSocketConnectionImpl::~TLSv12WebSocketConnectionImpl()
{
discard_connection();
}
void TLSv12WebSocketConnectionImpl::connect(ConnectionInfo const& connection)
{
VERIFY(!m_socket);
VERIFY(on_connected);
VERIFY(on_connection_error);
VERIFY(on_ready_to_read);
m_socket = TLS::TLSv12::construct(this);
m_socket->set_root_certificates(DefaultRootCACertificates::the().certificates());
m_socket->on_tls_error = [this](TLS::AlertDescription) {
on_connection_error();
};
m_socket->on_tls_ready_to_read = [this](auto&) {
on_ready_to_read();
};
m_socket->on_tls_ready_to_write = [this](auto&) {
on_connected();
};
m_socket->on_tls_finished = [this] {
on_connection_error();
};
m_socket->on_tls_certificate_request = [](auto&) {
// FIXME : Once we handle TLS certificate requests, handle it here as well.
};
bool success = m_socket->connect(connection.url().host(), connection.url().port());
if (!success) {
deferred_invoke([this](auto&) {
on_connection_error();
});
}
}
bool TLSv12WebSocketConnectionImpl::send(ReadonlyBytes data)
{
return m_socket->write(data);
}
bool TLSv12WebSocketConnectionImpl::can_read_line()
{
return m_socket->can_read_line();
}
String TLSv12WebSocketConnectionImpl::read_line(size_t size)
{
return m_socket->read_line(size);
}
bool TLSv12WebSocketConnectionImpl::can_read()
{
return m_socket->can_read();
}
ByteBuffer TLSv12WebSocketConnectionImpl::read(int max_size)
{
return m_socket->read(max_size);
}
bool TLSv12WebSocketConnectionImpl::eof()
{
return m_socket->eof();
}
void TLSv12WebSocketConnectionImpl::discard_connection()
{
if (!m_socket)
return;
m_socket->on_tls_connected = nullptr;
m_socket->on_tls_error = nullptr;
m_socket->on_tls_finished = nullptr;
m_socket->on_tls_certificate_request = nullptr;
m_socket->on_ready_to_read = nullptr;
remove_child(*m_socket);
m_socket = nullptr;
}
}