blob: c20a4495ebc87748d3eebfd4e8b9792d61b7ddb1 [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>
28
29#include <types/global.h>
30#include <types/compression.h>
31
32#include <proto/compression.h>
33#include <proto/proto_http.h>
34
35static const struct comp_algo comp_algos[] =
36{
37 { "identity", 8, identity_init, identity_add_data, identity_flush, identity_reset, identity_end },
38#ifdef USE_ZLIB
39 { "deflate", 7, deflate_init, deflate_add_data, deflate_flush, deflate_reset, deflate_end },
40 { "gzip", 4, gzip_init, deflate_add_data, deflate_flush, deflate_reset, deflate_end },
41#endif /* USE_ZLIB */
42 { NULL, 0, NULL , NULL, NULL, NULL, NULL }
43};
44
45/*
46 * Add a content-type in the configuration
47 */
48int comp_append_type(struct comp *comp, const char *type)
49{
50 struct comp_type *comp_type;
51
52 comp_type = calloc(1, sizeof(struct comp_type));
53 comp_type->name_len = strlen(type);
54 comp_type->name = strdup(type);
55 comp_type->next = comp->types;
56 comp->types = comp_type;
57 return 0;
58}
59
60/*
61 * Add an algorithm in the configuration
62 */
63int comp_append_algo(struct comp *comp, const char *algo)
64{
65 struct comp_algo *comp_algo;
66 int i;
67
68 for (i = 0; comp_algos[i].name; i++) {
69 if (!strcmp(algo, comp_algos[i].name)) {
70 comp_algo = calloc(1, sizeof(struct comp_algo));
71 memmove(comp_algo, &comp_algos[i], sizeof(struct comp_algo));
72 comp_algo->next = comp->algos;
73 comp->algos = comp_algo;
74 return 0;
75 }
76 }
77 return -1;
78}
79
80/* emit the chunksize followed by a CRLF on the output and return the number of
81 * bytes written. Appends <add_crlf> additional CRLF after the first one. Chunk
82 * sizes are truncated to 6 hex digits (16 MB) and padded left. The caller is
83 * responsible for ensuring there is enough room left in the output buffer for
84 * the string (8 bytes * add_crlf*2).
85 */
86int http_emit_chunk_size(char *out, unsigned int chksz, int add_crlf)
87{
88 int shift;
89 int pos = 0;
90
91 for (shift = 20; shift >= 0; shift -= 4)
92 out[pos++] = hextab[(chksz >> shift) & 0xF];
93
94 do {
95 out[pos++] = '\r';
96 out[pos++] = '\n';
97 } while (--add_crlf >= 0);
98
99 return pos;
100}
101
102/*
103 * Init HTTP compression
104 */
105int http_compression_buffer_init(struct session *s, struct buffer *in, struct buffer *out)
106{
107 struct http_msg *msg = &s->txn.rsp;
108 int left;
109
110 /* not enough space */
111 if (in->size - buffer_len(in) < 40)
112 return -1;
113
114 /*
115 * Skip data, we don't need them in the new buffer. They are results
116 * of CHUNK_CRLF and CHUNK_SIZE parsing.
117 */
118 b_adv(in, msg->next);
119 msg->next = 0;
120 msg->sov = 0;
121 msg->sol = 0;
122
123 out->size = global.tune.bufsize;
124 out->i = 0;
125 out->o = 0;
126 out->p = out->data;
127 /* copy output data */
128 if (in->o > 0) {
129 left = in->o - bo_contig_data(in);
130 memcpy(out->data, bo_ptr(in), bo_contig_data(in));
131 out->p += bo_contig_data(in);
132 if (left > 0) { /* second part of the buffer */
133 memcpy(out->p, in->data, left);
134 out->p += left;
135 }
136 out->o = in->o;
137 }
138 out->i += http_emit_chunk_size(out->p, 0, 0);
139
140 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;
149 int data_process_len;
150 int left;
151 int ret;
152
153 /*
154 * Skip data, we don't need them in the new buffer. They are results
155 * of CHUNK_CRLF and CHUNK_SIZE parsing.
156 */
157 b_adv(in, msg->next);
158 msg->next = 0;
159 msg->sov = 0;
160 msg->sol = 0;
161
162 /*
163 * select the smallest size between the announced chunk size, the input
164 * data, and the available output buffer size
165 */
166 data_process_len = MIN(in->i, msg->chunk_len);
167 data_process_len = MIN(out->size - buffer_len(out), data_process_len);
168
169 left = data_process_len - bi_contig_data(in);
170 if (left <= 0) {
William Lallemand1c2d6222012-10-30 15:52:53 +0100171 ret = s->comp_algo->add_data(&s->comp_ctx, bi_ptr(in),
William Lallemand82fe75c2012-10-23 10:25:10 +0200172 data_process_len, bi_end(out),
173 out->size - buffer_len(out));
174 if (ret < 0)
175 return -1;
176 out->i += ret;
177
178 } else {
William Lallemand1c2d6222012-10-30 15:52:53 +0100179 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 +0200180 if (ret < 0)
181 return -1;
182 out->i += ret;
William Lallemand1c2d6222012-10-30 15:52:53 +0100183 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 +0200184 if (ret < 0)
185 return -1;
186 out->i += ret;
187 }
188
189 b_adv(in, data_process_len);
190 msg->chunk_len -= data_process_len;
191
192 return 0;
193}
194
195/*
196 * Flush data in process, and write the header and footer of the chunk. Upon
197 * success, in and out buffers are swapped to avoid a copy.
198 */
199int http_compression_buffer_end(struct session *s, struct buffer **in, struct buffer **out, int end)
200{
201 int to_forward;
202 int left;
203 struct http_msg *msg = &s->txn.rsp;
204 struct buffer *ib = *in, *ob = *out;
William Lallemand08289f12012-10-31 11:19:18 +0100205
206#ifdef USE_ZLIB
William Lallemand82fe75c2012-10-23 10:25:10 +0200207 int ret;
208
209 /* flush data here */
210
211 if (end)
212 ret = s->comp_algo->flush(&s->comp_ctx, ob, Z_FINISH); /* end of data */
213 else
214 ret = s->comp_algo->flush(&s->comp_ctx, ob, Z_SYNC_FLUSH); /* end of buffer */
215
216 if (ret < 0)
217 return -1; /* flush failed */
218
William Lallemand08289f12012-10-31 11:19:18 +0100219#endif /* USE_ZLIB */
220
William Lallemand82fe75c2012-10-23 10:25:10 +0200221 if (ob->i > 8) {
222 /* more than a chunk size => some data were emitted */
223 char *tail = ob->p + ob->i;
224
225 /* write real size at the begining of the chunk, no need of wrapping */
226 http_emit_chunk_size(ob->p, ob->i - 8, 0);
227
228 /* chunked encoding requires CRLF after data */
229 *tail++ = '\r';
230 *tail++ = '\n';
231
232 if (!(msg->flags & HTTP_MSGF_TE_CHNK) && msg->chunk_len == 0) {
233 /* End of data, 0<CRLF><CRLF> is needed but we're not
234 * in chunked mode on input so we must add it ourselves.
235 */
236 memcpy(tail, "0\r\n\r\n", 5);
237 tail += 5;
238 }
239 ob->i = tail - ob->p;
240 } else {
241 /* no data were sent, cancel the chunk size */
242 ob->i = 0;
243 }
244
245 to_forward = ob->i;
246
247 /* copy the remaining data in the tmp buffer. */
248 if (ib->i > 0) {
249 left = ib->i - bi_contig_data(ib);
250 memcpy(bi_end(ob), bi_ptr(ib), bi_contig_data(ib));
251 ob->i += bi_contig_data(ib);
252 if (left > 0) {
253 memcpy(bi_end(ob), ib->data, left);
254 ob->i += left;
255 }
256 }
257
258 /* swap the buffers */
259 *in = ob;
260 *out = ib;
261
262 /* forward the new chunk without remaining data */
263 b_adv(ob, to_forward);
264
265 /* if there are data between p and next, there are trailers, must forward them */
266 b_adv(ob, msg->next);
267 msg->next = 0;
268
269 return to_forward;
270}
271
272
273/****************************
274 **** Identity algorithm ****
275 ****************************/
276
277/*
278 * Init the identity algorithm
279 */
William Lallemand1c2d6222012-10-30 15:52:53 +0100280int identity_init(struct comp_ctx *comp_ctx, int level)
William Lallemand82fe75c2012-10-23 10:25:10 +0200281{
282 return 0;
283}
284
285/*
286 * Process data
287 * Return size of processed data or -1 on error
288 */
William Lallemand1c2d6222012-10-30 15:52:53 +0100289int 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 +0200290{
291 if (out_len < in_len)
292 return -1;
293
294 memcpy(out_data, in_data, in_len);
295
296 return in_len;
297}
298
William Lallemand1c2d6222012-10-30 15:52:53 +0100299int identity_flush(struct comp_ctx *comp_ctx, struct buffer *out, int flag)
William Lallemand82fe75c2012-10-23 10:25:10 +0200300{
301 return 0;
302}
303
304
William Lallemand1c2d6222012-10-30 15:52:53 +0100305int identity_reset(struct comp_ctx *comp_ctx)
William Lallemand82fe75c2012-10-23 10:25:10 +0200306{
307 return 0;
308}
309
310/*
311 * Deinit the algorithm
312 */
William Lallemand1c2d6222012-10-30 15:52:53 +0100313int identity_end(struct comp_ctx *comp_ctx)
William Lallemand82fe75c2012-10-23 10:25:10 +0200314{
315 return 0;
316}
317
318
319#ifdef USE_ZLIB
320
321/**************************
322**** gzip algorithm ****
323***************************/
William Lallemand1c2d6222012-10-30 15:52:53 +0100324int gzip_init(struct comp_ctx *comp_ctx, int level)
William Lallemand82fe75c2012-10-23 10:25:10 +0200325{
William Lallemand1c2d6222012-10-30 15:52:53 +0100326 z_stream *strm = &comp_ctx->strm;
William Lallemand82fe75c2012-10-23 10:25:10 +0200327
328 strm->zalloc = Z_NULL;
329 strm->zfree = Z_NULL;
330 strm->opaque = Z_NULL;
331
William Lallemanda509e4c2012-11-07 16:54:34 +0100332 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 +0200333 return -1;
334
335 return 0;
336}
337/**************************
338**** Deflate algorithm ****
339***************************/
340
William Lallemand1c2d6222012-10-30 15:52:53 +0100341int deflate_init(struct comp_ctx *comp_ctx, int level)
William Lallemand82fe75c2012-10-23 10:25:10 +0200342{
William Lallemand1c2d6222012-10-30 15:52:53 +0100343 z_stream *strm = &comp_ctx->strm;
William Lallemand82fe75c2012-10-23 10:25:10 +0200344
345 strm->zalloc = Z_NULL;
346 strm->zfree = Z_NULL;
347 strm->opaque = Z_NULL;
348
William Lallemand1c2d6222012-10-30 15:52:53 +0100349 if (deflateInit(&comp_ctx->strm, level) != Z_OK)
William Lallemand82fe75c2012-10-23 10:25:10 +0200350 return -1;
351
352 return 0;
353}
354
William Lallemand1c2d6222012-10-30 15:52:53 +0100355int 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 +0200356{
William Lallemand1c2d6222012-10-30 15:52:53 +0100357 z_stream *strm = &comp_ctx->strm;
William Lallemand82fe75c2012-10-23 10:25:10 +0200358 int ret;
359
360 if (in_len <= 0)
361 return 0;
362
363
364 if (out_len <= 0)
365 return -1;
366
William Lallemand82fe75c2012-10-23 10:25:10 +0200367 strm->next_in = (unsigned char *)in_data;
368 strm->avail_in = in_len;
369 strm->next_out = (unsigned char *)out_data;
370 strm->avail_out = out_len;
371
372 ret = deflate(strm, Z_NO_FLUSH);
373 if (ret != Z_OK)
374 return -1;
375
376 /* deflate update the available data out */
377
378 return out_len - strm->avail_out;
379}
380
William Lallemand1c2d6222012-10-30 15:52:53 +0100381int deflate_flush(struct comp_ctx *comp_ctx, struct buffer *out, int flag)
William Lallemand82fe75c2012-10-23 10:25:10 +0200382{
383 int ret;
William Lallemand82fe75c2012-10-23 10:25:10 +0200384 int out_len = 0;
William Lallemand1c2d6222012-10-30 15:52:53 +0100385 z_stream *strm = &comp_ctx->strm;
William Lallemand82fe75c2012-10-23 10:25:10 +0200386
William Lallemand82fe75c2012-10-23 10:25:10 +0200387 strm->next_out = (unsigned char *)bi_end(out);
388 strm->avail_out = out->size - buffer_len(out);
389
390 ret = deflate(strm, flag);
391 if (ret != Z_OK && ret != Z_STREAM_END)
392 return -1;
393
394 out_len = (out->size - buffer_len(out)) - strm->avail_out;
395 out->i += out_len;
396
397 return out_len;
398}
399
William Lallemand1c2d6222012-10-30 15:52:53 +0100400int deflate_reset(struct comp_ctx *comp_ctx)
William Lallemand82fe75c2012-10-23 10:25:10 +0200401{
William Lallemand1c2d6222012-10-30 15:52:53 +0100402 z_stream *strm = &comp_ctx->strm;
William Lallemand82fe75c2012-10-23 10:25:10 +0200403
William Lallemand82fe75c2012-10-23 10:25:10 +0200404 if (deflateReset(strm) == Z_OK)
405 return 0;
406 return -1;
407}
408
William Lallemand1c2d6222012-10-30 15:52:53 +0100409int deflate_end(struct comp_ctx *comp_ctx)
William Lallemand82fe75c2012-10-23 10:25:10 +0200410{
William Lallemand1c2d6222012-10-30 15:52:53 +0100411 z_stream *strm = &comp_ctx->strm;
William Lallemand82fe75c2012-10-23 10:25:10 +0200412
William Lallemand82fe75c2012-10-23 10:25:10 +0200413 if (deflateEnd(strm) == Z_OK)
414 return 0;
415
416 return -1;
417}
418
419#endif /* USE_ZLIB */
420