blob: 9ddc858372de2258667f743fe28133a3efac6503 [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>
15#include <common/mini-clist.h>
16#include <common/standard.h>
17
18#include <types/compression.h>
19#include <types/filters.h>
20#include <types/proto_http.h>
21#include <types/proxy.h>
22#include <types/sample.h>
23
24#include <proto/compression.h>
Christopher Faulet92d36382015-11-05 13:35:03 +010025#include <proto/filters.h>
Christopher Faulet3d97c902015-12-09 14:59:38 +010026#include <proto/hdr_idx.h>
27#include <proto/proto_http.h>
28#include <proto/sample.h>
29#include <proto/stream.h>
30
Christopher Faulet92d36382015-11-05 13:35:03 +010031static const char *http_comp_flt_id = "compression filter";
32
33struct flt_ops comp_ops;
34
35static struct buffer *tmpbuf = &buf_empty;
Christopher Fauletb77c5c22015-12-07 16:48:42 +010036static struct buffer *zbuf = &buf_empty;
Christopher Faulet92d36382015-11-05 13:35:03 +010037
Christopher Faulet92d36382015-11-05 13:35:03 +010038struct comp_state {
39 struct comp_ctx *comp_ctx; /* compression context */
40 struct comp_algo *comp_algo; /* compression algorithm if not NULL */
Christopher Fauletb77c5c22015-12-07 16:48:42 +010041 int hdrs_len;
42 int tlrs_len;
Christopher Faulet2fb28802015-12-01 10:40:57 +010043 int consumed;
44 int initialized;
Christopher Fauletb77c5c22015-12-07 16:48:42 +010045 int finished;
Christopher Faulet92d36382015-11-05 13:35:03 +010046};
47
Christopher Faulet92d36382015-11-05 13:35:03 +010048static int select_compression_request_header(struct comp_state *st,
49 struct stream *s,
50 struct http_msg *msg);
51static int select_compression_response_header(struct comp_state *st,
52 struct stream *s,
53 struct http_msg *msg);
54
55static int http_compression_buffer_init(struct buffer *in, struct buffer *out);
56static int http_compression_buffer_add_data(struct comp_state *st,
57 struct buffer *in,
58 struct buffer *out, int sz);
59static int http_compression_buffer_end(struct comp_state *st, struct stream *s,
60 struct buffer **in, struct buffer **out,
Christopher Faulet2fb28802015-12-01 10:40:57 +010061 int end);
Christopher Faulet92d36382015-11-05 13:35:03 +010062
63/***********************************************************************/
64static int
Christopher Faulet443ea1a2016-02-04 13:40:26 +010065comp_flt_init(struct proxy *px, struct flt_conf *fconf)
Christopher Faulet92d36382015-11-05 13:35:03 +010066{
67
Christopher Fauletb77c5c22015-12-07 16:48:42 +010068 if (!tmpbuf->size && b_alloc(&tmpbuf) == NULL)
69 return -1;
70 if (!zbuf->size && b_alloc(&zbuf) == NULL)
71 return -1;
Christopher Faulet92d36382015-11-05 13:35:03 +010072 return 0;
73}
74
75static void
Christopher Faulet443ea1a2016-02-04 13:40:26 +010076comp_flt_deinit(struct proxy *px, struct flt_conf *fconf)
Christopher Faulet92d36382015-11-05 13:35:03 +010077{
78 if (tmpbuf->size)
79 b_free(&tmpbuf);
Christopher Fauletb77c5c22015-12-07 16:48:42 +010080 if (zbuf->size)
81 b_free(&zbuf);
Christopher Faulet92d36382015-11-05 13:35:03 +010082}
83
84static int
85comp_start_analyze(struct stream *s, struct filter *filter, struct channel *chn)
86{
87 if (filter->ctx == NULL) {
88 struct comp_state *st;
89
90 if (!(st = malloc(sizeof(*st))))
91 return -1;
92
Christopher Faulet2fb28802015-12-01 10:40:57 +010093 st->comp_algo = NULL;
94 st->comp_ctx = NULL;
Christopher Fauletb77c5c22015-12-07 16:48:42 +010095 st->hdrs_len = 0;
96 st->tlrs_len = 0;
Christopher Faulet2fb28802015-12-01 10:40:57 +010097 st->consumed = 0;
98 st->initialized = 0;
Christopher Fauletb77c5c22015-12-07 16:48:42 +010099 st->finished = 0;
Christopher Faulet2fb28802015-12-01 10:40:57 +0100100 filter->ctx = st;
Christopher Faulet92d36382015-11-05 13:35:03 +0100101 }
102 return 1;
103}
104
105static int
Christopher Faulet92d36382015-11-05 13:35:03 +0100106comp_end_analyze(struct stream *s, struct filter *filter, struct channel *chn)
107{
108 struct comp_state *st = filter->ctx;
Christopher Faulet92d36382015-11-05 13:35:03 +0100109
110 if (!st || !(chn->flags & CF_ISRESP))
111 goto end;
112
Christopher Faulet92d36382015-11-05 13:35:03 +0100113 if (!st->comp_algo || !s->txn->status)
114 goto release_ctx;
115
116 if (strm_fe(s)->mode == PR_MODE_HTTP)
117 strm_fe(s)->fe_counters.p.http.comp_rsp++;
118 if ((s->flags & SF_BE_ASSIGNED) && (s->be->mode == PR_MODE_HTTP))
119 s->be->be_counters.p.http.comp_rsp++;
120
121 /* release any possible compression context */
122 st->comp_algo->end(&st->comp_ctx);
123
124 release_ctx:
125 free(st);
126 filter->ctx = NULL;
127 end:
128 return 1;
129}
130
131static int
Christopher Faulet1339d742016-05-11 16:48:33 +0200132comp_http_headers(struct stream *s, struct filter *filter, struct http_msg *msg)
133{
134 struct comp_state *st = filter->ctx;
135
136 if (!strm_fe(s)->comp && !s->be->comp)
137 goto end;
138
139 if (!(msg->chn->flags & CF_ISRESP))
140 select_compression_request_header(st, s, msg);
141 else {
142 select_compression_response_header(st, s, msg);
143 if (st->comp_algo) {
144 register_data_filter(s, msg->chn, filter);
145 st->hdrs_len = s->txn->rsp.sov;
146 }
147 }
148
149 end:
150 return 1;
151}
152
153static int
Christopher Faulet2fb28802015-12-01 10:40:57 +0100154comp_http_data(struct stream *s, struct filter *filter, struct http_msg *msg)
Christopher Faulet92d36382015-11-05 13:35:03 +0100155{
156 struct comp_state *st = filter->ctx;
Christopher Faulet3e7bc672015-12-07 13:39:08 +0100157 struct buffer *buf = msg->chn->buf;
158 unsigned int *nxt = &flt_rsp_nxt(filter);
Christopher Faulet2fb28802015-12-01 10:40:57 +0100159 unsigned int len;
Christopher Faulet92d36382015-11-05 13:35:03 +0100160 int ret;
161
Christopher Faulet3e7bc672015-12-07 13:39:08 +0100162 len = MIN(msg->chunk_len + msg->next, buf->i) - *nxt;
Christopher Faulet2fb28802015-12-01 10:40:57 +0100163 if (!len)
164 return len;
165
166 if (!st->initialized) {
Christopher Fauletb77c5c22015-12-07 16:48:42 +0100167 unsigned int fwd = flt_rsp_fwd(filter) + st->hdrs_len;
Christopher Faulet3e7bc672015-12-07 13:39:08 +0100168
Christopher Fauletb77c5c22015-12-07 16:48:42 +0100169 b_reset(tmpbuf);
Christopher Faulet3e7bc672015-12-07 13:39:08 +0100170 b_adv(buf, fwd);
Christopher Fauletb77c5c22015-12-07 16:48:42 +0100171 ret = http_compression_buffer_init(buf, zbuf);
Christopher Faulet3e7bc672015-12-07 13:39:08 +0100172 b_rew(buf, fwd);
Christopher Faulet2fb28802015-12-01 10:40:57 +0100173 if (ret < 0) {
174 msg->chn->flags |= CF_WAKE_WRITE;
175 return 0;
176 }
177 }
Christopher Fauletb77c5c22015-12-07 16:48:42 +0100178
179 if (msg->flags & HTTP_MSGF_TE_CHNK) {
180 int block = bi_contig_data(buf);
181
182 len = MIN(tmpbuf->size - buffer_len(tmpbuf), len);
183 if (len > block) {
184 memcpy(bi_end(tmpbuf), b_ptr(buf, *nxt), block);
185 memcpy(bi_end(tmpbuf)+block, buf->data, len - block);
186 }
187 else
188 memcpy(bi_end(tmpbuf), b_ptr(buf, *nxt), len);
189 tmpbuf->i += len;
190 ret = len;
191 }
192 else {
193 b_adv(buf, *nxt);
194 ret = http_compression_buffer_add_data(st, buf, zbuf, len);
195 b_rew(buf, *nxt);
196 if (ret < 0)
197 return ret;
198 }
Christopher Faulet92d36382015-11-05 13:35:03 +0100199
Christopher Faulet2fb28802015-12-01 10:40:57 +0100200 st->initialized = 1;
201 msg->next += ret;
202 msg->chunk_len -= ret;
Christopher Faulet3e7bc672015-12-07 13:39:08 +0100203 *nxt = msg->next;
Christopher Faulet2fb28802015-12-01 10:40:57 +0100204 return 0;
Christopher Faulet92d36382015-11-05 13:35:03 +0100205}
206
207static int
Christopher Faulet2fb28802015-12-01 10:40:57 +0100208comp_http_chunk_trailers(struct stream *s, struct filter *filter,
209 struct http_msg *msg)
Christopher Faulet92d36382015-11-05 13:35:03 +0100210{
211 struct comp_state *st = filter->ctx;
Christopher Faulet92d36382015-11-05 13:35:03 +0100212
Christopher Fauletb77c5c22015-12-07 16:48:42 +0100213 if (!st->initialized) {
214 if (!st->finished) {
215 struct buffer *buf = msg->chn->buf;
216 unsigned int fwd = flt_rsp_fwd(filter) + st->hdrs_len;
Christopher Faulet2fb28802015-12-01 10:40:57 +0100217
Christopher Fauletb77c5c22015-12-07 16:48:42 +0100218 b_reset(tmpbuf);
219 b_adv(buf, fwd);
220 http_compression_buffer_init(buf, zbuf);
221 b_rew(buf, fwd);
222 st->initialized = 1;
223 }
224 }
225 st->tlrs_len = msg->sol;
Christopher Faulet2fb28802015-12-01 10:40:57 +0100226 return 1;
Christopher Faulet92d36382015-11-05 13:35:03 +0100227}
228
Christopher Faulet2fb28802015-12-01 10:40:57 +0100229
Christopher Faulet92d36382015-11-05 13:35:03 +0100230static int
231comp_http_forward_data(struct stream *s, struct filter *filter,
232 struct http_msg *msg, unsigned int len)
233{
234 struct comp_state *st = filter->ctx;
Christopher Faulet2fb28802015-12-01 10:40:57 +0100235 int ret;
Christopher Faulet92d36382015-11-05 13:35:03 +0100236
Christopher Faulet2fb28802015-12-01 10:40:57 +0100237 /* To work, previous filters MUST forward all data */
Christopher Fauletb77c5c22015-12-07 16:48:42 +0100238 if (flt_rsp_fwd(filter) + len != flt_rsp_nxt(filter)) {
Christopher Faulet2fb28802015-12-01 10:40:57 +0100239 Warning("HTTP compression failed: unexpected behavior of previous filters\n");
240 return -1;
Christopher Faulet92d36382015-11-05 13:35:03 +0100241 }
242
Christopher Faulet2fb28802015-12-01 10:40:57 +0100243 if (!st->initialized) {
Christopher Fauletb77c5c22015-12-07 16:48:42 +0100244 if (!len) {
245 /* Nothing to foward */
246 ret = len;
247 }
248 else if (st->hdrs_len > len) {
249 /* Forward part of headers */
250 ret = len;
251 st->hdrs_len -= len;
252 }
253 else if (st->hdrs_len > 0) {
254 /* Forward remaining headers */
255 ret = st->hdrs_len;
256 st->hdrs_len = 0;
257 }
258 else if (msg->msg_state < HTTP_MSG_TRAILERS) {
259 /* Do not forward anything for now. This only happens
260 * with chunk-encoded responses. Waiting data are part
261 * of the chunk envelope (the chunk size or the chunk
262 * CRLF). These data will be skipped during the
263 * compression. */
264 ret = 0;
265 }
266 else {
267 /* Forward trailers data */
268 ret = len;
269 }
Christopher Faulet2fb28802015-12-01 10:40:57 +0100270 return ret;
Christopher Faulet92d36382015-11-05 13:35:03 +0100271 }
272
Christopher Fauletb77c5c22015-12-07 16:48:42 +0100273 if (msg->flags & HTTP_MSGF_TE_CHNK) {
274 ret = http_compression_buffer_add_data(st, tmpbuf, zbuf, tmpbuf->i);
275 if (ret != tmpbuf->i) {
276 Warning("HTTP compression failed: Must consume %d bytes but only %d bytes consumed\n",
277 tmpbuf->i, ret);
278 return -1;
279 }
280 }
281
282 st->consumed = len - st->hdrs_len - st->tlrs_len;
283 b_adv(msg->chn->buf, flt_rsp_fwd(filter) + st->hdrs_len);
284 ret = http_compression_buffer_end(st, s, &msg->chn->buf, &zbuf, msg->msg_state >= HTTP_MSG_TRAILERS);
285 b_rew(msg->chn->buf, flt_rsp_fwd(filter) + st->hdrs_len);
Christopher Faulet2fb28802015-12-01 10:40:57 +0100286 if (ret < 0)
287 return ret;
Christopher Faulet92d36382015-11-05 13:35:03 +0100288
Christopher Fauletb77c5c22015-12-07 16:48:42 +0100289 flt_change_forward_size(filter, msg->chn, ret - st->consumed);
290 msg->next += (ret - st->consumed);
291 ret += st->hdrs_len + st->tlrs_len;
292
Christopher Faulet2fb28802015-12-01 10:40:57 +0100293 st->initialized = 0;
Christopher Fauletb77c5c22015-12-07 16:48:42 +0100294 st->finished = (msg->msg_state >= HTTP_MSG_TRAILERS);
295 st->hdrs_len = 0;
296 st->tlrs_len = 0;
Christopher Faulet92d36382015-11-05 13:35:03 +0100297 return ret;
298}
Christopher Faulet3d97c902015-12-09 14:59:38 +0100299
300/***********************************************************************/
301/*
302 * Selects a compression algorithm depending on the client request.
303 */
304int
Christopher Faulet92d36382015-11-05 13:35:03 +0100305select_compression_request_header(struct comp_state *st, struct stream *s,
306 struct http_msg *msg)
Christopher Faulet3d97c902015-12-09 14:59:38 +0100307{
308 struct http_txn *txn = s->txn;
Christopher Faulet92d36382015-11-05 13:35:03 +0100309 struct buffer *req = msg->chn->buf;
Christopher Faulet3d97c902015-12-09 14:59:38 +0100310 struct hdr_ctx ctx;
311 struct comp_algo *comp_algo = NULL;
312 struct comp_algo *comp_algo_back = NULL;
313
314 /* Disable compression for older user agents announcing themselves as "Mozilla/4"
315 * unless they are known good (MSIE 6 with XP SP2, or MSIE 7 and later).
316 * See http://zoompf.com/2012/02/lose-the-wait-http-compression for more details.
317 */
318 ctx.idx = 0;
319 if (http_find_header2("User-Agent", 10, req->p, &txn->hdr_idx, &ctx) &&
320 ctx.vlen >= 9 &&
321 memcmp(ctx.line + ctx.val, "Mozilla/4", 9) == 0 &&
322 (ctx.vlen < 31 ||
323 memcmp(ctx.line + ctx.val + 25, "MSIE ", 5) != 0 ||
324 ctx.line[ctx.val + 30] < '6' ||
325 (ctx.line[ctx.val + 30] == '6' &&
326 (ctx.vlen < 54 || memcmp(ctx.line + 51, "SV1", 3) != 0)))) {
Christopher Faulet92d36382015-11-05 13:35:03 +0100327 st->comp_algo = NULL;
Christopher Faulet3d97c902015-12-09 14:59:38 +0100328 return 0;
329 }
330
331 /* search for the algo in the backend in priority or the frontend */
Christopher Faulet92d36382015-11-05 13:35:03 +0100332 if ((s->be->comp && (comp_algo_back = s->be->comp->algos)) ||
333 (strm_fe(s)->comp && (comp_algo_back = strm_fe(s)->comp->algos))) {
Christopher Faulet3d97c902015-12-09 14:59:38 +0100334 int best_q = 0;
335
336 ctx.idx = 0;
337 while (http_find_header2("Accept-Encoding", 15, req->p, &txn->hdr_idx, &ctx)) {
338 const char *qval;
339 int q;
340 int toklen;
341
342 /* try to isolate the token from the optional q-value */
343 toklen = 0;
344 while (toklen < ctx.vlen && http_is_token[(unsigned char)*(ctx.line + ctx.val + toklen)])
345 toklen++;
346
347 qval = ctx.line + ctx.val + toklen;
348 while (1) {
349 while (qval < ctx.line + ctx.val + ctx.vlen && http_is_lws[(unsigned char)*qval])
350 qval++;
351
352 if (qval >= ctx.line + ctx.val + ctx.vlen || *qval != ';') {
353 qval = NULL;
354 break;
355 }
356 qval++;
357
358 while (qval < ctx.line + ctx.val + ctx.vlen && http_is_lws[(unsigned char)*qval])
359 qval++;
360
361 if (qval >= ctx.line + ctx.val + ctx.vlen) {
362 qval = NULL;
363 break;
364 }
365 if (strncmp(qval, "q=", MIN(ctx.line + ctx.val + ctx.vlen - qval, 2)) == 0)
366 break;
367
368 while (qval < ctx.line + ctx.val + ctx.vlen && *qval != ';')
369 qval++;
370 }
371
372 /* here we have qval pointing to the first "q=" attribute or NULL if not found */
373 q = qval ? parse_qvalue(qval + 2, NULL) : 1000;
374
375 if (q <= best_q)
376 continue;
377
378 for (comp_algo = comp_algo_back; comp_algo; comp_algo = comp_algo->next) {
379 if (*(ctx.line + ctx.val) == '*' ||
380 word_match(ctx.line + ctx.val, toklen, comp_algo->ua_name, comp_algo->ua_name_len)) {
Christopher Faulet92d36382015-11-05 13:35:03 +0100381 st->comp_algo = comp_algo;
Christopher Faulet3d97c902015-12-09 14:59:38 +0100382 best_q = q;
383 break;
384 }
385 }
386 }
387 }
388
389 /* remove all occurrences of the header when "compression offload" is set */
Christopher Faulet92d36382015-11-05 13:35:03 +0100390 if (st->comp_algo) {
391 if ((s->be->comp && s->be->comp->offload) ||
392 (strm_fe(s)->comp && strm_fe(s)->comp->offload)) {
Christopher Faulet3d97c902015-12-09 14:59:38 +0100393 http_remove_header2(msg, &txn->hdr_idx, &ctx);
394 ctx.idx = 0;
395 while (http_find_header2("Accept-Encoding", 15, req->p, &txn->hdr_idx, &ctx)) {
396 http_remove_header2(msg, &txn->hdr_idx, &ctx);
397 }
398 }
399 return 1;
400 }
401
402 /* identity is implicit does not require headers */
Christopher Faulet92d36382015-11-05 13:35:03 +0100403 if ((s->be->comp && (comp_algo_back = s->be->comp->algos)) ||
404 (strm_fe(s)->comp && (comp_algo_back = strm_fe(s)->comp->algos))) {
Christopher Faulet3d97c902015-12-09 14:59:38 +0100405 for (comp_algo = comp_algo_back; comp_algo; comp_algo = comp_algo->next) {
406 if (comp_algo->cfg_name_len == 8 && memcmp(comp_algo->cfg_name, "identity", 8) == 0) {
Christopher Faulet92d36382015-11-05 13:35:03 +0100407 st->comp_algo = comp_algo;
Christopher Faulet3d97c902015-12-09 14:59:38 +0100408 return 1;
409 }
410 }
411 }
412
Christopher Faulet92d36382015-11-05 13:35:03 +0100413 st->comp_algo = NULL;
Christopher Faulet3d97c902015-12-09 14:59:38 +0100414 return 0;
415}
416
Christopher Faulet92d36382015-11-05 13:35:03 +0100417
Christopher Faulet3d97c902015-12-09 14:59:38 +0100418/*
419 * Selects a comression algorithm depending of the server response.
420 */
Christopher Faulet92d36382015-11-05 13:35:03 +0100421static int
422select_compression_response_header(struct comp_state *st, struct stream *s, struct http_msg *msg)
Christopher Faulet3d97c902015-12-09 14:59:38 +0100423{
424 struct http_txn *txn = s->txn;
Christopher Faulet92d36382015-11-05 13:35:03 +0100425 struct buffer *res = msg->chn->buf;
Christopher Faulet3d97c902015-12-09 14:59:38 +0100426 struct hdr_ctx ctx;
427 struct comp_type *comp_type;
428
429 /* no common compression algorithm was found in request header */
Christopher Faulet92d36382015-11-05 13:35:03 +0100430 if (st->comp_algo == NULL)
Christopher Faulet3d97c902015-12-09 14:59:38 +0100431 goto fail;
432
433 /* HTTP < 1.1 should not be compressed */
434 if (!(msg->flags & HTTP_MSGF_VER_11) || !(txn->req.flags & HTTP_MSGF_VER_11))
435 goto fail;
436
Christopher Faulet92d36382015-11-05 13:35:03 +0100437 if (txn->meth == HTTP_METH_HEAD)
438 goto fail;
439
Christopher Faulet3d97c902015-12-09 14:59:38 +0100440 /* compress 200,201,202,203 responses only */
441 if ((txn->status != 200) &&
442 (txn->status != 201) &&
443 (txn->status != 202) &&
444 (txn->status != 203))
445 goto fail;
446
447
448 /* Content-Length is null */
449 if (!(msg->flags & HTTP_MSGF_TE_CHNK) && msg->body_len == 0)
450 goto fail;
451
452 /* content is already compressed */
453 ctx.idx = 0;
454 if (http_find_header2("Content-Encoding", 16, res->p, &txn->hdr_idx, &ctx))
455 goto fail;
456
457 /* no compression when Cache-Control: no-transform is present in the message */
458 ctx.idx = 0;
459 while (http_find_header2("Cache-Control", 13, res->p, &txn->hdr_idx, &ctx)) {
460 if (word_match(ctx.line + ctx.val, ctx.vlen, "no-transform", 12))
461 goto fail;
462 }
463
464 comp_type = NULL;
465
466 /* we don't want to compress multipart content-types, nor content-types that are
467 * not listed in the "compression type" directive if any. If no content-type was
468 * found but configuration requires one, we don't compress either. Backend has
469 * the priority.
470 */
471 ctx.idx = 0;
472 if (http_find_header2("Content-Type", 12, res->p, &txn->hdr_idx, &ctx)) {
473 if (ctx.vlen >= 9 && strncasecmp("multipart", ctx.line+ctx.val, 9) == 0)
474 goto fail;
475
476 if ((s->be->comp && (comp_type = s->be->comp->types)) ||
477 (strm_fe(s)->comp && (comp_type = strm_fe(s)->comp->types))) {
478 for (; comp_type; comp_type = comp_type->next) {
479 if (ctx.vlen >= comp_type->name_len &&
480 strncasecmp(ctx.line+ctx.val, comp_type->name, comp_type->name_len) == 0)
481 /* this Content-Type should be compressed */
482 break;
483 }
484 /* this Content-Type should not be compressed */
485 if (comp_type == NULL)
486 goto fail;
487 }
488 }
489 else { /* no content-type header */
Christopher Faulet92d36382015-11-05 13:35:03 +0100490 if ((s->be->comp && s->be->comp->types) ||
491 (strm_fe(s)->comp && strm_fe(s)->comp->types))
Christopher Faulet3d97c902015-12-09 14:59:38 +0100492 goto fail; /* a content-type was required */
493 }
494
495 /* limit compression rate */
496 if (global.comp_rate_lim > 0)
497 if (read_freq_ctr(&global.comp_bps_in) > global.comp_rate_lim)
498 goto fail;
499
500 /* limit cpu usage */
501 if (idle_pct < compress_min_idle)
502 goto fail;
503
504 /* initialize compression */
Christopher Faulet92d36382015-11-05 13:35:03 +0100505 if (st->comp_algo->init(&st->comp_ctx, global.tune.comp_maxlevel) < 0)
Christopher Faulet3d97c902015-12-09 14:59:38 +0100506 goto fail;
507
Christopher Faulet3d97c902015-12-09 14:59:38 +0100508 /* remove Content-Length header */
509 ctx.idx = 0;
510 if ((msg->flags & HTTP_MSGF_CNT_LEN) && http_find_header2("Content-Length", 14, res->p, &txn->hdr_idx, &ctx))
511 http_remove_header2(msg, &txn->hdr_idx, &ctx);
512
513 /* add Transfer-Encoding header */
514 if (!(msg->flags & HTTP_MSGF_TE_CHNK))
515 http_header_add_tail2(&txn->rsp, &txn->hdr_idx, "Transfer-Encoding: chunked", 26);
516
517 /*
518 * Add Content-Encoding header when it's not identity encoding.
519 * RFC 2616 : Identity encoding: This content-coding is used only in the
520 * Accept-Encoding header, and SHOULD NOT be used in the Content-Encoding
521 * header.
522 */
Christopher Faulet92d36382015-11-05 13:35:03 +0100523 if (st->comp_algo->cfg_name_len != 8 || memcmp(st->comp_algo->cfg_name, "identity", 8) != 0) {
Christopher Faulet3d97c902015-12-09 14:59:38 +0100524 trash.len = 18;
525 memcpy(trash.str, "Content-Encoding: ", trash.len);
Christopher Faulet92d36382015-11-05 13:35:03 +0100526 memcpy(trash.str + trash.len, st->comp_algo->ua_name, st->comp_algo->ua_name_len);
527 trash.len += st->comp_algo->ua_name_len;
Christopher Faulet3d97c902015-12-09 14:59:38 +0100528 trash.str[trash.len] = '\0';
529 http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len);
530 }
Christopher Faulet92d36382015-11-05 13:35:03 +0100531 msg->flags |= HTTP_MSGF_COMPRESSING;
Christopher Faulet3d97c902015-12-09 14:59:38 +0100532 return 1;
533
534fail:
Christopher Faulet92d36382015-11-05 13:35:03 +0100535 st->comp_algo = NULL;
Christopher Faulet3d97c902015-12-09 14:59:38 +0100536 return 0;
537}
538
539/***********************************************************************/
540/* emit the chunksize followed by a CRLF on the output and return the number of
541 * bytes written. It goes backwards and starts with the byte before <end>. It
542 * returns the number of bytes written which will not exceed 10 (8 digits, CR,
543 * and LF). The caller is responsible for ensuring there is enough room left in
544 * the output buffer for the string.
545 */
546static int
547http_emit_chunk_size(char *end, unsigned int chksz)
548{
549 char *beg = end;
550
551 *--beg = '\n';
552 *--beg = '\r';
553 do {
554 *--beg = hextab[chksz & 0xF];
555 } while (chksz >>= 4);
556 return end - beg;
557}
558
559/*
560 * Init HTTP compression
561 */
Christopher Faulet92d36382015-11-05 13:35:03 +0100562static int
563http_compression_buffer_init(struct buffer *in, struct buffer *out)
Christopher Faulet3d97c902015-12-09 14:59:38 +0100564{
565 /* output stream requires at least 10 bytes for the gzip header, plus
566 * at least 8 bytes for the gzip trailer (crc+len), plus a possible
567 * plus at most 5 bytes per 32kB block and 2 bytes to close the stream.
568 */
569 if (in->size - buffer_len(in) < 20 + 5 * ((in->i + 32767) >> 15))
570 return -1;
571
572 /* prepare an empty output buffer in which we reserve enough room for
573 * copying the output bytes from <in>, plus 10 extra bytes to write
574 * the chunk size. We don't copy the bytes yet so that if we have to
575 * cancel the operation later, it's cheap.
576 */
577 b_reset(out);
578 out->o = in->o;
579 out->p += out->o;
580 out->i = 10;
581 return 0;
582}
583
584/*
585 * Add data to compress
586 */
Christopher Faulet92d36382015-11-05 13:35:03 +0100587static int
588http_compression_buffer_add_data(struct comp_state *st, struct buffer *in,
589 struct buffer *out, int sz)
Christopher Faulet3d97c902015-12-09 14:59:38 +0100590{
Christopher Faulet3d97c902015-12-09 14:59:38 +0100591 int consumed_data = 0;
592 int data_process_len;
593 int block1, block2;
594
Christopher Faulet92d36382015-11-05 13:35:03 +0100595 if (!sz)
Christopher Faulet3e7bc672015-12-07 13:39:08 +0100596 goto end;
Christopher Faulet3d97c902015-12-09 14:59:38 +0100597
Christopher Faulet92d36382015-11-05 13:35:03 +0100598 /* select the smallest size between the announced chunk size, the input
Christopher Faulet3d97c902015-12-09 14:59:38 +0100599 * data, and the available output buffer size. The compressors are
Christopher Faulet92d36382015-11-05 13:35:03 +0100600 * assumed to be able to process all the bytes we pass to them at
601 * once. */
Christopher Faulet3e7bc672015-12-07 13:39:08 +0100602 data_process_len = MIN(out->size - buffer_len(out), sz);
Christopher Faulet92d36382015-11-05 13:35:03 +0100603
Christopher Faulet3d97c902015-12-09 14:59:38 +0100604 block1 = data_process_len;
605 if (block1 > bi_contig_data(in))
606 block1 = bi_contig_data(in);
607 block2 = data_process_len - block1;
608
609 /* compressors return < 0 upon error or the amount of bytes read */
Christopher Faulet92d36382015-11-05 13:35:03 +0100610 consumed_data = st->comp_algo->add_data(st->comp_ctx, bi_ptr(in), block1, out);
Christopher Faulet3e7bc672015-12-07 13:39:08 +0100611 if (consumed_data != block1 || !block2)
612 goto end;
613 consumed_data = st->comp_algo->add_data(st->comp_ctx, in->data, block2, out);
614 if (consumed_data < 0)
615 goto end;
616 consumed_data += block1;
617
618 end:
Christopher Faulet3d97c902015-12-09 14:59:38 +0100619 return consumed_data;
620}
621
622/*
623 * Flush data in process, and write the header and footer of the chunk. Upon
624 * success, in and out buffers are swapped to avoid a copy.
625 */
Christopher Faulet92d36382015-11-05 13:35:03 +0100626static int
627http_compression_buffer_end(struct comp_state *st, struct stream *s,
628 struct buffer **in, struct buffer **out,
Christopher Faulet2fb28802015-12-01 10:40:57 +0100629 int end)
Christopher Faulet3d97c902015-12-09 14:59:38 +0100630{
Christopher Faulet3d97c902015-12-09 14:59:38 +0100631 struct buffer *ib = *in, *ob = *out;
632 char *tail;
Christopher Faulet92d36382015-11-05 13:35:03 +0100633 int to_forward, left;
Christopher Faulet3d97c902015-12-09 14:59:38 +0100634
635#if defined(USE_SLZ) || defined(USE_ZLIB)
636 int ret;
637
638 /* flush data here */
Christopher Faulet3d97c902015-12-09 14:59:38 +0100639 if (end)
Christopher Faulet92d36382015-11-05 13:35:03 +0100640 ret = st->comp_algo->finish(st->comp_ctx, ob); /* end of data */
Christopher Faulet3d97c902015-12-09 14:59:38 +0100641 else
Christopher Faulet92d36382015-11-05 13:35:03 +0100642 ret = st->comp_algo->flush(st->comp_ctx, ob); /* end of buffer */
Christopher Faulet3d97c902015-12-09 14:59:38 +0100643
644 if (ret < 0)
645 return -1; /* flush failed */
646
647#endif /* USE_ZLIB */
Christopher Faulet3d97c902015-12-09 14:59:38 +0100648 if (ob->i == 10) {
649 /* No data were appended, let's drop the output buffer and
650 * keep the input buffer unchanged.
651 */
652 return 0;
653 }
654
655 /* OK so at this stage, we have an output buffer <ob> looking like this :
656 *
657 * <-- o --> <------ i ----->
658 * +---------+---+------------+-----------+
659 * | out | c | comp_in | empty |
660 * +---------+---+------------+-----------+
661 * data p size
662 *
663 * <out> is the room reserved to copy ib->o. It starts at ob->data and
664 * has not yet been filled. <c> is the room reserved to write the chunk
665 * size (10 bytes). <comp_in> is the compressed equivalent of the data
666 * part of ib->i. <empty> is the amount of empty bytes at the end of
667 * the buffer, into which we may have to copy the remaining bytes from
668 * ib->i after the data (chunk size, trailers, ...).
669 */
670
671 /* Write real size at the begining of the chunk, no need of wrapping.
672 * We write the chunk using a dynamic length and adjust ob->p and ob->i
673 * accordingly afterwards. That will move <out> away from <data>.
674 */
675 left = 10 - http_emit_chunk_size(ob->p + 10, ob->i - 10);
676 ob->p += left;
677 ob->i -= left;
678
679 /* Copy previous data from ib->o into ob->o */
680 if (ib->o > 0) {
681 left = bo_contig_data(ib);
682 memcpy(ob->p - ob->o, bo_ptr(ib), left);
683 if (ib->o - left) /* second part of the buffer */
684 memcpy(ob->p - ob->o + left, ib->data, ib->o - left);
685 }
686
687 /* chunked encoding requires CRLF after data */
688 tail = ob->p + ob->i;
689 *tail++ = '\r';
690 *tail++ = '\n';
691
Christopher Faulet2fb28802015-12-01 10:40:57 +0100692 /* At the end of data, we must write the empty chunk 0<CRLF>,
693 * and terminate the trailers section with a last <CRLF>. If
694 * we're forwarding a chunked-encoded response, we'll have a
695 * trailers section after the empty chunk which needs to be
696 * forwarded and which will provide the last CRLF. Otherwise
697 * we write it ourselves.
698 */
699 if (end) {
700 struct http_msg *msg = &s->txn->rsp;
701
702 memcpy(tail, "0\r\n", 3);
703 tail += 3;
Christopher Fauletb77c5c22015-12-07 16:48:42 +0100704 if (!(msg->flags & HTTP_MSGF_TE_CHNK)) {
Christopher Faulet2fb28802015-12-01 10:40:57 +0100705 memcpy(tail, "\r\n", 2);
706 tail += 2;
707 }
708 }
709
Christopher Faulet3d97c902015-12-09 14:59:38 +0100710 ob->i = tail - ob->p;
Christopher Faulet3d97c902015-12-09 14:59:38 +0100711 to_forward = ob->i;
712
713 /* update input rate */
Christopher Faulet92d36382015-11-05 13:35:03 +0100714 if (st->comp_ctx && st->comp_ctx->cur_lvl > 0) {
Christopher Faulet2fb28802015-12-01 10:40:57 +0100715 update_freq_ctr(&global.comp_bps_in, st->consumed);
716 strm_fe(s)->fe_counters.comp_in += st->consumed;
717 s->be->be_counters.comp_in += st->consumed;
Christopher Faulet3d97c902015-12-09 14:59:38 +0100718 } else {
Christopher Faulet2fb28802015-12-01 10:40:57 +0100719 strm_fe(s)->fe_counters.comp_byp += st->consumed;
720 s->be->be_counters.comp_byp += st->consumed;
Christopher Faulet3d97c902015-12-09 14:59:38 +0100721 }
722
723 /* copy the remaining data in the tmp buffer. */
Christopher Faulet2fb28802015-12-01 10:40:57 +0100724 b_adv(ib, st->consumed);
Christopher Faulet3d97c902015-12-09 14:59:38 +0100725 if (ib->i > 0) {
726 left = bi_contig_data(ib);
727 memcpy(ob->p + ob->i, bi_ptr(ib), left);
728 ob->i += left;
729 if (ib->i - left) {
730 memcpy(ob->p + ob->i, ib->data, ib->i - left);
731 ob->i += ib->i - left;
732 }
733 }
734
735 /* swap the buffers */
736 *in = ob;
737 *out = ib;
738
Christopher Faulet92d36382015-11-05 13:35:03 +0100739
740 if (st->comp_ctx && st->comp_ctx->cur_lvl > 0) {
Christopher Faulet3d97c902015-12-09 14:59:38 +0100741 update_freq_ctr(&global.comp_bps_out, to_forward);
742 strm_fe(s)->fe_counters.comp_out += to_forward;
743 s->be->be_counters.comp_out += to_forward;
744 }
745
Christopher Faulet3d97c902015-12-09 14:59:38 +0100746 return to_forward;
747}
748
749
750/***********************************************************************/
Christopher Faulet92d36382015-11-05 13:35:03 +0100751struct flt_ops comp_ops = {
752 .init = comp_flt_init,
753 .deinit = comp_flt_deinit,
754
755 .channel_start_analyze = comp_start_analyze,
Christopher Faulet92d36382015-11-05 13:35:03 +0100756 .channel_end_analyze = comp_end_analyze,
757
Christopher Faulet1339d742016-05-11 16:48:33 +0200758 .http_headers = comp_http_headers,
Christopher Faulet309c6412015-12-02 09:57:32 +0100759 .http_data = comp_http_data,
760 .http_chunk_trailers = comp_http_chunk_trailers,
761 .http_forward_data = comp_http_forward_data,
Christopher Faulet92d36382015-11-05 13:35:03 +0100762};
763
Christopher Faulet3d97c902015-12-09 14:59:38 +0100764static int
765parse_compression_options(char **args, int section, struct proxy *proxy,
766 struct proxy *defpx, const char *file, int line,
767 char **err)
768{
Christopher Faulet92d36382015-11-05 13:35:03 +0100769 struct comp *comp;
Christopher Faulet3d97c902015-12-09 14:59:38 +0100770
771 if (proxy->comp == NULL) {
Vincent Bernat02779b62016-04-03 13:48:43 +0200772 comp = calloc(1, sizeof(*comp));
Christopher Faulet3d97c902015-12-09 14:59:38 +0100773 proxy->comp = comp;
774 }
775 else
776 comp = proxy->comp;
777
778 if (!strcmp(args[1], "algo")) {
779 struct comp_ctx *ctx;
780 int cur_arg = 2;
781
782 if (!*args[cur_arg]) {
783 memprintf(err, "parsing [%s:%d] : '%s' expects <algorithm>\n",
784 file, line, args[0]);
785 return -1;
786 }
787 while (*(args[cur_arg])) {
788 if (comp_append_algo(comp, args[cur_arg]) < 0) {
789 memprintf(err, "'%s' : '%s' is not a supported algorithm.\n",
790 args[0], args[cur_arg]);
791 return -1;
792 }
793 if (proxy->comp->algos->init(&ctx, 9) == 0)
794 proxy->comp->algos->end(&ctx);
795 else {
796 memprintf(err, "'%s' : Can't init '%s' algorithm.\n",
797 args[0], args[cur_arg]);
798 return -1;
799 }
800 cur_arg++;
801 continue;
802 }
803 }
804 else if (!strcmp(args[1], "offload"))
805 comp->offload = 1;
806 else if (!strcmp(args[1], "type")) {
807 int cur_arg = 2;
808
809 if (!*args[cur_arg]) {
810 memprintf(err, "'%s' expects <type>\n", args[0]);
811 return -1;
812 }
813 while (*(args[cur_arg])) {
814 comp_append_type(comp, args[cur_arg]);
815 cur_arg++;
816 continue;
817 }
818 }
819 else {
820 memprintf(err, "'%s' expects 'algo', 'type' or 'offload'\n",
821 args[0]);
822 return -1;
823 }
824
825 return 0;
826}
827
Christopher Faulet92d36382015-11-05 13:35:03 +0100828static int
829parse_http_comp_flt(char **args, int *cur_arg, struct proxy *px,
Thierry Fournier3610c392016-04-13 18:27:51 +0200830 struct flt_conf *fconf, char **err, void *private)
Christopher Faulet92d36382015-11-05 13:35:03 +0100831{
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100832 struct flt_conf *fc, *back;
Christopher Faulet92d36382015-11-05 13:35:03 +0100833
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100834 list_for_each_entry_safe(fc, back, &px->filter_configs, list) {
835 if (fc->id == http_comp_flt_id) {
Christopher Faulet92d36382015-11-05 13:35:03 +0100836 memprintf(err, "%s: Proxy supports only one compression filter\n", px->id);
837 return -1;
838 }
839 }
840
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100841 fconf->id = http_comp_flt_id;
842 fconf->conf = NULL;
843 fconf->ops = &comp_ops;
Christopher Faulet92d36382015-11-05 13:35:03 +0100844 (*cur_arg)++;
845
846 return 0;
847}
848
849
850int
851check_legacy_http_comp_flt(struct proxy *proxy)
852{
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100853 struct flt_conf *fconf;
Christopher Faulet92d36382015-11-05 13:35:03 +0100854 int err = 0;
855
856 if (proxy->comp == NULL)
857 goto end;
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100858 if (!LIST_ISEMPTY(&proxy->filter_configs)) {
859 list_for_each_entry(fconf, &proxy->filter_configs, list) {
860 if (fconf->id == http_comp_flt_id)
Christopher Faulet92d36382015-11-05 13:35:03 +0100861 goto end;
862 }
863 Alert("config: %s '%s': require an explicit filter declaration to use HTTP compression\n",
864 proxy_type_str(proxy), proxy->id);
865 err++;
866 goto end;
867 }
868
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100869 fconf = calloc(1, sizeof(*fconf));
870 if (!fconf) {
Christopher Faulet92d36382015-11-05 13:35:03 +0100871 Alert("config: %s '%s': out of memory\n",
872 proxy_type_str(proxy), proxy->id);
873 err++;
874 goto end;
875 }
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100876 fconf->id = http_comp_flt_id;
877 fconf->conf = NULL;
878 fconf->ops = &comp_ops;
879 LIST_ADDQ(&proxy->filter_configs, &fconf->list);
Christopher Faulet92d36382015-11-05 13:35:03 +0100880
881 end:
882 return err;
883}
884
885/*
886 * boolean, returns true if compression is used (either gzip or deflate) in the
887 * response.
888 */
Christopher Faulet3d97c902015-12-09 14:59:38 +0100889static int
Christopher Faulet92d36382015-11-05 13:35:03 +0100890smp_fetch_res_comp(const struct arg *args, struct sample *smp, const char *kw,
891 void *private)
Christopher Faulet3d97c902015-12-09 14:59:38 +0100892{
Willy Tarreaube508f12016-03-10 11:47:01 +0100893 struct http_txn *txn = smp->strm ? smp->strm->txn : NULL;
Christopher Faulet92d36382015-11-05 13:35:03 +0100894
Christopher Faulet3d97c902015-12-09 14:59:38 +0100895 smp->data.type = SMP_T_BOOL;
Christopher Faulet92d36382015-11-05 13:35:03 +0100896 smp->data.u.sint = (txn && (txn->rsp.flags & HTTP_MSGF_COMPRESSING));
Christopher Faulet3d97c902015-12-09 14:59:38 +0100897 return 1;
898}
899
Christopher Faulet92d36382015-11-05 13:35:03 +0100900/*
901 * string, returns algo
902 */
Christopher Faulet3d97c902015-12-09 14:59:38 +0100903static int
Christopher Faulet92d36382015-11-05 13:35:03 +0100904smp_fetch_res_comp_algo(const struct arg *args, struct sample *smp,
905 const char *kw, void *private)
Christopher Faulet3d97c902015-12-09 14:59:38 +0100906{
Willy Tarreaube508f12016-03-10 11:47:01 +0100907 struct http_txn *txn = smp->strm ? smp->strm->txn : NULL;
Christopher Faulet92d36382015-11-05 13:35:03 +0100908 struct filter *filter;
909 struct comp_state *st;
910
911 if (!(txn || !(txn->rsp.flags & HTTP_MSGF_COMPRESSING)))
Christopher Faulet3d97c902015-12-09 14:59:38 +0100912 return 0;
913
Christopher Fauletfcf035c2015-12-03 11:48:03 +0100914 list_for_each_entry(filter, &strm_flt(smp->strm)->filters, list) {
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100915 if (FLT_ID(filter) != http_comp_flt_id)
Christopher Faulet92d36382015-11-05 13:35:03 +0100916 continue;
917
918 if (!(st = filter->ctx))
919 break;
920
921 smp->data.type = SMP_T_STR;
922 smp->flags = SMP_F_CONST;
923 smp->data.u.str.str = st->comp_algo->cfg_name;
924 smp->data.u.str.len = st->comp_algo->cfg_name_len;
925 return 1;
926 }
927 return 0;
Christopher Faulet3d97c902015-12-09 14:59:38 +0100928}
929
930/* Declare the config parser for "compression" keyword */
931static struct cfg_kw_list cfg_kws = {ILH, {
932 { CFG_LISTEN, "compression", parse_compression_options },
933 { 0, NULL, NULL },
934 }
935};
936
Christopher Faulet92d36382015-11-05 13:35:03 +0100937/* Declare the filter parser for "compression" keyword */
938static struct flt_kw_list filter_kws = { "COMP", { }, {
Thierry Fournier3610c392016-04-13 18:27:51 +0200939 { "compression", parse_http_comp_flt, NULL },
940 { NULL, NULL, NULL },
Christopher Faulet92d36382015-11-05 13:35:03 +0100941 }
942};
943
Christopher Faulet3d97c902015-12-09 14:59:38 +0100944/* Note: must not be declared <const> as its list will be overwritten */
945static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
Christopher Faulet92d36382015-11-05 13:35:03 +0100946 { "res.comp", smp_fetch_res_comp, 0, NULL, SMP_T_BOOL, SMP_USE_HRSHP },
947 { "res.comp_algo", smp_fetch_res_comp_algo, 0, NULL, SMP_T_STR, SMP_USE_HRSHP },
948 { /* END */ },
949 }
950};
Christopher Faulet3d97c902015-12-09 14:59:38 +0100951
952__attribute__((constructor))
Christopher Faulet92d36382015-11-05 13:35:03 +0100953static void
954__flt_http_comp_init(void)
Christopher Faulet3d97c902015-12-09 14:59:38 +0100955{
956 cfg_register_keywords(&cfg_kws);
Christopher Faulet92d36382015-11-05 13:35:03 +0100957 flt_register_keywords(&filter_kws);
Christopher Faulet3d97c902015-12-09 14:59:38 +0100958 sample_register_fetches(&sample_fetch_keywords);
959}