Christopher Faulet | 3d97c90 | 2015-12-09 14:59:38 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Stream filters related variables and functions. |
| 3 | * |
| 4 | * Copyright (C) 2015 Qualys Inc., Christopher Faulet <cfaulet@qualys.com> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License |
| 8 | * as published by the Free Software Foundation; either version |
| 9 | * 2 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | */ |
| 12 | |
| 13 | #include <common/buffer.h> |
| 14 | #include <common/cfgparse.h> |
Willy Tarreau | b96b77e | 2018-12-11 10:22:41 +0100 | [diff] [blame] | 15 | #include <common/htx.h> |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 16 | #include <common/initcall.h> |
Christopher Faulet | 3d97c90 | 2015-12-09 14:59:38 +0100 | [diff] [blame] | 17 | #include <common/mini-clist.h> |
| 18 | #include <common/standard.h> |
| 19 | |
| 20 | #include <types/compression.h> |
| 21 | #include <types/filters.h> |
Christopher Faulet | 3d97c90 | 2015-12-09 14:59:38 +0100 | [diff] [blame] | 22 | #include <types/proxy.h> |
| 23 | #include <types/sample.h> |
| 24 | |
| 25 | #include <proto/compression.h> |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 26 | #include <proto/filters.h> |
Christopher Faulet | 3d97c90 | 2015-12-09 14:59:38 +0100 | [diff] [blame] | 27 | #include <proto/hdr_idx.h> |
Christopher Faulet | e6902cd | 2018-11-30 22:29:48 +0100 | [diff] [blame] | 28 | #include <proto/http_htx.h> |
Christopher Faulet | 3d97c90 | 2015-12-09 14:59:38 +0100 | [diff] [blame] | 29 | #include <proto/proto_http.h> |
| 30 | #include <proto/sample.h> |
| 31 | #include <proto/stream.h> |
| 32 | |
Christopher Faulet | f4a4ef7 | 2018-12-07 17:39:53 +0100 | [diff] [blame] | 33 | const char *http_comp_flt_id = "compression filter"; |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 34 | |
| 35 | struct flt_ops comp_ops; |
| 36 | |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 37 | struct comp_state { |
| 38 | struct comp_ctx *comp_ctx; /* compression context */ |
| 39 | struct comp_algo *comp_algo; /* compression algorithm if not NULL */ |
Christopher Faulet | e6902cd | 2018-11-30 22:29:48 +0100 | [diff] [blame] | 40 | |
| 41 | /* Following fields are used by the legacy code only: */ |
Christopher Faulet | b77c5c2 | 2015-12-07 16:48:42 +0100 | [diff] [blame] | 42 | int hdrs_len; |
| 43 | int tlrs_len; |
Christopher Faulet | 2fb2880 | 2015-12-01 10:40:57 +0100 | [diff] [blame] | 44 | int consumed; |
| 45 | int initialized; |
Christopher Faulet | b77c5c2 | 2015-12-07 16:48:42 +0100 | [diff] [blame] | 46 | int finished; |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 47 | }; |
| 48 | |
Willy Tarreau | 8ceae72 | 2018-11-26 11:58:30 +0100 | [diff] [blame] | 49 | /* Pools used to allocate comp_state structs */ |
| 50 | DECLARE_STATIC_POOL(pool_head_comp_state, "comp_state", sizeof(struct comp_state)); |
| 51 | |
| 52 | static THREAD_LOCAL struct buffer tmpbuf; |
| 53 | static THREAD_LOCAL struct buffer zbuf; |
| 54 | static THREAD_LOCAL unsigned int buf_output; |
| 55 | |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 56 | static int select_compression_request_header(struct comp_state *st, |
| 57 | struct stream *s, |
| 58 | struct http_msg *msg); |
| 59 | static int select_compression_response_header(struct comp_state *st, |
| 60 | struct stream *s, |
| 61 | struct http_msg *msg); |
| 62 | |
Christopher Faulet | e6902cd | 2018-11-30 22:29:48 +0100 | [diff] [blame] | 63 | static int htx_compression_buffer_init(struct htx *htx, struct buffer *out); |
| 64 | static int htx_compression_buffer_add_data(struct comp_state *st, const char *data, size_t len, |
| 65 | struct buffer *out); |
| 66 | static int htx_compression_buffer_end(struct comp_state *st, struct buffer *out, int end); |
| 67 | |
Willy Tarreau | d54a8ce | 2018-06-29 18:42:02 +0200 | [diff] [blame] | 68 | static int http_compression_buffer_init(struct channel *inc, struct buffer *out, unsigned int *out_len); |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 69 | static int http_compression_buffer_add_data(struct comp_state *st, |
| 70 | struct buffer *in, |
Willy Tarreau | d54a8ce | 2018-06-29 18:42:02 +0200 | [diff] [blame] | 71 | int in_out, |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 72 | struct buffer *out, int sz); |
| 73 | static int http_compression_buffer_end(struct comp_state *st, struct stream *s, |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 74 | struct channel *chn, struct buffer *out, |
Willy Tarreau | d54a8ce | 2018-06-29 18:42:02 +0200 | [diff] [blame] | 75 | unsigned int *out_len, int end); |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 76 | |
| 77 | /***********************************************************************/ |
| 78 | static int |
Christopher Faulet | e6902cd | 2018-11-30 22:29:48 +0100 | [diff] [blame] | 79 | comp_flt_init(struct proxy *px, struct flt_conf *fconf) |
| 80 | { |
Christopher Faulet | 6e54095 | 2018-12-03 22:43:41 +0100 | [diff] [blame] | 81 | fconf->flags |= FLT_CFG_FL_HTX; |
Christopher Faulet | e6902cd | 2018-11-30 22:29:48 +0100 | [diff] [blame] | 82 | return 0; |
| 83 | } |
| 84 | |
| 85 | static int |
Christopher Faulet | 8ca3b4b | 2017-07-25 11:07:15 +0200 | [diff] [blame] | 86 | comp_flt_init_per_thread(struct proxy *px, struct flt_conf *fconf) |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 87 | { |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 88 | if (!tmpbuf.size && b_alloc(&tmpbuf) == NULL) |
Christopher Faulet | b77c5c2 | 2015-12-07 16:48:42 +0100 | [diff] [blame] | 89 | return -1; |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 90 | if (!zbuf.size && b_alloc(&zbuf) == NULL) |
Christopher Faulet | b77c5c2 | 2015-12-07 16:48:42 +0100 | [diff] [blame] | 91 | return -1; |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 92 | return 0; |
| 93 | } |
| 94 | |
| 95 | static void |
Christopher Faulet | 8ca3b4b | 2017-07-25 11:07:15 +0200 | [diff] [blame] | 96 | comp_flt_deinit_per_thread(struct proxy *px, struct flt_conf *fconf) |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 97 | { |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 98 | if (tmpbuf.size) |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 99 | b_free(&tmpbuf); |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 100 | if (zbuf.size) |
Christopher Faulet | b77c5c2 | 2015-12-07 16:48:42 +0100 | [diff] [blame] | 101 | b_free(&zbuf); |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | static int |
| 105 | comp_start_analyze(struct stream *s, struct filter *filter, struct channel *chn) |
| 106 | { |
Christopher Faulet | 8ca3b4b | 2017-07-25 11:07:15 +0200 | [diff] [blame] | 107 | |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 108 | if (filter->ctx == NULL) { |
| 109 | struct comp_state *st; |
| 110 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 111 | st = pool_alloc_dirty(pool_head_comp_state); |
Christopher Faulet | a03d4ad | 2017-06-26 16:53:33 +0200 | [diff] [blame] | 112 | if (st == NULL) |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 113 | return -1; |
| 114 | |
Christopher Faulet | 2fb2880 | 2015-12-01 10:40:57 +0100 | [diff] [blame] | 115 | st->comp_algo = NULL; |
| 116 | st->comp_ctx = NULL; |
Christopher Faulet | b77c5c2 | 2015-12-07 16:48:42 +0100 | [diff] [blame] | 117 | st->hdrs_len = 0; |
| 118 | st->tlrs_len = 0; |
Christopher Faulet | 2fb2880 | 2015-12-01 10:40:57 +0100 | [diff] [blame] | 119 | st->consumed = 0; |
| 120 | st->initialized = 0; |
Christopher Faulet | b77c5c2 | 2015-12-07 16:48:42 +0100 | [diff] [blame] | 121 | st->finished = 0; |
Christopher Faulet | 2fb2880 | 2015-12-01 10:40:57 +0100 | [diff] [blame] | 122 | filter->ctx = st; |
Christopher Faulet | 3dc860d | 2017-09-15 11:39:36 +0200 | [diff] [blame] | 123 | |
| 124 | /* Register post-analyzer on AN_RES_WAIT_HTTP because we need to |
| 125 | * analyze response headers before http-response rules execution |
| 126 | * to be sure we can use res.comp and res.comp_algo sample |
| 127 | * fetches */ |
| 128 | filter->post_analyzers |= AN_RES_WAIT_HTTP; |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 129 | } |
| 130 | return 1; |
| 131 | } |
| 132 | |
| 133 | static int |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 134 | comp_end_analyze(struct stream *s, struct filter *filter, struct channel *chn) |
| 135 | { |
| 136 | struct comp_state *st = filter->ctx; |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 137 | |
Christopher Faulet | d60b3cf | 2017-06-26 11:47:13 +0200 | [diff] [blame] | 138 | if (!st) |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 139 | goto end; |
| 140 | |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 141 | /* release any possible compression context */ |
Christopher Faulet | d60b3cf | 2017-06-26 11:47:13 +0200 | [diff] [blame] | 142 | if (st->comp_algo) |
| 143 | st->comp_algo->end(&st->comp_ctx); |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 144 | pool_free(pool_head_comp_state, st); |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 145 | filter->ctx = NULL; |
| 146 | end: |
| 147 | return 1; |
| 148 | } |
| 149 | |
| 150 | static int |
Christopher Faulet | 1339d74 | 2016-05-11 16:48:33 +0200 | [diff] [blame] | 151 | comp_http_headers(struct stream *s, struct filter *filter, struct http_msg *msg) |
| 152 | { |
| 153 | struct comp_state *st = filter->ctx; |
| 154 | |
| 155 | if (!strm_fe(s)->comp && !s->be->comp) |
| 156 | goto end; |
| 157 | |
| 158 | if (!(msg->chn->flags & CF_ISRESP)) |
| 159 | select_compression_request_header(st, s, msg); |
| 160 | else { |
Christopher Faulet | 3dc860d | 2017-09-15 11:39:36 +0200 | [diff] [blame] | 161 | /* Response headers have already been checked in |
| 162 | * comp_http_post_analyze callback. */ |
Christopher Faulet | 1339d74 | 2016-05-11 16:48:33 +0200 | [diff] [blame] | 163 | if (st->comp_algo) { |
| 164 | register_data_filter(s, msg->chn, filter); |
Christopher Faulet | e6902cd | 2018-11-30 22:29:48 +0100 | [diff] [blame] | 165 | if (!IS_HTX_STRM(s)) |
| 166 | st->hdrs_len = s->txn->rsp.sov; |
Christopher Faulet | 1339d74 | 2016-05-11 16:48:33 +0200 | [diff] [blame] | 167 | } |
| 168 | } |
| 169 | |
| 170 | end: |
| 171 | return 1; |
| 172 | } |
| 173 | |
| 174 | static int |
Christopher Faulet | 3dc860d | 2017-09-15 11:39:36 +0200 | [diff] [blame] | 175 | comp_http_post_analyze(struct stream *s, struct filter *filter, |
| 176 | struct channel *chn, unsigned an_bit) |
| 177 | { |
| 178 | struct http_txn *txn = s->txn; |
| 179 | struct http_msg *msg = &txn->rsp; |
| 180 | struct comp_state *st = filter->ctx; |
| 181 | |
| 182 | if (an_bit != AN_RES_WAIT_HTTP) |
| 183 | goto end; |
| 184 | |
| 185 | if (!strm_fe(s)->comp && !s->be->comp) |
| 186 | goto end; |
| 187 | |
| 188 | select_compression_response_header(st, s, msg); |
| 189 | |
| 190 | end: |
| 191 | return 1; |
| 192 | } |
| 193 | |
| 194 | static int |
Christopher Faulet | e6902cd | 2018-11-30 22:29:48 +0100 | [diff] [blame] | 195 | comp_http_payload(struct stream *s, struct filter *filter, struct http_msg *msg, |
| 196 | unsigned int offset, unsigned int len) |
| 197 | { |
| 198 | struct comp_state *st = filter->ctx; |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 199 | struct htx *htx = htxbuf(&msg->chn->buf); |
Christopher Faulet | e6902cd | 2018-11-30 22:29:48 +0100 | [diff] [blame] | 200 | struct htx_blk *blk; |
| 201 | struct htx_ret htx_ret; |
| 202 | int ret, consumed = 0, to_forward = 0; |
| 203 | |
| 204 | htx_ret = htx_find_blk(htx, offset); |
| 205 | blk = htx_ret.blk; |
| 206 | offset = htx_ret.ret; |
| 207 | |
| 208 | while (blk && len) { |
| 209 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 210 | uint32_t sz = htx_get_blksz(blk); |
| 211 | struct ist v; |
| 212 | |
| 213 | switch (type) { |
| 214 | case HTX_BLK_UNUSED: |
| 215 | break; |
| 216 | |
| 217 | case HTX_BLK_DATA: |
| 218 | v = htx_get_blk_value(htx, blk); |
| 219 | v.ptr += offset; |
| 220 | v.len -= offset; |
| 221 | if (v.len > len) |
| 222 | v.len = len; |
| 223 | if (htx_compression_buffer_init(htx, &trash) < 0) { |
| 224 | msg->chn->flags |= CF_WAKE_WRITE; |
| 225 | goto end; |
| 226 | } |
| 227 | ret = htx_compression_buffer_add_data(st, v.ptr, v.len, &trash); |
| 228 | if (ret < 0) |
| 229 | goto error; |
| 230 | if (htx_compression_buffer_end(st, &trash, 0) < 0) |
| 231 | goto error; |
| 232 | len -= ret; |
| 233 | consumed += ret; |
| 234 | to_forward += b_data(&trash); |
| 235 | if (ret == sz && !b_data(&trash)) { |
| 236 | offset = 0; |
| 237 | blk = htx_remove_blk(htx, blk); |
| 238 | continue; |
| 239 | } |
| 240 | v.len = ret; |
| 241 | blk = htx_replace_blk_value(htx, blk, v, ist2(b_head(&trash), b_data(&trash))); |
| 242 | break; |
| 243 | |
| 244 | case HTX_BLK_EOD: |
| 245 | case HTX_BLK_TLR: |
| 246 | case HTX_BLK_EOM: |
| 247 | if (msg->flags & HTTP_MSGF_COMPRESSING) { |
| 248 | if (htx_compression_buffer_init(htx, &trash) < 0) { |
| 249 | msg->chn->flags |= CF_WAKE_WRITE; |
| 250 | goto end; |
| 251 | } |
| 252 | if (htx_compression_buffer_end(st, &trash, 1) < 0) |
| 253 | goto error; |
| 254 | blk = htx_add_data_before(htx, blk, ist2(b_head(&trash), b_data(&trash))); |
| 255 | if (!blk) |
| 256 | goto error; |
| 257 | to_forward += b_data(&trash); |
| 258 | msg->flags &= ~HTTP_MSGF_COMPRESSING; |
| 259 | /* We let the mux add last empty chunk and empty trailers */ |
| 260 | } |
| 261 | /* fall through */ |
| 262 | |
| 263 | default: |
| 264 | sz -= offset; |
| 265 | if (sz > len) |
| 266 | sz = len; |
| 267 | consumed += sz; |
| 268 | to_forward += sz; |
| 269 | len -= sz; |
| 270 | break; |
| 271 | } |
| 272 | |
| 273 | offset = 0; |
| 274 | blk = htx_get_next_blk(htx, blk); |
| 275 | } |
| 276 | |
| 277 | end: |
| 278 | if (to_forward != consumed) |
| 279 | flt_update_offsets(filter, msg->chn, to_forward - consumed); |
| 280 | |
| 281 | if (st->comp_ctx && st->comp_ctx->cur_lvl > 0) { |
| 282 | update_freq_ctr(&global.comp_bps_out, to_forward); |
| 283 | HA_ATOMIC_ADD(&strm_fe(s)->fe_counters.comp_out, to_forward); |
| 284 | HA_ATOMIC_ADD(&s->be->be_counters.comp_out, to_forward); |
| 285 | } |
| 286 | return to_forward; |
| 287 | |
| 288 | error: |
| 289 | return -1; |
| 290 | } |
| 291 | |
| 292 | static int |
Christopher Faulet | 2fb2880 | 2015-12-01 10:40:57 +0100 | [diff] [blame] | 293 | comp_http_data(struct stream *s, struct filter *filter, struct http_msg *msg) |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 294 | { |
| 295 | struct comp_state *st = filter->ctx; |
Willy Tarreau | bcbd393 | 2018-06-06 07:13:22 +0200 | [diff] [blame] | 296 | struct channel *chn = msg->chn; |
Christopher Faulet | 3e7bc67 | 2015-12-07 13:39:08 +0100 | [diff] [blame] | 297 | unsigned int *nxt = &flt_rsp_nxt(filter); |
Christopher Faulet | 2fb2880 | 2015-12-01 10:40:57 +0100 | [diff] [blame] | 298 | unsigned int len; |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 299 | int ret; |
| 300 | |
Olivier Houchard | 0b66284 | 2018-06-29 18:16:31 +0200 | [diff] [blame] | 301 | len = MIN(msg->chunk_len + msg->next, ci_data(chn)) - *nxt; |
Christopher Faulet | 2fb2880 | 2015-12-01 10:40:57 +0100 | [diff] [blame] | 302 | if (!len) |
| 303 | return len; |
| 304 | |
| 305 | if (!st->initialized) { |
Christopher Faulet | b77c5c2 | 2015-12-07 16:48:42 +0100 | [diff] [blame] | 306 | unsigned int fwd = flt_rsp_fwd(filter) + st->hdrs_len; |
Christopher Faulet | 3e7bc67 | 2015-12-07 13:39:08 +0100 | [diff] [blame] | 307 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 308 | b_reset(&tmpbuf); |
Willy Tarreau | bcbd393 | 2018-06-06 07:13:22 +0200 | [diff] [blame] | 309 | c_adv(chn, fwd); |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 310 | ret = http_compression_buffer_init(chn, &zbuf, &buf_output); |
Willy Tarreau | bcbd393 | 2018-06-06 07:13:22 +0200 | [diff] [blame] | 311 | c_rew(chn, fwd); |
Christopher Faulet | 2fb2880 | 2015-12-01 10:40:57 +0100 | [diff] [blame] | 312 | if (ret < 0) { |
| 313 | msg->chn->flags |= CF_WAKE_WRITE; |
| 314 | return 0; |
| 315 | } |
| 316 | } |
Christopher Faulet | b77c5c2 | 2015-12-07 16:48:42 +0100 | [diff] [blame] | 317 | |
| 318 | if (msg->flags & HTTP_MSGF_TE_CHNK) { |
Christopher Faulet | 06ecf3a | 2016-09-22 15:31:43 +0200 | [diff] [blame] | 319 | int block; |
Christopher Faulet | b77c5c2 | 2015-12-07 16:48:42 +0100 | [diff] [blame] | 320 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 321 | len = MIN(b_room(&tmpbuf), len); |
Christopher Faulet | 06ecf3a | 2016-09-22 15:31:43 +0200 | [diff] [blame] | 322 | |
Willy Tarreau | bcbd393 | 2018-06-06 07:13:22 +0200 | [diff] [blame] | 323 | c_adv(chn, *nxt); |
Willy Tarreau | 7194d3c | 2018-06-06 16:55:45 +0200 | [diff] [blame] | 324 | block = ci_contig_data(chn); |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 325 | memcpy(b_tail(&tmpbuf), ci_head(chn), block); |
Christopher Faulet | 06ecf3a | 2016-09-22 15:31:43 +0200 | [diff] [blame] | 326 | if (len > block) |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 327 | memcpy(b_tail(&tmpbuf)+block, b_orig(&chn->buf), len-block); |
Willy Tarreau | bcbd393 | 2018-06-06 07:13:22 +0200 | [diff] [blame] | 328 | c_rew(chn, *nxt); |
Christopher Faulet | 06ecf3a | 2016-09-22 15:31:43 +0200 | [diff] [blame] | 329 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 330 | b_add(&tmpbuf, len); |
Christopher Faulet | b77c5c2 | 2015-12-07 16:48:42 +0100 | [diff] [blame] | 331 | ret = len; |
| 332 | } |
| 333 | else { |
Willy Tarreau | bcbd393 | 2018-06-06 07:13:22 +0200 | [diff] [blame] | 334 | c_adv(chn, *nxt); |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 335 | ret = http_compression_buffer_add_data(st, &chn->buf, co_data(chn), &zbuf, len); |
Willy Tarreau | bcbd393 | 2018-06-06 07:13:22 +0200 | [diff] [blame] | 336 | c_rew(chn, *nxt); |
Christopher Faulet | b77c5c2 | 2015-12-07 16:48:42 +0100 | [diff] [blame] | 337 | if (ret < 0) |
| 338 | return ret; |
| 339 | } |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 340 | |
Christopher Faulet | 2fb2880 | 2015-12-01 10:40:57 +0100 | [diff] [blame] | 341 | st->initialized = 1; |
| 342 | msg->next += ret; |
| 343 | msg->chunk_len -= ret; |
Christopher Faulet | 3e7bc67 | 2015-12-07 13:39:08 +0100 | [diff] [blame] | 344 | *nxt = msg->next; |
Christopher Faulet | 2fb2880 | 2015-12-01 10:40:57 +0100 | [diff] [blame] | 345 | return 0; |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 346 | } |
| 347 | |
| 348 | static int |
Christopher Faulet | 2fb2880 | 2015-12-01 10:40:57 +0100 | [diff] [blame] | 349 | comp_http_chunk_trailers(struct stream *s, struct filter *filter, |
| 350 | struct http_msg *msg) |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 351 | { |
| 352 | struct comp_state *st = filter->ctx; |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 353 | |
Christopher Faulet | b77c5c2 | 2015-12-07 16:48:42 +0100 | [diff] [blame] | 354 | if (!st->initialized) { |
| 355 | if (!st->finished) { |
Willy Tarreau | bcbd393 | 2018-06-06 07:13:22 +0200 | [diff] [blame] | 356 | struct channel *chn = msg->chn; |
Christopher Faulet | b77c5c2 | 2015-12-07 16:48:42 +0100 | [diff] [blame] | 357 | unsigned int fwd = flt_rsp_fwd(filter) + st->hdrs_len; |
Christopher Faulet | 2fb2880 | 2015-12-01 10:40:57 +0100 | [diff] [blame] | 358 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 359 | b_reset(&tmpbuf); |
Willy Tarreau | bcbd393 | 2018-06-06 07:13:22 +0200 | [diff] [blame] | 360 | c_adv(chn, fwd); |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 361 | http_compression_buffer_init(chn, &zbuf, &buf_output); |
Willy Tarreau | bcbd393 | 2018-06-06 07:13:22 +0200 | [diff] [blame] | 362 | c_rew(chn, fwd); |
Christopher Faulet | b77c5c2 | 2015-12-07 16:48:42 +0100 | [diff] [blame] | 363 | st->initialized = 1; |
| 364 | } |
| 365 | } |
| 366 | st->tlrs_len = msg->sol; |
Christopher Faulet | 2fb2880 | 2015-12-01 10:40:57 +0100 | [diff] [blame] | 367 | return 1; |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 368 | } |
| 369 | |
Christopher Faulet | 2fb2880 | 2015-12-01 10:40:57 +0100 | [diff] [blame] | 370 | |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 371 | static int |
| 372 | comp_http_forward_data(struct stream *s, struct filter *filter, |
| 373 | struct http_msg *msg, unsigned int len) |
| 374 | { |
| 375 | struct comp_state *st = filter->ctx; |
Christopher Faulet | 2fb2880 | 2015-12-01 10:40:57 +0100 | [diff] [blame] | 376 | int ret; |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 377 | |
Christopher Faulet | 2fb2880 | 2015-12-01 10:40:57 +0100 | [diff] [blame] | 378 | /* To work, previous filters MUST forward all data */ |
Christopher Faulet | b77c5c2 | 2015-12-07 16:48:42 +0100 | [diff] [blame] | 379 | if (flt_rsp_fwd(filter) + len != flt_rsp_nxt(filter)) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 380 | ha_warning("HTTP compression failed: unexpected behavior of previous filters\n"); |
Christopher Faulet | 2fb2880 | 2015-12-01 10:40:57 +0100 | [diff] [blame] | 381 | return -1; |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 382 | } |
| 383 | |
Christopher Faulet | 2fb2880 | 2015-12-01 10:40:57 +0100 | [diff] [blame] | 384 | if (!st->initialized) { |
Christopher Faulet | b77c5c2 | 2015-12-07 16:48:42 +0100 | [diff] [blame] | 385 | if (!len) { |
Joseph Herlant | 942eea3 | 2018-11-15 13:57:22 -0800 | [diff] [blame] | 386 | /* Nothing to forward */ |
Christopher Faulet | b77c5c2 | 2015-12-07 16:48:42 +0100 | [diff] [blame] | 387 | ret = len; |
| 388 | } |
| 389 | else if (st->hdrs_len > len) { |
| 390 | /* Forward part of headers */ |
| 391 | ret = len; |
| 392 | st->hdrs_len -= len; |
| 393 | } |
| 394 | else if (st->hdrs_len > 0) { |
| 395 | /* Forward remaining headers */ |
| 396 | ret = st->hdrs_len; |
| 397 | st->hdrs_len = 0; |
| 398 | } |
| 399 | else if (msg->msg_state < HTTP_MSG_TRAILERS) { |
| 400 | /* Do not forward anything for now. This only happens |
| 401 | * with chunk-encoded responses. Waiting data are part |
| 402 | * of the chunk envelope (the chunk size or the chunk |
| 403 | * CRLF). These data will be skipped during the |
| 404 | * compression. */ |
| 405 | ret = 0; |
| 406 | } |
| 407 | else { |
| 408 | /* Forward trailers data */ |
| 409 | ret = len; |
| 410 | } |
Christopher Faulet | 2fb2880 | 2015-12-01 10:40:57 +0100 | [diff] [blame] | 411 | return ret; |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 412 | } |
| 413 | |
Christopher Faulet | b77c5c2 | 2015-12-07 16:48:42 +0100 | [diff] [blame] | 414 | if (msg->flags & HTTP_MSGF_TE_CHNK) { |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 415 | ret = http_compression_buffer_add_data(st, &tmpbuf, 0, |
| 416 | &zbuf, b_data(&tmpbuf)); |
| 417 | if (ret != b_data(&tmpbuf)) { |
Willy Tarreau | 506a29a | 2018-07-18 10:07:58 +0200 | [diff] [blame] | 418 | ha_warning("HTTP compression failed: Must consume %u bytes but only %d bytes consumed\n", |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 419 | (unsigned int)b_data(&tmpbuf), ret); |
Christopher Faulet | b77c5c2 | 2015-12-07 16:48:42 +0100 | [diff] [blame] | 420 | return -1; |
| 421 | } |
| 422 | } |
| 423 | |
| 424 | st->consumed = len - st->hdrs_len - st->tlrs_len; |
Willy Tarreau | bcbd393 | 2018-06-06 07:13:22 +0200 | [diff] [blame] | 425 | c_adv(msg->chn, flt_rsp_fwd(filter) + st->hdrs_len); |
Willy Tarreau | d54a8ce | 2018-06-29 18:42:02 +0200 | [diff] [blame] | 426 | ret = http_compression_buffer_end(st, s, msg->chn, &zbuf, &buf_output, msg->msg_state >= HTTP_MSG_TRAILERS); |
Willy Tarreau | bcbd393 | 2018-06-06 07:13:22 +0200 | [diff] [blame] | 427 | c_rew(msg->chn, flt_rsp_fwd(filter) + st->hdrs_len); |
Christopher Faulet | 2fb2880 | 2015-12-01 10:40:57 +0100 | [diff] [blame] | 428 | if (ret < 0) |
| 429 | return ret; |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 430 | |
Christopher Faulet | b77c5c2 | 2015-12-07 16:48:42 +0100 | [diff] [blame] | 431 | flt_change_forward_size(filter, msg->chn, ret - st->consumed); |
| 432 | msg->next += (ret - st->consumed); |
| 433 | ret += st->hdrs_len + st->tlrs_len; |
| 434 | |
Christopher Faulet | 2fb2880 | 2015-12-01 10:40:57 +0100 | [diff] [blame] | 435 | st->initialized = 0; |
Christopher Faulet | b77c5c2 | 2015-12-07 16:48:42 +0100 | [diff] [blame] | 436 | st->finished = (msg->msg_state >= HTTP_MSG_TRAILERS); |
| 437 | st->hdrs_len = 0; |
| 438 | st->tlrs_len = 0; |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 439 | return ret; |
| 440 | } |
Christopher Faulet | 3d97c90 | 2015-12-09 14:59:38 +0100 | [diff] [blame] | 441 | |
Christopher Faulet | d60b3cf | 2017-06-26 11:47:13 +0200 | [diff] [blame] | 442 | static int |
| 443 | comp_http_end(struct stream *s, struct filter *filter, |
| 444 | struct http_msg *msg) |
| 445 | { |
| 446 | struct comp_state *st = filter->ctx; |
| 447 | |
| 448 | if (!(msg->chn->flags & CF_ISRESP) || !st || !st->comp_algo) |
| 449 | goto end; |
| 450 | |
| 451 | if (strm_fe(s)->mode == PR_MODE_HTTP) |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 452 | HA_ATOMIC_ADD(&strm_fe(s)->fe_counters.p.http.comp_rsp, 1); |
Christopher Faulet | d60b3cf | 2017-06-26 11:47:13 +0200 | [diff] [blame] | 453 | if ((s->flags & SF_BE_ASSIGNED) && (s->be->mode == PR_MODE_HTTP)) |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 454 | HA_ATOMIC_ADD(&s->be->be_counters.p.http.comp_rsp, 1); |
Christopher Faulet | d60b3cf | 2017-06-26 11:47:13 +0200 | [diff] [blame] | 455 | end: |
| 456 | return 1; |
| 457 | } |
Christopher Faulet | 3d97c90 | 2015-12-09 14:59:38 +0100 | [diff] [blame] | 458 | /***********************************************************************/ |
| 459 | /* |
| 460 | * Selects a compression algorithm depending on the client request. |
| 461 | */ |
Christopher Faulet | e6902cd | 2018-11-30 22:29:48 +0100 | [diff] [blame] | 462 | static int |
| 463 | http_select_comp_reqhdr(struct comp_state *st, struct stream *s, struct http_msg *msg) |
Christopher Faulet | 3d97c90 | 2015-12-09 14:59:38 +0100 | [diff] [blame] | 464 | { |
| 465 | struct http_txn *txn = s->txn; |
Olivier Houchard | 0b66284 | 2018-06-29 18:16:31 +0200 | [diff] [blame] | 466 | struct channel *req = msg->chn; |
Christopher Faulet | 3d97c90 | 2015-12-09 14:59:38 +0100 | [diff] [blame] | 467 | struct hdr_ctx ctx; |
| 468 | struct comp_algo *comp_algo = NULL; |
| 469 | struct comp_algo *comp_algo_back = NULL; |
| 470 | |
| 471 | /* Disable compression for older user agents announcing themselves as "Mozilla/4" |
| 472 | * unless they are known good (MSIE 6 with XP SP2, or MSIE 7 and later). |
| 473 | * See http://zoompf.com/2012/02/lose-the-wait-http-compression for more details. |
| 474 | */ |
| 475 | ctx.idx = 0; |
Olivier Houchard | 0b66284 | 2018-06-29 18:16:31 +0200 | [diff] [blame] | 476 | if (http_find_header2("User-Agent", 10, ci_head(req), &txn->hdr_idx, &ctx) && |
Christopher Faulet | 3d97c90 | 2015-12-09 14:59:38 +0100 | [diff] [blame] | 477 | ctx.vlen >= 9 && |
| 478 | memcmp(ctx.line + ctx.val, "Mozilla/4", 9) == 0 && |
| 479 | (ctx.vlen < 31 || |
| 480 | memcmp(ctx.line + ctx.val + 25, "MSIE ", 5) != 0 || |
| 481 | ctx.line[ctx.val + 30] < '6' || |
| 482 | (ctx.line[ctx.val + 30] == '6' && |
| 483 | (ctx.vlen < 54 || memcmp(ctx.line + 51, "SV1", 3) != 0)))) { |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 484 | st->comp_algo = NULL; |
Christopher Faulet | 3d97c90 | 2015-12-09 14:59:38 +0100 | [diff] [blame] | 485 | return 0; |
| 486 | } |
| 487 | |
| 488 | /* search for the algo in the backend in priority or the frontend */ |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 489 | if ((s->be->comp && (comp_algo_back = s->be->comp->algos)) || |
| 490 | (strm_fe(s)->comp && (comp_algo_back = strm_fe(s)->comp->algos))) { |
Christopher Faulet | 3d97c90 | 2015-12-09 14:59:38 +0100 | [diff] [blame] | 491 | int best_q = 0; |
| 492 | |
| 493 | ctx.idx = 0; |
Olivier Houchard | 0b66284 | 2018-06-29 18:16:31 +0200 | [diff] [blame] | 494 | while (http_find_header2("Accept-Encoding", 15, ci_head(req), &txn->hdr_idx, &ctx)) { |
Christopher Faulet | 3d97c90 | 2015-12-09 14:59:38 +0100 | [diff] [blame] | 495 | const char *qval; |
| 496 | int q; |
| 497 | int toklen; |
| 498 | |
| 499 | /* try to isolate the token from the optional q-value */ |
| 500 | toklen = 0; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 501 | while (toklen < ctx.vlen && HTTP_IS_TOKEN(*(ctx.line + ctx.val + toklen))) |
Christopher Faulet | 3d97c90 | 2015-12-09 14:59:38 +0100 | [diff] [blame] | 502 | toklen++; |
| 503 | |
| 504 | qval = ctx.line + ctx.val + toklen; |
| 505 | while (1) { |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 506 | while (qval < ctx.line + ctx.val + ctx.vlen && HTTP_IS_LWS(*qval)) |
Christopher Faulet | 3d97c90 | 2015-12-09 14:59:38 +0100 | [diff] [blame] | 507 | qval++; |
| 508 | |
| 509 | if (qval >= ctx.line + ctx.val + ctx.vlen || *qval != ';') { |
| 510 | qval = NULL; |
| 511 | break; |
| 512 | } |
| 513 | qval++; |
| 514 | |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 515 | while (qval < ctx.line + ctx.val + ctx.vlen && HTTP_IS_LWS(*qval)) |
Christopher Faulet | 3d97c90 | 2015-12-09 14:59:38 +0100 | [diff] [blame] | 516 | qval++; |
| 517 | |
| 518 | if (qval >= ctx.line + ctx.val + ctx.vlen) { |
| 519 | qval = NULL; |
| 520 | break; |
| 521 | } |
| 522 | if (strncmp(qval, "q=", MIN(ctx.line + ctx.val + ctx.vlen - qval, 2)) == 0) |
| 523 | break; |
| 524 | |
| 525 | while (qval < ctx.line + ctx.val + ctx.vlen && *qval != ';') |
| 526 | qval++; |
| 527 | } |
| 528 | |
| 529 | /* here we have qval pointing to the first "q=" attribute or NULL if not found */ |
Willy Tarreau | ab813a4 | 2018-09-10 18:41:28 +0200 | [diff] [blame] | 530 | q = qval ? http_parse_qvalue(qval + 2, NULL) : 1000; |
Christopher Faulet | 3d97c90 | 2015-12-09 14:59:38 +0100 | [diff] [blame] | 531 | |
| 532 | if (q <= best_q) |
| 533 | continue; |
| 534 | |
| 535 | for (comp_algo = comp_algo_back; comp_algo; comp_algo = comp_algo->next) { |
| 536 | if (*(ctx.line + ctx.val) == '*' || |
| 537 | word_match(ctx.line + ctx.val, toklen, comp_algo->ua_name, comp_algo->ua_name_len)) { |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 538 | st->comp_algo = comp_algo; |
Christopher Faulet | 3d97c90 | 2015-12-09 14:59:38 +0100 | [diff] [blame] | 539 | best_q = q; |
| 540 | break; |
| 541 | } |
| 542 | } |
| 543 | } |
| 544 | } |
| 545 | |
| 546 | /* remove all occurrences of the header when "compression offload" is set */ |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 547 | if (st->comp_algo) { |
| 548 | if ((s->be->comp && s->be->comp->offload) || |
| 549 | (strm_fe(s)->comp && strm_fe(s)->comp->offload)) { |
Christopher Faulet | 3d97c90 | 2015-12-09 14:59:38 +0100 | [diff] [blame] | 550 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
| 551 | ctx.idx = 0; |
Olivier Houchard | 0b66284 | 2018-06-29 18:16:31 +0200 | [diff] [blame] | 552 | while (http_find_header2("Accept-Encoding", 15, ci_head(req), &txn->hdr_idx, &ctx)) { |
Christopher Faulet | 3d97c90 | 2015-12-09 14:59:38 +0100 | [diff] [blame] | 553 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
| 554 | } |
| 555 | } |
Christopher Faulet | e6902cd | 2018-11-30 22:29:48 +0100 | [diff] [blame] | 556 | return 1; |
| 557 | } |
| 558 | |
| 559 | /* identity is implicit does not require headers */ |
| 560 | if ((s->be->comp && (comp_algo_back = s->be->comp->algos)) || |
| 561 | (strm_fe(s)->comp && (comp_algo_back = strm_fe(s)->comp->algos))) { |
| 562 | for (comp_algo = comp_algo_back; comp_algo; comp_algo = comp_algo->next) { |
| 563 | if (comp_algo->cfg_name_len == 8 && memcmp(comp_algo->cfg_name, "identity", 8) == 0) { |
| 564 | st->comp_algo = comp_algo; |
| 565 | return 1; |
| 566 | } |
| 567 | } |
| 568 | } |
| 569 | |
| 570 | st->comp_algo = NULL; |
| 571 | return 0; |
| 572 | } |
| 573 | |
| 574 | static int |
| 575 | htx_select_comp_reqhdr(struct comp_state *st, struct stream *s, struct http_msg *msg) |
| 576 | { |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 577 | struct htx *htx = htxbuf(&msg->chn->buf); |
Christopher Faulet | e6902cd | 2018-11-30 22:29:48 +0100 | [diff] [blame] | 578 | struct http_hdr_ctx ctx; |
| 579 | struct comp_algo *comp_algo = NULL; |
| 580 | struct comp_algo *comp_algo_back = NULL; |
| 581 | |
| 582 | /* Disable compression for older user agents announcing themselves as "Mozilla/4" |
| 583 | * unless they are known good (MSIE 6 with XP SP2, or MSIE 7 and later). |
| 584 | * See http://zoompf.com/2012/02/lose-the-wait-http-compression for more details. |
| 585 | */ |
| 586 | ctx.blk = NULL; |
| 587 | if (http_find_header(htx, ist("User-Agent"), &ctx, 1) && |
| 588 | ctx.value.len >= 9 && |
| 589 | memcmp(ctx.value.ptr, "Mozilla/4", 9) == 0 && |
| 590 | (ctx.value.len < 31 || |
| 591 | memcmp(ctx.value.ptr + 25, "MSIE ", 5) != 0 || |
| 592 | *(ctx.value.ptr + 30) < '6' || |
| 593 | (*(ctx.value.ptr + 30) == '6' && |
| 594 | (ctx.value.len < 54 || memcmp(ctx.value.ptr + 51, "SV1", 3) != 0)))) { |
| 595 | st->comp_algo = NULL; |
| 596 | return 0; |
| 597 | } |
| 598 | |
| 599 | /* search for the algo in the backend in priority or the frontend */ |
| 600 | if ((s->be->comp && (comp_algo_back = s->be->comp->algos)) || |
| 601 | (strm_fe(s)->comp && (comp_algo_back = strm_fe(s)->comp->algos))) { |
| 602 | int best_q = 0; |
| 603 | |
| 604 | ctx.blk = NULL; |
| 605 | while (http_find_header(htx, ist("Accept-Encoding"), &ctx, 0)) { |
| 606 | const char *qval; |
| 607 | int q; |
| 608 | int toklen; |
| 609 | |
| 610 | /* try to isolate the token from the optional q-value */ |
| 611 | toklen = 0; |
| 612 | while (toklen < ctx.value.len && HTTP_IS_TOKEN(*(ctx.value.ptr + toklen))) |
| 613 | toklen++; |
| 614 | |
| 615 | qval = ctx.value.ptr + toklen; |
| 616 | while (1) { |
| 617 | while (qval < ctx.value.ptr + ctx.value.len && HTTP_IS_LWS(*qval)) |
| 618 | qval++; |
| 619 | |
| 620 | if (qval >= ctx.value.ptr + ctx.value.len || *qval != ';') { |
| 621 | qval = NULL; |
| 622 | break; |
| 623 | } |
| 624 | qval++; |
| 625 | |
| 626 | while (qval < ctx.value.ptr + ctx.value.len && HTTP_IS_LWS(*qval)) |
| 627 | qval++; |
| 628 | |
| 629 | if (qval >= ctx.value.ptr + ctx.value.len) { |
| 630 | qval = NULL; |
| 631 | break; |
| 632 | } |
| 633 | if (strncmp(qval, "q=", MIN(ctx.value.ptr + ctx.value.len - qval, 2)) == 0) |
| 634 | break; |
| 635 | |
| 636 | while (qval < ctx.value.ptr + ctx.value.len && *qval != ';') |
| 637 | qval++; |
| 638 | } |
| 639 | |
| 640 | /* here we have qval pointing to the first "q=" attribute or NULL if not found */ |
| 641 | q = qval ? http_parse_qvalue(qval + 2, NULL) : 1000; |
| 642 | |
| 643 | if (q <= best_q) |
| 644 | continue; |
| 645 | |
| 646 | for (comp_algo = comp_algo_back; comp_algo; comp_algo = comp_algo->next) { |
| 647 | if (*(ctx.value.ptr) == '*' || |
| 648 | word_match(ctx.value.ptr, toklen, comp_algo->ua_name, comp_algo->ua_name_len)) { |
| 649 | st->comp_algo = comp_algo; |
| 650 | best_q = q; |
| 651 | break; |
| 652 | } |
| 653 | } |
| 654 | } |
| 655 | } |
| 656 | |
| 657 | /* remove all occurrences of the header when "compression offload" is set */ |
| 658 | if (st->comp_algo) { |
| 659 | if ((s->be->comp && s->be->comp->offload) || |
| 660 | (strm_fe(s)->comp && strm_fe(s)->comp->offload)) { |
| 661 | http_remove_header(htx, &ctx); |
| 662 | ctx.blk = NULL; |
| 663 | while (http_find_header(htx, ist("Accept-Encoding"), &ctx, 1)) |
| 664 | http_remove_header(htx, &ctx); |
| 665 | } |
Christopher Faulet | 3d97c90 | 2015-12-09 14:59:38 +0100 | [diff] [blame] | 666 | return 1; |
| 667 | } |
| 668 | |
| 669 | /* identity is implicit does not require headers */ |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 670 | if ((s->be->comp && (comp_algo_back = s->be->comp->algos)) || |
| 671 | (strm_fe(s)->comp && (comp_algo_back = strm_fe(s)->comp->algos))) { |
Christopher Faulet | 3d97c90 | 2015-12-09 14:59:38 +0100 | [diff] [blame] | 672 | for (comp_algo = comp_algo_back; comp_algo; comp_algo = comp_algo->next) { |
| 673 | if (comp_algo->cfg_name_len == 8 && memcmp(comp_algo->cfg_name, "identity", 8) == 0) { |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 674 | st->comp_algo = comp_algo; |
Christopher Faulet | 3d97c90 | 2015-12-09 14:59:38 +0100 | [diff] [blame] | 675 | return 1; |
| 676 | } |
| 677 | } |
| 678 | } |
| 679 | |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 680 | st->comp_algo = NULL; |
Christopher Faulet | 3d97c90 | 2015-12-09 14:59:38 +0100 | [diff] [blame] | 681 | return 0; |
| 682 | } |
| 683 | |
Christopher Faulet | e6902cd | 2018-11-30 22:29:48 +0100 | [diff] [blame] | 684 | static int |
| 685 | select_compression_request_header(struct comp_state *st, struct stream *s, |
| 686 | struct http_msg *msg) |
| 687 | { |
| 688 | if (IS_HTX_STRM(s)) |
| 689 | return htx_select_comp_reqhdr(st, s, msg); |
| 690 | else |
| 691 | return http_select_comp_reqhdr(st, s, msg); |
| 692 | } |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 693 | |
Christopher Faulet | 3d97c90 | 2015-12-09 14:59:38 +0100 | [diff] [blame] | 694 | /* |
| 695 | * Selects a comression algorithm depending of the server response. |
| 696 | */ |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 697 | static int |
Christopher Faulet | e6902cd | 2018-11-30 22:29:48 +0100 | [diff] [blame] | 698 | http_select_comp_reshdr(struct comp_state *st, struct stream *s, struct http_msg *msg) |
Christopher Faulet | 3d97c90 | 2015-12-09 14:59:38 +0100 | [diff] [blame] | 699 | { |
| 700 | struct http_txn *txn = s->txn; |
Olivier Houchard | 0b66284 | 2018-06-29 18:16:31 +0200 | [diff] [blame] | 701 | struct channel *c = msg->chn; |
Christopher Faulet | 3d97c90 | 2015-12-09 14:59:38 +0100 | [diff] [blame] | 702 | struct hdr_ctx ctx; |
| 703 | struct comp_type *comp_type; |
| 704 | |
| 705 | /* no common compression algorithm was found in request header */ |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 706 | if (st->comp_algo == NULL) |
Christopher Faulet | 3d97c90 | 2015-12-09 14:59:38 +0100 | [diff] [blame] | 707 | goto fail; |
| 708 | |
| 709 | /* HTTP < 1.1 should not be compressed */ |
| 710 | if (!(msg->flags & HTTP_MSGF_VER_11) || !(txn->req.flags & HTTP_MSGF_VER_11)) |
| 711 | goto fail; |
| 712 | |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 713 | if (txn->meth == HTTP_METH_HEAD) |
| 714 | goto fail; |
| 715 | |
Christopher Faulet | 3d97c90 | 2015-12-09 14:59:38 +0100 | [diff] [blame] | 716 | /* compress 200,201,202,203 responses only */ |
| 717 | if ((txn->status != 200) && |
| 718 | (txn->status != 201) && |
| 719 | (txn->status != 202) && |
| 720 | (txn->status != 203)) |
| 721 | goto fail; |
| 722 | |
| 723 | |
| 724 | /* Content-Length is null */ |
| 725 | if (!(msg->flags & HTTP_MSGF_TE_CHNK) && msg->body_len == 0) |
| 726 | goto fail; |
| 727 | |
| 728 | /* content is already compressed */ |
| 729 | ctx.idx = 0; |
Olivier Houchard | 0b66284 | 2018-06-29 18:16:31 +0200 | [diff] [blame] | 730 | if (http_find_header2("Content-Encoding", 16, ci_head(c), &txn->hdr_idx, &ctx)) |
Christopher Faulet | 3d97c90 | 2015-12-09 14:59:38 +0100 | [diff] [blame] | 731 | goto fail; |
| 732 | |
| 733 | /* no compression when Cache-Control: no-transform is present in the message */ |
| 734 | ctx.idx = 0; |
Olivier Houchard | 0b66284 | 2018-06-29 18:16:31 +0200 | [diff] [blame] | 735 | while (http_find_header2("Cache-Control", 13, ci_head(c), &txn->hdr_idx, &ctx)) { |
Christopher Faulet | 3d97c90 | 2015-12-09 14:59:38 +0100 | [diff] [blame] | 736 | if (word_match(ctx.line + ctx.val, ctx.vlen, "no-transform", 12)) |
| 737 | goto fail; |
| 738 | } |
| 739 | |
| 740 | comp_type = NULL; |
| 741 | |
| 742 | /* we don't want to compress multipart content-types, nor content-types that are |
| 743 | * not listed in the "compression type" directive if any. If no content-type was |
| 744 | * found but configuration requires one, we don't compress either. Backend has |
| 745 | * the priority. |
| 746 | */ |
| 747 | ctx.idx = 0; |
Olivier Houchard | 0b66284 | 2018-06-29 18:16:31 +0200 | [diff] [blame] | 748 | if (http_find_header2("Content-Type", 12, ci_head(c), &txn->hdr_idx, &ctx)) { |
Christopher Faulet | 3d97c90 | 2015-12-09 14:59:38 +0100 | [diff] [blame] | 749 | if (ctx.vlen >= 9 && strncasecmp("multipart", ctx.line+ctx.val, 9) == 0) |
| 750 | goto fail; |
| 751 | |
| 752 | if ((s->be->comp && (comp_type = s->be->comp->types)) || |
| 753 | (strm_fe(s)->comp && (comp_type = strm_fe(s)->comp->types))) { |
| 754 | for (; comp_type; comp_type = comp_type->next) { |
| 755 | if (ctx.vlen >= comp_type->name_len && |
| 756 | strncasecmp(ctx.line+ctx.val, comp_type->name, comp_type->name_len) == 0) |
| 757 | /* this Content-Type should be compressed */ |
| 758 | break; |
| 759 | } |
| 760 | /* this Content-Type should not be compressed */ |
| 761 | if (comp_type == NULL) |
| 762 | goto fail; |
| 763 | } |
| 764 | } |
| 765 | else { /* no content-type header */ |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 766 | if ((s->be->comp && s->be->comp->types) || |
| 767 | (strm_fe(s)->comp && strm_fe(s)->comp->types)) |
Christopher Faulet | 3d97c90 | 2015-12-09 14:59:38 +0100 | [diff] [blame] | 768 | goto fail; /* a content-type was required */ |
| 769 | } |
| 770 | |
| 771 | /* limit compression rate */ |
| 772 | if (global.comp_rate_lim > 0) |
| 773 | if (read_freq_ctr(&global.comp_bps_in) > global.comp_rate_lim) |
| 774 | goto fail; |
| 775 | |
| 776 | /* limit cpu usage */ |
| 777 | if (idle_pct < compress_min_idle) |
| 778 | goto fail; |
| 779 | |
| 780 | /* initialize compression */ |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 781 | if (st->comp_algo->init(&st->comp_ctx, global.tune.comp_maxlevel) < 0) |
Christopher Faulet | 3d97c90 | 2015-12-09 14:59:38 +0100 | [diff] [blame] | 782 | goto fail; |
| 783 | |
Christopher Faulet | 3d97c90 | 2015-12-09 14:59:38 +0100 | [diff] [blame] | 784 | /* remove Content-Length header */ |
| 785 | ctx.idx = 0; |
Olivier Houchard | 0b66284 | 2018-06-29 18:16:31 +0200 | [diff] [blame] | 786 | if ((msg->flags & HTTP_MSGF_CNT_LEN) && http_find_header2("Content-Length", 14, ci_head(c), &txn->hdr_idx, &ctx)) |
Christopher Faulet | 3d97c90 | 2015-12-09 14:59:38 +0100 | [diff] [blame] | 787 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
| 788 | |
| 789 | /* add Transfer-Encoding header */ |
| 790 | if (!(msg->flags & HTTP_MSGF_TE_CHNK)) |
| 791 | http_header_add_tail2(&txn->rsp, &txn->hdr_idx, "Transfer-Encoding: chunked", 26); |
| 792 | |
| 793 | /* |
| 794 | * Add Content-Encoding header when it's not identity encoding. |
| 795 | * RFC 2616 : Identity encoding: This content-coding is used only in the |
| 796 | * Accept-Encoding header, and SHOULD NOT be used in the Content-Encoding |
| 797 | * header. |
| 798 | */ |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 799 | if (st->comp_algo->cfg_name_len != 8 || memcmp(st->comp_algo->cfg_name, "identity", 8) != 0) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 800 | trash.data = 18; |
| 801 | memcpy(trash.area, "Content-Encoding: ", trash.data); |
| 802 | memcpy(trash.area + trash.data, st->comp_algo->ua_name, |
| 803 | st->comp_algo->ua_name_len); |
| 804 | trash.data += st->comp_algo->ua_name_len; |
| 805 | trash.area[trash.data] = '\0'; |
| 806 | http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.area, |
| 807 | trash.data); |
Christopher Faulet | 3d97c90 | 2015-12-09 14:59:38 +0100 | [diff] [blame] | 808 | } |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 809 | msg->flags |= HTTP_MSGF_COMPRESSING; |
Christopher Faulet | 3d97c90 | 2015-12-09 14:59:38 +0100 | [diff] [blame] | 810 | return 1; |
| 811 | |
| 812 | fail: |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 813 | st->comp_algo = NULL; |
Christopher Faulet | 3d97c90 | 2015-12-09 14:59:38 +0100 | [diff] [blame] | 814 | return 0; |
| 815 | } |
| 816 | |
Christopher Faulet | e6902cd | 2018-11-30 22:29:48 +0100 | [diff] [blame] | 817 | static int |
| 818 | htx_select_comp_reshdr(struct comp_state *st, struct stream *s, struct http_msg *msg) |
| 819 | { |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 820 | struct htx *htx = htxbuf(&msg->chn->buf); |
Christopher Faulet | e6902cd | 2018-11-30 22:29:48 +0100 | [diff] [blame] | 821 | struct http_txn *txn = s->txn; |
| 822 | struct http_hdr_ctx ctx; |
| 823 | struct comp_type *comp_type; |
| 824 | |
| 825 | /* no common compression algorithm was found in request header */ |
| 826 | if (st->comp_algo == NULL) |
| 827 | goto fail; |
| 828 | |
| 829 | /* HTTP < 1.1 should not be compressed */ |
| 830 | if (!(msg->flags & HTTP_MSGF_VER_11) || !(txn->req.flags & HTTP_MSGF_VER_11)) |
| 831 | goto fail; |
| 832 | |
| 833 | if (txn->meth == HTTP_METH_HEAD) |
| 834 | goto fail; |
| 835 | |
| 836 | /* compress 200,201,202,203 responses only */ |
| 837 | if ((txn->status != 200) && |
| 838 | (txn->status != 201) && |
| 839 | (txn->status != 202) && |
| 840 | (txn->status != 203)) |
| 841 | goto fail; |
| 842 | |
| 843 | if (msg->flags & HTTP_MSGF_BODYLESS) |
| 844 | goto fail; |
| 845 | |
| 846 | /* content is already compressed */ |
| 847 | ctx.blk = NULL; |
| 848 | if (http_find_header(htx, ist("Content-Encoding"), &ctx, 1)) |
| 849 | goto fail; |
| 850 | |
| 851 | /* no compression when Cache-Control: no-transform is present in the message */ |
| 852 | ctx.blk = NULL; |
| 853 | while (http_find_header(htx, ist("Cache-Control"), &ctx, 0)) { |
| 854 | if (word_match(ctx.value.ptr, ctx.value.len, "no-transform", 12)) |
| 855 | goto fail; |
| 856 | } |
| 857 | |
| 858 | comp_type = NULL; |
| 859 | |
| 860 | /* we don't want to compress multipart content-types, nor content-types that are |
| 861 | * not listed in the "compression type" directive if any. If no content-type was |
| 862 | * found but configuration requires one, we don't compress either. Backend has |
| 863 | * the priority. |
| 864 | */ |
| 865 | ctx.blk = NULL; |
| 866 | if (http_find_header(htx, ist("Content-Type"), &ctx, 1)) { |
| 867 | if (ctx.value.len >= 9 && strncasecmp("multipart", ctx.value.ptr, 9) == 0) |
| 868 | goto fail; |
| 869 | |
| 870 | if ((s->be->comp && (comp_type = s->be->comp->types)) || |
| 871 | (strm_fe(s)->comp && (comp_type = strm_fe(s)->comp->types))) { |
| 872 | for (; comp_type; comp_type = comp_type->next) { |
| 873 | if (ctx.value.len >= comp_type->name_len && |
| 874 | strncasecmp(ctx.value.ptr, comp_type->name, comp_type->name_len) == 0) |
| 875 | /* this Content-Type should be compressed */ |
| 876 | break; |
| 877 | } |
| 878 | /* this Content-Type should not be compressed */ |
| 879 | if (comp_type == NULL) |
| 880 | goto fail; |
| 881 | } |
| 882 | } |
| 883 | else { /* no content-type header */ |
| 884 | if ((s->be->comp && s->be->comp->types) || |
| 885 | (strm_fe(s)->comp && strm_fe(s)->comp->types)) |
| 886 | goto fail; /* a content-type was required */ |
| 887 | } |
| 888 | |
| 889 | /* limit compression rate */ |
| 890 | if (global.comp_rate_lim > 0) |
| 891 | if (read_freq_ctr(&global.comp_bps_in) > global.comp_rate_lim) |
| 892 | goto fail; |
| 893 | |
| 894 | /* limit cpu usage */ |
| 895 | if (idle_pct < compress_min_idle) |
| 896 | goto fail; |
| 897 | |
| 898 | /* initialize compression */ |
| 899 | if (st->comp_algo->init(&st->comp_ctx, global.tune.comp_maxlevel) < 0) |
| 900 | goto fail; |
| 901 | |
| 902 | /* |
| 903 | * Add Content-Encoding header when it's not identity encoding. |
| 904 | * RFC 2616 : Identity encoding: This content-coding is used only in the |
| 905 | * Accept-Encoding header, and SHOULD NOT be used in the Content-Encoding |
| 906 | * header. |
| 907 | */ |
| 908 | if (st->comp_algo->cfg_name_len != 8 || memcmp(st->comp_algo->cfg_name, "identity", 8) != 0) { |
| 909 | struct ist v = ist2(st->comp_algo->ua_name, st->comp_algo->ua_name_len); |
| 910 | |
| 911 | if (!http_add_header(htx, ist("Content-Encoding"), v)) |
| 912 | goto deinit_comp_ctx; |
| 913 | } |
| 914 | |
| 915 | /* remove Content-Length header */ |
| 916 | if (msg->flags & HTTP_MSGF_CNT_LEN) { |
| 917 | ctx.blk = NULL; |
| 918 | |
| 919 | while (http_find_header(htx, ist("Content-Length"), &ctx, 1)) |
| 920 | http_remove_header(htx, &ctx); |
| 921 | } |
| 922 | |
| 923 | /* add "Transfer-Encoding: chunked" header */ |
| 924 | if (!(msg->flags & HTTP_MSGF_TE_CHNK)) { |
| 925 | if (!http_add_header(htx, ist("Transfer-Encoding"), ist("chunked"))) |
| 926 | goto deinit_comp_ctx; |
| 927 | } |
| 928 | |
| 929 | msg->flags |= HTTP_MSGF_COMPRESSING; |
| 930 | return 1; |
| 931 | |
| 932 | deinit_comp_ctx: |
| 933 | st->comp_algo->end(&st->comp_ctx); |
| 934 | fail: |
| 935 | st->comp_algo = NULL; |
| 936 | return 0; |
| 937 | } |
| 938 | |
| 939 | static int |
| 940 | select_compression_response_header(struct comp_state *st, struct stream *s, struct http_msg *msg) |
| 941 | { |
| 942 | if (IS_HTX_STRM(s)) |
| 943 | return htx_select_comp_reshdr(st, s, msg); |
| 944 | else |
| 945 | return http_select_comp_reshdr(st, s, msg); |
| 946 | } |
Christopher Faulet | 3d97c90 | 2015-12-09 14:59:38 +0100 | [diff] [blame] | 947 | /***********************************************************************/ |
| 948 | /* emit the chunksize followed by a CRLF on the output and return the number of |
| 949 | * bytes written. It goes backwards and starts with the byte before <end>. It |
| 950 | * returns the number of bytes written which will not exceed 10 (8 digits, CR, |
| 951 | * and LF). The caller is responsible for ensuring there is enough room left in |
| 952 | * the output buffer for the string. |
| 953 | */ |
| 954 | static int |
| 955 | http_emit_chunk_size(char *end, unsigned int chksz) |
| 956 | { |
| 957 | char *beg = end; |
| 958 | |
| 959 | *--beg = '\n'; |
| 960 | *--beg = '\r'; |
| 961 | do { |
| 962 | *--beg = hextab[chksz & 0xF]; |
| 963 | } while (chksz >>= 4); |
| 964 | return end - beg; |
| 965 | } |
| 966 | |
| 967 | /* |
| 968 | * Init HTTP compression |
| 969 | */ |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 970 | static int |
Willy Tarreau | d54a8ce | 2018-06-29 18:42:02 +0200 | [diff] [blame] | 971 | http_compression_buffer_init(struct channel *inc, struct buffer *out, unsigned int *out_len) |
Christopher Faulet | 3d97c90 | 2015-12-09 14:59:38 +0100 | [diff] [blame] | 972 | { |
| 973 | /* output stream requires at least 10 bytes for the gzip header, plus |
| 974 | * at least 8 bytes for the gzip trailer (crc+len), plus a possible |
| 975 | * plus at most 5 bytes per 32kB block and 2 bytes to close the stream. |
| 976 | */ |
Olivier Houchard | 0b66284 | 2018-06-29 18:16:31 +0200 | [diff] [blame] | 977 | if (c_room(inc) < 20 + 5 * ((ci_data(inc) + 32767) >> 15)) |
Christopher Faulet | 3d97c90 | 2015-12-09 14:59:38 +0100 | [diff] [blame] | 978 | return -1; |
| 979 | |
| 980 | /* prepare an empty output buffer in which we reserve enough room for |
| 981 | * copying the output bytes from <in>, plus 10 extra bytes to write |
| 982 | * the chunk size. We don't copy the bytes yet so that if we have to |
| 983 | * cancel the operation later, it's cheap. |
| 984 | */ |
| 985 | b_reset(out); |
Willy Tarreau | d54a8ce | 2018-06-29 18:42:02 +0200 | [diff] [blame] | 986 | *out_len = co_data(inc); |
| 987 | out->head += *out_len + 10; |
Christopher Faulet | 3d97c90 | 2015-12-09 14:59:38 +0100 | [diff] [blame] | 988 | return 0; |
| 989 | } |
| 990 | |
Christopher Faulet | e6902cd | 2018-11-30 22:29:48 +0100 | [diff] [blame] | 991 | static int |
| 992 | htx_compression_buffer_init(struct htx *htx, struct buffer *out) |
| 993 | { |
| 994 | /* output stream requires at least 10 bytes for the gzip header, plus |
| 995 | * at least 8 bytes for the gzip trailer (crc+len), plus a possible |
| 996 | * plus at most 5 bytes per 32kB block and 2 bytes to close the stream. |
| 997 | */ |
| 998 | if (htx_free_space(htx) < 20 + 5 * ((htx->data + 32767) >> 15)) |
| 999 | return -1; |
| 1000 | b_reset(out); |
| 1001 | return 0; |
| 1002 | } |
| 1003 | |
Christopher Faulet | 3d97c90 | 2015-12-09 14:59:38 +0100 | [diff] [blame] | 1004 | /* |
| 1005 | * Add data to compress |
| 1006 | */ |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 1007 | static int |
| 1008 | http_compression_buffer_add_data(struct comp_state *st, struct buffer *in, |
Willy Tarreau | d54a8ce | 2018-06-29 18:42:02 +0200 | [diff] [blame] | 1009 | int in_out, struct buffer *out, int sz) |
Christopher Faulet | 3d97c90 | 2015-12-09 14:59:38 +0100 | [diff] [blame] | 1010 | { |
Christopher Faulet | 3d97c90 | 2015-12-09 14:59:38 +0100 | [diff] [blame] | 1011 | int consumed_data = 0; |
| 1012 | int data_process_len; |
| 1013 | int block1, block2; |
| 1014 | |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 1015 | if (!sz) |
Christopher Faulet | 3e7bc67 | 2015-12-07 13:39:08 +0100 | [diff] [blame] | 1016 | goto end; |
Christopher Faulet | 3d97c90 | 2015-12-09 14:59:38 +0100 | [diff] [blame] | 1017 | |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 1018 | /* select the smallest size between the announced chunk size, the input |
Christopher Faulet | 3d97c90 | 2015-12-09 14:59:38 +0100 | [diff] [blame] | 1019 | * data, and the available output buffer size. The compressors are |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 1020 | * assumed to be able to process all the bytes we pass to them at |
| 1021 | * once. */ |
Willy Tarreau | eac5259 | 2018-06-15 13:59:36 +0200 | [diff] [blame] | 1022 | data_process_len = MIN(b_room(out), sz); |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 1023 | |
Christopher Faulet | 3d97c90 | 2015-12-09 14:59:38 +0100 | [diff] [blame] | 1024 | block1 = data_process_len; |
Willy Tarreau | d54a8ce | 2018-06-29 18:42:02 +0200 | [diff] [blame] | 1025 | if (block1 > b_contig_data(in, in_out)) |
| 1026 | block1 = b_contig_data(in, in_out); |
Christopher Faulet | 3d97c90 | 2015-12-09 14:59:38 +0100 | [diff] [blame] | 1027 | block2 = data_process_len - block1; |
| 1028 | |
| 1029 | /* compressors return < 0 upon error or the amount of bytes read */ |
Willy Tarreau | d54a8ce | 2018-06-29 18:42:02 +0200 | [diff] [blame] | 1030 | consumed_data = st->comp_algo->add_data(st->comp_ctx, b_head(in) + in_out, block1, out); |
Christopher Faulet | 3e7bc67 | 2015-12-07 13:39:08 +0100 | [diff] [blame] | 1031 | if (consumed_data != block1 || !block2) |
| 1032 | goto end; |
Willy Tarreau | d54a8ce | 2018-06-29 18:42:02 +0200 | [diff] [blame] | 1033 | consumed_data = st->comp_algo->add_data(st->comp_ctx, b_peek(in, 0), block2, out); |
Christopher Faulet | 3e7bc67 | 2015-12-07 13:39:08 +0100 | [diff] [blame] | 1034 | if (consumed_data < 0) |
| 1035 | goto end; |
| 1036 | consumed_data += block1; |
| 1037 | |
| 1038 | end: |
Christopher Faulet | 3d97c90 | 2015-12-09 14:59:38 +0100 | [diff] [blame] | 1039 | return consumed_data; |
| 1040 | } |
| 1041 | |
Christopher Faulet | e6902cd | 2018-11-30 22:29:48 +0100 | [diff] [blame] | 1042 | static int |
| 1043 | htx_compression_buffer_add_data(struct comp_state *st, const char *data, size_t len, |
| 1044 | struct buffer *out) |
| 1045 | { |
| 1046 | return st->comp_algo->add_data(st->comp_ctx, data, len, out); |
| 1047 | } |
| 1048 | |
Christopher Faulet | 3d97c90 | 2015-12-09 14:59:38 +0100 | [diff] [blame] | 1049 | /* |
| 1050 | * Flush data in process, and write the header and footer of the chunk. Upon |
| 1051 | * success, in and out buffers are swapped to avoid a copy. |
| 1052 | */ |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 1053 | static int |
| 1054 | http_compression_buffer_end(struct comp_state *st, struct stream *s, |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1055 | struct channel *chn, struct buffer *out, |
Willy Tarreau | d54a8ce | 2018-06-29 18:42:02 +0200 | [diff] [blame] | 1056 | unsigned int *buf_out, int end) |
Christopher Faulet | 3d97c90 | 2015-12-09 14:59:38 +0100 | [diff] [blame] | 1057 | { |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1058 | struct buffer tmp_buf; |
Christopher Faulet | 3d97c90 | 2015-12-09 14:59:38 +0100 | [diff] [blame] | 1059 | char *tail; |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 1060 | int to_forward, left; |
Willy Tarreau | d54a8ce | 2018-06-29 18:42:02 +0200 | [diff] [blame] | 1061 | unsigned int tmp_out; |
Christopher Faulet | 3d97c90 | 2015-12-09 14:59:38 +0100 | [diff] [blame] | 1062 | |
| 1063 | #if defined(USE_SLZ) || defined(USE_ZLIB) |
| 1064 | int ret; |
| 1065 | |
| 1066 | /* flush data here */ |
Christopher Faulet | 3d97c90 | 2015-12-09 14:59:38 +0100 | [diff] [blame] | 1067 | if (end) |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1068 | ret = st->comp_algo->finish(st->comp_ctx, out); /* end of data */ |
Christopher Faulet | 3d97c90 | 2015-12-09 14:59:38 +0100 | [diff] [blame] | 1069 | else |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1070 | ret = st->comp_algo->flush(st->comp_ctx, out); /* end of buffer */ |
Christopher Faulet | 3d97c90 | 2015-12-09 14:59:38 +0100 | [diff] [blame] | 1071 | |
| 1072 | if (ret < 0) |
| 1073 | return -1; /* flush failed */ |
| 1074 | |
| 1075 | #endif /* USE_ZLIB */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1076 | if (b_data(out) == 0) { |
Christopher Faulet | 3d97c90 | 2015-12-09 14:59:38 +0100 | [diff] [blame] | 1077 | /* No data were appended, let's drop the output buffer and |
| 1078 | * keep the input buffer unchanged. |
| 1079 | */ |
| 1080 | return 0; |
| 1081 | } |
| 1082 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1083 | /* OK so at this stage, we have an output buffer <out> looking like this : |
Christopher Faulet | 3d97c90 | 2015-12-09 14:59:38 +0100 | [diff] [blame] | 1084 | * |
| 1085 | * <-- o --> <------ i -----> |
| 1086 | * +---------+---+------------+-----------+ |
| 1087 | * | out | c | comp_in | empty | |
| 1088 | * +---------+---+------------+-----------+ |
| 1089 | * data p size |
| 1090 | * |
Willy Tarreau | d54a8ce | 2018-06-29 18:42:02 +0200 | [diff] [blame] | 1091 | * <out> is the room reserved to copy the channel output. It starts at |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1092 | * out->area and has not yet been filled. <c> is the room reserved to |
Willy Tarreau | d54a8ce | 2018-06-29 18:42:02 +0200 | [diff] [blame] | 1093 | * write the chunk size (10 bytes). <comp_in> is the compressed |
| 1094 | * equivalent of the data part of ib->len. <empty> is the amount of |
| 1095 | * empty bytes at the end of the buffer, into which we may have to |
| 1096 | * copy the remaining bytes from ib->len after the data |
| 1097 | * (chunk size, trailers, ...). |
Christopher Faulet | 3d97c90 | 2015-12-09 14:59:38 +0100 | [diff] [blame] | 1098 | */ |
| 1099 | |
Joseph Herlant | 942eea3 | 2018-11-15 13:57:22 -0800 | [diff] [blame] | 1100 | /* Write real size at the beginning of the chunk, no need of wrapping. |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1101 | * We write the chunk using a dynamic length and adjust out->p and out->i |
Christopher Faulet | 3d97c90 | 2015-12-09 14:59:38 +0100 | [diff] [blame] | 1102 | * accordingly afterwards. That will move <out> away from <data>. |
| 1103 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1104 | left = http_emit_chunk_size(b_head(out), b_data(out)); |
| 1105 | b_add(out, left); |
| 1106 | out->head -= *buf_out + (left); |
| 1107 | /* Copy previous data from chn into out */ |
Willy Tarreau | d54a8ce | 2018-06-29 18:42:02 +0200 | [diff] [blame] | 1108 | if (co_data(chn) > 0) { |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1109 | left = b_contig_data(&chn->buf, 0); |
Willy Tarreau | d54a8ce | 2018-06-29 18:42:02 +0200 | [diff] [blame] | 1110 | if (left > *buf_out) |
| 1111 | left = *buf_out; |
Christopher Faulet | 3d97c90 | 2015-12-09 14:59:38 +0100 | [diff] [blame] | 1112 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1113 | memcpy(b_head(out), co_head(chn), left); |
| 1114 | b_add(out, left); |
Willy Tarreau | d54a8ce | 2018-06-29 18:42:02 +0200 | [diff] [blame] | 1115 | if (co_data(chn) - left) {/* second part of the buffer */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1116 | memcpy(b_head(out) + left, b_orig(&chn->buf), co_data(chn) - left); |
| 1117 | b_add(out, co_data(chn) - left); |
Willy Tarreau | d54a8ce | 2018-06-29 18:42:02 +0200 | [diff] [blame] | 1118 | } |
Christopher Faulet | 3d97c90 | 2015-12-09 14:59:38 +0100 | [diff] [blame] | 1119 | } |
| 1120 | |
| 1121 | /* chunked encoding requires CRLF after data */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1122 | tail = b_tail(out); |
Christopher Faulet | 3d97c90 | 2015-12-09 14:59:38 +0100 | [diff] [blame] | 1123 | *tail++ = '\r'; |
| 1124 | *tail++ = '\n'; |
| 1125 | |
Christopher Faulet | 2fb2880 | 2015-12-01 10:40:57 +0100 | [diff] [blame] | 1126 | /* At the end of data, we must write the empty chunk 0<CRLF>, |
| 1127 | * and terminate the trailers section with a last <CRLF>. If |
| 1128 | * we're forwarding a chunked-encoded response, we'll have a |
| 1129 | * trailers section after the empty chunk which needs to be |
| 1130 | * forwarded and which will provide the last CRLF. Otherwise |
| 1131 | * we write it ourselves. |
| 1132 | */ |
| 1133 | if (end) { |
| 1134 | struct http_msg *msg = &s->txn->rsp; |
| 1135 | |
| 1136 | memcpy(tail, "0\r\n", 3); |
| 1137 | tail += 3; |
Christopher Faulet | b77c5c2 | 2015-12-07 16:48:42 +0100 | [diff] [blame] | 1138 | if (!(msg->flags & HTTP_MSGF_TE_CHNK)) { |
Christopher Faulet | 2fb2880 | 2015-12-01 10:40:57 +0100 | [diff] [blame] | 1139 | memcpy(tail, "\r\n", 2); |
| 1140 | tail += 2; |
| 1141 | } |
| 1142 | } |
| 1143 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1144 | b_add(out, tail - b_tail(out)); |
| 1145 | to_forward = b_data(out) - *buf_out; |
Christopher Faulet | 3d97c90 | 2015-12-09 14:59:38 +0100 | [diff] [blame] | 1146 | |
| 1147 | /* update input rate */ |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 1148 | if (st->comp_ctx && st->comp_ctx->cur_lvl > 0) { |
Christopher Faulet | 2fb2880 | 2015-12-01 10:40:57 +0100 | [diff] [blame] | 1149 | update_freq_ctr(&global.comp_bps_in, st->consumed); |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 1150 | HA_ATOMIC_ADD(&strm_fe(s)->fe_counters.comp_in, st->consumed); |
| 1151 | HA_ATOMIC_ADD(&s->be->be_counters.comp_in, st->consumed); |
Christopher Faulet | 3d97c90 | 2015-12-09 14:59:38 +0100 | [diff] [blame] | 1152 | } else { |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 1153 | HA_ATOMIC_ADD(&strm_fe(s)->fe_counters.comp_byp, st->consumed); |
| 1154 | HA_ATOMIC_ADD(&s->be->be_counters.comp_byp, st->consumed); |
Christopher Faulet | 3d97c90 | 2015-12-09 14:59:38 +0100 | [diff] [blame] | 1155 | } |
| 1156 | |
| 1157 | /* copy the remaining data in the tmp buffer. */ |
Willy Tarreau | bcbd393 | 2018-06-06 07:13:22 +0200 | [diff] [blame] | 1158 | c_adv(chn, st->consumed); |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1159 | if (b_data(&chn->buf) - co_data(chn) > 0) { |
Willy Tarreau | 7194d3c | 2018-06-06 16:55:45 +0200 | [diff] [blame] | 1160 | left = ci_contig_data(chn); |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1161 | memcpy(b_tail(out), ci_head(chn), left); |
| 1162 | b_add(out, left); |
| 1163 | if (b_data(&chn->buf) - (co_data(chn) + left)) { |
| 1164 | memcpy(b_tail(out), b_orig(&chn->buf), b_data(&chn->buf) - left); |
| 1165 | b_add(out, b_data(&chn->buf) - left); |
Christopher Faulet | 3d97c90 | 2015-12-09 14:59:38 +0100 | [diff] [blame] | 1166 | } |
| 1167 | } |
Christopher Faulet | 3d97c90 | 2015-12-09 14:59:38 +0100 | [diff] [blame] | 1168 | /* swap the buffers */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1169 | tmp_buf = chn->buf; |
| 1170 | chn->buf = *out; |
| 1171 | *out = tmp_buf; |
| 1172 | |
Olivier Houchard | 08afac0 | 2018-06-22 19:26:39 +0200 | [diff] [blame] | 1173 | tmp_out = chn->output; |
| 1174 | chn->output = *buf_out; |
Willy Tarreau | d54a8ce | 2018-06-29 18:42:02 +0200 | [diff] [blame] | 1175 | *buf_out = tmp_out; |
| 1176 | |
Christopher Faulet | 3d97c90 | 2015-12-09 14:59:38 +0100 | [diff] [blame] | 1177 | |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 1178 | |
| 1179 | if (st->comp_ctx && st->comp_ctx->cur_lvl > 0) { |
Christopher Faulet | 3d97c90 | 2015-12-09 14:59:38 +0100 | [diff] [blame] | 1180 | update_freq_ctr(&global.comp_bps_out, to_forward); |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 1181 | HA_ATOMIC_ADD(&strm_fe(s)->fe_counters.comp_out, to_forward); |
| 1182 | HA_ATOMIC_ADD(&s->be->be_counters.comp_out, to_forward); |
Christopher Faulet | 3d97c90 | 2015-12-09 14:59:38 +0100 | [diff] [blame] | 1183 | } |
| 1184 | |
Christopher Faulet | 3d97c90 | 2015-12-09 14:59:38 +0100 | [diff] [blame] | 1185 | return to_forward; |
| 1186 | } |
| 1187 | |
Christopher Faulet | e6902cd | 2018-11-30 22:29:48 +0100 | [diff] [blame] | 1188 | static int |
| 1189 | htx_compression_buffer_end(struct comp_state *st, struct buffer *out, int end) |
| 1190 | { |
| 1191 | if (end) |
| 1192 | return st->comp_algo->finish(st->comp_ctx, out); |
| 1193 | else |
| 1194 | return st->comp_algo->flush(st->comp_ctx, out); |
| 1195 | } |
| 1196 | |
Christopher Faulet | 3d97c90 | 2015-12-09 14:59:38 +0100 | [diff] [blame] | 1197 | |
| 1198 | /***********************************************************************/ |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 1199 | struct flt_ops comp_ops = { |
Christopher Faulet | e6902cd | 2018-11-30 22:29:48 +0100 | [diff] [blame] | 1200 | .init = comp_flt_init, |
Christopher Faulet | 8ca3b4b | 2017-07-25 11:07:15 +0200 | [diff] [blame] | 1201 | .init_per_thread = comp_flt_init_per_thread, |
| 1202 | .deinit_per_thread = comp_flt_deinit_per_thread, |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 1203 | |
| 1204 | .channel_start_analyze = comp_start_analyze, |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 1205 | .channel_end_analyze = comp_end_analyze, |
Christopher Faulet | 3dc860d | 2017-09-15 11:39:36 +0200 | [diff] [blame] | 1206 | .channel_post_analyze = comp_http_post_analyze, |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 1207 | |
Christopher Faulet | 1339d74 | 2016-05-11 16:48:33 +0200 | [diff] [blame] | 1208 | .http_headers = comp_http_headers, |
Christopher Faulet | e6902cd | 2018-11-30 22:29:48 +0100 | [diff] [blame] | 1209 | .http_payload = comp_http_payload, |
| 1210 | .http_end = comp_http_end, |
| 1211 | |
Christopher Faulet | 309c641 | 2015-12-02 09:57:32 +0100 | [diff] [blame] | 1212 | .http_data = comp_http_data, |
| 1213 | .http_chunk_trailers = comp_http_chunk_trailers, |
| 1214 | .http_forward_data = comp_http_forward_data, |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 1215 | }; |
| 1216 | |
Christopher Faulet | 3d97c90 | 2015-12-09 14:59:38 +0100 | [diff] [blame] | 1217 | static int |
| 1218 | parse_compression_options(char **args, int section, struct proxy *proxy, |
| 1219 | struct proxy *defpx, const char *file, int line, |
| 1220 | char **err) |
| 1221 | { |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 1222 | struct comp *comp; |
Christopher Faulet | 3d97c90 | 2015-12-09 14:59:38 +0100 | [diff] [blame] | 1223 | |
| 1224 | if (proxy->comp == NULL) { |
Vincent Bernat | 02779b6 | 2016-04-03 13:48:43 +0200 | [diff] [blame] | 1225 | comp = calloc(1, sizeof(*comp)); |
Christopher Faulet | 3d97c90 | 2015-12-09 14:59:38 +0100 | [diff] [blame] | 1226 | proxy->comp = comp; |
| 1227 | } |
| 1228 | else |
| 1229 | comp = proxy->comp; |
| 1230 | |
| 1231 | if (!strcmp(args[1], "algo")) { |
| 1232 | struct comp_ctx *ctx; |
| 1233 | int cur_arg = 2; |
| 1234 | |
| 1235 | if (!*args[cur_arg]) { |
| 1236 | memprintf(err, "parsing [%s:%d] : '%s' expects <algorithm>\n", |
| 1237 | file, line, args[0]); |
| 1238 | return -1; |
| 1239 | } |
| 1240 | while (*(args[cur_arg])) { |
| 1241 | if (comp_append_algo(comp, args[cur_arg]) < 0) { |
| 1242 | memprintf(err, "'%s' : '%s' is not a supported algorithm.\n", |
| 1243 | args[0], args[cur_arg]); |
| 1244 | return -1; |
| 1245 | } |
| 1246 | if (proxy->comp->algos->init(&ctx, 9) == 0) |
| 1247 | proxy->comp->algos->end(&ctx); |
| 1248 | else { |
| 1249 | memprintf(err, "'%s' : Can't init '%s' algorithm.\n", |
| 1250 | args[0], args[cur_arg]); |
| 1251 | return -1; |
| 1252 | } |
| 1253 | cur_arg++; |
| 1254 | continue; |
| 1255 | } |
| 1256 | } |
| 1257 | else if (!strcmp(args[1], "offload")) |
| 1258 | comp->offload = 1; |
| 1259 | else if (!strcmp(args[1], "type")) { |
| 1260 | int cur_arg = 2; |
| 1261 | |
| 1262 | if (!*args[cur_arg]) { |
| 1263 | memprintf(err, "'%s' expects <type>\n", args[0]); |
| 1264 | return -1; |
| 1265 | } |
| 1266 | while (*(args[cur_arg])) { |
| 1267 | comp_append_type(comp, args[cur_arg]); |
| 1268 | cur_arg++; |
| 1269 | continue; |
| 1270 | } |
| 1271 | } |
| 1272 | else { |
| 1273 | memprintf(err, "'%s' expects 'algo', 'type' or 'offload'\n", |
| 1274 | args[0]); |
| 1275 | return -1; |
| 1276 | } |
| 1277 | |
| 1278 | return 0; |
| 1279 | } |
| 1280 | |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 1281 | static int |
| 1282 | parse_http_comp_flt(char **args, int *cur_arg, struct proxy *px, |
Thierry Fournier | 3610c39 | 2016-04-13 18:27:51 +0200 | [diff] [blame] | 1283 | struct flt_conf *fconf, char **err, void *private) |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 1284 | { |
Christopher Faulet | 443ea1a | 2016-02-04 13:40:26 +0100 | [diff] [blame] | 1285 | struct flt_conf *fc, *back; |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 1286 | |
Christopher Faulet | 443ea1a | 2016-02-04 13:40:26 +0100 | [diff] [blame] | 1287 | list_for_each_entry_safe(fc, back, &px->filter_configs, list) { |
| 1288 | if (fc->id == http_comp_flt_id) { |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 1289 | memprintf(err, "%s: Proxy supports only one compression filter\n", px->id); |
| 1290 | return -1; |
| 1291 | } |
| 1292 | } |
| 1293 | |
Christopher Faulet | 443ea1a | 2016-02-04 13:40:26 +0100 | [diff] [blame] | 1294 | fconf->id = http_comp_flt_id; |
| 1295 | fconf->conf = NULL; |
| 1296 | fconf->ops = &comp_ops; |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 1297 | (*cur_arg)++; |
| 1298 | |
| 1299 | return 0; |
| 1300 | } |
| 1301 | |
| 1302 | |
| 1303 | int |
Christopher Faulet | c9df7f7 | 2018-12-10 16:14:04 +0100 | [diff] [blame] | 1304 | check_implicit_http_comp_flt(struct proxy *proxy) |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 1305 | { |
Christopher Faulet | 443ea1a | 2016-02-04 13:40:26 +0100 | [diff] [blame] | 1306 | struct flt_conf *fconf; |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 1307 | int err = 0; |
| 1308 | |
| 1309 | if (proxy->comp == NULL) |
| 1310 | goto end; |
Christopher Faulet | 443ea1a | 2016-02-04 13:40:26 +0100 | [diff] [blame] | 1311 | if (!LIST_ISEMPTY(&proxy->filter_configs)) { |
| 1312 | list_for_each_entry(fconf, &proxy->filter_configs, list) { |
| 1313 | if (fconf->id == http_comp_flt_id) |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 1314 | goto end; |
| 1315 | } |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 1316 | ha_alert("config: %s '%s': require an explicit filter declaration to use HTTP compression\n", |
| 1317 | proxy_type_str(proxy), proxy->id); |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 1318 | err++; |
| 1319 | goto end; |
| 1320 | } |
| 1321 | |
Christopher Faulet | 443ea1a | 2016-02-04 13:40:26 +0100 | [diff] [blame] | 1322 | fconf = calloc(1, sizeof(*fconf)); |
| 1323 | if (!fconf) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 1324 | ha_alert("config: %s '%s': out of memory\n", |
| 1325 | proxy_type_str(proxy), proxy->id); |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 1326 | err++; |
| 1327 | goto end; |
| 1328 | } |
Christopher Faulet | 443ea1a | 2016-02-04 13:40:26 +0100 | [diff] [blame] | 1329 | fconf->id = http_comp_flt_id; |
| 1330 | fconf->conf = NULL; |
| 1331 | fconf->ops = &comp_ops; |
| 1332 | LIST_ADDQ(&proxy->filter_configs, &fconf->list); |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 1333 | |
| 1334 | end: |
| 1335 | return err; |
| 1336 | } |
| 1337 | |
| 1338 | /* |
| 1339 | * boolean, returns true if compression is used (either gzip or deflate) in the |
| 1340 | * response. |
| 1341 | */ |
Christopher Faulet | 3d97c90 | 2015-12-09 14:59:38 +0100 | [diff] [blame] | 1342 | static int |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 1343 | smp_fetch_res_comp(const struct arg *args, struct sample *smp, const char *kw, |
| 1344 | void *private) |
Christopher Faulet | 3d97c90 | 2015-12-09 14:59:38 +0100 | [diff] [blame] | 1345 | { |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 1346 | struct http_txn *txn = smp->strm ? smp->strm->txn : NULL; |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 1347 | |
Christopher Faulet | 3d97c90 | 2015-12-09 14:59:38 +0100 | [diff] [blame] | 1348 | smp->data.type = SMP_T_BOOL; |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 1349 | smp->data.u.sint = (txn && (txn->rsp.flags & HTTP_MSGF_COMPRESSING)); |
Christopher Faulet | 3d97c90 | 2015-12-09 14:59:38 +0100 | [diff] [blame] | 1350 | return 1; |
| 1351 | } |
| 1352 | |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 1353 | /* |
| 1354 | * string, returns algo |
| 1355 | */ |
Christopher Faulet | 3d97c90 | 2015-12-09 14:59:38 +0100 | [diff] [blame] | 1356 | static int |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 1357 | smp_fetch_res_comp_algo(const struct arg *args, struct sample *smp, |
| 1358 | const char *kw, void *private) |
Christopher Faulet | 3d97c90 | 2015-12-09 14:59:38 +0100 | [diff] [blame] | 1359 | { |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 1360 | struct http_txn *txn = smp->strm ? smp->strm->txn : NULL; |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 1361 | struct filter *filter; |
| 1362 | struct comp_state *st; |
| 1363 | |
Christopher Faulet | 03d8553 | 2017-09-15 10:14:43 +0200 | [diff] [blame] | 1364 | if (!txn || !(txn->rsp.flags & HTTP_MSGF_COMPRESSING)) |
Christopher Faulet | 3d97c90 | 2015-12-09 14:59:38 +0100 | [diff] [blame] | 1365 | return 0; |
| 1366 | |
Christopher Faulet | fcf035c | 2015-12-03 11:48:03 +0100 | [diff] [blame] | 1367 | list_for_each_entry(filter, &strm_flt(smp->strm)->filters, list) { |
Christopher Faulet | 443ea1a | 2016-02-04 13:40:26 +0100 | [diff] [blame] | 1368 | if (FLT_ID(filter) != http_comp_flt_id) |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 1369 | continue; |
| 1370 | |
| 1371 | if (!(st = filter->ctx)) |
| 1372 | break; |
| 1373 | |
| 1374 | smp->data.type = SMP_T_STR; |
| 1375 | smp->flags = SMP_F_CONST; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1376 | smp->data.u.str.area = st->comp_algo->cfg_name; |
| 1377 | smp->data.u.str.data = st->comp_algo->cfg_name_len; |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 1378 | return 1; |
| 1379 | } |
| 1380 | return 0; |
Christopher Faulet | 3d97c90 | 2015-12-09 14:59:38 +0100 | [diff] [blame] | 1381 | } |
| 1382 | |
| 1383 | /* Declare the config parser for "compression" keyword */ |
| 1384 | static struct cfg_kw_list cfg_kws = {ILH, { |
| 1385 | { CFG_LISTEN, "compression", parse_compression_options }, |
| 1386 | { 0, NULL, NULL }, |
| 1387 | } |
| 1388 | }; |
| 1389 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 1390 | INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws); |
| 1391 | |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 1392 | /* Declare the filter parser for "compression" keyword */ |
| 1393 | static struct flt_kw_list filter_kws = { "COMP", { }, { |
Thierry Fournier | 3610c39 | 2016-04-13 18:27:51 +0200 | [diff] [blame] | 1394 | { "compression", parse_http_comp_flt, NULL }, |
| 1395 | { NULL, NULL, NULL }, |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 1396 | } |
| 1397 | }; |
| 1398 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 1399 | INITCALL1(STG_REGISTER, flt_register_keywords, &filter_kws); |
| 1400 | |
Christopher Faulet | 3d97c90 | 2015-12-09 14:59:38 +0100 | [diff] [blame] | 1401 | /* Note: must not be declared <const> as its list will be overwritten */ |
| 1402 | static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, { |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 1403 | { "res.comp", smp_fetch_res_comp, 0, NULL, SMP_T_BOOL, SMP_USE_HRSHP }, |
| 1404 | { "res.comp_algo", smp_fetch_res_comp_algo, 0, NULL, SMP_T_STR, SMP_USE_HRSHP }, |
| 1405 | { /* END */ }, |
| 1406 | } |
| 1407 | }; |
Christopher Faulet | 3d97c90 | 2015-12-09 14:59:38 +0100 | [diff] [blame] | 1408 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 1409 | INITCALL1(STG_REGISTER, sample_register_fetches, &sample_fetch_keywords); |