Skip to content

Commit

Permalink
Fix: Decrypt TLS requests fully
Browse files Browse the repository at this point in the history
Add: Handle `Connection: close` header
  • Loading branch information
ishkhan42 committed Sep 7, 2023
1 parent e62487d commit 80cd292
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 7 deletions.
8 changes: 4 additions & 4 deletions src/headers/automata.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -165,10 +165,10 @@ void automata_t::operator()() noexcept {
connection.pipes.mark_submitted_outputs(completed_result);
if (!connection.pipes.has_remaining_outputs()) {
connection.exchanges++;
// if (connection.exchanges >= server.max_lifetime_exchanges) TODO Why?
// return close_gracefully();
// else
return receive_next();
if (connection.must_close())
return close_gracefully();
else
return receive_next();
} else {
connection.pipes.prepare_more_outputs();
return send_next();
Expand Down
18 changes: 15 additions & 3 deletions src/headers/connection.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,15 @@ struct connection_t {
bool expired() const noexcept {
return std::chrono::high_resolution_clock::now().time_since_epoch().count() - last_active_ns >
max_inactive_duration_ns_k;
};
}

bool is_ready() const noexcept { return tls_ctx == nullptr || ptls_handshake_is_complete(tls_ctx); }

bool must_close() const noexcept {
auto conn = protocol.get_header("Connection");
return conn == "Close" || conn == "close";
}

bool prepare_step() noexcept {
if (is_ready())
return true;
Expand Down Expand Up @@ -114,10 +119,17 @@ struct connection_t {
return;

work_buf.off = 0;
int res = 0;
size_t in_len = pipes.input_span().size();
int res = ptls_receive(tls_ctx, &work_buf, pipes.input_span().data(), &in_len);
void const* input = pipes.input_span().data();
while (in_len != 0 && res != -1) {
size_t consumed = in_len;
res = ptls_receive(tls_ctx, &work_buf, input, &consumed);
in_len -= consumed;
input += consumed;
}
if (res != -1) {
pipes.release_inputs();
pipes.release_current_input();
std::memcpy(pipes.next_input_address(), work_buf.base, work_buf.off);
pipes.absorb_input(work_buf.off);
}
Expand Down
3 changes: 3 additions & 0 deletions src/headers/containers.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,9 @@ class exchange_pipes_t {
input_.dynamic.reset();
input_.embedded_used = 0;
}

void release_current_input() noexcept { input_.embedded_used = 0; }

void release_outputs() noexcept {
output_.dynamic.reset();
output_.embedded_used = 0;
Expand Down

0 comments on commit 80cd292

Please sign in to comment.