From d8fec13d2c3083bdc8f4b1e0618e43ba7bd722cd Mon Sep 17 00:00:00 2001 From: Sergey Lyubka Date: Tue, 23 Jan 2024 11:09:09 +0000 Subject: [PATCH] Fix #2570: close only when TLS buffers drained --- mongoose.c | 8 +++----- src/sock.c | 9 ++++----- 2 files changed, 7 insertions(+), 10 deletions(-) diff --git a/mongoose.c b/mongoose.c index b8693a8d..9e4303d2 100644 --- a/mongoose.c +++ b/mongoose.c @@ -7299,15 +7299,13 @@ static void read_conn(struct mg_connection *c) { n = recv_raw(c, (char *) &c->rtls.buf[c->rtls.len], c->rtls.size - c->rtls.len); // MG_DEBUG(("%lu %ld", c->id, n)); - if (n == MG_IO_ERR) { + if (n == MG_IO_ERR && mg_tls_pending(c) == 0 && c->rtls.len == 0) { c->is_closing = 1; - } else if (n > 0) { - c->rtls.len += (size_t) n; + } else { + if (n > 0) c->rtls.len += (size_t) n; if (c->is_tls_hs) mg_tls_handshake(c); if (c->is_tls_hs) return; n = mg_tls_recv(c, buf, len); - } else if (n == MG_IO_WAIT) { - n = mg_tls_recv(c, buf, len); } } else { n = recv_raw(c, buf, len); diff --git a/src/sock.c b/src/sock.c index 6d98dd2a..64ab0c55 100644 --- a/src/sock.c +++ b/src/sock.c @@ -282,15 +282,14 @@ static void read_conn(struct mg_connection *c) { n = recv_raw(c, (char *) &c->rtls.buf[c->rtls.len], c->rtls.size - c->rtls.len); // MG_DEBUG(("%lu %ld", c->id, n)); - if (n == MG_IO_ERR) { + if (n == MG_IO_ERR && mg_tls_pending(c) == 0 && c->rtls.len == 0) { + // Close only if we have fully drained both raw (rtls) and TLS buffers c->is_closing = 1; - } else if (n > 0) { - c->rtls.len += (size_t) n; + } else { + if (n > 0) c->rtls.len += (size_t) n; if (c->is_tls_hs) mg_tls_handshake(c); if (c->is_tls_hs) return; n = mg_tls_recv(c, buf, len); - } else if (n == MG_IO_WAIT) { - n = mg_tls_recv(c, buf, len); } } else { n = recv_raw(c, buf, len);