diff --git a/include/cinatra/brzip.hpp b/include/cinatra/brzip.hpp index 2f0ad94e..75140d98 100644 --- a/include/cinatra/brzip.hpp +++ b/include/cinatra/brzip.hpp @@ -10,7 +10,7 @@ namespace cinatra::br_codec { #define BROTLI_BUFFER_SIZE 1024 -bool brotli_compress(std::string_view input, std::string &output) +inline bool brotli_compress(std::string_view input, std::string &output) { auto instance = BrotliEncoderCreateInstance(nullptr, nullptr, nullptr); std::array buffer; @@ -40,7 +40,7 @@ bool brotli_compress(std::string_view input, std::string &output) return true; } -bool brotli_decompress(std::string_view input, std::string &decompressed) +inline bool brotli_decompress(std::string_view input, std::string &decompressed) { if (input.size() == 0) return false; diff --git a/include/cinatra/coro_http_client.hpp b/include/cinatra/coro_http_client.hpp index ba5bcb60..1ce51d80 100644 --- a/include/cinatra/coro_http_client.hpp +++ b/include/cinatra/coro_http_client.hpp @@ -102,7 +102,7 @@ struct resp_data { int status = 0; bool eof = false; std::string_view resp_body; - std::string_view br_data; + std::string br_data; std::span resp_headers; #ifdef BENCHMARK_TEST uint64_t total = 0; @@ -1567,24 +1567,28 @@ class coro_http_client : public std::enable_shared_from_this { data.resp_body = unziped_str; else data.resp_body = reply; + + head_buf_.consume(content_len); + data.eof = (head_buf_.size() == 0); + co_return; } - else if (encoding_type_ == content_encoding::deflate) { + + if (encoding_type_ == content_encoding::deflate) { std::string inflate_str; bool r = gzip_codec::inflate(reply, inflate_str); if (r) data.resp_body = inflate_str; else data.resp_body = reply; + + head_buf_.consume(content_len); + data.eof = (head_buf_.size() == 0); + co_return; } #endif -#if (defined CINATRA_ENABLE_BROTLI) && (defined CINATRA_ENABLE_GZIP) - else if (encoding_type_ == content_encoding::br) -#endif -#if (defined CINATRA_ENABLE_BROTLI) && !defined(CINATRA_ENABLE_GZIP) - if (encoding_type_ == content_encoding::br) -#endif #ifdef CINATRA_ENABLE_BROTLI + if (encoding_type_ == content_encoding::br) { std::string unbr_str; bool r = br_codec::brotli_decompress(reply, unbr_str); @@ -1594,16 +1598,14 @@ class coro_http_client : public std::enable_shared_from_this { } else data.resp_body = reply; + + head_buf_.consume(content_len); + data.eof = (head_buf_.size() == 0); + co_return; } #endif -#if (defined(CINATRA_ENABLE_BROTLI) || defined(CINATRA_ENABLE_GZIP)) - else if (encoding_type_ == content_encoding::none) { -#endif - data.resp_body = reply; -#if (defined(CINATRA_ENABLE_BROTLI) || defined(CINATRA_ENABLE_GZIP)) - } -#endif + data.resp_body = reply; head_buf_.consume(content_len); } diff --git a/include/cinatra/coro_http_response.hpp b/include/cinatra/coro_http_response.hpp index 7f5812cc..10ed9f9f 100644 --- a/include/cinatra/coro_http_response.hpp +++ b/include/cinatra/coro_http_response.hpp @@ -72,7 +72,7 @@ class coro_http_response { if (client_encoding_type.empty() || client_encoding_type.find("gzip") != std::string_view::npos) { std::string encode_str; - bool r = gzip_codec::compress(content, encode_str, true); + bool r = gzip_codec::compress(content, encode_str); if (!r) { set_status_and_content(status_type::internal_server_error, "gzip compress error"); @@ -90,8 +90,11 @@ class coro_http_response { content_ = std::move(content); } } + has_set_content_ = true; + return; } - else if (encoding == content_encoding::deflate) { + + if (encoding == content_encoding::deflate) { if (client_encoding_type.empty() || client_encoding_type.find("deflate") != std::string_view::npos) { std::string deflate_str; @@ -113,15 +116,13 @@ class coro_http_response { content_ = std::move(content); } } + has_set_content_ = true; + return; } #endif -#if (defined CINATRA_ENABLE_BROTLI) && (defined CINATRA_ENABLE_GZIP) - else if (encoding == content_encoding::br) -#endif -#if (defined CINATRA_ENABLE_BROTLI) && !defined(CINATRA_ENABLE_GZIP) - if (encoding == content_encoding::br) -#endif + #ifdef CINATRA_ENABLE_BROTLI + if (encoding == content_encoding::br) { if (client_encoding_type.empty() || client_encoding_type.find("br") != std::string_view::npos) { @@ -144,18 +145,16 @@ class coro_http_response { content_ = std::move(content); } } + has_set_content_ = true; + return; } #endif -#if (defined CINATRA_ENABLE_BROTLI) || (defined CINATRA_ENABLE_GZIP) - else -#endif - { - if (is_view) { - content_view_ = content; - } - else { - content_ = std::move(content); - } + + if (is_view) { + content_view_ = content; + } + else { + content_ = std::move(content); } has_set_content_ = true; } diff --git a/tests/test_cinatra.cpp b/tests/test_cinatra.cpp index e4dde248..dd1a8142 100644 --- a/tests/test_cinatra.cpp +++ b/tests/test_cinatra.cpp @@ -50,10 +50,7 @@ TEST_CASE("test for gzip") { auto result = async_simple::coro::syncAwait(client.async_get(uri)); auto content = get_header_value(result.resp_headers, "Content-Encoding"); CHECK(get_header_value(result.resp_headers, "Content-Encoding") == "gzip"); - std::string decompress_data; - bool ret = gzip_codec::uncompress(result.resp_body, decompress_data); - CHECK(ret == true); - CHECK(decompress_data == "hello world"); + CHECK(result.resp_body == "hello world"); server.stop(); } @@ -143,8 +140,7 @@ TEST_CASE("test brotli type") { "/get", [](coro_http_request &req, coro_http_response &resp) { auto encoding_type = req.get_encoding_type(); - if (encoding_type == - content_encoding::br) { + if (encoding_type == content_encoding::br) { std::string decode_str; bool r = br_codec::brotli_decompress(req.get_body(), decode_str); CHECK(decode_str == "Hello World"); @@ -167,6 +163,7 @@ TEST_CASE("test brotli type") { auto result = async_simple::coro::syncAwait(client.async_post( "http://127.0.0.1:9001/get", ziped_str, req_content_type::none, headers)); CHECK(result.br_data == "ok"); + server.stop(); } #endif