Skip to content

Commit

Permalink
fix: clang-format fix
Browse files Browse the repository at this point in the history
  • Loading branch information
helintongh committed Jun 14, 2024
1 parent adb2d2a commit 2aaaeb2
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 94 deletions.
127 changes: 62 additions & 65 deletions include/cinatra/brzip.hpp
Original file line number Diff line number Diff line change
@@ -1,82 +1,79 @@
#pragma once
#include <string>
#include <string_view>
#include <array>
#include <sstream>
#include <brotli/decode.h>
#include <brotli/encode.h>

#include <array>
#include <sstream>
#include <string>
#include <string_view>

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<uint8_t, BROTLI_BUFFER_SIZE> buffer;
std::stringstream result;

size_t available_in = input.size(), available_out = buffer.size();
const uint8_t* next_in = reinterpret_cast<const uint8_t*>(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<uint8_t, BROTLI_BUFFER_SIZE> buffer;
std::stringstream result;

size_t available_in = input.size(), available_out = buffer.size();
const uint8_t *next_in = reinterpret_cast<const uint8_t *>(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<const char*>(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<const char *>(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
14 changes: 7 additions & 7 deletions include/cinatra/coro_http_client.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1447,7 +1447,8 @@ class coro_http_client : public std::enable_shared_from_this<coro_http_client> {
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 {
Expand Down Expand Up @@ -1567,7 +1568,7 @@ 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;
Expand All @@ -1588,17 +1589,16 @@ class coro_http_client : public std::enable_shared_from_this<coro_http_client> {
#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;
Expand Down
43 changes: 21 additions & 22 deletions include/cinatra/coro_http_response.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 2aaaeb2

Please sign in to comment.