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