Skip to content

Commit

Permalink
perf: Optimize payload length check before reading data (#629)
Browse files Browse the repository at this point in the history
  • Loading branch information
xukeawsl authored Sep 22, 2024
1 parent beb2335 commit 4563d1b
Showing 1 changed file with 12 additions and 9 deletions.
21 changes: 12 additions & 9 deletions include/cinatra/coro_http_connection.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -681,6 +681,18 @@ class coro_http_connection
head_buf_.consume(head_buf_.size());
std::span<char> payload{};
auto payload_length = ws_.payload_length();

if (max_part_size_ != 0 && payload_length > max_part_size_) {
std::string close_reason = "message_too_big";
std::string close_msg = ws_.format_close_payload(
close_code::too_big, close_reason.data(), close_reason.size());
co_await write_websocket(close_msg, opcode::close);
close();
result.ec = std::error_code(asio::error::message_size,
asio::error::get_system_category());
break;
}

if (payload_length > 0) {
detail::resize(body_, payload_length);
auto [ec, read_sz] =
Expand All @@ -693,15 +705,6 @@ class coro_http_connection
payload = body_;
}

if (max_part_size_ != 0 && payload_length > max_part_size_) {
std::string close_reason = "message_too_big";
std::string close_msg = ws_.format_close_payload(
close_code::too_big, close_reason.data(), close_reason.size());
co_await write_websocket(close_msg, opcode::close);
close();
break;
}

ws_frame_type type = ws_.parse_payload(payload);

switch (type) {
Expand Down

0 comments on commit 4563d1b

Please sign in to comment.