blob: d55f14e7244224e8d5fd6d21fcb0f76fb9913ddd [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
Willy Tarreau9b9531d2015-03-28 12:20:33 +0100133 /* output stream requires at least 10 bytes for the gzip header, plus
134 * at least 8 bytes for the gzip trailer (crc+len), plus a possible
135 * plus at most 5 bytes per 32kB block and 2 bytes to close the stream.
136 */
137 if (in->size - buffer_len(in) < 20 + 5 * ((in->i + 32767) >> 15))
138 return -1;
William Lallemand82fe75c2012-10-23 10:25:10 +0200139
Willy Tarreau7f2f8d52014-04-18 00:20:14 +0200140 /* We start by copying the current buffer's pending outgoing data into
141 * a new temporary buffer that we initialize with a new empty chunk.
William Lallemand82fe75c2012-10-23 10:25:10 +0200142 */
William Lallemand82fe75c2012-10-23 10:25:10 +0200143
144 out->size = global.tune.bufsize;
145 out->i = 0;
146 out->o = 0;
147 out->p = out->data;
Willy Tarreau7f2f8d52014-04-18 00:20:14 +0200148
William Lallemand82fe75c2012-10-23 10:25:10 +0200149 if (in->o > 0) {
150 left = in->o - bo_contig_data(in);
151 memcpy(out->data, bo_ptr(in), bo_contig_data(in));
152 out->p += bo_contig_data(in);
153 if (left > 0) { /* second part of the buffer */
154 memcpy(out->p, in->data, left);
155 out->p += left;
156 }
157 out->o = in->o;
158 }
159 out->i += http_emit_chunk_size(out->p, 0, 0);
160
161 return 0;
162}
163
164/*
165 * Add data to compress
166 */
167int http_compression_buffer_add_data(struct session *s, struct buffer *in, struct buffer *out)
168{
169 struct http_msg *msg = &s->txn.rsp;
William Lallemandbf3ae612012-11-19 12:35:37 +0100170 int consumed_data = 0;
William Lallemand82fe75c2012-10-23 10:25:10 +0200171 int data_process_len;
Willy Tarreau7f2f8d52014-04-18 00:20:14 +0200172 int block1, block2;
William Lallemand82fe75c2012-10-23 10:25:10 +0200173
174 /*
Willy Tarreau7f2f8d52014-04-18 00:20:14 +0200175 * Temporarily skip already parsed data and chunks to jump to the
176 * actual data block. It is fixed before leaving.
William Lallemand82fe75c2012-10-23 10:25:10 +0200177 */
178 b_adv(in, msg->next);
William Lallemand82fe75c2012-10-23 10:25:10 +0200179
180 /*
181 * select the smallest size between the announced chunk size, the input
Willy Tarreau7f2f8d52014-04-18 00:20:14 +0200182 * data, and the available output buffer size. The compressors are
183 * assumed to be able to process all the bytes we pass to them at once.
William Lallemand82fe75c2012-10-23 10:25:10 +0200184 */
185 data_process_len = MIN(in->i, msg->chunk_len);
186 data_process_len = MIN(out->size - buffer_len(out), data_process_len);
187
Willy Tarreau7f2f8d52014-04-18 00:20:14 +0200188 block1 = data_process_len;
189 if (block1 > bi_contig_data(in))
190 block1 = bi_contig_data(in);
191 block2 = data_process_len - block1;
William Lallemand82fe75c2012-10-23 10:25:10 +0200192
Willy Tarreau7f2f8d52014-04-18 00:20:14 +0200193 /* compressors return < 0 upon error or the amount of bytes read */
194 consumed_data = s->comp_algo->add_data(s->comp_ctx, bi_ptr(in), block1, out);
195 if (consumed_data >= 0 && block2 > 0) {
196 consumed_data = s->comp_algo->add_data(s->comp_ctx, in->data, block2, out);
197 if (consumed_data >= 0)
198 consumed_data += block1;
William Lallemand82fe75c2012-10-23 10:25:10 +0200199 }
200
Willy Tarreau7f2f8d52014-04-18 00:20:14 +0200201 /* restore original buffer pointer */
202 b_rew(in, msg->next);
William Lallemand82fe75c2012-10-23 10:25:10 +0200203
Willy Tarreau7f2f8d52014-04-18 00:20:14 +0200204 if (consumed_data > 0) {
205 msg->next += consumed_data;
206 msg->chunk_len -= consumed_data;
207 }
William Lallemandbf3ae612012-11-19 12:35:37 +0100208 return consumed_data;
William Lallemand82fe75c2012-10-23 10:25:10 +0200209}
210
211/*
212 * Flush data in process, and write the header and footer of the chunk. Upon
213 * success, in and out buffers are swapped to avoid a copy.
214 */
215int http_compression_buffer_end(struct session *s, struct buffer **in, struct buffer **out, int end)
216{
Willy Tarreau3ca54482014-04-23 19:31:17 +0200217 int to_forward;
William Lallemand82fe75c2012-10-23 10:25:10 +0200218 int left;
219 struct http_msg *msg = &s->txn.rsp;
220 struct buffer *ib = *in, *ob = *out;
William Lallemand08289f12012-10-31 11:19:18 +0100221
222#ifdef USE_ZLIB
William Lallemand82fe75c2012-10-23 10:25:10 +0200223 int ret;
224
225 /* flush data here */
226
227 if (end)
William Lallemand8b52bb32012-11-16 18:06:41 +0100228 ret = s->comp_algo->flush(s->comp_ctx, ob, Z_FINISH); /* end of data */
William Lallemand82fe75c2012-10-23 10:25:10 +0200229 else
William Lallemand8b52bb32012-11-16 18:06:41 +0100230 ret = s->comp_algo->flush(s->comp_ctx, ob, Z_SYNC_FLUSH); /* end of buffer */
William Lallemand82fe75c2012-10-23 10:25:10 +0200231
232 if (ret < 0)
233 return -1; /* flush failed */
234
William Lallemand08289f12012-10-31 11:19:18 +0100235#endif /* USE_ZLIB */
236
William Lallemand82fe75c2012-10-23 10:25:10 +0200237 if (ob->i > 8) {
238 /* more than a chunk size => some data were emitted */
239 char *tail = ob->p + ob->i;
240
241 /* write real size at the begining of the chunk, no need of wrapping */
242 http_emit_chunk_size(ob->p, ob->i - 8, 0);
243
244 /* chunked encoding requires CRLF after data */
245 *tail++ = '\r';
246 *tail++ = '\n';
247
Willy Tarreau7f2f8d52014-04-18 00:20:14 +0200248 /* At the end of data, we must write the empty chunk 0<CRLF>,
249 * and terminate the trailers section with a last <CRLF>. If
250 * we're forwarding a chunked-encoded response, we'll have a
251 * trailers section after the empty chunk which needs to be
252 * forwarded and which will provide the last CRLF. Otherwise
253 * we write it ourselves.
254 */
255 if (msg->msg_state >= HTTP_MSG_TRAILERS) {
256 memcpy(tail, "0\r\n", 3);
257 tail += 3;
258 if (msg->msg_state >= HTTP_MSG_DONE) {
259 memcpy(tail, "\r\n", 2);
260 tail += 2;
261 }
William Lallemand82fe75c2012-10-23 10:25:10 +0200262 }
263 ob->i = tail - ob->p;
264 } else {
265 /* no data were sent, cancel the chunk size */
266 ob->i = 0;
267 }
268
269 to_forward = ob->i;
Willy Tarreau55058a72012-11-21 08:27:21 +0100270
William Lallemandd85f9172012-11-09 17:05:39 +0100271 /* update input rate */
Willy Tarreau55058a72012-11-21 08:27:21 +0100272 if (s->comp_ctx && s->comp_ctx->cur_lvl > 0) {
Willy Tarreau3ca54482014-04-23 19:31:17 +0200273 update_freq_ctr(&global.comp_bps_in, msg->next);
274 s->fe->fe_counters.comp_in += msg->next;
275 s->be->be_counters.comp_in += msg->next;
Willy Tarreau55058a72012-11-21 08:27:21 +0100276 } else {
Willy Tarreau3ca54482014-04-23 19:31:17 +0200277 s->fe->fe_counters.comp_byp += msg->next;
278 s->be->be_counters.comp_byp += msg->next;
Willy Tarreau55058a72012-11-21 08:27:21 +0100279 }
William Lallemandd85f9172012-11-09 17:05:39 +0100280
William Lallemand82fe75c2012-10-23 10:25:10 +0200281 /* copy the remaining data in the tmp buffer. */
Willy Tarreau7f2f8d52014-04-18 00:20:14 +0200282 b_adv(ib, msg->next);
283 msg->next = 0;
284
William Lallemand82fe75c2012-10-23 10:25:10 +0200285 if (ib->i > 0) {
286 left = ib->i - bi_contig_data(ib);
287 memcpy(bi_end(ob), bi_ptr(ib), bi_contig_data(ib));
288 ob->i += bi_contig_data(ib);
289 if (left > 0) {
290 memcpy(bi_end(ob), ib->data, left);
291 ob->i += left;
292 }
293 }
294
295 /* swap the buffers */
296 *in = ob;
297 *out = ib;
298
Willy Tarreau55058a72012-11-21 08:27:21 +0100299 if (s->comp_ctx && s->comp_ctx->cur_lvl > 0) {
William Lallemandd85f9172012-11-09 17:05:39 +0100300 update_freq_ctr(&global.comp_bps_out, to_forward);
Willy Tarreau55058a72012-11-21 08:27:21 +0100301 s->fe->fe_counters.comp_out += to_forward;
302 s->be->be_counters.comp_out += to_forward;
303 }
William Lallemandd85f9172012-11-09 17:05:39 +0100304
William Lallemand82fe75c2012-10-23 10:25:10 +0200305 /* forward the new chunk without remaining data */
306 b_adv(ob, to_forward);
307
William Lallemand82fe75c2012-10-23 10:25:10 +0200308 return to_forward;
309}
310
William Lallemand8b52bb32012-11-16 18:06:41 +0100311/*
312 * Alloc the comp_ctx
313 */
314static inline int init_comp_ctx(struct comp_ctx **comp_ctx)
315{
316#ifdef USE_ZLIB
317 z_stream *strm;
318
William Lallemande3a7d992012-11-20 11:25:20 +0100319 if (global.maxzlibmem > 0 && (global.maxzlibmem - zlib_used_memory) < sizeof(struct comp_ctx))
William Lallemand8b52bb32012-11-16 18:06:41 +0100320 return -1;
321#endif
322
323 if (unlikely(pool_comp_ctx == NULL))
324 pool_comp_ctx = create_pool("comp_ctx", sizeof(struct comp_ctx), MEM_F_SHARED);
325
326 *comp_ctx = pool_alloc2(pool_comp_ctx);
327 if (*comp_ctx == NULL)
328 return -1;
329#ifdef USE_ZLIB
William Lallemande3a7d992012-11-20 11:25:20 +0100330 zlib_used_memory += sizeof(struct comp_ctx);
William Lallemand8b52bb32012-11-16 18:06:41 +0100331
332 strm = &(*comp_ctx)->strm;
333 strm->zalloc = alloc_zlib;
334 strm->zfree = free_zlib;
335 strm->opaque = *comp_ctx;
336#endif
337 return 0;
338}
339
340/*
341 * Dealloc the comp_ctx
342 */
343static inline int deinit_comp_ctx(struct comp_ctx **comp_ctx)
344{
345 if (!*comp_ctx)
346 return 0;
347
348 pool_free2(pool_comp_ctx, *comp_ctx);
349 *comp_ctx = NULL;
350
351#ifdef USE_ZLIB
William Lallemande3a7d992012-11-20 11:25:20 +0100352 zlib_used_memory -= sizeof(struct comp_ctx);
William Lallemand8b52bb32012-11-16 18:06:41 +0100353#endif
William Lallemand8b52bb32012-11-16 18:06:41 +0100354 return 0;
355}
356
William Lallemand82fe75c2012-10-23 10:25:10 +0200357
358/****************************
359 **** Identity algorithm ****
360 ****************************/
361
362/*
363 * Init the identity algorithm
364 */
William Lallemand8b52bb32012-11-16 18:06:41 +0100365int identity_init(struct comp_ctx **comp_ctx, int level)
William Lallemand82fe75c2012-10-23 10:25:10 +0200366{
367 return 0;
368}
369
370/*
371 * Process data
William Lallemandbf3ae612012-11-19 12:35:37 +0100372 * Return size of consumed data or -1 on error
William Lallemand82fe75c2012-10-23 10:25:10 +0200373 */
William Lallemandbf3ae612012-11-19 12:35:37 +0100374int 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 +0200375{
William Lallemandbf3ae612012-11-19 12:35:37 +0100376 char *out_data = bi_end(out);
377 int out_len = out->size - buffer_len(out);
378
William Lallemand82fe75c2012-10-23 10:25:10 +0200379 if (out_len < in_len)
380 return -1;
381
382 memcpy(out_data, in_data, in_len);
383
William Lallemandbf3ae612012-11-19 12:35:37 +0100384 out->i += in_len;
385
William Lallemand82fe75c2012-10-23 10:25:10 +0200386 return in_len;
387}
388
William Lallemand1c2d6222012-10-30 15:52:53 +0100389int identity_flush(struct comp_ctx *comp_ctx, struct buffer *out, int flag)
William Lallemand82fe75c2012-10-23 10:25:10 +0200390{
391 return 0;
392}
393
William Lallemand1c2d6222012-10-30 15:52:53 +0100394int identity_reset(struct comp_ctx *comp_ctx)
William Lallemand82fe75c2012-10-23 10:25:10 +0200395{
396 return 0;
397}
398
399/*
400 * Deinit the algorithm
401 */
William Lallemand8b52bb32012-11-16 18:06:41 +0100402int identity_end(struct comp_ctx **comp_ctx)
William Lallemand82fe75c2012-10-23 10:25:10 +0200403{
404 return 0;
405}
406
407
408#ifdef USE_ZLIB
William Lallemand2b502472012-10-30 14:30:39 +0100409/*
410 * This is a tricky allocation function using the zlib.
411 * This is based on the allocation order in deflateInit2.
412 */
413static void *alloc_zlib(void *opaque, unsigned int items, unsigned int size)
414{
415 struct comp_ctx *ctx = opaque;
416 static char round = 0; /* order in deflateInit2 */
417 void *buf = NULL;
Willy Tarreaub238b272014-12-24 18:07:55 +0100418 struct pool_head *pool = NULL;
William Lallemand2b502472012-10-30 14:30:39 +0100419
William Lallemande3a7d992012-11-20 11:25:20 +0100420 if (global.maxzlibmem > 0 && (global.maxzlibmem - zlib_used_memory) < (long)(items * size))
William Lallemand9d5f5482012-11-07 16:12:57 +0100421 goto end;
William Lallemand9d5f5482012-11-07 16:12:57 +0100422
William Lallemand2b502472012-10-30 14:30:39 +0100423 switch (round) {
424 case 0:
425 if (zlib_pool_deflate_state == NULL)
426 zlib_pool_deflate_state = create_pool("zlib_state", size * items, MEM_F_SHARED);
Willy Tarreaub238b272014-12-24 18:07:55 +0100427 pool = zlib_pool_deflate_state;
428 ctx->zlib_deflate_state = buf = pool_alloc2(pool);
William Lallemand2b502472012-10-30 14:30:39 +0100429 break;
430
431 case 1:
432 if (zlib_pool_window == NULL)
433 zlib_pool_window = create_pool("zlib_window", size * items, MEM_F_SHARED);
Willy Tarreaub238b272014-12-24 18:07:55 +0100434 pool = zlib_pool_window;
435 ctx->zlib_window = buf = pool_alloc2(pool);
William Lallemand2b502472012-10-30 14:30:39 +0100436 break;
437
438 case 2:
439 if (zlib_pool_prev == NULL)
440 zlib_pool_prev = create_pool("zlib_prev", size * items, MEM_F_SHARED);
Willy Tarreaub238b272014-12-24 18:07:55 +0100441 pool = zlib_pool_prev;
442 ctx->zlib_prev = buf = pool_alloc2(pool);
William Lallemand2b502472012-10-30 14:30:39 +0100443 break;
444
445 case 3:
446 if (zlib_pool_head == NULL)
447 zlib_pool_head = create_pool("zlib_head", size * items, MEM_F_SHARED);
Willy Tarreaub238b272014-12-24 18:07:55 +0100448 pool = zlib_pool_head;
449 ctx->zlib_head = buf = pool_alloc2(pool);
William Lallemand2b502472012-10-30 14:30:39 +0100450 break;
451
452 case 4:
453 if (zlib_pool_pending_buf == NULL)
454 zlib_pool_pending_buf = create_pool("zlib_pending_buf", size * items, MEM_F_SHARED);
Willy Tarreaub238b272014-12-24 18:07:55 +0100455 pool = zlib_pool_pending_buf;
456 ctx->zlib_pending_buf = buf = pool_alloc2(pool);
William Lallemand2b502472012-10-30 14:30:39 +0100457 break;
458 }
William Lallemande3a7d992012-11-20 11:25:20 +0100459 if (buf != NULL)
Willy Tarreaub238b272014-12-24 18:07:55 +0100460 zlib_used_memory += pool->size;
William Lallemand9d5f5482012-11-07 16:12:57 +0100461
462end:
William Lallemand2b502472012-10-30 14:30:39 +0100463
Willy Tarreau46909852012-11-15 14:57:56 +0100464 /* deflateInit2() first allocates and checks the deflate_state, then if
465 * it succeeds, it allocates all other 4 areas at ones and checks them
466 * at the end. So we want to correctly count the rounds depending on when
467 * zlib is supposed to abort.
468 */
469 if (buf || round)
470 round = (round + 1) % 5;
William Lallemand2b502472012-10-30 14:30:39 +0100471 return buf;
472}
473
474static void free_zlib(void *opaque, void *ptr)
475{
476 struct comp_ctx *ctx = opaque;
Willy Tarreaub1fbd052012-11-10 17:49:37 +0100477 struct pool_head *pool = NULL;
William Lallemand2b502472012-10-30 14:30:39 +0100478
479 if (ptr == ctx->zlib_window)
William Lallemand9d5f5482012-11-07 16:12:57 +0100480 pool = zlib_pool_window;
William Lallemand2b502472012-10-30 14:30:39 +0100481 else if (ptr == ctx->zlib_deflate_state)
William Lallemand9d5f5482012-11-07 16:12:57 +0100482 pool = zlib_pool_deflate_state;
William Lallemand2b502472012-10-30 14:30:39 +0100483 else if (ptr == ctx->zlib_prev)
William Lallemand9d5f5482012-11-07 16:12:57 +0100484 pool = zlib_pool_prev;
William Lallemand2b502472012-10-30 14:30:39 +0100485 else if (ptr == ctx->zlib_head)
William Lallemand9d5f5482012-11-07 16:12:57 +0100486 pool = zlib_pool_head;
William Lallemand2b502472012-10-30 14:30:39 +0100487 else if (ptr == ctx->zlib_pending_buf)
William Lallemand9d5f5482012-11-07 16:12:57 +0100488 pool = zlib_pool_pending_buf;
William Lallemand2b502472012-10-30 14:30:39 +0100489
William Lallemand9d5f5482012-11-07 16:12:57 +0100490 pool_free2(pool, ptr);
William Lallemande3a7d992012-11-20 11:25:20 +0100491 zlib_used_memory -= pool->size;
William Lallemand2b502472012-10-30 14:30:39 +0100492}
493
William Lallemand82fe75c2012-10-23 10:25:10 +0200494/**************************
495**** gzip algorithm ****
496***************************/
William Lallemand8b52bb32012-11-16 18:06:41 +0100497int gzip_init(struct comp_ctx **comp_ctx, int level)
William Lallemand82fe75c2012-10-23 10:25:10 +0200498{
William Lallemand8b52bb32012-11-16 18:06:41 +0100499 z_stream *strm;
William Lallemand82fe75c2012-10-23 10:25:10 +0200500
William Lallemand8b52bb32012-11-16 18:06:41 +0100501 if (init_comp_ctx(comp_ctx) < 0)
502 return -1;
William Lallemand9d5f5482012-11-07 16:12:57 +0100503
William Lallemand8b52bb32012-11-16 18:06:41 +0100504 strm = &(*comp_ctx)->strm;
William Lallemand82fe75c2012-10-23 10:25:10 +0200505
William Lallemand8b52bb32012-11-16 18:06:41 +0100506 if (deflateInit2(strm, level, Z_DEFLATED, global.tune.zlibwindowsize + 16, global.tune.zlibmemlevel, Z_DEFAULT_STRATEGY) != Z_OK) {
507 deinit_comp_ctx(comp_ctx);
William Lallemand82fe75c2012-10-23 10:25:10 +0200508 return -1;
William Lallemand8b52bb32012-11-16 18:06:41 +0100509 }
510
511 (*comp_ctx)->cur_lvl = level;
William Lallemand82fe75c2012-10-23 10:25:10 +0200512
513 return 0;
514}
515/**************************
516**** Deflate algorithm ****
517***************************/
518
William Lallemand8b52bb32012-11-16 18:06:41 +0100519int deflate_init(struct comp_ctx **comp_ctx, int level)
William Lallemand82fe75c2012-10-23 10:25:10 +0200520{
William Lallemand8b52bb32012-11-16 18:06:41 +0100521 z_stream *strm;
William Lallemand82fe75c2012-10-23 10:25:10 +0200522
William Lallemand8b52bb32012-11-16 18:06:41 +0100523 if (init_comp_ctx(comp_ctx) < 0)
524 return -1;
525
526 strm = &(*comp_ctx)->strm;
William Lallemand82fe75c2012-10-23 10:25:10 +0200527
Willy Tarreauc5599e72013-04-28 08:52:52 +0200528 if (deflateInit2(strm, level, Z_DEFLATED, global.tune.zlibwindowsize, global.tune.zlibmemlevel, Z_DEFAULT_STRATEGY) != Z_OK) {
William Lallemand8b52bb32012-11-16 18:06:41 +0100529 deinit_comp_ctx(comp_ctx);
William Lallemand82fe75c2012-10-23 10:25:10 +0200530 return -1;
William Lallemand8b52bb32012-11-16 18:06:41 +0100531 }
532
533 (*comp_ctx)->cur_lvl = level;
William Lallemand82fe75c2012-10-23 10:25:10 +0200534
535 return 0;
536}
537
William Lallemandbf3ae612012-11-19 12:35:37 +0100538/* Return the size of consumed data or -1 */
539int 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 +0200540{
William Lallemand82fe75c2012-10-23 10:25:10 +0200541 int ret;
William Lallemandbf3ae612012-11-19 12:35:37 +0100542 z_stream *strm = &comp_ctx->strm;
543 char *out_data = bi_end(out);
544 int out_len = out->size - buffer_len(out);
William Lallemand82fe75c2012-10-23 10:25:10 +0200545
546 if (in_len <= 0)
547 return 0;
548
549
550 if (out_len <= 0)
551 return -1;
552
William Lallemand82fe75c2012-10-23 10:25:10 +0200553 strm->next_in = (unsigned char *)in_data;
554 strm->avail_in = in_len;
555 strm->next_out = (unsigned char *)out_data;
556 strm->avail_out = out_len;
557
558 ret = deflate(strm, Z_NO_FLUSH);
559 if (ret != Z_OK)
560 return -1;
561
562 /* deflate update the available data out */
William Lallemandbf3ae612012-11-19 12:35:37 +0100563 out->i += out_len - strm->avail_out;
William Lallemand82fe75c2012-10-23 10:25:10 +0200564
William Lallemandbf3ae612012-11-19 12:35:37 +0100565 return in_len - strm->avail_in;
William Lallemand82fe75c2012-10-23 10:25:10 +0200566}
567
William Lallemand1c2d6222012-10-30 15:52:53 +0100568int deflate_flush(struct comp_ctx *comp_ctx, struct buffer *out, int flag)
William Lallemand82fe75c2012-10-23 10:25:10 +0200569{
570 int ret;
William Lallemand82fe75c2012-10-23 10:25:10 +0200571 int out_len = 0;
William Lallemand1c2d6222012-10-30 15:52:53 +0100572 z_stream *strm = &comp_ctx->strm;
William Lallemand82fe75c2012-10-23 10:25:10 +0200573
William Lallemand82fe75c2012-10-23 10:25:10 +0200574 strm->next_out = (unsigned char *)bi_end(out);
575 strm->avail_out = out->size - buffer_len(out);
576
577 ret = deflate(strm, flag);
578 if (ret != Z_OK && ret != Z_STREAM_END)
579 return -1;
580
581 out_len = (out->size - buffer_len(out)) - strm->avail_out;
582 out->i += out_len;
583
William Lallemand072a2bf2012-11-20 17:01:01 +0100584 /* compression limit */
585 if ((global.comp_rate_lim > 0 && (read_freq_ctr(&global.comp_bps_out) > global.comp_rate_lim)) || /* rate */
586 (idle_pct < compress_min_idle)) { /* idle */
587 /* decrease level */
588 if (comp_ctx->cur_lvl > 0) {
589 comp_ctx->cur_lvl--;
William Lallemandd85f9172012-11-09 17:05:39 +0100590 deflateParams(&comp_ctx->strm, comp_ctx->cur_lvl, Z_DEFAULT_STRATEGY);
591 }
William Lallemand072a2bf2012-11-20 17:01:01 +0100592
593 } else if (comp_ctx->cur_lvl < global.tune.comp_maxlevel) {
594 /* increase level */
595 comp_ctx->cur_lvl++ ;
596 deflateParams(&comp_ctx->strm, comp_ctx->cur_lvl, Z_DEFAULT_STRATEGY);
William Lallemandd85f9172012-11-09 17:05:39 +0100597 }
598
William Lallemand82fe75c2012-10-23 10:25:10 +0200599 return out_len;
600}
601
William Lallemand1c2d6222012-10-30 15:52:53 +0100602int deflate_reset(struct comp_ctx *comp_ctx)
William Lallemand82fe75c2012-10-23 10:25:10 +0200603{
William Lallemand1c2d6222012-10-30 15:52:53 +0100604 z_stream *strm = &comp_ctx->strm;
William Lallemand82fe75c2012-10-23 10:25:10 +0200605
William Lallemand82fe75c2012-10-23 10:25:10 +0200606 if (deflateReset(strm) == Z_OK)
607 return 0;
608 return -1;
609}
610
William Lallemand8b52bb32012-11-16 18:06:41 +0100611int deflate_end(struct comp_ctx **comp_ctx)
William Lallemand82fe75c2012-10-23 10:25:10 +0200612{
William Lallemand8b52bb32012-11-16 18:06:41 +0100613 z_stream *strm = &(*comp_ctx)->strm;
614 int ret;
William Lallemand82fe75c2012-10-23 10:25:10 +0200615
William Lallemand8b52bb32012-11-16 18:06:41 +0100616 ret = deflateEnd(strm);
William Lallemand82fe75c2012-10-23 10:25:10 +0200617
William Lallemand8b52bb32012-11-16 18:06:41 +0100618 deinit_comp_ctx(comp_ctx);
619
620 return ret;
William Lallemand82fe75c2012-10-23 10:25:10 +0200621}
622
623#endif /* USE_ZLIB */
624
William Lallemand727db8b2013-04-20 17:33:20 +0200625/* boolean, returns true if compression is used (either gzip or deflate) in the response */
626static int
627smp_fetch_res_comp(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +0200628 const struct arg *args, struct sample *smp, const char *kw)
William Lallemand727db8b2013-04-20 17:33:20 +0200629{
630 smp->type = SMP_T_BOOL;
631 smp->data.uint = (l4->comp_algo != NULL);
632 return 1;
633}
634
635/* string, returns algo */
636static int
637smp_fetch_res_comp_algo(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +0200638 const struct arg *args, struct sample *smp, const char *kw)
William Lallemand727db8b2013-04-20 17:33:20 +0200639{
640 if (!l4->comp_algo)
641 return 0;
642
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100643 smp->type = SMP_T_STR;
644 smp->flags = SMP_F_CONST;
William Lallemand727db8b2013-04-20 17:33:20 +0200645 smp->data.str.str = l4->comp_algo->name;
646 smp->data.str.len = l4->comp_algo->name_len;
647 return 1;
648}
649
650/* Note: must not be declared <const> as its list will be overwritten */
Willy Tarreaudc13c112013-06-21 23:16:39 +0200651static struct acl_kw_list acl_kws = {ILH, {
Willy Tarreau7f6fa692013-04-23 19:39:43 +0200652 { /* END */ },
William Lallemand727db8b2013-04-20 17:33:20 +0200653}};
654
655/* Note: must not be declared <const> as its list will be overwritten */
Willy Tarreaudc13c112013-06-21 23:16:39 +0200656static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
William Lallemand727db8b2013-04-20 17:33:20 +0200657 { "res.comp", smp_fetch_res_comp, 0, NULL, SMP_T_BOOL, SMP_USE_HRSHP },
658 { "res.comp_algo", smp_fetch_res_comp_algo, 0, NULL, SMP_T_STR, SMP_USE_HRSHP },
659 { /* END */ },
660}};
661
662__attribute__((constructor))
663static void __comp_fetch_init(void)
664{
665 acl_register_keywords(&acl_kws);
666 sample_register_fetches(&sample_fetch_keywords);
667}