|
| 1 | +/* |
| 2 | + * |
| 3 | + */ |
| 4 | + |
| 5 | +/* XXX Remove */ |
| 6 | +#define _GNU_SOURCE |
| 7 | +#include <unistd.h> |
| 8 | + |
| 9 | + |
| 10 | +#include <stddef.h> |
| 11 | +#include <stdint.h> |
| 12 | +#include <stdbool.h> |
| 13 | + |
| 14 | +#include <brotli/encode.h> |
| 15 | + |
| 16 | +#include <nxt_http_compression.h> |
| 17 | + |
| 18 | +static void nxt_brotli_free(const nxt_http_comp_compressor_ctx_t *ctx) |
| 19 | +{ |
| 20 | + BrotliEncoderState *brotli = ctx->brotli_ctx; |
| 21 | + |
| 22 | + BrotliEncoderDestroyInstance(brotli); |
| 23 | +} |
| 24 | + |
| 25 | +static void nxt_brotli_init(nxt_http_comp_compressor_ctx_t *ctx) |
| 26 | +{ |
| 27 | + BrotliEncoderState **brotli = &ctx->brotli_ctx; |
| 28 | + |
| 29 | + *brotli = BrotliEncoderCreateInstance(NULL, NULL, NULL); |
| 30 | + BrotliEncoderSetParameter(*brotli, BROTLI_PARAM_QUALITY, ctx->level); |
| 31 | + |
| 32 | + printf("%7d %s: brotli compression level [%d]\n", gettid(), __func__, |
| 33 | + ctx->level); |
| 34 | +} |
| 35 | + |
| 36 | +static size_t nxt_brotli_bound(const nxt_http_comp_compressor_ctx_t *ctx, |
| 37 | + size_t in_len) |
| 38 | +{ |
| 39 | + return BrotliEncoderMaxCompressedSize(in_len); |
| 40 | +} |
| 41 | + |
| 42 | +static ssize_t nxt_brotli_compress(nxt_http_comp_compressor_ctx_t *ctx, |
| 43 | + const uint8_t *in_buf, size_t in_len, |
| 44 | + uint8_t *out_buf, size_t out_len, bool last) |
| 45 | +{ |
| 46 | + bool ok; |
| 47 | + size_t out_bytes; |
| 48 | + uint8_t *outp; |
| 49 | + BrotliEncoderState *brotli = ctx->brotli_ctx; |
| 50 | + |
| 51 | + printf("%7d %s: last/%s\n", gettid(), __func__, last ? "true" : "false"); |
| 52 | + printf("%7d %s: in_len [%lu] out_len [%lu]\n", gettid(), __func__, |
| 53 | + in_len, out_len); |
| 54 | + |
| 55 | + outp = out_buf; |
| 56 | + |
| 57 | + ok = BrotliEncoderCompressStream(brotli, BROTLI_OPERATION_PROCESS, |
| 58 | + &in_len, &in_buf, &out_bytes, &outp, |
| 59 | + NULL); |
| 60 | + |
| 61 | + ok = BrotliEncoderCompressStream(brotli, BROTLI_OPERATION_FLUSH, |
| 62 | + &in_len, &in_buf, &out_bytes, &outp, |
| 63 | + NULL); |
| 64 | + |
| 65 | + printf("%7d %s: in_len [%lu] out_len [%lu] out_bytes [%lu]\n", gettid(), |
| 66 | + __func__, in_len, out_len, out_bytes); |
| 67 | + if (last) { |
| 68 | + ok = BrotliEncoderCompressStream(brotli, BROTLI_OPERATION_FINISH, |
| 69 | + &in_len, &in_buf, &out_bytes, &outp, |
| 70 | + NULL); |
| 71 | + nxt_brotli_free(ctx); |
| 72 | + } |
| 73 | + |
| 74 | + printf("%7d %s: in_len [%lu] out_len [%lu] out_bytes [%lu]\n", gettid(), |
| 75 | + __func__, in_len, out_len, out_bytes); |
| 76 | + printf("%7d %s: buf [%p] outp [%p]\n", gettid(), __func__, out_buf, outp); |
| 77 | + |
| 78 | + return out_len - out_bytes; |
| 79 | +} |
| 80 | + |
| 81 | +const nxt_http_comp_operations_t nxt_comp_brotli_ops = { |
| 82 | + .init = nxt_brotli_init, |
| 83 | + .bound = nxt_brotli_bound, |
| 84 | + .deflate = nxt_brotli_compress, |
| 85 | + .free_ctx = nxt_brotli_free, |
| 86 | +}; |
0 commit comments