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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
|
/*
* 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
#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
#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;
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 (*bound)(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 nxt_int_t nxt_http_comp_compress_app_response(nxt_http_request_t *r);
extern nxt_int_t nxt_http_comp_compress_static_response(nxt_task_t *task,
nxt_file_t **f, nxt_file_info_t *fi, size_t static_buf_len,
size_t *out_total);
extern bool nxt_http_comp_wants_compression(void);
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_compression_init(nxt_task_t *task,
nxt_router_conf_t *rtcf, const nxt_conf_value_t *comp_conf);
#endif /* _NXT_COMPRESSION_H_INCLUDED_ */
|