blob: fd053d5409be3ec23b4204f0299feaa3bc186748 [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
Willy Tarreau9f640a12015-03-28 15:46:00 +010058static int identity_init(struct comp_ctx **comp_ctx, int level);
59static int identity_add_data(struct comp_ctx *comp_ctx, const char *in_data, int in_len, struct buffer *out);
Willy Tarreau9787efa2015-03-28 19:17:31 +010060static int identity_flush(struct comp_ctx *comp_ctx, struct buffer *out);
61static int identity_finish(struct comp_ctx *comp_ctx, struct buffer *out);
Willy Tarreau9f640a12015-03-28 15:46:00 +010062static int identity_reset(struct comp_ctx *comp_ctx);
63static int identity_end(struct comp_ctx **comp_ctx);
64
65#ifdef USE_ZLIB
66static int gzip_init(struct comp_ctx **comp_ctx, int level);
Willy Tarreauc91840a2015-03-28 17:00:39 +010067static int raw_def_init(struct comp_ctx **comp_ctx, int level);
Willy Tarreau9f640a12015-03-28 15:46:00 +010068static int deflate_init(struct comp_ctx **comp_ctx, int level);
69static int deflate_add_data(struct comp_ctx *comp_ctx, const char *in_data, int in_len, struct buffer *out);
Willy Tarreau9787efa2015-03-28 19:17:31 +010070static int deflate_flush(struct comp_ctx *comp_ctx, struct buffer *out);
71static int deflate_finish(struct comp_ctx *comp_ctx, struct buffer *out);
Willy Tarreau9f640a12015-03-28 15:46:00 +010072static int deflate_reset(struct comp_ctx *comp_ctx);
73static int deflate_end(struct comp_ctx **comp_ctx);
74#endif /* USE_ZLIB */
75
William Lallemand2b502472012-10-30 14:30:39 +010076
Cyril Bonté6162c432012-11-10 19:27:47 +010077const struct comp_algo comp_algos[] =
William Lallemand82fe75c2012-10-23 10:25:10 +020078{
Willy Tarreau9787efa2015-03-28 19:17:31 +010079 { "identity", 8, "identity", 8, identity_init, identity_add_data, identity_flush, identity_finish, identity_reset, identity_end },
William Lallemand82fe75c2012-10-23 10:25:10 +020080#ifdef USE_ZLIB
Willy Tarreau9787efa2015-03-28 19:17:31 +010081 { "deflate", 7, "deflate", 7, deflate_init, deflate_add_data, deflate_flush, deflate_finish, deflate_reset, deflate_end },
82 { "raw-deflate", 11, "deflate", 7, raw_def_init, deflate_add_data, deflate_flush, deflate_finish, deflate_reset, deflate_end },
83 { "gzip", 4, "gzip", 4, gzip_init, deflate_add_data, deflate_flush, deflate_finish, deflate_reset, deflate_end },
William Lallemand82fe75c2012-10-23 10:25:10 +020084#endif /* USE_ZLIB */
Willy Tarreau615105e2015-03-28 16:40:46 +010085 { NULL, 0, NULL, 0, NULL , NULL, NULL, NULL, NULL }
William Lallemand82fe75c2012-10-23 10:25:10 +020086};
87
88/*
89 * Add a content-type in the configuration
90 */
91int comp_append_type(struct comp *comp, const char *type)
92{
93 struct comp_type *comp_type;
94
95 comp_type = calloc(1, sizeof(struct comp_type));
96 comp_type->name_len = strlen(type);
97 comp_type->name = strdup(type);
98 comp_type->next = comp->types;
99 comp->types = comp_type;
100 return 0;
101}
102
103/*
104 * Add an algorithm in the configuration
105 */
106int comp_append_algo(struct comp *comp, const char *algo)
107{
108 struct comp_algo *comp_algo;
109 int i;
110
Willy Tarreau615105e2015-03-28 16:40:46 +0100111 for (i = 0; comp_algos[i].cfg_name; i++) {
112 if (!strcmp(algo, comp_algos[i].cfg_name)) {
William Lallemand82fe75c2012-10-23 10:25:10 +0200113 comp_algo = calloc(1, sizeof(struct comp_algo));
114 memmove(comp_algo, &comp_algos[i], sizeof(struct comp_algo));
115 comp_algo->next = comp->algos;
116 comp->algos = comp_algo;
117 return 0;
118 }
119 }
120 return -1;
121}
122
123/* emit the chunksize followed by a CRLF on the output and return the number of
Willy Tarreau15530d22015-03-28 12:05:47 +0100124 * bytes written. It goes backwards and starts with the byte before <end>. It
125 * returns the number of bytes written which will not exceed 10 (8 digits, CR,
126 * and LF). The caller is responsible for ensuring there is enough room left in
127 * the output buffer for the string.
William Lallemand82fe75c2012-10-23 10:25:10 +0200128 */
Willy Tarreau15530d22015-03-28 12:05:47 +0100129int http_emit_chunk_size(char *end, unsigned int chksz)
William Lallemand82fe75c2012-10-23 10:25:10 +0200130{
Willy Tarreau15530d22015-03-28 12:05:47 +0100131 char *beg = end;
William Lallemand82fe75c2012-10-23 10:25:10 +0200132
Willy Tarreau15530d22015-03-28 12:05:47 +0100133 *--beg = '\n';
134 *--beg = '\r';
William Lallemand82fe75c2012-10-23 10:25:10 +0200135 do {
Willy Tarreau15530d22015-03-28 12:05:47 +0100136 *--beg = hextab[chksz & 0xF];
137 } while (chksz >>= 4);
138 return end - beg;
William Lallemand82fe75c2012-10-23 10:25:10 +0200139}
140
141/*
142 * Init HTTP compression
143 */
144int http_compression_buffer_init(struct session *s, struct buffer *in, struct buffer *out)
145{
Willy Tarreau2aee2212015-03-28 12:20:33 +0100146 /* output stream requires at least 10 bytes for the gzip header, plus
147 * at least 8 bytes for the gzip trailer (crc+len), plus a possible
148 * plus at most 5 bytes per 32kB block and 2 bytes to close the stream.
149 */
150 if (in->size - buffer_len(in) < 20 + 5 * ((in->i + 32767) >> 15))
Willy Tarreaud328af52015-03-28 11:10:56 +0100151 return -1;
William Lallemand82fe75c2012-10-23 10:25:10 +0200152
Willy Tarreaud328af52015-03-28 11:10:56 +0100153 /* prepare an empty output buffer in which we reserve enough room for
Willy Tarreau15530d22015-03-28 12:05:47 +0100154 * copying the output bytes from <in>, plus 10 extra bytes to write
Willy Tarreaud328af52015-03-28 11:10:56 +0100155 * the chunk size. We don't copy the bytes yet so that if we have to
156 * cancel the operation later, it's cheap.
William Lallemand82fe75c2012-10-23 10:25:10 +0200157 */
Willy Tarreau474cf542014-11-24 10:54:47 +0100158 b_reset(out);
Willy Tarreaud328af52015-03-28 11:10:56 +0100159 out->o = in->o;
160 out->p += out->o;
Willy Tarreau15530d22015-03-28 12:05:47 +0100161 out->i = 10;
William Lallemand82fe75c2012-10-23 10:25:10 +0200162 return 0;
163}
164
165/*
166 * Add data to compress
167 */
168int http_compression_buffer_add_data(struct session *s, struct buffer *in, struct buffer *out)
169{
170 struct http_msg *msg = &s->txn.rsp;
William Lallemandbf3ae612012-11-19 12:35:37 +0100171 int consumed_data = 0;
William Lallemand82fe75c2012-10-23 10:25:10 +0200172 int data_process_len;
Willy Tarreau7f2f8d52014-04-18 00:20:14 +0200173 int block1, block2;
William Lallemand82fe75c2012-10-23 10:25:10 +0200174
175 /*
Willy Tarreau7f2f8d52014-04-18 00:20:14 +0200176 * Temporarily skip already parsed data and chunks to jump to the
177 * actual data block. It is fixed before leaving.
William Lallemand82fe75c2012-10-23 10:25:10 +0200178 */
179 b_adv(in, msg->next);
William Lallemand82fe75c2012-10-23 10:25:10 +0200180
181 /*
182 * select the smallest size between the announced chunk size, the input
Willy Tarreau7f2f8d52014-04-18 00:20:14 +0200183 * data, and the available output buffer size. The compressors are
184 * assumed to be able to process all the bytes we pass to them at once.
William Lallemand82fe75c2012-10-23 10:25:10 +0200185 */
186 data_process_len = MIN(in->i, msg->chunk_len);
187 data_process_len = MIN(out->size - buffer_len(out), data_process_len);
188
Willy Tarreau7f2f8d52014-04-18 00:20:14 +0200189 block1 = data_process_len;
190 if (block1 > bi_contig_data(in))
191 block1 = bi_contig_data(in);
192 block2 = data_process_len - block1;
William Lallemand82fe75c2012-10-23 10:25:10 +0200193
Willy Tarreau7f2f8d52014-04-18 00:20:14 +0200194 /* compressors return < 0 upon error or the amount of bytes read */
195 consumed_data = s->comp_algo->add_data(s->comp_ctx, bi_ptr(in), block1, out);
196 if (consumed_data >= 0 && block2 > 0) {
197 consumed_data = s->comp_algo->add_data(s->comp_ctx, in->data, block2, out);
198 if (consumed_data >= 0)
199 consumed_data += block1;
William Lallemand82fe75c2012-10-23 10:25:10 +0200200 }
201
Willy Tarreau7f2f8d52014-04-18 00:20:14 +0200202 /* restore original buffer pointer */
203 b_rew(in, msg->next);
William Lallemand82fe75c2012-10-23 10:25:10 +0200204
Willy Tarreau7f2f8d52014-04-18 00:20:14 +0200205 if (consumed_data > 0) {
206 msg->next += consumed_data;
207 msg->chunk_len -= consumed_data;
208 }
William Lallemandbf3ae612012-11-19 12:35:37 +0100209 return consumed_data;
William Lallemand82fe75c2012-10-23 10:25:10 +0200210}
211
212/*
213 * Flush data in process, and write the header and footer of the chunk. Upon
214 * success, in and out buffers are swapped to avoid a copy.
215 */
216int http_compression_buffer_end(struct session *s, struct buffer **in, struct buffer **out, int end)
217{
Willy Tarreau3ca54482014-04-23 19:31:17 +0200218 int to_forward;
William Lallemand82fe75c2012-10-23 10:25:10 +0200219 int left;
220 struct http_msg *msg = &s->txn.rsp;
221 struct buffer *ib = *in, *ob = *out;
Willy Tarreaud328af52015-03-28 11:10:56 +0100222 char *tail;
William Lallemand08289f12012-10-31 11:19:18 +0100223
224#ifdef USE_ZLIB
William Lallemand82fe75c2012-10-23 10:25:10 +0200225 int ret;
226
227 /* flush data here */
228
229 if (end)
Willy Tarreau9787efa2015-03-28 19:17:31 +0100230 ret = s->comp_algo->finish(s->comp_ctx, ob); /* end of data */
William Lallemand82fe75c2012-10-23 10:25:10 +0200231 else
Willy Tarreau9787efa2015-03-28 19:17:31 +0100232 ret = s->comp_algo->flush(s->comp_ctx, ob); /* end of buffer */
William Lallemand82fe75c2012-10-23 10:25:10 +0200233
234 if (ret < 0)
235 return -1; /* flush failed */
236
William Lallemand08289f12012-10-31 11:19:18 +0100237#endif /* USE_ZLIB */
238
Willy Tarreau15530d22015-03-28 12:05:47 +0100239 if (ob->i == 10) {
Willy Tarreaud328af52015-03-28 11:10:56 +0100240 /* No data were appended, let's drop the output buffer and
241 * keep the input buffer unchanged.
242 */
243 return 0;
244 }
William Lallemand82fe75c2012-10-23 10:25:10 +0200245
Willy Tarreaud328af52015-03-28 11:10:56 +0100246 /* OK so at this stage, we have an output buffer <ob> looking like this :
247 *
248 * <-- o --> <------ i ----->
249 * +---------+---+------------+-----------+
250 * | out | c | comp_in | empty |
251 * +---------+---+------------+-----------+
252 * data p size
253 *
254 * <out> is the room reserved to copy ib->o. It starts at ob->data and
255 * has not yet been filled. <c> is the room reserved to write the chunk
Willy Tarreau15530d22015-03-28 12:05:47 +0100256 * size (10 bytes). <comp_in> is the compressed equivalent of the data
Willy Tarreaud328af52015-03-28 11:10:56 +0100257 * part of ib->i. <empty> is the amount of empty bytes at the end of
258 * the buffer, into which we may have to copy the remaining bytes from
259 * ib->i after the data (chunk size, trailers, ...).
260 */
William Lallemand82fe75c2012-10-23 10:25:10 +0200261
Willy Tarreaud328af52015-03-28 11:10:56 +0100262 /* Write real size at the begining of the chunk, no need of wrapping.
Willy Tarreau15530d22015-03-28 12:05:47 +0100263 * We write the chunk using a dynamic length and adjust ob->p and ob->i
264 * accordingly afterwards. That will move <out> away from <data>.
Willy Tarreaud328af52015-03-28 11:10:56 +0100265 */
Willy Tarreau15530d22015-03-28 12:05:47 +0100266 left = 10 - http_emit_chunk_size(ob->p + 10, ob->i - 10);
267 ob->p += left;
268 ob->i -= left;
William Lallemand82fe75c2012-10-23 10:25:10 +0200269
Willy Tarreaud328af52015-03-28 11:10:56 +0100270 /* Copy previous data from ib->o into ob->o */
271 if (ib->o > 0) {
272 left = bo_contig_data(ib);
273 memcpy(ob->p - ob->o, bo_ptr(ib), left);
274 if (ib->o - left) /* second part of the buffer */
275 memcpy(ob->p - ob->o + left, ib->data, ib->o - left);
276 }
277
278 /* chunked encoding requires CRLF after data */
279 tail = ob->p + ob->i;
280 *tail++ = '\r';
281 *tail++ = '\n';
282
283 /* At the end of data, we must write the empty chunk 0<CRLF>,
284 * and terminate the trailers section with a last <CRLF>. If
285 * we're forwarding a chunked-encoded response, we'll have a
286 * trailers section after the empty chunk which needs to be
287 * forwarded and which will provide the last CRLF. Otherwise
288 * we write it ourselves.
289 */
290 if (msg->msg_state >= HTTP_MSG_TRAILERS) {
291 memcpy(tail, "0\r\n", 3);
292 tail += 3;
293 if (msg->msg_state >= HTTP_MSG_DONE) {
294 memcpy(tail, "\r\n", 2);
295 tail += 2;
William Lallemand82fe75c2012-10-23 10:25:10 +0200296 }
William Lallemand82fe75c2012-10-23 10:25:10 +0200297 }
Willy Tarreaud328af52015-03-28 11:10:56 +0100298 ob->i = tail - ob->p;
William Lallemand82fe75c2012-10-23 10:25:10 +0200299
300 to_forward = ob->i;
Willy Tarreau55058a72012-11-21 08:27:21 +0100301
William Lallemandd85f9172012-11-09 17:05:39 +0100302 /* update input rate */
Willy Tarreau55058a72012-11-21 08:27:21 +0100303 if (s->comp_ctx && s->comp_ctx->cur_lvl > 0) {
Willy Tarreau3ca54482014-04-23 19:31:17 +0200304 update_freq_ctr(&global.comp_bps_in, msg->next);
305 s->fe->fe_counters.comp_in += msg->next;
306 s->be->be_counters.comp_in += msg->next;
Willy Tarreau55058a72012-11-21 08:27:21 +0100307 } else {
Willy Tarreau3ca54482014-04-23 19:31:17 +0200308 s->fe->fe_counters.comp_byp += msg->next;
309 s->be->be_counters.comp_byp += msg->next;
Willy Tarreau55058a72012-11-21 08:27:21 +0100310 }
William Lallemandd85f9172012-11-09 17:05:39 +0100311
William Lallemand82fe75c2012-10-23 10:25:10 +0200312 /* copy the remaining data in the tmp buffer. */
Willy Tarreau7f2f8d52014-04-18 00:20:14 +0200313 b_adv(ib, msg->next);
314 msg->next = 0;
315
William Lallemand82fe75c2012-10-23 10:25:10 +0200316 if (ib->i > 0) {
Willy Tarreaud328af52015-03-28 11:10:56 +0100317 left = bi_contig_data(ib);
318 memcpy(ob->p + ob->i, bi_ptr(ib), left);
319 ob->i += left;
320 if (ib->i - left) {
321 memcpy(ob->p + ob->i, ib->data, ib->i - left);
322 ob->i += ib->i - left;
William Lallemand82fe75c2012-10-23 10:25:10 +0200323 }
324 }
325
326 /* swap the buffers */
327 *in = ob;
328 *out = ib;
329
Willy Tarreau55058a72012-11-21 08:27:21 +0100330 if (s->comp_ctx && s->comp_ctx->cur_lvl > 0) {
William Lallemandd85f9172012-11-09 17:05:39 +0100331 update_freq_ctr(&global.comp_bps_out, to_forward);
Willy Tarreau55058a72012-11-21 08:27:21 +0100332 s->fe->fe_counters.comp_out += to_forward;
333 s->be->be_counters.comp_out += to_forward;
334 }
William Lallemandd85f9172012-11-09 17:05:39 +0100335
William Lallemand82fe75c2012-10-23 10:25:10 +0200336 /* forward the new chunk without remaining data */
337 b_adv(ob, to_forward);
338
William Lallemand82fe75c2012-10-23 10:25:10 +0200339 return to_forward;
340}
341
William Lallemand8b52bb32012-11-16 18:06:41 +0100342/*
343 * Alloc the comp_ctx
344 */
345static inline int init_comp_ctx(struct comp_ctx **comp_ctx)
346{
347#ifdef USE_ZLIB
348 z_stream *strm;
349
William Lallemande3a7d992012-11-20 11:25:20 +0100350 if (global.maxzlibmem > 0 && (global.maxzlibmem - zlib_used_memory) < sizeof(struct comp_ctx))
William Lallemand8b52bb32012-11-16 18:06:41 +0100351 return -1;
352#endif
353
354 if (unlikely(pool_comp_ctx == NULL))
355 pool_comp_ctx = create_pool("comp_ctx", sizeof(struct comp_ctx), MEM_F_SHARED);
356
357 *comp_ctx = pool_alloc2(pool_comp_ctx);
358 if (*comp_ctx == NULL)
359 return -1;
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
363 strm = &(*comp_ctx)->strm;
364 strm->zalloc = alloc_zlib;
365 strm->zfree = free_zlib;
366 strm->opaque = *comp_ctx;
367#endif
368 return 0;
369}
370
371/*
372 * Dealloc the comp_ctx
373 */
374static inline int deinit_comp_ctx(struct comp_ctx **comp_ctx)
375{
376 if (!*comp_ctx)
377 return 0;
378
379 pool_free2(pool_comp_ctx, *comp_ctx);
380 *comp_ctx = NULL;
381
382#ifdef USE_ZLIB
William Lallemande3a7d992012-11-20 11:25:20 +0100383 zlib_used_memory -= sizeof(struct comp_ctx);
William Lallemand8b52bb32012-11-16 18:06:41 +0100384#endif
William Lallemand8b52bb32012-11-16 18:06:41 +0100385 return 0;
386}
387
William Lallemand82fe75c2012-10-23 10:25:10 +0200388
389/****************************
390 **** Identity algorithm ****
391 ****************************/
392
393/*
394 * Init the identity algorithm
395 */
Willy Tarreau9f640a12015-03-28 15:46:00 +0100396static int identity_init(struct comp_ctx **comp_ctx, int level)
William Lallemand82fe75c2012-10-23 10:25:10 +0200397{
398 return 0;
399}
400
401/*
402 * Process data
William Lallemandbf3ae612012-11-19 12:35:37 +0100403 * Return size of consumed data or -1 on error
William Lallemand82fe75c2012-10-23 10:25:10 +0200404 */
Willy Tarreau9f640a12015-03-28 15:46:00 +0100405static int 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 +0200406{
William Lallemandbf3ae612012-11-19 12:35:37 +0100407 char *out_data = bi_end(out);
408 int out_len = out->size - buffer_len(out);
409
William Lallemand82fe75c2012-10-23 10:25:10 +0200410 if (out_len < in_len)
411 return -1;
412
413 memcpy(out_data, in_data, in_len);
414
William Lallemandbf3ae612012-11-19 12:35:37 +0100415 out->i += in_len;
416
William Lallemand82fe75c2012-10-23 10:25:10 +0200417 return in_len;
418}
419
Willy Tarreau9787efa2015-03-28 19:17:31 +0100420static int identity_flush(struct comp_ctx *comp_ctx, struct buffer *out)
William Lallemand82fe75c2012-10-23 10:25:10 +0200421{
422 return 0;
423}
424
Willy Tarreau9787efa2015-03-28 19:17:31 +0100425static int identity_finish(struct comp_ctx *comp_ctx, struct buffer *out)
426{
427 return 0;
428}
429
Willy Tarreau9f640a12015-03-28 15:46:00 +0100430static int identity_reset(struct comp_ctx *comp_ctx)
William Lallemand82fe75c2012-10-23 10:25:10 +0200431{
432 return 0;
433}
434
435/*
436 * Deinit the algorithm
437 */
Willy Tarreau9f640a12015-03-28 15:46:00 +0100438static int identity_end(struct comp_ctx **comp_ctx)
William Lallemand82fe75c2012-10-23 10:25:10 +0200439{
440 return 0;
441}
442
443
444#ifdef USE_ZLIB
William Lallemand2b502472012-10-30 14:30:39 +0100445/*
446 * This is a tricky allocation function using the zlib.
447 * This is based on the allocation order in deflateInit2.
448 */
449static void *alloc_zlib(void *opaque, unsigned int items, unsigned int size)
450{
451 struct comp_ctx *ctx = opaque;
452 static char round = 0; /* order in deflateInit2 */
453 void *buf = NULL;
Willy Tarreau4f31fc22014-12-24 18:07:55 +0100454 struct pool_head *pool = NULL;
William Lallemand2b502472012-10-30 14:30:39 +0100455
William Lallemande3a7d992012-11-20 11:25:20 +0100456 if (global.maxzlibmem > 0 && (global.maxzlibmem - zlib_used_memory) < (long)(items * size))
William Lallemand9d5f5482012-11-07 16:12:57 +0100457 goto end;
William Lallemand9d5f5482012-11-07 16:12:57 +0100458
William Lallemand2b502472012-10-30 14:30:39 +0100459 switch (round) {
460 case 0:
461 if (zlib_pool_deflate_state == NULL)
462 zlib_pool_deflate_state = create_pool("zlib_state", size * items, MEM_F_SHARED);
Willy Tarreau4f31fc22014-12-24 18:07:55 +0100463 pool = zlib_pool_deflate_state;
464 ctx->zlib_deflate_state = buf = pool_alloc2(pool);
William Lallemand2b502472012-10-30 14:30:39 +0100465 break;
466
467 case 1:
468 if (zlib_pool_window == NULL)
469 zlib_pool_window = create_pool("zlib_window", size * items, MEM_F_SHARED);
Willy Tarreau4f31fc22014-12-24 18:07:55 +0100470 pool = zlib_pool_window;
471 ctx->zlib_window = buf = pool_alloc2(pool);
William Lallemand2b502472012-10-30 14:30:39 +0100472 break;
473
474 case 2:
475 if (zlib_pool_prev == NULL)
476 zlib_pool_prev = create_pool("zlib_prev", size * items, MEM_F_SHARED);
Willy Tarreau4f31fc22014-12-24 18:07:55 +0100477 pool = zlib_pool_prev;
478 ctx->zlib_prev = buf = pool_alloc2(pool);
William Lallemand2b502472012-10-30 14:30:39 +0100479 break;
480
481 case 3:
482 if (zlib_pool_head == NULL)
483 zlib_pool_head = create_pool("zlib_head", size * items, MEM_F_SHARED);
Willy Tarreau4f31fc22014-12-24 18:07:55 +0100484 pool = zlib_pool_head;
485 ctx->zlib_head = buf = pool_alloc2(pool);
William Lallemand2b502472012-10-30 14:30:39 +0100486 break;
487
488 case 4:
489 if (zlib_pool_pending_buf == NULL)
490 zlib_pool_pending_buf = create_pool("zlib_pending_buf", size * items, MEM_F_SHARED);
Willy Tarreau4f31fc22014-12-24 18:07:55 +0100491 pool = zlib_pool_pending_buf;
492 ctx->zlib_pending_buf = buf = pool_alloc2(pool);
William Lallemand2b502472012-10-30 14:30:39 +0100493 break;
494 }
William Lallemande3a7d992012-11-20 11:25:20 +0100495 if (buf != NULL)
Willy Tarreau4f31fc22014-12-24 18:07:55 +0100496 zlib_used_memory += pool->size;
William Lallemand9d5f5482012-11-07 16:12:57 +0100497
498end:
William Lallemand2b502472012-10-30 14:30:39 +0100499
Willy Tarreau46909852012-11-15 14:57:56 +0100500 /* deflateInit2() first allocates and checks the deflate_state, then if
501 * it succeeds, it allocates all other 4 areas at ones and checks them
502 * at the end. So we want to correctly count the rounds depending on when
503 * zlib is supposed to abort.
504 */
505 if (buf || round)
506 round = (round + 1) % 5;
William Lallemand2b502472012-10-30 14:30:39 +0100507 return buf;
508}
509
510static void free_zlib(void *opaque, void *ptr)
511{
512 struct comp_ctx *ctx = opaque;
Willy Tarreaub1fbd052012-11-10 17:49:37 +0100513 struct pool_head *pool = NULL;
William Lallemand2b502472012-10-30 14:30:39 +0100514
515 if (ptr == ctx->zlib_window)
William Lallemand9d5f5482012-11-07 16:12:57 +0100516 pool = zlib_pool_window;
William Lallemand2b502472012-10-30 14:30:39 +0100517 else if (ptr == ctx->zlib_deflate_state)
William Lallemand9d5f5482012-11-07 16:12:57 +0100518 pool = zlib_pool_deflate_state;
William Lallemand2b502472012-10-30 14:30:39 +0100519 else if (ptr == ctx->zlib_prev)
William Lallemand9d5f5482012-11-07 16:12:57 +0100520 pool = zlib_pool_prev;
William Lallemand2b502472012-10-30 14:30:39 +0100521 else if (ptr == ctx->zlib_head)
William Lallemand9d5f5482012-11-07 16:12:57 +0100522 pool = zlib_pool_head;
William Lallemand2b502472012-10-30 14:30:39 +0100523 else if (ptr == ctx->zlib_pending_buf)
William Lallemand9d5f5482012-11-07 16:12:57 +0100524 pool = zlib_pool_pending_buf;
William Lallemand2b502472012-10-30 14:30:39 +0100525
William Lallemand9d5f5482012-11-07 16:12:57 +0100526 pool_free2(pool, ptr);
William Lallemande3a7d992012-11-20 11:25:20 +0100527 zlib_used_memory -= pool->size;
William Lallemand2b502472012-10-30 14:30:39 +0100528}
529
William Lallemand82fe75c2012-10-23 10:25:10 +0200530/**************************
531**** gzip algorithm ****
532***************************/
Willy Tarreau9f640a12015-03-28 15:46:00 +0100533static int gzip_init(struct comp_ctx **comp_ctx, int level)
William Lallemand82fe75c2012-10-23 10:25:10 +0200534{
William Lallemand8b52bb32012-11-16 18:06:41 +0100535 z_stream *strm;
William Lallemand82fe75c2012-10-23 10:25:10 +0200536
William Lallemand8b52bb32012-11-16 18:06:41 +0100537 if (init_comp_ctx(comp_ctx) < 0)
538 return -1;
William Lallemand9d5f5482012-11-07 16:12:57 +0100539
William Lallemand8b52bb32012-11-16 18:06:41 +0100540 strm = &(*comp_ctx)->strm;
William Lallemand82fe75c2012-10-23 10:25:10 +0200541
William Lallemand8b52bb32012-11-16 18:06:41 +0100542 if (deflateInit2(strm, level, Z_DEFLATED, global.tune.zlibwindowsize + 16, global.tune.zlibmemlevel, Z_DEFAULT_STRATEGY) != Z_OK) {
543 deinit_comp_ctx(comp_ctx);
William Lallemand82fe75c2012-10-23 10:25:10 +0200544 return -1;
William Lallemand8b52bb32012-11-16 18:06:41 +0100545 }
546
547 (*comp_ctx)->cur_lvl = level;
William Lallemand82fe75c2012-10-23 10:25:10 +0200548
549 return 0;
550}
Willy Tarreauc91840a2015-03-28 17:00:39 +0100551
552/* Raw deflate algorithm */
553static int raw_def_init(struct comp_ctx **comp_ctx, int level)
554{
555 z_stream *strm;
556
557 if (init_comp_ctx(comp_ctx) < 0)
558 return -1;
559
560 strm = &(*comp_ctx)->strm;
561
562 if (deflateInit2(strm, level, Z_DEFLATED, -global.tune.zlibwindowsize, global.tune.zlibmemlevel, Z_DEFAULT_STRATEGY) != Z_OK) {
563 deinit_comp_ctx(comp_ctx);
564 return -1;
565 }
566
567 (*comp_ctx)->cur_lvl = level;
568 return 0;
569}
570
William Lallemand82fe75c2012-10-23 10:25:10 +0200571/**************************
572**** Deflate algorithm ****
573***************************/
574
Willy Tarreau9f640a12015-03-28 15:46:00 +0100575static int deflate_init(struct comp_ctx **comp_ctx, int level)
William Lallemand82fe75c2012-10-23 10:25:10 +0200576{
William Lallemand8b52bb32012-11-16 18:06:41 +0100577 z_stream *strm;
William Lallemand82fe75c2012-10-23 10:25:10 +0200578
William Lallemand8b52bb32012-11-16 18:06:41 +0100579 if (init_comp_ctx(comp_ctx) < 0)
580 return -1;
581
582 strm = &(*comp_ctx)->strm;
William Lallemand82fe75c2012-10-23 10:25:10 +0200583
Willy Tarreauc5599e72013-04-28 08:52:52 +0200584 if (deflateInit2(strm, level, Z_DEFLATED, global.tune.zlibwindowsize, global.tune.zlibmemlevel, Z_DEFAULT_STRATEGY) != Z_OK) {
William Lallemand8b52bb32012-11-16 18:06:41 +0100585 deinit_comp_ctx(comp_ctx);
William Lallemand82fe75c2012-10-23 10:25:10 +0200586 return -1;
William Lallemand8b52bb32012-11-16 18:06:41 +0100587 }
588
589 (*comp_ctx)->cur_lvl = level;
William Lallemand82fe75c2012-10-23 10:25:10 +0200590
591 return 0;
592}
593
William Lallemandbf3ae612012-11-19 12:35:37 +0100594/* Return the size of consumed data or -1 */
Willy Tarreau9f640a12015-03-28 15:46:00 +0100595static int 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 +0200596{
William Lallemand82fe75c2012-10-23 10:25:10 +0200597 int ret;
William Lallemandbf3ae612012-11-19 12:35:37 +0100598 z_stream *strm = &comp_ctx->strm;
599 char *out_data = bi_end(out);
600 int out_len = out->size - buffer_len(out);
William Lallemand82fe75c2012-10-23 10:25:10 +0200601
602 if (in_len <= 0)
603 return 0;
604
605
606 if (out_len <= 0)
607 return -1;
608
William Lallemand82fe75c2012-10-23 10:25:10 +0200609 strm->next_in = (unsigned char *)in_data;
610 strm->avail_in = in_len;
611 strm->next_out = (unsigned char *)out_data;
612 strm->avail_out = out_len;
613
614 ret = deflate(strm, Z_NO_FLUSH);
615 if (ret != Z_OK)
616 return -1;
617
618 /* deflate update the available data out */
William Lallemandbf3ae612012-11-19 12:35:37 +0100619 out->i += out_len - strm->avail_out;
William Lallemand82fe75c2012-10-23 10:25:10 +0200620
William Lallemandbf3ae612012-11-19 12:35:37 +0100621 return in_len - strm->avail_in;
William Lallemand82fe75c2012-10-23 10:25:10 +0200622}
623
Willy Tarreau9787efa2015-03-28 19:17:31 +0100624static int deflate_flush_or_finish(struct comp_ctx *comp_ctx, struct buffer *out, int flag)
William Lallemand82fe75c2012-10-23 10:25:10 +0200625{
626 int ret;
William Lallemand82fe75c2012-10-23 10:25:10 +0200627 int out_len = 0;
William Lallemand1c2d6222012-10-30 15:52:53 +0100628 z_stream *strm = &comp_ctx->strm;
William Lallemand82fe75c2012-10-23 10:25:10 +0200629
William Lallemand82fe75c2012-10-23 10:25:10 +0200630 strm->next_out = (unsigned char *)bi_end(out);
631 strm->avail_out = out->size - buffer_len(out);
632
633 ret = deflate(strm, flag);
634 if (ret != Z_OK && ret != Z_STREAM_END)
635 return -1;
636
637 out_len = (out->size - buffer_len(out)) - strm->avail_out;
638 out->i += out_len;
639
William Lallemand072a2bf2012-11-20 17:01:01 +0100640 /* compression limit */
641 if ((global.comp_rate_lim > 0 && (read_freq_ctr(&global.comp_bps_out) > global.comp_rate_lim)) || /* rate */
642 (idle_pct < compress_min_idle)) { /* idle */
643 /* decrease level */
644 if (comp_ctx->cur_lvl > 0) {
645 comp_ctx->cur_lvl--;
William Lallemandd85f9172012-11-09 17:05:39 +0100646 deflateParams(&comp_ctx->strm, comp_ctx->cur_lvl, Z_DEFAULT_STRATEGY);
647 }
William Lallemand072a2bf2012-11-20 17:01:01 +0100648
649 } else if (comp_ctx->cur_lvl < global.tune.comp_maxlevel) {
650 /* increase level */
651 comp_ctx->cur_lvl++ ;
652 deflateParams(&comp_ctx->strm, comp_ctx->cur_lvl, Z_DEFAULT_STRATEGY);
William Lallemandd85f9172012-11-09 17:05:39 +0100653 }
654
William Lallemand82fe75c2012-10-23 10:25:10 +0200655 return out_len;
656}
657
Willy Tarreau9787efa2015-03-28 19:17:31 +0100658static int deflate_flush(struct comp_ctx *comp_ctx, struct buffer *out)
659{
660 return deflate_flush_or_finish(comp_ctx, out, Z_SYNC_FLUSH);
661}
662
663static int deflate_finish(struct comp_ctx *comp_ctx, struct buffer *out)
664{
665 return deflate_flush_or_finish(comp_ctx, out, Z_FINISH);
666}
667
Willy Tarreau9f640a12015-03-28 15:46:00 +0100668static int deflate_reset(struct comp_ctx *comp_ctx)
William Lallemand82fe75c2012-10-23 10:25:10 +0200669{
William Lallemand1c2d6222012-10-30 15:52:53 +0100670 z_stream *strm = &comp_ctx->strm;
William Lallemand82fe75c2012-10-23 10:25:10 +0200671
William Lallemand82fe75c2012-10-23 10:25:10 +0200672 if (deflateReset(strm) == Z_OK)
673 return 0;
674 return -1;
675}
676
Willy Tarreau9f640a12015-03-28 15:46:00 +0100677static int deflate_end(struct comp_ctx **comp_ctx)
William Lallemand82fe75c2012-10-23 10:25:10 +0200678{
William Lallemand8b52bb32012-11-16 18:06:41 +0100679 z_stream *strm = &(*comp_ctx)->strm;
680 int ret;
William Lallemand82fe75c2012-10-23 10:25:10 +0200681
William Lallemand8b52bb32012-11-16 18:06:41 +0100682 ret = deflateEnd(strm);
William Lallemand82fe75c2012-10-23 10:25:10 +0200683
William Lallemand8b52bb32012-11-16 18:06:41 +0100684 deinit_comp_ctx(comp_ctx);
685
686 return ret;
William Lallemand82fe75c2012-10-23 10:25:10 +0200687}
688
689#endif /* USE_ZLIB */
690
William Lallemand727db8b2013-04-20 17:33:20 +0200691/* boolean, returns true if compression is used (either gzip or deflate) in the response */
692static int
693smp_fetch_res_comp(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +0100694 const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemand727db8b2013-04-20 17:33:20 +0200695{
696 smp->type = SMP_T_BOOL;
697 smp->data.uint = (l4->comp_algo != NULL);
698 return 1;
699}
700
701/* string, returns algo */
702static int
703smp_fetch_res_comp_algo(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +0100704 const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemand727db8b2013-04-20 17:33:20 +0200705{
706 if (!l4->comp_algo)
707 return 0;
708
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100709 smp->type = SMP_T_STR;
710 smp->flags = SMP_F_CONST;
Willy Tarreau615105e2015-03-28 16:40:46 +0100711 smp->data.str.str = l4->comp_algo->cfg_name;
712 smp->data.str.len = l4->comp_algo->cfg_name_len;
William Lallemand727db8b2013-04-20 17:33:20 +0200713 return 1;
714}
715
716/* Note: must not be declared <const> as its list will be overwritten */
Willy Tarreaudc13c112013-06-21 23:16:39 +0200717static struct acl_kw_list acl_kws = {ILH, {
Willy Tarreau7f6fa692013-04-23 19:39:43 +0200718 { /* END */ },
William Lallemand727db8b2013-04-20 17:33:20 +0200719}};
720
721/* Note: must not be declared <const> as its list will be overwritten */
Willy Tarreaudc13c112013-06-21 23:16:39 +0200722static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
William Lallemand727db8b2013-04-20 17:33:20 +0200723 { "res.comp", smp_fetch_res_comp, 0, NULL, SMP_T_BOOL, SMP_USE_HRSHP },
724 { "res.comp_algo", smp_fetch_res_comp_algo, 0, NULL, SMP_T_STR, SMP_USE_HRSHP },
725 { /* END */ },
726}};
727
728__attribute__((constructor))
729static void __comp_fetch_init(void)
730{
731 acl_register_keywords(&acl_kws);
732 sample_register_fetches(&sample_fetch_keywords);
733}