summaryrefslogtreecommitdiffhomepage
path: root/src/nxt_http_compression.h
blob: a76f3993aede107a8c43ff7b29344116d9713645 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/*
 * Copyright (C) Andrew Clayton
 * Copyright (C) F5, Inc.
 */

#ifndef _NXT_COMPRESSION_H_INCLUDED_
#define _NXT_COMPRESSION_H_INCLUDED_

#include <nxt_auto_config.h>

#include <stddef.h>
#include <stdint.h>
#include <stdbool.h>

#if NXT_HAVE_ZLIB
#include <zlib.h>
#endif

#if NXT_HAVE_ZSTD
#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>
#include <nxt_conf.h>

#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_ */