blob: 134ff76f5f9ffddccb373617b43935045f0041da [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 Tarreau35b51c62018-09-10 15:38:55 +020023#include <common/http.h>
Willy Tarreau0108d902018-11-25 19:14:37 +010024#include <common/initcall.h>
Willy Tarreaucd3b0942012-04-27 21:52:18 +020025#include <common/standard.h>
Willy Tarreaua4312fa2013-04-02 16:34:32 +020026#include <common/uri_auth.h>
Emeric Brun53d1a982014-04-30 18:21:37 +020027#include <common/base64.h>
Willy Tarreaucd3b0942012-04-27 21:52:18 +020028
Willy Tarreau9fcb9842012-04-20 14:45:49 +020029#include <proto/arg.h>
Willy Tarreaua4312fa2013-04-02 16:34:32 +020030#include <proto/auth.h>
31#include <proto/log.h>
32#include <proto/proxy.h>
Willy Tarreaucd3b0942012-04-27 21:52:18 +020033#include <proto/sample.h>
Willy Tarreaua4312fa2013-04-02 16:34:32 +020034#include <proto/stick_table.h>
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +020035#include <proto/vars.h>
Emeric Brun107ca302010-01-04 16:16:05 +010036
Dragan Dosen6e5a9ca2017-10-24 09:18:23 +020037#include <import/sha1.h>
Thierry FOURNIER01e09742016-12-26 11:46:11 +010038#include <import/xxhash.h>
39
Willy Tarreau1cf8f082014-02-07 12:14:54 +010040/* sample type names */
41const char *smp_to_type[SMP_TYPES] = {
Thierry FOURNIER9c627e82015-06-03 20:12:35 +020042 [SMP_T_ANY] = "any",
Willy Tarreau1cf8f082014-02-07 12:14:54 +010043 [SMP_T_BOOL] = "bool",
Willy Tarreau1cf8f082014-02-07 12:14:54 +010044 [SMP_T_SINT] = "sint",
45 [SMP_T_ADDR] = "addr",
46 [SMP_T_IPV4] = "ipv4",
47 [SMP_T_IPV6] = "ipv6",
48 [SMP_T_STR] = "str",
49 [SMP_T_BIN] = "bin",
Thierry FOURNIER4c2479e2015-06-03 20:12:04 +020050 [SMP_T_METH] = "meth",
Willy Tarreau1cf8f082014-02-07 12:14:54 +010051};
52
Willy Tarreau12785782012-04-27 21:37:17 +020053/* static sample used in sample_process() when <p> is NULL */
Emeric Brune5c918b2017-06-14 14:15:36 +020054static THREAD_LOCAL struct sample temp_smp;
Emeric Brun107ca302010-01-04 16:16:05 +010055
Willy Tarreau12785782012-04-27 21:37:17 +020056/* list head of all known sample fetch keywords */
57static struct sample_fetch_kw_list sample_fetches = {
58 .list = LIST_HEAD_INIT(sample_fetches.list)
Emeric Brun107ca302010-01-04 16:16:05 +010059};
60
Willy Tarreau12785782012-04-27 21:37:17 +020061/* list head of all known sample format conversion keywords */
62static struct sample_conv_kw_list sample_convs = {
63 .list = LIST_HEAD_INIT(sample_convs.list)
Emeric Brun107ca302010-01-04 16:16:05 +010064};
65
Willy Tarreau80aca902013-01-07 15:42:20 +010066const unsigned int fetch_cap[SMP_SRC_ENTRIES] = {
67 [SMP_SRC_INTRN] = (SMP_VAL_FE_CON_ACC | SMP_VAL_FE_SES_ACC | SMP_VAL_FE_REQ_CNT |
68 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
69 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
70 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
71 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
72 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
73 SMP_VAL_FE_LOG_END),
74
75 [SMP_SRC_LISTN] = (SMP_VAL_FE_CON_ACC | SMP_VAL_FE_SES_ACC | SMP_VAL_FE_REQ_CNT |
76 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
77 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
78 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
79 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
80 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
81 SMP_VAL_FE_LOG_END),
82
83 [SMP_SRC_FTEND] = (SMP_VAL_FE_CON_ACC | SMP_VAL_FE_SES_ACC | SMP_VAL_FE_REQ_CNT |
84 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
85 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
86 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
87 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
88 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
89 SMP_VAL_FE_LOG_END),
90
91 [SMP_SRC_L4CLI] = (SMP_VAL_FE_CON_ACC | SMP_VAL_FE_SES_ACC | SMP_VAL_FE_REQ_CNT |
92 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
93 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
94 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
95 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
96 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
97 SMP_VAL_FE_LOG_END),
98
99 [SMP_SRC_L5CLI] = (SMP_VAL___________ | SMP_VAL_FE_SES_ACC | SMP_VAL_FE_REQ_CNT |
100 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
101 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
102 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
103 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
104 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
105 SMP_VAL_FE_LOG_END),
106
107 [SMP_SRC_TRACK] = (SMP_VAL_FE_CON_ACC | SMP_VAL_FE_SES_ACC | SMP_VAL_FE_REQ_CNT |
108 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
109 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
110 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
111 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
112 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
113 SMP_VAL_FE_LOG_END),
114
115 [SMP_SRC_L6REQ] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_FE_REQ_CNT |
116 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
117 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
118 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL___________ |
119 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
120 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
121 SMP_VAL___________),
122
123 [SMP_SRC_HRQHV] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_FE_REQ_CNT |
124 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
125 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
126 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL___________ |
127 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
128 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
129 SMP_VAL___________),
130
131 [SMP_SRC_HRQHP] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_FE_REQ_CNT |
132 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
133 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
134 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
135 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
136 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
137 SMP_VAL_FE_LOG_END),
138
139 [SMP_SRC_HRQBO] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
140 SMP_VAL___________ | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
141 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
142 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL___________ |
143 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
144 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
145 SMP_VAL___________),
146
147 [SMP_SRC_BKEND] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
148 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
149 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
150 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
151 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
152 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
153 SMP_VAL_FE_LOG_END),
154
155 [SMP_SRC_SERVR] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
156 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
157 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
158 SMP_VAL___________ | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
159 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
160 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
161 SMP_VAL_FE_LOG_END),
162
163 [SMP_SRC_L4SRV] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
164 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
165 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
166 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_BE_RES_CNT |
167 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
168 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
169 SMP_VAL_FE_LOG_END),
170
171 [SMP_SRC_L5SRV] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
172 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
173 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
174 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_BE_RES_CNT |
175 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
176 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
177 SMP_VAL_FE_LOG_END),
178
179 [SMP_SRC_L6RES] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
180 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
181 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
182 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_BE_RES_CNT |
183 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
184 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
185 SMP_VAL___________),
186
187 [SMP_SRC_HRSHV] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
188 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
189 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
190 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_BE_RES_CNT |
191 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
192 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
193 SMP_VAL___________),
194
195 [SMP_SRC_HRSHP] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
196 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
197 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
198 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_BE_RES_CNT |
199 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
200 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
201 SMP_VAL_FE_LOG_END),
202
203 [SMP_SRC_HRSBO] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
204 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
205 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
206 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
207 SMP_VAL___________ | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
208 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
209 SMP_VAL___________),
210
211 [SMP_SRC_RQFIN] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
212 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
213 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
214 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
215 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
216 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
217 SMP_VAL_FE_LOG_END),
218
219 [SMP_SRC_RSFIN] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
220 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
221 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
222 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
223 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
224 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
225 SMP_VAL_FE_LOG_END),
226
227 [SMP_SRC_TXFIN] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
228 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
229 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
230 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
231 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
232 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
233 SMP_VAL_FE_LOG_END),
234
235 [SMP_SRC_SSFIN] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
236 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
237 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
238 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
239 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
240 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
241 SMP_VAL_FE_LOG_END),
242};
243
244static const char *fetch_src_names[SMP_SRC_ENTRIES] = {
245 [SMP_SRC_INTRN] = "internal state",
246 [SMP_SRC_LISTN] = "listener",
247 [SMP_SRC_FTEND] = "frontend",
248 [SMP_SRC_L4CLI] = "client address",
249 [SMP_SRC_L5CLI] = "client-side connection",
250 [SMP_SRC_TRACK] = "track counters",
251 [SMP_SRC_L6REQ] = "request buffer",
252 [SMP_SRC_HRQHV] = "HTTP request headers",
253 [SMP_SRC_HRQHP] = "HTTP request",
254 [SMP_SRC_HRQBO] = "HTTP request body",
255 [SMP_SRC_BKEND] = "backend",
256 [SMP_SRC_SERVR] = "server",
257 [SMP_SRC_L4SRV] = "server address",
258 [SMP_SRC_L5SRV] = "server-side connection",
259 [SMP_SRC_L6RES] = "response buffer",
260 [SMP_SRC_HRSHV] = "HTTP response headers",
261 [SMP_SRC_HRSHP] = "HTTP response",
262 [SMP_SRC_HRSBO] = "HTTP response body",
263 [SMP_SRC_RQFIN] = "request buffer statistics",
264 [SMP_SRC_RSFIN] = "response buffer statistics",
265 [SMP_SRC_TXFIN] = "transaction statistics",
266 [SMP_SRC_SSFIN] = "session statistics",
267};
268
Willy Tarreaubf8e2512013-03-25 14:52:41 +0100269static const char *fetch_ckp_names[SMP_CKP_ENTRIES] = {
270 [SMP_CKP_FE_CON_ACC] = "frontend tcp-request connection rule",
271 [SMP_CKP_FE_SES_ACC] = "frontend tcp-request session rule",
272 [SMP_CKP_FE_REQ_CNT] = "frontend tcp-request content rule",
273 [SMP_CKP_FE_HRQ_HDR] = "frontend http-request header rule",
274 [SMP_CKP_FE_HRQ_BDY] = "frontend http-request body rule",
275 [SMP_CKP_FE_SET_BCK] = "frontend use-backend rule",
276 [SMP_CKP_BE_REQ_CNT] = "backend tcp-request content rule",
277 [SMP_CKP_BE_HRQ_HDR] = "backend http-request header rule",
278 [SMP_CKP_BE_HRQ_BDY] = "backend http-request body rule",
279 [SMP_CKP_BE_SET_SRV] = "backend use-server, balance or stick-match rule",
280 [SMP_CKP_BE_SRV_CON] = "server source selection",
281 [SMP_CKP_BE_RES_CNT] = "backend tcp-response content rule",
282 [SMP_CKP_BE_HRS_HDR] = "backend http-response header rule",
283 [SMP_CKP_BE_HRS_BDY] = "backend http-response body rule",
284 [SMP_CKP_BE_STO_RUL] = "backend stick-store rule",
285 [SMP_CKP_FE_RES_CNT] = "frontend tcp-response content rule",
286 [SMP_CKP_FE_HRS_HDR] = "frontend http-response header rule",
287 [SMP_CKP_FE_HRS_BDY] = "frontend http-response body rule",
288 [SMP_CKP_FE_LOG_END] = "logs",
289};
290
Thierry FOURNIER348971e2013-11-21 10:50:10 +0100291/* This function returns the type of the data returned by the sample_expr.
292 * It assumes that the <expr> and all of its converters are properly
293 * initialized.
294 */
295inline
296int smp_expr_output_type(struct sample_expr *expr)
297{
298 struct sample_conv_expr *smp_expr;
299
300 if (!LIST_ISEMPTY(&expr->conv_exprs)) {
301 smp_expr = LIST_PREV(&expr->conv_exprs, struct sample_conv_expr *, list);
302 return smp_expr->conv->out_type;
303 }
304 return expr->fetch->out_type;
305}
306
307
Willy Tarreau80aca902013-01-07 15:42:20 +0100308/* fill the trash with a comma-delimited list of source names for the <use> bit
309 * field which must be composed of a non-null set of SMP_USE_* flags. The return
310 * value is the pointer to the string in the trash buffer.
311 */
312const char *sample_src_names(unsigned int use)
313{
314 int bit;
315
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200316 trash.data = 0;
317 trash.area[0] = '\0';
Willy Tarreau80aca902013-01-07 15:42:20 +0100318 for (bit = 0; bit < SMP_SRC_ENTRIES; bit++) {
319 if (!(use & ~((1 << bit) - 1)))
320 break; /* no more bits */
321
322 if (!(use & (1 << bit)))
323 continue; /* bit not set */
324
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200325 trash.data += snprintf(trash.area + trash.data,
326 trash.size - trash.data, "%s%s",
327 (use & ((1 << bit) - 1)) ? "," : "",
328 fetch_src_names[bit]);
Willy Tarreau80aca902013-01-07 15:42:20 +0100329 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200330 return trash.area;
Willy Tarreau80aca902013-01-07 15:42:20 +0100331}
332
Willy Tarreaubf8e2512013-03-25 14:52:41 +0100333/* return a pointer to the correct sample checkpoint name, or "unknown" when
334 * the flags are invalid. Only the lowest bit is used, higher bits are ignored
335 * if set.
336 */
337const char *sample_ckp_names(unsigned int use)
338{
339 int bit;
340
341 for (bit = 0; bit < SMP_CKP_ENTRIES; bit++)
342 if (use & (1 << bit))
343 return fetch_ckp_names[bit];
344 return "unknown sample check place, please report this bug";
345}
346
Emeric Brun107ca302010-01-04 16:16:05 +0100347/*
Willy Tarreau80aca902013-01-07 15:42:20 +0100348 * Registers the sample fetch keyword list <kwl> as a list of valid keywords
349 * for next parsing sessions. The fetch keywords capabilities are also computed
350 * from their ->use field.
Emeric Brun107ca302010-01-04 16:16:05 +0100351 */
Willy Tarreau80aca902013-01-07 15:42:20 +0100352void sample_register_fetches(struct sample_fetch_kw_list *kwl)
Emeric Brun107ca302010-01-04 16:16:05 +0100353{
Willy Tarreau80aca902013-01-07 15:42:20 +0100354 struct sample_fetch *sf;
355 int bit;
356
357 for (sf = kwl->kw; sf->kw != NULL; sf++) {
358 for (bit = 0; bit < SMP_SRC_ENTRIES; bit++)
359 if (sf->use & (1 << bit))
360 sf->val |= fetch_cap[bit];
361 }
362 LIST_ADDQ(&sample_fetches.list, &kwl->list);
Emeric Brun107ca302010-01-04 16:16:05 +0100363}
364
365/*
Willy Tarreau12785782012-04-27 21:37:17 +0200366 * Registers the sample format coverstion keyword list <pckl> as a list of valid keywords for next
Emeric Brun107ca302010-01-04 16:16:05 +0100367 * parsing sessions.
368 */
Willy Tarreau12785782012-04-27 21:37:17 +0200369void sample_register_convs(struct sample_conv_kw_list *pckl)
Emeric Brun107ca302010-01-04 16:16:05 +0100370{
Willy Tarreau12785782012-04-27 21:37:17 +0200371 LIST_ADDQ(&sample_convs.list, &pckl->list);
Emeric Brun107ca302010-01-04 16:16:05 +0100372}
373
374/*
Willy Tarreau12785782012-04-27 21:37:17 +0200375 * Returns the pointer on sample fetch keyword structure identified by
Emeric Brun107ca302010-01-04 16:16:05 +0100376 * string of <len> in buffer <kw>.
377 *
378 */
Willy Tarreau12785782012-04-27 21:37:17 +0200379struct sample_fetch *find_sample_fetch(const char *kw, int len)
Emeric Brun107ca302010-01-04 16:16:05 +0100380{
381 int index;
Willy Tarreau12785782012-04-27 21:37:17 +0200382 struct sample_fetch_kw_list *kwl;
Emeric Brun107ca302010-01-04 16:16:05 +0100383
Willy Tarreau12785782012-04-27 21:37:17 +0200384 list_for_each_entry(kwl, &sample_fetches.list, list) {
Emeric Brun107ca302010-01-04 16:16:05 +0100385 for (index = 0; kwl->kw[index].kw != NULL; index++) {
386 if (strncmp(kwl->kw[index].kw, kw, len) == 0 &&
387 kwl->kw[index].kw[len] == '\0')
388 return &kwl->kw[index];
389 }
390 }
391 return NULL;
392}
393
Tim Duesterhusc555ee02018-01-25 16:24:44 +0100394/* This function browses the list of available sample fetches. <current> is
Thierry FOURNIER4d9a1d12014-12-08 14:49:19 +0100395 * the last used sample fetch. If it is the first call, it must set to NULL.
Tim Duesterhusc555ee02018-01-25 16:24:44 +0100396 * <idx> is the index of the next sample fetch entry. It is used as private
397 * value. It is useless to initiate it.
Thierry FOURNIER4d9a1d12014-12-08 14:49:19 +0100398 *
Tim Duesterhusc555ee02018-01-25 16:24:44 +0100399 * It returns always the new fetch_sample entry, and NULL when the end of
Thierry FOURNIER4d9a1d12014-12-08 14:49:19 +0100400 * the list is reached.
401 */
402struct sample_fetch *sample_fetch_getnext(struct sample_fetch *current, int *idx)
403{
404 struct sample_fetch_kw_list *kwl;
405 struct sample_fetch *base;
406
407 if (!current) {
408 /* Get first kwl entry. */
409 kwl = LIST_NEXT(&sample_fetches.list, struct sample_fetch_kw_list *, list);
410 (*idx) = 0;
411 } else {
412 /* Get kwl corresponding to the curret entry. */
413 base = current + 1 - (*idx);
414 kwl = container_of(base, struct sample_fetch_kw_list, kw);
415 }
416
417 while (1) {
418
419 /* Check if kwl is the last entry. */
420 if (&kwl->list == &sample_fetches.list)
421 return NULL;
422
423 /* idx contain the next keyword. If it is available, return it. */
424 if (kwl->kw[*idx].kw) {
425 (*idx)++;
426 return &kwl->kw[(*idx)-1];
427 }
428
429 /* get next entry in the main list, and return NULL if the end is reached. */
430 kwl = LIST_NEXT(&kwl->list, struct sample_fetch_kw_list *, list);
431
432 /* Set index to 0, ans do one other loop. */
433 (*idx) = 0;
434 }
435}
436
Thierry FOURNIER8fd13762015-03-10 23:56:48 +0100437/* This function browses the list of available converters. <current> is
438 * the last used converter. If it is the first call, it must set to NULL.
439 * <idx> is the index of the next converter entry. It is used as private
440 * value. It is useless to initiate it.
441 *
442 * It returns always the next sample_conv entry, and NULL when the end of
443 * the list is reached.
444 */
445struct sample_conv *sample_conv_getnext(struct sample_conv *current, int *idx)
446{
447 struct sample_conv_kw_list *kwl;
448 struct sample_conv *base;
449
450 if (!current) {
451 /* Get first kwl entry. */
452 kwl = LIST_NEXT(&sample_convs.list, struct sample_conv_kw_list *, list);
453 (*idx) = 0;
454 } else {
455 /* Get kwl corresponding to the curret entry. */
456 base = current + 1 - (*idx);
457 kwl = container_of(base, struct sample_conv_kw_list, kw);
458 }
459
460 while (1) {
461 /* Check if kwl is the last entry. */
462 if (&kwl->list == &sample_convs.list)
463 return NULL;
464
465 /* idx contain the next keyword. If it is available, return it. */
466 if (kwl->kw[*idx].kw) {
467 (*idx)++;
468 return &kwl->kw[(*idx)-1];
469 }
470
471 /* get next entry in the main list, and return NULL if the end is reached. */
472 kwl = LIST_NEXT(&kwl->list, struct sample_conv_kw_list *, list);
473
474 /* Set index to 0, ans do one other loop. */
475 (*idx) = 0;
476 }
477}
478
Emeric Brun107ca302010-01-04 16:16:05 +0100479/*
Willy Tarreau12785782012-04-27 21:37:17 +0200480 * Returns the pointer on sample format conversion keyword structure identified by
Emeric Brun107ca302010-01-04 16:16:05 +0100481 * string of <len> in buffer <kw>.
482 *
483 */
Willy Tarreau12785782012-04-27 21:37:17 +0200484struct sample_conv *find_sample_conv(const char *kw, int len)
Emeric Brun107ca302010-01-04 16:16:05 +0100485{
486 int index;
Willy Tarreau12785782012-04-27 21:37:17 +0200487 struct sample_conv_kw_list *kwl;
Emeric Brun107ca302010-01-04 16:16:05 +0100488
Willy Tarreau12785782012-04-27 21:37:17 +0200489 list_for_each_entry(kwl, &sample_convs.list, list) {
Emeric Brun107ca302010-01-04 16:16:05 +0100490 for (index = 0; kwl->kw[index].kw != NULL; index++) {
491 if (strncmp(kwl->kw[index].kw, kw, len) == 0 &&
492 kwl->kw[index].kw[len] == '\0')
493 return &kwl->kw[index];
494 }
495 }
496 return NULL;
497}
498
Emeric Brun107ca302010-01-04 16:16:05 +0100499/******************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +0200500/* Sample casts functions */
Emeric Brun107ca302010-01-04 16:16:05 +0100501/******************************************************************/
502
Willy Tarreau342acb42012-04-23 22:03:39 +0200503static int c_ip2int(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100504{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200505 smp->data.u.sint = ntohl(smp->data.u.ipv4.s_addr);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200506 smp->data.type = SMP_T_SINT;
Emeric Brun107ca302010-01-04 16:16:05 +0100507 return 1;
508}
509
Willy Tarreau342acb42012-04-23 22:03:39 +0200510static int c_ip2str(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100511{
Willy Tarreau83061a82018-07-13 11:56:34 +0200512 struct buffer *trash = get_trash_chunk();
Emeric Brun107ca302010-01-04 16:16:05 +0100513
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200514 if (!inet_ntop(AF_INET, (void *)&smp->data.u.ipv4, trash->area, trash->size))
Emeric Brun107ca302010-01-04 16:16:05 +0100515 return 0;
516
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200517 trash->data = strlen(trash->area);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200518 smp->data.u.str = *trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200519 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100520 smp->flags &= ~SMP_F_CONST;
Emeric Brun107ca302010-01-04 16:16:05 +0100521
522 return 1;
523}
524
Willy Tarreau342acb42012-04-23 22:03:39 +0200525static int c_ip2ipv6(struct sample *smp)
David du Colombier4f92d322011-03-24 11:09:31 +0100526{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200527 v4tov6(&smp->data.u.ipv6, &smp->data.u.ipv4);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200528 smp->data.type = SMP_T_IPV6;
David du Colombier4f92d322011-03-24 11:09:31 +0100529 return 1;
530}
531
Thierry FOURNIERcc4d1712015-07-24 09:04:56 +0200532static int c_ipv62ip(struct sample *smp)
533{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200534 if (!v6tov4(&smp->data.u.ipv4, &smp->data.u.ipv6))
Thierry FOURNIERcc4d1712015-07-24 09:04:56 +0200535 return 0;
Tim Duesterhusbf5ce022018-01-25 16:24:46 +0100536 smp->data.type = SMP_T_IPV4;
Thierry FOURNIERcc4d1712015-07-24 09:04:56 +0200537 return 1;
538}
539
Willy Tarreau342acb42012-04-23 22:03:39 +0200540static int c_ipv62str(struct sample *smp)
David du Colombier4f92d322011-03-24 11:09:31 +0100541{
Willy Tarreau83061a82018-07-13 11:56:34 +0200542 struct buffer *trash = get_trash_chunk();
David du Colombier4f92d322011-03-24 11:09:31 +0100543
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200544 if (!inet_ntop(AF_INET6, (void *)&smp->data.u.ipv6, trash->area, trash->size))
David du Colombier4f92d322011-03-24 11:09:31 +0100545 return 0;
546
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200547 trash->data = strlen(trash->area);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200548 smp->data.u.str = *trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200549 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100550 smp->flags &= ~SMP_F_CONST;
David du Colombier4f92d322011-03-24 11:09:31 +0100551 return 1;
552}
553
554/*
Willy Tarreau342acb42012-04-23 22:03:39 +0200555static int c_ipv62ip(struct sample *smp)
David du Colombier4f92d322011-03-24 11:09:31 +0100556{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200557 return v6tov4(&smp->data.u.ipv4, &smp->data.u.ipv6);
David du Colombier4f92d322011-03-24 11:09:31 +0100558}
559*/
560
Willy Tarreau342acb42012-04-23 22:03:39 +0200561static int c_int2ip(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100562{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200563 smp->data.u.ipv4.s_addr = htonl((unsigned int)smp->data.u.sint);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200564 smp->data.type = SMP_T_IPV4;
Emeric Brun107ca302010-01-04 16:16:05 +0100565 return 1;
566}
567
Thierry FOURNIERcc4d1712015-07-24 09:04:56 +0200568static int c_int2ipv6(struct sample *smp)
569{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200570 smp->data.u.ipv4.s_addr = htonl((unsigned int)smp->data.u.sint);
571 v4tov6(&smp->data.u.ipv6, &smp->data.u.ipv4);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200572 smp->data.type = SMP_T_IPV6;
Thierry FOURNIERcc4d1712015-07-24 09:04:56 +0200573 return 1;
574}
575
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100576static int c_str2addr(struct sample *smp)
577{
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200578 if (!buf2ip(smp->data.u.str.area, smp->data.u.str.data, &smp->data.u.ipv4)) {
579 if (!buf2ip6(smp->data.u.str.area, smp->data.u.str.data, &smp->data.u.ipv6))
Thierry FOURNIERfd139902013-12-11 12:38:57 +0100580 return 0;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200581 smp->data.type = SMP_T_IPV6;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100582 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIERfd139902013-12-11 12:38:57 +0100583 return 1;
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100584 }
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200585 smp->data.type = SMP_T_IPV4;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100586 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100587 return 1;
588}
589
Willy Tarreau342acb42012-04-23 22:03:39 +0200590static int c_str2ip(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100591{
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200592 if (!buf2ip(smp->data.u.str.area, smp->data.u.str.data, &smp->data.u.ipv4))
Emeric Brun107ca302010-01-04 16:16:05 +0100593 return 0;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200594 smp->data.type = SMP_T_IPV4;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100595 smp->flags &= ~SMP_F_CONST;
Emeric Brun107ca302010-01-04 16:16:05 +0100596 return 1;
597}
598
Willy Tarreau342acb42012-04-23 22:03:39 +0200599static int c_str2ipv6(struct sample *smp)
David du Colombier4f92d322011-03-24 11:09:31 +0100600{
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200601 if (!buf2ip6(smp->data.u.str.area, smp->data.u.str.data, &smp->data.u.ipv6))
Thierry FOURNIERfd139902013-12-11 12:38:57 +0100602 return 0;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200603 smp->data.type = SMP_T_IPV6;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100604 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIERfd139902013-12-11 12:38:57 +0100605 return 1;
David du Colombier4f92d322011-03-24 11:09:31 +0100606}
607
Emeric Brun4b9e8022014-11-03 18:17:10 +0100608/*
609 * The NULL char always enforces the end of string if it is met.
610 * Data is never changed, so we can ignore the CONST case
Thierry FOURNIERe87cac12014-03-12 15:07:59 +0100611 */
Emeric Brun8ac33d92012-10-17 13:36:06 +0200612static int c_bin2str(struct sample *smp)
613{
Emeric Brun4b9e8022014-11-03 18:17:10 +0100614 int i;
Emeric Brun8ac33d92012-10-17 13:36:06 +0200615
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200616 for (i = 0; i < smp->data.u.str.data; i++) {
617 if (!smp->data.u.str.area[i]) {
618 smp->data.u.str.data = i;
Thierry FOURNIERe87cac12014-03-12 15:07:59 +0100619 break;
Emeric Brun4b9e8022014-11-03 18:17:10 +0100620 }
Emeric Brun8ac33d92012-10-17 13:36:06 +0200621 }
Emeric Brun8ac33d92012-10-17 13:36:06 +0200622 return 1;
623}
624
Willy Tarreau342acb42012-04-23 22:03:39 +0200625static int c_int2str(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100626{
Willy Tarreau83061a82018-07-13 11:56:34 +0200627 struct buffer *trash = get_trash_chunk();
Emeric Brun107ca302010-01-04 16:16:05 +0100628 char *pos;
629
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200630 pos = lltoa_r(smp->data.u.sint, trash->area, trash->size);
Emeric Brun107ca302010-01-04 16:16:05 +0100631 if (!pos)
632 return 0;
633
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200634 trash->size = trash->size - (pos - trash->area);
635 trash->area = pos;
636 trash->data = strlen(pos);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200637 smp->data.u.str = *trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200638 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100639 smp->flags &= ~SMP_F_CONST;
Emeric Brun107ca302010-01-04 16:16:05 +0100640 return 1;
641}
642
Joseph Herlant757f5ad2018-11-15 12:14:56 -0800643/* This function unconditionally duplicates data and removes the "const" flag.
Willy Tarreauad635822016-08-08 19:21:09 +0200644 * For strings and binary blocks, it also provides a known allocated size with
645 * a length that is capped to the size, and ensures a trailing zero is always
646 * appended for strings. This is necessary for some operations which may
647 * require to extend the length. It returns 0 if it fails, 1 on success.
648 */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100649int smp_dup(struct sample *smp)
Emeric Brun485479d2010-09-23 18:02:19 +0200650{
Willy Tarreau83061a82018-07-13 11:56:34 +0200651 struct buffer *trash;
Emeric Brun485479d2010-09-23 18:02:19 +0200652
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200653 switch (smp->data.type) {
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100654 case SMP_T_BOOL:
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100655 case SMP_T_SINT:
656 case SMP_T_ADDR:
657 case SMP_T_IPV4:
658 case SMP_T_IPV6:
659 /* These type are not const. */
660 break;
Willy Tarreauad635822016-08-08 19:21:09 +0200661
Christopher Fauletec100512017-07-24 15:38:41 +0200662 case SMP_T_METH:
663 if (smp->data.u.meth.meth != HTTP_METH_OTHER)
664 break;
665 /* Fall through */
666
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100667 case SMP_T_STR:
Willy Tarreauad635822016-08-08 19:21:09 +0200668 trash = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200669 trash->data = smp->data.u.str.data;
670 if (trash->data > trash->size - 1)
671 trash->data = trash->size - 1;
Willy Tarreauad635822016-08-08 19:21:09 +0200672
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200673 memcpy(trash->area, smp->data.u.str.area, trash->data);
674 trash->area[trash->data] = 0;
Willy Tarreauad635822016-08-08 19:21:09 +0200675 smp->data.u.str = *trash;
676 break;
677
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100678 case SMP_T_BIN:
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100679 trash = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200680 trash->data = smp->data.u.str.data;
681 if (trash->data > trash->size)
682 trash->data = trash->size;
Willy Tarreauad635822016-08-08 19:21:09 +0200683
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200684 memcpy(trash->area, smp->data.u.str.area, trash->data);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200685 smp->data.u.str = *trash;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100686 break;
Christopher Fauletec100512017-07-24 15:38:41 +0200687
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100688 default:
689 /* Other cases are unexpected. */
690 return 0;
691 }
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100692
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100693 /* remove const flag */
694 smp->flags &= ~SMP_F_CONST;
Emeric Brun485479d2010-09-23 18:02:19 +0200695 return 1;
696}
697
Thierry FOURNIER0e9af552013-12-14 14:55:04 +0100698int c_none(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100699{
700 return 1;
701}
702
Willy Tarreau342acb42012-04-23 22:03:39 +0200703static int c_str2int(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100704{
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +0200705 const char *str;
706 const char *end;
Emeric Brun107ca302010-01-04 16:16:05 +0100707
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200708 if (smp->data.u.str.data == 0)
Thierry FOURNIER60bb0202014-01-27 18:20:48 +0100709 return 0;
710
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200711 str = smp->data.u.str.area;
712 end = smp->data.u.str.area + smp->data.u.str.data;
Emeric Brun107ca302010-01-04 16:16:05 +0100713
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200714 smp->data.u.sint = read_int64(&str, end);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200715 smp->data.type = SMP_T_SINT;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100716 smp->flags &= ~SMP_F_CONST;
Emeric Brun107ca302010-01-04 16:16:05 +0100717 return 1;
718}
719
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100720static int c_str2meth(struct sample *smp)
721{
722 enum http_meth_t meth;
723 int len;
724
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200725 meth = find_http_meth(smp->data.u.str.area, smp->data.u.str.data);
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100726 if (meth == HTTP_METH_OTHER) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200727 len = smp->data.u.str.data;
728 smp->data.u.meth.str.area = smp->data.u.str.area;
729 smp->data.u.meth.str.data = len;
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100730 }
731 else
732 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200733 smp->data.u.meth.meth = meth;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200734 smp->data.type = SMP_T_METH;
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100735 return 1;
736}
737
738static int c_meth2str(struct sample *smp)
739{
740 int len;
741 enum http_meth_t meth;
742
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200743 if (smp->data.u.meth.meth == HTTP_METH_OTHER) {
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100744 /* The method is unknown. Copy the original pointer. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200745 len = smp->data.u.meth.str.data;
746 smp->data.u.str.area = smp->data.u.meth.str.area;
747 smp->data.u.str.data = len;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200748 smp->data.type = SMP_T_STR;
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100749 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200750 else if (smp->data.u.meth.meth < HTTP_METH_OTHER) {
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100751 /* The method is known, copy the pointer containing the string. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200752 meth = smp->data.u.meth.meth;
Willy Tarreau35b51c62018-09-10 15:38:55 +0200753 smp->data.u.str.area = http_known_methods[meth].ptr;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200754 smp->data.u.str.data = http_known_methods[meth].len;
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100755 smp->flags |= SMP_F_CONST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200756 smp->data.type = SMP_T_STR;
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100757 }
758 else {
759 /* Unknown method */
760 return 0;
761 }
762 return 1;
763}
764
Willy Tarreau9700e5c2014-07-15 21:03:26 +0200765static int c_addr2bin(struct sample *smp)
766{
Willy Tarreau83061a82018-07-13 11:56:34 +0200767 struct buffer *chk = get_trash_chunk();
Willy Tarreau9700e5c2014-07-15 21:03:26 +0200768
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200769 if (smp->data.type == SMP_T_IPV4) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200770 chk->data = 4;
771 memcpy(chk->area, &smp->data.u.ipv4, chk->data);
Willy Tarreau9700e5c2014-07-15 21:03:26 +0200772 }
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200773 else if (smp->data.type == SMP_T_IPV6) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200774 chk->data = 16;
775 memcpy(chk->area, &smp->data.u.ipv6, chk->data);
Willy Tarreau9700e5c2014-07-15 21:03:26 +0200776 }
777 else
778 return 0;
779
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200780 smp->data.u.str = *chk;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200781 smp->data.type = SMP_T_BIN;
Willy Tarreau9700e5c2014-07-15 21:03:26 +0200782 return 1;
783}
784
Willy Tarreaubbfd1a22014-07-15 21:19:08 +0200785static int c_int2bin(struct sample *smp)
786{
Willy Tarreau83061a82018-07-13 11:56:34 +0200787 struct buffer *chk = get_trash_chunk();
Willy Tarreaubbfd1a22014-07-15 21:19:08 +0200788
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200789 *(unsigned long long int *) chk->area = my_htonll(smp->data.u.sint);
790 chk->data = 8;
Willy Tarreaubbfd1a22014-07-15 21:19:08 +0200791
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200792 smp->data.u.str = *chk;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200793 smp->data.type = SMP_T_BIN;
Willy Tarreaubbfd1a22014-07-15 21:19:08 +0200794 return 1;
795}
796
Willy Tarreau9700e5c2014-07-15 21:03:26 +0200797
Emeric Brun107ca302010-01-04 16:16:05 +0100798/*****************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +0200799/* Sample casts matrix: */
800/* sample_casts[from type][to type] */
801/* NULL pointer used for impossible sample casts */
Emeric Brun107ca302010-01-04 16:16:05 +0100802/*****************************************************************/
Emeric Brun107ca302010-01-04 16:16:05 +0100803
Thierry FOURNIER8af6ff12013-11-21 10:53:12 +0100804sample_cast_fct sample_casts[SMP_TYPES][SMP_TYPES] = {
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +0200805/* to: ANY BOOL SINT ADDR IPV4 IPV6 STR BIN METH */
806/* from: ANY */ { c_none, c_none, c_none, c_none, c_none, c_none, c_none, c_none, c_none, },
807/* BOOL */ { c_none, c_none, c_none, NULL, NULL, NULL, c_int2str, NULL, NULL, },
Thierry FOURNIERcc4d1712015-07-24 09:04:56 +0200808/* 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 +0200809/* ADDR */ { c_none, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, },
810/* IPV4 */ { c_none, NULL, c_ip2int, c_none, c_none, c_ip2ipv6, c_ip2str, c_addr2bin, NULL, },
Thierry FOURNIERcc4d1712015-07-24 09:04:56 +0200811/* IPV6 */ { c_none, NULL, NULL, c_none, c_ipv62ip,c_none, c_ipv62str, c_addr2bin, NULL, },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +0200812/* STR */ { c_none, c_str2int, c_str2int, c_str2addr, c_str2ip, c_str2ipv6, c_none, c_none, c_str2meth, },
813/* BIN */ { c_none, NULL, NULL, NULL, NULL, NULL, c_bin2str, c_none, c_str2meth, },
814/* METH */ { c_none, NULL, NULL, NULL, NULL, NULL, c_meth2str, c_meth2str, c_none, }
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200815};
Emeric Brun107ca302010-01-04 16:16:05 +0100816
Emeric Brun107ca302010-01-04 16:16:05 +0100817/*
Willy Tarreau12785782012-04-27 21:37:17 +0200818 * Parse a sample expression configuration:
Emeric Brun107ca302010-01-04 16:16:05 +0100819 * fetch keyword followed by format conversion keywords.
Willy Tarreau12785782012-04-27 21:37:17 +0200820 * Returns a pointer on allocated sample expression structure.
Willy Tarreaua4312fa2013-04-02 16:34:32 +0200821 * The caller must have set al->ctx.
Emeric Brun107ca302010-01-04 16:16:05 +0100822 */
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +0100823struct 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 +0100824{
Willy Tarreau833cc792013-07-24 15:34:19 +0200825 const char *begw; /* beginning of word */
826 const char *endw; /* end of word */
827 const char *endt; /* end of term */
Willy Tarreau12785782012-04-27 21:37:17 +0200828 struct sample_expr *expr;
829 struct sample_fetch *fetch;
830 struct sample_conv *conv;
Emeric Brun107ca302010-01-04 16:16:05 +0100831 unsigned long prev_type;
Willy Tarreau833cc792013-07-24 15:34:19 +0200832 char *fkw = NULL;
833 char *ckw = NULL;
Willy Tarreau689a1df2013-12-13 00:40:11 +0100834 int err_arg;
Emeric Brun107ca302010-01-04 16:16:05 +0100835
Willy Tarreau833cc792013-07-24 15:34:19 +0200836 begw = str[*idx];
837 for (endw = begw; *endw && *endw != '(' && *endw != ','; endw++);
838
839 if (endw == begw) {
Willy Tarreau975c1782013-12-12 23:16:54 +0100840 memprintf(err_msg, "missing fetch method");
Emeric Brun107ca302010-01-04 16:16:05 +0100841 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200842 }
Emeric Brun107ca302010-01-04 16:16:05 +0100843
Willy Tarreau833cc792013-07-24 15:34:19 +0200844 /* keep a copy of the current fetch keyword for error reporting */
845 fkw = my_strndup(begw, endw - begw);
Emeric Brun107ca302010-01-04 16:16:05 +0100846
Willy Tarreau833cc792013-07-24 15:34:19 +0200847 fetch = find_sample_fetch(begw, endw - begw);
848 if (!fetch) {
Willy Tarreau975c1782013-12-12 23:16:54 +0100849 memprintf(err_msg, "unknown fetch method '%s'", fkw);
Emeric Brun107ca302010-01-04 16:16:05 +0100850 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200851 }
Emeric Brun107ca302010-01-04 16:16:05 +0100852
Willy Tarreau833cc792013-07-24 15:34:19 +0200853 endt = endw;
854 if (*endt == '(') {
Willy Tarreau689a1df2013-12-13 00:40:11 +0100855 /* look for the end of this term and skip the opening parenthesis */
856 endt = ++endw;
Willy Tarreau833cc792013-07-24 15:34:19 +0200857 while (*endt && *endt != ')')
858 endt++;
859 if (*endt != ')') {
Willy Tarreau975c1782013-12-12 23:16:54 +0100860 memprintf(err_msg, "missing closing ')' after arguments to fetch keyword '%s'", fkw);
Willy Tarreau833cc792013-07-24 15:34:19 +0200861 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200862 }
Emeric Brun485479d2010-09-23 18:02:19 +0200863 }
Emeric Brun107ca302010-01-04 16:16:05 +0100864
Willy Tarreau833cc792013-07-24 15:34:19 +0200865 /* At this point, we have :
866 * - begw : beginning of the keyword
Willy Tarreau689a1df2013-12-13 00:40:11 +0100867 * - endw : end of the keyword, first character not part of keyword
868 * nor the opening parenthesis (so first character of args
869 * if present).
Willy Tarreau833cc792013-07-24 15:34:19 +0200870 * - endt : end of the term (=endw or last parenthesis if args are present)
871 */
872
873 if (fetch->out_type >= SMP_TYPES) {
Willy Tarreau975c1782013-12-12 23:16:54 +0100874 memprintf(err_msg, "returns type of fetch method '%s' is unknown", fkw);
Emeric Brun107ca302010-01-04 16:16:05 +0100875 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200876 }
Emeric Brun107ca302010-01-04 16:16:05 +0100877 prev_type = fetch->out_type;
Willy Tarreau833cc792013-07-24 15:34:19 +0200878
Vincent Bernat02779b62016-04-03 13:48:43 +0200879 expr = calloc(1, sizeof(*expr));
Emeric Brun485479d2010-09-23 18:02:19 +0200880 if (!expr)
881 goto out_error;
Emeric Brun107ca302010-01-04 16:16:05 +0100882
883 LIST_INIT(&(expr->conv_exprs));
884 expr->fetch = fetch;
Willy Tarreau2e845be2012-10-19 19:49:09 +0200885 expr->arg_p = empty_arg_list;
Emeric Brun107ca302010-01-04 16:16:05 +0100886
Willy Tarreau689a1df2013-12-13 00:40:11 +0100887 /* Note that we call the argument parser even with an empty string,
888 * this allows it to automatically create entries for mandatory
889 * implicit arguments (eg: local proxy name).
890 */
891 al->kw = expr->fetch->kw;
892 al->conv = NULL;
893 if (make_arg_list(endw, endt - endw, fetch->arg_mask, &expr->arg_p, err_msg, NULL, &err_arg, al) < 0) {
894 memprintf(err_msg, "fetch method '%s' : %s", fkw, *err_msg);
895 goto out_error;
896 }
Willy Tarreau2e845be2012-10-19 19:49:09 +0200897
Willy Tarreau689a1df2013-12-13 00:40:11 +0100898 if (!expr->arg_p) {
899 expr->arg_p = empty_arg_list;
Emeric Brun485479d2010-09-23 18:02:19 +0200900 }
Willy Tarreau689a1df2013-12-13 00:40:11 +0100901 else if (fetch->val_args && !fetch->val_args(expr->arg_p, err_msg)) {
902 memprintf(err_msg, "invalid args in fetch method '%s' : %s", fkw, *err_msg);
Emeric Brun485479d2010-09-23 18:02:19 +0200903 goto out_error;
Emeric Brun107ca302010-01-04 16:16:05 +0100904 }
905
Willy Tarreau833cc792013-07-24 15:34:19 +0200906 /* Now process the converters if any. We have two supported syntaxes
907 * for the converters, which can be combined :
908 * - comma-delimited list of converters just after the keyword and args ;
909 * - one converter per keyword
910 * The combination allows to have each keyword being a comma-delimited
911 * series of converters.
912 *
913 * We want to process the former first, then the latter. For this we start
914 * from the beginning of the supposed place in the exiting conv chain, which
915 * starts at the last comma (endt).
916 */
917
918 while (1) {
Willy Tarreau12785782012-04-27 21:37:17 +0200919 struct sample_conv_expr *conv_expr;
Emeric Brun107ca302010-01-04 16:16:05 +0100920
Willy Tarreau833cc792013-07-24 15:34:19 +0200921 if (*endt == ')') /* skip last closing parenthesis */
922 endt++;
Emeric Brun107ca302010-01-04 16:16:05 +0100923
Willy Tarreau833cc792013-07-24 15:34:19 +0200924 if (*endt && *endt != ',') {
925 if (ckw)
Willy Tarreau97108e02016-11-25 07:33:24 +0100926 memprintf(err_msg, "missing comma after converter '%s'", ckw);
Willy Tarreau833cc792013-07-24 15:34:19 +0200927 else
Willy Tarreau975c1782013-12-12 23:16:54 +0100928 memprintf(err_msg, "missing comma after fetch keyword '%s'", fkw);
Emeric Brun107ca302010-01-04 16:16:05 +0100929 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200930 }
Emeric Brun107ca302010-01-04 16:16:05 +0100931
Willy Tarreau833cc792013-07-24 15:34:19 +0200932 while (*endt == ',') /* then trailing commas */
933 endt++;
934
Willy Tarreau97108e02016-11-25 07:33:24 +0100935 begw = endt; /* start of converter */
Willy Tarreau833cc792013-07-24 15:34:19 +0200936
937 if (!*begw) {
938 /* none ? skip to next string */
939 (*idx)++;
940 begw = str[*idx];
941 if (!begw || !*begw)
942 break;
943 }
944
945 for (endw = begw; *endw && *endw != '(' && *endw != ','; endw++);
946
947 free(ckw);
948 ckw = my_strndup(begw, endw - begw);
949
950 conv = find_sample_conv(begw, endw - begw);
951 if (!conv) {
952 /* we found an isolated keyword that we don't know, it's not ours */
953 if (begw == str[*idx])
954 break;
Willy Tarreau97108e02016-11-25 07:33:24 +0100955 memprintf(err_msg, "unknown converter '%s'", ckw);
Willy Tarreau833cc792013-07-24 15:34:19 +0200956 goto out_error;
957 }
Emeric Brun107ca302010-01-04 16:16:05 +0100958
Willy Tarreau833cc792013-07-24 15:34:19 +0200959 endt = endw;
960 if (*endt == '(') {
961 /* look for the end of this term */
962 while (*endt && *endt != ')')
963 endt++;
964 if (*endt != ')') {
Willy Tarreau97108e02016-11-25 07:33:24 +0100965 memprintf(err_msg, "syntax error: missing ')' after converter '%s'", ckw);
Willy Tarreau833cc792013-07-24 15:34:19 +0200966 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200967 }
Willy Tarreau833cc792013-07-24 15:34:19 +0200968 }
969
970 if (conv->in_type >= SMP_TYPES || conv->out_type >= SMP_TYPES) {
Willy Tarreau97108e02016-11-25 07:33:24 +0100971 memprintf(err_msg, "returns type of converter '%s' is unknown", ckw);
Emeric Brun107ca302010-01-04 16:16:05 +0100972 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200973 }
Emeric Brun107ca302010-01-04 16:16:05 +0100974
975 /* If impossible type conversion */
Willy Tarreau12785782012-04-27 21:37:17 +0200976 if (!sample_casts[prev_type][conv->in_type]) {
Willy Tarreau97108e02016-11-25 07:33:24 +0100977 memprintf(err_msg, "converter '%s' cannot be applied", ckw);
Emeric Brun107ca302010-01-04 16:16:05 +0100978 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200979 }
Emeric Brun107ca302010-01-04 16:16:05 +0100980
981 prev_type = conv->out_type;
Vincent Bernat02779b62016-04-03 13:48:43 +0200982 conv_expr = calloc(1, sizeof(*conv_expr));
Emeric Brun485479d2010-09-23 18:02:19 +0200983 if (!conv_expr)
984 goto out_error;
Emeric Brun107ca302010-01-04 16:16:05 +0100985
986 LIST_ADDQ(&(expr->conv_exprs), &(conv_expr->list));
987 conv_expr->conv = conv;
988
Willy Tarreau833cc792013-07-24 15:34:19 +0200989 if (endt != endw) {
Willy Tarreaub27c0d32012-04-20 16:04:47 +0200990 int err_arg;
Willy Tarreau21d68a62012-04-20 15:52:36 +0200991
Willy Tarreau9fcb9842012-04-20 14:45:49 +0200992 if (!conv->arg_mask) {
Willy Tarreau97108e02016-11-25 07:33:24 +0100993 memprintf(err_msg, "converter '%s' does not support any args", ckw);
Emeric Brun485479d2010-09-23 18:02:19 +0200994 goto out_error;
995 }
Willy Tarreau9e92d322010-01-26 17:58:06 +0100996
Willy Tarreaua4312fa2013-04-02 16:34:32 +0200997 al->kw = expr->fetch->kw;
998 al->conv = conv_expr->conv->kw;
Willy Tarreau975c1782013-12-12 23:16:54 +0100999 if (make_arg_list(endw + 1, endt - endw - 1, conv->arg_mask, &conv_expr->arg_p, err_msg, NULL, &err_arg, al) < 0) {
Willy Tarreau97108e02016-11-25 07:33:24 +01001000 memprintf(err_msg, "invalid arg %d in converter '%s' : %s", err_arg+1, ckw, *err_msg);
Willy Tarreau21d68a62012-04-20 15:52:36 +02001001 goto out_error;
1002 }
1003
Willy Tarreau2e845be2012-10-19 19:49:09 +02001004 if (!conv_expr->arg_p)
1005 conv_expr->arg_p = empty_arg_list;
1006
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +01001007 if (conv->val_args && !conv->val_args(conv_expr->arg_p, conv, file, line, err_msg)) {
Willy Tarreau97108e02016-11-25 07:33:24 +01001008 memprintf(err_msg, "invalid args in converter '%s' : %s", ckw, *err_msg);
Emeric Brun485479d2010-09-23 18:02:19 +02001009 goto out_error;
1010 }
1011 }
Willy Tarreau833cc792013-07-24 15:34:19 +02001012 else if (ARGM(conv->arg_mask)) {
Willy Tarreau97108e02016-11-25 07:33:24 +01001013 memprintf(err_msg, "missing args for converter '%s'", ckw);
Emeric Brun485479d2010-09-23 18:02:19 +02001014 goto out_error;
Emeric Brun107ca302010-01-04 16:16:05 +01001015 }
1016 }
Emeric Brun485479d2010-09-23 18:02:19 +02001017
Willy Tarreau833cc792013-07-24 15:34:19 +02001018 out:
1019 free(fkw);
1020 free(ckw);
Emeric Brun107ca302010-01-04 16:16:05 +01001021 return expr;
1022
1023out_error:
Willy Tarreau12785782012-04-27 21:37:17 +02001024 /* TODO: prune_sample_expr(expr); */
Willy Tarreau833cc792013-07-24 15:34:19 +02001025 expr = NULL;
1026 goto out;
Emeric Brun107ca302010-01-04 16:16:05 +01001027}
1028
1029/*
Willy Tarreau12785782012-04-27 21:37:17 +02001030 * Process a fetch + format conversion of defined by the sample expression <expr>
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001031 * on request or response considering the <opt> parameter.
Willy Tarreau12785782012-04-27 21:37:17 +02001032 * Returns a pointer on a typed sample structure containing the result or NULL if
1033 * sample is not found or when format conversion failed.
Emeric Brun107ca302010-01-04 16:16:05 +01001034 * If <p> is not null, function returns results in structure pointed by <p>.
Willy Tarreau12785782012-04-27 21:37:17 +02001035 * If <p> is null, functions returns a pointer on a static sample structure.
Willy Tarreaub8c8f1f2012-04-23 22:38:26 +02001036 *
1037 * Note: the fetch functions are required to properly set the return type. The
1038 * conversion functions must do so too. However the cast functions do not need
1039 * to since they're made to cast mutiple types according to what is required.
Willy Tarreau6bcb0a82014-07-30 08:56:35 +02001040 *
1041 * The caller may indicate in <opt> if it considers the result final or not.
1042 * The caller needs to check the SMP_F_MAY_CHANGE flag in p->flags to verify
1043 * if the result is stable or not, according to the following table :
1044 *
1045 * return MAY_CHANGE FINAL Meaning for the sample
1046 * NULL 0 * Not present and will never be (eg: header)
1047 * NULL 1 0 Not present yet, could change (eg: POST param)
1048 * NULL 1 1 Not present yet, will not change anymore
1049 * smp 0 * Present and will not change (eg: header)
1050 * smp 1 0 Present, may change (eg: request length)
1051 * smp 1 1 Present, last known value (eg: request length)
Emeric Brun107ca302010-01-04 16:16:05 +01001052 */
Willy Tarreau192252e2015-04-04 01:47:55 +02001053struct sample *sample_process(struct proxy *px, struct session *sess,
1054 struct stream *strm, unsigned int opt,
Willy Tarreau12785782012-04-27 21:37:17 +02001055 struct sample_expr *expr, struct sample *p)
Emeric Brun107ca302010-01-04 16:16:05 +01001056{
Willy Tarreau12785782012-04-27 21:37:17 +02001057 struct sample_conv_expr *conv_expr;
Emeric Brun107ca302010-01-04 16:16:05 +01001058
Willy Tarreau18387e22013-07-25 12:02:38 +02001059 if (p == NULL) {
Willy Tarreaub4a88f02012-04-23 21:35:11 +02001060 p = &temp_smp;
Willy Tarreau6c616e02014-06-25 16:56:41 +02001061 memset(p, 0, sizeof(*p));
Willy Tarreau18387e22013-07-25 12:02:38 +02001062 }
Emeric Brun107ca302010-01-04 16:16:05 +01001063
Willy Tarreau1777ea62016-03-10 16:15:46 +01001064 smp_set_owner(p, px, sess, strm, opt);
Thierry FOURNIER0786d052015-05-11 15:42:45 +02001065 if (!expr->fetch->process(expr->arg_p, p, expr->fetch->kw, expr->fetch->private))
Emeric Brun107ca302010-01-04 16:16:05 +01001066 return NULL;
1067
Emeric Brun107ca302010-01-04 16:16:05 +01001068 list_for_each_entry(conv_expr, &expr->conv_exprs, list) {
Willy Tarreau12e50112012-04-25 17:21:49 +02001069 /* we want to ensure that p->type can be casted into
1070 * conv_expr->conv->in_type. We have 3 possibilities :
1071 * - NULL => not castable.
1072 * - c_none => nothing to do (let's optimize it)
1073 * - other => apply cast and prepare to fail
1074 */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001075 if (!sample_casts[p->data.type][conv_expr->conv->in_type])
Willy Tarreau12e50112012-04-25 17:21:49 +02001076 return NULL;
1077
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001078 if (sample_casts[p->data.type][conv_expr->conv->in_type] != c_none &&
1079 !sample_casts[p->data.type][conv_expr->conv->in_type](p))
Emeric Brun107ca302010-01-04 16:16:05 +01001080 return NULL;
1081
Willy Tarreau12e50112012-04-25 17:21:49 +02001082 /* OK cast succeeded */
1083
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001084 if (!conv_expr->conv->process(conv_expr->arg_p, p, conv_expr->conv->private))
Emeric Brun107ca302010-01-04 16:16:05 +01001085 return NULL;
Emeric Brun107ca302010-01-04 16:16:05 +01001086 }
1087 return p;
1088}
1089
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001090/*
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001091 * Resolve all remaining arguments in proxy <p>. Returns the number of
1092 * errors or 0 if everything is fine.
1093 */
1094int smp_resolve_args(struct proxy *p)
1095{
1096 struct arg_list *cur, *bak;
1097 const char *ctx, *where;
1098 const char *conv_ctx, *conv_pre, *conv_pos;
1099 struct userlist *ul;
Willy Tarreau46947782015-01-19 19:00:58 +01001100 struct my_regex *reg;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001101 struct arg *arg;
1102 int cfgerr = 0;
Willy Tarreau46947782015-01-19 19:00:58 +01001103 int rflags;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001104
1105 list_for_each_entry_safe(cur, bak, &p->conf.args.list, list) {
1106 struct proxy *px;
1107 struct server *srv;
1108 char *pname, *sname;
Willy Tarreau46947782015-01-19 19:00:58 +01001109 char *err;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001110
1111 arg = cur->arg;
1112
1113 /* prepare output messages */
1114 conv_pre = conv_pos = conv_ctx = "";
1115 if (cur->conv) {
1116 conv_ctx = cur->conv;
1117 conv_pre = "conversion keyword '";
1118 conv_pos = "' for ";
1119 }
1120
1121 where = "in";
1122 ctx = "sample fetch keyword";
1123 switch (cur->ctx) {
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001124 case ARGC_STK: where = "in stick rule in"; break;
1125 case ARGC_TRK: where = "in tracking rule in"; break;
1126 case ARGC_LOG: where = "in log-format string in"; break;
1127 case ARGC_LOGSD: where = "in log-format-sd string in"; break;
1128 case ARGC_HRQ: where = "in http-request header format string in"; break;
1129 case ARGC_HRS: where = "in http-response header format string in"; break;
1130 case ARGC_UIF: where = "in unique-id-format string in"; break;
1131 case ARGC_RDR: where = "in redirect format string in"; break;
1132 case ARGC_CAP: where = "in capture rule in"; break;
1133 case ARGC_ACL: ctx = "ACL keyword"; break;
1134 case ARGC_SRV: where = "in server directive in"; break;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001135 case ARGC_SPOE: where = "in spoe-message directive in"; break;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001136 }
1137
1138 /* set a few default settings */
1139 px = p;
1140 pname = p->id;
1141
1142 switch (arg->type) {
1143 case ARGT_SRV:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001144 if (!arg->data.str.data) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001145 ha_alert("parsing [%s:%d] : missing server name in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1146 cur->file, cur->line,
1147 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001148 cfgerr++;
1149 continue;
1150 }
1151
1152 /* we support two formats : "bck/srv" and "srv" */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001153 sname = strrchr(arg->data.str.area, '/');
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001154
1155 if (sname) {
1156 *sname++ = '\0';
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001157 pname = arg->data.str.area;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001158
Willy Tarreau9e0bb102015-05-26 11:24:42 +02001159 px = proxy_be_by_name(pname);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001160 if (!px) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001161 ha_alert("parsing [%s:%d] : unable to find proxy '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1162 cur->file, cur->line, pname,
1163 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001164 cfgerr++;
1165 break;
1166 }
1167 }
1168 else
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001169 sname = arg->data.str.area;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001170
1171 srv = findserver(px, sname);
1172 if (!srv) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001173 ha_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",
1174 cur->file, cur->line, sname, pname,
1175 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001176 cfgerr++;
1177 break;
1178 }
1179
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001180 free(arg->data.str.area);
1181 arg->data.str.area = NULL;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001182 arg->unresolved = 0;
1183 arg->data.srv = srv;
1184 break;
1185
1186 case ARGT_FE:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001187 if (arg->data.str.data) {
1188 pname = arg->data.str.area;
Willy Tarreau9e0bb102015-05-26 11:24:42 +02001189 px = proxy_fe_by_name(pname);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001190 }
1191
1192 if (!px) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001193 ha_alert("parsing [%s:%d] : unable to find frontend '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1194 cur->file, cur->line, pname,
1195 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001196 cfgerr++;
1197 break;
1198 }
1199
1200 if (!(px->cap & PR_CAP_FE)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001201 ha_alert("parsing [%s:%d] : proxy '%s', referenced in arg %d of %s%s%s%s '%s' %s proxy '%s', has not frontend capability.\n",
1202 cur->file, cur->line, pname,
1203 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001204 cfgerr++;
1205 break;
1206 }
1207
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001208 free(arg->data.str.area);
1209 arg->data.str.area = NULL;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001210 arg->unresolved = 0;
1211 arg->data.prx = px;
1212 break;
1213
1214 case ARGT_BE:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001215 if (arg->data.str.data) {
1216 pname = arg->data.str.area;
Willy Tarreau9e0bb102015-05-26 11:24:42 +02001217 px = proxy_be_by_name(pname);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001218 }
1219
1220 if (!px) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001221 ha_alert("parsing [%s:%d] : unable to find backend '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1222 cur->file, cur->line, pname,
1223 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001224 cfgerr++;
1225 break;
1226 }
1227
1228 if (!(px->cap & PR_CAP_BE)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001229 ha_alert("parsing [%s:%d] : proxy '%s', referenced in arg %d of %s%s%s%s '%s' %s proxy '%s', has not backend capability.\n",
1230 cur->file, cur->line, pname,
1231 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001232 cfgerr++;
1233 break;
1234 }
1235
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001236 free(arg->data.str.area);
1237 arg->data.str.area = NULL;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001238 arg->unresolved = 0;
1239 arg->data.prx = px;
1240 break;
1241
1242 case ARGT_TAB:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001243 if (arg->data.str.data) {
1244 pname = arg->data.str.area;
Willy Tarreaue2dc1fa2015-05-26 12:08:07 +02001245 px = proxy_tbl_by_name(pname);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001246 }
1247
1248 if (!px) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001249 ha_alert("parsing [%s:%d] : unable to find table '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1250 cur->file, cur->line, pname,
1251 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001252 cfgerr++;
1253 break;
1254 }
1255
1256 if (!px->table.size) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001257 ha_alert("parsing [%s:%d] : no table in proxy '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1258 cur->file, cur->line, pname,
1259 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001260 cfgerr++;
1261 break;
1262 }
1263
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001264 free(arg->data.str.area);
1265 arg->data.str.area = NULL;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001266 arg->unresolved = 0;
1267 arg->data.prx = px;
1268 break;
1269
1270 case ARGT_USR:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001271 if (!arg->data.str.data) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001272 ha_alert("parsing [%s:%d] : missing userlist name in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1273 cur->file, cur->line,
1274 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001275 cfgerr++;
1276 break;
1277 }
1278
1279 if (p->uri_auth && p->uri_auth->userlist &&
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001280 !strcmp(p->uri_auth->userlist->name, arg->data.str.area))
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001281 ul = p->uri_auth->userlist;
1282 else
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001283 ul = auth_find_userlist(arg->data.str.area);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001284
1285 if (!ul) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001286 ha_alert("parsing [%s:%d] : unable to find userlist '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001287 cur->file, cur->line,
1288 arg->data.str.area,
Christopher Faulet767a84b2017-11-24 16:50:31 +01001289 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001290 cfgerr++;
1291 break;
1292 }
1293
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001294 free(arg->data.str.area);
1295 arg->data.str.area = NULL;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001296 arg->unresolved = 0;
1297 arg->data.usr = ul;
1298 break;
Willy Tarreau46947782015-01-19 19:00:58 +01001299
1300 case ARGT_REG:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001301 if (!arg->data.str.data) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001302 ha_alert("parsing [%s:%d] : missing regex in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1303 cur->file, cur->line,
1304 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreau46947782015-01-19 19:00:58 +01001305 cfgerr++;
1306 continue;
1307 }
1308
1309 reg = calloc(1, sizeof(*reg));
1310 if (!reg) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001311 ha_alert("parsing [%s:%d] : not enough memory to build regex in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1312 cur->file, cur->line,
1313 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreau46947782015-01-19 19:00:58 +01001314 cfgerr++;
1315 continue;
1316 }
1317
1318 rflags = 0;
1319 rflags |= (arg->type_flags & ARGF_REG_ICASE) ? REG_ICASE : 0;
1320 err = NULL;
1321
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001322 if (!regex_comp(arg->data.str.area, reg, !(rflags & REG_ICASE), 1 /* capture substr */, &err)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001323 ha_alert("parsing [%s:%d] : error in regex '%s' in arg %d of %s%s%s%s '%s' %s proxy '%s' : %s.\n",
1324 cur->file, cur->line,
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001325 arg->data.str.area,
Christopher Faulet767a84b2017-11-24 16:50:31 +01001326 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id, err);
Willy Tarreau46947782015-01-19 19:00:58 +01001327 cfgerr++;
1328 continue;
1329 }
1330
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001331 free(arg->data.str.area);
1332 arg->data.str.area = NULL;
Willy Tarreau46947782015-01-19 19:00:58 +01001333 arg->unresolved = 0;
1334 arg->data.reg = reg;
1335 break;
1336
1337
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001338 }
1339
1340 LIST_DEL(&cur->list);
1341 free(cur);
1342 } /* end of args processing */
1343
1344 return cfgerr;
1345}
1346
1347/*
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001348 * Process a fetch + format conversion as defined by the sample expression
1349 * <expr> on request or response considering the <opt> parameter. The output is
Adis Nezirovic79beb242015-07-06 15:41:02 +02001350 * not explicitly set to <smp_type>, but shall be compatible with it as
1351 * specified by 'sample_casts' table. If a stable sample can be fetched, or an
1352 * unstable one when <opt> contains SMP_OPT_FINAL, the sample is converted and
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001353 * returned without the SMP_F_MAY_CHANGE flag. If an unstable sample is found
1354 * and <opt> does not contain SMP_OPT_FINAL, then the sample is returned as-is
1355 * with its SMP_F_MAY_CHANGE flag so that the caller can check it and decide to
1356 * take actions (eg: wait longer). If a sample could not be found or could not
Willy Tarreau6bcb0a82014-07-30 08:56:35 +02001357 * be converted, NULL is returned. The caller MUST NOT use the sample if the
1358 * SMP_F_MAY_CHANGE flag is present, as it is used only as a hint that there is
1359 * still hope to get it after waiting longer, and is not converted to string.
1360 * The possible output combinations are the following :
1361 *
1362 * return MAY_CHANGE FINAL Meaning for the sample
1363 * NULL * * Not present and will never be (eg: header)
1364 * smp 0 * Final value converted (eg: header)
1365 * smp 1 0 Not present yet, may appear later (eg: header)
1366 * smp 1 1 never happens (either flag is cleared on output)
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001367 */
Adis Nezirovic79beb242015-07-06 15:41:02 +02001368struct sample *sample_fetch_as_type(struct proxy *px, struct session *sess,
Willy Tarreau192252e2015-04-04 01:47:55 +02001369 struct stream *strm, unsigned int opt,
Adis Nezirovic79beb242015-07-06 15:41:02 +02001370 struct sample_expr *expr, int smp_type)
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001371{
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001372 struct sample *smp = &temp_smp;
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001373
Willy Tarreau6c616e02014-06-25 16:56:41 +02001374 memset(smp, 0, sizeof(*smp));
1375
Willy Tarreau192252e2015-04-04 01:47:55 +02001376 if (!sample_process(px, sess, strm, opt, expr, smp)) {
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001377 if ((smp->flags & SMP_F_MAY_CHANGE) && !(opt & SMP_OPT_FINAL))
1378 return smp;
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001379 return NULL;
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001380 }
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001381
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001382 if (!sample_casts[smp->data.type][smp_type])
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001383 return NULL;
1384
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001385 if (!sample_casts[smp->data.type][smp_type](smp))
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001386 return NULL;
1387
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001388 smp->flags &= ~SMP_F_MAY_CHANGE;
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001389 return smp;
1390}
1391
Christopher Faulet476e5d02016-10-26 11:34:47 +02001392static void release_sample_arg(struct arg *p)
1393{
1394 struct arg *p_back = p;
1395
1396 if (!p)
1397 return;
1398
1399 while (p->type != ARGT_STOP) {
1400 if (p->type == ARGT_STR || p->unresolved) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001401 free(p->data.str.area);
1402 p->data.str.area = NULL;
Christopher Faulet476e5d02016-10-26 11:34:47 +02001403 p->unresolved = 0;
1404 }
1405 else if (p->type == ARGT_REG) {
1406 if (p->data.reg) {
1407 regex_free(p->data.reg);
1408 free(p->data.reg);
1409 p->data.reg = NULL;
1410 }
1411 }
1412 p++;
1413 }
1414
1415 if (p_back != empty_arg_list)
1416 free(p_back);
1417}
1418
1419void release_sample_expr(struct sample_expr *expr)
1420{
1421 struct sample_conv_expr *conv_expr, *conv_exprb;
1422
1423 if (!expr)
1424 return;
1425
1426 list_for_each_entry_safe(conv_expr, conv_exprb, &expr->conv_exprs, list)
1427 release_sample_arg(conv_expr->arg_p);
1428 release_sample_arg(expr->arg_p);
1429 free(expr);
1430}
1431
Emeric Brun107ca302010-01-04 16:16:05 +01001432/*****************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +02001433/* Sample format convert functions */
Willy Tarreaub8c8f1f2012-04-23 22:38:26 +02001434/* These functions set the data type on return. */
Emeric Brun107ca302010-01-04 16:16:05 +01001435/*****************************************************************/
1436
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001437#ifdef DEBUG_EXPR
1438static int sample_conv_debug(const struct arg *arg_p, struct sample *smp, void *private)
1439{
1440 int i;
1441 struct sample tmp;
1442
1443 if (!(global.mode & MODE_QUIET) || (global.mode & (MODE_VERBOSE | MODE_STARTING))) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001444 fprintf(stderr, "[debug converter] type: %s ", smp_to_type[smp->data.type]);
1445 if (!sample_casts[smp->data.type][SMP_T_STR]) {
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001446 fprintf(stderr, "(undisplayable)");
1447 } else {
1448
1449 /* Copy sample fetch. This put the sample as const, the
1450 * cast will copy data if a transformation is required.
1451 */
1452 memcpy(&tmp, smp, sizeof(struct sample));
1453 tmp.flags = SMP_F_CONST;
1454
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001455 if (!sample_casts[smp->data.type][SMP_T_STR](&tmp))
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001456 fprintf(stderr, "(undisplayable)");
1457
1458 else {
1459 /* Display the displayable chars*. */
1460 fprintf(stderr, "<");
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001461 for (i = 0; i < tmp.data.u.str.data; i++) {
1462 if (isprint(tmp.data.u.str.area[i]))
1463 fputc(tmp.data.u.str.area[i],
1464 stderr);
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001465 else
1466 fputc('.', stderr);
1467 }
1468 }
1469 fprintf(stderr, ">\n");
1470 }
1471 }
1472 return 1;
1473}
1474#endif
1475
Holger Just1bfc24b2017-05-06 00:56:53 +02001476static int sample_conv_base642bin(const struct arg *arg_p, struct sample *smp, void *private)
1477{
Willy Tarreau83061a82018-07-13 11:56:34 +02001478 struct buffer *trash = get_trash_chunk();
Holger Just1bfc24b2017-05-06 00:56:53 +02001479 int bin_len;
1480
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001481 trash->data = 0;
1482 bin_len = base64dec(smp->data.u.str.area, smp->data.u.str.data,
1483 trash->area, trash->size);
Holger Just1bfc24b2017-05-06 00:56:53 +02001484 if (bin_len < 0)
1485 return 0;
1486
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001487 trash->data = bin_len;
Holger Just1bfc24b2017-05-06 00:56:53 +02001488 smp->data.u.str = *trash;
1489 smp->data.type = SMP_T_BIN;
1490 smp->flags &= ~SMP_F_CONST;
1491 return 1;
1492}
1493
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001494static int sample_conv_bin2base64(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brun53d1a982014-04-30 18:21:37 +02001495{
Willy Tarreau83061a82018-07-13 11:56:34 +02001496 struct buffer *trash = get_trash_chunk();
Emeric Brun53d1a982014-04-30 18:21:37 +02001497 int b64_len;
1498
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001499 trash->data = 0;
1500 b64_len = a2base64(smp->data.u.str.area, smp->data.u.str.data,
1501 trash->area, trash->size);
Emeric Brun53d1a982014-04-30 18:21:37 +02001502 if (b64_len < 0)
1503 return 0;
1504
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001505 trash->data = b64_len;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001506 smp->data.u.str = *trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001507 smp->data.type = SMP_T_STR;
Emeric Brun53d1a982014-04-30 18:21:37 +02001508 smp->flags &= ~SMP_F_CONST;
1509 return 1;
1510}
1511
Dragan Dosen6e5a9ca2017-10-24 09:18:23 +02001512static int sample_conv_sha1(const struct arg *arg_p, struct sample *smp, void *private)
1513{
1514 blk_SHA_CTX ctx;
Willy Tarreau83061a82018-07-13 11:56:34 +02001515 struct buffer *trash = get_trash_chunk();
Dragan Dosen6e5a9ca2017-10-24 09:18:23 +02001516
1517 memset(&ctx, 0, sizeof(ctx));
1518
1519 blk_SHA1_Init(&ctx);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001520 blk_SHA1_Update(&ctx, smp->data.u.str.area, smp->data.u.str.data);
1521 blk_SHA1_Final((unsigned char *) trash->area, &ctx);
Dragan Dosen6e5a9ca2017-10-24 09:18:23 +02001522
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001523 trash->data = 20;
Dragan Dosen6e5a9ca2017-10-24 09:18:23 +02001524 smp->data.u.str = *trash;
1525 smp->data.type = SMP_T_BIN;
1526 smp->flags &= ~SMP_F_CONST;
1527 return 1;
1528}
1529
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001530static int sample_conv_bin2hex(const struct arg *arg_p, struct sample *smp, void *private)
Thierry FOURNIER2f49d6d2014-03-12 15:01:52 +01001531{
Willy Tarreau83061a82018-07-13 11:56:34 +02001532 struct buffer *trash = get_trash_chunk();
Thierry FOURNIER2f49d6d2014-03-12 15:01:52 +01001533 unsigned char c;
1534 int ptr = 0;
1535
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001536 trash->data = 0;
1537 while (ptr < smp->data.u.str.data && trash->data <= trash->size - 2) {
1538 c = smp->data.u.str.area[ptr++];
1539 trash->area[trash->data++] = hextab[(c >> 4) & 0xF];
1540 trash->area[trash->data++] = hextab[c & 0xF];
Thierry FOURNIER2f49d6d2014-03-12 15:01:52 +01001541 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001542 smp->data.u.str = *trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001543 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01001544 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIER2f49d6d2014-03-12 15:01:52 +01001545 return 1;
1546}
1547
Dragan Dosen3f957b22017-10-24 09:27:34 +02001548static int sample_conv_hex2int(const struct arg *arg_p, struct sample *smp, void *private)
1549{
1550 long long int n = 0;
1551 int i, c;
1552
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001553 for (i = 0; i < smp->data.u.str.data; i++) {
1554 if ((c = hex2i(smp->data.u.str.area[i])) < 0)
Dragan Dosen3f957b22017-10-24 09:27:34 +02001555 return 0;
1556 n = (n << 4) + c;
1557 }
1558
1559 smp->data.u.sint = n;
1560 smp->data.type = SMP_T_SINT;
1561 smp->flags &= ~SMP_F_CONST;
1562 return 1;
1563}
1564
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001565/* hashes the binary input into a 32-bit unsigned int */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001566static int sample_conv_djb2(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001567{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001568 smp->data.u.sint = hash_djb2(smp->data.u.str.area,
1569 smp->data.u.str.data);
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001570 if (arg_p && arg_p->data.sint)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001571 smp->data.u.sint = full_hash(smp->data.u.sint);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001572 smp->data.type = SMP_T_SINT;
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001573 return 1;
1574}
1575
Willy Tarreau60a2ee72017-12-15 07:13:48 +01001576static int sample_conv_length(const struct arg *arg_p, struct sample *smp, void *private)
Etienne Carriereed0d24e2017-12-13 13:41:34 +01001577{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001578 int i = smp->data.u.str.data;
Etienne Carriereed0d24e2017-12-13 13:41:34 +01001579 smp->data.u.sint = i;
1580 smp->data.type = SMP_T_SINT;
1581 return 1;
1582}
1583
1584
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001585static int sample_conv_str2lower(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brun107ca302010-01-04 16:16:05 +01001586{
1587 int i;
1588
Willy Tarreauf0645dc2016-08-09 14:29:38 +02001589 if (!smp_make_rw(smp))
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01001590 return 0;
1591
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001592 for (i = 0; i < smp->data.u.str.data; i++) {
1593 if ((smp->data.u.str.area[i] >= 'A') && (smp->data.u.str.area[i] <= 'Z'))
1594 smp->data.u.str.area[i] += 'a' - 'A';
Emeric Brun107ca302010-01-04 16:16:05 +01001595 }
1596 return 1;
1597}
1598
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001599static int sample_conv_str2upper(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brun107ca302010-01-04 16:16:05 +01001600{
1601 int i;
1602
Willy Tarreauf0645dc2016-08-09 14:29:38 +02001603 if (!smp_make_rw(smp))
Emeric Brun485479d2010-09-23 18:02:19 +02001604 return 0;
1605
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001606 for (i = 0; i < smp->data.u.str.data; i++) {
1607 if ((smp->data.u.str.area[i] >= 'a') && (smp->data.u.str.area[i] <= 'z'))
1608 smp->data.u.str.area[i] += 'A' - 'a';
Emeric Brun107ca302010-01-04 16:16:05 +01001609 }
1610 return 1;
1611}
1612
Tim Duesterhus1478aa72018-01-25 16:24:51 +01001613/* takes the IPv4 mask in args[0] and an optional IPv6 mask in args[1] */
1614static int sample_conv_ipmask(const struct arg *args, struct sample *smp, void *private)
Willy Tarreaud31d6eb2010-01-26 18:01:41 +01001615{
Tim Duesterhus1478aa72018-01-25 16:24:51 +01001616 /* Attempt to convert to IPv4 to apply the correct mask. */
1617 c_ipv62ip(smp);
1618
1619 if (smp->data.type == SMP_T_IPV4) {
1620 smp->data.u.ipv4.s_addr &= args[0].data.ipv4.s_addr;
1621 smp->data.type = SMP_T_IPV4;
1622 }
1623 else if (smp->data.type == SMP_T_IPV6) {
1624 /* IPv6 cannot be converted without an IPv6 mask. */
1625 if (args[1].type != ARGT_IPV6)
1626 return 0;
1627
1628 *(uint32_t*)&smp->data.u.ipv6.s6_addr[0] &= *(uint32_t*)&args[1].data.ipv6.s6_addr[0];
1629 *(uint32_t*)&smp->data.u.ipv6.s6_addr[4] &= *(uint32_t*)&args[1].data.ipv6.s6_addr[4];
1630 *(uint32_t*)&smp->data.u.ipv6.s6_addr[8] &= *(uint32_t*)&args[1].data.ipv6.s6_addr[8];
1631 *(uint32_t*)&smp->data.u.ipv6.s6_addr[12] &= *(uint32_t*)&args[1].data.ipv6.s6_addr[12];
1632 smp->data.type = SMP_T_IPV6;
1633 }
1634
Willy Tarreaud31d6eb2010-01-26 18:01:41 +01001635 return 1;
1636}
1637
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001638/* takes an UINT value on input supposed to represent the time since EPOCH,
1639 * adds an optional offset found in args[1] and emits a string representing
1640 * the local time in the format specified in args[1] using strftime().
1641 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001642static int sample_conv_ltime(const struct arg *args, struct sample *smp, void *private)
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001643{
Willy Tarreau83061a82018-07-13 11:56:34 +02001644 struct buffer *temp;
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001645 /* With high numbers, the date returned can be negative, the 55 bits mask prevent this. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001646 time_t curr_date = smp->data.u.sint & 0x007fffffffffffffLL;
Thierry FOURNIERfac9ccf2015-07-08 00:15:20 +02001647 struct tm *tm;
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001648
1649 /* add offset */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001650 if (args[1].type == ARGT_SINT)
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001651 curr_date += args[1].data.sint;
1652
Thierry FOURNIERfac9ccf2015-07-08 00:15:20 +02001653 tm = localtime(&curr_date);
1654 if (!tm)
1655 return 0;
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001656 temp = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001657 temp->data = strftime(temp->area, temp->size, args[0].data.str.area,
1658 tm);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001659 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001660 smp->data.type = SMP_T_STR;
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001661 return 1;
1662}
1663
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001664/* hashes the binary input into a 32-bit unsigned int */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001665static int sample_conv_sdbm(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001666{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001667 smp->data.u.sint = hash_sdbm(smp->data.u.str.area,
1668 smp->data.u.str.data);
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001669 if (arg_p && arg_p->data.sint)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001670 smp->data.u.sint = full_hash(smp->data.u.sint);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001671 smp->data.type = SMP_T_SINT;
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001672 return 1;
1673}
1674
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001675/* takes an UINT value on input supposed to represent the time since EPOCH,
1676 * adds an optional offset found in args[1] and emits a string representing
1677 * the UTC date in the format specified in args[1] using strftime().
1678 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001679static int sample_conv_utime(const struct arg *args, struct sample *smp, void *private)
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001680{
Willy Tarreau83061a82018-07-13 11:56:34 +02001681 struct buffer *temp;
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001682 /* With high numbers, the date returned can be negative, the 55 bits mask prevent this. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001683 time_t curr_date = smp->data.u.sint & 0x007fffffffffffffLL;
Thierry FOURNIERfac9ccf2015-07-08 00:15:20 +02001684 struct tm *tm;
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001685
1686 /* add offset */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001687 if (args[1].type == ARGT_SINT)
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001688 curr_date += args[1].data.sint;
1689
Thierry FOURNIERfac9ccf2015-07-08 00:15:20 +02001690 tm = gmtime(&curr_date);
1691 if (!tm)
1692 return 0;
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001693 temp = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001694 temp->data = strftime(temp->area, temp->size, args[0].data.str.area,
1695 tm);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001696 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001697 smp->data.type = SMP_T_STR;
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001698 return 1;
1699}
1700
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001701/* hashes the binary input into a 32-bit unsigned int */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001702static int sample_conv_wt6(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001703{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001704 smp->data.u.sint = hash_wt6(smp->data.u.str.area,
1705 smp->data.u.str.data);
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001706 if (arg_p && arg_p->data.sint)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001707 smp->data.u.sint = full_hash(smp->data.u.sint);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001708 smp->data.type = SMP_T_SINT;
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001709 return 1;
1710}
1711
Thierry FOURNIER01e09742016-12-26 11:46:11 +01001712/* hashes the binary input into a 32-bit unsigned int using xxh.
1713 * The seed of the hash defaults to 0 but can be changd in argument 1.
1714 */
1715static int sample_conv_xxh32(const struct arg *arg_p, struct sample *smp, void *private)
1716{
1717 unsigned int seed;
1718
1719 if (arg_p && arg_p->data.sint)
1720 seed = arg_p->data.sint;
1721 else
1722 seed = 0;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001723 smp->data.u.sint = XXH32(smp->data.u.str.area, smp->data.u.str.data,
1724 seed);
Thierry FOURNIER01e09742016-12-26 11:46:11 +01001725 smp->data.type = SMP_T_SINT;
1726 return 1;
1727}
1728
1729/* hashes the binary input into a 64-bit unsigned int using xxh.
1730 * In fact, the function returns a 64 bit unsigned, but the sample
1731 * storage of haproxy only proposes 64-bits signed, so the value is
1732 * cast as signed. This cast doesn't impact the hash repartition.
1733 * The seed of the hash defaults to 0 but can be changd in argument 1.
1734 */
1735static int sample_conv_xxh64(const struct arg *arg_p, struct sample *smp, void *private)
1736{
1737 unsigned long long int seed;
1738
1739 if (arg_p && arg_p->data.sint)
1740 seed = (unsigned long long int)arg_p->data.sint;
1741 else
1742 seed = 0;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001743 smp->data.u.sint = (long long int)XXH64(smp->data.u.str.area,
1744 smp->data.u.str.data, seed);
Thierry FOURNIER01e09742016-12-26 11:46:11 +01001745 smp->data.type = SMP_T_SINT;
1746 return 1;
1747}
1748
Willy Tarreau80599772015-01-20 19:35:24 +01001749/* hashes the binary input into a 32-bit unsigned int */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001750static int sample_conv_crc32(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau80599772015-01-20 19:35:24 +01001751{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001752 smp->data.u.sint = hash_crc32(smp->data.u.str.area,
1753 smp->data.u.str.data);
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001754 if (arg_p && arg_p->data.sint)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001755 smp->data.u.sint = full_hash(smp->data.u.sint);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001756 smp->data.type = SMP_T_SINT;
Willy Tarreau80599772015-01-20 19:35:24 +01001757 return 1;
1758}
1759
Emmanuel Hocdet50791a72018-03-21 11:19:01 +01001760/* hashes the binary input into crc32c (RFC4960, Appendix B [8].) */
1761static int sample_conv_crc32c(const struct arg *arg_p, struct sample *smp, void *private)
1762{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001763 smp->data.u.sint = hash_crc32c(smp->data.u.str.area,
1764 smp->data.u.str.data);
Emmanuel Hocdet50791a72018-03-21 11:19:01 +01001765 if (arg_p && arg_p->data.sint)
1766 smp->data.u.sint = full_hash(smp->data.u.sint);
1767 smp->data.type = SMP_T_SINT;
1768 return 1;
1769}
1770
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001771/* This function escape special json characters. The returned string can be
1772 * safely set between two '"' and used as json string. The json string is
1773 * defined like this:
1774 *
1775 * any Unicode character except '"' or '\' or control character
1776 * \", \\, \/, \b, \f, \n, \r, \t, \u + four-hex-digits
1777 *
1778 * The enum input_type contain all the allowed mode for decoding the input
1779 * string.
1780 */
1781enum input_type {
1782 IT_ASCII = 0,
1783 IT_UTF8,
1784 IT_UTF8S,
1785 IT_UTF8P,
1786 IT_UTF8PS,
1787};
1788static int sample_conv_json_check(struct arg *arg, struct sample_conv *conv,
1789 const char *file, int line, char **err)
1790{
1791 if (!arg) {
1792 memprintf(err, "Unexpected empty arg list");
1793 return 0;
1794 }
1795
1796 if (arg->type != ARGT_STR) {
1797 memprintf(err, "Unexpected arg type");
1798 return 0;
1799 }
1800
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001801 if (strcmp(arg->data.str.area, "") == 0) {
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001802 arg->type = ARGT_SINT;
1803 arg->data.sint = IT_ASCII;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001804 return 1;
1805 }
1806
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001807 else if (strcmp(arg->data.str.area, "ascii") == 0) {
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001808 arg->type = ARGT_SINT;
1809 arg->data.sint = IT_ASCII;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001810 return 1;
1811 }
1812
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001813 else if (strcmp(arg->data.str.area, "utf8") == 0) {
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001814 arg->type = ARGT_SINT;
1815 arg->data.sint = IT_UTF8;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001816 return 1;
1817 }
1818
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001819 else if (strcmp(arg->data.str.area, "utf8s") == 0) {
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001820 arg->type = ARGT_SINT;
1821 arg->data.sint = IT_UTF8S;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001822 return 1;
1823 }
1824
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001825 else if (strcmp(arg->data.str.area, "utf8p") == 0) {
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001826 arg->type = ARGT_SINT;
1827 arg->data.sint = IT_UTF8P;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001828 return 1;
1829 }
1830
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001831 else if (strcmp(arg->data.str.area, "utf8ps") == 0) {
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001832 arg->type = ARGT_SINT;
1833 arg->data.sint = IT_UTF8PS;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001834 return 1;
1835 }
1836
Thierry FOURNIER / OZON.IOa69c9122016-11-23 01:13:57 +01001837 memprintf(err, "Unexpected input code type. "
1838 "Allowed value are 'ascii', 'utf8', 'utf8s', 'utf8p' and 'utf8ps'");
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001839 return 0;
1840}
1841
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001842static int sample_conv_json(const struct arg *arg_p, struct sample *smp, void *private)
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001843{
Willy Tarreau83061a82018-07-13 11:56:34 +02001844 struct buffer *temp;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001845 char _str[7]; /* \u + 4 hex digit + null char for sprintf. */
1846 const char *str;
1847 int len;
1848 enum input_type input_type = IT_ASCII;
1849 unsigned int c;
1850 unsigned int ret;
1851 char *p;
1852
1853 if (arg_p)
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001854 input_type = arg_p->data.sint;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001855
1856 temp = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001857 temp->data = 0;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001858
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001859 p = smp->data.u.str.area;
1860 while (p < smp->data.u.str.area + smp->data.u.str.data) {
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001861
1862 if (input_type == IT_ASCII) {
1863 /* Read input as ASCII. */
1864 c = *(unsigned char *)p;
1865 p++;
1866 }
1867 else {
1868 /* Read input as UTF8. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001869 ret = utf8_next(p,
1870 smp->data.u.str.data - ( p - smp->data.u.str.area),
1871 &c);
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001872 p += utf8_return_length(ret);
1873
1874 if (input_type == IT_UTF8 && utf8_return_code(ret) != UTF8_CODE_OK)
1875 return 0;
1876 if (input_type == IT_UTF8S && utf8_return_code(ret) != UTF8_CODE_OK)
1877 continue;
1878 if (input_type == IT_UTF8P && utf8_return_code(ret) & (UTF8_CODE_INVRANGE|UTF8_CODE_BADSEQ))
1879 return 0;
1880 if (input_type == IT_UTF8PS && utf8_return_code(ret) & (UTF8_CODE_INVRANGE|UTF8_CODE_BADSEQ))
1881 continue;
1882
1883 /* Check too big values. */
1884 if ((unsigned int)c > 0xffff) {
1885 if (input_type == IT_UTF8 || input_type == IT_UTF8P)
1886 return 0;
1887 continue;
1888 }
1889 }
1890
1891 /* Convert character. */
1892 if (c == '"') {
1893 len = 2;
1894 str = "\\\"";
1895 }
1896 else if (c == '\\') {
1897 len = 2;
1898 str = "\\\\";
1899 }
1900 else if (c == '/') {
1901 len = 2;
1902 str = "\\/";
1903 }
1904 else if (c == '\b') {
1905 len = 2;
1906 str = "\\b";
1907 }
1908 else if (c == '\f') {
1909 len = 2;
1910 str = "\\f";
1911 }
1912 else if (c == '\r') {
1913 len = 2;
1914 str = "\\r";
1915 }
1916 else if (c == '\n') {
1917 len = 2;
1918 str = "\\n";
1919 }
1920 else if (c == '\t') {
1921 len = 2;
1922 str = "\\t";
1923 }
1924 else if (c > 0xff || !isprint(c)) {
1925 /* isprint generate a segfault if c is too big. The man says that
1926 * c must have the value of an unsigned char or EOF.
1927 */
1928 len = 6;
1929 _str[0] = '\\';
1930 _str[1] = 'u';
1931 snprintf(&_str[2], 5, "%04x", (unsigned short)c);
1932 str = _str;
1933 }
1934 else {
1935 len = 1;
1936 str = (char *)&c;
1937 }
1938
1939 /* Check length */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001940 if (temp->data + len > temp->size)
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001941 return 0;
1942
1943 /* Copy string. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001944 memcpy(temp->area + temp->data, str, len);
1945 temp->data += len;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001946 }
1947
1948 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001949 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001950 smp->data.type = SMP_T_STR;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001951
1952 return 1;
1953}
1954
Emeric Brun54c4ac82014-11-03 15:32:43 +01001955/* This sample function is designed to extract some bytes from an input buffer.
1956 * First arg is the offset.
1957 * Optional second arg is the length to truncate */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001958static int sample_conv_bytes(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brun54c4ac82014-11-03 15:32:43 +01001959{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001960 if (smp->data.u.str.data <= arg_p[0].data.sint) {
1961 smp->data.u.str.data = 0;
Emeric Brun54c4ac82014-11-03 15:32:43 +01001962 return 1;
1963 }
1964
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001965 if (smp->data.u.str.size)
1966 smp->data.u.str.size -= arg_p[0].data.sint;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001967 smp->data.u.str.data -= arg_p[0].data.sint;
1968 smp->data.u.str.area += arg_p[0].data.sint;
Emeric Brun54c4ac82014-11-03 15:32:43 +01001969
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001970 if ((arg_p[1].type == ARGT_SINT) && (arg_p[1].data.sint < smp->data.u.str.data))
1971 smp->data.u.str.data = arg_p[1].data.sint;
Emeric Brun54c4ac82014-11-03 15:32:43 +01001972
1973 return 1;
1974}
1975
Emeric Brunf399b0d2014-11-03 17:07:03 +01001976static int sample_conv_field_check(struct arg *args, struct sample_conv *conv,
1977 const char *file, int line, char **err)
1978{
1979 struct arg *arg = args;
1980
1981 if (!arg) {
1982 memprintf(err, "Unexpected empty arg list");
1983 return 0;
1984 }
1985
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001986 if (arg->type != ARGT_SINT) {
Emeric Brunf399b0d2014-11-03 17:07:03 +01001987 memprintf(err, "Unexpected arg type");
1988 return 0;
1989 }
1990
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001991 if (!arg->data.sint) {
Emeric Brunf399b0d2014-11-03 17:07:03 +01001992 memprintf(err, "Unexpected value 0 for index");
1993 return 0;
1994 }
1995
1996 arg++;
1997
1998 if (arg->type != ARGT_STR) {
1999 memprintf(err, "Unexpected arg type");
2000 return 0;
2001 }
2002
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002003 if (!arg->data.str.data) {
Emeric Brunf399b0d2014-11-03 17:07:03 +01002004 memprintf(err, "Empty separators list");
2005 return 0;
2006 }
2007
2008 return 1;
2009}
2010
2011/* This sample function is designed to a return selected part of a string (field).
2012 * First arg is the index of the field (start at 1)
2013 * Second arg is a char list of separators (type string)
2014 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002015static int sample_conv_field(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brunf399b0d2014-11-03 17:07:03 +01002016{
Marcin Deranek9631a282018-04-16 14:30:46 +02002017 int field;
Emeric Brunf399b0d2014-11-03 17:07:03 +01002018 char *start, *end;
2019 int i;
Marcin Deranek9631a282018-04-16 14:30:46 +02002020 int count = (arg_p[2].type == ARGT_SINT) ? arg_p[2].data.sint : 1;
Emeric Brunf399b0d2014-11-03 17:07:03 +01002021
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002022 if (!arg_p[0].data.sint)
Emeric Brunf399b0d2014-11-03 17:07:03 +01002023 return 0;
2024
Marcin Deranek9631a282018-04-16 14:30:46 +02002025 if (arg_p[0].data.sint < 0) {
2026 field = -1;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002027 end = start = smp->data.u.str.area + smp->data.u.str.data;
2028 while (start > smp->data.u.str.area) {
2029 for (i = 0 ; i < arg_p[1].data.str.data; i++) {
2030 if (*(start-1) == arg_p[1].data.str.area[i]) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002031 if (field == arg_p[0].data.sint) {
2032 if (count == 1)
2033 goto found;
2034 else if (count > 1)
2035 count--;
2036 } else {
2037 end = start-1;
2038 field--;
2039 }
2040 break;
2041 }
Emeric Brunf399b0d2014-11-03 17:07:03 +01002042 }
Marcin Deranek9631a282018-04-16 14:30:46 +02002043 start--;
Emeric Brunf399b0d2014-11-03 17:07:03 +01002044 }
Marcin Deranek9631a282018-04-16 14:30:46 +02002045 } else {
2046 field = 1;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002047 end = start = smp->data.u.str.area;
2048 while (end - smp->data.u.str.area < smp->data.u.str.data) {
2049 for (i = 0 ; i < arg_p[1].data.str.data; i++) {
2050 if (*end == arg_p[1].data.str.area[i]) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002051 if (field == arg_p[0].data.sint) {
2052 if (count == 1)
2053 goto found;
2054 else if (count > 1)
2055 count--;
2056 } else {
2057 start = end+1;
2058 field++;
2059 }
2060 break;
2061 }
2062 }
2063 end++;
2064 }
Emeric Brunf399b0d2014-11-03 17:07:03 +01002065 }
2066
2067 /* Field not found */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002068 if (field != arg_p[0].data.sint) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002069 smp->data.u.str.data = 0;
Emeric Brunf399b0d2014-11-03 17:07:03 +01002070 return 1;
2071 }
2072found:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002073 smp->data.u.str.data = end - start;
Emeric Brunf399b0d2014-11-03 17:07:03 +01002074 /* If ret string is len 0, no need to
2075 change pointers or to update size */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002076 if (!smp->data.u.str.data)
Emeric Brunf399b0d2014-11-03 17:07:03 +01002077 return 1;
2078
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002079 smp->data.u.str.area = start;
Emeric Brunf399b0d2014-11-03 17:07:03 +01002080
2081 /* Compute remaining size if needed
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002082 Note: smp->data.u.str.size cannot be set to 0 */
2083 if (smp->data.u.str.size)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002084 smp->data.u.str.size -= start - smp->data.u.str.area;
Emeric Brunf399b0d2014-11-03 17:07:03 +01002085
2086 return 1;
2087}
2088
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002089/* This sample function is designed to return a word from a string.
2090 * First arg is the index of the word (start at 1)
2091 * Second arg is a char list of words separators (type string)
2092 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002093static int sample_conv_word(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002094{
Marcin Deranek9631a282018-04-16 14:30:46 +02002095 int word;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002096 char *start, *end;
2097 int i, issep, inword;
Marcin Deranek9631a282018-04-16 14:30:46 +02002098 int count = (arg_p[2].type == ARGT_SINT) ? arg_p[2].data.sint : 1;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002099
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002100 if (!arg_p[0].data.sint)
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002101 return 0;
2102
2103 word = 0;
2104 inword = 0;
Marcin Deranek9631a282018-04-16 14:30:46 +02002105 if (arg_p[0].data.sint < 0) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002106 end = start = smp->data.u.str.area + smp->data.u.str.data;
2107 while (start > smp->data.u.str.area) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002108 issep = 0;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002109 for (i = 0 ; i < arg_p[1].data.str.data; i++) {
2110 if (*(start-1) == arg_p[1].data.str.area[i]) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002111 issep = 1;
2112 break;
2113 }
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002114 }
Marcin Deranek9631a282018-04-16 14:30:46 +02002115 if (!inword) {
2116 if (!issep) {
2117 if (word != arg_p[0].data.sint) {
2118 word--;
2119 end = start;
2120 }
2121 inword = 1;
2122 }
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002123 }
Marcin Deranek9631a282018-04-16 14:30:46 +02002124 else if (issep) {
Willy Tarreau9eb2a4a2018-04-19 10:33:28 +02002125 if (word == arg_p[0].data.sint) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002126 if (count == 1)
2127 goto found;
2128 else if (count > 1)
2129 count--;
Willy Tarreau9eb2a4a2018-04-19 10:33:28 +02002130 }
Marcin Deranek9631a282018-04-16 14:30:46 +02002131 inword = 0;
2132 }
2133 start--;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002134 }
Marcin Deranek9631a282018-04-16 14:30:46 +02002135 } else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002136 end = start = smp->data.u.str.area;
2137 while (end - smp->data.u.str.area < smp->data.u.str.data) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002138 issep = 0;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002139 for (i = 0 ; i < arg_p[1].data.str.data; i++) {
2140 if (*end == arg_p[1].data.str.area[i]) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002141 issep = 1;
2142 break;
2143 }
2144 }
2145 if (!inword) {
2146 if (!issep) {
2147 if (word != arg_p[0].data.sint) {
2148 word++;
2149 start = end;
2150 }
2151 inword = 1;
2152 }
2153 }
2154 else if (issep) {
Willy Tarreau9eb2a4a2018-04-19 10:33:28 +02002155 if (word == arg_p[0].data.sint) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002156 if (count == 1)
2157 goto found;
2158 else if (count > 1)
2159 count--;
Willy Tarreau9eb2a4a2018-04-19 10:33:28 +02002160 }
Marcin Deranek9631a282018-04-16 14:30:46 +02002161 inword = 0;
2162 }
2163 end++;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002164 }
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002165 }
2166
2167 /* Field not found */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002168 if (word != arg_p[0].data.sint) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002169 smp->data.u.str.data = 0;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002170 return 1;
2171 }
2172found:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002173 smp->data.u.str.data = end - start;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002174 /* If ret string is len 0, no need to
2175 change pointers or to update size */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002176 if (!smp->data.u.str.data)
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002177 return 1;
2178
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002179 smp->data.u.str.area = start;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002180
2181 /* Compute remaining size if needed
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002182 Note: smp->data.u.str.size cannot be set to 0 */
2183 if (smp->data.u.str.size)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002184 smp->data.u.str.size -= start - smp->data.u.str.area;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002185
2186 return 1;
2187}
2188
Willy Tarreau7eda8492015-01-20 19:47:06 +01002189static int sample_conv_regsub_check(struct arg *args, struct sample_conv *conv,
2190 const char *file, int line, char **err)
2191{
2192 struct arg *arg = args;
2193 char *p;
2194 int len;
2195
2196 /* arg0 is a regex, it uses type_flag for ICASE and global match */
2197 arg[0].type_flags = 0;
2198
2199 if (arg[2].type != ARGT_STR)
2200 return 1;
2201
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002202 p = arg[2].data.str.area;
2203 len = arg[2].data.str.data;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002204 while (len) {
2205 if (*p == 'i') {
2206 arg[0].type_flags |= ARGF_REG_ICASE;
2207 }
2208 else if (*p == 'g') {
2209 arg[0].type_flags |= ARGF_REG_GLOB;
2210 }
2211 else {
2212 memprintf(err, "invalid regex flag '%c', only 'i' and 'g' are supported", *p);
2213 return 0;
2214 }
2215 p++;
2216 len--;
2217 }
2218 return 1;
2219}
2220
2221/* This sample function is designed to do the equivalent of s/match/replace/ on
2222 * the input string. It applies a regex and restarts from the last matched
2223 * location until nothing matches anymore. First arg is the regex to apply to
2224 * the input string, second arg is the replacement expression.
2225 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002226static int sample_conv_regsub(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau7eda8492015-01-20 19:47:06 +01002227{
2228 char *start, *end;
2229 struct my_regex *reg = arg_p[0].data.reg;
2230 regmatch_t pmatch[MAX_MATCH];
Willy Tarreau83061a82018-07-13 11:56:34 +02002231 struct buffer *trash = get_trash_chunk();
Willy Tarreau7eda8492015-01-20 19:47:06 +01002232 int flag, max;
2233 int found;
2234
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002235 start = smp->data.u.str.area;
2236 end = start + smp->data.u.str.data;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002237
2238 flag = 0;
2239 while (1) {
2240 /* check for last round which is used to copy remaining parts
2241 * when not running in global replacement mode.
2242 */
2243 found = 0;
2244 if ((arg_p[0].type_flags & ARGF_REG_GLOB) || !(flag & REG_NOTBOL)) {
2245 /* Note: we can have start == end on empty strings or at the end */
2246 found = regex_exec_match2(reg, start, end - start, MAX_MATCH, pmatch, flag);
2247 }
2248
2249 if (!found)
2250 pmatch[0].rm_so = end - start;
2251
2252 /* copy the heading non-matching part (which may also be the tail if nothing matches) */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002253 max = trash->size - trash->data;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002254 if (max && pmatch[0].rm_so > 0) {
2255 if (max > pmatch[0].rm_so)
2256 max = pmatch[0].rm_so;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002257 memcpy(trash->area + trash->data, start, max);
2258 trash->data += max;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002259 }
2260
2261 if (!found)
2262 break;
2263
2264 /* replace the matching part */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002265 max = trash->size - trash->data;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002266 if (max) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002267 if (max > arg_p[1].data.str.data)
2268 max = arg_p[1].data.str.data;
2269 memcpy(trash->area + trash->data,
2270 arg_p[1].data.str.area, max);
2271 trash->data += max;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002272 }
2273
2274 /* stop here if we're done with this string */
2275 if (start >= end)
2276 break;
2277
2278 /* We have a special case for matches of length 0 (eg: "x*y*").
2279 * These ones are considered to match in front of a character,
2280 * so we have to copy that character and skip to the next one.
2281 */
2282 if (!pmatch[0].rm_eo) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002283 if (trash->data < trash->size)
2284 trash->area[trash->data++] = start[pmatch[0].rm_eo];
Willy Tarreau7eda8492015-01-20 19:47:06 +01002285 pmatch[0].rm_eo++;
2286 }
2287
2288 start += pmatch[0].rm_eo;
2289 flag |= REG_NOTBOL;
2290 }
2291
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002292 smp->data.u.str = *trash;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002293 return 1;
2294}
2295
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002296/* This function check an operator entry. It expects a string.
2297 * The string can be an integer or a variable name.
2298 */
2299static int check_operator(struct arg *args, struct sample_conv *conv,
2300 const char *file, int line, char **err)
2301{
2302 const char *str;
2303 const char *end;
2304
2305 /* Try to decode a variable. */
2306 if (vars_check_arg(&args[0], NULL))
2307 return 1;
2308
2309 /* Try to convert an integer */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002310 str = args[0].data.str.area;
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002311 end = str + strlen(str);
2312 args[0].data.sint = read_int64(&str, end);
2313 if (*str != '\0') {
2314 memprintf(err, "expects an integer or a variable name");
2315 return 0;
2316 }
2317 args[0].type = ARGT_SINT;
2318 return 1;
2319}
2320
Willy Tarreau6204cd92016-03-10 16:33:04 +01002321/* This function returns a sample struct filled with an arg content.
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002322 * If the arg contain an integer, the integer is returned in the
2323 * sample. If the arg contains a variable descriptor, it returns the
2324 * variable value.
2325 *
2326 * This function returns 0 if an error occurs, otherwise it returns 1.
2327 */
Willy Tarreau6204cd92016-03-10 16:33:04 +01002328static inline int sample_conv_var2smp(const struct arg *arg, struct sample *smp)
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002329{
2330 switch (arg->type) {
2331 case ARGT_SINT:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002332 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002333 smp->data.u.sint = arg->data.sint;
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002334 return 1;
2335 case ARGT_VAR:
Willy Tarreau6204cd92016-03-10 16:33:04 +01002336 if (!vars_get_by_desc(&arg->data.var, smp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002337 return 0;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002338 if (!sample_casts[smp->data.type][SMP_T_SINT])
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002339 return 0;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002340 if (!sample_casts[smp->data.type][SMP_T_SINT](smp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002341 return 0;
2342 return 1;
2343 default:
2344 return 0;
2345 }
2346}
2347
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002348/* Takes a SINT on input, applies a binary twos complement and returns the SINT
Willy Tarreau97707872015-01-27 15:12:13 +01002349 * result.
2350 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002351static int sample_conv_binary_cpl(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002352{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002353 smp->data.u.sint = ~smp->data.u.sint;
Willy Tarreau97707872015-01-27 15:12:13 +01002354 return 1;
2355}
2356
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002357/* Takes a SINT on input, applies a binary "and" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08002358 * arg_p or in the variable described in arg_p, and returns the SINT result.
Willy Tarreau97707872015-01-27 15:12:13 +01002359 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002360static int sample_conv_binary_and(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002361{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002362 struct sample tmp;
2363
Willy Tarreau7560dd42016-03-10 16:28:58 +01002364 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreau6204cd92016-03-10 16:33:04 +01002365 if (!sample_conv_var2smp(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002366 return 0;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002367 smp->data.u.sint &= tmp.data.u.sint;
Willy Tarreau97707872015-01-27 15:12:13 +01002368 return 1;
2369}
2370
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002371/* Takes a SINT on input, applies a binary "or" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08002372 * arg_p or in the variable described in arg_p, and returns the SINT result.
Willy Tarreau97707872015-01-27 15:12:13 +01002373 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002374static int sample_conv_binary_or(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002375{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002376 struct sample tmp;
2377
Willy Tarreau7560dd42016-03-10 16:28:58 +01002378 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreau6204cd92016-03-10 16:33:04 +01002379 if (!sample_conv_var2smp(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002380 return 0;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002381 smp->data.u.sint |= tmp.data.u.sint;
Willy Tarreau97707872015-01-27 15:12:13 +01002382 return 1;
2383}
2384
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002385/* Takes a SINT on input, applies a binary "xor" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08002386 * arg_p or in the variable described in arg_p, and returns the SINT result.
Willy Tarreau97707872015-01-27 15:12:13 +01002387 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002388static int sample_conv_binary_xor(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002389{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002390 struct sample tmp;
2391
Willy Tarreau7560dd42016-03-10 16:28:58 +01002392 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreau6204cd92016-03-10 16:33:04 +01002393 if (!sample_conv_var2smp(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002394 return 0;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002395 smp->data.u.sint ^= tmp.data.u.sint;
Willy Tarreau97707872015-01-27 15:12:13 +01002396 return 1;
2397}
2398
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002399static inline long long int arith_add(long long int a, long long int b)
2400{
2401 /* Prevent overflow and makes capped calculus.
2402 * We must ensure that the check calculus doesn't
2403 * exceed the signed 64 bits limits.
2404 *
2405 * +----------+----------+
2406 * | a<0 | a>=0 |
2407 * +------+----------+----------+
2408 * | b<0 | MIN-a>b | no check |
2409 * +------+----------+----------+
2410 * | b>=0 | no check | MAX-a<b |
2411 * +------+----------+----------+
2412 */
2413 if ((a ^ b) >= 0) {
2414 /* signs are differents. */
2415 if (a < 0) {
2416 if (LLONG_MIN - a > b)
2417 return LLONG_MIN;
2418 }
2419 if (LLONG_MAX - a < b)
2420 return LLONG_MAX;
2421 }
2422 return a + b;
2423}
2424
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002425/* Takes a SINT on input, applies an arithmetic "add" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08002426 * arg_p or in the variable described in arg_p, and returns the SINT result.
Willy Tarreau97707872015-01-27 15:12:13 +01002427 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002428static int sample_conv_arith_add(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002429{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002430 struct sample tmp;
2431
Willy Tarreau7560dd42016-03-10 16:28:58 +01002432 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreau6204cd92016-03-10 16:33:04 +01002433 if (!sample_conv_var2smp(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002434 return 0;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002435 smp->data.u.sint = arith_add(smp->data.u.sint, tmp.data.u.sint);
Willy Tarreau97707872015-01-27 15:12:13 +01002436 return 1;
2437}
2438
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002439/* Takes a SINT on input, applies an arithmetic "sub" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08002440 * arg_p or in the variable described in arg_p, and returns the SINT result.
Willy Tarreau97707872015-01-27 15:12:13 +01002441 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002442static int sample_conv_arith_sub(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002443 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002444{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002445 struct sample tmp;
2446
Willy Tarreau7560dd42016-03-10 16:28:58 +01002447 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreau6204cd92016-03-10 16:33:04 +01002448 if (!sample_conv_var2smp(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002449 return 0;
2450
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002451 /* We cannot represent -LLONG_MIN because abs(LLONG_MIN) is greater
2452 * than abs(LLONG_MAX). So, the following code use LLONG_MAX in place
2453 * of -LLONG_MIN and correct the result.
2454 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002455 if (tmp.data.u.sint == LLONG_MIN) {
2456 smp->data.u.sint = arith_add(smp->data.u.sint, LLONG_MAX);
2457 if (smp->data.u.sint < LLONG_MAX)
2458 smp->data.u.sint++;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002459 return 1;
2460 }
2461
Joseph Herlant757f5ad2018-11-15 12:14:56 -08002462 /* standard subtraction: we use the "add" function and negate
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002463 * the second operand.
2464 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002465 smp->data.u.sint = arith_add(smp->data.u.sint, -tmp.data.u.sint);
Willy Tarreau97707872015-01-27 15:12:13 +01002466 return 1;
2467}
2468
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002469/* Takes a SINT on input, applies an arithmetic "mul" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08002470 * arg_p or in the variable described in arg_p, and returns the SINT result.
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002471 * If the result makes an overflow, then the largest possible quantity is
2472 * returned.
Willy Tarreau97707872015-01-27 15:12:13 +01002473 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002474static int sample_conv_arith_mul(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002475 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002476{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002477 struct sample tmp;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002478 long long int c;
2479
Willy Tarreau7560dd42016-03-10 16:28:58 +01002480 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreau6204cd92016-03-10 16:33:04 +01002481 if (!sample_conv_var2smp(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002482 return 0;
2483
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002484 /* prevent divide by 0 during the check */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002485 if (!smp->data.u.sint || !tmp.data.u.sint) {
2486 smp->data.u.sint = 0;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002487 return 1;
2488 }
2489
2490 /* The multiply between LLONG_MIN and -1 returns a
2491 * "floting point exception".
2492 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002493 if (smp->data.u.sint == LLONG_MIN && tmp.data.u.sint == -1) {
2494 smp->data.u.sint = LLONG_MAX;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002495 return 1;
2496 }
2497
2498 /* execute standard multiplication. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002499 c = smp->data.u.sint * tmp.data.u.sint;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002500
2501 /* check for overflow and makes capped multiply. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002502 if (smp->data.u.sint != c / tmp.data.u.sint) {
2503 if ((smp->data.u.sint < 0) == (tmp.data.u.sint < 0)) {
2504 smp->data.u.sint = LLONG_MAX;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002505 return 1;
2506 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002507 smp->data.u.sint = LLONG_MIN;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002508 return 1;
2509 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002510 smp->data.u.sint = c;
Willy Tarreau97707872015-01-27 15:12:13 +01002511 return 1;
2512}
2513
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002514/* Takes a SINT on input, applies an arithmetic "div" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08002515 * arg_p or in the variable described in arg_p, and returns the SINT result.
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002516 * If arg_p makes the result overflow, then the largest possible quantity is
2517 * returned.
Willy Tarreau97707872015-01-27 15:12:13 +01002518 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002519static int sample_conv_arith_div(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002520 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002521{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002522 struct sample tmp;
2523
Willy Tarreau7560dd42016-03-10 16:28:58 +01002524 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreau6204cd92016-03-10 16:33:04 +01002525 if (!sample_conv_var2smp(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002526 return 0;
2527
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002528 if (tmp.data.u.sint) {
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002529 /* The divide between LLONG_MIN and -1 returns a
2530 * "floting point exception".
2531 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002532 if (smp->data.u.sint == LLONG_MIN && tmp.data.u.sint == -1) {
2533 smp->data.u.sint = LLONG_MAX;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002534 return 1;
2535 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002536 smp->data.u.sint /= tmp.data.u.sint;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002537 return 1;
2538 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002539 smp->data.u.sint = LLONG_MAX;
Willy Tarreau97707872015-01-27 15:12:13 +01002540 return 1;
2541}
2542
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002543/* Takes a SINT on input, applies an arithmetic "mod" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08002544 * arg_p or in the variable described in arg_p, and returns the SINT result.
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002545 * If arg_p makes the result overflow, then 0 is returned.
Willy Tarreau97707872015-01-27 15:12:13 +01002546 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002547static int sample_conv_arith_mod(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002548 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002549{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002550 struct sample tmp;
2551
Willy Tarreau7560dd42016-03-10 16:28:58 +01002552 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreau6204cd92016-03-10 16:33:04 +01002553 if (!sample_conv_var2smp(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002554 return 0;
2555
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002556 if (tmp.data.u.sint) {
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002557 /* The divide between LLONG_MIN and -1 returns a
2558 * "floting point exception".
2559 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002560 if (smp->data.u.sint == LLONG_MIN && tmp.data.u.sint == -1) {
2561 smp->data.u.sint = 0;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002562 return 1;
2563 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002564 smp->data.u.sint %= tmp.data.u.sint;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002565 return 1;
2566 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002567 smp->data.u.sint = 0;
Willy Tarreau97707872015-01-27 15:12:13 +01002568 return 1;
2569}
2570
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002571/* Takes an SINT on input, applies an arithmetic "neg" and returns the SINT
Willy Tarreau97707872015-01-27 15:12:13 +01002572 * result.
2573 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002574static int sample_conv_arith_neg(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002575 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002576{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002577 if (smp->data.u.sint == LLONG_MIN)
2578 smp->data.u.sint = LLONG_MAX;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002579 else
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002580 smp->data.u.sint = -smp->data.u.sint;
Willy Tarreau97707872015-01-27 15:12:13 +01002581 return 1;
2582}
2583
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002584/* Takes a SINT on input, returns true is the value is non-null, otherwise
Willy Tarreau97707872015-01-27 15:12:13 +01002585 * false. The output is a BOOL.
2586 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002587static int sample_conv_arith_bool(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002588 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002589{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002590 smp->data.u.sint = !!smp->data.u.sint;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002591 smp->data.type = SMP_T_BOOL;
Willy Tarreau97707872015-01-27 15:12:13 +01002592 return 1;
2593}
2594
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002595/* Takes a SINT on input, returns false is the value is non-null, otherwise
Willy Tarreau97707872015-01-27 15:12:13 +01002596 * truee. The output is a BOOL.
2597 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002598static int sample_conv_arith_not(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002599 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002600{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002601 smp->data.u.sint = !smp->data.u.sint;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002602 smp->data.type = SMP_T_BOOL;
Willy Tarreau97707872015-01-27 15:12:13 +01002603 return 1;
2604}
2605
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002606/* Takes a SINT on input, returns true is the value is odd, otherwise false.
Willy Tarreau97707872015-01-27 15:12:13 +01002607 * The output is a BOOL.
2608 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002609static int sample_conv_arith_odd(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002610 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002611{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002612 smp->data.u.sint = smp->data.u.sint & 1;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002613 smp->data.type = SMP_T_BOOL;
Willy Tarreau97707872015-01-27 15:12:13 +01002614 return 1;
2615}
2616
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002617/* Takes a SINT on input, returns true is the value is even, otherwise false.
Willy Tarreau97707872015-01-27 15:12:13 +01002618 * The output is a BOOL.
2619 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002620static int sample_conv_arith_even(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002621 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002622{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002623 smp->data.u.sint = !(smp->data.u.sint & 1);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002624 smp->data.type = SMP_T_BOOL;
Willy Tarreau97707872015-01-27 15:12:13 +01002625 return 1;
2626}
2627
Willy Tarreau280f42b2018-02-19 15:34:12 +01002628/* appends an optional const string, an optional variable contents and another
2629 * optional const string to an existing string.
2630 */
2631static int sample_conv_concat(const struct arg *arg_p, struct sample *smp, void *private)
2632{
Willy Tarreau83061a82018-07-13 11:56:34 +02002633 struct buffer *trash;
Willy Tarreau280f42b2018-02-19 15:34:12 +01002634 struct sample tmp;
2635 int max;
2636
2637 trash = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002638 trash->data = smp->data.u.str.data;
2639 if (trash->data > trash->size - 1)
2640 trash->data = trash->size - 1;
Willy Tarreau280f42b2018-02-19 15:34:12 +01002641
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002642 memcpy(trash->area, smp->data.u.str.area, trash->data);
2643 trash->area[trash->data] = 0;
Willy Tarreau280f42b2018-02-19 15:34:12 +01002644
2645 /* append first string */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002646 max = arg_p[0].data.str.data;
2647 if (max > trash->size - 1 - trash->data)
2648 max = trash->size - 1 - trash->data;
Willy Tarreau280f42b2018-02-19 15:34:12 +01002649
2650 if (max) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002651 memcpy(trash->area + trash->data, arg_p[0].data.str.area, max);
2652 trash->data += max;
2653 trash->area[trash->data] = 0;
Willy Tarreau280f42b2018-02-19 15:34:12 +01002654 }
2655
2656 /* append second string (variable) if it's found and we can turn it
2657 * into a string.
2658 */
2659 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
2660 if (arg_p[1].type == ARGT_VAR && vars_get_by_desc(&arg_p[1].data.var, &tmp) &&
2661 (sample_casts[tmp.data.type][SMP_T_STR] == c_none ||
2662 sample_casts[tmp.data.type][SMP_T_STR](&tmp))) {
2663
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002664 max = tmp.data.u.str.data;
2665 if (max > trash->size - 1 - trash->data)
2666 max = trash->size - 1 - trash->data;
Willy Tarreau280f42b2018-02-19 15:34:12 +01002667
2668 if (max) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002669 memcpy(trash->area + trash->data, tmp.data.u.str.area,
2670 max);
2671 trash->data += max;
2672 trash->area[trash->data] = 0;
Willy Tarreau280f42b2018-02-19 15:34:12 +01002673 }
2674 }
2675
2676 /* append third string */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002677 max = arg_p[2].data.str.data;
2678 if (max > trash->size - 1 - trash->data)
2679 max = trash->size - 1 - trash->data;
Willy Tarreau280f42b2018-02-19 15:34:12 +01002680
2681 if (max) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002682 memcpy(trash->area + trash->data, arg_p[2].data.str.area, max);
2683 trash->data += max;
2684 trash->area[trash->data] = 0;
Willy Tarreau280f42b2018-02-19 15:34:12 +01002685 }
2686
2687 smp->data.u.str = *trash;
2688 smp->data.type = SMP_T_STR;
2689 return 1;
2690}
2691
2692/* This function checks the "concat" converter's arguments and extracts the
2693 * variable name and its scope.
2694 */
2695static int smp_check_concat(struct arg *args, struct sample_conv *conv,
2696 const char *file, int line, char **err)
2697{
2698 /* Try to decode a variable. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002699 if (args[1].data.str.data > 0 && !vars_check_arg(&args[1], NULL)) {
2700 memprintf(err, "failed to register variable name '%s'",
2701 args[1].data.str.area);
Willy Tarreau280f42b2018-02-19 15:34:12 +01002702 return 0;
2703 }
2704 return 1;
2705}
2706
Tim Duesterhusca097c12018-04-27 21:18:45 +02002707/* compares string with a variable containing a string. Return value
2708 * is compatible with strcmp(3)'s return value.
2709 */
2710static int sample_conv_strcmp(const struct arg *arg_p, struct sample *smp, void *private)
2711{
2712 struct sample tmp;
2713 int max, result;
2714
2715 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
2716 if (arg_p[0].type != ARGT_VAR)
2717 return 0;
2718 if (!vars_get_by_desc(&arg_p[0].data.var, &tmp))
2719 return 0;
2720 if (!sample_casts[tmp.data.type][SMP_T_STR](&tmp))
2721 return 0;
2722
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002723 max = MIN(smp->data.u.str.data, tmp.data.u.str.data);
2724 result = strncmp(smp->data.u.str.area, tmp.data.u.str.area, max);
Tim Duesterhusca097c12018-04-27 21:18:45 +02002725 if (result == 0) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002726 if (smp->data.u.str.data != tmp.data.u.str.data) {
2727 if (smp->data.u.str.data < tmp.data.u.str.data) {
Tim Duesterhusca097c12018-04-27 21:18:45 +02002728 result = -1;
2729 }
2730 else {
2731 result = 1;
2732 }
2733 }
2734 }
2735
2736 smp->data.u.sint = result;
2737 smp->data.type = SMP_T_SINT;
2738 return 1;
2739}
2740
2741/* This function checks the "strcmp" converter's arguments and extracts the
2742 * variable name and its scope.
2743 */
2744static int smp_check_strcmp(struct arg *args, struct sample_conv *conv,
2745 const char *file, int line, char **err)
2746{
2747 /* Try to decode a variable. */
2748 if (vars_check_arg(&args[0], NULL))
2749 return 1;
2750
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002751 memprintf(err, "failed to register variable name '%s'",
2752 args[0].data.str.area);
Tim Duesterhusca097c12018-04-27 21:18:45 +02002753 return 0;
2754}
2755
Willy Tarreau5b8ad222013-07-25 12:17:57 +02002756/************************************************************************/
2757/* All supported sample fetch functions must be declared here */
2758/************************************************************************/
2759
2760/* force TRUE to be returned at the fetch level */
2761static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002762smp_fetch_true(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau5b8ad222013-07-25 12:17:57 +02002763{
Willy Tarreau280f42b2018-02-19 15:34:12 +01002764 if (!smp_make_rw(smp))
2765 return 0;
2766
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002767 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002768 smp->data.u.sint = 1;
Willy Tarreau5b8ad222013-07-25 12:17:57 +02002769 return 1;
2770}
2771
2772/* force FALSE to be returned at the fetch level */
2773static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002774smp_fetch_false(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau5b8ad222013-07-25 12:17:57 +02002775{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002776 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002777 smp->data.u.sint = 0;
Willy Tarreau5b8ad222013-07-25 12:17:57 +02002778 return 1;
2779}
2780
2781/* retrieve environment variable $1 as a string */
2782static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002783smp_fetch_env(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau5b8ad222013-07-25 12:17:57 +02002784{
2785 char *env;
2786
2787 if (!args || args[0].type != ARGT_STR)
2788 return 0;
2789
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002790 env = getenv(args[0].data.str.area);
Willy Tarreau5b8ad222013-07-25 12:17:57 +02002791 if (!env)
2792 return 0;
2793
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002794 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01002795 smp->flags = SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002796 smp->data.u.str.area = env;
2797 smp->data.u.str.data = strlen(env);
Willy Tarreau5b8ad222013-07-25 12:17:57 +02002798 return 1;
2799}
2800
Willy Tarreau6236d3a2013-07-25 14:28:25 +02002801/* retrieve the current local date in epoch time, and applies an optional offset
2802 * of args[0] seconds.
2803 */
2804static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002805smp_fetch_date(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau6236d3a2013-07-25 14:28:25 +02002806{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002807 smp->data.u.sint = date.tv_sec;
Willy Tarreau6236d3a2013-07-25 14:28:25 +02002808
2809 /* add offset */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002810 if (args && args[0].type == ARGT_SINT)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002811 smp->data.u.sint += args[0].data.sint;
Willy Tarreau6236d3a2013-07-25 14:28:25 +02002812
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002813 smp->data.type = SMP_T_SINT;
Willy Tarreau6236d3a2013-07-25 14:28:25 +02002814 smp->flags |= SMP_F_VOL_TEST | SMP_F_MAY_CHANGE;
2815 return 1;
2816}
2817
Etienne Carrierea792a0a2018-01-17 13:43:24 +01002818/* retrieve the current microsecond part of the date */
2819static int
2820smp_fetch_date_us(const struct arg *args, struct sample *smp, const char *kw, void *private)
2821{
2822 smp->data.u.sint = date.tv_usec;
2823 smp->data.type = SMP_T_SINT;
2824 smp->flags |= SMP_F_VOL_TEST | SMP_F_MAY_CHANGE;
2825 return 1;
2826}
2827
2828
Nenad Merdanovic807a6e72017-03-12 22:00:00 +01002829/* returns the hostname */
2830static int
2831smp_fetch_hostname(const struct arg *args, struct sample *smp, const char *kw, void *private)
2832{
2833 smp->data.type = SMP_T_STR;
2834 smp->flags = SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002835 smp->data.u.str.area = hostname;
2836 smp->data.u.str.data = strlen(hostname);
Nenad Merdanovic807a6e72017-03-12 22:00:00 +01002837 return 1;
2838}
2839
Willy Tarreau0f30d262014-11-24 16:02:05 +01002840/* returns the number of processes */
2841static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002842smp_fetch_nbproc(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau0f30d262014-11-24 16:02:05 +01002843{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002844 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002845 smp->data.u.sint = global.nbproc;
Willy Tarreau0f30d262014-11-24 16:02:05 +01002846 return 1;
2847}
2848
2849/* returns the number of the current process (between 1 and nbproc */
2850static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002851smp_fetch_proc(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau0f30d262014-11-24 16:02:05 +01002852{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002853 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002854 smp->data.u.sint = relative_pid;
Willy Tarreau0f30d262014-11-24 16:02:05 +01002855 return 1;
2856}
2857
Christopher Faulet34adb2a2017-11-21 21:45:38 +01002858/* returns the number of the current thread (between 1 and nbthread */
2859static int
2860smp_fetch_thread(const struct arg *args, struct sample *smp, const char *kw, void *private)
2861{
2862 smp->data.type = SMP_T_SINT;
2863 smp->data.u.sint = tid;
2864 return 1;
2865}
2866
Willy Tarreau84310e22014-02-14 11:59:04 +01002867/* generate a random 32-bit integer for whatever purpose, with an optional
2868 * range specified in argument.
2869 */
2870static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002871smp_fetch_rand(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau84310e22014-02-14 11:59:04 +01002872{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002873 smp->data.u.sint = random();
Willy Tarreau84310e22014-02-14 11:59:04 +01002874
2875 /* reduce if needed. Don't do a modulo, use all bits! */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002876 if (args && args[0].type == ARGT_SINT)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002877 smp->data.u.sint = (smp->data.u.sint * args[0].data.sint) / ((u64)RAND_MAX+1);
Willy Tarreau84310e22014-02-14 11:59:04 +01002878
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002879 smp->data.type = SMP_T_SINT;
Willy Tarreau84310e22014-02-14 11:59:04 +01002880 smp->flags |= SMP_F_VOL_TEST | SMP_F_MAY_CHANGE;
2881 return 1;
2882}
2883
Willy Tarreau0f30d262014-11-24 16:02:05 +01002884/* returns true if the current process is stopping */
2885static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002886smp_fetch_stopping(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau0f30d262014-11-24 16:02:05 +01002887{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002888 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002889 smp->data.u.sint = stopping;
Willy Tarreau0f30d262014-11-24 16:02:05 +01002890 return 1;
2891}
2892
Willy Tarreau70fe9442018-11-22 16:07:39 +01002893/* returns the number of calls of the current stream's process_stream() */
2894static int
2895smp_fetch_cpu_calls(const struct arg *args, struct sample *smp, const char *kw, void *private)
2896{
2897 smp->data.type = SMP_T_SINT;
2898 smp->data.u.sint = smp->strm->task->calls;
2899 return 1;
2900}
2901
2902/* returns the average number of nanoseconds spent processing the stream per call */
2903static int
2904smp_fetch_cpu_ns_avg(const struct arg *args, struct sample *smp, const char *kw, void *private)
2905{
2906 smp->data.type = SMP_T_SINT;
2907 smp->data.u.sint = smp->strm->task->calls ? smp->strm->task->cpu_time / smp->strm->task->calls : 0;
2908 return 1;
2909}
2910
2911/* returns the total number of nanoseconds spent processing the stream */
2912static int
2913smp_fetch_cpu_ns_tot(const struct arg *args, struct sample *smp, const char *kw, void *private)
2914{
2915 smp->data.type = SMP_T_SINT;
2916 smp->data.u.sint = smp->strm->task->cpu_time;
2917 return 1;
2918}
2919
2920/* returns the average number of nanoseconds per call spent waiting for other tasks to be processed */
2921static int
2922smp_fetch_lat_ns_avg(const struct arg *args, struct sample *smp, const char *kw, void *private)
2923{
2924 smp->data.type = SMP_T_SINT;
2925 smp->data.u.sint = smp->strm->task->calls ? smp->strm->task->lat_time / smp->strm->task->calls : 0;
2926 return 1;
2927}
2928
2929/* returns the total number of nanoseconds per call spent waiting for other tasks to be processed */
2930static int
2931smp_fetch_lat_ns_tot(const struct arg *args, struct sample *smp, const char *kw, void *private)
2932{
2933 smp->data.type = SMP_T_SINT;
2934 smp->data.u.sint = smp->strm->task->lat_time;
2935 return 1;
2936}
2937
Thierry FOURNIERcc103292015-06-06 19:30:17 +02002938static int smp_fetch_const_str(const struct arg *args, struct sample *smp, const char *kw, void *private)
2939{
2940 smp->flags |= SMP_F_CONST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002941 smp->data.type = SMP_T_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002942 smp->data.u.str.area = args[0].data.str.area;
2943 smp->data.u.str.data = args[0].data.str.data;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02002944 return 1;
2945}
2946
2947static int smp_check_const_bool(struct arg *args, char **err)
2948{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002949 if (strcasecmp(args[0].data.str.area, "true") == 0 ||
2950 strcasecmp(args[0].data.str.area, "1") == 0) {
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002951 args[0].type = ARGT_SINT;
2952 args[0].data.sint = 1;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02002953 return 1;
2954 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002955 if (strcasecmp(args[0].data.str.area, "false") == 0 ||
2956 strcasecmp(args[0].data.str.area, "0") == 0) {
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002957 args[0].type = ARGT_SINT;
2958 args[0].data.sint = 0;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02002959 return 1;
2960 }
2961 memprintf(err, "Expects 'true', 'false', '0' or '1'");
2962 return 0;
2963}
2964
2965static int smp_fetch_const_bool(const struct arg *args, struct sample *smp, const char *kw, void *private)
2966{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002967 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002968 smp->data.u.sint = args[0].data.sint;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02002969 return 1;
2970}
2971
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002972static int smp_fetch_const_int(const struct arg *args, struct sample *smp, const char *kw, void *private)
Thierry FOURNIERcc103292015-06-06 19:30:17 +02002973{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002974 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002975 smp->data.u.sint = args[0].data.sint;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02002976 return 1;
2977}
2978
2979static int smp_fetch_const_ipv4(const struct arg *args, struct sample *smp, const char *kw, void *private)
2980{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002981 smp->data.type = SMP_T_IPV4;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002982 smp->data.u.ipv4 = args[0].data.ipv4;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02002983 return 1;
2984}
2985
2986static int smp_fetch_const_ipv6(const struct arg *args, struct sample *smp, const char *kw, void *private)
2987{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002988 smp->data.type = SMP_T_IPV6;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002989 smp->data.u.ipv6 = args[0].data.ipv6;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02002990 return 1;
2991}
2992
2993static int smp_check_const_bin(struct arg *args, char **err)
2994{
David Carlier64a16ab2016-04-08 10:37:02 +01002995 char *binstr = NULL;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02002996 int binstrlen;
2997
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002998 if (!parse_binary(args[0].data.str.area, &binstr, &binstrlen, err))
Thierry FOURNIERcc103292015-06-06 19:30:17 +02002999 return 0;
3000 args[0].type = ARGT_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003001 args[0].data.str.area = binstr;
3002 args[0].data.str.data = binstrlen;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003003 return 1;
3004}
3005
3006static int smp_fetch_const_bin(const struct arg *args, struct sample *smp, const char *kw, void *private)
3007{
3008 smp->flags |= SMP_F_CONST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003009 smp->data.type = SMP_T_BIN;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003010 smp->data.u.str.area = args[0].data.str.area;
3011 smp->data.u.str.data = args[0].data.str.data;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003012 return 1;
3013}
3014
3015static int smp_check_const_meth(struct arg *args, char **err)
3016{
3017 enum http_meth_t meth;
3018 int i;
3019
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003020 meth = find_http_meth(args[0].data.str.area, args[0].data.str.data);
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003021 if (meth != HTTP_METH_OTHER) {
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02003022 args[0].type = ARGT_SINT;
3023 args[0].data.sint = meth;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003024 } else {
3025 /* Check method avalaibility. A methos is a token defined as :
3026 * tchar = "!" / "#" / "$" / "%" / "&" / "'" / "*" / "+" / "-" / "." /
3027 * "^" / "_" / "`" / "|" / "~" / DIGIT / ALPHA
3028 * token = 1*tchar
3029 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003030 for (i = 0; i < args[0].data.str.data; i++) {
3031 if (!HTTP_IS_TOKEN(args[0].data.str.area[i])) {
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003032 memprintf(err, "expects valid method.");
3033 return 0;
3034 }
3035 }
3036 }
3037 return 1;
3038}
3039
3040static int smp_fetch_const_meth(const struct arg *args, struct sample *smp, const char *kw, void *private)
3041{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003042 smp->data.type = SMP_T_METH;
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02003043 if (args[0].type == ARGT_SINT) {
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003044 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003045 smp->data.u.meth.meth = args[0].data.sint;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003046 smp->data.u.meth.str.area = "";
3047 smp->data.u.meth.str.data = 0;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003048 } else {
3049 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003050 smp->data.u.meth.meth = HTTP_METH_OTHER;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003051 smp->data.u.meth.str.area = args[0].data.str.area;
3052 smp->data.u.meth.str.data = args[0].data.str.data;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003053 }
3054 return 1;
3055}
3056
Willy Tarreau5b8ad222013-07-25 12:17:57 +02003057/* Note: must not be declared <const> as its list will be overwritten.
3058 * Note: fetches that may return multiple types must be declared as the lowest
3059 * common denominator, the type that can be casted into all other ones. For
3060 * instance IPv4/IPv6 must be declared IPv4.
3061 */
3062static struct sample_fetch_kw_list smp_kws = {ILH, {
3063 { "always_false", smp_fetch_false, 0, NULL, SMP_T_BOOL, SMP_USE_INTRN },
3064 { "always_true", smp_fetch_true, 0, NULL, SMP_T_BOOL, SMP_USE_INTRN },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01003065 { "env", smp_fetch_env, ARG1(1,STR), NULL, SMP_T_STR, SMP_USE_INTRN },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02003066 { "date", smp_fetch_date, ARG1(0,SINT), NULL, SMP_T_SINT, SMP_USE_INTRN },
Etienne Carrierea792a0a2018-01-17 13:43:24 +01003067 { "date_us", smp_fetch_date_us, 0, NULL, SMP_T_SINT, SMP_USE_INTRN },
Nenad Merdanovic807a6e72017-03-12 22:00:00 +01003068 { "hostname", smp_fetch_hostname, 0, NULL, SMP_T_STR, SMP_USE_INTRN },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02003069 { "nbproc", smp_fetch_nbproc,0, NULL, SMP_T_SINT, SMP_USE_INTRN },
3070 { "proc", smp_fetch_proc, 0, NULL, SMP_T_SINT, SMP_USE_INTRN },
Christopher Faulet34adb2a2017-11-21 21:45:38 +01003071 { "thread", smp_fetch_thread, 0, NULL, SMP_T_SINT, SMP_USE_INTRN },
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02003072 { "rand", smp_fetch_rand, ARG1(0,SINT), NULL, SMP_T_SINT, SMP_USE_INTRN },
Willy Tarreau0f30d262014-11-24 16:02:05 +01003073 { "stopping", smp_fetch_stopping, 0, NULL, SMP_T_BOOL, SMP_USE_INTRN },
Willy Tarreau70fe9442018-11-22 16:07:39 +01003074 { "stopping", smp_fetch_stopping, 0, NULL, SMP_T_BOOL, SMP_USE_INTRN },
3075
3076 { "cpu_calls", smp_fetch_cpu_calls, 0, NULL, SMP_T_SINT, SMP_USE_INTRN },
3077 { "cpu_ns_avg", smp_fetch_cpu_ns_avg, 0, NULL, SMP_T_SINT, SMP_USE_INTRN },
3078 { "cpu_ns_tot", smp_fetch_cpu_ns_tot, 0, NULL, SMP_T_SINT, SMP_USE_INTRN },
3079 { "lat_ns_avg", smp_fetch_lat_ns_avg, 0, NULL, SMP_T_SINT, SMP_USE_INTRN },
3080 { "lat_ns_tot", smp_fetch_lat_ns_tot, 0, NULL, SMP_T_SINT, SMP_USE_INTRN },
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003081
3082 { "str", smp_fetch_const_str, ARG1(1,STR), NULL , SMP_T_STR, SMP_USE_INTRN },
3083 { "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 +02003084 { "int", smp_fetch_const_int, ARG1(1,SINT), NULL , SMP_T_SINT, SMP_USE_INTRN },
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003085 { "ipv4", smp_fetch_const_ipv4, ARG1(1,IPV4), NULL , SMP_T_IPV4, SMP_USE_INTRN },
3086 { "ipv6", smp_fetch_const_ipv6, ARG1(1,IPV6), NULL , SMP_T_IPV6, SMP_USE_INTRN },
3087 { "bin", smp_fetch_const_bin, ARG1(1,STR), smp_check_const_bin , SMP_T_BIN, SMP_USE_INTRN },
3088 { "meth", smp_fetch_const_meth, ARG1(1,STR), smp_check_const_meth, SMP_T_METH, SMP_USE_INTRN },
3089
Willy Tarreau5b8ad222013-07-25 12:17:57 +02003090 { /* END */ },
3091}};
3092
Willy Tarreau0108d902018-11-25 19:14:37 +01003093INITCALL1(STG_REGISTER, sample_register_fetches, &smp_kws);
3094
Emeric Brun107ca302010-01-04 16:16:05 +01003095/* Note: must not be declared <const> as its list will be overwritten */
Willy Tarreaudc13c112013-06-21 23:16:39 +02003096static struct sample_conv_kw_list sample_conv_kws = {ILH, {
Thierry FOURNIER9687c772015-05-07 15:46:29 +02003097#ifdef DEBUG_EXPR
3098 { "debug", sample_conv_debug, 0, NULL, SMP_T_ANY, SMP_T_ANY },
3099#endif
3100
Holger Just1bfc24b2017-05-06 00:56:53 +02003101 { "b64dec", sample_conv_base642bin,0, NULL, SMP_T_STR, SMP_T_BIN },
Emeric Brun53d1a982014-04-30 18:21:37 +02003102 { "base64", sample_conv_bin2base64,0, NULL, SMP_T_BIN, SMP_T_STR },
Willy Tarreau12785782012-04-27 21:37:17 +02003103 { "upper", sample_conv_str2upper, 0, NULL, SMP_T_STR, SMP_T_STR },
3104 { "lower", sample_conv_str2lower, 0, NULL, SMP_T_STR, SMP_T_STR },
Willy Tarreau60a2ee72017-12-15 07:13:48 +01003105 { "length", sample_conv_length, 0, NULL, SMP_T_STR, SMP_T_SINT },
Thierry FOURNIER2f49d6d2014-03-12 15:01:52 +01003106 { "hex", sample_conv_bin2hex, 0, NULL, SMP_T_BIN, SMP_T_STR },
Dragan Dosen3f957b22017-10-24 09:27:34 +02003107 { "hex2i", sample_conv_hex2int, 0, NULL, SMP_T_STR, SMP_T_SINT },
Tim Duesterhus1478aa72018-01-25 16:24:51 +01003108 { "ipmask", sample_conv_ipmask, ARG2(1,MSK4,MSK6), NULL, SMP_T_ADDR, SMP_T_IPV4 },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02003109 { "ltime", sample_conv_ltime, ARG2(1,STR,SINT), NULL, SMP_T_SINT, SMP_T_STR },
3110 { "utime", sample_conv_utime, ARG2(1,STR,SINT), NULL, SMP_T_SINT, SMP_T_STR },
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02003111 { "crc32", sample_conv_crc32, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
Emmanuel Hocdet50791a72018-03-21 11:19:01 +01003112 { "crc32c", sample_conv_crc32c, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02003113 { "djb2", sample_conv_djb2, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
3114 { "sdbm", sample_conv_sdbm, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
3115 { "wt6", sample_conv_wt6, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
Thierry FOURNIER01e09742016-12-26 11:46:11 +01003116 { "xxh32", sample_conv_xxh32, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
3117 { "xxh64", sample_conv_xxh64, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02003118 { "json", sample_conv_json, ARG1(1,STR), sample_conv_json_check, SMP_T_STR, SMP_T_STR },
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02003119 { "bytes", sample_conv_bytes, ARG2(1,SINT,SINT), NULL, SMP_T_BIN, SMP_T_BIN },
Marcin Deranek9631a282018-04-16 14:30:46 +02003120 { "field", sample_conv_field, ARG3(2,SINT,STR,SINT), sample_conv_field_check, SMP_T_STR, SMP_T_STR },
3121 { "word", sample_conv_word, ARG3(2,SINT,STR,SINT), sample_conv_field_check, SMP_T_STR, SMP_T_STR },
Willy Tarreau7eda8492015-01-20 19:47:06 +01003122 { "regsub", sample_conv_regsub, ARG3(2,REG,STR,STR), sample_conv_regsub_check, SMP_T_STR, SMP_T_STR },
Dragan Dosen6e5a9ca2017-10-24 09:18:23 +02003123 { "sha1", sample_conv_sha1, 0, NULL, SMP_T_BIN, SMP_T_BIN },
Willy Tarreau280f42b2018-02-19 15:34:12 +01003124 { "concat", sample_conv_concat, ARG3(1,STR,STR,STR), smp_check_concat, SMP_T_STR, SMP_T_STR },
Tim Duesterhusca097c12018-04-27 21:18:45 +02003125 { "strcmp", sample_conv_strcmp, ARG1(1,STR), smp_check_strcmp, SMP_T_STR, SMP_T_SINT },
Willy Tarreau97707872015-01-27 15:12:13 +01003126
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02003127 { "and", sample_conv_binary_and, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
3128 { "or", sample_conv_binary_or, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
3129 { "xor", sample_conv_binary_xor, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
3130 { "cpl", sample_conv_binary_cpl, 0, NULL, SMP_T_SINT, SMP_T_SINT },
3131 { "bool", sample_conv_arith_bool, 0, NULL, SMP_T_SINT, SMP_T_BOOL },
3132 { "not", sample_conv_arith_not, 0, NULL, SMP_T_SINT, SMP_T_BOOL },
3133 { "odd", sample_conv_arith_odd, 0, NULL, SMP_T_SINT, SMP_T_BOOL },
3134 { "even", sample_conv_arith_even, 0, NULL, SMP_T_SINT, SMP_T_BOOL },
3135 { "add", sample_conv_arith_add, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
3136 { "sub", sample_conv_arith_sub, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
3137 { "mul", sample_conv_arith_mul, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
3138 { "div", sample_conv_arith_div, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
3139 { "mod", sample_conv_arith_mod, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
3140 { "neg", sample_conv_arith_neg, 0, NULL, SMP_T_SINT, SMP_T_SINT },
Willy Tarreau97707872015-01-27 15:12:13 +01003141
Willy Tarreau9fcb9842012-04-20 14:45:49 +02003142 { NULL, NULL, 0, 0, 0 },
Emeric Brun107ca302010-01-04 16:16:05 +01003143}};
3144
Willy Tarreau0108d902018-11-25 19:14:37 +01003145INITCALL1(STG_REGISTER, sample_register_convs, &sample_conv_kws);