blob: 635b2d191062b68fd5ba227a093032fb35f04ad8 [file] [log] [blame]
Christopher Fauletd7c91962015-04-30 11:48:27 +02001/*
2 * include/types/filteers.h
3 * This file defines everything related to stream filters.
4 *
5 * Copyright (C) 2015 Qualys Inc., Christopher Faulet <cfaulet@qualys.com>
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation, version 2.1
10 * exclusively.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21#ifndef _TYPES_FILTERS_H
22#define _TYPES_FILTERS_H
23
24#include <common/config.h>
25#include <common/mini-clist.h>
26
27struct http_msg;
28struct proxy;
29struct stream;
30struct channel;
31struct filter;
32
33/* Descriptor for a "filter" keyword. The ->parse() function returns 0 in case
34 * of success, or a combination of ERR_* flags if an error is encountered. The
35 * function pointer can be NULL if not implemented. The function also has an
36 * access to the current "server" config line. The ->skip value tells the parser
37 * how many words have to be skipped after the keyword. If the function needs to
38 * parse more keywords, it needs to update cur_arg.
39 */
40struct flt_kw {
41 const char *kw;
42 int (*parse)(char **args, int *cur_arg, struct proxy *px,
43 struct filter *filter, char **err);
44};
45
46/*
47 * A keyword list. It is a NULL-terminated array of keywords. It embeds a struct
48 * list in order to be linked to other lists, allowing it to easily be declared
49 * where it is needed, and linked without duplicating data nor allocating
50 * memory. It is also possible to indicate a scope for the keywords.
51 */
52struct flt_kw_list {
53 const char *scope;
54 struct list list;
55 struct flt_kw kw[VAR_ARRAY];
56};
57
58/*
59 * Filter flags set for a specific filter on channel
60 *
61 * - FILTER_FL_FORWARD_DATA : When this flag is set, the rest of the data is
62 * directly forwarded. For chunk-encoded HTTP
63 * messages, this flag is reseted between each
64 * chunks.
65 */
66#define FILTER_FL_FORWARD_DATA 0x00000001
67
68
69/*
70 * Callbacks available on a filter:
71 *
72 * - init : Initializes the filter for a proxy. Returns a
73 * negative value if an error occurs.
74 * - deinit : Cleans up what the init function has done.
75 * - check : Check the filter config for a proxy. Returns the
76 * number of errors encountered.
77 *
78 *
79 * - stream_start : Called when a stream is started. This callback will
80 * only be called for filters defined on a proxy with
81 * the frontend capability.
82 * Returns a negative value if an error occurs, any
83 * other value otherwise.
84 * - stream_stop : Called when a stream is stopped. This callback will
85 * only be called for filters defined on a proxy with
86 * the frontend capability.
87 *
88 *
89 * - channel_start_analyze: Called when a filter starts to analyze a channel.
90 * Returns a negative value if an error occurs, 0 if
91 * it needs to wait, any other value otherwise.
92 * - channel_analyze : Called before each analyzer attached to a channel,
93 * expects analyzers responsible for data sending.
94 * Returns a negative value if an error occurs, 0 if
95 * it needs to wait, any other value otherwise.
96 * - channel_end_analyze : Called when all other analyzers have finished their
97 * processing.
98 * Returns a negative value if an error occurs, 0 if
99 * it needs to wait, any other value otherwise.
100 *
101 *
102 * - http_headers : Called just before headers sending and parsing of
103 * the body. At this step, headers are fully parsed
104 * and the processing on it is finished.
105 * Returns a negative value if an error occurs, 0 if
106 * it needs to read more data (or to wait for some
107 * reason), any other value otherwise.
108 * - http_start_chunk : Called when we start to process a new chunk
109 * (for chunk-encoded request/response only). At this
110 * step, the chunk length is known and non-null.
111 * Returns a negative value if an error occurs, 0 if
112 * it needs to read more data (or to wait for some
113 * reason), any other value otherwise.
114 * - http_data : Called when unparsed body data are available.
115 * Returns a negative value if an error occurs, else
116 * the number of consumed bytes.
117 * - http_last_chunk : Called when the last chunk (with a zero length) is
118 * received.
119 * Returns a negative value if an error occurs, 0 if
120 * it needs to read more data (or to wait for some
121 * reason), any other value otherwise.
122 * - http_end_chunk : Called at the end of a chunk (expect for the last
123 * one).
124 * Returns a negative value if an error occurs, 0 if
125 * it needs to read more data (or to wait for some
126 * reason), any other value otherwise.
127 * - http_chunk_trailers : Called when part of trailer headers of a
128 * chunk-encoded request/response are ready to be
129 * processed.
130 * Returns a negative value if an error occurs, any
131 * other value otherwise.
132 * - http_end : Called when all the request/response has been
133 * processed and all body data has been forwarded.
134 * Returns a negative value if an error occurs, 0 if
135 * it needs to wait for some reason, any other value
136 * otherwise.
137 * - http_reset : Called when the HTTP message is reseted. It happens
138 * when a 100-continue response is received.
139 * Returns nothing.
140 * - http_reply : Called when, at any time, HA proxy decides to stop
141 * the HTTP message's processing and to send a message
142 * to the client (mainly, when an error or a redirect
143 * occur).
144 * Returns nothing.
145 * - http_forward_data : Called when some data can be consumed.
146 * Returns a negative value if an error occurs, else
147 * the number of forwarded bytes.
148 * - tcp_data : Called when unparsed data are available.
149 * Returns a negative value if an error occurs, else
150 * the number of consumed bytes.
151 * - tcp_forward_data : Called when some data can be consumed.
152 * Returns a negative value if an error occurs, else
153 * or the number of forwarded bytes.
154 */
155struct flt_ops {
156 /*
157 * Callbacks to manage the filter lifecycle
158 */
159 int (*init) (struct proxy *p, struct filter *f);
160 void (*deinit)(struct proxy *p, struct filter *f);
161 int (*check) (struct proxy *p, struct filter *f);
162
163 /*
164 * Stream callbacks
165 */
166 int (*stream_start) (struct stream *s, struct filter *f);
167 void (*stream_stop) (struct stream *s, struct filter *f);
168
169 /*
170 * Channel callbacks
171 */
172 int (*channel_start_analyze)(struct stream *s, struct filter *f, struct channel *chn);
173 int (*channel_analyze) (struct stream *s, struct filter *f, struct channel *chn, unsigned int an_bit);
174 int (*channel_end_analyze) (struct stream *s, struct filter *f, struct channel *chn);
175
176 /*
177 * HTTP callbacks
178 */
179 int (*http_headers) (struct stream *s, struct filter *f, struct http_msg *msg);
180 int (*http_start_chunk) (struct stream *s, struct filter *f, struct http_msg *msg);
181 int (*http_data) (struct stream *s, struct filter *f, struct http_msg *msg);
182 int (*http_last_chunk) (struct stream *s, struct filter *f, struct http_msg *msg);
183 int (*http_end_chunk) (struct stream *s, struct filter *f, struct http_msg *msg);
184 int (*http_chunk_trailers)(struct stream *s, struct filter *f, struct http_msg *msg);
185 int (*http_end) (struct stream *s, struct filter *f, struct http_msg *msg);
186 void (*http_reset) (struct stream *s, struct filter *f, struct http_msg *msg);
187
188 void (*http_reply) (struct stream *s, struct filter *f, short status,
189 const struct chunk *msg);
190 int (*http_forward_data) (struct stream *s, struct filter *f, struct http_msg *msg,
191 unsigned int len);
192
193 /*
194 * TCP callbacks
195 */
196 int (*tcp_data) (struct stream *s, struct filter *f, struct channel *chn);
197 int (*tcp_forward_data)(struct stream *s, struct filter *f, struct channel *chn,
198 unsigned int len);
199};
200
201/*
202 * Structure representing the state of a filter. When attached to a proxy, only
203 * <ops> and <conf> field (and optionnaly <id>) are set. All other fields are
204 * used when the filter is attached to a stream.
205 *
206 * 2D-Array fields are used to store info per channel. The first index stands
207 * for the request channel, and the second one for the response channel.
208 * Especially, <next> and <fwd> are offets representing amount of data that the
209 * filter are, respectively, parsed and forwarded on a channel. Filters can
210 * access these values using FLT_NXT and FLT_FWD macros.
211 */
212struct filter {
213 const char *id; /* The filter id */
214 struct flt_ops *ops; /* The filter callbacks */
215 void *conf; /* The filter configuration */
216 void *ctx; /* The filter context (opaque) */
217 int is_backend_filter; /* Flag to specify if the filter is a "backend" filter */
218 unsigned int flags[2]; /* 0: request, 1: response */
219 unsigned int next[2]; /* Offset, relative to buf->p, to the next byte to parse for a specific channel
220 * 0: request channel, 1: response channel */
221 unsigned int fwd[2]; /* Offset, relative to buf->p, to the next byte to forward for a specific channel
222 * 0: request channel, 1: response channel */
223 struct list list; /* Next filter for the same proxy/stream */
224};
225
226#endif /* _TYPES_FILTERS_H */
227
228/*
229 * Local variables:
230 * c-indent-level: 8
231 * c-basic-offset: 8
232 * End:
233 */