Skip to content

Commit

Permalink
fxi and some simplify
Browse files Browse the repository at this point in the history
  • Loading branch information
qicosmos committed Apr 19, 2024
1 parent 2a4fdbf commit 3819115
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 39 deletions.
29 changes: 14 additions & 15 deletions include/cinatra/coro_http_client.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -411,13 +411,12 @@ class coro_http_client : public std::enable_shared_from_this<coro_http_client> {
#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<char> msg(dest_buf.data(), dest_buf.size());
auto header = ws.encode_frame(msg, op, true, true);
std::vector<asio::const_buffer> buffers;
buffers.push_back(asio::buffer(header));
buffers.push_back(asio::buffer(dest_buf));
std::vector<asio::const_buffer> buffers{asio::buffer(header),
asio::buffer(dest_buf)};

auto [ec, sz] = co_await async_write(buffers);
if (ec) {
Expand Down Expand Up @@ -454,14 +453,12 @@ class coro_http_client : public std::enable_shared_from_this<coro_http_client> {
#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<char> msg(dest_buf.data(), dest_buf.size());
std::string header = ws.encode_frame(msg, op, result.eof, true);
std::vector<asio::const_buffer> buffers;
buffers.push_back(asio::buffer(header));
buffers.push_back(asio::buffer(dest_buf));

std::vector<asio::const_buffer> buffers{asio::buffer(header),
asio::buffer(dest_buf)};
auto [ec, sz] = co_await async_write(buffers);
if (ec) {
data.net_err = ec;
Expand Down Expand Up @@ -1925,15 +1922,16 @@ class coro_http_client : public std::enable_shared_from_this<coro_http_client> {
#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
Expand Down Expand Up @@ -2128,6 +2126,7 @@ class coro_http_client : public std::enable_shared_from_this<coro_http_client> {
bool enable_ws_deflate_ = false;
#ifdef CINATRA_ENABLE_GZIP
bool is_server_support_ws_deflate_ = false;
std::string inflate_str_;
#endif

#ifdef BENCHMARK_TEST
Expand Down
32 changes: 12 additions & 20 deletions include/cinatra/coro_http_connection.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -573,38 +573,30 @@ class coro_http_connection

async_simple::coro::Lazy<std::error_code> write_websocket(
std::string_view msg, opcode op = opcode::text) {
std::vector<asio::const_buffer> 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<asio::const_buffer> 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<asio::const_buffer> 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<websocket_result> read_websocket() {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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_;
Expand Down
8 changes: 4 additions & 4 deletions include/cinatra/gzip.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand All @@ -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 {
Expand Down Expand Up @@ -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};
Expand All @@ -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 {
Expand Down

0 comments on commit 3819115

Please sign in to comment.