blob: f0cf9b6c244590707ed6c75ef5eaac6d133e8ac8 [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) {
Willy Tarreau64bca592016-12-21 20:13:11 +0100295 err_code |= flt_init(px);
296 if (err_code & (ERR_ABORT|ERR_FATAL)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100297 ha_alert("Failed to initialize filters for proxy '%s'.\n",
298 px->id);
Willy Tarreau64bca592016-12-21 20:13:11 +0100299 return err_code;
300 }
301 }
302 return 0;
303}
304
Joseph Herlantb35ea682018-11-15 12:24:23 -0800305/* Calls flt_init_per_thread() for all proxies, see above. Be careful here, it
306 * returns 0 if an error occurred. This is the opposite of flt_init_all. */
Christopher Faulet71a6a8e2017-07-27 16:33:28 +0200307static int
308flt_init_all_per_thread()
309{
310 struct proxy *px;
311 int err_code = 0;
312
Olivier Houchardfbc74e82017-11-24 16:54:05 +0100313 for (px = proxies_list; px; px = px->next) {
Christopher Faulet71a6a8e2017-07-27 16:33:28 +0200314 err_code = flt_init_per_thread(px);
315 if (err_code & (ERR_ABORT|ERR_FATAL)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100316 ha_alert("Failed to initialize filters for proxy '%s' for thread %u.\n",
317 px->id, tid);
Christopher Faulet71a6a8e2017-07-27 16:33:28 +0200318 return 0;
319 }
320 }
321 return 1;
322}
323
Christopher Fauletd7c91962015-04-30 11:48:27 +0200324/*
325 * Calls 'check' callback for all filters attached to a proxy. This happens
326 * after the configuration parsing but before filters initialization. Returns
327 * the number of encountered errors.
328 */
329int
330flt_check(struct proxy *proxy)
331{
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100332 struct flt_conf *fconf;
333 int err = 0;
Christopher Fauletd7c91962015-04-30 11:48:27 +0200334
Christopher Fauletff17b182019-01-07 15:03:22 +0100335 err += check_implicit_http_comp_flt(proxy);
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100336 list_for_each_entry(fconf, &proxy->filter_configs, list) {
337 if (fconf->ops->check)
338 err += fconf->ops->check(proxy, fconf);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200339 }
340 return err;
341}
342
343/*
344 * Calls 'denit' callback for all filters attached to a proxy. This happens when
345 * HAProxy is stopped.
346 */
347void
348flt_deinit(struct proxy *proxy)
349{
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100350 struct flt_conf *fconf, *back;
Christopher Fauletd7c91962015-04-30 11:48:27 +0200351
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100352 list_for_each_entry_safe(fconf, back, &proxy->filter_configs, list) {
353 if (fconf->ops->deinit)
354 fconf->ops->deinit(proxy, fconf);
355 LIST_DEL(&fconf->list);
356 free(fconf);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200357 }
358}
359
Christopher Faulet71a6a8e2017-07-27 16:33:28 +0200360/*
361 * Calls 'denit_per_thread' callback for all filters attached to a proxy for
362 * each threads. This happens before exiting a thread.
363 */
364void
365flt_deinit_per_thread(struct proxy *proxy)
366{
367 struct flt_conf *fconf, *back;
368
369 list_for_each_entry_safe(fconf, back, &proxy->filter_configs, list) {
370 if (fconf->ops->deinit_per_thread)
371 fconf->ops->deinit_per_thread(proxy, fconf);
372 }
373}
374
375
376/* Calls flt_deinit_per_thread() for all proxies, see above */
377static void
378flt_deinit_all_per_thread()
379{
380 struct proxy *px;
381
Olivier Houchardfbc74e82017-11-24 16:54:05 +0100382 for (px = proxies_list; px; px = px->next)
Christopher Faulet71a6a8e2017-07-27 16:33:28 +0200383 flt_deinit_per_thread(px);
384}
385
Christopher Faulet92d36382015-11-05 13:35:03 +0100386/* Attaches a filter to a stream. Returns -1 if an error occurs, 0 otherwise. */
387static int
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100388flt_stream_add_filter(struct stream *s, struct flt_conf *fconf, unsigned int flags)
Christopher Faulet92d36382015-11-05 13:35:03 +0100389{
Christopher Faulet75bc9132018-11-30 15:18:09 +0100390 struct filter *f;
Christopher Faulet31ed32d2016-06-21 11:42:37 +0200391
Christopher Faulet0f17a9b2019-04-05 10:11:38 +0200392 if (IS_HTX_STRM(s) && !(fconf->flags & FLT_CFG_FL_HTX))
Christopher Faulet75bc9132018-11-30 15:18:09 +0100393 return 0;
394
395 f = pool_alloc(pool_head_filter);
Christopher Faulet92d36382015-11-05 13:35:03 +0100396 if (!f) /* not enough memory */
397 return -1;
398 memset(f, 0, sizeof(*f));
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100399 f->config = fconf;
Christopher Fauletda02e172015-12-04 09:25:05 +0100400 f->flags |= flags;
Christopher Faulet31ed32d2016-06-21 11:42:37 +0200401
402 if (FLT_OPS(f)->attach) {
403 int ret = FLT_OPS(f)->attach(s, f);
404 if (ret <= 0) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100405 pool_free(pool_head_filter, f);
Christopher Faulet31ed32d2016-06-21 11:42:37 +0200406 return ret;
407 }
408 }
409
Christopher Fauletfcf035c2015-12-03 11:48:03 +0100410 LIST_ADDQ(&strm_flt(s)->filters, &f->list);
Christopher Fauletda02e172015-12-04 09:25:05 +0100411 strm_flt(s)->flags |= STRM_FLT_FL_HAS_FILTERS;
Christopher Faulet92d36382015-11-05 13:35:03 +0100412 return 0;
413}
414
415/*
416 * Called when a stream is created. It attaches all frontend filters to the
417 * stream. Returns -1 if an error occurs, 0 otherwise.
418 */
419int
420flt_stream_init(struct stream *s)
421{
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100422 struct flt_conf *fconf;
Christopher Faulet92d36382015-11-05 13:35:03 +0100423
Christopher Fauletfcf035c2015-12-03 11:48:03 +0100424 memset(strm_flt(s), 0, sizeof(*strm_flt(s)));
425 LIST_INIT(&strm_flt(s)->filters);
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100426 list_for_each_entry(fconf, &strm_fe(s)->filter_configs, list) {
427 if (flt_stream_add_filter(s, fconf, 0) < 0)
Christopher Faulet92d36382015-11-05 13:35:03 +0100428 return -1;
429 }
430 return 0;
431}
432
433/*
434 * Called when a stream is closed or when analyze ends (For an HTTP stream, this
435 * happens after each request/response exchange). When analyze ends, backend
436 * filters are removed. When the stream is closed, all filters attached to the
437 * stream are removed.
438 */
439void
440flt_stream_release(struct stream *s, int only_backend)
441{
442 struct filter *filter, *back;
443
Christopher Fauletfcf035c2015-12-03 11:48:03 +0100444 list_for_each_entry_safe(filter, back, &strm_flt(s)->filters, list) {
Christopher Fauletda02e172015-12-04 09:25:05 +0100445 if (!only_backend || (filter->flags & FLT_FL_IS_BACKEND_FILTER)) {
Christopher Faulet31ed32d2016-06-21 11:42:37 +0200446 if (FLT_OPS(filter)->detach)
447 FLT_OPS(filter)->detach(s, filter);
Christopher Faulet92d36382015-11-05 13:35:03 +0100448 LIST_DEL(&filter->list);
Willy Tarreaubafbe012017-11-24 17:34:44 +0100449 pool_free(pool_head_filter, filter);
Christopher Faulet92d36382015-11-05 13:35:03 +0100450 }
451 }
Christopher Fauletfcf035c2015-12-03 11:48:03 +0100452 if (LIST_ISEMPTY(&strm_flt(s)->filters))
Christopher Fauletda02e172015-12-04 09:25:05 +0100453 strm_flt(s)->flags &= ~STRM_FLT_FL_HAS_FILTERS;
Christopher Faulet92d36382015-11-05 13:35:03 +0100454}
455
Christopher Fauletd7c91962015-04-30 11:48:27 +0200456/*
457 * Calls 'stream_start' for all filters attached to a stream. This happens when
458 * the stream is created, just after calling flt_stream_init
459 * function. Returns -1 if an error occurs, 0 otherwise.
460 */
461int
462flt_stream_start(struct stream *s)
463{
464 struct filter *filter;
465
Christopher Fauletfcf035c2015-12-03 11:48:03 +0100466 list_for_each_entry(filter, &strm_flt(s)->filters, list) {
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100467 if (FLT_OPS(filter)->stream_start && FLT_OPS(filter)->stream_start(s, filter) < 0)
Christopher Fauletd7c91962015-04-30 11:48:27 +0200468 return -1;
469 }
470 return 0;
471}
472
473/*
474 * Calls 'stream_stop' for all filters attached to a stream. This happens when
475 * the stream is stopped, just before calling flt_stream_release function.
476 */
477void
478flt_stream_stop(struct stream *s)
479{
480 struct filter *filter;
481
Christopher Fauletfcf035c2015-12-03 11:48:03 +0100482 list_for_each_entry(filter, &strm_flt(s)->filters, list) {
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100483 if (FLT_OPS(filter)->stream_stop)
484 FLT_OPS(filter)->stream_stop(s, filter);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200485 }
486}
487
Christopher Faulet92d36382015-11-05 13:35:03 +0100488/*
Christopher Fauleta00d8172016-11-10 14:58:05 +0100489 * Calls 'check_timeouts' for all filters attached to a stream. This happens when
490 * the stream is woken up because of expired timer.
491 */
492void
493flt_stream_check_timeouts(struct stream *s)
494{
495 struct filter *filter;
496
497 list_for_each_entry(filter, &strm_flt(s)->filters, list) {
498 if (FLT_OPS(filter)->check_timeouts)
499 FLT_OPS(filter)->check_timeouts(s, filter);
500 }
501}
502
503/*
Christopher Faulet92d36382015-11-05 13:35:03 +0100504 * Called when a backend is set for a stream. If the frontend and the backend
Christopher Faulet31ed32d2016-06-21 11:42:37 +0200505 * are not the same, this function attaches all backend filters to the
506 * stream. Returns -1 if an error occurs, 0 otherwise.
Christopher Faulet92d36382015-11-05 13:35:03 +0100507 */
508int
509flt_set_stream_backend(struct stream *s, struct proxy *be)
510{
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100511 struct flt_conf *fconf;
Christopher Faulet31ed32d2016-06-21 11:42:37 +0200512 struct filter *filter;
Christopher Faulet92d36382015-11-05 13:35:03 +0100513
514 if (strm_fe(s) == be)
Christopher Faulet31ed32d2016-06-21 11:42:37 +0200515 goto end;
Christopher Faulet92d36382015-11-05 13:35:03 +0100516
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100517 list_for_each_entry(fconf, &be->filter_configs, list) {
518 if (flt_stream_add_filter(s, fconf, FLT_FL_IS_BACKEND_FILTER) < 0)
Christopher Faulet92d36382015-11-05 13:35:03 +0100519 return -1;
520 }
Christopher Faulet31ed32d2016-06-21 11:42:37 +0200521
522 end:
523 list_for_each_entry(filter, &strm_flt(s)->filters, list) {
524 if (FLT_OPS(filter)->stream_set_backend &&
525 FLT_OPS(filter)->stream_set_backend(s, filter, be) < 0)
526 return -1;
527 }
528
Christopher Faulet92d36382015-11-05 13:35:03 +0100529 return 0;
530}
531
Christopher Fauletd7c91962015-04-30 11:48:27 +0200532/*
533 * Calls 'http_data' callback for all "data" filters attached to a stream. This
534 * function is called when incoming data are available (excluding chunks
535 * envelope for chunked messages) in the AN_REQ_HTTP_XFER_BODY and
536 * AN_RES_HTTP_XFER_BODY analyzers. It takes care to update the next offset of
537 * filters and adjusts available data to be sure that a filter cannot parse more
538 * data than its predecessors. A filter can choose to not consume all available
539 * data. Returns -1 if an error occurs, the number of consumed bytes otherwise.
Christopher Faulet75bc9132018-11-30 15:18:09 +0100540 *
541 * DEPRECATED FUNCTION - CALLED FROM LEGACY HTTP ANALYZERS
Christopher Fauletd7c91962015-04-30 11:48:27 +0200542 */
543int
544flt_http_data(struct stream *s, struct http_msg *msg)
545{
Christopher Fauletfcf035c2015-12-03 11:48:03 +0100546 struct filter *filter;
Christopher Fauletd7c91962015-04-30 11:48:27 +0200547 unsigned int buf_i;
Christopher Faulet55048a42016-06-21 10:44:32 +0200548 int delta = 0, ret = 0;
Christopher Fauletd7c91962015-04-30 11:48:27 +0200549
Christopher Fauletd7c91962015-04-30 11:48:27 +0200550 /* Save buffer state */
Willy Tarreau44a41a82018-06-19 07:16:31 +0200551 buf_i = ci_data(msg->chn);
Christopher Fauletfcf035c2015-12-03 11:48:03 +0100552
Christopher Fauletfcf035c2015-12-03 11:48:03 +0100553 list_for_each_entry(filter, &strm_flt(s)->filters, list) {
Christopher Fauletda02e172015-12-04 09:25:05 +0100554 unsigned int *nxt;
555
556 /* Call "data" filters only */
557 if (!IS_DATA_FILTER(filter, msg->chn))
558 continue;
559
Christopher Faulet2fb28802015-12-01 10:40:57 +0100560 /* If the HTTP parser is ahead, we update the next offset of the
561 * current filter. This happens for chunked messages, at the
Joseph Herlantb35ea682018-11-15 12:24:23 -0800562 * beginning of a new chunk. */
Christopher Fauletda02e172015-12-04 09:25:05 +0100563 nxt = &FLT_NXT(filter, msg->chn);
564 if (msg->next > *nxt)
565 *nxt = msg->next;
566
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100567 if (FLT_OPS(filter)->http_data) {
Willy Tarreau44a41a82018-06-19 07:16:31 +0200568 unsigned int i = ci_data(msg->chn);
Christopher Faulet55048a42016-06-21 10:44:32 +0200569
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100570 ret = FLT_OPS(filter)->http_data(s, filter, msg);
Christopher Fauletda02e172015-12-04 09:25:05 +0100571 if (ret < 0)
Christopher Fauletd7c91962015-04-30 11:48:27 +0200572 break;
Willy Tarreau44a41a82018-06-19 07:16:31 +0200573 delta += (int)(ci_data(msg->chn) - i);
Christopher Fauletfcf035c2015-12-03 11:48:03 +0100574
575 /* Update the next offset of the current filter */
Christopher Fauletda02e172015-12-04 09:25:05 +0100576 *nxt += ret;
Christopher Fauletfcf035c2015-12-03 11:48:03 +0100577
578 /* And set this value as the bound for the next
579 * filter. It will not able to parse more data than this
580 * one. */
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200581 b_set_data(&msg->chn->buf, co_data(msg->chn) + *nxt);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200582 }
583 else {
Christopher Fauletfcf035c2015-12-03 11:48:03 +0100584 /* Consume all available data and update the next offset
585 * of the current filter. buf->i is untouched here. */
Willy Tarreau44a41a82018-06-19 07:16:31 +0200586 ret = MIN(msg->chunk_len + msg->next, ci_data(msg->chn)) - *nxt;
Christopher Fauletda02e172015-12-04 09:25:05 +0100587 *nxt += ret;
Christopher Fauletd7c91962015-04-30 11:48:27 +0200588 }
Christopher Fauletd7c91962015-04-30 11:48:27 +0200589 }
Christopher Fauletfcf035c2015-12-03 11:48:03 +0100590
Christopher Fauletd7c91962015-04-30 11:48:27 +0200591 /* Restore the original buffer state */
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200592 b_set_data(&msg->chn->buf, co_data(msg->chn) + buf_i + delta);
Christopher Fauletfcf035c2015-12-03 11:48:03 +0100593
Christopher Fauletd7c91962015-04-30 11:48:27 +0200594 return ret;
595}
596
Christopher Fauletd7c91962015-04-30 11:48:27 +0200597/*
598 * Calls 'http_chunk_trailers' callback for all "data" filters attached to a
599 * stream. This function is called for chunked messages only when a part of the
600 * trailers was parsed in the AN_REQ_HTTP_XFER_BODY and AN_RES_HTTP_XFER_BODY
601 * analyzers. Filters can know how much data were parsed by the HTTP parsing
602 * until the last call with the msg->sol value. Returns a negative value if an
603 * error occurs, any other value otherwise.
Christopher Faulet75bc9132018-11-30 15:18:09 +0100604 *
605 * DEPRECATED FUNCTION - CALLED FROM LEGACY HTTP ANALYZERS
Christopher Fauletd7c91962015-04-30 11:48:27 +0200606 */
607int
608flt_http_chunk_trailers(struct stream *s, struct http_msg *msg)
609{
Christopher Faulet2fb28802015-12-01 10:40:57 +0100610 struct filter *filter;
Christopher Fauletda02e172015-12-04 09:25:05 +0100611 int ret = 1;
Christopher Fauletd7c91962015-04-30 11:48:27 +0200612
Christopher Fauletfcf035c2015-12-03 11:48:03 +0100613 list_for_each_entry(filter, &strm_flt(s)->filters, list) {
Christopher Fauletda02e172015-12-04 09:25:05 +0100614 unsigned int *nxt;
615
616 /* Call "data" filters only */
617 if (!IS_DATA_FILTER(filter, msg->chn))
618 continue;
619
Christopher Faulet2fb28802015-12-01 10:40:57 +0100620 /* Be sure to set the next offset of the filter at the right
621 * place. This is really useful when the first part of the
622 * trailers was parsed. */
Christopher Fauletda02e172015-12-04 09:25:05 +0100623 nxt = &FLT_NXT(filter, msg->chn);
624 *nxt = msg->next;
625
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100626 if (FLT_OPS(filter)->http_chunk_trailers) {
627 ret = FLT_OPS(filter)->http_chunk_trailers(s, filter, msg);
Christopher Faulet2fb28802015-12-01 10:40:57 +0100628 if (ret < 0)
629 break;
Christopher Fauletd7c91962015-04-30 11:48:27 +0200630 }
Christopher Faulet2fb28802015-12-01 10:40:57 +0100631 /* Update the next offset of the current filter. Here all data
632 * are always consumed. */
Christopher Fauletda02e172015-12-04 09:25:05 +0100633 *nxt += msg->sol;
Christopher Faulet2fb28802015-12-01 10:40:57 +0100634 }
Christopher Fauletd7c91962015-04-30 11:48:27 +0200635 return ret;
636}
637
638/*
639 * Calls 'http_end' callback for all filters attached to a stream. All filters
640 * are called here, but only if there is at least one "data" filter. This
641 * functions is called when all data were parsed and forwarded. 'http_end'
642 * callback is resumable, so this function returns a negative value if an error
643 * occurs, 0 if it needs to wait for some reason, any other value otherwise.
Christopher Faulet75bc9132018-11-30 15:18:09 +0100644 *
645 * Be carefull, this function can be called from the HTTP legacy analyzers or
646 * from HTX analyzers. If your filter is compatible with the two modes, use
647 * IS_HTX_STRM macro on the stream.
Christopher Fauletd7c91962015-04-30 11:48:27 +0200648 */
649int
650flt_http_end(struct stream *s, struct http_msg *msg)
651{
652 int ret = 1;
653
Christopher Fauletd7c91962015-04-30 11:48:27 +0200654 RESUME_FILTER_LOOP(s, msg->chn) {
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100655 if (FLT_OPS(filter)->http_end) {
656 ret = FLT_OPS(filter)->http_end(s, filter, msg);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200657 if (ret <= 0)
658 BREAK_EXECUTION(s, msg->chn, end);
659 }
Christopher Fauletd7c91962015-04-30 11:48:27 +0200660 } RESUME_FILTER_END;
661end:
662 return ret;
663}
664
665/*
666 * Calls 'http_reset' callback for all filters attached to a stream. This
667 * happens when a 100-continue response is received.
Christopher Faulet75bc9132018-11-30 15:18:09 +0100668 *
669 * Be carefull, this function can be called from the HTTP legacy analyzers or
670 * from HTX analyzers. If your filter is compatible with the two modes, use
671 * IS_HTX_STRM macro on the stream.
Christopher Fauletd7c91962015-04-30 11:48:27 +0200672 */
673void
674flt_http_reset(struct stream *s, struct http_msg *msg)
675{
676 struct filter *filter;
677
Christopher Fauletfcf035c2015-12-03 11:48:03 +0100678 list_for_each_entry(filter, &strm_flt(s)->filters, list) {
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100679 if (FLT_OPS(filter)->http_reset)
680 FLT_OPS(filter)->http_reset(s, filter, msg);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200681 }
682}
683
684/*
685 * Calls 'http_reply' callback for all filters attached to a stream when HA
686 * decides to stop the HTTP message processing.
Christopher Faulet75bc9132018-11-30 15:18:09 +0100687 *
688 * Be carefull, this function can be called from the HTTP legacy analyzers or
689 * from HTX analyzers. If your filter is compatible with the two modes, use
690 * IS_HTX_STRM macro on the stream.
Christopher Fauletd7c91962015-04-30 11:48:27 +0200691 */
692void
Willy Tarreau83061a82018-07-13 11:56:34 +0200693flt_http_reply(struct stream *s, short status, const struct buffer *msg)
Christopher Fauletd7c91962015-04-30 11:48:27 +0200694{
695 struct filter *filter;
696
Christopher Fauletfcf035c2015-12-03 11:48:03 +0100697 list_for_each_entry(filter, &strm_flt(s)->filters, list) {
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100698 if (FLT_OPS(filter)->http_reply)
699 FLT_OPS(filter)->http_reply(s, filter, status, msg);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200700 }
701}
702
703/*
Christopher Faulet75bc9132018-11-30 15:18:09 +0100704 * Calls 'http_forward_data' callback for all "data" filters attached to a HTTP
705 * legacy stream. This function is called when some data can be forwarded in the
Christopher Fauletd7c91962015-04-30 11:48:27 +0200706 * AN_REQ_HTTP_XFER_BODY and AN_RES_HTTP_XFER_BODY analyzers. It takes care to
707 * update the forward offset of filters and adjusts "forwardable" data to be
708 * sure that a filter cannot forward more data than its predecessors. A filter
709 * can choose to not forward all parsed data. Returns a negative value if an
710 * error occurs, else the number of forwarded bytes.
Christopher Faulet75bc9132018-11-30 15:18:09 +0100711 *
712 * DEPRECATED FUNCTION - CALLED FROM LEGACY HTTP ANALYZERS
Christopher Fauletd7c91962015-04-30 11:48:27 +0200713 */
714int
715flt_http_forward_data(struct stream *s, struct http_msg *msg, unsigned int len)
716{
Christopher Fauletfcf035c2015-12-03 11:48:03 +0100717 struct filter *filter;
Christopher Fauletd7c91962015-04-30 11:48:27 +0200718 int ret = len;
719
Christopher Fauletfcf035c2015-12-03 11:48:03 +0100720 list_for_each_entry(filter, &strm_flt(s)->filters, list) {
Christopher Fauletda02e172015-12-04 09:25:05 +0100721 unsigned int *nxt, *fwd;
722
723 /* Call "data" filters only */
724 if (!IS_DATA_FILTER(filter, msg->chn))
725 continue;
726
Christopher Faulet2fb28802015-12-01 10:40:57 +0100727 /* If the HTTP parser is ahead, we update the next offset of the
728 * current filter. This happens for chunked messages, when the
729 * chunk envelope is parsed. */
Christopher Fauletda02e172015-12-04 09:25:05 +0100730 nxt = &FLT_NXT(filter, msg->chn);
731 fwd = &FLT_FWD(filter, msg->chn);
732 if (msg->next > *nxt)
733 *nxt = msg->next;
734
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100735 if (FLT_OPS(filter)->http_forward_data) {
Christopher Fauletda02e172015-12-04 09:25:05 +0100736 /* Remove bytes that the current filter considered as
737 * forwarded */
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100738 ret = FLT_OPS(filter)->http_forward_data(s, filter, msg, ret - *fwd);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200739 if (ret < 0)
740 goto end;
741 }
742
743 /* Adjust bytes that the current filter considers as
744 * forwarded */
Christopher Fauletda02e172015-12-04 09:25:05 +0100745 *fwd += ret;
Christopher Fauletd7c91962015-04-30 11:48:27 +0200746
747 /* And set this value as the bound for the next filter. It will
748 * not able to forward more data than the current one. */
Christopher Fauletda02e172015-12-04 09:25:05 +0100749 ret = *fwd;
Christopher Fauletd7c91962015-04-30 11:48:27 +0200750 }
751
752 if (!ret)
753 goto end;
754
755 /* Finally, adjust filters offsets by removing data that HAProxy will
756 * forward. */
Christopher Fauletfcf035c2015-12-03 11:48:03 +0100757 list_for_each_entry(filter, &strm_flt(s)->filters, list) {
Christopher Fauletda02e172015-12-04 09:25:05 +0100758 if (!IS_DATA_FILTER(filter, msg->chn))
759 continue;
Christopher Fauletd7c91962015-04-30 11:48:27 +0200760 FLT_NXT(filter, msg->chn) -= ret;
761 FLT_FWD(filter, msg->chn) -= ret;
762 }
763 end:
764 return ret;
765}
766
767/*
Christopher Faulet75bc9132018-11-30 15:18:09 +0100768 * Calls 'http_payload' callback for all "data" filters attached to a
769 * stream. This function is called when some data can be forwarded in the
770 * AN_REQ_HTTP_XFER_BODY and AN_RES_HTTP_XFER_BODY analyzers. It takes care to
771 * update the filters and the stream offset to be sure that a filter cannot
772 * forward more data than its predecessors. A filter can choose to not forward
773 * all data. Returns a negative value if an error occurs, else the number of
774 * forwarded bytes.
775 *
776 * Be carefull, this callback is only called from HTX analyzers. So the
777 * channel's buffer must be considered as an HTX structured. Of course, your
778 * filter must support HTX streams.
779 */
780int
781flt_http_payload(struct stream *s, struct http_msg *msg, unsigned int len)
782{
783 struct filter *filter;
784 unsigned long long *strm_off = &FLT_STRM_OFF(s, msg->chn);
Christopher Fauletee847d42019-05-23 11:55:33 +0200785 int ret = len - co_data(msg->chn);
Christopher Faulet75bc9132018-11-30 15:18:09 +0100786
787 list_for_each_entry(filter, &strm_flt(s)->filters, list) {
788 /* Call "data" filters only */
789 if (!IS_DATA_FILTER(filter, msg->chn))
790 continue;
791 if (FLT_OPS(filter)->http_payload) {
792 unsigned long long *flt_off = &FLT_OFF(filter, msg->chn);
793 unsigned int offset = *flt_off - *strm_off;
794
Christopher Fauletee847d42019-05-23 11:55:33 +0200795 ret = FLT_OPS(filter)->http_payload(s, filter, msg, offset, ret - offset);
Christopher Faulet75bc9132018-11-30 15:18:09 +0100796 if (ret < 0)
797 goto end;
798 *flt_off += ret;
799 ret += offset;
800 }
801 }
802 *strm_off += ret;
Christopher Faulet75bc9132018-11-30 15:18:09 +0100803 end:
804 return ret;
805}
806
807/*
Christopher Fauletd7c91962015-04-30 11:48:27 +0200808 * Calls 'channel_start_analyze' callback for all filters attached to a
809 * stream. This function is called when we start to analyze a request or a
810 * response. For frontend filters, it is called before all other analyzers. For
811 * backend ones, it is called before all backend
812 * analyzers. 'channel_start_analyze' callback is resumable, so this function
813 * returns 0 if an error occurs or if it needs to wait, any other value
814 * otherwise.
815 */
816int
817flt_start_analyze(struct stream *s, struct channel *chn, unsigned int an_bit)
818{
819 int ret = 1;
820
821 /* If this function is called, this means there is at least one filter,
822 * so we do not need to check the filter list's emptiness. */
823
Christopher Faulete6006242017-03-10 11:52:44 +0100824 /* Set flag on channel to tell that the channel is filtered */
825 chn->flags |= CF_FLT_ANALYZE;
826
Christopher Fauletd7c91962015-04-30 11:48:27 +0200827 RESUME_FILTER_LOOP(s, chn) {
Christopher Faulet0184ea72017-01-05 14:06:34 +0100828 if (!(chn->flags & CF_ISRESP)) {
829 if (an_bit == AN_REQ_FLT_START_BE &&
830 !(filter->flags & FLT_FL_IS_BACKEND_FILTER))
831 continue;
832 }
833 else {
834 if (an_bit == AN_RES_FLT_START_BE &&
835 !(filter->flags & FLT_FL_IS_BACKEND_FILTER))
836 continue;
837 }
Christopher Fauletd7c91962015-04-30 11:48:27 +0200838
Christopher Fauletfcf035c2015-12-03 11:48:03 +0100839 FLT_NXT(filter, chn) = 0;
840 FLT_FWD(filter, chn) = 0;
Christopher Fauletd7c91962015-04-30 11:48:27 +0200841
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100842 if (FLT_OPS(filter)->channel_start_analyze) {
843 ret = FLT_OPS(filter)->channel_start_analyze(s, filter, chn);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200844 if (ret <= 0)
845 BREAK_EXECUTION(s, chn, end);
846 }
847 } RESUME_FILTER_END;
848
849 end:
850 return handle_analyzer_result(s, chn, an_bit, ret);
851}
852
853/*
Christopher Faulet3a394fa2016-05-11 17:13:39 +0200854 * Calls 'channel_pre_analyze' callback for all filters attached to a
855 * stream. This function is called BEFORE each analyzer attached to a channel,
856 * expects analyzers responsible for data sending. 'channel_pre_analyze'
857 * callback is resumable, so this function returns 0 if an error occurs or if it
858 * needs to wait, any other value otherwise.
859 *
860 * Note this function can be called many times for the same analyzer. In fact,
861 * it is called until the analyzer finishes its processing.
Christopher Fauletd7c91962015-04-30 11:48:27 +0200862 */
863int
Christopher Faulet3a394fa2016-05-11 17:13:39 +0200864flt_pre_analyze(struct stream *s, struct channel *chn, unsigned int an_bit)
Christopher Fauletd7c91962015-04-30 11:48:27 +0200865{
866 int ret = 1;
867
Christopher Fauletd7c91962015-04-30 11:48:27 +0200868 RESUME_FILTER_LOOP(s, chn) {
Christopher Faulet3a394fa2016-05-11 17:13:39 +0200869 if (FLT_OPS(filter)->channel_pre_analyze && (filter->pre_analyzers & an_bit)) {
870 ret = FLT_OPS(filter)->channel_pre_analyze(s, filter, chn, an_bit);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200871 if (ret <= 0)
872 BREAK_EXECUTION(s, chn, check_result);
873 }
874 } RESUME_FILTER_END;
875
876 check_result:
Christopher Faulet309c6412015-12-02 09:57:32 +0100877 return handle_analyzer_result(s, chn, 0, ret);
878}
879
880/*
Christopher Faulet3a394fa2016-05-11 17:13:39 +0200881 * Calls 'channel_post_analyze' callback for all filters attached to a
882 * stream. This function is called AFTER each analyzer attached to a channel,
883 * expects analyzers responsible for data sending. 'channel_post_analyze'
884 * callback is NOT resumable, so this function returns a 0 if an error occurs,
885 * any other value otherwise.
886 *
887 * Here, AFTER means when the analyzer finishes its processing.
888 */
889int
890flt_post_analyze(struct stream *s, struct channel *chn, unsigned int an_bit)
891{
892 struct filter *filter;
893 int ret = 1;
894
895 list_for_each_entry(filter, &strm_flt(s)->filters, list) {
896 if (FLT_OPS(filter)->channel_post_analyze && (filter->post_analyzers & an_bit)) {
897 ret = FLT_OPS(filter)->channel_post_analyze(s, filter, chn, an_bit);
898 if (ret < 0)
899 break;
900 }
901 }
902 return handle_analyzer_result(s, chn, 0, ret);
903}
904
905/*
Christopher Faulet0184ea72017-01-05 14:06:34 +0100906 * This function is the AN_REQ/RES_FLT_HTTP_HDRS analyzer, used to filter HTTP
907 * headers or a request or a response. Returns 0 if an error occurs or if it
908 * needs to wait, any other value otherwise.
Christopher Faulet75bc9132018-11-30 15:18:09 +0100909 *
910 * Be carefull, this function can be called from the HTTP legacy analyzers or
911 * from HTX analyzers. If your filter is compatible with the two modes, use
912 * IS_HTX_STRM macro on the stream.
Christopher Faulet309c6412015-12-02 09:57:32 +0100913 */
914int
915flt_analyze_http_headers(struct stream *s, struct channel *chn, unsigned int an_bit)
916{
Christopher Faulet1339d742016-05-11 16:48:33 +0200917 struct filter *filter;
918 struct http_msg *msg;
919 int ret = 1;
Christopher Faulet309c6412015-12-02 09:57:32 +0100920
Christopher Faulet1339d742016-05-11 16:48:33 +0200921 msg = ((chn->flags & CF_ISRESP) ? &s->txn->rsp : &s->txn->req);
Christopher Faulet309c6412015-12-02 09:57:32 +0100922 RESUME_FILTER_LOOP(s, chn) {
Christopher Faulet1339d742016-05-11 16:48:33 +0200923 if (FLT_OPS(filter)->http_headers) {
924 ret = FLT_OPS(filter)->http_headers(s, filter, msg);
Christopher Faulet309c6412015-12-02 09:57:32 +0100925 if (ret <= 0)
926 BREAK_EXECUTION(s, chn, check_result);
927 }
928 } RESUME_FILTER_END;
929
Christopher Faulet17fd8a22019-05-23 10:31:07 +0200930 if (IS_HTX_STRM(s))
931 channel_htx_fwd_headers(chn, htxbuf(&chn->buf));
Christopher Faulet75bc9132018-11-30 15:18:09 +0100932 else {
933 /* We increase next offset of all "data" filters after all processing on
934 * headers because any filter can alter them. So the definitive size of
935 * headers (msg->sov) is only known when all filters have been
936 * called. */
937 list_for_each_entry(filter, &strm_flt(s)->filters, list) {
938 /* Handle "data" filters only */
939 if (!IS_DATA_FILTER(filter, chn))
940 continue;
941 FLT_NXT(filter, chn) = msg->sov;
942 }
Christopher Faulet309c6412015-12-02 09:57:32 +0100943 }
944
945 check_result:
946 return handle_analyzer_result(s, chn, an_bit, ret);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200947}
948
949/*
950 * Calls 'channel_end_analyze' callback for all filters attached to a
951 * stream. This function is called when we stop to analyze a request or a
952 * response. It is called after all other analyzers. 'channel_end_analyze'
953 * callback is resumable, so this function returns 0 if an error occurs or if it
954 * needs to wait, any other value otherwise.
955 */
956int
957flt_end_analyze(struct stream *s, struct channel *chn, unsigned int an_bit)
958{
959 int ret = 1;
960
Christopher Faulete6006242017-03-10 11:52:44 +0100961 /* Check if all filters attached on the stream have finished their
962 * processing on this channel. */
963 if (!(chn->flags & CF_FLT_ANALYZE))
964 goto sync;
965
Christopher Fauletd7c91962015-04-30 11:48:27 +0200966 RESUME_FILTER_LOOP(s, chn) {
Christopher Fauletfcf035c2015-12-03 11:48:03 +0100967 FLT_NXT(filter, chn) = 0;
968 FLT_FWD(filter, chn) = 0;
Christopher Fauletda02e172015-12-04 09:25:05 +0100969 unregister_data_filter(s, chn, filter);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200970
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100971 if (FLT_OPS(filter)->channel_end_analyze) {
972 ret = FLT_OPS(filter)->channel_end_analyze(s, filter, chn);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200973 if (ret <= 0)
974 BREAK_EXECUTION(s, chn, end);
975 }
976 } RESUME_FILTER_END;
977
Christopher Faulete6006242017-03-10 11:52:44 +0100978 end:
979 /* We don't remove yet this analyzer because we need to synchronize the
980 * both channels. So here, we just remove the flag CF_FLT_ANALYZE. */
981 ret = handle_analyzer_result(s, chn, 0, ret);
Christopher Faulet570f7992017-07-06 15:53:02 +0200982 if (ret) {
Christopher Faulete6006242017-03-10 11:52:44 +0100983 chn->flags &= ~CF_FLT_ANALYZE;
Christopher Faulet02c7b222015-12-22 12:01:29 +0100984
Christopher Faulet570f7992017-07-06 15:53:02 +0200985 /* Pretend there is an activity on both channels. Flag on the
986 * current one will be automatically removed, so only the other
987 * one will remain. This is a way to be sure that
988 * 'channel_end_analyze' callback will have a chance to be
989 * called at least once for the other side to finish the current
Joseph Herlantb35ea682018-11-15 12:24:23 -0800990 * processing. Of course, this is the filter responsibility to
Christopher Faulet570f7992017-07-06 15:53:02 +0200991 * wakeup the stream if it choose to loop on this callback. */
992 s->req.flags |= CF_WAKE_ONCE;
993 s->res.flags |= CF_WAKE_ONCE;
994 }
995
996
Christopher Faulete6006242017-03-10 11:52:44 +0100997 sync:
998 /* Now we can check if filters have finished their work on the both
999 * channels */
1000 if (!(s->req.flags & CF_FLT_ANALYZE) && !(s->res.flags & CF_FLT_ANALYZE)) {
1001 /* Sync channels by removing this analyzer for the both channels */
1002 s->req.analysers &= ~AN_REQ_FLT_END;
1003 s->res.analysers &= ~AN_RES_FLT_END;
Christopher Fauletc6062be2016-10-31 11:22:37 +01001004
Christopher Faulete6006242017-03-10 11:52:44 +01001005 /* Clean up the HTTP transaction if needed */
1006 if (s->txn && (s->txn->flags & TX_WAIT_CLEANUP))
1007 http_end_txn_clean_session(s);
Christopher Fauletd7c91962015-04-30 11:48:27 +02001008
Christopher Faulete6006242017-03-10 11:52:44 +01001009 /* Remove backend filters from the list */
1010 flt_stream_release(s, 1);
Christopher Fauletd7c91962015-04-30 11:48:27 +02001011 }
Christopher Faulet2b553de2017-03-30 11:13:22 +02001012
Christopher Fauletd7c91962015-04-30 11:48:27 +02001013 return ret;
1014}
1015
1016
1017/*
1018 * Calls 'tcp_data' callback for all "data" filters attached to a stream. This
1019 * function is called when incoming data are available. It takes care to update
1020 * the next offset of filters and adjusts available data to be sure that a
1021 * filter cannot parse more data than its predecessors. A filter can choose to
1022 * not consume all available data. Returns -1 if an error occurs, the number of
1023 * consumed bytes otherwise.
1024 */
1025static int
1026flt_data(struct stream *s, struct channel *chn)
1027{
Christopher Fauletda02e172015-12-04 09:25:05 +01001028 struct filter *filter;
Christopher Fauletd7c91962015-04-30 11:48:27 +02001029 unsigned int buf_i;
Christopher Faulet55048a42016-06-21 10:44:32 +02001030 int delta = 0, ret = 0;
Christopher Fauletd7c91962015-04-30 11:48:27 +02001031
1032 /* Save buffer state */
Willy Tarreau44a41a82018-06-19 07:16:31 +02001033 buf_i = ci_data(chn);
Christopher Fauletfcf035c2015-12-03 11:48:03 +01001034
1035 list_for_each_entry(filter, &strm_flt(s)->filters, list) {
Christopher Fauletda02e172015-12-04 09:25:05 +01001036 unsigned int *nxt;
1037
1038 /* Call "data" filters only */
1039 if (!IS_DATA_FILTER(filter, chn))
1040 continue;
1041
1042 nxt = &FLT_NXT(filter, chn);
Christopher Faulet443ea1a2016-02-04 13:40:26 +01001043 if (FLT_OPS(filter)->tcp_data) {
Willy Tarreau44a41a82018-06-19 07:16:31 +02001044 unsigned int i = ci_data(chn);
Christopher Faulet55048a42016-06-21 10:44:32 +02001045
Christopher Faulet443ea1a2016-02-04 13:40:26 +01001046 ret = FLT_OPS(filter)->tcp_data(s, filter, chn);
Christopher Fauletd7c91962015-04-30 11:48:27 +02001047 if (ret < 0)
1048 break;
Willy Tarreau44a41a82018-06-19 07:16:31 +02001049 delta += (int)(ci_data(chn) - i);
Christopher Fauletd7c91962015-04-30 11:48:27 +02001050
Christopher Fauletfcf035c2015-12-03 11:48:03 +01001051 /* Increase next offset of the current filter */
Christopher Fauletda02e172015-12-04 09:25:05 +01001052 *nxt += ret;
Christopher Fauletfcf035c2015-12-03 11:48:03 +01001053
1054 /* And set this value as the bound for the next
1055 * filter. It will not able to parse more data than the
1056 * current one. */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001057 b_set_data(&chn->buf, co_data(chn) + *nxt);
Christopher Fauletfcf035c2015-12-03 11:48:03 +01001058 }
1059 else {
1060 /* Consume all available data */
Willy Tarreau44a41a82018-06-19 07:16:31 +02001061 *nxt = ci_data(chn);
Christopher Fauletfcf035c2015-12-03 11:48:03 +01001062 }
Christopher Fauletd7c91962015-04-30 11:48:27 +02001063
1064 /* Update <ret> value to be sure to have the last one when we
Christopher Fauletda02e172015-12-04 09:25:05 +01001065 * exit from the loop. This value will be used to know how much
1066 * data are "forwardable" */
1067 ret = *nxt;
Christopher Fauletd7c91962015-04-30 11:48:27 +02001068 }
Christopher Fauletfcf035c2015-12-03 11:48:03 +01001069
1070 /* Restore the original buffer state */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001071 b_set_data(&chn->buf, co_data(chn) + buf_i + delta);
Christopher Fauletfcf035c2015-12-03 11:48:03 +01001072
Christopher Fauletd7c91962015-04-30 11:48:27 +02001073 return ret;
1074}
1075
1076/*
1077 * Calls 'tcp_forward_data' callback for all "data" filters attached to a
1078 * stream. This function is called when some data can be forwarded. It takes
1079 * care to update the forward offset of filters and adjusts "forwardable" data
1080 * to be sure that a filter cannot forward more data than its predecessors. A
1081 * filter can choose to not forward all parsed data. Returns a negative value if
1082 * an error occurs, else the number of forwarded bytes.
1083 */
1084static int
1085flt_forward_data(struct stream *s, struct channel *chn, unsigned int len)
1086{
Christopher Fauletda02e172015-12-04 09:25:05 +01001087 struct filter *filter;
Christopher Fauletd7c91962015-04-30 11:48:27 +02001088 int ret = len;
1089
Christopher Fauletfcf035c2015-12-03 11:48:03 +01001090 list_for_each_entry(filter, &strm_flt(s)->filters, list) {
Christopher Fauletda02e172015-12-04 09:25:05 +01001091 unsigned int *fwd;
1092
1093 /* Call "data" filters only */
1094 if (!IS_DATA_FILTER(filter, chn))
1095 continue;
1096
1097 fwd = &FLT_FWD(filter, chn);
Christopher Faulet443ea1a2016-02-04 13:40:26 +01001098 if (FLT_OPS(filter)->tcp_forward_data) {
Christopher Fauletd7c91962015-04-30 11:48:27 +02001099 /* Remove bytes that the current filter considered as
1100 * forwarded */
Christopher Faulet443ea1a2016-02-04 13:40:26 +01001101 ret = FLT_OPS(filter)->tcp_forward_data(s, filter, chn, ret - *fwd);
Christopher Fauletd7c91962015-04-30 11:48:27 +02001102 if (ret < 0)
1103 goto end;
1104 }
1105
Christopher Fauletda02e172015-12-04 09:25:05 +01001106 /* Adjust bytes that the current filter considers as
Christopher Fauletd7c91962015-04-30 11:48:27 +02001107 * forwarded */
Christopher Fauletda02e172015-12-04 09:25:05 +01001108 *fwd += ret;
Christopher Fauletd7c91962015-04-30 11:48:27 +02001109
1110 /* And set this value as the bound for the next filter. It will
1111 * not able to forward more data than the current one. */
Christopher Fauletda02e172015-12-04 09:25:05 +01001112 ret = *fwd;
Christopher Fauletd7c91962015-04-30 11:48:27 +02001113 }
1114
1115 if (!ret)
1116 goto end;
1117
Christopher Fauletda02e172015-12-04 09:25:05 +01001118 /* Finally, adjust filters offsets by removing data that HAProxy will
1119 * forward. */
Christopher Fauletfcf035c2015-12-03 11:48:03 +01001120 list_for_each_entry(filter, &strm_flt(s)->filters, list) {
Christopher Fauletda02e172015-12-04 09:25:05 +01001121 if (!IS_DATA_FILTER(filter, chn))
1122 continue;
Christopher Fauletd7c91962015-04-30 11:48:27 +02001123 FLT_NXT(filter, chn) -= ret;
1124 FLT_FWD(filter, chn) -= ret;
1125 }
1126
Christopher Fauletd7c91962015-04-30 11:48:27 +02001127 end:
1128 return ret;
1129}
1130
1131/*
1132 * Called when TCP data must be filtered on a channel. This function is the
Christopher Faulet0184ea72017-01-05 14:06:34 +01001133 * AN_REQ/RES_FLT_XFER_DATA analyzer. When called, it is responsible to forward
1134 * data when the proxy is not in http mode. Behind the scene, it calls
1135 * consecutively 'tcp_data' and 'tcp_forward_data' callbacks for all "data"
1136 * filters attached to a stream. Returns 0 if an error occurs or if it needs to
1137 * wait, any other value otherwise.
Christopher Fauletd7c91962015-04-30 11:48:27 +02001138 */
1139int
1140flt_xfer_data(struct stream *s, struct channel *chn, unsigned int an_bit)
1141{
1142 int ret = 1;
1143
Christopher Fauletda02e172015-12-04 09:25:05 +01001144 /* If there is no "data" filters, we do nothing */
1145 if (!HAS_DATA_FILTERS(s, chn))
1146 goto end;
Christopher Fauletd7c91962015-04-30 11:48:27 +02001147
1148 /* Be sure that the output is still opened. Else we stop the data
1149 * filtering. */
1150 if ((chn->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau44a41a82018-06-19 07:16:31 +02001151 ((chn->flags & CF_SHUTW) && (chn->to_forward || co_data(chn))))
Christopher Fauletd7c91962015-04-30 11:48:27 +02001152 goto end;
1153
1154 /* Let all "data" filters parsing incoming data */
1155 ret = flt_data(s, chn);
1156 if (ret < 0)
1157 goto end;
1158
1159 /* And forward them */
1160 ret = flt_forward_data(s, chn, ret);
1161 if (ret < 0)
1162 goto end;
1163
Christopher Fauletda02e172015-12-04 09:25:05 +01001164 /* Consume data that all filters consider as forwarded. */
Willy Tarreaubcbd3932018-06-06 07:13:22 +02001165 c_adv(chn, ret);
Christopher Fauletda02e172015-12-04 09:25:05 +01001166
Christopher Fauletd7c91962015-04-30 11:48:27 +02001167 /* Stop waiting data if the input in closed and no data is pending or if
1168 * the output is closed. */
1169 if ((chn->flags & CF_SHUTW) ||
Willy Tarreau5ba65522018-06-15 15:14:53 +02001170 ((chn->flags & CF_SHUTR) && !ci_data(chn))) {
Christopher Fauletd7c91962015-04-30 11:48:27 +02001171 ret = 1;
1172 goto end;
1173 }
1174
1175 /* Wait for data */
1176 return 0;
1177 end:
1178 /* Terminate the data filtering. If <ret> is negative, an error was
1179 * encountered during the filtering. */
1180 return handle_analyzer_result(s, chn, an_bit, ret);
1181}
1182
1183/*
1184 * Handles result of filter's analyzers. It returns 0 if an error occurs or if
1185 * it needs to wait, any other value otherwise.
1186 */
1187static int
1188handle_analyzer_result(struct stream *s, struct channel *chn,
1189 unsigned int an_bit, int ret)
1190{
1191 int finst;
1192
1193 if (ret < 0)
1194 goto return_bad_req;
1195 else if (!ret)
1196 goto wait;
1197
1198 /* End of job, return OK */
1199 if (an_bit) {
1200 chn->analysers &= ~an_bit;
1201 chn->analyse_exp = TICK_ETERNITY;
1202 }
1203 return 1;
1204
1205 return_bad_req:
1206 /* An error occurs */
1207 channel_abort(&s->req);
1208 channel_abort(&s->res);
1209
1210 if (!(chn->flags & CF_ISRESP)) {
Christopher Faulet0184ea72017-01-05 14:06:34 +01001211 s->req.analysers &= AN_REQ_FLT_END;
Christopher Fauletd7c91962015-04-30 11:48:27 +02001212 finst = SF_FINST_R;
1213 /* FIXME: incr counters */
1214 }
1215 else {
Christopher Faulet0184ea72017-01-05 14:06:34 +01001216 s->res.analysers &= AN_RES_FLT_END;
Christopher Fauletd7c91962015-04-30 11:48:27 +02001217 finst = SF_FINST_H;
1218 /* FIXME: incr counters */
1219 }
1220
1221 if (s->txn) {
1222 /* Do not do that when we are waiting for the next request */
1223 if (s->txn->status)
1224 http_reply_and_close(s, s->txn->status, NULL);
1225 else {
1226 s->txn->status = 400;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001227 http_reply_and_close(s, 400, http_error_message(s));
Christopher Fauletd7c91962015-04-30 11:48:27 +02001228 }
1229 }
1230
1231 if (!(s->flags & SF_ERR_MASK))
1232 s->flags |= SF_ERR_PRXCOND;
1233 if (!(s->flags & SF_FINST_MASK))
1234 s->flags |= finst;
1235 return 0;
1236
1237 wait:
1238 if (!(chn->flags & CF_ISRESP))
1239 channel_dont_connect(chn);
1240 return 0;
1241}
1242
1243
1244/* Note: must not be declared <const> as its list will be overwritten.
1245 * Please take care of keeping this list alphabetically sorted, doing so helps
1246 * all code contributors.
1247 * Optional keywords are also declared with a NULL ->parse() function so that
1248 * the config parser can report an appropriate error when a known keyword was
1249 * not enabled. */
1250static struct cfg_kw_list cfg_kws = {ILH, {
1251 { CFG_LISTEN, "filter", parse_filter },
1252 { 0, NULL, NULL },
1253 }
1254};
1255
Willy Tarreau0108d902018-11-25 19:14:37 +01001256INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
1257
Willy Tarreau172f5ce2018-11-26 11:21:50 +01001258REGISTER_POST_CHECK(flt_init_all);
1259REGISTER_PER_THREAD_INIT(flt_init_all_per_thread);
1260REGISTER_PER_THREAD_DEINIT(flt_deinit_all_per_thread);
1261
Christopher Fauletd7c91962015-04-30 11:48:27 +02001262/*
1263 * Local variables:
1264 * c-indent-level: 8
1265 * c-basic-offset: 8
1266 * End:
1267 */