blob: 95272fbbb05e4bcd5e5304fcee8fa29813d2a014 [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
Willy Tarreau15530d22015-03-28 12:05:47 +0100105 * bytes written. It goes backwards and starts with the byte before <end>. It
106 * returns the number of bytes written which will not exceed 10 (8 digits, CR,
107 * and LF). The caller is responsible for ensuring there is enough room left in
108 * the output buffer for the string.
William Lallemand82fe75c2012-10-23 10:25:10 +0200109 */
Willy Tarreau15530d22015-03-28 12:05:47 +0100110int http_emit_chunk_size(char *end, unsigned int chksz)
William Lallemand82fe75c2012-10-23 10:25:10 +0200111{
Willy Tarreau15530d22015-03-28 12:05:47 +0100112 char *beg = end;
William Lallemand82fe75c2012-10-23 10:25:10 +0200113
Willy Tarreau15530d22015-03-28 12:05:47 +0100114 *--beg = '\n';
115 *--beg = '\r';
William Lallemand82fe75c2012-10-23 10:25:10 +0200116 do {
Willy Tarreau15530d22015-03-28 12:05:47 +0100117 *--beg = hextab[chksz & 0xF];
118 } while (chksz >>= 4);
119 return end - beg;
William Lallemand82fe75c2012-10-23 10:25:10 +0200120}
121
122/*
123 * Init HTTP compression
124 */
125int http_compression_buffer_init(struct session *s, struct buffer *in, struct buffer *out)
126{
William Lallemand82fe75c2012-10-23 10:25:10 +0200127 /* not enough space */
Willy Tarreau15530d22015-03-28 12:05:47 +0100128 if (in->size - buffer_len(in) < 42)
Willy Tarreaud328af52015-03-28 11:10:56 +0100129 return -1;
William Lallemand82fe75c2012-10-23 10:25:10 +0200130
Willy Tarreaud328af52015-03-28 11:10:56 +0100131 /* prepare an empty output buffer in which we reserve enough room for
Willy Tarreau15530d22015-03-28 12:05:47 +0100132 * copying the output bytes from <in>, plus 10 extra bytes to write
Willy Tarreaud328af52015-03-28 11:10:56 +0100133 * the chunk size. We don't copy the bytes yet so that if we have to
134 * cancel the operation later, it's cheap.
William Lallemand82fe75c2012-10-23 10:25:10 +0200135 */
Willy Tarreau474cf542014-11-24 10:54:47 +0100136 b_reset(out);
Willy Tarreaud328af52015-03-28 11:10:56 +0100137 out->o = in->o;
138 out->p += out->o;
Willy Tarreau15530d22015-03-28 12:05:47 +0100139 out->i = 10;
William Lallemand82fe75c2012-10-23 10:25:10 +0200140 return 0;
141}
142
143/*
144 * Add data to compress
145 */
146int http_compression_buffer_add_data(struct session *s, struct buffer *in, struct buffer *out)
147{
148 struct http_msg *msg = &s->txn.rsp;
William Lallemandbf3ae612012-11-19 12:35:37 +0100149 int consumed_data = 0;
William Lallemand82fe75c2012-10-23 10:25:10 +0200150 int data_process_len;
Willy Tarreau7f2f8d52014-04-18 00:20:14 +0200151 int block1, block2;
William Lallemand82fe75c2012-10-23 10:25:10 +0200152
153 /*
Willy Tarreau7f2f8d52014-04-18 00:20:14 +0200154 * Temporarily skip already parsed data and chunks to jump to the
155 * actual data block. It is fixed before leaving.
William Lallemand82fe75c2012-10-23 10:25:10 +0200156 */
157 b_adv(in, msg->next);
William Lallemand82fe75c2012-10-23 10:25:10 +0200158
159 /*
160 * select the smallest size between the announced chunk size, the input
Willy Tarreau7f2f8d52014-04-18 00:20:14 +0200161 * data, and the available output buffer size. The compressors are
162 * assumed to be able to process all the bytes we pass to them at once.
William Lallemand82fe75c2012-10-23 10:25:10 +0200163 */
164 data_process_len = MIN(in->i, msg->chunk_len);
165 data_process_len = MIN(out->size - buffer_len(out), data_process_len);
166
Willy Tarreau7f2f8d52014-04-18 00:20:14 +0200167 block1 = data_process_len;
168 if (block1 > bi_contig_data(in))
169 block1 = bi_contig_data(in);
170 block2 = data_process_len - block1;
William Lallemand82fe75c2012-10-23 10:25:10 +0200171
Willy Tarreau7f2f8d52014-04-18 00:20:14 +0200172 /* compressors return < 0 upon error or the amount of bytes read */
173 consumed_data = s->comp_algo->add_data(s->comp_ctx, bi_ptr(in), block1, out);
174 if (consumed_data >= 0 && block2 > 0) {
175 consumed_data = s->comp_algo->add_data(s->comp_ctx, in->data, block2, out);
176 if (consumed_data >= 0)
177 consumed_data += block1;
William Lallemand82fe75c2012-10-23 10:25:10 +0200178 }
179
Willy Tarreau7f2f8d52014-04-18 00:20:14 +0200180 /* restore original buffer pointer */
181 b_rew(in, msg->next);
William Lallemand82fe75c2012-10-23 10:25:10 +0200182
Willy Tarreau7f2f8d52014-04-18 00:20:14 +0200183 if (consumed_data > 0) {
184 msg->next += consumed_data;
185 msg->chunk_len -= consumed_data;
186 }
William Lallemandbf3ae612012-11-19 12:35:37 +0100187 return consumed_data;
William Lallemand82fe75c2012-10-23 10:25:10 +0200188}
189
190/*
191 * Flush data in process, and write the header and footer of the chunk. Upon
192 * success, in and out buffers are swapped to avoid a copy.
193 */
194int http_compression_buffer_end(struct session *s, struct buffer **in, struct buffer **out, int end)
195{
Willy Tarreau3ca54482014-04-23 19:31:17 +0200196 int to_forward;
William Lallemand82fe75c2012-10-23 10:25:10 +0200197 int left;
198 struct http_msg *msg = &s->txn.rsp;
199 struct buffer *ib = *in, *ob = *out;
Willy Tarreaud328af52015-03-28 11:10:56 +0100200 char *tail;
William Lallemand08289f12012-10-31 11:19:18 +0100201
202#ifdef USE_ZLIB
William Lallemand82fe75c2012-10-23 10:25:10 +0200203 int ret;
204
205 /* flush data here */
206
207 if (end)
William Lallemand8b52bb32012-11-16 18:06:41 +0100208 ret = s->comp_algo->flush(s->comp_ctx, ob, Z_FINISH); /* end of data */
William Lallemand82fe75c2012-10-23 10:25:10 +0200209 else
William Lallemand8b52bb32012-11-16 18:06:41 +0100210 ret = s->comp_algo->flush(s->comp_ctx, ob, Z_SYNC_FLUSH); /* end of buffer */
William Lallemand82fe75c2012-10-23 10:25:10 +0200211
212 if (ret < 0)
213 return -1; /* flush failed */
214
William Lallemand08289f12012-10-31 11:19:18 +0100215#endif /* USE_ZLIB */
216
Willy Tarreau15530d22015-03-28 12:05:47 +0100217 if (ob->i == 10) {
Willy Tarreaud328af52015-03-28 11:10:56 +0100218 /* No data were appended, let's drop the output buffer and
219 * keep the input buffer unchanged.
220 */
221 return 0;
222 }
William Lallemand82fe75c2012-10-23 10:25:10 +0200223
Willy Tarreaud328af52015-03-28 11:10:56 +0100224 /* OK so at this stage, we have an output buffer <ob> looking like this :
225 *
226 * <-- o --> <------ i ----->
227 * +---------+---+------------+-----------+
228 * | out | c | comp_in | empty |
229 * +---------+---+------------+-----------+
230 * data p size
231 *
232 * <out> is the room reserved to copy ib->o. It starts at ob->data and
233 * has not yet been filled. <c> is the room reserved to write the chunk
Willy Tarreau15530d22015-03-28 12:05:47 +0100234 * size (10 bytes). <comp_in> is the compressed equivalent of the data
Willy Tarreaud328af52015-03-28 11:10:56 +0100235 * part of ib->i. <empty> is the amount of empty bytes at the end of
236 * the buffer, into which we may have to copy the remaining bytes from
237 * ib->i after the data (chunk size, trailers, ...).
238 */
William Lallemand82fe75c2012-10-23 10:25:10 +0200239
Willy Tarreaud328af52015-03-28 11:10:56 +0100240 /* Write real size at the begining of the chunk, no need of wrapping.
Willy Tarreau15530d22015-03-28 12:05:47 +0100241 * We write the chunk using a dynamic length and adjust ob->p and ob->i
242 * accordingly afterwards. That will move <out> away from <data>.
Willy Tarreaud328af52015-03-28 11:10:56 +0100243 */
Willy Tarreau15530d22015-03-28 12:05:47 +0100244 left = 10 - http_emit_chunk_size(ob->p + 10, ob->i - 10);
245 ob->p += left;
246 ob->i -= left;
William Lallemand82fe75c2012-10-23 10:25:10 +0200247
Willy Tarreaud328af52015-03-28 11:10:56 +0100248 /* Copy previous data from ib->o into ob->o */
249 if (ib->o > 0) {
250 left = bo_contig_data(ib);
251 memcpy(ob->p - ob->o, bo_ptr(ib), left);
252 if (ib->o - left) /* second part of the buffer */
253 memcpy(ob->p - ob->o + left, ib->data, ib->o - left);
254 }
255
256 /* chunked encoding requires CRLF after data */
257 tail = ob->p + ob->i;
258 *tail++ = '\r';
259 *tail++ = '\n';
260
261 /* At the end of data, we must write the empty chunk 0<CRLF>,
262 * and terminate the trailers section with a last <CRLF>. If
263 * we're forwarding a chunked-encoded response, we'll have a
264 * trailers section after the empty chunk which needs to be
265 * forwarded and which will provide the last CRLF. Otherwise
266 * we write it ourselves.
267 */
268 if (msg->msg_state >= HTTP_MSG_TRAILERS) {
269 memcpy(tail, "0\r\n", 3);
270 tail += 3;
271 if (msg->msg_state >= HTTP_MSG_DONE) {
272 memcpy(tail, "\r\n", 2);
273 tail += 2;
William Lallemand82fe75c2012-10-23 10:25:10 +0200274 }
William Lallemand82fe75c2012-10-23 10:25:10 +0200275 }
Willy Tarreaud328af52015-03-28 11:10:56 +0100276 ob->i = tail - ob->p;
William Lallemand82fe75c2012-10-23 10:25:10 +0200277
278 to_forward = ob->i;
Willy Tarreau55058a72012-11-21 08:27:21 +0100279
William Lallemandd85f9172012-11-09 17:05:39 +0100280 /* update input rate */
Willy Tarreau55058a72012-11-21 08:27:21 +0100281 if (s->comp_ctx && s->comp_ctx->cur_lvl > 0) {
Willy Tarreau3ca54482014-04-23 19:31:17 +0200282 update_freq_ctr(&global.comp_bps_in, msg->next);
283 s->fe->fe_counters.comp_in += msg->next;
284 s->be->be_counters.comp_in += msg->next;
Willy Tarreau55058a72012-11-21 08:27:21 +0100285 } else {
Willy Tarreau3ca54482014-04-23 19:31:17 +0200286 s->fe->fe_counters.comp_byp += msg->next;
287 s->be->be_counters.comp_byp += msg->next;
Willy Tarreau55058a72012-11-21 08:27:21 +0100288 }
William Lallemandd85f9172012-11-09 17:05:39 +0100289
William Lallemand82fe75c2012-10-23 10:25:10 +0200290 /* copy the remaining data in the tmp buffer. */
Willy Tarreau7f2f8d52014-04-18 00:20:14 +0200291 b_adv(ib, msg->next);
292 msg->next = 0;
293
William Lallemand82fe75c2012-10-23 10:25:10 +0200294 if (ib->i > 0) {
Willy Tarreaud328af52015-03-28 11:10:56 +0100295 left = bi_contig_data(ib);
296 memcpy(ob->p + ob->i, bi_ptr(ib), left);
297 ob->i += left;
298 if (ib->i - left) {
299 memcpy(ob->p + ob->i, ib->data, ib->i - left);
300 ob->i += ib->i - left;
William Lallemand82fe75c2012-10-23 10:25:10 +0200301 }
302 }
303
304 /* swap the buffers */
305 *in = ob;
306 *out = ib;
307
Willy Tarreau55058a72012-11-21 08:27:21 +0100308 if (s->comp_ctx && s->comp_ctx->cur_lvl > 0) {
William Lallemandd85f9172012-11-09 17:05:39 +0100309 update_freq_ctr(&global.comp_bps_out, to_forward);
Willy Tarreau55058a72012-11-21 08:27:21 +0100310 s->fe->fe_counters.comp_out += to_forward;
311 s->be->be_counters.comp_out += to_forward;
312 }
William Lallemandd85f9172012-11-09 17:05:39 +0100313
William Lallemand82fe75c2012-10-23 10:25:10 +0200314 /* forward the new chunk without remaining data */
315 b_adv(ob, to_forward);
316
William Lallemand82fe75c2012-10-23 10:25:10 +0200317 return to_forward;
318}
319
William Lallemand8b52bb32012-11-16 18:06:41 +0100320/*
321 * Alloc the comp_ctx
322 */
323static inline int init_comp_ctx(struct comp_ctx **comp_ctx)
324{
325#ifdef USE_ZLIB
326 z_stream *strm;
327
William Lallemande3a7d992012-11-20 11:25:20 +0100328 if (global.maxzlibmem > 0 && (global.maxzlibmem - zlib_used_memory) < sizeof(struct comp_ctx))
William Lallemand8b52bb32012-11-16 18:06:41 +0100329 return -1;
330#endif
331
332 if (unlikely(pool_comp_ctx == NULL))
333 pool_comp_ctx = create_pool("comp_ctx", sizeof(struct comp_ctx), MEM_F_SHARED);
334
335 *comp_ctx = pool_alloc2(pool_comp_ctx);
336 if (*comp_ctx == NULL)
337 return -1;
338#ifdef USE_ZLIB
William Lallemande3a7d992012-11-20 11:25:20 +0100339 zlib_used_memory += sizeof(struct comp_ctx);
William Lallemand8b52bb32012-11-16 18:06:41 +0100340
341 strm = &(*comp_ctx)->strm;
342 strm->zalloc = alloc_zlib;
343 strm->zfree = free_zlib;
344 strm->opaque = *comp_ctx;
345#endif
346 return 0;
347}
348
349/*
350 * Dealloc the comp_ctx
351 */
352static inline int deinit_comp_ctx(struct comp_ctx **comp_ctx)
353{
354 if (!*comp_ctx)
355 return 0;
356
357 pool_free2(pool_comp_ctx, *comp_ctx);
358 *comp_ctx = NULL;
359
360#ifdef USE_ZLIB
William Lallemande3a7d992012-11-20 11:25:20 +0100361 zlib_used_memory -= sizeof(struct comp_ctx);
William Lallemand8b52bb32012-11-16 18:06:41 +0100362#endif
William Lallemand8b52bb32012-11-16 18:06:41 +0100363 return 0;
364}
365
William Lallemand82fe75c2012-10-23 10:25:10 +0200366
367/****************************
368 **** Identity algorithm ****
369 ****************************/
370
371/*
372 * Init the identity algorithm
373 */
William Lallemand8b52bb32012-11-16 18:06:41 +0100374int identity_init(struct comp_ctx **comp_ctx, int level)
William Lallemand82fe75c2012-10-23 10:25:10 +0200375{
376 return 0;
377}
378
379/*
380 * Process data
William Lallemandbf3ae612012-11-19 12:35:37 +0100381 * Return size of consumed data or -1 on error
William Lallemand82fe75c2012-10-23 10:25:10 +0200382 */
William Lallemandbf3ae612012-11-19 12:35:37 +0100383int 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 +0200384{
William Lallemandbf3ae612012-11-19 12:35:37 +0100385 char *out_data = bi_end(out);
386 int out_len = out->size - buffer_len(out);
387
William Lallemand82fe75c2012-10-23 10:25:10 +0200388 if (out_len < in_len)
389 return -1;
390
391 memcpy(out_data, in_data, in_len);
392
William Lallemandbf3ae612012-11-19 12:35:37 +0100393 out->i += in_len;
394
William Lallemand82fe75c2012-10-23 10:25:10 +0200395 return in_len;
396}
397
William Lallemand1c2d6222012-10-30 15:52:53 +0100398int identity_flush(struct comp_ctx *comp_ctx, struct buffer *out, int flag)
William Lallemand82fe75c2012-10-23 10:25:10 +0200399{
400 return 0;
401}
402
William Lallemand1c2d6222012-10-30 15:52:53 +0100403int identity_reset(struct comp_ctx *comp_ctx)
William Lallemand82fe75c2012-10-23 10:25:10 +0200404{
405 return 0;
406}
407
408/*
409 * Deinit the algorithm
410 */
William Lallemand8b52bb32012-11-16 18:06:41 +0100411int identity_end(struct comp_ctx **comp_ctx)
William Lallemand82fe75c2012-10-23 10:25:10 +0200412{
413 return 0;
414}
415
416
417#ifdef USE_ZLIB
William Lallemand2b502472012-10-30 14:30:39 +0100418/*
419 * This is a tricky allocation function using the zlib.
420 * This is based on the allocation order in deflateInit2.
421 */
422static void *alloc_zlib(void *opaque, unsigned int items, unsigned int size)
423{
424 struct comp_ctx *ctx = opaque;
425 static char round = 0; /* order in deflateInit2 */
426 void *buf = NULL;
Willy Tarreau4f31fc22014-12-24 18:07:55 +0100427 struct pool_head *pool = NULL;
William Lallemand2b502472012-10-30 14:30:39 +0100428
William Lallemande3a7d992012-11-20 11:25:20 +0100429 if (global.maxzlibmem > 0 && (global.maxzlibmem - zlib_used_memory) < (long)(items * size))
William Lallemand9d5f5482012-11-07 16:12:57 +0100430 goto end;
William Lallemand9d5f5482012-11-07 16:12:57 +0100431
William Lallemand2b502472012-10-30 14:30:39 +0100432 switch (round) {
433 case 0:
434 if (zlib_pool_deflate_state == NULL)
435 zlib_pool_deflate_state = create_pool("zlib_state", size * items, MEM_F_SHARED);
Willy Tarreau4f31fc22014-12-24 18:07:55 +0100436 pool = zlib_pool_deflate_state;
437 ctx->zlib_deflate_state = buf = pool_alloc2(pool);
William Lallemand2b502472012-10-30 14:30:39 +0100438 break;
439
440 case 1:
441 if (zlib_pool_window == NULL)
442 zlib_pool_window = create_pool("zlib_window", size * items, MEM_F_SHARED);
Willy Tarreau4f31fc22014-12-24 18:07:55 +0100443 pool = zlib_pool_window;
444 ctx->zlib_window = buf = pool_alloc2(pool);
William Lallemand2b502472012-10-30 14:30:39 +0100445 break;
446
447 case 2:
448 if (zlib_pool_prev == NULL)
449 zlib_pool_prev = create_pool("zlib_prev", size * items, MEM_F_SHARED);
Willy Tarreau4f31fc22014-12-24 18:07:55 +0100450 pool = zlib_pool_prev;
451 ctx->zlib_prev = buf = pool_alloc2(pool);
William Lallemand2b502472012-10-30 14:30:39 +0100452 break;
453
454 case 3:
455 if (zlib_pool_head == NULL)
456 zlib_pool_head = create_pool("zlib_head", size * items, MEM_F_SHARED);
Willy Tarreau4f31fc22014-12-24 18:07:55 +0100457 pool = zlib_pool_head;
458 ctx->zlib_head = buf = pool_alloc2(pool);
William Lallemand2b502472012-10-30 14:30:39 +0100459 break;
460
461 case 4:
462 if (zlib_pool_pending_buf == NULL)
463 zlib_pool_pending_buf = create_pool("zlib_pending_buf", size * items, MEM_F_SHARED);
Willy Tarreau4f31fc22014-12-24 18:07:55 +0100464 pool = zlib_pool_pending_buf;
465 ctx->zlib_pending_buf = buf = pool_alloc2(pool);
William Lallemand2b502472012-10-30 14:30:39 +0100466 break;
467 }
William Lallemande3a7d992012-11-20 11:25:20 +0100468 if (buf != NULL)
Willy Tarreau4f31fc22014-12-24 18:07:55 +0100469 zlib_used_memory += pool->size;
William Lallemand9d5f5482012-11-07 16:12:57 +0100470
471end:
William Lallemand2b502472012-10-30 14:30:39 +0100472
Willy Tarreau46909852012-11-15 14:57:56 +0100473 /* deflateInit2() first allocates and checks the deflate_state, then if
474 * it succeeds, it allocates all other 4 areas at ones and checks them
475 * at the end. So we want to correctly count the rounds depending on when
476 * zlib is supposed to abort.
477 */
478 if (buf || round)
479 round = (round + 1) % 5;
William Lallemand2b502472012-10-30 14:30:39 +0100480 return buf;
481}
482
483static void free_zlib(void *opaque, void *ptr)
484{
485 struct comp_ctx *ctx = opaque;
Willy Tarreaub1fbd052012-11-10 17:49:37 +0100486 struct pool_head *pool = NULL;
William Lallemand2b502472012-10-30 14:30:39 +0100487
488 if (ptr == ctx->zlib_window)
William Lallemand9d5f5482012-11-07 16:12:57 +0100489 pool = zlib_pool_window;
William Lallemand2b502472012-10-30 14:30:39 +0100490 else if (ptr == ctx->zlib_deflate_state)
William Lallemand9d5f5482012-11-07 16:12:57 +0100491 pool = zlib_pool_deflate_state;
William Lallemand2b502472012-10-30 14:30:39 +0100492 else if (ptr == ctx->zlib_prev)
William Lallemand9d5f5482012-11-07 16:12:57 +0100493 pool = zlib_pool_prev;
William Lallemand2b502472012-10-30 14:30:39 +0100494 else if (ptr == ctx->zlib_head)
William Lallemand9d5f5482012-11-07 16:12:57 +0100495 pool = zlib_pool_head;
William Lallemand2b502472012-10-30 14:30:39 +0100496 else if (ptr == ctx->zlib_pending_buf)
William Lallemand9d5f5482012-11-07 16:12:57 +0100497 pool = zlib_pool_pending_buf;
William Lallemand2b502472012-10-30 14:30:39 +0100498
William Lallemand9d5f5482012-11-07 16:12:57 +0100499 pool_free2(pool, ptr);
William Lallemande3a7d992012-11-20 11:25:20 +0100500 zlib_used_memory -= pool->size;
William Lallemand2b502472012-10-30 14:30:39 +0100501}
502
William Lallemand82fe75c2012-10-23 10:25:10 +0200503/**************************
504**** gzip algorithm ****
505***************************/
William Lallemand8b52bb32012-11-16 18:06:41 +0100506int gzip_init(struct comp_ctx **comp_ctx, int level)
William Lallemand82fe75c2012-10-23 10:25:10 +0200507{
William Lallemand8b52bb32012-11-16 18:06:41 +0100508 z_stream *strm;
William Lallemand82fe75c2012-10-23 10:25:10 +0200509
William Lallemand8b52bb32012-11-16 18:06:41 +0100510 if (init_comp_ctx(comp_ctx) < 0)
511 return -1;
William Lallemand9d5f5482012-11-07 16:12:57 +0100512
William Lallemand8b52bb32012-11-16 18:06:41 +0100513 strm = &(*comp_ctx)->strm;
William Lallemand82fe75c2012-10-23 10:25:10 +0200514
William Lallemand8b52bb32012-11-16 18:06:41 +0100515 if (deflateInit2(strm, level, Z_DEFLATED, global.tune.zlibwindowsize + 16, global.tune.zlibmemlevel, Z_DEFAULT_STRATEGY) != Z_OK) {
516 deinit_comp_ctx(comp_ctx);
William Lallemand82fe75c2012-10-23 10:25:10 +0200517 return -1;
William Lallemand8b52bb32012-11-16 18:06:41 +0100518 }
519
520 (*comp_ctx)->cur_lvl = level;
William Lallemand82fe75c2012-10-23 10:25:10 +0200521
522 return 0;
523}
524/**************************
525**** Deflate algorithm ****
526***************************/
527
William Lallemand8b52bb32012-11-16 18:06:41 +0100528int deflate_init(struct comp_ctx **comp_ctx, int level)
William Lallemand82fe75c2012-10-23 10:25:10 +0200529{
William Lallemand8b52bb32012-11-16 18:06:41 +0100530 z_stream *strm;
William Lallemand82fe75c2012-10-23 10:25:10 +0200531
William Lallemand8b52bb32012-11-16 18:06:41 +0100532 if (init_comp_ctx(comp_ctx) < 0)
533 return -1;
534
535 strm = &(*comp_ctx)->strm;
William Lallemand82fe75c2012-10-23 10:25:10 +0200536
Willy Tarreauc5599e72013-04-28 08:52:52 +0200537 if (deflateInit2(strm, level, Z_DEFLATED, global.tune.zlibwindowsize, global.tune.zlibmemlevel, Z_DEFAULT_STRATEGY) != Z_OK) {
William Lallemand8b52bb32012-11-16 18:06:41 +0100538 deinit_comp_ctx(comp_ctx);
William Lallemand82fe75c2012-10-23 10:25:10 +0200539 return -1;
William Lallemand8b52bb32012-11-16 18:06:41 +0100540 }
541
542 (*comp_ctx)->cur_lvl = level;
William Lallemand82fe75c2012-10-23 10:25:10 +0200543
544 return 0;
545}
546
William Lallemandbf3ae612012-11-19 12:35:37 +0100547/* Return the size of consumed data or -1 */
548int 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 +0200549{
William Lallemand82fe75c2012-10-23 10:25:10 +0200550 int ret;
William Lallemandbf3ae612012-11-19 12:35:37 +0100551 z_stream *strm = &comp_ctx->strm;
552 char *out_data = bi_end(out);
553 int out_len = out->size - buffer_len(out);
William Lallemand82fe75c2012-10-23 10:25:10 +0200554
555 if (in_len <= 0)
556 return 0;
557
558
559 if (out_len <= 0)
560 return -1;
561
William Lallemand82fe75c2012-10-23 10:25:10 +0200562 strm->next_in = (unsigned char *)in_data;
563 strm->avail_in = in_len;
564 strm->next_out = (unsigned char *)out_data;
565 strm->avail_out = out_len;
566
567 ret = deflate(strm, Z_NO_FLUSH);
568 if (ret != Z_OK)
569 return -1;
570
571 /* deflate update the available data out */
William Lallemandbf3ae612012-11-19 12:35:37 +0100572 out->i += out_len - strm->avail_out;
William Lallemand82fe75c2012-10-23 10:25:10 +0200573
William Lallemandbf3ae612012-11-19 12:35:37 +0100574 return in_len - strm->avail_in;
William Lallemand82fe75c2012-10-23 10:25:10 +0200575}
576
William Lallemand1c2d6222012-10-30 15:52:53 +0100577int deflate_flush(struct comp_ctx *comp_ctx, struct buffer *out, int flag)
William Lallemand82fe75c2012-10-23 10:25:10 +0200578{
579 int ret;
William Lallemand82fe75c2012-10-23 10:25:10 +0200580 int out_len = 0;
William Lallemand1c2d6222012-10-30 15:52:53 +0100581 z_stream *strm = &comp_ctx->strm;
William Lallemand82fe75c2012-10-23 10:25:10 +0200582
William Lallemand82fe75c2012-10-23 10:25:10 +0200583 strm->next_out = (unsigned char *)bi_end(out);
584 strm->avail_out = out->size - buffer_len(out);
585
586 ret = deflate(strm, flag);
587 if (ret != Z_OK && ret != Z_STREAM_END)
588 return -1;
589
590 out_len = (out->size - buffer_len(out)) - strm->avail_out;
591 out->i += out_len;
592
William Lallemand072a2bf2012-11-20 17:01:01 +0100593 /* compression limit */
594 if ((global.comp_rate_lim > 0 && (read_freq_ctr(&global.comp_bps_out) > global.comp_rate_lim)) || /* rate */
595 (idle_pct < compress_min_idle)) { /* idle */
596 /* decrease level */
597 if (comp_ctx->cur_lvl > 0) {
598 comp_ctx->cur_lvl--;
William Lallemandd85f9172012-11-09 17:05:39 +0100599 deflateParams(&comp_ctx->strm, comp_ctx->cur_lvl, Z_DEFAULT_STRATEGY);
600 }
William Lallemand072a2bf2012-11-20 17:01:01 +0100601
602 } else if (comp_ctx->cur_lvl < global.tune.comp_maxlevel) {
603 /* increase level */
604 comp_ctx->cur_lvl++ ;
605 deflateParams(&comp_ctx->strm, comp_ctx->cur_lvl, Z_DEFAULT_STRATEGY);
William Lallemandd85f9172012-11-09 17:05:39 +0100606 }
607
William Lallemand82fe75c2012-10-23 10:25:10 +0200608 return out_len;
609}
610
William Lallemand1c2d6222012-10-30 15:52:53 +0100611int deflate_reset(struct comp_ctx *comp_ctx)
William Lallemand82fe75c2012-10-23 10:25:10 +0200612{
William Lallemand1c2d6222012-10-30 15:52:53 +0100613 z_stream *strm = &comp_ctx->strm;
William Lallemand82fe75c2012-10-23 10:25:10 +0200614
William Lallemand82fe75c2012-10-23 10:25:10 +0200615 if (deflateReset(strm) == Z_OK)
616 return 0;
617 return -1;
618}
619
William Lallemand8b52bb32012-11-16 18:06:41 +0100620int deflate_end(struct comp_ctx **comp_ctx)
William Lallemand82fe75c2012-10-23 10:25:10 +0200621{
William Lallemand8b52bb32012-11-16 18:06:41 +0100622 z_stream *strm = &(*comp_ctx)->strm;
623 int ret;
William Lallemand82fe75c2012-10-23 10:25:10 +0200624
William Lallemand8b52bb32012-11-16 18:06:41 +0100625 ret = deflateEnd(strm);
William Lallemand82fe75c2012-10-23 10:25:10 +0200626
William Lallemand8b52bb32012-11-16 18:06:41 +0100627 deinit_comp_ctx(comp_ctx);
628
629 return ret;
William Lallemand82fe75c2012-10-23 10:25:10 +0200630}
631
632#endif /* USE_ZLIB */
633
William Lallemand727db8b2013-04-20 17:33:20 +0200634/* boolean, returns true if compression is used (either gzip or deflate) in the response */
635static int
636smp_fetch_res_comp(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +0100637 const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemand727db8b2013-04-20 17:33:20 +0200638{
639 smp->type = SMP_T_BOOL;
640 smp->data.uint = (l4->comp_algo != NULL);
641 return 1;
642}
643
644/* string, returns algo */
645static int
646smp_fetch_res_comp_algo(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +0100647 const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemand727db8b2013-04-20 17:33:20 +0200648{
649 if (!l4->comp_algo)
650 return 0;
651
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100652 smp->type = SMP_T_STR;
653 smp->flags = SMP_F_CONST;
William Lallemand727db8b2013-04-20 17:33:20 +0200654 smp->data.str.str = l4->comp_algo->name;
655 smp->data.str.len = l4->comp_algo->name_len;
656 return 1;
657}
658
659/* Note: must not be declared <const> as its list will be overwritten */
Willy Tarreaudc13c112013-06-21 23:16:39 +0200660static struct acl_kw_list acl_kws = {ILH, {
Willy Tarreau7f6fa692013-04-23 19:39:43 +0200661 { /* END */ },
William Lallemand727db8b2013-04-20 17:33:20 +0200662}};
663
664/* Note: must not be declared <const> as its list will be overwritten */
Willy Tarreaudc13c112013-06-21 23:16:39 +0200665static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
William Lallemand727db8b2013-04-20 17:33:20 +0200666 { "res.comp", smp_fetch_res_comp, 0, NULL, SMP_T_BOOL, SMP_USE_HRSHP },
667 { "res.comp_algo", smp_fetch_res_comp_algo, 0, NULL, SMP_T_STR, SMP_USE_HRSHP },
668 { /* END */ },
669}};
670
671__attribute__((constructor))
672static void __comp_fetch_init(void)
673{
674 acl_register_keywords(&acl_kws);
675 sample_register_fetches(&sample_fetch_keywords);
676}