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 | \ |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 60 | if (strm_flt(strm)->current[CHN_IDX(chn)]) { \ |
| 61 | filter = strm_flt(strm)->current[CHN_IDX(chn)]; \ |
| 62 | strm_flt(strm)->current[CHN_IDX(chn)] = NULL; \ |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 63 | goto resume_execution; \ |
| 64 | } \ |
| 65 | \ |
Christopher Faulet | fcf035c | 2015-12-03 11:48:03 +0100 | [diff] [blame] | 66 | list_for_each_entry(filter, &strm_flt(s)->filters, list) { \ |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 67 | resume_execution: |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 68 | |
| 69 | #define RESUME_FILTER_END \ |
| 70 | } \ |
| 71 | } while(0) |
| 72 | |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 73 | #define BREAK_EXECUTION(strm, chn, label) \ |
| 74 | do { \ |
| 75 | strm_flt(strm)->current[CHN_IDX(chn)] = filter; \ |
| 76 | goto label; \ |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 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 | /* |
Christopher Faulet | b3f4e14 | 2016-03-07 12:46:38 +0100 | [diff] [blame] | 154 | * Lists the known filters on <out> |
| 155 | */ |
| 156 | void |
| 157 | list_filters(FILE *out) |
| 158 | { |
| 159 | char *filters, *p, *f; |
| 160 | |
| 161 | fprintf(out, "Available filters :\n"); |
| 162 | flt_dump_kws(&filters); |
| 163 | for (p = filters; (f = strtok_r(p,"\n",&p));) |
| 164 | fprintf(out, "\t%s\n", f); |
| 165 | free(filters); |
| 166 | } |
| 167 | |
| 168 | /* |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 169 | * Parses the "filter" keyword. All keywords must be handled by filters |
| 170 | * themselves |
| 171 | */ |
| 172 | static int |
| 173 | parse_filter(char **args, int section_type, struct proxy *curpx, |
| 174 | struct proxy *defpx, const char *file, int line, char **err) |
| 175 | { |
Christopher Faulet | 443ea1a | 2016-02-04 13:40:26 +0100 | [diff] [blame] | 176 | struct flt_conf *fconf = NULL; |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 177 | |
| 178 | /* Filter cannot be defined on a default proxy */ |
| 179 | if (curpx == defpx) { |
Christopher Faulet | cc7317d | 2016-04-04 10:51:17 +0200 | [diff] [blame] | 180 | memprintf(err, "parsing [%s:%d] : %s is not allowed in a 'default' section.", |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 181 | file, line, args[0]); |
| 182 | return -1; |
| 183 | } |
| 184 | if (!strcmp(args[0], "filter")) { |
| 185 | struct flt_kw *kw; |
| 186 | int cur_arg; |
| 187 | |
| 188 | if (!*args[1]) { |
| 189 | memprintf(err, |
| 190 | "parsing [%s:%d] : missing argument for '%s' in %s '%s'.", |
| 191 | file, line, args[0], proxy_type_str(curpx), curpx->id); |
| 192 | goto error; |
| 193 | } |
Christopher Faulet | 443ea1a | 2016-02-04 13:40:26 +0100 | [diff] [blame] | 194 | fconf = calloc(1, sizeof(*fconf)); |
| 195 | if (!fconf) { |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 196 | memprintf(err, "'%s' : out of memory", args[0]); |
| 197 | goto error; |
| 198 | } |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 199 | |
| 200 | cur_arg = 1; |
| 201 | kw = flt_find_kw(args[cur_arg]); |
| 202 | if (kw) { |
| 203 | if (!kw->parse) { |
| 204 | memprintf(err, "parsing [%s:%d] : '%s' : " |
| 205 | "'%s' option is not implemented in this version (check build options).", |
| 206 | file, line, args[0], args[cur_arg]); |
| 207 | goto error; |
| 208 | } |
Thierry Fournier | 3610c39 | 2016-04-13 18:27:51 +0200 | [diff] [blame] | 209 | if (kw->parse(args, &cur_arg, curpx, fconf, err, kw->private) != 0) { |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 210 | if (err && *err) |
| 211 | memprintf(err, "'%s' : '%s'", |
| 212 | args[0], *err); |
| 213 | else |
| 214 | memprintf(err, "'%s' : error encountered while processing '%s'", |
| 215 | args[0], args[cur_arg]); |
| 216 | goto error; |
| 217 | } |
| 218 | } |
| 219 | else { |
| 220 | flt_dump_kws(err); |
| 221 | indent_msg(err, 4); |
| 222 | memprintf(err, "'%s' : unknown keyword '%s'.%s%s", |
| 223 | args[0], args[cur_arg], |
| 224 | err && *err ? " Registered keywords :" : "", err && *err ? *err : ""); |
| 225 | goto error; |
| 226 | } |
| 227 | if (*args[cur_arg]) { |
| 228 | memprintf(err, "'%s %s' : unknown keyword '%s'.", |
| 229 | args[0], args[1], args[cur_arg]); |
| 230 | goto error; |
| 231 | } |
Christopher Faulet | 00e818a | 2016-04-19 17:00:44 +0200 | [diff] [blame] | 232 | if (fconf->ops == NULL) { |
| 233 | memprintf(err, "'%s %s' : no callbacks defined.", |
| 234 | args[0], args[1]); |
| 235 | goto error; |
| 236 | } |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 237 | |
Christopher Faulet | 443ea1a | 2016-02-04 13:40:26 +0100 | [diff] [blame] | 238 | LIST_ADDQ(&curpx->filter_configs, &fconf->list); |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 239 | } |
| 240 | return 0; |
| 241 | |
| 242 | error: |
Christopher Faulet | 443ea1a | 2016-02-04 13:40:26 +0100 | [diff] [blame] | 243 | free(fconf); |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 244 | return -1; |
| 245 | |
| 246 | |
| 247 | } |
| 248 | |
| 249 | /* |
| 250 | * Calls 'init' callback for all filters attached to a proxy. This happens after |
| 251 | * the configuration parsing. Filters can finish to fill their config. Returns |
| 252 | * (ERR_ALERT|ERR_FATAL) if an error occurs, 0 otherwise. |
| 253 | */ |
Willy Tarreau | 64bca59 | 2016-12-21 20:13:11 +0100 | [diff] [blame] | 254 | static int |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 255 | flt_init(struct proxy *proxy) |
| 256 | { |
Christopher Faulet | 443ea1a | 2016-02-04 13:40:26 +0100 | [diff] [blame] | 257 | struct flt_conf *fconf; |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 258 | |
Christopher Faulet | 443ea1a | 2016-02-04 13:40:26 +0100 | [diff] [blame] | 259 | list_for_each_entry(fconf, &proxy->filter_configs, list) { |
| 260 | if (fconf->ops->init && fconf->ops->init(proxy, fconf) < 0) |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 261 | return ERR_ALERT|ERR_FATAL; |
| 262 | } |
| 263 | return 0; |
| 264 | } |
| 265 | |
Willy Tarreau | 64bca59 | 2016-12-21 20:13:11 +0100 | [diff] [blame] | 266 | /* Calls flt_init() for all proxies, see above */ |
| 267 | static int |
| 268 | flt_init_all() |
| 269 | { |
| 270 | struct proxy *px; |
| 271 | int err_code = 0; |
| 272 | |
| 273 | for (px = proxy; px; px = px->next) { |
| 274 | err_code |= flt_init(px); |
| 275 | if (err_code & (ERR_ABORT|ERR_FATAL)) { |
| 276 | Alert("Failed to initialize filters for proxy '%s'.\n", |
| 277 | px->id); |
| 278 | return err_code; |
| 279 | } |
| 280 | } |
| 281 | return 0; |
| 282 | } |
| 283 | |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 284 | /* |
| 285 | * Calls 'check' callback for all filters attached to a proxy. This happens |
| 286 | * after the configuration parsing but before filters initialization. Returns |
| 287 | * the number of encountered errors. |
| 288 | */ |
| 289 | int |
| 290 | flt_check(struct proxy *proxy) |
| 291 | { |
Christopher Faulet | 443ea1a | 2016-02-04 13:40:26 +0100 | [diff] [blame] | 292 | struct flt_conf *fconf; |
| 293 | int err = 0; |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 294 | |
Christopher Faulet | 443ea1a | 2016-02-04 13:40:26 +0100 | [diff] [blame] | 295 | list_for_each_entry(fconf, &proxy->filter_configs, list) { |
| 296 | if (fconf->ops->check) |
| 297 | err += fconf->ops->check(proxy, fconf); |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 298 | } |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 299 | err += check_legacy_http_comp_flt(proxy); |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 300 | return err; |
| 301 | } |
| 302 | |
| 303 | /* |
| 304 | * Calls 'denit' callback for all filters attached to a proxy. This happens when |
| 305 | * HAProxy is stopped. |
| 306 | */ |
| 307 | void |
| 308 | flt_deinit(struct proxy *proxy) |
| 309 | { |
Christopher Faulet | 443ea1a | 2016-02-04 13:40:26 +0100 | [diff] [blame] | 310 | struct flt_conf *fconf, *back; |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 311 | |
Christopher Faulet | 443ea1a | 2016-02-04 13:40:26 +0100 | [diff] [blame] | 312 | list_for_each_entry_safe(fconf, back, &proxy->filter_configs, list) { |
| 313 | if (fconf->ops->deinit) |
| 314 | fconf->ops->deinit(proxy, fconf); |
| 315 | LIST_DEL(&fconf->list); |
| 316 | free(fconf); |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 317 | } |
| 318 | } |
| 319 | |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 320 | /* Attaches a filter to a stream. Returns -1 if an error occurs, 0 otherwise. */ |
| 321 | static int |
Christopher Faulet | 443ea1a | 2016-02-04 13:40:26 +0100 | [diff] [blame] | 322 | flt_stream_add_filter(struct stream *s, struct flt_conf *fconf, unsigned int flags) |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 323 | { |
| 324 | struct filter *f = pool_alloc2(pool2_filter); |
Christopher Faulet | 31ed32d | 2016-06-21 11:42:37 +0200 | [diff] [blame] | 325 | |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 326 | if (!f) /* not enough memory */ |
| 327 | return -1; |
| 328 | memset(f, 0, sizeof(*f)); |
Christopher Faulet | 443ea1a | 2016-02-04 13:40:26 +0100 | [diff] [blame] | 329 | f->config = fconf; |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 330 | f->flags |= flags; |
Christopher Faulet | 31ed32d | 2016-06-21 11:42:37 +0200 | [diff] [blame] | 331 | |
| 332 | if (FLT_OPS(f)->attach) { |
| 333 | int ret = FLT_OPS(f)->attach(s, f); |
| 334 | if (ret <= 0) { |
| 335 | pool_free2(pool2_filter, f); |
| 336 | return ret; |
| 337 | } |
| 338 | } |
| 339 | |
Christopher Faulet | fcf035c | 2015-12-03 11:48:03 +0100 | [diff] [blame] | 340 | LIST_ADDQ(&strm_flt(s)->filters, &f->list); |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 341 | strm_flt(s)->flags |= STRM_FLT_FL_HAS_FILTERS; |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 342 | return 0; |
| 343 | } |
| 344 | |
| 345 | /* |
| 346 | * Called when a stream is created. It attaches all frontend filters to the |
| 347 | * stream. Returns -1 if an error occurs, 0 otherwise. |
| 348 | */ |
| 349 | int |
| 350 | flt_stream_init(struct stream *s) |
| 351 | { |
Christopher Faulet | 443ea1a | 2016-02-04 13:40:26 +0100 | [diff] [blame] | 352 | struct flt_conf *fconf; |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 353 | |
Christopher Faulet | fcf035c | 2015-12-03 11:48:03 +0100 | [diff] [blame] | 354 | memset(strm_flt(s), 0, sizeof(*strm_flt(s))); |
| 355 | LIST_INIT(&strm_flt(s)->filters); |
Christopher Faulet | 443ea1a | 2016-02-04 13:40:26 +0100 | [diff] [blame] | 356 | list_for_each_entry(fconf, &strm_fe(s)->filter_configs, list) { |
| 357 | if (flt_stream_add_filter(s, fconf, 0) < 0) |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 358 | return -1; |
| 359 | } |
| 360 | return 0; |
| 361 | } |
| 362 | |
| 363 | /* |
| 364 | * Called when a stream is closed or when analyze ends (For an HTTP stream, this |
| 365 | * happens after each request/response exchange). When analyze ends, backend |
| 366 | * filters are removed. When the stream is closed, all filters attached to the |
| 367 | * stream are removed. |
| 368 | */ |
| 369 | void |
| 370 | flt_stream_release(struct stream *s, int only_backend) |
| 371 | { |
| 372 | struct filter *filter, *back; |
| 373 | |
Christopher Faulet | fcf035c | 2015-12-03 11:48:03 +0100 | [diff] [blame] | 374 | list_for_each_entry_safe(filter, back, &strm_flt(s)->filters, list) { |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 375 | if (!only_backend || (filter->flags & FLT_FL_IS_BACKEND_FILTER)) { |
Christopher Faulet | 31ed32d | 2016-06-21 11:42:37 +0200 | [diff] [blame] | 376 | if (FLT_OPS(filter)->detach) |
| 377 | FLT_OPS(filter)->detach(s, filter); |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 378 | LIST_DEL(&filter->list); |
| 379 | pool_free2(pool2_filter, filter); |
| 380 | } |
| 381 | } |
Christopher Faulet | fcf035c | 2015-12-03 11:48:03 +0100 | [diff] [blame] | 382 | if (LIST_ISEMPTY(&strm_flt(s)->filters)) |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 383 | strm_flt(s)->flags &= ~STRM_FLT_FL_HAS_FILTERS; |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 384 | } |
| 385 | |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 386 | /* |
| 387 | * Calls 'stream_start' for all filters attached to a stream. This happens when |
| 388 | * the stream is created, just after calling flt_stream_init |
| 389 | * function. Returns -1 if an error occurs, 0 otherwise. |
| 390 | */ |
| 391 | int |
| 392 | flt_stream_start(struct stream *s) |
| 393 | { |
| 394 | struct filter *filter; |
| 395 | |
Christopher Faulet | fcf035c | 2015-12-03 11:48:03 +0100 | [diff] [blame] | 396 | list_for_each_entry(filter, &strm_flt(s)->filters, list) { |
Christopher Faulet | 443ea1a | 2016-02-04 13:40:26 +0100 | [diff] [blame] | 397 | if (FLT_OPS(filter)->stream_start && FLT_OPS(filter)->stream_start(s, filter) < 0) |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 398 | return -1; |
| 399 | } |
| 400 | return 0; |
| 401 | } |
| 402 | |
| 403 | /* |
| 404 | * Calls 'stream_stop' for all filters attached to a stream. This happens when |
| 405 | * the stream is stopped, just before calling flt_stream_release function. |
| 406 | */ |
| 407 | void |
| 408 | flt_stream_stop(struct stream *s) |
| 409 | { |
| 410 | struct filter *filter; |
| 411 | |
Christopher Faulet | fcf035c | 2015-12-03 11:48:03 +0100 | [diff] [blame] | 412 | list_for_each_entry(filter, &strm_flt(s)->filters, list) { |
Christopher Faulet | 443ea1a | 2016-02-04 13:40:26 +0100 | [diff] [blame] | 413 | if (FLT_OPS(filter)->stream_stop) |
| 414 | FLT_OPS(filter)->stream_stop(s, filter); |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 415 | } |
| 416 | } |
| 417 | |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 418 | /* |
Christopher Faulet | a00d817 | 2016-11-10 14:58:05 +0100 | [diff] [blame] | 419 | * Calls 'check_timeouts' for all filters attached to a stream. This happens when |
| 420 | * the stream is woken up because of expired timer. |
| 421 | */ |
| 422 | void |
| 423 | flt_stream_check_timeouts(struct stream *s) |
| 424 | { |
| 425 | struct filter *filter; |
| 426 | |
| 427 | list_for_each_entry(filter, &strm_flt(s)->filters, list) { |
| 428 | if (FLT_OPS(filter)->check_timeouts) |
| 429 | FLT_OPS(filter)->check_timeouts(s, filter); |
| 430 | } |
| 431 | } |
| 432 | |
| 433 | /* |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 434 | * Called when a backend is set for a stream. If the frontend and the backend |
Christopher Faulet | 31ed32d | 2016-06-21 11:42:37 +0200 | [diff] [blame] | 435 | * are not the same, this function attaches all backend filters to the |
| 436 | * stream. Returns -1 if an error occurs, 0 otherwise. |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 437 | */ |
| 438 | int |
| 439 | flt_set_stream_backend(struct stream *s, struct proxy *be) |
| 440 | { |
Christopher Faulet | 443ea1a | 2016-02-04 13:40:26 +0100 | [diff] [blame] | 441 | struct flt_conf *fconf; |
Christopher Faulet | 31ed32d | 2016-06-21 11:42:37 +0200 | [diff] [blame] | 442 | struct filter *filter; |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 443 | |
| 444 | if (strm_fe(s) == be) |
Christopher Faulet | 31ed32d | 2016-06-21 11:42:37 +0200 | [diff] [blame] | 445 | goto end; |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 446 | |
Christopher Faulet | 443ea1a | 2016-02-04 13:40:26 +0100 | [diff] [blame] | 447 | list_for_each_entry(fconf, &be->filter_configs, list) { |
| 448 | if (flt_stream_add_filter(s, fconf, FLT_FL_IS_BACKEND_FILTER) < 0) |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 449 | return -1; |
| 450 | } |
Christopher Faulet | 31ed32d | 2016-06-21 11:42:37 +0200 | [diff] [blame] | 451 | |
| 452 | end: |
| 453 | list_for_each_entry(filter, &strm_flt(s)->filters, list) { |
| 454 | if (FLT_OPS(filter)->stream_set_backend && |
| 455 | FLT_OPS(filter)->stream_set_backend(s, filter, be) < 0) |
| 456 | return -1; |
| 457 | } |
| 458 | |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 459 | return 0; |
| 460 | } |
| 461 | |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 462 | /* |
| 463 | * Calls 'http_data' callback for all "data" filters attached to a stream. This |
| 464 | * function is called when incoming data are available (excluding chunks |
| 465 | * envelope for chunked messages) in the AN_REQ_HTTP_XFER_BODY and |
| 466 | * AN_RES_HTTP_XFER_BODY analyzers. It takes care to update the next offset of |
| 467 | * filters and adjusts available data to be sure that a filter cannot parse more |
| 468 | * data than its predecessors. A filter can choose to not consume all available |
| 469 | * data. Returns -1 if an error occurs, the number of consumed bytes otherwise. |
| 470 | */ |
| 471 | int |
| 472 | flt_http_data(struct stream *s, struct http_msg *msg) |
| 473 | { |
Christopher Faulet | fcf035c | 2015-12-03 11:48:03 +0100 | [diff] [blame] | 474 | struct filter *filter; |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 475 | unsigned int buf_i; |
Christopher Faulet | 55048a4 | 2016-06-21 10:44:32 +0200 | [diff] [blame] | 476 | int delta = 0, ret = 0; |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 477 | |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 478 | /* Save buffer state */ |
Christopher Faulet | 55048a4 | 2016-06-21 10:44:32 +0200 | [diff] [blame] | 479 | buf_i = msg->chn->buf->i; |
Christopher Faulet | fcf035c | 2015-12-03 11:48:03 +0100 | [diff] [blame] | 480 | |
Christopher Faulet | fcf035c | 2015-12-03 11:48:03 +0100 | [diff] [blame] | 481 | list_for_each_entry(filter, &strm_flt(s)->filters, list) { |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 482 | unsigned int *nxt; |
| 483 | |
| 484 | /* Call "data" filters only */ |
| 485 | if (!IS_DATA_FILTER(filter, msg->chn)) |
| 486 | continue; |
| 487 | |
Christopher Faulet | 2fb2880 | 2015-12-01 10:40:57 +0100 | [diff] [blame] | 488 | /* If the HTTP parser is ahead, we update the next offset of the |
| 489 | * current filter. This happens for chunked messages, at the |
| 490 | * begining of a new chunk. */ |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 491 | nxt = &FLT_NXT(filter, msg->chn); |
| 492 | if (msg->next > *nxt) |
| 493 | *nxt = msg->next; |
| 494 | |
Christopher Faulet | 443ea1a | 2016-02-04 13:40:26 +0100 | [diff] [blame] | 495 | if (FLT_OPS(filter)->http_data) { |
Christopher Faulet | 55048a4 | 2016-06-21 10:44:32 +0200 | [diff] [blame] | 496 | unsigned int i = msg->chn->buf->i; |
| 497 | |
Christopher Faulet | 443ea1a | 2016-02-04 13:40:26 +0100 | [diff] [blame] | 498 | ret = FLT_OPS(filter)->http_data(s, filter, msg); |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 499 | if (ret < 0) |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 500 | break; |
Christopher Faulet | 55048a4 | 2016-06-21 10:44:32 +0200 | [diff] [blame] | 501 | delta += (int)(msg->chn->buf->i - i); |
Christopher Faulet | fcf035c | 2015-12-03 11:48:03 +0100 | [diff] [blame] | 502 | |
| 503 | /* Update the next offset of the current filter */ |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 504 | *nxt += ret; |
Christopher Faulet | fcf035c | 2015-12-03 11:48:03 +0100 | [diff] [blame] | 505 | |
| 506 | /* And set this value as the bound for the next |
| 507 | * filter. It will not able to parse more data than this |
| 508 | * one. */ |
Christopher Faulet | 55048a4 | 2016-06-21 10:44:32 +0200 | [diff] [blame] | 509 | msg->chn->buf->i = *nxt; |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 510 | } |
| 511 | else { |
Christopher Faulet | fcf035c | 2015-12-03 11:48:03 +0100 | [diff] [blame] | 512 | /* Consume all available data and update the next offset |
| 513 | * of the current filter. buf->i is untouched here. */ |
Christopher Faulet | 55048a4 | 2016-06-21 10:44:32 +0200 | [diff] [blame] | 514 | ret = MIN(msg->chunk_len + msg->next, msg->chn->buf->i) - *nxt; |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 515 | *nxt += ret; |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 516 | } |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 517 | } |
Christopher Faulet | fcf035c | 2015-12-03 11:48:03 +0100 | [diff] [blame] | 518 | |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 519 | /* Restore the original buffer state */ |
Christopher Faulet | 55048a4 | 2016-06-21 10:44:32 +0200 | [diff] [blame] | 520 | msg->chn->buf->i = buf_i + delta; |
Christopher Faulet | fcf035c | 2015-12-03 11:48:03 +0100 | [diff] [blame] | 521 | |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 522 | return ret; |
| 523 | } |
| 524 | |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 525 | /* |
| 526 | * Calls 'http_chunk_trailers' callback for all "data" filters attached to a |
| 527 | * stream. This function is called for chunked messages only when a part of the |
| 528 | * trailers was parsed in the AN_REQ_HTTP_XFER_BODY and AN_RES_HTTP_XFER_BODY |
| 529 | * analyzers. Filters can know how much data were parsed by the HTTP parsing |
| 530 | * until the last call with the msg->sol value. Returns a negative value if an |
| 531 | * error occurs, any other value otherwise. |
| 532 | */ |
| 533 | int |
| 534 | flt_http_chunk_trailers(struct stream *s, struct http_msg *msg) |
| 535 | { |
Christopher Faulet | 2fb2880 | 2015-12-01 10:40:57 +0100 | [diff] [blame] | 536 | struct filter *filter; |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 537 | int ret = 1; |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 538 | |
Christopher Faulet | fcf035c | 2015-12-03 11:48:03 +0100 | [diff] [blame] | 539 | list_for_each_entry(filter, &strm_flt(s)->filters, list) { |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 540 | unsigned int *nxt; |
| 541 | |
| 542 | /* Call "data" filters only */ |
| 543 | if (!IS_DATA_FILTER(filter, msg->chn)) |
| 544 | continue; |
| 545 | |
Christopher Faulet | 2fb2880 | 2015-12-01 10:40:57 +0100 | [diff] [blame] | 546 | /* Be sure to set the next offset of the filter at the right |
| 547 | * place. This is really useful when the first part of the |
| 548 | * trailers was parsed. */ |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 549 | nxt = &FLT_NXT(filter, msg->chn); |
| 550 | *nxt = msg->next; |
| 551 | |
Christopher Faulet | 443ea1a | 2016-02-04 13:40:26 +0100 | [diff] [blame] | 552 | if (FLT_OPS(filter)->http_chunk_trailers) { |
| 553 | ret = FLT_OPS(filter)->http_chunk_trailers(s, filter, msg); |
Christopher Faulet | 2fb2880 | 2015-12-01 10:40:57 +0100 | [diff] [blame] | 554 | if (ret < 0) |
| 555 | break; |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 556 | } |
Christopher Faulet | 2fb2880 | 2015-12-01 10:40:57 +0100 | [diff] [blame] | 557 | /* Update the next offset of the current filter. Here all data |
| 558 | * are always consumed. */ |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 559 | *nxt += msg->sol; |
Christopher Faulet | 2fb2880 | 2015-12-01 10:40:57 +0100 | [diff] [blame] | 560 | } |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 561 | return ret; |
| 562 | } |
| 563 | |
| 564 | /* |
| 565 | * Calls 'http_end' callback for all filters attached to a stream. All filters |
| 566 | * are called here, but only if there is at least one "data" filter. This |
| 567 | * functions is called when all data were parsed and forwarded. 'http_end' |
| 568 | * callback is resumable, so this function returns a negative value if an error |
| 569 | * occurs, 0 if it needs to wait for some reason, any other value otherwise. |
| 570 | */ |
| 571 | int |
| 572 | flt_http_end(struct stream *s, struct http_msg *msg) |
| 573 | { |
| 574 | int ret = 1; |
| 575 | |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 576 | RESUME_FILTER_LOOP(s, msg->chn) { |
Christopher Faulet | 443ea1a | 2016-02-04 13:40:26 +0100 | [diff] [blame] | 577 | if (FLT_OPS(filter)->http_end) { |
| 578 | ret = FLT_OPS(filter)->http_end(s, filter, msg); |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 579 | if (ret <= 0) |
| 580 | BREAK_EXECUTION(s, msg->chn, end); |
| 581 | } |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 582 | } RESUME_FILTER_END; |
| 583 | end: |
| 584 | return ret; |
| 585 | } |
| 586 | |
| 587 | /* |
| 588 | * Calls 'http_reset' callback for all filters attached to a stream. This |
| 589 | * happens when a 100-continue response is received. |
| 590 | */ |
| 591 | void |
| 592 | flt_http_reset(struct stream *s, struct http_msg *msg) |
| 593 | { |
| 594 | struct filter *filter; |
| 595 | |
Christopher Faulet | fcf035c | 2015-12-03 11:48:03 +0100 | [diff] [blame] | 596 | list_for_each_entry(filter, &strm_flt(s)->filters, list) { |
Christopher Faulet | 443ea1a | 2016-02-04 13:40:26 +0100 | [diff] [blame] | 597 | if (FLT_OPS(filter)->http_reset) |
| 598 | FLT_OPS(filter)->http_reset(s, filter, msg); |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 599 | } |
| 600 | } |
| 601 | |
| 602 | /* |
| 603 | * Calls 'http_reply' callback for all filters attached to a stream when HA |
| 604 | * decides to stop the HTTP message processing. |
| 605 | */ |
| 606 | void |
| 607 | flt_http_reply(struct stream *s, short status, const struct chunk *msg) |
| 608 | { |
| 609 | struct filter *filter; |
| 610 | |
Christopher Faulet | fcf035c | 2015-12-03 11:48:03 +0100 | [diff] [blame] | 611 | list_for_each_entry(filter, &strm_flt(s)->filters, list) { |
Christopher Faulet | 443ea1a | 2016-02-04 13:40:26 +0100 | [diff] [blame] | 612 | if (FLT_OPS(filter)->http_reply) |
| 613 | FLT_OPS(filter)->http_reply(s, filter, status, msg); |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 614 | } |
| 615 | } |
| 616 | |
| 617 | /* |
| 618 | * Calls 'http_forward_data' callback for all "data" filters attached to a |
| 619 | * stream. This function is called when some data can be forwarded in the |
| 620 | * AN_REQ_HTTP_XFER_BODY and AN_RES_HTTP_XFER_BODY analyzers. It takes care to |
| 621 | * update the forward offset of filters and adjusts "forwardable" data to be |
| 622 | * sure that a filter cannot forward more data than its predecessors. A filter |
| 623 | * can choose to not forward all parsed data. Returns a negative value if an |
| 624 | * error occurs, else the number of forwarded bytes. |
| 625 | */ |
| 626 | int |
| 627 | flt_http_forward_data(struct stream *s, struct http_msg *msg, unsigned int len) |
| 628 | { |
Christopher Faulet | fcf035c | 2015-12-03 11:48:03 +0100 | [diff] [blame] | 629 | struct filter *filter; |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 630 | int ret = len; |
| 631 | |
Christopher Faulet | fcf035c | 2015-12-03 11:48:03 +0100 | [diff] [blame] | 632 | list_for_each_entry(filter, &strm_flt(s)->filters, list) { |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 633 | unsigned int *nxt, *fwd; |
| 634 | |
| 635 | /* Call "data" filters only */ |
| 636 | if (!IS_DATA_FILTER(filter, msg->chn)) |
| 637 | continue; |
| 638 | |
Christopher Faulet | 2fb2880 | 2015-12-01 10:40:57 +0100 | [diff] [blame] | 639 | /* If the HTTP parser is ahead, we update the next offset of the |
| 640 | * current filter. This happens for chunked messages, when the |
| 641 | * chunk envelope is parsed. */ |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 642 | nxt = &FLT_NXT(filter, msg->chn); |
| 643 | fwd = &FLT_FWD(filter, msg->chn); |
| 644 | if (msg->next > *nxt) |
| 645 | *nxt = msg->next; |
| 646 | |
Christopher Faulet | 443ea1a | 2016-02-04 13:40:26 +0100 | [diff] [blame] | 647 | if (FLT_OPS(filter)->http_forward_data) { |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 648 | /* Remove bytes that the current filter considered as |
| 649 | * forwarded */ |
Christopher Faulet | 443ea1a | 2016-02-04 13:40:26 +0100 | [diff] [blame] | 650 | ret = FLT_OPS(filter)->http_forward_data(s, filter, msg, ret - *fwd); |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 651 | if (ret < 0) |
| 652 | goto end; |
| 653 | } |
| 654 | |
| 655 | /* Adjust bytes that the current filter considers as |
| 656 | * forwarded */ |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 657 | *fwd += ret; |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 658 | |
| 659 | /* And set this value as the bound for the next filter. It will |
| 660 | * not able to forward more data than the current one. */ |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 661 | ret = *fwd; |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 662 | } |
| 663 | |
| 664 | if (!ret) |
| 665 | goto end; |
| 666 | |
| 667 | /* Finally, adjust filters offsets by removing data that HAProxy will |
| 668 | * forward. */ |
Christopher Faulet | fcf035c | 2015-12-03 11:48:03 +0100 | [diff] [blame] | 669 | list_for_each_entry(filter, &strm_flt(s)->filters, list) { |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 670 | if (!IS_DATA_FILTER(filter, msg->chn)) |
| 671 | continue; |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 672 | FLT_NXT(filter, msg->chn) -= ret; |
| 673 | FLT_FWD(filter, msg->chn) -= ret; |
| 674 | } |
| 675 | end: |
| 676 | return ret; |
| 677 | } |
| 678 | |
| 679 | /* |
| 680 | * Calls 'channel_start_analyze' callback for all filters attached to a |
| 681 | * stream. This function is called when we start to analyze a request or a |
| 682 | * response. For frontend filters, it is called before all other analyzers. For |
| 683 | * backend ones, it is called before all backend |
| 684 | * analyzers. 'channel_start_analyze' callback is resumable, so this function |
| 685 | * returns 0 if an error occurs or if it needs to wait, any other value |
| 686 | * otherwise. |
| 687 | */ |
| 688 | int |
| 689 | flt_start_analyze(struct stream *s, struct channel *chn, unsigned int an_bit) |
| 690 | { |
| 691 | int ret = 1; |
| 692 | |
| 693 | /* If this function is called, this means there is at least one filter, |
| 694 | * so we do not need to check the filter list's emptiness. */ |
| 695 | |
Christopher Faulet | e600624 | 2017-03-10 11:52:44 +0100 | [diff] [blame] | 696 | /* Set flag on channel to tell that the channel is filtered */ |
| 697 | chn->flags |= CF_FLT_ANALYZE; |
| 698 | |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 699 | RESUME_FILTER_LOOP(s, chn) { |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 700 | if (!(chn->flags & CF_ISRESP)) { |
| 701 | if (an_bit == AN_REQ_FLT_START_BE && |
| 702 | !(filter->flags & FLT_FL_IS_BACKEND_FILTER)) |
| 703 | continue; |
| 704 | } |
| 705 | else { |
| 706 | if (an_bit == AN_RES_FLT_START_BE && |
| 707 | !(filter->flags & FLT_FL_IS_BACKEND_FILTER)) |
| 708 | continue; |
| 709 | } |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 710 | |
Christopher Faulet | fcf035c | 2015-12-03 11:48:03 +0100 | [diff] [blame] | 711 | FLT_NXT(filter, chn) = 0; |
| 712 | FLT_FWD(filter, chn) = 0; |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 713 | |
Christopher Faulet | 443ea1a | 2016-02-04 13:40:26 +0100 | [diff] [blame] | 714 | if (FLT_OPS(filter)->channel_start_analyze) { |
| 715 | ret = FLT_OPS(filter)->channel_start_analyze(s, filter, chn); |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 716 | if (ret <= 0) |
| 717 | BREAK_EXECUTION(s, chn, end); |
| 718 | } |
| 719 | } RESUME_FILTER_END; |
| 720 | |
| 721 | end: |
| 722 | return handle_analyzer_result(s, chn, an_bit, ret); |
| 723 | } |
| 724 | |
| 725 | /* |
Christopher Faulet | 3a394fa | 2016-05-11 17:13:39 +0200 | [diff] [blame] | 726 | * Calls 'channel_pre_analyze' callback for all filters attached to a |
| 727 | * stream. This function is called BEFORE each analyzer attached to a channel, |
| 728 | * expects analyzers responsible for data sending. 'channel_pre_analyze' |
| 729 | * callback is resumable, so this function returns 0 if an error occurs or if it |
| 730 | * needs to wait, any other value otherwise. |
| 731 | * |
| 732 | * Note this function can be called many times for the same analyzer. In fact, |
| 733 | * it is called until the analyzer finishes its processing. |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 734 | */ |
| 735 | int |
Christopher Faulet | 3a394fa | 2016-05-11 17:13:39 +0200 | [diff] [blame] | 736 | flt_pre_analyze(struct stream *s, struct channel *chn, unsigned int an_bit) |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 737 | { |
| 738 | int ret = 1; |
| 739 | |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 740 | RESUME_FILTER_LOOP(s, chn) { |
Christopher Faulet | 3a394fa | 2016-05-11 17:13:39 +0200 | [diff] [blame] | 741 | if (FLT_OPS(filter)->channel_pre_analyze && (filter->pre_analyzers & an_bit)) { |
| 742 | ret = FLT_OPS(filter)->channel_pre_analyze(s, filter, chn, an_bit); |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 743 | if (ret <= 0) |
| 744 | BREAK_EXECUTION(s, chn, check_result); |
| 745 | } |
| 746 | } RESUME_FILTER_END; |
| 747 | |
| 748 | check_result: |
Christopher Faulet | 309c641 | 2015-12-02 09:57:32 +0100 | [diff] [blame] | 749 | return handle_analyzer_result(s, chn, 0, ret); |
| 750 | } |
| 751 | |
| 752 | /* |
Christopher Faulet | 3a394fa | 2016-05-11 17:13:39 +0200 | [diff] [blame] | 753 | * Calls 'channel_post_analyze' callback for all filters attached to a |
| 754 | * stream. This function is called AFTER each analyzer attached to a channel, |
| 755 | * expects analyzers responsible for data sending. 'channel_post_analyze' |
| 756 | * callback is NOT resumable, so this function returns a 0 if an error occurs, |
| 757 | * any other value otherwise. |
| 758 | * |
| 759 | * Here, AFTER means when the analyzer finishes its processing. |
| 760 | */ |
| 761 | int |
| 762 | flt_post_analyze(struct stream *s, struct channel *chn, unsigned int an_bit) |
| 763 | { |
| 764 | struct filter *filter; |
| 765 | int ret = 1; |
| 766 | |
| 767 | list_for_each_entry(filter, &strm_flt(s)->filters, list) { |
| 768 | if (FLT_OPS(filter)->channel_post_analyze && (filter->post_analyzers & an_bit)) { |
| 769 | ret = FLT_OPS(filter)->channel_post_analyze(s, filter, chn, an_bit); |
| 770 | if (ret < 0) |
| 771 | break; |
| 772 | } |
| 773 | } |
| 774 | return handle_analyzer_result(s, chn, 0, ret); |
| 775 | } |
| 776 | |
| 777 | /* |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 778 | * This function is the AN_REQ/RES_FLT_HTTP_HDRS analyzer, used to filter HTTP |
| 779 | * headers or a request or a response. Returns 0 if an error occurs or if it |
| 780 | * needs to wait, any other value otherwise. |
Christopher Faulet | 309c641 | 2015-12-02 09:57:32 +0100 | [diff] [blame] | 781 | */ |
| 782 | int |
| 783 | flt_analyze_http_headers(struct stream *s, struct channel *chn, unsigned int an_bit) |
| 784 | { |
Christopher Faulet | 1339d74 | 2016-05-11 16:48:33 +0200 | [diff] [blame] | 785 | struct filter *filter; |
| 786 | struct http_msg *msg; |
| 787 | int ret = 1; |
Christopher Faulet | 309c641 | 2015-12-02 09:57:32 +0100 | [diff] [blame] | 788 | |
Christopher Faulet | 1339d74 | 2016-05-11 16:48:33 +0200 | [diff] [blame] | 789 | msg = ((chn->flags & CF_ISRESP) ? &s->txn->rsp : &s->txn->req); |
Christopher Faulet | 309c641 | 2015-12-02 09:57:32 +0100 | [diff] [blame] | 790 | RESUME_FILTER_LOOP(s, chn) { |
Christopher Faulet | 1339d74 | 2016-05-11 16:48:33 +0200 | [diff] [blame] | 791 | if (FLT_OPS(filter)->http_headers) { |
| 792 | ret = FLT_OPS(filter)->http_headers(s, filter, msg); |
Christopher Faulet | 309c641 | 2015-12-02 09:57:32 +0100 | [diff] [blame] | 793 | if (ret <= 0) |
| 794 | BREAK_EXECUTION(s, chn, check_result); |
| 795 | } |
| 796 | } RESUME_FILTER_END; |
| 797 | |
| 798 | /* We increase next offset of all "data" filters after all processing on |
| 799 | * headers because any filter can alter them. So the definitive size of |
| 800 | * headers (msg->sov) is only known when all filters have been |
| 801 | * called. */ |
Christopher Faulet | fcf035c | 2015-12-03 11:48:03 +0100 | [diff] [blame] | 802 | list_for_each_entry(filter, &strm_flt(s)->filters, list) { |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 803 | /* Handle "data" filters only */ |
| 804 | if (!IS_DATA_FILTER(filter, chn)) |
| 805 | continue; |
Christopher Faulet | 1339d74 | 2016-05-11 16:48:33 +0200 | [diff] [blame] | 806 | FLT_NXT(filter, chn) = msg->sov; |
Christopher Faulet | 309c641 | 2015-12-02 09:57:32 +0100 | [diff] [blame] | 807 | } |
| 808 | |
| 809 | check_result: |
| 810 | return handle_analyzer_result(s, chn, an_bit, ret); |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 811 | } |
| 812 | |
| 813 | /* |
| 814 | * Calls 'channel_end_analyze' callback for all filters attached to a |
| 815 | * stream. This function is called when we stop to analyze a request or a |
| 816 | * response. It is called after all other analyzers. 'channel_end_analyze' |
| 817 | * callback is resumable, so this function returns 0 if an error occurs or if it |
| 818 | * needs to wait, any other value otherwise. |
| 819 | */ |
| 820 | int |
| 821 | flt_end_analyze(struct stream *s, struct channel *chn, unsigned int an_bit) |
| 822 | { |
| 823 | int ret = 1; |
| 824 | |
Christopher Faulet | e600624 | 2017-03-10 11:52:44 +0100 | [diff] [blame] | 825 | /* Check if all filters attached on the stream have finished their |
| 826 | * processing on this channel. */ |
| 827 | if (!(chn->flags & CF_FLT_ANALYZE)) |
| 828 | goto sync; |
| 829 | |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 830 | RESUME_FILTER_LOOP(s, chn) { |
Christopher Faulet | fcf035c | 2015-12-03 11:48:03 +0100 | [diff] [blame] | 831 | FLT_NXT(filter, chn) = 0; |
| 832 | FLT_FWD(filter, chn) = 0; |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 833 | unregister_data_filter(s, chn, filter); |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 834 | |
Christopher Faulet | 443ea1a | 2016-02-04 13:40:26 +0100 | [diff] [blame] | 835 | if (FLT_OPS(filter)->channel_end_analyze) { |
| 836 | ret = FLT_OPS(filter)->channel_end_analyze(s, filter, chn); |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 837 | if (ret <= 0) |
| 838 | BREAK_EXECUTION(s, chn, end); |
| 839 | } |
| 840 | } RESUME_FILTER_END; |
| 841 | |
Christopher Faulet | e600624 | 2017-03-10 11:52:44 +0100 | [diff] [blame] | 842 | end: |
| 843 | /* We don't remove yet this analyzer because we need to synchronize the |
| 844 | * both channels. So here, we just remove the flag CF_FLT_ANALYZE. */ |
| 845 | ret = handle_analyzer_result(s, chn, 0, ret); |
Christopher Faulet | 570f799 | 2017-07-06 15:53:02 +0200 | [diff] [blame] | 846 | if (ret) { |
Christopher Faulet | e600624 | 2017-03-10 11:52:44 +0100 | [diff] [blame] | 847 | chn->flags &= ~CF_FLT_ANALYZE; |
Christopher Faulet | 02c7b22 | 2015-12-22 12:01:29 +0100 | [diff] [blame] | 848 | |
Christopher Faulet | 570f799 | 2017-07-06 15:53:02 +0200 | [diff] [blame] | 849 | /* Pretend there is an activity on both channels. Flag on the |
| 850 | * current one will be automatically removed, so only the other |
| 851 | * one will remain. This is a way to be sure that |
| 852 | * 'channel_end_analyze' callback will have a chance to be |
| 853 | * called at least once for the other side to finish the current |
| 854 | * processing. Of course, this is the filter responsiblity to |
| 855 | * wakeup the stream if it choose to loop on this callback. */ |
| 856 | s->req.flags |= CF_WAKE_ONCE; |
| 857 | s->res.flags |= CF_WAKE_ONCE; |
| 858 | } |
| 859 | |
| 860 | |
Christopher Faulet | e600624 | 2017-03-10 11:52:44 +0100 | [diff] [blame] | 861 | sync: |
| 862 | /* Now we can check if filters have finished their work on the both |
| 863 | * channels */ |
| 864 | if (!(s->req.flags & CF_FLT_ANALYZE) && !(s->res.flags & CF_FLT_ANALYZE)) { |
| 865 | /* Sync channels by removing this analyzer for the both channels */ |
| 866 | s->req.analysers &= ~AN_REQ_FLT_END; |
| 867 | s->res.analysers &= ~AN_RES_FLT_END; |
Christopher Faulet | c6062be | 2016-10-31 11:22:37 +0100 | [diff] [blame] | 868 | |
Christopher Faulet | e600624 | 2017-03-10 11:52:44 +0100 | [diff] [blame] | 869 | /* Clean up the HTTP transaction if needed */ |
| 870 | if (s->txn && (s->txn->flags & TX_WAIT_CLEANUP)) |
| 871 | http_end_txn_clean_session(s); |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 872 | |
Christopher Faulet | e600624 | 2017-03-10 11:52:44 +0100 | [diff] [blame] | 873 | /* Remove backend filters from the list */ |
| 874 | flt_stream_release(s, 1); |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 875 | } |
Christopher Faulet | 2b553de | 2017-03-30 11:13:22 +0200 | [diff] [blame] | 876 | |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 877 | return ret; |
| 878 | } |
| 879 | |
| 880 | |
| 881 | /* |
| 882 | * Calls 'tcp_data' callback for all "data" filters attached to a stream. This |
| 883 | * function is called when incoming data are available. It takes care to update |
| 884 | * the next offset of filters and adjusts available data to be sure that a |
| 885 | * filter cannot parse more data than its predecessors. A filter can choose to |
| 886 | * not consume all available data. Returns -1 if an error occurs, the number of |
| 887 | * consumed bytes otherwise. |
| 888 | */ |
| 889 | static int |
| 890 | flt_data(struct stream *s, struct channel *chn) |
| 891 | { |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 892 | struct filter *filter; |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 893 | unsigned int buf_i; |
Christopher Faulet | 55048a4 | 2016-06-21 10:44:32 +0200 | [diff] [blame] | 894 | int delta = 0, ret = 0; |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 895 | |
| 896 | /* Save buffer state */ |
Christopher Faulet | 55048a4 | 2016-06-21 10:44:32 +0200 | [diff] [blame] | 897 | buf_i = chn->buf->i; |
Christopher Faulet | fcf035c | 2015-12-03 11:48:03 +0100 | [diff] [blame] | 898 | |
| 899 | list_for_each_entry(filter, &strm_flt(s)->filters, list) { |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 900 | unsigned int *nxt; |
| 901 | |
| 902 | /* Call "data" filters only */ |
| 903 | if (!IS_DATA_FILTER(filter, chn)) |
| 904 | continue; |
| 905 | |
| 906 | nxt = &FLT_NXT(filter, chn); |
Christopher Faulet | 443ea1a | 2016-02-04 13:40:26 +0100 | [diff] [blame] | 907 | if (FLT_OPS(filter)->tcp_data) { |
Christopher Faulet | 55048a4 | 2016-06-21 10:44:32 +0200 | [diff] [blame] | 908 | unsigned int i = chn->buf->i; |
| 909 | |
Christopher Faulet | 443ea1a | 2016-02-04 13:40:26 +0100 | [diff] [blame] | 910 | ret = FLT_OPS(filter)->tcp_data(s, filter, chn); |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 911 | if (ret < 0) |
| 912 | break; |
Christopher Faulet | 55048a4 | 2016-06-21 10:44:32 +0200 | [diff] [blame] | 913 | delta += (int)(chn->buf->i - i); |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 914 | |
Christopher Faulet | fcf035c | 2015-12-03 11:48:03 +0100 | [diff] [blame] | 915 | /* Increase next offset of the current filter */ |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 916 | *nxt += ret; |
Christopher Faulet | fcf035c | 2015-12-03 11:48:03 +0100 | [diff] [blame] | 917 | |
| 918 | /* And set this value as the bound for the next |
| 919 | * filter. It will not able to parse more data than the |
| 920 | * current one. */ |
Christopher Faulet | 55048a4 | 2016-06-21 10:44:32 +0200 | [diff] [blame] | 921 | chn->buf->i = *nxt; |
Christopher Faulet | fcf035c | 2015-12-03 11:48:03 +0100 | [diff] [blame] | 922 | } |
| 923 | else { |
| 924 | /* Consume all available data */ |
Christopher Faulet | 55048a4 | 2016-06-21 10:44:32 +0200 | [diff] [blame] | 925 | *nxt = chn->buf->i; |
Christopher Faulet | fcf035c | 2015-12-03 11:48:03 +0100 | [diff] [blame] | 926 | } |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 927 | |
| 928 | /* Update <ret> value to be sure to have the last one when we |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 929 | * exit from the loop. This value will be used to know how much |
| 930 | * data are "forwardable" */ |
| 931 | ret = *nxt; |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 932 | } |
Christopher Faulet | fcf035c | 2015-12-03 11:48:03 +0100 | [diff] [blame] | 933 | |
| 934 | /* Restore the original buffer state */ |
Christopher Faulet | 55048a4 | 2016-06-21 10:44:32 +0200 | [diff] [blame] | 935 | chn->buf->i = buf_i + delta; |
Christopher Faulet | fcf035c | 2015-12-03 11:48:03 +0100 | [diff] [blame] | 936 | |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 937 | return ret; |
| 938 | } |
| 939 | |
| 940 | /* |
| 941 | * Calls 'tcp_forward_data' callback for all "data" filters attached to a |
| 942 | * stream. This function is called when some data can be forwarded. It takes |
| 943 | * care to update the forward offset of filters and adjusts "forwardable" data |
| 944 | * to be sure that a filter cannot forward more data than its predecessors. A |
| 945 | * filter can choose to not forward all parsed data. Returns a negative value if |
| 946 | * an error occurs, else the number of forwarded bytes. |
| 947 | */ |
| 948 | static int |
| 949 | flt_forward_data(struct stream *s, struct channel *chn, unsigned int len) |
| 950 | { |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 951 | struct filter *filter; |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 952 | int ret = len; |
| 953 | |
Christopher Faulet | fcf035c | 2015-12-03 11:48:03 +0100 | [diff] [blame] | 954 | list_for_each_entry(filter, &strm_flt(s)->filters, list) { |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 955 | unsigned int *fwd; |
| 956 | |
| 957 | /* Call "data" filters only */ |
| 958 | if (!IS_DATA_FILTER(filter, chn)) |
| 959 | continue; |
| 960 | |
| 961 | fwd = &FLT_FWD(filter, chn); |
Christopher Faulet | 443ea1a | 2016-02-04 13:40:26 +0100 | [diff] [blame] | 962 | if (FLT_OPS(filter)->tcp_forward_data) { |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 963 | /* Remove bytes that the current filter considered as |
| 964 | * forwarded */ |
Christopher Faulet | 443ea1a | 2016-02-04 13:40:26 +0100 | [diff] [blame] | 965 | ret = FLT_OPS(filter)->tcp_forward_data(s, filter, chn, ret - *fwd); |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 966 | if (ret < 0) |
| 967 | goto end; |
| 968 | } |
| 969 | |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 970 | /* Adjust bytes that the current filter considers as |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 971 | * forwarded */ |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 972 | *fwd += ret; |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 973 | |
| 974 | /* And set this value as the bound for the next filter. It will |
| 975 | * not able to forward more data than the current one. */ |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 976 | ret = *fwd; |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 977 | } |
| 978 | |
| 979 | if (!ret) |
| 980 | goto end; |
| 981 | |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 982 | /* Finally, adjust filters offsets by removing data that HAProxy will |
| 983 | * forward. */ |
Christopher Faulet | fcf035c | 2015-12-03 11:48:03 +0100 | [diff] [blame] | 984 | list_for_each_entry(filter, &strm_flt(s)->filters, list) { |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 985 | if (!IS_DATA_FILTER(filter, chn)) |
| 986 | continue; |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 987 | FLT_NXT(filter, chn) -= ret; |
| 988 | FLT_FWD(filter, chn) -= ret; |
| 989 | } |
| 990 | |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 991 | end: |
| 992 | return ret; |
| 993 | } |
| 994 | |
| 995 | /* |
| 996 | * Called when TCP data must be filtered on a channel. This function is the |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 997 | * AN_REQ/RES_FLT_XFER_DATA analyzer. When called, it is responsible to forward |
| 998 | * data when the proxy is not in http mode. Behind the scene, it calls |
| 999 | * consecutively 'tcp_data' and 'tcp_forward_data' callbacks for all "data" |
| 1000 | * filters attached to a stream. Returns 0 if an error occurs or if it needs to |
| 1001 | * wait, any other value otherwise. |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 1002 | */ |
| 1003 | int |
| 1004 | flt_xfer_data(struct stream *s, struct channel *chn, unsigned int an_bit) |
| 1005 | { |
| 1006 | int ret = 1; |
| 1007 | |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 1008 | /* If there is no "data" filters, we do nothing */ |
| 1009 | if (!HAS_DATA_FILTERS(s, chn)) |
| 1010 | goto end; |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 1011 | |
| 1012 | /* Be sure that the output is still opened. Else we stop the data |
| 1013 | * filtering. */ |
| 1014 | if ((chn->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) || |
| 1015 | ((chn->flags & CF_SHUTW) && (chn->to_forward || chn->buf->o))) |
| 1016 | goto end; |
| 1017 | |
| 1018 | /* Let all "data" filters parsing incoming data */ |
| 1019 | ret = flt_data(s, chn); |
| 1020 | if (ret < 0) |
| 1021 | goto end; |
| 1022 | |
| 1023 | /* And forward them */ |
| 1024 | ret = flt_forward_data(s, chn, ret); |
| 1025 | if (ret < 0) |
| 1026 | goto end; |
| 1027 | |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 1028 | /* Consume data that all filters consider as forwarded. */ |
| 1029 | b_adv(chn->buf, ret); |
| 1030 | |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 1031 | /* Stop waiting data if the input in closed and no data is pending or if |
| 1032 | * the output is closed. */ |
| 1033 | if ((chn->flags & CF_SHUTW) || |
| 1034 | ((chn->flags & CF_SHUTR) && !buffer_pending(chn->buf))) { |
| 1035 | ret = 1; |
| 1036 | goto end; |
| 1037 | } |
| 1038 | |
| 1039 | /* Wait for data */ |
| 1040 | return 0; |
| 1041 | end: |
| 1042 | /* Terminate the data filtering. If <ret> is negative, an error was |
| 1043 | * encountered during the filtering. */ |
| 1044 | return handle_analyzer_result(s, chn, an_bit, ret); |
| 1045 | } |
| 1046 | |
| 1047 | /* |
| 1048 | * Handles result of filter's analyzers. It returns 0 if an error occurs or if |
| 1049 | * it needs to wait, any other value otherwise. |
| 1050 | */ |
| 1051 | static int |
| 1052 | handle_analyzer_result(struct stream *s, struct channel *chn, |
| 1053 | unsigned int an_bit, int ret) |
| 1054 | { |
| 1055 | int finst; |
| 1056 | |
| 1057 | if (ret < 0) |
| 1058 | goto return_bad_req; |
| 1059 | else if (!ret) |
| 1060 | goto wait; |
| 1061 | |
| 1062 | /* End of job, return OK */ |
| 1063 | if (an_bit) { |
| 1064 | chn->analysers &= ~an_bit; |
| 1065 | chn->analyse_exp = TICK_ETERNITY; |
| 1066 | } |
| 1067 | return 1; |
| 1068 | |
| 1069 | return_bad_req: |
| 1070 | /* An error occurs */ |
| 1071 | channel_abort(&s->req); |
| 1072 | channel_abort(&s->res); |
| 1073 | |
| 1074 | if (!(chn->flags & CF_ISRESP)) { |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 1075 | s->req.analysers &= AN_REQ_FLT_END; |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 1076 | finst = SF_FINST_R; |
| 1077 | /* FIXME: incr counters */ |
| 1078 | } |
| 1079 | else { |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 1080 | s->res.analysers &= AN_RES_FLT_END; |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 1081 | finst = SF_FINST_H; |
| 1082 | /* FIXME: incr counters */ |
| 1083 | } |
| 1084 | |
| 1085 | if (s->txn) { |
| 1086 | /* Do not do that when we are waiting for the next request */ |
| 1087 | if (s->txn->status) |
| 1088 | http_reply_and_close(s, s->txn->status, NULL); |
| 1089 | else { |
| 1090 | s->txn->status = 400; |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 1091 | http_reply_and_close(s, 400, http_error_message(s)); |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 1092 | } |
| 1093 | } |
| 1094 | |
| 1095 | if (!(s->flags & SF_ERR_MASK)) |
| 1096 | s->flags |= SF_ERR_PRXCOND; |
| 1097 | if (!(s->flags & SF_FINST_MASK)) |
| 1098 | s->flags |= finst; |
| 1099 | return 0; |
| 1100 | |
| 1101 | wait: |
| 1102 | if (!(chn->flags & CF_ISRESP)) |
| 1103 | channel_dont_connect(chn); |
| 1104 | return 0; |
| 1105 | } |
| 1106 | |
| 1107 | |
| 1108 | /* Note: must not be declared <const> as its list will be overwritten. |
| 1109 | * Please take care of keeping this list alphabetically sorted, doing so helps |
| 1110 | * all code contributors. |
| 1111 | * Optional keywords are also declared with a NULL ->parse() function so that |
| 1112 | * the config parser can report an appropriate error when a known keyword was |
| 1113 | * not enabled. */ |
| 1114 | static struct cfg_kw_list cfg_kws = {ILH, { |
| 1115 | { CFG_LISTEN, "filter", parse_filter }, |
| 1116 | { 0, NULL, NULL }, |
| 1117 | } |
| 1118 | }; |
| 1119 | |
| 1120 | __attribute__((constructor)) |
| 1121 | static void |
| 1122 | __filters_init(void) |
| 1123 | { |
| 1124 | pool2_filter = create_pool("filter", sizeof(struct filter), MEM_F_SHARED); |
| 1125 | cfg_register_keywords(&cfg_kws); |
Willy Tarreau | 64bca59 | 2016-12-21 20:13:11 +0100 | [diff] [blame] | 1126 | hap_register_post_check(flt_init_all); |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 1127 | } |
| 1128 | |
| 1129 | __attribute__((destructor)) |
| 1130 | static void |
| 1131 | __filters_deinit(void) |
| 1132 | { |
| 1133 | pool_destroy2(pool2_filter); |
| 1134 | } |
| 1135 | |
| 1136 | /* |
| 1137 | * Local variables: |
| 1138 | * c-indent-level: 8 |
| 1139 | * c-basic-offset: 8 |
| 1140 | * End: |
| 1141 | */ |