blob: 74a8ae72a077a8cdb1a0366828ae7eafc5a73781 [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 Faulet3d97c902015-12-09 14:59:38 +010027#include <proto/hdr_idx.h>
Christopher Faulete6902cd2018-11-30 22:29:48 +010028#include <proto/http_htx.h>
Christopher Faulet3d97c902015-12-09 14:59:38 +010029#include <proto/proto_http.h>
30#include <proto/sample.h>
31#include <proto/stream.h>
32
Christopher Fauletf4a4ef72018-12-07 17:39:53 +010033const char *http_comp_flt_id = "compression filter";
Christopher Faulet92d36382015-11-05 13:35:03 +010034
35struct flt_ops comp_ops;
36
Christopher Faulet92d36382015-11-05 13:35:03 +010037struct comp_state {
38 struct comp_ctx *comp_ctx; /* compression context */
39 struct comp_algo *comp_algo; /* compression algorithm if not NULL */
Christopher Faulete6902cd2018-11-30 22:29:48 +010040
41 /* Following fields are used by the legacy code only: */
Christopher Fauletb77c5c22015-12-07 16:48:42 +010042 int hdrs_len;
43 int tlrs_len;
Christopher Faulet2fb28802015-12-01 10:40:57 +010044 int consumed;
45 int initialized;
Christopher Fauletb77c5c22015-12-07 16:48:42 +010046 int finished;
Christopher Faulet92d36382015-11-05 13:35:03 +010047};
48
Willy Tarreau8ceae722018-11-26 11:58:30 +010049/* Pools used to allocate comp_state structs */
50DECLARE_STATIC_POOL(pool_head_comp_state, "comp_state", sizeof(struct comp_state));
51
52static THREAD_LOCAL struct buffer tmpbuf;
53static THREAD_LOCAL struct buffer zbuf;
54static THREAD_LOCAL unsigned int buf_output;
55
Christopher Faulet92d36382015-11-05 13:35:03 +010056static int select_compression_request_header(struct comp_state *st,
57 struct stream *s,
58 struct http_msg *msg);
59static int select_compression_response_header(struct comp_state *st,
60 struct stream *s,
61 struct http_msg *msg);
62
Christopher Faulete6902cd2018-11-30 22:29:48 +010063static int htx_compression_buffer_init(struct htx *htx, struct buffer *out);
64static int htx_compression_buffer_add_data(struct comp_state *st, const char *data, size_t len,
65 struct buffer *out);
66static int htx_compression_buffer_end(struct comp_state *st, struct buffer *out, int end);
67
Willy Tarreaud54a8ce2018-06-29 18:42:02 +020068static int http_compression_buffer_init(struct channel *inc, struct buffer *out, unsigned int *out_len);
Christopher Faulet92d36382015-11-05 13:35:03 +010069static int http_compression_buffer_add_data(struct comp_state *st,
70 struct buffer *in,
Willy Tarreaud54a8ce2018-06-29 18:42:02 +020071 int in_out,
Christopher Faulet92d36382015-11-05 13:35:03 +010072 struct buffer *out, int sz);
73static int http_compression_buffer_end(struct comp_state *st, struct stream *s,
Willy Tarreauc9fa0482018-07-10 17:43:27 +020074 struct channel *chn, struct buffer *out,
Willy Tarreaud54a8ce2018-06-29 18:42:02 +020075 unsigned int *out_len, int end);
Christopher Faulet92d36382015-11-05 13:35:03 +010076
77/***********************************************************************/
78static int
Christopher Faulete6902cd2018-11-30 22:29:48 +010079comp_flt_init(struct proxy *px, struct flt_conf *fconf)
80{
Christopher Faulet6e540952018-12-03 22:43:41 +010081 fconf->flags |= FLT_CFG_FL_HTX;
Christopher Faulete6902cd2018-11-30 22:29:48 +010082 return 0;
83}
84
85static int
Christopher Faulet8ca3b4b2017-07-25 11:07:15 +020086comp_flt_init_per_thread(struct proxy *px, struct flt_conf *fconf)
Christopher Faulet92d36382015-11-05 13:35:03 +010087{
Willy Tarreauc9fa0482018-07-10 17:43:27 +020088 if (!tmpbuf.size && b_alloc(&tmpbuf) == NULL)
Christopher Fauletb77c5c22015-12-07 16:48:42 +010089 return -1;
Willy Tarreauc9fa0482018-07-10 17:43:27 +020090 if (!zbuf.size && b_alloc(&zbuf) == NULL)
Christopher Fauletb77c5c22015-12-07 16:48:42 +010091 return -1;
Christopher Faulet92d36382015-11-05 13:35:03 +010092 return 0;
93}
94
95static void
Christopher Faulet8ca3b4b2017-07-25 11:07:15 +020096comp_flt_deinit_per_thread(struct proxy *px, struct flt_conf *fconf)
Christopher Faulet92d36382015-11-05 13:35:03 +010097{
Willy Tarreauc9fa0482018-07-10 17:43:27 +020098 if (tmpbuf.size)
Christopher Faulet92d36382015-11-05 13:35:03 +010099 b_free(&tmpbuf);
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200100 if (zbuf.size)
Christopher Fauletb77c5c22015-12-07 16:48:42 +0100101 b_free(&zbuf);
Christopher Faulet92d36382015-11-05 13:35:03 +0100102}
103
104static int
105comp_start_analyze(struct stream *s, struct filter *filter, struct channel *chn)
106{
Christopher Faulet8ca3b4b2017-07-25 11:07:15 +0200107
Christopher Faulet92d36382015-11-05 13:35:03 +0100108 if (filter->ctx == NULL) {
109 struct comp_state *st;
110
Willy Tarreaubafbe012017-11-24 17:34:44 +0100111 st = pool_alloc_dirty(pool_head_comp_state);
Christopher Fauleta03d4ad2017-06-26 16:53:33 +0200112 if (st == NULL)
Christopher Faulet92d36382015-11-05 13:35:03 +0100113 return -1;
114
Christopher Faulet2fb28802015-12-01 10:40:57 +0100115 st->comp_algo = NULL;
116 st->comp_ctx = NULL;
Christopher Fauletb77c5c22015-12-07 16:48:42 +0100117 st->hdrs_len = 0;
118 st->tlrs_len = 0;
Christopher Faulet2fb28802015-12-01 10:40:57 +0100119 st->consumed = 0;
120 st->initialized = 0;
Christopher Fauletb77c5c22015-12-07 16:48:42 +0100121 st->finished = 0;
Christopher Faulet2fb28802015-12-01 10:40:57 +0100122 filter->ctx = st;
Christopher Faulet3dc860d2017-09-15 11:39:36 +0200123
124 /* Register post-analyzer on AN_RES_WAIT_HTTP because we need to
125 * analyze response headers before http-response rules execution
126 * to be sure we can use res.comp and res.comp_algo sample
127 * fetches */
128 filter->post_analyzers |= AN_RES_WAIT_HTTP;
Christopher Faulet92d36382015-11-05 13:35:03 +0100129 }
130 return 1;
131}
132
133static int
Christopher Faulet92d36382015-11-05 13:35:03 +0100134comp_end_analyze(struct stream *s, struct filter *filter, struct channel *chn)
135{
136 struct comp_state *st = filter->ctx;
Christopher Faulet92d36382015-11-05 13:35:03 +0100137
Christopher Fauletd60b3cf2017-06-26 11:47:13 +0200138 if (!st)
Christopher Faulet92d36382015-11-05 13:35:03 +0100139 goto end;
140
Christopher Faulet92d36382015-11-05 13:35:03 +0100141 /* release any possible compression context */
Christopher Fauletd60b3cf2017-06-26 11:47:13 +0200142 if (st->comp_algo)
143 st->comp_algo->end(&st->comp_ctx);
Willy Tarreaubafbe012017-11-24 17:34:44 +0100144 pool_free(pool_head_comp_state, st);
Christopher Faulet92d36382015-11-05 13:35:03 +0100145 filter->ctx = NULL;
146 end:
147 return 1;
148}
149
150static int
Christopher Faulet1339d742016-05-11 16:48:33 +0200151comp_http_headers(struct stream *s, struct filter *filter, struct http_msg *msg)
152{
153 struct comp_state *st = filter->ctx;
154
155 if (!strm_fe(s)->comp && !s->be->comp)
156 goto end;
157
158 if (!(msg->chn->flags & CF_ISRESP))
159 select_compression_request_header(st, s, msg);
160 else {
Christopher Faulet3dc860d2017-09-15 11:39:36 +0200161 /* Response headers have already been checked in
162 * comp_http_post_analyze callback. */
Christopher Faulet1339d742016-05-11 16:48:33 +0200163 if (st->comp_algo) {
164 register_data_filter(s, msg->chn, filter);
Christopher Faulete6902cd2018-11-30 22:29:48 +0100165 if (!IS_HTX_STRM(s))
166 st->hdrs_len = s->txn->rsp.sov;
Christopher Faulet1339d742016-05-11 16:48:33 +0200167 }
168 }
169
170 end:
171 return 1;
172}
173
174static int
Christopher Faulet3dc860d2017-09-15 11:39:36 +0200175comp_http_post_analyze(struct stream *s, struct filter *filter,
176 struct channel *chn, unsigned an_bit)
177{
178 struct http_txn *txn = s->txn;
179 struct http_msg *msg = &txn->rsp;
180 struct comp_state *st = filter->ctx;
181
182 if (an_bit != AN_RES_WAIT_HTTP)
183 goto end;
184
185 if (!strm_fe(s)->comp && !s->be->comp)
186 goto end;
187
188 select_compression_response_header(st, s, msg);
189
190 end:
191 return 1;
192}
193
194static int
Christopher Faulete6902cd2018-11-30 22:29:48 +0100195comp_http_payload(struct stream *s, struct filter *filter, struct http_msg *msg,
196 unsigned int offset, unsigned int len)
197{
198 struct comp_state *st = filter->ctx;
Christopher Faulet27ba2dc2018-12-05 11:53:24 +0100199 struct htx *htx = htxbuf(&msg->chn->buf);
Christopher Faulete6902cd2018-11-30 22:29:48 +0100200 struct htx_blk *blk;
201 struct htx_ret htx_ret;
202 int ret, consumed = 0, to_forward = 0;
203
204 htx_ret = htx_find_blk(htx, offset);
205 blk = htx_ret.blk;
206 offset = htx_ret.ret;
207
208 while (blk && len) {
209 enum htx_blk_type type = htx_get_blk_type(blk);
210 uint32_t sz = htx_get_blksz(blk);
211 struct ist v;
212
213 switch (type) {
214 case HTX_BLK_UNUSED:
215 break;
216
217 case HTX_BLK_DATA:
218 v = htx_get_blk_value(htx, blk);
219 v.ptr += offset;
220 v.len -= offset;
221 if (v.len > len)
222 v.len = len;
223 if (htx_compression_buffer_init(htx, &trash) < 0) {
224 msg->chn->flags |= CF_WAKE_WRITE;
225 goto end;
226 }
227 ret = htx_compression_buffer_add_data(st, v.ptr, v.len, &trash);
228 if (ret < 0)
229 goto error;
230 if (htx_compression_buffer_end(st, &trash, 0) < 0)
231 goto error;
232 len -= ret;
233 consumed += ret;
234 to_forward += b_data(&trash);
235 if (ret == sz && !b_data(&trash)) {
236 offset = 0;
237 blk = htx_remove_blk(htx, blk);
238 continue;
239 }
240 v.len = ret;
241 blk = htx_replace_blk_value(htx, blk, v, ist2(b_head(&trash), b_data(&trash)));
242 break;
243
244 case HTX_BLK_EOD:
245 case HTX_BLK_TLR:
246 case HTX_BLK_EOM:
247 if (msg->flags & HTTP_MSGF_COMPRESSING) {
248 if (htx_compression_buffer_init(htx, &trash) < 0) {
249 msg->chn->flags |= CF_WAKE_WRITE;
250 goto end;
251 }
252 if (htx_compression_buffer_end(st, &trash, 1) < 0)
253 goto error;
254 blk = htx_add_data_before(htx, blk, ist2(b_head(&trash), b_data(&trash)));
255 if (!blk)
256 goto error;
257 to_forward += b_data(&trash);
258 msg->flags &= ~HTTP_MSGF_COMPRESSING;
259 /* We let the mux add last empty chunk and empty trailers */
260 }
261 /* fall through */
262
263 default:
264 sz -= offset;
265 if (sz > len)
266 sz = len;
267 consumed += sz;
268 to_forward += sz;
269 len -= sz;
270 break;
271 }
272
273 offset = 0;
274 blk = htx_get_next_blk(htx, blk);
275 }
276
277 end:
278 if (to_forward != consumed)
279 flt_update_offsets(filter, msg->chn, to_forward - consumed);
280
281 if (st->comp_ctx && st->comp_ctx->cur_lvl > 0) {
282 update_freq_ctr(&global.comp_bps_out, to_forward);
283 HA_ATOMIC_ADD(&strm_fe(s)->fe_counters.comp_out, to_forward);
284 HA_ATOMIC_ADD(&s->be->be_counters.comp_out, to_forward);
285 }
286 return to_forward;
287
288 error:
289 return -1;
290}
291
292static int
Christopher Faulet2fb28802015-12-01 10:40:57 +0100293comp_http_data(struct stream *s, struct filter *filter, struct http_msg *msg)
Christopher Faulet92d36382015-11-05 13:35:03 +0100294{
295 struct comp_state *st = filter->ctx;
Willy Tarreaubcbd3932018-06-06 07:13:22 +0200296 struct channel *chn = msg->chn;
Christopher Faulet3e7bc672015-12-07 13:39:08 +0100297 unsigned int *nxt = &flt_rsp_nxt(filter);
Christopher Faulet2fb28802015-12-01 10:40:57 +0100298 unsigned int len;
Christopher Faulet92d36382015-11-05 13:35:03 +0100299 int ret;
300
Olivier Houchard0b662842018-06-29 18:16:31 +0200301 len = MIN(msg->chunk_len + msg->next, ci_data(chn)) - *nxt;
Christopher Faulet2fb28802015-12-01 10:40:57 +0100302 if (!len)
303 return len;
304
305 if (!st->initialized) {
Christopher Fauletb77c5c22015-12-07 16:48:42 +0100306 unsigned int fwd = flt_rsp_fwd(filter) + st->hdrs_len;
Christopher Faulet3e7bc672015-12-07 13:39:08 +0100307
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200308 b_reset(&tmpbuf);
Willy Tarreaubcbd3932018-06-06 07:13:22 +0200309 c_adv(chn, fwd);
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200310 ret = http_compression_buffer_init(chn, &zbuf, &buf_output);
Willy Tarreaubcbd3932018-06-06 07:13:22 +0200311 c_rew(chn, fwd);
Christopher Faulet2fb28802015-12-01 10:40:57 +0100312 if (ret < 0) {
313 msg->chn->flags |= CF_WAKE_WRITE;
314 return 0;
315 }
316 }
Christopher Fauletb77c5c22015-12-07 16:48:42 +0100317
318 if (msg->flags & HTTP_MSGF_TE_CHNK) {
Christopher Faulet06ecf3a2016-09-22 15:31:43 +0200319 int block;
Christopher Fauletb77c5c22015-12-07 16:48:42 +0100320
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200321 len = MIN(b_room(&tmpbuf), len);
Christopher Faulet06ecf3a2016-09-22 15:31:43 +0200322
Willy Tarreaubcbd3932018-06-06 07:13:22 +0200323 c_adv(chn, *nxt);
Willy Tarreau7194d3c2018-06-06 16:55:45 +0200324 block = ci_contig_data(chn);
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200325 memcpy(b_tail(&tmpbuf), ci_head(chn), block);
Christopher Faulet06ecf3a2016-09-22 15:31:43 +0200326 if (len > block)
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200327 memcpy(b_tail(&tmpbuf)+block, b_orig(&chn->buf), len-block);
Willy Tarreaubcbd3932018-06-06 07:13:22 +0200328 c_rew(chn, *nxt);
Christopher Faulet06ecf3a2016-09-22 15:31:43 +0200329
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200330 b_add(&tmpbuf, len);
Christopher Fauletb77c5c22015-12-07 16:48:42 +0100331 ret = len;
332 }
333 else {
Willy Tarreaubcbd3932018-06-06 07:13:22 +0200334 c_adv(chn, *nxt);
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200335 ret = http_compression_buffer_add_data(st, &chn->buf, co_data(chn), &zbuf, len);
Willy Tarreaubcbd3932018-06-06 07:13:22 +0200336 c_rew(chn, *nxt);
Christopher Fauletb77c5c22015-12-07 16:48:42 +0100337 if (ret < 0)
338 return ret;
339 }
Christopher Faulet92d36382015-11-05 13:35:03 +0100340
Christopher Faulet2fb28802015-12-01 10:40:57 +0100341 st->initialized = 1;
342 msg->next += ret;
343 msg->chunk_len -= ret;
Christopher Faulet3e7bc672015-12-07 13:39:08 +0100344 *nxt = msg->next;
Christopher Faulet2fb28802015-12-01 10:40:57 +0100345 return 0;
Christopher Faulet92d36382015-11-05 13:35:03 +0100346}
347
348static int
Christopher Faulet2fb28802015-12-01 10:40:57 +0100349comp_http_chunk_trailers(struct stream *s, struct filter *filter,
350 struct http_msg *msg)
Christopher Faulet92d36382015-11-05 13:35:03 +0100351{
352 struct comp_state *st = filter->ctx;
Christopher Faulet92d36382015-11-05 13:35:03 +0100353
Christopher Fauletb77c5c22015-12-07 16:48:42 +0100354 if (!st->initialized) {
355 if (!st->finished) {
Willy Tarreaubcbd3932018-06-06 07:13:22 +0200356 struct channel *chn = msg->chn;
Christopher Fauletb77c5c22015-12-07 16:48:42 +0100357 unsigned int fwd = flt_rsp_fwd(filter) + st->hdrs_len;
Christopher Faulet2fb28802015-12-01 10:40:57 +0100358
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200359 b_reset(&tmpbuf);
Willy Tarreaubcbd3932018-06-06 07:13:22 +0200360 c_adv(chn, fwd);
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200361 http_compression_buffer_init(chn, &zbuf, &buf_output);
Willy Tarreaubcbd3932018-06-06 07:13:22 +0200362 c_rew(chn, fwd);
Christopher Fauletb77c5c22015-12-07 16:48:42 +0100363 st->initialized = 1;
364 }
365 }
366 st->tlrs_len = msg->sol;
Christopher Faulet2fb28802015-12-01 10:40:57 +0100367 return 1;
Christopher Faulet92d36382015-11-05 13:35:03 +0100368}
369
Christopher Faulet2fb28802015-12-01 10:40:57 +0100370
Christopher Faulet92d36382015-11-05 13:35:03 +0100371static int
372comp_http_forward_data(struct stream *s, struct filter *filter,
373 struct http_msg *msg, unsigned int len)
374{
375 struct comp_state *st = filter->ctx;
Christopher Faulet2fb28802015-12-01 10:40:57 +0100376 int ret;
Christopher Faulet92d36382015-11-05 13:35:03 +0100377
Christopher Faulet2fb28802015-12-01 10:40:57 +0100378 /* To work, previous filters MUST forward all data */
Christopher Fauletb77c5c22015-12-07 16:48:42 +0100379 if (flt_rsp_fwd(filter) + len != flt_rsp_nxt(filter)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100380 ha_warning("HTTP compression failed: unexpected behavior of previous filters\n");
Christopher Faulet2fb28802015-12-01 10:40:57 +0100381 return -1;
Christopher Faulet92d36382015-11-05 13:35:03 +0100382 }
383
Christopher Faulet2fb28802015-12-01 10:40:57 +0100384 if (!st->initialized) {
Christopher Fauletb77c5c22015-12-07 16:48:42 +0100385 if (!len) {
Joseph Herlant942eea32018-11-15 13:57:22 -0800386 /* Nothing to forward */
Christopher Fauletb77c5c22015-12-07 16:48:42 +0100387 ret = len;
388 }
389 else if (st->hdrs_len > len) {
390 /* Forward part of headers */
391 ret = len;
392 st->hdrs_len -= len;
393 }
394 else if (st->hdrs_len > 0) {
395 /* Forward remaining headers */
396 ret = st->hdrs_len;
397 st->hdrs_len = 0;
398 }
399 else if (msg->msg_state < HTTP_MSG_TRAILERS) {
400 /* Do not forward anything for now. This only happens
401 * with chunk-encoded responses. Waiting data are part
402 * of the chunk envelope (the chunk size or the chunk
403 * CRLF). These data will be skipped during the
404 * compression. */
405 ret = 0;
406 }
407 else {
408 /* Forward trailers data */
409 ret = len;
410 }
Christopher Faulet2fb28802015-12-01 10:40:57 +0100411 return ret;
Christopher Faulet92d36382015-11-05 13:35:03 +0100412 }
413
Christopher Fauletb77c5c22015-12-07 16:48:42 +0100414 if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200415 ret = http_compression_buffer_add_data(st, &tmpbuf, 0,
416 &zbuf, b_data(&tmpbuf));
417 if (ret != b_data(&tmpbuf)) {
Willy Tarreau506a29a2018-07-18 10:07:58 +0200418 ha_warning("HTTP compression failed: Must consume %u bytes but only %d bytes consumed\n",
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200419 (unsigned int)b_data(&tmpbuf), ret);
Christopher Fauletb77c5c22015-12-07 16:48:42 +0100420 return -1;
421 }
422 }
423
424 st->consumed = len - st->hdrs_len - st->tlrs_len;
Willy Tarreaubcbd3932018-06-06 07:13:22 +0200425 c_adv(msg->chn, flt_rsp_fwd(filter) + st->hdrs_len);
Willy Tarreaud54a8ce2018-06-29 18:42:02 +0200426 ret = http_compression_buffer_end(st, s, msg->chn, &zbuf, &buf_output, msg->msg_state >= HTTP_MSG_TRAILERS);
Willy Tarreaubcbd3932018-06-06 07:13:22 +0200427 c_rew(msg->chn, flt_rsp_fwd(filter) + st->hdrs_len);
Christopher Faulet2fb28802015-12-01 10:40:57 +0100428 if (ret < 0)
429 return ret;
Christopher Faulet92d36382015-11-05 13:35:03 +0100430
Christopher Fauletb77c5c22015-12-07 16:48:42 +0100431 flt_change_forward_size(filter, msg->chn, ret - st->consumed);
432 msg->next += (ret - st->consumed);
433 ret += st->hdrs_len + st->tlrs_len;
434
Christopher Faulet2fb28802015-12-01 10:40:57 +0100435 st->initialized = 0;
Christopher Fauletb77c5c22015-12-07 16:48:42 +0100436 st->finished = (msg->msg_state >= HTTP_MSG_TRAILERS);
437 st->hdrs_len = 0;
438 st->tlrs_len = 0;
Christopher Faulet92d36382015-11-05 13:35:03 +0100439 return ret;
440}
Christopher Faulet3d97c902015-12-09 14:59:38 +0100441
Christopher Fauletd60b3cf2017-06-26 11:47:13 +0200442static int
443comp_http_end(struct stream *s, struct filter *filter,
444 struct http_msg *msg)
445{
446 struct comp_state *st = filter->ctx;
447
448 if (!(msg->chn->flags & CF_ISRESP) || !st || !st->comp_algo)
449 goto end;
450
451 if (strm_fe(s)->mode == PR_MODE_HTTP)
Christopher Fauletff8abcd2017-06-02 15:33:24 +0200452 HA_ATOMIC_ADD(&strm_fe(s)->fe_counters.p.http.comp_rsp, 1);
Christopher Fauletd60b3cf2017-06-26 11:47:13 +0200453 if ((s->flags & SF_BE_ASSIGNED) && (s->be->mode == PR_MODE_HTTP))
Christopher Fauletff8abcd2017-06-02 15:33:24 +0200454 HA_ATOMIC_ADD(&s->be->be_counters.p.http.comp_rsp, 1);
Christopher Fauletd60b3cf2017-06-26 11:47:13 +0200455 end:
456 return 1;
457}
Christopher Faulet3d97c902015-12-09 14:59:38 +0100458/***********************************************************************/
459/*
460 * Selects a compression algorithm depending on the client request.
461 */
Christopher Faulete6902cd2018-11-30 22:29:48 +0100462static int
463http_select_comp_reqhdr(struct comp_state *st, struct stream *s, struct http_msg *msg)
Christopher Faulet3d97c902015-12-09 14:59:38 +0100464{
465 struct http_txn *txn = s->txn;
Olivier Houchard0b662842018-06-29 18:16:31 +0200466 struct channel *req = msg->chn;
Christopher Faulet3d97c902015-12-09 14:59:38 +0100467 struct hdr_ctx ctx;
468 struct comp_algo *comp_algo = NULL;
469 struct comp_algo *comp_algo_back = NULL;
470
471 /* Disable compression for older user agents announcing themselves as "Mozilla/4"
472 * unless they are known good (MSIE 6 with XP SP2, or MSIE 7 and later).
473 * See http://zoompf.com/2012/02/lose-the-wait-http-compression for more details.
474 */
475 ctx.idx = 0;
Olivier Houchard0b662842018-06-29 18:16:31 +0200476 if (http_find_header2("User-Agent", 10, ci_head(req), &txn->hdr_idx, &ctx) &&
Christopher Faulet3d97c902015-12-09 14:59:38 +0100477 ctx.vlen >= 9 &&
478 memcmp(ctx.line + ctx.val, "Mozilla/4", 9) == 0 &&
479 (ctx.vlen < 31 ||
480 memcmp(ctx.line + ctx.val + 25, "MSIE ", 5) != 0 ||
481 ctx.line[ctx.val + 30] < '6' ||
482 (ctx.line[ctx.val + 30] == '6' &&
483 (ctx.vlen < 54 || memcmp(ctx.line + 51, "SV1", 3) != 0)))) {
Christopher Faulet92d36382015-11-05 13:35:03 +0100484 st->comp_algo = NULL;
Christopher Faulet3d97c902015-12-09 14:59:38 +0100485 return 0;
486 }
487
488 /* search for the algo in the backend in priority or the frontend */
Christopher Faulet92d36382015-11-05 13:35:03 +0100489 if ((s->be->comp && (comp_algo_back = s->be->comp->algos)) ||
490 (strm_fe(s)->comp && (comp_algo_back = strm_fe(s)->comp->algos))) {
Christopher Faulet3d97c902015-12-09 14:59:38 +0100491 int best_q = 0;
492
493 ctx.idx = 0;
Olivier Houchard0b662842018-06-29 18:16:31 +0200494 while (http_find_header2("Accept-Encoding", 15, ci_head(req), &txn->hdr_idx, &ctx)) {
Christopher Faulet3d97c902015-12-09 14:59:38 +0100495 const char *qval;
496 int q;
497 int toklen;
498
499 /* try to isolate the token from the optional q-value */
500 toklen = 0;
Willy Tarreau2235b262016-11-05 15:50:20 +0100501 while (toklen < ctx.vlen && HTTP_IS_TOKEN(*(ctx.line + ctx.val + toklen)))
Christopher Faulet3d97c902015-12-09 14:59:38 +0100502 toklen++;
503
504 qval = ctx.line + ctx.val + toklen;
505 while (1) {
Willy Tarreau2235b262016-11-05 15:50:20 +0100506 while (qval < ctx.line + ctx.val + ctx.vlen && HTTP_IS_LWS(*qval))
Christopher Faulet3d97c902015-12-09 14:59:38 +0100507 qval++;
508
509 if (qval >= ctx.line + ctx.val + ctx.vlen || *qval != ';') {
510 qval = NULL;
511 break;
512 }
513 qval++;
514
Willy Tarreau2235b262016-11-05 15:50:20 +0100515 while (qval < ctx.line + ctx.val + ctx.vlen && HTTP_IS_LWS(*qval))
Christopher Faulet3d97c902015-12-09 14:59:38 +0100516 qval++;
517
518 if (qval >= ctx.line + ctx.val + ctx.vlen) {
519 qval = NULL;
520 break;
521 }
522 if (strncmp(qval, "q=", MIN(ctx.line + ctx.val + ctx.vlen - qval, 2)) == 0)
523 break;
524
525 while (qval < ctx.line + ctx.val + ctx.vlen && *qval != ';')
526 qval++;
527 }
528
529 /* here we have qval pointing to the first "q=" attribute or NULL if not found */
Willy Tarreauab813a42018-09-10 18:41:28 +0200530 q = qval ? http_parse_qvalue(qval + 2, NULL) : 1000;
Christopher Faulet3d97c902015-12-09 14:59:38 +0100531
532 if (q <= best_q)
533 continue;
534
535 for (comp_algo = comp_algo_back; comp_algo; comp_algo = comp_algo->next) {
536 if (*(ctx.line + ctx.val) == '*' ||
537 word_match(ctx.line + ctx.val, toklen, comp_algo->ua_name, comp_algo->ua_name_len)) {
Christopher Faulet92d36382015-11-05 13:35:03 +0100538 st->comp_algo = comp_algo;
Christopher Faulet3d97c902015-12-09 14:59:38 +0100539 best_q = q;
540 break;
541 }
542 }
543 }
544 }
545
546 /* remove all occurrences of the header when "compression offload" is set */
Christopher Faulet92d36382015-11-05 13:35:03 +0100547 if (st->comp_algo) {
548 if ((s->be->comp && s->be->comp->offload) ||
549 (strm_fe(s)->comp && strm_fe(s)->comp->offload)) {
Christopher Faulet3d97c902015-12-09 14:59:38 +0100550 http_remove_header2(msg, &txn->hdr_idx, &ctx);
551 ctx.idx = 0;
Olivier Houchard0b662842018-06-29 18:16:31 +0200552 while (http_find_header2("Accept-Encoding", 15, ci_head(req), &txn->hdr_idx, &ctx)) {
Christopher Faulet3d97c902015-12-09 14:59:38 +0100553 http_remove_header2(msg, &txn->hdr_idx, &ctx);
554 }
555 }
Christopher Faulete6902cd2018-11-30 22:29:48 +0100556 return 1;
557 }
558
559 /* identity is implicit does not require headers */
560 if ((s->be->comp && (comp_algo_back = s->be->comp->algos)) ||
561 (strm_fe(s)->comp && (comp_algo_back = strm_fe(s)->comp->algos))) {
562 for (comp_algo = comp_algo_back; comp_algo; comp_algo = comp_algo->next) {
563 if (comp_algo->cfg_name_len == 8 && memcmp(comp_algo->cfg_name, "identity", 8) == 0) {
564 st->comp_algo = comp_algo;
565 return 1;
566 }
567 }
568 }
569
570 st->comp_algo = NULL;
571 return 0;
572}
573
574static int
575htx_select_comp_reqhdr(struct comp_state *st, struct stream *s, struct http_msg *msg)
576{
Christopher Faulet27ba2dc2018-12-05 11:53:24 +0100577 struct htx *htx = htxbuf(&msg->chn->buf);
Christopher Faulete6902cd2018-11-30 22:29:48 +0100578 struct http_hdr_ctx ctx;
579 struct comp_algo *comp_algo = NULL;
580 struct comp_algo *comp_algo_back = NULL;
581
582 /* Disable compression for older user agents announcing themselves as "Mozilla/4"
583 * unless they are known good (MSIE 6 with XP SP2, or MSIE 7 and later).
584 * See http://zoompf.com/2012/02/lose-the-wait-http-compression for more details.
585 */
586 ctx.blk = NULL;
587 if (http_find_header(htx, ist("User-Agent"), &ctx, 1) &&
588 ctx.value.len >= 9 &&
589 memcmp(ctx.value.ptr, "Mozilla/4", 9) == 0 &&
590 (ctx.value.len < 31 ||
591 memcmp(ctx.value.ptr + 25, "MSIE ", 5) != 0 ||
592 *(ctx.value.ptr + 30) < '6' ||
593 (*(ctx.value.ptr + 30) == '6' &&
594 (ctx.value.len < 54 || memcmp(ctx.value.ptr + 51, "SV1", 3) != 0)))) {
595 st->comp_algo = NULL;
596 return 0;
597 }
598
599 /* search for the algo in the backend in priority or the frontend */
600 if ((s->be->comp && (comp_algo_back = s->be->comp->algos)) ||
601 (strm_fe(s)->comp && (comp_algo_back = strm_fe(s)->comp->algos))) {
602 int best_q = 0;
603
604 ctx.blk = NULL;
605 while (http_find_header(htx, ist("Accept-Encoding"), &ctx, 0)) {
606 const char *qval;
607 int q;
608 int toklen;
609
610 /* try to isolate the token from the optional q-value */
611 toklen = 0;
612 while (toklen < ctx.value.len && HTTP_IS_TOKEN(*(ctx.value.ptr + toklen)))
613 toklen++;
614
615 qval = ctx.value.ptr + toklen;
616 while (1) {
617 while (qval < ctx.value.ptr + ctx.value.len && HTTP_IS_LWS(*qval))
618 qval++;
619
620 if (qval >= ctx.value.ptr + ctx.value.len || *qval != ';') {
621 qval = NULL;
622 break;
623 }
624 qval++;
625
626 while (qval < ctx.value.ptr + ctx.value.len && HTTP_IS_LWS(*qval))
627 qval++;
628
629 if (qval >= ctx.value.ptr + ctx.value.len) {
630 qval = NULL;
631 break;
632 }
633 if (strncmp(qval, "q=", MIN(ctx.value.ptr + ctx.value.len - qval, 2)) == 0)
634 break;
635
636 while (qval < ctx.value.ptr + ctx.value.len && *qval != ';')
637 qval++;
638 }
639
640 /* here we have qval pointing to the first "q=" attribute or NULL if not found */
641 q = qval ? http_parse_qvalue(qval + 2, NULL) : 1000;
642
643 if (q <= best_q)
644 continue;
645
646 for (comp_algo = comp_algo_back; comp_algo; comp_algo = comp_algo->next) {
647 if (*(ctx.value.ptr) == '*' ||
648 word_match(ctx.value.ptr, toklen, comp_algo->ua_name, comp_algo->ua_name_len)) {
649 st->comp_algo = comp_algo;
650 best_q = q;
651 break;
652 }
653 }
654 }
655 }
656
657 /* remove all occurrences of the header when "compression offload" is set */
658 if (st->comp_algo) {
659 if ((s->be->comp && s->be->comp->offload) ||
660 (strm_fe(s)->comp && strm_fe(s)->comp->offload)) {
661 http_remove_header(htx, &ctx);
662 ctx.blk = NULL;
663 while (http_find_header(htx, ist("Accept-Encoding"), &ctx, 1))
664 http_remove_header(htx, &ctx);
665 }
Christopher Faulet3d97c902015-12-09 14:59:38 +0100666 return 1;
667 }
668
669 /* identity is implicit does not require headers */
Christopher Faulet92d36382015-11-05 13:35:03 +0100670 if ((s->be->comp && (comp_algo_back = s->be->comp->algos)) ||
671 (strm_fe(s)->comp && (comp_algo_back = strm_fe(s)->comp->algos))) {
Christopher Faulet3d97c902015-12-09 14:59:38 +0100672 for (comp_algo = comp_algo_back; comp_algo; comp_algo = comp_algo->next) {
673 if (comp_algo->cfg_name_len == 8 && memcmp(comp_algo->cfg_name, "identity", 8) == 0) {
Christopher Faulet92d36382015-11-05 13:35:03 +0100674 st->comp_algo = comp_algo;
Christopher Faulet3d97c902015-12-09 14:59:38 +0100675 return 1;
676 }
677 }
678 }
679
Christopher Faulet92d36382015-11-05 13:35:03 +0100680 st->comp_algo = NULL;
Christopher Faulet3d97c902015-12-09 14:59:38 +0100681 return 0;
682}
683
Christopher Faulete6902cd2018-11-30 22:29:48 +0100684static int
685select_compression_request_header(struct comp_state *st, struct stream *s,
686 struct http_msg *msg)
687{
688 if (IS_HTX_STRM(s))
689 return htx_select_comp_reqhdr(st, s, msg);
690 else
691 return http_select_comp_reqhdr(st, s, msg);
692}
Christopher Faulet92d36382015-11-05 13:35:03 +0100693
Christopher Faulet3d97c902015-12-09 14:59:38 +0100694/*
695 * Selects a comression algorithm depending of the server response.
696 */
Christopher Faulet92d36382015-11-05 13:35:03 +0100697static int
Christopher Faulete6902cd2018-11-30 22:29:48 +0100698http_select_comp_reshdr(struct comp_state *st, struct stream *s, struct http_msg *msg)
Christopher Faulet3d97c902015-12-09 14:59:38 +0100699{
700 struct http_txn *txn = s->txn;
Olivier Houchard0b662842018-06-29 18:16:31 +0200701 struct channel *c = msg->chn;
Christopher Faulet3d97c902015-12-09 14:59:38 +0100702 struct hdr_ctx ctx;
703 struct comp_type *comp_type;
704
705 /* no common compression algorithm was found in request header */
Christopher Faulet92d36382015-11-05 13:35:03 +0100706 if (st->comp_algo == NULL)
Christopher Faulet3d97c902015-12-09 14:59:38 +0100707 goto fail;
708
709 /* HTTP < 1.1 should not be compressed */
710 if (!(msg->flags & HTTP_MSGF_VER_11) || !(txn->req.flags & HTTP_MSGF_VER_11))
711 goto fail;
712
Christopher Faulet92d36382015-11-05 13:35:03 +0100713 if (txn->meth == HTTP_METH_HEAD)
714 goto fail;
715
Christopher Faulet3d97c902015-12-09 14:59:38 +0100716 /* compress 200,201,202,203 responses only */
717 if ((txn->status != 200) &&
718 (txn->status != 201) &&
719 (txn->status != 202) &&
720 (txn->status != 203))
721 goto fail;
722
723
724 /* Content-Length is null */
725 if (!(msg->flags & HTTP_MSGF_TE_CHNK) && msg->body_len == 0)
726 goto fail;
727
728 /* content is already compressed */
729 ctx.idx = 0;
Olivier Houchard0b662842018-06-29 18:16:31 +0200730 if (http_find_header2("Content-Encoding", 16, ci_head(c), &txn->hdr_idx, &ctx))
Christopher Faulet3d97c902015-12-09 14:59:38 +0100731 goto fail;
732
733 /* no compression when Cache-Control: no-transform is present in the message */
734 ctx.idx = 0;
Olivier Houchard0b662842018-06-29 18:16:31 +0200735 while (http_find_header2("Cache-Control", 13, ci_head(c), &txn->hdr_idx, &ctx)) {
Christopher Faulet3d97c902015-12-09 14:59:38 +0100736 if (word_match(ctx.line + ctx.val, ctx.vlen, "no-transform", 12))
737 goto fail;
738 }
739
740 comp_type = NULL;
741
742 /* we don't want to compress multipart content-types, nor content-types that are
743 * not listed in the "compression type" directive if any. If no content-type was
744 * found but configuration requires one, we don't compress either. Backend has
745 * the priority.
746 */
747 ctx.idx = 0;
Olivier Houchard0b662842018-06-29 18:16:31 +0200748 if (http_find_header2("Content-Type", 12, ci_head(c), &txn->hdr_idx, &ctx)) {
Christopher Faulet3d97c902015-12-09 14:59:38 +0100749 if (ctx.vlen >= 9 && strncasecmp("multipart", ctx.line+ctx.val, 9) == 0)
750 goto fail;
751
752 if ((s->be->comp && (comp_type = s->be->comp->types)) ||
753 (strm_fe(s)->comp && (comp_type = strm_fe(s)->comp->types))) {
754 for (; comp_type; comp_type = comp_type->next) {
755 if (ctx.vlen >= comp_type->name_len &&
756 strncasecmp(ctx.line+ctx.val, comp_type->name, comp_type->name_len) == 0)
757 /* this Content-Type should be compressed */
758 break;
759 }
760 /* this Content-Type should not be compressed */
761 if (comp_type == NULL)
762 goto fail;
763 }
764 }
765 else { /* no content-type header */
Christopher Faulet92d36382015-11-05 13:35:03 +0100766 if ((s->be->comp && s->be->comp->types) ||
767 (strm_fe(s)->comp && strm_fe(s)->comp->types))
Christopher Faulet3d97c902015-12-09 14:59:38 +0100768 goto fail; /* a content-type was required */
769 }
770
771 /* limit compression rate */
772 if (global.comp_rate_lim > 0)
773 if (read_freq_ctr(&global.comp_bps_in) > global.comp_rate_lim)
774 goto fail;
775
776 /* limit cpu usage */
777 if (idle_pct < compress_min_idle)
778 goto fail;
779
780 /* initialize compression */
Christopher Faulet92d36382015-11-05 13:35:03 +0100781 if (st->comp_algo->init(&st->comp_ctx, global.tune.comp_maxlevel) < 0)
Christopher Faulet3d97c902015-12-09 14:59:38 +0100782 goto fail;
783
Christopher Faulet3d97c902015-12-09 14:59:38 +0100784 /* remove Content-Length header */
785 ctx.idx = 0;
Olivier Houchard0b662842018-06-29 18:16:31 +0200786 if ((msg->flags & HTTP_MSGF_CNT_LEN) && http_find_header2("Content-Length", 14, ci_head(c), &txn->hdr_idx, &ctx))
Christopher Faulet3d97c902015-12-09 14:59:38 +0100787 http_remove_header2(msg, &txn->hdr_idx, &ctx);
788
789 /* add Transfer-Encoding header */
790 if (!(msg->flags & HTTP_MSGF_TE_CHNK))
791 http_header_add_tail2(&txn->rsp, &txn->hdr_idx, "Transfer-Encoding: chunked", 26);
792
793 /*
794 * Add Content-Encoding header when it's not identity encoding.
795 * RFC 2616 : Identity encoding: This content-coding is used only in the
796 * Accept-Encoding header, and SHOULD NOT be used in the Content-Encoding
797 * header.
798 */
Christopher Faulet92d36382015-11-05 13:35:03 +0100799 if (st->comp_algo->cfg_name_len != 8 || memcmp(st->comp_algo->cfg_name, "identity", 8) != 0) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200800 trash.data = 18;
801 memcpy(trash.area, "Content-Encoding: ", trash.data);
802 memcpy(trash.area + trash.data, st->comp_algo->ua_name,
803 st->comp_algo->ua_name_len);
804 trash.data += st->comp_algo->ua_name_len;
805 trash.area[trash.data] = '\0';
806 http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.area,
807 trash.data);
Christopher Faulet3d97c902015-12-09 14:59:38 +0100808 }
Christopher Faulet92d36382015-11-05 13:35:03 +0100809 msg->flags |= HTTP_MSGF_COMPRESSING;
Christopher Faulet3d97c902015-12-09 14:59:38 +0100810 return 1;
811
812fail:
Christopher Faulet92d36382015-11-05 13:35:03 +0100813 st->comp_algo = NULL;
Christopher Faulet3d97c902015-12-09 14:59:38 +0100814 return 0;
815}
816
Christopher Faulete6902cd2018-11-30 22:29:48 +0100817static int
818htx_select_comp_reshdr(struct comp_state *st, struct stream *s, struct http_msg *msg)
819{
Christopher Faulet27ba2dc2018-12-05 11:53:24 +0100820 struct htx *htx = htxbuf(&msg->chn->buf);
Christopher Faulete6902cd2018-11-30 22:29:48 +0100821 struct http_txn *txn = s->txn;
822 struct http_hdr_ctx ctx;
823 struct comp_type *comp_type;
824
825 /* no common compression algorithm was found in request header */
826 if (st->comp_algo == NULL)
827 goto fail;
828
829 /* HTTP < 1.1 should not be compressed */
830 if (!(msg->flags & HTTP_MSGF_VER_11) || !(txn->req.flags & HTTP_MSGF_VER_11))
831 goto fail;
832
833 if (txn->meth == HTTP_METH_HEAD)
834 goto fail;
835
836 /* compress 200,201,202,203 responses only */
837 if ((txn->status != 200) &&
838 (txn->status != 201) &&
839 (txn->status != 202) &&
840 (txn->status != 203))
841 goto fail;
842
843 if (msg->flags & HTTP_MSGF_BODYLESS)
844 goto fail;
845
846 /* content is already compressed */
847 ctx.blk = NULL;
848 if (http_find_header(htx, ist("Content-Encoding"), &ctx, 1))
849 goto fail;
850
851 /* no compression when Cache-Control: no-transform is present in the message */
852 ctx.blk = NULL;
853 while (http_find_header(htx, ist("Cache-Control"), &ctx, 0)) {
854 if (word_match(ctx.value.ptr, ctx.value.len, "no-transform", 12))
855 goto fail;
856 }
857
858 comp_type = NULL;
859
860 /* we don't want to compress multipart content-types, nor content-types that are
861 * not listed in the "compression type" directive if any. If no content-type was
862 * found but configuration requires one, we don't compress either. Backend has
863 * the priority.
864 */
865 ctx.blk = NULL;
866 if (http_find_header(htx, ist("Content-Type"), &ctx, 1)) {
867 if (ctx.value.len >= 9 && strncasecmp("multipart", ctx.value.ptr, 9) == 0)
868 goto fail;
869
870 if ((s->be->comp && (comp_type = s->be->comp->types)) ||
871 (strm_fe(s)->comp && (comp_type = strm_fe(s)->comp->types))) {
872 for (; comp_type; comp_type = comp_type->next) {
873 if (ctx.value.len >= comp_type->name_len &&
874 strncasecmp(ctx.value.ptr, comp_type->name, comp_type->name_len) == 0)
875 /* this Content-Type should be compressed */
876 break;
877 }
878 /* this Content-Type should not be compressed */
879 if (comp_type == NULL)
880 goto fail;
881 }
882 }
883 else { /* no content-type header */
884 if ((s->be->comp && s->be->comp->types) ||
885 (strm_fe(s)->comp && strm_fe(s)->comp->types))
886 goto fail; /* a content-type was required */
887 }
888
889 /* limit compression rate */
890 if (global.comp_rate_lim > 0)
891 if (read_freq_ctr(&global.comp_bps_in) > global.comp_rate_lim)
892 goto fail;
893
894 /* limit cpu usage */
895 if (idle_pct < compress_min_idle)
896 goto fail;
897
898 /* initialize compression */
899 if (st->comp_algo->init(&st->comp_ctx, global.tune.comp_maxlevel) < 0)
900 goto fail;
901
902 /*
903 * Add Content-Encoding header when it's not identity encoding.
904 * RFC 2616 : Identity encoding: This content-coding is used only in the
905 * Accept-Encoding header, and SHOULD NOT be used in the Content-Encoding
906 * header.
907 */
908 if (st->comp_algo->cfg_name_len != 8 || memcmp(st->comp_algo->cfg_name, "identity", 8) != 0) {
909 struct ist v = ist2(st->comp_algo->ua_name, st->comp_algo->ua_name_len);
910
911 if (!http_add_header(htx, ist("Content-Encoding"), v))
912 goto deinit_comp_ctx;
913 }
914
915 /* remove Content-Length header */
916 if (msg->flags & HTTP_MSGF_CNT_LEN) {
917 ctx.blk = NULL;
918
919 while (http_find_header(htx, ist("Content-Length"), &ctx, 1))
920 http_remove_header(htx, &ctx);
921 }
922
923 /* add "Transfer-Encoding: chunked" header */
924 if (!(msg->flags & HTTP_MSGF_TE_CHNK)) {
925 if (!http_add_header(htx, ist("Transfer-Encoding"), ist("chunked")))
926 goto deinit_comp_ctx;
927 }
928
929 msg->flags |= HTTP_MSGF_COMPRESSING;
930 return 1;
931
932 deinit_comp_ctx:
933 st->comp_algo->end(&st->comp_ctx);
934 fail:
935 st->comp_algo = NULL;
936 return 0;
937}
938
939static int
940select_compression_response_header(struct comp_state *st, struct stream *s, struct http_msg *msg)
941{
942 if (IS_HTX_STRM(s))
943 return htx_select_comp_reshdr(st, s, msg);
944 else
945 return http_select_comp_reshdr(st, s, msg);
946}
Christopher Faulet3d97c902015-12-09 14:59:38 +0100947/***********************************************************************/
948/* emit the chunksize followed by a CRLF on the output and return the number of
949 * bytes written. It goes backwards and starts with the byte before <end>. It
950 * returns the number of bytes written which will not exceed 10 (8 digits, CR,
951 * and LF). The caller is responsible for ensuring there is enough room left in
952 * the output buffer for the string.
953 */
954static int
955http_emit_chunk_size(char *end, unsigned int chksz)
956{
957 char *beg = end;
958
959 *--beg = '\n';
960 *--beg = '\r';
961 do {
962 *--beg = hextab[chksz & 0xF];
963 } while (chksz >>= 4);
964 return end - beg;
965}
966
967/*
968 * Init HTTP compression
969 */
Christopher Faulet92d36382015-11-05 13:35:03 +0100970static int
Willy Tarreaud54a8ce2018-06-29 18:42:02 +0200971http_compression_buffer_init(struct channel *inc, struct buffer *out, unsigned int *out_len)
Christopher Faulet3d97c902015-12-09 14:59:38 +0100972{
973 /* output stream requires at least 10 bytes for the gzip header, plus
974 * at least 8 bytes for the gzip trailer (crc+len), plus a possible
975 * plus at most 5 bytes per 32kB block and 2 bytes to close the stream.
976 */
Olivier Houchard0b662842018-06-29 18:16:31 +0200977 if (c_room(inc) < 20 + 5 * ((ci_data(inc) + 32767) >> 15))
Christopher Faulet3d97c902015-12-09 14:59:38 +0100978 return -1;
979
980 /* prepare an empty output buffer in which we reserve enough room for
981 * copying the output bytes from <in>, plus 10 extra bytes to write
982 * the chunk size. We don't copy the bytes yet so that if we have to
983 * cancel the operation later, it's cheap.
984 */
985 b_reset(out);
Willy Tarreaud54a8ce2018-06-29 18:42:02 +0200986 *out_len = co_data(inc);
987 out->head += *out_len + 10;
Christopher Faulet3d97c902015-12-09 14:59:38 +0100988 return 0;
989}
990
Christopher Faulete6902cd2018-11-30 22:29:48 +0100991static int
992htx_compression_buffer_init(struct htx *htx, struct buffer *out)
993{
994 /* output stream requires at least 10 bytes for the gzip header, plus
995 * at least 8 bytes for the gzip trailer (crc+len), plus a possible
996 * plus at most 5 bytes per 32kB block and 2 bytes to close the stream.
997 */
998 if (htx_free_space(htx) < 20 + 5 * ((htx->data + 32767) >> 15))
999 return -1;
1000 b_reset(out);
1001 return 0;
1002}
1003
Christopher Faulet3d97c902015-12-09 14:59:38 +01001004/*
1005 * Add data to compress
1006 */
Christopher Faulet92d36382015-11-05 13:35:03 +01001007static int
1008http_compression_buffer_add_data(struct comp_state *st, struct buffer *in,
Willy Tarreaud54a8ce2018-06-29 18:42:02 +02001009 int in_out, struct buffer *out, int sz)
Christopher Faulet3d97c902015-12-09 14:59:38 +01001010{
Christopher Faulet3d97c902015-12-09 14:59:38 +01001011 int consumed_data = 0;
1012 int data_process_len;
1013 int block1, block2;
1014
Christopher Faulet92d36382015-11-05 13:35:03 +01001015 if (!sz)
Christopher Faulet3e7bc672015-12-07 13:39:08 +01001016 goto end;
Christopher Faulet3d97c902015-12-09 14:59:38 +01001017
Christopher Faulet92d36382015-11-05 13:35:03 +01001018 /* select the smallest size between the announced chunk size, the input
Christopher Faulet3d97c902015-12-09 14:59:38 +01001019 * data, and the available output buffer size. The compressors are
Christopher Faulet92d36382015-11-05 13:35:03 +01001020 * assumed to be able to process all the bytes we pass to them at
1021 * once. */
Willy Tarreaueac52592018-06-15 13:59:36 +02001022 data_process_len = MIN(b_room(out), sz);
Christopher Faulet92d36382015-11-05 13:35:03 +01001023
Christopher Faulet3d97c902015-12-09 14:59:38 +01001024 block1 = data_process_len;
Willy Tarreaud54a8ce2018-06-29 18:42:02 +02001025 if (block1 > b_contig_data(in, in_out))
1026 block1 = b_contig_data(in, in_out);
Christopher Faulet3d97c902015-12-09 14:59:38 +01001027 block2 = data_process_len - block1;
1028
1029 /* compressors return < 0 upon error or the amount of bytes read */
Willy Tarreaud54a8ce2018-06-29 18:42:02 +02001030 consumed_data = st->comp_algo->add_data(st->comp_ctx, b_head(in) + in_out, block1, out);
Christopher Faulet3e7bc672015-12-07 13:39:08 +01001031 if (consumed_data != block1 || !block2)
1032 goto end;
Willy Tarreaud54a8ce2018-06-29 18:42:02 +02001033 consumed_data = st->comp_algo->add_data(st->comp_ctx, b_peek(in, 0), block2, out);
Christopher Faulet3e7bc672015-12-07 13:39:08 +01001034 if (consumed_data < 0)
1035 goto end;
1036 consumed_data += block1;
1037
1038 end:
Christopher Faulet3d97c902015-12-09 14:59:38 +01001039 return consumed_data;
1040}
1041
Christopher Faulete6902cd2018-11-30 22:29:48 +01001042static int
1043htx_compression_buffer_add_data(struct comp_state *st, const char *data, size_t len,
1044 struct buffer *out)
1045{
1046 return st->comp_algo->add_data(st->comp_ctx, data, len, out);
1047}
1048
Christopher Faulet3d97c902015-12-09 14:59:38 +01001049/*
1050 * Flush data in process, and write the header and footer of the chunk. Upon
1051 * success, in and out buffers are swapped to avoid a copy.
1052 */
Christopher Faulet92d36382015-11-05 13:35:03 +01001053static int
1054http_compression_buffer_end(struct comp_state *st, struct stream *s,
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001055 struct channel *chn, struct buffer *out,
Willy Tarreaud54a8ce2018-06-29 18:42:02 +02001056 unsigned int *buf_out, int end)
Christopher Faulet3d97c902015-12-09 14:59:38 +01001057{
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001058 struct buffer tmp_buf;
Christopher Faulet3d97c902015-12-09 14:59:38 +01001059 char *tail;
Christopher Faulet92d36382015-11-05 13:35:03 +01001060 int to_forward, left;
Willy Tarreaud54a8ce2018-06-29 18:42:02 +02001061 unsigned int tmp_out;
Christopher Faulet3d97c902015-12-09 14:59:38 +01001062
1063#if defined(USE_SLZ) || defined(USE_ZLIB)
1064 int ret;
1065
1066 /* flush data here */
Christopher Faulet3d97c902015-12-09 14:59:38 +01001067 if (end)
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001068 ret = st->comp_algo->finish(st->comp_ctx, out); /* end of data */
Christopher Faulet3d97c902015-12-09 14:59:38 +01001069 else
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001070 ret = st->comp_algo->flush(st->comp_ctx, out); /* end of buffer */
Christopher Faulet3d97c902015-12-09 14:59:38 +01001071
1072 if (ret < 0)
1073 return -1; /* flush failed */
1074
1075#endif /* USE_ZLIB */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001076 if (b_data(out) == 0) {
Christopher Faulet3d97c902015-12-09 14:59:38 +01001077 /* No data were appended, let's drop the output buffer and
1078 * keep the input buffer unchanged.
1079 */
1080 return 0;
1081 }
1082
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001083 /* OK so at this stage, we have an output buffer <out> looking like this :
Christopher Faulet3d97c902015-12-09 14:59:38 +01001084 *
1085 * <-- o --> <------ i ----->
1086 * +---------+---+------------+-----------+
1087 * | out | c | comp_in | empty |
1088 * +---------+---+------------+-----------+
1089 * data p size
1090 *
Willy Tarreaud54a8ce2018-06-29 18:42:02 +02001091 * <out> is the room reserved to copy the channel output. It starts at
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001092 * out->area and has not yet been filled. <c> is the room reserved to
Willy Tarreaud54a8ce2018-06-29 18:42:02 +02001093 * write the chunk size (10 bytes). <comp_in> is the compressed
1094 * equivalent of the data part of ib->len. <empty> is the amount of
1095 * empty bytes at the end of the buffer, into which we may have to
1096 * copy the remaining bytes from ib->len after the data
1097 * (chunk size, trailers, ...).
Christopher Faulet3d97c902015-12-09 14:59:38 +01001098 */
1099
Joseph Herlant942eea32018-11-15 13:57:22 -08001100 /* Write real size at the beginning of the chunk, no need of wrapping.
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001101 * We write the chunk using a dynamic length and adjust out->p and out->i
Christopher Faulet3d97c902015-12-09 14:59:38 +01001102 * accordingly afterwards. That will move <out> away from <data>.
1103 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001104 left = http_emit_chunk_size(b_head(out), b_data(out));
1105 b_add(out, left);
1106 out->head -= *buf_out + (left);
1107 /* Copy previous data from chn into out */
Willy Tarreaud54a8ce2018-06-29 18:42:02 +02001108 if (co_data(chn) > 0) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001109 left = b_contig_data(&chn->buf, 0);
Willy Tarreaud54a8ce2018-06-29 18:42:02 +02001110 if (left > *buf_out)
1111 left = *buf_out;
Christopher Faulet3d97c902015-12-09 14:59:38 +01001112
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001113 memcpy(b_head(out), co_head(chn), left);
1114 b_add(out, left);
Willy Tarreaud54a8ce2018-06-29 18:42:02 +02001115 if (co_data(chn) - left) {/* second part of the buffer */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001116 memcpy(b_head(out) + left, b_orig(&chn->buf), co_data(chn) - left);
1117 b_add(out, co_data(chn) - left);
Willy Tarreaud54a8ce2018-06-29 18:42:02 +02001118 }
Christopher Faulet3d97c902015-12-09 14:59:38 +01001119 }
1120
1121 /* chunked encoding requires CRLF after data */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001122 tail = b_tail(out);
Christopher Faulet3d97c902015-12-09 14:59:38 +01001123 *tail++ = '\r';
1124 *tail++ = '\n';
1125
Christopher Faulet2fb28802015-12-01 10:40:57 +01001126 /* At the end of data, we must write the empty chunk 0<CRLF>,
1127 * and terminate the trailers section with a last <CRLF>. If
1128 * we're forwarding a chunked-encoded response, we'll have a
1129 * trailers section after the empty chunk which needs to be
1130 * forwarded and which will provide the last CRLF. Otherwise
1131 * we write it ourselves.
1132 */
1133 if (end) {
1134 struct http_msg *msg = &s->txn->rsp;
1135
1136 memcpy(tail, "0\r\n", 3);
1137 tail += 3;
Christopher Fauletb77c5c22015-12-07 16:48:42 +01001138 if (!(msg->flags & HTTP_MSGF_TE_CHNK)) {
Christopher Faulet2fb28802015-12-01 10:40:57 +01001139 memcpy(tail, "\r\n", 2);
1140 tail += 2;
1141 }
1142 }
1143
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001144 b_add(out, tail - b_tail(out));
1145 to_forward = b_data(out) - *buf_out;
Christopher Faulet3d97c902015-12-09 14:59:38 +01001146
1147 /* update input rate */
Christopher Faulet92d36382015-11-05 13:35:03 +01001148 if (st->comp_ctx && st->comp_ctx->cur_lvl > 0) {
Christopher Faulet2fb28802015-12-01 10:40:57 +01001149 update_freq_ctr(&global.comp_bps_in, st->consumed);
Christopher Fauletff8abcd2017-06-02 15:33:24 +02001150 HA_ATOMIC_ADD(&strm_fe(s)->fe_counters.comp_in, st->consumed);
1151 HA_ATOMIC_ADD(&s->be->be_counters.comp_in, st->consumed);
Christopher Faulet3d97c902015-12-09 14:59:38 +01001152 } else {
Christopher Fauletff8abcd2017-06-02 15:33:24 +02001153 HA_ATOMIC_ADD(&strm_fe(s)->fe_counters.comp_byp, st->consumed);
1154 HA_ATOMIC_ADD(&s->be->be_counters.comp_byp, st->consumed);
Christopher Faulet3d97c902015-12-09 14:59:38 +01001155 }
1156
1157 /* copy the remaining data in the tmp buffer. */
Willy Tarreaubcbd3932018-06-06 07:13:22 +02001158 c_adv(chn, st->consumed);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001159 if (b_data(&chn->buf) - co_data(chn) > 0) {
Willy Tarreau7194d3c2018-06-06 16:55:45 +02001160 left = ci_contig_data(chn);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001161 memcpy(b_tail(out), ci_head(chn), left);
1162 b_add(out, left);
1163 if (b_data(&chn->buf) - (co_data(chn) + left)) {
1164 memcpy(b_tail(out), b_orig(&chn->buf), b_data(&chn->buf) - left);
1165 b_add(out, b_data(&chn->buf) - left);
Christopher Faulet3d97c902015-12-09 14:59:38 +01001166 }
1167 }
Christopher Faulet3d97c902015-12-09 14:59:38 +01001168 /* swap the buffers */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001169 tmp_buf = chn->buf;
1170 chn->buf = *out;
1171 *out = tmp_buf;
1172
Olivier Houchard08afac02018-06-22 19:26:39 +02001173 tmp_out = chn->output;
1174 chn->output = *buf_out;
Willy Tarreaud54a8ce2018-06-29 18:42:02 +02001175 *buf_out = tmp_out;
1176
Christopher Faulet3d97c902015-12-09 14:59:38 +01001177
Christopher Faulet92d36382015-11-05 13:35:03 +01001178
1179 if (st->comp_ctx && st->comp_ctx->cur_lvl > 0) {
Christopher Faulet3d97c902015-12-09 14:59:38 +01001180 update_freq_ctr(&global.comp_bps_out, to_forward);
Christopher Fauletff8abcd2017-06-02 15:33:24 +02001181 HA_ATOMIC_ADD(&strm_fe(s)->fe_counters.comp_out, to_forward);
1182 HA_ATOMIC_ADD(&s->be->be_counters.comp_out, to_forward);
Christopher Faulet3d97c902015-12-09 14:59:38 +01001183 }
1184
Christopher Faulet3d97c902015-12-09 14:59:38 +01001185 return to_forward;
1186}
1187
Christopher Faulete6902cd2018-11-30 22:29:48 +01001188static int
1189htx_compression_buffer_end(struct comp_state *st, struct buffer *out, int end)
1190{
1191 if (end)
1192 return st->comp_algo->finish(st->comp_ctx, out);
1193 else
1194 return st->comp_algo->flush(st->comp_ctx, out);
1195}
1196
Christopher Faulet3d97c902015-12-09 14:59:38 +01001197
1198/***********************************************************************/
Christopher Faulet92d36382015-11-05 13:35:03 +01001199struct flt_ops comp_ops = {
Christopher Faulete6902cd2018-11-30 22:29:48 +01001200 .init = comp_flt_init,
Christopher Faulet8ca3b4b2017-07-25 11:07:15 +02001201 .init_per_thread = comp_flt_init_per_thread,
1202 .deinit_per_thread = comp_flt_deinit_per_thread,
Christopher Faulet92d36382015-11-05 13:35:03 +01001203
1204 .channel_start_analyze = comp_start_analyze,
Christopher Faulet92d36382015-11-05 13:35:03 +01001205 .channel_end_analyze = comp_end_analyze,
Christopher Faulet3dc860d2017-09-15 11:39:36 +02001206 .channel_post_analyze = comp_http_post_analyze,
Christopher Faulet92d36382015-11-05 13:35:03 +01001207
Christopher Faulet1339d742016-05-11 16:48:33 +02001208 .http_headers = comp_http_headers,
Christopher Faulete6902cd2018-11-30 22:29:48 +01001209 .http_payload = comp_http_payload,
1210 .http_end = comp_http_end,
1211
Christopher Faulet309c6412015-12-02 09:57:32 +01001212 .http_data = comp_http_data,
1213 .http_chunk_trailers = comp_http_chunk_trailers,
1214 .http_forward_data = comp_http_forward_data,
Christopher Faulet92d36382015-11-05 13:35:03 +01001215};
1216
Christopher Faulet3d97c902015-12-09 14:59:38 +01001217static int
1218parse_compression_options(char **args, int section, struct proxy *proxy,
1219 struct proxy *defpx, const char *file, int line,
1220 char **err)
1221{
Christopher Faulet92d36382015-11-05 13:35:03 +01001222 struct comp *comp;
Christopher Faulet3d97c902015-12-09 14:59:38 +01001223
1224 if (proxy->comp == NULL) {
Vincent Bernat02779b62016-04-03 13:48:43 +02001225 comp = calloc(1, sizeof(*comp));
Christopher Faulet3d97c902015-12-09 14:59:38 +01001226 proxy->comp = comp;
1227 }
1228 else
1229 comp = proxy->comp;
1230
1231 if (!strcmp(args[1], "algo")) {
1232 struct comp_ctx *ctx;
1233 int cur_arg = 2;
1234
1235 if (!*args[cur_arg]) {
1236 memprintf(err, "parsing [%s:%d] : '%s' expects <algorithm>\n",
1237 file, line, args[0]);
1238 return -1;
1239 }
1240 while (*(args[cur_arg])) {
1241 if (comp_append_algo(comp, args[cur_arg]) < 0) {
1242 memprintf(err, "'%s' : '%s' is not a supported algorithm.\n",
1243 args[0], args[cur_arg]);
1244 return -1;
1245 }
1246 if (proxy->comp->algos->init(&ctx, 9) == 0)
1247 proxy->comp->algos->end(&ctx);
1248 else {
1249 memprintf(err, "'%s' : Can't init '%s' algorithm.\n",
1250 args[0], args[cur_arg]);
1251 return -1;
1252 }
1253 cur_arg++;
1254 continue;
1255 }
1256 }
1257 else if (!strcmp(args[1], "offload"))
1258 comp->offload = 1;
1259 else if (!strcmp(args[1], "type")) {
1260 int cur_arg = 2;
1261
1262 if (!*args[cur_arg]) {
1263 memprintf(err, "'%s' expects <type>\n", args[0]);
1264 return -1;
1265 }
1266 while (*(args[cur_arg])) {
1267 comp_append_type(comp, args[cur_arg]);
1268 cur_arg++;
1269 continue;
1270 }
1271 }
1272 else {
1273 memprintf(err, "'%s' expects 'algo', 'type' or 'offload'\n",
1274 args[0]);
1275 return -1;
1276 }
1277
1278 return 0;
1279}
1280
Christopher Faulet92d36382015-11-05 13:35:03 +01001281static int
1282parse_http_comp_flt(char **args, int *cur_arg, struct proxy *px,
Thierry Fournier3610c392016-04-13 18:27:51 +02001283 struct flt_conf *fconf, char **err, void *private)
Christopher Faulet92d36382015-11-05 13:35:03 +01001284{
Christopher Faulet443ea1a2016-02-04 13:40:26 +01001285 struct flt_conf *fc, *back;
Christopher Faulet92d36382015-11-05 13:35:03 +01001286
Christopher Faulet443ea1a2016-02-04 13:40:26 +01001287 list_for_each_entry_safe(fc, back, &px->filter_configs, list) {
1288 if (fc->id == http_comp_flt_id) {
Christopher Faulet92d36382015-11-05 13:35:03 +01001289 memprintf(err, "%s: Proxy supports only one compression filter\n", px->id);
1290 return -1;
1291 }
1292 }
1293
Christopher Faulet443ea1a2016-02-04 13:40:26 +01001294 fconf->id = http_comp_flt_id;
1295 fconf->conf = NULL;
1296 fconf->ops = &comp_ops;
Christopher Faulet92d36382015-11-05 13:35:03 +01001297 (*cur_arg)++;
1298
1299 return 0;
1300}
1301
1302
1303int
Christopher Fauletc9df7f72018-12-10 16:14:04 +01001304check_implicit_http_comp_flt(struct proxy *proxy)
Christopher Faulet92d36382015-11-05 13:35:03 +01001305{
Christopher Faulet443ea1a2016-02-04 13:40:26 +01001306 struct flt_conf *fconf;
Christopher Faulet92d36382015-11-05 13:35:03 +01001307 int err = 0;
1308
1309 if (proxy->comp == NULL)
1310 goto end;
Christopher Faulet443ea1a2016-02-04 13:40:26 +01001311 if (!LIST_ISEMPTY(&proxy->filter_configs)) {
1312 list_for_each_entry(fconf, &proxy->filter_configs, list) {
1313 if (fconf->id == http_comp_flt_id)
Christopher Faulet92d36382015-11-05 13:35:03 +01001314 goto end;
1315 }
Christopher Faulet767a84b2017-11-24 16:50:31 +01001316 ha_alert("config: %s '%s': require an explicit filter declaration to use HTTP compression\n",
1317 proxy_type_str(proxy), proxy->id);
Christopher Faulet92d36382015-11-05 13:35:03 +01001318 err++;
1319 goto end;
1320 }
1321
Christopher Faulet443ea1a2016-02-04 13:40:26 +01001322 fconf = calloc(1, sizeof(*fconf));
1323 if (!fconf) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001324 ha_alert("config: %s '%s': out of memory\n",
1325 proxy_type_str(proxy), proxy->id);
Christopher Faulet92d36382015-11-05 13:35:03 +01001326 err++;
1327 goto end;
1328 }
Christopher Faulet443ea1a2016-02-04 13:40:26 +01001329 fconf->id = http_comp_flt_id;
1330 fconf->conf = NULL;
1331 fconf->ops = &comp_ops;
1332 LIST_ADDQ(&proxy->filter_configs, &fconf->list);
Christopher Faulet92d36382015-11-05 13:35:03 +01001333
1334 end:
1335 return err;
1336}
1337
1338/*
1339 * boolean, returns true if compression is used (either gzip or deflate) in the
1340 * response.
1341 */
Christopher Faulet3d97c902015-12-09 14:59:38 +01001342static int
Christopher Faulet92d36382015-11-05 13:35:03 +01001343smp_fetch_res_comp(const struct arg *args, struct sample *smp, const char *kw,
1344 void *private)
Christopher Faulet3d97c902015-12-09 14:59:38 +01001345{
Willy Tarreaube508f12016-03-10 11:47:01 +01001346 struct http_txn *txn = smp->strm ? smp->strm->txn : NULL;
Christopher Faulet92d36382015-11-05 13:35:03 +01001347
Christopher Faulet3d97c902015-12-09 14:59:38 +01001348 smp->data.type = SMP_T_BOOL;
Christopher Faulet92d36382015-11-05 13:35:03 +01001349 smp->data.u.sint = (txn && (txn->rsp.flags & HTTP_MSGF_COMPRESSING));
Christopher Faulet3d97c902015-12-09 14:59:38 +01001350 return 1;
1351}
1352
Christopher Faulet92d36382015-11-05 13:35:03 +01001353/*
1354 * string, returns algo
1355 */
Christopher Faulet3d97c902015-12-09 14:59:38 +01001356static int
Christopher Faulet92d36382015-11-05 13:35:03 +01001357smp_fetch_res_comp_algo(const struct arg *args, struct sample *smp,
1358 const char *kw, void *private)
Christopher Faulet3d97c902015-12-09 14:59:38 +01001359{
Willy Tarreaube508f12016-03-10 11:47:01 +01001360 struct http_txn *txn = smp->strm ? smp->strm->txn : NULL;
Christopher Faulet92d36382015-11-05 13:35:03 +01001361 struct filter *filter;
1362 struct comp_state *st;
1363
Christopher Faulet03d85532017-09-15 10:14:43 +02001364 if (!txn || !(txn->rsp.flags & HTTP_MSGF_COMPRESSING))
Christopher Faulet3d97c902015-12-09 14:59:38 +01001365 return 0;
1366
Christopher Fauletfcf035c2015-12-03 11:48:03 +01001367 list_for_each_entry(filter, &strm_flt(smp->strm)->filters, list) {
Christopher Faulet443ea1a2016-02-04 13:40:26 +01001368 if (FLT_ID(filter) != http_comp_flt_id)
Christopher Faulet92d36382015-11-05 13:35:03 +01001369 continue;
1370
1371 if (!(st = filter->ctx))
1372 break;
1373
1374 smp->data.type = SMP_T_STR;
1375 smp->flags = SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001376 smp->data.u.str.area = st->comp_algo->cfg_name;
1377 smp->data.u.str.data = st->comp_algo->cfg_name_len;
Christopher Faulet92d36382015-11-05 13:35:03 +01001378 return 1;
1379 }
1380 return 0;
Christopher Faulet3d97c902015-12-09 14:59:38 +01001381}
1382
1383/* Declare the config parser for "compression" keyword */
1384static struct cfg_kw_list cfg_kws = {ILH, {
1385 { CFG_LISTEN, "compression", parse_compression_options },
1386 { 0, NULL, NULL },
1387 }
1388};
1389
Willy Tarreau0108d902018-11-25 19:14:37 +01001390INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
1391
Christopher Faulet92d36382015-11-05 13:35:03 +01001392/* Declare the filter parser for "compression" keyword */
1393static struct flt_kw_list filter_kws = { "COMP", { }, {
Thierry Fournier3610c392016-04-13 18:27:51 +02001394 { "compression", parse_http_comp_flt, NULL },
1395 { NULL, NULL, NULL },
Christopher Faulet92d36382015-11-05 13:35:03 +01001396 }
1397};
1398
Willy Tarreau0108d902018-11-25 19:14:37 +01001399INITCALL1(STG_REGISTER, flt_register_keywords, &filter_kws);
1400
Christopher Faulet3d97c902015-12-09 14:59:38 +01001401/* Note: must not be declared <const> as its list will be overwritten */
1402static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
Christopher Faulet92d36382015-11-05 13:35:03 +01001403 { "res.comp", smp_fetch_res_comp, 0, NULL, SMP_T_BOOL, SMP_USE_HRSHP },
1404 { "res.comp_algo", smp_fetch_res_comp_algo, 0, NULL, SMP_T_STR, SMP_USE_HRSHP },
1405 { /* END */ },
1406 }
1407};
Christopher Faulet3d97c902015-12-09 14:59:38 +01001408
Willy Tarreau0108d902018-11-25 19:14:37 +01001409INITCALL1(STG_REGISTER, sample_register_fetches, &sample_fetch_keywords);