1 /*
2  * include/haproxy/sample.h
3  * Functions for samples management.
4  *
5  * Copyright (C) 2009-2010 EXCELIANCE, Emeric Brun <ebrun@exceliance.fr>
6  * Copyright (C) 2012 Willy Tarreau <w@1wt.eu>
7  *
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 
23 #ifndef _HAPROXY_SAMPLE_H
24 #define _HAPROXY_SAMPLE_H
25 
26 #include <haproxy/api.h>
27 #include <haproxy/arg-t.h>
28 #include <haproxy/sample-t.h>
29 #include <haproxy/stick_table-t.h>
30 
31 extern sample_cast_fct sample_casts[SMP_TYPES][SMP_TYPES];
32 extern const unsigned int fetch_cap[SMP_SRC_ENTRIES];
33 extern const char *smp_to_type[SMP_TYPES];
34 
35 struct sample_expr *sample_parse_expr(char **str, int *idx, const char *file, int line, char **err, struct arg_list *al, char **endptr);
36 struct sample_conv *find_sample_conv(const char *kw, int len);
37 struct sample *sample_process(struct proxy *px, struct session *sess,
38                               struct stream *strm, unsigned int opt,
39                               struct sample_expr *expr, struct sample *p);
40 struct sample *sample_fetch_as_type(struct proxy *px, struct session *sess,
41                                    struct stream *strm, unsigned int opt,
42                                    struct sample_expr *expr, int smp_type);
43 int sample_conv_var2smp(const struct var_desc *var, struct sample *smp, int type);
44 int sample_conv_var2smp_sint(const struct arg *arg, struct sample *smp);
45 int sample_conv_var2smp_str(const struct arg *arg, struct sample *smp);
46 void release_sample_expr(struct sample_expr *expr);
47 void sample_register_fetches(struct sample_fetch_kw_list *psl);
48 void sample_register_convs(struct sample_conv_kw_list *psl);
49 const char *sample_src_names(unsigned int use);
50 const char *sample_ckp_names(unsigned int use);
51 struct sample_fetch *find_sample_fetch(const char *kw, int len);
52 struct sample_fetch *sample_fetch_getnext(struct sample_fetch *current, int *idx);
53 struct sample_conv *sample_conv_getnext(struct sample_conv *current, int *idx);
54 int smp_resolve_args(struct proxy *p);
55 int smp_check_date_unit(struct arg *args, char **err);
56 int smp_expr_output_type(struct sample_expr *expr);
57 int c_none(struct sample *smp);
58 int smp_dup(struct sample *smp);
59 
60 /*
61  * This function just apply a cast on sample. It returns 0 if the cast is not
62  * available or if the cast fails, otherwise returns 1. It does not modify the
63  * input sample on failure.
64  */
65 static inline
sample_convert(struct sample * sample,int req_type)66 int sample_convert(struct sample *sample, int req_type)
67 {
68 	if (!sample_casts[sample->data.type][req_type])
69 		return 0;
70 	if (sample_casts[sample->data.type][req_type] == c_none)
71 		return 1;
72 	return sample_casts[sample->data.type][req_type](sample);
73 }
74 
75 static inline
smp_set_owner(struct sample * smp,struct proxy * px,struct session * sess,struct stream * strm,int opt)76 struct 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 
86 
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  */
93 static inline
smp_is_safe(struct sample * smp)94 int smp_is_safe(struct sample *smp)
95 {
96 	switch (smp->data.type) {
97 	case SMP_T_METH:
98 		if (smp->data.u.meth.meth != HTTP_METH_OTHER)
99 			return 1;
100 		/* Fall through */
101 
102 	case SMP_T_STR:
103 		if (!smp->data.u.str.size || smp->data.u.str.data >= smp->data.u.str.size)
104 			return 0;
105 
106 		if (smp->data.u.str.area[smp->data.u.str.data] == 0)
107 			return 1;
108 
109 		if (smp->flags & SMP_F_CONST)
110 			return 0;
111 
112 		smp->data.u.str.area[smp->data.u.str.data] = 0;
113 		return 1;
114 
115 	case SMP_T_BIN:
116 		return !smp->data.u.str.size || smp->data.u.str.data <= smp->data.u.str.size;
117 
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  */
127 static inline
smp_make_safe(struct sample * smp)128 int smp_make_safe(struct sample *smp)
129 {
130 	return smp && (smp_is_safe(smp) || smp_dup(smp));
131 }
132 
133 /* 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  */
140 static inline
smp_is_rw(struct sample * smp)141 int smp_is_rw(struct sample *smp)
142 {
143 	if (smp->flags & SMP_F_CONST)
144 		return 0;
145 
146 	switch (smp->data.type) {
147 	case SMP_T_METH:
148 		if (smp->data.u.meth.meth != HTTP_METH_OTHER)
149 			return 1;
150 		/* Fall through */
151 
152 	case SMP_T_STR:
153 		if (!smp->data.u.str.size ||
154 		    smp->data.u.str.data >= smp->data.u.str.size)
155 			return 0;
156 
157 		if (smp->data.u.str.area[smp->data.u.str.data] != 0)
158 			smp->data.u.str.area[smp->data.u.str.data] = 0;
159 		return 1;
160 
161 	case SMP_T_BIN:
162 		return smp->data.u.str.size &&
163 		       smp->data.u.str.data <= smp->data.u.str.size;
164 
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  */
174 static inline
smp_make_rw(struct sample * smp)175 int smp_make_rw(struct sample *smp)
176 {
177 	return smp && (smp_is_rw(smp) || smp_dup(smp));
178 }
179 
180 #endif /* _HAPROXY_SAMPLE_H */
181