blob: 138390d9db8dc9f486323dd0ba8225f4ec72d2c0 [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 Tarreaucd3b0942012-04-27 21:52:18 +020024#include <common/standard.h>
Willy Tarreaua4312fa2013-04-02 16:34:32 +020025#include <common/uri_auth.h>
Emeric Brun53d1a982014-04-30 18:21:37 +020026#include <common/base64.h>
Willy Tarreaucd3b0942012-04-27 21:52:18 +020027
Willy Tarreau9fcb9842012-04-20 14:45:49 +020028#include <proto/arg.h>
Willy Tarreaua4312fa2013-04-02 16:34:32 +020029#include <proto/auth.h>
30#include <proto/log.h>
31#include <proto/proxy.h>
Willy Tarreaucd3b0942012-04-27 21:52:18 +020032#include <proto/sample.h>
Willy Tarreaua4312fa2013-04-02 16:34:32 +020033#include <proto/stick_table.h>
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +020034#include <proto/vars.h>
Emeric Brun107ca302010-01-04 16:16:05 +010035
Dragan Dosen6e5a9ca2017-10-24 09:18:23 +020036#include <import/sha1.h>
Thierry FOURNIER01e09742016-12-26 11:46:11 +010037#include <import/xxhash.h>
38
Willy Tarreau1cf8f082014-02-07 12:14:54 +010039/* sample type names */
40const char *smp_to_type[SMP_TYPES] = {
Thierry FOURNIER9c627e82015-06-03 20:12:35 +020041 [SMP_T_ANY] = "any",
Willy Tarreau1cf8f082014-02-07 12:14:54 +010042 [SMP_T_BOOL] = "bool",
Willy Tarreau1cf8f082014-02-07 12:14:54 +010043 [SMP_T_SINT] = "sint",
44 [SMP_T_ADDR] = "addr",
45 [SMP_T_IPV4] = "ipv4",
46 [SMP_T_IPV6] = "ipv6",
47 [SMP_T_STR] = "str",
48 [SMP_T_BIN] = "bin",
Thierry FOURNIER4c2479e2015-06-03 20:12:04 +020049 [SMP_T_METH] = "meth",
Willy Tarreau1cf8f082014-02-07 12:14:54 +010050};
51
Willy Tarreau12785782012-04-27 21:37:17 +020052/* static sample used in sample_process() when <p> is NULL */
Emeric Brune5c918b2017-06-14 14:15:36 +020053static THREAD_LOCAL struct sample temp_smp;
Emeric Brun107ca302010-01-04 16:16:05 +010054
Willy Tarreau12785782012-04-27 21:37:17 +020055/* list head of all known sample fetch keywords */
56static struct sample_fetch_kw_list sample_fetches = {
57 .list = LIST_HEAD_INIT(sample_fetches.list)
Emeric Brun107ca302010-01-04 16:16:05 +010058};
59
Willy Tarreau12785782012-04-27 21:37:17 +020060/* list head of all known sample format conversion keywords */
61static struct sample_conv_kw_list sample_convs = {
62 .list = LIST_HEAD_INIT(sample_convs.list)
Emeric Brun107ca302010-01-04 16:16:05 +010063};
64
Willy Tarreau80aca902013-01-07 15:42:20 +010065const unsigned int fetch_cap[SMP_SRC_ENTRIES] = {
66 [SMP_SRC_INTRN] = (SMP_VAL_FE_CON_ACC | SMP_VAL_FE_SES_ACC | SMP_VAL_FE_REQ_CNT |
67 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
68 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
69 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
70 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
71 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
72 SMP_VAL_FE_LOG_END),
73
74 [SMP_SRC_LISTN] = (SMP_VAL_FE_CON_ACC | SMP_VAL_FE_SES_ACC | SMP_VAL_FE_REQ_CNT |
75 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
76 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
77 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
78 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
79 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
80 SMP_VAL_FE_LOG_END),
81
82 [SMP_SRC_FTEND] = (SMP_VAL_FE_CON_ACC | SMP_VAL_FE_SES_ACC | SMP_VAL_FE_REQ_CNT |
83 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
84 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
85 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
86 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
87 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
88 SMP_VAL_FE_LOG_END),
89
90 [SMP_SRC_L4CLI] = (SMP_VAL_FE_CON_ACC | SMP_VAL_FE_SES_ACC | SMP_VAL_FE_REQ_CNT |
91 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
92 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
93 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
94 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
95 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
96 SMP_VAL_FE_LOG_END),
97
98 [SMP_SRC_L5CLI] = (SMP_VAL___________ | SMP_VAL_FE_SES_ACC | SMP_VAL_FE_REQ_CNT |
99 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
100 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
101 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
102 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
103 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
104 SMP_VAL_FE_LOG_END),
105
106 [SMP_SRC_TRACK] = (SMP_VAL_FE_CON_ACC | SMP_VAL_FE_SES_ACC | SMP_VAL_FE_REQ_CNT |
107 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
108 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
109 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
110 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
111 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
112 SMP_VAL_FE_LOG_END),
113
114 [SMP_SRC_L6REQ] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_FE_REQ_CNT |
115 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
116 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
117 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL___________ |
118 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
119 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
120 SMP_VAL___________),
121
122 [SMP_SRC_HRQHV] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_FE_REQ_CNT |
123 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
124 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
125 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL___________ |
126 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
127 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
128 SMP_VAL___________),
129
130 [SMP_SRC_HRQHP] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_FE_REQ_CNT |
131 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
132 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
133 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
134 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
135 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
136 SMP_VAL_FE_LOG_END),
137
138 [SMP_SRC_HRQBO] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
139 SMP_VAL___________ | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
140 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
141 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL___________ |
142 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
143 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
144 SMP_VAL___________),
145
146 [SMP_SRC_BKEND] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
147 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
148 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
149 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
150 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
151 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
152 SMP_VAL_FE_LOG_END),
153
154 [SMP_SRC_SERVR] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
155 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
156 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
157 SMP_VAL___________ | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
158 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
159 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
160 SMP_VAL_FE_LOG_END),
161
162 [SMP_SRC_L4SRV] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
163 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
164 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
165 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_BE_RES_CNT |
166 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
167 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
168 SMP_VAL_FE_LOG_END),
169
170 [SMP_SRC_L5SRV] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
171 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
172 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
173 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_BE_RES_CNT |
174 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
175 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
176 SMP_VAL_FE_LOG_END),
177
178 [SMP_SRC_L6RES] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
179 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
180 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
181 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_BE_RES_CNT |
182 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
183 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
184 SMP_VAL___________),
185
186 [SMP_SRC_HRSHV] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
187 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
188 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
189 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_BE_RES_CNT |
190 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
191 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
192 SMP_VAL___________),
193
194 [SMP_SRC_HRSHP] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
195 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
196 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
197 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_BE_RES_CNT |
198 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
199 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
200 SMP_VAL_FE_LOG_END),
201
202 [SMP_SRC_HRSBO] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
203 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_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
207 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
208 SMP_VAL___________),
209
210 [SMP_SRC_RQFIN] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
211 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_FE_LOG_END),
217
218 [SMP_SRC_RSFIN] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
219 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_FE_LOG_END),
225
226 [SMP_SRC_TXFIN] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
227 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_FE_LOG_END),
233
234 [SMP_SRC_SSFIN] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
235 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_FE_LOG_END),
241};
242
243static const char *fetch_src_names[SMP_SRC_ENTRIES] = {
244 [SMP_SRC_INTRN] = "internal state",
245 [SMP_SRC_LISTN] = "listener",
246 [SMP_SRC_FTEND] = "frontend",
247 [SMP_SRC_L4CLI] = "client address",
248 [SMP_SRC_L5CLI] = "client-side connection",
249 [SMP_SRC_TRACK] = "track counters",
250 [SMP_SRC_L6REQ] = "request buffer",
251 [SMP_SRC_HRQHV] = "HTTP request headers",
252 [SMP_SRC_HRQHP] = "HTTP request",
253 [SMP_SRC_HRQBO] = "HTTP request body",
254 [SMP_SRC_BKEND] = "backend",
255 [SMP_SRC_SERVR] = "server",
256 [SMP_SRC_L4SRV] = "server address",
257 [SMP_SRC_L5SRV] = "server-side connection",
258 [SMP_SRC_L6RES] = "response buffer",
259 [SMP_SRC_HRSHV] = "HTTP response headers",
260 [SMP_SRC_HRSHP] = "HTTP response",
261 [SMP_SRC_HRSBO] = "HTTP response body",
262 [SMP_SRC_RQFIN] = "request buffer statistics",
263 [SMP_SRC_RSFIN] = "response buffer statistics",
264 [SMP_SRC_TXFIN] = "transaction statistics",
265 [SMP_SRC_SSFIN] = "session statistics",
266};
267
Willy Tarreaubf8e2512013-03-25 14:52:41 +0100268static const char *fetch_ckp_names[SMP_CKP_ENTRIES] = {
269 [SMP_CKP_FE_CON_ACC] = "frontend tcp-request connection rule",
270 [SMP_CKP_FE_SES_ACC] = "frontend tcp-request session rule",
271 [SMP_CKP_FE_REQ_CNT] = "frontend tcp-request content rule",
272 [SMP_CKP_FE_HRQ_HDR] = "frontend http-request header rule",
273 [SMP_CKP_FE_HRQ_BDY] = "frontend http-request body rule",
274 [SMP_CKP_FE_SET_BCK] = "frontend use-backend rule",
275 [SMP_CKP_BE_REQ_CNT] = "backend tcp-request content rule",
276 [SMP_CKP_BE_HRQ_HDR] = "backend http-request header rule",
277 [SMP_CKP_BE_HRQ_BDY] = "backend http-request body rule",
278 [SMP_CKP_BE_SET_SRV] = "backend use-server, balance or stick-match rule",
279 [SMP_CKP_BE_SRV_CON] = "server source selection",
280 [SMP_CKP_BE_RES_CNT] = "backend tcp-response content rule",
281 [SMP_CKP_BE_HRS_HDR] = "backend http-response header rule",
282 [SMP_CKP_BE_HRS_BDY] = "backend http-response body rule",
283 [SMP_CKP_BE_STO_RUL] = "backend stick-store rule",
284 [SMP_CKP_FE_RES_CNT] = "frontend tcp-response content rule",
285 [SMP_CKP_FE_HRS_HDR] = "frontend http-response header rule",
286 [SMP_CKP_FE_HRS_BDY] = "frontend http-response body rule",
287 [SMP_CKP_FE_LOG_END] = "logs",
288};
289
Thierry FOURNIER348971e2013-11-21 10:50:10 +0100290/* This function returns the type of the data returned by the sample_expr.
291 * It assumes that the <expr> and all of its converters are properly
292 * initialized.
293 */
294inline
295int smp_expr_output_type(struct sample_expr *expr)
296{
297 struct sample_conv_expr *smp_expr;
298
299 if (!LIST_ISEMPTY(&expr->conv_exprs)) {
300 smp_expr = LIST_PREV(&expr->conv_exprs, struct sample_conv_expr *, list);
301 return smp_expr->conv->out_type;
302 }
303 return expr->fetch->out_type;
304}
305
306
Willy Tarreau80aca902013-01-07 15:42:20 +0100307/* fill the trash with a comma-delimited list of source names for the <use> bit
308 * field which must be composed of a non-null set of SMP_USE_* flags. The return
309 * value is the pointer to the string in the trash buffer.
310 */
311const char *sample_src_names(unsigned int use)
312{
313 int bit;
314
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200315 trash.data = 0;
316 trash.area[0] = '\0';
Willy Tarreau80aca902013-01-07 15:42:20 +0100317 for (bit = 0; bit < SMP_SRC_ENTRIES; bit++) {
318 if (!(use & ~((1 << bit) - 1)))
319 break; /* no more bits */
320
321 if (!(use & (1 << bit)))
322 continue; /* bit not set */
323
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200324 trash.data += snprintf(trash.area + trash.data,
325 trash.size - trash.data, "%s%s",
326 (use & ((1 << bit) - 1)) ? "," : "",
327 fetch_src_names[bit]);
Willy Tarreau80aca902013-01-07 15:42:20 +0100328 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200329 return trash.area;
Willy Tarreau80aca902013-01-07 15:42:20 +0100330}
331
Willy Tarreaubf8e2512013-03-25 14:52:41 +0100332/* return a pointer to the correct sample checkpoint name, or "unknown" when
333 * the flags are invalid. Only the lowest bit is used, higher bits are ignored
334 * if set.
335 */
336const char *sample_ckp_names(unsigned int use)
337{
338 int bit;
339
340 for (bit = 0; bit < SMP_CKP_ENTRIES; bit++)
341 if (use & (1 << bit))
342 return fetch_ckp_names[bit];
343 return "unknown sample check place, please report this bug";
344}
345
Emeric Brun107ca302010-01-04 16:16:05 +0100346/*
Willy Tarreau80aca902013-01-07 15:42:20 +0100347 * Registers the sample fetch keyword list <kwl> as a list of valid keywords
348 * for next parsing sessions. The fetch keywords capabilities are also computed
349 * from their ->use field.
Emeric Brun107ca302010-01-04 16:16:05 +0100350 */
Willy Tarreau80aca902013-01-07 15:42:20 +0100351void sample_register_fetches(struct sample_fetch_kw_list *kwl)
Emeric Brun107ca302010-01-04 16:16:05 +0100352{
Willy Tarreau80aca902013-01-07 15:42:20 +0100353 struct sample_fetch *sf;
354 int bit;
355
356 for (sf = kwl->kw; sf->kw != NULL; sf++) {
357 for (bit = 0; bit < SMP_SRC_ENTRIES; bit++)
358 if (sf->use & (1 << bit))
359 sf->val |= fetch_cap[bit];
360 }
361 LIST_ADDQ(&sample_fetches.list, &kwl->list);
Emeric Brun107ca302010-01-04 16:16:05 +0100362}
363
364/*
Willy Tarreau12785782012-04-27 21:37:17 +0200365 * Registers the sample format coverstion keyword list <pckl> as a list of valid keywords for next
Emeric Brun107ca302010-01-04 16:16:05 +0100366 * parsing sessions.
367 */
Willy Tarreau12785782012-04-27 21:37:17 +0200368void sample_register_convs(struct sample_conv_kw_list *pckl)
Emeric Brun107ca302010-01-04 16:16:05 +0100369{
Willy Tarreau12785782012-04-27 21:37:17 +0200370 LIST_ADDQ(&sample_convs.list, &pckl->list);
Emeric Brun107ca302010-01-04 16:16:05 +0100371}
372
373/*
Willy Tarreau12785782012-04-27 21:37:17 +0200374 * Returns the pointer on sample fetch keyword structure identified by
Emeric Brun107ca302010-01-04 16:16:05 +0100375 * string of <len> in buffer <kw>.
376 *
377 */
Willy Tarreau12785782012-04-27 21:37:17 +0200378struct sample_fetch *find_sample_fetch(const char *kw, int len)
Emeric Brun107ca302010-01-04 16:16:05 +0100379{
380 int index;
Willy Tarreau12785782012-04-27 21:37:17 +0200381 struct sample_fetch_kw_list *kwl;
Emeric Brun107ca302010-01-04 16:16:05 +0100382
Willy Tarreau12785782012-04-27 21:37:17 +0200383 list_for_each_entry(kwl, &sample_fetches.list, list) {
Emeric Brun107ca302010-01-04 16:16:05 +0100384 for (index = 0; kwl->kw[index].kw != NULL; index++) {
385 if (strncmp(kwl->kw[index].kw, kw, len) == 0 &&
386 kwl->kw[index].kw[len] == '\0')
387 return &kwl->kw[index];
388 }
389 }
390 return NULL;
391}
392
Tim Duesterhusc555ee02018-01-25 16:24:44 +0100393/* This function browses the list of available sample fetches. <current> is
Thierry FOURNIER4d9a1d12014-12-08 14:49:19 +0100394 * the last used sample fetch. If it is the first call, it must set to NULL.
Tim Duesterhusc555ee02018-01-25 16:24:44 +0100395 * <idx> is the index of the next sample fetch entry. It is used as private
396 * value. It is useless to initiate it.
Thierry FOURNIER4d9a1d12014-12-08 14:49:19 +0100397 *
Tim Duesterhusc555ee02018-01-25 16:24:44 +0100398 * It returns always the new fetch_sample entry, and NULL when the end of
Thierry FOURNIER4d9a1d12014-12-08 14:49:19 +0100399 * the list is reached.
400 */
401struct sample_fetch *sample_fetch_getnext(struct sample_fetch *current, int *idx)
402{
403 struct sample_fetch_kw_list *kwl;
404 struct sample_fetch *base;
405
406 if (!current) {
407 /* Get first kwl entry. */
408 kwl = LIST_NEXT(&sample_fetches.list, struct sample_fetch_kw_list *, list);
409 (*idx) = 0;
410 } else {
411 /* Get kwl corresponding to the curret entry. */
412 base = current + 1 - (*idx);
413 kwl = container_of(base, struct sample_fetch_kw_list, kw);
414 }
415
416 while (1) {
417
418 /* Check if kwl is the last entry. */
419 if (&kwl->list == &sample_fetches.list)
420 return NULL;
421
422 /* idx contain the next keyword. If it is available, return it. */
423 if (kwl->kw[*idx].kw) {
424 (*idx)++;
425 return &kwl->kw[(*idx)-1];
426 }
427
428 /* get next entry in the main list, and return NULL if the end is reached. */
429 kwl = LIST_NEXT(&kwl->list, struct sample_fetch_kw_list *, list);
430
431 /* Set index to 0, ans do one other loop. */
432 (*idx) = 0;
433 }
434}
435
Thierry FOURNIER8fd13762015-03-10 23:56:48 +0100436/* This function browses the list of available converters. <current> is
437 * the last used converter. If it is the first call, it must set to NULL.
438 * <idx> is the index of the next converter entry. It is used as private
439 * value. It is useless to initiate it.
440 *
441 * It returns always the next sample_conv entry, and NULL when the end of
442 * the list is reached.
443 */
444struct sample_conv *sample_conv_getnext(struct sample_conv *current, int *idx)
445{
446 struct sample_conv_kw_list *kwl;
447 struct sample_conv *base;
448
449 if (!current) {
450 /* Get first kwl entry. */
451 kwl = LIST_NEXT(&sample_convs.list, struct sample_conv_kw_list *, list);
452 (*idx) = 0;
453 } else {
454 /* Get kwl corresponding to the curret entry. */
455 base = current + 1 - (*idx);
456 kwl = container_of(base, struct sample_conv_kw_list, kw);
457 }
458
459 while (1) {
460 /* Check if kwl is the last entry. */
461 if (&kwl->list == &sample_convs.list)
462 return NULL;
463
464 /* idx contain the next keyword. If it is available, return it. */
465 if (kwl->kw[*idx].kw) {
466 (*idx)++;
467 return &kwl->kw[(*idx)-1];
468 }
469
470 /* get next entry in the main list, and return NULL if the end is reached. */
471 kwl = LIST_NEXT(&kwl->list, struct sample_conv_kw_list *, list);
472
473 /* Set index to 0, ans do one other loop. */
474 (*idx) = 0;
475 }
476}
477
Emeric Brun107ca302010-01-04 16:16:05 +0100478/*
Willy Tarreau12785782012-04-27 21:37:17 +0200479 * Returns the pointer on sample format conversion keyword structure identified by
Emeric Brun107ca302010-01-04 16:16:05 +0100480 * string of <len> in buffer <kw>.
481 *
482 */
Willy Tarreau12785782012-04-27 21:37:17 +0200483struct sample_conv *find_sample_conv(const char *kw, int len)
Emeric Brun107ca302010-01-04 16:16:05 +0100484{
485 int index;
Willy Tarreau12785782012-04-27 21:37:17 +0200486 struct sample_conv_kw_list *kwl;
Emeric Brun107ca302010-01-04 16:16:05 +0100487
Willy Tarreau12785782012-04-27 21:37:17 +0200488 list_for_each_entry(kwl, &sample_convs.list, list) {
Emeric Brun107ca302010-01-04 16:16:05 +0100489 for (index = 0; kwl->kw[index].kw != NULL; index++) {
490 if (strncmp(kwl->kw[index].kw, kw, len) == 0 &&
491 kwl->kw[index].kw[len] == '\0')
492 return &kwl->kw[index];
493 }
494 }
495 return NULL;
496}
497
Emeric Brun107ca302010-01-04 16:16:05 +0100498/******************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +0200499/* Sample casts functions */
Emeric Brun107ca302010-01-04 16:16:05 +0100500/******************************************************************/
501
Willy Tarreau342acb42012-04-23 22:03:39 +0200502static int c_ip2int(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100503{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200504 smp->data.u.sint = ntohl(smp->data.u.ipv4.s_addr);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200505 smp->data.type = SMP_T_SINT;
Emeric Brun107ca302010-01-04 16:16:05 +0100506 return 1;
507}
508
Willy Tarreau342acb42012-04-23 22:03:39 +0200509static int c_ip2str(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100510{
Willy Tarreau83061a82018-07-13 11:56:34 +0200511 struct buffer *trash = get_trash_chunk();
Emeric Brun107ca302010-01-04 16:16:05 +0100512
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200513 if (!inet_ntop(AF_INET, (void *)&smp->data.u.ipv4, trash->area, trash->size))
Emeric Brun107ca302010-01-04 16:16:05 +0100514 return 0;
515
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200516 trash->data = strlen(trash->area);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200517 smp->data.u.str = *trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200518 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100519 smp->flags &= ~SMP_F_CONST;
Emeric Brun107ca302010-01-04 16:16:05 +0100520
521 return 1;
522}
523
Willy Tarreau342acb42012-04-23 22:03:39 +0200524static int c_ip2ipv6(struct sample *smp)
David du Colombier4f92d322011-03-24 11:09:31 +0100525{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200526 v4tov6(&smp->data.u.ipv6, &smp->data.u.ipv4);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200527 smp->data.type = SMP_T_IPV6;
David du Colombier4f92d322011-03-24 11:09:31 +0100528 return 1;
529}
530
Thierry FOURNIERcc4d1712015-07-24 09:04:56 +0200531static int c_ipv62ip(struct sample *smp)
532{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200533 if (!v6tov4(&smp->data.u.ipv4, &smp->data.u.ipv6))
Thierry FOURNIERcc4d1712015-07-24 09:04:56 +0200534 return 0;
Tim Duesterhusbf5ce022018-01-25 16:24:46 +0100535 smp->data.type = SMP_T_IPV4;
Thierry FOURNIERcc4d1712015-07-24 09:04:56 +0200536 return 1;
537}
538
Willy Tarreau342acb42012-04-23 22:03:39 +0200539static int c_ipv62str(struct sample *smp)
David du Colombier4f92d322011-03-24 11:09:31 +0100540{
Willy Tarreau83061a82018-07-13 11:56:34 +0200541 struct buffer *trash = get_trash_chunk();
David du Colombier4f92d322011-03-24 11:09:31 +0100542
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200543 if (!inet_ntop(AF_INET6, (void *)&smp->data.u.ipv6, trash->area, trash->size))
David du Colombier4f92d322011-03-24 11:09:31 +0100544 return 0;
545
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200546 trash->data = strlen(trash->area);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200547 smp->data.u.str = *trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200548 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100549 smp->flags &= ~SMP_F_CONST;
David du Colombier4f92d322011-03-24 11:09:31 +0100550 return 1;
551}
552
553/*
Willy Tarreau342acb42012-04-23 22:03:39 +0200554static int c_ipv62ip(struct sample *smp)
David du Colombier4f92d322011-03-24 11:09:31 +0100555{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200556 return v6tov4(&smp->data.u.ipv4, &smp->data.u.ipv6);
David du Colombier4f92d322011-03-24 11:09:31 +0100557}
558*/
559
Willy Tarreau342acb42012-04-23 22:03:39 +0200560static int c_int2ip(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100561{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200562 smp->data.u.ipv4.s_addr = htonl((unsigned int)smp->data.u.sint);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200563 smp->data.type = SMP_T_IPV4;
Emeric Brun107ca302010-01-04 16:16:05 +0100564 return 1;
565}
566
Thierry FOURNIERcc4d1712015-07-24 09:04:56 +0200567static int c_int2ipv6(struct sample *smp)
568{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200569 smp->data.u.ipv4.s_addr = htonl((unsigned int)smp->data.u.sint);
570 v4tov6(&smp->data.u.ipv6, &smp->data.u.ipv4);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200571 smp->data.type = SMP_T_IPV6;
Thierry FOURNIERcc4d1712015-07-24 09:04:56 +0200572 return 1;
573}
574
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100575static int c_str2addr(struct sample *smp)
576{
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200577 if (!buf2ip(smp->data.u.str.area, smp->data.u.str.data, &smp->data.u.ipv4)) {
578 if (!buf2ip6(smp->data.u.str.area, smp->data.u.str.data, &smp->data.u.ipv6))
Thierry FOURNIERfd139902013-12-11 12:38:57 +0100579 return 0;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200580 smp->data.type = SMP_T_IPV6;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100581 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIERfd139902013-12-11 12:38:57 +0100582 return 1;
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100583 }
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200584 smp->data.type = SMP_T_IPV4;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100585 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100586 return 1;
587}
588
Willy Tarreau342acb42012-04-23 22:03:39 +0200589static int c_str2ip(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100590{
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200591 if (!buf2ip(smp->data.u.str.area, smp->data.u.str.data, &smp->data.u.ipv4))
Emeric Brun107ca302010-01-04 16:16:05 +0100592 return 0;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200593 smp->data.type = SMP_T_IPV4;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100594 smp->flags &= ~SMP_F_CONST;
Emeric Brun107ca302010-01-04 16:16:05 +0100595 return 1;
596}
597
Willy Tarreau342acb42012-04-23 22:03:39 +0200598static int c_str2ipv6(struct sample *smp)
David du Colombier4f92d322011-03-24 11:09:31 +0100599{
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200600 if (!buf2ip6(smp->data.u.str.area, smp->data.u.str.data, &smp->data.u.ipv6))
Thierry FOURNIERfd139902013-12-11 12:38:57 +0100601 return 0;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200602 smp->data.type = SMP_T_IPV6;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100603 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIERfd139902013-12-11 12:38:57 +0100604 return 1;
David du Colombier4f92d322011-03-24 11:09:31 +0100605}
606
Emeric Brun4b9e8022014-11-03 18:17:10 +0100607/*
608 * The NULL char always enforces the end of string if it is met.
609 * Data is never changed, so we can ignore the CONST case
Thierry FOURNIERe87cac12014-03-12 15:07:59 +0100610 */
Emeric Brun8ac33d92012-10-17 13:36:06 +0200611static int c_bin2str(struct sample *smp)
612{
Emeric Brun4b9e8022014-11-03 18:17:10 +0100613 int i;
Emeric Brun8ac33d92012-10-17 13:36:06 +0200614
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200615 for (i = 0; i < smp->data.u.str.data; i++) {
616 if (!smp->data.u.str.area[i]) {
617 smp->data.u.str.data = i;
Thierry FOURNIERe87cac12014-03-12 15:07:59 +0100618 break;
Emeric Brun4b9e8022014-11-03 18:17:10 +0100619 }
Emeric Brun8ac33d92012-10-17 13:36:06 +0200620 }
Emeric Brun8ac33d92012-10-17 13:36:06 +0200621 return 1;
622}
623
Willy Tarreau342acb42012-04-23 22:03:39 +0200624static int c_int2str(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100625{
Willy Tarreau83061a82018-07-13 11:56:34 +0200626 struct buffer *trash = get_trash_chunk();
Emeric Brun107ca302010-01-04 16:16:05 +0100627 char *pos;
628
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200629 pos = lltoa_r(smp->data.u.sint, trash->area, trash->size);
Emeric Brun107ca302010-01-04 16:16:05 +0100630 if (!pos)
631 return 0;
632
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200633 trash->size = trash->size - (pos - trash->area);
634 trash->area = pos;
635 trash->data = strlen(pos);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200636 smp->data.u.str = *trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200637 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100638 smp->flags &= ~SMP_F_CONST;
Emeric Brun107ca302010-01-04 16:16:05 +0100639 return 1;
640}
641
Willy Tarreauad635822016-08-08 19:21:09 +0200642/* This function inconditionally duplicates data and removes the "const" flag.
643 * For strings and binary blocks, it also provides a known allocated size with
644 * a length that is capped to the size, and ensures a trailing zero is always
645 * appended for strings. This is necessary for some operations which may
646 * require to extend the length. It returns 0 if it fails, 1 on success.
647 */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100648int smp_dup(struct sample *smp)
Emeric Brun485479d2010-09-23 18:02:19 +0200649{
Willy Tarreau83061a82018-07-13 11:56:34 +0200650 struct buffer *trash;
Emeric Brun485479d2010-09-23 18:02:19 +0200651
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200652 switch (smp->data.type) {
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100653 case SMP_T_BOOL:
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100654 case SMP_T_SINT:
655 case SMP_T_ADDR:
656 case SMP_T_IPV4:
657 case SMP_T_IPV6:
658 /* These type are not const. */
659 break;
Willy Tarreauad635822016-08-08 19:21:09 +0200660
Christopher Fauletec100512017-07-24 15:38:41 +0200661 case SMP_T_METH:
662 if (smp->data.u.meth.meth != HTTP_METH_OTHER)
663 break;
664 /* Fall through */
665
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100666 case SMP_T_STR:
Willy Tarreauad635822016-08-08 19:21:09 +0200667 trash = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200668 trash->data = smp->data.u.str.data;
669 if (trash->data > trash->size - 1)
670 trash->data = trash->size - 1;
Willy Tarreauad635822016-08-08 19:21:09 +0200671
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200672 memcpy(trash->area, smp->data.u.str.area, trash->data);
673 trash->area[trash->data] = 0;
Willy Tarreauad635822016-08-08 19:21:09 +0200674 smp->data.u.str = *trash;
675 break;
676
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100677 case SMP_T_BIN:
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100678 trash = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200679 trash->data = smp->data.u.str.data;
680 if (trash->data > trash->size)
681 trash->data = trash->size;
Willy Tarreauad635822016-08-08 19:21:09 +0200682
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200683 memcpy(trash->area, smp->data.u.str.area, trash->data);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200684 smp->data.u.str = *trash;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100685 break;
Christopher Fauletec100512017-07-24 15:38:41 +0200686
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100687 default:
688 /* Other cases are unexpected. */
689 return 0;
690 }
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100691
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100692 /* remove const flag */
693 smp->flags &= ~SMP_F_CONST;
Emeric Brun485479d2010-09-23 18:02:19 +0200694 return 1;
695}
696
Thierry FOURNIER0e9af552013-12-14 14:55:04 +0100697int c_none(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100698{
699 return 1;
700}
701
Willy Tarreau342acb42012-04-23 22:03:39 +0200702static int c_str2int(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100703{
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +0200704 const char *str;
705 const char *end;
Emeric Brun107ca302010-01-04 16:16:05 +0100706
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200707 if (smp->data.u.str.data == 0)
Thierry FOURNIER60bb0202014-01-27 18:20:48 +0100708 return 0;
709
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200710 str = smp->data.u.str.area;
711 end = smp->data.u.str.area + smp->data.u.str.data;
Emeric Brun107ca302010-01-04 16:16:05 +0100712
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200713 smp->data.u.sint = read_int64(&str, end);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200714 smp->data.type = SMP_T_SINT;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100715 smp->flags &= ~SMP_F_CONST;
Emeric Brun107ca302010-01-04 16:16:05 +0100716 return 1;
717}
718
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100719static int c_str2meth(struct sample *smp)
720{
721 enum http_meth_t meth;
722 int len;
723
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200724 meth = find_http_meth(smp->data.u.str.area, smp->data.u.str.data);
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100725 if (meth == HTTP_METH_OTHER) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200726 len = smp->data.u.str.data;
727 smp->data.u.meth.str.area = smp->data.u.str.area;
728 smp->data.u.meth.str.data = len;
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100729 }
730 else
731 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200732 smp->data.u.meth.meth = meth;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200733 smp->data.type = SMP_T_METH;
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100734 return 1;
735}
736
737static int c_meth2str(struct sample *smp)
738{
739 int len;
740 enum http_meth_t meth;
741
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200742 if (smp->data.u.meth.meth == HTTP_METH_OTHER) {
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100743 /* The method is unknown. Copy the original pointer. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200744 len = smp->data.u.meth.str.data;
745 smp->data.u.str.area = smp->data.u.meth.str.area;
746 smp->data.u.str.data = len;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200747 smp->data.type = SMP_T_STR;
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100748 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200749 else if (smp->data.u.meth.meth < HTTP_METH_OTHER) {
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100750 /* The method is known, copy the pointer containing the string. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200751 meth = smp->data.u.meth.meth;
Willy Tarreau35b51c62018-09-10 15:38:55 +0200752 smp->data.u.str.area = http_known_methods[meth].ptr;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200753 smp->data.u.str.data = http_known_methods[meth].len;
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100754 smp->flags |= SMP_F_CONST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200755 smp->data.type = SMP_T_STR;
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100756 }
757 else {
758 /* Unknown method */
759 return 0;
760 }
761 return 1;
762}
763
Willy Tarreau9700e5c2014-07-15 21:03:26 +0200764static int c_addr2bin(struct sample *smp)
765{
Willy Tarreau83061a82018-07-13 11:56:34 +0200766 struct buffer *chk = get_trash_chunk();
Willy Tarreau9700e5c2014-07-15 21:03:26 +0200767
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200768 if (smp->data.type == SMP_T_IPV4) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200769 chk->data = 4;
770 memcpy(chk->area, &smp->data.u.ipv4, chk->data);
Willy Tarreau9700e5c2014-07-15 21:03:26 +0200771 }
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200772 else if (smp->data.type == SMP_T_IPV6) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200773 chk->data = 16;
774 memcpy(chk->area, &smp->data.u.ipv6, chk->data);
Willy Tarreau9700e5c2014-07-15 21:03:26 +0200775 }
776 else
777 return 0;
778
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200779 smp->data.u.str = *chk;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200780 smp->data.type = SMP_T_BIN;
Willy Tarreau9700e5c2014-07-15 21:03:26 +0200781 return 1;
782}
783
Willy Tarreaubbfd1a22014-07-15 21:19:08 +0200784static int c_int2bin(struct sample *smp)
785{
Willy Tarreau83061a82018-07-13 11:56:34 +0200786 struct buffer *chk = get_trash_chunk();
Willy Tarreaubbfd1a22014-07-15 21:19:08 +0200787
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200788 *(unsigned long long int *) chk->area = my_htonll(smp->data.u.sint);
789 chk->data = 8;
Willy Tarreaubbfd1a22014-07-15 21:19:08 +0200790
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200791 smp->data.u.str = *chk;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200792 smp->data.type = SMP_T_BIN;
Willy Tarreaubbfd1a22014-07-15 21:19:08 +0200793 return 1;
794}
795
Willy Tarreau9700e5c2014-07-15 21:03:26 +0200796
Emeric Brun107ca302010-01-04 16:16:05 +0100797/*****************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +0200798/* Sample casts matrix: */
799/* sample_casts[from type][to type] */
800/* NULL pointer used for impossible sample casts */
Emeric Brun107ca302010-01-04 16:16:05 +0100801/*****************************************************************/
Emeric Brun107ca302010-01-04 16:16:05 +0100802
Thierry FOURNIER8af6ff12013-11-21 10:53:12 +0100803sample_cast_fct sample_casts[SMP_TYPES][SMP_TYPES] = {
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +0200804/* to: ANY BOOL SINT ADDR IPV4 IPV6 STR BIN METH */
805/* from: ANY */ { c_none, c_none, c_none, c_none, c_none, c_none, c_none, c_none, c_none, },
806/* BOOL */ { c_none, c_none, c_none, NULL, NULL, NULL, c_int2str, NULL, NULL, },
Thierry FOURNIERcc4d1712015-07-24 09:04:56 +0200807/* 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 +0200808/* ADDR */ { c_none, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, },
809/* IPV4 */ { c_none, NULL, c_ip2int, c_none, c_none, c_ip2ipv6, c_ip2str, c_addr2bin, NULL, },
Thierry FOURNIERcc4d1712015-07-24 09:04:56 +0200810/* IPV6 */ { c_none, NULL, NULL, c_none, c_ipv62ip,c_none, c_ipv62str, c_addr2bin, NULL, },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +0200811/* STR */ { c_none, c_str2int, c_str2int, c_str2addr, c_str2ip, c_str2ipv6, c_none, c_none, c_str2meth, },
812/* BIN */ { c_none, NULL, NULL, NULL, NULL, NULL, c_bin2str, c_none, c_str2meth, },
813/* METH */ { c_none, NULL, NULL, NULL, NULL, NULL, c_meth2str, c_meth2str, c_none, }
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200814};
Emeric Brun107ca302010-01-04 16:16:05 +0100815
Emeric Brun107ca302010-01-04 16:16:05 +0100816/*
Willy Tarreau12785782012-04-27 21:37:17 +0200817 * Parse a sample expression configuration:
Emeric Brun107ca302010-01-04 16:16:05 +0100818 * fetch keyword followed by format conversion keywords.
Willy Tarreau12785782012-04-27 21:37:17 +0200819 * Returns a pointer on allocated sample expression structure.
Willy Tarreaua4312fa2013-04-02 16:34:32 +0200820 * The caller must have set al->ctx.
Emeric Brun107ca302010-01-04 16:16:05 +0100821 */
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +0100822struct 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 +0100823{
Willy Tarreau833cc792013-07-24 15:34:19 +0200824 const char *begw; /* beginning of word */
825 const char *endw; /* end of word */
826 const char *endt; /* end of term */
Willy Tarreau12785782012-04-27 21:37:17 +0200827 struct sample_expr *expr;
828 struct sample_fetch *fetch;
829 struct sample_conv *conv;
Emeric Brun107ca302010-01-04 16:16:05 +0100830 unsigned long prev_type;
Willy Tarreau833cc792013-07-24 15:34:19 +0200831 char *fkw = NULL;
832 char *ckw = NULL;
Willy Tarreau689a1df2013-12-13 00:40:11 +0100833 int err_arg;
Emeric Brun107ca302010-01-04 16:16:05 +0100834
Willy Tarreau833cc792013-07-24 15:34:19 +0200835 begw = str[*idx];
836 for (endw = begw; *endw && *endw != '(' && *endw != ','; endw++);
837
838 if (endw == begw) {
Willy Tarreau975c1782013-12-12 23:16:54 +0100839 memprintf(err_msg, "missing fetch method");
Emeric Brun107ca302010-01-04 16:16:05 +0100840 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200841 }
Emeric Brun107ca302010-01-04 16:16:05 +0100842
Willy Tarreau833cc792013-07-24 15:34:19 +0200843 /* keep a copy of the current fetch keyword for error reporting */
844 fkw = my_strndup(begw, endw - begw);
Emeric Brun107ca302010-01-04 16:16:05 +0100845
Willy Tarreau833cc792013-07-24 15:34:19 +0200846 fetch = find_sample_fetch(begw, endw - begw);
847 if (!fetch) {
Willy Tarreau975c1782013-12-12 23:16:54 +0100848 memprintf(err_msg, "unknown fetch method '%s'", fkw);
Emeric Brun107ca302010-01-04 16:16:05 +0100849 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200850 }
Emeric Brun107ca302010-01-04 16:16:05 +0100851
Willy Tarreau833cc792013-07-24 15:34:19 +0200852 endt = endw;
853 if (*endt == '(') {
Willy Tarreau689a1df2013-12-13 00:40:11 +0100854 /* look for the end of this term and skip the opening parenthesis */
855 endt = ++endw;
Willy Tarreau833cc792013-07-24 15:34:19 +0200856 while (*endt && *endt != ')')
857 endt++;
858 if (*endt != ')') {
Willy Tarreau975c1782013-12-12 23:16:54 +0100859 memprintf(err_msg, "missing closing ')' after arguments to fetch keyword '%s'", fkw);
Willy Tarreau833cc792013-07-24 15:34:19 +0200860 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200861 }
Emeric Brun485479d2010-09-23 18:02:19 +0200862 }
Emeric Brun107ca302010-01-04 16:16:05 +0100863
Willy Tarreau833cc792013-07-24 15:34:19 +0200864 /* At this point, we have :
865 * - begw : beginning of the keyword
Willy Tarreau689a1df2013-12-13 00:40:11 +0100866 * - endw : end of the keyword, first character not part of keyword
867 * nor the opening parenthesis (so first character of args
868 * if present).
Willy Tarreau833cc792013-07-24 15:34:19 +0200869 * - endt : end of the term (=endw or last parenthesis if args are present)
870 */
871
872 if (fetch->out_type >= SMP_TYPES) {
Willy Tarreau975c1782013-12-12 23:16:54 +0100873 memprintf(err_msg, "returns type of fetch method '%s' is unknown", fkw);
Emeric Brun107ca302010-01-04 16:16:05 +0100874 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200875 }
Emeric Brun107ca302010-01-04 16:16:05 +0100876 prev_type = fetch->out_type;
Willy Tarreau833cc792013-07-24 15:34:19 +0200877
Vincent Bernat02779b62016-04-03 13:48:43 +0200878 expr = calloc(1, sizeof(*expr));
Emeric Brun485479d2010-09-23 18:02:19 +0200879 if (!expr)
880 goto out_error;
Emeric Brun107ca302010-01-04 16:16:05 +0100881
882 LIST_INIT(&(expr->conv_exprs));
883 expr->fetch = fetch;
Willy Tarreau2e845be2012-10-19 19:49:09 +0200884 expr->arg_p = empty_arg_list;
Emeric Brun107ca302010-01-04 16:16:05 +0100885
Willy Tarreau689a1df2013-12-13 00:40:11 +0100886 /* Note that we call the argument parser even with an empty string,
887 * this allows it to automatically create entries for mandatory
888 * implicit arguments (eg: local proxy name).
889 */
890 al->kw = expr->fetch->kw;
891 al->conv = NULL;
892 if (make_arg_list(endw, endt - endw, fetch->arg_mask, &expr->arg_p, err_msg, NULL, &err_arg, al) < 0) {
893 memprintf(err_msg, "fetch method '%s' : %s", fkw, *err_msg);
894 goto out_error;
895 }
Willy Tarreau2e845be2012-10-19 19:49:09 +0200896
Willy Tarreau689a1df2013-12-13 00:40:11 +0100897 if (!expr->arg_p) {
898 expr->arg_p = empty_arg_list;
Emeric Brun485479d2010-09-23 18:02:19 +0200899 }
Willy Tarreau689a1df2013-12-13 00:40:11 +0100900 else if (fetch->val_args && !fetch->val_args(expr->arg_p, err_msg)) {
901 memprintf(err_msg, "invalid args in fetch method '%s' : %s", fkw, *err_msg);
Emeric Brun485479d2010-09-23 18:02:19 +0200902 goto out_error;
Emeric Brun107ca302010-01-04 16:16:05 +0100903 }
904
Willy Tarreau833cc792013-07-24 15:34:19 +0200905 /* Now process the converters if any. We have two supported syntaxes
906 * for the converters, which can be combined :
907 * - comma-delimited list of converters just after the keyword and args ;
908 * - one converter per keyword
909 * The combination allows to have each keyword being a comma-delimited
910 * series of converters.
911 *
912 * We want to process the former first, then the latter. For this we start
913 * from the beginning of the supposed place in the exiting conv chain, which
914 * starts at the last comma (endt).
915 */
916
917 while (1) {
Willy Tarreau12785782012-04-27 21:37:17 +0200918 struct sample_conv_expr *conv_expr;
Emeric Brun107ca302010-01-04 16:16:05 +0100919
Willy Tarreau833cc792013-07-24 15:34:19 +0200920 if (*endt == ')') /* skip last closing parenthesis */
921 endt++;
Emeric Brun107ca302010-01-04 16:16:05 +0100922
Willy Tarreau833cc792013-07-24 15:34:19 +0200923 if (*endt && *endt != ',') {
924 if (ckw)
Willy Tarreau97108e02016-11-25 07:33:24 +0100925 memprintf(err_msg, "missing comma after converter '%s'", ckw);
Willy Tarreau833cc792013-07-24 15:34:19 +0200926 else
Willy Tarreau975c1782013-12-12 23:16:54 +0100927 memprintf(err_msg, "missing comma after fetch keyword '%s'", fkw);
Emeric Brun107ca302010-01-04 16:16:05 +0100928 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200929 }
Emeric Brun107ca302010-01-04 16:16:05 +0100930
Willy Tarreau833cc792013-07-24 15:34:19 +0200931 while (*endt == ',') /* then trailing commas */
932 endt++;
933
Willy Tarreau97108e02016-11-25 07:33:24 +0100934 begw = endt; /* start of converter */
Willy Tarreau833cc792013-07-24 15:34:19 +0200935
936 if (!*begw) {
937 /* none ? skip to next string */
938 (*idx)++;
939 begw = str[*idx];
940 if (!begw || !*begw)
941 break;
942 }
943
944 for (endw = begw; *endw && *endw != '(' && *endw != ','; endw++);
945
946 free(ckw);
947 ckw = my_strndup(begw, endw - begw);
948
949 conv = find_sample_conv(begw, endw - begw);
950 if (!conv) {
951 /* we found an isolated keyword that we don't know, it's not ours */
952 if (begw == str[*idx])
953 break;
Willy Tarreau97108e02016-11-25 07:33:24 +0100954 memprintf(err_msg, "unknown converter '%s'", ckw);
Willy Tarreau833cc792013-07-24 15:34:19 +0200955 goto out_error;
956 }
Emeric Brun107ca302010-01-04 16:16:05 +0100957
Willy Tarreau833cc792013-07-24 15:34:19 +0200958 endt = endw;
959 if (*endt == '(') {
960 /* look for the end of this term */
961 while (*endt && *endt != ')')
962 endt++;
963 if (*endt != ')') {
Willy Tarreau97108e02016-11-25 07:33:24 +0100964 memprintf(err_msg, "syntax error: missing ')' after converter '%s'", ckw);
Willy Tarreau833cc792013-07-24 15:34:19 +0200965 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200966 }
Willy Tarreau833cc792013-07-24 15:34:19 +0200967 }
968
969 if (conv->in_type >= SMP_TYPES || conv->out_type >= SMP_TYPES) {
Willy Tarreau97108e02016-11-25 07:33:24 +0100970 memprintf(err_msg, "returns type of converter '%s' is unknown", ckw);
Emeric Brun107ca302010-01-04 16:16:05 +0100971 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200972 }
Emeric Brun107ca302010-01-04 16:16:05 +0100973
974 /* If impossible type conversion */
Willy Tarreau12785782012-04-27 21:37:17 +0200975 if (!sample_casts[prev_type][conv->in_type]) {
Willy Tarreau97108e02016-11-25 07:33:24 +0100976 memprintf(err_msg, "converter '%s' cannot be applied", ckw);
Emeric Brun107ca302010-01-04 16:16:05 +0100977 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200978 }
Emeric Brun107ca302010-01-04 16:16:05 +0100979
980 prev_type = conv->out_type;
Vincent Bernat02779b62016-04-03 13:48:43 +0200981 conv_expr = calloc(1, sizeof(*conv_expr));
Emeric Brun485479d2010-09-23 18:02:19 +0200982 if (!conv_expr)
983 goto out_error;
Emeric Brun107ca302010-01-04 16:16:05 +0100984
985 LIST_ADDQ(&(expr->conv_exprs), &(conv_expr->list));
986 conv_expr->conv = conv;
987
Willy Tarreau833cc792013-07-24 15:34:19 +0200988 if (endt != endw) {
Willy Tarreaub27c0d32012-04-20 16:04:47 +0200989 int err_arg;
Willy Tarreau21d68a62012-04-20 15:52:36 +0200990
Willy Tarreau9fcb9842012-04-20 14:45:49 +0200991 if (!conv->arg_mask) {
Willy Tarreau97108e02016-11-25 07:33:24 +0100992 memprintf(err_msg, "converter '%s' does not support any args", ckw);
Emeric Brun485479d2010-09-23 18:02:19 +0200993 goto out_error;
994 }
Willy Tarreau9e92d322010-01-26 17:58:06 +0100995
Willy Tarreaua4312fa2013-04-02 16:34:32 +0200996 al->kw = expr->fetch->kw;
997 al->conv = conv_expr->conv->kw;
Willy Tarreau975c1782013-12-12 23:16:54 +0100998 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 +0100999 memprintf(err_msg, "invalid arg %d in converter '%s' : %s", err_arg+1, ckw, *err_msg);
Willy Tarreau21d68a62012-04-20 15:52:36 +02001000 goto out_error;
1001 }
1002
Willy Tarreau2e845be2012-10-19 19:49:09 +02001003 if (!conv_expr->arg_p)
1004 conv_expr->arg_p = empty_arg_list;
1005
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +01001006 if (conv->val_args && !conv->val_args(conv_expr->arg_p, conv, file, line, err_msg)) {
Willy Tarreau97108e02016-11-25 07:33:24 +01001007 memprintf(err_msg, "invalid args in converter '%s' : %s", ckw, *err_msg);
Emeric Brun485479d2010-09-23 18:02:19 +02001008 goto out_error;
1009 }
1010 }
Willy Tarreau833cc792013-07-24 15:34:19 +02001011 else if (ARGM(conv->arg_mask)) {
Willy Tarreau97108e02016-11-25 07:33:24 +01001012 memprintf(err_msg, "missing args for converter '%s'", ckw);
Emeric Brun485479d2010-09-23 18:02:19 +02001013 goto out_error;
Emeric Brun107ca302010-01-04 16:16:05 +01001014 }
1015 }
Emeric Brun485479d2010-09-23 18:02:19 +02001016
Willy Tarreau833cc792013-07-24 15:34:19 +02001017 out:
1018 free(fkw);
1019 free(ckw);
Emeric Brun107ca302010-01-04 16:16:05 +01001020 return expr;
1021
1022out_error:
Willy Tarreau12785782012-04-27 21:37:17 +02001023 /* TODO: prune_sample_expr(expr); */
Willy Tarreau833cc792013-07-24 15:34:19 +02001024 expr = NULL;
1025 goto out;
Emeric Brun107ca302010-01-04 16:16:05 +01001026}
1027
1028/*
Willy Tarreau12785782012-04-27 21:37:17 +02001029 * Process a fetch + format conversion of defined by the sample expression <expr>
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001030 * on request or response considering the <opt> parameter.
Willy Tarreau12785782012-04-27 21:37:17 +02001031 * Returns a pointer on a typed sample structure containing the result or NULL if
1032 * sample is not found or when format conversion failed.
Emeric Brun107ca302010-01-04 16:16:05 +01001033 * If <p> is not null, function returns results in structure pointed by <p>.
Willy Tarreau12785782012-04-27 21:37:17 +02001034 * If <p> is null, functions returns a pointer on a static sample structure.
Willy Tarreaub8c8f1f2012-04-23 22:38:26 +02001035 *
1036 * Note: the fetch functions are required to properly set the return type. The
1037 * conversion functions must do so too. However the cast functions do not need
1038 * to since they're made to cast mutiple types according to what is required.
Willy Tarreau6bcb0a82014-07-30 08:56:35 +02001039 *
1040 * The caller may indicate in <opt> if it considers the result final or not.
1041 * The caller needs to check the SMP_F_MAY_CHANGE flag in p->flags to verify
1042 * if the result is stable or not, according to the following table :
1043 *
1044 * return MAY_CHANGE FINAL Meaning for the sample
1045 * NULL 0 * Not present and will never be (eg: header)
1046 * NULL 1 0 Not present yet, could change (eg: POST param)
1047 * NULL 1 1 Not present yet, will not change anymore
1048 * smp 0 * Present and will not change (eg: header)
1049 * smp 1 0 Present, may change (eg: request length)
1050 * smp 1 1 Present, last known value (eg: request length)
Emeric Brun107ca302010-01-04 16:16:05 +01001051 */
Willy Tarreau192252e2015-04-04 01:47:55 +02001052struct sample *sample_process(struct proxy *px, struct session *sess,
1053 struct stream *strm, unsigned int opt,
Willy Tarreau12785782012-04-27 21:37:17 +02001054 struct sample_expr *expr, struct sample *p)
Emeric Brun107ca302010-01-04 16:16:05 +01001055{
Willy Tarreau12785782012-04-27 21:37:17 +02001056 struct sample_conv_expr *conv_expr;
Emeric Brun107ca302010-01-04 16:16:05 +01001057
Willy Tarreau18387e22013-07-25 12:02:38 +02001058 if (p == NULL) {
Willy Tarreaub4a88f02012-04-23 21:35:11 +02001059 p = &temp_smp;
Willy Tarreau6c616e02014-06-25 16:56:41 +02001060 memset(p, 0, sizeof(*p));
Willy Tarreau18387e22013-07-25 12:02:38 +02001061 }
Emeric Brun107ca302010-01-04 16:16:05 +01001062
Willy Tarreau1777ea62016-03-10 16:15:46 +01001063 smp_set_owner(p, px, sess, strm, opt);
Thierry FOURNIER0786d052015-05-11 15:42:45 +02001064 if (!expr->fetch->process(expr->arg_p, p, expr->fetch->kw, expr->fetch->private))
Emeric Brun107ca302010-01-04 16:16:05 +01001065 return NULL;
1066
Emeric Brun107ca302010-01-04 16:16:05 +01001067 list_for_each_entry(conv_expr, &expr->conv_exprs, list) {
Willy Tarreau12e50112012-04-25 17:21:49 +02001068 /* we want to ensure that p->type can be casted into
1069 * conv_expr->conv->in_type. We have 3 possibilities :
1070 * - NULL => not castable.
1071 * - c_none => nothing to do (let's optimize it)
1072 * - other => apply cast and prepare to fail
1073 */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001074 if (!sample_casts[p->data.type][conv_expr->conv->in_type])
Willy Tarreau12e50112012-04-25 17:21:49 +02001075 return NULL;
1076
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001077 if (sample_casts[p->data.type][conv_expr->conv->in_type] != c_none &&
1078 !sample_casts[p->data.type][conv_expr->conv->in_type](p))
Emeric Brun107ca302010-01-04 16:16:05 +01001079 return NULL;
1080
Willy Tarreau12e50112012-04-25 17:21:49 +02001081 /* OK cast succeeded */
1082
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001083 if (!conv_expr->conv->process(conv_expr->arg_p, p, conv_expr->conv->private))
Emeric Brun107ca302010-01-04 16:16:05 +01001084 return NULL;
Emeric Brun107ca302010-01-04 16:16:05 +01001085 }
1086 return p;
1087}
1088
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001089/*
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001090 * Resolve all remaining arguments in proxy <p>. Returns the number of
1091 * errors or 0 if everything is fine.
1092 */
1093int smp_resolve_args(struct proxy *p)
1094{
1095 struct arg_list *cur, *bak;
1096 const char *ctx, *where;
1097 const char *conv_ctx, *conv_pre, *conv_pos;
1098 struct userlist *ul;
Willy Tarreau46947782015-01-19 19:00:58 +01001099 struct my_regex *reg;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001100 struct arg *arg;
1101 int cfgerr = 0;
Willy Tarreau46947782015-01-19 19:00:58 +01001102 int rflags;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001103
1104 list_for_each_entry_safe(cur, bak, &p->conf.args.list, list) {
1105 struct proxy *px;
1106 struct server *srv;
1107 char *pname, *sname;
Willy Tarreau46947782015-01-19 19:00:58 +01001108 char *err;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001109
1110 arg = cur->arg;
1111
1112 /* prepare output messages */
1113 conv_pre = conv_pos = conv_ctx = "";
1114 if (cur->conv) {
1115 conv_ctx = cur->conv;
1116 conv_pre = "conversion keyword '";
1117 conv_pos = "' for ";
1118 }
1119
1120 where = "in";
1121 ctx = "sample fetch keyword";
1122 switch (cur->ctx) {
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001123 case ARGC_STK: where = "in stick rule in"; break;
1124 case ARGC_TRK: where = "in tracking rule in"; break;
1125 case ARGC_LOG: where = "in log-format string in"; break;
1126 case ARGC_LOGSD: where = "in log-format-sd string in"; break;
1127 case ARGC_HRQ: where = "in http-request header format string in"; break;
1128 case ARGC_HRS: where = "in http-response header format string in"; break;
1129 case ARGC_UIF: where = "in unique-id-format string in"; break;
1130 case ARGC_RDR: where = "in redirect format string in"; break;
1131 case ARGC_CAP: where = "in capture rule in"; break;
1132 case ARGC_ACL: ctx = "ACL keyword"; break;
1133 case ARGC_SRV: where = "in server directive in"; break;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001134 case ARGC_SPOE: where = "in spoe-message directive in"; break;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001135 }
1136
1137 /* set a few default settings */
1138 px = p;
1139 pname = p->id;
1140
1141 switch (arg->type) {
1142 case ARGT_SRV:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001143 if (!arg->data.str.data) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001144 ha_alert("parsing [%s:%d] : missing server name in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1145 cur->file, cur->line,
1146 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001147 cfgerr++;
1148 continue;
1149 }
1150
1151 /* we support two formats : "bck/srv" and "srv" */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001152 sname = strrchr(arg->data.str.area, '/');
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001153
1154 if (sname) {
1155 *sname++ = '\0';
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001156 pname = arg->data.str.area;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001157
Willy Tarreau9e0bb102015-05-26 11:24:42 +02001158 px = proxy_be_by_name(pname);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001159 if (!px) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001160 ha_alert("parsing [%s:%d] : unable to find proxy '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1161 cur->file, cur->line, pname,
1162 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001163 cfgerr++;
1164 break;
1165 }
1166 }
1167 else
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001168 sname = arg->data.str.area;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001169
1170 srv = findserver(px, sname);
1171 if (!srv) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001172 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",
1173 cur->file, cur->line, sname, pname,
1174 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001175 cfgerr++;
1176 break;
1177 }
1178
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001179 free(arg->data.str.area);
1180 arg->data.str.area = NULL;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001181 arg->unresolved = 0;
1182 arg->data.srv = srv;
1183 break;
1184
1185 case ARGT_FE:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001186 if (arg->data.str.data) {
1187 pname = arg->data.str.area;
Willy Tarreau9e0bb102015-05-26 11:24:42 +02001188 px = proxy_fe_by_name(pname);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001189 }
1190
1191 if (!px) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001192 ha_alert("parsing [%s:%d] : unable to find frontend '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1193 cur->file, cur->line, pname,
1194 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001195 cfgerr++;
1196 break;
1197 }
1198
1199 if (!(px->cap & PR_CAP_FE)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001200 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",
1201 cur->file, cur->line, pname,
1202 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001203 cfgerr++;
1204 break;
1205 }
1206
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001207 free(arg->data.str.area);
1208 arg->data.str.area = NULL;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001209 arg->unresolved = 0;
1210 arg->data.prx = px;
1211 break;
1212
1213 case ARGT_BE:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001214 if (arg->data.str.data) {
1215 pname = arg->data.str.area;
Willy Tarreau9e0bb102015-05-26 11:24:42 +02001216 px = proxy_be_by_name(pname);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001217 }
1218
1219 if (!px) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001220 ha_alert("parsing [%s:%d] : unable to find backend '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1221 cur->file, cur->line, pname,
1222 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001223 cfgerr++;
1224 break;
1225 }
1226
1227 if (!(px->cap & PR_CAP_BE)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001228 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",
1229 cur->file, cur->line, pname,
1230 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001231 cfgerr++;
1232 break;
1233 }
1234
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001235 free(arg->data.str.area);
1236 arg->data.str.area = NULL;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001237 arg->unresolved = 0;
1238 arg->data.prx = px;
1239 break;
1240
1241 case ARGT_TAB:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001242 if (arg->data.str.data) {
1243 pname = arg->data.str.area;
Willy Tarreaue2dc1fa2015-05-26 12:08:07 +02001244 px = proxy_tbl_by_name(pname);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001245 }
1246
1247 if (!px) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001248 ha_alert("parsing [%s:%d] : unable to find table '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1249 cur->file, cur->line, pname,
1250 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001251 cfgerr++;
1252 break;
1253 }
1254
1255 if (!px->table.size) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001256 ha_alert("parsing [%s:%d] : no table in proxy '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1257 cur->file, cur->line, pname,
1258 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001259 cfgerr++;
1260 break;
1261 }
1262
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001263 free(arg->data.str.area);
1264 arg->data.str.area = NULL;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001265 arg->unresolved = 0;
1266 arg->data.prx = px;
1267 break;
1268
1269 case ARGT_USR:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001270 if (!arg->data.str.data) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001271 ha_alert("parsing [%s:%d] : missing userlist name in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1272 cur->file, cur->line,
1273 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001274 cfgerr++;
1275 break;
1276 }
1277
1278 if (p->uri_auth && p->uri_auth->userlist &&
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001279 !strcmp(p->uri_auth->userlist->name, arg->data.str.area))
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001280 ul = p->uri_auth->userlist;
1281 else
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001282 ul = auth_find_userlist(arg->data.str.area);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001283
1284 if (!ul) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001285 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 +02001286 cur->file, cur->line,
1287 arg->data.str.area,
Christopher Faulet767a84b2017-11-24 16:50:31 +01001288 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001289 cfgerr++;
1290 break;
1291 }
1292
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001293 free(arg->data.str.area);
1294 arg->data.str.area = NULL;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001295 arg->unresolved = 0;
1296 arg->data.usr = ul;
1297 break;
Willy Tarreau46947782015-01-19 19:00:58 +01001298
1299 case ARGT_REG:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001300 if (!arg->data.str.data) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001301 ha_alert("parsing [%s:%d] : missing regex in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1302 cur->file, cur->line,
1303 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreau46947782015-01-19 19:00:58 +01001304 cfgerr++;
1305 continue;
1306 }
1307
1308 reg = calloc(1, sizeof(*reg));
1309 if (!reg) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001310 ha_alert("parsing [%s:%d] : not enough memory to build regex in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1311 cur->file, cur->line,
1312 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreau46947782015-01-19 19:00:58 +01001313 cfgerr++;
1314 continue;
1315 }
1316
1317 rflags = 0;
1318 rflags |= (arg->type_flags & ARGF_REG_ICASE) ? REG_ICASE : 0;
1319 err = NULL;
1320
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001321 if (!regex_comp(arg->data.str.area, reg, !(rflags & REG_ICASE), 1 /* capture substr */, &err)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001322 ha_alert("parsing [%s:%d] : error in regex '%s' in arg %d of %s%s%s%s '%s' %s proxy '%s' : %s.\n",
1323 cur->file, cur->line,
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001324 arg->data.str.area,
Christopher Faulet767a84b2017-11-24 16:50:31 +01001325 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id, err);
Willy Tarreau46947782015-01-19 19:00:58 +01001326 cfgerr++;
1327 continue;
1328 }
1329
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001330 free(arg->data.str.area);
1331 arg->data.str.area = NULL;
Willy Tarreau46947782015-01-19 19:00:58 +01001332 arg->unresolved = 0;
1333 arg->data.reg = reg;
1334 break;
1335
1336
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001337 }
1338
1339 LIST_DEL(&cur->list);
1340 free(cur);
1341 } /* end of args processing */
1342
1343 return cfgerr;
1344}
1345
1346/*
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001347 * Process a fetch + format conversion as defined by the sample expression
1348 * <expr> on request or response considering the <opt> parameter. The output is
Adis Nezirovic79beb242015-07-06 15:41:02 +02001349 * not explicitly set to <smp_type>, but shall be compatible with it as
1350 * specified by 'sample_casts' table. If a stable sample can be fetched, or an
1351 * unstable one when <opt> contains SMP_OPT_FINAL, the sample is converted and
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001352 * returned without the SMP_F_MAY_CHANGE flag. If an unstable sample is found
1353 * and <opt> does not contain SMP_OPT_FINAL, then the sample is returned as-is
1354 * with its SMP_F_MAY_CHANGE flag so that the caller can check it and decide to
1355 * take actions (eg: wait longer). If a sample could not be found or could not
Willy Tarreau6bcb0a82014-07-30 08:56:35 +02001356 * be converted, NULL is returned. The caller MUST NOT use the sample if the
1357 * SMP_F_MAY_CHANGE flag is present, as it is used only as a hint that there is
1358 * still hope to get it after waiting longer, and is not converted to string.
1359 * The possible output combinations are the following :
1360 *
1361 * return MAY_CHANGE FINAL Meaning for the sample
1362 * NULL * * Not present and will never be (eg: header)
1363 * smp 0 * Final value converted (eg: header)
1364 * smp 1 0 Not present yet, may appear later (eg: header)
1365 * smp 1 1 never happens (either flag is cleared on output)
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001366 */
Adis Nezirovic79beb242015-07-06 15:41:02 +02001367struct sample *sample_fetch_as_type(struct proxy *px, struct session *sess,
Willy Tarreau192252e2015-04-04 01:47:55 +02001368 struct stream *strm, unsigned int opt,
Adis Nezirovic79beb242015-07-06 15:41:02 +02001369 struct sample_expr *expr, int smp_type)
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001370{
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001371 struct sample *smp = &temp_smp;
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001372
Willy Tarreau6c616e02014-06-25 16:56:41 +02001373 memset(smp, 0, sizeof(*smp));
1374
Willy Tarreau192252e2015-04-04 01:47:55 +02001375 if (!sample_process(px, sess, strm, opt, expr, smp)) {
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001376 if ((smp->flags & SMP_F_MAY_CHANGE) && !(opt & SMP_OPT_FINAL))
1377 return smp;
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001378 return NULL;
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001379 }
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001380
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001381 if (!sample_casts[smp->data.type][smp_type])
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001382 return NULL;
1383
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001384 if (!sample_casts[smp->data.type][smp_type](smp))
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001385 return NULL;
1386
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001387 smp->flags &= ~SMP_F_MAY_CHANGE;
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001388 return smp;
1389}
1390
Christopher Faulet476e5d02016-10-26 11:34:47 +02001391static void release_sample_arg(struct arg *p)
1392{
1393 struct arg *p_back = p;
1394
1395 if (!p)
1396 return;
1397
1398 while (p->type != ARGT_STOP) {
1399 if (p->type == ARGT_STR || p->unresolved) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001400 free(p->data.str.area);
1401 p->data.str.area = NULL;
Christopher Faulet476e5d02016-10-26 11:34:47 +02001402 p->unresolved = 0;
1403 }
1404 else if (p->type == ARGT_REG) {
1405 if (p->data.reg) {
1406 regex_free(p->data.reg);
1407 free(p->data.reg);
1408 p->data.reg = NULL;
1409 }
1410 }
1411 p++;
1412 }
1413
1414 if (p_back != empty_arg_list)
1415 free(p_back);
1416}
1417
1418void release_sample_expr(struct sample_expr *expr)
1419{
1420 struct sample_conv_expr *conv_expr, *conv_exprb;
1421
1422 if (!expr)
1423 return;
1424
1425 list_for_each_entry_safe(conv_expr, conv_exprb, &expr->conv_exprs, list)
1426 release_sample_arg(conv_expr->arg_p);
1427 release_sample_arg(expr->arg_p);
1428 free(expr);
1429}
1430
Emeric Brun107ca302010-01-04 16:16:05 +01001431/*****************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +02001432/* Sample format convert functions */
Willy Tarreaub8c8f1f2012-04-23 22:38:26 +02001433/* These functions set the data type on return. */
Emeric Brun107ca302010-01-04 16:16:05 +01001434/*****************************************************************/
1435
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001436#ifdef DEBUG_EXPR
1437static int sample_conv_debug(const struct arg *arg_p, struct sample *smp, void *private)
1438{
1439 int i;
1440 struct sample tmp;
1441
1442 if (!(global.mode & MODE_QUIET) || (global.mode & (MODE_VERBOSE | MODE_STARTING))) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001443 fprintf(stderr, "[debug converter] type: %s ", smp_to_type[smp->data.type]);
1444 if (!sample_casts[smp->data.type][SMP_T_STR]) {
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001445 fprintf(stderr, "(undisplayable)");
1446 } else {
1447
1448 /* Copy sample fetch. This put the sample as const, the
1449 * cast will copy data if a transformation is required.
1450 */
1451 memcpy(&tmp, smp, sizeof(struct sample));
1452 tmp.flags = SMP_F_CONST;
1453
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001454 if (!sample_casts[smp->data.type][SMP_T_STR](&tmp))
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001455 fprintf(stderr, "(undisplayable)");
1456
1457 else {
1458 /* Display the displayable chars*. */
1459 fprintf(stderr, "<");
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001460 for (i = 0; i < tmp.data.u.str.data; i++) {
1461 if (isprint(tmp.data.u.str.area[i]))
1462 fputc(tmp.data.u.str.area[i],
1463 stderr);
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001464 else
1465 fputc('.', stderr);
1466 }
1467 }
1468 fprintf(stderr, ">\n");
1469 }
1470 }
1471 return 1;
1472}
1473#endif
1474
Holger Just1bfc24b2017-05-06 00:56:53 +02001475static int sample_conv_base642bin(const struct arg *arg_p, struct sample *smp, void *private)
1476{
Willy Tarreau83061a82018-07-13 11:56:34 +02001477 struct buffer *trash = get_trash_chunk();
Holger Just1bfc24b2017-05-06 00:56:53 +02001478 int bin_len;
1479
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001480 trash->data = 0;
1481 bin_len = base64dec(smp->data.u.str.area, smp->data.u.str.data,
1482 trash->area, trash->size);
Holger Just1bfc24b2017-05-06 00:56:53 +02001483 if (bin_len < 0)
1484 return 0;
1485
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001486 trash->data = bin_len;
Holger Just1bfc24b2017-05-06 00:56:53 +02001487 smp->data.u.str = *trash;
1488 smp->data.type = SMP_T_BIN;
1489 smp->flags &= ~SMP_F_CONST;
1490 return 1;
1491}
1492
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001493static int sample_conv_bin2base64(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brun53d1a982014-04-30 18:21:37 +02001494{
Willy Tarreau83061a82018-07-13 11:56:34 +02001495 struct buffer *trash = get_trash_chunk();
Emeric Brun53d1a982014-04-30 18:21:37 +02001496 int b64_len;
1497
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001498 trash->data = 0;
1499 b64_len = a2base64(smp->data.u.str.area, smp->data.u.str.data,
1500 trash->area, trash->size);
Emeric Brun53d1a982014-04-30 18:21:37 +02001501 if (b64_len < 0)
1502 return 0;
1503
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001504 trash->data = b64_len;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001505 smp->data.u.str = *trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001506 smp->data.type = SMP_T_STR;
Emeric Brun53d1a982014-04-30 18:21:37 +02001507 smp->flags &= ~SMP_F_CONST;
1508 return 1;
1509}
1510
Dragan Dosen6e5a9ca2017-10-24 09:18:23 +02001511static int sample_conv_sha1(const struct arg *arg_p, struct sample *smp, void *private)
1512{
1513 blk_SHA_CTX ctx;
Willy Tarreau83061a82018-07-13 11:56:34 +02001514 struct buffer *trash = get_trash_chunk();
Dragan Dosen6e5a9ca2017-10-24 09:18:23 +02001515
1516 memset(&ctx, 0, sizeof(ctx));
1517
1518 blk_SHA1_Init(&ctx);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001519 blk_SHA1_Update(&ctx, smp->data.u.str.area, smp->data.u.str.data);
1520 blk_SHA1_Final((unsigned char *) trash->area, &ctx);
Dragan Dosen6e5a9ca2017-10-24 09:18:23 +02001521
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001522 trash->data = 20;
Dragan Dosen6e5a9ca2017-10-24 09:18:23 +02001523 smp->data.u.str = *trash;
1524 smp->data.type = SMP_T_BIN;
1525 smp->flags &= ~SMP_F_CONST;
1526 return 1;
1527}
1528
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001529static int sample_conv_bin2hex(const struct arg *arg_p, struct sample *smp, void *private)
Thierry FOURNIER2f49d6d2014-03-12 15:01:52 +01001530{
Willy Tarreau83061a82018-07-13 11:56:34 +02001531 struct buffer *trash = get_trash_chunk();
Thierry FOURNIER2f49d6d2014-03-12 15:01:52 +01001532 unsigned char c;
1533 int ptr = 0;
1534
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001535 trash->data = 0;
1536 while (ptr < smp->data.u.str.data && trash->data <= trash->size - 2) {
1537 c = smp->data.u.str.area[ptr++];
1538 trash->area[trash->data++] = hextab[(c >> 4) & 0xF];
1539 trash->area[trash->data++] = hextab[c & 0xF];
Thierry FOURNIER2f49d6d2014-03-12 15:01:52 +01001540 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001541 smp->data.u.str = *trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001542 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01001543 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIER2f49d6d2014-03-12 15:01:52 +01001544 return 1;
1545}
1546
Dragan Dosen3f957b22017-10-24 09:27:34 +02001547static int sample_conv_hex2int(const struct arg *arg_p, struct sample *smp, void *private)
1548{
1549 long long int n = 0;
1550 int i, c;
1551
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001552 for (i = 0; i < smp->data.u.str.data; i++) {
1553 if ((c = hex2i(smp->data.u.str.area[i])) < 0)
Dragan Dosen3f957b22017-10-24 09:27:34 +02001554 return 0;
1555 n = (n << 4) + c;
1556 }
1557
1558 smp->data.u.sint = n;
1559 smp->data.type = SMP_T_SINT;
1560 smp->flags &= ~SMP_F_CONST;
1561 return 1;
1562}
1563
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001564/* hashes the binary input into a 32-bit unsigned int */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001565static int sample_conv_djb2(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001566{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001567 smp->data.u.sint = hash_djb2(smp->data.u.str.area,
1568 smp->data.u.str.data);
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001569 if (arg_p && arg_p->data.sint)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001570 smp->data.u.sint = full_hash(smp->data.u.sint);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001571 smp->data.type = SMP_T_SINT;
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001572 return 1;
1573}
1574
Willy Tarreau60a2ee72017-12-15 07:13:48 +01001575static int sample_conv_length(const struct arg *arg_p, struct sample *smp, void *private)
Etienne Carriereed0d24e2017-12-13 13:41:34 +01001576{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001577 int i = smp->data.u.str.data;
Etienne Carriereed0d24e2017-12-13 13:41:34 +01001578 smp->data.u.sint = i;
1579 smp->data.type = SMP_T_SINT;
1580 return 1;
1581}
1582
1583
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001584static int sample_conv_str2lower(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brun107ca302010-01-04 16:16:05 +01001585{
1586 int i;
1587
Willy Tarreauf0645dc2016-08-09 14:29:38 +02001588 if (!smp_make_rw(smp))
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01001589 return 0;
1590
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001591 for (i = 0; i < smp->data.u.str.data; i++) {
1592 if ((smp->data.u.str.area[i] >= 'A') && (smp->data.u.str.area[i] <= 'Z'))
1593 smp->data.u.str.area[i] += 'a' - 'A';
Emeric Brun107ca302010-01-04 16:16:05 +01001594 }
1595 return 1;
1596}
1597
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001598static int sample_conv_str2upper(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brun107ca302010-01-04 16:16:05 +01001599{
1600 int i;
1601
Willy Tarreauf0645dc2016-08-09 14:29:38 +02001602 if (!smp_make_rw(smp))
Emeric Brun485479d2010-09-23 18:02:19 +02001603 return 0;
1604
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001605 for (i = 0; i < smp->data.u.str.data; i++) {
1606 if ((smp->data.u.str.area[i] >= 'a') && (smp->data.u.str.area[i] <= 'z'))
1607 smp->data.u.str.area[i] += 'A' - 'a';
Emeric Brun107ca302010-01-04 16:16:05 +01001608 }
1609 return 1;
1610}
1611
Tim Duesterhus1478aa72018-01-25 16:24:51 +01001612/* takes the IPv4 mask in args[0] and an optional IPv6 mask in args[1] */
1613static int sample_conv_ipmask(const struct arg *args, struct sample *smp, void *private)
Willy Tarreaud31d6eb2010-01-26 18:01:41 +01001614{
Tim Duesterhus1478aa72018-01-25 16:24:51 +01001615 /* Attempt to convert to IPv4 to apply the correct mask. */
1616 c_ipv62ip(smp);
1617
1618 if (smp->data.type == SMP_T_IPV4) {
1619 smp->data.u.ipv4.s_addr &= args[0].data.ipv4.s_addr;
1620 smp->data.type = SMP_T_IPV4;
1621 }
1622 else if (smp->data.type == SMP_T_IPV6) {
1623 /* IPv6 cannot be converted without an IPv6 mask. */
1624 if (args[1].type != ARGT_IPV6)
1625 return 0;
1626
1627 *(uint32_t*)&smp->data.u.ipv6.s6_addr[0] &= *(uint32_t*)&args[1].data.ipv6.s6_addr[0];
1628 *(uint32_t*)&smp->data.u.ipv6.s6_addr[4] &= *(uint32_t*)&args[1].data.ipv6.s6_addr[4];
1629 *(uint32_t*)&smp->data.u.ipv6.s6_addr[8] &= *(uint32_t*)&args[1].data.ipv6.s6_addr[8];
1630 *(uint32_t*)&smp->data.u.ipv6.s6_addr[12] &= *(uint32_t*)&args[1].data.ipv6.s6_addr[12];
1631 smp->data.type = SMP_T_IPV6;
1632 }
1633
Willy Tarreaud31d6eb2010-01-26 18:01:41 +01001634 return 1;
1635}
1636
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001637/* takes an UINT value on input supposed to represent the time since EPOCH,
1638 * adds an optional offset found in args[1] and emits a string representing
1639 * the local time in the format specified in args[1] using strftime().
1640 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001641static int sample_conv_ltime(const struct arg *args, struct sample *smp, void *private)
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001642{
Willy Tarreau83061a82018-07-13 11:56:34 +02001643 struct buffer *temp;
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001644 /* With high numbers, the date returned can be negative, the 55 bits mask prevent this. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001645 time_t curr_date = smp->data.u.sint & 0x007fffffffffffffLL;
Thierry FOURNIERfac9ccf2015-07-08 00:15:20 +02001646 struct tm *tm;
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001647
1648 /* add offset */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001649 if (args[1].type == ARGT_SINT)
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001650 curr_date += args[1].data.sint;
1651
Thierry FOURNIERfac9ccf2015-07-08 00:15:20 +02001652 tm = localtime(&curr_date);
1653 if (!tm)
1654 return 0;
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001655 temp = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001656 temp->data = strftime(temp->area, temp->size, args[0].data.str.area,
1657 tm);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001658 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001659 smp->data.type = SMP_T_STR;
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001660 return 1;
1661}
1662
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001663/* hashes the binary input into a 32-bit unsigned int */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001664static int sample_conv_sdbm(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001665{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001666 smp->data.u.sint = hash_sdbm(smp->data.u.str.area,
1667 smp->data.u.str.data);
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001668 if (arg_p && arg_p->data.sint)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001669 smp->data.u.sint = full_hash(smp->data.u.sint);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001670 smp->data.type = SMP_T_SINT;
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001671 return 1;
1672}
1673
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001674/* takes an UINT value on input supposed to represent the time since EPOCH,
1675 * adds an optional offset found in args[1] and emits a string representing
1676 * the UTC date in the format specified in args[1] using strftime().
1677 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001678static int sample_conv_utime(const struct arg *args, struct sample *smp, void *private)
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001679{
Willy Tarreau83061a82018-07-13 11:56:34 +02001680 struct buffer *temp;
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001681 /* With high numbers, the date returned can be negative, the 55 bits mask prevent this. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001682 time_t curr_date = smp->data.u.sint & 0x007fffffffffffffLL;
Thierry FOURNIERfac9ccf2015-07-08 00:15:20 +02001683 struct tm *tm;
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001684
1685 /* add offset */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001686 if (args[1].type == ARGT_SINT)
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001687 curr_date += args[1].data.sint;
1688
Thierry FOURNIERfac9ccf2015-07-08 00:15:20 +02001689 tm = gmtime(&curr_date);
1690 if (!tm)
1691 return 0;
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001692 temp = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001693 temp->data = strftime(temp->area, temp->size, args[0].data.str.area,
1694 tm);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001695 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001696 smp->data.type = SMP_T_STR;
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001697 return 1;
1698}
1699
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001700/* hashes the binary input into a 32-bit unsigned int */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001701static int sample_conv_wt6(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001702{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001703 smp->data.u.sint = hash_wt6(smp->data.u.str.area,
1704 smp->data.u.str.data);
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001705 if (arg_p && arg_p->data.sint)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001706 smp->data.u.sint = full_hash(smp->data.u.sint);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001707 smp->data.type = SMP_T_SINT;
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001708 return 1;
1709}
1710
Thierry FOURNIER01e09742016-12-26 11:46:11 +01001711/* hashes the binary input into a 32-bit unsigned int using xxh.
1712 * The seed of the hash defaults to 0 but can be changd in argument 1.
1713 */
1714static int sample_conv_xxh32(const struct arg *arg_p, struct sample *smp, void *private)
1715{
1716 unsigned int seed;
1717
1718 if (arg_p && arg_p->data.sint)
1719 seed = arg_p->data.sint;
1720 else
1721 seed = 0;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001722 smp->data.u.sint = XXH32(smp->data.u.str.area, smp->data.u.str.data,
1723 seed);
Thierry FOURNIER01e09742016-12-26 11:46:11 +01001724 smp->data.type = SMP_T_SINT;
1725 return 1;
1726}
1727
1728/* hashes the binary input into a 64-bit unsigned int using xxh.
1729 * In fact, the function returns a 64 bit unsigned, but the sample
1730 * storage of haproxy only proposes 64-bits signed, so the value is
1731 * cast as signed. This cast doesn't impact the hash repartition.
1732 * The seed of the hash defaults to 0 but can be changd in argument 1.
1733 */
1734static int sample_conv_xxh64(const struct arg *arg_p, struct sample *smp, void *private)
1735{
1736 unsigned long long int seed;
1737
1738 if (arg_p && arg_p->data.sint)
1739 seed = (unsigned long long int)arg_p->data.sint;
1740 else
1741 seed = 0;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001742 smp->data.u.sint = (long long int)XXH64(smp->data.u.str.area,
1743 smp->data.u.str.data, seed);
Thierry FOURNIER01e09742016-12-26 11:46:11 +01001744 smp->data.type = SMP_T_SINT;
1745 return 1;
1746}
1747
Willy Tarreau80599772015-01-20 19:35:24 +01001748/* hashes the binary input into a 32-bit unsigned int */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001749static int sample_conv_crc32(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau80599772015-01-20 19:35:24 +01001750{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001751 smp->data.u.sint = hash_crc32(smp->data.u.str.area,
1752 smp->data.u.str.data);
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001753 if (arg_p && arg_p->data.sint)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001754 smp->data.u.sint = full_hash(smp->data.u.sint);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001755 smp->data.type = SMP_T_SINT;
Willy Tarreau80599772015-01-20 19:35:24 +01001756 return 1;
1757}
1758
Emmanuel Hocdet50791a72018-03-21 11:19:01 +01001759/* hashes the binary input into crc32c (RFC4960, Appendix B [8].) */
1760static int sample_conv_crc32c(const struct arg *arg_p, struct sample *smp, void *private)
1761{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001762 smp->data.u.sint = hash_crc32c(smp->data.u.str.area,
1763 smp->data.u.str.data);
Emmanuel Hocdet50791a72018-03-21 11:19:01 +01001764 if (arg_p && arg_p->data.sint)
1765 smp->data.u.sint = full_hash(smp->data.u.sint);
1766 smp->data.type = SMP_T_SINT;
1767 return 1;
1768}
1769
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001770/* This function escape special json characters. The returned string can be
1771 * safely set between two '"' and used as json string. The json string is
1772 * defined like this:
1773 *
1774 * any Unicode character except '"' or '\' or control character
1775 * \", \\, \/, \b, \f, \n, \r, \t, \u + four-hex-digits
1776 *
1777 * The enum input_type contain all the allowed mode for decoding the input
1778 * string.
1779 */
1780enum input_type {
1781 IT_ASCII = 0,
1782 IT_UTF8,
1783 IT_UTF8S,
1784 IT_UTF8P,
1785 IT_UTF8PS,
1786};
1787static int sample_conv_json_check(struct arg *arg, struct sample_conv *conv,
1788 const char *file, int line, char **err)
1789{
1790 if (!arg) {
1791 memprintf(err, "Unexpected empty arg list");
1792 return 0;
1793 }
1794
1795 if (arg->type != ARGT_STR) {
1796 memprintf(err, "Unexpected arg type");
1797 return 0;
1798 }
1799
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001800 if (strcmp(arg->data.str.area, "") == 0) {
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001801 arg->type = ARGT_SINT;
1802 arg->data.sint = IT_ASCII;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001803 return 1;
1804 }
1805
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001806 else if (strcmp(arg->data.str.area, "ascii") == 0) {
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001807 arg->type = ARGT_SINT;
1808 arg->data.sint = IT_ASCII;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001809 return 1;
1810 }
1811
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001812 else if (strcmp(arg->data.str.area, "utf8") == 0) {
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001813 arg->type = ARGT_SINT;
1814 arg->data.sint = IT_UTF8;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001815 return 1;
1816 }
1817
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001818 else if (strcmp(arg->data.str.area, "utf8s") == 0) {
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001819 arg->type = ARGT_SINT;
1820 arg->data.sint = IT_UTF8S;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001821 return 1;
1822 }
1823
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001824 else if (strcmp(arg->data.str.area, "utf8p") == 0) {
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001825 arg->type = ARGT_SINT;
1826 arg->data.sint = IT_UTF8P;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001827 return 1;
1828 }
1829
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001830 else if (strcmp(arg->data.str.area, "utf8ps") == 0) {
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001831 arg->type = ARGT_SINT;
1832 arg->data.sint = IT_UTF8PS;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001833 return 1;
1834 }
1835
Thierry FOURNIER / OZON.IOa69c9122016-11-23 01:13:57 +01001836 memprintf(err, "Unexpected input code type. "
1837 "Allowed value are 'ascii', 'utf8', 'utf8s', 'utf8p' and 'utf8ps'");
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001838 return 0;
1839}
1840
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001841static int sample_conv_json(const struct arg *arg_p, struct sample *smp, void *private)
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001842{
Willy Tarreau83061a82018-07-13 11:56:34 +02001843 struct buffer *temp;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001844 char _str[7]; /* \u + 4 hex digit + null char for sprintf. */
1845 const char *str;
1846 int len;
1847 enum input_type input_type = IT_ASCII;
1848 unsigned int c;
1849 unsigned int ret;
1850 char *p;
1851
1852 if (arg_p)
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001853 input_type = arg_p->data.sint;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001854
1855 temp = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001856 temp->data = 0;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001857
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001858 p = smp->data.u.str.area;
1859 while (p < smp->data.u.str.area + smp->data.u.str.data) {
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001860
1861 if (input_type == IT_ASCII) {
1862 /* Read input as ASCII. */
1863 c = *(unsigned char *)p;
1864 p++;
1865 }
1866 else {
1867 /* Read input as UTF8. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001868 ret = utf8_next(p,
1869 smp->data.u.str.data - ( p - smp->data.u.str.area),
1870 &c);
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001871 p += utf8_return_length(ret);
1872
1873 if (input_type == IT_UTF8 && utf8_return_code(ret) != UTF8_CODE_OK)
1874 return 0;
1875 if (input_type == IT_UTF8S && utf8_return_code(ret) != UTF8_CODE_OK)
1876 continue;
1877 if (input_type == IT_UTF8P && utf8_return_code(ret) & (UTF8_CODE_INVRANGE|UTF8_CODE_BADSEQ))
1878 return 0;
1879 if (input_type == IT_UTF8PS && utf8_return_code(ret) & (UTF8_CODE_INVRANGE|UTF8_CODE_BADSEQ))
1880 continue;
1881
1882 /* Check too big values. */
1883 if ((unsigned int)c > 0xffff) {
1884 if (input_type == IT_UTF8 || input_type == IT_UTF8P)
1885 return 0;
1886 continue;
1887 }
1888 }
1889
1890 /* Convert character. */
1891 if (c == '"') {
1892 len = 2;
1893 str = "\\\"";
1894 }
1895 else if (c == '\\') {
1896 len = 2;
1897 str = "\\\\";
1898 }
1899 else if (c == '/') {
1900 len = 2;
1901 str = "\\/";
1902 }
1903 else if (c == '\b') {
1904 len = 2;
1905 str = "\\b";
1906 }
1907 else if (c == '\f') {
1908 len = 2;
1909 str = "\\f";
1910 }
1911 else if (c == '\r') {
1912 len = 2;
1913 str = "\\r";
1914 }
1915 else if (c == '\n') {
1916 len = 2;
1917 str = "\\n";
1918 }
1919 else if (c == '\t') {
1920 len = 2;
1921 str = "\\t";
1922 }
1923 else if (c > 0xff || !isprint(c)) {
1924 /* isprint generate a segfault if c is too big. The man says that
1925 * c must have the value of an unsigned char or EOF.
1926 */
1927 len = 6;
1928 _str[0] = '\\';
1929 _str[1] = 'u';
1930 snprintf(&_str[2], 5, "%04x", (unsigned short)c);
1931 str = _str;
1932 }
1933 else {
1934 len = 1;
1935 str = (char *)&c;
1936 }
1937
1938 /* Check length */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001939 if (temp->data + len > temp->size)
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001940 return 0;
1941
1942 /* Copy string. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001943 memcpy(temp->area + temp->data, str, len);
1944 temp->data += len;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001945 }
1946
1947 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001948 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001949 smp->data.type = SMP_T_STR;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001950
1951 return 1;
1952}
1953
Emeric Brun54c4ac82014-11-03 15:32:43 +01001954/* This sample function is designed to extract some bytes from an input buffer.
1955 * First arg is the offset.
1956 * Optional second arg is the length to truncate */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001957static int sample_conv_bytes(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brun54c4ac82014-11-03 15:32:43 +01001958{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001959 if (smp->data.u.str.data <= arg_p[0].data.sint) {
1960 smp->data.u.str.data = 0;
Emeric Brun54c4ac82014-11-03 15:32:43 +01001961 return 1;
1962 }
1963
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001964 if (smp->data.u.str.size)
1965 smp->data.u.str.size -= arg_p[0].data.sint;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001966 smp->data.u.str.data -= arg_p[0].data.sint;
1967 smp->data.u.str.area += arg_p[0].data.sint;
Emeric Brun54c4ac82014-11-03 15:32:43 +01001968
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001969 if ((arg_p[1].type == ARGT_SINT) && (arg_p[1].data.sint < smp->data.u.str.data))
1970 smp->data.u.str.data = arg_p[1].data.sint;
Emeric Brun54c4ac82014-11-03 15:32:43 +01001971
1972 return 1;
1973}
1974
Emeric Brunf399b0d2014-11-03 17:07:03 +01001975static int sample_conv_field_check(struct arg *args, struct sample_conv *conv,
1976 const char *file, int line, char **err)
1977{
1978 struct arg *arg = args;
1979
1980 if (!arg) {
1981 memprintf(err, "Unexpected empty arg list");
1982 return 0;
1983 }
1984
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001985 if (arg->type != ARGT_SINT) {
Emeric Brunf399b0d2014-11-03 17:07:03 +01001986 memprintf(err, "Unexpected arg type");
1987 return 0;
1988 }
1989
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001990 if (!arg->data.sint) {
Emeric Brunf399b0d2014-11-03 17:07:03 +01001991 memprintf(err, "Unexpected value 0 for index");
1992 return 0;
1993 }
1994
1995 arg++;
1996
1997 if (arg->type != ARGT_STR) {
1998 memprintf(err, "Unexpected arg type");
1999 return 0;
2000 }
2001
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002002 if (!arg->data.str.data) {
Emeric Brunf399b0d2014-11-03 17:07:03 +01002003 memprintf(err, "Empty separators list");
2004 return 0;
2005 }
2006
2007 return 1;
2008}
2009
2010/* This sample function is designed to a return selected part of a string (field).
2011 * First arg is the index of the field (start at 1)
2012 * Second arg is a char list of separators (type string)
2013 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002014static int sample_conv_field(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brunf399b0d2014-11-03 17:07:03 +01002015{
Marcin Deranek9631a282018-04-16 14:30:46 +02002016 int field;
Emeric Brunf399b0d2014-11-03 17:07:03 +01002017 char *start, *end;
2018 int i;
Marcin Deranek9631a282018-04-16 14:30:46 +02002019 int count = (arg_p[2].type == ARGT_SINT) ? arg_p[2].data.sint : 1;
Emeric Brunf399b0d2014-11-03 17:07:03 +01002020
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002021 if (!arg_p[0].data.sint)
Emeric Brunf399b0d2014-11-03 17:07:03 +01002022 return 0;
2023
Marcin Deranek9631a282018-04-16 14:30:46 +02002024 if (arg_p[0].data.sint < 0) {
2025 field = -1;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002026 end = start = smp->data.u.str.area + smp->data.u.str.data;
2027 while (start > smp->data.u.str.area) {
2028 for (i = 0 ; i < arg_p[1].data.str.data; i++) {
2029 if (*(start-1) == arg_p[1].data.str.area[i]) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002030 if (field == arg_p[0].data.sint) {
2031 if (count == 1)
2032 goto found;
2033 else if (count > 1)
2034 count--;
2035 } else {
2036 end = start-1;
2037 field--;
2038 }
2039 break;
2040 }
Emeric Brunf399b0d2014-11-03 17:07:03 +01002041 }
Marcin Deranek9631a282018-04-16 14:30:46 +02002042 start--;
Emeric Brunf399b0d2014-11-03 17:07:03 +01002043 }
Marcin Deranek9631a282018-04-16 14:30:46 +02002044 } else {
2045 field = 1;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002046 end = start = smp->data.u.str.area;
2047 while (end - smp->data.u.str.area < smp->data.u.str.data) {
2048 for (i = 0 ; i < arg_p[1].data.str.data; i++) {
2049 if (*end == arg_p[1].data.str.area[i]) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002050 if (field == arg_p[0].data.sint) {
2051 if (count == 1)
2052 goto found;
2053 else if (count > 1)
2054 count--;
2055 } else {
2056 start = end+1;
2057 field++;
2058 }
2059 break;
2060 }
2061 }
2062 end++;
2063 }
Emeric Brunf399b0d2014-11-03 17:07:03 +01002064 }
2065
2066 /* Field not found */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002067 if (field != arg_p[0].data.sint) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002068 smp->data.u.str.data = 0;
Emeric Brunf399b0d2014-11-03 17:07:03 +01002069 return 1;
2070 }
2071found:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002072 smp->data.u.str.data = end - start;
Emeric Brunf399b0d2014-11-03 17:07:03 +01002073 /* If ret string is len 0, no need to
2074 change pointers or to update size */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002075 if (!smp->data.u.str.data)
Emeric Brunf399b0d2014-11-03 17:07:03 +01002076 return 1;
2077
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002078 smp->data.u.str.area = start;
Emeric Brunf399b0d2014-11-03 17:07:03 +01002079
2080 /* Compute remaining size if needed
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002081 Note: smp->data.u.str.size cannot be set to 0 */
2082 if (smp->data.u.str.size)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002083 smp->data.u.str.size -= start - smp->data.u.str.area;
Emeric Brunf399b0d2014-11-03 17:07:03 +01002084
2085 return 1;
2086}
2087
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002088/* This sample function is designed to return a word from a string.
2089 * First arg is the index of the word (start at 1)
2090 * Second arg is a char list of words separators (type string)
2091 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002092static int sample_conv_word(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002093{
Marcin Deranek9631a282018-04-16 14:30:46 +02002094 int word;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002095 char *start, *end;
2096 int i, issep, inword;
Marcin Deranek9631a282018-04-16 14:30:46 +02002097 int count = (arg_p[2].type == ARGT_SINT) ? arg_p[2].data.sint : 1;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002098
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002099 if (!arg_p[0].data.sint)
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002100 return 0;
2101
2102 word = 0;
2103 inword = 0;
Marcin Deranek9631a282018-04-16 14:30:46 +02002104 if (arg_p[0].data.sint < 0) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002105 end = start = smp->data.u.str.area + smp->data.u.str.data;
2106 while (start > smp->data.u.str.area) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002107 issep = 0;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002108 for (i = 0 ; i < arg_p[1].data.str.data; i++) {
2109 if (*(start-1) == arg_p[1].data.str.area[i]) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002110 issep = 1;
2111 break;
2112 }
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002113 }
Marcin Deranek9631a282018-04-16 14:30:46 +02002114 if (!inword) {
2115 if (!issep) {
2116 if (word != arg_p[0].data.sint) {
2117 word--;
2118 end = start;
2119 }
2120 inword = 1;
2121 }
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002122 }
Marcin Deranek9631a282018-04-16 14:30:46 +02002123 else if (issep) {
Willy Tarreau9eb2a4a2018-04-19 10:33:28 +02002124 if (word == arg_p[0].data.sint) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002125 if (count == 1)
2126 goto found;
2127 else if (count > 1)
2128 count--;
Willy Tarreau9eb2a4a2018-04-19 10:33:28 +02002129 }
Marcin Deranek9631a282018-04-16 14:30:46 +02002130 inword = 0;
2131 }
2132 start--;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002133 }
Marcin Deranek9631a282018-04-16 14:30:46 +02002134 } else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002135 end = start = smp->data.u.str.area;
2136 while (end - smp->data.u.str.area < smp->data.u.str.data) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002137 issep = 0;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002138 for (i = 0 ; i < arg_p[1].data.str.data; i++) {
2139 if (*end == arg_p[1].data.str.area[i]) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002140 issep = 1;
2141 break;
2142 }
2143 }
2144 if (!inword) {
2145 if (!issep) {
2146 if (word != arg_p[0].data.sint) {
2147 word++;
2148 start = end;
2149 }
2150 inword = 1;
2151 }
2152 }
2153 else if (issep) {
Willy Tarreau9eb2a4a2018-04-19 10:33:28 +02002154 if (word == arg_p[0].data.sint) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002155 if (count == 1)
2156 goto found;
2157 else if (count > 1)
2158 count--;
Willy Tarreau9eb2a4a2018-04-19 10:33:28 +02002159 }
Marcin Deranek9631a282018-04-16 14:30:46 +02002160 inword = 0;
2161 }
2162 end++;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002163 }
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002164 }
2165
2166 /* Field not found */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002167 if (word != arg_p[0].data.sint) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002168 smp->data.u.str.data = 0;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002169 return 1;
2170 }
2171found:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002172 smp->data.u.str.data = end - start;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002173 /* If ret string is len 0, no need to
2174 change pointers or to update size */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002175 if (!smp->data.u.str.data)
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002176 return 1;
2177
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002178 smp->data.u.str.area = start;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002179
2180 /* Compute remaining size if needed
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002181 Note: smp->data.u.str.size cannot be set to 0 */
2182 if (smp->data.u.str.size)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002183 smp->data.u.str.size -= start - smp->data.u.str.area;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002184
2185 return 1;
2186}
2187
Willy Tarreau7eda8492015-01-20 19:47:06 +01002188static int sample_conv_regsub_check(struct arg *args, struct sample_conv *conv,
2189 const char *file, int line, char **err)
2190{
2191 struct arg *arg = args;
2192 char *p;
2193 int len;
2194
2195 /* arg0 is a regex, it uses type_flag for ICASE and global match */
2196 arg[0].type_flags = 0;
2197
2198 if (arg[2].type != ARGT_STR)
2199 return 1;
2200
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002201 p = arg[2].data.str.area;
2202 len = arg[2].data.str.data;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002203 while (len) {
2204 if (*p == 'i') {
2205 arg[0].type_flags |= ARGF_REG_ICASE;
2206 }
2207 else if (*p == 'g') {
2208 arg[0].type_flags |= ARGF_REG_GLOB;
2209 }
2210 else {
2211 memprintf(err, "invalid regex flag '%c', only 'i' and 'g' are supported", *p);
2212 return 0;
2213 }
2214 p++;
2215 len--;
2216 }
2217 return 1;
2218}
2219
2220/* This sample function is designed to do the equivalent of s/match/replace/ on
2221 * the input string. It applies a regex and restarts from the last matched
2222 * location until nothing matches anymore. First arg is the regex to apply to
2223 * the input string, second arg is the replacement expression.
2224 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002225static int sample_conv_regsub(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau7eda8492015-01-20 19:47:06 +01002226{
2227 char *start, *end;
2228 struct my_regex *reg = arg_p[0].data.reg;
2229 regmatch_t pmatch[MAX_MATCH];
Willy Tarreau83061a82018-07-13 11:56:34 +02002230 struct buffer *trash = get_trash_chunk();
Willy Tarreau7eda8492015-01-20 19:47:06 +01002231 int flag, max;
2232 int found;
2233
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002234 start = smp->data.u.str.area;
2235 end = start + smp->data.u.str.data;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002236
2237 flag = 0;
2238 while (1) {
2239 /* check for last round which is used to copy remaining parts
2240 * when not running in global replacement mode.
2241 */
2242 found = 0;
2243 if ((arg_p[0].type_flags & ARGF_REG_GLOB) || !(flag & REG_NOTBOL)) {
2244 /* Note: we can have start == end on empty strings or at the end */
2245 found = regex_exec_match2(reg, start, end - start, MAX_MATCH, pmatch, flag);
2246 }
2247
2248 if (!found)
2249 pmatch[0].rm_so = end - start;
2250
2251 /* copy the heading non-matching part (which may also be the tail if nothing matches) */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002252 max = trash->size - trash->data;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002253 if (max && pmatch[0].rm_so > 0) {
2254 if (max > pmatch[0].rm_so)
2255 max = pmatch[0].rm_so;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002256 memcpy(trash->area + trash->data, start, max);
2257 trash->data += max;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002258 }
2259
2260 if (!found)
2261 break;
2262
2263 /* replace the matching part */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002264 max = trash->size - trash->data;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002265 if (max) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002266 if (max > arg_p[1].data.str.data)
2267 max = arg_p[1].data.str.data;
2268 memcpy(trash->area + trash->data,
2269 arg_p[1].data.str.area, max);
2270 trash->data += max;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002271 }
2272
2273 /* stop here if we're done with this string */
2274 if (start >= end)
2275 break;
2276
2277 /* We have a special case for matches of length 0 (eg: "x*y*").
2278 * These ones are considered to match in front of a character,
2279 * so we have to copy that character and skip to the next one.
2280 */
2281 if (!pmatch[0].rm_eo) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002282 if (trash->data < trash->size)
2283 trash->area[trash->data++] = start[pmatch[0].rm_eo];
Willy Tarreau7eda8492015-01-20 19:47:06 +01002284 pmatch[0].rm_eo++;
2285 }
2286
2287 start += pmatch[0].rm_eo;
2288 flag |= REG_NOTBOL;
2289 }
2290
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002291 smp->data.u.str = *trash;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002292 return 1;
2293}
2294
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002295/* This function check an operator entry. It expects a string.
2296 * The string can be an integer or a variable name.
2297 */
2298static int check_operator(struct arg *args, struct sample_conv *conv,
2299 const char *file, int line, char **err)
2300{
2301 const char *str;
2302 const char *end;
2303
2304 /* Try to decode a variable. */
2305 if (vars_check_arg(&args[0], NULL))
2306 return 1;
2307
2308 /* Try to convert an integer */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002309 str = args[0].data.str.area;
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002310 end = str + strlen(str);
2311 args[0].data.sint = read_int64(&str, end);
2312 if (*str != '\0') {
2313 memprintf(err, "expects an integer or a variable name");
2314 return 0;
2315 }
2316 args[0].type = ARGT_SINT;
2317 return 1;
2318}
2319
Willy Tarreau6204cd92016-03-10 16:33:04 +01002320/* This function returns a sample struct filled with an arg content.
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002321 * If the arg contain an integer, the integer is returned in the
2322 * sample. If the arg contains a variable descriptor, it returns the
2323 * variable value.
2324 *
2325 * This function returns 0 if an error occurs, otherwise it returns 1.
2326 */
Willy Tarreau6204cd92016-03-10 16:33:04 +01002327static inline int sample_conv_var2smp(const struct arg *arg, struct sample *smp)
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002328{
2329 switch (arg->type) {
2330 case ARGT_SINT:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002331 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002332 smp->data.u.sint = arg->data.sint;
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002333 return 1;
2334 case ARGT_VAR:
Willy Tarreau6204cd92016-03-10 16:33:04 +01002335 if (!vars_get_by_desc(&arg->data.var, smp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002336 return 0;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002337 if (!sample_casts[smp->data.type][SMP_T_SINT])
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002338 return 0;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002339 if (!sample_casts[smp->data.type][SMP_T_SINT](smp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002340 return 0;
2341 return 1;
2342 default:
2343 return 0;
2344 }
2345}
2346
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002347/* Takes a SINT on input, applies a binary twos complement and returns the SINT
Willy Tarreau97707872015-01-27 15:12:13 +01002348 * result.
2349 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002350static int sample_conv_binary_cpl(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002351{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002352 smp->data.u.sint = ~smp->data.u.sint;
Willy Tarreau97707872015-01-27 15:12:13 +01002353 return 1;
2354}
2355
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002356/* Takes a SINT on input, applies a binary "and" with the SINT directly in
2357 * arg_p or in the varaible described in arg_p, and returns the SINT result.
Willy Tarreau97707872015-01-27 15:12:13 +01002358 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002359static int sample_conv_binary_and(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002360{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002361 struct sample tmp;
2362
Willy Tarreau7560dd42016-03-10 16:28:58 +01002363 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreau6204cd92016-03-10 16:33:04 +01002364 if (!sample_conv_var2smp(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002365 return 0;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002366 smp->data.u.sint &= tmp.data.u.sint;
Willy Tarreau97707872015-01-27 15:12:13 +01002367 return 1;
2368}
2369
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002370/* Takes a SINT on input, applies a binary "or" with the SINT directly in
2371 * arg_p or in the varaible described in arg_p, and returns the SINT result.
Willy Tarreau97707872015-01-27 15:12:13 +01002372 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002373static int sample_conv_binary_or(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002374{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002375 struct sample tmp;
2376
Willy Tarreau7560dd42016-03-10 16:28:58 +01002377 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreau6204cd92016-03-10 16:33:04 +01002378 if (!sample_conv_var2smp(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002379 return 0;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002380 smp->data.u.sint |= tmp.data.u.sint;
Willy Tarreau97707872015-01-27 15:12:13 +01002381 return 1;
2382}
2383
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002384/* Takes a SINT on input, applies a binary "xor" with the SINT directly in
2385 * arg_p or in the varaible described in arg_p, and returns the SINT result.
Willy Tarreau97707872015-01-27 15:12:13 +01002386 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002387static int sample_conv_binary_xor(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002388{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002389 struct sample tmp;
2390
Willy Tarreau7560dd42016-03-10 16:28:58 +01002391 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreau6204cd92016-03-10 16:33:04 +01002392 if (!sample_conv_var2smp(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002393 return 0;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002394 smp->data.u.sint ^= tmp.data.u.sint;
Willy Tarreau97707872015-01-27 15:12:13 +01002395 return 1;
2396}
2397
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002398static inline long long int arith_add(long long int a, long long int b)
2399{
2400 /* Prevent overflow and makes capped calculus.
2401 * We must ensure that the check calculus doesn't
2402 * exceed the signed 64 bits limits.
2403 *
2404 * +----------+----------+
2405 * | a<0 | a>=0 |
2406 * +------+----------+----------+
2407 * | b<0 | MIN-a>b | no check |
2408 * +------+----------+----------+
2409 * | b>=0 | no check | MAX-a<b |
2410 * +------+----------+----------+
2411 */
2412 if ((a ^ b) >= 0) {
2413 /* signs are differents. */
2414 if (a < 0) {
2415 if (LLONG_MIN - a > b)
2416 return LLONG_MIN;
2417 }
2418 if (LLONG_MAX - a < b)
2419 return LLONG_MAX;
2420 }
2421 return a + b;
2422}
2423
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002424/* Takes a SINT on input, applies an arithmetic "add" with the SINT directly in
2425 * arg_p or in the varaible described in arg_p, and returns the SINT result.
Willy Tarreau97707872015-01-27 15:12:13 +01002426 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002427static int sample_conv_arith_add(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002428{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002429 struct sample tmp;
2430
Willy Tarreau7560dd42016-03-10 16:28:58 +01002431 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreau6204cd92016-03-10 16:33:04 +01002432 if (!sample_conv_var2smp(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002433 return 0;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002434 smp->data.u.sint = arith_add(smp->data.u.sint, tmp.data.u.sint);
Willy Tarreau97707872015-01-27 15:12:13 +01002435 return 1;
2436}
2437
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002438/* Takes a SINT on input, applies an arithmetic "sub" with the SINT directly in
2439 * arg_p or in the varaible described in arg_p, and returns the SINT result.
Willy Tarreau97707872015-01-27 15:12:13 +01002440 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002441static int sample_conv_arith_sub(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002442 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002443{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002444 struct sample tmp;
2445
Willy Tarreau7560dd42016-03-10 16:28:58 +01002446 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreau6204cd92016-03-10 16:33:04 +01002447 if (!sample_conv_var2smp(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002448 return 0;
2449
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002450 /* We cannot represent -LLONG_MIN because abs(LLONG_MIN) is greater
2451 * than abs(LLONG_MAX). So, the following code use LLONG_MAX in place
2452 * of -LLONG_MIN and correct the result.
2453 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002454 if (tmp.data.u.sint == LLONG_MIN) {
2455 smp->data.u.sint = arith_add(smp->data.u.sint, LLONG_MAX);
2456 if (smp->data.u.sint < LLONG_MAX)
2457 smp->data.u.sint++;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002458 return 1;
2459 }
2460
2461 /* standard substraction: we use the "add" function and negate
2462 * the second operand.
2463 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002464 smp->data.u.sint = arith_add(smp->data.u.sint, -tmp.data.u.sint);
Willy Tarreau97707872015-01-27 15:12:13 +01002465 return 1;
2466}
2467
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002468/* Takes a SINT on input, applies an arithmetic "mul" with the SINT directly in
2469 * arg_p or in the varaible described in arg_p, and returns the SINT result.
2470 * If the result makes an overflow, then the largest possible quantity is
2471 * returned.
Willy Tarreau97707872015-01-27 15:12:13 +01002472 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002473static int sample_conv_arith_mul(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002474 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002475{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002476 struct sample tmp;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002477 long long int c;
2478
Willy Tarreau7560dd42016-03-10 16:28:58 +01002479 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreau6204cd92016-03-10 16:33:04 +01002480 if (!sample_conv_var2smp(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002481 return 0;
2482
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002483 /* prevent divide by 0 during the check */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002484 if (!smp->data.u.sint || !tmp.data.u.sint) {
2485 smp->data.u.sint = 0;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002486 return 1;
2487 }
2488
2489 /* The multiply between LLONG_MIN and -1 returns a
2490 * "floting point exception".
2491 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002492 if (smp->data.u.sint == LLONG_MIN && tmp.data.u.sint == -1) {
2493 smp->data.u.sint = LLONG_MAX;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002494 return 1;
2495 }
2496
2497 /* execute standard multiplication. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002498 c = smp->data.u.sint * tmp.data.u.sint;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002499
2500 /* check for overflow and makes capped multiply. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002501 if (smp->data.u.sint != c / tmp.data.u.sint) {
2502 if ((smp->data.u.sint < 0) == (tmp.data.u.sint < 0)) {
2503 smp->data.u.sint = LLONG_MAX;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002504 return 1;
2505 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002506 smp->data.u.sint = LLONG_MIN;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002507 return 1;
2508 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002509 smp->data.u.sint = c;
Willy Tarreau97707872015-01-27 15:12:13 +01002510 return 1;
2511}
2512
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002513/* Takes a SINT on input, applies an arithmetic "div" with the SINT directly in
2514 * arg_p or in the varaible described in arg_p, and returns the SINT result.
2515 * If arg_p makes the result overflow, then the largest possible quantity is
2516 * returned.
Willy Tarreau97707872015-01-27 15:12:13 +01002517 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002518static int sample_conv_arith_div(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002519 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002520{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002521 struct sample tmp;
2522
Willy Tarreau7560dd42016-03-10 16:28:58 +01002523 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreau6204cd92016-03-10 16:33:04 +01002524 if (!sample_conv_var2smp(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002525 return 0;
2526
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002527 if (tmp.data.u.sint) {
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002528 /* The divide between LLONG_MIN and -1 returns a
2529 * "floting point exception".
2530 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002531 if (smp->data.u.sint == LLONG_MIN && tmp.data.u.sint == -1) {
2532 smp->data.u.sint = LLONG_MAX;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002533 return 1;
2534 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002535 smp->data.u.sint /= tmp.data.u.sint;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002536 return 1;
2537 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002538 smp->data.u.sint = LLONG_MAX;
Willy Tarreau97707872015-01-27 15:12:13 +01002539 return 1;
2540}
2541
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002542/* Takes a SINT on input, applies an arithmetic "mod" with the SINT directly in
2543 * arg_p or in the varaible described in arg_p, and returns the SINT result.
2544 * If arg_p makes the result overflow, then 0 is returned.
Willy Tarreau97707872015-01-27 15:12:13 +01002545 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002546static int sample_conv_arith_mod(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002547 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002548{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002549 struct sample tmp;
2550
Willy Tarreau7560dd42016-03-10 16:28:58 +01002551 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreau6204cd92016-03-10 16:33:04 +01002552 if (!sample_conv_var2smp(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002553 return 0;
2554
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002555 if (tmp.data.u.sint) {
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002556 /* The divide between LLONG_MIN and -1 returns a
2557 * "floting point exception".
2558 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002559 if (smp->data.u.sint == LLONG_MIN && tmp.data.u.sint == -1) {
2560 smp->data.u.sint = 0;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002561 return 1;
2562 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002563 smp->data.u.sint %= tmp.data.u.sint;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002564 return 1;
2565 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002566 smp->data.u.sint = 0;
Willy Tarreau97707872015-01-27 15:12:13 +01002567 return 1;
2568}
2569
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002570/* Takes an SINT on input, applies an arithmetic "neg" and returns the SINT
Willy Tarreau97707872015-01-27 15:12:13 +01002571 * result.
2572 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002573static int sample_conv_arith_neg(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002574 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002575{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002576 if (smp->data.u.sint == LLONG_MIN)
2577 smp->data.u.sint = LLONG_MAX;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002578 else
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002579 smp->data.u.sint = -smp->data.u.sint;
Willy Tarreau97707872015-01-27 15:12:13 +01002580 return 1;
2581}
2582
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002583/* Takes a SINT on input, returns true is the value is non-null, otherwise
Willy Tarreau97707872015-01-27 15:12:13 +01002584 * false. The output is a BOOL.
2585 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002586static int sample_conv_arith_bool(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002587 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002588{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002589 smp->data.u.sint = !!smp->data.u.sint;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002590 smp->data.type = SMP_T_BOOL;
Willy Tarreau97707872015-01-27 15:12:13 +01002591 return 1;
2592}
2593
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002594/* Takes a SINT on input, returns false is the value is non-null, otherwise
Willy Tarreau97707872015-01-27 15:12:13 +01002595 * truee. The output is a BOOL.
2596 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002597static int sample_conv_arith_not(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002598 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002599{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002600 smp->data.u.sint = !smp->data.u.sint;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002601 smp->data.type = SMP_T_BOOL;
Willy Tarreau97707872015-01-27 15:12:13 +01002602 return 1;
2603}
2604
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002605/* Takes a SINT on input, returns true is the value is odd, otherwise false.
Willy Tarreau97707872015-01-27 15:12:13 +01002606 * The output is a BOOL.
2607 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002608static int sample_conv_arith_odd(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002609 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002610{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002611 smp->data.u.sint = smp->data.u.sint & 1;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002612 smp->data.type = SMP_T_BOOL;
Willy Tarreau97707872015-01-27 15:12:13 +01002613 return 1;
2614}
2615
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002616/* Takes a SINT on input, returns true is the value is even, otherwise false.
Willy Tarreau97707872015-01-27 15:12:13 +01002617 * The output is a BOOL.
2618 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002619static int sample_conv_arith_even(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002620 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002621{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002622 smp->data.u.sint = !(smp->data.u.sint & 1);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002623 smp->data.type = SMP_T_BOOL;
Willy Tarreau97707872015-01-27 15:12:13 +01002624 return 1;
2625}
2626
Willy Tarreau280f42b2018-02-19 15:34:12 +01002627/* appends an optional const string, an optional variable contents and another
2628 * optional const string to an existing string.
2629 */
2630static int sample_conv_concat(const struct arg *arg_p, struct sample *smp, void *private)
2631{
Willy Tarreau83061a82018-07-13 11:56:34 +02002632 struct buffer *trash;
Willy Tarreau280f42b2018-02-19 15:34:12 +01002633 struct sample tmp;
2634 int max;
2635
2636 trash = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002637 trash->data = smp->data.u.str.data;
2638 if (trash->data > trash->size - 1)
2639 trash->data = trash->size - 1;
Willy Tarreau280f42b2018-02-19 15:34:12 +01002640
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002641 memcpy(trash->area, smp->data.u.str.area, trash->data);
2642 trash->area[trash->data] = 0;
Willy Tarreau280f42b2018-02-19 15:34:12 +01002643
2644 /* append first string */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002645 max = arg_p[0].data.str.data;
2646 if (max > trash->size - 1 - trash->data)
2647 max = trash->size - 1 - trash->data;
Willy Tarreau280f42b2018-02-19 15:34:12 +01002648
2649 if (max) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002650 memcpy(trash->area + trash->data, arg_p[0].data.str.area, max);
2651 trash->data += max;
2652 trash->area[trash->data] = 0;
Willy Tarreau280f42b2018-02-19 15:34:12 +01002653 }
2654
2655 /* append second string (variable) if it's found and we can turn it
2656 * into a string.
2657 */
2658 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
2659 if (arg_p[1].type == ARGT_VAR && vars_get_by_desc(&arg_p[1].data.var, &tmp) &&
2660 (sample_casts[tmp.data.type][SMP_T_STR] == c_none ||
2661 sample_casts[tmp.data.type][SMP_T_STR](&tmp))) {
2662
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002663 max = tmp.data.u.str.data;
2664 if (max > trash->size - 1 - trash->data)
2665 max = trash->size - 1 - trash->data;
Willy Tarreau280f42b2018-02-19 15:34:12 +01002666
2667 if (max) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002668 memcpy(trash->area + trash->data, tmp.data.u.str.area,
2669 max);
2670 trash->data += max;
2671 trash->area[trash->data] = 0;
Willy Tarreau280f42b2018-02-19 15:34:12 +01002672 }
2673 }
2674
2675 /* append third string */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002676 max = arg_p[2].data.str.data;
2677 if (max > trash->size - 1 - trash->data)
2678 max = trash->size - 1 - trash->data;
Willy Tarreau280f42b2018-02-19 15:34:12 +01002679
2680 if (max) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002681 memcpy(trash->area + trash->data, arg_p[2].data.str.area, max);
2682 trash->data += max;
2683 trash->area[trash->data] = 0;
Willy Tarreau280f42b2018-02-19 15:34:12 +01002684 }
2685
2686 smp->data.u.str = *trash;
2687 smp->data.type = SMP_T_STR;
2688 return 1;
2689}
2690
2691/* This function checks the "concat" converter's arguments and extracts the
2692 * variable name and its scope.
2693 */
2694static int smp_check_concat(struct arg *args, struct sample_conv *conv,
2695 const char *file, int line, char **err)
2696{
2697 /* Try to decode a variable. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002698 if (args[1].data.str.data > 0 && !vars_check_arg(&args[1], NULL)) {
2699 memprintf(err, "failed to register variable name '%s'",
2700 args[1].data.str.area);
Willy Tarreau280f42b2018-02-19 15:34:12 +01002701 return 0;
2702 }
2703 return 1;
2704}
2705
Tim Duesterhusca097c12018-04-27 21:18:45 +02002706/* compares string with a variable containing a string. Return value
2707 * is compatible with strcmp(3)'s return value.
2708 */
2709static int sample_conv_strcmp(const struct arg *arg_p, struct sample *smp, void *private)
2710{
2711 struct sample tmp;
2712 int max, result;
2713
2714 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
2715 if (arg_p[0].type != ARGT_VAR)
2716 return 0;
2717 if (!vars_get_by_desc(&arg_p[0].data.var, &tmp))
2718 return 0;
2719 if (!sample_casts[tmp.data.type][SMP_T_STR](&tmp))
2720 return 0;
2721
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002722 max = MIN(smp->data.u.str.data, tmp.data.u.str.data);
2723 result = strncmp(smp->data.u.str.area, tmp.data.u.str.area, max);
Tim Duesterhusca097c12018-04-27 21:18:45 +02002724 if (result == 0) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002725 if (smp->data.u.str.data != tmp.data.u.str.data) {
2726 if (smp->data.u.str.data < tmp.data.u.str.data) {
Tim Duesterhusca097c12018-04-27 21:18:45 +02002727 result = -1;
2728 }
2729 else {
2730 result = 1;
2731 }
2732 }
2733 }
2734
2735 smp->data.u.sint = result;
2736 smp->data.type = SMP_T_SINT;
2737 return 1;
2738}
2739
2740/* This function checks the "strcmp" converter's arguments and extracts the
2741 * variable name and its scope.
2742 */
2743static int smp_check_strcmp(struct arg *args, struct sample_conv *conv,
2744 const char *file, int line, char **err)
2745{
2746 /* Try to decode a variable. */
2747 if (vars_check_arg(&args[0], NULL))
2748 return 1;
2749
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002750 memprintf(err, "failed to register variable name '%s'",
2751 args[0].data.str.area);
Tim Duesterhusca097c12018-04-27 21:18:45 +02002752 return 0;
2753}
2754
Willy Tarreau5b8ad222013-07-25 12:17:57 +02002755/************************************************************************/
2756/* All supported sample fetch functions must be declared here */
2757/************************************************************************/
2758
2759/* force TRUE to be returned at the fetch level */
2760static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002761smp_fetch_true(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau5b8ad222013-07-25 12:17:57 +02002762{
Willy Tarreau280f42b2018-02-19 15:34:12 +01002763 if (!smp_make_rw(smp))
2764 return 0;
2765
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002766 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002767 smp->data.u.sint = 1;
Willy Tarreau5b8ad222013-07-25 12:17:57 +02002768 return 1;
2769}
2770
2771/* force FALSE to be returned at the fetch level */
2772static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002773smp_fetch_false(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau5b8ad222013-07-25 12:17:57 +02002774{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002775 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002776 smp->data.u.sint = 0;
Willy Tarreau5b8ad222013-07-25 12:17:57 +02002777 return 1;
2778}
2779
2780/* retrieve environment variable $1 as a string */
2781static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002782smp_fetch_env(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau5b8ad222013-07-25 12:17:57 +02002783{
2784 char *env;
2785
2786 if (!args || args[0].type != ARGT_STR)
2787 return 0;
2788
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002789 env = getenv(args[0].data.str.area);
Willy Tarreau5b8ad222013-07-25 12:17:57 +02002790 if (!env)
2791 return 0;
2792
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002793 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01002794 smp->flags = SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002795 smp->data.u.str.area = env;
2796 smp->data.u.str.data = strlen(env);
Willy Tarreau5b8ad222013-07-25 12:17:57 +02002797 return 1;
2798}
2799
Willy Tarreau6236d3a2013-07-25 14:28:25 +02002800/* retrieve the current local date in epoch time, and applies an optional offset
2801 * of args[0] seconds.
2802 */
2803static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002804smp_fetch_date(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau6236d3a2013-07-25 14:28:25 +02002805{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002806 smp->data.u.sint = date.tv_sec;
Willy Tarreau6236d3a2013-07-25 14:28:25 +02002807
2808 /* add offset */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002809 if (args && args[0].type == ARGT_SINT)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002810 smp->data.u.sint += args[0].data.sint;
Willy Tarreau6236d3a2013-07-25 14:28:25 +02002811
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002812 smp->data.type = SMP_T_SINT;
Willy Tarreau6236d3a2013-07-25 14:28:25 +02002813 smp->flags |= SMP_F_VOL_TEST | SMP_F_MAY_CHANGE;
2814 return 1;
2815}
2816
Etienne Carrierea792a0a2018-01-17 13:43:24 +01002817/* retrieve the current microsecond part of the date */
2818static int
2819smp_fetch_date_us(const struct arg *args, struct sample *smp, const char *kw, void *private)
2820{
2821 smp->data.u.sint = date.tv_usec;
2822 smp->data.type = SMP_T_SINT;
2823 smp->flags |= SMP_F_VOL_TEST | SMP_F_MAY_CHANGE;
2824 return 1;
2825}
2826
2827
Nenad Merdanovic807a6e72017-03-12 22:00:00 +01002828/* returns the hostname */
2829static int
2830smp_fetch_hostname(const struct arg *args, struct sample *smp, const char *kw, void *private)
2831{
2832 smp->data.type = SMP_T_STR;
2833 smp->flags = SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002834 smp->data.u.str.area = hostname;
2835 smp->data.u.str.data = strlen(hostname);
Nenad Merdanovic807a6e72017-03-12 22:00:00 +01002836 return 1;
2837}
2838
Willy Tarreau0f30d262014-11-24 16:02:05 +01002839/* returns the number of processes */
2840static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002841smp_fetch_nbproc(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau0f30d262014-11-24 16:02:05 +01002842{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002843 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002844 smp->data.u.sint = global.nbproc;
Willy Tarreau0f30d262014-11-24 16:02:05 +01002845 return 1;
2846}
2847
2848/* returns the number of the current process (between 1 and nbproc */
2849static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002850smp_fetch_proc(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau0f30d262014-11-24 16:02:05 +01002851{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002852 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002853 smp->data.u.sint = relative_pid;
Willy Tarreau0f30d262014-11-24 16:02:05 +01002854 return 1;
2855}
2856
Christopher Faulet34adb2a2017-11-21 21:45:38 +01002857/* returns the number of the current thread (between 1 and nbthread */
2858static int
2859smp_fetch_thread(const struct arg *args, struct sample *smp, const char *kw, void *private)
2860{
2861 smp->data.type = SMP_T_SINT;
2862 smp->data.u.sint = tid;
2863 return 1;
2864}
2865
Willy Tarreau84310e22014-02-14 11:59:04 +01002866/* generate a random 32-bit integer for whatever purpose, with an optional
2867 * range specified in argument.
2868 */
2869static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002870smp_fetch_rand(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau84310e22014-02-14 11:59:04 +01002871{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002872 smp->data.u.sint = random();
Willy Tarreau84310e22014-02-14 11:59:04 +01002873
2874 /* reduce if needed. Don't do a modulo, use all bits! */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002875 if (args && args[0].type == ARGT_SINT)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002876 smp->data.u.sint = (smp->data.u.sint * args[0].data.sint) / ((u64)RAND_MAX+1);
Willy Tarreau84310e22014-02-14 11:59:04 +01002877
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002878 smp->data.type = SMP_T_SINT;
Willy Tarreau84310e22014-02-14 11:59:04 +01002879 smp->flags |= SMP_F_VOL_TEST | SMP_F_MAY_CHANGE;
2880 return 1;
2881}
2882
Willy Tarreau0f30d262014-11-24 16:02:05 +01002883/* returns true if the current process is stopping */
2884static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002885smp_fetch_stopping(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau0f30d262014-11-24 16:02:05 +01002886{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002887 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002888 smp->data.u.sint = stopping;
Willy Tarreau0f30d262014-11-24 16:02:05 +01002889 return 1;
2890}
2891
Thierry FOURNIERcc103292015-06-06 19:30:17 +02002892static int smp_fetch_const_str(const struct arg *args, struct sample *smp, const char *kw, void *private)
2893{
2894 smp->flags |= SMP_F_CONST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002895 smp->data.type = SMP_T_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002896 smp->data.u.str.area = args[0].data.str.area;
2897 smp->data.u.str.data = args[0].data.str.data;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02002898 return 1;
2899}
2900
2901static int smp_check_const_bool(struct arg *args, char **err)
2902{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002903 if (strcasecmp(args[0].data.str.area, "true") == 0 ||
2904 strcasecmp(args[0].data.str.area, "1") == 0) {
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002905 args[0].type = ARGT_SINT;
2906 args[0].data.sint = 1;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02002907 return 1;
2908 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002909 if (strcasecmp(args[0].data.str.area, "false") == 0 ||
2910 strcasecmp(args[0].data.str.area, "0") == 0) {
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002911 args[0].type = ARGT_SINT;
2912 args[0].data.sint = 0;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02002913 return 1;
2914 }
2915 memprintf(err, "Expects 'true', 'false', '0' or '1'");
2916 return 0;
2917}
2918
2919static int smp_fetch_const_bool(const struct arg *args, struct sample *smp, const char *kw, void *private)
2920{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002921 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002922 smp->data.u.sint = args[0].data.sint;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02002923 return 1;
2924}
2925
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002926static int smp_fetch_const_int(const struct arg *args, struct sample *smp, const char *kw, void *private)
Thierry FOURNIERcc103292015-06-06 19:30:17 +02002927{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002928 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002929 smp->data.u.sint = args[0].data.sint;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02002930 return 1;
2931}
2932
2933static int smp_fetch_const_ipv4(const struct arg *args, struct sample *smp, const char *kw, void *private)
2934{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002935 smp->data.type = SMP_T_IPV4;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002936 smp->data.u.ipv4 = args[0].data.ipv4;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02002937 return 1;
2938}
2939
2940static int smp_fetch_const_ipv6(const struct arg *args, struct sample *smp, const char *kw, void *private)
2941{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002942 smp->data.type = SMP_T_IPV6;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002943 smp->data.u.ipv6 = args[0].data.ipv6;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02002944 return 1;
2945}
2946
2947static int smp_check_const_bin(struct arg *args, char **err)
2948{
David Carlier64a16ab2016-04-08 10:37:02 +01002949 char *binstr = NULL;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02002950 int binstrlen;
2951
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002952 if (!parse_binary(args[0].data.str.area, &binstr, &binstrlen, err))
Thierry FOURNIERcc103292015-06-06 19:30:17 +02002953 return 0;
2954 args[0].type = ARGT_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002955 args[0].data.str.area = binstr;
2956 args[0].data.str.data = binstrlen;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02002957 return 1;
2958}
2959
2960static int smp_fetch_const_bin(const struct arg *args, struct sample *smp, const char *kw, void *private)
2961{
2962 smp->flags |= SMP_F_CONST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002963 smp->data.type = SMP_T_BIN;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002964 smp->data.u.str.area = args[0].data.str.area;
2965 smp->data.u.str.data = args[0].data.str.data;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02002966 return 1;
2967}
2968
2969static int smp_check_const_meth(struct arg *args, char **err)
2970{
2971 enum http_meth_t meth;
2972 int i;
2973
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002974 meth = find_http_meth(args[0].data.str.area, args[0].data.str.data);
Thierry FOURNIERcc103292015-06-06 19:30:17 +02002975 if (meth != HTTP_METH_OTHER) {
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002976 args[0].type = ARGT_SINT;
2977 args[0].data.sint = meth;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02002978 } else {
2979 /* Check method avalaibility. A methos is a token defined as :
2980 * tchar = "!" / "#" / "$" / "%" / "&" / "'" / "*" / "+" / "-" / "." /
2981 * "^" / "_" / "`" / "|" / "~" / DIGIT / ALPHA
2982 * token = 1*tchar
2983 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002984 for (i = 0; i < args[0].data.str.data; i++) {
2985 if (!HTTP_IS_TOKEN(args[0].data.str.area[i])) {
Thierry FOURNIERcc103292015-06-06 19:30:17 +02002986 memprintf(err, "expects valid method.");
2987 return 0;
2988 }
2989 }
2990 }
2991 return 1;
2992}
2993
2994static int smp_fetch_const_meth(const struct arg *args, struct sample *smp, const char *kw, void *private)
2995{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002996 smp->data.type = SMP_T_METH;
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002997 if (args[0].type == ARGT_SINT) {
Thierry FOURNIERcc103292015-06-06 19:30:17 +02002998 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002999 smp->data.u.meth.meth = args[0].data.sint;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003000 smp->data.u.meth.str.area = "";
3001 smp->data.u.meth.str.data = 0;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003002 } else {
3003 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003004 smp->data.u.meth.meth = HTTP_METH_OTHER;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003005 smp->data.u.meth.str.area = args[0].data.str.area;
3006 smp->data.u.meth.str.data = args[0].data.str.data;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003007 }
3008 return 1;
3009}
3010
Willy Tarreau5b8ad222013-07-25 12:17:57 +02003011/* Note: must not be declared <const> as its list will be overwritten.
3012 * Note: fetches that may return multiple types must be declared as the lowest
3013 * common denominator, the type that can be casted into all other ones. For
3014 * instance IPv4/IPv6 must be declared IPv4.
3015 */
3016static struct sample_fetch_kw_list smp_kws = {ILH, {
3017 { "always_false", smp_fetch_false, 0, NULL, SMP_T_BOOL, SMP_USE_INTRN },
3018 { "always_true", smp_fetch_true, 0, NULL, SMP_T_BOOL, SMP_USE_INTRN },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01003019 { "env", smp_fetch_env, ARG1(1,STR), NULL, SMP_T_STR, SMP_USE_INTRN },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02003020 { "date", smp_fetch_date, ARG1(0,SINT), NULL, SMP_T_SINT, SMP_USE_INTRN },
Etienne Carrierea792a0a2018-01-17 13:43:24 +01003021 { "date_us", smp_fetch_date_us, 0, NULL, SMP_T_SINT, SMP_USE_INTRN },
Nenad Merdanovic807a6e72017-03-12 22:00:00 +01003022 { "hostname", smp_fetch_hostname, 0, NULL, SMP_T_STR, SMP_USE_INTRN },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02003023 { "nbproc", smp_fetch_nbproc,0, NULL, SMP_T_SINT, SMP_USE_INTRN },
3024 { "proc", smp_fetch_proc, 0, NULL, SMP_T_SINT, SMP_USE_INTRN },
Christopher Faulet34adb2a2017-11-21 21:45:38 +01003025 { "thread", smp_fetch_thread, 0, NULL, SMP_T_SINT, SMP_USE_INTRN },
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02003026 { "rand", smp_fetch_rand, ARG1(0,SINT), NULL, SMP_T_SINT, SMP_USE_INTRN },
Willy Tarreau0f30d262014-11-24 16:02:05 +01003027 { "stopping", smp_fetch_stopping, 0, NULL, SMP_T_BOOL, SMP_USE_INTRN },
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003028
3029 { "str", smp_fetch_const_str, ARG1(1,STR), NULL , SMP_T_STR, SMP_USE_INTRN },
3030 { "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 +02003031 { "int", smp_fetch_const_int, ARG1(1,SINT), NULL , SMP_T_SINT, SMP_USE_INTRN },
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003032 { "ipv4", smp_fetch_const_ipv4, ARG1(1,IPV4), NULL , SMP_T_IPV4, SMP_USE_INTRN },
3033 { "ipv6", smp_fetch_const_ipv6, ARG1(1,IPV6), NULL , SMP_T_IPV6, SMP_USE_INTRN },
3034 { "bin", smp_fetch_const_bin, ARG1(1,STR), smp_check_const_bin , SMP_T_BIN, SMP_USE_INTRN },
3035 { "meth", smp_fetch_const_meth, ARG1(1,STR), smp_check_const_meth, SMP_T_METH, SMP_USE_INTRN },
3036
Willy Tarreau5b8ad222013-07-25 12:17:57 +02003037 { /* END */ },
3038}};
3039
Emeric Brun107ca302010-01-04 16:16:05 +01003040/* Note: must not be declared <const> as its list will be overwritten */
Willy Tarreaudc13c112013-06-21 23:16:39 +02003041static struct sample_conv_kw_list sample_conv_kws = {ILH, {
Thierry FOURNIER9687c772015-05-07 15:46:29 +02003042#ifdef DEBUG_EXPR
3043 { "debug", sample_conv_debug, 0, NULL, SMP_T_ANY, SMP_T_ANY },
3044#endif
3045
Holger Just1bfc24b2017-05-06 00:56:53 +02003046 { "b64dec", sample_conv_base642bin,0, NULL, SMP_T_STR, SMP_T_BIN },
Emeric Brun53d1a982014-04-30 18:21:37 +02003047 { "base64", sample_conv_bin2base64,0, NULL, SMP_T_BIN, SMP_T_STR },
Willy Tarreau12785782012-04-27 21:37:17 +02003048 { "upper", sample_conv_str2upper, 0, NULL, SMP_T_STR, SMP_T_STR },
3049 { "lower", sample_conv_str2lower, 0, NULL, SMP_T_STR, SMP_T_STR },
Willy Tarreau60a2ee72017-12-15 07:13:48 +01003050 { "length", sample_conv_length, 0, NULL, SMP_T_STR, SMP_T_SINT },
Thierry FOURNIER2f49d6d2014-03-12 15:01:52 +01003051 { "hex", sample_conv_bin2hex, 0, NULL, SMP_T_BIN, SMP_T_STR },
Dragan Dosen3f957b22017-10-24 09:27:34 +02003052 { "hex2i", sample_conv_hex2int, 0, NULL, SMP_T_STR, SMP_T_SINT },
Tim Duesterhus1478aa72018-01-25 16:24:51 +01003053 { "ipmask", sample_conv_ipmask, ARG2(1,MSK4,MSK6), NULL, SMP_T_ADDR, SMP_T_IPV4 },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02003054 { "ltime", sample_conv_ltime, ARG2(1,STR,SINT), NULL, SMP_T_SINT, SMP_T_STR },
3055 { "utime", sample_conv_utime, ARG2(1,STR,SINT), NULL, SMP_T_SINT, SMP_T_STR },
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02003056 { "crc32", sample_conv_crc32, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
Emmanuel Hocdet50791a72018-03-21 11:19:01 +01003057 { "crc32c", sample_conv_crc32c, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02003058 { "djb2", sample_conv_djb2, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
3059 { "sdbm", sample_conv_sdbm, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
3060 { "wt6", sample_conv_wt6, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
Thierry FOURNIER01e09742016-12-26 11:46:11 +01003061 { "xxh32", sample_conv_xxh32, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
3062 { "xxh64", sample_conv_xxh64, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02003063 { "json", sample_conv_json, ARG1(1,STR), sample_conv_json_check, SMP_T_STR, SMP_T_STR },
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02003064 { "bytes", sample_conv_bytes, ARG2(1,SINT,SINT), NULL, SMP_T_BIN, SMP_T_BIN },
Marcin Deranek9631a282018-04-16 14:30:46 +02003065 { "field", sample_conv_field, ARG3(2,SINT,STR,SINT), sample_conv_field_check, SMP_T_STR, SMP_T_STR },
3066 { "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 +01003067 { "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 +02003068 { "sha1", sample_conv_sha1, 0, NULL, SMP_T_BIN, SMP_T_BIN },
Willy Tarreau280f42b2018-02-19 15:34:12 +01003069 { "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 +02003070 { "strcmp", sample_conv_strcmp, ARG1(1,STR), smp_check_strcmp, SMP_T_STR, SMP_T_SINT },
Willy Tarreau97707872015-01-27 15:12:13 +01003071
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02003072 { "and", sample_conv_binary_and, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
3073 { "or", sample_conv_binary_or, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
3074 { "xor", sample_conv_binary_xor, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
3075 { "cpl", sample_conv_binary_cpl, 0, NULL, SMP_T_SINT, SMP_T_SINT },
3076 { "bool", sample_conv_arith_bool, 0, NULL, SMP_T_SINT, SMP_T_BOOL },
3077 { "not", sample_conv_arith_not, 0, NULL, SMP_T_SINT, SMP_T_BOOL },
3078 { "odd", sample_conv_arith_odd, 0, NULL, SMP_T_SINT, SMP_T_BOOL },
3079 { "even", sample_conv_arith_even, 0, NULL, SMP_T_SINT, SMP_T_BOOL },
3080 { "add", sample_conv_arith_add, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
3081 { "sub", sample_conv_arith_sub, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
3082 { "mul", sample_conv_arith_mul, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
3083 { "div", sample_conv_arith_div, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
3084 { "mod", sample_conv_arith_mod, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
3085 { "neg", sample_conv_arith_neg, 0, NULL, SMP_T_SINT, SMP_T_SINT },
Willy Tarreau97707872015-01-27 15:12:13 +01003086
Willy Tarreau9fcb9842012-04-20 14:45:49 +02003087 { NULL, NULL, 0, 0, 0 },
Emeric Brun107ca302010-01-04 16:16:05 +01003088}};
3089
3090__attribute__((constructor))
Willy Tarreau12785782012-04-27 21:37:17 +02003091static void __sample_init(void)
Emeric Brun107ca302010-01-04 16:16:05 +01003092{
Willy Tarreau5b8ad222013-07-25 12:17:57 +02003093 /* register sample fetch and format conversion keywords */
3094 sample_register_fetches(&smp_kws);
Willy Tarreau12785782012-04-27 21:37:17 +02003095 sample_register_convs(&sample_conv_kws);
Emeric Brun107ca302010-01-04 16:16:05 +01003096}