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