Skip to content

Commit d9c4945

Browse files
committed
UPD | fix bugs
1 parent bf78ffa commit d9c4945

File tree

6 files changed

+82
-39
lines changed

6 files changed

+82
-39
lines changed

include/worker/ManapiTcp.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,8 @@ namespace manapi::net::worker {
7777

7878
int onaccept_bind_ (const worker::shared_conn &conn) MANAPIHTTP_NOEXCEPT;
7979

80+
virtual int conn_after_write (const worker::shared_conn &conn) MANAPIHTTP_NOEXCEPT;
81+
8082
ev::shared_tcp watcher_accept_;
8183
protected:
8284
std::size_t count;

include/worker/ManapiTlsOverTcp.hpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,8 @@ namespace manapi::net::worker {
9191
int ssl_session_ctx_id{1};
9292
protected:
9393
int onaccept_event_(const worker::shared_conn &conn) MANAPIHTTP_NOEXCEPT override;
94+
95+
int conn_after_write(const worker::shared_conn &conn) MANAPIHTTP_NOEXCEPT override;
9496
private:
9597
static shared_conn connection_init_cb (void *user_data) MANAPIHTTP_NOEXCEPT;
9698

@@ -100,7 +102,7 @@ namespace manapi::net::worker {
100102

101103
int manapi_do_handshake_ (const shared_conn &conn, tls_connection_t *data);
102104

103-
int ssl_bio_flush_write_ (const shared_conn &conn, tls_connection_t *m, std::size_t max_cnt);
105+
int ssl_bio_flush_write_ (const shared_conn &conn, tls_connection_t *m, std::size_t max_cnt, bool fin);
104106

105107
int ssl_bio_flush_read_ (const shared_conn &conn, tls_connection_t *m, std::size_t max_cnt);
106108

main.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@ int main() {
2121
resp->file (manapi::filesystem::path::join(FOLDER, "index.html")).unwrap();
2222
}).unwrap();
2323

24-
route.GET ("/", FOLDER).unwrap();
24+
route.GET ("/", FOLDER, [] (http::req &req, http::uresp resp) -> void {
25+
resp->compress_enabled(true);
26+
}).unwrap();
2527

2628
manapi::async::run ([route] () mutable -> manapi::future<> {
2729
manapi::unwrap(co_await route.config(manapi::filesystem::path::join(".", "config.json")));

src/include/ManapiUtils.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
# define MANAPIHTTP_NONUNIX false
3333
#endif
3434

35-
#define WORKER_MAX_CNT 100000
35+
#define WORKER_MAX_CNT INT_MAX
3636

3737
namespace manapi::sockets {
3838
enum ip_version {

src/worker/ManapiTcp.cpp

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,9 @@ void manapi::net::worker::TCP::close_connection(shared_conn conn, int flags) MAN
388388

389389
auto connection = conn->as<tcp_connection_t>();
390390

391+
manapi_log_trace(manapi::debug::LOG_TRACE_LOW, "TCP:close_conection() %p flags=%d",
392+
conn.get(), flags);
393+
391394
if (flags & CLOSE_CONN_EOR || flags & CLOSE_CONN_EOS) {
392395
conn->wrk.flags |= WRK_INTERFACE_IS_DRAINING;
393396
}
@@ -407,7 +410,7 @@ void manapi::net::worker::TCP::close_connection(shared_conn conn, int flags) MAN
407410
connection->flags |= CONN_RECV_END;
408411
connection->flags |= CONN_SEND_END;
409412

410-
manapi_log_trace(manapi::debug::LOG_TRACE_LOW, "tcp:closing the connection %p with CLOSE_CONN_FINISHED "
413+
manapi_log_trace(manapi::debug::LOG_TRACE_LOW, "TCP:closing the connection %p with CLOSE_CONN_FINISHED "
411414
"top->cur_send_size=%d top->send_size=%d", conn.get(), connection->top->cur_send_size, connection->top->send_size);
412415

413416
if (connection->top->cur_send_size != connection->top->send_size) {
@@ -805,8 +808,6 @@ int manapi::net::worker::TCP::flush_write_(const worker::shared_conn &connection
805808
auto conn = connection->as<tcp_connection_t>();
806809

807810
conn->top->send_size -= nbuff;
808-
s.reset();
809-
b.reset();
810811

811812
if (status) {
812813
/* error */
@@ -828,13 +829,17 @@ int manapi::net::worker::TCP::flush_write_(const worker::shared_conn &connection
828829
return;
829830
}
830831

832+
if (dynamic_cast<TCP*> (conn->worker)->conn_after_write(connection)) {
833+
conn->worker->close_connection(connection, CLOSE_CONN_ERR);
834+
return;
835+
}
836+
831837
if (conn->flags & CONN_SEND_END && conn->flags & CONN_RECV_END) {
832-
manapi_log_trace(manapi::debug::LOG_TRACE_LOW, "packets were sent after the closing conn %p",
838+
manapi_log_trace(manapi::debug::LOG_TRACE_LOW, "TCP:packets were sent after closing conn %p",
833839
connection.get());
834840
conn->worker->close_connection(connection, CLOSE_CONN_FINISHED);
835841
return;
836842
}
837-
838843
}, buffptr, nbuff /* nbuf */);
839844

840845
conn->top->cur_send_size = 0;
@@ -863,7 +868,7 @@ int manapi::net::worker::TCP::flush_write_(const worker::shared_conn &connection
863868
}
864869

865870
}
866-
}
871+
}
867872
}
868873

869874
return CONN_IO_OK;
@@ -1009,3 +1014,8 @@ int manapi::net::worker::TCP::onaccept_bind_(const worker::shared_conn &conn) MA
10091014

10101015
return 0;
10111016
}
1017+
1018+
int manapi::net::worker::TCP::conn_after_write(const worker::shared_conn &conn) MANAPIHTTP_NOEXCEPT {
1019+
// nothing.
1020+
return ERR_OK;
1021+
}

src/worker/ManapiTlsOverTcp.cpp

Lines changed: 57 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ void manapi::net::worker::TLS::close_connection(shared_conn conn, int flags) MAN
6060
if (connection->flags & CONN_REMOVED)
6161
return;
6262

63-
manapi_log_trace(debug::LOG_TRACE_LOW, "TLS:close_connection() %p flags=%d", connection, flags);
63+
manapi_log_trace(debug::LOG_TRACE_LOW, "TLS:close_connection() %p flags=%d", conn.get(), flags);
6464

6565
if ((connection->flags & CONN_TLS_SHUTDOWN)) {
6666
if ((flags & (CLOSE_CONN_EOS|CLOSE_CONN_EOR)))
@@ -137,15 +137,15 @@ ssize_t manapi::net::worker::TLS::sync_write_ex(const shared_conn &conn, ev::buf
137137
return -1;
138138

139139
char buffer[32768];
140-
size_t cursor = 0;
140+
int cursor = 0;
141141
size_t lastcur = 0;
142142
ssize_t total = 0;
143143

144144
while (nbuff) {
145145
if (connection->top->send_size > maxcnt)
146146
break;
147147

148-
auto const copy = std::min<std::size_t>(buff->len - lastcur, sizeof (buffer) - cursor);
148+
auto const copy = std::min<int>(buff->len - lastcur, sizeof (buffer) - cursor);
149149
memcpy (buffer + cursor, buff->base + lastcur, copy);
150150

151151
lastcur += copy;
@@ -158,7 +158,7 @@ ssize_t manapi::net::worker::TLS::sync_write_ex(const shared_conn &conn, ev::buf
158158
}
159159

160160
if (cursor == sizeof (buffer) || (!nbuff && cursor)) {
161-
size_t current = 0;
161+
int current = 0;
162162
while (current != cursor) {
163163
int rhs;
164164

@@ -183,7 +183,7 @@ ssize_t manapi::net::worker::TLS::sync_write_ex(const shared_conn &conn, ev::buf
183183
if (err) {
184184
if (err == this->ssl_error_want_read_
185185
|| err == this->ssl_error_want_write_) {
186-
auto const erhs = this->ssl_bio_flush_write_(conn, connection, maxcnt);
186+
auto const erhs = this->ssl_bio_flush_write_(conn, connection, maxcnt, false);
187187

188188
if (erhs) {
189189
if (erhs == CONN_IO_WANT_WRITE) {
@@ -202,7 +202,7 @@ ssize_t manapi::net::worker::TLS::sync_write_ex(const shared_conn &conn, ev::buf
202202
return total + current;
203203

204204
continue;
205-
}
205+
}
206206

207207
return CONN_IO_ERROR;
208208
}
@@ -224,9 +224,9 @@ ssize_t manapi::net::worker::TLS::sync_write_ex(const shared_conn &conn, ev::buf
224224
}
225225
}
226226

227-
bool const cfinish = finish && total == size;
228227

229-
auto const err = this->ssl_bio_flush_write_(conn, connection, maxcnt);
228+
bool const cfinish = finish && total == size;
229+
auto const err = this->ssl_bio_flush_write_(conn, connection, maxcnt, cfinish);
230230

231231
if (err) {
232232
if (err == CONN_IO_WANT_WRITE) {
@@ -384,7 +384,7 @@ void manapi::net::worker::TLS::shutdown_async_(shared_conn conn) {
384384
bool flg = false;
385385
while (true) {
386386
auto rhs = this->ssl_shutdown_(s->ssl);
387-
manapi_log_trace(debug::LOG_TRACE_LOW, "TLS:Shutdown %p = %d", s, rhs);
387+
manapi_log_trace(debug::LOG_TRACE_LOW, "TLS:Shutdown %p = %d", conn.get(), rhs);
388388

389389

390390
if (!rhs) {
@@ -419,7 +419,7 @@ void manapi::net::worker::TLS::shutdown_async_(shared_conn conn) {
419419
}
420420

421421
if (err == this->ssl_error_want_write_) {
422-
if (this->ssl_bio_flush_write_(conn, s, WORKER_MAX_CNT))
422+
if (this->ssl_bio_flush_write_(conn, s, WORKER_MAX_CNT, false))
423423
goto err;
424424
if (this->flush_write_(conn, true))
425425
goto err;
@@ -455,7 +455,7 @@ void manapi::net::worker::TLS::shutdown_async_(shared_conn conn) {
455455
}
456456

457457
write:
458-
if (this->ssl_bio_flush_write_(conn, s, WORKER_MAX_CNT))
458+
if (this->ssl_bio_flush_write_(conn, s, WORKER_MAX_CNT, false))
459459
goto err;
460460
if (this->flush_write_(conn, true))
461461
goto err;
@@ -475,7 +475,7 @@ void manapi::net::worker::TLS::onrecv(const std::shared_ptr<ev::tcp> &watcher, c
475475
this->read_stop_(conn, data);
476476
}
477477

478-
manapi_log_trace_hard("TLS:recv %p flags=%d size=%zu", data, data->flags, buffer.size());
478+
manapi_log_trace_hard("TLS:recv %p flags=%d size=%zu", conn.get(), data->flags, buffer.size());
479479

480480
while (size) {
481481
auto rhs = this->ssl_bio_write_(data->rbio, buff, static_cast<int>(size));
@@ -513,7 +513,7 @@ void manapi::net::worker::TLS::onrecv(const std::shared_ptr<ev::tcp> &watcher, c
513513
data->flags ^= CONN_TLS_EARLY_DATA;
514514
data->flags |= CONN_TLS_EARLY_FINISHED;
515515
manapi_log_trace(debug::LOG_TRACE_LOW,
516-
"TLS:Early data was read %p", data);
516+
"TLS:Early data was read %p", conn.get());
517517
continue;
518518
}
519519

@@ -532,7 +532,7 @@ void manapi::net::worker::TLS::onrecv(const std::shared_ptr<ev::tcp> &watcher, c
532532
err == this->ssl_error_want_read_ ||
533533
err == this->ssl_error_want_write_) {
534534

535-
if (this->ssl_bio_flush_write_(conn, data, WORKER_MAX_CNT))
535+
if (this->ssl_bio_flush_write_(conn, data, WORKER_MAX_CNT, false))
536536
goto err;
537537
if (this->flush_write_(conn, true))
538538
goto err;
@@ -603,6 +603,30 @@ int manapi::net::worker::TLS::onaccept_event_(const worker::shared_conn &conn) M
603603
return ERR_OK;
604604
}
605605

606+
int manapi::net::worker::TLS::conn_after_write(const worker::shared_conn &conn) MANAPIHTTP_NOEXCEPT {
607+
if (auto rhs = TCP::conn_after_write(conn)) {
608+
return rhs;
609+
}
610+
611+
auto const err = this->ssl_bio_flush_write_(conn, conn->as<tls_connection_t>(),
612+
this->config_->max_buffer_stack, false);
613+
614+
if (err) {
615+
if (err == CONN_IO_WANT_WRITE) {
616+
if (this->flush_write_(conn, true)) {
617+
this->close_connection(conn, CLOSE_CONN_ERR);
618+
619+
return ERR_UNAVAILABLE;
620+
}
621+
return ERR_OK;
622+
}
623+
624+
this->close_connection(conn, CLOSE_CONN_ERR);
625+
}
626+
627+
return ERR_OK;
628+
}
629+
606630
manapi::net::worker::shared_conn manapi::net::worker::TLS::connection_init_cb(void *user_data) MANAPIHTTP_NOEXCEPT {
607631
auto const w = static_cast<TLS *> (user_data);
608632
try {
@@ -694,7 +718,7 @@ int manapi::net::worker::TLS::manapi_do_process(const shared_conn &conn, tls_con
694718
case CONN_IO_OK:
695719
data->flags ^= CONN_TLS_EARLY_DATA;
696720
manapi_log_trace(debug::LOG_TRACE_LOW,
697-
"TLS:Early data was read %p", data);
721+
"TLS:Early data was read %p", conn.get());
698722
break;
699723
case CONN_IO_ERROR:
700724
return CONN_IO_ERROR;
@@ -713,7 +737,7 @@ int manapi::net::worker::TLS::manapi_do_process(const shared_conn &conn, tls_con
713737
auto const err = this->ssl_get_error_(data->ssl, nread);
714738
if (err == this->ssl_error_want_read_ || err == this->ssl_error_want_write_) {
715739
/* force write all data */
716-
if (this->ssl_bio_flush_write_(conn, data, WORKER_MAX_CNT)) {
740+
if (this->ssl_bio_flush_write_(conn, data, WORKER_MAX_CNT, false)) {
717741
return CONN_IO_ERROR;
718742
}
719743

@@ -758,12 +782,12 @@ int manapi::net::worker::TLS::manapi_do_handshake_(const shared_conn &conn, tls_
758782

759783
if (!(data->flags & (CONN_TLS_EARLY_DATA|CONN_TLS_EARLY_FINISHED)) && this->ssl_early_data_is_enabled_(this->ctx)) {
760784
data->flags |= CONN_TLS_EARLY_DATA;
761-
manapi_log_trace(debug::LOG_TRACE_LOW, "TLS:Try early data %p", data);
785+
manapi_log_trace(debug::LOG_TRACE_LOW, "TLS:Try early data %p", conn.get());
762786
return CONN_IO_OK;
763787
}
764788

765789
int rhs = this->ssl_accept_(data->ssl);
766-
manapi_log_trace(debug::LOG_TRACE_LOW, "TLS:Handshake() %p = %d", data, rhs);
790+
manapi_log_trace(debug::LOG_TRACE_LOW, "TLS:Handshake() %p = %d", conn.get(), rhs);
767791

768792
if (rhs == 1) {
769793
if (!conn->wrk.data) {
@@ -776,7 +800,7 @@ int manapi::net::worker::TLS::manapi_do_handshake_(const shared_conn &conn, tls_
776800

777801
if (status == this->ssl_error_want_read_ || status == this->ssl_error_want_write_) {
778802
/* force write all data */
779-
if (this->ssl_bio_flush_write_(conn, data, WORKER_MAX_CNT)) {
803+
if (this->ssl_bio_flush_write_(conn, data, WORKER_MAX_CNT, false)) {
780804
return CONN_IO_ERROR;
781805
}
782806

@@ -805,7 +829,7 @@ int manapi::net::worker::TLS::manapi_do_handshake_(const shared_conn &conn, tls_
805829
return CONN_IO_AGAIN;
806830
}
807831

808-
int manapi::net::worker::TLS::ssl_bio_flush_write_(const shared_conn &conn, tls_connection_t *m, std::size_t max_cnt) {
832+
int manapi::net::worker::TLS::ssl_bio_flush_write_(const shared_conn &conn, tls_connection_t *m, std::size_t max_cnt, bool fin) {
809833
int rhs;
810834
auto top = &m->top->send;
811835

@@ -814,22 +838,25 @@ int manapi::net::worker::TLS::ssl_bio_flush_write_(const shared_conn &conn, tls_
814838

815839
try {
816840
while (m->top) {
817-
if (max_cnt < m->top->send_size)
841+
if ((max_cnt < m->top->send_size) && !fin)
818842
break;
819843

820-
if (max_cnt < 10000)
821-
nfastfast = std::min<ssize_t>((max_cnt - m->top->send_size + 1) * this->config_->buffer_size,
844+
if (max_cnt != WORKER_MAX_CNT) {
845+
const std::size_t calc = (max_cnt - m->top->send_size + 1) * this->config_->buffer_size;
846+
nfastfast = std::min<ssize_t>(static_cast<ssize_t>(calc),
822847
(sizeof (fastfast)));
823-
else
848+
}
849+
else {
824850
nfastfast = sizeof (fastfast);
851+
}
825852

826853
rhs = this->ssl_bio_read_(m->wbio, fastfast,
827854
static_cast<int>(nfastfast));
828855

829856
if (rhs > 0) {
830857
ssize_t alr = 0;
831858
if (!m->top->send_size && (rhs > 32 || (m->flags & CONN_TLS_SHUTDOWN))) {
832-
manapi_log_trace_hard("TLS:write %p flags=%d size=%d", m, m->flags, rhs);
859+
manapi_log_trace_hard("TLS:write %p flags=%d size=%d", conn.get(), m->flags, rhs);
833860
alr = m->watcher->try_write(fastfast, rhs);
834861
if (alr < 0) {
835862
if (alr == ev::ERR_AGAIN)
@@ -846,7 +873,7 @@ int manapi::net::worker::TLS::ssl_bio_flush_write_(const shared_conn &conn, tls_
846873
auto const prev = m->top->send_size;
847874

848875
if (TLS::connection_io_send(top, fastfast + alr, rhs - alr, &this->bufferpool(),
849-
this->config_->buffer_size, &m->top->send_size, WORKER_MAX_CNT) < 0) {
876+
this->config_->buffer_size, &m->top->send_size, WORKER_MAX_CNT) != (rhs - alr)) {
850877
return CONN_IO_ERROR;
851878
}
852879

@@ -945,7 +972,7 @@ int manapi::net::worker::TLS::ssl_bio_flush_read_(const shared_conn &conn, tls_c
945972
if (max_cnt < m->top->recv_size)
946973
break;
947974

948-
if (max_cnt < 10000)
975+
if (max_cnt < WORKER_MAX_CNT)
949976
nfastfast = std::min<ssize_t>((max_cnt - m->top->recv_size + 1) * this->config_->buffer_size,
950977
(sizeof (fastfast)));
951978
else
@@ -961,7 +988,7 @@ int manapi::net::worker::TLS::ssl_bio_flush_read_(const shared_conn &conn, tls_c
961988
else {
962989
rhs = static_cast<int>(readbytes);
963990

964-
if (this->ssl_bio_flush_write_(conn, m, WORKER_MAX_CNT)) {
991+
if (this->ssl_bio_flush_write_(conn, m, WORKER_MAX_CNT, false)) {
965992
return CONN_IO_ERROR;
966993
}
967994

@@ -975,7 +1002,7 @@ int manapi::net::worker::TLS::ssl_bio_flush_read_(const shared_conn &conn, tls_c
9751002
case CONN_IO_OK:
9761003
m->flags ^= CONN_TLS_EARLY_DATA;
9771004
manapi_log_trace(debug::LOG_TRACE_LOW,
978-
"TLS:Early data was read %p", m);
1005+
"TLS:Early data was read %p", conn.get());
9791006
break;
9801007
case CONN_IO_ERROR:
9811008
return CONN_IO_ERROR;
@@ -1002,7 +1029,7 @@ int manapi::net::worker::TLS::ssl_bio_flush_read_(const shared_conn &conn, tls_c
10021029

10031030
if (err == this->ssl_error_want_read_ || err == this->ssl_error_want_write_) {
10041031
/* force write all data */
1005-
if (this->ssl_bio_flush_write_(conn, m, WORKER_MAX_CNT)) {
1032+
if (this->ssl_bio_flush_write_(conn, m, WORKER_MAX_CNT, false)) {
10061033
return CONN_IO_ERROR;
10071034
}
10081035

0 commit comments

Comments
 (0)