From 4071de5797da0a104b4983fc1ad393d3744b6128 Mon Sep 17 00:00:00 2001 From: Andrew Clayton Date: Tue, 11 Jun 2024 00:24:28 +0100 Subject: [WIP] HTTP Compression Support --- src/nxt_http_compression.h | 91 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 91 insertions(+) create mode 100644 src/nxt_http_compression.h (limited to 'src/nxt_http_compression.h') diff --git a/src/nxt_http_compression.h b/src/nxt_http_compression.h new file mode 100644 index 00000000..a76f3993 --- /dev/null +++ b/src/nxt_http_compression.h @@ -0,0 +1,91 @@ +/* + * Copyright (C) Andrew Clayton + * Copyright (C) F5, Inc. + */ + +#ifndef _NXT_COMPRESSION_H_INCLUDED_ +#define _NXT_COMPRESSION_H_INCLUDED_ + +#include + +#include +#include +#include + +#if NXT_HAVE_ZLIB +#include +#endif + +#if NXT_HAVE_ZSTD +#include +#endif + +#if NXT_HAVE_BROTLI +#include +#endif + +#include +#include +#include +#include + +#if NXT_HAVE_ZLIB +#define NXT_HTTP_COMP_ZLIB_DEFAULT_LEVEL Z_DEFAULT_COMPRESSION +#endif +#if NXT_HAVE_ZSTD +#define NXT_HTTP_COMP_ZSTD_DEFAULT_LEVEL ZSTD_CLEVEL_DEFAULT +#endif +#if NXT_HAVE_BROTLI +#define NXT_HTTP_COMP_BROTLI_DEFAULT_LEVEL BROTLI_DEFAULT_QUALITY +#endif + +typedef struct nxt_http_comp_compressor_ctx_s nxt_http_comp_compressor_ctx_t; +typedef struct nxt_http_comp_operations_s nxt_http_comp_operations_t; + +struct nxt_http_comp_compressor_ctx_s { + int8_t level; + + union { +#if NXT_HAVE_ZLIB + z_stream zlib_ctx; +#endif +#if NXT_HAVE_ZSTD + ZSTD_CStream *zstd_ctx; +#endif +#if NXT_HAVE_BROTLI + BrotliEncoderState *brotli_ctx; +#endif + }; +}; + +struct nxt_http_comp_operations_s { + void (*init)(nxt_http_comp_compressor_ctx_t *ctx); + size_t (*compressed_size)(const nxt_http_comp_compressor_ctx_t *ctx, + size_t in_len); + ssize_t (*deflate)(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); + void (*free_ctx)(const nxt_http_comp_compressor_ctx_t *ctx); +}; + +#if NXT_HAVE_ZLIB +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 + +#if NXT_HAVE_BROTLI +extern const nxt_http_comp_operations_t nxt_comp_brotli_ops; +#endif + +extern bool nxt_http_comp_compressor_is_valid(const nxt_str_t *token); +extern nxt_int_t nxt_http_comp_check_compression(nxt_task_t *task, + nxt_http_request_t *r); +extern nxt_int_t nxt_http_comp_compress_response(nxt_buf_t *out); +extern nxt_int_t nxt_http_comp_compression_init(nxt_task_t *task, + nxt_router_conf_t *rtcf, const nxt_conf_value_t *comp_conf); + +#endif /* _NXT_COMPRESSION_H_INCLUDED_ */ -- cgit