2010-01-04 10:16:05 -05:00
|
|
|
/*
|
2020-06-04 09:33:47 -04:00
|
|
|
* include/haproxy/sample.h
|
2012-04-27 15:52:18 -04:00
|
|
|
* Functions for samples management.
|
2010-01-04 10:16:05 -05:00
|
|
|
*
|
|
|
|
|
* Copyright (C) 2009-2010 EXCELIANCE, Emeric Brun <ebrun@exceliance.fr>
|
2012-04-27 15:52:18 -04:00
|
|
|
* Copyright (C) 2012 Willy Tarreau <w@1wt.eu>
|
2010-01-04 10:16:05 -05:00
|
|
|
*
|
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
|
* License as published by the Free Software Foundation, version 2.1
|
|
|
|
|
* exclusively.
|
|
|
|
|
*
|
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
|
*/
|
|
|
|
|
|
2020-06-04 09:33:47 -04:00
|
|
|
#ifndef _HAPROXY_SAMPLE_H
|
|
|
|
|
#define _HAPROXY_SAMPLE_H
|
2010-01-04 10:16:05 -05:00
|
|
|
|
2020-06-04 09:33:47 -04:00
|
|
|
#include <haproxy/api.h>
|
2020-06-09 03:07:15 -04:00
|
|
|
#include <haproxy/arg-t.h>
|
2020-06-04 09:33:47 -04:00
|
|
|
#include <haproxy/sample-t.h>
|
2020-06-04 12:46:44 -04:00
|
|
|
#include <haproxy/stick_table-t.h>
|
2010-01-04 10:16:05 -05:00
|
|
|
|
2020-06-04 09:33:47 -04:00
|
|
|
extern sample_cast_fct sample_casts[SMP_TYPES][SMP_TYPES];
|
|
|
|
|
extern const unsigned int fetch_cap[SMP_SRC_ENTRIES];
|
2014-02-07 06:14:54 -05:00
|
|
|
extern const char *smp_to_type[SMP_TYPES];
|
|
|
|
|
|
2020-02-14 10:50:14 -05:00
|
|
|
struct sample_expr *sample_parse_expr(char **str, int *idx, const char *file, int line, char **err, struct arg_list *al, char **endptr);
|
2023-06-05 08:30:45 -04:00
|
|
|
int sample_parse_expr_cnv(char **str, int *idx, char **endptr, char **err_msg, struct arg_list *al, const char *file, int line,
|
|
|
|
|
struct sample_expr *expr, const char *start);
|
2013-11-21 04:51:50 -05:00
|
|
|
struct sample_conv *find_sample_conv(const char *kw, int len);
|
2015-04-03 19:47:55 -04:00
|
|
|
struct sample *sample_process(struct proxy *px, struct session *sess,
|
|
|
|
|
struct stream *strm, unsigned int opt,
|
|
|
|
|
struct sample_expr *expr, struct sample *p);
|
2015-07-06 09:41:02 -04:00
|
|
|
struct sample *sample_fetch_as_type(struct proxy *px, struct session *sess,
|
2015-04-03 19:47:55 -04:00
|
|
|
struct stream *strm, unsigned int opt,
|
2015-07-06 09:41:02 -04:00
|
|
|
struct sample_expr *expr, int smp_type);
|
2021-10-06 09:29:00 -04:00
|
|
|
int sample_conv_var2smp(const struct var_desc *var, struct sample *smp, int type);
|
2021-10-06 09:19:05 -04:00
|
|
|
int sample_conv_var2smp_sint(const struct arg *arg, struct sample *smp);
|
2021-10-06 09:20:18 -04:00
|
|
|
int sample_conv_var2smp_str(const struct arg *arg, struct sample *smp);
|
2016-10-26 05:34:47 -04:00
|
|
|
void release_sample_expr(struct sample_expr *expr);
|
2012-04-27 15:37:17 -04:00
|
|
|
void sample_register_fetches(struct sample_fetch_kw_list *psl);
|
|
|
|
|
void sample_register_convs(struct sample_conv_kw_list *psl);
|
MEDIUM: samples: use new flags to describe compatibility between fetches and their usages
Samples fetches were relying on two flags SMP_CAP_REQ/SMP_CAP_RES to describe
whether they were compatible with requests rules or with response rules. This
was never reliable because we need a finer granularity (eg: an HTTP request
method needs to parse an HTTP request, and is available past this point).
Some fetches are also dependant on the context (eg: "hdr" uses request or
response depending where it's involved, causing some abiguity).
In order to solve this, we need to precisely indicate in fetches what they
use, and their users will have to compare with what they have.
So now we have a bunch of bits indicating where the sample is fetched in the
processing chain, with a few variants indicating for some of them if it is
permanent or volatile (eg: an HTTP status is stored into the transaction so
it is permanent, despite being caught in the response contents).
The fetches also have a second mask indicating their validity domain. This one
is computed from a conversion table at registration time, so there is no need
for doing it by hand. This validity domain consists in a bitmask with one bit
set for each usage point in the processing chain. Some provisions were made
for upcoming controls such as connection-based TCP rules which apply on top of
the connection layer but before instantiating the session.
Then everywhere a fetch is used, the bit for the control point is checked in
the fetch's validity domain, and it becomes possible to finely ensure that a
fetch will work or not.
Note that we need these two separate bitfields because some fetches are usable
both in request and response (eg: "hdr", "payload"). So the keyword will have
a "use" field made of a combination of several SMP_USE_* values, which will be
converted into a wider list of SMP_VAL_* flags.
The knowledge of permanent vs dynamic information has disappeared for now, as
it was never used. Later we'll probably reintroduce it differently when
dealing with variables. Its only use at the moment could have been to avoid
caching a dynamic rate measurement, but nothing is cached as of now.
2013-01-07 09:42:20 -05:00
|
|
|
const char *sample_src_names(unsigned int use);
|
2013-03-25 03:12:18 -04:00
|
|
|
const char *sample_ckp_names(unsigned int use);
|
2013-01-11 09:49:37 -05:00
|
|
|
struct sample_fetch *find_sample_fetch(const char *kw, int len);
|
2022-03-29 10:51:29 -04:00
|
|
|
void smp_dump_fetch_kw(void);
|
2022-03-29 10:59:49 -04:00
|
|
|
void smp_dump_conv_kw(void);
|
2014-12-08 08:49:19 -05:00
|
|
|
struct sample_fetch *sample_fetch_getnext(struct sample_fetch *current, int *idx);
|
2015-03-10 18:56:48 -04:00
|
|
|
struct sample_conv *sample_conv_getnext(struct sample_conv *current, int *idx);
|
2021-03-26 11:11:55 -04:00
|
|
|
int smp_resolve_args(struct proxy *p, char **err);
|
2019-10-30 11:57:28 -04:00
|
|
|
int smp_check_date_unit(struct arg *args, char **err);
|
2013-11-21 04:50:10 -05:00
|
|
|
int smp_expr_output_type(struct sample_expr *expr);
|
2013-12-14 08:55:04 -05:00
|
|
|
int c_none(struct sample *smp);
|
2023-06-06 13:03:45 -04:00
|
|
|
int c_pseudo(struct sample *smp);
|
2013-12-16 18:20:33 -05:00
|
|
|
int smp_dup(struct sample *smp);
|
2012-04-27 15:52:18 -04:00
|
|
|
|
2013-12-06 09:36:54 -05:00
|
|
|
/*
|
|
|
|
|
* This function just apply a cast on sample. It returns 0 if the cast is not
|
2018-11-15 15:14:56 -05:00
|
|
|
* available or if the cast fails, otherwise returns 1. It does not modify the
|
2013-12-06 09:36:54 -05:00
|
|
|
* input sample on failure.
|
|
|
|
|
*/
|
|
|
|
|
static inline
|
|
|
|
|
int sample_convert(struct sample *sample, int req_type)
|
|
|
|
|
{
|
2015-08-19 03:00:18 -04:00
|
|
|
if (!sample_casts[sample->data.type][req_type])
|
2013-12-06 09:36:54 -05:00
|
|
|
return 0;
|
2015-08-19 03:00:18 -04:00
|
|
|
if (sample_casts[sample->data.type][req_type] == c_none)
|
2013-12-14 08:55:04 -05:00
|
|
|
return 1;
|
2015-08-19 03:00:18 -04:00
|
|
|
return sample_casts[sample->data.type][req_type](sample);
|
2013-12-06 09:36:54 -05:00
|
|
|
}
|
|
|
|
|
|
2016-03-10 10:15:46 -05:00
|
|
|
static inline
|
|
|
|
|
struct sample *smp_set_owner(struct sample *smp, struct proxy *px,
|
|
|
|
|
struct session *sess, struct stream *strm, int opt)
|
|
|
|
|
{
|
|
|
|
|
smp->px = px;
|
|
|
|
|
smp->sess = sess;
|
|
|
|
|
smp->strm = strm;
|
|
|
|
|
smp->opt = opt;
|
|
|
|
|
return smp;
|
|
|
|
|
}
|
|
|
|
|
|
2016-08-09 05:37:54 -04:00
|
|
|
|
|
|
|
|
/* Returns 1 if a sample may be safely used. It performs a few checks on the
|
|
|
|
|
* string length versus size, same for the binary version, and ensures that
|
|
|
|
|
* strings are properly terminated by a zero. If this last point is not granted
|
|
|
|
|
* but the string is not const, then the \0 is appended. Otherwise it returns 0,
|
|
|
|
|
* meaning the caller may need to call smp_dup() before going further.
|
|
|
|
|
*/
|
|
|
|
|
static inline
|
|
|
|
|
int smp_is_safe(struct sample *smp)
|
|
|
|
|
{
|
|
|
|
|
switch (smp->data.type) {
|
2017-07-24 10:07:12 -04:00
|
|
|
case SMP_T_METH:
|
|
|
|
|
if (smp->data.u.meth.meth != HTTP_METH_OTHER)
|
|
|
|
|
return 1;
|
2022-11-14 00:59:59 -05:00
|
|
|
__fallthrough;
|
2017-07-24 10:07:12 -04:00
|
|
|
|
2016-08-09 05:37:54 -04:00
|
|
|
case SMP_T_STR:
|
2021-02-18 04:22:48 -05:00
|
|
|
if (!smp->data.u.str.size || smp->data.u.str.data >= smp->data.u.str.size)
|
2016-08-09 05:37:54 -04:00
|
|
|
return 0;
|
|
|
|
|
|
2018-07-13 04:54:26 -04:00
|
|
|
if (smp->data.u.str.area[smp->data.u.str.data] == 0)
|
2016-08-09 05:37:54 -04:00
|
|
|
return 1;
|
|
|
|
|
|
2021-02-18 04:22:48 -05:00
|
|
|
if (smp->flags & SMP_F_CONST)
|
2016-08-09 05:37:54 -04:00
|
|
|
return 0;
|
|
|
|
|
|
2018-07-13 04:54:26 -04:00
|
|
|
smp->data.u.str.area[smp->data.u.str.data] = 0;
|
2016-08-09 05:37:54 -04:00
|
|
|
return 1;
|
|
|
|
|
|
|
|
|
|
case SMP_T_BIN:
|
2018-08-21 23:07:14 -04:00
|
|
|
return !smp->data.u.str.size || smp->data.u.str.data <= smp->data.u.str.size;
|
2016-08-09 05:37:54 -04:00
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* checks that a sample may freely be used, or duplicates it to normalize it.
|
|
|
|
|
* Returns 1 on success, 0 if the sample must not be used. The function also
|
|
|
|
|
* checks for NULL to simplify the calling code.
|
|
|
|
|
*/
|
|
|
|
|
static inline
|
|
|
|
|
int smp_make_safe(struct sample *smp)
|
|
|
|
|
{
|
|
|
|
|
return smp && (smp_is_safe(smp) || smp_dup(smp));
|
|
|
|
|
}
|
|
|
|
|
|
2016-08-09 05:49:20 -04:00
|
|
|
/* Returns 1 if a sample may be safely modified in place. It performs a few
|
|
|
|
|
* checks on the string length versus size, same for the binary version, and
|
|
|
|
|
* ensures that strings are properly terminated by a zero, and of course that
|
|
|
|
|
* the size is allocate and that the SMP_F_CONST flag is not set. If only the
|
|
|
|
|
* trailing zero is missing, it is appended. Otherwise it returns 0, meaning
|
|
|
|
|
* the caller may need to call smp_dup() before going further.
|
|
|
|
|
*/
|
|
|
|
|
static inline
|
|
|
|
|
int smp_is_rw(struct sample *smp)
|
|
|
|
|
{
|
|
|
|
|
if (smp->flags & SMP_F_CONST)
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
switch (smp->data.type) {
|
2017-07-24 10:07:12 -04:00
|
|
|
case SMP_T_METH:
|
|
|
|
|
if (smp->data.u.meth.meth != HTTP_METH_OTHER)
|
|
|
|
|
return 1;
|
2022-11-14 00:59:59 -05:00
|
|
|
__fallthrough;
|
2017-07-24 10:07:12 -04:00
|
|
|
|
2016-08-09 05:49:20 -04:00
|
|
|
case SMP_T_STR:
|
|
|
|
|
if (!smp->data.u.str.size ||
|
2018-07-13 04:54:26 -04:00
|
|
|
smp->data.u.str.data >= smp->data.u.str.size)
|
2016-08-09 05:49:20 -04:00
|
|
|
return 0;
|
|
|
|
|
|
2018-07-13 04:54:26 -04:00
|
|
|
if (smp->data.u.str.area[smp->data.u.str.data] != 0)
|
|
|
|
|
smp->data.u.str.area[smp->data.u.str.data] = 0;
|
2016-08-09 05:49:20 -04:00
|
|
|
return 1;
|
|
|
|
|
|
|
|
|
|
case SMP_T_BIN:
|
|
|
|
|
return smp->data.u.str.size &&
|
2018-07-13 04:54:26 -04:00
|
|
|
smp->data.u.str.data <= smp->data.u.str.size;
|
2016-08-09 05:49:20 -04:00
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* checks that a sample may freely be modified, or duplicates it to normalize
|
|
|
|
|
* it and make it R/W. Returns 1 on success, 0 if the sample must not be used.
|
|
|
|
|
* The function also checks for NULL to simplify the calling code.
|
|
|
|
|
*/
|
|
|
|
|
static inline
|
|
|
|
|
int smp_make_rw(struct sample *smp)
|
|
|
|
|
{
|
|
|
|
|
return smp && (smp_is_rw(smp) || smp_dup(smp));
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-04 09:33:47 -04:00
|
|
|
#endif /* _HAPROXY_SAMPLE_H */
|