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