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 | |
Willy Tarreau | 4c7e4b7 | 2020-05-27 12:58:42 +0200 | [diff] [blame] | 13 | #include <haproxy/api.h> |
Willy Tarreau | 6be7849 | 2020-06-05 00:00:29 +0200 | [diff] [blame] | 14 | #include <haproxy/cfgparse.h> |
Willy Tarreau | 0a3bd39 | 2020-06-04 08:52:38 +0200 | [diff] [blame] | 15 | #include <haproxy/compression.h> |
Willy Tarreau | 2741c8c | 2020-06-02 11:28:02 +0200 | [diff] [blame] | 16 | #include <haproxy/dynbuf.h> |
Willy Tarreau | c7babd8 | 2020-06-04 21:29:29 +0200 | [diff] [blame] | 17 | #include <haproxy/filters.h> |
Willy Tarreau | cd72d8c | 2020-06-02 19:11:26 +0200 | [diff] [blame] | 18 | #include <haproxy/http.h> |
Willy Tarreau | c2b1ff0 | 2020-06-04 21:21:03 +0200 | [diff] [blame] | 19 | #include <haproxy/http_ana-t.h> |
Willy Tarreau | 8773533 | 2020-06-04 09:08:41 +0200 | [diff] [blame] | 20 | #include <haproxy/http_htx.h> |
Willy Tarreau | 16f958c | 2020-06-03 08:44:35 +0200 | [diff] [blame] | 21 | #include <haproxy/htx.h> |
Willy Tarreau | 853b297 | 2020-05-27 18:01:47 +0200 | [diff] [blame] | 22 | #include <haproxy/list.h> |
Willy Tarreau | 202f93d | 2021-05-08 20:34:16 +0200 | [diff] [blame] | 23 | #include <haproxy/proxy.h> |
Willy Tarreau | e6ce10b | 2020-06-04 15:33:47 +0200 | [diff] [blame] | 24 | #include <haproxy/sample.h> |
Willy Tarreau | dfd3de8 | 2020-06-04 23:46:14 +0200 | [diff] [blame] | 25 | #include <haproxy/stream.h> |
Willy Tarreau | 48fbcae | 2020-06-03 18:09:46 +0200 | [diff] [blame] | 26 | #include <haproxy/tools.h> |
Christopher Faulet | 3d97c90 | 2015-12-09 14:59:38 +0100 | [diff] [blame] | 27 | |
Christopher Faulet | 12554d0 | 2021-06-09 17:12:44 +0200 | [diff] [blame] | 28 | #define COMP_STATE_PROCESSING 0x01 |
| 29 | |
Christopher Faulet | f4a4ef7 | 2018-12-07 17:39:53 +0100 | [diff] [blame] | 30 | const char *http_comp_flt_id = "compression filter"; |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 31 | |
| 32 | struct flt_ops comp_ops; |
| 33 | |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 34 | struct comp_state { |
Olivier Houchard | dfc11da | 2023-04-05 16:25:57 +0200 | [diff] [blame] | 35 | /* |
| 36 | * For both comp_ctx and comp_algo, COMP_DIR_REQ is the index |
| 37 | * for requests, and COMP_DIR_RES for responses |
| 38 | */ |
| 39 | struct comp_ctx *comp_ctx[2]; /* compression context */ |
| 40 | struct comp_algo *comp_algo[2]; /* compression algorithm if not NULL */ |
Christopher Faulet | 12554d0 | 2021-06-09 17:12:44 +0200 | [diff] [blame] | 41 | unsigned int flags; /* COMP_STATE_* */ |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 42 | }; |
| 43 | |
Willy Tarreau | 8ceae72 | 2018-11-26 11:58:30 +0100 | [diff] [blame] | 44 | /* Pools used to allocate comp_state structs */ |
| 45 | DECLARE_STATIC_POOL(pool_head_comp_state, "comp_state", sizeof(struct comp_state)); |
| 46 | |
| 47 | static THREAD_LOCAL struct buffer tmpbuf; |
| 48 | static THREAD_LOCAL struct buffer zbuf; |
Willy Tarreau | 8ceae72 | 2018-11-26 11:58:30 +0100 | [diff] [blame] | 49 | |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 50 | static int select_compression_request_header(struct comp_state *st, |
| 51 | struct stream *s, |
| 52 | struct http_msg *msg); |
| 53 | static int select_compression_response_header(struct comp_state *st, |
| 54 | struct stream *s, |
| 55 | struct http_msg *msg); |
Olivier Houchard | dfc11da | 2023-04-05 16:25:57 +0200 | [diff] [blame] | 56 | static int set_compression_header(struct comp_state *st, |
| 57 | struct stream *s, |
| 58 | struct http_msg *msg); |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 59 | |
Christopher Faulet | e6902cd | 2018-11-30 22:29:48 +0100 | [diff] [blame] | 60 | static int htx_compression_buffer_init(struct htx *htx, struct buffer *out); |
| 61 | static int htx_compression_buffer_add_data(struct comp_state *st, const char *data, size_t len, |
Olivier Houchard | dfc11da | 2023-04-05 16:25:57 +0200 | [diff] [blame] | 62 | struct buffer *out, int dir); |
| 63 | static int htx_compression_buffer_end(struct comp_state *st, struct buffer *out, int end, int dir); |
Christopher Faulet | e6902cd | 2018-11-30 22:29:48 +0100 | [diff] [blame] | 64 | |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 65 | /***********************************************************************/ |
| 66 | static int |
Christopher Faulet | e6902cd | 2018-11-30 22:29:48 +0100 | [diff] [blame] | 67 | comp_flt_init(struct proxy *px, struct flt_conf *fconf) |
| 68 | { |
Christopher Faulet | 6e54095 | 2018-12-03 22:43:41 +0100 | [diff] [blame] | 69 | fconf->flags |= FLT_CFG_FL_HTX; |
Christopher Faulet | e6902cd | 2018-11-30 22:29:48 +0100 | [diff] [blame] | 70 | return 0; |
| 71 | } |
| 72 | |
| 73 | static int |
Christopher Faulet | 8ca3b4b | 2017-07-25 11:07:15 +0200 | [diff] [blame] | 74 | comp_flt_init_per_thread(struct proxy *px, struct flt_conf *fconf) |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 75 | { |
Willy Tarreau | 862ad82 | 2021-03-22 16:16:22 +0100 | [diff] [blame] | 76 | if (b_alloc(&tmpbuf) == NULL) |
Christopher Faulet | b77c5c2 | 2015-12-07 16:48:42 +0100 | [diff] [blame] | 77 | return -1; |
Willy Tarreau | 862ad82 | 2021-03-22 16:16:22 +0100 | [diff] [blame] | 78 | if (b_alloc(&zbuf) == NULL) |
Christopher Faulet | b77c5c2 | 2015-12-07 16:48:42 +0100 | [diff] [blame] | 79 | return -1; |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 80 | return 0; |
| 81 | } |
| 82 | |
| 83 | static void |
Christopher Faulet | 8ca3b4b | 2017-07-25 11:07:15 +0200 | [diff] [blame] | 84 | comp_flt_deinit_per_thread(struct proxy *px, struct flt_conf *fconf) |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 85 | { |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 86 | if (tmpbuf.size) |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 87 | b_free(&tmpbuf); |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 88 | if (zbuf.size) |
Christopher Faulet | b77c5c2 | 2015-12-07 16:48:42 +0100 | [diff] [blame] | 89 | b_free(&zbuf); |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | static int |
Christopher Faulet | 5e89651 | 2020-03-06 14:59:05 +0100 | [diff] [blame] | 93 | comp_strm_init(struct stream *s, struct filter *filter) |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 94 | { |
Christopher Faulet | 5e89651 | 2020-03-06 14:59:05 +0100 | [diff] [blame] | 95 | struct comp_state *st; |
Christopher Faulet | 8ca3b4b | 2017-07-25 11:07:15 +0200 | [diff] [blame] | 96 | |
Willy Tarreau | 5bfeb21 | 2021-03-22 15:08:17 +0100 | [diff] [blame] | 97 | st = pool_alloc(pool_head_comp_state); |
Christopher Faulet | 5e89651 | 2020-03-06 14:59:05 +0100 | [diff] [blame] | 98 | if (st == NULL) |
| 99 | return -1; |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 100 | |
Olivier Houchard | dfc11da | 2023-04-05 16:25:57 +0200 | [diff] [blame] | 101 | st->comp_algo[COMP_DIR_REQ] = NULL; |
| 102 | st->comp_algo[COMP_DIR_RES] = NULL; |
| 103 | st->comp_ctx[COMP_DIR_REQ] = NULL; |
| 104 | st->comp_ctx[COMP_DIR_RES] = NULL; |
Christopher Faulet | 12554d0 | 2021-06-09 17:12:44 +0200 | [diff] [blame] | 105 | st->flags = 0; |
Christopher Faulet | 5e89651 | 2020-03-06 14:59:05 +0100 | [diff] [blame] | 106 | filter->ctx = st; |
Christopher Faulet | 3dc860d | 2017-09-15 11:39:36 +0200 | [diff] [blame] | 107 | |
Christopher Faulet | 5e89651 | 2020-03-06 14:59:05 +0100 | [diff] [blame] | 108 | /* Register post-analyzer on AN_RES_WAIT_HTTP because we need to |
| 109 | * analyze response headers before http-response rules execution |
| 110 | * to be sure we can use res.comp and res.comp_algo sample |
| 111 | * fetches */ |
| 112 | filter->post_analyzers |= AN_RES_WAIT_HTTP; |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 113 | return 1; |
| 114 | } |
| 115 | |
Christopher Faulet | 5e89651 | 2020-03-06 14:59:05 +0100 | [diff] [blame] | 116 | static void |
| 117 | comp_strm_deinit(struct stream *s, struct filter *filter) |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 118 | { |
| 119 | struct comp_state *st = filter->ctx; |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 120 | |
Christopher Faulet | d60b3cf | 2017-06-26 11:47:13 +0200 | [diff] [blame] | 121 | if (!st) |
Christopher Faulet | 5e89651 | 2020-03-06 14:59:05 +0100 | [diff] [blame] | 122 | return; |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 123 | |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 124 | /* release any possible compression context */ |
Olivier Houchard | dfc11da | 2023-04-05 16:25:57 +0200 | [diff] [blame] | 125 | if (st->comp_algo[COMP_DIR_REQ]) |
| 126 | st->comp_algo[COMP_DIR_REQ]->end(&st->comp_ctx[COMP_DIR_REQ]); |
| 127 | if (st->comp_algo[COMP_DIR_RES]) |
| 128 | st->comp_algo[COMP_DIR_RES]->end(&st->comp_ctx[COMP_DIR_RES]); |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 129 | pool_free(pool_head_comp_state, st); |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 130 | filter->ctx = NULL; |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 131 | } |
| 132 | |
Olivier Houchard | ead43fe | 2023-04-06 00:33:48 +0200 | [diff] [blame^] | 133 | static void |
| 134 | comp_prepare_compress_request(struct comp_state *st, struct stream *s, struct http_msg *msg) |
| 135 | { |
| 136 | struct htx *htx = htxbuf(&msg->chn->buf); |
| 137 | struct http_txn *txn = s->txn; |
| 138 | struct http_hdr_ctx ctx; |
| 139 | struct comp_type *comp_type; |
| 140 | |
| 141 | ctx.blk = NULL; |
| 142 | /* Already compressed, don't bother */ |
| 143 | if (http_find_header(htx, ist("Content-Encoding"), &ctx, 1)) |
| 144 | return; |
| 145 | /* HTTP < 1.1 should not be compressed */ |
| 146 | if (!(msg->flags & HTTP_MSGF_VER_11) || !(txn->req.flags & HTTP_MSGF_VER_11)) |
| 147 | return; |
| 148 | comp_type = NULL; |
| 149 | |
| 150 | /* |
| 151 | * We don't want to compress content-types not listed in the "compression type" directive if any. If no content-type was found but configuration |
| 152 | * requires one, we don't compress either. Backend has the priority. |
| 153 | */ |
| 154 | ctx.blk = NULL; |
| 155 | if (http_find_header(htx, ist("Content-Type"), &ctx, 1)) { |
| 156 | if ((s->be->comp && (comp_type = s->be->comp->types_req)) || |
| 157 | (strm_fe(s)->comp && (comp_type = strm_fe(s)->comp->types_req))) { |
| 158 | for (; comp_type; comp_type = comp_type->next) { |
| 159 | if (ctx.value.len >= comp_type->name_len && |
| 160 | strncasecmp(ctx.value.ptr, comp_type->name, comp_type->name_len) == 0) |
| 161 | /* this Content-Type should be compressed */ |
| 162 | break; |
| 163 | } |
| 164 | /* this Content-Type should not be compressed */ |
| 165 | if (comp_type == NULL) |
| 166 | goto fail; |
| 167 | } |
| 168 | } |
| 169 | else { /* no content-type header */ |
| 170 | if ((s->be->comp && s->be->comp->types_req) || |
| 171 | (strm_fe(s)->comp && strm_fe(s)->comp->types_req)) |
| 172 | goto fail; /* a content-type was required */ |
| 173 | } |
| 174 | |
| 175 | /* limit compression rate */ |
| 176 | if (global.comp_rate_lim > 0) |
| 177 | if (read_freq_ctr(&global.comp_bps_in) > global.comp_rate_lim) |
| 178 | goto fail; |
| 179 | |
| 180 | /* limit cpu usage */ |
| 181 | if (th_ctx->idle_pct < compress_min_idle) |
| 182 | goto fail; |
| 183 | |
| 184 | if (txn->meth == HTTP_METH_HEAD) |
| 185 | return; |
| 186 | if (s->be->comp->algo_req != NULL) |
| 187 | st->comp_algo[COMP_DIR_REQ] = s->be->comp->algo_req; |
| 188 | else if (strm_fe(s)->comp->algo_req != NULL) |
| 189 | st->comp_algo[COMP_DIR_REQ] = strm_fe(s)->comp->algo_req; |
| 190 | |
| 191 | |
| 192 | /* limit compression rate */ |
| 193 | if (global.comp_rate_lim > 0) |
| 194 | if (read_freq_ctr(&global.comp_bps_in) > global.comp_rate_lim) |
| 195 | goto fail; |
| 196 | |
| 197 | /* limit cpu usage */ |
| 198 | if (th_ctx->idle_pct < compress_min_idle) |
| 199 | goto fail; |
| 200 | |
| 201 | /* initialize compression */ |
| 202 | if (st->comp_algo[COMP_DIR_REQ]->init(&st->comp_ctx[COMP_DIR_REQ], global.tune.comp_maxlevel) < 0) |
| 203 | goto fail; |
| 204 | |
| 205 | return; |
| 206 | fail: |
| 207 | st->comp_algo[COMP_DIR_REQ] = NULL; |
| 208 | } |
| 209 | |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 210 | static int |
Christopher Faulet | 1339d74 | 2016-05-11 16:48:33 +0200 | [diff] [blame] | 211 | comp_http_headers(struct stream *s, struct filter *filter, struct http_msg *msg) |
| 212 | { |
| 213 | struct comp_state *st = filter->ctx; |
Olivier Houchard | ead43fe | 2023-04-06 00:33:48 +0200 | [diff] [blame^] | 214 | int comp_flags = 0; |
Christopher Faulet | 1339d74 | 2016-05-11 16:48:33 +0200 | [diff] [blame] | 215 | |
| 216 | if (!strm_fe(s)->comp && !s->be->comp) |
| 217 | goto end; |
Olivier Houchard | ead43fe | 2023-04-06 00:33:48 +0200 | [diff] [blame^] | 218 | if (strm_fe(s)->comp) |
| 219 | comp_flags |= strm_fe(s)->comp->flags; |
| 220 | if (s->be->comp) |
| 221 | comp_flags |= s->be->comp->flags; |
Christopher Faulet | 1339d74 | 2016-05-11 16:48:33 +0200 | [diff] [blame] | 222 | |
Olivier Houchard | ead43fe | 2023-04-06 00:33:48 +0200 | [diff] [blame^] | 223 | if (!(msg->chn->flags & CF_ISRESP)) { |
| 224 | if (comp_flags & COMP_FL_DIR_REQ) { |
| 225 | comp_prepare_compress_request(st, s, msg); |
| 226 | if (st->comp_algo[COMP_DIR_REQ]) { |
| 227 | if (!set_compression_header(st, s, msg)) |
| 228 | goto end; |
| 229 | register_data_filter(s, msg->chn, filter); |
| 230 | st->flags |= COMP_STATE_PROCESSING; |
| 231 | } |
| 232 | } |
| 233 | if (comp_flags & COMP_FL_DIR_RES) |
| 234 | select_compression_request_header(st, s, msg); |
| 235 | } else if (comp_flags & COMP_FL_DIR_RES) { |
Christopher Faulet | 3dc860d | 2017-09-15 11:39:36 +0200 | [diff] [blame] | 236 | /* Response headers have already been checked in |
| 237 | * comp_http_post_analyze callback. */ |
Olivier Houchard | dfc11da | 2023-04-05 16:25:57 +0200 | [diff] [blame] | 238 | if (st->comp_algo[COMP_DIR_RES]) { |
| 239 | if (!set_compression_header(st, s, msg)) |
Christopher Faulet | 27d93c3 | 2018-12-15 22:32:02 +0100 | [diff] [blame] | 240 | goto end; |
Christopher Faulet | 1339d74 | 2016-05-11 16:48:33 +0200 | [diff] [blame] | 241 | register_data_filter(s, msg->chn, filter); |
Christopher Faulet | 12554d0 | 2021-06-09 17:12:44 +0200 | [diff] [blame] | 242 | st->flags |= COMP_STATE_PROCESSING; |
Christopher Faulet | 1339d74 | 2016-05-11 16:48:33 +0200 | [diff] [blame] | 243 | } |
| 244 | } |
| 245 | |
| 246 | end: |
| 247 | return 1; |
| 248 | } |
| 249 | |
| 250 | static int |
Christopher Faulet | 3dc860d | 2017-09-15 11:39:36 +0200 | [diff] [blame] | 251 | comp_http_post_analyze(struct stream *s, struct filter *filter, |
| 252 | struct channel *chn, unsigned an_bit) |
| 253 | { |
| 254 | struct http_txn *txn = s->txn; |
| 255 | struct http_msg *msg = &txn->rsp; |
| 256 | struct comp_state *st = filter->ctx; |
| 257 | |
| 258 | if (an_bit != AN_RES_WAIT_HTTP) |
| 259 | goto end; |
| 260 | |
| 261 | if (!strm_fe(s)->comp && !s->be->comp) |
| 262 | goto end; |
| 263 | |
| 264 | select_compression_response_header(st, s, msg); |
| 265 | |
| 266 | end: |
| 267 | return 1; |
| 268 | } |
| 269 | |
| 270 | static int |
Christopher Faulet | e6902cd | 2018-11-30 22:29:48 +0100 | [diff] [blame] | 271 | comp_http_payload(struct stream *s, struct filter *filter, struct http_msg *msg, |
| 272 | unsigned int offset, unsigned int len) |
| 273 | { |
| 274 | struct comp_state *st = filter->ctx; |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 275 | struct htx *htx = htxbuf(&msg->chn->buf); |
Christopher Faulet | e6a62bf | 2020-03-02 16:20:05 +0100 | [diff] [blame] | 276 | struct htx_ret htxret = htx_find_offset(htx, offset); |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 277 | struct htx_blk *blk, *next; |
| 278 | int ret, consumed = 0, to_forward = 0, last = 0; |
Olivier Houchard | dfc11da | 2023-04-05 16:25:57 +0200 | [diff] [blame] | 279 | int dir; |
| 280 | |
| 281 | if (msg->chn->flags & CF_ISRESP) |
| 282 | dir = COMP_DIR_RES; |
| 283 | else |
| 284 | dir = COMP_DIR_REQ; |
Christopher Faulet | e6902cd | 2018-11-30 22:29:48 +0100 | [diff] [blame] | 285 | |
Christopher Faulet | e6a62bf | 2020-03-02 16:20:05 +0100 | [diff] [blame] | 286 | blk = htxret.blk; |
| 287 | offset = htxret.ret; |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 288 | for (next = NULL; blk && len; blk = next) { |
Christopher Faulet | e6902cd | 2018-11-30 22:29:48 +0100 | [diff] [blame] | 289 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 290 | uint32_t sz = htx_get_blksz(blk); |
| 291 | struct ist v; |
| 292 | |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 293 | next = htx_get_next_blk(htx, blk); |
| 294 | while (next && htx_get_blk_type(next) == HTX_BLK_UNUSED) |
Christopher Faulet | 86ca0e5 | 2021-06-09 16:59:02 +0200 | [diff] [blame] | 295 | next = htx_get_next_blk(htx, next); |
Christopher Faulet | e6902cd | 2018-11-30 22:29:48 +0100 | [diff] [blame] | 296 | |
Christopher Faulet | 12554d0 | 2021-06-09 17:12:44 +0200 | [diff] [blame] | 297 | if (!(st->flags & COMP_STATE_PROCESSING)) |
| 298 | goto consume; |
| 299 | |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 300 | if (htx_compression_buffer_init(htx, &trash) < 0) { |
| 301 | msg->chn->flags |= CF_WAKE_WRITE; |
| 302 | goto end; |
| 303 | } |
| 304 | |
| 305 | switch (type) { |
Christopher Faulet | e6902cd | 2018-11-30 22:29:48 +0100 | [diff] [blame] | 306 | case HTX_BLK_DATA: |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 307 | /* it is the last data block */ |
| 308 | last = ((!next && (htx->flags & HTX_FL_EOM)) || (next && htx_get_blk_type(next) != HTX_BLK_DATA)); |
Christopher Faulet | e6902cd | 2018-11-30 22:29:48 +0100 | [diff] [blame] | 309 | v = htx_get_blk_value(htx, blk); |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 310 | v = istadv(v, offset); |
| 311 | if (v.len > len) { |
| 312 | last = 0; |
Christopher Faulet | e6902cd | 2018-11-30 22:29:48 +0100 | [diff] [blame] | 313 | v.len = len; |
Christopher Faulet | e6902cd | 2018-11-30 22:29:48 +0100 | [diff] [blame] | 314 | } |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 315 | |
Olivier Houchard | dfc11da | 2023-04-05 16:25:57 +0200 | [diff] [blame] | 316 | ret = htx_compression_buffer_add_data(st, v.ptr, v.len, &trash, dir); |
| 317 | if (ret < 0 || htx_compression_buffer_end(st, &trash, last, dir) < 0) |
Christopher Faulet | e6902cd | 2018-11-30 22:29:48 +0100 | [diff] [blame] | 318 | goto error; |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 319 | BUG_ON(v.len != ret); |
| 320 | |
| 321 | if (ret == sz && !b_data(&trash)) |
| 322 | next = htx_remove_blk(htx, blk); |
Christopher Faulet | 402740c | 2021-06-09 17:04:37 +0200 | [diff] [blame] | 323 | else { |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 324 | blk = htx_replace_blk_value(htx, blk, v, ist2(b_head(&trash), b_data(&trash))); |
Christopher Faulet | 402740c | 2021-06-09 17:04:37 +0200 | [diff] [blame] | 325 | next = htx_get_next_blk(htx, blk); |
| 326 | } |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 327 | |
Christopher Faulet | e6902cd | 2018-11-30 22:29:48 +0100 | [diff] [blame] | 328 | len -= ret; |
| 329 | consumed += ret; |
| 330 | to_forward += b_data(&trash); |
Christopher Faulet | 12554d0 | 2021-06-09 17:12:44 +0200 | [diff] [blame] | 331 | if (last) |
| 332 | st->flags &= ~COMP_STATE_PROCESSING; |
Christopher Faulet | e6902cd | 2018-11-30 22:29:48 +0100 | [diff] [blame] | 333 | break; |
| 334 | |
Christopher Faulet | e6902cd | 2018-11-30 22:29:48 +0100 | [diff] [blame] | 335 | case HTX_BLK_TLR: |
Christopher Faulet | 2d7c539 | 2019-06-03 10:41:26 +0200 | [diff] [blame] | 336 | case HTX_BLK_EOT: |
Olivier Houchard | dfc11da | 2023-04-05 16:25:57 +0200 | [diff] [blame] | 337 | if (htx_compression_buffer_end(st, &trash, 1, dir) < 0) |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 338 | goto error; |
| 339 | if (b_data(&trash)) { |
| 340 | struct htx_blk *last = htx_add_last_data(htx, ist2(b_head(&trash), b_data(&trash))); |
| 341 | if (!last) |
Christopher Faulet | e6902cd | 2018-11-30 22:29:48 +0100 | [diff] [blame] | 342 | goto error; |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 343 | blk = htx_get_next_blk(htx, last); |
| 344 | if (!blk) |
| 345 | goto error; |
Christopher Faulet | 402740c | 2021-06-09 17:04:37 +0200 | [diff] [blame] | 346 | next = htx_get_next_blk(htx, blk); |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 347 | to_forward += b_data(&trash); |
Christopher Faulet | e6902cd | 2018-11-30 22:29:48 +0100 | [diff] [blame] | 348 | } |
Christopher Faulet | 12554d0 | 2021-06-09 17:12:44 +0200 | [diff] [blame] | 349 | st->flags &= ~COMP_STATE_PROCESSING; |
Willy Tarreau | 91d398c | 2022-11-14 07:36:05 +0100 | [diff] [blame] | 350 | __fallthrough; |
Christopher Faulet | e6902cd | 2018-11-30 22:29:48 +0100 | [diff] [blame] | 351 | |
| 352 | default: |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 353 | consume: |
Christopher Faulet | e6902cd | 2018-11-30 22:29:48 +0100 | [diff] [blame] | 354 | sz -= offset; |
| 355 | if (sz > len) |
| 356 | sz = len; |
| 357 | consumed += sz; |
| 358 | to_forward += sz; |
| 359 | len -= sz; |
| 360 | break; |
| 361 | } |
| 362 | |
| 363 | offset = 0; |
Christopher Faulet | e6902cd | 2018-11-30 22:29:48 +0100 | [diff] [blame] | 364 | } |
| 365 | |
| 366 | end: |
| 367 | if (to_forward != consumed) |
| 368 | flt_update_offsets(filter, msg->chn, to_forward - consumed); |
| 369 | |
Olivier Houchard | dfc11da | 2023-04-05 16:25:57 +0200 | [diff] [blame] | 370 | if (st->comp_ctx[dir] && st->comp_ctx[dir]->cur_lvl > 0) { |
Willy Tarreau | ef6fd85 | 2019-02-04 11:48:03 +0100 | [diff] [blame] | 371 | update_freq_ctr(&global.comp_bps_in, consumed); |
Olivier Houchard | dea25f5 | 2023-04-06 00:33:01 +0200 | [diff] [blame] | 372 | _HA_ATOMIC_ADD(&strm_fe(s)->fe_counters.comp_in[dir], consumed); |
| 373 | _HA_ATOMIC_ADD(&s->be->be_counters.comp_in[dir], consumed); |
Christopher Faulet | e6902cd | 2018-11-30 22:29:48 +0100 | [diff] [blame] | 374 | update_freq_ctr(&global.comp_bps_out, to_forward); |
Olivier Houchard | dea25f5 | 2023-04-06 00:33:01 +0200 | [diff] [blame] | 375 | _HA_ATOMIC_ADD(&strm_fe(s)->fe_counters.comp_out[dir], to_forward); |
| 376 | _HA_ATOMIC_ADD(&s->be->be_counters.comp_out[dir], to_forward); |
Willy Tarreau | ef6fd85 | 2019-02-04 11:48:03 +0100 | [diff] [blame] | 377 | } else { |
Olivier Houchard | dea25f5 | 2023-04-06 00:33:01 +0200 | [diff] [blame] | 378 | _HA_ATOMIC_ADD(&strm_fe(s)->fe_counters.comp_byp[dir], consumed); |
| 379 | _HA_ATOMIC_ADD(&s->be->be_counters.comp_byp[dir], consumed); |
Christopher Faulet | e6902cd | 2018-11-30 22:29:48 +0100 | [diff] [blame] | 380 | } |
| 381 | return to_forward; |
| 382 | |
| 383 | error: |
| 384 | return -1; |
| 385 | } |
| 386 | |
Christopher Faulet | 2fb2880 | 2015-12-01 10:40:57 +0100 | [diff] [blame] | 387 | |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 388 | static int |
Christopher Faulet | d60b3cf | 2017-06-26 11:47:13 +0200 | [diff] [blame] | 389 | comp_http_end(struct stream *s, struct filter *filter, |
| 390 | struct http_msg *msg) |
| 391 | { |
| 392 | struct comp_state *st = filter->ctx; |
| 393 | |
Olivier Houchard | dfc11da | 2023-04-05 16:25:57 +0200 | [diff] [blame] | 394 | if (!(msg->chn->flags & CF_ISRESP) || !st || !st->comp_algo[COMP_DIR_RES]) |
Christopher Faulet | d60b3cf | 2017-06-26 11:47:13 +0200 | [diff] [blame] | 395 | goto end; |
| 396 | |
| 397 | if (strm_fe(s)->mode == PR_MODE_HTTP) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 398 | _HA_ATOMIC_INC(&strm_fe(s)->fe_counters.p.http.comp_rsp); |
Christopher Faulet | d60b3cf | 2017-06-26 11:47:13 +0200 | [diff] [blame] | 399 | if ((s->flags & SF_BE_ASSIGNED) && (s->be->mode == PR_MODE_HTTP)) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 400 | _HA_ATOMIC_INC(&s->be->be_counters.p.http.comp_rsp); |
Christopher Faulet | d60b3cf | 2017-06-26 11:47:13 +0200 | [diff] [blame] | 401 | end: |
| 402 | return 1; |
| 403 | } |
Christopher Faulet | 27d93c3 | 2018-12-15 22:32:02 +0100 | [diff] [blame] | 404 | |
Christopher Faulet | 89f2b16 | 2019-07-15 21:16:04 +0200 | [diff] [blame] | 405 | /***********************************************************************/ |
Christopher Faulet | 27d93c3 | 2018-12-15 22:32:02 +0100 | [diff] [blame] | 406 | static int |
Olivier Houchard | dfc11da | 2023-04-05 16:25:57 +0200 | [diff] [blame] | 407 | set_compression_header(struct comp_state *st, struct stream *s, struct http_msg *msg) |
Christopher Faulet | 27d93c3 | 2018-12-15 22:32:02 +0100 | [diff] [blame] | 408 | { |
| 409 | struct htx *htx = htxbuf(&msg->chn->buf); |
Christopher Faulet | 39e436e | 2022-04-15 15:32:03 +0200 | [diff] [blame] | 410 | struct htx_sl *sl; |
Tim Duesterhus | b229f01 | 2019-01-29 16:38:56 +0100 | [diff] [blame] | 411 | struct http_hdr_ctx ctx; |
Olivier Houchard | dfc11da | 2023-04-05 16:25:57 +0200 | [diff] [blame] | 412 | struct comp_algo *comp_algo; |
| 413 | int comp_index; |
| 414 | |
| 415 | if (msg->chn->flags & CF_ISRESP) |
| 416 | comp_index = COMP_DIR_RES; |
| 417 | else |
| 418 | comp_index = COMP_DIR_REQ; |
Christopher Faulet | 27d93c3 | 2018-12-15 22:32:02 +0100 | [diff] [blame] | 419 | |
Christopher Faulet | 39e436e | 2022-04-15 15:32:03 +0200 | [diff] [blame] | 420 | sl = http_get_stline(htx); |
| 421 | if (!sl) |
| 422 | goto error; |
| 423 | |
Olivier Houchard | dfc11da | 2023-04-05 16:25:57 +0200 | [diff] [blame] | 424 | comp_algo = st->comp_algo[comp_index]; |
| 425 | |
Christopher Faulet | 910b757 | 2022-10-24 08:39:29 +0200 | [diff] [blame] | 426 | /* add "Transfer-Encoding: chunked" header */ |
| 427 | if (!(msg->flags & HTTP_MSGF_TE_CHNK)) { |
| 428 | if (!http_add_header(htx, ist("Transfer-Encoding"), ist("chunked"))) |
| 429 | goto error; |
| 430 | msg->flags |= HTTP_MSGF_TE_CHNK; |
| 431 | sl->flags |= (HTX_SL_F_XFER_ENC|HTX_SL_F_CHNK); |
| 432 | } |
| 433 | |
Christopher Faulet | 27d93c3 | 2018-12-15 22:32:02 +0100 | [diff] [blame] | 434 | /* remove Content-Length header */ |
| 435 | if (msg->flags & HTTP_MSGF_CNT_LEN) { |
Christopher Faulet | 27d93c3 | 2018-12-15 22:32:02 +0100 | [diff] [blame] | 436 | ctx.blk = NULL; |
| 437 | while (http_find_header(htx, ist("Content-Length"), &ctx, 1)) |
| 438 | http_remove_header(htx, &ctx); |
Christopher Faulet | 39e436e | 2022-04-15 15:32:03 +0200 | [diff] [blame] | 439 | msg->flags &= ~HTTP_MSGF_CNT_LEN; |
| 440 | sl->flags &= ~HTX_SL_F_CLEN; |
Christopher Faulet | 27d93c3 | 2018-12-15 22:32:02 +0100 | [diff] [blame] | 441 | } |
| 442 | |
Tim Duesterhus | b229f01 | 2019-01-29 16:38:56 +0100 | [diff] [blame] | 443 | /* convert "ETag" header to a weak ETag */ |
| 444 | ctx.blk = NULL; |
| 445 | if (http_find_header(htx, ist("ETag"), &ctx, 1)) { |
| 446 | if (ctx.value.ptr[0] == '"') { |
| 447 | /* This a strong ETag. Convert it to a weak one. */ |
| 448 | struct ist v = ist2(trash.area, 0); |
| 449 | if (istcat(&v, ist("W/"), trash.size) == -1 || istcat(&v, ctx.value, trash.size) == -1) |
| 450 | goto error; |
| 451 | |
| 452 | if (!http_replace_header_value(htx, &ctx, v)) |
| 453 | goto error; |
| 454 | } |
| 455 | } |
| 456 | |
Tim Duesterhus | 721d686 | 2019-06-17 16:10:07 +0200 | [diff] [blame] | 457 | if (!http_add_header(htx, ist("Vary"), ist("Accept-Encoding"))) |
| 458 | goto error; |
| 459 | |
Christopher Faulet | 910b757 | 2022-10-24 08:39:29 +0200 | [diff] [blame] | 460 | /* |
| 461 | * Add Content-Encoding header when it's not identity encoding. |
| 462 | * RFC 2616 : Identity encoding: This content-coding is used only in the |
| 463 | * Accept-Encoding header, and SHOULD NOT be used in the Content-Encoding |
| 464 | * header. |
| 465 | */ |
Olivier Houchard | dfc11da | 2023-04-05 16:25:57 +0200 | [diff] [blame] | 466 | if (comp_algo->cfg_name_len != 8 || memcmp(comp_algo->cfg_name, "identity", 8) != 0) { |
| 467 | struct ist v = ist2(comp_algo->ua_name, comp_algo->ua_name_len); |
Christopher Faulet | 910b757 | 2022-10-24 08:39:29 +0200 | [diff] [blame] | 468 | |
| 469 | if (!http_add_header(htx, ist("Content-Encoding"), v)) |
| 470 | goto error; |
| 471 | } |
| 472 | |
Christopher Faulet | 27d93c3 | 2018-12-15 22:32:02 +0100 | [diff] [blame] | 473 | return 1; |
| 474 | |
| 475 | error: |
Olivier Houchard | dfc11da | 2023-04-05 16:25:57 +0200 | [diff] [blame] | 476 | st->comp_algo[comp_index]->end(&st->comp_ctx[comp_index]); |
| 477 | st->comp_algo[comp_index] = NULL; |
Christopher Faulet | 27d93c3 | 2018-12-15 22:32:02 +0100 | [diff] [blame] | 478 | return 0; |
| 479 | } |
| 480 | |
Christopher Faulet | 3d97c90 | 2015-12-09 14:59:38 +0100 | [diff] [blame] | 481 | /* |
| 482 | * Selects a compression algorithm depending on the client request. |
| 483 | */ |
Christopher Faulet | e6902cd | 2018-11-30 22:29:48 +0100 | [diff] [blame] | 484 | static int |
Christopher Faulet | 89f2b16 | 2019-07-15 21:16:04 +0200 | [diff] [blame] | 485 | select_compression_request_header(struct comp_state *st, struct stream *s, struct http_msg *msg) |
Christopher Faulet | e6902cd | 2018-11-30 22:29:48 +0100 | [diff] [blame] | 486 | { |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 487 | struct htx *htx = htxbuf(&msg->chn->buf); |
Christopher Faulet | e6902cd | 2018-11-30 22:29:48 +0100 | [diff] [blame] | 488 | struct http_hdr_ctx ctx; |
| 489 | struct comp_algo *comp_algo = NULL; |
| 490 | struct comp_algo *comp_algo_back = NULL; |
| 491 | |
| 492 | /* Disable compression for older user agents announcing themselves as "Mozilla/4" |
| 493 | * unless they are known good (MSIE 6 with XP SP2, or MSIE 7 and later). |
| 494 | * See http://zoompf.com/2012/02/lose-the-wait-http-compression for more details. |
| 495 | */ |
| 496 | ctx.blk = NULL; |
| 497 | if (http_find_header(htx, ist("User-Agent"), &ctx, 1) && |
| 498 | ctx.value.len >= 9 && |
| 499 | memcmp(ctx.value.ptr, "Mozilla/4", 9) == 0 && |
| 500 | (ctx.value.len < 31 || |
| 501 | memcmp(ctx.value.ptr + 25, "MSIE ", 5) != 0 || |
| 502 | *(ctx.value.ptr + 30) < '6' || |
| 503 | (*(ctx.value.ptr + 30) == '6' && |
| 504 | (ctx.value.len < 54 || memcmp(ctx.value.ptr + 51, "SV1", 3) != 0)))) { |
Olivier Houchard | dfc11da | 2023-04-05 16:25:57 +0200 | [diff] [blame] | 505 | st->comp_algo[COMP_DIR_RES] = NULL; |
Christopher Faulet | e6902cd | 2018-11-30 22:29:48 +0100 | [diff] [blame] | 506 | return 0; |
| 507 | } |
| 508 | |
| 509 | /* search for the algo in the backend in priority or the frontend */ |
Olivier Houchard | db573e9 | 2023-04-05 17:32:36 +0200 | [diff] [blame] | 510 | if ((s->be->comp && (comp_algo_back = s->be->comp->algos_res)) || |
| 511 | (strm_fe(s)->comp && (comp_algo_back = strm_fe(s)->comp->algos_res))) { |
Christopher Faulet | e6902cd | 2018-11-30 22:29:48 +0100 | [diff] [blame] | 512 | int best_q = 0; |
| 513 | |
| 514 | ctx.blk = NULL; |
| 515 | while (http_find_header(htx, ist("Accept-Encoding"), &ctx, 0)) { |
| 516 | const char *qval; |
| 517 | int q; |
| 518 | int toklen; |
| 519 | |
| 520 | /* try to isolate the token from the optional q-value */ |
| 521 | toklen = 0; |
| 522 | while (toklen < ctx.value.len && HTTP_IS_TOKEN(*(ctx.value.ptr + toklen))) |
| 523 | toklen++; |
| 524 | |
| 525 | qval = ctx.value.ptr + toklen; |
| 526 | while (1) { |
Tim Duesterhus | 7750850 | 2022-03-15 13:11:06 +0100 | [diff] [blame] | 527 | while (qval < istend(ctx.value) && HTTP_IS_LWS(*qval)) |
Christopher Faulet | e6902cd | 2018-11-30 22:29:48 +0100 | [diff] [blame] | 528 | qval++; |
| 529 | |
Tim Duesterhus | 7750850 | 2022-03-15 13:11:06 +0100 | [diff] [blame] | 530 | if (qval >= istend(ctx.value) || *qval != ';') { |
Christopher Faulet | e6902cd | 2018-11-30 22:29:48 +0100 | [diff] [blame] | 531 | qval = NULL; |
| 532 | break; |
| 533 | } |
| 534 | qval++; |
| 535 | |
Tim Duesterhus | 7750850 | 2022-03-15 13:11:06 +0100 | [diff] [blame] | 536 | while (qval < istend(ctx.value) && HTTP_IS_LWS(*qval)) |
Christopher Faulet | e6902cd | 2018-11-30 22:29:48 +0100 | [diff] [blame] | 537 | qval++; |
| 538 | |
Tim Duesterhus | 7750850 | 2022-03-15 13:11:06 +0100 | [diff] [blame] | 539 | if (qval >= istend(ctx.value)) { |
Christopher Faulet | e6902cd | 2018-11-30 22:29:48 +0100 | [diff] [blame] | 540 | qval = NULL; |
| 541 | break; |
| 542 | } |
Tim Duesterhus | 7750850 | 2022-03-15 13:11:06 +0100 | [diff] [blame] | 543 | if (strncmp(qval, "q=", MIN(istend(ctx.value) - qval, 2)) == 0) |
Christopher Faulet | e6902cd | 2018-11-30 22:29:48 +0100 | [diff] [blame] | 544 | break; |
| 545 | |
Tim Duesterhus | 7750850 | 2022-03-15 13:11:06 +0100 | [diff] [blame] | 546 | while (qval < istend(ctx.value) && *qval != ';') |
Christopher Faulet | e6902cd | 2018-11-30 22:29:48 +0100 | [diff] [blame] | 547 | qval++; |
| 548 | } |
| 549 | |
| 550 | /* here we have qval pointing to the first "q=" attribute or NULL if not found */ |
| 551 | q = qval ? http_parse_qvalue(qval + 2, NULL) : 1000; |
| 552 | |
| 553 | if (q <= best_q) |
| 554 | continue; |
| 555 | |
| 556 | for (comp_algo = comp_algo_back; comp_algo; comp_algo = comp_algo->next) { |
| 557 | if (*(ctx.value.ptr) == '*' || |
| 558 | word_match(ctx.value.ptr, toklen, comp_algo->ua_name, comp_algo->ua_name_len)) { |
Olivier Houchard | dfc11da | 2023-04-05 16:25:57 +0200 | [diff] [blame] | 559 | st->comp_algo[COMP_DIR_RES] = comp_algo; |
Christopher Faulet | e6902cd | 2018-11-30 22:29:48 +0100 | [diff] [blame] | 560 | best_q = q; |
| 561 | break; |
| 562 | } |
| 563 | } |
| 564 | } |
| 565 | } |
| 566 | |
| 567 | /* remove all occurrences of the header when "compression offload" is set */ |
Olivier Houchard | dfc11da | 2023-04-05 16:25:57 +0200 | [diff] [blame] | 568 | if (st->comp_algo[COMP_DIR_RES]) { |
Olivier Houchard | 3ce0f01 | 2023-04-03 22:22:24 +0200 | [diff] [blame] | 569 | if ((s->be->comp && (s->be->comp->flags & COMP_FL_OFFLOAD)) || |
| 570 | (strm_fe(s)->comp && (strm_fe(s)->comp->flags & COMP_FL_OFFLOAD))) { |
Christopher Faulet | e6902cd | 2018-11-30 22:29:48 +0100 | [diff] [blame] | 571 | http_remove_header(htx, &ctx); |
| 572 | ctx.blk = NULL; |
| 573 | while (http_find_header(htx, ist("Accept-Encoding"), &ctx, 1)) |
| 574 | http_remove_header(htx, &ctx); |
| 575 | } |
Christopher Faulet | 3d97c90 | 2015-12-09 14:59:38 +0100 | [diff] [blame] | 576 | return 1; |
| 577 | } |
| 578 | |
| 579 | /* identity is implicit does not require headers */ |
Olivier Houchard | db573e9 | 2023-04-05 17:32:36 +0200 | [diff] [blame] | 580 | if ((s->be->comp && (comp_algo_back = s->be->comp->algos_res)) || |
| 581 | (strm_fe(s)->comp && (comp_algo_back = strm_fe(s)->comp->algos_res))) { |
Christopher Faulet | 3d97c90 | 2015-12-09 14:59:38 +0100 | [diff] [blame] | 582 | for (comp_algo = comp_algo_back; comp_algo; comp_algo = comp_algo->next) { |
| 583 | if (comp_algo->cfg_name_len == 8 && memcmp(comp_algo->cfg_name, "identity", 8) == 0) { |
Olivier Houchard | dfc11da | 2023-04-05 16:25:57 +0200 | [diff] [blame] | 584 | st->comp_algo[COMP_DIR_RES] = comp_algo; |
Christopher Faulet | 3d97c90 | 2015-12-09 14:59:38 +0100 | [diff] [blame] | 585 | return 1; |
| 586 | } |
| 587 | } |
| 588 | } |
| 589 | |
Olivier Houchard | dfc11da | 2023-04-05 16:25:57 +0200 | [diff] [blame] | 590 | st->comp_algo[COMP_DIR_RES] = NULL; |
Christopher Faulet | 3d97c90 | 2015-12-09 14:59:38 +0100 | [diff] [blame] | 591 | return 0; |
| 592 | } |
| 593 | |
| 594 | /* |
Ilya Shipitsin | 46a030c | 2020-07-05 16:36:08 +0500 | [diff] [blame] | 595 | * Selects a compression algorithm depending of the server response. |
Christopher Faulet | 3d97c90 | 2015-12-09 14:59:38 +0100 | [diff] [blame] | 596 | */ |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 597 | static int |
Christopher Faulet | 89f2b16 | 2019-07-15 21:16:04 +0200 | [diff] [blame] | 598 | select_compression_response_header(struct comp_state *st, struct stream *s, struct http_msg *msg) |
Christopher Faulet | e6902cd | 2018-11-30 22:29:48 +0100 | [diff] [blame] | 599 | { |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 600 | struct htx *htx = htxbuf(&msg->chn->buf); |
Christopher Faulet | e6902cd | 2018-11-30 22:29:48 +0100 | [diff] [blame] | 601 | struct http_txn *txn = s->txn; |
| 602 | struct http_hdr_ctx ctx; |
| 603 | struct comp_type *comp_type; |
| 604 | |
| 605 | /* no common compression algorithm was found in request header */ |
Olivier Houchard | dfc11da | 2023-04-05 16:25:57 +0200 | [diff] [blame] | 606 | if (st->comp_algo[COMP_DIR_RES] == NULL) |
Christopher Faulet | e6902cd | 2018-11-30 22:29:48 +0100 | [diff] [blame] | 607 | goto fail; |
| 608 | |
Christopher Faulet | 1d3613a | 2019-01-07 14:41:59 +0100 | [diff] [blame] | 609 | /* compression already in progress */ |
| 610 | if (msg->flags & HTTP_MSGF_COMPRESSING) |
| 611 | goto fail; |
| 612 | |
Christopher Faulet | e6902cd | 2018-11-30 22:29:48 +0100 | [diff] [blame] | 613 | /* HTTP < 1.1 should not be compressed */ |
| 614 | if (!(msg->flags & HTTP_MSGF_VER_11) || !(txn->req.flags & HTTP_MSGF_VER_11)) |
| 615 | goto fail; |
| 616 | |
| 617 | if (txn->meth == HTTP_METH_HEAD) |
| 618 | goto fail; |
| 619 | |
| 620 | /* compress 200,201,202,203 responses only */ |
| 621 | if ((txn->status != 200) && |
| 622 | (txn->status != 201) && |
| 623 | (txn->status != 202) && |
| 624 | (txn->status != 203)) |
| 625 | goto fail; |
| 626 | |
Christopher Faulet | c963eb2 | 2018-12-21 14:53:54 +0100 | [diff] [blame] | 627 | if (!(msg->flags & HTTP_MSGF_XFER_LEN) || msg->flags & HTTP_MSGF_BODYLESS) |
Christopher Faulet | e6902cd | 2018-11-30 22:29:48 +0100 | [diff] [blame] | 628 | goto fail; |
| 629 | |
| 630 | /* content is already compressed */ |
| 631 | ctx.blk = NULL; |
| 632 | if (http_find_header(htx, ist("Content-Encoding"), &ctx, 1)) |
| 633 | goto fail; |
| 634 | |
| 635 | /* no compression when Cache-Control: no-transform is present in the message */ |
| 636 | ctx.blk = NULL; |
| 637 | while (http_find_header(htx, ist("Cache-Control"), &ctx, 0)) { |
| 638 | if (word_match(ctx.value.ptr, ctx.value.len, "no-transform", 12)) |
| 639 | goto fail; |
| 640 | } |
| 641 | |
Tim Duesterhus | b229f01 | 2019-01-29 16:38:56 +0100 | [diff] [blame] | 642 | /* no compression when ETag is malformed */ |
| 643 | ctx.blk = NULL; |
| 644 | if (http_find_header(htx, ist("ETag"), &ctx, 1)) { |
Tim Duesterhus | 6414cd1 | 2020-09-01 18:32:35 +0200 | [diff] [blame] | 645 | if (http_get_etag_type(ctx.value) == ETAG_INVALID) |
Tim Duesterhus | b229f01 | 2019-01-29 16:38:56 +0100 | [diff] [blame] | 646 | goto fail; |
Tim Duesterhus | b229f01 | 2019-01-29 16:38:56 +0100 | [diff] [blame] | 647 | } |
| 648 | /* no compression when multiple ETags are present |
| 649 | * Note: Do not reset ctx.blk! |
| 650 | */ |
| 651 | if (http_find_header(htx, ist("ETag"), &ctx, 1)) |
| 652 | goto fail; |
| 653 | |
Christopher Faulet | e6902cd | 2018-11-30 22:29:48 +0100 | [diff] [blame] | 654 | comp_type = NULL; |
| 655 | |
| 656 | /* we don't want to compress multipart content-types, nor content-types that are |
| 657 | * not listed in the "compression type" directive if any. If no content-type was |
| 658 | * found but configuration requires one, we don't compress either. Backend has |
| 659 | * the priority. |
| 660 | */ |
| 661 | ctx.blk = NULL; |
| 662 | if (http_find_header(htx, ist("Content-Type"), &ctx, 1)) { |
| 663 | if (ctx.value.len >= 9 && strncasecmp("multipart", ctx.value.ptr, 9) == 0) |
| 664 | goto fail; |
| 665 | |
Olivier Houchard | db573e9 | 2023-04-05 17:32:36 +0200 | [diff] [blame] | 666 | if ((s->be->comp && (comp_type = s->be->comp->types_res)) || |
| 667 | (strm_fe(s)->comp && (comp_type = strm_fe(s)->comp->types_res))) { |
Christopher Faulet | e6902cd | 2018-11-30 22:29:48 +0100 | [diff] [blame] | 668 | for (; comp_type; comp_type = comp_type->next) { |
| 669 | if (ctx.value.len >= comp_type->name_len && |
| 670 | strncasecmp(ctx.value.ptr, comp_type->name, comp_type->name_len) == 0) |
| 671 | /* this Content-Type should be compressed */ |
| 672 | break; |
| 673 | } |
| 674 | /* this Content-Type should not be compressed */ |
| 675 | if (comp_type == NULL) |
| 676 | goto fail; |
| 677 | } |
| 678 | } |
| 679 | else { /* no content-type header */ |
Olivier Houchard | db573e9 | 2023-04-05 17:32:36 +0200 | [diff] [blame] | 680 | if ((s->be->comp && s->be->comp->types_res) || |
| 681 | (strm_fe(s)->comp && strm_fe(s)->comp->types_res)) |
Christopher Faulet | e6902cd | 2018-11-30 22:29:48 +0100 | [diff] [blame] | 682 | goto fail; /* a content-type was required */ |
| 683 | } |
| 684 | |
| 685 | /* limit compression rate */ |
| 686 | if (global.comp_rate_lim > 0) |
| 687 | if (read_freq_ctr(&global.comp_bps_in) > global.comp_rate_lim) |
| 688 | goto fail; |
| 689 | |
| 690 | /* limit cpu usage */ |
Willy Tarreau | 45c38e2 | 2021-09-30 18:28:49 +0200 | [diff] [blame] | 691 | if (th_ctx->idle_pct < compress_min_idle) |
Christopher Faulet | e6902cd | 2018-11-30 22:29:48 +0100 | [diff] [blame] | 692 | goto fail; |
| 693 | |
| 694 | /* initialize compression */ |
Olivier Houchard | dfc11da | 2023-04-05 16:25:57 +0200 | [diff] [blame] | 695 | if (st->comp_algo[COMP_DIR_RES]->init(&st->comp_ctx[COMP_DIR_RES], global.tune.comp_maxlevel) < 0) |
Christopher Faulet | e6902cd | 2018-11-30 22:29:48 +0100 | [diff] [blame] | 696 | goto fail; |
Christopher Faulet | e6902cd | 2018-11-30 22:29:48 +0100 | [diff] [blame] | 697 | msg->flags |= HTTP_MSGF_COMPRESSING; |
| 698 | return 1; |
| 699 | |
Christopher Faulet | e6902cd | 2018-11-30 22:29:48 +0100 | [diff] [blame] | 700 | fail: |
Olivier Houchard | dfc11da | 2023-04-05 16:25:57 +0200 | [diff] [blame] | 701 | st->comp_algo[COMP_DIR_RES] = NULL; |
Christopher Faulet | e6902cd | 2018-11-30 22:29:48 +0100 | [diff] [blame] | 702 | return 0; |
| 703 | } |
| 704 | |
Christopher Faulet | 3d97c90 | 2015-12-09 14:59:38 +0100 | [diff] [blame] | 705 | /***********************************************************************/ |
Christopher Faulet | e6902cd | 2018-11-30 22:29:48 +0100 | [diff] [blame] | 706 | static int |
| 707 | htx_compression_buffer_init(struct htx *htx, struct buffer *out) |
| 708 | { |
| 709 | /* output stream requires at least 10 bytes for the gzip header, plus |
| 710 | * at least 8 bytes for the gzip trailer (crc+len), plus a possible |
| 711 | * plus at most 5 bytes per 32kB block and 2 bytes to close the stream. |
| 712 | */ |
| 713 | if (htx_free_space(htx) < 20 + 5 * ((htx->data + 32767) >> 15)) |
| 714 | return -1; |
| 715 | b_reset(out); |
| 716 | return 0; |
| 717 | } |
| 718 | |
Christopher Faulet | e6902cd | 2018-11-30 22:29:48 +0100 | [diff] [blame] | 719 | static int |
| 720 | htx_compression_buffer_add_data(struct comp_state *st, const char *data, size_t len, |
Olivier Houchard | dfc11da | 2023-04-05 16:25:57 +0200 | [diff] [blame] | 721 | struct buffer *out, int dir) |
Christopher Faulet | e6902cd | 2018-11-30 22:29:48 +0100 | [diff] [blame] | 722 | { |
Olivier Houchard | dfc11da | 2023-04-05 16:25:57 +0200 | [diff] [blame] | 723 | |
| 724 | return st->comp_algo[dir]->add_data(st->comp_ctx[dir], data, len, out); |
Christopher Faulet | e6902cd | 2018-11-30 22:29:48 +0100 | [diff] [blame] | 725 | } |
| 726 | |
Christopher Faulet | e6902cd | 2018-11-30 22:29:48 +0100 | [diff] [blame] | 727 | static int |
Olivier Houchard | dfc11da | 2023-04-05 16:25:57 +0200 | [diff] [blame] | 728 | htx_compression_buffer_end(struct comp_state *st, struct buffer *out, int end, int dir) |
Christopher Faulet | e6902cd | 2018-11-30 22:29:48 +0100 | [diff] [blame] | 729 | { |
Olivier Houchard | dfc11da | 2023-04-05 16:25:57 +0200 | [diff] [blame] | 730 | |
Christopher Faulet | e6902cd | 2018-11-30 22:29:48 +0100 | [diff] [blame] | 731 | if (end) |
Olivier Houchard | dfc11da | 2023-04-05 16:25:57 +0200 | [diff] [blame] | 732 | return st->comp_algo[dir]->finish(st->comp_ctx[dir], out); |
Christopher Faulet | e6902cd | 2018-11-30 22:29:48 +0100 | [diff] [blame] | 733 | else |
Olivier Houchard | dfc11da | 2023-04-05 16:25:57 +0200 | [diff] [blame] | 734 | return st->comp_algo[dir]->flush(st->comp_ctx[dir], out); |
Christopher Faulet | e6902cd | 2018-11-30 22:29:48 +0100 | [diff] [blame] | 735 | } |
| 736 | |
Christopher Faulet | 3d97c90 | 2015-12-09 14:59:38 +0100 | [diff] [blame] | 737 | |
| 738 | /***********************************************************************/ |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 739 | struct flt_ops comp_ops = { |
Christopher Faulet | e6902cd | 2018-11-30 22:29:48 +0100 | [diff] [blame] | 740 | .init = comp_flt_init, |
Christopher Faulet | 8ca3b4b | 2017-07-25 11:07:15 +0200 | [diff] [blame] | 741 | .init_per_thread = comp_flt_init_per_thread, |
| 742 | .deinit_per_thread = comp_flt_deinit_per_thread, |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 743 | |
Christopher Faulet | 5e89651 | 2020-03-06 14:59:05 +0100 | [diff] [blame] | 744 | .attach = comp_strm_init, |
| 745 | .detach = comp_strm_deinit, |
| 746 | |
Christopher Faulet | 3dc860d | 2017-09-15 11:39:36 +0200 | [diff] [blame] | 747 | .channel_post_analyze = comp_http_post_analyze, |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 748 | |
Christopher Faulet | 1339d74 | 2016-05-11 16:48:33 +0200 | [diff] [blame] | 749 | .http_headers = comp_http_headers, |
Christopher Faulet | e6902cd | 2018-11-30 22:29:48 +0100 | [diff] [blame] | 750 | .http_payload = comp_http_payload, |
| 751 | .http_end = comp_http_end, |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 752 | }; |
| 753 | |
Christopher Faulet | 3d97c90 | 2015-12-09 14:59:38 +0100 | [diff] [blame] | 754 | static int |
| 755 | parse_compression_options(char **args, int section, struct proxy *proxy, |
Willy Tarreau | 0182516 | 2021-03-09 09:53:46 +0100 | [diff] [blame] | 756 | const struct proxy *defpx, const char *file, int line, |
Christopher Faulet | 3d97c90 | 2015-12-09 14:59:38 +0100 | [diff] [blame] | 757 | char **err) |
| 758 | { |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 759 | struct comp *comp; |
Christopher Faulet | 44d34bf | 2021-11-05 12:06:14 +0100 | [diff] [blame] | 760 | int ret = 0; |
Christopher Faulet | 3d97c90 | 2015-12-09 14:59:38 +0100 | [diff] [blame] | 761 | |
| 762 | if (proxy->comp == NULL) { |
Vincent Bernat | 02779b6 | 2016-04-03 13:48:43 +0200 | [diff] [blame] | 763 | comp = calloc(1, sizeof(*comp)); |
Olivier Houchard | ead43fe | 2023-04-06 00:33:48 +0200 | [diff] [blame^] | 764 | /* Always default to compress responses */ |
| 765 | comp->flags = COMP_FL_DIR_RES; |
Christopher Faulet | 3d97c90 | 2015-12-09 14:59:38 +0100 | [diff] [blame] | 766 | proxy->comp = comp; |
| 767 | } |
| 768 | else |
| 769 | comp = proxy->comp; |
| 770 | |
Olivier Houchard | db573e9 | 2023-04-05 17:32:36 +0200 | [diff] [blame] | 771 | if (strcmp(args[1], "algo") == 0 || strcmp(args[1], "algo-res") == 0) { |
Christopher Faulet | 3d97c90 | 2015-12-09 14:59:38 +0100 | [diff] [blame] | 772 | struct comp_ctx *ctx; |
| 773 | int cur_arg = 2; |
| 774 | |
| 775 | if (!*args[cur_arg]) { |
Christopher Faulet | 44d34bf | 2021-11-05 12:06:14 +0100 | [diff] [blame] | 776 | memprintf(err, "parsing [%s:%d] : '%s' expects <algorithm>.", |
Christopher Faulet | 3d97c90 | 2015-12-09 14:59:38 +0100 | [diff] [blame] | 777 | file, line, args[0]); |
Christopher Faulet | 44d34bf | 2021-11-05 12:06:14 +0100 | [diff] [blame] | 778 | ret = -1; |
| 779 | goto end; |
Christopher Faulet | 3d97c90 | 2015-12-09 14:59:38 +0100 | [diff] [blame] | 780 | } |
| 781 | while (*(args[cur_arg])) { |
Olivier Houchard | db573e9 | 2023-04-05 17:32:36 +0200 | [diff] [blame] | 782 | int retval = comp_append_algo(&comp->algos_res, args[cur_arg]); |
Remi Tricot-Le Breton | 6443bcc | 2021-05-17 10:35:08 +0200 | [diff] [blame] | 783 | if (retval) { |
| 784 | if (retval < 0) |
Christopher Faulet | 44d34bf | 2021-11-05 12:06:14 +0100 | [diff] [blame] | 785 | memprintf(err, "'%s' : '%s' is not a supported algorithm.", |
Remi Tricot-Le Breton | 6443bcc | 2021-05-17 10:35:08 +0200 | [diff] [blame] | 786 | args[0], args[cur_arg]); |
| 787 | else |
Christopher Faulet | 44d34bf | 2021-11-05 12:06:14 +0100 | [diff] [blame] | 788 | memprintf(err, "'%s' : out of memory while parsing algo '%s'.", |
Remi Tricot-Le Breton | 6443bcc | 2021-05-17 10:35:08 +0200 | [diff] [blame] | 789 | args[0], args[cur_arg]); |
Christopher Faulet | 44d34bf | 2021-11-05 12:06:14 +0100 | [diff] [blame] | 790 | ret = -1; |
| 791 | goto end; |
Christopher Faulet | 3d97c90 | 2015-12-09 14:59:38 +0100 | [diff] [blame] | 792 | } |
Remi Tricot-Le Breton | 6443bcc | 2021-05-17 10:35:08 +0200 | [diff] [blame] | 793 | |
Olivier Houchard | db573e9 | 2023-04-05 17:32:36 +0200 | [diff] [blame] | 794 | if (proxy->comp->algos_res->init(&ctx, 9) == 0) |
| 795 | proxy->comp->algos_res->end(&ctx); |
Christopher Faulet | 3d97c90 | 2015-12-09 14:59:38 +0100 | [diff] [blame] | 796 | else { |
Christopher Faulet | 44d34bf | 2021-11-05 12:06:14 +0100 | [diff] [blame] | 797 | memprintf(err, "'%s' : Can't init '%s' algorithm.", |
Christopher Faulet | 3d97c90 | 2015-12-09 14:59:38 +0100 | [diff] [blame] | 798 | args[0], args[cur_arg]); |
Christopher Faulet | 44d34bf | 2021-11-05 12:06:14 +0100 | [diff] [blame] | 799 | ret = -1; |
| 800 | goto end; |
Christopher Faulet | 3d97c90 | 2015-12-09 14:59:38 +0100 | [diff] [blame] | 801 | } |
| 802 | cur_arg++; |
| 803 | continue; |
| 804 | } |
| 805 | } |
Olivier Houchard | ead43fe | 2023-04-06 00:33:48 +0200 | [diff] [blame^] | 806 | else if (strcmp(args[1], "algo-req") == 0) { |
| 807 | struct comp_ctx *ctx; |
| 808 | int retval = comp_append_algo(&comp->algo_req, args[2]); |
| 809 | |
| 810 | if (retval) { |
| 811 | if (retval < 0) |
| 812 | memprintf(err, "'%s' : '%s' is not a supported algorithm.", |
| 813 | args[0], args[2]); |
| 814 | else |
| 815 | memprintf(err, "'%s' : out of memory while parsing algo '%s'.", |
| 816 | args[0], args[2]); |
| 817 | ret = -1; |
| 818 | goto end; |
| 819 | } |
| 820 | |
| 821 | if (proxy->comp->algo_req->init(&ctx, 9) == 0) |
| 822 | proxy->comp->algo_req->end(&ctx); |
| 823 | else { |
| 824 | memprintf(err, "'%s' : Can't init '%s' algorithm.", |
| 825 | args[0], args[2]); |
| 826 | ret = -1; |
| 827 | goto end; |
| 828 | } |
| 829 | } |
Christopher Faulet | 44d34bf | 2021-11-05 12:06:14 +0100 | [diff] [blame] | 830 | else if (strcmp(args[1], "offload") == 0) { |
| 831 | if (proxy->cap & PR_CAP_DEF) { |
| 832 | memprintf(err, "'%s' : '%s' ignored in 'defaults' section.", |
| 833 | args[0], args[1]); |
| 834 | ret = 1; |
| 835 | } |
Olivier Houchard | 3ce0f01 | 2023-04-03 22:22:24 +0200 | [diff] [blame] | 836 | comp->flags |= COMP_FL_OFFLOAD; |
Christopher Faulet | 44d34bf | 2021-11-05 12:06:14 +0100 | [diff] [blame] | 837 | } |
Olivier Houchard | db573e9 | 2023-04-05 17:32:36 +0200 | [diff] [blame] | 838 | else if (strcmp(args[1], "type") == 0 || strcmp(args[1], "type-res") == 0) { |
Christopher Faulet | 3d97c90 | 2015-12-09 14:59:38 +0100 | [diff] [blame] | 839 | int cur_arg = 2; |
| 840 | |
| 841 | if (!*args[cur_arg]) { |
Christopher Faulet | 44d34bf | 2021-11-05 12:06:14 +0100 | [diff] [blame] | 842 | memprintf(err, "'%s' expects <type>.", args[0]); |
| 843 | ret = -1; |
| 844 | goto end; |
Christopher Faulet | 3d97c90 | 2015-12-09 14:59:38 +0100 | [diff] [blame] | 845 | } |
| 846 | while (*(args[cur_arg])) { |
Olivier Houchard | db573e9 | 2023-04-05 17:32:36 +0200 | [diff] [blame] | 847 | if (comp_append_type(&comp->types_res, args[cur_arg])) { |
Remi Tricot-Le Breton | 6443bcc | 2021-05-17 10:35:08 +0200 | [diff] [blame] | 848 | memprintf(err, "'%s': out of memory.", args[0]); |
Christopher Faulet | 44d34bf | 2021-11-05 12:06:14 +0100 | [diff] [blame] | 849 | ret = -1; |
| 850 | goto end; |
Remi Tricot-Le Breton | 6443bcc | 2021-05-17 10:35:08 +0200 | [diff] [blame] | 851 | } |
Christopher Faulet | 3d97c90 | 2015-12-09 14:59:38 +0100 | [diff] [blame] | 852 | cur_arg++; |
| 853 | continue; |
| 854 | } |
| 855 | } |
Olivier Houchard | ead43fe | 2023-04-06 00:33:48 +0200 | [diff] [blame^] | 856 | else if (strcmp(args[1], "type-req") == 0) { |
| 857 | int cur_arg = 2; |
| 858 | |
| 859 | if (!*args[cur_arg]) { |
| 860 | memprintf(err, "'%s' expects <type>.", args[0]); |
| 861 | ret = -1; |
| 862 | goto end; |
| 863 | } |
| 864 | while (*(args[cur_arg])) { |
| 865 | if (comp_append_type(&comp->types_req, args[cur_arg])) { |
| 866 | memprintf(err, "'%s': out of memory.", args[0]); |
| 867 | ret = -1; |
| 868 | goto end; |
| 869 | } |
| 870 | cur_arg++; |
| 871 | continue; |
| 872 | } |
| 873 | } |
| 874 | else if (strcmp(args[1], "direction") == 0) { |
| 875 | if (!args[2]) { |
| 876 | memprintf(err, "'%s' expects 'request', 'response', or 'both'.", args[0]); |
| 877 | ret = -1; |
| 878 | goto end; |
| 879 | } |
| 880 | if (strcmp(args[2], "request") == 0) { |
| 881 | comp->flags &= ~COMP_FL_DIR_RES; |
| 882 | comp->flags |= COMP_FL_DIR_REQ; |
| 883 | } else if (strcmp(args[2], "response") == 0) { |
| 884 | comp->flags &= COMP_FL_DIR_REQ; |
| 885 | comp->flags |= COMP_FL_DIR_RES; |
| 886 | } else if (strcmp(args[2], "both") == 0) |
| 887 | comp->flags |= COMP_FL_DIR_REQ | COMP_FL_DIR_RES; |
| 888 | else { |
| 889 | memprintf(err, "'%s' expects 'request', 'response', or 'both'.", args[0]); |
| 890 | ret = -1; |
| 891 | goto end; |
| 892 | } |
| 893 | } |
Christopher Faulet | 3d97c90 | 2015-12-09 14:59:38 +0100 | [diff] [blame] | 894 | else { |
Olivier Houchard | ead43fe | 2023-04-06 00:33:48 +0200 | [diff] [blame^] | 895 | memprintf(err, "'%s' expects 'algo', 'type' 'direction' or 'offload'", |
Christopher Faulet | 3d97c90 | 2015-12-09 14:59:38 +0100 | [diff] [blame] | 896 | args[0]); |
Christopher Faulet | 44d34bf | 2021-11-05 12:06:14 +0100 | [diff] [blame] | 897 | ret = -1; |
| 898 | goto end; |
Christopher Faulet | 3d97c90 | 2015-12-09 14:59:38 +0100 | [diff] [blame] | 899 | } |
| 900 | |
Christopher Faulet | 44d34bf | 2021-11-05 12:06:14 +0100 | [diff] [blame] | 901 | end: |
| 902 | return ret; |
Christopher Faulet | 3d97c90 | 2015-12-09 14:59:38 +0100 | [diff] [blame] | 903 | } |
| 904 | |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 905 | static int |
| 906 | parse_http_comp_flt(char **args, int *cur_arg, struct proxy *px, |
Thierry Fournier | 3610c39 | 2016-04-13 18:27:51 +0200 | [diff] [blame] | 907 | struct flt_conf *fconf, char **err, void *private) |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 908 | { |
Christopher Faulet | 443ea1a | 2016-02-04 13:40:26 +0100 | [diff] [blame] | 909 | struct flt_conf *fc, *back; |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 910 | |
Christopher Faulet | 443ea1a | 2016-02-04 13:40:26 +0100 | [diff] [blame] | 911 | list_for_each_entry_safe(fc, back, &px->filter_configs, list) { |
| 912 | if (fc->id == http_comp_flt_id) { |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 913 | memprintf(err, "%s: Proxy supports only one compression filter\n", px->id); |
| 914 | return -1; |
| 915 | } |
| 916 | } |
| 917 | |
Christopher Faulet | 443ea1a | 2016-02-04 13:40:26 +0100 | [diff] [blame] | 918 | fconf->id = http_comp_flt_id; |
| 919 | fconf->conf = NULL; |
| 920 | fconf->ops = &comp_ops; |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 921 | (*cur_arg)++; |
| 922 | |
| 923 | return 0; |
| 924 | } |
| 925 | |
| 926 | |
| 927 | int |
Christopher Faulet | c9df7f7 | 2018-12-10 16:14:04 +0100 | [diff] [blame] | 928 | check_implicit_http_comp_flt(struct proxy *proxy) |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 929 | { |
Christopher Faulet | 443ea1a | 2016-02-04 13:40:26 +0100 | [diff] [blame] | 930 | struct flt_conf *fconf; |
Christopher Faulet | 27d93c3 | 2018-12-15 22:32:02 +0100 | [diff] [blame] | 931 | int explicit = 0; |
| 932 | int comp = 0; |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 933 | int err = 0; |
| 934 | |
| 935 | if (proxy->comp == NULL) |
| 936 | goto end; |
Christopher Faulet | 443ea1a | 2016-02-04 13:40:26 +0100 | [diff] [blame] | 937 | if (!LIST_ISEMPTY(&proxy->filter_configs)) { |
| 938 | list_for_each_entry(fconf, &proxy->filter_configs, list) { |
| 939 | if (fconf->id == http_comp_flt_id) |
Christopher Faulet | 27d93c3 | 2018-12-15 22:32:02 +0100 | [diff] [blame] | 940 | comp = 1; |
| 941 | else if (fconf->id == cache_store_flt_id) { |
| 942 | if (comp) { |
| 943 | ha_alert("config: %s '%s': unable to enable the compression filter " |
| 944 | "before any cache filter.\n", |
| 945 | proxy_type_str(proxy), proxy->id); |
| 946 | err++; |
| 947 | goto end; |
| 948 | } |
| 949 | } |
Christopher Faulet | 78fbb9f | 2019-08-11 23:11:03 +0200 | [diff] [blame] | 950 | else if (fconf->id == fcgi_flt_id) |
| 951 | continue; |
Christopher Faulet | 27d93c3 | 2018-12-15 22:32:02 +0100 | [diff] [blame] | 952 | else |
| 953 | explicit = 1; |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 954 | } |
Christopher Faulet | 27d93c3 | 2018-12-15 22:32:02 +0100 | [diff] [blame] | 955 | } |
| 956 | if (comp) |
| 957 | goto end; |
| 958 | else if (explicit) { |
| 959 | ha_alert("config: %s '%s': require an explicit filter declaration to use " |
| 960 | "HTTP compression\n", proxy_type_str(proxy), proxy->id); |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 961 | err++; |
| 962 | goto end; |
| 963 | } |
| 964 | |
Christopher Faulet | 27d93c3 | 2018-12-15 22:32:02 +0100 | [diff] [blame] | 965 | /* Implicit declaration of the compression filter is always the last |
| 966 | * one */ |
Christopher Faulet | 443ea1a | 2016-02-04 13:40:26 +0100 | [diff] [blame] | 967 | fconf = calloc(1, sizeof(*fconf)); |
| 968 | if (!fconf) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 969 | ha_alert("config: %s '%s': out of memory\n", |
| 970 | proxy_type_str(proxy), proxy->id); |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 971 | err++; |
| 972 | goto end; |
| 973 | } |
Christopher Faulet | 443ea1a | 2016-02-04 13:40:26 +0100 | [diff] [blame] | 974 | fconf->id = http_comp_flt_id; |
| 975 | fconf->conf = NULL; |
| 976 | fconf->ops = &comp_ops; |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 977 | LIST_APPEND(&proxy->filter_configs, &fconf->list); |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 978 | end: |
| 979 | return err; |
| 980 | } |
| 981 | |
| 982 | /* |
| 983 | * boolean, returns true if compression is used (either gzip or deflate) in the |
| 984 | * response. |
| 985 | */ |
Christopher Faulet | 3d97c90 | 2015-12-09 14:59:38 +0100 | [diff] [blame] | 986 | static int |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 987 | smp_fetch_res_comp(const struct arg *args, struct sample *smp, const char *kw, |
| 988 | void *private) |
Christopher Faulet | 3d97c90 | 2015-12-09 14:59:38 +0100 | [diff] [blame] | 989 | { |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 990 | struct http_txn *txn = smp->strm ? smp->strm->txn : NULL; |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 991 | |
Christopher Faulet | 3d97c90 | 2015-12-09 14:59:38 +0100 | [diff] [blame] | 992 | smp->data.type = SMP_T_BOOL; |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 993 | smp->data.u.sint = (txn && (txn->rsp.flags & HTTP_MSGF_COMPRESSING)); |
Christopher Faulet | 3d97c90 | 2015-12-09 14:59:38 +0100 | [diff] [blame] | 994 | return 1; |
| 995 | } |
| 996 | |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 997 | /* |
| 998 | * string, returns algo |
| 999 | */ |
Christopher Faulet | 3d97c90 | 2015-12-09 14:59:38 +0100 | [diff] [blame] | 1000 | static int |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 1001 | smp_fetch_res_comp_algo(const struct arg *args, struct sample *smp, |
| 1002 | const char *kw, void *private) |
Christopher Faulet | 3d97c90 | 2015-12-09 14:59:38 +0100 | [diff] [blame] | 1003 | { |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 1004 | struct http_txn *txn = smp->strm ? smp->strm->txn : NULL; |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 1005 | struct filter *filter; |
| 1006 | struct comp_state *st; |
| 1007 | |
Christopher Faulet | 03d8553 | 2017-09-15 10:14:43 +0200 | [diff] [blame] | 1008 | if (!txn || !(txn->rsp.flags & HTTP_MSGF_COMPRESSING)) |
Christopher Faulet | 3d97c90 | 2015-12-09 14:59:38 +0100 | [diff] [blame] | 1009 | return 0; |
| 1010 | |
Christopher Faulet | fcf035c | 2015-12-03 11:48:03 +0100 | [diff] [blame] | 1011 | list_for_each_entry(filter, &strm_flt(smp->strm)->filters, list) { |
Christopher Faulet | 443ea1a | 2016-02-04 13:40:26 +0100 | [diff] [blame] | 1012 | if (FLT_ID(filter) != http_comp_flt_id) |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 1013 | continue; |
| 1014 | |
| 1015 | if (!(st = filter->ctx)) |
| 1016 | break; |
| 1017 | |
| 1018 | smp->data.type = SMP_T_STR; |
| 1019 | smp->flags = SMP_F_CONST; |
Olivier Houchard | dfc11da | 2023-04-05 16:25:57 +0200 | [diff] [blame] | 1020 | smp->data.u.str.area = st->comp_algo[COMP_DIR_RES]->cfg_name; |
| 1021 | smp->data.u.str.data = st->comp_algo[COMP_DIR_RES]->cfg_name_len; |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 1022 | return 1; |
| 1023 | } |
| 1024 | return 0; |
Christopher Faulet | 3d97c90 | 2015-12-09 14:59:38 +0100 | [diff] [blame] | 1025 | } |
| 1026 | |
| 1027 | /* Declare the config parser for "compression" keyword */ |
| 1028 | static struct cfg_kw_list cfg_kws = {ILH, { |
| 1029 | { CFG_LISTEN, "compression", parse_compression_options }, |
| 1030 | { 0, NULL, NULL }, |
| 1031 | } |
| 1032 | }; |
| 1033 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 1034 | INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws); |
| 1035 | |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 1036 | /* Declare the filter parser for "compression" keyword */ |
| 1037 | static struct flt_kw_list filter_kws = { "COMP", { }, { |
Thierry Fournier | 3610c39 | 2016-04-13 18:27:51 +0200 | [diff] [blame] | 1038 | { "compression", parse_http_comp_flt, NULL }, |
| 1039 | { NULL, NULL, NULL }, |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 1040 | } |
| 1041 | }; |
| 1042 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 1043 | INITCALL1(STG_REGISTER, flt_register_keywords, &filter_kws); |
| 1044 | |
Christopher Faulet | 3d97c90 | 2015-12-09 14:59:38 +0100 | [diff] [blame] | 1045 | /* Note: must not be declared <const> as its list will be overwritten */ |
| 1046 | static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, { |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 1047 | { "res.comp", smp_fetch_res_comp, 0, NULL, SMP_T_BOOL, SMP_USE_HRSHP }, |
| 1048 | { "res.comp_algo", smp_fetch_res_comp_algo, 0, NULL, SMP_T_STR, SMP_USE_HRSHP }, |
| 1049 | { /* END */ }, |
| 1050 | } |
| 1051 | }; |
Christopher Faulet | 3d97c90 | 2015-12-09 14:59:38 +0100 | [diff] [blame] | 1052 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 1053 | INITCALL1(STG_REGISTER, sample_register_fetches, &sample_fetch_keywords); |