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