summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorAndrew Clayton <a.clayton@nginx.com>2024-11-20 16:20:57 +0000
committerAndrew Clayton <a.clayton@nginx.com>2024-11-29 00:45:24 +0000
commit0de46a7db5d443bfc0e15cfbe654387377690d2e (patch)
tree52310c95bd52f1f825877934375ac9dc46feff2c
parent87665a5b1f415bad309cffb96c2885971cc0de4b (diff)
downloadunit-0de46a7db5d443bfc0e15cfbe654387377690d2e.tar.gz
unit-0de46a7db5d443bfc0e15cfbe654387377690d2e.tar.bz2
http: Add support for brotli compression
Signed-off-by: Andrew Clayton <a.clayton@nginx.com>
-rw-r--r--src/nxt_brotli.c93
-rw-r--r--src/nxt_http_compression.c12
-rw-r--r--src/nxt_http_compression.h16
3 files changed, 121 insertions, 0 deletions
diff --git a/src/nxt_brotli.c b/src/nxt_brotli.c
new file mode 100644
index 00000000..ad2f4659
--- /dev/null
+++ b/src/nxt_brotli.c
@@ -0,0 +1,93 @@
+/*
+ *
+ */
+
+/* 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_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;
+ 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,
+ .bound = nxt_brotli_bound,
+ .deflate = nxt_brotli_compress,
+ .free_ctx = nxt_brotli_free,
+};
diff --git a/src/nxt_http_compression.c b/src/nxt_http_compression.c
index 58824b36..6f85617a 100644
--- a/src/nxt_http_compression.c
+++ b/src/nxt_http_compression.c
@@ -33,6 +33,9 @@ enum nxt_http_comp_scheme_e {
#if NXT_HAVE_ZSTD
NXT_HTTP_COMP_SCHEME_ZSTD,
#endif
+#if NXT_HAVE_BROTLI
+ NXT_HTTP_COMP_SCHEME_BROTLI,
+#endif
/* keep last */
NXT_HTTP_COMP_SCHEME_UNKNOWN
@@ -117,6 +120,15 @@ static const nxt_http_comp_type_t compressors[] = {
.comp_max = NXT_HTTP_COMP_ZSTD_COMP_MAX,
.cops = &nxt_comp_zstd_ops,
#endif
+#if NXT_HAVE_BROTLI
+ }, {
+ .token = nxt_string("br"),
+ .scheme = NXT_HTTP_COMP_SCHEME_BROTLI,
+ .def_compr = NXT_HTTP_COMP_BROTLI_DEFAULT_LEVEL,
+ .comp_min = NXT_HTTP_COMP_BROTLI_COMP_MIN,
+ .comp_max = NXT_HTTP_COMP_BROTLI_COMP_MAX,
+ .cops = &nxt_comp_brotli_ops,
+#endif
},
};
diff --git a/src/nxt_http_compression.h b/src/nxt_http_compression.h
index 2d51d2a5..c88e4033 100644
--- a/src/nxt_http_compression.h
+++ b/src/nxt_http_compression.h
@@ -20,6 +20,10 @@
#include <zstd.h>
#endif
+#if NXT_HAVE_BROTLI
+#include <brotli/encode.h>
+#endif
+
#include <nxt_main.h>
#include <nxt_router.h>
#include <nxt_string.h>
@@ -36,6 +40,11 @@
#define NXT_HTTP_COMP_ZSTD_COMP_MIN (-7)
#define NXT_HTTP_COMP_ZSTD_COMP_MAX 22
#endif
+#if NXT_HAVE_BROTLI
+#define NXT_HTTP_COMP_BROTLI_DEFAULT_LEVEL BROTLI_DEFAULT_QUALITY
+#define NXT_HTTP_COMP_BROTLI_COMP_MIN BROTLI_MIN_QUALITY
+#define NXT_HTTP_COMP_BROTLI_COMP_MAX BROTLI_MAX_QUALITY
+#endif
typedef struct nxt_http_comp_compressor_ctx_s nxt_http_comp_compressor_ctx_t;
@@ -51,6 +60,9 @@ struct nxt_http_comp_compressor_ctx_s {
#if NXT_HAVE_ZSTD
ZSTD_CStream *zstd_ctx;
#endif
+#if NXT_HAVE_BROTLI
+ BrotliEncoderState *brotli_ctx;
+#endif
};
};
@@ -74,6 +86,10 @@ extern const nxt_http_comp_operations_t nxt_comp_gzip_ops;
extern const nxt_http_comp_operations_t nxt_comp_zstd_ops;
#endif
+#if NXT_HAVE_BROTLI
+extern const nxt_http_comp_operations_t nxt_comp_brotli_ops;
+#endif
+
extern bool nxt_http_comp_wants_compression(void);
extern bool nxt_http_comp_compressor_is_valid(const nxt_str_t *token);