1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 20:57:35 +00:00

LibTLS: Close the underlying socket on EOF

There's no reason to keep waiting when there's nothing else to come.
This makes RequestServer not spin on Core::Socket::read() (in some
scenarios).
This commit is contained in:
Ali Mohammad Pur 2021-09-16 00:26:03 +04:30 committed by Andreas Kling
parent ef22a1b2dc
commit 23febbed41
2 changed files with 21 additions and 13 deletions

View file

@ -142,20 +142,21 @@ bool TLSv12::common_connect(const struct sockaddr* saddr, socklen_t length)
return true;
}
void TLSv12::notify_client_for_app_data()
{
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);
}
m_has_scheduled_app_data_flush = false;
}
void TLSv12::read_from_socket()
{
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 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();
@ -188,10 +189,15 @@ 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);
@ -210,7 +216,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()) {