blob: 0c75aa0c1d82768a646b83a55bdfc45107329e17 [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 /* 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
Tim Duesterhuseaf65002019-06-17 16:10:07 +0200523 if (http_header_add_tail2(msg, &txn->hdr_idx, "Vary: Accept-Encoding", 21) < 0)
524 goto error;
525
Christopher Faulet27d93c32018-12-15 22:32:02 +0100526 return 1;
527
528 error:
529 st->comp_algo->end(&st->comp_ctx);
530 st->comp_algo = NULL;
531 return 0;
532}
533
534static int
535htx_set_comp_reshdr(struct comp_state *st, struct stream *s, struct http_msg *msg)
536{
537 struct htx *htx = htxbuf(&msg->chn->buf);
Tim Duesterhusb229f012019-01-29 16:38:56 +0100538 struct http_hdr_ctx ctx;
Christopher Faulet27d93c32018-12-15 22:32:02 +0100539
540 /*
541 * Add Content-Encoding header when it's not identity encoding.
542 * RFC 2616 : Identity encoding: This content-coding is used only in the
543 * Accept-Encoding header, and SHOULD NOT be used in the Content-Encoding
544 * header.
545 */
546 if (st->comp_algo->cfg_name_len != 8 || memcmp(st->comp_algo->cfg_name, "identity", 8) != 0) {
547 struct ist v = ist2(st->comp_algo->ua_name, st->comp_algo->ua_name_len);
548
549 if (!http_add_header(htx, ist("Content-Encoding"), v))
550 goto error;
551 }
552
553 /* remove Content-Length header */
554 if (msg->flags & HTTP_MSGF_CNT_LEN) {
Christopher Faulet27d93c32018-12-15 22:32:02 +0100555 ctx.blk = NULL;
556 while (http_find_header(htx, ist("Content-Length"), &ctx, 1))
557 http_remove_header(htx, &ctx);
558 }
559
560 /* add "Transfer-Encoding: chunked" header */
561 if (!(msg->flags & HTTP_MSGF_TE_CHNK)) {
562 if (!http_add_header(htx, ist("Transfer-Encoding"), ist("chunked")))
563 goto error;
564 }
565
Tim Duesterhusb229f012019-01-29 16:38:56 +0100566 /* convert "ETag" header to a weak ETag */
567 ctx.blk = NULL;
568 if (http_find_header(htx, ist("ETag"), &ctx, 1)) {
569 if (ctx.value.ptr[0] == '"') {
570 /* This a strong ETag. Convert it to a weak one. */
571 struct ist v = ist2(trash.area, 0);
572 if (istcat(&v, ist("W/"), trash.size) == -1 || istcat(&v, ctx.value, trash.size) == -1)
573 goto error;
574
575 if (!http_replace_header_value(htx, &ctx, v))
576 goto error;
577 }
578 }
579
Tim Duesterhuseaf65002019-06-17 16:10:07 +0200580 if (!http_add_header(htx, ist("Vary"), ist("Accept-Encoding")))
581 goto error;
582
Christopher Faulet27d93c32018-12-15 22:32:02 +0100583 return 1;
584
585 error:
586 st->comp_algo->end(&st->comp_ctx);
587 st->comp_algo = NULL;
588 return 0;
589}
590
591static int
592set_compression_response_header(struct comp_state *st, struct stream *s, struct http_msg *msg)
593{
594 if (IS_HTX_STRM(s))
595 return htx_set_comp_reshdr(st, s, msg);
596 else
597 return http_set_comp_reshdr(st, s, msg);
598}
599
Christopher Faulet3d97c902015-12-09 14:59:38 +0100600/*
601 * Selects a compression algorithm depending on the client request.
602 */
Christopher Faulete6902cd2018-11-30 22:29:48 +0100603static int
604http_select_comp_reqhdr(struct comp_state *st, struct stream *s, struct http_msg *msg)
Christopher Faulet3d97c902015-12-09 14:59:38 +0100605{
606 struct http_txn *txn = s->txn;
Olivier Houchard0b662842018-06-29 18:16:31 +0200607 struct channel *req = msg->chn;
Christopher Faulet3d97c902015-12-09 14:59:38 +0100608 struct hdr_ctx ctx;
609 struct comp_algo *comp_algo = NULL;
610 struct comp_algo *comp_algo_back = NULL;
611
612 /* Disable compression for older user agents announcing themselves as "Mozilla/4"
613 * unless they are known good (MSIE 6 with XP SP2, or MSIE 7 and later).
614 * See http://zoompf.com/2012/02/lose-the-wait-http-compression for more details.
615 */
616 ctx.idx = 0;
Olivier Houchard0b662842018-06-29 18:16:31 +0200617 if (http_find_header2("User-Agent", 10, ci_head(req), &txn->hdr_idx, &ctx) &&
Christopher Faulet3d97c902015-12-09 14:59:38 +0100618 ctx.vlen >= 9 &&
619 memcmp(ctx.line + ctx.val, "Mozilla/4", 9) == 0 &&
620 (ctx.vlen < 31 ||
621 memcmp(ctx.line + ctx.val + 25, "MSIE ", 5) != 0 ||
622 ctx.line[ctx.val + 30] < '6' ||
623 (ctx.line[ctx.val + 30] == '6' &&
624 (ctx.vlen < 54 || memcmp(ctx.line + 51, "SV1", 3) != 0)))) {
Christopher Faulet92d36382015-11-05 13:35:03 +0100625 st->comp_algo = NULL;
Christopher Faulet3d97c902015-12-09 14:59:38 +0100626 return 0;
627 }
628
629 /* search for the algo in the backend in priority or the frontend */
Christopher Faulet92d36382015-11-05 13:35:03 +0100630 if ((s->be->comp && (comp_algo_back = s->be->comp->algos)) ||
631 (strm_fe(s)->comp && (comp_algo_back = strm_fe(s)->comp->algos))) {
Christopher Faulet3d97c902015-12-09 14:59:38 +0100632 int best_q = 0;
633
634 ctx.idx = 0;
Olivier Houchard0b662842018-06-29 18:16:31 +0200635 while (http_find_header2("Accept-Encoding", 15, ci_head(req), &txn->hdr_idx, &ctx)) {
Christopher Faulet3d97c902015-12-09 14:59:38 +0100636 const char *qval;
637 int q;
638 int toklen;
639
640 /* try to isolate the token from the optional q-value */
641 toklen = 0;
Willy Tarreau2235b262016-11-05 15:50:20 +0100642 while (toklen < ctx.vlen && HTTP_IS_TOKEN(*(ctx.line + ctx.val + toklen)))
Christopher Faulet3d97c902015-12-09 14:59:38 +0100643 toklen++;
644
645 qval = ctx.line + ctx.val + toklen;
646 while (1) {
Willy Tarreau2235b262016-11-05 15:50:20 +0100647 while (qval < ctx.line + ctx.val + ctx.vlen && HTTP_IS_LWS(*qval))
Christopher Faulet3d97c902015-12-09 14:59:38 +0100648 qval++;
649
650 if (qval >= ctx.line + ctx.val + ctx.vlen || *qval != ';') {
651 qval = NULL;
652 break;
653 }
654 qval++;
655
Willy Tarreau2235b262016-11-05 15:50:20 +0100656 while (qval < ctx.line + ctx.val + ctx.vlen && HTTP_IS_LWS(*qval))
Christopher Faulet3d97c902015-12-09 14:59:38 +0100657 qval++;
658
659 if (qval >= ctx.line + ctx.val + ctx.vlen) {
660 qval = NULL;
661 break;
662 }
663 if (strncmp(qval, "q=", MIN(ctx.line + ctx.val + ctx.vlen - qval, 2)) == 0)
664 break;
665
666 while (qval < ctx.line + ctx.val + ctx.vlen && *qval != ';')
667 qval++;
668 }
669
670 /* here we have qval pointing to the first "q=" attribute or NULL if not found */
Willy Tarreauab813a42018-09-10 18:41:28 +0200671 q = qval ? http_parse_qvalue(qval + 2, NULL) : 1000;
Christopher Faulet3d97c902015-12-09 14:59:38 +0100672
673 if (q <= best_q)
674 continue;
675
676 for (comp_algo = comp_algo_back; comp_algo; comp_algo = comp_algo->next) {
677 if (*(ctx.line + ctx.val) == '*' ||
678 word_match(ctx.line + ctx.val, toklen, comp_algo->ua_name, comp_algo->ua_name_len)) {
Christopher Faulet92d36382015-11-05 13:35:03 +0100679 st->comp_algo = comp_algo;
Christopher Faulet3d97c902015-12-09 14:59:38 +0100680 best_q = q;
681 break;
682 }
683 }
684 }
685 }
686
687 /* remove all occurrences of the header when "compression offload" is set */
Christopher Faulet92d36382015-11-05 13:35:03 +0100688 if (st->comp_algo) {
689 if ((s->be->comp && s->be->comp->offload) ||
690 (strm_fe(s)->comp && strm_fe(s)->comp->offload)) {
Christopher Faulet3d97c902015-12-09 14:59:38 +0100691 http_remove_header2(msg, &txn->hdr_idx, &ctx);
692 ctx.idx = 0;
Olivier Houchard0b662842018-06-29 18:16:31 +0200693 while (http_find_header2("Accept-Encoding", 15, ci_head(req), &txn->hdr_idx, &ctx)) {
Christopher Faulet3d97c902015-12-09 14:59:38 +0100694 http_remove_header2(msg, &txn->hdr_idx, &ctx);
695 }
696 }
Christopher Faulete6902cd2018-11-30 22:29:48 +0100697 return 1;
698 }
699
700 /* identity is implicit does not require headers */
701 if ((s->be->comp && (comp_algo_back = s->be->comp->algos)) ||
702 (strm_fe(s)->comp && (comp_algo_back = strm_fe(s)->comp->algos))) {
703 for (comp_algo = comp_algo_back; comp_algo; comp_algo = comp_algo->next) {
704 if (comp_algo->cfg_name_len == 8 && memcmp(comp_algo->cfg_name, "identity", 8) == 0) {
705 st->comp_algo = comp_algo;
706 return 1;
707 }
708 }
709 }
710
711 st->comp_algo = NULL;
712 return 0;
713}
714
715static int
716htx_select_comp_reqhdr(struct comp_state *st, struct stream *s, struct http_msg *msg)
717{
Christopher Faulet27ba2dc2018-12-05 11:53:24 +0100718 struct htx *htx = htxbuf(&msg->chn->buf);
Christopher Faulete6902cd2018-11-30 22:29:48 +0100719 struct http_hdr_ctx ctx;
720 struct comp_algo *comp_algo = NULL;
721 struct comp_algo *comp_algo_back = NULL;
722
723 /* Disable compression for older user agents announcing themselves as "Mozilla/4"
724 * unless they are known good (MSIE 6 with XP SP2, or MSIE 7 and later).
725 * See http://zoompf.com/2012/02/lose-the-wait-http-compression for more details.
726 */
727 ctx.blk = NULL;
728 if (http_find_header(htx, ist("User-Agent"), &ctx, 1) &&
729 ctx.value.len >= 9 &&
730 memcmp(ctx.value.ptr, "Mozilla/4", 9) == 0 &&
731 (ctx.value.len < 31 ||
732 memcmp(ctx.value.ptr + 25, "MSIE ", 5) != 0 ||
733 *(ctx.value.ptr + 30) < '6' ||
734 (*(ctx.value.ptr + 30) == '6' &&
735 (ctx.value.len < 54 || memcmp(ctx.value.ptr + 51, "SV1", 3) != 0)))) {
736 st->comp_algo = NULL;
737 return 0;
738 }
739
740 /* search for the algo in the backend in priority or the frontend */
741 if ((s->be->comp && (comp_algo_back = s->be->comp->algos)) ||
742 (strm_fe(s)->comp && (comp_algo_back = strm_fe(s)->comp->algos))) {
743 int best_q = 0;
744
745 ctx.blk = NULL;
746 while (http_find_header(htx, ist("Accept-Encoding"), &ctx, 0)) {
747 const char *qval;
748 int q;
749 int toklen;
750
751 /* try to isolate the token from the optional q-value */
752 toklen = 0;
753 while (toklen < ctx.value.len && HTTP_IS_TOKEN(*(ctx.value.ptr + toklen)))
754 toklen++;
755
756 qval = ctx.value.ptr + toklen;
757 while (1) {
758 while (qval < ctx.value.ptr + ctx.value.len && HTTP_IS_LWS(*qval))
759 qval++;
760
761 if (qval >= ctx.value.ptr + ctx.value.len || *qval != ';') {
762 qval = NULL;
763 break;
764 }
765 qval++;
766
767 while (qval < ctx.value.ptr + ctx.value.len && HTTP_IS_LWS(*qval))
768 qval++;
769
770 if (qval >= ctx.value.ptr + ctx.value.len) {
771 qval = NULL;
772 break;
773 }
774 if (strncmp(qval, "q=", MIN(ctx.value.ptr + ctx.value.len - qval, 2)) == 0)
775 break;
776
777 while (qval < ctx.value.ptr + ctx.value.len && *qval != ';')
778 qval++;
779 }
780
781 /* here we have qval pointing to the first "q=" attribute or NULL if not found */
782 q = qval ? http_parse_qvalue(qval + 2, NULL) : 1000;
783
784 if (q <= best_q)
785 continue;
786
787 for (comp_algo = comp_algo_back; comp_algo; comp_algo = comp_algo->next) {
788 if (*(ctx.value.ptr) == '*' ||
789 word_match(ctx.value.ptr, toklen, comp_algo->ua_name, comp_algo->ua_name_len)) {
790 st->comp_algo = comp_algo;
791 best_q = q;
792 break;
793 }
794 }
795 }
796 }
797
798 /* remove all occurrences of the header when "compression offload" is set */
799 if (st->comp_algo) {
800 if ((s->be->comp && s->be->comp->offload) ||
801 (strm_fe(s)->comp && strm_fe(s)->comp->offload)) {
802 http_remove_header(htx, &ctx);
803 ctx.blk = NULL;
804 while (http_find_header(htx, ist("Accept-Encoding"), &ctx, 1))
805 http_remove_header(htx, &ctx);
806 }
Christopher Faulet3d97c902015-12-09 14:59:38 +0100807 return 1;
808 }
809
810 /* identity is implicit does not require headers */
Christopher Faulet92d36382015-11-05 13:35:03 +0100811 if ((s->be->comp && (comp_algo_back = s->be->comp->algos)) ||
812 (strm_fe(s)->comp && (comp_algo_back = strm_fe(s)->comp->algos))) {
Christopher Faulet3d97c902015-12-09 14:59:38 +0100813 for (comp_algo = comp_algo_back; comp_algo; comp_algo = comp_algo->next) {
814 if (comp_algo->cfg_name_len == 8 && memcmp(comp_algo->cfg_name, "identity", 8) == 0) {
Christopher Faulet92d36382015-11-05 13:35:03 +0100815 st->comp_algo = comp_algo;
Christopher Faulet3d97c902015-12-09 14:59:38 +0100816 return 1;
817 }
818 }
819 }
820
Christopher Faulet92d36382015-11-05 13:35:03 +0100821 st->comp_algo = NULL;
Christopher Faulet3d97c902015-12-09 14:59:38 +0100822 return 0;
823}
824
Christopher Faulete6902cd2018-11-30 22:29:48 +0100825static int
826select_compression_request_header(struct comp_state *st, struct stream *s,
827 struct http_msg *msg)
828{
829 if (IS_HTX_STRM(s))
830 return htx_select_comp_reqhdr(st, s, msg);
831 else
832 return http_select_comp_reqhdr(st, s, msg);
833}
Christopher Faulet92d36382015-11-05 13:35:03 +0100834
Christopher Faulet3d97c902015-12-09 14:59:38 +0100835/*
836 * Selects a comression algorithm depending of the server response.
837 */
Christopher Faulet92d36382015-11-05 13:35:03 +0100838static int
Christopher Faulete6902cd2018-11-30 22:29:48 +0100839http_select_comp_reshdr(struct comp_state *st, struct stream *s, struct http_msg *msg)
Christopher Faulet3d97c902015-12-09 14:59:38 +0100840{
841 struct http_txn *txn = s->txn;
Olivier Houchard0b662842018-06-29 18:16:31 +0200842 struct channel *c = msg->chn;
Christopher Faulet3d97c902015-12-09 14:59:38 +0100843 struct hdr_ctx ctx;
844 struct comp_type *comp_type;
845
846 /* no common compression algorithm was found in request header */
Christopher Faulet92d36382015-11-05 13:35:03 +0100847 if (st->comp_algo == NULL)
Christopher Faulet3d97c902015-12-09 14:59:38 +0100848 goto fail;
849
Christopher Faulet1d3613a2019-01-07 14:41:59 +0100850 /* compression already in progress */
851 if (msg->flags & HTTP_MSGF_COMPRESSING)
852 goto fail;
853
Christopher Faulet3d97c902015-12-09 14:59:38 +0100854 /* HTTP < 1.1 should not be compressed */
855 if (!(msg->flags & HTTP_MSGF_VER_11) || !(txn->req.flags & HTTP_MSGF_VER_11))
856 goto fail;
857
Christopher Faulet92d36382015-11-05 13:35:03 +0100858 if (txn->meth == HTTP_METH_HEAD)
859 goto fail;
860
Christopher Faulet3d97c902015-12-09 14:59:38 +0100861 /* compress 200,201,202,203 responses only */
862 if ((txn->status != 200) &&
863 (txn->status != 201) &&
864 (txn->status != 202) &&
865 (txn->status != 203))
866 goto fail;
867
868
869 /* Content-Length is null */
870 if (!(msg->flags & HTTP_MSGF_TE_CHNK) && msg->body_len == 0)
871 goto fail;
872
873 /* content is already compressed */
874 ctx.idx = 0;
Olivier Houchard0b662842018-06-29 18:16:31 +0200875 if (http_find_header2("Content-Encoding", 16, ci_head(c), &txn->hdr_idx, &ctx))
Christopher Faulet3d97c902015-12-09 14:59:38 +0100876 goto fail;
877
878 /* no compression when Cache-Control: no-transform is present in the message */
879 ctx.idx = 0;
Olivier Houchard0b662842018-06-29 18:16:31 +0200880 while (http_find_header2("Cache-Control", 13, ci_head(c), &txn->hdr_idx, &ctx)) {
Christopher Faulet3d97c902015-12-09 14:59:38 +0100881 if (word_match(ctx.line + ctx.val, ctx.vlen, "no-transform", 12))
882 goto fail;
883 }
884
Tim Duesterhusb229f012019-01-29 16:38:56 +0100885 /* no compression when ETag is malformed */
886 ctx.idx = 0;
887 if (http_find_full_header2("ETag", 4, ci_head(c), &txn->hdr_idx, &ctx)) {
888 if (!(((ctx.vlen >= 4 && memcmp(ctx.line + ctx.val, "W/\"", 3) == 0) || /* Either a weak ETag */
889 (ctx.vlen >= 2 && ctx.line[ctx.val] == '"')) && /* or strong ETag */
890 ctx.line[ctx.val + ctx.vlen - 1] == '"')) {
891 goto fail;
892 }
893 }
894 /* no compression when multiple ETags are present
895 * Note: Do not reset ctx.idx!
896 */
897 if (http_find_full_header2("ETag", 4, ci_head(c), &txn->hdr_idx, &ctx))
898 goto fail;
899
Christopher Faulet3d97c902015-12-09 14:59:38 +0100900 comp_type = NULL;
901
902 /* we don't want to compress multipart content-types, nor content-types that are
903 * not listed in the "compression type" directive if any. If no content-type was
904 * found but configuration requires one, we don't compress either. Backend has
905 * the priority.
906 */
907 ctx.idx = 0;
Olivier Houchard0b662842018-06-29 18:16:31 +0200908 if (http_find_header2("Content-Type", 12, ci_head(c), &txn->hdr_idx, &ctx)) {
Christopher Faulet3d97c902015-12-09 14:59:38 +0100909 if (ctx.vlen >= 9 && strncasecmp("multipart", ctx.line+ctx.val, 9) == 0)
910 goto fail;
911
912 if ((s->be->comp && (comp_type = s->be->comp->types)) ||
913 (strm_fe(s)->comp && (comp_type = strm_fe(s)->comp->types))) {
914 for (; comp_type; comp_type = comp_type->next) {
915 if (ctx.vlen >= comp_type->name_len &&
916 strncasecmp(ctx.line+ctx.val, comp_type->name, comp_type->name_len) == 0)
917 /* this Content-Type should be compressed */
918 break;
919 }
920 /* this Content-Type should not be compressed */
921 if (comp_type == NULL)
922 goto fail;
923 }
924 }
925 else { /* no content-type header */
Christopher Faulet92d36382015-11-05 13:35:03 +0100926 if ((s->be->comp && s->be->comp->types) ||
927 (strm_fe(s)->comp && strm_fe(s)->comp->types))
Christopher Faulet3d97c902015-12-09 14:59:38 +0100928 goto fail; /* a content-type was required */
929 }
930
931 /* limit compression rate */
932 if (global.comp_rate_lim > 0)
933 if (read_freq_ctr(&global.comp_bps_in) > global.comp_rate_lim)
934 goto fail;
935
936 /* limit cpu usage */
Willy Tarreau81036f22019-05-20 19:24:50 +0200937 if (ti->idle_pct < compress_min_idle)
Christopher Faulet3d97c902015-12-09 14:59:38 +0100938 goto fail;
939
940 /* initialize compression */
Christopher Faulet92d36382015-11-05 13:35:03 +0100941 if (st->comp_algo->init(&st->comp_ctx, global.tune.comp_maxlevel) < 0)
Christopher Faulet3d97c902015-12-09 14:59:38 +0100942 goto fail;
Christopher Faulet92d36382015-11-05 13:35:03 +0100943 msg->flags |= HTTP_MSGF_COMPRESSING;
Christopher Faulet3d97c902015-12-09 14:59:38 +0100944 return 1;
945
946fail:
Christopher Faulet92d36382015-11-05 13:35:03 +0100947 st->comp_algo = NULL;
Christopher Faulet3d97c902015-12-09 14:59:38 +0100948 return 0;
949}
950
Christopher Faulete6902cd2018-11-30 22:29:48 +0100951static int
952htx_select_comp_reshdr(struct comp_state *st, struct stream *s, struct http_msg *msg)
953{
Christopher Faulet27ba2dc2018-12-05 11:53:24 +0100954 struct htx *htx = htxbuf(&msg->chn->buf);
Christopher Faulete6902cd2018-11-30 22:29:48 +0100955 struct http_txn *txn = s->txn;
956 struct http_hdr_ctx ctx;
957 struct comp_type *comp_type;
958
959 /* no common compression algorithm was found in request header */
960 if (st->comp_algo == NULL)
961 goto fail;
962
Christopher Faulet1d3613a2019-01-07 14:41:59 +0100963 /* compression already in progress */
964 if (msg->flags & HTTP_MSGF_COMPRESSING)
965 goto fail;
966
Christopher Faulete6902cd2018-11-30 22:29:48 +0100967 /* HTTP < 1.1 should not be compressed */
968 if (!(msg->flags & HTTP_MSGF_VER_11) || !(txn->req.flags & HTTP_MSGF_VER_11))
969 goto fail;
970
971 if (txn->meth == HTTP_METH_HEAD)
972 goto fail;
973
974 /* compress 200,201,202,203 responses only */
975 if ((txn->status != 200) &&
976 (txn->status != 201) &&
977 (txn->status != 202) &&
978 (txn->status != 203))
979 goto fail;
980
Christopher Fauletc963eb22018-12-21 14:53:54 +0100981 if (!(msg->flags & HTTP_MSGF_XFER_LEN) || msg->flags & HTTP_MSGF_BODYLESS)
Christopher Faulete6902cd2018-11-30 22:29:48 +0100982 goto fail;
983
984 /* content is already compressed */
985 ctx.blk = NULL;
986 if (http_find_header(htx, ist("Content-Encoding"), &ctx, 1))
987 goto fail;
988
989 /* no compression when Cache-Control: no-transform is present in the message */
990 ctx.blk = NULL;
991 while (http_find_header(htx, ist("Cache-Control"), &ctx, 0)) {
992 if (word_match(ctx.value.ptr, ctx.value.len, "no-transform", 12))
993 goto fail;
994 }
995
Tim Duesterhusb229f012019-01-29 16:38:56 +0100996 /* no compression when ETag is malformed */
997 ctx.blk = NULL;
998 if (http_find_header(htx, ist("ETag"), &ctx, 1)) {
999 if (!(((ctx.value.len >= 4 && memcmp(ctx.value.ptr, "W/\"", 3) == 0) || /* Either a weak ETag */
1000 (ctx.value.len >= 2 && ctx.value.ptr[0] == '"')) && /* or strong ETag */
1001 ctx.value.ptr[ctx.value.len - 1] == '"')) {
1002 goto fail;
1003 }
1004 }
1005 /* no compression when multiple ETags are present
1006 * Note: Do not reset ctx.blk!
1007 */
1008 if (http_find_header(htx, ist("ETag"), &ctx, 1))
1009 goto fail;
1010
Christopher Faulete6902cd2018-11-30 22:29:48 +01001011 comp_type = NULL;
1012
1013 /* we don't want to compress multipart content-types, nor content-types that are
1014 * not listed in the "compression type" directive if any. If no content-type was
1015 * found but configuration requires one, we don't compress either. Backend has
1016 * the priority.
1017 */
1018 ctx.blk = NULL;
1019 if (http_find_header(htx, ist("Content-Type"), &ctx, 1)) {
1020 if (ctx.value.len >= 9 && strncasecmp("multipart", ctx.value.ptr, 9) == 0)
1021 goto fail;
1022
1023 if ((s->be->comp && (comp_type = s->be->comp->types)) ||
1024 (strm_fe(s)->comp && (comp_type = strm_fe(s)->comp->types))) {
1025 for (; comp_type; comp_type = comp_type->next) {
1026 if (ctx.value.len >= comp_type->name_len &&
1027 strncasecmp(ctx.value.ptr, comp_type->name, comp_type->name_len) == 0)
1028 /* this Content-Type should be compressed */
1029 break;
1030 }
1031 /* this Content-Type should not be compressed */
1032 if (comp_type == NULL)
1033 goto fail;
1034 }
1035 }
1036 else { /* no content-type header */
1037 if ((s->be->comp && s->be->comp->types) ||
1038 (strm_fe(s)->comp && strm_fe(s)->comp->types))
1039 goto fail; /* a content-type was required */
1040 }
1041
1042 /* limit compression rate */
1043 if (global.comp_rate_lim > 0)
1044 if (read_freq_ctr(&global.comp_bps_in) > global.comp_rate_lim)
1045 goto fail;
1046
1047 /* limit cpu usage */
Willy Tarreau81036f22019-05-20 19:24:50 +02001048 if (ti->idle_pct < compress_min_idle)
Christopher Faulete6902cd2018-11-30 22:29:48 +01001049 goto fail;
1050
1051 /* initialize compression */
1052 if (st->comp_algo->init(&st->comp_ctx, global.tune.comp_maxlevel) < 0)
1053 goto fail;
Christopher Faulete6902cd2018-11-30 22:29:48 +01001054 msg->flags |= HTTP_MSGF_COMPRESSING;
1055 return 1;
1056
1057 deinit_comp_ctx:
1058 st->comp_algo->end(&st->comp_ctx);
1059 fail:
1060 st->comp_algo = NULL;
1061 return 0;
1062}
1063
1064static int
1065select_compression_response_header(struct comp_state *st, struct stream *s, struct http_msg *msg)
1066{
1067 if (IS_HTX_STRM(s))
1068 return htx_select_comp_reshdr(st, s, msg);
1069 else
1070 return http_select_comp_reshdr(st, s, msg);
1071}
Christopher Faulet3d97c902015-12-09 14:59:38 +01001072/***********************************************************************/
1073/* emit the chunksize followed by a CRLF on the output and return the number of
1074 * bytes written. It goes backwards and starts with the byte before <end>. It
1075 * returns the number of bytes written which will not exceed 10 (8 digits, CR,
1076 * and LF). The caller is responsible for ensuring there is enough room left in
1077 * the output buffer for the string.
1078 */
1079static int
1080http_emit_chunk_size(char *end, unsigned int chksz)
1081{
1082 char *beg = end;
1083
1084 *--beg = '\n';
1085 *--beg = '\r';
1086 do {
1087 *--beg = hextab[chksz & 0xF];
1088 } while (chksz >>= 4);
1089 return end - beg;
1090}
1091
1092/*
1093 * Init HTTP compression
1094 */
Christopher Faulet92d36382015-11-05 13:35:03 +01001095static int
Christopher Fauletb61481c2018-12-17 13:17:53 +01001096http_compression_buffer_init(struct channel *inc, struct buffer *out)
Christopher Faulet3d97c902015-12-09 14:59:38 +01001097{
1098 /* output stream requires at least 10 bytes for the gzip header, plus
1099 * at least 8 bytes for the gzip trailer (crc+len), plus a possible
1100 * plus at most 5 bytes per 32kB block and 2 bytes to close the stream.
1101 */
Olivier Houchard0b662842018-06-29 18:16:31 +02001102 if (c_room(inc) < 20 + 5 * ((ci_data(inc) + 32767) >> 15))
Christopher Faulet3d97c902015-12-09 14:59:38 +01001103 return -1;
1104
1105 /* prepare an empty output buffer in which we reserve enough room for
1106 * copying the output bytes from <in>, plus 10 extra bytes to write
1107 * the chunk size. We don't copy the bytes yet so that if we have to
1108 * cancel the operation later, it's cheap.
1109 */
1110 b_reset(out);
Christopher Fauletb61481c2018-12-17 13:17:53 +01001111 out->head += co_data(inc) + 10;
Christopher Faulet3d97c902015-12-09 14:59:38 +01001112 return 0;
1113}
1114
Christopher Faulete6902cd2018-11-30 22:29:48 +01001115static int
1116htx_compression_buffer_init(struct htx *htx, struct buffer *out)
1117{
1118 /* output stream requires at least 10 bytes for the gzip header, plus
1119 * at least 8 bytes for the gzip trailer (crc+len), plus a possible
1120 * plus at most 5 bytes per 32kB block and 2 bytes to close the stream.
1121 */
1122 if (htx_free_space(htx) < 20 + 5 * ((htx->data + 32767) >> 15))
1123 return -1;
1124 b_reset(out);
1125 return 0;
1126}
1127
Christopher Faulet3d97c902015-12-09 14:59:38 +01001128/*
1129 * Add data to compress
1130 */
Christopher Faulet92d36382015-11-05 13:35:03 +01001131static int
1132http_compression_buffer_add_data(struct comp_state *st, struct buffer *in,
Willy Tarreaud54a8ce2018-06-29 18:42:02 +02001133 int in_out, struct buffer *out, int sz)
Christopher Faulet3d97c902015-12-09 14:59:38 +01001134{
Christopher Faulet3d97c902015-12-09 14:59:38 +01001135 int consumed_data = 0;
1136 int data_process_len;
1137 int block1, block2;
1138
Christopher Faulet92d36382015-11-05 13:35:03 +01001139 if (!sz)
Christopher Faulet3e7bc672015-12-07 13:39:08 +01001140 goto end;
Christopher Faulet3d97c902015-12-09 14:59:38 +01001141
Christopher Faulet92d36382015-11-05 13:35:03 +01001142 /* select the smallest size between the announced chunk size, the input
Christopher Faulet3d97c902015-12-09 14:59:38 +01001143 * data, and the available output buffer size. The compressors are
Christopher Faulet92d36382015-11-05 13:35:03 +01001144 * assumed to be able to process all the bytes we pass to them at
1145 * once. */
Willy Tarreaueac52592018-06-15 13:59:36 +02001146 data_process_len = MIN(b_room(out), sz);
Christopher Faulet92d36382015-11-05 13:35:03 +01001147
Christopher Faulet3d97c902015-12-09 14:59:38 +01001148 block1 = data_process_len;
Willy Tarreaud54a8ce2018-06-29 18:42:02 +02001149 if (block1 > b_contig_data(in, in_out))
1150 block1 = b_contig_data(in, in_out);
Christopher Faulet3d97c902015-12-09 14:59:38 +01001151 block2 = data_process_len - block1;
1152
1153 /* compressors return < 0 upon error or the amount of bytes read */
Christopher Faulet96667202018-12-17 12:02:57 +01001154 consumed_data = st->comp_algo->add_data(st->comp_ctx, b_peek(in, in_out), block1, out);
Christopher Faulet3e7bc672015-12-07 13:39:08 +01001155 if (consumed_data != block1 || !block2)
1156 goto end;
Christopher Faulet96667202018-12-17 12:02:57 +01001157 consumed_data = st->comp_algo->add_data(st->comp_ctx, b_orig(in), block2, out);
Christopher Faulet3e7bc672015-12-07 13:39:08 +01001158 if (consumed_data < 0)
1159 goto end;
1160 consumed_data += block1;
1161
1162 end:
Christopher Faulet3d97c902015-12-09 14:59:38 +01001163 return consumed_data;
1164}
1165
Christopher Faulete6902cd2018-11-30 22:29:48 +01001166static int
1167htx_compression_buffer_add_data(struct comp_state *st, const char *data, size_t len,
1168 struct buffer *out)
1169{
1170 return st->comp_algo->add_data(st->comp_ctx, data, len, out);
1171}
1172
Christopher Faulet3d97c902015-12-09 14:59:38 +01001173/*
1174 * Flush data in process, and write the header and footer of the chunk. Upon
1175 * success, in and out buffers are swapped to avoid a copy.
1176 */
Christopher Faulet92d36382015-11-05 13:35:03 +01001177static int
1178http_compression_buffer_end(struct comp_state *st, struct stream *s,
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001179 struct channel *chn, struct buffer *out,
Christopher Fauletb61481c2018-12-17 13:17:53 +01001180 int end)
Christopher Faulet3d97c902015-12-09 14:59:38 +01001181{
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001182 struct buffer tmp_buf;
Christopher Faulet3d97c902015-12-09 14:59:38 +01001183 char *tail;
Christopher Faulet92d36382015-11-05 13:35:03 +01001184 int to_forward, left;
Christopher Faulet3d97c902015-12-09 14:59:38 +01001185
1186#if defined(USE_SLZ) || defined(USE_ZLIB)
1187 int ret;
1188
1189 /* flush data here */
Christopher Faulet3d97c902015-12-09 14:59:38 +01001190 if (end)
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001191 ret = st->comp_algo->finish(st->comp_ctx, out); /* end of data */
Christopher Faulet3d97c902015-12-09 14:59:38 +01001192 else
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001193 ret = st->comp_algo->flush(st->comp_ctx, out); /* end of buffer */
Christopher Faulet3d97c902015-12-09 14:59:38 +01001194
1195 if (ret < 0)
1196 return -1; /* flush failed */
1197
1198#endif /* USE_ZLIB */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001199 if (b_data(out) == 0) {
Christopher Faulet3d97c902015-12-09 14:59:38 +01001200 /* No data were appended, let's drop the output buffer and
1201 * keep the input buffer unchanged.
1202 */
1203 return 0;
1204 }
1205
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001206 /* OK so at this stage, we have an output buffer <out> looking like this :
Christopher Faulet3d97c902015-12-09 14:59:38 +01001207 *
1208 * <-- o --> <------ i ----->
1209 * +---------+---+------------+-----------+
1210 * | out | c | comp_in | empty |
1211 * +---------+---+------------+-----------+
1212 * data p size
1213 *
Willy Tarreaud54a8ce2018-06-29 18:42:02 +02001214 * <out> is the room reserved to copy the channel output. It starts at
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001215 * out->area and has not yet been filled. <c> is the room reserved to
Willy Tarreaud54a8ce2018-06-29 18:42:02 +02001216 * write the chunk size (10 bytes). <comp_in> is the compressed
1217 * equivalent of the data part of ib->len. <empty> is the amount of
1218 * empty bytes at the end of the buffer, into which we may have to
1219 * copy the remaining bytes from ib->len after the data
1220 * (chunk size, trailers, ...).
Christopher Faulet3d97c902015-12-09 14:59:38 +01001221 */
1222
Joseph Herlant942eea32018-11-15 13:57:22 -08001223 /* Write real size at the beginning of the chunk, no need of wrapping.
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001224 * We write the chunk using a dynamic length and adjust out->p and out->i
Christopher Faulet3d97c902015-12-09 14:59:38 +01001225 * accordingly afterwards. That will move <out> away from <data>.
1226 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001227 left = http_emit_chunk_size(b_head(out), b_data(out));
1228 b_add(out, left);
Christopher Fauletb61481c2018-12-17 13:17:53 +01001229 out->head -= co_data(chn) + (left);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001230 /* Copy previous data from chn into out */
Willy Tarreaud54a8ce2018-06-29 18:42:02 +02001231 if (co_data(chn) > 0) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001232 left = b_contig_data(&chn->buf, 0);
Christopher Fauletb61481c2018-12-17 13:17:53 +01001233 if (left > co_data(chn))
1234 left = co_data(chn);
Christopher Faulet3d97c902015-12-09 14:59:38 +01001235
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001236 memcpy(b_head(out), co_head(chn), left);
1237 b_add(out, left);
Willy Tarreaud54a8ce2018-06-29 18:42:02 +02001238 if (co_data(chn) - left) {/* second part of the buffer */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001239 memcpy(b_head(out) + left, b_orig(&chn->buf), co_data(chn) - left);
1240 b_add(out, co_data(chn) - left);
Willy Tarreaud54a8ce2018-06-29 18:42:02 +02001241 }
Christopher Faulet3d97c902015-12-09 14:59:38 +01001242 }
1243
1244 /* chunked encoding requires CRLF after data */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001245 tail = b_tail(out);
Christopher Faulet3d97c902015-12-09 14:59:38 +01001246 *tail++ = '\r';
1247 *tail++ = '\n';
1248
Christopher Faulet2fb28802015-12-01 10:40:57 +01001249 /* At the end of data, we must write the empty chunk 0<CRLF>,
1250 * and terminate the trailers section with a last <CRLF>. If
1251 * we're forwarding a chunked-encoded response, we'll have a
1252 * trailers section after the empty chunk which needs to be
1253 * forwarded and which will provide the last CRLF. Otherwise
1254 * we write it ourselves.
1255 */
1256 if (end) {
1257 struct http_msg *msg = &s->txn->rsp;
1258
1259 memcpy(tail, "0\r\n", 3);
1260 tail += 3;
Christopher Fauletb77c5c22015-12-07 16:48:42 +01001261 if (!(msg->flags & HTTP_MSGF_TE_CHNK)) {
Christopher Faulet2fb28802015-12-01 10:40:57 +01001262 memcpy(tail, "\r\n", 2);
1263 tail += 2;
1264 }
1265 }
1266
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001267 b_add(out, tail - b_tail(out));
Christopher Fauletb61481c2018-12-17 13:17:53 +01001268 to_forward = b_data(out) - co_data(chn);
Christopher Faulet3d97c902015-12-09 14:59:38 +01001269
1270 /* update input rate */
Christopher Faulet92d36382015-11-05 13:35:03 +01001271 if (st->comp_ctx && st->comp_ctx->cur_lvl > 0) {
Christopher Faulet2fb28802015-12-01 10:40:57 +01001272 update_freq_ctr(&global.comp_bps_in, st->consumed);
Olivier Houchard43da3432019-03-08 18:50:27 +01001273 _HA_ATOMIC_ADD(&strm_fe(s)->fe_counters.comp_in, st->consumed);
1274 _HA_ATOMIC_ADD(&s->be->be_counters.comp_in, st->consumed);
Christopher Faulet3d97c902015-12-09 14:59:38 +01001275 } else {
Olivier Houchard43da3432019-03-08 18:50:27 +01001276 _HA_ATOMIC_ADD(&strm_fe(s)->fe_counters.comp_byp, st->consumed);
1277 _HA_ATOMIC_ADD(&s->be->be_counters.comp_byp, st->consumed);
Christopher Faulet3d97c902015-12-09 14:59:38 +01001278 }
1279
1280 /* copy the remaining data in the tmp buffer. */
Willy Tarreaubcbd3932018-06-06 07:13:22 +02001281 c_adv(chn, st->consumed);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001282 if (b_data(&chn->buf) - co_data(chn) > 0) {
Willy Tarreau7194d3c2018-06-06 16:55:45 +02001283 left = ci_contig_data(chn);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001284 memcpy(b_tail(out), ci_head(chn), left);
1285 b_add(out, left);
1286 if (b_data(&chn->buf) - (co_data(chn) + left)) {
1287 memcpy(b_tail(out), b_orig(&chn->buf), b_data(&chn->buf) - left);
1288 b_add(out, b_data(&chn->buf) - left);
Christopher Faulet3d97c902015-12-09 14:59:38 +01001289 }
1290 }
Christopher Fauletb61481c2018-12-17 13:17:53 +01001291 c_rew(chn, st->consumed);
1292
Christopher Faulet3d97c902015-12-09 14:59:38 +01001293 /* swap the buffers */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001294 tmp_buf = chn->buf;
1295 chn->buf = *out;
1296 *out = tmp_buf;
1297
Christopher Faulet92d36382015-11-05 13:35:03 +01001298 if (st->comp_ctx && st->comp_ctx->cur_lvl > 0) {
Christopher Faulet3d97c902015-12-09 14:59:38 +01001299 update_freq_ctr(&global.comp_bps_out, to_forward);
Olivier Houchard43da3432019-03-08 18:50:27 +01001300 _HA_ATOMIC_ADD(&strm_fe(s)->fe_counters.comp_out, to_forward);
1301 _HA_ATOMIC_ADD(&s->be->be_counters.comp_out, to_forward);
Christopher Faulet3d97c902015-12-09 14:59:38 +01001302 }
1303
Christopher Faulet3d97c902015-12-09 14:59:38 +01001304 return to_forward;
1305}
1306
Christopher Faulete6902cd2018-11-30 22:29:48 +01001307static int
1308htx_compression_buffer_end(struct comp_state *st, struct buffer *out, int end)
1309{
1310 if (end)
1311 return st->comp_algo->finish(st->comp_ctx, out);
1312 else
1313 return st->comp_algo->flush(st->comp_ctx, out);
1314}
1315
Christopher Faulet3d97c902015-12-09 14:59:38 +01001316
1317/***********************************************************************/
Christopher Faulet92d36382015-11-05 13:35:03 +01001318struct flt_ops comp_ops = {
Christopher Faulete6902cd2018-11-30 22:29:48 +01001319 .init = comp_flt_init,
Christopher Faulet8ca3b4b2017-07-25 11:07:15 +02001320 .init_per_thread = comp_flt_init_per_thread,
1321 .deinit_per_thread = comp_flt_deinit_per_thread,
Christopher Faulet92d36382015-11-05 13:35:03 +01001322
1323 .channel_start_analyze = comp_start_analyze,
Christopher Faulet92d36382015-11-05 13:35:03 +01001324 .channel_end_analyze = comp_end_analyze,
Christopher Faulet3dc860d2017-09-15 11:39:36 +02001325 .channel_post_analyze = comp_http_post_analyze,
Christopher Faulet92d36382015-11-05 13:35:03 +01001326
Christopher Faulet1339d742016-05-11 16:48:33 +02001327 .http_headers = comp_http_headers,
Christopher Faulete6902cd2018-11-30 22:29:48 +01001328 .http_payload = comp_http_payload,
1329 .http_end = comp_http_end,
1330
Christopher Faulet309c6412015-12-02 09:57:32 +01001331 .http_data = comp_http_data,
1332 .http_chunk_trailers = comp_http_chunk_trailers,
1333 .http_forward_data = comp_http_forward_data,
Christopher Faulet92d36382015-11-05 13:35:03 +01001334};
1335
Christopher Faulet3d97c902015-12-09 14:59:38 +01001336static int
1337parse_compression_options(char **args, int section, struct proxy *proxy,
1338 struct proxy *defpx, const char *file, int line,
1339 char **err)
1340{
Christopher Faulet92d36382015-11-05 13:35:03 +01001341 struct comp *comp;
Christopher Faulet3d97c902015-12-09 14:59:38 +01001342
1343 if (proxy->comp == NULL) {
Vincent Bernat02779b62016-04-03 13:48:43 +02001344 comp = calloc(1, sizeof(*comp));
Christopher Faulet3d97c902015-12-09 14:59:38 +01001345 proxy->comp = comp;
1346 }
1347 else
1348 comp = proxy->comp;
1349
1350 if (!strcmp(args[1], "algo")) {
1351 struct comp_ctx *ctx;
1352 int cur_arg = 2;
1353
1354 if (!*args[cur_arg]) {
1355 memprintf(err, "parsing [%s:%d] : '%s' expects <algorithm>\n",
1356 file, line, args[0]);
1357 return -1;
1358 }
1359 while (*(args[cur_arg])) {
Remi Tricot-Le Bretondcec0ae2021-05-17 10:35:08 +02001360 int retval = comp_append_algo(comp, args[cur_arg]);
1361 if (retval) {
1362 if (retval < 0)
1363 memprintf(err, "'%s' : '%s' is not a supported algorithm.\n",
1364 args[0], args[cur_arg]);
1365 else
1366 memprintf(err, "'%s' : out of memory while parsing algo '%s'.\n",
1367 args[0], args[cur_arg]);
Christopher Faulet3d97c902015-12-09 14:59:38 +01001368 return -1;
1369 }
Remi Tricot-Le Bretondcec0ae2021-05-17 10:35:08 +02001370
Christopher Faulet3d97c902015-12-09 14:59:38 +01001371 if (proxy->comp->algos->init(&ctx, 9) == 0)
1372 proxy->comp->algos->end(&ctx);
1373 else {
1374 memprintf(err, "'%s' : Can't init '%s' algorithm.\n",
1375 args[0], args[cur_arg]);
1376 return -1;
1377 }
1378 cur_arg++;
1379 continue;
1380 }
1381 }
1382 else if (!strcmp(args[1], "offload"))
1383 comp->offload = 1;
1384 else if (!strcmp(args[1], "type")) {
1385 int cur_arg = 2;
1386
1387 if (!*args[cur_arg]) {
1388 memprintf(err, "'%s' expects <type>\n", args[0]);
1389 return -1;
1390 }
1391 while (*(args[cur_arg])) {
Remi Tricot-Le Bretondcec0ae2021-05-17 10:35:08 +02001392 if (comp_append_type(comp, args[cur_arg])) {
1393 memprintf(err, "'%s': out of memory.", args[0]);
1394 return -1;
1395 }
Christopher Faulet3d97c902015-12-09 14:59:38 +01001396 cur_arg++;
1397 continue;
1398 }
1399 }
1400 else {
1401 memprintf(err, "'%s' expects 'algo', 'type' or 'offload'\n",
1402 args[0]);
1403 return -1;
1404 }
1405
1406 return 0;
1407}
1408
Christopher Faulet92d36382015-11-05 13:35:03 +01001409static int
1410parse_http_comp_flt(char **args, int *cur_arg, struct proxy *px,
Thierry Fournier3610c392016-04-13 18:27:51 +02001411 struct flt_conf *fconf, char **err, void *private)
Christopher Faulet92d36382015-11-05 13:35:03 +01001412{
Christopher Faulet443ea1a2016-02-04 13:40:26 +01001413 struct flt_conf *fc, *back;
Christopher Faulet92d36382015-11-05 13:35:03 +01001414
Christopher Faulet443ea1a2016-02-04 13:40:26 +01001415 list_for_each_entry_safe(fc, back, &px->filter_configs, list) {
1416 if (fc->id == http_comp_flt_id) {
Christopher Faulet92d36382015-11-05 13:35:03 +01001417 memprintf(err, "%s: Proxy supports only one compression filter\n", px->id);
1418 return -1;
1419 }
1420 }
1421
Christopher Faulet443ea1a2016-02-04 13:40:26 +01001422 fconf->id = http_comp_flt_id;
1423 fconf->conf = NULL;
1424 fconf->ops = &comp_ops;
Christopher Faulet92d36382015-11-05 13:35:03 +01001425 (*cur_arg)++;
1426
1427 return 0;
1428}
1429
1430
1431int
Christopher Fauletc9df7f72018-12-10 16:14:04 +01001432check_implicit_http_comp_flt(struct proxy *proxy)
Christopher Faulet92d36382015-11-05 13:35:03 +01001433{
Christopher Faulet443ea1a2016-02-04 13:40:26 +01001434 struct flt_conf *fconf;
Christopher Faulet27d93c32018-12-15 22:32:02 +01001435 int explicit = 0;
1436 int comp = 0;
Christopher Faulet92d36382015-11-05 13:35:03 +01001437 int err = 0;
1438
1439 if (proxy->comp == NULL)
1440 goto end;
Christopher Faulet443ea1a2016-02-04 13:40:26 +01001441 if (!LIST_ISEMPTY(&proxy->filter_configs)) {
1442 list_for_each_entry(fconf, &proxy->filter_configs, list) {
1443 if (fconf->id == http_comp_flt_id)
Christopher Faulet27d93c32018-12-15 22:32:02 +01001444 comp = 1;
1445 else if (fconf->id == cache_store_flt_id) {
1446 if (comp) {
1447 ha_alert("config: %s '%s': unable to enable the compression filter "
1448 "before any cache filter.\n",
1449 proxy_type_str(proxy), proxy->id);
1450 err++;
1451 goto end;
1452 }
1453 }
1454 else
1455 explicit = 1;
Christopher Faulet92d36382015-11-05 13:35:03 +01001456 }
Christopher Faulet27d93c32018-12-15 22:32:02 +01001457 }
1458 if (comp)
1459 goto end;
1460 else if (explicit) {
1461 ha_alert("config: %s '%s': require an explicit filter declaration to use "
1462 "HTTP compression\n", proxy_type_str(proxy), proxy->id);
Christopher Faulet92d36382015-11-05 13:35:03 +01001463 err++;
1464 goto end;
1465 }
1466
Christopher Faulet27d93c32018-12-15 22:32:02 +01001467 /* Implicit declaration of the compression filter is always the last
1468 * one */
Christopher Faulet443ea1a2016-02-04 13:40:26 +01001469 fconf = calloc(1, sizeof(*fconf));
1470 if (!fconf) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001471 ha_alert("config: %s '%s': out of memory\n",
1472 proxy_type_str(proxy), proxy->id);
Christopher Faulet92d36382015-11-05 13:35:03 +01001473 err++;
1474 goto end;
1475 }
Christopher Faulet443ea1a2016-02-04 13:40:26 +01001476 fconf->id = http_comp_flt_id;
1477 fconf->conf = NULL;
1478 fconf->ops = &comp_ops;
1479 LIST_ADDQ(&proxy->filter_configs, &fconf->list);
Christopher Faulet92d36382015-11-05 13:35:03 +01001480 end:
1481 return err;
1482}
1483
1484/*
1485 * boolean, returns true if compression is used (either gzip or deflate) in the
1486 * response.
1487 */
Christopher Faulet3d97c902015-12-09 14:59:38 +01001488static int
Christopher Faulet92d36382015-11-05 13:35:03 +01001489smp_fetch_res_comp(const struct arg *args, struct sample *smp, const char *kw,
1490 void *private)
Christopher Faulet3d97c902015-12-09 14:59:38 +01001491{
Willy Tarreaube508f12016-03-10 11:47:01 +01001492 struct http_txn *txn = smp->strm ? smp->strm->txn : NULL;
Christopher Faulet92d36382015-11-05 13:35:03 +01001493
Christopher Faulet3d97c902015-12-09 14:59:38 +01001494 smp->data.type = SMP_T_BOOL;
Christopher Faulet92d36382015-11-05 13:35:03 +01001495 smp->data.u.sint = (txn && (txn->rsp.flags & HTTP_MSGF_COMPRESSING));
Christopher Faulet3d97c902015-12-09 14:59:38 +01001496 return 1;
1497}
1498
Christopher Faulet92d36382015-11-05 13:35:03 +01001499/*
1500 * string, returns algo
1501 */
Christopher Faulet3d97c902015-12-09 14:59:38 +01001502static int
Christopher Faulet92d36382015-11-05 13:35:03 +01001503smp_fetch_res_comp_algo(const struct arg *args, struct sample *smp,
1504 const char *kw, void *private)
Christopher Faulet3d97c902015-12-09 14:59:38 +01001505{
Willy Tarreaube508f12016-03-10 11:47:01 +01001506 struct http_txn *txn = smp->strm ? smp->strm->txn : NULL;
Christopher Faulet92d36382015-11-05 13:35:03 +01001507 struct filter *filter;
1508 struct comp_state *st;
1509
Christopher Faulet03d85532017-09-15 10:14:43 +02001510 if (!txn || !(txn->rsp.flags & HTTP_MSGF_COMPRESSING))
Christopher Faulet3d97c902015-12-09 14:59:38 +01001511 return 0;
1512
Christopher Fauletfcf035c2015-12-03 11:48:03 +01001513 list_for_each_entry(filter, &strm_flt(smp->strm)->filters, list) {
Christopher Faulet443ea1a2016-02-04 13:40:26 +01001514 if (FLT_ID(filter) != http_comp_flt_id)
Christopher Faulet92d36382015-11-05 13:35:03 +01001515 continue;
1516
1517 if (!(st = filter->ctx))
1518 break;
1519
1520 smp->data.type = SMP_T_STR;
1521 smp->flags = SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001522 smp->data.u.str.area = st->comp_algo->cfg_name;
1523 smp->data.u.str.data = st->comp_algo->cfg_name_len;
Christopher Faulet92d36382015-11-05 13:35:03 +01001524 return 1;
1525 }
1526 return 0;
Christopher Faulet3d97c902015-12-09 14:59:38 +01001527}
1528
1529/* Declare the config parser for "compression" keyword */
1530static struct cfg_kw_list cfg_kws = {ILH, {
1531 { CFG_LISTEN, "compression", parse_compression_options },
1532 { 0, NULL, NULL },
1533 }
1534};
1535
Willy Tarreau0108d902018-11-25 19:14:37 +01001536INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
1537
Christopher Faulet92d36382015-11-05 13:35:03 +01001538/* Declare the filter parser for "compression" keyword */
1539static struct flt_kw_list filter_kws = { "COMP", { }, {
Thierry Fournier3610c392016-04-13 18:27:51 +02001540 { "compression", parse_http_comp_flt, NULL },
1541 { NULL, NULL, NULL },
Christopher Faulet92d36382015-11-05 13:35:03 +01001542 }
1543};
1544
Willy Tarreau0108d902018-11-25 19:14:37 +01001545INITCALL1(STG_REGISTER, flt_register_keywords, &filter_kws);
1546
Christopher Faulet3d97c902015-12-09 14:59:38 +01001547/* Note: must not be declared <const> as its list will be overwritten */
1548static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
Christopher Faulet92d36382015-11-05 13:35:03 +01001549 { "res.comp", smp_fetch_res_comp, 0, NULL, SMP_T_BOOL, SMP_USE_HRSHP },
1550 { "res.comp_algo", smp_fetch_res_comp_algo, 0, NULL, SMP_T_STR, SMP_USE_HRSHP },
1551 { /* END */ },
1552 }
1553};
Christopher Faulet3d97c902015-12-09 14:59:38 +01001554
Willy Tarreau0108d902018-11-25 19:14:37 +01001555INITCALL1(STG_REGISTER, sample_register_fetches, &sample_fetch_keywords);