summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibTLS/Socket.cpp
diff options
context:
space:
mode:
authorAli Mohammad Pur <ali.mpfard@gmail.com>2021-09-16 17:18:49 +0430
committerAndreas Kling <kling@serenityos.org>2021-09-16 16:42:51 +0200
commitf4d3c54c12f64edec0eeb1395c6531787dbd738e (patch)
tree6df9e8d36195156391a83723849bd70cdf86fb05 /Userland/Libraries/LibTLS/Socket.cpp
parentdda216c3340ece9f01606c20c92e9aa454f387d0 (diff)
downloadserenity-f4d3c54c12f64edec0eeb1395c6531787dbd738e.zip
LibTLS: Close the underlying socket on EOF
This is 23febbed41d8296cf9e532a17145822cd099b591 but without the bug that makes the CI hang :^)
Diffstat (limited to 'Userland/Libraries/LibTLS/Socket.cpp')
-rw-r--r--Userland/Libraries/LibTLS/Socket.cpp48
1 files changed, 33 insertions, 15 deletions
diff --git a/Userland/Libraries/LibTLS/Socket.cpp b/Userland/Libraries/LibTLS/Socket.cpp
index ebfc3c24e6..58bc5c1086 100644
--- a/Userland/Libraries/LibTLS/Socket.cpp
+++ b/Userland/Libraries/LibTLS/Socket.cpp
@@ -142,31 +142,42 @@ bool TLSv12::common_connect(const struct sockaddr* saddr, socklen_t length)
return true;
}
-void TLSv12::read_from_socket()
+void TLSv12::notify_client_for_app_data()
{
- auto did_schedule_read = false;
- auto notify_client_for_app_data = [&] {
- if (m_context.application_buffer.size() > 0) {
- if (!did_schedule_read) {
- deferred_invoke([&] { read_from_socket(); });
- did_schedule_read = true;
- }
- if (on_tls_ready_to_read)
- on_tls_ready_to_read(*this);
+ if (m_context.application_buffer.size() > 0) {
+ if (!m_has_scheduled_app_data_flush) {
+ deferred_invoke([this] { notify_client_for_app_data(); });
+ m_has_scheduled_app_data_flush = true;
}
- };
+ if (on_tls_ready_to_read)
+ on_tls_ready_to_read(*this);
+ } else {
+ if (m_context.connection_finished && !m_context.has_invoked_finish_or_error_callback) {
+ m_context.has_invoked_finish_or_error_callback = true;
+ if (on_tls_finished)
+ on_tls_finished();
+ }
+ }
+ m_has_scheduled_app_data_flush = false;
+}
+void TLSv12::read_from_socket()
+{
// If there's anything before we consume stuff, let the client know
// since we won't be consuming things if the connection is terminated.
notify_client_for_app_data();
+ ScopeGuard notify_guard {
+ [this] {
+ // If anything new shows up, tell the client about the event.
+ notify_client_for_app_data();
+ }
+ };
+
if (!check_connection_state(true))
return;
consume(Core::Socket::read(4 * MiB));
-
- // If anything new shows up, tell the client about the event.
- notify_client_for_app_data();
}
void TLSv12::write_into_socket()
@@ -188,20 +199,27 @@ void TLSv12::write_into_socket()
bool TLSv12::check_connection_state(bool read)
{
+ if (m_context.connection_finished)
+ return false;
+
if (!Core::Socket::is_open() || !Core::Socket::is_connected() || Core::Socket::eof()) {
// an abrupt closure (the server is a jerk)
dbgln_if(TLS_DEBUG, "Socket not open, assuming abrupt closure");
m_context.connection_finished = true;
+ Core::Socket::close();
+ return false;
}
if (m_context.critical_error) {
dbgln_if(TLS_DEBUG, "CRITICAL ERROR {} :(", m_context.critical_error);
+ m_context.has_invoked_finish_or_error_callback = true;
if (on_tls_error)
on_tls_error((AlertDescription)m_context.critical_error);
return false;
}
if (((read && m_context.application_buffer.size() == 0) || !read) && m_context.connection_finished) {
if (m_context.application_buffer.size() == 0 && m_context.connection_status != ConnectionStatus::Disconnected) {
+ m_context.has_invoked_finish_or_error_callback = true;
if (on_tls_finished)
on_tls_finished();
}
@@ -210,7 +228,7 @@ bool TLSv12::check_connection_state(bool read)
m_context.tls_buffer.size(),
m_context.application_buffer.size());
} else {
- m_context.connection_finished = false;
+ m_context.connection_finished = true;
dbgln_if(TLS_DEBUG, "FINISHED");
}
if (!m_context.application_buffer.size()) {