blob: b456fcd7a4afe29bd3fba8519545552887863a93 [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
13#include <common/buffer.h>
14#include <common/cfgparse.h>
Willy Tarreaub96b77e2018-12-11 10:22:41 +010015#include <common/htx.h>
Willy Tarreau0108d902018-11-25 19:14:37 +010016#include <common/initcall.h>
Christopher Faulet3d97c902015-12-09 14:59:38 +010017#include <common/mini-clist.h>
18#include <common/standard.h>
19
20#include <types/compression.h>
21#include <types/filters.h>
Christopher Faulet3d97c902015-12-09 14:59:38 +010022#include <types/proxy.h>
23#include <types/sample.h>
24
25#include <proto/compression.h>
Christopher Faulet92d36382015-11-05 13:35:03 +010026#include <proto/filters.h>
Christopher Faulete6902cd2018-11-30 22:29:48 +010027#include <proto/http_htx.h>
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020028#include <proto/http_ana.h>
Christopher Faulet3d97c902015-12-09 14:59:38 +010029#include <proto/sample.h>
30#include <proto/stream.h>
31
Christopher Fauletf4a4ef72018-12-07 17:39:53 +010032const char *http_comp_flt_id = "compression filter";
Christopher Faulet92d36382015-11-05 13:35:03 +010033
34struct flt_ops comp_ops;
35
Christopher Faulet92d36382015-11-05 13:35:03 +010036struct comp_state {
37 struct comp_ctx *comp_ctx; /* compression context */
38 struct comp_algo *comp_algo; /* compression algorithm if not NULL */
Christopher Faulet92d36382015-11-05 13:35:03 +010039};
40
Willy Tarreau8ceae722018-11-26 11:58:30 +010041/* Pools used to allocate comp_state structs */
42DECLARE_STATIC_POOL(pool_head_comp_state, "comp_state", sizeof(struct comp_state));
43
44static THREAD_LOCAL struct buffer tmpbuf;
45static THREAD_LOCAL struct buffer zbuf;
Willy Tarreau8ceae722018-11-26 11:58:30 +010046
Christopher Faulet92d36382015-11-05 13:35:03 +010047static int select_compression_request_header(struct comp_state *st,
48 struct stream *s,
49 struct http_msg *msg);
50static int select_compression_response_header(struct comp_state *st,
51 struct stream *s,
52 struct http_msg *msg);
Christopher Faulet27d93c32018-12-15 22:32:02 +010053static int set_compression_response_header(struct comp_state *st,
54 struct stream *s,
55 struct http_msg *msg);
Christopher Faulet92d36382015-11-05 13:35:03 +010056
Christopher Faulete6902cd2018-11-30 22:29:48 +010057static int htx_compression_buffer_init(struct htx *htx, struct buffer *out);
58static int htx_compression_buffer_add_data(struct comp_state *st, const char *data, size_t len,
59 struct buffer *out);
60static int htx_compression_buffer_end(struct comp_state *st, struct buffer *out, int end);
61
Christopher Faulet92d36382015-11-05 13:35:03 +010062/***********************************************************************/
63static int
Christopher Faulete6902cd2018-11-30 22:29:48 +010064comp_flt_init(struct proxy *px, struct flt_conf *fconf)
65{
Christopher Faulet6e540952018-12-03 22:43:41 +010066 fconf->flags |= FLT_CFG_FL_HTX;
Christopher Faulete6902cd2018-11-30 22:29:48 +010067 return 0;
68}
69
70static int
Christopher Faulet8ca3b4b2017-07-25 11:07:15 +020071comp_flt_init_per_thread(struct proxy *px, struct flt_conf *fconf)
Christopher Faulet92d36382015-11-05 13:35:03 +010072{
Willy Tarreauc9fa0482018-07-10 17:43:27 +020073 if (!tmpbuf.size && b_alloc(&tmpbuf) == NULL)
Christopher Fauletb77c5c22015-12-07 16:48:42 +010074 return -1;
Willy Tarreauc9fa0482018-07-10 17:43:27 +020075 if (!zbuf.size && b_alloc(&zbuf) == NULL)
Christopher Fauletb77c5c22015-12-07 16:48:42 +010076 return -1;
Christopher Faulet92d36382015-11-05 13:35:03 +010077 return 0;
78}
79
80static void
Christopher Faulet8ca3b4b2017-07-25 11:07:15 +020081comp_flt_deinit_per_thread(struct proxy *px, struct flt_conf *fconf)
Christopher Faulet92d36382015-11-05 13:35:03 +010082{
Willy Tarreauc9fa0482018-07-10 17:43:27 +020083 if (tmpbuf.size)
Christopher Faulet92d36382015-11-05 13:35:03 +010084 b_free(&tmpbuf);
Willy Tarreauc9fa0482018-07-10 17:43:27 +020085 if (zbuf.size)
Christopher Fauletb77c5c22015-12-07 16:48:42 +010086 b_free(&zbuf);
Christopher Faulet92d36382015-11-05 13:35:03 +010087}
88
89static int
90comp_start_analyze(struct stream *s, struct filter *filter, struct channel *chn)
91{
Christopher Faulet8ca3b4b2017-07-25 11:07:15 +020092
Christopher Faulet92d36382015-11-05 13:35:03 +010093 if (filter->ctx == NULL) {
94 struct comp_state *st;
95
Willy Tarreaubafbe012017-11-24 17:34:44 +010096 st = pool_alloc_dirty(pool_head_comp_state);
Christopher Fauleta03d4ad2017-06-26 16:53:33 +020097 if (st == NULL)
Christopher Faulet92d36382015-11-05 13:35:03 +010098 return -1;
99
Christopher Faulet89f2b162019-07-15 21:16:04 +0200100 st->comp_algo = NULL;
101 st->comp_ctx = NULL;
102 filter->ctx = st;
Christopher Faulet3dc860d2017-09-15 11:39:36 +0200103
104 /* Register post-analyzer on AN_RES_WAIT_HTTP because we need to
105 * analyze response headers before http-response rules execution
106 * to be sure we can use res.comp and res.comp_algo sample
107 * fetches */
108 filter->post_analyzers |= AN_RES_WAIT_HTTP;
Christopher Faulet92d36382015-11-05 13:35:03 +0100109 }
110 return 1;
111}
112
113static int
Christopher Faulet92d36382015-11-05 13:35:03 +0100114comp_end_analyze(struct stream *s, struct filter *filter, struct channel *chn)
115{
116 struct comp_state *st = filter->ctx;
Christopher Faulet92d36382015-11-05 13:35:03 +0100117
Christopher Fauletd60b3cf2017-06-26 11:47:13 +0200118 if (!st)
Christopher Faulet92d36382015-11-05 13:35:03 +0100119 goto end;
120
Christopher Faulet92d36382015-11-05 13:35:03 +0100121 /* release any possible compression context */
Christopher Fauletd60b3cf2017-06-26 11:47:13 +0200122 if (st->comp_algo)
123 st->comp_algo->end(&st->comp_ctx);
Willy Tarreaubafbe012017-11-24 17:34:44 +0100124 pool_free(pool_head_comp_state, st);
Christopher Faulet92d36382015-11-05 13:35:03 +0100125 filter->ctx = NULL;
126 end:
127 return 1;
128}
129
130static int
Christopher Faulet1339d742016-05-11 16:48:33 +0200131comp_http_headers(struct stream *s, struct filter *filter, struct http_msg *msg)
132{
133 struct comp_state *st = filter->ctx;
134
135 if (!strm_fe(s)->comp && !s->be->comp)
136 goto end;
137
138 if (!(msg->chn->flags & CF_ISRESP))
139 select_compression_request_header(st, s, msg);
140 else {
Christopher Faulet3dc860d2017-09-15 11:39:36 +0200141 /* Response headers have already been checked in
142 * comp_http_post_analyze callback. */
Christopher Faulet1339d742016-05-11 16:48:33 +0200143 if (st->comp_algo) {
Christopher Faulet27d93c32018-12-15 22:32:02 +0100144 if (!set_compression_response_header(st, s, msg))
145 goto end;
Christopher Faulet1339d742016-05-11 16:48:33 +0200146 register_data_filter(s, msg->chn, filter);
Christopher Faulet1339d742016-05-11 16:48:33 +0200147 }
148 }
149
150 end:
151 return 1;
152}
153
154static int
Christopher Faulet3dc860d2017-09-15 11:39:36 +0200155comp_http_post_analyze(struct stream *s, struct filter *filter,
156 struct channel *chn, unsigned an_bit)
157{
158 struct http_txn *txn = s->txn;
159 struct http_msg *msg = &txn->rsp;
160 struct comp_state *st = filter->ctx;
161
162 if (an_bit != AN_RES_WAIT_HTTP)
163 goto end;
164
165 if (!strm_fe(s)->comp && !s->be->comp)
166 goto end;
167
168 select_compression_response_header(st, s, msg);
169
170 end:
171 return 1;
172}
173
174static int
Christopher Faulete6902cd2018-11-30 22:29:48 +0100175comp_http_payload(struct stream *s, struct filter *filter, struct http_msg *msg,
176 unsigned int offset, unsigned int len)
177{
178 struct comp_state *st = filter->ctx;
Christopher Faulet27ba2dc2018-12-05 11:53:24 +0100179 struct htx *htx = htxbuf(&msg->chn->buf);
Christopher Faulete6a62bf2020-03-02 16:20:05 +0100180 struct htx_ret htxret = htx_find_offset(htx, offset);
Christopher Faulete6902cd2018-11-30 22:29:48 +0100181 struct htx_blk *blk;
Christopher Faulete6902cd2018-11-30 22:29:48 +0100182 int ret, consumed = 0, to_forward = 0;
183
Christopher Faulete6a62bf2020-03-02 16:20:05 +0100184 blk = htxret.blk;
185 offset = htxret.ret;
186 for (; blk && len; blk = htx_get_next_blk(htx, blk)) {
Christopher Faulete6902cd2018-11-30 22:29:48 +0100187 enum htx_blk_type type = htx_get_blk_type(blk);
188 uint32_t sz = htx_get_blksz(blk);
189 struct ist v;
190
191 switch (type) {
192 case HTX_BLK_UNUSED:
193 break;
194
195 case HTX_BLK_DATA:
196 v = htx_get_blk_value(htx, blk);
197 v.ptr += offset;
198 v.len -= offset;
199 if (v.len > len)
200 v.len = len;
201 if (htx_compression_buffer_init(htx, &trash) < 0) {
202 msg->chn->flags |= CF_WAKE_WRITE;
203 goto end;
204 }
205 ret = htx_compression_buffer_add_data(st, v.ptr, v.len, &trash);
206 if (ret < 0)
207 goto error;
208 if (htx_compression_buffer_end(st, &trash, 0) < 0)
209 goto error;
210 len -= ret;
211 consumed += ret;
212 to_forward += b_data(&trash);
213 if (ret == sz && !b_data(&trash)) {
214 offset = 0;
215 blk = htx_remove_blk(htx, blk);
216 continue;
217 }
218 v.len = ret;
219 blk = htx_replace_blk_value(htx, blk, v, ist2(b_head(&trash), b_data(&trash)));
220 break;
221
Christopher Faulete6902cd2018-11-30 22:29:48 +0100222 case HTX_BLK_TLR:
Christopher Faulet2d7c5392019-06-03 10:41:26 +0200223 case HTX_BLK_EOT:
Christopher Faulete6902cd2018-11-30 22:29:48 +0100224 case HTX_BLK_EOM:
225 if (msg->flags & HTTP_MSGF_COMPRESSING) {
226 if (htx_compression_buffer_init(htx, &trash) < 0) {
227 msg->chn->flags |= CF_WAKE_WRITE;
228 goto end;
229 }
230 if (htx_compression_buffer_end(st, &trash, 1) < 0)
231 goto error;
Christopher Fauletd238ae32018-12-21 15:10:25 +0100232 if (b_data(&trash)) {
Christopher Faulet86bc8df2019-06-11 10:38:38 +0200233 struct htx_blk *last = htx_add_last_data(htx, ist2(b_head(&trash), b_data(&trash)));
234 if (!last)
235 goto error;
236 blk = htx_get_next_blk(htx, last);
Christopher Fauletd238ae32018-12-21 15:10:25 +0100237 if (!blk)
238 goto error;
239 to_forward += b_data(&trash);
240 }
Christopher Faulete6902cd2018-11-30 22:29:48 +0100241 msg->flags &= ~HTTP_MSGF_COMPRESSING;
242 /* We let the mux add last empty chunk and empty trailers */
243 }
244 /* fall through */
245
246 default:
247 sz -= offset;
248 if (sz > len)
249 sz = len;
250 consumed += sz;
251 to_forward += sz;
252 len -= sz;
253 break;
254 }
255
256 offset = 0;
Christopher Faulete6902cd2018-11-30 22:29:48 +0100257 }
258
259 end:
260 if (to_forward != consumed)
261 flt_update_offsets(filter, msg->chn, to_forward - consumed);
262
263 if (st->comp_ctx && st->comp_ctx->cur_lvl > 0) {
Willy Tarreauef6fd852019-02-04 11:48:03 +0100264 update_freq_ctr(&global.comp_bps_in, consumed);
Olivier Houchard43da3432019-03-08 18:50:27 +0100265 _HA_ATOMIC_ADD(&strm_fe(s)->fe_counters.comp_in, consumed);
266 _HA_ATOMIC_ADD(&s->be->be_counters.comp_in, consumed);
Christopher Faulete6902cd2018-11-30 22:29:48 +0100267 update_freq_ctr(&global.comp_bps_out, to_forward);
Olivier Houchard43da3432019-03-08 18:50:27 +0100268 _HA_ATOMIC_ADD(&strm_fe(s)->fe_counters.comp_out, to_forward);
269 _HA_ATOMIC_ADD(&s->be->be_counters.comp_out, to_forward);
Willy Tarreauef6fd852019-02-04 11:48:03 +0100270 } else {
Olivier Houchard43da3432019-03-08 18:50:27 +0100271 _HA_ATOMIC_ADD(&strm_fe(s)->fe_counters.comp_byp, consumed);
272 _HA_ATOMIC_ADD(&s->be->be_counters.comp_byp, consumed);
Christopher Faulete6902cd2018-11-30 22:29:48 +0100273 }
274 return to_forward;
275
276 error:
277 return -1;
278}
279
Christopher Faulet2fb28802015-12-01 10:40:57 +0100280
Christopher Faulet92d36382015-11-05 13:35:03 +0100281static int
Christopher Fauletd60b3cf2017-06-26 11:47:13 +0200282comp_http_end(struct stream *s, struct filter *filter,
283 struct http_msg *msg)
284{
285 struct comp_state *st = filter->ctx;
286
287 if (!(msg->chn->flags & CF_ISRESP) || !st || !st->comp_algo)
288 goto end;
289
290 if (strm_fe(s)->mode == PR_MODE_HTTP)
Olivier Houchard43da3432019-03-08 18:50:27 +0100291 _HA_ATOMIC_ADD(&strm_fe(s)->fe_counters.p.http.comp_rsp, 1);
Christopher Fauletd60b3cf2017-06-26 11:47:13 +0200292 if ((s->flags & SF_BE_ASSIGNED) && (s->be->mode == PR_MODE_HTTP))
Olivier Houchard43da3432019-03-08 18:50:27 +0100293 _HA_ATOMIC_ADD(&s->be->be_counters.p.http.comp_rsp, 1);
Christopher Fauletd60b3cf2017-06-26 11:47:13 +0200294 end:
295 return 1;
296}
Christopher Faulet27d93c32018-12-15 22:32:02 +0100297
Christopher Faulet89f2b162019-07-15 21:16:04 +0200298/***********************************************************************/
Christopher Faulet27d93c32018-12-15 22:32:02 +0100299static int
Christopher Faulet89f2b162019-07-15 21:16:04 +0200300set_compression_response_header(struct comp_state *st, struct stream *s, struct http_msg *msg)
Christopher Faulet27d93c32018-12-15 22:32:02 +0100301{
302 struct htx *htx = htxbuf(&msg->chn->buf);
Tim Duesterhusb229f012019-01-29 16:38:56 +0100303 struct http_hdr_ctx ctx;
Christopher Faulet27d93c32018-12-15 22:32:02 +0100304
305 /*
306 * Add Content-Encoding header when it's not identity encoding.
307 * RFC 2616 : Identity encoding: This content-coding is used only in the
308 * Accept-Encoding header, and SHOULD NOT be used in the Content-Encoding
309 * header.
310 */
311 if (st->comp_algo->cfg_name_len != 8 || memcmp(st->comp_algo->cfg_name, "identity", 8) != 0) {
312 struct ist v = ist2(st->comp_algo->ua_name, st->comp_algo->ua_name_len);
313
314 if (!http_add_header(htx, ist("Content-Encoding"), v))
315 goto error;
316 }
317
318 /* remove Content-Length header */
319 if (msg->flags & HTTP_MSGF_CNT_LEN) {
Christopher Faulet27d93c32018-12-15 22:32:02 +0100320 ctx.blk = NULL;
321 while (http_find_header(htx, ist("Content-Length"), &ctx, 1))
322 http_remove_header(htx, &ctx);
323 }
324
325 /* add "Transfer-Encoding: chunked" header */
326 if (!(msg->flags & HTTP_MSGF_TE_CHNK)) {
327 if (!http_add_header(htx, ist("Transfer-Encoding"), ist("chunked")))
328 goto error;
329 }
330
Tim Duesterhusb229f012019-01-29 16:38:56 +0100331 /* convert "ETag" header to a weak ETag */
332 ctx.blk = NULL;
333 if (http_find_header(htx, ist("ETag"), &ctx, 1)) {
334 if (ctx.value.ptr[0] == '"') {
335 /* This a strong ETag. Convert it to a weak one. */
336 struct ist v = ist2(trash.area, 0);
337 if (istcat(&v, ist("W/"), trash.size) == -1 || istcat(&v, ctx.value, trash.size) == -1)
338 goto error;
339
340 if (!http_replace_header_value(htx, &ctx, v))
341 goto error;
342 }
343 }
344
Tim Duesterhus721d6862019-06-17 16:10:07 +0200345 if (!http_add_header(htx, ist("Vary"), ist("Accept-Encoding")))
346 goto error;
347
Christopher Faulet27d93c32018-12-15 22:32:02 +0100348 return 1;
349
350 error:
351 st->comp_algo->end(&st->comp_ctx);
352 st->comp_algo = NULL;
353 return 0;
354}
355
Christopher Faulet3d97c902015-12-09 14:59:38 +0100356/*
357 * Selects a compression algorithm depending on the client request.
358 */
Christopher Faulete6902cd2018-11-30 22:29:48 +0100359static int
Christopher Faulet89f2b162019-07-15 21:16:04 +0200360select_compression_request_header(struct comp_state *st, struct stream *s, struct http_msg *msg)
Christopher Faulete6902cd2018-11-30 22:29:48 +0100361{
Christopher Faulet27ba2dc2018-12-05 11:53:24 +0100362 struct htx *htx = htxbuf(&msg->chn->buf);
Christopher Faulete6902cd2018-11-30 22:29:48 +0100363 struct http_hdr_ctx ctx;
364 struct comp_algo *comp_algo = NULL;
365 struct comp_algo *comp_algo_back = NULL;
366
367 /* Disable compression for older user agents announcing themselves as "Mozilla/4"
368 * unless they are known good (MSIE 6 with XP SP2, or MSIE 7 and later).
369 * See http://zoompf.com/2012/02/lose-the-wait-http-compression for more details.
370 */
371 ctx.blk = NULL;
372 if (http_find_header(htx, ist("User-Agent"), &ctx, 1) &&
373 ctx.value.len >= 9 &&
374 memcmp(ctx.value.ptr, "Mozilla/4", 9) == 0 &&
375 (ctx.value.len < 31 ||
376 memcmp(ctx.value.ptr + 25, "MSIE ", 5) != 0 ||
377 *(ctx.value.ptr + 30) < '6' ||
378 (*(ctx.value.ptr + 30) == '6' &&
379 (ctx.value.len < 54 || memcmp(ctx.value.ptr + 51, "SV1", 3) != 0)))) {
380 st->comp_algo = NULL;
381 return 0;
382 }
383
384 /* search for the algo in the backend in priority or the frontend */
385 if ((s->be->comp && (comp_algo_back = s->be->comp->algos)) ||
386 (strm_fe(s)->comp && (comp_algo_back = strm_fe(s)->comp->algos))) {
387 int best_q = 0;
388
389 ctx.blk = NULL;
390 while (http_find_header(htx, ist("Accept-Encoding"), &ctx, 0)) {
391 const char *qval;
392 int q;
393 int toklen;
394
395 /* try to isolate the token from the optional q-value */
396 toklen = 0;
397 while (toklen < ctx.value.len && HTTP_IS_TOKEN(*(ctx.value.ptr + toklen)))
398 toklen++;
399
400 qval = ctx.value.ptr + toklen;
401 while (1) {
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 || *qval != ';') {
406 qval = NULL;
407 break;
408 }
409 qval++;
410
411 while (qval < ctx.value.ptr + ctx.value.len && HTTP_IS_LWS(*qval))
412 qval++;
413
414 if (qval >= ctx.value.ptr + ctx.value.len) {
415 qval = NULL;
416 break;
417 }
418 if (strncmp(qval, "q=", MIN(ctx.value.ptr + ctx.value.len - qval, 2)) == 0)
419 break;
420
421 while (qval < ctx.value.ptr + ctx.value.len && *qval != ';')
422 qval++;
423 }
424
425 /* here we have qval pointing to the first "q=" attribute or NULL if not found */
426 q = qval ? http_parse_qvalue(qval + 2, NULL) : 1000;
427
428 if (q <= best_q)
429 continue;
430
431 for (comp_algo = comp_algo_back; comp_algo; comp_algo = comp_algo->next) {
432 if (*(ctx.value.ptr) == '*' ||
433 word_match(ctx.value.ptr, toklen, comp_algo->ua_name, comp_algo->ua_name_len)) {
434 st->comp_algo = comp_algo;
435 best_q = q;
436 break;
437 }
438 }
439 }
440 }
441
442 /* remove all occurrences of the header when "compression offload" is set */
443 if (st->comp_algo) {
444 if ((s->be->comp && s->be->comp->offload) ||
445 (strm_fe(s)->comp && strm_fe(s)->comp->offload)) {
446 http_remove_header(htx, &ctx);
447 ctx.blk = NULL;
448 while (http_find_header(htx, ist("Accept-Encoding"), &ctx, 1))
449 http_remove_header(htx, &ctx);
450 }
Christopher Faulet3d97c902015-12-09 14:59:38 +0100451 return 1;
452 }
453
454 /* identity is implicit does not require headers */
Christopher Faulet92d36382015-11-05 13:35:03 +0100455 if ((s->be->comp && (comp_algo_back = s->be->comp->algos)) ||
456 (strm_fe(s)->comp && (comp_algo_back = strm_fe(s)->comp->algos))) {
Christopher Faulet3d97c902015-12-09 14:59:38 +0100457 for (comp_algo = comp_algo_back; comp_algo; comp_algo = comp_algo->next) {
458 if (comp_algo->cfg_name_len == 8 && memcmp(comp_algo->cfg_name, "identity", 8) == 0) {
Christopher Faulet92d36382015-11-05 13:35:03 +0100459 st->comp_algo = comp_algo;
Christopher Faulet3d97c902015-12-09 14:59:38 +0100460 return 1;
461 }
462 }
463 }
464
Christopher Faulet92d36382015-11-05 13:35:03 +0100465 st->comp_algo = NULL;
Christopher Faulet3d97c902015-12-09 14:59:38 +0100466 return 0;
467}
468
469/*
470 * Selects a comression algorithm depending of the server response.
471 */
Christopher Faulet92d36382015-11-05 13:35:03 +0100472static int
Christopher Faulet89f2b162019-07-15 21:16:04 +0200473select_compression_response_header(struct comp_state *st, struct stream *s, struct http_msg *msg)
Christopher Faulete6902cd2018-11-30 22:29:48 +0100474{
Christopher Faulet27ba2dc2018-12-05 11:53:24 +0100475 struct htx *htx = htxbuf(&msg->chn->buf);
Christopher Faulete6902cd2018-11-30 22:29:48 +0100476 struct http_txn *txn = s->txn;
477 struct http_hdr_ctx ctx;
478 struct comp_type *comp_type;
479
480 /* no common compression algorithm was found in request header */
481 if (st->comp_algo == NULL)
482 goto fail;
483
Christopher Faulet1d3613a2019-01-07 14:41:59 +0100484 /* compression already in progress */
485 if (msg->flags & HTTP_MSGF_COMPRESSING)
486 goto fail;
487
Christopher Faulete6902cd2018-11-30 22:29:48 +0100488 /* HTTP < 1.1 should not be compressed */
489 if (!(msg->flags & HTTP_MSGF_VER_11) || !(txn->req.flags & HTTP_MSGF_VER_11))
490 goto fail;
491
492 if (txn->meth == HTTP_METH_HEAD)
493 goto fail;
494
495 /* compress 200,201,202,203 responses only */
496 if ((txn->status != 200) &&
497 (txn->status != 201) &&
498 (txn->status != 202) &&
499 (txn->status != 203))
500 goto fail;
501
Christopher Fauletc963eb22018-12-21 14:53:54 +0100502 if (!(msg->flags & HTTP_MSGF_XFER_LEN) || msg->flags & HTTP_MSGF_BODYLESS)
Christopher Faulete6902cd2018-11-30 22:29:48 +0100503 goto fail;
504
505 /* content is already compressed */
506 ctx.blk = NULL;
507 if (http_find_header(htx, ist("Content-Encoding"), &ctx, 1))
508 goto fail;
509
510 /* no compression when Cache-Control: no-transform is present in the message */
511 ctx.blk = NULL;
512 while (http_find_header(htx, ist("Cache-Control"), &ctx, 0)) {
513 if (word_match(ctx.value.ptr, ctx.value.len, "no-transform", 12))
514 goto fail;
515 }
516
Tim Duesterhusb229f012019-01-29 16:38:56 +0100517 /* no compression when ETag is malformed */
518 ctx.blk = NULL;
519 if (http_find_header(htx, ist("ETag"), &ctx, 1)) {
520 if (!(((ctx.value.len >= 4 && memcmp(ctx.value.ptr, "W/\"", 3) == 0) || /* Either a weak ETag */
521 (ctx.value.len >= 2 && ctx.value.ptr[0] == '"')) && /* or strong ETag */
522 ctx.value.ptr[ctx.value.len - 1] == '"')) {
523 goto fail;
524 }
525 }
526 /* no compression when multiple ETags are present
527 * Note: Do not reset ctx.blk!
528 */
529 if (http_find_header(htx, ist("ETag"), &ctx, 1))
530 goto fail;
531
Christopher Faulete6902cd2018-11-30 22:29:48 +0100532 comp_type = NULL;
533
534 /* we don't want to compress multipart content-types, nor content-types that are
535 * not listed in the "compression type" directive if any. If no content-type was
536 * found but configuration requires one, we don't compress either. Backend has
537 * the priority.
538 */
539 ctx.blk = NULL;
540 if (http_find_header(htx, ist("Content-Type"), &ctx, 1)) {
541 if (ctx.value.len >= 9 && strncasecmp("multipart", ctx.value.ptr, 9) == 0)
542 goto fail;
543
544 if ((s->be->comp && (comp_type = s->be->comp->types)) ||
545 (strm_fe(s)->comp && (comp_type = strm_fe(s)->comp->types))) {
546 for (; comp_type; comp_type = comp_type->next) {
547 if (ctx.value.len >= comp_type->name_len &&
548 strncasecmp(ctx.value.ptr, comp_type->name, comp_type->name_len) == 0)
549 /* this Content-Type should be compressed */
550 break;
551 }
552 /* this Content-Type should not be compressed */
553 if (comp_type == NULL)
554 goto fail;
555 }
556 }
557 else { /* no content-type header */
558 if ((s->be->comp && s->be->comp->types) ||
559 (strm_fe(s)->comp && strm_fe(s)->comp->types))
560 goto fail; /* a content-type was required */
561 }
562
563 /* limit compression rate */
564 if (global.comp_rate_lim > 0)
565 if (read_freq_ctr(&global.comp_bps_in) > global.comp_rate_lim)
566 goto fail;
567
568 /* limit cpu usage */
Willy Tarreau81036f22019-05-20 19:24:50 +0200569 if (ti->idle_pct < compress_min_idle)
Christopher Faulete6902cd2018-11-30 22:29:48 +0100570 goto fail;
571
572 /* initialize compression */
573 if (st->comp_algo->init(&st->comp_ctx, global.tune.comp_maxlevel) < 0)
574 goto fail;
Christopher Faulete6902cd2018-11-30 22:29:48 +0100575 msg->flags |= HTTP_MSGF_COMPRESSING;
576 return 1;
577
Christopher Faulete6902cd2018-11-30 22:29:48 +0100578 fail:
579 st->comp_algo = NULL;
580 return 0;
581}
582
Christopher Faulet3d97c902015-12-09 14:59:38 +0100583/***********************************************************************/
Christopher Faulete6902cd2018-11-30 22:29:48 +0100584static int
585htx_compression_buffer_init(struct htx *htx, struct buffer *out)
586{
587 /* output stream requires at least 10 bytes for the gzip header, plus
588 * at least 8 bytes for the gzip trailer (crc+len), plus a possible
589 * plus at most 5 bytes per 32kB block and 2 bytes to close the stream.
590 */
591 if (htx_free_space(htx) < 20 + 5 * ((htx->data + 32767) >> 15))
592 return -1;
593 b_reset(out);
594 return 0;
595}
596
Christopher Faulete6902cd2018-11-30 22:29:48 +0100597static int
598htx_compression_buffer_add_data(struct comp_state *st, const char *data, size_t len,
599 struct buffer *out)
600{
601 return st->comp_algo->add_data(st->comp_ctx, data, len, out);
602}
603
Christopher Faulete6902cd2018-11-30 22:29:48 +0100604static int
605htx_compression_buffer_end(struct comp_state *st, struct buffer *out, int end)
606{
607 if (end)
608 return st->comp_algo->finish(st->comp_ctx, out);
609 else
610 return st->comp_algo->flush(st->comp_ctx, out);
611}
612
Christopher Faulet3d97c902015-12-09 14:59:38 +0100613
614/***********************************************************************/
Christopher Faulet92d36382015-11-05 13:35:03 +0100615struct flt_ops comp_ops = {
Christopher Faulete6902cd2018-11-30 22:29:48 +0100616 .init = comp_flt_init,
Christopher Faulet8ca3b4b2017-07-25 11:07:15 +0200617 .init_per_thread = comp_flt_init_per_thread,
618 .deinit_per_thread = comp_flt_deinit_per_thread,
Christopher Faulet92d36382015-11-05 13:35:03 +0100619
620 .channel_start_analyze = comp_start_analyze,
Christopher Faulet92d36382015-11-05 13:35:03 +0100621 .channel_end_analyze = comp_end_analyze,
Christopher Faulet3dc860d2017-09-15 11:39:36 +0200622 .channel_post_analyze = comp_http_post_analyze,
Christopher Faulet92d36382015-11-05 13:35:03 +0100623
Christopher Faulet1339d742016-05-11 16:48:33 +0200624 .http_headers = comp_http_headers,
Christopher Faulete6902cd2018-11-30 22:29:48 +0100625 .http_payload = comp_http_payload,
626 .http_end = comp_http_end,
Christopher Faulet92d36382015-11-05 13:35:03 +0100627};
628
Christopher Faulet3d97c902015-12-09 14:59:38 +0100629static int
630parse_compression_options(char **args, int section, struct proxy *proxy,
631 struct proxy *defpx, const char *file, int line,
632 char **err)
633{
Christopher Faulet92d36382015-11-05 13:35:03 +0100634 struct comp *comp;
Christopher Faulet3d97c902015-12-09 14:59:38 +0100635
636 if (proxy->comp == NULL) {
Vincent Bernat02779b62016-04-03 13:48:43 +0200637 comp = calloc(1, sizeof(*comp));
Christopher Faulet3d97c902015-12-09 14:59:38 +0100638 proxy->comp = comp;
639 }
640 else
641 comp = proxy->comp;
642
643 if (!strcmp(args[1], "algo")) {
644 struct comp_ctx *ctx;
645 int cur_arg = 2;
646
647 if (!*args[cur_arg]) {
648 memprintf(err, "parsing [%s:%d] : '%s' expects <algorithm>\n",
649 file, line, args[0]);
650 return -1;
651 }
652 while (*(args[cur_arg])) {
653 if (comp_append_algo(comp, args[cur_arg]) < 0) {
654 memprintf(err, "'%s' : '%s' is not a supported algorithm.\n",
655 args[0], args[cur_arg]);
656 return -1;
657 }
658 if (proxy->comp->algos->init(&ctx, 9) == 0)
659 proxy->comp->algos->end(&ctx);
660 else {
661 memprintf(err, "'%s' : Can't init '%s' algorithm.\n",
662 args[0], args[cur_arg]);
663 return -1;
664 }
665 cur_arg++;
666 continue;
667 }
668 }
669 else if (!strcmp(args[1], "offload"))
670 comp->offload = 1;
671 else if (!strcmp(args[1], "type")) {
672 int cur_arg = 2;
673
674 if (!*args[cur_arg]) {
675 memprintf(err, "'%s' expects <type>\n", args[0]);
676 return -1;
677 }
678 while (*(args[cur_arg])) {
679 comp_append_type(comp, args[cur_arg]);
680 cur_arg++;
681 continue;
682 }
683 }
684 else {
685 memprintf(err, "'%s' expects 'algo', 'type' or 'offload'\n",
686 args[0]);
687 return -1;
688 }
689
690 return 0;
691}
692
Christopher Faulet92d36382015-11-05 13:35:03 +0100693static int
694parse_http_comp_flt(char **args, int *cur_arg, struct proxy *px,
Thierry Fournier3610c392016-04-13 18:27:51 +0200695 struct flt_conf *fconf, char **err, void *private)
Christopher Faulet92d36382015-11-05 13:35:03 +0100696{
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100697 struct flt_conf *fc, *back;
Christopher Faulet92d36382015-11-05 13:35:03 +0100698
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100699 list_for_each_entry_safe(fc, back, &px->filter_configs, list) {
700 if (fc->id == http_comp_flt_id) {
Christopher Faulet92d36382015-11-05 13:35:03 +0100701 memprintf(err, "%s: Proxy supports only one compression filter\n", px->id);
702 return -1;
703 }
704 }
705
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100706 fconf->id = http_comp_flt_id;
707 fconf->conf = NULL;
708 fconf->ops = &comp_ops;
Christopher Faulet92d36382015-11-05 13:35:03 +0100709 (*cur_arg)++;
710
711 return 0;
712}
713
714
715int
Christopher Fauletc9df7f72018-12-10 16:14:04 +0100716check_implicit_http_comp_flt(struct proxy *proxy)
Christopher Faulet92d36382015-11-05 13:35:03 +0100717{
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100718 struct flt_conf *fconf;
Christopher Faulet27d93c32018-12-15 22:32:02 +0100719 int explicit = 0;
720 int comp = 0;
Christopher Faulet92d36382015-11-05 13:35:03 +0100721 int err = 0;
722
723 if (proxy->comp == NULL)
724 goto end;
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100725 if (!LIST_ISEMPTY(&proxy->filter_configs)) {
726 list_for_each_entry(fconf, &proxy->filter_configs, list) {
727 if (fconf->id == http_comp_flt_id)
Christopher Faulet27d93c32018-12-15 22:32:02 +0100728 comp = 1;
729 else if (fconf->id == cache_store_flt_id) {
730 if (comp) {
731 ha_alert("config: %s '%s': unable to enable the compression filter "
732 "before any cache filter.\n",
733 proxy_type_str(proxy), proxy->id);
734 err++;
735 goto end;
736 }
737 }
Christopher Faulet78fbb9f2019-08-11 23:11:03 +0200738 else if (fconf->id == fcgi_flt_id)
739 continue;
Christopher Faulet27d93c32018-12-15 22:32:02 +0100740 else
741 explicit = 1;
Christopher Faulet92d36382015-11-05 13:35:03 +0100742 }
Christopher Faulet27d93c32018-12-15 22:32:02 +0100743 }
744 if (comp)
745 goto end;
746 else if (explicit) {
747 ha_alert("config: %s '%s': require an explicit filter declaration to use "
748 "HTTP compression\n", proxy_type_str(proxy), proxy->id);
Christopher Faulet92d36382015-11-05 13:35:03 +0100749 err++;
750 goto end;
751 }
752
Christopher Faulet27d93c32018-12-15 22:32:02 +0100753 /* Implicit declaration of the compression filter is always the last
754 * one */
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100755 fconf = calloc(1, sizeof(*fconf));
756 if (!fconf) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100757 ha_alert("config: %s '%s': out of memory\n",
758 proxy_type_str(proxy), proxy->id);
Christopher Faulet92d36382015-11-05 13:35:03 +0100759 err++;
760 goto end;
761 }
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100762 fconf->id = http_comp_flt_id;
763 fconf->conf = NULL;
764 fconf->ops = &comp_ops;
765 LIST_ADDQ(&proxy->filter_configs, &fconf->list);
Christopher Faulet92d36382015-11-05 13:35:03 +0100766 end:
767 return err;
768}
769
770/*
771 * boolean, returns true if compression is used (either gzip or deflate) in the
772 * response.
773 */
Christopher Faulet3d97c902015-12-09 14:59:38 +0100774static int
Christopher Faulet92d36382015-11-05 13:35:03 +0100775smp_fetch_res_comp(const struct arg *args, struct sample *smp, const char *kw,
776 void *private)
Christopher Faulet3d97c902015-12-09 14:59:38 +0100777{
Willy Tarreaube508f12016-03-10 11:47:01 +0100778 struct http_txn *txn = smp->strm ? smp->strm->txn : NULL;
Christopher Faulet92d36382015-11-05 13:35:03 +0100779
Christopher Faulet3d97c902015-12-09 14:59:38 +0100780 smp->data.type = SMP_T_BOOL;
Christopher Faulet92d36382015-11-05 13:35:03 +0100781 smp->data.u.sint = (txn && (txn->rsp.flags & HTTP_MSGF_COMPRESSING));
Christopher Faulet3d97c902015-12-09 14:59:38 +0100782 return 1;
783}
784
Christopher Faulet92d36382015-11-05 13:35:03 +0100785/*
786 * string, returns algo
787 */
Christopher Faulet3d97c902015-12-09 14:59:38 +0100788static int
Christopher Faulet92d36382015-11-05 13:35:03 +0100789smp_fetch_res_comp_algo(const struct arg *args, struct sample *smp,
790 const char *kw, void *private)
Christopher Faulet3d97c902015-12-09 14:59:38 +0100791{
Willy Tarreaube508f12016-03-10 11:47:01 +0100792 struct http_txn *txn = smp->strm ? smp->strm->txn : NULL;
Christopher Faulet92d36382015-11-05 13:35:03 +0100793 struct filter *filter;
794 struct comp_state *st;
795
Christopher Faulet03d85532017-09-15 10:14:43 +0200796 if (!txn || !(txn->rsp.flags & HTTP_MSGF_COMPRESSING))
Christopher Faulet3d97c902015-12-09 14:59:38 +0100797 return 0;
798
Christopher Fauletfcf035c2015-12-03 11:48:03 +0100799 list_for_each_entry(filter, &strm_flt(smp->strm)->filters, list) {
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100800 if (FLT_ID(filter) != http_comp_flt_id)
Christopher Faulet92d36382015-11-05 13:35:03 +0100801 continue;
802
803 if (!(st = filter->ctx))
804 break;
805
806 smp->data.type = SMP_T_STR;
807 smp->flags = SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200808 smp->data.u.str.area = st->comp_algo->cfg_name;
809 smp->data.u.str.data = st->comp_algo->cfg_name_len;
Christopher Faulet92d36382015-11-05 13:35:03 +0100810 return 1;
811 }
812 return 0;
Christopher Faulet3d97c902015-12-09 14:59:38 +0100813}
814
815/* Declare the config parser for "compression" keyword */
816static struct cfg_kw_list cfg_kws = {ILH, {
817 { CFG_LISTEN, "compression", parse_compression_options },
818 { 0, NULL, NULL },
819 }
820};
821
Willy Tarreau0108d902018-11-25 19:14:37 +0100822INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
823
Christopher Faulet92d36382015-11-05 13:35:03 +0100824/* Declare the filter parser for "compression" keyword */
825static struct flt_kw_list filter_kws = { "COMP", { }, {
Thierry Fournier3610c392016-04-13 18:27:51 +0200826 { "compression", parse_http_comp_flt, NULL },
827 { NULL, NULL, NULL },
Christopher Faulet92d36382015-11-05 13:35:03 +0100828 }
829};
830
Willy Tarreau0108d902018-11-25 19:14:37 +0100831INITCALL1(STG_REGISTER, flt_register_keywords, &filter_kws);
832
Christopher Faulet3d97c902015-12-09 14:59:38 +0100833/* Note: must not be declared <const> as its list will be overwritten */
834static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
Christopher Faulet92d36382015-11-05 13:35:03 +0100835 { "res.comp", smp_fetch_res_comp, 0, NULL, SMP_T_BOOL, SMP_USE_HRSHP },
836 { "res.comp_algo", smp_fetch_res_comp_algo, 0, NULL, SMP_T_STR, SMP_USE_HRSHP },
837 { /* END */ },
838 }
839};
Christopher Faulet3d97c902015-12-09 14:59:38 +0100840
Willy Tarreau0108d902018-11-25 19:14:37 +0100841INITCALL1(STG_REGISTER, sample_register_fetches, &sample_fetch_keywords);