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