Skip to content

Commit

Permalink
LibTLS: Use a setter for on_tls_ready_to_write with some more smarts
Browse files Browse the repository at this point in the history
The callback should be called as soon as the connection is established,
and if we actually set the callback when it already is, we expect it to
be called immediately.
  • Loading branch information
alimpfard committed Sep 19, 2021
1 parent d3ea081 commit 436693c
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 13 deletions.
5 changes: 3 additions & 2 deletions Tests/LibTLS/TestTLSHandshake.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,11 @@ TEST_CASE(test_TLS_hello_handshake)
tls->set_root_certificates(s_root_ca_certificates);
bool sent_request = false;
ByteBuffer contents;
tls->on_tls_ready_to_write = [&](TLS::TLSv12& tls) {
tls->set_on_tls_ready_to_write([&](TLS::TLSv12& tls) {
if (sent_request)
return;
sent_request = true;
Core::deferred_invoke([&tls] { tls.set_on_tls_ready_to_write(nullptr); });
if (!tls.write("GET / HTTP/1.1\r\nHost: "_b)) {
FAIL("write(0) failed");
loop.quit(0);
Expand All @@ -87,7 +88,7 @@ TEST_CASE(test_TLS_hello_handshake)
FAIL("write(2) failed");
loop.quit(0);
}
};
});
tls->on_tls_ready_to_read = [&](TLS::TLSv12& tls) {
auto data = tls.read();
if (!data.has_value()) {
Expand Down
5 changes: 3 additions & 2 deletions Userland/Libraries/LibGemini/GeminiJob.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,10 @@ void GeminiJob::register_on_ready_to_read(Function<void()> callback)

void GeminiJob::register_on_ready_to_write(Function<void()> callback)
{
m_socket->on_tls_ready_to_write = [callback = move(callback)](auto&) {
m_socket->set_on_tls_ready_to_write([callback = move(callback)](auto& tls) {
Core::deferred_invoke([&tls] { tls.set_on_tls_ready_to_write(nullptr); });
callback();
};
});
}

bool GeminiJob::can_read_line() const
Expand Down
6 changes: 4 additions & 2 deletions Userland/Libraries/LibHTTP/HttpsJob.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ void HttpsJob::shutdown()
return;
m_socket->on_tls_ready_to_read = nullptr;
m_socket->on_tls_connected = nullptr;
m_socket->set_on_tls_ready_to_write(nullptr);
m_socket = nullptr;
}

Expand Down Expand Up @@ -97,9 +98,10 @@ void HttpsJob::register_on_ready_to_read(Function<void()> callback)

void HttpsJob::register_on_ready_to_write(Function<void()> callback)
{
m_socket->on_tls_ready_to_write = [callback = move(callback)](auto&) {
m_socket->set_on_tls_ready_to_write([callback = move(callback)](auto& tls) {
Core::deferred_invoke([&tls] { tls.set_on_tls_ready_to_write(nullptr); });
callback();
};
});
}

bool HttpsJob::can_read_line() const
Expand Down
11 changes: 10 additions & 1 deletion Userland/Libraries/LibTLS/TLSv12.h
Original file line number Diff line number Diff line change
Expand Up @@ -373,8 +373,16 @@ class TLSv12 : public Core::Socket {
bool can_read() const { return m_context.application_buffer.size() > 0; }
String read_line(size_t max_size);

void set_on_tls_ready_to_write(Function<void(TLSv12&)> function)
{
on_tls_ready_to_write = move(function);
if (on_tls_ready_to_write) {
if (is_established())
on_tls_ready_to_write(*this);
}
}

Function<void(TLSv12&)> on_tls_ready_to_read;
Function<void(TLSv12&)> on_tls_ready_to_write;
Function<void(AlertDescription)> on_tls_error;
Function<void()> on_tls_connected;
Function<void()> on_tls_finished;
Expand Down Expand Up @@ -521,6 +529,7 @@ class TLSv12 : public Core::Socket {
i32 m_max_wait_time_for_handshake_in_seconds { 10 };

RefPtr<Core::Timer> m_handshake_timeout_timer;
Function<void(TLSv12&)> on_tls_ready_to_write;
};

}
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,10 @@ void TLSv12WebSocketConnectionImpl::connect(ConnectionInfo const& connection)
m_socket->on_tls_ready_to_read = [this](auto&) {
on_ready_to_read();
};
m_socket->on_tls_ready_to_write = [this](auto&) {
m_socket->set_on_tls_ready_to_write([this](auto& tls) {
tls.set_on_tls_ready_to_write(nullptr);
on_connected();
};
});
m_socket->on_tls_finished = [this] {
on_connection_error();
};
Expand Down
9 changes: 5 additions & 4 deletions Userland/Utilities/test-crypto.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -152,12 +152,12 @@ static void tls(const char* message, size_t len)
if (buffer.has_value())
out("{}", StringView { buffer->data(), buffer->size() });
};
tls->on_tls_ready_to_write = [&](auto&) {
tls->set_on_tls_ready_to_write([&](auto&) {
if (write.size()) {
tls->write(write);
write.clear();
}
};
});
tls->on_tls_error = [&](auto) {
g_loop.quit(1);
};
Expand Down Expand Up @@ -2013,10 +2013,11 @@ static void tls_test_client_hello()
tls->set_root_certificates(s_root_ca_certificates);
bool sent_request = false;
ByteBuffer contents;
tls->on_tls_ready_to_write = [&](TLS::TLSv12& tls) {
tls->set_on_tls_ready_to_write([&](TLS::TLSv12& tls) {
if (sent_request)
return;
sent_request = true;
Core::deferred_invoke([&tls] { tls.set_on_tls_ready_to_write(nullptr); });
if (!tls.write("GET / HTTP/1.1\r\nHost: "_b)) {
FAIL(write(0) failed);
loop.quit(0);
Expand All @@ -2030,7 +2031,7 @@ static void tls_test_client_hello()
FAIL(write(2) failed);
loop.quit(0);
}
};
});
tls->on_tls_ready_to_read = [&](TLS::TLSv12& tls) {
auto data = tls.read();
if (!data.has_value()) {
Expand Down

0 comments on commit 436693c

Please sign in to comment.