blob: 3b939e1890f9c75800925dae6cd82a11aad49253 [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
Olivier Houchardead43fe2023-04-06 00:33:48 +0200133static void
134comp_prepare_compress_request(struct comp_state *st, struct stream *s, struct http_msg *msg)
135{
136 struct htx *htx = htxbuf(&msg->chn->buf);
137 struct http_txn *txn = s->txn;
138 struct http_hdr_ctx ctx;
139 struct comp_type *comp_type;
140
141 ctx.blk = NULL;
142 /* Already compressed, don't bother */
143 if (http_find_header(htx, ist("Content-Encoding"), &ctx, 1))
144 return;
145 /* HTTP < 1.1 should not be compressed */
146 if (!(msg->flags & HTTP_MSGF_VER_11) || !(txn->req.flags & HTTP_MSGF_VER_11))
147 return;
148 comp_type = NULL;
149
150 /*
151 * We don't want to compress content-types not listed in the "compression type" directive if any. If no content-type was found but configuration
152 * requires one, we don't compress either. Backend has the priority.
153 */
154 ctx.blk = NULL;
155 if (http_find_header(htx, ist("Content-Type"), &ctx, 1)) {
156 if ((s->be->comp && (comp_type = s->be->comp->types_req)) ||
157 (strm_fe(s)->comp && (comp_type = strm_fe(s)->comp->types_req))) {
158 for (; comp_type; comp_type = comp_type->next) {
159 if (ctx.value.len >= comp_type->name_len &&
160 strncasecmp(ctx.value.ptr, comp_type->name, comp_type->name_len) == 0)
161 /* this Content-Type should be compressed */
162 break;
163 }
164 /* this Content-Type should not be compressed */
165 if (comp_type == NULL)
166 goto fail;
167 }
168 }
169 else { /* no content-type header */
170 if ((s->be->comp && s->be->comp->types_req) ||
171 (strm_fe(s)->comp && strm_fe(s)->comp->types_req))
172 goto fail; /* a content-type was required */
173 }
174
175 /* limit compression rate */
176 if (global.comp_rate_lim > 0)
177 if (read_freq_ctr(&global.comp_bps_in) > global.comp_rate_lim)
178 goto fail;
179
180 /* limit cpu usage */
181 if (th_ctx->idle_pct < compress_min_idle)
182 goto fail;
183
184 if (txn->meth == HTTP_METH_HEAD)
185 return;
186 if (s->be->comp->algo_req != NULL)
187 st->comp_algo[COMP_DIR_REQ] = s->be->comp->algo_req;
188 else if (strm_fe(s)->comp->algo_req != NULL)
189 st->comp_algo[COMP_DIR_REQ] = strm_fe(s)->comp->algo_req;
190
191
192 /* limit compression rate */
193 if (global.comp_rate_lim > 0)
194 if (read_freq_ctr(&global.comp_bps_in) > global.comp_rate_lim)
195 goto fail;
196
197 /* limit cpu usage */
198 if (th_ctx->idle_pct < compress_min_idle)
199 goto fail;
200
201 /* initialize compression */
202 if (st->comp_algo[COMP_DIR_REQ]->init(&st->comp_ctx[COMP_DIR_REQ], global.tune.comp_maxlevel) < 0)
203 goto fail;
204
205 return;
206fail:
207 st->comp_algo[COMP_DIR_REQ] = NULL;
208}
209
Christopher Faulet92d36382015-11-05 13:35:03 +0100210static int
Christopher Faulet1339d742016-05-11 16:48:33 +0200211comp_http_headers(struct stream *s, struct filter *filter, struct http_msg *msg)
212{
213 struct comp_state *st = filter->ctx;
Olivier Houchardead43fe2023-04-06 00:33:48 +0200214 int comp_flags = 0;
Christopher Faulet1339d742016-05-11 16:48:33 +0200215
216 if (!strm_fe(s)->comp && !s->be->comp)
217 goto end;
Olivier Houchardead43fe2023-04-06 00:33:48 +0200218 if (strm_fe(s)->comp)
219 comp_flags |= strm_fe(s)->comp->flags;
220 if (s->be->comp)
221 comp_flags |= s->be->comp->flags;
Christopher Faulet1339d742016-05-11 16:48:33 +0200222
Olivier Houchardead43fe2023-04-06 00:33:48 +0200223 if (!(msg->chn->flags & CF_ISRESP)) {
224 if (comp_flags & COMP_FL_DIR_REQ) {
225 comp_prepare_compress_request(st, s, msg);
226 if (st->comp_algo[COMP_DIR_REQ]) {
227 if (!set_compression_header(st, s, msg))
228 goto end;
229 register_data_filter(s, msg->chn, filter);
230 st->flags |= COMP_STATE_PROCESSING;
231 }
232 }
233 if (comp_flags & COMP_FL_DIR_RES)
234 select_compression_request_header(st, s, msg);
235 } else if (comp_flags & COMP_FL_DIR_RES) {
Christopher Faulet3dc860d2017-09-15 11:39:36 +0200236 /* Response headers have already been checked in
237 * comp_http_post_analyze callback. */
Olivier Houcharddfc11da2023-04-05 16:25:57 +0200238 if (st->comp_algo[COMP_DIR_RES]) {
239 if (!set_compression_header(st, s, msg))
Christopher Faulet27d93c32018-12-15 22:32:02 +0100240 goto end;
Christopher Faulet1339d742016-05-11 16:48:33 +0200241 register_data_filter(s, msg->chn, filter);
Christopher Faulet12554d02021-06-09 17:12:44 +0200242 st->flags |= COMP_STATE_PROCESSING;
Christopher Faulet1339d742016-05-11 16:48:33 +0200243 }
244 }
245
246 end:
247 return 1;
248}
249
250static int
Christopher Faulet3dc860d2017-09-15 11:39:36 +0200251comp_http_post_analyze(struct stream *s, struct filter *filter,
252 struct channel *chn, unsigned an_bit)
253{
254 struct http_txn *txn = s->txn;
255 struct http_msg *msg = &txn->rsp;
256 struct comp_state *st = filter->ctx;
257
258 if (an_bit != AN_RES_WAIT_HTTP)
259 goto end;
260
261 if (!strm_fe(s)->comp && !s->be->comp)
262 goto end;
263
264 select_compression_response_header(st, s, msg);
265
266 end:
267 return 1;
268}
269
270static int
Christopher Faulete6902cd2018-11-30 22:29:48 +0100271comp_http_payload(struct stream *s, struct filter *filter, struct http_msg *msg,
272 unsigned int offset, unsigned int len)
273{
274 struct comp_state *st = filter->ctx;
Christopher Faulet27ba2dc2018-12-05 11:53:24 +0100275 struct htx *htx = htxbuf(&msg->chn->buf);
Christopher Faulete6a62bf2020-03-02 16:20:05 +0100276 struct htx_ret htxret = htx_find_offset(htx, offset);
Christopher Fauletd1ac2b92020-12-02 19:12:22 +0100277 struct htx_blk *blk, *next;
278 int ret, consumed = 0, to_forward = 0, last = 0;
Olivier Houcharddfc11da2023-04-05 16:25:57 +0200279 int dir;
280
281 if (msg->chn->flags & CF_ISRESP)
282 dir = COMP_DIR_RES;
283 else
284 dir = COMP_DIR_REQ;
Christopher Faulete6902cd2018-11-30 22:29:48 +0100285
Christopher Faulete6a62bf2020-03-02 16:20:05 +0100286 blk = htxret.blk;
287 offset = htxret.ret;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +0100288 for (next = NULL; blk && len; blk = next) {
Christopher Faulete6902cd2018-11-30 22:29:48 +0100289 enum htx_blk_type type = htx_get_blk_type(blk);
290 uint32_t sz = htx_get_blksz(blk);
291 struct ist v;
292
Christopher Fauletd1ac2b92020-12-02 19:12:22 +0100293 next = htx_get_next_blk(htx, blk);
294 while (next && htx_get_blk_type(next) == HTX_BLK_UNUSED)
Christopher Faulet86ca0e52021-06-09 16:59:02 +0200295 next = htx_get_next_blk(htx, next);
Christopher Faulete6902cd2018-11-30 22:29:48 +0100296
Christopher Faulet12554d02021-06-09 17:12:44 +0200297 if (!(st->flags & COMP_STATE_PROCESSING))
298 goto consume;
299
Christopher Fauletd1ac2b92020-12-02 19:12:22 +0100300 if (htx_compression_buffer_init(htx, &trash) < 0) {
301 msg->chn->flags |= CF_WAKE_WRITE;
302 goto end;
303 }
304
305 switch (type) {
Christopher Faulete6902cd2018-11-30 22:29:48 +0100306 case HTX_BLK_DATA:
Christopher Fauletd1ac2b92020-12-02 19:12:22 +0100307 /* it is the last data block */
308 last = ((!next && (htx->flags & HTX_FL_EOM)) || (next && htx_get_blk_type(next) != HTX_BLK_DATA));
Christopher Faulete6902cd2018-11-30 22:29:48 +0100309 v = htx_get_blk_value(htx, blk);
Christopher Fauletd1ac2b92020-12-02 19:12:22 +0100310 v = istadv(v, offset);
311 if (v.len > len) {
312 last = 0;
Christopher Faulete6902cd2018-11-30 22:29:48 +0100313 v.len = len;
Christopher Faulete6902cd2018-11-30 22:29:48 +0100314 }
Christopher Fauletd1ac2b92020-12-02 19:12:22 +0100315
Olivier Houcharddfc11da2023-04-05 16:25:57 +0200316 ret = htx_compression_buffer_add_data(st, v.ptr, v.len, &trash, dir);
317 if (ret < 0 || htx_compression_buffer_end(st, &trash, last, dir) < 0)
Christopher Faulete6902cd2018-11-30 22:29:48 +0100318 goto error;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +0100319 BUG_ON(v.len != ret);
320
321 if (ret == sz && !b_data(&trash))
322 next = htx_remove_blk(htx, blk);
Christopher Faulet402740c2021-06-09 17:04:37 +0200323 else {
Christopher Fauletd1ac2b92020-12-02 19:12:22 +0100324 blk = htx_replace_blk_value(htx, blk, v, ist2(b_head(&trash), b_data(&trash)));
Christopher Faulet402740c2021-06-09 17:04:37 +0200325 next = htx_get_next_blk(htx, blk);
326 }
Christopher Fauletd1ac2b92020-12-02 19:12:22 +0100327
Christopher Faulete6902cd2018-11-30 22:29:48 +0100328 len -= ret;
329 consumed += ret;
330 to_forward += b_data(&trash);
Christopher Faulet12554d02021-06-09 17:12:44 +0200331 if (last)
332 st->flags &= ~COMP_STATE_PROCESSING;
Christopher Faulete6902cd2018-11-30 22:29:48 +0100333 break;
334
Christopher Faulete6902cd2018-11-30 22:29:48 +0100335 case HTX_BLK_TLR:
Christopher Faulet2d7c5392019-06-03 10:41:26 +0200336 case HTX_BLK_EOT:
Olivier Houcharddfc11da2023-04-05 16:25:57 +0200337 if (htx_compression_buffer_end(st, &trash, 1, dir) < 0)
Christopher Fauletd1ac2b92020-12-02 19:12:22 +0100338 goto error;
339 if (b_data(&trash)) {
340 struct htx_blk *last = htx_add_last_data(htx, ist2(b_head(&trash), b_data(&trash)));
341 if (!last)
Christopher Faulete6902cd2018-11-30 22:29:48 +0100342 goto error;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +0100343 blk = htx_get_next_blk(htx, last);
344 if (!blk)
345 goto error;
Christopher Faulet402740c2021-06-09 17:04:37 +0200346 next = htx_get_next_blk(htx, blk);
Christopher Fauletd1ac2b92020-12-02 19:12:22 +0100347 to_forward += b_data(&trash);
Christopher Faulete6902cd2018-11-30 22:29:48 +0100348 }
Christopher Faulet12554d02021-06-09 17:12:44 +0200349 st->flags &= ~COMP_STATE_PROCESSING;
Willy Tarreau91d398c2022-11-14 07:36:05 +0100350 __fallthrough;
Christopher Faulete6902cd2018-11-30 22:29:48 +0100351
352 default:
Christopher Fauletd1ac2b92020-12-02 19:12:22 +0100353 consume:
Christopher Faulete6902cd2018-11-30 22:29:48 +0100354 sz -= offset;
355 if (sz > len)
356 sz = len;
357 consumed += sz;
358 to_forward += sz;
359 len -= sz;
360 break;
361 }
362
363 offset = 0;
Christopher Faulete6902cd2018-11-30 22:29:48 +0100364 }
365
366 end:
367 if (to_forward != consumed)
368 flt_update_offsets(filter, msg->chn, to_forward - consumed);
369
Olivier Houcharddfc11da2023-04-05 16:25:57 +0200370 if (st->comp_ctx[dir] && st->comp_ctx[dir]->cur_lvl > 0) {
Willy Tarreauef6fd852019-02-04 11:48:03 +0100371 update_freq_ctr(&global.comp_bps_in, consumed);
Olivier Houcharddea25f52023-04-06 00:33:01 +0200372 _HA_ATOMIC_ADD(&strm_fe(s)->fe_counters.comp_in[dir], consumed);
373 _HA_ATOMIC_ADD(&s->be->be_counters.comp_in[dir], consumed);
Christopher Faulete6902cd2018-11-30 22:29:48 +0100374 update_freq_ctr(&global.comp_bps_out, to_forward);
Olivier Houcharddea25f52023-04-06 00:33:01 +0200375 _HA_ATOMIC_ADD(&strm_fe(s)->fe_counters.comp_out[dir], to_forward);
376 _HA_ATOMIC_ADD(&s->be->be_counters.comp_out[dir], to_forward);
Willy Tarreauef6fd852019-02-04 11:48:03 +0100377 } else {
Olivier Houcharddea25f52023-04-06 00:33:01 +0200378 _HA_ATOMIC_ADD(&strm_fe(s)->fe_counters.comp_byp[dir], consumed);
379 _HA_ATOMIC_ADD(&s->be->be_counters.comp_byp[dir], consumed);
Christopher Faulete6902cd2018-11-30 22:29:48 +0100380 }
381 return to_forward;
382
383 error:
384 return -1;
385}
386
Christopher Faulet2fb28802015-12-01 10:40:57 +0100387
Christopher Faulet92d36382015-11-05 13:35:03 +0100388static int
Christopher Fauletd60b3cf2017-06-26 11:47:13 +0200389comp_http_end(struct stream *s, struct filter *filter,
390 struct http_msg *msg)
391{
392 struct comp_state *st = filter->ctx;
393
Olivier Houcharddfc11da2023-04-05 16:25:57 +0200394 if (!(msg->chn->flags & CF_ISRESP) || !st || !st->comp_algo[COMP_DIR_RES])
Christopher Fauletd60b3cf2017-06-26 11:47:13 +0200395 goto end;
396
397 if (strm_fe(s)->mode == PR_MODE_HTTP)
Willy Tarreau4781b152021-04-06 13:53:36 +0200398 _HA_ATOMIC_INC(&strm_fe(s)->fe_counters.p.http.comp_rsp);
Christopher Fauletd60b3cf2017-06-26 11:47:13 +0200399 if ((s->flags & SF_BE_ASSIGNED) && (s->be->mode == PR_MODE_HTTP))
Willy Tarreau4781b152021-04-06 13:53:36 +0200400 _HA_ATOMIC_INC(&s->be->be_counters.p.http.comp_rsp);
Christopher Fauletd60b3cf2017-06-26 11:47:13 +0200401 end:
402 return 1;
403}
Christopher Faulet27d93c32018-12-15 22:32:02 +0100404
Christopher Faulet89f2b162019-07-15 21:16:04 +0200405/***********************************************************************/
Christopher Faulet27d93c32018-12-15 22:32:02 +0100406static int
Olivier Houcharddfc11da2023-04-05 16:25:57 +0200407set_compression_header(struct comp_state *st, struct stream *s, struct http_msg *msg)
Christopher Faulet27d93c32018-12-15 22:32:02 +0100408{
409 struct htx *htx = htxbuf(&msg->chn->buf);
Christopher Faulet39e436e2022-04-15 15:32:03 +0200410 struct htx_sl *sl;
Tim Duesterhusb229f012019-01-29 16:38:56 +0100411 struct http_hdr_ctx ctx;
Olivier Houcharddfc11da2023-04-05 16:25:57 +0200412 struct comp_algo *comp_algo;
413 int comp_index;
414
415 if (msg->chn->flags & CF_ISRESP)
416 comp_index = COMP_DIR_RES;
417 else
418 comp_index = COMP_DIR_REQ;
Christopher Faulet27d93c32018-12-15 22:32:02 +0100419
Christopher Faulet39e436e2022-04-15 15:32:03 +0200420 sl = http_get_stline(htx);
421 if (!sl)
422 goto error;
423
Olivier Houcharddfc11da2023-04-05 16:25:57 +0200424 comp_algo = st->comp_algo[comp_index];
425
Christopher Faulet910b7572022-10-24 08:39:29 +0200426 /* add "Transfer-Encoding: chunked" header */
427 if (!(msg->flags & HTTP_MSGF_TE_CHNK)) {
428 if (!http_add_header(htx, ist("Transfer-Encoding"), ist("chunked")))
429 goto error;
430 msg->flags |= HTTP_MSGF_TE_CHNK;
431 sl->flags |= (HTX_SL_F_XFER_ENC|HTX_SL_F_CHNK);
432 }
433
Christopher Faulet27d93c32018-12-15 22:32:02 +0100434 /* remove Content-Length header */
435 if (msg->flags & HTTP_MSGF_CNT_LEN) {
Christopher Faulet27d93c32018-12-15 22:32:02 +0100436 ctx.blk = NULL;
437 while (http_find_header(htx, ist("Content-Length"), &ctx, 1))
438 http_remove_header(htx, &ctx);
Christopher Faulet39e436e2022-04-15 15:32:03 +0200439 msg->flags &= ~HTTP_MSGF_CNT_LEN;
440 sl->flags &= ~HTX_SL_F_CLEN;
Christopher Faulet27d93c32018-12-15 22:32:02 +0100441 }
442
Tim Duesterhusb229f012019-01-29 16:38:56 +0100443 /* convert "ETag" header to a weak ETag */
444 ctx.blk = NULL;
445 if (http_find_header(htx, ist("ETag"), &ctx, 1)) {
446 if (ctx.value.ptr[0] == '"') {
447 /* This a strong ETag. Convert it to a weak one. */
448 struct ist v = ist2(trash.area, 0);
449 if (istcat(&v, ist("W/"), trash.size) == -1 || istcat(&v, ctx.value, trash.size) == -1)
450 goto error;
451
452 if (!http_replace_header_value(htx, &ctx, v))
453 goto error;
454 }
455 }
456
Tim Duesterhus721d6862019-06-17 16:10:07 +0200457 if (!http_add_header(htx, ist("Vary"), ist("Accept-Encoding")))
458 goto error;
459
Christopher Faulet910b7572022-10-24 08:39:29 +0200460 /*
461 * Add Content-Encoding header when it's not identity encoding.
462 * RFC 2616 : Identity encoding: This content-coding is used only in the
463 * Accept-Encoding header, and SHOULD NOT be used in the Content-Encoding
464 * header.
465 */
Olivier Houcharddfc11da2023-04-05 16:25:57 +0200466 if (comp_algo->cfg_name_len != 8 || memcmp(comp_algo->cfg_name, "identity", 8) != 0) {
467 struct ist v = ist2(comp_algo->ua_name, comp_algo->ua_name_len);
Christopher Faulet910b7572022-10-24 08:39:29 +0200468
469 if (!http_add_header(htx, ist("Content-Encoding"), v))
470 goto error;
471 }
472
Christopher Faulet27d93c32018-12-15 22:32:02 +0100473 return 1;
474
475 error:
Olivier Houcharddfc11da2023-04-05 16:25:57 +0200476 st->comp_algo[comp_index]->end(&st->comp_ctx[comp_index]);
477 st->comp_algo[comp_index] = NULL;
Christopher Faulet27d93c32018-12-15 22:32:02 +0100478 return 0;
479}
480
Christopher Faulet3d97c902015-12-09 14:59:38 +0100481/*
482 * Selects a compression algorithm depending on the client request.
483 */
Christopher Faulete6902cd2018-11-30 22:29:48 +0100484static int
Christopher Faulet89f2b162019-07-15 21:16:04 +0200485select_compression_request_header(struct comp_state *st, struct stream *s, struct http_msg *msg)
Christopher Faulete6902cd2018-11-30 22:29:48 +0100486{
Christopher Faulet27ba2dc2018-12-05 11:53:24 +0100487 struct htx *htx = htxbuf(&msg->chn->buf);
Christopher Faulete6902cd2018-11-30 22:29:48 +0100488 struct http_hdr_ctx ctx;
489 struct comp_algo *comp_algo = NULL;
490 struct comp_algo *comp_algo_back = NULL;
491
492 /* Disable compression for older user agents announcing themselves as "Mozilla/4"
493 * unless they are known good (MSIE 6 with XP SP2, or MSIE 7 and later).
494 * See http://zoompf.com/2012/02/lose-the-wait-http-compression for more details.
495 */
496 ctx.blk = NULL;
497 if (http_find_header(htx, ist("User-Agent"), &ctx, 1) &&
498 ctx.value.len >= 9 &&
499 memcmp(ctx.value.ptr, "Mozilla/4", 9) == 0 &&
500 (ctx.value.len < 31 ||
501 memcmp(ctx.value.ptr + 25, "MSIE ", 5) != 0 ||
502 *(ctx.value.ptr + 30) < '6' ||
503 (*(ctx.value.ptr + 30) == '6' &&
504 (ctx.value.len < 54 || memcmp(ctx.value.ptr + 51, "SV1", 3) != 0)))) {
Olivier Houcharddfc11da2023-04-05 16:25:57 +0200505 st->comp_algo[COMP_DIR_RES] = NULL;
Christopher Faulete6902cd2018-11-30 22:29:48 +0100506 return 0;
507 }
508
509 /* search for the algo in the backend in priority or the frontend */
Olivier Houcharddb573e92023-04-05 17:32:36 +0200510 if ((s->be->comp && (comp_algo_back = s->be->comp->algos_res)) ||
511 (strm_fe(s)->comp && (comp_algo_back = strm_fe(s)->comp->algos_res))) {
Christopher Faulete6902cd2018-11-30 22:29:48 +0100512 int best_q = 0;
513
514 ctx.blk = NULL;
515 while (http_find_header(htx, ist("Accept-Encoding"), &ctx, 0)) {
516 const char *qval;
517 int q;
518 int toklen;
519
520 /* try to isolate the token from the optional q-value */
521 toklen = 0;
522 while (toklen < ctx.value.len && HTTP_IS_TOKEN(*(ctx.value.ptr + toklen)))
523 toklen++;
524
525 qval = ctx.value.ptr + toklen;
526 while (1) {
Tim Duesterhus77508502022-03-15 13:11:06 +0100527 while (qval < istend(ctx.value) && HTTP_IS_LWS(*qval))
Christopher Faulete6902cd2018-11-30 22:29:48 +0100528 qval++;
529
Tim Duesterhus77508502022-03-15 13:11:06 +0100530 if (qval >= istend(ctx.value) || *qval != ';') {
Christopher Faulete6902cd2018-11-30 22:29:48 +0100531 qval = NULL;
532 break;
533 }
534 qval++;
535
Tim Duesterhus77508502022-03-15 13:11:06 +0100536 while (qval < istend(ctx.value) && HTTP_IS_LWS(*qval))
Christopher Faulete6902cd2018-11-30 22:29:48 +0100537 qval++;
538
Tim Duesterhus77508502022-03-15 13:11:06 +0100539 if (qval >= istend(ctx.value)) {
Christopher Faulete6902cd2018-11-30 22:29:48 +0100540 qval = NULL;
541 break;
542 }
Tim Duesterhus77508502022-03-15 13:11:06 +0100543 if (strncmp(qval, "q=", MIN(istend(ctx.value) - qval, 2)) == 0)
Christopher Faulete6902cd2018-11-30 22:29:48 +0100544 break;
545
Tim Duesterhus77508502022-03-15 13:11:06 +0100546 while (qval < istend(ctx.value) && *qval != ';')
Christopher Faulete6902cd2018-11-30 22:29:48 +0100547 qval++;
548 }
549
550 /* here we have qval pointing to the first "q=" attribute or NULL if not found */
551 q = qval ? http_parse_qvalue(qval + 2, NULL) : 1000;
552
553 if (q <= best_q)
554 continue;
555
556 for (comp_algo = comp_algo_back; comp_algo; comp_algo = comp_algo->next) {
557 if (*(ctx.value.ptr) == '*' ||
558 word_match(ctx.value.ptr, toklen, comp_algo->ua_name, comp_algo->ua_name_len)) {
Olivier Houcharddfc11da2023-04-05 16:25:57 +0200559 st->comp_algo[COMP_DIR_RES] = comp_algo;
Christopher Faulete6902cd2018-11-30 22:29:48 +0100560 best_q = q;
561 break;
562 }
563 }
564 }
565 }
566
567 /* remove all occurrences of the header when "compression offload" is set */
Olivier Houcharddfc11da2023-04-05 16:25:57 +0200568 if (st->comp_algo[COMP_DIR_RES]) {
Olivier Houchard3ce0f012023-04-03 22:22:24 +0200569 if ((s->be->comp && (s->be->comp->flags & COMP_FL_OFFLOAD)) ||
570 (strm_fe(s)->comp && (strm_fe(s)->comp->flags & COMP_FL_OFFLOAD))) {
Christopher Faulete6902cd2018-11-30 22:29:48 +0100571 http_remove_header(htx, &ctx);
572 ctx.blk = NULL;
573 while (http_find_header(htx, ist("Accept-Encoding"), &ctx, 1))
574 http_remove_header(htx, &ctx);
575 }
Christopher Faulet3d97c902015-12-09 14:59:38 +0100576 return 1;
577 }
578
579 /* identity is implicit does not require headers */
Olivier Houcharddb573e92023-04-05 17:32:36 +0200580 if ((s->be->comp && (comp_algo_back = s->be->comp->algos_res)) ||
581 (strm_fe(s)->comp && (comp_algo_back = strm_fe(s)->comp->algos_res))) {
Christopher Faulet3d97c902015-12-09 14:59:38 +0100582 for (comp_algo = comp_algo_back; comp_algo; comp_algo = comp_algo->next) {
583 if (comp_algo->cfg_name_len == 8 && memcmp(comp_algo->cfg_name, "identity", 8) == 0) {
Olivier Houcharddfc11da2023-04-05 16:25:57 +0200584 st->comp_algo[COMP_DIR_RES] = comp_algo;
Christopher Faulet3d97c902015-12-09 14:59:38 +0100585 return 1;
586 }
587 }
588 }
589
Olivier Houcharddfc11da2023-04-05 16:25:57 +0200590 st->comp_algo[COMP_DIR_RES] = NULL;
Christopher Faulet3d97c902015-12-09 14:59:38 +0100591 return 0;
592}
593
594/*
Ilya Shipitsin46a030c2020-07-05 16:36:08 +0500595 * Selects a compression algorithm depending of the server response.
Christopher Faulet3d97c902015-12-09 14:59:38 +0100596 */
Christopher Faulet92d36382015-11-05 13:35:03 +0100597static int
Christopher Faulet89f2b162019-07-15 21:16:04 +0200598select_compression_response_header(struct comp_state *st, struct stream *s, struct http_msg *msg)
Christopher Faulete6902cd2018-11-30 22:29:48 +0100599{
Christopher Faulet27ba2dc2018-12-05 11:53:24 +0100600 struct htx *htx = htxbuf(&msg->chn->buf);
Christopher Faulete6902cd2018-11-30 22:29:48 +0100601 struct http_txn *txn = s->txn;
602 struct http_hdr_ctx ctx;
603 struct comp_type *comp_type;
604
605 /* no common compression algorithm was found in request header */
Olivier Houcharddfc11da2023-04-05 16:25:57 +0200606 if (st->comp_algo[COMP_DIR_RES] == NULL)
Christopher Faulete6902cd2018-11-30 22:29:48 +0100607 goto fail;
608
Christopher Faulet1d3613a2019-01-07 14:41:59 +0100609 /* compression already in progress */
610 if (msg->flags & HTTP_MSGF_COMPRESSING)
611 goto fail;
612
Christopher Faulete6902cd2018-11-30 22:29:48 +0100613 /* HTTP < 1.1 should not be compressed */
614 if (!(msg->flags & HTTP_MSGF_VER_11) || !(txn->req.flags & HTTP_MSGF_VER_11))
615 goto fail;
616
617 if (txn->meth == HTTP_METH_HEAD)
618 goto fail;
619
620 /* compress 200,201,202,203 responses only */
621 if ((txn->status != 200) &&
622 (txn->status != 201) &&
623 (txn->status != 202) &&
624 (txn->status != 203))
625 goto fail;
626
Christopher Fauletc963eb22018-12-21 14:53:54 +0100627 if (!(msg->flags & HTTP_MSGF_XFER_LEN) || msg->flags & HTTP_MSGF_BODYLESS)
Christopher Faulete6902cd2018-11-30 22:29:48 +0100628 goto fail;
629
630 /* content is already compressed */
631 ctx.blk = NULL;
632 if (http_find_header(htx, ist("Content-Encoding"), &ctx, 1))
633 goto fail;
634
635 /* no compression when Cache-Control: no-transform is present in the message */
636 ctx.blk = NULL;
637 while (http_find_header(htx, ist("Cache-Control"), &ctx, 0)) {
638 if (word_match(ctx.value.ptr, ctx.value.len, "no-transform", 12))
639 goto fail;
640 }
641
Tim Duesterhusb229f012019-01-29 16:38:56 +0100642 /* no compression when ETag is malformed */
643 ctx.blk = NULL;
644 if (http_find_header(htx, ist("ETag"), &ctx, 1)) {
Tim Duesterhus6414cd12020-09-01 18:32:35 +0200645 if (http_get_etag_type(ctx.value) == ETAG_INVALID)
Tim Duesterhusb229f012019-01-29 16:38:56 +0100646 goto fail;
Tim Duesterhusb229f012019-01-29 16:38:56 +0100647 }
648 /* no compression when multiple ETags are present
649 * Note: Do not reset ctx.blk!
650 */
651 if (http_find_header(htx, ist("ETag"), &ctx, 1))
652 goto fail;
653
Christopher Faulete6902cd2018-11-30 22:29:48 +0100654 comp_type = NULL;
655
656 /* we don't want to compress multipart content-types, nor content-types that are
657 * not listed in the "compression type" directive if any. If no content-type was
658 * found but configuration requires one, we don't compress either. Backend has
659 * the priority.
660 */
661 ctx.blk = NULL;
662 if (http_find_header(htx, ist("Content-Type"), &ctx, 1)) {
663 if (ctx.value.len >= 9 && strncasecmp("multipart", ctx.value.ptr, 9) == 0)
664 goto fail;
665
Olivier Houcharddb573e92023-04-05 17:32:36 +0200666 if ((s->be->comp && (comp_type = s->be->comp->types_res)) ||
667 (strm_fe(s)->comp && (comp_type = strm_fe(s)->comp->types_res))) {
Christopher Faulete6902cd2018-11-30 22:29:48 +0100668 for (; comp_type; comp_type = comp_type->next) {
669 if (ctx.value.len >= comp_type->name_len &&
670 strncasecmp(ctx.value.ptr, comp_type->name, comp_type->name_len) == 0)
671 /* this Content-Type should be compressed */
672 break;
673 }
674 /* this Content-Type should not be compressed */
675 if (comp_type == NULL)
676 goto fail;
677 }
678 }
679 else { /* no content-type header */
Olivier Houcharddb573e92023-04-05 17:32:36 +0200680 if ((s->be->comp && s->be->comp->types_res) ||
681 (strm_fe(s)->comp && strm_fe(s)->comp->types_res))
Christopher Faulete6902cd2018-11-30 22:29:48 +0100682 goto fail; /* a content-type was required */
683 }
684
685 /* limit compression rate */
686 if (global.comp_rate_lim > 0)
687 if (read_freq_ctr(&global.comp_bps_in) > global.comp_rate_lim)
688 goto fail;
689
690 /* limit cpu usage */
Willy Tarreau45c38e22021-09-30 18:28:49 +0200691 if (th_ctx->idle_pct < compress_min_idle)
Christopher Faulete6902cd2018-11-30 22:29:48 +0100692 goto fail;
693
694 /* initialize compression */
Olivier Houcharddfc11da2023-04-05 16:25:57 +0200695 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 +0100696 goto fail;
Christopher Faulete6902cd2018-11-30 22:29:48 +0100697 msg->flags |= HTTP_MSGF_COMPRESSING;
698 return 1;
699
Christopher Faulete6902cd2018-11-30 22:29:48 +0100700 fail:
Olivier Houcharddfc11da2023-04-05 16:25:57 +0200701 st->comp_algo[COMP_DIR_RES] = NULL;
Christopher Faulete6902cd2018-11-30 22:29:48 +0100702 return 0;
703}
704
Christopher Faulet3d97c902015-12-09 14:59:38 +0100705/***********************************************************************/
Christopher Faulete6902cd2018-11-30 22:29:48 +0100706static int
707htx_compression_buffer_init(struct htx *htx, struct buffer *out)
708{
709 /* output stream requires at least 10 bytes for the gzip header, plus
710 * at least 8 bytes for the gzip trailer (crc+len), plus a possible
711 * plus at most 5 bytes per 32kB block and 2 bytes to close the stream.
712 */
713 if (htx_free_space(htx) < 20 + 5 * ((htx->data + 32767) >> 15))
714 return -1;
715 b_reset(out);
716 return 0;
717}
718
Christopher Faulete6902cd2018-11-30 22:29:48 +0100719static int
720htx_compression_buffer_add_data(struct comp_state *st, const char *data, size_t len,
Olivier Houcharddfc11da2023-04-05 16:25:57 +0200721 struct buffer *out, int dir)
Christopher Faulete6902cd2018-11-30 22:29:48 +0100722{
Olivier Houcharddfc11da2023-04-05 16:25:57 +0200723
724 return st->comp_algo[dir]->add_data(st->comp_ctx[dir], data, len, out);
Christopher Faulete6902cd2018-11-30 22:29:48 +0100725}
726
Christopher Faulete6902cd2018-11-30 22:29:48 +0100727static int
Olivier Houcharddfc11da2023-04-05 16:25:57 +0200728htx_compression_buffer_end(struct comp_state *st, struct buffer *out, int end, int dir)
Christopher Faulete6902cd2018-11-30 22:29:48 +0100729{
Olivier Houcharddfc11da2023-04-05 16:25:57 +0200730
Christopher Faulete6902cd2018-11-30 22:29:48 +0100731 if (end)
Olivier Houcharddfc11da2023-04-05 16:25:57 +0200732 return st->comp_algo[dir]->finish(st->comp_ctx[dir], out);
Christopher Faulete6902cd2018-11-30 22:29:48 +0100733 else
Olivier Houcharddfc11da2023-04-05 16:25:57 +0200734 return st->comp_algo[dir]->flush(st->comp_ctx[dir], out);
Christopher Faulete6902cd2018-11-30 22:29:48 +0100735}
736
Christopher Faulet3d97c902015-12-09 14:59:38 +0100737
738/***********************************************************************/
Christopher Faulet92d36382015-11-05 13:35:03 +0100739struct flt_ops comp_ops = {
Christopher Faulete6902cd2018-11-30 22:29:48 +0100740 .init = comp_flt_init,
Christopher Faulet8ca3b4b2017-07-25 11:07:15 +0200741 .init_per_thread = comp_flt_init_per_thread,
742 .deinit_per_thread = comp_flt_deinit_per_thread,
Christopher Faulet92d36382015-11-05 13:35:03 +0100743
Christopher Faulet5e896512020-03-06 14:59:05 +0100744 .attach = comp_strm_init,
745 .detach = comp_strm_deinit,
746
Christopher Faulet3dc860d2017-09-15 11:39:36 +0200747 .channel_post_analyze = comp_http_post_analyze,
Christopher Faulet92d36382015-11-05 13:35:03 +0100748
Christopher Faulet1339d742016-05-11 16:48:33 +0200749 .http_headers = comp_http_headers,
Christopher Faulete6902cd2018-11-30 22:29:48 +0100750 .http_payload = comp_http_payload,
751 .http_end = comp_http_end,
Christopher Faulet92d36382015-11-05 13:35:03 +0100752};
753
Christopher Faulet3d97c902015-12-09 14:59:38 +0100754static int
755parse_compression_options(char **args, int section, struct proxy *proxy,
Willy Tarreau01825162021-03-09 09:53:46 +0100756 const struct proxy *defpx, const char *file, int line,
Christopher Faulet3d97c902015-12-09 14:59:38 +0100757 char **err)
758{
Christopher Faulet92d36382015-11-05 13:35:03 +0100759 struct comp *comp;
Christopher Faulet44d34bf2021-11-05 12:06:14 +0100760 int ret = 0;
Christopher Faulet3d97c902015-12-09 14:59:38 +0100761
762 if (proxy->comp == NULL) {
Vincent Bernat02779b62016-04-03 13:48:43 +0200763 comp = calloc(1, sizeof(*comp));
Olivier Houchardead43fe2023-04-06 00:33:48 +0200764 /* Always default to compress responses */
765 comp->flags = COMP_FL_DIR_RES;
Christopher Faulet3d97c902015-12-09 14:59:38 +0100766 proxy->comp = comp;
767 }
768 else
769 comp = proxy->comp;
770
Olivier Houcharddb573e92023-04-05 17:32:36 +0200771 if (strcmp(args[1], "algo") == 0 || strcmp(args[1], "algo-res") == 0) {
Christopher Faulet3d97c902015-12-09 14:59:38 +0100772 struct comp_ctx *ctx;
773 int cur_arg = 2;
774
775 if (!*args[cur_arg]) {
Christopher Faulet44d34bf2021-11-05 12:06:14 +0100776 memprintf(err, "parsing [%s:%d] : '%s' expects <algorithm>.",
Christopher Faulet3d97c902015-12-09 14:59:38 +0100777 file, line, args[0]);
Christopher Faulet44d34bf2021-11-05 12:06:14 +0100778 ret = -1;
779 goto end;
Christopher Faulet3d97c902015-12-09 14:59:38 +0100780 }
781 while (*(args[cur_arg])) {
Olivier Houcharddb573e92023-04-05 17:32:36 +0200782 int retval = comp_append_algo(&comp->algos_res, args[cur_arg]);
Remi Tricot-Le Breton6443bcc2021-05-17 10:35:08 +0200783 if (retval) {
784 if (retval < 0)
Christopher Faulet44d34bf2021-11-05 12:06:14 +0100785 memprintf(err, "'%s' : '%s' is not a supported algorithm.",
Remi Tricot-Le Breton6443bcc2021-05-17 10:35:08 +0200786 args[0], args[cur_arg]);
787 else
Christopher Faulet44d34bf2021-11-05 12:06:14 +0100788 memprintf(err, "'%s' : out of memory while parsing algo '%s'.",
Remi Tricot-Le Breton6443bcc2021-05-17 10:35:08 +0200789 args[0], args[cur_arg]);
Christopher Faulet44d34bf2021-11-05 12:06:14 +0100790 ret = -1;
791 goto end;
Christopher Faulet3d97c902015-12-09 14:59:38 +0100792 }
Remi Tricot-Le Breton6443bcc2021-05-17 10:35:08 +0200793
Olivier Houcharddb573e92023-04-05 17:32:36 +0200794 if (proxy->comp->algos_res->init(&ctx, 9) == 0)
795 proxy->comp->algos_res->end(&ctx);
Christopher Faulet3d97c902015-12-09 14:59:38 +0100796 else {
Christopher Faulet44d34bf2021-11-05 12:06:14 +0100797 memprintf(err, "'%s' : Can't init '%s' algorithm.",
Christopher Faulet3d97c902015-12-09 14:59:38 +0100798 args[0], args[cur_arg]);
Christopher Faulet44d34bf2021-11-05 12:06:14 +0100799 ret = -1;
800 goto end;
Christopher Faulet3d97c902015-12-09 14:59:38 +0100801 }
802 cur_arg++;
803 continue;
804 }
805 }
Olivier Houchardead43fe2023-04-06 00:33:48 +0200806 else if (strcmp(args[1], "algo-req") == 0) {
807 struct comp_ctx *ctx;
808 int retval = comp_append_algo(&comp->algo_req, args[2]);
809
810 if (retval) {
811 if (retval < 0)
812 memprintf(err, "'%s' : '%s' is not a supported algorithm.",
813 args[0], args[2]);
814 else
815 memprintf(err, "'%s' : out of memory while parsing algo '%s'.",
816 args[0], args[2]);
817 ret = -1;
818 goto end;
819 }
820
821 if (proxy->comp->algo_req->init(&ctx, 9) == 0)
822 proxy->comp->algo_req->end(&ctx);
823 else {
824 memprintf(err, "'%s' : Can't init '%s' algorithm.",
825 args[0], args[2]);
826 ret = -1;
827 goto end;
828 }
829 }
Christopher Faulet44d34bf2021-11-05 12:06:14 +0100830 else if (strcmp(args[1], "offload") == 0) {
831 if (proxy->cap & PR_CAP_DEF) {
832 memprintf(err, "'%s' : '%s' ignored in 'defaults' section.",
833 args[0], args[1]);
834 ret = 1;
835 }
Olivier Houchard3ce0f012023-04-03 22:22:24 +0200836 comp->flags |= COMP_FL_OFFLOAD;
Christopher Faulet44d34bf2021-11-05 12:06:14 +0100837 }
Olivier Houcharddb573e92023-04-05 17:32:36 +0200838 else if (strcmp(args[1], "type") == 0 || strcmp(args[1], "type-res") == 0) {
Christopher Faulet3d97c902015-12-09 14:59:38 +0100839 int cur_arg = 2;
840
841 if (!*args[cur_arg]) {
Christopher Faulet44d34bf2021-11-05 12:06:14 +0100842 memprintf(err, "'%s' expects <type>.", args[0]);
843 ret = -1;
844 goto end;
Christopher Faulet3d97c902015-12-09 14:59:38 +0100845 }
846 while (*(args[cur_arg])) {
Olivier Houcharddb573e92023-04-05 17:32:36 +0200847 if (comp_append_type(&comp->types_res, args[cur_arg])) {
Remi Tricot-Le Breton6443bcc2021-05-17 10:35:08 +0200848 memprintf(err, "'%s': out of memory.", args[0]);
Christopher Faulet44d34bf2021-11-05 12:06:14 +0100849 ret = -1;
850 goto end;
Remi Tricot-Le Breton6443bcc2021-05-17 10:35:08 +0200851 }
Christopher Faulet3d97c902015-12-09 14:59:38 +0100852 cur_arg++;
853 continue;
854 }
855 }
Olivier Houchardead43fe2023-04-06 00:33:48 +0200856 else if (strcmp(args[1], "type-req") == 0) {
857 int cur_arg = 2;
858
859 if (!*args[cur_arg]) {
860 memprintf(err, "'%s' expects <type>.", args[0]);
861 ret = -1;
862 goto end;
863 }
864 while (*(args[cur_arg])) {
865 if (comp_append_type(&comp->types_req, args[cur_arg])) {
866 memprintf(err, "'%s': out of memory.", args[0]);
867 ret = -1;
868 goto end;
869 }
870 cur_arg++;
871 continue;
872 }
873 }
874 else if (strcmp(args[1], "direction") == 0) {
875 if (!args[2]) {
876 memprintf(err, "'%s' expects 'request', 'response', or 'both'.", args[0]);
877 ret = -1;
878 goto end;
879 }
880 if (strcmp(args[2], "request") == 0) {
881 comp->flags &= ~COMP_FL_DIR_RES;
882 comp->flags |= COMP_FL_DIR_REQ;
883 } else if (strcmp(args[2], "response") == 0) {
884 comp->flags &= COMP_FL_DIR_REQ;
885 comp->flags |= COMP_FL_DIR_RES;
886 } else if (strcmp(args[2], "both") == 0)
887 comp->flags |= COMP_FL_DIR_REQ | COMP_FL_DIR_RES;
888 else {
889 memprintf(err, "'%s' expects 'request', 'response', or 'both'.", args[0]);
890 ret = -1;
891 goto end;
892 }
893 }
Christopher Faulet3d97c902015-12-09 14:59:38 +0100894 else {
Olivier Houchardead43fe2023-04-06 00:33:48 +0200895 memprintf(err, "'%s' expects 'algo', 'type' 'direction' or 'offload'",
Christopher Faulet3d97c902015-12-09 14:59:38 +0100896 args[0]);
Christopher Faulet44d34bf2021-11-05 12:06:14 +0100897 ret = -1;
898 goto end;
Christopher Faulet3d97c902015-12-09 14:59:38 +0100899 }
900
Christopher Faulet44d34bf2021-11-05 12:06:14 +0100901 end:
902 return ret;
Christopher Faulet3d97c902015-12-09 14:59:38 +0100903}
904
Christopher Faulet92d36382015-11-05 13:35:03 +0100905static int
906parse_http_comp_flt(char **args, int *cur_arg, struct proxy *px,
Thierry Fournier3610c392016-04-13 18:27:51 +0200907 struct flt_conf *fconf, char **err, void *private)
Christopher Faulet92d36382015-11-05 13:35:03 +0100908{
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100909 struct flt_conf *fc, *back;
Christopher Faulet92d36382015-11-05 13:35:03 +0100910
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100911 list_for_each_entry_safe(fc, back, &px->filter_configs, list) {
912 if (fc->id == http_comp_flt_id) {
Christopher Faulet92d36382015-11-05 13:35:03 +0100913 memprintf(err, "%s: Proxy supports only one compression filter\n", px->id);
914 return -1;
915 }
916 }
917
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100918 fconf->id = http_comp_flt_id;
919 fconf->conf = NULL;
920 fconf->ops = &comp_ops;
Christopher Faulet92d36382015-11-05 13:35:03 +0100921 (*cur_arg)++;
922
923 return 0;
924}
925
926
927int
Christopher Fauletc9df7f72018-12-10 16:14:04 +0100928check_implicit_http_comp_flt(struct proxy *proxy)
Christopher Faulet92d36382015-11-05 13:35:03 +0100929{
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100930 struct flt_conf *fconf;
Christopher Faulet27d93c32018-12-15 22:32:02 +0100931 int explicit = 0;
932 int comp = 0;
Christopher Faulet92d36382015-11-05 13:35:03 +0100933 int err = 0;
934
935 if (proxy->comp == NULL)
936 goto end;
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100937 if (!LIST_ISEMPTY(&proxy->filter_configs)) {
938 list_for_each_entry(fconf, &proxy->filter_configs, list) {
939 if (fconf->id == http_comp_flt_id)
Christopher Faulet27d93c32018-12-15 22:32:02 +0100940 comp = 1;
941 else if (fconf->id == cache_store_flt_id) {
942 if (comp) {
943 ha_alert("config: %s '%s': unable to enable the compression filter "
944 "before any cache filter.\n",
945 proxy_type_str(proxy), proxy->id);
946 err++;
947 goto end;
948 }
949 }
Christopher Faulet78fbb9f2019-08-11 23:11:03 +0200950 else if (fconf->id == fcgi_flt_id)
951 continue;
Christopher Faulet27d93c32018-12-15 22:32:02 +0100952 else
953 explicit = 1;
Christopher Faulet92d36382015-11-05 13:35:03 +0100954 }
Christopher Faulet27d93c32018-12-15 22:32:02 +0100955 }
956 if (comp)
957 goto end;
958 else if (explicit) {
959 ha_alert("config: %s '%s': require an explicit filter declaration to use "
960 "HTTP compression\n", proxy_type_str(proxy), proxy->id);
Christopher Faulet92d36382015-11-05 13:35:03 +0100961 err++;
962 goto end;
963 }
964
Christopher Faulet27d93c32018-12-15 22:32:02 +0100965 /* Implicit declaration of the compression filter is always the last
966 * one */
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100967 fconf = calloc(1, sizeof(*fconf));
968 if (!fconf) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100969 ha_alert("config: %s '%s': out of memory\n",
970 proxy_type_str(proxy), proxy->id);
Christopher Faulet92d36382015-11-05 13:35:03 +0100971 err++;
972 goto end;
973 }
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100974 fconf->id = http_comp_flt_id;
975 fconf->conf = NULL;
976 fconf->ops = &comp_ops;
Willy Tarreau2b718102021-04-21 07:32:39 +0200977 LIST_APPEND(&proxy->filter_configs, &fconf->list);
Christopher Faulet92d36382015-11-05 13:35:03 +0100978 end:
979 return err;
980}
981
982/*
983 * boolean, returns true if compression is used (either gzip or deflate) in the
984 * response.
985 */
Christopher Faulet3d97c902015-12-09 14:59:38 +0100986static int
Christopher Faulet92d36382015-11-05 13:35:03 +0100987smp_fetch_res_comp(const struct arg *args, struct sample *smp, const char *kw,
988 void *private)
Christopher Faulet3d97c902015-12-09 14:59:38 +0100989{
Willy Tarreaube508f12016-03-10 11:47:01 +0100990 struct http_txn *txn = smp->strm ? smp->strm->txn : NULL;
Christopher Faulet92d36382015-11-05 13:35:03 +0100991
Christopher Faulet3d97c902015-12-09 14:59:38 +0100992 smp->data.type = SMP_T_BOOL;
Christopher Faulet92d36382015-11-05 13:35:03 +0100993 smp->data.u.sint = (txn && (txn->rsp.flags & HTTP_MSGF_COMPRESSING));
Christopher Faulet3d97c902015-12-09 14:59:38 +0100994 return 1;
995}
996
Christopher Faulet92d36382015-11-05 13:35:03 +0100997/*
998 * string, returns algo
999 */
Christopher Faulet3d97c902015-12-09 14:59:38 +01001000static int
Christopher Faulet92d36382015-11-05 13:35:03 +01001001smp_fetch_res_comp_algo(const struct arg *args, struct sample *smp,
1002 const char *kw, void *private)
Christopher Faulet3d97c902015-12-09 14:59:38 +01001003{
Willy Tarreaube508f12016-03-10 11:47:01 +01001004 struct http_txn *txn = smp->strm ? smp->strm->txn : NULL;
Christopher Faulet92d36382015-11-05 13:35:03 +01001005 struct filter *filter;
1006 struct comp_state *st;
1007
Christopher Faulet03d85532017-09-15 10:14:43 +02001008 if (!txn || !(txn->rsp.flags & HTTP_MSGF_COMPRESSING))
Christopher Faulet3d97c902015-12-09 14:59:38 +01001009 return 0;
1010
Christopher Fauletfcf035c2015-12-03 11:48:03 +01001011 list_for_each_entry(filter, &strm_flt(smp->strm)->filters, list) {
Christopher Faulet443ea1a2016-02-04 13:40:26 +01001012 if (FLT_ID(filter) != http_comp_flt_id)
Christopher Faulet92d36382015-11-05 13:35:03 +01001013 continue;
1014
1015 if (!(st = filter->ctx))
1016 break;
1017
1018 smp->data.type = SMP_T_STR;
1019 smp->flags = SMP_F_CONST;
Olivier Houcharddfc11da2023-04-05 16:25:57 +02001020 smp->data.u.str.area = st->comp_algo[COMP_DIR_RES]->cfg_name;
1021 smp->data.u.str.data = st->comp_algo[COMP_DIR_RES]->cfg_name_len;
Christopher Faulet92d36382015-11-05 13:35:03 +01001022 return 1;
1023 }
1024 return 0;
Christopher Faulet3d97c902015-12-09 14:59:38 +01001025}
1026
1027/* Declare the config parser for "compression" keyword */
1028static struct cfg_kw_list cfg_kws = {ILH, {
1029 { CFG_LISTEN, "compression", parse_compression_options },
1030 { 0, NULL, NULL },
1031 }
1032};
1033
Willy Tarreau0108d902018-11-25 19:14:37 +01001034INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
1035
Christopher Faulet92d36382015-11-05 13:35:03 +01001036/* Declare the filter parser for "compression" keyword */
1037static struct flt_kw_list filter_kws = { "COMP", { }, {
Thierry Fournier3610c392016-04-13 18:27:51 +02001038 { "compression", parse_http_comp_flt, NULL },
1039 { NULL, NULL, NULL },
Christopher Faulet92d36382015-11-05 13:35:03 +01001040 }
1041};
1042
Willy Tarreau0108d902018-11-25 19:14:37 +01001043INITCALL1(STG_REGISTER, flt_register_keywords, &filter_kws);
1044
Christopher Faulet3d97c902015-12-09 14:59:38 +01001045/* Note: must not be declared <const> as its list will be overwritten */
1046static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
Christopher Faulet92d36382015-11-05 13:35:03 +01001047 { "res.comp", smp_fetch_res_comp, 0, NULL, SMP_T_BOOL, SMP_USE_HRSHP },
1048 { "res.comp_algo", smp_fetch_res_comp_algo, 0, NULL, SMP_T_STR, SMP_USE_HRSHP },
1049 { /* END */ },
1050 }
1051};
Christopher Faulet3d97c902015-12-09 14:59:38 +01001052
Willy Tarreau0108d902018-11-25 19:14:37 +01001053INITCALL1(STG_REGISTER, sample_register_fetches, &sample_fetch_keywords);