haproxy/include/proto/sample.h

177 lines
5.8 KiB
C
Raw Normal View History

/*
* include/proto/sample.h
* Functions for samples management.
*
* Copyright (C) 2009-2010 EXCELIANCE, Emeric Brun <ebrun@exceliance.fr>
* Copyright (C) 2012 Willy Tarreau <w@1wt.eu>
*
* 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
*/
#ifndef _PROTO_SAMPLE_H
#define _PROTO_SAMPLE_H
#include <types/sample.h>
#include <types/stick_table.h>
extern const char *smp_to_type[SMP_TYPES];
struct sample_expr *sample_parse_expr(char **str, int *idx, const char *file, int line, char **err, struct arg_list *al);
struct sample_conv *find_sample_conv(const char *kw, int len);
struct sample *sample_process(struct proxy *px, struct session *sess,
struct stream *strm, unsigned int opt,
struct sample_expr *expr, struct sample *p);
struct sample *sample_fetch_as_type(struct proxy *px, struct session *sess,
struct stream *strm, unsigned int opt,
struct sample_expr *expr, int smp_type);
void release_sample_expr(struct sample_expr *expr);
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);
const char *sample_ckp_names(unsigned int use);
struct sample_fetch *find_sample_fetch(const char *kw, int len);
struct sample_fetch *sample_fetch_getnext(struct sample_fetch *current, int *idx);
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);
int smp_expr_output_type(struct sample_expr *expr);
int c_none(struct sample *smp);
int smp_dup(struct sample *smp);
/*
* 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)
{
if (!sample_casts[sample->data.type][req_type])
return 0;
if (sample_casts[sample->data.type][req_type] == c_none)
return 1;
return sample_casts[sample->data.type][req_type](sample);
}
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;
}
/* 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) {
case SMP_T_METH:
if (smp->data.u.meth.meth != HTTP_METH_OTHER)
return 1;
/* Fall through */
case SMP_T_STR:
if ((smp->data.u.str.data < 0) ||
(smp->data.u.str.size && smp->data.u.str.data >= smp->data.u.str.size))
return 0;
if (smp->data.u.str.area[smp->data.u.str.data] == 0)
return 1;
if (!smp->data.u.str.size || (smp->flags & SMP_F_CONST))
return 0;
smp->data.u.str.area[smp->data.u.str.data] = 0;
return 1;
case SMP_T_BIN:
return (smp->data.u.str.data >= 0) &&
(!smp->data.u.str.size || smp->data.u.str.data <= smp->data.u.str.size);
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));
}
/* 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) {
case SMP_T_METH:
if (smp->data.u.meth.meth != HTTP_METH_OTHER)
return 1;
/* Fall through */
case SMP_T_STR:
if (!smp->data.u.str.size ||
smp->data.u.str.data < 0 ||
smp->data.u.str.data >= smp->data.u.str.size)
return 0;
if (smp->data.u.str.area[smp->data.u.str.data] != 0)
smp->data.u.str.area[smp->data.u.str.data] = 0;
return 1;
case SMP_T_BIN:
return smp->data.u.str.size &&
smp->data.u.str.data >= 0 &&
smp->data.u.str.data <= smp->data.u.str.size;
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));
}
#endif /* _PROTO_SAMPLE_H */