blob: b1c01484fbc680ec68beacb858973c2ad5a79fd1 [file] [log] [blame]
Emeric Brun107ca302010-01-04 16:16:05 +01001/*
Willy Tarreaucd3b0942012-04-27 21:52:18 +02002 * Sample management functions.
Emeric Brun107ca302010-01-04 16:16:05 +01003 *
4 * Copyright 2009-2010 EXCELIANCE, Emeric Brun <ebrun@exceliance.fr>
Willy Tarreaucd3b0942012-04-27 21:52:18 +02005 * Copyright (C) 2012 Willy Tarreau <w@1wt.eu>
Emeric Brun107ca302010-01-04 16:16:05 +01006 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
11 *
12 */
13
Thierry FOURNIER317e1c42014-08-12 10:20:47 +020014#include <ctype.h>
Emeric Brun107ca302010-01-04 16:16:05 +010015#include <string.h>
16#include <arpa/inet.h>
Willy Tarreaudd2f85e2012-09-02 22:34:23 +020017#include <stdio.h>
Emeric Brun107ca302010-01-04 16:16:05 +010018
Willy Tarreau7e2c6472012-10-29 20:44:36 +010019#include <types/global.h>
20
Willy Tarreauc7e42382012-08-24 19:22:53 +020021#include <common/chunk.h>
Willy Tarreau23ec4ca2014-07-15 20:15:37 +020022#include <common/hash.h>
Willy Tarreaucd3b0942012-04-27 21:52:18 +020023#include <common/standard.h>
Willy Tarreaua4312fa2013-04-02 16:34:32 +020024#include <common/uri_auth.h>
Emeric Brun53d1a982014-04-30 18:21:37 +020025#include <common/base64.h>
Willy Tarreaucd3b0942012-04-27 21:52:18 +020026
Willy Tarreau9fcb9842012-04-20 14:45:49 +020027#include <proto/arg.h>
Willy Tarreaua4312fa2013-04-02 16:34:32 +020028#include <proto/auth.h>
29#include <proto/log.h>
Thierry FOURNIERd4373142013-12-17 01:10:10 +010030#include <proto/proto_http.h>
Willy Tarreaua4312fa2013-04-02 16:34:32 +020031#include <proto/proxy.h>
Willy Tarreaucd3b0942012-04-27 21:52:18 +020032#include <proto/sample.h>
Willy Tarreaua4312fa2013-04-02 16:34:32 +020033#include <proto/stick_table.h>
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +020034#include <proto/vars.h>
Emeric Brun107ca302010-01-04 16:16:05 +010035
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
315 trash.len = 0;
316 trash.str[0] = '\0';
317 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
324 trash.len += snprintf(trash.str + trash.len, trash.size - trash.len, "%s%s",
325 (use & ((1 << bit) - 1)) ? "," : "",
326 fetch_src_names[bit]);
327 }
328 return trash.str;
329}
330
Willy Tarreaubf8e2512013-03-25 14:52:41 +0100331/* return a pointer to the correct sample checkpoint name, or "unknown" when
332 * the flags are invalid. Only the lowest bit is used, higher bits are ignored
333 * if set.
334 */
335const char *sample_ckp_names(unsigned int use)
336{
337 int bit;
338
339 for (bit = 0; bit < SMP_CKP_ENTRIES; bit++)
340 if (use & (1 << bit))
341 return fetch_ckp_names[bit];
342 return "unknown sample check place, please report this bug";
343}
344
Emeric Brun107ca302010-01-04 16:16:05 +0100345/*
Willy Tarreau80aca902013-01-07 15:42:20 +0100346 * Registers the sample fetch keyword list <kwl> as a list of valid keywords
347 * for next parsing sessions. The fetch keywords capabilities are also computed
348 * from their ->use field.
Emeric Brun107ca302010-01-04 16:16:05 +0100349 */
Willy Tarreau80aca902013-01-07 15:42:20 +0100350void sample_register_fetches(struct sample_fetch_kw_list *kwl)
Emeric Brun107ca302010-01-04 16:16:05 +0100351{
Willy Tarreau80aca902013-01-07 15:42:20 +0100352 struct sample_fetch *sf;
353 int bit;
354
355 for (sf = kwl->kw; sf->kw != NULL; sf++) {
356 for (bit = 0; bit < SMP_SRC_ENTRIES; bit++)
357 if (sf->use & (1 << bit))
358 sf->val |= fetch_cap[bit];
359 }
360 LIST_ADDQ(&sample_fetches.list, &kwl->list);
Emeric Brun107ca302010-01-04 16:16:05 +0100361}
362
363/*
Willy Tarreau12785782012-04-27 21:37:17 +0200364 * Registers the sample format coverstion keyword list <pckl> as a list of valid keywords for next
Emeric Brun107ca302010-01-04 16:16:05 +0100365 * parsing sessions.
366 */
Willy Tarreau12785782012-04-27 21:37:17 +0200367void sample_register_convs(struct sample_conv_kw_list *pckl)
Emeric Brun107ca302010-01-04 16:16:05 +0100368{
Willy Tarreau12785782012-04-27 21:37:17 +0200369 LIST_ADDQ(&sample_convs.list, &pckl->list);
Emeric Brun107ca302010-01-04 16:16:05 +0100370}
371
372/*
Willy Tarreau12785782012-04-27 21:37:17 +0200373 * Returns the pointer on sample fetch keyword structure identified by
Emeric Brun107ca302010-01-04 16:16:05 +0100374 * string of <len> in buffer <kw>.
375 *
376 */
Willy Tarreau12785782012-04-27 21:37:17 +0200377struct sample_fetch *find_sample_fetch(const char *kw, int len)
Emeric Brun107ca302010-01-04 16:16:05 +0100378{
379 int index;
Willy Tarreau12785782012-04-27 21:37:17 +0200380 struct sample_fetch_kw_list *kwl;
Emeric Brun107ca302010-01-04 16:16:05 +0100381
Willy Tarreau12785782012-04-27 21:37:17 +0200382 list_for_each_entry(kwl, &sample_fetches.list, list) {
Emeric Brun107ca302010-01-04 16:16:05 +0100383 for (index = 0; kwl->kw[index].kw != NULL; index++) {
384 if (strncmp(kwl->kw[index].kw, kw, len) == 0 &&
385 kwl->kw[index].kw[len] == '\0')
386 return &kwl->kw[index];
387 }
388 }
389 return NULL;
390}
391
Tim Duesterhusc555ee02018-01-25 16:24:44 +0100392/* This function browses the list of available sample fetches. <current> is
Thierry FOURNIER4d9a1d12014-12-08 14:49:19 +0100393 * the last used sample fetch. If it is the first call, it must set to NULL.
Tim Duesterhusc555ee02018-01-25 16:24:44 +0100394 * <idx> is the index of the next sample fetch entry. It is used as private
395 * value. It is useless to initiate it.
Thierry FOURNIER4d9a1d12014-12-08 14:49:19 +0100396 *
Tim Duesterhusc555ee02018-01-25 16:24:44 +0100397 * It returns always the new fetch_sample entry, and NULL when the end of
Thierry FOURNIER4d9a1d12014-12-08 14:49:19 +0100398 * the list is reached.
399 */
400struct sample_fetch *sample_fetch_getnext(struct sample_fetch *current, int *idx)
401{
402 struct sample_fetch_kw_list *kwl;
403 struct sample_fetch *base;
404
405 if (!current) {
406 /* Get first kwl entry. */
407 kwl = LIST_NEXT(&sample_fetches.list, struct sample_fetch_kw_list *, list);
408 (*idx) = 0;
409 } else {
410 /* Get kwl corresponding to the curret entry. */
411 base = current + 1 - (*idx);
412 kwl = container_of(base, struct sample_fetch_kw_list, kw);
413 }
414
415 while (1) {
416
417 /* Check if kwl is the last entry. */
418 if (&kwl->list == &sample_fetches.list)
419 return NULL;
420
421 /* idx contain the next keyword. If it is available, return it. */
422 if (kwl->kw[*idx].kw) {
423 (*idx)++;
424 return &kwl->kw[(*idx)-1];
425 }
426
427 /* get next entry in the main list, and return NULL if the end is reached. */
428 kwl = LIST_NEXT(&kwl->list, struct sample_fetch_kw_list *, list);
429
430 /* Set index to 0, ans do one other loop. */
431 (*idx) = 0;
432 }
433}
434
Thierry FOURNIER8fd13762015-03-10 23:56:48 +0100435/* This function browses the list of available converters. <current> is
436 * the last used converter. If it is the first call, it must set to NULL.
437 * <idx> is the index of the next converter entry. It is used as private
438 * value. It is useless to initiate it.
439 *
440 * It returns always the next sample_conv entry, and NULL when the end of
441 * the list is reached.
442 */
443struct sample_conv *sample_conv_getnext(struct sample_conv *current, int *idx)
444{
445 struct sample_conv_kw_list *kwl;
446 struct sample_conv *base;
447
448 if (!current) {
449 /* Get first kwl entry. */
450 kwl = LIST_NEXT(&sample_convs.list, struct sample_conv_kw_list *, list);
451 (*idx) = 0;
452 } else {
453 /* Get kwl corresponding to the curret entry. */
454 base = current + 1 - (*idx);
455 kwl = container_of(base, struct sample_conv_kw_list, kw);
456 }
457
458 while (1) {
459 /* Check if kwl is the last entry. */
460 if (&kwl->list == &sample_convs.list)
461 return NULL;
462
463 /* idx contain the next keyword. If it is available, return it. */
464 if (kwl->kw[*idx].kw) {
465 (*idx)++;
466 return &kwl->kw[(*idx)-1];
467 }
468
469 /* get next entry in the main list, and return NULL if the end is reached. */
470 kwl = LIST_NEXT(&kwl->list, struct sample_conv_kw_list *, list);
471
472 /* Set index to 0, ans do one other loop. */
473 (*idx) = 0;
474 }
475}
476
Emeric Brun107ca302010-01-04 16:16:05 +0100477/*
Willy Tarreau12785782012-04-27 21:37:17 +0200478 * Returns the pointer on sample format conversion keyword structure identified by
Emeric Brun107ca302010-01-04 16:16:05 +0100479 * string of <len> in buffer <kw>.
480 *
481 */
Willy Tarreau12785782012-04-27 21:37:17 +0200482struct sample_conv *find_sample_conv(const char *kw, int len)
Emeric Brun107ca302010-01-04 16:16:05 +0100483{
484 int index;
Willy Tarreau12785782012-04-27 21:37:17 +0200485 struct sample_conv_kw_list *kwl;
Emeric Brun107ca302010-01-04 16:16:05 +0100486
Willy Tarreau12785782012-04-27 21:37:17 +0200487 list_for_each_entry(kwl, &sample_convs.list, list) {
Emeric Brun107ca302010-01-04 16:16:05 +0100488 for (index = 0; kwl->kw[index].kw != NULL; index++) {
489 if (strncmp(kwl->kw[index].kw, kw, len) == 0 &&
490 kwl->kw[index].kw[len] == '\0')
491 return &kwl->kw[index];
492 }
493 }
494 return NULL;
495}
496
Emeric Brun107ca302010-01-04 16:16:05 +0100497/******************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +0200498/* Sample casts functions */
Emeric Brun107ca302010-01-04 16:16:05 +0100499/******************************************************************/
500
Willy Tarreau342acb42012-04-23 22:03:39 +0200501static int c_ip2int(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100502{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200503 smp->data.u.sint = ntohl(smp->data.u.ipv4.s_addr);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200504 smp->data.type = SMP_T_SINT;
Emeric Brun107ca302010-01-04 16:16:05 +0100505 return 1;
506}
507
Willy Tarreau342acb42012-04-23 22:03:39 +0200508static int c_ip2str(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100509{
Willy Tarreau47ca5452012-12-23 20:22:19 +0100510 struct chunk *trash = get_trash_chunk();
Emeric Brun107ca302010-01-04 16:16:05 +0100511
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200512 if (!inet_ntop(AF_INET, (void *)&smp->data.u.ipv4, trash->str, trash->size))
Emeric Brun107ca302010-01-04 16:16:05 +0100513 return 0;
514
515 trash->len = strlen(trash->str);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200516 smp->data.u.str = *trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200517 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100518 smp->flags &= ~SMP_F_CONST;
Emeric Brun107ca302010-01-04 16:16:05 +0100519
520 return 1;
521}
522
Willy Tarreau342acb42012-04-23 22:03:39 +0200523static int c_ip2ipv6(struct sample *smp)
David du Colombier4f92d322011-03-24 11:09:31 +0100524{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200525 v4tov6(&smp->data.u.ipv6, &smp->data.u.ipv4);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200526 smp->data.type = SMP_T_IPV6;
David du Colombier4f92d322011-03-24 11:09:31 +0100527 return 1;
528}
529
Thierry FOURNIERcc4d1712015-07-24 09:04:56 +0200530static int c_ipv62ip(struct sample *smp)
531{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200532 if (!v6tov4(&smp->data.u.ipv4, &smp->data.u.ipv6))
Thierry FOURNIERcc4d1712015-07-24 09:04:56 +0200533 return 0;
Tim Duesterhusbf5ce022018-01-25 16:24:46 +0100534 smp->data.type = SMP_T_IPV4;
Thierry FOURNIERcc4d1712015-07-24 09:04:56 +0200535 return 1;
536}
537
Willy Tarreau342acb42012-04-23 22:03:39 +0200538static int c_ipv62str(struct sample *smp)
David du Colombier4f92d322011-03-24 11:09:31 +0100539{
Willy Tarreau47ca5452012-12-23 20:22:19 +0100540 struct chunk *trash = get_trash_chunk();
David du Colombier4f92d322011-03-24 11:09:31 +0100541
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200542 if (!inet_ntop(AF_INET6, (void *)&smp->data.u.ipv6, trash->str, trash->size))
David du Colombier4f92d322011-03-24 11:09:31 +0100543 return 0;
544
545 trash->len = strlen(trash->str);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200546 smp->data.u.str = *trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200547 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100548 smp->flags &= ~SMP_F_CONST;
David du Colombier4f92d322011-03-24 11:09:31 +0100549 return 1;
550}
551
552/*
Willy Tarreau342acb42012-04-23 22:03:39 +0200553static int c_ipv62ip(struct sample *smp)
David du Colombier4f92d322011-03-24 11:09:31 +0100554{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200555 return v6tov4(&smp->data.u.ipv4, &smp->data.u.ipv6);
David du Colombier4f92d322011-03-24 11:09:31 +0100556}
557*/
558
Willy Tarreau342acb42012-04-23 22:03:39 +0200559static int c_int2ip(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100560{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200561 smp->data.u.ipv4.s_addr = htonl((unsigned int)smp->data.u.sint);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200562 smp->data.type = SMP_T_IPV4;
Emeric Brun107ca302010-01-04 16:16:05 +0100563 return 1;
564}
565
Thierry FOURNIERcc4d1712015-07-24 09:04:56 +0200566static int c_int2ipv6(struct sample *smp)
567{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200568 smp->data.u.ipv4.s_addr = htonl((unsigned int)smp->data.u.sint);
569 v4tov6(&smp->data.u.ipv6, &smp->data.u.ipv4);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200570 smp->data.type = SMP_T_IPV6;
Thierry FOURNIERcc4d1712015-07-24 09:04:56 +0200571 return 1;
572}
573
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100574static int c_str2addr(struct sample *smp)
575{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200576 if (!buf2ip(smp->data.u.str.str, smp->data.u.str.len, &smp->data.u.ipv4)) {
577 if (!buf2ip6(smp->data.u.str.str, smp->data.u.str.len, &smp->data.u.ipv6))
Thierry FOURNIERfd139902013-12-11 12:38:57 +0100578 return 0;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200579 smp->data.type = SMP_T_IPV6;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100580 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIERfd139902013-12-11 12:38:57 +0100581 return 1;
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100582 }
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200583 smp->data.type = SMP_T_IPV4;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100584 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100585 return 1;
586}
587
Willy Tarreau342acb42012-04-23 22:03:39 +0200588static int c_str2ip(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100589{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200590 if (!buf2ip(smp->data.u.str.str, smp->data.u.str.len, &smp->data.u.ipv4))
Emeric Brun107ca302010-01-04 16:16:05 +0100591 return 0;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200592 smp->data.type = SMP_T_IPV4;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100593 smp->flags &= ~SMP_F_CONST;
Emeric Brun107ca302010-01-04 16:16:05 +0100594 return 1;
595}
596
Willy Tarreau342acb42012-04-23 22:03:39 +0200597static int c_str2ipv6(struct sample *smp)
David du Colombier4f92d322011-03-24 11:09:31 +0100598{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200599 if (!buf2ip6(smp->data.u.str.str, smp->data.u.str.len, &smp->data.u.ipv6))
Thierry FOURNIERfd139902013-12-11 12:38:57 +0100600 return 0;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200601 smp->data.type = SMP_T_IPV6;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100602 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIERfd139902013-12-11 12:38:57 +0100603 return 1;
David du Colombier4f92d322011-03-24 11:09:31 +0100604}
605
Emeric Brun4b9e8022014-11-03 18:17:10 +0100606/*
607 * The NULL char always enforces the end of string if it is met.
608 * Data is never changed, so we can ignore the CONST case
Thierry FOURNIERe87cac12014-03-12 15:07:59 +0100609 */
Emeric Brun8ac33d92012-10-17 13:36:06 +0200610static int c_bin2str(struct sample *smp)
611{
Emeric Brun4b9e8022014-11-03 18:17:10 +0100612 int i;
Emeric Brun8ac33d92012-10-17 13:36:06 +0200613
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200614 for (i = 0; i < smp->data.u.str.len; i++) {
615 if (!smp->data.u.str.str[i]) {
616 smp->data.u.str.len = i;
Thierry FOURNIERe87cac12014-03-12 15:07:59 +0100617 break;
Emeric Brun4b9e8022014-11-03 18:17:10 +0100618 }
Emeric Brun8ac33d92012-10-17 13:36:06 +0200619 }
Emeric Brun8ac33d92012-10-17 13:36:06 +0200620 return 1;
621}
622
Willy Tarreau342acb42012-04-23 22:03:39 +0200623static int c_int2str(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100624{
Willy Tarreau47ca5452012-12-23 20:22:19 +0100625 struct chunk *trash = get_trash_chunk();
Emeric Brun107ca302010-01-04 16:16:05 +0100626 char *pos;
627
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200628 pos = lltoa_r(smp->data.u.sint, trash->str, trash->size);
Emeric Brun107ca302010-01-04 16:16:05 +0100629 if (!pos)
630 return 0;
631
Emeric Brun485479d2010-09-23 18:02:19 +0200632 trash->size = trash->size - (pos - trash->str);
Emeric Brun107ca302010-01-04 16:16:05 +0100633 trash->str = pos;
634 trash->len = strlen(pos);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200635 smp->data.u.str = *trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200636 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100637 smp->flags &= ~SMP_F_CONST;
Emeric Brun107ca302010-01-04 16:16:05 +0100638 return 1;
639}
640
Willy Tarreauad635822016-08-08 19:21:09 +0200641/* This function inconditionally duplicates data and removes the "const" flag.
642 * For strings and binary blocks, it also provides a known allocated size with
643 * a length that is capped to the size, and ensures a trailing zero is always
644 * appended for strings. This is necessary for some operations which may
645 * require to extend the length. It returns 0 if it fails, 1 on success.
646 */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100647int smp_dup(struct sample *smp)
Emeric Brun485479d2010-09-23 18:02:19 +0200648{
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100649 struct chunk *trash;
Emeric Brun485479d2010-09-23 18:02:19 +0200650
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200651 switch (smp->data.type) {
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100652 case SMP_T_BOOL:
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100653 case SMP_T_SINT:
654 case SMP_T_ADDR:
655 case SMP_T_IPV4:
656 case SMP_T_IPV6:
657 /* These type are not const. */
658 break;
Willy Tarreauad635822016-08-08 19:21:09 +0200659
Christopher Fauletec100512017-07-24 15:38:41 +0200660 case SMP_T_METH:
661 if (smp->data.u.meth.meth != HTTP_METH_OTHER)
662 break;
663 /* Fall through */
664
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100665 case SMP_T_STR:
Willy Tarreauad635822016-08-08 19:21:09 +0200666 trash = get_trash_chunk();
667 trash->len = smp->data.u.str.len;
668 if (trash->len > trash->size - 1)
669 trash->len = trash->size - 1;
670
671 memcpy(trash->str, smp->data.u.str.str, trash->len);
672 trash->str[trash->len] = 0;
673 smp->data.u.str = *trash;
674 break;
675
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100676 case SMP_T_BIN:
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100677 trash = get_trash_chunk();
Willy Tarreauad635822016-08-08 19:21:09 +0200678 trash->len = smp->data.u.str.len;
679 if (trash->len > trash->size)
680 trash->len = trash->size;
681
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200682 memcpy(trash->str, smp->data.u.str.str, trash->len);
683 smp->data.u.str = *trash;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100684 break;
Christopher Fauletec100512017-07-24 15:38:41 +0200685
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100686 default:
687 /* Other cases are unexpected. */
688 return 0;
689 }
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100690
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100691 /* remove const flag */
692 smp->flags &= ~SMP_F_CONST;
Emeric Brun485479d2010-09-23 18:02:19 +0200693 return 1;
694}
695
Thierry FOURNIER0e9af552013-12-14 14:55:04 +0100696int c_none(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100697{
698 return 1;
699}
700
Willy Tarreau342acb42012-04-23 22:03:39 +0200701static int c_str2int(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100702{
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +0200703 const char *str;
704 const char *end;
Emeric Brun107ca302010-01-04 16:16:05 +0100705
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200706 if (smp->data.u.str.len == 0)
Thierry FOURNIER60bb0202014-01-27 18:20:48 +0100707 return 0;
708
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200709 str = smp->data.u.str.str;
710 end = smp->data.u.str.str + smp->data.u.str.len;
Emeric Brun107ca302010-01-04 16:16:05 +0100711
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200712 smp->data.u.sint = read_int64(&str, end);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200713 smp->data.type = SMP_T_SINT;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100714 smp->flags &= ~SMP_F_CONST;
Emeric Brun107ca302010-01-04 16:16:05 +0100715 return 1;
716}
717
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100718static int c_str2meth(struct sample *smp)
719{
720 enum http_meth_t meth;
721 int len;
722
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200723 meth = find_http_meth(smp->data.u.str.str, smp->data.u.str.len);
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100724 if (meth == HTTP_METH_OTHER) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200725 len = smp->data.u.str.len;
726 smp->data.u.meth.str.str = smp->data.u.str.str;
727 smp->data.u.meth.str.len = len;
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100728 }
729 else
730 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200731 smp->data.u.meth.meth = meth;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200732 smp->data.type = SMP_T_METH;
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100733 return 1;
734}
735
736static int c_meth2str(struct sample *smp)
737{
738 int len;
739 enum http_meth_t meth;
740
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200741 if (smp->data.u.meth.meth == HTTP_METH_OTHER) {
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100742 /* The method is unknown. Copy the original pointer. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200743 len = smp->data.u.meth.str.len;
744 smp->data.u.str.str = smp->data.u.meth.str.str;
745 smp->data.u.str.len = len;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200746 smp->data.type = SMP_T_STR;
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100747 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200748 else if (smp->data.u.meth.meth < HTTP_METH_OTHER) {
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100749 /* The method is known, copy the pointer containing the string. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200750 meth = smp->data.u.meth.meth;
751 smp->data.u.str.str = http_known_methods[meth].name;
752 smp->data.u.str.len = http_known_methods[meth].len;
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100753 smp->flags |= SMP_F_CONST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200754 smp->data.type = SMP_T_STR;
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100755 }
756 else {
757 /* Unknown method */
758 return 0;
759 }
760 return 1;
761}
762
Willy Tarreau9700e5c2014-07-15 21:03:26 +0200763static int c_addr2bin(struct sample *smp)
764{
765 struct chunk *chk = get_trash_chunk();
766
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200767 if (smp->data.type == SMP_T_IPV4) {
Willy Tarreau9700e5c2014-07-15 21:03:26 +0200768 chk->len = 4;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200769 memcpy(chk->str, &smp->data.u.ipv4, chk->len);
Willy Tarreau9700e5c2014-07-15 21:03:26 +0200770 }
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200771 else if (smp->data.type == SMP_T_IPV6) {
Willy Tarreau9700e5c2014-07-15 21:03:26 +0200772 chk->len = 16;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200773 memcpy(chk->str, &smp->data.u.ipv6, chk->len);
Willy Tarreau9700e5c2014-07-15 21:03:26 +0200774 }
775 else
776 return 0;
777
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200778 smp->data.u.str = *chk;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200779 smp->data.type = SMP_T_BIN;
Willy Tarreau9700e5c2014-07-15 21:03:26 +0200780 return 1;
781}
782
Willy Tarreaubbfd1a22014-07-15 21:19:08 +0200783static int c_int2bin(struct sample *smp)
784{
785 struct chunk *chk = get_trash_chunk();
786
Willy Tarreau5f6e9052016-05-20 06:29:59 +0200787 *(unsigned long long int *)chk->str = my_htonll(smp->data.u.sint);
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +0200788 chk->len = 8;
Willy Tarreaubbfd1a22014-07-15 21:19:08 +0200789
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200790 smp->data.u.str = *chk;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200791 smp->data.type = SMP_T_BIN;
Willy Tarreaubbfd1a22014-07-15 21:19:08 +0200792 return 1;
793}
794
Willy Tarreau9700e5c2014-07-15 21:03:26 +0200795
Emeric Brun107ca302010-01-04 16:16:05 +0100796/*****************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +0200797/* Sample casts matrix: */
798/* sample_casts[from type][to type] */
799/* NULL pointer used for impossible sample casts */
Emeric Brun107ca302010-01-04 16:16:05 +0100800/*****************************************************************/
Emeric Brun107ca302010-01-04 16:16:05 +0100801
Thierry FOURNIER8af6ff12013-11-21 10:53:12 +0100802sample_cast_fct sample_casts[SMP_TYPES][SMP_TYPES] = {
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +0200803/* to: ANY BOOL SINT ADDR IPV4 IPV6 STR BIN METH */
804/* from: ANY */ { c_none, c_none, c_none, c_none, c_none, c_none, c_none, c_none, c_none, },
805/* BOOL */ { c_none, c_none, c_none, NULL, NULL, NULL, c_int2str, NULL, NULL, },
Thierry FOURNIERcc4d1712015-07-24 09:04:56 +0200806/* 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 +0200807/* ADDR */ { c_none, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, },
808/* IPV4 */ { c_none, NULL, c_ip2int, c_none, c_none, c_ip2ipv6, c_ip2str, c_addr2bin, NULL, },
Thierry FOURNIERcc4d1712015-07-24 09:04:56 +0200809/* IPV6 */ { c_none, NULL, NULL, c_none, c_ipv62ip,c_none, c_ipv62str, c_addr2bin, NULL, },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +0200810/* STR */ { c_none, c_str2int, c_str2int, c_str2addr, c_str2ip, c_str2ipv6, c_none, c_none, c_str2meth, },
811/* BIN */ { c_none, NULL, NULL, NULL, NULL, NULL, c_bin2str, c_none, c_str2meth, },
812/* METH */ { c_none, NULL, NULL, NULL, NULL, NULL, c_meth2str, c_meth2str, c_none, }
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200813};
Emeric Brun107ca302010-01-04 16:16:05 +0100814
Emeric Brun107ca302010-01-04 16:16:05 +0100815/*
Willy Tarreau12785782012-04-27 21:37:17 +0200816 * Parse a sample expression configuration:
Emeric Brun107ca302010-01-04 16:16:05 +0100817 * fetch keyword followed by format conversion keywords.
Willy Tarreau12785782012-04-27 21:37:17 +0200818 * Returns a pointer on allocated sample expression structure.
Willy Tarreaua4312fa2013-04-02 16:34:32 +0200819 * The caller must have set al->ctx.
Emeric Brun107ca302010-01-04 16:16:05 +0100820 */
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +0100821struct 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 +0100822{
Willy Tarreau833cc792013-07-24 15:34:19 +0200823 const char *begw; /* beginning of word */
824 const char *endw; /* end of word */
825 const char *endt; /* end of term */
Willy Tarreau12785782012-04-27 21:37:17 +0200826 struct sample_expr *expr;
827 struct sample_fetch *fetch;
828 struct sample_conv *conv;
Emeric Brun107ca302010-01-04 16:16:05 +0100829 unsigned long prev_type;
Willy Tarreau833cc792013-07-24 15:34:19 +0200830 char *fkw = NULL;
831 char *ckw = NULL;
Willy Tarreau689a1df2013-12-13 00:40:11 +0100832 int err_arg;
Emeric Brun107ca302010-01-04 16:16:05 +0100833
Willy Tarreau833cc792013-07-24 15:34:19 +0200834 begw = str[*idx];
835 for (endw = begw; *endw && *endw != '(' && *endw != ','; endw++);
836
837 if (endw == begw) {
Willy Tarreau975c1782013-12-12 23:16:54 +0100838 memprintf(err_msg, "missing fetch method");
Emeric Brun107ca302010-01-04 16:16:05 +0100839 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200840 }
Emeric Brun107ca302010-01-04 16:16:05 +0100841
Willy Tarreau833cc792013-07-24 15:34:19 +0200842 /* keep a copy of the current fetch keyword for error reporting */
843 fkw = my_strndup(begw, endw - begw);
Emeric Brun107ca302010-01-04 16:16:05 +0100844
Willy Tarreau833cc792013-07-24 15:34:19 +0200845 fetch = find_sample_fetch(begw, endw - begw);
846 if (!fetch) {
Willy Tarreau975c1782013-12-12 23:16:54 +0100847 memprintf(err_msg, "unknown fetch method '%s'", fkw);
Emeric Brun107ca302010-01-04 16:16:05 +0100848 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200849 }
Emeric Brun107ca302010-01-04 16:16:05 +0100850
Willy Tarreau833cc792013-07-24 15:34:19 +0200851 endt = endw;
852 if (*endt == '(') {
Willy Tarreau689a1df2013-12-13 00:40:11 +0100853 /* look for the end of this term and skip the opening parenthesis */
854 endt = ++endw;
Willy Tarreau833cc792013-07-24 15:34:19 +0200855 while (*endt && *endt != ')')
856 endt++;
857 if (*endt != ')') {
Willy Tarreau975c1782013-12-12 23:16:54 +0100858 memprintf(err_msg, "missing closing ')' after arguments to fetch keyword '%s'", fkw);
Willy Tarreau833cc792013-07-24 15:34:19 +0200859 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200860 }
Emeric Brun485479d2010-09-23 18:02:19 +0200861 }
Emeric Brun107ca302010-01-04 16:16:05 +0100862
Willy Tarreau833cc792013-07-24 15:34:19 +0200863 /* At this point, we have :
864 * - begw : beginning of the keyword
Willy Tarreau689a1df2013-12-13 00:40:11 +0100865 * - endw : end of the keyword, first character not part of keyword
866 * nor the opening parenthesis (so first character of args
867 * if present).
Willy Tarreau833cc792013-07-24 15:34:19 +0200868 * - endt : end of the term (=endw or last parenthesis if args are present)
869 */
870
871 if (fetch->out_type >= SMP_TYPES) {
Willy Tarreau975c1782013-12-12 23:16:54 +0100872 memprintf(err_msg, "returns type of fetch method '%s' is unknown", fkw);
Emeric Brun107ca302010-01-04 16:16:05 +0100873 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200874 }
Emeric Brun107ca302010-01-04 16:16:05 +0100875 prev_type = fetch->out_type;
Willy Tarreau833cc792013-07-24 15:34:19 +0200876
Vincent Bernat02779b62016-04-03 13:48:43 +0200877 expr = calloc(1, sizeof(*expr));
Emeric Brun485479d2010-09-23 18:02:19 +0200878 if (!expr)
879 goto out_error;
Emeric Brun107ca302010-01-04 16:16:05 +0100880
881 LIST_INIT(&(expr->conv_exprs));
882 expr->fetch = fetch;
Willy Tarreau2e845be2012-10-19 19:49:09 +0200883 expr->arg_p = empty_arg_list;
Emeric Brun107ca302010-01-04 16:16:05 +0100884
Willy Tarreau689a1df2013-12-13 00:40:11 +0100885 /* Note that we call the argument parser even with an empty string,
886 * this allows it to automatically create entries for mandatory
887 * implicit arguments (eg: local proxy name).
888 */
889 al->kw = expr->fetch->kw;
890 al->conv = NULL;
891 if (make_arg_list(endw, endt - endw, fetch->arg_mask, &expr->arg_p, err_msg, NULL, &err_arg, al) < 0) {
892 memprintf(err_msg, "fetch method '%s' : %s", fkw, *err_msg);
893 goto out_error;
894 }
Willy Tarreau2e845be2012-10-19 19:49:09 +0200895
Willy Tarreau689a1df2013-12-13 00:40:11 +0100896 if (!expr->arg_p) {
897 expr->arg_p = empty_arg_list;
Emeric Brun485479d2010-09-23 18:02:19 +0200898 }
Willy Tarreau689a1df2013-12-13 00:40:11 +0100899 else if (fetch->val_args && !fetch->val_args(expr->arg_p, err_msg)) {
900 memprintf(err_msg, "invalid args in fetch method '%s' : %s", fkw, *err_msg);
Emeric Brun485479d2010-09-23 18:02:19 +0200901 goto out_error;
Emeric Brun107ca302010-01-04 16:16:05 +0100902 }
903
Willy Tarreau833cc792013-07-24 15:34:19 +0200904 /* Now process the converters if any. We have two supported syntaxes
905 * for the converters, which can be combined :
906 * - comma-delimited list of converters just after the keyword and args ;
907 * - one converter per keyword
908 * The combination allows to have each keyword being a comma-delimited
909 * series of converters.
910 *
911 * We want to process the former first, then the latter. For this we start
912 * from the beginning of the supposed place in the exiting conv chain, which
913 * starts at the last comma (endt).
914 */
915
916 while (1) {
Willy Tarreau12785782012-04-27 21:37:17 +0200917 struct sample_conv_expr *conv_expr;
Emeric Brun107ca302010-01-04 16:16:05 +0100918
Willy Tarreau833cc792013-07-24 15:34:19 +0200919 if (*endt == ')') /* skip last closing parenthesis */
920 endt++;
Emeric Brun107ca302010-01-04 16:16:05 +0100921
Willy Tarreau833cc792013-07-24 15:34:19 +0200922 if (*endt && *endt != ',') {
923 if (ckw)
Willy Tarreau97108e02016-11-25 07:33:24 +0100924 memprintf(err_msg, "missing comma after converter '%s'", ckw);
Willy Tarreau833cc792013-07-24 15:34:19 +0200925 else
Willy Tarreau975c1782013-12-12 23:16:54 +0100926 memprintf(err_msg, "missing comma after fetch keyword '%s'", fkw);
Emeric Brun107ca302010-01-04 16:16:05 +0100927 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200928 }
Emeric Brun107ca302010-01-04 16:16:05 +0100929
Willy Tarreau833cc792013-07-24 15:34:19 +0200930 while (*endt == ',') /* then trailing commas */
931 endt++;
932
Willy Tarreau97108e02016-11-25 07:33:24 +0100933 begw = endt; /* start of converter */
Willy Tarreau833cc792013-07-24 15:34:19 +0200934
935 if (!*begw) {
936 /* none ? skip to next string */
937 (*idx)++;
938 begw = str[*idx];
939 if (!begw || !*begw)
940 break;
941 }
942
943 for (endw = begw; *endw && *endw != '(' && *endw != ','; endw++);
944
945 free(ckw);
946 ckw = my_strndup(begw, endw - begw);
947
948 conv = find_sample_conv(begw, endw - begw);
949 if (!conv) {
950 /* we found an isolated keyword that we don't know, it's not ours */
951 if (begw == str[*idx])
952 break;
Willy Tarreau97108e02016-11-25 07:33:24 +0100953 memprintf(err_msg, "unknown converter '%s'", ckw);
Willy Tarreau833cc792013-07-24 15:34:19 +0200954 goto out_error;
955 }
Emeric Brun107ca302010-01-04 16:16:05 +0100956
Willy Tarreau833cc792013-07-24 15:34:19 +0200957 endt = endw;
958 if (*endt == '(') {
959 /* look for the end of this term */
960 while (*endt && *endt != ')')
961 endt++;
962 if (*endt != ')') {
Willy Tarreau97108e02016-11-25 07:33:24 +0100963 memprintf(err_msg, "syntax error: missing ')' after converter '%s'", ckw);
Willy Tarreau833cc792013-07-24 15:34:19 +0200964 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200965 }
Willy Tarreau833cc792013-07-24 15:34:19 +0200966 }
967
968 if (conv->in_type >= SMP_TYPES || conv->out_type >= SMP_TYPES) {
Willy Tarreau97108e02016-11-25 07:33:24 +0100969 memprintf(err_msg, "returns type of converter '%s' is unknown", ckw);
Emeric Brun107ca302010-01-04 16:16:05 +0100970 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200971 }
Emeric Brun107ca302010-01-04 16:16:05 +0100972
973 /* If impossible type conversion */
Willy Tarreau12785782012-04-27 21:37:17 +0200974 if (!sample_casts[prev_type][conv->in_type]) {
Willy Tarreau97108e02016-11-25 07:33:24 +0100975 memprintf(err_msg, "converter '%s' cannot be applied", ckw);
Emeric Brun107ca302010-01-04 16:16:05 +0100976 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200977 }
Emeric Brun107ca302010-01-04 16:16:05 +0100978
979 prev_type = conv->out_type;
Vincent Bernat02779b62016-04-03 13:48:43 +0200980 conv_expr = calloc(1, sizeof(*conv_expr));
Emeric Brun485479d2010-09-23 18:02:19 +0200981 if (!conv_expr)
982 goto out_error;
Emeric Brun107ca302010-01-04 16:16:05 +0100983
984 LIST_ADDQ(&(expr->conv_exprs), &(conv_expr->list));
985 conv_expr->conv = conv;
986
Willy Tarreau833cc792013-07-24 15:34:19 +0200987 if (endt != endw) {
Willy Tarreaub27c0d32012-04-20 16:04:47 +0200988 int err_arg;
Willy Tarreau21d68a62012-04-20 15:52:36 +0200989
Willy Tarreau9fcb9842012-04-20 14:45:49 +0200990 if (!conv->arg_mask) {
Willy Tarreau97108e02016-11-25 07:33:24 +0100991 memprintf(err_msg, "converter '%s' does not support any args", ckw);
Emeric Brun485479d2010-09-23 18:02:19 +0200992 goto out_error;
993 }
Willy Tarreau9e92d322010-01-26 17:58:06 +0100994
Willy Tarreaua4312fa2013-04-02 16:34:32 +0200995 al->kw = expr->fetch->kw;
996 al->conv = conv_expr->conv->kw;
Willy Tarreau975c1782013-12-12 23:16:54 +0100997 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 +0100998 memprintf(err_msg, "invalid arg %d in converter '%s' : %s", err_arg+1, ckw, *err_msg);
Willy Tarreau21d68a62012-04-20 15:52:36 +0200999 goto out_error;
1000 }
1001
Willy Tarreau2e845be2012-10-19 19:49:09 +02001002 if (!conv_expr->arg_p)
1003 conv_expr->arg_p = empty_arg_list;
1004
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +01001005 if (conv->val_args && !conv->val_args(conv_expr->arg_p, conv, file, line, err_msg)) {
Willy Tarreau97108e02016-11-25 07:33:24 +01001006 memprintf(err_msg, "invalid args in converter '%s' : %s", ckw, *err_msg);
Emeric Brun485479d2010-09-23 18:02:19 +02001007 goto out_error;
1008 }
1009 }
Willy Tarreau833cc792013-07-24 15:34:19 +02001010 else if (ARGM(conv->arg_mask)) {
Willy Tarreau97108e02016-11-25 07:33:24 +01001011 memprintf(err_msg, "missing args for converter '%s'", ckw);
Emeric Brun485479d2010-09-23 18:02:19 +02001012 goto out_error;
Emeric Brun107ca302010-01-04 16:16:05 +01001013 }
1014 }
Emeric Brun485479d2010-09-23 18:02:19 +02001015
Willy Tarreau833cc792013-07-24 15:34:19 +02001016 out:
1017 free(fkw);
1018 free(ckw);
Emeric Brun107ca302010-01-04 16:16:05 +01001019 return expr;
1020
1021out_error:
Willy Tarreau12785782012-04-27 21:37:17 +02001022 /* TODO: prune_sample_expr(expr); */
Willy Tarreau833cc792013-07-24 15:34:19 +02001023 expr = NULL;
1024 goto out;
Emeric Brun107ca302010-01-04 16:16:05 +01001025}
1026
1027/*
Willy Tarreau12785782012-04-27 21:37:17 +02001028 * Process a fetch + format conversion of defined by the sample expression <expr>
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001029 * on request or response considering the <opt> parameter.
Willy Tarreau12785782012-04-27 21:37:17 +02001030 * Returns a pointer on a typed sample structure containing the result or NULL if
1031 * sample is not found or when format conversion failed.
Emeric Brun107ca302010-01-04 16:16:05 +01001032 * If <p> is not null, function returns results in structure pointed by <p>.
Willy Tarreau12785782012-04-27 21:37:17 +02001033 * If <p> is null, functions returns a pointer on a static sample structure.
Willy Tarreaub8c8f1f2012-04-23 22:38:26 +02001034 *
1035 * Note: the fetch functions are required to properly set the return type. The
1036 * conversion functions must do so too. However the cast functions do not need
1037 * to since they're made to cast mutiple types according to what is required.
Willy Tarreau6bcb0a82014-07-30 08:56:35 +02001038 *
1039 * The caller may indicate in <opt> if it considers the result final or not.
1040 * The caller needs to check the SMP_F_MAY_CHANGE flag in p->flags to verify
1041 * if the result is stable or not, according to the following table :
1042 *
1043 * return MAY_CHANGE FINAL Meaning for the sample
1044 * NULL 0 * Not present and will never be (eg: header)
1045 * NULL 1 0 Not present yet, could change (eg: POST param)
1046 * NULL 1 1 Not present yet, will not change anymore
1047 * smp 0 * Present and will not change (eg: header)
1048 * smp 1 0 Present, may change (eg: request length)
1049 * smp 1 1 Present, last known value (eg: request length)
Emeric Brun107ca302010-01-04 16:16:05 +01001050 */
Willy Tarreau192252e2015-04-04 01:47:55 +02001051struct sample *sample_process(struct proxy *px, struct session *sess,
1052 struct stream *strm, unsigned int opt,
Willy Tarreau12785782012-04-27 21:37:17 +02001053 struct sample_expr *expr, struct sample *p)
Emeric Brun107ca302010-01-04 16:16:05 +01001054{
Willy Tarreau12785782012-04-27 21:37:17 +02001055 struct sample_conv_expr *conv_expr;
Emeric Brun107ca302010-01-04 16:16:05 +01001056
Willy Tarreau18387e22013-07-25 12:02:38 +02001057 if (p == NULL) {
Willy Tarreaub4a88f02012-04-23 21:35:11 +02001058 p = &temp_smp;
Willy Tarreau6c616e02014-06-25 16:56:41 +02001059 memset(p, 0, sizeof(*p));
Willy Tarreau18387e22013-07-25 12:02:38 +02001060 }
Emeric Brun107ca302010-01-04 16:16:05 +01001061
Willy Tarreau1777ea62016-03-10 16:15:46 +01001062 smp_set_owner(p, px, sess, strm, opt);
Thierry FOURNIER0786d052015-05-11 15:42:45 +02001063 if (!expr->fetch->process(expr->arg_p, p, expr->fetch->kw, expr->fetch->private))
Emeric Brun107ca302010-01-04 16:16:05 +01001064 return NULL;
1065
Emeric Brun107ca302010-01-04 16:16:05 +01001066 list_for_each_entry(conv_expr, &expr->conv_exprs, list) {
Willy Tarreau12e50112012-04-25 17:21:49 +02001067 /* we want to ensure that p->type can be casted into
1068 * conv_expr->conv->in_type. We have 3 possibilities :
1069 * - NULL => not castable.
1070 * - c_none => nothing to do (let's optimize it)
1071 * - other => apply cast and prepare to fail
1072 */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001073 if (!sample_casts[p->data.type][conv_expr->conv->in_type])
Willy Tarreau12e50112012-04-25 17:21:49 +02001074 return NULL;
1075
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001076 if (sample_casts[p->data.type][conv_expr->conv->in_type] != c_none &&
1077 !sample_casts[p->data.type][conv_expr->conv->in_type](p))
Emeric Brun107ca302010-01-04 16:16:05 +01001078 return NULL;
1079
Willy Tarreau12e50112012-04-25 17:21:49 +02001080 /* OK cast succeeded */
1081
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001082 if (!conv_expr->conv->process(conv_expr->arg_p, p, conv_expr->conv->private))
Emeric Brun107ca302010-01-04 16:16:05 +01001083 return NULL;
Emeric Brun107ca302010-01-04 16:16:05 +01001084 }
1085 return p;
1086}
1087
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001088/*
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001089 * Resolve all remaining arguments in proxy <p>. Returns the number of
1090 * errors or 0 if everything is fine.
1091 */
1092int smp_resolve_args(struct proxy *p)
1093{
1094 struct arg_list *cur, *bak;
1095 const char *ctx, *where;
1096 const char *conv_ctx, *conv_pre, *conv_pos;
1097 struct userlist *ul;
Willy Tarreau46947782015-01-19 19:00:58 +01001098 struct my_regex *reg;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001099 struct arg *arg;
1100 int cfgerr = 0;
Willy Tarreau46947782015-01-19 19:00:58 +01001101 int rflags;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001102
1103 list_for_each_entry_safe(cur, bak, &p->conf.args.list, list) {
1104 struct proxy *px;
1105 struct server *srv;
1106 char *pname, *sname;
Willy Tarreau46947782015-01-19 19:00:58 +01001107 char *err;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001108
1109 arg = cur->arg;
1110
1111 /* prepare output messages */
1112 conv_pre = conv_pos = conv_ctx = "";
1113 if (cur->conv) {
1114 conv_ctx = cur->conv;
1115 conv_pre = "conversion keyword '";
1116 conv_pos = "' for ";
1117 }
1118
1119 where = "in";
1120 ctx = "sample fetch keyword";
1121 switch (cur->ctx) {
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001122 case ARGC_STK: where = "in stick rule in"; break;
1123 case ARGC_TRK: where = "in tracking rule in"; break;
1124 case ARGC_LOG: where = "in log-format string in"; break;
1125 case ARGC_LOGSD: where = "in log-format-sd string in"; break;
1126 case ARGC_HRQ: where = "in http-request header format string in"; break;
1127 case ARGC_HRS: where = "in http-response header format string in"; break;
1128 case ARGC_UIF: where = "in unique-id-format string in"; break;
1129 case ARGC_RDR: where = "in redirect format string in"; break;
1130 case ARGC_CAP: where = "in capture rule in"; break;
1131 case ARGC_ACL: ctx = "ACL keyword"; break;
1132 case ARGC_SRV: where = "in server directive in"; break;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001133 case ARGC_SPOE: where = "in spoe-message directive in"; break;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001134 }
1135
1136 /* set a few default settings */
1137 px = p;
1138 pname = p->id;
1139
1140 switch (arg->type) {
1141 case ARGT_SRV:
1142 if (!arg->data.str.len) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001143 ha_alert("parsing [%s:%d] : missing server name in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1144 cur->file, cur->line,
1145 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001146 cfgerr++;
1147 continue;
1148 }
1149
1150 /* we support two formats : "bck/srv" and "srv" */
1151 sname = strrchr(arg->data.str.str, '/');
1152
1153 if (sname) {
1154 *sname++ = '\0';
1155 pname = arg->data.str.str;
1156
Willy Tarreau9e0bb102015-05-26 11:24:42 +02001157 px = proxy_be_by_name(pname);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001158 if (!px) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001159 ha_alert("parsing [%s:%d] : unable to find proxy '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1160 cur->file, cur->line, pname,
1161 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001162 cfgerr++;
1163 break;
1164 }
1165 }
1166 else
1167 sname = arg->data.str.str;
1168
1169 srv = findserver(px, sname);
1170 if (!srv) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001171 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",
1172 cur->file, cur->line, sname, pname,
1173 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001174 cfgerr++;
1175 break;
1176 }
1177
1178 free(arg->data.str.str);
1179 arg->data.str.str = NULL;
1180 arg->unresolved = 0;
1181 arg->data.srv = srv;
1182 break;
1183
1184 case ARGT_FE:
1185 if (arg->data.str.len) {
1186 pname = arg->data.str.str;
Willy Tarreau9e0bb102015-05-26 11:24:42 +02001187 px = proxy_fe_by_name(pname);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001188 }
1189
1190 if (!px) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001191 ha_alert("parsing [%s:%d] : unable to find frontend '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1192 cur->file, cur->line, pname,
1193 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001194 cfgerr++;
1195 break;
1196 }
1197
1198 if (!(px->cap & PR_CAP_FE)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001199 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",
1200 cur->file, cur->line, pname,
1201 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001202 cfgerr++;
1203 break;
1204 }
1205
1206 free(arg->data.str.str);
1207 arg->data.str.str = NULL;
1208 arg->unresolved = 0;
1209 arg->data.prx = px;
1210 break;
1211
1212 case ARGT_BE:
1213 if (arg->data.str.len) {
1214 pname = arg->data.str.str;
Willy Tarreau9e0bb102015-05-26 11:24:42 +02001215 px = proxy_be_by_name(pname);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001216 }
1217
1218 if (!px) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001219 ha_alert("parsing [%s:%d] : unable to find backend '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1220 cur->file, cur->line, pname,
1221 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001222 cfgerr++;
1223 break;
1224 }
1225
1226 if (!(px->cap & PR_CAP_BE)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001227 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",
1228 cur->file, cur->line, pname,
1229 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001230 cfgerr++;
1231 break;
1232 }
1233
1234 free(arg->data.str.str);
1235 arg->data.str.str = NULL;
1236 arg->unresolved = 0;
1237 arg->data.prx = px;
1238 break;
1239
1240 case ARGT_TAB:
1241 if (arg->data.str.len) {
1242 pname = arg->data.str.str;
Willy Tarreaue2dc1fa2015-05-26 12:08:07 +02001243 px = proxy_tbl_by_name(pname);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001244 }
1245
1246 if (!px) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001247 ha_alert("parsing [%s:%d] : unable to find table '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1248 cur->file, cur->line, pname,
1249 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001250 cfgerr++;
1251 break;
1252 }
1253
1254 if (!px->table.size) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001255 ha_alert("parsing [%s:%d] : no table in proxy '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1256 cur->file, cur->line, pname,
1257 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001258 cfgerr++;
1259 break;
1260 }
1261
1262 free(arg->data.str.str);
1263 arg->data.str.str = NULL;
1264 arg->unresolved = 0;
1265 arg->data.prx = px;
1266 break;
1267
1268 case ARGT_USR:
1269 if (!arg->data.str.len) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001270 ha_alert("parsing [%s:%d] : missing userlist name in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1271 cur->file, cur->line,
1272 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001273 cfgerr++;
1274 break;
1275 }
1276
1277 if (p->uri_auth && p->uri_auth->userlist &&
1278 !strcmp(p->uri_auth->userlist->name, arg->data.str.str))
1279 ul = p->uri_auth->userlist;
1280 else
1281 ul = auth_find_userlist(arg->data.str.str);
1282
1283 if (!ul) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001284 ha_alert("parsing [%s:%d] : unable to find userlist '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1285 cur->file, cur->line, arg->data.str.str,
1286 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001287 cfgerr++;
1288 break;
1289 }
1290
1291 free(arg->data.str.str);
1292 arg->data.str.str = NULL;
1293 arg->unresolved = 0;
1294 arg->data.usr = ul;
1295 break;
Willy Tarreau46947782015-01-19 19:00:58 +01001296
1297 case ARGT_REG:
1298 if (!arg->data.str.len) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001299 ha_alert("parsing [%s:%d] : missing regex in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1300 cur->file, cur->line,
1301 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreau46947782015-01-19 19:00:58 +01001302 cfgerr++;
1303 continue;
1304 }
1305
1306 reg = calloc(1, sizeof(*reg));
1307 if (!reg) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001308 ha_alert("parsing [%s:%d] : not enough memory to build regex in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1309 cur->file, cur->line,
1310 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreau46947782015-01-19 19:00:58 +01001311 cfgerr++;
1312 continue;
1313 }
1314
1315 rflags = 0;
1316 rflags |= (arg->type_flags & ARGF_REG_ICASE) ? REG_ICASE : 0;
1317 err = NULL;
1318
Willy Tarreaud817e462015-01-23 20:23:17 +01001319 if (!regex_comp(arg->data.str.str, reg, !(rflags & REG_ICASE), 1 /* capture substr */, &err)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001320 ha_alert("parsing [%s:%d] : error in regex '%s' in arg %d of %s%s%s%s '%s' %s proxy '%s' : %s.\n",
1321 cur->file, cur->line,
1322 arg->data.str.str,
1323 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id, err);
Willy Tarreau46947782015-01-19 19:00:58 +01001324 cfgerr++;
1325 continue;
1326 }
1327
1328 free(arg->data.str.str);
1329 arg->data.str.str = NULL;
1330 arg->unresolved = 0;
1331 arg->data.reg = reg;
1332 break;
1333
1334
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001335 }
1336
1337 LIST_DEL(&cur->list);
1338 free(cur);
1339 } /* end of args processing */
1340
1341 return cfgerr;
1342}
1343
1344/*
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001345 * Process a fetch + format conversion as defined by the sample expression
1346 * <expr> on request or response considering the <opt> parameter. The output is
Adis Nezirovic79beb242015-07-06 15:41:02 +02001347 * not explicitly set to <smp_type>, but shall be compatible with it as
1348 * specified by 'sample_casts' table. If a stable sample can be fetched, or an
1349 * unstable one when <opt> contains SMP_OPT_FINAL, the sample is converted and
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001350 * returned without the SMP_F_MAY_CHANGE flag. If an unstable sample is found
1351 * and <opt> does not contain SMP_OPT_FINAL, then the sample is returned as-is
1352 * with its SMP_F_MAY_CHANGE flag so that the caller can check it and decide to
1353 * take actions (eg: wait longer). If a sample could not be found or could not
Willy Tarreau6bcb0a82014-07-30 08:56:35 +02001354 * be converted, NULL is returned. The caller MUST NOT use the sample if the
1355 * SMP_F_MAY_CHANGE flag is present, as it is used only as a hint that there is
1356 * still hope to get it after waiting longer, and is not converted to string.
1357 * The possible output combinations are the following :
1358 *
1359 * return MAY_CHANGE FINAL Meaning for the sample
1360 * NULL * * Not present and will never be (eg: header)
1361 * smp 0 * Final value converted (eg: header)
1362 * smp 1 0 Not present yet, may appear later (eg: header)
1363 * smp 1 1 never happens (either flag is cleared on output)
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001364 */
Adis Nezirovic79beb242015-07-06 15:41:02 +02001365struct sample *sample_fetch_as_type(struct proxy *px, struct session *sess,
Willy Tarreau192252e2015-04-04 01:47:55 +02001366 struct stream *strm, unsigned int opt,
Adis Nezirovic79beb242015-07-06 15:41:02 +02001367 struct sample_expr *expr, int smp_type)
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001368{
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001369 struct sample *smp = &temp_smp;
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001370
Willy Tarreau6c616e02014-06-25 16:56:41 +02001371 memset(smp, 0, sizeof(*smp));
1372
Willy Tarreau192252e2015-04-04 01:47:55 +02001373 if (!sample_process(px, sess, strm, opt, expr, smp)) {
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001374 if ((smp->flags & SMP_F_MAY_CHANGE) && !(opt & SMP_OPT_FINAL))
1375 return smp;
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001376 return NULL;
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001377 }
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001378
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001379 if (!sample_casts[smp->data.type][smp_type])
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001380 return NULL;
1381
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001382 if (!sample_casts[smp->data.type][smp_type](smp))
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001383 return NULL;
1384
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001385 smp->flags &= ~SMP_F_MAY_CHANGE;
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001386 return smp;
1387}
1388
Christopher Faulet476e5d02016-10-26 11:34:47 +02001389static void release_sample_arg(struct arg *p)
1390{
1391 struct arg *p_back = p;
1392
1393 if (!p)
1394 return;
1395
1396 while (p->type != ARGT_STOP) {
1397 if (p->type == ARGT_STR || p->unresolved) {
1398 free(p->data.str.str);
1399 p->data.str.str = NULL;
1400 p->unresolved = 0;
1401 }
1402 else if (p->type == ARGT_REG) {
1403 if (p->data.reg) {
1404 regex_free(p->data.reg);
1405 free(p->data.reg);
1406 p->data.reg = NULL;
1407 }
1408 }
1409 p++;
1410 }
1411
1412 if (p_back != empty_arg_list)
1413 free(p_back);
1414}
1415
1416void release_sample_expr(struct sample_expr *expr)
1417{
1418 struct sample_conv_expr *conv_expr, *conv_exprb;
1419
1420 if (!expr)
1421 return;
1422
1423 list_for_each_entry_safe(conv_expr, conv_exprb, &expr->conv_exprs, list)
1424 release_sample_arg(conv_expr->arg_p);
1425 release_sample_arg(expr->arg_p);
1426 free(expr);
1427}
1428
Emeric Brun107ca302010-01-04 16:16:05 +01001429/*****************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +02001430/* Sample format convert functions */
Willy Tarreaub8c8f1f2012-04-23 22:38:26 +02001431/* These functions set the data type on return. */
Emeric Brun107ca302010-01-04 16:16:05 +01001432/*****************************************************************/
1433
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001434#ifdef DEBUG_EXPR
1435static int sample_conv_debug(const struct arg *arg_p, struct sample *smp, void *private)
1436{
1437 int i;
1438 struct sample tmp;
1439
1440 if (!(global.mode & MODE_QUIET) || (global.mode & (MODE_VERBOSE | MODE_STARTING))) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001441 fprintf(stderr, "[debug converter] type: %s ", smp_to_type[smp->data.type]);
1442 if (!sample_casts[smp->data.type][SMP_T_STR]) {
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001443 fprintf(stderr, "(undisplayable)");
1444 } else {
1445
1446 /* Copy sample fetch. This put the sample as const, the
1447 * cast will copy data if a transformation is required.
1448 */
1449 memcpy(&tmp, smp, sizeof(struct sample));
1450 tmp.flags = SMP_F_CONST;
1451
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001452 if (!sample_casts[smp->data.type][SMP_T_STR](&tmp))
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001453 fprintf(stderr, "(undisplayable)");
1454
1455 else {
1456 /* Display the displayable chars*. */
1457 fprintf(stderr, "<");
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001458 for (i = 0; i < tmp.data.u.str.len; i++) {
1459 if (isprint(tmp.data.u.str.str[i]))
1460 fputc(tmp.data.u.str.str[i], stderr);
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001461 else
1462 fputc('.', stderr);
1463 }
1464 }
1465 fprintf(stderr, ">\n");
1466 }
1467 }
1468 return 1;
1469}
1470#endif
1471
Holger Just1bfc24b2017-05-06 00:56:53 +02001472static int sample_conv_base642bin(const struct arg *arg_p, struct sample *smp, void *private)
1473{
1474 struct chunk *trash = get_trash_chunk();
1475 int bin_len;
1476
1477 trash->len = 0;
1478 bin_len = base64dec(smp->data.u.str.str, smp->data.u.str.len, trash->str, trash->size);
1479 if (bin_len < 0)
1480 return 0;
1481
1482 trash->len = bin_len;
1483 smp->data.u.str = *trash;
1484 smp->data.type = SMP_T_BIN;
1485 smp->flags &= ~SMP_F_CONST;
1486 return 1;
1487}
1488
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001489static int sample_conv_bin2base64(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brun53d1a982014-04-30 18:21:37 +02001490{
1491 struct chunk *trash = get_trash_chunk();
1492 int b64_len;
1493
1494 trash->len = 0;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001495 b64_len = a2base64(smp->data.u.str.str, smp->data.u.str.len, trash->str, trash->size);
Emeric Brun53d1a982014-04-30 18:21:37 +02001496 if (b64_len < 0)
1497 return 0;
1498
1499 trash->len = b64_len;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001500 smp->data.u.str = *trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001501 smp->data.type = SMP_T_STR;
Emeric Brun53d1a982014-04-30 18:21:37 +02001502 smp->flags &= ~SMP_F_CONST;
1503 return 1;
1504}
1505
Dragan Dosen6e5a9ca2017-10-24 09:18:23 +02001506static int sample_conv_sha1(const struct arg *arg_p, struct sample *smp, void *private)
1507{
1508 blk_SHA_CTX ctx;
1509 struct chunk *trash = get_trash_chunk();
1510
1511 memset(&ctx, 0, sizeof(ctx));
1512
1513 blk_SHA1_Init(&ctx);
1514 blk_SHA1_Update(&ctx, smp->data.u.str.str, smp->data.u.str.len);
1515 blk_SHA1_Final((unsigned char *)trash->str, &ctx);
1516
1517 trash->len = 20;
1518 smp->data.u.str = *trash;
1519 smp->data.type = SMP_T_BIN;
1520 smp->flags &= ~SMP_F_CONST;
1521 return 1;
1522}
1523
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001524static int sample_conv_bin2hex(const struct arg *arg_p, struct sample *smp, void *private)
Thierry FOURNIER2f49d6d2014-03-12 15:01:52 +01001525{
1526 struct chunk *trash = get_trash_chunk();
1527 unsigned char c;
1528 int ptr = 0;
1529
1530 trash->len = 0;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001531 while (ptr < smp->data.u.str.len && trash->len <= trash->size - 2) {
1532 c = smp->data.u.str.str[ptr++];
Thierry FOURNIER2f49d6d2014-03-12 15:01:52 +01001533 trash->str[trash->len++] = hextab[(c >> 4) & 0xF];
1534 trash->str[trash->len++] = hextab[c & 0xF];
1535 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001536 smp->data.u.str = *trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001537 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01001538 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIER2f49d6d2014-03-12 15:01:52 +01001539 return 1;
1540}
1541
Dragan Dosen3f957b22017-10-24 09:27:34 +02001542static int sample_conv_hex2int(const struct arg *arg_p, struct sample *smp, void *private)
1543{
1544 long long int n = 0;
1545 int i, c;
1546
1547 for (i = 0; i < smp->data.u.str.len; i++) {
1548 if ((c = hex2i(smp->data.u.str.str[i])) < 0)
1549 return 0;
1550 n = (n << 4) + c;
1551 }
1552
1553 smp->data.u.sint = n;
1554 smp->data.type = SMP_T_SINT;
1555 smp->flags &= ~SMP_F_CONST;
1556 return 1;
1557}
1558
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001559/* hashes the binary input into a 32-bit unsigned int */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001560static int sample_conv_djb2(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001561{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001562 smp->data.u.sint = hash_djb2(smp->data.u.str.str, smp->data.u.str.len);
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001563 if (arg_p && arg_p->data.sint)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001564 smp->data.u.sint = full_hash(smp->data.u.sint);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001565 smp->data.type = SMP_T_SINT;
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001566 return 1;
1567}
1568
Willy Tarreau60a2ee72017-12-15 07:13:48 +01001569static int sample_conv_length(const struct arg *arg_p, struct sample *smp, void *private)
Etienne Carriereed0d24e2017-12-13 13:41:34 +01001570{
1571 int i = smp->data.u.str.len;
1572 smp->data.u.sint = i;
1573 smp->data.type = SMP_T_SINT;
1574 return 1;
1575}
1576
1577
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001578static int sample_conv_str2lower(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brun107ca302010-01-04 16:16:05 +01001579{
1580 int i;
1581
Willy Tarreauf0645dc2016-08-09 14:29:38 +02001582 if (!smp_make_rw(smp))
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01001583 return 0;
1584
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001585 for (i = 0; i < smp->data.u.str.len; i++) {
1586 if ((smp->data.u.str.str[i] >= 'A') && (smp->data.u.str.str[i] <= 'Z'))
1587 smp->data.u.str.str[i] += 'a' - 'A';
Emeric Brun107ca302010-01-04 16:16:05 +01001588 }
1589 return 1;
1590}
1591
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001592static int sample_conv_str2upper(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brun107ca302010-01-04 16:16:05 +01001593{
1594 int i;
1595
Willy Tarreauf0645dc2016-08-09 14:29:38 +02001596 if (!smp_make_rw(smp))
Emeric Brun485479d2010-09-23 18:02:19 +02001597 return 0;
1598
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001599 for (i = 0; i < smp->data.u.str.len; i++) {
1600 if ((smp->data.u.str.str[i] >= 'a') && (smp->data.u.str.str[i] <= 'z'))
1601 smp->data.u.str.str[i] += 'A' - 'a';
Emeric Brun107ca302010-01-04 16:16:05 +01001602 }
1603 return 1;
1604}
1605
Tim Duesterhus1478aa72018-01-25 16:24:51 +01001606/* takes the IPv4 mask in args[0] and an optional IPv6 mask in args[1] */
1607static int sample_conv_ipmask(const struct arg *args, struct sample *smp, void *private)
Willy Tarreaud31d6eb2010-01-26 18:01:41 +01001608{
Tim Duesterhus1478aa72018-01-25 16:24:51 +01001609 /* Attempt to convert to IPv4 to apply the correct mask. */
1610 c_ipv62ip(smp);
1611
1612 if (smp->data.type == SMP_T_IPV4) {
1613 smp->data.u.ipv4.s_addr &= args[0].data.ipv4.s_addr;
1614 smp->data.type = SMP_T_IPV4;
1615 }
1616 else if (smp->data.type == SMP_T_IPV6) {
1617 /* IPv6 cannot be converted without an IPv6 mask. */
1618 if (args[1].type != ARGT_IPV6)
1619 return 0;
1620
1621 *(uint32_t*)&smp->data.u.ipv6.s6_addr[0] &= *(uint32_t*)&args[1].data.ipv6.s6_addr[0];
1622 *(uint32_t*)&smp->data.u.ipv6.s6_addr[4] &= *(uint32_t*)&args[1].data.ipv6.s6_addr[4];
1623 *(uint32_t*)&smp->data.u.ipv6.s6_addr[8] &= *(uint32_t*)&args[1].data.ipv6.s6_addr[8];
1624 *(uint32_t*)&smp->data.u.ipv6.s6_addr[12] &= *(uint32_t*)&args[1].data.ipv6.s6_addr[12];
1625 smp->data.type = SMP_T_IPV6;
1626 }
1627
Willy Tarreaud31d6eb2010-01-26 18:01:41 +01001628 return 1;
1629}
1630
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001631/* takes an UINT value on input supposed to represent the time since EPOCH,
1632 * adds an optional offset found in args[1] and emits a string representing
1633 * the local time in the format specified in args[1] using strftime().
1634 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001635static int sample_conv_ltime(const struct arg *args, struct sample *smp, void *private)
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001636{
1637 struct chunk *temp;
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001638 /* With high numbers, the date returned can be negative, the 55 bits mask prevent this. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001639 time_t curr_date = smp->data.u.sint & 0x007fffffffffffffLL;
Thierry FOURNIERfac9ccf2015-07-08 00:15:20 +02001640 struct tm *tm;
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001641
1642 /* add offset */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001643 if (args[1].type == ARGT_SINT)
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001644 curr_date += args[1].data.sint;
1645
Thierry FOURNIERfac9ccf2015-07-08 00:15:20 +02001646 tm = localtime(&curr_date);
1647 if (!tm)
1648 return 0;
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001649 temp = get_trash_chunk();
Thierry FOURNIERfac9ccf2015-07-08 00:15:20 +02001650 temp->len = strftime(temp->str, temp->size, args[0].data.str.str, tm);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001651 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001652 smp->data.type = SMP_T_STR;
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001653 return 1;
1654}
1655
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001656/* hashes the binary input into a 32-bit unsigned int */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001657static int sample_conv_sdbm(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001658{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001659 smp->data.u.sint = hash_sdbm(smp->data.u.str.str, smp->data.u.str.len);
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001660 if (arg_p && arg_p->data.sint)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001661 smp->data.u.sint = full_hash(smp->data.u.sint);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001662 smp->data.type = SMP_T_SINT;
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001663 return 1;
1664}
1665
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001666/* takes an UINT value on input supposed to represent the time since EPOCH,
1667 * adds an optional offset found in args[1] and emits a string representing
1668 * the UTC date in the format specified in args[1] using strftime().
1669 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001670static int sample_conv_utime(const struct arg *args, struct sample *smp, void *private)
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001671{
1672 struct chunk *temp;
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001673 /* With high numbers, the date returned can be negative, the 55 bits mask prevent this. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001674 time_t curr_date = smp->data.u.sint & 0x007fffffffffffffLL;
Thierry FOURNIERfac9ccf2015-07-08 00:15:20 +02001675 struct tm *tm;
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001676
1677 /* add offset */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001678 if (args[1].type == ARGT_SINT)
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001679 curr_date += args[1].data.sint;
1680
Thierry FOURNIERfac9ccf2015-07-08 00:15:20 +02001681 tm = gmtime(&curr_date);
1682 if (!tm)
1683 return 0;
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001684 temp = get_trash_chunk();
Thierry FOURNIERfac9ccf2015-07-08 00:15:20 +02001685 temp->len = strftime(temp->str, temp->size, args[0].data.str.str, tm);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001686 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001687 smp->data.type = SMP_T_STR;
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001688 return 1;
1689}
1690
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001691/* hashes the binary input into a 32-bit unsigned int */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001692static int sample_conv_wt6(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001693{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001694 smp->data.u.sint = hash_wt6(smp->data.u.str.str, smp->data.u.str.len);
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001695 if (arg_p && arg_p->data.sint)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001696 smp->data.u.sint = full_hash(smp->data.u.sint);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001697 smp->data.type = SMP_T_SINT;
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001698 return 1;
1699}
1700
Thierry FOURNIER01e09742016-12-26 11:46:11 +01001701/* hashes the binary input into a 32-bit unsigned int using xxh.
1702 * The seed of the hash defaults to 0 but can be changd in argument 1.
1703 */
1704static int sample_conv_xxh32(const struct arg *arg_p, struct sample *smp, void *private)
1705{
1706 unsigned int seed;
1707
1708 if (arg_p && arg_p->data.sint)
1709 seed = arg_p->data.sint;
1710 else
1711 seed = 0;
1712 smp->data.u.sint = XXH32(smp->data.u.str.str, smp->data.u.str.len, seed);
1713 smp->data.type = SMP_T_SINT;
1714 return 1;
1715}
1716
1717/* hashes the binary input into a 64-bit unsigned int using xxh.
1718 * In fact, the function returns a 64 bit unsigned, but the sample
1719 * storage of haproxy only proposes 64-bits signed, so the value is
1720 * cast as signed. This cast doesn't impact the hash repartition.
1721 * The seed of the hash defaults to 0 but can be changd in argument 1.
1722 */
1723static int sample_conv_xxh64(const struct arg *arg_p, struct sample *smp, void *private)
1724{
1725 unsigned long long int seed;
1726
1727 if (arg_p && arg_p->data.sint)
1728 seed = (unsigned long long int)arg_p->data.sint;
1729 else
1730 seed = 0;
1731 smp->data.u.sint = (long long int)XXH64(smp->data.u.str.str, smp->data.u.str.len, seed);
1732 smp->data.type = SMP_T_SINT;
1733 return 1;
1734}
1735
Willy Tarreau80599772015-01-20 19:35:24 +01001736/* hashes the binary input into a 32-bit unsigned int */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001737static int sample_conv_crc32(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau80599772015-01-20 19:35:24 +01001738{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001739 smp->data.u.sint = hash_crc32(smp->data.u.str.str, smp->data.u.str.len);
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001740 if (arg_p && arg_p->data.sint)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001741 smp->data.u.sint = full_hash(smp->data.u.sint);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001742 smp->data.type = SMP_T_SINT;
Willy Tarreau80599772015-01-20 19:35:24 +01001743 return 1;
1744}
1745
Emmanuel Hocdet50791a72018-03-21 11:19:01 +01001746/* hashes the binary input into crc32c (RFC4960, Appendix B [8].) */
1747static int sample_conv_crc32c(const struct arg *arg_p, struct sample *smp, void *private)
1748{
1749 smp->data.u.sint = hash_crc32c(smp->data.u.str.str, smp->data.u.str.len);
1750 if (arg_p && arg_p->data.sint)
1751 smp->data.u.sint = full_hash(smp->data.u.sint);
1752 smp->data.type = SMP_T_SINT;
1753 return 1;
1754}
1755
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001756/* This function escape special json characters. The returned string can be
1757 * safely set between two '"' and used as json string. The json string is
1758 * defined like this:
1759 *
1760 * any Unicode character except '"' or '\' or control character
1761 * \", \\, \/, \b, \f, \n, \r, \t, \u + four-hex-digits
1762 *
1763 * The enum input_type contain all the allowed mode for decoding the input
1764 * string.
1765 */
1766enum input_type {
1767 IT_ASCII = 0,
1768 IT_UTF8,
1769 IT_UTF8S,
1770 IT_UTF8P,
1771 IT_UTF8PS,
1772};
1773static int sample_conv_json_check(struct arg *arg, struct sample_conv *conv,
1774 const char *file, int line, char **err)
1775{
1776 if (!arg) {
1777 memprintf(err, "Unexpected empty arg list");
1778 return 0;
1779 }
1780
1781 if (arg->type != ARGT_STR) {
1782 memprintf(err, "Unexpected arg type");
1783 return 0;
1784 }
1785
1786 if (strcmp(arg->data.str.str, "") == 0) {
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001787 arg->type = ARGT_SINT;
1788 arg->data.sint = IT_ASCII;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001789 return 1;
1790 }
1791
1792 else if (strcmp(arg->data.str.str, "ascii") == 0) {
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001793 arg->type = ARGT_SINT;
1794 arg->data.sint = IT_ASCII;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001795 return 1;
1796 }
1797
1798 else if (strcmp(arg->data.str.str, "utf8") == 0) {
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001799 arg->type = ARGT_SINT;
1800 arg->data.sint = IT_UTF8;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001801 return 1;
1802 }
1803
1804 else if (strcmp(arg->data.str.str, "utf8s") == 0) {
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001805 arg->type = ARGT_SINT;
1806 arg->data.sint = IT_UTF8S;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001807 return 1;
1808 }
1809
1810 else if (strcmp(arg->data.str.str, "utf8p") == 0) {
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001811 arg->type = ARGT_SINT;
1812 arg->data.sint = IT_UTF8P;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001813 return 1;
1814 }
1815
1816 else if (strcmp(arg->data.str.str, "utf8ps") == 0) {
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001817 arg->type = ARGT_SINT;
1818 arg->data.sint = IT_UTF8PS;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001819 return 1;
1820 }
1821
Thierry FOURNIER / OZON.IOa69c9122016-11-23 01:13:57 +01001822 memprintf(err, "Unexpected input code type. "
1823 "Allowed value are 'ascii', 'utf8', 'utf8s', 'utf8p' and 'utf8ps'");
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001824 return 0;
1825}
1826
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001827static int sample_conv_json(const struct arg *arg_p, struct sample *smp, void *private)
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001828{
1829 struct chunk *temp;
1830 char _str[7]; /* \u + 4 hex digit + null char for sprintf. */
1831 const char *str;
1832 int len;
1833 enum input_type input_type = IT_ASCII;
1834 unsigned int c;
1835 unsigned int ret;
1836 char *p;
1837
1838 if (arg_p)
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001839 input_type = arg_p->data.sint;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001840
1841 temp = get_trash_chunk();
1842 temp->len = 0;
1843
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001844 p = smp->data.u.str.str;
1845 while (p < smp->data.u.str.str + smp->data.u.str.len) {
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001846
1847 if (input_type == IT_ASCII) {
1848 /* Read input as ASCII. */
1849 c = *(unsigned char *)p;
1850 p++;
1851 }
1852 else {
1853 /* Read input as UTF8. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001854 ret = utf8_next(p, smp->data.u.str.len - ( p - smp->data.u.str.str ), &c);
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001855 p += utf8_return_length(ret);
1856
1857 if (input_type == IT_UTF8 && utf8_return_code(ret) != UTF8_CODE_OK)
1858 return 0;
1859 if (input_type == IT_UTF8S && utf8_return_code(ret) != UTF8_CODE_OK)
1860 continue;
1861 if (input_type == IT_UTF8P && utf8_return_code(ret) & (UTF8_CODE_INVRANGE|UTF8_CODE_BADSEQ))
1862 return 0;
1863 if (input_type == IT_UTF8PS && utf8_return_code(ret) & (UTF8_CODE_INVRANGE|UTF8_CODE_BADSEQ))
1864 continue;
1865
1866 /* Check too big values. */
1867 if ((unsigned int)c > 0xffff) {
1868 if (input_type == IT_UTF8 || input_type == IT_UTF8P)
1869 return 0;
1870 continue;
1871 }
1872 }
1873
1874 /* Convert character. */
1875 if (c == '"') {
1876 len = 2;
1877 str = "\\\"";
1878 }
1879 else if (c == '\\') {
1880 len = 2;
1881 str = "\\\\";
1882 }
1883 else if (c == '/') {
1884 len = 2;
1885 str = "\\/";
1886 }
1887 else if (c == '\b') {
1888 len = 2;
1889 str = "\\b";
1890 }
1891 else if (c == '\f') {
1892 len = 2;
1893 str = "\\f";
1894 }
1895 else if (c == '\r') {
1896 len = 2;
1897 str = "\\r";
1898 }
1899 else if (c == '\n') {
1900 len = 2;
1901 str = "\\n";
1902 }
1903 else if (c == '\t') {
1904 len = 2;
1905 str = "\\t";
1906 }
1907 else if (c > 0xff || !isprint(c)) {
1908 /* isprint generate a segfault if c is too big. The man says that
1909 * c must have the value of an unsigned char or EOF.
1910 */
1911 len = 6;
1912 _str[0] = '\\';
1913 _str[1] = 'u';
1914 snprintf(&_str[2], 5, "%04x", (unsigned short)c);
1915 str = _str;
1916 }
1917 else {
1918 len = 1;
1919 str = (char *)&c;
1920 }
1921
1922 /* Check length */
1923 if (temp->len + len > temp->size)
1924 return 0;
1925
1926 /* Copy string. */
1927 memcpy(temp->str + temp->len, str, len);
1928 temp->len += len;
1929 }
1930
1931 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001932 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001933 smp->data.type = SMP_T_STR;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001934
1935 return 1;
1936}
1937
Emeric Brun54c4ac82014-11-03 15:32:43 +01001938/* This sample function is designed to extract some bytes from an input buffer.
1939 * First arg is the offset.
1940 * Optional second arg is the length to truncate */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001941static int sample_conv_bytes(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brun54c4ac82014-11-03 15:32:43 +01001942{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001943 if (smp->data.u.str.len <= arg_p[0].data.sint) {
1944 smp->data.u.str.len = 0;
Emeric Brun54c4ac82014-11-03 15:32:43 +01001945 return 1;
1946 }
1947
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001948 if (smp->data.u.str.size)
1949 smp->data.u.str.size -= arg_p[0].data.sint;
1950 smp->data.u.str.len -= arg_p[0].data.sint;
1951 smp->data.u.str.str += arg_p[0].data.sint;
Emeric Brun54c4ac82014-11-03 15:32:43 +01001952
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001953 if ((arg_p[1].type == ARGT_SINT) && (arg_p[1].data.sint < smp->data.u.str.len))
1954 smp->data.u.str.len = arg_p[1].data.sint;
Emeric Brun54c4ac82014-11-03 15:32:43 +01001955
1956 return 1;
1957}
1958
Emeric Brunf399b0d2014-11-03 17:07:03 +01001959static int sample_conv_field_check(struct arg *args, struct sample_conv *conv,
1960 const char *file, int line, char **err)
1961{
1962 struct arg *arg = args;
1963
1964 if (!arg) {
1965 memprintf(err, "Unexpected empty arg list");
1966 return 0;
1967 }
1968
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001969 if (arg->type != ARGT_SINT) {
Emeric Brunf399b0d2014-11-03 17:07:03 +01001970 memprintf(err, "Unexpected arg type");
1971 return 0;
1972 }
1973
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001974 if (!arg->data.sint) {
Emeric Brunf399b0d2014-11-03 17:07:03 +01001975 memprintf(err, "Unexpected value 0 for index");
1976 return 0;
1977 }
1978
1979 arg++;
1980
1981 if (arg->type != ARGT_STR) {
1982 memprintf(err, "Unexpected arg type");
1983 return 0;
1984 }
1985
1986 if (!arg->data.str.len) {
1987 memprintf(err, "Empty separators list");
1988 return 0;
1989 }
1990
1991 return 1;
1992}
1993
1994/* This sample function is designed to a return selected part of a string (field).
1995 * First arg is the index of the field (start at 1)
1996 * Second arg is a char list of separators (type string)
1997 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001998static int sample_conv_field(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brunf399b0d2014-11-03 17:07:03 +01001999{
Marcin Deranek9631a282018-04-16 14:30:46 +02002000 int field;
Emeric Brunf399b0d2014-11-03 17:07:03 +01002001 char *start, *end;
2002 int i;
Marcin Deranek9631a282018-04-16 14:30:46 +02002003 int count = (arg_p[2].type == ARGT_SINT) ? arg_p[2].data.sint : 1;
Emeric Brunf399b0d2014-11-03 17:07:03 +01002004
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002005 if (!arg_p[0].data.sint)
Emeric Brunf399b0d2014-11-03 17:07:03 +01002006 return 0;
2007
Marcin Deranek9631a282018-04-16 14:30:46 +02002008 if (arg_p[0].data.sint < 0) {
2009 field = -1;
2010 end = start = smp->data.u.str.str + smp->data.u.str.len;
2011 while (start > smp->data.u.str.str) {
2012 for (i = 0 ; i < arg_p[1].data.str.len ; i++) {
2013 if (*(start-1) == arg_p[1].data.str.str[i]) {
2014 if (field == arg_p[0].data.sint) {
2015 if (count == 1)
2016 goto found;
2017 else if (count > 1)
2018 count--;
2019 } else {
2020 end = start-1;
2021 field--;
2022 }
2023 break;
2024 }
Emeric Brunf399b0d2014-11-03 17:07:03 +01002025 }
Marcin Deranek9631a282018-04-16 14:30:46 +02002026 start--;
Emeric Brunf399b0d2014-11-03 17:07:03 +01002027 }
Marcin Deranek9631a282018-04-16 14:30:46 +02002028 } else {
2029 field = 1;
2030 end = start = smp->data.u.str.str;
2031 while (end - smp->data.u.str.str < smp->data.u.str.len) {
2032 for (i = 0 ; i < arg_p[1].data.str.len ; i++) {
2033 if (*end == arg_p[1].data.str.str[i]) {
2034 if (field == arg_p[0].data.sint) {
2035 if (count == 1)
2036 goto found;
2037 else if (count > 1)
2038 count--;
2039 } else {
2040 start = end+1;
2041 field++;
2042 }
2043 break;
2044 }
2045 }
2046 end++;
2047 }
Emeric Brunf399b0d2014-11-03 17:07:03 +01002048 }
2049
2050 /* Field not found */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002051 if (field != arg_p[0].data.sint) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002052 smp->data.u.str.len = 0;
Emeric Brunf399b0d2014-11-03 17:07:03 +01002053 return 1;
2054 }
2055found:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002056 smp->data.u.str.len = end - start;
Emeric Brunf399b0d2014-11-03 17:07:03 +01002057 /* If ret string is len 0, no need to
2058 change pointers or to update size */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002059 if (!smp->data.u.str.len)
Emeric Brunf399b0d2014-11-03 17:07:03 +01002060 return 1;
2061
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002062 smp->data.u.str.str = start;
Emeric Brunf399b0d2014-11-03 17:07:03 +01002063
2064 /* Compute remaining size if needed
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002065 Note: smp->data.u.str.size cannot be set to 0 */
2066 if (smp->data.u.str.size)
2067 smp->data.u.str.size -= start - smp->data.u.str.str;
Emeric Brunf399b0d2014-11-03 17:07:03 +01002068
2069 return 1;
2070}
2071
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002072/* This sample function is designed to return a word from a string.
2073 * First arg is the index of the word (start at 1)
2074 * Second arg is a char list of words separators (type string)
2075 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002076static int sample_conv_word(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002077{
Marcin Deranek9631a282018-04-16 14:30:46 +02002078 int word;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002079 char *start, *end;
2080 int i, issep, inword;
Marcin Deranek9631a282018-04-16 14:30:46 +02002081 int count = (arg_p[2].type == ARGT_SINT) ? arg_p[2].data.sint : 1;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002082
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002083 if (!arg_p[0].data.sint)
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002084 return 0;
2085
2086 word = 0;
2087 inword = 0;
Marcin Deranek9631a282018-04-16 14:30:46 +02002088 if (arg_p[0].data.sint < 0) {
2089 end = start = smp->data.u.str.str + smp->data.u.str.len;
2090 while (start > smp->data.u.str.str) {
2091 issep = 0;
2092 for (i = 0 ; i < arg_p[1].data.str.len ; i++) {
2093 if (*(start-1) == arg_p[1].data.str.str[i]) {
2094 issep = 1;
2095 break;
2096 }
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002097 }
Marcin Deranek9631a282018-04-16 14:30:46 +02002098 if (!inword) {
2099 if (!issep) {
2100 if (word != arg_p[0].data.sint) {
2101 word--;
2102 end = start;
2103 }
2104 inword = 1;
2105 }
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002106 }
Marcin Deranek9631a282018-04-16 14:30:46 +02002107 else if (issep) {
Willy Tarreau9eb2a4a2018-04-19 10:33:28 +02002108 if (word == arg_p[0].data.sint) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002109 if (count == 1)
2110 goto found;
2111 else if (count > 1)
2112 count--;
Willy Tarreau9eb2a4a2018-04-19 10:33:28 +02002113 }
Marcin Deranek9631a282018-04-16 14:30:46 +02002114 inword = 0;
2115 }
2116 start--;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002117 }
Marcin Deranek9631a282018-04-16 14:30:46 +02002118 } else {
2119 end = start = smp->data.u.str.str;
2120 while (end - smp->data.u.str.str < smp->data.u.str.len) {
2121 issep = 0;
2122 for (i = 0 ; i < arg_p[1].data.str.len ; i++) {
2123 if (*end == arg_p[1].data.str.str[i]) {
2124 issep = 1;
2125 break;
2126 }
2127 }
2128 if (!inword) {
2129 if (!issep) {
2130 if (word != arg_p[0].data.sint) {
2131 word++;
2132 start = end;
2133 }
2134 inword = 1;
2135 }
2136 }
2137 else if (issep) {
Willy Tarreau9eb2a4a2018-04-19 10:33:28 +02002138 if (word == arg_p[0].data.sint) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002139 if (count == 1)
2140 goto found;
2141 else if (count > 1)
2142 count--;
Willy Tarreau9eb2a4a2018-04-19 10:33:28 +02002143 }
Marcin Deranek9631a282018-04-16 14:30:46 +02002144 inword = 0;
2145 }
2146 end++;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002147 }
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002148 }
2149
2150 /* Field not found */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002151 if (word != arg_p[0].data.sint) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002152 smp->data.u.str.len = 0;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002153 return 1;
2154 }
2155found:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002156 smp->data.u.str.len = end - start;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002157 /* If ret string is len 0, no need to
2158 change pointers or to update size */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002159 if (!smp->data.u.str.len)
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002160 return 1;
2161
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002162 smp->data.u.str.str = start;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002163
2164 /* Compute remaining size if needed
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002165 Note: smp->data.u.str.size cannot be set to 0 */
2166 if (smp->data.u.str.size)
2167 smp->data.u.str.size -= start - smp->data.u.str.str;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002168
2169 return 1;
2170}
2171
Willy Tarreau7eda8492015-01-20 19:47:06 +01002172static int sample_conv_regsub_check(struct arg *args, struct sample_conv *conv,
2173 const char *file, int line, char **err)
2174{
2175 struct arg *arg = args;
2176 char *p;
2177 int len;
2178
2179 /* arg0 is a regex, it uses type_flag for ICASE and global match */
2180 arg[0].type_flags = 0;
2181
2182 if (arg[2].type != ARGT_STR)
2183 return 1;
2184
2185 p = arg[2].data.str.str;
2186 len = arg[2].data.str.len;
2187 while (len) {
2188 if (*p == 'i') {
2189 arg[0].type_flags |= ARGF_REG_ICASE;
2190 }
2191 else if (*p == 'g') {
2192 arg[0].type_flags |= ARGF_REG_GLOB;
2193 }
2194 else {
2195 memprintf(err, "invalid regex flag '%c', only 'i' and 'g' are supported", *p);
2196 return 0;
2197 }
2198 p++;
2199 len--;
2200 }
2201 return 1;
2202}
2203
2204/* This sample function is designed to do the equivalent of s/match/replace/ on
2205 * the input string. It applies a regex and restarts from the last matched
2206 * location until nothing matches anymore. First arg is the regex to apply to
2207 * the input string, second arg is the replacement expression.
2208 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002209static int sample_conv_regsub(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau7eda8492015-01-20 19:47:06 +01002210{
2211 char *start, *end;
2212 struct my_regex *reg = arg_p[0].data.reg;
2213 regmatch_t pmatch[MAX_MATCH];
2214 struct chunk *trash = get_trash_chunk();
2215 int flag, max;
2216 int found;
2217
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002218 start = smp->data.u.str.str;
2219 end = start + smp->data.u.str.len;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002220
2221 flag = 0;
2222 while (1) {
2223 /* check for last round which is used to copy remaining parts
2224 * when not running in global replacement mode.
2225 */
2226 found = 0;
2227 if ((arg_p[0].type_flags & ARGF_REG_GLOB) || !(flag & REG_NOTBOL)) {
2228 /* Note: we can have start == end on empty strings or at the end */
2229 found = regex_exec_match2(reg, start, end - start, MAX_MATCH, pmatch, flag);
2230 }
2231
2232 if (!found)
2233 pmatch[0].rm_so = end - start;
2234
2235 /* copy the heading non-matching part (which may also be the tail if nothing matches) */
2236 max = trash->size - trash->len;
2237 if (max && pmatch[0].rm_so > 0) {
2238 if (max > pmatch[0].rm_so)
2239 max = pmatch[0].rm_so;
2240 memcpy(trash->str + trash->len, start, max);
2241 trash->len += max;
2242 }
2243
2244 if (!found)
2245 break;
2246
2247 /* replace the matching part */
2248 max = trash->size - trash->len;
2249 if (max) {
2250 if (max > arg_p[1].data.str.len)
2251 max = arg_p[1].data.str.len;
2252 memcpy(trash->str + trash->len, arg_p[1].data.str.str, max);
2253 trash->len += max;
2254 }
2255
2256 /* stop here if we're done with this string */
2257 if (start >= end)
2258 break;
2259
2260 /* We have a special case for matches of length 0 (eg: "x*y*").
2261 * These ones are considered to match in front of a character,
2262 * so we have to copy that character and skip to the next one.
2263 */
2264 if (!pmatch[0].rm_eo) {
2265 if (trash->len < trash->size)
2266 trash->str[trash->len++] = start[pmatch[0].rm_eo];
2267 pmatch[0].rm_eo++;
2268 }
2269
2270 start += pmatch[0].rm_eo;
2271 flag |= REG_NOTBOL;
2272 }
2273
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002274 smp->data.u.str = *trash;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002275 return 1;
2276}
2277
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002278/* This function check an operator entry. It expects a string.
2279 * The string can be an integer or a variable name.
2280 */
2281static int check_operator(struct arg *args, struct sample_conv *conv,
2282 const char *file, int line, char **err)
2283{
2284 const char *str;
2285 const char *end;
2286
2287 /* Try to decode a variable. */
2288 if (vars_check_arg(&args[0], NULL))
2289 return 1;
2290
2291 /* Try to convert an integer */
2292 str = args[0].data.str.str;
2293 end = str + strlen(str);
2294 args[0].data.sint = read_int64(&str, end);
2295 if (*str != '\0') {
2296 memprintf(err, "expects an integer or a variable name");
2297 return 0;
2298 }
2299 args[0].type = ARGT_SINT;
2300 return 1;
2301}
2302
Willy Tarreau6204cd92016-03-10 16:33:04 +01002303/* This function returns a sample struct filled with an arg content.
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002304 * If the arg contain an integer, the integer is returned in the
2305 * sample. If the arg contains a variable descriptor, it returns the
2306 * variable value.
2307 *
2308 * This function returns 0 if an error occurs, otherwise it returns 1.
2309 */
Willy Tarreau6204cd92016-03-10 16:33:04 +01002310static inline int sample_conv_var2smp(const struct arg *arg, struct sample *smp)
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002311{
2312 switch (arg->type) {
2313 case ARGT_SINT:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002314 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002315 smp->data.u.sint = arg->data.sint;
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002316 return 1;
2317 case ARGT_VAR:
Willy Tarreau6204cd92016-03-10 16:33:04 +01002318 if (!vars_get_by_desc(&arg->data.var, smp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002319 return 0;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002320 if (!sample_casts[smp->data.type][SMP_T_SINT])
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002321 return 0;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002322 if (!sample_casts[smp->data.type][SMP_T_SINT](smp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002323 return 0;
2324 return 1;
2325 default:
2326 return 0;
2327 }
2328}
2329
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002330/* Takes a SINT on input, applies a binary twos complement and returns the SINT
Willy Tarreau97707872015-01-27 15:12:13 +01002331 * result.
2332 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002333static int sample_conv_binary_cpl(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002334{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002335 smp->data.u.sint = ~smp->data.u.sint;
Willy Tarreau97707872015-01-27 15:12:13 +01002336 return 1;
2337}
2338
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002339/* Takes a SINT on input, applies a binary "and" with the SINT directly in
2340 * arg_p or in the varaible described in arg_p, and returns the SINT result.
Willy Tarreau97707872015-01-27 15:12:13 +01002341 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002342static int sample_conv_binary_and(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002343{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002344 struct sample tmp;
2345
Willy Tarreau7560dd42016-03-10 16:28:58 +01002346 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreau6204cd92016-03-10 16:33:04 +01002347 if (!sample_conv_var2smp(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002348 return 0;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002349 smp->data.u.sint &= tmp.data.u.sint;
Willy Tarreau97707872015-01-27 15:12:13 +01002350 return 1;
2351}
2352
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002353/* Takes a SINT on input, applies a binary "or" with the SINT directly in
2354 * arg_p or in the varaible described in arg_p, and returns the SINT result.
Willy Tarreau97707872015-01-27 15:12:13 +01002355 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002356static int sample_conv_binary_or(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002357{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002358 struct sample tmp;
2359
Willy Tarreau7560dd42016-03-10 16:28:58 +01002360 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreau6204cd92016-03-10 16:33:04 +01002361 if (!sample_conv_var2smp(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002362 return 0;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002363 smp->data.u.sint |= tmp.data.u.sint;
Willy Tarreau97707872015-01-27 15:12:13 +01002364 return 1;
2365}
2366
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002367/* Takes a SINT on input, applies a binary "xor" with the SINT directly in
2368 * arg_p or in the varaible described in arg_p, and returns the SINT result.
Willy Tarreau97707872015-01-27 15:12:13 +01002369 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002370static int sample_conv_binary_xor(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002371{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002372 struct sample tmp;
2373
Willy Tarreau7560dd42016-03-10 16:28:58 +01002374 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreau6204cd92016-03-10 16:33:04 +01002375 if (!sample_conv_var2smp(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002376 return 0;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002377 smp->data.u.sint ^= tmp.data.u.sint;
Willy Tarreau97707872015-01-27 15:12:13 +01002378 return 1;
2379}
2380
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002381static inline long long int arith_add(long long int a, long long int b)
2382{
2383 /* Prevent overflow and makes capped calculus.
2384 * We must ensure that the check calculus doesn't
2385 * exceed the signed 64 bits limits.
2386 *
2387 * +----------+----------+
2388 * | a<0 | a>=0 |
2389 * +------+----------+----------+
2390 * | b<0 | MIN-a>b | no check |
2391 * +------+----------+----------+
2392 * | b>=0 | no check | MAX-a<b |
2393 * +------+----------+----------+
2394 */
2395 if ((a ^ b) >= 0) {
2396 /* signs are differents. */
2397 if (a < 0) {
2398 if (LLONG_MIN - a > b)
2399 return LLONG_MIN;
2400 }
2401 if (LLONG_MAX - a < b)
2402 return LLONG_MAX;
2403 }
2404 return a + b;
2405}
2406
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002407/* Takes a SINT on input, applies an arithmetic "add" with the SINT directly in
2408 * arg_p or in the varaible described in arg_p, and returns the SINT result.
Willy Tarreau97707872015-01-27 15:12:13 +01002409 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002410static int sample_conv_arith_add(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002411{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002412 struct sample tmp;
2413
Willy Tarreau7560dd42016-03-10 16:28:58 +01002414 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreau6204cd92016-03-10 16:33:04 +01002415 if (!sample_conv_var2smp(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002416 return 0;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002417 smp->data.u.sint = arith_add(smp->data.u.sint, tmp.data.u.sint);
Willy Tarreau97707872015-01-27 15:12:13 +01002418 return 1;
2419}
2420
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002421/* Takes a SINT on input, applies an arithmetic "sub" with the SINT directly in
2422 * arg_p or in the varaible described in arg_p, and returns the SINT result.
Willy Tarreau97707872015-01-27 15:12:13 +01002423 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002424static int sample_conv_arith_sub(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002425 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002426{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002427 struct sample tmp;
2428
Willy Tarreau7560dd42016-03-10 16:28:58 +01002429 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreau6204cd92016-03-10 16:33:04 +01002430 if (!sample_conv_var2smp(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002431 return 0;
2432
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002433 /* We cannot represent -LLONG_MIN because abs(LLONG_MIN) is greater
2434 * than abs(LLONG_MAX). So, the following code use LLONG_MAX in place
2435 * of -LLONG_MIN and correct the result.
2436 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002437 if (tmp.data.u.sint == LLONG_MIN) {
2438 smp->data.u.sint = arith_add(smp->data.u.sint, LLONG_MAX);
2439 if (smp->data.u.sint < LLONG_MAX)
2440 smp->data.u.sint++;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002441 return 1;
2442 }
2443
2444 /* standard substraction: we use the "add" function and negate
2445 * the second operand.
2446 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002447 smp->data.u.sint = arith_add(smp->data.u.sint, -tmp.data.u.sint);
Willy Tarreau97707872015-01-27 15:12:13 +01002448 return 1;
2449}
2450
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002451/* Takes a SINT on input, applies an arithmetic "mul" with the SINT directly in
2452 * arg_p or in the varaible described in arg_p, and returns the SINT result.
2453 * If the result makes an overflow, then the largest possible quantity is
2454 * returned.
Willy Tarreau97707872015-01-27 15:12:13 +01002455 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002456static int sample_conv_arith_mul(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002457 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002458{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002459 struct sample tmp;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002460 long long int c;
2461
Willy Tarreau7560dd42016-03-10 16:28:58 +01002462 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreau6204cd92016-03-10 16:33:04 +01002463 if (!sample_conv_var2smp(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002464 return 0;
2465
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002466 /* prevent divide by 0 during the check */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002467 if (!smp->data.u.sint || !tmp.data.u.sint) {
2468 smp->data.u.sint = 0;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002469 return 1;
2470 }
2471
2472 /* The multiply between LLONG_MIN and -1 returns a
2473 * "floting point exception".
2474 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002475 if (smp->data.u.sint == LLONG_MIN && tmp.data.u.sint == -1) {
2476 smp->data.u.sint = LLONG_MAX;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002477 return 1;
2478 }
2479
2480 /* execute standard multiplication. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002481 c = smp->data.u.sint * tmp.data.u.sint;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002482
2483 /* check for overflow and makes capped multiply. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002484 if (smp->data.u.sint != c / tmp.data.u.sint) {
2485 if ((smp->data.u.sint < 0) == (tmp.data.u.sint < 0)) {
2486 smp->data.u.sint = LLONG_MAX;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002487 return 1;
2488 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002489 smp->data.u.sint = LLONG_MIN;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002490 return 1;
2491 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002492 smp->data.u.sint = c;
Willy Tarreau97707872015-01-27 15:12:13 +01002493 return 1;
2494}
2495
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002496/* Takes a SINT on input, applies an arithmetic "div" with the SINT directly in
2497 * arg_p or in the varaible described in arg_p, and returns the SINT result.
2498 * If arg_p makes the result overflow, then the largest possible quantity is
2499 * returned.
Willy Tarreau97707872015-01-27 15:12:13 +01002500 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002501static int sample_conv_arith_div(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002502 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002503{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002504 struct sample tmp;
2505
Willy Tarreau7560dd42016-03-10 16:28:58 +01002506 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreau6204cd92016-03-10 16:33:04 +01002507 if (!sample_conv_var2smp(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002508 return 0;
2509
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002510 if (tmp.data.u.sint) {
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002511 /* The divide between LLONG_MIN and -1 returns a
2512 * "floting point exception".
2513 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002514 if (smp->data.u.sint == LLONG_MIN && tmp.data.u.sint == -1) {
2515 smp->data.u.sint = LLONG_MAX;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002516 return 1;
2517 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002518 smp->data.u.sint /= tmp.data.u.sint;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002519 return 1;
2520 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002521 smp->data.u.sint = LLONG_MAX;
Willy Tarreau97707872015-01-27 15:12:13 +01002522 return 1;
2523}
2524
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002525/* Takes a SINT on input, applies an arithmetic "mod" with the SINT directly in
2526 * arg_p or in the varaible described in arg_p, and returns the SINT result.
2527 * If arg_p makes the result overflow, then 0 is returned.
Willy Tarreau97707872015-01-27 15:12:13 +01002528 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002529static int sample_conv_arith_mod(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002530 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002531{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002532 struct sample tmp;
2533
Willy Tarreau7560dd42016-03-10 16:28:58 +01002534 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreau6204cd92016-03-10 16:33:04 +01002535 if (!sample_conv_var2smp(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002536 return 0;
2537
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002538 if (tmp.data.u.sint) {
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002539 /* The divide between LLONG_MIN and -1 returns a
2540 * "floting point exception".
2541 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002542 if (smp->data.u.sint == LLONG_MIN && tmp.data.u.sint == -1) {
2543 smp->data.u.sint = 0;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002544 return 1;
2545 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002546 smp->data.u.sint %= tmp.data.u.sint;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002547 return 1;
2548 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002549 smp->data.u.sint = 0;
Willy Tarreau97707872015-01-27 15:12:13 +01002550 return 1;
2551}
2552
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002553/* Takes an SINT on input, applies an arithmetic "neg" and returns the SINT
Willy Tarreau97707872015-01-27 15:12:13 +01002554 * result.
2555 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002556static int sample_conv_arith_neg(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002557 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002558{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002559 if (smp->data.u.sint == LLONG_MIN)
2560 smp->data.u.sint = LLONG_MAX;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002561 else
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002562 smp->data.u.sint = -smp->data.u.sint;
Willy Tarreau97707872015-01-27 15:12:13 +01002563 return 1;
2564}
2565
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002566/* Takes a SINT on input, returns true is the value is non-null, otherwise
Willy Tarreau97707872015-01-27 15:12:13 +01002567 * false. The output is a BOOL.
2568 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002569static int sample_conv_arith_bool(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002570 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002571{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002572 smp->data.u.sint = !!smp->data.u.sint;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002573 smp->data.type = SMP_T_BOOL;
Willy Tarreau97707872015-01-27 15:12:13 +01002574 return 1;
2575}
2576
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002577/* Takes a SINT on input, returns false is the value is non-null, otherwise
Willy Tarreau97707872015-01-27 15:12:13 +01002578 * truee. The output is a BOOL.
2579 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002580static int sample_conv_arith_not(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002581 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002582{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002583 smp->data.u.sint = !smp->data.u.sint;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002584 smp->data.type = SMP_T_BOOL;
Willy Tarreau97707872015-01-27 15:12:13 +01002585 return 1;
2586}
2587
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002588/* Takes a SINT on input, returns true is the value is odd, otherwise false.
Willy Tarreau97707872015-01-27 15:12:13 +01002589 * The output is a BOOL.
2590 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002591static int sample_conv_arith_odd(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002592 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002593{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002594 smp->data.u.sint = smp->data.u.sint & 1;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002595 smp->data.type = SMP_T_BOOL;
Willy Tarreau97707872015-01-27 15:12:13 +01002596 return 1;
2597}
2598
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002599/* Takes a SINT on input, returns true is the value is even, otherwise false.
Willy Tarreau97707872015-01-27 15:12:13 +01002600 * The output is a BOOL.
2601 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002602static int sample_conv_arith_even(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002603 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002604{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002605 smp->data.u.sint = !(smp->data.u.sint & 1);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002606 smp->data.type = SMP_T_BOOL;
Willy Tarreau97707872015-01-27 15:12:13 +01002607 return 1;
2608}
2609
Willy Tarreau280f42b2018-02-19 15:34:12 +01002610/* appends an optional const string, an optional variable contents and another
2611 * optional const string to an existing string.
2612 */
2613static int sample_conv_concat(const struct arg *arg_p, struct sample *smp, void *private)
2614{
2615 struct chunk *trash;
2616 struct sample tmp;
2617 int max;
2618
2619 trash = get_trash_chunk();
2620 trash->len = smp->data.u.str.len;
2621 if (trash->len > trash->size - 1)
2622 trash->len = trash->size - 1;
2623
2624 memcpy(trash->str, smp->data.u.str.str, trash->len);
2625 trash->str[trash->len] = 0;
2626
2627 /* append first string */
2628 max = arg_p[0].data.str.len;
2629 if (max > trash->size - 1 - trash->len)
2630 max = trash->size - 1 - trash->len;
2631
2632 if (max) {
2633 memcpy(trash->str + trash->len, arg_p[0].data.str.str, max);
2634 trash->len += max;
2635 trash->str[trash->len] = 0;
2636 }
2637
2638 /* append second string (variable) if it's found and we can turn it
2639 * into a string.
2640 */
2641 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
2642 if (arg_p[1].type == ARGT_VAR && vars_get_by_desc(&arg_p[1].data.var, &tmp) &&
2643 (sample_casts[tmp.data.type][SMP_T_STR] == c_none ||
2644 sample_casts[tmp.data.type][SMP_T_STR](&tmp))) {
2645
2646 max = tmp.data.u.str.len;
2647 if (max > trash->size - 1 - trash->len)
2648 max = trash->size - 1 - trash->len;
2649
2650 if (max) {
2651 memcpy(trash->str + trash->len, tmp.data.u.str.str, max);
2652 trash->len += max;
2653 trash->str[trash->len] = 0;
2654 }
2655 }
2656
2657 /* append third string */
2658 max = arg_p[2].data.str.len;
2659 if (max > trash->size - 1 - trash->len)
2660 max = trash->size - 1 - trash->len;
2661
2662 if (max) {
2663 memcpy(trash->str + trash->len, arg_p[2].data.str.str, max);
2664 trash->len += max;
2665 trash->str[trash->len] = 0;
2666 }
2667
2668 smp->data.u.str = *trash;
2669 smp->data.type = SMP_T_STR;
2670 return 1;
2671}
2672
2673/* This function checks the "concat" converter's arguments and extracts the
2674 * variable name and its scope.
2675 */
2676static int smp_check_concat(struct arg *args, struct sample_conv *conv,
2677 const char *file, int line, char **err)
2678{
2679 /* Try to decode a variable. */
2680 if (args[1].data.str.len > 0 && !vars_check_arg(&args[1], NULL)) {
2681 memprintf(err, "failed to register variable name '%s'", args[1].data.str.str);
2682 return 0;
2683 }
2684 return 1;
2685}
2686
Willy Tarreau5b8ad222013-07-25 12:17:57 +02002687/************************************************************************/
2688/* All supported sample fetch functions must be declared here */
2689/************************************************************************/
2690
2691/* force TRUE to be returned at the fetch level */
2692static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002693smp_fetch_true(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau5b8ad222013-07-25 12:17:57 +02002694{
Willy Tarreau280f42b2018-02-19 15:34:12 +01002695 if (!smp_make_rw(smp))
2696 return 0;
2697
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002698 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002699 smp->data.u.sint = 1;
Willy Tarreau5b8ad222013-07-25 12:17:57 +02002700 return 1;
2701}
2702
2703/* force FALSE to be returned at the fetch level */
2704static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002705smp_fetch_false(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau5b8ad222013-07-25 12:17:57 +02002706{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002707 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002708 smp->data.u.sint = 0;
Willy Tarreau5b8ad222013-07-25 12:17:57 +02002709 return 1;
2710}
2711
2712/* retrieve environment variable $1 as a string */
2713static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002714smp_fetch_env(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau5b8ad222013-07-25 12:17:57 +02002715{
2716 char *env;
2717
2718 if (!args || args[0].type != ARGT_STR)
2719 return 0;
2720
2721 env = getenv(args[0].data.str.str);
2722 if (!env)
2723 return 0;
2724
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002725 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01002726 smp->flags = SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002727 smp->data.u.str.str = env;
2728 smp->data.u.str.len = strlen(env);
Willy Tarreau5b8ad222013-07-25 12:17:57 +02002729 return 1;
2730}
2731
Willy Tarreau6236d3a2013-07-25 14:28:25 +02002732/* retrieve the current local date in epoch time, and applies an optional offset
2733 * of args[0] seconds.
2734 */
2735static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002736smp_fetch_date(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau6236d3a2013-07-25 14:28:25 +02002737{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002738 smp->data.u.sint = date.tv_sec;
Willy Tarreau6236d3a2013-07-25 14:28:25 +02002739
2740 /* add offset */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002741 if (args && args[0].type == ARGT_SINT)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002742 smp->data.u.sint += args[0].data.sint;
Willy Tarreau6236d3a2013-07-25 14:28:25 +02002743
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002744 smp->data.type = SMP_T_SINT;
Willy Tarreau6236d3a2013-07-25 14:28:25 +02002745 smp->flags |= SMP_F_VOL_TEST | SMP_F_MAY_CHANGE;
2746 return 1;
2747}
2748
Etienne Carrierea792a0a2018-01-17 13:43:24 +01002749/* retrieve the current microsecond part of the date */
2750static int
2751smp_fetch_date_us(const struct arg *args, struct sample *smp, const char *kw, void *private)
2752{
2753 smp->data.u.sint = date.tv_usec;
2754 smp->data.type = SMP_T_SINT;
2755 smp->flags |= SMP_F_VOL_TEST | SMP_F_MAY_CHANGE;
2756 return 1;
2757}
2758
2759
Nenad Merdanovic807a6e72017-03-12 22:00:00 +01002760/* returns the hostname */
2761static int
2762smp_fetch_hostname(const struct arg *args, struct sample *smp, const char *kw, void *private)
2763{
2764 smp->data.type = SMP_T_STR;
2765 smp->flags = SMP_F_CONST;
2766 smp->data.u.str.str = hostname;
2767 smp->data.u.str.len = strlen(hostname);
2768 return 1;
2769}
2770
Willy Tarreau0f30d262014-11-24 16:02:05 +01002771/* returns the number of processes */
2772static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002773smp_fetch_nbproc(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau0f30d262014-11-24 16:02:05 +01002774{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002775 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002776 smp->data.u.sint = global.nbproc;
Willy Tarreau0f30d262014-11-24 16:02:05 +01002777 return 1;
2778}
2779
2780/* returns the number of the current process (between 1 and nbproc */
2781static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002782smp_fetch_proc(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau0f30d262014-11-24 16:02:05 +01002783{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002784 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002785 smp->data.u.sint = relative_pid;
Willy Tarreau0f30d262014-11-24 16:02:05 +01002786 return 1;
2787}
2788
Christopher Faulet34adb2a2017-11-21 21:45:38 +01002789/* returns the number of the current thread (between 1 and nbthread */
2790static int
2791smp_fetch_thread(const struct arg *args, struct sample *smp, const char *kw, void *private)
2792{
2793 smp->data.type = SMP_T_SINT;
2794 smp->data.u.sint = tid;
2795 return 1;
2796}
2797
Willy Tarreau84310e22014-02-14 11:59:04 +01002798/* generate a random 32-bit integer for whatever purpose, with an optional
2799 * range specified in argument.
2800 */
2801static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002802smp_fetch_rand(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau84310e22014-02-14 11:59:04 +01002803{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002804 smp->data.u.sint = random();
Willy Tarreau84310e22014-02-14 11:59:04 +01002805
2806 /* reduce if needed. Don't do a modulo, use all bits! */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002807 if (args && args[0].type == ARGT_SINT)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002808 smp->data.u.sint = (smp->data.u.sint * args[0].data.sint) / ((u64)RAND_MAX+1);
Willy Tarreau84310e22014-02-14 11:59:04 +01002809
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002810 smp->data.type = SMP_T_SINT;
Willy Tarreau84310e22014-02-14 11:59:04 +01002811 smp->flags |= SMP_F_VOL_TEST | SMP_F_MAY_CHANGE;
2812 return 1;
2813}
2814
Willy Tarreau0f30d262014-11-24 16:02:05 +01002815/* returns true if the current process is stopping */
2816static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002817smp_fetch_stopping(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau0f30d262014-11-24 16:02:05 +01002818{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002819 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002820 smp->data.u.sint = stopping;
Willy Tarreau0f30d262014-11-24 16:02:05 +01002821 return 1;
2822}
2823
Thierry FOURNIERcc103292015-06-06 19:30:17 +02002824static int smp_fetch_const_str(const struct arg *args, struct sample *smp, const char *kw, void *private)
2825{
2826 smp->flags |= SMP_F_CONST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002827 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002828 smp->data.u.str.str = args[0].data.str.str;
2829 smp->data.u.str.len = args[0].data.str.len;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02002830 return 1;
2831}
2832
2833static int smp_check_const_bool(struct arg *args, char **err)
2834{
2835 if (strcasecmp(args[0].data.str.str, "true") == 0 ||
2836 strcasecmp(args[0].data.str.str, "1") == 0) {
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002837 args[0].type = ARGT_SINT;
2838 args[0].data.sint = 1;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02002839 return 1;
2840 }
2841 if (strcasecmp(args[0].data.str.str, "false") == 0 ||
2842 strcasecmp(args[0].data.str.str, "0") == 0) {
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002843 args[0].type = ARGT_SINT;
2844 args[0].data.sint = 0;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02002845 return 1;
2846 }
2847 memprintf(err, "Expects 'true', 'false', '0' or '1'");
2848 return 0;
2849}
2850
2851static int smp_fetch_const_bool(const struct arg *args, struct sample *smp, const char *kw, void *private)
2852{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002853 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002854 smp->data.u.sint = args[0].data.sint;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02002855 return 1;
2856}
2857
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002858static int smp_fetch_const_int(const struct arg *args, struct sample *smp, const char *kw, void *private)
Thierry FOURNIERcc103292015-06-06 19:30:17 +02002859{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002860 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002861 smp->data.u.sint = args[0].data.sint;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02002862 return 1;
2863}
2864
2865static int smp_fetch_const_ipv4(const struct arg *args, struct sample *smp, const char *kw, void *private)
2866{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002867 smp->data.type = SMP_T_IPV4;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002868 smp->data.u.ipv4 = args[0].data.ipv4;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02002869 return 1;
2870}
2871
2872static int smp_fetch_const_ipv6(const struct arg *args, struct sample *smp, const char *kw, void *private)
2873{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002874 smp->data.type = SMP_T_IPV6;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002875 smp->data.u.ipv6 = args[0].data.ipv6;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02002876 return 1;
2877}
2878
2879static int smp_check_const_bin(struct arg *args, char **err)
2880{
David Carlier64a16ab2016-04-08 10:37:02 +01002881 char *binstr = NULL;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02002882 int binstrlen;
2883
2884 if (!parse_binary(args[0].data.str.str, &binstr, &binstrlen, err))
2885 return 0;
2886 args[0].type = ARGT_STR;
2887 args[0].data.str.str = binstr;
2888 args[0].data.str.len = binstrlen;
2889 return 1;
2890}
2891
2892static int smp_fetch_const_bin(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_BIN;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002896 smp->data.u.str.str = args[0].data.str.str;
2897 smp->data.u.str.len = args[0].data.str.len;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02002898 return 1;
2899}
2900
2901static int smp_check_const_meth(struct arg *args, char **err)
2902{
2903 enum http_meth_t meth;
2904 int i;
2905
2906 meth = find_http_meth(args[0].data.str.str, args[0].data.str.len);
2907 if (meth != HTTP_METH_OTHER) {
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002908 args[0].type = ARGT_SINT;
2909 args[0].data.sint = meth;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02002910 } else {
2911 /* Check method avalaibility. A methos is a token defined as :
2912 * tchar = "!" / "#" / "$" / "%" / "&" / "'" / "*" / "+" / "-" / "." /
2913 * "^" / "_" / "`" / "|" / "~" / DIGIT / ALPHA
2914 * token = 1*tchar
2915 */
2916 for (i = 0; i < args[0].data.str.len; i++) {
Willy Tarreau2235b262016-11-05 15:50:20 +01002917 if (!HTTP_IS_TOKEN(args[0].data.str.str[i])) {
Thierry FOURNIERcc103292015-06-06 19:30:17 +02002918 memprintf(err, "expects valid method.");
2919 return 0;
2920 }
2921 }
2922 }
2923 return 1;
2924}
2925
2926static int smp_fetch_const_meth(const struct arg *args, struct sample *smp, const char *kw, void *private)
2927{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002928 smp->data.type = SMP_T_METH;
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002929 if (args[0].type == ARGT_SINT) {
Thierry FOURNIERcc103292015-06-06 19:30:17 +02002930 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002931 smp->data.u.meth.meth = args[0].data.sint;
2932 smp->data.u.meth.str.str = "";
2933 smp->data.u.meth.str.len = 0;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02002934 } else {
2935 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002936 smp->data.u.meth.meth = HTTP_METH_OTHER;
2937 smp->data.u.meth.str.str = args[0].data.str.str;
2938 smp->data.u.meth.str.len = args[0].data.str.len;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02002939 }
2940 return 1;
2941}
2942
Willy Tarreau5b8ad222013-07-25 12:17:57 +02002943/* Note: must not be declared <const> as its list will be overwritten.
2944 * Note: fetches that may return multiple types must be declared as the lowest
2945 * common denominator, the type that can be casted into all other ones. For
2946 * instance IPv4/IPv6 must be declared IPv4.
2947 */
2948static struct sample_fetch_kw_list smp_kws = {ILH, {
2949 { "always_false", smp_fetch_false, 0, NULL, SMP_T_BOOL, SMP_USE_INTRN },
2950 { "always_true", smp_fetch_true, 0, NULL, SMP_T_BOOL, SMP_USE_INTRN },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01002951 { "env", smp_fetch_env, ARG1(1,STR), NULL, SMP_T_STR, SMP_USE_INTRN },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002952 { "date", smp_fetch_date, ARG1(0,SINT), NULL, SMP_T_SINT, SMP_USE_INTRN },
Etienne Carrierea792a0a2018-01-17 13:43:24 +01002953 { "date_us", smp_fetch_date_us, 0, NULL, SMP_T_SINT, SMP_USE_INTRN },
Nenad Merdanovic807a6e72017-03-12 22:00:00 +01002954 { "hostname", smp_fetch_hostname, 0, NULL, SMP_T_STR, SMP_USE_INTRN },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002955 { "nbproc", smp_fetch_nbproc,0, NULL, SMP_T_SINT, SMP_USE_INTRN },
2956 { "proc", smp_fetch_proc, 0, NULL, SMP_T_SINT, SMP_USE_INTRN },
Christopher Faulet34adb2a2017-11-21 21:45:38 +01002957 { "thread", smp_fetch_thread, 0, NULL, SMP_T_SINT, SMP_USE_INTRN },
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002958 { "rand", smp_fetch_rand, ARG1(0,SINT), NULL, SMP_T_SINT, SMP_USE_INTRN },
Willy Tarreau0f30d262014-11-24 16:02:05 +01002959 { "stopping", smp_fetch_stopping, 0, NULL, SMP_T_BOOL, SMP_USE_INTRN },
Thierry FOURNIERcc103292015-06-06 19:30:17 +02002960
2961 { "str", smp_fetch_const_str, ARG1(1,STR), NULL , SMP_T_STR, SMP_USE_INTRN },
2962 { "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 +02002963 { "int", smp_fetch_const_int, ARG1(1,SINT), NULL , SMP_T_SINT, SMP_USE_INTRN },
Thierry FOURNIERcc103292015-06-06 19:30:17 +02002964 { "ipv4", smp_fetch_const_ipv4, ARG1(1,IPV4), NULL , SMP_T_IPV4, SMP_USE_INTRN },
2965 { "ipv6", smp_fetch_const_ipv6, ARG1(1,IPV6), NULL , SMP_T_IPV6, SMP_USE_INTRN },
2966 { "bin", smp_fetch_const_bin, ARG1(1,STR), smp_check_const_bin , SMP_T_BIN, SMP_USE_INTRN },
2967 { "meth", smp_fetch_const_meth, ARG1(1,STR), smp_check_const_meth, SMP_T_METH, SMP_USE_INTRN },
2968
Willy Tarreau5b8ad222013-07-25 12:17:57 +02002969 { /* END */ },
2970}};
2971
Emeric Brun107ca302010-01-04 16:16:05 +01002972/* Note: must not be declared <const> as its list will be overwritten */
Willy Tarreaudc13c112013-06-21 23:16:39 +02002973static struct sample_conv_kw_list sample_conv_kws = {ILH, {
Thierry FOURNIER9687c772015-05-07 15:46:29 +02002974#ifdef DEBUG_EXPR
2975 { "debug", sample_conv_debug, 0, NULL, SMP_T_ANY, SMP_T_ANY },
2976#endif
2977
Holger Just1bfc24b2017-05-06 00:56:53 +02002978 { "b64dec", sample_conv_base642bin,0, NULL, SMP_T_STR, SMP_T_BIN },
Emeric Brun53d1a982014-04-30 18:21:37 +02002979 { "base64", sample_conv_bin2base64,0, NULL, SMP_T_BIN, SMP_T_STR },
Willy Tarreau12785782012-04-27 21:37:17 +02002980 { "upper", sample_conv_str2upper, 0, NULL, SMP_T_STR, SMP_T_STR },
2981 { "lower", sample_conv_str2lower, 0, NULL, SMP_T_STR, SMP_T_STR },
Willy Tarreau60a2ee72017-12-15 07:13:48 +01002982 { "length", sample_conv_length, 0, NULL, SMP_T_STR, SMP_T_SINT },
Thierry FOURNIER2f49d6d2014-03-12 15:01:52 +01002983 { "hex", sample_conv_bin2hex, 0, NULL, SMP_T_BIN, SMP_T_STR },
Dragan Dosen3f957b22017-10-24 09:27:34 +02002984 { "hex2i", sample_conv_hex2int, 0, NULL, SMP_T_STR, SMP_T_SINT },
Tim Duesterhus1478aa72018-01-25 16:24:51 +01002985 { "ipmask", sample_conv_ipmask, ARG2(1,MSK4,MSK6), NULL, SMP_T_ADDR, SMP_T_IPV4 },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002986 { "ltime", sample_conv_ltime, ARG2(1,STR,SINT), NULL, SMP_T_SINT, SMP_T_STR },
2987 { "utime", sample_conv_utime, ARG2(1,STR,SINT), NULL, SMP_T_SINT, SMP_T_STR },
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002988 { "crc32", sample_conv_crc32, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
Emmanuel Hocdet50791a72018-03-21 11:19:01 +01002989 { "crc32c", sample_conv_crc32c, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002990 { "djb2", sample_conv_djb2, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
2991 { "sdbm", sample_conv_sdbm, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
2992 { "wt6", sample_conv_wt6, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
Thierry FOURNIER01e09742016-12-26 11:46:11 +01002993 { "xxh32", sample_conv_xxh32, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
2994 { "xxh64", sample_conv_xxh64, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002995 { "json", sample_conv_json, ARG1(1,STR), sample_conv_json_check, SMP_T_STR, SMP_T_STR },
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002996 { "bytes", sample_conv_bytes, ARG2(1,SINT,SINT), NULL, SMP_T_BIN, SMP_T_BIN },
Marcin Deranek9631a282018-04-16 14:30:46 +02002997 { "field", sample_conv_field, ARG3(2,SINT,STR,SINT), sample_conv_field_check, SMP_T_STR, SMP_T_STR },
2998 { "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 +01002999 { "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 +02003000 { "sha1", sample_conv_sha1, 0, NULL, SMP_T_BIN, SMP_T_BIN },
Willy Tarreau280f42b2018-02-19 15:34:12 +01003001 { "concat", sample_conv_concat, ARG3(1,STR,STR,STR), smp_check_concat, SMP_T_STR, SMP_T_STR },
Willy Tarreau97707872015-01-27 15:12:13 +01003002
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02003003 { "and", sample_conv_binary_and, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
3004 { "or", sample_conv_binary_or, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
3005 { "xor", sample_conv_binary_xor, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
3006 { "cpl", sample_conv_binary_cpl, 0, NULL, SMP_T_SINT, SMP_T_SINT },
3007 { "bool", sample_conv_arith_bool, 0, NULL, SMP_T_SINT, SMP_T_BOOL },
3008 { "not", sample_conv_arith_not, 0, NULL, SMP_T_SINT, SMP_T_BOOL },
3009 { "odd", sample_conv_arith_odd, 0, NULL, SMP_T_SINT, SMP_T_BOOL },
3010 { "even", sample_conv_arith_even, 0, NULL, SMP_T_SINT, SMP_T_BOOL },
3011 { "add", sample_conv_arith_add, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
3012 { "sub", sample_conv_arith_sub, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
3013 { "mul", sample_conv_arith_mul, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
3014 { "div", sample_conv_arith_div, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
3015 { "mod", sample_conv_arith_mod, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
3016 { "neg", sample_conv_arith_neg, 0, NULL, SMP_T_SINT, SMP_T_SINT },
Willy Tarreau97707872015-01-27 15:12:13 +01003017
Willy Tarreau9fcb9842012-04-20 14:45:49 +02003018 { NULL, NULL, 0, 0, 0 },
Emeric Brun107ca302010-01-04 16:16:05 +01003019}};
3020
3021__attribute__((constructor))
Willy Tarreau12785782012-04-27 21:37:17 +02003022static void __sample_init(void)
Emeric Brun107ca302010-01-04 16:16:05 +01003023{
Willy Tarreau5b8ad222013-07-25 12:17:57 +02003024 /* register sample fetch and format conversion keywords */
3025 sample_register_fetches(&smp_kws);
Willy Tarreau12785782012-04-27 21:37:17 +02003026 sample_register_convs(&sample_conv_kws);
Emeric Brun107ca302010-01-04 16:16:05 +01003027}