Skip to content

Commit 511b217

Browse files
committed
http: Add support for zstd compression
Signed-off-by: Andrew Clayton <[email protected]>
1 parent 47cbbbb commit 511b217

File tree

3 files changed

+97
-0
lines changed

3 files changed

+97
-0
lines changed

src/nxt_http_compression.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ enum nxt_http_comp_scheme_e {
3131
NXT_HTTP_COMP_SCHEME_DEFLATE,
3232
NXT_HTTP_COMP_SCHEME_GZIP,
3333
#endif
34+
#if NXT_HAVE_ZSTD
35+
NXT_HTTP_COMP_SCHEME_ZSTD,
36+
#endif
3437

3538
/* keep last */
3639
NXT_HTTP_COMP_SCHEME_UNKNOWN
@@ -108,6 +111,15 @@ static const nxt_http_comp_type_t nxt_http_comp_compressors[] = {
108111
.comp_min = NXT_HTTP_COMP_ZLIB_COMP_MIN,
109112
.comp_max = NXT_HTTP_COMP_ZLIB_COMP_MAX,
110113
.cops = &nxt_http_comp_gzip_ops,
114+
#endif
115+
#if NXT_HAVE_ZSTD
116+
}, {
117+
.token = nxt_string("zstd"),
118+
.scheme = NXT_HTTP_COMP_SCHEME_ZSTD,
119+
.def_compr = NXT_HTTP_COMP_ZSTD_DEFAULT_LEVEL,
120+
.comp_min = NXT_HTTP_COMP_ZSTD_COMP_MIN,
121+
.comp_max = NXT_HTTP_COMP_ZSTD_COMP_MAX,
122+
.cops = &nxt_http_comp_zstd_ops,
111123
#endif
112124
},
113125
};

src/nxt_http_compression.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@
1616
#include <zlib.h>
1717
#endif
1818

19+
#if NXT_HAVE_ZSTD
20+
#include <zstd.h>
21+
#endif
22+
1923
#include <nxt_main.h>
2024
#include <nxt_router.h>
2125
#include <nxt_string.h>
@@ -27,6 +31,11 @@
2731
#define NXT_HTTP_COMP_ZLIB_COMP_MIN Z_DEFAULT_COMPRESSION
2832
#define NXT_HTTP_COMP_ZLIB_COMP_MAX Z_BEST_COMPRESSION
2933
#endif
34+
#if NXT_HAVE_ZSTD
35+
#define NXT_HTTP_COMP_ZSTD_DEFAULT_LEVEL ZSTD_CLEVEL_DEFAULT
36+
#define NXT_HTTP_COMP_ZSTD_COMP_MIN (-7)
37+
#define NXT_HTTP_COMP_ZSTD_COMP_MAX 22
38+
#endif
3039

3140

3241
typedef struct nxt_http_comp_compressor_ctx_s nxt_http_comp_compressor_ctx_t;
@@ -38,6 +47,9 @@ struct nxt_http_comp_compressor_ctx_s {
3847
union {
3948
#if NXT_HAVE_ZLIB
4049
z_stream zlib_ctx;
50+
#endif
51+
#if NXT_HAVE_ZSTD
52+
ZSTD_CStream *zstd_ctx;
4153
#endif
4254
};
4355
};
@@ -57,6 +69,10 @@ extern const nxt_http_comp_operations_t nxt_http_comp_deflate_ops;
5769
extern const nxt_http_comp_operations_t nxt_http_comp_gzip_ops;
5870
#endif
5971

72+
#if NXT_HAVE_ZSTD
73+
extern const nxt_http_comp_operations_t nxt_http_comp_zstd_ops;
74+
#endif
75+
6076

6177
extern bool nxt_http_comp_wants_compression(void);
6278
extern bool nxt_http_comp_compressor_is_valid(const nxt_str_t *token);

src/nxt_zstd.c

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
* Copyright (C) Andrew Clayton
3+
* Copyright (C) F5, Inc.
4+
*/
5+
6+
#include <stddef.h>
7+
#include <stdint.h>
8+
#include <stdbool.h>
9+
10+
#include <zstd.h>
11+
12+
#include "nxt_http_compression.h"
13+
14+
15+
static int
16+
nxt_zstd_init(nxt_http_comp_compressor_ctx_t *ctx)
17+
{
18+
ZSTD_CStream **zstd = &ctx->zstd_ctx;
19+
20+
*zstd = ZSTD_createCStream();
21+
if (*zstd == NULL) {
22+
return -1;
23+
}
24+
ZSTD_initCStream(*zstd, ctx->level);
25+
26+
return 0;
27+
}
28+
29+
30+
static size_t
31+
nxt_zstd_bound(const nxt_http_comp_compressor_ctx_t *ctx, size_t in_len)
32+
{
33+
return ZSTD_compressBound(in_len);
34+
}
35+
36+
37+
static ssize_t
38+
nxt_zstd_compress(nxt_http_comp_compressor_ctx_t *ctx, const uint8_t *in_buf,
39+
size_t in_len, uint8_t *out_buf, size_t out_len, bool last)
40+
{
41+
size_t ret;
42+
ZSTD_CStream *zstd = ctx->zstd_ctx;
43+
ZSTD_inBuffer zinb = { .src = in_buf, .size = in_len };
44+
ZSTD_outBuffer zoutb = { .dst = out_buf, .size = out_len };
45+
46+
ret = ZSTD_compressStream(zstd, &zoutb, &zinb);
47+
48+
if (zinb.pos < zinb.size) {
49+
ret = ZSTD_flushStream(zstd, &zoutb);
50+
}
51+
52+
if (last) {
53+
ret = ZSTD_endStream(zstd, &zoutb);
54+
ZSTD_freeCStream(zstd);
55+
}
56+
57+
if (ZSTD_isError(ret)) {
58+
return -1;
59+
}
60+
61+
return zoutb.pos;
62+
}
63+
64+
65+
const nxt_http_comp_operations_t nxt_http_comp_zstd_ops = {
66+
.init = nxt_zstd_init,
67+
.bound = nxt_zstd_bound,
68+
.deflate = nxt_zstd_compress,
69+
};

0 commit comments

Comments
 (0)