mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 23:27:36 +00:00
LibTLS: "Properly" handle the server dropping the connection
Contrary to popular belief, not every implementation of TLS follows the specs. Some of them just drop the connection without sending a proper close_notify, and we should handle that gracefully.
This commit is contained in:
parent
8aeccf4f02
commit
b028a123b8
3 changed files with 42 additions and 30 deletions
|
@ -156,6 +156,17 @@ void HttpsJob::on_socket_connected()
|
||||||
}
|
}
|
||||||
ASSERT(m_state == State::InBody);
|
ASSERT(m_state == State::InBody);
|
||||||
ASSERT(tls.can_read());
|
ASSERT(tls.can_read());
|
||||||
|
|
||||||
|
while (tls.can_read())
|
||||||
|
read_body(tls);
|
||||||
|
|
||||||
|
if (!tls.is_established())
|
||||||
|
return finish_up();
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
void HttpsJob::read_body(TLS::TLSv12& tls)
|
||||||
|
{
|
||||||
auto payload = tls.read(64 * KB);
|
auto payload = tls.read(64 * KB);
|
||||||
if (!payload) {
|
if (!payload) {
|
||||||
if (tls.eof())
|
if (tls.eof())
|
||||||
|
@ -186,7 +197,6 @@ void HttpsJob::on_socket_connected()
|
||||||
finish_up();
|
finish_up();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void HttpsJob::finish_up()
|
void HttpsJob::finish_up()
|
||||||
|
|
|
@ -50,6 +50,7 @@ private:
|
||||||
RefPtr<TLS::TLSv12> construct_socket() { return TLS::TLSv12::construct(this); }
|
RefPtr<TLS::TLSv12> construct_socket() { return TLS::TLSv12::construct(this); }
|
||||||
void on_socket_connected();
|
void on_socket_connected();
|
||||||
void finish_up();
|
void finish_up();
|
||||||
|
void read_body(TLS::TLSv12&);
|
||||||
|
|
||||||
enum class State {
|
enum class State {
|
||||||
InStatus,
|
InStatus,
|
||||||
|
@ -66,6 +67,7 @@ private:
|
||||||
Vector<ByteBuffer> m_received_buffers;
|
Vector<ByteBuffer> m_received_buffers;
|
||||||
size_t m_received_size { 0 };
|
size_t m_received_size { 0 };
|
||||||
bool m_sent_data { false };
|
bool m_sent_data { false };
|
||||||
|
bool m_queued_finish { false };
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -113,7 +113,7 @@ bool TLSv12::common_connect(const struct sockaddr* saddr, socklen_t length)
|
||||||
|
|
||||||
Core::Socket::on_connected = [this] {
|
Core::Socket::on_connected = [this] {
|
||||||
Core::Socket::on_ready_to_read = [this] {
|
Core::Socket::on_ready_to_read = [this] {
|
||||||
if (!Core::Socket::is_open()) {
|
if (!Core::Socket::is_open() || !Core::Socket::is_connected() || Core::Socket::eof()) {
|
||||||
// an abrupt closure (the server is a jerk)
|
// an abrupt closure (the server is a jerk)
|
||||||
dbg() << "Socket not open, assuming abrupt closure";
|
dbg() << "Socket not open, assuming abrupt closure";
|
||||||
m_context.connection_finished = true;
|
m_context.connection_finished = true;
|
||||||
|
@ -143,7 +143,7 @@ bool TLSv12::common_connect(const struct sockaddr* saddr, socklen_t length)
|
||||||
on_tls_ready_to_read(*this);
|
on_tls_ready_to_read(*this);
|
||||||
};
|
};
|
||||||
Core::Socket::on_ready_to_write = [this] {
|
Core::Socket::on_ready_to_write = [this] {
|
||||||
if (!Core::Socket::is_open()) {
|
if (!Core::Socket::is_open() || !Core::Socket::is_connected() || Core::Socket::eof()) {
|
||||||
// an abrupt closure (the server is a jerk)
|
// an abrupt closure (the server is a jerk)
|
||||||
dbg() << "Socket not open, assuming abrupt closure";
|
dbg() << "Socket not open, assuming abrupt closure";
|
||||||
m_context.connection_finished = true;
|
m_context.connection_finished = true;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue