blob: 0360d94be2049cbe3bc1dc1da00cf1f84f405b55 [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 Tarreaua264d962020-06-04 22:29:18 +020023#include <haproxy/proxy-t.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 Fauletf4a4ef72018-12-07 17:39:53 +010028const char *http_comp_flt_id = "compression filter";
Christopher Faulet92d36382015-11-05 13:35:03 +010029
30struct flt_ops comp_ops;
31
Christopher Faulet92d36382015-11-05 13:35:03 +010032struct comp_state {
33 struct comp_ctx *comp_ctx; /* compression context */
34 struct comp_algo *comp_algo; /* compression algorithm if not NULL */
Christopher Faulet92d36382015-11-05 13:35:03 +010035};
36
Willy Tarreau8ceae722018-11-26 11:58:30 +010037/* Pools used to allocate comp_state structs */
38DECLARE_STATIC_POOL(pool_head_comp_state, "comp_state", sizeof(struct comp_state));
39
40static THREAD_LOCAL struct buffer tmpbuf;
41static THREAD_LOCAL struct buffer zbuf;
Willy Tarreau8ceae722018-11-26 11:58:30 +010042
Christopher Faulet92d36382015-11-05 13:35:03 +010043static int select_compression_request_header(struct comp_state *st,
44 struct stream *s,
45 struct http_msg *msg);
46static int select_compression_response_header(struct comp_state *st,
47 struct stream *s,
48 struct http_msg *msg);
Christopher Faulet27d93c32018-12-15 22:32:02 +010049static int set_compression_response_header(struct comp_state *st,
50 struct stream *s,
51 struct http_msg *msg);
Christopher Faulet92d36382015-11-05 13:35:03 +010052
Christopher Faulete6902cd2018-11-30 22:29:48 +010053static int htx_compression_buffer_init(struct htx *htx, struct buffer *out);
54static int htx_compression_buffer_add_data(struct comp_state *st, const char *data, size_t len,
55 struct buffer *out);
56static int htx_compression_buffer_end(struct comp_state *st, struct buffer *out, int end);
57
Christopher Faulet92d36382015-11-05 13:35:03 +010058/***********************************************************************/
59static int
Christopher Faulete6902cd2018-11-30 22:29:48 +010060comp_flt_init(struct proxy *px, struct flt_conf *fconf)
61{
Christopher Faulet6e540952018-12-03 22:43:41 +010062 fconf->flags |= FLT_CFG_FL_HTX;
Christopher Faulete6902cd2018-11-30 22:29:48 +010063 return 0;
64}
65
66static int
Christopher Faulet8ca3b4b2017-07-25 11:07:15 +020067comp_flt_init_per_thread(struct proxy *px, struct flt_conf *fconf)
Christopher Faulet92d36382015-11-05 13:35:03 +010068{
Willy Tarreauc9fa0482018-07-10 17:43:27 +020069 if (!tmpbuf.size && b_alloc(&tmpbuf) == NULL)
Christopher Fauletb77c5c22015-12-07 16:48:42 +010070 return -1;
Willy Tarreauc9fa0482018-07-10 17:43:27 +020071 if (!zbuf.size && b_alloc(&zbuf) == NULL)
Christopher Fauletb77c5c22015-12-07 16:48:42 +010072 return -1;
Christopher Faulet92d36382015-11-05 13:35:03 +010073 return 0;
74}
75
76static void
Christopher Faulet8ca3b4b2017-07-25 11:07:15 +020077comp_flt_deinit_per_thread(struct proxy *px, struct flt_conf *fconf)
Christopher Faulet92d36382015-11-05 13:35:03 +010078{
Willy Tarreauc9fa0482018-07-10 17:43:27 +020079 if (tmpbuf.size)
Christopher Faulet92d36382015-11-05 13:35:03 +010080 b_free(&tmpbuf);
Willy Tarreauc9fa0482018-07-10 17:43:27 +020081 if (zbuf.size)
Christopher Fauletb77c5c22015-12-07 16:48:42 +010082 b_free(&zbuf);
Christopher Faulet92d36382015-11-05 13:35:03 +010083}
84
85static int
Christopher Faulet5e896512020-03-06 14:59:05 +010086comp_strm_init(struct stream *s, struct filter *filter)
Christopher Faulet92d36382015-11-05 13:35:03 +010087{
Christopher Faulet5e896512020-03-06 14:59:05 +010088 struct comp_state *st;
Christopher Faulet8ca3b4b2017-07-25 11:07:15 +020089
Christopher Faulet5e896512020-03-06 14:59:05 +010090 st = pool_alloc_dirty(pool_head_comp_state);
91 if (st == NULL)
92 return -1;
Christopher Faulet92d36382015-11-05 13:35:03 +010093
Christopher Faulet5e896512020-03-06 14:59:05 +010094 st->comp_algo = NULL;
95 st->comp_ctx = NULL;
96 filter->ctx = st;
Christopher Faulet3dc860d2017-09-15 11:39:36 +020097
Christopher Faulet5e896512020-03-06 14:59:05 +010098 /* Register post-analyzer on AN_RES_WAIT_HTTP because we need to
99 * analyze response headers before http-response rules execution
100 * to be sure we can use res.comp and res.comp_algo sample
101 * fetches */
102 filter->post_analyzers |= AN_RES_WAIT_HTTP;
Christopher Faulet92d36382015-11-05 13:35:03 +0100103 return 1;
104}
105
Christopher Faulet5e896512020-03-06 14:59:05 +0100106static void
107comp_strm_deinit(struct stream *s, struct filter *filter)
Christopher Faulet92d36382015-11-05 13:35:03 +0100108{
109 struct comp_state *st = filter->ctx;
Christopher Faulet92d36382015-11-05 13:35:03 +0100110
Christopher Fauletd60b3cf2017-06-26 11:47:13 +0200111 if (!st)
Christopher Faulet5e896512020-03-06 14:59:05 +0100112 return;
Christopher Faulet92d36382015-11-05 13:35:03 +0100113
Christopher Faulet92d36382015-11-05 13:35:03 +0100114 /* release any possible compression context */
Christopher Fauletd60b3cf2017-06-26 11:47:13 +0200115 if (st->comp_algo)
116 st->comp_algo->end(&st->comp_ctx);
Willy Tarreaubafbe012017-11-24 17:34:44 +0100117 pool_free(pool_head_comp_state, st);
Christopher Faulet92d36382015-11-05 13:35:03 +0100118 filter->ctx = NULL;
Christopher Faulet92d36382015-11-05 13:35:03 +0100119}
120
121static int
Christopher Faulet1339d742016-05-11 16:48:33 +0200122comp_http_headers(struct stream *s, struct filter *filter, struct http_msg *msg)
123{
124 struct comp_state *st = filter->ctx;
125
126 if (!strm_fe(s)->comp && !s->be->comp)
127 goto end;
128
129 if (!(msg->chn->flags & CF_ISRESP))
130 select_compression_request_header(st, s, msg);
131 else {
Christopher Faulet3dc860d2017-09-15 11:39:36 +0200132 /* Response headers have already been checked in
133 * comp_http_post_analyze callback. */
Christopher Faulet1339d742016-05-11 16:48:33 +0200134 if (st->comp_algo) {
Christopher Faulet27d93c32018-12-15 22:32:02 +0100135 if (!set_compression_response_header(st, s, msg))
136 goto end;
Christopher Faulet1339d742016-05-11 16:48:33 +0200137 register_data_filter(s, msg->chn, filter);
Christopher Faulet1339d742016-05-11 16:48:33 +0200138 }
139 }
140
141 end:
142 return 1;
143}
144
145static int
Christopher Faulet3dc860d2017-09-15 11:39:36 +0200146comp_http_post_analyze(struct stream *s, struct filter *filter,
147 struct channel *chn, unsigned an_bit)
148{
149 struct http_txn *txn = s->txn;
150 struct http_msg *msg = &txn->rsp;
151 struct comp_state *st = filter->ctx;
152
153 if (an_bit != AN_RES_WAIT_HTTP)
154 goto end;
155
156 if (!strm_fe(s)->comp && !s->be->comp)
157 goto end;
158
159 select_compression_response_header(st, s, msg);
160
161 end:
162 return 1;
163}
164
165static int
Christopher Faulete6902cd2018-11-30 22:29:48 +0100166comp_http_payload(struct stream *s, struct filter *filter, struct http_msg *msg,
167 unsigned int offset, unsigned int len)
168{
169 struct comp_state *st = filter->ctx;
Christopher Faulet27ba2dc2018-12-05 11:53:24 +0100170 struct htx *htx = htxbuf(&msg->chn->buf);
Christopher Faulete6a62bf2020-03-02 16:20:05 +0100171 struct htx_ret htxret = htx_find_offset(htx, offset);
Christopher Faulete6902cd2018-11-30 22:29:48 +0100172 struct htx_blk *blk;
Christopher Faulete6902cd2018-11-30 22:29:48 +0100173 int ret, consumed = 0, to_forward = 0;
174
Christopher Faulete6a62bf2020-03-02 16:20:05 +0100175 blk = htxret.blk;
176 offset = htxret.ret;
177 for (; blk && len; blk = htx_get_next_blk(htx, blk)) {
Christopher Faulete6902cd2018-11-30 22:29:48 +0100178 enum htx_blk_type type = htx_get_blk_type(blk);
179 uint32_t sz = htx_get_blksz(blk);
180 struct ist v;
181
182 switch (type) {
183 case HTX_BLK_UNUSED:
184 break;
185
186 case HTX_BLK_DATA:
187 v = htx_get_blk_value(htx, blk);
188 v.ptr += offset;
189 v.len -= offset;
190 if (v.len > len)
191 v.len = len;
192 if (htx_compression_buffer_init(htx, &trash) < 0) {
193 msg->chn->flags |= CF_WAKE_WRITE;
194 goto end;
195 }
196 ret = htx_compression_buffer_add_data(st, v.ptr, v.len, &trash);
197 if (ret < 0)
198 goto error;
199 if (htx_compression_buffer_end(st, &trash, 0) < 0)
200 goto error;
201 len -= ret;
202 consumed += ret;
203 to_forward += b_data(&trash);
204 if (ret == sz && !b_data(&trash)) {
205 offset = 0;
206 blk = htx_remove_blk(htx, blk);
207 continue;
208 }
209 v.len = ret;
210 blk = htx_replace_blk_value(htx, blk, v, ist2(b_head(&trash), b_data(&trash)));
211 break;
212
Christopher Faulete6902cd2018-11-30 22:29:48 +0100213 case HTX_BLK_TLR:
Christopher Faulet2d7c5392019-06-03 10:41:26 +0200214 case HTX_BLK_EOT:
Christopher Faulete6902cd2018-11-30 22:29:48 +0100215 case HTX_BLK_EOM:
216 if (msg->flags & HTTP_MSGF_COMPRESSING) {
217 if (htx_compression_buffer_init(htx, &trash) < 0) {
218 msg->chn->flags |= CF_WAKE_WRITE;
219 goto end;
220 }
221 if (htx_compression_buffer_end(st, &trash, 1) < 0)
222 goto error;
Christopher Fauletd238ae32018-12-21 15:10:25 +0100223 if (b_data(&trash)) {
Christopher Faulet86bc8df2019-06-11 10:38:38 +0200224 struct htx_blk *last = htx_add_last_data(htx, ist2(b_head(&trash), b_data(&trash)));
225 if (!last)
226 goto error;
227 blk = htx_get_next_blk(htx, last);
Christopher Fauletd238ae32018-12-21 15:10:25 +0100228 if (!blk)
229 goto error;
230 to_forward += b_data(&trash);
231 }
Christopher Faulete6902cd2018-11-30 22:29:48 +0100232 msg->flags &= ~HTTP_MSGF_COMPRESSING;
233 /* We let the mux add last empty chunk and empty trailers */
234 }
235 /* fall through */
236
237 default:
238 sz -= offset;
239 if (sz > len)
240 sz = len;
241 consumed += sz;
242 to_forward += sz;
243 len -= sz;
244 break;
245 }
246
247 offset = 0;
Christopher Faulete6902cd2018-11-30 22:29:48 +0100248 }
249
250 end:
251 if (to_forward != consumed)
252 flt_update_offsets(filter, msg->chn, to_forward - consumed);
253
254 if (st->comp_ctx && st->comp_ctx->cur_lvl > 0) {
Willy Tarreauef6fd852019-02-04 11:48:03 +0100255 update_freq_ctr(&global.comp_bps_in, consumed);
Olivier Houchard43da3432019-03-08 18:50:27 +0100256 _HA_ATOMIC_ADD(&strm_fe(s)->fe_counters.comp_in, consumed);
257 _HA_ATOMIC_ADD(&s->be->be_counters.comp_in, consumed);
Christopher Faulete6902cd2018-11-30 22:29:48 +0100258 update_freq_ctr(&global.comp_bps_out, to_forward);
Olivier Houchard43da3432019-03-08 18:50:27 +0100259 _HA_ATOMIC_ADD(&strm_fe(s)->fe_counters.comp_out, to_forward);
260 _HA_ATOMIC_ADD(&s->be->be_counters.comp_out, to_forward);
Willy Tarreauef6fd852019-02-04 11:48:03 +0100261 } else {
Olivier Houchard43da3432019-03-08 18:50:27 +0100262 _HA_ATOMIC_ADD(&strm_fe(s)->fe_counters.comp_byp, consumed);
263 _HA_ATOMIC_ADD(&s->be->be_counters.comp_byp, consumed);
Christopher Faulete6902cd2018-11-30 22:29:48 +0100264 }
265 return to_forward;
266
267 error:
268 return -1;
269}
270
Christopher Faulet2fb28802015-12-01 10:40:57 +0100271
Christopher Faulet92d36382015-11-05 13:35:03 +0100272static int
Christopher Fauletd60b3cf2017-06-26 11:47:13 +0200273comp_http_end(struct stream *s, struct filter *filter,
274 struct http_msg *msg)
275{
276 struct comp_state *st = filter->ctx;
277
278 if (!(msg->chn->flags & CF_ISRESP) || !st || !st->comp_algo)
279 goto end;
280
281 if (strm_fe(s)->mode == PR_MODE_HTTP)
Olivier Houchard43da3432019-03-08 18:50:27 +0100282 _HA_ATOMIC_ADD(&strm_fe(s)->fe_counters.p.http.comp_rsp, 1);
Christopher Fauletd60b3cf2017-06-26 11:47:13 +0200283 if ((s->flags & SF_BE_ASSIGNED) && (s->be->mode == PR_MODE_HTTP))
Olivier Houchard43da3432019-03-08 18:50:27 +0100284 _HA_ATOMIC_ADD(&s->be->be_counters.p.http.comp_rsp, 1);
Christopher Fauletd60b3cf2017-06-26 11:47:13 +0200285 end:
286 return 1;
287}
Christopher Faulet27d93c32018-12-15 22:32:02 +0100288
Christopher Faulet89f2b162019-07-15 21:16:04 +0200289/***********************************************************************/
Christopher Faulet27d93c32018-12-15 22:32:02 +0100290static int
Christopher Faulet89f2b162019-07-15 21:16:04 +0200291set_compression_response_header(struct comp_state *st, struct stream *s, struct http_msg *msg)
Christopher Faulet27d93c32018-12-15 22:32:02 +0100292{
293 struct htx *htx = htxbuf(&msg->chn->buf);
Tim Duesterhusb229f012019-01-29 16:38:56 +0100294 struct http_hdr_ctx ctx;
Christopher Faulet27d93c32018-12-15 22:32:02 +0100295
296 /*
297 * Add Content-Encoding header when it's not identity encoding.
298 * RFC 2616 : Identity encoding: This content-coding is used only in the
299 * Accept-Encoding header, and SHOULD NOT be used in the Content-Encoding
300 * header.
301 */
302 if (st->comp_algo->cfg_name_len != 8 || memcmp(st->comp_algo->cfg_name, "identity", 8) != 0) {
303 struct ist v = ist2(st->comp_algo->ua_name, st->comp_algo->ua_name_len);
304
305 if (!http_add_header(htx, ist("Content-Encoding"), v))
306 goto error;
307 }
308
309 /* remove Content-Length header */
310 if (msg->flags & HTTP_MSGF_CNT_LEN) {
Christopher Faulet27d93c32018-12-15 22:32:02 +0100311 ctx.blk = NULL;
312 while (http_find_header(htx, ist("Content-Length"), &ctx, 1))
313 http_remove_header(htx, &ctx);
314 }
315
316 /* add "Transfer-Encoding: chunked" header */
317 if (!(msg->flags & HTTP_MSGF_TE_CHNK)) {
318 if (!http_add_header(htx, ist("Transfer-Encoding"), ist("chunked")))
319 goto error;
320 }
321
Tim Duesterhusb229f012019-01-29 16:38:56 +0100322 /* convert "ETag" header to a weak ETag */
323 ctx.blk = NULL;
324 if (http_find_header(htx, ist("ETag"), &ctx, 1)) {
325 if (ctx.value.ptr[0] == '"') {
326 /* This a strong ETag. Convert it to a weak one. */
327 struct ist v = ist2(trash.area, 0);
328 if (istcat(&v, ist("W/"), trash.size) == -1 || istcat(&v, ctx.value, trash.size) == -1)
329 goto error;
330
331 if (!http_replace_header_value(htx, &ctx, v))
332 goto error;
333 }
334 }
335
Tim Duesterhus721d6862019-06-17 16:10:07 +0200336 if (!http_add_header(htx, ist("Vary"), ist("Accept-Encoding")))
337 goto error;
338
Christopher Faulet27d93c32018-12-15 22:32:02 +0100339 return 1;
340
341 error:
342 st->comp_algo->end(&st->comp_ctx);
343 st->comp_algo = NULL;
344 return 0;
345}
346
Christopher Faulet3d97c902015-12-09 14:59:38 +0100347/*
348 * Selects a compression algorithm depending on the client request.
349 */
Christopher Faulete6902cd2018-11-30 22:29:48 +0100350static int
Christopher Faulet89f2b162019-07-15 21:16:04 +0200351select_compression_request_header(struct comp_state *st, struct stream *s, struct http_msg *msg)
Christopher Faulete6902cd2018-11-30 22:29:48 +0100352{
Christopher Faulet27ba2dc2018-12-05 11:53:24 +0100353 struct htx *htx = htxbuf(&msg->chn->buf);
Christopher Faulete6902cd2018-11-30 22:29:48 +0100354 struct http_hdr_ctx ctx;
355 struct comp_algo *comp_algo = NULL;
356 struct comp_algo *comp_algo_back = NULL;
357
358 /* Disable compression for older user agents announcing themselves as "Mozilla/4"
359 * unless they are known good (MSIE 6 with XP SP2, or MSIE 7 and later).
360 * See http://zoompf.com/2012/02/lose-the-wait-http-compression for more details.
361 */
362 ctx.blk = NULL;
363 if (http_find_header(htx, ist("User-Agent"), &ctx, 1) &&
364 ctx.value.len >= 9 &&
365 memcmp(ctx.value.ptr, "Mozilla/4", 9) == 0 &&
366 (ctx.value.len < 31 ||
367 memcmp(ctx.value.ptr + 25, "MSIE ", 5) != 0 ||
368 *(ctx.value.ptr + 30) < '6' ||
369 (*(ctx.value.ptr + 30) == '6' &&
370 (ctx.value.len < 54 || memcmp(ctx.value.ptr + 51, "SV1", 3) != 0)))) {
371 st->comp_algo = NULL;
372 return 0;
373 }
374
375 /* search for the algo in the backend in priority or the frontend */
376 if ((s->be->comp && (comp_algo_back = s->be->comp->algos)) ||
377 (strm_fe(s)->comp && (comp_algo_back = strm_fe(s)->comp->algos))) {
378 int best_q = 0;
379
380 ctx.blk = NULL;
381 while (http_find_header(htx, ist("Accept-Encoding"), &ctx, 0)) {
382 const char *qval;
383 int q;
384 int toklen;
385
386 /* try to isolate the token from the optional q-value */
387 toklen = 0;
388 while (toklen < ctx.value.len && HTTP_IS_TOKEN(*(ctx.value.ptr + toklen)))
389 toklen++;
390
391 qval = ctx.value.ptr + toklen;
392 while (1) {
393 while (qval < ctx.value.ptr + ctx.value.len && HTTP_IS_LWS(*qval))
394 qval++;
395
396 if (qval >= ctx.value.ptr + ctx.value.len || *qval != ';') {
397 qval = NULL;
398 break;
399 }
400 qval++;
401
402 while (qval < ctx.value.ptr + ctx.value.len && HTTP_IS_LWS(*qval))
403 qval++;
404
405 if (qval >= ctx.value.ptr + ctx.value.len) {
406 qval = NULL;
407 break;
408 }
409 if (strncmp(qval, "q=", MIN(ctx.value.ptr + ctx.value.len - qval, 2)) == 0)
410 break;
411
412 while (qval < ctx.value.ptr + ctx.value.len && *qval != ';')
413 qval++;
414 }
415
416 /* here we have qval pointing to the first "q=" attribute or NULL if not found */
417 q = qval ? http_parse_qvalue(qval + 2, NULL) : 1000;
418
419 if (q <= best_q)
420 continue;
421
422 for (comp_algo = comp_algo_back; comp_algo; comp_algo = comp_algo->next) {
423 if (*(ctx.value.ptr) == '*' ||
424 word_match(ctx.value.ptr, toklen, comp_algo->ua_name, comp_algo->ua_name_len)) {
425 st->comp_algo = comp_algo;
426 best_q = q;
427 break;
428 }
429 }
430 }
431 }
432
433 /* remove all occurrences of the header when "compression offload" is set */
434 if (st->comp_algo) {
435 if ((s->be->comp && s->be->comp->offload) ||
436 (strm_fe(s)->comp && strm_fe(s)->comp->offload)) {
437 http_remove_header(htx, &ctx);
438 ctx.blk = NULL;
439 while (http_find_header(htx, ist("Accept-Encoding"), &ctx, 1))
440 http_remove_header(htx, &ctx);
441 }
Christopher Faulet3d97c902015-12-09 14:59:38 +0100442 return 1;
443 }
444
445 /* identity is implicit does not require headers */
Christopher Faulet92d36382015-11-05 13:35:03 +0100446 if ((s->be->comp && (comp_algo_back = s->be->comp->algos)) ||
447 (strm_fe(s)->comp && (comp_algo_back = strm_fe(s)->comp->algos))) {
Christopher Faulet3d97c902015-12-09 14:59:38 +0100448 for (comp_algo = comp_algo_back; comp_algo; comp_algo = comp_algo->next) {
449 if (comp_algo->cfg_name_len == 8 && memcmp(comp_algo->cfg_name, "identity", 8) == 0) {
Christopher Faulet92d36382015-11-05 13:35:03 +0100450 st->comp_algo = comp_algo;
Christopher Faulet3d97c902015-12-09 14:59:38 +0100451 return 1;
452 }
453 }
454 }
455
Christopher Faulet92d36382015-11-05 13:35:03 +0100456 st->comp_algo = NULL;
Christopher Faulet3d97c902015-12-09 14:59:38 +0100457 return 0;
458}
459
460/*
Ilya Shipitsin46a030c2020-07-05 16:36:08 +0500461 * Selects a compression algorithm depending of the server response.
Christopher Faulet3d97c902015-12-09 14:59:38 +0100462 */
Christopher Faulet92d36382015-11-05 13:35:03 +0100463static int
Christopher Faulet89f2b162019-07-15 21:16:04 +0200464select_compression_response_header(struct comp_state *st, struct stream *s, struct http_msg *msg)
Christopher Faulete6902cd2018-11-30 22:29:48 +0100465{
Christopher Faulet27ba2dc2018-12-05 11:53:24 +0100466 struct htx *htx = htxbuf(&msg->chn->buf);
Christopher Faulete6902cd2018-11-30 22:29:48 +0100467 struct http_txn *txn = s->txn;
468 struct http_hdr_ctx ctx;
469 struct comp_type *comp_type;
470
471 /* no common compression algorithm was found in request header */
472 if (st->comp_algo == NULL)
473 goto fail;
474
Christopher Faulet1d3613a2019-01-07 14:41:59 +0100475 /* compression already in progress */
476 if (msg->flags & HTTP_MSGF_COMPRESSING)
477 goto fail;
478
Christopher Faulete6902cd2018-11-30 22:29:48 +0100479 /* HTTP < 1.1 should not be compressed */
480 if (!(msg->flags & HTTP_MSGF_VER_11) || !(txn->req.flags & HTTP_MSGF_VER_11))
481 goto fail;
482
483 if (txn->meth == HTTP_METH_HEAD)
484 goto fail;
485
486 /* compress 200,201,202,203 responses only */
487 if ((txn->status != 200) &&
488 (txn->status != 201) &&
489 (txn->status != 202) &&
490 (txn->status != 203))
491 goto fail;
492
Christopher Fauletc963eb22018-12-21 14:53:54 +0100493 if (!(msg->flags & HTTP_MSGF_XFER_LEN) || msg->flags & HTTP_MSGF_BODYLESS)
Christopher Faulete6902cd2018-11-30 22:29:48 +0100494 goto fail;
495
496 /* content is already compressed */
497 ctx.blk = NULL;
498 if (http_find_header(htx, ist("Content-Encoding"), &ctx, 1))
499 goto fail;
500
501 /* no compression when Cache-Control: no-transform is present in the message */
502 ctx.blk = NULL;
503 while (http_find_header(htx, ist("Cache-Control"), &ctx, 0)) {
504 if (word_match(ctx.value.ptr, ctx.value.len, "no-transform", 12))
505 goto fail;
506 }
507
Tim Duesterhusb229f012019-01-29 16:38:56 +0100508 /* no compression when ETag is malformed */
509 ctx.blk = NULL;
510 if (http_find_header(htx, ist("ETag"), &ctx, 1)) {
Tim Duesterhus6414cd12020-09-01 18:32:35 +0200511 if (http_get_etag_type(ctx.value) == ETAG_INVALID)
Tim Duesterhusb229f012019-01-29 16:38:56 +0100512 goto fail;
Tim Duesterhusb229f012019-01-29 16:38:56 +0100513 }
514 /* no compression when multiple ETags are present
515 * Note: Do not reset ctx.blk!
516 */
517 if (http_find_header(htx, ist("ETag"), &ctx, 1))
518 goto fail;
519
Christopher Faulete6902cd2018-11-30 22:29:48 +0100520 comp_type = NULL;
521
522 /* we don't want to compress multipart content-types, nor content-types that are
523 * not listed in the "compression type" directive if any. If no content-type was
524 * found but configuration requires one, we don't compress either. Backend has
525 * the priority.
526 */
527 ctx.blk = NULL;
528 if (http_find_header(htx, ist("Content-Type"), &ctx, 1)) {
529 if (ctx.value.len >= 9 && strncasecmp("multipart", ctx.value.ptr, 9) == 0)
530 goto fail;
531
532 if ((s->be->comp && (comp_type = s->be->comp->types)) ||
533 (strm_fe(s)->comp && (comp_type = strm_fe(s)->comp->types))) {
534 for (; comp_type; comp_type = comp_type->next) {
535 if (ctx.value.len >= comp_type->name_len &&
536 strncasecmp(ctx.value.ptr, comp_type->name, comp_type->name_len) == 0)
537 /* this Content-Type should be compressed */
538 break;
539 }
540 /* this Content-Type should not be compressed */
541 if (comp_type == NULL)
542 goto fail;
543 }
544 }
545 else { /* no content-type header */
546 if ((s->be->comp && s->be->comp->types) ||
547 (strm_fe(s)->comp && strm_fe(s)->comp->types))
548 goto fail; /* a content-type was required */
549 }
550
551 /* limit compression rate */
552 if (global.comp_rate_lim > 0)
553 if (read_freq_ctr(&global.comp_bps_in) > global.comp_rate_lim)
554 goto fail;
555
556 /* limit cpu usage */
Willy Tarreau81036f22019-05-20 19:24:50 +0200557 if (ti->idle_pct < compress_min_idle)
Christopher Faulete6902cd2018-11-30 22:29:48 +0100558 goto fail;
559
560 /* initialize compression */
561 if (st->comp_algo->init(&st->comp_ctx, global.tune.comp_maxlevel) < 0)
562 goto fail;
Christopher Faulete6902cd2018-11-30 22:29:48 +0100563 msg->flags |= HTTP_MSGF_COMPRESSING;
564 return 1;
565
Christopher Faulete6902cd2018-11-30 22:29:48 +0100566 fail:
567 st->comp_algo = NULL;
568 return 0;
569}
570
Christopher Faulet3d97c902015-12-09 14:59:38 +0100571/***********************************************************************/
Christopher Faulete6902cd2018-11-30 22:29:48 +0100572static int
573htx_compression_buffer_init(struct htx *htx, struct buffer *out)
574{
575 /* output stream requires at least 10 bytes for the gzip header, plus
576 * at least 8 bytes for the gzip trailer (crc+len), plus a possible
577 * plus at most 5 bytes per 32kB block and 2 bytes to close the stream.
578 */
579 if (htx_free_space(htx) < 20 + 5 * ((htx->data + 32767) >> 15))
580 return -1;
581 b_reset(out);
582 return 0;
583}
584
Christopher Faulete6902cd2018-11-30 22:29:48 +0100585static int
586htx_compression_buffer_add_data(struct comp_state *st, const char *data, size_t len,
587 struct buffer *out)
588{
589 return st->comp_algo->add_data(st->comp_ctx, data, len, out);
590}
591
Christopher Faulete6902cd2018-11-30 22:29:48 +0100592static int
593htx_compression_buffer_end(struct comp_state *st, struct buffer *out, int end)
594{
595 if (end)
596 return st->comp_algo->finish(st->comp_ctx, out);
597 else
598 return st->comp_algo->flush(st->comp_ctx, out);
599}
600
Christopher Faulet3d97c902015-12-09 14:59:38 +0100601
602/***********************************************************************/
Christopher Faulet92d36382015-11-05 13:35:03 +0100603struct flt_ops comp_ops = {
Christopher Faulete6902cd2018-11-30 22:29:48 +0100604 .init = comp_flt_init,
Christopher Faulet8ca3b4b2017-07-25 11:07:15 +0200605 .init_per_thread = comp_flt_init_per_thread,
606 .deinit_per_thread = comp_flt_deinit_per_thread,
Christopher Faulet92d36382015-11-05 13:35:03 +0100607
Christopher Faulet5e896512020-03-06 14:59:05 +0100608 .attach = comp_strm_init,
609 .detach = comp_strm_deinit,
610
Christopher Faulet3dc860d2017-09-15 11:39:36 +0200611 .channel_post_analyze = comp_http_post_analyze,
Christopher Faulet92d36382015-11-05 13:35:03 +0100612
Christopher Faulet1339d742016-05-11 16:48:33 +0200613 .http_headers = comp_http_headers,
Christopher Faulete6902cd2018-11-30 22:29:48 +0100614 .http_payload = comp_http_payload,
615 .http_end = comp_http_end,
Christopher Faulet92d36382015-11-05 13:35:03 +0100616};
617
Christopher Faulet3d97c902015-12-09 14:59:38 +0100618static int
619parse_compression_options(char **args, int section, struct proxy *proxy,
620 struct proxy *defpx, const char *file, int line,
621 char **err)
622{
Christopher Faulet92d36382015-11-05 13:35:03 +0100623 struct comp *comp;
Christopher Faulet3d97c902015-12-09 14:59:38 +0100624
625 if (proxy->comp == NULL) {
Vincent Bernat02779b62016-04-03 13:48:43 +0200626 comp = calloc(1, sizeof(*comp));
Christopher Faulet3d97c902015-12-09 14:59:38 +0100627 proxy->comp = comp;
628 }
629 else
630 comp = proxy->comp;
631
632 if (!strcmp(args[1], "algo")) {
633 struct comp_ctx *ctx;
634 int cur_arg = 2;
635
636 if (!*args[cur_arg]) {
637 memprintf(err, "parsing [%s:%d] : '%s' expects <algorithm>\n",
638 file, line, args[0]);
639 return -1;
640 }
641 while (*(args[cur_arg])) {
642 if (comp_append_algo(comp, args[cur_arg]) < 0) {
643 memprintf(err, "'%s' : '%s' is not a supported algorithm.\n",
644 args[0], args[cur_arg]);
645 return -1;
646 }
647 if (proxy->comp->algos->init(&ctx, 9) == 0)
648 proxy->comp->algos->end(&ctx);
649 else {
650 memprintf(err, "'%s' : Can't init '%s' algorithm.\n",
651 args[0], args[cur_arg]);
652 return -1;
653 }
654 cur_arg++;
655 continue;
656 }
657 }
658 else if (!strcmp(args[1], "offload"))
659 comp->offload = 1;
660 else if (!strcmp(args[1], "type")) {
661 int cur_arg = 2;
662
663 if (!*args[cur_arg]) {
664 memprintf(err, "'%s' expects <type>\n", args[0]);
665 return -1;
666 }
667 while (*(args[cur_arg])) {
668 comp_append_type(comp, args[cur_arg]);
669 cur_arg++;
670 continue;
671 }
672 }
673 else {
674 memprintf(err, "'%s' expects 'algo', 'type' or 'offload'\n",
675 args[0]);
676 return -1;
677 }
678
679 return 0;
680}
681
Christopher Faulet92d36382015-11-05 13:35:03 +0100682static int
683parse_http_comp_flt(char **args, int *cur_arg, struct proxy *px,
Thierry Fournier3610c392016-04-13 18:27:51 +0200684 struct flt_conf *fconf, char **err, void *private)
Christopher Faulet92d36382015-11-05 13:35:03 +0100685{
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100686 struct flt_conf *fc, *back;
Christopher Faulet92d36382015-11-05 13:35:03 +0100687
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100688 list_for_each_entry_safe(fc, back, &px->filter_configs, list) {
689 if (fc->id == http_comp_flt_id) {
Christopher Faulet92d36382015-11-05 13:35:03 +0100690 memprintf(err, "%s: Proxy supports only one compression filter\n", px->id);
691 return -1;
692 }
693 }
694
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100695 fconf->id = http_comp_flt_id;
696 fconf->conf = NULL;
697 fconf->ops = &comp_ops;
Christopher Faulet92d36382015-11-05 13:35:03 +0100698 (*cur_arg)++;
699
700 return 0;
701}
702
703
704int
Christopher Fauletc9df7f72018-12-10 16:14:04 +0100705check_implicit_http_comp_flt(struct proxy *proxy)
Christopher Faulet92d36382015-11-05 13:35:03 +0100706{
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100707 struct flt_conf *fconf;
Christopher Faulet27d93c32018-12-15 22:32:02 +0100708 int explicit = 0;
709 int comp = 0;
Christopher Faulet92d36382015-11-05 13:35:03 +0100710 int err = 0;
711
712 if (proxy->comp == NULL)
713 goto end;
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100714 if (!LIST_ISEMPTY(&proxy->filter_configs)) {
715 list_for_each_entry(fconf, &proxy->filter_configs, list) {
716 if (fconf->id == http_comp_flt_id)
Christopher Faulet27d93c32018-12-15 22:32:02 +0100717 comp = 1;
718 else if (fconf->id == cache_store_flt_id) {
719 if (comp) {
720 ha_alert("config: %s '%s': unable to enable the compression filter "
721 "before any cache filter.\n",
722 proxy_type_str(proxy), proxy->id);
723 err++;
724 goto end;
725 }
726 }
Christopher Faulet78fbb9f2019-08-11 23:11:03 +0200727 else if (fconf->id == fcgi_flt_id)
728 continue;
Christopher Faulet27d93c32018-12-15 22:32:02 +0100729 else
730 explicit = 1;
Christopher Faulet92d36382015-11-05 13:35:03 +0100731 }
Christopher Faulet27d93c32018-12-15 22:32:02 +0100732 }
733 if (comp)
734 goto end;
735 else if (explicit) {
736 ha_alert("config: %s '%s': require an explicit filter declaration to use "
737 "HTTP compression\n", proxy_type_str(proxy), proxy->id);
Christopher Faulet92d36382015-11-05 13:35:03 +0100738 err++;
739 goto end;
740 }
741
Christopher Faulet27d93c32018-12-15 22:32:02 +0100742 /* Implicit declaration of the compression filter is always the last
743 * one */
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100744 fconf = calloc(1, sizeof(*fconf));
745 if (!fconf) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100746 ha_alert("config: %s '%s': out of memory\n",
747 proxy_type_str(proxy), proxy->id);
Christopher Faulet92d36382015-11-05 13:35:03 +0100748 err++;
749 goto end;
750 }
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100751 fconf->id = http_comp_flt_id;
752 fconf->conf = NULL;
753 fconf->ops = &comp_ops;
754 LIST_ADDQ(&proxy->filter_configs, &fconf->list);
Christopher Faulet92d36382015-11-05 13:35:03 +0100755 end:
756 return err;
757}
758
759/*
760 * boolean, returns true if compression is used (either gzip or deflate) in the
761 * response.
762 */
Christopher Faulet3d97c902015-12-09 14:59:38 +0100763static int
Christopher Faulet92d36382015-11-05 13:35:03 +0100764smp_fetch_res_comp(const struct arg *args, struct sample *smp, const char *kw,
765 void *private)
Christopher Faulet3d97c902015-12-09 14:59:38 +0100766{
Willy Tarreaube508f12016-03-10 11:47:01 +0100767 struct http_txn *txn = smp->strm ? smp->strm->txn : NULL;
Christopher Faulet92d36382015-11-05 13:35:03 +0100768
Christopher Faulet3d97c902015-12-09 14:59:38 +0100769 smp->data.type = SMP_T_BOOL;
Christopher Faulet92d36382015-11-05 13:35:03 +0100770 smp->data.u.sint = (txn && (txn->rsp.flags & HTTP_MSGF_COMPRESSING));
Christopher Faulet3d97c902015-12-09 14:59:38 +0100771 return 1;
772}
773
Christopher Faulet92d36382015-11-05 13:35:03 +0100774/*
775 * string, returns algo
776 */
Christopher Faulet3d97c902015-12-09 14:59:38 +0100777static int
Christopher Faulet92d36382015-11-05 13:35:03 +0100778smp_fetch_res_comp_algo(const struct arg *args, struct sample *smp,
779 const char *kw, void *private)
Christopher Faulet3d97c902015-12-09 14:59:38 +0100780{
Willy Tarreaube508f12016-03-10 11:47:01 +0100781 struct http_txn *txn = smp->strm ? smp->strm->txn : NULL;
Christopher Faulet92d36382015-11-05 13:35:03 +0100782 struct filter *filter;
783 struct comp_state *st;
784
Christopher Faulet03d85532017-09-15 10:14:43 +0200785 if (!txn || !(txn->rsp.flags & HTTP_MSGF_COMPRESSING))
Christopher Faulet3d97c902015-12-09 14:59:38 +0100786 return 0;
787
Christopher Fauletfcf035c2015-12-03 11:48:03 +0100788 list_for_each_entry(filter, &strm_flt(smp->strm)->filters, list) {
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100789 if (FLT_ID(filter) != http_comp_flt_id)
Christopher Faulet92d36382015-11-05 13:35:03 +0100790 continue;
791
792 if (!(st = filter->ctx))
793 break;
794
795 smp->data.type = SMP_T_STR;
796 smp->flags = SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200797 smp->data.u.str.area = st->comp_algo->cfg_name;
798 smp->data.u.str.data = st->comp_algo->cfg_name_len;
Christopher Faulet92d36382015-11-05 13:35:03 +0100799 return 1;
800 }
801 return 0;
Christopher Faulet3d97c902015-12-09 14:59:38 +0100802}
803
804/* Declare the config parser for "compression" keyword */
805static struct cfg_kw_list cfg_kws = {ILH, {
806 { CFG_LISTEN, "compression", parse_compression_options },
807 { 0, NULL, NULL },
808 }
809};
810
Willy Tarreau0108d902018-11-25 19:14:37 +0100811INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
812
Christopher Faulet92d36382015-11-05 13:35:03 +0100813/* Declare the filter parser for "compression" keyword */
814static struct flt_kw_list filter_kws = { "COMP", { }, {
Thierry Fournier3610c392016-04-13 18:27:51 +0200815 { "compression", parse_http_comp_flt, NULL },
816 { NULL, NULL, NULL },
Christopher Faulet92d36382015-11-05 13:35:03 +0100817 }
818};
819
Willy Tarreau0108d902018-11-25 19:14:37 +0100820INITCALL1(STG_REGISTER, flt_register_keywords, &filter_kws);
821
Christopher Faulet3d97c902015-12-09 14:59:38 +0100822/* Note: must not be declared <const> as its list will be overwritten */
823static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
Christopher Faulet92d36382015-11-05 13:35:03 +0100824 { "res.comp", smp_fetch_res_comp, 0, NULL, SMP_T_BOOL, SMP_USE_HRSHP },
825 { "res.comp_algo", smp_fetch_res_comp_algo, 0, NULL, SMP_T_STR, SMP_USE_HRSHP },
826 { /* END */ },
827 }
828};
Christopher Faulet3d97c902015-12-09 14:59:38 +0100829
Willy Tarreau0108d902018-11-25 19:14:37 +0100830INITCALL1(STG_REGISTER, sample_register_fetches, &sample_fetch_keywords);