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