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