blob: f42f77101fcb35146086028ee4fb7cc6ab0e64ca [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{
131 struct http_msg *msg = &s->txn.rsp;
132 int left;
133
134 /* not enough space */
135 if (in->size - buffer_len(in) < 40)
136 return -1;
137
138 /*
139 * Skip data, we don't need them in the new buffer. They are results
140 * of CHUNK_CRLF and CHUNK_SIZE parsing.
141 */
142 b_adv(in, msg->next);
143 msg->next = 0;
144 msg->sov = 0;
145 msg->sol = 0;
146
147 out->size = global.tune.bufsize;
148 out->i = 0;
149 out->o = 0;
150 out->p = out->data;
151 /* copy output data */
152 if (in->o > 0) {
153 left = in->o - bo_contig_data(in);
154 memcpy(out->data, bo_ptr(in), bo_contig_data(in));
155 out->p += bo_contig_data(in);
156 if (left > 0) { /* second part of the buffer */
157 memcpy(out->p, in->data, left);
158 out->p += left;
159 }
160 out->o = in->o;
161 }
162 out->i += http_emit_chunk_size(out->p, 0, 0);
163
164 return 0;
165}
166
167/*
168 * Add data to compress
169 */
170int http_compression_buffer_add_data(struct session *s, struct buffer *in, struct buffer *out)
171{
172 struct http_msg *msg = &s->txn.rsp;
William Lallemandbf3ae612012-11-19 12:35:37 +0100173 int consumed_data = 0;
William Lallemand82fe75c2012-10-23 10:25:10 +0200174 int data_process_len;
175 int left;
176 int ret;
177
178 /*
179 * Skip data, we don't need them in the new buffer. They are results
180 * of CHUNK_CRLF and CHUNK_SIZE parsing.
181 */
182 b_adv(in, msg->next);
183 msg->next = 0;
184 msg->sov = 0;
185 msg->sol = 0;
186
187 /*
188 * select the smallest size between the announced chunk size, the input
189 * data, and the available output buffer size
190 */
191 data_process_len = MIN(in->i, msg->chunk_len);
192 data_process_len = MIN(out->size - buffer_len(out), data_process_len);
193
194 left = data_process_len - bi_contig_data(in);
195 if (left <= 0) {
William Lallemand8b52bb32012-11-16 18:06:41 +0100196 consumed_data += ret = s->comp_algo->add_data(s->comp_ctx, bi_ptr(in), data_process_len, out);
William Lallemand82fe75c2012-10-23 10:25:10 +0200197 if (ret < 0)
198 return -1;
William Lallemand82fe75c2012-10-23 10:25:10 +0200199
200 } else {
William Lallemand8b52bb32012-11-16 18:06:41 +0100201 consumed_data += ret = s->comp_algo->add_data(s->comp_ctx, bi_ptr(in), bi_contig_data(in), out);
William Lallemand82fe75c2012-10-23 10:25:10 +0200202 if (ret < 0)
203 return -1;
William Lallemand8b52bb32012-11-16 18:06:41 +0100204 consumed_data += ret = s->comp_algo->add_data(s->comp_ctx, in->data, left, out);
William Lallemand82fe75c2012-10-23 10:25:10 +0200205 if (ret < 0)
206 return -1;
William Lallemand82fe75c2012-10-23 10:25:10 +0200207 }
208
209 b_adv(in, data_process_len);
210 msg->chunk_len -= data_process_len;
211
William Lallemandbf3ae612012-11-19 12:35:37 +0100212 return consumed_data;
William Lallemand82fe75c2012-10-23 10:25:10 +0200213}
214
215/*
216 * Flush data in process, and write the header and footer of the chunk. Upon
217 * success, in and out buffers are swapped to avoid a copy.
218 */
219int http_compression_buffer_end(struct session *s, struct buffer **in, struct buffer **out, int end)
220{
Willy Tarreau55058a72012-11-21 08:27:21 +0100221 int to_forward, forwarded;
William Lallemand82fe75c2012-10-23 10:25:10 +0200222 int left;
223 struct http_msg *msg = &s->txn.rsp;
224 struct buffer *ib = *in, *ob = *out;
William Lallemand08289f12012-10-31 11:19:18 +0100225
226#ifdef USE_ZLIB
William Lallemand82fe75c2012-10-23 10:25:10 +0200227 int ret;
228
229 /* flush data here */
230
231 if (end)
William Lallemand8b52bb32012-11-16 18:06:41 +0100232 ret = s->comp_algo->flush(s->comp_ctx, ob, Z_FINISH); /* end of data */
William Lallemand82fe75c2012-10-23 10:25:10 +0200233 else
William Lallemand8b52bb32012-11-16 18:06:41 +0100234 ret = s->comp_algo->flush(s->comp_ctx, ob, Z_SYNC_FLUSH); /* end of buffer */
William Lallemand82fe75c2012-10-23 10:25:10 +0200235
236 if (ret < 0)
237 return -1; /* flush failed */
238
William Lallemand08289f12012-10-31 11:19:18 +0100239#endif /* USE_ZLIB */
240
William Lallemand82fe75c2012-10-23 10:25:10 +0200241 if (ob->i > 8) {
242 /* more than a chunk size => some data were emitted */
243 char *tail = ob->p + ob->i;
244
245 /* write real size at the begining of the chunk, no need of wrapping */
246 http_emit_chunk_size(ob->p, ob->i - 8, 0);
247
248 /* chunked encoding requires CRLF after data */
249 *tail++ = '\r';
250 *tail++ = '\n';
251
252 if (!(msg->flags & HTTP_MSGF_TE_CHNK) && msg->chunk_len == 0) {
253 /* End of data, 0<CRLF><CRLF> is needed but we're not
254 * in chunked mode on input so we must add it ourselves.
255 */
256 memcpy(tail, "0\r\n\r\n", 5);
257 tail += 5;
258 }
259 ob->i = tail - ob->p;
260 } else {
261 /* no data were sent, cancel the chunk size */
262 ob->i = 0;
263 }
264
265 to_forward = ob->i;
Willy Tarreau55058a72012-11-21 08:27:21 +0100266
William Lallemandd85f9172012-11-09 17:05:39 +0100267 /* update input rate */
Willy Tarreau55058a72012-11-21 08:27:21 +0100268 forwarded = ib->o - ob->o;
269 if (s->comp_ctx && s->comp_ctx->cur_lvl > 0) {
270 update_freq_ctr(&global.comp_bps_in, forwarded);
271 s->fe->fe_counters.comp_in += forwarded;
272 s->be->be_counters.comp_in += forwarded;
273 } else {
274 s->fe->fe_counters.comp_byp += forwarded;
275 s->be->be_counters.comp_byp += forwarded;
276 }
William Lallemandd85f9172012-11-09 17:05:39 +0100277
William Lallemand82fe75c2012-10-23 10:25:10 +0200278 /* copy the remaining data in the tmp buffer. */
279 if (ib->i > 0) {
280 left = ib->i - bi_contig_data(ib);
281 memcpy(bi_end(ob), bi_ptr(ib), bi_contig_data(ib));
282 ob->i += bi_contig_data(ib);
283 if (left > 0) {
284 memcpy(bi_end(ob), ib->data, left);
285 ob->i += left;
286 }
287 }
288
289 /* swap the buffers */
290 *in = ob;
291 *out = ib;
292
Willy Tarreau55058a72012-11-21 08:27:21 +0100293 if (s->comp_ctx && s->comp_ctx->cur_lvl > 0) {
William Lallemandd85f9172012-11-09 17:05:39 +0100294 update_freq_ctr(&global.comp_bps_out, to_forward);
Willy Tarreau55058a72012-11-21 08:27:21 +0100295 s->fe->fe_counters.comp_out += to_forward;
296 s->be->be_counters.comp_out += to_forward;
297 }
William Lallemandd85f9172012-11-09 17:05:39 +0100298
William Lallemand82fe75c2012-10-23 10:25:10 +0200299 /* forward the new chunk without remaining data */
300 b_adv(ob, to_forward);
301
William Lallemand82fe75c2012-10-23 10:25:10 +0200302 return to_forward;
303}
304
William Lallemand8b52bb32012-11-16 18:06:41 +0100305/*
306 * Alloc the comp_ctx
307 */
308static inline int init_comp_ctx(struct comp_ctx **comp_ctx)
309{
310#ifdef USE_ZLIB
311 z_stream *strm;
312
William Lallemande3a7d992012-11-20 11:25:20 +0100313 if (global.maxzlibmem > 0 && (global.maxzlibmem - zlib_used_memory) < sizeof(struct comp_ctx))
William Lallemand8b52bb32012-11-16 18:06:41 +0100314 return -1;
315#endif
316
317 if (unlikely(pool_comp_ctx == NULL))
318 pool_comp_ctx = create_pool("comp_ctx", sizeof(struct comp_ctx), MEM_F_SHARED);
319
320 *comp_ctx = pool_alloc2(pool_comp_ctx);
321 if (*comp_ctx == NULL)
322 return -1;
323#ifdef USE_ZLIB
William Lallemande3a7d992012-11-20 11:25:20 +0100324 zlib_used_memory += sizeof(struct comp_ctx);
William Lallemand8b52bb32012-11-16 18:06:41 +0100325
326 strm = &(*comp_ctx)->strm;
327 strm->zalloc = alloc_zlib;
328 strm->zfree = free_zlib;
329 strm->opaque = *comp_ctx;
330#endif
331 return 0;
332}
333
334/*
335 * Dealloc the comp_ctx
336 */
337static inline int deinit_comp_ctx(struct comp_ctx **comp_ctx)
338{
339 if (!*comp_ctx)
340 return 0;
341
342 pool_free2(pool_comp_ctx, *comp_ctx);
343 *comp_ctx = NULL;
344
345#ifdef USE_ZLIB
William Lallemande3a7d992012-11-20 11:25:20 +0100346 zlib_used_memory -= sizeof(struct comp_ctx);
William Lallemand8b52bb32012-11-16 18:06:41 +0100347#endif
William Lallemand8b52bb32012-11-16 18:06:41 +0100348 return 0;
349}
350
William Lallemand82fe75c2012-10-23 10:25:10 +0200351
352/****************************
353 **** Identity algorithm ****
354 ****************************/
355
356/*
357 * Init the identity algorithm
358 */
William Lallemand8b52bb32012-11-16 18:06:41 +0100359int identity_init(struct comp_ctx **comp_ctx, int level)
William Lallemand82fe75c2012-10-23 10:25:10 +0200360{
361 return 0;
362}
363
364/*
365 * Process data
William Lallemandbf3ae612012-11-19 12:35:37 +0100366 * Return size of consumed data or -1 on error
William Lallemand82fe75c2012-10-23 10:25:10 +0200367 */
William Lallemandbf3ae612012-11-19 12:35:37 +0100368int 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 +0200369{
William Lallemandbf3ae612012-11-19 12:35:37 +0100370 char *out_data = bi_end(out);
371 int out_len = out->size - buffer_len(out);
372
William Lallemand82fe75c2012-10-23 10:25:10 +0200373 if (out_len < in_len)
374 return -1;
375
376 memcpy(out_data, in_data, in_len);
377
William Lallemandbf3ae612012-11-19 12:35:37 +0100378 out->i += in_len;
379
William Lallemand82fe75c2012-10-23 10:25:10 +0200380 return in_len;
381}
382
William Lallemand1c2d6222012-10-30 15:52:53 +0100383int identity_flush(struct comp_ctx *comp_ctx, struct buffer *out, int flag)
William Lallemand82fe75c2012-10-23 10:25:10 +0200384{
385 return 0;
386}
387
William Lallemand1c2d6222012-10-30 15:52:53 +0100388int identity_reset(struct comp_ctx *comp_ctx)
William Lallemand82fe75c2012-10-23 10:25:10 +0200389{
390 return 0;
391}
392
393/*
394 * Deinit the algorithm
395 */
William Lallemand8b52bb32012-11-16 18:06:41 +0100396int identity_end(struct comp_ctx **comp_ctx)
William Lallemand82fe75c2012-10-23 10:25:10 +0200397{
398 return 0;
399}
400
401
402#ifdef USE_ZLIB
William Lallemand2b502472012-10-30 14:30:39 +0100403/*
404 * This is a tricky allocation function using the zlib.
405 * This is based on the allocation order in deflateInit2.
406 */
407static void *alloc_zlib(void *opaque, unsigned int items, unsigned int size)
408{
409 struct comp_ctx *ctx = opaque;
410 static char round = 0; /* order in deflateInit2 */
411 void *buf = NULL;
412
William Lallemande3a7d992012-11-20 11:25:20 +0100413 if (global.maxzlibmem > 0 && (global.maxzlibmem - zlib_used_memory) < (long)(items * size))
William Lallemand9d5f5482012-11-07 16:12:57 +0100414 goto end;
William Lallemand9d5f5482012-11-07 16:12:57 +0100415
William Lallemand2b502472012-10-30 14:30:39 +0100416 switch (round) {
417 case 0:
418 if (zlib_pool_deflate_state == NULL)
419 zlib_pool_deflate_state = create_pool("zlib_state", size * items, MEM_F_SHARED);
420 ctx->zlib_deflate_state = buf = pool_alloc2(zlib_pool_deflate_state);
421 break;
422
423 case 1:
424 if (zlib_pool_window == NULL)
425 zlib_pool_window = create_pool("zlib_window", size * items, MEM_F_SHARED);
426 ctx->zlib_window = buf = pool_alloc2(zlib_pool_window);
427 break;
428
429 case 2:
430 if (zlib_pool_prev == NULL)
431 zlib_pool_prev = create_pool("zlib_prev", size * items, MEM_F_SHARED);
432 ctx->zlib_prev = buf = pool_alloc2(zlib_pool_prev);
433 break;
434
435 case 3:
436 if (zlib_pool_head == NULL)
437 zlib_pool_head = create_pool("zlib_head", size * items, MEM_F_SHARED);
438 ctx->zlib_head = buf = pool_alloc2(zlib_pool_head);
439 break;
440
441 case 4:
442 if (zlib_pool_pending_buf == NULL)
443 zlib_pool_pending_buf = create_pool("zlib_pending_buf", size * items, MEM_F_SHARED);
444 ctx->zlib_pending_buf = buf = pool_alloc2(zlib_pool_pending_buf);
445 break;
446 }
William Lallemande3a7d992012-11-20 11:25:20 +0100447 if (buf != NULL)
448 zlib_used_memory += items * size;
William Lallemand9d5f5482012-11-07 16:12:57 +0100449
450end:
William Lallemand2b502472012-10-30 14:30:39 +0100451
Willy Tarreau46909852012-11-15 14:57:56 +0100452 /* deflateInit2() first allocates and checks the deflate_state, then if
453 * it succeeds, it allocates all other 4 areas at ones and checks them
454 * at the end. So we want to correctly count the rounds depending on when
455 * zlib is supposed to abort.
456 */
457 if (buf || round)
458 round = (round + 1) % 5;
William Lallemand2b502472012-10-30 14:30:39 +0100459 return buf;
460}
461
462static void free_zlib(void *opaque, void *ptr)
463{
464 struct comp_ctx *ctx = opaque;
Willy Tarreaub1fbd052012-11-10 17:49:37 +0100465 struct pool_head *pool = NULL;
William Lallemand2b502472012-10-30 14:30:39 +0100466
467 if (ptr == ctx->zlib_window)
William Lallemand9d5f5482012-11-07 16:12:57 +0100468 pool = zlib_pool_window;
William Lallemand2b502472012-10-30 14:30:39 +0100469 else if (ptr == ctx->zlib_deflate_state)
William Lallemand9d5f5482012-11-07 16:12:57 +0100470 pool = zlib_pool_deflate_state;
William Lallemand2b502472012-10-30 14:30:39 +0100471 else if (ptr == ctx->zlib_prev)
William Lallemand9d5f5482012-11-07 16:12:57 +0100472 pool = zlib_pool_prev;
William Lallemand2b502472012-10-30 14:30:39 +0100473 else if (ptr == ctx->zlib_head)
William Lallemand9d5f5482012-11-07 16:12:57 +0100474 pool = zlib_pool_head;
William Lallemand2b502472012-10-30 14:30:39 +0100475 else if (ptr == ctx->zlib_pending_buf)
William Lallemand9d5f5482012-11-07 16:12:57 +0100476 pool = zlib_pool_pending_buf;
William Lallemand2b502472012-10-30 14:30:39 +0100477
William Lallemand9d5f5482012-11-07 16:12:57 +0100478 pool_free2(pool, ptr);
William Lallemande3a7d992012-11-20 11:25:20 +0100479 zlib_used_memory -= pool->size;
William Lallemand2b502472012-10-30 14:30:39 +0100480}
481
William Lallemand82fe75c2012-10-23 10:25:10 +0200482/**************************
483**** gzip algorithm ****
484***************************/
William Lallemand8b52bb32012-11-16 18:06:41 +0100485int gzip_init(struct comp_ctx **comp_ctx, int level)
William Lallemand82fe75c2012-10-23 10:25:10 +0200486{
William Lallemand8b52bb32012-11-16 18:06:41 +0100487 z_stream *strm;
William Lallemand82fe75c2012-10-23 10:25:10 +0200488
William Lallemand8b52bb32012-11-16 18:06:41 +0100489 if (init_comp_ctx(comp_ctx) < 0)
490 return -1;
William Lallemand9d5f5482012-11-07 16:12:57 +0100491
William Lallemand8b52bb32012-11-16 18:06:41 +0100492 strm = &(*comp_ctx)->strm;
William Lallemand82fe75c2012-10-23 10:25:10 +0200493
William Lallemand8b52bb32012-11-16 18:06:41 +0100494 if (deflateInit2(strm, level, Z_DEFLATED, global.tune.zlibwindowsize + 16, global.tune.zlibmemlevel, Z_DEFAULT_STRATEGY) != Z_OK) {
495 deinit_comp_ctx(comp_ctx);
William Lallemand82fe75c2012-10-23 10:25:10 +0200496 return -1;
William Lallemand8b52bb32012-11-16 18:06:41 +0100497 }
498
499 (*comp_ctx)->cur_lvl = level;
William Lallemand82fe75c2012-10-23 10:25:10 +0200500
501 return 0;
502}
503/**************************
504**** Deflate algorithm ****
505***************************/
506
William Lallemand8b52bb32012-11-16 18:06:41 +0100507int deflate_init(struct comp_ctx **comp_ctx, int level)
William Lallemand82fe75c2012-10-23 10:25:10 +0200508{
William Lallemand8b52bb32012-11-16 18:06:41 +0100509 z_stream *strm;
William Lallemand82fe75c2012-10-23 10:25:10 +0200510
William Lallemand8b52bb32012-11-16 18:06:41 +0100511 if (init_comp_ctx(comp_ctx) < 0)
512 return -1;
513
514 strm = &(*comp_ctx)->strm;
William Lallemand82fe75c2012-10-23 10:25:10 +0200515
Willy Tarreauc5599e72013-04-28 08:52:52 +0200516 if (deflateInit2(strm, level, Z_DEFLATED, global.tune.zlibwindowsize, global.tune.zlibmemlevel, Z_DEFAULT_STRATEGY) != Z_OK) {
William Lallemand8b52bb32012-11-16 18:06:41 +0100517 deinit_comp_ctx(comp_ctx);
William Lallemand82fe75c2012-10-23 10:25:10 +0200518 return -1;
William Lallemand8b52bb32012-11-16 18:06:41 +0100519 }
520
521 (*comp_ctx)->cur_lvl = level;
William Lallemand82fe75c2012-10-23 10:25:10 +0200522
523 return 0;
524}
525
William Lallemandbf3ae612012-11-19 12:35:37 +0100526/* Return the size of consumed data or -1 */
527int 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 +0200528{
William Lallemand82fe75c2012-10-23 10:25:10 +0200529 int ret;
William Lallemandbf3ae612012-11-19 12:35:37 +0100530 z_stream *strm = &comp_ctx->strm;
531 char *out_data = bi_end(out);
532 int out_len = out->size - buffer_len(out);
William Lallemand82fe75c2012-10-23 10:25:10 +0200533
534 if (in_len <= 0)
535 return 0;
536
537
538 if (out_len <= 0)
539 return -1;
540
William Lallemand82fe75c2012-10-23 10:25:10 +0200541 strm->next_in = (unsigned char *)in_data;
542 strm->avail_in = in_len;
543 strm->next_out = (unsigned char *)out_data;
544 strm->avail_out = out_len;
545
546 ret = deflate(strm, Z_NO_FLUSH);
547 if (ret != Z_OK)
548 return -1;
549
550 /* deflate update the available data out */
William Lallemandbf3ae612012-11-19 12:35:37 +0100551 out->i += out_len - strm->avail_out;
William Lallemand82fe75c2012-10-23 10:25:10 +0200552
William Lallemandbf3ae612012-11-19 12:35:37 +0100553 return in_len - strm->avail_in;
William Lallemand82fe75c2012-10-23 10:25:10 +0200554}
555
William Lallemand1c2d6222012-10-30 15:52:53 +0100556int deflate_flush(struct comp_ctx *comp_ctx, struct buffer *out, int flag)
William Lallemand82fe75c2012-10-23 10:25:10 +0200557{
558 int ret;
William Lallemand82fe75c2012-10-23 10:25:10 +0200559 int out_len = 0;
William Lallemand1c2d6222012-10-30 15:52:53 +0100560 z_stream *strm = &comp_ctx->strm;
William Lallemand82fe75c2012-10-23 10:25:10 +0200561
William Lallemand82fe75c2012-10-23 10:25:10 +0200562 strm->next_out = (unsigned char *)bi_end(out);
563 strm->avail_out = out->size - buffer_len(out);
564
565 ret = deflate(strm, flag);
566 if (ret != Z_OK && ret != Z_STREAM_END)
567 return -1;
568
569 out_len = (out->size - buffer_len(out)) - strm->avail_out;
570 out->i += out_len;
571
William Lallemand072a2bf2012-11-20 17:01:01 +0100572 /* compression limit */
573 if ((global.comp_rate_lim > 0 && (read_freq_ctr(&global.comp_bps_out) > global.comp_rate_lim)) || /* rate */
574 (idle_pct < compress_min_idle)) { /* idle */
575 /* decrease level */
576 if (comp_ctx->cur_lvl > 0) {
577 comp_ctx->cur_lvl--;
William Lallemandd85f9172012-11-09 17:05:39 +0100578 deflateParams(&comp_ctx->strm, comp_ctx->cur_lvl, Z_DEFAULT_STRATEGY);
579 }
William Lallemand072a2bf2012-11-20 17:01:01 +0100580
581 } else if (comp_ctx->cur_lvl < global.tune.comp_maxlevel) {
582 /* increase level */
583 comp_ctx->cur_lvl++ ;
584 deflateParams(&comp_ctx->strm, comp_ctx->cur_lvl, Z_DEFAULT_STRATEGY);
William Lallemandd85f9172012-11-09 17:05:39 +0100585 }
586
William Lallemand82fe75c2012-10-23 10:25:10 +0200587 return out_len;
588}
589
William Lallemand1c2d6222012-10-30 15:52:53 +0100590int deflate_reset(struct comp_ctx *comp_ctx)
William Lallemand82fe75c2012-10-23 10:25:10 +0200591{
William Lallemand1c2d6222012-10-30 15:52:53 +0100592 z_stream *strm = &comp_ctx->strm;
William Lallemand82fe75c2012-10-23 10:25:10 +0200593
William Lallemand82fe75c2012-10-23 10:25:10 +0200594 if (deflateReset(strm) == Z_OK)
595 return 0;
596 return -1;
597}
598
William Lallemand8b52bb32012-11-16 18:06:41 +0100599int deflate_end(struct comp_ctx **comp_ctx)
William Lallemand82fe75c2012-10-23 10:25:10 +0200600{
William Lallemand8b52bb32012-11-16 18:06:41 +0100601 z_stream *strm = &(*comp_ctx)->strm;
602 int ret;
William Lallemand82fe75c2012-10-23 10:25:10 +0200603
William Lallemand8b52bb32012-11-16 18:06:41 +0100604 ret = deflateEnd(strm);
William Lallemand82fe75c2012-10-23 10:25:10 +0200605
William Lallemand8b52bb32012-11-16 18:06:41 +0100606 deinit_comp_ctx(comp_ctx);
607
608 return ret;
William Lallemand82fe75c2012-10-23 10:25:10 +0200609}
610
611#endif /* USE_ZLIB */
612
William Lallemand727db8b2013-04-20 17:33:20 +0200613/* boolean, returns true if compression is used (either gzip or deflate) in the response */
614static int
615smp_fetch_res_comp(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +0200616 const struct arg *args, struct sample *smp, const char *kw)
William Lallemand727db8b2013-04-20 17:33:20 +0200617{
618 smp->type = SMP_T_BOOL;
619 smp->data.uint = (l4->comp_algo != NULL);
620 return 1;
621}
622
623/* string, returns algo */
624static int
625smp_fetch_res_comp_algo(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +0200626 const struct arg *args, struct sample *smp, const char *kw)
William Lallemand727db8b2013-04-20 17:33:20 +0200627{
628 if (!l4->comp_algo)
629 return 0;
630
Willy Tarreau4a4e6bc2014-03-11 16:23:05 +0100631 smp->type = SMP_T_CSTR;
William Lallemand727db8b2013-04-20 17:33:20 +0200632 smp->data.str.str = l4->comp_algo->name;
633 smp->data.str.len = l4->comp_algo->name_len;
634 return 1;
635}
636
637/* Note: must not be declared <const> as its list will be overwritten */
Willy Tarreaudc13c112013-06-21 23:16:39 +0200638static struct acl_kw_list acl_kws = {ILH, {
Willy Tarreau7f6fa692013-04-23 19:39:43 +0200639 { /* END */ },
William Lallemand727db8b2013-04-20 17:33:20 +0200640}};
641
642/* Note: must not be declared <const> as its list will be overwritten */
Willy Tarreaudc13c112013-06-21 23:16:39 +0200643static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
William Lallemand727db8b2013-04-20 17:33:20 +0200644 { "res.comp", smp_fetch_res_comp, 0, NULL, SMP_T_BOOL, SMP_USE_HRSHP },
645 { "res.comp_algo", smp_fetch_res_comp_algo, 0, NULL, SMP_T_STR, SMP_USE_HRSHP },
646 { /* END */ },
647}};
648
649__attribute__((constructor))
650static void __comp_fetch_init(void)
651{
652 acl_register_keywords(&acl_kws);
653 sample_register_fetches(&sample_fetch_keywords);
654}