]> git.kaiwu.me - haproxy.git/commitdiff
MEDIUM: flt-http-comp: Enable compression directions from algorithms
authorChristopher Faulet <cfaulet@haproxy.com>
Tue, 29 Sep 2026 13:51:49 +0000 (15:51 +0200)
committerChristopher Faulet <cfaulet@haproxy.com>
Fri, 9 Oct 2026 14:17:35 +0000 (16:17 +0200)
Compression options used to default to the response direction, even when
only a request algorithm was configured. Request compression then needed an
explicit direction or comp-req filter declaration. Also, the initial
direction depended on whether a filter declaration or a compression option
allocated the configuration first.

Start with no direction and let algo-req enable request compression and
algo/algo-res enable response compression. Add COMP_FL_DIR_FORCED to
remember an explicit compression direction, so that subsequent algorithm
directives do not re-enable a direction it excluded. This flag is also
inherited from defaults along with the other compression settings.

Only algorithm and direction directives enable compression implicitly.
Type, minimum-size and offload options configure its behavior but do not
enable a direction by themselves. Without an algorithm for that direction,
no compression can take place anyway.

This also avoids implicitly enabling response compression just because
request compression options were parsed before an explicit comp-req
filter. The legacy compression algo directive still enables responses.

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

index fac475eeef441417869b9bb37596a39a61e96718..39e4175710faaa36aad097576cbe73bba02d75b6 100644 (file)
@@ -44,6 +44,7 @@
 #define COMP_FL_OFFLOAD                0x00000001 /* Compression offload */
 #define COMP_FL_DIR_REQ                0x00000002 /* Compress requests */
 #define COMP_FL_DIR_RES                0x00000004 /* Compress responses */
+#define COMP_FL_DIR_FORCED     0x00000008 /* Compress direction was forced */
 
 struct comp {
        struct comp_algo *algos_res; /* Algos available for response */
index a5e3c0a6fc9a62cfdb7d24879a236711cd9b539c..1d6ca1a9cf05df7c0b4d2ada08cf67016f489c49 100644 (file)
@@ -857,8 +857,8 @@ parse_compression_options(char **args, int section, struct proxy *proxy,
        int ret = 0;
        const char *res;
 
-       /* always default to compress responses */
-       comp = proxy_get_comp(proxy, COMP_FL_DIR_RES);
+       /* Algorithms enable their direction unless an explicit direction was set. */
+       comp = proxy_get_comp(proxy, 0);
        if (comp == NULL) {
                memprintf(err, "'%s': out of memory.", args[0]);
                ret = -1;
@@ -899,6 +899,8 @@ parse_compression_options(char **args, int section, struct proxy *proxy,
                        cur_arg++;
                        continue;
                }
+               if (!(comp->flags & COMP_FL_DIR_FORCED))
+                       comp->flags |= COMP_FL_DIR_RES;
        }
        else if (strcmp(args[1], "algo-req") == 0) {
                struct comp_ctx *ctx;
@@ -923,6 +925,8 @@ parse_compression_options(char **args, int section, struct proxy *proxy,
                        ret = -1;
                        goto end;
                }
+               if (!(comp->flags & COMP_FL_DIR_FORCED))
+                       comp->flags |= COMP_FL_DIR_REQ;
        }
        else if (strcmp(args[1], "offload") == 0) {
                if (proxy->cap & PR_CAP_DEF) {
@@ -1002,12 +1006,12 @@ parse_compression_options(char **args, int section, struct proxy *proxy,
                }
                if (strcmp(args[2], "request") == 0) {
                        comp->flags &= ~COMP_FL_DIR_RES;
-                       comp->flags |= COMP_FL_DIR_REQ;
+                       comp->flags |= (COMP_FL_DIR_FORCED | COMP_FL_DIR_REQ);
                } else if (strcmp(args[2], "response") == 0) {
                        comp->flags &= ~COMP_FL_DIR_REQ;
-                       comp->flags |= COMP_FL_DIR_RES;
+                       comp->flags |= (COMP_FL_DIR_FORCED | COMP_FL_DIR_RES);
                } else if (strcmp(args[2], "both") == 0)
-                       comp->flags |= COMP_FL_DIR_REQ | COMP_FL_DIR_RES;
+                       comp->flags |= (COMP_FL_DIR_FORCED | COMP_FL_DIR_REQ | COMP_FL_DIR_RES);
                else {
                        memprintf(err, "'%s' expects 'request', 'response', or 'both'.", args[0]);
                        ret = -1;