diff options
author | Andrew Clayton <a.clayton@nginx.com> | 2024-06-11 00:24:28 +0100 |
---|---|---|
committer | Andrew Clayton <a.clayton@nginx.com> | 2024-06-19 16:15:04 +0100 |
commit | 4071de5797da0a104b4983fc1ad393d3744b6128 (patch) | |
tree | 003803a861f2609cfae58db412e1c3d7ee6dc8f9 /src/nxt_zlib.c | |
parent | ea5c41b8056997ed40138020272b5159271f1b87 (diff) | |
download | unit-compr.tar.gz unit-compr.tar.bz2 |
[WIP] HTTP Compression Supportcompr
Diffstat (limited to 'src/nxt_zlib.c')
-rw-r--r-- | src/nxt_zlib.c | 79 |
1 files changed, 79 insertions, 0 deletions
diff --git a/src/nxt_zlib.c b/src/nxt_zlib.c new file mode 100644 index 00000000..2dcc53a8 --- /dev/null +++ b/src/nxt_zlib.c @@ -0,0 +1,79 @@ +/* + * + */ + +#include <stddef.h> +#include <stdint.h> +#include <stdbool.h> + +#include <zlib.h> + +#include <nxt_http_compression.h> + +static void nxt_zlib_gzip_init(nxt_http_comp_compressor_ctx_t *ctx) +{ + int ret; + z_stream *z = &ctx->zlib_ctx; + + *z = (z_stream){ }; + + ret = deflateInit2(z, ctx->level, Z_DEFLATED, 9 + 16, 8, + Z_DEFAULT_STRATEGY); +} + +static void nxt_zlib_deflate_init(nxt_http_comp_compressor_ctx_t *ctx) +{ + int ret; + z_stream *z = &ctx->zlib_ctx; + + *z = (z_stream){ }; + + ret = deflateInit2(z, ctx->level, Z_DEFLATED, 9, 8, Z_DEFAULT_STRATEGY); +} + +static size_t nxt_zlib_compressed_size(const nxt_http_comp_compressor_ctx_t *ctx, + size_t in_len) +{ + z_stream *z = &ctx->zlib_ctx; + + return deflateBound(z, in_len); +} + +static ssize_t nxt_zlib_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) +{ + int ret; + z_stream *z = &ctx->zlib_ctx; + size_t compressed_bytes = z->total_out; + + z->avail_in = in_len; + z->next_in = (z_const Bytef *)in_buf; + + z->avail_out = out_len; + z->next_out = out_buf; + + ret = deflate(z, last ? Z_FINISH : Z_SYNC_FLUSH); + if (ret == Z_STREAM_ERROR || ret == Z_BUF_ERROR) { + deflateEnd(z); + printf("%s: ret = %d\n", __func__, ret); + return -1; + } + + if (last) + deflateEnd(z); + + return z->total_out - compressed_bytes; +} + +const nxt_http_comp_operations_t nxt_comp_deflate_ops = { + .init = nxt_zlib_deflate_init, + .compressed_size = nxt_zlib_compressed_size, + .deflate = nxt_zlib_deflate, +}; + +const nxt_http_comp_operations_t nxt_comp_gzip_ops = { + .init = nxt_zlib_gzip_init, + .compressed_size = nxt_zlib_compressed_size, + .deflate = nxt_zlib_deflate, +}; |