traeak commented on code in PR #13843:
URL: https://github.com/apache/trafficserver/pull/13843#discussion_r4288065443
##########
plugins/stats_over_http/stats_over_http.cc:
##########
@@ -901,94 +787,54 @@ json_out_stats(stats_state *my_state)
#if HAVE_BROTLI_ENCODE_H
static bool
-br_encode(stats_state *my_state, const char *data, int64_t len, bool finish,
TSIOBuffer out)
+br_compress(const std::string &in, std::string &out)
{
- BrotliEncoderOperation const op = finish ? BROTLI_OPERATION_FINISH :
BROTLI_OPERATION_PROCESS;
- const uint8_t *next_in = reinterpret_cast<const uint8_t
*>(data);
- size_t avail_in = static_cast<size_t>(len);
-
- do {
- int64_t avail = 0;
- uint8_t *next_out = reinterpret_cast<uint8_t
*>(TSIOBufferBlockWriteStart(TSIOBufferStart(out), &avail));
- size_t avail_out = static_cast<size_t>(avail);
-
- if (!BrotliEncoderCompressStream(my_state->bstrm.br, op, &avail_in,
&next_in, &avail_out, &next_out, nullptr)) {
- return false;
- }
- TSIOBufferProduce(out, avail - avail_out);
- } while (avail_in > 0 || BrotliEncoderHasMoreOutput(my_state->bstrm.br) ||
- (finish && !BrotliEncoderIsFinished(my_state->bstrm.br)));
+ size_t out_size = BrotliEncoderMaxCompressedSize(in.size());
+ if (out_size == 0) {
+ Dbg(dbg_ctl, "brotli input too large: %zu", in.size());
+ return false;
+ }
+ out.resize(out_size);
+ if (BrotliEncoderCompress(BROTLI_DEFAULT_QUALITY, BROTLI_DEFAULT_WINDOW,
BROTLI_DEFAULT_MODE, in.size(),
+ reinterpret_cast<const uint8_t *>(in.data()),
&out_size,
+ reinterpret_cast<uint8_t *>(out.data())) ==
BROTLI_FALSE) {
+ Dbg(dbg_ctl, "brotli compress error");
+ return false;
+ }
+ out.resize(out_size);
return true;
}
#endif
static bool
-gzip_encode(stats_state *my_state, const char *data, int64_t len, bool finish,
TSIOBuffer out)
+gzip_compress(const std::string &in, std::string &out, int mode)
{
- z_stream &zstrm = my_state->zstrm;
- int const flush = finish ? Z_FINISH : Z_NO_FLUSH;
- int err = Z_OK;
-
- zstrm.next_in = reinterpret_cast<Bytef *>(const_cast<char *>(data));
- zstrm.avail_in = static_cast<uInt>(len);
- do {
- int64_t avail = 0;
- zstrm.next_out = reinterpret_cast<Bytef
*>(TSIOBufferBlockWriteStart(TSIOBufferStart(out), &avail));
- zstrm.avail_out = static_cast<uInt>(avail);
- err = deflate(&zstrm, flush);
- TSIOBufferProduce(out, avail - zstrm.avail_out);
- } while (err == Z_OK && (finish || zstrm.avail_out == 0));
-
- return finish ? err == Z_STREAM_END : (err == Z_OK || err == Z_BUF_ERROR);
-}
+ z_stream zstrm{};
-static bool
-encode_chunk(stats_state *my_state, const char *data, int64_t len, bool
finish, TSIOBuffer out)
-{
-#if HAVE_BROTLI_ENCODE_H
- if (my_state->encoding == encoding_format_t::BR) {
- return br_encode(my_state, data, len, finish, out);
+ zstrm.data_type = Z_ASCII;
+ if (int err = deflateInit2(&zstrm, ZLIB_COMPRESSION_LEVEL, Z_DEFLATED, mode,
ZLIB_MEMLEVEL, Z_DEFAULT_STRATEGY); err != Z_OK) {
+ Dbg(dbg_ctl, "gzip initialization failed: %d", err);
+ return false;
}
-#endif
- return gzip_encode(my_state, data, len, finish, out);
-}
-// Replaces the uncompressed body in the response buffer with its compressed
form. The write VIO
-// has already sent the response header, so the body is all that remains in
resp_reader.
-static void
-compress_out_stats(stats_state *my_state)
-{
- TSIOBuffer out = TSIOBufferCreate();
- TSIOBufferReader out_reader = TSIOBufferReaderAlloc(out);
- int64_t const body_bytes = TSIOBufferReaderAvail(my_state->resp_reader);
- bool ok = true;
+ out.resize(deflateBound(&zstrm, in.size()));
+ zstrm.next_in = reinterpret_cast<Bytef *>(const_cast<char *>(in.data()));
+ zstrm.avail_in = in.size();
+ zstrm.next_out = reinterpret_cast<Bytef *>(out.data());
+ zstrm.avail_out = out.size();
Review Comment:
Technically valid, though no real stats dump is anywhere near 4 GB. Will
address anyways (send back uncompressed).
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]