Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [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/debug.h> |
| 15 | #include <common/cfgparse.h> |
| 16 | #include <common/compat.h> |
| 17 | #include <common/config.h> |
| 18 | #include <common/errors.h> |
| 19 | #include <common/namespace.h> |
| 20 | #include <common/standard.h> |
| 21 | |
| 22 | #include <types/filters.h> |
| 23 | #include <types/proto_http.h> |
| 24 | |
| 25 | #include <proto/compression.h> |
| 26 | #include <proto/filters.h> |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 27 | #include <proto/flt_http_comp.h> |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 28 | #include <proto/proto_http.h> |
| 29 | #include <proto/stream.h> |
| 30 | #include <proto/stream_interface.h> |
| 31 | |
| 32 | /* Pool used to allocate filters */ |
| 33 | struct pool_head *pool2_filter = NULL; |
| 34 | |
| 35 | static int handle_analyzer_result(struct stream *s, struct channel *chn, unsigned int an_bit, int ret); |
| 36 | |
| 37 | /* - RESUME_FILTER_LOOP and RESUME_FILTER_END must always be used together. |
| 38 | * The first one begins a loop and the seconds one ends it. |
| 39 | * |
| 40 | * - BREAK_EXECUTION must be used to break the loop and set the filter from |
| 41 | * which to resume the next time. |
| 42 | * |
| 43 | * Here is an exemple: |
| 44 | * |
| 45 | * RESUME_FILTER_LOOP(stream, channel) { |
| 46 | * ... |
| 47 | * if (cond) |
| 48 | * BREAK_EXECUTION(stream, channel, label); |
| 49 | * ... |
| 50 | * } RESUME_FILTER_END; |
| 51 | * ... |
| 52 | * label: |
| 53 | * ... |
| 54 | * |
| 55 | */ |
| 56 | #define RESUME_FILTER_LOOP(strm, chn) \ |
| 57 | do { \ |
| 58 | struct filter *filter; \ |
| 59 | \ |
| 60 | if ((strm)->strm_flt.current[CHN_IDX(chn)]) { \ |
| 61 | filter = (strm)->strm_flt.current[CHN_IDX(chn)]; \ |
| 62 | (strm)->strm_flt.current[CHN_IDX(chn)] = NULL; \ |
| 63 | goto resume_execution; \ |
| 64 | } \ |
| 65 | \ |
| 66 | list_for_each_entry(filter, &s->strm_flt.filters, list) { \ |
| 67 | resume_execution: |
| 68 | |
| 69 | #define RESUME_FILTER_END \ |
| 70 | } \ |
| 71 | } while(0) |
| 72 | |
| 73 | #define BREAK_EXECUTION(strm, chn, label) \ |
| 74 | do { \ |
| 75 | (strm)->strm_flt.current[CHN_IDX(chn)] = filter; \ |
| 76 | goto label; \ |
| 77 | } while (0) |
| 78 | |
| 79 | |
| 80 | /* List head of all known filter keywords */ |
| 81 | static struct flt_kw_list flt_keywords = { |
| 82 | .list = LIST_HEAD_INIT(flt_keywords.list) |
| 83 | }; |
| 84 | |
| 85 | /* |
| 86 | * Registers the filter keyword list <kwl> as a list of valid keywords for next |
| 87 | * parsing sessions. |
| 88 | */ |
| 89 | void |
| 90 | flt_register_keywords(struct flt_kw_list *kwl) |
| 91 | { |
| 92 | LIST_ADDQ(&flt_keywords.list, &kwl->list); |
| 93 | } |
| 94 | |
| 95 | /* |
| 96 | * Returns a pointer to the filter keyword <kw>, or NULL if not found. If the |
| 97 | * keyword is found with a NULL ->parse() function, then an attempt is made to |
| 98 | * find one with a valid ->parse() function. This way it is possible to declare |
| 99 | * platform-dependant, known keywords as NULL, then only declare them as valid |
| 100 | * if some options are met. Note that if the requested keyword contains an |
| 101 | * opening parenthesis, everything from this point is ignored. |
| 102 | */ |
| 103 | struct flt_kw * |
| 104 | flt_find_kw(const char *kw) |
| 105 | { |
| 106 | int index; |
| 107 | const char *kwend; |
| 108 | struct flt_kw_list *kwl; |
| 109 | struct flt_kw *ret = NULL; |
| 110 | |
| 111 | kwend = strchr(kw, '('); |
| 112 | if (!kwend) |
| 113 | kwend = kw + strlen(kw); |
| 114 | |
| 115 | list_for_each_entry(kwl, &flt_keywords.list, list) { |
| 116 | for (index = 0; kwl->kw[index].kw != NULL; index++) { |
| 117 | if ((strncmp(kwl->kw[index].kw, kw, kwend - kw) == 0) && |
| 118 | kwl->kw[index].kw[kwend-kw] == 0) { |
| 119 | if (kwl->kw[index].parse) |
| 120 | return &kwl->kw[index]; /* found it !*/ |
| 121 | else |
| 122 | ret = &kwl->kw[index]; /* may be OK */ |
| 123 | } |
| 124 | } |
| 125 | } |
| 126 | return ret; |
| 127 | } |
| 128 | |
| 129 | /* |
| 130 | * Dumps all registered "filter" keywords to the <out> string pointer. The |
| 131 | * unsupported keywords are only dumped if their supported form was not found. |
| 132 | */ |
| 133 | void |
| 134 | flt_dump_kws(char **out) |
| 135 | { |
| 136 | struct flt_kw_list *kwl; |
| 137 | int index; |
| 138 | |
| 139 | *out = NULL; |
| 140 | list_for_each_entry(kwl, &flt_keywords.list, list) { |
| 141 | for (index = 0; kwl->kw[index].kw != NULL; index++) { |
| 142 | if (kwl->kw[index].parse || |
| 143 | flt_find_kw(kwl->kw[index].kw) == &kwl->kw[index]) { |
| 144 | memprintf(out, "%s[%4s] %s%s\n", *out ? *out : "", |
| 145 | kwl->scope, |
| 146 | kwl->kw[index].kw, |
| 147 | kwl->kw[index].parse ? "" : " (not supported)"); |
| 148 | } |
| 149 | } |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | /* |
| 154 | * Parses the "filter" keyword. All keywords must be handled by filters |
| 155 | * themselves |
| 156 | */ |
| 157 | static int |
| 158 | parse_filter(char **args, int section_type, struct proxy *curpx, |
| 159 | struct proxy *defpx, const char *file, int line, char **err) |
| 160 | { |
| 161 | struct filter *filter = NULL; |
| 162 | |
| 163 | /* Filter cannot be defined on a default proxy */ |
| 164 | if (curpx == defpx) { |
| 165 | memprintf(err, "parsing [%s:%d] : %s is only allowed in a 'default' section.", |
| 166 | file, line, args[0]); |
| 167 | return -1; |
| 168 | } |
| 169 | if (!strcmp(args[0], "filter")) { |
| 170 | struct flt_kw *kw; |
| 171 | int cur_arg; |
| 172 | |
| 173 | if (!*args[1]) { |
| 174 | memprintf(err, |
| 175 | "parsing [%s:%d] : missing argument for '%s' in %s '%s'.", |
| 176 | file, line, args[0], proxy_type_str(curpx), curpx->id); |
| 177 | goto error; |
| 178 | } |
| 179 | filter = pool_alloc2(pool2_filter); |
| 180 | if (!filter) { |
| 181 | memprintf(err, "'%s' : out of memory", args[0]); |
| 182 | goto error; |
| 183 | } |
| 184 | memset(filter, 0, sizeof(*filter)); |
| 185 | |
| 186 | cur_arg = 1; |
| 187 | kw = flt_find_kw(args[cur_arg]); |
| 188 | if (kw) { |
| 189 | if (!kw->parse) { |
| 190 | memprintf(err, "parsing [%s:%d] : '%s' : " |
| 191 | "'%s' option is not implemented in this version (check build options).", |
| 192 | file, line, args[0], args[cur_arg]); |
| 193 | goto error; |
| 194 | } |
| 195 | if (kw->parse(args, &cur_arg, curpx, filter, err) != 0) { |
| 196 | if (err && *err) |
| 197 | memprintf(err, "'%s' : '%s'", |
| 198 | args[0], *err); |
| 199 | else |
| 200 | memprintf(err, "'%s' : error encountered while processing '%s'", |
| 201 | args[0], args[cur_arg]); |
| 202 | goto error; |
| 203 | } |
| 204 | } |
| 205 | else { |
| 206 | flt_dump_kws(err); |
| 207 | indent_msg(err, 4); |
| 208 | memprintf(err, "'%s' : unknown keyword '%s'.%s%s", |
| 209 | args[0], args[cur_arg], |
| 210 | err && *err ? " Registered keywords :" : "", err && *err ? *err : ""); |
| 211 | goto error; |
| 212 | } |
| 213 | if (*args[cur_arg]) { |
| 214 | memprintf(err, "'%s %s' : unknown keyword '%s'.", |
| 215 | args[0], args[1], args[cur_arg]); |
| 216 | goto error; |
| 217 | } |
| 218 | |
| 219 | LIST_ADDQ(&curpx->filters, &filter->list); |
| 220 | } |
| 221 | return 0; |
| 222 | |
| 223 | error: |
| 224 | if (filter) |
| 225 | pool_free2(pool2_filter, filter); |
| 226 | return -1; |
| 227 | |
| 228 | |
| 229 | } |
| 230 | |
| 231 | /* |
| 232 | * Calls 'init' callback for all filters attached to a proxy. This happens after |
| 233 | * the configuration parsing. Filters can finish to fill their config. Returns |
| 234 | * (ERR_ALERT|ERR_FATAL) if an error occurs, 0 otherwise. |
| 235 | */ |
| 236 | int |
| 237 | flt_init(struct proxy *proxy) |
| 238 | { |
| 239 | struct filter *filter; |
| 240 | |
| 241 | list_for_each_entry(filter, &proxy->filters, list) { |
| 242 | if (filter->ops->init && filter->ops->init(proxy, filter) < 0) |
| 243 | return ERR_ALERT|ERR_FATAL; |
| 244 | } |
| 245 | return 0; |
| 246 | } |
| 247 | |
| 248 | /* |
| 249 | * Calls 'check' callback for all filters attached to a proxy. This happens |
| 250 | * after the configuration parsing but before filters initialization. Returns |
| 251 | * the number of encountered errors. |
| 252 | */ |
| 253 | int |
| 254 | flt_check(struct proxy *proxy) |
| 255 | { |
| 256 | struct filter *filter; |
| 257 | int err = 0; |
| 258 | |
| 259 | list_for_each_entry(filter, &proxy->filters, list) { |
| 260 | if (filter->ops->check) |
| 261 | err += filter->ops->check(proxy, filter); |
| 262 | } |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 263 | err += check_legacy_http_comp_flt(proxy); |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 264 | return err; |
| 265 | } |
| 266 | |
| 267 | /* |
| 268 | * Calls 'denit' callback for all filters attached to a proxy. This happens when |
| 269 | * HAProxy is stopped. |
| 270 | */ |
| 271 | void |
| 272 | flt_deinit(struct proxy *proxy) |
| 273 | { |
| 274 | struct filter *filter, *back; |
| 275 | |
| 276 | list_for_each_entry_safe(filter, back, &proxy->filters, list) { |
| 277 | if (filter->ops->deinit) |
| 278 | filter->ops->deinit(proxy, filter); |
| 279 | LIST_DEL(&filter->list); |
| 280 | pool_free2(pool2_filter, filter); |
| 281 | } |
| 282 | } |
| 283 | |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 284 | /* Attaches a filter to a stream. Returns -1 if an error occurs, 0 otherwise. */ |
| 285 | static int |
| 286 | flt_stream_add_filter(struct stream *s, struct filter *filter, |
| 287 | int is_backend) |
| 288 | { |
| 289 | struct filter *f = pool_alloc2(pool2_filter); |
| 290 | if (!f) /* not enough memory */ |
| 291 | return -1; |
| 292 | memset(f, 0, sizeof(*f)); |
| 293 | f->id = filter->id; |
| 294 | f->ops = filter->ops; |
| 295 | f->conf = filter->conf; |
| 296 | f->is_backend_filter = is_backend; |
| 297 | LIST_ADDQ(&s->strm_flt.filters, &f->list); |
Christopher Faulet | 3e34429 | 2015-11-24 16:24:13 +0100 | [diff] [blame^] | 298 | s->strm_flt.has_filters = 1; |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 299 | return 0; |
| 300 | } |
| 301 | |
| 302 | /* |
| 303 | * Called when a stream is created. It attaches all frontend filters to the |
| 304 | * stream. Returns -1 if an error occurs, 0 otherwise. |
| 305 | */ |
| 306 | int |
| 307 | flt_stream_init(struct stream *s) |
| 308 | { |
| 309 | struct filter *filter; |
| 310 | |
| 311 | LIST_INIT(&s->strm_flt.filters); |
| 312 | memset(s->strm_flt.current, 0, sizeof(s->strm_flt.current)); |
Christopher Faulet | 3e34429 | 2015-11-24 16:24:13 +0100 | [diff] [blame^] | 313 | s->strm_flt.has_filters = 0; |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 314 | list_for_each_entry(filter, &strm_fe(s)->filters, list) { |
| 315 | if (flt_stream_add_filter(s, filter, 0) < 0) |
| 316 | return -1; |
| 317 | } |
| 318 | return 0; |
| 319 | } |
| 320 | |
| 321 | /* |
| 322 | * Called when a stream is closed or when analyze ends (For an HTTP stream, this |
| 323 | * happens after each request/response exchange). When analyze ends, backend |
| 324 | * filters are removed. When the stream is closed, all filters attached to the |
| 325 | * stream are removed. |
| 326 | */ |
| 327 | void |
| 328 | flt_stream_release(struct stream *s, int only_backend) |
| 329 | { |
| 330 | struct filter *filter, *back; |
| 331 | |
| 332 | list_for_each_entry_safe(filter, back, &s->strm_flt.filters, list) { |
| 333 | if (!only_backend || filter->is_backend_filter) { |
| 334 | LIST_DEL(&filter->list); |
| 335 | pool_free2(pool2_filter, filter); |
| 336 | } |
| 337 | } |
Christopher Faulet | 3e34429 | 2015-11-24 16:24:13 +0100 | [diff] [blame^] | 338 | if (LIST_ISEMPTY(&s->strm_flt.filters)) |
| 339 | s->strm_flt.has_filters = 0; |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 340 | } |
| 341 | |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 342 | /* |
| 343 | * Calls 'stream_start' for all filters attached to a stream. This happens when |
| 344 | * the stream is created, just after calling flt_stream_init |
| 345 | * function. Returns -1 if an error occurs, 0 otherwise. |
| 346 | */ |
| 347 | int |
| 348 | flt_stream_start(struct stream *s) |
| 349 | { |
| 350 | struct filter *filter; |
| 351 | |
| 352 | list_for_each_entry(filter, &s->strm_flt.filters, list) { |
| 353 | if (filter->ops->stream_start && filter->ops->stream_start(s, filter) < 0) |
| 354 | return -1; |
| 355 | } |
| 356 | return 0; |
| 357 | } |
| 358 | |
| 359 | /* |
| 360 | * Calls 'stream_stop' for all filters attached to a stream. This happens when |
| 361 | * the stream is stopped, just before calling flt_stream_release function. |
| 362 | */ |
| 363 | void |
| 364 | flt_stream_stop(struct stream *s) |
| 365 | { |
| 366 | struct filter *filter; |
| 367 | |
| 368 | list_for_each_entry(filter, &s->strm_flt.filters, list) { |
| 369 | if (filter->ops->stream_stop) |
| 370 | filter->ops->stream_stop(s, filter); |
| 371 | } |
| 372 | } |
| 373 | |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 374 | /* |
| 375 | * Called when a backend is set for a stream. If the frontend and the backend |
| 376 | * are the same, this function does nothing. Else it attaches all backend |
| 377 | * filters to the stream. Returns -1 if an error occurs, 0 otherwise. |
| 378 | */ |
| 379 | int |
| 380 | flt_set_stream_backend(struct stream *s, struct proxy *be) |
| 381 | { |
| 382 | struct filter *filter; |
| 383 | |
| 384 | if (strm_fe(s) == be) |
| 385 | return 0; |
| 386 | |
| 387 | list_for_each_entry(filter, &be->filters, list) { |
| 388 | if (flt_stream_add_filter(s, filter, 1) < 0) |
| 389 | return -1; |
| 390 | } |
| 391 | return 0; |
| 392 | } |
| 393 | |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 394 | int |
| 395 | flt_http_headers(struct stream *s, struct http_msg *msg) |
| 396 | { |
| 397 | struct filter *filter; |
| 398 | int ret = 1; |
| 399 | |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 400 | RESUME_FILTER_LOOP(s, msg->chn) { |
| 401 | if (filter->ops && filter->ops->http_headers) { |
| 402 | ret = filter->ops->http_headers(s, filter, msg); |
| 403 | if (ret <= 0) |
| 404 | BREAK_EXECUTION(s, msg->chn, end); |
| 405 | } |
| 406 | } RESUME_FILTER_END; |
| 407 | |
| 408 | /* We increase FLT_NXT offset after all processing on headers because |
| 409 | * any filter can alter them. So the definitive size of headers |
| 410 | * (msg->sov) is only known when all filters have been called. */ |
| 411 | list_for_each_entry(filter, &s->strm_flt.filters, list) { |
| 412 | FLT_NXT(filter, msg->chn) = msg->sov; |
| 413 | } |
| 414 | end: |
| 415 | return ret; |
| 416 | } |
| 417 | |
| 418 | int |
| 419 | flt_http_start_chunk(struct stream *s, struct http_msg *msg) |
| 420 | { |
| 421 | int ret = 1; |
| 422 | |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 423 | RESUME_FILTER_LOOP(s, msg->chn) { |
| 424 | if (filter->ops->http_start_chunk) { |
| 425 | ret = filter->ops->http_start_chunk(s, filter, msg); |
| 426 | if (ret <= 0) |
| 427 | BREAK_EXECUTION(s, msg->chn, end); |
| 428 | } |
| 429 | FLT_NXT(filter, msg->chn) += msg->sol; |
| 430 | } RESUME_FILTER_END; |
| 431 | end: |
| 432 | return ret; |
| 433 | } |
| 434 | |
| 435 | /* |
| 436 | * Calls 'http_data' callback for all "data" filters attached to a stream. This |
| 437 | * function is called when incoming data are available (excluding chunks |
| 438 | * envelope for chunked messages) in the AN_REQ_HTTP_XFER_BODY and |
| 439 | * AN_RES_HTTP_XFER_BODY analyzers. It takes care to update the next offset of |
| 440 | * filters and adjusts available data to be sure that a filter cannot parse more |
| 441 | * data than its predecessors. A filter can choose to not consume all available |
| 442 | * data. Returns -1 if an error occurs, the number of consumed bytes otherwise. |
| 443 | */ |
| 444 | int |
| 445 | flt_http_data(struct stream *s, struct http_msg *msg) |
| 446 | { |
| 447 | struct filter *filter = NULL; |
| 448 | unsigned int buf_i; |
| 449 | int ret = 0; |
| 450 | |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 451 | /* Save buffer state */ |
| 452 | buf_i = msg->chn->buf->i; |
| 453 | list_for_each_entry(filter, &s->strm_flt.filters, list) { |
| 454 | if (filter->ops->http_data && !flt_want_forward_data(filter, msg->chn)) { |
| 455 | ret = filter->ops->http_data(s, filter, msg); |
| 456 | if (ret < 0) |
| 457 | break; |
| 458 | } |
| 459 | else { |
| 460 | /* msg->chunk_len is the remaining size of data to parse |
| 461 | * in the body (or in the current chunk for |
| 462 | * chunk-encoded messages) from the HTTP parser point of |
| 463 | * view (relatively to msg->next). To have it from the |
| 464 | * filter point of view, we need to be add (msg->next |
| 465 | * -FLT_NEXT) to it. */ |
| 466 | ret = MIN(msg->chunk_len + msg->next, msg->chn->buf->i) - FLT_NXT(filter, msg->chn); |
| 467 | } |
| 468 | |
| 469 | /* Increase FLT_NXT offset of the current filter */ |
| 470 | FLT_NXT(filter, msg->chn) += ret; |
| 471 | |
| 472 | /* And set this value as the bound for the next filter. It will |
| 473 | * not able to parse more data than the current one. */ |
| 474 | msg->chn->buf->i = FLT_NXT(filter, msg->chn); |
| 475 | } |
| 476 | /* Restore the original buffer state */ |
| 477 | msg->chn->buf->i = buf_i; |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 478 | return ret; |
| 479 | } |
| 480 | |
| 481 | int |
| 482 | flt_http_end_chunk(struct stream *s, struct http_msg *msg) |
| 483 | { |
| 484 | int ret = 1; |
| 485 | |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 486 | RESUME_FILTER_LOOP(s, msg->chn) { |
| 487 | if (filter->ops->http_end_chunk) { |
| 488 | ret = filter->ops->http_end_chunk(s, filter, msg); |
| 489 | if (ret <= 0) |
| 490 | BREAK_EXECUTION(s, msg->chn, end); |
| 491 | } |
| 492 | flt_reset_forward_data(filter, msg->chn); |
| 493 | FLT_NXT(filter, msg->chn) += msg->sol; |
| 494 | } RESUME_FILTER_END; |
| 495 | end: |
| 496 | return ret; |
| 497 | } |
| 498 | |
| 499 | int |
| 500 | flt_http_last_chunk(struct stream *s, struct http_msg *msg) |
| 501 | { |
| 502 | int ret = 1; |
| 503 | |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 504 | RESUME_FILTER_LOOP(s, msg->chn) { |
| 505 | if (filter->ops->http_last_chunk) { |
| 506 | ret = filter->ops->http_last_chunk(s, filter, msg); |
| 507 | if (ret <= 0) |
| 508 | BREAK_EXECUTION(s, msg->chn, end); |
| 509 | } |
| 510 | flt_reset_forward_data(filter, msg->chn); |
| 511 | FLT_NXT(filter, msg->chn) += msg->sol; |
| 512 | } RESUME_FILTER_END; |
| 513 | end: |
| 514 | return ret; |
| 515 | } |
| 516 | |
| 517 | |
| 518 | /* |
| 519 | * Calls 'http_chunk_trailers' callback for all "data" filters attached to a |
| 520 | * stream. This function is called for chunked messages only when a part of the |
| 521 | * trailers was parsed in the AN_REQ_HTTP_XFER_BODY and AN_RES_HTTP_XFER_BODY |
| 522 | * analyzers. Filters can know how much data were parsed by the HTTP parsing |
| 523 | * until the last call with the msg->sol value. Returns a negative value if an |
| 524 | * error occurs, any other value otherwise. |
| 525 | */ |
| 526 | int |
| 527 | flt_http_chunk_trailers(struct stream *s, struct http_msg *msg) |
| 528 | { |
| 529 | int ret = 1; |
| 530 | |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 531 | RESUME_FILTER_LOOP(s, msg->chn) { |
| 532 | if (filter->ops->http_chunk_trailers) { |
| 533 | ret = filter->ops->http_chunk_trailers(s, filter, msg); |
| 534 | if (ret <= 0) |
| 535 | BREAK_EXECUTION(s, msg->chn, end); |
| 536 | } |
| 537 | FLT_NXT(filter, msg->chn) += msg->sol; |
| 538 | } RESUME_FILTER_END; |
| 539 | end: |
| 540 | return ret; |
| 541 | } |
| 542 | |
| 543 | /* |
| 544 | * Calls 'http_end' callback for all filters attached to a stream. All filters |
| 545 | * are called here, but only if there is at least one "data" filter. This |
| 546 | * functions is called when all data were parsed and forwarded. 'http_end' |
| 547 | * callback is resumable, so this function returns a negative value if an error |
| 548 | * occurs, 0 if it needs to wait for some reason, any other value otherwise. |
| 549 | */ |
| 550 | int |
| 551 | flt_http_end(struct stream *s, struct http_msg *msg) |
| 552 | { |
| 553 | int ret = 1; |
| 554 | |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 555 | RESUME_FILTER_LOOP(s, msg->chn) { |
| 556 | if (filter->ops->http_end) { |
| 557 | ret = filter->ops->http_end(s, filter, msg); |
| 558 | if (ret <= 0) |
| 559 | BREAK_EXECUTION(s, msg->chn, end); |
| 560 | } |
| 561 | flt_reset_forward_data(filter, msg->chn); |
| 562 | } RESUME_FILTER_END; |
| 563 | end: |
| 564 | return ret; |
| 565 | } |
| 566 | |
| 567 | /* |
| 568 | * Calls 'http_reset' callback for all filters attached to a stream. This |
| 569 | * happens when a 100-continue response is received. |
| 570 | */ |
| 571 | void |
| 572 | flt_http_reset(struct stream *s, struct http_msg *msg) |
| 573 | { |
| 574 | struct filter *filter; |
| 575 | |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 576 | list_for_each_entry(filter, &s->strm_flt.filters, list) { |
| 577 | if (filter->ops->http_reset) |
| 578 | filter->ops->http_reset(s, filter, msg); |
| 579 | } |
| 580 | } |
| 581 | |
| 582 | /* |
| 583 | * Calls 'http_reply' callback for all filters attached to a stream when HA |
| 584 | * decides to stop the HTTP message processing. |
| 585 | */ |
| 586 | void |
| 587 | flt_http_reply(struct stream *s, short status, const struct chunk *msg) |
| 588 | { |
| 589 | struct filter *filter; |
| 590 | |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 591 | list_for_each_entry(filter, &s->strm_flt.filters, list) { |
| 592 | if (filter->ops->http_reply) |
| 593 | filter->ops->http_reply(s, filter, status, msg); |
| 594 | } |
| 595 | } |
| 596 | |
| 597 | /* |
| 598 | * Calls 'http_forward_data' callback for all "data" filters attached to a |
| 599 | * stream. This function is called when some data can be forwarded in the |
| 600 | * AN_REQ_HTTP_XFER_BODY and AN_RES_HTTP_XFER_BODY analyzers. It takes care to |
| 601 | * update the forward offset of filters and adjusts "forwardable" data to be |
| 602 | * sure that a filter cannot forward more data than its predecessors. A filter |
| 603 | * can choose to not forward all parsed data. Returns a negative value if an |
| 604 | * error occurs, else the number of forwarded bytes. |
| 605 | */ |
| 606 | int |
| 607 | flt_http_forward_data(struct stream *s, struct http_msg *msg, unsigned int len) |
| 608 | { |
| 609 | struct filter *filter = NULL; |
| 610 | int ret = len; |
| 611 | |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 612 | list_for_each_entry(filter, &s->strm_flt.filters, list) { |
| 613 | if (filter->ops->http_forward_data) { |
| 614 | /* Remove bytes that the current filter considered as |
| 615 | * forwarded */ |
| 616 | ret = filter->ops->http_forward_data(s, filter, msg, |
| 617 | ret - FLT_FWD(filter, msg->chn)); |
| 618 | if (ret < 0) |
| 619 | goto end; |
| 620 | } |
| 621 | |
| 622 | /* Adjust bytes that the current filter considers as |
| 623 | * forwarded */ |
| 624 | FLT_FWD(filter, msg->chn) += ret; |
| 625 | |
| 626 | /* And set this value as the bound for the next filter. It will |
| 627 | * not able to forward more data than the current one. */ |
| 628 | ret = FLT_FWD(filter, msg->chn); |
| 629 | } |
| 630 | |
| 631 | if (!ret) |
| 632 | goto end; |
| 633 | |
| 634 | /* Finally, adjust filters offsets by removing data that HAProxy will |
| 635 | * forward. */ |
| 636 | list_for_each_entry(filter, &s->strm_flt.filters, list) { |
| 637 | FLT_NXT(filter, msg->chn) -= ret; |
| 638 | FLT_FWD(filter, msg->chn) -= ret; |
| 639 | } |
| 640 | end: |
| 641 | return ret; |
| 642 | } |
| 643 | |
| 644 | /* |
| 645 | * Calls 'channel_start_analyze' callback for all filters attached to a |
| 646 | * stream. This function is called when we start to analyze a request or a |
| 647 | * response. For frontend filters, it is called before all other analyzers. For |
| 648 | * backend ones, it is called before all backend |
| 649 | * analyzers. 'channel_start_analyze' callback is resumable, so this function |
| 650 | * returns 0 if an error occurs or if it needs to wait, any other value |
| 651 | * otherwise. |
| 652 | */ |
| 653 | int |
| 654 | flt_start_analyze(struct stream *s, struct channel *chn, unsigned int an_bit) |
| 655 | { |
| 656 | int ret = 1; |
| 657 | |
| 658 | /* If this function is called, this means there is at least one filter, |
| 659 | * so we do not need to check the filter list's emptiness. */ |
| 660 | |
| 661 | RESUME_FILTER_LOOP(s, chn) { |
| 662 | if (an_bit == AN_FLT_START_BE && !filter->is_backend_filter) |
| 663 | continue; |
| 664 | |
| 665 | filter->next[CHN_IDX(chn)] = 0; |
| 666 | filter->fwd[CHN_IDX(chn)] = 0; |
| 667 | |
| 668 | if (filter->ops->channel_start_analyze) { |
| 669 | ret = filter->ops->channel_start_analyze(s, filter, chn); |
| 670 | if (ret <= 0) |
| 671 | BREAK_EXECUTION(s, chn, end); |
| 672 | } |
| 673 | } RESUME_FILTER_END; |
| 674 | |
| 675 | end: |
| 676 | return handle_analyzer_result(s, chn, an_bit, ret); |
| 677 | } |
| 678 | |
| 679 | /* |
| 680 | * Calls 'channel_analyze' callback for all filters attached to a stream. This |
| 681 | * function is called before each analyzer attached to a channel, expects |
| 682 | * analyzers responsible for data sending. 'channel_analyze' callback is |
| 683 | * resumable, so this function returns 0 if an error occurs or if it needs to |
| 684 | * wait, any other value otherwise. |
| 685 | */ |
| 686 | int |
| 687 | flt_analyze(struct stream *s, struct channel *chn, unsigned int an_bit) |
| 688 | { |
| 689 | int ret = 1; |
| 690 | |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 691 | RESUME_FILTER_LOOP(s, chn) { |
| 692 | if (filter->ops->channel_analyze) { |
| 693 | ret = filter->ops->channel_analyze(s, filter, chn, an_bit); |
| 694 | if (ret <= 0) |
| 695 | BREAK_EXECUTION(s, chn, check_result); |
| 696 | } |
| 697 | } RESUME_FILTER_END; |
| 698 | |
| 699 | check_result: |
| 700 | ret = handle_analyzer_result(s, chn, 0, ret); |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 701 | return ret; |
| 702 | } |
| 703 | |
| 704 | /* |
| 705 | * Calls 'channel_end_analyze' callback for all filters attached to a |
| 706 | * stream. This function is called when we stop to analyze a request or a |
| 707 | * response. It is called after all other analyzers. 'channel_end_analyze' |
| 708 | * callback is resumable, so this function returns 0 if an error occurs or if it |
| 709 | * needs to wait, any other value otherwise. |
| 710 | */ |
| 711 | int |
| 712 | flt_end_analyze(struct stream *s, struct channel *chn, unsigned int an_bit) |
| 713 | { |
| 714 | int ret = 1; |
| 715 | |
| 716 | /* If this function is called, this means there is at least one filter, |
| 717 | * so we do not need to check the filter list's emptiness. */ |
| 718 | |
| 719 | RESUME_FILTER_LOOP(s, chn) { |
| 720 | filter->next[CHN_IDX(chn)] = 0; |
| 721 | |
| 722 | if (filter->ops->channel_end_analyze) { |
| 723 | ret = filter->ops->channel_end_analyze(s, filter, chn); |
| 724 | if (ret <= 0) |
| 725 | BREAK_EXECUTION(s, chn, end); |
| 726 | } |
| 727 | } RESUME_FILTER_END; |
| 728 | |
| 729 | end: |
| 730 | ret = handle_analyzer_result(s, chn, an_bit, ret); |
Christopher Faulet | 02c7b22 | 2015-12-22 12:01:29 +0100 | [diff] [blame] | 731 | |
| 732 | /* Check if 'channel_end_analyze' callback has been called for the |
| 733 | * request and the response. */ |
| 734 | if (!(s->req.analysers & AN_FLT_END) && !(s->res.analysers & AN_FLT_END)) { |
Christopher Faulet | 02c7b22 | 2015-12-22 12:01:29 +0100 | [diff] [blame] | 735 | /* When we are waiting for a new request, so we must reset |
| 736 | * stream analyzers. The input must not be closed the request |
| 737 | * channel, else it is useless to wait. */ |
| 738 | if (s->txn && (s->txn->flags & TX_WAIT_NEXT_RQ) && !channel_input_closed(&s->req)) { |
| 739 | s->req.analysers = strm_li(s) ? strm_li(s)->analysers : 0; |
| 740 | s->res.analysers = 0; |
| 741 | } |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 742 | |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 743 | /* Remove backend filters from the list */ |
| 744 | flt_stream_release(s, 1); |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 745 | } |
| 746 | else if (ret) { |
| 747 | /* Analyzer ends only for one channel. So wake up the stream to |
| 748 | * be sure to process it for the other side as soon as |
| 749 | * possible. */ |
| 750 | task_wakeup(s->task, TASK_WOKEN_MSG); |
| 751 | } |
| 752 | return ret; |
| 753 | } |
| 754 | |
| 755 | |
| 756 | /* |
| 757 | * Calls 'tcp_data' callback for all "data" filters attached to a stream. This |
| 758 | * function is called when incoming data are available. It takes care to update |
| 759 | * the next offset of filters and adjusts available data to be sure that a |
| 760 | * filter cannot parse more data than its predecessors. A filter can choose to |
| 761 | * not consume all available data. Returns -1 if an error occurs, the number of |
| 762 | * consumed bytes otherwise. |
| 763 | */ |
| 764 | static int |
| 765 | flt_data(struct stream *s, struct channel *chn) |
| 766 | { |
| 767 | struct filter *filter = NULL; |
| 768 | unsigned int buf_i; |
| 769 | int ret = chn->buf->i; |
| 770 | |
| 771 | /* Save buffer state */ |
| 772 | buf_i = chn->buf->i; |
| 773 | list_for_each_entry(filter, &s->strm_flt.filters, list) { |
| 774 | if (filter->ops->tcp_data && !flt_want_forward_data(filter, chn)) { |
| 775 | ret = filter->ops->tcp_data(s, filter, chn); |
| 776 | if (ret < 0) |
| 777 | break; |
| 778 | } |
| 779 | else |
| 780 | ret = chn->buf->i - FLT_NXT(filter, chn); |
| 781 | |
| 782 | /* Increase next offset of the current filter */ |
| 783 | FLT_NXT(filter, chn) += ret; |
| 784 | |
| 785 | /* Update <ret> value to be sure to have the last one when we |
| 786 | * exit from the loop. */ |
| 787 | ret = FLT_NXT(filter, chn); |
| 788 | |
| 789 | /* And set this value as the bound for the next filter. It will |
| 790 | * not able to parse more data than the current one. */ |
| 791 | chn->buf->i = FLT_NXT(filter, chn); |
| 792 | } |
| 793 | // Restore the original buffer state |
| 794 | chn->buf->i = buf_i; |
| 795 | return ret; |
| 796 | } |
| 797 | |
| 798 | /* |
| 799 | * Calls 'tcp_forward_data' callback for all "data" filters attached to a |
| 800 | * stream. This function is called when some data can be forwarded. It takes |
| 801 | * care to update the forward offset of filters and adjusts "forwardable" data |
| 802 | * to be sure that a filter cannot forward more data than its predecessors. A |
| 803 | * filter can choose to not forward all parsed data. Returns a negative value if |
| 804 | * an error occurs, else the number of forwarded bytes. |
| 805 | */ |
| 806 | static int |
| 807 | flt_forward_data(struct stream *s, struct channel *chn, unsigned int len) |
| 808 | { |
| 809 | struct filter *filter = NULL; |
| 810 | int ret = len; |
| 811 | |
| 812 | list_for_each_entry(filter, &s->strm_flt.filters, list) { |
| 813 | if (filter->ops->tcp_forward_data) { |
| 814 | /* Remove bytes that the current filter considered as |
| 815 | * forwarded */ |
| 816 | ret = filter->ops->tcp_forward_data(s, filter, chn, ret - FLT_FWD(filter, chn)); |
| 817 | if (ret < 0) |
| 818 | goto end; |
| 819 | } |
| 820 | |
| 821 | /* Adjust bytes taht the current filter considers as |
| 822 | * forwarded */ |
| 823 | FLT_FWD(filter, chn) += ret; |
| 824 | |
| 825 | /* And set this value as the bound for the next filter. It will |
| 826 | * not able to forward more data than the current one. */ |
| 827 | ret = FLT_FWD(filter, chn); |
| 828 | } |
| 829 | |
| 830 | if (!ret) |
| 831 | goto end; |
| 832 | |
| 833 | /* Adjust forward counter and next offset of filters by removing data |
| 834 | * that HAProxy will consider as forwarded. */ |
| 835 | list_for_each_entry(filter, &s->strm_flt.filters, list) { |
| 836 | FLT_NXT(filter, chn) -= ret; |
| 837 | FLT_FWD(filter, chn) -= ret; |
| 838 | } |
| 839 | |
| 840 | /* Consume data that all filters consider as forwarded. */ |
| 841 | b_adv(chn->buf, ret); |
| 842 | end: |
| 843 | return ret; |
| 844 | } |
| 845 | |
| 846 | /* |
| 847 | * Called when TCP data must be filtered on a channel. This function is the |
| 848 | * AN_FLT_XFER_DATA analyzer. When called, it is responsible to forward data |
| 849 | * when the proxy is not in http mode. Behind the scene, it calls consecutively |
| 850 | * 'tcp_data' and 'tcp_forward_data' callbacks for all "data" filters attached |
| 851 | * to a stream. Returns 0 if an error occurs or if it needs to wait, any other |
| 852 | * value otherwise. |
| 853 | */ |
| 854 | int |
| 855 | flt_xfer_data(struct stream *s, struct channel *chn, unsigned int an_bit) |
| 856 | { |
| 857 | int ret = 1; |
| 858 | |
| 859 | /* If this function is called, this means there is at least one filter, |
| 860 | * so we do not need to check the filter list's emptiness. */ |
| 861 | |
| 862 | /* Be sure that the output is still opened. Else we stop the data |
| 863 | * filtering. */ |
| 864 | if ((chn->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) || |
| 865 | ((chn->flags & CF_SHUTW) && (chn->to_forward || chn->buf->o))) |
| 866 | goto end; |
| 867 | |
| 868 | /* Let all "data" filters parsing incoming data */ |
| 869 | ret = flt_data(s, chn); |
| 870 | if (ret < 0) |
| 871 | goto end; |
| 872 | |
| 873 | /* And forward them */ |
| 874 | ret = flt_forward_data(s, chn, ret); |
| 875 | if (ret < 0) |
| 876 | goto end; |
| 877 | |
| 878 | /* Stop waiting data if the input in closed and no data is pending or if |
| 879 | * the output is closed. */ |
| 880 | if ((chn->flags & CF_SHUTW) || |
| 881 | ((chn->flags & CF_SHUTR) && !buffer_pending(chn->buf))) { |
| 882 | ret = 1; |
| 883 | goto end; |
| 884 | } |
| 885 | |
| 886 | /* Wait for data */ |
| 887 | return 0; |
| 888 | end: |
| 889 | /* Terminate the data filtering. If <ret> is negative, an error was |
| 890 | * encountered during the filtering. */ |
| 891 | return handle_analyzer_result(s, chn, an_bit, ret); |
| 892 | } |
| 893 | |
| 894 | /* |
| 895 | * Handles result of filter's analyzers. It returns 0 if an error occurs or if |
| 896 | * it needs to wait, any other value otherwise. |
| 897 | */ |
| 898 | static int |
| 899 | handle_analyzer_result(struct stream *s, struct channel *chn, |
| 900 | unsigned int an_bit, int ret) |
| 901 | { |
| 902 | int finst; |
| 903 | |
| 904 | if (ret < 0) |
| 905 | goto return_bad_req; |
| 906 | else if (!ret) |
| 907 | goto wait; |
| 908 | |
| 909 | /* End of job, return OK */ |
| 910 | if (an_bit) { |
| 911 | chn->analysers &= ~an_bit; |
| 912 | chn->analyse_exp = TICK_ETERNITY; |
| 913 | } |
| 914 | return 1; |
| 915 | |
| 916 | return_bad_req: |
| 917 | /* An error occurs */ |
| 918 | channel_abort(&s->req); |
| 919 | channel_abort(&s->res); |
| 920 | |
| 921 | if (!(chn->flags & CF_ISRESP)) { |
| 922 | s->req.analysers &= AN_FLT_END; |
| 923 | finst = SF_FINST_R; |
| 924 | /* FIXME: incr counters */ |
| 925 | } |
| 926 | else { |
| 927 | s->res.analysers &= AN_FLT_END; |
| 928 | finst = SF_FINST_H; |
| 929 | /* FIXME: incr counters */ |
| 930 | } |
| 931 | |
| 932 | if (s->txn) { |
| 933 | /* Do not do that when we are waiting for the next request */ |
| 934 | if (s->txn->status) |
| 935 | http_reply_and_close(s, s->txn->status, NULL); |
| 936 | else { |
| 937 | s->txn->status = 400; |
| 938 | http_reply_and_close(s, 400, http_error_message(s, HTTP_ERR_400)); |
| 939 | } |
| 940 | } |
| 941 | |
| 942 | if (!(s->flags & SF_ERR_MASK)) |
| 943 | s->flags |= SF_ERR_PRXCOND; |
| 944 | if (!(s->flags & SF_FINST_MASK)) |
| 945 | s->flags |= finst; |
| 946 | return 0; |
| 947 | |
| 948 | wait: |
| 949 | if (!(chn->flags & CF_ISRESP)) |
| 950 | channel_dont_connect(chn); |
| 951 | return 0; |
| 952 | } |
| 953 | |
| 954 | |
| 955 | /* Note: must not be declared <const> as its list will be overwritten. |
| 956 | * Please take care of keeping this list alphabetically sorted, doing so helps |
| 957 | * all code contributors. |
| 958 | * Optional keywords are also declared with a NULL ->parse() function so that |
| 959 | * the config parser can report an appropriate error when a known keyword was |
| 960 | * not enabled. */ |
| 961 | static struct cfg_kw_list cfg_kws = {ILH, { |
| 962 | { CFG_LISTEN, "filter", parse_filter }, |
| 963 | { 0, NULL, NULL }, |
| 964 | } |
| 965 | }; |
| 966 | |
| 967 | __attribute__((constructor)) |
| 968 | static void |
| 969 | __filters_init(void) |
| 970 | { |
| 971 | pool2_filter = create_pool("filter", sizeof(struct filter), MEM_F_SHARED); |
| 972 | cfg_register_keywords(&cfg_kws); |
| 973 | } |
| 974 | |
| 975 | __attribute__((destructor)) |
| 976 | static void |
| 977 | __filters_deinit(void) |
| 978 | { |
| 979 | pool_destroy2(pool2_filter); |
| 980 | } |
| 981 | |
| 982 | /* |
| 983 | * Local variables: |
| 984 | * c-indent-level: 8 |
| 985 | * c-basic-offset: 8 |
| 986 | * End: |
| 987 | */ |