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