summaryrefslogtreecommitdiffhomepage
path: root/src/nxt_brotli.c
diff options
context:
space:
mode:
authorAndrew Clayton <a.clayton@nginx.com>2024-11-20 16:20:57 +0000
committerAndrew Clayton <a.clayton@nginx.com>2025-04-14 18:11:53 +0100
commit804ca81fd958cd29bd52296063224fc73d40c42f (patch)
tree14bf943f6e5a3a07ec5ef1469956bb35233ef159 /src/nxt_brotli.c
parent511b2177a6414e745bf9daa65f77bda932117780 (diff)
downloadunit-804ca81fd958cd29bd52296063224fc73d40c42f.tar.gz
unit-804ca81fd958cd29bd52296063224fc73d40c42f.tar.bz2
http: Add support for brotli compression
Signed-off-by: Andrew Clayton <a.clayton@nginx.com>
Diffstat (limited to 'src/nxt_brotli.c')
-rw-r--r--src/nxt_brotli.c78
1 files changed, 78 insertions, 0 deletions
diff --git a/src/nxt_brotli.c b/src/nxt_brotli.c
new file mode 100644
index 00000000..773b8b6f
--- /dev/null
+++ b/src/nxt_brotli.c
@@ -0,0 +1,78 @@
+/*
+ * Copyright (C) Andrew Clayton
+ * Copyright (C) F5, Inc.
+ */
+
+#include <stddef.h>
+#include <stdint.h>
+#include <stdbool.h>
+
+#include <brotli/encode.h>
+
+#include "nxt_http_compression.h"
+
+
+static int
+nxt_brotli_init(nxt_http_comp_compressor_ctx_t *ctx)
+{
+ BrotliEncoderState **brotli = &ctx->brotli_ctx;
+
+ *brotli = BrotliEncoderCreateInstance(NULL, NULL, NULL);
+ if (*brotli == NULL) {
+ return -1;
+ }
+ BrotliEncoderSetParameter(*brotli, BROTLI_PARAM_QUALITY, ctx->level);
+
+ return 0;
+}
+
+
+static size_t
+nxt_brotli_bound(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 = out_len;
+ BrotliEncoderState *brotli = ctx->brotli_ctx;
+
+ ok = BrotliEncoderCompressStream(brotli, BROTLI_OPERATION_PROCESS,
+ &in_len, &in_buf, &out_bytes, &out_buf,
+ NULL);
+ if (!ok) {
+ return -1;
+ }
+
+ ok = BrotliEncoderCompressStream(brotli, BROTLI_OPERATION_FLUSH,
+ &in_len, &in_buf, &out_bytes, &out_buf,
+ NULL);
+ if (!ok) {
+ return -1;
+ }
+
+ if (last) {
+ ok = BrotliEncoderCompressStream(brotli, BROTLI_OPERATION_FINISH,
+ &in_len, &in_buf, &out_bytes,
+ &out_buf, NULL);
+ if (!ok) {
+ return -1;
+ }
+
+ BrotliEncoderDestroyInstance(brotli);
+ }
+
+ return out_len - out_bytes;
+}
+
+
+const nxt_http_comp_operations_t nxt_http_comp_brotli_ops = {
+ .init = nxt_brotli_init,
+ .bound = nxt_brotli_bound,
+ .deflate = nxt_brotli_compress,
+};