From 3819115f5896ba8454fc5b0e23d63e4ad6711e24 Mon Sep 17 00:00:00 2001 From: qicosmos Date: Fri, 19 Apr 2024 10:40:09 +0800 Subject: [PATCH] fxi and some simplify --- include/cinatra/coro_http_client.hpp | 29 +++++++++++---------- include/cinatra/coro_http_connection.hpp | 32 +++++++++--------------- include/cinatra/gzip.hpp | 8 +++--- 3 files changed, 30 insertions(+), 39 deletions(-) diff --git a/include/cinatra/coro_http_client.hpp b/include/cinatra/coro_http_client.hpp index 72d3cba2..57143ea0 100644 --- a/include/cinatra/coro_http_client.hpp +++ b/include/cinatra/coro_http_client.hpp @@ -411,13 +411,12 @@ class coro_http_client : public std::enable_shared_from_this { #ifdef CINATRA_ENABLE_GZIP if (enable_ws_deflate_ && is_server_support_ws_deflate_) { std::string dest_buf; - if (cinatra::gzip_codec::deflate( - std::string(source.begin(), source.end()), dest_buf)) { + if (cinatra::gzip_codec::deflate({source.data(), source.size()}, + dest_buf)) { std::span msg(dest_buf.data(), dest_buf.size()); auto header = ws.encode_frame(msg, op, true, true); - std::vector buffers; - buffers.push_back(asio::buffer(header)); - buffers.push_back(asio::buffer(dest_buf)); + std::vector buffers{asio::buffer(header), + asio::buffer(dest_buf)}; auto [ec, sz] = co_await async_write(buffers); if (ec) { @@ -454,14 +453,12 @@ class coro_http_client : public std::enable_shared_from_this { #ifdef CINATRA_ENABLE_GZIP if (enable_ws_deflate_ && is_server_support_ws_deflate_) { std::string dest_buf; - if (cinatra::gzip_codec::deflate(std::string(result.buf.data()), - dest_buf)) { + if (cinatra::gzip_codec::deflate( + {result.buf.data(), result.buf.size()}, dest_buf)) { std::span msg(dest_buf.data(), dest_buf.size()); std::string header = ws.encode_frame(msg, op, result.eof, true); - std::vector buffers; - buffers.push_back(asio::buffer(header)); - buffers.push_back(asio::buffer(dest_buf)); - + std::vector buffers{asio::buffer(header), + asio::buffer(dest_buf)}; auto [ec, sz] = co_await async_write(buffers); if (ec) { data.net_err = ec; @@ -1925,15 +1922,16 @@ class coro_http_client : public std::enable_shared_from_this { #ifdef CINATRA_ENABLE_GZIP if (!is_close_frame && is_server_support_ws_deflate_ && enable_ws_deflate_) { - std::string out; - if (!cinatra::gzip_codec::inflate(std::string(data_ptr), out)) { + inflate_str_.clear(); + if (!cinatra::gzip_codec::inflate({data_ptr, payload_len}, + inflate_str_)) { CINATRA_LOG_ERROR << "uncompuress data error"; data.status = 404; data.net_err = std::make_error_code(std::errc::protocol_error); - break; + co_return data; } data.status = 200; - data.resp_body = {out.data(), out.size()}; + data.resp_body = {inflate_str_.data(), inflate_str_.size()}; } else { #endif @@ -2128,6 +2126,7 @@ class coro_http_client : public std::enable_shared_from_this { bool enable_ws_deflate_ = false; #ifdef CINATRA_ENABLE_GZIP bool is_server_support_ws_deflate_ = false; + std::string inflate_str_; #endif #ifdef BENCHMARK_TEST diff --git a/include/cinatra/coro_http_connection.hpp b/include/cinatra/coro_http_connection.hpp index f398f372..026e2854 100644 --- a/include/cinatra/coro_http_connection.hpp +++ b/include/cinatra/coro_http_connection.hpp @@ -573,38 +573,30 @@ class coro_http_connection async_simple::coro::Lazy write_websocket( std::string_view msg, opcode op = opcode::text) { + std::vector buffers; + std::string header; #ifdef CINATRA_ENABLE_GZIP + std::string dest_buf; if (is_client_ws_compressed_ && msg.size() > 0) { - std::string dest_buf; - std::cout << "msg before: " << msg << std::endl; - if (!cinatra::gzip_codec::deflate(std::string(msg), dest_buf)) { + if (!cinatra::gzip_codec::deflate(msg, dest_buf)) { CINATRA_LOG_ERROR << "compuress data error, data: " << msg; co_return std::make_error_code(std::errc::protocol_error); } - std::cout << "dest_buf is: " << dest_buf << std::endl; - - auto header = ws_.format_header(dest_buf.length(), op, true); - std::vector buffers; + header = ws_.format_header(dest_buf.length(), op, true); buffers.push_back(asio::buffer(header)); buffers.push_back(asio::buffer(dest_buf)); - - auto [ec, sz] = co_await async_write(buffers); - co_return ec; } else { #endif - - auto header = ws_.format_header(msg.length(), op); - std::vector buffers; + header = ws_.format_header(msg.length(), op); buffers.push_back(asio::buffer(header)); buffers.push_back(asio::buffer(msg)); - - auto [ec, sz] = co_await async_write(buffers); - co_return ec; #ifdef CINATRA_ENABLE_GZIP } #endif + auto [ec, sz] = co_await async_write(buffers); + co_return ec; } async_simple::coro::Lazy read_websocket() { @@ -661,16 +653,15 @@ class coro_http_connection case cinatra::ws_frame_type::WS_BINARY_FRAME: { #ifdef CINATRA_ENABLE_GZIP if (is_client_ws_compressed_) { - std::cout << "come to inflate logic\n"; - std::string out; + inflate_str_.clear(); if (!cinatra::gzip_codec::inflate( - std::string(payload.begin(), payload.end()), out)) { + {payload.data(), payload.size()}, inflate_str_)) { CINATRA_LOG_ERROR << "uncompuress data error"; result.ec = std::make_error_code(std::errc::protocol_error); break; } result.eof = true; - result.data = {out.data(), out.size()}; + result.data = {inflate_str_.data(), inflate_str_.size()}; break; } else { @@ -901,6 +892,7 @@ class coro_http_connection #ifdef CINATRA_ENABLE_GZIP bool is_client_ws_compressed_ = false; + std::string inflate_str_; #endif websocket ws_; diff --git a/include/cinatra/gzip.hpp b/include/cinatra/gzip.hpp index 29bd7f46..f8a09980 100644 --- a/include/cinatra/gzip.hpp +++ b/include/cinatra/gzip.hpp @@ -141,7 +141,7 @@ inline int uncompress_file(const char *src_file, const char *out_file_name) { return 0; } -inline bool inflate(const std::string &str_src, std::string &str_dest) { +inline bool inflate(std::string_view str_src, std::string &str_dest) { int err = Z_DATA_ERROR; // Create stream z_stream zs = {0}; @@ -157,7 +157,7 @@ inline bool inflate(const std::string &str_src, std::string &str_dest) { } // Use whatever input is provided - zs.next_in = (Bytef *)(str_src.c_str()); + zs.next_in = (Bytef *)(str_src.data()); zs.avail_in = str_src.length(); do { @@ -217,7 +217,7 @@ inline bool inflate(const std::string &str_src, std::string &str_dest) { return err == Z_OK; } -inline bool deflate(const std::string &str_src, std::string &str_dest) { +inline bool deflate(std::string_view str_src, std::string &str_dest) { int err = Z_DATA_ERROR; // Create stream z_stream zs = {0}; @@ -232,7 +232,7 @@ inline bool deflate(const std::string &str_src, std::string &str_dest) { return false; } // Use whatever input is provided - zs.next_in = (Bytef *)(str_src.c_str()); + zs.next_in = (Bytef *)(str_src.data()); zs.avail_in = str_src.length(); do {