Christopher Faulet | 3d97c90 | 2015-12-09 14:59:38 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Stream filters related variables and functions. |
| 3 | * |
| 4 | * Copyright (C) 2015 Qualys Inc., Christopher Faulet <cfaulet@qualys.com> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License |
| 8 | * as published by the Free Software Foundation; either version |
| 9 | * 2 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | */ |
| 12 | |
| 13 | #include <common/buffer.h> |
| 14 | #include <common/cfgparse.h> |
| 15 | #include <common/mini-clist.h> |
| 16 | #include <common/standard.h> |
| 17 | |
| 18 | #include <types/compression.h> |
| 19 | #include <types/filters.h> |
| 20 | #include <types/proto_http.h> |
| 21 | #include <types/proxy.h> |
| 22 | #include <types/sample.h> |
| 23 | |
| 24 | #include <proto/compression.h> |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 25 | #include <proto/filters.h> |
Christopher Faulet | 3d97c90 | 2015-12-09 14:59:38 +0100 | [diff] [blame] | 26 | #include <proto/hdr_idx.h> |
| 27 | #include <proto/proto_http.h> |
| 28 | #include <proto/sample.h> |
| 29 | #include <proto/stream.h> |
| 30 | |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 31 | static const char *http_comp_flt_id = "compression filter"; |
| 32 | |
| 33 | struct flt_ops comp_ops; |
| 34 | |
Christopher Faulet | a03d4ad | 2017-06-26 16:53:33 +0200 | [diff] [blame] | 35 | |
| 36 | /* Pools used to allocate comp_state structs */ |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 37 | static struct pool_head *pool_head_comp_state = NULL; |
Christopher Faulet | a03d4ad | 2017-06-26 16:53:33 +0200 | [diff] [blame] | 38 | |
Christopher Faulet | 8ca3b4b | 2017-07-25 11:07:15 +0200 | [diff] [blame] | 39 | static THREAD_LOCAL struct buffer *tmpbuf = &buf_empty; |
| 40 | static THREAD_LOCAL struct buffer *zbuf = &buf_empty; |
Willy Tarreau | d54a8ce | 2018-06-29 18:42:02 +0200 | [diff] [blame] | 41 | static THREAD_LOCAL unsigned int buf_output; |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 42 | |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 43 | struct comp_state { |
| 44 | struct comp_ctx *comp_ctx; /* compression context */ |
| 45 | struct comp_algo *comp_algo; /* compression algorithm if not NULL */ |
Christopher Faulet | b77c5c2 | 2015-12-07 16:48:42 +0100 | [diff] [blame] | 46 | int hdrs_len; |
| 47 | int tlrs_len; |
Christopher Faulet | 2fb2880 | 2015-12-01 10:40:57 +0100 | [diff] [blame] | 48 | int consumed; |
| 49 | int initialized; |
Christopher Faulet | b77c5c2 | 2015-12-07 16:48:42 +0100 | [diff] [blame] | 50 | int finished; |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 51 | }; |
| 52 | |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 53 | static int select_compression_request_header(struct comp_state *st, |
| 54 | struct stream *s, |
| 55 | struct http_msg *msg); |
| 56 | static int select_compression_response_header(struct comp_state *st, |
| 57 | struct stream *s, |
| 58 | struct http_msg *msg); |
| 59 | |
Willy Tarreau | d54a8ce | 2018-06-29 18:42:02 +0200 | [diff] [blame] | 60 | static int http_compression_buffer_init(struct channel *inc, struct buffer *out, unsigned int *out_len); |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 61 | static int http_compression_buffer_add_data(struct comp_state *st, |
| 62 | struct buffer *in, |
Willy Tarreau | d54a8ce | 2018-06-29 18:42:02 +0200 | [diff] [blame] | 63 | int in_out, |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 64 | struct buffer *out, int sz); |
| 65 | static int http_compression_buffer_end(struct comp_state *st, struct stream *s, |
Willy Tarreau | 4d45238 | 2018-06-06 07:15:47 +0200 | [diff] [blame] | 66 | struct channel *chn, struct buffer **out, |
Willy Tarreau | d54a8ce | 2018-06-29 18:42:02 +0200 | [diff] [blame] | 67 | unsigned int *out_len, int end); |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 68 | |
| 69 | /***********************************************************************/ |
| 70 | static int |
Christopher Faulet | 8ca3b4b | 2017-07-25 11:07:15 +0200 | [diff] [blame] | 71 | comp_flt_init_per_thread(struct proxy *px, struct flt_conf *fconf) |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 72 | { |
Christopher Faulet | b77c5c2 | 2015-12-07 16:48:42 +0100 | [diff] [blame] | 73 | if (!tmpbuf->size && b_alloc(&tmpbuf) == NULL) |
| 74 | return -1; |
| 75 | if (!zbuf->size && b_alloc(&zbuf) == NULL) |
| 76 | return -1; |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 77 | return 0; |
| 78 | } |
| 79 | |
| 80 | static void |
Christopher Faulet | 8ca3b4b | 2017-07-25 11:07:15 +0200 | [diff] [blame] | 81 | comp_flt_deinit_per_thread(struct proxy *px, struct flt_conf *fconf) |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 82 | { |
| 83 | if (tmpbuf->size) |
| 84 | b_free(&tmpbuf); |
Christopher Faulet | b77c5c2 | 2015-12-07 16:48:42 +0100 | [diff] [blame] | 85 | if (zbuf->size) |
| 86 | b_free(&zbuf); |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | static int |
| 90 | comp_start_analyze(struct stream *s, struct filter *filter, struct channel *chn) |
| 91 | { |
Christopher Faulet | 8ca3b4b | 2017-07-25 11:07:15 +0200 | [diff] [blame] | 92 | |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 93 | if (filter->ctx == NULL) { |
| 94 | struct comp_state *st; |
| 95 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 96 | st = pool_alloc_dirty(pool_head_comp_state); |
Christopher Faulet | a03d4ad | 2017-06-26 16:53:33 +0200 | [diff] [blame] | 97 | if (st == NULL) |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 98 | return -1; |
| 99 | |
Christopher Faulet | 2fb2880 | 2015-12-01 10:40:57 +0100 | [diff] [blame] | 100 | st->comp_algo = NULL; |
| 101 | st->comp_ctx = NULL; |
Christopher Faulet | b77c5c2 | 2015-12-07 16:48:42 +0100 | [diff] [blame] | 102 | st->hdrs_len = 0; |
| 103 | st->tlrs_len = 0; |
Christopher Faulet | 2fb2880 | 2015-12-01 10:40:57 +0100 | [diff] [blame] | 104 | st->consumed = 0; |
| 105 | st->initialized = 0; |
Christopher Faulet | b77c5c2 | 2015-12-07 16:48:42 +0100 | [diff] [blame] | 106 | st->finished = 0; |
Christopher Faulet | 2fb2880 | 2015-12-01 10:40:57 +0100 | [diff] [blame] | 107 | filter->ctx = st; |
Christopher Faulet | 3dc860d | 2017-09-15 11:39:36 +0200 | [diff] [blame] | 108 | |
| 109 | /* Register post-analyzer on AN_RES_WAIT_HTTP because we need to |
| 110 | * analyze response headers before http-response rules execution |
| 111 | * to be sure we can use res.comp and res.comp_algo sample |
| 112 | * fetches */ |
| 113 | filter->post_analyzers |= AN_RES_WAIT_HTTP; |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 114 | } |
| 115 | return 1; |
| 116 | } |
| 117 | |
| 118 | static int |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 119 | comp_end_analyze(struct stream *s, struct filter *filter, struct channel *chn) |
| 120 | { |
| 121 | struct comp_state *st = filter->ctx; |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 122 | |
Christopher Faulet | d60b3cf | 2017-06-26 11:47:13 +0200 | [diff] [blame] | 123 | if (!st) |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 124 | goto end; |
| 125 | |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 126 | /* release any possible compression context */ |
Christopher Faulet | d60b3cf | 2017-06-26 11:47:13 +0200 | [diff] [blame] | 127 | if (st->comp_algo) |
| 128 | st->comp_algo->end(&st->comp_ctx); |
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; |
| 131 | end: |
| 132 | return 1; |
| 133 | } |
| 134 | |
| 135 | static int |
Christopher Faulet | 1339d74 | 2016-05-11 16:48:33 +0200 | [diff] [blame] | 136 | comp_http_headers(struct stream *s, struct filter *filter, struct http_msg *msg) |
| 137 | { |
| 138 | struct comp_state *st = filter->ctx; |
| 139 | |
| 140 | if (!strm_fe(s)->comp && !s->be->comp) |
| 141 | goto end; |
| 142 | |
| 143 | if (!(msg->chn->flags & CF_ISRESP)) |
| 144 | select_compression_request_header(st, s, msg); |
| 145 | else { |
Christopher Faulet | 3dc860d | 2017-09-15 11:39:36 +0200 | [diff] [blame] | 146 | /* Response headers have already been checked in |
| 147 | * comp_http_post_analyze callback. */ |
Christopher Faulet | 1339d74 | 2016-05-11 16:48:33 +0200 | [diff] [blame] | 148 | if (st->comp_algo) { |
| 149 | register_data_filter(s, msg->chn, filter); |
| 150 | st->hdrs_len = s->txn->rsp.sov; |
| 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 | 2fb2880 | 2015-12-01 10:40:57 +0100 | [diff] [blame] | 179 | comp_http_data(struct stream *s, struct filter *filter, struct http_msg *msg) |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 180 | { |
| 181 | struct comp_state *st = filter->ctx; |
Willy Tarreau | bcbd393 | 2018-06-06 07:13:22 +0200 | [diff] [blame] | 182 | struct channel *chn = msg->chn; |
Christopher Faulet | 3e7bc67 | 2015-12-07 13:39:08 +0100 | [diff] [blame] | 183 | unsigned int *nxt = &flt_rsp_nxt(filter); |
Christopher Faulet | 2fb2880 | 2015-12-01 10:40:57 +0100 | [diff] [blame] | 184 | unsigned int len; |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 185 | int ret; |
| 186 | |
Olivier Houchard | 0b66284 | 2018-06-29 18:16:31 +0200 | [diff] [blame] | 187 | len = MIN(msg->chunk_len + msg->next, ci_data(chn)) - *nxt; |
Christopher Faulet | 2fb2880 | 2015-12-01 10:40:57 +0100 | [diff] [blame] | 188 | if (!len) |
| 189 | return len; |
| 190 | |
| 191 | if (!st->initialized) { |
Christopher Faulet | b77c5c2 | 2015-12-07 16:48:42 +0100 | [diff] [blame] | 192 | unsigned int fwd = flt_rsp_fwd(filter) + st->hdrs_len; |
Christopher Faulet | 3e7bc67 | 2015-12-07 13:39:08 +0100 | [diff] [blame] | 193 | |
Christopher Faulet | b77c5c2 | 2015-12-07 16:48:42 +0100 | [diff] [blame] | 194 | b_reset(tmpbuf); |
Willy Tarreau | bcbd393 | 2018-06-06 07:13:22 +0200 | [diff] [blame] | 195 | c_adv(chn, fwd); |
Willy Tarreau | d54a8ce | 2018-06-29 18:42:02 +0200 | [diff] [blame] | 196 | ret = http_compression_buffer_init(chn, zbuf, &buf_output); |
Willy Tarreau | bcbd393 | 2018-06-06 07:13:22 +0200 | [diff] [blame] | 197 | c_rew(chn, fwd); |
Christopher Faulet | 2fb2880 | 2015-12-01 10:40:57 +0100 | [diff] [blame] | 198 | if (ret < 0) { |
| 199 | msg->chn->flags |= CF_WAKE_WRITE; |
| 200 | return 0; |
| 201 | } |
| 202 | } |
Christopher Faulet | b77c5c2 | 2015-12-07 16:48:42 +0100 | [diff] [blame] | 203 | |
| 204 | if (msg->flags & HTTP_MSGF_TE_CHNK) { |
Christopher Faulet | 06ecf3a | 2016-09-22 15:31:43 +0200 | [diff] [blame] | 205 | int block; |
Christopher Faulet | b77c5c2 | 2015-12-07 16:48:42 +0100 | [diff] [blame] | 206 | |
Willy Tarreau | eac5259 | 2018-06-15 13:59:36 +0200 | [diff] [blame] | 207 | len = MIN(b_room(tmpbuf), len); |
Christopher Faulet | 06ecf3a | 2016-09-22 15:31:43 +0200 | [diff] [blame] | 208 | |
Willy Tarreau | bcbd393 | 2018-06-06 07:13:22 +0200 | [diff] [blame] | 209 | c_adv(chn, *nxt); |
Willy Tarreau | 7194d3c | 2018-06-06 16:55:45 +0200 | [diff] [blame] | 210 | block = ci_contig_data(chn); |
Willy Tarreau | 8f9c72d | 2018-06-07 18:46:28 +0200 | [diff] [blame] | 211 | memcpy(b_tail(tmpbuf), ci_head(chn), block); |
Christopher Faulet | 06ecf3a | 2016-09-22 15:31:43 +0200 | [diff] [blame] | 212 | if (len > block) |
Olivier Houchard | 0b66284 | 2018-06-29 18:16:31 +0200 | [diff] [blame] | 213 | memcpy(b_tail(tmpbuf)+block, b_orig(chn->buf), len-block); |
Willy Tarreau | bcbd393 | 2018-06-06 07:13:22 +0200 | [diff] [blame] | 214 | c_rew(chn, *nxt); |
Christopher Faulet | 06ecf3a | 2016-09-22 15:31:43 +0200 | [diff] [blame] | 215 | |
Olivier Houchard | acd1403 | 2018-06-28 18:17:23 +0200 | [diff] [blame] | 216 | b_add(tmpbuf, len); |
Christopher Faulet | b77c5c2 | 2015-12-07 16:48:42 +0100 | [diff] [blame] | 217 | ret = len; |
| 218 | } |
| 219 | else { |
Willy Tarreau | bcbd393 | 2018-06-06 07:13:22 +0200 | [diff] [blame] | 220 | c_adv(chn, *nxt); |
Willy Tarreau | d54a8ce | 2018-06-29 18:42:02 +0200 | [diff] [blame] | 221 | ret = http_compression_buffer_add_data(st, chn->buf, co_data(chn), zbuf, len); |
Willy Tarreau | bcbd393 | 2018-06-06 07:13:22 +0200 | [diff] [blame] | 222 | c_rew(chn, *nxt); |
Christopher Faulet | b77c5c2 | 2015-12-07 16:48:42 +0100 | [diff] [blame] | 223 | if (ret < 0) |
| 224 | return ret; |
| 225 | } |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 226 | |
Christopher Faulet | 2fb2880 | 2015-12-01 10:40:57 +0100 | [diff] [blame] | 227 | st->initialized = 1; |
| 228 | msg->next += ret; |
| 229 | msg->chunk_len -= ret; |
Christopher Faulet | 3e7bc67 | 2015-12-07 13:39:08 +0100 | [diff] [blame] | 230 | *nxt = msg->next; |
Christopher Faulet | 2fb2880 | 2015-12-01 10:40:57 +0100 | [diff] [blame] | 231 | return 0; |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | static int |
Christopher Faulet | 2fb2880 | 2015-12-01 10:40:57 +0100 | [diff] [blame] | 235 | comp_http_chunk_trailers(struct stream *s, struct filter *filter, |
| 236 | struct http_msg *msg) |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 237 | { |
| 238 | struct comp_state *st = filter->ctx; |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 239 | |
Christopher Faulet | b77c5c2 | 2015-12-07 16:48:42 +0100 | [diff] [blame] | 240 | if (!st->initialized) { |
| 241 | if (!st->finished) { |
Willy Tarreau | bcbd393 | 2018-06-06 07:13:22 +0200 | [diff] [blame] | 242 | struct channel *chn = msg->chn; |
Christopher Faulet | b77c5c2 | 2015-12-07 16:48:42 +0100 | [diff] [blame] | 243 | unsigned int fwd = flt_rsp_fwd(filter) + st->hdrs_len; |
Christopher Faulet | 2fb2880 | 2015-12-01 10:40:57 +0100 | [diff] [blame] | 244 | |
Christopher Faulet | b77c5c2 | 2015-12-07 16:48:42 +0100 | [diff] [blame] | 245 | b_reset(tmpbuf); |
Willy Tarreau | bcbd393 | 2018-06-06 07:13:22 +0200 | [diff] [blame] | 246 | c_adv(chn, fwd); |
Willy Tarreau | d54a8ce | 2018-06-29 18:42:02 +0200 | [diff] [blame] | 247 | http_compression_buffer_init(chn, zbuf, &buf_output); |
Willy Tarreau | bcbd393 | 2018-06-06 07:13:22 +0200 | [diff] [blame] | 248 | c_rew(chn, fwd); |
Christopher Faulet | b77c5c2 | 2015-12-07 16:48:42 +0100 | [diff] [blame] | 249 | st->initialized = 1; |
| 250 | } |
| 251 | } |
| 252 | st->tlrs_len = msg->sol; |
Christopher Faulet | 2fb2880 | 2015-12-01 10:40:57 +0100 | [diff] [blame] | 253 | return 1; |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 254 | } |
| 255 | |
Christopher Faulet | 2fb2880 | 2015-12-01 10:40:57 +0100 | [diff] [blame] | 256 | |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 257 | static int |
| 258 | comp_http_forward_data(struct stream *s, struct filter *filter, |
| 259 | struct http_msg *msg, unsigned int len) |
| 260 | { |
| 261 | struct comp_state *st = filter->ctx; |
Christopher Faulet | 2fb2880 | 2015-12-01 10:40:57 +0100 | [diff] [blame] | 262 | int ret; |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 263 | |
Christopher Faulet | 2fb2880 | 2015-12-01 10:40:57 +0100 | [diff] [blame] | 264 | /* To work, previous filters MUST forward all data */ |
Christopher Faulet | b77c5c2 | 2015-12-07 16:48:42 +0100 | [diff] [blame] | 265 | if (flt_rsp_fwd(filter) + len != flt_rsp_nxt(filter)) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 266 | ha_warning("HTTP compression failed: unexpected behavior of previous filters\n"); |
Christopher Faulet | 2fb2880 | 2015-12-01 10:40:57 +0100 | [diff] [blame] | 267 | return -1; |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 268 | } |
| 269 | |
Christopher Faulet | 2fb2880 | 2015-12-01 10:40:57 +0100 | [diff] [blame] | 270 | if (!st->initialized) { |
Christopher Faulet | b77c5c2 | 2015-12-07 16:48:42 +0100 | [diff] [blame] | 271 | if (!len) { |
| 272 | /* Nothing to foward */ |
| 273 | ret = len; |
| 274 | } |
| 275 | else if (st->hdrs_len > len) { |
| 276 | /* Forward part of headers */ |
| 277 | ret = len; |
| 278 | st->hdrs_len -= len; |
| 279 | } |
| 280 | else if (st->hdrs_len > 0) { |
| 281 | /* Forward remaining headers */ |
| 282 | ret = st->hdrs_len; |
| 283 | st->hdrs_len = 0; |
| 284 | } |
| 285 | else if (msg->msg_state < HTTP_MSG_TRAILERS) { |
| 286 | /* Do not forward anything for now. This only happens |
| 287 | * with chunk-encoded responses. Waiting data are part |
| 288 | * of the chunk envelope (the chunk size or the chunk |
| 289 | * CRLF). These data will be skipped during the |
| 290 | * compression. */ |
| 291 | ret = 0; |
| 292 | } |
| 293 | else { |
| 294 | /* Forward trailers data */ |
| 295 | ret = len; |
| 296 | } |
Christopher Faulet | 2fb2880 | 2015-12-01 10:40:57 +0100 | [diff] [blame] | 297 | return ret; |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 298 | } |
| 299 | |
Christopher Faulet | b77c5c2 | 2015-12-07 16:48:42 +0100 | [diff] [blame] | 300 | if (msg->flags & HTTP_MSGF_TE_CHNK) { |
Willy Tarreau | d54a8ce | 2018-06-29 18:42:02 +0200 | [diff] [blame] | 301 | ret = http_compression_buffer_add_data(st, tmpbuf, 0, |
| 302 | zbuf, b_data(tmpbuf)); |
| 303 | if (ret != b_data(tmpbuf)) { |
Willy Tarreau | 506a29a | 2018-07-18 10:07:58 +0200 | [diff] [blame] | 304 | ha_warning("HTTP compression failed: Must consume %u bytes but only %d bytes consumed\n", |
Willy Tarreau | d54a8ce | 2018-06-29 18:42:02 +0200 | [diff] [blame] | 305 | (unsigned int)b_data(tmpbuf), ret); |
Christopher Faulet | b77c5c2 | 2015-12-07 16:48:42 +0100 | [diff] [blame] | 306 | return -1; |
| 307 | } |
| 308 | } |
| 309 | |
| 310 | st->consumed = len - st->hdrs_len - st->tlrs_len; |
Willy Tarreau | bcbd393 | 2018-06-06 07:13:22 +0200 | [diff] [blame] | 311 | c_adv(msg->chn, flt_rsp_fwd(filter) + st->hdrs_len); |
Willy Tarreau | d54a8ce | 2018-06-29 18:42:02 +0200 | [diff] [blame] | 312 | ret = http_compression_buffer_end(st, s, msg->chn, &zbuf, &buf_output, msg->msg_state >= HTTP_MSG_TRAILERS); |
Willy Tarreau | bcbd393 | 2018-06-06 07:13:22 +0200 | [diff] [blame] | 313 | c_rew(msg->chn, flt_rsp_fwd(filter) + st->hdrs_len); |
Christopher Faulet | 2fb2880 | 2015-12-01 10:40:57 +0100 | [diff] [blame] | 314 | if (ret < 0) |
| 315 | return ret; |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 316 | |
Christopher Faulet | b77c5c2 | 2015-12-07 16:48:42 +0100 | [diff] [blame] | 317 | flt_change_forward_size(filter, msg->chn, ret - st->consumed); |
| 318 | msg->next += (ret - st->consumed); |
| 319 | ret += st->hdrs_len + st->tlrs_len; |
| 320 | |
Christopher Faulet | 2fb2880 | 2015-12-01 10:40:57 +0100 | [diff] [blame] | 321 | st->initialized = 0; |
Christopher Faulet | b77c5c2 | 2015-12-07 16:48:42 +0100 | [diff] [blame] | 322 | st->finished = (msg->msg_state >= HTTP_MSG_TRAILERS); |
| 323 | st->hdrs_len = 0; |
| 324 | st->tlrs_len = 0; |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 325 | return ret; |
| 326 | } |
Christopher Faulet | 3d97c90 | 2015-12-09 14:59:38 +0100 | [diff] [blame] | 327 | |
Christopher Faulet | d60b3cf | 2017-06-26 11:47:13 +0200 | [diff] [blame] | 328 | static int |
| 329 | comp_http_end(struct stream *s, struct filter *filter, |
| 330 | struct http_msg *msg) |
| 331 | { |
| 332 | struct comp_state *st = filter->ctx; |
| 333 | |
| 334 | if (!(msg->chn->flags & CF_ISRESP) || !st || !st->comp_algo) |
| 335 | goto end; |
| 336 | |
| 337 | if (strm_fe(s)->mode == PR_MODE_HTTP) |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 338 | HA_ATOMIC_ADD(&strm_fe(s)->fe_counters.p.http.comp_rsp, 1); |
Christopher Faulet | d60b3cf | 2017-06-26 11:47:13 +0200 | [diff] [blame] | 339 | if ((s->flags & SF_BE_ASSIGNED) && (s->be->mode == PR_MODE_HTTP)) |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 340 | HA_ATOMIC_ADD(&s->be->be_counters.p.http.comp_rsp, 1); |
Christopher Faulet | d60b3cf | 2017-06-26 11:47:13 +0200 | [diff] [blame] | 341 | end: |
| 342 | return 1; |
| 343 | } |
Christopher Faulet | 3d97c90 | 2015-12-09 14:59:38 +0100 | [diff] [blame] | 344 | /***********************************************************************/ |
| 345 | /* |
| 346 | * Selects a compression algorithm depending on the client request. |
| 347 | */ |
| 348 | int |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 349 | select_compression_request_header(struct comp_state *st, struct stream *s, |
| 350 | struct http_msg *msg) |
Christopher Faulet | 3d97c90 | 2015-12-09 14:59:38 +0100 | [diff] [blame] | 351 | { |
| 352 | struct http_txn *txn = s->txn; |
Olivier Houchard | 0b66284 | 2018-06-29 18:16:31 +0200 | [diff] [blame] | 353 | struct channel *req = msg->chn; |
Christopher Faulet | 3d97c90 | 2015-12-09 14:59:38 +0100 | [diff] [blame] | 354 | struct hdr_ctx ctx; |
| 355 | struct comp_algo *comp_algo = NULL; |
| 356 | struct comp_algo *comp_algo_back = NULL; |
| 357 | |
| 358 | /* Disable compression for older user agents announcing themselves as "Mozilla/4" |
| 359 | * unless they are known good (MSIE 6 with XP SP2, or MSIE 7 and later). |
| 360 | * See http://zoompf.com/2012/02/lose-the-wait-http-compression for more details. |
| 361 | */ |
| 362 | ctx.idx = 0; |
Olivier Houchard | 0b66284 | 2018-06-29 18:16:31 +0200 | [diff] [blame] | 363 | if (http_find_header2("User-Agent", 10, ci_head(req), &txn->hdr_idx, &ctx) && |
Christopher Faulet | 3d97c90 | 2015-12-09 14:59:38 +0100 | [diff] [blame] | 364 | ctx.vlen >= 9 && |
| 365 | memcmp(ctx.line + ctx.val, "Mozilla/4", 9) == 0 && |
| 366 | (ctx.vlen < 31 || |
| 367 | memcmp(ctx.line + ctx.val + 25, "MSIE ", 5) != 0 || |
| 368 | ctx.line[ctx.val + 30] < '6' || |
| 369 | (ctx.line[ctx.val + 30] == '6' && |
| 370 | (ctx.vlen < 54 || memcmp(ctx.line + 51, "SV1", 3) != 0)))) { |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 371 | st->comp_algo = NULL; |
Christopher Faulet | 3d97c90 | 2015-12-09 14:59:38 +0100 | [diff] [blame] | 372 | return 0; |
| 373 | } |
| 374 | |
| 375 | /* search for the algo in the backend in priority or the frontend */ |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 376 | if ((s->be->comp && (comp_algo_back = s->be->comp->algos)) || |
| 377 | (strm_fe(s)->comp && (comp_algo_back = strm_fe(s)->comp->algos))) { |
Christopher Faulet | 3d97c90 | 2015-12-09 14:59:38 +0100 | [diff] [blame] | 378 | int best_q = 0; |
| 379 | |
| 380 | ctx.idx = 0; |
Olivier Houchard | 0b66284 | 2018-06-29 18:16:31 +0200 | [diff] [blame] | 381 | while (http_find_header2("Accept-Encoding", 15, ci_head(req), &txn->hdr_idx, &ctx)) { |
Christopher Faulet | 3d97c90 | 2015-12-09 14:59:38 +0100 | [diff] [blame] | 382 | const char *qval; |
| 383 | int q; |
| 384 | int toklen; |
| 385 | |
| 386 | /* try to isolate the token from the optional q-value */ |
| 387 | toklen = 0; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 388 | while (toklen < ctx.vlen && HTTP_IS_TOKEN(*(ctx.line + ctx.val + toklen))) |
Christopher Faulet | 3d97c90 | 2015-12-09 14:59:38 +0100 | [diff] [blame] | 389 | toklen++; |
| 390 | |
| 391 | qval = ctx.line + ctx.val + toklen; |
| 392 | while (1) { |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 393 | while (qval < ctx.line + ctx.val + ctx.vlen && HTTP_IS_LWS(*qval)) |
Christopher Faulet | 3d97c90 | 2015-12-09 14:59:38 +0100 | [diff] [blame] | 394 | qval++; |
| 395 | |
| 396 | if (qval >= ctx.line + ctx.val + ctx.vlen || *qval != ';') { |
| 397 | qval = NULL; |
| 398 | break; |
| 399 | } |
| 400 | qval++; |
| 401 | |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 402 | while (qval < ctx.line + ctx.val + ctx.vlen && HTTP_IS_LWS(*qval)) |
Christopher Faulet | 3d97c90 | 2015-12-09 14:59:38 +0100 | [diff] [blame] | 403 | qval++; |
| 404 | |
| 405 | if (qval >= ctx.line + ctx.val + ctx.vlen) { |
| 406 | qval = NULL; |
| 407 | break; |
| 408 | } |
| 409 | if (strncmp(qval, "q=", MIN(ctx.line + ctx.val + ctx.vlen - qval, 2)) == 0) |
| 410 | break; |
| 411 | |
| 412 | while (qval < ctx.line + ctx.val + ctx.vlen && *qval != ';') |
| 413 | qval++; |
| 414 | } |
| 415 | |
| 416 | /* here we have qval pointing to the first "q=" attribute or NULL if not found */ |
| 417 | q = qval ? parse_qvalue(qval + 2, NULL) : 1000; |
| 418 | |
| 419 | if (q <= best_q) |
| 420 | continue; |
| 421 | |
| 422 | for (comp_algo = comp_algo_back; comp_algo; comp_algo = comp_algo->next) { |
| 423 | if (*(ctx.line + ctx.val) == '*' || |
| 424 | word_match(ctx.line + ctx.val, toklen, comp_algo->ua_name, comp_algo->ua_name_len)) { |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 425 | st->comp_algo = comp_algo; |
Christopher Faulet | 3d97c90 | 2015-12-09 14:59:38 +0100 | [diff] [blame] | 426 | best_q = q; |
| 427 | break; |
| 428 | } |
| 429 | } |
| 430 | } |
| 431 | } |
| 432 | |
| 433 | /* remove all occurrences of the header when "compression offload" is set */ |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 434 | if (st->comp_algo) { |
| 435 | if ((s->be->comp && s->be->comp->offload) || |
| 436 | (strm_fe(s)->comp && strm_fe(s)->comp->offload)) { |
Christopher Faulet | 3d97c90 | 2015-12-09 14:59:38 +0100 | [diff] [blame] | 437 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
| 438 | ctx.idx = 0; |
Olivier Houchard | 0b66284 | 2018-06-29 18:16:31 +0200 | [diff] [blame] | 439 | while (http_find_header2("Accept-Encoding", 15, ci_head(req), &txn->hdr_idx, &ctx)) { |
Christopher Faulet | 3d97c90 | 2015-12-09 14:59:38 +0100 | [diff] [blame] | 440 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
| 441 | } |
| 442 | } |
| 443 | return 1; |
| 444 | } |
| 445 | |
| 446 | /* identity is implicit does not require headers */ |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 447 | if ((s->be->comp && (comp_algo_back = s->be->comp->algos)) || |
| 448 | (strm_fe(s)->comp && (comp_algo_back = strm_fe(s)->comp->algos))) { |
Christopher Faulet | 3d97c90 | 2015-12-09 14:59:38 +0100 | [diff] [blame] | 449 | for (comp_algo = comp_algo_back; comp_algo; comp_algo = comp_algo->next) { |
| 450 | 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] | 451 | st->comp_algo = comp_algo; |
Christopher Faulet | 3d97c90 | 2015-12-09 14:59:38 +0100 | [diff] [blame] | 452 | return 1; |
| 453 | } |
| 454 | } |
| 455 | } |
| 456 | |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 457 | st->comp_algo = NULL; |
Christopher Faulet | 3d97c90 | 2015-12-09 14:59:38 +0100 | [diff] [blame] | 458 | return 0; |
| 459 | } |
| 460 | |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 461 | |
Christopher Faulet | 3d97c90 | 2015-12-09 14:59:38 +0100 | [diff] [blame] | 462 | /* |
| 463 | * Selects a comression algorithm depending of the server response. |
| 464 | */ |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 465 | static int |
| 466 | select_compression_response_header(struct comp_state *st, struct stream *s, struct http_msg *msg) |
Christopher Faulet | 3d97c90 | 2015-12-09 14:59:38 +0100 | [diff] [blame] | 467 | { |
| 468 | struct http_txn *txn = s->txn; |
Olivier Houchard | 0b66284 | 2018-06-29 18:16:31 +0200 | [diff] [blame] | 469 | struct channel *c = msg->chn; |
Christopher Faulet | 3d97c90 | 2015-12-09 14:59:38 +0100 | [diff] [blame] | 470 | struct hdr_ctx ctx; |
| 471 | struct comp_type *comp_type; |
| 472 | |
| 473 | /* no common compression algorithm was found in request header */ |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 474 | if (st->comp_algo == NULL) |
Christopher Faulet | 3d97c90 | 2015-12-09 14:59:38 +0100 | [diff] [blame] | 475 | goto fail; |
| 476 | |
| 477 | /* HTTP < 1.1 should not be compressed */ |
| 478 | if (!(msg->flags & HTTP_MSGF_VER_11) || !(txn->req.flags & HTTP_MSGF_VER_11)) |
| 479 | goto fail; |
| 480 | |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 481 | if (txn->meth == HTTP_METH_HEAD) |
| 482 | goto fail; |
| 483 | |
Christopher Faulet | 3d97c90 | 2015-12-09 14:59:38 +0100 | [diff] [blame] | 484 | /* compress 200,201,202,203 responses only */ |
| 485 | if ((txn->status != 200) && |
| 486 | (txn->status != 201) && |
| 487 | (txn->status != 202) && |
| 488 | (txn->status != 203)) |
| 489 | goto fail; |
| 490 | |
| 491 | |
| 492 | /* Content-Length is null */ |
| 493 | if (!(msg->flags & HTTP_MSGF_TE_CHNK) && msg->body_len == 0) |
| 494 | goto fail; |
| 495 | |
| 496 | /* content is already compressed */ |
| 497 | ctx.idx = 0; |
Olivier Houchard | 0b66284 | 2018-06-29 18:16:31 +0200 | [diff] [blame] | 498 | if (http_find_header2("Content-Encoding", 16, ci_head(c), &txn->hdr_idx, &ctx)) |
Christopher Faulet | 3d97c90 | 2015-12-09 14:59:38 +0100 | [diff] [blame] | 499 | goto fail; |
| 500 | |
| 501 | /* no compression when Cache-Control: no-transform is present in the message */ |
| 502 | ctx.idx = 0; |
Olivier Houchard | 0b66284 | 2018-06-29 18:16:31 +0200 | [diff] [blame] | 503 | while (http_find_header2("Cache-Control", 13, ci_head(c), &txn->hdr_idx, &ctx)) { |
Christopher Faulet | 3d97c90 | 2015-12-09 14:59:38 +0100 | [diff] [blame] | 504 | if (word_match(ctx.line + ctx.val, ctx.vlen, "no-transform", 12)) |
| 505 | goto fail; |
| 506 | } |
| 507 | |
| 508 | comp_type = NULL; |
| 509 | |
| 510 | /* we don't want to compress multipart content-types, nor content-types that are |
| 511 | * not listed in the "compression type" directive if any. If no content-type was |
| 512 | * found but configuration requires one, we don't compress either. Backend has |
| 513 | * the priority. |
| 514 | */ |
| 515 | ctx.idx = 0; |
Olivier Houchard | 0b66284 | 2018-06-29 18:16:31 +0200 | [diff] [blame] | 516 | if (http_find_header2("Content-Type", 12, ci_head(c), &txn->hdr_idx, &ctx)) { |
Christopher Faulet | 3d97c90 | 2015-12-09 14:59:38 +0100 | [diff] [blame] | 517 | if (ctx.vlen >= 9 && strncasecmp("multipart", ctx.line+ctx.val, 9) == 0) |
| 518 | goto fail; |
| 519 | |
| 520 | if ((s->be->comp && (comp_type = s->be->comp->types)) || |
| 521 | (strm_fe(s)->comp && (comp_type = strm_fe(s)->comp->types))) { |
| 522 | for (; comp_type; comp_type = comp_type->next) { |
| 523 | if (ctx.vlen >= comp_type->name_len && |
| 524 | strncasecmp(ctx.line+ctx.val, comp_type->name, comp_type->name_len) == 0) |
| 525 | /* this Content-Type should be compressed */ |
| 526 | break; |
| 527 | } |
| 528 | /* this Content-Type should not be compressed */ |
| 529 | if (comp_type == NULL) |
| 530 | goto fail; |
| 531 | } |
| 532 | } |
| 533 | else { /* no content-type header */ |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 534 | if ((s->be->comp && s->be->comp->types) || |
| 535 | (strm_fe(s)->comp && strm_fe(s)->comp->types)) |
Christopher Faulet | 3d97c90 | 2015-12-09 14:59:38 +0100 | [diff] [blame] | 536 | goto fail; /* a content-type was required */ |
| 537 | } |
| 538 | |
| 539 | /* limit compression rate */ |
| 540 | if (global.comp_rate_lim > 0) |
| 541 | if (read_freq_ctr(&global.comp_bps_in) > global.comp_rate_lim) |
| 542 | goto fail; |
| 543 | |
| 544 | /* limit cpu usage */ |
| 545 | if (idle_pct < compress_min_idle) |
| 546 | goto fail; |
| 547 | |
| 548 | /* initialize compression */ |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 549 | if (st->comp_algo->init(&st->comp_ctx, global.tune.comp_maxlevel) < 0) |
Christopher Faulet | 3d97c90 | 2015-12-09 14:59:38 +0100 | [diff] [blame] | 550 | goto fail; |
| 551 | |
Christopher Faulet | 3d97c90 | 2015-12-09 14:59:38 +0100 | [diff] [blame] | 552 | /* remove Content-Length header */ |
| 553 | ctx.idx = 0; |
Olivier Houchard | 0b66284 | 2018-06-29 18:16:31 +0200 | [diff] [blame] | 554 | if ((msg->flags & HTTP_MSGF_CNT_LEN) && http_find_header2("Content-Length", 14, ci_head(c), &txn->hdr_idx, &ctx)) |
Christopher Faulet | 3d97c90 | 2015-12-09 14:59:38 +0100 | [diff] [blame] | 555 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
| 556 | |
| 557 | /* add Transfer-Encoding header */ |
| 558 | if (!(msg->flags & HTTP_MSGF_TE_CHNK)) |
| 559 | http_header_add_tail2(&txn->rsp, &txn->hdr_idx, "Transfer-Encoding: chunked", 26); |
| 560 | |
| 561 | /* |
| 562 | * Add Content-Encoding header when it's not identity encoding. |
| 563 | * RFC 2616 : Identity encoding: This content-coding is used only in the |
| 564 | * Accept-Encoding header, and SHOULD NOT be used in the Content-Encoding |
| 565 | * header. |
| 566 | */ |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 567 | if (st->comp_algo->cfg_name_len != 8 || memcmp(st->comp_algo->cfg_name, "identity", 8) != 0) { |
Christopher Faulet | 3d97c90 | 2015-12-09 14:59:38 +0100 | [diff] [blame] | 568 | trash.len = 18; |
| 569 | memcpy(trash.str, "Content-Encoding: ", trash.len); |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 570 | memcpy(trash.str + trash.len, st->comp_algo->ua_name, st->comp_algo->ua_name_len); |
| 571 | trash.len += st->comp_algo->ua_name_len; |
Christopher Faulet | 3d97c90 | 2015-12-09 14:59:38 +0100 | [diff] [blame] | 572 | trash.str[trash.len] = '\0'; |
| 573 | http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len); |
| 574 | } |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 575 | msg->flags |= HTTP_MSGF_COMPRESSING; |
Christopher Faulet | 3d97c90 | 2015-12-09 14:59:38 +0100 | [diff] [blame] | 576 | return 1; |
| 577 | |
| 578 | fail: |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 579 | st->comp_algo = NULL; |
Christopher Faulet | 3d97c90 | 2015-12-09 14:59:38 +0100 | [diff] [blame] | 580 | return 0; |
| 581 | } |
| 582 | |
| 583 | /***********************************************************************/ |
| 584 | /* emit the chunksize followed by a CRLF on the output and return the number of |
| 585 | * bytes written. It goes backwards and starts with the byte before <end>. It |
| 586 | * returns the number of bytes written which will not exceed 10 (8 digits, CR, |
| 587 | * and LF). The caller is responsible for ensuring there is enough room left in |
| 588 | * the output buffer for the string. |
| 589 | */ |
| 590 | static int |
| 591 | http_emit_chunk_size(char *end, unsigned int chksz) |
| 592 | { |
| 593 | char *beg = end; |
| 594 | |
| 595 | *--beg = '\n'; |
| 596 | *--beg = '\r'; |
| 597 | do { |
| 598 | *--beg = hextab[chksz & 0xF]; |
| 599 | } while (chksz >>= 4); |
| 600 | return end - beg; |
| 601 | } |
| 602 | |
| 603 | /* |
| 604 | * Init HTTP compression |
| 605 | */ |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 606 | static int |
Willy Tarreau | d54a8ce | 2018-06-29 18:42:02 +0200 | [diff] [blame] | 607 | http_compression_buffer_init(struct channel *inc, struct buffer *out, unsigned int *out_len) |
Christopher Faulet | 3d97c90 | 2015-12-09 14:59:38 +0100 | [diff] [blame] | 608 | { |
| 609 | /* output stream requires at least 10 bytes for the gzip header, plus |
| 610 | * at least 8 bytes for the gzip trailer (crc+len), plus a possible |
| 611 | * plus at most 5 bytes per 32kB block and 2 bytes to close the stream. |
| 612 | */ |
Olivier Houchard | 0b66284 | 2018-06-29 18:16:31 +0200 | [diff] [blame] | 613 | if (c_room(inc) < 20 + 5 * ((ci_data(inc) + 32767) >> 15)) |
Christopher Faulet | 3d97c90 | 2015-12-09 14:59:38 +0100 | [diff] [blame] | 614 | return -1; |
| 615 | |
| 616 | /* prepare an empty output buffer in which we reserve enough room for |
| 617 | * copying the output bytes from <in>, plus 10 extra bytes to write |
| 618 | * the chunk size. We don't copy the bytes yet so that if we have to |
| 619 | * cancel the operation later, it's cheap. |
| 620 | */ |
| 621 | b_reset(out); |
Willy Tarreau | d54a8ce | 2018-06-29 18:42:02 +0200 | [diff] [blame] | 622 | *out_len = co_data(inc); |
| 623 | out->head += *out_len + 10; |
Christopher Faulet | 3d97c90 | 2015-12-09 14:59:38 +0100 | [diff] [blame] | 624 | return 0; |
| 625 | } |
| 626 | |
| 627 | /* |
| 628 | * Add data to compress |
| 629 | */ |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 630 | static int |
| 631 | http_compression_buffer_add_data(struct comp_state *st, struct buffer *in, |
Willy Tarreau | d54a8ce | 2018-06-29 18:42:02 +0200 | [diff] [blame] | 632 | int in_out, struct buffer *out, int sz) |
Christopher Faulet | 3d97c90 | 2015-12-09 14:59:38 +0100 | [diff] [blame] | 633 | { |
Christopher Faulet | 3d97c90 | 2015-12-09 14:59:38 +0100 | [diff] [blame] | 634 | int consumed_data = 0; |
| 635 | int data_process_len; |
| 636 | int block1, block2; |
| 637 | |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 638 | if (!sz) |
Christopher Faulet | 3e7bc67 | 2015-12-07 13:39:08 +0100 | [diff] [blame] | 639 | goto end; |
Christopher Faulet | 3d97c90 | 2015-12-09 14:59:38 +0100 | [diff] [blame] | 640 | |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 641 | /* select the smallest size between the announced chunk size, the input |
Christopher Faulet | 3d97c90 | 2015-12-09 14:59:38 +0100 | [diff] [blame] | 642 | * data, and the available output buffer size. The compressors are |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 643 | * assumed to be able to process all the bytes we pass to them at |
| 644 | * once. */ |
Willy Tarreau | eac5259 | 2018-06-15 13:59:36 +0200 | [diff] [blame] | 645 | data_process_len = MIN(b_room(out), sz); |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 646 | |
Christopher Faulet | 3d97c90 | 2015-12-09 14:59:38 +0100 | [diff] [blame] | 647 | block1 = data_process_len; |
Willy Tarreau | d54a8ce | 2018-06-29 18:42:02 +0200 | [diff] [blame] | 648 | if (block1 > b_contig_data(in, in_out)) |
| 649 | block1 = b_contig_data(in, in_out); |
Christopher Faulet | 3d97c90 | 2015-12-09 14:59:38 +0100 | [diff] [blame] | 650 | block2 = data_process_len - block1; |
| 651 | |
| 652 | /* compressors return < 0 upon error or the amount of bytes read */ |
Willy Tarreau | d54a8ce | 2018-06-29 18:42:02 +0200 | [diff] [blame] | 653 | consumed_data = st->comp_algo->add_data(st->comp_ctx, b_head(in) + in_out, block1, out); |
Christopher Faulet | 3e7bc67 | 2015-12-07 13:39:08 +0100 | [diff] [blame] | 654 | if (consumed_data != block1 || !block2) |
| 655 | goto end; |
Willy Tarreau | d54a8ce | 2018-06-29 18:42:02 +0200 | [diff] [blame] | 656 | consumed_data = st->comp_algo->add_data(st->comp_ctx, b_peek(in, 0), block2, out); |
Christopher Faulet | 3e7bc67 | 2015-12-07 13:39:08 +0100 | [diff] [blame] | 657 | if (consumed_data < 0) |
| 658 | goto end; |
| 659 | consumed_data += block1; |
| 660 | |
| 661 | end: |
Christopher Faulet | 3d97c90 | 2015-12-09 14:59:38 +0100 | [diff] [blame] | 662 | return consumed_data; |
| 663 | } |
| 664 | |
| 665 | /* |
| 666 | * Flush data in process, and write the header and footer of the chunk. Upon |
| 667 | * success, in and out buffers are swapped to avoid a copy. |
| 668 | */ |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 669 | static int |
| 670 | http_compression_buffer_end(struct comp_state *st, struct stream *s, |
Willy Tarreau | 4d45238 | 2018-06-06 07:15:47 +0200 | [diff] [blame] | 671 | struct channel *chn, struct buffer **out, |
Willy Tarreau | d54a8ce | 2018-06-29 18:42:02 +0200 | [diff] [blame] | 672 | unsigned int *buf_out, int end) |
Christopher Faulet | 3d97c90 | 2015-12-09 14:59:38 +0100 | [diff] [blame] | 673 | { |
Willy Tarreau | d54a8ce | 2018-06-29 18:42:02 +0200 | [diff] [blame] | 674 | struct buffer *ob = *out; |
Christopher Faulet | 3d97c90 | 2015-12-09 14:59:38 +0100 | [diff] [blame] | 675 | char *tail; |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 676 | int to_forward, left; |
Willy Tarreau | d54a8ce | 2018-06-29 18:42:02 +0200 | [diff] [blame] | 677 | unsigned int tmp_out; |
Christopher Faulet | 3d97c90 | 2015-12-09 14:59:38 +0100 | [diff] [blame] | 678 | |
| 679 | #if defined(USE_SLZ) || defined(USE_ZLIB) |
| 680 | int ret; |
| 681 | |
| 682 | /* flush data here */ |
Christopher Faulet | 3d97c90 | 2015-12-09 14:59:38 +0100 | [diff] [blame] | 683 | if (end) |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 684 | ret = st->comp_algo->finish(st->comp_ctx, ob); /* end of data */ |
Christopher Faulet | 3d97c90 | 2015-12-09 14:59:38 +0100 | [diff] [blame] | 685 | else |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 686 | ret = st->comp_algo->flush(st->comp_ctx, ob); /* end of buffer */ |
Christopher Faulet | 3d97c90 | 2015-12-09 14:59:38 +0100 | [diff] [blame] | 687 | |
| 688 | if (ret < 0) |
| 689 | return -1; /* flush failed */ |
| 690 | |
| 691 | #endif /* USE_ZLIB */ |
Olivier Houchard | 0b66284 | 2018-06-29 18:16:31 +0200 | [diff] [blame] | 692 | if (b_data(ob) == 0) { |
Christopher Faulet | 3d97c90 | 2015-12-09 14:59:38 +0100 | [diff] [blame] | 693 | /* No data were appended, let's drop the output buffer and |
| 694 | * keep the input buffer unchanged. |
| 695 | */ |
| 696 | return 0; |
| 697 | } |
| 698 | |
| 699 | /* OK so at this stage, we have an output buffer <ob> looking like this : |
| 700 | * |
| 701 | * <-- o --> <------ i -----> |
| 702 | * +---------+---+------------+-----------+ |
| 703 | * | out | c | comp_in | empty | |
| 704 | * +---------+---+------------+-----------+ |
| 705 | * data p size |
| 706 | * |
Willy Tarreau | d54a8ce | 2018-06-29 18:42:02 +0200 | [diff] [blame] | 707 | * <out> is the room reserved to copy the channel output. It starts at |
Willy Tarreau | 892f1db | 2018-07-09 10:55:37 +0200 | [diff] [blame^] | 708 | * ob->area and has not yet been filled. <c> is the room reserved to |
Willy Tarreau | d54a8ce | 2018-06-29 18:42:02 +0200 | [diff] [blame] | 709 | * write the chunk size (10 bytes). <comp_in> is the compressed |
| 710 | * equivalent of the data part of ib->len. <empty> is the amount of |
| 711 | * empty bytes at the end of the buffer, into which we may have to |
| 712 | * copy the remaining bytes from ib->len after the data |
| 713 | * (chunk size, trailers, ...). |
Christopher Faulet | 3d97c90 | 2015-12-09 14:59:38 +0100 | [diff] [blame] | 714 | */ |
| 715 | |
| 716 | /* Write real size at the begining of the chunk, no need of wrapping. |
| 717 | * We write the chunk using a dynamic length and adjust ob->p and ob->i |
| 718 | * accordingly afterwards. That will move <out> away from <data>. |
| 719 | */ |
Willy Tarreau | d54a8ce | 2018-06-29 18:42:02 +0200 | [diff] [blame] | 720 | left = http_emit_chunk_size(b_head(ob), b_data(ob)); |
| 721 | b_add(ob, left); |
| 722 | ob->head -= *buf_out + (left); |
| 723 | /* Copy previous data from chn into ob */ |
| 724 | if (co_data(chn) > 0) { |
| 725 | left = b_contig_data(chn->buf, 0); |
| 726 | if (left > *buf_out) |
| 727 | left = *buf_out; |
Christopher Faulet | 3d97c90 | 2015-12-09 14:59:38 +0100 | [diff] [blame] | 728 | |
Willy Tarreau | d54a8ce | 2018-06-29 18:42:02 +0200 | [diff] [blame] | 729 | memcpy(b_head(ob), co_head(chn), left); |
| 730 | b_add(ob, left); |
| 731 | if (co_data(chn) - left) {/* second part of the buffer */ |
| 732 | memcpy(b_head(ob) + left, b_orig(chn->buf), co_data(chn) - left); |
| 733 | b_add(ob, co_data(chn) - left); |
| 734 | } |
Christopher Faulet | 3d97c90 | 2015-12-09 14:59:38 +0100 | [diff] [blame] | 735 | } |
| 736 | |
| 737 | /* chunked encoding requires CRLF after data */ |
Willy Tarreau | d54a8ce | 2018-06-29 18:42:02 +0200 | [diff] [blame] | 738 | tail = b_tail(ob); |
Christopher Faulet | 3d97c90 | 2015-12-09 14:59:38 +0100 | [diff] [blame] | 739 | *tail++ = '\r'; |
| 740 | *tail++ = '\n'; |
| 741 | |
Christopher Faulet | 2fb2880 | 2015-12-01 10:40:57 +0100 | [diff] [blame] | 742 | /* At the end of data, we must write the empty chunk 0<CRLF>, |
| 743 | * and terminate the trailers section with a last <CRLF>. If |
| 744 | * we're forwarding a chunked-encoded response, we'll have a |
| 745 | * trailers section after the empty chunk which needs to be |
| 746 | * forwarded and which will provide the last CRLF. Otherwise |
| 747 | * we write it ourselves. |
| 748 | */ |
| 749 | if (end) { |
| 750 | struct http_msg *msg = &s->txn->rsp; |
| 751 | |
| 752 | memcpy(tail, "0\r\n", 3); |
| 753 | tail += 3; |
Christopher Faulet | b77c5c2 | 2015-12-07 16:48:42 +0100 | [diff] [blame] | 754 | if (!(msg->flags & HTTP_MSGF_TE_CHNK)) { |
Christopher Faulet | 2fb2880 | 2015-12-01 10:40:57 +0100 | [diff] [blame] | 755 | memcpy(tail, "\r\n", 2); |
| 756 | tail += 2; |
| 757 | } |
| 758 | } |
| 759 | |
Willy Tarreau | d54a8ce | 2018-06-29 18:42:02 +0200 | [diff] [blame] | 760 | b_add(ob, tail - b_tail(ob)); |
| 761 | to_forward = b_data(ob) - *buf_out; |
Christopher Faulet | 3d97c90 | 2015-12-09 14:59:38 +0100 | [diff] [blame] | 762 | |
| 763 | /* update input rate */ |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 764 | if (st->comp_ctx && st->comp_ctx->cur_lvl > 0) { |
Christopher Faulet | 2fb2880 | 2015-12-01 10:40:57 +0100 | [diff] [blame] | 765 | update_freq_ctr(&global.comp_bps_in, st->consumed); |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 766 | HA_ATOMIC_ADD(&strm_fe(s)->fe_counters.comp_in, st->consumed); |
| 767 | HA_ATOMIC_ADD(&s->be->be_counters.comp_in, st->consumed); |
Christopher Faulet | 3d97c90 | 2015-12-09 14:59:38 +0100 | [diff] [blame] | 768 | } else { |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 769 | HA_ATOMIC_ADD(&strm_fe(s)->fe_counters.comp_byp, st->consumed); |
| 770 | HA_ATOMIC_ADD(&s->be->be_counters.comp_byp, st->consumed); |
Christopher Faulet | 3d97c90 | 2015-12-09 14:59:38 +0100 | [diff] [blame] | 771 | } |
| 772 | |
| 773 | /* copy the remaining data in the tmp buffer. */ |
Willy Tarreau | bcbd393 | 2018-06-06 07:13:22 +0200 | [diff] [blame] | 774 | c_adv(chn, st->consumed); |
Willy Tarreau | d54a8ce | 2018-06-29 18:42:02 +0200 | [diff] [blame] | 775 | if (b_data(chn->buf) - co_data(chn) > 0) { |
Willy Tarreau | 7194d3c | 2018-06-06 16:55:45 +0200 | [diff] [blame] | 776 | left = ci_contig_data(chn); |
Willy Tarreau | d54a8ce | 2018-06-29 18:42:02 +0200 | [diff] [blame] | 777 | memcpy(b_tail(ob), ci_head(chn), left); |
Olivier Houchard | acd1403 | 2018-06-28 18:17:23 +0200 | [diff] [blame] | 778 | b_add(ob, left); |
Willy Tarreau | d54a8ce | 2018-06-29 18:42:02 +0200 | [diff] [blame] | 779 | if (b_data(chn->buf) - (co_data(chn) + left)) { |
| 780 | memcpy(b_tail(ob), b_orig(chn->buf), b_data(chn->buf) - left); |
| 781 | b_add(ob, b_data(chn->buf) - left); |
Christopher Faulet | 3d97c90 | 2015-12-09 14:59:38 +0100 | [diff] [blame] | 782 | } |
| 783 | } |
Christopher Faulet | 3d97c90 | 2015-12-09 14:59:38 +0100 | [diff] [blame] | 784 | /* swap the buffers */ |
Willy Tarreau | d54a8ce | 2018-06-29 18:42:02 +0200 | [diff] [blame] | 785 | *out = chn->buf; |
Willy Tarreau | 4d45238 | 2018-06-06 07:15:47 +0200 | [diff] [blame] | 786 | chn->buf = ob; |
Willy Tarreau | d54a8ce | 2018-06-29 18:42:02 +0200 | [diff] [blame] | 787 | tmp_out = chn->buf->output; |
| 788 | chn->buf->output = *buf_out; |
| 789 | *buf_out = tmp_out; |
| 790 | |
Christopher Faulet | 3d97c90 | 2015-12-09 14:59:38 +0100 | [diff] [blame] | 791 | |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 792 | |
| 793 | if (st->comp_ctx && st->comp_ctx->cur_lvl > 0) { |
Christopher Faulet | 3d97c90 | 2015-12-09 14:59:38 +0100 | [diff] [blame] | 794 | update_freq_ctr(&global.comp_bps_out, to_forward); |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 795 | HA_ATOMIC_ADD(&strm_fe(s)->fe_counters.comp_out, to_forward); |
| 796 | HA_ATOMIC_ADD(&s->be->be_counters.comp_out, to_forward); |
Christopher Faulet | 3d97c90 | 2015-12-09 14:59:38 +0100 | [diff] [blame] | 797 | } |
| 798 | |
Christopher Faulet | 3d97c90 | 2015-12-09 14:59:38 +0100 | [diff] [blame] | 799 | return to_forward; |
| 800 | } |
| 801 | |
| 802 | |
| 803 | /***********************************************************************/ |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 804 | struct flt_ops comp_ops = { |
Christopher Faulet | 8ca3b4b | 2017-07-25 11:07:15 +0200 | [diff] [blame] | 805 | .init_per_thread = comp_flt_init_per_thread, |
| 806 | .deinit_per_thread = comp_flt_deinit_per_thread, |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 807 | |
| 808 | .channel_start_analyze = comp_start_analyze, |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 809 | .channel_end_analyze = comp_end_analyze, |
Christopher Faulet | 3dc860d | 2017-09-15 11:39:36 +0200 | [diff] [blame] | 810 | .channel_post_analyze = comp_http_post_analyze, |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 811 | |
Christopher Faulet | 1339d74 | 2016-05-11 16:48:33 +0200 | [diff] [blame] | 812 | .http_headers = comp_http_headers, |
Christopher Faulet | 309c641 | 2015-12-02 09:57:32 +0100 | [diff] [blame] | 813 | .http_data = comp_http_data, |
| 814 | .http_chunk_trailers = comp_http_chunk_trailers, |
| 815 | .http_forward_data = comp_http_forward_data, |
Christopher Faulet | d60b3cf | 2017-06-26 11:47:13 +0200 | [diff] [blame] | 816 | .http_end = comp_http_end, |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 817 | }; |
| 818 | |
Christopher Faulet | 3d97c90 | 2015-12-09 14:59:38 +0100 | [diff] [blame] | 819 | static int |
| 820 | parse_compression_options(char **args, int section, struct proxy *proxy, |
| 821 | struct proxy *defpx, const char *file, int line, |
| 822 | char **err) |
| 823 | { |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 824 | struct comp *comp; |
Christopher Faulet | 3d97c90 | 2015-12-09 14:59:38 +0100 | [diff] [blame] | 825 | |
| 826 | if (proxy->comp == NULL) { |
Vincent Bernat | 02779b6 | 2016-04-03 13:48:43 +0200 | [diff] [blame] | 827 | comp = calloc(1, sizeof(*comp)); |
Christopher Faulet | 3d97c90 | 2015-12-09 14:59:38 +0100 | [diff] [blame] | 828 | proxy->comp = comp; |
| 829 | } |
| 830 | else |
| 831 | comp = proxy->comp; |
| 832 | |
| 833 | if (!strcmp(args[1], "algo")) { |
| 834 | struct comp_ctx *ctx; |
| 835 | int cur_arg = 2; |
| 836 | |
| 837 | if (!*args[cur_arg]) { |
| 838 | memprintf(err, "parsing [%s:%d] : '%s' expects <algorithm>\n", |
| 839 | file, line, args[0]); |
| 840 | return -1; |
| 841 | } |
| 842 | while (*(args[cur_arg])) { |
| 843 | if (comp_append_algo(comp, args[cur_arg]) < 0) { |
| 844 | memprintf(err, "'%s' : '%s' is not a supported algorithm.\n", |
| 845 | args[0], args[cur_arg]); |
| 846 | return -1; |
| 847 | } |
| 848 | if (proxy->comp->algos->init(&ctx, 9) == 0) |
| 849 | proxy->comp->algos->end(&ctx); |
| 850 | else { |
| 851 | memprintf(err, "'%s' : Can't init '%s' algorithm.\n", |
| 852 | args[0], args[cur_arg]); |
| 853 | return -1; |
| 854 | } |
| 855 | cur_arg++; |
| 856 | continue; |
| 857 | } |
| 858 | } |
| 859 | else if (!strcmp(args[1], "offload")) |
| 860 | comp->offload = 1; |
| 861 | else if (!strcmp(args[1], "type")) { |
| 862 | int cur_arg = 2; |
| 863 | |
| 864 | if (!*args[cur_arg]) { |
| 865 | memprintf(err, "'%s' expects <type>\n", args[0]); |
| 866 | return -1; |
| 867 | } |
| 868 | while (*(args[cur_arg])) { |
| 869 | comp_append_type(comp, args[cur_arg]); |
| 870 | cur_arg++; |
| 871 | continue; |
| 872 | } |
| 873 | } |
| 874 | else { |
| 875 | memprintf(err, "'%s' expects 'algo', 'type' or 'offload'\n", |
| 876 | args[0]); |
| 877 | return -1; |
| 878 | } |
| 879 | |
| 880 | return 0; |
| 881 | } |
| 882 | |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 883 | static int |
| 884 | parse_http_comp_flt(char **args, int *cur_arg, struct proxy *px, |
Thierry Fournier | 3610c39 | 2016-04-13 18:27:51 +0200 | [diff] [blame] | 885 | struct flt_conf *fconf, char **err, void *private) |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 886 | { |
Christopher Faulet | 443ea1a | 2016-02-04 13:40:26 +0100 | [diff] [blame] | 887 | struct flt_conf *fc, *back; |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 888 | |
Christopher Faulet | 443ea1a | 2016-02-04 13:40:26 +0100 | [diff] [blame] | 889 | list_for_each_entry_safe(fc, back, &px->filter_configs, list) { |
| 890 | if (fc->id == http_comp_flt_id) { |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 891 | memprintf(err, "%s: Proxy supports only one compression filter\n", px->id); |
| 892 | return -1; |
| 893 | } |
| 894 | } |
| 895 | |
Christopher Faulet | 443ea1a | 2016-02-04 13:40:26 +0100 | [diff] [blame] | 896 | fconf->id = http_comp_flt_id; |
| 897 | fconf->conf = NULL; |
| 898 | fconf->ops = &comp_ops; |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 899 | (*cur_arg)++; |
| 900 | |
| 901 | return 0; |
| 902 | } |
| 903 | |
| 904 | |
| 905 | int |
| 906 | check_legacy_http_comp_flt(struct proxy *proxy) |
| 907 | { |
Christopher Faulet | 443ea1a | 2016-02-04 13:40:26 +0100 | [diff] [blame] | 908 | struct flt_conf *fconf; |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 909 | int err = 0; |
| 910 | |
| 911 | if (proxy->comp == NULL) |
| 912 | goto end; |
Christopher Faulet | 443ea1a | 2016-02-04 13:40:26 +0100 | [diff] [blame] | 913 | if (!LIST_ISEMPTY(&proxy->filter_configs)) { |
| 914 | list_for_each_entry(fconf, &proxy->filter_configs, list) { |
| 915 | if (fconf->id == http_comp_flt_id) |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 916 | goto end; |
| 917 | } |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 918 | ha_alert("config: %s '%s': require an explicit filter declaration to use HTTP compression\n", |
| 919 | proxy_type_str(proxy), proxy->id); |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 920 | err++; |
| 921 | goto end; |
| 922 | } |
| 923 | |
Christopher Faulet | 443ea1a | 2016-02-04 13:40:26 +0100 | [diff] [blame] | 924 | fconf = calloc(1, sizeof(*fconf)); |
| 925 | if (!fconf) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 926 | ha_alert("config: %s '%s': out of memory\n", |
| 927 | proxy_type_str(proxy), proxy->id); |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 928 | err++; |
| 929 | goto end; |
| 930 | } |
Christopher Faulet | 443ea1a | 2016-02-04 13:40:26 +0100 | [diff] [blame] | 931 | fconf->id = http_comp_flt_id; |
| 932 | fconf->conf = NULL; |
| 933 | fconf->ops = &comp_ops; |
| 934 | LIST_ADDQ(&proxy->filter_configs, &fconf->list); |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 935 | |
| 936 | end: |
| 937 | return err; |
| 938 | } |
| 939 | |
| 940 | /* |
| 941 | * boolean, returns true if compression is used (either gzip or deflate) in the |
| 942 | * response. |
| 943 | */ |
Christopher Faulet | 3d97c90 | 2015-12-09 14:59:38 +0100 | [diff] [blame] | 944 | static int |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 945 | smp_fetch_res_comp(const struct arg *args, struct sample *smp, const char *kw, |
| 946 | void *private) |
Christopher Faulet | 3d97c90 | 2015-12-09 14:59:38 +0100 | [diff] [blame] | 947 | { |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 948 | struct http_txn *txn = smp->strm ? smp->strm->txn : NULL; |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 949 | |
Christopher Faulet | 3d97c90 | 2015-12-09 14:59:38 +0100 | [diff] [blame] | 950 | smp->data.type = SMP_T_BOOL; |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 951 | smp->data.u.sint = (txn && (txn->rsp.flags & HTTP_MSGF_COMPRESSING)); |
Christopher Faulet | 3d97c90 | 2015-12-09 14:59:38 +0100 | [diff] [blame] | 952 | return 1; |
| 953 | } |
| 954 | |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 955 | /* |
| 956 | * string, returns algo |
| 957 | */ |
Christopher Faulet | 3d97c90 | 2015-12-09 14:59:38 +0100 | [diff] [blame] | 958 | static int |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 959 | smp_fetch_res_comp_algo(const struct arg *args, struct sample *smp, |
| 960 | const char *kw, void *private) |
Christopher Faulet | 3d97c90 | 2015-12-09 14:59:38 +0100 | [diff] [blame] | 961 | { |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 962 | struct http_txn *txn = smp->strm ? smp->strm->txn : NULL; |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 963 | struct filter *filter; |
| 964 | struct comp_state *st; |
| 965 | |
Christopher Faulet | 03d8553 | 2017-09-15 10:14:43 +0200 | [diff] [blame] | 966 | if (!txn || !(txn->rsp.flags & HTTP_MSGF_COMPRESSING)) |
Christopher Faulet | 3d97c90 | 2015-12-09 14:59:38 +0100 | [diff] [blame] | 967 | return 0; |
| 968 | |
Christopher Faulet | fcf035c | 2015-12-03 11:48:03 +0100 | [diff] [blame] | 969 | list_for_each_entry(filter, &strm_flt(smp->strm)->filters, list) { |
Christopher Faulet | 443ea1a | 2016-02-04 13:40:26 +0100 | [diff] [blame] | 970 | if (FLT_ID(filter) != http_comp_flt_id) |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 971 | continue; |
| 972 | |
| 973 | if (!(st = filter->ctx)) |
| 974 | break; |
| 975 | |
| 976 | smp->data.type = SMP_T_STR; |
| 977 | smp->flags = SMP_F_CONST; |
| 978 | smp->data.u.str.str = st->comp_algo->cfg_name; |
| 979 | smp->data.u.str.len = st->comp_algo->cfg_name_len; |
| 980 | return 1; |
| 981 | } |
| 982 | return 0; |
Christopher Faulet | 3d97c90 | 2015-12-09 14:59:38 +0100 | [diff] [blame] | 983 | } |
| 984 | |
| 985 | /* Declare the config parser for "compression" keyword */ |
| 986 | static struct cfg_kw_list cfg_kws = {ILH, { |
| 987 | { CFG_LISTEN, "compression", parse_compression_options }, |
| 988 | { 0, NULL, NULL }, |
| 989 | } |
| 990 | }; |
| 991 | |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 992 | /* Declare the filter parser for "compression" keyword */ |
| 993 | static struct flt_kw_list filter_kws = { "COMP", { }, { |
Thierry Fournier | 3610c39 | 2016-04-13 18:27:51 +0200 | [diff] [blame] | 994 | { "compression", parse_http_comp_flt, NULL }, |
| 995 | { NULL, NULL, NULL }, |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 996 | } |
| 997 | }; |
| 998 | |
Christopher Faulet | 3d97c90 | 2015-12-09 14:59:38 +0100 | [diff] [blame] | 999 | /* Note: must not be declared <const> as its list will be overwritten */ |
| 1000 | static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, { |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 1001 | { "res.comp", smp_fetch_res_comp, 0, NULL, SMP_T_BOOL, SMP_USE_HRSHP }, |
| 1002 | { "res.comp_algo", smp_fetch_res_comp_algo, 0, NULL, SMP_T_STR, SMP_USE_HRSHP }, |
| 1003 | { /* END */ }, |
| 1004 | } |
| 1005 | }; |
Christopher Faulet | 3d97c90 | 2015-12-09 14:59:38 +0100 | [diff] [blame] | 1006 | |
| 1007 | __attribute__((constructor)) |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 1008 | static void |
| 1009 | __flt_http_comp_init(void) |
Christopher Faulet | 3d97c90 | 2015-12-09 14:59:38 +0100 | [diff] [blame] | 1010 | { |
| 1011 | cfg_register_keywords(&cfg_kws); |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 1012 | flt_register_keywords(&filter_kws); |
Christopher Faulet | 3d97c90 | 2015-12-09 14:59:38 +0100 | [diff] [blame] | 1013 | sample_register_fetches(&sample_fetch_keywords); |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 1014 | pool_head_comp_state = create_pool("comp_state", sizeof(struct comp_state), MEM_F_SHARED); |
Christopher Faulet | 3d97c90 | 2015-12-09 14:59:38 +0100 | [diff] [blame] | 1015 | } |