blob: f1e73970827f3fc80f1647d1b2d7c3552ee55f56 [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) {
Christopher Faulet06ecf3a2016-09-22 15:31:43 +0200180 int block;
Christopher Fauletb77c5c22015-12-07 16:48:42 +0100181
182 len = MIN(tmpbuf->size - buffer_len(tmpbuf), len);
Christopher Faulet06ecf3a2016-09-22 15:31:43 +0200183
184 b_adv(buf, *nxt);
185 block = bi_contig_data(buf);
186 memcpy(bi_end(tmpbuf), bi_ptr(buf), block);
187 if (len > block)
188 memcpy(bi_end(tmpbuf)+block, buf->data, len-block);
189 b_rew(buf, *nxt);
190
Christopher Fauletb77c5c22015-12-07 16:48:42 +0100191 tmpbuf->i += len;
192 ret = len;
193 }
194 else {
195 b_adv(buf, *nxt);
196 ret = http_compression_buffer_add_data(st, buf, zbuf, len);
197 b_rew(buf, *nxt);
198 if (ret < 0)
199 return ret;
200 }
Christopher Faulet92d36382015-11-05 13:35:03 +0100201
Christopher Faulet2fb28802015-12-01 10:40:57 +0100202 st->initialized = 1;
203 msg->next += ret;
204 msg->chunk_len -= ret;
Christopher Faulet3e7bc672015-12-07 13:39:08 +0100205 *nxt = msg->next;
Christopher Faulet2fb28802015-12-01 10:40:57 +0100206 return 0;
Christopher Faulet92d36382015-11-05 13:35:03 +0100207}
208
209static int
Christopher Faulet2fb28802015-12-01 10:40:57 +0100210comp_http_chunk_trailers(struct stream *s, struct filter *filter,
211 struct http_msg *msg)
Christopher Faulet92d36382015-11-05 13:35:03 +0100212{
213 struct comp_state *st = filter->ctx;
Christopher Faulet92d36382015-11-05 13:35:03 +0100214
Christopher Fauletb77c5c22015-12-07 16:48:42 +0100215 if (!st->initialized) {
216 if (!st->finished) {
217 struct buffer *buf = msg->chn->buf;
218 unsigned int fwd = flt_rsp_fwd(filter) + st->hdrs_len;
Christopher Faulet2fb28802015-12-01 10:40:57 +0100219
Christopher Fauletb77c5c22015-12-07 16:48:42 +0100220 b_reset(tmpbuf);
221 b_adv(buf, fwd);
222 http_compression_buffer_init(buf, zbuf);
223 b_rew(buf, fwd);
224 st->initialized = 1;
225 }
226 }
227 st->tlrs_len = msg->sol;
Christopher Faulet2fb28802015-12-01 10:40:57 +0100228 return 1;
Christopher Faulet92d36382015-11-05 13:35:03 +0100229}
230
Christopher Faulet2fb28802015-12-01 10:40:57 +0100231
Christopher Faulet92d36382015-11-05 13:35:03 +0100232static int
233comp_http_forward_data(struct stream *s, struct filter *filter,
234 struct http_msg *msg, unsigned int len)
235{
236 struct comp_state *st = filter->ctx;
Christopher Faulet2fb28802015-12-01 10:40:57 +0100237 int ret;
Christopher Faulet92d36382015-11-05 13:35:03 +0100238
Christopher Faulet2fb28802015-12-01 10:40:57 +0100239 /* To work, previous filters MUST forward all data */
Christopher Fauletb77c5c22015-12-07 16:48:42 +0100240 if (flt_rsp_fwd(filter) + len != flt_rsp_nxt(filter)) {
Christopher Faulet2fb28802015-12-01 10:40:57 +0100241 Warning("HTTP compression failed: unexpected behavior of previous filters\n");
242 return -1;
Christopher Faulet92d36382015-11-05 13:35:03 +0100243 }
244
Christopher Faulet2fb28802015-12-01 10:40:57 +0100245 if (!st->initialized) {
Christopher Fauletb77c5c22015-12-07 16:48:42 +0100246 if (!len) {
247 /* Nothing to foward */
248 ret = len;
249 }
250 else if (st->hdrs_len > len) {
251 /* Forward part of headers */
252 ret = len;
253 st->hdrs_len -= len;
254 }
255 else if (st->hdrs_len > 0) {
256 /* Forward remaining headers */
257 ret = st->hdrs_len;
258 st->hdrs_len = 0;
259 }
260 else if (msg->msg_state < HTTP_MSG_TRAILERS) {
261 /* Do not forward anything for now. This only happens
262 * with chunk-encoded responses. Waiting data are part
263 * of the chunk envelope (the chunk size or the chunk
264 * CRLF). These data will be skipped during the
265 * compression. */
266 ret = 0;
267 }
268 else {
269 /* Forward trailers data */
270 ret = len;
271 }
Christopher Faulet2fb28802015-12-01 10:40:57 +0100272 return ret;
Christopher Faulet92d36382015-11-05 13:35:03 +0100273 }
274
Christopher Fauletb77c5c22015-12-07 16:48:42 +0100275 if (msg->flags & HTTP_MSGF_TE_CHNK) {
276 ret = http_compression_buffer_add_data(st, tmpbuf, zbuf, tmpbuf->i);
277 if (ret != tmpbuf->i) {
278 Warning("HTTP compression failed: Must consume %d bytes but only %d bytes consumed\n",
279 tmpbuf->i, ret);
280 return -1;
281 }
282 }
283
284 st->consumed = len - st->hdrs_len - st->tlrs_len;
285 b_adv(msg->chn->buf, flt_rsp_fwd(filter) + st->hdrs_len);
286 ret = http_compression_buffer_end(st, s, &msg->chn->buf, &zbuf, msg->msg_state >= HTTP_MSG_TRAILERS);
287 b_rew(msg->chn->buf, flt_rsp_fwd(filter) + st->hdrs_len);
Christopher Faulet2fb28802015-12-01 10:40:57 +0100288 if (ret < 0)
289 return ret;
Christopher Faulet92d36382015-11-05 13:35:03 +0100290
Christopher Fauletb77c5c22015-12-07 16:48:42 +0100291 flt_change_forward_size(filter, msg->chn, ret - st->consumed);
292 msg->next += (ret - st->consumed);
293 ret += st->hdrs_len + st->tlrs_len;
294
Christopher Faulet2fb28802015-12-01 10:40:57 +0100295 st->initialized = 0;
Christopher Fauletb77c5c22015-12-07 16:48:42 +0100296 st->finished = (msg->msg_state >= HTTP_MSG_TRAILERS);
297 st->hdrs_len = 0;
298 st->tlrs_len = 0;
Christopher Faulet92d36382015-11-05 13:35:03 +0100299 return ret;
300}
Christopher Faulet3d97c902015-12-09 14:59:38 +0100301
302/***********************************************************************/
303/*
304 * Selects a compression algorithm depending on the client request.
305 */
306int
Christopher Faulet92d36382015-11-05 13:35:03 +0100307select_compression_request_header(struct comp_state *st, struct stream *s,
308 struct http_msg *msg)
Christopher Faulet3d97c902015-12-09 14:59:38 +0100309{
310 struct http_txn *txn = s->txn;
Christopher Faulet92d36382015-11-05 13:35:03 +0100311 struct buffer *req = msg->chn->buf;
Christopher Faulet3d97c902015-12-09 14:59:38 +0100312 struct hdr_ctx ctx;
313 struct comp_algo *comp_algo = NULL;
314 struct comp_algo *comp_algo_back = NULL;
315
316 /* Disable compression for older user agents announcing themselves as "Mozilla/4"
317 * unless they are known good (MSIE 6 with XP SP2, or MSIE 7 and later).
318 * See http://zoompf.com/2012/02/lose-the-wait-http-compression for more details.
319 */
320 ctx.idx = 0;
321 if (http_find_header2("User-Agent", 10, req->p, &txn->hdr_idx, &ctx) &&
322 ctx.vlen >= 9 &&
323 memcmp(ctx.line + ctx.val, "Mozilla/4", 9) == 0 &&
324 (ctx.vlen < 31 ||
325 memcmp(ctx.line + ctx.val + 25, "MSIE ", 5) != 0 ||
326 ctx.line[ctx.val + 30] < '6' ||
327 (ctx.line[ctx.val + 30] == '6' &&
328 (ctx.vlen < 54 || memcmp(ctx.line + 51, "SV1", 3) != 0)))) {
Christopher Faulet92d36382015-11-05 13:35:03 +0100329 st->comp_algo = NULL;
Christopher Faulet3d97c902015-12-09 14:59:38 +0100330 return 0;
331 }
332
333 /* search for the algo in the backend in priority or the frontend */
Christopher Faulet92d36382015-11-05 13:35:03 +0100334 if ((s->be->comp && (comp_algo_back = s->be->comp->algos)) ||
335 (strm_fe(s)->comp && (comp_algo_back = strm_fe(s)->comp->algos))) {
Christopher Faulet3d97c902015-12-09 14:59:38 +0100336 int best_q = 0;
337
338 ctx.idx = 0;
339 while (http_find_header2("Accept-Encoding", 15, req->p, &txn->hdr_idx, &ctx)) {
340 const char *qval;
341 int q;
342 int toklen;
343
344 /* try to isolate the token from the optional q-value */
345 toklen = 0;
Willy Tarreau2235b262016-11-05 15:50:20 +0100346 while (toklen < ctx.vlen && HTTP_IS_TOKEN(*(ctx.line + ctx.val + toklen)))
Christopher Faulet3d97c902015-12-09 14:59:38 +0100347 toklen++;
348
349 qval = ctx.line + ctx.val + toklen;
350 while (1) {
Willy Tarreau2235b262016-11-05 15:50:20 +0100351 while (qval < ctx.line + ctx.val + ctx.vlen && HTTP_IS_LWS(*qval))
Christopher Faulet3d97c902015-12-09 14:59:38 +0100352 qval++;
353
354 if (qval >= ctx.line + ctx.val + ctx.vlen || *qval != ';') {
355 qval = NULL;
356 break;
357 }
358 qval++;
359
Willy Tarreau2235b262016-11-05 15:50:20 +0100360 while (qval < ctx.line + ctx.val + ctx.vlen && HTTP_IS_LWS(*qval))
Christopher Faulet3d97c902015-12-09 14:59:38 +0100361 qval++;
362
363 if (qval >= ctx.line + ctx.val + ctx.vlen) {
364 qval = NULL;
365 break;
366 }
367 if (strncmp(qval, "q=", MIN(ctx.line + ctx.val + ctx.vlen - qval, 2)) == 0)
368 break;
369
370 while (qval < ctx.line + ctx.val + ctx.vlen && *qval != ';')
371 qval++;
372 }
373
374 /* here we have qval pointing to the first "q=" attribute or NULL if not found */
375 q = qval ? parse_qvalue(qval + 2, NULL) : 1000;
376
377 if (q <= best_q)
378 continue;
379
380 for (comp_algo = comp_algo_back; comp_algo; comp_algo = comp_algo->next) {
381 if (*(ctx.line + ctx.val) == '*' ||
382 word_match(ctx.line + ctx.val, toklen, comp_algo->ua_name, comp_algo->ua_name_len)) {
Christopher Faulet92d36382015-11-05 13:35:03 +0100383 st->comp_algo = comp_algo;
Christopher Faulet3d97c902015-12-09 14:59:38 +0100384 best_q = q;
385 break;
386 }
387 }
388 }
389 }
390
391 /* remove all occurrences of the header when "compression offload" is set */
Christopher Faulet92d36382015-11-05 13:35:03 +0100392 if (st->comp_algo) {
393 if ((s->be->comp && s->be->comp->offload) ||
394 (strm_fe(s)->comp && strm_fe(s)->comp->offload)) {
Christopher Faulet3d97c902015-12-09 14:59:38 +0100395 http_remove_header2(msg, &txn->hdr_idx, &ctx);
396 ctx.idx = 0;
397 while (http_find_header2("Accept-Encoding", 15, req->p, &txn->hdr_idx, &ctx)) {
398 http_remove_header2(msg, &txn->hdr_idx, &ctx);
399 }
400 }
401 return 1;
402 }
403
404 /* identity is implicit does not require headers */
Christopher Faulet92d36382015-11-05 13:35:03 +0100405 if ((s->be->comp && (comp_algo_back = s->be->comp->algos)) ||
406 (strm_fe(s)->comp && (comp_algo_back = strm_fe(s)->comp->algos))) {
Christopher Faulet3d97c902015-12-09 14:59:38 +0100407 for (comp_algo = comp_algo_back; comp_algo; comp_algo = comp_algo->next) {
408 if (comp_algo->cfg_name_len == 8 && memcmp(comp_algo->cfg_name, "identity", 8) == 0) {
Christopher Faulet92d36382015-11-05 13:35:03 +0100409 st->comp_algo = comp_algo;
Christopher Faulet3d97c902015-12-09 14:59:38 +0100410 return 1;
411 }
412 }
413 }
414
Christopher Faulet92d36382015-11-05 13:35:03 +0100415 st->comp_algo = NULL;
Christopher Faulet3d97c902015-12-09 14:59:38 +0100416 return 0;
417}
418
Christopher Faulet92d36382015-11-05 13:35:03 +0100419
Christopher Faulet3d97c902015-12-09 14:59:38 +0100420/*
421 * Selects a comression algorithm depending of the server response.
422 */
Christopher Faulet92d36382015-11-05 13:35:03 +0100423static int
424select_compression_response_header(struct comp_state *st, struct stream *s, struct http_msg *msg)
Christopher Faulet3d97c902015-12-09 14:59:38 +0100425{
426 struct http_txn *txn = s->txn;
Christopher Faulet92d36382015-11-05 13:35:03 +0100427 struct buffer *res = msg->chn->buf;
Christopher Faulet3d97c902015-12-09 14:59:38 +0100428 struct hdr_ctx ctx;
429 struct comp_type *comp_type;
430
431 /* no common compression algorithm was found in request header */
Christopher Faulet92d36382015-11-05 13:35:03 +0100432 if (st->comp_algo == NULL)
Christopher Faulet3d97c902015-12-09 14:59:38 +0100433 goto fail;
434
435 /* HTTP < 1.1 should not be compressed */
436 if (!(msg->flags & HTTP_MSGF_VER_11) || !(txn->req.flags & HTTP_MSGF_VER_11))
437 goto fail;
438
Christopher Faulet92d36382015-11-05 13:35:03 +0100439 if (txn->meth == HTTP_METH_HEAD)
440 goto fail;
441
Christopher Faulet3d97c902015-12-09 14:59:38 +0100442 /* compress 200,201,202,203 responses only */
443 if ((txn->status != 200) &&
444 (txn->status != 201) &&
445 (txn->status != 202) &&
446 (txn->status != 203))
447 goto fail;
448
449
450 /* Content-Length is null */
451 if (!(msg->flags & HTTP_MSGF_TE_CHNK) && msg->body_len == 0)
452 goto fail;
453
454 /* content is already compressed */
455 ctx.idx = 0;
456 if (http_find_header2("Content-Encoding", 16, res->p, &txn->hdr_idx, &ctx))
457 goto fail;
458
459 /* no compression when Cache-Control: no-transform is present in the message */
460 ctx.idx = 0;
461 while (http_find_header2("Cache-Control", 13, res->p, &txn->hdr_idx, &ctx)) {
462 if (word_match(ctx.line + ctx.val, ctx.vlen, "no-transform", 12))
463 goto fail;
464 }
465
466 comp_type = NULL;
467
468 /* we don't want to compress multipart content-types, nor content-types that are
469 * not listed in the "compression type" directive if any. If no content-type was
470 * found but configuration requires one, we don't compress either. Backend has
471 * the priority.
472 */
473 ctx.idx = 0;
474 if (http_find_header2("Content-Type", 12, res->p, &txn->hdr_idx, &ctx)) {
475 if (ctx.vlen >= 9 && strncasecmp("multipart", ctx.line+ctx.val, 9) == 0)
476 goto fail;
477
478 if ((s->be->comp && (comp_type = s->be->comp->types)) ||
479 (strm_fe(s)->comp && (comp_type = strm_fe(s)->comp->types))) {
480 for (; comp_type; comp_type = comp_type->next) {
481 if (ctx.vlen >= comp_type->name_len &&
482 strncasecmp(ctx.line+ctx.val, comp_type->name, comp_type->name_len) == 0)
483 /* this Content-Type should be compressed */
484 break;
485 }
486 /* this Content-Type should not be compressed */
487 if (comp_type == NULL)
488 goto fail;
489 }
490 }
491 else { /* no content-type header */
Christopher Faulet92d36382015-11-05 13:35:03 +0100492 if ((s->be->comp && s->be->comp->types) ||
493 (strm_fe(s)->comp && strm_fe(s)->comp->types))
Christopher Faulet3d97c902015-12-09 14:59:38 +0100494 goto fail; /* a content-type was required */
495 }
496
497 /* limit compression rate */
498 if (global.comp_rate_lim > 0)
499 if (read_freq_ctr(&global.comp_bps_in) > global.comp_rate_lim)
500 goto fail;
501
502 /* limit cpu usage */
503 if (idle_pct < compress_min_idle)
504 goto fail;
505
506 /* initialize compression */
Christopher Faulet92d36382015-11-05 13:35:03 +0100507 if (st->comp_algo->init(&st->comp_ctx, global.tune.comp_maxlevel) < 0)
Christopher Faulet3d97c902015-12-09 14:59:38 +0100508 goto fail;
509
Christopher Faulet3d97c902015-12-09 14:59:38 +0100510 /* remove Content-Length header */
511 ctx.idx = 0;
512 if ((msg->flags & HTTP_MSGF_CNT_LEN) && http_find_header2("Content-Length", 14, res->p, &txn->hdr_idx, &ctx))
513 http_remove_header2(msg, &txn->hdr_idx, &ctx);
514
515 /* add Transfer-Encoding header */
516 if (!(msg->flags & HTTP_MSGF_TE_CHNK))
517 http_header_add_tail2(&txn->rsp, &txn->hdr_idx, "Transfer-Encoding: chunked", 26);
518
519 /*
520 * Add Content-Encoding header when it's not identity encoding.
521 * RFC 2616 : Identity encoding: This content-coding is used only in the
522 * Accept-Encoding header, and SHOULD NOT be used in the Content-Encoding
523 * header.
524 */
Christopher Faulet92d36382015-11-05 13:35:03 +0100525 if (st->comp_algo->cfg_name_len != 8 || memcmp(st->comp_algo->cfg_name, "identity", 8) != 0) {
Christopher Faulet3d97c902015-12-09 14:59:38 +0100526 trash.len = 18;
527 memcpy(trash.str, "Content-Encoding: ", trash.len);
Christopher Faulet92d36382015-11-05 13:35:03 +0100528 memcpy(trash.str + trash.len, st->comp_algo->ua_name, st->comp_algo->ua_name_len);
529 trash.len += st->comp_algo->ua_name_len;
Christopher Faulet3d97c902015-12-09 14:59:38 +0100530 trash.str[trash.len] = '\0';
531 http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len);
532 }
Christopher Faulet92d36382015-11-05 13:35:03 +0100533 msg->flags |= HTTP_MSGF_COMPRESSING;
Christopher Faulet3d97c902015-12-09 14:59:38 +0100534 return 1;
535
536fail:
Christopher Faulet92d36382015-11-05 13:35:03 +0100537 st->comp_algo = NULL;
Christopher Faulet3d97c902015-12-09 14:59:38 +0100538 return 0;
539}
540
541/***********************************************************************/
542/* emit the chunksize followed by a CRLF on the output and return the number of
543 * bytes written. It goes backwards and starts with the byte before <end>. It
544 * returns the number of bytes written which will not exceed 10 (8 digits, CR,
545 * and LF). The caller is responsible for ensuring there is enough room left in
546 * the output buffer for the string.
547 */
548static int
549http_emit_chunk_size(char *end, unsigned int chksz)
550{
551 char *beg = end;
552
553 *--beg = '\n';
554 *--beg = '\r';
555 do {
556 *--beg = hextab[chksz & 0xF];
557 } while (chksz >>= 4);
558 return end - beg;
559}
560
561/*
562 * Init HTTP compression
563 */
Christopher Faulet92d36382015-11-05 13:35:03 +0100564static int
565http_compression_buffer_init(struct buffer *in, struct buffer *out)
Christopher Faulet3d97c902015-12-09 14:59:38 +0100566{
567 /* output stream requires at least 10 bytes for the gzip header, plus
568 * at least 8 bytes for the gzip trailer (crc+len), plus a possible
569 * plus at most 5 bytes per 32kB block and 2 bytes to close the stream.
570 */
571 if (in->size - buffer_len(in) < 20 + 5 * ((in->i + 32767) >> 15))
572 return -1;
573
574 /* prepare an empty output buffer in which we reserve enough room for
575 * copying the output bytes from <in>, plus 10 extra bytes to write
576 * the chunk size. We don't copy the bytes yet so that if we have to
577 * cancel the operation later, it's cheap.
578 */
579 b_reset(out);
580 out->o = in->o;
581 out->p += out->o;
582 out->i = 10;
583 return 0;
584}
585
586/*
587 * Add data to compress
588 */
Christopher Faulet92d36382015-11-05 13:35:03 +0100589static int
590http_compression_buffer_add_data(struct comp_state *st, struct buffer *in,
591 struct buffer *out, int sz)
Christopher Faulet3d97c902015-12-09 14:59:38 +0100592{
Christopher Faulet3d97c902015-12-09 14:59:38 +0100593 int consumed_data = 0;
594 int data_process_len;
595 int block1, block2;
596
Christopher Faulet92d36382015-11-05 13:35:03 +0100597 if (!sz)
Christopher Faulet3e7bc672015-12-07 13:39:08 +0100598 goto end;
Christopher Faulet3d97c902015-12-09 14:59:38 +0100599
Christopher Faulet92d36382015-11-05 13:35:03 +0100600 /* select the smallest size between the announced chunk size, the input
Christopher Faulet3d97c902015-12-09 14:59:38 +0100601 * data, and the available output buffer size. The compressors are
Christopher Faulet92d36382015-11-05 13:35:03 +0100602 * assumed to be able to process all the bytes we pass to them at
603 * once. */
Christopher Faulet3e7bc672015-12-07 13:39:08 +0100604 data_process_len = MIN(out->size - buffer_len(out), sz);
Christopher Faulet92d36382015-11-05 13:35:03 +0100605
Christopher Faulet3d97c902015-12-09 14:59:38 +0100606 block1 = data_process_len;
607 if (block1 > bi_contig_data(in))
608 block1 = bi_contig_data(in);
609 block2 = data_process_len - block1;
610
611 /* compressors return < 0 upon error or the amount of bytes read */
Christopher Faulet92d36382015-11-05 13:35:03 +0100612 consumed_data = st->comp_algo->add_data(st->comp_ctx, bi_ptr(in), block1, out);
Christopher Faulet3e7bc672015-12-07 13:39:08 +0100613 if (consumed_data != block1 || !block2)
614 goto end;
615 consumed_data = st->comp_algo->add_data(st->comp_ctx, in->data, block2, out);
616 if (consumed_data < 0)
617 goto end;
618 consumed_data += block1;
619
620 end:
Christopher Faulet3d97c902015-12-09 14:59:38 +0100621 return consumed_data;
622}
623
624/*
625 * Flush data in process, and write the header and footer of the chunk. Upon
626 * success, in and out buffers are swapped to avoid a copy.
627 */
Christopher Faulet92d36382015-11-05 13:35:03 +0100628static int
629http_compression_buffer_end(struct comp_state *st, struct stream *s,
630 struct buffer **in, struct buffer **out,
Christopher Faulet2fb28802015-12-01 10:40:57 +0100631 int end)
Christopher Faulet3d97c902015-12-09 14:59:38 +0100632{
Christopher Faulet3d97c902015-12-09 14:59:38 +0100633 struct buffer *ib = *in, *ob = *out;
634 char *tail;
Christopher Faulet92d36382015-11-05 13:35:03 +0100635 int to_forward, left;
Christopher Faulet3d97c902015-12-09 14:59:38 +0100636
637#if defined(USE_SLZ) || defined(USE_ZLIB)
638 int ret;
639
640 /* flush data here */
Christopher Faulet3d97c902015-12-09 14:59:38 +0100641 if (end)
Christopher Faulet92d36382015-11-05 13:35:03 +0100642 ret = st->comp_algo->finish(st->comp_ctx, ob); /* end of data */
Christopher Faulet3d97c902015-12-09 14:59:38 +0100643 else
Christopher Faulet92d36382015-11-05 13:35:03 +0100644 ret = st->comp_algo->flush(st->comp_ctx, ob); /* end of buffer */
Christopher Faulet3d97c902015-12-09 14:59:38 +0100645
646 if (ret < 0)
647 return -1; /* flush failed */
648
649#endif /* USE_ZLIB */
Christopher Faulet3d97c902015-12-09 14:59:38 +0100650 if (ob->i == 10) {
651 /* No data were appended, let's drop the output buffer and
652 * keep the input buffer unchanged.
653 */
654 return 0;
655 }
656
657 /* OK so at this stage, we have an output buffer <ob> looking like this :
658 *
659 * <-- o --> <------ i ----->
660 * +---------+---+------------+-----------+
661 * | out | c | comp_in | empty |
662 * +---------+---+------------+-----------+
663 * data p size
664 *
665 * <out> is the room reserved to copy ib->o. It starts at ob->data and
666 * has not yet been filled. <c> is the room reserved to write the chunk
667 * size (10 bytes). <comp_in> is the compressed equivalent of the data
668 * part of ib->i. <empty> is the amount of empty bytes at the end of
669 * the buffer, into which we may have to copy the remaining bytes from
670 * ib->i after the data (chunk size, trailers, ...).
671 */
672
673 /* Write real size at the begining of the chunk, no need of wrapping.
674 * We write the chunk using a dynamic length and adjust ob->p and ob->i
675 * accordingly afterwards. That will move <out> away from <data>.
676 */
677 left = 10 - http_emit_chunk_size(ob->p + 10, ob->i - 10);
678 ob->p += left;
679 ob->i -= left;
680
681 /* Copy previous data from ib->o into ob->o */
682 if (ib->o > 0) {
683 left = bo_contig_data(ib);
684 memcpy(ob->p - ob->o, bo_ptr(ib), left);
685 if (ib->o - left) /* second part of the buffer */
686 memcpy(ob->p - ob->o + left, ib->data, ib->o - left);
687 }
688
689 /* chunked encoding requires CRLF after data */
690 tail = ob->p + ob->i;
691 *tail++ = '\r';
692 *tail++ = '\n';
693
Christopher Faulet2fb28802015-12-01 10:40:57 +0100694 /* At the end of data, we must write the empty chunk 0<CRLF>,
695 * and terminate the trailers section with a last <CRLF>. If
696 * we're forwarding a chunked-encoded response, we'll have a
697 * trailers section after the empty chunk which needs to be
698 * forwarded and which will provide the last CRLF. Otherwise
699 * we write it ourselves.
700 */
701 if (end) {
702 struct http_msg *msg = &s->txn->rsp;
703
704 memcpy(tail, "0\r\n", 3);
705 tail += 3;
Christopher Fauletb77c5c22015-12-07 16:48:42 +0100706 if (!(msg->flags & HTTP_MSGF_TE_CHNK)) {
Christopher Faulet2fb28802015-12-01 10:40:57 +0100707 memcpy(tail, "\r\n", 2);
708 tail += 2;
709 }
710 }
711
Christopher Faulet3d97c902015-12-09 14:59:38 +0100712 ob->i = tail - ob->p;
Christopher Faulet3d97c902015-12-09 14:59:38 +0100713 to_forward = ob->i;
714
715 /* update input rate */
Christopher Faulet92d36382015-11-05 13:35:03 +0100716 if (st->comp_ctx && st->comp_ctx->cur_lvl > 0) {
Christopher Faulet2fb28802015-12-01 10:40:57 +0100717 update_freq_ctr(&global.comp_bps_in, st->consumed);
718 strm_fe(s)->fe_counters.comp_in += st->consumed;
719 s->be->be_counters.comp_in += st->consumed;
Christopher Faulet3d97c902015-12-09 14:59:38 +0100720 } else {
Christopher Faulet2fb28802015-12-01 10:40:57 +0100721 strm_fe(s)->fe_counters.comp_byp += st->consumed;
722 s->be->be_counters.comp_byp += st->consumed;
Christopher Faulet3d97c902015-12-09 14:59:38 +0100723 }
724
725 /* copy the remaining data in the tmp buffer. */
Christopher Faulet2fb28802015-12-01 10:40:57 +0100726 b_adv(ib, st->consumed);
Christopher Faulet3d97c902015-12-09 14:59:38 +0100727 if (ib->i > 0) {
728 left = bi_contig_data(ib);
729 memcpy(ob->p + ob->i, bi_ptr(ib), left);
730 ob->i += left;
731 if (ib->i - left) {
732 memcpy(ob->p + ob->i, ib->data, ib->i - left);
733 ob->i += ib->i - left;
734 }
735 }
736
737 /* swap the buffers */
738 *in = ob;
739 *out = ib;
740
Christopher Faulet92d36382015-11-05 13:35:03 +0100741
742 if (st->comp_ctx && st->comp_ctx->cur_lvl > 0) {
Christopher Faulet3d97c902015-12-09 14:59:38 +0100743 update_freq_ctr(&global.comp_bps_out, to_forward);
744 strm_fe(s)->fe_counters.comp_out += to_forward;
745 s->be->be_counters.comp_out += to_forward;
746 }
747
Christopher Faulet3d97c902015-12-09 14:59:38 +0100748 return to_forward;
749}
750
751
752/***********************************************************************/
Christopher Faulet92d36382015-11-05 13:35:03 +0100753struct flt_ops comp_ops = {
754 .init = comp_flt_init,
755 .deinit = comp_flt_deinit,
756
757 .channel_start_analyze = comp_start_analyze,
Christopher Faulet92d36382015-11-05 13:35:03 +0100758 .channel_end_analyze = comp_end_analyze,
759
Christopher Faulet1339d742016-05-11 16:48:33 +0200760 .http_headers = comp_http_headers,
Christopher Faulet309c6412015-12-02 09:57:32 +0100761 .http_data = comp_http_data,
762 .http_chunk_trailers = comp_http_chunk_trailers,
763 .http_forward_data = comp_http_forward_data,
Christopher Faulet92d36382015-11-05 13:35:03 +0100764};
765
Christopher Faulet3d97c902015-12-09 14:59:38 +0100766static int
767parse_compression_options(char **args, int section, struct proxy *proxy,
768 struct proxy *defpx, const char *file, int line,
769 char **err)
770{
Christopher Faulet92d36382015-11-05 13:35:03 +0100771 struct comp *comp;
Christopher Faulet3d97c902015-12-09 14:59:38 +0100772
773 if (proxy->comp == NULL) {
Vincent Bernat02779b62016-04-03 13:48:43 +0200774 comp = calloc(1, sizeof(*comp));
Christopher Faulet3d97c902015-12-09 14:59:38 +0100775 proxy->comp = comp;
776 }
777 else
778 comp = proxy->comp;
779
780 if (!strcmp(args[1], "algo")) {
781 struct comp_ctx *ctx;
782 int cur_arg = 2;
783
784 if (!*args[cur_arg]) {
785 memprintf(err, "parsing [%s:%d] : '%s' expects <algorithm>\n",
786 file, line, args[0]);
787 return -1;
788 }
789 while (*(args[cur_arg])) {
790 if (comp_append_algo(comp, args[cur_arg]) < 0) {
791 memprintf(err, "'%s' : '%s' is not a supported algorithm.\n",
792 args[0], args[cur_arg]);
793 return -1;
794 }
795 if (proxy->comp->algos->init(&ctx, 9) == 0)
796 proxy->comp->algos->end(&ctx);
797 else {
798 memprintf(err, "'%s' : Can't init '%s' algorithm.\n",
799 args[0], args[cur_arg]);
800 return -1;
801 }
802 cur_arg++;
803 continue;
804 }
805 }
806 else if (!strcmp(args[1], "offload"))
807 comp->offload = 1;
808 else if (!strcmp(args[1], "type")) {
809 int cur_arg = 2;
810
811 if (!*args[cur_arg]) {
812 memprintf(err, "'%s' expects <type>\n", args[0]);
813 return -1;
814 }
815 while (*(args[cur_arg])) {
816 comp_append_type(comp, args[cur_arg]);
817 cur_arg++;
818 continue;
819 }
820 }
821 else {
822 memprintf(err, "'%s' expects 'algo', 'type' or 'offload'\n",
823 args[0]);
824 return -1;
825 }
826
827 return 0;
828}
829
Christopher Faulet92d36382015-11-05 13:35:03 +0100830static int
831parse_http_comp_flt(char **args, int *cur_arg, struct proxy *px,
Thierry Fournier3610c392016-04-13 18:27:51 +0200832 struct flt_conf *fconf, char **err, void *private)
Christopher Faulet92d36382015-11-05 13:35:03 +0100833{
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100834 struct flt_conf *fc, *back;
Christopher Faulet92d36382015-11-05 13:35:03 +0100835
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100836 list_for_each_entry_safe(fc, back, &px->filter_configs, list) {
837 if (fc->id == http_comp_flt_id) {
Christopher Faulet92d36382015-11-05 13:35:03 +0100838 memprintf(err, "%s: Proxy supports only one compression filter\n", px->id);
839 return -1;
840 }
841 }
842
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100843 fconf->id = http_comp_flt_id;
844 fconf->conf = NULL;
845 fconf->ops = &comp_ops;
Christopher Faulet92d36382015-11-05 13:35:03 +0100846 (*cur_arg)++;
847
848 return 0;
849}
850
851
852int
853check_legacy_http_comp_flt(struct proxy *proxy)
854{
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100855 struct flt_conf *fconf;
Christopher Faulet92d36382015-11-05 13:35:03 +0100856 int err = 0;
857
858 if (proxy->comp == NULL)
859 goto end;
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100860 if (!LIST_ISEMPTY(&proxy->filter_configs)) {
861 list_for_each_entry(fconf, &proxy->filter_configs, list) {
862 if (fconf->id == http_comp_flt_id)
Christopher Faulet92d36382015-11-05 13:35:03 +0100863 goto end;
864 }
865 Alert("config: %s '%s': require an explicit filter declaration to use HTTP compression\n",
866 proxy_type_str(proxy), proxy->id);
867 err++;
868 goto end;
869 }
870
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100871 fconf = calloc(1, sizeof(*fconf));
872 if (!fconf) {
Christopher Faulet92d36382015-11-05 13:35:03 +0100873 Alert("config: %s '%s': out of memory\n",
874 proxy_type_str(proxy), proxy->id);
875 err++;
876 goto end;
877 }
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100878 fconf->id = http_comp_flt_id;
879 fconf->conf = NULL;
880 fconf->ops = &comp_ops;
881 LIST_ADDQ(&proxy->filter_configs, &fconf->list);
Christopher Faulet92d36382015-11-05 13:35:03 +0100882
883 end:
884 return err;
885}
886
887/*
888 * boolean, returns true if compression is used (either gzip or deflate) in the
889 * response.
890 */
Christopher Faulet3d97c902015-12-09 14:59:38 +0100891static int
Christopher Faulet92d36382015-11-05 13:35:03 +0100892smp_fetch_res_comp(const struct arg *args, struct sample *smp, const char *kw,
893 void *private)
Christopher Faulet3d97c902015-12-09 14:59:38 +0100894{
Willy Tarreaube508f12016-03-10 11:47:01 +0100895 struct http_txn *txn = smp->strm ? smp->strm->txn : NULL;
Christopher Faulet92d36382015-11-05 13:35:03 +0100896
Christopher Faulet3d97c902015-12-09 14:59:38 +0100897 smp->data.type = SMP_T_BOOL;
Christopher Faulet92d36382015-11-05 13:35:03 +0100898 smp->data.u.sint = (txn && (txn->rsp.flags & HTTP_MSGF_COMPRESSING));
Christopher Faulet3d97c902015-12-09 14:59:38 +0100899 return 1;
900}
901
Christopher Faulet92d36382015-11-05 13:35:03 +0100902/*
903 * string, returns algo
904 */
Christopher Faulet3d97c902015-12-09 14:59:38 +0100905static int
Christopher Faulet92d36382015-11-05 13:35:03 +0100906smp_fetch_res_comp_algo(const struct arg *args, struct sample *smp,
907 const char *kw, void *private)
Christopher Faulet3d97c902015-12-09 14:59:38 +0100908{
Willy Tarreaube508f12016-03-10 11:47:01 +0100909 struct http_txn *txn = smp->strm ? smp->strm->txn : NULL;
Christopher Faulet92d36382015-11-05 13:35:03 +0100910 struct filter *filter;
911 struct comp_state *st;
912
913 if (!(txn || !(txn->rsp.flags & HTTP_MSGF_COMPRESSING)))
Christopher Faulet3d97c902015-12-09 14:59:38 +0100914 return 0;
915
Christopher Fauletfcf035c2015-12-03 11:48:03 +0100916 list_for_each_entry(filter, &strm_flt(smp->strm)->filters, list) {
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100917 if (FLT_ID(filter) != http_comp_flt_id)
Christopher Faulet92d36382015-11-05 13:35:03 +0100918 continue;
919
920 if (!(st = filter->ctx))
921 break;
922
923 smp->data.type = SMP_T_STR;
924 smp->flags = SMP_F_CONST;
925 smp->data.u.str.str = st->comp_algo->cfg_name;
926 smp->data.u.str.len = st->comp_algo->cfg_name_len;
927 return 1;
928 }
929 return 0;
Christopher Faulet3d97c902015-12-09 14:59:38 +0100930}
931
932/* Declare the config parser for "compression" keyword */
933static struct cfg_kw_list cfg_kws = {ILH, {
934 { CFG_LISTEN, "compression", parse_compression_options },
935 { 0, NULL, NULL },
936 }
937};
938
Christopher Faulet92d36382015-11-05 13:35:03 +0100939/* Declare the filter parser for "compression" keyword */
940static struct flt_kw_list filter_kws = { "COMP", { }, {
Thierry Fournier3610c392016-04-13 18:27:51 +0200941 { "compression", parse_http_comp_flt, NULL },
942 { NULL, NULL, NULL },
Christopher Faulet92d36382015-11-05 13:35:03 +0100943 }
944};
945
Christopher Faulet3d97c902015-12-09 14:59:38 +0100946/* Note: must not be declared <const> as its list will be overwritten */
947static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
Christopher Faulet92d36382015-11-05 13:35:03 +0100948 { "res.comp", smp_fetch_res_comp, 0, NULL, SMP_T_BOOL, SMP_USE_HRSHP },
949 { "res.comp_algo", smp_fetch_res_comp_algo, 0, NULL, SMP_T_STR, SMP_USE_HRSHP },
950 { /* END */ },
951 }
952};
Christopher Faulet3d97c902015-12-09 14:59:38 +0100953
954__attribute__((constructor))
Christopher Faulet92d36382015-11-05 13:35:03 +0100955static void
956__flt_http_comp_init(void)
Christopher Faulet3d97c902015-12-09 14:59:38 +0100957{
958 cfg_register_keywords(&cfg_kws);
Christopher Faulet92d36382015-11-05 13:35:03 +0100959 flt_register_keywords(&filter_kws);
Christopher Faulet3d97c902015-12-09 14:59:38 +0100960 sample_register_fetches(&sample_fetch_keywords);
961}