diff options
| author | Andrew Clayton <a.clayton@nginx.com> | 2024-11-20 16:19:29 +0000 |
|---|---|---|
| committer | Andrew Clayton <a.clayton@nginx.com> | 2024-11-29 00:45:24 +0000 |
| commit | 87665a5b1f415bad309cffb96c2885971cc0de4b (patch) | |
| tree | 003a744e254828882b309c7d3a700f8c66e32929 | |
| parent | 9e836e683f1125955e6d69afd90e8fac328f1b4b (diff) | |
| download | unit-87665a5b1f415bad309cffb96c2885971cc0de4b.tar.gz unit-87665a5b1f415bad309cffb96c2885971cc0de4b.tar.bz2 | |
http: Add support for zstd compression
Signed-off-by: Andrew Clayton <a.clayton@nginx.com>
| -rw-r--r-- | src/nxt_http_compression.c | 12 | ||||
| -rw-r--r-- | src/nxt_http_compression.h | 16 | ||||
| -rw-r--r-- | src/nxt_zstd.c | 81 |
3 files changed, 109 insertions, 0 deletions
diff --git a/src/nxt_http_compression.c b/src/nxt_http_compression.c index 0c2931a9..58824b36 100644 --- a/src/nxt_http_compression.c +++ b/src/nxt_http_compression.c @@ -30,6 +30,9 @@ enum nxt_http_comp_scheme_e { NXT_HTTP_COMP_SCHEME_DEFLATE, NXT_HTTP_COMP_SCHEME_GZIP, #endif +#if NXT_HAVE_ZSTD + NXT_HTTP_COMP_SCHEME_ZSTD, +#endif /* keep last */ NXT_HTTP_COMP_SCHEME_UNKNOWN @@ -105,6 +108,15 @@ static const nxt_http_comp_type_t compressors[] = { .comp_max = NXT_HTTP_COMP_ZLIB_COMP_MAX, .cops = &nxt_comp_gzip_ops, #endif +#if NXT_HAVE_ZSTD + }, { + .token = nxt_string("zstd"), + .scheme = NXT_HTTP_COMP_SCHEME_ZSTD, + .def_compr = NXT_HTTP_COMP_ZSTD_DEFAULT_LEVEL, + .comp_min = NXT_HTTP_COMP_ZSTD_COMP_MIN, + .comp_max = NXT_HTTP_COMP_ZSTD_COMP_MAX, + .cops = &nxt_comp_zstd_ops, +#endif }, }; diff --git a/src/nxt_http_compression.h b/src/nxt_http_compression.h index c67cd001..2d51d2a5 100644 --- a/src/nxt_http_compression.h +++ b/src/nxt_http_compression.h @@ -16,6 +16,10 @@ #include <zlib.h> #endif +#if NXT_HAVE_ZSTD +#include <zstd.h> +#endif + #include <nxt_main.h> #include <nxt_router.h> #include <nxt_string.h> @@ -27,6 +31,11 @@ #define NXT_HTTP_COMP_ZLIB_COMP_MIN Z_DEFAULT_COMPRESSION #define NXT_HTTP_COMP_ZLIB_COMP_MAX Z_BEST_COMPRESSION #endif +#if NXT_HAVE_ZSTD +#define NXT_HTTP_COMP_ZSTD_DEFAULT_LEVEL ZSTD_CLEVEL_DEFAULT +#define NXT_HTTP_COMP_ZSTD_COMP_MIN (-7) +#define NXT_HTTP_COMP_ZSTD_COMP_MAX 22 +#endif typedef struct nxt_http_comp_compressor_ctx_s nxt_http_comp_compressor_ctx_t; @@ -39,6 +48,9 @@ struct nxt_http_comp_compressor_ctx_s { #if NXT_HAVE_ZLIB z_stream zlib_ctx; #endif +#if NXT_HAVE_ZSTD + ZSTD_CStream *zstd_ctx; +#endif }; }; @@ -58,6 +70,10 @@ extern const nxt_http_comp_operations_t nxt_comp_deflate_ops; extern const nxt_http_comp_operations_t nxt_comp_gzip_ops; #endif +#if NXT_HAVE_ZSTD +extern const nxt_http_comp_operations_t nxt_comp_zstd_ops; +#endif + extern bool nxt_http_comp_wants_compression(void); extern bool nxt_http_comp_compressor_is_valid(const nxt_str_t *token); diff --git a/src/nxt_zstd.c b/src/nxt_zstd.c new file mode 100644 index 00000000..3877ca8c --- /dev/null +++ b/src/nxt_zstd.c @@ -0,0 +1,81 @@ +/* + * + */ + +#include <stddef.h> +#include <stdint.h> +#include <stdbool.h> + +#include <zstd.h> + +#include "nxt_http_compression.h" + + +static void +nxt_zstd_free(const nxt_http_comp_compressor_ctx_t *ctx) +{ + ZSTD_CStream *zstd = ctx->zstd_ctx; + + ZSTD_freeCStream(zstd); +} + + +static void +nxt_zstd_init(nxt_http_comp_compressor_ctx_t *ctx) +{ + ZSTD_CStream **zstd = &ctx->zstd_ctx; + + *zstd = ZSTD_createCStream(); + ZSTD_initCStream(*zstd, ctx->level); + + printf("%s: zstd compression level [%d]\n", __func__, ctx->level); +} + + +static size_t +nxt_zstd_bound(const nxt_http_comp_compressor_ctx_t *ctx, size_t in_len) +{ + return ZSTD_compressBound(in_len); +} + + +static ssize_t +nxt_zstd_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) +{ + size_t ret; + ZSTD_CStream *zstd = ctx->zstd_ctx; + ZSTD_inBuffer zinb = { .src = in_buf, .size = in_len }; + ZSTD_outBuffer zoutb = { .dst = out_buf, .size = out_len }; + + printf("%s: in_len [%lu] out_len [%lu] last [%s]\n", __func__, + in_len, out_len, last ? "true" : "false"); + + ret = ZSTD_compressStream(zstd, &zoutb, &zinb); + + if (zinb.pos < zinb.size) { + printf("%s: short by [%d]\n", __func__, zinb.pos < zinb.size); + ret = ZSTD_flushStream(zstd, &zoutb); + } + + if (last) { + ret = ZSTD_endStream(zstd, &zoutb); + nxt_zstd_free(ctx); + } + + printf("%s: ret [%lu]\n", __func__, ret); + if (ZSTD_isError(ret)) { + printf("%s: [%s]\n", __func__, ZSTD_getErrorName(ret)); + return -1; + } + + return zoutb.pos; +} + + +const nxt_http_comp_operations_t nxt_comp_zstd_ops = { + .init = nxt_zstd_init, + .bound = nxt_zstd_bound, + .deflate = nxt_zstd_compress, + .free_ctx = nxt_zstd_free, +}; |
