LibTLS: Remove blocking option and simplify Options struct

The complex macro for options with defaults doesn't make sense
now that there's only one option.
This commit is contained in:
Andrew Kaster 2025-06-22 14:38:25 -06:00 committed by Ali Mohammad Pur
commit d9c85288d9
Notes: github-actions[bot] 2025-06-23 15:50:30 +00:00
7 changed files with 7 additions and 31 deletions

View file

@ -202,7 +202,7 @@ TLSv12::~TLSv12()
ErrorOr<NonnullOwnPtr<TLSv12>> TLSv12::connect_internal(NonnullOwnPtr<Core::TCPSocket> socket, ByteString const& host, Options options)
{
TRY(socket->set_blocking(options.blocking));
TRY(socket->set_blocking(false));
auto* ssl_ctx = OPENSSL_TRY_PTR(SSL_CTX_new(TLS_client_method()));
ArmedScopeGuard free_ssl_ctx = [&] { SSL_CTX_free(ssl_ctx); };

View file

@ -14,26 +14,7 @@
namespace TLS {
struct Options {
#define OPTION_WITH_DEFAULTS(typ, name, ...) \
static typ default_##name() \
{ \
return typ { __VA_ARGS__ }; \
} \
typ name = default_##name(); \
Options& set_##name(typ new_value) & \
{ \
name = move(new_value); \
return *this; \
} \
Options&& set_##name(typ new_value) && \
{ \
name = move(new_value); \
return move(*this); \
}
OPTION_WITH_DEFAULTS(Optional<ByteString>, root_certificates_path, )
OPTION_WITH_DEFAULTS(bool, blocking, true)
Optional<ByteString> root_certificates_path;
};
class TLSv12 final : public Core::Socket {

View file

@ -46,8 +46,7 @@ void WebSocketImplSerenity::connect(ConnectionInfo const& connection_info)
auto host = connection_info.url().serialized_host().to_byte_string();
if (connection_info.is_secure()) {
TLS::Options options;
options.set_root_certificates_path(connection_info.root_certificates_path());
options.set_blocking(false);
options.root_certificates_path = connection_info.root_certificates_path();
return TRY(Core::BufferedSocket<TLS::TLSv12>::create(
TRY(TLS::TLSv12::connect(host, connection_info.url().port_or_default(), move(options)))));

View file

@ -61,10 +61,9 @@ static NonnullRefPtr<Resolver> default_resolver()
if (g_dns_info.use_dns_over_tls) {
TLS::Options options;
options.set_blocking(false);
if (!g_default_certificate_path.is_empty())
options.set_root_certificates_path(g_default_certificate_path);
options.root_certificates_path = g_default_certificate_path;
return DNS::Resolver::SocketResult {
MaybeOwned<Core::Socket>(TRY(TLS::TLSv12::connect(*g_dns_info.server_address, *g_dns_info.server_hostname, move(options)))),

View file

@ -94,8 +94,7 @@ TEST_CASE(test_tls)
Core::SocketAddress addr = { IPv4Address::from_string("1.1.1.1"sv).value(), static_cast<u16>(853) };
TLS::Options options;
options.set_root_certificates_path(locate_ca_certs_file());
options.set_blocking(false);
options.root_certificates_path = locate_ca_certs_file();
return DNS::Resolver::SocketResult {
MaybeOwned<Core::Socket>(TRY(TLS::TLSv12::connect(addr, "1.1.1.1", move(options)))),

View file

@ -42,8 +42,7 @@ TEST_CASE(test_TLS_hello_handshake)
Core::EventLoop loop;
TLS::Options options;
options.blocking = false;
options.set_root_certificates_path(locate_ca_certs_file());
options.root_certificates_path = locate_ca_certs_file();
auto tls = TRY_OR_FAIL(Core::BufferedSocket<TLS::TLSv12>::create(TRY_OR_FAIL(TLS::TLSv12::connect(DEFAULT_SERVER, port, move(options)))));

View file

@ -78,8 +78,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
auto make_resolver = [&](Core::SocketAddress const& address) -> ErrorOr<DNS::Resolver::SocketResult> {
if (use_tls) {
TLS::Options options;
options.set_root_certificates_path(cert_path);
options.set_blocking(false);
options.root_certificates_path = cert_path;
auto tls = TRY(TLS::TLSv12::connect(address, server_address, move(options)));
return DNS::Resolver::SocketResult { move(tls), DNS::Resolver::ConnectionMode::TCP };