]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
CLEANUP: flt_http_comp: comp_state doesn't bother about the direction anymore
authorAurelien DARRAGON <adarragon@haproxy.com>
Wed, 4 Mar 2026 16:41:35 +0000 (17:41 +0100)
committerAurelien DARRAGON <adarragon@haproxy.com>
Fri, 6 Mar 2026 12:55:41 +0000 (13:55 +0100)
no need to have duplicated comp_ctx and comp_algo for request vs response
in comp_state struct, because thanks to previous commit compression filter
is either oriented on the request or the response, and 2 distinct filters
are instanciated when we need to handle both requests and responses
compression.

Thus we can save us from duplicated struct members and related operations.

src/flt_http_comp.c

index 98733138b8118306cc46a62173b81e015d443caf..ea9adddc94c76d097c56db1d40be1136b8687c7d 100644 (file)
@@ -34,12 +34,8 @@ struct flt_ops comp_req_ops;
 struct flt_ops comp_res_ops;
 
 struct comp_state {
-       /*
-        * For both comp_ctx and comp_algo, COMP_DIR_REQ is the index
-        * for requests, and COMP_DIR_RES for responses
-        */
-       struct comp_ctx  *comp_ctx[2];   /* compression context */
-       struct comp_algo *comp_algo[2];  /* compression algorithm if not NULL */
+       struct comp_ctx  *comp_ctx;   /* compression context */
+       struct comp_algo *comp_algo;  /* compression algorithm if not NULL */
        unsigned int      flags;      /* COMP_STATE_* */
 };
 
@@ -78,10 +74,8 @@ comp_strm_init(struct stream *s, struct filter *filter)
        if (st == NULL)
                return -1;
 
-       st->comp_algo[COMP_DIR_REQ] = NULL;
-       st->comp_algo[COMP_DIR_RES] = NULL;
-       st->comp_ctx[COMP_DIR_REQ]  = NULL;
-       st->comp_ctx[COMP_DIR_RES] = NULL;
+       st->comp_algo = NULL;
+       st->comp_ctx = NULL;
        st->flags     = 0;
        filter->ctx   = st;
 
@@ -102,10 +96,8 @@ comp_strm_deinit(struct stream *s, struct filter *filter)
                return;
 
        /* release any possible compression context */
-       if (st->comp_algo[COMP_DIR_REQ])
-               st->comp_algo[COMP_DIR_REQ]->end(&st->comp_ctx[COMP_DIR_REQ]);
-       if (st->comp_algo[COMP_DIR_RES])
-               st->comp_algo[COMP_DIR_RES]->end(&st->comp_ctx[COMP_DIR_RES]);
+       if (st->comp_algo)
+               st->comp_algo->end(&st->comp_ctx);
        pool_free(pool_head_comp_state, st);
        filter->ctx = NULL;
 }
@@ -174,9 +166,9 @@ comp_prepare_compress_request(struct comp_state *st, struct stream *s, struct ht
        if (txn->meth == HTTP_METH_HEAD)
                return;
        if (s->be->comp && s->be->comp->algo_req != NULL)
-               st->comp_algo[COMP_DIR_REQ] = s->be->comp->algo_req;
+               st->comp_algo = s->be->comp->algo_req;
        else if (strm_fe(s)->comp && strm_fe(s)->comp->algo_req != NULL)
-               st->comp_algo[COMP_DIR_REQ] = strm_fe(s)->comp->algo_req;
+               st->comp_algo = strm_fe(s)->comp->algo_req;
        else
                goto fail; /* no algo selected: nothing to do */
 
@@ -191,12 +183,12 @@ comp_prepare_compress_request(struct comp_state *st, struct stream *s, struct ht
                goto fail;
 
        /* initialize compression */
-       if (st->comp_algo[COMP_DIR_REQ]->init(&st->comp_ctx[COMP_DIR_REQ], global.tune.comp_maxlevel) < 0)
+       if (st->comp_algo->init(&st->comp_ctx, global.tune.comp_maxlevel) < 0)
                goto fail;
 
        return;
 fail:
-       st->comp_algo[COMP_DIR_REQ] = NULL;
+       st->comp_algo = NULL;
 }
 
 static int
@@ -218,7 +210,7 @@ comp_req_http_headers(struct stream *s, struct filter *filter, struct http_msg *
 
        if (!(msg->chn->flags & CF_ISRESP)) {
                comp_prepare_compress_request(st, s, msg);
-               if (st->comp_algo[COMP_DIR_REQ]) {
+               if (st->comp_algo) {
                        if (!set_compression_header(st, s, msg))
                                goto end;
                        register_data_filter(s, msg->chn, filter);
@@ -253,7 +245,7 @@ comp_res_http_headers(struct stream *s, struct filter *filter, struct http_msg *
        else {
                /* Response headers have already been checked in
                 * comp_res_http_post_analyze callback. */
-               if (st->comp_algo[COMP_DIR_RES]) {
+               if (st->comp_algo) {
                        if (!set_compression_header(st, s, msg))
                                goto end;
                        register_data_filter(s, msg->chn, filter);
@@ -383,7 +375,7 @@ comp_http_payload(struct stream *s, struct filter *filter, struct http_msg *msg,
        if (to_forward != consumed)
                flt_update_offsets(filter, msg->chn, to_forward - consumed);
 
-       if (st->comp_ctx[dir] && st->comp_ctx[dir]->cur_lvl > 0) {
+       if (st->comp_ctx && st->comp_ctx->cur_lvl > 0) {
                update_freq_ctr(&global.comp_bps_in, consumed);
                if (s->sess->fe_tgcounters) {
                        _HA_ATOMIC_ADD(&s->sess->fe_tgcounters->comp_in[dir], consumed);
@@ -432,7 +424,7 @@ comp_res_http_end(struct stream *s, struct filter *filter,
 {
        struct comp_state *st = filter->ctx;
 
-       if (!(msg->chn->flags & CF_ISRESP) || !st || !st->comp_algo[COMP_DIR_RES])
+       if (!(msg->chn->flags & CF_ISRESP) || !st || !st->comp_algo)
                goto end;
 
        if (strm_fe(s)->mode == PR_MODE_HTTP && s->sess->fe_tgcounters)
@@ -452,18 +444,12 @@ set_compression_header(struct comp_state *st, struct stream *s, struct http_msg
        struct htx_sl *sl;
        struct http_hdr_ctx ctx, last_vary;
        struct comp_algo *comp_algo;
-       int comp_index;
-
-       if (msg->chn->flags & CF_ISRESP)
-               comp_index = COMP_DIR_RES;
-       else
-               comp_index = COMP_DIR_REQ;
 
        sl = http_get_stline(htx);
        if (!sl)
                goto error;
 
-       comp_algo = st->comp_algo[comp_index];
+       comp_algo = st->comp_algo;
 
        /* add "Transfer-Encoding: chunked" header */
        if (!(msg->flags & HTTP_MSGF_TE_CHNK)) {
@@ -537,8 +523,8 @@ set_compression_header(struct comp_state *st, struct stream *s, struct http_msg
        return 1;
 
   error:
-       st->comp_algo[comp_index]->end(&st->comp_ctx[comp_index]);
-       st->comp_algo[comp_index] = NULL;
+       st->comp_algo->end(&st->comp_ctx);
+       st->comp_algo = NULL;
        return 0;
 }
 
@@ -566,7 +552,7 @@ select_compression_request_header(struct comp_state *st, struct stream *s, struc
             *(ctx.value.ptr + 30) < '6' ||
             (*(ctx.value.ptr + 30) == '6' &&
              (ctx.value.len < 54 || memcmp(ctx.value.ptr + 51, "SV1", 3) != 0)))) {
-               st->comp_algo[COMP_DIR_RES] = NULL;
+               st->comp_algo = NULL;
                return 0;
        }
 
@@ -620,7 +606,7 @@ select_compression_request_header(struct comp_state *st, struct stream *s, struc
                        for (comp_algo = comp_algo_back; comp_algo; comp_algo = comp_algo->next) {
                                if (*(ctx.value.ptr) == '*' ||
                                    word_match(ctx.value.ptr, toklen, comp_algo->ua_name, comp_algo->ua_name_len)) {
-                                       st->comp_algo[COMP_DIR_RES] = comp_algo;
+                                       st->comp_algo = comp_algo;
                                        best_q = q;
                                        break;
                                }
@@ -629,7 +615,7 @@ select_compression_request_header(struct comp_state *st, struct stream *s, struc
        }
 
        /* remove all occurrences of the header when "compression offload" is set */
-       if (st->comp_algo[COMP_DIR_RES]) {
+       if (st->comp_algo) {
                if ((s->be->comp && (s->be->comp->flags & COMP_FL_OFFLOAD)) ||
                    (strm_fe(s)->comp && (strm_fe(s)->comp->flags & COMP_FL_OFFLOAD))) {
                        http_remove_header(htx, &ctx);
@@ -645,13 +631,13 @@ select_compression_request_header(struct comp_state *st, struct stream *s, struc
            (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;
+                               st->comp_algo = comp_algo;
                                return 1;
                        }
                }
        }
 
-       st->comp_algo[COMP_DIR_RES] = NULL;
+       st->comp_algo = NULL;
        return 0;
 }
 
@@ -668,7 +654,7 @@ select_compression_response_header(struct comp_state *st, struct stream *s, stru
        unsigned int comp_minsize = 0;
 
        /* no common compression algorithm was found in request header */
-       if (st->comp_algo[COMP_DIR_RES] == NULL)
+       if (st->comp_algo == NULL)
                goto fail;
 
        /* compression already in progress */
@@ -766,13 +752,13 @@ select_compression_response_header(struct comp_state *st, struct stream *s, stru
                goto fail;
 
        /* initialize compression */
-       if (st->comp_algo[COMP_DIR_RES]->init(&st->comp_ctx[COMP_DIR_RES], global.tune.comp_maxlevel) < 0)
+       if (st->comp_algo->init(&st->comp_ctx, global.tune.comp_maxlevel) < 0)
                goto fail;
        msg->flags |= HTTP_MSGF_COMPRESSING;
        return 1;
 
   fail:
-       st->comp_algo[COMP_DIR_RES] = NULL;
+       st->comp_algo = NULL;
        return 0;
 }
 
@@ -795,7 +781,7 @@ htx_compression_buffer_add_data(struct comp_state *st, const char *data, size_t
                                struct buffer *out, int dir)
 {
 
-       return st->comp_algo[dir]->add_data(st->comp_ctx[dir], data, len, out);
+       return st->comp_algo->add_data(st->comp_ctx, data, len, out);
 }
 
 static int
@@ -803,9 +789,9 @@ htx_compression_buffer_end(struct comp_state *st, struct buffer *out, int end, i
 {
 
        if (end)
-               return st->comp_algo[dir]->finish(st->comp_ctx[dir], out);
+               return st->comp_algo->finish(st->comp_ctx, out);
        else
-               return st->comp_algo[dir]->flush(st->comp_ctx[dir], out);
+               return st->comp_algo->flush(st->comp_ctx, out);
 }
 
 
@@ -1237,8 +1223,8 @@ smp_fetch_res_comp_algo(const struct arg *args, struct sample *smp,
 
                smp->data.type = SMP_T_STR;
                smp->flags = SMP_F_CONST;
-               smp->data.u.str.area = st->comp_algo[COMP_DIR_RES]->cfg_name;
-               smp->data.u.str.data = st->comp_algo[COMP_DIR_RES]->cfg_name_len;
+               smp->data.u.str.area = st->comp_algo->cfg_name;
+               smp->data.u.str.data = st->comp_algo->cfg_name_len;
                return 1;
        }
        return 0;