MINOR: lua: Add a function to get a filter attached to a channel class

For now, there is no support for filters written in lua. So this function,
if called, will always return NULL. But when it will be called in a filter
context, it will return the filter structure attached to a channel
class. This function is also responsible to set the offset of data that may
be processed and the length of these data. When called outside a filter
context (so from an action), the offset is the input data position and the
length is the input data length. From a filter, the offset and the length of
data that may be filtered are retrieved the filter context.
This commit is contained in:
Christopher Faulet 2020-02-25 15:21:02 +01:00
parent 69c581a092
commit 9f55a5012e

View File

@ -196,6 +196,8 @@ lua_State *hlua_init_state(int thread_id);
*/
static lua_State *hlua_states[MAX_THREADS + 1];
#define HLUA_FLT_CTX_FL_PAYLOAD 0x00000001
struct hlua_reg_filter {
char *name;
int flt_ref[MAX_THREADS + 1];
@ -219,6 +221,8 @@ struct hlua_flt_ctx {
DECLARE_STATIC_POOL(pool_head_hlua_flt_ctx, "hlua_flt_ctx", sizeof(struct hlua_flt_ctx));
static int hlua_filter_from_payload(struct filter *filter);
/* This is the chained list of struct hlua_flt referenced
* for haproxy filters. It is used for a post-initialisation control.
*/
@ -3000,6 +3004,30 @@ static int hlua_channel_new(lua_State *L, struct channel *channel)
return 1;
}
/* Helper function returning a filter attached to a channel at the position <ud>
* in the stack, filling the current offset and length of the filter. If no
* filter is attached, NULL is returned and <offet> and <len> are not
* initialized.
*/
static __maybe_unused struct filter *hlua_channel_filter(lua_State *L, int ud, struct channel *chn, size_t *offset, size_t *len)
{
struct filter *filter = NULL;
if (lua_getfield(L, ud, "__filter") == LUA_TLIGHTUSERDATA) {
struct hlua_flt_ctx *flt_ctx;
filter = lua_touserdata (L, -1);
flt_ctx = filter->ctx;
if (hlua_filter_from_payload(filter)) {
*offset = flt_ctx->cur_off[CHN_IDX(chn)];
*len = flt_ctx->cur_len[CHN_IDX(chn)];
}
}
lua_pop(L, 1);
return filter;
}
/* Copies <len> bytes of data present in the channel's buffer, starting at the
* offset <offset>, and put it in a LUA string variable. It is the caller
* responsibility to ensure <len> and <offset> are valid. It always return the
@ -8812,6 +8840,13 @@ static void hlua_filter_delete(struct stream *s, struct filter *filter)
filter->ctx = NULL;
}
static int hlua_filter_from_payload(struct filter *filter)
{
struct hlua_flt_ctx *flt_ctx = filter->ctx;
return (flt_ctx && !!(flt_ctx->flags & HLUA_FLT_CTX_FL_PAYLOAD));
}
static int hlua_filter_parse_fct(char **args, int *cur_arg, struct proxy *px,
struct flt_conf *fconf, char **err, void *private)
{