blob: 5a79955bcb8a235c447d0068a60ccd4afcc0df48 [file] [log] [blame]
Emeric Brun107ca302010-01-04 16:16:05 +01001/*
Willy Tarreaucd3b0942012-04-27 21:52:18 +02002 * Sample management functions.
Emeric Brun107ca302010-01-04 16:16:05 +01003 *
4 * Copyright 2009-2010 EXCELIANCE, Emeric Brun <ebrun@exceliance.fr>
Willy Tarreaucd3b0942012-04-27 21:52:18 +02005 * Copyright (C) 2012 Willy Tarreau <w@1wt.eu>
Emeric Brun107ca302010-01-04 16:16:05 +01006 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
11 *
12 */
13
Thierry FOURNIER317e1c42014-08-12 10:20:47 +020014#include <ctype.h>
Emeric Brun107ca302010-01-04 16:16:05 +010015#include <string.h>
16#include <arpa/inet.h>
Willy Tarreaudd2f85e2012-09-02 22:34:23 +020017#include <stdio.h>
Emeric Brun107ca302010-01-04 16:16:05 +010018
Willy Tarreau7e2c6472012-10-29 20:44:36 +010019#include <types/global.h>
20
Willy Tarreauc7e42382012-08-24 19:22:53 +020021#include <common/chunk.h>
Willy Tarreau23ec4ca2014-07-15 20:15:37 +020022#include <common/hash.h>
Willy Tarreaucd3b0942012-04-27 21:52:18 +020023#include <common/standard.h>
Willy Tarreaua4312fa2013-04-02 16:34:32 +020024#include <common/uri_auth.h>
Emeric Brun53d1a982014-04-30 18:21:37 +020025#include <common/base64.h>
Willy Tarreaucd3b0942012-04-27 21:52:18 +020026
Willy Tarreau9fcb9842012-04-20 14:45:49 +020027#include <proto/arg.h>
Willy Tarreaua4312fa2013-04-02 16:34:32 +020028#include <proto/auth.h>
29#include <proto/log.h>
Thierry FOURNIERd4373142013-12-17 01:10:10 +010030#include <proto/proto_http.h>
Willy Tarreaua4312fa2013-04-02 16:34:32 +020031#include <proto/proxy.h>
Willy Tarreaucd3b0942012-04-27 21:52:18 +020032#include <proto/sample.h>
Willy Tarreaua4312fa2013-04-02 16:34:32 +020033#include <proto/stick_table.h>
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +020034#include <proto/vars.h>
Emeric Brun107ca302010-01-04 16:16:05 +010035
Willy Tarreau1cf8f082014-02-07 12:14:54 +010036/* sample type names */
37const char *smp_to_type[SMP_TYPES] = {
Thierry FOURNIER9c627e82015-06-03 20:12:35 +020038 [SMP_T_ANY] = "any",
Willy Tarreau1cf8f082014-02-07 12:14:54 +010039 [SMP_T_BOOL] = "bool",
Willy Tarreau1cf8f082014-02-07 12:14:54 +010040 [SMP_T_SINT] = "sint",
41 [SMP_T_ADDR] = "addr",
42 [SMP_T_IPV4] = "ipv4",
43 [SMP_T_IPV6] = "ipv6",
44 [SMP_T_STR] = "str",
45 [SMP_T_BIN] = "bin",
Thierry FOURNIER4c2479e2015-06-03 20:12:04 +020046 [SMP_T_METH] = "meth",
Willy Tarreau1cf8f082014-02-07 12:14:54 +010047};
48
Willy Tarreau12785782012-04-27 21:37:17 +020049/* static sample used in sample_process() when <p> is NULL */
Willy Tarreaub4a88f02012-04-23 21:35:11 +020050static struct sample temp_smp;
Emeric Brun107ca302010-01-04 16:16:05 +010051
Willy Tarreau12785782012-04-27 21:37:17 +020052/* list head of all known sample fetch keywords */
53static struct sample_fetch_kw_list sample_fetches = {
54 .list = LIST_HEAD_INIT(sample_fetches.list)
Emeric Brun107ca302010-01-04 16:16:05 +010055};
56
Willy Tarreau12785782012-04-27 21:37:17 +020057/* list head of all known sample format conversion keywords */
58static struct sample_conv_kw_list sample_convs = {
59 .list = LIST_HEAD_INIT(sample_convs.list)
Emeric Brun107ca302010-01-04 16:16:05 +010060};
61
Willy Tarreau80aca902013-01-07 15:42:20 +010062const unsigned int fetch_cap[SMP_SRC_ENTRIES] = {
63 [SMP_SRC_INTRN] = (SMP_VAL_FE_CON_ACC | SMP_VAL_FE_SES_ACC | SMP_VAL_FE_REQ_CNT |
64 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
65 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
66 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
67 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
68 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
69 SMP_VAL_FE_LOG_END),
70
71 [SMP_SRC_LISTN] = (SMP_VAL_FE_CON_ACC | SMP_VAL_FE_SES_ACC | SMP_VAL_FE_REQ_CNT |
72 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
73 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
74 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
75 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
76 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
77 SMP_VAL_FE_LOG_END),
78
79 [SMP_SRC_FTEND] = (SMP_VAL_FE_CON_ACC | SMP_VAL_FE_SES_ACC | SMP_VAL_FE_REQ_CNT |
80 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
81 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
82 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
83 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
84 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
85 SMP_VAL_FE_LOG_END),
86
87 [SMP_SRC_L4CLI] = (SMP_VAL_FE_CON_ACC | SMP_VAL_FE_SES_ACC | SMP_VAL_FE_REQ_CNT |
88 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
89 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
90 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
91 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
92 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
93 SMP_VAL_FE_LOG_END),
94
95 [SMP_SRC_L5CLI] = (SMP_VAL___________ | SMP_VAL_FE_SES_ACC | SMP_VAL_FE_REQ_CNT |
96 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
97 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
98 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
99 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
100 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
101 SMP_VAL_FE_LOG_END),
102
103 [SMP_SRC_TRACK] = (SMP_VAL_FE_CON_ACC | SMP_VAL_FE_SES_ACC | SMP_VAL_FE_REQ_CNT |
104 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
105 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
106 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
107 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
108 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
109 SMP_VAL_FE_LOG_END),
110
111 [SMP_SRC_L6REQ] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_FE_REQ_CNT |
112 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
113 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
114 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL___________ |
115 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
116 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
117 SMP_VAL___________),
118
119 [SMP_SRC_HRQHV] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_FE_REQ_CNT |
120 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
121 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
122 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL___________ |
123 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
124 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
125 SMP_VAL___________),
126
127 [SMP_SRC_HRQHP] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_FE_REQ_CNT |
128 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
129 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
130 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
131 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
132 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
133 SMP_VAL_FE_LOG_END),
134
135 [SMP_SRC_HRQBO] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
136 SMP_VAL___________ | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
137 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
138 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL___________ |
139 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
140 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
141 SMP_VAL___________),
142
143 [SMP_SRC_BKEND] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
144 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
145 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
146 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
147 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
148 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
149 SMP_VAL_FE_LOG_END),
150
151 [SMP_SRC_SERVR] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
152 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
153 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
154 SMP_VAL___________ | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
155 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
156 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
157 SMP_VAL_FE_LOG_END),
158
159 [SMP_SRC_L4SRV] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
160 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
161 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
162 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_BE_RES_CNT |
163 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
164 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
165 SMP_VAL_FE_LOG_END),
166
167 [SMP_SRC_L5SRV] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
168 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
169 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
170 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_BE_RES_CNT |
171 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
172 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
173 SMP_VAL_FE_LOG_END),
174
175 [SMP_SRC_L6RES] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
176 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
177 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
178 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_BE_RES_CNT |
179 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
180 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
181 SMP_VAL___________),
182
183 [SMP_SRC_HRSHV] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
184 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
185 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
186 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_BE_RES_CNT |
187 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
188 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
189 SMP_VAL___________),
190
191 [SMP_SRC_HRSHP] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
192 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
193 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
194 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_BE_RES_CNT |
195 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
196 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
197 SMP_VAL_FE_LOG_END),
198
199 [SMP_SRC_HRSBO] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
200 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
201 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
202 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
203 SMP_VAL___________ | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
204 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
205 SMP_VAL___________),
206
207 [SMP_SRC_RQFIN] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
208 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
209 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
210 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
211 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
212 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
213 SMP_VAL_FE_LOG_END),
214
215 [SMP_SRC_RSFIN] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
216 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
217 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
218 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
219 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
220 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
221 SMP_VAL_FE_LOG_END),
222
223 [SMP_SRC_TXFIN] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
224 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
225 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
226 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
227 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
228 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
229 SMP_VAL_FE_LOG_END),
230
231 [SMP_SRC_SSFIN] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
232 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
233 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
234 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
235 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
236 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
237 SMP_VAL_FE_LOG_END),
238};
239
240static const char *fetch_src_names[SMP_SRC_ENTRIES] = {
241 [SMP_SRC_INTRN] = "internal state",
242 [SMP_SRC_LISTN] = "listener",
243 [SMP_SRC_FTEND] = "frontend",
244 [SMP_SRC_L4CLI] = "client address",
245 [SMP_SRC_L5CLI] = "client-side connection",
246 [SMP_SRC_TRACK] = "track counters",
247 [SMP_SRC_L6REQ] = "request buffer",
248 [SMP_SRC_HRQHV] = "HTTP request headers",
249 [SMP_SRC_HRQHP] = "HTTP request",
250 [SMP_SRC_HRQBO] = "HTTP request body",
251 [SMP_SRC_BKEND] = "backend",
252 [SMP_SRC_SERVR] = "server",
253 [SMP_SRC_L4SRV] = "server address",
254 [SMP_SRC_L5SRV] = "server-side connection",
255 [SMP_SRC_L6RES] = "response buffer",
256 [SMP_SRC_HRSHV] = "HTTP response headers",
257 [SMP_SRC_HRSHP] = "HTTP response",
258 [SMP_SRC_HRSBO] = "HTTP response body",
259 [SMP_SRC_RQFIN] = "request buffer statistics",
260 [SMP_SRC_RSFIN] = "response buffer statistics",
261 [SMP_SRC_TXFIN] = "transaction statistics",
262 [SMP_SRC_SSFIN] = "session statistics",
263};
264
Willy Tarreaubf8e2512013-03-25 14:52:41 +0100265static const char *fetch_ckp_names[SMP_CKP_ENTRIES] = {
266 [SMP_CKP_FE_CON_ACC] = "frontend tcp-request connection rule",
267 [SMP_CKP_FE_SES_ACC] = "frontend tcp-request session rule",
268 [SMP_CKP_FE_REQ_CNT] = "frontend tcp-request content rule",
269 [SMP_CKP_FE_HRQ_HDR] = "frontend http-request header rule",
270 [SMP_CKP_FE_HRQ_BDY] = "frontend http-request body rule",
271 [SMP_CKP_FE_SET_BCK] = "frontend use-backend rule",
272 [SMP_CKP_BE_REQ_CNT] = "backend tcp-request content rule",
273 [SMP_CKP_BE_HRQ_HDR] = "backend http-request header rule",
274 [SMP_CKP_BE_HRQ_BDY] = "backend http-request body rule",
275 [SMP_CKP_BE_SET_SRV] = "backend use-server, balance or stick-match rule",
276 [SMP_CKP_BE_SRV_CON] = "server source selection",
277 [SMP_CKP_BE_RES_CNT] = "backend tcp-response content rule",
278 [SMP_CKP_BE_HRS_HDR] = "backend http-response header rule",
279 [SMP_CKP_BE_HRS_BDY] = "backend http-response body rule",
280 [SMP_CKP_BE_STO_RUL] = "backend stick-store rule",
281 [SMP_CKP_FE_RES_CNT] = "frontend tcp-response content rule",
282 [SMP_CKP_FE_HRS_HDR] = "frontend http-response header rule",
283 [SMP_CKP_FE_HRS_BDY] = "frontend http-response body rule",
284 [SMP_CKP_FE_LOG_END] = "logs",
285};
286
Thierry FOURNIER348971e2013-11-21 10:50:10 +0100287/* This function returns the type of the data returned by the sample_expr.
288 * It assumes that the <expr> and all of its converters are properly
289 * initialized.
290 */
291inline
292int smp_expr_output_type(struct sample_expr *expr)
293{
294 struct sample_conv_expr *smp_expr;
295
296 if (!LIST_ISEMPTY(&expr->conv_exprs)) {
297 smp_expr = LIST_PREV(&expr->conv_exprs, struct sample_conv_expr *, list);
298 return smp_expr->conv->out_type;
299 }
300 return expr->fetch->out_type;
301}
302
303
Willy Tarreau80aca902013-01-07 15:42:20 +0100304/* fill the trash with a comma-delimited list of source names for the <use> bit
305 * field which must be composed of a non-null set of SMP_USE_* flags. The return
306 * value is the pointer to the string in the trash buffer.
307 */
308const char *sample_src_names(unsigned int use)
309{
310 int bit;
311
312 trash.len = 0;
313 trash.str[0] = '\0';
314 for (bit = 0; bit < SMP_SRC_ENTRIES; bit++) {
315 if (!(use & ~((1 << bit) - 1)))
316 break; /* no more bits */
317
318 if (!(use & (1 << bit)))
319 continue; /* bit not set */
320
321 trash.len += snprintf(trash.str + trash.len, trash.size - trash.len, "%s%s",
322 (use & ((1 << bit) - 1)) ? "," : "",
323 fetch_src_names[bit]);
324 }
325 return trash.str;
326}
327
Willy Tarreaubf8e2512013-03-25 14:52:41 +0100328/* return a pointer to the correct sample checkpoint name, or "unknown" when
329 * the flags are invalid. Only the lowest bit is used, higher bits are ignored
330 * if set.
331 */
332const char *sample_ckp_names(unsigned int use)
333{
334 int bit;
335
336 for (bit = 0; bit < SMP_CKP_ENTRIES; bit++)
337 if (use & (1 << bit))
338 return fetch_ckp_names[bit];
339 return "unknown sample check place, please report this bug";
340}
341
Emeric Brun107ca302010-01-04 16:16:05 +0100342/*
Willy Tarreau80aca902013-01-07 15:42:20 +0100343 * Registers the sample fetch keyword list <kwl> as a list of valid keywords
344 * for next parsing sessions. The fetch keywords capabilities are also computed
345 * from their ->use field.
Emeric Brun107ca302010-01-04 16:16:05 +0100346 */
Willy Tarreau80aca902013-01-07 15:42:20 +0100347void sample_register_fetches(struct sample_fetch_kw_list *kwl)
Emeric Brun107ca302010-01-04 16:16:05 +0100348{
Willy Tarreau80aca902013-01-07 15:42:20 +0100349 struct sample_fetch *sf;
350 int bit;
351
352 for (sf = kwl->kw; sf->kw != NULL; sf++) {
353 for (bit = 0; bit < SMP_SRC_ENTRIES; bit++)
354 if (sf->use & (1 << bit))
355 sf->val |= fetch_cap[bit];
356 }
357 LIST_ADDQ(&sample_fetches.list, &kwl->list);
Emeric Brun107ca302010-01-04 16:16:05 +0100358}
359
360/*
Willy Tarreau12785782012-04-27 21:37:17 +0200361 * Registers the sample format coverstion keyword list <pckl> as a list of valid keywords for next
Emeric Brun107ca302010-01-04 16:16:05 +0100362 * parsing sessions.
363 */
Willy Tarreau12785782012-04-27 21:37:17 +0200364void sample_register_convs(struct sample_conv_kw_list *pckl)
Emeric Brun107ca302010-01-04 16:16:05 +0100365{
Willy Tarreau12785782012-04-27 21:37:17 +0200366 LIST_ADDQ(&sample_convs.list, &pckl->list);
Emeric Brun107ca302010-01-04 16:16:05 +0100367}
368
369/*
Willy Tarreau12785782012-04-27 21:37:17 +0200370 * Returns the pointer on sample fetch keyword structure identified by
Emeric Brun107ca302010-01-04 16:16:05 +0100371 * string of <len> in buffer <kw>.
372 *
373 */
Willy Tarreau12785782012-04-27 21:37:17 +0200374struct sample_fetch *find_sample_fetch(const char *kw, int len)
Emeric Brun107ca302010-01-04 16:16:05 +0100375{
376 int index;
Willy Tarreau12785782012-04-27 21:37:17 +0200377 struct sample_fetch_kw_list *kwl;
Emeric Brun107ca302010-01-04 16:16:05 +0100378
Willy Tarreau12785782012-04-27 21:37:17 +0200379 list_for_each_entry(kwl, &sample_fetches.list, list) {
Emeric Brun107ca302010-01-04 16:16:05 +0100380 for (index = 0; kwl->kw[index].kw != NULL; index++) {
381 if (strncmp(kwl->kw[index].kw, kw, len) == 0 &&
382 kwl->kw[index].kw[len] == '\0')
383 return &kwl->kw[index];
384 }
385 }
386 return NULL;
387}
388
Thierry FOURNIER4d9a1d12014-12-08 14:49:19 +0100389/* This fucntion browse the list of available saple fetch. <current> is
390 * the last used sample fetch. If it is the first call, it must set to NULL.
391 * <idx> is the index of the next sampleèfetch entry. It is used as private
392 * value. It is useles to initiate it.
393 *
394 * It returns always the newt fetch_sample entry, and NULL when the end of
395 * the list is reached.
396 */
397struct sample_fetch *sample_fetch_getnext(struct sample_fetch *current, int *idx)
398{
399 struct sample_fetch_kw_list *kwl;
400 struct sample_fetch *base;
401
402 if (!current) {
403 /* Get first kwl entry. */
404 kwl = LIST_NEXT(&sample_fetches.list, struct sample_fetch_kw_list *, list);
405 (*idx) = 0;
406 } else {
407 /* Get kwl corresponding to the curret entry. */
408 base = current + 1 - (*idx);
409 kwl = container_of(base, struct sample_fetch_kw_list, kw);
410 }
411
412 while (1) {
413
414 /* Check if kwl is the last entry. */
415 if (&kwl->list == &sample_fetches.list)
416 return NULL;
417
418 /* idx contain the next keyword. If it is available, return it. */
419 if (kwl->kw[*idx].kw) {
420 (*idx)++;
421 return &kwl->kw[(*idx)-1];
422 }
423
424 /* get next entry in the main list, and return NULL if the end is reached. */
425 kwl = LIST_NEXT(&kwl->list, struct sample_fetch_kw_list *, list);
426
427 /* Set index to 0, ans do one other loop. */
428 (*idx) = 0;
429 }
430}
431
Thierry FOURNIER8fd13762015-03-10 23:56:48 +0100432/* This function browses the list of available converters. <current> is
433 * the last used converter. If it is the first call, it must set to NULL.
434 * <idx> is the index of the next converter entry. It is used as private
435 * value. It is useless to initiate it.
436 *
437 * It returns always the next sample_conv entry, and NULL when the end of
438 * the list is reached.
439 */
440struct sample_conv *sample_conv_getnext(struct sample_conv *current, int *idx)
441{
442 struct sample_conv_kw_list *kwl;
443 struct sample_conv *base;
444
445 if (!current) {
446 /* Get first kwl entry. */
447 kwl = LIST_NEXT(&sample_convs.list, struct sample_conv_kw_list *, list);
448 (*idx) = 0;
449 } else {
450 /* Get kwl corresponding to the curret entry. */
451 base = current + 1 - (*idx);
452 kwl = container_of(base, struct sample_conv_kw_list, kw);
453 }
454
455 while (1) {
456 /* Check if kwl is the last entry. */
457 if (&kwl->list == &sample_convs.list)
458 return NULL;
459
460 /* idx contain the next keyword. If it is available, return it. */
461 if (kwl->kw[*idx].kw) {
462 (*idx)++;
463 return &kwl->kw[(*idx)-1];
464 }
465
466 /* get next entry in the main list, and return NULL if the end is reached. */
467 kwl = LIST_NEXT(&kwl->list, struct sample_conv_kw_list *, list);
468
469 /* Set index to 0, ans do one other loop. */
470 (*idx) = 0;
471 }
472}
473
Emeric Brun107ca302010-01-04 16:16:05 +0100474/*
Willy Tarreau12785782012-04-27 21:37:17 +0200475 * Returns the pointer on sample format conversion keyword structure identified by
Emeric Brun107ca302010-01-04 16:16:05 +0100476 * string of <len> in buffer <kw>.
477 *
478 */
Willy Tarreau12785782012-04-27 21:37:17 +0200479struct sample_conv *find_sample_conv(const char *kw, int len)
Emeric Brun107ca302010-01-04 16:16:05 +0100480{
481 int index;
Willy Tarreau12785782012-04-27 21:37:17 +0200482 struct sample_conv_kw_list *kwl;
Emeric Brun107ca302010-01-04 16:16:05 +0100483
Willy Tarreau12785782012-04-27 21:37:17 +0200484 list_for_each_entry(kwl, &sample_convs.list, list) {
Emeric Brun107ca302010-01-04 16:16:05 +0100485 for (index = 0; kwl->kw[index].kw != NULL; index++) {
486 if (strncmp(kwl->kw[index].kw, kw, len) == 0 &&
487 kwl->kw[index].kw[len] == '\0')
488 return &kwl->kw[index];
489 }
490 }
491 return NULL;
492}
493
Emeric Brun107ca302010-01-04 16:16:05 +0100494/******************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +0200495/* Sample casts functions */
Willy Tarreaub8c8f1f2012-04-23 22:38:26 +0200496/* Note: these functions do *NOT* set the output type on the */
497/* sample, the caller is responsible for doing this on return. */
Emeric Brun107ca302010-01-04 16:16:05 +0100498/******************************************************************/
499
Willy Tarreau342acb42012-04-23 22:03:39 +0200500static int c_ip2int(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100501{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200502 smp->data.u.sint = ntohl(smp->data.u.ipv4.s_addr);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200503 smp->data.type = SMP_T_SINT;
Emeric Brun107ca302010-01-04 16:16:05 +0100504 return 1;
505}
506
Willy Tarreau342acb42012-04-23 22:03:39 +0200507static int c_ip2str(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100508{
Willy Tarreau47ca5452012-12-23 20:22:19 +0100509 struct chunk *trash = get_trash_chunk();
Emeric Brun107ca302010-01-04 16:16:05 +0100510
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200511 if (!inet_ntop(AF_INET, (void *)&smp->data.u.ipv4, trash->str, trash->size))
Emeric Brun107ca302010-01-04 16:16:05 +0100512 return 0;
513
514 trash->len = strlen(trash->str);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200515 smp->data.u.str = *trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200516 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100517 smp->flags &= ~SMP_F_CONST;
Emeric Brun107ca302010-01-04 16:16:05 +0100518
519 return 1;
520}
521
Willy Tarreau342acb42012-04-23 22:03:39 +0200522static int c_ip2ipv6(struct sample *smp)
David du Colombier4f92d322011-03-24 11:09:31 +0100523{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200524 v4tov6(&smp->data.u.ipv6, &smp->data.u.ipv4);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200525 smp->data.type = SMP_T_IPV6;
David du Colombier4f92d322011-03-24 11:09:31 +0100526 return 1;
527}
528
Thierry FOURNIERcc4d1712015-07-24 09:04:56 +0200529static int c_ipv62ip(struct sample *smp)
530{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200531 if (!v6tov4(&smp->data.u.ipv4, &smp->data.u.ipv6))
Thierry FOURNIERcc4d1712015-07-24 09:04:56 +0200532 return 0;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200533 smp->data.type = SMP_T_IPV6;
Thierry FOURNIERcc4d1712015-07-24 09:04:56 +0200534 return 1;
535}
536
Willy Tarreau342acb42012-04-23 22:03:39 +0200537static int c_ipv62str(struct sample *smp)
David du Colombier4f92d322011-03-24 11:09:31 +0100538{
Willy Tarreau47ca5452012-12-23 20:22:19 +0100539 struct chunk *trash = get_trash_chunk();
David du Colombier4f92d322011-03-24 11:09:31 +0100540
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200541 if (!inet_ntop(AF_INET6, (void *)&smp->data.u.ipv6, trash->str, trash->size))
David du Colombier4f92d322011-03-24 11:09:31 +0100542 return 0;
543
544 trash->len = strlen(trash->str);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200545 smp->data.u.str = *trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200546 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100547 smp->flags &= ~SMP_F_CONST;
David du Colombier4f92d322011-03-24 11:09:31 +0100548 return 1;
549}
550
551/*
Willy Tarreau342acb42012-04-23 22:03:39 +0200552static int c_ipv62ip(struct sample *smp)
David du Colombier4f92d322011-03-24 11:09:31 +0100553{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200554 return v6tov4(&smp->data.u.ipv4, &smp->data.u.ipv6);
David du Colombier4f92d322011-03-24 11:09:31 +0100555}
556*/
557
Willy Tarreau342acb42012-04-23 22:03:39 +0200558static int c_int2ip(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100559{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200560 smp->data.u.ipv4.s_addr = htonl((unsigned int)smp->data.u.sint);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200561 smp->data.type = SMP_T_IPV4;
Emeric Brun107ca302010-01-04 16:16:05 +0100562 return 1;
563}
564
Thierry FOURNIERcc4d1712015-07-24 09:04:56 +0200565static int c_int2ipv6(struct sample *smp)
566{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200567 smp->data.u.ipv4.s_addr = htonl((unsigned int)smp->data.u.sint);
568 v4tov6(&smp->data.u.ipv6, &smp->data.u.ipv4);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200569 smp->data.type = SMP_T_IPV6;
Thierry FOURNIERcc4d1712015-07-24 09:04:56 +0200570 return 1;
571}
572
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100573static int c_str2addr(struct sample *smp)
574{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200575 if (!buf2ip(smp->data.u.str.str, smp->data.u.str.len, &smp->data.u.ipv4)) {
576 if (!buf2ip6(smp->data.u.str.str, smp->data.u.str.len, &smp->data.u.ipv6))
Thierry FOURNIERfd139902013-12-11 12:38:57 +0100577 return 0;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200578 smp->data.type = SMP_T_IPV6;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100579 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIERfd139902013-12-11 12:38:57 +0100580 return 1;
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100581 }
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200582 smp->data.type = SMP_T_IPV4;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100583 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100584 return 1;
585}
586
Willy Tarreau342acb42012-04-23 22:03:39 +0200587static int c_str2ip(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100588{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200589 if (!buf2ip(smp->data.u.str.str, smp->data.u.str.len, &smp->data.u.ipv4))
Emeric Brun107ca302010-01-04 16:16:05 +0100590 return 0;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200591 smp->data.type = SMP_T_IPV4;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100592 smp->flags &= ~SMP_F_CONST;
Emeric Brun107ca302010-01-04 16:16:05 +0100593 return 1;
594}
595
Willy Tarreau342acb42012-04-23 22:03:39 +0200596static int c_str2ipv6(struct sample *smp)
David du Colombier4f92d322011-03-24 11:09:31 +0100597{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200598 if (!buf2ip6(smp->data.u.str.str, smp->data.u.str.len, &smp->data.u.ipv6))
Thierry FOURNIERfd139902013-12-11 12:38:57 +0100599 return 0;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200600 smp->data.type = SMP_T_IPV6;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100601 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIERfd139902013-12-11 12:38:57 +0100602 return 1;
David du Colombier4f92d322011-03-24 11:09:31 +0100603}
604
Emeric Brun4b9e8022014-11-03 18:17:10 +0100605/*
606 * The NULL char always enforces the end of string if it is met.
607 * Data is never changed, so we can ignore the CONST case
Thierry FOURNIERe87cac12014-03-12 15:07:59 +0100608 */
Emeric Brun8ac33d92012-10-17 13:36:06 +0200609static int c_bin2str(struct sample *smp)
610{
Emeric Brun4b9e8022014-11-03 18:17:10 +0100611 int i;
Emeric Brun8ac33d92012-10-17 13:36:06 +0200612
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200613 for (i = 0; i < smp->data.u.str.len; i++) {
614 if (!smp->data.u.str.str[i]) {
615 smp->data.u.str.len = i;
Thierry FOURNIERe87cac12014-03-12 15:07:59 +0100616 break;
Emeric Brun4b9e8022014-11-03 18:17:10 +0100617 }
Emeric Brun8ac33d92012-10-17 13:36:06 +0200618 }
Emeric Brun8ac33d92012-10-17 13:36:06 +0200619 return 1;
620}
621
Willy Tarreau342acb42012-04-23 22:03:39 +0200622static int c_int2str(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100623{
Willy Tarreau47ca5452012-12-23 20:22:19 +0100624 struct chunk *trash = get_trash_chunk();
Emeric Brun107ca302010-01-04 16:16:05 +0100625 char *pos;
626
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200627 pos = lltoa_r(smp->data.u.sint, trash->str, trash->size);
Emeric Brun107ca302010-01-04 16:16:05 +0100628 if (!pos)
629 return 0;
630
Emeric Brun485479d2010-09-23 18:02:19 +0200631 trash->size = trash->size - (pos - trash->str);
Emeric Brun107ca302010-01-04 16:16:05 +0100632 trash->str = pos;
633 trash->len = strlen(pos);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200634 smp->data.u.str = *trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200635 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100636 smp->flags &= ~SMP_F_CONST;
Emeric Brun107ca302010-01-04 16:16:05 +0100637 return 1;
638}
639
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100640/* This function duplicates data and removes the flag "const". */
641int smp_dup(struct sample *smp)
Emeric Brun485479d2010-09-23 18:02:19 +0200642{
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100643 struct chunk *trash;
Emeric Brun485479d2010-09-23 18:02:19 +0200644
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100645 /* If the const flag is not set, we don't need to duplicate the
646 * pattern as it can be modified in place.
647 */
648 if (!(smp->flags & SMP_F_CONST))
649 return 1;
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100650
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200651 switch (smp->data.type) {
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100652 case SMP_T_BOOL:
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100653 case SMP_T_SINT:
654 case SMP_T_ADDR:
655 case SMP_T_IPV4:
656 case SMP_T_IPV6:
657 /* These type are not const. */
658 break;
659 case SMP_T_STR:
660 case SMP_T_BIN:
661 /* Duplicate data. */
662 trash = get_trash_chunk();
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200663 trash->len = smp->data.u.str.len < trash->size ? smp->data.u.str.len : trash->size;
664 memcpy(trash->str, smp->data.u.str.str, trash->len);
665 smp->data.u.str = *trash;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100666 break;
667 default:
668 /* Other cases are unexpected. */
669 return 0;
670 }
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100671
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100672 /* remove const flag */
673 smp->flags &= ~SMP_F_CONST;
Emeric Brun485479d2010-09-23 18:02:19 +0200674 return 1;
675}
676
Thierry FOURNIER0e9af552013-12-14 14:55:04 +0100677int c_none(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100678{
679 return 1;
680}
681
Willy Tarreau342acb42012-04-23 22:03:39 +0200682static int c_str2int(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100683{
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +0200684 const char *str;
685 const char *end;
Emeric Brun107ca302010-01-04 16:16:05 +0100686
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200687 if (smp->data.u.str.len == 0)
Thierry FOURNIER60bb0202014-01-27 18:20:48 +0100688 return 0;
689
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200690 str = smp->data.u.str.str;
691 end = smp->data.u.str.str + smp->data.u.str.len;
Emeric Brun107ca302010-01-04 16:16:05 +0100692
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200693 smp->data.u.sint = read_int64(&str, end);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200694 smp->data.type = SMP_T_SINT;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100695 smp->flags &= ~SMP_F_CONST;
Emeric Brun107ca302010-01-04 16:16:05 +0100696 return 1;
697}
698
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100699static int c_str2meth(struct sample *smp)
700{
701 enum http_meth_t meth;
702 int len;
703
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200704 meth = find_http_meth(smp->data.u.str.str, smp->data.u.str.len);
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100705 if (meth == HTTP_METH_OTHER) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200706 len = smp->data.u.str.len;
707 smp->data.u.meth.str.str = smp->data.u.str.str;
708 smp->data.u.meth.str.len = len;
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100709 }
710 else
711 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200712 smp->data.u.meth.meth = meth;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200713 smp->data.type = SMP_T_METH;
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100714 return 1;
715}
716
717static int c_meth2str(struct sample *smp)
718{
719 int len;
720 enum http_meth_t meth;
721
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200722 if (smp->data.u.meth.meth == HTTP_METH_OTHER) {
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100723 /* The method is unknown. Copy the original pointer. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200724 len = smp->data.u.meth.str.len;
725 smp->data.u.str.str = smp->data.u.meth.str.str;
726 smp->data.u.str.len = len;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200727 smp->data.type = SMP_T_STR;
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100728 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200729 else if (smp->data.u.meth.meth < HTTP_METH_OTHER) {
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100730 /* The method is known, copy the pointer containing the string. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200731 meth = smp->data.u.meth.meth;
732 smp->data.u.str.str = http_known_methods[meth].name;
733 smp->data.u.str.len = http_known_methods[meth].len;
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100734 smp->flags |= SMP_F_CONST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200735 smp->data.type = SMP_T_STR;
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100736 }
737 else {
738 /* Unknown method */
739 return 0;
740 }
741 return 1;
742}
743
Willy Tarreau9700e5c2014-07-15 21:03:26 +0200744static int c_addr2bin(struct sample *smp)
745{
746 struct chunk *chk = get_trash_chunk();
747
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200748 if (smp->data.type == SMP_T_IPV4) {
Willy Tarreau9700e5c2014-07-15 21:03:26 +0200749 chk->len = 4;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200750 memcpy(chk->str, &smp->data.u.ipv4, chk->len);
Willy Tarreau9700e5c2014-07-15 21:03:26 +0200751 }
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200752 else if (smp->data.type == SMP_T_IPV6) {
Willy Tarreau9700e5c2014-07-15 21:03:26 +0200753 chk->len = 16;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200754 memcpy(chk->str, &smp->data.u.ipv6, chk->len);
Willy Tarreau9700e5c2014-07-15 21:03:26 +0200755 }
756 else
757 return 0;
758
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200759 smp->data.u.str = *chk;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200760 smp->data.type = SMP_T_BIN;
Willy Tarreau9700e5c2014-07-15 21:03:26 +0200761 return 1;
762}
763
Willy Tarreaubbfd1a22014-07-15 21:19:08 +0200764static int c_int2bin(struct sample *smp)
765{
766 struct chunk *chk = get_trash_chunk();
767
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200768 *(unsigned long long int *)chk->str = htonll(smp->data.u.sint);
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +0200769 chk->len = 8;
Willy Tarreaubbfd1a22014-07-15 21:19:08 +0200770
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200771 smp->data.u.str = *chk;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200772 smp->data.type = SMP_T_BIN;
Willy Tarreaubbfd1a22014-07-15 21:19:08 +0200773 return 1;
774}
775
Willy Tarreau9700e5c2014-07-15 21:03:26 +0200776
Emeric Brun107ca302010-01-04 16:16:05 +0100777/*****************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +0200778/* Sample casts matrix: */
779/* sample_casts[from type][to type] */
780/* NULL pointer used for impossible sample casts */
Emeric Brun107ca302010-01-04 16:16:05 +0100781/*****************************************************************/
Emeric Brun107ca302010-01-04 16:16:05 +0100782
Thierry FOURNIER8af6ff12013-11-21 10:53:12 +0100783sample_cast_fct sample_casts[SMP_TYPES][SMP_TYPES] = {
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +0200784/* to: ANY BOOL SINT ADDR IPV4 IPV6 STR BIN METH */
785/* from: ANY */ { c_none, c_none, c_none, c_none, c_none, c_none, c_none, c_none, c_none, },
786/* BOOL */ { c_none, c_none, c_none, NULL, NULL, NULL, c_int2str, NULL, NULL, },
Thierry FOURNIERcc4d1712015-07-24 09:04:56 +0200787/* SINT */ { c_none, c_none, c_none, c_int2ip, c_int2ip, c_int2ipv6, c_int2str, c_int2bin, NULL, },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +0200788/* ADDR */ { c_none, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, },
789/* IPV4 */ { c_none, NULL, c_ip2int, c_none, c_none, c_ip2ipv6, c_ip2str, c_addr2bin, NULL, },
Thierry FOURNIERcc4d1712015-07-24 09:04:56 +0200790/* IPV6 */ { c_none, NULL, NULL, c_none, c_ipv62ip,c_none, c_ipv62str, c_addr2bin, NULL, },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +0200791/* STR */ { c_none, c_str2int, c_str2int, c_str2addr, c_str2ip, c_str2ipv6, c_none, c_none, c_str2meth, },
792/* BIN */ { c_none, NULL, NULL, NULL, NULL, NULL, c_bin2str, c_none, c_str2meth, },
793/* METH */ { c_none, NULL, NULL, NULL, NULL, NULL, c_meth2str, c_meth2str, c_none, }
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200794};
Emeric Brun107ca302010-01-04 16:16:05 +0100795
Emeric Brun107ca302010-01-04 16:16:05 +0100796/*
Willy Tarreau12785782012-04-27 21:37:17 +0200797 * Parse a sample expression configuration:
Emeric Brun107ca302010-01-04 16:16:05 +0100798 * fetch keyword followed by format conversion keywords.
Willy Tarreau12785782012-04-27 21:37:17 +0200799 * Returns a pointer on allocated sample expression structure.
Willy Tarreaua4312fa2013-04-02 16:34:32 +0200800 * The caller must have set al->ctx.
Emeric Brun107ca302010-01-04 16:16:05 +0100801 */
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +0100802struct sample_expr *sample_parse_expr(char **str, int *idx, const char *file, int line, char **err_msg, struct arg_list *al)
Emeric Brun107ca302010-01-04 16:16:05 +0100803{
Willy Tarreau833cc792013-07-24 15:34:19 +0200804 const char *begw; /* beginning of word */
805 const char *endw; /* end of word */
806 const char *endt; /* end of term */
Willy Tarreau12785782012-04-27 21:37:17 +0200807 struct sample_expr *expr;
808 struct sample_fetch *fetch;
809 struct sample_conv *conv;
Emeric Brun107ca302010-01-04 16:16:05 +0100810 unsigned long prev_type;
Willy Tarreau833cc792013-07-24 15:34:19 +0200811 char *fkw = NULL;
812 char *ckw = NULL;
Willy Tarreau689a1df2013-12-13 00:40:11 +0100813 int err_arg;
Emeric Brun107ca302010-01-04 16:16:05 +0100814
Willy Tarreau833cc792013-07-24 15:34:19 +0200815 begw = str[*idx];
816 for (endw = begw; *endw && *endw != '(' && *endw != ','; endw++);
817
818 if (endw == begw) {
Willy Tarreau975c1782013-12-12 23:16:54 +0100819 memprintf(err_msg, "missing fetch method");
Emeric Brun107ca302010-01-04 16:16:05 +0100820 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200821 }
Emeric Brun107ca302010-01-04 16:16:05 +0100822
Willy Tarreau833cc792013-07-24 15:34:19 +0200823 /* keep a copy of the current fetch keyword for error reporting */
824 fkw = my_strndup(begw, endw - begw);
Emeric Brun107ca302010-01-04 16:16:05 +0100825
Willy Tarreau833cc792013-07-24 15:34:19 +0200826 fetch = find_sample_fetch(begw, endw - begw);
827 if (!fetch) {
Willy Tarreau975c1782013-12-12 23:16:54 +0100828 memprintf(err_msg, "unknown fetch method '%s'", fkw);
Emeric Brun107ca302010-01-04 16:16:05 +0100829 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200830 }
Emeric Brun107ca302010-01-04 16:16:05 +0100831
Willy Tarreau833cc792013-07-24 15:34:19 +0200832 endt = endw;
833 if (*endt == '(') {
Willy Tarreau689a1df2013-12-13 00:40:11 +0100834 /* look for the end of this term and skip the opening parenthesis */
835 endt = ++endw;
Willy Tarreau833cc792013-07-24 15:34:19 +0200836 while (*endt && *endt != ')')
837 endt++;
838 if (*endt != ')') {
Willy Tarreau975c1782013-12-12 23:16:54 +0100839 memprintf(err_msg, "missing closing ')' after arguments to fetch keyword '%s'", fkw);
Willy Tarreau833cc792013-07-24 15:34:19 +0200840 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200841 }
Emeric Brun485479d2010-09-23 18:02:19 +0200842 }
Emeric Brun107ca302010-01-04 16:16:05 +0100843
Willy Tarreau833cc792013-07-24 15:34:19 +0200844 /* At this point, we have :
845 * - begw : beginning of the keyword
Willy Tarreau689a1df2013-12-13 00:40:11 +0100846 * - endw : end of the keyword, first character not part of keyword
847 * nor the opening parenthesis (so first character of args
848 * if present).
Willy Tarreau833cc792013-07-24 15:34:19 +0200849 * - endt : end of the term (=endw or last parenthesis if args are present)
850 */
851
852 if (fetch->out_type >= SMP_TYPES) {
Willy Tarreau975c1782013-12-12 23:16:54 +0100853 memprintf(err_msg, "returns type of fetch method '%s' is unknown", fkw);
Emeric Brun107ca302010-01-04 16:16:05 +0100854 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200855 }
Emeric Brun107ca302010-01-04 16:16:05 +0100856 prev_type = fetch->out_type;
Willy Tarreau833cc792013-07-24 15:34:19 +0200857
Willy Tarreau12785782012-04-27 21:37:17 +0200858 expr = calloc(1, sizeof(struct sample_expr));
Emeric Brun485479d2010-09-23 18:02:19 +0200859 if (!expr)
860 goto out_error;
Emeric Brun107ca302010-01-04 16:16:05 +0100861
862 LIST_INIT(&(expr->conv_exprs));
863 expr->fetch = fetch;
Willy Tarreau2e845be2012-10-19 19:49:09 +0200864 expr->arg_p = empty_arg_list;
Emeric Brun107ca302010-01-04 16:16:05 +0100865
Willy Tarreau689a1df2013-12-13 00:40:11 +0100866 /* Note that we call the argument parser even with an empty string,
867 * this allows it to automatically create entries for mandatory
868 * implicit arguments (eg: local proxy name).
869 */
870 al->kw = expr->fetch->kw;
871 al->conv = NULL;
872 if (make_arg_list(endw, endt - endw, fetch->arg_mask, &expr->arg_p, err_msg, NULL, &err_arg, al) < 0) {
873 memprintf(err_msg, "fetch method '%s' : %s", fkw, *err_msg);
874 goto out_error;
875 }
Willy Tarreau2e845be2012-10-19 19:49:09 +0200876
Willy Tarreau689a1df2013-12-13 00:40:11 +0100877 if (!expr->arg_p) {
878 expr->arg_p = empty_arg_list;
Emeric Brun485479d2010-09-23 18:02:19 +0200879 }
Willy Tarreau689a1df2013-12-13 00:40:11 +0100880 else if (fetch->val_args && !fetch->val_args(expr->arg_p, err_msg)) {
881 memprintf(err_msg, "invalid args in fetch method '%s' : %s", fkw, *err_msg);
Emeric Brun485479d2010-09-23 18:02:19 +0200882 goto out_error;
Emeric Brun107ca302010-01-04 16:16:05 +0100883 }
884
Willy Tarreau833cc792013-07-24 15:34:19 +0200885 /* Now process the converters if any. We have two supported syntaxes
886 * for the converters, which can be combined :
887 * - comma-delimited list of converters just after the keyword and args ;
888 * - one converter per keyword
889 * The combination allows to have each keyword being a comma-delimited
890 * series of converters.
891 *
892 * We want to process the former first, then the latter. For this we start
893 * from the beginning of the supposed place in the exiting conv chain, which
894 * starts at the last comma (endt).
895 */
896
897 while (1) {
Willy Tarreau12785782012-04-27 21:37:17 +0200898 struct sample_conv_expr *conv_expr;
Emeric Brun107ca302010-01-04 16:16:05 +0100899
Willy Tarreau833cc792013-07-24 15:34:19 +0200900 if (*endt == ')') /* skip last closing parenthesis */
901 endt++;
Emeric Brun107ca302010-01-04 16:16:05 +0100902
Willy Tarreau833cc792013-07-24 15:34:19 +0200903 if (*endt && *endt != ',') {
904 if (ckw)
Willy Tarreau975c1782013-12-12 23:16:54 +0100905 memprintf(err_msg, "missing comma after conv keyword '%s'", ckw);
Willy Tarreau833cc792013-07-24 15:34:19 +0200906 else
Willy Tarreau975c1782013-12-12 23:16:54 +0100907 memprintf(err_msg, "missing comma after fetch keyword '%s'", fkw);
Emeric Brun107ca302010-01-04 16:16:05 +0100908 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200909 }
Emeric Brun107ca302010-01-04 16:16:05 +0100910
Willy Tarreau833cc792013-07-24 15:34:19 +0200911 while (*endt == ',') /* then trailing commas */
912 endt++;
913
914 begw = endt; /* start of conv keyword */
915
916 if (!*begw) {
917 /* none ? skip to next string */
918 (*idx)++;
919 begw = str[*idx];
920 if (!begw || !*begw)
921 break;
922 }
923
924 for (endw = begw; *endw && *endw != '(' && *endw != ','; endw++);
925
926 free(ckw);
927 ckw = my_strndup(begw, endw - begw);
928
929 conv = find_sample_conv(begw, endw - begw);
930 if (!conv) {
931 /* we found an isolated keyword that we don't know, it's not ours */
932 if (begw == str[*idx])
933 break;
Willy Tarreau975c1782013-12-12 23:16:54 +0100934 memprintf(err_msg, "unknown conv method '%s'", ckw);
Willy Tarreau833cc792013-07-24 15:34:19 +0200935 goto out_error;
936 }
Emeric Brun107ca302010-01-04 16:16:05 +0100937
Willy Tarreau833cc792013-07-24 15:34:19 +0200938 endt = endw;
939 if (*endt == '(') {
940 /* look for the end of this term */
941 while (*endt && *endt != ')')
942 endt++;
943 if (*endt != ')') {
Willy Tarreau975c1782013-12-12 23:16:54 +0100944 memprintf(err_msg, "syntax error: missing ')' after conv keyword '%s'", ckw);
Willy Tarreau833cc792013-07-24 15:34:19 +0200945 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200946 }
Willy Tarreau833cc792013-07-24 15:34:19 +0200947 }
948
949 if (conv->in_type >= SMP_TYPES || conv->out_type >= SMP_TYPES) {
Willy Tarreau975c1782013-12-12 23:16:54 +0100950 memprintf(err_msg, "returns type of conv method '%s' is unknown", ckw);
Emeric Brun107ca302010-01-04 16:16:05 +0100951 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200952 }
Emeric Brun107ca302010-01-04 16:16:05 +0100953
954 /* If impossible type conversion */
Willy Tarreau12785782012-04-27 21:37:17 +0200955 if (!sample_casts[prev_type][conv->in_type]) {
Willy Tarreau975c1782013-12-12 23:16:54 +0100956 memprintf(err_msg, "conv method '%s' cannot be applied", ckw);
Emeric Brun107ca302010-01-04 16:16:05 +0100957 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200958 }
Emeric Brun107ca302010-01-04 16:16:05 +0100959
960 prev_type = conv->out_type;
Willy Tarreau12785782012-04-27 21:37:17 +0200961 conv_expr = calloc(1, sizeof(struct sample_conv_expr));
Emeric Brun485479d2010-09-23 18:02:19 +0200962 if (!conv_expr)
963 goto out_error;
Emeric Brun107ca302010-01-04 16:16:05 +0100964
965 LIST_ADDQ(&(expr->conv_exprs), &(conv_expr->list));
966 conv_expr->conv = conv;
967
Willy Tarreau833cc792013-07-24 15:34:19 +0200968 if (endt != endw) {
Willy Tarreaub27c0d32012-04-20 16:04:47 +0200969 int err_arg;
Willy Tarreau21d68a62012-04-20 15:52:36 +0200970
Willy Tarreau9fcb9842012-04-20 14:45:49 +0200971 if (!conv->arg_mask) {
Willy Tarreau975c1782013-12-12 23:16:54 +0100972 memprintf(err_msg, "conv method '%s' does not support any args", ckw);
Emeric Brun485479d2010-09-23 18:02:19 +0200973 goto out_error;
974 }
Willy Tarreau9e92d322010-01-26 17:58:06 +0100975
Willy Tarreaua4312fa2013-04-02 16:34:32 +0200976 al->kw = expr->fetch->kw;
977 al->conv = conv_expr->conv->kw;
Willy Tarreau975c1782013-12-12 23:16:54 +0100978 if (make_arg_list(endw + 1, endt - endw - 1, conv->arg_mask, &conv_expr->arg_p, err_msg, NULL, &err_arg, al) < 0) {
979 memprintf(err_msg, "invalid arg %d in conv method '%s' : %s", err_arg+1, ckw, *err_msg);
Willy Tarreau21d68a62012-04-20 15:52:36 +0200980 goto out_error;
981 }
982
Willy Tarreau2e845be2012-10-19 19:49:09 +0200983 if (!conv_expr->arg_p)
984 conv_expr->arg_p = empty_arg_list;
985
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +0100986 if (conv->val_args && !conv->val_args(conv_expr->arg_p, conv, file, line, err_msg)) {
Willy Tarreau975c1782013-12-12 23:16:54 +0100987 memprintf(err_msg, "invalid args in conv method '%s' : %s", ckw, *err_msg);
Emeric Brun485479d2010-09-23 18:02:19 +0200988 goto out_error;
989 }
990 }
Willy Tarreau833cc792013-07-24 15:34:19 +0200991 else if (ARGM(conv->arg_mask)) {
Willy Tarreau975c1782013-12-12 23:16:54 +0100992 memprintf(err_msg, "missing args for conv method '%s'", ckw);
Emeric Brun485479d2010-09-23 18:02:19 +0200993 goto out_error;
Emeric Brun107ca302010-01-04 16:16:05 +0100994 }
995 }
Emeric Brun485479d2010-09-23 18:02:19 +0200996
Willy Tarreau833cc792013-07-24 15:34:19 +0200997 out:
998 free(fkw);
999 free(ckw);
Emeric Brun107ca302010-01-04 16:16:05 +01001000 return expr;
1001
1002out_error:
Willy Tarreau12785782012-04-27 21:37:17 +02001003 /* TODO: prune_sample_expr(expr); */
Willy Tarreau833cc792013-07-24 15:34:19 +02001004 expr = NULL;
1005 goto out;
Emeric Brun107ca302010-01-04 16:16:05 +01001006}
1007
1008/*
Willy Tarreau12785782012-04-27 21:37:17 +02001009 * Process a fetch + format conversion of defined by the sample expression <expr>
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001010 * on request or response considering the <opt> parameter.
Willy Tarreau12785782012-04-27 21:37:17 +02001011 * Returns a pointer on a typed sample structure containing the result or NULL if
1012 * sample is not found or when format conversion failed.
Emeric Brun107ca302010-01-04 16:16:05 +01001013 * If <p> is not null, function returns results in structure pointed by <p>.
Willy Tarreau12785782012-04-27 21:37:17 +02001014 * If <p> is null, functions returns a pointer on a static sample structure.
Willy Tarreaub8c8f1f2012-04-23 22:38:26 +02001015 *
1016 * Note: the fetch functions are required to properly set the return type. The
1017 * conversion functions must do so too. However the cast functions do not need
1018 * to since they're made to cast mutiple types according to what is required.
Willy Tarreau6bcb0a82014-07-30 08:56:35 +02001019 *
1020 * The caller may indicate in <opt> if it considers the result final or not.
1021 * The caller needs to check the SMP_F_MAY_CHANGE flag in p->flags to verify
1022 * if the result is stable or not, according to the following table :
1023 *
1024 * return MAY_CHANGE FINAL Meaning for the sample
1025 * NULL 0 * Not present and will never be (eg: header)
1026 * NULL 1 0 Not present yet, could change (eg: POST param)
1027 * NULL 1 1 Not present yet, will not change anymore
1028 * smp 0 * Present and will not change (eg: header)
1029 * smp 1 0 Present, may change (eg: request length)
1030 * smp 1 1 Present, last known value (eg: request length)
Emeric Brun107ca302010-01-04 16:16:05 +01001031 */
Willy Tarreau192252e2015-04-04 01:47:55 +02001032struct sample *sample_process(struct proxy *px, struct session *sess,
1033 struct stream *strm, unsigned int opt,
Willy Tarreau12785782012-04-27 21:37:17 +02001034 struct sample_expr *expr, struct sample *p)
Emeric Brun107ca302010-01-04 16:16:05 +01001035{
Willy Tarreau12785782012-04-27 21:37:17 +02001036 struct sample_conv_expr *conv_expr;
Emeric Brun107ca302010-01-04 16:16:05 +01001037
Willy Tarreau18387e22013-07-25 12:02:38 +02001038 if (p == NULL) {
Willy Tarreaub4a88f02012-04-23 21:35:11 +02001039 p = &temp_smp;
Willy Tarreau6c616e02014-06-25 16:56:41 +02001040 memset(p, 0, sizeof(*p));
Willy Tarreau18387e22013-07-25 12:02:38 +02001041 }
Emeric Brun107ca302010-01-04 16:16:05 +01001042
Thierry FOURNIER6879ad32015-05-11 11:54:58 +02001043 p->px = px;
1044 p->sess = sess;
1045 p->strm = strm;
Thierry FOURNIER1d33b882015-05-11 15:25:29 +02001046 p->opt = opt;
Thierry FOURNIER0786d052015-05-11 15:42:45 +02001047 if (!expr->fetch->process(expr->arg_p, p, expr->fetch->kw, expr->fetch->private))
Emeric Brun107ca302010-01-04 16:16:05 +01001048 return NULL;
1049
Emeric Brun107ca302010-01-04 16:16:05 +01001050 list_for_each_entry(conv_expr, &expr->conv_exprs, list) {
Willy Tarreau12e50112012-04-25 17:21:49 +02001051 /* we want to ensure that p->type can be casted into
1052 * conv_expr->conv->in_type. We have 3 possibilities :
1053 * - NULL => not castable.
1054 * - c_none => nothing to do (let's optimize it)
1055 * - other => apply cast and prepare to fail
1056 */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001057 if (!sample_casts[p->data.type][conv_expr->conv->in_type])
Willy Tarreau12e50112012-04-25 17:21:49 +02001058 return NULL;
1059
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001060 if (sample_casts[p->data.type][conv_expr->conv->in_type] != c_none &&
1061 !sample_casts[p->data.type][conv_expr->conv->in_type](p))
Emeric Brun107ca302010-01-04 16:16:05 +01001062 return NULL;
1063
Willy Tarreau12e50112012-04-25 17:21:49 +02001064 /* OK cast succeeded */
1065
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001066 if (!conv_expr->conv->process(conv_expr->arg_p, p, conv_expr->conv->private))
Emeric Brun107ca302010-01-04 16:16:05 +01001067 return NULL;
Emeric Brun107ca302010-01-04 16:16:05 +01001068 }
1069 return p;
1070}
1071
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001072/*
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001073 * Resolve all remaining arguments in proxy <p>. Returns the number of
1074 * errors or 0 if everything is fine.
1075 */
1076int smp_resolve_args(struct proxy *p)
1077{
1078 struct arg_list *cur, *bak;
1079 const char *ctx, *where;
1080 const char *conv_ctx, *conv_pre, *conv_pos;
1081 struct userlist *ul;
Willy Tarreau46947782015-01-19 19:00:58 +01001082 struct my_regex *reg;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001083 struct arg *arg;
1084 int cfgerr = 0;
Willy Tarreau46947782015-01-19 19:00:58 +01001085 int rflags;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001086
1087 list_for_each_entry_safe(cur, bak, &p->conf.args.list, list) {
1088 struct proxy *px;
1089 struct server *srv;
1090 char *pname, *sname;
Willy Tarreau46947782015-01-19 19:00:58 +01001091 char *err;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001092
1093 arg = cur->arg;
1094
1095 /* prepare output messages */
1096 conv_pre = conv_pos = conv_ctx = "";
1097 if (cur->conv) {
1098 conv_ctx = cur->conv;
1099 conv_pre = "conversion keyword '";
1100 conv_pos = "' for ";
1101 }
1102
1103 where = "in";
1104 ctx = "sample fetch keyword";
1105 switch (cur->ctx) {
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001106 case ARGC_STK: where = "in stick rule in"; break;
1107 case ARGC_TRK: where = "in tracking rule in"; break;
1108 case ARGC_LOG: where = "in log-format string in"; break;
1109 case ARGC_LOGSD: where = "in log-format-sd string in"; break;
1110 case ARGC_HRQ: where = "in http-request header format string in"; break;
1111 case ARGC_HRS: where = "in http-response header format string in"; break;
1112 case ARGC_UIF: where = "in unique-id-format string in"; break;
1113 case ARGC_RDR: where = "in redirect format string in"; break;
1114 case ARGC_CAP: where = "in capture rule in"; break;
1115 case ARGC_ACL: ctx = "ACL keyword"; break;
1116 case ARGC_SRV: where = "in server directive in"; break;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001117 }
1118
1119 /* set a few default settings */
1120 px = p;
1121 pname = p->id;
1122
1123 switch (arg->type) {
1124 case ARGT_SRV:
1125 if (!arg->data.str.len) {
1126 Alert("parsing [%s:%d] : missing server name in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1127 cur->file, cur->line,
1128 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
1129 cfgerr++;
1130 continue;
1131 }
1132
1133 /* we support two formats : "bck/srv" and "srv" */
1134 sname = strrchr(arg->data.str.str, '/');
1135
1136 if (sname) {
1137 *sname++ = '\0';
1138 pname = arg->data.str.str;
1139
Willy Tarreau9e0bb102015-05-26 11:24:42 +02001140 px = proxy_be_by_name(pname);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001141 if (!px) {
1142 Alert("parsing [%s:%d] : unable to find proxy '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1143 cur->file, cur->line, pname,
1144 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
1145 cfgerr++;
1146 break;
1147 }
1148 }
1149 else
1150 sname = arg->data.str.str;
1151
1152 srv = findserver(px, sname);
1153 if (!srv) {
1154 Alert("parsing [%s:%d] : unable to find server '%s' in proxy '%s', referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1155 cur->file, cur->line, sname, pname,
1156 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
1157 cfgerr++;
1158 break;
1159 }
1160
1161 free(arg->data.str.str);
1162 arg->data.str.str = NULL;
1163 arg->unresolved = 0;
1164 arg->data.srv = srv;
1165 break;
1166
1167 case ARGT_FE:
1168 if (arg->data.str.len) {
1169 pname = arg->data.str.str;
Willy Tarreau9e0bb102015-05-26 11:24:42 +02001170 px = proxy_fe_by_name(pname);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001171 }
1172
1173 if (!px) {
1174 Alert("parsing [%s:%d] : unable to find frontend '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1175 cur->file, cur->line, pname,
1176 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
1177 cfgerr++;
1178 break;
1179 }
1180
1181 if (!(px->cap & PR_CAP_FE)) {
1182 Alert("parsing [%s:%d] : proxy '%s', referenced in arg %d of %s%s%s%s '%s' %s proxy '%s', has not frontend capability.\n",
1183 cur->file, cur->line, pname,
1184 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
1185 cfgerr++;
1186 break;
1187 }
1188
1189 free(arg->data.str.str);
1190 arg->data.str.str = NULL;
1191 arg->unresolved = 0;
1192 arg->data.prx = px;
1193 break;
1194
1195 case ARGT_BE:
1196 if (arg->data.str.len) {
1197 pname = arg->data.str.str;
Willy Tarreau9e0bb102015-05-26 11:24:42 +02001198 px = proxy_be_by_name(pname);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001199 }
1200
1201 if (!px) {
1202 Alert("parsing [%s:%d] : unable to find backend '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1203 cur->file, cur->line, pname,
1204 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
1205 cfgerr++;
1206 break;
1207 }
1208
1209 if (!(px->cap & PR_CAP_BE)) {
1210 Alert("parsing [%s:%d] : proxy '%s', referenced in arg %d of %s%s%s%s '%s' %s proxy '%s', has not backend capability.\n",
1211 cur->file, cur->line, pname,
1212 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
1213 cfgerr++;
1214 break;
1215 }
1216
1217 free(arg->data.str.str);
1218 arg->data.str.str = NULL;
1219 arg->unresolved = 0;
1220 arg->data.prx = px;
1221 break;
1222
1223 case ARGT_TAB:
1224 if (arg->data.str.len) {
1225 pname = arg->data.str.str;
Willy Tarreaue2dc1fa2015-05-26 12:08:07 +02001226 px = proxy_tbl_by_name(pname);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001227 }
1228
1229 if (!px) {
1230 Alert("parsing [%s:%d] : unable to find table '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1231 cur->file, cur->line, pname,
1232 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
1233 cfgerr++;
1234 break;
1235 }
1236
1237 if (!px->table.size) {
1238 Alert("parsing [%s:%d] : no table in proxy '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1239 cur->file, cur->line, pname,
1240 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
1241 cfgerr++;
1242 break;
1243 }
1244
1245 free(arg->data.str.str);
1246 arg->data.str.str = NULL;
1247 arg->unresolved = 0;
1248 arg->data.prx = px;
1249 break;
1250
1251 case ARGT_USR:
1252 if (!arg->data.str.len) {
1253 Alert("parsing [%s:%d] : missing userlist name in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1254 cur->file, cur->line,
1255 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
1256 cfgerr++;
1257 break;
1258 }
1259
1260 if (p->uri_auth && p->uri_auth->userlist &&
1261 !strcmp(p->uri_auth->userlist->name, arg->data.str.str))
1262 ul = p->uri_auth->userlist;
1263 else
1264 ul = auth_find_userlist(arg->data.str.str);
1265
1266 if (!ul) {
1267 Alert("parsing [%s:%d] : unable to find userlist '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1268 cur->file, cur->line, arg->data.str.str,
1269 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
1270 cfgerr++;
1271 break;
1272 }
1273
1274 free(arg->data.str.str);
1275 arg->data.str.str = NULL;
1276 arg->unresolved = 0;
1277 arg->data.usr = ul;
1278 break;
Willy Tarreau46947782015-01-19 19:00:58 +01001279
1280 case ARGT_REG:
1281 if (!arg->data.str.len) {
1282 Alert("parsing [%s:%d] : missing regex in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1283 cur->file, cur->line,
1284 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
1285 cfgerr++;
1286 continue;
1287 }
1288
1289 reg = calloc(1, sizeof(*reg));
1290 if (!reg) {
1291 Alert("parsing [%s:%d] : not enough memory to build regex in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1292 cur->file, cur->line,
1293 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
1294 cfgerr++;
1295 continue;
1296 }
1297
1298 rflags = 0;
1299 rflags |= (arg->type_flags & ARGF_REG_ICASE) ? REG_ICASE : 0;
1300 err = NULL;
1301
Willy Tarreaud817e462015-01-23 20:23:17 +01001302 if (!regex_comp(arg->data.str.str, reg, !(rflags & REG_ICASE), 1 /* capture substr */, &err)) {
Willy Tarreau46947782015-01-19 19:00:58 +01001303 Alert("parsing [%s:%d] : error in regex '%s' in arg %d of %s%s%s%s '%s' %s proxy '%s' : %s.\n",
1304 cur->file, cur->line,
1305 arg->data.str.str,
1306 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id, err);
1307 cfgerr++;
1308 continue;
1309 }
1310
1311 free(arg->data.str.str);
1312 arg->data.str.str = NULL;
1313 arg->unresolved = 0;
1314 arg->data.reg = reg;
1315 break;
1316
1317
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001318 }
1319
1320 LIST_DEL(&cur->list);
1321 free(cur);
1322 } /* end of args processing */
1323
1324 return cfgerr;
1325}
1326
1327/*
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001328 * Process a fetch + format conversion as defined by the sample expression
1329 * <expr> on request or response considering the <opt> parameter. The output is
Adis Nezirovic79beb242015-07-06 15:41:02 +02001330 * not explicitly set to <smp_type>, but shall be compatible with it as
1331 * specified by 'sample_casts' table. If a stable sample can be fetched, or an
1332 * unstable one when <opt> contains SMP_OPT_FINAL, the sample is converted and
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001333 * returned without the SMP_F_MAY_CHANGE flag. If an unstable sample is found
1334 * and <opt> does not contain SMP_OPT_FINAL, then the sample is returned as-is
1335 * with its SMP_F_MAY_CHANGE flag so that the caller can check it and decide to
1336 * take actions (eg: wait longer). If a sample could not be found or could not
Willy Tarreau6bcb0a82014-07-30 08:56:35 +02001337 * be converted, NULL is returned. The caller MUST NOT use the sample if the
1338 * SMP_F_MAY_CHANGE flag is present, as it is used only as a hint that there is
1339 * still hope to get it after waiting longer, and is not converted to string.
1340 * The possible output combinations are the following :
1341 *
1342 * return MAY_CHANGE FINAL Meaning for the sample
1343 * NULL * * Not present and will never be (eg: header)
1344 * smp 0 * Final value converted (eg: header)
1345 * smp 1 0 Not present yet, may appear later (eg: header)
1346 * smp 1 1 never happens (either flag is cleared on output)
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001347 */
Adis Nezirovic79beb242015-07-06 15:41:02 +02001348struct sample *sample_fetch_as_type(struct proxy *px, struct session *sess,
Willy Tarreau192252e2015-04-04 01:47:55 +02001349 struct stream *strm, unsigned int opt,
Adis Nezirovic79beb242015-07-06 15:41:02 +02001350 struct sample_expr *expr, int smp_type)
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001351{
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001352 struct sample *smp = &temp_smp;
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001353
Willy Tarreau6c616e02014-06-25 16:56:41 +02001354 memset(smp, 0, sizeof(*smp));
1355
Willy Tarreau192252e2015-04-04 01:47:55 +02001356 if (!sample_process(px, sess, strm, opt, expr, smp)) {
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001357 if ((smp->flags & SMP_F_MAY_CHANGE) && !(opt & SMP_OPT_FINAL))
1358 return smp;
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001359 return NULL;
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001360 }
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001361
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001362 if (!sample_casts[smp->data.type][smp_type])
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001363 return NULL;
1364
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001365 if (!sample_casts[smp->data.type][smp_type](smp))
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001366 return NULL;
1367
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001368 smp->flags &= ~SMP_F_MAY_CHANGE;
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001369 return smp;
1370}
1371
Emeric Brun107ca302010-01-04 16:16:05 +01001372/*****************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +02001373/* Sample format convert functions */
Willy Tarreaub8c8f1f2012-04-23 22:38:26 +02001374/* These functions set the data type on return. */
Emeric Brun107ca302010-01-04 16:16:05 +01001375/*****************************************************************/
1376
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001377#ifdef DEBUG_EXPR
1378static int sample_conv_debug(const struct arg *arg_p, struct sample *smp, void *private)
1379{
1380 int i;
1381 struct sample tmp;
1382
1383 if (!(global.mode & MODE_QUIET) || (global.mode & (MODE_VERBOSE | MODE_STARTING))) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001384 fprintf(stderr, "[debug converter] type: %s ", smp_to_type[smp->data.type]);
1385 if (!sample_casts[smp->data.type][SMP_T_STR]) {
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001386 fprintf(stderr, "(undisplayable)");
1387 } else {
1388
1389 /* Copy sample fetch. This put the sample as const, the
1390 * cast will copy data if a transformation is required.
1391 */
1392 memcpy(&tmp, smp, sizeof(struct sample));
1393 tmp.flags = SMP_F_CONST;
1394
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001395 if (!sample_casts[smp->data.type][SMP_T_STR](&tmp))
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001396 fprintf(stderr, "(undisplayable)");
1397
1398 else {
1399 /* Display the displayable chars*. */
1400 fprintf(stderr, "<");
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001401 for (i = 0; i < tmp.data.u.str.len; i++) {
1402 if (isprint(tmp.data.u.str.str[i]))
1403 fputc(tmp.data.u.str.str[i], stderr);
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001404 else
1405 fputc('.', stderr);
1406 }
1407 }
1408 fprintf(stderr, ">\n");
1409 }
1410 }
1411 return 1;
1412}
1413#endif
1414
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001415static int sample_conv_bin2base64(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brun53d1a982014-04-30 18:21:37 +02001416{
1417 struct chunk *trash = get_trash_chunk();
1418 int b64_len;
1419
1420 trash->len = 0;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001421 b64_len = a2base64(smp->data.u.str.str, smp->data.u.str.len, trash->str, trash->size);
Emeric Brun53d1a982014-04-30 18:21:37 +02001422 if (b64_len < 0)
1423 return 0;
1424
1425 trash->len = b64_len;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001426 smp->data.u.str = *trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001427 smp->data.type = SMP_T_STR;
Emeric Brun53d1a982014-04-30 18:21:37 +02001428 smp->flags &= ~SMP_F_CONST;
1429 return 1;
1430}
1431
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001432static int sample_conv_bin2hex(const struct arg *arg_p, struct sample *smp, void *private)
Thierry FOURNIER2f49d6d2014-03-12 15:01:52 +01001433{
1434 struct chunk *trash = get_trash_chunk();
1435 unsigned char c;
1436 int ptr = 0;
1437
1438 trash->len = 0;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001439 while (ptr < smp->data.u.str.len && trash->len <= trash->size - 2) {
1440 c = smp->data.u.str.str[ptr++];
Thierry FOURNIER2f49d6d2014-03-12 15:01:52 +01001441 trash->str[trash->len++] = hextab[(c >> 4) & 0xF];
1442 trash->str[trash->len++] = hextab[c & 0xF];
1443 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001444 smp->data.u.str = *trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001445 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01001446 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIER2f49d6d2014-03-12 15:01:52 +01001447 return 1;
1448}
1449
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001450/* hashes the binary input into a 32-bit unsigned int */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001451static int sample_conv_djb2(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001452{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001453 smp->data.u.sint = hash_djb2(smp->data.u.str.str, smp->data.u.str.len);
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001454 if (arg_p && arg_p->data.sint)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001455 smp->data.u.sint = full_hash(smp->data.u.sint);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001456 smp->data.type = SMP_T_SINT;
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001457 return 1;
1458}
1459
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001460static int sample_conv_str2lower(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brun107ca302010-01-04 16:16:05 +01001461{
1462 int i;
1463
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01001464 if (!smp_dup(smp))
1465 return 0;
1466
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001467 if (!smp->data.u.str.size)
Emeric Brun485479d2010-09-23 18:02:19 +02001468 return 0;
1469
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001470 for (i = 0; i < smp->data.u.str.len; i++) {
1471 if ((smp->data.u.str.str[i] >= 'A') && (smp->data.u.str.str[i] <= 'Z'))
1472 smp->data.u.str.str[i] += 'a' - 'A';
Emeric Brun107ca302010-01-04 16:16:05 +01001473 }
1474 return 1;
1475}
1476
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001477static int sample_conv_str2upper(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brun107ca302010-01-04 16:16:05 +01001478{
1479 int i;
1480
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01001481 if (!smp_dup(smp))
1482 return 0;
1483
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001484 if (!smp->data.u.str.size)
Emeric Brun485479d2010-09-23 18:02:19 +02001485 return 0;
1486
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001487 for (i = 0; i < smp->data.u.str.len; i++) {
1488 if ((smp->data.u.str.str[i] >= 'a') && (smp->data.u.str.str[i] <= 'z'))
1489 smp->data.u.str.str[i] += 'A' - 'a';
Emeric Brun107ca302010-01-04 16:16:05 +01001490 }
1491 return 1;
1492}
1493
Willy Tarreauf9954102012-04-20 14:03:29 +02001494/* takes the netmask in arg_p */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001495static int sample_conv_ipmask(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud31d6eb2010-01-26 18:01:41 +01001496{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001497 smp->data.u.ipv4.s_addr &= arg_p->data.ipv4.s_addr;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001498 smp->data.type = SMP_T_IPV4;
Willy Tarreaud31d6eb2010-01-26 18:01:41 +01001499 return 1;
1500}
1501
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001502/* takes an UINT value on input supposed to represent the time since EPOCH,
1503 * adds an optional offset found in args[1] and emits a string representing
1504 * the local time in the format specified in args[1] using strftime().
1505 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001506static int sample_conv_ltime(const struct arg *args, struct sample *smp, void *private)
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001507{
1508 struct chunk *temp;
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001509 /* With high numbers, the date returned can be negative, the 55 bits mask prevent this. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001510 time_t curr_date = smp->data.u.sint & 0x007fffffffffffffLL;
Thierry FOURNIERfac9ccf2015-07-08 00:15:20 +02001511 struct tm *tm;
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001512
1513 /* add offset */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001514 if (args[1].type == ARGT_SINT)
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001515 curr_date += args[1].data.sint;
1516
Thierry FOURNIERfac9ccf2015-07-08 00:15:20 +02001517 tm = localtime(&curr_date);
1518 if (!tm)
1519 return 0;
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001520 temp = get_trash_chunk();
Thierry FOURNIERfac9ccf2015-07-08 00:15:20 +02001521 temp->len = strftime(temp->str, temp->size, args[0].data.str.str, tm);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001522 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001523 smp->data.type = SMP_T_STR;
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001524 return 1;
1525}
1526
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001527/* hashes the binary input into a 32-bit unsigned int */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001528static int sample_conv_sdbm(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001529{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001530 smp->data.u.sint = hash_sdbm(smp->data.u.str.str, smp->data.u.str.len);
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001531 if (arg_p && arg_p->data.sint)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001532 smp->data.u.sint = full_hash(smp->data.u.sint);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001533 smp->data.type = SMP_T_SINT;
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001534 return 1;
1535}
1536
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001537/* takes an UINT value on input supposed to represent the time since EPOCH,
1538 * adds an optional offset found in args[1] and emits a string representing
1539 * the UTC date in the format specified in args[1] using strftime().
1540 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001541static int sample_conv_utime(const struct arg *args, struct sample *smp, void *private)
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001542{
1543 struct chunk *temp;
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001544 /* With high numbers, the date returned can be negative, the 55 bits mask prevent this. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001545 time_t curr_date = smp->data.u.sint & 0x007fffffffffffffLL;
Thierry FOURNIERfac9ccf2015-07-08 00:15:20 +02001546 struct tm *tm;
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001547
1548 /* add offset */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001549 if (args[1].type == ARGT_SINT)
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001550 curr_date += args[1].data.sint;
1551
Thierry FOURNIERfac9ccf2015-07-08 00:15:20 +02001552 tm = gmtime(&curr_date);
1553 if (!tm)
1554 return 0;
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001555 temp = get_trash_chunk();
Thierry FOURNIERfac9ccf2015-07-08 00:15:20 +02001556 temp->len = strftime(temp->str, temp->size, args[0].data.str.str, tm);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001557 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001558 smp->data.type = SMP_T_STR;
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001559 return 1;
1560}
1561
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001562/* hashes the binary input into a 32-bit unsigned int */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001563static int sample_conv_wt6(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001564{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001565 smp->data.u.sint = hash_wt6(smp->data.u.str.str, smp->data.u.str.len);
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001566 if (arg_p && arg_p->data.sint)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001567 smp->data.u.sint = full_hash(smp->data.u.sint);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001568 smp->data.type = SMP_T_SINT;
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001569 return 1;
1570}
1571
Willy Tarreau80599772015-01-20 19:35:24 +01001572/* hashes the binary input into a 32-bit unsigned int */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001573static int sample_conv_crc32(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau80599772015-01-20 19:35:24 +01001574{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001575 smp->data.u.sint = hash_crc32(smp->data.u.str.str, smp->data.u.str.len);
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001576 if (arg_p && arg_p->data.sint)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001577 smp->data.u.sint = full_hash(smp->data.u.sint);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001578 smp->data.type = SMP_T_SINT;
Willy Tarreau80599772015-01-20 19:35:24 +01001579 return 1;
1580}
1581
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001582/* This function escape special json characters. The returned string can be
1583 * safely set between two '"' and used as json string. The json string is
1584 * defined like this:
1585 *
1586 * any Unicode character except '"' or '\' or control character
1587 * \", \\, \/, \b, \f, \n, \r, \t, \u + four-hex-digits
1588 *
1589 * The enum input_type contain all the allowed mode for decoding the input
1590 * string.
1591 */
1592enum input_type {
1593 IT_ASCII = 0,
1594 IT_UTF8,
1595 IT_UTF8S,
1596 IT_UTF8P,
1597 IT_UTF8PS,
1598};
1599static int sample_conv_json_check(struct arg *arg, struct sample_conv *conv,
1600 const char *file, int line, char **err)
1601{
1602 if (!arg) {
1603 memprintf(err, "Unexpected empty arg list");
1604 return 0;
1605 }
1606
1607 if (arg->type != ARGT_STR) {
1608 memprintf(err, "Unexpected arg type");
1609 return 0;
1610 }
1611
1612 if (strcmp(arg->data.str.str, "") == 0) {
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001613 arg->type = ARGT_SINT;
1614 arg->data.sint = IT_ASCII;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001615 return 1;
1616 }
1617
1618 else if (strcmp(arg->data.str.str, "ascii") == 0) {
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001619 arg->type = ARGT_SINT;
1620 arg->data.sint = IT_ASCII;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001621 return 1;
1622 }
1623
1624 else if (strcmp(arg->data.str.str, "utf8") == 0) {
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001625 arg->type = ARGT_SINT;
1626 arg->data.sint = IT_UTF8;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001627 return 1;
1628 }
1629
1630 else if (strcmp(arg->data.str.str, "utf8s") == 0) {
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001631 arg->type = ARGT_SINT;
1632 arg->data.sint = IT_UTF8S;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001633 return 1;
1634 }
1635
1636 else if (strcmp(arg->data.str.str, "utf8p") == 0) {
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001637 arg->type = ARGT_SINT;
1638 arg->data.sint = IT_UTF8P;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001639 return 1;
1640 }
1641
1642 else if (strcmp(arg->data.str.str, "utf8ps") == 0) {
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001643 arg->type = ARGT_SINT;
1644 arg->data.sint = IT_UTF8PS;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001645 return 1;
1646 }
1647
1648 memprintf(err, "Unexpected input code type at file '%s', line %d. "
1649 "Allowed value are 'ascii', 'utf8', 'utf8p' and 'utf8pp'", file, line);
1650 return 0;
1651}
1652
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001653static int sample_conv_json(const struct arg *arg_p, struct sample *smp, void *private)
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001654{
1655 struct chunk *temp;
1656 char _str[7]; /* \u + 4 hex digit + null char for sprintf. */
1657 const char *str;
1658 int len;
1659 enum input_type input_type = IT_ASCII;
1660 unsigned int c;
1661 unsigned int ret;
1662 char *p;
1663
1664 if (arg_p)
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001665 input_type = arg_p->data.sint;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001666
1667 temp = get_trash_chunk();
1668 temp->len = 0;
1669
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001670 p = smp->data.u.str.str;
1671 while (p < smp->data.u.str.str + smp->data.u.str.len) {
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001672
1673 if (input_type == IT_ASCII) {
1674 /* Read input as ASCII. */
1675 c = *(unsigned char *)p;
1676 p++;
1677 }
1678 else {
1679 /* Read input as UTF8. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001680 ret = utf8_next(p, smp->data.u.str.len - ( p - smp->data.u.str.str ), &c);
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001681 p += utf8_return_length(ret);
1682
1683 if (input_type == IT_UTF8 && utf8_return_code(ret) != UTF8_CODE_OK)
1684 return 0;
1685 if (input_type == IT_UTF8S && utf8_return_code(ret) != UTF8_CODE_OK)
1686 continue;
1687 if (input_type == IT_UTF8P && utf8_return_code(ret) & (UTF8_CODE_INVRANGE|UTF8_CODE_BADSEQ))
1688 return 0;
1689 if (input_type == IT_UTF8PS && utf8_return_code(ret) & (UTF8_CODE_INVRANGE|UTF8_CODE_BADSEQ))
1690 continue;
1691
1692 /* Check too big values. */
1693 if ((unsigned int)c > 0xffff) {
1694 if (input_type == IT_UTF8 || input_type == IT_UTF8P)
1695 return 0;
1696 continue;
1697 }
1698 }
1699
1700 /* Convert character. */
1701 if (c == '"') {
1702 len = 2;
1703 str = "\\\"";
1704 }
1705 else if (c == '\\') {
1706 len = 2;
1707 str = "\\\\";
1708 }
1709 else if (c == '/') {
1710 len = 2;
1711 str = "\\/";
1712 }
1713 else if (c == '\b') {
1714 len = 2;
1715 str = "\\b";
1716 }
1717 else if (c == '\f') {
1718 len = 2;
1719 str = "\\f";
1720 }
1721 else if (c == '\r') {
1722 len = 2;
1723 str = "\\r";
1724 }
1725 else if (c == '\n') {
1726 len = 2;
1727 str = "\\n";
1728 }
1729 else if (c == '\t') {
1730 len = 2;
1731 str = "\\t";
1732 }
1733 else if (c > 0xff || !isprint(c)) {
1734 /* isprint generate a segfault if c is too big. The man says that
1735 * c must have the value of an unsigned char or EOF.
1736 */
1737 len = 6;
1738 _str[0] = '\\';
1739 _str[1] = 'u';
1740 snprintf(&_str[2], 5, "%04x", (unsigned short)c);
1741 str = _str;
1742 }
1743 else {
1744 len = 1;
1745 str = (char *)&c;
1746 }
1747
1748 /* Check length */
1749 if (temp->len + len > temp->size)
1750 return 0;
1751
1752 /* Copy string. */
1753 memcpy(temp->str + temp->len, str, len);
1754 temp->len += len;
1755 }
1756
1757 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001758 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001759 smp->data.type = SMP_T_STR;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001760
1761 return 1;
1762}
1763
Emeric Brun54c4ac82014-11-03 15:32:43 +01001764/* This sample function is designed to extract some bytes from an input buffer.
1765 * First arg is the offset.
1766 * Optional second arg is the length to truncate */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001767static int sample_conv_bytes(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brun54c4ac82014-11-03 15:32:43 +01001768{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001769 if (smp->data.u.str.len <= arg_p[0].data.sint) {
1770 smp->data.u.str.len = 0;
Emeric Brun54c4ac82014-11-03 15:32:43 +01001771 return 1;
1772 }
1773
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001774 if (smp->data.u.str.size)
1775 smp->data.u.str.size -= arg_p[0].data.sint;
1776 smp->data.u.str.len -= arg_p[0].data.sint;
1777 smp->data.u.str.str += arg_p[0].data.sint;
Emeric Brun54c4ac82014-11-03 15:32:43 +01001778
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001779 if ((arg_p[1].type == ARGT_SINT) && (arg_p[1].data.sint < smp->data.u.str.len))
1780 smp->data.u.str.len = arg_p[1].data.sint;
Emeric Brun54c4ac82014-11-03 15:32:43 +01001781
1782 return 1;
1783}
1784
Emeric Brunf399b0d2014-11-03 17:07:03 +01001785static int sample_conv_field_check(struct arg *args, struct sample_conv *conv,
1786 const char *file, int line, char **err)
1787{
1788 struct arg *arg = args;
1789
1790 if (!arg) {
1791 memprintf(err, "Unexpected empty arg list");
1792 return 0;
1793 }
1794
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001795 if (arg->type != ARGT_SINT) {
Emeric Brunf399b0d2014-11-03 17:07:03 +01001796 memprintf(err, "Unexpected arg type");
1797 return 0;
1798 }
1799
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001800 if (!arg->data.sint) {
Emeric Brunf399b0d2014-11-03 17:07:03 +01001801 memprintf(err, "Unexpected value 0 for index");
1802 return 0;
1803 }
1804
1805 arg++;
1806
1807 if (arg->type != ARGT_STR) {
1808 memprintf(err, "Unexpected arg type");
1809 return 0;
1810 }
1811
1812 if (!arg->data.str.len) {
1813 memprintf(err, "Empty separators list");
1814 return 0;
1815 }
1816
1817 return 1;
1818}
1819
1820/* This sample function is designed to a return selected part of a string (field).
1821 * First arg is the index of the field (start at 1)
1822 * Second arg is a char list of separators (type string)
1823 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001824static int sample_conv_field(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brunf399b0d2014-11-03 17:07:03 +01001825{
1826 unsigned int field;
1827 char *start, *end;
1828 int i;
1829
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001830 if (!arg_p[0].data.sint)
Emeric Brunf399b0d2014-11-03 17:07:03 +01001831 return 0;
1832
1833 field = 1;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001834 end = start = smp->data.u.str.str;
1835 while (end - smp->data.u.str.str < smp->data.u.str.len) {
Emeric Brunf399b0d2014-11-03 17:07:03 +01001836
1837 for (i = 0 ; i < arg_p[1].data.str.len ; i++) {
1838 if (*end == arg_p[1].data.str.str[i]) {
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001839 if (field == arg_p[0].data.sint)
Emeric Brunf399b0d2014-11-03 17:07:03 +01001840 goto found;
1841 start = end+1;
1842 field++;
1843 break;
1844 }
1845 }
1846 end++;
1847 }
1848
1849 /* Field not found */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001850 if (field != arg_p[0].data.sint) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001851 smp->data.u.str.len = 0;
Emeric Brunf399b0d2014-11-03 17:07:03 +01001852 return 1;
1853 }
1854found:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001855 smp->data.u.str.len = end - start;
Emeric Brunf399b0d2014-11-03 17:07:03 +01001856 /* If ret string is len 0, no need to
1857 change pointers or to update size */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001858 if (!smp->data.u.str.len)
Emeric Brunf399b0d2014-11-03 17:07:03 +01001859 return 1;
1860
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001861 smp->data.u.str.str = start;
Emeric Brunf399b0d2014-11-03 17:07:03 +01001862
1863 /* Compute remaining size if needed
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001864 Note: smp->data.u.str.size cannot be set to 0 */
1865 if (smp->data.u.str.size)
1866 smp->data.u.str.size -= start - smp->data.u.str.str;
Emeric Brunf399b0d2014-11-03 17:07:03 +01001867
1868 return 1;
1869}
1870
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01001871/* This sample function is designed to return a word from a string.
1872 * First arg is the index of the word (start at 1)
1873 * Second arg is a char list of words separators (type string)
1874 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001875static int sample_conv_word(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01001876{
1877 unsigned int word;
1878 char *start, *end;
1879 int i, issep, inword;
1880
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001881 if (!arg_p[0].data.sint)
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01001882 return 0;
1883
1884 word = 0;
1885 inword = 0;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001886 end = start = smp->data.u.str.str;
1887 while (end - smp->data.u.str.str < smp->data.u.str.len) {
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01001888 issep = 0;
1889 for (i = 0 ; i < arg_p[1].data.str.len ; i++) {
1890 if (*end == arg_p[1].data.str.str[i]) {
1891 issep = 1;
1892 break;
1893 }
1894 }
1895 if (!inword) {
1896 if (!issep) {
1897 word++;
1898 start = end;
1899 inword = 1;
1900 }
1901 }
1902 else if (issep) {
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001903 if (word == arg_p[0].data.sint)
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01001904 goto found;
1905 inword = 0;
1906 }
1907 end++;
1908 }
1909
1910 /* Field not found */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001911 if (word != arg_p[0].data.sint) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001912 smp->data.u.str.len = 0;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01001913 return 1;
1914 }
1915found:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001916 smp->data.u.str.len = end - start;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01001917 /* If ret string is len 0, no need to
1918 change pointers or to update size */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001919 if (!smp->data.u.str.len)
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01001920 return 1;
1921
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001922 smp->data.u.str.str = start;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01001923
1924 /* Compute remaining size if needed
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001925 Note: smp->data.u.str.size cannot be set to 0 */
1926 if (smp->data.u.str.size)
1927 smp->data.u.str.size -= start - smp->data.u.str.str;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01001928
1929 return 1;
1930}
1931
Willy Tarreau7eda8492015-01-20 19:47:06 +01001932static int sample_conv_regsub_check(struct arg *args, struct sample_conv *conv,
1933 const char *file, int line, char **err)
1934{
1935 struct arg *arg = args;
1936 char *p;
1937 int len;
1938
1939 /* arg0 is a regex, it uses type_flag for ICASE and global match */
1940 arg[0].type_flags = 0;
1941
1942 if (arg[2].type != ARGT_STR)
1943 return 1;
1944
1945 p = arg[2].data.str.str;
1946 len = arg[2].data.str.len;
1947 while (len) {
1948 if (*p == 'i') {
1949 arg[0].type_flags |= ARGF_REG_ICASE;
1950 }
1951 else if (*p == 'g') {
1952 arg[0].type_flags |= ARGF_REG_GLOB;
1953 }
1954 else {
1955 memprintf(err, "invalid regex flag '%c', only 'i' and 'g' are supported", *p);
1956 return 0;
1957 }
1958 p++;
1959 len--;
1960 }
1961 return 1;
1962}
1963
1964/* This sample function is designed to do the equivalent of s/match/replace/ on
1965 * the input string. It applies a regex and restarts from the last matched
1966 * location until nothing matches anymore. First arg is the regex to apply to
1967 * the input string, second arg is the replacement expression.
1968 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001969static int sample_conv_regsub(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau7eda8492015-01-20 19:47:06 +01001970{
1971 char *start, *end;
1972 struct my_regex *reg = arg_p[0].data.reg;
1973 regmatch_t pmatch[MAX_MATCH];
1974 struct chunk *trash = get_trash_chunk();
1975 int flag, max;
1976 int found;
1977
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001978 start = smp->data.u.str.str;
1979 end = start + smp->data.u.str.len;
Willy Tarreau7eda8492015-01-20 19:47:06 +01001980
1981 flag = 0;
1982 while (1) {
1983 /* check for last round which is used to copy remaining parts
1984 * when not running in global replacement mode.
1985 */
1986 found = 0;
1987 if ((arg_p[0].type_flags & ARGF_REG_GLOB) || !(flag & REG_NOTBOL)) {
1988 /* Note: we can have start == end on empty strings or at the end */
1989 found = regex_exec_match2(reg, start, end - start, MAX_MATCH, pmatch, flag);
1990 }
1991
1992 if (!found)
1993 pmatch[0].rm_so = end - start;
1994
1995 /* copy the heading non-matching part (which may also be the tail if nothing matches) */
1996 max = trash->size - trash->len;
1997 if (max && pmatch[0].rm_so > 0) {
1998 if (max > pmatch[0].rm_so)
1999 max = pmatch[0].rm_so;
2000 memcpy(trash->str + trash->len, start, max);
2001 trash->len += max;
2002 }
2003
2004 if (!found)
2005 break;
2006
2007 /* replace the matching part */
2008 max = trash->size - trash->len;
2009 if (max) {
2010 if (max > arg_p[1].data.str.len)
2011 max = arg_p[1].data.str.len;
2012 memcpy(trash->str + trash->len, arg_p[1].data.str.str, max);
2013 trash->len += max;
2014 }
2015
2016 /* stop here if we're done with this string */
2017 if (start >= end)
2018 break;
2019
2020 /* We have a special case for matches of length 0 (eg: "x*y*").
2021 * These ones are considered to match in front of a character,
2022 * so we have to copy that character and skip to the next one.
2023 */
2024 if (!pmatch[0].rm_eo) {
2025 if (trash->len < trash->size)
2026 trash->str[trash->len++] = start[pmatch[0].rm_eo];
2027 pmatch[0].rm_eo++;
2028 }
2029
2030 start += pmatch[0].rm_eo;
2031 flag |= REG_NOTBOL;
2032 }
2033
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002034 smp->data.u.str = *trash;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002035 return 1;
2036}
2037
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002038/* This function check an operator entry. It expects a string.
2039 * The string can be an integer or a variable name.
2040 */
2041static int check_operator(struct arg *args, struct sample_conv *conv,
2042 const char *file, int line, char **err)
2043{
2044 const char *str;
2045 const char *end;
2046
2047 /* Try to decode a variable. */
2048 if (vars_check_arg(&args[0], NULL))
2049 return 1;
2050
2051 /* Try to convert an integer */
2052 str = args[0].data.str.str;
2053 end = str + strlen(str);
2054 args[0].data.sint = read_int64(&str, end);
2055 if (*str != '\0') {
2056 memprintf(err, "expects an integer or a variable name");
2057 return 0;
2058 }
2059 args[0].type = ARGT_SINT;
2060 return 1;
2061}
2062
2063/* This fucntion returns a sample struct filled with a arg content.
2064 * If the arg contain an integer, the integer is returned in the
2065 * sample. If the arg contains a variable descriptor, it returns the
2066 * variable value.
2067 *
2068 * This function returns 0 if an error occurs, otherwise it returns 1.
2069 */
2070static inline int sample_conv_var2smp(const struct arg *arg, struct stream *strm, struct sample *smp)
2071{
2072 switch (arg->type) {
2073 case ARGT_SINT:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002074 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002075 smp->data.u.sint = arg->data.sint;
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002076 return 1;
2077 case ARGT_VAR:
2078 if (!vars_get_by_desc(&arg->data.var, strm, smp))
2079 return 0;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002080 if (!sample_casts[smp->data.type][SMP_T_SINT])
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002081 return 0;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002082 if (!sample_casts[smp->data.type][SMP_T_SINT](smp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002083 return 0;
2084 return 1;
2085 default:
2086 return 0;
2087 }
2088}
2089
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002090/* Takes a SINT on input, applies a binary twos complement and returns the SINT
Willy Tarreau97707872015-01-27 15:12:13 +01002091 * result.
2092 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002093static int sample_conv_binary_cpl(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002094{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002095 smp->data.u.sint = ~smp->data.u.sint;
Willy Tarreau97707872015-01-27 15:12:13 +01002096 return 1;
2097}
2098
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002099/* Takes a SINT on input, applies a binary "and" with the SINT directly in
2100 * arg_p or in the varaible described in arg_p, and returns the SINT result.
Willy Tarreau97707872015-01-27 15:12:13 +01002101 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002102static int sample_conv_binary_and(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002103{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002104 struct sample tmp;
2105
2106 if (!sample_conv_var2smp(arg_p, smp->strm, &tmp))
2107 return 0;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002108 smp->data.u.sint &= tmp.data.u.sint;
Willy Tarreau97707872015-01-27 15:12:13 +01002109 return 1;
2110}
2111
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002112/* Takes a SINT on input, applies a binary "or" with the SINT directly in
2113 * arg_p or in the varaible described in arg_p, and returns the SINT result.
Willy Tarreau97707872015-01-27 15:12:13 +01002114 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002115static int sample_conv_binary_or(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002116{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002117 struct sample tmp;
2118
2119 if (!sample_conv_var2smp(arg_p, smp->strm, &tmp))
2120 return 0;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002121 smp->data.u.sint |= tmp.data.u.sint;
Willy Tarreau97707872015-01-27 15:12:13 +01002122 return 1;
2123}
2124
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002125/* Takes a SINT on input, applies a binary "xor" with the SINT directly in
2126 * arg_p or in the varaible described in arg_p, and returns the SINT result.
Willy Tarreau97707872015-01-27 15:12:13 +01002127 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002128static int sample_conv_binary_xor(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002129{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002130 struct sample tmp;
2131
2132 if (!sample_conv_var2smp(arg_p, smp->strm, &tmp))
2133 return 0;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002134 smp->data.u.sint ^= tmp.data.u.sint;
Willy Tarreau97707872015-01-27 15:12:13 +01002135 return 1;
2136}
2137
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002138static inline long long int arith_add(long long int a, long long int b)
2139{
2140 /* Prevent overflow and makes capped calculus.
2141 * We must ensure that the check calculus doesn't
2142 * exceed the signed 64 bits limits.
2143 *
2144 * +----------+----------+
2145 * | a<0 | a>=0 |
2146 * +------+----------+----------+
2147 * | b<0 | MIN-a>b | no check |
2148 * +------+----------+----------+
2149 * | b>=0 | no check | MAX-a<b |
2150 * +------+----------+----------+
2151 */
2152 if ((a ^ b) >= 0) {
2153 /* signs are differents. */
2154 if (a < 0) {
2155 if (LLONG_MIN - a > b)
2156 return LLONG_MIN;
2157 }
2158 if (LLONG_MAX - a < b)
2159 return LLONG_MAX;
2160 }
2161 return a + b;
2162}
2163
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002164/* Takes a SINT on input, applies an arithmetic "add" with the SINT directly in
2165 * arg_p or in the varaible described in arg_p, and returns the SINT result.
Willy Tarreau97707872015-01-27 15:12:13 +01002166 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002167static int sample_conv_arith_add(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002168{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002169 struct sample tmp;
2170
2171 if (!sample_conv_var2smp(arg_p, smp->strm, &tmp))
2172 return 0;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002173 smp->data.u.sint = arith_add(smp->data.u.sint, tmp.data.u.sint);
Willy Tarreau97707872015-01-27 15:12:13 +01002174 return 1;
2175}
2176
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002177/* Takes a SINT on input, applies an arithmetic "sub" with the SINT directly in
2178 * arg_p or in the varaible described in arg_p, and returns the SINT result.
Willy Tarreau97707872015-01-27 15:12:13 +01002179 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002180static int sample_conv_arith_sub(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002181 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002182{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002183 struct sample tmp;
2184
2185 if (!sample_conv_var2smp(arg_p, smp->strm, &tmp))
2186 return 0;
2187
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002188 /* We cannot represent -LLONG_MIN because abs(LLONG_MIN) is greater
2189 * than abs(LLONG_MAX). So, the following code use LLONG_MAX in place
2190 * of -LLONG_MIN and correct the result.
2191 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002192 if (tmp.data.u.sint == LLONG_MIN) {
2193 smp->data.u.sint = arith_add(smp->data.u.sint, LLONG_MAX);
2194 if (smp->data.u.sint < LLONG_MAX)
2195 smp->data.u.sint++;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002196 return 1;
2197 }
2198
2199 /* standard substraction: we use the "add" function and negate
2200 * the second operand.
2201 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002202 smp->data.u.sint = arith_add(smp->data.u.sint, -tmp.data.u.sint);
Willy Tarreau97707872015-01-27 15:12:13 +01002203 return 1;
2204}
2205
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002206/* Takes a SINT on input, applies an arithmetic "mul" with the SINT directly in
2207 * arg_p or in the varaible described in arg_p, and returns the SINT result.
2208 * If the result makes an overflow, then the largest possible quantity is
2209 * returned.
Willy Tarreau97707872015-01-27 15:12:13 +01002210 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002211static int sample_conv_arith_mul(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002212 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002213{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002214 struct sample tmp;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002215 long long int c;
2216
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002217 if (!sample_conv_var2smp(arg_p, smp->strm, &tmp))
2218 return 0;
2219
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002220 /* prevent divide by 0 during the check */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002221 if (!smp->data.u.sint || !tmp.data.u.sint) {
2222 smp->data.u.sint = 0;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002223 return 1;
2224 }
2225
2226 /* The multiply between LLONG_MIN and -1 returns a
2227 * "floting point exception".
2228 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002229 if (smp->data.u.sint == LLONG_MIN && tmp.data.u.sint == -1) {
2230 smp->data.u.sint = LLONG_MAX;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002231 return 1;
2232 }
2233
2234 /* execute standard multiplication. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002235 c = smp->data.u.sint * tmp.data.u.sint;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002236
2237 /* check for overflow and makes capped multiply. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002238 if (smp->data.u.sint != c / tmp.data.u.sint) {
2239 if ((smp->data.u.sint < 0) == (tmp.data.u.sint < 0)) {
2240 smp->data.u.sint = LLONG_MAX;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002241 return 1;
2242 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002243 smp->data.u.sint = LLONG_MIN;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002244 return 1;
2245 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002246 smp->data.u.sint = c;
Willy Tarreau97707872015-01-27 15:12:13 +01002247 return 1;
2248}
2249
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002250/* Takes a SINT on input, applies an arithmetic "div" with the SINT directly in
2251 * arg_p or in the varaible described in arg_p, and returns the SINT result.
2252 * If arg_p makes the result overflow, then the largest possible quantity is
2253 * returned.
Willy Tarreau97707872015-01-27 15:12:13 +01002254 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002255static int sample_conv_arith_div(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002256 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002257{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002258 struct sample tmp;
2259
2260 if (!sample_conv_var2smp(arg_p, smp->strm, &tmp))
2261 return 0;
2262
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002263 if (tmp.data.u.sint) {
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002264 /* The divide between LLONG_MIN and -1 returns a
2265 * "floting point exception".
2266 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002267 if (smp->data.u.sint == LLONG_MIN && tmp.data.u.sint == -1) {
2268 smp->data.u.sint = LLONG_MAX;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002269 return 1;
2270 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002271 smp->data.u.sint /= tmp.data.u.sint;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002272 return 1;
2273 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002274 smp->data.u.sint = LLONG_MAX;
Willy Tarreau97707872015-01-27 15:12:13 +01002275 return 1;
2276}
2277
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002278/* Takes a SINT on input, applies an arithmetic "mod" with the SINT directly in
2279 * arg_p or in the varaible described in arg_p, and returns the SINT result.
2280 * If arg_p makes the result overflow, then 0 is returned.
Willy Tarreau97707872015-01-27 15:12:13 +01002281 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002282static int sample_conv_arith_mod(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002283 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002284{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002285 struct sample tmp;
2286
2287 if (!sample_conv_var2smp(arg_p, smp->strm, &tmp))
2288 return 0;
2289
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002290 if (tmp.data.u.sint) {
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002291 /* The divide between LLONG_MIN and -1 returns a
2292 * "floting point exception".
2293 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002294 if (smp->data.u.sint == LLONG_MIN && tmp.data.u.sint == -1) {
2295 smp->data.u.sint = 0;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002296 return 1;
2297 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002298 smp->data.u.sint %= tmp.data.u.sint;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002299 return 1;
2300 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002301 smp->data.u.sint = 0;
Willy Tarreau97707872015-01-27 15:12:13 +01002302 return 1;
2303}
2304
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002305/* Takes an SINT on input, applies an arithmetic "neg" and returns the SINT
Willy Tarreau97707872015-01-27 15:12:13 +01002306 * result.
2307 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002308static int sample_conv_arith_neg(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002309 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002310{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002311 if (smp->data.u.sint == LLONG_MIN)
2312 smp->data.u.sint = LLONG_MAX;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002313 else
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002314 smp->data.u.sint = -smp->data.u.sint;
Willy Tarreau97707872015-01-27 15:12:13 +01002315 return 1;
2316}
2317
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002318/* Takes a SINT on input, returns true is the value is non-null, otherwise
Willy Tarreau97707872015-01-27 15:12:13 +01002319 * false. The output is a BOOL.
2320 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002321static int sample_conv_arith_bool(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002322 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002323{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002324 smp->data.u.sint = !!smp->data.u.sint;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002325 smp->data.type = SMP_T_BOOL;
Willy Tarreau97707872015-01-27 15:12:13 +01002326 return 1;
2327}
2328
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002329/* Takes a SINT on input, returns false is the value is non-null, otherwise
Willy Tarreau97707872015-01-27 15:12:13 +01002330 * truee. The output is a BOOL.
2331 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002332static int sample_conv_arith_not(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002333 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002334{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002335 smp->data.u.sint = !smp->data.u.sint;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002336 smp->data.type = SMP_T_BOOL;
Willy Tarreau97707872015-01-27 15:12:13 +01002337 return 1;
2338}
2339
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002340/* Takes a SINT on input, returns true is the value is odd, otherwise false.
Willy Tarreau97707872015-01-27 15:12:13 +01002341 * The output is a BOOL.
2342 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002343static int sample_conv_arith_odd(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002344 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002345{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002346 smp->data.u.sint = smp->data.u.sint & 1;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002347 smp->data.type = SMP_T_BOOL;
Willy Tarreau97707872015-01-27 15:12:13 +01002348 return 1;
2349}
2350
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002351/* Takes a SINT on input, returns true is the value is even, otherwise false.
Willy Tarreau97707872015-01-27 15:12:13 +01002352 * The output is a BOOL.
2353 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002354static int sample_conv_arith_even(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002355 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002356{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002357 smp->data.u.sint = !(smp->data.u.sint & 1);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002358 smp->data.type = SMP_T_BOOL;
Willy Tarreau97707872015-01-27 15:12:13 +01002359 return 1;
2360}
2361
Willy Tarreau5b8ad222013-07-25 12:17:57 +02002362/************************************************************************/
2363/* All supported sample fetch functions must be declared here */
2364/************************************************************************/
2365
2366/* force TRUE to be returned at the fetch level */
2367static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002368smp_fetch_true(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau5b8ad222013-07-25 12:17:57 +02002369{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002370 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002371 smp->data.u.sint = 1;
Willy Tarreau5b8ad222013-07-25 12:17:57 +02002372 return 1;
2373}
2374
2375/* force FALSE to be returned at the fetch level */
2376static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002377smp_fetch_false(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau5b8ad222013-07-25 12:17:57 +02002378{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002379 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002380 smp->data.u.sint = 0;
Willy Tarreau5b8ad222013-07-25 12:17:57 +02002381 return 1;
2382}
2383
2384/* retrieve environment variable $1 as a string */
2385static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002386smp_fetch_env(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau5b8ad222013-07-25 12:17:57 +02002387{
2388 char *env;
2389
2390 if (!args || args[0].type != ARGT_STR)
2391 return 0;
2392
2393 env = getenv(args[0].data.str.str);
2394 if (!env)
2395 return 0;
2396
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002397 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01002398 smp->flags = SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002399 smp->data.u.str.str = env;
2400 smp->data.u.str.len = strlen(env);
Willy Tarreau5b8ad222013-07-25 12:17:57 +02002401 return 1;
2402}
2403
Willy Tarreau6236d3a2013-07-25 14:28:25 +02002404/* retrieve the current local date in epoch time, and applies an optional offset
2405 * of args[0] seconds.
2406 */
2407static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002408smp_fetch_date(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau6236d3a2013-07-25 14:28:25 +02002409{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002410 smp->data.u.sint = date.tv_sec;
Willy Tarreau6236d3a2013-07-25 14:28:25 +02002411
2412 /* add offset */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002413 if (args && args[0].type == ARGT_SINT)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002414 smp->data.u.sint += args[0].data.sint;
Willy Tarreau6236d3a2013-07-25 14:28:25 +02002415
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002416 smp->data.type = SMP_T_SINT;
Willy Tarreau6236d3a2013-07-25 14:28:25 +02002417 smp->flags |= SMP_F_VOL_TEST | SMP_F_MAY_CHANGE;
2418 return 1;
2419}
2420
Willy Tarreau0f30d262014-11-24 16:02:05 +01002421/* returns the number of processes */
2422static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002423smp_fetch_nbproc(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau0f30d262014-11-24 16:02:05 +01002424{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002425 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002426 smp->data.u.sint = global.nbproc;
Willy Tarreau0f30d262014-11-24 16:02:05 +01002427 return 1;
2428}
2429
2430/* returns the number of the current process (between 1 and nbproc */
2431static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002432smp_fetch_proc(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau0f30d262014-11-24 16:02:05 +01002433{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002434 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002435 smp->data.u.sint = relative_pid;
Willy Tarreau0f30d262014-11-24 16:02:05 +01002436 return 1;
2437}
2438
Willy Tarreau84310e22014-02-14 11:59:04 +01002439/* generate a random 32-bit integer for whatever purpose, with an optional
2440 * range specified in argument.
2441 */
2442static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002443smp_fetch_rand(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau84310e22014-02-14 11:59:04 +01002444{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002445 smp->data.u.sint = random();
Willy Tarreau84310e22014-02-14 11:59:04 +01002446
2447 /* reduce if needed. Don't do a modulo, use all bits! */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002448 if (args && args[0].type == ARGT_SINT)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002449 smp->data.u.sint = (smp->data.u.sint * args[0].data.sint) / ((u64)RAND_MAX+1);
Willy Tarreau84310e22014-02-14 11:59:04 +01002450
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002451 smp->data.type = SMP_T_SINT;
Willy Tarreau84310e22014-02-14 11:59:04 +01002452 smp->flags |= SMP_F_VOL_TEST | SMP_F_MAY_CHANGE;
2453 return 1;
2454}
2455
Willy Tarreau0f30d262014-11-24 16:02:05 +01002456/* returns true if the current process is stopping */
2457static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002458smp_fetch_stopping(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau0f30d262014-11-24 16:02:05 +01002459{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002460 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002461 smp->data.u.sint = stopping;
Willy Tarreau0f30d262014-11-24 16:02:05 +01002462 return 1;
2463}
2464
Thierry FOURNIERcc103292015-06-06 19:30:17 +02002465static int smp_fetch_const_str(const struct arg *args, struct sample *smp, const char *kw, void *private)
2466{
2467 smp->flags |= SMP_F_CONST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002468 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002469 smp->data.u.str.str = args[0].data.str.str;
2470 smp->data.u.str.len = args[0].data.str.len;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02002471 return 1;
2472}
2473
2474static int smp_check_const_bool(struct arg *args, char **err)
2475{
2476 if (strcasecmp(args[0].data.str.str, "true") == 0 ||
2477 strcasecmp(args[0].data.str.str, "1") == 0) {
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002478 args[0].type = ARGT_SINT;
2479 args[0].data.sint = 1;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02002480 return 1;
2481 }
2482 if (strcasecmp(args[0].data.str.str, "false") == 0 ||
2483 strcasecmp(args[0].data.str.str, "0") == 0) {
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002484 args[0].type = ARGT_SINT;
2485 args[0].data.sint = 0;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02002486 return 1;
2487 }
2488 memprintf(err, "Expects 'true', 'false', '0' or '1'");
2489 return 0;
2490}
2491
2492static int smp_fetch_const_bool(const struct arg *args, struct sample *smp, const char *kw, void *private)
2493{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002494 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002495 smp->data.u.sint = args[0].data.sint;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02002496 return 1;
2497}
2498
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002499static int smp_fetch_const_int(const struct arg *args, struct sample *smp, const char *kw, void *private)
Thierry FOURNIERcc103292015-06-06 19:30:17 +02002500{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002501 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002502 smp->data.u.sint = args[0].data.sint;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02002503 return 1;
2504}
2505
2506static int smp_fetch_const_ipv4(const struct arg *args, struct sample *smp, const char *kw, void *private)
2507{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002508 smp->data.type = SMP_T_IPV4;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002509 smp->data.u.ipv4 = args[0].data.ipv4;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02002510 return 1;
2511}
2512
2513static int smp_fetch_const_ipv6(const struct arg *args, struct sample *smp, const char *kw, void *private)
2514{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002515 smp->data.type = SMP_T_IPV6;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002516 smp->data.u.ipv6 = args[0].data.ipv6;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02002517 return 1;
2518}
2519
2520static int smp_check_const_bin(struct arg *args, char **err)
2521{
2522 char *binstr;
2523 int binstrlen;
2524
2525 if (!parse_binary(args[0].data.str.str, &binstr, &binstrlen, err))
2526 return 0;
2527 args[0].type = ARGT_STR;
2528 args[0].data.str.str = binstr;
2529 args[0].data.str.len = binstrlen;
2530 return 1;
2531}
2532
2533static int smp_fetch_const_bin(const struct arg *args, struct sample *smp, const char *kw, void *private)
2534{
2535 smp->flags |= SMP_F_CONST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002536 smp->data.type = SMP_T_BIN;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002537 smp->data.u.str.str = args[0].data.str.str;
2538 smp->data.u.str.len = args[0].data.str.len;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02002539 return 1;
2540}
2541
2542static int smp_check_const_meth(struct arg *args, char **err)
2543{
2544 enum http_meth_t meth;
2545 int i;
2546
2547 meth = find_http_meth(args[0].data.str.str, args[0].data.str.len);
2548 if (meth != HTTP_METH_OTHER) {
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002549 args[0].type = ARGT_SINT;
2550 args[0].data.sint = meth;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02002551 } else {
2552 /* Check method avalaibility. A methos is a token defined as :
2553 * tchar = "!" / "#" / "$" / "%" / "&" / "'" / "*" / "+" / "-" / "." /
2554 * "^" / "_" / "`" / "|" / "~" / DIGIT / ALPHA
2555 * token = 1*tchar
2556 */
2557 for (i = 0; i < args[0].data.str.len; i++) {
2558 if (!http_is_token[(unsigned char)args[0].data.str.str[i]]) {
2559 memprintf(err, "expects valid method.");
2560 return 0;
2561 }
2562 }
2563 }
2564 return 1;
2565}
2566
2567static int smp_fetch_const_meth(const struct arg *args, struct sample *smp, const char *kw, void *private)
2568{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002569 smp->data.type = SMP_T_METH;
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002570 if (args[0].type == ARGT_SINT) {
Thierry FOURNIERcc103292015-06-06 19:30:17 +02002571 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002572 smp->data.u.meth.meth = args[0].data.sint;
2573 smp->data.u.meth.str.str = "";
2574 smp->data.u.meth.str.len = 0;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02002575 } else {
2576 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002577 smp->data.u.meth.meth = HTTP_METH_OTHER;
2578 smp->data.u.meth.str.str = args[0].data.str.str;
2579 smp->data.u.meth.str.len = args[0].data.str.len;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02002580 }
2581 return 1;
2582}
2583
Willy Tarreau5b8ad222013-07-25 12:17:57 +02002584/* Note: must not be declared <const> as its list will be overwritten.
2585 * Note: fetches that may return multiple types must be declared as the lowest
2586 * common denominator, the type that can be casted into all other ones. For
2587 * instance IPv4/IPv6 must be declared IPv4.
2588 */
2589static struct sample_fetch_kw_list smp_kws = {ILH, {
2590 { "always_false", smp_fetch_false, 0, NULL, SMP_T_BOOL, SMP_USE_INTRN },
2591 { "always_true", smp_fetch_true, 0, NULL, SMP_T_BOOL, SMP_USE_INTRN },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01002592 { "env", smp_fetch_env, ARG1(1,STR), NULL, SMP_T_STR, SMP_USE_INTRN },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002593 { "date", smp_fetch_date, ARG1(0,SINT), NULL, SMP_T_SINT, SMP_USE_INTRN },
2594 { "nbproc", smp_fetch_nbproc,0, NULL, SMP_T_SINT, SMP_USE_INTRN },
2595 { "proc", smp_fetch_proc, 0, NULL, SMP_T_SINT, SMP_USE_INTRN },
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002596 { "rand", smp_fetch_rand, ARG1(0,SINT), NULL, SMP_T_SINT, SMP_USE_INTRN },
Willy Tarreau0f30d262014-11-24 16:02:05 +01002597 { "stopping", smp_fetch_stopping, 0, NULL, SMP_T_BOOL, SMP_USE_INTRN },
Thierry FOURNIERcc103292015-06-06 19:30:17 +02002598
2599 { "str", smp_fetch_const_str, ARG1(1,STR), NULL , SMP_T_STR, SMP_USE_INTRN },
2600 { "bool", smp_fetch_const_bool, ARG1(1,STR), smp_check_const_bool, SMP_T_BOOL, SMP_USE_INTRN },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002601 { "int", smp_fetch_const_int, ARG1(1,SINT), NULL , SMP_T_SINT, SMP_USE_INTRN },
Thierry FOURNIERcc103292015-06-06 19:30:17 +02002602 { "ipv4", smp_fetch_const_ipv4, ARG1(1,IPV4), NULL , SMP_T_IPV4, SMP_USE_INTRN },
2603 { "ipv6", smp_fetch_const_ipv6, ARG1(1,IPV6), NULL , SMP_T_IPV6, SMP_USE_INTRN },
2604 { "bin", smp_fetch_const_bin, ARG1(1,STR), smp_check_const_bin , SMP_T_BIN, SMP_USE_INTRN },
2605 { "meth", smp_fetch_const_meth, ARG1(1,STR), smp_check_const_meth, SMP_T_METH, SMP_USE_INTRN },
2606
Willy Tarreau5b8ad222013-07-25 12:17:57 +02002607 { /* END */ },
2608}};
2609
Emeric Brun107ca302010-01-04 16:16:05 +01002610/* Note: must not be declared <const> as its list will be overwritten */
Willy Tarreaudc13c112013-06-21 23:16:39 +02002611static struct sample_conv_kw_list sample_conv_kws = {ILH, {
Thierry FOURNIER9687c772015-05-07 15:46:29 +02002612#ifdef DEBUG_EXPR
2613 { "debug", sample_conv_debug, 0, NULL, SMP_T_ANY, SMP_T_ANY },
2614#endif
2615
Emeric Brun53d1a982014-04-30 18:21:37 +02002616 { "base64", sample_conv_bin2base64,0, NULL, SMP_T_BIN, SMP_T_STR },
Willy Tarreau12785782012-04-27 21:37:17 +02002617 { "upper", sample_conv_str2upper, 0, NULL, SMP_T_STR, SMP_T_STR },
2618 { "lower", sample_conv_str2lower, 0, NULL, SMP_T_STR, SMP_T_STR },
Thierry FOURNIER2f49d6d2014-03-12 15:01:52 +01002619 { "hex", sample_conv_bin2hex, 0, NULL, SMP_T_BIN, SMP_T_STR },
Willy Tarreau12785782012-04-27 21:37:17 +02002620 { "ipmask", sample_conv_ipmask, ARG1(1,MSK4), NULL, SMP_T_IPV4, SMP_T_IPV4 },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002621 { "ltime", sample_conv_ltime, ARG2(1,STR,SINT), NULL, SMP_T_SINT, SMP_T_STR },
2622 { "utime", sample_conv_utime, ARG2(1,STR,SINT), NULL, SMP_T_SINT, SMP_T_STR },
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002623 { "crc32", sample_conv_crc32, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
2624 { "djb2", sample_conv_djb2, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
2625 { "sdbm", sample_conv_sdbm, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
2626 { "wt6", sample_conv_wt6, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002627 { "json", sample_conv_json, ARG1(1,STR), sample_conv_json_check, SMP_T_STR, SMP_T_STR },
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002628 { "bytes", sample_conv_bytes, ARG2(1,SINT,SINT), NULL, SMP_T_BIN, SMP_T_BIN },
2629 { "field", sample_conv_field, ARG2(2,SINT,STR), sample_conv_field_check, SMP_T_STR, SMP_T_STR },
2630 { "word", sample_conv_word, ARG2(2,SINT,STR), sample_conv_field_check, SMP_T_STR, SMP_T_STR },
Willy Tarreau7eda8492015-01-20 19:47:06 +01002631 { "regsub", sample_conv_regsub, ARG3(2,REG,STR,STR), sample_conv_regsub_check, SMP_T_STR, SMP_T_STR },
Willy Tarreau97707872015-01-27 15:12:13 +01002632
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002633 { "and", sample_conv_binary_and, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
2634 { "or", sample_conv_binary_or, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
2635 { "xor", sample_conv_binary_xor, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
2636 { "cpl", sample_conv_binary_cpl, 0, NULL, SMP_T_SINT, SMP_T_SINT },
2637 { "bool", sample_conv_arith_bool, 0, NULL, SMP_T_SINT, SMP_T_BOOL },
2638 { "not", sample_conv_arith_not, 0, NULL, SMP_T_SINT, SMP_T_BOOL },
2639 { "odd", sample_conv_arith_odd, 0, NULL, SMP_T_SINT, SMP_T_BOOL },
2640 { "even", sample_conv_arith_even, 0, NULL, SMP_T_SINT, SMP_T_BOOL },
2641 { "add", sample_conv_arith_add, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
2642 { "sub", sample_conv_arith_sub, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
2643 { "mul", sample_conv_arith_mul, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
2644 { "div", sample_conv_arith_div, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
2645 { "mod", sample_conv_arith_mod, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
2646 { "neg", sample_conv_arith_neg, 0, NULL, SMP_T_SINT, SMP_T_SINT },
Willy Tarreau97707872015-01-27 15:12:13 +01002647
Willy Tarreau9fcb9842012-04-20 14:45:49 +02002648 { NULL, NULL, 0, 0, 0 },
Emeric Brun107ca302010-01-04 16:16:05 +01002649}};
2650
2651__attribute__((constructor))
Willy Tarreau12785782012-04-27 21:37:17 +02002652static void __sample_init(void)
Emeric Brun107ca302010-01-04 16:16:05 +01002653{
Willy Tarreau5b8ad222013-07-25 12:17:57 +02002654 /* register sample fetch and format conversion keywords */
2655 sample_register_fetches(&smp_kws);
Willy Tarreau12785782012-04-27 21:37:17 +02002656 sample_register_convs(&sample_conv_kws);
Emeric Brun107ca302010-01-04 16:16:05 +01002657}