Skip to content

Commit

Permalink
feat: fix bug
Browse files Browse the repository at this point in the history
  • Loading branch information
helintongh committed Jun 14, 2024
1 parent 6902527 commit c547bf2
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 41 deletions.
4 changes: 2 additions & 2 deletions include/cinatra/brzip.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<uint8_t, BROTLI_BUFFER_SIZE> buffer;
Expand Down Expand Up @@ -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;
Expand Down
32 changes: 17 additions & 15 deletions include/cinatra/coro_http_client.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<http_header> resp_headers;
#ifdef BENCHMARK_TEST
uint64_t total = 0;
Expand Down Expand Up @@ -1567,24 +1567,28 @@ class coro_http_client : public std::enable_shared_from_this<coro_http_client> {
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);
Expand All @@ -1594,16 +1598,14 @@ class coro_http_client : public std::enable_shared_from_this<coro_http_client> {
}
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);
}
Expand Down
35 changes: 17 additions & 18 deletions include/cinatra/coro_http_response.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -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;
Expand All @@ -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) {
Expand All @@ -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;
}
Expand Down
9 changes: 3 additions & 6 deletions tests/test_cinatra.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down Expand Up @@ -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");
Expand All @@ -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

Expand Down

0 comments on commit c547bf2

Please sign in to comment.