blob: 670205f03cf01b16d63f5679f8c48857b45161ca [file] [log] [blame]
Emeric Brun107ca302010-01-04 16:16:05 +01001/*
Willy Tarreaue6ce10b2020-06-04 15:33:47 +02002 * include/haproxy/sample.h
Willy Tarreaucd3b0942012-04-27 21:52:18 +02003 * Functions for samples management.
Emeric Brun107ca302010-01-04 16:16:05 +01004 *
5 * Copyright (C) 2009-2010 EXCELIANCE, Emeric Brun <ebrun@exceliance.fr>
Willy Tarreaucd3b0942012-04-27 21:52:18 +02006 * Copyright (C) 2012 Willy Tarreau <w@1wt.eu>
Emeric Brun107ca302010-01-04 16:16:05 +01007 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation, version 2.1
11 * exclusively.
12 *
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22
Willy Tarreaue6ce10b2020-06-04 15:33:47 +020023#ifndef _HAPROXY_SAMPLE_H
24#define _HAPROXY_SAMPLE_H
Emeric Brun107ca302010-01-04 16:16:05 +010025
Willy Tarreaue6ce10b2020-06-04 15:33:47 +020026#include <haproxy/api.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020027#include <haproxy/arg-t.h>
Willy Tarreaue6ce10b2020-06-04 15:33:47 +020028#include <haproxy/sample-t.h>
Willy Tarreau872f2ea2020-06-04 18:46:44 +020029#include <haproxy/stick_table-t.h>
Emeric Brun107ca302010-01-04 16:16:05 +010030
Willy Tarreaue6ce10b2020-06-04 15:33:47 +020031extern sample_cast_fct sample_casts[SMP_TYPES][SMP_TYPES];
32extern const unsigned int fetch_cap[SMP_SRC_ENTRIES];
Willy Tarreau1cf8f082014-02-07 12:14:54 +010033extern const char *smp_to_type[SMP_TYPES];
34
Willy Tarreaue3b57bf2020-02-14 16:50:14 +010035struct sample_expr *sample_parse_expr(char **str, int *idx, const char *file, int line, char **err, struct arg_list *al, char **endptr);
Thierry FOURNIER20f49962013-11-21 10:51:50 +010036struct sample_conv *find_sample_conv(const char *kw, int len);
Willy Tarreau192252e2015-04-04 01:47:55 +020037struct sample *sample_process(struct proxy *px, struct session *sess,
38 struct stream *strm, unsigned int opt,
39 struct sample_expr *expr, struct sample *p);
Adis Nezirovic79beb242015-07-06 15:41:02 +020040struct sample *sample_fetch_as_type(struct proxy *px, struct session *sess,
Willy Tarreau192252e2015-04-04 01:47:55 +020041 struct stream *strm, unsigned int opt,
Adis Nezirovic79beb242015-07-06 15:41:02 +020042 struct sample_expr *expr, int smp_type);
Willy Tarreauea573502021-10-06 15:29:00 +020043int sample_conv_var2smp(const struct var_desc *var, struct sample *smp, int type);
Willy Tarreau9cfb71f2021-10-06 15:19:05 +020044int sample_conv_var2smp_sint(const struct arg *arg, struct sample *smp);
Willy Tarreau05cd2e62021-10-06 15:20:18 +020045int sample_conv_var2smp_str(const struct arg *arg, struct sample *smp);
Christopher Faulet476e5d02016-10-26 11:34:47 +020046void release_sample_expr(struct sample_expr *expr);
Willy Tarreau12785782012-04-27 21:37:17 +020047void sample_register_fetches(struct sample_fetch_kw_list *psl);
48void sample_register_convs(struct sample_conv_kw_list *psl);
Willy Tarreau80aca902013-01-07 15:42:20 +010049const char *sample_src_names(unsigned int use);
Willy Tarreaua91d0a52013-03-25 08:12:18 +010050const char *sample_ckp_names(unsigned int use);
Willy Tarreau8ed669b2013-01-11 15:49:37 +010051struct sample_fetch *find_sample_fetch(const char *kw, int len);
Thierry FOURNIER4d9a1d12014-12-08 14:49:19 +010052struct sample_fetch *sample_fetch_getnext(struct sample_fetch *current, int *idx);
Thierry FOURNIER8fd13762015-03-10 23:56:48 +010053struct sample_conv *sample_conv_getnext(struct sample_conv *current, int *idx);
Willy Tarreau77e6a4e2021-03-26 16:11:55 +010054int smp_resolve_args(struct proxy *p, char **err);
Damien Claisseae6f1252019-10-30 15:57:28 +000055int smp_check_date_unit(struct arg *args, char **err);
Thierry FOURNIER348971e2013-11-21 10:50:10 +010056int smp_expr_output_type(struct sample_expr *expr);
Thierry FOURNIER0e9af552013-12-14 14:55:04 +010057int c_none(struct sample *smp);
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010058int smp_dup(struct sample *smp);
Willy Tarreaucd3b0942012-04-27 21:52:18 +020059
Thierry FOURNIERe3ded592013-12-06 15:36:54 +010060/*
61 * This function just apply a cast on sample. It returns 0 if the cast is not
Joseph Herlant757f5ad2018-11-15 12:14:56 -080062 * available or if the cast fails, otherwise returns 1. It does not modify the
Thierry FOURNIERe3ded592013-12-06 15:36:54 +010063 * input sample on failure.
64 */
65static inline
66int sample_convert(struct sample *sample, int req_type)
67{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020068 if (!sample_casts[sample->data.type][req_type])
Thierry FOURNIERe3ded592013-12-06 15:36:54 +010069 return 0;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020070 if (sample_casts[sample->data.type][req_type] == c_none)
Thierry FOURNIER0e9af552013-12-14 14:55:04 +010071 return 1;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020072 return sample_casts[sample->data.type][req_type](sample);
Thierry FOURNIERe3ded592013-12-06 15:36:54 +010073}
74
Willy Tarreau1777ea62016-03-10 16:15:46 +010075static inline
76struct sample *smp_set_owner(struct sample *smp, struct proxy *px,
77 struct session *sess, struct stream *strm, int opt)
78{
79 smp->px = px;
80 smp->sess = sess;
81 smp->strm = strm;
82 smp->opt = opt;
83 return smp;
84}
85
Willy Tarreau2c594792016-08-09 11:37:54 +020086
87/* Returns 1 if a sample may be safely used. It performs a few checks on the
88 * string length versus size, same for the binary version, and ensures that
89 * strings are properly terminated by a zero. If this last point is not granted
90 * but the string is not const, then the \0 is appended. Otherwise it returns 0,
91 * meaning the caller may need to call smp_dup() before going further.
92 */
93static inline
94int smp_is_safe(struct sample *smp)
95{
96 switch (smp->data.type) {
Christopher Faulet5db105e2017-07-24 16:07:12 +020097 case SMP_T_METH:
98 if (smp->data.u.meth.meth != HTTP_METH_OTHER)
99 return 1;
100 /* Fall through */
101
Willy Tarreau2c594792016-08-09 11:37:54 +0200102 case SMP_T_STR:
Christopher Faulet8dd40fb2021-02-18 10:22:48 +0100103 if (!smp->data.u.str.size || smp->data.u.str.data >= smp->data.u.str.size)
Willy Tarreau2c594792016-08-09 11:37:54 +0200104 return 0;
105
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200106 if (smp->data.u.str.area[smp->data.u.str.data] == 0)
Willy Tarreau2c594792016-08-09 11:37:54 +0200107 return 1;
108
Christopher Faulet8dd40fb2021-02-18 10:22:48 +0100109 if (smp->flags & SMP_F_CONST)
Willy Tarreau2c594792016-08-09 11:37:54 +0200110 return 0;
111
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200112 smp->data.u.str.area[smp->data.u.str.data] = 0;
Willy Tarreau2c594792016-08-09 11:37:54 +0200113 return 1;
114
115 case SMP_T_BIN:
Willy Tarreaub5092322018-08-22 05:07:14 +0200116 return !smp->data.u.str.size || smp->data.u.str.data <= smp->data.u.str.size;
Willy Tarreau2c594792016-08-09 11:37:54 +0200117
118 default:
119 return 1;
120 }
121}
122
123/* checks that a sample may freely be used, or duplicates it to normalize it.
124 * Returns 1 on success, 0 if the sample must not be used. The function also
125 * checks for NULL to simplify the calling code.
126 */
127static inline
128int smp_make_safe(struct sample *smp)
129{
130 return smp && (smp_is_safe(smp) || smp_dup(smp));
131}
132
Willy Tarreau77128f52016-08-09 11:49:20 +0200133/* Returns 1 if a sample may be safely modified in place. It performs a few
134 * checks on the string length versus size, same for the binary version, and
135 * ensures that strings are properly terminated by a zero, and of course that
136 * the size is allocate and that the SMP_F_CONST flag is not set. If only the
137 * trailing zero is missing, it is appended. Otherwise it returns 0, meaning
138 * the caller may need to call smp_dup() before going further.
139 */
140static inline
141int smp_is_rw(struct sample *smp)
142{
143 if (smp->flags & SMP_F_CONST)
144 return 0;
145
146 switch (smp->data.type) {
Christopher Faulet5db105e2017-07-24 16:07:12 +0200147 case SMP_T_METH:
148 if (smp->data.u.meth.meth != HTTP_METH_OTHER)
149 return 1;
150 /* Fall through */
151
Willy Tarreau77128f52016-08-09 11:49:20 +0200152 case SMP_T_STR:
153 if (!smp->data.u.str.size ||
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200154 smp->data.u.str.data >= smp->data.u.str.size)
Willy Tarreau77128f52016-08-09 11:49:20 +0200155 return 0;
156
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200157 if (smp->data.u.str.area[smp->data.u.str.data] != 0)
158 smp->data.u.str.area[smp->data.u.str.data] = 0;
Willy Tarreau77128f52016-08-09 11:49:20 +0200159 return 1;
160
161 case SMP_T_BIN:
162 return smp->data.u.str.size &&
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200163 smp->data.u.str.data <= smp->data.u.str.size;
Willy Tarreau77128f52016-08-09 11:49:20 +0200164
165 default:
166 return 1;
167 }
168}
169
170/* checks that a sample may freely be modified, or duplicates it to normalize
171 * it and make it R/W. Returns 1 on success, 0 if the sample must not be used.
172 * The function also checks for NULL to simplify the calling code.
173 */
174static inline
175int smp_make_rw(struct sample *smp)
176{
177 return smp && (smp_is_rw(smp) || smp_dup(smp));
178}
179
Willy Tarreaue6ce10b2020-06-04 15:33:47 +0200180#endif /* _HAPROXY_SAMPLE_H */