blob: 94226d2d5e049648a62261ee0ed19f83b1f68041 [file] [log] [blame]
Emeric Brun107ca302010-01-04 16:16:05 +01001/*
Willy Tarreaucd3b0942012-04-27 21:52:18 +02002 * include/proto/sample.h
3 * 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 Tarreaucd3b0942012-04-27 21:52:18 +020023#ifndef _PROTO_SAMPLE_H
24#define _PROTO_SAMPLE_H
Emeric Brun107ca302010-01-04 16:16:05 +010025
Willy Tarreaucd3b0942012-04-27 21:52:18 +020026#include <types/sample.h>
Emeric Brun107ca302010-01-04 16:16:05 +010027#include <types/stick_table.h>
28
Willy Tarreau1cf8f082014-02-07 12:14:54 +010029extern const char *smp_to_type[SMP_TYPES];
30
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +010031struct sample_expr *sample_parse_expr(char **str, int *idx, const char *file, int line, char **err, struct arg_list *al);
Thierry FOURNIER20f49962013-11-21 10:51:50 +010032struct sample_conv *find_sample_conv(const char *kw, int len);
Willy Tarreau192252e2015-04-04 01:47:55 +020033struct sample *sample_process(struct proxy *px, struct session *sess,
34 struct stream *strm, unsigned int opt,
35 struct sample_expr *expr, struct sample *p);
Adis Nezirovic79beb242015-07-06 15:41:02 +020036struct sample *sample_fetch_as_type(struct proxy *px, struct session *sess,
Willy Tarreau192252e2015-04-04 01:47:55 +020037 struct stream *strm, unsigned int opt,
Adis Nezirovic79beb242015-07-06 15:41:02 +020038 struct sample_expr *expr, int smp_type);
Christopher Faulet476e5d02016-10-26 11:34:47 +020039void release_sample_expr(struct sample_expr *expr);
Willy Tarreau12785782012-04-27 21:37:17 +020040void sample_register_fetches(struct sample_fetch_kw_list *psl);
41void sample_register_convs(struct sample_conv_kw_list *psl);
Willy Tarreau80aca902013-01-07 15:42:20 +010042const char *sample_src_names(unsigned int use);
Willy Tarreaua91d0a52013-03-25 08:12:18 +010043const char *sample_ckp_names(unsigned int use);
Willy Tarreau8ed669b2013-01-11 15:49:37 +010044struct sample_fetch *find_sample_fetch(const char *kw, int len);
Thierry FOURNIER4d9a1d12014-12-08 14:49:19 +010045struct sample_fetch *sample_fetch_getnext(struct sample_fetch *current, int *idx);
Thierry FOURNIER8fd13762015-03-10 23:56:48 +010046struct sample_conv *sample_conv_getnext(struct sample_conv *current, int *idx);
Willy Tarreaua4312fa2013-04-02 16:34:32 +020047int smp_resolve_args(struct proxy *p);
Thierry FOURNIER348971e2013-11-21 10:50:10 +010048int smp_expr_output_type(struct sample_expr *expr);
Thierry FOURNIER0e9af552013-12-14 14:55:04 +010049int c_none(struct sample *smp);
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010050int smp_dup(struct sample *smp);
Willy Tarreaucd3b0942012-04-27 21:52:18 +020051
Thierry FOURNIERe3ded592013-12-06 15:36:54 +010052/*
53 * This function just apply a cast on sample. It returns 0 if the cast is not
54 * avalaible or if the cast fails, otherwise returns 1. It does not modify the
55 * input sample on failure.
56 */
57static inline
58int sample_convert(struct sample *sample, int req_type)
59{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020060 if (!sample_casts[sample->data.type][req_type])
Thierry FOURNIERe3ded592013-12-06 15:36:54 +010061 return 0;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020062 if (sample_casts[sample->data.type][req_type] == c_none)
Thierry FOURNIER0e9af552013-12-14 14:55:04 +010063 return 1;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020064 return sample_casts[sample->data.type][req_type](sample);
Thierry FOURNIERe3ded592013-12-06 15:36:54 +010065}
66
Willy Tarreau1777ea62016-03-10 16:15:46 +010067static inline
68struct sample *smp_set_owner(struct sample *smp, struct proxy *px,
69 struct session *sess, struct stream *strm, int opt)
70{
71 smp->px = px;
72 smp->sess = sess;
73 smp->strm = strm;
74 smp->opt = opt;
75 return smp;
76}
77
Willy Tarreau2c594792016-08-09 11:37:54 +020078
79/* Returns 1 if a sample may be safely used. It performs a few checks on the
80 * string length versus size, same for the binary version, and ensures that
81 * strings are properly terminated by a zero. If this last point is not granted
82 * but the string is not const, then the \0 is appended. Otherwise it returns 0,
83 * meaning the caller may need to call smp_dup() before going further.
84 */
85static inline
86int smp_is_safe(struct sample *smp)
87{
88 switch (smp->data.type) {
Christopher Faulet5db105e2017-07-24 16:07:12 +020089 case SMP_T_METH:
90 if (smp->data.u.meth.meth != HTTP_METH_OTHER)
91 return 1;
92 /* Fall through */
93
Willy Tarreau2c594792016-08-09 11:37:54 +020094 case SMP_T_STR:
95 if ((smp->data.u.str.len < 0) ||
96 (smp->data.u.str.size && smp->data.u.str.len >= smp->data.u.str.size))
97 return 0;
98
99 if (smp->data.u.str.str[smp->data.u.str.len] == 0)
100 return 1;
101
102 if (!smp->data.u.str.size || (smp->flags & SMP_F_CONST))
103 return 0;
104
105 smp->data.u.str.str[smp->data.u.str.len] = 0;
106 return 1;
107
108 case SMP_T_BIN:
109 return (smp->data.u.str.len >= 0) &&
110 (!smp->data.u.str.size || smp->data.u.str.len <= smp->data.u.str.size);
111
112 default:
113 return 1;
114 }
115}
116
117/* checks that a sample may freely be used, or duplicates it to normalize it.
118 * Returns 1 on success, 0 if the sample must not be used. The function also
119 * checks for NULL to simplify the calling code.
120 */
121static inline
122int smp_make_safe(struct sample *smp)
123{
124 return smp && (smp_is_safe(smp) || smp_dup(smp));
125}
126
Willy Tarreau77128f52016-08-09 11:49:20 +0200127/* Returns 1 if a sample may be safely modified in place. It performs a few
128 * checks on the string length versus size, same for the binary version, and
129 * ensures that strings are properly terminated by a zero, and of course that
130 * the size is allocate and that the SMP_F_CONST flag is not set. If only the
131 * trailing zero is missing, it is appended. Otherwise it returns 0, meaning
132 * the caller may need to call smp_dup() before going further.
133 */
134static inline
135int smp_is_rw(struct sample *smp)
136{
137 if (smp->flags & SMP_F_CONST)
138 return 0;
139
140 switch (smp->data.type) {
Christopher Faulet5db105e2017-07-24 16:07:12 +0200141 case SMP_T_METH:
142 if (smp->data.u.meth.meth != HTTP_METH_OTHER)
143 return 1;
144 /* Fall through */
145
Willy Tarreau77128f52016-08-09 11:49:20 +0200146 case SMP_T_STR:
147 if (!smp->data.u.str.size ||
148 smp->data.u.str.len < 0 ||
149 smp->data.u.str.len >= smp->data.u.str.size)
150 return 0;
151
152 if (smp->data.u.str.str[smp->data.u.str.len] != 0)
153 smp->data.u.str.str[smp->data.u.str.len] = 0;
154 return 1;
155
156 case SMP_T_BIN:
157 return smp->data.u.str.size &&
158 smp->data.u.str.len >= 0 &&
159 smp->data.u.str.len <= smp->data.u.str.size;
160
161 default:
162 return 1;
163 }
164}
165
166/* checks that a sample may freely be modified, or duplicates it to normalize
167 * it and make it R/W. Returns 1 on success, 0 if the sample must not be used.
168 * The function also checks for NULL to simplify the calling code.
169 */
170static inline
171int smp_make_rw(struct sample *smp)
172{
173 return smp && (smp_is_rw(smp) || smp_dup(smp));
174}
175
Willy Tarreaucd3b0942012-04-27 21:52:18 +0200176#endif /* _PROTO_SAMPLE_H */