2010-01-04 15:16:05 +00:00
|
|
|
/*
|
2012-04-27 19:52:18 +00:00
|
|
|
* include/proto/sample.h
|
|
|
|
* Functions for samples management.
|
2010-01-04 15:16:05 +00:00
|
|
|
*
|
|
|
|
* Copyright (C) 2009-2010 EXCELIANCE, Emeric Brun <ebrun@exceliance.fr>
|
2012-04-27 19:52:18 +00:00
|
|
|
* Copyright (C) 2012 Willy Tarreau <w@1wt.eu>
|
2010-01-04 15:16:05 +00: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
|
|
|
|
*/
|
|
|
|
|
2012-04-27 19:52:18 +00:00
|
|
|
#ifndef _PROTO_SAMPLE_H
|
|
|
|
#define _PROTO_SAMPLE_H
|
2010-01-04 15:16:05 +00:00
|
|
|
|
2012-04-27 19:52:18 +00:00
|
|
|
#include <types/sample.h>
|
2010-01-04 15:16:05 +00:00
|
|
|
#include <types/stick_table.h>
|
|
|
|
|
2014-02-07 11:14:54 +00:00
|
|
|
extern const char *smp_to_type[SMP_TYPES];
|
|
|
|
|
2014-02-11 13:00:19 +00:00
|
|
|
struct sample_expr *sample_parse_expr(char **str, int *idx, const char *file, int line, char **err, struct arg_list *al);
|
2013-11-21 09:51:50 +00:00
|
|
|
struct sample_conv *find_sample_conv(const char *kw, int len);
|
2015-04-03 23:47:55 +00: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 13:41:02 +00:00
|
|
|
struct sample *sample_fetch_as_type(struct proxy *px, struct session *sess,
|
2015-04-03 23:47:55 +00:00
|
|
|
struct stream *strm, unsigned int opt,
|
2015-07-06 13:41:02 +00:00
|
|
|
struct sample_expr *expr, int smp_type);
|
2016-10-26 09:34:47 +00:00
|
|
|
void release_sample_expr(struct sample_expr *expr);
|
2012-04-27 19:37:17 +00: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 14:42:20 +00:00
|
|
|
const char *sample_src_names(unsigned int use);
|
2013-03-25 07:12:18 +00:00
|
|
|
const char *sample_ckp_names(unsigned int use);
|
2013-01-11 14:49:37 +00:00
|
|
|
struct sample_fetch *find_sample_fetch(const char *kw, int len);
|
2014-12-08 13:49:19 +00:00
|
|
|
struct sample_fetch *sample_fetch_getnext(struct sample_fetch *current, int *idx);
|
2015-03-10 22:56:48 +00:00
|
|
|
struct sample_conv *sample_conv_getnext(struct sample_conv *current, int *idx);
|
MAJOR: sample: maintain a per-proxy list of the fetch args to resolve
While ACL args were resolved after all the config was parsed, it was not the
case with sample fetch args because they're almost everywhere now.
The issue is that ACLs now solely rely on sample fetches, so their args
resolving doesn't work anymore. And many fetches involving a server, a
proxy or a userlist don't work at all.
The real issue is that at the bottom layers we have no information about
proxies, line numbers, even ACLs in order to report understandable errors,
and that at the top layers we have no visibility over the locations where
fetches are referenced (think log node).
After failing multiple unsatisfying solutions attempts, we now have a new
concept of args list. The principle is that every proxy has a list head
which contains a number of indications such as the config keyword, the
context where it's used, the file and line number, etc... and a list of
arguments. This list head is of the same type as the elements, so it
serves as a template for adding new elements. This way, it is filled from
top to bottom by the callers with the information they have (eg: line
numbers, ACL name, ...) and the lower layers just have to duplicate it and
add an element when they face an argument they cannot resolve yet.
Then at the end of the configuration parsing, a loop passes over each
proxy's list and resolves all the args in sequence. And this way there is
all necessary information to report verbose errors.
The first immediate benefit is that for the first time we got very precise
location of issues (arg number in a keyword in its context, ...). Second,
in order to do this we had to parse log-format and unique-id-format a bit
earlier, so that was a great opportunity for doing so when the directives
are encountered (unless it's a default section). This way, the recorded
line numbers for these args are the ones of the place where the log format
is declared, not the end of the file.
Userlists report slightly more information now. They're the only remaining
ones in the ACL resolving function.
2013-04-02 14:34:32 +00:00
|
|
|
int smp_resolve_args(struct proxy *p);
|
2013-11-21 09:50:10 +00:00
|
|
|
int smp_expr_output_type(struct sample_expr *expr);
|
2013-12-14 13:55:04 +00:00
|
|
|
int c_none(struct sample *smp);
|
2013-12-16 23:20:33 +00:00
|
|
|
int smp_dup(struct sample *smp);
|
2012-04-27 19:52:18 +00:00
|
|
|
|
2013-12-06 14:36:54 +00:00
|
|
|
/*
|
|
|
|
* This function just apply a cast on sample. It returns 0 if the cast is not
|
|
|
|
* avalaible or if the cast fails, otherwise returns 1. It does not modify the
|
|
|
|
* input sample on failure.
|
|
|
|
*/
|
|
|
|
static inline
|
|
|
|
int sample_convert(struct sample *sample, int req_type)
|
|
|
|
{
|
2015-08-19 07:00:18 +00:00
|
|
|
if (!sample_casts[sample->data.type][req_type])
|
2013-12-06 14:36:54 +00:00
|
|
|
return 0;
|
2015-08-19 07:00:18 +00:00
|
|
|
if (sample_casts[sample->data.type][req_type] == c_none)
|
2013-12-14 13:55:04 +00:00
|
|
|
return 1;
|
2015-08-19 07:00:18 +00:00
|
|
|
return sample_casts[sample->data.type][req_type](sample);
|
2013-12-06 14:36:54 +00:00
|
|
|
}
|
|
|
|
|
2016-03-10 15:15:46 +00: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 09:37:54 +00: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 14:07:12 +00:00
|
|
|
case SMP_T_METH:
|
|
|
|
if (smp->data.u.meth.meth != HTTP_METH_OTHER)
|
|
|
|
return 1;
|
|
|
|
/* Fall through */
|
|
|
|
|
2016-08-09 09:37:54 +00:00
|
|
|
case SMP_T_STR:
|
2018-08-22 03:07:14 +00:00
|
|
|
if (smp->data.u.str.size && smp->data.u.str.data >= smp->data.u.str.size)
|
2016-08-09 09:37:54 +00:00
|
|
|
return 0;
|
|
|
|
|
2018-07-13 08:54:26 +00:00
|
|
|
if (smp->data.u.str.area[smp->data.u.str.data] == 0)
|
2016-08-09 09:37:54 +00:00
|
|
|
return 1;
|
|
|
|
|
|
|
|
if (!smp->data.u.str.size || (smp->flags & SMP_F_CONST))
|
|
|
|
return 0;
|
|
|
|
|
2018-07-13 08:54:26 +00:00
|
|
|
smp->data.u.str.area[smp->data.u.str.data] = 0;
|
2016-08-09 09:37:54 +00:00
|
|
|
return 1;
|
|
|
|
|
|
|
|
case SMP_T_BIN:
|
2018-08-22 03:07:14 +00:00
|
|
|
return !smp->data.u.str.size || smp->data.u.str.data <= smp->data.u.str.size;
|
2016-08-09 09:37:54 +00: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 09:49:20 +00: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 14:07:12 +00:00
|
|
|
case SMP_T_METH:
|
|
|
|
if (smp->data.u.meth.meth != HTTP_METH_OTHER)
|
|
|
|
return 1;
|
|
|
|
/* Fall through */
|
|
|
|
|
2016-08-09 09:49:20 +00:00
|
|
|
case SMP_T_STR:
|
|
|
|
if (!smp->data.u.str.size ||
|
2018-07-13 08:54:26 +00:00
|
|
|
smp->data.u.str.data >= smp->data.u.str.size)
|
2016-08-09 09:49:20 +00:00
|
|
|
return 0;
|
|
|
|
|
2018-07-13 08:54:26 +00: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 09:49:20 +00:00
|
|
|
return 1;
|
|
|
|
|
|
|
|
case SMP_T_BIN:
|
|
|
|
return smp->data.u.str.size &&
|
2018-07-13 08:54:26 +00:00
|
|
|
smp->data.u.str.data <= smp->data.u.str.size;
|
2016-08-09 09:49:20 +00: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));
|
|
|
|
}
|
|
|
|
|
2012-04-27 19:52:18 +00:00
|
|
|
#endif /* _PROTO_SAMPLE_H */
|