blob: 6cad8e1b9008381a7dbd8f9bce834f8ad1d635bb [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) {
Willy Tarreauef6fd852019-02-04 11:48:03 +0100288 update_freq_ctr(&global.comp_bps_in, consumed);
Olivier Houchard43da3432019-03-08 18:50:27 +0100289 _HA_ATOMIC_ADD(&strm_fe(s)->fe_counters.comp_in, consumed);
290 _HA_ATOMIC_ADD(&s->be->be_counters.comp_in, consumed);
Christopher Faulete6902cd2018-11-30 22:29:48 +0100291 update_freq_ctr(&global.comp_bps_out, to_forward);
Olivier Houchard43da3432019-03-08 18:50:27 +0100292 _HA_ATOMIC_ADD(&strm_fe(s)->fe_counters.comp_out, to_forward);
293 _HA_ATOMIC_ADD(&s->be->be_counters.comp_out, to_forward);
Willy Tarreauef6fd852019-02-04 11:48:03 +0100294 } else {
Olivier Houchard43da3432019-03-08 18:50:27 +0100295 _HA_ATOMIC_ADD(&strm_fe(s)->fe_counters.comp_byp, consumed);
296 _HA_ATOMIC_ADD(&s->be->be_counters.comp_byp, consumed);
Christopher Faulete6902cd2018-11-30 22:29:48 +0100297 }
298 return to_forward;
299
300 error:
301 return -1;
302}
303
304static int
Christopher Faulet2fb28802015-12-01 10:40:57 +0100305comp_http_data(struct stream *s, struct filter *filter, struct http_msg *msg)
Christopher Faulet92d36382015-11-05 13:35:03 +0100306{
307 struct comp_state *st = filter->ctx;
Willy Tarreaubcbd3932018-06-06 07:13:22 +0200308 struct channel *chn = msg->chn;
Christopher Faulet3e7bc672015-12-07 13:39:08 +0100309 unsigned int *nxt = &flt_rsp_nxt(filter);
Christopher Faulet2fb28802015-12-01 10:40:57 +0100310 unsigned int len;
Christopher Faulet92d36382015-11-05 13:35:03 +0100311 int ret;
312
Olivier Houchard0b662842018-06-29 18:16:31 +0200313 len = MIN(msg->chunk_len + msg->next, ci_data(chn)) - *nxt;
Christopher Faulet2fb28802015-12-01 10:40:57 +0100314 if (!len)
315 return len;
316
317 if (!st->initialized) {
Christopher Fauletb77c5c22015-12-07 16:48:42 +0100318 unsigned int fwd = flt_rsp_fwd(filter) + st->hdrs_len;
Christopher Faulet3e7bc672015-12-07 13:39:08 +0100319
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200320 b_reset(&tmpbuf);
Willy Tarreaubcbd3932018-06-06 07:13:22 +0200321 c_adv(chn, fwd);
Christopher Fauletb61481c2018-12-17 13:17:53 +0100322 ret = http_compression_buffer_init(chn, &zbuf);
Willy Tarreaubcbd3932018-06-06 07:13:22 +0200323 c_rew(chn, fwd);
Christopher Faulet2fb28802015-12-01 10:40:57 +0100324 if (ret < 0) {
325 msg->chn->flags |= CF_WAKE_WRITE;
326 return 0;
327 }
328 }
Christopher Fauletb77c5c22015-12-07 16:48:42 +0100329
330 if (msg->flags & HTTP_MSGF_TE_CHNK) {
Christopher Faulet06ecf3a2016-09-22 15:31:43 +0200331 int block;
Christopher Fauletb77c5c22015-12-07 16:48:42 +0100332
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200333 len = MIN(b_room(&tmpbuf), len);
Christopher Faulet06ecf3a2016-09-22 15:31:43 +0200334
Willy Tarreaubcbd3932018-06-06 07:13:22 +0200335 c_adv(chn, *nxt);
Willy Tarreau7194d3c2018-06-06 16:55:45 +0200336 block = ci_contig_data(chn);
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200337 memcpy(b_tail(&tmpbuf), ci_head(chn), block);
Christopher Faulet06ecf3a2016-09-22 15:31:43 +0200338 if (len > block)
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200339 memcpy(b_tail(&tmpbuf)+block, b_orig(&chn->buf), len-block);
Willy Tarreaubcbd3932018-06-06 07:13:22 +0200340 c_rew(chn, *nxt);
Christopher Faulet06ecf3a2016-09-22 15:31:43 +0200341
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200342 b_add(&tmpbuf, len);
Christopher Fauletb77c5c22015-12-07 16:48:42 +0100343 ret = len;
344 }
345 else {
Willy Tarreaubcbd3932018-06-06 07:13:22 +0200346 c_adv(chn, *nxt);
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200347 ret = http_compression_buffer_add_data(st, &chn->buf, co_data(chn), &zbuf, len);
Willy Tarreaubcbd3932018-06-06 07:13:22 +0200348 c_rew(chn, *nxt);
Christopher Fauletb77c5c22015-12-07 16:48:42 +0100349 if (ret < 0)
350 return ret;
351 }
Christopher Faulet92d36382015-11-05 13:35:03 +0100352
Christopher Faulet2fb28802015-12-01 10:40:57 +0100353 st->initialized = 1;
354 msg->next += ret;
355 msg->chunk_len -= ret;
Christopher Faulet3e7bc672015-12-07 13:39:08 +0100356 *nxt = msg->next;
Christopher Faulet2fb28802015-12-01 10:40:57 +0100357 return 0;
Christopher Faulet92d36382015-11-05 13:35:03 +0100358}
359
360static int
Christopher Faulet2fb28802015-12-01 10:40:57 +0100361comp_http_chunk_trailers(struct stream *s, struct filter *filter,
362 struct http_msg *msg)
Christopher Faulet92d36382015-11-05 13:35:03 +0100363{
364 struct comp_state *st = filter->ctx;
Christopher Faulet92d36382015-11-05 13:35:03 +0100365
Christopher Fauletb77c5c22015-12-07 16:48:42 +0100366 if (!st->initialized) {
367 if (!st->finished) {
Willy Tarreaubcbd3932018-06-06 07:13:22 +0200368 struct channel *chn = msg->chn;
Christopher Fauletb77c5c22015-12-07 16:48:42 +0100369 unsigned int fwd = flt_rsp_fwd(filter) + st->hdrs_len;
Christopher Faulet2fb28802015-12-01 10:40:57 +0100370
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200371 b_reset(&tmpbuf);
Willy Tarreaubcbd3932018-06-06 07:13:22 +0200372 c_adv(chn, fwd);
Christopher Fauletb61481c2018-12-17 13:17:53 +0100373 http_compression_buffer_init(chn, &zbuf);
Willy Tarreaubcbd3932018-06-06 07:13:22 +0200374 c_rew(chn, fwd);
Christopher Fauletb77c5c22015-12-07 16:48:42 +0100375 st->initialized = 1;
376 }
377 }
378 st->tlrs_len = msg->sol;
Christopher Faulet2fb28802015-12-01 10:40:57 +0100379 return 1;
Christopher Faulet92d36382015-11-05 13:35:03 +0100380}
381
Christopher Faulet2fb28802015-12-01 10:40:57 +0100382
Christopher Faulet92d36382015-11-05 13:35:03 +0100383static int
384comp_http_forward_data(struct stream *s, struct filter *filter,
385 struct http_msg *msg, unsigned int len)
386{
387 struct comp_state *st = filter->ctx;
Christopher Faulet2fb28802015-12-01 10:40:57 +0100388 int ret;
Christopher Faulet92d36382015-11-05 13:35:03 +0100389
Christopher Faulet2fb28802015-12-01 10:40:57 +0100390 /* To work, previous filters MUST forward all data */
Christopher Fauletb77c5c22015-12-07 16:48:42 +0100391 if (flt_rsp_fwd(filter) + len != flt_rsp_nxt(filter)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100392 ha_warning("HTTP compression failed: unexpected behavior of previous filters\n");
Christopher Faulet2fb28802015-12-01 10:40:57 +0100393 return -1;
Christopher Faulet92d36382015-11-05 13:35:03 +0100394 }
395
Christopher Faulet2fb28802015-12-01 10:40:57 +0100396 if (!st->initialized) {
Christopher Fauletb77c5c22015-12-07 16:48:42 +0100397 if (!len) {
Joseph Herlant942eea32018-11-15 13:57:22 -0800398 /* Nothing to forward */
Christopher Fauletb77c5c22015-12-07 16:48:42 +0100399 ret = len;
400 }
401 else if (st->hdrs_len > len) {
402 /* Forward part of headers */
403 ret = len;
404 st->hdrs_len -= len;
405 }
406 else if (st->hdrs_len > 0) {
407 /* Forward remaining headers */
408 ret = st->hdrs_len;
409 st->hdrs_len = 0;
410 }
411 else if (msg->msg_state < HTTP_MSG_TRAILERS) {
412 /* Do not forward anything for now. This only happens
413 * with chunk-encoded responses. Waiting data are part
414 * of the chunk envelope (the chunk size or the chunk
415 * CRLF). These data will be skipped during the
416 * compression. */
417 ret = 0;
418 }
419 else {
420 /* Forward trailers data */
421 ret = len;
422 }
Christopher Faulet2fb28802015-12-01 10:40:57 +0100423 return ret;
Christopher Faulet92d36382015-11-05 13:35:03 +0100424 }
425
Christopher Fauletb77c5c22015-12-07 16:48:42 +0100426 if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200427 ret = http_compression_buffer_add_data(st, &tmpbuf, 0,
428 &zbuf, b_data(&tmpbuf));
429 if (ret != b_data(&tmpbuf)) {
Willy Tarreau506a29a2018-07-18 10:07:58 +0200430 ha_warning("HTTP compression failed: Must consume %u bytes but only %d bytes consumed\n",
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200431 (unsigned int)b_data(&tmpbuf), ret);
Christopher Fauletb77c5c22015-12-07 16:48:42 +0100432 return -1;
433 }
434 }
435
436 st->consumed = len - st->hdrs_len - st->tlrs_len;
Willy Tarreaubcbd3932018-06-06 07:13:22 +0200437 c_adv(msg->chn, flt_rsp_fwd(filter) + st->hdrs_len);
Christopher Fauletb61481c2018-12-17 13:17:53 +0100438 ret = http_compression_buffer_end(st, s, msg->chn, &zbuf, msg->msg_state >= HTTP_MSG_TRAILERS);
Willy Tarreaubcbd3932018-06-06 07:13:22 +0200439 c_rew(msg->chn, flt_rsp_fwd(filter) + st->hdrs_len);
Christopher Faulet2fb28802015-12-01 10:40:57 +0100440 if (ret < 0)
441 return ret;
Christopher Faulet92d36382015-11-05 13:35:03 +0100442
Christopher Fauletb77c5c22015-12-07 16:48:42 +0100443 flt_change_forward_size(filter, msg->chn, ret - st->consumed);
444 msg->next += (ret - st->consumed);
445 ret += st->hdrs_len + st->tlrs_len;
446
Christopher Faulet2fb28802015-12-01 10:40:57 +0100447 st->initialized = 0;
Christopher Fauletb77c5c22015-12-07 16:48:42 +0100448 st->finished = (msg->msg_state >= HTTP_MSG_TRAILERS);
449 st->hdrs_len = 0;
450 st->tlrs_len = 0;
Christopher Faulet92d36382015-11-05 13:35:03 +0100451 return ret;
452}
Christopher Faulet3d97c902015-12-09 14:59:38 +0100453
Christopher Fauletd60b3cf2017-06-26 11:47:13 +0200454static int
455comp_http_end(struct stream *s, struct filter *filter,
456 struct http_msg *msg)
457{
458 struct comp_state *st = filter->ctx;
459
460 if (!(msg->chn->flags & CF_ISRESP) || !st || !st->comp_algo)
461 goto end;
462
463 if (strm_fe(s)->mode == PR_MODE_HTTP)
Olivier Houchard43da3432019-03-08 18:50:27 +0100464 _HA_ATOMIC_ADD(&strm_fe(s)->fe_counters.p.http.comp_rsp, 1);
Christopher Fauletd60b3cf2017-06-26 11:47:13 +0200465 if ((s->flags & SF_BE_ASSIGNED) && (s->be->mode == PR_MODE_HTTP))
Olivier Houchard43da3432019-03-08 18:50:27 +0100466 _HA_ATOMIC_ADD(&s->be->be_counters.p.http.comp_rsp, 1);
Christopher Fauletd60b3cf2017-06-26 11:47:13 +0200467 end:
468 return 1;
469}
Christopher Faulet3d97c902015-12-09 14:59:38 +0100470/***********************************************************************/
Christopher Faulet27d93c32018-12-15 22:32:02 +0100471static int
472http_set_comp_reshdr(struct comp_state *st, struct stream *s, struct http_msg *msg)
473{
474 struct http_txn *txn = s->txn;
Tim Duesterhusb229f012019-01-29 16:38:56 +0100475 struct hdr_ctx ctx;
Christopher Faulet27d93c32018-12-15 22:32:02 +0100476
477 /*
478 * Add Content-Encoding header when it's not identity encoding.
479 * RFC 2616 : Identity encoding: This content-coding is used only in the
480 * Accept-Encoding header, and SHOULD NOT be used in the Content-Encoding
481 * header.
482 */
483 if (st->comp_algo->cfg_name_len != 8 || memcmp(st->comp_algo->cfg_name, "identity", 8) != 0) {
484 trash.data = 18;
485 memcpy(trash.area, "Content-Encoding: ", trash.data);
486 memcpy(trash.area + trash.data, st->comp_algo->ua_name,
487 st->comp_algo->ua_name_len);
488 trash.data += st->comp_algo->ua_name_len;
489 trash.area[trash.data] = '\0';
490 if (http_header_add_tail2(msg, &txn->hdr_idx, trash.area, trash.data) < 0)
491 goto error;
492 }
493
494 /* remove Content-Length header */
495 if (msg->flags & HTTP_MSGF_CNT_LEN) {
Christopher Faulet27d93c32018-12-15 22:32:02 +0100496 ctx.idx = 0;
497 while (http_find_header2("Content-Length", 14, ci_head(&s->res), &txn->hdr_idx, &ctx))
498 http_remove_header2(msg, &txn->hdr_idx, &ctx);
499 }
500
501 /* add Transfer-Encoding header */
502 if (!(msg->flags & HTTP_MSGF_TE_CHNK)) {
503 if (http_header_add_tail2(msg, &txn->hdr_idx, "Transfer-Encoding: chunked", 26) < 0)
504 goto error;
505 }
506
Tim Duesterhusb229f012019-01-29 16:38:56 +0100507 ctx.idx = 0;
508 if (http_find_full_header2("ETag", 4, ci_head(&s->res), &txn->hdr_idx, &ctx)) {
509 if (ctx.line[ctx.val] == '"') {
510 /* This a strong ETag. Convert it to a weak one. */
511 trash.data = 8;
512 if (trash.data + ctx.vlen > trash.size)
513 goto error;
514 memcpy(trash.area, "ETag: W/", trash.data);
515 memcpy(trash.area + trash.data, ctx.line + ctx.val, ctx.vlen);
516 trash.data += ctx.vlen;
517 trash.area[trash.data] = '\0';
518 http_remove_header2(msg, &txn->hdr_idx, &ctx);
519 if (http_header_add_tail2(msg, &txn->hdr_idx, trash.area, trash.data) < 0)
520 goto error;
521 }
522 }
Christopher Faulet27d93c32018-12-15 22:32:02 +0100523
524 return 1;
525
526 error:
527 st->comp_algo->end(&st->comp_ctx);
528 st->comp_algo = NULL;
529 return 0;
530}
531
532static int
533htx_set_comp_reshdr(struct comp_state *st, struct stream *s, struct http_msg *msg)
534{
535 struct htx *htx = htxbuf(&msg->chn->buf);
Tim Duesterhusb229f012019-01-29 16:38:56 +0100536 struct http_hdr_ctx ctx;
Christopher Faulet27d93c32018-12-15 22:32:02 +0100537
538 /*
539 * Add Content-Encoding header when it's not identity encoding.
540 * RFC 2616 : Identity encoding: This content-coding is used only in the
541 * Accept-Encoding header, and SHOULD NOT be used in the Content-Encoding
542 * header.
543 */
544 if (st->comp_algo->cfg_name_len != 8 || memcmp(st->comp_algo->cfg_name, "identity", 8) != 0) {
545 struct ist v = ist2(st->comp_algo->ua_name, st->comp_algo->ua_name_len);
546
547 if (!http_add_header(htx, ist("Content-Encoding"), v))
548 goto error;
549 }
550
551 /* remove Content-Length header */
552 if (msg->flags & HTTP_MSGF_CNT_LEN) {
Christopher Faulet27d93c32018-12-15 22:32:02 +0100553 ctx.blk = NULL;
554 while (http_find_header(htx, ist("Content-Length"), &ctx, 1))
555 http_remove_header(htx, &ctx);
556 }
557
558 /* add "Transfer-Encoding: chunked" header */
559 if (!(msg->flags & HTTP_MSGF_TE_CHNK)) {
560 if (!http_add_header(htx, ist("Transfer-Encoding"), ist("chunked")))
561 goto error;
562 }
563
Tim Duesterhusb229f012019-01-29 16:38:56 +0100564 /* convert "ETag" header to a weak ETag */
565 ctx.blk = NULL;
566 if (http_find_header(htx, ist("ETag"), &ctx, 1)) {
567 if (ctx.value.ptr[0] == '"') {
568 /* This a strong ETag. Convert it to a weak one. */
569 struct ist v = ist2(trash.area, 0);
570 if (istcat(&v, ist("W/"), trash.size) == -1 || istcat(&v, ctx.value, trash.size) == -1)
571 goto error;
572
573 if (!http_replace_header_value(htx, &ctx, v))
574 goto error;
575 }
576 }
577
Christopher Faulet27d93c32018-12-15 22:32:02 +0100578 return 1;
579
580 error:
581 st->comp_algo->end(&st->comp_ctx);
582 st->comp_algo = NULL;
583 return 0;
584}
585
586static int
587set_compression_response_header(struct comp_state *st, struct stream *s, struct http_msg *msg)
588{
589 if (IS_HTX_STRM(s))
590 return htx_set_comp_reshdr(st, s, msg);
591 else
592 return http_set_comp_reshdr(st, s, msg);
593}
594
Christopher Faulet3d97c902015-12-09 14:59:38 +0100595/*
596 * Selects a compression algorithm depending on the client request.
597 */
Christopher Faulete6902cd2018-11-30 22:29:48 +0100598static int
599http_select_comp_reqhdr(struct comp_state *st, struct stream *s, struct http_msg *msg)
Christopher Faulet3d97c902015-12-09 14:59:38 +0100600{
601 struct http_txn *txn = s->txn;
Olivier Houchard0b662842018-06-29 18:16:31 +0200602 struct channel *req = msg->chn;
Christopher Faulet3d97c902015-12-09 14:59:38 +0100603 struct hdr_ctx ctx;
604 struct comp_algo *comp_algo = NULL;
605 struct comp_algo *comp_algo_back = NULL;
606
607 /* Disable compression for older user agents announcing themselves as "Mozilla/4"
608 * unless they are known good (MSIE 6 with XP SP2, or MSIE 7 and later).
609 * See http://zoompf.com/2012/02/lose-the-wait-http-compression for more details.
610 */
611 ctx.idx = 0;
Olivier Houchard0b662842018-06-29 18:16:31 +0200612 if (http_find_header2("User-Agent", 10, ci_head(req), &txn->hdr_idx, &ctx) &&
Christopher Faulet3d97c902015-12-09 14:59:38 +0100613 ctx.vlen >= 9 &&
614 memcmp(ctx.line + ctx.val, "Mozilla/4", 9) == 0 &&
615 (ctx.vlen < 31 ||
616 memcmp(ctx.line + ctx.val + 25, "MSIE ", 5) != 0 ||
617 ctx.line[ctx.val + 30] < '6' ||
618 (ctx.line[ctx.val + 30] == '6' &&
619 (ctx.vlen < 54 || memcmp(ctx.line + 51, "SV1", 3) != 0)))) {
Christopher Faulet92d36382015-11-05 13:35:03 +0100620 st->comp_algo = NULL;
Christopher Faulet3d97c902015-12-09 14:59:38 +0100621 return 0;
622 }
623
624 /* search for the algo in the backend in priority or the frontend */
Christopher Faulet92d36382015-11-05 13:35:03 +0100625 if ((s->be->comp && (comp_algo_back = s->be->comp->algos)) ||
626 (strm_fe(s)->comp && (comp_algo_back = strm_fe(s)->comp->algos))) {
Christopher Faulet3d97c902015-12-09 14:59:38 +0100627 int best_q = 0;
628
629 ctx.idx = 0;
Olivier Houchard0b662842018-06-29 18:16:31 +0200630 while (http_find_header2("Accept-Encoding", 15, ci_head(req), &txn->hdr_idx, &ctx)) {
Christopher Faulet3d97c902015-12-09 14:59:38 +0100631 const char *qval;
632 int q;
633 int toklen;
634
635 /* try to isolate the token from the optional q-value */
636 toklen = 0;
Willy Tarreau2235b262016-11-05 15:50:20 +0100637 while (toklen < ctx.vlen && HTTP_IS_TOKEN(*(ctx.line + ctx.val + toklen)))
Christopher Faulet3d97c902015-12-09 14:59:38 +0100638 toklen++;
639
640 qval = ctx.line + ctx.val + toklen;
641 while (1) {
Willy Tarreau2235b262016-11-05 15:50:20 +0100642 while (qval < ctx.line + ctx.val + ctx.vlen && HTTP_IS_LWS(*qval))
Christopher Faulet3d97c902015-12-09 14:59:38 +0100643 qval++;
644
645 if (qval >= ctx.line + ctx.val + ctx.vlen || *qval != ';') {
646 qval = NULL;
647 break;
648 }
649 qval++;
650
Willy Tarreau2235b262016-11-05 15:50:20 +0100651 while (qval < ctx.line + ctx.val + ctx.vlen && HTTP_IS_LWS(*qval))
Christopher Faulet3d97c902015-12-09 14:59:38 +0100652 qval++;
653
654 if (qval >= ctx.line + ctx.val + ctx.vlen) {
655 qval = NULL;
656 break;
657 }
658 if (strncmp(qval, "q=", MIN(ctx.line + ctx.val + ctx.vlen - qval, 2)) == 0)
659 break;
660
661 while (qval < ctx.line + ctx.val + ctx.vlen && *qval != ';')
662 qval++;
663 }
664
665 /* here we have qval pointing to the first "q=" attribute or NULL if not found */
Willy Tarreauab813a42018-09-10 18:41:28 +0200666 q = qval ? http_parse_qvalue(qval + 2, NULL) : 1000;
Christopher Faulet3d97c902015-12-09 14:59:38 +0100667
668 if (q <= best_q)
669 continue;
670
671 for (comp_algo = comp_algo_back; comp_algo; comp_algo = comp_algo->next) {
672 if (*(ctx.line + ctx.val) == '*' ||
673 word_match(ctx.line + ctx.val, toklen, comp_algo->ua_name, comp_algo->ua_name_len)) {
Christopher Faulet92d36382015-11-05 13:35:03 +0100674 st->comp_algo = comp_algo;
Christopher Faulet3d97c902015-12-09 14:59:38 +0100675 best_q = q;
676 break;
677 }
678 }
679 }
680 }
681
682 /* remove all occurrences of the header when "compression offload" is set */
Christopher Faulet92d36382015-11-05 13:35:03 +0100683 if (st->comp_algo) {
684 if ((s->be->comp && s->be->comp->offload) ||
685 (strm_fe(s)->comp && strm_fe(s)->comp->offload)) {
Christopher Faulet3d97c902015-12-09 14:59:38 +0100686 http_remove_header2(msg, &txn->hdr_idx, &ctx);
687 ctx.idx = 0;
Olivier Houchard0b662842018-06-29 18:16:31 +0200688 while (http_find_header2("Accept-Encoding", 15, ci_head(req), &txn->hdr_idx, &ctx)) {
Christopher Faulet3d97c902015-12-09 14:59:38 +0100689 http_remove_header2(msg, &txn->hdr_idx, &ctx);
690 }
691 }
Christopher Faulete6902cd2018-11-30 22:29:48 +0100692 return 1;
693 }
694
695 /* identity is implicit does not require headers */
696 if ((s->be->comp && (comp_algo_back = s->be->comp->algos)) ||
697 (strm_fe(s)->comp && (comp_algo_back = strm_fe(s)->comp->algos))) {
698 for (comp_algo = comp_algo_back; comp_algo; comp_algo = comp_algo->next) {
699 if (comp_algo->cfg_name_len == 8 && memcmp(comp_algo->cfg_name, "identity", 8) == 0) {
700 st->comp_algo = comp_algo;
701 return 1;
702 }
703 }
704 }
705
706 st->comp_algo = NULL;
707 return 0;
708}
709
710static int
711htx_select_comp_reqhdr(struct comp_state *st, struct stream *s, struct http_msg *msg)
712{
Christopher Faulet27ba2dc2018-12-05 11:53:24 +0100713 struct htx *htx = htxbuf(&msg->chn->buf);
Christopher Faulete6902cd2018-11-30 22:29:48 +0100714 struct http_hdr_ctx ctx;
715 struct comp_algo *comp_algo = NULL;
716 struct comp_algo *comp_algo_back = NULL;
717
718 /* Disable compression for older user agents announcing themselves as "Mozilla/4"
719 * unless they are known good (MSIE 6 with XP SP2, or MSIE 7 and later).
720 * See http://zoompf.com/2012/02/lose-the-wait-http-compression for more details.
721 */
722 ctx.blk = NULL;
723 if (http_find_header(htx, ist("User-Agent"), &ctx, 1) &&
724 ctx.value.len >= 9 &&
725 memcmp(ctx.value.ptr, "Mozilla/4", 9) == 0 &&
726 (ctx.value.len < 31 ||
727 memcmp(ctx.value.ptr + 25, "MSIE ", 5) != 0 ||
728 *(ctx.value.ptr + 30) < '6' ||
729 (*(ctx.value.ptr + 30) == '6' &&
730 (ctx.value.len < 54 || memcmp(ctx.value.ptr + 51, "SV1", 3) != 0)))) {
731 st->comp_algo = NULL;
732 return 0;
733 }
734
735 /* search for the algo in the backend in priority or the frontend */
736 if ((s->be->comp && (comp_algo_back = s->be->comp->algos)) ||
737 (strm_fe(s)->comp && (comp_algo_back = strm_fe(s)->comp->algos))) {
738 int best_q = 0;
739
740 ctx.blk = NULL;
741 while (http_find_header(htx, ist("Accept-Encoding"), &ctx, 0)) {
742 const char *qval;
743 int q;
744 int toklen;
745
746 /* try to isolate the token from the optional q-value */
747 toklen = 0;
748 while (toklen < ctx.value.len && HTTP_IS_TOKEN(*(ctx.value.ptr + toklen)))
749 toklen++;
750
751 qval = ctx.value.ptr + toklen;
752 while (1) {
753 while (qval < ctx.value.ptr + ctx.value.len && HTTP_IS_LWS(*qval))
754 qval++;
755
756 if (qval >= ctx.value.ptr + ctx.value.len || *qval != ';') {
757 qval = NULL;
758 break;
759 }
760 qval++;
761
762 while (qval < ctx.value.ptr + ctx.value.len && HTTP_IS_LWS(*qval))
763 qval++;
764
765 if (qval >= ctx.value.ptr + ctx.value.len) {
766 qval = NULL;
767 break;
768 }
769 if (strncmp(qval, "q=", MIN(ctx.value.ptr + ctx.value.len - qval, 2)) == 0)
770 break;
771
772 while (qval < ctx.value.ptr + ctx.value.len && *qval != ';')
773 qval++;
774 }
775
776 /* here we have qval pointing to the first "q=" attribute or NULL if not found */
777 q = qval ? http_parse_qvalue(qval + 2, NULL) : 1000;
778
779 if (q <= best_q)
780 continue;
781
782 for (comp_algo = comp_algo_back; comp_algo; comp_algo = comp_algo->next) {
783 if (*(ctx.value.ptr) == '*' ||
784 word_match(ctx.value.ptr, toklen, comp_algo->ua_name, comp_algo->ua_name_len)) {
785 st->comp_algo = comp_algo;
786 best_q = q;
787 break;
788 }
789 }
790 }
791 }
792
793 /* remove all occurrences of the header when "compression offload" is set */
794 if (st->comp_algo) {
795 if ((s->be->comp && s->be->comp->offload) ||
796 (strm_fe(s)->comp && strm_fe(s)->comp->offload)) {
797 http_remove_header(htx, &ctx);
798 ctx.blk = NULL;
799 while (http_find_header(htx, ist("Accept-Encoding"), &ctx, 1))
800 http_remove_header(htx, &ctx);
801 }
Christopher Faulet3d97c902015-12-09 14:59:38 +0100802 return 1;
803 }
804
805 /* identity is implicit does not require headers */
Christopher Faulet92d36382015-11-05 13:35:03 +0100806 if ((s->be->comp && (comp_algo_back = s->be->comp->algos)) ||
807 (strm_fe(s)->comp && (comp_algo_back = strm_fe(s)->comp->algos))) {
Christopher Faulet3d97c902015-12-09 14:59:38 +0100808 for (comp_algo = comp_algo_back; comp_algo; comp_algo = comp_algo->next) {
809 if (comp_algo->cfg_name_len == 8 && memcmp(comp_algo->cfg_name, "identity", 8) == 0) {
Christopher Faulet92d36382015-11-05 13:35:03 +0100810 st->comp_algo = comp_algo;
Christopher Faulet3d97c902015-12-09 14:59:38 +0100811 return 1;
812 }
813 }
814 }
815
Christopher Faulet92d36382015-11-05 13:35:03 +0100816 st->comp_algo = NULL;
Christopher Faulet3d97c902015-12-09 14:59:38 +0100817 return 0;
818}
819
Christopher Faulete6902cd2018-11-30 22:29:48 +0100820static int
821select_compression_request_header(struct comp_state *st, struct stream *s,
822 struct http_msg *msg)
823{
824 if (IS_HTX_STRM(s))
825 return htx_select_comp_reqhdr(st, s, msg);
826 else
827 return http_select_comp_reqhdr(st, s, msg);
828}
Christopher Faulet92d36382015-11-05 13:35:03 +0100829
Christopher Faulet3d97c902015-12-09 14:59:38 +0100830/*
831 * Selects a comression algorithm depending of the server response.
832 */
Christopher Faulet92d36382015-11-05 13:35:03 +0100833static int
Christopher Faulete6902cd2018-11-30 22:29:48 +0100834http_select_comp_reshdr(struct comp_state *st, struct stream *s, struct http_msg *msg)
Christopher Faulet3d97c902015-12-09 14:59:38 +0100835{
836 struct http_txn *txn = s->txn;
Olivier Houchard0b662842018-06-29 18:16:31 +0200837 struct channel *c = msg->chn;
Christopher Faulet3d97c902015-12-09 14:59:38 +0100838 struct hdr_ctx ctx;
839 struct comp_type *comp_type;
840
841 /* no common compression algorithm was found in request header */
Christopher Faulet92d36382015-11-05 13:35:03 +0100842 if (st->comp_algo == NULL)
Christopher Faulet3d97c902015-12-09 14:59:38 +0100843 goto fail;
844
Christopher Faulet1d3613a2019-01-07 14:41:59 +0100845 /* compression already in progress */
846 if (msg->flags & HTTP_MSGF_COMPRESSING)
847 goto fail;
848
Christopher Faulet3d97c902015-12-09 14:59:38 +0100849 /* HTTP < 1.1 should not be compressed */
850 if (!(msg->flags & HTTP_MSGF_VER_11) || !(txn->req.flags & HTTP_MSGF_VER_11))
851 goto fail;
852
Christopher Faulet92d36382015-11-05 13:35:03 +0100853 if (txn->meth == HTTP_METH_HEAD)
854 goto fail;
855
Christopher Faulet3d97c902015-12-09 14:59:38 +0100856 /* compress 200,201,202,203 responses only */
857 if ((txn->status != 200) &&
858 (txn->status != 201) &&
859 (txn->status != 202) &&
860 (txn->status != 203))
861 goto fail;
862
863
864 /* Content-Length is null */
865 if (!(msg->flags & HTTP_MSGF_TE_CHNK) && msg->body_len == 0)
866 goto fail;
867
868 /* content is already compressed */
869 ctx.idx = 0;
Olivier Houchard0b662842018-06-29 18:16:31 +0200870 if (http_find_header2("Content-Encoding", 16, ci_head(c), &txn->hdr_idx, &ctx))
Christopher Faulet3d97c902015-12-09 14:59:38 +0100871 goto fail;
872
873 /* no compression when Cache-Control: no-transform is present in the message */
874 ctx.idx = 0;
Olivier Houchard0b662842018-06-29 18:16:31 +0200875 while (http_find_header2("Cache-Control", 13, ci_head(c), &txn->hdr_idx, &ctx)) {
Christopher Faulet3d97c902015-12-09 14:59:38 +0100876 if (word_match(ctx.line + ctx.val, ctx.vlen, "no-transform", 12))
877 goto fail;
878 }
879
Tim Duesterhusb229f012019-01-29 16:38:56 +0100880 /* no compression when ETag is malformed */
881 ctx.idx = 0;
882 if (http_find_full_header2("ETag", 4, ci_head(c), &txn->hdr_idx, &ctx)) {
883 if (!(((ctx.vlen >= 4 && memcmp(ctx.line + ctx.val, "W/\"", 3) == 0) || /* Either a weak ETag */
884 (ctx.vlen >= 2 && ctx.line[ctx.val] == '"')) && /* or strong ETag */
885 ctx.line[ctx.val + ctx.vlen - 1] == '"')) {
886 goto fail;
887 }
888 }
889 /* no compression when multiple ETags are present
890 * Note: Do not reset ctx.idx!
891 */
892 if (http_find_full_header2("ETag", 4, ci_head(c), &txn->hdr_idx, &ctx))
893 goto fail;
894
Christopher Faulet3d97c902015-12-09 14:59:38 +0100895 comp_type = NULL;
896
897 /* we don't want to compress multipart content-types, nor content-types that are
898 * not listed in the "compression type" directive if any. If no content-type was
899 * found but configuration requires one, we don't compress either. Backend has
900 * the priority.
901 */
902 ctx.idx = 0;
Olivier Houchard0b662842018-06-29 18:16:31 +0200903 if (http_find_header2("Content-Type", 12, ci_head(c), &txn->hdr_idx, &ctx)) {
Christopher Faulet3d97c902015-12-09 14:59:38 +0100904 if (ctx.vlen >= 9 && strncasecmp("multipart", ctx.line+ctx.val, 9) == 0)
905 goto fail;
906
907 if ((s->be->comp && (comp_type = s->be->comp->types)) ||
908 (strm_fe(s)->comp && (comp_type = strm_fe(s)->comp->types))) {
909 for (; comp_type; comp_type = comp_type->next) {
910 if (ctx.vlen >= comp_type->name_len &&
911 strncasecmp(ctx.line+ctx.val, comp_type->name, comp_type->name_len) == 0)
912 /* this Content-Type should be compressed */
913 break;
914 }
915 /* this Content-Type should not be compressed */
916 if (comp_type == NULL)
917 goto fail;
918 }
919 }
920 else { /* no content-type header */
Christopher Faulet92d36382015-11-05 13:35:03 +0100921 if ((s->be->comp && s->be->comp->types) ||
922 (strm_fe(s)->comp && strm_fe(s)->comp->types))
Christopher Faulet3d97c902015-12-09 14:59:38 +0100923 goto fail; /* a content-type was required */
924 }
925
926 /* limit compression rate */
927 if (global.comp_rate_lim > 0)
928 if (read_freq_ctr(&global.comp_bps_in) > global.comp_rate_lim)
929 goto fail;
930
931 /* limit cpu usage */
932 if (idle_pct < compress_min_idle)
933 goto fail;
934
935 /* initialize compression */
Christopher Faulet92d36382015-11-05 13:35:03 +0100936 if (st->comp_algo->init(&st->comp_ctx, global.tune.comp_maxlevel) < 0)
Christopher Faulet3d97c902015-12-09 14:59:38 +0100937 goto fail;
Christopher Faulet92d36382015-11-05 13:35:03 +0100938 msg->flags |= HTTP_MSGF_COMPRESSING;
Christopher Faulet3d97c902015-12-09 14:59:38 +0100939 return 1;
940
941fail:
Christopher Faulet92d36382015-11-05 13:35:03 +0100942 st->comp_algo = NULL;
Christopher Faulet3d97c902015-12-09 14:59:38 +0100943 return 0;
944}
945
Christopher Faulete6902cd2018-11-30 22:29:48 +0100946static int
947htx_select_comp_reshdr(struct comp_state *st, struct stream *s, struct http_msg *msg)
948{
Christopher Faulet27ba2dc2018-12-05 11:53:24 +0100949 struct htx *htx = htxbuf(&msg->chn->buf);
Christopher Faulete6902cd2018-11-30 22:29:48 +0100950 struct http_txn *txn = s->txn;
951 struct http_hdr_ctx ctx;
952 struct comp_type *comp_type;
953
954 /* no common compression algorithm was found in request header */
955 if (st->comp_algo == NULL)
956 goto fail;
957
Christopher Faulet1d3613a2019-01-07 14:41:59 +0100958 /* compression already in progress */
959 if (msg->flags & HTTP_MSGF_COMPRESSING)
960 goto fail;
961
Christopher Faulete6902cd2018-11-30 22:29:48 +0100962 /* HTTP < 1.1 should not be compressed */
963 if (!(msg->flags & HTTP_MSGF_VER_11) || !(txn->req.flags & HTTP_MSGF_VER_11))
964 goto fail;
965
966 if (txn->meth == HTTP_METH_HEAD)
967 goto fail;
968
969 /* compress 200,201,202,203 responses only */
970 if ((txn->status != 200) &&
971 (txn->status != 201) &&
972 (txn->status != 202) &&
973 (txn->status != 203))
974 goto fail;
975
Christopher Fauletc963eb22018-12-21 14:53:54 +0100976 if (!(msg->flags & HTTP_MSGF_XFER_LEN) || msg->flags & HTTP_MSGF_BODYLESS)
Christopher Faulete6902cd2018-11-30 22:29:48 +0100977 goto fail;
978
979 /* content is already compressed */
980 ctx.blk = NULL;
981 if (http_find_header(htx, ist("Content-Encoding"), &ctx, 1))
982 goto fail;
983
984 /* no compression when Cache-Control: no-transform is present in the message */
985 ctx.blk = NULL;
986 while (http_find_header(htx, ist("Cache-Control"), &ctx, 0)) {
987 if (word_match(ctx.value.ptr, ctx.value.len, "no-transform", 12))
988 goto fail;
989 }
990
Tim Duesterhusb229f012019-01-29 16:38:56 +0100991 /* no compression when ETag is malformed */
992 ctx.blk = NULL;
993 if (http_find_header(htx, ist("ETag"), &ctx, 1)) {
994 if (!(((ctx.value.len >= 4 && memcmp(ctx.value.ptr, "W/\"", 3) == 0) || /* Either a weak ETag */
995 (ctx.value.len >= 2 && ctx.value.ptr[0] == '"')) && /* or strong ETag */
996 ctx.value.ptr[ctx.value.len - 1] == '"')) {
997 goto fail;
998 }
999 }
1000 /* no compression when multiple ETags are present
1001 * Note: Do not reset ctx.blk!
1002 */
1003 if (http_find_header(htx, ist("ETag"), &ctx, 1))
1004 goto fail;
1005
Christopher Faulete6902cd2018-11-30 22:29:48 +01001006 comp_type = NULL;
1007
1008 /* we don't want to compress multipart content-types, nor content-types that are
1009 * not listed in the "compression type" directive if any. If no content-type was
1010 * found but configuration requires one, we don't compress either. Backend has
1011 * the priority.
1012 */
1013 ctx.blk = NULL;
1014 if (http_find_header(htx, ist("Content-Type"), &ctx, 1)) {
1015 if (ctx.value.len >= 9 && strncasecmp("multipart", ctx.value.ptr, 9) == 0)
1016 goto fail;
1017
1018 if ((s->be->comp && (comp_type = s->be->comp->types)) ||
1019 (strm_fe(s)->comp && (comp_type = strm_fe(s)->comp->types))) {
1020 for (; comp_type; comp_type = comp_type->next) {
1021 if (ctx.value.len >= comp_type->name_len &&
1022 strncasecmp(ctx.value.ptr, comp_type->name, comp_type->name_len) == 0)
1023 /* this Content-Type should be compressed */
1024 break;
1025 }
1026 /* this Content-Type should not be compressed */
1027 if (comp_type == NULL)
1028 goto fail;
1029 }
1030 }
1031 else { /* no content-type header */
1032 if ((s->be->comp && s->be->comp->types) ||
1033 (strm_fe(s)->comp && strm_fe(s)->comp->types))
1034 goto fail; /* a content-type was required */
1035 }
1036
1037 /* limit compression rate */
1038 if (global.comp_rate_lim > 0)
1039 if (read_freq_ctr(&global.comp_bps_in) > global.comp_rate_lim)
1040 goto fail;
1041
1042 /* limit cpu usage */
1043 if (idle_pct < compress_min_idle)
1044 goto fail;
1045
1046 /* initialize compression */
1047 if (st->comp_algo->init(&st->comp_ctx, global.tune.comp_maxlevel) < 0)
1048 goto fail;
Christopher Faulete6902cd2018-11-30 22:29:48 +01001049 msg->flags |= HTTP_MSGF_COMPRESSING;
1050 return 1;
1051
1052 deinit_comp_ctx:
1053 st->comp_algo->end(&st->comp_ctx);
1054 fail:
1055 st->comp_algo = NULL;
1056 return 0;
1057}
1058
1059static int
1060select_compression_response_header(struct comp_state *st, struct stream *s, struct http_msg *msg)
1061{
1062 if (IS_HTX_STRM(s))
1063 return htx_select_comp_reshdr(st, s, msg);
1064 else
1065 return http_select_comp_reshdr(st, s, msg);
1066}
Christopher Faulet3d97c902015-12-09 14:59:38 +01001067/***********************************************************************/
1068/* emit the chunksize followed by a CRLF on the output and return the number of
1069 * bytes written. It goes backwards and starts with the byte before <end>. It
1070 * returns the number of bytes written which will not exceed 10 (8 digits, CR,
1071 * and LF). The caller is responsible for ensuring there is enough room left in
1072 * the output buffer for the string.
1073 */
1074static int
1075http_emit_chunk_size(char *end, unsigned int chksz)
1076{
1077 char *beg = end;
1078
1079 *--beg = '\n';
1080 *--beg = '\r';
1081 do {
1082 *--beg = hextab[chksz & 0xF];
1083 } while (chksz >>= 4);
1084 return end - beg;
1085}
1086
1087/*
1088 * Init HTTP compression
1089 */
Christopher Faulet92d36382015-11-05 13:35:03 +01001090static int
Christopher Fauletb61481c2018-12-17 13:17:53 +01001091http_compression_buffer_init(struct channel *inc, struct buffer *out)
Christopher Faulet3d97c902015-12-09 14:59:38 +01001092{
1093 /* output stream requires at least 10 bytes for the gzip header, plus
1094 * at least 8 bytes for the gzip trailer (crc+len), plus a possible
1095 * plus at most 5 bytes per 32kB block and 2 bytes to close the stream.
1096 */
Olivier Houchard0b662842018-06-29 18:16:31 +02001097 if (c_room(inc) < 20 + 5 * ((ci_data(inc) + 32767) >> 15))
Christopher Faulet3d97c902015-12-09 14:59:38 +01001098 return -1;
1099
1100 /* prepare an empty output buffer in which we reserve enough room for
1101 * copying the output bytes from <in>, plus 10 extra bytes to write
1102 * the chunk size. We don't copy the bytes yet so that if we have to
1103 * cancel the operation later, it's cheap.
1104 */
1105 b_reset(out);
Christopher Fauletb61481c2018-12-17 13:17:53 +01001106 out->head += co_data(inc) + 10;
Christopher Faulet3d97c902015-12-09 14:59:38 +01001107 return 0;
1108}
1109
Christopher Faulete6902cd2018-11-30 22:29:48 +01001110static int
1111htx_compression_buffer_init(struct htx *htx, struct buffer *out)
1112{
1113 /* output stream requires at least 10 bytes for the gzip header, plus
1114 * at least 8 bytes for the gzip trailer (crc+len), plus a possible
1115 * plus at most 5 bytes per 32kB block and 2 bytes to close the stream.
1116 */
1117 if (htx_free_space(htx) < 20 + 5 * ((htx->data + 32767) >> 15))
1118 return -1;
1119 b_reset(out);
1120 return 0;
1121}
1122
Christopher Faulet3d97c902015-12-09 14:59:38 +01001123/*
1124 * Add data to compress
1125 */
Christopher Faulet92d36382015-11-05 13:35:03 +01001126static int
1127http_compression_buffer_add_data(struct comp_state *st, struct buffer *in,
Willy Tarreaud54a8ce2018-06-29 18:42:02 +02001128 int in_out, struct buffer *out, int sz)
Christopher Faulet3d97c902015-12-09 14:59:38 +01001129{
Christopher Faulet3d97c902015-12-09 14:59:38 +01001130 int consumed_data = 0;
1131 int data_process_len;
1132 int block1, block2;
1133
Christopher Faulet92d36382015-11-05 13:35:03 +01001134 if (!sz)
Christopher Faulet3e7bc672015-12-07 13:39:08 +01001135 goto end;
Christopher Faulet3d97c902015-12-09 14:59:38 +01001136
Christopher Faulet92d36382015-11-05 13:35:03 +01001137 /* select the smallest size between the announced chunk size, the input
Christopher Faulet3d97c902015-12-09 14:59:38 +01001138 * data, and the available output buffer size. The compressors are
Christopher Faulet92d36382015-11-05 13:35:03 +01001139 * assumed to be able to process all the bytes we pass to them at
1140 * once. */
Willy Tarreaueac52592018-06-15 13:59:36 +02001141 data_process_len = MIN(b_room(out), sz);
Christopher Faulet92d36382015-11-05 13:35:03 +01001142
Christopher Faulet3d97c902015-12-09 14:59:38 +01001143 block1 = data_process_len;
Willy Tarreaud54a8ce2018-06-29 18:42:02 +02001144 if (block1 > b_contig_data(in, in_out))
1145 block1 = b_contig_data(in, in_out);
Christopher Faulet3d97c902015-12-09 14:59:38 +01001146 block2 = data_process_len - block1;
1147
1148 /* compressors return < 0 upon error or the amount of bytes read */
Christopher Faulet96667202018-12-17 12:02:57 +01001149 consumed_data = st->comp_algo->add_data(st->comp_ctx, b_peek(in, in_out), block1, out);
Christopher Faulet3e7bc672015-12-07 13:39:08 +01001150 if (consumed_data != block1 || !block2)
1151 goto end;
Christopher Faulet96667202018-12-17 12:02:57 +01001152 consumed_data = st->comp_algo->add_data(st->comp_ctx, b_orig(in), block2, out);
Christopher Faulet3e7bc672015-12-07 13:39:08 +01001153 if (consumed_data < 0)
1154 goto end;
1155 consumed_data += block1;
1156
1157 end:
Christopher Faulet3d97c902015-12-09 14:59:38 +01001158 return consumed_data;
1159}
1160
Christopher Faulete6902cd2018-11-30 22:29:48 +01001161static int
1162htx_compression_buffer_add_data(struct comp_state *st, const char *data, size_t len,
1163 struct buffer *out)
1164{
1165 return st->comp_algo->add_data(st->comp_ctx, data, len, out);
1166}
1167
Christopher Faulet3d97c902015-12-09 14:59:38 +01001168/*
1169 * Flush data in process, and write the header and footer of the chunk. Upon
1170 * success, in and out buffers are swapped to avoid a copy.
1171 */
Christopher Faulet92d36382015-11-05 13:35:03 +01001172static int
1173http_compression_buffer_end(struct comp_state *st, struct stream *s,
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001174 struct channel *chn, struct buffer *out,
Christopher Fauletb61481c2018-12-17 13:17:53 +01001175 int end)
Christopher Faulet3d97c902015-12-09 14:59:38 +01001176{
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001177 struct buffer tmp_buf;
Christopher Faulet3d97c902015-12-09 14:59:38 +01001178 char *tail;
Christopher Faulet92d36382015-11-05 13:35:03 +01001179 int to_forward, left;
Christopher Faulet3d97c902015-12-09 14:59:38 +01001180
1181#if defined(USE_SLZ) || defined(USE_ZLIB)
1182 int ret;
1183
1184 /* flush data here */
Christopher Faulet3d97c902015-12-09 14:59:38 +01001185 if (end)
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001186 ret = st->comp_algo->finish(st->comp_ctx, out); /* end of data */
Christopher Faulet3d97c902015-12-09 14:59:38 +01001187 else
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001188 ret = st->comp_algo->flush(st->comp_ctx, out); /* end of buffer */
Christopher Faulet3d97c902015-12-09 14:59:38 +01001189
1190 if (ret < 0)
1191 return -1; /* flush failed */
1192
1193#endif /* USE_ZLIB */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001194 if (b_data(out) == 0) {
Christopher Faulet3d97c902015-12-09 14:59:38 +01001195 /* No data were appended, let's drop the output buffer and
1196 * keep the input buffer unchanged.
1197 */
1198 return 0;
1199 }
1200
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001201 /* OK so at this stage, we have an output buffer <out> looking like this :
Christopher Faulet3d97c902015-12-09 14:59:38 +01001202 *
1203 * <-- o --> <------ i ----->
1204 * +---------+---+------------+-----------+
1205 * | out | c | comp_in | empty |
1206 * +---------+---+------------+-----------+
1207 * data p size
1208 *
Willy Tarreaud54a8ce2018-06-29 18:42:02 +02001209 * <out> is the room reserved to copy the channel output. It starts at
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001210 * out->area and has not yet been filled. <c> is the room reserved to
Willy Tarreaud54a8ce2018-06-29 18:42:02 +02001211 * write the chunk size (10 bytes). <comp_in> is the compressed
1212 * equivalent of the data part of ib->len. <empty> is the amount of
1213 * empty bytes at the end of the buffer, into which we may have to
1214 * copy the remaining bytes from ib->len after the data
1215 * (chunk size, trailers, ...).
Christopher Faulet3d97c902015-12-09 14:59:38 +01001216 */
1217
Joseph Herlant942eea32018-11-15 13:57:22 -08001218 /* Write real size at the beginning of the chunk, no need of wrapping.
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001219 * We write the chunk using a dynamic length and adjust out->p and out->i
Christopher Faulet3d97c902015-12-09 14:59:38 +01001220 * accordingly afterwards. That will move <out> away from <data>.
1221 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001222 left = http_emit_chunk_size(b_head(out), b_data(out));
1223 b_add(out, left);
Christopher Fauletb61481c2018-12-17 13:17:53 +01001224 out->head -= co_data(chn) + (left);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001225 /* Copy previous data from chn into out */
Willy Tarreaud54a8ce2018-06-29 18:42:02 +02001226 if (co_data(chn) > 0) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001227 left = b_contig_data(&chn->buf, 0);
Christopher Fauletb61481c2018-12-17 13:17:53 +01001228 if (left > co_data(chn))
1229 left = co_data(chn);
Christopher Faulet3d97c902015-12-09 14:59:38 +01001230
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001231 memcpy(b_head(out), co_head(chn), left);
1232 b_add(out, left);
Willy Tarreaud54a8ce2018-06-29 18:42:02 +02001233 if (co_data(chn) - left) {/* second part of the buffer */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001234 memcpy(b_head(out) + left, b_orig(&chn->buf), co_data(chn) - left);
1235 b_add(out, co_data(chn) - left);
Willy Tarreaud54a8ce2018-06-29 18:42:02 +02001236 }
Christopher Faulet3d97c902015-12-09 14:59:38 +01001237 }
1238
1239 /* chunked encoding requires CRLF after data */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001240 tail = b_tail(out);
Christopher Faulet3d97c902015-12-09 14:59:38 +01001241 *tail++ = '\r';
1242 *tail++ = '\n';
1243
Christopher Faulet2fb28802015-12-01 10:40:57 +01001244 /* At the end of data, we must write the empty chunk 0<CRLF>,
1245 * and terminate the trailers section with a last <CRLF>. If
1246 * we're forwarding a chunked-encoded response, we'll have a
1247 * trailers section after the empty chunk which needs to be
1248 * forwarded and which will provide the last CRLF. Otherwise
1249 * we write it ourselves.
1250 */
1251 if (end) {
1252 struct http_msg *msg = &s->txn->rsp;
1253
1254 memcpy(tail, "0\r\n", 3);
1255 tail += 3;
Christopher Fauletb77c5c22015-12-07 16:48:42 +01001256 if (!(msg->flags & HTTP_MSGF_TE_CHNK)) {
Christopher Faulet2fb28802015-12-01 10:40:57 +01001257 memcpy(tail, "\r\n", 2);
1258 tail += 2;
1259 }
1260 }
1261
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001262 b_add(out, tail - b_tail(out));
Christopher Fauletb61481c2018-12-17 13:17:53 +01001263 to_forward = b_data(out) - co_data(chn);
Christopher Faulet3d97c902015-12-09 14:59:38 +01001264
1265 /* update input rate */
Christopher Faulet92d36382015-11-05 13:35:03 +01001266 if (st->comp_ctx && st->comp_ctx->cur_lvl > 0) {
Christopher Faulet2fb28802015-12-01 10:40:57 +01001267 update_freq_ctr(&global.comp_bps_in, st->consumed);
Olivier Houchard43da3432019-03-08 18:50:27 +01001268 _HA_ATOMIC_ADD(&strm_fe(s)->fe_counters.comp_in, st->consumed);
1269 _HA_ATOMIC_ADD(&s->be->be_counters.comp_in, st->consumed);
Christopher Faulet3d97c902015-12-09 14:59:38 +01001270 } else {
Olivier Houchard43da3432019-03-08 18:50:27 +01001271 _HA_ATOMIC_ADD(&strm_fe(s)->fe_counters.comp_byp, st->consumed);
1272 _HA_ATOMIC_ADD(&s->be->be_counters.comp_byp, st->consumed);
Christopher Faulet3d97c902015-12-09 14:59:38 +01001273 }
1274
1275 /* copy the remaining data in the tmp buffer. */
Willy Tarreaubcbd3932018-06-06 07:13:22 +02001276 c_adv(chn, st->consumed);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001277 if (b_data(&chn->buf) - co_data(chn) > 0) {
Willy Tarreau7194d3c2018-06-06 16:55:45 +02001278 left = ci_contig_data(chn);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001279 memcpy(b_tail(out), ci_head(chn), left);
1280 b_add(out, left);
1281 if (b_data(&chn->buf) - (co_data(chn) + left)) {
1282 memcpy(b_tail(out), b_orig(&chn->buf), b_data(&chn->buf) - left);
1283 b_add(out, b_data(&chn->buf) - left);
Christopher Faulet3d97c902015-12-09 14:59:38 +01001284 }
1285 }
Christopher Fauletb61481c2018-12-17 13:17:53 +01001286 c_rew(chn, st->consumed);
1287
Christopher Faulet3d97c902015-12-09 14:59:38 +01001288 /* swap the buffers */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001289 tmp_buf = chn->buf;
1290 chn->buf = *out;
1291 *out = tmp_buf;
1292
Christopher Faulet92d36382015-11-05 13:35:03 +01001293 if (st->comp_ctx && st->comp_ctx->cur_lvl > 0) {
Christopher Faulet3d97c902015-12-09 14:59:38 +01001294 update_freq_ctr(&global.comp_bps_out, to_forward);
Olivier Houchard43da3432019-03-08 18:50:27 +01001295 _HA_ATOMIC_ADD(&strm_fe(s)->fe_counters.comp_out, to_forward);
1296 _HA_ATOMIC_ADD(&s->be->be_counters.comp_out, to_forward);
Christopher Faulet3d97c902015-12-09 14:59:38 +01001297 }
1298
Christopher Faulet3d97c902015-12-09 14:59:38 +01001299 return to_forward;
1300}
1301
Christopher Faulete6902cd2018-11-30 22:29:48 +01001302static int
1303htx_compression_buffer_end(struct comp_state *st, struct buffer *out, int end)
1304{
1305 if (end)
1306 return st->comp_algo->finish(st->comp_ctx, out);
1307 else
1308 return st->comp_algo->flush(st->comp_ctx, out);
1309}
1310
Christopher Faulet3d97c902015-12-09 14:59:38 +01001311
1312/***********************************************************************/
Christopher Faulet92d36382015-11-05 13:35:03 +01001313struct flt_ops comp_ops = {
Christopher Faulete6902cd2018-11-30 22:29:48 +01001314 .init = comp_flt_init,
Christopher Faulet8ca3b4b2017-07-25 11:07:15 +02001315 .init_per_thread = comp_flt_init_per_thread,
1316 .deinit_per_thread = comp_flt_deinit_per_thread,
Christopher Faulet92d36382015-11-05 13:35:03 +01001317
1318 .channel_start_analyze = comp_start_analyze,
Christopher Faulet92d36382015-11-05 13:35:03 +01001319 .channel_end_analyze = comp_end_analyze,
Christopher Faulet3dc860d2017-09-15 11:39:36 +02001320 .channel_post_analyze = comp_http_post_analyze,
Christopher Faulet92d36382015-11-05 13:35:03 +01001321
Christopher Faulet1339d742016-05-11 16:48:33 +02001322 .http_headers = comp_http_headers,
Christopher Faulete6902cd2018-11-30 22:29:48 +01001323 .http_payload = comp_http_payload,
1324 .http_end = comp_http_end,
1325
Christopher Faulet309c6412015-12-02 09:57:32 +01001326 .http_data = comp_http_data,
1327 .http_chunk_trailers = comp_http_chunk_trailers,
1328 .http_forward_data = comp_http_forward_data,
Christopher Faulet92d36382015-11-05 13:35:03 +01001329};
1330
Christopher Faulet3d97c902015-12-09 14:59:38 +01001331static int
1332parse_compression_options(char **args, int section, struct proxy *proxy,
1333 struct proxy *defpx, const char *file, int line,
1334 char **err)
1335{
Christopher Faulet92d36382015-11-05 13:35:03 +01001336 struct comp *comp;
Christopher Faulet3d97c902015-12-09 14:59:38 +01001337
1338 if (proxy->comp == NULL) {
Vincent Bernat02779b62016-04-03 13:48:43 +02001339 comp = calloc(1, sizeof(*comp));
Christopher Faulet3d97c902015-12-09 14:59:38 +01001340 proxy->comp = comp;
1341 }
1342 else
1343 comp = proxy->comp;
1344
1345 if (!strcmp(args[1], "algo")) {
1346 struct comp_ctx *ctx;
1347 int cur_arg = 2;
1348
1349 if (!*args[cur_arg]) {
1350 memprintf(err, "parsing [%s:%d] : '%s' expects <algorithm>\n",
1351 file, line, args[0]);
1352 return -1;
1353 }
1354 while (*(args[cur_arg])) {
1355 if (comp_append_algo(comp, args[cur_arg]) < 0) {
1356 memprintf(err, "'%s' : '%s' is not a supported algorithm.\n",
1357 args[0], args[cur_arg]);
1358 return -1;
1359 }
1360 if (proxy->comp->algos->init(&ctx, 9) == 0)
1361 proxy->comp->algos->end(&ctx);
1362 else {
1363 memprintf(err, "'%s' : Can't init '%s' algorithm.\n",
1364 args[0], args[cur_arg]);
1365 return -1;
1366 }
1367 cur_arg++;
1368 continue;
1369 }
1370 }
1371 else if (!strcmp(args[1], "offload"))
1372 comp->offload = 1;
1373 else if (!strcmp(args[1], "type")) {
1374 int cur_arg = 2;
1375
1376 if (!*args[cur_arg]) {
1377 memprintf(err, "'%s' expects <type>\n", args[0]);
1378 return -1;
1379 }
1380 while (*(args[cur_arg])) {
1381 comp_append_type(comp, args[cur_arg]);
1382 cur_arg++;
1383 continue;
1384 }
1385 }
1386 else {
1387 memprintf(err, "'%s' expects 'algo', 'type' or 'offload'\n",
1388 args[0]);
1389 return -1;
1390 }
1391
1392 return 0;
1393}
1394
Christopher Faulet92d36382015-11-05 13:35:03 +01001395static int
1396parse_http_comp_flt(char **args, int *cur_arg, struct proxy *px,
Thierry Fournier3610c392016-04-13 18:27:51 +02001397 struct flt_conf *fconf, char **err, void *private)
Christopher Faulet92d36382015-11-05 13:35:03 +01001398{
Christopher Faulet443ea1a2016-02-04 13:40:26 +01001399 struct flt_conf *fc, *back;
Christopher Faulet92d36382015-11-05 13:35:03 +01001400
Christopher Faulet443ea1a2016-02-04 13:40:26 +01001401 list_for_each_entry_safe(fc, back, &px->filter_configs, list) {
1402 if (fc->id == http_comp_flt_id) {
Christopher Faulet92d36382015-11-05 13:35:03 +01001403 memprintf(err, "%s: Proxy supports only one compression filter\n", px->id);
1404 return -1;
1405 }
1406 }
1407
Christopher Faulet443ea1a2016-02-04 13:40:26 +01001408 fconf->id = http_comp_flt_id;
1409 fconf->conf = NULL;
1410 fconf->ops = &comp_ops;
Christopher Faulet92d36382015-11-05 13:35:03 +01001411 (*cur_arg)++;
1412
1413 return 0;
1414}
1415
1416
1417int
Christopher Fauletc9df7f72018-12-10 16:14:04 +01001418check_implicit_http_comp_flt(struct proxy *proxy)
Christopher Faulet92d36382015-11-05 13:35:03 +01001419{
Christopher Faulet443ea1a2016-02-04 13:40:26 +01001420 struct flt_conf *fconf;
Christopher Faulet27d93c32018-12-15 22:32:02 +01001421 int explicit = 0;
1422 int comp = 0;
Christopher Faulet92d36382015-11-05 13:35:03 +01001423 int err = 0;
1424
1425 if (proxy->comp == NULL)
1426 goto end;
Christopher Faulet443ea1a2016-02-04 13:40:26 +01001427 if (!LIST_ISEMPTY(&proxy->filter_configs)) {
1428 list_for_each_entry(fconf, &proxy->filter_configs, list) {
1429 if (fconf->id == http_comp_flt_id)
Christopher Faulet27d93c32018-12-15 22:32:02 +01001430 comp = 1;
1431 else if (fconf->id == cache_store_flt_id) {
1432 if (comp) {
1433 ha_alert("config: %s '%s': unable to enable the compression filter "
1434 "before any cache filter.\n",
1435 proxy_type_str(proxy), proxy->id);
1436 err++;
1437 goto end;
1438 }
1439 }
1440 else
1441 explicit = 1;
Christopher Faulet92d36382015-11-05 13:35:03 +01001442 }
Christopher Faulet27d93c32018-12-15 22:32:02 +01001443 }
1444 if (comp)
1445 goto end;
1446 else if (explicit) {
1447 ha_alert("config: %s '%s': require an explicit filter declaration to use "
1448 "HTTP compression\n", proxy_type_str(proxy), proxy->id);
Christopher Faulet92d36382015-11-05 13:35:03 +01001449 err++;
1450 goto end;
1451 }
1452
Christopher Faulet27d93c32018-12-15 22:32:02 +01001453 /* Implicit declaration of the compression filter is always the last
1454 * one */
Christopher Faulet443ea1a2016-02-04 13:40:26 +01001455 fconf = calloc(1, sizeof(*fconf));
1456 if (!fconf) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001457 ha_alert("config: %s '%s': out of memory\n",
1458 proxy_type_str(proxy), proxy->id);
Christopher Faulet92d36382015-11-05 13:35:03 +01001459 err++;
1460 goto end;
1461 }
Christopher Faulet443ea1a2016-02-04 13:40:26 +01001462 fconf->id = http_comp_flt_id;
1463 fconf->conf = NULL;
1464 fconf->ops = &comp_ops;
1465 LIST_ADDQ(&proxy->filter_configs, &fconf->list);
Christopher Faulet92d36382015-11-05 13:35:03 +01001466 end:
1467 return err;
1468}
1469
1470/*
1471 * boolean, returns true if compression is used (either gzip or deflate) in the
1472 * response.
1473 */
Christopher Faulet3d97c902015-12-09 14:59:38 +01001474static int
Christopher Faulet92d36382015-11-05 13:35:03 +01001475smp_fetch_res_comp(const struct arg *args, struct sample *smp, const char *kw,
1476 void *private)
Christopher Faulet3d97c902015-12-09 14:59:38 +01001477{
Willy Tarreaube508f12016-03-10 11:47:01 +01001478 struct http_txn *txn = smp->strm ? smp->strm->txn : NULL;
Christopher Faulet92d36382015-11-05 13:35:03 +01001479
Christopher Faulet3d97c902015-12-09 14:59:38 +01001480 smp->data.type = SMP_T_BOOL;
Christopher Faulet92d36382015-11-05 13:35:03 +01001481 smp->data.u.sint = (txn && (txn->rsp.flags & HTTP_MSGF_COMPRESSING));
Christopher Faulet3d97c902015-12-09 14:59:38 +01001482 return 1;
1483}
1484
Christopher Faulet92d36382015-11-05 13:35:03 +01001485/*
1486 * string, returns algo
1487 */
Christopher Faulet3d97c902015-12-09 14:59:38 +01001488static int
Christopher Faulet92d36382015-11-05 13:35:03 +01001489smp_fetch_res_comp_algo(const struct arg *args, struct sample *smp,
1490 const char *kw, void *private)
Christopher Faulet3d97c902015-12-09 14:59:38 +01001491{
Willy Tarreaube508f12016-03-10 11:47:01 +01001492 struct http_txn *txn = smp->strm ? smp->strm->txn : NULL;
Christopher Faulet92d36382015-11-05 13:35:03 +01001493 struct filter *filter;
1494 struct comp_state *st;
1495
Christopher Faulet03d85532017-09-15 10:14:43 +02001496 if (!txn || !(txn->rsp.flags & HTTP_MSGF_COMPRESSING))
Christopher Faulet3d97c902015-12-09 14:59:38 +01001497 return 0;
1498
Christopher Fauletfcf035c2015-12-03 11:48:03 +01001499 list_for_each_entry(filter, &strm_flt(smp->strm)->filters, list) {
Christopher Faulet443ea1a2016-02-04 13:40:26 +01001500 if (FLT_ID(filter) != http_comp_flt_id)
Christopher Faulet92d36382015-11-05 13:35:03 +01001501 continue;
1502
1503 if (!(st = filter->ctx))
1504 break;
1505
1506 smp->data.type = SMP_T_STR;
1507 smp->flags = SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001508 smp->data.u.str.area = st->comp_algo->cfg_name;
1509 smp->data.u.str.data = st->comp_algo->cfg_name_len;
Christopher Faulet92d36382015-11-05 13:35:03 +01001510 return 1;
1511 }
1512 return 0;
Christopher Faulet3d97c902015-12-09 14:59:38 +01001513}
1514
1515/* Declare the config parser for "compression" keyword */
1516static struct cfg_kw_list cfg_kws = {ILH, {
1517 { CFG_LISTEN, "compression", parse_compression_options },
1518 { 0, NULL, NULL },
1519 }
1520};
1521
Willy Tarreau0108d902018-11-25 19:14:37 +01001522INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
1523
Christopher Faulet92d36382015-11-05 13:35:03 +01001524/* Declare the filter parser for "compression" keyword */
1525static struct flt_kw_list filter_kws = { "COMP", { }, {
Thierry Fournier3610c392016-04-13 18:27:51 +02001526 { "compression", parse_http_comp_flt, NULL },
1527 { NULL, NULL, NULL },
Christopher Faulet92d36382015-11-05 13:35:03 +01001528 }
1529};
1530
Willy Tarreau0108d902018-11-25 19:14:37 +01001531INITCALL1(STG_REGISTER, flt_register_keywords, &filter_kws);
1532
Christopher Faulet3d97c902015-12-09 14:59:38 +01001533/* Note: must not be declared <const> as its list will be overwritten */
1534static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
Christopher Faulet92d36382015-11-05 13:35:03 +01001535 { "res.comp", smp_fetch_res_comp, 0, NULL, SMP_T_BOOL, SMP_USE_HRSHP },
1536 { "res.comp_algo", smp_fetch_res_comp_algo, 0, NULL, SMP_T_STR, SMP_USE_HRSHP },
1537 { /* END */ },
1538 }
1539};
Christopher Faulet3d97c902015-12-09 14:59:38 +01001540
Willy Tarreau0108d902018-11-25 19:14:37 +01001541INITCALL1(STG_REGISTER, sample_register_fetches, &sample_fetch_keywords);