Skip to content

Commit 01e7cd5

Browse files
authored
Merge pull request #2646 from cesanta/tls
Fix #2619 - use mg_tls_pending() API to check for buffered TLS data
2 parents 9f498d2 + 3ac9bd0 commit 01e7cd5

File tree

3 files changed

+30
-24
lines changed

3 files changed

+30
-24
lines changed

mongoose.c

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7237,8 +7237,8 @@ long mg_io_send(struct mg_connection *c, const void *buf, size_t len) {
72377237
bool mg_send(struct mg_connection *c, const void *buf, size_t len) {
72387238
if (c->is_udp) {
72397239
long n = mg_io_send(c, buf, len);
7240-
MG_DEBUG(("%lu %ld %d:%d %ld err %d", c->id, c->fd, (int) c->send.len,
7241-
(int) c->recv.len, n, MG_SOCK_ERR(n)));
7240+
MG_DEBUG(("%lu %ld %lu:%lu:%lu %ld err %d", c->id, c->fd, c->send.len,
7241+
c->recv.len, c->rtls.len, n, MG_SOCK_ERR(n)));
72427242
iolog(c, (char *) buf, n, false);
72437243
return n > 0;
72447244
} else {
@@ -7392,9 +7392,8 @@ static void read_conn(struct mg_connection *c) {
73927392
} else {
73937393
n = recv_raw(c, buf, len);
73947394
}
7395-
MG_DEBUG(("%lu %p snd %ld/%ld rcv %ld/%ld n=%ld err=%d", c->id, c->fd,
7396-
(long) c->send.len, (long) c->send.size, (long) c->recv.len,
7397-
(long) c->recv.size, n, MG_SOCK_ERR(n)));
7395+
MG_DEBUG(("%lu %ld %lu:%lu:%lu %ld err %d", c->id, c->fd, c->send.len,
7396+
c->recv.len, c->rtls.len, n, MG_SOCK_ERR(n)));
73987397
iolog(c, buf, n, true);
73997398
}
74007399
}
@@ -7582,7 +7581,7 @@ static void mg_iotest(struct mg_mgr *mgr, int ms) {
75827581
size_t max = 1;
75837582
for (struct mg_connection *c = mgr->conns; c != NULL; c = c->next) {
75847583
c->is_readable = c->is_writable = 0;
7585-
if (c->rtls.len > 0) ms = 1, c->is_readable = 1;
7584+
if (mg_tls_pending(c) > 0) ms = 1, c->is_readable = 1;
75867585
if (can_write(c)) MG_EPOLL_MOD(c, 1);
75877586
if (c->is_closing) ms = 1;
75887587
max++;
@@ -7598,7 +7597,7 @@ static void mg_iotest(struct mg_mgr *mgr, int ms) {
75987597
bool wr = evs[i].events & EPOLLOUT;
75997598
c->is_readable = can_read(c) && rd ? 1U : 0;
76007599
c->is_writable = can_write(c) && wr ? 1U : 0;
7601-
if (c->rtls.len > 0) c->is_readable = 1;
7600+
if (mg_tls_pending(c) > 0) c->is_readable = 1;
76027601
}
76037602
}
76047603
(void) skip_iotest;
@@ -7612,7 +7611,7 @@ static void mg_iotest(struct mg_mgr *mgr, int ms) {
76127611
c->is_readable = c->is_writable = 0;
76137612
if (skip_iotest(c)) {
76147613
// Socket not valid, ignore
7615-
} else if (c->rtls.len > 0) {
7614+
} else if (mg_tls_pending(c) > 0) {
76167615
ms = 1; // Don't wait if TLS is ready
76177616
} else {
76187617
fds[n].fd = FD(c);
@@ -7634,7 +7633,7 @@ static void mg_iotest(struct mg_mgr *mgr, int ms) {
76347633
for (struct mg_connection *c = mgr->conns; c != NULL; c = c->next) {
76357634
if (skip_iotest(c)) {
76367635
// Socket not valid, ignore
7637-
} else if (c->rtls.len > 0) {
7636+
} else if (mg_tls_pending(c) > 0) {
76387637
c->is_readable = 1;
76397638
} else {
76407639
if (fds[n].revents & POLLERR) {
@@ -7643,7 +7642,7 @@ static void mg_iotest(struct mg_mgr *mgr, int ms) {
76437642
c->is_readable =
76447643
(unsigned) (fds[n].revents & (POLLIN | POLLHUP) ? 1 : 0);
76457644
c->is_writable = (unsigned) (fds[n].revents & POLLOUT ? 1 : 0);
7646-
if (c->rtls.len > 0) c->is_readable = 1;
7645+
if (mg_tls_pending(c) > 0) c->is_readable = 1;
76477646
}
76487647
n++;
76497648
}
@@ -7665,7 +7664,7 @@ static void mg_iotest(struct mg_mgr *mgr, int ms) {
76657664
FD_SET(FD(c), &eset);
76667665
if (can_read(c)) FD_SET(FD(c), &rset);
76677666
if (can_write(c)) FD_SET(FD(c), &wset);
7668-
if (c->rtls.len > 0) tvp = &tv_zero;
7667+
if (mg_tls_pending(c) > 0) tvp = &tv_zero;
76697668
if (FD(c) > maxfd) maxfd = FD(c);
76707669
if (c->is_closing) ms = 1;
76717670
}
@@ -7687,7 +7686,7 @@ static void mg_iotest(struct mg_mgr *mgr, int ms) {
76877686
} else {
76887687
c->is_readable = FD(c) != MG_INVALID_SOCKET && FD_ISSET(FD(c), &rset);
76897688
c->is_writable = FD(c) != MG_INVALID_SOCKET && FD_ISSET(FD(c), &wset);
7690-
if (c->rtls.len > 0) c->is_readable = 1;
7689+
if (mg_tls_pending(c) > 0) c->is_readable = 1;
76917690
}
76927691
}
76937692
#endif

src/sock.c

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,8 @@ long mg_io_send(struct mg_connection *c, const void *buf, size_t len) {
136136
bool mg_send(struct mg_connection *c, const void *buf, size_t len) {
137137
if (c->is_udp) {
138138
long n = mg_io_send(c, buf, len);
139-
MG_DEBUG(("%lu %ld %d:%d %ld err %d", c->id, c->fd, (int) c->send.len,
140-
(int) c->recv.len, n, MG_SOCK_ERR(n)));
139+
MG_DEBUG(("%lu %ld %lu:%lu:%lu %ld err %d", c->id, c->fd, c->send.len,
140+
c->recv.len, c->rtls.len, n, MG_SOCK_ERR(n)));
141141
iolog(c, (char *) buf, n, false);
142142
return n > 0;
143143
} else {
@@ -291,9 +291,8 @@ static void read_conn(struct mg_connection *c) {
291291
} else {
292292
n = recv_raw(c, buf, len);
293293
}
294-
MG_DEBUG(("%lu %p snd %ld/%ld rcv %ld/%ld n=%ld err=%d", c->id, c->fd,
295-
(long) c->send.len, (long) c->send.size, (long) c->recv.len,
296-
(long) c->recv.size, n, MG_SOCK_ERR(n)));
294+
MG_DEBUG(("%lu %ld %lu:%lu:%lu %ld err %d", c->id, c->fd, c->send.len,
295+
c->recv.len, c->rtls.len, n, MG_SOCK_ERR(n)));
297296
iolog(c, buf, n, true);
298297
}
299298
}
@@ -481,7 +480,7 @@ static void mg_iotest(struct mg_mgr *mgr, int ms) {
481480
size_t max = 1;
482481
for (struct mg_connection *c = mgr->conns; c != NULL; c = c->next) {
483482
c->is_readable = c->is_writable = 0;
484-
if (c->rtls.len > 0) ms = 1, c->is_readable = 1;
483+
if (mg_tls_pending(c) > 0) ms = 1, c->is_readable = 1;
485484
if (can_write(c)) MG_EPOLL_MOD(c, 1);
486485
if (c->is_closing) ms = 1;
487486
max++;
@@ -497,7 +496,7 @@ static void mg_iotest(struct mg_mgr *mgr, int ms) {
497496
bool wr = evs[i].events & EPOLLOUT;
498497
c->is_readable = can_read(c) && rd ? 1U : 0;
499498
c->is_writable = can_write(c) && wr ? 1U : 0;
500-
if (c->rtls.len > 0) c->is_readable = 1;
499+
if (mg_tls_pending(c) > 0) c->is_readable = 1;
501500
}
502501
}
503502
(void) skip_iotest;
@@ -511,7 +510,7 @@ static void mg_iotest(struct mg_mgr *mgr, int ms) {
511510
c->is_readable = c->is_writable = 0;
512511
if (skip_iotest(c)) {
513512
// Socket not valid, ignore
514-
} else if (c->rtls.len > 0) {
513+
} else if (mg_tls_pending(c) > 0) {
515514
ms = 1; // Don't wait if TLS is ready
516515
} else {
517516
fds[n].fd = FD(c);
@@ -533,7 +532,7 @@ static void mg_iotest(struct mg_mgr *mgr, int ms) {
533532
for (struct mg_connection *c = mgr->conns; c != NULL; c = c->next) {
534533
if (skip_iotest(c)) {
535534
// Socket not valid, ignore
536-
} else if (c->rtls.len > 0) {
535+
} else if (mg_tls_pending(c) > 0) {
537536
c->is_readable = 1;
538537
} else {
539538
if (fds[n].revents & POLLERR) {
@@ -542,7 +541,7 @@ static void mg_iotest(struct mg_mgr *mgr, int ms) {
542541
c->is_readable =
543542
(unsigned) (fds[n].revents & (POLLIN | POLLHUP) ? 1 : 0);
544543
c->is_writable = (unsigned) (fds[n].revents & POLLOUT ? 1 : 0);
545-
if (c->rtls.len > 0) c->is_readable = 1;
544+
if (mg_tls_pending(c) > 0) c->is_readable = 1;
546545
}
547546
n++;
548547
}
@@ -564,7 +563,7 @@ static void mg_iotest(struct mg_mgr *mgr, int ms) {
564563
FD_SET(FD(c), &eset);
565564
if (can_read(c)) FD_SET(FD(c), &rset);
566565
if (can_write(c)) FD_SET(FD(c), &wset);
567-
if (c->rtls.len > 0) tvp = &tv_zero;
566+
if (mg_tls_pending(c) > 0) tvp = &tv_zero;
568567
if (FD(c) > maxfd) maxfd = FD(c);
569568
if (c->is_closing) ms = 1;
570569
}
@@ -586,7 +585,7 @@ static void mg_iotest(struct mg_mgr *mgr, int ms) {
586585
} else {
587586
c->is_readable = FD(c) != MG_INVALID_SOCKET && FD_ISSET(FD(c), &rset);
588587
c->is_writable = FD(c) != MG_INVALID_SOCKET && FD_ISSET(FD(c), &wset);
589-
if (c->rtls.len > 0) c->is_readable = 1;
588+
if (mg_tls_pending(c) > 0) c->is_readable = 1;
590589
}
591590
}
592591
#endif

test/unit_test.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1210,6 +1210,7 @@ static void test_tls(void) {
12101210
const char *url = "https://127.0.0.1:12347";
12111211
char buf[FETCH_BUF_SIZE];
12121212
struct mg_tls_opts opts;
1213+
struct mg_str data = mg_unpacked("/Makefile");
12131214
memset(&opts, 0, sizeof(opts));
12141215
// opts.ca = mg_str(s_tls_ca);
12151216
opts.cert = mg_str(s_tls_cert);
@@ -1220,6 +1221,13 @@ static void test_tls(void) {
12201221
ASSERT(fetch(&mgr, buf, url, "GET /a.txt HTTP/1.0\n\n") == 200);
12211222
// MG_INFO(("%s", buf));
12221223
ASSERT(cmpbody(buf, "hello\n") == 0);
1224+
// POST a larger file, make sure we drain TLS buffers and read all, #2619
1225+
ASSERT(data.ptr != NULL && data.len > 0);
1226+
ASSERT(fetch(&mgr, buf, url,
1227+
"POST /foo/bar HTTP/1.0\n"
1228+
"Content-Length: %lu\n\n"
1229+
"%s",
1230+
data.len, data.ptr) == 200);
12231231
mg_mgr_free(&mgr);
12241232
ASSERT(mgr.conns == NULL);
12251233
#endif

0 commit comments

Comments
 (0)