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 | */ |
| 254 | int |
| 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 | |
| 266 | /* |
| 267 | * Calls 'check' callback for all filters attached to a proxy. This happens |
| 268 | * after the configuration parsing but before filters initialization. Returns |
| 269 | * the number of encountered errors. |
| 270 | */ |
| 271 | int |
| 272 | flt_check(struct proxy *proxy) |
| 273 | { |
Christopher Faulet | 443ea1a | 2016-02-04 13:40:26 +0100 | [diff] [blame] | 274 | struct flt_conf *fconf; |
| 275 | int err = 0; |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 276 | |
Christopher Faulet | 443ea1a | 2016-02-04 13:40:26 +0100 | [diff] [blame] | 277 | list_for_each_entry(fconf, &proxy->filter_configs, list) { |
| 278 | if (fconf->ops->check) |
| 279 | err += fconf->ops->check(proxy, fconf); |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 280 | } |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 281 | err += check_legacy_http_comp_flt(proxy); |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 282 | return err; |
| 283 | } |
| 284 | |
| 285 | /* |
| 286 | * Calls 'denit' callback for all filters attached to a proxy. This happens when |
| 287 | * HAProxy is stopped. |
| 288 | */ |
| 289 | void |
| 290 | flt_deinit(struct proxy *proxy) |
| 291 | { |
Christopher Faulet | 443ea1a | 2016-02-04 13:40:26 +0100 | [diff] [blame] | 292 | struct flt_conf *fconf, *back; |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 293 | |
Christopher Faulet | 443ea1a | 2016-02-04 13:40:26 +0100 | [diff] [blame] | 294 | list_for_each_entry_safe(fconf, back, &proxy->filter_configs, list) { |
| 295 | if (fconf->ops->deinit) |
| 296 | fconf->ops->deinit(proxy, fconf); |
| 297 | LIST_DEL(&fconf->list); |
| 298 | free(fconf); |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 299 | } |
| 300 | } |
| 301 | |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 302 | /* Attaches a filter to a stream. Returns -1 if an error occurs, 0 otherwise. */ |
| 303 | static int |
Christopher Faulet | 443ea1a | 2016-02-04 13:40:26 +0100 | [diff] [blame] | 304 | 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] | 305 | { |
| 306 | struct filter *f = pool_alloc2(pool2_filter); |
| 307 | if (!f) /* not enough memory */ |
| 308 | return -1; |
| 309 | memset(f, 0, sizeof(*f)); |
Christopher Faulet | 443ea1a | 2016-02-04 13:40:26 +0100 | [diff] [blame] | 310 | f->config = fconf; |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 311 | f->flags |= flags; |
Christopher Faulet | fcf035c | 2015-12-03 11:48:03 +0100 | [diff] [blame] | 312 | LIST_ADDQ(&strm_flt(s)->filters, &f->list); |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 313 | strm_flt(s)->flags |= STRM_FLT_FL_HAS_FILTERS; |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 314 | return 0; |
| 315 | } |
| 316 | |
| 317 | /* |
| 318 | * Called when a stream is created. It attaches all frontend filters to the |
| 319 | * stream. Returns -1 if an error occurs, 0 otherwise. |
| 320 | */ |
| 321 | int |
| 322 | flt_stream_init(struct stream *s) |
| 323 | { |
Christopher Faulet | 443ea1a | 2016-02-04 13:40:26 +0100 | [diff] [blame] | 324 | struct flt_conf *fconf; |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 325 | |
Christopher Faulet | fcf035c | 2015-12-03 11:48:03 +0100 | [diff] [blame] | 326 | memset(strm_flt(s), 0, sizeof(*strm_flt(s))); |
| 327 | LIST_INIT(&strm_flt(s)->filters); |
Christopher Faulet | 443ea1a | 2016-02-04 13:40:26 +0100 | [diff] [blame] | 328 | list_for_each_entry(fconf, &strm_fe(s)->filter_configs, list) { |
| 329 | if (flt_stream_add_filter(s, fconf, 0) < 0) |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 330 | return -1; |
| 331 | } |
| 332 | return 0; |
| 333 | } |
| 334 | |
| 335 | /* |
| 336 | * Called when a stream is closed or when analyze ends (For an HTTP stream, this |
| 337 | * happens after each request/response exchange). When analyze ends, backend |
| 338 | * filters are removed. When the stream is closed, all filters attached to the |
| 339 | * stream are removed. |
| 340 | */ |
| 341 | void |
| 342 | flt_stream_release(struct stream *s, int only_backend) |
| 343 | { |
| 344 | struct filter *filter, *back; |
| 345 | |
Christopher Faulet | fcf035c | 2015-12-03 11:48:03 +0100 | [diff] [blame] | 346 | list_for_each_entry_safe(filter, back, &strm_flt(s)->filters, list) { |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 347 | if (!only_backend || (filter->flags & FLT_FL_IS_BACKEND_FILTER)) { |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 348 | LIST_DEL(&filter->list); |
| 349 | pool_free2(pool2_filter, filter); |
| 350 | } |
| 351 | } |
Christopher Faulet | fcf035c | 2015-12-03 11:48:03 +0100 | [diff] [blame] | 352 | if (LIST_ISEMPTY(&strm_flt(s)->filters)) |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 353 | strm_flt(s)->flags &= ~STRM_FLT_FL_HAS_FILTERS; |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 354 | } |
| 355 | |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 356 | /* |
| 357 | * Calls 'stream_start' for all filters attached to a stream. This happens when |
| 358 | * the stream is created, just after calling flt_stream_init |
| 359 | * function. Returns -1 if an error occurs, 0 otherwise. |
| 360 | */ |
| 361 | int |
| 362 | flt_stream_start(struct stream *s) |
| 363 | { |
| 364 | struct filter *filter; |
| 365 | |
Christopher Faulet | fcf035c | 2015-12-03 11:48:03 +0100 | [diff] [blame] | 366 | list_for_each_entry(filter, &strm_flt(s)->filters, list) { |
Christopher Faulet | 443ea1a | 2016-02-04 13:40:26 +0100 | [diff] [blame] | 367 | 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] | 368 | return -1; |
| 369 | } |
| 370 | return 0; |
| 371 | } |
| 372 | |
| 373 | /* |
| 374 | * Calls 'stream_stop' for all filters attached to a stream. This happens when |
| 375 | * the stream is stopped, just before calling flt_stream_release function. |
| 376 | */ |
| 377 | void |
| 378 | flt_stream_stop(struct stream *s) |
| 379 | { |
| 380 | struct filter *filter; |
| 381 | |
Christopher Faulet | fcf035c | 2015-12-03 11:48:03 +0100 | [diff] [blame] | 382 | list_for_each_entry(filter, &strm_flt(s)->filters, list) { |
Christopher Faulet | 443ea1a | 2016-02-04 13:40:26 +0100 | [diff] [blame] | 383 | if (FLT_OPS(filter)->stream_stop) |
| 384 | FLT_OPS(filter)->stream_stop(s, filter); |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 385 | } |
| 386 | } |
| 387 | |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 388 | /* |
| 389 | * Called when a backend is set for a stream. If the frontend and the backend |
| 390 | * are the same, this function does nothing. Else it attaches all backend |
| 391 | * filters to the stream. Returns -1 if an error occurs, 0 otherwise. |
| 392 | */ |
| 393 | int |
| 394 | flt_set_stream_backend(struct stream *s, struct proxy *be) |
| 395 | { |
Christopher Faulet | 443ea1a | 2016-02-04 13:40:26 +0100 | [diff] [blame] | 396 | struct flt_conf *fconf; |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 397 | |
| 398 | if (strm_fe(s) == be) |
| 399 | return 0; |
| 400 | |
Christopher Faulet | 443ea1a | 2016-02-04 13:40:26 +0100 | [diff] [blame] | 401 | list_for_each_entry(fconf, &be->filter_configs, list) { |
| 402 | 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] | 403 | return -1; |
| 404 | } |
| 405 | return 0; |
| 406 | } |
| 407 | |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 408 | /* |
| 409 | * Calls 'http_data' callback for all "data" filters attached to a stream. This |
| 410 | * function is called when incoming data are available (excluding chunks |
| 411 | * envelope for chunked messages) in the AN_REQ_HTTP_XFER_BODY and |
| 412 | * AN_RES_HTTP_XFER_BODY analyzers. It takes care to update the next offset of |
| 413 | * filters and adjusts available data to be sure that a filter cannot parse more |
| 414 | * data than its predecessors. A filter can choose to not consume all available |
| 415 | * data. Returns -1 if an error occurs, the number of consumed bytes otherwise. |
| 416 | */ |
| 417 | int |
| 418 | flt_http_data(struct stream *s, struct http_msg *msg) |
| 419 | { |
Christopher Faulet | fcf035c | 2015-12-03 11:48:03 +0100 | [diff] [blame] | 420 | struct filter *filter; |
| 421 | struct buffer *buf = msg->chn->buf; |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 422 | unsigned int buf_i; |
| 423 | int ret = 0; |
| 424 | |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 425 | /* Save buffer state */ |
Christopher Faulet | fcf035c | 2015-12-03 11:48:03 +0100 | [diff] [blame] | 426 | buf_i = buf->i; |
| 427 | |
Christopher Faulet | fcf035c | 2015-12-03 11:48:03 +0100 | [diff] [blame] | 428 | list_for_each_entry(filter, &strm_flt(s)->filters, list) { |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 429 | unsigned int *nxt; |
| 430 | |
| 431 | /* Call "data" filters only */ |
| 432 | if (!IS_DATA_FILTER(filter, msg->chn)) |
| 433 | continue; |
| 434 | |
Christopher Faulet | 2fb2880 | 2015-12-01 10:40:57 +0100 | [diff] [blame] | 435 | /* If the HTTP parser is ahead, we update the next offset of the |
| 436 | * current filter. This happens for chunked messages, at the |
| 437 | * begining of a new chunk. */ |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 438 | nxt = &FLT_NXT(filter, msg->chn); |
| 439 | if (msg->next > *nxt) |
| 440 | *nxt = msg->next; |
| 441 | |
Christopher Faulet | 443ea1a | 2016-02-04 13:40:26 +0100 | [diff] [blame] | 442 | if (FLT_OPS(filter)->http_data) { |
| 443 | ret = FLT_OPS(filter)->http_data(s, filter, msg); |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 444 | if (ret < 0) |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 445 | break; |
Christopher Faulet | fcf035c | 2015-12-03 11:48:03 +0100 | [diff] [blame] | 446 | |
| 447 | /* Update the next offset of the current filter */ |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 448 | *nxt += ret; |
Christopher Faulet | fcf035c | 2015-12-03 11:48:03 +0100 | [diff] [blame] | 449 | |
| 450 | /* And set this value as the bound for the next |
| 451 | * filter. It will not able to parse more data than this |
| 452 | * one. */ |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 453 | buf->i = *nxt; |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 454 | } |
| 455 | else { |
Christopher Faulet | fcf035c | 2015-12-03 11:48:03 +0100 | [diff] [blame] | 456 | /* Consume all available data and update the next offset |
| 457 | * of the current filter. buf->i is untouched here. */ |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 458 | ret = MIN(msg->chunk_len + msg->next, buf->i) - *nxt; |
| 459 | *nxt += ret; |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 460 | } |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 461 | } |
Christopher Faulet | fcf035c | 2015-12-03 11:48:03 +0100 | [diff] [blame] | 462 | |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 463 | /* Restore the original buffer state */ |
Christopher Faulet | fcf035c | 2015-12-03 11:48:03 +0100 | [diff] [blame] | 464 | buf->i = buf_i; |
| 465 | |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 466 | return ret; |
| 467 | } |
| 468 | |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 469 | /* |
| 470 | * Calls 'http_chunk_trailers' callback for all "data" filters attached to a |
| 471 | * stream. This function is called for chunked messages only when a part of the |
| 472 | * trailers was parsed in the AN_REQ_HTTP_XFER_BODY and AN_RES_HTTP_XFER_BODY |
| 473 | * analyzers. Filters can know how much data were parsed by the HTTP parsing |
| 474 | * until the last call with the msg->sol value. Returns a negative value if an |
| 475 | * error occurs, any other value otherwise. |
| 476 | */ |
| 477 | int |
| 478 | flt_http_chunk_trailers(struct stream *s, struct http_msg *msg) |
| 479 | { |
Christopher Faulet | 2fb2880 | 2015-12-01 10:40:57 +0100 | [diff] [blame] | 480 | struct filter *filter; |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 481 | int ret = 1; |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 482 | |
Christopher Faulet | fcf035c | 2015-12-03 11:48:03 +0100 | [diff] [blame] | 483 | list_for_each_entry(filter, &strm_flt(s)->filters, list) { |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 484 | unsigned int *nxt; |
| 485 | |
| 486 | /* Call "data" filters only */ |
| 487 | if (!IS_DATA_FILTER(filter, msg->chn)) |
| 488 | continue; |
| 489 | |
Christopher Faulet | 2fb2880 | 2015-12-01 10:40:57 +0100 | [diff] [blame] | 490 | /* Be sure to set the next offset of the filter at the right |
| 491 | * place. This is really useful when the first part of the |
| 492 | * trailers was parsed. */ |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 493 | nxt = &FLT_NXT(filter, msg->chn); |
| 494 | *nxt = msg->next; |
| 495 | |
Christopher Faulet | 443ea1a | 2016-02-04 13:40:26 +0100 | [diff] [blame] | 496 | if (FLT_OPS(filter)->http_chunk_trailers) { |
| 497 | ret = FLT_OPS(filter)->http_chunk_trailers(s, filter, msg); |
Christopher Faulet | 2fb2880 | 2015-12-01 10:40:57 +0100 | [diff] [blame] | 498 | if (ret < 0) |
| 499 | break; |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 500 | } |
Christopher Faulet | 2fb2880 | 2015-12-01 10:40:57 +0100 | [diff] [blame] | 501 | /* Update the next offset of the current filter. Here all data |
| 502 | * are always consumed. */ |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 503 | *nxt += msg->sol; |
Christopher Faulet | 2fb2880 | 2015-12-01 10:40:57 +0100 | [diff] [blame] | 504 | } |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 505 | return ret; |
| 506 | } |
| 507 | |
| 508 | /* |
| 509 | * Calls 'http_end' callback for all filters attached to a stream. All filters |
| 510 | * are called here, but only if there is at least one "data" filter. This |
| 511 | * functions is called when all data were parsed and forwarded. 'http_end' |
| 512 | * callback is resumable, so this function returns a negative value if an error |
| 513 | * occurs, 0 if it needs to wait for some reason, any other value otherwise. |
| 514 | */ |
| 515 | int |
| 516 | flt_http_end(struct stream *s, struct http_msg *msg) |
| 517 | { |
| 518 | int ret = 1; |
| 519 | |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 520 | RESUME_FILTER_LOOP(s, msg->chn) { |
Christopher Faulet | 443ea1a | 2016-02-04 13:40:26 +0100 | [diff] [blame] | 521 | if (FLT_OPS(filter)->http_end) { |
| 522 | ret = FLT_OPS(filter)->http_end(s, filter, msg); |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 523 | if (ret <= 0) |
| 524 | BREAK_EXECUTION(s, msg->chn, end); |
| 525 | } |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 526 | } RESUME_FILTER_END; |
| 527 | end: |
| 528 | return ret; |
| 529 | } |
| 530 | |
| 531 | /* |
| 532 | * Calls 'http_reset' callback for all filters attached to a stream. This |
| 533 | * happens when a 100-continue response is received. |
| 534 | */ |
| 535 | void |
| 536 | flt_http_reset(struct stream *s, struct http_msg *msg) |
| 537 | { |
| 538 | struct filter *filter; |
| 539 | |
Christopher Faulet | fcf035c | 2015-12-03 11:48:03 +0100 | [diff] [blame] | 540 | list_for_each_entry(filter, &strm_flt(s)->filters, list) { |
Christopher Faulet | 443ea1a | 2016-02-04 13:40:26 +0100 | [diff] [blame] | 541 | if (FLT_OPS(filter)->http_reset) |
| 542 | FLT_OPS(filter)->http_reset(s, filter, msg); |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 543 | } |
| 544 | } |
| 545 | |
| 546 | /* |
| 547 | * Calls 'http_reply' callback for all filters attached to a stream when HA |
| 548 | * decides to stop the HTTP message processing. |
| 549 | */ |
| 550 | void |
| 551 | flt_http_reply(struct stream *s, short status, const struct chunk *msg) |
| 552 | { |
| 553 | struct filter *filter; |
| 554 | |
Christopher Faulet | fcf035c | 2015-12-03 11:48:03 +0100 | [diff] [blame] | 555 | list_for_each_entry(filter, &strm_flt(s)->filters, list) { |
Christopher Faulet | 443ea1a | 2016-02-04 13:40:26 +0100 | [diff] [blame] | 556 | if (FLT_OPS(filter)->http_reply) |
| 557 | FLT_OPS(filter)->http_reply(s, filter, status, msg); |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 558 | } |
| 559 | } |
| 560 | |
| 561 | /* |
| 562 | * Calls 'http_forward_data' callback for all "data" filters attached to a |
| 563 | * stream. This function is called when some data can be forwarded in the |
| 564 | * AN_REQ_HTTP_XFER_BODY and AN_RES_HTTP_XFER_BODY analyzers. It takes care to |
| 565 | * update the forward offset of filters and adjusts "forwardable" data to be |
| 566 | * sure that a filter cannot forward more data than its predecessors. A filter |
| 567 | * can choose to not forward all parsed data. Returns a negative value if an |
| 568 | * error occurs, else the number of forwarded bytes. |
| 569 | */ |
| 570 | int |
| 571 | flt_http_forward_data(struct stream *s, struct http_msg *msg, unsigned int len) |
| 572 | { |
Christopher Faulet | fcf035c | 2015-12-03 11:48:03 +0100 | [diff] [blame] | 573 | struct filter *filter; |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 574 | int ret = len; |
| 575 | |
Christopher Faulet | fcf035c | 2015-12-03 11:48:03 +0100 | [diff] [blame] | 576 | list_for_each_entry(filter, &strm_flt(s)->filters, list) { |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 577 | unsigned int *nxt, *fwd; |
| 578 | |
| 579 | /* Call "data" filters only */ |
| 580 | if (!IS_DATA_FILTER(filter, msg->chn)) |
| 581 | continue; |
| 582 | |
Christopher Faulet | 2fb2880 | 2015-12-01 10:40:57 +0100 | [diff] [blame] | 583 | /* If the HTTP parser is ahead, we update the next offset of the |
| 584 | * current filter. This happens for chunked messages, when the |
| 585 | * chunk envelope is parsed. */ |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 586 | nxt = &FLT_NXT(filter, msg->chn); |
| 587 | fwd = &FLT_FWD(filter, msg->chn); |
| 588 | if (msg->next > *nxt) |
| 589 | *nxt = msg->next; |
| 590 | |
Christopher Faulet | 443ea1a | 2016-02-04 13:40:26 +0100 | [diff] [blame] | 591 | if (FLT_OPS(filter)->http_forward_data) { |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 592 | /* Remove bytes that the current filter considered as |
| 593 | * forwarded */ |
Christopher Faulet | 443ea1a | 2016-02-04 13:40:26 +0100 | [diff] [blame] | 594 | ret = FLT_OPS(filter)->http_forward_data(s, filter, msg, ret - *fwd); |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 595 | if (ret < 0) |
| 596 | goto end; |
| 597 | } |
| 598 | |
| 599 | /* Adjust bytes that the current filter considers as |
| 600 | * forwarded */ |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 601 | *fwd += ret; |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 602 | |
| 603 | /* And set this value as the bound for the next filter. It will |
| 604 | * not able to forward more data than the current one. */ |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 605 | ret = *fwd; |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 606 | } |
| 607 | |
| 608 | if (!ret) |
| 609 | goto end; |
| 610 | |
| 611 | /* Finally, adjust filters offsets by removing data that HAProxy will |
| 612 | * forward. */ |
Christopher Faulet | fcf035c | 2015-12-03 11:48:03 +0100 | [diff] [blame] | 613 | list_for_each_entry(filter, &strm_flt(s)->filters, list) { |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 614 | if (!IS_DATA_FILTER(filter, msg->chn)) |
| 615 | continue; |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 616 | FLT_NXT(filter, msg->chn) -= ret; |
| 617 | FLT_FWD(filter, msg->chn) -= ret; |
| 618 | } |
| 619 | end: |
| 620 | return ret; |
| 621 | } |
| 622 | |
| 623 | /* |
| 624 | * Calls 'channel_start_analyze' callback for all filters attached to a |
| 625 | * stream. This function is called when we start to analyze a request or a |
| 626 | * response. For frontend filters, it is called before all other analyzers. For |
| 627 | * backend ones, it is called before all backend |
| 628 | * analyzers. 'channel_start_analyze' callback is resumable, so this function |
| 629 | * returns 0 if an error occurs or if it needs to wait, any other value |
| 630 | * otherwise. |
| 631 | */ |
| 632 | int |
| 633 | flt_start_analyze(struct stream *s, struct channel *chn, unsigned int an_bit) |
| 634 | { |
| 635 | int ret = 1; |
| 636 | |
| 637 | /* If this function is called, this means there is at least one filter, |
| 638 | * so we do not need to check the filter list's emptiness. */ |
| 639 | |
| 640 | RESUME_FILTER_LOOP(s, chn) { |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 641 | if (an_bit == AN_FLT_START_BE && !(filter->flags & FLT_FL_IS_BACKEND_FILTER)) |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 642 | continue; |
| 643 | |
Christopher Faulet | fcf035c | 2015-12-03 11:48:03 +0100 | [diff] [blame] | 644 | FLT_NXT(filter, chn) = 0; |
| 645 | FLT_FWD(filter, chn) = 0; |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 646 | |
Christopher Faulet | 443ea1a | 2016-02-04 13:40:26 +0100 | [diff] [blame] | 647 | if (FLT_OPS(filter)->channel_start_analyze) { |
| 648 | ret = FLT_OPS(filter)->channel_start_analyze(s, filter, chn); |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 649 | if (ret <= 0) |
| 650 | BREAK_EXECUTION(s, chn, end); |
| 651 | } |
| 652 | } RESUME_FILTER_END; |
| 653 | |
| 654 | end: |
| 655 | return handle_analyzer_result(s, chn, an_bit, ret); |
| 656 | } |
| 657 | |
| 658 | /* |
| 659 | * Calls 'channel_analyze' callback for all filters attached to a stream. This |
| 660 | * function is called before each analyzer attached to a channel, expects |
| 661 | * analyzers responsible for data sending. 'channel_analyze' callback is |
| 662 | * resumable, so this function returns 0 if an error occurs or if it needs to |
| 663 | * wait, any other value otherwise. |
| 664 | */ |
| 665 | int |
| 666 | flt_analyze(struct stream *s, struct channel *chn, unsigned int an_bit) |
| 667 | { |
| 668 | int ret = 1; |
| 669 | |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 670 | RESUME_FILTER_LOOP(s, chn) { |
Christopher Faulet | 443ea1a | 2016-02-04 13:40:26 +0100 | [diff] [blame] | 671 | if (FLT_OPS(filter)->channel_analyze) { |
| 672 | ret = FLT_OPS(filter)->channel_analyze(s, filter, chn, an_bit); |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 673 | if (ret <= 0) |
| 674 | BREAK_EXECUTION(s, chn, check_result); |
| 675 | } |
| 676 | } RESUME_FILTER_END; |
| 677 | |
| 678 | check_result: |
Christopher Faulet | 309c641 | 2015-12-02 09:57:32 +0100 | [diff] [blame] | 679 | return handle_analyzer_result(s, chn, 0, ret); |
| 680 | } |
| 681 | |
| 682 | /* |
| 683 | * This function do the same that the previsous one, but for the |
| 684 | * AN_FLT_HTTP_HDRS analyzer. The difference is what is done when all filters |
| 685 | * have been called. Returns 0 if an error occurs or if it needs to wait, any |
| 686 | * other value otherwise. |
| 687 | */ |
| 688 | int |
| 689 | flt_analyze_http_headers(struct stream *s, struct channel *chn, unsigned int an_bit) |
| 690 | { |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 691 | struct filter *filter; |
| 692 | int ret = 1; |
Christopher Faulet | 309c641 | 2015-12-02 09:57:32 +0100 | [diff] [blame] | 693 | |
| 694 | RESUME_FILTER_LOOP(s, chn) { |
Christopher Faulet | 443ea1a | 2016-02-04 13:40:26 +0100 | [diff] [blame] | 695 | if (FLT_OPS(filter)->channel_analyze) { |
| 696 | ret = FLT_OPS(filter)->channel_analyze(s, filter, chn, an_bit); |
Christopher Faulet | 309c641 | 2015-12-02 09:57:32 +0100 | [diff] [blame] | 697 | if (ret <= 0) |
| 698 | BREAK_EXECUTION(s, chn, check_result); |
| 699 | } |
| 700 | } RESUME_FILTER_END; |
| 701 | |
| 702 | /* We increase next offset of all "data" filters after all processing on |
| 703 | * headers because any filter can alter them. So the definitive size of |
| 704 | * headers (msg->sov) is only known when all filters have been |
| 705 | * called. */ |
Christopher Faulet | fcf035c | 2015-12-03 11:48:03 +0100 | [diff] [blame] | 706 | list_for_each_entry(filter, &strm_flt(s)->filters, list) { |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 707 | /* Handle "data" filters only */ |
| 708 | if (!IS_DATA_FILTER(filter, chn)) |
| 709 | continue; |
| 710 | |
| 711 | FLT_NXT(filter, chn) = ((chn->flags & CF_ISRESP) |
| 712 | ? s->txn->rsp.sov : s->txn->req.sov); |
Christopher Faulet | 309c641 | 2015-12-02 09:57:32 +0100 | [diff] [blame] | 713 | } |
| 714 | |
| 715 | check_result: |
| 716 | return handle_analyzer_result(s, chn, an_bit, ret); |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 717 | } |
| 718 | |
| 719 | /* |
| 720 | * Calls 'channel_end_analyze' callback for all filters attached to a |
| 721 | * stream. This function is called when we stop to analyze a request or a |
| 722 | * response. It is called after all other analyzers. 'channel_end_analyze' |
| 723 | * callback is resumable, so this function returns 0 if an error occurs or if it |
| 724 | * needs to wait, any other value otherwise. |
| 725 | */ |
| 726 | int |
| 727 | flt_end_analyze(struct stream *s, struct channel *chn, unsigned int an_bit) |
| 728 | { |
| 729 | int ret = 1; |
| 730 | |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 731 | RESUME_FILTER_LOOP(s, chn) { |
Christopher Faulet | fcf035c | 2015-12-03 11:48:03 +0100 | [diff] [blame] | 732 | FLT_NXT(filter, chn) = 0; |
| 733 | FLT_FWD(filter, chn) = 0; |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 734 | unregister_data_filter(s, chn, filter); |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 735 | |
Christopher Faulet | 443ea1a | 2016-02-04 13:40:26 +0100 | [diff] [blame] | 736 | if (FLT_OPS(filter)->channel_end_analyze) { |
| 737 | ret = FLT_OPS(filter)->channel_end_analyze(s, filter, chn); |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 738 | if (ret <= 0) |
| 739 | BREAK_EXECUTION(s, chn, end); |
| 740 | } |
| 741 | } RESUME_FILTER_END; |
| 742 | |
| 743 | end: |
| 744 | ret = handle_analyzer_result(s, chn, an_bit, ret); |
Christopher Faulet | 02c7b22 | 2015-12-22 12:01:29 +0100 | [diff] [blame] | 745 | |
| 746 | /* Check if 'channel_end_analyze' callback has been called for the |
| 747 | * request and the response. */ |
| 748 | if (!(s->req.analysers & AN_FLT_END) && !(s->res.analysers & AN_FLT_END)) { |
Christopher Faulet | 02c7b22 | 2015-12-22 12:01:29 +0100 | [diff] [blame] | 749 | /* When we are waiting for a new request, so we must reset |
| 750 | * stream analyzers. The input must not be closed the request |
| 751 | * channel, else it is useless to wait. */ |
| 752 | if (s->txn && (s->txn->flags & TX_WAIT_NEXT_RQ) && !channel_input_closed(&s->req)) { |
| 753 | s->req.analysers = strm_li(s) ? strm_li(s)->analysers : 0; |
| 754 | s->res.analysers = 0; |
| 755 | } |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 756 | |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 757 | /* Remove backend filters from the list */ |
| 758 | flt_stream_release(s, 1); |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 759 | } |
| 760 | else if (ret) { |
| 761 | /* Analyzer ends only for one channel. So wake up the stream to |
| 762 | * be sure to process it for the other side as soon as |
| 763 | * possible. */ |
| 764 | task_wakeup(s->task, TASK_WOKEN_MSG); |
| 765 | } |
| 766 | return ret; |
| 767 | } |
| 768 | |
| 769 | |
| 770 | /* |
| 771 | * Calls 'tcp_data' callback for all "data" filters attached to a stream. This |
| 772 | * function is called when incoming data are available. It takes care to update |
| 773 | * the next offset of filters and adjusts available data to be sure that a |
| 774 | * filter cannot parse more data than its predecessors. A filter can choose to |
| 775 | * not consume all available data. Returns -1 if an error occurs, the number of |
| 776 | * consumed bytes otherwise. |
| 777 | */ |
| 778 | static int |
| 779 | flt_data(struct stream *s, struct channel *chn) |
| 780 | { |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 781 | struct filter *filter; |
Christopher Faulet | fcf035c | 2015-12-03 11:48:03 +0100 | [diff] [blame] | 782 | struct buffer *buf = chn->buf; |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 783 | unsigned int buf_i; |
Christopher Faulet | fcf035c | 2015-12-03 11:48:03 +0100 | [diff] [blame] | 784 | int ret = 0; |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 785 | |
| 786 | /* Save buffer state */ |
Christopher Faulet | fcf035c | 2015-12-03 11:48:03 +0100 | [diff] [blame] | 787 | buf_i = buf->i; |
| 788 | |
| 789 | list_for_each_entry(filter, &strm_flt(s)->filters, list) { |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 790 | unsigned int *nxt; |
| 791 | |
| 792 | /* Call "data" filters only */ |
| 793 | if (!IS_DATA_FILTER(filter, chn)) |
| 794 | continue; |
| 795 | |
| 796 | nxt = &FLT_NXT(filter, chn); |
Christopher Faulet | 443ea1a | 2016-02-04 13:40:26 +0100 | [diff] [blame] | 797 | if (FLT_OPS(filter)->tcp_data) { |
| 798 | ret = FLT_OPS(filter)->tcp_data(s, filter, chn); |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 799 | if (ret < 0) |
| 800 | break; |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 801 | |
Christopher Faulet | fcf035c | 2015-12-03 11:48:03 +0100 | [diff] [blame] | 802 | /* Increase next offset of the current filter */ |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 803 | *nxt += ret; |
Christopher Faulet | fcf035c | 2015-12-03 11:48:03 +0100 | [diff] [blame] | 804 | |
| 805 | /* And set this value as the bound for the next |
| 806 | * filter. It will not able to parse more data than the |
| 807 | * current one. */ |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 808 | buf->i = *nxt; |
Christopher Faulet | fcf035c | 2015-12-03 11:48:03 +0100 | [diff] [blame] | 809 | } |
| 810 | else { |
| 811 | /* Consume all available data */ |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 812 | *nxt = buf->i; |
Christopher Faulet | fcf035c | 2015-12-03 11:48:03 +0100 | [diff] [blame] | 813 | } |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 814 | |
| 815 | /* 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] | 816 | * exit from the loop. This value will be used to know how much |
| 817 | * data are "forwardable" */ |
| 818 | ret = *nxt; |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 819 | } |
Christopher Faulet | fcf035c | 2015-12-03 11:48:03 +0100 | [diff] [blame] | 820 | |
| 821 | /* Restore the original buffer state */ |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 822 | chn->buf->i = buf_i; |
Christopher Faulet | fcf035c | 2015-12-03 11:48:03 +0100 | [diff] [blame] | 823 | |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 824 | return ret; |
| 825 | } |
| 826 | |
| 827 | /* |
| 828 | * Calls 'tcp_forward_data' callback for all "data" filters attached to a |
| 829 | * stream. This function is called when some data can be forwarded. It takes |
| 830 | * care to update the forward offset of filters and adjusts "forwardable" data |
| 831 | * to be sure that a filter cannot forward more data than its predecessors. A |
| 832 | * filter can choose to not forward all parsed data. Returns a negative value if |
| 833 | * an error occurs, else the number of forwarded bytes. |
| 834 | */ |
| 835 | static int |
| 836 | flt_forward_data(struct stream *s, struct channel *chn, unsigned int len) |
| 837 | { |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 838 | struct filter *filter; |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 839 | int ret = len; |
| 840 | |
Christopher Faulet | fcf035c | 2015-12-03 11:48:03 +0100 | [diff] [blame] | 841 | list_for_each_entry(filter, &strm_flt(s)->filters, list) { |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 842 | unsigned int *fwd; |
| 843 | |
| 844 | /* Call "data" filters only */ |
| 845 | if (!IS_DATA_FILTER(filter, chn)) |
| 846 | continue; |
| 847 | |
| 848 | fwd = &FLT_FWD(filter, chn); |
Christopher Faulet | 443ea1a | 2016-02-04 13:40:26 +0100 | [diff] [blame] | 849 | if (FLT_OPS(filter)->tcp_forward_data) { |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 850 | /* Remove bytes that the current filter considered as |
| 851 | * forwarded */ |
Christopher Faulet | 443ea1a | 2016-02-04 13:40:26 +0100 | [diff] [blame] | 852 | ret = FLT_OPS(filter)->tcp_forward_data(s, filter, chn, ret - *fwd); |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 853 | if (ret < 0) |
| 854 | goto end; |
| 855 | } |
| 856 | |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 857 | /* Adjust bytes that the current filter considers as |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 858 | * forwarded */ |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 859 | *fwd += ret; |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 860 | |
| 861 | /* And set this value as the bound for the next filter. It will |
| 862 | * not able to forward more data than the current one. */ |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 863 | ret = *fwd; |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 864 | } |
| 865 | |
| 866 | if (!ret) |
| 867 | goto end; |
| 868 | |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 869 | /* Finally, adjust filters offsets by removing data that HAProxy will |
| 870 | * forward. */ |
Christopher Faulet | fcf035c | 2015-12-03 11:48:03 +0100 | [diff] [blame] | 871 | list_for_each_entry(filter, &strm_flt(s)->filters, list) { |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 872 | if (!IS_DATA_FILTER(filter, chn)) |
| 873 | continue; |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 874 | FLT_NXT(filter, chn) -= ret; |
| 875 | FLT_FWD(filter, chn) -= ret; |
| 876 | } |
| 877 | |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 878 | end: |
| 879 | return ret; |
| 880 | } |
| 881 | |
| 882 | /* |
| 883 | * Called when TCP data must be filtered on a channel. This function is the |
| 884 | * AN_FLT_XFER_DATA analyzer. When called, it is responsible to forward data |
| 885 | * when the proxy is not in http mode. Behind the scene, it calls consecutively |
| 886 | * 'tcp_data' and 'tcp_forward_data' callbacks for all "data" filters attached |
| 887 | * to a stream. Returns 0 if an error occurs or if it needs to wait, any other |
| 888 | * value otherwise. |
| 889 | */ |
| 890 | int |
| 891 | flt_xfer_data(struct stream *s, struct channel *chn, unsigned int an_bit) |
| 892 | { |
| 893 | int ret = 1; |
| 894 | |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 895 | /* If there is no "data" filters, we do nothing */ |
| 896 | if (!HAS_DATA_FILTERS(s, chn)) |
| 897 | goto end; |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 898 | |
| 899 | /* Be sure that the output is still opened. Else we stop the data |
| 900 | * filtering. */ |
| 901 | if ((chn->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) || |
| 902 | ((chn->flags & CF_SHUTW) && (chn->to_forward || chn->buf->o))) |
| 903 | goto end; |
| 904 | |
| 905 | /* Let all "data" filters parsing incoming data */ |
| 906 | ret = flt_data(s, chn); |
| 907 | if (ret < 0) |
| 908 | goto end; |
| 909 | |
| 910 | /* And forward them */ |
| 911 | ret = flt_forward_data(s, chn, ret); |
| 912 | if (ret < 0) |
| 913 | goto end; |
| 914 | |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 915 | /* Consume data that all filters consider as forwarded. */ |
| 916 | b_adv(chn->buf, ret); |
| 917 | |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 918 | /* Stop waiting data if the input in closed and no data is pending or if |
| 919 | * the output is closed. */ |
| 920 | if ((chn->flags & CF_SHUTW) || |
| 921 | ((chn->flags & CF_SHUTR) && !buffer_pending(chn->buf))) { |
| 922 | ret = 1; |
| 923 | goto end; |
| 924 | } |
| 925 | |
| 926 | /* Wait for data */ |
| 927 | return 0; |
| 928 | end: |
| 929 | /* Terminate the data filtering. If <ret> is negative, an error was |
| 930 | * encountered during the filtering. */ |
| 931 | return handle_analyzer_result(s, chn, an_bit, ret); |
| 932 | } |
| 933 | |
| 934 | /* |
| 935 | * Handles result of filter's analyzers. It returns 0 if an error occurs or if |
| 936 | * it needs to wait, any other value otherwise. |
| 937 | */ |
| 938 | static int |
| 939 | handle_analyzer_result(struct stream *s, struct channel *chn, |
| 940 | unsigned int an_bit, int ret) |
| 941 | { |
| 942 | int finst; |
| 943 | |
| 944 | if (ret < 0) |
| 945 | goto return_bad_req; |
| 946 | else if (!ret) |
| 947 | goto wait; |
| 948 | |
| 949 | /* End of job, return OK */ |
| 950 | if (an_bit) { |
| 951 | chn->analysers &= ~an_bit; |
| 952 | chn->analyse_exp = TICK_ETERNITY; |
| 953 | } |
| 954 | return 1; |
| 955 | |
| 956 | return_bad_req: |
| 957 | /* An error occurs */ |
| 958 | channel_abort(&s->req); |
| 959 | channel_abort(&s->res); |
| 960 | |
| 961 | if (!(chn->flags & CF_ISRESP)) { |
| 962 | s->req.analysers &= AN_FLT_END; |
| 963 | finst = SF_FINST_R; |
| 964 | /* FIXME: incr counters */ |
| 965 | } |
| 966 | else { |
| 967 | s->res.analysers &= AN_FLT_END; |
| 968 | finst = SF_FINST_H; |
| 969 | /* FIXME: incr counters */ |
| 970 | } |
| 971 | |
| 972 | if (s->txn) { |
| 973 | /* Do not do that when we are waiting for the next request */ |
| 974 | if (s->txn->status) |
| 975 | http_reply_and_close(s, s->txn->status, NULL); |
| 976 | else { |
| 977 | s->txn->status = 400; |
| 978 | http_reply_and_close(s, 400, http_error_message(s, HTTP_ERR_400)); |
| 979 | } |
| 980 | } |
| 981 | |
| 982 | if (!(s->flags & SF_ERR_MASK)) |
| 983 | s->flags |= SF_ERR_PRXCOND; |
| 984 | if (!(s->flags & SF_FINST_MASK)) |
| 985 | s->flags |= finst; |
| 986 | return 0; |
| 987 | |
| 988 | wait: |
| 989 | if (!(chn->flags & CF_ISRESP)) |
| 990 | channel_dont_connect(chn); |
| 991 | return 0; |
| 992 | } |
| 993 | |
| 994 | |
| 995 | /* Note: must not be declared <const> as its list will be overwritten. |
| 996 | * Please take care of keeping this list alphabetically sorted, doing so helps |
| 997 | * all code contributors. |
| 998 | * Optional keywords are also declared with a NULL ->parse() function so that |
| 999 | * the config parser can report an appropriate error when a known keyword was |
| 1000 | * not enabled. */ |
| 1001 | static struct cfg_kw_list cfg_kws = {ILH, { |
| 1002 | { CFG_LISTEN, "filter", parse_filter }, |
| 1003 | { 0, NULL, NULL }, |
| 1004 | } |
| 1005 | }; |
| 1006 | |
| 1007 | __attribute__((constructor)) |
| 1008 | static void |
| 1009 | __filters_init(void) |
| 1010 | { |
| 1011 | pool2_filter = create_pool("filter", sizeof(struct filter), MEM_F_SHARED); |
| 1012 | cfg_register_keywords(&cfg_kws); |
| 1013 | } |
| 1014 | |
| 1015 | __attribute__((destructor)) |
| 1016 | static void |
| 1017 | __filters_deinit(void) |
| 1018 | { |
| 1019 | pool_destroy2(pool2_filter); |
| 1020 | } |
| 1021 | |
| 1022 | /* |
| 1023 | * Local variables: |
| 1024 | * c-indent-level: 8 |
| 1025 | * c-basic-offset: 8 |
| 1026 | * End: |
| 1027 | */ |