blob: 4c942aad702f12573e0ced7b9cd06bcf5965ef47 [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
33#include <proto/compression.h>
34#include <proto/proto_http.h>
35
William Lallemand2b502472012-10-30 14:30:39 +010036
37#ifdef USE_ZLIB
38
39/* zlib allocation */
40static struct pool_head *zlib_pool_deflate_state = NULL;
41static struct pool_head *zlib_pool_window = NULL;
42static struct pool_head *zlib_pool_prev = NULL;
43static struct pool_head *zlib_pool_head = NULL;
44static struct pool_head *zlib_pool_pending_buf = NULL;
45
William Lallemand9d5f5482012-11-07 16:12:57 +010046static long long zlib_memory_available = -1;
47
48
William Lallemand2b502472012-10-30 14:30:39 +010049#endif
50
51
William Lallemand82fe75c2012-10-23 10:25:10 +020052static const struct comp_algo comp_algos[] =
53{
54 { "identity", 8, identity_init, identity_add_data, identity_flush, identity_reset, identity_end },
55#ifdef USE_ZLIB
56 { "deflate", 7, deflate_init, deflate_add_data, deflate_flush, deflate_reset, deflate_end },
57 { "gzip", 4, gzip_init, deflate_add_data, deflate_flush, deflate_reset, deflate_end },
58#endif /* USE_ZLIB */
59 { NULL, 0, NULL , NULL, NULL, NULL, NULL }
60};
61
62/*
63 * Add a content-type in the configuration
64 */
65int comp_append_type(struct comp *comp, const char *type)
66{
67 struct comp_type *comp_type;
68
69 comp_type = calloc(1, sizeof(struct comp_type));
70 comp_type->name_len = strlen(type);
71 comp_type->name = strdup(type);
72 comp_type->next = comp->types;
73 comp->types = comp_type;
74 return 0;
75}
76
77/*
78 * Add an algorithm in the configuration
79 */
80int comp_append_algo(struct comp *comp, const char *algo)
81{
82 struct comp_algo *comp_algo;
83 int i;
84
85 for (i = 0; comp_algos[i].name; i++) {
86 if (!strcmp(algo, comp_algos[i].name)) {
87 comp_algo = calloc(1, sizeof(struct comp_algo));
88 memmove(comp_algo, &comp_algos[i], sizeof(struct comp_algo));
89 comp_algo->next = comp->algos;
90 comp->algos = comp_algo;
91 return 0;
92 }
93 }
94 return -1;
95}
96
97/* emit the chunksize followed by a CRLF on the output and return the number of
98 * bytes written. Appends <add_crlf> additional CRLF after the first one. Chunk
99 * sizes are truncated to 6 hex digits (16 MB) and padded left. The caller is
100 * responsible for ensuring there is enough room left in the output buffer for
101 * the string (8 bytes * add_crlf*2).
102 */
103int http_emit_chunk_size(char *out, unsigned int chksz, int add_crlf)
104{
105 int shift;
106 int pos = 0;
107
108 for (shift = 20; shift >= 0; shift -= 4)
109 out[pos++] = hextab[(chksz >> shift) & 0xF];
110
111 do {
112 out[pos++] = '\r';
113 out[pos++] = '\n';
114 } while (--add_crlf >= 0);
115
116 return pos;
117}
118
119/*
120 * Init HTTP compression
121 */
122int http_compression_buffer_init(struct session *s, struct buffer *in, struct buffer *out)
123{
124 struct http_msg *msg = &s->txn.rsp;
125 int left;
126
127 /* not enough space */
128 if (in->size - buffer_len(in) < 40)
129 return -1;
130
131 /*
132 * Skip data, we don't need them in the new buffer. They are results
133 * of CHUNK_CRLF and CHUNK_SIZE parsing.
134 */
135 b_adv(in, msg->next);
136 msg->next = 0;
137 msg->sov = 0;
138 msg->sol = 0;
139
140 out->size = global.tune.bufsize;
141 out->i = 0;
142 out->o = 0;
143 out->p = out->data;
144 /* copy output data */
145 if (in->o > 0) {
146 left = in->o - bo_contig_data(in);
147 memcpy(out->data, bo_ptr(in), bo_contig_data(in));
148 out->p += bo_contig_data(in);
149 if (left > 0) { /* second part of the buffer */
150 memcpy(out->p, in->data, left);
151 out->p += left;
152 }
153 out->o = in->o;
154 }
155 out->i += http_emit_chunk_size(out->p, 0, 0);
156
157 return 0;
158}
159
160/*
161 * Add data to compress
162 */
163int http_compression_buffer_add_data(struct session *s, struct buffer *in, struct buffer *out)
164{
165 struct http_msg *msg = &s->txn.rsp;
166 int data_process_len;
167 int left;
168 int ret;
169
170 /*
171 * Skip data, we don't need them in the new buffer. They are results
172 * of CHUNK_CRLF and CHUNK_SIZE parsing.
173 */
174 b_adv(in, msg->next);
175 msg->next = 0;
176 msg->sov = 0;
177 msg->sol = 0;
178
179 /*
180 * select the smallest size between the announced chunk size, the input
181 * data, and the available output buffer size
182 */
183 data_process_len = MIN(in->i, msg->chunk_len);
184 data_process_len = MIN(out->size - buffer_len(out), data_process_len);
185
186 left = data_process_len - bi_contig_data(in);
187 if (left <= 0) {
William Lallemand1c2d6222012-10-30 15:52:53 +0100188 ret = s->comp_algo->add_data(&s->comp_ctx, bi_ptr(in),
William Lallemand82fe75c2012-10-23 10:25:10 +0200189 data_process_len, bi_end(out),
190 out->size - buffer_len(out));
191 if (ret < 0)
192 return -1;
193 out->i += ret;
194
195 } else {
William Lallemand1c2d6222012-10-30 15:52:53 +0100196 ret = s->comp_algo->add_data(&s->comp_ctx, bi_ptr(in), bi_contig_data(in), bi_end(out), out->size - buffer_len(out));
William Lallemand82fe75c2012-10-23 10:25:10 +0200197 if (ret < 0)
198 return -1;
199 out->i += ret;
William Lallemand1c2d6222012-10-30 15:52:53 +0100200 ret = s->comp_algo->add_data(&s->comp_ctx, in->data, left, bi_end(out), out->size - buffer_len(out));
William Lallemand82fe75c2012-10-23 10:25:10 +0200201 if (ret < 0)
202 return -1;
203 out->i += ret;
204 }
205
206 b_adv(in, data_process_len);
207 msg->chunk_len -= data_process_len;
208
209 return 0;
210}
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{
218 int to_forward;
219 int left;
220 struct http_msg *msg = &s->txn.rsp;
221 struct buffer *ib = *in, *ob = *out;
William Lallemand08289f12012-10-31 11:19:18 +0100222
223#ifdef USE_ZLIB
William Lallemand82fe75c2012-10-23 10:25:10 +0200224 int ret;
225
226 /* flush data here */
227
228 if (end)
229 ret = s->comp_algo->flush(&s->comp_ctx, ob, Z_FINISH); /* end of data */
230 else
231 ret = s->comp_algo->flush(&s->comp_ctx, ob, Z_SYNC_FLUSH); /* end of buffer */
232
233 if (ret < 0)
234 return -1; /* flush failed */
235
William Lallemand08289f12012-10-31 11:19:18 +0100236#endif /* USE_ZLIB */
237
William Lallemand82fe75c2012-10-23 10:25:10 +0200238 if (ob->i > 8) {
239 /* more than a chunk size => some data were emitted */
240 char *tail = ob->p + ob->i;
241
242 /* write real size at the begining of the chunk, no need of wrapping */
243 http_emit_chunk_size(ob->p, ob->i - 8, 0);
244
245 /* chunked encoding requires CRLF after data */
246 *tail++ = '\r';
247 *tail++ = '\n';
248
249 if (!(msg->flags & HTTP_MSGF_TE_CHNK) && msg->chunk_len == 0) {
250 /* End of data, 0<CRLF><CRLF> is needed but we're not
251 * in chunked mode on input so we must add it ourselves.
252 */
253 memcpy(tail, "0\r\n\r\n", 5);
254 tail += 5;
255 }
256 ob->i = tail - ob->p;
257 } else {
258 /* no data were sent, cancel the chunk size */
259 ob->i = 0;
260 }
261
262 to_forward = ob->i;
263
264 /* copy the remaining data in the tmp buffer. */
265 if (ib->i > 0) {
266 left = ib->i - bi_contig_data(ib);
267 memcpy(bi_end(ob), bi_ptr(ib), bi_contig_data(ib));
268 ob->i += bi_contig_data(ib);
269 if (left > 0) {
270 memcpy(bi_end(ob), ib->data, left);
271 ob->i += left;
272 }
273 }
274
275 /* swap the buffers */
276 *in = ob;
277 *out = ib;
278
279 /* forward the new chunk without remaining data */
280 b_adv(ob, to_forward);
281
282 /* if there are data between p and next, there are trailers, must forward them */
283 b_adv(ob, msg->next);
284 msg->next = 0;
285
286 return to_forward;
287}
288
289
290/****************************
291 **** Identity algorithm ****
292 ****************************/
293
294/*
295 * Init the identity algorithm
296 */
William Lallemand1c2d6222012-10-30 15:52:53 +0100297int identity_init(struct comp_ctx *comp_ctx, int level)
William Lallemand82fe75c2012-10-23 10:25:10 +0200298{
299 return 0;
300}
301
302/*
303 * Process data
304 * Return size of processed data or -1 on error
305 */
William Lallemand1c2d6222012-10-30 15:52:53 +0100306int identity_add_data(struct comp_ctx *comp_ctx, const char *in_data, int in_len, char *out_data, int out_len)
William Lallemand82fe75c2012-10-23 10:25:10 +0200307{
308 if (out_len < in_len)
309 return -1;
310
311 memcpy(out_data, in_data, in_len);
312
313 return in_len;
314}
315
William Lallemand1c2d6222012-10-30 15:52:53 +0100316int identity_flush(struct comp_ctx *comp_ctx, struct buffer *out, int flag)
William Lallemand82fe75c2012-10-23 10:25:10 +0200317{
318 return 0;
319}
320
321
William Lallemand1c2d6222012-10-30 15:52:53 +0100322int identity_reset(struct comp_ctx *comp_ctx)
William Lallemand82fe75c2012-10-23 10:25:10 +0200323{
324 return 0;
325}
326
327/*
328 * Deinit the algorithm
329 */
William Lallemand1c2d6222012-10-30 15:52:53 +0100330int identity_end(struct comp_ctx *comp_ctx)
William Lallemand82fe75c2012-10-23 10:25:10 +0200331{
332 return 0;
333}
334
335
336#ifdef USE_ZLIB
William Lallemand2b502472012-10-30 14:30:39 +0100337/*
338 * This is a tricky allocation function using the zlib.
339 * This is based on the allocation order in deflateInit2.
340 */
341static void *alloc_zlib(void *opaque, unsigned int items, unsigned int size)
342{
343 struct comp_ctx *ctx = opaque;
344 static char round = 0; /* order in deflateInit2 */
345 void *buf = NULL;
346
William Lallemand9d5f5482012-11-07 16:12:57 +0100347 if (global.maxzlibmem > 0 && zlib_memory_available < items * size){
348 buf = NULL;
349 goto end;
350 }
351
William Lallemand2b502472012-10-30 14:30:39 +0100352 switch (round) {
353 case 0:
354 if (zlib_pool_deflate_state == NULL)
355 zlib_pool_deflate_state = create_pool("zlib_state", size * items, MEM_F_SHARED);
356 ctx->zlib_deflate_state = buf = pool_alloc2(zlib_pool_deflate_state);
357 break;
358
359 case 1:
360 if (zlib_pool_window == NULL)
361 zlib_pool_window = create_pool("zlib_window", size * items, MEM_F_SHARED);
362 ctx->zlib_window = buf = pool_alloc2(zlib_pool_window);
363 break;
364
365 case 2:
366 if (zlib_pool_prev == NULL)
367 zlib_pool_prev = create_pool("zlib_prev", size * items, MEM_F_SHARED);
368 ctx->zlib_prev = buf = pool_alloc2(zlib_pool_prev);
369 break;
370
371 case 3:
372 if (zlib_pool_head == NULL)
373 zlib_pool_head = create_pool("zlib_head", size * items, MEM_F_SHARED);
374 ctx->zlib_head = buf = pool_alloc2(zlib_pool_head);
375 break;
376
377 case 4:
378 if (zlib_pool_pending_buf == NULL)
379 zlib_pool_pending_buf = create_pool("zlib_pending_buf", size * items, MEM_F_SHARED);
380 ctx->zlib_pending_buf = buf = pool_alloc2(zlib_pool_pending_buf);
381 break;
382 }
William Lallemand9d5f5482012-11-07 16:12:57 +0100383 if (buf != NULL && global.maxzlibmem > 0)
384 zlib_memory_available -= items * size;
385
386end:
William Lallemand2b502472012-10-30 14:30:39 +0100387
388 round = (round + 1) % 5; /* there are 5 zalloc call in deflateInit2 */
389 return buf;
390}
391
392static void free_zlib(void *opaque, void *ptr)
393{
394 struct comp_ctx *ctx = opaque;
William Lallemand9d5f5482012-11-07 16:12:57 +0100395 struct pool_head *pool;
William Lallemand2b502472012-10-30 14:30:39 +0100396
397 if (ptr == ctx->zlib_window)
William Lallemand9d5f5482012-11-07 16:12:57 +0100398 pool = zlib_pool_window;
William Lallemand2b502472012-10-30 14:30:39 +0100399 else if (ptr == ctx->zlib_deflate_state)
William Lallemand9d5f5482012-11-07 16:12:57 +0100400 pool = zlib_pool_deflate_state;
William Lallemand2b502472012-10-30 14:30:39 +0100401 else if (ptr == ctx->zlib_prev)
William Lallemand9d5f5482012-11-07 16:12:57 +0100402 pool = zlib_pool_prev;
William Lallemand2b502472012-10-30 14:30:39 +0100403 else if (ptr == ctx->zlib_head)
William Lallemand9d5f5482012-11-07 16:12:57 +0100404 pool = zlib_pool_head;
William Lallemand2b502472012-10-30 14:30:39 +0100405 else if (ptr == ctx->zlib_pending_buf)
William Lallemand9d5f5482012-11-07 16:12:57 +0100406 pool = zlib_pool_pending_buf;
William Lallemand2b502472012-10-30 14:30:39 +0100407
William Lallemand9d5f5482012-11-07 16:12:57 +0100408 pool_free2(pool, ptr);
409 if (global.maxzlibmem > 0)
410 zlib_memory_available += pool->size;
William Lallemand2b502472012-10-30 14:30:39 +0100411}
412
William Lallemand82fe75c2012-10-23 10:25:10 +0200413
414/**************************
415**** gzip algorithm ****
416***************************/
William Lallemand1c2d6222012-10-30 15:52:53 +0100417int gzip_init(struct comp_ctx *comp_ctx, int level)
William Lallemand82fe75c2012-10-23 10:25:10 +0200418{
William Lallemand1c2d6222012-10-30 15:52:53 +0100419 z_stream *strm = &comp_ctx->strm;
William Lallemand82fe75c2012-10-23 10:25:10 +0200420
William Lallemand9d5f5482012-11-07 16:12:57 +0100421 if (global.maxzlibmem > 0 && zlib_memory_available < 0)
422 zlib_memory_available = global.maxzlibmem * 1024 * 1024; /* Megabytes to bytes */
423
William Lallemand2b502472012-10-30 14:30:39 +0100424 strm->zalloc = alloc_zlib;
425 strm->zfree = free_zlib;
426 strm->opaque = comp_ctx;
William Lallemand82fe75c2012-10-23 10:25:10 +0200427
William Lallemanda509e4c2012-11-07 16:54:34 +0100428 if (deflateInit2(&comp_ctx->strm, level, Z_DEFLATED, global.tune.zlibwindowsize + 16, global.tune.zlibmemlevel, Z_DEFAULT_STRATEGY) != Z_OK)
William Lallemand82fe75c2012-10-23 10:25:10 +0200429 return -1;
430
431 return 0;
432}
433/**************************
434**** Deflate algorithm ****
435***************************/
436
William Lallemand1c2d6222012-10-30 15:52:53 +0100437int deflate_init(struct comp_ctx *comp_ctx, int level)
William Lallemand82fe75c2012-10-23 10:25:10 +0200438{
William Lallemand1c2d6222012-10-30 15:52:53 +0100439 z_stream *strm = &comp_ctx->strm;
William Lallemand82fe75c2012-10-23 10:25:10 +0200440
William Lallemand2b502472012-10-30 14:30:39 +0100441 strm->zalloc = alloc_zlib;
442 strm->zfree = free_zlib;
443 strm->opaque = comp_ctx;
William Lallemand82fe75c2012-10-23 10:25:10 +0200444
William Lallemand1c2d6222012-10-30 15:52:53 +0100445 if (deflateInit(&comp_ctx->strm, level) != Z_OK)
William Lallemand82fe75c2012-10-23 10:25:10 +0200446 return -1;
447
448 return 0;
449}
450
William Lallemand1c2d6222012-10-30 15:52:53 +0100451int deflate_add_data(struct comp_ctx *comp_ctx, const char *in_data, int in_len, char *out_data, int out_len)
William Lallemand82fe75c2012-10-23 10:25:10 +0200452{
William Lallemand1c2d6222012-10-30 15:52:53 +0100453 z_stream *strm = &comp_ctx->strm;
William Lallemand82fe75c2012-10-23 10:25:10 +0200454 int ret;
455
456 if (in_len <= 0)
457 return 0;
458
459
460 if (out_len <= 0)
461 return -1;
462
William Lallemand82fe75c2012-10-23 10:25:10 +0200463 strm->next_in = (unsigned char *)in_data;
464 strm->avail_in = in_len;
465 strm->next_out = (unsigned char *)out_data;
466 strm->avail_out = out_len;
467
468 ret = deflate(strm, Z_NO_FLUSH);
469 if (ret != Z_OK)
470 return -1;
471
472 /* deflate update the available data out */
473
474 return out_len - strm->avail_out;
475}
476
William Lallemand1c2d6222012-10-30 15:52:53 +0100477int deflate_flush(struct comp_ctx *comp_ctx, struct buffer *out, int flag)
William Lallemand82fe75c2012-10-23 10:25:10 +0200478{
479 int ret;
William Lallemand82fe75c2012-10-23 10:25:10 +0200480 int out_len = 0;
William Lallemand1c2d6222012-10-30 15:52:53 +0100481 z_stream *strm = &comp_ctx->strm;
William Lallemand82fe75c2012-10-23 10:25:10 +0200482
William Lallemand82fe75c2012-10-23 10:25:10 +0200483 strm->next_out = (unsigned char *)bi_end(out);
484 strm->avail_out = out->size - buffer_len(out);
485
486 ret = deflate(strm, flag);
487 if (ret != Z_OK && ret != Z_STREAM_END)
488 return -1;
489
490 out_len = (out->size - buffer_len(out)) - strm->avail_out;
491 out->i += out_len;
492
493 return out_len;
494}
495
William Lallemand1c2d6222012-10-30 15:52:53 +0100496int deflate_reset(struct comp_ctx *comp_ctx)
William Lallemand82fe75c2012-10-23 10:25:10 +0200497{
William Lallemand1c2d6222012-10-30 15:52:53 +0100498 z_stream *strm = &comp_ctx->strm;
William Lallemand82fe75c2012-10-23 10:25:10 +0200499
William Lallemand82fe75c2012-10-23 10:25:10 +0200500 if (deflateReset(strm) == Z_OK)
501 return 0;
502 return -1;
503}
504
William Lallemand1c2d6222012-10-30 15:52:53 +0100505int deflate_end(struct comp_ctx *comp_ctx)
William Lallemand82fe75c2012-10-23 10:25:10 +0200506{
William Lallemand1c2d6222012-10-30 15:52:53 +0100507 z_stream *strm = &comp_ctx->strm;
William Lallemand82fe75c2012-10-23 10:25:10 +0200508
William Lallemand9d5f5482012-11-07 16:12:57 +0100509 if (deflateEnd(strm) != Z_OK)
510 return -1;
William Lallemand82fe75c2012-10-23 10:25:10 +0200511
William Lallemand9d5f5482012-11-07 16:12:57 +0100512 return 0;
William Lallemand82fe75c2012-10-23 10:25:10 +0200513}
514
515#endif /* USE_ZLIB */
516