diff --git a/include/cinatra/brzip.hpp b/include/cinatra/brzip.hpp index 75140d98..c8815689 100644 --- a/include/cinatra/brzip.hpp +++ b/include/cinatra/brzip.hpp @@ -1,82 +1,79 @@ #pragma once -#include -#include -#include -#include #include #include +#include +#include +#include +#include + namespace cinatra::br_codec { #define BROTLI_BUFFER_SIZE 1024 -inline bool brotli_compress(std::string_view input, std::string &output) -{ - auto instance = BrotliEncoderCreateInstance(nullptr, nullptr, nullptr); - std::array buffer; - std::stringstream result; - - size_t available_in = input.size(), available_out = buffer.size(); - const uint8_t* next_in = reinterpret_cast(input.data()); - uint8_t* next_out = buffer.data(); +inline bool brotli_compress(std::string_view input, std::string &output) { + auto instance = BrotliEncoderCreateInstance(nullptr, nullptr, nullptr); + std::array buffer; + std::stringstream result; + + size_t available_in = input.size(), available_out = buffer.size(); + const uint8_t *next_in = reinterpret_cast(input.data()); + uint8_t *next_out = buffer.data(); - do - { - int ret = BrotliEncoderCompressStream - ( - instance, BROTLI_OPERATION_FINISH, - &available_in, &next_in, &available_out, &next_out, nullptr - ); - if (!ret) - return false; - result.write(reinterpret_cast(buffer.data()), buffer.size() - available_out); - available_out = buffer.size(); - next_out = buffer.data(); - } - while (!(available_in == 0 && BrotliEncoderIsFinished(instance))); + do { + int ret = BrotliEncoderCompressStream(instance, BROTLI_OPERATION_FINISH, + &available_in, &next_in, + &available_out, &next_out, nullptr); + if (!ret) + return false; + result.write(reinterpret_cast(buffer.data()), + buffer.size() - available_out); + available_out = buffer.size(); + next_out = buffer.data(); + } while (!(available_in == 0 && BrotliEncoderIsFinished(instance))); - BrotliEncoderDestroyInstance(instance); - output = result.str(); - return true; + BrotliEncoderDestroyInstance(instance); + output = result.str(); + return true; } -inline bool brotli_decompress(std::string_view input, std::string &decompressed) -{ - if (input.size() == 0) - return false; +inline bool brotli_decompress(std::string_view input, + std::string &decompressed) { + if (input.size() == 0) + return false; - size_t available_in = input.size(); - auto next_in = (const uint8_t *)(input.data()); - decompressed = std::string(available_in * 3, 0); - size_t available_out = decompressed.size(); - auto next_out = (uint8_t *)(decompressed.data()); - size_t total_out{0}; - bool done = false; - auto s = BrotliDecoderCreateInstance(nullptr, nullptr, nullptr); - while (!done) { - auto result = BrotliDecoderDecompressStream( - s, &available_in, &next_in, &available_out, &next_out, &total_out); - if (result == BROTLI_DECODER_RESULT_SUCCESS) { - decompressed.resize(total_out); - BrotliDecoderDestroyInstance(s); - return true; - } - else if (result == BROTLI_DECODER_RESULT_NEEDS_MORE_OUTPUT) { - if (total_out != decompressed.size()) { - return false; - } - decompressed.resize(total_out * 2); - next_out = (uint8_t *)(decompressed.data() + total_out); - available_out = total_out; - } - else { - decompressed.resize(0); - BrotliDecoderDestroyInstance(s); - return true; - } + size_t available_in = input.size(); + auto next_in = (const uint8_t *)(input.data()); + decompressed = std::string(available_in * 3, 0); + size_t available_out = decompressed.size(); + auto next_out = (uint8_t *)(decompressed.data()); + size_t total_out{0}; + bool done = false; + auto s = BrotliDecoderCreateInstance(nullptr, nullptr, nullptr); + while (!done) { + auto result = BrotliDecoderDecompressStream( + s, &available_in, &next_in, &available_out, &next_out, &total_out); + if (result == BROTLI_DECODER_RESULT_SUCCESS) { + decompressed.resize(total_out); + BrotliDecoderDestroyInstance(s); + return true; } - return true; -} + else if (result == BROTLI_DECODER_RESULT_NEEDS_MORE_OUTPUT) { + if (total_out != decompressed.size()) { + return false; + } + decompressed.resize(total_out * 2); + next_out = (uint8_t *)(decompressed.data() + total_out); + available_out = total_out; + } + else { + decompressed.resize(0); + BrotliDecoderDestroyInstance(s); + return true; + } + } + return true; } +} // namespace cinatra::br_codec // namespace cinatra::br_codec \ No newline at end of file diff --git a/include/cinatra/coro_http_client.hpp b/include/cinatra/coro_http_client.hpp index 1ce51d80..8f8c321e 100644 --- a/include/cinatra/coro_http_client.hpp +++ b/include/cinatra/coro_http_client.hpp @@ -1447,7 +1447,8 @@ class coro_http_client : public std::enable_shared_from_this { else if (parser_.get_header_value("Content-Encoding").find("deflate") != std::string_view::npos) encoding_type_ = content_encoding::deflate; - else if (parser_.get_header_value("Content-Encoding").find("br") != std::string_view::npos) + else if (parser_.get_header_value("Content-Encoding").find("br") != + std::string_view::npos) encoding_type_ = content_encoding::br; } else { @@ -1567,7 +1568,7 @@ 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; @@ -1588,17 +1589,16 @@ class coro_http_client : public std::enable_shared_from_this { #endif #ifdef CINATRA_ENABLE_BROTLI - if (encoding_type_ == content_encoding::br) - { + if (encoding_type_ == content_encoding::br) { std::string unbr_str; bool r = br_codec::brotli_decompress(reply, unbr_str); if (r) { - data.resp_body = unbr_str; - data.br_data = unbr_str; + data.resp_body = unbr_str; + data.br_data = unbr_str; } else data.resp_body = reply; - + head_buf_.consume(content_len); data.eof = (head_buf_.size() == 0); co_return; diff --git a/include/cinatra/coro_http_response.hpp b/include/cinatra/coro_http_response.hpp index 10ed9f9f..f4b76b64 100644 --- a/include/cinatra/coro_http_response.hpp +++ b/include/cinatra/coro_http_response.hpp @@ -122,32 +122,31 @@ class coro_http_response { #endif #ifdef CINATRA_ENABLE_BROTLI - if (encoding == content_encoding::br) - { - if (client_encoding_type.empty() || - client_encoding_type.find("br") != std::string_view::npos) { - std::string br_str; - bool r = br_codec::brotli_compress(content, br_str); - if (!r) { - set_status_and_content(status_type::internal_server_error, - "br compress error"); - } - else { - add_header("Content-Encoding", "br"); - set_content(std::move(br_str)); - } + if (encoding == content_encoding::br) { + if (client_encoding_type.empty() || + client_encoding_type.find("br") != std::string_view::npos) { + std::string br_str; + bool r = br_codec::brotli_compress(content, br_str); + if (!r) { + set_status_and_content(status_type::internal_server_error, + "br compress error"); + } + else { + add_header("Content-Encoding", "br"); + set_content(std::move(br_str)); + } + } + else { + if (is_view) { + content_view_ = content; } else { - if (is_view) { - content_view_ = content; - } - else { - content_ = std::move(content); - } + content_ = std::move(content); } - has_set_content_ = true; - return; } + has_set_content_ = true; + return; + } #endif if (is_view) {