From 3da4a0ac69574c4998af05009d237454c481e071 Mon Sep 17 00:00:00 2001 From: Ivan Fefer Date: Tue, 8 Sep 2020 19:18:14 +0300 Subject: [PATCH] Add compression buffer size customization (#644) * add compression buffer size customization and small brotli refactor * allocat brotli buffer once * add init to brotli decoder buffer --- httplib.h | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/httplib.h b/httplib.h index de2ae93..edd03d5 100644 --- a/httplib.h +++ b/httplib.h @@ -80,6 +80,10 @@ #define CPPHTTPLIB_RECV_BUFSIZ size_t(4096u) #endif +#ifndef CPPHTTPLIB_COMPRESSION_BUFSIZ +#define CPPHTTPLIB_COMPRESSION_BUFSIZ size_t(16384u) +#endif + #ifndef CPPHTTPLIB_THREAD_POOL_COUNT #define CPPHTTPLIB_THREAD_POOL_COUNT \ ((std::max)(8u, std::thread::hardware_concurrency() > 0 \ @@ -2226,7 +2230,7 @@ public: int ret = Z_OK; - std::array buff{}; + std::array buff{}; do { strm_.avail_out = buff.size(); strm_.next_out = reinterpret_cast(buff.data()); @@ -2277,7 +2281,7 @@ public: strm_.avail_in = static_cast(data_length); strm_.next_in = const_cast(reinterpret_cast(data)); - std::array buff{}; + std::array buff{}; while (strm_.avail_in > 0) { strm_.avail_out = buff.size(); strm_.next_out = reinterpret_cast(buff.data()); @@ -2315,7 +2319,7 @@ public: bool compress(const char *data, size_t data_length, bool last, Callback callback) override { - std::array buff{}; + std::array buff{}; auto operation = last ? BROTLI_OPERATION_FINISH : BROTLI_OPERATION_PROCESS; auto available_in = data_length; @@ -2377,18 +2381,18 @@ public: decoder_r = BROTLI_DECODER_RESULT_NEEDS_MORE_OUTPUT; + std::array buff{}; while (decoder_r == BROTLI_DECODER_RESULT_NEEDS_MORE_OUTPUT) { - char output[1024]; - char *next_out = output; - size_t avail_out = sizeof(output); + char *next_out = buff.data(); + size_t avail_out = buff.size(); decoder_r = BrotliDecoderDecompressStream( decoder_s, &avail_in, &next_in, &avail_out, - reinterpret_cast(&next_out), &total_out); + reinterpret_cast(&next_out), &total_out); if (decoder_r == BROTLI_DECODER_RESULT_ERROR) { return false; } - if (!callback((const char *)output, sizeof(output) - avail_out)) { + if (!callback(buff.data(), buff.size() - avail_out)) { return false; } }