blob: 399f4178ee7d3233287811c363d5ab1f1d07665f [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 \
60 if ((strm)->strm_flt.current[CHN_IDX(chn)]) { \
61 filter = (strm)->strm_flt.current[CHN_IDX(chn)]; \
62 (strm)->strm_flt.current[CHN_IDX(chn)] = NULL; \
63 goto resume_execution; \
64 } \
65 \
Christopher Fauletfcf035c2015-12-03 11:48:03 +010066 list_for_each_entry(filter, &strm_flt(s)->filters, list) { \
Christopher Fauletd7c91962015-04-30 11:48:27 +020067 resume_execution:
68
69#define RESUME_FILTER_END \
70 } \
71 } while(0)
72
73#define BREAK_EXECUTION(strm, chn, label) \
74 do { \
75 (strm)->strm_flt.current[CHN_IDX(chn)] = filter; \
76 goto label; \
77 } 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/*
154 * Parses the "filter" keyword. All keywords must be handled by filters
155 * themselves
156 */
157static int
158parse_filter(char **args, int section_type, struct proxy *curpx,
159 struct proxy *defpx, const char *file, int line, char **err)
160{
161 struct filter *filter = NULL;
162
163 /* Filter cannot be defined on a default proxy */
164 if (curpx == defpx) {
165 memprintf(err, "parsing [%s:%d] : %s is only allowed in a 'default' section.",
166 file, line, args[0]);
167 return -1;
168 }
169 if (!strcmp(args[0], "filter")) {
170 struct flt_kw *kw;
171 int cur_arg;
172
173 if (!*args[1]) {
174 memprintf(err,
175 "parsing [%s:%d] : missing argument for '%s' in %s '%s'.",
176 file, line, args[0], proxy_type_str(curpx), curpx->id);
177 goto error;
178 }
179 filter = pool_alloc2(pool2_filter);
180 if (!filter) {
181 memprintf(err, "'%s' : out of memory", args[0]);
182 goto error;
183 }
184 memset(filter, 0, sizeof(*filter));
185
186 cur_arg = 1;
187 kw = flt_find_kw(args[cur_arg]);
188 if (kw) {
189 if (!kw->parse) {
190 memprintf(err, "parsing [%s:%d] : '%s' : "
191 "'%s' option is not implemented in this version (check build options).",
192 file, line, args[0], args[cur_arg]);
193 goto error;
194 }
195 if (kw->parse(args, &cur_arg, curpx, filter, err) != 0) {
196 if (err && *err)
197 memprintf(err, "'%s' : '%s'",
198 args[0], *err);
199 else
200 memprintf(err, "'%s' : error encountered while processing '%s'",
201 args[0], args[cur_arg]);
202 goto error;
203 }
204 }
205 else {
206 flt_dump_kws(err);
207 indent_msg(err, 4);
208 memprintf(err, "'%s' : unknown keyword '%s'.%s%s",
209 args[0], args[cur_arg],
210 err && *err ? " Registered keywords :" : "", err && *err ? *err : "");
211 goto error;
212 }
213 if (*args[cur_arg]) {
214 memprintf(err, "'%s %s' : unknown keyword '%s'.",
215 args[0], args[1], args[cur_arg]);
216 goto error;
217 }
218
219 LIST_ADDQ(&curpx->filters, &filter->list);
220 }
221 return 0;
222
223 error:
224 if (filter)
225 pool_free2(pool2_filter, filter);
226 return -1;
227
228
229}
230
231/*
232 * Calls 'init' callback for all filters attached to a proxy. This happens after
233 * the configuration parsing. Filters can finish to fill their config. Returns
234 * (ERR_ALERT|ERR_FATAL) if an error occurs, 0 otherwise.
235 */
236int
237flt_init(struct proxy *proxy)
238{
239 struct filter *filter;
240
241 list_for_each_entry(filter, &proxy->filters, list) {
242 if (filter->ops->init && filter->ops->init(proxy, filter) < 0)
243 return ERR_ALERT|ERR_FATAL;
244 }
245 return 0;
246}
247
248/*
249 * Calls 'check' callback for all filters attached to a proxy. This happens
250 * after the configuration parsing but before filters initialization. Returns
251 * the number of encountered errors.
252 */
253int
254flt_check(struct proxy *proxy)
255{
256 struct filter *filter;
257 int err = 0;
258
259 list_for_each_entry(filter, &proxy->filters, list) {
260 if (filter->ops->check)
261 err += filter->ops->check(proxy, filter);
262 }
Christopher Faulet92d36382015-11-05 13:35:03 +0100263 err += check_legacy_http_comp_flt(proxy);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200264 return err;
265}
266
267/*
268 * Calls 'denit' callback for all filters attached to a proxy. This happens when
269 * HAProxy is stopped.
270 */
271void
272flt_deinit(struct proxy *proxy)
273{
274 struct filter *filter, *back;
275
276 list_for_each_entry_safe(filter, back, &proxy->filters, list) {
277 if (filter->ops->deinit)
278 filter->ops->deinit(proxy, filter);
279 LIST_DEL(&filter->list);
280 pool_free2(pool2_filter, filter);
281 }
282}
283
Christopher Faulet92d36382015-11-05 13:35:03 +0100284/* Attaches a filter to a stream. Returns -1 if an error occurs, 0 otherwise. */
285static int
286flt_stream_add_filter(struct stream *s, struct filter *filter,
287 int is_backend)
288{
289 struct filter *f = pool_alloc2(pool2_filter);
290 if (!f) /* not enough memory */
291 return -1;
292 memset(f, 0, sizeof(*f));
293 f->id = filter->id;
294 f->ops = filter->ops;
295 f->conf = filter->conf;
296 f->is_backend_filter = is_backend;
Christopher Fauletfcf035c2015-12-03 11:48:03 +0100297 LIST_ADDQ(&strm_flt(s)->filters, &f->list);
298 strm_flt(s)->has_filters = 1;
Christopher Faulet92d36382015-11-05 13:35:03 +0100299 return 0;
300}
301
302/*
303 * Called when a stream is created. It attaches all frontend filters to the
304 * stream. Returns -1 if an error occurs, 0 otherwise.
305 */
306int
307flt_stream_init(struct stream *s)
308{
309 struct filter *filter;
310
Christopher Fauletfcf035c2015-12-03 11:48:03 +0100311 memset(strm_flt(s), 0, sizeof(*strm_flt(s)));
312 LIST_INIT(&strm_flt(s)->filters);
Christopher Faulet92d36382015-11-05 13:35:03 +0100313 list_for_each_entry(filter, &strm_fe(s)->filters, list) {
314 if (flt_stream_add_filter(s, filter, 0) < 0)
315 return -1;
316 }
317 return 0;
318}
319
320/*
321 * Called when a stream is closed or when analyze ends (For an HTTP stream, this
322 * happens after each request/response exchange). When analyze ends, backend
323 * filters are removed. When the stream is closed, all filters attached to the
324 * stream are removed.
325 */
326void
327flt_stream_release(struct stream *s, int only_backend)
328{
329 struct filter *filter, *back;
330
Christopher Fauletfcf035c2015-12-03 11:48:03 +0100331 list_for_each_entry_safe(filter, back, &strm_flt(s)->filters, list) {
Christopher Faulet92d36382015-11-05 13:35:03 +0100332 if (!only_backend || filter->is_backend_filter) {
333 LIST_DEL(&filter->list);
334 pool_free2(pool2_filter, filter);
335 }
336 }
Christopher Fauletfcf035c2015-12-03 11:48:03 +0100337 if (LIST_ISEMPTY(&strm_flt(s)->filters))
338 strm_flt(s)->has_filters = 0;
Christopher Faulet92d36382015-11-05 13:35:03 +0100339}
340
Christopher Fauletd7c91962015-04-30 11:48:27 +0200341/*
342 * Calls 'stream_start' for all filters attached to a stream. This happens when
343 * the stream is created, just after calling flt_stream_init
344 * function. Returns -1 if an error occurs, 0 otherwise.
345 */
346int
347flt_stream_start(struct stream *s)
348{
349 struct filter *filter;
350
Christopher Fauletfcf035c2015-12-03 11:48:03 +0100351 list_for_each_entry(filter, &strm_flt(s)->filters, list) {
Christopher Fauletd7c91962015-04-30 11:48:27 +0200352 if (filter->ops->stream_start && filter->ops->stream_start(s, filter) < 0)
353 return -1;
354 }
355 return 0;
356}
357
358/*
359 * Calls 'stream_stop' for all filters attached to a stream. This happens when
360 * the stream is stopped, just before calling flt_stream_release function.
361 */
362void
363flt_stream_stop(struct stream *s)
364{
365 struct filter *filter;
366
Christopher Fauletfcf035c2015-12-03 11:48:03 +0100367 list_for_each_entry(filter, &strm_flt(s)->filters, list) {
Christopher Fauletd7c91962015-04-30 11:48:27 +0200368 if (filter->ops->stream_stop)
369 filter->ops->stream_stop(s, filter);
370 }
371}
372
Christopher Faulet92d36382015-11-05 13:35:03 +0100373/*
374 * Called when a backend is set for a stream. If the frontend and the backend
375 * are the same, this function does nothing. Else it attaches all backend
376 * filters to the stream. Returns -1 if an error occurs, 0 otherwise.
377 */
378int
379flt_set_stream_backend(struct stream *s, struct proxy *be)
380{
381 struct filter *filter;
382
383 if (strm_fe(s) == be)
384 return 0;
385
386 list_for_each_entry(filter, &be->filters, list) {
387 if (flt_stream_add_filter(s, filter, 1) < 0)
388 return -1;
389 }
390 return 0;
391}
392
Christopher Fauletd7c91962015-04-30 11:48:27 +0200393/*
394 * Calls 'http_data' callback for all "data" filters attached to a stream. This
395 * function is called when incoming data are available (excluding chunks
396 * envelope for chunked messages) in the AN_REQ_HTTP_XFER_BODY and
397 * AN_RES_HTTP_XFER_BODY analyzers. It takes care to update the next offset of
398 * filters and adjusts available data to be sure that a filter cannot parse more
399 * data than its predecessors. A filter can choose to not consume all available
400 * data. Returns -1 if an error occurs, the number of consumed bytes otherwise.
401 */
402int
403flt_http_data(struct stream *s, struct http_msg *msg)
404{
Christopher Fauletfcf035c2015-12-03 11:48:03 +0100405 struct filter *filter;
406 struct buffer *buf = msg->chn->buf;
Christopher Fauletd7c91962015-04-30 11:48:27 +0200407 unsigned int buf_i;
408 int ret = 0;
409
Christopher Fauletd7c91962015-04-30 11:48:27 +0200410 /* Save buffer state */
Christopher Fauletfcf035c2015-12-03 11:48:03 +0100411 buf_i = buf->i;
412
413 buf->i = MIN(msg->chunk_len + msg->next, buf->i);
414 list_for_each_entry(filter, &strm_flt(s)->filters, list) {
Christopher Faulet2fb28802015-12-01 10:40:57 +0100415 /* If the HTTP parser is ahead, we update the next offset of the
416 * current filter. This happens for chunked messages, at the
417 * begining of a new chunk. */
418 if (msg->next > FLT_NXT(filter, msg->chn))
419 FLT_NXT(filter, msg->chn) = msg->next;
Christopher Fauletd7c91962015-04-30 11:48:27 +0200420 if (filter->ops->http_data && !flt_want_forward_data(filter, msg->chn)) {
421 ret = filter->ops->http_data(s, filter, msg);
Christopher Faulet2fb28802015-12-01 10:40:57 +0100422 if (ret <= 0)
Christopher Fauletd7c91962015-04-30 11:48:27 +0200423 break;
Christopher Fauletfcf035c2015-12-03 11:48:03 +0100424
425 /* Update the next offset of the current filter */
426 FLT_NXT(filter, msg->chn) += ret;
427
428 /* And set this value as the bound for the next
429 * filter. It will not able to parse more data than this
430 * one. */
431 buf->i = FLT_NXT(filter, msg->chn);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200432 }
433 else {
Christopher Fauletfcf035c2015-12-03 11:48:03 +0100434 /* Consume all available data and update the next offset
435 * of the current filter. buf->i is untouched here. */
436 ret = buf->i - FLT_NXT(filter, msg->chn);
437 FLT_NXT(filter, msg->chn) = buf->i;
Christopher Fauletd7c91962015-04-30 11:48:27 +0200438 }
Christopher Fauletd7c91962015-04-30 11:48:27 +0200439 }
Christopher Fauletfcf035c2015-12-03 11:48:03 +0100440
Christopher Fauletd7c91962015-04-30 11:48:27 +0200441 /* Restore the original buffer state */
Christopher Fauletfcf035c2015-12-03 11:48:03 +0100442 buf->i = buf_i;
443
Christopher Fauletd7c91962015-04-30 11:48:27 +0200444 return ret;
445}
446
Christopher Fauletd7c91962015-04-30 11:48:27 +0200447/*
448 * Calls 'http_chunk_trailers' callback for all "data" filters attached to a
449 * stream. This function is called for chunked messages only when a part of the
450 * trailers was parsed in the AN_REQ_HTTP_XFER_BODY and AN_RES_HTTP_XFER_BODY
451 * analyzers. Filters can know how much data were parsed by the HTTP parsing
452 * until the last call with the msg->sol value. Returns a negative value if an
453 * error occurs, any other value otherwise.
454 */
455int
456flt_http_chunk_trailers(struct stream *s, struct http_msg *msg)
457{
Christopher Faulet2fb28802015-12-01 10:40:57 +0100458 struct filter *filter;
Christopher Fauletfcf035c2015-12-03 11:48:03 +0100459 int ret = 1;
Christopher Fauletd7c91962015-04-30 11:48:27 +0200460
Christopher Fauletfcf035c2015-12-03 11:48:03 +0100461 list_for_each_entry(filter, &strm_flt(s)->filters, list) {
Christopher Faulet2fb28802015-12-01 10:40:57 +0100462 /* Be sure to set the next offset of the filter at the right
463 * place. This is really useful when the first part of the
464 * trailers was parsed. */
465 FLT_NXT(filter, msg->chn) = msg->next;
Christopher Fauletd7c91962015-04-30 11:48:27 +0200466 if (filter->ops->http_chunk_trailers) {
467 ret = filter->ops->http_chunk_trailers(s, filter, msg);
Christopher Faulet2fb28802015-12-01 10:40:57 +0100468 if (ret < 0)
469 break;
Christopher Fauletd7c91962015-04-30 11:48:27 +0200470 }
Christopher Faulet2fb28802015-12-01 10:40:57 +0100471 /* Update the next offset of the current filter. Here all data
472 * are always consumed. */
Christopher Fauletd7c91962015-04-30 11:48:27 +0200473 FLT_NXT(filter, msg->chn) += msg->sol;
Christopher Faulet2fb28802015-12-01 10:40:57 +0100474 }
Christopher Fauletd7c91962015-04-30 11:48:27 +0200475 return ret;
476}
477
478/*
479 * Calls 'http_end' callback for all filters attached to a stream. All filters
480 * are called here, but only if there is at least one "data" filter. This
481 * functions is called when all data were parsed and forwarded. 'http_end'
482 * callback is resumable, so this function returns a negative value if an error
483 * occurs, 0 if it needs to wait for some reason, any other value otherwise.
484 */
485int
486flt_http_end(struct stream *s, struct http_msg *msg)
487{
488 int ret = 1;
489
Christopher Fauletd7c91962015-04-30 11:48:27 +0200490 RESUME_FILTER_LOOP(s, msg->chn) {
491 if (filter->ops->http_end) {
492 ret = filter->ops->http_end(s, filter, msg);
493 if (ret <= 0)
494 BREAK_EXECUTION(s, msg->chn, end);
495 }
496 flt_reset_forward_data(filter, msg->chn);
497 } RESUME_FILTER_END;
498end:
499 return ret;
500}
501
502/*
503 * Calls 'http_reset' callback for all filters attached to a stream. This
504 * happens when a 100-continue response is received.
505 */
506void
507flt_http_reset(struct stream *s, struct http_msg *msg)
508{
509 struct filter *filter;
510
Christopher Fauletfcf035c2015-12-03 11:48:03 +0100511 list_for_each_entry(filter, &strm_flt(s)->filters, list) {
Christopher Fauletd7c91962015-04-30 11:48:27 +0200512 if (filter->ops->http_reset)
513 filter->ops->http_reset(s, filter, msg);
514 }
515}
516
517/*
518 * Calls 'http_reply' callback for all filters attached to a stream when HA
519 * decides to stop the HTTP message processing.
520 */
521void
522flt_http_reply(struct stream *s, short status, const struct chunk *msg)
523{
524 struct filter *filter;
525
Christopher Fauletfcf035c2015-12-03 11:48:03 +0100526 list_for_each_entry(filter, &strm_flt(s)->filters, list) {
Christopher Fauletd7c91962015-04-30 11:48:27 +0200527 if (filter->ops->http_reply)
528 filter->ops->http_reply(s, filter, status, msg);
529 }
530}
531
532/*
533 * Calls 'http_forward_data' callback for all "data" filters attached to a
534 * stream. This function is called when some data can be forwarded in the
535 * AN_REQ_HTTP_XFER_BODY and AN_RES_HTTP_XFER_BODY analyzers. It takes care to
536 * update the forward offset of filters and adjusts "forwardable" data to be
537 * sure that a filter cannot forward more data than its predecessors. A filter
538 * can choose to not forward all parsed data. Returns a negative value if an
539 * error occurs, else the number of forwarded bytes.
540 */
541int
542flt_http_forward_data(struct stream *s, struct http_msg *msg, unsigned int len)
543{
Christopher Fauletfcf035c2015-12-03 11:48:03 +0100544 struct filter *filter;
Christopher Fauletd7c91962015-04-30 11:48:27 +0200545 int ret = len;
546
Christopher Fauletfcf035c2015-12-03 11:48:03 +0100547 list_for_each_entry(filter, &strm_flt(s)->filters, list) {
Christopher Faulet2fb28802015-12-01 10:40:57 +0100548 /* If the HTTP parser is ahead, we update the next offset of the
549 * current filter. This happens for chunked messages, when the
550 * chunk envelope is parsed. */
551 if (msg->next > FLT_NXT(filter, msg->chn))
552 FLT_NXT(filter, msg->chn) = msg->next;
Christopher Fauletd7c91962015-04-30 11:48:27 +0200553 if (filter->ops->http_forward_data) {
554 /* Remove bytes that the current filter considered as
555 * forwarded */
556 ret = filter->ops->http_forward_data(s, filter, msg,
557 ret - FLT_FWD(filter, msg->chn));
558 if (ret < 0)
559 goto end;
560 }
561
562 /* Adjust bytes that the current filter considers as
563 * forwarded */
564 FLT_FWD(filter, msg->chn) += ret;
565
566 /* And set this value as the bound for the next filter. It will
567 * not able to forward more data than the current one. */
568 ret = FLT_FWD(filter, msg->chn);
569 }
570
571 if (!ret)
572 goto end;
573
574 /* Finally, adjust filters offsets by removing data that HAProxy will
575 * forward. */
Christopher Fauletfcf035c2015-12-03 11:48:03 +0100576 list_for_each_entry(filter, &strm_flt(s)->filters, list) {
Christopher Fauletd7c91962015-04-30 11:48:27 +0200577 FLT_NXT(filter, msg->chn) -= ret;
578 FLT_FWD(filter, msg->chn) -= ret;
579 }
580 end:
581 return ret;
582}
583
584/*
585 * Calls 'channel_start_analyze' callback for all filters attached to a
586 * stream. This function is called when we start to analyze a request or a
587 * response. For frontend filters, it is called before all other analyzers. For
588 * backend ones, it is called before all backend
589 * analyzers. 'channel_start_analyze' callback is resumable, so this function
590 * returns 0 if an error occurs or if it needs to wait, any other value
591 * otherwise.
592 */
593int
594flt_start_analyze(struct stream *s, struct channel *chn, unsigned int an_bit)
595{
596 int ret = 1;
597
598 /* If this function is called, this means there is at least one filter,
599 * so we do not need to check the filter list's emptiness. */
600
601 RESUME_FILTER_LOOP(s, chn) {
602 if (an_bit == AN_FLT_START_BE && !filter->is_backend_filter)
603 continue;
604
Christopher Fauletfcf035c2015-12-03 11:48:03 +0100605 FLT_NXT(filter, chn) = 0;
606 FLT_FWD(filter, chn) = 0;
Christopher Fauletd7c91962015-04-30 11:48:27 +0200607
608 if (filter->ops->channel_start_analyze) {
609 ret = filter->ops->channel_start_analyze(s, filter, chn);
610 if (ret <= 0)
611 BREAK_EXECUTION(s, chn, end);
612 }
613 } RESUME_FILTER_END;
614
615 end:
616 return handle_analyzer_result(s, chn, an_bit, ret);
617}
618
619/*
620 * Calls 'channel_analyze' callback for all filters attached to a stream. This
621 * function is called before each analyzer attached to a channel, expects
622 * analyzers responsible for data sending. 'channel_analyze' callback is
623 * resumable, so this function returns 0 if an error occurs or if it needs to
624 * wait, any other value otherwise.
625 */
626int
627flt_analyze(struct stream *s, struct channel *chn, unsigned int an_bit)
628{
629 int ret = 1;
630
Christopher Fauletd7c91962015-04-30 11:48:27 +0200631 RESUME_FILTER_LOOP(s, chn) {
632 if (filter->ops->channel_analyze) {
633 ret = filter->ops->channel_analyze(s, filter, chn, an_bit);
634 if (ret <= 0)
635 BREAK_EXECUTION(s, chn, check_result);
636 }
637 } RESUME_FILTER_END;
638
639 check_result:
Christopher Faulet309c6412015-12-02 09:57:32 +0100640 return handle_analyzer_result(s, chn, 0, ret);
641}
642
643/*
644 * This function do the same that the previsous one, but for the
645 * AN_FLT_HTTP_HDRS analyzer. The difference is what is done when all filters
646 * have been called. Returns 0 if an error occurs or if it needs to wait, any
647 * other value otherwise.
648 */
649int
650flt_analyze_http_headers(struct stream *s, struct channel *chn, unsigned int an_bit)
651{
652 struct filter *filter;
653 struct http_msg *msg;
654 int ret = 1;
655
656 RESUME_FILTER_LOOP(s, chn) {
657 if (filter->ops->channel_analyze) {
658 ret = filter->ops->channel_analyze(s, filter, chn, an_bit);
659 if (ret <= 0)
660 BREAK_EXECUTION(s, chn, check_result);
661 }
662 } RESUME_FILTER_END;
663
664 /* We increase next offset of all "data" filters after all processing on
665 * headers because any filter can alter them. So the definitive size of
666 * headers (msg->sov) is only known when all filters have been
667 * called. */
668 msg = ((chn->flags & CF_ISRESP) ? &s->txn->rsp : &s->txn->req);
Christopher Fauletfcf035c2015-12-03 11:48:03 +0100669 list_for_each_entry(filter, &strm_flt(s)->filters, list) {
Christopher Faulet309c6412015-12-02 09:57:32 +0100670 FLT_NXT(filter, msg->chn) = msg->sov;
671 }
672
673 check_result:
674 return handle_analyzer_result(s, chn, an_bit, ret);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200675}
676
677/*
678 * Calls 'channel_end_analyze' callback for all filters attached to a
679 * stream. This function is called when we stop to analyze a request or a
680 * response. It is called after all other analyzers. 'channel_end_analyze'
681 * callback is resumable, so this function returns 0 if an error occurs or if it
682 * needs to wait, any other value otherwise.
683 */
684int
685flt_end_analyze(struct stream *s, struct channel *chn, unsigned int an_bit)
686{
687 int ret = 1;
688
689 /* If this function is called, this means there is at least one filter,
690 * so we do not need to check the filter list's emptiness. */
691
692 RESUME_FILTER_LOOP(s, chn) {
Christopher Fauletfcf035c2015-12-03 11:48:03 +0100693 FLT_NXT(filter, chn) = 0;
694 FLT_FWD(filter, chn) = 0;
Christopher Fauletd7c91962015-04-30 11:48:27 +0200695
696 if (filter->ops->channel_end_analyze) {
697 ret = filter->ops->channel_end_analyze(s, filter, chn);
698 if (ret <= 0)
699 BREAK_EXECUTION(s, chn, end);
700 }
701 } RESUME_FILTER_END;
702
703end:
704 ret = handle_analyzer_result(s, chn, an_bit, ret);
Christopher Faulet02c7b222015-12-22 12:01:29 +0100705
706 /* Check if 'channel_end_analyze' callback has been called for the
707 * request and the response. */
708 if (!(s->req.analysers & AN_FLT_END) && !(s->res.analysers & AN_FLT_END)) {
Christopher Faulet02c7b222015-12-22 12:01:29 +0100709 /* When we are waiting for a new request, so we must reset
710 * stream analyzers. The input must not be closed the request
711 * channel, else it is useless to wait. */
712 if (s->txn && (s->txn->flags & TX_WAIT_NEXT_RQ) && !channel_input_closed(&s->req)) {
713 s->req.analysers = strm_li(s) ? strm_li(s)->analysers : 0;
714 s->res.analysers = 0;
715 }
Christopher Fauletd7c91962015-04-30 11:48:27 +0200716
Christopher Faulet92d36382015-11-05 13:35:03 +0100717 /* Remove backend filters from the list */
718 flt_stream_release(s, 1);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200719 }
720 else if (ret) {
721 /* Analyzer ends only for one channel. So wake up the stream to
722 * be sure to process it for the other side as soon as
723 * possible. */
724 task_wakeup(s->task, TASK_WOKEN_MSG);
725 }
726 return ret;
727}
728
729
730/*
731 * Calls 'tcp_data' callback for all "data" filters attached to a stream. This
732 * function is called when incoming data are available. It takes care to update
733 * the next offset of filters and adjusts available data to be sure that a
734 * filter cannot parse more data than its predecessors. A filter can choose to
735 * not consume all available data. Returns -1 if an error occurs, the number of
736 * consumed bytes otherwise.
737 */
738static int
739flt_data(struct stream *s, struct channel *chn)
740{
741 struct filter *filter = NULL;
Christopher Fauletfcf035c2015-12-03 11:48:03 +0100742 struct buffer *buf = chn->buf;
Christopher Fauletd7c91962015-04-30 11:48:27 +0200743 unsigned int buf_i;
Christopher Fauletfcf035c2015-12-03 11:48:03 +0100744 int ret = 0;
Christopher Fauletd7c91962015-04-30 11:48:27 +0200745
746 /* Save buffer state */
Christopher Fauletfcf035c2015-12-03 11:48:03 +0100747 buf_i = buf->i;
748
749 list_for_each_entry(filter, &strm_flt(s)->filters, list) {
Christopher Fauletd7c91962015-04-30 11:48:27 +0200750 if (filter->ops->tcp_data && !flt_want_forward_data(filter, chn)) {
751 ret = filter->ops->tcp_data(s, filter, chn);
752 if (ret < 0)
753 break;
Christopher Fauletd7c91962015-04-30 11:48:27 +0200754
Christopher Fauletfcf035c2015-12-03 11:48:03 +0100755 /* Increase next offset of the current filter */
756 FLT_NXT(filter, chn) += ret;
757
758 /* And set this value as the bound for the next
759 * filter. It will not able to parse more data than the
760 * current one. */
761 buf->i = FLT_NXT(filter, chn);
762 }
763 else {
764 /* Consume all available data */
765 FLT_NXT(filter, chn) = buf->i;
766 }
Christopher Fauletd7c91962015-04-30 11:48:27 +0200767
768 /* Update <ret> value to be sure to have the last one when we
769 * exit from the loop. */
770 ret = FLT_NXT(filter, chn);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200771 }
Christopher Fauletfcf035c2015-12-03 11:48:03 +0100772
773 /* Restore the original buffer state */
Christopher Fauletd7c91962015-04-30 11:48:27 +0200774 chn->buf->i = buf_i;
Christopher Fauletfcf035c2015-12-03 11:48:03 +0100775
Christopher Fauletd7c91962015-04-30 11:48:27 +0200776 return ret;
777}
778
779/*
780 * Calls 'tcp_forward_data' callback for all "data" filters attached to a
781 * stream. This function is called when some data can be forwarded. It takes
782 * care to update the forward offset of filters and adjusts "forwardable" data
783 * to be sure that a filter cannot forward more data than its predecessors. A
784 * filter can choose to not forward all parsed data. Returns a negative value if
785 * an error occurs, else the number of forwarded bytes.
786 */
787static int
788flt_forward_data(struct stream *s, struct channel *chn, unsigned int len)
789{
790 struct filter *filter = NULL;
791 int ret = len;
792
Christopher Fauletfcf035c2015-12-03 11:48:03 +0100793 list_for_each_entry(filter, &strm_flt(s)->filters, list) {
Christopher Fauletd7c91962015-04-30 11:48:27 +0200794 if (filter->ops->tcp_forward_data) {
795 /* Remove bytes that the current filter considered as
796 * forwarded */
Christopher Fauletfcf035c2015-12-03 11:48:03 +0100797 ret = filter->ops->tcp_forward_data(s, filter, chn,
798 ret - FLT_FWD(filter, chn));
Christopher Fauletd7c91962015-04-30 11:48:27 +0200799 if (ret < 0)
800 goto end;
801 }
802
803 /* Adjust bytes taht the current filter considers as
804 * forwarded */
805 FLT_FWD(filter, chn) += ret;
806
807 /* And set this value as the bound for the next filter. It will
808 * not able to forward more data than the current one. */
809 ret = FLT_FWD(filter, chn);
810 }
811
812 if (!ret)
813 goto end;
814
815 /* Adjust forward counter and next offset of filters by removing data
816 * that HAProxy will consider as forwarded. */
Christopher Fauletfcf035c2015-12-03 11:48:03 +0100817 list_for_each_entry(filter, &strm_flt(s)->filters, list) {
Christopher Fauletd7c91962015-04-30 11:48:27 +0200818 FLT_NXT(filter, chn) -= ret;
819 FLT_FWD(filter, chn) -= ret;
820 }
821
822 /* Consume data that all filters consider as forwarded. */
823 b_adv(chn->buf, ret);
824 end:
825 return ret;
826}
827
828/*
829 * Called when TCP data must be filtered on a channel. This function is the
830 * AN_FLT_XFER_DATA analyzer. When called, it is responsible to forward data
831 * when the proxy is not in http mode. Behind the scene, it calls consecutively
832 * 'tcp_data' and 'tcp_forward_data' callbacks for all "data" filters attached
833 * to a stream. Returns 0 if an error occurs or if it needs to wait, any other
834 * value otherwise.
835 */
836int
837flt_xfer_data(struct stream *s, struct channel *chn, unsigned int an_bit)
838{
839 int ret = 1;
840
841 /* If this function is called, this means there is at least one filter,
842 * so we do not need to check the filter list's emptiness. */
843
844 /* Be sure that the output is still opened. Else we stop the data
845 * filtering. */
846 if ((chn->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
847 ((chn->flags & CF_SHUTW) && (chn->to_forward || chn->buf->o)))
848 goto end;
849
850 /* Let all "data" filters parsing incoming data */
851 ret = flt_data(s, chn);
852 if (ret < 0)
853 goto end;
854
855 /* And forward them */
856 ret = flt_forward_data(s, chn, ret);
857 if (ret < 0)
858 goto end;
859
860 /* Stop waiting data if the input in closed and no data is pending or if
861 * the output is closed. */
862 if ((chn->flags & CF_SHUTW) ||
863 ((chn->flags & CF_SHUTR) && !buffer_pending(chn->buf))) {
864 ret = 1;
865 goto end;
866 }
867
868 /* Wait for data */
869 return 0;
870 end:
871 /* Terminate the data filtering. If <ret> is negative, an error was
872 * encountered during the filtering. */
873 return handle_analyzer_result(s, chn, an_bit, ret);
874}
875
876/*
877 * Handles result of filter's analyzers. It returns 0 if an error occurs or if
878 * it needs to wait, any other value otherwise.
879 */
880static int
881handle_analyzer_result(struct stream *s, struct channel *chn,
882 unsigned int an_bit, int ret)
883{
884 int finst;
885
886 if (ret < 0)
887 goto return_bad_req;
888 else if (!ret)
889 goto wait;
890
891 /* End of job, return OK */
892 if (an_bit) {
893 chn->analysers &= ~an_bit;
894 chn->analyse_exp = TICK_ETERNITY;
895 }
896 return 1;
897
898 return_bad_req:
899 /* An error occurs */
900 channel_abort(&s->req);
901 channel_abort(&s->res);
902
903 if (!(chn->flags & CF_ISRESP)) {
904 s->req.analysers &= AN_FLT_END;
905 finst = SF_FINST_R;
906 /* FIXME: incr counters */
907 }
908 else {
909 s->res.analysers &= AN_FLT_END;
910 finst = SF_FINST_H;
911 /* FIXME: incr counters */
912 }
913
914 if (s->txn) {
915 /* Do not do that when we are waiting for the next request */
916 if (s->txn->status)
917 http_reply_and_close(s, s->txn->status, NULL);
918 else {
919 s->txn->status = 400;
920 http_reply_and_close(s, 400, http_error_message(s, HTTP_ERR_400));
921 }
922 }
923
924 if (!(s->flags & SF_ERR_MASK))
925 s->flags |= SF_ERR_PRXCOND;
926 if (!(s->flags & SF_FINST_MASK))
927 s->flags |= finst;
928 return 0;
929
930 wait:
931 if (!(chn->flags & CF_ISRESP))
932 channel_dont_connect(chn);
933 return 0;
934}
935
936
937/* Note: must not be declared <const> as its list will be overwritten.
938 * Please take care of keeping this list alphabetically sorted, doing so helps
939 * all code contributors.
940 * Optional keywords are also declared with a NULL ->parse() function so that
941 * the config parser can report an appropriate error when a known keyword was
942 * not enabled. */
943static struct cfg_kw_list cfg_kws = {ILH, {
944 { CFG_LISTEN, "filter", parse_filter },
945 { 0, NULL, NULL },
946 }
947};
948
949__attribute__((constructor))
950static void
951__filters_init(void)
952{
953 pool2_filter = create_pool("filter", sizeof(struct filter), MEM_F_SHARED);
954 cfg_register_keywords(&cfg_kws);
955}
956
957__attribute__((destructor))
958static void
959__filters_deinit(void)
960{
961 pool_destroy2(pool2_filter);
962}
963
964/*
965 * Local variables:
966 * c-indent-level: 8
967 * c-basic-offset: 8
968 * End:
969 */