diff options
| author | Andrew Clayton <a.clayton@nginx.com> | 2024-11-20 16:20:57 +0000 |
|---|---|---|
| committer | Andrew Clayton <a.clayton@nginx.com> | 2025-04-14 18:11:53 +0100 |
| commit | 804ca81fd958cd29bd52296063224fc73d40c42f (patch) | |
| tree | 14bf943f6e5a3a07ec5ef1469956bb35233ef159 /src | |
| parent | 511b2177a6414e745bf9daa65f77bda932117780 (diff) | |
| download | unit-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')
| -rw-r--r-- | src/nxt_brotli.c | 78 | ||||
| -rw-r--r-- | src/nxt_http_compression.c | 12 | ||||
| -rw-r--r-- | src/nxt_http_compression.h | 16 |
3 files changed, 106 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, +}; diff --git a/src/nxt_http_compression.c b/src/nxt_http_compression.c index dfd4a13a..e6083ec3 100644 --- a/src/nxt_http_compression.c +++ b/src/nxt_http_compression.c @@ -34,6 +34,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 @@ -121,6 +124,15 @@ static const nxt_http_comp_type_t nxt_http_comp_compressors[] = { .comp_max = NXT_HTTP_COMP_ZSTD_COMP_MAX, .cops = &nxt_http_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_http_comp_brotli_ops, +#endif }, }; diff --git a/src/nxt_http_compression.h b/src/nxt_http_compression.h index 7c731a86..c2007f8e 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 }; }; @@ -73,6 +85,10 @@ extern const nxt_http_comp_operations_t nxt_http_comp_gzip_ops; extern const nxt_http_comp_operations_t nxt_http_comp_zstd_ops; #endif +#if NXT_HAVE_BROTLI +extern const nxt_http_comp_operations_t nxt_http_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); |
