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