Skip to content

Commit 1b75017

Browse files
authored
Merge pull request #2849 from cesanta/nit
improve close on TLS
2 parents 4f6a78c + 3ffe5e9 commit 1b75017

File tree

4 files changed

+16
-6
lines changed

4 files changed

+16
-6
lines changed

mongoose.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7529,9 +7529,13 @@ static void read_conn(struct mg_connection *c) {
75297529
if (!ioalloc(c, &c->rtls)) return;
75307530
n = recv_raw(c, (char *) &c->rtls.buf[c->rtls.len],
75317531
c->rtls.size - c->rtls.len);
7532-
if (n == MG_IO_ERR && mg_tls_pending(c) == 0) {
7533-
// Close only if we have fully drained both raw (rtls) and TLS buffers
7534-
c->is_closing = 1;
7532+
if (n == MG_IO_ERR) {
7533+
if (c->rtls.len == 0 || c->is_io_err) {
7534+
// Close only when we have fully drained both rtls and TLS buffers
7535+
c->is_closing = 1; // or there's nothing we can do about it.
7536+
} else { // TLS buffer is capped to max record size, mark and
7537+
c->is_io_err = 1; // give TLS a chance to process that.
7538+
}
75357539
} else {
75367540
if (n > 0) c->rtls.len += (size_t) n;
75377541
if (c->is_tls_hs) mg_tls_handshake(c);

mongoose.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2157,6 +2157,7 @@ struct mg_connection {
21572157
unsigned is_resp : 1; // Response is still being generated
21582158
unsigned is_readable : 1; // Connection is ready to read
21592159
unsigned is_writable : 1; // Connection is ready to write
2160+
unsigned is_io_err : 1; // Remember IO_ERR condition for later use
21602161
};
21612162

21622163
void mg_mgr_poll(struct mg_mgr *, int ms);

src/net.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ struct mg_connection {
7676
unsigned is_resp : 1; // Response is still being generated
7777
unsigned is_readable : 1; // Connection is ready to read
7878
unsigned is_writable : 1; // Connection is ready to write
79+
unsigned is_io_err : 1; // Remember IO_ERR condition for later use
7980
};
8081

8182
void mg_mgr_poll(struct mg_mgr *, int ms);

src/sock.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -282,9 +282,13 @@ static void read_conn(struct mg_connection *c) {
282282
if (!ioalloc(c, &c->rtls)) return;
283283
n = recv_raw(c, (char *) &c->rtls.buf[c->rtls.len],
284284
c->rtls.size - c->rtls.len);
285-
if (n == MG_IO_ERR && mg_tls_pending(c) == 0) {
286-
// Close only if we have fully drained both raw (rtls) and TLS buffers
287-
c->is_closing = 1;
285+
if (n == MG_IO_ERR) {
286+
if (c->rtls.len == 0 || c->is_io_err) {
287+
// Close only when we have fully drained both rtls and TLS buffers
288+
c->is_closing = 1; // or there's nothing we can do about it.
289+
} else { // TLS buffer is capped to max record size, mark and
290+
c->is_io_err = 1; // give TLS a chance to process that.
291+
}
288292
} else {
289293
if (n > 0) c->rtls.len += (size_t) n;
290294
if (c->is_tls_hs) mg_tls_handshake(c);

0 commit comments

Comments
 (0)