blob: cfe864e35a73eac75ca9873a87b7dd467acc116d [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;
Willy Tarreau8ceae722018-11-26 11:58:30 +010054
Christopher Faulet92d36382015-11-05 13:35:03 +010055static int select_compression_request_header(struct comp_state *st,
56 struct stream *s,
57 struct http_msg *msg);
58static int select_compression_response_header(struct comp_state *st,
59 struct stream *s,
60 struct http_msg *msg);
Christopher Faulet27d93c32018-12-15 22:32:02 +010061static int set_compression_response_header(struct comp_state *st,
62 struct stream *s,
63 struct http_msg *msg);
Christopher Faulet92d36382015-11-05 13:35:03 +010064
Christopher Faulete6902cd2018-11-30 22:29:48 +010065static int htx_compression_buffer_init(struct htx *htx, struct buffer *out);
66static int htx_compression_buffer_add_data(struct comp_state *st, const char *data, size_t len,
67 struct buffer *out);
68static int htx_compression_buffer_end(struct comp_state *st, struct buffer *out, int end);
69
Christopher Fauletb61481c2018-12-17 13:17:53 +010070static int http_compression_buffer_init(struct channel *inc, struct buffer *out);
Christopher Faulet92d36382015-11-05 13:35:03 +010071static int http_compression_buffer_add_data(struct comp_state *st,
72 struct buffer *in,
Willy Tarreaud54a8ce2018-06-29 18:42:02 +020073 int in_out,
Christopher Faulet92d36382015-11-05 13:35:03 +010074 struct buffer *out, int sz);
75static int http_compression_buffer_end(struct comp_state *st, struct stream *s,
Willy Tarreauc9fa0482018-07-10 17:43:27 +020076 struct channel *chn, struct buffer *out,
Christopher Fauletb61481c2018-12-17 13:17:53 +010077 int end);
Christopher Faulet92d36382015-11-05 13:35:03 +010078
79/***********************************************************************/
80static int
Christopher Faulete6902cd2018-11-30 22:29:48 +010081comp_flt_init(struct proxy *px, struct flt_conf *fconf)
82{
Christopher Faulet6e540952018-12-03 22:43:41 +010083 fconf->flags |= FLT_CFG_FL_HTX;
Christopher Faulete6902cd2018-11-30 22:29:48 +010084 return 0;
85}
86
87static int
Christopher Faulet8ca3b4b2017-07-25 11:07:15 +020088comp_flt_init_per_thread(struct proxy *px, struct flt_conf *fconf)
Christopher Faulet92d36382015-11-05 13:35:03 +010089{
Willy Tarreauc9fa0482018-07-10 17:43:27 +020090 if (!tmpbuf.size && b_alloc(&tmpbuf) == NULL)
Christopher Fauletb77c5c22015-12-07 16:48:42 +010091 return -1;
Willy Tarreauc9fa0482018-07-10 17:43:27 +020092 if (!zbuf.size && b_alloc(&zbuf) == NULL)
Christopher Fauletb77c5c22015-12-07 16:48:42 +010093 return -1;
Christopher Faulet92d36382015-11-05 13:35:03 +010094 return 0;
95}
96
97static void
Christopher Faulet8ca3b4b2017-07-25 11:07:15 +020098comp_flt_deinit_per_thread(struct proxy *px, struct flt_conf *fconf)
Christopher Faulet92d36382015-11-05 13:35:03 +010099{
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200100 if (tmpbuf.size)
Christopher Faulet92d36382015-11-05 13:35:03 +0100101 b_free(&tmpbuf);
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200102 if (zbuf.size)
Christopher Fauletb77c5c22015-12-07 16:48:42 +0100103 b_free(&zbuf);
Christopher Faulet92d36382015-11-05 13:35:03 +0100104}
105
106static int
107comp_start_analyze(struct stream *s, struct filter *filter, struct channel *chn)
108{
Christopher Faulet8ca3b4b2017-07-25 11:07:15 +0200109
Christopher Faulet92d36382015-11-05 13:35:03 +0100110 if (filter->ctx == NULL) {
111 struct comp_state *st;
112
Willy Tarreaubafbe012017-11-24 17:34:44 +0100113 st = pool_alloc_dirty(pool_head_comp_state);
Christopher Fauleta03d4ad2017-06-26 16:53:33 +0200114 if (st == NULL)
Christopher Faulet92d36382015-11-05 13:35:03 +0100115 return -1;
116
Christopher Faulet2fb28802015-12-01 10:40:57 +0100117 st->comp_algo = NULL;
118 st->comp_ctx = NULL;
Christopher Fauletb77c5c22015-12-07 16:48:42 +0100119 st->hdrs_len = 0;
120 st->tlrs_len = 0;
Christopher Faulet2fb28802015-12-01 10:40:57 +0100121 st->consumed = 0;
122 st->initialized = 0;
Christopher Fauletb77c5c22015-12-07 16:48:42 +0100123 st->finished = 0;
Christopher Faulet2fb28802015-12-01 10:40:57 +0100124 filter->ctx = st;
Christopher Faulet3dc860d2017-09-15 11:39:36 +0200125
126 /* Register post-analyzer on AN_RES_WAIT_HTTP because we need to
127 * analyze response headers before http-response rules execution
128 * to be sure we can use res.comp and res.comp_algo sample
129 * fetches */
130 filter->post_analyzers |= AN_RES_WAIT_HTTP;
Christopher Faulet92d36382015-11-05 13:35:03 +0100131 }
132 return 1;
133}
134
135static int
Christopher Faulet92d36382015-11-05 13:35:03 +0100136comp_end_analyze(struct stream *s, struct filter *filter, struct channel *chn)
137{
138 struct comp_state *st = filter->ctx;
Christopher Faulet92d36382015-11-05 13:35:03 +0100139
Christopher Fauletd60b3cf2017-06-26 11:47:13 +0200140 if (!st)
Christopher Faulet92d36382015-11-05 13:35:03 +0100141 goto end;
142
Christopher Faulet92d36382015-11-05 13:35:03 +0100143 /* release any possible compression context */
Christopher Fauletd60b3cf2017-06-26 11:47:13 +0200144 if (st->comp_algo)
145 st->comp_algo->end(&st->comp_ctx);
Willy Tarreaubafbe012017-11-24 17:34:44 +0100146 pool_free(pool_head_comp_state, st);
Christopher Faulet92d36382015-11-05 13:35:03 +0100147 filter->ctx = NULL;
148 end:
149 return 1;
150}
151
152static int
Christopher Faulet1339d742016-05-11 16:48:33 +0200153comp_http_headers(struct stream *s, struct filter *filter, struct http_msg *msg)
154{
155 struct comp_state *st = filter->ctx;
156
157 if (!strm_fe(s)->comp && !s->be->comp)
158 goto end;
159
160 if (!(msg->chn->flags & CF_ISRESP))
161 select_compression_request_header(st, s, msg);
162 else {
Christopher Faulet3dc860d2017-09-15 11:39:36 +0200163 /* Response headers have already been checked in
164 * comp_http_post_analyze callback. */
Christopher Faulet1339d742016-05-11 16:48:33 +0200165 if (st->comp_algo) {
Christopher Faulet27d93c32018-12-15 22:32:02 +0100166 if (!set_compression_response_header(st, s, msg))
167 goto end;
Christopher Faulet1339d742016-05-11 16:48:33 +0200168 register_data_filter(s, msg->chn, filter);
Christopher Faulete6902cd2018-11-30 22:29:48 +0100169 if (!IS_HTX_STRM(s))
170 st->hdrs_len = s->txn->rsp.sov;
Christopher Faulet1339d742016-05-11 16:48:33 +0200171 }
172 }
173
174 end:
175 return 1;
176}
177
178static int
Christopher Faulet3dc860d2017-09-15 11:39:36 +0200179comp_http_post_analyze(struct stream *s, struct filter *filter,
180 struct channel *chn, unsigned an_bit)
181{
182 struct http_txn *txn = s->txn;
183 struct http_msg *msg = &txn->rsp;
184 struct comp_state *st = filter->ctx;
185
186 if (an_bit != AN_RES_WAIT_HTTP)
187 goto end;
188
189 if (!strm_fe(s)->comp && !s->be->comp)
190 goto end;
191
192 select_compression_response_header(st, s, msg);
193
194 end:
195 return 1;
196}
197
198static int
Christopher Faulete6902cd2018-11-30 22:29:48 +0100199comp_http_payload(struct stream *s, struct filter *filter, struct http_msg *msg,
200 unsigned int offset, unsigned int len)
201{
202 struct comp_state *st = filter->ctx;
Christopher Faulet27ba2dc2018-12-05 11:53:24 +0100203 struct htx *htx = htxbuf(&msg->chn->buf);
Christopher Faulete6902cd2018-11-30 22:29:48 +0100204 struct htx_blk *blk;
205 struct htx_ret htx_ret;
206 int ret, consumed = 0, to_forward = 0;
207
208 htx_ret = htx_find_blk(htx, offset);
209 blk = htx_ret.blk;
210 offset = htx_ret.ret;
211
212 while (blk && len) {
213 enum htx_blk_type type = htx_get_blk_type(blk);
214 uint32_t sz = htx_get_blksz(blk);
215 struct ist v;
216
217 switch (type) {
218 case HTX_BLK_UNUSED:
219 break;
220
221 case HTX_BLK_DATA:
222 v = htx_get_blk_value(htx, blk);
223 v.ptr += offset;
224 v.len -= offset;
225 if (v.len > len)
226 v.len = len;
227 if (htx_compression_buffer_init(htx, &trash) < 0) {
228 msg->chn->flags |= CF_WAKE_WRITE;
229 goto end;
230 }
231 ret = htx_compression_buffer_add_data(st, v.ptr, v.len, &trash);
232 if (ret < 0)
233 goto error;
234 if (htx_compression_buffer_end(st, &trash, 0) < 0)
235 goto error;
236 len -= ret;
237 consumed += ret;
238 to_forward += b_data(&trash);
239 if (ret == sz && !b_data(&trash)) {
240 offset = 0;
241 blk = htx_remove_blk(htx, blk);
242 continue;
243 }
244 v.len = ret;
245 blk = htx_replace_blk_value(htx, blk, v, ist2(b_head(&trash), b_data(&trash)));
246 break;
247
248 case HTX_BLK_EOD:
249 case HTX_BLK_TLR:
250 case HTX_BLK_EOM:
251 if (msg->flags & HTTP_MSGF_COMPRESSING) {
252 if (htx_compression_buffer_init(htx, &trash) < 0) {
253 msg->chn->flags |= CF_WAKE_WRITE;
254 goto end;
255 }
256 if (htx_compression_buffer_end(st, &trash, 1) < 0)
257 goto error;
Christopher Fauletd238ae32018-12-21 15:10:25 +0100258 if (b_data(&trash)) {
259 blk = htx_add_data_before(htx, blk, ist2(b_head(&trash), b_data(&trash)));
260 if (!blk)
261 goto error;
262 to_forward += b_data(&trash);
263 }
Christopher Faulete6902cd2018-11-30 22:29:48 +0100264 msg->flags &= ~HTTP_MSGF_COMPRESSING;
265 /* We let the mux add last empty chunk and empty trailers */
266 }
267 /* fall through */
268
269 default:
270 sz -= offset;
271 if (sz > len)
272 sz = len;
273 consumed += sz;
274 to_forward += sz;
275 len -= sz;
276 break;
277 }
278
279 offset = 0;
280 blk = htx_get_next_blk(htx, blk);
281 }
282
283 end:
284 if (to_forward != consumed)
285 flt_update_offsets(filter, msg->chn, to_forward - consumed);
286
287 if (st->comp_ctx && st->comp_ctx->cur_lvl > 0) {
288 update_freq_ctr(&global.comp_bps_out, to_forward);
289 HA_ATOMIC_ADD(&strm_fe(s)->fe_counters.comp_out, to_forward);
290 HA_ATOMIC_ADD(&s->be->be_counters.comp_out, to_forward);
291 }
292 return to_forward;
293
294 error:
295 return -1;
296}
297
298static int
Christopher Faulet2fb28802015-12-01 10:40:57 +0100299comp_http_data(struct stream *s, struct filter *filter, struct http_msg *msg)
Christopher Faulet92d36382015-11-05 13:35:03 +0100300{
301 struct comp_state *st = filter->ctx;
Willy Tarreaubcbd3932018-06-06 07:13:22 +0200302 struct channel *chn = msg->chn;
Christopher Faulet3e7bc672015-12-07 13:39:08 +0100303 unsigned int *nxt = &flt_rsp_nxt(filter);
Christopher Faulet2fb28802015-12-01 10:40:57 +0100304 unsigned int len;
Christopher Faulet92d36382015-11-05 13:35:03 +0100305 int ret;
306
Olivier Houchard0b662842018-06-29 18:16:31 +0200307 len = MIN(msg->chunk_len + msg->next, ci_data(chn)) - *nxt;
Christopher Faulet2fb28802015-12-01 10:40:57 +0100308 if (!len)
309 return len;
310
311 if (!st->initialized) {
Christopher Fauletb77c5c22015-12-07 16:48:42 +0100312 unsigned int fwd = flt_rsp_fwd(filter) + st->hdrs_len;
Christopher Faulet3e7bc672015-12-07 13:39:08 +0100313
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200314 b_reset(&tmpbuf);
Willy Tarreaubcbd3932018-06-06 07:13:22 +0200315 c_adv(chn, fwd);
Christopher Fauletb61481c2018-12-17 13:17:53 +0100316 ret = http_compression_buffer_init(chn, &zbuf);
Willy Tarreaubcbd3932018-06-06 07:13:22 +0200317 c_rew(chn, fwd);
Christopher Faulet2fb28802015-12-01 10:40:57 +0100318 if (ret < 0) {
319 msg->chn->flags |= CF_WAKE_WRITE;
320 return 0;
321 }
322 }
Christopher Fauletb77c5c22015-12-07 16:48:42 +0100323
324 if (msg->flags & HTTP_MSGF_TE_CHNK) {
Christopher Faulet06ecf3a2016-09-22 15:31:43 +0200325 int block;
Christopher Fauletb77c5c22015-12-07 16:48:42 +0100326
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200327 len = MIN(b_room(&tmpbuf), len);
Christopher Faulet06ecf3a2016-09-22 15:31:43 +0200328
Willy Tarreaubcbd3932018-06-06 07:13:22 +0200329 c_adv(chn, *nxt);
Willy Tarreau7194d3c2018-06-06 16:55:45 +0200330 block = ci_contig_data(chn);
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200331 memcpy(b_tail(&tmpbuf), ci_head(chn), block);
Christopher Faulet06ecf3a2016-09-22 15:31:43 +0200332 if (len > block)
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200333 memcpy(b_tail(&tmpbuf)+block, b_orig(&chn->buf), len-block);
Willy Tarreaubcbd3932018-06-06 07:13:22 +0200334 c_rew(chn, *nxt);
Christopher Faulet06ecf3a2016-09-22 15:31:43 +0200335
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200336 b_add(&tmpbuf, len);
Christopher Fauletb77c5c22015-12-07 16:48:42 +0100337 ret = len;
338 }
339 else {
Willy Tarreaubcbd3932018-06-06 07:13:22 +0200340 c_adv(chn, *nxt);
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200341 ret = http_compression_buffer_add_data(st, &chn->buf, co_data(chn), &zbuf, len);
Willy Tarreaubcbd3932018-06-06 07:13:22 +0200342 c_rew(chn, *nxt);
Christopher Fauletb77c5c22015-12-07 16:48:42 +0100343 if (ret < 0)
344 return ret;
345 }
Christopher Faulet92d36382015-11-05 13:35:03 +0100346
Christopher Faulet2fb28802015-12-01 10:40:57 +0100347 st->initialized = 1;
348 msg->next += ret;
349 msg->chunk_len -= ret;
Christopher Faulet3e7bc672015-12-07 13:39:08 +0100350 *nxt = msg->next;
Christopher Faulet2fb28802015-12-01 10:40:57 +0100351 return 0;
Christopher Faulet92d36382015-11-05 13:35:03 +0100352}
353
354static int
Christopher Faulet2fb28802015-12-01 10:40:57 +0100355comp_http_chunk_trailers(struct stream *s, struct filter *filter,
356 struct http_msg *msg)
Christopher Faulet92d36382015-11-05 13:35:03 +0100357{
358 struct comp_state *st = filter->ctx;
Christopher Faulet92d36382015-11-05 13:35:03 +0100359
Christopher Fauletb77c5c22015-12-07 16:48:42 +0100360 if (!st->initialized) {
361 if (!st->finished) {
Willy Tarreaubcbd3932018-06-06 07:13:22 +0200362 struct channel *chn = msg->chn;
Christopher Fauletb77c5c22015-12-07 16:48:42 +0100363 unsigned int fwd = flt_rsp_fwd(filter) + st->hdrs_len;
Christopher Faulet2fb28802015-12-01 10:40:57 +0100364
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200365 b_reset(&tmpbuf);
Willy Tarreaubcbd3932018-06-06 07:13:22 +0200366 c_adv(chn, fwd);
Christopher Fauletb61481c2018-12-17 13:17:53 +0100367 http_compression_buffer_init(chn, &zbuf);
Willy Tarreaubcbd3932018-06-06 07:13:22 +0200368 c_rew(chn, fwd);
Christopher Fauletb77c5c22015-12-07 16:48:42 +0100369 st->initialized = 1;
370 }
371 }
372 st->tlrs_len = msg->sol;
Christopher Faulet2fb28802015-12-01 10:40:57 +0100373 return 1;
Christopher Faulet92d36382015-11-05 13:35:03 +0100374}
375
Christopher Faulet2fb28802015-12-01 10:40:57 +0100376
Christopher Faulet92d36382015-11-05 13:35:03 +0100377static int
378comp_http_forward_data(struct stream *s, struct filter *filter,
379 struct http_msg *msg, unsigned int len)
380{
381 struct comp_state *st = filter->ctx;
Christopher Faulet2fb28802015-12-01 10:40:57 +0100382 int ret;
Christopher Faulet92d36382015-11-05 13:35:03 +0100383
Christopher Faulet2fb28802015-12-01 10:40:57 +0100384 /* To work, previous filters MUST forward all data */
Christopher Fauletb77c5c22015-12-07 16:48:42 +0100385 if (flt_rsp_fwd(filter) + len != flt_rsp_nxt(filter)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100386 ha_warning("HTTP compression failed: unexpected behavior of previous filters\n");
Christopher Faulet2fb28802015-12-01 10:40:57 +0100387 return -1;
Christopher Faulet92d36382015-11-05 13:35:03 +0100388 }
389
Christopher Faulet2fb28802015-12-01 10:40:57 +0100390 if (!st->initialized) {
Christopher Fauletb77c5c22015-12-07 16:48:42 +0100391 if (!len) {
Joseph Herlant942eea32018-11-15 13:57:22 -0800392 /* Nothing to forward */
Christopher Fauletb77c5c22015-12-07 16:48:42 +0100393 ret = len;
394 }
395 else if (st->hdrs_len > len) {
396 /* Forward part of headers */
397 ret = len;
398 st->hdrs_len -= len;
399 }
400 else if (st->hdrs_len > 0) {
401 /* Forward remaining headers */
402 ret = st->hdrs_len;
403 st->hdrs_len = 0;
404 }
405 else if (msg->msg_state < HTTP_MSG_TRAILERS) {
406 /* Do not forward anything for now. This only happens
407 * with chunk-encoded responses. Waiting data are part
408 * of the chunk envelope (the chunk size or the chunk
409 * CRLF). These data will be skipped during the
410 * compression. */
411 ret = 0;
412 }
413 else {
414 /* Forward trailers data */
415 ret = len;
416 }
Christopher Faulet2fb28802015-12-01 10:40:57 +0100417 return ret;
Christopher Faulet92d36382015-11-05 13:35:03 +0100418 }
419
Christopher Fauletb77c5c22015-12-07 16:48:42 +0100420 if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200421 ret = http_compression_buffer_add_data(st, &tmpbuf, 0,
422 &zbuf, b_data(&tmpbuf));
423 if (ret != b_data(&tmpbuf)) {
Willy Tarreau506a29a2018-07-18 10:07:58 +0200424 ha_warning("HTTP compression failed: Must consume %u bytes but only %d bytes consumed\n",
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200425 (unsigned int)b_data(&tmpbuf), ret);
Christopher Fauletb77c5c22015-12-07 16:48:42 +0100426 return -1;
427 }
428 }
429
430 st->consumed = len - st->hdrs_len - st->tlrs_len;
Willy Tarreaubcbd3932018-06-06 07:13:22 +0200431 c_adv(msg->chn, flt_rsp_fwd(filter) + st->hdrs_len);
Christopher Fauletb61481c2018-12-17 13:17:53 +0100432 ret = http_compression_buffer_end(st, s, msg->chn, &zbuf, msg->msg_state >= HTTP_MSG_TRAILERS);
Willy Tarreaubcbd3932018-06-06 07:13:22 +0200433 c_rew(msg->chn, flt_rsp_fwd(filter) + st->hdrs_len);
Christopher Faulet2fb28802015-12-01 10:40:57 +0100434 if (ret < 0)
435 return ret;
Christopher Faulet92d36382015-11-05 13:35:03 +0100436
Christopher Fauletb77c5c22015-12-07 16:48:42 +0100437 flt_change_forward_size(filter, msg->chn, ret - st->consumed);
438 msg->next += (ret - st->consumed);
439 ret += st->hdrs_len + st->tlrs_len;
440
Christopher Faulet2fb28802015-12-01 10:40:57 +0100441 st->initialized = 0;
Christopher Fauletb77c5c22015-12-07 16:48:42 +0100442 st->finished = (msg->msg_state >= HTTP_MSG_TRAILERS);
443 st->hdrs_len = 0;
444 st->tlrs_len = 0;
Christopher Faulet92d36382015-11-05 13:35:03 +0100445 return ret;
446}
Christopher Faulet3d97c902015-12-09 14:59:38 +0100447
Christopher Fauletd60b3cf2017-06-26 11:47:13 +0200448static int
449comp_http_end(struct stream *s, struct filter *filter,
450 struct http_msg *msg)
451{
452 struct comp_state *st = filter->ctx;
453
454 if (!(msg->chn->flags & CF_ISRESP) || !st || !st->comp_algo)
455 goto end;
456
457 if (strm_fe(s)->mode == PR_MODE_HTTP)
Christopher Fauletff8abcd2017-06-02 15:33:24 +0200458 HA_ATOMIC_ADD(&strm_fe(s)->fe_counters.p.http.comp_rsp, 1);
Christopher Fauletd60b3cf2017-06-26 11:47:13 +0200459 if ((s->flags & SF_BE_ASSIGNED) && (s->be->mode == PR_MODE_HTTP))
Christopher Fauletff8abcd2017-06-02 15:33:24 +0200460 HA_ATOMIC_ADD(&s->be->be_counters.p.http.comp_rsp, 1);
Christopher Fauletd60b3cf2017-06-26 11:47:13 +0200461 end:
462 return 1;
463}
Christopher Faulet3d97c902015-12-09 14:59:38 +0100464/***********************************************************************/
Christopher Faulet27d93c32018-12-15 22:32:02 +0100465static int
466http_set_comp_reshdr(struct comp_state *st, struct stream *s, struct http_msg *msg)
467{
468 struct http_txn *txn = s->txn;
Tim Duesterhusb229f012019-01-29 16:38:56 +0100469 struct hdr_ctx ctx;
Christopher Faulet27d93c32018-12-15 22:32:02 +0100470
471 /*
472 * Add Content-Encoding header when it's not identity encoding.
473 * RFC 2616 : Identity encoding: This content-coding is used only in the
474 * Accept-Encoding header, and SHOULD NOT be used in the Content-Encoding
475 * header.
476 */
477 if (st->comp_algo->cfg_name_len != 8 || memcmp(st->comp_algo->cfg_name, "identity", 8) != 0) {
478 trash.data = 18;
479 memcpy(trash.area, "Content-Encoding: ", trash.data);
480 memcpy(trash.area + trash.data, st->comp_algo->ua_name,
481 st->comp_algo->ua_name_len);
482 trash.data += st->comp_algo->ua_name_len;
483 trash.area[trash.data] = '\0';
484 if (http_header_add_tail2(msg, &txn->hdr_idx, trash.area, trash.data) < 0)
485 goto error;
486 }
487
488 /* remove Content-Length header */
489 if (msg->flags & HTTP_MSGF_CNT_LEN) {
Christopher Faulet27d93c32018-12-15 22:32:02 +0100490 ctx.idx = 0;
491 while (http_find_header2("Content-Length", 14, ci_head(&s->res), &txn->hdr_idx, &ctx))
492 http_remove_header2(msg, &txn->hdr_idx, &ctx);
493 }
494
495 /* add Transfer-Encoding header */
496 if (!(msg->flags & HTTP_MSGF_TE_CHNK)) {
497 if (http_header_add_tail2(msg, &txn->hdr_idx, "Transfer-Encoding: chunked", 26) < 0)
498 goto error;
499 }
500
Tim Duesterhusb229f012019-01-29 16:38:56 +0100501 ctx.idx = 0;
502 if (http_find_full_header2("ETag", 4, ci_head(&s->res), &txn->hdr_idx, &ctx)) {
503 if (ctx.line[ctx.val] == '"') {
504 /* This a strong ETag. Convert it to a weak one. */
505 trash.data = 8;
506 if (trash.data + ctx.vlen > trash.size)
507 goto error;
508 memcpy(trash.area, "ETag: W/", trash.data);
509 memcpy(trash.area + trash.data, ctx.line + ctx.val, ctx.vlen);
510 trash.data += ctx.vlen;
511 trash.area[trash.data] = '\0';
512 http_remove_header2(msg, &txn->hdr_idx, &ctx);
513 if (http_header_add_tail2(msg, &txn->hdr_idx, trash.area, trash.data) < 0)
514 goto error;
515 }
516 }
Christopher Faulet27d93c32018-12-15 22:32:02 +0100517
518 return 1;
519
520 error:
521 st->comp_algo->end(&st->comp_ctx);
522 st->comp_algo = NULL;
523 return 0;
524}
525
526static int
527htx_set_comp_reshdr(struct comp_state *st, struct stream *s, struct http_msg *msg)
528{
529 struct htx *htx = htxbuf(&msg->chn->buf);
Tim Duesterhusb229f012019-01-29 16:38:56 +0100530 struct http_hdr_ctx ctx;
Christopher Faulet27d93c32018-12-15 22:32:02 +0100531
532 /*
533 * Add Content-Encoding header when it's not identity encoding.
534 * RFC 2616 : Identity encoding: This content-coding is used only in the
535 * Accept-Encoding header, and SHOULD NOT be used in the Content-Encoding
536 * header.
537 */
538 if (st->comp_algo->cfg_name_len != 8 || memcmp(st->comp_algo->cfg_name, "identity", 8) != 0) {
539 struct ist v = ist2(st->comp_algo->ua_name, st->comp_algo->ua_name_len);
540
541 if (!http_add_header(htx, ist("Content-Encoding"), v))
542 goto error;
543 }
544
545 /* remove Content-Length header */
546 if (msg->flags & HTTP_MSGF_CNT_LEN) {
Christopher Faulet27d93c32018-12-15 22:32:02 +0100547 ctx.blk = NULL;
548 while (http_find_header(htx, ist("Content-Length"), &ctx, 1))
549 http_remove_header(htx, &ctx);
550 }
551
552 /* add "Transfer-Encoding: chunked" header */
553 if (!(msg->flags & HTTP_MSGF_TE_CHNK)) {
554 if (!http_add_header(htx, ist("Transfer-Encoding"), ist("chunked")))
555 goto error;
556 }
557
Tim Duesterhusb229f012019-01-29 16:38:56 +0100558 /* convert "ETag" header to a weak ETag */
559 ctx.blk = NULL;
560 if (http_find_header(htx, ist("ETag"), &ctx, 1)) {
561 if (ctx.value.ptr[0] == '"') {
562 /* This a strong ETag. Convert it to a weak one. */
563 struct ist v = ist2(trash.area, 0);
564 if (istcat(&v, ist("W/"), trash.size) == -1 || istcat(&v, ctx.value, trash.size) == -1)
565 goto error;
566
567 if (!http_replace_header_value(htx, &ctx, v))
568 goto error;
569 }
570 }
571
Christopher Faulet27d93c32018-12-15 22:32:02 +0100572 return 1;
573
574 error:
575 st->comp_algo->end(&st->comp_ctx);
576 st->comp_algo = NULL;
577 return 0;
578}
579
580static int
581set_compression_response_header(struct comp_state *st, struct stream *s, struct http_msg *msg)
582{
583 if (IS_HTX_STRM(s))
584 return htx_set_comp_reshdr(st, s, msg);
585 else
586 return http_set_comp_reshdr(st, s, msg);
587}
588
Christopher Faulet3d97c902015-12-09 14:59:38 +0100589/*
590 * Selects a compression algorithm depending on the client request.
591 */
Christopher Faulete6902cd2018-11-30 22:29:48 +0100592static int
593http_select_comp_reqhdr(struct comp_state *st, struct stream *s, struct http_msg *msg)
Christopher Faulet3d97c902015-12-09 14:59:38 +0100594{
595 struct http_txn *txn = s->txn;
Olivier Houchard0b662842018-06-29 18:16:31 +0200596 struct channel *req = msg->chn;
Christopher Faulet3d97c902015-12-09 14:59:38 +0100597 struct hdr_ctx ctx;
598 struct comp_algo *comp_algo = NULL;
599 struct comp_algo *comp_algo_back = NULL;
600
601 /* Disable compression for older user agents announcing themselves as "Mozilla/4"
602 * unless they are known good (MSIE 6 with XP SP2, or MSIE 7 and later).
603 * See http://zoompf.com/2012/02/lose-the-wait-http-compression for more details.
604 */
605 ctx.idx = 0;
Olivier Houchard0b662842018-06-29 18:16:31 +0200606 if (http_find_header2("User-Agent", 10, ci_head(req), &txn->hdr_idx, &ctx) &&
Christopher Faulet3d97c902015-12-09 14:59:38 +0100607 ctx.vlen >= 9 &&
608 memcmp(ctx.line + ctx.val, "Mozilla/4", 9) == 0 &&
609 (ctx.vlen < 31 ||
610 memcmp(ctx.line + ctx.val + 25, "MSIE ", 5) != 0 ||
611 ctx.line[ctx.val + 30] < '6' ||
612 (ctx.line[ctx.val + 30] == '6' &&
613 (ctx.vlen < 54 || memcmp(ctx.line + 51, "SV1", 3) != 0)))) {
Christopher Faulet92d36382015-11-05 13:35:03 +0100614 st->comp_algo = NULL;
Christopher Faulet3d97c902015-12-09 14:59:38 +0100615 return 0;
616 }
617
618 /* search for the algo in the backend in priority or the frontend */
Christopher Faulet92d36382015-11-05 13:35:03 +0100619 if ((s->be->comp && (comp_algo_back = s->be->comp->algos)) ||
620 (strm_fe(s)->comp && (comp_algo_back = strm_fe(s)->comp->algos))) {
Christopher Faulet3d97c902015-12-09 14:59:38 +0100621 int best_q = 0;
622
623 ctx.idx = 0;
Olivier Houchard0b662842018-06-29 18:16:31 +0200624 while (http_find_header2("Accept-Encoding", 15, ci_head(req), &txn->hdr_idx, &ctx)) {
Christopher Faulet3d97c902015-12-09 14:59:38 +0100625 const char *qval;
626 int q;
627 int toklen;
628
629 /* try to isolate the token from the optional q-value */
630 toklen = 0;
Willy Tarreau2235b262016-11-05 15:50:20 +0100631 while (toklen < ctx.vlen && HTTP_IS_TOKEN(*(ctx.line + ctx.val + toklen)))
Christopher Faulet3d97c902015-12-09 14:59:38 +0100632 toklen++;
633
634 qval = ctx.line + ctx.val + toklen;
635 while (1) {
Willy Tarreau2235b262016-11-05 15:50:20 +0100636 while (qval < ctx.line + ctx.val + ctx.vlen && HTTP_IS_LWS(*qval))
Christopher Faulet3d97c902015-12-09 14:59:38 +0100637 qval++;
638
639 if (qval >= ctx.line + ctx.val + ctx.vlen || *qval != ';') {
640 qval = NULL;
641 break;
642 }
643 qval++;
644
Willy Tarreau2235b262016-11-05 15:50:20 +0100645 while (qval < ctx.line + ctx.val + ctx.vlen && HTTP_IS_LWS(*qval))
Christopher Faulet3d97c902015-12-09 14:59:38 +0100646 qval++;
647
648 if (qval >= ctx.line + ctx.val + ctx.vlen) {
649 qval = NULL;
650 break;
651 }
652 if (strncmp(qval, "q=", MIN(ctx.line + ctx.val + ctx.vlen - qval, 2)) == 0)
653 break;
654
655 while (qval < ctx.line + ctx.val + ctx.vlen && *qval != ';')
656 qval++;
657 }
658
659 /* here we have qval pointing to the first "q=" attribute or NULL if not found */
Willy Tarreauab813a42018-09-10 18:41:28 +0200660 q = qval ? http_parse_qvalue(qval + 2, NULL) : 1000;
Christopher Faulet3d97c902015-12-09 14:59:38 +0100661
662 if (q <= best_q)
663 continue;
664
665 for (comp_algo = comp_algo_back; comp_algo; comp_algo = comp_algo->next) {
666 if (*(ctx.line + ctx.val) == '*' ||
667 word_match(ctx.line + ctx.val, toklen, comp_algo->ua_name, comp_algo->ua_name_len)) {
Christopher Faulet92d36382015-11-05 13:35:03 +0100668 st->comp_algo = comp_algo;
Christopher Faulet3d97c902015-12-09 14:59:38 +0100669 best_q = q;
670 break;
671 }
672 }
673 }
674 }
675
676 /* remove all occurrences of the header when "compression offload" is set */
Christopher Faulet92d36382015-11-05 13:35:03 +0100677 if (st->comp_algo) {
678 if ((s->be->comp && s->be->comp->offload) ||
679 (strm_fe(s)->comp && strm_fe(s)->comp->offload)) {
Christopher Faulet3d97c902015-12-09 14:59:38 +0100680 http_remove_header2(msg, &txn->hdr_idx, &ctx);
681 ctx.idx = 0;
Olivier Houchard0b662842018-06-29 18:16:31 +0200682 while (http_find_header2("Accept-Encoding", 15, ci_head(req), &txn->hdr_idx, &ctx)) {
Christopher Faulet3d97c902015-12-09 14:59:38 +0100683 http_remove_header2(msg, &txn->hdr_idx, &ctx);
684 }
685 }
Christopher Faulete6902cd2018-11-30 22:29:48 +0100686 return 1;
687 }
688
689 /* identity is implicit does not require headers */
690 if ((s->be->comp && (comp_algo_back = s->be->comp->algos)) ||
691 (strm_fe(s)->comp && (comp_algo_back = strm_fe(s)->comp->algos))) {
692 for (comp_algo = comp_algo_back; comp_algo; comp_algo = comp_algo->next) {
693 if (comp_algo->cfg_name_len == 8 && memcmp(comp_algo->cfg_name, "identity", 8) == 0) {
694 st->comp_algo = comp_algo;
695 return 1;
696 }
697 }
698 }
699
700 st->comp_algo = NULL;
701 return 0;
702}
703
704static int
705htx_select_comp_reqhdr(struct comp_state *st, struct stream *s, struct http_msg *msg)
706{
Christopher Faulet27ba2dc2018-12-05 11:53:24 +0100707 struct htx *htx = htxbuf(&msg->chn->buf);
Christopher Faulete6902cd2018-11-30 22:29:48 +0100708 struct http_hdr_ctx ctx;
709 struct comp_algo *comp_algo = NULL;
710 struct comp_algo *comp_algo_back = NULL;
711
712 /* Disable compression for older user agents announcing themselves as "Mozilla/4"
713 * unless they are known good (MSIE 6 with XP SP2, or MSIE 7 and later).
714 * See http://zoompf.com/2012/02/lose-the-wait-http-compression for more details.
715 */
716 ctx.blk = NULL;
717 if (http_find_header(htx, ist("User-Agent"), &ctx, 1) &&
718 ctx.value.len >= 9 &&
719 memcmp(ctx.value.ptr, "Mozilla/4", 9) == 0 &&
720 (ctx.value.len < 31 ||
721 memcmp(ctx.value.ptr + 25, "MSIE ", 5) != 0 ||
722 *(ctx.value.ptr + 30) < '6' ||
723 (*(ctx.value.ptr + 30) == '6' &&
724 (ctx.value.len < 54 || memcmp(ctx.value.ptr + 51, "SV1", 3) != 0)))) {
725 st->comp_algo = NULL;
726 return 0;
727 }
728
729 /* search for the algo in the backend in priority or the frontend */
730 if ((s->be->comp && (comp_algo_back = s->be->comp->algos)) ||
731 (strm_fe(s)->comp && (comp_algo_back = strm_fe(s)->comp->algos))) {
732 int best_q = 0;
733
734 ctx.blk = NULL;
735 while (http_find_header(htx, ist("Accept-Encoding"), &ctx, 0)) {
736 const char *qval;
737 int q;
738 int toklen;
739
740 /* try to isolate the token from the optional q-value */
741 toklen = 0;
742 while (toklen < ctx.value.len && HTTP_IS_TOKEN(*(ctx.value.ptr + toklen)))
743 toklen++;
744
745 qval = ctx.value.ptr + toklen;
746 while (1) {
747 while (qval < ctx.value.ptr + ctx.value.len && HTTP_IS_LWS(*qval))
748 qval++;
749
750 if (qval >= ctx.value.ptr + ctx.value.len || *qval != ';') {
751 qval = NULL;
752 break;
753 }
754 qval++;
755
756 while (qval < ctx.value.ptr + ctx.value.len && HTTP_IS_LWS(*qval))
757 qval++;
758
759 if (qval >= ctx.value.ptr + ctx.value.len) {
760 qval = NULL;
761 break;
762 }
763 if (strncmp(qval, "q=", MIN(ctx.value.ptr + ctx.value.len - qval, 2)) == 0)
764 break;
765
766 while (qval < ctx.value.ptr + ctx.value.len && *qval != ';')
767 qval++;
768 }
769
770 /* here we have qval pointing to the first "q=" attribute or NULL if not found */
771 q = qval ? http_parse_qvalue(qval + 2, NULL) : 1000;
772
773 if (q <= best_q)
774 continue;
775
776 for (comp_algo = comp_algo_back; comp_algo; comp_algo = comp_algo->next) {
777 if (*(ctx.value.ptr) == '*' ||
778 word_match(ctx.value.ptr, toklen, comp_algo->ua_name, comp_algo->ua_name_len)) {
779 st->comp_algo = comp_algo;
780 best_q = q;
781 break;
782 }
783 }
784 }
785 }
786
787 /* remove all occurrences of the header when "compression offload" is set */
788 if (st->comp_algo) {
789 if ((s->be->comp && s->be->comp->offload) ||
790 (strm_fe(s)->comp && strm_fe(s)->comp->offload)) {
791 http_remove_header(htx, &ctx);
792 ctx.blk = NULL;
793 while (http_find_header(htx, ist("Accept-Encoding"), &ctx, 1))
794 http_remove_header(htx, &ctx);
795 }
Christopher Faulet3d97c902015-12-09 14:59:38 +0100796 return 1;
797 }
798
799 /* identity is implicit does not require headers */
Christopher Faulet92d36382015-11-05 13:35:03 +0100800 if ((s->be->comp && (comp_algo_back = s->be->comp->algos)) ||
801 (strm_fe(s)->comp && (comp_algo_back = strm_fe(s)->comp->algos))) {
Christopher Faulet3d97c902015-12-09 14:59:38 +0100802 for (comp_algo = comp_algo_back; comp_algo; comp_algo = comp_algo->next) {
803 if (comp_algo->cfg_name_len == 8 && memcmp(comp_algo->cfg_name, "identity", 8) == 0) {
Christopher Faulet92d36382015-11-05 13:35:03 +0100804 st->comp_algo = comp_algo;
Christopher Faulet3d97c902015-12-09 14:59:38 +0100805 return 1;
806 }
807 }
808 }
809
Christopher Faulet92d36382015-11-05 13:35:03 +0100810 st->comp_algo = NULL;
Christopher Faulet3d97c902015-12-09 14:59:38 +0100811 return 0;
812}
813
Christopher Faulete6902cd2018-11-30 22:29:48 +0100814static int
815select_compression_request_header(struct comp_state *st, struct stream *s,
816 struct http_msg *msg)
817{
818 if (IS_HTX_STRM(s))
819 return htx_select_comp_reqhdr(st, s, msg);
820 else
821 return http_select_comp_reqhdr(st, s, msg);
822}
Christopher Faulet92d36382015-11-05 13:35:03 +0100823
Christopher Faulet3d97c902015-12-09 14:59:38 +0100824/*
825 * Selects a comression algorithm depending of the server response.
826 */
Christopher Faulet92d36382015-11-05 13:35:03 +0100827static int
Christopher Faulete6902cd2018-11-30 22:29:48 +0100828http_select_comp_reshdr(struct comp_state *st, struct stream *s, struct http_msg *msg)
Christopher Faulet3d97c902015-12-09 14:59:38 +0100829{
830 struct http_txn *txn = s->txn;
Olivier Houchard0b662842018-06-29 18:16:31 +0200831 struct channel *c = msg->chn;
Christopher Faulet3d97c902015-12-09 14:59:38 +0100832 struct hdr_ctx ctx;
833 struct comp_type *comp_type;
834
835 /* no common compression algorithm was found in request header */
Christopher Faulet92d36382015-11-05 13:35:03 +0100836 if (st->comp_algo == NULL)
Christopher Faulet3d97c902015-12-09 14:59:38 +0100837 goto fail;
838
Christopher Faulet1d3613a2019-01-07 14:41:59 +0100839 /* compression already in progress */
840 if (msg->flags & HTTP_MSGF_COMPRESSING)
841 goto fail;
842
Christopher Faulet3d97c902015-12-09 14:59:38 +0100843 /* HTTP < 1.1 should not be compressed */
844 if (!(msg->flags & HTTP_MSGF_VER_11) || !(txn->req.flags & HTTP_MSGF_VER_11))
845 goto fail;
846
Christopher Faulet92d36382015-11-05 13:35:03 +0100847 if (txn->meth == HTTP_METH_HEAD)
848 goto fail;
849
Christopher Faulet3d97c902015-12-09 14:59:38 +0100850 /* compress 200,201,202,203 responses only */
851 if ((txn->status != 200) &&
852 (txn->status != 201) &&
853 (txn->status != 202) &&
854 (txn->status != 203))
855 goto fail;
856
857
858 /* Content-Length is null */
859 if (!(msg->flags & HTTP_MSGF_TE_CHNK) && msg->body_len == 0)
860 goto fail;
861
862 /* content is already compressed */
863 ctx.idx = 0;
Olivier Houchard0b662842018-06-29 18:16:31 +0200864 if (http_find_header2("Content-Encoding", 16, ci_head(c), &txn->hdr_idx, &ctx))
Christopher Faulet3d97c902015-12-09 14:59:38 +0100865 goto fail;
866
867 /* no compression when Cache-Control: no-transform is present in the message */
868 ctx.idx = 0;
Olivier Houchard0b662842018-06-29 18:16:31 +0200869 while (http_find_header2("Cache-Control", 13, ci_head(c), &txn->hdr_idx, &ctx)) {
Christopher Faulet3d97c902015-12-09 14:59:38 +0100870 if (word_match(ctx.line + ctx.val, ctx.vlen, "no-transform", 12))
871 goto fail;
872 }
873
Tim Duesterhusb229f012019-01-29 16:38:56 +0100874 /* no compression when ETag is malformed */
875 ctx.idx = 0;
876 if (http_find_full_header2("ETag", 4, ci_head(c), &txn->hdr_idx, &ctx)) {
877 if (!(((ctx.vlen >= 4 && memcmp(ctx.line + ctx.val, "W/\"", 3) == 0) || /* Either a weak ETag */
878 (ctx.vlen >= 2 && ctx.line[ctx.val] == '"')) && /* or strong ETag */
879 ctx.line[ctx.val + ctx.vlen - 1] == '"')) {
880 goto fail;
881 }
882 }
883 /* no compression when multiple ETags are present
884 * Note: Do not reset ctx.idx!
885 */
886 if (http_find_full_header2("ETag", 4, ci_head(c), &txn->hdr_idx, &ctx))
887 goto fail;
888
Christopher Faulet3d97c902015-12-09 14:59:38 +0100889 comp_type = NULL;
890
891 /* we don't want to compress multipart content-types, nor content-types that are
892 * not listed in the "compression type" directive if any. If no content-type was
893 * found but configuration requires one, we don't compress either. Backend has
894 * the priority.
895 */
896 ctx.idx = 0;
Olivier Houchard0b662842018-06-29 18:16:31 +0200897 if (http_find_header2("Content-Type", 12, ci_head(c), &txn->hdr_idx, &ctx)) {
Christopher Faulet3d97c902015-12-09 14:59:38 +0100898 if (ctx.vlen >= 9 && strncasecmp("multipart", ctx.line+ctx.val, 9) == 0)
899 goto fail;
900
901 if ((s->be->comp && (comp_type = s->be->comp->types)) ||
902 (strm_fe(s)->comp && (comp_type = strm_fe(s)->comp->types))) {
903 for (; comp_type; comp_type = comp_type->next) {
904 if (ctx.vlen >= comp_type->name_len &&
905 strncasecmp(ctx.line+ctx.val, comp_type->name, comp_type->name_len) == 0)
906 /* this Content-Type should be compressed */
907 break;
908 }
909 /* this Content-Type should not be compressed */
910 if (comp_type == NULL)
911 goto fail;
912 }
913 }
914 else { /* no content-type header */
Christopher Faulet92d36382015-11-05 13:35:03 +0100915 if ((s->be->comp && s->be->comp->types) ||
916 (strm_fe(s)->comp && strm_fe(s)->comp->types))
Christopher Faulet3d97c902015-12-09 14:59:38 +0100917 goto fail; /* a content-type was required */
918 }
919
920 /* limit compression rate */
921 if (global.comp_rate_lim > 0)
922 if (read_freq_ctr(&global.comp_bps_in) > global.comp_rate_lim)
923 goto fail;
924
925 /* limit cpu usage */
926 if (idle_pct < compress_min_idle)
927 goto fail;
928
929 /* initialize compression */
Christopher Faulet92d36382015-11-05 13:35:03 +0100930 if (st->comp_algo->init(&st->comp_ctx, global.tune.comp_maxlevel) < 0)
Christopher Faulet3d97c902015-12-09 14:59:38 +0100931 goto fail;
Christopher Faulet92d36382015-11-05 13:35:03 +0100932 msg->flags |= HTTP_MSGF_COMPRESSING;
Christopher Faulet3d97c902015-12-09 14:59:38 +0100933 return 1;
934
935fail:
Christopher Faulet92d36382015-11-05 13:35:03 +0100936 st->comp_algo = NULL;
Christopher Faulet3d97c902015-12-09 14:59:38 +0100937 return 0;
938}
939
Christopher Faulete6902cd2018-11-30 22:29:48 +0100940static int
941htx_select_comp_reshdr(struct comp_state *st, struct stream *s, struct http_msg *msg)
942{
Christopher Faulet27ba2dc2018-12-05 11:53:24 +0100943 struct htx *htx = htxbuf(&msg->chn->buf);
Christopher Faulete6902cd2018-11-30 22:29:48 +0100944 struct http_txn *txn = s->txn;
945 struct http_hdr_ctx ctx;
946 struct comp_type *comp_type;
947
948 /* no common compression algorithm was found in request header */
949 if (st->comp_algo == NULL)
950 goto fail;
951
Christopher Faulet1d3613a2019-01-07 14:41:59 +0100952 /* compression already in progress */
953 if (msg->flags & HTTP_MSGF_COMPRESSING)
954 goto fail;
955
Christopher Faulete6902cd2018-11-30 22:29:48 +0100956 /* HTTP < 1.1 should not be compressed */
957 if (!(msg->flags & HTTP_MSGF_VER_11) || !(txn->req.flags & HTTP_MSGF_VER_11))
958 goto fail;
959
960 if (txn->meth == HTTP_METH_HEAD)
961 goto fail;
962
963 /* compress 200,201,202,203 responses only */
964 if ((txn->status != 200) &&
965 (txn->status != 201) &&
966 (txn->status != 202) &&
967 (txn->status != 203))
968 goto fail;
969
Christopher Fauletc963eb22018-12-21 14:53:54 +0100970 if (!(msg->flags & HTTP_MSGF_XFER_LEN) || msg->flags & HTTP_MSGF_BODYLESS)
Christopher Faulete6902cd2018-11-30 22:29:48 +0100971 goto fail;
972
973 /* content is already compressed */
974 ctx.blk = NULL;
975 if (http_find_header(htx, ist("Content-Encoding"), &ctx, 1))
976 goto fail;
977
978 /* no compression when Cache-Control: no-transform is present in the message */
979 ctx.blk = NULL;
980 while (http_find_header(htx, ist("Cache-Control"), &ctx, 0)) {
981 if (word_match(ctx.value.ptr, ctx.value.len, "no-transform", 12))
982 goto fail;
983 }
984
Tim Duesterhusb229f012019-01-29 16:38:56 +0100985 /* no compression when ETag is malformed */
986 ctx.blk = NULL;
987 if (http_find_header(htx, ist("ETag"), &ctx, 1)) {
988 if (!(((ctx.value.len >= 4 && memcmp(ctx.value.ptr, "W/\"", 3) == 0) || /* Either a weak ETag */
989 (ctx.value.len >= 2 && ctx.value.ptr[0] == '"')) && /* or strong ETag */
990 ctx.value.ptr[ctx.value.len - 1] == '"')) {
991 goto fail;
992 }
993 }
994 /* no compression when multiple ETags are present
995 * Note: Do not reset ctx.blk!
996 */
997 if (http_find_header(htx, ist("ETag"), &ctx, 1))
998 goto fail;
999
Christopher Faulete6902cd2018-11-30 22:29:48 +01001000 comp_type = NULL;
1001
1002 /* we don't want to compress multipart content-types, nor content-types that are
1003 * not listed in the "compression type" directive if any. If no content-type was
1004 * found but configuration requires one, we don't compress either. Backend has
1005 * the priority.
1006 */
1007 ctx.blk = NULL;
1008 if (http_find_header(htx, ist("Content-Type"), &ctx, 1)) {
1009 if (ctx.value.len >= 9 && strncasecmp("multipart", ctx.value.ptr, 9) == 0)
1010 goto fail;
1011
1012 if ((s->be->comp && (comp_type = s->be->comp->types)) ||
1013 (strm_fe(s)->comp && (comp_type = strm_fe(s)->comp->types))) {
1014 for (; comp_type; comp_type = comp_type->next) {
1015 if (ctx.value.len >= comp_type->name_len &&
1016 strncasecmp(ctx.value.ptr, comp_type->name, comp_type->name_len) == 0)
1017 /* this Content-Type should be compressed */
1018 break;
1019 }
1020 /* this Content-Type should not be compressed */
1021 if (comp_type == NULL)
1022 goto fail;
1023 }
1024 }
1025 else { /* no content-type header */
1026 if ((s->be->comp && s->be->comp->types) ||
1027 (strm_fe(s)->comp && strm_fe(s)->comp->types))
1028 goto fail; /* a content-type was required */
1029 }
1030
1031 /* limit compression rate */
1032 if (global.comp_rate_lim > 0)
1033 if (read_freq_ctr(&global.comp_bps_in) > global.comp_rate_lim)
1034 goto fail;
1035
1036 /* limit cpu usage */
1037 if (idle_pct < compress_min_idle)
1038 goto fail;
1039
1040 /* initialize compression */
1041 if (st->comp_algo->init(&st->comp_ctx, global.tune.comp_maxlevel) < 0)
1042 goto fail;
Christopher Faulete6902cd2018-11-30 22:29:48 +01001043 msg->flags |= HTTP_MSGF_COMPRESSING;
1044 return 1;
1045
1046 deinit_comp_ctx:
1047 st->comp_algo->end(&st->comp_ctx);
1048 fail:
1049 st->comp_algo = NULL;
1050 return 0;
1051}
1052
1053static int
1054select_compression_response_header(struct comp_state *st, struct stream *s, struct http_msg *msg)
1055{
1056 if (IS_HTX_STRM(s))
1057 return htx_select_comp_reshdr(st, s, msg);
1058 else
1059 return http_select_comp_reshdr(st, s, msg);
1060}
Christopher Faulet3d97c902015-12-09 14:59:38 +01001061/***********************************************************************/
1062/* emit the chunksize followed by a CRLF on the output and return the number of
1063 * bytes written. It goes backwards and starts with the byte before <end>. It
1064 * returns the number of bytes written which will not exceed 10 (8 digits, CR,
1065 * and LF). The caller is responsible for ensuring there is enough room left in
1066 * the output buffer for the string.
1067 */
1068static int
1069http_emit_chunk_size(char *end, unsigned int chksz)
1070{
1071 char *beg = end;
1072
1073 *--beg = '\n';
1074 *--beg = '\r';
1075 do {
1076 *--beg = hextab[chksz & 0xF];
1077 } while (chksz >>= 4);
1078 return end - beg;
1079}
1080
1081/*
1082 * Init HTTP compression
1083 */
Christopher Faulet92d36382015-11-05 13:35:03 +01001084static int
Christopher Fauletb61481c2018-12-17 13:17:53 +01001085http_compression_buffer_init(struct channel *inc, struct buffer *out)
Christopher Faulet3d97c902015-12-09 14:59:38 +01001086{
1087 /* output stream requires at least 10 bytes for the gzip header, plus
1088 * at least 8 bytes for the gzip trailer (crc+len), plus a possible
1089 * plus at most 5 bytes per 32kB block and 2 bytes to close the stream.
1090 */
Olivier Houchard0b662842018-06-29 18:16:31 +02001091 if (c_room(inc) < 20 + 5 * ((ci_data(inc) + 32767) >> 15))
Christopher Faulet3d97c902015-12-09 14:59:38 +01001092 return -1;
1093
1094 /* prepare an empty output buffer in which we reserve enough room for
1095 * copying the output bytes from <in>, plus 10 extra bytes to write
1096 * the chunk size. We don't copy the bytes yet so that if we have to
1097 * cancel the operation later, it's cheap.
1098 */
1099 b_reset(out);
Christopher Fauletb61481c2018-12-17 13:17:53 +01001100 out->head += co_data(inc) + 10;
Christopher Faulet3d97c902015-12-09 14:59:38 +01001101 return 0;
1102}
1103
Christopher Faulete6902cd2018-11-30 22:29:48 +01001104static int
1105htx_compression_buffer_init(struct htx *htx, struct buffer *out)
1106{
1107 /* output stream requires at least 10 bytes for the gzip header, plus
1108 * at least 8 bytes for the gzip trailer (crc+len), plus a possible
1109 * plus at most 5 bytes per 32kB block and 2 bytes to close the stream.
1110 */
1111 if (htx_free_space(htx) < 20 + 5 * ((htx->data + 32767) >> 15))
1112 return -1;
1113 b_reset(out);
1114 return 0;
1115}
1116
Christopher Faulet3d97c902015-12-09 14:59:38 +01001117/*
1118 * Add data to compress
1119 */
Christopher Faulet92d36382015-11-05 13:35:03 +01001120static int
1121http_compression_buffer_add_data(struct comp_state *st, struct buffer *in,
Willy Tarreaud54a8ce2018-06-29 18:42:02 +02001122 int in_out, struct buffer *out, int sz)
Christopher Faulet3d97c902015-12-09 14:59:38 +01001123{
Christopher Faulet3d97c902015-12-09 14:59:38 +01001124 int consumed_data = 0;
1125 int data_process_len;
1126 int block1, block2;
1127
Christopher Faulet92d36382015-11-05 13:35:03 +01001128 if (!sz)
Christopher Faulet3e7bc672015-12-07 13:39:08 +01001129 goto end;
Christopher Faulet3d97c902015-12-09 14:59:38 +01001130
Christopher Faulet92d36382015-11-05 13:35:03 +01001131 /* select the smallest size between the announced chunk size, the input
Christopher Faulet3d97c902015-12-09 14:59:38 +01001132 * data, and the available output buffer size. The compressors are
Christopher Faulet92d36382015-11-05 13:35:03 +01001133 * assumed to be able to process all the bytes we pass to them at
1134 * once. */
Willy Tarreaueac52592018-06-15 13:59:36 +02001135 data_process_len = MIN(b_room(out), sz);
Christopher Faulet92d36382015-11-05 13:35:03 +01001136
Christopher Faulet3d97c902015-12-09 14:59:38 +01001137 block1 = data_process_len;
Willy Tarreaud54a8ce2018-06-29 18:42:02 +02001138 if (block1 > b_contig_data(in, in_out))
1139 block1 = b_contig_data(in, in_out);
Christopher Faulet3d97c902015-12-09 14:59:38 +01001140 block2 = data_process_len - block1;
1141
1142 /* compressors return < 0 upon error or the amount of bytes read */
Christopher Faulet96667202018-12-17 12:02:57 +01001143 consumed_data = st->comp_algo->add_data(st->comp_ctx, b_peek(in, in_out), block1, out);
Christopher Faulet3e7bc672015-12-07 13:39:08 +01001144 if (consumed_data != block1 || !block2)
1145 goto end;
Christopher Faulet96667202018-12-17 12:02:57 +01001146 consumed_data = st->comp_algo->add_data(st->comp_ctx, b_orig(in), block2, out);
Christopher Faulet3e7bc672015-12-07 13:39:08 +01001147 if (consumed_data < 0)
1148 goto end;
1149 consumed_data += block1;
1150
1151 end:
Christopher Faulet3d97c902015-12-09 14:59:38 +01001152 return consumed_data;
1153}
1154
Christopher Faulete6902cd2018-11-30 22:29:48 +01001155static int
1156htx_compression_buffer_add_data(struct comp_state *st, const char *data, size_t len,
1157 struct buffer *out)
1158{
1159 return st->comp_algo->add_data(st->comp_ctx, data, len, out);
1160}
1161
Christopher Faulet3d97c902015-12-09 14:59:38 +01001162/*
1163 * Flush data in process, and write the header and footer of the chunk. Upon
1164 * success, in and out buffers are swapped to avoid a copy.
1165 */
Christopher Faulet92d36382015-11-05 13:35:03 +01001166static int
1167http_compression_buffer_end(struct comp_state *st, struct stream *s,
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001168 struct channel *chn, struct buffer *out,
Christopher Fauletb61481c2018-12-17 13:17:53 +01001169 int end)
Christopher Faulet3d97c902015-12-09 14:59:38 +01001170{
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001171 struct buffer tmp_buf;
Christopher Faulet3d97c902015-12-09 14:59:38 +01001172 char *tail;
Christopher Faulet92d36382015-11-05 13:35:03 +01001173 int to_forward, left;
Christopher Faulet3d97c902015-12-09 14:59:38 +01001174
1175#if defined(USE_SLZ) || defined(USE_ZLIB)
1176 int ret;
1177
1178 /* flush data here */
Christopher Faulet3d97c902015-12-09 14:59:38 +01001179 if (end)
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001180 ret = st->comp_algo->finish(st->comp_ctx, out); /* end of data */
Christopher Faulet3d97c902015-12-09 14:59:38 +01001181 else
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001182 ret = st->comp_algo->flush(st->comp_ctx, out); /* end of buffer */
Christopher Faulet3d97c902015-12-09 14:59:38 +01001183
1184 if (ret < 0)
1185 return -1; /* flush failed */
1186
1187#endif /* USE_ZLIB */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001188 if (b_data(out) == 0) {
Christopher Faulet3d97c902015-12-09 14:59:38 +01001189 /* No data were appended, let's drop the output buffer and
1190 * keep the input buffer unchanged.
1191 */
1192 return 0;
1193 }
1194
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001195 /* OK so at this stage, we have an output buffer <out> looking like this :
Christopher Faulet3d97c902015-12-09 14:59:38 +01001196 *
1197 * <-- o --> <------ i ----->
1198 * +---------+---+------------+-----------+
1199 * | out | c | comp_in | empty |
1200 * +---------+---+------------+-----------+
1201 * data p size
1202 *
Willy Tarreaud54a8ce2018-06-29 18:42:02 +02001203 * <out> is the room reserved to copy the channel output. It starts at
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001204 * out->area and has not yet been filled. <c> is the room reserved to
Willy Tarreaud54a8ce2018-06-29 18:42:02 +02001205 * write the chunk size (10 bytes). <comp_in> is the compressed
1206 * equivalent of the data part of ib->len. <empty> is the amount of
1207 * empty bytes at the end of the buffer, into which we may have to
1208 * copy the remaining bytes from ib->len after the data
1209 * (chunk size, trailers, ...).
Christopher Faulet3d97c902015-12-09 14:59:38 +01001210 */
1211
Joseph Herlant942eea32018-11-15 13:57:22 -08001212 /* Write real size at the beginning of the chunk, no need of wrapping.
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001213 * We write the chunk using a dynamic length and adjust out->p and out->i
Christopher Faulet3d97c902015-12-09 14:59:38 +01001214 * accordingly afterwards. That will move <out> away from <data>.
1215 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001216 left = http_emit_chunk_size(b_head(out), b_data(out));
1217 b_add(out, left);
Christopher Fauletb61481c2018-12-17 13:17:53 +01001218 out->head -= co_data(chn) + (left);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001219 /* Copy previous data from chn into out */
Willy Tarreaud54a8ce2018-06-29 18:42:02 +02001220 if (co_data(chn) > 0) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001221 left = b_contig_data(&chn->buf, 0);
Christopher Fauletb61481c2018-12-17 13:17:53 +01001222 if (left > co_data(chn))
1223 left = co_data(chn);
Christopher Faulet3d97c902015-12-09 14:59:38 +01001224
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001225 memcpy(b_head(out), co_head(chn), left);
1226 b_add(out, left);
Willy Tarreaud54a8ce2018-06-29 18:42:02 +02001227 if (co_data(chn) - left) {/* second part of the buffer */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001228 memcpy(b_head(out) + left, b_orig(&chn->buf), co_data(chn) - left);
1229 b_add(out, co_data(chn) - left);
Willy Tarreaud54a8ce2018-06-29 18:42:02 +02001230 }
Christopher Faulet3d97c902015-12-09 14:59:38 +01001231 }
1232
1233 /* chunked encoding requires CRLF after data */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001234 tail = b_tail(out);
Christopher Faulet3d97c902015-12-09 14:59:38 +01001235 *tail++ = '\r';
1236 *tail++ = '\n';
1237
Christopher Faulet2fb28802015-12-01 10:40:57 +01001238 /* At the end of data, we must write the empty chunk 0<CRLF>,
1239 * and terminate the trailers section with a last <CRLF>. If
1240 * we're forwarding a chunked-encoded response, we'll have a
1241 * trailers section after the empty chunk which needs to be
1242 * forwarded and which will provide the last CRLF. Otherwise
1243 * we write it ourselves.
1244 */
1245 if (end) {
1246 struct http_msg *msg = &s->txn->rsp;
1247
1248 memcpy(tail, "0\r\n", 3);
1249 tail += 3;
Christopher Fauletb77c5c22015-12-07 16:48:42 +01001250 if (!(msg->flags & HTTP_MSGF_TE_CHNK)) {
Christopher Faulet2fb28802015-12-01 10:40:57 +01001251 memcpy(tail, "\r\n", 2);
1252 tail += 2;
1253 }
1254 }
1255
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001256 b_add(out, tail - b_tail(out));
Christopher Fauletb61481c2018-12-17 13:17:53 +01001257 to_forward = b_data(out) - co_data(chn);
Christopher Faulet3d97c902015-12-09 14:59:38 +01001258
1259 /* update input rate */
Christopher Faulet92d36382015-11-05 13:35:03 +01001260 if (st->comp_ctx && st->comp_ctx->cur_lvl > 0) {
Christopher Faulet2fb28802015-12-01 10:40:57 +01001261 update_freq_ctr(&global.comp_bps_in, st->consumed);
Christopher Fauletff8abcd2017-06-02 15:33:24 +02001262 HA_ATOMIC_ADD(&strm_fe(s)->fe_counters.comp_in, st->consumed);
1263 HA_ATOMIC_ADD(&s->be->be_counters.comp_in, st->consumed);
Christopher Faulet3d97c902015-12-09 14:59:38 +01001264 } else {
Christopher Fauletff8abcd2017-06-02 15:33:24 +02001265 HA_ATOMIC_ADD(&strm_fe(s)->fe_counters.comp_byp, st->consumed);
1266 HA_ATOMIC_ADD(&s->be->be_counters.comp_byp, st->consumed);
Christopher Faulet3d97c902015-12-09 14:59:38 +01001267 }
1268
1269 /* copy the remaining data in the tmp buffer. */
Willy Tarreaubcbd3932018-06-06 07:13:22 +02001270 c_adv(chn, st->consumed);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001271 if (b_data(&chn->buf) - co_data(chn) > 0) {
Willy Tarreau7194d3c2018-06-06 16:55:45 +02001272 left = ci_contig_data(chn);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001273 memcpy(b_tail(out), ci_head(chn), left);
1274 b_add(out, left);
1275 if (b_data(&chn->buf) - (co_data(chn) + left)) {
1276 memcpy(b_tail(out), b_orig(&chn->buf), b_data(&chn->buf) - left);
1277 b_add(out, b_data(&chn->buf) - left);
Christopher Faulet3d97c902015-12-09 14:59:38 +01001278 }
1279 }
Christopher Fauletb61481c2018-12-17 13:17:53 +01001280 c_rew(chn, st->consumed);
1281
Christopher Faulet3d97c902015-12-09 14:59:38 +01001282 /* swap the buffers */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001283 tmp_buf = chn->buf;
1284 chn->buf = *out;
1285 *out = tmp_buf;
1286
Christopher Faulet92d36382015-11-05 13:35:03 +01001287 if (st->comp_ctx && st->comp_ctx->cur_lvl > 0) {
Christopher Faulet3d97c902015-12-09 14:59:38 +01001288 update_freq_ctr(&global.comp_bps_out, to_forward);
Christopher Fauletff8abcd2017-06-02 15:33:24 +02001289 HA_ATOMIC_ADD(&strm_fe(s)->fe_counters.comp_out, to_forward);
1290 HA_ATOMIC_ADD(&s->be->be_counters.comp_out, to_forward);
Christopher Faulet3d97c902015-12-09 14:59:38 +01001291 }
1292
Christopher Faulet3d97c902015-12-09 14:59:38 +01001293 return to_forward;
1294}
1295
Christopher Faulete6902cd2018-11-30 22:29:48 +01001296static int
1297htx_compression_buffer_end(struct comp_state *st, struct buffer *out, int end)
1298{
1299 if (end)
1300 return st->comp_algo->finish(st->comp_ctx, out);
1301 else
1302 return st->comp_algo->flush(st->comp_ctx, out);
1303}
1304
Christopher Faulet3d97c902015-12-09 14:59:38 +01001305
1306/***********************************************************************/
Christopher Faulet92d36382015-11-05 13:35:03 +01001307struct flt_ops comp_ops = {
Christopher Faulete6902cd2018-11-30 22:29:48 +01001308 .init = comp_flt_init,
Christopher Faulet8ca3b4b2017-07-25 11:07:15 +02001309 .init_per_thread = comp_flt_init_per_thread,
1310 .deinit_per_thread = comp_flt_deinit_per_thread,
Christopher Faulet92d36382015-11-05 13:35:03 +01001311
1312 .channel_start_analyze = comp_start_analyze,
Christopher Faulet92d36382015-11-05 13:35:03 +01001313 .channel_end_analyze = comp_end_analyze,
Christopher Faulet3dc860d2017-09-15 11:39:36 +02001314 .channel_post_analyze = comp_http_post_analyze,
Christopher Faulet92d36382015-11-05 13:35:03 +01001315
Christopher Faulet1339d742016-05-11 16:48:33 +02001316 .http_headers = comp_http_headers,
Christopher Faulete6902cd2018-11-30 22:29:48 +01001317 .http_payload = comp_http_payload,
1318 .http_end = comp_http_end,
1319
Christopher Faulet309c6412015-12-02 09:57:32 +01001320 .http_data = comp_http_data,
1321 .http_chunk_trailers = comp_http_chunk_trailers,
1322 .http_forward_data = comp_http_forward_data,
Christopher Faulet92d36382015-11-05 13:35:03 +01001323};
1324
Christopher Faulet3d97c902015-12-09 14:59:38 +01001325static int
1326parse_compression_options(char **args, int section, struct proxy *proxy,
1327 struct proxy *defpx, const char *file, int line,
1328 char **err)
1329{
Christopher Faulet92d36382015-11-05 13:35:03 +01001330 struct comp *comp;
Christopher Faulet3d97c902015-12-09 14:59:38 +01001331
1332 if (proxy->comp == NULL) {
Vincent Bernat02779b62016-04-03 13:48:43 +02001333 comp = calloc(1, sizeof(*comp));
Christopher Faulet3d97c902015-12-09 14:59:38 +01001334 proxy->comp = comp;
1335 }
1336 else
1337 comp = proxy->comp;
1338
1339 if (!strcmp(args[1], "algo")) {
1340 struct comp_ctx *ctx;
1341 int cur_arg = 2;
1342
1343 if (!*args[cur_arg]) {
1344 memprintf(err, "parsing [%s:%d] : '%s' expects <algorithm>\n",
1345 file, line, args[0]);
1346 return -1;
1347 }
1348 while (*(args[cur_arg])) {
1349 if (comp_append_algo(comp, args[cur_arg]) < 0) {
1350 memprintf(err, "'%s' : '%s' is not a supported algorithm.\n",
1351 args[0], args[cur_arg]);
1352 return -1;
1353 }
1354 if (proxy->comp->algos->init(&ctx, 9) == 0)
1355 proxy->comp->algos->end(&ctx);
1356 else {
1357 memprintf(err, "'%s' : Can't init '%s' algorithm.\n",
1358 args[0], args[cur_arg]);
1359 return -1;
1360 }
1361 cur_arg++;
1362 continue;
1363 }
1364 }
1365 else if (!strcmp(args[1], "offload"))
1366 comp->offload = 1;
1367 else if (!strcmp(args[1], "type")) {
1368 int cur_arg = 2;
1369
1370 if (!*args[cur_arg]) {
1371 memprintf(err, "'%s' expects <type>\n", args[0]);
1372 return -1;
1373 }
1374 while (*(args[cur_arg])) {
1375 comp_append_type(comp, args[cur_arg]);
1376 cur_arg++;
1377 continue;
1378 }
1379 }
1380 else {
1381 memprintf(err, "'%s' expects 'algo', 'type' or 'offload'\n",
1382 args[0]);
1383 return -1;
1384 }
1385
1386 return 0;
1387}
1388
Christopher Faulet92d36382015-11-05 13:35:03 +01001389static int
1390parse_http_comp_flt(char **args, int *cur_arg, struct proxy *px,
Thierry Fournier3610c392016-04-13 18:27:51 +02001391 struct flt_conf *fconf, char **err, void *private)
Christopher Faulet92d36382015-11-05 13:35:03 +01001392{
Christopher Faulet443ea1a2016-02-04 13:40:26 +01001393 struct flt_conf *fc, *back;
Christopher Faulet92d36382015-11-05 13:35:03 +01001394
Christopher Faulet443ea1a2016-02-04 13:40:26 +01001395 list_for_each_entry_safe(fc, back, &px->filter_configs, list) {
1396 if (fc->id == http_comp_flt_id) {
Christopher Faulet92d36382015-11-05 13:35:03 +01001397 memprintf(err, "%s: Proxy supports only one compression filter\n", px->id);
1398 return -1;
1399 }
1400 }
1401
Christopher Faulet443ea1a2016-02-04 13:40:26 +01001402 fconf->id = http_comp_flt_id;
1403 fconf->conf = NULL;
1404 fconf->ops = &comp_ops;
Christopher Faulet92d36382015-11-05 13:35:03 +01001405 (*cur_arg)++;
1406
1407 return 0;
1408}
1409
1410
1411int
Christopher Fauletc9df7f72018-12-10 16:14:04 +01001412check_implicit_http_comp_flt(struct proxy *proxy)
Christopher Faulet92d36382015-11-05 13:35:03 +01001413{
Christopher Faulet443ea1a2016-02-04 13:40:26 +01001414 struct flt_conf *fconf;
Christopher Faulet27d93c32018-12-15 22:32:02 +01001415 int explicit = 0;
1416 int comp = 0;
Christopher Faulet92d36382015-11-05 13:35:03 +01001417 int err = 0;
1418
1419 if (proxy->comp == NULL)
1420 goto end;
Christopher Faulet443ea1a2016-02-04 13:40:26 +01001421 if (!LIST_ISEMPTY(&proxy->filter_configs)) {
1422 list_for_each_entry(fconf, &proxy->filter_configs, list) {
1423 if (fconf->id == http_comp_flt_id)
Christopher Faulet27d93c32018-12-15 22:32:02 +01001424 comp = 1;
1425 else if (fconf->id == cache_store_flt_id) {
1426 if (comp) {
1427 ha_alert("config: %s '%s': unable to enable the compression filter "
1428 "before any cache filter.\n",
1429 proxy_type_str(proxy), proxy->id);
1430 err++;
1431 goto end;
1432 }
1433 }
1434 else
1435 explicit = 1;
Christopher Faulet92d36382015-11-05 13:35:03 +01001436 }
Christopher Faulet27d93c32018-12-15 22:32:02 +01001437 }
1438 if (comp)
1439 goto end;
1440 else if (explicit) {
1441 ha_alert("config: %s '%s': require an explicit filter declaration to use "
1442 "HTTP compression\n", proxy_type_str(proxy), proxy->id);
Christopher Faulet92d36382015-11-05 13:35:03 +01001443 err++;
1444 goto end;
1445 }
1446
Christopher Faulet27d93c32018-12-15 22:32:02 +01001447 /* Implicit declaration of the compression filter is always the last
1448 * one */
Christopher Faulet443ea1a2016-02-04 13:40:26 +01001449 fconf = calloc(1, sizeof(*fconf));
1450 if (!fconf) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001451 ha_alert("config: %s '%s': out of memory\n",
1452 proxy_type_str(proxy), proxy->id);
Christopher Faulet92d36382015-11-05 13:35:03 +01001453 err++;
1454 goto end;
1455 }
Christopher Faulet443ea1a2016-02-04 13:40:26 +01001456 fconf->id = http_comp_flt_id;
1457 fconf->conf = NULL;
1458 fconf->ops = &comp_ops;
1459 LIST_ADDQ(&proxy->filter_configs, &fconf->list);
Christopher Faulet92d36382015-11-05 13:35:03 +01001460 end:
1461 return err;
1462}
1463
1464/*
1465 * boolean, returns true if compression is used (either gzip or deflate) in the
1466 * response.
1467 */
Christopher Faulet3d97c902015-12-09 14:59:38 +01001468static int
Christopher Faulet92d36382015-11-05 13:35:03 +01001469smp_fetch_res_comp(const struct arg *args, struct sample *smp, const char *kw,
1470 void *private)
Christopher Faulet3d97c902015-12-09 14:59:38 +01001471{
Willy Tarreaube508f12016-03-10 11:47:01 +01001472 struct http_txn *txn = smp->strm ? smp->strm->txn : NULL;
Christopher Faulet92d36382015-11-05 13:35:03 +01001473
Christopher Faulet3d97c902015-12-09 14:59:38 +01001474 smp->data.type = SMP_T_BOOL;
Christopher Faulet92d36382015-11-05 13:35:03 +01001475 smp->data.u.sint = (txn && (txn->rsp.flags & HTTP_MSGF_COMPRESSING));
Christopher Faulet3d97c902015-12-09 14:59:38 +01001476 return 1;
1477}
1478
Christopher Faulet92d36382015-11-05 13:35:03 +01001479/*
1480 * string, returns algo
1481 */
Christopher Faulet3d97c902015-12-09 14:59:38 +01001482static int
Christopher Faulet92d36382015-11-05 13:35:03 +01001483smp_fetch_res_comp_algo(const struct arg *args, struct sample *smp,
1484 const char *kw, void *private)
Christopher Faulet3d97c902015-12-09 14:59:38 +01001485{
Willy Tarreaube508f12016-03-10 11:47:01 +01001486 struct http_txn *txn = smp->strm ? smp->strm->txn : NULL;
Christopher Faulet92d36382015-11-05 13:35:03 +01001487 struct filter *filter;
1488 struct comp_state *st;
1489
Christopher Faulet03d85532017-09-15 10:14:43 +02001490 if (!txn || !(txn->rsp.flags & HTTP_MSGF_COMPRESSING))
Christopher Faulet3d97c902015-12-09 14:59:38 +01001491 return 0;
1492
Christopher Fauletfcf035c2015-12-03 11:48:03 +01001493 list_for_each_entry(filter, &strm_flt(smp->strm)->filters, list) {
Christopher Faulet443ea1a2016-02-04 13:40:26 +01001494 if (FLT_ID(filter) != http_comp_flt_id)
Christopher Faulet92d36382015-11-05 13:35:03 +01001495 continue;
1496
1497 if (!(st = filter->ctx))
1498 break;
1499
1500 smp->data.type = SMP_T_STR;
1501 smp->flags = SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001502 smp->data.u.str.area = st->comp_algo->cfg_name;
1503 smp->data.u.str.data = st->comp_algo->cfg_name_len;
Christopher Faulet92d36382015-11-05 13:35:03 +01001504 return 1;
1505 }
1506 return 0;
Christopher Faulet3d97c902015-12-09 14:59:38 +01001507}
1508
1509/* Declare the config parser for "compression" keyword */
1510static struct cfg_kw_list cfg_kws = {ILH, {
1511 { CFG_LISTEN, "compression", parse_compression_options },
1512 { 0, NULL, NULL },
1513 }
1514};
1515
Willy Tarreau0108d902018-11-25 19:14:37 +01001516INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
1517
Christopher Faulet92d36382015-11-05 13:35:03 +01001518/* Declare the filter parser for "compression" keyword */
1519static struct flt_kw_list filter_kws = { "COMP", { }, {
Thierry Fournier3610c392016-04-13 18:27:51 +02001520 { "compression", parse_http_comp_flt, NULL },
1521 { NULL, NULL, NULL },
Christopher Faulet92d36382015-11-05 13:35:03 +01001522 }
1523};
1524
Willy Tarreau0108d902018-11-25 19:14:37 +01001525INITCALL1(STG_REGISTER, flt_register_keywords, &filter_kws);
1526
Christopher Faulet3d97c902015-12-09 14:59:38 +01001527/* Note: must not be declared <const> as its list will be overwritten */
1528static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
Christopher Faulet92d36382015-11-05 13:35:03 +01001529 { "res.comp", smp_fetch_res_comp, 0, NULL, SMP_T_BOOL, SMP_USE_HRSHP },
1530 { "res.comp_algo", smp_fetch_res_comp_algo, 0, NULL, SMP_T_STR, SMP_USE_HRSHP },
1531 { /* END */ },
1532 }
1533};
Christopher Faulet3d97c902015-12-09 14:59:38 +01001534
Willy Tarreau0108d902018-11-25 19:14:37 +01001535INITCALL1(STG_REGISTER, sample_register_fetches, &sample_fetch_keywords);