Skip to content

Commit 1e0fa0b

Browse files
authored
feat: add support for dynamic modules (#10)
* add support for dynamic module * add support for dynamic module * Switch level checking bounds to use ZSTD_minCLevel() to allow negative levels As of zstd 1.3.4 negative compression levels are supported for faster compression. The ZSTD_minCLevel() function was added in version 1.3.6. (merged manually from tpunder/zstd-nginx-module)
1 parent d082422 commit 1e0fa0b

File tree

5 files changed

+279
-144
lines changed

5 files changed

+279
-144
lines changed

config

Lines changed: 8 additions & 140 deletions
Original file line numberDiff line numberDiff line change
@@ -1,143 +1,11 @@
1-
if [ "$ngx_module_link" != ADDON ]; then
2-
cat << END
3-
$0: error: ngx_http_zstd_filter_module can only be linked as an addon.
4-
END
5-
fi
1+
# Make sure the module knows it is a submodule.
2+
ngx_addon_name=ngx_zstd
3+
. $ngx_addon_dir/filter/config
64

7-
ngx_feature_incs="#include <zstd.h>"
8-
ngx_feature_test="(void) ZSTD_createCCtx();"
9-
ngx_feature_libs=
10-
ngx_feature_run=yes
5+
# Make sure the module knows it is a submodule.
6+
ngx_addon_name=ngx_zstd
7+
. $ngx_addon_dir/static/config
118

12-
ngx_zstd_opt_I=
13-
ngx_zstd_opt_L=
9+
# The final name for reporting.
10+
ngx_addon_name=ngx_zstd
1411

15-
if [ -n "$ZSTD_INC" -o -n "$ZSTD_LIB" ]; then
16-
ngx_feature="ZStandard static library in $ZSTD_INC and $ZSTD_LIB"
17-
ngx_feature_path=$ZSTD_INC
18-
19-
# we try the static shared library firstly
20-
ngx_zstd_opt_I="-I$ZSTD_INC -DZSTD_STATIC_LINKING_ONLY"
21-
ngx_zstd_opt_L="$ZSTD_LIB/libzstd.a"
22-
SAVED_CC_TAST_FLAGS=$CC_TEST_FLAGS
23-
CC_TEST_FLAGS="$ngx_zstd_opt_I $CC_TEST_FLAGS"
24-
SAVED_NGX_TEST_LD_OPT=$NGX_TEST_LD_OPT
25-
NGX_TEST_LD_OPT="$ngx_zstd_opt_L $NGX_TEST_LD_OPT"
26-
27-
. auto/feature
28-
29-
# restore
30-
CC_TEST_FLAGS=$SAVED_CC_TAST_FLAGS
31-
NGX_TEST_LD_OPT=$SAVED_NGX_TEST_LD_OPT
32-
33-
if [ $ngx_found = no ]; then
34-
# then try the dynamic shared library
35-
ngx_feature="ZStandard dynamic library in $ZSTD_INC and $ZSTD_LIB"
36-
ngx_zstd_opt_L="-L$ZSTD_LIB -lzstd -Wl,-rpath, $ZSTD_LIB"
37-
38-
SAVED_CC_TAST_FLAGS=$CC_TEST_FLAGS
39-
CC_TEST_FLAGS="$ngx_zstd_opt_I $CC_TEST_FLAGS"
40-
SAVED_NGX_TEST_LD_OPT=$NGX_TEST_LD_OPT
41-
NGX_TEST_LD_OPT="$ngx_zstd_opt_L $NGX_TEST_LD_OPT"
42-
43-
. auto/feature
44-
45-
# restore
46-
CC_TEST_FLAGS=$SAVED_CC_TAST_FLAGS
47-
NGX_TEST_LD_OPT=$SAVED_NGX_TEST_LD_OPT
48-
49-
if [ $ngx_found = no ]; then
50-
cat << END
51-
$0: error: ngx_http_zstd_filter_module requires the ZStandard library, please be sure that "\$ZSTD_INC" and "\$ZSTD_LIB" are set correctly.
52-
END
53-
exit 1
54-
fi
55-
56-
cat << END
57-
$0: warning: ngx_http_zstd_filter_module uses advanced ZStandard APIs (which are still considered experimental) while you are trying to link the dynamic shared library ($ZSTD_LIB).
58-
END
59-
fi
60-
else
61-
# auto-discovery
62-
ngx_feature="ZStandard static library"
63-
ngx_zstd_opt_I="-DZSTD_STATIC_LINKING_ONLY"
64-
ngx_zstd_opt_L="-l:libzstd.a"
65-
66-
# still we consider the static library firstly
67-
SAVED_CC_TAST_FLAGS=$CC_TEST_FLAGS
68-
CC_TEST_FLAGS="$ngx_zstd_opt_I $CC_TEST_FLAGS"
69-
SAVED_NGX_TEST_LD_OPT=$NGX_TEST_LD_OPT
70-
NGX_TEST_LD_OPT="$ngx_zstd_opt_L $NGX_TEST_LD_OPT"
71-
72-
. auto/feature
73-
74-
# restore
75-
CC_TEST_FLAGS=$SAVED_CC_TAST_FLAGS
76-
NGX_TEST_LD_OPT=$SAVED_NGX_TEST_LD_OPT
77-
78-
if [ $ngx_found = no ]; then
79-
80-
ngx_feature="ZStandard dynamic library"
81-
ngx_zstd_opt_L="-lzstd"
82-
SAVED_CC_TAST_FLAGS=$CC_TEST_FLAGS
83-
CC_TEST_FLAGS="$ngx_zstd_opt_I $CC_TEST_FLAGS"
84-
SAVED_NGX_TEST_LD_OPT=$NGX_TEST_LD_OPT
85-
NGX_TEST_LD_OPT="$ngx_zstd_opt_L $NGX_TEST_LD_OPT"
86-
87-
. auto/feature
88-
89-
if [ $ngx_found = no ]; then
90-
cat << END
91-
$0: error: ngx_http_zstd_filter_module requires the ZStandard library.
92-
END
93-
exit 1
94-
fi
95-
96-
# restore
97-
CC_TEST_FLAGS=$SAVED_CC_TAST_FLAGS
98-
NGX_TEST_LD_OPT=$SAVED_NGX_TEST_LD_OPT
99-
100-
cat << END
101-
$0: warning: ngx_http_zstd_filter_module uses advanced ZStandard APIs (which are still considered experimental) while you are trying to link the dynamic shared library.
102-
END
103-
fi
104-
105-
# TODO we need more tries for the different OS port.
106-
fi
107-
108-
HTTP_ZSTD_SRCS="$ngx_addon_dir/src/ngx_http_zstd_filter_module.c"
109-
110-
ngx_module_type=HTTP_FILTER
111-
ngx_module_name=ngx_http_zstd_filter_module
112-
ngx_module_incs="$ngx_zstd_opt_I"
113-
ngx_module_srcs=$HTTP_ZSTD_SRCS
114-
115-
. auto/module
116-
117-
# ngx_http_zstd_filter_module should be positioned after
118-
# ngx_http_postpone_filter_module (just like ngx_http_gzip_filter_module),
119-
# so we put it before ngx_http_gzip_filter_module
120-
# (or ngx_http_range_header_filter_module if ngx_http_gzip_filter_module is not
121-
# compiled).
122-
if [ "$HTTP_GZIP" = YES ]; then
123-
next=ngx_http_gzip_filter_module
124-
else
125-
next=ngx_http_range_header_filter_module
126-
fi
127-
128-
HTTP_FILTER_MODULES=`echo $HTTP_FILTER_MODULES \
129-
| sed "s/$ngx_module_name//" \
130-
| sed "s/$next/$next $ngx_module_name/"`
131-
132-
CFLAGS="$ngx_zstd_opt_I $CFLAGS"
133-
NGX_LD_OPT="$ngx_zstd_opt_L $NGX_LD_OPT"
134-
135-
# build the ngx_http_zstd_static_module
136-
HTTP_ZSTD_SRCS="$ngx_addon_dir/src/ngx_http_zstd_static_module.c"
137-
138-
ngx_module_type=HTTP
139-
ngx_module_name=ngx_http_zstd_static_module
140-
ngx_module_incs="$ngx_zstd_opt_I"
141-
ngx_module_srcs=$HTTP_ZSTD_SRCS
142-
143-
. auto/module

filter/config

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
ngx_feature_incs="#include <zstd.h>"
2+
ngx_feature_test="(void) ZSTD_createCCtx();"
3+
ngx_feature_libs=
4+
ngx_feature_run=yes
5+
6+
ngx_zstd_opt_I=
7+
ngx_zstd_opt_L=
8+
9+
if [ -n "$ZSTD_INC" -o -n "$ZSTD_LIB" ]; then
10+
ngx_feature="ZStandard static library in $ZSTD_INC and $ZSTD_LIB"
11+
ngx_feature_path=$ZSTD_INC
12+
13+
# we try the static shared library firstly
14+
ngx_zstd_opt_I="-I$ZSTD_INC -DZSTD_STATIC_LINKING_ONLY"
15+
ngx_zstd_opt_L="$ZSTD_LIB/libzstd.a"
16+
SAVED_CC_TAST_FLAGS=$CC_TEST_FLAGS
17+
CC_TEST_FLAGS="$ngx_zstd_opt_I $CC_TEST_FLAGS"
18+
SAVED_NGX_TEST_LD_OPT=$NGX_TEST_LD_OPT
19+
NGX_TEST_LD_OPT="$ngx_zstd_opt_L $NGX_TEST_LD_OPT"
20+
21+
. auto/feature
22+
23+
# restore
24+
CC_TEST_FLAGS=$SAVED_CC_TAST_FLAGS
25+
NGX_TEST_LD_OPT=$SAVED_NGX_TEST_LD_OPT
26+
27+
if [ $ngx_found = no ]; then
28+
# then try the dynamic shared library
29+
ngx_feature="ZStandard dynamic library in $ZSTD_INC and $ZSTD_LIB"
30+
ngx_zstd_opt_L="-L$ZSTD_LIB -lzstd -Wl,-rpath, $ZSTD_LIB"
31+
32+
SAVED_CC_TAST_FLAGS=$CC_TEST_FLAGS
33+
CC_TEST_FLAGS="$ngx_zstd_opt_I $CC_TEST_FLAGS"
34+
SAVED_NGX_TEST_LD_OPT=$NGX_TEST_LD_OPT
35+
NGX_TEST_LD_OPT="$ngx_zstd_opt_L $NGX_TEST_LD_OPT"
36+
37+
. auto/feature
38+
39+
# restore
40+
CC_TEST_FLAGS=$SAVED_CC_TAST_FLAGS
41+
NGX_TEST_LD_OPT=$SAVED_NGX_TEST_LD_OPT
42+
43+
if [ $ngx_found = no ]; then
44+
cat << END
45+
$0: error: ngx_http_zstd_filter_module requires the ZStandard library, please be sure that "\$ZSTD_INC" and "\$ZSTD_LIB" are set correctly.
46+
END
47+
exit 1
48+
fi
49+
50+
fi
51+
else
52+
# auto-discovery
53+
ngx_feature="ZStandard static library"
54+
ngx_zstd_opt_I="-DZSTD_STATIC_LINKING_ONLY"
55+
ngx_zstd_opt_L="-l:libzstd.a"
56+
57+
# still we consider the static library firstly
58+
SAVED_CC_TAST_FLAGS=$CC_TEST_FLAGS
59+
CC_TEST_FLAGS="$ngx_zstd_opt_I $CC_TEST_FLAGS"
60+
SAVED_NGX_TEST_LD_OPT=$NGX_TEST_LD_OPT
61+
NGX_TEST_LD_OPT="$ngx_zstd_opt_L $NGX_TEST_LD_OPT"
62+
63+
. auto/feature
64+
65+
# restore
66+
CC_TEST_FLAGS=$SAVED_CC_TAST_FLAGS
67+
NGX_TEST_LD_OPT=$SAVED_NGX_TEST_LD_OPT
68+
69+
if [ $ngx_found = no ]; then
70+
71+
ngx_feature="ZStandard dynamic library"
72+
ngx_zstd_opt_L="-lzstd"
73+
SAVED_CC_TAST_FLAGS=$CC_TEST_FLAGS
74+
CC_TEST_FLAGS="$ngx_zstd_opt_I $CC_TEST_FLAGS"
75+
SAVED_NGX_TEST_LD_OPT=$NGX_TEST_LD_OPT
76+
NGX_TEST_LD_OPT="$ngx_zstd_opt_L $NGX_TEST_LD_OPT"
77+
78+
. auto/feature
79+
80+
if [ $ngx_found = no ]; then
81+
cat << END
82+
$0: error: ngx_http_zstd_filter_module requires the ZStandard library.
83+
END
84+
exit 1
85+
fi
86+
87+
# restore
88+
CC_TEST_FLAGS=$SAVED_CC_TAST_FLAGS
89+
NGX_TEST_LD_OPT=$SAVED_NGX_TEST_LD_OPT
90+
91+
cat << END
92+
$0: warning: ngx_http_zstd_filter_module uses advanced ZStandard APIs (which are still considered experimental) while you are trying to link the dynamic shared library.
93+
END
94+
fi
95+
96+
# TODO we need more tries for the different OS port.
97+
fi
98+
99+
NGX_LD_OPT="$ngx_zstd_opt_L $NGX_LD_OPT"
100+
101+
HTTP_ZSTD_SRCS="$ngx_addon_dir/filter/ngx_http_zstd_filter_module.c"
102+
103+
ngx_addon_name=ngx_http_zstd_filter_module
104+
ngx_module_type=HTTP_FILTER
105+
ngx_module_name=ngx_http_zstd_filter_module
106+
ngx_module_incs="$ngx_zstd_opt_I"
107+
ngx_module_srcs=$HTTP_ZSTD_SRCS
108+
ngx_module_libs=$NGX_LD_OPT
109+
. auto/module
110+

src/ngx_http_zstd_filter_module.c renamed to filter/ngx_http_zstd_filter_module.c

Lines changed: 50 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ static ngx_int_t ngx_http_zstd_ratio_variable(ngx_http_request_t *r,
102102
static void * ngx_http_zstd_filter_alloc(void *opaque, size_t size);
103103
static void ngx_http_zstd_filter_free(void *opaque, void *address);
104104
static char *ngx_http_zstd_comp_level(ngx_conf_t *cf, void *post, void *data);
105+
static char *ngx_conf_zstd_set_num_slot_with_negatives(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
105106

106107

107108
static ngx_http_zstd_comp_level_bounds_t ngx_http_zstd_comp_level_bounds = {
@@ -121,7 +122,7 @@ static ngx_command_t ngx_http_zstd_filter_commands[] = {
121122

122123
{ ngx_string("zstd_comp_level"),
123124
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
124-
ngx_conf_set_num_slot,
125+
ngx_conf_zstd_set_num_slot_with_negatives,
125126
NGX_HTTP_LOC_CONF_OFFSET,
126127
offsetof(ngx_http_zstd_loc_conf_t, level),
127128
&ngx_http_zstd_comp_level_bounds },
@@ -964,13 +965,58 @@ ngx_http_zstd_comp_level(ngx_conf_t *cf, void *post, void *data)
964965
{
965966
ngx_int_t *np = data;
966967

967-
if (*np < 1 || *np > ZSTD_maxCLevel()) {
968+
if (*np == 0 || *np < (ngx_int_t)ZSTD_minCLevel() || *np > ZSTD_maxCLevel()) {
968969
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
969-
"zstd compress level must between 1 and %i",
970-
ZSTD_maxCLevel());
970+
"zstd compress level must between %i and %i excluding 0",
971+
(ngx_int_t)ZSTD_minCLevel(), ZSTD_maxCLevel());
971972

972973
return NGX_CONF_ERROR;
973974
}
974975

975976
return NGX_CONF_OK;
976977
}
978+
979+
static char *
980+
ngx_conf_zstd_set_num_slot_with_negatives(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
981+
{
982+
char *p = conf;
983+
984+
ngx_int_t *np;
985+
ngx_str_t *value;
986+
ngx_conf_post_t *post;
987+
988+
989+
np = (ngx_int_t *) (p + cmd->offset);
990+
991+
if (*np != NGX_CONF_UNSET) {
992+
return "is duplicate";
993+
}
994+
995+
value = cf->args->elts;
996+
997+
if (*(value[1].data) == '-') {
998+
// Parse ignoring the leading '-' character
999+
*np = ngx_atoi(value[1].data + 1, value[1].len - 1);
1000+
1001+
// NGX_ERROR is -1 so we need to check for that before making the parsed
1002+
// result negative
1003+
if (*np == NGX_ERROR) {
1004+
return "invalid number";
1005+
}
1006+
1007+
*np = -*np;
1008+
} else {
1009+
*np = ngx_atoi(value[1].data, value[1].len);
1010+
1011+
if (*np == NGX_ERROR) {
1012+
return "invalid number";
1013+
}
1014+
}
1015+
1016+
if (cmd->post) {
1017+
post = cmd->post;
1018+
return post->post_handler(cf, post, np);
1019+
}
1020+
1021+
return NGX_CONF_OK;
1022+
}

0 commit comments

Comments
 (0)