blob: 5dacc90d7debeb9109b91060c110b3567b2740df [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;
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 Faulet2fb28802015-12-01 10:40:57 +010040 int sov;
41 int consumed;
42 int initialized;
Christopher Faulet92d36382015-11-05 13:35:03 +010043};
44
Christopher Faulet92d36382015-11-05 13:35:03 +010045static int select_compression_request_header(struct comp_state *st,
46 struct stream *s,
47 struct http_msg *msg);
48static int select_compression_response_header(struct comp_state *st,
49 struct stream *s,
50 struct http_msg *msg);
51
52static int http_compression_buffer_init(struct buffer *in, struct buffer *out);
53static int http_compression_buffer_add_data(struct comp_state *st,
54 struct buffer *in,
55 struct buffer *out, int sz);
56static int http_compression_buffer_end(struct comp_state *st, struct stream *s,
57 struct buffer **in, struct buffer **out,
Christopher Faulet2fb28802015-12-01 10:40:57 +010058 int end);
Christopher Faulet92d36382015-11-05 13:35:03 +010059
60/***********************************************************************/
61static int
62comp_flt_init(struct proxy *px, struct filter *filter)
63{
64
65 /* We need a compression buffer in the DATA state to put the output of
66 * compressed data, and in CRLF state to let the TRAILERS state finish
67 * the job of removing the trailing CRLF.
68 */
69 if (!tmpbuf->size) {
70 if (b_alloc(&tmpbuf) == NULL)
71 return -1;
72 }
73 return 0;
74}
75
76static void
77comp_flt_deinit(struct proxy *px, struct filter *filter)
78{
79 if (tmpbuf->size)
80 b_free(&tmpbuf);
81}
82
83static int
84comp_start_analyze(struct stream *s, struct filter *filter, struct channel *chn)
85{
86 if (filter->ctx == NULL) {
87 struct comp_state *st;
88
89 if (!(st = malloc(sizeof(*st))))
90 return -1;
91
Christopher Faulet2fb28802015-12-01 10:40:57 +010092 st->comp_algo = NULL;
93 st->comp_ctx = NULL;
94 st->sov = 0;
95 st->consumed = 0;
96 st->initialized = 0;
97 filter->ctx = st;
Christopher Faulet92d36382015-11-05 13:35:03 +010098 }
99 return 1;
100}
101
102static int
103comp_analyze(struct stream *s, struct filter *filter, struct channel *chn,
104 unsigned int an_bit)
105{
106 struct comp_state *st = filter->ctx;
107
108 if (!strm_fe(s)->comp && !s->be->comp)
109 goto end;
110
111 switch (an_bit) {
112 case AN_RES_HTTP_PROCESS_BE:
113 select_compression_response_header(st, s, &s->txn->rsp);
Christopher Faulet2fb28802015-12-01 10:40:57 +0100114 if (st->comp_algo)
115 st->sov = s->txn->rsp.sov;
Christopher Faulet92d36382015-11-05 13:35:03 +0100116 break;
117 }
118 end:
119 return 1;
120}
121
122static int
123comp_end_analyze(struct stream *s, struct filter *filter, struct channel *chn)
124{
125 struct comp_state *st = filter->ctx;
Christopher Faulet92d36382015-11-05 13:35:03 +0100126
127 if (!st || !(chn->flags & CF_ISRESP))
128 goto end;
129
Christopher Faulet92d36382015-11-05 13:35:03 +0100130 if (!st->comp_algo || !s->txn->status)
131 goto release_ctx;
132
133 if (strm_fe(s)->mode == PR_MODE_HTTP)
134 strm_fe(s)->fe_counters.p.http.comp_rsp++;
135 if ((s->flags & SF_BE_ASSIGNED) && (s->be->mode == PR_MODE_HTTP))
136 s->be->be_counters.p.http.comp_rsp++;
137
138 /* release any possible compression context */
139 st->comp_algo->end(&st->comp_ctx);
140
141 release_ctx:
142 free(st);
143 filter->ctx = NULL;
144 end:
145 return 1;
146}
147
148static int
149comp_http_headers(struct stream *s, struct filter *filter,
150 struct http_msg *msg)
151{
152 struct comp_state *st = filter->ctx;
153
154 if (strm_fe(s)->comp || s->be->comp) {
155 if (!(msg->chn->flags & CF_ISRESP))
156 select_compression_request_header(st, s, msg);
157 }
158 return 1;
159}
160
161static int
Christopher Faulet2fb28802015-12-01 10:40:57 +0100162comp_http_data(struct stream *s, struct filter *filter, struct http_msg *msg)
Christopher Faulet92d36382015-11-05 13:35:03 +0100163{
164 struct comp_state *st = filter->ctx;
Christopher Faulet2fb28802015-12-01 10:40:57 +0100165 unsigned int len;
Christopher Faulet92d36382015-11-05 13:35:03 +0100166 int ret;
167
168 if (!(msg->chn->flags & CF_ISRESP) || !st->comp_algo) {
169 flt_set_forward_data(filter, msg->chn);
170 return 1;
171 }
172
Christopher Faulet2fb28802015-12-01 10:40:57 +0100173 len = MIN(msg->chunk_len + msg->next, msg->chn->buf->i) - FLT_NXT(filter, msg->chn);
174 if (!len)
175 return len;
176
177 if (!st->initialized) {
178 b_adv(msg->chn->buf, FLT_FWD(filter, msg->chn) + st->sov);
179 ret = http_compression_buffer_init(msg->chn->buf, tmpbuf);
180 b_rew(msg->chn->buf, FLT_FWD(filter, msg->chn) + st->sov);
181 if (ret < 0) {
182 msg->chn->flags |= CF_WAKE_WRITE;
183 return 0;
184 }
185 }
186 b_adv(msg->chn->buf, FLT_NXT(filter, msg->chn));
187 ret = http_compression_buffer_add_data(st, msg->chn->buf, tmpbuf, len);
188 b_rew(msg->chn->buf, FLT_NXT(filter, msg->chn));
189 if (ret < 0)
190 return ret;
Christopher Faulet92d36382015-11-05 13:35:03 +0100191
Christopher Faulet2fb28802015-12-01 10:40:57 +0100192 st->initialized = 1;
193 msg->next += ret;
194 msg->chunk_len -= ret;
195 FLT_NXT(filter, msg->chn) = msg->next;
196 return 0;
Christopher Faulet92d36382015-11-05 13:35:03 +0100197}
198
199static int
Christopher Faulet2fb28802015-12-01 10:40:57 +0100200comp_http_chunk_trailers(struct stream *s, struct filter *filter,
201 struct http_msg *msg)
Christopher Faulet92d36382015-11-05 13:35:03 +0100202{
203 struct comp_state *st = filter->ctx;
Christopher Faulet2fb28802015-12-01 10:40:57 +0100204 int ret;
Christopher Faulet92d36382015-11-05 13:35:03 +0100205
Christopher Faulet92d36382015-11-05 13:35:03 +0100206 if (!(msg->chn->flags & CF_ISRESP) || !st->comp_algo) {
207 flt_set_forward_data(filter, msg->chn);
Christopher Faulet2fb28802015-12-01 10:40:57 +0100208 return 1;
Christopher Faulet92d36382015-11-05 13:35:03 +0100209 }
Christopher Faulet92d36382015-11-05 13:35:03 +0100210
Christopher Faulet2fb28802015-12-01 10:40:57 +0100211 if (!st->initialized)
212 return 1;
Christopher Faulet92d36382015-11-05 13:35:03 +0100213
Christopher Faulet2fb28802015-12-01 10:40:57 +0100214 st->consumed = msg->next - st->sov;
215 b_adv(msg->chn->buf, FLT_FWD(filter, msg->chn) + st->sov);
216 ret = http_compression_buffer_end(st, s, &msg->chn->buf, &tmpbuf, 1);
217 if (ret < 0)
218 return ret;
219
220 st->initialized = 0;
221 st->sov = 0;
222 msg->next = ret;
223 FLT_NXT(filter, msg->chn) = ret;
224 FLT_FWD(filter, msg->chn) = 0;
225 return 1;
Christopher Faulet92d36382015-11-05 13:35:03 +0100226}
227
Christopher Faulet2fb28802015-12-01 10:40:57 +0100228
Christopher Faulet92d36382015-11-05 13:35:03 +0100229static int
230comp_http_forward_data(struct stream *s, struct filter *filter,
231 struct http_msg *msg, unsigned int len)
232{
233 struct comp_state *st = filter->ctx;
Christopher Faulet2fb28802015-12-01 10:40:57 +0100234 int ret;
Christopher Faulet92d36382015-11-05 13:35:03 +0100235
236 if (!(msg->chn->flags & CF_ISRESP) || !st->comp_algo) {
237 flt_set_forward_data(filter, msg->chn);
Christopher Faulet2fb28802015-12-01 10:40:57 +0100238 ret = len;
239 return ret;
Christopher Faulet92d36382015-11-05 13:35:03 +0100240 }
241
Christopher Faulet2fb28802015-12-01 10:40:57 +0100242 /* To work, previous filters MUST forward all data */
243 if (FLT_FWD(filter, msg->chn) + len != FLT_NXT(filter, msg->chn)) {
244 Warning("HTTP compression failed: unexpected behavior of previous filters\n");
245 return -1;
Christopher Faulet92d36382015-11-05 13:35:03 +0100246 }
247
Christopher Faulet2fb28802015-12-01 10:40:57 +0100248 if (!st->initialized) {
249 ret = len;
250 st->sov = ((st->sov > ret) ? (st->sov-ret) : 0);
251 return ret;
Christopher Faulet92d36382015-11-05 13:35:03 +0100252 }
253
Christopher Faulet2fb28802015-12-01 10:40:57 +0100254 st->consumed = len - st->sov;
255 b_adv(msg->chn->buf, FLT_FWD(filter, msg->chn) + st->sov);
256 ret = http_compression_buffer_end(st, s, &msg->chn->buf, &tmpbuf,
257 msg->msg_state == HTTP_MSG_ENDING);
258 if (ret < 0)
259 return ret;
Christopher Faulet92d36382015-11-05 13:35:03 +0100260
Christopher Faulet2fb28802015-12-01 10:40:57 +0100261 st->initialized = 0;
262 st->sov = 0;
263 msg->next = ret;
264 FLT_NXT(filter, msg->chn) = ret;
265 FLT_FWD(filter, msg->chn) = 0;
Christopher Faulet92d36382015-11-05 13:35:03 +0100266 return ret;
267}
Christopher Faulet3d97c902015-12-09 14:59:38 +0100268
269/***********************************************************************/
270/*
271 * Selects a compression algorithm depending on the client request.
272 */
273int
Christopher Faulet92d36382015-11-05 13:35:03 +0100274select_compression_request_header(struct comp_state *st, struct stream *s,
275 struct http_msg *msg)
Christopher Faulet3d97c902015-12-09 14:59:38 +0100276{
277 struct http_txn *txn = s->txn;
Christopher Faulet92d36382015-11-05 13:35:03 +0100278 struct buffer *req = msg->chn->buf;
Christopher Faulet3d97c902015-12-09 14:59:38 +0100279 struct hdr_ctx ctx;
280 struct comp_algo *comp_algo = NULL;
281 struct comp_algo *comp_algo_back = NULL;
282
283 /* Disable compression for older user agents announcing themselves as "Mozilla/4"
284 * unless they are known good (MSIE 6 with XP SP2, or MSIE 7 and later).
285 * See http://zoompf.com/2012/02/lose-the-wait-http-compression for more details.
286 */
287 ctx.idx = 0;
288 if (http_find_header2("User-Agent", 10, req->p, &txn->hdr_idx, &ctx) &&
289 ctx.vlen >= 9 &&
290 memcmp(ctx.line + ctx.val, "Mozilla/4", 9) == 0 &&
291 (ctx.vlen < 31 ||
292 memcmp(ctx.line + ctx.val + 25, "MSIE ", 5) != 0 ||
293 ctx.line[ctx.val + 30] < '6' ||
294 (ctx.line[ctx.val + 30] == '6' &&
295 (ctx.vlen < 54 || memcmp(ctx.line + 51, "SV1", 3) != 0)))) {
Christopher Faulet92d36382015-11-05 13:35:03 +0100296 st->comp_algo = NULL;
Christopher Faulet3d97c902015-12-09 14:59:38 +0100297 return 0;
298 }
299
300 /* search for the algo in the backend in priority or the frontend */
Christopher Faulet92d36382015-11-05 13:35:03 +0100301 if ((s->be->comp && (comp_algo_back = s->be->comp->algos)) ||
302 (strm_fe(s)->comp && (comp_algo_back = strm_fe(s)->comp->algos))) {
Christopher Faulet3d97c902015-12-09 14:59:38 +0100303 int best_q = 0;
304
305 ctx.idx = 0;
306 while (http_find_header2("Accept-Encoding", 15, req->p, &txn->hdr_idx, &ctx)) {
307 const char *qval;
308 int q;
309 int toklen;
310
311 /* try to isolate the token from the optional q-value */
312 toklen = 0;
313 while (toklen < ctx.vlen && http_is_token[(unsigned char)*(ctx.line + ctx.val + toklen)])
314 toklen++;
315
316 qval = ctx.line + ctx.val + toklen;
317 while (1) {
318 while (qval < ctx.line + ctx.val + ctx.vlen && http_is_lws[(unsigned char)*qval])
319 qval++;
320
321 if (qval >= ctx.line + ctx.val + ctx.vlen || *qval != ';') {
322 qval = NULL;
323 break;
324 }
325 qval++;
326
327 while (qval < ctx.line + ctx.val + ctx.vlen && http_is_lws[(unsigned char)*qval])
328 qval++;
329
330 if (qval >= ctx.line + ctx.val + ctx.vlen) {
331 qval = NULL;
332 break;
333 }
334 if (strncmp(qval, "q=", MIN(ctx.line + ctx.val + ctx.vlen - qval, 2)) == 0)
335 break;
336
337 while (qval < ctx.line + ctx.val + ctx.vlen && *qval != ';')
338 qval++;
339 }
340
341 /* here we have qval pointing to the first "q=" attribute or NULL if not found */
342 q = qval ? parse_qvalue(qval + 2, NULL) : 1000;
343
344 if (q <= best_q)
345 continue;
346
347 for (comp_algo = comp_algo_back; comp_algo; comp_algo = comp_algo->next) {
348 if (*(ctx.line + ctx.val) == '*' ||
349 word_match(ctx.line + ctx.val, toklen, comp_algo->ua_name, comp_algo->ua_name_len)) {
Christopher Faulet92d36382015-11-05 13:35:03 +0100350 st->comp_algo = comp_algo;
Christopher Faulet3d97c902015-12-09 14:59:38 +0100351 best_q = q;
352 break;
353 }
354 }
355 }
356 }
357
358 /* remove all occurrences of the header when "compression offload" is set */
Christopher Faulet92d36382015-11-05 13:35:03 +0100359 if (st->comp_algo) {
360 if ((s->be->comp && s->be->comp->offload) ||
361 (strm_fe(s)->comp && strm_fe(s)->comp->offload)) {
Christopher Faulet3d97c902015-12-09 14:59:38 +0100362 http_remove_header2(msg, &txn->hdr_idx, &ctx);
363 ctx.idx = 0;
364 while (http_find_header2("Accept-Encoding", 15, req->p, &txn->hdr_idx, &ctx)) {
365 http_remove_header2(msg, &txn->hdr_idx, &ctx);
366 }
367 }
368 return 1;
369 }
370
371 /* identity is implicit does not require headers */
Christopher Faulet92d36382015-11-05 13:35:03 +0100372 if ((s->be->comp && (comp_algo_back = s->be->comp->algos)) ||
373 (strm_fe(s)->comp && (comp_algo_back = strm_fe(s)->comp->algos))) {
Christopher Faulet3d97c902015-12-09 14:59:38 +0100374 for (comp_algo = comp_algo_back; comp_algo; comp_algo = comp_algo->next) {
375 if (comp_algo->cfg_name_len == 8 && memcmp(comp_algo->cfg_name, "identity", 8) == 0) {
Christopher Faulet92d36382015-11-05 13:35:03 +0100376 st->comp_algo = comp_algo;
Christopher Faulet3d97c902015-12-09 14:59:38 +0100377 return 1;
378 }
379 }
380 }
381
Christopher Faulet92d36382015-11-05 13:35:03 +0100382 st->comp_algo = NULL;
Christopher Faulet3d97c902015-12-09 14:59:38 +0100383 return 0;
384}
385
Christopher Faulet92d36382015-11-05 13:35:03 +0100386
Christopher Faulet3d97c902015-12-09 14:59:38 +0100387/*
388 * Selects a comression algorithm depending of the server response.
389 */
Christopher Faulet92d36382015-11-05 13:35:03 +0100390static int
391select_compression_response_header(struct comp_state *st, struct stream *s, struct http_msg *msg)
Christopher Faulet3d97c902015-12-09 14:59:38 +0100392{
393 struct http_txn *txn = s->txn;
Christopher Faulet92d36382015-11-05 13:35:03 +0100394 struct buffer *res = msg->chn->buf;
Christopher Faulet3d97c902015-12-09 14:59:38 +0100395 struct hdr_ctx ctx;
396 struct comp_type *comp_type;
397
398 /* no common compression algorithm was found in request header */
Christopher Faulet92d36382015-11-05 13:35:03 +0100399 if (st->comp_algo == NULL)
Christopher Faulet3d97c902015-12-09 14:59:38 +0100400 goto fail;
401
402 /* HTTP < 1.1 should not be compressed */
403 if (!(msg->flags & HTTP_MSGF_VER_11) || !(txn->req.flags & HTTP_MSGF_VER_11))
404 goto fail;
405
Christopher Faulet92d36382015-11-05 13:35:03 +0100406 if (txn->meth == HTTP_METH_HEAD)
407 goto fail;
408
Christopher Faulet3d97c902015-12-09 14:59:38 +0100409 /* compress 200,201,202,203 responses only */
410 if ((txn->status != 200) &&
411 (txn->status != 201) &&
412 (txn->status != 202) &&
413 (txn->status != 203))
414 goto fail;
415
416
417 /* Content-Length is null */
418 if (!(msg->flags & HTTP_MSGF_TE_CHNK) && msg->body_len == 0)
419 goto fail;
420
421 /* content is already compressed */
422 ctx.idx = 0;
423 if (http_find_header2("Content-Encoding", 16, res->p, &txn->hdr_idx, &ctx))
424 goto fail;
425
426 /* no compression when Cache-Control: no-transform is present in the message */
427 ctx.idx = 0;
428 while (http_find_header2("Cache-Control", 13, res->p, &txn->hdr_idx, &ctx)) {
429 if (word_match(ctx.line + ctx.val, ctx.vlen, "no-transform", 12))
430 goto fail;
431 }
432
433 comp_type = NULL;
434
435 /* we don't want to compress multipart content-types, nor content-types that are
436 * not listed in the "compression type" directive if any. If no content-type was
437 * found but configuration requires one, we don't compress either. Backend has
438 * the priority.
439 */
440 ctx.idx = 0;
441 if (http_find_header2("Content-Type", 12, res->p, &txn->hdr_idx, &ctx)) {
442 if (ctx.vlen >= 9 && strncasecmp("multipart", ctx.line+ctx.val, 9) == 0)
443 goto fail;
444
445 if ((s->be->comp && (comp_type = s->be->comp->types)) ||
446 (strm_fe(s)->comp && (comp_type = strm_fe(s)->comp->types))) {
447 for (; comp_type; comp_type = comp_type->next) {
448 if (ctx.vlen >= comp_type->name_len &&
449 strncasecmp(ctx.line+ctx.val, comp_type->name, comp_type->name_len) == 0)
450 /* this Content-Type should be compressed */
451 break;
452 }
453 /* this Content-Type should not be compressed */
454 if (comp_type == NULL)
455 goto fail;
456 }
457 }
458 else { /* no content-type header */
Christopher Faulet92d36382015-11-05 13:35:03 +0100459 if ((s->be->comp && s->be->comp->types) ||
460 (strm_fe(s)->comp && strm_fe(s)->comp->types))
Christopher Faulet3d97c902015-12-09 14:59:38 +0100461 goto fail; /* a content-type was required */
462 }
463
464 /* limit compression rate */
465 if (global.comp_rate_lim > 0)
466 if (read_freq_ctr(&global.comp_bps_in) > global.comp_rate_lim)
467 goto fail;
468
469 /* limit cpu usage */
470 if (idle_pct < compress_min_idle)
471 goto fail;
472
473 /* initialize compression */
Christopher Faulet92d36382015-11-05 13:35:03 +0100474 if (st->comp_algo->init(&st->comp_ctx, global.tune.comp_maxlevel) < 0)
Christopher Faulet3d97c902015-12-09 14:59:38 +0100475 goto fail;
476
Christopher Faulet3d97c902015-12-09 14:59:38 +0100477 /* remove Content-Length header */
478 ctx.idx = 0;
479 if ((msg->flags & HTTP_MSGF_CNT_LEN) && http_find_header2("Content-Length", 14, res->p, &txn->hdr_idx, &ctx))
480 http_remove_header2(msg, &txn->hdr_idx, &ctx);
481
482 /* add Transfer-Encoding header */
483 if (!(msg->flags & HTTP_MSGF_TE_CHNK))
484 http_header_add_tail2(&txn->rsp, &txn->hdr_idx, "Transfer-Encoding: chunked", 26);
485
486 /*
487 * Add Content-Encoding header when it's not identity encoding.
488 * RFC 2616 : Identity encoding: This content-coding is used only in the
489 * Accept-Encoding header, and SHOULD NOT be used in the Content-Encoding
490 * header.
491 */
Christopher Faulet92d36382015-11-05 13:35:03 +0100492 if (st->comp_algo->cfg_name_len != 8 || memcmp(st->comp_algo->cfg_name, "identity", 8) != 0) {
Christopher Faulet3d97c902015-12-09 14:59:38 +0100493 trash.len = 18;
494 memcpy(trash.str, "Content-Encoding: ", trash.len);
Christopher Faulet92d36382015-11-05 13:35:03 +0100495 memcpy(trash.str + trash.len, st->comp_algo->ua_name, st->comp_algo->ua_name_len);
496 trash.len += st->comp_algo->ua_name_len;
Christopher Faulet3d97c902015-12-09 14:59:38 +0100497 trash.str[trash.len] = '\0';
498 http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len);
499 }
Christopher Faulet92d36382015-11-05 13:35:03 +0100500 msg->flags |= HTTP_MSGF_COMPRESSING;
Christopher Faulet3d97c902015-12-09 14:59:38 +0100501 return 1;
502
503fail:
Christopher Faulet92d36382015-11-05 13:35:03 +0100504 st->comp_algo = NULL;
Christopher Faulet3d97c902015-12-09 14:59:38 +0100505 return 0;
506}
507
508/***********************************************************************/
509/* emit the chunksize followed by a CRLF on the output and return the number of
510 * bytes written. It goes backwards and starts with the byte before <end>. It
511 * returns the number of bytes written which will not exceed 10 (8 digits, CR,
512 * and LF). The caller is responsible for ensuring there is enough room left in
513 * the output buffer for the string.
514 */
515static int
516http_emit_chunk_size(char *end, unsigned int chksz)
517{
518 char *beg = end;
519
520 *--beg = '\n';
521 *--beg = '\r';
522 do {
523 *--beg = hextab[chksz & 0xF];
524 } while (chksz >>= 4);
525 return end - beg;
526}
527
528/*
529 * Init HTTP compression
530 */
Christopher Faulet92d36382015-11-05 13:35:03 +0100531static int
532http_compression_buffer_init(struct buffer *in, struct buffer *out)
Christopher Faulet3d97c902015-12-09 14:59:38 +0100533{
534 /* output stream requires at least 10 bytes for the gzip header, plus
535 * at least 8 bytes for the gzip trailer (crc+len), plus a possible
536 * plus at most 5 bytes per 32kB block and 2 bytes to close the stream.
537 */
538 if (in->size - buffer_len(in) < 20 + 5 * ((in->i + 32767) >> 15))
539 return -1;
540
541 /* prepare an empty output buffer in which we reserve enough room for
542 * copying the output bytes from <in>, plus 10 extra bytes to write
543 * the chunk size. We don't copy the bytes yet so that if we have to
544 * cancel the operation later, it's cheap.
545 */
546 b_reset(out);
547 out->o = in->o;
548 out->p += out->o;
549 out->i = 10;
550 return 0;
551}
552
553/*
554 * Add data to compress
555 */
Christopher Faulet92d36382015-11-05 13:35:03 +0100556static int
557http_compression_buffer_add_data(struct comp_state *st, struct buffer *in,
558 struct buffer *out, int sz)
Christopher Faulet3d97c902015-12-09 14:59:38 +0100559{
Christopher Faulet3d97c902015-12-09 14:59:38 +0100560 int consumed_data = 0;
561 int data_process_len;
562 int block1, block2;
563
Christopher Faulet92d36382015-11-05 13:35:03 +0100564 if (!sz)
565 return 0;
Christopher Faulet3d97c902015-12-09 14:59:38 +0100566
Christopher Faulet92d36382015-11-05 13:35:03 +0100567 /* select the smallest size between the announced chunk size, the input
Christopher Faulet3d97c902015-12-09 14:59:38 +0100568 * data, and the available output buffer size. The compressors are
Christopher Faulet92d36382015-11-05 13:35:03 +0100569 * assumed to be able to process all the bytes we pass to them at
570 * once. */
571 data_process_len = sz;
Christopher Faulet3d97c902015-12-09 14:59:38 +0100572 data_process_len = MIN(out->size - buffer_len(out), data_process_len);
573
Christopher Faulet92d36382015-11-05 13:35:03 +0100574
Christopher Faulet3d97c902015-12-09 14:59:38 +0100575 block1 = data_process_len;
576 if (block1 > bi_contig_data(in))
577 block1 = bi_contig_data(in);
578 block2 = data_process_len - block1;
579
580 /* compressors return < 0 upon error or the amount of bytes read */
Christopher Faulet92d36382015-11-05 13:35:03 +0100581 consumed_data = st->comp_algo->add_data(st->comp_ctx, bi_ptr(in), block1, out);
Christopher Faulet3d97c902015-12-09 14:59:38 +0100582 if (consumed_data >= 0 && block2 > 0) {
Christopher Faulet92d36382015-11-05 13:35:03 +0100583 consumed_data = st->comp_algo->add_data(st->comp_ctx, in->data, block2, out);
Christopher Faulet3d97c902015-12-09 14:59:38 +0100584 if (consumed_data >= 0)
585 consumed_data += block1;
586 }
Christopher Faulet3d97c902015-12-09 14:59:38 +0100587 return consumed_data;
588}
589
590/*
591 * Flush data in process, and write the header and footer of the chunk. Upon
592 * success, in and out buffers are swapped to avoid a copy.
593 */
Christopher Faulet92d36382015-11-05 13:35:03 +0100594static int
595http_compression_buffer_end(struct comp_state *st, struct stream *s,
596 struct buffer **in, struct buffer **out,
Christopher Faulet2fb28802015-12-01 10:40:57 +0100597 int end)
Christopher Faulet3d97c902015-12-09 14:59:38 +0100598{
Christopher Faulet3d97c902015-12-09 14:59:38 +0100599 struct buffer *ib = *in, *ob = *out;
600 char *tail;
Christopher Faulet92d36382015-11-05 13:35:03 +0100601 int to_forward, left;
Christopher Faulet3d97c902015-12-09 14:59:38 +0100602
603#if defined(USE_SLZ) || defined(USE_ZLIB)
604 int ret;
605
606 /* flush data here */
Christopher Faulet3d97c902015-12-09 14:59:38 +0100607 if (end)
Christopher Faulet92d36382015-11-05 13:35:03 +0100608 ret = st->comp_algo->finish(st->comp_ctx, ob); /* end of data */
Christopher Faulet3d97c902015-12-09 14:59:38 +0100609 else
Christopher Faulet92d36382015-11-05 13:35:03 +0100610 ret = st->comp_algo->flush(st->comp_ctx, ob); /* end of buffer */
Christopher Faulet3d97c902015-12-09 14:59:38 +0100611
612 if (ret < 0)
613 return -1; /* flush failed */
614
615#endif /* USE_ZLIB */
616
617 if (ob->i == 10) {
618 /* No data were appended, let's drop the output buffer and
619 * keep the input buffer unchanged.
620 */
621 return 0;
622 }
623
624 /* OK so at this stage, we have an output buffer <ob> looking like this :
625 *
626 * <-- o --> <------ i ----->
627 * +---------+---+------------+-----------+
628 * | out | c | comp_in | empty |
629 * +---------+---+------------+-----------+
630 * data p size
631 *
632 * <out> is the room reserved to copy ib->o. It starts at ob->data and
633 * has not yet been filled. <c> is the room reserved to write the chunk
634 * size (10 bytes). <comp_in> is the compressed equivalent of the data
635 * part of ib->i. <empty> is the amount of empty bytes at the end of
636 * the buffer, into which we may have to copy the remaining bytes from
637 * ib->i after the data (chunk size, trailers, ...).
638 */
639
640 /* Write real size at the begining of the chunk, no need of wrapping.
641 * We write the chunk using a dynamic length and adjust ob->p and ob->i
642 * accordingly afterwards. That will move <out> away from <data>.
643 */
644 left = 10 - http_emit_chunk_size(ob->p + 10, ob->i - 10);
645 ob->p += left;
646 ob->i -= left;
647
648 /* Copy previous data from ib->o into ob->o */
649 if (ib->o > 0) {
650 left = bo_contig_data(ib);
651 memcpy(ob->p - ob->o, bo_ptr(ib), left);
652 if (ib->o - left) /* second part of the buffer */
653 memcpy(ob->p - ob->o + left, ib->data, ib->o - left);
654 }
655
656 /* chunked encoding requires CRLF after data */
657 tail = ob->p + ob->i;
658 *tail++ = '\r';
659 *tail++ = '\n';
660
Christopher Faulet2fb28802015-12-01 10:40:57 +0100661 /* At the end of data, we must write the empty chunk 0<CRLF>,
662 * and terminate the trailers section with a last <CRLF>. If
663 * we're forwarding a chunked-encoded response, we'll have a
664 * trailers section after the empty chunk which needs to be
665 * forwarded and which will provide the last CRLF. Otherwise
666 * we write it ourselves.
667 */
668 if (end) {
669 struct http_msg *msg = &s->txn->rsp;
670
671 memcpy(tail, "0\r\n", 3);
672 tail += 3;
673 if (msg->msg_state == HTTP_MSG_ENDING) {
674 memcpy(tail, "\r\n", 2);
675 tail += 2;
676 }
677 }
678
Christopher Faulet3d97c902015-12-09 14:59:38 +0100679 ob->i = tail - ob->p;
Christopher Faulet3d97c902015-12-09 14:59:38 +0100680 to_forward = ob->i;
681
682 /* update input rate */
Christopher Faulet92d36382015-11-05 13:35:03 +0100683 if (st->comp_ctx && st->comp_ctx->cur_lvl > 0) {
Christopher Faulet2fb28802015-12-01 10:40:57 +0100684 update_freq_ctr(&global.comp_bps_in, st->consumed);
685 strm_fe(s)->fe_counters.comp_in += st->consumed;
686 s->be->be_counters.comp_in += st->consumed;
Christopher Faulet3d97c902015-12-09 14:59:38 +0100687 } else {
Christopher Faulet2fb28802015-12-01 10:40:57 +0100688 strm_fe(s)->fe_counters.comp_byp += st->consumed;
689 s->be->be_counters.comp_byp += st->consumed;
Christopher Faulet3d97c902015-12-09 14:59:38 +0100690 }
691
692 /* copy the remaining data in the tmp buffer. */
Christopher Faulet2fb28802015-12-01 10:40:57 +0100693 b_adv(ib, st->consumed);
Christopher Faulet3d97c902015-12-09 14:59:38 +0100694 if (ib->i > 0) {
695 left = bi_contig_data(ib);
696 memcpy(ob->p + ob->i, bi_ptr(ib), left);
697 ob->i += left;
698 if (ib->i - left) {
699 memcpy(ob->p + ob->i, ib->data, ib->i - left);
700 ob->i += ib->i - left;
701 }
702 }
703
704 /* swap the buffers */
705 *in = ob;
706 *out = ib;
707
Christopher Faulet92d36382015-11-05 13:35:03 +0100708
709 if (st->comp_ctx && st->comp_ctx->cur_lvl > 0) {
Christopher Faulet3d97c902015-12-09 14:59:38 +0100710 update_freq_ctr(&global.comp_bps_out, to_forward);
711 strm_fe(s)->fe_counters.comp_out += to_forward;
712 s->be->be_counters.comp_out += to_forward;
713 }
714
Christopher Faulet3d97c902015-12-09 14:59:38 +0100715 return to_forward;
716}
717
718
719/***********************************************************************/
Christopher Faulet92d36382015-11-05 13:35:03 +0100720struct flt_ops comp_ops = {
721 .init = comp_flt_init,
722 .deinit = comp_flt_deinit,
723
724 .channel_start_analyze = comp_start_analyze,
725 .channel_analyze = comp_analyze,
726 .channel_end_analyze = comp_end_analyze,
727
728 .http_headers = comp_http_headers,
Christopher Faulet92d36382015-11-05 13:35:03 +0100729 .http_data = comp_http_data,
Christopher Faulet2fb28802015-12-01 10:40:57 +0100730 .http_chunk_trailers = comp_http_chunk_trailers,
Christopher Faulet92d36382015-11-05 13:35:03 +0100731 .http_forward_data = comp_http_forward_data,
732};
733
Christopher Faulet3d97c902015-12-09 14:59:38 +0100734static int
735parse_compression_options(char **args, int section, struct proxy *proxy,
736 struct proxy *defpx, const char *file, int line,
737 char **err)
738{
Christopher Faulet92d36382015-11-05 13:35:03 +0100739 struct comp *comp;
Christopher Faulet3d97c902015-12-09 14:59:38 +0100740
741 if (proxy->comp == NULL) {
742 comp = calloc(1, sizeof(struct comp));
743 proxy->comp = comp;
744 }
745 else
746 comp = proxy->comp;
747
748 if (!strcmp(args[1], "algo")) {
749 struct comp_ctx *ctx;
750 int cur_arg = 2;
751
752 if (!*args[cur_arg]) {
753 memprintf(err, "parsing [%s:%d] : '%s' expects <algorithm>\n",
754 file, line, args[0]);
755 return -1;
756 }
757 while (*(args[cur_arg])) {
758 if (comp_append_algo(comp, args[cur_arg]) < 0) {
759 memprintf(err, "'%s' : '%s' is not a supported algorithm.\n",
760 args[0], args[cur_arg]);
761 return -1;
762 }
763 if (proxy->comp->algos->init(&ctx, 9) == 0)
764 proxy->comp->algos->end(&ctx);
765 else {
766 memprintf(err, "'%s' : Can't init '%s' algorithm.\n",
767 args[0], args[cur_arg]);
768 return -1;
769 }
770 cur_arg++;
771 continue;
772 }
773 }
774 else if (!strcmp(args[1], "offload"))
775 comp->offload = 1;
776 else if (!strcmp(args[1], "type")) {
777 int cur_arg = 2;
778
779 if (!*args[cur_arg]) {
780 memprintf(err, "'%s' expects <type>\n", args[0]);
781 return -1;
782 }
783 while (*(args[cur_arg])) {
784 comp_append_type(comp, args[cur_arg]);
785 cur_arg++;
786 continue;
787 }
788 }
789 else {
790 memprintf(err, "'%s' expects 'algo', 'type' or 'offload'\n",
791 args[0]);
792 return -1;
793 }
794
795 return 0;
796}
797
Christopher Faulet92d36382015-11-05 13:35:03 +0100798static int
799parse_http_comp_flt(char **args, int *cur_arg, struct proxy *px,
800 struct filter *filter, char **err)
801{
802 struct filter *flt, *back;
803
804 list_for_each_entry_safe(flt, back, &px->filters, list) {
805 if (flt->id == http_comp_flt_id) {
806 memprintf(err, "%s: Proxy supports only one compression filter\n", px->id);
807 return -1;
808 }
809 }
810
811 filter->id = http_comp_flt_id;
812 filter->conf = NULL;
813 filter->ops = &comp_ops;
814 (*cur_arg)++;
815
816 return 0;
817}
818
819
820int
821check_legacy_http_comp_flt(struct proxy *proxy)
822{
823 struct filter *filter;
824 int err = 0;
825
826 if (proxy->comp == NULL)
827 goto end;
828 if (!LIST_ISEMPTY(&proxy->filters)) {
829 list_for_each_entry(filter, &proxy->filters, list) {
830 if (filter->id == http_comp_flt_id)
831 goto end;
832 }
833 Alert("config: %s '%s': require an explicit filter declaration to use HTTP compression\n",
834 proxy_type_str(proxy), proxy->id);
835 err++;
836 goto end;
837 }
838
839 filter = pool_alloc2(pool2_filter);
840 if (!filter) {
841 Alert("config: %s '%s': out of memory\n",
842 proxy_type_str(proxy), proxy->id);
843 err++;
844 goto end;
845 }
846 memset(filter, 0, sizeof(*filter));
847 filter->id = http_comp_flt_id;
848 filter->conf = NULL;
849 filter->ops = &comp_ops;
850 LIST_ADDQ(&proxy->filters, &filter->list);
851
852 end:
853 return err;
854}
855
856/*
857 * boolean, returns true if compression is used (either gzip or deflate) in the
858 * response.
859 */
Christopher Faulet3d97c902015-12-09 14:59:38 +0100860static int
Christopher Faulet92d36382015-11-05 13:35:03 +0100861smp_fetch_res_comp(const struct arg *args, struct sample *smp, const char *kw,
862 void *private)
Christopher Faulet3d97c902015-12-09 14:59:38 +0100863{
Christopher Faulet92d36382015-11-05 13:35:03 +0100864 struct http_txn *txn = smp->strm->txn;
865
Christopher Faulet3d97c902015-12-09 14:59:38 +0100866 smp->data.type = SMP_T_BOOL;
Christopher Faulet92d36382015-11-05 13:35:03 +0100867 smp->data.u.sint = (txn && (txn->rsp.flags & HTTP_MSGF_COMPRESSING));
Christopher Faulet3d97c902015-12-09 14:59:38 +0100868 return 1;
869}
870
Christopher Faulet92d36382015-11-05 13:35:03 +0100871/*
872 * string, returns algo
873 */
Christopher Faulet3d97c902015-12-09 14:59:38 +0100874static int
Christopher Faulet92d36382015-11-05 13:35:03 +0100875smp_fetch_res_comp_algo(const struct arg *args, struct sample *smp,
876 const char *kw, void *private)
Christopher Faulet3d97c902015-12-09 14:59:38 +0100877{
Christopher Faulet92d36382015-11-05 13:35:03 +0100878 struct http_txn *txn = smp->strm->txn;
879 struct filter *filter;
880 struct comp_state *st;
881
882 if (!(txn || !(txn->rsp.flags & HTTP_MSGF_COMPRESSING)))
Christopher Faulet3d97c902015-12-09 14:59:38 +0100883 return 0;
884
Christopher Faulet92d36382015-11-05 13:35:03 +0100885 list_for_each_entry(filter, &smp->strm->strm_flt.filters, list) {
886 if (filter->id != http_comp_flt_id)
887 continue;
888
889 if (!(st = filter->ctx))
890 break;
891
892 smp->data.type = SMP_T_STR;
893 smp->flags = SMP_F_CONST;
894 smp->data.u.str.str = st->comp_algo->cfg_name;
895 smp->data.u.str.len = st->comp_algo->cfg_name_len;
896 return 1;
897 }
898 return 0;
Christopher Faulet3d97c902015-12-09 14:59:38 +0100899}
900
901/* Declare the config parser for "compression" keyword */
902static struct cfg_kw_list cfg_kws = {ILH, {
903 { CFG_LISTEN, "compression", parse_compression_options },
904 { 0, NULL, NULL },
905 }
906};
907
Christopher Faulet92d36382015-11-05 13:35:03 +0100908/* Declare the filter parser for "compression" keyword */
909static struct flt_kw_list filter_kws = { "COMP", { }, {
910 { "compression", parse_http_comp_flt },
911 { NULL, NULL },
912 }
913};
914
Christopher Faulet3d97c902015-12-09 14:59:38 +0100915/* Note: must not be declared <const> as its list will be overwritten */
916static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
Christopher Faulet92d36382015-11-05 13:35:03 +0100917 { "res.comp", smp_fetch_res_comp, 0, NULL, SMP_T_BOOL, SMP_USE_HRSHP },
918 { "res.comp_algo", smp_fetch_res_comp_algo, 0, NULL, SMP_T_STR, SMP_USE_HRSHP },
919 { /* END */ },
920 }
921};
Christopher Faulet3d97c902015-12-09 14:59:38 +0100922
923__attribute__((constructor))
Christopher Faulet92d36382015-11-05 13:35:03 +0100924static void
925__flt_http_comp_init(void)
Christopher Faulet3d97c902015-12-09 14:59:38 +0100926{
927 cfg_register_keywords(&cfg_kws);
Christopher Faulet92d36382015-11-05 13:35:03 +0100928 flt_register_keywords(&filter_kws);
Christopher Faulet3d97c902015-12-09 14:59:38 +0100929 sample_register_fetches(&sample_fetch_keywords);
930}