Skip to content

Commit

Permalink
enable_ws_deflate_ (#665)
Browse files Browse the repository at this point in the history
  • Loading branch information
qicosmos authored Dec 9, 2024
1 parent 2b3f395 commit c74feaf
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 13 deletions.
25 changes: 12 additions & 13 deletions include/cinatra/coro_http_client.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -400,9 +400,8 @@ 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,
bool need_compression = false) {
auto header = ws.encode_frame(msg, op, eof, need_compression);
bool eof = true) {
auto header = ws.encode_frame(msg, op, eof, enable_ws_deflate_);
std::vector<asio::const_buffer> buffers{
asio::buffer(header), asio::buffer(msg.data(), msg.size())};

Expand Down Expand Up @@ -446,27 +445,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;
if (enable_ws_deflate_) {
gzip_compress({source.data(), source.size()}, dest_buf, span, data);
}
#endif
co_await write_ws_frame(span, ws, op, data, true, need_compress);
co_await write_ws_frame(span, ws, op, data, true);
}
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;
if (enable_ws_deflate_) {
gzip_compress({result.buf.data(), result.buf.size()}, dest_buf, span,
data);
}
#endif
co_await write_ws_frame(span, ws, op, data, result.eof, need_compress);
co_await write_ws_frame(span, ws, op, data, result.eof);

if (result.eof || data.status == 404) {
break;
Expand Down Expand Up @@ -2402,8 +2401,8 @@ class coro_http_client : public std::enable_shared_from_this<coro_http_client> {
bool should_reset_ = false;
config config_;

#ifdef CINATRA_ENABLE_GZIP
bool enable_ws_deflate_ = false;
#ifdef CINATRA_ENABLE_GZIP
bool is_server_support_ws_deflate_ = false;
std::string inflate_str_;
#endif
Expand Down
4 changes: 4 additions & 0 deletions tests/test_cinatra.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1055,6 +1055,8 @@ async_simple::coro::Lazy<void> test_collect_all() {
std::vector<async_simple::coro::Lazy<resp_data>> futures;
for (int i = 0; i < 2; ++i) {
auto client = std::make_shared<coro_http_client>();
client->set_conn_timeout(3s);
client->set_req_timeout(5s);
v.push_back(client);
futures.push_back(client->async_get("http://www.baidu.com/"));
}
Expand Down Expand Up @@ -1167,6 +1169,8 @@ TEST_CASE("test request with out buffer") {
{
detail::resize(str, 1024 * 64);
coro_http_client client;
client.set_conn_timeout(3s);
client.set_req_timeout(5s);
std::string dest = "http://www.baidu.com";
auto ret = client.async_request(dest, http_method::GET, req_context<>{}, {},
std::span<char>{str.data(), str.size()});
Expand Down

0 comments on commit c74feaf

Please sign in to comment.