blob: 31c8e2171c3fe2914bb40179d4dbfdcf15e32c01 [file] [log] [blame]
Christopher Faulet3d97c902015-12-09 14:59:38 +01001/*
2 * Stream filters related variables and functions.
3 *
4 * Copyright (C) 2015 Qualys Inc., Christopher Faulet <cfaulet@qualys.com>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
Willy Tarreau4c7e4b72020-05-27 12:58:42 +020013#include <haproxy/api.h>
Willy Tarreau6be78492020-06-05 00:00:29 +020014#include <haproxy/cfgparse.h>
Willy Tarreau0a3bd392020-06-04 08:52:38 +020015#include <haproxy/compression.h>
Willy Tarreau2741c8c2020-06-02 11:28:02 +020016#include <haproxy/dynbuf.h>
Willy Tarreauc7babd82020-06-04 21:29:29 +020017#include <haproxy/filters.h>
Willy Tarreaucd72d8c2020-06-02 19:11:26 +020018#include <haproxy/http.h>
Willy Tarreauc2b1ff02020-06-04 21:21:03 +020019#include <haproxy/http_ana-t.h>
Willy Tarreau87735332020-06-04 09:08:41 +020020#include <haproxy/http_htx.h>
Willy Tarreau16f958c2020-06-03 08:44:35 +020021#include <haproxy/htx.h>
Willy Tarreau853b2972020-05-27 18:01:47 +020022#include <haproxy/list.h>
Willy Tarreau202f93d2021-05-08 20:34:16 +020023#include <haproxy/proxy.h>
Willy Tarreaue6ce10b2020-06-04 15:33:47 +020024#include <haproxy/sample.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020025#include <haproxy/stream.h>
Willy Tarreau48fbcae2020-06-03 18:09:46 +020026#include <haproxy/tools.h>
Christopher Faulet3d97c902015-12-09 14:59:38 +010027
Christopher Faulet12554d02021-06-09 17:12:44 +020028#define COMP_STATE_PROCESSING 0x01
29
Christopher Fauletf4a4ef72018-12-07 17:39:53 +010030const char *http_comp_flt_id = "compression filter";
Christopher Faulet92d36382015-11-05 13:35:03 +010031
32struct flt_ops comp_ops;
33
Christopher Faulet92d36382015-11-05 13:35:03 +010034struct comp_state {
Olivier Houcharddfc11da2023-04-05 16:25:57 +020035 /*
36 * For both comp_ctx and comp_algo, COMP_DIR_REQ is the index
37 * for requests, and COMP_DIR_RES for responses
38 */
39 struct comp_ctx *comp_ctx[2]; /* compression context */
40 struct comp_algo *comp_algo[2]; /* compression algorithm if not NULL */
Christopher Faulet12554d02021-06-09 17:12:44 +020041 unsigned int flags; /* COMP_STATE_* */
Christopher Faulet92d36382015-11-05 13:35:03 +010042};
43
Willy Tarreau8ceae722018-11-26 11:58:30 +010044/* Pools used to allocate comp_state structs */
45DECLARE_STATIC_POOL(pool_head_comp_state, "comp_state", sizeof(struct comp_state));
46
47static THREAD_LOCAL struct buffer tmpbuf;
48static THREAD_LOCAL struct buffer zbuf;
Willy Tarreau8ceae722018-11-26 11:58:30 +010049
Christopher Faulet92d36382015-11-05 13:35:03 +010050static int select_compression_request_header(struct comp_state *st,
51 struct stream *s,
52 struct http_msg *msg);
53static int select_compression_response_header(struct comp_state *st,
54 struct stream *s,
55 struct http_msg *msg);
Olivier Houcharddfc11da2023-04-05 16:25:57 +020056static int set_compression_header(struct comp_state *st,
57 struct stream *s,
58 struct http_msg *msg);
Christopher Faulet92d36382015-11-05 13:35:03 +010059
Christopher Faulete6902cd2018-11-30 22:29:48 +010060static int htx_compression_buffer_init(struct htx *htx, struct buffer *out);
61static int htx_compression_buffer_add_data(struct comp_state *st, const char *data, size_t len,
Olivier Houcharddfc11da2023-04-05 16:25:57 +020062 struct buffer *out, int dir);
63static int htx_compression_buffer_end(struct comp_state *st, struct buffer *out, int end, int dir);
Christopher Faulete6902cd2018-11-30 22:29:48 +010064
Christopher Faulet92d36382015-11-05 13:35:03 +010065/***********************************************************************/
66static int
Christopher Faulete6902cd2018-11-30 22:29:48 +010067comp_flt_init(struct proxy *px, struct flt_conf *fconf)
68{
Christopher Faulet6e540952018-12-03 22:43:41 +010069 fconf->flags |= FLT_CFG_FL_HTX;
Christopher Faulete6902cd2018-11-30 22:29:48 +010070 return 0;
71}
72
73static int
Christopher Faulet8ca3b4b2017-07-25 11:07:15 +020074comp_flt_init_per_thread(struct proxy *px, struct flt_conf *fconf)
Christopher Faulet92d36382015-11-05 13:35:03 +010075{
Willy Tarreau862ad822021-03-22 16:16:22 +010076 if (b_alloc(&tmpbuf) == NULL)
Christopher Fauletb77c5c22015-12-07 16:48:42 +010077 return -1;
Willy Tarreau862ad822021-03-22 16:16:22 +010078 if (b_alloc(&zbuf) == NULL)
Christopher Fauletb77c5c22015-12-07 16:48:42 +010079 return -1;
Christopher Faulet92d36382015-11-05 13:35:03 +010080 return 0;
81}
82
83static void
Christopher Faulet8ca3b4b2017-07-25 11:07:15 +020084comp_flt_deinit_per_thread(struct proxy *px, struct flt_conf *fconf)
Christopher Faulet92d36382015-11-05 13:35:03 +010085{
Willy Tarreauc9fa0482018-07-10 17:43:27 +020086 if (tmpbuf.size)
Christopher Faulet92d36382015-11-05 13:35:03 +010087 b_free(&tmpbuf);
Willy Tarreauc9fa0482018-07-10 17:43:27 +020088 if (zbuf.size)
Christopher Fauletb77c5c22015-12-07 16:48:42 +010089 b_free(&zbuf);
Christopher Faulet92d36382015-11-05 13:35:03 +010090}
91
92static int
Christopher Faulet5e896512020-03-06 14:59:05 +010093comp_strm_init(struct stream *s, struct filter *filter)
Christopher Faulet92d36382015-11-05 13:35:03 +010094{
Christopher Faulet5e896512020-03-06 14:59:05 +010095 struct comp_state *st;
Christopher Faulet8ca3b4b2017-07-25 11:07:15 +020096
Willy Tarreau5bfeb212021-03-22 15:08:17 +010097 st = pool_alloc(pool_head_comp_state);
Christopher Faulet5e896512020-03-06 14:59:05 +010098 if (st == NULL)
99 return -1;
Christopher Faulet92d36382015-11-05 13:35:03 +0100100
Olivier Houcharddfc11da2023-04-05 16:25:57 +0200101 st->comp_algo[COMP_DIR_REQ] = NULL;
102 st->comp_algo[COMP_DIR_RES] = NULL;
103 st->comp_ctx[COMP_DIR_REQ] = NULL;
104 st->comp_ctx[COMP_DIR_RES] = NULL;
Christopher Faulet12554d02021-06-09 17:12:44 +0200105 st->flags = 0;
Christopher Faulet5e896512020-03-06 14:59:05 +0100106 filter->ctx = st;
Christopher Faulet3dc860d2017-09-15 11:39:36 +0200107
Christopher Faulet5e896512020-03-06 14:59:05 +0100108 /* Register post-analyzer on AN_RES_WAIT_HTTP because we need to
109 * analyze response headers before http-response rules execution
110 * to be sure we can use res.comp and res.comp_algo sample
111 * fetches */
112 filter->post_analyzers |= AN_RES_WAIT_HTTP;
Christopher Faulet92d36382015-11-05 13:35:03 +0100113 return 1;
114}
115
Christopher Faulet5e896512020-03-06 14:59:05 +0100116static void
117comp_strm_deinit(struct stream *s, struct filter *filter)
Christopher Faulet92d36382015-11-05 13:35:03 +0100118{
119 struct comp_state *st = filter->ctx;
Christopher Faulet92d36382015-11-05 13:35:03 +0100120
Christopher Fauletd60b3cf2017-06-26 11:47:13 +0200121 if (!st)
Christopher Faulet5e896512020-03-06 14:59:05 +0100122 return;
Christopher Faulet92d36382015-11-05 13:35:03 +0100123
Christopher Faulet92d36382015-11-05 13:35:03 +0100124 /* release any possible compression context */
Olivier Houcharddfc11da2023-04-05 16:25:57 +0200125 if (st->comp_algo[COMP_DIR_REQ])
126 st->comp_algo[COMP_DIR_REQ]->end(&st->comp_ctx[COMP_DIR_REQ]);
127 if (st->comp_algo[COMP_DIR_RES])
128 st->comp_algo[COMP_DIR_RES]->end(&st->comp_ctx[COMP_DIR_RES]);
Willy Tarreaubafbe012017-11-24 17:34:44 +0100129 pool_free(pool_head_comp_state, st);
Christopher Faulet92d36382015-11-05 13:35:03 +0100130 filter->ctx = NULL;
Christopher Faulet92d36382015-11-05 13:35:03 +0100131}
132
133static int
Christopher Faulet1339d742016-05-11 16:48:33 +0200134comp_http_headers(struct stream *s, struct filter *filter, struct http_msg *msg)
135{
136 struct comp_state *st = filter->ctx;
137
138 if (!strm_fe(s)->comp && !s->be->comp)
139 goto end;
140
141 if (!(msg->chn->flags & CF_ISRESP))
142 select_compression_request_header(st, s, msg);
143 else {
Christopher Faulet3dc860d2017-09-15 11:39:36 +0200144 /* Response headers have already been checked in
145 * comp_http_post_analyze callback. */
Olivier Houcharddfc11da2023-04-05 16:25:57 +0200146 if (st->comp_algo[COMP_DIR_RES]) {
147 if (!set_compression_header(st, s, msg))
Christopher Faulet27d93c32018-12-15 22:32:02 +0100148 goto end;
Christopher Faulet1339d742016-05-11 16:48:33 +0200149 register_data_filter(s, msg->chn, filter);
Christopher Faulet12554d02021-06-09 17:12:44 +0200150 st->flags |= COMP_STATE_PROCESSING;
Christopher Faulet1339d742016-05-11 16:48:33 +0200151 }
152 }
153
154 end:
155 return 1;
156}
157
158static int
Christopher Faulet3dc860d2017-09-15 11:39:36 +0200159comp_http_post_analyze(struct stream *s, struct filter *filter,
160 struct channel *chn, unsigned an_bit)
161{
162 struct http_txn *txn = s->txn;
163 struct http_msg *msg = &txn->rsp;
164 struct comp_state *st = filter->ctx;
165
166 if (an_bit != AN_RES_WAIT_HTTP)
167 goto end;
168
169 if (!strm_fe(s)->comp && !s->be->comp)
170 goto end;
171
172 select_compression_response_header(st, s, msg);
173
174 end:
175 return 1;
176}
177
178static int
Christopher Faulete6902cd2018-11-30 22:29:48 +0100179comp_http_payload(struct stream *s, struct filter *filter, struct http_msg *msg,
180 unsigned int offset, unsigned int len)
181{
182 struct comp_state *st = filter->ctx;
Christopher Faulet27ba2dc2018-12-05 11:53:24 +0100183 struct htx *htx = htxbuf(&msg->chn->buf);
Christopher Faulete6a62bf2020-03-02 16:20:05 +0100184 struct htx_ret htxret = htx_find_offset(htx, offset);
Christopher Fauletd1ac2b92020-12-02 19:12:22 +0100185 struct htx_blk *blk, *next;
186 int ret, consumed = 0, to_forward = 0, last = 0;
Olivier Houcharddfc11da2023-04-05 16:25:57 +0200187 int dir;
188
189 if (msg->chn->flags & CF_ISRESP)
190 dir = COMP_DIR_RES;
191 else
192 dir = COMP_DIR_REQ;
Christopher Faulete6902cd2018-11-30 22:29:48 +0100193
Christopher Faulete6a62bf2020-03-02 16:20:05 +0100194 blk = htxret.blk;
195 offset = htxret.ret;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +0100196 for (next = NULL; blk && len; blk = next) {
Christopher Faulete6902cd2018-11-30 22:29:48 +0100197 enum htx_blk_type type = htx_get_blk_type(blk);
198 uint32_t sz = htx_get_blksz(blk);
199 struct ist v;
200
Christopher Fauletd1ac2b92020-12-02 19:12:22 +0100201 next = htx_get_next_blk(htx, blk);
202 while (next && htx_get_blk_type(next) == HTX_BLK_UNUSED)
Christopher Faulet86ca0e52021-06-09 16:59:02 +0200203 next = htx_get_next_blk(htx, next);
Christopher Faulete6902cd2018-11-30 22:29:48 +0100204
Christopher Faulet12554d02021-06-09 17:12:44 +0200205 if (!(st->flags & COMP_STATE_PROCESSING))
206 goto consume;
207
Christopher Fauletd1ac2b92020-12-02 19:12:22 +0100208 if (htx_compression_buffer_init(htx, &trash) < 0) {
209 msg->chn->flags |= CF_WAKE_WRITE;
210 goto end;
211 }
212
213 switch (type) {
Christopher Faulete6902cd2018-11-30 22:29:48 +0100214 case HTX_BLK_DATA:
Christopher Fauletd1ac2b92020-12-02 19:12:22 +0100215 /* it is the last data block */
216 last = ((!next && (htx->flags & HTX_FL_EOM)) || (next && htx_get_blk_type(next) != HTX_BLK_DATA));
Christopher Faulete6902cd2018-11-30 22:29:48 +0100217 v = htx_get_blk_value(htx, blk);
Christopher Fauletd1ac2b92020-12-02 19:12:22 +0100218 v = istadv(v, offset);
219 if (v.len > len) {
220 last = 0;
Christopher Faulete6902cd2018-11-30 22:29:48 +0100221 v.len = len;
Christopher Faulete6902cd2018-11-30 22:29:48 +0100222 }
Christopher Fauletd1ac2b92020-12-02 19:12:22 +0100223
Olivier Houcharddfc11da2023-04-05 16:25:57 +0200224 ret = htx_compression_buffer_add_data(st, v.ptr, v.len, &trash, dir);
225 if (ret < 0 || htx_compression_buffer_end(st, &trash, last, dir) < 0)
Christopher Faulete6902cd2018-11-30 22:29:48 +0100226 goto error;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +0100227 BUG_ON(v.len != ret);
228
229 if (ret == sz && !b_data(&trash))
230 next = htx_remove_blk(htx, blk);
Christopher Faulet402740c2021-06-09 17:04:37 +0200231 else {
Christopher Fauletd1ac2b92020-12-02 19:12:22 +0100232 blk = htx_replace_blk_value(htx, blk, v, ist2(b_head(&trash), b_data(&trash)));
Christopher Faulet402740c2021-06-09 17:04:37 +0200233 next = htx_get_next_blk(htx, blk);
234 }
Christopher Fauletd1ac2b92020-12-02 19:12:22 +0100235
Christopher Faulete6902cd2018-11-30 22:29:48 +0100236 len -= ret;
237 consumed += ret;
238 to_forward += b_data(&trash);
Christopher Faulet12554d02021-06-09 17:12:44 +0200239 if (last)
240 st->flags &= ~COMP_STATE_PROCESSING;
Christopher Faulete6902cd2018-11-30 22:29:48 +0100241 break;
242
Christopher Faulete6902cd2018-11-30 22:29:48 +0100243 case HTX_BLK_TLR:
Christopher Faulet2d7c5392019-06-03 10:41:26 +0200244 case HTX_BLK_EOT:
Olivier Houcharddfc11da2023-04-05 16:25:57 +0200245 if (htx_compression_buffer_end(st, &trash, 1, dir) < 0)
Christopher Fauletd1ac2b92020-12-02 19:12:22 +0100246 goto error;
247 if (b_data(&trash)) {
248 struct htx_blk *last = htx_add_last_data(htx, ist2(b_head(&trash), b_data(&trash)));
249 if (!last)
Christopher Faulete6902cd2018-11-30 22:29:48 +0100250 goto error;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +0100251 blk = htx_get_next_blk(htx, last);
252 if (!blk)
253 goto error;
Christopher Faulet402740c2021-06-09 17:04:37 +0200254 next = htx_get_next_blk(htx, blk);
Christopher Fauletd1ac2b92020-12-02 19:12:22 +0100255 to_forward += b_data(&trash);
Christopher Faulete6902cd2018-11-30 22:29:48 +0100256 }
Christopher Faulet12554d02021-06-09 17:12:44 +0200257 st->flags &= ~COMP_STATE_PROCESSING;
Willy Tarreau91d398c2022-11-14 07:36:05 +0100258 __fallthrough;
Christopher Faulete6902cd2018-11-30 22:29:48 +0100259
260 default:
Christopher Fauletd1ac2b92020-12-02 19:12:22 +0100261 consume:
Christopher Faulete6902cd2018-11-30 22:29:48 +0100262 sz -= offset;
263 if (sz > len)
264 sz = len;
265 consumed += sz;
266 to_forward += sz;
267 len -= sz;
268 break;
269 }
270
271 offset = 0;
Christopher Faulete6902cd2018-11-30 22:29:48 +0100272 }
273
274 end:
275 if (to_forward != consumed)
276 flt_update_offsets(filter, msg->chn, to_forward - consumed);
277
Olivier Houcharddfc11da2023-04-05 16:25:57 +0200278 if (st->comp_ctx[dir] && st->comp_ctx[dir]->cur_lvl > 0) {
Willy Tarreauef6fd852019-02-04 11:48:03 +0100279 update_freq_ctr(&global.comp_bps_in, consumed);
Olivier Houcharddea25f52023-04-06 00:33:01 +0200280 _HA_ATOMIC_ADD(&strm_fe(s)->fe_counters.comp_in[dir], consumed);
281 _HA_ATOMIC_ADD(&s->be->be_counters.comp_in[dir], consumed);
Christopher Faulete6902cd2018-11-30 22:29:48 +0100282 update_freq_ctr(&global.comp_bps_out, to_forward);
Olivier Houcharddea25f52023-04-06 00:33:01 +0200283 _HA_ATOMIC_ADD(&strm_fe(s)->fe_counters.comp_out[dir], to_forward);
284 _HA_ATOMIC_ADD(&s->be->be_counters.comp_out[dir], to_forward);
Willy Tarreauef6fd852019-02-04 11:48:03 +0100285 } else {
Olivier Houcharddea25f52023-04-06 00:33:01 +0200286 _HA_ATOMIC_ADD(&strm_fe(s)->fe_counters.comp_byp[dir], consumed);
287 _HA_ATOMIC_ADD(&s->be->be_counters.comp_byp[dir], consumed);
Christopher Faulete6902cd2018-11-30 22:29:48 +0100288 }
289 return to_forward;
290
291 error:
292 return -1;
293}
294
Christopher Faulet2fb28802015-12-01 10:40:57 +0100295
Christopher Faulet92d36382015-11-05 13:35:03 +0100296static int
Christopher Fauletd60b3cf2017-06-26 11:47:13 +0200297comp_http_end(struct stream *s, struct filter *filter,
298 struct http_msg *msg)
299{
300 struct comp_state *st = filter->ctx;
301
Olivier Houcharddfc11da2023-04-05 16:25:57 +0200302 if (!(msg->chn->flags & CF_ISRESP) || !st || !st->comp_algo[COMP_DIR_RES])
Christopher Fauletd60b3cf2017-06-26 11:47:13 +0200303 goto end;
304
305 if (strm_fe(s)->mode == PR_MODE_HTTP)
Willy Tarreau4781b152021-04-06 13:53:36 +0200306 _HA_ATOMIC_INC(&strm_fe(s)->fe_counters.p.http.comp_rsp);
Christopher Fauletd60b3cf2017-06-26 11:47:13 +0200307 if ((s->flags & SF_BE_ASSIGNED) && (s->be->mode == PR_MODE_HTTP))
Willy Tarreau4781b152021-04-06 13:53:36 +0200308 _HA_ATOMIC_INC(&s->be->be_counters.p.http.comp_rsp);
Christopher Fauletd60b3cf2017-06-26 11:47:13 +0200309 end:
310 return 1;
311}
Christopher Faulet27d93c32018-12-15 22:32:02 +0100312
Christopher Faulet89f2b162019-07-15 21:16:04 +0200313/***********************************************************************/
Christopher Faulet27d93c32018-12-15 22:32:02 +0100314static int
Olivier Houcharddfc11da2023-04-05 16:25:57 +0200315set_compression_header(struct comp_state *st, struct stream *s, struct http_msg *msg)
Christopher Faulet27d93c32018-12-15 22:32:02 +0100316{
317 struct htx *htx = htxbuf(&msg->chn->buf);
Christopher Faulet39e436e2022-04-15 15:32:03 +0200318 struct htx_sl *sl;
Tim Duesterhusb229f012019-01-29 16:38:56 +0100319 struct http_hdr_ctx ctx;
Olivier Houcharddfc11da2023-04-05 16:25:57 +0200320 struct comp_algo *comp_algo;
321 int comp_index;
322
323 if (msg->chn->flags & CF_ISRESP)
324 comp_index = COMP_DIR_RES;
325 else
326 comp_index = COMP_DIR_REQ;
Christopher Faulet27d93c32018-12-15 22:32:02 +0100327
Christopher Faulet39e436e2022-04-15 15:32:03 +0200328 sl = http_get_stline(htx);
329 if (!sl)
330 goto error;
331
Olivier Houcharddfc11da2023-04-05 16:25:57 +0200332 comp_algo = st->comp_algo[comp_index];
333
Christopher Faulet910b7572022-10-24 08:39:29 +0200334 /* add "Transfer-Encoding: chunked" header */
335 if (!(msg->flags & HTTP_MSGF_TE_CHNK)) {
336 if (!http_add_header(htx, ist("Transfer-Encoding"), ist("chunked")))
337 goto error;
338 msg->flags |= HTTP_MSGF_TE_CHNK;
339 sl->flags |= (HTX_SL_F_XFER_ENC|HTX_SL_F_CHNK);
340 }
341
Christopher Faulet27d93c32018-12-15 22:32:02 +0100342 /* remove Content-Length header */
343 if (msg->flags & HTTP_MSGF_CNT_LEN) {
Christopher Faulet27d93c32018-12-15 22:32:02 +0100344 ctx.blk = NULL;
345 while (http_find_header(htx, ist("Content-Length"), &ctx, 1))
346 http_remove_header(htx, &ctx);
Christopher Faulet39e436e2022-04-15 15:32:03 +0200347 msg->flags &= ~HTTP_MSGF_CNT_LEN;
348 sl->flags &= ~HTX_SL_F_CLEN;
Christopher Faulet27d93c32018-12-15 22:32:02 +0100349 }
350
Tim Duesterhusb229f012019-01-29 16:38:56 +0100351 /* convert "ETag" header to a weak ETag */
352 ctx.blk = NULL;
353 if (http_find_header(htx, ist("ETag"), &ctx, 1)) {
354 if (ctx.value.ptr[0] == '"') {
355 /* This a strong ETag. Convert it to a weak one. */
356 struct ist v = ist2(trash.area, 0);
357 if (istcat(&v, ist("W/"), trash.size) == -1 || istcat(&v, ctx.value, trash.size) == -1)
358 goto error;
359
360 if (!http_replace_header_value(htx, &ctx, v))
361 goto error;
362 }
363 }
364
Tim Duesterhus721d6862019-06-17 16:10:07 +0200365 if (!http_add_header(htx, ist("Vary"), ist("Accept-Encoding")))
366 goto error;
367
Christopher Faulet910b7572022-10-24 08:39:29 +0200368 /*
369 * Add Content-Encoding header when it's not identity encoding.
370 * RFC 2616 : Identity encoding: This content-coding is used only in the
371 * Accept-Encoding header, and SHOULD NOT be used in the Content-Encoding
372 * header.
373 */
Olivier Houcharddfc11da2023-04-05 16:25:57 +0200374 if (comp_algo->cfg_name_len != 8 || memcmp(comp_algo->cfg_name, "identity", 8) != 0) {
375 struct ist v = ist2(comp_algo->ua_name, comp_algo->ua_name_len);
Christopher Faulet910b7572022-10-24 08:39:29 +0200376
377 if (!http_add_header(htx, ist("Content-Encoding"), v))
378 goto error;
379 }
380
Christopher Faulet27d93c32018-12-15 22:32:02 +0100381 return 1;
382
383 error:
Olivier Houcharddfc11da2023-04-05 16:25:57 +0200384 st->comp_algo[comp_index]->end(&st->comp_ctx[comp_index]);
385 st->comp_algo[comp_index] = NULL;
Christopher Faulet27d93c32018-12-15 22:32:02 +0100386 return 0;
387}
388
Christopher Faulet3d97c902015-12-09 14:59:38 +0100389/*
390 * Selects a compression algorithm depending on the client request.
391 */
Christopher Faulete6902cd2018-11-30 22:29:48 +0100392static int
Christopher Faulet89f2b162019-07-15 21:16:04 +0200393select_compression_request_header(struct comp_state *st, struct stream *s, struct http_msg *msg)
Christopher Faulete6902cd2018-11-30 22:29:48 +0100394{
Christopher Faulet27ba2dc2018-12-05 11:53:24 +0100395 struct htx *htx = htxbuf(&msg->chn->buf);
Christopher Faulete6902cd2018-11-30 22:29:48 +0100396 struct http_hdr_ctx ctx;
397 struct comp_algo *comp_algo = NULL;
398 struct comp_algo *comp_algo_back = NULL;
399
400 /* Disable compression for older user agents announcing themselves as "Mozilla/4"
401 * unless they are known good (MSIE 6 with XP SP2, or MSIE 7 and later).
402 * See http://zoompf.com/2012/02/lose-the-wait-http-compression for more details.
403 */
404 ctx.blk = NULL;
405 if (http_find_header(htx, ist("User-Agent"), &ctx, 1) &&
406 ctx.value.len >= 9 &&
407 memcmp(ctx.value.ptr, "Mozilla/4", 9) == 0 &&
408 (ctx.value.len < 31 ||
409 memcmp(ctx.value.ptr + 25, "MSIE ", 5) != 0 ||
410 *(ctx.value.ptr + 30) < '6' ||
411 (*(ctx.value.ptr + 30) == '6' &&
412 (ctx.value.len < 54 || memcmp(ctx.value.ptr + 51, "SV1", 3) != 0)))) {
Olivier Houcharddfc11da2023-04-05 16:25:57 +0200413 st->comp_algo[COMP_DIR_RES] = NULL;
Christopher Faulete6902cd2018-11-30 22:29:48 +0100414 return 0;
415 }
416
417 /* search for the algo in the backend in priority or the frontend */
Olivier Houcharddb573e92023-04-05 17:32:36 +0200418 if ((s->be->comp && (comp_algo_back = s->be->comp->algos_res)) ||
419 (strm_fe(s)->comp && (comp_algo_back = strm_fe(s)->comp->algos_res))) {
Christopher Faulete6902cd2018-11-30 22:29:48 +0100420 int best_q = 0;
421
422 ctx.blk = NULL;
423 while (http_find_header(htx, ist("Accept-Encoding"), &ctx, 0)) {
424 const char *qval;
425 int q;
426 int toklen;
427
428 /* try to isolate the token from the optional q-value */
429 toklen = 0;
430 while (toklen < ctx.value.len && HTTP_IS_TOKEN(*(ctx.value.ptr + toklen)))
431 toklen++;
432
433 qval = ctx.value.ptr + toklen;
434 while (1) {
Tim Duesterhus77508502022-03-15 13:11:06 +0100435 while (qval < istend(ctx.value) && HTTP_IS_LWS(*qval))
Christopher Faulete6902cd2018-11-30 22:29:48 +0100436 qval++;
437
Tim Duesterhus77508502022-03-15 13:11:06 +0100438 if (qval >= istend(ctx.value) || *qval != ';') {
Christopher Faulete6902cd2018-11-30 22:29:48 +0100439 qval = NULL;
440 break;
441 }
442 qval++;
443
Tim Duesterhus77508502022-03-15 13:11:06 +0100444 while (qval < istend(ctx.value) && HTTP_IS_LWS(*qval))
Christopher Faulete6902cd2018-11-30 22:29:48 +0100445 qval++;
446
Tim Duesterhus77508502022-03-15 13:11:06 +0100447 if (qval >= istend(ctx.value)) {
Christopher Faulete6902cd2018-11-30 22:29:48 +0100448 qval = NULL;
449 break;
450 }
Tim Duesterhus77508502022-03-15 13:11:06 +0100451 if (strncmp(qval, "q=", MIN(istend(ctx.value) - qval, 2)) == 0)
Christopher Faulete6902cd2018-11-30 22:29:48 +0100452 break;
453
Tim Duesterhus77508502022-03-15 13:11:06 +0100454 while (qval < istend(ctx.value) && *qval != ';')
Christopher Faulete6902cd2018-11-30 22:29:48 +0100455 qval++;
456 }
457
458 /* here we have qval pointing to the first "q=" attribute or NULL if not found */
459 q = qval ? http_parse_qvalue(qval + 2, NULL) : 1000;
460
461 if (q <= best_q)
462 continue;
463
464 for (comp_algo = comp_algo_back; comp_algo; comp_algo = comp_algo->next) {
465 if (*(ctx.value.ptr) == '*' ||
466 word_match(ctx.value.ptr, toklen, comp_algo->ua_name, comp_algo->ua_name_len)) {
Olivier Houcharddfc11da2023-04-05 16:25:57 +0200467 st->comp_algo[COMP_DIR_RES] = comp_algo;
Christopher Faulete6902cd2018-11-30 22:29:48 +0100468 best_q = q;
469 break;
470 }
471 }
472 }
473 }
474
475 /* remove all occurrences of the header when "compression offload" is set */
Olivier Houcharddfc11da2023-04-05 16:25:57 +0200476 if (st->comp_algo[COMP_DIR_RES]) {
Olivier Houchard3ce0f012023-04-03 22:22:24 +0200477 if ((s->be->comp && (s->be->comp->flags & COMP_FL_OFFLOAD)) ||
478 (strm_fe(s)->comp && (strm_fe(s)->comp->flags & COMP_FL_OFFLOAD))) {
Christopher Faulete6902cd2018-11-30 22:29:48 +0100479 http_remove_header(htx, &ctx);
480 ctx.blk = NULL;
481 while (http_find_header(htx, ist("Accept-Encoding"), &ctx, 1))
482 http_remove_header(htx, &ctx);
483 }
Christopher Faulet3d97c902015-12-09 14:59:38 +0100484 return 1;
485 }
486
487 /* identity is implicit does not require headers */
Olivier Houcharddb573e92023-04-05 17:32:36 +0200488 if ((s->be->comp && (comp_algo_back = s->be->comp->algos_res)) ||
489 (strm_fe(s)->comp && (comp_algo_back = strm_fe(s)->comp->algos_res))) {
Christopher Faulet3d97c902015-12-09 14:59:38 +0100490 for (comp_algo = comp_algo_back; comp_algo; comp_algo = comp_algo->next) {
491 if (comp_algo->cfg_name_len == 8 && memcmp(comp_algo->cfg_name, "identity", 8) == 0) {
Olivier Houcharddfc11da2023-04-05 16:25:57 +0200492 st->comp_algo[COMP_DIR_RES] = comp_algo;
Christopher Faulet3d97c902015-12-09 14:59:38 +0100493 return 1;
494 }
495 }
496 }
497
Olivier Houcharddfc11da2023-04-05 16:25:57 +0200498 st->comp_algo[COMP_DIR_RES] = NULL;
Christopher Faulet3d97c902015-12-09 14:59:38 +0100499 return 0;
500}
501
502/*
Ilya Shipitsin46a030c2020-07-05 16:36:08 +0500503 * Selects a compression algorithm depending of the server response.
Christopher Faulet3d97c902015-12-09 14:59:38 +0100504 */
Christopher Faulet92d36382015-11-05 13:35:03 +0100505static int
Christopher Faulet89f2b162019-07-15 21:16:04 +0200506select_compression_response_header(struct comp_state *st, struct stream *s, struct http_msg *msg)
Christopher Faulete6902cd2018-11-30 22:29:48 +0100507{
Christopher Faulet27ba2dc2018-12-05 11:53:24 +0100508 struct htx *htx = htxbuf(&msg->chn->buf);
Christopher Faulete6902cd2018-11-30 22:29:48 +0100509 struct http_txn *txn = s->txn;
510 struct http_hdr_ctx ctx;
511 struct comp_type *comp_type;
512
513 /* no common compression algorithm was found in request header */
Olivier Houcharddfc11da2023-04-05 16:25:57 +0200514 if (st->comp_algo[COMP_DIR_RES] == NULL)
Christopher Faulete6902cd2018-11-30 22:29:48 +0100515 goto fail;
516
Christopher Faulet1d3613a2019-01-07 14:41:59 +0100517 /* compression already in progress */
518 if (msg->flags & HTTP_MSGF_COMPRESSING)
519 goto fail;
520
Christopher Faulete6902cd2018-11-30 22:29:48 +0100521 /* HTTP < 1.1 should not be compressed */
522 if (!(msg->flags & HTTP_MSGF_VER_11) || !(txn->req.flags & HTTP_MSGF_VER_11))
523 goto fail;
524
525 if (txn->meth == HTTP_METH_HEAD)
526 goto fail;
527
528 /* compress 200,201,202,203 responses only */
529 if ((txn->status != 200) &&
530 (txn->status != 201) &&
531 (txn->status != 202) &&
532 (txn->status != 203))
533 goto fail;
534
Christopher Fauletc963eb22018-12-21 14:53:54 +0100535 if (!(msg->flags & HTTP_MSGF_XFER_LEN) || msg->flags & HTTP_MSGF_BODYLESS)
Christopher Faulete6902cd2018-11-30 22:29:48 +0100536 goto fail;
537
538 /* content is already compressed */
539 ctx.blk = NULL;
540 if (http_find_header(htx, ist("Content-Encoding"), &ctx, 1))
541 goto fail;
542
543 /* no compression when Cache-Control: no-transform is present in the message */
544 ctx.blk = NULL;
545 while (http_find_header(htx, ist("Cache-Control"), &ctx, 0)) {
546 if (word_match(ctx.value.ptr, ctx.value.len, "no-transform", 12))
547 goto fail;
548 }
549
Tim Duesterhusb229f012019-01-29 16:38:56 +0100550 /* no compression when ETag is malformed */
551 ctx.blk = NULL;
552 if (http_find_header(htx, ist("ETag"), &ctx, 1)) {
Tim Duesterhus6414cd12020-09-01 18:32:35 +0200553 if (http_get_etag_type(ctx.value) == ETAG_INVALID)
Tim Duesterhusb229f012019-01-29 16:38:56 +0100554 goto fail;
Tim Duesterhusb229f012019-01-29 16:38:56 +0100555 }
556 /* no compression when multiple ETags are present
557 * Note: Do not reset ctx.blk!
558 */
559 if (http_find_header(htx, ist("ETag"), &ctx, 1))
560 goto fail;
561
Christopher Faulete6902cd2018-11-30 22:29:48 +0100562 comp_type = NULL;
563
564 /* we don't want to compress multipart content-types, nor content-types that are
565 * not listed in the "compression type" directive if any. If no content-type was
566 * found but configuration requires one, we don't compress either. Backend has
567 * the priority.
568 */
569 ctx.blk = NULL;
570 if (http_find_header(htx, ist("Content-Type"), &ctx, 1)) {
571 if (ctx.value.len >= 9 && strncasecmp("multipart", ctx.value.ptr, 9) == 0)
572 goto fail;
573
Olivier Houcharddb573e92023-04-05 17:32:36 +0200574 if ((s->be->comp && (comp_type = s->be->comp->types_res)) ||
575 (strm_fe(s)->comp && (comp_type = strm_fe(s)->comp->types_res))) {
Christopher Faulete6902cd2018-11-30 22:29:48 +0100576 for (; comp_type; comp_type = comp_type->next) {
577 if (ctx.value.len >= comp_type->name_len &&
578 strncasecmp(ctx.value.ptr, comp_type->name, comp_type->name_len) == 0)
579 /* this Content-Type should be compressed */
580 break;
581 }
582 /* this Content-Type should not be compressed */
583 if (comp_type == NULL)
584 goto fail;
585 }
586 }
587 else { /* no content-type header */
Olivier Houcharddb573e92023-04-05 17:32:36 +0200588 if ((s->be->comp && s->be->comp->types_res) ||
589 (strm_fe(s)->comp && strm_fe(s)->comp->types_res))
Christopher Faulete6902cd2018-11-30 22:29:48 +0100590 goto fail; /* a content-type was required */
591 }
592
593 /* limit compression rate */
594 if (global.comp_rate_lim > 0)
595 if (read_freq_ctr(&global.comp_bps_in) > global.comp_rate_lim)
596 goto fail;
597
598 /* limit cpu usage */
Willy Tarreau45c38e22021-09-30 18:28:49 +0200599 if (th_ctx->idle_pct < compress_min_idle)
Christopher Faulete6902cd2018-11-30 22:29:48 +0100600 goto fail;
601
602 /* initialize compression */
Olivier Houcharddfc11da2023-04-05 16:25:57 +0200603 if (st->comp_algo[COMP_DIR_RES]->init(&st->comp_ctx[COMP_DIR_RES], global.tune.comp_maxlevel) < 0)
Christopher Faulete6902cd2018-11-30 22:29:48 +0100604 goto fail;
Christopher Faulete6902cd2018-11-30 22:29:48 +0100605 msg->flags |= HTTP_MSGF_COMPRESSING;
606 return 1;
607
Christopher Faulete6902cd2018-11-30 22:29:48 +0100608 fail:
Olivier Houcharddfc11da2023-04-05 16:25:57 +0200609 st->comp_algo[COMP_DIR_RES] = NULL;
Christopher Faulete6902cd2018-11-30 22:29:48 +0100610 return 0;
611}
612
Christopher Faulet3d97c902015-12-09 14:59:38 +0100613/***********************************************************************/
Christopher Faulete6902cd2018-11-30 22:29:48 +0100614static int
615htx_compression_buffer_init(struct htx *htx, struct buffer *out)
616{
617 /* output stream requires at least 10 bytes for the gzip header, plus
618 * at least 8 bytes for the gzip trailer (crc+len), plus a possible
619 * plus at most 5 bytes per 32kB block and 2 bytes to close the stream.
620 */
621 if (htx_free_space(htx) < 20 + 5 * ((htx->data + 32767) >> 15))
622 return -1;
623 b_reset(out);
624 return 0;
625}
626
Christopher Faulete6902cd2018-11-30 22:29:48 +0100627static int
628htx_compression_buffer_add_data(struct comp_state *st, const char *data, size_t len,
Olivier Houcharddfc11da2023-04-05 16:25:57 +0200629 struct buffer *out, int dir)
Christopher Faulete6902cd2018-11-30 22:29:48 +0100630{
Olivier Houcharddfc11da2023-04-05 16:25:57 +0200631
632 return st->comp_algo[dir]->add_data(st->comp_ctx[dir], data, len, out);
Christopher Faulete6902cd2018-11-30 22:29:48 +0100633}
634
Christopher Faulete6902cd2018-11-30 22:29:48 +0100635static int
Olivier Houcharddfc11da2023-04-05 16:25:57 +0200636htx_compression_buffer_end(struct comp_state *st, struct buffer *out, int end, int dir)
Christopher Faulete6902cd2018-11-30 22:29:48 +0100637{
Olivier Houcharddfc11da2023-04-05 16:25:57 +0200638
Christopher Faulete6902cd2018-11-30 22:29:48 +0100639 if (end)
Olivier Houcharddfc11da2023-04-05 16:25:57 +0200640 return st->comp_algo[dir]->finish(st->comp_ctx[dir], out);
Christopher Faulete6902cd2018-11-30 22:29:48 +0100641 else
Olivier Houcharddfc11da2023-04-05 16:25:57 +0200642 return st->comp_algo[dir]->flush(st->comp_ctx[dir], out);
Christopher Faulete6902cd2018-11-30 22:29:48 +0100643}
644
Christopher Faulet3d97c902015-12-09 14:59:38 +0100645
646/***********************************************************************/
Christopher Faulet92d36382015-11-05 13:35:03 +0100647struct flt_ops comp_ops = {
Christopher Faulete6902cd2018-11-30 22:29:48 +0100648 .init = comp_flt_init,
Christopher Faulet8ca3b4b2017-07-25 11:07:15 +0200649 .init_per_thread = comp_flt_init_per_thread,
650 .deinit_per_thread = comp_flt_deinit_per_thread,
Christopher Faulet92d36382015-11-05 13:35:03 +0100651
Christopher Faulet5e896512020-03-06 14:59:05 +0100652 .attach = comp_strm_init,
653 .detach = comp_strm_deinit,
654
Christopher Faulet3dc860d2017-09-15 11:39:36 +0200655 .channel_post_analyze = comp_http_post_analyze,
Christopher Faulet92d36382015-11-05 13:35:03 +0100656
Christopher Faulet1339d742016-05-11 16:48:33 +0200657 .http_headers = comp_http_headers,
Christopher Faulete6902cd2018-11-30 22:29:48 +0100658 .http_payload = comp_http_payload,
659 .http_end = comp_http_end,
Christopher Faulet92d36382015-11-05 13:35:03 +0100660};
661
Christopher Faulet3d97c902015-12-09 14:59:38 +0100662static int
663parse_compression_options(char **args, int section, struct proxy *proxy,
Willy Tarreau01825162021-03-09 09:53:46 +0100664 const struct proxy *defpx, const char *file, int line,
Christopher Faulet3d97c902015-12-09 14:59:38 +0100665 char **err)
666{
Christopher Faulet92d36382015-11-05 13:35:03 +0100667 struct comp *comp;
Christopher Faulet44d34bf2021-11-05 12:06:14 +0100668 int ret = 0;
Christopher Faulet3d97c902015-12-09 14:59:38 +0100669
670 if (proxy->comp == NULL) {
Vincent Bernat02779b62016-04-03 13:48:43 +0200671 comp = calloc(1, sizeof(*comp));
Christopher Faulet3d97c902015-12-09 14:59:38 +0100672 proxy->comp = comp;
673 }
674 else
675 comp = proxy->comp;
676
Olivier Houcharddb573e92023-04-05 17:32:36 +0200677 if (strcmp(args[1], "algo") == 0 || strcmp(args[1], "algo-res") == 0) {
Christopher Faulet3d97c902015-12-09 14:59:38 +0100678 struct comp_ctx *ctx;
679 int cur_arg = 2;
680
681 if (!*args[cur_arg]) {
Christopher Faulet44d34bf2021-11-05 12:06:14 +0100682 memprintf(err, "parsing [%s:%d] : '%s' expects <algorithm>.",
Christopher Faulet3d97c902015-12-09 14:59:38 +0100683 file, line, args[0]);
Christopher Faulet44d34bf2021-11-05 12:06:14 +0100684 ret = -1;
685 goto end;
Christopher Faulet3d97c902015-12-09 14:59:38 +0100686 }
687 while (*(args[cur_arg])) {
Olivier Houcharddb573e92023-04-05 17:32:36 +0200688 int retval = comp_append_algo(&comp->algos_res, args[cur_arg]);
Remi Tricot-Le Breton6443bcc2021-05-17 10:35:08 +0200689 if (retval) {
690 if (retval < 0)
Christopher Faulet44d34bf2021-11-05 12:06:14 +0100691 memprintf(err, "'%s' : '%s' is not a supported algorithm.",
Remi Tricot-Le Breton6443bcc2021-05-17 10:35:08 +0200692 args[0], args[cur_arg]);
693 else
Christopher Faulet44d34bf2021-11-05 12:06:14 +0100694 memprintf(err, "'%s' : out of memory while parsing algo '%s'.",
Remi Tricot-Le Breton6443bcc2021-05-17 10:35:08 +0200695 args[0], args[cur_arg]);
Christopher Faulet44d34bf2021-11-05 12:06:14 +0100696 ret = -1;
697 goto end;
Christopher Faulet3d97c902015-12-09 14:59:38 +0100698 }
Remi Tricot-Le Breton6443bcc2021-05-17 10:35:08 +0200699
Olivier Houcharddb573e92023-04-05 17:32:36 +0200700 if (proxy->comp->algos_res->init(&ctx, 9) == 0)
701 proxy->comp->algos_res->end(&ctx);
Christopher Faulet3d97c902015-12-09 14:59:38 +0100702 else {
Christopher Faulet44d34bf2021-11-05 12:06:14 +0100703 memprintf(err, "'%s' : Can't init '%s' algorithm.",
Christopher Faulet3d97c902015-12-09 14:59:38 +0100704 args[0], args[cur_arg]);
Christopher Faulet44d34bf2021-11-05 12:06:14 +0100705 ret = -1;
706 goto end;
Christopher Faulet3d97c902015-12-09 14:59:38 +0100707 }
708 cur_arg++;
709 continue;
710 }
711 }
Christopher Faulet44d34bf2021-11-05 12:06:14 +0100712 else if (strcmp(args[1], "offload") == 0) {
713 if (proxy->cap & PR_CAP_DEF) {
714 memprintf(err, "'%s' : '%s' ignored in 'defaults' section.",
715 args[0], args[1]);
716 ret = 1;
717 }
Olivier Houchard3ce0f012023-04-03 22:22:24 +0200718 comp->flags |= COMP_FL_OFFLOAD;
Christopher Faulet44d34bf2021-11-05 12:06:14 +0100719 }
Olivier Houcharddb573e92023-04-05 17:32:36 +0200720 else if (strcmp(args[1], "type") == 0 || strcmp(args[1], "type-res") == 0) {
Christopher Faulet3d97c902015-12-09 14:59:38 +0100721 int cur_arg = 2;
722
723 if (!*args[cur_arg]) {
Christopher Faulet44d34bf2021-11-05 12:06:14 +0100724 memprintf(err, "'%s' expects <type>.", args[0]);
725 ret = -1;
726 goto end;
Christopher Faulet3d97c902015-12-09 14:59:38 +0100727 }
728 while (*(args[cur_arg])) {
Olivier Houcharddb573e92023-04-05 17:32:36 +0200729 if (comp_append_type(&comp->types_res, args[cur_arg])) {
Remi Tricot-Le Breton6443bcc2021-05-17 10:35:08 +0200730 memprintf(err, "'%s': out of memory.", args[0]);
Christopher Faulet44d34bf2021-11-05 12:06:14 +0100731 ret = -1;
732 goto end;
Remi Tricot-Le Breton6443bcc2021-05-17 10:35:08 +0200733 }
Christopher Faulet3d97c902015-12-09 14:59:38 +0100734 cur_arg++;
735 continue;
736 }
737 }
738 else {
Christopher Faulet44d34bf2021-11-05 12:06:14 +0100739 memprintf(err, "'%s' expects 'algo', 'type' or 'offload'",
Christopher Faulet3d97c902015-12-09 14:59:38 +0100740 args[0]);
Christopher Faulet44d34bf2021-11-05 12:06:14 +0100741 ret = -1;
742 goto end;
Christopher Faulet3d97c902015-12-09 14:59:38 +0100743 }
744
Christopher Faulet44d34bf2021-11-05 12:06:14 +0100745 end:
746 return ret;
Christopher Faulet3d97c902015-12-09 14:59:38 +0100747}
748
Christopher Faulet92d36382015-11-05 13:35:03 +0100749static int
750parse_http_comp_flt(char **args, int *cur_arg, struct proxy *px,
Thierry Fournier3610c392016-04-13 18:27:51 +0200751 struct flt_conf *fconf, char **err, void *private)
Christopher Faulet92d36382015-11-05 13:35:03 +0100752{
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100753 struct flt_conf *fc, *back;
Christopher Faulet92d36382015-11-05 13:35:03 +0100754
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100755 list_for_each_entry_safe(fc, back, &px->filter_configs, list) {
756 if (fc->id == http_comp_flt_id) {
Christopher Faulet92d36382015-11-05 13:35:03 +0100757 memprintf(err, "%s: Proxy supports only one compression filter\n", px->id);
758 return -1;
759 }
760 }
761
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100762 fconf->id = http_comp_flt_id;
763 fconf->conf = NULL;
764 fconf->ops = &comp_ops;
Christopher Faulet92d36382015-11-05 13:35:03 +0100765 (*cur_arg)++;
766
767 return 0;
768}
769
770
771int
Christopher Fauletc9df7f72018-12-10 16:14:04 +0100772check_implicit_http_comp_flt(struct proxy *proxy)
Christopher Faulet92d36382015-11-05 13:35:03 +0100773{
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100774 struct flt_conf *fconf;
Christopher Faulet27d93c32018-12-15 22:32:02 +0100775 int explicit = 0;
776 int comp = 0;
Christopher Faulet92d36382015-11-05 13:35:03 +0100777 int err = 0;
778
779 if (proxy->comp == NULL)
780 goto end;
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100781 if (!LIST_ISEMPTY(&proxy->filter_configs)) {
782 list_for_each_entry(fconf, &proxy->filter_configs, list) {
783 if (fconf->id == http_comp_flt_id)
Christopher Faulet27d93c32018-12-15 22:32:02 +0100784 comp = 1;
785 else if (fconf->id == cache_store_flt_id) {
786 if (comp) {
787 ha_alert("config: %s '%s': unable to enable the compression filter "
788 "before any cache filter.\n",
789 proxy_type_str(proxy), proxy->id);
790 err++;
791 goto end;
792 }
793 }
Christopher Faulet78fbb9f2019-08-11 23:11:03 +0200794 else if (fconf->id == fcgi_flt_id)
795 continue;
Christopher Faulet27d93c32018-12-15 22:32:02 +0100796 else
797 explicit = 1;
Christopher Faulet92d36382015-11-05 13:35:03 +0100798 }
Christopher Faulet27d93c32018-12-15 22:32:02 +0100799 }
800 if (comp)
801 goto end;
802 else if (explicit) {
803 ha_alert("config: %s '%s': require an explicit filter declaration to use "
804 "HTTP compression\n", proxy_type_str(proxy), proxy->id);
Christopher Faulet92d36382015-11-05 13:35:03 +0100805 err++;
806 goto end;
807 }
808
Christopher Faulet27d93c32018-12-15 22:32:02 +0100809 /* Implicit declaration of the compression filter is always the last
810 * one */
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100811 fconf = calloc(1, sizeof(*fconf));
812 if (!fconf) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100813 ha_alert("config: %s '%s': out of memory\n",
814 proxy_type_str(proxy), proxy->id);
Christopher Faulet92d36382015-11-05 13:35:03 +0100815 err++;
816 goto end;
817 }
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100818 fconf->id = http_comp_flt_id;
819 fconf->conf = NULL;
820 fconf->ops = &comp_ops;
Willy Tarreau2b718102021-04-21 07:32:39 +0200821 LIST_APPEND(&proxy->filter_configs, &fconf->list);
Christopher Faulet92d36382015-11-05 13:35:03 +0100822 end:
823 return err;
824}
825
826/*
827 * boolean, returns true if compression is used (either gzip or deflate) in the
828 * response.
829 */
Christopher Faulet3d97c902015-12-09 14:59:38 +0100830static int
Christopher Faulet92d36382015-11-05 13:35:03 +0100831smp_fetch_res_comp(const struct arg *args, struct sample *smp, const char *kw,
832 void *private)
Christopher Faulet3d97c902015-12-09 14:59:38 +0100833{
Willy Tarreaube508f12016-03-10 11:47:01 +0100834 struct http_txn *txn = smp->strm ? smp->strm->txn : NULL;
Christopher Faulet92d36382015-11-05 13:35:03 +0100835
Christopher Faulet3d97c902015-12-09 14:59:38 +0100836 smp->data.type = SMP_T_BOOL;
Christopher Faulet92d36382015-11-05 13:35:03 +0100837 smp->data.u.sint = (txn && (txn->rsp.flags & HTTP_MSGF_COMPRESSING));
Christopher Faulet3d97c902015-12-09 14:59:38 +0100838 return 1;
839}
840
Christopher Faulet92d36382015-11-05 13:35:03 +0100841/*
842 * string, returns algo
843 */
Christopher Faulet3d97c902015-12-09 14:59:38 +0100844static int
Christopher Faulet92d36382015-11-05 13:35:03 +0100845smp_fetch_res_comp_algo(const struct arg *args, struct sample *smp,
846 const char *kw, void *private)
Christopher Faulet3d97c902015-12-09 14:59:38 +0100847{
Willy Tarreaube508f12016-03-10 11:47:01 +0100848 struct http_txn *txn = smp->strm ? smp->strm->txn : NULL;
Christopher Faulet92d36382015-11-05 13:35:03 +0100849 struct filter *filter;
850 struct comp_state *st;
851
Christopher Faulet03d85532017-09-15 10:14:43 +0200852 if (!txn || !(txn->rsp.flags & HTTP_MSGF_COMPRESSING))
Christopher Faulet3d97c902015-12-09 14:59:38 +0100853 return 0;
854
Christopher Fauletfcf035c2015-12-03 11:48:03 +0100855 list_for_each_entry(filter, &strm_flt(smp->strm)->filters, list) {
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100856 if (FLT_ID(filter) != http_comp_flt_id)
Christopher Faulet92d36382015-11-05 13:35:03 +0100857 continue;
858
859 if (!(st = filter->ctx))
860 break;
861
862 smp->data.type = SMP_T_STR;
863 smp->flags = SMP_F_CONST;
Olivier Houcharddfc11da2023-04-05 16:25:57 +0200864 smp->data.u.str.area = st->comp_algo[COMP_DIR_RES]->cfg_name;
865 smp->data.u.str.data = st->comp_algo[COMP_DIR_RES]->cfg_name_len;
Christopher Faulet92d36382015-11-05 13:35:03 +0100866 return 1;
867 }
868 return 0;
Christopher Faulet3d97c902015-12-09 14:59:38 +0100869}
870
871/* Declare the config parser for "compression" keyword */
872static struct cfg_kw_list cfg_kws = {ILH, {
873 { CFG_LISTEN, "compression", parse_compression_options },
874 { 0, NULL, NULL },
875 }
876};
877
Willy Tarreau0108d902018-11-25 19:14:37 +0100878INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
879
Christopher Faulet92d36382015-11-05 13:35:03 +0100880/* Declare the filter parser for "compression" keyword */
881static struct flt_kw_list filter_kws = { "COMP", { }, {
Thierry Fournier3610c392016-04-13 18:27:51 +0200882 { "compression", parse_http_comp_flt, NULL },
883 { NULL, NULL, NULL },
Christopher Faulet92d36382015-11-05 13:35:03 +0100884 }
885};
886
Willy Tarreau0108d902018-11-25 19:14:37 +0100887INITCALL1(STG_REGISTER, flt_register_keywords, &filter_kws);
888
Christopher Faulet3d97c902015-12-09 14:59:38 +0100889/* Note: must not be declared <const> as its list will be overwritten */
890static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
Christopher Faulet92d36382015-11-05 13:35:03 +0100891 { "res.comp", smp_fetch_res_comp, 0, NULL, SMP_T_BOOL, SMP_USE_HRSHP },
892 { "res.comp_algo", smp_fetch_res_comp_algo, 0, NULL, SMP_T_STR, SMP_USE_HRSHP },
893 { /* END */ },
894 }
895};
Christopher Faulet3d97c902015-12-09 14:59:38 +0100896
Willy Tarreau0108d902018-11-25 19:14:37 +0100897INITCALL1(STG_REGISTER, sample_register_fetches, &sample_fetch_keywords);