1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
|
/*
* 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);
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);
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.len < 0) ||
(smp->data.u.str.size && smp->data.u.str.len >= smp->data.u.str.size))
return 0;
if (smp->data.u.str.str[smp->data.u.str.len] == 0)
return 1;
if (!smp->data.u.str.size || (smp->flags & SMP_F_CONST))
return 0;
smp->data.u.str.str[smp->data.u.str.len] = 0;
return 1;
case SMP_T_BIN:
return (smp->data.u.str.len >= 0) &&
(!smp->data.u.str.size || smp->data.u.str.len <= 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.len < 0 ||
smp->data.u.str.len >= smp->data.u.str.size)
return 0;
if (smp->data.u.str.str[smp->data.u.str.len] != 0)
smp->data.u.str.str[smp->data.u.str.len] = 0;
return 1;
case SMP_T_BIN:
return smp->data.u.str.size &&
smp->data.u.str.len >= 0 &&
smp->data.u.str.len <= 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 */
|