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