William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 1 | /* |
| 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 Tarreau | 3476364 | 2012-10-26 15:05:35 +0200 | [diff] [blame] | 15 | |
William Lallemand | 08289f1 | 2012-10-31 11:19:18 +0100 | [diff] [blame] | 16 | #ifdef USE_ZLIB |
Willy Tarreau | 3476364 | 2012-10-26 15:05:35 +0200 | [diff] [blame] | 17 | /* 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 Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 23 | #include <zlib.h> |
Willy Tarreau | 3476364 | 2012-10-26 15:05:35 +0200 | [diff] [blame] | 24 | #undef free_func |
William Lallemand | 08289f1 | 2012-10-31 11:19:18 +0100 | [diff] [blame] | 25 | #endif /* USE_ZLIB */ |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 26 | |
| 27 | #include <common/compat.h> |
William Lallemand | 2b50247 | 2012-10-30 14:30:39 +0100 | [diff] [blame] | 28 | #include <common/memory.h> |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 29 | |
| 30 | #include <types/global.h> |
| 31 | #include <types/compression.h> |
| 32 | |
William Lallemand | 727db8b | 2013-04-20 17:33:20 +0200 | [diff] [blame] | 33 | #include <proto/acl.h> |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 34 | #include <proto/compression.h> |
William Lallemand | d85f917 | 2012-11-09 17:05:39 +0100 | [diff] [blame] | 35 | #include <proto/freq_ctr.h> |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 36 | #include <proto/proto_http.h> |
| 37 | |
William Lallemand | 2b50247 | 2012-10-30 14:30:39 +0100 | [diff] [blame] | 38 | |
| 39 | #ifdef USE_ZLIB |
| 40 | |
William Lallemand | 8b52bb3 | 2012-11-16 18:06:41 +0100 | [diff] [blame] | 41 | static void *alloc_zlib(void *opaque, unsigned int items, unsigned int size); |
| 42 | static void free_zlib(void *opaque, void *ptr); |
| 43 | |
William Lallemand | 2b50247 | 2012-10-30 14:30:39 +0100 | [diff] [blame] | 44 | /* zlib allocation */ |
| 45 | static struct pool_head *zlib_pool_deflate_state = NULL; |
| 46 | static struct pool_head *zlib_pool_window = NULL; |
| 47 | static struct pool_head *zlib_pool_prev = NULL; |
| 48 | static struct pool_head *zlib_pool_head = NULL; |
| 49 | static struct pool_head *zlib_pool_pending_buf = NULL; |
| 50 | |
William Lallemand | e3a7d99 | 2012-11-20 11:25:20 +0100 | [diff] [blame] | 51 | long zlib_used_memory = 0; |
William Lallemand | 9d5f548 | 2012-11-07 16:12:57 +0100 | [diff] [blame] | 52 | |
William Lallemand | 2b50247 | 2012-10-30 14:30:39 +0100 | [diff] [blame] | 53 | #endif |
| 54 | |
William Lallemand | 072a2bf | 2012-11-20 17:01:01 +0100 | [diff] [blame] | 55 | unsigned int compress_min_idle = 0; |
William Lallemand | 8b52bb3 | 2012-11-16 18:06:41 +0100 | [diff] [blame] | 56 | static struct pool_head *pool_comp_ctx = NULL; |
| 57 | |
William Lallemand | 2b50247 | 2012-10-30 14:30:39 +0100 | [diff] [blame] | 58 | |
Cyril Bonté | 6162c43 | 2012-11-10 19:27:47 +0100 | [diff] [blame] | 59 | const struct comp_algo comp_algos[] = |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 60 | { |
| 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 | */ |
| 72 | int 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 | */ |
| 87 | int comp_append_algo(struct comp *comp, const char *algo) |
| 88 | { |
| 89 | struct comp_algo *comp_algo; |
| 90 | int i; |
| 91 | |
| 92 | for (i = 0; comp_algos[i].name; i++) { |
| 93 | if (!strcmp(algo, comp_algos[i].name)) { |
| 94 | comp_algo = calloc(1, sizeof(struct comp_algo)); |
| 95 | memmove(comp_algo, &comp_algos[i], sizeof(struct comp_algo)); |
| 96 | comp_algo->next = comp->algos; |
| 97 | comp->algos = comp_algo; |
| 98 | return 0; |
| 99 | } |
| 100 | } |
| 101 | return -1; |
| 102 | } |
| 103 | |
| 104 | /* emit the chunksize followed by a CRLF on the output and return the number of |
| 105 | * bytes written. Appends <add_crlf> additional CRLF after the first one. Chunk |
| 106 | * sizes are truncated to 6 hex digits (16 MB) and padded left. The caller is |
| 107 | * responsible for ensuring there is enough room left in the output buffer for |
| 108 | * the string (8 bytes * add_crlf*2). |
| 109 | */ |
| 110 | int http_emit_chunk_size(char *out, unsigned int chksz, int add_crlf) |
| 111 | { |
| 112 | int shift; |
| 113 | int pos = 0; |
| 114 | |
| 115 | for (shift = 20; shift >= 0; shift -= 4) |
| 116 | out[pos++] = hextab[(chksz >> shift) & 0xF]; |
| 117 | |
| 118 | do { |
| 119 | out[pos++] = '\r'; |
| 120 | out[pos++] = '\n'; |
| 121 | } while (--add_crlf >= 0); |
| 122 | |
| 123 | return pos; |
| 124 | } |
| 125 | |
| 126 | /* |
| 127 | * Init HTTP compression |
| 128 | */ |
| 129 | int http_compression_buffer_init(struct session *s, struct buffer *in, struct buffer *out) |
| 130 | { |
| 131 | struct http_msg *msg = &s->txn.rsp; |
| 132 | int left; |
| 133 | |
| 134 | /* not enough space */ |
| 135 | if (in->size - buffer_len(in) < 40) |
| 136 | return -1; |
| 137 | |
| 138 | /* |
| 139 | * Skip data, we don't need them in the new buffer. They are results |
| 140 | * of CHUNK_CRLF and CHUNK_SIZE parsing. |
| 141 | */ |
| 142 | b_adv(in, msg->next); |
| 143 | msg->next = 0; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 144 | |
| 145 | out->size = global.tune.bufsize; |
| 146 | out->i = 0; |
| 147 | out->o = 0; |
| 148 | out->p = out->data; |
| 149 | /* copy output data */ |
| 150 | if (in->o > 0) { |
| 151 | left = in->o - bo_contig_data(in); |
| 152 | memcpy(out->data, bo_ptr(in), bo_contig_data(in)); |
| 153 | out->p += bo_contig_data(in); |
| 154 | if (left > 0) { /* second part of the buffer */ |
| 155 | memcpy(out->p, in->data, left); |
| 156 | out->p += left; |
| 157 | } |
| 158 | out->o = in->o; |
| 159 | } |
| 160 | out->i += http_emit_chunk_size(out->p, 0, 0); |
| 161 | |
| 162 | return 0; |
| 163 | } |
| 164 | |
| 165 | /* |
| 166 | * Add data to compress |
| 167 | */ |
| 168 | int http_compression_buffer_add_data(struct session *s, struct buffer *in, struct buffer *out) |
| 169 | { |
| 170 | struct http_msg *msg = &s->txn.rsp; |
William Lallemand | bf3ae61 | 2012-11-19 12:35:37 +0100 | [diff] [blame] | 171 | int consumed_data = 0; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 172 | int data_process_len; |
| 173 | int left; |
| 174 | int ret; |
| 175 | |
| 176 | /* |
| 177 | * Skip data, we don't need them in the new buffer. They are results |
| 178 | * of CHUNK_CRLF and CHUNK_SIZE parsing. |
| 179 | */ |
| 180 | b_adv(in, msg->next); |
| 181 | msg->next = 0; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 182 | |
| 183 | /* |
| 184 | * select the smallest size between the announced chunk size, the input |
| 185 | * data, and the available output buffer size |
| 186 | */ |
| 187 | data_process_len = MIN(in->i, msg->chunk_len); |
| 188 | data_process_len = MIN(out->size - buffer_len(out), data_process_len); |
| 189 | |
| 190 | left = data_process_len - bi_contig_data(in); |
| 191 | if (left <= 0) { |
William Lallemand | 8b52bb3 | 2012-11-16 18:06:41 +0100 | [diff] [blame] | 192 | consumed_data += ret = s->comp_algo->add_data(s->comp_ctx, bi_ptr(in), data_process_len, out); |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 193 | if (ret < 0) |
| 194 | return -1; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 195 | |
| 196 | } else { |
William Lallemand | 8b52bb3 | 2012-11-16 18:06:41 +0100 | [diff] [blame] | 197 | consumed_data += ret = s->comp_algo->add_data(s->comp_ctx, bi_ptr(in), bi_contig_data(in), out); |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 198 | if (ret < 0) |
| 199 | return -1; |
William Lallemand | 8b52bb3 | 2012-11-16 18:06:41 +0100 | [diff] [blame] | 200 | consumed_data += ret = s->comp_algo->add_data(s->comp_ctx, in->data, left, out); |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 201 | if (ret < 0) |
| 202 | return -1; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | b_adv(in, data_process_len); |
| 206 | msg->chunk_len -= data_process_len; |
| 207 | |
William Lallemand | bf3ae61 | 2012-11-19 12:35:37 +0100 | [diff] [blame] | 208 | return consumed_data; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | /* |
| 212 | * Flush data in process, and write the header and footer of the chunk. Upon |
| 213 | * success, in and out buffers are swapped to avoid a copy. |
| 214 | */ |
| 215 | int http_compression_buffer_end(struct session *s, struct buffer **in, struct buffer **out, int end) |
| 216 | { |
Willy Tarreau | 55058a7 | 2012-11-21 08:27:21 +0100 | [diff] [blame] | 217 | int to_forward, forwarded; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 218 | int left; |
| 219 | struct http_msg *msg = &s->txn.rsp; |
| 220 | struct buffer *ib = *in, *ob = *out; |
William Lallemand | 08289f1 | 2012-10-31 11:19:18 +0100 | [diff] [blame] | 221 | |
| 222 | #ifdef USE_ZLIB |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 223 | int ret; |
| 224 | |
| 225 | /* flush data here */ |
| 226 | |
| 227 | if (end) |
William Lallemand | 8b52bb3 | 2012-11-16 18:06:41 +0100 | [diff] [blame] | 228 | ret = s->comp_algo->flush(s->comp_ctx, ob, Z_FINISH); /* end of data */ |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 229 | else |
William Lallemand | 8b52bb3 | 2012-11-16 18:06:41 +0100 | [diff] [blame] | 230 | ret = s->comp_algo->flush(s->comp_ctx, ob, Z_SYNC_FLUSH); /* end of buffer */ |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 231 | |
| 232 | if (ret < 0) |
| 233 | return -1; /* flush failed */ |
| 234 | |
William Lallemand | 08289f1 | 2012-10-31 11:19:18 +0100 | [diff] [blame] | 235 | #endif /* USE_ZLIB */ |
| 236 | |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 237 | if (ob->i > 8) { |
| 238 | /* more than a chunk size => some data were emitted */ |
| 239 | char *tail = ob->p + ob->i; |
| 240 | |
| 241 | /* write real size at the begining of the chunk, no need of wrapping */ |
| 242 | http_emit_chunk_size(ob->p, ob->i - 8, 0); |
| 243 | |
| 244 | /* chunked encoding requires CRLF after data */ |
| 245 | *tail++ = '\r'; |
| 246 | *tail++ = '\n'; |
| 247 | |
| 248 | if (!(msg->flags & HTTP_MSGF_TE_CHNK) && msg->chunk_len == 0) { |
| 249 | /* End of data, 0<CRLF><CRLF> is needed but we're not |
| 250 | * in chunked mode on input so we must add it ourselves. |
| 251 | */ |
| 252 | memcpy(tail, "0\r\n\r\n", 5); |
| 253 | tail += 5; |
| 254 | } |
| 255 | ob->i = tail - ob->p; |
| 256 | } else { |
| 257 | /* no data were sent, cancel the chunk size */ |
| 258 | ob->i = 0; |
| 259 | } |
| 260 | |
| 261 | to_forward = ob->i; |
Willy Tarreau | 55058a7 | 2012-11-21 08:27:21 +0100 | [diff] [blame] | 262 | |
William Lallemand | d85f917 | 2012-11-09 17:05:39 +0100 | [diff] [blame] | 263 | /* update input rate */ |
Willy Tarreau | 55058a7 | 2012-11-21 08:27:21 +0100 | [diff] [blame] | 264 | forwarded = ib->o - ob->o; |
| 265 | if (s->comp_ctx && s->comp_ctx->cur_lvl > 0) { |
| 266 | update_freq_ctr(&global.comp_bps_in, forwarded); |
| 267 | s->fe->fe_counters.comp_in += forwarded; |
| 268 | s->be->be_counters.comp_in += forwarded; |
| 269 | } else { |
| 270 | s->fe->fe_counters.comp_byp += forwarded; |
| 271 | s->be->be_counters.comp_byp += forwarded; |
| 272 | } |
William Lallemand | d85f917 | 2012-11-09 17:05:39 +0100 | [diff] [blame] | 273 | |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 274 | /* copy the remaining data in the tmp buffer. */ |
| 275 | if (ib->i > 0) { |
| 276 | left = ib->i - bi_contig_data(ib); |
| 277 | memcpy(bi_end(ob), bi_ptr(ib), bi_contig_data(ib)); |
| 278 | ob->i += bi_contig_data(ib); |
| 279 | if (left > 0) { |
| 280 | memcpy(bi_end(ob), ib->data, left); |
| 281 | ob->i += left; |
| 282 | } |
| 283 | } |
| 284 | |
| 285 | /* swap the buffers */ |
| 286 | *in = ob; |
| 287 | *out = ib; |
| 288 | |
Willy Tarreau | 55058a7 | 2012-11-21 08:27:21 +0100 | [diff] [blame] | 289 | if (s->comp_ctx && s->comp_ctx->cur_lvl > 0) { |
William Lallemand | d85f917 | 2012-11-09 17:05:39 +0100 | [diff] [blame] | 290 | update_freq_ctr(&global.comp_bps_out, to_forward); |
Willy Tarreau | 55058a7 | 2012-11-21 08:27:21 +0100 | [diff] [blame] | 291 | s->fe->fe_counters.comp_out += to_forward; |
| 292 | s->be->be_counters.comp_out += to_forward; |
| 293 | } |
William Lallemand | d85f917 | 2012-11-09 17:05:39 +0100 | [diff] [blame] | 294 | |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 295 | /* forward the new chunk without remaining data */ |
| 296 | b_adv(ob, to_forward); |
| 297 | |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 298 | return to_forward; |
| 299 | } |
| 300 | |
William Lallemand | 8b52bb3 | 2012-11-16 18:06:41 +0100 | [diff] [blame] | 301 | /* |
| 302 | * Alloc the comp_ctx |
| 303 | */ |
| 304 | static inline int init_comp_ctx(struct comp_ctx **comp_ctx) |
| 305 | { |
| 306 | #ifdef USE_ZLIB |
| 307 | z_stream *strm; |
| 308 | |
William Lallemand | e3a7d99 | 2012-11-20 11:25:20 +0100 | [diff] [blame] | 309 | if (global.maxzlibmem > 0 && (global.maxzlibmem - zlib_used_memory) < sizeof(struct comp_ctx)) |
William Lallemand | 8b52bb3 | 2012-11-16 18:06:41 +0100 | [diff] [blame] | 310 | return -1; |
| 311 | #endif |
| 312 | |
| 313 | if (unlikely(pool_comp_ctx == NULL)) |
| 314 | pool_comp_ctx = create_pool("comp_ctx", sizeof(struct comp_ctx), MEM_F_SHARED); |
| 315 | |
| 316 | *comp_ctx = pool_alloc2(pool_comp_ctx); |
| 317 | if (*comp_ctx == NULL) |
| 318 | return -1; |
| 319 | #ifdef USE_ZLIB |
William Lallemand | e3a7d99 | 2012-11-20 11:25:20 +0100 | [diff] [blame] | 320 | zlib_used_memory += sizeof(struct comp_ctx); |
William Lallemand | 8b52bb3 | 2012-11-16 18:06:41 +0100 | [diff] [blame] | 321 | |
| 322 | strm = &(*comp_ctx)->strm; |
| 323 | strm->zalloc = alloc_zlib; |
| 324 | strm->zfree = free_zlib; |
| 325 | strm->opaque = *comp_ctx; |
| 326 | #endif |
| 327 | return 0; |
| 328 | } |
| 329 | |
| 330 | /* |
| 331 | * Dealloc the comp_ctx |
| 332 | */ |
| 333 | static inline int deinit_comp_ctx(struct comp_ctx **comp_ctx) |
| 334 | { |
| 335 | if (!*comp_ctx) |
| 336 | return 0; |
| 337 | |
| 338 | pool_free2(pool_comp_ctx, *comp_ctx); |
| 339 | *comp_ctx = NULL; |
| 340 | |
| 341 | #ifdef USE_ZLIB |
William Lallemand | e3a7d99 | 2012-11-20 11:25:20 +0100 | [diff] [blame] | 342 | zlib_used_memory -= sizeof(struct comp_ctx); |
William Lallemand | 8b52bb3 | 2012-11-16 18:06:41 +0100 | [diff] [blame] | 343 | #endif |
William Lallemand | 8b52bb3 | 2012-11-16 18:06:41 +0100 | [diff] [blame] | 344 | return 0; |
| 345 | } |
| 346 | |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 347 | |
| 348 | /**************************** |
| 349 | **** Identity algorithm **** |
| 350 | ****************************/ |
| 351 | |
| 352 | /* |
| 353 | * Init the identity algorithm |
| 354 | */ |
William Lallemand | 8b52bb3 | 2012-11-16 18:06:41 +0100 | [diff] [blame] | 355 | int identity_init(struct comp_ctx **comp_ctx, int level) |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 356 | { |
| 357 | return 0; |
| 358 | } |
| 359 | |
| 360 | /* |
| 361 | * Process data |
William Lallemand | bf3ae61 | 2012-11-19 12:35:37 +0100 | [diff] [blame] | 362 | * Return size of consumed data or -1 on error |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 363 | */ |
William Lallemand | bf3ae61 | 2012-11-19 12:35:37 +0100 | [diff] [blame] | 364 | int identity_add_data(struct comp_ctx *comp_ctx, const char *in_data, int in_len, struct buffer *out) |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 365 | { |
William Lallemand | bf3ae61 | 2012-11-19 12:35:37 +0100 | [diff] [blame] | 366 | char *out_data = bi_end(out); |
| 367 | int out_len = out->size - buffer_len(out); |
| 368 | |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 369 | if (out_len < in_len) |
| 370 | return -1; |
| 371 | |
| 372 | memcpy(out_data, in_data, in_len); |
| 373 | |
William Lallemand | bf3ae61 | 2012-11-19 12:35:37 +0100 | [diff] [blame] | 374 | out->i += in_len; |
| 375 | |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 376 | return in_len; |
| 377 | } |
| 378 | |
William Lallemand | 1c2d622 | 2012-10-30 15:52:53 +0100 | [diff] [blame] | 379 | int identity_flush(struct comp_ctx *comp_ctx, struct buffer *out, int flag) |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 380 | { |
| 381 | return 0; |
| 382 | } |
| 383 | |
William Lallemand | 1c2d622 | 2012-10-30 15:52:53 +0100 | [diff] [blame] | 384 | int identity_reset(struct comp_ctx *comp_ctx) |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 385 | { |
| 386 | return 0; |
| 387 | } |
| 388 | |
| 389 | /* |
| 390 | * Deinit the algorithm |
| 391 | */ |
William Lallemand | 8b52bb3 | 2012-11-16 18:06:41 +0100 | [diff] [blame] | 392 | int identity_end(struct comp_ctx **comp_ctx) |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 393 | { |
| 394 | return 0; |
| 395 | } |
| 396 | |
| 397 | |
| 398 | #ifdef USE_ZLIB |
William Lallemand | 2b50247 | 2012-10-30 14:30:39 +0100 | [diff] [blame] | 399 | /* |
| 400 | * This is a tricky allocation function using the zlib. |
| 401 | * This is based on the allocation order in deflateInit2. |
| 402 | */ |
| 403 | static void *alloc_zlib(void *opaque, unsigned int items, unsigned int size) |
| 404 | { |
| 405 | struct comp_ctx *ctx = opaque; |
| 406 | static char round = 0; /* order in deflateInit2 */ |
| 407 | void *buf = NULL; |
| 408 | |
William Lallemand | e3a7d99 | 2012-11-20 11:25:20 +0100 | [diff] [blame] | 409 | if (global.maxzlibmem > 0 && (global.maxzlibmem - zlib_used_memory) < (long)(items * size)) |
William Lallemand | 9d5f548 | 2012-11-07 16:12:57 +0100 | [diff] [blame] | 410 | goto end; |
William Lallemand | 9d5f548 | 2012-11-07 16:12:57 +0100 | [diff] [blame] | 411 | |
William Lallemand | 2b50247 | 2012-10-30 14:30:39 +0100 | [diff] [blame] | 412 | switch (round) { |
| 413 | case 0: |
| 414 | if (zlib_pool_deflate_state == NULL) |
| 415 | zlib_pool_deflate_state = create_pool("zlib_state", size * items, MEM_F_SHARED); |
| 416 | ctx->zlib_deflate_state = buf = pool_alloc2(zlib_pool_deflate_state); |
| 417 | break; |
| 418 | |
| 419 | case 1: |
| 420 | if (zlib_pool_window == NULL) |
| 421 | zlib_pool_window = create_pool("zlib_window", size * items, MEM_F_SHARED); |
| 422 | ctx->zlib_window = buf = pool_alloc2(zlib_pool_window); |
| 423 | break; |
| 424 | |
| 425 | case 2: |
| 426 | if (zlib_pool_prev == NULL) |
| 427 | zlib_pool_prev = create_pool("zlib_prev", size * items, MEM_F_SHARED); |
| 428 | ctx->zlib_prev = buf = pool_alloc2(zlib_pool_prev); |
| 429 | break; |
| 430 | |
| 431 | case 3: |
| 432 | if (zlib_pool_head == NULL) |
| 433 | zlib_pool_head = create_pool("zlib_head", size * items, MEM_F_SHARED); |
| 434 | ctx->zlib_head = buf = pool_alloc2(zlib_pool_head); |
| 435 | break; |
| 436 | |
| 437 | case 4: |
| 438 | if (zlib_pool_pending_buf == NULL) |
| 439 | zlib_pool_pending_buf = create_pool("zlib_pending_buf", size * items, MEM_F_SHARED); |
| 440 | ctx->zlib_pending_buf = buf = pool_alloc2(zlib_pool_pending_buf); |
| 441 | break; |
| 442 | } |
William Lallemand | e3a7d99 | 2012-11-20 11:25:20 +0100 | [diff] [blame] | 443 | if (buf != NULL) |
| 444 | zlib_used_memory += items * size; |
William Lallemand | 9d5f548 | 2012-11-07 16:12:57 +0100 | [diff] [blame] | 445 | |
| 446 | end: |
William Lallemand | 2b50247 | 2012-10-30 14:30:39 +0100 | [diff] [blame] | 447 | |
Willy Tarreau | 4690985 | 2012-11-15 14:57:56 +0100 | [diff] [blame] | 448 | /* deflateInit2() first allocates and checks the deflate_state, then if |
| 449 | * it succeeds, it allocates all other 4 areas at ones and checks them |
| 450 | * at the end. So we want to correctly count the rounds depending on when |
| 451 | * zlib is supposed to abort. |
| 452 | */ |
| 453 | if (buf || round) |
| 454 | round = (round + 1) % 5; |
William Lallemand | 2b50247 | 2012-10-30 14:30:39 +0100 | [diff] [blame] | 455 | return buf; |
| 456 | } |
| 457 | |
| 458 | static void free_zlib(void *opaque, void *ptr) |
| 459 | { |
| 460 | struct comp_ctx *ctx = opaque; |
Willy Tarreau | b1fbd05 | 2012-11-10 17:49:37 +0100 | [diff] [blame] | 461 | struct pool_head *pool = NULL; |
William Lallemand | 2b50247 | 2012-10-30 14:30:39 +0100 | [diff] [blame] | 462 | |
| 463 | if (ptr == ctx->zlib_window) |
William Lallemand | 9d5f548 | 2012-11-07 16:12:57 +0100 | [diff] [blame] | 464 | pool = zlib_pool_window; |
William Lallemand | 2b50247 | 2012-10-30 14:30:39 +0100 | [diff] [blame] | 465 | else if (ptr == ctx->zlib_deflate_state) |
William Lallemand | 9d5f548 | 2012-11-07 16:12:57 +0100 | [diff] [blame] | 466 | pool = zlib_pool_deflate_state; |
William Lallemand | 2b50247 | 2012-10-30 14:30:39 +0100 | [diff] [blame] | 467 | else if (ptr == ctx->zlib_prev) |
William Lallemand | 9d5f548 | 2012-11-07 16:12:57 +0100 | [diff] [blame] | 468 | pool = zlib_pool_prev; |
William Lallemand | 2b50247 | 2012-10-30 14:30:39 +0100 | [diff] [blame] | 469 | else if (ptr == ctx->zlib_head) |
William Lallemand | 9d5f548 | 2012-11-07 16:12:57 +0100 | [diff] [blame] | 470 | pool = zlib_pool_head; |
William Lallemand | 2b50247 | 2012-10-30 14:30:39 +0100 | [diff] [blame] | 471 | else if (ptr == ctx->zlib_pending_buf) |
William Lallemand | 9d5f548 | 2012-11-07 16:12:57 +0100 | [diff] [blame] | 472 | pool = zlib_pool_pending_buf; |
William Lallemand | 2b50247 | 2012-10-30 14:30:39 +0100 | [diff] [blame] | 473 | |
William Lallemand | 9d5f548 | 2012-11-07 16:12:57 +0100 | [diff] [blame] | 474 | pool_free2(pool, ptr); |
William Lallemand | e3a7d99 | 2012-11-20 11:25:20 +0100 | [diff] [blame] | 475 | zlib_used_memory -= pool->size; |
William Lallemand | 2b50247 | 2012-10-30 14:30:39 +0100 | [diff] [blame] | 476 | } |
| 477 | |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 478 | /************************** |
| 479 | **** gzip algorithm **** |
| 480 | ***************************/ |
William Lallemand | 8b52bb3 | 2012-11-16 18:06:41 +0100 | [diff] [blame] | 481 | int gzip_init(struct comp_ctx **comp_ctx, int level) |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 482 | { |
William Lallemand | 8b52bb3 | 2012-11-16 18:06:41 +0100 | [diff] [blame] | 483 | z_stream *strm; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 484 | |
William Lallemand | 8b52bb3 | 2012-11-16 18:06:41 +0100 | [diff] [blame] | 485 | if (init_comp_ctx(comp_ctx) < 0) |
| 486 | return -1; |
William Lallemand | 9d5f548 | 2012-11-07 16:12:57 +0100 | [diff] [blame] | 487 | |
William Lallemand | 8b52bb3 | 2012-11-16 18:06:41 +0100 | [diff] [blame] | 488 | strm = &(*comp_ctx)->strm; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 489 | |
William Lallemand | 8b52bb3 | 2012-11-16 18:06:41 +0100 | [diff] [blame] | 490 | if (deflateInit2(strm, level, Z_DEFLATED, global.tune.zlibwindowsize + 16, global.tune.zlibmemlevel, Z_DEFAULT_STRATEGY) != Z_OK) { |
| 491 | deinit_comp_ctx(comp_ctx); |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 492 | return -1; |
William Lallemand | 8b52bb3 | 2012-11-16 18:06:41 +0100 | [diff] [blame] | 493 | } |
| 494 | |
| 495 | (*comp_ctx)->cur_lvl = level; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 496 | |
| 497 | return 0; |
| 498 | } |
| 499 | /************************** |
| 500 | **** Deflate algorithm **** |
| 501 | ***************************/ |
| 502 | |
William Lallemand | 8b52bb3 | 2012-11-16 18:06:41 +0100 | [diff] [blame] | 503 | int deflate_init(struct comp_ctx **comp_ctx, int level) |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 504 | { |
William Lallemand | 8b52bb3 | 2012-11-16 18:06:41 +0100 | [diff] [blame] | 505 | z_stream *strm; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 506 | |
William Lallemand | 8b52bb3 | 2012-11-16 18:06:41 +0100 | [diff] [blame] | 507 | if (init_comp_ctx(comp_ctx) < 0) |
| 508 | return -1; |
| 509 | |
| 510 | strm = &(*comp_ctx)->strm; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 511 | |
Willy Tarreau | c5599e7 | 2013-04-28 08:52:52 +0200 | [diff] [blame] | 512 | if (deflateInit2(strm, level, Z_DEFLATED, global.tune.zlibwindowsize, global.tune.zlibmemlevel, Z_DEFAULT_STRATEGY) != Z_OK) { |
William Lallemand | 8b52bb3 | 2012-11-16 18:06:41 +0100 | [diff] [blame] | 513 | deinit_comp_ctx(comp_ctx); |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 514 | return -1; |
William Lallemand | 8b52bb3 | 2012-11-16 18:06:41 +0100 | [diff] [blame] | 515 | } |
| 516 | |
| 517 | (*comp_ctx)->cur_lvl = level; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 518 | |
| 519 | return 0; |
| 520 | } |
| 521 | |
William Lallemand | bf3ae61 | 2012-11-19 12:35:37 +0100 | [diff] [blame] | 522 | /* Return the size of consumed data or -1 */ |
| 523 | int deflate_add_data(struct comp_ctx *comp_ctx, const char *in_data, int in_len, struct buffer *out) |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 524 | { |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 525 | int ret; |
William Lallemand | bf3ae61 | 2012-11-19 12:35:37 +0100 | [diff] [blame] | 526 | z_stream *strm = &comp_ctx->strm; |
| 527 | char *out_data = bi_end(out); |
| 528 | int out_len = out->size - buffer_len(out); |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 529 | |
| 530 | if (in_len <= 0) |
| 531 | return 0; |
| 532 | |
| 533 | |
| 534 | if (out_len <= 0) |
| 535 | return -1; |
| 536 | |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 537 | strm->next_in = (unsigned char *)in_data; |
| 538 | strm->avail_in = in_len; |
| 539 | strm->next_out = (unsigned char *)out_data; |
| 540 | strm->avail_out = out_len; |
| 541 | |
| 542 | ret = deflate(strm, Z_NO_FLUSH); |
| 543 | if (ret != Z_OK) |
| 544 | return -1; |
| 545 | |
| 546 | /* deflate update the available data out */ |
William Lallemand | bf3ae61 | 2012-11-19 12:35:37 +0100 | [diff] [blame] | 547 | out->i += out_len - strm->avail_out; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 548 | |
William Lallemand | bf3ae61 | 2012-11-19 12:35:37 +0100 | [diff] [blame] | 549 | return in_len - strm->avail_in; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 550 | } |
| 551 | |
William Lallemand | 1c2d622 | 2012-10-30 15:52:53 +0100 | [diff] [blame] | 552 | int deflate_flush(struct comp_ctx *comp_ctx, struct buffer *out, int flag) |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 553 | { |
| 554 | int ret; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 555 | int out_len = 0; |
William Lallemand | 1c2d622 | 2012-10-30 15:52:53 +0100 | [diff] [blame] | 556 | z_stream *strm = &comp_ctx->strm; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 557 | |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 558 | strm->next_out = (unsigned char *)bi_end(out); |
| 559 | strm->avail_out = out->size - buffer_len(out); |
| 560 | |
| 561 | ret = deflate(strm, flag); |
| 562 | if (ret != Z_OK && ret != Z_STREAM_END) |
| 563 | return -1; |
| 564 | |
| 565 | out_len = (out->size - buffer_len(out)) - strm->avail_out; |
| 566 | out->i += out_len; |
| 567 | |
William Lallemand | 072a2bf | 2012-11-20 17:01:01 +0100 | [diff] [blame] | 568 | /* compression limit */ |
| 569 | if ((global.comp_rate_lim > 0 && (read_freq_ctr(&global.comp_bps_out) > global.comp_rate_lim)) || /* rate */ |
| 570 | (idle_pct < compress_min_idle)) { /* idle */ |
| 571 | /* decrease level */ |
| 572 | if (comp_ctx->cur_lvl > 0) { |
| 573 | comp_ctx->cur_lvl--; |
William Lallemand | d85f917 | 2012-11-09 17:05:39 +0100 | [diff] [blame] | 574 | deflateParams(&comp_ctx->strm, comp_ctx->cur_lvl, Z_DEFAULT_STRATEGY); |
| 575 | } |
William Lallemand | 072a2bf | 2012-11-20 17:01:01 +0100 | [diff] [blame] | 576 | |
| 577 | } else if (comp_ctx->cur_lvl < global.tune.comp_maxlevel) { |
| 578 | /* increase level */ |
| 579 | comp_ctx->cur_lvl++ ; |
| 580 | deflateParams(&comp_ctx->strm, comp_ctx->cur_lvl, Z_DEFAULT_STRATEGY); |
William Lallemand | d85f917 | 2012-11-09 17:05:39 +0100 | [diff] [blame] | 581 | } |
| 582 | |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 583 | return out_len; |
| 584 | } |
| 585 | |
William Lallemand | 1c2d622 | 2012-10-30 15:52:53 +0100 | [diff] [blame] | 586 | int deflate_reset(struct comp_ctx *comp_ctx) |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 587 | { |
William Lallemand | 1c2d622 | 2012-10-30 15:52:53 +0100 | [diff] [blame] | 588 | z_stream *strm = &comp_ctx->strm; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 589 | |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 590 | if (deflateReset(strm) == Z_OK) |
| 591 | return 0; |
| 592 | return -1; |
| 593 | } |
| 594 | |
William Lallemand | 8b52bb3 | 2012-11-16 18:06:41 +0100 | [diff] [blame] | 595 | int deflate_end(struct comp_ctx **comp_ctx) |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 596 | { |
William Lallemand | 8b52bb3 | 2012-11-16 18:06:41 +0100 | [diff] [blame] | 597 | z_stream *strm = &(*comp_ctx)->strm; |
| 598 | int ret; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 599 | |
William Lallemand | 8b52bb3 | 2012-11-16 18:06:41 +0100 | [diff] [blame] | 600 | ret = deflateEnd(strm); |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 601 | |
William Lallemand | 8b52bb3 | 2012-11-16 18:06:41 +0100 | [diff] [blame] | 602 | deinit_comp_ctx(comp_ctx); |
| 603 | |
| 604 | return ret; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 605 | } |
| 606 | |
| 607 | #endif /* USE_ZLIB */ |
| 608 | |
William Lallemand | 727db8b | 2013-04-20 17:33:20 +0200 | [diff] [blame] | 609 | /* boolean, returns true if compression is used (either gzip or deflate) in the response */ |
| 610 | static int |
| 611 | smp_fetch_res_comp(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Willy Tarreau | ef38c39 | 2013-07-22 16:29:32 +0200 | [diff] [blame] | 612 | const struct arg *args, struct sample *smp, const char *kw) |
William Lallemand | 727db8b | 2013-04-20 17:33:20 +0200 | [diff] [blame] | 613 | { |
| 614 | smp->type = SMP_T_BOOL; |
| 615 | smp->data.uint = (l4->comp_algo != NULL); |
| 616 | return 1; |
| 617 | } |
| 618 | |
| 619 | /* string, returns algo */ |
| 620 | static int |
| 621 | smp_fetch_res_comp_algo(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Willy Tarreau | ef38c39 | 2013-07-22 16:29:32 +0200 | [diff] [blame] | 622 | const struct arg *args, struct sample *smp, const char *kw) |
William Lallemand | 727db8b | 2013-04-20 17:33:20 +0200 | [diff] [blame] | 623 | { |
| 624 | if (!l4->comp_algo) |
| 625 | return 0; |
| 626 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 627 | smp->type = SMP_T_STR; |
| 628 | smp->flags = SMP_F_CONST; |
William Lallemand | 727db8b | 2013-04-20 17:33:20 +0200 | [diff] [blame] | 629 | smp->data.str.str = l4->comp_algo->name; |
| 630 | smp->data.str.len = l4->comp_algo->name_len; |
| 631 | return 1; |
| 632 | } |
| 633 | |
| 634 | /* Note: must not be declared <const> as its list will be overwritten */ |
Willy Tarreau | dc13c11 | 2013-06-21 23:16:39 +0200 | [diff] [blame] | 635 | static struct acl_kw_list acl_kws = {ILH, { |
Willy Tarreau | 7f6fa69 | 2013-04-23 19:39:43 +0200 | [diff] [blame] | 636 | { /* END */ }, |
William Lallemand | 727db8b | 2013-04-20 17:33:20 +0200 | [diff] [blame] | 637 | }}; |
| 638 | |
| 639 | /* Note: must not be declared <const> as its list will be overwritten */ |
Willy Tarreau | dc13c11 | 2013-06-21 23:16:39 +0200 | [diff] [blame] | 640 | static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, { |
William Lallemand | 727db8b | 2013-04-20 17:33:20 +0200 | [diff] [blame] | 641 | { "res.comp", smp_fetch_res_comp, 0, NULL, SMP_T_BOOL, SMP_USE_HRSHP }, |
| 642 | { "res.comp_algo", smp_fetch_res_comp_algo, 0, NULL, SMP_T_STR, SMP_USE_HRSHP }, |
| 643 | { /* END */ }, |
| 644 | }}; |
| 645 | |
| 646 | __attribute__((constructor)) |
| 647 | static void __comp_fetch_init(void) |
| 648 | { |
| 649 | acl_register_keywords(&acl_kws); |
| 650 | sample_register_fetches(&sample_fetch_keywords); |
| 651 | } |