blob: ac3bd08da88aee84099bb0ece7cc0d8c41c61551 [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 Faulet6acd7e42020-03-02 16:20:05 +0100204 struct htx_ret htxret = htx_find_offset(htx, offset);
Christopher Faulete6902cd2018-11-30 22:29:48 +0100205 struct htx_blk *blk;
Christopher Faulete6902cd2018-11-30 22:29:48 +0100206 int ret, consumed = 0, to_forward = 0;
207
Christopher Faulet6acd7e42020-03-02 16:20:05 +0100208 blk = htxret.blk;
209 offset = htxret.ret;
210 for (; blk && len; blk = htx_get_next_blk(htx, blk)) {
Christopher Faulete6902cd2018-11-30 22:29:48 +0100211 enum htx_blk_type type = htx_get_blk_type(blk);
212 uint32_t sz = htx_get_blksz(blk);
213 struct ist v;
214
215 switch (type) {
216 case HTX_BLK_UNUSED:
217 break;
218
219 case HTX_BLK_DATA:
220 v = htx_get_blk_value(htx, blk);
221 v.ptr += offset;
222 v.len -= offset;
223 if (v.len > len)
224 v.len = len;
225 if (htx_compression_buffer_init(htx, &trash) < 0) {
226 msg->chn->flags |= CF_WAKE_WRITE;
227 goto end;
228 }
229 ret = htx_compression_buffer_add_data(st, v.ptr, v.len, &trash);
230 if (ret < 0)
231 goto error;
232 if (htx_compression_buffer_end(st, &trash, 0) < 0)
233 goto error;
234 len -= ret;
235 consumed += ret;
236 to_forward += b_data(&trash);
237 if (ret == sz && !b_data(&trash)) {
238 offset = 0;
239 blk = htx_remove_blk(htx, blk);
240 continue;
241 }
242 v.len = ret;
243 blk = htx_replace_blk_value(htx, blk, v, ist2(b_head(&trash), b_data(&trash)));
244 break;
245
Christopher Faulete6902cd2018-11-30 22:29:48 +0100246 case HTX_BLK_TLR:
Christopher Faulet2d7c5392019-06-03 10:41:26 +0200247 case HTX_BLK_EOT:
Christopher Faulete6902cd2018-11-30 22:29:48 +0100248 case HTX_BLK_EOM:
249 if (msg->flags & HTTP_MSGF_COMPRESSING) {
250 if (htx_compression_buffer_init(htx, &trash) < 0) {
251 msg->chn->flags |= CF_WAKE_WRITE;
252 goto end;
253 }
254 if (htx_compression_buffer_end(st, &trash, 1) < 0)
255 goto error;
Christopher Fauletd238ae32018-12-21 15:10:25 +0100256 if (b_data(&trash)) {
Christopher Faulet86bc8df2019-06-11 10:38:38 +0200257 struct htx_blk *last = htx_add_last_data(htx, ist2(b_head(&trash), b_data(&trash)));
258 if (!last)
259 goto error;
260 blk = htx_get_next_blk(htx, last);
Christopher Fauletd238ae32018-12-21 15:10:25 +0100261 if (!blk)
262 goto error;
263 to_forward += b_data(&trash);
264 }
Christopher Faulete6902cd2018-11-30 22:29:48 +0100265 msg->flags &= ~HTTP_MSGF_COMPRESSING;
266 /* We let the mux add last empty chunk and empty trailers */
267 }
268 /* fall through */
269
270 default:
271 sz -= offset;
272 if (sz > len)
273 sz = len;
274 consumed += sz;
275 to_forward += sz;
276 len -= sz;
277 break;
278 }
279
280 offset = 0;
Christopher Faulete6902cd2018-11-30 22:29:48 +0100281 }
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
Tim Duesterhuseaf65002019-06-17 16:10:07 +0200524 if (http_header_add_tail2(msg, &txn->hdr_idx, "Vary: Accept-Encoding", 21) < 0)
525 goto error;
526
Christopher Faulet27d93c32018-12-15 22:32:02 +0100527 return 1;
528
529 error:
530 st->comp_algo->end(&st->comp_ctx);
531 st->comp_algo = NULL;
532 return 0;
533}
534
535static int
536htx_set_comp_reshdr(struct comp_state *st, struct stream *s, struct http_msg *msg)
537{
538 struct htx *htx = htxbuf(&msg->chn->buf);
Tim Duesterhusb229f012019-01-29 16:38:56 +0100539 struct http_hdr_ctx ctx;
Christopher Faulet27d93c32018-12-15 22:32:02 +0100540
541 /*
542 * Add Content-Encoding header when it's not identity encoding.
543 * RFC 2616 : Identity encoding: This content-coding is used only in the
544 * Accept-Encoding header, and SHOULD NOT be used in the Content-Encoding
545 * header.
546 */
547 if (st->comp_algo->cfg_name_len != 8 || memcmp(st->comp_algo->cfg_name, "identity", 8) != 0) {
548 struct ist v = ist2(st->comp_algo->ua_name, st->comp_algo->ua_name_len);
549
550 if (!http_add_header(htx, ist("Content-Encoding"), v))
551 goto error;
552 }
553
554 /* remove Content-Length header */
555 if (msg->flags & HTTP_MSGF_CNT_LEN) {
Christopher Faulet27d93c32018-12-15 22:32:02 +0100556 ctx.blk = NULL;
557 while (http_find_header(htx, ist("Content-Length"), &ctx, 1))
558 http_remove_header(htx, &ctx);
559 }
560
561 /* add "Transfer-Encoding: chunked" header */
562 if (!(msg->flags & HTTP_MSGF_TE_CHNK)) {
563 if (!http_add_header(htx, ist("Transfer-Encoding"), ist("chunked")))
564 goto error;
565 }
566
Tim Duesterhusb229f012019-01-29 16:38:56 +0100567 /* convert "ETag" header to a weak ETag */
568 ctx.blk = NULL;
569 if (http_find_header(htx, ist("ETag"), &ctx, 1)) {
570 if (ctx.value.ptr[0] == '"') {
571 /* This a strong ETag. Convert it to a weak one. */
572 struct ist v = ist2(trash.area, 0);
573 if (istcat(&v, ist("W/"), trash.size) == -1 || istcat(&v, ctx.value, trash.size) == -1)
574 goto error;
575
576 if (!http_replace_header_value(htx, &ctx, v))
577 goto error;
578 }
579 }
580
Tim Duesterhuseaf65002019-06-17 16:10:07 +0200581 if (!http_add_header(htx, ist("Vary"), ist("Accept-Encoding")))
582 goto error;
583
Christopher Faulet27d93c32018-12-15 22:32:02 +0100584 return 1;
585
586 error:
587 st->comp_algo->end(&st->comp_ctx);
588 st->comp_algo = NULL;
589 return 0;
590}
591
592static int
593set_compression_response_header(struct comp_state *st, struct stream *s, struct http_msg *msg)
594{
595 if (IS_HTX_STRM(s))
596 return htx_set_comp_reshdr(st, s, msg);
597 else
598 return http_set_comp_reshdr(st, s, msg);
599}
600
Christopher Faulet3d97c902015-12-09 14:59:38 +0100601/*
602 * Selects a compression algorithm depending on the client request.
603 */
Christopher Faulete6902cd2018-11-30 22:29:48 +0100604static int
605http_select_comp_reqhdr(struct comp_state *st, struct stream *s, struct http_msg *msg)
Christopher Faulet3d97c902015-12-09 14:59:38 +0100606{
607 struct http_txn *txn = s->txn;
Olivier Houchard0b662842018-06-29 18:16:31 +0200608 struct channel *req = msg->chn;
Christopher Faulet3d97c902015-12-09 14:59:38 +0100609 struct hdr_ctx ctx;
610 struct comp_algo *comp_algo = NULL;
611 struct comp_algo *comp_algo_back = NULL;
612
613 /* Disable compression for older user agents announcing themselves as "Mozilla/4"
614 * unless they are known good (MSIE 6 with XP SP2, or MSIE 7 and later).
615 * See http://zoompf.com/2012/02/lose-the-wait-http-compression for more details.
616 */
617 ctx.idx = 0;
Olivier Houchard0b662842018-06-29 18:16:31 +0200618 if (http_find_header2("User-Agent", 10, ci_head(req), &txn->hdr_idx, &ctx) &&
Christopher Faulet3d97c902015-12-09 14:59:38 +0100619 ctx.vlen >= 9 &&
620 memcmp(ctx.line + ctx.val, "Mozilla/4", 9) == 0 &&
621 (ctx.vlen < 31 ||
622 memcmp(ctx.line + ctx.val + 25, "MSIE ", 5) != 0 ||
623 ctx.line[ctx.val + 30] < '6' ||
624 (ctx.line[ctx.val + 30] == '6' &&
625 (ctx.vlen < 54 || memcmp(ctx.line + 51, "SV1", 3) != 0)))) {
Christopher Faulet92d36382015-11-05 13:35:03 +0100626 st->comp_algo = NULL;
Christopher Faulet3d97c902015-12-09 14:59:38 +0100627 return 0;
628 }
629
630 /* search for the algo in the backend in priority or the frontend */
Christopher Faulet92d36382015-11-05 13:35:03 +0100631 if ((s->be->comp && (comp_algo_back = s->be->comp->algos)) ||
632 (strm_fe(s)->comp && (comp_algo_back = strm_fe(s)->comp->algos))) {
Christopher Faulet3d97c902015-12-09 14:59:38 +0100633 int best_q = 0;
634
635 ctx.idx = 0;
Olivier Houchard0b662842018-06-29 18:16:31 +0200636 while (http_find_header2("Accept-Encoding", 15, ci_head(req), &txn->hdr_idx, &ctx)) {
Christopher Faulet3d97c902015-12-09 14:59:38 +0100637 const char *qval;
638 int q;
639 int toklen;
640
641 /* try to isolate the token from the optional q-value */
642 toklen = 0;
Willy Tarreau2235b262016-11-05 15:50:20 +0100643 while (toklen < ctx.vlen && HTTP_IS_TOKEN(*(ctx.line + ctx.val + toklen)))
Christopher Faulet3d97c902015-12-09 14:59:38 +0100644 toklen++;
645
646 qval = ctx.line + ctx.val + toklen;
647 while (1) {
Willy Tarreau2235b262016-11-05 15:50:20 +0100648 while (qval < ctx.line + ctx.val + ctx.vlen && HTTP_IS_LWS(*qval))
Christopher Faulet3d97c902015-12-09 14:59:38 +0100649 qval++;
650
651 if (qval >= ctx.line + ctx.val + ctx.vlen || *qval != ';') {
652 qval = NULL;
653 break;
654 }
655 qval++;
656
Willy Tarreau2235b262016-11-05 15:50:20 +0100657 while (qval < ctx.line + ctx.val + ctx.vlen && HTTP_IS_LWS(*qval))
Christopher Faulet3d97c902015-12-09 14:59:38 +0100658 qval++;
659
660 if (qval >= ctx.line + ctx.val + ctx.vlen) {
661 qval = NULL;
662 break;
663 }
664 if (strncmp(qval, "q=", MIN(ctx.line + ctx.val + ctx.vlen - qval, 2)) == 0)
665 break;
666
667 while (qval < ctx.line + ctx.val + ctx.vlen && *qval != ';')
668 qval++;
669 }
670
671 /* here we have qval pointing to the first "q=" attribute or NULL if not found */
Willy Tarreauab813a42018-09-10 18:41:28 +0200672 q = qval ? http_parse_qvalue(qval + 2, NULL) : 1000;
Christopher Faulet3d97c902015-12-09 14:59:38 +0100673
674 if (q <= best_q)
675 continue;
676
677 for (comp_algo = comp_algo_back; comp_algo; comp_algo = comp_algo->next) {
678 if (*(ctx.line + ctx.val) == '*' ||
679 word_match(ctx.line + ctx.val, toklen, comp_algo->ua_name, comp_algo->ua_name_len)) {
Christopher Faulet92d36382015-11-05 13:35:03 +0100680 st->comp_algo = comp_algo;
Christopher Faulet3d97c902015-12-09 14:59:38 +0100681 best_q = q;
682 break;
683 }
684 }
685 }
686 }
687
688 /* remove all occurrences of the header when "compression offload" is set */
Christopher Faulet92d36382015-11-05 13:35:03 +0100689 if (st->comp_algo) {
690 if ((s->be->comp && s->be->comp->offload) ||
691 (strm_fe(s)->comp && strm_fe(s)->comp->offload)) {
Christopher Faulet3d97c902015-12-09 14:59:38 +0100692 http_remove_header2(msg, &txn->hdr_idx, &ctx);
693 ctx.idx = 0;
Olivier Houchard0b662842018-06-29 18:16:31 +0200694 while (http_find_header2("Accept-Encoding", 15, ci_head(req), &txn->hdr_idx, &ctx)) {
Christopher Faulet3d97c902015-12-09 14:59:38 +0100695 http_remove_header2(msg, &txn->hdr_idx, &ctx);
696 }
697 }
Christopher Faulete6902cd2018-11-30 22:29:48 +0100698 return 1;
699 }
700
701 /* identity is implicit does not require headers */
702 if ((s->be->comp && (comp_algo_back = s->be->comp->algos)) ||
703 (strm_fe(s)->comp && (comp_algo_back = strm_fe(s)->comp->algos))) {
704 for (comp_algo = comp_algo_back; comp_algo; comp_algo = comp_algo->next) {
705 if (comp_algo->cfg_name_len == 8 && memcmp(comp_algo->cfg_name, "identity", 8) == 0) {
706 st->comp_algo = comp_algo;
707 return 1;
708 }
709 }
710 }
711
712 st->comp_algo = NULL;
713 return 0;
714}
715
716static int
717htx_select_comp_reqhdr(struct comp_state *st, struct stream *s, struct http_msg *msg)
718{
Christopher Faulet27ba2dc2018-12-05 11:53:24 +0100719 struct htx *htx = htxbuf(&msg->chn->buf);
Christopher Faulete6902cd2018-11-30 22:29:48 +0100720 struct http_hdr_ctx ctx;
721 struct comp_algo *comp_algo = NULL;
722 struct comp_algo *comp_algo_back = NULL;
723
724 /* Disable compression for older user agents announcing themselves as "Mozilla/4"
725 * unless they are known good (MSIE 6 with XP SP2, or MSIE 7 and later).
726 * See http://zoompf.com/2012/02/lose-the-wait-http-compression for more details.
727 */
728 ctx.blk = NULL;
729 if (http_find_header(htx, ist("User-Agent"), &ctx, 1) &&
730 ctx.value.len >= 9 &&
731 memcmp(ctx.value.ptr, "Mozilla/4", 9) == 0 &&
732 (ctx.value.len < 31 ||
733 memcmp(ctx.value.ptr + 25, "MSIE ", 5) != 0 ||
734 *(ctx.value.ptr + 30) < '6' ||
735 (*(ctx.value.ptr + 30) == '6' &&
736 (ctx.value.len < 54 || memcmp(ctx.value.ptr + 51, "SV1", 3) != 0)))) {
737 st->comp_algo = NULL;
738 return 0;
739 }
740
741 /* search for the algo in the backend in priority or the frontend */
742 if ((s->be->comp && (comp_algo_back = s->be->comp->algos)) ||
743 (strm_fe(s)->comp && (comp_algo_back = strm_fe(s)->comp->algos))) {
744 int best_q = 0;
745
746 ctx.blk = NULL;
747 while (http_find_header(htx, ist("Accept-Encoding"), &ctx, 0)) {
748 const char *qval;
749 int q;
750 int toklen;
751
752 /* try to isolate the token from the optional q-value */
753 toklen = 0;
754 while (toklen < ctx.value.len && HTTP_IS_TOKEN(*(ctx.value.ptr + toklen)))
755 toklen++;
756
757 qval = ctx.value.ptr + toklen;
758 while (1) {
759 while (qval < ctx.value.ptr + ctx.value.len && HTTP_IS_LWS(*qval))
760 qval++;
761
762 if (qval >= ctx.value.ptr + ctx.value.len || *qval != ';') {
763 qval = NULL;
764 break;
765 }
766 qval++;
767
768 while (qval < ctx.value.ptr + ctx.value.len && HTTP_IS_LWS(*qval))
769 qval++;
770
771 if (qval >= ctx.value.ptr + ctx.value.len) {
772 qval = NULL;
773 break;
774 }
775 if (strncmp(qval, "q=", MIN(ctx.value.ptr + ctx.value.len - qval, 2)) == 0)
776 break;
777
778 while (qval < ctx.value.ptr + ctx.value.len && *qval != ';')
779 qval++;
780 }
781
782 /* here we have qval pointing to the first "q=" attribute or NULL if not found */
783 q = qval ? http_parse_qvalue(qval + 2, NULL) : 1000;
784
785 if (q <= best_q)
786 continue;
787
788 for (comp_algo = comp_algo_back; comp_algo; comp_algo = comp_algo->next) {
789 if (*(ctx.value.ptr) == '*' ||
790 word_match(ctx.value.ptr, toklen, comp_algo->ua_name, comp_algo->ua_name_len)) {
791 st->comp_algo = comp_algo;
792 best_q = q;
793 break;
794 }
795 }
796 }
797 }
798
799 /* remove all occurrences of the header when "compression offload" is set */
800 if (st->comp_algo) {
801 if ((s->be->comp && s->be->comp->offload) ||
802 (strm_fe(s)->comp && strm_fe(s)->comp->offload)) {
803 http_remove_header(htx, &ctx);
804 ctx.blk = NULL;
805 while (http_find_header(htx, ist("Accept-Encoding"), &ctx, 1))
806 http_remove_header(htx, &ctx);
807 }
Christopher Faulet3d97c902015-12-09 14:59:38 +0100808 return 1;
809 }
810
811 /* identity is implicit does not require headers */
Christopher Faulet92d36382015-11-05 13:35:03 +0100812 if ((s->be->comp && (comp_algo_back = s->be->comp->algos)) ||
813 (strm_fe(s)->comp && (comp_algo_back = strm_fe(s)->comp->algos))) {
Christopher Faulet3d97c902015-12-09 14:59:38 +0100814 for (comp_algo = comp_algo_back; comp_algo; comp_algo = comp_algo->next) {
815 if (comp_algo->cfg_name_len == 8 && memcmp(comp_algo->cfg_name, "identity", 8) == 0) {
Christopher Faulet92d36382015-11-05 13:35:03 +0100816 st->comp_algo = comp_algo;
Christopher Faulet3d97c902015-12-09 14:59:38 +0100817 return 1;
818 }
819 }
820 }
821
Christopher Faulet92d36382015-11-05 13:35:03 +0100822 st->comp_algo = NULL;
Christopher Faulet3d97c902015-12-09 14:59:38 +0100823 return 0;
824}
825
Christopher Faulete6902cd2018-11-30 22:29:48 +0100826static int
827select_compression_request_header(struct comp_state *st, struct stream *s,
828 struct http_msg *msg)
829{
830 if (IS_HTX_STRM(s))
831 return htx_select_comp_reqhdr(st, s, msg);
832 else
833 return http_select_comp_reqhdr(st, s, msg);
834}
Christopher Faulet92d36382015-11-05 13:35:03 +0100835
Christopher Faulet3d97c902015-12-09 14:59:38 +0100836/*
837 * Selects a comression algorithm depending of the server response.
838 */
Christopher Faulet92d36382015-11-05 13:35:03 +0100839static int
Christopher Faulete6902cd2018-11-30 22:29:48 +0100840http_select_comp_reshdr(struct comp_state *st, struct stream *s, struct http_msg *msg)
Christopher Faulet3d97c902015-12-09 14:59:38 +0100841{
842 struct http_txn *txn = s->txn;
Olivier Houchard0b662842018-06-29 18:16:31 +0200843 struct channel *c = msg->chn;
Christopher Faulet3d97c902015-12-09 14:59:38 +0100844 struct hdr_ctx ctx;
845 struct comp_type *comp_type;
846
847 /* no common compression algorithm was found in request header */
Christopher Faulet92d36382015-11-05 13:35:03 +0100848 if (st->comp_algo == NULL)
Christopher Faulet3d97c902015-12-09 14:59:38 +0100849 goto fail;
850
Christopher Faulet1d3613a2019-01-07 14:41:59 +0100851 /* compression already in progress */
852 if (msg->flags & HTTP_MSGF_COMPRESSING)
853 goto fail;
854
Christopher Faulet3d97c902015-12-09 14:59:38 +0100855 /* HTTP < 1.1 should not be compressed */
856 if (!(msg->flags & HTTP_MSGF_VER_11) || !(txn->req.flags & HTTP_MSGF_VER_11))
857 goto fail;
858
Christopher Faulet92d36382015-11-05 13:35:03 +0100859 if (txn->meth == HTTP_METH_HEAD)
860 goto fail;
861
Christopher Faulet3d97c902015-12-09 14:59:38 +0100862 /* compress 200,201,202,203 responses only */
863 if ((txn->status != 200) &&
864 (txn->status != 201) &&
865 (txn->status != 202) &&
866 (txn->status != 203))
867 goto fail;
868
869
870 /* Content-Length is null */
871 if (!(msg->flags & HTTP_MSGF_TE_CHNK) && msg->body_len == 0)
872 goto fail;
873
874 /* content is already compressed */
875 ctx.idx = 0;
Olivier Houchard0b662842018-06-29 18:16:31 +0200876 if (http_find_header2("Content-Encoding", 16, ci_head(c), &txn->hdr_idx, &ctx))
Christopher Faulet3d97c902015-12-09 14:59:38 +0100877 goto fail;
878
879 /* no compression when Cache-Control: no-transform is present in the message */
880 ctx.idx = 0;
Olivier Houchard0b662842018-06-29 18:16:31 +0200881 while (http_find_header2("Cache-Control", 13, ci_head(c), &txn->hdr_idx, &ctx)) {
Christopher Faulet3d97c902015-12-09 14:59:38 +0100882 if (word_match(ctx.line + ctx.val, ctx.vlen, "no-transform", 12))
883 goto fail;
884 }
885
Tim Duesterhusb229f012019-01-29 16:38:56 +0100886 /* no compression when ETag is malformed */
887 ctx.idx = 0;
888 if (http_find_full_header2("ETag", 4, ci_head(c), &txn->hdr_idx, &ctx)) {
889 if (!(((ctx.vlen >= 4 && memcmp(ctx.line + ctx.val, "W/\"", 3) == 0) || /* Either a weak ETag */
890 (ctx.vlen >= 2 && ctx.line[ctx.val] == '"')) && /* or strong ETag */
891 ctx.line[ctx.val + ctx.vlen - 1] == '"')) {
892 goto fail;
893 }
894 }
895 /* no compression when multiple ETags are present
896 * Note: Do not reset ctx.idx!
897 */
898 if (http_find_full_header2("ETag", 4, ci_head(c), &txn->hdr_idx, &ctx))
899 goto fail;
900
Christopher Faulet3d97c902015-12-09 14:59:38 +0100901 comp_type = NULL;
902
903 /* we don't want to compress multipart content-types, nor content-types that are
904 * not listed in the "compression type" directive if any. If no content-type was
905 * found but configuration requires one, we don't compress either. Backend has
906 * the priority.
907 */
908 ctx.idx = 0;
Olivier Houchard0b662842018-06-29 18:16:31 +0200909 if (http_find_header2("Content-Type", 12, ci_head(c), &txn->hdr_idx, &ctx)) {
Christopher Faulet3d97c902015-12-09 14:59:38 +0100910 if (ctx.vlen >= 9 && strncasecmp("multipart", ctx.line+ctx.val, 9) == 0)
911 goto fail;
912
913 if ((s->be->comp && (comp_type = s->be->comp->types)) ||
914 (strm_fe(s)->comp && (comp_type = strm_fe(s)->comp->types))) {
915 for (; comp_type; comp_type = comp_type->next) {
916 if (ctx.vlen >= comp_type->name_len &&
917 strncasecmp(ctx.line+ctx.val, comp_type->name, comp_type->name_len) == 0)
918 /* this Content-Type should be compressed */
919 break;
920 }
921 /* this Content-Type should not be compressed */
922 if (comp_type == NULL)
923 goto fail;
924 }
925 }
926 else { /* no content-type header */
Christopher Faulet92d36382015-11-05 13:35:03 +0100927 if ((s->be->comp && s->be->comp->types) ||
928 (strm_fe(s)->comp && strm_fe(s)->comp->types))
Christopher Faulet3d97c902015-12-09 14:59:38 +0100929 goto fail; /* a content-type was required */
930 }
931
932 /* limit compression rate */
933 if (global.comp_rate_lim > 0)
934 if (read_freq_ctr(&global.comp_bps_in) > global.comp_rate_lim)
935 goto fail;
936
937 /* limit cpu usage */
Willy Tarreau81036f22019-05-20 19:24:50 +0200938 if (ti->idle_pct < compress_min_idle)
Christopher Faulet3d97c902015-12-09 14:59:38 +0100939 goto fail;
940
941 /* initialize compression */
Christopher Faulet92d36382015-11-05 13:35:03 +0100942 if (st->comp_algo->init(&st->comp_ctx, global.tune.comp_maxlevel) < 0)
Christopher Faulet3d97c902015-12-09 14:59:38 +0100943 goto fail;
Christopher Faulet92d36382015-11-05 13:35:03 +0100944 msg->flags |= HTTP_MSGF_COMPRESSING;
Christopher Faulet3d97c902015-12-09 14:59:38 +0100945 return 1;
946
947fail:
Christopher Faulet92d36382015-11-05 13:35:03 +0100948 st->comp_algo = NULL;
Christopher Faulet3d97c902015-12-09 14:59:38 +0100949 return 0;
950}
951
Christopher Faulete6902cd2018-11-30 22:29:48 +0100952static int
953htx_select_comp_reshdr(struct comp_state *st, struct stream *s, struct http_msg *msg)
954{
Christopher Faulet27ba2dc2018-12-05 11:53:24 +0100955 struct htx *htx = htxbuf(&msg->chn->buf);
Christopher Faulete6902cd2018-11-30 22:29:48 +0100956 struct http_txn *txn = s->txn;
957 struct http_hdr_ctx ctx;
958 struct comp_type *comp_type;
959
960 /* no common compression algorithm was found in request header */
961 if (st->comp_algo == NULL)
962 goto fail;
963
Christopher Faulet1d3613a2019-01-07 14:41:59 +0100964 /* compression already in progress */
965 if (msg->flags & HTTP_MSGF_COMPRESSING)
966 goto fail;
967
Christopher Faulete6902cd2018-11-30 22:29:48 +0100968 /* HTTP < 1.1 should not be compressed */
969 if (!(msg->flags & HTTP_MSGF_VER_11) || !(txn->req.flags & HTTP_MSGF_VER_11))
970 goto fail;
971
972 if (txn->meth == HTTP_METH_HEAD)
973 goto fail;
974
975 /* compress 200,201,202,203 responses only */
976 if ((txn->status != 200) &&
977 (txn->status != 201) &&
978 (txn->status != 202) &&
979 (txn->status != 203))
980 goto fail;
981
Christopher Fauletc963eb22018-12-21 14:53:54 +0100982 if (!(msg->flags & HTTP_MSGF_XFER_LEN) || msg->flags & HTTP_MSGF_BODYLESS)
Christopher Faulete6902cd2018-11-30 22:29:48 +0100983 goto fail;
984
985 /* content is already compressed */
986 ctx.blk = NULL;
987 if (http_find_header(htx, ist("Content-Encoding"), &ctx, 1))
988 goto fail;
989
990 /* no compression when Cache-Control: no-transform is present in the message */
991 ctx.blk = NULL;
992 while (http_find_header(htx, ist("Cache-Control"), &ctx, 0)) {
993 if (word_match(ctx.value.ptr, ctx.value.len, "no-transform", 12))
994 goto fail;
995 }
996
Tim Duesterhusb229f012019-01-29 16:38:56 +0100997 /* no compression when ETag is malformed */
998 ctx.blk = NULL;
999 if (http_find_header(htx, ist("ETag"), &ctx, 1)) {
1000 if (!(((ctx.value.len >= 4 && memcmp(ctx.value.ptr, "W/\"", 3) == 0) || /* Either a weak ETag */
1001 (ctx.value.len >= 2 && ctx.value.ptr[0] == '"')) && /* or strong ETag */
1002 ctx.value.ptr[ctx.value.len - 1] == '"')) {
1003 goto fail;
1004 }
1005 }
1006 /* no compression when multiple ETags are present
1007 * Note: Do not reset ctx.blk!
1008 */
1009 if (http_find_header(htx, ist("ETag"), &ctx, 1))
1010 goto fail;
1011
Christopher Faulete6902cd2018-11-30 22:29:48 +01001012 comp_type = NULL;
1013
1014 /* we don't want to compress multipart content-types, nor content-types that are
1015 * not listed in the "compression type" directive if any. If no content-type was
1016 * found but configuration requires one, we don't compress either. Backend has
1017 * the priority.
1018 */
1019 ctx.blk = NULL;
1020 if (http_find_header(htx, ist("Content-Type"), &ctx, 1)) {
1021 if (ctx.value.len >= 9 && strncasecmp("multipart", ctx.value.ptr, 9) == 0)
1022 goto fail;
1023
1024 if ((s->be->comp && (comp_type = s->be->comp->types)) ||
1025 (strm_fe(s)->comp && (comp_type = strm_fe(s)->comp->types))) {
1026 for (; comp_type; comp_type = comp_type->next) {
1027 if (ctx.value.len >= comp_type->name_len &&
1028 strncasecmp(ctx.value.ptr, comp_type->name, comp_type->name_len) == 0)
1029 /* this Content-Type should be compressed */
1030 break;
1031 }
1032 /* this Content-Type should not be compressed */
1033 if (comp_type == NULL)
1034 goto fail;
1035 }
1036 }
1037 else { /* no content-type header */
1038 if ((s->be->comp && s->be->comp->types) ||
1039 (strm_fe(s)->comp && strm_fe(s)->comp->types))
1040 goto fail; /* a content-type was required */
1041 }
1042
1043 /* limit compression rate */
1044 if (global.comp_rate_lim > 0)
1045 if (read_freq_ctr(&global.comp_bps_in) > global.comp_rate_lim)
1046 goto fail;
1047
1048 /* limit cpu usage */
Willy Tarreau81036f22019-05-20 19:24:50 +02001049 if (ti->idle_pct < compress_min_idle)
Christopher Faulete6902cd2018-11-30 22:29:48 +01001050 goto fail;
1051
1052 /* initialize compression */
1053 if (st->comp_algo->init(&st->comp_ctx, global.tune.comp_maxlevel) < 0)
1054 goto fail;
Christopher Faulete6902cd2018-11-30 22:29:48 +01001055 msg->flags |= HTTP_MSGF_COMPRESSING;
1056 return 1;
1057
1058 deinit_comp_ctx:
1059 st->comp_algo->end(&st->comp_ctx);
1060 fail:
1061 st->comp_algo = NULL;
1062 return 0;
1063}
1064
1065static int
1066select_compression_response_header(struct comp_state *st, struct stream *s, struct http_msg *msg)
1067{
1068 if (IS_HTX_STRM(s))
1069 return htx_select_comp_reshdr(st, s, msg);
1070 else
1071 return http_select_comp_reshdr(st, s, msg);
1072}
Christopher Faulet3d97c902015-12-09 14:59:38 +01001073/***********************************************************************/
1074/* emit the chunksize followed by a CRLF on the output and return the number of
1075 * bytes written. It goes backwards and starts with the byte before <end>. It
1076 * returns the number of bytes written which will not exceed 10 (8 digits, CR,
1077 * and LF). The caller is responsible for ensuring there is enough room left in
1078 * the output buffer for the string.
1079 */
1080static int
1081http_emit_chunk_size(char *end, unsigned int chksz)
1082{
1083 char *beg = end;
1084
1085 *--beg = '\n';
1086 *--beg = '\r';
1087 do {
1088 *--beg = hextab[chksz & 0xF];
1089 } while (chksz >>= 4);
1090 return end - beg;
1091}
1092
1093/*
1094 * Init HTTP compression
1095 */
Christopher Faulet92d36382015-11-05 13:35:03 +01001096static int
Christopher Fauletb61481c2018-12-17 13:17:53 +01001097http_compression_buffer_init(struct channel *inc, struct buffer *out)
Christopher Faulet3d97c902015-12-09 14:59:38 +01001098{
1099 /* output stream requires at least 10 bytes for the gzip header, plus
1100 * at least 8 bytes for the gzip trailer (crc+len), plus a possible
1101 * plus at most 5 bytes per 32kB block and 2 bytes to close the stream.
1102 */
Olivier Houchard0b662842018-06-29 18:16:31 +02001103 if (c_room(inc) < 20 + 5 * ((ci_data(inc) + 32767) >> 15))
Christopher Faulet3d97c902015-12-09 14:59:38 +01001104 return -1;
1105
1106 /* prepare an empty output buffer in which we reserve enough room for
1107 * copying the output bytes from <in>, plus 10 extra bytes to write
1108 * the chunk size. We don't copy the bytes yet so that if we have to
1109 * cancel the operation later, it's cheap.
1110 */
1111 b_reset(out);
Christopher Fauletb61481c2018-12-17 13:17:53 +01001112 out->head += co_data(inc) + 10;
Christopher Faulet3d97c902015-12-09 14:59:38 +01001113 return 0;
1114}
1115
Christopher Faulete6902cd2018-11-30 22:29:48 +01001116static int
1117htx_compression_buffer_init(struct htx *htx, struct buffer *out)
1118{
1119 /* output stream requires at least 10 bytes for the gzip header, plus
1120 * at least 8 bytes for the gzip trailer (crc+len), plus a possible
1121 * plus at most 5 bytes per 32kB block and 2 bytes to close the stream.
1122 */
1123 if (htx_free_space(htx) < 20 + 5 * ((htx->data + 32767) >> 15))
1124 return -1;
1125 b_reset(out);
1126 return 0;
1127}
1128
Christopher Faulet3d97c902015-12-09 14:59:38 +01001129/*
1130 * Add data to compress
1131 */
Christopher Faulet92d36382015-11-05 13:35:03 +01001132static int
1133http_compression_buffer_add_data(struct comp_state *st, struct buffer *in,
Willy Tarreaud54a8ce2018-06-29 18:42:02 +02001134 int in_out, struct buffer *out, int sz)
Christopher Faulet3d97c902015-12-09 14:59:38 +01001135{
Christopher Faulet3d97c902015-12-09 14:59:38 +01001136 int consumed_data = 0;
1137 int data_process_len;
1138 int block1, block2;
1139
Christopher Faulet92d36382015-11-05 13:35:03 +01001140 if (!sz)
Christopher Faulet3e7bc672015-12-07 13:39:08 +01001141 goto end;
Christopher Faulet3d97c902015-12-09 14:59:38 +01001142
Christopher Faulet92d36382015-11-05 13:35:03 +01001143 /* select the smallest size between the announced chunk size, the input
Christopher Faulet3d97c902015-12-09 14:59:38 +01001144 * data, and the available output buffer size. The compressors are
Christopher Faulet92d36382015-11-05 13:35:03 +01001145 * assumed to be able to process all the bytes we pass to them at
1146 * once. */
Willy Tarreaueac52592018-06-15 13:59:36 +02001147 data_process_len = MIN(b_room(out), sz);
Christopher Faulet92d36382015-11-05 13:35:03 +01001148
Christopher Faulet3d97c902015-12-09 14:59:38 +01001149 block1 = data_process_len;
Willy Tarreaud54a8ce2018-06-29 18:42:02 +02001150 if (block1 > b_contig_data(in, in_out))
1151 block1 = b_contig_data(in, in_out);
Christopher Faulet3d97c902015-12-09 14:59:38 +01001152 block2 = data_process_len - block1;
1153
1154 /* compressors return < 0 upon error or the amount of bytes read */
Christopher Faulet96667202018-12-17 12:02:57 +01001155 consumed_data = st->comp_algo->add_data(st->comp_ctx, b_peek(in, in_out), block1, out);
Christopher Faulet3e7bc672015-12-07 13:39:08 +01001156 if (consumed_data != block1 || !block2)
1157 goto end;
Christopher Faulet96667202018-12-17 12:02:57 +01001158 consumed_data = st->comp_algo->add_data(st->comp_ctx, b_orig(in), block2, out);
Christopher Faulet3e7bc672015-12-07 13:39:08 +01001159 if (consumed_data < 0)
1160 goto end;
1161 consumed_data += block1;
1162
1163 end:
Christopher Faulet3d97c902015-12-09 14:59:38 +01001164 return consumed_data;
1165}
1166
Christopher Faulete6902cd2018-11-30 22:29:48 +01001167static int
1168htx_compression_buffer_add_data(struct comp_state *st, const char *data, size_t len,
1169 struct buffer *out)
1170{
1171 return st->comp_algo->add_data(st->comp_ctx, data, len, out);
1172}
1173
Christopher Faulet3d97c902015-12-09 14:59:38 +01001174/*
1175 * Flush data in process, and write the header and footer of the chunk. Upon
1176 * success, in and out buffers are swapped to avoid a copy.
1177 */
Christopher Faulet92d36382015-11-05 13:35:03 +01001178static int
1179http_compression_buffer_end(struct comp_state *st, struct stream *s,
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001180 struct channel *chn, struct buffer *out,
Christopher Fauletb61481c2018-12-17 13:17:53 +01001181 int end)
Christopher Faulet3d97c902015-12-09 14:59:38 +01001182{
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001183 struct buffer tmp_buf;
Christopher Faulet3d97c902015-12-09 14:59:38 +01001184 char *tail;
Christopher Faulet92d36382015-11-05 13:35:03 +01001185 int to_forward, left;
Christopher Faulet3d97c902015-12-09 14:59:38 +01001186
1187#if defined(USE_SLZ) || defined(USE_ZLIB)
1188 int ret;
1189
1190 /* flush data here */
Christopher Faulet3d97c902015-12-09 14:59:38 +01001191 if (end)
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001192 ret = st->comp_algo->finish(st->comp_ctx, out); /* end of data */
Christopher Faulet3d97c902015-12-09 14:59:38 +01001193 else
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001194 ret = st->comp_algo->flush(st->comp_ctx, out); /* end of buffer */
Christopher Faulet3d97c902015-12-09 14:59:38 +01001195
1196 if (ret < 0)
1197 return -1; /* flush failed */
1198
1199#endif /* USE_ZLIB */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001200 if (b_data(out) == 0) {
Christopher Faulet3d97c902015-12-09 14:59:38 +01001201 /* No data were appended, let's drop the output buffer and
1202 * keep the input buffer unchanged.
1203 */
1204 return 0;
1205 }
1206
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001207 /* OK so at this stage, we have an output buffer <out> looking like this :
Christopher Faulet3d97c902015-12-09 14:59:38 +01001208 *
1209 * <-- o --> <------ i ----->
1210 * +---------+---+------------+-----------+
1211 * | out | c | comp_in | empty |
1212 * +---------+---+------------+-----------+
1213 * data p size
1214 *
Willy Tarreaud54a8ce2018-06-29 18:42:02 +02001215 * <out> is the room reserved to copy the channel output. It starts at
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001216 * out->area and has not yet been filled. <c> is the room reserved to
Willy Tarreaud54a8ce2018-06-29 18:42:02 +02001217 * write the chunk size (10 bytes). <comp_in> is the compressed
1218 * equivalent of the data part of ib->len. <empty> is the amount of
1219 * empty bytes at the end of the buffer, into which we may have to
1220 * copy the remaining bytes from ib->len after the data
1221 * (chunk size, trailers, ...).
Christopher Faulet3d97c902015-12-09 14:59:38 +01001222 */
1223
Joseph Herlant942eea32018-11-15 13:57:22 -08001224 /* Write real size at the beginning of the chunk, no need of wrapping.
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001225 * We write the chunk using a dynamic length and adjust out->p and out->i
Christopher Faulet3d97c902015-12-09 14:59:38 +01001226 * accordingly afterwards. That will move <out> away from <data>.
1227 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001228 left = http_emit_chunk_size(b_head(out), b_data(out));
1229 b_add(out, left);
Christopher Fauletb61481c2018-12-17 13:17:53 +01001230 out->head -= co_data(chn) + (left);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001231 /* Copy previous data from chn into out */
Willy Tarreaud54a8ce2018-06-29 18:42:02 +02001232 if (co_data(chn) > 0) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001233 left = b_contig_data(&chn->buf, 0);
Christopher Fauletb61481c2018-12-17 13:17:53 +01001234 if (left > co_data(chn))
1235 left = co_data(chn);
Christopher Faulet3d97c902015-12-09 14:59:38 +01001236
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001237 memcpy(b_head(out), co_head(chn), left);
1238 b_add(out, left);
Willy Tarreaud54a8ce2018-06-29 18:42:02 +02001239 if (co_data(chn) - left) {/* second part of the buffer */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001240 memcpy(b_head(out) + left, b_orig(&chn->buf), co_data(chn) - left);
1241 b_add(out, co_data(chn) - left);
Willy Tarreaud54a8ce2018-06-29 18:42:02 +02001242 }
Christopher Faulet3d97c902015-12-09 14:59:38 +01001243 }
1244
1245 /* chunked encoding requires CRLF after data */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001246 tail = b_tail(out);
Christopher Faulet3d97c902015-12-09 14:59:38 +01001247 *tail++ = '\r';
1248 *tail++ = '\n';
1249
Christopher Faulet2fb28802015-12-01 10:40:57 +01001250 /* At the end of data, we must write the empty chunk 0<CRLF>,
1251 * and terminate the trailers section with a last <CRLF>. If
1252 * we're forwarding a chunked-encoded response, we'll have a
1253 * trailers section after the empty chunk which needs to be
1254 * forwarded and which will provide the last CRLF. Otherwise
1255 * we write it ourselves.
1256 */
1257 if (end) {
1258 struct http_msg *msg = &s->txn->rsp;
1259
1260 memcpy(tail, "0\r\n", 3);
1261 tail += 3;
Christopher Fauletb77c5c22015-12-07 16:48:42 +01001262 if (!(msg->flags & HTTP_MSGF_TE_CHNK)) {
Christopher Faulet2fb28802015-12-01 10:40:57 +01001263 memcpy(tail, "\r\n", 2);
1264 tail += 2;
1265 }
1266 }
1267
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001268 b_add(out, tail - b_tail(out));
Christopher Fauletb61481c2018-12-17 13:17:53 +01001269 to_forward = b_data(out) - co_data(chn);
Christopher Faulet3d97c902015-12-09 14:59:38 +01001270
1271 /* update input rate */
Christopher Faulet92d36382015-11-05 13:35:03 +01001272 if (st->comp_ctx && st->comp_ctx->cur_lvl > 0) {
Christopher Faulet2fb28802015-12-01 10:40:57 +01001273 update_freq_ctr(&global.comp_bps_in, st->consumed);
Olivier Houchard43da3432019-03-08 18:50:27 +01001274 _HA_ATOMIC_ADD(&strm_fe(s)->fe_counters.comp_in, st->consumed);
1275 _HA_ATOMIC_ADD(&s->be->be_counters.comp_in, st->consumed);
Christopher Faulet3d97c902015-12-09 14:59:38 +01001276 } else {
Olivier Houchard43da3432019-03-08 18:50:27 +01001277 _HA_ATOMIC_ADD(&strm_fe(s)->fe_counters.comp_byp, st->consumed);
1278 _HA_ATOMIC_ADD(&s->be->be_counters.comp_byp, st->consumed);
Christopher Faulet3d97c902015-12-09 14:59:38 +01001279 }
1280
1281 /* copy the remaining data in the tmp buffer. */
Willy Tarreaubcbd3932018-06-06 07:13:22 +02001282 c_adv(chn, st->consumed);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001283 if (b_data(&chn->buf) - co_data(chn) > 0) {
Willy Tarreau7194d3c2018-06-06 16:55:45 +02001284 left = ci_contig_data(chn);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001285 memcpy(b_tail(out), ci_head(chn), left);
1286 b_add(out, left);
1287 if (b_data(&chn->buf) - (co_data(chn) + left)) {
1288 memcpy(b_tail(out), b_orig(&chn->buf), b_data(&chn->buf) - left);
1289 b_add(out, b_data(&chn->buf) - left);
Christopher Faulet3d97c902015-12-09 14:59:38 +01001290 }
1291 }
Christopher Fauletb61481c2018-12-17 13:17:53 +01001292 c_rew(chn, st->consumed);
1293
Christopher Faulet3d97c902015-12-09 14:59:38 +01001294 /* swap the buffers */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001295 tmp_buf = chn->buf;
1296 chn->buf = *out;
1297 *out = tmp_buf;
1298
Christopher Faulet92d36382015-11-05 13:35:03 +01001299 if (st->comp_ctx && st->comp_ctx->cur_lvl > 0) {
Christopher Faulet3d97c902015-12-09 14:59:38 +01001300 update_freq_ctr(&global.comp_bps_out, to_forward);
Olivier Houchard43da3432019-03-08 18:50:27 +01001301 _HA_ATOMIC_ADD(&strm_fe(s)->fe_counters.comp_out, to_forward);
1302 _HA_ATOMIC_ADD(&s->be->be_counters.comp_out, to_forward);
Christopher Faulet3d97c902015-12-09 14:59:38 +01001303 }
1304
Christopher Faulet3d97c902015-12-09 14:59:38 +01001305 return to_forward;
1306}
1307
Christopher Faulete6902cd2018-11-30 22:29:48 +01001308static int
1309htx_compression_buffer_end(struct comp_state *st, struct buffer *out, int end)
1310{
1311 if (end)
1312 return st->comp_algo->finish(st->comp_ctx, out);
1313 else
1314 return st->comp_algo->flush(st->comp_ctx, out);
1315}
1316
Christopher Faulet3d97c902015-12-09 14:59:38 +01001317
1318/***********************************************************************/
Christopher Faulet92d36382015-11-05 13:35:03 +01001319struct flt_ops comp_ops = {
Christopher Faulete6902cd2018-11-30 22:29:48 +01001320 .init = comp_flt_init,
Christopher Faulet8ca3b4b2017-07-25 11:07:15 +02001321 .init_per_thread = comp_flt_init_per_thread,
1322 .deinit_per_thread = comp_flt_deinit_per_thread,
Christopher Faulet92d36382015-11-05 13:35:03 +01001323
1324 .channel_start_analyze = comp_start_analyze,
Christopher Faulet92d36382015-11-05 13:35:03 +01001325 .channel_end_analyze = comp_end_analyze,
Christopher Faulet3dc860d2017-09-15 11:39:36 +02001326 .channel_post_analyze = comp_http_post_analyze,
Christopher Faulet92d36382015-11-05 13:35:03 +01001327
Christopher Faulet1339d742016-05-11 16:48:33 +02001328 .http_headers = comp_http_headers,
Christopher Faulete6902cd2018-11-30 22:29:48 +01001329 .http_payload = comp_http_payload,
1330 .http_end = comp_http_end,
1331
Christopher Faulet309c6412015-12-02 09:57:32 +01001332 .http_data = comp_http_data,
1333 .http_chunk_trailers = comp_http_chunk_trailers,
1334 .http_forward_data = comp_http_forward_data,
Christopher Faulet92d36382015-11-05 13:35:03 +01001335};
1336
Christopher Faulet3d97c902015-12-09 14:59:38 +01001337static int
1338parse_compression_options(char **args, int section, struct proxy *proxy,
1339 struct proxy *defpx, const char *file, int line,
1340 char **err)
1341{
Christopher Faulet92d36382015-11-05 13:35:03 +01001342 struct comp *comp;
Christopher Faulet3d97c902015-12-09 14:59:38 +01001343
1344 if (proxy->comp == NULL) {
Vincent Bernat02779b62016-04-03 13:48:43 +02001345 comp = calloc(1, sizeof(*comp));
Christopher Faulet3d97c902015-12-09 14:59:38 +01001346 proxy->comp = comp;
1347 }
1348 else
1349 comp = proxy->comp;
1350
1351 if (!strcmp(args[1], "algo")) {
1352 struct comp_ctx *ctx;
1353 int cur_arg = 2;
1354
1355 if (!*args[cur_arg]) {
1356 memprintf(err, "parsing [%s:%d] : '%s' expects <algorithm>\n",
1357 file, line, args[0]);
1358 return -1;
1359 }
1360 while (*(args[cur_arg])) {
1361 if (comp_append_algo(comp, args[cur_arg]) < 0) {
1362 memprintf(err, "'%s' : '%s' is not a supported algorithm.\n",
1363 args[0], args[cur_arg]);
1364 return -1;
1365 }
1366 if (proxy->comp->algos->init(&ctx, 9) == 0)
1367 proxy->comp->algos->end(&ctx);
1368 else {
1369 memprintf(err, "'%s' : Can't init '%s' algorithm.\n",
1370 args[0], args[cur_arg]);
1371 return -1;
1372 }
1373 cur_arg++;
1374 continue;
1375 }
1376 }
1377 else if (!strcmp(args[1], "offload"))
1378 comp->offload = 1;
1379 else if (!strcmp(args[1], "type")) {
1380 int cur_arg = 2;
1381
1382 if (!*args[cur_arg]) {
1383 memprintf(err, "'%s' expects <type>\n", args[0]);
1384 return -1;
1385 }
1386 while (*(args[cur_arg])) {
1387 comp_append_type(comp, args[cur_arg]);
1388 cur_arg++;
1389 continue;
1390 }
1391 }
1392 else {
1393 memprintf(err, "'%s' expects 'algo', 'type' or 'offload'\n",
1394 args[0]);
1395 return -1;
1396 }
1397
1398 return 0;
1399}
1400
Christopher Faulet92d36382015-11-05 13:35:03 +01001401static int
1402parse_http_comp_flt(char **args, int *cur_arg, struct proxy *px,
Thierry Fournier3610c392016-04-13 18:27:51 +02001403 struct flt_conf *fconf, char **err, void *private)
Christopher Faulet92d36382015-11-05 13:35:03 +01001404{
Christopher Faulet443ea1a2016-02-04 13:40:26 +01001405 struct flt_conf *fc, *back;
Christopher Faulet92d36382015-11-05 13:35:03 +01001406
Christopher Faulet443ea1a2016-02-04 13:40:26 +01001407 list_for_each_entry_safe(fc, back, &px->filter_configs, list) {
1408 if (fc->id == http_comp_flt_id) {
Christopher Faulet92d36382015-11-05 13:35:03 +01001409 memprintf(err, "%s: Proxy supports only one compression filter\n", px->id);
1410 return -1;
1411 }
1412 }
1413
Christopher Faulet443ea1a2016-02-04 13:40:26 +01001414 fconf->id = http_comp_flt_id;
1415 fconf->conf = NULL;
1416 fconf->ops = &comp_ops;
Christopher Faulet92d36382015-11-05 13:35:03 +01001417 (*cur_arg)++;
1418
1419 return 0;
1420}
1421
1422
1423int
Christopher Fauletc9df7f72018-12-10 16:14:04 +01001424check_implicit_http_comp_flt(struct proxy *proxy)
Christopher Faulet92d36382015-11-05 13:35:03 +01001425{
Christopher Faulet443ea1a2016-02-04 13:40:26 +01001426 struct flt_conf *fconf;
Christopher Faulet27d93c32018-12-15 22:32:02 +01001427 int explicit = 0;
1428 int comp = 0;
Christopher Faulet92d36382015-11-05 13:35:03 +01001429 int err = 0;
1430
1431 if (proxy->comp == NULL)
1432 goto end;
Christopher Faulet443ea1a2016-02-04 13:40:26 +01001433 if (!LIST_ISEMPTY(&proxy->filter_configs)) {
1434 list_for_each_entry(fconf, &proxy->filter_configs, list) {
1435 if (fconf->id == http_comp_flt_id)
Christopher Faulet27d93c32018-12-15 22:32:02 +01001436 comp = 1;
1437 else if (fconf->id == cache_store_flt_id) {
1438 if (comp) {
1439 ha_alert("config: %s '%s': unable to enable the compression filter "
1440 "before any cache filter.\n",
1441 proxy_type_str(proxy), proxy->id);
1442 err++;
1443 goto end;
1444 }
1445 }
1446 else
1447 explicit = 1;
Christopher Faulet92d36382015-11-05 13:35:03 +01001448 }
Christopher Faulet27d93c32018-12-15 22:32:02 +01001449 }
1450 if (comp)
1451 goto end;
1452 else if (explicit) {
1453 ha_alert("config: %s '%s': require an explicit filter declaration to use "
1454 "HTTP compression\n", proxy_type_str(proxy), proxy->id);
Christopher Faulet92d36382015-11-05 13:35:03 +01001455 err++;
1456 goto end;
1457 }
1458
Christopher Faulet27d93c32018-12-15 22:32:02 +01001459 /* Implicit declaration of the compression filter is always the last
1460 * one */
Christopher Faulet443ea1a2016-02-04 13:40:26 +01001461 fconf = calloc(1, sizeof(*fconf));
1462 if (!fconf) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001463 ha_alert("config: %s '%s': out of memory\n",
1464 proxy_type_str(proxy), proxy->id);
Christopher Faulet92d36382015-11-05 13:35:03 +01001465 err++;
1466 goto end;
1467 }
Christopher Faulet443ea1a2016-02-04 13:40:26 +01001468 fconf->id = http_comp_flt_id;
1469 fconf->conf = NULL;
1470 fconf->ops = &comp_ops;
1471 LIST_ADDQ(&proxy->filter_configs, &fconf->list);
Christopher Faulet92d36382015-11-05 13:35:03 +01001472 end:
1473 return err;
1474}
1475
1476/*
1477 * boolean, returns true if compression is used (either gzip or deflate) in the
1478 * response.
1479 */
Christopher Faulet3d97c902015-12-09 14:59:38 +01001480static int
Christopher Faulet92d36382015-11-05 13:35:03 +01001481smp_fetch_res_comp(const struct arg *args, struct sample *smp, const char *kw,
1482 void *private)
Christopher Faulet3d97c902015-12-09 14:59:38 +01001483{
Willy Tarreaube508f12016-03-10 11:47:01 +01001484 struct http_txn *txn = smp->strm ? smp->strm->txn : NULL;
Christopher Faulet92d36382015-11-05 13:35:03 +01001485
Christopher Faulet3d97c902015-12-09 14:59:38 +01001486 smp->data.type = SMP_T_BOOL;
Christopher Faulet92d36382015-11-05 13:35:03 +01001487 smp->data.u.sint = (txn && (txn->rsp.flags & HTTP_MSGF_COMPRESSING));
Christopher Faulet3d97c902015-12-09 14:59:38 +01001488 return 1;
1489}
1490
Christopher Faulet92d36382015-11-05 13:35:03 +01001491/*
1492 * string, returns algo
1493 */
Christopher Faulet3d97c902015-12-09 14:59:38 +01001494static int
Christopher Faulet92d36382015-11-05 13:35:03 +01001495smp_fetch_res_comp_algo(const struct arg *args, struct sample *smp,
1496 const char *kw, void *private)
Christopher Faulet3d97c902015-12-09 14:59:38 +01001497{
Willy Tarreaube508f12016-03-10 11:47:01 +01001498 struct http_txn *txn = smp->strm ? smp->strm->txn : NULL;
Christopher Faulet92d36382015-11-05 13:35:03 +01001499 struct filter *filter;
1500 struct comp_state *st;
1501
Christopher Faulet03d85532017-09-15 10:14:43 +02001502 if (!txn || !(txn->rsp.flags & HTTP_MSGF_COMPRESSING))
Christopher Faulet3d97c902015-12-09 14:59:38 +01001503 return 0;
1504
Christopher Fauletfcf035c2015-12-03 11:48:03 +01001505 list_for_each_entry(filter, &strm_flt(smp->strm)->filters, list) {
Christopher Faulet443ea1a2016-02-04 13:40:26 +01001506 if (FLT_ID(filter) != http_comp_flt_id)
Christopher Faulet92d36382015-11-05 13:35:03 +01001507 continue;
1508
1509 if (!(st = filter->ctx))
1510 break;
1511
1512 smp->data.type = SMP_T_STR;
1513 smp->flags = SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001514 smp->data.u.str.area = st->comp_algo->cfg_name;
1515 smp->data.u.str.data = st->comp_algo->cfg_name_len;
Christopher Faulet92d36382015-11-05 13:35:03 +01001516 return 1;
1517 }
1518 return 0;
Christopher Faulet3d97c902015-12-09 14:59:38 +01001519}
1520
1521/* Declare the config parser for "compression" keyword */
1522static struct cfg_kw_list cfg_kws = {ILH, {
1523 { CFG_LISTEN, "compression", parse_compression_options },
1524 { 0, NULL, NULL },
1525 }
1526};
1527
Willy Tarreau0108d902018-11-25 19:14:37 +01001528INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
1529
Christopher Faulet92d36382015-11-05 13:35:03 +01001530/* Declare the filter parser for "compression" keyword */
1531static struct flt_kw_list filter_kws = { "COMP", { }, {
Thierry Fournier3610c392016-04-13 18:27:51 +02001532 { "compression", parse_http_comp_flt, NULL },
1533 { NULL, NULL, NULL },
Christopher Faulet92d36382015-11-05 13:35:03 +01001534 }
1535};
1536
Willy Tarreau0108d902018-11-25 19:14:37 +01001537INITCALL1(STG_REGISTER, flt_register_keywords, &filter_kws);
1538
Christopher Faulet3d97c902015-12-09 14:59:38 +01001539/* Note: must not be declared <const> as its list will be overwritten */
1540static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
Christopher Faulet92d36382015-11-05 13:35:03 +01001541 { "res.comp", smp_fetch_res_comp, 0, NULL, SMP_T_BOOL, SMP_USE_HRSHP },
1542 { "res.comp_algo", smp_fetch_res_comp_algo, 0, NULL, SMP_T_STR, SMP_USE_HRSHP },
1543 { /* END */ },
1544 }
1545};
Christopher Faulet3d97c902015-12-09 14:59:38 +01001546
Willy Tarreau0108d902018-11-25 19:14:37 +01001547INITCALL1(STG_REGISTER, sample_register_fetches, &sample_fetch_keywords);