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 | |
Willy Tarreau | 418b8c0 | 2015-03-29 03:32:06 +0200 | [diff] [blame] | 16 | #if defined(USE_SLZ) |
| 17 | #include <slz.h> |
| 18 | #elif defined(USE_ZLIB) |
Willy Tarreau | 3476364 | 2012-10-26 15:05:35 +0200 | [diff] [blame] | 19 | /* Note: the crappy zlib and openssl libs both define the "free_func" type. |
| 20 | * That's a very clever idea to use such a generic name in general purpose |
| 21 | * libraries, really... The zlib one is easier to redefine than openssl's, |
| 22 | * so let's only fix this one. |
| 23 | */ |
| 24 | #define free_func zlib_free_func |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 25 | #include <zlib.h> |
Willy Tarreau | 3476364 | 2012-10-26 15:05:35 +0200 | [diff] [blame] | 26 | #undef free_func |
William Lallemand | 08289f1 | 2012-10-31 11:19:18 +0100 | [diff] [blame] | 27 | #endif /* USE_ZLIB */ |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 28 | |
| 29 | #include <common/compat.h> |
William Lallemand | 2b50247 | 2012-10-30 14:30:39 +0100 | [diff] [blame] | 30 | #include <common/memory.h> |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 31 | |
| 32 | #include <types/global.h> |
| 33 | #include <types/compression.h> |
| 34 | |
William Lallemand | 727db8b | 2013-04-20 17:33:20 +0200 | [diff] [blame] | 35 | #include <proto/acl.h> |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 36 | #include <proto/compression.h> |
William Lallemand | d85f917 | 2012-11-09 17:05:39 +0100 | [diff] [blame] | 37 | #include <proto/freq_ctr.h> |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 38 | #include <proto/proto_http.h> |
| 39 | |
William Lallemand | 2b50247 | 2012-10-30 14:30:39 +0100 | [diff] [blame] | 40 | |
| 41 | #ifdef USE_ZLIB |
| 42 | |
William Lallemand | 8b52bb3 | 2012-11-16 18:06:41 +0100 | [diff] [blame] | 43 | static void *alloc_zlib(void *opaque, unsigned int items, unsigned int size); |
| 44 | static void free_zlib(void *opaque, void *ptr); |
| 45 | |
William Lallemand | 2b50247 | 2012-10-30 14:30:39 +0100 | [diff] [blame] | 46 | /* zlib allocation */ |
| 47 | static struct pool_head *zlib_pool_deflate_state = NULL; |
| 48 | static struct pool_head *zlib_pool_window = NULL; |
| 49 | static struct pool_head *zlib_pool_prev = NULL; |
| 50 | static struct pool_head *zlib_pool_head = NULL; |
| 51 | static struct pool_head *zlib_pool_pending_buf = NULL; |
| 52 | |
William Lallemand | e3a7d99 | 2012-11-20 11:25:20 +0100 | [diff] [blame] | 53 | long zlib_used_memory = 0; |
William Lallemand | 9d5f548 | 2012-11-07 16:12:57 +0100 | [diff] [blame] | 54 | |
William Lallemand | 2b50247 | 2012-10-30 14:30:39 +0100 | [diff] [blame] | 55 | #endif |
| 56 | |
William Lallemand | 072a2bf | 2012-11-20 17:01:01 +0100 | [diff] [blame] | 57 | unsigned int compress_min_idle = 0; |
William Lallemand | 8b52bb3 | 2012-11-16 18:06:41 +0100 | [diff] [blame] | 58 | static struct pool_head *pool_comp_ctx = NULL; |
| 59 | |
Willy Tarreau | 9f640a1 | 2015-03-28 15:46:00 +0100 | [diff] [blame] | 60 | static int identity_init(struct comp_ctx **comp_ctx, int level); |
| 61 | 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] | 62 | static int identity_flush(struct comp_ctx *comp_ctx, struct buffer *out); |
| 63 | static int identity_finish(struct comp_ctx *comp_ctx, struct buffer *out); |
Willy Tarreau | 9f640a1 | 2015-03-28 15:46:00 +0100 | [diff] [blame] | 64 | static int identity_end(struct comp_ctx **comp_ctx); |
| 65 | |
Willy Tarreau | 418b8c0 | 2015-03-29 03:32:06 +0200 | [diff] [blame] | 66 | #if defined(USE_SLZ) |
| 67 | |
| 68 | static int rfc1950_init(struct comp_ctx **comp_ctx, int level); |
| 69 | static int rfc1951_init(struct comp_ctx **comp_ctx, int level); |
| 70 | static int rfc1952_init(struct comp_ctx **comp_ctx, int level); |
| 71 | static int rfc195x_add_data(struct comp_ctx *comp_ctx, const char *in_data, int in_len, struct buffer *out); |
| 72 | static int rfc195x_flush(struct comp_ctx *comp_ctx, struct buffer *out); |
| 73 | static int rfc195x_finish(struct comp_ctx *comp_ctx, struct buffer *out); |
| 74 | static int rfc195x_end(struct comp_ctx **comp_ctx); |
| 75 | |
| 76 | #elif defined(USE_ZLIB) |
Willy Tarreau | 7b21877 | 2015-03-28 22:08:25 +0100 | [diff] [blame] | 77 | |
Willy Tarreau | 9f640a1 | 2015-03-28 15:46:00 +0100 | [diff] [blame] | 78 | static int gzip_init(struct comp_ctx **comp_ctx, int level); |
Willy Tarreau | c91840a | 2015-03-28 17:00:39 +0100 | [diff] [blame] | 79 | static int raw_def_init(struct comp_ctx **comp_ctx, int level); |
Willy Tarreau | 9f640a1 | 2015-03-28 15:46:00 +0100 | [diff] [blame] | 80 | static int deflate_init(struct comp_ctx **comp_ctx, int level); |
| 81 | 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] | 82 | static int deflate_flush(struct comp_ctx *comp_ctx, struct buffer *out); |
| 83 | static int deflate_finish(struct comp_ctx *comp_ctx, struct buffer *out); |
Willy Tarreau | 9f640a1 | 2015-03-28 15:46:00 +0100 | [diff] [blame] | 84 | static int deflate_end(struct comp_ctx **comp_ctx); |
Willy Tarreau | 7b21877 | 2015-03-28 22:08:25 +0100 | [diff] [blame] | 85 | |
Willy Tarreau | 9f640a1 | 2015-03-28 15:46:00 +0100 | [diff] [blame] | 86 | #endif /* USE_ZLIB */ |
| 87 | |
William Lallemand | 2b50247 | 2012-10-30 14:30:39 +0100 | [diff] [blame] | 88 | |
Cyril Bonté | 6162c43 | 2012-11-10 19:27:47 +0100 | [diff] [blame] | 89 | const struct comp_algo comp_algos[] = |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 90 | { |
Willy Tarreau | 7b21877 | 2015-03-28 22:08:25 +0100 | [diff] [blame] | 91 | { "identity", 8, "identity", 8, identity_init, identity_add_data, identity_flush, identity_finish, identity_end }, |
Willy Tarreau | 418b8c0 | 2015-03-29 03:32:06 +0200 | [diff] [blame] | 92 | #if defined(USE_SLZ) |
| 93 | { "deflate", 7, "deflate", 7, rfc1950_init, rfc195x_add_data, rfc195x_flush, rfc195x_finish, rfc195x_end }, |
| 94 | { "raw-deflate", 11, "deflate", 7, rfc1951_init, rfc195x_add_data, rfc195x_flush, rfc195x_finish, rfc195x_end }, |
| 95 | { "gzip", 4, "gzip", 4, rfc1952_init, rfc195x_add_data, rfc195x_flush, rfc195x_finish, rfc195x_end }, |
| 96 | #elif defined(USE_ZLIB) |
Willy Tarreau | 7b21877 | 2015-03-28 22:08:25 +0100 | [diff] [blame] | 97 | { "deflate", 7, "deflate", 7, deflate_init, deflate_add_data, deflate_flush, deflate_finish, deflate_end }, |
| 98 | { "raw-deflate", 11, "deflate", 7, raw_def_init, deflate_add_data, deflate_flush, deflate_finish, deflate_end }, |
| 99 | { "gzip", 4, "gzip", 4, gzip_init, deflate_add_data, deflate_flush, deflate_finish, deflate_end }, |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 100 | #endif /* USE_ZLIB */ |
Willy Tarreau | 615105e | 2015-03-28 16:40:46 +0100 | [diff] [blame] | 101 | { NULL, 0, NULL, 0, NULL , NULL, NULL, NULL, NULL } |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 102 | }; |
| 103 | |
| 104 | /* |
| 105 | * Add a content-type in the configuration |
| 106 | */ |
| 107 | int comp_append_type(struct comp *comp, const char *type) |
| 108 | { |
| 109 | struct comp_type *comp_type; |
| 110 | |
| 111 | comp_type = calloc(1, sizeof(struct comp_type)); |
| 112 | comp_type->name_len = strlen(type); |
| 113 | comp_type->name = strdup(type); |
| 114 | comp_type->next = comp->types; |
| 115 | comp->types = comp_type; |
| 116 | return 0; |
| 117 | } |
| 118 | |
| 119 | /* |
| 120 | * Add an algorithm in the configuration |
| 121 | */ |
| 122 | int comp_append_algo(struct comp *comp, const char *algo) |
| 123 | { |
| 124 | struct comp_algo *comp_algo; |
| 125 | int i; |
| 126 | |
Willy Tarreau | 615105e | 2015-03-28 16:40:46 +0100 | [diff] [blame] | 127 | for (i = 0; comp_algos[i].cfg_name; i++) { |
| 128 | if (!strcmp(algo, comp_algos[i].cfg_name)) { |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 129 | comp_algo = calloc(1, sizeof(struct comp_algo)); |
| 130 | memmove(comp_algo, &comp_algos[i], sizeof(struct comp_algo)); |
| 131 | comp_algo->next = comp->algos; |
| 132 | comp->algos = comp_algo; |
| 133 | return 0; |
| 134 | } |
| 135 | } |
| 136 | return -1; |
| 137 | } |
| 138 | |
| 139 | /* 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] | 140 | * bytes written. It goes backwards and starts with the byte before <end>. It |
| 141 | * returns the number of bytes written which will not exceed 10 (8 digits, CR, |
| 142 | * and LF). The caller is responsible for ensuring there is enough room left in |
| 143 | * the output buffer for the string. |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 144 | */ |
Willy Tarreau | 15530d2 | 2015-03-28 12:05:47 +0100 | [diff] [blame] | 145 | int http_emit_chunk_size(char *end, unsigned int chksz) |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 146 | { |
Willy Tarreau | 15530d2 | 2015-03-28 12:05:47 +0100 | [diff] [blame] | 147 | char *beg = end; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 148 | |
Willy Tarreau | 15530d2 | 2015-03-28 12:05:47 +0100 | [diff] [blame] | 149 | *--beg = '\n'; |
| 150 | *--beg = '\r'; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 151 | do { |
Willy Tarreau | 15530d2 | 2015-03-28 12:05:47 +0100 | [diff] [blame] | 152 | *--beg = hextab[chksz & 0xF]; |
| 153 | } while (chksz >>= 4); |
| 154 | return end - beg; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | /* |
| 158 | * Init HTTP compression |
| 159 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame^] | 160 | int http_compression_buffer_init(struct stream *s, struct buffer *in, struct buffer *out) |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 161 | { |
Willy Tarreau | 2aee221 | 2015-03-28 12:20:33 +0100 | [diff] [blame] | 162 | /* output stream requires at least 10 bytes for the gzip header, plus |
| 163 | * at least 8 bytes for the gzip trailer (crc+len), plus a possible |
| 164 | * plus at most 5 bytes per 32kB block and 2 bytes to close the stream. |
| 165 | */ |
| 166 | if (in->size - buffer_len(in) < 20 + 5 * ((in->i + 32767) >> 15)) |
Willy Tarreau | d328af5 | 2015-03-28 11:10:56 +0100 | [diff] [blame] | 167 | return -1; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 168 | |
Willy Tarreau | d328af5 | 2015-03-28 11:10:56 +0100 | [diff] [blame] | 169 | /* prepare an empty output buffer in which we reserve enough room for |
Willy Tarreau | 15530d2 | 2015-03-28 12:05:47 +0100 | [diff] [blame] | 170 | * copying the output bytes from <in>, plus 10 extra bytes to write |
Willy Tarreau | d328af5 | 2015-03-28 11:10:56 +0100 | [diff] [blame] | 171 | * the chunk size. We don't copy the bytes yet so that if we have to |
| 172 | * cancel the operation later, it's cheap. |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 173 | */ |
Willy Tarreau | 474cf54 | 2014-11-24 10:54:47 +0100 | [diff] [blame] | 174 | b_reset(out); |
Willy Tarreau | d328af5 | 2015-03-28 11:10:56 +0100 | [diff] [blame] | 175 | out->o = in->o; |
| 176 | out->p += out->o; |
Willy Tarreau | 15530d2 | 2015-03-28 12:05:47 +0100 | [diff] [blame] | 177 | out->i = 10; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 178 | return 0; |
| 179 | } |
| 180 | |
| 181 | /* |
| 182 | * Add data to compress |
| 183 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame^] | 184 | int http_compression_buffer_add_data(struct stream *s, struct buffer *in, struct buffer *out) |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 185 | { |
| 186 | struct http_msg *msg = &s->txn.rsp; |
William Lallemand | bf3ae61 | 2012-11-19 12:35:37 +0100 | [diff] [blame] | 187 | int consumed_data = 0; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 188 | int data_process_len; |
Willy Tarreau | 7f2f8d5 | 2014-04-18 00:20:14 +0200 | [diff] [blame] | 189 | int block1, block2; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 190 | |
| 191 | /* |
Willy Tarreau | 7f2f8d5 | 2014-04-18 00:20:14 +0200 | [diff] [blame] | 192 | * Temporarily skip already parsed data and chunks to jump to the |
| 193 | * actual data block. It is fixed before leaving. |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 194 | */ |
| 195 | b_adv(in, msg->next); |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 196 | |
| 197 | /* |
| 198 | * select the smallest size between the announced chunk size, the input |
Willy Tarreau | 7f2f8d5 | 2014-04-18 00:20:14 +0200 | [diff] [blame] | 199 | * data, and the available output buffer size. The compressors are |
| 200 | * 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] | 201 | */ |
| 202 | data_process_len = MIN(in->i, msg->chunk_len); |
| 203 | data_process_len = MIN(out->size - buffer_len(out), data_process_len); |
| 204 | |
Willy Tarreau | 7f2f8d5 | 2014-04-18 00:20:14 +0200 | [diff] [blame] | 205 | block1 = data_process_len; |
| 206 | if (block1 > bi_contig_data(in)) |
| 207 | block1 = bi_contig_data(in); |
| 208 | block2 = data_process_len - block1; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 209 | |
Willy Tarreau | 7f2f8d5 | 2014-04-18 00:20:14 +0200 | [diff] [blame] | 210 | /* compressors return < 0 upon error or the amount of bytes read */ |
| 211 | consumed_data = s->comp_algo->add_data(s->comp_ctx, bi_ptr(in), block1, out); |
| 212 | if (consumed_data >= 0 && block2 > 0) { |
| 213 | consumed_data = s->comp_algo->add_data(s->comp_ctx, in->data, block2, out); |
| 214 | if (consumed_data >= 0) |
| 215 | consumed_data += block1; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 216 | } |
| 217 | |
Willy Tarreau | 7f2f8d5 | 2014-04-18 00:20:14 +0200 | [diff] [blame] | 218 | /* restore original buffer pointer */ |
| 219 | b_rew(in, msg->next); |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 220 | |
Willy Tarreau | 7f2f8d5 | 2014-04-18 00:20:14 +0200 | [diff] [blame] | 221 | if (consumed_data > 0) { |
| 222 | msg->next += consumed_data; |
| 223 | msg->chunk_len -= consumed_data; |
| 224 | } |
William Lallemand | bf3ae61 | 2012-11-19 12:35:37 +0100 | [diff] [blame] | 225 | return consumed_data; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | /* |
| 229 | * Flush data in process, and write the header and footer of the chunk. Upon |
| 230 | * success, in and out buffers are swapped to avoid a copy. |
| 231 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame^] | 232 | int http_compression_buffer_end(struct stream *s, struct buffer **in, struct buffer **out, int end) |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 233 | { |
Willy Tarreau | 3ca5448 | 2014-04-23 19:31:17 +0200 | [diff] [blame] | 234 | int to_forward; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 235 | int left; |
| 236 | struct http_msg *msg = &s->txn.rsp; |
| 237 | struct buffer *ib = *in, *ob = *out; |
Willy Tarreau | d328af5 | 2015-03-28 11:10:56 +0100 | [diff] [blame] | 238 | char *tail; |
William Lallemand | 08289f1 | 2012-10-31 11:19:18 +0100 | [diff] [blame] | 239 | |
Willy Tarreau | 418b8c0 | 2015-03-29 03:32:06 +0200 | [diff] [blame] | 240 | #if defined(USE_SLZ) || defined(USE_ZLIB) |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 241 | int ret; |
| 242 | |
| 243 | /* flush data here */ |
| 244 | |
| 245 | if (end) |
Willy Tarreau | 9787efa | 2015-03-28 19:17:31 +0100 | [diff] [blame] | 246 | ret = s->comp_algo->finish(s->comp_ctx, ob); /* end of data */ |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 247 | else |
Willy Tarreau | 9787efa | 2015-03-28 19:17:31 +0100 | [diff] [blame] | 248 | ret = s->comp_algo->flush(s->comp_ctx, ob); /* end of buffer */ |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 249 | |
| 250 | if (ret < 0) |
| 251 | return -1; /* flush failed */ |
| 252 | |
William Lallemand | 08289f1 | 2012-10-31 11:19:18 +0100 | [diff] [blame] | 253 | #endif /* USE_ZLIB */ |
| 254 | |
Willy Tarreau | 15530d2 | 2015-03-28 12:05:47 +0100 | [diff] [blame] | 255 | if (ob->i == 10) { |
Willy Tarreau | d328af5 | 2015-03-28 11:10:56 +0100 | [diff] [blame] | 256 | /* No data were appended, let's drop the output buffer and |
| 257 | * keep the input buffer unchanged. |
| 258 | */ |
| 259 | return 0; |
| 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 | /* OK so at this stage, we have an output buffer <ob> looking like this : |
| 263 | * |
| 264 | * <-- o --> <------ i -----> |
| 265 | * +---------+---+------------+-----------+ |
| 266 | * | out | c | comp_in | empty | |
| 267 | * +---------+---+------------+-----------+ |
| 268 | * data p size |
| 269 | * |
| 270 | * <out> is the room reserved to copy ib->o. It starts at ob->data and |
| 271 | * 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] | 272 | * size (10 bytes). <comp_in> is the compressed equivalent of the data |
Willy Tarreau | d328af5 | 2015-03-28 11:10:56 +0100 | [diff] [blame] | 273 | * part of ib->i. <empty> is the amount of empty bytes at the end of |
| 274 | * the buffer, into which we may have to copy the remaining bytes from |
| 275 | * ib->i after the data (chunk size, trailers, ...). |
| 276 | */ |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 277 | |
Willy Tarreau | d328af5 | 2015-03-28 11:10:56 +0100 | [diff] [blame] | 278 | /* 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] | 279 | * We write the chunk using a dynamic length and adjust ob->p and ob->i |
| 280 | * accordingly afterwards. That will move <out> away from <data>. |
Willy Tarreau | d328af5 | 2015-03-28 11:10:56 +0100 | [diff] [blame] | 281 | */ |
Willy Tarreau | 15530d2 | 2015-03-28 12:05:47 +0100 | [diff] [blame] | 282 | left = 10 - http_emit_chunk_size(ob->p + 10, ob->i - 10); |
| 283 | ob->p += left; |
| 284 | ob->i -= left; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 285 | |
Willy Tarreau | d328af5 | 2015-03-28 11:10:56 +0100 | [diff] [blame] | 286 | /* Copy previous data from ib->o into ob->o */ |
| 287 | if (ib->o > 0) { |
| 288 | left = bo_contig_data(ib); |
| 289 | memcpy(ob->p - ob->o, bo_ptr(ib), left); |
| 290 | if (ib->o - left) /* second part of the buffer */ |
| 291 | memcpy(ob->p - ob->o + left, ib->data, ib->o - left); |
| 292 | } |
| 293 | |
| 294 | /* chunked encoding requires CRLF after data */ |
| 295 | tail = ob->p + ob->i; |
| 296 | *tail++ = '\r'; |
| 297 | *tail++ = '\n'; |
| 298 | |
| 299 | /* At the end of data, we must write the empty chunk 0<CRLF>, |
| 300 | * and terminate the trailers section with a last <CRLF>. If |
| 301 | * we're forwarding a chunked-encoded response, we'll have a |
| 302 | * trailers section after the empty chunk which needs to be |
| 303 | * forwarded and which will provide the last CRLF. Otherwise |
| 304 | * we write it ourselves. |
| 305 | */ |
| 306 | if (msg->msg_state >= HTTP_MSG_TRAILERS) { |
| 307 | memcpy(tail, "0\r\n", 3); |
| 308 | tail += 3; |
| 309 | if (msg->msg_state >= HTTP_MSG_DONE) { |
| 310 | memcpy(tail, "\r\n", 2); |
| 311 | tail += 2; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 312 | } |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 313 | } |
Willy Tarreau | d328af5 | 2015-03-28 11:10:56 +0100 | [diff] [blame] | 314 | ob->i = tail - ob->p; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 315 | |
| 316 | to_forward = ob->i; |
Willy Tarreau | 55058a7 | 2012-11-21 08:27:21 +0100 | [diff] [blame] | 317 | |
William Lallemand | d85f917 | 2012-11-09 17:05:39 +0100 | [diff] [blame] | 318 | /* update input rate */ |
Willy Tarreau | 55058a7 | 2012-11-21 08:27:21 +0100 | [diff] [blame] | 319 | if (s->comp_ctx && s->comp_ctx->cur_lvl > 0) { |
Willy Tarreau | 3ca5448 | 2014-04-23 19:31:17 +0200 | [diff] [blame] | 320 | update_freq_ctr(&global.comp_bps_in, msg->next); |
| 321 | s->fe->fe_counters.comp_in += msg->next; |
| 322 | s->be->be_counters.comp_in += msg->next; |
Willy Tarreau | 55058a7 | 2012-11-21 08:27:21 +0100 | [diff] [blame] | 323 | } else { |
Willy Tarreau | 3ca5448 | 2014-04-23 19:31:17 +0200 | [diff] [blame] | 324 | s->fe->fe_counters.comp_byp += msg->next; |
| 325 | s->be->be_counters.comp_byp += msg->next; |
Willy Tarreau | 55058a7 | 2012-11-21 08:27:21 +0100 | [diff] [blame] | 326 | } |
William Lallemand | d85f917 | 2012-11-09 17:05:39 +0100 | [diff] [blame] | 327 | |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 328 | /* copy the remaining data in the tmp buffer. */ |
Willy Tarreau | 7f2f8d5 | 2014-04-18 00:20:14 +0200 | [diff] [blame] | 329 | b_adv(ib, msg->next); |
| 330 | msg->next = 0; |
| 331 | |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 332 | if (ib->i > 0) { |
Willy Tarreau | d328af5 | 2015-03-28 11:10:56 +0100 | [diff] [blame] | 333 | left = bi_contig_data(ib); |
| 334 | memcpy(ob->p + ob->i, bi_ptr(ib), left); |
| 335 | ob->i += left; |
| 336 | if (ib->i - left) { |
| 337 | memcpy(ob->p + ob->i, ib->data, ib->i - left); |
| 338 | ob->i += ib->i - left; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 339 | } |
| 340 | } |
| 341 | |
| 342 | /* swap the buffers */ |
| 343 | *in = ob; |
| 344 | *out = ib; |
| 345 | |
Willy Tarreau | 55058a7 | 2012-11-21 08:27:21 +0100 | [diff] [blame] | 346 | if (s->comp_ctx && s->comp_ctx->cur_lvl > 0) { |
William Lallemand | d85f917 | 2012-11-09 17:05:39 +0100 | [diff] [blame] | 347 | update_freq_ctr(&global.comp_bps_out, to_forward); |
Willy Tarreau | 55058a7 | 2012-11-21 08:27:21 +0100 | [diff] [blame] | 348 | s->fe->fe_counters.comp_out += to_forward; |
| 349 | s->be->be_counters.comp_out += to_forward; |
| 350 | } |
William Lallemand | d85f917 | 2012-11-09 17:05:39 +0100 | [diff] [blame] | 351 | |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 352 | /* forward the new chunk without remaining data */ |
| 353 | b_adv(ob, to_forward); |
| 354 | |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 355 | return to_forward; |
| 356 | } |
| 357 | |
William Lallemand | 8b52bb3 | 2012-11-16 18:06:41 +0100 | [diff] [blame] | 358 | /* |
| 359 | * Alloc the comp_ctx |
| 360 | */ |
| 361 | static inline int init_comp_ctx(struct comp_ctx **comp_ctx) |
| 362 | { |
| 363 | #ifdef USE_ZLIB |
| 364 | z_stream *strm; |
| 365 | |
William Lallemand | e3a7d99 | 2012-11-20 11:25:20 +0100 | [diff] [blame] | 366 | 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] | 367 | return -1; |
| 368 | #endif |
| 369 | |
| 370 | if (unlikely(pool_comp_ctx == NULL)) |
| 371 | pool_comp_ctx = create_pool("comp_ctx", sizeof(struct comp_ctx), MEM_F_SHARED); |
| 372 | |
| 373 | *comp_ctx = pool_alloc2(pool_comp_ctx); |
| 374 | if (*comp_ctx == NULL) |
| 375 | return -1; |
Willy Tarreau | 418b8c0 | 2015-03-29 03:32:06 +0200 | [diff] [blame] | 376 | #if defined(USE_SLZ) |
| 377 | (*comp_ctx)->direct_ptr = NULL; |
| 378 | (*comp_ctx)->direct_len = 0; |
| 379 | (*comp_ctx)->queued = NULL; |
| 380 | #elif defined(USE_ZLIB) |
William Lallemand | e3a7d99 | 2012-11-20 11:25:20 +0100 | [diff] [blame] | 381 | zlib_used_memory += sizeof(struct comp_ctx); |
William Lallemand | 8b52bb3 | 2012-11-16 18:06:41 +0100 | [diff] [blame] | 382 | |
| 383 | strm = &(*comp_ctx)->strm; |
| 384 | strm->zalloc = alloc_zlib; |
| 385 | strm->zfree = free_zlib; |
| 386 | strm->opaque = *comp_ctx; |
| 387 | #endif |
| 388 | return 0; |
| 389 | } |
| 390 | |
| 391 | /* |
| 392 | * Dealloc the comp_ctx |
| 393 | */ |
| 394 | static inline int deinit_comp_ctx(struct comp_ctx **comp_ctx) |
| 395 | { |
| 396 | if (!*comp_ctx) |
| 397 | return 0; |
| 398 | |
| 399 | pool_free2(pool_comp_ctx, *comp_ctx); |
| 400 | *comp_ctx = NULL; |
| 401 | |
| 402 | #ifdef USE_ZLIB |
William Lallemand | e3a7d99 | 2012-11-20 11:25:20 +0100 | [diff] [blame] | 403 | zlib_used_memory -= sizeof(struct comp_ctx); |
William Lallemand | 8b52bb3 | 2012-11-16 18:06:41 +0100 | [diff] [blame] | 404 | #endif |
William Lallemand | 8b52bb3 | 2012-11-16 18:06:41 +0100 | [diff] [blame] | 405 | return 0; |
| 406 | } |
| 407 | |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 408 | |
| 409 | /**************************** |
| 410 | **** Identity algorithm **** |
| 411 | ****************************/ |
| 412 | |
| 413 | /* |
| 414 | * Init the identity algorithm |
| 415 | */ |
Willy Tarreau | 9f640a1 | 2015-03-28 15:46:00 +0100 | [diff] [blame] | 416 | static int identity_init(struct comp_ctx **comp_ctx, int level) |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 417 | { |
| 418 | return 0; |
| 419 | } |
| 420 | |
| 421 | /* |
| 422 | * Process data |
William Lallemand | bf3ae61 | 2012-11-19 12:35:37 +0100 | [diff] [blame] | 423 | * Return size of consumed data or -1 on error |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 424 | */ |
Willy Tarreau | 9f640a1 | 2015-03-28 15:46:00 +0100 | [diff] [blame] | 425 | 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] | 426 | { |
William Lallemand | bf3ae61 | 2012-11-19 12:35:37 +0100 | [diff] [blame] | 427 | char *out_data = bi_end(out); |
| 428 | int out_len = out->size - buffer_len(out); |
| 429 | |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 430 | if (out_len < in_len) |
| 431 | return -1; |
| 432 | |
| 433 | memcpy(out_data, in_data, in_len); |
| 434 | |
William Lallemand | bf3ae61 | 2012-11-19 12:35:37 +0100 | [diff] [blame] | 435 | out->i += in_len; |
| 436 | |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 437 | return in_len; |
| 438 | } |
| 439 | |
Willy Tarreau | 9787efa | 2015-03-28 19:17:31 +0100 | [diff] [blame] | 440 | static int identity_flush(struct comp_ctx *comp_ctx, struct buffer *out) |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 441 | { |
| 442 | return 0; |
| 443 | } |
| 444 | |
Willy Tarreau | 9787efa | 2015-03-28 19:17:31 +0100 | [diff] [blame] | 445 | static int identity_finish(struct comp_ctx *comp_ctx, struct buffer *out) |
| 446 | { |
| 447 | return 0; |
| 448 | } |
| 449 | |
Willy Tarreau | 418b8c0 | 2015-03-29 03:32:06 +0200 | [diff] [blame] | 450 | /* |
| 451 | * Deinit the algorithm |
| 452 | */ |
| 453 | static int identity_end(struct comp_ctx **comp_ctx) |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 454 | { |
| 455 | return 0; |
| 456 | } |
| 457 | |
Willy Tarreau | 418b8c0 | 2015-03-29 03:32:06 +0200 | [diff] [blame] | 458 | |
| 459 | #ifdef USE_SLZ |
| 460 | |
| 461 | /* SLZ's gzip format (RFC1952). Returns < 0 on error. */ |
| 462 | static int rfc1952_init(struct comp_ctx **comp_ctx, int level) |
| 463 | { |
| 464 | if (init_comp_ctx(comp_ctx) < 0) |
| 465 | return -1; |
| 466 | |
| 467 | (*comp_ctx)->cur_lvl = !!level; |
| 468 | return slz_rfc1952_init(&(*comp_ctx)->strm, !!level); |
| 469 | } |
| 470 | |
| 471 | /* SLZ's raw deflate format (RFC1951). Returns < 0 on error. */ |
| 472 | static int rfc1951_init(struct comp_ctx **comp_ctx, int level) |
| 473 | { |
| 474 | if (init_comp_ctx(comp_ctx) < 0) |
| 475 | return -1; |
| 476 | |
| 477 | (*comp_ctx)->cur_lvl = !!level; |
| 478 | return slz_rfc1951_init(&(*comp_ctx)->strm, !!level); |
| 479 | } |
| 480 | |
| 481 | /* SLZ's zlib format (RFC1950). Returns < 0 on error. */ |
| 482 | static int rfc1950_init(struct comp_ctx **comp_ctx, int level) |
| 483 | { |
| 484 | if (init_comp_ctx(comp_ctx) < 0) |
| 485 | return -1; |
| 486 | |
| 487 | (*comp_ctx)->cur_lvl = !!level; |
| 488 | return slz_rfc1950_init(&(*comp_ctx)->strm, !!level); |
| 489 | } |
| 490 | |
| 491 | /* Return the size of consumed data or -1. The output buffer is unused at this |
| 492 | * point, we only keep a reference to the input data or a copy of them if the |
| 493 | * reference is already used. |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 494 | */ |
Willy Tarreau | 418b8c0 | 2015-03-29 03:32:06 +0200 | [diff] [blame] | 495 | static int rfc195x_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] | 496 | { |
Willy Tarreau | 418b8c0 | 2015-03-29 03:32:06 +0200 | [diff] [blame] | 497 | static struct buffer *tmpbuf = &buf_empty; |
| 498 | |
| 499 | if (in_len <= 0) |
| 500 | return 0; |
| 501 | |
| 502 | if (comp_ctx->direct_ptr && !comp_ctx->queued) { |
| 503 | /* data already being pointed to, we're in front of fragmented |
| 504 | * data and need a buffer now. We reuse the same buffer, as it's |
| 505 | * not used out of the scope of a series of add_data()*, end(). |
| 506 | */ |
| 507 | if (unlikely(!tmpbuf->size)) { |
| 508 | /* this is the first time we need the compression buffer */ |
| 509 | if (b_alloc(&tmpbuf) == NULL) |
| 510 | return -1; /* no memory */ |
| 511 | } |
| 512 | b_reset(tmpbuf); |
| 513 | memcpy(bi_end(tmpbuf), comp_ctx->direct_ptr, comp_ctx->direct_len); |
| 514 | tmpbuf->i += comp_ctx->direct_len; |
| 515 | comp_ctx->direct_ptr = NULL; |
| 516 | comp_ctx->direct_len = 0; |
| 517 | comp_ctx->queued = tmpbuf; |
| 518 | /* fall through buffer copy */ |
| 519 | } |
| 520 | |
| 521 | if (comp_ctx->queued) { |
| 522 | /* data already pending */ |
| 523 | memcpy(bi_end(comp_ctx->queued), in_data, in_len); |
| 524 | comp_ctx->queued->i += in_len; |
| 525 | return in_len; |
| 526 | } |
| 527 | |
| 528 | comp_ctx->direct_ptr = in_data; |
| 529 | comp_ctx->direct_len = in_len; |
| 530 | return in_len; |
| 531 | } |
| 532 | |
| 533 | /* Compresses the data accumulated using add_data(), and optionally sends the |
| 534 | * format-specific trailer if <finish> is non-null. <out> is expected to have a |
| 535 | * large enough free non-wrapping space as verified by http_comp_buffer_init(). |
| 536 | * The number of bytes emitted is reported. |
| 537 | */ |
| 538 | static int rfc195x_flush_or_finish(struct comp_ctx *comp_ctx, struct buffer *out, int finish) |
| 539 | { |
| 540 | struct slz_stream *strm = &comp_ctx->strm; |
| 541 | const char *in_ptr; |
| 542 | int in_len; |
| 543 | int out_len; |
| 544 | |
| 545 | in_ptr = comp_ctx->direct_ptr; |
| 546 | in_len = comp_ctx->direct_len; |
| 547 | |
| 548 | if (comp_ctx->queued) { |
| 549 | in_ptr = comp_ctx->queued->p; |
| 550 | in_len = comp_ctx->queued->i; |
| 551 | } |
| 552 | |
| 553 | out_len = out->i; |
| 554 | |
| 555 | if (in_ptr) |
| 556 | out->i += slz_encode(strm, bi_end(out), in_ptr, in_len, !finish); |
| 557 | |
| 558 | if (finish) |
| 559 | out->i += slz_finish(strm, bi_end(out)); |
| 560 | |
| 561 | out_len = out->i - out_len; |
| 562 | |
| 563 | /* very important, we must wipe the data we've just flushed */ |
| 564 | comp_ctx->direct_len = 0; |
| 565 | comp_ctx->direct_ptr = NULL; |
| 566 | comp_ctx->queued = NULL; |
| 567 | |
| 568 | /* Verify compression rate limiting and CPU usage */ |
| 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 | if (comp_ctx->cur_lvl > 0) |
| 572 | strm->level = --comp_ctx->cur_lvl; |
| 573 | } |
| 574 | else if (comp_ctx->cur_lvl < global.tune.comp_maxlevel && comp_ctx->cur_lvl < 1) { |
| 575 | strm->level = ++comp_ctx->cur_lvl; |
| 576 | } |
| 577 | |
| 578 | /* and that's all */ |
| 579 | return out_len; |
| 580 | } |
| 581 | |
| 582 | static int rfc195x_flush(struct comp_ctx *comp_ctx, struct buffer *out) |
| 583 | { |
| 584 | return rfc195x_flush_or_finish(comp_ctx, out, 0); |
| 585 | } |
| 586 | |
| 587 | static int rfc195x_finish(struct comp_ctx *comp_ctx, struct buffer *out) |
| 588 | { |
| 589 | return rfc195x_flush_or_finish(comp_ctx, out, 1); |
| 590 | } |
| 591 | |
| 592 | /* we just need to free the comp_ctx here, nothing was allocated */ |
| 593 | static int rfc195x_end(struct comp_ctx **comp_ctx) |
| 594 | { |
| 595 | deinit_comp_ctx(comp_ctx); |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 596 | return 0; |
| 597 | } |
| 598 | |
Willy Tarreau | 418b8c0 | 2015-03-29 03:32:06 +0200 | [diff] [blame] | 599 | #elif defined(USE_ZLIB) /* ! USE_SLZ */ |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 600 | |
William Lallemand | 2b50247 | 2012-10-30 14:30:39 +0100 | [diff] [blame] | 601 | /* |
| 602 | * This is a tricky allocation function using the zlib. |
| 603 | * This is based on the allocation order in deflateInit2. |
| 604 | */ |
| 605 | static void *alloc_zlib(void *opaque, unsigned int items, unsigned int size) |
| 606 | { |
| 607 | struct comp_ctx *ctx = opaque; |
| 608 | static char round = 0; /* order in deflateInit2 */ |
| 609 | void *buf = NULL; |
Willy Tarreau | 4f31fc2 | 2014-12-24 18:07:55 +0100 | [diff] [blame] | 610 | struct pool_head *pool = NULL; |
William Lallemand | 2b50247 | 2012-10-30 14:30:39 +0100 | [diff] [blame] | 611 | |
William Lallemand | e3a7d99 | 2012-11-20 11:25:20 +0100 | [diff] [blame] | 612 | if (global.maxzlibmem > 0 && (global.maxzlibmem - zlib_used_memory) < (long)(items * size)) |
William Lallemand | 9d5f548 | 2012-11-07 16:12:57 +0100 | [diff] [blame] | 613 | goto end; |
William Lallemand | 9d5f548 | 2012-11-07 16:12:57 +0100 | [diff] [blame] | 614 | |
William Lallemand | 2b50247 | 2012-10-30 14:30:39 +0100 | [diff] [blame] | 615 | switch (round) { |
| 616 | case 0: |
| 617 | if (zlib_pool_deflate_state == NULL) |
| 618 | 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] | 619 | pool = zlib_pool_deflate_state; |
| 620 | ctx->zlib_deflate_state = buf = pool_alloc2(pool); |
William Lallemand | 2b50247 | 2012-10-30 14:30:39 +0100 | [diff] [blame] | 621 | break; |
| 622 | |
| 623 | case 1: |
| 624 | if (zlib_pool_window == NULL) |
| 625 | zlib_pool_window = create_pool("zlib_window", size * items, MEM_F_SHARED); |
Willy Tarreau | 4f31fc2 | 2014-12-24 18:07:55 +0100 | [diff] [blame] | 626 | pool = zlib_pool_window; |
| 627 | ctx->zlib_window = buf = pool_alloc2(pool); |
William Lallemand | 2b50247 | 2012-10-30 14:30:39 +0100 | [diff] [blame] | 628 | break; |
| 629 | |
| 630 | case 2: |
| 631 | if (zlib_pool_prev == NULL) |
| 632 | zlib_pool_prev = create_pool("zlib_prev", size * items, MEM_F_SHARED); |
Willy Tarreau | 4f31fc2 | 2014-12-24 18:07:55 +0100 | [diff] [blame] | 633 | pool = zlib_pool_prev; |
| 634 | ctx->zlib_prev = buf = pool_alloc2(pool); |
William Lallemand | 2b50247 | 2012-10-30 14:30:39 +0100 | [diff] [blame] | 635 | break; |
| 636 | |
| 637 | case 3: |
| 638 | if (zlib_pool_head == NULL) |
| 639 | zlib_pool_head = create_pool("zlib_head", size * items, MEM_F_SHARED); |
Willy Tarreau | 4f31fc2 | 2014-12-24 18:07:55 +0100 | [diff] [blame] | 640 | pool = zlib_pool_head; |
| 641 | ctx->zlib_head = buf = pool_alloc2(pool); |
William Lallemand | 2b50247 | 2012-10-30 14:30:39 +0100 | [diff] [blame] | 642 | break; |
| 643 | |
| 644 | case 4: |
| 645 | if (zlib_pool_pending_buf == NULL) |
| 646 | 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] | 647 | pool = zlib_pool_pending_buf; |
| 648 | ctx->zlib_pending_buf = buf = pool_alloc2(pool); |
William Lallemand | 2b50247 | 2012-10-30 14:30:39 +0100 | [diff] [blame] | 649 | break; |
| 650 | } |
William Lallemand | e3a7d99 | 2012-11-20 11:25:20 +0100 | [diff] [blame] | 651 | if (buf != NULL) |
Willy Tarreau | 4f31fc2 | 2014-12-24 18:07:55 +0100 | [diff] [blame] | 652 | zlib_used_memory += pool->size; |
William Lallemand | 9d5f548 | 2012-11-07 16:12:57 +0100 | [diff] [blame] | 653 | |
| 654 | end: |
William Lallemand | 2b50247 | 2012-10-30 14:30:39 +0100 | [diff] [blame] | 655 | |
Willy Tarreau | 4690985 | 2012-11-15 14:57:56 +0100 | [diff] [blame] | 656 | /* deflateInit2() first allocates and checks the deflate_state, then if |
| 657 | * it succeeds, it allocates all other 4 areas at ones and checks them |
| 658 | * at the end. So we want to correctly count the rounds depending on when |
| 659 | * zlib is supposed to abort. |
| 660 | */ |
| 661 | if (buf || round) |
| 662 | round = (round + 1) % 5; |
William Lallemand | 2b50247 | 2012-10-30 14:30:39 +0100 | [diff] [blame] | 663 | return buf; |
| 664 | } |
| 665 | |
| 666 | static void free_zlib(void *opaque, void *ptr) |
| 667 | { |
| 668 | struct comp_ctx *ctx = opaque; |
Willy Tarreau | b1fbd05 | 2012-11-10 17:49:37 +0100 | [diff] [blame] | 669 | struct pool_head *pool = NULL; |
William Lallemand | 2b50247 | 2012-10-30 14:30:39 +0100 | [diff] [blame] | 670 | |
| 671 | if (ptr == ctx->zlib_window) |
William Lallemand | 9d5f548 | 2012-11-07 16:12:57 +0100 | [diff] [blame] | 672 | pool = zlib_pool_window; |
William Lallemand | 2b50247 | 2012-10-30 14:30:39 +0100 | [diff] [blame] | 673 | else if (ptr == ctx->zlib_deflate_state) |
William Lallemand | 9d5f548 | 2012-11-07 16:12:57 +0100 | [diff] [blame] | 674 | pool = zlib_pool_deflate_state; |
William Lallemand | 2b50247 | 2012-10-30 14:30:39 +0100 | [diff] [blame] | 675 | else if (ptr == ctx->zlib_prev) |
William Lallemand | 9d5f548 | 2012-11-07 16:12:57 +0100 | [diff] [blame] | 676 | pool = zlib_pool_prev; |
William Lallemand | 2b50247 | 2012-10-30 14:30:39 +0100 | [diff] [blame] | 677 | else if (ptr == ctx->zlib_head) |
William Lallemand | 9d5f548 | 2012-11-07 16:12:57 +0100 | [diff] [blame] | 678 | pool = zlib_pool_head; |
William Lallemand | 2b50247 | 2012-10-30 14:30:39 +0100 | [diff] [blame] | 679 | else if (ptr == ctx->zlib_pending_buf) |
William Lallemand | 9d5f548 | 2012-11-07 16:12:57 +0100 | [diff] [blame] | 680 | pool = zlib_pool_pending_buf; |
William Lallemand | 2b50247 | 2012-10-30 14:30:39 +0100 | [diff] [blame] | 681 | |
William Lallemand | 9d5f548 | 2012-11-07 16:12:57 +0100 | [diff] [blame] | 682 | pool_free2(pool, ptr); |
William Lallemand | e3a7d99 | 2012-11-20 11:25:20 +0100 | [diff] [blame] | 683 | zlib_used_memory -= pool->size; |
William Lallemand | 2b50247 | 2012-10-30 14:30:39 +0100 | [diff] [blame] | 684 | } |
| 685 | |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 686 | /************************** |
| 687 | **** gzip algorithm **** |
| 688 | ***************************/ |
Willy Tarreau | 9f640a1 | 2015-03-28 15:46:00 +0100 | [diff] [blame] | 689 | static int gzip_init(struct comp_ctx **comp_ctx, int level) |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 690 | { |
William Lallemand | 8b52bb3 | 2012-11-16 18:06:41 +0100 | [diff] [blame] | 691 | z_stream *strm; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 692 | |
William Lallemand | 8b52bb3 | 2012-11-16 18:06:41 +0100 | [diff] [blame] | 693 | if (init_comp_ctx(comp_ctx) < 0) |
| 694 | return -1; |
William Lallemand | 9d5f548 | 2012-11-07 16:12:57 +0100 | [diff] [blame] | 695 | |
William Lallemand | 8b52bb3 | 2012-11-16 18:06:41 +0100 | [diff] [blame] | 696 | strm = &(*comp_ctx)->strm; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 697 | |
William Lallemand | 8b52bb3 | 2012-11-16 18:06:41 +0100 | [diff] [blame] | 698 | if (deflateInit2(strm, level, Z_DEFLATED, global.tune.zlibwindowsize + 16, global.tune.zlibmemlevel, Z_DEFAULT_STRATEGY) != Z_OK) { |
| 699 | deinit_comp_ctx(comp_ctx); |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 700 | return -1; |
William Lallemand | 8b52bb3 | 2012-11-16 18:06:41 +0100 | [diff] [blame] | 701 | } |
| 702 | |
| 703 | (*comp_ctx)->cur_lvl = level; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 704 | |
| 705 | return 0; |
| 706 | } |
Willy Tarreau | c91840a | 2015-03-28 17:00:39 +0100 | [diff] [blame] | 707 | |
| 708 | /* Raw deflate algorithm */ |
| 709 | static int raw_def_init(struct comp_ctx **comp_ctx, int level) |
| 710 | { |
| 711 | z_stream *strm; |
| 712 | |
| 713 | if (init_comp_ctx(comp_ctx) < 0) |
| 714 | return -1; |
| 715 | |
| 716 | strm = &(*comp_ctx)->strm; |
| 717 | |
| 718 | if (deflateInit2(strm, level, Z_DEFLATED, -global.tune.zlibwindowsize, global.tune.zlibmemlevel, Z_DEFAULT_STRATEGY) != Z_OK) { |
| 719 | deinit_comp_ctx(comp_ctx); |
| 720 | return -1; |
| 721 | } |
| 722 | |
| 723 | (*comp_ctx)->cur_lvl = level; |
| 724 | return 0; |
| 725 | } |
| 726 | |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 727 | /************************** |
| 728 | **** Deflate algorithm **** |
| 729 | ***************************/ |
| 730 | |
Willy Tarreau | 9f640a1 | 2015-03-28 15:46:00 +0100 | [diff] [blame] | 731 | static int deflate_init(struct comp_ctx **comp_ctx, int level) |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 732 | { |
William Lallemand | 8b52bb3 | 2012-11-16 18:06:41 +0100 | [diff] [blame] | 733 | z_stream *strm; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 734 | |
William Lallemand | 8b52bb3 | 2012-11-16 18:06:41 +0100 | [diff] [blame] | 735 | if (init_comp_ctx(comp_ctx) < 0) |
| 736 | return -1; |
| 737 | |
| 738 | strm = &(*comp_ctx)->strm; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 739 | |
Willy Tarreau | c5599e7 | 2013-04-28 08:52:52 +0200 | [diff] [blame] | 740 | 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] | 741 | deinit_comp_ctx(comp_ctx); |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 742 | return -1; |
William Lallemand | 8b52bb3 | 2012-11-16 18:06:41 +0100 | [diff] [blame] | 743 | } |
| 744 | |
| 745 | (*comp_ctx)->cur_lvl = level; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 746 | |
| 747 | return 0; |
| 748 | } |
| 749 | |
William Lallemand | bf3ae61 | 2012-11-19 12:35:37 +0100 | [diff] [blame] | 750 | /* Return the size of consumed data or -1 */ |
Willy Tarreau | 9f640a1 | 2015-03-28 15:46:00 +0100 | [diff] [blame] | 751 | 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] | 752 | { |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 753 | int ret; |
William Lallemand | bf3ae61 | 2012-11-19 12:35:37 +0100 | [diff] [blame] | 754 | z_stream *strm = &comp_ctx->strm; |
| 755 | char *out_data = bi_end(out); |
| 756 | int out_len = out->size - buffer_len(out); |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 757 | |
| 758 | if (in_len <= 0) |
| 759 | return 0; |
| 760 | |
| 761 | |
| 762 | if (out_len <= 0) |
| 763 | return -1; |
| 764 | |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 765 | strm->next_in = (unsigned char *)in_data; |
| 766 | strm->avail_in = in_len; |
| 767 | strm->next_out = (unsigned char *)out_data; |
| 768 | strm->avail_out = out_len; |
| 769 | |
| 770 | ret = deflate(strm, Z_NO_FLUSH); |
| 771 | if (ret != Z_OK) |
| 772 | return -1; |
| 773 | |
| 774 | /* deflate update the available data out */ |
William Lallemand | bf3ae61 | 2012-11-19 12:35:37 +0100 | [diff] [blame] | 775 | out->i += out_len - strm->avail_out; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 776 | |
William Lallemand | bf3ae61 | 2012-11-19 12:35:37 +0100 | [diff] [blame] | 777 | return in_len - strm->avail_in; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 778 | } |
| 779 | |
Willy Tarreau | 9787efa | 2015-03-28 19:17:31 +0100 | [diff] [blame] | 780 | 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] | 781 | { |
| 782 | int ret; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 783 | int out_len = 0; |
William Lallemand | 1c2d622 | 2012-10-30 15:52:53 +0100 | [diff] [blame] | 784 | z_stream *strm = &comp_ctx->strm; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 785 | |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 786 | strm->next_out = (unsigned char *)bi_end(out); |
| 787 | strm->avail_out = out->size - buffer_len(out); |
| 788 | |
| 789 | ret = deflate(strm, flag); |
| 790 | if (ret != Z_OK && ret != Z_STREAM_END) |
| 791 | return -1; |
| 792 | |
| 793 | out_len = (out->size - buffer_len(out)) - strm->avail_out; |
| 794 | out->i += out_len; |
| 795 | |
William Lallemand | 072a2bf | 2012-11-20 17:01:01 +0100 | [diff] [blame] | 796 | /* compression limit */ |
| 797 | if ((global.comp_rate_lim > 0 && (read_freq_ctr(&global.comp_bps_out) > global.comp_rate_lim)) || /* rate */ |
| 798 | (idle_pct < compress_min_idle)) { /* idle */ |
| 799 | /* decrease level */ |
| 800 | if (comp_ctx->cur_lvl > 0) { |
| 801 | comp_ctx->cur_lvl--; |
William Lallemand | d85f917 | 2012-11-09 17:05:39 +0100 | [diff] [blame] | 802 | deflateParams(&comp_ctx->strm, comp_ctx->cur_lvl, Z_DEFAULT_STRATEGY); |
| 803 | } |
William Lallemand | 072a2bf | 2012-11-20 17:01:01 +0100 | [diff] [blame] | 804 | |
| 805 | } else if (comp_ctx->cur_lvl < global.tune.comp_maxlevel) { |
| 806 | /* increase level */ |
| 807 | comp_ctx->cur_lvl++ ; |
| 808 | deflateParams(&comp_ctx->strm, comp_ctx->cur_lvl, Z_DEFAULT_STRATEGY); |
William Lallemand | d85f917 | 2012-11-09 17:05:39 +0100 | [diff] [blame] | 809 | } |
| 810 | |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 811 | return out_len; |
| 812 | } |
| 813 | |
Willy Tarreau | 9787efa | 2015-03-28 19:17:31 +0100 | [diff] [blame] | 814 | static int deflate_flush(struct comp_ctx *comp_ctx, struct buffer *out) |
| 815 | { |
| 816 | return deflate_flush_or_finish(comp_ctx, out, Z_SYNC_FLUSH); |
| 817 | } |
| 818 | |
| 819 | static int deflate_finish(struct comp_ctx *comp_ctx, struct buffer *out) |
| 820 | { |
| 821 | return deflate_flush_or_finish(comp_ctx, out, Z_FINISH); |
| 822 | } |
| 823 | |
Willy Tarreau | 9f640a1 | 2015-03-28 15:46:00 +0100 | [diff] [blame] | 824 | static int deflate_end(struct comp_ctx **comp_ctx) |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 825 | { |
William Lallemand | 8b52bb3 | 2012-11-16 18:06:41 +0100 | [diff] [blame] | 826 | z_stream *strm = &(*comp_ctx)->strm; |
| 827 | int ret; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 828 | |
William Lallemand | 8b52bb3 | 2012-11-16 18:06:41 +0100 | [diff] [blame] | 829 | ret = deflateEnd(strm); |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 830 | |
William Lallemand | 8b52bb3 | 2012-11-16 18:06:41 +0100 | [diff] [blame] | 831 | deinit_comp_ctx(comp_ctx); |
| 832 | |
| 833 | return ret; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 834 | } |
| 835 | |
| 836 | #endif /* USE_ZLIB */ |
| 837 | |
William Lallemand | 727db8b | 2013-04-20 17:33:20 +0200 | [diff] [blame] | 838 | /* boolean, returns true if compression is used (either gzip or deflate) in the response */ |
| 839 | static int |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame^] | 840 | smp_fetch_res_comp(struct proxy *px, struct stream *l4, void *l7, unsigned int opt, |
Thierry FOURNIER | f41a809 | 2014-12-07 18:37:57 +0100 | [diff] [blame] | 841 | const struct arg *args, struct sample *smp, const char *kw, void *private) |
William Lallemand | 727db8b | 2013-04-20 17:33:20 +0200 | [diff] [blame] | 842 | { |
| 843 | smp->type = SMP_T_BOOL; |
| 844 | smp->data.uint = (l4->comp_algo != NULL); |
| 845 | return 1; |
| 846 | } |
| 847 | |
| 848 | /* string, returns algo */ |
| 849 | static int |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame^] | 850 | smp_fetch_res_comp_algo(struct proxy *px, struct stream *l4, void *l7, unsigned int opt, |
Thierry FOURNIER | f41a809 | 2014-12-07 18:37:57 +0100 | [diff] [blame] | 851 | const struct arg *args, struct sample *smp, const char *kw, void *private) |
William Lallemand | 727db8b | 2013-04-20 17:33:20 +0200 | [diff] [blame] | 852 | { |
| 853 | if (!l4->comp_algo) |
| 854 | return 0; |
| 855 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 856 | smp->type = SMP_T_STR; |
| 857 | smp->flags = SMP_F_CONST; |
Willy Tarreau | 615105e | 2015-03-28 16:40:46 +0100 | [diff] [blame] | 858 | smp->data.str.str = l4->comp_algo->cfg_name; |
| 859 | smp->data.str.len = l4->comp_algo->cfg_name_len; |
William Lallemand | 727db8b | 2013-04-20 17:33:20 +0200 | [diff] [blame] | 860 | return 1; |
| 861 | } |
| 862 | |
| 863 | /* Note: must not be declared <const> as its list will be overwritten */ |
Willy Tarreau | dc13c11 | 2013-06-21 23:16:39 +0200 | [diff] [blame] | 864 | static struct acl_kw_list acl_kws = {ILH, { |
Willy Tarreau | 7f6fa69 | 2013-04-23 19:39:43 +0200 | [diff] [blame] | 865 | { /* END */ }, |
William Lallemand | 727db8b | 2013-04-20 17:33:20 +0200 | [diff] [blame] | 866 | }}; |
| 867 | |
| 868 | /* Note: must not be declared <const> as its list will be overwritten */ |
Willy Tarreau | dc13c11 | 2013-06-21 23:16:39 +0200 | [diff] [blame] | 869 | static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, { |
William Lallemand | 727db8b | 2013-04-20 17:33:20 +0200 | [diff] [blame] | 870 | { "res.comp", smp_fetch_res_comp, 0, NULL, SMP_T_BOOL, SMP_USE_HRSHP }, |
| 871 | { "res.comp_algo", smp_fetch_res_comp_algo, 0, NULL, SMP_T_STR, SMP_USE_HRSHP }, |
| 872 | { /* END */ }, |
| 873 | }}; |
| 874 | |
| 875 | __attribute__((constructor)) |
| 876 | static void __comp_fetch_init(void) |
| 877 | { |
Willy Tarreau | 418b8c0 | 2015-03-29 03:32:06 +0200 | [diff] [blame] | 878 | #ifdef USE_SLZ |
| 879 | slz_make_crc_table(); |
| 880 | slz_prepare_dist_table(); |
| 881 | #endif |
William Lallemand | 727db8b | 2013-04-20 17:33:20 +0200 | [diff] [blame] | 882 | acl_register_keywords(&acl_kws); |
| 883 | sample_register_fetches(&sample_fetch_keywords); |
| 884 | } |