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