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