blob: 963cb597d2512b253d478154b10d4dd35eb09d54 [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();
Olivier Houchard4468f1c2018-12-07 15:23:41 +0100669 trash->data = smp->data.type == SMP_T_STR ?
670 smp->data.u.str.data : smp->data.u.meth.str.data;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200671 if (trash->data > trash->size - 1)
672 trash->data = trash->size - 1;
Willy Tarreauad635822016-08-08 19:21:09 +0200673
Olivier Houchard4468f1c2018-12-07 15:23:41 +0100674 memcpy(trash->area, smp->data.type == SMP_T_STR ?
675 smp->data.u.str.area : smp->data.u.meth.str.area,
676 trash->data);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200677 trash->area[trash->data] = 0;
Willy Tarreauad635822016-08-08 19:21:09 +0200678 smp->data.u.str = *trash;
679 break;
680
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100681 case SMP_T_BIN:
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100682 trash = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200683 trash->data = smp->data.u.str.data;
684 if (trash->data > trash->size)
685 trash->data = trash->size;
Willy Tarreauad635822016-08-08 19:21:09 +0200686
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200687 memcpy(trash->area, smp->data.u.str.area, trash->data);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200688 smp->data.u.str = *trash;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100689 break;
Christopher Fauletec100512017-07-24 15:38:41 +0200690
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100691 default:
692 /* Other cases are unexpected. */
693 return 0;
694 }
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100695
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100696 /* remove const flag */
697 smp->flags &= ~SMP_F_CONST;
Emeric Brun485479d2010-09-23 18:02:19 +0200698 return 1;
699}
700
Thierry FOURNIER0e9af552013-12-14 14:55:04 +0100701int c_none(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100702{
703 return 1;
704}
705
Willy Tarreau342acb42012-04-23 22:03:39 +0200706static int c_str2int(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100707{
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +0200708 const char *str;
709 const char *end;
Emeric Brun107ca302010-01-04 16:16:05 +0100710
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200711 if (smp->data.u.str.data == 0)
Thierry FOURNIER60bb0202014-01-27 18:20:48 +0100712 return 0;
713
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200714 str = smp->data.u.str.area;
715 end = smp->data.u.str.area + smp->data.u.str.data;
Emeric Brun107ca302010-01-04 16:16:05 +0100716
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200717 smp->data.u.sint = read_int64(&str, end);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200718 smp->data.type = SMP_T_SINT;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100719 smp->flags &= ~SMP_F_CONST;
Emeric Brun107ca302010-01-04 16:16:05 +0100720 return 1;
721}
722
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100723static int c_str2meth(struct sample *smp)
724{
725 enum http_meth_t meth;
726 int len;
727
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200728 meth = find_http_meth(smp->data.u.str.area, smp->data.u.str.data);
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100729 if (meth == HTTP_METH_OTHER) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200730 len = smp->data.u.str.data;
731 smp->data.u.meth.str.area = smp->data.u.str.area;
732 smp->data.u.meth.str.data = len;
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100733 }
734 else
735 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200736 smp->data.u.meth.meth = meth;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200737 smp->data.type = SMP_T_METH;
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100738 return 1;
739}
740
741static int c_meth2str(struct sample *smp)
742{
743 int len;
744 enum http_meth_t meth;
745
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200746 if (smp->data.u.meth.meth == HTTP_METH_OTHER) {
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100747 /* The method is unknown. Copy the original pointer. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200748 len = smp->data.u.meth.str.data;
749 smp->data.u.str.area = smp->data.u.meth.str.area;
750 smp->data.u.str.data = len;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200751 smp->data.type = SMP_T_STR;
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100752 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200753 else if (smp->data.u.meth.meth < HTTP_METH_OTHER) {
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100754 /* The method is known, copy the pointer containing the string. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200755 meth = smp->data.u.meth.meth;
Willy Tarreau35b51c62018-09-10 15:38:55 +0200756 smp->data.u.str.area = http_known_methods[meth].ptr;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200757 smp->data.u.str.data = http_known_methods[meth].len;
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100758 smp->flags |= SMP_F_CONST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200759 smp->data.type = SMP_T_STR;
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100760 }
761 else {
762 /* Unknown method */
763 return 0;
764 }
765 return 1;
766}
767
Willy Tarreau9700e5c2014-07-15 21:03:26 +0200768static int c_addr2bin(struct sample *smp)
769{
Willy Tarreau83061a82018-07-13 11:56:34 +0200770 struct buffer *chk = get_trash_chunk();
Willy Tarreau9700e5c2014-07-15 21:03:26 +0200771
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200772 if (smp->data.type == SMP_T_IPV4) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200773 chk->data = 4;
774 memcpy(chk->area, &smp->data.u.ipv4, chk->data);
Willy Tarreau9700e5c2014-07-15 21:03:26 +0200775 }
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200776 else if (smp->data.type == SMP_T_IPV6) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200777 chk->data = 16;
778 memcpy(chk->area, &smp->data.u.ipv6, chk->data);
Willy Tarreau9700e5c2014-07-15 21:03:26 +0200779 }
780 else
781 return 0;
782
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200783 smp->data.u.str = *chk;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200784 smp->data.type = SMP_T_BIN;
Willy Tarreau9700e5c2014-07-15 21:03:26 +0200785 return 1;
786}
787
Willy Tarreaubbfd1a22014-07-15 21:19:08 +0200788static int c_int2bin(struct sample *smp)
789{
Willy Tarreau83061a82018-07-13 11:56:34 +0200790 struct buffer *chk = get_trash_chunk();
Willy Tarreaubbfd1a22014-07-15 21:19:08 +0200791
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200792 *(unsigned long long int *) chk->area = my_htonll(smp->data.u.sint);
793 chk->data = 8;
Willy Tarreaubbfd1a22014-07-15 21:19:08 +0200794
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200795 smp->data.u.str = *chk;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200796 smp->data.type = SMP_T_BIN;
Willy Tarreaubbfd1a22014-07-15 21:19:08 +0200797 return 1;
798}
799
Willy Tarreau9700e5c2014-07-15 21:03:26 +0200800
Emeric Brun107ca302010-01-04 16:16:05 +0100801/*****************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +0200802/* Sample casts matrix: */
803/* sample_casts[from type][to type] */
804/* NULL pointer used for impossible sample casts */
Emeric Brun107ca302010-01-04 16:16:05 +0100805/*****************************************************************/
Emeric Brun107ca302010-01-04 16:16:05 +0100806
Thierry FOURNIER8af6ff12013-11-21 10:53:12 +0100807sample_cast_fct sample_casts[SMP_TYPES][SMP_TYPES] = {
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +0200808/* to: ANY BOOL SINT ADDR IPV4 IPV6 STR BIN METH */
809/* from: ANY */ { c_none, c_none, c_none, c_none, c_none, c_none, c_none, c_none, c_none, },
810/* BOOL */ { c_none, c_none, c_none, NULL, NULL, NULL, c_int2str, NULL, NULL, },
Thierry FOURNIERcc4d1712015-07-24 09:04:56 +0200811/* 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 +0200812/* ADDR */ { c_none, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, },
813/* IPV4 */ { c_none, NULL, c_ip2int, c_none, c_none, c_ip2ipv6, c_ip2str, c_addr2bin, NULL, },
Thierry FOURNIERcc4d1712015-07-24 09:04:56 +0200814/* IPV6 */ { c_none, NULL, NULL, c_none, c_ipv62ip,c_none, c_ipv62str, c_addr2bin, NULL, },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +0200815/* STR */ { c_none, c_str2int, c_str2int, c_str2addr, c_str2ip, c_str2ipv6, c_none, c_none, c_str2meth, },
816/* BIN */ { c_none, NULL, NULL, NULL, NULL, NULL, c_bin2str, c_none, c_str2meth, },
817/* METH */ { c_none, NULL, NULL, NULL, NULL, NULL, c_meth2str, c_meth2str, c_none, }
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200818};
Emeric Brun107ca302010-01-04 16:16:05 +0100819
Emeric Brun107ca302010-01-04 16:16:05 +0100820/*
Willy Tarreau12785782012-04-27 21:37:17 +0200821 * Parse a sample expression configuration:
Emeric Brun107ca302010-01-04 16:16:05 +0100822 * fetch keyword followed by format conversion keywords.
Willy Tarreau12785782012-04-27 21:37:17 +0200823 * Returns a pointer on allocated sample expression structure.
Willy Tarreaua4312fa2013-04-02 16:34:32 +0200824 * The caller must have set al->ctx.
Emeric Brun107ca302010-01-04 16:16:05 +0100825 */
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +0100826struct 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 +0100827{
Willy Tarreau833cc792013-07-24 15:34:19 +0200828 const char *begw; /* beginning of word */
829 const char *endw; /* end of word */
830 const char *endt; /* end of term */
Willy Tarreau12785782012-04-27 21:37:17 +0200831 struct sample_expr *expr;
832 struct sample_fetch *fetch;
833 struct sample_conv *conv;
Emeric Brun107ca302010-01-04 16:16:05 +0100834 unsigned long prev_type;
Willy Tarreau833cc792013-07-24 15:34:19 +0200835 char *fkw = NULL;
836 char *ckw = NULL;
Willy Tarreau689a1df2013-12-13 00:40:11 +0100837 int err_arg;
Emeric Brun107ca302010-01-04 16:16:05 +0100838
Willy Tarreau833cc792013-07-24 15:34:19 +0200839 begw = str[*idx];
840 for (endw = begw; *endw && *endw != '(' && *endw != ','; endw++);
841
842 if (endw == begw) {
Willy Tarreau975c1782013-12-12 23:16:54 +0100843 memprintf(err_msg, "missing fetch method");
Emeric Brun107ca302010-01-04 16:16:05 +0100844 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200845 }
Emeric Brun107ca302010-01-04 16:16:05 +0100846
Willy Tarreau833cc792013-07-24 15:34:19 +0200847 /* keep a copy of the current fetch keyword for error reporting */
848 fkw = my_strndup(begw, endw - begw);
Emeric Brun107ca302010-01-04 16:16:05 +0100849
Willy Tarreau833cc792013-07-24 15:34:19 +0200850 fetch = find_sample_fetch(begw, endw - begw);
851 if (!fetch) {
Willy Tarreau975c1782013-12-12 23:16:54 +0100852 memprintf(err_msg, "unknown fetch method '%s'", fkw);
Emeric Brun107ca302010-01-04 16:16:05 +0100853 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200854 }
Emeric Brun107ca302010-01-04 16:16:05 +0100855
Willy Tarreau833cc792013-07-24 15:34:19 +0200856 endt = endw;
857 if (*endt == '(') {
Willy Tarreau689a1df2013-12-13 00:40:11 +0100858 /* look for the end of this term and skip the opening parenthesis */
859 endt = ++endw;
Willy Tarreau833cc792013-07-24 15:34:19 +0200860 while (*endt && *endt != ')')
861 endt++;
862 if (*endt != ')') {
Willy Tarreau975c1782013-12-12 23:16:54 +0100863 memprintf(err_msg, "missing closing ')' after arguments to fetch keyword '%s'", fkw);
Willy Tarreau833cc792013-07-24 15:34:19 +0200864 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200865 }
Emeric Brun485479d2010-09-23 18:02:19 +0200866 }
Emeric Brun107ca302010-01-04 16:16:05 +0100867
Willy Tarreau833cc792013-07-24 15:34:19 +0200868 /* At this point, we have :
869 * - begw : beginning of the keyword
Willy Tarreau689a1df2013-12-13 00:40:11 +0100870 * - endw : end of the keyword, first character not part of keyword
871 * nor the opening parenthesis (so first character of args
872 * if present).
Willy Tarreau833cc792013-07-24 15:34:19 +0200873 * - endt : end of the term (=endw or last parenthesis if args are present)
874 */
875
876 if (fetch->out_type >= SMP_TYPES) {
Willy Tarreau975c1782013-12-12 23:16:54 +0100877 memprintf(err_msg, "returns type of fetch method '%s' is unknown", fkw);
Emeric Brun107ca302010-01-04 16:16:05 +0100878 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200879 }
Emeric Brun107ca302010-01-04 16:16:05 +0100880 prev_type = fetch->out_type;
Willy Tarreau833cc792013-07-24 15:34:19 +0200881
Vincent Bernat02779b62016-04-03 13:48:43 +0200882 expr = calloc(1, sizeof(*expr));
Emeric Brun485479d2010-09-23 18:02:19 +0200883 if (!expr)
884 goto out_error;
Emeric Brun107ca302010-01-04 16:16:05 +0100885
886 LIST_INIT(&(expr->conv_exprs));
887 expr->fetch = fetch;
Willy Tarreau2e845be2012-10-19 19:49:09 +0200888 expr->arg_p = empty_arg_list;
Emeric Brun107ca302010-01-04 16:16:05 +0100889
Willy Tarreau689a1df2013-12-13 00:40:11 +0100890 /* Note that we call the argument parser even with an empty string,
891 * this allows it to automatically create entries for mandatory
892 * implicit arguments (eg: local proxy name).
893 */
894 al->kw = expr->fetch->kw;
895 al->conv = NULL;
896 if (make_arg_list(endw, endt - endw, fetch->arg_mask, &expr->arg_p, err_msg, NULL, &err_arg, al) < 0) {
897 memprintf(err_msg, "fetch method '%s' : %s", fkw, *err_msg);
898 goto out_error;
899 }
Willy Tarreau2e845be2012-10-19 19:49:09 +0200900
Willy Tarreau689a1df2013-12-13 00:40:11 +0100901 if (!expr->arg_p) {
902 expr->arg_p = empty_arg_list;
Emeric Brun485479d2010-09-23 18:02:19 +0200903 }
Willy Tarreau689a1df2013-12-13 00:40:11 +0100904 else if (fetch->val_args && !fetch->val_args(expr->arg_p, err_msg)) {
905 memprintf(err_msg, "invalid args in fetch method '%s' : %s", fkw, *err_msg);
Emeric Brun485479d2010-09-23 18:02:19 +0200906 goto out_error;
Emeric Brun107ca302010-01-04 16:16:05 +0100907 }
908
Willy Tarreau833cc792013-07-24 15:34:19 +0200909 /* Now process the converters if any. We have two supported syntaxes
910 * for the converters, which can be combined :
911 * - comma-delimited list of converters just after the keyword and args ;
912 * - one converter per keyword
913 * The combination allows to have each keyword being a comma-delimited
914 * series of converters.
915 *
916 * We want to process the former first, then the latter. For this we start
917 * from the beginning of the supposed place in the exiting conv chain, which
918 * starts at the last comma (endt).
919 */
920
921 while (1) {
Willy Tarreau12785782012-04-27 21:37:17 +0200922 struct sample_conv_expr *conv_expr;
Emeric Brun107ca302010-01-04 16:16:05 +0100923
Willy Tarreau833cc792013-07-24 15:34:19 +0200924 if (*endt == ')') /* skip last closing parenthesis */
925 endt++;
Emeric Brun107ca302010-01-04 16:16:05 +0100926
Willy Tarreau833cc792013-07-24 15:34:19 +0200927 if (*endt && *endt != ',') {
928 if (ckw)
Willy Tarreau97108e02016-11-25 07:33:24 +0100929 memprintf(err_msg, "missing comma after converter '%s'", ckw);
Willy Tarreau833cc792013-07-24 15:34:19 +0200930 else
Willy Tarreau975c1782013-12-12 23:16:54 +0100931 memprintf(err_msg, "missing comma after fetch keyword '%s'", fkw);
Emeric Brun107ca302010-01-04 16:16:05 +0100932 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200933 }
Emeric Brun107ca302010-01-04 16:16:05 +0100934
Willy Tarreau833cc792013-07-24 15:34:19 +0200935 while (*endt == ',') /* then trailing commas */
936 endt++;
937
Willy Tarreau97108e02016-11-25 07:33:24 +0100938 begw = endt; /* start of converter */
Willy Tarreau833cc792013-07-24 15:34:19 +0200939
940 if (!*begw) {
941 /* none ? skip to next string */
942 (*idx)++;
943 begw = str[*idx];
944 if (!begw || !*begw)
945 break;
946 }
947
948 for (endw = begw; *endw && *endw != '(' && *endw != ','; endw++);
949
950 free(ckw);
951 ckw = my_strndup(begw, endw - begw);
952
953 conv = find_sample_conv(begw, endw - begw);
954 if (!conv) {
955 /* we found an isolated keyword that we don't know, it's not ours */
956 if (begw == str[*idx])
957 break;
Willy Tarreau97108e02016-11-25 07:33:24 +0100958 memprintf(err_msg, "unknown converter '%s'", ckw);
Willy Tarreau833cc792013-07-24 15:34:19 +0200959 goto out_error;
960 }
Emeric Brun107ca302010-01-04 16:16:05 +0100961
Willy Tarreau833cc792013-07-24 15:34:19 +0200962 endt = endw;
963 if (*endt == '(') {
964 /* look for the end of this term */
965 while (*endt && *endt != ')')
966 endt++;
967 if (*endt != ')') {
Willy Tarreau97108e02016-11-25 07:33:24 +0100968 memprintf(err_msg, "syntax error: missing ')' after converter '%s'", ckw);
Willy Tarreau833cc792013-07-24 15:34:19 +0200969 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200970 }
Willy Tarreau833cc792013-07-24 15:34:19 +0200971 }
972
973 if (conv->in_type >= SMP_TYPES || conv->out_type >= SMP_TYPES) {
Willy Tarreau97108e02016-11-25 07:33:24 +0100974 memprintf(err_msg, "returns type of converter '%s' is unknown", ckw);
Emeric Brun107ca302010-01-04 16:16:05 +0100975 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200976 }
Emeric Brun107ca302010-01-04 16:16:05 +0100977
978 /* If impossible type conversion */
Willy Tarreau12785782012-04-27 21:37:17 +0200979 if (!sample_casts[prev_type][conv->in_type]) {
Willy Tarreau97108e02016-11-25 07:33:24 +0100980 memprintf(err_msg, "converter '%s' cannot be applied", ckw);
Emeric Brun107ca302010-01-04 16:16:05 +0100981 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200982 }
Emeric Brun107ca302010-01-04 16:16:05 +0100983
984 prev_type = conv->out_type;
Vincent Bernat02779b62016-04-03 13:48:43 +0200985 conv_expr = calloc(1, sizeof(*conv_expr));
Emeric Brun485479d2010-09-23 18:02:19 +0200986 if (!conv_expr)
987 goto out_error;
Emeric Brun107ca302010-01-04 16:16:05 +0100988
989 LIST_ADDQ(&(expr->conv_exprs), &(conv_expr->list));
990 conv_expr->conv = conv;
991
Willy Tarreau833cc792013-07-24 15:34:19 +0200992 if (endt != endw) {
Willy Tarreaub27c0d32012-04-20 16:04:47 +0200993 int err_arg;
Willy Tarreau21d68a62012-04-20 15:52:36 +0200994
Willy Tarreau9fcb9842012-04-20 14:45:49 +0200995 if (!conv->arg_mask) {
Willy Tarreau97108e02016-11-25 07:33:24 +0100996 memprintf(err_msg, "converter '%s' does not support any args", ckw);
Emeric Brun485479d2010-09-23 18:02:19 +0200997 goto out_error;
998 }
Willy Tarreau9e92d322010-01-26 17:58:06 +0100999
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001000 al->kw = expr->fetch->kw;
1001 al->conv = conv_expr->conv->kw;
Willy Tarreau975c1782013-12-12 23:16:54 +01001002 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 +01001003 memprintf(err_msg, "invalid arg %d in converter '%s' : %s", err_arg+1, ckw, *err_msg);
Willy Tarreau21d68a62012-04-20 15:52:36 +02001004 goto out_error;
1005 }
1006
Willy Tarreau2e845be2012-10-19 19:49:09 +02001007 if (!conv_expr->arg_p)
1008 conv_expr->arg_p = empty_arg_list;
1009
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +01001010 if (conv->val_args && !conv->val_args(conv_expr->arg_p, conv, file, line, err_msg)) {
Willy Tarreau97108e02016-11-25 07:33:24 +01001011 memprintf(err_msg, "invalid args in converter '%s' : %s", ckw, *err_msg);
Emeric Brun485479d2010-09-23 18:02:19 +02001012 goto out_error;
1013 }
1014 }
Willy Tarreau833cc792013-07-24 15:34:19 +02001015 else if (ARGM(conv->arg_mask)) {
Willy Tarreau97108e02016-11-25 07:33:24 +01001016 memprintf(err_msg, "missing args for converter '%s'", ckw);
Emeric Brun485479d2010-09-23 18:02:19 +02001017 goto out_error;
Emeric Brun107ca302010-01-04 16:16:05 +01001018 }
1019 }
Emeric Brun485479d2010-09-23 18:02:19 +02001020
Willy Tarreau833cc792013-07-24 15:34:19 +02001021 out:
1022 free(fkw);
1023 free(ckw);
Emeric Brun107ca302010-01-04 16:16:05 +01001024 return expr;
1025
1026out_error:
Willy Tarreau12785782012-04-27 21:37:17 +02001027 /* TODO: prune_sample_expr(expr); */
Willy Tarreau833cc792013-07-24 15:34:19 +02001028 expr = NULL;
1029 goto out;
Emeric Brun107ca302010-01-04 16:16:05 +01001030}
1031
1032/*
Willy Tarreau12785782012-04-27 21:37:17 +02001033 * Process a fetch + format conversion of defined by the sample expression <expr>
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001034 * on request or response considering the <opt> parameter.
Willy Tarreau12785782012-04-27 21:37:17 +02001035 * Returns a pointer on a typed sample structure containing the result or NULL if
1036 * sample is not found or when format conversion failed.
Emeric Brun107ca302010-01-04 16:16:05 +01001037 * If <p> is not null, function returns results in structure pointed by <p>.
Willy Tarreau12785782012-04-27 21:37:17 +02001038 * If <p> is null, functions returns a pointer on a static sample structure.
Willy Tarreaub8c8f1f2012-04-23 22:38:26 +02001039 *
1040 * Note: the fetch functions are required to properly set the return type. The
1041 * conversion functions must do so too. However the cast functions do not need
1042 * to since they're made to cast mutiple types according to what is required.
Willy Tarreau6bcb0a82014-07-30 08:56:35 +02001043 *
1044 * The caller may indicate in <opt> if it considers the result final or not.
1045 * The caller needs to check the SMP_F_MAY_CHANGE flag in p->flags to verify
1046 * if the result is stable or not, according to the following table :
1047 *
1048 * return MAY_CHANGE FINAL Meaning for the sample
1049 * NULL 0 * Not present and will never be (eg: header)
1050 * NULL 1 0 Not present yet, could change (eg: POST param)
1051 * NULL 1 1 Not present yet, will not change anymore
1052 * smp 0 * Present and will not change (eg: header)
1053 * smp 1 0 Present, may change (eg: request length)
1054 * smp 1 1 Present, last known value (eg: request length)
Emeric Brun107ca302010-01-04 16:16:05 +01001055 */
Willy Tarreau192252e2015-04-04 01:47:55 +02001056struct sample *sample_process(struct proxy *px, struct session *sess,
1057 struct stream *strm, unsigned int opt,
Willy Tarreau12785782012-04-27 21:37:17 +02001058 struct sample_expr *expr, struct sample *p)
Emeric Brun107ca302010-01-04 16:16:05 +01001059{
Willy Tarreau12785782012-04-27 21:37:17 +02001060 struct sample_conv_expr *conv_expr;
Emeric Brun107ca302010-01-04 16:16:05 +01001061
Willy Tarreau18387e22013-07-25 12:02:38 +02001062 if (p == NULL) {
Willy Tarreaub4a88f02012-04-23 21:35:11 +02001063 p = &temp_smp;
Willy Tarreau6c616e02014-06-25 16:56:41 +02001064 memset(p, 0, sizeof(*p));
Willy Tarreau18387e22013-07-25 12:02:38 +02001065 }
Emeric Brun107ca302010-01-04 16:16:05 +01001066
Willy Tarreau1777ea62016-03-10 16:15:46 +01001067 smp_set_owner(p, px, sess, strm, opt);
Thierry FOURNIER0786d052015-05-11 15:42:45 +02001068 if (!expr->fetch->process(expr->arg_p, p, expr->fetch->kw, expr->fetch->private))
Emeric Brun107ca302010-01-04 16:16:05 +01001069 return NULL;
1070
Emeric Brun107ca302010-01-04 16:16:05 +01001071 list_for_each_entry(conv_expr, &expr->conv_exprs, list) {
Willy Tarreau12e50112012-04-25 17:21:49 +02001072 /* we want to ensure that p->type can be casted into
1073 * conv_expr->conv->in_type. We have 3 possibilities :
1074 * - NULL => not castable.
1075 * - c_none => nothing to do (let's optimize it)
1076 * - other => apply cast and prepare to fail
1077 */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001078 if (!sample_casts[p->data.type][conv_expr->conv->in_type])
Willy Tarreau12e50112012-04-25 17:21:49 +02001079 return NULL;
1080
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001081 if (sample_casts[p->data.type][conv_expr->conv->in_type] != c_none &&
1082 !sample_casts[p->data.type][conv_expr->conv->in_type](p))
Emeric Brun107ca302010-01-04 16:16:05 +01001083 return NULL;
1084
Willy Tarreau12e50112012-04-25 17:21:49 +02001085 /* OK cast succeeded */
1086
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001087 if (!conv_expr->conv->process(conv_expr->arg_p, p, conv_expr->conv->private))
Emeric Brun107ca302010-01-04 16:16:05 +01001088 return NULL;
Emeric Brun107ca302010-01-04 16:16:05 +01001089 }
1090 return p;
1091}
1092
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001093/*
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001094 * Resolve all remaining arguments in proxy <p>. Returns the number of
1095 * errors or 0 if everything is fine.
1096 */
1097int smp_resolve_args(struct proxy *p)
1098{
1099 struct arg_list *cur, *bak;
1100 const char *ctx, *where;
1101 const char *conv_ctx, *conv_pre, *conv_pos;
1102 struct userlist *ul;
Willy Tarreau46947782015-01-19 19:00:58 +01001103 struct my_regex *reg;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001104 struct arg *arg;
1105 int cfgerr = 0;
Willy Tarreau46947782015-01-19 19:00:58 +01001106 int rflags;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001107
1108 list_for_each_entry_safe(cur, bak, &p->conf.args.list, list) {
1109 struct proxy *px;
1110 struct server *srv;
1111 char *pname, *sname;
Willy Tarreau46947782015-01-19 19:00:58 +01001112 char *err;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001113
1114 arg = cur->arg;
1115
1116 /* prepare output messages */
1117 conv_pre = conv_pos = conv_ctx = "";
1118 if (cur->conv) {
1119 conv_ctx = cur->conv;
1120 conv_pre = "conversion keyword '";
1121 conv_pos = "' for ";
1122 }
1123
1124 where = "in";
1125 ctx = "sample fetch keyword";
1126 switch (cur->ctx) {
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001127 case ARGC_STK: where = "in stick rule in"; break;
1128 case ARGC_TRK: where = "in tracking rule in"; break;
1129 case ARGC_LOG: where = "in log-format string in"; break;
1130 case ARGC_LOGSD: where = "in log-format-sd string in"; break;
1131 case ARGC_HRQ: where = "in http-request header format string in"; break;
1132 case ARGC_HRS: where = "in http-response header format string in"; break;
1133 case ARGC_UIF: where = "in unique-id-format string in"; break;
1134 case ARGC_RDR: where = "in redirect format string in"; break;
1135 case ARGC_CAP: where = "in capture rule in"; break;
1136 case ARGC_ACL: ctx = "ACL keyword"; break;
1137 case ARGC_SRV: where = "in server directive in"; break;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001138 case ARGC_SPOE: where = "in spoe-message directive in"; break;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001139 }
1140
1141 /* set a few default settings */
1142 px = p;
1143 pname = p->id;
1144
1145 switch (arg->type) {
1146 case ARGT_SRV:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001147 if (!arg->data.str.data) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001148 ha_alert("parsing [%s:%d] : missing server name in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1149 cur->file, cur->line,
1150 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001151 cfgerr++;
1152 continue;
1153 }
1154
1155 /* we support two formats : "bck/srv" and "srv" */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001156 sname = strrchr(arg->data.str.area, '/');
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001157
1158 if (sname) {
1159 *sname++ = '\0';
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001160 pname = arg->data.str.area;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001161
Willy Tarreau9e0bb102015-05-26 11:24:42 +02001162 px = proxy_be_by_name(pname);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001163 if (!px) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001164 ha_alert("parsing [%s:%d] : unable to find proxy '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1165 cur->file, cur->line, pname,
1166 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001167 cfgerr++;
1168 break;
1169 }
1170 }
1171 else
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001172 sname = arg->data.str.area;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001173
1174 srv = findserver(px, sname);
1175 if (!srv) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001176 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",
1177 cur->file, cur->line, sname, pname,
1178 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001179 cfgerr++;
1180 break;
1181 }
1182
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001183 free(arg->data.str.area);
1184 arg->data.str.area = NULL;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001185 arg->unresolved = 0;
1186 arg->data.srv = srv;
1187 break;
1188
1189 case ARGT_FE:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001190 if (arg->data.str.data) {
1191 pname = arg->data.str.area;
Willy Tarreau9e0bb102015-05-26 11:24:42 +02001192 px = proxy_fe_by_name(pname);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001193 }
1194
1195 if (!px) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001196 ha_alert("parsing [%s:%d] : unable to find frontend '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1197 cur->file, cur->line, pname,
1198 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001199 cfgerr++;
1200 break;
1201 }
1202
1203 if (!(px->cap & PR_CAP_FE)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001204 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",
1205 cur->file, cur->line, pname,
1206 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001207 cfgerr++;
1208 break;
1209 }
1210
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001211 free(arg->data.str.area);
1212 arg->data.str.area = NULL;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001213 arg->unresolved = 0;
1214 arg->data.prx = px;
1215 break;
1216
1217 case ARGT_BE:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001218 if (arg->data.str.data) {
1219 pname = arg->data.str.area;
Willy Tarreau9e0bb102015-05-26 11:24:42 +02001220 px = proxy_be_by_name(pname);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001221 }
1222
1223 if (!px) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001224 ha_alert("parsing [%s:%d] : unable to find backend '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1225 cur->file, cur->line, pname,
1226 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001227 cfgerr++;
1228 break;
1229 }
1230
1231 if (!(px->cap & PR_CAP_BE)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001232 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",
1233 cur->file, cur->line, pname,
1234 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001235 cfgerr++;
1236 break;
1237 }
1238
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001239 free(arg->data.str.area);
1240 arg->data.str.area = NULL;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001241 arg->unresolved = 0;
1242 arg->data.prx = px;
1243 break;
1244
1245 case ARGT_TAB:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001246 if (arg->data.str.data) {
1247 pname = arg->data.str.area;
Willy Tarreaue2dc1fa2015-05-26 12:08:07 +02001248 px = proxy_tbl_by_name(pname);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001249 }
1250
1251 if (!px) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001252 ha_alert("parsing [%s:%d] : unable to find table '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1253 cur->file, cur->line, pname,
1254 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001255 cfgerr++;
1256 break;
1257 }
1258
1259 if (!px->table.size) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001260 ha_alert("parsing [%s:%d] : no table in proxy '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1261 cur->file, cur->line, pname,
1262 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001263 cfgerr++;
1264 break;
1265 }
1266
Willy Tarreau151e1ca2019-02-05 11:38:38 +01001267 if (p->bind_proc & ~px->bind_proc) {
1268 ha_alert("parsing [%s:%d] : stick-table '%s' not present on all processes covered by proxy '%s'.\n",
1269 cur->file, cur->line, px->id, p->id);
1270 return 0;
1271 }
1272
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001273 free(arg->data.str.area);
1274 arg->data.str.area = NULL;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001275 arg->unresolved = 0;
1276 arg->data.prx = px;
1277 break;
1278
1279 case ARGT_USR:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001280 if (!arg->data.str.data) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001281 ha_alert("parsing [%s:%d] : missing userlist name in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1282 cur->file, cur->line,
1283 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001284 cfgerr++;
1285 break;
1286 }
1287
1288 if (p->uri_auth && p->uri_auth->userlist &&
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001289 !strcmp(p->uri_auth->userlist->name, arg->data.str.area))
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001290 ul = p->uri_auth->userlist;
1291 else
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001292 ul = auth_find_userlist(arg->data.str.area);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001293
1294 if (!ul) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001295 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 +02001296 cur->file, cur->line,
1297 arg->data.str.area,
Christopher Faulet767a84b2017-11-24 16:50:31 +01001298 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001299 cfgerr++;
1300 break;
1301 }
1302
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001303 free(arg->data.str.area);
1304 arg->data.str.area = NULL;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001305 arg->unresolved = 0;
1306 arg->data.usr = ul;
1307 break;
Willy Tarreau46947782015-01-19 19:00:58 +01001308
1309 case ARGT_REG:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001310 if (!arg->data.str.data) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001311 ha_alert("parsing [%s:%d] : missing 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 reg = calloc(1, sizeof(*reg));
1319 if (!reg) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001320 ha_alert("parsing [%s:%d] : not enough memory to build regex in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1321 cur->file, cur->line,
1322 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreau46947782015-01-19 19:00:58 +01001323 cfgerr++;
1324 continue;
1325 }
1326
1327 rflags = 0;
1328 rflags |= (arg->type_flags & ARGF_REG_ICASE) ? REG_ICASE : 0;
1329 err = NULL;
1330
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001331 if (!regex_comp(arg->data.str.area, reg, !(rflags & REG_ICASE), 1 /* capture substr */, &err)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001332 ha_alert("parsing [%s:%d] : error in regex '%s' in arg %d of %s%s%s%s '%s' %s proxy '%s' : %s.\n",
1333 cur->file, cur->line,
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001334 arg->data.str.area,
Christopher Faulet767a84b2017-11-24 16:50:31 +01001335 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id, err);
Willy Tarreau46947782015-01-19 19:00:58 +01001336 cfgerr++;
1337 continue;
1338 }
1339
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001340 free(arg->data.str.area);
1341 arg->data.str.area = NULL;
Willy Tarreau46947782015-01-19 19:00:58 +01001342 arg->unresolved = 0;
1343 arg->data.reg = reg;
1344 break;
1345
1346
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001347 }
1348
1349 LIST_DEL(&cur->list);
1350 free(cur);
1351 } /* end of args processing */
1352
1353 return cfgerr;
1354}
1355
1356/*
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001357 * Process a fetch + format conversion as defined by the sample expression
1358 * <expr> on request or response considering the <opt> parameter. The output is
Adis Nezirovic79beb242015-07-06 15:41:02 +02001359 * not explicitly set to <smp_type>, but shall be compatible with it as
1360 * specified by 'sample_casts' table. If a stable sample can be fetched, or an
1361 * unstable one when <opt> contains SMP_OPT_FINAL, the sample is converted and
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001362 * returned without the SMP_F_MAY_CHANGE flag. If an unstable sample is found
1363 * and <opt> does not contain SMP_OPT_FINAL, then the sample is returned as-is
1364 * with its SMP_F_MAY_CHANGE flag so that the caller can check it and decide to
1365 * take actions (eg: wait longer). If a sample could not be found or could not
Willy Tarreau6bcb0a82014-07-30 08:56:35 +02001366 * be converted, NULL is returned. The caller MUST NOT use the sample if the
1367 * SMP_F_MAY_CHANGE flag is present, as it is used only as a hint that there is
1368 * still hope to get it after waiting longer, and is not converted to string.
1369 * The possible output combinations are the following :
1370 *
1371 * return MAY_CHANGE FINAL Meaning for the sample
1372 * NULL * * Not present and will never be (eg: header)
1373 * smp 0 * Final value converted (eg: header)
1374 * smp 1 0 Not present yet, may appear later (eg: header)
1375 * smp 1 1 never happens (either flag is cleared on output)
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001376 */
Adis Nezirovic79beb242015-07-06 15:41:02 +02001377struct sample *sample_fetch_as_type(struct proxy *px, struct session *sess,
Willy Tarreau192252e2015-04-04 01:47:55 +02001378 struct stream *strm, unsigned int opt,
Adis Nezirovic79beb242015-07-06 15:41:02 +02001379 struct sample_expr *expr, int smp_type)
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001380{
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001381 struct sample *smp = &temp_smp;
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001382
Willy Tarreau6c616e02014-06-25 16:56:41 +02001383 memset(smp, 0, sizeof(*smp));
1384
Willy Tarreau192252e2015-04-04 01:47:55 +02001385 if (!sample_process(px, sess, strm, opt, expr, smp)) {
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001386 if ((smp->flags & SMP_F_MAY_CHANGE) && !(opt & SMP_OPT_FINAL))
1387 return smp;
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001388 return NULL;
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001389 }
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001390
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001391 if (!sample_casts[smp->data.type][smp_type])
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001392 return NULL;
1393
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001394 if (!sample_casts[smp->data.type][smp_type](smp))
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001395 return NULL;
1396
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001397 smp->flags &= ~SMP_F_MAY_CHANGE;
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001398 return smp;
1399}
1400
Christopher Faulet476e5d02016-10-26 11:34:47 +02001401static void release_sample_arg(struct arg *p)
1402{
1403 struct arg *p_back = p;
1404
1405 if (!p)
1406 return;
1407
1408 while (p->type != ARGT_STOP) {
1409 if (p->type == ARGT_STR || p->unresolved) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001410 free(p->data.str.area);
1411 p->data.str.area = NULL;
Christopher Faulet476e5d02016-10-26 11:34:47 +02001412 p->unresolved = 0;
1413 }
1414 else if (p->type == ARGT_REG) {
1415 if (p->data.reg) {
1416 regex_free(p->data.reg);
1417 free(p->data.reg);
1418 p->data.reg = NULL;
1419 }
1420 }
1421 p++;
1422 }
1423
1424 if (p_back != empty_arg_list)
1425 free(p_back);
1426}
1427
1428void release_sample_expr(struct sample_expr *expr)
1429{
1430 struct sample_conv_expr *conv_expr, *conv_exprb;
1431
1432 if (!expr)
1433 return;
1434
1435 list_for_each_entry_safe(conv_expr, conv_exprb, &expr->conv_exprs, list)
1436 release_sample_arg(conv_expr->arg_p);
1437 release_sample_arg(expr->arg_p);
1438 free(expr);
1439}
1440
Emeric Brun107ca302010-01-04 16:16:05 +01001441/*****************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +02001442/* Sample format convert functions */
Willy Tarreaub8c8f1f2012-04-23 22:38:26 +02001443/* These functions set the data type on return. */
Emeric Brun107ca302010-01-04 16:16:05 +01001444/*****************************************************************/
1445
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001446#ifdef DEBUG_EXPR
1447static int sample_conv_debug(const struct arg *arg_p, struct sample *smp, void *private)
1448{
1449 int i;
1450 struct sample tmp;
1451
1452 if (!(global.mode & MODE_QUIET) || (global.mode & (MODE_VERBOSE | MODE_STARTING))) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001453 fprintf(stderr, "[debug converter] type: %s ", smp_to_type[smp->data.type]);
1454 if (!sample_casts[smp->data.type][SMP_T_STR]) {
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001455 fprintf(stderr, "(undisplayable)");
1456 } else {
1457
1458 /* Copy sample fetch. This put the sample as const, the
1459 * cast will copy data if a transformation is required.
1460 */
1461 memcpy(&tmp, smp, sizeof(struct sample));
1462 tmp.flags = SMP_F_CONST;
1463
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001464 if (!sample_casts[smp->data.type][SMP_T_STR](&tmp))
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001465 fprintf(stderr, "(undisplayable)");
1466
1467 else {
1468 /* Display the displayable chars*. */
1469 fprintf(stderr, "<");
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001470 for (i = 0; i < tmp.data.u.str.data; i++) {
1471 if (isprint(tmp.data.u.str.area[i]))
1472 fputc(tmp.data.u.str.area[i],
1473 stderr);
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001474 else
1475 fputc('.', stderr);
1476 }
1477 }
1478 fprintf(stderr, ">\n");
1479 }
1480 }
1481 return 1;
1482}
1483#endif
1484
Holger Just1bfc24b2017-05-06 00:56:53 +02001485static int sample_conv_base642bin(const struct arg *arg_p, struct sample *smp, void *private)
1486{
Willy Tarreau83061a82018-07-13 11:56:34 +02001487 struct buffer *trash = get_trash_chunk();
Holger Just1bfc24b2017-05-06 00:56:53 +02001488 int bin_len;
1489
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001490 trash->data = 0;
1491 bin_len = base64dec(smp->data.u.str.area, smp->data.u.str.data,
1492 trash->area, trash->size);
Holger Just1bfc24b2017-05-06 00:56:53 +02001493 if (bin_len < 0)
1494 return 0;
1495
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001496 trash->data = bin_len;
Holger Just1bfc24b2017-05-06 00:56:53 +02001497 smp->data.u.str = *trash;
1498 smp->data.type = SMP_T_BIN;
1499 smp->flags &= ~SMP_F_CONST;
1500 return 1;
1501}
1502
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001503static int sample_conv_bin2base64(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brun53d1a982014-04-30 18:21:37 +02001504{
Willy Tarreau83061a82018-07-13 11:56:34 +02001505 struct buffer *trash = get_trash_chunk();
Emeric Brun53d1a982014-04-30 18:21:37 +02001506 int b64_len;
1507
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001508 trash->data = 0;
1509 b64_len = a2base64(smp->data.u.str.area, smp->data.u.str.data,
1510 trash->area, trash->size);
Emeric Brun53d1a982014-04-30 18:21:37 +02001511 if (b64_len < 0)
1512 return 0;
1513
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001514 trash->data = b64_len;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001515 smp->data.u.str = *trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001516 smp->data.type = SMP_T_STR;
Emeric Brun53d1a982014-04-30 18:21:37 +02001517 smp->flags &= ~SMP_F_CONST;
1518 return 1;
1519}
1520
Dragan Dosen6e5a9ca2017-10-24 09:18:23 +02001521static int sample_conv_sha1(const struct arg *arg_p, struct sample *smp, void *private)
1522{
1523 blk_SHA_CTX ctx;
Willy Tarreau83061a82018-07-13 11:56:34 +02001524 struct buffer *trash = get_trash_chunk();
Dragan Dosen6e5a9ca2017-10-24 09:18:23 +02001525
1526 memset(&ctx, 0, sizeof(ctx));
1527
1528 blk_SHA1_Init(&ctx);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001529 blk_SHA1_Update(&ctx, smp->data.u.str.area, smp->data.u.str.data);
1530 blk_SHA1_Final((unsigned char *) trash->area, &ctx);
Dragan Dosen6e5a9ca2017-10-24 09:18:23 +02001531
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001532 trash->data = 20;
Dragan Dosen6e5a9ca2017-10-24 09:18:23 +02001533 smp->data.u.str = *trash;
1534 smp->data.type = SMP_T_BIN;
1535 smp->flags &= ~SMP_F_CONST;
1536 return 1;
1537}
1538
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001539static int sample_conv_bin2hex(const struct arg *arg_p, struct sample *smp, void *private)
Thierry FOURNIER2f49d6d2014-03-12 15:01:52 +01001540{
Willy Tarreau83061a82018-07-13 11:56:34 +02001541 struct buffer *trash = get_trash_chunk();
Thierry FOURNIER2f49d6d2014-03-12 15:01:52 +01001542 unsigned char c;
1543 int ptr = 0;
1544
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001545 trash->data = 0;
1546 while (ptr < smp->data.u.str.data && trash->data <= trash->size - 2) {
1547 c = smp->data.u.str.area[ptr++];
1548 trash->area[trash->data++] = hextab[(c >> 4) & 0xF];
1549 trash->area[trash->data++] = hextab[c & 0xF];
Thierry FOURNIER2f49d6d2014-03-12 15:01:52 +01001550 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001551 smp->data.u.str = *trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001552 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01001553 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIER2f49d6d2014-03-12 15:01:52 +01001554 return 1;
1555}
1556
Dragan Dosen3f957b22017-10-24 09:27:34 +02001557static int sample_conv_hex2int(const struct arg *arg_p, struct sample *smp, void *private)
1558{
1559 long long int n = 0;
1560 int i, c;
1561
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001562 for (i = 0; i < smp->data.u.str.data; i++) {
1563 if ((c = hex2i(smp->data.u.str.area[i])) < 0)
Dragan Dosen3f957b22017-10-24 09:27:34 +02001564 return 0;
1565 n = (n << 4) + c;
1566 }
1567
1568 smp->data.u.sint = n;
1569 smp->data.type = SMP_T_SINT;
1570 smp->flags &= ~SMP_F_CONST;
1571 return 1;
1572}
1573
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001574/* hashes the binary input into a 32-bit unsigned int */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001575static int sample_conv_djb2(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001576{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001577 smp->data.u.sint = hash_djb2(smp->data.u.str.area,
1578 smp->data.u.str.data);
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001579 if (arg_p && arg_p->data.sint)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001580 smp->data.u.sint = full_hash(smp->data.u.sint);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001581 smp->data.type = SMP_T_SINT;
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001582 return 1;
1583}
1584
Willy Tarreau60a2ee72017-12-15 07:13:48 +01001585static int sample_conv_length(const struct arg *arg_p, struct sample *smp, void *private)
Etienne Carriereed0d24e2017-12-13 13:41:34 +01001586{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001587 int i = smp->data.u.str.data;
Etienne Carriereed0d24e2017-12-13 13:41:34 +01001588 smp->data.u.sint = i;
1589 smp->data.type = SMP_T_SINT;
1590 return 1;
1591}
1592
1593
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001594static int sample_conv_str2lower(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brun107ca302010-01-04 16:16:05 +01001595{
1596 int i;
1597
Willy Tarreauf0645dc2016-08-09 14:29:38 +02001598 if (!smp_make_rw(smp))
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01001599 return 0;
1600
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001601 for (i = 0; i < smp->data.u.str.data; i++) {
1602 if ((smp->data.u.str.area[i] >= 'A') && (smp->data.u.str.area[i] <= 'Z'))
1603 smp->data.u.str.area[i] += 'a' - 'A';
Emeric Brun107ca302010-01-04 16:16:05 +01001604 }
1605 return 1;
1606}
1607
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001608static int sample_conv_str2upper(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brun107ca302010-01-04 16:16:05 +01001609{
1610 int i;
1611
Willy Tarreauf0645dc2016-08-09 14:29:38 +02001612 if (!smp_make_rw(smp))
Emeric Brun485479d2010-09-23 18:02:19 +02001613 return 0;
1614
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001615 for (i = 0; i < smp->data.u.str.data; i++) {
1616 if ((smp->data.u.str.area[i] >= 'a') && (smp->data.u.str.area[i] <= 'z'))
1617 smp->data.u.str.area[i] += 'A' - 'a';
Emeric Brun107ca302010-01-04 16:16:05 +01001618 }
1619 return 1;
1620}
1621
Tim Duesterhus1478aa72018-01-25 16:24:51 +01001622/* takes the IPv4 mask in args[0] and an optional IPv6 mask in args[1] */
1623static int sample_conv_ipmask(const struct arg *args, struct sample *smp, void *private)
Willy Tarreaud31d6eb2010-01-26 18:01:41 +01001624{
Tim Duesterhus1478aa72018-01-25 16:24:51 +01001625 /* Attempt to convert to IPv4 to apply the correct mask. */
1626 c_ipv62ip(smp);
1627
1628 if (smp->data.type == SMP_T_IPV4) {
1629 smp->data.u.ipv4.s_addr &= args[0].data.ipv4.s_addr;
1630 smp->data.type = SMP_T_IPV4;
1631 }
1632 else if (smp->data.type == SMP_T_IPV6) {
1633 /* IPv6 cannot be converted without an IPv6 mask. */
1634 if (args[1].type != ARGT_IPV6)
1635 return 0;
1636
1637 *(uint32_t*)&smp->data.u.ipv6.s6_addr[0] &= *(uint32_t*)&args[1].data.ipv6.s6_addr[0];
1638 *(uint32_t*)&smp->data.u.ipv6.s6_addr[4] &= *(uint32_t*)&args[1].data.ipv6.s6_addr[4];
1639 *(uint32_t*)&smp->data.u.ipv6.s6_addr[8] &= *(uint32_t*)&args[1].data.ipv6.s6_addr[8];
1640 *(uint32_t*)&smp->data.u.ipv6.s6_addr[12] &= *(uint32_t*)&args[1].data.ipv6.s6_addr[12];
1641 smp->data.type = SMP_T_IPV6;
1642 }
1643
Willy Tarreaud31d6eb2010-01-26 18:01:41 +01001644 return 1;
1645}
1646
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001647/* takes an UINT value on input supposed to represent the time since EPOCH,
1648 * adds an optional offset found in args[1] and emits a string representing
1649 * the local time in the format specified in args[1] using strftime().
1650 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001651static int sample_conv_ltime(const struct arg *args, struct sample *smp, void *private)
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001652{
Willy Tarreau83061a82018-07-13 11:56:34 +02001653 struct buffer *temp;
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001654 /* With high numbers, the date returned can be negative, the 55 bits mask prevent this. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001655 time_t curr_date = smp->data.u.sint & 0x007fffffffffffffLL;
Thierry FOURNIERfac9ccf2015-07-08 00:15:20 +02001656 struct tm *tm;
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001657
1658 /* add offset */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001659 if (args[1].type == ARGT_SINT)
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001660 curr_date += args[1].data.sint;
1661
Thierry FOURNIERfac9ccf2015-07-08 00:15:20 +02001662 tm = localtime(&curr_date);
1663 if (!tm)
1664 return 0;
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001665 temp = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001666 temp->data = strftime(temp->area, temp->size, args[0].data.str.area,
1667 tm);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001668 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001669 smp->data.type = SMP_T_STR;
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001670 return 1;
1671}
1672
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001673/* hashes the binary input into a 32-bit unsigned int */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001674static int sample_conv_sdbm(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001675{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001676 smp->data.u.sint = hash_sdbm(smp->data.u.str.area,
1677 smp->data.u.str.data);
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001678 if (arg_p && arg_p->data.sint)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001679 smp->data.u.sint = full_hash(smp->data.u.sint);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001680 smp->data.type = SMP_T_SINT;
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001681 return 1;
1682}
1683
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001684/* takes an UINT value on input supposed to represent the time since EPOCH,
1685 * adds an optional offset found in args[1] and emits a string representing
1686 * the UTC date in the format specified in args[1] using strftime().
1687 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001688static int sample_conv_utime(const struct arg *args, struct sample *smp, void *private)
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001689{
Willy Tarreau83061a82018-07-13 11:56:34 +02001690 struct buffer *temp;
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001691 /* With high numbers, the date returned can be negative, the 55 bits mask prevent this. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001692 time_t curr_date = smp->data.u.sint & 0x007fffffffffffffLL;
Thierry FOURNIERfac9ccf2015-07-08 00:15:20 +02001693 struct tm *tm;
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001694
1695 /* add offset */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001696 if (args[1].type == ARGT_SINT)
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001697 curr_date += args[1].data.sint;
1698
Thierry FOURNIERfac9ccf2015-07-08 00:15:20 +02001699 tm = gmtime(&curr_date);
1700 if (!tm)
1701 return 0;
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001702 temp = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001703 temp->data = strftime(temp->area, temp->size, args[0].data.str.area,
1704 tm);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001705 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001706 smp->data.type = SMP_T_STR;
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001707 return 1;
1708}
1709
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001710/* hashes the binary input into a 32-bit unsigned int */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001711static int sample_conv_wt6(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001712{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001713 smp->data.u.sint = hash_wt6(smp->data.u.str.area,
1714 smp->data.u.str.data);
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001715 if (arg_p && arg_p->data.sint)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001716 smp->data.u.sint = full_hash(smp->data.u.sint);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001717 smp->data.type = SMP_T_SINT;
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001718 return 1;
1719}
1720
Thierry FOURNIER01e09742016-12-26 11:46:11 +01001721/* hashes the binary input into a 32-bit unsigned int using xxh.
1722 * The seed of the hash defaults to 0 but can be changd in argument 1.
1723 */
1724static int sample_conv_xxh32(const struct arg *arg_p, struct sample *smp, void *private)
1725{
1726 unsigned int seed;
1727
1728 if (arg_p && arg_p->data.sint)
1729 seed = arg_p->data.sint;
1730 else
1731 seed = 0;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001732 smp->data.u.sint = XXH32(smp->data.u.str.area, smp->data.u.str.data,
1733 seed);
Thierry FOURNIER01e09742016-12-26 11:46:11 +01001734 smp->data.type = SMP_T_SINT;
1735 return 1;
1736}
1737
1738/* hashes the binary input into a 64-bit unsigned int using xxh.
1739 * In fact, the function returns a 64 bit unsigned, but the sample
1740 * storage of haproxy only proposes 64-bits signed, so the value is
1741 * cast as signed. This cast doesn't impact the hash repartition.
1742 * The seed of the hash defaults to 0 but can be changd in argument 1.
1743 */
1744static int sample_conv_xxh64(const struct arg *arg_p, struct sample *smp, void *private)
1745{
1746 unsigned long long int seed;
1747
1748 if (arg_p && arg_p->data.sint)
1749 seed = (unsigned long long int)arg_p->data.sint;
1750 else
1751 seed = 0;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001752 smp->data.u.sint = (long long int)XXH64(smp->data.u.str.area,
1753 smp->data.u.str.data, seed);
Thierry FOURNIER01e09742016-12-26 11:46:11 +01001754 smp->data.type = SMP_T_SINT;
1755 return 1;
1756}
1757
Willy Tarreau80599772015-01-20 19:35:24 +01001758/* hashes the binary input into a 32-bit unsigned int */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001759static int sample_conv_crc32(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau80599772015-01-20 19:35:24 +01001760{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001761 smp->data.u.sint = hash_crc32(smp->data.u.str.area,
1762 smp->data.u.str.data);
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001763 if (arg_p && arg_p->data.sint)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001764 smp->data.u.sint = full_hash(smp->data.u.sint);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001765 smp->data.type = SMP_T_SINT;
Willy Tarreau80599772015-01-20 19:35:24 +01001766 return 1;
1767}
1768
Emmanuel Hocdet50791a72018-03-21 11:19:01 +01001769/* hashes the binary input into crc32c (RFC4960, Appendix B [8].) */
1770static int sample_conv_crc32c(const struct arg *arg_p, struct sample *smp, void *private)
1771{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001772 smp->data.u.sint = hash_crc32c(smp->data.u.str.area,
1773 smp->data.u.str.data);
Emmanuel Hocdet50791a72018-03-21 11:19:01 +01001774 if (arg_p && arg_p->data.sint)
1775 smp->data.u.sint = full_hash(smp->data.u.sint);
1776 smp->data.type = SMP_T_SINT;
1777 return 1;
1778}
1779
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001780/* This function escape special json characters. The returned string can be
1781 * safely set between two '"' and used as json string. The json string is
1782 * defined like this:
1783 *
1784 * any Unicode character except '"' or '\' or control character
1785 * \", \\, \/, \b, \f, \n, \r, \t, \u + four-hex-digits
1786 *
1787 * The enum input_type contain all the allowed mode for decoding the input
1788 * string.
1789 */
1790enum input_type {
1791 IT_ASCII = 0,
1792 IT_UTF8,
1793 IT_UTF8S,
1794 IT_UTF8P,
1795 IT_UTF8PS,
1796};
1797static int sample_conv_json_check(struct arg *arg, struct sample_conv *conv,
1798 const char *file, int line, char **err)
1799{
1800 if (!arg) {
1801 memprintf(err, "Unexpected empty arg list");
1802 return 0;
1803 }
1804
1805 if (arg->type != ARGT_STR) {
1806 memprintf(err, "Unexpected arg type");
1807 return 0;
1808 }
1809
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001810 if (strcmp(arg->data.str.area, "") == 0) {
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001811 arg->type = ARGT_SINT;
1812 arg->data.sint = IT_ASCII;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001813 return 1;
1814 }
1815
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001816 else if (strcmp(arg->data.str.area, "ascii") == 0) {
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001817 arg->type = ARGT_SINT;
1818 arg->data.sint = IT_ASCII;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001819 return 1;
1820 }
1821
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001822 else if (strcmp(arg->data.str.area, "utf8") == 0) {
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001823 arg->type = ARGT_SINT;
1824 arg->data.sint = IT_UTF8;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001825 return 1;
1826 }
1827
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001828 else if (strcmp(arg->data.str.area, "utf8s") == 0) {
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001829 arg->type = ARGT_SINT;
1830 arg->data.sint = IT_UTF8S;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001831 return 1;
1832 }
1833
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001834 else if (strcmp(arg->data.str.area, "utf8p") == 0) {
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001835 arg->type = ARGT_SINT;
1836 arg->data.sint = IT_UTF8P;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001837 return 1;
1838 }
1839
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001840 else if (strcmp(arg->data.str.area, "utf8ps") == 0) {
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001841 arg->type = ARGT_SINT;
1842 arg->data.sint = IT_UTF8PS;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001843 return 1;
1844 }
1845
Thierry FOURNIER / OZON.IOa69c9122016-11-23 01:13:57 +01001846 memprintf(err, "Unexpected input code type. "
1847 "Allowed value are 'ascii', 'utf8', 'utf8s', 'utf8p' and 'utf8ps'");
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001848 return 0;
1849}
1850
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001851static int sample_conv_json(const struct arg *arg_p, struct sample *smp, void *private)
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001852{
Willy Tarreau83061a82018-07-13 11:56:34 +02001853 struct buffer *temp;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001854 char _str[7]; /* \u + 4 hex digit + null char for sprintf. */
1855 const char *str;
1856 int len;
1857 enum input_type input_type = IT_ASCII;
1858 unsigned int c;
1859 unsigned int ret;
1860 char *p;
1861
1862 if (arg_p)
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001863 input_type = arg_p->data.sint;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001864
1865 temp = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001866 temp->data = 0;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001867
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001868 p = smp->data.u.str.area;
1869 while (p < smp->data.u.str.area + smp->data.u.str.data) {
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001870
1871 if (input_type == IT_ASCII) {
1872 /* Read input as ASCII. */
1873 c = *(unsigned char *)p;
1874 p++;
1875 }
1876 else {
1877 /* Read input as UTF8. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001878 ret = utf8_next(p,
1879 smp->data.u.str.data - ( p - smp->data.u.str.area),
1880 &c);
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001881 p += utf8_return_length(ret);
1882
1883 if (input_type == IT_UTF8 && utf8_return_code(ret) != UTF8_CODE_OK)
1884 return 0;
1885 if (input_type == IT_UTF8S && utf8_return_code(ret) != UTF8_CODE_OK)
1886 continue;
1887 if (input_type == IT_UTF8P && utf8_return_code(ret) & (UTF8_CODE_INVRANGE|UTF8_CODE_BADSEQ))
1888 return 0;
1889 if (input_type == IT_UTF8PS && utf8_return_code(ret) & (UTF8_CODE_INVRANGE|UTF8_CODE_BADSEQ))
1890 continue;
1891
1892 /* Check too big values. */
1893 if ((unsigned int)c > 0xffff) {
1894 if (input_type == IT_UTF8 || input_type == IT_UTF8P)
1895 return 0;
1896 continue;
1897 }
1898 }
1899
1900 /* Convert character. */
1901 if (c == '"') {
1902 len = 2;
1903 str = "\\\"";
1904 }
1905 else if (c == '\\') {
1906 len = 2;
1907 str = "\\\\";
1908 }
1909 else if (c == '/') {
1910 len = 2;
1911 str = "\\/";
1912 }
1913 else if (c == '\b') {
1914 len = 2;
1915 str = "\\b";
1916 }
1917 else if (c == '\f') {
1918 len = 2;
1919 str = "\\f";
1920 }
1921 else if (c == '\r') {
1922 len = 2;
1923 str = "\\r";
1924 }
1925 else if (c == '\n') {
1926 len = 2;
1927 str = "\\n";
1928 }
1929 else if (c == '\t') {
1930 len = 2;
1931 str = "\\t";
1932 }
1933 else if (c > 0xff || !isprint(c)) {
1934 /* isprint generate a segfault if c is too big. The man says that
1935 * c must have the value of an unsigned char or EOF.
1936 */
1937 len = 6;
1938 _str[0] = '\\';
1939 _str[1] = 'u';
1940 snprintf(&_str[2], 5, "%04x", (unsigned short)c);
1941 str = _str;
1942 }
1943 else {
1944 len = 1;
1945 str = (char *)&c;
1946 }
1947
1948 /* Check length */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001949 if (temp->data + len > temp->size)
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001950 return 0;
1951
1952 /* Copy string. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001953 memcpy(temp->area + temp->data, str, len);
1954 temp->data += len;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001955 }
1956
1957 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001958 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001959 smp->data.type = SMP_T_STR;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001960
1961 return 1;
1962}
1963
Emeric Brun54c4ac82014-11-03 15:32:43 +01001964/* This sample function is designed to extract some bytes from an input buffer.
1965 * First arg is the offset.
1966 * Optional second arg is the length to truncate */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001967static int sample_conv_bytes(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brun54c4ac82014-11-03 15:32:43 +01001968{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001969 if (smp->data.u.str.data <= arg_p[0].data.sint) {
1970 smp->data.u.str.data = 0;
Emeric Brun54c4ac82014-11-03 15:32:43 +01001971 return 1;
1972 }
1973
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001974 if (smp->data.u.str.size)
1975 smp->data.u.str.size -= arg_p[0].data.sint;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001976 smp->data.u.str.data -= arg_p[0].data.sint;
1977 smp->data.u.str.area += arg_p[0].data.sint;
Emeric Brun54c4ac82014-11-03 15:32:43 +01001978
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001979 if ((arg_p[1].type == ARGT_SINT) && (arg_p[1].data.sint < smp->data.u.str.data))
1980 smp->data.u.str.data = arg_p[1].data.sint;
Emeric Brun54c4ac82014-11-03 15:32:43 +01001981
1982 return 1;
1983}
1984
Emeric Brunf399b0d2014-11-03 17:07:03 +01001985static int sample_conv_field_check(struct arg *args, struct sample_conv *conv,
1986 const char *file, int line, char **err)
1987{
1988 struct arg *arg = args;
1989
1990 if (!arg) {
1991 memprintf(err, "Unexpected empty arg list");
1992 return 0;
1993 }
1994
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001995 if (arg->type != ARGT_SINT) {
Emeric Brunf399b0d2014-11-03 17:07:03 +01001996 memprintf(err, "Unexpected arg type");
1997 return 0;
1998 }
1999
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002000 if (!arg->data.sint) {
Emeric Brunf399b0d2014-11-03 17:07:03 +01002001 memprintf(err, "Unexpected value 0 for index");
2002 return 0;
2003 }
2004
2005 arg++;
2006
2007 if (arg->type != ARGT_STR) {
2008 memprintf(err, "Unexpected arg type");
2009 return 0;
2010 }
2011
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002012 if (!arg->data.str.data) {
Emeric Brunf399b0d2014-11-03 17:07:03 +01002013 memprintf(err, "Empty separators list");
2014 return 0;
2015 }
2016
2017 return 1;
2018}
2019
2020/* This sample function is designed to a return selected part of a string (field).
2021 * First arg is the index of the field (start at 1)
2022 * Second arg is a char list of separators (type string)
2023 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002024static int sample_conv_field(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brunf399b0d2014-11-03 17:07:03 +01002025{
Marcin Deranek9631a282018-04-16 14:30:46 +02002026 int field;
Emeric Brunf399b0d2014-11-03 17:07:03 +01002027 char *start, *end;
2028 int i;
Marcin Deranek9631a282018-04-16 14:30:46 +02002029 int count = (arg_p[2].type == ARGT_SINT) ? arg_p[2].data.sint : 1;
Emeric Brunf399b0d2014-11-03 17:07:03 +01002030
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002031 if (!arg_p[0].data.sint)
Emeric Brunf399b0d2014-11-03 17:07:03 +01002032 return 0;
2033
Marcin Deranek9631a282018-04-16 14:30:46 +02002034 if (arg_p[0].data.sint < 0) {
2035 field = -1;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002036 end = start = smp->data.u.str.area + smp->data.u.str.data;
2037 while (start > smp->data.u.str.area) {
2038 for (i = 0 ; i < arg_p[1].data.str.data; i++) {
2039 if (*(start-1) == arg_p[1].data.str.area[i]) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002040 if (field == arg_p[0].data.sint) {
2041 if (count == 1)
2042 goto found;
2043 else if (count > 1)
2044 count--;
2045 } else {
2046 end = start-1;
2047 field--;
2048 }
2049 break;
2050 }
Emeric Brunf399b0d2014-11-03 17:07:03 +01002051 }
Marcin Deranek9631a282018-04-16 14:30:46 +02002052 start--;
Emeric Brunf399b0d2014-11-03 17:07:03 +01002053 }
Marcin Deranek9631a282018-04-16 14:30:46 +02002054 } else {
2055 field = 1;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002056 end = start = smp->data.u.str.area;
2057 while (end - smp->data.u.str.area < smp->data.u.str.data) {
2058 for (i = 0 ; i < arg_p[1].data.str.data; i++) {
2059 if (*end == arg_p[1].data.str.area[i]) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002060 if (field == arg_p[0].data.sint) {
2061 if (count == 1)
2062 goto found;
2063 else if (count > 1)
2064 count--;
2065 } else {
2066 start = end+1;
2067 field++;
2068 }
2069 break;
2070 }
2071 }
2072 end++;
2073 }
Emeric Brunf399b0d2014-11-03 17:07:03 +01002074 }
2075
2076 /* Field not found */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002077 if (field != arg_p[0].data.sint) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002078 smp->data.u.str.data = 0;
Emeric Brunf399b0d2014-11-03 17:07:03 +01002079 return 1;
2080 }
2081found:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002082 smp->data.u.str.data = end - start;
Emeric Brunf399b0d2014-11-03 17:07:03 +01002083 /* If ret string is len 0, no need to
2084 change pointers or to update size */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002085 if (!smp->data.u.str.data)
Emeric Brunf399b0d2014-11-03 17:07:03 +01002086 return 1;
2087
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002088 smp->data.u.str.area = start;
Emeric Brunf399b0d2014-11-03 17:07:03 +01002089
2090 /* Compute remaining size if needed
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002091 Note: smp->data.u.str.size cannot be set to 0 */
2092 if (smp->data.u.str.size)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002093 smp->data.u.str.size -= start - smp->data.u.str.area;
Emeric Brunf399b0d2014-11-03 17:07:03 +01002094
2095 return 1;
2096}
2097
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002098/* This sample function is designed to return a word from a string.
2099 * First arg is the index of the word (start at 1)
2100 * Second arg is a char list of words separators (type string)
2101 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002102static int sample_conv_word(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002103{
Marcin Deranek9631a282018-04-16 14:30:46 +02002104 int word;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002105 char *start, *end;
2106 int i, issep, inword;
Marcin Deranek9631a282018-04-16 14:30:46 +02002107 int count = (arg_p[2].type == ARGT_SINT) ? arg_p[2].data.sint : 1;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002108
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002109 if (!arg_p[0].data.sint)
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002110 return 0;
2111
2112 word = 0;
2113 inword = 0;
Marcin Deranek9631a282018-04-16 14:30:46 +02002114 if (arg_p[0].data.sint < 0) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002115 end = start = smp->data.u.str.area + smp->data.u.str.data;
2116 while (start > smp->data.u.str.area) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002117 issep = 0;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002118 for (i = 0 ; i < arg_p[1].data.str.data; i++) {
2119 if (*(start-1) == arg_p[1].data.str.area[i]) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002120 issep = 1;
2121 break;
2122 }
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002123 }
Marcin Deranek9631a282018-04-16 14:30:46 +02002124 if (!inword) {
2125 if (!issep) {
2126 if (word != arg_p[0].data.sint) {
2127 word--;
2128 end = start;
2129 }
2130 inword = 1;
2131 }
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002132 }
Marcin Deranek9631a282018-04-16 14:30:46 +02002133 else if (issep) {
Willy Tarreau9eb2a4a2018-04-19 10:33:28 +02002134 if (word == arg_p[0].data.sint) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002135 if (count == 1)
2136 goto found;
2137 else if (count > 1)
2138 count--;
Willy Tarreau9eb2a4a2018-04-19 10:33:28 +02002139 }
Marcin Deranek9631a282018-04-16 14:30:46 +02002140 inword = 0;
2141 }
2142 start--;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002143 }
Marcin Deranek9631a282018-04-16 14:30:46 +02002144 } else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002145 end = start = smp->data.u.str.area;
2146 while (end - smp->data.u.str.area < smp->data.u.str.data) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002147 issep = 0;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002148 for (i = 0 ; i < arg_p[1].data.str.data; i++) {
2149 if (*end == arg_p[1].data.str.area[i]) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002150 issep = 1;
2151 break;
2152 }
2153 }
2154 if (!inword) {
2155 if (!issep) {
2156 if (word != arg_p[0].data.sint) {
2157 word++;
2158 start = end;
2159 }
2160 inword = 1;
2161 }
2162 }
2163 else if (issep) {
Willy Tarreau9eb2a4a2018-04-19 10:33:28 +02002164 if (word == arg_p[0].data.sint) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002165 if (count == 1)
2166 goto found;
2167 else if (count > 1)
2168 count--;
Willy Tarreau9eb2a4a2018-04-19 10:33:28 +02002169 }
Marcin Deranek9631a282018-04-16 14:30:46 +02002170 inword = 0;
2171 }
2172 end++;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002173 }
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002174 }
2175
2176 /* Field not found */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002177 if (word != arg_p[0].data.sint) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002178 smp->data.u.str.data = 0;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002179 return 1;
2180 }
2181found:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002182 smp->data.u.str.data = end - start;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002183 /* If ret string is len 0, no need to
2184 change pointers or to update size */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002185 if (!smp->data.u.str.data)
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002186 return 1;
2187
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002188 smp->data.u.str.area = start;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002189
2190 /* Compute remaining size if needed
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002191 Note: smp->data.u.str.size cannot be set to 0 */
2192 if (smp->data.u.str.size)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002193 smp->data.u.str.size -= start - smp->data.u.str.area;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002194
2195 return 1;
2196}
2197
Willy Tarreau7eda8492015-01-20 19:47:06 +01002198static int sample_conv_regsub_check(struct arg *args, struct sample_conv *conv,
2199 const char *file, int line, char **err)
2200{
2201 struct arg *arg = args;
2202 char *p;
2203 int len;
2204
2205 /* arg0 is a regex, it uses type_flag for ICASE and global match */
2206 arg[0].type_flags = 0;
2207
2208 if (arg[2].type != ARGT_STR)
2209 return 1;
2210
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002211 p = arg[2].data.str.area;
2212 len = arg[2].data.str.data;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002213 while (len) {
2214 if (*p == 'i') {
2215 arg[0].type_flags |= ARGF_REG_ICASE;
2216 }
2217 else if (*p == 'g') {
2218 arg[0].type_flags |= ARGF_REG_GLOB;
2219 }
2220 else {
2221 memprintf(err, "invalid regex flag '%c', only 'i' and 'g' are supported", *p);
2222 return 0;
2223 }
2224 p++;
2225 len--;
2226 }
2227 return 1;
2228}
2229
2230/* This sample function is designed to do the equivalent of s/match/replace/ on
2231 * the input string. It applies a regex and restarts from the last matched
2232 * location until nothing matches anymore. First arg is the regex to apply to
2233 * the input string, second arg is the replacement expression.
2234 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002235static int sample_conv_regsub(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau7eda8492015-01-20 19:47:06 +01002236{
2237 char *start, *end;
2238 struct my_regex *reg = arg_p[0].data.reg;
2239 regmatch_t pmatch[MAX_MATCH];
Willy Tarreau83061a82018-07-13 11:56:34 +02002240 struct buffer *trash = get_trash_chunk();
Willy Tarreau7eda8492015-01-20 19:47:06 +01002241 int flag, max;
2242 int found;
2243
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002244 start = smp->data.u.str.area;
2245 end = start + smp->data.u.str.data;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002246
2247 flag = 0;
2248 while (1) {
2249 /* check for last round which is used to copy remaining parts
2250 * when not running in global replacement mode.
2251 */
2252 found = 0;
2253 if ((arg_p[0].type_flags & ARGF_REG_GLOB) || !(flag & REG_NOTBOL)) {
2254 /* Note: we can have start == end on empty strings or at the end */
2255 found = regex_exec_match2(reg, start, end - start, MAX_MATCH, pmatch, flag);
2256 }
2257
2258 if (!found)
2259 pmatch[0].rm_so = end - start;
2260
2261 /* copy the heading non-matching part (which may also be the tail if nothing matches) */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002262 max = trash->size - trash->data;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002263 if (max && pmatch[0].rm_so > 0) {
2264 if (max > pmatch[0].rm_so)
2265 max = pmatch[0].rm_so;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002266 memcpy(trash->area + trash->data, start, max);
2267 trash->data += max;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002268 }
2269
2270 if (!found)
2271 break;
2272
2273 /* replace the matching part */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002274 max = trash->size - trash->data;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002275 if (max) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002276 if (max > arg_p[1].data.str.data)
2277 max = arg_p[1].data.str.data;
2278 memcpy(trash->area + trash->data,
2279 arg_p[1].data.str.area, max);
2280 trash->data += max;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002281 }
2282
2283 /* stop here if we're done with this string */
2284 if (start >= end)
2285 break;
2286
2287 /* We have a special case for matches of length 0 (eg: "x*y*").
2288 * These ones are considered to match in front of a character,
2289 * so we have to copy that character and skip to the next one.
2290 */
2291 if (!pmatch[0].rm_eo) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002292 if (trash->data < trash->size)
2293 trash->area[trash->data++] = start[pmatch[0].rm_eo];
Willy Tarreau7eda8492015-01-20 19:47:06 +01002294 pmatch[0].rm_eo++;
2295 }
2296
2297 start += pmatch[0].rm_eo;
2298 flag |= REG_NOTBOL;
2299 }
2300
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002301 smp->data.u.str = *trash;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002302 return 1;
2303}
2304
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002305/* This function check an operator entry. It expects a string.
2306 * The string can be an integer or a variable name.
2307 */
2308static int check_operator(struct arg *args, struct sample_conv *conv,
2309 const char *file, int line, char **err)
2310{
2311 const char *str;
2312 const char *end;
2313
2314 /* Try to decode a variable. */
2315 if (vars_check_arg(&args[0], NULL))
2316 return 1;
2317
2318 /* Try to convert an integer */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002319 str = args[0].data.str.area;
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002320 end = str + strlen(str);
2321 args[0].data.sint = read_int64(&str, end);
2322 if (*str != '\0') {
2323 memprintf(err, "expects an integer or a variable name");
2324 return 0;
2325 }
2326 args[0].type = ARGT_SINT;
2327 return 1;
2328}
2329
Willy Tarreau6204cd92016-03-10 16:33:04 +01002330/* This function returns a sample struct filled with an arg content.
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002331 * If the arg contain an integer, the integer is returned in the
2332 * sample. If the arg contains a variable descriptor, it returns the
2333 * variable value.
2334 *
2335 * This function returns 0 if an error occurs, otherwise it returns 1.
2336 */
Willy Tarreau6204cd92016-03-10 16:33:04 +01002337static inline int sample_conv_var2smp(const struct arg *arg, struct sample *smp)
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002338{
2339 switch (arg->type) {
2340 case ARGT_SINT:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002341 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002342 smp->data.u.sint = arg->data.sint;
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002343 return 1;
2344 case ARGT_VAR:
Willy Tarreau6204cd92016-03-10 16:33:04 +01002345 if (!vars_get_by_desc(&arg->data.var, smp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002346 return 0;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002347 if (!sample_casts[smp->data.type][SMP_T_SINT])
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002348 return 0;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002349 if (!sample_casts[smp->data.type][SMP_T_SINT](smp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002350 return 0;
2351 return 1;
2352 default:
2353 return 0;
2354 }
2355}
2356
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002357/* Takes a SINT on input, applies a binary twos complement and returns the SINT
Willy Tarreau97707872015-01-27 15:12:13 +01002358 * result.
2359 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002360static int sample_conv_binary_cpl(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002361{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002362 smp->data.u.sint = ~smp->data.u.sint;
Willy Tarreau97707872015-01-27 15:12:13 +01002363 return 1;
2364}
2365
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002366/* Takes a SINT on input, applies a binary "and" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08002367 * arg_p or in the variable described in arg_p, and returns the SINT result.
Willy Tarreau97707872015-01-27 15:12:13 +01002368 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002369static int sample_conv_binary_and(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002370{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002371 struct sample tmp;
2372
Willy Tarreau7560dd42016-03-10 16:28:58 +01002373 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreau6204cd92016-03-10 16:33:04 +01002374 if (!sample_conv_var2smp(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002375 return 0;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002376 smp->data.u.sint &= tmp.data.u.sint;
Willy Tarreau97707872015-01-27 15:12:13 +01002377 return 1;
2378}
2379
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002380/* Takes a SINT on input, applies a binary "or" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08002381 * arg_p or in the variable described in arg_p, and returns the SINT result.
Willy Tarreau97707872015-01-27 15:12:13 +01002382 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002383static int sample_conv_binary_or(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002384{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002385 struct sample tmp;
2386
Willy Tarreau7560dd42016-03-10 16:28:58 +01002387 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreau6204cd92016-03-10 16:33:04 +01002388 if (!sample_conv_var2smp(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002389 return 0;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002390 smp->data.u.sint |= tmp.data.u.sint;
Willy Tarreau97707872015-01-27 15:12:13 +01002391 return 1;
2392}
2393
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002394/* Takes a SINT on input, applies a binary "xor" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08002395 * arg_p or in the variable described in arg_p, and returns the SINT result.
Willy Tarreau97707872015-01-27 15:12:13 +01002396 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002397static int sample_conv_binary_xor(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002398{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002399 struct sample tmp;
2400
Willy Tarreau7560dd42016-03-10 16:28:58 +01002401 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreau6204cd92016-03-10 16:33:04 +01002402 if (!sample_conv_var2smp(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002403 return 0;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002404 smp->data.u.sint ^= tmp.data.u.sint;
Willy Tarreau97707872015-01-27 15:12:13 +01002405 return 1;
2406}
2407
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002408static inline long long int arith_add(long long int a, long long int b)
2409{
2410 /* Prevent overflow and makes capped calculus.
2411 * We must ensure that the check calculus doesn't
2412 * exceed the signed 64 bits limits.
2413 *
2414 * +----------+----------+
2415 * | a<0 | a>=0 |
2416 * +------+----------+----------+
2417 * | b<0 | MIN-a>b | no check |
2418 * +------+----------+----------+
2419 * | b>=0 | no check | MAX-a<b |
2420 * +------+----------+----------+
2421 */
2422 if ((a ^ b) >= 0) {
2423 /* signs are differents. */
2424 if (a < 0) {
2425 if (LLONG_MIN - a > b)
2426 return LLONG_MIN;
2427 }
2428 if (LLONG_MAX - a < b)
2429 return LLONG_MAX;
2430 }
2431 return a + b;
2432}
2433
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002434/* Takes a SINT on input, applies an arithmetic "add" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08002435 * arg_p or in the variable described in arg_p, and returns the SINT result.
Willy Tarreau97707872015-01-27 15:12:13 +01002436 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002437static int sample_conv_arith_add(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002438{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002439 struct sample tmp;
2440
Willy Tarreau7560dd42016-03-10 16:28:58 +01002441 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreau6204cd92016-03-10 16:33:04 +01002442 if (!sample_conv_var2smp(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002443 return 0;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002444 smp->data.u.sint = arith_add(smp->data.u.sint, tmp.data.u.sint);
Willy Tarreau97707872015-01-27 15:12:13 +01002445 return 1;
2446}
2447
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002448/* Takes a SINT on input, applies an arithmetic "sub" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08002449 * arg_p or in the variable described in arg_p, and returns the SINT result.
Willy Tarreau97707872015-01-27 15:12:13 +01002450 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002451static int sample_conv_arith_sub(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002452 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002453{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002454 struct sample tmp;
2455
Willy Tarreau7560dd42016-03-10 16:28:58 +01002456 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreau6204cd92016-03-10 16:33:04 +01002457 if (!sample_conv_var2smp(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002458 return 0;
2459
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002460 /* We cannot represent -LLONG_MIN because abs(LLONG_MIN) is greater
2461 * than abs(LLONG_MAX). So, the following code use LLONG_MAX in place
2462 * of -LLONG_MIN and correct the result.
2463 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002464 if (tmp.data.u.sint == LLONG_MIN) {
2465 smp->data.u.sint = arith_add(smp->data.u.sint, LLONG_MAX);
2466 if (smp->data.u.sint < LLONG_MAX)
2467 smp->data.u.sint++;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002468 return 1;
2469 }
2470
Joseph Herlant757f5ad2018-11-15 12:14:56 -08002471 /* standard subtraction: we use the "add" function and negate
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002472 * the second operand.
2473 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002474 smp->data.u.sint = arith_add(smp->data.u.sint, -tmp.data.u.sint);
Willy Tarreau97707872015-01-27 15:12:13 +01002475 return 1;
2476}
2477
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002478/* Takes a SINT on input, applies an arithmetic "mul" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08002479 * arg_p or in the variable described in arg_p, and returns the SINT result.
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002480 * If the result makes an overflow, then the largest possible quantity is
2481 * returned.
Willy Tarreau97707872015-01-27 15:12:13 +01002482 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002483static int sample_conv_arith_mul(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002484 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002485{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002486 struct sample tmp;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002487 long long int c;
2488
Willy Tarreau7560dd42016-03-10 16:28:58 +01002489 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreau6204cd92016-03-10 16:33:04 +01002490 if (!sample_conv_var2smp(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002491 return 0;
2492
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002493 /* prevent divide by 0 during the check */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002494 if (!smp->data.u.sint || !tmp.data.u.sint) {
2495 smp->data.u.sint = 0;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002496 return 1;
2497 }
2498
2499 /* The multiply between LLONG_MIN and -1 returns a
2500 * "floting point exception".
2501 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002502 if (smp->data.u.sint == LLONG_MIN && tmp.data.u.sint == -1) {
2503 smp->data.u.sint = LLONG_MAX;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002504 return 1;
2505 }
2506
2507 /* execute standard multiplication. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002508 c = smp->data.u.sint * tmp.data.u.sint;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002509
2510 /* check for overflow and makes capped multiply. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002511 if (smp->data.u.sint != c / tmp.data.u.sint) {
2512 if ((smp->data.u.sint < 0) == (tmp.data.u.sint < 0)) {
2513 smp->data.u.sint = LLONG_MAX;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002514 return 1;
2515 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002516 smp->data.u.sint = LLONG_MIN;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002517 return 1;
2518 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002519 smp->data.u.sint = c;
Willy Tarreau97707872015-01-27 15:12:13 +01002520 return 1;
2521}
2522
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002523/* Takes a SINT on input, applies an arithmetic "div" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08002524 * arg_p or in the variable described in arg_p, and returns the SINT result.
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002525 * If arg_p makes the result overflow, then the largest possible quantity is
2526 * returned.
Willy Tarreau97707872015-01-27 15:12:13 +01002527 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002528static int sample_conv_arith_div(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002529 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002530{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002531 struct sample tmp;
2532
Willy Tarreau7560dd42016-03-10 16:28:58 +01002533 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreau6204cd92016-03-10 16:33:04 +01002534 if (!sample_conv_var2smp(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002535 return 0;
2536
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002537 if (tmp.data.u.sint) {
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002538 /* The divide between LLONG_MIN and -1 returns a
2539 * "floting point exception".
2540 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002541 if (smp->data.u.sint == LLONG_MIN && tmp.data.u.sint == -1) {
2542 smp->data.u.sint = LLONG_MAX;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002543 return 1;
2544 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002545 smp->data.u.sint /= tmp.data.u.sint;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002546 return 1;
2547 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002548 smp->data.u.sint = LLONG_MAX;
Willy Tarreau97707872015-01-27 15:12:13 +01002549 return 1;
2550}
2551
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002552/* Takes a SINT on input, applies an arithmetic "mod" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08002553 * arg_p or in the variable described in arg_p, and returns the SINT result.
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002554 * If arg_p makes the result overflow, then 0 is returned.
Willy Tarreau97707872015-01-27 15:12:13 +01002555 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002556static int sample_conv_arith_mod(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002557 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002558{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002559 struct sample tmp;
2560
Willy Tarreau7560dd42016-03-10 16:28:58 +01002561 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreau6204cd92016-03-10 16:33:04 +01002562 if (!sample_conv_var2smp(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002563 return 0;
2564
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002565 if (tmp.data.u.sint) {
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002566 /* The divide between LLONG_MIN and -1 returns a
2567 * "floting point exception".
2568 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002569 if (smp->data.u.sint == LLONG_MIN && tmp.data.u.sint == -1) {
2570 smp->data.u.sint = 0;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002571 return 1;
2572 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002573 smp->data.u.sint %= tmp.data.u.sint;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002574 return 1;
2575 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002576 smp->data.u.sint = 0;
Willy Tarreau97707872015-01-27 15:12:13 +01002577 return 1;
2578}
2579
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002580/* Takes an SINT on input, applies an arithmetic "neg" and returns the SINT
Willy Tarreau97707872015-01-27 15:12:13 +01002581 * result.
2582 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002583static int sample_conv_arith_neg(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002584 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002585{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002586 if (smp->data.u.sint == LLONG_MIN)
2587 smp->data.u.sint = LLONG_MAX;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002588 else
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002589 smp->data.u.sint = -smp->data.u.sint;
Willy Tarreau97707872015-01-27 15:12:13 +01002590 return 1;
2591}
2592
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002593/* Takes a SINT on input, returns true is the value is non-null, otherwise
Willy Tarreau97707872015-01-27 15:12:13 +01002594 * false. The output is a BOOL.
2595 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002596static int sample_conv_arith_bool(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002597 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002598{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002599 smp->data.u.sint = !!smp->data.u.sint;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002600 smp->data.type = SMP_T_BOOL;
Willy Tarreau97707872015-01-27 15:12:13 +01002601 return 1;
2602}
2603
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002604/* Takes a SINT on input, returns false is the value is non-null, otherwise
Willy Tarreau97707872015-01-27 15:12:13 +01002605 * truee. The output is a BOOL.
2606 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002607static int sample_conv_arith_not(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002608 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002609{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002610 smp->data.u.sint = !smp->data.u.sint;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002611 smp->data.type = SMP_T_BOOL;
Willy Tarreau97707872015-01-27 15:12:13 +01002612 return 1;
2613}
2614
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002615/* Takes a SINT on input, returns true is the value is odd, otherwise false.
Willy Tarreau97707872015-01-27 15:12:13 +01002616 * The output is a BOOL.
2617 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002618static int sample_conv_arith_odd(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002619 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002620{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002621 smp->data.u.sint = smp->data.u.sint & 1;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002622 smp->data.type = SMP_T_BOOL;
Willy Tarreau97707872015-01-27 15:12:13 +01002623 return 1;
2624}
2625
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002626/* Takes a SINT on input, returns true is the value is even, otherwise false.
Willy Tarreau97707872015-01-27 15:12:13 +01002627 * The output is a BOOL.
2628 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002629static int sample_conv_arith_even(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002630 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002631{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002632 smp->data.u.sint = !(smp->data.u.sint & 1);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002633 smp->data.type = SMP_T_BOOL;
Willy Tarreau97707872015-01-27 15:12:13 +01002634 return 1;
2635}
2636
Willy Tarreau280f42b2018-02-19 15:34:12 +01002637/* appends an optional const string, an optional variable contents and another
2638 * optional const string to an existing string.
2639 */
2640static int sample_conv_concat(const struct arg *arg_p, struct sample *smp, void *private)
2641{
Willy Tarreau83061a82018-07-13 11:56:34 +02002642 struct buffer *trash;
Willy Tarreau280f42b2018-02-19 15:34:12 +01002643 struct sample tmp;
2644 int max;
2645
2646 trash = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002647 trash->data = smp->data.u.str.data;
2648 if (trash->data > trash->size - 1)
2649 trash->data = trash->size - 1;
Willy Tarreau280f42b2018-02-19 15:34:12 +01002650
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002651 memcpy(trash->area, smp->data.u.str.area, trash->data);
2652 trash->area[trash->data] = 0;
Willy Tarreau280f42b2018-02-19 15:34:12 +01002653
2654 /* append first string */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002655 max = arg_p[0].data.str.data;
2656 if (max > trash->size - 1 - trash->data)
2657 max = trash->size - 1 - trash->data;
Willy Tarreau280f42b2018-02-19 15:34:12 +01002658
2659 if (max) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002660 memcpy(trash->area + trash->data, arg_p[0].data.str.area, max);
2661 trash->data += max;
2662 trash->area[trash->data] = 0;
Willy Tarreau280f42b2018-02-19 15:34:12 +01002663 }
2664
2665 /* append second string (variable) if it's found and we can turn it
2666 * into a string.
2667 */
2668 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
2669 if (arg_p[1].type == ARGT_VAR && vars_get_by_desc(&arg_p[1].data.var, &tmp) &&
2670 (sample_casts[tmp.data.type][SMP_T_STR] == c_none ||
2671 sample_casts[tmp.data.type][SMP_T_STR](&tmp))) {
2672
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002673 max = tmp.data.u.str.data;
2674 if (max > trash->size - 1 - trash->data)
2675 max = trash->size - 1 - trash->data;
Willy Tarreau280f42b2018-02-19 15:34:12 +01002676
2677 if (max) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002678 memcpy(trash->area + trash->data, tmp.data.u.str.area,
2679 max);
2680 trash->data += max;
2681 trash->area[trash->data] = 0;
Willy Tarreau280f42b2018-02-19 15:34:12 +01002682 }
2683 }
2684
2685 /* append third string */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002686 max = arg_p[2].data.str.data;
2687 if (max > trash->size - 1 - trash->data)
2688 max = trash->size - 1 - trash->data;
Willy Tarreau280f42b2018-02-19 15:34:12 +01002689
2690 if (max) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002691 memcpy(trash->area + trash->data, arg_p[2].data.str.area, max);
2692 trash->data += max;
2693 trash->area[trash->data] = 0;
Willy Tarreau280f42b2018-02-19 15:34:12 +01002694 }
2695
2696 smp->data.u.str = *trash;
2697 smp->data.type = SMP_T_STR;
2698 return 1;
2699}
2700
2701/* This function checks the "concat" converter's arguments and extracts the
2702 * variable name and its scope.
2703 */
2704static int smp_check_concat(struct arg *args, struct sample_conv *conv,
2705 const char *file, int line, char **err)
2706{
2707 /* Try to decode a variable. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002708 if (args[1].data.str.data > 0 && !vars_check_arg(&args[1], NULL)) {
2709 memprintf(err, "failed to register variable name '%s'",
2710 args[1].data.str.area);
Willy Tarreau280f42b2018-02-19 15:34:12 +01002711 return 0;
2712 }
2713 return 1;
2714}
2715
Tim Duesterhusca097c12018-04-27 21:18:45 +02002716/* compares string with a variable containing a string. Return value
2717 * is compatible with strcmp(3)'s return value.
2718 */
2719static int sample_conv_strcmp(const struct arg *arg_p, struct sample *smp, void *private)
2720{
2721 struct sample tmp;
2722 int max, result;
2723
2724 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
2725 if (arg_p[0].type != ARGT_VAR)
2726 return 0;
2727 if (!vars_get_by_desc(&arg_p[0].data.var, &tmp))
2728 return 0;
2729 if (!sample_casts[tmp.data.type][SMP_T_STR](&tmp))
2730 return 0;
2731
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002732 max = MIN(smp->data.u.str.data, tmp.data.u.str.data);
2733 result = strncmp(smp->data.u.str.area, tmp.data.u.str.area, max);
Tim Duesterhusca097c12018-04-27 21:18:45 +02002734 if (result == 0) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002735 if (smp->data.u.str.data != tmp.data.u.str.data) {
2736 if (smp->data.u.str.data < tmp.data.u.str.data) {
Tim Duesterhusca097c12018-04-27 21:18:45 +02002737 result = -1;
2738 }
2739 else {
2740 result = 1;
2741 }
2742 }
2743 }
2744
2745 smp->data.u.sint = result;
2746 smp->data.type = SMP_T_SINT;
2747 return 1;
2748}
2749
2750/* This function checks the "strcmp" converter's arguments and extracts the
2751 * variable name and its scope.
2752 */
2753static int smp_check_strcmp(struct arg *args, struct sample_conv *conv,
2754 const char *file, int line, char **err)
2755{
2756 /* Try to decode a variable. */
2757 if (vars_check_arg(&args[0], NULL))
2758 return 1;
2759
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002760 memprintf(err, "failed to register variable name '%s'",
2761 args[0].data.str.area);
Tim Duesterhusca097c12018-04-27 21:18:45 +02002762 return 0;
2763}
2764
Willy Tarreau5b8ad222013-07-25 12:17:57 +02002765/************************************************************************/
2766/* All supported sample fetch functions must be declared here */
2767/************************************************************************/
2768
2769/* force TRUE to be returned at the fetch level */
2770static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002771smp_fetch_true(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau5b8ad222013-07-25 12:17:57 +02002772{
Willy Tarreau280f42b2018-02-19 15:34:12 +01002773 if (!smp_make_rw(smp))
2774 return 0;
2775
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 = 1;
Willy Tarreau5b8ad222013-07-25 12:17:57 +02002778 return 1;
2779}
2780
2781/* force FALSE to be returned at the fetch level */
2782static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002783smp_fetch_false(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau5b8ad222013-07-25 12:17:57 +02002784{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002785 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002786 smp->data.u.sint = 0;
Willy Tarreau5b8ad222013-07-25 12:17:57 +02002787 return 1;
2788}
2789
2790/* retrieve environment variable $1 as a string */
2791static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002792smp_fetch_env(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau5b8ad222013-07-25 12:17:57 +02002793{
2794 char *env;
2795
2796 if (!args || args[0].type != ARGT_STR)
2797 return 0;
2798
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002799 env = getenv(args[0].data.str.area);
Willy Tarreau5b8ad222013-07-25 12:17:57 +02002800 if (!env)
2801 return 0;
2802
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002803 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01002804 smp->flags = SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002805 smp->data.u.str.area = env;
2806 smp->data.u.str.data = strlen(env);
Willy Tarreau5b8ad222013-07-25 12:17:57 +02002807 return 1;
2808}
2809
Willy Tarreau6236d3a2013-07-25 14:28:25 +02002810/* retrieve the current local date in epoch time, and applies an optional offset
2811 * of args[0] seconds.
2812 */
2813static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002814smp_fetch_date(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau6236d3a2013-07-25 14:28:25 +02002815{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002816 smp->data.u.sint = date.tv_sec;
Willy Tarreau6236d3a2013-07-25 14:28:25 +02002817
2818 /* add offset */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002819 if (args && args[0].type == ARGT_SINT)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002820 smp->data.u.sint += args[0].data.sint;
Willy Tarreau6236d3a2013-07-25 14:28:25 +02002821
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002822 smp->data.type = SMP_T_SINT;
Willy Tarreau6236d3a2013-07-25 14:28:25 +02002823 smp->flags |= SMP_F_VOL_TEST | SMP_F_MAY_CHANGE;
2824 return 1;
2825}
2826
Etienne Carrierea792a0a2018-01-17 13:43:24 +01002827/* retrieve the current microsecond part of the date */
2828static int
2829smp_fetch_date_us(const struct arg *args, struct sample *smp, const char *kw, void *private)
2830{
2831 smp->data.u.sint = date.tv_usec;
2832 smp->data.type = SMP_T_SINT;
2833 smp->flags |= SMP_F_VOL_TEST | SMP_F_MAY_CHANGE;
2834 return 1;
2835}
2836
2837
Nenad Merdanovic807a6e72017-03-12 22:00:00 +01002838/* returns the hostname */
2839static int
2840smp_fetch_hostname(const struct arg *args, struct sample *smp, const char *kw, void *private)
2841{
2842 smp->data.type = SMP_T_STR;
2843 smp->flags = SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002844 smp->data.u.str.area = hostname;
2845 smp->data.u.str.data = strlen(hostname);
Nenad Merdanovic807a6e72017-03-12 22:00:00 +01002846 return 1;
2847}
2848
Willy Tarreau0f30d262014-11-24 16:02:05 +01002849/* returns the number of processes */
2850static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002851smp_fetch_nbproc(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 = global.nbproc;
Willy Tarreau0f30d262014-11-24 16:02:05 +01002855 return 1;
2856}
2857
2858/* returns the number of the current process (between 1 and nbproc */
2859static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002860smp_fetch_proc(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau0f30d262014-11-24 16:02:05 +01002861{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002862 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002863 smp->data.u.sint = relative_pid;
Willy Tarreau0f30d262014-11-24 16:02:05 +01002864 return 1;
2865}
2866
Christopher Faulet34adb2a2017-11-21 21:45:38 +01002867/* returns the number of the current thread (between 1 and nbthread */
2868static int
2869smp_fetch_thread(const struct arg *args, struct sample *smp, const char *kw, void *private)
2870{
2871 smp->data.type = SMP_T_SINT;
2872 smp->data.u.sint = tid;
2873 return 1;
2874}
2875
Willy Tarreau84310e22014-02-14 11:59:04 +01002876/* generate a random 32-bit integer for whatever purpose, with an optional
2877 * range specified in argument.
2878 */
2879static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002880smp_fetch_rand(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau84310e22014-02-14 11:59:04 +01002881{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002882 smp->data.u.sint = random();
Willy Tarreau84310e22014-02-14 11:59:04 +01002883
2884 /* reduce if needed. Don't do a modulo, use all bits! */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002885 if (args && args[0].type == ARGT_SINT)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002886 smp->data.u.sint = (smp->data.u.sint * args[0].data.sint) / ((u64)RAND_MAX+1);
Willy Tarreau84310e22014-02-14 11:59:04 +01002887
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002888 smp->data.type = SMP_T_SINT;
Willy Tarreau84310e22014-02-14 11:59:04 +01002889 smp->flags |= SMP_F_VOL_TEST | SMP_F_MAY_CHANGE;
2890 return 1;
2891}
2892
Willy Tarreau0f30d262014-11-24 16:02:05 +01002893/* returns true if the current process is stopping */
2894static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002895smp_fetch_stopping(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau0f30d262014-11-24 16:02:05 +01002896{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002897 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002898 smp->data.u.sint = stopping;
Willy Tarreau0f30d262014-11-24 16:02:05 +01002899 return 1;
2900}
2901
Willy Tarreau70fe9442018-11-22 16:07:39 +01002902/* returns the number of calls of the current stream's process_stream() */
2903static int
2904smp_fetch_cpu_calls(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;
2908 return 1;
2909}
2910
2911/* returns the average number of nanoseconds spent processing the stream per call */
2912static int
2913smp_fetch_cpu_ns_avg(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->calls ? smp->strm->task->cpu_time / smp->strm->task->calls : 0;
2917 return 1;
2918}
2919
2920/* returns the total number of nanoseconds spent processing the stream */
2921static int
2922smp_fetch_cpu_ns_tot(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->cpu_time;
2926 return 1;
2927}
2928
2929/* returns the average number of nanoseconds per call spent waiting for other tasks to be processed */
2930static int
2931smp_fetch_lat_ns_avg(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->calls ? smp->strm->task->lat_time / smp->strm->task->calls : 0;
2935 return 1;
2936}
2937
2938/* returns the total number of nanoseconds per call spent waiting for other tasks to be processed */
2939static int
2940smp_fetch_lat_ns_tot(const struct arg *args, struct sample *smp, const char *kw, void *private)
2941{
2942 smp->data.type = SMP_T_SINT;
2943 smp->data.u.sint = smp->strm->task->lat_time;
2944 return 1;
2945}
2946
Thierry FOURNIERcc103292015-06-06 19:30:17 +02002947static int smp_fetch_const_str(const struct arg *args, struct sample *smp, const char *kw, void *private)
2948{
2949 smp->flags |= SMP_F_CONST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002950 smp->data.type = SMP_T_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002951 smp->data.u.str.area = args[0].data.str.area;
2952 smp->data.u.str.data = args[0].data.str.data;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02002953 return 1;
2954}
2955
2956static int smp_check_const_bool(struct arg *args, char **err)
2957{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002958 if (strcasecmp(args[0].data.str.area, "true") == 0 ||
2959 strcasecmp(args[0].data.str.area, "1") == 0) {
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002960 args[0].type = ARGT_SINT;
2961 args[0].data.sint = 1;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02002962 return 1;
2963 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002964 if (strcasecmp(args[0].data.str.area, "false") == 0 ||
2965 strcasecmp(args[0].data.str.area, "0") == 0) {
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002966 args[0].type = ARGT_SINT;
2967 args[0].data.sint = 0;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02002968 return 1;
2969 }
2970 memprintf(err, "Expects 'true', 'false', '0' or '1'");
2971 return 0;
2972}
2973
2974static int smp_fetch_const_bool(const struct arg *args, struct sample *smp, const char *kw, void *private)
2975{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002976 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002977 smp->data.u.sint = args[0].data.sint;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02002978 return 1;
2979}
2980
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002981static int smp_fetch_const_int(const struct arg *args, struct sample *smp, const char *kw, void *private)
Thierry FOURNIERcc103292015-06-06 19:30:17 +02002982{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002983 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002984 smp->data.u.sint = args[0].data.sint;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02002985 return 1;
2986}
2987
2988static int smp_fetch_const_ipv4(const struct arg *args, struct sample *smp, const char *kw, void *private)
2989{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002990 smp->data.type = SMP_T_IPV4;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002991 smp->data.u.ipv4 = args[0].data.ipv4;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02002992 return 1;
2993}
2994
2995static int smp_fetch_const_ipv6(const struct arg *args, struct sample *smp, const char *kw, void *private)
2996{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002997 smp->data.type = SMP_T_IPV6;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002998 smp->data.u.ipv6 = args[0].data.ipv6;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02002999 return 1;
3000}
3001
3002static int smp_check_const_bin(struct arg *args, char **err)
3003{
David Carlier64a16ab2016-04-08 10:37:02 +01003004 char *binstr = NULL;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003005 int binstrlen;
3006
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003007 if (!parse_binary(args[0].data.str.area, &binstr, &binstrlen, err))
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003008 return 0;
3009 args[0].type = ARGT_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003010 args[0].data.str.area = binstr;
3011 args[0].data.str.data = binstrlen;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003012 return 1;
3013}
3014
3015static int smp_fetch_const_bin(const struct arg *args, struct sample *smp, const char *kw, void *private)
3016{
3017 smp->flags |= SMP_F_CONST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003018 smp->data.type = SMP_T_BIN;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003019 smp->data.u.str.area = args[0].data.str.area;
3020 smp->data.u.str.data = args[0].data.str.data;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003021 return 1;
3022}
3023
3024static int smp_check_const_meth(struct arg *args, char **err)
3025{
3026 enum http_meth_t meth;
3027 int i;
3028
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003029 meth = find_http_meth(args[0].data.str.area, args[0].data.str.data);
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003030 if (meth != HTTP_METH_OTHER) {
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02003031 args[0].type = ARGT_SINT;
3032 args[0].data.sint = meth;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003033 } else {
3034 /* Check method avalaibility. A methos is a token defined as :
3035 * tchar = "!" / "#" / "$" / "%" / "&" / "'" / "*" / "+" / "-" / "." /
3036 * "^" / "_" / "`" / "|" / "~" / DIGIT / ALPHA
3037 * token = 1*tchar
3038 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003039 for (i = 0; i < args[0].data.str.data; i++) {
3040 if (!HTTP_IS_TOKEN(args[0].data.str.area[i])) {
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003041 memprintf(err, "expects valid method.");
3042 return 0;
3043 }
3044 }
3045 }
3046 return 1;
3047}
3048
3049static int smp_fetch_const_meth(const struct arg *args, struct sample *smp, const char *kw, void *private)
3050{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003051 smp->data.type = SMP_T_METH;
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02003052 if (args[0].type == ARGT_SINT) {
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003053 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003054 smp->data.u.meth.meth = args[0].data.sint;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003055 smp->data.u.meth.str.area = "";
3056 smp->data.u.meth.str.data = 0;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003057 } else {
3058 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003059 smp->data.u.meth.meth = HTTP_METH_OTHER;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003060 smp->data.u.meth.str.area = args[0].data.str.area;
3061 smp->data.u.meth.str.data = args[0].data.str.data;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003062 }
3063 return 1;
3064}
3065
Willy Tarreau5b8ad222013-07-25 12:17:57 +02003066/* Note: must not be declared <const> as its list will be overwritten.
3067 * Note: fetches that may return multiple types must be declared as the lowest
3068 * common denominator, the type that can be casted into all other ones. For
3069 * instance IPv4/IPv6 must be declared IPv4.
3070 */
3071static struct sample_fetch_kw_list smp_kws = {ILH, {
3072 { "always_false", smp_fetch_false, 0, NULL, SMP_T_BOOL, SMP_USE_INTRN },
3073 { "always_true", smp_fetch_true, 0, NULL, SMP_T_BOOL, SMP_USE_INTRN },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01003074 { "env", smp_fetch_env, ARG1(1,STR), NULL, SMP_T_STR, SMP_USE_INTRN },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02003075 { "date", smp_fetch_date, ARG1(0,SINT), NULL, SMP_T_SINT, SMP_USE_INTRN },
Etienne Carrierea792a0a2018-01-17 13:43:24 +01003076 { "date_us", smp_fetch_date_us, 0, NULL, SMP_T_SINT, SMP_USE_INTRN },
Nenad Merdanovic807a6e72017-03-12 22:00:00 +01003077 { "hostname", smp_fetch_hostname, 0, NULL, SMP_T_STR, SMP_USE_INTRN },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02003078 { "nbproc", smp_fetch_nbproc,0, NULL, SMP_T_SINT, SMP_USE_INTRN },
3079 { "proc", smp_fetch_proc, 0, NULL, SMP_T_SINT, SMP_USE_INTRN },
Christopher Faulet34adb2a2017-11-21 21:45:38 +01003080 { "thread", smp_fetch_thread, 0, NULL, SMP_T_SINT, SMP_USE_INTRN },
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02003081 { "rand", smp_fetch_rand, ARG1(0,SINT), NULL, SMP_T_SINT, SMP_USE_INTRN },
Willy Tarreau0f30d262014-11-24 16:02:05 +01003082 { "stopping", smp_fetch_stopping, 0, NULL, SMP_T_BOOL, SMP_USE_INTRN },
Willy Tarreau70fe9442018-11-22 16:07:39 +01003083 { "stopping", smp_fetch_stopping, 0, NULL, SMP_T_BOOL, SMP_USE_INTRN },
3084
3085 { "cpu_calls", smp_fetch_cpu_calls, 0, NULL, SMP_T_SINT, SMP_USE_INTRN },
3086 { "cpu_ns_avg", smp_fetch_cpu_ns_avg, 0, NULL, SMP_T_SINT, SMP_USE_INTRN },
3087 { "cpu_ns_tot", smp_fetch_cpu_ns_tot, 0, NULL, SMP_T_SINT, SMP_USE_INTRN },
3088 { "lat_ns_avg", smp_fetch_lat_ns_avg, 0, NULL, SMP_T_SINT, SMP_USE_INTRN },
3089 { "lat_ns_tot", smp_fetch_lat_ns_tot, 0, NULL, SMP_T_SINT, SMP_USE_INTRN },
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003090
3091 { "str", smp_fetch_const_str, ARG1(1,STR), NULL , SMP_T_STR, SMP_USE_INTRN },
3092 { "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 +02003093 { "int", smp_fetch_const_int, ARG1(1,SINT), NULL , SMP_T_SINT, SMP_USE_INTRN },
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003094 { "ipv4", smp_fetch_const_ipv4, ARG1(1,IPV4), NULL , SMP_T_IPV4, SMP_USE_INTRN },
3095 { "ipv6", smp_fetch_const_ipv6, ARG1(1,IPV6), NULL , SMP_T_IPV6, SMP_USE_INTRN },
3096 { "bin", smp_fetch_const_bin, ARG1(1,STR), smp_check_const_bin , SMP_T_BIN, SMP_USE_INTRN },
3097 { "meth", smp_fetch_const_meth, ARG1(1,STR), smp_check_const_meth, SMP_T_METH, SMP_USE_INTRN },
3098
Willy Tarreau5b8ad222013-07-25 12:17:57 +02003099 { /* END */ },
3100}};
3101
Willy Tarreau0108d902018-11-25 19:14:37 +01003102INITCALL1(STG_REGISTER, sample_register_fetches, &smp_kws);
3103
Emeric Brun107ca302010-01-04 16:16:05 +01003104/* Note: must not be declared <const> as its list will be overwritten */
Willy Tarreaudc13c112013-06-21 23:16:39 +02003105static struct sample_conv_kw_list sample_conv_kws = {ILH, {
Thierry FOURNIER9687c772015-05-07 15:46:29 +02003106#ifdef DEBUG_EXPR
3107 { "debug", sample_conv_debug, 0, NULL, SMP_T_ANY, SMP_T_ANY },
3108#endif
3109
Holger Just1bfc24b2017-05-06 00:56:53 +02003110 { "b64dec", sample_conv_base642bin,0, NULL, SMP_T_STR, SMP_T_BIN },
Emeric Brun53d1a982014-04-30 18:21:37 +02003111 { "base64", sample_conv_bin2base64,0, NULL, SMP_T_BIN, SMP_T_STR },
Willy Tarreau12785782012-04-27 21:37:17 +02003112 { "upper", sample_conv_str2upper, 0, NULL, SMP_T_STR, SMP_T_STR },
3113 { "lower", sample_conv_str2lower, 0, NULL, SMP_T_STR, SMP_T_STR },
Willy Tarreau60a2ee72017-12-15 07:13:48 +01003114 { "length", sample_conv_length, 0, NULL, SMP_T_STR, SMP_T_SINT },
Thierry FOURNIER2f49d6d2014-03-12 15:01:52 +01003115 { "hex", sample_conv_bin2hex, 0, NULL, SMP_T_BIN, SMP_T_STR },
Dragan Dosen3f957b22017-10-24 09:27:34 +02003116 { "hex2i", sample_conv_hex2int, 0, NULL, SMP_T_STR, SMP_T_SINT },
Tim Duesterhus1478aa72018-01-25 16:24:51 +01003117 { "ipmask", sample_conv_ipmask, ARG2(1,MSK4,MSK6), NULL, SMP_T_ADDR, SMP_T_IPV4 },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02003118 { "ltime", sample_conv_ltime, ARG2(1,STR,SINT), NULL, SMP_T_SINT, SMP_T_STR },
3119 { "utime", sample_conv_utime, ARG2(1,STR,SINT), NULL, SMP_T_SINT, SMP_T_STR },
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02003120 { "crc32", sample_conv_crc32, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
Emmanuel Hocdet50791a72018-03-21 11:19:01 +01003121 { "crc32c", sample_conv_crc32c, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02003122 { "djb2", sample_conv_djb2, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
3123 { "sdbm", sample_conv_sdbm, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
3124 { "wt6", sample_conv_wt6, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
Thierry FOURNIER01e09742016-12-26 11:46:11 +01003125 { "xxh32", sample_conv_xxh32, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
3126 { "xxh64", sample_conv_xxh64, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02003127 { "json", sample_conv_json, ARG1(1,STR), sample_conv_json_check, SMP_T_STR, SMP_T_STR },
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02003128 { "bytes", sample_conv_bytes, ARG2(1,SINT,SINT), NULL, SMP_T_BIN, SMP_T_BIN },
Marcin Deranek9631a282018-04-16 14:30:46 +02003129 { "field", sample_conv_field, ARG3(2,SINT,STR,SINT), sample_conv_field_check, SMP_T_STR, SMP_T_STR },
3130 { "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 +01003131 { "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 +02003132 { "sha1", sample_conv_sha1, 0, NULL, SMP_T_BIN, SMP_T_BIN },
Willy Tarreau280f42b2018-02-19 15:34:12 +01003133 { "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 +02003134 { "strcmp", sample_conv_strcmp, ARG1(1,STR), smp_check_strcmp, SMP_T_STR, SMP_T_SINT },
Willy Tarreau97707872015-01-27 15:12:13 +01003135
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02003136 { "and", sample_conv_binary_and, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
3137 { "or", sample_conv_binary_or, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
3138 { "xor", sample_conv_binary_xor, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
3139 { "cpl", sample_conv_binary_cpl, 0, NULL, SMP_T_SINT, SMP_T_SINT },
3140 { "bool", sample_conv_arith_bool, 0, NULL, SMP_T_SINT, SMP_T_BOOL },
3141 { "not", sample_conv_arith_not, 0, NULL, SMP_T_SINT, SMP_T_BOOL },
3142 { "odd", sample_conv_arith_odd, 0, NULL, SMP_T_SINT, SMP_T_BOOL },
3143 { "even", sample_conv_arith_even, 0, NULL, SMP_T_SINT, SMP_T_BOOL },
3144 { "add", sample_conv_arith_add, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
3145 { "sub", sample_conv_arith_sub, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
3146 { "mul", sample_conv_arith_mul, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
3147 { "div", sample_conv_arith_div, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
3148 { "mod", sample_conv_arith_mod, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
3149 { "neg", sample_conv_arith_neg, 0, NULL, SMP_T_SINT, SMP_T_SINT },
Willy Tarreau97707872015-01-27 15:12:13 +01003150
Willy Tarreau9fcb9842012-04-20 14:45:49 +02003151 { NULL, NULL, 0, 0, 0 },
Emeric Brun107ca302010-01-04 16:16:05 +01003152}};
3153
Willy Tarreau0108d902018-11-25 19:14:37 +01003154INITCALL1(STG_REGISTER, sample_register_convs, &sample_conv_kws);