diff options
| author | arcadia-devtools <[email protected]> | 2022-05-05 17:03:26 +0300 |
|---|---|---|
| committer | arcadia-devtools <[email protected]> | 2022-05-05 17:03:26 +0300 |
| commit | b94ffc4f9fb0de2cccb10069715302687b494c34 (patch) | |
| tree | 0a992a17f01285d3976b78bd57be64a3691e9a59 /library/cpp/actors/http/http_compress.cpp | |
| parent | f8831bfbf91cd9e5f400dbc59372651bf756d87a (diff) | |
intermediate changes
ref:670915a6a4cf5edb3a63ce999d592acd20f1f248
Diffstat (limited to 'library/cpp/actors/http/http_compress.cpp')
| -rw-r--r-- | library/cpp/actors/http/http_compress.cpp | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/library/cpp/actors/http/http_compress.cpp b/library/cpp/actors/http/http_compress.cpp new file mode 100644 index 00000000000..33ff3e16746 --- /dev/null +++ b/library/cpp/actors/http/http_compress.cpp @@ -0,0 +1,42 @@ +#include "http.h" + +#include <contrib/libs/zlib/zlib.h> + +namespace NHttp { + +TString THttpOutgoingResponse::CompressDeflate(TStringBuf source) { + int compressionlevel = Z_BEST_COMPRESSION; + z_stream zs = {}; + + if (deflateInit(&zs, compressionlevel) != Z_OK) { + throw yexception() << "deflateInit failed while compressing"; + } + + zs.next_in = (Bytef*)source.data(); + zs.avail_in = source.size(); + + int ret; + char outbuffer[32768]; + TString result; + + // retrieve the compressed bytes blockwise + do { + zs.next_out = reinterpret_cast<Bytef*>(outbuffer); + zs.avail_out = sizeof(outbuffer); + + ret = deflate(&zs, Z_FINISH); + + if (result.size() < zs.total_out) { + result.append(outbuffer, zs.total_out - result.size()); + } + } while (ret == Z_OK); + + deflateEnd(&zs); + + if (ret != Z_STREAM_END) { + throw yexception() << "Exception during zlib compression: (" << ret << ") " << zs.msg; + } + return result; +} + +} |
