blob: 77cbd1bf34d495d48d063e639b78963db05f5f62 [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
Willy Tarreauad635822016-08-08 19:21:09 +0200640/* This function inconditionally duplicates data and removes the "const" flag.
641 * For strings and binary blocks, it also provides a known allocated size with
642 * a length that is capped to the size, and ensures a trailing zero is always
643 * appended for strings. This is necessary for some operations which may
644 * require to extend the length. It returns 0 if it fails, 1 on success.
645 */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100646int smp_dup(struct sample *smp)
Emeric Brun485479d2010-09-23 18:02:19 +0200647{
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100648 struct chunk *trash;
Emeric Brun485479d2010-09-23 18:02:19 +0200649
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100650 /* If the const flag is not set, we don't need to duplicate the
651 * pattern as it can be modified in place.
652 */
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100653
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200654 switch (smp->data.type) {
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100655 case SMP_T_BOOL:
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100656 case SMP_T_SINT:
657 case SMP_T_ADDR:
658 case SMP_T_IPV4:
659 case SMP_T_IPV6:
660 /* These type are not const. */
661 break;
Willy Tarreauad635822016-08-08 19:21:09 +0200662
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100663 case SMP_T_STR:
Willy Tarreauad635822016-08-08 19:21:09 +0200664 trash = get_trash_chunk();
665 trash->len = smp->data.u.str.len;
666 if (trash->len > trash->size - 1)
667 trash->len = trash->size - 1;
668
669 memcpy(trash->str, smp->data.u.str.str, trash->len);
670 trash->str[trash->len] = 0;
671 smp->data.u.str = *trash;
672 break;
673
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100674 case SMP_T_BIN:
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100675 trash = get_trash_chunk();
Willy Tarreauad635822016-08-08 19:21:09 +0200676 trash->len = smp->data.u.str.len;
677 if (trash->len > trash->size)
678 trash->len = trash->size;
679
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200680 memcpy(trash->str, smp->data.u.str.str, trash->len);
681 smp->data.u.str = *trash;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100682 break;
683 default:
684 /* Other cases are unexpected. */
685 return 0;
686 }
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100687
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100688 /* remove const flag */
689 smp->flags &= ~SMP_F_CONST;
Emeric Brun485479d2010-09-23 18:02:19 +0200690 return 1;
691}
692
Thierry FOURNIER0e9af552013-12-14 14:55:04 +0100693int c_none(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100694{
695 return 1;
696}
697
Willy Tarreau342acb42012-04-23 22:03:39 +0200698static int c_str2int(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100699{
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +0200700 const char *str;
701 const char *end;
Emeric Brun107ca302010-01-04 16:16:05 +0100702
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200703 if (smp->data.u.str.len == 0)
Thierry FOURNIER60bb0202014-01-27 18:20:48 +0100704 return 0;
705
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200706 str = smp->data.u.str.str;
707 end = smp->data.u.str.str + smp->data.u.str.len;
Emeric Brun107ca302010-01-04 16:16:05 +0100708
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200709 smp->data.u.sint = read_int64(&str, end);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200710 smp->data.type = SMP_T_SINT;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100711 smp->flags &= ~SMP_F_CONST;
Emeric Brun107ca302010-01-04 16:16:05 +0100712 return 1;
713}
714
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100715static int c_str2meth(struct sample *smp)
716{
717 enum http_meth_t meth;
718 int len;
719
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200720 meth = find_http_meth(smp->data.u.str.str, smp->data.u.str.len);
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100721 if (meth == HTTP_METH_OTHER) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200722 len = smp->data.u.str.len;
723 smp->data.u.meth.str.str = smp->data.u.str.str;
724 smp->data.u.meth.str.len = len;
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100725 }
726 else
727 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200728 smp->data.u.meth.meth = meth;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200729 smp->data.type = SMP_T_METH;
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100730 return 1;
731}
732
733static int c_meth2str(struct sample *smp)
734{
735 int len;
736 enum http_meth_t meth;
737
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200738 if (smp->data.u.meth.meth == HTTP_METH_OTHER) {
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100739 /* The method is unknown. Copy the original pointer. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200740 len = smp->data.u.meth.str.len;
741 smp->data.u.str.str = smp->data.u.meth.str.str;
742 smp->data.u.str.len = len;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200743 smp->data.type = SMP_T_STR;
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100744 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200745 else if (smp->data.u.meth.meth < HTTP_METH_OTHER) {
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100746 /* The method is known, copy the pointer containing the string. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200747 meth = smp->data.u.meth.meth;
748 smp->data.u.str.str = http_known_methods[meth].name;
749 smp->data.u.str.len = http_known_methods[meth].len;
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100750 smp->flags |= SMP_F_CONST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200751 smp->data.type = SMP_T_STR;
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100752 }
753 else {
754 /* Unknown method */
755 return 0;
756 }
757 return 1;
758}
759
Willy Tarreau9700e5c2014-07-15 21:03:26 +0200760static int c_addr2bin(struct sample *smp)
761{
762 struct chunk *chk = get_trash_chunk();
763
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200764 if (smp->data.type == SMP_T_IPV4) {
Willy Tarreau9700e5c2014-07-15 21:03:26 +0200765 chk->len = 4;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200766 memcpy(chk->str, &smp->data.u.ipv4, chk->len);
Willy Tarreau9700e5c2014-07-15 21:03:26 +0200767 }
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200768 else if (smp->data.type == SMP_T_IPV6) {
Willy Tarreau9700e5c2014-07-15 21:03:26 +0200769 chk->len = 16;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200770 memcpy(chk->str, &smp->data.u.ipv6, chk->len);
Willy Tarreau9700e5c2014-07-15 21:03:26 +0200771 }
772 else
773 return 0;
774
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200775 smp->data.u.str = *chk;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200776 smp->data.type = SMP_T_BIN;
Willy Tarreau9700e5c2014-07-15 21:03:26 +0200777 return 1;
778}
779
Willy Tarreaubbfd1a22014-07-15 21:19:08 +0200780static int c_int2bin(struct sample *smp)
781{
782 struct chunk *chk = get_trash_chunk();
783
Willy Tarreau5f6e9052016-05-20 06:29:59 +0200784 *(unsigned long long int *)chk->str = my_htonll(smp->data.u.sint);
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +0200785 chk->len = 8;
Willy Tarreaubbfd1a22014-07-15 21:19:08 +0200786
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200787 smp->data.u.str = *chk;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200788 smp->data.type = SMP_T_BIN;
Willy Tarreaubbfd1a22014-07-15 21:19:08 +0200789 return 1;
790}
791
Willy Tarreau9700e5c2014-07-15 21:03:26 +0200792
Emeric Brun107ca302010-01-04 16:16:05 +0100793/*****************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +0200794/* Sample casts matrix: */
795/* sample_casts[from type][to type] */
796/* NULL pointer used for impossible sample casts */
Emeric Brun107ca302010-01-04 16:16:05 +0100797/*****************************************************************/
Emeric Brun107ca302010-01-04 16:16:05 +0100798
Thierry FOURNIER8af6ff12013-11-21 10:53:12 +0100799sample_cast_fct sample_casts[SMP_TYPES][SMP_TYPES] = {
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +0200800/* to: ANY BOOL SINT ADDR IPV4 IPV6 STR BIN METH */
801/* from: ANY */ { c_none, c_none, c_none, c_none, c_none, c_none, c_none, c_none, c_none, },
802/* BOOL */ { c_none, c_none, c_none, NULL, NULL, NULL, c_int2str, NULL, NULL, },
Thierry FOURNIERcc4d1712015-07-24 09:04:56 +0200803/* 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 +0200804/* ADDR */ { c_none, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, },
805/* IPV4 */ { c_none, NULL, c_ip2int, c_none, c_none, c_ip2ipv6, c_ip2str, c_addr2bin, NULL, },
Thierry FOURNIERcc4d1712015-07-24 09:04:56 +0200806/* IPV6 */ { c_none, NULL, NULL, c_none, c_ipv62ip,c_none, c_ipv62str, c_addr2bin, NULL, },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +0200807/* STR */ { c_none, c_str2int, c_str2int, c_str2addr, c_str2ip, c_str2ipv6, c_none, c_none, c_str2meth, },
808/* BIN */ { c_none, NULL, NULL, NULL, NULL, NULL, c_bin2str, c_none, c_str2meth, },
809/* METH */ { c_none, NULL, NULL, NULL, NULL, NULL, c_meth2str, c_meth2str, c_none, }
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200810};
Emeric Brun107ca302010-01-04 16:16:05 +0100811
Emeric Brun107ca302010-01-04 16:16:05 +0100812/*
Willy Tarreau12785782012-04-27 21:37:17 +0200813 * Parse a sample expression configuration:
Emeric Brun107ca302010-01-04 16:16:05 +0100814 * fetch keyword followed by format conversion keywords.
Willy Tarreau12785782012-04-27 21:37:17 +0200815 * Returns a pointer on allocated sample expression structure.
Willy Tarreaua4312fa2013-04-02 16:34:32 +0200816 * The caller must have set al->ctx.
Emeric Brun107ca302010-01-04 16:16:05 +0100817 */
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +0100818struct 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 +0100819{
Willy Tarreau833cc792013-07-24 15:34:19 +0200820 const char *begw; /* beginning of word */
821 const char *endw; /* end of word */
822 const char *endt; /* end of term */
Willy Tarreau12785782012-04-27 21:37:17 +0200823 struct sample_expr *expr;
824 struct sample_fetch *fetch;
825 struct sample_conv *conv;
Emeric Brun107ca302010-01-04 16:16:05 +0100826 unsigned long prev_type;
Willy Tarreau833cc792013-07-24 15:34:19 +0200827 char *fkw = NULL;
828 char *ckw = NULL;
Willy Tarreau689a1df2013-12-13 00:40:11 +0100829 int err_arg;
Emeric Brun107ca302010-01-04 16:16:05 +0100830
Willy Tarreau833cc792013-07-24 15:34:19 +0200831 begw = str[*idx];
832 for (endw = begw; *endw && *endw != '(' && *endw != ','; endw++);
833
834 if (endw == begw) {
Willy Tarreau975c1782013-12-12 23:16:54 +0100835 memprintf(err_msg, "missing fetch method");
Emeric Brun107ca302010-01-04 16:16:05 +0100836 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200837 }
Emeric Brun107ca302010-01-04 16:16:05 +0100838
Willy Tarreau833cc792013-07-24 15:34:19 +0200839 /* keep a copy of the current fetch keyword for error reporting */
840 fkw = my_strndup(begw, endw - begw);
Emeric Brun107ca302010-01-04 16:16:05 +0100841
Willy Tarreau833cc792013-07-24 15:34:19 +0200842 fetch = find_sample_fetch(begw, endw - begw);
843 if (!fetch) {
Willy Tarreau975c1782013-12-12 23:16:54 +0100844 memprintf(err_msg, "unknown fetch method '%s'", fkw);
Emeric Brun107ca302010-01-04 16:16:05 +0100845 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200846 }
Emeric Brun107ca302010-01-04 16:16:05 +0100847
Willy Tarreau833cc792013-07-24 15:34:19 +0200848 endt = endw;
849 if (*endt == '(') {
Willy Tarreau689a1df2013-12-13 00:40:11 +0100850 /* look for the end of this term and skip the opening parenthesis */
851 endt = ++endw;
Willy Tarreau833cc792013-07-24 15:34:19 +0200852 while (*endt && *endt != ')')
853 endt++;
854 if (*endt != ')') {
Willy Tarreau975c1782013-12-12 23:16:54 +0100855 memprintf(err_msg, "missing closing ')' after arguments to fetch keyword '%s'", fkw);
Willy Tarreau833cc792013-07-24 15:34:19 +0200856 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200857 }
Emeric Brun485479d2010-09-23 18:02:19 +0200858 }
Emeric Brun107ca302010-01-04 16:16:05 +0100859
Willy Tarreau833cc792013-07-24 15:34:19 +0200860 /* At this point, we have :
861 * - begw : beginning of the keyword
Willy Tarreau689a1df2013-12-13 00:40:11 +0100862 * - endw : end of the keyword, first character not part of keyword
863 * nor the opening parenthesis (so first character of args
864 * if present).
Willy Tarreau833cc792013-07-24 15:34:19 +0200865 * - endt : end of the term (=endw or last parenthesis if args are present)
866 */
867
868 if (fetch->out_type >= SMP_TYPES) {
Willy Tarreau975c1782013-12-12 23:16:54 +0100869 memprintf(err_msg, "returns type of fetch method '%s' is unknown", fkw);
Emeric Brun107ca302010-01-04 16:16:05 +0100870 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200871 }
Emeric Brun107ca302010-01-04 16:16:05 +0100872 prev_type = fetch->out_type;
Willy Tarreau833cc792013-07-24 15:34:19 +0200873
Vincent Bernat02779b62016-04-03 13:48:43 +0200874 expr = calloc(1, sizeof(*expr));
Emeric Brun485479d2010-09-23 18:02:19 +0200875 if (!expr)
876 goto out_error;
Emeric Brun107ca302010-01-04 16:16:05 +0100877
878 LIST_INIT(&(expr->conv_exprs));
879 expr->fetch = fetch;
Willy Tarreau2e845be2012-10-19 19:49:09 +0200880 expr->arg_p = empty_arg_list;
Emeric Brun107ca302010-01-04 16:16:05 +0100881
Willy Tarreau689a1df2013-12-13 00:40:11 +0100882 /* Note that we call the argument parser even with an empty string,
883 * this allows it to automatically create entries for mandatory
884 * implicit arguments (eg: local proxy name).
885 */
886 al->kw = expr->fetch->kw;
887 al->conv = NULL;
888 if (make_arg_list(endw, endt - endw, fetch->arg_mask, &expr->arg_p, err_msg, NULL, &err_arg, al) < 0) {
889 memprintf(err_msg, "fetch method '%s' : %s", fkw, *err_msg);
890 goto out_error;
891 }
Willy Tarreau2e845be2012-10-19 19:49:09 +0200892
Willy Tarreau689a1df2013-12-13 00:40:11 +0100893 if (!expr->arg_p) {
894 expr->arg_p = empty_arg_list;
Emeric Brun485479d2010-09-23 18:02:19 +0200895 }
Willy Tarreau689a1df2013-12-13 00:40:11 +0100896 else if (fetch->val_args && !fetch->val_args(expr->arg_p, err_msg)) {
897 memprintf(err_msg, "invalid args in fetch method '%s' : %s", fkw, *err_msg);
Emeric Brun485479d2010-09-23 18:02:19 +0200898 goto out_error;
Emeric Brun107ca302010-01-04 16:16:05 +0100899 }
900
Willy Tarreau833cc792013-07-24 15:34:19 +0200901 /* Now process the converters if any. We have two supported syntaxes
902 * for the converters, which can be combined :
903 * - comma-delimited list of converters just after the keyword and args ;
904 * - one converter per keyword
905 * The combination allows to have each keyword being a comma-delimited
906 * series of converters.
907 *
908 * We want to process the former first, then the latter. For this we start
909 * from the beginning of the supposed place in the exiting conv chain, which
910 * starts at the last comma (endt).
911 */
912
913 while (1) {
Willy Tarreau12785782012-04-27 21:37:17 +0200914 struct sample_conv_expr *conv_expr;
Emeric Brun107ca302010-01-04 16:16:05 +0100915
Willy Tarreau833cc792013-07-24 15:34:19 +0200916 if (*endt == ')') /* skip last closing parenthesis */
917 endt++;
Emeric Brun107ca302010-01-04 16:16:05 +0100918
Willy Tarreau833cc792013-07-24 15:34:19 +0200919 if (*endt && *endt != ',') {
920 if (ckw)
Willy Tarreau975c1782013-12-12 23:16:54 +0100921 memprintf(err_msg, "missing comma after conv keyword '%s'", ckw);
Willy Tarreau833cc792013-07-24 15:34:19 +0200922 else
Willy Tarreau975c1782013-12-12 23:16:54 +0100923 memprintf(err_msg, "missing comma after fetch keyword '%s'", fkw);
Emeric Brun107ca302010-01-04 16:16:05 +0100924 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200925 }
Emeric Brun107ca302010-01-04 16:16:05 +0100926
Willy Tarreau833cc792013-07-24 15:34:19 +0200927 while (*endt == ',') /* then trailing commas */
928 endt++;
929
930 begw = endt; /* start of conv keyword */
931
932 if (!*begw) {
933 /* none ? skip to next string */
934 (*idx)++;
935 begw = str[*idx];
936 if (!begw || !*begw)
937 break;
938 }
939
940 for (endw = begw; *endw && *endw != '(' && *endw != ','; endw++);
941
942 free(ckw);
943 ckw = my_strndup(begw, endw - begw);
944
945 conv = find_sample_conv(begw, endw - begw);
946 if (!conv) {
947 /* we found an isolated keyword that we don't know, it's not ours */
948 if (begw == str[*idx])
949 break;
Willy Tarreau975c1782013-12-12 23:16:54 +0100950 memprintf(err_msg, "unknown conv method '%s'", ckw);
Willy Tarreau833cc792013-07-24 15:34:19 +0200951 goto out_error;
952 }
Emeric Brun107ca302010-01-04 16:16:05 +0100953
Willy Tarreau833cc792013-07-24 15:34:19 +0200954 endt = endw;
955 if (*endt == '(') {
956 /* look for the end of this term */
957 while (*endt && *endt != ')')
958 endt++;
959 if (*endt != ')') {
Willy Tarreau975c1782013-12-12 23:16:54 +0100960 memprintf(err_msg, "syntax error: missing ')' after conv keyword '%s'", ckw);
Willy Tarreau833cc792013-07-24 15:34:19 +0200961 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200962 }
Willy Tarreau833cc792013-07-24 15:34:19 +0200963 }
964
965 if (conv->in_type >= SMP_TYPES || conv->out_type >= SMP_TYPES) {
Willy Tarreau975c1782013-12-12 23:16:54 +0100966 memprintf(err_msg, "returns type of conv method '%s' is unknown", ckw);
Emeric Brun107ca302010-01-04 16:16:05 +0100967 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200968 }
Emeric Brun107ca302010-01-04 16:16:05 +0100969
970 /* If impossible type conversion */
Willy Tarreau12785782012-04-27 21:37:17 +0200971 if (!sample_casts[prev_type][conv->in_type]) {
Willy Tarreau975c1782013-12-12 23:16:54 +0100972 memprintf(err_msg, "conv method '%s' cannot be applied", ckw);
Emeric Brun107ca302010-01-04 16:16:05 +0100973 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200974 }
Emeric Brun107ca302010-01-04 16:16:05 +0100975
976 prev_type = conv->out_type;
Vincent Bernat02779b62016-04-03 13:48:43 +0200977 conv_expr = calloc(1, sizeof(*conv_expr));
Emeric Brun485479d2010-09-23 18:02:19 +0200978 if (!conv_expr)
979 goto out_error;
Emeric Brun107ca302010-01-04 16:16:05 +0100980
981 LIST_ADDQ(&(expr->conv_exprs), &(conv_expr->list));
982 conv_expr->conv = conv;
983
Willy Tarreau833cc792013-07-24 15:34:19 +0200984 if (endt != endw) {
Willy Tarreaub27c0d32012-04-20 16:04:47 +0200985 int err_arg;
Willy Tarreau21d68a62012-04-20 15:52:36 +0200986
Willy Tarreau9fcb9842012-04-20 14:45:49 +0200987 if (!conv->arg_mask) {
Willy Tarreau975c1782013-12-12 23:16:54 +0100988 memprintf(err_msg, "conv method '%s' does not support any args", ckw);
Emeric Brun485479d2010-09-23 18:02:19 +0200989 goto out_error;
990 }
Willy Tarreau9e92d322010-01-26 17:58:06 +0100991
Willy Tarreaua4312fa2013-04-02 16:34:32 +0200992 al->kw = expr->fetch->kw;
993 al->conv = conv_expr->conv->kw;
Willy Tarreau975c1782013-12-12 23:16:54 +0100994 if (make_arg_list(endw + 1, endt - endw - 1, conv->arg_mask, &conv_expr->arg_p, err_msg, NULL, &err_arg, al) < 0) {
995 memprintf(err_msg, "invalid arg %d in conv method '%s' : %s", err_arg+1, ckw, *err_msg);
Willy Tarreau21d68a62012-04-20 15:52:36 +0200996 goto out_error;
997 }
998
Willy Tarreau2e845be2012-10-19 19:49:09 +0200999 if (!conv_expr->arg_p)
1000 conv_expr->arg_p = empty_arg_list;
1001
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +01001002 if (conv->val_args && !conv->val_args(conv_expr->arg_p, conv, file, line, err_msg)) {
Willy Tarreau975c1782013-12-12 23:16:54 +01001003 memprintf(err_msg, "invalid args in conv method '%s' : %s", ckw, *err_msg);
Emeric Brun485479d2010-09-23 18:02:19 +02001004 goto out_error;
1005 }
1006 }
Willy Tarreau833cc792013-07-24 15:34:19 +02001007 else if (ARGM(conv->arg_mask)) {
Willy Tarreau975c1782013-12-12 23:16:54 +01001008 memprintf(err_msg, "missing args for conv method '%s'", ckw);
Emeric Brun485479d2010-09-23 18:02:19 +02001009 goto out_error;
Emeric Brun107ca302010-01-04 16:16:05 +01001010 }
1011 }
Emeric Brun485479d2010-09-23 18:02:19 +02001012
Willy Tarreau833cc792013-07-24 15:34:19 +02001013 out:
1014 free(fkw);
1015 free(ckw);
Emeric Brun107ca302010-01-04 16:16:05 +01001016 return expr;
1017
1018out_error:
Willy Tarreau12785782012-04-27 21:37:17 +02001019 /* TODO: prune_sample_expr(expr); */
Willy Tarreau833cc792013-07-24 15:34:19 +02001020 expr = NULL;
1021 goto out;
Emeric Brun107ca302010-01-04 16:16:05 +01001022}
1023
1024/*
Willy Tarreau12785782012-04-27 21:37:17 +02001025 * Process a fetch + format conversion of defined by the sample expression <expr>
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001026 * on request or response considering the <opt> parameter.
Willy Tarreau12785782012-04-27 21:37:17 +02001027 * Returns a pointer on a typed sample structure containing the result or NULL if
1028 * sample is not found or when format conversion failed.
Emeric Brun107ca302010-01-04 16:16:05 +01001029 * If <p> is not null, function returns results in structure pointed by <p>.
Willy Tarreau12785782012-04-27 21:37:17 +02001030 * If <p> is null, functions returns a pointer on a static sample structure.
Willy Tarreaub8c8f1f2012-04-23 22:38:26 +02001031 *
1032 * Note: the fetch functions are required to properly set the return type. The
1033 * conversion functions must do so too. However the cast functions do not need
1034 * to since they're made to cast mutiple types according to what is required.
Willy Tarreau6bcb0a82014-07-30 08:56:35 +02001035 *
1036 * The caller may indicate in <opt> if it considers the result final or not.
1037 * The caller needs to check the SMP_F_MAY_CHANGE flag in p->flags to verify
1038 * if the result is stable or not, according to the following table :
1039 *
1040 * return MAY_CHANGE FINAL Meaning for the sample
1041 * NULL 0 * Not present and will never be (eg: header)
1042 * NULL 1 0 Not present yet, could change (eg: POST param)
1043 * NULL 1 1 Not present yet, will not change anymore
1044 * smp 0 * Present and will not change (eg: header)
1045 * smp 1 0 Present, may change (eg: request length)
1046 * smp 1 1 Present, last known value (eg: request length)
Emeric Brun107ca302010-01-04 16:16:05 +01001047 */
Willy Tarreau192252e2015-04-04 01:47:55 +02001048struct sample *sample_process(struct proxy *px, struct session *sess,
1049 struct stream *strm, unsigned int opt,
Willy Tarreau12785782012-04-27 21:37:17 +02001050 struct sample_expr *expr, struct sample *p)
Emeric Brun107ca302010-01-04 16:16:05 +01001051{
Willy Tarreau12785782012-04-27 21:37:17 +02001052 struct sample_conv_expr *conv_expr;
Emeric Brun107ca302010-01-04 16:16:05 +01001053
Willy Tarreau18387e22013-07-25 12:02:38 +02001054 if (p == NULL) {
Willy Tarreaub4a88f02012-04-23 21:35:11 +02001055 p = &temp_smp;
Willy Tarreau6c616e02014-06-25 16:56:41 +02001056 memset(p, 0, sizeof(*p));
Willy Tarreau18387e22013-07-25 12:02:38 +02001057 }
Emeric Brun107ca302010-01-04 16:16:05 +01001058
Willy Tarreau1777ea62016-03-10 16:15:46 +01001059 smp_set_owner(p, px, sess, strm, opt);
Thierry FOURNIER0786d052015-05-11 15:42:45 +02001060 if (!expr->fetch->process(expr->arg_p, p, expr->fetch->kw, expr->fetch->private))
Emeric Brun107ca302010-01-04 16:16:05 +01001061 return NULL;
1062
Emeric Brun107ca302010-01-04 16:16:05 +01001063 list_for_each_entry(conv_expr, &expr->conv_exprs, list) {
Willy Tarreau12e50112012-04-25 17:21:49 +02001064 /* we want to ensure that p->type can be casted into
1065 * conv_expr->conv->in_type. We have 3 possibilities :
1066 * - NULL => not castable.
1067 * - c_none => nothing to do (let's optimize it)
1068 * - other => apply cast and prepare to fail
1069 */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001070 if (!sample_casts[p->data.type][conv_expr->conv->in_type])
Willy Tarreau12e50112012-04-25 17:21:49 +02001071 return NULL;
1072
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001073 if (sample_casts[p->data.type][conv_expr->conv->in_type] != c_none &&
1074 !sample_casts[p->data.type][conv_expr->conv->in_type](p))
Emeric Brun107ca302010-01-04 16:16:05 +01001075 return NULL;
1076
Willy Tarreau12e50112012-04-25 17:21:49 +02001077 /* OK cast succeeded */
1078
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001079 if (!conv_expr->conv->process(conv_expr->arg_p, p, conv_expr->conv->private))
Emeric Brun107ca302010-01-04 16:16:05 +01001080 return NULL;
Emeric Brun107ca302010-01-04 16:16:05 +01001081 }
1082 return p;
1083}
1084
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001085/*
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001086 * Resolve all remaining arguments in proxy <p>. Returns the number of
1087 * errors or 0 if everything is fine.
1088 */
1089int smp_resolve_args(struct proxy *p)
1090{
1091 struct arg_list *cur, *bak;
1092 const char *ctx, *where;
1093 const char *conv_ctx, *conv_pre, *conv_pos;
1094 struct userlist *ul;
Willy Tarreau46947782015-01-19 19:00:58 +01001095 struct my_regex *reg;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001096 struct arg *arg;
1097 int cfgerr = 0;
Willy Tarreau46947782015-01-19 19:00:58 +01001098 int rflags;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001099
1100 list_for_each_entry_safe(cur, bak, &p->conf.args.list, list) {
1101 struct proxy *px;
1102 struct server *srv;
1103 char *pname, *sname;
Willy Tarreau46947782015-01-19 19:00:58 +01001104 char *err;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001105
1106 arg = cur->arg;
1107
1108 /* prepare output messages */
1109 conv_pre = conv_pos = conv_ctx = "";
1110 if (cur->conv) {
1111 conv_ctx = cur->conv;
1112 conv_pre = "conversion keyword '";
1113 conv_pos = "' for ";
1114 }
1115
1116 where = "in";
1117 ctx = "sample fetch keyword";
1118 switch (cur->ctx) {
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001119 case ARGC_STK: where = "in stick rule in"; break;
1120 case ARGC_TRK: where = "in tracking rule in"; break;
1121 case ARGC_LOG: where = "in log-format string in"; break;
1122 case ARGC_LOGSD: where = "in log-format-sd string in"; break;
1123 case ARGC_HRQ: where = "in http-request header format string in"; break;
1124 case ARGC_HRS: where = "in http-response header format string in"; break;
1125 case ARGC_UIF: where = "in unique-id-format string in"; break;
1126 case ARGC_RDR: where = "in redirect format string in"; break;
1127 case ARGC_CAP: where = "in capture rule in"; break;
1128 case ARGC_ACL: ctx = "ACL keyword"; break;
1129 case ARGC_SRV: where = "in server directive in"; break;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001130 }
1131
1132 /* set a few default settings */
1133 px = p;
1134 pname = p->id;
1135
1136 switch (arg->type) {
1137 case ARGT_SRV:
1138 if (!arg->data.str.len) {
1139 Alert("parsing [%s:%d] : missing server name in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1140 cur->file, cur->line,
1141 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
1142 cfgerr++;
1143 continue;
1144 }
1145
1146 /* we support two formats : "bck/srv" and "srv" */
1147 sname = strrchr(arg->data.str.str, '/');
1148
1149 if (sname) {
1150 *sname++ = '\0';
1151 pname = arg->data.str.str;
1152
Willy Tarreau9e0bb102015-05-26 11:24:42 +02001153 px = proxy_be_by_name(pname);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001154 if (!px) {
1155 Alert("parsing [%s:%d] : unable to find proxy '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1156 cur->file, cur->line, pname,
1157 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
1158 cfgerr++;
1159 break;
1160 }
1161 }
1162 else
1163 sname = arg->data.str.str;
1164
1165 srv = findserver(px, sname);
1166 if (!srv) {
1167 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",
1168 cur->file, cur->line, sname, pname,
1169 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
1170 cfgerr++;
1171 break;
1172 }
1173
1174 free(arg->data.str.str);
1175 arg->data.str.str = NULL;
1176 arg->unresolved = 0;
1177 arg->data.srv = srv;
1178 break;
1179
1180 case ARGT_FE:
1181 if (arg->data.str.len) {
1182 pname = arg->data.str.str;
Willy Tarreau9e0bb102015-05-26 11:24:42 +02001183 px = proxy_fe_by_name(pname);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001184 }
1185
1186 if (!px) {
1187 Alert("parsing [%s:%d] : unable to find frontend '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1188 cur->file, cur->line, pname,
1189 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
1190 cfgerr++;
1191 break;
1192 }
1193
1194 if (!(px->cap & PR_CAP_FE)) {
1195 Alert("parsing [%s:%d] : proxy '%s', referenced in arg %d of %s%s%s%s '%s' %s proxy '%s', has not frontend capability.\n",
1196 cur->file, cur->line, pname,
1197 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
1198 cfgerr++;
1199 break;
1200 }
1201
1202 free(arg->data.str.str);
1203 arg->data.str.str = NULL;
1204 arg->unresolved = 0;
1205 arg->data.prx = px;
1206 break;
1207
1208 case ARGT_BE:
1209 if (arg->data.str.len) {
1210 pname = arg->data.str.str;
Willy Tarreau9e0bb102015-05-26 11:24:42 +02001211 px = proxy_be_by_name(pname);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001212 }
1213
1214 if (!px) {
1215 Alert("parsing [%s:%d] : unable to find backend '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1216 cur->file, cur->line, pname,
1217 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
1218 cfgerr++;
1219 break;
1220 }
1221
1222 if (!(px->cap & PR_CAP_BE)) {
1223 Alert("parsing [%s:%d] : proxy '%s', referenced in arg %d of %s%s%s%s '%s' %s proxy '%s', has not backend capability.\n",
1224 cur->file, cur->line, pname,
1225 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
1226 cfgerr++;
1227 break;
1228 }
1229
1230 free(arg->data.str.str);
1231 arg->data.str.str = NULL;
1232 arg->unresolved = 0;
1233 arg->data.prx = px;
1234 break;
1235
1236 case ARGT_TAB:
1237 if (arg->data.str.len) {
1238 pname = arg->data.str.str;
Willy Tarreaue2dc1fa2015-05-26 12:08:07 +02001239 px = proxy_tbl_by_name(pname);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001240 }
1241
1242 if (!px) {
1243 Alert("parsing [%s:%d] : unable to find table '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1244 cur->file, cur->line, pname,
1245 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
1246 cfgerr++;
1247 break;
1248 }
1249
1250 if (!px->table.size) {
1251 Alert("parsing [%s:%d] : no table in proxy '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1252 cur->file, cur->line, pname,
1253 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
1254 cfgerr++;
1255 break;
1256 }
1257
1258 free(arg->data.str.str);
1259 arg->data.str.str = NULL;
1260 arg->unresolved = 0;
1261 arg->data.prx = px;
1262 break;
1263
1264 case ARGT_USR:
1265 if (!arg->data.str.len) {
1266 Alert("parsing [%s:%d] : missing userlist name in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1267 cur->file, cur->line,
1268 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
1269 cfgerr++;
1270 break;
1271 }
1272
1273 if (p->uri_auth && p->uri_auth->userlist &&
1274 !strcmp(p->uri_auth->userlist->name, arg->data.str.str))
1275 ul = p->uri_auth->userlist;
1276 else
1277 ul = auth_find_userlist(arg->data.str.str);
1278
1279 if (!ul) {
1280 Alert("parsing [%s:%d] : unable to find userlist '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1281 cur->file, cur->line, arg->data.str.str,
1282 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
1283 cfgerr++;
1284 break;
1285 }
1286
1287 free(arg->data.str.str);
1288 arg->data.str.str = NULL;
1289 arg->unresolved = 0;
1290 arg->data.usr = ul;
1291 break;
Willy Tarreau46947782015-01-19 19:00:58 +01001292
1293 case ARGT_REG:
1294 if (!arg->data.str.len) {
1295 Alert("parsing [%s:%d] : missing regex in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1296 cur->file, cur->line,
1297 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
1298 cfgerr++;
1299 continue;
1300 }
1301
1302 reg = calloc(1, sizeof(*reg));
1303 if (!reg) {
1304 Alert("parsing [%s:%d] : not enough memory to build regex in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1305 cur->file, cur->line,
1306 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
1307 cfgerr++;
1308 continue;
1309 }
1310
1311 rflags = 0;
1312 rflags |= (arg->type_flags & ARGF_REG_ICASE) ? REG_ICASE : 0;
1313 err = NULL;
1314
Willy Tarreaud817e462015-01-23 20:23:17 +01001315 if (!regex_comp(arg->data.str.str, reg, !(rflags & REG_ICASE), 1 /* capture substr */, &err)) {
Willy Tarreau46947782015-01-19 19:00:58 +01001316 Alert("parsing [%s:%d] : error in regex '%s' in arg %d of %s%s%s%s '%s' %s proxy '%s' : %s.\n",
1317 cur->file, cur->line,
1318 arg->data.str.str,
1319 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id, err);
1320 cfgerr++;
1321 continue;
1322 }
1323
1324 free(arg->data.str.str);
1325 arg->data.str.str = NULL;
1326 arg->unresolved = 0;
1327 arg->data.reg = reg;
1328 break;
1329
1330
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001331 }
1332
1333 LIST_DEL(&cur->list);
1334 free(cur);
1335 } /* end of args processing */
1336
1337 return cfgerr;
1338}
1339
1340/*
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001341 * Process a fetch + format conversion as defined by the sample expression
1342 * <expr> on request or response considering the <opt> parameter. The output is
Adis Nezirovic79beb242015-07-06 15:41:02 +02001343 * not explicitly set to <smp_type>, but shall be compatible with it as
1344 * specified by 'sample_casts' table. If a stable sample can be fetched, or an
1345 * unstable one when <opt> contains SMP_OPT_FINAL, the sample is converted and
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001346 * returned without the SMP_F_MAY_CHANGE flag. If an unstable sample is found
1347 * and <opt> does not contain SMP_OPT_FINAL, then the sample is returned as-is
1348 * with its SMP_F_MAY_CHANGE flag so that the caller can check it and decide to
1349 * take actions (eg: wait longer). If a sample could not be found or could not
Willy Tarreau6bcb0a82014-07-30 08:56:35 +02001350 * be converted, NULL is returned. The caller MUST NOT use the sample if the
1351 * SMP_F_MAY_CHANGE flag is present, as it is used only as a hint that there is
1352 * still hope to get it after waiting longer, and is not converted to string.
1353 * The possible output combinations are the following :
1354 *
1355 * return MAY_CHANGE FINAL Meaning for the sample
1356 * NULL * * Not present and will never be (eg: header)
1357 * smp 0 * Final value converted (eg: header)
1358 * smp 1 0 Not present yet, may appear later (eg: header)
1359 * smp 1 1 never happens (either flag is cleared on output)
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001360 */
Adis Nezirovic79beb242015-07-06 15:41:02 +02001361struct sample *sample_fetch_as_type(struct proxy *px, struct session *sess,
Willy Tarreau192252e2015-04-04 01:47:55 +02001362 struct stream *strm, unsigned int opt,
Adis Nezirovic79beb242015-07-06 15:41:02 +02001363 struct sample_expr *expr, int smp_type)
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001364{
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001365 struct sample *smp = &temp_smp;
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001366
Willy Tarreau6c616e02014-06-25 16:56:41 +02001367 memset(smp, 0, sizeof(*smp));
1368
Willy Tarreau192252e2015-04-04 01:47:55 +02001369 if (!sample_process(px, sess, strm, opt, expr, smp)) {
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001370 if ((smp->flags & SMP_F_MAY_CHANGE) && !(opt & SMP_OPT_FINAL))
1371 return smp;
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001372 return NULL;
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001373 }
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001374
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001375 if (!sample_casts[smp->data.type][smp_type])
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001376 return NULL;
1377
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001378 if (!sample_casts[smp->data.type][smp_type](smp))
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001379 return NULL;
1380
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001381 smp->flags &= ~SMP_F_MAY_CHANGE;
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001382 return smp;
1383}
1384
Emeric Brun107ca302010-01-04 16:16:05 +01001385/*****************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +02001386/* Sample format convert functions */
Willy Tarreaub8c8f1f2012-04-23 22:38:26 +02001387/* These functions set the data type on return. */
Emeric Brun107ca302010-01-04 16:16:05 +01001388/*****************************************************************/
1389
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001390#ifdef DEBUG_EXPR
1391static int sample_conv_debug(const struct arg *arg_p, struct sample *smp, void *private)
1392{
1393 int i;
1394 struct sample tmp;
1395
1396 if (!(global.mode & MODE_QUIET) || (global.mode & (MODE_VERBOSE | MODE_STARTING))) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001397 fprintf(stderr, "[debug converter] type: %s ", smp_to_type[smp->data.type]);
1398 if (!sample_casts[smp->data.type][SMP_T_STR]) {
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001399 fprintf(stderr, "(undisplayable)");
1400 } else {
1401
1402 /* Copy sample fetch. This put the sample as const, the
1403 * cast will copy data if a transformation is required.
1404 */
1405 memcpy(&tmp, smp, sizeof(struct sample));
1406 tmp.flags = SMP_F_CONST;
1407
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001408 if (!sample_casts[smp->data.type][SMP_T_STR](&tmp))
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001409 fprintf(stderr, "(undisplayable)");
1410
1411 else {
1412 /* Display the displayable chars*. */
1413 fprintf(stderr, "<");
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001414 for (i = 0; i < tmp.data.u.str.len; i++) {
1415 if (isprint(tmp.data.u.str.str[i]))
1416 fputc(tmp.data.u.str.str[i], stderr);
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001417 else
1418 fputc('.', stderr);
1419 }
1420 }
1421 fprintf(stderr, ">\n");
1422 }
1423 }
1424 return 1;
1425}
1426#endif
1427
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001428static int sample_conv_bin2base64(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brun53d1a982014-04-30 18:21:37 +02001429{
1430 struct chunk *trash = get_trash_chunk();
1431 int b64_len;
1432
1433 trash->len = 0;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001434 b64_len = a2base64(smp->data.u.str.str, smp->data.u.str.len, trash->str, trash->size);
Emeric Brun53d1a982014-04-30 18:21:37 +02001435 if (b64_len < 0)
1436 return 0;
1437
1438 trash->len = b64_len;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001439 smp->data.u.str = *trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001440 smp->data.type = SMP_T_STR;
Emeric Brun53d1a982014-04-30 18:21:37 +02001441 smp->flags &= ~SMP_F_CONST;
1442 return 1;
1443}
1444
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001445static int sample_conv_bin2hex(const struct arg *arg_p, struct sample *smp, void *private)
Thierry FOURNIER2f49d6d2014-03-12 15:01:52 +01001446{
1447 struct chunk *trash = get_trash_chunk();
1448 unsigned char c;
1449 int ptr = 0;
1450
1451 trash->len = 0;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001452 while (ptr < smp->data.u.str.len && trash->len <= trash->size - 2) {
1453 c = smp->data.u.str.str[ptr++];
Thierry FOURNIER2f49d6d2014-03-12 15:01:52 +01001454 trash->str[trash->len++] = hextab[(c >> 4) & 0xF];
1455 trash->str[trash->len++] = hextab[c & 0xF];
1456 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001457 smp->data.u.str = *trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001458 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01001459 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIER2f49d6d2014-03-12 15:01:52 +01001460 return 1;
1461}
1462
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001463/* hashes the binary input into a 32-bit unsigned int */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001464static int sample_conv_djb2(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001465{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001466 smp->data.u.sint = hash_djb2(smp->data.u.str.str, smp->data.u.str.len);
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001467 if (arg_p && arg_p->data.sint)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001468 smp->data.u.sint = full_hash(smp->data.u.sint);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001469 smp->data.type = SMP_T_SINT;
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001470 return 1;
1471}
1472
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001473static int sample_conv_str2lower(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brun107ca302010-01-04 16:16:05 +01001474{
1475 int i;
1476
Willy Tarreauf0645dc2016-08-09 14:29:38 +02001477 if (!smp_make_rw(smp))
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01001478 return 0;
1479
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001480 for (i = 0; i < smp->data.u.str.len; i++) {
1481 if ((smp->data.u.str.str[i] >= 'A') && (smp->data.u.str.str[i] <= 'Z'))
1482 smp->data.u.str.str[i] += 'a' - 'A';
Emeric Brun107ca302010-01-04 16:16:05 +01001483 }
1484 return 1;
1485}
1486
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001487static int sample_conv_str2upper(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brun107ca302010-01-04 16:16:05 +01001488{
1489 int i;
1490
Willy Tarreauf0645dc2016-08-09 14:29:38 +02001491 if (!smp_make_rw(smp))
Emeric Brun485479d2010-09-23 18:02:19 +02001492 return 0;
1493
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001494 for (i = 0; i < smp->data.u.str.len; i++) {
1495 if ((smp->data.u.str.str[i] >= 'a') && (smp->data.u.str.str[i] <= 'z'))
1496 smp->data.u.str.str[i] += 'A' - 'a';
Emeric Brun107ca302010-01-04 16:16:05 +01001497 }
1498 return 1;
1499}
1500
Willy Tarreauf9954102012-04-20 14:03:29 +02001501/* takes the netmask in arg_p */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001502static int sample_conv_ipmask(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud31d6eb2010-01-26 18:01:41 +01001503{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001504 smp->data.u.ipv4.s_addr &= arg_p->data.ipv4.s_addr;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001505 smp->data.type = SMP_T_IPV4;
Willy Tarreaud31d6eb2010-01-26 18:01:41 +01001506 return 1;
1507}
1508
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001509/* takes an UINT value on input supposed to represent the time since EPOCH,
1510 * adds an optional offset found in args[1] and emits a string representing
1511 * the local time in the format specified in args[1] using strftime().
1512 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001513static int sample_conv_ltime(const struct arg *args, struct sample *smp, void *private)
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001514{
1515 struct chunk *temp;
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001516 /* With high numbers, the date returned can be negative, the 55 bits mask prevent this. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001517 time_t curr_date = smp->data.u.sint & 0x007fffffffffffffLL;
Thierry FOURNIERfac9ccf2015-07-08 00:15:20 +02001518 struct tm *tm;
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001519
1520 /* add offset */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001521 if (args[1].type == ARGT_SINT)
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001522 curr_date += args[1].data.sint;
1523
Thierry FOURNIERfac9ccf2015-07-08 00:15:20 +02001524 tm = localtime(&curr_date);
1525 if (!tm)
1526 return 0;
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001527 temp = get_trash_chunk();
Thierry FOURNIERfac9ccf2015-07-08 00:15:20 +02001528 temp->len = strftime(temp->str, temp->size, args[0].data.str.str, tm);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001529 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001530 smp->data.type = SMP_T_STR;
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001531 return 1;
1532}
1533
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001534/* hashes the binary input into a 32-bit unsigned int */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001535static int sample_conv_sdbm(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001536{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001537 smp->data.u.sint = hash_sdbm(smp->data.u.str.str, smp->data.u.str.len);
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001538 if (arg_p && arg_p->data.sint)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001539 smp->data.u.sint = full_hash(smp->data.u.sint);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001540 smp->data.type = SMP_T_SINT;
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001541 return 1;
1542}
1543
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001544/* takes an UINT value on input supposed to represent the time since EPOCH,
1545 * adds an optional offset found in args[1] and emits a string representing
1546 * the UTC date in the format specified in args[1] using strftime().
1547 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001548static int sample_conv_utime(const struct arg *args, struct sample *smp, void *private)
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001549{
1550 struct chunk *temp;
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001551 /* With high numbers, the date returned can be negative, the 55 bits mask prevent this. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001552 time_t curr_date = smp->data.u.sint & 0x007fffffffffffffLL;
Thierry FOURNIERfac9ccf2015-07-08 00:15:20 +02001553 struct tm *tm;
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001554
1555 /* add offset */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001556 if (args[1].type == ARGT_SINT)
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001557 curr_date += args[1].data.sint;
1558
Thierry FOURNIERfac9ccf2015-07-08 00:15:20 +02001559 tm = gmtime(&curr_date);
1560 if (!tm)
1561 return 0;
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001562 temp = get_trash_chunk();
Thierry FOURNIERfac9ccf2015-07-08 00:15:20 +02001563 temp->len = strftime(temp->str, temp->size, args[0].data.str.str, tm);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001564 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001565 smp->data.type = SMP_T_STR;
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001566 return 1;
1567}
1568
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001569/* hashes the binary input into a 32-bit unsigned int */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001570static int sample_conv_wt6(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001571{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001572 smp->data.u.sint = hash_wt6(smp->data.u.str.str, smp->data.u.str.len);
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001573 if (arg_p && arg_p->data.sint)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001574 smp->data.u.sint = full_hash(smp->data.u.sint);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001575 smp->data.type = SMP_T_SINT;
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001576 return 1;
1577}
1578
Willy Tarreau80599772015-01-20 19:35:24 +01001579/* hashes the binary input into a 32-bit unsigned int */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001580static int sample_conv_crc32(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau80599772015-01-20 19:35:24 +01001581{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001582 smp->data.u.sint = hash_crc32(smp->data.u.str.str, smp->data.u.str.len);
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001583 if (arg_p && arg_p->data.sint)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001584 smp->data.u.sint = full_hash(smp->data.u.sint);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001585 smp->data.type = SMP_T_SINT;
Willy Tarreau80599772015-01-20 19:35:24 +01001586 return 1;
1587}
1588
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001589/* This function escape special json characters. The returned string can be
1590 * safely set between two '"' and used as json string. The json string is
1591 * defined like this:
1592 *
1593 * any Unicode character except '"' or '\' or control character
1594 * \", \\, \/, \b, \f, \n, \r, \t, \u + four-hex-digits
1595 *
1596 * The enum input_type contain all the allowed mode for decoding the input
1597 * string.
1598 */
1599enum input_type {
1600 IT_ASCII = 0,
1601 IT_UTF8,
1602 IT_UTF8S,
1603 IT_UTF8P,
1604 IT_UTF8PS,
1605};
1606static int sample_conv_json_check(struct arg *arg, struct sample_conv *conv,
1607 const char *file, int line, char **err)
1608{
1609 if (!arg) {
1610 memprintf(err, "Unexpected empty arg list");
1611 return 0;
1612 }
1613
1614 if (arg->type != ARGT_STR) {
1615 memprintf(err, "Unexpected arg type");
1616 return 0;
1617 }
1618
1619 if (strcmp(arg->data.str.str, "") == 0) {
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001620 arg->type = ARGT_SINT;
1621 arg->data.sint = IT_ASCII;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001622 return 1;
1623 }
1624
1625 else if (strcmp(arg->data.str.str, "ascii") == 0) {
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001626 arg->type = ARGT_SINT;
1627 arg->data.sint = IT_ASCII;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001628 return 1;
1629 }
1630
1631 else if (strcmp(arg->data.str.str, "utf8") == 0) {
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001632 arg->type = ARGT_SINT;
1633 arg->data.sint = IT_UTF8;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001634 return 1;
1635 }
1636
1637 else if (strcmp(arg->data.str.str, "utf8s") == 0) {
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001638 arg->type = ARGT_SINT;
1639 arg->data.sint = IT_UTF8S;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001640 return 1;
1641 }
1642
1643 else if (strcmp(arg->data.str.str, "utf8p") == 0) {
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001644 arg->type = ARGT_SINT;
1645 arg->data.sint = IT_UTF8P;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001646 return 1;
1647 }
1648
1649 else if (strcmp(arg->data.str.str, "utf8ps") == 0) {
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001650 arg->type = ARGT_SINT;
1651 arg->data.sint = IT_UTF8PS;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001652 return 1;
1653 }
1654
1655 memprintf(err, "Unexpected input code type at file '%s', line %d. "
Herve COMMOWICK8dfe8632016-08-05 12:01:20 +02001656 "Allowed value are 'ascii', 'utf8', 'utf8s', 'utf8p' and 'utf8ps'", file, line);
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001657 return 0;
1658}
1659
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001660static int sample_conv_json(const struct arg *arg_p, struct sample *smp, void *private)
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001661{
1662 struct chunk *temp;
1663 char _str[7]; /* \u + 4 hex digit + null char for sprintf. */
1664 const char *str;
1665 int len;
1666 enum input_type input_type = IT_ASCII;
1667 unsigned int c;
1668 unsigned int ret;
1669 char *p;
1670
1671 if (arg_p)
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001672 input_type = arg_p->data.sint;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001673
1674 temp = get_trash_chunk();
1675 temp->len = 0;
1676
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001677 p = smp->data.u.str.str;
1678 while (p < smp->data.u.str.str + smp->data.u.str.len) {
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001679
1680 if (input_type == IT_ASCII) {
1681 /* Read input as ASCII. */
1682 c = *(unsigned char *)p;
1683 p++;
1684 }
1685 else {
1686 /* Read input as UTF8. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001687 ret = utf8_next(p, smp->data.u.str.len - ( p - smp->data.u.str.str ), &c);
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001688 p += utf8_return_length(ret);
1689
1690 if (input_type == IT_UTF8 && utf8_return_code(ret) != UTF8_CODE_OK)
1691 return 0;
1692 if (input_type == IT_UTF8S && utf8_return_code(ret) != UTF8_CODE_OK)
1693 continue;
1694 if (input_type == IT_UTF8P && utf8_return_code(ret) & (UTF8_CODE_INVRANGE|UTF8_CODE_BADSEQ))
1695 return 0;
1696 if (input_type == IT_UTF8PS && utf8_return_code(ret) & (UTF8_CODE_INVRANGE|UTF8_CODE_BADSEQ))
1697 continue;
1698
1699 /* Check too big values. */
1700 if ((unsigned int)c > 0xffff) {
1701 if (input_type == IT_UTF8 || input_type == IT_UTF8P)
1702 return 0;
1703 continue;
1704 }
1705 }
1706
1707 /* Convert character. */
1708 if (c == '"') {
1709 len = 2;
1710 str = "\\\"";
1711 }
1712 else if (c == '\\') {
1713 len = 2;
1714 str = "\\\\";
1715 }
1716 else if (c == '/') {
1717 len = 2;
1718 str = "\\/";
1719 }
1720 else if (c == '\b') {
1721 len = 2;
1722 str = "\\b";
1723 }
1724 else if (c == '\f') {
1725 len = 2;
1726 str = "\\f";
1727 }
1728 else if (c == '\r') {
1729 len = 2;
1730 str = "\\r";
1731 }
1732 else if (c == '\n') {
1733 len = 2;
1734 str = "\\n";
1735 }
1736 else if (c == '\t') {
1737 len = 2;
1738 str = "\\t";
1739 }
1740 else if (c > 0xff || !isprint(c)) {
1741 /* isprint generate a segfault if c is too big. The man says that
1742 * c must have the value of an unsigned char or EOF.
1743 */
1744 len = 6;
1745 _str[0] = '\\';
1746 _str[1] = 'u';
1747 snprintf(&_str[2], 5, "%04x", (unsigned short)c);
1748 str = _str;
1749 }
1750 else {
1751 len = 1;
1752 str = (char *)&c;
1753 }
1754
1755 /* Check length */
1756 if (temp->len + len > temp->size)
1757 return 0;
1758
1759 /* Copy string. */
1760 memcpy(temp->str + temp->len, str, len);
1761 temp->len += len;
1762 }
1763
1764 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001765 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001766 smp->data.type = SMP_T_STR;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001767
1768 return 1;
1769}
1770
Emeric Brun54c4ac82014-11-03 15:32:43 +01001771/* This sample function is designed to extract some bytes from an input buffer.
1772 * First arg is the offset.
1773 * Optional second arg is the length to truncate */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001774static int sample_conv_bytes(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brun54c4ac82014-11-03 15:32:43 +01001775{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001776 if (smp->data.u.str.len <= arg_p[0].data.sint) {
1777 smp->data.u.str.len = 0;
Emeric Brun54c4ac82014-11-03 15:32:43 +01001778 return 1;
1779 }
1780
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001781 if (smp->data.u.str.size)
1782 smp->data.u.str.size -= arg_p[0].data.sint;
1783 smp->data.u.str.len -= arg_p[0].data.sint;
1784 smp->data.u.str.str += arg_p[0].data.sint;
Emeric Brun54c4ac82014-11-03 15:32:43 +01001785
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001786 if ((arg_p[1].type == ARGT_SINT) && (arg_p[1].data.sint < smp->data.u.str.len))
1787 smp->data.u.str.len = arg_p[1].data.sint;
Emeric Brun54c4ac82014-11-03 15:32:43 +01001788
1789 return 1;
1790}
1791
Emeric Brunf399b0d2014-11-03 17:07:03 +01001792static int sample_conv_field_check(struct arg *args, struct sample_conv *conv,
1793 const char *file, int line, char **err)
1794{
1795 struct arg *arg = args;
1796
1797 if (!arg) {
1798 memprintf(err, "Unexpected empty arg list");
1799 return 0;
1800 }
1801
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001802 if (arg->type != ARGT_SINT) {
Emeric Brunf399b0d2014-11-03 17:07:03 +01001803 memprintf(err, "Unexpected arg type");
1804 return 0;
1805 }
1806
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001807 if (!arg->data.sint) {
Emeric Brunf399b0d2014-11-03 17:07:03 +01001808 memprintf(err, "Unexpected value 0 for index");
1809 return 0;
1810 }
1811
1812 arg++;
1813
1814 if (arg->type != ARGT_STR) {
1815 memprintf(err, "Unexpected arg type");
1816 return 0;
1817 }
1818
1819 if (!arg->data.str.len) {
1820 memprintf(err, "Empty separators list");
1821 return 0;
1822 }
1823
1824 return 1;
1825}
1826
1827/* This sample function is designed to a return selected part of a string (field).
1828 * First arg is the index of the field (start at 1)
1829 * Second arg is a char list of separators (type string)
1830 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001831static int sample_conv_field(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brunf399b0d2014-11-03 17:07:03 +01001832{
1833 unsigned int field;
1834 char *start, *end;
1835 int i;
1836
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001837 if (!arg_p[0].data.sint)
Emeric Brunf399b0d2014-11-03 17:07:03 +01001838 return 0;
1839
1840 field = 1;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001841 end = start = smp->data.u.str.str;
1842 while (end - smp->data.u.str.str < smp->data.u.str.len) {
Emeric Brunf399b0d2014-11-03 17:07:03 +01001843
1844 for (i = 0 ; i < arg_p[1].data.str.len ; i++) {
1845 if (*end == arg_p[1].data.str.str[i]) {
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001846 if (field == arg_p[0].data.sint)
Emeric Brunf399b0d2014-11-03 17:07:03 +01001847 goto found;
1848 start = end+1;
1849 field++;
1850 break;
1851 }
1852 }
1853 end++;
1854 }
1855
1856 /* Field not found */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001857 if (field != arg_p[0].data.sint) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001858 smp->data.u.str.len = 0;
Emeric Brunf399b0d2014-11-03 17:07:03 +01001859 return 1;
1860 }
1861found:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001862 smp->data.u.str.len = end - start;
Emeric Brunf399b0d2014-11-03 17:07:03 +01001863 /* If ret string is len 0, no need to
1864 change pointers or to update size */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001865 if (!smp->data.u.str.len)
Emeric Brunf399b0d2014-11-03 17:07:03 +01001866 return 1;
1867
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001868 smp->data.u.str.str = start;
Emeric Brunf399b0d2014-11-03 17:07:03 +01001869
1870 /* Compute remaining size if needed
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001871 Note: smp->data.u.str.size cannot be set to 0 */
1872 if (smp->data.u.str.size)
1873 smp->data.u.str.size -= start - smp->data.u.str.str;
Emeric Brunf399b0d2014-11-03 17:07:03 +01001874
1875 return 1;
1876}
1877
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01001878/* This sample function is designed to return a word from a string.
1879 * First arg is the index of the word (start at 1)
1880 * Second arg is a char list of words separators (type string)
1881 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001882static int sample_conv_word(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01001883{
1884 unsigned int word;
1885 char *start, *end;
1886 int i, issep, inword;
1887
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001888 if (!arg_p[0].data.sint)
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01001889 return 0;
1890
1891 word = 0;
1892 inword = 0;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001893 end = start = smp->data.u.str.str;
1894 while (end - smp->data.u.str.str < smp->data.u.str.len) {
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01001895 issep = 0;
1896 for (i = 0 ; i < arg_p[1].data.str.len ; i++) {
1897 if (*end == arg_p[1].data.str.str[i]) {
1898 issep = 1;
1899 break;
1900 }
1901 }
1902 if (!inword) {
1903 if (!issep) {
1904 word++;
1905 start = end;
1906 inword = 1;
1907 }
1908 }
1909 else if (issep) {
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001910 if (word == arg_p[0].data.sint)
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01001911 goto found;
1912 inword = 0;
1913 }
1914 end++;
1915 }
1916
1917 /* Field not found */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001918 if (word != arg_p[0].data.sint) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001919 smp->data.u.str.len = 0;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01001920 return 1;
1921 }
1922found:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001923 smp->data.u.str.len = end - start;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01001924 /* If ret string is len 0, no need to
1925 change pointers or to update size */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001926 if (!smp->data.u.str.len)
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01001927 return 1;
1928
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001929 smp->data.u.str.str = start;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01001930
1931 /* Compute remaining size if needed
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001932 Note: smp->data.u.str.size cannot be set to 0 */
1933 if (smp->data.u.str.size)
1934 smp->data.u.str.size -= start - smp->data.u.str.str;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01001935
1936 return 1;
1937}
1938
Willy Tarreau7eda8492015-01-20 19:47:06 +01001939static int sample_conv_regsub_check(struct arg *args, struct sample_conv *conv,
1940 const char *file, int line, char **err)
1941{
1942 struct arg *arg = args;
1943 char *p;
1944 int len;
1945
1946 /* arg0 is a regex, it uses type_flag for ICASE and global match */
1947 arg[0].type_flags = 0;
1948
1949 if (arg[2].type != ARGT_STR)
1950 return 1;
1951
1952 p = arg[2].data.str.str;
1953 len = arg[2].data.str.len;
1954 while (len) {
1955 if (*p == 'i') {
1956 arg[0].type_flags |= ARGF_REG_ICASE;
1957 }
1958 else if (*p == 'g') {
1959 arg[0].type_flags |= ARGF_REG_GLOB;
1960 }
1961 else {
1962 memprintf(err, "invalid regex flag '%c', only 'i' and 'g' are supported", *p);
1963 return 0;
1964 }
1965 p++;
1966 len--;
1967 }
1968 return 1;
1969}
1970
1971/* This sample function is designed to do the equivalent of s/match/replace/ on
1972 * the input string. It applies a regex and restarts from the last matched
1973 * location until nothing matches anymore. First arg is the regex to apply to
1974 * the input string, second arg is the replacement expression.
1975 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001976static int sample_conv_regsub(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau7eda8492015-01-20 19:47:06 +01001977{
1978 char *start, *end;
1979 struct my_regex *reg = arg_p[0].data.reg;
1980 regmatch_t pmatch[MAX_MATCH];
1981 struct chunk *trash = get_trash_chunk();
1982 int flag, max;
1983 int found;
1984
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001985 start = smp->data.u.str.str;
1986 end = start + smp->data.u.str.len;
Willy Tarreau7eda8492015-01-20 19:47:06 +01001987
1988 flag = 0;
1989 while (1) {
1990 /* check for last round which is used to copy remaining parts
1991 * when not running in global replacement mode.
1992 */
1993 found = 0;
1994 if ((arg_p[0].type_flags & ARGF_REG_GLOB) || !(flag & REG_NOTBOL)) {
1995 /* Note: we can have start == end on empty strings or at the end */
1996 found = regex_exec_match2(reg, start, end - start, MAX_MATCH, pmatch, flag);
1997 }
1998
1999 if (!found)
2000 pmatch[0].rm_so = end - start;
2001
2002 /* copy the heading non-matching part (which may also be the tail if nothing matches) */
2003 max = trash->size - trash->len;
2004 if (max && pmatch[0].rm_so > 0) {
2005 if (max > pmatch[0].rm_so)
2006 max = pmatch[0].rm_so;
2007 memcpy(trash->str + trash->len, start, max);
2008 trash->len += max;
2009 }
2010
2011 if (!found)
2012 break;
2013
2014 /* replace the matching part */
2015 max = trash->size - trash->len;
2016 if (max) {
2017 if (max > arg_p[1].data.str.len)
2018 max = arg_p[1].data.str.len;
2019 memcpy(trash->str + trash->len, arg_p[1].data.str.str, max);
2020 trash->len += max;
2021 }
2022
2023 /* stop here if we're done with this string */
2024 if (start >= end)
2025 break;
2026
2027 /* We have a special case for matches of length 0 (eg: "x*y*").
2028 * These ones are considered to match in front of a character,
2029 * so we have to copy that character and skip to the next one.
2030 */
2031 if (!pmatch[0].rm_eo) {
2032 if (trash->len < trash->size)
2033 trash->str[trash->len++] = start[pmatch[0].rm_eo];
2034 pmatch[0].rm_eo++;
2035 }
2036
2037 start += pmatch[0].rm_eo;
2038 flag |= REG_NOTBOL;
2039 }
2040
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002041 smp->data.u.str = *trash;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002042 return 1;
2043}
2044
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002045/* This function check an operator entry. It expects a string.
2046 * The string can be an integer or a variable name.
2047 */
2048static int check_operator(struct arg *args, struct sample_conv *conv,
2049 const char *file, int line, char **err)
2050{
2051 const char *str;
2052 const char *end;
2053
2054 /* Try to decode a variable. */
2055 if (vars_check_arg(&args[0], NULL))
2056 return 1;
2057
2058 /* Try to convert an integer */
2059 str = args[0].data.str.str;
2060 end = str + strlen(str);
2061 args[0].data.sint = read_int64(&str, end);
2062 if (*str != '\0') {
2063 memprintf(err, "expects an integer or a variable name");
2064 return 0;
2065 }
2066 args[0].type = ARGT_SINT;
2067 return 1;
2068}
2069
Willy Tarreau6204cd92016-03-10 16:33:04 +01002070/* This function returns a sample struct filled with an arg content.
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002071 * If the arg contain an integer, the integer is returned in the
2072 * sample. If the arg contains a variable descriptor, it returns the
2073 * variable value.
2074 *
2075 * This function returns 0 if an error occurs, otherwise it returns 1.
2076 */
Willy Tarreau6204cd92016-03-10 16:33:04 +01002077static inline int sample_conv_var2smp(const struct arg *arg, struct sample *smp)
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002078{
2079 switch (arg->type) {
2080 case ARGT_SINT:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002081 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002082 smp->data.u.sint = arg->data.sint;
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002083 return 1;
2084 case ARGT_VAR:
Willy Tarreau6204cd92016-03-10 16:33:04 +01002085 if (!vars_get_by_desc(&arg->data.var, smp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002086 return 0;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002087 if (!sample_casts[smp->data.type][SMP_T_SINT])
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002088 return 0;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002089 if (!sample_casts[smp->data.type][SMP_T_SINT](smp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002090 return 0;
2091 return 1;
2092 default:
2093 return 0;
2094 }
2095}
2096
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002097/* Takes a SINT on input, applies a binary twos complement and returns the SINT
Willy Tarreau97707872015-01-27 15:12:13 +01002098 * result.
2099 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002100static int sample_conv_binary_cpl(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002101{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002102 smp->data.u.sint = ~smp->data.u.sint;
Willy Tarreau97707872015-01-27 15:12:13 +01002103 return 1;
2104}
2105
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002106/* Takes a SINT on input, applies a binary "and" with the SINT directly in
2107 * arg_p or in the varaible described in arg_p, and returns the SINT result.
Willy Tarreau97707872015-01-27 15:12:13 +01002108 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002109static int sample_conv_binary_and(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002110{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002111 struct sample tmp;
2112
Willy Tarreau7560dd42016-03-10 16:28:58 +01002113 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreau6204cd92016-03-10 16:33:04 +01002114 if (!sample_conv_var2smp(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002115 return 0;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002116 smp->data.u.sint &= tmp.data.u.sint;
Willy Tarreau97707872015-01-27 15:12:13 +01002117 return 1;
2118}
2119
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002120/* Takes a SINT on input, applies a binary "or" with the SINT directly in
2121 * arg_p or in the varaible described in arg_p, and returns the SINT result.
Willy Tarreau97707872015-01-27 15:12:13 +01002122 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002123static int sample_conv_binary_or(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002124{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002125 struct sample tmp;
2126
Willy Tarreau7560dd42016-03-10 16:28:58 +01002127 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreau6204cd92016-03-10 16:33:04 +01002128 if (!sample_conv_var2smp(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002129 return 0;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002130 smp->data.u.sint |= tmp.data.u.sint;
Willy Tarreau97707872015-01-27 15:12:13 +01002131 return 1;
2132}
2133
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002134/* Takes a SINT on input, applies a binary "xor" with the SINT directly in
2135 * arg_p or in the varaible described in arg_p, and returns the SINT result.
Willy Tarreau97707872015-01-27 15:12:13 +01002136 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002137static int sample_conv_binary_xor(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002138{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002139 struct sample tmp;
2140
Willy Tarreau7560dd42016-03-10 16:28:58 +01002141 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreau6204cd92016-03-10 16:33:04 +01002142 if (!sample_conv_var2smp(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002143 return 0;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002144 smp->data.u.sint ^= tmp.data.u.sint;
Willy Tarreau97707872015-01-27 15:12:13 +01002145 return 1;
2146}
2147
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002148static inline long long int arith_add(long long int a, long long int b)
2149{
2150 /* Prevent overflow and makes capped calculus.
2151 * We must ensure that the check calculus doesn't
2152 * exceed the signed 64 bits limits.
2153 *
2154 * +----------+----------+
2155 * | a<0 | a>=0 |
2156 * +------+----------+----------+
2157 * | b<0 | MIN-a>b | no check |
2158 * +------+----------+----------+
2159 * | b>=0 | no check | MAX-a<b |
2160 * +------+----------+----------+
2161 */
2162 if ((a ^ b) >= 0) {
2163 /* signs are differents. */
2164 if (a < 0) {
2165 if (LLONG_MIN - a > b)
2166 return LLONG_MIN;
2167 }
2168 if (LLONG_MAX - a < b)
2169 return LLONG_MAX;
2170 }
2171 return a + b;
2172}
2173
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002174/* Takes a SINT on input, applies an arithmetic "add" with the SINT directly in
2175 * arg_p or in the varaible described in arg_p, and returns the SINT result.
Willy Tarreau97707872015-01-27 15:12:13 +01002176 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002177static int sample_conv_arith_add(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002178{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002179 struct sample tmp;
2180
Willy Tarreau7560dd42016-03-10 16:28:58 +01002181 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreau6204cd92016-03-10 16:33:04 +01002182 if (!sample_conv_var2smp(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002183 return 0;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002184 smp->data.u.sint = arith_add(smp->data.u.sint, tmp.data.u.sint);
Willy Tarreau97707872015-01-27 15:12:13 +01002185 return 1;
2186}
2187
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002188/* Takes a SINT on input, applies an arithmetic "sub" with the SINT directly in
2189 * arg_p or in the varaible described in arg_p, and returns the SINT result.
Willy Tarreau97707872015-01-27 15:12:13 +01002190 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002191static int sample_conv_arith_sub(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002192 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002193{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002194 struct sample tmp;
2195
Willy Tarreau7560dd42016-03-10 16:28:58 +01002196 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreau6204cd92016-03-10 16:33:04 +01002197 if (!sample_conv_var2smp(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002198 return 0;
2199
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002200 /* We cannot represent -LLONG_MIN because abs(LLONG_MIN) is greater
2201 * than abs(LLONG_MAX). So, the following code use LLONG_MAX in place
2202 * of -LLONG_MIN and correct the result.
2203 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002204 if (tmp.data.u.sint == LLONG_MIN) {
2205 smp->data.u.sint = arith_add(smp->data.u.sint, LLONG_MAX);
2206 if (smp->data.u.sint < LLONG_MAX)
2207 smp->data.u.sint++;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002208 return 1;
2209 }
2210
2211 /* standard substraction: we use the "add" function and negate
2212 * the second operand.
2213 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002214 smp->data.u.sint = arith_add(smp->data.u.sint, -tmp.data.u.sint);
Willy Tarreau97707872015-01-27 15:12:13 +01002215 return 1;
2216}
2217
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002218/* Takes a SINT on input, applies an arithmetic "mul" with the SINT directly in
2219 * arg_p or in the varaible described in arg_p, and returns the SINT result.
2220 * If the result makes an overflow, then the largest possible quantity is
2221 * returned.
Willy Tarreau97707872015-01-27 15:12:13 +01002222 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002223static int sample_conv_arith_mul(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002224 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002225{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002226 struct sample tmp;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002227 long long int c;
2228
Willy Tarreau7560dd42016-03-10 16:28:58 +01002229 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreau6204cd92016-03-10 16:33:04 +01002230 if (!sample_conv_var2smp(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002231 return 0;
2232
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002233 /* prevent divide by 0 during the check */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002234 if (!smp->data.u.sint || !tmp.data.u.sint) {
2235 smp->data.u.sint = 0;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002236 return 1;
2237 }
2238
2239 /* The multiply between LLONG_MIN and -1 returns a
2240 * "floting point exception".
2241 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002242 if (smp->data.u.sint == LLONG_MIN && tmp.data.u.sint == -1) {
2243 smp->data.u.sint = LLONG_MAX;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002244 return 1;
2245 }
2246
2247 /* execute standard multiplication. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002248 c = smp->data.u.sint * tmp.data.u.sint;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002249
2250 /* check for overflow and makes capped multiply. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002251 if (smp->data.u.sint != c / tmp.data.u.sint) {
2252 if ((smp->data.u.sint < 0) == (tmp.data.u.sint < 0)) {
2253 smp->data.u.sint = LLONG_MAX;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002254 return 1;
2255 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002256 smp->data.u.sint = LLONG_MIN;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002257 return 1;
2258 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002259 smp->data.u.sint = c;
Willy Tarreau97707872015-01-27 15:12:13 +01002260 return 1;
2261}
2262
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002263/* Takes a SINT on input, applies an arithmetic "div" with the SINT directly in
2264 * arg_p or in the varaible described in arg_p, and returns the SINT result.
2265 * If arg_p makes the result overflow, then the largest possible quantity is
2266 * returned.
Willy Tarreau97707872015-01-27 15:12:13 +01002267 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002268static int sample_conv_arith_div(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002269 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002270{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002271 struct sample tmp;
2272
Willy Tarreau7560dd42016-03-10 16:28:58 +01002273 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreau6204cd92016-03-10 16:33:04 +01002274 if (!sample_conv_var2smp(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002275 return 0;
2276
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002277 if (tmp.data.u.sint) {
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002278 /* The divide between LLONG_MIN and -1 returns a
2279 * "floting point exception".
2280 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002281 if (smp->data.u.sint == LLONG_MIN && tmp.data.u.sint == -1) {
2282 smp->data.u.sint = LLONG_MAX;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002283 return 1;
2284 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002285 smp->data.u.sint /= tmp.data.u.sint;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002286 return 1;
2287 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002288 smp->data.u.sint = LLONG_MAX;
Willy Tarreau97707872015-01-27 15:12:13 +01002289 return 1;
2290}
2291
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002292/* Takes a SINT on input, applies an arithmetic "mod" with the SINT directly in
2293 * arg_p or in the varaible described in arg_p, and returns the SINT result.
2294 * If arg_p makes the result overflow, then 0 is returned.
Willy Tarreau97707872015-01-27 15:12:13 +01002295 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002296static int sample_conv_arith_mod(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002297 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002298{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002299 struct sample tmp;
2300
Willy Tarreau7560dd42016-03-10 16:28:58 +01002301 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreau6204cd92016-03-10 16:33:04 +01002302 if (!sample_conv_var2smp(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002303 return 0;
2304
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002305 if (tmp.data.u.sint) {
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002306 /* The divide between LLONG_MIN and -1 returns a
2307 * "floting point exception".
2308 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002309 if (smp->data.u.sint == LLONG_MIN && tmp.data.u.sint == -1) {
2310 smp->data.u.sint = 0;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002311 return 1;
2312 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002313 smp->data.u.sint %= tmp.data.u.sint;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002314 return 1;
2315 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002316 smp->data.u.sint = 0;
Willy Tarreau97707872015-01-27 15:12:13 +01002317 return 1;
2318}
2319
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002320/* Takes an SINT on input, applies an arithmetic "neg" and returns the SINT
Willy Tarreau97707872015-01-27 15:12:13 +01002321 * result.
2322 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002323static int sample_conv_arith_neg(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002324 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002325{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002326 if (smp->data.u.sint == LLONG_MIN)
2327 smp->data.u.sint = LLONG_MAX;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002328 else
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002329 smp->data.u.sint = -smp->data.u.sint;
Willy Tarreau97707872015-01-27 15:12:13 +01002330 return 1;
2331}
2332
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002333/* Takes a SINT on input, returns true is the value is non-null, otherwise
Willy Tarreau97707872015-01-27 15:12:13 +01002334 * false. The output is a BOOL.
2335 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002336static int sample_conv_arith_bool(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002337 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002338{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002339 smp->data.u.sint = !!smp->data.u.sint;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002340 smp->data.type = SMP_T_BOOL;
Willy Tarreau97707872015-01-27 15:12:13 +01002341 return 1;
2342}
2343
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002344/* Takes a SINT on input, returns false is the value is non-null, otherwise
Willy Tarreau97707872015-01-27 15:12:13 +01002345 * truee. The output is a BOOL.
2346 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002347static int sample_conv_arith_not(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002348 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002349{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002350 smp->data.u.sint = !smp->data.u.sint;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002351 smp->data.type = SMP_T_BOOL;
Willy Tarreau97707872015-01-27 15:12:13 +01002352 return 1;
2353}
2354
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002355/* Takes a SINT on input, returns true is the value is odd, otherwise false.
Willy Tarreau97707872015-01-27 15:12:13 +01002356 * The output is a BOOL.
2357 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002358static int sample_conv_arith_odd(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002359 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002360{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002361 smp->data.u.sint = smp->data.u.sint & 1;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002362 smp->data.type = SMP_T_BOOL;
Willy Tarreau97707872015-01-27 15:12:13 +01002363 return 1;
2364}
2365
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002366/* Takes a SINT on input, returns true is the value is even, otherwise false.
Willy Tarreau97707872015-01-27 15:12:13 +01002367 * The output is a BOOL.
2368 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002369static int sample_conv_arith_even(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002370 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002371{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002372 smp->data.u.sint = !(smp->data.u.sint & 1);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002373 smp->data.type = SMP_T_BOOL;
Willy Tarreau97707872015-01-27 15:12:13 +01002374 return 1;
2375}
2376
Willy Tarreau5b8ad222013-07-25 12:17:57 +02002377/************************************************************************/
2378/* All supported sample fetch functions must be declared here */
2379/************************************************************************/
2380
2381/* force TRUE to be returned at the fetch level */
2382static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002383smp_fetch_true(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau5b8ad222013-07-25 12:17:57 +02002384{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002385 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002386 smp->data.u.sint = 1;
Willy Tarreau5b8ad222013-07-25 12:17:57 +02002387 return 1;
2388}
2389
2390/* force FALSE to be returned at the fetch level */
2391static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002392smp_fetch_false(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau5b8ad222013-07-25 12:17:57 +02002393{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002394 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002395 smp->data.u.sint = 0;
Willy Tarreau5b8ad222013-07-25 12:17:57 +02002396 return 1;
2397}
2398
2399/* retrieve environment variable $1 as a string */
2400static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002401smp_fetch_env(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau5b8ad222013-07-25 12:17:57 +02002402{
2403 char *env;
2404
2405 if (!args || args[0].type != ARGT_STR)
2406 return 0;
2407
2408 env = getenv(args[0].data.str.str);
2409 if (!env)
2410 return 0;
2411
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002412 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01002413 smp->flags = SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002414 smp->data.u.str.str = env;
2415 smp->data.u.str.len = strlen(env);
Willy Tarreau5b8ad222013-07-25 12:17:57 +02002416 return 1;
2417}
2418
Willy Tarreau6236d3a2013-07-25 14:28:25 +02002419/* retrieve the current local date in epoch time, and applies an optional offset
2420 * of args[0] seconds.
2421 */
2422static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002423smp_fetch_date(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau6236d3a2013-07-25 14:28:25 +02002424{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002425 smp->data.u.sint = date.tv_sec;
Willy Tarreau6236d3a2013-07-25 14:28:25 +02002426
2427 /* add offset */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002428 if (args && args[0].type == ARGT_SINT)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002429 smp->data.u.sint += args[0].data.sint;
Willy Tarreau6236d3a2013-07-25 14:28:25 +02002430
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002431 smp->data.type = SMP_T_SINT;
Willy Tarreau6236d3a2013-07-25 14:28:25 +02002432 smp->flags |= SMP_F_VOL_TEST | SMP_F_MAY_CHANGE;
2433 return 1;
2434}
2435
Willy Tarreau0f30d262014-11-24 16:02:05 +01002436/* returns the number of processes */
2437static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002438smp_fetch_nbproc(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau0f30d262014-11-24 16:02:05 +01002439{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002440 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002441 smp->data.u.sint = global.nbproc;
Willy Tarreau0f30d262014-11-24 16:02:05 +01002442 return 1;
2443}
2444
2445/* returns the number of the current process (between 1 and nbproc */
2446static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002447smp_fetch_proc(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau0f30d262014-11-24 16:02:05 +01002448{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002449 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002450 smp->data.u.sint = relative_pid;
Willy Tarreau0f30d262014-11-24 16:02:05 +01002451 return 1;
2452}
2453
Willy Tarreau84310e22014-02-14 11:59:04 +01002454/* generate a random 32-bit integer for whatever purpose, with an optional
2455 * range specified in argument.
2456 */
2457static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002458smp_fetch_rand(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau84310e22014-02-14 11:59:04 +01002459{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002460 smp->data.u.sint = random();
Willy Tarreau84310e22014-02-14 11:59:04 +01002461
2462 /* reduce if needed. Don't do a modulo, use all bits! */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002463 if (args && args[0].type == ARGT_SINT)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002464 smp->data.u.sint = (smp->data.u.sint * args[0].data.sint) / ((u64)RAND_MAX+1);
Willy Tarreau84310e22014-02-14 11:59:04 +01002465
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002466 smp->data.type = SMP_T_SINT;
Willy Tarreau84310e22014-02-14 11:59:04 +01002467 smp->flags |= SMP_F_VOL_TEST | SMP_F_MAY_CHANGE;
2468 return 1;
2469}
2470
Willy Tarreau0f30d262014-11-24 16:02:05 +01002471/* returns true if the current process is stopping */
2472static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002473smp_fetch_stopping(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau0f30d262014-11-24 16:02:05 +01002474{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002475 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002476 smp->data.u.sint = stopping;
Willy Tarreau0f30d262014-11-24 16:02:05 +01002477 return 1;
2478}
2479
Thierry FOURNIERcc103292015-06-06 19:30:17 +02002480static int smp_fetch_const_str(const struct arg *args, struct sample *smp, const char *kw, void *private)
2481{
2482 smp->flags |= SMP_F_CONST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002483 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002484 smp->data.u.str.str = args[0].data.str.str;
2485 smp->data.u.str.len = args[0].data.str.len;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02002486 return 1;
2487}
2488
2489static int smp_check_const_bool(struct arg *args, char **err)
2490{
2491 if (strcasecmp(args[0].data.str.str, "true") == 0 ||
2492 strcasecmp(args[0].data.str.str, "1") == 0) {
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002493 args[0].type = ARGT_SINT;
2494 args[0].data.sint = 1;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02002495 return 1;
2496 }
2497 if (strcasecmp(args[0].data.str.str, "false") == 0 ||
2498 strcasecmp(args[0].data.str.str, "0") == 0) {
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002499 args[0].type = ARGT_SINT;
2500 args[0].data.sint = 0;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02002501 return 1;
2502 }
2503 memprintf(err, "Expects 'true', 'false', '0' or '1'");
2504 return 0;
2505}
2506
2507static int smp_fetch_const_bool(const struct arg *args, struct sample *smp, const char *kw, void *private)
2508{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002509 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002510 smp->data.u.sint = args[0].data.sint;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02002511 return 1;
2512}
2513
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002514static int smp_fetch_const_int(const struct arg *args, struct sample *smp, const char *kw, void *private)
Thierry FOURNIERcc103292015-06-06 19:30:17 +02002515{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002516 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002517 smp->data.u.sint = args[0].data.sint;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02002518 return 1;
2519}
2520
2521static int smp_fetch_const_ipv4(const struct arg *args, struct sample *smp, const char *kw, void *private)
2522{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002523 smp->data.type = SMP_T_IPV4;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002524 smp->data.u.ipv4 = args[0].data.ipv4;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02002525 return 1;
2526}
2527
2528static int smp_fetch_const_ipv6(const struct arg *args, struct sample *smp, const char *kw, void *private)
2529{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002530 smp->data.type = SMP_T_IPV6;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002531 smp->data.u.ipv6 = args[0].data.ipv6;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02002532 return 1;
2533}
2534
2535static int smp_check_const_bin(struct arg *args, char **err)
2536{
David Carlier64a16ab2016-04-08 10:37:02 +01002537 char *binstr = NULL;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02002538 int binstrlen;
2539
2540 if (!parse_binary(args[0].data.str.str, &binstr, &binstrlen, err))
2541 return 0;
2542 args[0].type = ARGT_STR;
2543 args[0].data.str.str = binstr;
2544 args[0].data.str.len = binstrlen;
2545 return 1;
2546}
2547
2548static int smp_fetch_const_bin(const struct arg *args, struct sample *smp, const char *kw, void *private)
2549{
2550 smp->flags |= SMP_F_CONST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002551 smp->data.type = SMP_T_BIN;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002552 smp->data.u.str.str = args[0].data.str.str;
2553 smp->data.u.str.len = args[0].data.str.len;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02002554 return 1;
2555}
2556
2557static int smp_check_const_meth(struct arg *args, char **err)
2558{
2559 enum http_meth_t meth;
2560 int i;
2561
2562 meth = find_http_meth(args[0].data.str.str, args[0].data.str.len);
2563 if (meth != HTTP_METH_OTHER) {
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002564 args[0].type = ARGT_SINT;
2565 args[0].data.sint = meth;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02002566 } else {
2567 /* Check method avalaibility. A methos is a token defined as :
2568 * tchar = "!" / "#" / "$" / "%" / "&" / "'" / "*" / "+" / "-" / "." /
2569 * "^" / "_" / "`" / "|" / "~" / DIGIT / ALPHA
2570 * token = 1*tchar
2571 */
2572 for (i = 0; i < args[0].data.str.len; i++) {
2573 if (!http_is_token[(unsigned char)args[0].data.str.str[i]]) {
2574 memprintf(err, "expects valid method.");
2575 return 0;
2576 }
2577 }
2578 }
2579 return 1;
2580}
2581
2582static int smp_fetch_const_meth(const struct arg *args, struct sample *smp, const char *kw, void *private)
2583{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002584 smp->data.type = SMP_T_METH;
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002585 if (args[0].type == ARGT_SINT) {
Thierry FOURNIERcc103292015-06-06 19:30:17 +02002586 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002587 smp->data.u.meth.meth = args[0].data.sint;
2588 smp->data.u.meth.str.str = "";
2589 smp->data.u.meth.str.len = 0;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02002590 } else {
2591 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002592 smp->data.u.meth.meth = HTTP_METH_OTHER;
2593 smp->data.u.meth.str.str = args[0].data.str.str;
2594 smp->data.u.meth.str.len = args[0].data.str.len;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02002595 }
2596 return 1;
2597}
2598
Willy Tarreau5b8ad222013-07-25 12:17:57 +02002599/* Note: must not be declared <const> as its list will be overwritten.
2600 * Note: fetches that may return multiple types must be declared as the lowest
2601 * common denominator, the type that can be casted into all other ones. For
2602 * instance IPv4/IPv6 must be declared IPv4.
2603 */
2604static struct sample_fetch_kw_list smp_kws = {ILH, {
2605 { "always_false", smp_fetch_false, 0, NULL, SMP_T_BOOL, SMP_USE_INTRN },
2606 { "always_true", smp_fetch_true, 0, NULL, SMP_T_BOOL, SMP_USE_INTRN },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01002607 { "env", smp_fetch_env, ARG1(1,STR), NULL, SMP_T_STR, SMP_USE_INTRN },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002608 { "date", smp_fetch_date, ARG1(0,SINT), NULL, SMP_T_SINT, SMP_USE_INTRN },
2609 { "nbproc", smp_fetch_nbproc,0, NULL, SMP_T_SINT, SMP_USE_INTRN },
2610 { "proc", smp_fetch_proc, 0, NULL, SMP_T_SINT, SMP_USE_INTRN },
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002611 { "rand", smp_fetch_rand, ARG1(0,SINT), NULL, SMP_T_SINT, SMP_USE_INTRN },
Willy Tarreau0f30d262014-11-24 16:02:05 +01002612 { "stopping", smp_fetch_stopping, 0, NULL, SMP_T_BOOL, SMP_USE_INTRN },
Thierry FOURNIERcc103292015-06-06 19:30:17 +02002613
2614 { "str", smp_fetch_const_str, ARG1(1,STR), NULL , SMP_T_STR, SMP_USE_INTRN },
2615 { "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 +02002616 { "int", smp_fetch_const_int, ARG1(1,SINT), NULL , SMP_T_SINT, SMP_USE_INTRN },
Thierry FOURNIERcc103292015-06-06 19:30:17 +02002617 { "ipv4", smp_fetch_const_ipv4, ARG1(1,IPV4), NULL , SMP_T_IPV4, SMP_USE_INTRN },
2618 { "ipv6", smp_fetch_const_ipv6, ARG1(1,IPV6), NULL , SMP_T_IPV6, SMP_USE_INTRN },
2619 { "bin", smp_fetch_const_bin, ARG1(1,STR), smp_check_const_bin , SMP_T_BIN, SMP_USE_INTRN },
2620 { "meth", smp_fetch_const_meth, ARG1(1,STR), smp_check_const_meth, SMP_T_METH, SMP_USE_INTRN },
2621
Willy Tarreau5b8ad222013-07-25 12:17:57 +02002622 { /* END */ },
2623}};
2624
Emeric Brun107ca302010-01-04 16:16:05 +01002625/* Note: must not be declared <const> as its list will be overwritten */
Willy Tarreaudc13c112013-06-21 23:16:39 +02002626static struct sample_conv_kw_list sample_conv_kws = {ILH, {
Thierry FOURNIER9687c772015-05-07 15:46:29 +02002627#ifdef DEBUG_EXPR
2628 { "debug", sample_conv_debug, 0, NULL, SMP_T_ANY, SMP_T_ANY },
2629#endif
2630
Emeric Brun53d1a982014-04-30 18:21:37 +02002631 { "base64", sample_conv_bin2base64,0, NULL, SMP_T_BIN, SMP_T_STR },
Willy Tarreau12785782012-04-27 21:37:17 +02002632 { "upper", sample_conv_str2upper, 0, NULL, SMP_T_STR, SMP_T_STR },
2633 { "lower", sample_conv_str2lower, 0, NULL, SMP_T_STR, SMP_T_STR },
Thierry FOURNIER2f49d6d2014-03-12 15:01:52 +01002634 { "hex", sample_conv_bin2hex, 0, NULL, SMP_T_BIN, SMP_T_STR },
Willy Tarreau12785782012-04-27 21:37:17 +02002635 { "ipmask", sample_conv_ipmask, ARG1(1,MSK4), NULL, SMP_T_IPV4, SMP_T_IPV4 },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002636 { "ltime", sample_conv_ltime, ARG2(1,STR,SINT), NULL, SMP_T_SINT, SMP_T_STR },
2637 { "utime", sample_conv_utime, ARG2(1,STR,SINT), NULL, SMP_T_SINT, SMP_T_STR },
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002638 { "crc32", sample_conv_crc32, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
2639 { "djb2", sample_conv_djb2, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
2640 { "sdbm", sample_conv_sdbm, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
2641 { "wt6", sample_conv_wt6, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002642 { "json", sample_conv_json, ARG1(1,STR), sample_conv_json_check, SMP_T_STR, SMP_T_STR },
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002643 { "bytes", sample_conv_bytes, ARG2(1,SINT,SINT), NULL, SMP_T_BIN, SMP_T_BIN },
2644 { "field", sample_conv_field, ARG2(2,SINT,STR), sample_conv_field_check, SMP_T_STR, SMP_T_STR },
2645 { "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 +01002646 { "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 +01002647
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002648 { "and", sample_conv_binary_and, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
2649 { "or", sample_conv_binary_or, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
2650 { "xor", sample_conv_binary_xor, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
2651 { "cpl", sample_conv_binary_cpl, 0, NULL, SMP_T_SINT, SMP_T_SINT },
2652 { "bool", sample_conv_arith_bool, 0, NULL, SMP_T_SINT, SMP_T_BOOL },
2653 { "not", sample_conv_arith_not, 0, NULL, SMP_T_SINT, SMP_T_BOOL },
2654 { "odd", sample_conv_arith_odd, 0, NULL, SMP_T_SINT, SMP_T_BOOL },
2655 { "even", sample_conv_arith_even, 0, NULL, SMP_T_SINT, SMP_T_BOOL },
2656 { "add", sample_conv_arith_add, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
2657 { "sub", sample_conv_arith_sub, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
2658 { "mul", sample_conv_arith_mul, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
2659 { "div", sample_conv_arith_div, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
2660 { "mod", sample_conv_arith_mod, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
2661 { "neg", sample_conv_arith_neg, 0, NULL, SMP_T_SINT, SMP_T_SINT },
Willy Tarreau97707872015-01-27 15:12:13 +01002662
Willy Tarreau9fcb9842012-04-20 14:45:49 +02002663 { NULL, NULL, 0, 0, 0 },
Emeric Brun107ca302010-01-04 16:16:05 +01002664}};
2665
2666__attribute__((constructor))
Willy Tarreau12785782012-04-27 21:37:17 +02002667static void __sample_init(void)
Emeric Brun107ca302010-01-04 16:16:05 +01002668{
Willy Tarreau5b8ad222013-07-25 12:17:57 +02002669 /* register sample fetch and format conversion keywords */
2670 sample_register_fetches(&smp_kws);
Willy Tarreau12785782012-04-27 21:37:17 +02002671 sample_register_convs(&sample_conv_kws);
Emeric Brun107ca302010-01-04 16:16:05 +01002672}