blob: 8c3485e10c7c5ce3780ed713f859d9dc160d9e8f [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>
19#include <common/namespace.h>
20#include <common/standard.h>
21
22#include <types/filters.h>
23#include <types/proto_http.h>
24
25#include <proto/compression.h>
26#include <proto/filters.h>
Christopher Faulet92d36382015-11-05 13:35:03 +010027#include <proto/flt_http_comp.h>
Christopher Fauletd7c91962015-04-30 11:48:27 +020028#include <proto/proto_http.h>
29#include <proto/stream.h>
30#include <proto/stream_interface.h>
31
32/* Pool used to allocate filters */
33struct pool_head *pool2_filter = NULL;
34
35static int handle_analyzer_result(struct stream *s, struct channel *chn, unsigned int an_bit, int ret);
36
37/* - RESUME_FILTER_LOOP and RESUME_FILTER_END must always be used together.
38 * The first one begins a loop and the seconds one ends it.
39 *
40 * - BREAK_EXECUTION must be used to break the loop and set the filter from
41 * which to resume the next time.
42 *
43 * Here is an exemple:
44 *
45 * RESUME_FILTER_LOOP(stream, channel) {
46 * ...
47 * if (cond)
48 * BREAK_EXECUTION(stream, channel, label);
49 * ...
50 * } RESUME_FILTER_END;
51 * ...
52 * label:
53 * ...
54 *
55 */
56#define RESUME_FILTER_LOOP(strm, chn) \
57 do { \
58 struct filter *filter; \
59 \
Christopher Fauletda02e172015-12-04 09:25:05 +010060 if (strm_flt(strm)->current[CHN_IDX(chn)]) { \
61 filter = strm_flt(strm)->current[CHN_IDX(chn)]; \
62 strm_flt(strm)->current[CHN_IDX(chn)] = NULL; \
Christopher Fauletd7c91962015-04-30 11:48:27 +020063 goto resume_execution; \
64 } \
65 \
Christopher Fauletfcf035c2015-12-03 11:48:03 +010066 list_for_each_entry(filter, &strm_flt(s)->filters, list) { \
Christopher Fauletda02e172015-12-04 09:25:05 +010067 resume_execution:
Christopher Fauletd7c91962015-04-30 11:48:27 +020068
69#define RESUME_FILTER_END \
70 } \
71 } while(0)
72
Christopher Fauletda02e172015-12-04 09:25:05 +010073#define BREAK_EXECUTION(strm, chn, label) \
74 do { \
75 strm_flt(strm)->current[CHN_IDX(chn)] = filter; \
76 goto label; \
Christopher Fauletd7c91962015-04-30 11:48:27 +020077 } while (0)
78
79
80/* List head of all known filter keywords */
81static struct flt_kw_list flt_keywords = {
82 .list = LIST_HEAD_INIT(flt_keywords.list)
83};
84
85/*
86 * Registers the filter keyword list <kwl> as a list of valid keywords for next
87 * parsing sessions.
88 */
89void
90flt_register_keywords(struct flt_kw_list *kwl)
91{
92 LIST_ADDQ(&flt_keywords.list, &kwl->list);
93}
94
95/*
96 * Returns a pointer to the filter keyword <kw>, or NULL if not found. If the
97 * keyword is found with a NULL ->parse() function, then an attempt is made to
98 * find one with a valid ->parse() function. This way it is possible to declare
99 * platform-dependant, known keywords as NULL, then only declare them as valid
100 * if some options are met. Note that if the requested keyword contains an
101 * opening parenthesis, everything from this point is ignored.
102 */
103struct flt_kw *
104flt_find_kw(const char *kw)
105{
106 int index;
107 const char *kwend;
108 struct flt_kw_list *kwl;
109 struct flt_kw *ret = NULL;
110
111 kwend = strchr(kw, '(');
112 if (!kwend)
113 kwend = kw + strlen(kw);
114
115 list_for_each_entry(kwl, &flt_keywords.list, list) {
116 for (index = 0; kwl->kw[index].kw != NULL; index++) {
117 if ((strncmp(kwl->kw[index].kw, kw, kwend - kw) == 0) &&
118 kwl->kw[index].kw[kwend-kw] == 0) {
119 if (kwl->kw[index].parse)
120 return &kwl->kw[index]; /* found it !*/
121 else
122 ret = &kwl->kw[index]; /* may be OK */
123 }
124 }
125 }
126 return ret;
127}
128
129/*
130 * Dumps all registered "filter" keywords to the <out> string pointer. The
131 * unsupported keywords are only dumped if their supported form was not found.
132 */
133void
134flt_dump_kws(char **out)
135{
136 struct flt_kw_list *kwl;
137 int index;
138
139 *out = NULL;
140 list_for_each_entry(kwl, &flt_keywords.list, list) {
141 for (index = 0; kwl->kw[index].kw != NULL; index++) {
142 if (kwl->kw[index].parse ||
143 flt_find_kw(kwl->kw[index].kw) == &kwl->kw[index]) {
144 memprintf(out, "%s[%4s] %s%s\n", *out ? *out : "",
145 kwl->scope,
146 kwl->kw[index].kw,
147 kwl->kw[index].parse ? "" : " (not supported)");
148 }
149 }
150 }
151}
152
153/*
Christopher Fauletb3f4e142016-03-07 12:46:38 +0100154 * Lists the known filters on <out>
155 */
156void
157list_filters(FILE *out)
158{
159 char *filters, *p, *f;
160
161 fprintf(out, "Available filters :\n");
162 flt_dump_kws(&filters);
163 for (p = filters; (f = strtok_r(p,"\n",&p));)
164 fprintf(out, "\t%s\n", f);
165 free(filters);
166}
167
168/*
Christopher Fauletd7c91962015-04-30 11:48:27 +0200169 * Parses the "filter" keyword. All keywords must be handled by filters
170 * themselves
171 */
172static int
173parse_filter(char **args, int section_type, struct proxy *curpx,
174 struct proxy *defpx, const char *file, int line, char **err)
175{
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100176 struct flt_conf *fconf = NULL;
Christopher Fauletd7c91962015-04-30 11:48:27 +0200177
178 /* Filter cannot be defined on a default proxy */
179 if (curpx == defpx) {
Christopher Fauletcc7317d2016-04-04 10:51:17 +0200180 memprintf(err, "parsing [%s:%d] : %s is not allowed in a 'default' section.",
Christopher Fauletd7c91962015-04-30 11:48:27 +0200181 file, line, args[0]);
182 return -1;
183 }
184 if (!strcmp(args[0], "filter")) {
185 struct flt_kw *kw;
186 int cur_arg;
187
188 if (!*args[1]) {
189 memprintf(err,
190 "parsing [%s:%d] : missing argument for '%s' in %s '%s'.",
191 file, line, args[0], proxy_type_str(curpx), curpx->id);
192 goto error;
193 }
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100194 fconf = calloc(1, sizeof(*fconf));
195 if (!fconf) {
Christopher Fauletd7c91962015-04-30 11:48:27 +0200196 memprintf(err, "'%s' : out of memory", args[0]);
197 goto error;
198 }
Christopher Fauletd7c91962015-04-30 11:48:27 +0200199
200 cur_arg = 1;
201 kw = flt_find_kw(args[cur_arg]);
202 if (kw) {
203 if (!kw->parse) {
204 memprintf(err, "parsing [%s:%d] : '%s' : "
205 "'%s' option is not implemented in this version (check build options).",
206 file, line, args[0], args[cur_arg]);
207 goto error;
208 }
Thierry Fournier3610c392016-04-13 18:27:51 +0200209 if (kw->parse(args, &cur_arg, curpx, fconf, err, kw->private) != 0) {
Christopher Fauletd7c91962015-04-30 11:48:27 +0200210 if (err && *err)
211 memprintf(err, "'%s' : '%s'",
212 args[0], *err);
213 else
214 memprintf(err, "'%s' : error encountered while processing '%s'",
215 args[0], args[cur_arg]);
216 goto error;
217 }
218 }
219 else {
220 flt_dump_kws(err);
221 indent_msg(err, 4);
222 memprintf(err, "'%s' : unknown keyword '%s'.%s%s",
223 args[0], args[cur_arg],
224 err && *err ? " Registered keywords :" : "", err && *err ? *err : "");
225 goto error;
226 }
227 if (*args[cur_arg]) {
228 memprintf(err, "'%s %s' : unknown keyword '%s'.",
229 args[0], args[1], args[cur_arg]);
230 goto error;
231 }
Christopher Faulet00e818a2016-04-19 17:00:44 +0200232 if (fconf->ops == NULL) {
233 memprintf(err, "'%s %s' : no callbacks defined.",
234 args[0], args[1]);
235 goto error;
236 }
Christopher Fauletd7c91962015-04-30 11:48:27 +0200237
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100238 LIST_ADDQ(&curpx->filter_configs, &fconf->list);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200239 }
240 return 0;
241
242 error:
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100243 free(fconf);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200244 return -1;
245
246
247}
248
249/*
250 * Calls 'init' callback for all filters attached to a proxy. This happens after
251 * the configuration parsing. Filters can finish to fill their config. Returns
252 * (ERR_ALERT|ERR_FATAL) if an error occurs, 0 otherwise.
253 */
254int
255flt_init(struct proxy *proxy)
256{
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100257 struct flt_conf *fconf;
Christopher Fauletd7c91962015-04-30 11:48:27 +0200258
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100259 list_for_each_entry(fconf, &proxy->filter_configs, list) {
260 if (fconf->ops->init && fconf->ops->init(proxy, fconf) < 0)
Christopher Fauletd7c91962015-04-30 11:48:27 +0200261 return ERR_ALERT|ERR_FATAL;
262 }
263 return 0;
264}
265
266/*
267 * Calls 'check' callback for all filters attached to a proxy. This happens
268 * after the configuration parsing but before filters initialization. Returns
269 * the number of encountered errors.
270 */
271int
272flt_check(struct proxy *proxy)
273{
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100274 struct flt_conf *fconf;
275 int err = 0;
Christopher Fauletd7c91962015-04-30 11:48:27 +0200276
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100277 list_for_each_entry(fconf, &proxy->filter_configs, list) {
278 if (fconf->ops->check)
279 err += fconf->ops->check(proxy, fconf);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200280 }
Christopher Faulet92d36382015-11-05 13:35:03 +0100281 err += check_legacy_http_comp_flt(proxy);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200282 return err;
283}
284
285/*
286 * Calls 'denit' callback for all filters attached to a proxy. This happens when
287 * HAProxy is stopped.
288 */
289void
290flt_deinit(struct proxy *proxy)
291{
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100292 struct flt_conf *fconf, *back;
Christopher Fauletd7c91962015-04-30 11:48:27 +0200293
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100294 list_for_each_entry_safe(fconf, back, &proxy->filter_configs, list) {
295 if (fconf->ops->deinit)
296 fconf->ops->deinit(proxy, fconf);
297 LIST_DEL(&fconf->list);
298 free(fconf);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200299 }
300}
301
Christopher Faulet92d36382015-11-05 13:35:03 +0100302/* Attaches a filter to a stream. Returns -1 if an error occurs, 0 otherwise. */
303static int
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100304flt_stream_add_filter(struct stream *s, struct flt_conf *fconf, unsigned int flags)
Christopher Faulet92d36382015-11-05 13:35:03 +0100305{
306 struct filter *f = pool_alloc2(pool2_filter);
Christopher Faulet31ed32d2016-06-21 11:42:37 +0200307
Christopher Faulet92d36382015-11-05 13:35:03 +0100308 if (!f) /* not enough memory */
309 return -1;
310 memset(f, 0, sizeof(*f));
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100311 f->config = fconf;
Christopher Fauletda02e172015-12-04 09:25:05 +0100312 f->flags |= flags;
Christopher Faulet31ed32d2016-06-21 11:42:37 +0200313
314 if (FLT_OPS(f)->attach) {
315 int ret = FLT_OPS(f)->attach(s, f);
316 if (ret <= 0) {
317 pool_free2(pool2_filter, f);
318 return ret;
319 }
320 }
321
Christopher Fauletfcf035c2015-12-03 11:48:03 +0100322 LIST_ADDQ(&strm_flt(s)->filters, &f->list);
Christopher Fauletda02e172015-12-04 09:25:05 +0100323 strm_flt(s)->flags |= STRM_FLT_FL_HAS_FILTERS;
Christopher Faulet92d36382015-11-05 13:35:03 +0100324 return 0;
325}
326
327/*
328 * Called when a stream is created. It attaches all frontend filters to the
329 * stream. Returns -1 if an error occurs, 0 otherwise.
330 */
331int
332flt_stream_init(struct stream *s)
333{
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100334 struct flt_conf *fconf;
Christopher Faulet92d36382015-11-05 13:35:03 +0100335
Christopher Fauletfcf035c2015-12-03 11:48:03 +0100336 memset(strm_flt(s), 0, sizeof(*strm_flt(s)));
337 LIST_INIT(&strm_flt(s)->filters);
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100338 list_for_each_entry(fconf, &strm_fe(s)->filter_configs, list) {
339 if (flt_stream_add_filter(s, fconf, 0) < 0)
Christopher Faulet92d36382015-11-05 13:35:03 +0100340 return -1;
341 }
342 return 0;
343}
344
345/*
346 * Called when a stream is closed or when analyze ends (For an HTTP stream, this
347 * happens after each request/response exchange). When analyze ends, backend
348 * filters are removed. When the stream is closed, all filters attached to the
349 * stream are removed.
350 */
351void
352flt_stream_release(struct stream *s, int only_backend)
353{
354 struct filter *filter, *back;
355
Christopher Fauletfcf035c2015-12-03 11:48:03 +0100356 list_for_each_entry_safe(filter, back, &strm_flt(s)->filters, list) {
Christopher Fauletda02e172015-12-04 09:25:05 +0100357 if (!only_backend || (filter->flags & FLT_FL_IS_BACKEND_FILTER)) {
Christopher Faulet31ed32d2016-06-21 11:42:37 +0200358 if (FLT_OPS(filter)->detach)
359 FLT_OPS(filter)->detach(s, filter);
Christopher Faulet92d36382015-11-05 13:35:03 +0100360 LIST_DEL(&filter->list);
361 pool_free2(pool2_filter, filter);
362 }
363 }
Christopher Fauletfcf035c2015-12-03 11:48:03 +0100364 if (LIST_ISEMPTY(&strm_flt(s)->filters))
Christopher Fauletda02e172015-12-04 09:25:05 +0100365 strm_flt(s)->flags &= ~STRM_FLT_FL_HAS_FILTERS;
Christopher Faulet92d36382015-11-05 13:35:03 +0100366}
367
Christopher Fauletd7c91962015-04-30 11:48:27 +0200368/*
369 * Calls 'stream_start' for all filters attached to a stream. This happens when
370 * the stream is created, just after calling flt_stream_init
371 * function. Returns -1 if an error occurs, 0 otherwise.
372 */
373int
374flt_stream_start(struct stream *s)
375{
376 struct filter *filter;
377
Christopher Fauletfcf035c2015-12-03 11:48:03 +0100378 list_for_each_entry(filter, &strm_flt(s)->filters, list) {
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100379 if (FLT_OPS(filter)->stream_start && FLT_OPS(filter)->stream_start(s, filter) < 0)
Christopher Fauletd7c91962015-04-30 11:48:27 +0200380 return -1;
381 }
382 return 0;
383}
384
385/*
386 * Calls 'stream_stop' for all filters attached to a stream. This happens when
387 * the stream is stopped, just before calling flt_stream_release function.
388 */
389void
390flt_stream_stop(struct stream *s)
391{
392 struct filter *filter;
393
Christopher Fauletfcf035c2015-12-03 11:48:03 +0100394 list_for_each_entry(filter, &strm_flt(s)->filters, list) {
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100395 if (FLT_OPS(filter)->stream_stop)
396 FLT_OPS(filter)->stream_stop(s, filter);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200397 }
398}
399
Christopher Faulet92d36382015-11-05 13:35:03 +0100400/*
401 * Called when a backend is set for a stream. If the frontend and the backend
Christopher Faulet31ed32d2016-06-21 11:42:37 +0200402 * are not the same, this function attaches all backend filters to the
403 * stream. Returns -1 if an error occurs, 0 otherwise.
Christopher Faulet92d36382015-11-05 13:35:03 +0100404 */
405int
406flt_set_stream_backend(struct stream *s, struct proxy *be)
407{
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100408 struct flt_conf *fconf;
Christopher Faulet31ed32d2016-06-21 11:42:37 +0200409 struct filter *filter;
Christopher Faulet92d36382015-11-05 13:35:03 +0100410
411 if (strm_fe(s) == be)
Christopher Faulet31ed32d2016-06-21 11:42:37 +0200412 goto end;
Christopher Faulet92d36382015-11-05 13:35:03 +0100413
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100414 list_for_each_entry(fconf, &be->filter_configs, list) {
415 if (flt_stream_add_filter(s, fconf, FLT_FL_IS_BACKEND_FILTER) < 0)
Christopher Faulet92d36382015-11-05 13:35:03 +0100416 return -1;
417 }
Christopher Faulet31ed32d2016-06-21 11:42:37 +0200418
419 end:
420 list_for_each_entry(filter, &strm_flt(s)->filters, list) {
421 if (FLT_OPS(filter)->stream_set_backend &&
422 FLT_OPS(filter)->stream_set_backend(s, filter, be) < 0)
423 return -1;
424 }
425
Christopher Faulet92d36382015-11-05 13:35:03 +0100426 return 0;
427}
428
Christopher Fauletd7c91962015-04-30 11:48:27 +0200429/*
430 * Calls 'http_data' callback for all "data" filters attached to a stream. This
431 * function is called when incoming data are available (excluding chunks
432 * envelope for chunked messages) in the AN_REQ_HTTP_XFER_BODY and
433 * AN_RES_HTTP_XFER_BODY analyzers. It takes care to update the next offset of
434 * filters and adjusts available data to be sure that a filter cannot parse more
435 * data than its predecessors. A filter can choose to not consume all available
436 * data. Returns -1 if an error occurs, the number of consumed bytes otherwise.
437 */
438int
439flt_http_data(struct stream *s, struct http_msg *msg)
440{
Christopher Fauletfcf035c2015-12-03 11:48:03 +0100441 struct filter *filter;
Christopher Fauletd7c91962015-04-30 11:48:27 +0200442 unsigned int buf_i;
Christopher Faulet55048a42016-06-21 10:44:32 +0200443 int delta = 0, ret = 0;
Christopher Fauletd7c91962015-04-30 11:48:27 +0200444
Christopher Fauletd7c91962015-04-30 11:48:27 +0200445 /* Save buffer state */
Christopher Faulet55048a42016-06-21 10:44:32 +0200446 buf_i = msg->chn->buf->i;
Christopher Fauletfcf035c2015-12-03 11:48:03 +0100447
Christopher Fauletfcf035c2015-12-03 11:48:03 +0100448 list_for_each_entry(filter, &strm_flt(s)->filters, list) {
Christopher Fauletda02e172015-12-04 09:25:05 +0100449 unsigned int *nxt;
450
451 /* Call "data" filters only */
452 if (!IS_DATA_FILTER(filter, msg->chn))
453 continue;
454
Christopher Faulet2fb28802015-12-01 10:40:57 +0100455 /* If the HTTP parser is ahead, we update the next offset of the
456 * current filter. This happens for chunked messages, at the
457 * begining of a new chunk. */
Christopher Fauletda02e172015-12-04 09:25:05 +0100458 nxt = &FLT_NXT(filter, msg->chn);
459 if (msg->next > *nxt)
460 *nxt = msg->next;
461
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100462 if (FLT_OPS(filter)->http_data) {
Christopher Faulet55048a42016-06-21 10:44:32 +0200463 unsigned int i = msg->chn->buf->i;
464
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100465 ret = FLT_OPS(filter)->http_data(s, filter, msg);
Christopher Fauletda02e172015-12-04 09:25:05 +0100466 if (ret < 0)
Christopher Fauletd7c91962015-04-30 11:48:27 +0200467 break;
Christopher Faulet55048a42016-06-21 10:44:32 +0200468 delta += (int)(msg->chn->buf->i - i);
Christopher Fauletfcf035c2015-12-03 11:48:03 +0100469
470 /* Update the next offset of the current filter */
Christopher Fauletda02e172015-12-04 09:25:05 +0100471 *nxt += ret;
Christopher Fauletfcf035c2015-12-03 11:48:03 +0100472
473 /* And set this value as the bound for the next
474 * filter. It will not able to parse more data than this
475 * one. */
Christopher Faulet55048a42016-06-21 10:44:32 +0200476 msg->chn->buf->i = *nxt;
Christopher Fauletd7c91962015-04-30 11:48:27 +0200477 }
478 else {
Christopher Fauletfcf035c2015-12-03 11:48:03 +0100479 /* Consume all available data and update the next offset
480 * of the current filter. buf->i is untouched here. */
Christopher Faulet55048a42016-06-21 10:44:32 +0200481 ret = MIN(msg->chunk_len + msg->next, msg->chn->buf->i) - *nxt;
Christopher Fauletda02e172015-12-04 09:25:05 +0100482 *nxt += ret;
Christopher Fauletd7c91962015-04-30 11:48:27 +0200483 }
Christopher Fauletd7c91962015-04-30 11:48:27 +0200484 }
Christopher Fauletfcf035c2015-12-03 11:48:03 +0100485
Christopher Fauletd7c91962015-04-30 11:48:27 +0200486 /* Restore the original buffer state */
Christopher Faulet55048a42016-06-21 10:44:32 +0200487 msg->chn->buf->i = buf_i + delta;
Christopher Fauletfcf035c2015-12-03 11:48:03 +0100488
Christopher Fauletd7c91962015-04-30 11:48:27 +0200489 return ret;
490}
491
Christopher Fauletd7c91962015-04-30 11:48:27 +0200492/*
493 * Calls 'http_chunk_trailers' callback for all "data" filters attached to a
494 * stream. This function is called for chunked messages only when a part of the
495 * trailers was parsed in the AN_REQ_HTTP_XFER_BODY and AN_RES_HTTP_XFER_BODY
496 * analyzers. Filters can know how much data were parsed by the HTTP parsing
497 * until the last call with the msg->sol value. Returns a negative value if an
498 * error occurs, any other value otherwise.
499 */
500int
501flt_http_chunk_trailers(struct stream *s, struct http_msg *msg)
502{
Christopher Faulet2fb28802015-12-01 10:40:57 +0100503 struct filter *filter;
Christopher Fauletda02e172015-12-04 09:25:05 +0100504 int ret = 1;
Christopher Fauletd7c91962015-04-30 11:48:27 +0200505
Christopher Fauletfcf035c2015-12-03 11:48:03 +0100506 list_for_each_entry(filter, &strm_flt(s)->filters, list) {
Christopher Fauletda02e172015-12-04 09:25:05 +0100507 unsigned int *nxt;
508
509 /* Call "data" filters only */
510 if (!IS_DATA_FILTER(filter, msg->chn))
511 continue;
512
Christopher Faulet2fb28802015-12-01 10:40:57 +0100513 /* Be sure to set the next offset of the filter at the right
514 * place. This is really useful when the first part of the
515 * trailers was parsed. */
Christopher Fauletda02e172015-12-04 09:25:05 +0100516 nxt = &FLT_NXT(filter, msg->chn);
517 *nxt = msg->next;
518
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100519 if (FLT_OPS(filter)->http_chunk_trailers) {
520 ret = FLT_OPS(filter)->http_chunk_trailers(s, filter, msg);
Christopher Faulet2fb28802015-12-01 10:40:57 +0100521 if (ret < 0)
522 break;
Christopher Fauletd7c91962015-04-30 11:48:27 +0200523 }
Christopher Faulet2fb28802015-12-01 10:40:57 +0100524 /* Update the next offset of the current filter. Here all data
525 * are always consumed. */
Christopher Fauletda02e172015-12-04 09:25:05 +0100526 *nxt += msg->sol;
Christopher Faulet2fb28802015-12-01 10:40:57 +0100527 }
Christopher Fauletd7c91962015-04-30 11:48:27 +0200528 return ret;
529}
530
531/*
532 * Calls 'http_end' callback for all filters attached to a stream. All filters
533 * are called here, but only if there is at least one "data" filter. This
534 * functions is called when all data were parsed and forwarded. 'http_end'
535 * callback is resumable, so this function returns a negative value if an error
536 * occurs, 0 if it needs to wait for some reason, any other value otherwise.
537 */
538int
539flt_http_end(struct stream *s, struct http_msg *msg)
540{
541 int ret = 1;
542
Christopher Fauletd7c91962015-04-30 11:48:27 +0200543 RESUME_FILTER_LOOP(s, msg->chn) {
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100544 if (FLT_OPS(filter)->http_end) {
545 ret = FLT_OPS(filter)->http_end(s, filter, msg);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200546 if (ret <= 0)
547 BREAK_EXECUTION(s, msg->chn, end);
548 }
Christopher Fauletd7c91962015-04-30 11:48:27 +0200549 } RESUME_FILTER_END;
550end:
551 return ret;
552}
553
554/*
555 * Calls 'http_reset' callback for all filters attached to a stream. This
556 * happens when a 100-continue response is received.
557 */
558void
559flt_http_reset(struct stream *s, struct http_msg *msg)
560{
561 struct filter *filter;
562
Christopher Fauletfcf035c2015-12-03 11:48:03 +0100563 list_for_each_entry(filter, &strm_flt(s)->filters, list) {
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100564 if (FLT_OPS(filter)->http_reset)
565 FLT_OPS(filter)->http_reset(s, filter, msg);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200566 }
567}
568
569/*
570 * Calls 'http_reply' callback for all filters attached to a stream when HA
571 * decides to stop the HTTP message processing.
572 */
573void
574flt_http_reply(struct stream *s, short status, const struct chunk *msg)
575{
576 struct filter *filter;
577
Christopher Fauletfcf035c2015-12-03 11:48:03 +0100578 list_for_each_entry(filter, &strm_flt(s)->filters, list) {
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100579 if (FLT_OPS(filter)->http_reply)
580 FLT_OPS(filter)->http_reply(s, filter, status, msg);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200581 }
582}
583
584/*
585 * Calls 'http_forward_data' callback for all "data" filters attached to a
586 * stream. This function is called when some data can be forwarded in the
587 * AN_REQ_HTTP_XFER_BODY and AN_RES_HTTP_XFER_BODY analyzers. It takes care to
588 * update the forward offset of filters and adjusts "forwardable" data to be
589 * sure that a filter cannot forward more data than its predecessors. A filter
590 * can choose to not forward all parsed data. Returns a negative value if an
591 * error occurs, else the number of forwarded bytes.
592 */
593int
594flt_http_forward_data(struct stream *s, struct http_msg *msg, unsigned int len)
595{
Christopher Fauletfcf035c2015-12-03 11:48:03 +0100596 struct filter *filter;
Christopher Fauletd7c91962015-04-30 11:48:27 +0200597 int ret = len;
598
Christopher Fauletfcf035c2015-12-03 11:48:03 +0100599 list_for_each_entry(filter, &strm_flt(s)->filters, list) {
Christopher Fauletda02e172015-12-04 09:25:05 +0100600 unsigned int *nxt, *fwd;
601
602 /* Call "data" filters only */
603 if (!IS_DATA_FILTER(filter, msg->chn))
604 continue;
605
Christopher Faulet2fb28802015-12-01 10:40:57 +0100606 /* If the HTTP parser is ahead, we update the next offset of the
607 * current filter. This happens for chunked messages, when the
608 * chunk envelope is parsed. */
Christopher Fauletda02e172015-12-04 09:25:05 +0100609 nxt = &FLT_NXT(filter, msg->chn);
610 fwd = &FLT_FWD(filter, msg->chn);
611 if (msg->next > *nxt)
612 *nxt = msg->next;
613
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100614 if (FLT_OPS(filter)->http_forward_data) {
Christopher Fauletda02e172015-12-04 09:25:05 +0100615 /* Remove bytes that the current filter considered as
616 * forwarded */
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100617 ret = FLT_OPS(filter)->http_forward_data(s, filter, msg, ret - *fwd);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200618 if (ret < 0)
619 goto end;
620 }
621
622 /* Adjust bytes that the current filter considers as
623 * forwarded */
Christopher Fauletda02e172015-12-04 09:25:05 +0100624 *fwd += ret;
Christopher Fauletd7c91962015-04-30 11:48:27 +0200625
626 /* And set this value as the bound for the next filter. It will
627 * not able to forward more data than the current one. */
Christopher Fauletda02e172015-12-04 09:25:05 +0100628 ret = *fwd;
Christopher Fauletd7c91962015-04-30 11:48:27 +0200629 }
630
631 if (!ret)
632 goto end;
633
634 /* Finally, adjust filters offsets by removing data that HAProxy will
635 * forward. */
Christopher Fauletfcf035c2015-12-03 11:48:03 +0100636 list_for_each_entry(filter, &strm_flt(s)->filters, list) {
Christopher Fauletda02e172015-12-04 09:25:05 +0100637 if (!IS_DATA_FILTER(filter, msg->chn))
638 continue;
Christopher Fauletd7c91962015-04-30 11:48:27 +0200639 FLT_NXT(filter, msg->chn) -= ret;
640 FLT_FWD(filter, msg->chn) -= ret;
641 }
642 end:
643 return ret;
644}
645
646/*
647 * Calls 'channel_start_analyze' callback for all filters attached to a
648 * stream. This function is called when we start to analyze a request or a
649 * response. For frontend filters, it is called before all other analyzers. For
650 * backend ones, it is called before all backend
651 * analyzers. 'channel_start_analyze' callback is resumable, so this function
652 * returns 0 if an error occurs or if it needs to wait, any other value
653 * otherwise.
654 */
655int
656flt_start_analyze(struct stream *s, struct channel *chn, unsigned int an_bit)
657{
658 int ret = 1;
659
660 /* If this function is called, this means there is at least one filter,
661 * so we do not need to check the filter list's emptiness. */
662
663 RESUME_FILTER_LOOP(s, chn) {
Christopher Fauletda02e172015-12-04 09:25:05 +0100664 if (an_bit == AN_FLT_START_BE && !(filter->flags & FLT_FL_IS_BACKEND_FILTER))
Christopher Fauletd7c91962015-04-30 11:48:27 +0200665 continue;
666
Christopher Fauletfcf035c2015-12-03 11:48:03 +0100667 FLT_NXT(filter, chn) = 0;
668 FLT_FWD(filter, chn) = 0;
Christopher Fauletd7c91962015-04-30 11:48:27 +0200669
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100670 if (FLT_OPS(filter)->channel_start_analyze) {
671 ret = FLT_OPS(filter)->channel_start_analyze(s, filter, chn);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200672 if (ret <= 0)
673 BREAK_EXECUTION(s, chn, end);
674 }
675 } RESUME_FILTER_END;
676
677 end:
678 return handle_analyzer_result(s, chn, an_bit, ret);
679}
680
681/*
Christopher Faulet3a394fa2016-05-11 17:13:39 +0200682 * Calls 'channel_pre_analyze' callback for all filters attached to a
683 * stream. This function is called BEFORE each analyzer attached to a channel,
684 * expects analyzers responsible for data sending. 'channel_pre_analyze'
685 * callback is resumable, so this function returns 0 if an error occurs or if it
686 * needs to wait, any other value otherwise.
687 *
688 * Note this function can be called many times for the same analyzer. In fact,
689 * it is called until the analyzer finishes its processing.
Christopher Fauletd7c91962015-04-30 11:48:27 +0200690 */
691int
Christopher Faulet3a394fa2016-05-11 17:13:39 +0200692flt_pre_analyze(struct stream *s, struct channel *chn, unsigned int an_bit)
Christopher Fauletd7c91962015-04-30 11:48:27 +0200693{
694 int ret = 1;
695
Christopher Fauletd7c91962015-04-30 11:48:27 +0200696 RESUME_FILTER_LOOP(s, chn) {
Christopher Faulet3a394fa2016-05-11 17:13:39 +0200697 if (FLT_OPS(filter)->channel_pre_analyze && (filter->pre_analyzers & an_bit)) {
698 ret = FLT_OPS(filter)->channel_pre_analyze(s, filter, chn, an_bit);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200699 if (ret <= 0)
700 BREAK_EXECUTION(s, chn, check_result);
701 }
702 } RESUME_FILTER_END;
703
704 check_result:
Christopher Faulet309c6412015-12-02 09:57:32 +0100705 return handle_analyzer_result(s, chn, 0, ret);
706}
707
708/*
Christopher Faulet3a394fa2016-05-11 17:13:39 +0200709 * Calls 'channel_post_analyze' callback for all filters attached to a
710 * stream. This function is called AFTER each analyzer attached to a channel,
711 * expects analyzers responsible for data sending. 'channel_post_analyze'
712 * callback is NOT resumable, so this function returns a 0 if an error occurs,
713 * any other value otherwise.
714 *
715 * Here, AFTER means when the analyzer finishes its processing.
716 */
717int
718flt_post_analyze(struct stream *s, struct channel *chn, unsigned int an_bit)
719{
720 struct filter *filter;
721 int ret = 1;
722
723 list_for_each_entry(filter, &strm_flt(s)->filters, list) {
724 if (FLT_OPS(filter)->channel_post_analyze && (filter->post_analyzers & an_bit)) {
725 ret = FLT_OPS(filter)->channel_post_analyze(s, filter, chn, an_bit);
726 if (ret < 0)
727 break;
728 }
729 }
730 return handle_analyzer_result(s, chn, 0, ret);
731}
732
733/*
Christopher Faulet1339d742016-05-11 16:48:33 +0200734 * This function is the AN_FLT_HTTP_HDRS analyzer, used to filter HTTP headers
735 * or a request or a response. Returns 0 if an error occurs or if it needs to
736 * wait, any other value otherwise.
Christopher Faulet309c6412015-12-02 09:57:32 +0100737 */
738int
739flt_analyze_http_headers(struct stream *s, struct channel *chn, unsigned int an_bit)
740{
Christopher Faulet1339d742016-05-11 16:48:33 +0200741 struct filter *filter;
742 struct http_msg *msg;
743 int ret = 1;
Christopher Faulet309c6412015-12-02 09:57:32 +0100744
Christopher Faulet1339d742016-05-11 16:48:33 +0200745 msg = ((chn->flags & CF_ISRESP) ? &s->txn->rsp : &s->txn->req);
Christopher Faulet309c6412015-12-02 09:57:32 +0100746 RESUME_FILTER_LOOP(s, chn) {
Christopher Faulet1339d742016-05-11 16:48:33 +0200747 if (FLT_OPS(filter)->http_headers) {
748 ret = FLT_OPS(filter)->http_headers(s, filter, msg);
Christopher Faulet309c6412015-12-02 09:57:32 +0100749 if (ret <= 0)
750 BREAK_EXECUTION(s, chn, check_result);
751 }
752 } RESUME_FILTER_END;
753
754 /* We increase next offset of all "data" filters after all processing on
755 * headers because any filter can alter them. So the definitive size of
756 * headers (msg->sov) is only known when all filters have been
757 * called. */
Christopher Fauletfcf035c2015-12-03 11:48:03 +0100758 list_for_each_entry(filter, &strm_flt(s)->filters, list) {
Christopher Fauletda02e172015-12-04 09:25:05 +0100759 /* Handle "data" filters only */
760 if (!IS_DATA_FILTER(filter, chn))
761 continue;
Christopher Faulet1339d742016-05-11 16:48:33 +0200762 FLT_NXT(filter, chn) = msg->sov;
Christopher Faulet309c6412015-12-02 09:57:32 +0100763 }
764
765 check_result:
766 return handle_analyzer_result(s, chn, an_bit, ret);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200767}
768
769/*
770 * Calls 'channel_end_analyze' callback for all filters attached to a
771 * stream. This function is called when we stop to analyze a request or a
772 * response. It is called after all other analyzers. 'channel_end_analyze'
773 * callback is resumable, so this function returns 0 if an error occurs or if it
774 * needs to wait, any other value otherwise.
775 */
776int
777flt_end_analyze(struct stream *s, struct channel *chn, unsigned int an_bit)
778{
779 int ret = 1;
780
Christopher Fauletd7c91962015-04-30 11:48:27 +0200781 RESUME_FILTER_LOOP(s, chn) {
Christopher Fauletfcf035c2015-12-03 11:48:03 +0100782 FLT_NXT(filter, chn) = 0;
783 FLT_FWD(filter, chn) = 0;
Christopher Fauletda02e172015-12-04 09:25:05 +0100784 unregister_data_filter(s, chn, filter);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200785
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100786 if (FLT_OPS(filter)->channel_end_analyze) {
787 ret = FLT_OPS(filter)->channel_end_analyze(s, filter, chn);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200788 if (ret <= 0)
789 BREAK_EXECUTION(s, chn, end);
790 }
791 } RESUME_FILTER_END;
792
793end:
794 ret = handle_analyzer_result(s, chn, an_bit, ret);
Christopher Faulet02c7b222015-12-22 12:01:29 +0100795
796 /* Check if 'channel_end_analyze' callback has been called for the
797 * request and the response. */
798 if (!(s->req.analysers & AN_FLT_END) && !(s->res.analysers & AN_FLT_END)) {
Christopher Faulet02c7b222015-12-22 12:01:29 +0100799 /* When we are waiting for a new request, so we must reset
800 * stream analyzers. The input must not be closed the request
801 * channel, else it is useless to wait. */
802 if (s->txn && (s->txn->flags & TX_WAIT_NEXT_RQ) && !channel_input_closed(&s->req)) {
803 s->req.analysers = strm_li(s) ? strm_li(s)->analysers : 0;
804 s->res.analysers = 0;
Christopher Fauletc6062be2016-10-31 11:22:37 +0100805
806 /* Remove backend filters from the list */
807 flt_stream_release(s, 1);
Christopher Faulet02c7b222015-12-22 12:01:29 +0100808 }
Christopher Fauletd7c91962015-04-30 11:48:27 +0200809
Christopher Fauletd7c91962015-04-30 11:48:27 +0200810 }
811 else if (ret) {
812 /* Analyzer ends only for one channel. So wake up the stream to
813 * be sure to process it for the other side as soon as
814 * possible. */
815 task_wakeup(s->task, TASK_WOKEN_MSG);
816 }
817 return ret;
818}
819
820
821/*
822 * Calls 'tcp_data' callback for all "data" filters attached to a stream. This
823 * function is called when incoming data are available. It takes care to update
824 * the next offset of filters and adjusts available data to be sure that a
825 * filter cannot parse more data than its predecessors. A filter can choose to
826 * not consume all available data. Returns -1 if an error occurs, the number of
827 * consumed bytes otherwise.
828 */
829static int
830flt_data(struct stream *s, struct channel *chn)
831{
Christopher Fauletda02e172015-12-04 09:25:05 +0100832 struct filter *filter;
Christopher Fauletd7c91962015-04-30 11:48:27 +0200833 unsigned int buf_i;
Christopher Faulet55048a42016-06-21 10:44:32 +0200834 int delta = 0, ret = 0;
Christopher Fauletd7c91962015-04-30 11:48:27 +0200835
836 /* Save buffer state */
Christopher Faulet55048a42016-06-21 10:44:32 +0200837 buf_i = chn->buf->i;
Christopher Fauletfcf035c2015-12-03 11:48:03 +0100838
839 list_for_each_entry(filter, &strm_flt(s)->filters, list) {
Christopher Fauletda02e172015-12-04 09:25:05 +0100840 unsigned int *nxt;
841
842 /* Call "data" filters only */
843 if (!IS_DATA_FILTER(filter, chn))
844 continue;
845
846 nxt = &FLT_NXT(filter, chn);
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100847 if (FLT_OPS(filter)->tcp_data) {
Christopher Faulet55048a42016-06-21 10:44:32 +0200848 unsigned int i = chn->buf->i;
849
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100850 ret = FLT_OPS(filter)->tcp_data(s, filter, chn);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200851 if (ret < 0)
852 break;
Christopher Faulet55048a42016-06-21 10:44:32 +0200853 delta += (int)(chn->buf->i - i);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200854
Christopher Fauletfcf035c2015-12-03 11:48:03 +0100855 /* Increase next offset of the current filter */
Christopher Fauletda02e172015-12-04 09:25:05 +0100856 *nxt += ret;
Christopher Fauletfcf035c2015-12-03 11:48:03 +0100857
858 /* And set this value as the bound for the next
859 * filter. It will not able to parse more data than the
860 * current one. */
Christopher Faulet55048a42016-06-21 10:44:32 +0200861 chn->buf->i = *nxt;
Christopher Fauletfcf035c2015-12-03 11:48:03 +0100862 }
863 else {
864 /* Consume all available data */
Christopher Faulet55048a42016-06-21 10:44:32 +0200865 *nxt = chn->buf->i;
Christopher Fauletfcf035c2015-12-03 11:48:03 +0100866 }
Christopher Fauletd7c91962015-04-30 11:48:27 +0200867
868 /* Update <ret> value to be sure to have the last one when we
Christopher Fauletda02e172015-12-04 09:25:05 +0100869 * exit from the loop. This value will be used to know how much
870 * data are "forwardable" */
871 ret = *nxt;
Christopher Fauletd7c91962015-04-30 11:48:27 +0200872 }
Christopher Fauletfcf035c2015-12-03 11:48:03 +0100873
874 /* Restore the original buffer state */
Christopher Faulet55048a42016-06-21 10:44:32 +0200875 chn->buf->i = buf_i + delta;
Christopher Fauletfcf035c2015-12-03 11:48:03 +0100876
Christopher Fauletd7c91962015-04-30 11:48:27 +0200877 return ret;
878}
879
880/*
881 * Calls 'tcp_forward_data' callback for all "data" filters attached to a
882 * stream. This function is called when some data can be forwarded. It takes
883 * care to update the forward offset of filters and adjusts "forwardable" data
884 * to be sure that a filter cannot forward more data than its predecessors. A
885 * filter can choose to not forward all parsed data. Returns a negative value if
886 * an error occurs, else the number of forwarded bytes.
887 */
888static int
889flt_forward_data(struct stream *s, struct channel *chn, unsigned int len)
890{
Christopher Fauletda02e172015-12-04 09:25:05 +0100891 struct filter *filter;
Christopher Fauletd7c91962015-04-30 11:48:27 +0200892 int ret = len;
893
Christopher Fauletfcf035c2015-12-03 11:48:03 +0100894 list_for_each_entry(filter, &strm_flt(s)->filters, list) {
Christopher Fauletda02e172015-12-04 09:25:05 +0100895 unsigned int *fwd;
896
897 /* Call "data" filters only */
898 if (!IS_DATA_FILTER(filter, chn))
899 continue;
900
901 fwd = &FLT_FWD(filter, chn);
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100902 if (FLT_OPS(filter)->tcp_forward_data) {
Christopher Fauletd7c91962015-04-30 11:48:27 +0200903 /* Remove bytes that the current filter considered as
904 * forwarded */
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100905 ret = FLT_OPS(filter)->tcp_forward_data(s, filter, chn, ret - *fwd);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200906 if (ret < 0)
907 goto end;
908 }
909
Christopher Fauletda02e172015-12-04 09:25:05 +0100910 /* Adjust bytes that the current filter considers as
Christopher Fauletd7c91962015-04-30 11:48:27 +0200911 * forwarded */
Christopher Fauletda02e172015-12-04 09:25:05 +0100912 *fwd += ret;
Christopher Fauletd7c91962015-04-30 11:48:27 +0200913
914 /* And set this value as the bound for the next filter. It will
915 * not able to forward more data than the current one. */
Christopher Fauletda02e172015-12-04 09:25:05 +0100916 ret = *fwd;
Christopher Fauletd7c91962015-04-30 11:48:27 +0200917 }
918
919 if (!ret)
920 goto end;
921
Christopher Fauletda02e172015-12-04 09:25:05 +0100922 /* Finally, adjust filters offsets by removing data that HAProxy will
923 * forward. */
Christopher Fauletfcf035c2015-12-03 11:48:03 +0100924 list_for_each_entry(filter, &strm_flt(s)->filters, list) {
Christopher Fauletda02e172015-12-04 09:25:05 +0100925 if (!IS_DATA_FILTER(filter, chn))
926 continue;
Christopher Fauletd7c91962015-04-30 11:48:27 +0200927 FLT_NXT(filter, chn) -= ret;
928 FLT_FWD(filter, chn) -= ret;
929 }
930
Christopher Fauletd7c91962015-04-30 11:48:27 +0200931 end:
932 return ret;
933}
934
935/*
936 * Called when TCP data must be filtered on a channel. This function is the
937 * AN_FLT_XFER_DATA analyzer. When called, it is responsible to forward data
938 * when the proxy is not in http mode. Behind the scene, it calls consecutively
939 * 'tcp_data' and 'tcp_forward_data' callbacks for all "data" filters attached
940 * to a stream. Returns 0 if an error occurs or if it needs to wait, any other
941 * value otherwise.
942 */
943int
944flt_xfer_data(struct stream *s, struct channel *chn, unsigned int an_bit)
945{
946 int ret = 1;
947
Christopher Fauletda02e172015-12-04 09:25:05 +0100948 /* If there is no "data" filters, we do nothing */
949 if (!HAS_DATA_FILTERS(s, chn))
950 goto end;
Christopher Fauletd7c91962015-04-30 11:48:27 +0200951
952 /* Be sure that the output is still opened. Else we stop the data
953 * filtering. */
954 if ((chn->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
955 ((chn->flags & CF_SHUTW) && (chn->to_forward || chn->buf->o)))
956 goto end;
957
958 /* Let all "data" filters parsing incoming data */
959 ret = flt_data(s, chn);
960 if (ret < 0)
961 goto end;
962
963 /* And forward them */
964 ret = flt_forward_data(s, chn, ret);
965 if (ret < 0)
966 goto end;
967
Christopher Fauletda02e172015-12-04 09:25:05 +0100968 /* Consume data that all filters consider as forwarded. */
969 b_adv(chn->buf, ret);
970
Christopher Fauletd7c91962015-04-30 11:48:27 +0200971 /* Stop waiting data if the input in closed and no data is pending or if
972 * the output is closed. */
973 if ((chn->flags & CF_SHUTW) ||
974 ((chn->flags & CF_SHUTR) && !buffer_pending(chn->buf))) {
975 ret = 1;
976 goto end;
977 }
978
979 /* Wait for data */
980 return 0;
981 end:
982 /* Terminate the data filtering. If <ret> is negative, an error was
983 * encountered during the filtering. */
984 return handle_analyzer_result(s, chn, an_bit, ret);
985}
986
987/*
988 * Handles result of filter's analyzers. It returns 0 if an error occurs or if
989 * it needs to wait, any other value otherwise.
990 */
991static int
992handle_analyzer_result(struct stream *s, struct channel *chn,
993 unsigned int an_bit, int ret)
994{
995 int finst;
996
997 if (ret < 0)
998 goto return_bad_req;
999 else if (!ret)
1000 goto wait;
1001
1002 /* End of job, return OK */
1003 if (an_bit) {
1004 chn->analysers &= ~an_bit;
1005 chn->analyse_exp = TICK_ETERNITY;
1006 }
1007 return 1;
1008
1009 return_bad_req:
1010 /* An error occurs */
1011 channel_abort(&s->req);
1012 channel_abort(&s->res);
1013
1014 if (!(chn->flags & CF_ISRESP)) {
1015 s->req.analysers &= AN_FLT_END;
1016 finst = SF_FINST_R;
1017 /* FIXME: incr counters */
1018 }
1019 else {
1020 s->res.analysers &= AN_FLT_END;
1021 finst = SF_FINST_H;
1022 /* FIXME: incr counters */
1023 }
1024
1025 if (s->txn) {
1026 /* Do not do that when we are waiting for the next request */
1027 if (s->txn->status)
1028 http_reply_and_close(s, s->txn->status, NULL);
1029 else {
1030 s->txn->status = 400;
1031 http_reply_and_close(s, 400, http_error_message(s, HTTP_ERR_400));
1032 }
1033 }
1034
1035 if (!(s->flags & SF_ERR_MASK))
1036 s->flags |= SF_ERR_PRXCOND;
1037 if (!(s->flags & SF_FINST_MASK))
1038 s->flags |= finst;
1039 return 0;
1040
1041 wait:
1042 if (!(chn->flags & CF_ISRESP))
1043 channel_dont_connect(chn);
1044 return 0;
1045}
1046
1047
1048/* Note: must not be declared <const> as its list will be overwritten.
1049 * Please take care of keeping this list alphabetically sorted, doing so helps
1050 * all code contributors.
1051 * Optional keywords are also declared with a NULL ->parse() function so that
1052 * the config parser can report an appropriate error when a known keyword was
1053 * not enabled. */
1054static struct cfg_kw_list cfg_kws = {ILH, {
1055 { CFG_LISTEN, "filter", parse_filter },
1056 { 0, NULL, NULL },
1057 }
1058};
1059
1060__attribute__((constructor))
1061static void
1062__filters_init(void)
1063{
1064 pool2_filter = create_pool("filter", sizeof(struct filter), MEM_F_SHARED);
1065 cfg_register_keywords(&cfg_kws);
1066}
1067
1068__attribute__((destructor))
1069static void
1070__filters_deinit(void)
1071{
1072 pool_destroy2(pool2_filter);
1073}
1074
1075/*
1076 * Local variables:
1077 * c-indent-level: 8
1078 * c-basic-offset: 8
1079 * End:
1080 */