blob: f756b20127ddf13382af64bed9053f09dbbf67d1 [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
248 case HTX_BLK_EOD:
249 case HTX_BLK_TLR:
250 case HTX_BLK_EOM:
251 if (msg->flags & HTTP_MSGF_COMPRESSING) {
252 if (htx_compression_buffer_init(htx, &trash) < 0) {
253 msg->chn->flags |= CF_WAKE_WRITE;
254 goto end;
255 }
256 if (htx_compression_buffer_end(st, &trash, 1) < 0)
257 goto error;
Christopher Fauletd238ae32018-12-21 15:10:25 +0100258 if (b_data(&trash)) {
259 blk = htx_add_data_before(htx, blk, ist2(b_head(&trash), b_data(&trash)));
260 if (!blk)
261 goto error;
262 to_forward += b_data(&trash);
263 }
Christopher Faulete6902cd2018-11-30 22:29:48 +0100264 msg->flags &= ~HTTP_MSGF_COMPRESSING;
265 /* We let the mux add last empty chunk and empty trailers */
266 }
267 /* fall through */
268
269 default:
270 sz -= offset;
271 if (sz > len)
272 sz = len;
273 consumed += sz;
274 to_forward += sz;
275 len -= sz;
276 break;
277 }
278
279 offset = 0;
Christopher Faulete6902cd2018-11-30 22:29:48 +0100280 }
281
282 end:
283 if (to_forward != consumed)
284 flt_update_offsets(filter, msg->chn, to_forward - consumed);
285
286 if (st->comp_ctx && st->comp_ctx->cur_lvl > 0) {
Willy Tarreauef6fd852019-02-04 11:48:03 +0100287 update_freq_ctr(&global.comp_bps_in, consumed);
Olivier Houchard43da3432019-03-08 18:50:27 +0100288 _HA_ATOMIC_ADD(&strm_fe(s)->fe_counters.comp_in, consumed);
289 _HA_ATOMIC_ADD(&s->be->be_counters.comp_in, consumed);
Christopher Faulete6902cd2018-11-30 22:29:48 +0100290 update_freq_ctr(&global.comp_bps_out, to_forward);
Olivier Houchard43da3432019-03-08 18:50:27 +0100291 _HA_ATOMIC_ADD(&strm_fe(s)->fe_counters.comp_out, to_forward);
292 _HA_ATOMIC_ADD(&s->be->be_counters.comp_out, to_forward);
Willy Tarreauef6fd852019-02-04 11:48:03 +0100293 } else {
Olivier Houchard43da3432019-03-08 18:50:27 +0100294 _HA_ATOMIC_ADD(&strm_fe(s)->fe_counters.comp_byp, consumed);
295 _HA_ATOMIC_ADD(&s->be->be_counters.comp_byp, consumed);
Christopher Faulete6902cd2018-11-30 22:29:48 +0100296 }
297 return to_forward;
298
299 error:
300 return -1;
301}
302
303static int
Christopher Faulet2fb28802015-12-01 10:40:57 +0100304comp_http_data(struct stream *s, struct filter *filter, struct http_msg *msg)
Christopher Faulet92d36382015-11-05 13:35:03 +0100305{
306 struct comp_state *st = filter->ctx;
Willy Tarreaubcbd3932018-06-06 07:13:22 +0200307 struct channel *chn = msg->chn;
Christopher Faulet3e7bc672015-12-07 13:39:08 +0100308 unsigned int *nxt = &flt_rsp_nxt(filter);
Christopher Faulet2fb28802015-12-01 10:40:57 +0100309 unsigned int len;
Christopher Faulet92d36382015-11-05 13:35:03 +0100310 int ret;
311
Olivier Houchard0b662842018-06-29 18:16:31 +0200312 len = MIN(msg->chunk_len + msg->next, ci_data(chn)) - *nxt;
Christopher Faulet2fb28802015-12-01 10:40:57 +0100313 if (!len)
314 return len;
315
316 if (!st->initialized) {
Christopher Fauletb77c5c22015-12-07 16:48:42 +0100317 unsigned int fwd = flt_rsp_fwd(filter) + st->hdrs_len;
Christopher Faulet3e7bc672015-12-07 13:39:08 +0100318
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200319 b_reset(&tmpbuf);
Willy Tarreaubcbd3932018-06-06 07:13:22 +0200320 c_adv(chn, fwd);
Christopher Fauletb61481c2018-12-17 13:17:53 +0100321 ret = http_compression_buffer_init(chn, &zbuf);
Willy Tarreaubcbd3932018-06-06 07:13:22 +0200322 c_rew(chn, fwd);
Christopher Faulet2fb28802015-12-01 10:40:57 +0100323 if (ret < 0) {
324 msg->chn->flags |= CF_WAKE_WRITE;
325 return 0;
326 }
327 }
Christopher Fauletb77c5c22015-12-07 16:48:42 +0100328
329 if (msg->flags & HTTP_MSGF_TE_CHNK) {
Christopher Faulet06ecf3a2016-09-22 15:31:43 +0200330 int block;
Christopher Fauletb77c5c22015-12-07 16:48:42 +0100331
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200332 len = MIN(b_room(&tmpbuf), len);
Christopher Faulet06ecf3a2016-09-22 15:31:43 +0200333
Willy Tarreaubcbd3932018-06-06 07:13:22 +0200334 c_adv(chn, *nxt);
Willy Tarreau7194d3c2018-06-06 16:55:45 +0200335 block = ci_contig_data(chn);
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200336 memcpy(b_tail(&tmpbuf), ci_head(chn), block);
Christopher Faulet06ecf3a2016-09-22 15:31:43 +0200337 if (len > block)
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200338 memcpy(b_tail(&tmpbuf)+block, b_orig(&chn->buf), len-block);
Willy Tarreaubcbd3932018-06-06 07:13:22 +0200339 c_rew(chn, *nxt);
Christopher Faulet06ecf3a2016-09-22 15:31:43 +0200340
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200341 b_add(&tmpbuf, len);
Christopher Fauletb77c5c22015-12-07 16:48:42 +0100342 ret = len;
343 }
344 else {
Willy Tarreaubcbd3932018-06-06 07:13:22 +0200345 c_adv(chn, *nxt);
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200346 ret = http_compression_buffer_add_data(st, &chn->buf, co_data(chn), &zbuf, len);
Willy Tarreaubcbd3932018-06-06 07:13:22 +0200347 c_rew(chn, *nxt);
Christopher Fauletb77c5c22015-12-07 16:48:42 +0100348 if (ret < 0)
349 return ret;
350 }
Christopher Faulet92d36382015-11-05 13:35:03 +0100351
Christopher Faulet2fb28802015-12-01 10:40:57 +0100352 st->initialized = 1;
353 msg->next += ret;
354 msg->chunk_len -= ret;
Christopher Faulet3e7bc672015-12-07 13:39:08 +0100355 *nxt = msg->next;
Christopher Faulet2fb28802015-12-01 10:40:57 +0100356 return 0;
Christopher Faulet92d36382015-11-05 13:35:03 +0100357}
358
359static int
Christopher Faulet2fb28802015-12-01 10:40:57 +0100360comp_http_chunk_trailers(struct stream *s, struct filter *filter,
361 struct http_msg *msg)
Christopher Faulet92d36382015-11-05 13:35:03 +0100362{
363 struct comp_state *st = filter->ctx;
Christopher Faulet92d36382015-11-05 13:35:03 +0100364
Christopher Fauletb77c5c22015-12-07 16:48:42 +0100365 if (!st->initialized) {
366 if (!st->finished) {
Willy Tarreaubcbd3932018-06-06 07:13:22 +0200367 struct channel *chn = msg->chn;
Christopher Fauletb77c5c22015-12-07 16:48:42 +0100368 unsigned int fwd = flt_rsp_fwd(filter) + st->hdrs_len;
Christopher Faulet2fb28802015-12-01 10:40:57 +0100369
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200370 b_reset(&tmpbuf);
Willy Tarreaubcbd3932018-06-06 07:13:22 +0200371 c_adv(chn, fwd);
Christopher Fauletb61481c2018-12-17 13:17:53 +0100372 http_compression_buffer_init(chn, &zbuf);
Willy Tarreaubcbd3932018-06-06 07:13:22 +0200373 c_rew(chn, fwd);
Christopher Fauletb77c5c22015-12-07 16:48:42 +0100374 st->initialized = 1;
375 }
376 }
377 st->tlrs_len = msg->sol;
Christopher Faulet2fb28802015-12-01 10:40:57 +0100378 return 1;
Christopher Faulet92d36382015-11-05 13:35:03 +0100379}
380
Christopher Faulet2fb28802015-12-01 10:40:57 +0100381
Christopher Faulet92d36382015-11-05 13:35:03 +0100382static int
383comp_http_forward_data(struct stream *s, struct filter *filter,
384 struct http_msg *msg, unsigned int len)
385{
386 struct comp_state *st = filter->ctx;
Christopher Faulet2fb28802015-12-01 10:40:57 +0100387 int ret;
Christopher Faulet92d36382015-11-05 13:35:03 +0100388
Christopher Faulet2fb28802015-12-01 10:40:57 +0100389 /* To work, previous filters MUST forward all data */
Christopher Fauletb77c5c22015-12-07 16:48:42 +0100390 if (flt_rsp_fwd(filter) + len != flt_rsp_nxt(filter)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100391 ha_warning("HTTP compression failed: unexpected behavior of previous filters\n");
Christopher Faulet2fb28802015-12-01 10:40:57 +0100392 return -1;
Christopher Faulet92d36382015-11-05 13:35:03 +0100393 }
394
Christopher Faulet2fb28802015-12-01 10:40:57 +0100395 if (!st->initialized) {
Christopher Fauletb77c5c22015-12-07 16:48:42 +0100396 if (!len) {
Joseph Herlant942eea32018-11-15 13:57:22 -0800397 /* Nothing to forward */
Christopher Fauletb77c5c22015-12-07 16:48:42 +0100398 ret = len;
399 }
400 else if (st->hdrs_len > len) {
401 /* Forward part of headers */
402 ret = len;
403 st->hdrs_len -= len;
404 }
405 else if (st->hdrs_len > 0) {
406 /* Forward remaining headers */
407 ret = st->hdrs_len;
408 st->hdrs_len = 0;
409 }
410 else if (msg->msg_state < HTTP_MSG_TRAILERS) {
411 /* Do not forward anything for now. This only happens
412 * with chunk-encoded responses. Waiting data are part
413 * of the chunk envelope (the chunk size or the chunk
414 * CRLF). These data will be skipped during the
415 * compression. */
416 ret = 0;
417 }
418 else {
419 /* Forward trailers data */
420 ret = len;
421 }
Christopher Faulet2fb28802015-12-01 10:40:57 +0100422 return ret;
Christopher Faulet92d36382015-11-05 13:35:03 +0100423 }
424
Christopher Fauletb77c5c22015-12-07 16:48:42 +0100425 if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200426 ret = http_compression_buffer_add_data(st, &tmpbuf, 0,
427 &zbuf, b_data(&tmpbuf));
428 if (ret != b_data(&tmpbuf)) {
Willy Tarreau506a29a2018-07-18 10:07:58 +0200429 ha_warning("HTTP compression failed: Must consume %u bytes but only %d bytes consumed\n",
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200430 (unsigned int)b_data(&tmpbuf), ret);
Christopher Fauletb77c5c22015-12-07 16:48:42 +0100431 return -1;
432 }
433 }
434
435 st->consumed = len - st->hdrs_len - st->tlrs_len;
Willy Tarreaubcbd3932018-06-06 07:13:22 +0200436 c_adv(msg->chn, flt_rsp_fwd(filter) + st->hdrs_len);
Christopher Fauletb61481c2018-12-17 13:17:53 +0100437 ret = http_compression_buffer_end(st, s, msg->chn, &zbuf, msg->msg_state >= HTTP_MSG_TRAILERS);
Willy Tarreaubcbd3932018-06-06 07:13:22 +0200438 c_rew(msg->chn, flt_rsp_fwd(filter) + st->hdrs_len);
Christopher Faulet2fb28802015-12-01 10:40:57 +0100439 if (ret < 0)
440 return ret;
Christopher Faulet92d36382015-11-05 13:35:03 +0100441
Christopher Fauletb77c5c22015-12-07 16:48:42 +0100442 flt_change_forward_size(filter, msg->chn, ret - st->consumed);
443 msg->next += (ret - st->consumed);
444 ret += st->hdrs_len + st->tlrs_len;
445
Christopher Faulet2fb28802015-12-01 10:40:57 +0100446 st->initialized = 0;
Christopher Fauletb77c5c22015-12-07 16:48:42 +0100447 st->finished = (msg->msg_state >= HTTP_MSG_TRAILERS);
448 st->hdrs_len = 0;
449 st->tlrs_len = 0;
Christopher Faulet92d36382015-11-05 13:35:03 +0100450 return ret;
451}
Christopher Faulet3d97c902015-12-09 14:59:38 +0100452
Christopher Fauletd60b3cf2017-06-26 11:47:13 +0200453static int
454comp_http_end(struct stream *s, struct filter *filter,
455 struct http_msg *msg)
456{
457 struct comp_state *st = filter->ctx;
458
459 if (!(msg->chn->flags & CF_ISRESP) || !st || !st->comp_algo)
460 goto end;
461
462 if (strm_fe(s)->mode == PR_MODE_HTTP)
Olivier Houchard43da3432019-03-08 18:50:27 +0100463 _HA_ATOMIC_ADD(&strm_fe(s)->fe_counters.p.http.comp_rsp, 1);
Christopher Fauletd60b3cf2017-06-26 11:47:13 +0200464 if ((s->flags & SF_BE_ASSIGNED) && (s->be->mode == PR_MODE_HTTP))
Olivier Houchard43da3432019-03-08 18:50:27 +0100465 _HA_ATOMIC_ADD(&s->be->be_counters.p.http.comp_rsp, 1);
Christopher Fauletd60b3cf2017-06-26 11:47:13 +0200466 end:
467 return 1;
468}
Christopher Faulet3d97c902015-12-09 14:59:38 +0100469/***********************************************************************/
Christopher Faulet27d93c32018-12-15 22:32:02 +0100470static int
471http_set_comp_reshdr(struct comp_state *st, struct stream *s, struct http_msg *msg)
472{
473 struct http_txn *txn = s->txn;
Tim Duesterhusb229f012019-01-29 16:38:56 +0100474 struct hdr_ctx ctx;
Christopher Faulet27d93c32018-12-15 22:32:02 +0100475
476 /*
477 * Add Content-Encoding header when it's not identity encoding.
478 * RFC 2616 : Identity encoding: This content-coding is used only in the
479 * Accept-Encoding header, and SHOULD NOT be used in the Content-Encoding
480 * header.
481 */
482 if (st->comp_algo->cfg_name_len != 8 || memcmp(st->comp_algo->cfg_name, "identity", 8) != 0) {
483 trash.data = 18;
484 memcpy(trash.area, "Content-Encoding: ", trash.data);
485 memcpy(trash.area + trash.data, st->comp_algo->ua_name,
486 st->comp_algo->ua_name_len);
487 trash.data += st->comp_algo->ua_name_len;
488 trash.area[trash.data] = '\0';
489 if (http_header_add_tail2(msg, &txn->hdr_idx, trash.area, trash.data) < 0)
490 goto error;
491 }
492
493 /* remove Content-Length header */
494 if (msg->flags & HTTP_MSGF_CNT_LEN) {
Christopher Faulet27d93c32018-12-15 22:32:02 +0100495 ctx.idx = 0;
496 while (http_find_header2("Content-Length", 14, ci_head(&s->res), &txn->hdr_idx, &ctx))
497 http_remove_header2(msg, &txn->hdr_idx, &ctx);
498 }
499
500 /* add Transfer-Encoding header */
501 if (!(msg->flags & HTTP_MSGF_TE_CHNK)) {
502 if (http_header_add_tail2(msg, &txn->hdr_idx, "Transfer-Encoding: chunked", 26) < 0)
503 goto error;
504 }
505
Tim Duesterhusb229f012019-01-29 16:38:56 +0100506 ctx.idx = 0;
507 if (http_find_full_header2("ETag", 4, ci_head(&s->res), &txn->hdr_idx, &ctx)) {
508 if (ctx.line[ctx.val] == '"') {
509 /* This a strong ETag. Convert it to a weak one. */
510 trash.data = 8;
511 if (trash.data + ctx.vlen > trash.size)
512 goto error;
513 memcpy(trash.area, "ETag: W/", trash.data);
514 memcpy(trash.area + trash.data, ctx.line + ctx.val, ctx.vlen);
515 trash.data += ctx.vlen;
516 trash.area[trash.data] = '\0';
517 http_remove_header2(msg, &txn->hdr_idx, &ctx);
518 if (http_header_add_tail2(msg, &txn->hdr_idx, trash.area, trash.data) < 0)
519 goto error;
520 }
521 }
Christopher Faulet27d93c32018-12-15 22:32:02 +0100522
523 return 1;
524
525 error:
526 st->comp_algo->end(&st->comp_ctx);
527 st->comp_algo = NULL;
528 return 0;
529}
530
531static int
532htx_set_comp_reshdr(struct comp_state *st, struct stream *s, struct http_msg *msg)
533{
534 struct htx *htx = htxbuf(&msg->chn->buf);
Tim Duesterhusb229f012019-01-29 16:38:56 +0100535 struct http_hdr_ctx ctx;
Christopher Faulet27d93c32018-12-15 22:32:02 +0100536
537 /*
538 * Add Content-Encoding header when it's not identity encoding.
539 * RFC 2616 : Identity encoding: This content-coding is used only in the
540 * Accept-Encoding header, and SHOULD NOT be used in the Content-Encoding
541 * header.
542 */
543 if (st->comp_algo->cfg_name_len != 8 || memcmp(st->comp_algo->cfg_name, "identity", 8) != 0) {
544 struct ist v = ist2(st->comp_algo->ua_name, st->comp_algo->ua_name_len);
545
546 if (!http_add_header(htx, ist("Content-Encoding"), v))
547 goto error;
548 }
549
550 /* remove Content-Length header */
551 if (msg->flags & HTTP_MSGF_CNT_LEN) {
Christopher Faulet27d93c32018-12-15 22:32:02 +0100552 ctx.blk = NULL;
553 while (http_find_header(htx, ist("Content-Length"), &ctx, 1))
554 http_remove_header(htx, &ctx);
555 }
556
557 /* add "Transfer-Encoding: chunked" header */
558 if (!(msg->flags & HTTP_MSGF_TE_CHNK)) {
559 if (!http_add_header(htx, ist("Transfer-Encoding"), ist("chunked")))
560 goto error;
561 }
562
Tim Duesterhusb229f012019-01-29 16:38:56 +0100563 /* convert "ETag" header to a weak ETag */
564 ctx.blk = NULL;
565 if (http_find_header(htx, ist("ETag"), &ctx, 1)) {
566 if (ctx.value.ptr[0] == '"') {
567 /* This a strong ETag. Convert it to a weak one. */
568 struct ist v = ist2(trash.area, 0);
569 if (istcat(&v, ist("W/"), trash.size) == -1 || istcat(&v, ctx.value, trash.size) == -1)
570 goto error;
571
572 if (!http_replace_header_value(htx, &ctx, v))
573 goto error;
574 }
575 }
576
Christopher Faulet27d93c32018-12-15 22:32:02 +0100577 return 1;
578
579 error:
580 st->comp_algo->end(&st->comp_ctx);
581 st->comp_algo = NULL;
582 return 0;
583}
584
585static int
586set_compression_response_header(struct comp_state *st, struct stream *s, struct http_msg *msg)
587{
588 if (IS_HTX_STRM(s))
589 return htx_set_comp_reshdr(st, s, msg);
590 else
591 return http_set_comp_reshdr(st, s, msg);
592}
593
Christopher Faulet3d97c902015-12-09 14:59:38 +0100594/*
595 * Selects a compression algorithm depending on the client request.
596 */
Christopher Faulete6902cd2018-11-30 22:29:48 +0100597static int
598http_select_comp_reqhdr(struct comp_state *st, struct stream *s, struct http_msg *msg)
Christopher Faulet3d97c902015-12-09 14:59:38 +0100599{
600 struct http_txn *txn = s->txn;
Olivier Houchard0b662842018-06-29 18:16:31 +0200601 struct channel *req = msg->chn;
Christopher Faulet3d97c902015-12-09 14:59:38 +0100602 struct hdr_ctx ctx;
603 struct comp_algo *comp_algo = NULL;
604 struct comp_algo *comp_algo_back = NULL;
605
606 /* Disable compression for older user agents announcing themselves as "Mozilla/4"
607 * unless they are known good (MSIE 6 with XP SP2, or MSIE 7 and later).
608 * See http://zoompf.com/2012/02/lose-the-wait-http-compression for more details.
609 */
610 ctx.idx = 0;
Olivier Houchard0b662842018-06-29 18:16:31 +0200611 if (http_find_header2("User-Agent", 10, ci_head(req), &txn->hdr_idx, &ctx) &&
Christopher Faulet3d97c902015-12-09 14:59:38 +0100612 ctx.vlen >= 9 &&
613 memcmp(ctx.line + ctx.val, "Mozilla/4", 9) == 0 &&
614 (ctx.vlen < 31 ||
615 memcmp(ctx.line + ctx.val + 25, "MSIE ", 5) != 0 ||
616 ctx.line[ctx.val + 30] < '6' ||
617 (ctx.line[ctx.val + 30] == '6' &&
618 (ctx.vlen < 54 || memcmp(ctx.line + 51, "SV1", 3) != 0)))) {
Christopher Faulet92d36382015-11-05 13:35:03 +0100619 st->comp_algo = NULL;
Christopher Faulet3d97c902015-12-09 14:59:38 +0100620 return 0;
621 }
622
623 /* search for the algo in the backend in priority or the frontend */
Christopher Faulet92d36382015-11-05 13:35:03 +0100624 if ((s->be->comp && (comp_algo_back = s->be->comp->algos)) ||
625 (strm_fe(s)->comp && (comp_algo_back = strm_fe(s)->comp->algos))) {
Christopher Faulet3d97c902015-12-09 14:59:38 +0100626 int best_q = 0;
627
628 ctx.idx = 0;
Olivier Houchard0b662842018-06-29 18:16:31 +0200629 while (http_find_header2("Accept-Encoding", 15, ci_head(req), &txn->hdr_idx, &ctx)) {
Christopher Faulet3d97c902015-12-09 14:59:38 +0100630 const char *qval;
631 int q;
632 int toklen;
633
634 /* try to isolate the token from the optional q-value */
635 toklen = 0;
Willy Tarreau2235b262016-11-05 15:50:20 +0100636 while (toklen < ctx.vlen && HTTP_IS_TOKEN(*(ctx.line + ctx.val + toklen)))
Christopher Faulet3d97c902015-12-09 14:59:38 +0100637 toklen++;
638
639 qval = ctx.line + ctx.val + toklen;
640 while (1) {
Willy Tarreau2235b262016-11-05 15:50:20 +0100641 while (qval < ctx.line + ctx.val + ctx.vlen && HTTP_IS_LWS(*qval))
Christopher Faulet3d97c902015-12-09 14:59:38 +0100642 qval++;
643
644 if (qval >= ctx.line + ctx.val + ctx.vlen || *qval != ';') {
645 qval = NULL;
646 break;
647 }
648 qval++;
649
Willy Tarreau2235b262016-11-05 15:50:20 +0100650 while (qval < ctx.line + ctx.val + ctx.vlen && HTTP_IS_LWS(*qval))
Christopher Faulet3d97c902015-12-09 14:59:38 +0100651 qval++;
652
653 if (qval >= ctx.line + ctx.val + ctx.vlen) {
654 qval = NULL;
655 break;
656 }
657 if (strncmp(qval, "q=", MIN(ctx.line + ctx.val + ctx.vlen - qval, 2)) == 0)
658 break;
659
660 while (qval < ctx.line + ctx.val + ctx.vlen && *qval != ';')
661 qval++;
662 }
663
664 /* here we have qval pointing to the first "q=" attribute or NULL if not found */
Willy Tarreauab813a42018-09-10 18:41:28 +0200665 q = qval ? http_parse_qvalue(qval + 2, NULL) : 1000;
Christopher Faulet3d97c902015-12-09 14:59:38 +0100666
667 if (q <= best_q)
668 continue;
669
670 for (comp_algo = comp_algo_back; comp_algo; comp_algo = comp_algo->next) {
671 if (*(ctx.line + ctx.val) == '*' ||
672 word_match(ctx.line + ctx.val, toklen, comp_algo->ua_name, comp_algo->ua_name_len)) {
Christopher Faulet92d36382015-11-05 13:35:03 +0100673 st->comp_algo = comp_algo;
Christopher Faulet3d97c902015-12-09 14:59:38 +0100674 best_q = q;
675 break;
676 }
677 }
678 }
679 }
680
681 /* remove all occurrences of the header when "compression offload" is set */
Christopher Faulet92d36382015-11-05 13:35:03 +0100682 if (st->comp_algo) {
683 if ((s->be->comp && s->be->comp->offload) ||
684 (strm_fe(s)->comp && strm_fe(s)->comp->offload)) {
Christopher Faulet3d97c902015-12-09 14:59:38 +0100685 http_remove_header2(msg, &txn->hdr_idx, &ctx);
686 ctx.idx = 0;
Olivier Houchard0b662842018-06-29 18:16:31 +0200687 while (http_find_header2("Accept-Encoding", 15, ci_head(req), &txn->hdr_idx, &ctx)) {
Christopher Faulet3d97c902015-12-09 14:59:38 +0100688 http_remove_header2(msg, &txn->hdr_idx, &ctx);
689 }
690 }
Christopher Faulete6902cd2018-11-30 22:29:48 +0100691 return 1;
692 }
693
694 /* identity is implicit does not require headers */
695 if ((s->be->comp && (comp_algo_back = s->be->comp->algos)) ||
696 (strm_fe(s)->comp && (comp_algo_back = strm_fe(s)->comp->algos))) {
697 for (comp_algo = comp_algo_back; comp_algo; comp_algo = comp_algo->next) {
698 if (comp_algo->cfg_name_len == 8 && memcmp(comp_algo->cfg_name, "identity", 8) == 0) {
699 st->comp_algo = comp_algo;
700 return 1;
701 }
702 }
703 }
704
705 st->comp_algo = NULL;
706 return 0;
707}
708
709static int
710htx_select_comp_reqhdr(struct comp_state *st, struct stream *s, struct http_msg *msg)
711{
Christopher Faulet27ba2dc2018-12-05 11:53:24 +0100712 struct htx *htx = htxbuf(&msg->chn->buf);
Christopher Faulete6902cd2018-11-30 22:29:48 +0100713 struct http_hdr_ctx ctx;
714 struct comp_algo *comp_algo = NULL;
715 struct comp_algo *comp_algo_back = NULL;
716
717 /* Disable compression for older user agents announcing themselves as "Mozilla/4"
718 * unless they are known good (MSIE 6 with XP SP2, or MSIE 7 and later).
719 * See http://zoompf.com/2012/02/lose-the-wait-http-compression for more details.
720 */
721 ctx.blk = NULL;
722 if (http_find_header(htx, ist("User-Agent"), &ctx, 1) &&
723 ctx.value.len >= 9 &&
724 memcmp(ctx.value.ptr, "Mozilla/4", 9) == 0 &&
725 (ctx.value.len < 31 ||
726 memcmp(ctx.value.ptr + 25, "MSIE ", 5) != 0 ||
727 *(ctx.value.ptr + 30) < '6' ||
728 (*(ctx.value.ptr + 30) == '6' &&
729 (ctx.value.len < 54 || memcmp(ctx.value.ptr + 51, "SV1", 3) != 0)))) {
730 st->comp_algo = NULL;
731 return 0;
732 }
733
734 /* search for the algo in the backend in priority or the frontend */
735 if ((s->be->comp && (comp_algo_back = s->be->comp->algos)) ||
736 (strm_fe(s)->comp && (comp_algo_back = strm_fe(s)->comp->algos))) {
737 int best_q = 0;
738
739 ctx.blk = NULL;
740 while (http_find_header(htx, ist("Accept-Encoding"), &ctx, 0)) {
741 const char *qval;
742 int q;
743 int toklen;
744
745 /* try to isolate the token from the optional q-value */
746 toklen = 0;
747 while (toklen < ctx.value.len && HTTP_IS_TOKEN(*(ctx.value.ptr + toklen)))
748 toklen++;
749
750 qval = ctx.value.ptr + toklen;
751 while (1) {
752 while (qval < ctx.value.ptr + ctx.value.len && HTTP_IS_LWS(*qval))
753 qval++;
754
755 if (qval >= ctx.value.ptr + ctx.value.len || *qval != ';') {
756 qval = NULL;
757 break;
758 }
759 qval++;
760
761 while (qval < ctx.value.ptr + ctx.value.len && HTTP_IS_LWS(*qval))
762 qval++;
763
764 if (qval >= ctx.value.ptr + ctx.value.len) {
765 qval = NULL;
766 break;
767 }
768 if (strncmp(qval, "q=", MIN(ctx.value.ptr + ctx.value.len - qval, 2)) == 0)
769 break;
770
771 while (qval < ctx.value.ptr + ctx.value.len && *qval != ';')
772 qval++;
773 }
774
775 /* here we have qval pointing to the first "q=" attribute or NULL if not found */
776 q = qval ? http_parse_qvalue(qval + 2, NULL) : 1000;
777
778 if (q <= best_q)
779 continue;
780
781 for (comp_algo = comp_algo_back; comp_algo; comp_algo = comp_algo->next) {
782 if (*(ctx.value.ptr) == '*' ||
783 word_match(ctx.value.ptr, toklen, comp_algo->ua_name, comp_algo->ua_name_len)) {
784 st->comp_algo = comp_algo;
785 best_q = q;
786 break;
787 }
788 }
789 }
790 }
791
792 /* remove all occurrences of the header when "compression offload" is set */
793 if (st->comp_algo) {
794 if ((s->be->comp && s->be->comp->offload) ||
795 (strm_fe(s)->comp && strm_fe(s)->comp->offload)) {
796 http_remove_header(htx, &ctx);
797 ctx.blk = NULL;
798 while (http_find_header(htx, ist("Accept-Encoding"), &ctx, 1))
799 http_remove_header(htx, &ctx);
800 }
Christopher Faulet3d97c902015-12-09 14:59:38 +0100801 return 1;
802 }
803
804 /* identity is implicit does not require headers */
Christopher Faulet92d36382015-11-05 13:35:03 +0100805 if ((s->be->comp && (comp_algo_back = s->be->comp->algos)) ||
806 (strm_fe(s)->comp && (comp_algo_back = strm_fe(s)->comp->algos))) {
Christopher Faulet3d97c902015-12-09 14:59:38 +0100807 for (comp_algo = comp_algo_back; comp_algo; comp_algo = comp_algo->next) {
808 if (comp_algo->cfg_name_len == 8 && memcmp(comp_algo->cfg_name, "identity", 8) == 0) {
Christopher Faulet92d36382015-11-05 13:35:03 +0100809 st->comp_algo = comp_algo;
Christopher Faulet3d97c902015-12-09 14:59:38 +0100810 return 1;
811 }
812 }
813 }
814
Christopher Faulet92d36382015-11-05 13:35:03 +0100815 st->comp_algo = NULL;
Christopher Faulet3d97c902015-12-09 14:59:38 +0100816 return 0;
817}
818
Christopher Faulete6902cd2018-11-30 22:29:48 +0100819static int
820select_compression_request_header(struct comp_state *st, struct stream *s,
821 struct http_msg *msg)
822{
823 if (IS_HTX_STRM(s))
824 return htx_select_comp_reqhdr(st, s, msg);
825 else
826 return http_select_comp_reqhdr(st, s, msg);
827}
Christopher Faulet92d36382015-11-05 13:35:03 +0100828
Christopher Faulet3d97c902015-12-09 14:59:38 +0100829/*
830 * Selects a comression algorithm depending of the server response.
831 */
Christopher Faulet92d36382015-11-05 13:35:03 +0100832static int
Christopher Faulete6902cd2018-11-30 22:29:48 +0100833http_select_comp_reshdr(struct comp_state *st, struct stream *s, struct http_msg *msg)
Christopher Faulet3d97c902015-12-09 14:59:38 +0100834{
835 struct http_txn *txn = s->txn;
Olivier Houchard0b662842018-06-29 18:16:31 +0200836 struct channel *c = msg->chn;
Christopher Faulet3d97c902015-12-09 14:59:38 +0100837 struct hdr_ctx ctx;
838 struct comp_type *comp_type;
839
840 /* no common compression algorithm was found in request header */
Christopher Faulet92d36382015-11-05 13:35:03 +0100841 if (st->comp_algo == NULL)
Christopher Faulet3d97c902015-12-09 14:59:38 +0100842 goto fail;
843
Christopher Faulet1d3613a2019-01-07 14:41:59 +0100844 /* compression already in progress */
845 if (msg->flags & HTTP_MSGF_COMPRESSING)
846 goto fail;
847
Christopher Faulet3d97c902015-12-09 14:59:38 +0100848 /* HTTP < 1.1 should not be compressed */
849 if (!(msg->flags & HTTP_MSGF_VER_11) || !(txn->req.flags & HTTP_MSGF_VER_11))
850 goto fail;
851
Christopher Faulet92d36382015-11-05 13:35:03 +0100852 if (txn->meth == HTTP_METH_HEAD)
853 goto fail;
854
Christopher Faulet3d97c902015-12-09 14:59:38 +0100855 /* compress 200,201,202,203 responses only */
856 if ((txn->status != 200) &&
857 (txn->status != 201) &&
858 (txn->status != 202) &&
859 (txn->status != 203))
860 goto fail;
861
862
863 /* Content-Length is null */
864 if (!(msg->flags & HTTP_MSGF_TE_CHNK) && msg->body_len == 0)
865 goto fail;
866
867 /* content is already compressed */
868 ctx.idx = 0;
Olivier Houchard0b662842018-06-29 18:16:31 +0200869 if (http_find_header2("Content-Encoding", 16, ci_head(c), &txn->hdr_idx, &ctx))
Christopher Faulet3d97c902015-12-09 14:59:38 +0100870 goto fail;
871
872 /* no compression when Cache-Control: no-transform is present in the message */
873 ctx.idx = 0;
Olivier Houchard0b662842018-06-29 18:16:31 +0200874 while (http_find_header2("Cache-Control", 13, ci_head(c), &txn->hdr_idx, &ctx)) {
Christopher Faulet3d97c902015-12-09 14:59:38 +0100875 if (word_match(ctx.line + ctx.val, ctx.vlen, "no-transform", 12))
876 goto fail;
877 }
878
Tim Duesterhusb229f012019-01-29 16:38:56 +0100879 /* no compression when ETag is malformed */
880 ctx.idx = 0;
881 if (http_find_full_header2("ETag", 4, ci_head(c), &txn->hdr_idx, &ctx)) {
882 if (!(((ctx.vlen >= 4 && memcmp(ctx.line + ctx.val, "W/\"", 3) == 0) || /* Either a weak ETag */
883 (ctx.vlen >= 2 && ctx.line[ctx.val] == '"')) && /* or strong ETag */
884 ctx.line[ctx.val + ctx.vlen - 1] == '"')) {
885 goto fail;
886 }
887 }
888 /* no compression when multiple ETags are present
889 * Note: Do not reset ctx.idx!
890 */
891 if (http_find_full_header2("ETag", 4, ci_head(c), &txn->hdr_idx, &ctx))
892 goto fail;
893
Christopher Faulet3d97c902015-12-09 14:59:38 +0100894 comp_type = NULL;
895
896 /* we don't want to compress multipart content-types, nor content-types that are
897 * not listed in the "compression type" directive if any. If no content-type was
898 * found but configuration requires one, we don't compress either. Backend has
899 * the priority.
900 */
901 ctx.idx = 0;
Olivier Houchard0b662842018-06-29 18:16:31 +0200902 if (http_find_header2("Content-Type", 12, ci_head(c), &txn->hdr_idx, &ctx)) {
Christopher Faulet3d97c902015-12-09 14:59:38 +0100903 if (ctx.vlen >= 9 && strncasecmp("multipart", ctx.line+ctx.val, 9) == 0)
904 goto fail;
905
906 if ((s->be->comp && (comp_type = s->be->comp->types)) ||
907 (strm_fe(s)->comp && (comp_type = strm_fe(s)->comp->types))) {
908 for (; comp_type; comp_type = comp_type->next) {
909 if (ctx.vlen >= comp_type->name_len &&
910 strncasecmp(ctx.line+ctx.val, comp_type->name, comp_type->name_len) == 0)
911 /* this Content-Type should be compressed */
912 break;
913 }
914 /* this Content-Type should not be compressed */
915 if (comp_type == NULL)
916 goto fail;
917 }
918 }
919 else { /* no content-type header */
Christopher Faulet92d36382015-11-05 13:35:03 +0100920 if ((s->be->comp && s->be->comp->types) ||
921 (strm_fe(s)->comp && strm_fe(s)->comp->types))
Christopher Faulet3d97c902015-12-09 14:59:38 +0100922 goto fail; /* a content-type was required */
923 }
924
925 /* limit compression rate */
926 if (global.comp_rate_lim > 0)
927 if (read_freq_ctr(&global.comp_bps_in) > global.comp_rate_lim)
928 goto fail;
929
930 /* limit cpu usage */
Willy Tarreau81036f22019-05-20 19:24:50 +0200931 if (ti->idle_pct < compress_min_idle)
Christopher Faulet3d97c902015-12-09 14:59:38 +0100932 goto fail;
933
934 /* initialize compression */
Christopher Faulet92d36382015-11-05 13:35:03 +0100935 if (st->comp_algo->init(&st->comp_ctx, global.tune.comp_maxlevel) < 0)
Christopher Faulet3d97c902015-12-09 14:59:38 +0100936 goto fail;
Christopher Faulet92d36382015-11-05 13:35:03 +0100937 msg->flags |= HTTP_MSGF_COMPRESSING;
Christopher Faulet3d97c902015-12-09 14:59:38 +0100938 return 1;
939
940fail:
Christopher Faulet92d36382015-11-05 13:35:03 +0100941 st->comp_algo = NULL;
Christopher Faulet3d97c902015-12-09 14:59:38 +0100942 return 0;
943}
944
Christopher Faulete6902cd2018-11-30 22:29:48 +0100945static int
946htx_select_comp_reshdr(struct comp_state *st, struct stream *s, struct http_msg *msg)
947{
Christopher Faulet27ba2dc2018-12-05 11:53:24 +0100948 struct htx *htx = htxbuf(&msg->chn->buf);
Christopher Faulete6902cd2018-11-30 22:29:48 +0100949 struct http_txn *txn = s->txn;
950 struct http_hdr_ctx ctx;
951 struct comp_type *comp_type;
952
953 /* no common compression algorithm was found in request header */
954 if (st->comp_algo == NULL)
955 goto fail;
956
Christopher Faulet1d3613a2019-01-07 14:41:59 +0100957 /* compression already in progress */
958 if (msg->flags & HTTP_MSGF_COMPRESSING)
959 goto fail;
960
Christopher Faulete6902cd2018-11-30 22:29:48 +0100961 /* HTTP < 1.1 should not be compressed */
962 if (!(msg->flags & HTTP_MSGF_VER_11) || !(txn->req.flags & HTTP_MSGF_VER_11))
963 goto fail;
964
965 if (txn->meth == HTTP_METH_HEAD)
966 goto fail;
967
968 /* compress 200,201,202,203 responses only */
969 if ((txn->status != 200) &&
970 (txn->status != 201) &&
971 (txn->status != 202) &&
972 (txn->status != 203))
973 goto fail;
974
Christopher Fauletc963eb22018-12-21 14:53:54 +0100975 if (!(msg->flags & HTTP_MSGF_XFER_LEN) || msg->flags & HTTP_MSGF_BODYLESS)
Christopher Faulete6902cd2018-11-30 22:29:48 +0100976 goto fail;
977
978 /* content is already compressed */
979 ctx.blk = NULL;
980 if (http_find_header(htx, ist("Content-Encoding"), &ctx, 1))
981 goto fail;
982
983 /* no compression when Cache-Control: no-transform is present in the message */
984 ctx.blk = NULL;
985 while (http_find_header(htx, ist("Cache-Control"), &ctx, 0)) {
986 if (word_match(ctx.value.ptr, ctx.value.len, "no-transform", 12))
987 goto fail;
988 }
989
Tim Duesterhusb229f012019-01-29 16:38:56 +0100990 /* no compression when ETag is malformed */
991 ctx.blk = NULL;
992 if (http_find_header(htx, ist("ETag"), &ctx, 1)) {
993 if (!(((ctx.value.len >= 4 && memcmp(ctx.value.ptr, "W/\"", 3) == 0) || /* Either a weak ETag */
994 (ctx.value.len >= 2 && ctx.value.ptr[0] == '"')) && /* or strong ETag */
995 ctx.value.ptr[ctx.value.len - 1] == '"')) {
996 goto fail;
997 }
998 }
999 /* no compression when multiple ETags are present
1000 * Note: Do not reset ctx.blk!
1001 */
1002 if (http_find_header(htx, ist("ETag"), &ctx, 1))
1003 goto fail;
1004
Christopher Faulete6902cd2018-11-30 22:29:48 +01001005 comp_type = NULL;
1006
1007 /* we don't want to compress multipart content-types, nor content-types that are
1008 * not listed in the "compression type" directive if any. If no content-type was
1009 * found but configuration requires one, we don't compress either. Backend has
1010 * the priority.
1011 */
1012 ctx.blk = NULL;
1013 if (http_find_header(htx, ist("Content-Type"), &ctx, 1)) {
1014 if (ctx.value.len >= 9 && strncasecmp("multipart", ctx.value.ptr, 9) == 0)
1015 goto fail;
1016
1017 if ((s->be->comp && (comp_type = s->be->comp->types)) ||
1018 (strm_fe(s)->comp && (comp_type = strm_fe(s)->comp->types))) {
1019 for (; comp_type; comp_type = comp_type->next) {
1020 if (ctx.value.len >= comp_type->name_len &&
1021 strncasecmp(ctx.value.ptr, comp_type->name, comp_type->name_len) == 0)
1022 /* this Content-Type should be compressed */
1023 break;
1024 }
1025 /* this Content-Type should not be compressed */
1026 if (comp_type == NULL)
1027 goto fail;
1028 }
1029 }
1030 else { /* no content-type header */
1031 if ((s->be->comp && s->be->comp->types) ||
1032 (strm_fe(s)->comp && strm_fe(s)->comp->types))
1033 goto fail; /* a content-type was required */
1034 }
1035
1036 /* limit compression rate */
1037 if (global.comp_rate_lim > 0)
1038 if (read_freq_ctr(&global.comp_bps_in) > global.comp_rate_lim)
1039 goto fail;
1040
1041 /* limit cpu usage */
Willy Tarreau81036f22019-05-20 19:24:50 +02001042 if (ti->idle_pct < compress_min_idle)
Christopher Faulete6902cd2018-11-30 22:29:48 +01001043 goto fail;
1044
1045 /* initialize compression */
1046 if (st->comp_algo->init(&st->comp_ctx, global.tune.comp_maxlevel) < 0)
1047 goto fail;
Christopher Faulete6902cd2018-11-30 22:29:48 +01001048 msg->flags |= HTTP_MSGF_COMPRESSING;
1049 return 1;
1050
1051 deinit_comp_ctx:
1052 st->comp_algo->end(&st->comp_ctx);
1053 fail:
1054 st->comp_algo = NULL;
1055 return 0;
1056}
1057
1058static int
1059select_compression_response_header(struct comp_state *st, struct stream *s, struct http_msg *msg)
1060{
1061 if (IS_HTX_STRM(s))
1062 return htx_select_comp_reshdr(st, s, msg);
1063 else
1064 return http_select_comp_reshdr(st, s, msg);
1065}
Christopher Faulet3d97c902015-12-09 14:59:38 +01001066/***********************************************************************/
1067/* emit the chunksize followed by a CRLF on the output and return the number of
1068 * bytes written. It goes backwards and starts with the byte before <end>. It
1069 * returns the number of bytes written which will not exceed 10 (8 digits, CR,
1070 * and LF). The caller is responsible for ensuring there is enough room left in
1071 * the output buffer for the string.
1072 */
1073static int
1074http_emit_chunk_size(char *end, unsigned int chksz)
1075{
1076 char *beg = end;
1077
1078 *--beg = '\n';
1079 *--beg = '\r';
1080 do {
1081 *--beg = hextab[chksz & 0xF];
1082 } while (chksz >>= 4);
1083 return end - beg;
1084}
1085
1086/*
1087 * Init HTTP compression
1088 */
Christopher Faulet92d36382015-11-05 13:35:03 +01001089static int
Christopher Fauletb61481c2018-12-17 13:17:53 +01001090http_compression_buffer_init(struct channel *inc, struct buffer *out)
Christopher Faulet3d97c902015-12-09 14:59:38 +01001091{
1092 /* output stream requires at least 10 bytes for the gzip header, plus
1093 * at least 8 bytes for the gzip trailer (crc+len), plus a possible
1094 * plus at most 5 bytes per 32kB block and 2 bytes to close the stream.
1095 */
Olivier Houchard0b662842018-06-29 18:16:31 +02001096 if (c_room(inc) < 20 + 5 * ((ci_data(inc) + 32767) >> 15))
Christopher Faulet3d97c902015-12-09 14:59:38 +01001097 return -1;
1098
1099 /* prepare an empty output buffer in which we reserve enough room for
1100 * copying the output bytes from <in>, plus 10 extra bytes to write
1101 * the chunk size. We don't copy the bytes yet so that if we have to
1102 * cancel the operation later, it's cheap.
1103 */
1104 b_reset(out);
Christopher Fauletb61481c2018-12-17 13:17:53 +01001105 out->head += co_data(inc) + 10;
Christopher Faulet3d97c902015-12-09 14:59:38 +01001106 return 0;
1107}
1108
Christopher Faulete6902cd2018-11-30 22:29:48 +01001109static int
1110htx_compression_buffer_init(struct htx *htx, struct buffer *out)
1111{
1112 /* output stream requires at least 10 bytes for the gzip header, plus
1113 * at least 8 bytes for the gzip trailer (crc+len), plus a possible
1114 * plus at most 5 bytes per 32kB block and 2 bytes to close the stream.
1115 */
1116 if (htx_free_space(htx) < 20 + 5 * ((htx->data + 32767) >> 15))
1117 return -1;
1118 b_reset(out);
1119 return 0;
1120}
1121
Christopher Faulet3d97c902015-12-09 14:59:38 +01001122/*
1123 * Add data to compress
1124 */
Christopher Faulet92d36382015-11-05 13:35:03 +01001125static int
1126http_compression_buffer_add_data(struct comp_state *st, struct buffer *in,
Willy Tarreaud54a8ce2018-06-29 18:42:02 +02001127 int in_out, struct buffer *out, int sz)
Christopher Faulet3d97c902015-12-09 14:59:38 +01001128{
Christopher Faulet3d97c902015-12-09 14:59:38 +01001129 int consumed_data = 0;
1130 int data_process_len;
1131 int block1, block2;
1132
Christopher Faulet92d36382015-11-05 13:35:03 +01001133 if (!sz)
Christopher Faulet3e7bc672015-12-07 13:39:08 +01001134 goto end;
Christopher Faulet3d97c902015-12-09 14:59:38 +01001135
Christopher Faulet92d36382015-11-05 13:35:03 +01001136 /* select the smallest size between the announced chunk size, the input
Christopher Faulet3d97c902015-12-09 14:59:38 +01001137 * data, and the available output buffer size. The compressors are
Christopher Faulet92d36382015-11-05 13:35:03 +01001138 * assumed to be able to process all the bytes we pass to them at
1139 * once. */
Willy Tarreaueac52592018-06-15 13:59:36 +02001140 data_process_len = MIN(b_room(out), sz);
Christopher Faulet92d36382015-11-05 13:35:03 +01001141
Christopher Faulet3d97c902015-12-09 14:59:38 +01001142 block1 = data_process_len;
Willy Tarreaud54a8ce2018-06-29 18:42:02 +02001143 if (block1 > b_contig_data(in, in_out))
1144 block1 = b_contig_data(in, in_out);
Christopher Faulet3d97c902015-12-09 14:59:38 +01001145 block2 = data_process_len - block1;
1146
1147 /* compressors return < 0 upon error or the amount of bytes read */
Christopher Faulet96667202018-12-17 12:02:57 +01001148 consumed_data = st->comp_algo->add_data(st->comp_ctx, b_peek(in, in_out), block1, out);
Christopher Faulet3e7bc672015-12-07 13:39:08 +01001149 if (consumed_data != block1 || !block2)
1150 goto end;
Christopher Faulet96667202018-12-17 12:02:57 +01001151 consumed_data = st->comp_algo->add_data(st->comp_ctx, b_orig(in), block2, out);
Christopher Faulet3e7bc672015-12-07 13:39:08 +01001152 if (consumed_data < 0)
1153 goto end;
1154 consumed_data += block1;
1155
1156 end:
Christopher Faulet3d97c902015-12-09 14:59:38 +01001157 return consumed_data;
1158}
1159
Christopher Faulete6902cd2018-11-30 22:29:48 +01001160static int
1161htx_compression_buffer_add_data(struct comp_state *st, const char *data, size_t len,
1162 struct buffer *out)
1163{
1164 return st->comp_algo->add_data(st->comp_ctx, data, len, out);
1165}
1166
Christopher Faulet3d97c902015-12-09 14:59:38 +01001167/*
1168 * Flush data in process, and write the header and footer of the chunk. Upon
1169 * success, in and out buffers are swapped to avoid a copy.
1170 */
Christopher Faulet92d36382015-11-05 13:35:03 +01001171static int
1172http_compression_buffer_end(struct comp_state *st, struct stream *s,
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001173 struct channel *chn, struct buffer *out,
Christopher Fauletb61481c2018-12-17 13:17:53 +01001174 int end)
Christopher Faulet3d97c902015-12-09 14:59:38 +01001175{
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001176 struct buffer tmp_buf;
Christopher Faulet3d97c902015-12-09 14:59:38 +01001177 char *tail;
Christopher Faulet92d36382015-11-05 13:35:03 +01001178 int to_forward, left;
Christopher Faulet3d97c902015-12-09 14:59:38 +01001179
1180#if defined(USE_SLZ) || defined(USE_ZLIB)
1181 int ret;
1182
1183 /* flush data here */
Christopher Faulet3d97c902015-12-09 14:59:38 +01001184 if (end)
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001185 ret = st->comp_algo->finish(st->comp_ctx, out); /* end of data */
Christopher Faulet3d97c902015-12-09 14:59:38 +01001186 else
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001187 ret = st->comp_algo->flush(st->comp_ctx, out); /* end of buffer */
Christopher Faulet3d97c902015-12-09 14:59:38 +01001188
1189 if (ret < 0)
1190 return -1; /* flush failed */
1191
1192#endif /* USE_ZLIB */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001193 if (b_data(out) == 0) {
Christopher Faulet3d97c902015-12-09 14:59:38 +01001194 /* No data were appended, let's drop the output buffer and
1195 * keep the input buffer unchanged.
1196 */
1197 return 0;
1198 }
1199
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001200 /* OK so at this stage, we have an output buffer <out> looking like this :
Christopher Faulet3d97c902015-12-09 14:59:38 +01001201 *
1202 * <-- o --> <------ i ----->
1203 * +---------+---+------------+-----------+
1204 * | out | c | comp_in | empty |
1205 * +---------+---+------------+-----------+
1206 * data p size
1207 *
Willy Tarreaud54a8ce2018-06-29 18:42:02 +02001208 * <out> is the room reserved to copy the channel output. It starts at
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001209 * out->area and has not yet been filled. <c> is the room reserved to
Willy Tarreaud54a8ce2018-06-29 18:42:02 +02001210 * write the chunk size (10 bytes). <comp_in> is the compressed
1211 * equivalent of the data part of ib->len. <empty> is the amount of
1212 * empty bytes at the end of the buffer, into which we may have to
1213 * copy the remaining bytes from ib->len after the data
1214 * (chunk size, trailers, ...).
Christopher Faulet3d97c902015-12-09 14:59:38 +01001215 */
1216
Joseph Herlant942eea32018-11-15 13:57:22 -08001217 /* Write real size at the beginning of the chunk, no need of wrapping.
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001218 * We write the chunk using a dynamic length and adjust out->p and out->i
Christopher Faulet3d97c902015-12-09 14:59:38 +01001219 * accordingly afterwards. That will move <out> away from <data>.
1220 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001221 left = http_emit_chunk_size(b_head(out), b_data(out));
1222 b_add(out, left);
Christopher Fauletb61481c2018-12-17 13:17:53 +01001223 out->head -= co_data(chn) + (left);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001224 /* Copy previous data from chn into out */
Willy Tarreaud54a8ce2018-06-29 18:42:02 +02001225 if (co_data(chn) > 0) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001226 left = b_contig_data(&chn->buf, 0);
Christopher Fauletb61481c2018-12-17 13:17:53 +01001227 if (left > co_data(chn))
1228 left = co_data(chn);
Christopher Faulet3d97c902015-12-09 14:59:38 +01001229
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001230 memcpy(b_head(out), co_head(chn), left);
1231 b_add(out, left);
Willy Tarreaud54a8ce2018-06-29 18:42:02 +02001232 if (co_data(chn) - left) {/* second part of the buffer */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001233 memcpy(b_head(out) + left, b_orig(&chn->buf), co_data(chn) - left);
1234 b_add(out, co_data(chn) - left);
Willy Tarreaud54a8ce2018-06-29 18:42:02 +02001235 }
Christopher Faulet3d97c902015-12-09 14:59:38 +01001236 }
1237
1238 /* chunked encoding requires CRLF after data */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001239 tail = b_tail(out);
Christopher Faulet3d97c902015-12-09 14:59:38 +01001240 *tail++ = '\r';
1241 *tail++ = '\n';
1242
Christopher Faulet2fb28802015-12-01 10:40:57 +01001243 /* At the end of data, we must write the empty chunk 0<CRLF>,
1244 * and terminate the trailers section with a last <CRLF>. If
1245 * we're forwarding a chunked-encoded response, we'll have a
1246 * trailers section after the empty chunk which needs to be
1247 * forwarded and which will provide the last CRLF. Otherwise
1248 * we write it ourselves.
1249 */
1250 if (end) {
1251 struct http_msg *msg = &s->txn->rsp;
1252
1253 memcpy(tail, "0\r\n", 3);
1254 tail += 3;
Christopher Fauletb77c5c22015-12-07 16:48:42 +01001255 if (!(msg->flags & HTTP_MSGF_TE_CHNK)) {
Christopher Faulet2fb28802015-12-01 10:40:57 +01001256 memcpy(tail, "\r\n", 2);
1257 tail += 2;
1258 }
1259 }
1260
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001261 b_add(out, tail - b_tail(out));
Christopher Fauletb61481c2018-12-17 13:17:53 +01001262 to_forward = b_data(out) - co_data(chn);
Christopher Faulet3d97c902015-12-09 14:59:38 +01001263
1264 /* update input rate */
Christopher Faulet92d36382015-11-05 13:35:03 +01001265 if (st->comp_ctx && st->comp_ctx->cur_lvl > 0) {
Christopher Faulet2fb28802015-12-01 10:40:57 +01001266 update_freq_ctr(&global.comp_bps_in, st->consumed);
Olivier Houchard43da3432019-03-08 18:50:27 +01001267 _HA_ATOMIC_ADD(&strm_fe(s)->fe_counters.comp_in, st->consumed);
1268 _HA_ATOMIC_ADD(&s->be->be_counters.comp_in, st->consumed);
Christopher Faulet3d97c902015-12-09 14:59:38 +01001269 } else {
Olivier Houchard43da3432019-03-08 18:50:27 +01001270 _HA_ATOMIC_ADD(&strm_fe(s)->fe_counters.comp_byp, st->consumed);
1271 _HA_ATOMIC_ADD(&s->be->be_counters.comp_byp, st->consumed);
Christopher Faulet3d97c902015-12-09 14:59:38 +01001272 }
1273
1274 /* copy the remaining data in the tmp buffer. */
Willy Tarreaubcbd3932018-06-06 07:13:22 +02001275 c_adv(chn, st->consumed);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001276 if (b_data(&chn->buf) - co_data(chn) > 0) {
Willy Tarreau7194d3c2018-06-06 16:55:45 +02001277 left = ci_contig_data(chn);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001278 memcpy(b_tail(out), ci_head(chn), left);
1279 b_add(out, left);
1280 if (b_data(&chn->buf) - (co_data(chn) + left)) {
1281 memcpy(b_tail(out), b_orig(&chn->buf), b_data(&chn->buf) - left);
1282 b_add(out, b_data(&chn->buf) - left);
Christopher Faulet3d97c902015-12-09 14:59:38 +01001283 }
1284 }
Christopher Fauletb61481c2018-12-17 13:17:53 +01001285 c_rew(chn, st->consumed);
1286
Christopher Faulet3d97c902015-12-09 14:59:38 +01001287 /* swap the buffers */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001288 tmp_buf = chn->buf;
1289 chn->buf = *out;
1290 *out = tmp_buf;
1291
Christopher Faulet92d36382015-11-05 13:35:03 +01001292 if (st->comp_ctx && st->comp_ctx->cur_lvl > 0) {
Christopher Faulet3d97c902015-12-09 14:59:38 +01001293 update_freq_ctr(&global.comp_bps_out, to_forward);
Olivier Houchard43da3432019-03-08 18:50:27 +01001294 _HA_ATOMIC_ADD(&strm_fe(s)->fe_counters.comp_out, to_forward);
1295 _HA_ATOMIC_ADD(&s->be->be_counters.comp_out, to_forward);
Christopher Faulet3d97c902015-12-09 14:59:38 +01001296 }
1297
Christopher Faulet3d97c902015-12-09 14:59:38 +01001298 return to_forward;
1299}
1300
Christopher Faulete6902cd2018-11-30 22:29:48 +01001301static int
1302htx_compression_buffer_end(struct comp_state *st, struct buffer *out, int end)
1303{
1304 if (end)
1305 return st->comp_algo->finish(st->comp_ctx, out);
1306 else
1307 return st->comp_algo->flush(st->comp_ctx, out);
1308}
1309
Christopher Faulet3d97c902015-12-09 14:59:38 +01001310
1311/***********************************************************************/
Christopher Faulet92d36382015-11-05 13:35:03 +01001312struct flt_ops comp_ops = {
Christopher Faulete6902cd2018-11-30 22:29:48 +01001313 .init = comp_flt_init,
Christopher Faulet8ca3b4b2017-07-25 11:07:15 +02001314 .init_per_thread = comp_flt_init_per_thread,
1315 .deinit_per_thread = comp_flt_deinit_per_thread,
Christopher Faulet92d36382015-11-05 13:35:03 +01001316
1317 .channel_start_analyze = comp_start_analyze,
Christopher Faulet92d36382015-11-05 13:35:03 +01001318 .channel_end_analyze = comp_end_analyze,
Christopher Faulet3dc860d2017-09-15 11:39:36 +02001319 .channel_post_analyze = comp_http_post_analyze,
Christopher Faulet92d36382015-11-05 13:35:03 +01001320
Christopher Faulet1339d742016-05-11 16:48:33 +02001321 .http_headers = comp_http_headers,
Christopher Faulete6902cd2018-11-30 22:29:48 +01001322 .http_payload = comp_http_payload,
1323 .http_end = comp_http_end,
1324
Christopher Faulet309c6412015-12-02 09:57:32 +01001325 .http_data = comp_http_data,
1326 .http_chunk_trailers = comp_http_chunk_trailers,
1327 .http_forward_data = comp_http_forward_data,
Christopher Faulet92d36382015-11-05 13:35:03 +01001328};
1329
Christopher Faulet3d97c902015-12-09 14:59:38 +01001330static int
1331parse_compression_options(char **args, int section, struct proxy *proxy,
1332 struct proxy *defpx, const char *file, int line,
1333 char **err)
1334{
Christopher Faulet92d36382015-11-05 13:35:03 +01001335 struct comp *comp;
Christopher Faulet3d97c902015-12-09 14:59:38 +01001336
1337 if (proxy->comp == NULL) {
Vincent Bernat02779b62016-04-03 13:48:43 +02001338 comp = calloc(1, sizeof(*comp));
Christopher Faulet3d97c902015-12-09 14:59:38 +01001339 proxy->comp = comp;
1340 }
1341 else
1342 comp = proxy->comp;
1343
1344 if (!strcmp(args[1], "algo")) {
1345 struct comp_ctx *ctx;
1346 int cur_arg = 2;
1347
1348 if (!*args[cur_arg]) {
1349 memprintf(err, "parsing [%s:%d] : '%s' expects <algorithm>\n",
1350 file, line, args[0]);
1351 return -1;
1352 }
1353 while (*(args[cur_arg])) {
1354 if (comp_append_algo(comp, args[cur_arg]) < 0) {
1355 memprintf(err, "'%s' : '%s' is not a supported algorithm.\n",
1356 args[0], args[cur_arg]);
1357 return -1;
1358 }
1359 if (proxy->comp->algos->init(&ctx, 9) == 0)
1360 proxy->comp->algos->end(&ctx);
1361 else {
1362 memprintf(err, "'%s' : Can't init '%s' algorithm.\n",
1363 args[0], args[cur_arg]);
1364 return -1;
1365 }
1366 cur_arg++;
1367 continue;
1368 }
1369 }
1370 else if (!strcmp(args[1], "offload"))
1371 comp->offload = 1;
1372 else if (!strcmp(args[1], "type")) {
1373 int cur_arg = 2;
1374
1375 if (!*args[cur_arg]) {
1376 memprintf(err, "'%s' expects <type>\n", args[0]);
1377 return -1;
1378 }
1379 while (*(args[cur_arg])) {
1380 comp_append_type(comp, args[cur_arg]);
1381 cur_arg++;
1382 continue;
1383 }
1384 }
1385 else {
1386 memprintf(err, "'%s' expects 'algo', 'type' or 'offload'\n",
1387 args[0]);
1388 return -1;
1389 }
1390
1391 return 0;
1392}
1393
Christopher Faulet92d36382015-11-05 13:35:03 +01001394static int
1395parse_http_comp_flt(char **args, int *cur_arg, struct proxy *px,
Thierry Fournier3610c392016-04-13 18:27:51 +02001396 struct flt_conf *fconf, char **err, void *private)
Christopher Faulet92d36382015-11-05 13:35:03 +01001397{
Christopher Faulet443ea1a2016-02-04 13:40:26 +01001398 struct flt_conf *fc, *back;
Christopher Faulet92d36382015-11-05 13:35:03 +01001399
Christopher Faulet443ea1a2016-02-04 13:40:26 +01001400 list_for_each_entry_safe(fc, back, &px->filter_configs, list) {
1401 if (fc->id == http_comp_flt_id) {
Christopher Faulet92d36382015-11-05 13:35:03 +01001402 memprintf(err, "%s: Proxy supports only one compression filter\n", px->id);
1403 return -1;
1404 }
1405 }
1406
Christopher Faulet443ea1a2016-02-04 13:40:26 +01001407 fconf->id = http_comp_flt_id;
1408 fconf->conf = NULL;
1409 fconf->ops = &comp_ops;
Christopher Faulet92d36382015-11-05 13:35:03 +01001410 (*cur_arg)++;
1411
1412 return 0;
1413}
1414
1415
1416int
Christopher Fauletc9df7f72018-12-10 16:14:04 +01001417check_implicit_http_comp_flt(struct proxy *proxy)
Christopher Faulet92d36382015-11-05 13:35:03 +01001418{
Christopher Faulet443ea1a2016-02-04 13:40:26 +01001419 struct flt_conf *fconf;
Christopher Faulet27d93c32018-12-15 22:32:02 +01001420 int explicit = 0;
1421 int comp = 0;
Christopher Faulet92d36382015-11-05 13:35:03 +01001422 int err = 0;
1423
1424 if (proxy->comp == NULL)
1425 goto end;
Christopher Faulet443ea1a2016-02-04 13:40:26 +01001426 if (!LIST_ISEMPTY(&proxy->filter_configs)) {
1427 list_for_each_entry(fconf, &proxy->filter_configs, list) {
1428 if (fconf->id == http_comp_flt_id)
Christopher Faulet27d93c32018-12-15 22:32:02 +01001429 comp = 1;
1430 else if (fconf->id == cache_store_flt_id) {
1431 if (comp) {
1432 ha_alert("config: %s '%s': unable to enable the compression filter "
1433 "before any cache filter.\n",
1434 proxy_type_str(proxy), proxy->id);
1435 err++;
1436 goto end;
1437 }
1438 }
1439 else
1440 explicit = 1;
Christopher Faulet92d36382015-11-05 13:35:03 +01001441 }
Christopher Faulet27d93c32018-12-15 22:32:02 +01001442 }
1443 if (comp)
1444 goto end;
1445 else if (explicit) {
1446 ha_alert("config: %s '%s': require an explicit filter declaration to use "
1447 "HTTP compression\n", proxy_type_str(proxy), proxy->id);
Christopher Faulet92d36382015-11-05 13:35:03 +01001448 err++;
1449 goto end;
1450 }
1451
Christopher Faulet27d93c32018-12-15 22:32:02 +01001452 /* Implicit declaration of the compression filter is always the last
1453 * one */
Christopher Faulet443ea1a2016-02-04 13:40:26 +01001454 fconf = calloc(1, sizeof(*fconf));
1455 if (!fconf) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001456 ha_alert("config: %s '%s': out of memory\n",
1457 proxy_type_str(proxy), proxy->id);
Christopher Faulet92d36382015-11-05 13:35:03 +01001458 err++;
1459 goto end;
1460 }
Christopher Faulet443ea1a2016-02-04 13:40:26 +01001461 fconf->id = http_comp_flt_id;
1462 fconf->conf = NULL;
1463 fconf->ops = &comp_ops;
1464 LIST_ADDQ(&proxy->filter_configs, &fconf->list);
Christopher Faulet92d36382015-11-05 13:35:03 +01001465 end:
1466 return err;
1467}
1468
1469/*
1470 * boolean, returns true if compression is used (either gzip or deflate) in the
1471 * response.
1472 */
Christopher Faulet3d97c902015-12-09 14:59:38 +01001473static int
Christopher Faulet92d36382015-11-05 13:35:03 +01001474smp_fetch_res_comp(const struct arg *args, struct sample *smp, const char *kw,
1475 void *private)
Christopher Faulet3d97c902015-12-09 14:59:38 +01001476{
Willy Tarreaube508f12016-03-10 11:47:01 +01001477 struct http_txn *txn = smp->strm ? smp->strm->txn : NULL;
Christopher Faulet92d36382015-11-05 13:35:03 +01001478
Christopher Faulet3d97c902015-12-09 14:59:38 +01001479 smp->data.type = SMP_T_BOOL;
Christopher Faulet92d36382015-11-05 13:35:03 +01001480 smp->data.u.sint = (txn && (txn->rsp.flags & HTTP_MSGF_COMPRESSING));
Christopher Faulet3d97c902015-12-09 14:59:38 +01001481 return 1;
1482}
1483
Christopher Faulet92d36382015-11-05 13:35:03 +01001484/*
1485 * string, returns algo
1486 */
Christopher Faulet3d97c902015-12-09 14:59:38 +01001487static int
Christopher Faulet92d36382015-11-05 13:35:03 +01001488smp_fetch_res_comp_algo(const struct arg *args, struct sample *smp,
1489 const char *kw, void *private)
Christopher Faulet3d97c902015-12-09 14:59:38 +01001490{
Willy Tarreaube508f12016-03-10 11:47:01 +01001491 struct http_txn *txn = smp->strm ? smp->strm->txn : NULL;
Christopher Faulet92d36382015-11-05 13:35:03 +01001492 struct filter *filter;
1493 struct comp_state *st;
1494
Christopher Faulet03d85532017-09-15 10:14:43 +02001495 if (!txn || !(txn->rsp.flags & HTTP_MSGF_COMPRESSING))
Christopher Faulet3d97c902015-12-09 14:59:38 +01001496 return 0;
1497
Christopher Fauletfcf035c2015-12-03 11:48:03 +01001498 list_for_each_entry(filter, &strm_flt(smp->strm)->filters, list) {
Christopher Faulet443ea1a2016-02-04 13:40:26 +01001499 if (FLT_ID(filter) != http_comp_flt_id)
Christopher Faulet92d36382015-11-05 13:35:03 +01001500 continue;
1501
1502 if (!(st = filter->ctx))
1503 break;
1504
1505 smp->data.type = SMP_T_STR;
1506 smp->flags = SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001507 smp->data.u.str.area = st->comp_algo->cfg_name;
1508 smp->data.u.str.data = st->comp_algo->cfg_name_len;
Christopher Faulet92d36382015-11-05 13:35:03 +01001509 return 1;
1510 }
1511 return 0;
Christopher Faulet3d97c902015-12-09 14:59:38 +01001512}
1513
1514/* Declare the config parser for "compression" keyword */
1515static struct cfg_kw_list cfg_kws = {ILH, {
1516 { CFG_LISTEN, "compression", parse_compression_options },
1517 { 0, NULL, NULL },
1518 }
1519};
1520
Willy Tarreau0108d902018-11-25 19:14:37 +01001521INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
1522
Christopher Faulet92d36382015-11-05 13:35:03 +01001523/* Declare the filter parser for "compression" keyword */
1524static struct flt_kw_list filter_kws = { "COMP", { }, {
Thierry Fournier3610c392016-04-13 18:27:51 +02001525 { "compression", parse_http_comp_flt, NULL },
1526 { NULL, NULL, NULL },
Christopher Faulet92d36382015-11-05 13:35:03 +01001527 }
1528};
1529
Willy Tarreau0108d902018-11-25 19:14:37 +01001530INITCALL1(STG_REGISTER, flt_register_keywords, &filter_kws);
1531
Christopher Faulet3d97c902015-12-09 14:59:38 +01001532/* Note: must not be declared <const> as its list will be overwritten */
1533static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
Christopher Faulet92d36382015-11-05 13:35:03 +01001534 { "res.comp", smp_fetch_res_comp, 0, NULL, SMP_T_BOOL, SMP_USE_HRSHP },
1535 { "res.comp_algo", smp_fetch_res_comp_algo, 0, NULL, SMP_T_STR, SMP_USE_HRSHP },
1536 { /* END */ },
1537 }
1538};
Christopher Faulet3d97c902015-12-09 14:59:38 +01001539
Willy Tarreau0108d902018-11-25 19:14:37 +01001540INITCALL1(STG_REGISTER, sample_register_fetches, &sample_fetch_keywords);