improve close on TLS

This commit is contained in:
Sergio R. Caprile 2024-07-30 19:30:14 -03:00
parent 4f6a78cde8
commit 3ffe5e958f
4 changed files with 16 additions and 6 deletions

View File

@ -7529,9 +7529,13 @@ static void read_conn(struct mg_connection *c) {
if (!ioalloc(c, &c->rtls)) return; if (!ioalloc(c, &c->rtls)) return;
n = recv_raw(c, (char *) &c->rtls.buf[c->rtls.len], n = recv_raw(c, (char *) &c->rtls.buf[c->rtls.len],
c->rtls.size - c->rtls.len); c->rtls.size - c->rtls.len);
if (n == MG_IO_ERR && mg_tls_pending(c) == 0) { if (n == MG_IO_ERR) {
// Close only if we have fully drained both raw (rtls) and TLS buffers if (c->rtls.len == 0 || c->is_io_err) {
c->is_closing = 1; // Close only when we have fully drained both rtls and TLS buffers
c->is_closing = 1; // or there's nothing we can do about it.
} else { // TLS buffer is capped to max record size, mark and
c->is_io_err = 1; // give TLS a chance to process that.
}
} else { } else {
if (n > 0) c->rtls.len += (size_t) n; if (n > 0) c->rtls.len += (size_t) n;
if (c->is_tls_hs) mg_tls_handshake(c); if (c->is_tls_hs) mg_tls_handshake(c);

View File

@ -2157,6 +2157,7 @@ struct mg_connection {
unsigned is_resp : 1; // Response is still being generated unsigned is_resp : 1; // Response is still being generated
unsigned is_readable : 1; // Connection is ready to read unsigned is_readable : 1; // Connection is ready to read
unsigned is_writable : 1; // Connection is ready to write unsigned is_writable : 1; // Connection is ready to write
unsigned is_io_err : 1; // Remember IO_ERR condition for later use
}; };
void mg_mgr_poll(struct mg_mgr *, int ms); void mg_mgr_poll(struct mg_mgr *, int ms);

View File

@ -76,6 +76,7 @@ struct mg_connection {
unsigned is_resp : 1; // Response is still being generated unsigned is_resp : 1; // Response is still being generated
unsigned is_readable : 1; // Connection is ready to read unsigned is_readable : 1; // Connection is ready to read
unsigned is_writable : 1; // Connection is ready to write unsigned is_writable : 1; // Connection is ready to write
unsigned is_io_err : 1; // Remember IO_ERR condition for later use
}; };
void mg_mgr_poll(struct mg_mgr *, int ms); void mg_mgr_poll(struct mg_mgr *, int ms);

View File

@ -282,9 +282,13 @@ static void read_conn(struct mg_connection *c) {
if (!ioalloc(c, &c->rtls)) return; if (!ioalloc(c, &c->rtls)) return;
n = recv_raw(c, (char *) &c->rtls.buf[c->rtls.len], n = recv_raw(c, (char *) &c->rtls.buf[c->rtls.len],
c->rtls.size - c->rtls.len); c->rtls.size - c->rtls.len);
if (n == MG_IO_ERR && mg_tls_pending(c) == 0) { if (n == MG_IO_ERR) {
// Close only if we have fully drained both raw (rtls) and TLS buffers if (c->rtls.len == 0 || c->is_io_err) {
c->is_closing = 1; // Close only when we have fully drained both rtls and TLS buffers
c->is_closing = 1; // or there's nothing we can do about it.
} else { // TLS buffer is capped to max record size, mark and
c->is_io_err = 1; // give TLS a chance to process that.
}
} else { } else {
if (n > 0) c->rtls.len += (size_t) n; if (n > 0) c->rtls.len += (size_t) n;
if (c->is_tls_hs) mg_tls_handshake(c); if (c->is_tls_hs) mg_tls_handshake(c);