diff options
author | Andrew Clayton <a.clayton@nginx.com> | 2024-06-11 00:24:28 +0100 |
---|---|---|
committer | Andrew Clayton <a.clayton@nginx.com> | 2024-06-19 16:15:04 +0100 |
commit | 4071de5797da0a104b4983fc1ad393d3744b6128 (patch) | |
tree | 003803a861f2609cfae58db412e1c3d7ee6dc8f9 /src/nxt_brotli.c | |
parent | ea5c41b8056997ed40138020272b5159271f1b87 (diff) | |
download | unit-compr.tar.gz unit-compr.tar.bz2 |
[WIP] HTTP Compression Supportcompr
Diffstat (limited to 'src/nxt_brotli.c')
-rw-r--r-- | src/nxt_brotli.c | 86 |
1 files changed, 86 insertions, 0 deletions
diff --git a/src/nxt_brotli.c b/src/nxt_brotli.c new file mode 100644 index 00000000..0c9b3658 --- /dev/null +++ b/src/nxt_brotli.c @@ -0,0 +1,86 @@ +/* + * + */ + +/* XXX Remove */ +#define _GNU_SOURCE +#include <unistd.h> + + +#include <stddef.h> +#include <stdint.h> +#include <stdbool.h> + +#include <brotli/encode.h> + +#include <nxt_http_compression.h> + +static void nxt_brotli_free(const nxt_http_comp_compressor_ctx_t *ctx) +{ + BrotliEncoderState *brotli = ctx->brotli_ctx; + + BrotliEncoderDestroyInstance(brotli); +} + +static void nxt_brotli_init(nxt_http_comp_compressor_ctx_t *ctx) +{ + BrotliEncoderState **brotli = &ctx->brotli_ctx; + + *brotli = BrotliEncoderCreateInstance(NULL, NULL, NULL); + BrotliEncoderSetParameter(*brotli, BROTLI_PARAM_QUALITY, ctx->level); + + printf("%7d %s: brotli compression level [%d]\n", gettid(), __func__, + ctx->level); +} + +static size_t nxt_brotli_compressed_size(const nxt_http_comp_compressor_ctx_t *ctx, + size_t in_len) +{ + return BrotliEncoderMaxCompressedSize(in_len); +} + +static ssize_t nxt_brotli_compress(nxt_http_comp_compressor_ctx_t *ctx, + const uint8_t *in_buf, size_t in_len, + uint8_t *out_buf, size_t out_len, bool last) +{ + bool ok; + size_t out_bytes; + uint8_t *outp; + BrotliEncoderState *brotli = ctx->brotli_ctx; + + printf("%7d %s: last/%s\n", gettid(), __func__, last ? "true" : "false"); + printf("%7d %s: in_len [%lu] out_len [%lu]\n", gettid(), __func__, + in_len, out_len); + + outp = out_buf; + + ok = BrotliEncoderCompressStream(brotli, BROTLI_OPERATION_PROCESS, + &in_len, &in_buf, &out_bytes, &outp, + NULL); + + ok = BrotliEncoderCompressStream(brotli, BROTLI_OPERATION_FLUSH, + &in_len, &in_buf, &out_bytes, &outp, + NULL); + + printf("%7d %s: in_len [%lu] out_len [%lu] out_bytes [%lu]\n", gettid(), + __func__, in_len, out_len, out_bytes); + if (last) { + ok = BrotliEncoderCompressStream(brotli, BROTLI_OPERATION_FINISH, + &in_len, &in_buf, &out_bytes, &outp, + NULL); + nxt_brotli_free(ctx); + } + + printf("%7d %s: in_len [%lu] out_len [%lu] out_bytes [%lu]\n", gettid(), + __func__, in_len, out_len, out_bytes); + printf("%7d %s: buf [%p] outp [%p]\n", gettid(), __func__, out_buf, outp); + + return out_len - out_bytes; +} + +const nxt_http_comp_operations_t nxt_comp_brotli_ops = { + .init = nxt_brotli_init, + .compressed_size = nxt_brotli_compressed_size, + .deflate = nxt_brotli_compress, + .free_ctx = nxt_brotli_free, +}; |