]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: compression: Store algo and type for both request and response
authorOlivier Houchard <ohouchard@backtrace.io>
Wed, 5 Apr 2023 15:32:36 +0000 (17:32 +0200)
committerOlivier Houchard <cognet@ci0.org>
Thu, 6 Apr 2023 22:46:59 +0000 (00:46 +0200)
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.

include/haproxy/compression-t.h
include/haproxy/compression.h
src/compression.c
src/flt_http_comp.c
src/proxy.c

index 6851103316b88df9f40ab3fb2a3615310a4795a5..346cf2e51ba8c7bd21f77899c8798fd0a137da4c 100644 (file)
 
 #define COMP_FL_OFFLOAD                0x00000001 /* Compression offload */
 struct comp {
-       struct comp_algo *algos;
-       struct comp_type *types;
+       struct comp_algo *algos_res; /* Algos available for response */
+       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;
 };
 
index ba2bdab7cc0654db0ae1a12df1b5d260167ff33a..851ea239333da7b14c05f370cc19fda8765f9a32 100644 (file)
@@ -27,8 +27,8 @@
 
 extern unsigned int compress_min_idle;
 
-int comp_append_type(struct comp *comp, const char *type);
-int comp_append_algo(struct comp *comp, const char *algo);
+int comp_append_type(struct comp_type **types, const char *type);
+int comp_append_algo(struct comp_algo **algos, const char *algo);
 
 #ifdef USE_ZLIB
 extern long zlib_used_memory;
index 3ce2a60057f35430df6720da458e780607e41a3b..7a8a21912237a0a83f101de8630e3831fe12dd8a 100644 (file)
@@ -110,7 +110,7 @@ const struct comp_algo comp_algos[] =
  * Add a content-type in the configuration
  * 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;
 
@@ -119,8 +119,8 @@ int comp_append_type(struct comp *comp, const char *type)
                return 1;
        comp_type->name_len = strlen(type);
        comp_type->name = strdup(type);
-       comp_type->next = comp->types;
-       comp->types = comp_type;
+       comp_type->next = *types;
+       *types = comp_type;
        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
  * 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;
        int i;
@@ -140,8 +140,8 @@ int comp_append_algo(struct comp *comp, const char *algo)
                        if (!comp_algo)
                                return 1;
                        memmove(comp_algo, &comp_algos[i], sizeof(struct comp_algo));
-                       comp_algo->next = comp->algos;
-                       comp->algos = comp_algo;
+                       comp_algo->next = *algos;
+                       *algos = comp_algo;
                        return 0;
                }
        }
index ad2b77c0b89894590ec8775e6f62236834c169d0..8c56d0a46eb77a24602caa597c8f5aaa11f3bb86 100644 (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 */
-       if ((s->be->comp && (comp_algo_back = s->be->comp->algos)) ||
-           (strm_fe(s)->comp && (comp_algo_back = strm_fe(s)->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_res))) {
                int best_q = 0;
 
                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 */
-       if ((s->be->comp && (comp_algo_back = s->be->comp->algos)) ||
-           (strm_fe(s)->comp && (comp_algo_back = strm_fe(s)->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_res))) {
                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) {
                                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)
                        goto fail;
 
-               if ((s->be->comp && (comp_type = s->be->comp->types)) ||
-                   (strm_fe(s)->comp && (comp_type = strm_fe(s)->comp->types))) {
+               if ((s->be->comp && (comp_type = s->be->comp->types_res)) ||
+                   (strm_fe(s)->comp && (comp_type = strm_fe(s)->comp->types_res))) {
                        for (; comp_type; comp_type = comp_type->next) {
                                if (ctx.value.len >= comp_type->name_len &&
                                    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 */
-               if ((s->be->comp && s->be->comp->types) ||
-                   (strm_fe(s)->comp && strm_fe(s)->comp->types))
+               if ((s->be->comp && s->be->comp->types_res) ||
+                   (strm_fe(s)->comp && strm_fe(s)->comp->types_res))
                        goto fail; /* a content-type was required */
        }
 
@@ -674,7 +674,7 @@ parse_compression_options(char **args, int section, struct proxy *proxy,
        else
                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;
                int              cur_arg = 2;
 
@@ -685,7 +685,7 @@ parse_compression_options(char **args, int section, struct proxy *proxy,
                        goto end;
                }
                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 < 0)
                                        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;
                        }
 
-                       if (proxy->comp->algos->init(&ctx, 9) == 0)
-                               proxy->comp->algos->end(&ctx);
+                       if (proxy->comp->algos_res->init(&ctx, 9) == 0)
+                               proxy->comp->algos_res->end(&ctx);
                        else {
                                memprintf(err, "'%s' : Can't init '%s' algorithm.",
                                          args[0], args[cur_arg]);
@@ -717,7 +717,7 @@ parse_compression_options(char **args, int section, struct proxy *proxy,
                }
                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;
 
                if (!*args[cur_arg]) {
@@ -726,7 +726,7 @@ parse_compression_options(char **args, int section, struct proxy *proxy,
                        goto end;
                }
                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]);
                                ret = -1;
                                goto end;
index 8d4de56e3ac05a177375349045c2dd20753f606d..a025e18e9a8744b267155dd9afbf8ededf3fe0de 100644 (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);
                        return 1;
                }
-               curproxy->comp->algos = defproxy->comp->algos;
-               curproxy->comp->types = defproxy->comp->types;
+               curproxy->comp->algos_res = defproxy->comp->algos_res;
+               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)