MINOR: compression: Store algo and type for both request and response

Make provision for being able to store both compression algorithms and
content-types to compress for both requests and responses. For now only
the responses one are used.
This commit is contained in:
Olivier Houchard 2023-04-05 17:32:36 +02:00 committed by Olivier Houchard
parent dfc11da561
commit db573e9c58
5 changed files with 30 additions and 26 deletions

View File

@ -43,8 +43,10 @@
#define COMP_FL_OFFLOAD 0x00000001 /* Compression offload */ #define COMP_FL_OFFLOAD 0x00000001 /* Compression offload */
struct comp { struct comp {
struct comp_algo *algos; struct comp_algo *algos_res; /* Algos available for response */
struct comp_type *types; struct comp_algo *algo_req; /* Algo to use for request */
struct comp_type *types_req; /* Types to be compressed for requests */
struct comp_type *types_res; /* Types to be compressed for responses */
unsigned int flags; unsigned int flags;
}; };

View File

@ -27,8 +27,8 @@
extern unsigned int compress_min_idle; extern unsigned int compress_min_idle;
int comp_append_type(struct comp *comp, const char *type); int comp_append_type(struct comp_type **types, const char *type);
int comp_append_algo(struct comp *comp, const char *algo); int comp_append_algo(struct comp_algo **algos, const char *algo);
#ifdef USE_ZLIB #ifdef USE_ZLIB
extern long zlib_used_memory; extern long zlib_used_memory;

View File

@ -110,7 +110,7 @@ const struct comp_algo comp_algos[] =
* Add a content-type in the configuration * Add a content-type in the configuration
* Returns 0 in case of success, 1 in case of allocation failure. * Returns 0 in case of success, 1 in case of allocation failure.
*/ */
int comp_append_type(struct comp *comp, const char *type) int comp_append_type(struct comp_type **types, const char *type)
{ {
struct comp_type *comp_type; struct comp_type *comp_type;
@ -119,8 +119,8 @@ int comp_append_type(struct comp *comp, const char *type)
return 1; return 1;
comp_type->name_len = strlen(type); comp_type->name_len = strlen(type);
comp_type->name = strdup(type); comp_type->name = strdup(type);
comp_type->next = comp->types; comp_type->next = *types;
comp->types = comp_type; *types = comp_type;
return 0; return 0;
} }
@ -129,7 +129,7 @@ int comp_append_type(struct comp *comp, const char *type)
* Returns 0 in case of success, -1 if the <algo> is unmanaged, 1 in case of * Returns 0 in case of success, -1 if the <algo> is unmanaged, 1 in case of
* allocation failure. * allocation failure.
*/ */
int comp_append_algo(struct comp *comp, const char *algo) int comp_append_algo(struct comp_algo **algos, const char *algo)
{ {
struct comp_algo *comp_algo; struct comp_algo *comp_algo;
int i; int i;
@ -140,8 +140,8 @@ int comp_append_algo(struct comp *comp, const char *algo)
if (!comp_algo) if (!comp_algo)
return 1; return 1;
memmove(comp_algo, &comp_algos[i], sizeof(struct comp_algo)); memmove(comp_algo, &comp_algos[i], sizeof(struct comp_algo));
comp_algo->next = comp->algos; comp_algo->next = *algos;
comp->algos = comp_algo; *algos = comp_algo;
return 0; return 0;
} }
} }

View File

@ -415,8 +415,8 @@ select_compression_request_header(struct comp_state *st, struct stream *s, struc
} }
/* search for the algo in the backend in priority or the frontend */ /* search for the algo in the backend in priority or the frontend */
if ((s->be->comp && (comp_algo_back = s->be->comp->algos)) || if ((s->be->comp && (comp_algo_back = s->be->comp->algos_res)) ||
(strm_fe(s)->comp && (comp_algo_back = strm_fe(s)->comp->algos))) { (strm_fe(s)->comp && (comp_algo_back = strm_fe(s)->comp->algos_res))) {
int best_q = 0; int best_q = 0;
ctx.blk = NULL; ctx.blk = NULL;
@ -485,8 +485,8 @@ select_compression_request_header(struct comp_state *st, struct stream *s, struc
} }
/* identity is implicit does not require headers */ /* identity is implicit does not require headers */
if ((s->be->comp && (comp_algo_back = s->be->comp->algos)) || if ((s->be->comp && (comp_algo_back = s->be->comp->algos_res)) ||
(strm_fe(s)->comp && (comp_algo_back = strm_fe(s)->comp->algos))) { (strm_fe(s)->comp && (comp_algo_back = strm_fe(s)->comp->algos_res))) {
for (comp_algo = comp_algo_back; comp_algo; comp_algo = comp_algo->next) { for (comp_algo = comp_algo_back; comp_algo; comp_algo = comp_algo->next) {
if (comp_algo->cfg_name_len == 8 && memcmp(comp_algo->cfg_name, "identity", 8) == 0) { if (comp_algo->cfg_name_len == 8 && memcmp(comp_algo->cfg_name, "identity", 8) == 0) {
st->comp_algo[COMP_DIR_RES] = comp_algo; st->comp_algo[COMP_DIR_RES] = comp_algo;
@ -571,8 +571,8 @@ select_compression_response_header(struct comp_state *st, struct stream *s, stru
if (ctx.value.len >= 9 && strncasecmp("multipart", ctx.value.ptr, 9) == 0) if (ctx.value.len >= 9 && strncasecmp("multipart", ctx.value.ptr, 9) == 0)
goto fail; goto fail;
if ((s->be->comp && (comp_type = s->be->comp->types)) || if ((s->be->comp && (comp_type = s->be->comp->types_res)) ||
(strm_fe(s)->comp && (comp_type = strm_fe(s)->comp->types))) { (strm_fe(s)->comp && (comp_type = strm_fe(s)->comp->types_res))) {
for (; comp_type; comp_type = comp_type->next) { for (; comp_type; comp_type = comp_type->next) {
if (ctx.value.len >= comp_type->name_len && if (ctx.value.len >= comp_type->name_len &&
strncasecmp(ctx.value.ptr, comp_type->name, comp_type->name_len) == 0) strncasecmp(ctx.value.ptr, comp_type->name, comp_type->name_len) == 0)
@ -585,8 +585,8 @@ select_compression_response_header(struct comp_state *st, struct stream *s, stru
} }
} }
else { /* no content-type header */ else { /* no content-type header */
if ((s->be->comp && s->be->comp->types) || if ((s->be->comp && s->be->comp->types_res) ||
(strm_fe(s)->comp && strm_fe(s)->comp->types)) (strm_fe(s)->comp && strm_fe(s)->comp->types_res))
goto fail; /* a content-type was required */ goto fail; /* a content-type was required */
} }
@ -674,7 +674,7 @@ parse_compression_options(char **args, int section, struct proxy *proxy,
else else
comp = proxy->comp; comp = proxy->comp;
if (strcmp(args[1], "algo") == 0) { if (strcmp(args[1], "algo") == 0 || strcmp(args[1], "algo-res") == 0) {
struct comp_ctx *ctx; struct comp_ctx *ctx;
int cur_arg = 2; int cur_arg = 2;
@ -685,7 +685,7 @@ parse_compression_options(char **args, int section, struct proxy *proxy,
goto end; goto end;
} }
while (*(args[cur_arg])) { while (*(args[cur_arg])) {
int retval = comp_append_algo(comp, args[cur_arg]); int retval = comp_append_algo(&comp->algos_res, args[cur_arg]);
if (retval) { if (retval) {
if (retval < 0) if (retval < 0)
memprintf(err, "'%s' : '%s' is not a supported algorithm.", memprintf(err, "'%s' : '%s' is not a supported algorithm.",
@ -697,8 +697,8 @@ parse_compression_options(char **args, int section, struct proxy *proxy,
goto end; goto end;
} }
if (proxy->comp->algos->init(&ctx, 9) == 0) if (proxy->comp->algos_res->init(&ctx, 9) == 0)
proxy->comp->algos->end(&ctx); proxy->comp->algos_res->end(&ctx);
else { else {
memprintf(err, "'%s' : Can't init '%s' algorithm.", memprintf(err, "'%s' : Can't init '%s' algorithm.",
args[0], args[cur_arg]); args[0], args[cur_arg]);
@ -717,7 +717,7 @@ parse_compression_options(char **args, int section, struct proxy *proxy,
} }
comp->flags |= COMP_FL_OFFLOAD; comp->flags |= COMP_FL_OFFLOAD;
} }
else if (strcmp(args[1], "type") == 0) { else if (strcmp(args[1], "type") == 0 || strcmp(args[1], "type-res") == 0) {
int cur_arg = 2; int cur_arg = 2;
if (!*args[cur_arg]) { if (!*args[cur_arg]) {
@ -726,7 +726,7 @@ parse_compression_options(char **args, int section, struct proxy *proxy,
goto end; goto end;
} }
while (*(args[cur_arg])) { while (*(args[cur_arg])) {
if (comp_append_type(comp, args[cur_arg])) { if (comp_append_type(&comp->types_res, args[cur_arg])) {
memprintf(err, "'%s': out of memory.", args[0]); memprintf(err, "'%s': out of memory.", args[0]);
ret = -1; ret = -1;
goto end; goto end;

View File

@ -1901,8 +1901,10 @@ static int proxy_defproxy_cpy(struct proxy *curproxy, const struct proxy *defpro
memprintf(errmsg, "proxy '%s': out of memory for default compression options", curproxy->id); memprintf(errmsg, "proxy '%s': out of memory for default compression options", curproxy->id);
return 1; return 1;
} }
curproxy->comp->algos = defproxy->comp->algos; curproxy->comp->algos_res = defproxy->comp->algos_res;
curproxy->comp->types = defproxy->comp->types; curproxy->comp->algo_req = defproxy->comp->algo_req;
curproxy->comp->types_res = defproxy->comp->types_res;
curproxy->comp->types_req = defproxy->comp->types_req;
} }
if (defproxy->check_path) if (defproxy->check_path)