Skip to content

Commit

Permalink
fix write ws frame (#664)
Browse files Browse the repository at this point in the history
  • Loading branch information
qicosmos authored Dec 9, 2024
1 parent 9957d37 commit 2b3f395
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
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

0 comments on commit 2b3f395

Please sign in to comment.