Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix write ws frame #664

Merged
merged 2 commits into from
Dec 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions include/cinatra/coro_http_client.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -400,8 +400,9 @@ class coro_http_client : public std::enable_shared_from_this<coro_http_client> {
async_simple::coro::Lazy<void> write_ws_frame(std::span<char> msg,
websocket ws, opcode op,
resp_data &data,
bool eof = true) {
auto header = ws.encode_frame(msg, op, eof, true);
bool eof = true,
bool need_compression = false) {
auto header = ws.encode_frame(msg, op, eof, need_compression);
std::vector<asio::const_buffer> buffers{
asio::buffer(header), asio::buffer(msg.data(), msg.size())};

Expand Down Expand Up @@ -445,23 +446,27 @@ class coro_http_client : public std::enable_shared_from_this<coro_http_client> {

std::span<char> span{};
if constexpr (is_span_v<Source>) {
bool need_compress = false;
span = {source.data(), source.size()};
#ifdef CINATRA_ENABLE_GZIP
std::string dest_buf;
gzip_compress({source.data(), source.size()}, dest_buf, span, data);
need_compress = true;
#endif
co_await write_ws_frame(span, ws, op, data);
co_await write_ws_frame(span, ws, op, data, true, need_compress);
}
else {
while (true) {
auto result = co_await source();
bool need_compress = false;
span = {result.buf.data(), result.buf.size()};
#ifdef CINATRA_ENABLE_GZIP
std::string dest_buf;
gzip_compress({result.buf.data(), result.buf.size()}, dest_buf, span,
data);
need_compress = true;
#endif
co_await write_ws_frame(span, ws, op, data, result.eof);
co_await write_ws_frame(span, ws, op, data, result.eof, need_compress);

if (result.eof || data.status == 404) {
break;
Expand Down
2 changes: 1 addition & 1 deletion include/cinatra/coro_http_connection.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -597,7 +597,7 @@ class coro_http_connection
std::string dest_buf;
if (is_client_ws_compressed_ && msg.size() > 0) {
if (!cinatra::gzip_codec::deflate(msg, dest_buf)) {
CINATRA_LOG_ERROR << "compuress data error, data: " << msg;
CINATRA_LOG_ERROR << "compress data error, data: " << msg;
co_return std::make_error_code(std::errc::protocol_error);
}

Expand Down
Loading