blob: 3fd13eb9ca00cf3a8498a8abbc58a5620826f5c0 [file] [log] [blame]
Christopher Fauletd7c91962015-04-30 11:48:27 +02001/*
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>
Willy Tarreaub96b77e2018-12-11 10:22:41 +010019#include <common/htx.h>
Willy Tarreau0108d902018-11-25 19:14:37 +010020#include <common/initcall.h>
Christopher Fauletd7c91962015-04-30 11:48:27 +020021#include <common/namespace.h>
22#include <common/standard.h>
Christopher Faulet71a6a8e2017-07-27 16:33:28 +020023#include <common/hathreads.h>
Christopher Fauletd7c91962015-04-30 11:48:27 +020024
25#include <types/filters.h>
26#include <types/proto_http.h>
27
28#include <proto/compression.h>
29#include <proto/filters.h>
Christopher Faulet92d36382015-11-05 13:35:03 +010030#include <proto/flt_http_comp.h>
Christopher Faulet75bc9132018-11-30 15:18:09 +010031#include <proto/http_htx.h>
Christopher Fauletd7c91962015-04-30 11:48:27 +020032#include <proto/proto_http.h>
33#include <proto/stream.h>
34#include <proto/stream_interface.h>
35
36/* Pool used to allocate filters */
Willy Tarreau8ceae722018-11-26 11:58:30 +010037DECLARE_STATIC_POOL(pool_head_filter, "filter", sizeof(struct filter));
Christopher Fauletd7c91962015-04-30 11:48:27 +020038
39static int handle_analyzer_result(struct stream *s, struct channel *chn, unsigned int an_bit, int ret);
40
41/* - RESUME_FILTER_LOOP and RESUME_FILTER_END must always be used together.
42 * The first one begins a loop and the seconds one ends it.
43 *
44 * - BREAK_EXECUTION must be used to break the loop and set the filter from
45 * which to resume the next time.
46 *
Bertrand Jacquin874a35c2018-09-10 21:26:07 +010047 * Here is an example:
Christopher Fauletd7c91962015-04-30 11:48:27 +020048 *
49 * RESUME_FILTER_LOOP(stream, channel) {
50 * ...
51 * if (cond)
52 * BREAK_EXECUTION(stream, channel, label);
53 * ...
54 * } RESUME_FILTER_END;
55 * ...
56 * label:
57 * ...
58 *
59 */
60#define RESUME_FILTER_LOOP(strm, chn) \
61 do { \
62 struct filter *filter; \
63 \
Christopher Fauletda02e172015-12-04 09:25:05 +010064 if (strm_flt(strm)->current[CHN_IDX(chn)]) { \
65 filter = strm_flt(strm)->current[CHN_IDX(chn)]; \
66 strm_flt(strm)->current[CHN_IDX(chn)] = NULL; \
Christopher Fauletd7c91962015-04-30 11:48:27 +020067 goto resume_execution; \
68 } \
69 \
Christopher Fauletfcf035c2015-12-03 11:48:03 +010070 list_for_each_entry(filter, &strm_flt(s)->filters, list) { \
Christopher Fauletda02e172015-12-04 09:25:05 +010071 resume_execution:
Christopher Fauletd7c91962015-04-30 11:48:27 +020072
73#define RESUME_FILTER_END \
74 } \
75 } while(0)
76
Christopher Fauletda02e172015-12-04 09:25:05 +010077#define BREAK_EXECUTION(strm, chn, label) \
78 do { \
79 strm_flt(strm)->current[CHN_IDX(chn)] = filter; \
80 goto label; \
Christopher Fauletd7c91962015-04-30 11:48:27 +020081 } while (0)
82
83
84/* List head of all known filter keywords */
85static struct flt_kw_list flt_keywords = {
86 .list = LIST_HEAD_INIT(flt_keywords.list)
87};
88
89/*
90 * Registers the filter keyword list <kwl> as a list of valid keywords for next
91 * parsing sessions.
92 */
93void
94flt_register_keywords(struct flt_kw_list *kwl)
95{
96 LIST_ADDQ(&flt_keywords.list, &kwl->list);
97}
98
99/*
100 * Returns a pointer to the filter keyword <kw>, or NULL if not found. If the
101 * keyword is found with a NULL ->parse() function, then an attempt is made to
102 * find one with a valid ->parse() function. This way it is possible to declare
103 * platform-dependant, known keywords as NULL, then only declare them as valid
104 * if some options are met. Note that if the requested keyword contains an
105 * opening parenthesis, everything from this point is ignored.
106 */
107struct flt_kw *
108flt_find_kw(const char *kw)
109{
110 int index;
111 const char *kwend;
112 struct flt_kw_list *kwl;
113 struct flt_kw *ret = NULL;
114
115 kwend = strchr(kw, '(');
116 if (!kwend)
117 kwend = kw + strlen(kw);
118
119 list_for_each_entry(kwl, &flt_keywords.list, list) {
120 for (index = 0; kwl->kw[index].kw != NULL; index++) {
121 if ((strncmp(kwl->kw[index].kw, kw, kwend - kw) == 0) &&
122 kwl->kw[index].kw[kwend-kw] == 0) {
123 if (kwl->kw[index].parse)
124 return &kwl->kw[index]; /* found it !*/
125 else
126 ret = &kwl->kw[index]; /* may be OK */
127 }
128 }
129 }
130 return ret;
131}
132
133/*
134 * Dumps all registered "filter" keywords to the <out> string pointer. The
135 * unsupported keywords are only dumped if their supported form was not found.
136 */
137void
138flt_dump_kws(char **out)
139{
140 struct flt_kw_list *kwl;
141 int index;
142
143 *out = NULL;
144 list_for_each_entry(kwl, &flt_keywords.list, list) {
145 for (index = 0; kwl->kw[index].kw != NULL; index++) {
146 if (kwl->kw[index].parse ||
147 flt_find_kw(kwl->kw[index].kw) == &kwl->kw[index]) {
148 memprintf(out, "%s[%4s] %s%s\n", *out ? *out : "",
149 kwl->scope,
150 kwl->kw[index].kw,
151 kwl->kw[index].parse ? "" : " (not supported)");
152 }
153 }
154 }
155}
156
157/*
Christopher Fauletb3f4e142016-03-07 12:46:38 +0100158 * Lists the known filters on <out>
159 */
160void
161list_filters(FILE *out)
162{
163 char *filters, *p, *f;
164
165 fprintf(out, "Available filters :\n");
166 flt_dump_kws(&filters);
167 for (p = filters; (f = strtok_r(p,"\n",&p));)
168 fprintf(out, "\t%s\n", f);
169 free(filters);
170}
171
172/*
Christopher Fauletd7c91962015-04-30 11:48:27 +0200173 * Parses the "filter" keyword. All keywords must be handled by filters
174 * themselves
175 */
176static int
177parse_filter(char **args, int section_type, struct proxy *curpx,
178 struct proxy *defpx, const char *file, int line, char **err)
179{
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100180 struct flt_conf *fconf = NULL;
Christopher Fauletd7c91962015-04-30 11:48:27 +0200181
182 /* Filter cannot be defined on a default proxy */
183 if (curpx == defpx) {
Christopher Fauletcc7317d2016-04-04 10:51:17 +0200184 memprintf(err, "parsing [%s:%d] : %s is not allowed in a 'default' section.",
Christopher Fauletd7c91962015-04-30 11:48:27 +0200185 file, line, args[0]);
186 return -1;
187 }
188 if (!strcmp(args[0], "filter")) {
189 struct flt_kw *kw;
190 int cur_arg;
191
192 if (!*args[1]) {
193 memprintf(err,
194 "parsing [%s:%d] : missing argument for '%s' in %s '%s'.",
195 file, line, args[0], proxy_type_str(curpx), curpx->id);
196 goto error;
197 }
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100198 fconf = calloc(1, sizeof(*fconf));
199 if (!fconf) {
Christopher Fauletd7c91962015-04-30 11:48:27 +0200200 memprintf(err, "'%s' : out of memory", args[0]);
201 goto error;
202 }
Christopher Fauletd7c91962015-04-30 11:48:27 +0200203
204 cur_arg = 1;
205 kw = flt_find_kw(args[cur_arg]);
206 if (kw) {
207 if (!kw->parse) {
208 memprintf(err, "parsing [%s:%d] : '%s' : "
209 "'%s' option is not implemented in this version (check build options).",
210 file, line, args[0], args[cur_arg]);
211 goto error;
212 }
Thierry Fournier3610c392016-04-13 18:27:51 +0200213 if (kw->parse(args, &cur_arg, curpx, fconf, err, kw->private) != 0) {
Christopher Fauletd7c91962015-04-30 11:48:27 +0200214 if (err && *err)
215 memprintf(err, "'%s' : '%s'",
216 args[0], *err);
217 else
218 memprintf(err, "'%s' : error encountered while processing '%s'",
219 args[0], args[cur_arg]);
220 goto error;
221 }
222 }
223 else {
224 flt_dump_kws(err);
225 indent_msg(err, 4);
226 memprintf(err, "'%s' : unknown keyword '%s'.%s%s",
227 args[0], args[cur_arg],
228 err && *err ? " Registered keywords :" : "", err && *err ? *err : "");
229 goto error;
230 }
231 if (*args[cur_arg]) {
232 memprintf(err, "'%s %s' : unknown keyword '%s'.",
233 args[0], args[1], args[cur_arg]);
234 goto error;
235 }
Christopher Faulet00e818a2016-04-19 17:00:44 +0200236 if (fconf->ops == NULL) {
237 memprintf(err, "'%s %s' : no callbacks defined.",
238 args[0], args[1]);
239 goto error;
240 }
Christopher Fauletd7c91962015-04-30 11:48:27 +0200241
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100242 LIST_ADDQ(&curpx->filter_configs, &fconf->list);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200243 }
244 return 0;
245
246 error:
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100247 free(fconf);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200248 return -1;
249
250
251}
252
253/*
254 * Calls 'init' callback for all filters attached to a proxy. This happens after
255 * the configuration parsing. Filters can finish to fill their config. Returns
256 * (ERR_ALERT|ERR_FATAL) if an error occurs, 0 otherwise.
257 */
Willy Tarreau64bca592016-12-21 20:13:11 +0100258static int
Christopher Fauletd7c91962015-04-30 11:48:27 +0200259flt_init(struct proxy *proxy)
260{
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100261 struct flt_conf *fconf;
Christopher Fauletd7c91962015-04-30 11:48:27 +0200262
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100263 list_for_each_entry(fconf, &proxy->filter_configs, list) {
264 if (fconf->ops->init && fconf->ops->init(proxy, fconf) < 0)
Christopher Fauletd7c91962015-04-30 11:48:27 +0200265 return ERR_ALERT|ERR_FATAL;
266 }
267 return 0;
268}
269
Christopher Faulet71a6a8e2017-07-27 16:33:28 +0200270/*
271 * Calls 'init_per_thread' callback for all filters attached to a proxy for each
272 * threads. This happens after the thread creation. Filters can finish to fill
273 * their config. Returns (ERR_ALERT|ERR_FATAL) if an error occurs, 0 otherwise.
274 */
275static int
276flt_init_per_thread(struct proxy *proxy)
277{
278 struct flt_conf *fconf;
279
280 list_for_each_entry(fconf, &proxy->filter_configs, list) {
281 if (fconf->ops->init_per_thread && fconf->ops->init_per_thread(proxy, fconf) < 0)
282 return ERR_ALERT|ERR_FATAL;
283 }
284 return 0;
285}
286
Willy Tarreau64bca592016-12-21 20:13:11 +0100287/* Calls flt_init() for all proxies, see above */
288static int
289flt_init_all()
290{
291 struct proxy *px;
292 int err_code = 0;
293
Olivier Houchardfbc74e82017-11-24 16:54:05 +0100294 for (px = proxies_list; px; px = px->next) {
Christopher Faulet73e7d1c2020-11-02 16:08:09 +0100295 if (px->state == PR_STSTOPPED) {
296 flt_deinit(px);
297 continue;
298 }
Willy Tarreau64bca592016-12-21 20:13:11 +0100299 err_code |= flt_init(px);
300 if (err_code & (ERR_ABORT|ERR_FATAL)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100301 ha_alert("Failed to initialize filters for proxy '%s'.\n",
302 px->id);
Willy Tarreau64bca592016-12-21 20:13:11 +0100303 return err_code;
304 }
305 }
306 return 0;
307}
308
Joseph Herlantb35ea682018-11-15 12:24:23 -0800309/* Calls flt_init_per_thread() for all proxies, see above. Be careful here, it
310 * returns 0 if an error occurred. This is the opposite of flt_init_all. */
Christopher Faulet71a6a8e2017-07-27 16:33:28 +0200311static int
312flt_init_all_per_thread()
313{
314 struct proxy *px;
315 int err_code = 0;
316
Olivier Houchardfbc74e82017-11-24 16:54:05 +0100317 for (px = proxies_list; px; px = px->next) {
Christopher Faulet73e7d1c2020-11-02 16:08:09 +0100318 if (px->state == PR_STSTOPPED)
319 continue;
320
Christopher Faulet71a6a8e2017-07-27 16:33:28 +0200321 err_code = flt_init_per_thread(px);
322 if (err_code & (ERR_ABORT|ERR_FATAL)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100323 ha_alert("Failed to initialize filters for proxy '%s' for thread %u.\n",
324 px->id, tid);
Christopher Faulet71a6a8e2017-07-27 16:33:28 +0200325 return 0;
326 }
327 }
328 return 1;
329}
330
Christopher Fauletd7c91962015-04-30 11:48:27 +0200331/*
332 * Calls 'check' callback for all filters attached to a proxy. This happens
333 * after the configuration parsing but before filters initialization. Returns
334 * the number of encountered errors.
335 */
336int
337flt_check(struct proxy *proxy)
338{
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100339 struct flt_conf *fconf;
340 int err = 0;
Christopher Fauletd7c91962015-04-30 11:48:27 +0200341
Christopher Fauletff17b182019-01-07 15:03:22 +0100342 err += check_implicit_http_comp_flt(proxy);
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100343 list_for_each_entry(fconf, &proxy->filter_configs, list) {
344 if (fconf->ops->check)
345 err += fconf->ops->check(proxy, fconf);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200346 }
347 return err;
348}
349
350/*
351 * Calls 'denit' callback for all filters attached to a proxy. This happens when
352 * HAProxy is stopped.
353 */
354void
355flt_deinit(struct proxy *proxy)
356{
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100357 struct flt_conf *fconf, *back;
Christopher Fauletd7c91962015-04-30 11:48:27 +0200358
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100359 list_for_each_entry_safe(fconf, back, &proxy->filter_configs, list) {
Christopher Faulet73e7d1c2020-11-02 16:08:09 +0100360 if (proxy->state != PR_STSTOPPED && fconf->ops->deinit)
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100361 fconf->ops->deinit(proxy, fconf);
362 LIST_DEL(&fconf->list);
363 free(fconf);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200364 }
365}
366
Christopher Faulet71a6a8e2017-07-27 16:33:28 +0200367/*
368 * Calls 'denit_per_thread' callback for all filters attached to a proxy for
369 * each threads. This happens before exiting a thread.
370 */
371void
372flt_deinit_per_thread(struct proxy *proxy)
373{
374 struct flt_conf *fconf, *back;
375
376 list_for_each_entry_safe(fconf, back, &proxy->filter_configs, list) {
377 if (fconf->ops->deinit_per_thread)
378 fconf->ops->deinit_per_thread(proxy, fconf);
379 }
380}
381
382
383/* Calls flt_deinit_per_thread() for all proxies, see above */
384static void
385flt_deinit_all_per_thread()
386{
387 struct proxy *px;
388
Christopher Faulet73e7d1c2020-11-02 16:08:09 +0100389 for (px = proxies_list; px; px = px->next) {
390 if (px->state != PR_STSTOPPED)
391 flt_deinit_per_thread(px);
392 }
Christopher Faulet71a6a8e2017-07-27 16:33:28 +0200393}
394
Christopher Faulet92d36382015-11-05 13:35:03 +0100395/* Attaches a filter to a stream. Returns -1 if an error occurs, 0 otherwise. */
396static int
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100397flt_stream_add_filter(struct stream *s, struct flt_conf *fconf, unsigned int flags)
Christopher Faulet92d36382015-11-05 13:35:03 +0100398{
Christopher Faulet75bc9132018-11-30 15:18:09 +0100399 struct filter *f;
Christopher Faulet31ed32d2016-06-21 11:42:37 +0200400
Christopher Faulet0f17a9b2019-04-05 10:11:38 +0200401 if (IS_HTX_STRM(s) && !(fconf->flags & FLT_CFG_FL_HTX))
Christopher Faulet75bc9132018-11-30 15:18:09 +0100402 return 0;
403
404 f = pool_alloc(pool_head_filter);
Christopher Faulet92d36382015-11-05 13:35:03 +0100405 if (!f) /* not enough memory */
406 return -1;
407 memset(f, 0, sizeof(*f));
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100408 f->config = fconf;
Christopher Fauletda02e172015-12-04 09:25:05 +0100409 f->flags |= flags;
Christopher Faulet31ed32d2016-06-21 11:42:37 +0200410
411 if (FLT_OPS(f)->attach) {
412 int ret = FLT_OPS(f)->attach(s, f);
413 if (ret <= 0) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100414 pool_free(pool_head_filter, f);
Christopher Faulet31ed32d2016-06-21 11:42:37 +0200415 return ret;
416 }
417 }
418
Christopher Fauletfcf035c2015-12-03 11:48:03 +0100419 LIST_ADDQ(&strm_flt(s)->filters, &f->list);
Christopher Fauletda02e172015-12-04 09:25:05 +0100420 strm_flt(s)->flags |= STRM_FLT_FL_HAS_FILTERS;
Christopher Faulet92d36382015-11-05 13:35:03 +0100421 return 0;
422}
423
424/*
425 * Called when a stream is created. It attaches all frontend filters to the
426 * stream. Returns -1 if an error occurs, 0 otherwise.
427 */
428int
429flt_stream_init(struct stream *s)
430{
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100431 struct flt_conf *fconf;
Christopher Faulet92d36382015-11-05 13:35:03 +0100432
Christopher Fauletfcf035c2015-12-03 11:48:03 +0100433 memset(strm_flt(s), 0, sizeof(*strm_flt(s)));
434 LIST_INIT(&strm_flt(s)->filters);
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100435 list_for_each_entry(fconf, &strm_fe(s)->filter_configs, list) {
436 if (flt_stream_add_filter(s, fconf, 0) < 0)
Christopher Faulet92d36382015-11-05 13:35:03 +0100437 return -1;
438 }
439 return 0;
440}
441
442/*
443 * Called when a stream is closed or when analyze ends (For an HTTP stream, this
444 * happens after each request/response exchange). When analyze ends, backend
445 * filters are removed. When the stream is closed, all filters attached to the
446 * stream are removed.
447 */
448void
449flt_stream_release(struct stream *s, int only_backend)
450{
451 struct filter *filter, *back;
452
Christopher Fauletfcf035c2015-12-03 11:48:03 +0100453 list_for_each_entry_safe(filter, back, &strm_flt(s)->filters, list) {
Christopher Fauletda02e172015-12-04 09:25:05 +0100454 if (!only_backend || (filter->flags & FLT_FL_IS_BACKEND_FILTER)) {
Christopher Faulet31ed32d2016-06-21 11:42:37 +0200455 if (FLT_OPS(filter)->detach)
456 FLT_OPS(filter)->detach(s, filter);
Christopher Faulet92d36382015-11-05 13:35:03 +0100457 LIST_DEL(&filter->list);
Willy Tarreaubafbe012017-11-24 17:34:44 +0100458 pool_free(pool_head_filter, filter);
Christopher Faulet92d36382015-11-05 13:35:03 +0100459 }
460 }
Christopher Fauletfcf035c2015-12-03 11:48:03 +0100461 if (LIST_ISEMPTY(&strm_flt(s)->filters))
Christopher Fauletda02e172015-12-04 09:25:05 +0100462 strm_flt(s)->flags &= ~STRM_FLT_FL_HAS_FILTERS;
Christopher Faulet92d36382015-11-05 13:35:03 +0100463}
464
Christopher Fauletd7c91962015-04-30 11:48:27 +0200465/*
466 * Calls 'stream_start' for all filters attached to a stream. This happens when
467 * the stream is created, just after calling flt_stream_init
468 * function. Returns -1 if an error occurs, 0 otherwise.
469 */
470int
471flt_stream_start(struct stream *s)
472{
473 struct filter *filter;
474
Christopher Fauletfcf035c2015-12-03 11:48:03 +0100475 list_for_each_entry(filter, &strm_flt(s)->filters, list) {
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100476 if (FLT_OPS(filter)->stream_start && FLT_OPS(filter)->stream_start(s, filter) < 0)
Christopher Fauletd7c91962015-04-30 11:48:27 +0200477 return -1;
478 }
479 return 0;
480}
481
482/*
483 * Calls 'stream_stop' for all filters attached to a stream. This happens when
484 * the stream is stopped, just before calling flt_stream_release function.
485 */
486void
487flt_stream_stop(struct stream *s)
488{
489 struct filter *filter;
490
Christopher Fauletfcf035c2015-12-03 11:48:03 +0100491 list_for_each_entry(filter, &strm_flt(s)->filters, list) {
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100492 if (FLT_OPS(filter)->stream_stop)
493 FLT_OPS(filter)->stream_stop(s, filter);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200494 }
495}
496
Christopher Faulet92d36382015-11-05 13:35:03 +0100497/*
Christopher Fauleta00d8172016-11-10 14:58:05 +0100498 * Calls 'check_timeouts' for all filters attached to a stream. This happens when
499 * the stream is woken up because of expired timer.
500 */
501void
502flt_stream_check_timeouts(struct stream *s)
503{
504 struct filter *filter;
505
506 list_for_each_entry(filter, &strm_flt(s)->filters, list) {
507 if (FLT_OPS(filter)->check_timeouts)
508 FLT_OPS(filter)->check_timeouts(s, filter);
509 }
510}
511
512/*
Christopher Faulet92d36382015-11-05 13:35:03 +0100513 * Called when a backend is set for a stream. If the frontend and the backend
Christopher Faulet31ed32d2016-06-21 11:42:37 +0200514 * are not the same, this function attaches all backend filters to the
515 * stream. Returns -1 if an error occurs, 0 otherwise.
Christopher Faulet92d36382015-11-05 13:35:03 +0100516 */
517int
518flt_set_stream_backend(struct stream *s, struct proxy *be)
519{
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100520 struct flt_conf *fconf;
Christopher Faulet31ed32d2016-06-21 11:42:37 +0200521 struct filter *filter;
Christopher Faulet92d36382015-11-05 13:35:03 +0100522
523 if (strm_fe(s) == be)
Christopher Faulet31ed32d2016-06-21 11:42:37 +0200524 goto end;
Christopher Faulet92d36382015-11-05 13:35:03 +0100525
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100526 list_for_each_entry(fconf, &be->filter_configs, list) {
527 if (flt_stream_add_filter(s, fconf, FLT_FL_IS_BACKEND_FILTER) < 0)
Christopher Faulet92d36382015-11-05 13:35:03 +0100528 return -1;
529 }
Christopher Faulet31ed32d2016-06-21 11:42:37 +0200530
531 end:
532 list_for_each_entry(filter, &strm_flt(s)->filters, list) {
533 if (FLT_OPS(filter)->stream_set_backend &&
534 FLT_OPS(filter)->stream_set_backend(s, filter, be) < 0)
535 return -1;
536 }
537
Christopher Faulet92d36382015-11-05 13:35:03 +0100538 return 0;
539}
540
Christopher Fauletd7c91962015-04-30 11:48:27 +0200541/*
542 * Calls 'http_data' callback for all "data" filters attached to a stream. This
543 * function is called when incoming data are available (excluding chunks
544 * envelope for chunked messages) in the AN_REQ_HTTP_XFER_BODY and
545 * AN_RES_HTTP_XFER_BODY analyzers. It takes care to update the next offset of
546 * filters and adjusts available data to be sure that a filter cannot parse more
547 * data than its predecessors. A filter can choose to not consume all available
548 * data. Returns -1 if an error occurs, the number of consumed bytes otherwise.
Christopher Faulet75bc9132018-11-30 15:18:09 +0100549 *
550 * DEPRECATED FUNCTION - CALLED FROM LEGACY HTTP ANALYZERS
Christopher Fauletd7c91962015-04-30 11:48:27 +0200551 */
552int
553flt_http_data(struct stream *s, struct http_msg *msg)
554{
Christopher Fauletfcf035c2015-12-03 11:48:03 +0100555 struct filter *filter;
Christopher Fauletd7c91962015-04-30 11:48:27 +0200556 unsigned int buf_i;
Christopher Faulet55048a42016-06-21 10:44:32 +0200557 int delta = 0, ret = 0;
Christopher Fauletd7c91962015-04-30 11:48:27 +0200558
Christopher Fauletd7c91962015-04-30 11:48:27 +0200559 /* Save buffer state */
Willy Tarreau44a41a82018-06-19 07:16:31 +0200560 buf_i = ci_data(msg->chn);
Christopher Fauletfcf035c2015-12-03 11:48:03 +0100561
Christopher Fauletfcf035c2015-12-03 11:48:03 +0100562 list_for_each_entry(filter, &strm_flt(s)->filters, list) {
Christopher Fauletda02e172015-12-04 09:25:05 +0100563 unsigned int *nxt;
564
565 /* Call "data" filters only */
566 if (!IS_DATA_FILTER(filter, msg->chn))
567 continue;
568
Christopher Faulet2fb28802015-12-01 10:40:57 +0100569 /* If the HTTP parser is ahead, we update the next offset of the
570 * current filter. This happens for chunked messages, at the
Joseph Herlantb35ea682018-11-15 12:24:23 -0800571 * beginning of a new chunk. */
Christopher Fauletda02e172015-12-04 09:25:05 +0100572 nxt = &FLT_NXT(filter, msg->chn);
573 if (msg->next > *nxt)
574 *nxt = msg->next;
575
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100576 if (FLT_OPS(filter)->http_data) {
Willy Tarreau44a41a82018-06-19 07:16:31 +0200577 unsigned int i = ci_data(msg->chn);
Christopher Faulet55048a42016-06-21 10:44:32 +0200578
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100579 ret = FLT_OPS(filter)->http_data(s, filter, msg);
Christopher Fauletda02e172015-12-04 09:25:05 +0100580 if (ret < 0)
Christopher Fauletd7c91962015-04-30 11:48:27 +0200581 break;
Willy Tarreau44a41a82018-06-19 07:16:31 +0200582 delta += (int)(ci_data(msg->chn) - i);
Christopher Fauletfcf035c2015-12-03 11:48:03 +0100583
584 /* Update the next offset of the current filter */
Christopher Fauletda02e172015-12-04 09:25:05 +0100585 *nxt += ret;
Christopher Fauletfcf035c2015-12-03 11:48:03 +0100586
587 /* And set this value as the bound for the next
588 * filter. It will not able to parse more data than this
589 * one. */
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200590 b_set_data(&msg->chn->buf, co_data(msg->chn) + *nxt);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200591 }
592 else {
Christopher Fauletfcf035c2015-12-03 11:48:03 +0100593 /* Consume all available data and update the next offset
594 * of the current filter. buf->i is untouched here. */
Willy Tarreau44a41a82018-06-19 07:16:31 +0200595 ret = MIN(msg->chunk_len + msg->next, ci_data(msg->chn)) - *nxt;
Christopher Fauletda02e172015-12-04 09:25:05 +0100596 *nxt += ret;
Christopher Fauletd7c91962015-04-30 11:48:27 +0200597 }
Christopher Fauletd7c91962015-04-30 11:48:27 +0200598 }
Christopher Fauletfcf035c2015-12-03 11:48:03 +0100599
Christopher Fauletd7c91962015-04-30 11:48:27 +0200600 /* Restore the original buffer state */
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200601 b_set_data(&msg->chn->buf, co_data(msg->chn) + buf_i + delta);
Christopher Fauletfcf035c2015-12-03 11:48:03 +0100602
Christopher Fauletd7c91962015-04-30 11:48:27 +0200603 return ret;
604}
605
Christopher Fauletd7c91962015-04-30 11:48:27 +0200606/*
607 * Calls 'http_chunk_trailers' callback for all "data" filters attached to a
608 * stream. This function is called for chunked messages only when a part of the
609 * trailers was parsed in the AN_REQ_HTTP_XFER_BODY and AN_RES_HTTP_XFER_BODY
610 * analyzers. Filters can know how much data were parsed by the HTTP parsing
611 * until the last call with the msg->sol value. Returns a negative value if an
612 * error occurs, any other value otherwise.
Christopher Faulet75bc9132018-11-30 15:18:09 +0100613 *
614 * DEPRECATED FUNCTION - CALLED FROM LEGACY HTTP ANALYZERS
Christopher Fauletd7c91962015-04-30 11:48:27 +0200615 */
616int
617flt_http_chunk_trailers(struct stream *s, struct http_msg *msg)
618{
Christopher Faulet2fb28802015-12-01 10:40:57 +0100619 struct filter *filter;
Christopher Fauletda02e172015-12-04 09:25:05 +0100620 int ret = 1;
Christopher Fauletd7c91962015-04-30 11:48:27 +0200621
Christopher Fauletfcf035c2015-12-03 11:48:03 +0100622 list_for_each_entry(filter, &strm_flt(s)->filters, list) {
Christopher Fauletda02e172015-12-04 09:25:05 +0100623 unsigned int *nxt;
624
625 /* Call "data" filters only */
626 if (!IS_DATA_FILTER(filter, msg->chn))
627 continue;
628
Christopher Faulet2fb28802015-12-01 10:40:57 +0100629 /* Be sure to set the next offset of the filter at the right
630 * place. This is really useful when the first part of the
631 * trailers was parsed. */
Christopher Fauletda02e172015-12-04 09:25:05 +0100632 nxt = &FLT_NXT(filter, msg->chn);
633 *nxt = msg->next;
634
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100635 if (FLT_OPS(filter)->http_chunk_trailers) {
636 ret = FLT_OPS(filter)->http_chunk_trailers(s, filter, msg);
Christopher Faulet2fb28802015-12-01 10:40:57 +0100637 if (ret < 0)
638 break;
Christopher Fauletd7c91962015-04-30 11:48:27 +0200639 }
Christopher Faulet2fb28802015-12-01 10:40:57 +0100640 /* Update the next offset of the current filter. Here all data
641 * are always consumed. */
Christopher Fauletda02e172015-12-04 09:25:05 +0100642 *nxt += msg->sol;
Christopher Faulet2fb28802015-12-01 10:40:57 +0100643 }
Christopher Fauletd7c91962015-04-30 11:48:27 +0200644 return ret;
645}
646
647/*
648 * Calls 'http_end' callback for all filters attached to a stream. All filters
649 * are called here, but only if there is at least one "data" filter. This
650 * functions is called when all data were parsed and forwarded. 'http_end'
651 * callback is resumable, so this function returns a negative value if an error
652 * occurs, 0 if it needs to wait for some reason, any other value otherwise.
Christopher Faulet75bc9132018-11-30 15:18:09 +0100653 *
654 * Be carefull, this function can be called from the HTTP legacy analyzers or
655 * from HTX analyzers. If your filter is compatible with the two modes, use
656 * IS_HTX_STRM macro on the stream.
Christopher Fauletd7c91962015-04-30 11:48:27 +0200657 */
658int
659flt_http_end(struct stream *s, struct http_msg *msg)
660{
661 int ret = 1;
662
Christopher Fauletd7c91962015-04-30 11:48:27 +0200663 RESUME_FILTER_LOOP(s, msg->chn) {
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100664 if (FLT_OPS(filter)->http_end) {
665 ret = FLT_OPS(filter)->http_end(s, filter, msg);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200666 if (ret <= 0)
667 BREAK_EXECUTION(s, msg->chn, end);
668 }
Christopher Fauletd7c91962015-04-30 11:48:27 +0200669 } RESUME_FILTER_END;
670end:
671 return ret;
672}
673
674/*
675 * Calls 'http_reset' callback for all filters attached to a stream. This
676 * happens when a 100-continue response is received.
Christopher Faulet75bc9132018-11-30 15:18:09 +0100677 *
678 * Be carefull, this function can be called from the HTTP legacy analyzers or
679 * from HTX analyzers. If your filter is compatible with the two modes, use
680 * IS_HTX_STRM macro on the stream.
Christopher Fauletd7c91962015-04-30 11:48:27 +0200681 */
682void
683flt_http_reset(struct stream *s, struct http_msg *msg)
684{
685 struct filter *filter;
686
Christopher Fauletfcf035c2015-12-03 11:48:03 +0100687 list_for_each_entry(filter, &strm_flt(s)->filters, list) {
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100688 if (FLT_OPS(filter)->http_reset)
689 FLT_OPS(filter)->http_reset(s, filter, msg);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200690 }
691}
692
693/*
694 * Calls 'http_reply' callback for all filters attached to a stream when HA
695 * decides to stop the HTTP message processing.
Christopher Faulet75bc9132018-11-30 15:18:09 +0100696 *
697 * Be carefull, this function can be called from the HTTP legacy analyzers or
698 * from HTX analyzers. If your filter is compatible with the two modes, use
699 * IS_HTX_STRM macro on the stream.
Christopher Fauletd7c91962015-04-30 11:48:27 +0200700 */
701void
Willy Tarreau83061a82018-07-13 11:56:34 +0200702flt_http_reply(struct stream *s, short status, const struct buffer *msg)
Christopher Fauletd7c91962015-04-30 11:48:27 +0200703{
704 struct filter *filter;
705
Christopher Fauletfcf035c2015-12-03 11:48:03 +0100706 list_for_each_entry(filter, &strm_flt(s)->filters, list) {
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100707 if (FLT_OPS(filter)->http_reply)
708 FLT_OPS(filter)->http_reply(s, filter, status, msg);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200709 }
710}
711
712/*
Christopher Faulet75bc9132018-11-30 15:18:09 +0100713 * Calls 'http_forward_data' callback for all "data" filters attached to a HTTP
714 * legacy stream. This function is called when some data can be forwarded in the
Christopher Fauletd7c91962015-04-30 11:48:27 +0200715 * AN_REQ_HTTP_XFER_BODY and AN_RES_HTTP_XFER_BODY analyzers. It takes care to
716 * update the forward offset of filters and adjusts "forwardable" data to be
717 * sure that a filter cannot forward more data than its predecessors. A filter
718 * can choose to not forward all parsed data. Returns a negative value if an
719 * error occurs, else the number of forwarded bytes.
Christopher Faulet75bc9132018-11-30 15:18:09 +0100720 *
721 * DEPRECATED FUNCTION - CALLED FROM LEGACY HTTP ANALYZERS
Christopher Fauletd7c91962015-04-30 11:48:27 +0200722 */
723int
724flt_http_forward_data(struct stream *s, struct http_msg *msg, unsigned int len)
725{
Christopher Fauletfcf035c2015-12-03 11:48:03 +0100726 struct filter *filter;
Christopher Fauletd7c91962015-04-30 11:48:27 +0200727 int ret = len;
728
Christopher Fauletfcf035c2015-12-03 11:48:03 +0100729 list_for_each_entry(filter, &strm_flt(s)->filters, list) {
Christopher Fauletda02e172015-12-04 09:25:05 +0100730 unsigned int *nxt, *fwd;
731
732 /* Call "data" filters only */
733 if (!IS_DATA_FILTER(filter, msg->chn))
734 continue;
735
Christopher Faulet2fb28802015-12-01 10:40:57 +0100736 /* If the HTTP parser is ahead, we update the next offset of the
737 * current filter. This happens for chunked messages, when the
738 * chunk envelope is parsed. */
Christopher Fauletda02e172015-12-04 09:25:05 +0100739 nxt = &FLT_NXT(filter, msg->chn);
740 fwd = &FLT_FWD(filter, msg->chn);
741 if (msg->next > *nxt)
742 *nxt = msg->next;
743
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100744 if (FLT_OPS(filter)->http_forward_data) {
Christopher Fauletda02e172015-12-04 09:25:05 +0100745 /* Remove bytes that the current filter considered as
746 * forwarded */
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100747 ret = FLT_OPS(filter)->http_forward_data(s, filter, msg, ret - *fwd);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200748 if (ret < 0)
749 goto end;
750 }
751
752 /* Adjust bytes that the current filter considers as
753 * forwarded */
Christopher Fauletda02e172015-12-04 09:25:05 +0100754 *fwd += ret;
Christopher Fauletd7c91962015-04-30 11:48:27 +0200755
756 /* And set this value as the bound for the next filter. It will
757 * not able to forward more data than the current one. */
Christopher Fauletda02e172015-12-04 09:25:05 +0100758 ret = *fwd;
Christopher Fauletd7c91962015-04-30 11:48:27 +0200759 }
760
761 if (!ret)
762 goto end;
763
764 /* Finally, adjust filters offsets by removing data that HAProxy will
765 * forward. */
Christopher Fauletfcf035c2015-12-03 11:48:03 +0100766 list_for_each_entry(filter, &strm_flt(s)->filters, list) {
Christopher Fauletda02e172015-12-04 09:25:05 +0100767 if (!IS_DATA_FILTER(filter, msg->chn))
768 continue;
Christopher Fauletd7c91962015-04-30 11:48:27 +0200769 FLT_NXT(filter, msg->chn) -= ret;
770 FLT_FWD(filter, msg->chn) -= ret;
771 }
772 end:
773 return ret;
774}
775
776/*
Christopher Faulet75bc9132018-11-30 15:18:09 +0100777 * Calls 'http_payload' callback for all "data" filters attached to a
778 * stream. This function is called when some data can be forwarded in the
779 * AN_REQ_HTTP_XFER_BODY and AN_RES_HTTP_XFER_BODY analyzers. It takes care to
780 * update the filters and the stream offset to be sure that a filter cannot
781 * forward more data than its predecessors. A filter can choose to not forward
782 * all data. Returns a negative value if an error occurs, else the number of
783 * forwarded bytes.
784 *
785 * Be carefull, this callback is only called from HTX analyzers. So the
786 * channel's buffer must be considered as an HTX structured. Of course, your
787 * filter must support HTX streams.
788 */
789int
790flt_http_payload(struct stream *s, struct http_msg *msg, unsigned int len)
791{
792 struct filter *filter;
793 unsigned long long *strm_off = &FLT_STRM_OFF(s, msg->chn);
Christopher Faulet421e7692019-06-13 11:16:45 +0200794 unsigned int out = co_data(msg->chn);
Christopher Faulet11920a42020-02-26 15:47:22 +0100795 int ret, data;
Christopher Faulet75bc9132018-11-30 15:18:09 +0100796
Christopher Faulet11920a42020-02-26 15:47:22 +0100797 ret = data = len - out;
Christopher Faulet75bc9132018-11-30 15:18:09 +0100798 list_for_each_entry(filter, &strm_flt(s)->filters, list) {
799 /* Call "data" filters only */
800 if (!IS_DATA_FILTER(filter, msg->chn))
801 continue;
802 if (FLT_OPS(filter)->http_payload) {
803 unsigned long long *flt_off = &FLT_OFF(filter, msg->chn);
804 unsigned int offset = *flt_off - *strm_off;
805
Christopher Fauleta2f67b32020-02-07 16:40:33 +0100806 ret = FLT_OPS(filter)->http_payload(s, filter, msg, out + offset, data - offset);
Christopher Faulet75bc9132018-11-30 15:18:09 +0100807 if (ret < 0)
808 goto end;
Christopher Faulet31df6362020-02-24 16:20:09 +0100809 data = ret + *flt_off - *strm_off;
Christopher Faulet75bc9132018-11-30 15:18:09 +0100810 *flt_off += ret;
Christopher Faulet75bc9132018-11-30 15:18:09 +0100811 }
812 }
Christopher Fauleta2f67b32020-02-07 16:40:33 +0100813
814 /* Only forward data if the last filter decides to forward something */
815 if (ret > 0) {
816 ret = data;
817 *strm_off += ret;
818 }
Christopher Faulet75bc9132018-11-30 15:18:09 +0100819 end:
820 return ret;
821}
822
823/*
Christopher Fauletd7c91962015-04-30 11:48:27 +0200824 * Calls 'channel_start_analyze' callback for all filters attached to a
825 * stream. This function is called when we start to analyze a request or a
826 * response. For frontend filters, it is called before all other analyzers. For
827 * backend ones, it is called before all backend
828 * analyzers. 'channel_start_analyze' callback is resumable, so this function
829 * returns 0 if an error occurs or if it needs to wait, any other value
830 * otherwise.
831 */
832int
833flt_start_analyze(struct stream *s, struct channel *chn, unsigned int an_bit)
834{
835 int ret = 1;
836
837 /* If this function is called, this means there is at least one filter,
838 * so we do not need to check the filter list's emptiness. */
839
Christopher Faulete6006242017-03-10 11:52:44 +0100840 /* Set flag on channel to tell that the channel is filtered */
841 chn->flags |= CF_FLT_ANALYZE;
842
Christopher Fauletd7c91962015-04-30 11:48:27 +0200843 RESUME_FILTER_LOOP(s, chn) {
Christopher Faulet0184ea72017-01-05 14:06:34 +0100844 if (!(chn->flags & CF_ISRESP)) {
845 if (an_bit == AN_REQ_FLT_START_BE &&
846 !(filter->flags & FLT_FL_IS_BACKEND_FILTER))
847 continue;
848 }
849 else {
850 if (an_bit == AN_RES_FLT_START_BE &&
851 !(filter->flags & FLT_FL_IS_BACKEND_FILTER))
852 continue;
853 }
Christopher Fauletd7c91962015-04-30 11:48:27 +0200854
Christopher Fauletfcf035c2015-12-03 11:48:03 +0100855 FLT_NXT(filter, chn) = 0;
856 FLT_FWD(filter, chn) = 0;
Christopher Fauletd7c91962015-04-30 11:48:27 +0200857
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100858 if (FLT_OPS(filter)->channel_start_analyze) {
859 ret = FLT_OPS(filter)->channel_start_analyze(s, filter, chn);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200860 if (ret <= 0)
861 BREAK_EXECUTION(s, chn, end);
862 }
863 } RESUME_FILTER_END;
864
865 end:
866 return handle_analyzer_result(s, chn, an_bit, ret);
867}
868
869/*
Christopher Faulet3a394fa2016-05-11 17:13:39 +0200870 * Calls 'channel_pre_analyze' callback for all filters attached to a
871 * stream. This function is called BEFORE each analyzer attached to a channel,
872 * expects analyzers responsible for data sending. 'channel_pre_analyze'
873 * callback is resumable, so this function returns 0 if an error occurs or if it
874 * needs to wait, any other value otherwise.
875 *
876 * Note this function can be called many times for the same analyzer. In fact,
877 * it is called until the analyzer finishes its processing.
Christopher Fauletd7c91962015-04-30 11:48:27 +0200878 */
879int
Christopher Faulet3a394fa2016-05-11 17:13:39 +0200880flt_pre_analyze(struct stream *s, struct channel *chn, unsigned int an_bit)
Christopher Fauletd7c91962015-04-30 11:48:27 +0200881{
882 int ret = 1;
883
Christopher Fauletd7c91962015-04-30 11:48:27 +0200884 RESUME_FILTER_LOOP(s, chn) {
Christopher Faulet3a394fa2016-05-11 17:13:39 +0200885 if (FLT_OPS(filter)->channel_pre_analyze && (filter->pre_analyzers & an_bit)) {
886 ret = FLT_OPS(filter)->channel_pre_analyze(s, filter, chn, an_bit);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200887 if (ret <= 0)
888 BREAK_EXECUTION(s, chn, check_result);
889 }
890 } RESUME_FILTER_END;
891
892 check_result:
Christopher Faulet309c6412015-12-02 09:57:32 +0100893 return handle_analyzer_result(s, chn, 0, ret);
894}
895
896/*
Christopher Faulet3a394fa2016-05-11 17:13:39 +0200897 * Calls 'channel_post_analyze' callback for all filters attached to a
898 * stream. This function is called AFTER each analyzer attached to a channel,
899 * expects analyzers responsible for data sending. 'channel_post_analyze'
900 * callback is NOT resumable, so this function returns a 0 if an error occurs,
901 * any other value otherwise.
902 *
903 * Here, AFTER means when the analyzer finishes its processing.
904 */
905int
906flt_post_analyze(struct stream *s, struct channel *chn, unsigned int an_bit)
907{
908 struct filter *filter;
909 int ret = 1;
910
911 list_for_each_entry(filter, &strm_flt(s)->filters, list) {
912 if (FLT_OPS(filter)->channel_post_analyze && (filter->post_analyzers & an_bit)) {
913 ret = FLT_OPS(filter)->channel_post_analyze(s, filter, chn, an_bit);
914 if (ret < 0)
915 break;
916 }
917 }
918 return handle_analyzer_result(s, chn, 0, ret);
919}
920
921/*
Christopher Faulet0184ea72017-01-05 14:06:34 +0100922 * This function is the AN_REQ/RES_FLT_HTTP_HDRS analyzer, used to filter HTTP
923 * headers or a request or a response. Returns 0 if an error occurs or if it
924 * needs to wait, any other value otherwise.
Christopher Faulet75bc9132018-11-30 15:18:09 +0100925 *
926 * Be carefull, this function can be called from the HTTP legacy analyzers or
927 * from HTX analyzers. If your filter is compatible with the two modes, use
928 * IS_HTX_STRM macro on the stream.
Christopher Faulet309c6412015-12-02 09:57:32 +0100929 */
930int
931flt_analyze_http_headers(struct stream *s, struct channel *chn, unsigned int an_bit)
932{
Christopher Faulet1339d742016-05-11 16:48:33 +0200933 struct filter *filter;
934 struct http_msg *msg;
935 int ret = 1;
Christopher Faulet309c6412015-12-02 09:57:32 +0100936
Christopher Faulet1339d742016-05-11 16:48:33 +0200937 msg = ((chn->flags & CF_ISRESP) ? &s->txn->rsp : &s->txn->req);
Christopher Faulet309c6412015-12-02 09:57:32 +0100938 RESUME_FILTER_LOOP(s, chn) {
Christopher Faulet1339d742016-05-11 16:48:33 +0200939 if (FLT_OPS(filter)->http_headers) {
940 ret = FLT_OPS(filter)->http_headers(s, filter, msg);
Christopher Faulet309c6412015-12-02 09:57:32 +0100941 if (ret <= 0)
942 BREAK_EXECUTION(s, chn, check_result);
943 }
944 } RESUME_FILTER_END;
945
Christopher Fauletdf7d6812020-02-12 15:31:20 +0100946 if (IS_HTX_STRM(s)) {
947 if (HAS_DATA_FILTERS(s, chn)) {
948 size_t data = http_get_hdrs_size(htxbuf(&chn->buf));
949 struct filter *f;
950
951 list_for_each_entry(f, &strm_flt(s)->filters, list) {
952 if (IS_DATA_FILTER(f, chn))
953 FLT_OFF(f, chn) = data;
954 }
955 }
956 }
Christopher Faulet75bc9132018-11-30 15:18:09 +0100957 else {
958 /* We increase next offset of all "data" filters after all processing on
959 * headers because any filter can alter them. So the definitive size of
960 * headers (msg->sov) is only known when all filters have been
961 * called. */
962 list_for_each_entry(filter, &strm_flt(s)->filters, list) {
963 /* Handle "data" filters only */
964 if (!IS_DATA_FILTER(filter, chn))
965 continue;
966 FLT_NXT(filter, chn) = msg->sov;
967 }
Christopher Faulet309c6412015-12-02 09:57:32 +0100968 }
969
970 check_result:
971 return handle_analyzer_result(s, chn, an_bit, ret);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200972}
973
974/*
975 * Calls 'channel_end_analyze' callback for all filters attached to a
976 * stream. This function is called when we stop to analyze a request or a
977 * response. It is called after all other analyzers. 'channel_end_analyze'
978 * callback is resumable, so this function returns 0 if an error occurs or if it
979 * needs to wait, any other value otherwise.
980 */
981int
982flt_end_analyze(struct stream *s, struct channel *chn, unsigned int an_bit)
983{
984 int ret = 1;
985
Christopher Faulete6006242017-03-10 11:52:44 +0100986 /* Check if all filters attached on the stream have finished their
987 * processing on this channel. */
988 if (!(chn->flags & CF_FLT_ANALYZE))
989 goto sync;
990
Christopher Fauletd7c91962015-04-30 11:48:27 +0200991 RESUME_FILTER_LOOP(s, chn) {
Christopher Fauletfcf035c2015-12-03 11:48:03 +0100992 FLT_NXT(filter, chn) = 0;
993 FLT_FWD(filter, chn) = 0;
Christopher Fauletda02e172015-12-04 09:25:05 +0100994 unregister_data_filter(s, chn, filter);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200995
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100996 if (FLT_OPS(filter)->channel_end_analyze) {
997 ret = FLT_OPS(filter)->channel_end_analyze(s, filter, chn);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200998 if (ret <= 0)
999 BREAK_EXECUTION(s, chn, end);
1000 }
1001 } RESUME_FILTER_END;
1002
Christopher Faulete6006242017-03-10 11:52:44 +01001003 end:
1004 /* We don't remove yet this analyzer because we need to synchronize the
1005 * both channels. So here, we just remove the flag CF_FLT_ANALYZE. */
1006 ret = handle_analyzer_result(s, chn, 0, ret);
Christopher Faulet570f7992017-07-06 15:53:02 +02001007 if (ret) {
Christopher Faulete6006242017-03-10 11:52:44 +01001008 chn->flags &= ~CF_FLT_ANALYZE;
Christopher Faulet02c7b222015-12-22 12:01:29 +01001009
Christopher Faulet570f7992017-07-06 15:53:02 +02001010 /* Pretend there is an activity on both channels. Flag on the
1011 * current one will be automatically removed, so only the other
1012 * one will remain. This is a way to be sure that
1013 * 'channel_end_analyze' callback will have a chance to be
1014 * called at least once for the other side to finish the current
Joseph Herlantb35ea682018-11-15 12:24:23 -08001015 * processing. Of course, this is the filter responsibility to
Christopher Faulet570f7992017-07-06 15:53:02 +02001016 * wakeup the stream if it choose to loop on this callback. */
1017 s->req.flags |= CF_WAKE_ONCE;
1018 s->res.flags |= CF_WAKE_ONCE;
1019 }
1020
1021
Christopher Faulete6006242017-03-10 11:52:44 +01001022 sync:
1023 /* Now we can check if filters have finished their work on the both
1024 * channels */
1025 if (!(s->req.flags & CF_FLT_ANALYZE) && !(s->res.flags & CF_FLT_ANALYZE)) {
1026 /* Sync channels by removing this analyzer for the both channels */
1027 s->req.analysers &= ~AN_REQ_FLT_END;
1028 s->res.analysers &= ~AN_RES_FLT_END;
Christopher Fauletc6062be2016-10-31 11:22:37 +01001029
Christopher Faulete6006242017-03-10 11:52:44 +01001030 /* Clean up the HTTP transaction if needed */
1031 if (s->txn && (s->txn->flags & TX_WAIT_CLEANUP))
1032 http_end_txn_clean_session(s);
Christopher Fauletd7c91962015-04-30 11:48:27 +02001033
Christopher Faulete6006242017-03-10 11:52:44 +01001034 /* Remove backend filters from the list */
1035 flt_stream_release(s, 1);
Christopher Fauletd7c91962015-04-30 11:48:27 +02001036 }
Christopher Faulet2b553de2017-03-30 11:13:22 +02001037
Christopher Fauletd7c91962015-04-30 11:48:27 +02001038 return ret;
1039}
1040
1041
1042/*
1043 * Calls 'tcp_data' callback for all "data" filters attached to a stream. This
1044 * function is called when incoming data are available. It takes care to update
1045 * the next offset of filters and adjusts available data to be sure that a
1046 * filter cannot parse more data than its predecessors. A filter can choose to
1047 * not consume all available data. Returns -1 if an error occurs, the number of
1048 * consumed bytes otherwise.
1049 */
1050static int
1051flt_data(struct stream *s, struct channel *chn)
1052{
Christopher Fauletda02e172015-12-04 09:25:05 +01001053 struct filter *filter;
Christopher Fauletd7c91962015-04-30 11:48:27 +02001054 unsigned int buf_i;
Christopher Faulet55048a42016-06-21 10:44:32 +02001055 int delta = 0, ret = 0;
Christopher Fauletd7c91962015-04-30 11:48:27 +02001056
1057 /* Save buffer state */
Willy Tarreau44a41a82018-06-19 07:16:31 +02001058 buf_i = ci_data(chn);
Christopher Fauletfcf035c2015-12-03 11:48:03 +01001059
1060 list_for_each_entry(filter, &strm_flt(s)->filters, list) {
Christopher Fauletda02e172015-12-04 09:25:05 +01001061 unsigned int *nxt;
1062
1063 /* Call "data" filters only */
1064 if (!IS_DATA_FILTER(filter, chn))
1065 continue;
1066
1067 nxt = &FLT_NXT(filter, chn);
Christopher Faulet443ea1a2016-02-04 13:40:26 +01001068 if (FLT_OPS(filter)->tcp_data) {
Willy Tarreau44a41a82018-06-19 07:16:31 +02001069 unsigned int i = ci_data(chn);
Christopher Faulet55048a42016-06-21 10:44:32 +02001070
Christopher Faulet443ea1a2016-02-04 13:40:26 +01001071 ret = FLT_OPS(filter)->tcp_data(s, filter, chn);
Christopher Fauletd7c91962015-04-30 11:48:27 +02001072 if (ret < 0)
1073 break;
Willy Tarreau44a41a82018-06-19 07:16:31 +02001074 delta += (int)(ci_data(chn) - i);
Christopher Fauletd7c91962015-04-30 11:48:27 +02001075
Christopher Fauletfcf035c2015-12-03 11:48:03 +01001076 /* Increase next offset of the current filter */
Christopher Fauletda02e172015-12-04 09:25:05 +01001077 *nxt += ret;
Christopher Fauletfcf035c2015-12-03 11:48:03 +01001078
1079 /* And set this value as the bound for the next
1080 * filter. It will not able to parse more data than the
1081 * current one. */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001082 b_set_data(&chn->buf, co_data(chn) + *nxt);
Christopher Fauletfcf035c2015-12-03 11:48:03 +01001083 }
1084 else {
1085 /* Consume all available data */
Willy Tarreau44a41a82018-06-19 07:16:31 +02001086 *nxt = ci_data(chn);
Christopher Fauletfcf035c2015-12-03 11:48:03 +01001087 }
Christopher Fauletd7c91962015-04-30 11:48:27 +02001088
1089 /* Update <ret> value to be sure to have the last one when we
Christopher Fauletda02e172015-12-04 09:25:05 +01001090 * exit from the loop. This value will be used to know how much
1091 * data are "forwardable" */
1092 ret = *nxt;
Christopher Fauletd7c91962015-04-30 11:48:27 +02001093 }
Christopher Fauletfcf035c2015-12-03 11:48:03 +01001094
1095 /* Restore the original buffer state */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001096 b_set_data(&chn->buf, co_data(chn) + buf_i + delta);
Christopher Fauletfcf035c2015-12-03 11:48:03 +01001097
Christopher Fauletd7c91962015-04-30 11:48:27 +02001098 return ret;
1099}
1100
1101/*
1102 * Calls 'tcp_forward_data' callback for all "data" filters attached to a
1103 * stream. This function is called when some data can be forwarded. It takes
1104 * care to update the forward offset of filters and adjusts "forwardable" data
1105 * to be sure that a filter cannot forward more data than its predecessors. A
1106 * filter can choose to not forward all parsed data. Returns a negative value if
1107 * an error occurs, else the number of forwarded bytes.
1108 */
1109static int
1110flt_forward_data(struct stream *s, struct channel *chn, unsigned int len)
1111{
Christopher Fauletda02e172015-12-04 09:25:05 +01001112 struct filter *filter;
Christopher Fauletd7c91962015-04-30 11:48:27 +02001113 int ret = len;
1114
Christopher Fauletfcf035c2015-12-03 11:48:03 +01001115 list_for_each_entry(filter, &strm_flt(s)->filters, list) {
Christopher Fauletda02e172015-12-04 09:25:05 +01001116 unsigned int *fwd;
1117
1118 /* Call "data" filters only */
1119 if (!IS_DATA_FILTER(filter, chn))
1120 continue;
1121
1122 fwd = &FLT_FWD(filter, chn);
Christopher Faulet443ea1a2016-02-04 13:40:26 +01001123 if (FLT_OPS(filter)->tcp_forward_data) {
Christopher Fauletd7c91962015-04-30 11:48:27 +02001124 /* Remove bytes that the current filter considered as
1125 * forwarded */
Christopher Faulet443ea1a2016-02-04 13:40:26 +01001126 ret = FLT_OPS(filter)->tcp_forward_data(s, filter, chn, ret - *fwd);
Christopher Fauletd7c91962015-04-30 11:48:27 +02001127 if (ret < 0)
1128 goto end;
1129 }
1130
Christopher Fauletda02e172015-12-04 09:25:05 +01001131 /* Adjust bytes that the current filter considers as
Christopher Fauletd7c91962015-04-30 11:48:27 +02001132 * forwarded */
Christopher Fauletda02e172015-12-04 09:25:05 +01001133 *fwd += ret;
Christopher Fauletd7c91962015-04-30 11:48:27 +02001134
1135 /* And set this value as the bound for the next filter. It will
1136 * not able to forward more data than the current one. */
Christopher Fauletda02e172015-12-04 09:25:05 +01001137 ret = *fwd;
Christopher Fauletd7c91962015-04-30 11:48:27 +02001138 }
1139
1140 if (!ret)
1141 goto end;
1142
Christopher Fauletda02e172015-12-04 09:25:05 +01001143 /* Finally, adjust filters offsets by removing data that HAProxy will
1144 * forward. */
Christopher Fauletfcf035c2015-12-03 11:48:03 +01001145 list_for_each_entry(filter, &strm_flt(s)->filters, list) {
Christopher Fauletda02e172015-12-04 09:25:05 +01001146 if (!IS_DATA_FILTER(filter, chn))
1147 continue;
Christopher Fauletd7c91962015-04-30 11:48:27 +02001148 FLT_NXT(filter, chn) -= ret;
1149 FLT_FWD(filter, chn) -= ret;
1150 }
1151
Christopher Fauletd7c91962015-04-30 11:48:27 +02001152 end:
1153 return ret;
1154}
1155
1156/*
1157 * Called when TCP data must be filtered on a channel. This function is the
Christopher Faulet0184ea72017-01-05 14:06:34 +01001158 * AN_REQ/RES_FLT_XFER_DATA analyzer. When called, it is responsible to forward
1159 * data when the proxy is not in http mode. Behind the scene, it calls
1160 * consecutively 'tcp_data' and 'tcp_forward_data' callbacks for all "data"
1161 * filters attached to a stream. Returns 0 if an error occurs or if it needs to
1162 * wait, any other value otherwise.
Christopher Fauletd7c91962015-04-30 11:48:27 +02001163 */
1164int
1165flt_xfer_data(struct stream *s, struct channel *chn, unsigned int an_bit)
1166{
1167 int ret = 1;
1168
Christopher Fauletda02e172015-12-04 09:25:05 +01001169 /* If there is no "data" filters, we do nothing */
Christopher Faulet166bd752019-11-08 15:31:49 +01001170 if (!HAS_DATA_FILTERS(s, chn) || (s->flags & SF_HTX))
Christopher Fauletda02e172015-12-04 09:25:05 +01001171 goto end;
Christopher Fauletd7c91962015-04-30 11:48:27 +02001172
1173 /* Be sure that the output is still opened. Else we stop the data
1174 * filtering. */
1175 if ((chn->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau44a41a82018-06-19 07:16:31 +02001176 ((chn->flags & CF_SHUTW) && (chn->to_forward || co_data(chn))))
Christopher Fauletd7c91962015-04-30 11:48:27 +02001177 goto end;
1178
1179 /* Let all "data" filters parsing incoming data */
1180 ret = flt_data(s, chn);
1181 if (ret < 0)
1182 goto end;
1183
1184 /* And forward them */
1185 ret = flt_forward_data(s, chn, ret);
1186 if (ret < 0)
1187 goto end;
1188
Christopher Fauletda02e172015-12-04 09:25:05 +01001189 /* Consume data that all filters consider as forwarded. */
Willy Tarreaubcbd3932018-06-06 07:13:22 +02001190 c_adv(chn, ret);
Christopher Fauletda02e172015-12-04 09:25:05 +01001191
Christopher Fauletd7c91962015-04-30 11:48:27 +02001192 /* Stop waiting data if the input in closed and no data is pending or if
1193 * the output is closed. */
1194 if ((chn->flags & CF_SHUTW) ||
Willy Tarreau5ba65522018-06-15 15:14:53 +02001195 ((chn->flags & CF_SHUTR) && !ci_data(chn))) {
Christopher Fauletd7c91962015-04-30 11:48:27 +02001196 ret = 1;
1197 goto end;
1198 }
1199
1200 /* Wait for data */
1201 return 0;
1202 end:
1203 /* Terminate the data filtering. If <ret> is negative, an error was
1204 * encountered during the filtering. */
1205 return handle_analyzer_result(s, chn, an_bit, ret);
1206}
1207
1208/*
1209 * Handles result of filter's analyzers. It returns 0 if an error occurs or if
1210 * it needs to wait, any other value otherwise.
1211 */
1212static int
1213handle_analyzer_result(struct stream *s, struct channel *chn,
1214 unsigned int an_bit, int ret)
1215{
1216 int finst;
Christopher Fauleteab13f02019-09-06 15:24:55 +02001217 int status = 0;
Christopher Fauletd7c91962015-04-30 11:48:27 +02001218
1219 if (ret < 0)
1220 goto return_bad_req;
1221 else if (!ret)
1222 goto wait;
1223
1224 /* End of job, return OK */
1225 if (an_bit) {
1226 chn->analysers &= ~an_bit;
1227 chn->analyse_exp = TICK_ETERNITY;
1228 }
1229 return 1;
1230
1231 return_bad_req:
1232 /* An error occurs */
1233 channel_abort(&s->req);
1234 channel_abort(&s->res);
1235
1236 if (!(chn->flags & CF_ISRESP)) {
Christopher Faulet0184ea72017-01-05 14:06:34 +01001237 s->req.analysers &= AN_REQ_FLT_END;
Christopher Fauletd7c91962015-04-30 11:48:27 +02001238 finst = SF_FINST_R;
Christopher Fauleteab13f02019-09-06 15:24:55 +02001239 status = 400;
Christopher Fauletd7c91962015-04-30 11:48:27 +02001240 /* FIXME: incr counters */
1241 }
1242 else {
Christopher Faulet0184ea72017-01-05 14:06:34 +01001243 s->res.analysers &= AN_RES_FLT_END;
Christopher Fauletd7c91962015-04-30 11:48:27 +02001244 finst = SF_FINST_H;
Christopher Fauleteab13f02019-09-06 15:24:55 +02001245 status = 502;
Christopher Fauletd7c91962015-04-30 11:48:27 +02001246 /* FIXME: incr counters */
1247 }
1248
1249 if (s->txn) {
1250 /* Do not do that when we are waiting for the next request */
Christopher Fauleteab13f02019-09-06 15:24:55 +02001251 if (s->txn->status > 0)
Christopher Fauletd7c91962015-04-30 11:48:27 +02001252 http_reply_and_close(s, s->txn->status, NULL);
1253 else {
Christopher Fauleteab13f02019-09-06 15:24:55 +02001254 s->txn->status = status;
1255 http_reply_and_close(s, status, http_error_message(s));
Christopher Fauletd7c91962015-04-30 11:48:27 +02001256 }
1257 }
1258
1259 if (!(s->flags & SF_ERR_MASK))
1260 s->flags |= SF_ERR_PRXCOND;
1261 if (!(s->flags & SF_FINST_MASK))
1262 s->flags |= finst;
1263 return 0;
1264
1265 wait:
1266 if (!(chn->flags & CF_ISRESP))
1267 channel_dont_connect(chn);
1268 return 0;
1269}
1270
1271
1272/* Note: must not be declared <const> as its list will be overwritten.
1273 * Please take care of keeping this list alphabetically sorted, doing so helps
1274 * all code contributors.
1275 * Optional keywords are also declared with a NULL ->parse() function so that
1276 * the config parser can report an appropriate error when a known keyword was
1277 * not enabled. */
1278static struct cfg_kw_list cfg_kws = {ILH, {
1279 { CFG_LISTEN, "filter", parse_filter },
1280 { 0, NULL, NULL },
1281 }
1282};
1283
Willy Tarreau0108d902018-11-25 19:14:37 +01001284INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
1285
Willy Tarreau172f5ce2018-11-26 11:21:50 +01001286REGISTER_POST_CHECK(flt_init_all);
1287REGISTER_PER_THREAD_INIT(flt_init_all_per_thread);
1288REGISTER_PER_THREAD_DEINIT(flt_deinit_all_per_thread);
1289
Christopher Fauletd7c91962015-04-30 11:48:27 +02001290/*
1291 * Local variables:
1292 * c-indent-level: 8
1293 * c-basic-offset: 8
1294 * End:
1295 */