blob: b04dcd145f997d6fc126bd22914b120c092503f7 [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;
Christopher Faulete6902cd2018-11-30 22:29:48 +0100205 int ret, consumed = 0, to_forward = 0;
206
Christopher Fauletee847d42019-05-23 11:55:33 +0200207 for (blk = htx_get_first_blk(htx); blk && len; blk = htx_get_next_blk(htx, blk)) {
Christopher Faulete6902cd2018-11-30 22:29:48 +0100208 enum htx_blk_type type = htx_get_blk_type(blk);
209 uint32_t sz = htx_get_blksz(blk);
210 struct ist v;
211
Christopher Fauletee847d42019-05-23 11:55:33 +0200212 if (offset >= sz) {
213 offset -= sz;
214 continue;
215 }
216
Christopher Faulete6902cd2018-11-30 22:29:48 +0100217 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
Christopher Faulete6902cd2018-11-30 22:29:48 +0100248 case HTX_BLK_TLR:
Christopher Faulet2d7c5392019-06-03 10:41:26 +0200249 case HTX_BLK_EOT:
Christopher Faulete6902cd2018-11-30 22:29:48 +0100250 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)) {
Christopher Faulet86bc8df2019-06-11 10:38:38 +0200259 struct htx_blk *last = htx_add_last_data(htx, ist2(b_head(&trash), b_data(&trash)));
260 if (!last)
261 goto error;
262 blk = htx_get_next_blk(htx, last);
Christopher Fauletd238ae32018-12-21 15:10:25 +0100263 if (!blk)
264 goto error;
265 to_forward += b_data(&trash);
266 }
Christopher Faulete6902cd2018-11-30 22:29:48 +0100267 msg->flags &= ~HTTP_MSGF_COMPRESSING;
268 /* We let the mux add last empty chunk and empty trailers */
269 }
270 /* fall through */
271
272 default:
273 sz -= offset;
274 if (sz > len)
275 sz = len;
276 consumed += sz;
277 to_forward += sz;
278 len -= sz;
279 break;
280 }
281
282 offset = 0;
Christopher Faulete6902cd2018-11-30 22:29:48 +0100283 }
284
285 end:
286 if (to_forward != consumed)
287 flt_update_offsets(filter, msg->chn, to_forward - consumed);
288
289 if (st->comp_ctx && st->comp_ctx->cur_lvl > 0) {
Willy Tarreauef6fd852019-02-04 11:48:03 +0100290 update_freq_ctr(&global.comp_bps_in, consumed);
Olivier Houchard43da3432019-03-08 18:50:27 +0100291 _HA_ATOMIC_ADD(&strm_fe(s)->fe_counters.comp_in, consumed);
292 _HA_ATOMIC_ADD(&s->be->be_counters.comp_in, consumed);
Christopher Faulete6902cd2018-11-30 22:29:48 +0100293 update_freq_ctr(&global.comp_bps_out, to_forward);
Olivier Houchard43da3432019-03-08 18:50:27 +0100294 _HA_ATOMIC_ADD(&strm_fe(s)->fe_counters.comp_out, to_forward);
295 _HA_ATOMIC_ADD(&s->be->be_counters.comp_out, to_forward);
Willy Tarreauef6fd852019-02-04 11:48:03 +0100296 } else {
Olivier Houchard43da3432019-03-08 18:50:27 +0100297 _HA_ATOMIC_ADD(&strm_fe(s)->fe_counters.comp_byp, consumed);
298 _HA_ATOMIC_ADD(&s->be->be_counters.comp_byp, consumed);
Christopher Faulete6902cd2018-11-30 22:29:48 +0100299 }
300 return to_forward;
301
302 error:
303 return -1;
304}
305
306static int
Christopher Faulet2fb28802015-12-01 10:40:57 +0100307comp_http_data(struct stream *s, struct filter *filter, struct http_msg *msg)
Christopher Faulet92d36382015-11-05 13:35:03 +0100308{
309 struct comp_state *st = filter->ctx;
Willy Tarreaubcbd3932018-06-06 07:13:22 +0200310 struct channel *chn = msg->chn;
Christopher Faulet3e7bc672015-12-07 13:39:08 +0100311 unsigned int *nxt = &flt_rsp_nxt(filter);
Christopher Faulet2fb28802015-12-01 10:40:57 +0100312 unsigned int len;
Christopher Faulet92d36382015-11-05 13:35:03 +0100313 int ret;
314
Olivier Houchard0b662842018-06-29 18:16:31 +0200315 len = MIN(msg->chunk_len + msg->next, ci_data(chn)) - *nxt;
Christopher Faulet2fb28802015-12-01 10:40:57 +0100316 if (!len)
317 return len;
318
319 if (!st->initialized) {
Christopher Fauletb77c5c22015-12-07 16:48:42 +0100320 unsigned int fwd = flt_rsp_fwd(filter) + st->hdrs_len;
Christopher Faulet3e7bc672015-12-07 13:39:08 +0100321
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200322 b_reset(&tmpbuf);
Willy Tarreaubcbd3932018-06-06 07:13:22 +0200323 c_adv(chn, fwd);
Christopher Fauletb61481c2018-12-17 13:17:53 +0100324 ret = http_compression_buffer_init(chn, &zbuf);
Willy Tarreaubcbd3932018-06-06 07:13:22 +0200325 c_rew(chn, fwd);
Christopher Faulet2fb28802015-12-01 10:40:57 +0100326 if (ret < 0) {
327 msg->chn->flags |= CF_WAKE_WRITE;
328 return 0;
329 }
330 }
Christopher Fauletb77c5c22015-12-07 16:48:42 +0100331
332 if (msg->flags & HTTP_MSGF_TE_CHNK) {
Christopher Faulet06ecf3a2016-09-22 15:31:43 +0200333 int block;
Christopher Fauletb77c5c22015-12-07 16:48:42 +0100334
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200335 len = MIN(b_room(&tmpbuf), len);
Christopher Faulet06ecf3a2016-09-22 15:31:43 +0200336
Willy Tarreaubcbd3932018-06-06 07:13:22 +0200337 c_adv(chn, *nxt);
Willy Tarreau7194d3c2018-06-06 16:55:45 +0200338 block = ci_contig_data(chn);
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200339 memcpy(b_tail(&tmpbuf), ci_head(chn), block);
Christopher Faulet06ecf3a2016-09-22 15:31:43 +0200340 if (len > block)
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200341 memcpy(b_tail(&tmpbuf)+block, b_orig(&chn->buf), len-block);
Willy Tarreaubcbd3932018-06-06 07:13:22 +0200342 c_rew(chn, *nxt);
Christopher Faulet06ecf3a2016-09-22 15:31:43 +0200343
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200344 b_add(&tmpbuf, len);
Christopher Fauletb77c5c22015-12-07 16:48:42 +0100345 ret = len;
346 }
347 else {
Willy Tarreaubcbd3932018-06-06 07:13:22 +0200348 c_adv(chn, *nxt);
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200349 ret = http_compression_buffer_add_data(st, &chn->buf, co_data(chn), &zbuf, len);
Willy Tarreaubcbd3932018-06-06 07:13:22 +0200350 c_rew(chn, *nxt);
Christopher Fauletb77c5c22015-12-07 16:48:42 +0100351 if (ret < 0)
352 return ret;
353 }
Christopher Faulet92d36382015-11-05 13:35:03 +0100354
Christopher Faulet2fb28802015-12-01 10:40:57 +0100355 st->initialized = 1;
356 msg->next += ret;
357 msg->chunk_len -= ret;
Christopher Faulet3e7bc672015-12-07 13:39:08 +0100358 *nxt = msg->next;
Christopher Faulet2fb28802015-12-01 10:40:57 +0100359 return 0;
Christopher Faulet92d36382015-11-05 13:35:03 +0100360}
361
362static int
Christopher Faulet2fb28802015-12-01 10:40:57 +0100363comp_http_chunk_trailers(struct stream *s, struct filter *filter,
364 struct http_msg *msg)
Christopher Faulet92d36382015-11-05 13:35:03 +0100365{
366 struct comp_state *st = filter->ctx;
Christopher Faulet92d36382015-11-05 13:35:03 +0100367
Christopher Fauletb77c5c22015-12-07 16:48:42 +0100368 if (!st->initialized) {
369 if (!st->finished) {
Willy Tarreaubcbd3932018-06-06 07:13:22 +0200370 struct channel *chn = msg->chn;
Christopher Fauletb77c5c22015-12-07 16:48:42 +0100371 unsigned int fwd = flt_rsp_fwd(filter) + st->hdrs_len;
Christopher Faulet2fb28802015-12-01 10:40:57 +0100372
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200373 b_reset(&tmpbuf);
Willy Tarreaubcbd3932018-06-06 07:13:22 +0200374 c_adv(chn, fwd);
Christopher Fauletb61481c2018-12-17 13:17:53 +0100375 http_compression_buffer_init(chn, &zbuf);
Willy Tarreaubcbd3932018-06-06 07:13:22 +0200376 c_rew(chn, fwd);
Christopher Fauletb77c5c22015-12-07 16:48:42 +0100377 st->initialized = 1;
378 }
379 }
380 st->tlrs_len = msg->sol;
Christopher Faulet2fb28802015-12-01 10:40:57 +0100381 return 1;
Christopher Faulet92d36382015-11-05 13:35:03 +0100382}
383
Christopher Faulet2fb28802015-12-01 10:40:57 +0100384
Christopher Faulet92d36382015-11-05 13:35:03 +0100385static int
386comp_http_forward_data(struct stream *s, struct filter *filter,
387 struct http_msg *msg, unsigned int len)
388{
389 struct comp_state *st = filter->ctx;
Christopher Faulet2fb28802015-12-01 10:40:57 +0100390 int ret;
Christopher Faulet92d36382015-11-05 13:35:03 +0100391
Christopher Faulet2fb28802015-12-01 10:40:57 +0100392 /* To work, previous filters MUST forward all data */
Christopher Fauletb77c5c22015-12-07 16:48:42 +0100393 if (flt_rsp_fwd(filter) + len != flt_rsp_nxt(filter)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100394 ha_warning("HTTP compression failed: unexpected behavior of previous filters\n");
Christopher Faulet2fb28802015-12-01 10:40:57 +0100395 return -1;
Christopher Faulet92d36382015-11-05 13:35:03 +0100396 }
397
Christopher Faulet2fb28802015-12-01 10:40:57 +0100398 if (!st->initialized) {
Christopher Fauletb77c5c22015-12-07 16:48:42 +0100399 if (!len) {
Joseph Herlant942eea32018-11-15 13:57:22 -0800400 /* Nothing to forward */
Christopher Fauletb77c5c22015-12-07 16:48:42 +0100401 ret = len;
402 }
403 else if (st->hdrs_len > len) {
404 /* Forward part of headers */
405 ret = len;
406 st->hdrs_len -= len;
407 }
408 else if (st->hdrs_len > 0) {
409 /* Forward remaining headers */
410 ret = st->hdrs_len;
411 st->hdrs_len = 0;
412 }
413 else if (msg->msg_state < HTTP_MSG_TRAILERS) {
414 /* Do not forward anything for now. This only happens
415 * with chunk-encoded responses. Waiting data are part
416 * of the chunk envelope (the chunk size or the chunk
417 * CRLF). These data will be skipped during the
418 * compression. */
419 ret = 0;
420 }
421 else {
422 /* Forward trailers data */
423 ret = len;
424 }
Christopher Faulet2fb28802015-12-01 10:40:57 +0100425 return ret;
Christopher Faulet92d36382015-11-05 13:35:03 +0100426 }
427
Christopher Fauletb77c5c22015-12-07 16:48:42 +0100428 if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200429 ret = http_compression_buffer_add_data(st, &tmpbuf, 0,
430 &zbuf, b_data(&tmpbuf));
431 if (ret != b_data(&tmpbuf)) {
Willy Tarreau506a29a2018-07-18 10:07:58 +0200432 ha_warning("HTTP compression failed: Must consume %u bytes but only %d bytes consumed\n",
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200433 (unsigned int)b_data(&tmpbuf), ret);
Christopher Fauletb77c5c22015-12-07 16:48:42 +0100434 return -1;
435 }
436 }
437
438 st->consumed = len - st->hdrs_len - st->tlrs_len;
Willy Tarreaubcbd3932018-06-06 07:13:22 +0200439 c_adv(msg->chn, flt_rsp_fwd(filter) + st->hdrs_len);
Christopher Fauletb61481c2018-12-17 13:17:53 +0100440 ret = http_compression_buffer_end(st, s, msg->chn, &zbuf, msg->msg_state >= HTTP_MSG_TRAILERS);
Willy Tarreaubcbd3932018-06-06 07:13:22 +0200441 c_rew(msg->chn, flt_rsp_fwd(filter) + st->hdrs_len);
Christopher Faulet2fb28802015-12-01 10:40:57 +0100442 if (ret < 0)
443 return ret;
Christopher Faulet92d36382015-11-05 13:35:03 +0100444
Christopher Fauletb77c5c22015-12-07 16:48:42 +0100445 flt_change_forward_size(filter, msg->chn, ret - st->consumed);
446 msg->next += (ret - st->consumed);
447 ret += st->hdrs_len + st->tlrs_len;
448
Christopher Faulet2fb28802015-12-01 10:40:57 +0100449 st->initialized = 0;
Christopher Fauletb77c5c22015-12-07 16:48:42 +0100450 st->finished = (msg->msg_state >= HTTP_MSG_TRAILERS);
451 st->hdrs_len = 0;
452 st->tlrs_len = 0;
Christopher Faulet92d36382015-11-05 13:35:03 +0100453 return ret;
454}
Christopher Faulet3d97c902015-12-09 14:59:38 +0100455
Christopher Fauletd60b3cf2017-06-26 11:47:13 +0200456static int
457comp_http_end(struct stream *s, struct filter *filter,
458 struct http_msg *msg)
459{
460 struct comp_state *st = filter->ctx;
461
462 if (!(msg->chn->flags & CF_ISRESP) || !st || !st->comp_algo)
463 goto end;
464
465 if (strm_fe(s)->mode == PR_MODE_HTTP)
Olivier Houchard43da3432019-03-08 18:50:27 +0100466 _HA_ATOMIC_ADD(&strm_fe(s)->fe_counters.p.http.comp_rsp, 1);
Christopher Fauletd60b3cf2017-06-26 11:47:13 +0200467 if ((s->flags & SF_BE_ASSIGNED) && (s->be->mode == PR_MODE_HTTP))
Olivier Houchard43da3432019-03-08 18:50:27 +0100468 _HA_ATOMIC_ADD(&s->be->be_counters.p.http.comp_rsp, 1);
Christopher Fauletd60b3cf2017-06-26 11:47:13 +0200469 end:
470 return 1;
471}
Christopher Faulet3d97c902015-12-09 14:59:38 +0100472/***********************************************************************/
Christopher Faulet27d93c32018-12-15 22:32:02 +0100473static int
474http_set_comp_reshdr(struct comp_state *st, struct stream *s, struct http_msg *msg)
475{
476 struct http_txn *txn = s->txn;
Tim Duesterhusb229f012019-01-29 16:38:56 +0100477 struct hdr_ctx ctx;
Christopher Faulet27d93c32018-12-15 22:32:02 +0100478
479 /*
480 * Add Content-Encoding header when it's not identity encoding.
481 * RFC 2616 : Identity encoding: This content-coding is used only in the
482 * Accept-Encoding header, and SHOULD NOT be used in the Content-Encoding
483 * header.
484 */
485 if (st->comp_algo->cfg_name_len != 8 || memcmp(st->comp_algo->cfg_name, "identity", 8) != 0) {
486 trash.data = 18;
487 memcpy(trash.area, "Content-Encoding: ", trash.data);
488 memcpy(trash.area + trash.data, st->comp_algo->ua_name,
489 st->comp_algo->ua_name_len);
490 trash.data += st->comp_algo->ua_name_len;
491 trash.area[trash.data] = '\0';
492 if (http_header_add_tail2(msg, &txn->hdr_idx, trash.area, trash.data) < 0)
493 goto error;
494 }
495
496 /* remove Content-Length header */
497 if (msg->flags & HTTP_MSGF_CNT_LEN) {
Christopher Faulet27d93c32018-12-15 22:32:02 +0100498 ctx.idx = 0;
499 while (http_find_header2("Content-Length", 14, ci_head(&s->res), &txn->hdr_idx, &ctx))
500 http_remove_header2(msg, &txn->hdr_idx, &ctx);
501 }
502
503 /* add Transfer-Encoding header */
504 if (!(msg->flags & HTTP_MSGF_TE_CHNK)) {
505 if (http_header_add_tail2(msg, &txn->hdr_idx, "Transfer-Encoding: chunked", 26) < 0)
506 goto error;
507 }
508
Tim Duesterhusb229f012019-01-29 16:38:56 +0100509 ctx.idx = 0;
510 if (http_find_full_header2("ETag", 4, ci_head(&s->res), &txn->hdr_idx, &ctx)) {
511 if (ctx.line[ctx.val] == '"') {
512 /* This a strong ETag. Convert it to a weak one. */
513 trash.data = 8;
514 if (trash.data + ctx.vlen > trash.size)
515 goto error;
516 memcpy(trash.area, "ETag: W/", trash.data);
517 memcpy(trash.area + trash.data, ctx.line + ctx.val, ctx.vlen);
518 trash.data += ctx.vlen;
519 trash.area[trash.data] = '\0';
520 http_remove_header2(msg, &txn->hdr_idx, &ctx);
521 if (http_header_add_tail2(msg, &txn->hdr_idx, trash.area, trash.data) < 0)
522 goto error;
523 }
524 }
Christopher Faulet27d93c32018-12-15 22:32:02 +0100525
526 return 1;
527
528 error:
529 st->comp_algo->end(&st->comp_ctx);
530 st->comp_algo = NULL;
531 return 0;
532}
533
534static int
535htx_set_comp_reshdr(struct comp_state *st, struct stream *s, struct http_msg *msg)
536{
537 struct htx *htx = htxbuf(&msg->chn->buf);
Tim Duesterhusb229f012019-01-29 16:38:56 +0100538 struct http_hdr_ctx ctx;
Christopher Faulet27d93c32018-12-15 22:32:02 +0100539
540 /*
541 * Add Content-Encoding header when it's not identity encoding.
542 * RFC 2616 : Identity encoding: This content-coding is used only in the
543 * Accept-Encoding header, and SHOULD NOT be used in the Content-Encoding
544 * header.
545 */
546 if (st->comp_algo->cfg_name_len != 8 || memcmp(st->comp_algo->cfg_name, "identity", 8) != 0) {
547 struct ist v = ist2(st->comp_algo->ua_name, st->comp_algo->ua_name_len);
548
549 if (!http_add_header(htx, ist("Content-Encoding"), v))
550 goto error;
551 }
552
553 /* remove Content-Length header */
554 if (msg->flags & HTTP_MSGF_CNT_LEN) {
Christopher Faulet27d93c32018-12-15 22:32:02 +0100555 ctx.blk = NULL;
556 while (http_find_header(htx, ist("Content-Length"), &ctx, 1))
557 http_remove_header(htx, &ctx);
558 }
559
560 /* add "Transfer-Encoding: chunked" header */
561 if (!(msg->flags & HTTP_MSGF_TE_CHNK)) {
562 if (!http_add_header(htx, ist("Transfer-Encoding"), ist("chunked")))
563 goto error;
564 }
565
Tim Duesterhusb229f012019-01-29 16:38:56 +0100566 /* convert "ETag" header to a weak ETag */
567 ctx.blk = NULL;
568 if (http_find_header(htx, ist("ETag"), &ctx, 1)) {
569 if (ctx.value.ptr[0] == '"') {
570 /* This a strong ETag. Convert it to a weak one. */
571 struct ist v = ist2(trash.area, 0);
572 if (istcat(&v, ist("W/"), trash.size) == -1 || istcat(&v, ctx.value, trash.size) == -1)
573 goto error;
574
575 if (!http_replace_header_value(htx, &ctx, v))
576 goto error;
577 }
578 }
579
Christopher Faulet27d93c32018-12-15 22:32:02 +0100580 return 1;
581
582 error:
583 st->comp_algo->end(&st->comp_ctx);
584 st->comp_algo = NULL;
585 return 0;
586}
587
588static int
589set_compression_response_header(struct comp_state *st, struct stream *s, struct http_msg *msg)
590{
591 if (IS_HTX_STRM(s))
592 return htx_set_comp_reshdr(st, s, msg);
593 else
594 return http_set_comp_reshdr(st, s, msg);
595}
596
Christopher Faulet3d97c902015-12-09 14:59:38 +0100597/*
598 * Selects a compression algorithm depending on the client request.
599 */
Christopher Faulete6902cd2018-11-30 22:29:48 +0100600static int
601http_select_comp_reqhdr(struct comp_state *st, struct stream *s, struct http_msg *msg)
Christopher Faulet3d97c902015-12-09 14:59:38 +0100602{
603 struct http_txn *txn = s->txn;
Olivier Houchard0b662842018-06-29 18:16:31 +0200604 struct channel *req = msg->chn;
Christopher Faulet3d97c902015-12-09 14:59:38 +0100605 struct hdr_ctx ctx;
606 struct comp_algo *comp_algo = NULL;
607 struct comp_algo *comp_algo_back = NULL;
608
609 /* Disable compression for older user agents announcing themselves as "Mozilla/4"
610 * unless they are known good (MSIE 6 with XP SP2, or MSIE 7 and later).
611 * See http://zoompf.com/2012/02/lose-the-wait-http-compression for more details.
612 */
613 ctx.idx = 0;
Olivier Houchard0b662842018-06-29 18:16:31 +0200614 if (http_find_header2("User-Agent", 10, ci_head(req), &txn->hdr_idx, &ctx) &&
Christopher Faulet3d97c902015-12-09 14:59:38 +0100615 ctx.vlen >= 9 &&
616 memcmp(ctx.line + ctx.val, "Mozilla/4", 9) == 0 &&
617 (ctx.vlen < 31 ||
618 memcmp(ctx.line + ctx.val + 25, "MSIE ", 5) != 0 ||
619 ctx.line[ctx.val + 30] < '6' ||
620 (ctx.line[ctx.val + 30] == '6' &&
621 (ctx.vlen < 54 || memcmp(ctx.line + 51, "SV1", 3) != 0)))) {
Christopher Faulet92d36382015-11-05 13:35:03 +0100622 st->comp_algo = NULL;
Christopher Faulet3d97c902015-12-09 14:59:38 +0100623 return 0;
624 }
625
626 /* search for the algo in the backend in priority or the frontend */
Christopher Faulet92d36382015-11-05 13:35:03 +0100627 if ((s->be->comp && (comp_algo_back = s->be->comp->algos)) ||
628 (strm_fe(s)->comp && (comp_algo_back = strm_fe(s)->comp->algos))) {
Christopher Faulet3d97c902015-12-09 14:59:38 +0100629 int best_q = 0;
630
631 ctx.idx = 0;
Olivier Houchard0b662842018-06-29 18:16:31 +0200632 while (http_find_header2("Accept-Encoding", 15, ci_head(req), &txn->hdr_idx, &ctx)) {
Christopher Faulet3d97c902015-12-09 14:59:38 +0100633 const char *qval;
634 int q;
635 int toklen;
636
637 /* try to isolate the token from the optional q-value */
638 toklen = 0;
Willy Tarreau2235b262016-11-05 15:50:20 +0100639 while (toklen < ctx.vlen && HTTP_IS_TOKEN(*(ctx.line + ctx.val + toklen)))
Christopher Faulet3d97c902015-12-09 14:59:38 +0100640 toklen++;
641
642 qval = ctx.line + ctx.val + toklen;
643 while (1) {
Willy Tarreau2235b262016-11-05 15:50:20 +0100644 while (qval < ctx.line + ctx.val + ctx.vlen && HTTP_IS_LWS(*qval))
Christopher Faulet3d97c902015-12-09 14:59:38 +0100645 qval++;
646
647 if (qval >= ctx.line + ctx.val + ctx.vlen || *qval != ';') {
648 qval = NULL;
649 break;
650 }
651 qval++;
652
Willy Tarreau2235b262016-11-05 15:50:20 +0100653 while (qval < ctx.line + ctx.val + ctx.vlen && HTTP_IS_LWS(*qval))
Christopher Faulet3d97c902015-12-09 14:59:38 +0100654 qval++;
655
656 if (qval >= ctx.line + ctx.val + ctx.vlen) {
657 qval = NULL;
658 break;
659 }
660 if (strncmp(qval, "q=", MIN(ctx.line + ctx.val + ctx.vlen - qval, 2)) == 0)
661 break;
662
663 while (qval < ctx.line + ctx.val + ctx.vlen && *qval != ';')
664 qval++;
665 }
666
667 /* here we have qval pointing to the first "q=" attribute or NULL if not found */
Willy Tarreauab813a42018-09-10 18:41:28 +0200668 q = qval ? http_parse_qvalue(qval + 2, NULL) : 1000;
Christopher Faulet3d97c902015-12-09 14:59:38 +0100669
670 if (q <= best_q)
671 continue;
672
673 for (comp_algo = comp_algo_back; comp_algo; comp_algo = comp_algo->next) {
674 if (*(ctx.line + ctx.val) == '*' ||
675 word_match(ctx.line + ctx.val, toklen, comp_algo->ua_name, comp_algo->ua_name_len)) {
Christopher Faulet92d36382015-11-05 13:35:03 +0100676 st->comp_algo = comp_algo;
Christopher Faulet3d97c902015-12-09 14:59:38 +0100677 best_q = q;
678 break;
679 }
680 }
681 }
682 }
683
684 /* remove all occurrences of the header when "compression offload" is set */
Christopher Faulet92d36382015-11-05 13:35:03 +0100685 if (st->comp_algo) {
686 if ((s->be->comp && s->be->comp->offload) ||
687 (strm_fe(s)->comp && strm_fe(s)->comp->offload)) {
Christopher Faulet3d97c902015-12-09 14:59:38 +0100688 http_remove_header2(msg, &txn->hdr_idx, &ctx);
689 ctx.idx = 0;
Olivier Houchard0b662842018-06-29 18:16:31 +0200690 while (http_find_header2("Accept-Encoding", 15, ci_head(req), &txn->hdr_idx, &ctx)) {
Christopher Faulet3d97c902015-12-09 14:59:38 +0100691 http_remove_header2(msg, &txn->hdr_idx, &ctx);
692 }
693 }
Christopher Faulete6902cd2018-11-30 22:29:48 +0100694 return 1;
695 }
696
697 /* identity is implicit does not require headers */
698 if ((s->be->comp && (comp_algo_back = s->be->comp->algos)) ||
699 (strm_fe(s)->comp && (comp_algo_back = strm_fe(s)->comp->algos))) {
700 for (comp_algo = comp_algo_back; comp_algo; comp_algo = comp_algo->next) {
701 if (comp_algo->cfg_name_len == 8 && memcmp(comp_algo->cfg_name, "identity", 8) == 0) {
702 st->comp_algo = comp_algo;
703 return 1;
704 }
705 }
706 }
707
708 st->comp_algo = NULL;
709 return 0;
710}
711
712static int
713htx_select_comp_reqhdr(struct comp_state *st, struct stream *s, struct http_msg *msg)
714{
Christopher Faulet27ba2dc2018-12-05 11:53:24 +0100715 struct htx *htx = htxbuf(&msg->chn->buf);
Christopher Faulete6902cd2018-11-30 22:29:48 +0100716 struct http_hdr_ctx ctx;
717 struct comp_algo *comp_algo = NULL;
718 struct comp_algo *comp_algo_back = NULL;
719
720 /* Disable compression for older user agents announcing themselves as "Mozilla/4"
721 * unless they are known good (MSIE 6 with XP SP2, or MSIE 7 and later).
722 * See http://zoompf.com/2012/02/lose-the-wait-http-compression for more details.
723 */
724 ctx.blk = NULL;
725 if (http_find_header(htx, ist("User-Agent"), &ctx, 1) &&
726 ctx.value.len >= 9 &&
727 memcmp(ctx.value.ptr, "Mozilla/4", 9) == 0 &&
728 (ctx.value.len < 31 ||
729 memcmp(ctx.value.ptr + 25, "MSIE ", 5) != 0 ||
730 *(ctx.value.ptr + 30) < '6' ||
731 (*(ctx.value.ptr + 30) == '6' &&
732 (ctx.value.len < 54 || memcmp(ctx.value.ptr + 51, "SV1", 3) != 0)))) {
733 st->comp_algo = NULL;
734 return 0;
735 }
736
737 /* search for the algo in the backend in priority or the frontend */
738 if ((s->be->comp && (comp_algo_back = s->be->comp->algos)) ||
739 (strm_fe(s)->comp && (comp_algo_back = strm_fe(s)->comp->algos))) {
740 int best_q = 0;
741
742 ctx.blk = NULL;
743 while (http_find_header(htx, ist("Accept-Encoding"), &ctx, 0)) {
744 const char *qval;
745 int q;
746 int toklen;
747
748 /* try to isolate the token from the optional q-value */
749 toklen = 0;
750 while (toklen < ctx.value.len && HTTP_IS_TOKEN(*(ctx.value.ptr + toklen)))
751 toklen++;
752
753 qval = ctx.value.ptr + toklen;
754 while (1) {
755 while (qval < ctx.value.ptr + ctx.value.len && HTTP_IS_LWS(*qval))
756 qval++;
757
758 if (qval >= ctx.value.ptr + ctx.value.len || *qval != ';') {
759 qval = NULL;
760 break;
761 }
762 qval++;
763
764 while (qval < ctx.value.ptr + ctx.value.len && HTTP_IS_LWS(*qval))
765 qval++;
766
767 if (qval >= ctx.value.ptr + ctx.value.len) {
768 qval = NULL;
769 break;
770 }
771 if (strncmp(qval, "q=", MIN(ctx.value.ptr + ctx.value.len - qval, 2)) == 0)
772 break;
773
774 while (qval < ctx.value.ptr + ctx.value.len && *qval != ';')
775 qval++;
776 }
777
778 /* here we have qval pointing to the first "q=" attribute or NULL if not found */
779 q = qval ? http_parse_qvalue(qval + 2, NULL) : 1000;
780
781 if (q <= best_q)
782 continue;
783
784 for (comp_algo = comp_algo_back; comp_algo; comp_algo = comp_algo->next) {
785 if (*(ctx.value.ptr) == '*' ||
786 word_match(ctx.value.ptr, toklen, comp_algo->ua_name, comp_algo->ua_name_len)) {
787 st->comp_algo = comp_algo;
788 best_q = q;
789 break;
790 }
791 }
792 }
793 }
794
795 /* remove all occurrences of the header when "compression offload" is set */
796 if (st->comp_algo) {
797 if ((s->be->comp && s->be->comp->offload) ||
798 (strm_fe(s)->comp && strm_fe(s)->comp->offload)) {
799 http_remove_header(htx, &ctx);
800 ctx.blk = NULL;
801 while (http_find_header(htx, ist("Accept-Encoding"), &ctx, 1))
802 http_remove_header(htx, &ctx);
803 }
Christopher Faulet3d97c902015-12-09 14:59:38 +0100804 return 1;
805 }
806
807 /* identity is implicit does not require headers */
Christopher Faulet92d36382015-11-05 13:35:03 +0100808 if ((s->be->comp && (comp_algo_back = s->be->comp->algos)) ||
809 (strm_fe(s)->comp && (comp_algo_back = strm_fe(s)->comp->algos))) {
Christopher Faulet3d97c902015-12-09 14:59:38 +0100810 for (comp_algo = comp_algo_back; comp_algo; comp_algo = comp_algo->next) {
811 if (comp_algo->cfg_name_len == 8 && memcmp(comp_algo->cfg_name, "identity", 8) == 0) {
Christopher Faulet92d36382015-11-05 13:35:03 +0100812 st->comp_algo = comp_algo;
Christopher Faulet3d97c902015-12-09 14:59:38 +0100813 return 1;
814 }
815 }
816 }
817
Christopher Faulet92d36382015-11-05 13:35:03 +0100818 st->comp_algo = NULL;
Christopher Faulet3d97c902015-12-09 14:59:38 +0100819 return 0;
820}
821
Christopher Faulete6902cd2018-11-30 22:29:48 +0100822static int
823select_compression_request_header(struct comp_state *st, struct stream *s,
824 struct http_msg *msg)
825{
826 if (IS_HTX_STRM(s))
827 return htx_select_comp_reqhdr(st, s, msg);
828 else
829 return http_select_comp_reqhdr(st, s, msg);
830}
Christopher Faulet92d36382015-11-05 13:35:03 +0100831
Christopher Faulet3d97c902015-12-09 14:59:38 +0100832/*
833 * Selects a comression algorithm depending of the server response.
834 */
Christopher Faulet92d36382015-11-05 13:35:03 +0100835static int
Christopher Faulete6902cd2018-11-30 22:29:48 +0100836http_select_comp_reshdr(struct comp_state *st, struct stream *s, struct http_msg *msg)
Christopher Faulet3d97c902015-12-09 14:59:38 +0100837{
838 struct http_txn *txn = s->txn;
Olivier Houchard0b662842018-06-29 18:16:31 +0200839 struct channel *c = msg->chn;
Christopher Faulet3d97c902015-12-09 14:59:38 +0100840 struct hdr_ctx ctx;
841 struct comp_type *comp_type;
842
843 /* no common compression algorithm was found in request header */
Christopher Faulet92d36382015-11-05 13:35:03 +0100844 if (st->comp_algo == NULL)
Christopher Faulet3d97c902015-12-09 14:59:38 +0100845 goto fail;
846
Christopher Faulet1d3613a2019-01-07 14:41:59 +0100847 /* compression already in progress */
848 if (msg->flags & HTTP_MSGF_COMPRESSING)
849 goto fail;
850
Christopher Faulet3d97c902015-12-09 14:59:38 +0100851 /* HTTP < 1.1 should not be compressed */
852 if (!(msg->flags & HTTP_MSGF_VER_11) || !(txn->req.flags & HTTP_MSGF_VER_11))
853 goto fail;
854
Christopher Faulet92d36382015-11-05 13:35:03 +0100855 if (txn->meth == HTTP_METH_HEAD)
856 goto fail;
857
Christopher Faulet3d97c902015-12-09 14:59:38 +0100858 /* compress 200,201,202,203 responses only */
859 if ((txn->status != 200) &&
860 (txn->status != 201) &&
861 (txn->status != 202) &&
862 (txn->status != 203))
863 goto fail;
864
865
866 /* Content-Length is null */
867 if (!(msg->flags & HTTP_MSGF_TE_CHNK) && msg->body_len == 0)
868 goto fail;
869
870 /* content is already compressed */
871 ctx.idx = 0;
Olivier Houchard0b662842018-06-29 18:16:31 +0200872 if (http_find_header2("Content-Encoding", 16, ci_head(c), &txn->hdr_idx, &ctx))
Christopher Faulet3d97c902015-12-09 14:59:38 +0100873 goto fail;
874
875 /* no compression when Cache-Control: no-transform is present in the message */
876 ctx.idx = 0;
Olivier Houchard0b662842018-06-29 18:16:31 +0200877 while (http_find_header2("Cache-Control", 13, ci_head(c), &txn->hdr_idx, &ctx)) {
Christopher Faulet3d97c902015-12-09 14:59:38 +0100878 if (word_match(ctx.line + ctx.val, ctx.vlen, "no-transform", 12))
879 goto fail;
880 }
881
Tim Duesterhusb229f012019-01-29 16:38:56 +0100882 /* no compression when ETag is malformed */
883 ctx.idx = 0;
884 if (http_find_full_header2("ETag", 4, ci_head(c), &txn->hdr_idx, &ctx)) {
885 if (!(((ctx.vlen >= 4 && memcmp(ctx.line + ctx.val, "W/\"", 3) == 0) || /* Either a weak ETag */
886 (ctx.vlen >= 2 && ctx.line[ctx.val] == '"')) && /* or strong ETag */
887 ctx.line[ctx.val + ctx.vlen - 1] == '"')) {
888 goto fail;
889 }
890 }
891 /* no compression when multiple ETags are present
892 * Note: Do not reset ctx.idx!
893 */
894 if (http_find_full_header2("ETag", 4, ci_head(c), &txn->hdr_idx, &ctx))
895 goto fail;
896
Christopher Faulet3d97c902015-12-09 14:59:38 +0100897 comp_type = NULL;
898
899 /* we don't want to compress multipart content-types, nor content-types that are
900 * not listed in the "compression type" directive if any. If no content-type was
901 * found but configuration requires one, we don't compress either. Backend has
902 * the priority.
903 */
904 ctx.idx = 0;
Olivier Houchard0b662842018-06-29 18:16:31 +0200905 if (http_find_header2("Content-Type", 12, ci_head(c), &txn->hdr_idx, &ctx)) {
Christopher Faulet3d97c902015-12-09 14:59:38 +0100906 if (ctx.vlen >= 9 && strncasecmp("multipart", ctx.line+ctx.val, 9) == 0)
907 goto fail;
908
909 if ((s->be->comp && (comp_type = s->be->comp->types)) ||
910 (strm_fe(s)->comp && (comp_type = strm_fe(s)->comp->types))) {
911 for (; comp_type; comp_type = comp_type->next) {
912 if (ctx.vlen >= comp_type->name_len &&
913 strncasecmp(ctx.line+ctx.val, comp_type->name, comp_type->name_len) == 0)
914 /* this Content-Type should be compressed */
915 break;
916 }
917 /* this Content-Type should not be compressed */
918 if (comp_type == NULL)
919 goto fail;
920 }
921 }
922 else { /* no content-type header */
Christopher Faulet92d36382015-11-05 13:35:03 +0100923 if ((s->be->comp && s->be->comp->types) ||
924 (strm_fe(s)->comp && strm_fe(s)->comp->types))
Christopher Faulet3d97c902015-12-09 14:59:38 +0100925 goto fail; /* a content-type was required */
926 }
927
928 /* limit compression rate */
929 if (global.comp_rate_lim > 0)
930 if (read_freq_ctr(&global.comp_bps_in) > global.comp_rate_lim)
931 goto fail;
932
933 /* limit cpu usage */
Willy Tarreau81036f22019-05-20 19:24:50 +0200934 if (ti->idle_pct < compress_min_idle)
Christopher Faulet3d97c902015-12-09 14:59:38 +0100935 goto fail;
936
937 /* initialize compression */
Christopher Faulet92d36382015-11-05 13:35:03 +0100938 if (st->comp_algo->init(&st->comp_ctx, global.tune.comp_maxlevel) < 0)
Christopher Faulet3d97c902015-12-09 14:59:38 +0100939 goto fail;
Christopher Faulet92d36382015-11-05 13:35:03 +0100940 msg->flags |= HTTP_MSGF_COMPRESSING;
Christopher Faulet3d97c902015-12-09 14:59:38 +0100941 return 1;
942
943fail:
Christopher Faulet92d36382015-11-05 13:35:03 +0100944 st->comp_algo = NULL;
Christopher Faulet3d97c902015-12-09 14:59:38 +0100945 return 0;
946}
947
Christopher Faulete6902cd2018-11-30 22:29:48 +0100948static int
949htx_select_comp_reshdr(struct comp_state *st, struct stream *s, struct http_msg *msg)
950{
Christopher Faulet27ba2dc2018-12-05 11:53:24 +0100951 struct htx *htx = htxbuf(&msg->chn->buf);
Christopher Faulete6902cd2018-11-30 22:29:48 +0100952 struct http_txn *txn = s->txn;
953 struct http_hdr_ctx ctx;
954 struct comp_type *comp_type;
955
956 /* no common compression algorithm was found in request header */
957 if (st->comp_algo == NULL)
958 goto fail;
959
Christopher Faulet1d3613a2019-01-07 14:41:59 +0100960 /* compression already in progress */
961 if (msg->flags & HTTP_MSGF_COMPRESSING)
962 goto fail;
963
Christopher Faulete6902cd2018-11-30 22:29:48 +0100964 /* HTTP < 1.1 should not be compressed */
965 if (!(msg->flags & HTTP_MSGF_VER_11) || !(txn->req.flags & HTTP_MSGF_VER_11))
966 goto fail;
967
968 if (txn->meth == HTTP_METH_HEAD)
969 goto fail;
970
971 /* compress 200,201,202,203 responses only */
972 if ((txn->status != 200) &&
973 (txn->status != 201) &&
974 (txn->status != 202) &&
975 (txn->status != 203))
976 goto fail;
977
Christopher Fauletc963eb22018-12-21 14:53:54 +0100978 if (!(msg->flags & HTTP_MSGF_XFER_LEN) || msg->flags & HTTP_MSGF_BODYLESS)
Christopher Faulete6902cd2018-11-30 22:29:48 +0100979 goto fail;
980
981 /* content is already compressed */
982 ctx.blk = NULL;
983 if (http_find_header(htx, ist("Content-Encoding"), &ctx, 1))
984 goto fail;
985
986 /* no compression when Cache-Control: no-transform is present in the message */
987 ctx.blk = NULL;
988 while (http_find_header(htx, ist("Cache-Control"), &ctx, 0)) {
989 if (word_match(ctx.value.ptr, ctx.value.len, "no-transform", 12))
990 goto fail;
991 }
992
Tim Duesterhusb229f012019-01-29 16:38:56 +0100993 /* no compression when ETag is malformed */
994 ctx.blk = NULL;
995 if (http_find_header(htx, ist("ETag"), &ctx, 1)) {
996 if (!(((ctx.value.len >= 4 && memcmp(ctx.value.ptr, "W/\"", 3) == 0) || /* Either a weak ETag */
997 (ctx.value.len >= 2 && ctx.value.ptr[0] == '"')) && /* or strong ETag */
998 ctx.value.ptr[ctx.value.len - 1] == '"')) {
999 goto fail;
1000 }
1001 }
1002 /* no compression when multiple ETags are present
1003 * Note: Do not reset ctx.blk!
1004 */
1005 if (http_find_header(htx, ist("ETag"), &ctx, 1))
1006 goto fail;
1007
Christopher Faulete6902cd2018-11-30 22:29:48 +01001008 comp_type = NULL;
1009
1010 /* we don't want to compress multipart content-types, nor content-types that are
1011 * not listed in the "compression type" directive if any. If no content-type was
1012 * found but configuration requires one, we don't compress either. Backend has
1013 * the priority.
1014 */
1015 ctx.blk = NULL;
1016 if (http_find_header(htx, ist("Content-Type"), &ctx, 1)) {
1017 if (ctx.value.len >= 9 && strncasecmp("multipart", ctx.value.ptr, 9) == 0)
1018 goto fail;
1019
1020 if ((s->be->comp && (comp_type = s->be->comp->types)) ||
1021 (strm_fe(s)->comp && (comp_type = strm_fe(s)->comp->types))) {
1022 for (; comp_type; comp_type = comp_type->next) {
1023 if (ctx.value.len >= comp_type->name_len &&
1024 strncasecmp(ctx.value.ptr, comp_type->name, comp_type->name_len) == 0)
1025 /* this Content-Type should be compressed */
1026 break;
1027 }
1028 /* this Content-Type should not be compressed */
1029 if (comp_type == NULL)
1030 goto fail;
1031 }
1032 }
1033 else { /* no content-type header */
1034 if ((s->be->comp && s->be->comp->types) ||
1035 (strm_fe(s)->comp && strm_fe(s)->comp->types))
1036 goto fail; /* a content-type was required */
1037 }
1038
1039 /* limit compression rate */
1040 if (global.comp_rate_lim > 0)
1041 if (read_freq_ctr(&global.comp_bps_in) > global.comp_rate_lim)
1042 goto fail;
1043
1044 /* limit cpu usage */
Willy Tarreau81036f22019-05-20 19:24:50 +02001045 if (ti->idle_pct < compress_min_idle)
Christopher Faulete6902cd2018-11-30 22:29:48 +01001046 goto fail;
1047
1048 /* initialize compression */
1049 if (st->comp_algo->init(&st->comp_ctx, global.tune.comp_maxlevel) < 0)
1050 goto fail;
Christopher Faulete6902cd2018-11-30 22:29:48 +01001051 msg->flags |= HTTP_MSGF_COMPRESSING;
1052 return 1;
1053
1054 deinit_comp_ctx:
1055 st->comp_algo->end(&st->comp_ctx);
1056 fail:
1057 st->comp_algo = NULL;
1058 return 0;
1059}
1060
1061static int
1062select_compression_response_header(struct comp_state *st, struct stream *s, struct http_msg *msg)
1063{
1064 if (IS_HTX_STRM(s))
1065 return htx_select_comp_reshdr(st, s, msg);
1066 else
1067 return http_select_comp_reshdr(st, s, msg);
1068}
Christopher Faulet3d97c902015-12-09 14:59:38 +01001069/***********************************************************************/
1070/* emit the chunksize followed by a CRLF on the output and return the number of
1071 * bytes written. It goes backwards and starts with the byte before <end>. It
1072 * returns the number of bytes written which will not exceed 10 (8 digits, CR,
1073 * and LF). The caller is responsible for ensuring there is enough room left in
1074 * the output buffer for the string.
1075 */
1076static int
1077http_emit_chunk_size(char *end, unsigned int chksz)
1078{
1079 char *beg = end;
1080
1081 *--beg = '\n';
1082 *--beg = '\r';
1083 do {
1084 *--beg = hextab[chksz & 0xF];
1085 } while (chksz >>= 4);
1086 return end - beg;
1087}
1088
1089/*
1090 * Init HTTP compression
1091 */
Christopher Faulet92d36382015-11-05 13:35:03 +01001092static int
Christopher Fauletb61481c2018-12-17 13:17:53 +01001093http_compression_buffer_init(struct channel *inc, struct buffer *out)
Christopher Faulet3d97c902015-12-09 14:59:38 +01001094{
1095 /* output stream requires at least 10 bytes for the gzip header, plus
1096 * at least 8 bytes for the gzip trailer (crc+len), plus a possible
1097 * plus at most 5 bytes per 32kB block and 2 bytes to close the stream.
1098 */
Olivier Houchard0b662842018-06-29 18:16:31 +02001099 if (c_room(inc) < 20 + 5 * ((ci_data(inc) + 32767) >> 15))
Christopher Faulet3d97c902015-12-09 14:59:38 +01001100 return -1;
1101
1102 /* prepare an empty output buffer in which we reserve enough room for
1103 * copying the output bytes from <in>, plus 10 extra bytes to write
1104 * the chunk size. We don't copy the bytes yet so that if we have to
1105 * cancel the operation later, it's cheap.
1106 */
1107 b_reset(out);
Christopher Fauletb61481c2018-12-17 13:17:53 +01001108 out->head += co_data(inc) + 10;
Christopher Faulet3d97c902015-12-09 14:59:38 +01001109 return 0;
1110}
1111
Christopher Faulete6902cd2018-11-30 22:29:48 +01001112static int
1113htx_compression_buffer_init(struct htx *htx, struct buffer *out)
1114{
1115 /* output stream requires at least 10 bytes for the gzip header, plus
1116 * at least 8 bytes for the gzip trailer (crc+len), plus a possible
1117 * plus at most 5 bytes per 32kB block and 2 bytes to close the stream.
1118 */
1119 if (htx_free_space(htx) < 20 + 5 * ((htx->data + 32767) >> 15))
1120 return -1;
1121 b_reset(out);
1122 return 0;
1123}
1124
Christopher Faulet3d97c902015-12-09 14:59:38 +01001125/*
1126 * Add data to compress
1127 */
Christopher Faulet92d36382015-11-05 13:35:03 +01001128static int
1129http_compression_buffer_add_data(struct comp_state *st, struct buffer *in,
Willy Tarreaud54a8ce2018-06-29 18:42:02 +02001130 int in_out, struct buffer *out, int sz)
Christopher Faulet3d97c902015-12-09 14:59:38 +01001131{
Christopher Faulet3d97c902015-12-09 14:59:38 +01001132 int consumed_data = 0;
1133 int data_process_len;
1134 int block1, block2;
1135
Christopher Faulet92d36382015-11-05 13:35:03 +01001136 if (!sz)
Christopher Faulet3e7bc672015-12-07 13:39:08 +01001137 goto end;
Christopher Faulet3d97c902015-12-09 14:59:38 +01001138
Christopher Faulet92d36382015-11-05 13:35:03 +01001139 /* select the smallest size between the announced chunk size, the input
Christopher Faulet3d97c902015-12-09 14:59:38 +01001140 * data, and the available output buffer size. The compressors are
Christopher Faulet92d36382015-11-05 13:35:03 +01001141 * assumed to be able to process all the bytes we pass to them at
1142 * once. */
Willy Tarreaueac52592018-06-15 13:59:36 +02001143 data_process_len = MIN(b_room(out), sz);
Christopher Faulet92d36382015-11-05 13:35:03 +01001144
Christopher Faulet3d97c902015-12-09 14:59:38 +01001145 block1 = data_process_len;
Willy Tarreaud54a8ce2018-06-29 18:42:02 +02001146 if (block1 > b_contig_data(in, in_out))
1147 block1 = b_contig_data(in, in_out);
Christopher Faulet3d97c902015-12-09 14:59:38 +01001148 block2 = data_process_len - block1;
1149
1150 /* compressors return < 0 upon error or the amount of bytes read */
Christopher Faulet96667202018-12-17 12:02:57 +01001151 consumed_data = st->comp_algo->add_data(st->comp_ctx, b_peek(in, in_out), block1, out);
Christopher Faulet3e7bc672015-12-07 13:39:08 +01001152 if (consumed_data != block1 || !block2)
1153 goto end;
Christopher Faulet96667202018-12-17 12:02:57 +01001154 consumed_data = st->comp_algo->add_data(st->comp_ctx, b_orig(in), block2, out);
Christopher Faulet3e7bc672015-12-07 13:39:08 +01001155 if (consumed_data < 0)
1156 goto end;
1157 consumed_data += block1;
1158
1159 end:
Christopher Faulet3d97c902015-12-09 14:59:38 +01001160 return consumed_data;
1161}
1162
Christopher Faulete6902cd2018-11-30 22:29:48 +01001163static int
1164htx_compression_buffer_add_data(struct comp_state *st, const char *data, size_t len,
1165 struct buffer *out)
1166{
1167 return st->comp_algo->add_data(st->comp_ctx, data, len, out);
1168}
1169
Christopher Faulet3d97c902015-12-09 14:59:38 +01001170/*
1171 * Flush data in process, and write the header and footer of the chunk. Upon
1172 * success, in and out buffers are swapped to avoid a copy.
1173 */
Christopher Faulet92d36382015-11-05 13:35:03 +01001174static int
1175http_compression_buffer_end(struct comp_state *st, struct stream *s,
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001176 struct channel *chn, struct buffer *out,
Christopher Fauletb61481c2018-12-17 13:17:53 +01001177 int end)
Christopher Faulet3d97c902015-12-09 14:59:38 +01001178{
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001179 struct buffer tmp_buf;
Christopher Faulet3d97c902015-12-09 14:59:38 +01001180 char *tail;
Christopher Faulet92d36382015-11-05 13:35:03 +01001181 int to_forward, left;
Christopher Faulet3d97c902015-12-09 14:59:38 +01001182
1183#if defined(USE_SLZ) || defined(USE_ZLIB)
1184 int ret;
1185
1186 /* flush data here */
Christopher Faulet3d97c902015-12-09 14:59:38 +01001187 if (end)
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001188 ret = st->comp_algo->finish(st->comp_ctx, out); /* end of data */
Christopher Faulet3d97c902015-12-09 14:59:38 +01001189 else
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001190 ret = st->comp_algo->flush(st->comp_ctx, out); /* end of buffer */
Christopher Faulet3d97c902015-12-09 14:59:38 +01001191
1192 if (ret < 0)
1193 return -1; /* flush failed */
1194
1195#endif /* USE_ZLIB */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001196 if (b_data(out) == 0) {
Christopher Faulet3d97c902015-12-09 14:59:38 +01001197 /* No data were appended, let's drop the output buffer and
1198 * keep the input buffer unchanged.
1199 */
1200 return 0;
1201 }
1202
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001203 /* OK so at this stage, we have an output buffer <out> looking like this :
Christopher Faulet3d97c902015-12-09 14:59:38 +01001204 *
1205 * <-- o --> <------ i ----->
1206 * +---------+---+------------+-----------+
1207 * | out | c | comp_in | empty |
1208 * +---------+---+------------+-----------+
1209 * data p size
1210 *
Willy Tarreaud54a8ce2018-06-29 18:42:02 +02001211 * <out> is the room reserved to copy the channel output. It starts at
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001212 * out->area and has not yet been filled. <c> is the room reserved to
Willy Tarreaud54a8ce2018-06-29 18:42:02 +02001213 * write the chunk size (10 bytes). <comp_in> is the compressed
1214 * equivalent of the data part of ib->len. <empty> is the amount of
1215 * empty bytes at the end of the buffer, into which we may have to
1216 * copy the remaining bytes from ib->len after the data
1217 * (chunk size, trailers, ...).
Christopher Faulet3d97c902015-12-09 14:59:38 +01001218 */
1219
Joseph Herlant942eea32018-11-15 13:57:22 -08001220 /* Write real size at the beginning of the chunk, no need of wrapping.
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001221 * We write the chunk using a dynamic length and adjust out->p and out->i
Christopher Faulet3d97c902015-12-09 14:59:38 +01001222 * accordingly afterwards. That will move <out> away from <data>.
1223 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001224 left = http_emit_chunk_size(b_head(out), b_data(out));
1225 b_add(out, left);
Christopher Fauletb61481c2018-12-17 13:17:53 +01001226 out->head -= co_data(chn) + (left);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001227 /* Copy previous data from chn into out */
Willy Tarreaud54a8ce2018-06-29 18:42:02 +02001228 if (co_data(chn) > 0) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001229 left = b_contig_data(&chn->buf, 0);
Christopher Fauletb61481c2018-12-17 13:17:53 +01001230 if (left > co_data(chn))
1231 left = co_data(chn);
Christopher Faulet3d97c902015-12-09 14:59:38 +01001232
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001233 memcpy(b_head(out), co_head(chn), left);
1234 b_add(out, left);
Willy Tarreaud54a8ce2018-06-29 18:42:02 +02001235 if (co_data(chn) - left) {/* second part of the buffer */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001236 memcpy(b_head(out) + left, b_orig(&chn->buf), co_data(chn) - left);
1237 b_add(out, co_data(chn) - left);
Willy Tarreaud54a8ce2018-06-29 18:42:02 +02001238 }
Christopher Faulet3d97c902015-12-09 14:59:38 +01001239 }
1240
1241 /* chunked encoding requires CRLF after data */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001242 tail = b_tail(out);
Christopher Faulet3d97c902015-12-09 14:59:38 +01001243 *tail++ = '\r';
1244 *tail++ = '\n';
1245
Christopher Faulet2fb28802015-12-01 10:40:57 +01001246 /* At the end of data, we must write the empty chunk 0<CRLF>,
1247 * and terminate the trailers section with a last <CRLF>. If
1248 * we're forwarding a chunked-encoded response, we'll have a
1249 * trailers section after the empty chunk which needs to be
1250 * forwarded and which will provide the last CRLF. Otherwise
1251 * we write it ourselves.
1252 */
1253 if (end) {
1254 struct http_msg *msg = &s->txn->rsp;
1255
1256 memcpy(tail, "0\r\n", 3);
1257 tail += 3;
Christopher Fauletb77c5c22015-12-07 16:48:42 +01001258 if (!(msg->flags & HTTP_MSGF_TE_CHNK)) {
Christopher Faulet2fb28802015-12-01 10:40:57 +01001259 memcpy(tail, "\r\n", 2);
1260 tail += 2;
1261 }
1262 }
1263
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001264 b_add(out, tail - b_tail(out));
Christopher Fauletb61481c2018-12-17 13:17:53 +01001265 to_forward = b_data(out) - co_data(chn);
Christopher Faulet3d97c902015-12-09 14:59:38 +01001266
1267 /* update input rate */
Christopher Faulet92d36382015-11-05 13:35:03 +01001268 if (st->comp_ctx && st->comp_ctx->cur_lvl > 0) {
Christopher Faulet2fb28802015-12-01 10:40:57 +01001269 update_freq_ctr(&global.comp_bps_in, st->consumed);
Olivier Houchard43da3432019-03-08 18:50:27 +01001270 _HA_ATOMIC_ADD(&strm_fe(s)->fe_counters.comp_in, st->consumed);
1271 _HA_ATOMIC_ADD(&s->be->be_counters.comp_in, st->consumed);
Christopher Faulet3d97c902015-12-09 14:59:38 +01001272 } else {
Olivier Houchard43da3432019-03-08 18:50:27 +01001273 _HA_ATOMIC_ADD(&strm_fe(s)->fe_counters.comp_byp, st->consumed);
1274 _HA_ATOMIC_ADD(&s->be->be_counters.comp_byp, st->consumed);
Christopher Faulet3d97c902015-12-09 14:59:38 +01001275 }
1276
1277 /* copy the remaining data in the tmp buffer. */
Willy Tarreaubcbd3932018-06-06 07:13:22 +02001278 c_adv(chn, st->consumed);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001279 if (b_data(&chn->buf) - co_data(chn) > 0) {
Willy Tarreau7194d3c2018-06-06 16:55:45 +02001280 left = ci_contig_data(chn);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001281 memcpy(b_tail(out), ci_head(chn), left);
1282 b_add(out, left);
1283 if (b_data(&chn->buf) - (co_data(chn) + left)) {
1284 memcpy(b_tail(out), b_orig(&chn->buf), b_data(&chn->buf) - left);
1285 b_add(out, b_data(&chn->buf) - left);
Christopher Faulet3d97c902015-12-09 14:59:38 +01001286 }
1287 }
Christopher Fauletb61481c2018-12-17 13:17:53 +01001288 c_rew(chn, st->consumed);
1289
Christopher Faulet3d97c902015-12-09 14:59:38 +01001290 /* swap the buffers */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001291 tmp_buf = chn->buf;
1292 chn->buf = *out;
1293 *out = tmp_buf;
1294
Christopher Faulet92d36382015-11-05 13:35:03 +01001295 if (st->comp_ctx && st->comp_ctx->cur_lvl > 0) {
Christopher Faulet3d97c902015-12-09 14:59:38 +01001296 update_freq_ctr(&global.comp_bps_out, to_forward);
Olivier Houchard43da3432019-03-08 18:50:27 +01001297 _HA_ATOMIC_ADD(&strm_fe(s)->fe_counters.comp_out, to_forward);
1298 _HA_ATOMIC_ADD(&s->be->be_counters.comp_out, to_forward);
Christopher Faulet3d97c902015-12-09 14:59:38 +01001299 }
1300
Christopher Faulet3d97c902015-12-09 14:59:38 +01001301 return to_forward;
1302}
1303
Christopher Faulete6902cd2018-11-30 22:29:48 +01001304static int
1305htx_compression_buffer_end(struct comp_state *st, struct buffer *out, int end)
1306{
1307 if (end)
1308 return st->comp_algo->finish(st->comp_ctx, out);
1309 else
1310 return st->comp_algo->flush(st->comp_ctx, out);
1311}
1312
Christopher Faulet3d97c902015-12-09 14:59:38 +01001313
1314/***********************************************************************/
Christopher Faulet92d36382015-11-05 13:35:03 +01001315struct flt_ops comp_ops = {
Christopher Faulete6902cd2018-11-30 22:29:48 +01001316 .init = comp_flt_init,
Christopher Faulet8ca3b4b2017-07-25 11:07:15 +02001317 .init_per_thread = comp_flt_init_per_thread,
1318 .deinit_per_thread = comp_flt_deinit_per_thread,
Christopher Faulet92d36382015-11-05 13:35:03 +01001319
1320 .channel_start_analyze = comp_start_analyze,
Christopher Faulet92d36382015-11-05 13:35:03 +01001321 .channel_end_analyze = comp_end_analyze,
Christopher Faulet3dc860d2017-09-15 11:39:36 +02001322 .channel_post_analyze = comp_http_post_analyze,
Christopher Faulet92d36382015-11-05 13:35:03 +01001323
Christopher Faulet1339d742016-05-11 16:48:33 +02001324 .http_headers = comp_http_headers,
Christopher Faulete6902cd2018-11-30 22:29:48 +01001325 .http_payload = comp_http_payload,
1326 .http_end = comp_http_end,
1327
Christopher Faulet309c6412015-12-02 09:57:32 +01001328 .http_data = comp_http_data,
1329 .http_chunk_trailers = comp_http_chunk_trailers,
1330 .http_forward_data = comp_http_forward_data,
Christopher Faulet92d36382015-11-05 13:35:03 +01001331};
1332
Christopher Faulet3d97c902015-12-09 14:59:38 +01001333static int
1334parse_compression_options(char **args, int section, struct proxy *proxy,
1335 struct proxy *defpx, const char *file, int line,
1336 char **err)
1337{
Christopher Faulet92d36382015-11-05 13:35:03 +01001338 struct comp *comp;
Christopher Faulet3d97c902015-12-09 14:59:38 +01001339
1340 if (proxy->comp == NULL) {
Vincent Bernat02779b62016-04-03 13:48:43 +02001341 comp = calloc(1, sizeof(*comp));
Christopher Faulet3d97c902015-12-09 14:59:38 +01001342 proxy->comp = comp;
1343 }
1344 else
1345 comp = proxy->comp;
1346
1347 if (!strcmp(args[1], "algo")) {
1348 struct comp_ctx *ctx;
1349 int cur_arg = 2;
1350
1351 if (!*args[cur_arg]) {
1352 memprintf(err, "parsing [%s:%d] : '%s' expects <algorithm>\n",
1353 file, line, args[0]);
1354 return -1;
1355 }
1356 while (*(args[cur_arg])) {
1357 if (comp_append_algo(comp, args[cur_arg]) < 0) {
1358 memprintf(err, "'%s' : '%s' is not a supported algorithm.\n",
1359 args[0], args[cur_arg]);
1360 return -1;
1361 }
1362 if (proxy->comp->algos->init(&ctx, 9) == 0)
1363 proxy->comp->algos->end(&ctx);
1364 else {
1365 memprintf(err, "'%s' : Can't init '%s' algorithm.\n",
1366 args[0], args[cur_arg]);
1367 return -1;
1368 }
1369 cur_arg++;
1370 continue;
1371 }
1372 }
1373 else if (!strcmp(args[1], "offload"))
1374 comp->offload = 1;
1375 else if (!strcmp(args[1], "type")) {
1376 int cur_arg = 2;
1377
1378 if (!*args[cur_arg]) {
1379 memprintf(err, "'%s' expects <type>\n", args[0]);
1380 return -1;
1381 }
1382 while (*(args[cur_arg])) {
1383 comp_append_type(comp, args[cur_arg]);
1384 cur_arg++;
1385 continue;
1386 }
1387 }
1388 else {
1389 memprintf(err, "'%s' expects 'algo', 'type' or 'offload'\n",
1390 args[0]);
1391 return -1;
1392 }
1393
1394 return 0;
1395}
1396
Christopher Faulet92d36382015-11-05 13:35:03 +01001397static int
1398parse_http_comp_flt(char **args, int *cur_arg, struct proxy *px,
Thierry Fournier3610c392016-04-13 18:27:51 +02001399 struct flt_conf *fconf, char **err, void *private)
Christopher Faulet92d36382015-11-05 13:35:03 +01001400{
Christopher Faulet443ea1a2016-02-04 13:40:26 +01001401 struct flt_conf *fc, *back;
Christopher Faulet92d36382015-11-05 13:35:03 +01001402
Christopher Faulet443ea1a2016-02-04 13:40:26 +01001403 list_for_each_entry_safe(fc, back, &px->filter_configs, list) {
1404 if (fc->id == http_comp_flt_id) {
Christopher Faulet92d36382015-11-05 13:35:03 +01001405 memprintf(err, "%s: Proxy supports only one compression filter\n", px->id);
1406 return -1;
1407 }
1408 }
1409
Christopher Faulet443ea1a2016-02-04 13:40:26 +01001410 fconf->id = http_comp_flt_id;
1411 fconf->conf = NULL;
1412 fconf->ops = &comp_ops;
Christopher Faulet92d36382015-11-05 13:35:03 +01001413 (*cur_arg)++;
1414
1415 return 0;
1416}
1417
1418
1419int
Christopher Fauletc9df7f72018-12-10 16:14:04 +01001420check_implicit_http_comp_flt(struct proxy *proxy)
Christopher Faulet92d36382015-11-05 13:35:03 +01001421{
Christopher Faulet443ea1a2016-02-04 13:40:26 +01001422 struct flt_conf *fconf;
Christopher Faulet27d93c32018-12-15 22:32:02 +01001423 int explicit = 0;
1424 int comp = 0;
Christopher Faulet92d36382015-11-05 13:35:03 +01001425 int err = 0;
1426
1427 if (proxy->comp == NULL)
1428 goto end;
Christopher Faulet443ea1a2016-02-04 13:40:26 +01001429 if (!LIST_ISEMPTY(&proxy->filter_configs)) {
1430 list_for_each_entry(fconf, &proxy->filter_configs, list) {
1431 if (fconf->id == http_comp_flt_id)
Christopher Faulet27d93c32018-12-15 22:32:02 +01001432 comp = 1;
1433 else if (fconf->id == cache_store_flt_id) {
1434 if (comp) {
1435 ha_alert("config: %s '%s': unable to enable the compression filter "
1436 "before any cache filter.\n",
1437 proxy_type_str(proxy), proxy->id);
1438 err++;
1439 goto end;
1440 }
1441 }
1442 else
1443 explicit = 1;
Christopher Faulet92d36382015-11-05 13:35:03 +01001444 }
Christopher Faulet27d93c32018-12-15 22:32:02 +01001445 }
1446 if (comp)
1447 goto end;
1448 else if (explicit) {
1449 ha_alert("config: %s '%s': require an explicit filter declaration to use "
1450 "HTTP compression\n", proxy_type_str(proxy), proxy->id);
Christopher Faulet92d36382015-11-05 13:35:03 +01001451 err++;
1452 goto end;
1453 }
1454
Christopher Faulet27d93c32018-12-15 22:32:02 +01001455 /* Implicit declaration of the compression filter is always the last
1456 * one */
Christopher Faulet443ea1a2016-02-04 13:40:26 +01001457 fconf = calloc(1, sizeof(*fconf));
1458 if (!fconf) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001459 ha_alert("config: %s '%s': out of memory\n",
1460 proxy_type_str(proxy), proxy->id);
Christopher Faulet92d36382015-11-05 13:35:03 +01001461 err++;
1462 goto end;
1463 }
Christopher Faulet443ea1a2016-02-04 13:40:26 +01001464 fconf->id = http_comp_flt_id;
1465 fconf->conf = NULL;
1466 fconf->ops = &comp_ops;
1467 LIST_ADDQ(&proxy->filter_configs, &fconf->list);
Christopher Faulet92d36382015-11-05 13:35:03 +01001468 end:
1469 return err;
1470}
1471
1472/*
1473 * boolean, returns true if compression is used (either gzip or deflate) in the
1474 * response.
1475 */
Christopher Faulet3d97c902015-12-09 14:59:38 +01001476static int
Christopher Faulet92d36382015-11-05 13:35:03 +01001477smp_fetch_res_comp(const struct arg *args, struct sample *smp, const char *kw,
1478 void *private)
Christopher Faulet3d97c902015-12-09 14:59:38 +01001479{
Willy Tarreaube508f12016-03-10 11:47:01 +01001480 struct http_txn *txn = smp->strm ? smp->strm->txn : NULL;
Christopher Faulet92d36382015-11-05 13:35:03 +01001481
Christopher Faulet3d97c902015-12-09 14:59:38 +01001482 smp->data.type = SMP_T_BOOL;
Christopher Faulet92d36382015-11-05 13:35:03 +01001483 smp->data.u.sint = (txn && (txn->rsp.flags & HTTP_MSGF_COMPRESSING));
Christopher Faulet3d97c902015-12-09 14:59:38 +01001484 return 1;
1485}
1486
Christopher Faulet92d36382015-11-05 13:35:03 +01001487/*
1488 * string, returns algo
1489 */
Christopher Faulet3d97c902015-12-09 14:59:38 +01001490static int
Christopher Faulet92d36382015-11-05 13:35:03 +01001491smp_fetch_res_comp_algo(const struct arg *args, struct sample *smp,
1492 const char *kw, void *private)
Christopher Faulet3d97c902015-12-09 14:59:38 +01001493{
Willy Tarreaube508f12016-03-10 11:47:01 +01001494 struct http_txn *txn = smp->strm ? smp->strm->txn : NULL;
Christopher Faulet92d36382015-11-05 13:35:03 +01001495 struct filter *filter;
1496 struct comp_state *st;
1497
Christopher Faulet03d85532017-09-15 10:14:43 +02001498 if (!txn || !(txn->rsp.flags & HTTP_MSGF_COMPRESSING))
Christopher Faulet3d97c902015-12-09 14:59:38 +01001499 return 0;
1500
Christopher Fauletfcf035c2015-12-03 11:48:03 +01001501 list_for_each_entry(filter, &strm_flt(smp->strm)->filters, list) {
Christopher Faulet443ea1a2016-02-04 13:40:26 +01001502 if (FLT_ID(filter) != http_comp_flt_id)
Christopher Faulet92d36382015-11-05 13:35:03 +01001503 continue;
1504
1505 if (!(st = filter->ctx))
1506 break;
1507
1508 smp->data.type = SMP_T_STR;
1509 smp->flags = SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001510 smp->data.u.str.area = st->comp_algo->cfg_name;
1511 smp->data.u.str.data = st->comp_algo->cfg_name_len;
Christopher Faulet92d36382015-11-05 13:35:03 +01001512 return 1;
1513 }
1514 return 0;
Christopher Faulet3d97c902015-12-09 14:59:38 +01001515}
1516
1517/* Declare the config parser for "compression" keyword */
1518static struct cfg_kw_list cfg_kws = {ILH, {
1519 { CFG_LISTEN, "compression", parse_compression_options },
1520 { 0, NULL, NULL },
1521 }
1522};
1523
Willy Tarreau0108d902018-11-25 19:14:37 +01001524INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
1525
Christopher Faulet92d36382015-11-05 13:35:03 +01001526/* Declare the filter parser for "compression" keyword */
1527static struct flt_kw_list filter_kws = { "COMP", { }, {
Thierry Fournier3610c392016-04-13 18:27:51 +02001528 { "compression", parse_http_comp_flt, NULL },
1529 { NULL, NULL, NULL },
Christopher Faulet92d36382015-11-05 13:35:03 +01001530 }
1531};
1532
Willy Tarreau0108d902018-11-25 19:14:37 +01001533INITCALL1(STG_REGISTER, flt_register_keywords, &filter_kws);
1534
Christopher Faulet3d97c902015-12-09 14:59:38 +01001535/* Note: must not be declared <const> as its list will be overwritten */
1536static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
Christopher Faulet92d36382015-11-05 13:35:03 +01001537 { "res.comp", smp_fetch_res_comp, 0, NULL, SMP_T_BOOL, SMP_USE_HRSHP },
1538 { "res.comp_algo", smp_fetch_res_comp_algo, 0, NULL, SMP_T_STR, SMP_USE_HRSHP },
1539 { /* END */ },
1540 }
1541};
Christopher Faulet3d97c902015-12-09 14:59:38 +01001542
Willy Tarreau0108d902018-11-25 19:14:37 +01001543INITCALL1(STG_REGISTER, sample_register_fetches, &sample_fetch_keywords);