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