blob: 17a81e80e64b54a89319bcba9417f282e0d7ff3f [file] [log] [blame]
William Lallemand82fe75c2012-10-23 10:25:10 +02001/*
2 * HTTP compression.
3 *
4 * Copyright 2012 Exceliance, David Du Colombier <dducolombier@exceliance.fr>
5 * William Lallemand <wlallemand@exceliance.fr>
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
11 *
12 */
13
14#include <stdio.h>
Willy Tarreau34763642012-10-26 15:05:35 +020015
William Lallemand08289f12012-10-31 11:19:18 +010016#ifdef USE_ZLIB
Willy Tarreau34763642012-10-26 15:05:35 +020017/* Note: the crappy zlib and openssl libs both define the "free_func" type.
18 * That's a very clever idea to use such a generic name in general purpose
19 * libraries, really... The zlib one is easier to redefine than openssl's,
20 * so let's only fix this one.
21 */
22#define free_func zlib_free_func
William Lallemand82fe75c2012-10-23 10:25:10 +020023#include <zlib.h>
Willy Tarreau34763642012-10-26 15:05:35 +020024#undef free_func
William Lallemand08289f12012-10-31 11:19:18 +010025#endif /* USE_ZLIB */
William Lallemand82fe75c2012-10-23 10:25:10 +020026
27#include <common/compat.h>
William Lallemand2b502472012-10-30 14:30:39 +010028#include <common/memory.h>
William Lallemand82fe75c2012-10-23 10:25:10 +020029
30#include <types/global.h>
31#include <types/compression.h>
32
William Lallemand727db8b2013-04-20 17:33:20 +020033#include <proto/acl.h>
William Lallemand82fe75c2012-10-23 10:25:10 +020034#include <proto/compression.h>
William Lallemandd85f9172012-11-09 17:05:39 +010035#include <proto/freq_ctr.h>
William Lallemand82fe75c2012-10-23 10:25:10 +020036#include <proto/proto_http.h>
37
William Lallemand2b502472012-10-30 14:30:39 +010038
39#ifdef USE_ZLIB
40
William Lallemand8b52bb32012-11-16 18:06:41 +010041static void *alloc_zlib(void *opaque, unsigned int items, unsigned int size);
42static void free_zlib(void *opaque, void *ptr);
43
William Lallemand2b502472012-10-30 14:30:39 +010044/* zlib allocation */
45static struct pool_head *zlib_pool_deflate_state = NULL;
46static struct pool_head *zlib_pool_window = NULL;
47static struct pool_head *zlib_pool_prev = NULL;
48static struct pool_head *zlib_pool_head = NULL;
49static struct pool_head *zlib_pool_pending_buf = NULL;
50
William Lallemande3a7d992012-11-20 11:25:20 +010051long zlib_used_memory = 0;
William Lallemand9d5f5482012-11-07 16:12:57 +010052
William Lallemand2b502472012-10-30 14:30:39 +010053#endif
54
William Lallemand072a2bf2012-11-20 17:01:01 +010055unsigned int compress_min_idle = 0;
William Lallemand8b52bb32012-11-16 18:06:41 +010056static struct pool_head *pool_comp_ctx = NULL;
57
William Lallemand2b502472012-10-30 14:30:39 +010058
Cyril Bonté6162c432012-11-10 19:27:47 +010059const struct comp_algo comp_algos[] =
William Lallemand82fe75c2012-10-23 10:25:10 +020060{
61 { "identity", 8, identity_init, identity_add_data, identity_flush, identity_reset, identity_end },
62#ifdef USE_ZLIB
63 { "deflate", 7, deflate_init, deflate_add_data, deflate_flush, deflate_reset, deflate_end },
64 { "gzip", 4, gzip_init, deflate_add_data, deflate_flush, deflate_reset, deflate_end },
65#endif /* USE_ZLIB */
66 { NULL, 0, NULL , NULL, NULL, NULL, NULL }
67};
68
69/*
70 * Add a content-type in the configuration
71 */
72int comp_append_type(struct comp *comp, const char *type)
73{
74 struct comp_type *comp_type;
75
76 comp_type = calloc(1, sizeof(struct comp_type));
77 comp_type->name_len = strlen(type);
78 comp_type->name = strdup(type);
79 comp_type->next = comp->types;
80 comp->types = comp_type;
81 return 0;
82}
83
84/*
85 * Add an algorithm in the configuration
86 */
87int comp_append_algo(struct comp *comp, const char *algo)
88{
89 struct comp_algo *comp_algo;
90 int i;
91
92 for (i = 0; comp_algos[i].name; i++) {
93 if (!strcmp(algo, comp_algos[i].name)) {
94 comp_algo = calloc(1, sizeof(struct comp_algo));
95 memmove(comp_algo, &comp_algos[i], sizeof(struct comp_algo));
96 comp_algo->next = comp->algos;
97 comp->algos = comp_algo;
98 return 0;
99 }
100 }
101 return -1;
102}
103
104/* emit the chunksize followed by a CRLF on the output and return the number of
105 * bytes written. Appends <add_crlf> additional CRLF after the first one. Chunk
106 * sizes are truncated to 6 hex digits (16 MB) and padded left. The caller is
107 * responsible for ensuring there is enough room left in the output buffer for
108 * the string (8 bytes * add_crlf*2).
109 */
110int http_emit_chunk_size(char *out, unsigned int chksz, int add_crlf)
111{
112 int shift;
113 int pos = 0;
114
115 for (shift = 20; shift >= 0; shift -= 4)
116 out[pos++] = hextab[(chksz >> shift) & 0xF];
117
118 do {
119 out[pos++] = '\r';
120 out[pos++] = '\n';
121 } while (--add_crlf >= 0);
122
123 return pos;
124}
125
126/*
127 * Init HTTP compression
128 */
129int http_compression_buffer_init(struct session *s, struct buffer *in, struct buffer *out)
130{
William Lallemand82fe75c2012-10-23 10:25:10 +0200131 int left;
132
133 /* not enough space */
134 if (in->size - buffer_len(in) < 40)
135 return -1;
136
Willy Tarreau7f2f8d52014-04-18 00:20:14 +0200137 /* We start by copying the current buffer's pending outgoing data into
138 * a new temporary buffer that we initialize with a new empty chunk.
William Lallemand82fe75c2012-10-23 10:25:10 +0200139 */
William Lallemand82fe75c2012-10-23 10:25:10 +0200140
141 out->size = global.tune.bufsize;
142 out->i = 0;
143 out->o = 0;
144 out->p = out->data;
Willy Tarreau7f2f8d52014-04-18 00:20:14 +0200145
William Lallemand82fe75c2012-10-23 10:25:10 +0200146 if (in->o > 0) {
147 left = in->o - bo_contig_data(in);
148 memcpy(out->data, bo_ptr(in), bo_contig_data(in));
149 out->p += bo_contig_data(in);
150 if (left > 0) { /* second part of the buffer */
151 memcpy(out->p, in->data, left);
152 out->p += left;
153 }
154 out->o = in->o;
155 }
156 out->i += http_emit_chunk_size(out->p, 0, 0);
157
158 return 0;
159}
160
161/*
162 * Add data to compress
163 */
164int http_compression_buffer_add_data(struct session *s, struct buffer *in, struct buffer *out)
165{
166 struct http_msg *msg = &s->txn.rsp;
William Lallemandbf3ae612012-11-19 12:35:37 +0100167 int consumed_data = 0;
William Lallemand82fe75c2012-10-23 10:25:10 +0200168 int data_process_len;
Willy Tarreau7f2f8d52014-04-18 00:20:14 +0200169 int block1, block2;
William Lallemand82fe75c2012-10-23 10:25:10 +0200170
171 /*
Willy Tarreau7f2f8d52014-04-18 00:20:14 +0200172 * Temporarily skip already parsed data and chunks to jump to the
173 * actual data block. It is fixed before leaving.
William Lallemand82fe75c2012-10-23 10:25:10 +0200174 */
175 b_adv(in, msg->next);
William Lallemand82fe75c2012-10-23 10:25:10 +0200176
177 /*
178 * select the smallest size between the announced chunk size, the input
Willy Tarreau7f2f8d52014-04-18 00:20:14 +0200179 * data, and the available output buffer size. The compressors are
180 * assumed to be able to process all the bytes we pass to them at once.
William Lallemand82fe75c2012-10-23 10:25:10 +0200181 */
182 data_process_len = MIN(in->i, msg->chunk_len);
183 data_process_len = MIN(out->size - buffer_len(out), data_process_len);
184
Willy Tarreau7f2f8d52014-04-18 00:20:14 +0200185 block1 = data_process_len;
186 if (block1 > bi_contig_data(in))
187 block1 = bi_contig_data(in);
188 block2 = data_process_len - block1;
William Lallemand82fe75c2012-10-23 10:25:10 +0200189
Willy Tarreau7f2f8d52014-04-18 00:20:14 +0200190 /* compressors return < 0 upon error or the amount of bytes read */
191 consumed_data = s->comp_algo->add_data(s->comp_ctx, bi_ptr(in), block1, out);
192 if (consumed_data >= 0 && block2 > 0) {
193 consumed_data = s->comp_algo->add_data(s->comp_ctx, in->data, block2, out);
194 if (consumed_data >= 0)
195 consumed_data += block1;
William Lallemand82fe75c2012-10-23 10:25:10 +0200196 }
197
Willy Tarreau7f2f8d52014-04-18 00:20:14 +0200198 /* restore original buffer pointer */
199 b_rew(in, msg->next);
William Lallemand82fe75c2012-10-23 10:25:10 +0200200
Willy Tarreau7f2f8d52014-04-18 00:20:14 +0200201 if (consumed_data > 0) {
202 msg->next += consumed_data;
203 msg->chunk_len -= consumed_data;
204 }
William Lallemandbf3ae612012-11-19 12:35:37 +0100205 return consumed_data;
William Lallemand82fe75c2012-10-23 10:25:10 +0200206}
207
208/*
209 * Flush data in process, and write the header and footer of the chunk. Upon
210 * success, in and out buffers are swapped to avoid a copy.
211 */
212int http_compression_buffer_end(struct session *s, struct buffer **in, struct buffer **out, int end)
213{
Willy Tarreau55058a72012-11-21 08:27:21 +0100214 int to_forward, forwarded;
William Lallemand82fe75c2012-10-23 10:25:10 +0200215 int left;
216 struct http_msg *msg = &s->txn.rsp;
217 struct buffer *ib = *in, *ob = *out;
William Lallemand08289f12012-10-31 11:19:18 +0100218
219#ifdef USE_ZLIB
William Lallemand82fe75c2012-10-23 10:25:10 +0200220 int ret;
221
222 /* flush data here */
223
224 if (end)
William Lallemand8b52bb32012-11-16 18:06:41 +0100225 ret = s->comp_algo->flush(s->comp_ctx, ob, Z_FINISH); /* end of data */
William Lallemand82fe75c2012-10-23 10:25:10 +0200226 else
William Lallemand8b52bb32012-11-16 18:06:41 +0100227 ret = s->comp_algo->flush(s->comp_ctx, ob, Z_SYNC_FLUSH); /* end of buffer */
William Lallemand82fe75c2012-10-23 10:25:10 +0200228
229 if (ret < 0)
230 return -1; /* flush failed */
231
William Lallemand08289f12012-10-31 11:19:18 +0100232#endif /* USE_ZLIB */
233
William Lallemand82fe75c2012-10-23 10:25:10 +0200234 if (ob->i > 8) {
235 /* more than a chunk size => some data were emitted */
236 char *tail = ob->p + ob->i;
237
238 /* write real size at the begining of the chunk, no need of wrapping */
239 http_emit_chunk_size(ob->p, ob->i - 8, 0);
240
241 /* chunked encoding requires CRLF after data */
242 *tail++ = '\r';
243 *tail++ = '\n';
244
Willy Tarreau7f2f8d52014-04-18 00:20:14 +0200245 /* At the end of data, we must write the empty chunk 0<CRLF>,
246 * and terminate the trailers section with a last <CRLF>. If
247 * we're forwarding a chunked-encoded response, we'll have a
248 * trailers section after the empty chunk which needs to be
249 * forwarded and which will provide the last CRLF. Otherwise
250 * we write it ourselves.
251 */
252 if (msg->msg_state >= HTTP_MSG_TRAILERS) {
253 memcpy(tail, "0\r\n", 3);
254 tail += 3;
255 if (msg->msg_state >= HTTP_MSG_DONE) {
256 memcpy(tail, "\r\n", 2);
257 tail += 2;
258 }
William Lallemand82fe75c2012-10-23 10:25:10 +0200259 }
260 ob->i = tail - ob->p;
261 } else {
262 /* no data were sent, cancel the chunk size */
263 ob->i = 0;
264 }
265
266 to_forward = ob->i;
Willy Tarreau55058a72012-11-21 08:27:21 +0100267
William Lallemandd85f9172012-11-09 17:05:39 +0100268 /* update input rate */
Willy Tarreau55058a72012-11-21 08:27:21 +0100269 forwarded = ib->o - ob->o;
270 if (s->comp_ctx && s->comp_ctx->cur_lvl > 0) {
271 update_freq_ctr(&global.comp_bps_in, forwarded);
272 s->fe->fe_counters.comp_in += forwarded;
273 s->be->be_counters.comp_in += forwarded;
274 } else {
275 s->fe->fe_counters.comp_byp += forwarded;
276 s->be->be_counters.comp_byp += forwarded;
277 }
William Lallemandd85f9172012-11-09 17:05:39 +0100278
William Lallemand82fe75c2012-10-23 10:25:10 +0200279 /* copy the remaining data in the tmp buffer. */
Willy Tarreau7f2f8d52014-04-18 00:20:14 +0200280 b_adv(ib, msg->next);
281 msg->next = 0;
282
William Lallemand82fe75c2012-10-23 10:25:10 +0200283 if (ib->i > 0) {
284 left = ib->i - bi_contig_data(ib);
285 memcpy(bi_end(ob), bi_ptr(ib), bi_contig_data(ib));
286 ob->i += bi_contig_data(ib);
287 if (left > 0) {
288 memcpy(bi_end(ob), ib->data, left);
289 ob->i += left;
290 }
291 }
292
293 /* swap the buffers */
294 *in = ob;
295 *out = ib;
296
Willy Tarreau55058a72012-11-21 08:27:21 +0100297 if (s->comp_ctx && s->comp_ctx->cur_lvl > 0) {
William Lallemandd85f9172012-11-09 17:05:39 +0100298 update_freq_ctr(&global.comp_bps_out, to_forward);
Willy Tarreau55058a72012-11-21 08:27:21 +0100299 s->fe->fe_counters.comp_out += to_forward;
300 s->be->be_counters.comp_out += to_forward;
301 }
William Lallemandd85f9172012-11-09 17:05:39 +0100302
William Lallemand82fe75c2012-10-23 10:25:10 +0200303 /* forward the new chunk without remaining data */
304 b_adv(ob, to_forward);
305
William Lallemand82fe75c2012-10-23 10:25:10 +0200306 return to_forward;
307}
308
William Lallemand8b52bb32012-11-16 18:06:41 +0100309/*
310 * Alloc the comp_ctx
311 */
312static inline int init_comp_ctx(struct comp_ctx **comp_ctx)
313{
314#ifdef USE_ZLIB
315 z_stream *strm;
316
William Lallemande3a7d992012-11-20 11:25:20 +0100317 if (global.maxzlibmem > 0 && (global.maxzlibmem - zlib_used_memory) < sizeof(struct comp_ctx))
William Lallemand8b52bb32012-11-16 18:06:41 +0100318 return -1;
319#endif
320
321 if (unlikely(pool_comp_ctx == NULL))
322 pool_comp_ctx = create_pool("comp_ctx", sizeof(struct comp_ctx), MEM_F_SHARED);
323
324 *comp_ctx = pool_alloc2(pool_comp_ctx);
325 if (*comp_ctx == NULL)
326 return -1;
327#ifdef USE_ZLIB
William Lallemande3a7d992012-11-20 11:25:20 +0100328 zlib_used_memory += sizeof(struct comp_ctx);
William Lallemand8b52bb32012-11-16 18:06:41 +0100329
330 strm = &(*comp_ctx)->strm;
331 strm->zalloc = alloc_zlib;
332 strm->zfree = free_zlib;
333 strm->opaque = *comp_ctx;
334#endif
335 return 0;
336}
337
338/*
339 * Dealloc the comp_ctx
340 */
341static inline int deinit_comp_ctx(struct comp_ctx **comp_ctx)
342{
343 if (!*comp_ctx)
344 return 0;
345
346 pool_free2(pool_comp_ctx, *comp_ctx);
347 *comp_ctx = NULL;
348
349#ifdef USE_ZLIB
William Lallemande3a7d992012-11-20 11:25:20 +0100350 zlib_used_memory -= sizeof(struct comp_ctx);
William Lallemand8b52bb32012-11-16 18:06:41 +0100351#endif
William Lallemand8b52bb32012-11-16 18:06:41 +0100352 return 0;
353}
354
William Lallemand82fe75c2012-10-23 10:25:10 +0200355
356/****************************
357 **** Identity algorithm ****
358 ****************************/
359
360/*
361 * Init the identity algorithm
362 */
William Lallemand8b52bb32012-11-16 18:06:41 +0100363int identity_init(struct comp_ctx **comp_ctx, int level)
William Lallemand82fe75c2012-10-23 10:25:10 +0200364{
365 return 0;
366}
367
368/*
369 * Process data
William Lallemandbf3ae612012-11-19 12:35:37 +0100370 * Return size of consumed data or -1 on error
William Lallemand82fe75c2012-10-23 10:25:10 +0200371 */
William Lallemandbf3ae612012-11-19 12:35:37 +0100372int identity_add_data(struct comp_ctx *comp_ctx, const char *in_data, int in_len, struct buffer *out)
William Lallemand82fe75c2012-10-23 10:25:10 +0200373{
William Lallemandbf3ae612012-11-19 12:35:37 +0100374 char *out_data = bi_end(out);
375 int out_len = out->size - buffer_len(out);
376
William Lallemand82fe75c2012-10-23 10:25:10 +0200377 if (out_len < in_len)
378 return -1;
379
380 memcpy(out_data, in_data, in_len);
381
William Lallemandbf3ae612012-11-19 12:35:37 +0100382 out->i += in_len;
383
William Lallemand82fe75c2012-10-23 10:25:10 +0200384 return in_len;
385}
386
William Lallemand1c2d6222012-10-30 15:52:53 +0100387int identity_flush(struct comp_ctx *comp_ctx, struct buffer *out, int flag)
William Lallemand82fe75c2012-10-23 10:25:10 +0200388{
389 return 0;
390}
391
William Lallemand1c2d6222012-10-30 15:52:53 +0100392int identity_reset(struct comp_ctx *comp_ctx)
William Lallemand82fe75c2012-10-23 10:25:10 +0200393{
394 return 0;
395}
396
397/*
398 * Deinit the algorithm
399 */
William Lallemand8b52bb32012-11-16 18:06:41 +0100400int identity_end(struct comp_ctx **comp_ctx)
William Lallemand82fe75c2012-10-23 10:25:10 +0200401{
402 return 0;
403}
404
405
406#ifdef USE_ZLIB
William Lallemand2b502472012-10-30 14:30:39 +0100407/*
408 * This is a tricky allocation function using the zlib.
409 * This is based on the allocation order in deflateInit2.
410 */
411static void *alloc_zlib(void *opaque, unsigned int items, unsigned int size)
412{
413 struct comp_ctx *ctx = opaque;
414 static char round = 0; /* order in deflateInit2 */
415 void *buf = NULL;
416
William Lallemande3a7d992012-11-20 11:25:20 +0100417 if (global.maxzlibmem > 0 && (global.maxzlibmem - zlib_used_memory) < (long)(items * size))
William Lallemand9d5f5482012-11-07 16:12:57 +0100418 goto end;
William Lallemand9d5f5482012-11-07 16:12:57 +0100419
William Lallemand2b502472012-10-30 14:30:39 +0100420 switch (round) {
421 case 0:
422 if (zlib_pool_deflate_state == NULL)
423 zlib_pool_deflate_state = create_pool("zlib_state", size * items, MEM_F_SHARED);
424 ctx->zlib_deflate_state = buf = pool_alloc2(zlib_pool_deflate_state);
425 break;
426
427 case 1:
428 if (zlib_pool_window == NULL)
429 zlib_pool_window = create_pool("zlib_window", size * items, MEM_F_SHARED);
430 ctx->zlib_window = buf = pool_alloc2(zlib_pool_window);
431 break;
432
433 case 2:
434 if (zlib_pool_prev == NULL)
435 zlib_pool_prev = create_pool("zlib_prev", size * items, MEM_F_SHARED);
436 ctx->zlib_prev = buf = pool_alloc2(zlib_pool_prev);
437 break;
438
439 case 3:
440 if (zlib_pool_head == NULL)
441 zlib_pool_head = create_pool("zlib_head", size * items, MEM_F_SHARED);
442 ctx->zlib_head = buf = pool_alloc2(zlib_pool_head);
443 break;
444
445 case 4:
446 if (zlib_pool_pending_buf == NULL)
447 zlib_pool_pending_buf = create_pool("zlib_pending_buf", size * items, MEM_F_SHARED);
448 ctx->zlib_pending_buf = buf = pool_alloc2(zlib_pool_pending_buf);
449 break;
450 }
William Lallemande3a7d992012-11-20 11:25:20 +0100451 if (buf != NULL)
452 zlib_used_memory += items * size;
William Lallemand9d5f5482012-11-07 16:12:57 +0100453
454end:
William Lallemand2b502472012-10-30 14:30:39 +0100455
Willy Tarreau46909852012-11-15 14:57:56 +0100456 /* deflateInit2() first allocates and checks the deflate_state, then if
457 * it succeeds, it allocates all other 4 areas at ones and checks them
458 * at the end. So we want to correctly count the rounds depending on when
459 * zlib is supposed to abort.
460 */
461 if (buf || round)
462 round = (round + 1) % 5;
William Lallemand2b502472012-10-30 14:30:39 +0100463 return buf;
464}
465
466static void free_zlib(void *opaque, void *ptr)
467{
468 struct comp_ctx *ctx = opaque;
Willy Tarreaub1fbd052012-11-10 17:49:37 +0100469 struct pool_head *pool = NULL;
William Lallemand2b502472012-10-30 14:30:39 +0100470
471 if (ptr == ctx->zlib_window)
William Lallemand9d5f5482012-11-07 16:12:57 +0100472 pool = zlib_pool_window;
William Lallemand2b502472012-10-30 14:30:39 +0100473 else if (ptr == ctx->zlib_deflate_state)
William Lallemand9d5f5482012-11-07 16:12:57 +0100474 pool = zlib_pool_deflate_state;
William Lallemand2b502472012-10-30 14:30:39 +0100475 else if (ptr == ctx->zlib_prev)
William Lallemand9d5f5482012-11-07 16:12:57 +0100476 pool = zlib_pool_prev;
William Lallemand2b502472012-10-30 14:30:39 +0100477 else if (ptr == ctx->zlib_head)
William Lallemand9d5f5482012-11-07 16:12:57 +0100478 pool = zlib_pool_head;
William Lallemand2b502472012-10-30 14:30:39 +0100479 else if (ptr == ctx->zlib_pending_buf)
William Lallemand9d5f5482012-11-07 16:12:57 +0100480 pool = zlib_pool_pending_buf;
William Lallemand2b502472012-10-30 14:30:39 +0100481
William Lallemand9d5f5482012-11-07 16:12:57 +0100482 pool_free2(pool, ptr);
William Lallemande3a7d992012-11-20 11:25:20 +0100483 zlib_used_memory -= pool->size;
William Lallemand2b502472012-10-30 14:30:39 +0100484}
485
William Lallemand82fe75c2012-10-23 10:25:10 +0200486/**************************
487**** gzip algorithm ****
488***************************/
William Lallemand8b52bb32012-11-16 18:06:41 +0100489int gzip_init(struct comp_ctx **comp_ctx, int level)
William Lallemand82fe75c2012-10-23 10:25:10 +0200490{
William Lallemand8b52bb32012-11-16 18:06:41 +0100491 z_stream *strm;
William Lallemand82fe75c2012-10-23 10:25:10 +0200492
William Lallemand8b52bb32012-11-16 18:06:41 +0100493 if (init_comp_ctx(comp_ctx) < 0)
494 return -1;
William Lallemand9d5f5482012-11-07 16:12:57 +0100495
William Lallemand8b52bb32012-11-16 18:06:41 +0100496 strm = &(*comp_ctx)->strm;
William Lallemand82fe75c2012-10-23 10:25:10 +0200497
William Lallemand8b52bb32012-11-16 18:06:41 +0100498 if (deflateInit2(strm, level, Z_DEFLATED, global.tune.zlibwindowsize + 16, global.tune.zlibmemlevel, Z_DEFAULT_STRATEGY) != Z_OK) {
499 deinit_comp_ctx(comp_ctx);
William Lallemand82fe75c2012-10-23 10:25:10 +0200500 return -1;
William Lallemand8b52bb32012-11-16 18:06:41 +0100501 }
502
503 (*comp_ctx)->cur_lvl = level;
William Lallemand82fe75c2012-10-23 10:25:10 +0200504
505 return 0;
506}
507/**************************
508**** Deflate algorithm ****
509***************************/
510
William Lallemand8b52bb32012-11-16 18:06:41 +0100511int deflate_init(struct comp_ctx **comp_ctx, int level)
William Lallemand82fe75c2012-10-23 10:25:10 +0200512{
William Lallemand8b52bb32012-11-16 18:06:41 +0100513 z_stream *strm;
William Lallemand82fe75c2012-10-23 10:25:10 +0200514
William Lallemand8b52bb32012-11-16 18:06:41 +0100515 if (init_comp_ctx(comp_ctx) < 0)
516 return -1;
517
518 strm = &(*comp_ctx)->strm;
William Lallemand82fe75c2012-10-23 10:25:10 +0200519
Willy Tarreauc5599e72013-04-28 08:52:52 +0200520 if (deflateInit2(strm, level, Z_DEFLATED, global.tune.zlibwindowsize, global.tune.zlibmemlevel, Z_DEFAULT_STRATEGY) != Z_OK) {
William Lallemand8b52bb32012-11-16 18:06:41 +0100521 deinit_comp_ctx(comp_ctx);
William Lallemand82fe75c2012-10-23 10:25:10 +0200522 return -1;
William Lallemand8b52bb32012-11-16 18:06:41 +0100523 }
524
525 (*comp_ctx)->cur_lvl = level;
William Lallemand82fe75c2012-10-23 10:25:10 +0200526
527 return 0;
528}
529
William Lallemandbf3ae612012-11-19 12:35:37 +0100530/* Return the size of consumed data or -1 */
531int deflate_add_data(struct comp_ctx *comp_ctx, const char *in_data, int in_len, struct buffer *out)
William Lallemand82fe75c2012-10-23 10:25:10 +0200532{
William Lallemand82fe75c2012-10-23 10:25:10 +0200533 int ret;
William Lallemandbf3ae612012-11-19 12:35:37 +0100534 z_stream *strm = &comp_ctx->strm;
535 char *out_data = bi_end(out);
536 int out_len = out->size - buffer_len(out);
William Lallemand82fe75c2012-10-23 10:25:10 +0200537
538 if (in_len <= 0)
539 return 0;
540
541
542 if (out_len <= 0)
543 return -1;
544
William Lallemand82fe75c2012-10-23 10:25:10 +0200545 strm->next_in = (unsigned char *)in_data;
546 strm->avail_in = in_len;
547 strm->next_out = (unsigned char *)out_data;
548 strm->avail_out = out_len;
549
550 ret = deflate(strm, Z_NO_FLUSH);
551 if (ret != Z_OK)
552 return -1;
553
554 /* deflate update the available data out */
William Lallemandbf3ae612012-11-19 12:35:37 +0100555 out->i += out_len - strm->avail_out;
William Lallemand82fe75c2012-10-23 10:25:10 +0200556
William Lallemandbf3ae612012-11-19 12:35:37 +0100557 return in_len - strm->avail_in;
William Lallemand82fe75c2012-10-23 10:25:10 +0200558}
559
William Lallemand1c2d6222012-10-30 15:52:53 +0100560int deflate_flush(struct comp_ctx *comp_ctx, struct buffer *out, int flag)
William Lallemand82fe75c2012-10-23 10:25:10 +0200561{
562 int ret;
William Lallemand82fe75c2012-10-23 10:25:10 +0200563 int out_len = 0;
William Lallemand1c2d6222012-10-30 15:52:53 +0100564 z_stream *strm = &comp_ctx->strm;
William Lallemand82fe75c2012-10-23 10:25:10 +0200565
William Lallemand82fe75c2012-10-23 10:25:10 +0200566 strm->next_out = (unsigned char *)bi_end(out);
567 strm->avail_out = out->size - buffer_len(out);
568
569 ret = deflate(strm, flag);
570 if (ret != Z_OK && ret != Z_STREAM_END)
571 return -1;
572
573 out_len = (out->size - buffer_len(out)) - strm->avail_out;
574 out->i += out_len;
575
William Lallemand072a2bf2012-11-20 17:01:01 +0100576 /* compression limit */
577 if ((global.comp_rate_lim > 0 && (read_freq_ctr(&global.comp_bps_out) > global.comp_rate_lim)) || /* rate */
578 (idle_pct < compress_min_idle)) { /* idle */
579 /* decrease level */
580 if (comp_ctx->cur_lvl > 0) {
581 comp_ctx->cur_lvl--;
William Lallemandd85f9172012-11-09 17:05:39 +0100582 deflateParams(&comp_ctx->strm, comp_ctx->cur_lvl, Z_DEFAULT_STRATEGY);
583 }
William Lallemand072a2bf2012-11-20 17:01:01 +0100584
585 } else if (comp_ctx->cur_lvl < global.tune.comp_maxlevel) {
586 /* increase level */
587 comp_ctx->cur_lvl++ ;
588 deflateParams(&comp_ctx->strm, comp_ctx->cur_lvl, Z_DEFAULT_STRATEGY);
William Lallemandd85f9172012-11-09 17:05:39 +0100589 }
590
William Lallemand82fe75c2012-10-23 10:25:10 +0200591 return out_len;
592}
593
William Lallemand1c2d6222012-10-30 15:52:53 +0100594int deflate_reset(struct comp_ctx *comp_ctx)
William Lallemand82fe75c2012-10-23 10:25:10 +0200595{
William Lallemand1c2d6222012-10-30 15:52:53 +0100596 z_stream *strm = &comp_ctx->strm;
William Lallemand82fe75c2012-10-23 10:25:10 +0200597
William Lallemand82fe75c2012-10-23 10:25:10 +0200598 if (deflateReset(strm) == Z_OK)
599 return 0;
600 return -1;
601}
602
William Lallemand8b52bb32012-11-16 18:06:41 +0100603int deflate_end(struct comp_ctx **comp_ctx)
William Lallemand82fe75c2012-10-23 10:25:10 +0200604{
William Lallemand8b52bb32012-11-16 18:06:41 +0100605 z_stream *strm = &(*comp_ctx)->strm;
606 int ret;
William Lallemand82fe75c2012-10-23 10:25:10 +0200607
William Lallemand8b52bb32012-11-16 18:06:41 +0100608 ret = deflateEnd(strm);
William Lallemand82fe75c2012-10-23 10:25:10 +0200609
William Lallemand8b52bb32012-11-16 18:06:41 +0100610 deinit_comp_ctx(comp_ctx);
611
612 return ret;
William Lallemand82fe75c2012-10-23 10:25:10 +0200613}
614
615#endif /* USE_ZLIB */
616
William Lallemand727db8b2013-04-20 17:33:20 +0200617/* boolean, returns true if compression is used (either gzip or deflate) in the response */
618static int
619smp_fetch_res_comp(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +0200620 const struct arg *args, struct sample *smp, const char *kw)
William Lallemand727db8b2013-04-20 17:33:20 +0200621{
622 smp->type = SMP_T_BOOL;
623 smp->data.uint = (l4->comp_algo != NULL);
624 return 1;
625}
626
627/* string, returns algo */
628static int
629smp_fetch_res_comp_algo(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +0200630 const struct arg *args, struct sample *smp, const char *kw)
William Lallemand727db8b2013-04-20 17:33:20 +0200631{
632 if (!l4->comp_algo)
633 return 0;
634
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100635 smp->type = SMP_T_STR;
636 smp->flags = SMP_F_CONST;
William Lallemand727db8b2013-04-20 17:33:20 +0200637 smp->data.str.str = l4->comp_algo->name;
638 smp->data.str.len = l4->comp_algo->name_len;
639 return 1;
640}
641
642/* Note: must not be declared <const> as its list will be overwritten */
Willy Tarreaudc13c112013-06-21 23:16:39 +0200643static struct acl_kw_list acl_kws = {ILH, {
Willy Tarreau7f6fa692013-04-23 19:39:43 +0200644 { /* END */ },
William Lallemand727db8b2013-04-20 17:33:20 +0200645}};
646
647/* Note: must not be declared <const> as its list will be overwritten */
Willy Tarreaudc13c112013-06-21 23:16:39 +0200648static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
William Lallemand727db8b2013-04-20 17:33:20 +0200649 { "res.comp", smp_fetch_res_comp, 0, NULL, SMP_T_BOOL, SMP_USE_HRSHP },
650 { "res.comp_algo", smp_fetch_res_comp_algo, 0, NULL, SMP_T_STR, SMP_USE_HRSHP },
651 { /* END */ },
652}};
653
654__attribute__((constructor))
655static void __comp_fetch_init(void)
656{
657 acl_register_keywords(&acl_kws);
658 sample_register_fetches(&sample_fetch_keywords);
659}