1 /*
2  * include/proto/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 _PROTO_SAMPLE_H
24 #define _PROTO_SAMPLE_H
25 
26 #include <types/sample.h>
27 #include <types/stick_table.h>
28 
29 extern const char *smp_to_type[SMP_TYPES];
30 
31 struct sample_expr *sample_parse_expr(char **str, int *idx, const char *file, int line, char **err, struct arg_list *al);
32 struct sample_conv *find_sample_conv(const char *kw, int len);
33 struct sample *sample_process(struct proxy *px, struct session *sess,
34                               struct stream *strm, unsigned int opt,
35                               struct sample_expr *expr, struct sample *p);
36 struct sample *sample_fetch_as_type(struct proxy *px, struct session *sess,
37                                    struct stream *strm, unsigned int opt,
38                                    struct sample_expr *expr, int smp_type);
39 void release_sample_expr(struct sample_expr *expr);
40 void sample_register_fetches(struct sample_fetch_kw_list *psl);
41 void sample_register_convs(struct sample_conv_kw_list *psl);
42 const char *sample_src_names(unsigned int use);
43 const char *sample_ckp_names(unsigned int use);
44 struct sample_fetch *find_sample_fetch(const char *kw, int len);
45 struct sample_fetch *sample_fetch_getnext(struct sample_fetch *current, int *idx);
46 struct sample_conv *sample_conv_getnext(struct sample_conv *current, int *idx);
47 int smp_resolve_args(struct proxy *p);
48 int smp_check_date_unit(struct arg *args, char **err);
49 int smp_expr_output_type(struct sample_expr *expr);
50 int c_none(struct sample *smp);
51 int smp_dup(struct sample *smp);
52 
53 /*
54  * This function just apply a cast on sample. It returns 0 if the cast is not
55  * available or if the cast fails, otherwise returns 1. It does not modify the
56  * input sample on failure.
57  */
58 static inline
sample_convert(struct sample * sample,int req_type)59 int sample_convert(struct sample *sample, int req_type)
60 {
61 	if (!sample_casts[sample->data.type][req_type])
62 		return 0;
63 	if (sample_casts[sample->data.type][req_type] == c_none)
64 		return 1;
65 	return sample_casts[sample->data.type][req_type](sample);
66 }
67 
68 static inline
smp_set_owner(struct sample * smp,struct proxy * px,struct session * sess,struct stream * strm,int opt)69 struct sample *smp_set_owner(struct sample *smp, struct proxy *px,
70                              struct session *sess, struct stream *strm, int opt)
71 {
72 	smp->px   = px;
73 	smp->sess = sess;
74 	smp->strm = strm;
75 	smp->opt  = opt;
76 	return smp;
77 }
78 
79 
80 /* Returns 1 if a sample may be safely used. It performs a few checks on the
81  * string length versus size, same for the binary version, and ensures that
82  * strings are properly terminated by a zero. If this last point is not granted
83  * but the string is not const, then the \0 is appended. Otherwise it returns 0,
84  * meaning the caller may need to call smp_dup() before going further.
85  */
86 static inline
smp_is_safe(struct sample * smp)87 int smp_is_safe(struct sample *smp)
88 {
89 	switch (smp->data.type) {
90 	case SMP_T_METH:
91 		if (smp->data.u.meth.meth != HTTP_METH_OTHER)
92 			return 1;
93 		/* Fall through */
94 
95 	case SMP_T_STR:
96 		if (smp->data.u.str.size && smp->data.u.str.data >= smp->data.u.str.size)
97 			return 0;
98 
99 		if (smp->data.u.str.area[smp->data.u.str.data] == 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.area[smp->data.u.str.data] = 0;
106 		return 1;
107 
108 	case SMP_T_BIN:
109 		return !smp->data.u.str.size || smp->data.u.str.data <= smp->data.u.str.size;
110 
111 	default:
112 		return 1;
113 	}
114 }
115 
116 /* checks that a sample may freely be used, or duplicates it to normalize it.
117  * Returns 1 on success, 0 if the sample must not be used. The function also
118  * checks for NULL to simplify the calling code.
119  */
120 static inline
smp_make_safe(struct sample * smp)121 int smp_make_safe(struct sample *smp)
122 {
123 	return smp && (smp_is_safe(smp) || smp_dup(smp));
124 }
125 
126 /* Returns 1 if a sample may be safely modified in place. It performs a few
127  * checks on the string length versus size, same for the binary version, and
128  * ensures that strings are properly terminated by a zero, and of course that
129  * the size is allocate and that the SMP_F_CONST flag is not set. If only the
130  * trailing zero is missing, it is appended. Otherwise it returns 0, meaning
131  * the caller may need to call smp_dup() before going further.
132  */
133 static inline
smp_is_rw(struct sample * smp)134 int smp_is_rw(struct sample *smp)
135 {
136 	if (smp->flags & SMP_F_CONST)
137 		return 0;
138 
139 	switch (smp->data.type) {
140 	case SMP_T_METH:
141 		if (smp->data.u.meth.meth != HTTP_METH_OTHER)
142 			return 1;
143 		/* Fall through */
144 
145 	case SMP_T_STR:
146 		if (!smp->data.u.str.size ||
147 		    smp->data.u.str.data >= smp->data.u.str.size)
148 			return 0;
149 
150 		if (smp->data.u.str.area[smp->data.u.str.data] != 0)
151 			smp->data.u.str.area[smp->data.u.str.data] = 0;
152 		return 1;
153 
154 	case SMP_T_BIN:
155 		return smp->data.u.str.size &&
156 		       smp->data.u.str.data <= smp->data.u.str.size;
157 
158 	default:
159 		return 1;
160 	}
161 }
162 
163 /* checks that a sample may freely be modified, or duplicates it to normalize
164  * it and make it R/W. Returns 1 on success, 0 if the sample must not be used.
165  * The function also checks for NULL to simplify the calling code.
166  */
167 static inline
smp_make_rw(struct sample * smp)168 int smp_make_rw(struct sample *smp)
169 {
170 	return smp && (smp_is_rw(smp) || smp_dup(smp));
171 }
172 
173 #endif /* _PROTO_SAMPLE_H */
174