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