blob: ed2aee01024a8f792b0cee2cd3a6e354afe311c9 [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
Thierry FOURNIER4d9a1d12014-12-08 14:49:19 +0100392/* This fucntion browse the list of available saple fetch. <current> is
393 * the last used sample fetch. If it is the first call, it must set to NULL.
394 * <idx> is the index of the next sampleèfetch entry. It is used as private
395 * value. It is useles to initiate it.
396 *
397 * It returns always the newt fetch_sample entry, and NULL when the end of
398 * 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 */
Willy Tarreaub8c8f1f2012-04-23 22:38:26 +0200499/* Note: these functions do *NOT* set the output type on the */
500/* sample, the caller is responsible for doing this on return. */
Emeric Brun107ca302010-01-04 16:16:05 +0100501/******************************************************************/
502
Willy Tarreau342acb42012-04-23 22:03:39 +0200503static int c_ip2int(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100504{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200505 smp->data.u.sint = ntohl(smp->data.u.ipv4.s_addr);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200506 smp->data.type = SMP_T_SINT;
Emeric Brun107ca302010-01-04 16:16:05 +0100507 return 1;
508}
509
Willy Tarreau342acb42012-04-23 22:03:39 +0200510static int c_ip2str(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100511{
Willy Tarreau47ca5452012-12-23 20:22:19 +0100512 struct chunk *trash = get_trash_chunk();
Emeric Brun107ca302010-01-04 16:16:05 +0100513
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200514 if (!inet_ntop(AF_INET, (void *)&smp->data.u.ipv4, trash->str, trash->size))
Emeric Brun107ca302010-01-04 16:16:05 +0100515 return 0;
516
517 trash->len = strlen(trash->str);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200518 smp->data.u.str = *trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200519 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100520 smp->flags &= ~SMP_F_CONST;
Emeric Brun107ca302010-01-04 16:16:05 +0100521
522 return 1;
523}
524
Willy Tarreau342acb42012-04-23 22:03:39 +0200525static int c_ip2ipv6(struct sample *smp)
David du Colombier4f92d322011-03-24 11:09:31 +0100526{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200527 v4tov6(&smp->data.u.ipv6, &smp->data.u.ipv4);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200528 smp->data.type = SMP_T_IPV6;
David du Colombier4f92d322011-03-24 11:09:31 +0100529 return 1;
530}
531
Thierry FOURNIERcc4d1712015-07-24 09:04:56 +0200532static int c_ipv62ip(struct sample *smp)
533{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200534 if (!v6tov4(&smp->data.u.ipv4, &smp->data.u.ipv6))
Thierry FOURNIERcc4d1712015-07-24 09:04:56 +0200535 return 0;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200536 smp->data.type = SMP_T_IPV6;
Thierry FOURNIERcc4d1712015-07-24 09:04:56 +0200537 return 1;
538}
539
Willy Tarreau342acb42012-04-23 22:03:39 +0200540static int c_ipv62str(struct sample *smp)
David du Colombier4f92d322011-03-24 11:09:31 +0100541{
Willy Tarreau47ca5452012-12-23 20:22:19 +0100542 struct chunk *trash = get_trash_chunk();
David du Colombier4f92d322011-03-24 11:09:31 +0100543
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200544 if (!inet_ntop(AF_INET6, (void *)&smp->data.u.ipv6, trash->str, trash->size))
David du Colombier4f92d322011-03-24 11:09:31 +0100545 return 0;
546
547 trash->len = strlen(trash->str);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200548 smp->data.u.str = *trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200549 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100550 smp->flags &= ~SMP_F_CONST;
David du Colombier4f92d322011-03-24 11:09:31 +0100551 return 1;
552}
553
554/*
Willy Tarreau342acb42012-04-23 22:03:39 +0200555static int c_ipv62ip(struct sample *smp)
David du Colombier4f92d322011-03-24 11:09:31 +0100556{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200557 return v6tov4(&smp->data.u.ipv4, &smp->data.u.ipv6);
David du Colombier4f92d322011-03-24 11:09:31 +0100558}
559*/
560
Willy Tarreau342acb42012-04-23 22:03:39 +0200561static int c_int2ip(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100562{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200563 smp->data.u.ipv4.s_addr = htonl((unsigned int)smp->data.u.sint);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200564 smp->data.type = SMP_T_IPV4;
Emeric Brun107ca302010-01-04 16:16:05 +0100565 return 1;
566}
567
Thierry FOURNIERcc4d1712015-07-24 09:04:56 +0200568static int c_int2ipv6(struct sample *smp)
569{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200570 smp->data.u.ipv4.s_addr = htonl((unsigned int)smp->data.u.sint);
571 v4tov6(&smp->data.u.ipv6, &smp->data.u.ipv4);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200572 smp->data.type = SMP_T_IPV6;
Thierry FOURNIERcc4d1712015-07-24 09:04:56 +0200573 return 1;
574}
575
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100576static int c_str2addr(struct sample *smp)
577{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200578 if (!buf2ip(smp->data.u.str.str, smp->data.u.str.len, &smp->data.u.ipv4)) {
579 if (!buf2ip6(smp->data.u.str.str, smp->data.u.str.len, &smp->data.u.ipv6))
Thierry FOURNIERfd139902013-12-11 12:38:57 +0100580 return 0;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200581 smp->data.type = SMP_T_IPV6;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100582 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIERfd139902013-12-11 12:38:57 +0100583 return 1;
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100584 }
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200585 smp->data.type = SMP_T_IPV4;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100586 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100587 return 1;
588}
589
Willy Tarreau342acb42012-04-23 22:03:39 +0200590static int c_str2ip(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100591{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200592 if (!buf2ip(smp->data.u.str.str, smp->data.u.str.len, &smp->data.u.ipv4))
Emeric Brun107ca302010-01-04 16:16:05 +0100593 return 0;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200594 smp->data.type = SMP_T_IPV4;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100595 smp->flags &= ~SMP_F_CONST;
Emeric Brun107ca302010-01-04 16:16:05 +0100596 return 1;
597}
598
Willy Tarreau342acb42012-04-23 22:03:39 +0200599static int c_str2ipv6(struct sample *smp)
David du Colombier4f92d322011-03-24 11:09:31 +0100600{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200601 if (!buf2ip6(smp->data.u.str.str, smp->data.u.str.len, &smp->data.u.ipv6))
Thierry FOURNIERfd139902013-12-11 12:38:57 +0100602 return 0;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200603 smp->data.type = SMP_T_IPV6;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100604 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIERfd139902013-12-11 12:38:57 +0100605 return 1;
David du Colombier4f92d322011-03-24 11:09:31 +0100606}
607
Emeric Brun4b9e8022014-11-03 18:17:10 +0100608/*
609 * The NULL char always enforces the end of string if it is met.
610 * Data is never changed, so we can ignore the CONST case
Thierry FOURNIERe87cac12014-03-12 15:07:59 +0100611 */
Emeric Brun8ac33d92012-10-17 13:36:06 +0200612static int c_bin2str(struct sample *smp)
613{
Emeric Brun4b9e8022014-11-03 18:17:10 +0100614 int i;
Emeric Brun8ac33d92012-10-17 13:36:06 +0200615
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200616 for (i = 0; i < smp->data.u.str.len; i++) {
617 if (!smp->data.u.str.str[i]) {
618 smp->data.u.str.len = i;
Thierry FOURNIERe87cac12014-03-12 15:07:59 +0100619 break;
Emeric Brun4b9e8022014-11-03 18:17:10 +0100620 }
Emeric Brun8ac33d92012-10-17 13:36:06 +0200621 }
Emeric Brun8ac33d92012-10-17 13:36:06 +0200622 return 1;
623}
624
Willy Tarreau342acb42012-04-23 22:03:39 +0200625static int c_int2str(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100626{
Willy Tarreau47ca5452012-12-23 20:22:19 +0100627 struct chunk *trash = get_trash_chunk();
Emeric Brun107ca302010-01-04 16:16:05 +0100628 char *pos;
629
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200630 pos = lltoa_r(smp->data.u.sint, trash->str, trash->size);
Emeric Brun107ca302010-01-04 16:16:05 +0100631 if (!pos)
632 return 0;
633
Emeric Brun485479d2010-09-23 18:02:19 +0200634 trash->size = trash->size - (pos - trash->str);
Emeric Brun107ca302010-01-04 16:16:05 +0100635 trash->str = pos;
636 trash->len = strlen(pos);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200637 smp->data.u.str = *trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200638 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100639 smp->flags &= ~SMP_F_CONST;
Emeric Brun107ca302010-01-04 16:16:05 +0100640 return 1;
641}
642
Willy Tarreauad635822016-08-08 19:21:09 +0200643/* This function inconditionally duplicates data and removes the "const" flag.
644 * For strings and binary blocks, it also provides a known allocated size with
645 * a length that is capped to the size, and ensures a trailing zero is always
646 * appended for strings. This is necessary for some operations which may
647 * require to extend the length. It returns 0 if it fails, 1 on success.
648 */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100649int smp_dup(struct sample *smp)
Emeric Brun485479d2010-09-23 18:02:19 +0200650{
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100651 struct chunk *trash;
Emeric Brun485479d2010-09-23 18:02:19 +0200652
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200653 switch (smp->data.type) {
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100654 case SMP_T_BOOL:
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100655 case SMP_T_SINT:
656 case SMP_T_ADDR:
657 case SMP_T_IPV4:
658 case SMP_T_IPV6:
659 /* These type are not const. */
660 break;
Willy Tarreauad635822016-08-08 19:21:09 +0200661
Christopher Fauletec100512017-07-24 15:38:41 +0200662 case SMP_T_METH:
663 if (smp->data.u.meth.meth != HTTP_METH_OTHER)
664 break;
665 /* Fall through */
666
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100667 case SMP_T_STR:
Willy Tarreauad635822016-08-08 19:21:09 +0200668 trash = get_trash_chunk();
669 trash->len = smp->data.u.str.len;
670 if (trash->len > trash->size - 1)
671 trash->len = trash->size - 1;
672
673 memcpy(trash->str, smp->data.u.str.str, trash->len);
674 trash->str[trash->len] = 0;
675 smp->data.u.str = *trash;
676 break;
677
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100678 case SMP_T_BIN:
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100679 trash = get_trash_chunk();
Willy Tarreauad635822016-08-08 19:21:09 +0200680 trash->len = smp->data.u.str.len;
681 if (trash->len > trash->size)
682 trash->len = trash->size;
683
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200684 memcpy(trash->str, smp->data.u.str.str, trash->len);
685 smp->data.u.str = *trash;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100686 break;
Christopher Fauletec100512017-07-24 15:38:41 +0200687
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100688 default:
689 /* Other cases are unexpected. */
690 return 0;
691 }
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100692
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100693 /* remove const flag */
694 smp->flags &= ~SMP_F_CONST;
Emeric Brun485479d2010-09-23 18:02:19 +0200695 return 1;
696}
697
Thierry FOURNIER0e9af552013-12-14 14:55:04 +0100698int c_none(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100699{
700 return 1;
701}
702
Willy Tarreau342acb42012-04-23 22:03:39 +0200703static int c_str2int(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100704{
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +0200705 const char *str;
706 const char *end;
Emeric Brun107ca302010-01-04 16:16:05 +0100707
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200708 if (smp->data.u.str.len == 0)
Thierry FOURNIER60bb0202014-01-27 18:20:48 +0100709 return 0;
710
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200711 str = smp->data.u.str.str;
712 end = smp->data.u.str.str + smp->data.u.str.len;
Emeric Brun107ca302010-01-04 16:16:05 +0100713
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200714 smp->data.u.sint = read_int64(&str, end);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200715 smp->data.type = SMP_T_SINT;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100716 smp->flags &= ~SMP_F_CONST;
Emeric Brun107ca302010-01-04 16:16:05 +0100717 return 1;
718}
719
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100720static int c_str2meth(struct sample *smp)
721{
722 enum http_meth_t meth;
723 int len;
724
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200725 meth = find_http_meth(smp->data.u.str.str, smp->data.u.str.len);
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100726 if (meth == HTTP_METH_OTHER) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200727 len = smp->data.u.str.len;
728 smp->data.u.meth.str.str = smp->data.u.str.str;
729 smp->data.u.meth.str.len = len;
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100730 }
731 else
732 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200733 smp->data.u.meth.meth = meth;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200734 smp->data.type = SMP_T_METH;
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100735 return 1;
736}
737
738static int c_meth2str(struct sample *smp)
739{
740 int len;
741 enum http_meth_t meth;
742
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200743 if (smp->data.u.meth.meth == HTTP_METH_OTHER) {
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100744 /* The method is unknown. Copy the original pointer. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200745 len = smp->data.u.meth.str.len;
746 smp->data.u.str.str = smp->data.u.meth.str.str;
747 smp->data.u.str.len = len;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200748 smp->data.type = SMP_T_STR;
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100749 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200750 else if (smp->data.u.meth.meth < HTTP_METH_OTHER) {
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100751 /* The method is known, copy the pointer containing the string. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200752 meth = smp->data.u.meth.meth;
753 smp->data.u.str.str = http_known_methods[meth].name;
754 smp->data.u.str.len = http_known_methods[meth].len;
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100755 smp->flags |= SMP_F_CONST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200756 smp->data.type = SMP_T_STR;
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100757 }
758 else {
759 /* Unknown method */
760 return 0;
761 }
762 return 1;
763}
764
Willy Tarreau9700e5c2014-07-15 21:03:26 +0200765static int c_addr2bin(struct sample *smp)
766{
767 struct chunk *chk = get_trash_chunk();
768
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200769 if (smp->data.type == SMP_T_IPV4) {
Willy Tarreau9700e5c2014-07-15 21:03:26 +0200770 chk->len = 4;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200771 memcpy(chk->str, &smp->data.u.ipv4, chk->len);
Willy Tarreau9700e5c2014-07-15 21:03:26 +0200772 }
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200773 else if (smp->data.type == SMP_T_IPV6) {
Willy Tarreau9700e5c2014-07-15 21:03:26 +0200774 chk->len = 16;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200775 memcpy(chk->str, &smp->data.u.ipv6, chk->len);
Willy Tarreau9700e5c2014-07-15 21:03:26 +0200776 }
777 else
778 return 0;
779
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200780 smp->data.u.str = *chk;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200781 smp->data.type = SMP_T_BIN;
Willy Tarreau9700e5c2014-07-15 21:03:26 +0200782 return 1;
783}
784
Willy Tarreaubbfd1a22014-07-15 21:19:08 +0200785static int c_int2bin(struct sample *smp)
786{
787 struct chunk *chk = get_trash_chunk();
788
Willy Tarreau5f6e9052016-05-20 06:29:59 +0200789 *(unsigned long long int *)chk->str = my_htonll(smp->data.u.sint);
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +0200790 chk->len = 8;
Willy Tarreaubbfd1a22014-07-15 21:19:08 +0200791
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200792 smp->data.u.str = *chk;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200793 smp->data.type = SMP_T_BIN;
Willy Tarreaubbfd1a22014-07-15 21:19:08 +0200794 return 1;
795}
796
Willy Tarreau9700e5c2014-07-15 21:03:26 +0200797
Emeric Brun107ca302010-01-04 16:16:05 +0100798/*****************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +0200799/* Sample casts matrix: */
800/* sample_casts[from type][to type] */
801/* NULL pointer used for impossible sample casts */
Emeric Brun107ca302010-01-04 16:16:05 +0100802/*****************************************************************/
Emeric Brun107ca302010-01-04 16:16:05 +0100803
Thierry FOURNIER8af6ff12013-11-21 10:53:12 +0100804sample_cast_fct sample_casts[SMP_TYPES][SMP_TYPES] = {
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +0200805/* to: ANY BOOL SINT ADDR IPV4 IPV6 STR BIN METH */
806/* from: ANY */ { c_none, c_none, c_none, c_none, c_none, c_none, c_none, c_none, c_none, },
807/* BOOL */ { c_none, c_none, c_none, NULL, NULL, NULL, c_int2str, NULL, NULL, },
Thierry FOURNIERcc4d1712015-07-24 09:04:56 +0200808/* SINT */ { c_none, c_none, c_none, c_int2ip, c_int2ip, c_int2ipv6, c_int2str, c_int2bin, NULL, },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +0200809/* ADDR */ { c_none, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, },
810/* IPV4 */ { c_none, NULL, c_ip2int, c_none, c_none, c_ip2ipv6, c_ip2str, c_addr2bin, NULL, },
Thierry FOURNIERcc4d1712015-07-24 09:04:56 +0200811/* IPV6 */ { c_none, NULL, NULL, c_none, c_ipv62ip,c_none, c_ipv62str, c_addr2bin, NULL, },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +0200812/* STR */ { c_none, c_str2int, c_str2int, c_str2addr, c_str2ip, c_str2ipv6, c_none, c_none, c_str2meth, },
813/* BIN */ { c_none, NULL, NULL, NULL, NULL, NULL, c_bin2str, c_none, c_str2meth, },
814/* METH */ { c_none, NULL, NULL, NULL, NULL, NULL, c_meth2str, c_meth2str, c_none, }
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200815};
Emeric Brun107ca302010-01-04 16:16:05 +0100816
Emeric Brun107ca302010-01-04 16:16:05 +0100817/*
Willy Tarreau12785782012-04-27 21:37:17 +0200818 * Parse a sample expression configuration:
Emeric Brun107ca302010-01-04 16:16:05 +0100819 * fetch keyword followed by format conversion keywords.
Willy Tarreau12785782012-04-27 21:37:17 +0200820 * Returns a pointer on allocated sample expression structure.
Willy Tarreaua4312fa2013-04-02 16:34:32 +0200821 * The caller must have set al->ctx.
Emeric Brun107ca302010-01-04 16:16:05 +0100822 */
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +0100823struct sample_expr *sample_parse_expr(char **str, int *idx, const char *file, int line, char **err_msg, struct arg_list *al)
Emeric Brun107ca302010-01-04 16:16:05 +0100824{
Willy Tarreau833cc792013-07-24 15:34:19 +0200825 const char *begw; /* beginning of word */
826 const char *endw; /* end of word */
827 const char *endt; /* end of term */
Willy Tarreau12785782012-04-27 21:37:17 +0200828 struct sample_expr *expr;
829 struct sample_fetch *fetch;
830 struct sample_conv *conv;
Emeric Brun107ca302010-01-04 16:16:05 +0100831 unsigned long prev_type;
Willy Tarreau833cc792013-07-24 15:34:19 +0200832 char *fkw = NULL;
833 char *ckw = NULL;
Willy Tarreau689a1df2013-12-13 00:40:11 +0100834 int err_arg;
Emeric Brun107ca302010-01-04 16:16:05 +0100835
Willy Tarreau833cc792013-07-24 15:34:19 +0200836 begw = str[*idx];
837 for (endw = begw; *endw && *endw != '(' && *endw != ','; endw++);
838
839 if (endw == begw) {
Willy Tarreau975c1782013-12-12 23:16:54 +0100840 memprintf(err_msg, "missing fetch method");
Emeric Brun107ca302010-01-04 16:16:05 +0100841 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200842 }
Emeric Brun107ca302010-01-04 16:16:05 +0100843
Willy Tarreau833cc792013-07-24 15:34:19 +0200844 /* keep a copy of the current fetch keyword for error reporting */
845 fkw = my_strndup(begw, endw - begw);
Emeric Brun107ca302010-01-04 16:16:05 +0100846
Willy Tarreau833cc792013-07-24 15:34:19 +0200847 fetch = find_sample_fetch(begw, endw - begw);
848 if (!fetch) {
Willy Tarreau975c1782013-12-12 23:16:54 +0100849 memprintf(err_msg, "unknown fetch method '%s'", fkw);
Emeric Brun107ca302010-01-04 16:16:05 +0100850 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200851 }
Emeric Brun107ca302010-01-04 16:16:05 +0100852
Willy Tarreau833cc792013-07-24 15:34:19 +0200853 endt = endw;
854 if (*endt == '(') {
Willy Tarreau689a1df2013-12-13 00:40:11 +0100855 /* look for the end of this term and skip the opening parenthesis */
856 endt = ++endw;
Willy Tarreau833cc792013-07-24 15:34:19 +0200857 while (*endt && *endt != ')')
858 endt++;
859 if (*endt != ')') {
Willy Tarreau975c1782013-12-12 23:16:54 +0100860 memprintf(err_msg, "missing closing ')' after arguments to fetch keyword '%s'", fkw);
Willy Tarreau833cc792013-07-24 15:34:19 +0200861 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200862 }
Emeric Brun485479d2010-09-23 18:02:19 +0200863 }
Emeric Brun107ca302010-01-04 16:16:05 +0100864
Willy Tarreau833cc792013-07-24 15:34:19 +0200865 /* At this point, we have :
866 * - begw : beginning of the keyword
Willy Tarreau689a1df2013-12-13 00:40:11 +0100867 * - endw : end of the keyword, first character not part of keyword
868 * nor the opening parenthesis (so first character of args
869 * if present).
Willy Tarreau833cc792013-07-24 15:34:19 +0200870 * - endt : end of the term (=endw or last parenthesis if args are present)
871 */
872
873 if (fetch->out_type >= SMP_TYPES) {
Willy Tarreau975c1782013-12-12 23:16:54 +0100874 memprintf(err_msg, "returns type of fetch method '%s' is unknown", fkw);
Emeric Brun107ca302010-01-04 16:16:05 +0100875 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200876 }
Emeric Brun107ca302010-01-04 16:16:05 +0100877 prev_type = fetch->out_type;
Willy Tarreau833cc792013-07-24 15:34:19 +0200878
Vincent Bernat02779b62016-04-03 13:48:43 +0200879 expr = calloc(1, sizeof(*expr));
Emeric Brun485479d2010-09-23 18:02:19 +0200880 if (!expr)
881 goto out_error;
Emeric Brun107ca302010-01-04 16:16:05 +0100882
883 LIST_INIT(&(expr->conv_exprs));
884 expr->fetch = fetch;
Willy Tarreau2e845be2012-10-19 19:49:09 +0200885 expr->arg_p = empty_arg_list;
Emeric Brun107ca302010-01-04 16:16:05 +0100886
Willy Tarreau689a1df2013-12-13 00:40:11 +0100887 /* Note that we call the argument parser even with an empty string,
888 * this allows it to automatically create entries for mandatory
889 * implicit arguments (eg: local proxy name).
890 */
891 al->kw = expr->fetch->kw;
892 al->conv = NULL;
893 if (make_arg_list(endw, endt - endw, fetch->arg_mask, &expr->arg_p, err_msg, NULL, &err_arg, al) < 0) {
894 memprintf(err_msg, "fetch method '%s' : %s", fkw, *err_msg);
895 goto out_error;
896 }
Willy Tarreau2e845be2012-10-19 19:49:09 +0200897
Willy Tarreau689a1df2013-12-13 00:40:11 +0100898 if (!expr->arg_p) {
899 expr->arg_p = empty_arg_list;
Emeric Brun485479d2010-09-23 18:02:19 +0200900 }
Willy Tarreau689a1df2013-12-13 00:40:11 +0100901 else if (fetch->val_args && !fetch->val_args(expr->arg_p, err_msg)) {
902 memprintf(err_msg, "invalid args in fetch method '%s' : %s", fkw, *err_msg);
Emeric Brun485479d2010-09-23 18:02:19 +0200903 goto out_error;
Emeric Brun107ca302010-01-04 16:16:05 +0100904 }
905
Willy Tarreau833cc792013-07-24 15:34:19 +0200906 /* Now process the converters if any. We have two supported syntaxes
907 * for the converters, which can be combined :
908 * - comma-delimited list of converters just after the keyword and args ;
909 * - one converter per keyword
910 * The combination allows to have each keyword being a comma-delimited
911 * series of converters.
912 *
913 * We want to process the former first, then the latter. For this we start
914 * from the beginning of the supposed place in the exiting conv chain, which
915 * starts at the last comma (endt).
916 */
917
918 while (1) {
Willy Tarreau12785782012-04-27 21:37:17 +0200919 struct sample_conv_expr *conv_expr;
Emeric Brun107ca302010-01-04 16:16:05 +0100920
Willy Tarreau833cc792013-07-24 15:34:19 +0200921 if (*endt == ')') /* skip last closing parenthesis */
922 endt++;
Emeric Brun107ca302010-01-04 16:16:05 +0100923
Willy Tarreau833cc792013-07-24 15:34:19 +0200924 if (*endt && *endt != ',') {
925 if (ckw)
Willy Tarreau97108e02016-11-25 07:33:24 +0100926 memprintf(err_msg, "missing comma after converter '%s'", ckw);
Willy Tarreau833cc792013-07-24 15:34:19 +0200927 else
Willy Tarreau975c1782013-12-12 23:16:54 +0100928 memprintf(err_msg, "missing comma after fetch keyword '%s'", fkw);
Emeric Brun107ca302010-01-04 16:16:05 +0100929 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200930 }
Emeric Brun107ca302010-01-04 16:16:05 +0100931
Willy Tarreau833cc792013-07-24 15:34:19 +0200932 while (*endt == ',') /* then trailing commas */
933 endt++;
934
Willy Tarreau97108e02016-11-25 07:33:24 +0100935 begw = endt; /* start of converter */
Willy Tarreau833cc792013-07-24 15:34:19 +0200936
937 if (!*begw) {
938 /* none ? skip to next string */
939 (*idx)++;
940 begw = str[*idx];
941 if (!begw || !*begw)
942 break;
943 }
944
945 for (endw = begw; *endw && *endw != '(' && *endw != ','; endw++);
946
947 free(ckw);
948 ckw = my_strndup(begw, endw - begw);
949
950 conv = find_sample_conv(begw, endw - begw);
951 if (!conv) {
952 /* we found an isolated keyword that we don't know, it's not ours */
953 if (begw == str[*idx])
954 break;
Willy Tarreau97108e02016-11-25 07:33:24 +0100955 memprintf(err_msg, "unknown converter '%s'", ckw);
Willy Tarreau833cc792013-07-24 15:34:19 +0200956 goto out_error;
957 }
Emeric Brun107ca302010-01-04 16:16:05 +0100958
Willy Tarreau833cc792013-07-24 15:34:19 +0200959 endt = endw;
960 if (*endt == '(') {
961 /* look for the end of this term */
962 while (*endt && *endt != ')')
963 endt++;
964 if (*endt != ')') {
Willy Tarreau97108e02016-11-25 07:33:24 +0100965 memprintf(err_msg, "syntax error: missing ')' after converter '%s'", ckw);
Willy Tarreau833cc792013-07-24 15:34:19 +0200966 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200967 }
Willy Tarreau833cc792013-07-24 15:34:19 +0200968 }
969
970 if (conv->in_type >= SMP_TYPES || conv->out_type >= SMP_TYPES) {
Willy Tarreau97108e02016-11-25 07:33:24 +0100971 memprintf(err_msg, "returns type of converter '%s' is unknown", ckw);
Emeric Brun107ca302010-01-04 16:16:05 +0100972 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200973 }
Emeric Brun107ca302010-01-04 16:16:05 +0100974
975 /* If impossible type conversion */
Willy Tarreau12785782012-04-27 21:37:17 +0200976 if (!sample_casts[prev_type][conv->in_type]) {
Willy Tarreau97108e02016-11-25 07:33:24 +0100977 memprintf(err_msg, "converter '%s' cannot be applied", ckw);
Emeric Brun107ca302010-01-04 16:16:05 +0100978 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200979 }
Emeric Brun107ca302010-01-04 16:16:05 +0100980
981 prev_type = conv->out_type;
Vincent Bernat02779b62016-04-03 13:48:43 +0200982 conv_expr = calloc(1, sizeof(*conv_expr));
Emeric Brun485479d2010-09-23 18:02:19 +0200983 if (!conv_expr)
984 goto out_error;
Emeric Brun107ca302010-01-04 16:16:05 +0100985
986 LIST_ADDQ(&(expr->conv_exprs), &(conv_expr->list));
987 conv_expr->conv = conv;
988
Willy Tarreau833cc792013-07-24 15:34:19 +0200989 if (endt != endw) {
Willy Tarreaub27c0d32012-04-20 16:04:47 +0200990 int err_arg;
Willy Tarreau21d68a62012-04-20 15:52:36 +0200991
Willy Tarreau9fcb9842012-04-20 14:45:49 +0200992 if (!conv->arg_mask) {
Willy Tarreau97108e02016-11-25 07:33:24 +0100993 memprintf(err_msg, "converter '%s' does not support any args", ckw);
Emeric Brun485479d2010-09-23 18:02:19 +0200994 goto out_error;
995 }
Willy Tarreau9e92d322010-01-26 17:58:06 +0100996
Willy Tarreaua4312fa2013-04-02 16:34:32 +0200997 al->kw = expr->fetch->kw;
998 al->conv = conv_expr->conv->kw;
Willy Tarreau975c1782013-12-12 23:16:54 +0100999 if (make_arg_list(endw + 1, endt - endw - 1, conv->arg_mask, &conv_expr->arg_p, err_msg, NULL, &err_arg, al) < 0) {
Willy Tarreau97108e02016-11-25 07:33:24 +01001000 memprintf(err_msg, "invalid arg %d in converter '%s' : %s", err_arg+1, ckw, *err_msg);
Willy Tarreau21d68a62012-04-20 15:52:36 +02001001 goto out_error;
1002 }
1003
Willy Tarreau2e845be2012-10-19 19:49:09 +02001004 if (!conv_expr->arg_p)
1005 conv_expr->arg_p = empty_arg_list;
1006
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +01001007 if (conv->val_args && !conv->val_args(conv_expr->arg_p, conv, file, line, err_msg)) {
Willy Tarreau97108e02016-11-25 07:33:24 +01001008 memprintf(err_msg, "invalid args in converter '%s' : %s", ckw, *err_msg);
Emeric Brun485479d2010-09-23 18:02:19 +02001009 goto out_error;
1010 }
1011 }
Willy Tarreau833cc792013-07-24 15:34:19 +02001012 else if (ARGM(conv->arg_mask)) {
Willy Tarreau97108e02016-11-25 07:33:24 +01001013 memprintf(err_msg, "missing args for converter '%s'", ckw);
Emeric Brun485479d2010-09-23 18:02:19 +02001014 goto out_error;
Emeric Brun107ca302010-01-04 16:16:05 +01001015 }
1016 }
Emeric Brun485479d2010-09-23 18:02:19 +02001017
Willy Tarreau833cc792013-07-24 15:34:19 +02001018 out:
1019 free(fkw);
1020 free(ckw);
Emeric Brun107ca302010-01-04 16:16:05 +01001021 return expr;
1022
1023out_error:
Willy Tarreau12785782012-04-27 21:37:17 +02001024 /* TODO: prune_sample_expr(expr); */
Willy Tarreau833cc792013-07-24 15:34:19 +02001025 expr = NULL;
1026 goto out;
Emeric Brun107ca302010-01-04 16:16:05 +01001027}
1028
1029/*
Willy Tarreau12785782012-04-27 21:37:17 +02001030 * Process a fetch + format conversion of defined by the sample expression <expr>
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001031 * on request or response considering the <opt> parameter.
Willy Tarreau12785782012-04-27 21:37:17 +02001032 * Returns a pointer on a typed sample structure containing the result or NULL if
1033 * sample is not found or when format conversion failed.
Emeric Brun107ca302010-01-04 16:16:05 +01001034 * If <p> is not null, function returns results in structure pointed by <p>.
Willy Tarreau12785782012-04-27 21:37:17 +02001035 * If <p> is null, functions returns a pointer on a static sample structure.
Willy Tarreaub8c8f1f2012-04-23 22:38:26 +02001036 *
1037 * Note: the fetch functions are required to properly set the return type. The
1038 * conversion functions must do so too. However the cast functions do not need
1039 * to since they're made to cast mutiple types according to what is required.
Willy Tarreau6bcb0a82014-07-30 08:56:35 +02001040 *
1041 * The caller may indicate in <opt> if it considers the result final or not.
1042 * The caller needs to check the SMP_F_MAY_CHANGE flag in p->flags to verify
1043 * if the result is stable or not, according to the following table :
1044 *
1045 * return MAY_CHANGE FINAL Meaning for the sample
1046 * NULL 0 * Not present and will never be (eg: header)
1047 * NULL 1 0 Not present yet, could change (eg: POST param)
1048 * NULL 1 1 Not present yet, will not change anymore
1049 * smp 0 * Present and will not change (eg: header)
1050 * smp 1 0 Present, may change (eg: request length)
1051 * smp 1 1 Present, last known value (eg: request length)
Emeric Brun107ca302010-01-04 16:16:05 +01001052 */
Willy Tarreau192252e2015-04-04 01:47:55 +02001053struct sample *sample_process(struct proxy *px, struct session *sess,
1054 struct stream *strm, unsigned int opt,
Willy Tarreau12785782012-04-27 21:37:17 +02001055 struct sample_expr *expr, struct sample *p)
Emeric Brun107ca302010-01-04 16:16:05 +01001056{
Willy Tarreau12785782012-04-27 21:37:17 +02001057 struct sample_conv_expr *conv_expr;
Emeric Brun107ca302010-01-04 16:16:05 +01001058
Willy Tarreau18387e22013-07-25 12:02:38 +02001059 if (p == NULL) {
Willy Tarreaub4a88f02012-04-23 21:35:11 +02001060 p = &temp_smp;
Willy Tarreau6c616e02014-06-25 16:56:41 +02001061 memset(p, 0, sizeof(*p));
Willy Tarreau18387e22013-07-25 12:02:38 +02001062 }
Emeric Brun107ca302010-01-04 16:16:05 +01001063
Willy Tarreau1777ea62016-03-10 16:15:46 +01001064 smp_set_owner(p, px, sess, strm, opt);
Thierry FOURNIER0786d052015-05-11 15:42:45 +02001065 if (!expr->fetch->process(expr->arg_p, p, expr->fetch->kw, expr->fetch->private))
Emeric Brun107ca302010-01-04 16:16:05 +01001066 return NULL;
1067
Emeric Brun107ca302010-01-04 16:16:05 +01001068 list_for_each_entry(conv_expr, &expr->conv_exprs, list) {
Willy Tarreau12e50112012-04-25 17:21:49 +02001069 /* we want to ensure that p->type can be casted into
1070 * conv_expr->conv->in_type. We have 3 possibilities :
1071 * - NULL => not castable.
1072 * - c_none => nothing to do (let's optimize it)
1073 * - other => apply cast and prepare to fail
1074 */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001075 if (!sample_casts[p->data.type][conv_expr->conv->in_type])
Willy Tarreau12e50112012-04-25 17:21:49 +02001076 return NULL;
1077
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001078 if (sample_casts[p->data.type][conv_expr->conv->in_type] != c_none &&
1079 !sample_casts[p->data.type][conv_expr->conv->in_type](p))
Emeric Brun107ca302010-01-04 16:16:05 +01001080 return NULL;
1081
Willy Tarreau12e50112012-04-25 17:21:49 +02001082 /* OK cast succeeded */
1083
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001084 if (!conv_expr->conv->process(conv_expr->arg_p, p, conv_expr->conv->private))
Emeric Brun107ca302010-01-04 16:16:05 +01001085 return NULL;
Emeric Brun107ca302010-01-04 16:16:05 +01001086 }
1087 return p;
1088}
1089
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001090/*
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001091 * Resolve all remaining arguments in proxy <p>. Returns the number of
1092 * errors or 0 if everything is fine.
1093 */
1094int smp_resolve_args(struct proxy *p)
1095{
1096 struct arg_list *cur, *bak;
1097 const char *ctx, *where;
1098 const char *conv_ctx, *conv_pre, *conv_pos;
1099 struct userlist *ul;
Willy Tarreau46947782015-01-19 19:00:58 +01001100 struct my_regex *reg;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001101 struct arg *arg;
1102 int cfgerr = 0;
Willy Tarreau46947782015-01-19 19:00:58 +01001103 int rflags;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001104
1105 list_for_each_entry_safe(cur, bak, &p->conf.args.list, list) {
1106 struct proxy *px;
1107 struct server *srv;
1108 char *pname, *sname;
Willy Tarreau46947782015-01-19 19:00:58 +01001109 char *err;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001110
1111 arg = cur->arg;
1112
1113 /* prepare output messages */
1114 conv_pre = conv_pos = conv_ctx = "";
1115 if (cur->conv) {
1116 conv_ctx = cur->conv;
1117 conv_pre = "conversion keyword '";
1118 conv_pos = "' for ";
1119 }
1120
1121 where = "in";
1122 ctx = "sample fetch keyword";
1123 switch (cur->ctx) {
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001124 case ARGC_STK: where = "in stick rule in"; break;
1125 case ARGC_TRK: where = "in tracking rule in"; break;
1126 case ARGC_LOG: where = "in log-format string in"; break;
1127 case ARGC_LOGSD: where = "in log-format-sd string in"; break;
1128 case ARGC_HRQ: where = "in http-request header format string in"; break;
1129 case ARGC_HRS: where = "in http-response header format string in"; break;
1130 case ARGC_UIF: where = "in unique-id-format string in"; break;
1131 case ARGC_RDR: where = "in redirect format string in"; break;
1132 case ARGC_CAP: where = "in capture rule in"; break;
1133 case ARGC_ACL: ctx = "ACL keyword"; break;
1134 case ARGC_SRV: where = "in server directive in"; break;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001135 case ARGC_SPOE: where = "in spoe-message directive in"; break;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001136 }
1137
1138 /* set a few default settings */
1139 px = p;
1140 pname = p->id;
1141
1142 switch (arg->type) {
1143 case ARGT_SRV:
1144 if (!arg->data.str.len) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001145 ha_alert("parsing [%s:%d] : missing server name in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1146 cur->file, cur->line,
1147 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001148 cfgerr++;
1149 continue;
1150 }
1151
1152 /* we support two formats : "bck/srv" and "srv" */
1153 sname = strrchr(arg->data.str.str, '/');
1154
1155 if (sname) {
1156 *sname++ = '\0';
1157 pname = arg->data.str.str;
1158
Willy Tarreau9e0bb102015-05-26 11:24:42 +02001159 px = proxy_be_by_name(pname);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001160 if (!px) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001161 ha_alert("parsing [%s:%d] : unable to find proxy '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1162 cur->file, cur->line, pname,
1163 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001164 cfgerr++;
1165 break;
1166 }
1167 }
1168 else
1169 sname = arg->data.str.str;
1170
1171 srv = findserver(px, sname);
1172 if (!srv) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001173 ha_alert("parsing [%s:%d] : unable to find server '%s' in proxy '%s', referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1174 cur->file, cur->line, sname, pname,
1175 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001176 cfgerr++;
1177 break;
1178 }
1179
1180 free(arg->data.str.str);
1181 arg->data.str.str = NULL;
1182 arg->unresolved = 0;
1183 arg->data.srv = srv;
1184 break;
1185
1186 case ARGT_FE:
1187 if (arg->data.str.len) {
1188 pname = arg->data.str.str;
Willy Tarreau9e0bb102015-05-26 11:24:42 +02001189 px = proxy_fe_by_name(pname);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001190 }
1191
1192 if (!px) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001193 ha_alert("parsing [%s:%d] : unable to find frontend '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1194 cur->file, cur->line, pname,
1195 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001196 cfgerr++;
1197 break;
1198 }
1199
1200 if (!(px->cap & PR_CAP_FE)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001201 ha_alert("parsing [%s:%d] : proxy '%s', referenced in arg %d of %s%s%s%s '%s' %s proxy '%s', has not frontend capability.\n",
1202 cur->file, cur->line, pname,
1203 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001204 cfgerr++;
1205 break;
1206 }
1207
1208 free(arg->data.str.str);
1209 arg->data.str.str = NULL;
1210 arg->unresolved = 0;
1211 arg->data.prx = px;
1212 break;
1213
1214 case ARGT_BE:
1215 if (arg->data.str.len) {
1216 pname = arg->data.str.str;
Willy Tarreau9e0bb102015-05-26 11:24:42 +02001217 px = proxy_be_by_name(pname);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001218 }
1219
1220 if (!px) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001221 ha_alert("parsing [%s:%d] : unable to find backend '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1222 cur->file, cur->line, pname,
1223 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001224 cfgerr++;
1225 break;
1226 }
1227
1228 if (!(px->cap & PR_CAP_BE)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001229 ha_alert("parsing [%s:%d] : proxy '%s', referenced in arg %d of %s%s%s%s '%s' %s proxy '%s', has not backend capability.\n",
1230 cur->file, cur->line, pname,
1231 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001232 cfgerr++;
1233 break;
1234 }
1235
1236 free(arg->data.str.str);
1237 arg->data.str.str = NULL;
1238 arg->unresolved = 0;
1239 arg->data.prx = px;
1240 break;
1241
1242 case ARGT_TAB:
1243 if (arg->data.str.len) {
1244 pname = arg->data.str.str;
Willy Tarreaue2dc1fa2015-05-26 12:08:07 +02001245 px = proxy_tbl_by_name(pname);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001246 }
1247
1248 if (!px) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001249 ha_alert("parsing [%s:%d] : unable to find table '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1250 cur->file, cur->line, pname,
1251 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001252 cfgerr++;
1253 break;
1254 }
1255
1256 if (!px->table.size) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001257 ha_alert("parsing [%s:%d] : no table in proxy '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1258 cur->file, cur->line, pname,
1259 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001260 cfgerr++;
1261 break;
1262 }
1263
1264 free(arg->data.str.str);
1265 arg->data.str.str = NULL;
1266 arg->unresolved = 0;
1267 arg->data.prx = px;
1268 break;
1269
1270 case ARGT_USR:
1271 if (!arg->data.str.len) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001272 ha_alert("parsing [%s:%d] : missing userlist name in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1273 cur->file, cur->line,
1274 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001275 cfgerr++;
1276 break;
1277 }
1278
1279 if (p->uri_auth && p->uri_auth->userlist &&
1280 !strcmp(p->uri_auth->userlist->name, arg->data.str.str))
1281 ul = p->uri_auth->userlist;
1282 else
1283 ul = auth_find_userlist(arg->data.str.str);
1284
1285 if (!ul) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001286 ha_alert("parsing [%s:%d] : unable to find userlist '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1287 cur->file, cur->line, arg->data.str.str,
1288 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001289 cfgerr++;
1290 break;
1291 }
1292
1293 free(arg->data.str.str);
1294 arg->data.str.str = NULL;
1295 arg->unresolved = 0;
1296 arg->data.usr = ul;
1297 break;
Willy Tarreau46947782015-01-19 19:00:58 +01001298
1299 case ARGT_REG:
1300 if (!arg->data.str.len) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001301 ha_alert("parsing [%s:%d] : missing regex in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1302 cur->file, cur->line,
1303 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreau46947782015-01-19 19:00:58 +01001304 cfgerr++;
1305 continue;
1306 }
1307
1308 reg = calloc(1, sizeof(*reg));
1309 if (!reg) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001310 ha_alert("parsing [%s:%d] : not enough memory to build regex in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1311 cur->file, cur->line,
1312 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreau46947782015-01-19 19:00:58 +01001313 cfgerr++;
1314 continue;
1315 }
1316
1317 rflags = 0;
1318 rflags |= (arg->type_flags & ARGF_REG_ICASE) ? REG_ICASE : 0;
1319 err = NULL;
1320
Willy Tarreaud817e462015-01-23 20:23:17 +01001321 if (!regex_comp(arg->data.str.str, reg, !(rflags & REG_ICASE), 1 /* capture substr */, &err)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001322 ha_alert("parsing [%s:%d] : error in regex '%s' in arg %d of %s%s%s%s '%s' %s proxy '%s' : %s.\n",
1323 cur->file, cur->line,
1324 arg->data.str.str,
1325 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id, err);
Willy Tarreau46947782015-01-19 19:00:58 +01001326 cfgerr++;
1327 continue;
1328 }
1329
1330 free(arg->data.str.str);
1331 arg->data.str.str = NULL;
1332 arg->unresolved = 0;
1333 arg->data.reg = reg;
1334 break;
1335
1336
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001337 }
1338
1339 LIST_DEL(&cur->list);
1340 free(cur);
1341 } /* end of args processing */
1342
1343 return cfgerr;
1344}
1345
1346/*
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001347 * Process a fetch + format conversion as defined by the sample expression
1348 * <expr> on request or response considering the <opt> parameter. The output is
Adis Nezirovic79beb242015-07-06 15:41:02 +02001349 * not explicitly set to <smp_type>, but shall be compatible with it as
1350 * specified by 'sample_casts' table. If a stable sample can be fetched, or an
1351 * unstable one when <opt> contains SMP_OPT_FINAL, the sample is converted and
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001352 * returned without the SMP_F_MAY_CHANGE flag. If an unstable sample is found
1353 * and <opt> does not contain SMP_OPT_FINAL, then the sample is returned as-is
1354 * with its SMP_F_MAY_CHANGE flag so that the caller can check it and decide to
1355 * take actions (eg: wait longer). If a sample could not be found or could not
Willy Tarreau6bcb0a82014-07-30 08:56:35 +02001356 * be converted, NULL is returned. The caller MUST NOT use the sample if the
1357 * SMP_F_MAY_CHANGE flag is present, as it is used only as a hint that there is
1358 * still hope to get it after waiting longer, and is not converted to string.
1359 * The possible output combinations are the following :
1360 *
1361 * return MAY_CHANGE FINAL Meaning for the sample
1362 * NULL * * Not present and will never be (eg: header)
1363 * smp 0 * Final value converted (eg: header)
1364 * smp 1 0 Not present yet, may appear later (eg: header)
1365 * smp 1 1 never happens (either flag is cleared on output)
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001366 */
Adis Nezirovic79beb242015-07-06 15:41:02 +02001367struct sample *sample_fetch_as_type(struct proxy *px, struct session *sess,
Willy Tarreau192252e2015-04-04 01:47:55 +02001368 struct stream *strm, unsigned int opt,
Adis Nezirovic79beb242015-07-06 15:41:02 +02001369 struct sample_expr *expr, int smp_type)
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001370{
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001371 struct sample *smp = &temp_smp;
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001372
Willy Tarreau6c616e02014-06-25 16:56:41 +02001373 memset(smp, 0, sizeof(*smp));
1374
Willy Tarreau192252e2015-04-04 01:47:55 +02001375 if (!sample_process(px, sess, strm, opt, expr, smp)) {
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001376 if ((smp->flags & SMP_F_MAY_CHANGE) && !(opt & SMP_OPT_FINAL))
1377 return smp;
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001378 return NULL;
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001379 }
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001380
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001381 if (!sample_casts[smp->data.type][smp_type])
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001382 return NULL;
1383
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001384 if (!sample_casts[smp->data.type][smp_type](smp))
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001385 return NULL;
1386
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001387 smp->flags &= ~SMP_F_MAY_CHANGE;
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001388 return smp;
1389}
1390
Christopher Faulet476e5d02016-10-26 11:34:47 +02001391static void release_sample_arg(struct arg *p)
1392{
1393 struct arg *p_back = p;
1394
1395 if (!p)
1396 return;
1397
1398 while (p->type != ARGT_STOP) {
1399 if (p->type == ARGT_STR || p->unresolved) {
1400 free(p->data.str.str);
1401 p->data.str.str = NULL;
1402 p->unresolved = 0;
1403 }
1404 else if (p->type == ARGT_REG) {
1405 if (p->data.reg) {
1406 regex_free(p->data.reg);
1407 free(p->data.reg);
1408 p->data.reg = NULL;
1409 }
1410 }
1411 p++;
1412 }
1413
1414 if (p_back != empty_arg_list)
1415 free(p_back);
1416}
1417
1418void release_sample_expr(struct sample_expr *expr)
1419{
1420 struct sample_conv_expr *conv_expr, *conv_exprb;
1421
1422 if (!expr)
1423 return;
1424
1425 list_for_each_entry_safe(conv_expr, conv_exprb, &expr->conv_exprs, list)
1426 release_sample_arg(conv_expr->arg_p);
1427 release_sample_arg(expr->arg_p);
1428 free(expr);
1429}
1430
Emeric Brun107ca302010-01-04 16:16:05 +01001431/*****************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +02001432/* Sample format convert functions */
Willy Tarreaub8c8f1f2012-04-23 22:38:26 +02001433/* These functions set the data type on return. */
Emeric Brun107ca302010-01-04 16:16:05 +01001434/*****************************************************************/
1435
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001436#ifdef DEBUG_EXPR
1437static int sample_conv_debug(const struct arg *arg_p, struct sample *smp, void *private)
1438{
1439 int i;
1440 struct sample tmp;
1441
1442 if (!(global.mode & MODE_QUIET) || (global.mode & (MODE_VERBOSE | MODE_STARTING))) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001443 fprintf(stderr, "[debug converter] type: %s ", smp_to_type[smp->data.type]);
1444 if (!sample_casts[smp->data.type][SMP_T_STR]) {
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001445 fprintf(stderr, "(undisplayable)");
1446 } else {
1447
1448 /* Copy sample fetch. This put the sample as const, the
1449 * cast will copy data if a transformation is required.
1450 */
1451 memcpy(&tmp, smp, sizeof(struct sample));
1452 tmp.flags = SMP_F_CONST;
1453
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001454 if (!sample_casts[smp->data.type][SMP_T_STR](&tmp))
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001455 fprintf(stderr, "(undisplayable)");
1456
1457 else {
1458 /* Display the displayable chars*. */
1459 fprintf(stderr, "<");
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001460 for (i = 0; i < tmp.data.u.str.len; i++) {
1461 if (isprint(tmp.data.u.str.str[i]))
1462 fputc(tmp.data.u.str.str[i], stderr);
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001463 else
1464 fputc('.', stderr);
1465 }
1466 }
1467 fprintf(stderr, ">\n");
1468 }
1469 }
1470 return 1;
1471}
1472#endif
1473
Holger Just1bfc24b2017-05-06 00:56:53 +02001474static int sample_conv_base642bin(const struct arg *arg_p, struct sample *smp, void *private)
1475{
1476 struct chunk *trash = get_trash_chunk();
1477 int bin_len;
1478
1479 trash->len = 0;
1480 bin_len = base64dec(smp->data.u.str.str, smp->data.u.str.len, trash->str, trash->size);
1481 if (bin_len < 0)
1482 return 0;
1483
1484 trash->len = bin_len;
1485 smp->data.u.str = *trash;
1486 smp->data.type = SMP_T_BIN;
1487 smp->flags &= ~SMP_F_CONST;
1488 return 1;
1489}
1490
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001491static int sample_conv_bin2base64(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brun53d1a982014-04-30 18:21:37 +02001492{
1493 struct chunk *trash = get_trash_chunk();
1494 int b64_len;
1495
1496 trash->len = 0;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001497 b64_len = a2base64(smp->data.u.str.str, smp->data.u.str.len, trash->str, trash->size);
Emeric Brun53d1a982014-04-30 18:21:37 +02001498 if (b64_len < 0)
1499 return 0;
1500
1501 trash->len = b64_len;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001502 smp->data.u.str = *trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001503 smp->data.type = SMP_T_STR;
Emeric Brun53d1a982014-04-30 18:21:37 +02001504 smp->flags &= ~SMP_F_CONST;
1505 return 1;
1506}
1507
Dragan Dosen6e5a9ca2017-10-24 09:18:23 +02001508static int sample_conv_sha1(const struct arg *arg_p, struct sample *smp, void *private)
1509{
1510 blk_SHA_CTX ctx;
1511 struct chunk *trash = get_trash_chunk();
1512
1513 memset(&ctx, 0, sizeof(ctx));
1514
1515 blk_SHA1_Init(&ctx);
1516 blk_SHA1_Update(&ctx, smp->data.u.str.str, smp->data.u.str.len);
1517 blk_SHA1_Final((unsigned char *)trash->str, &ctx);
1518
1519 trash->len = 20;
1520 smp->data.u.str = *trash;
1521 smp->data.type = SMP_T_BIN;
1522 smp->flags &= ~SMP_F_CONST;
1523 return 1;
1524}
1525
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001526static int sample_conv_bin2hex(const struct arg *arg_p, struct sample *smp, void *private)
Thierry FOURNIER2f49d6d2014-03-12 15:01:52 +01001527{
1528 struct chunk *trash = get_trash_chunk();
1529 unsigned char c;
1530 int ptr = 0;
1531
1532 trash->len = 0;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001533 while (ptr < smp->data.u.str.len && trash->len <= trash->size - 2) {
1534 c = smp->data.u.str.str[ptr++];
Thierry FOURNIER2f49d6d2014-03-12 15:01:52 +01001535 trash->str[trash->len++] = hextab[(c >> 4) & 0xF];
1536 trash->str[trash->len++] = hextab[c & 0xF];
1537 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001538 smp->data.u.str = *trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001539 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01001540 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIER2f49d6d2014-03-12 15:01:52 +01001541 return 1;
1542}
1543
Dragan Dosen3f957b22017-10-24 09:27:34 +02001544static int sample_conv_hex2int(const struct arg *arg_p, struct sample *smp, void *private)
1545{
1546 long long int n = 0;
1547 int i, c;
1548
1549 for (i = 0; i < smp->data.u.str.len; i++) {
1550 if ((c = hex2i(smp->data.u.str.str[i])) < 0)
1551 return 0;
1552 n = (n << 4) + c;
1553 }
1554
1555 smp->data.u.sint = n;
1556 smp->data.type = SMP_T_SINT;
1557 smp->flags &= ~SMP_F_CONST;
1558 return 1;
1559}
1560
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001561/* hashes the binary input into a 32-bit unsigned int */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001562static int sample_conv_djb2(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001563{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001564 smp->data.u.sint = hash_djb2(smp->data.u.str.str, smp->data.u.str.len);
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001565 if (arg_p && arg_p->data.sint)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001566 smp->data.u.sint = full_hash(smp->data.u.sint);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001567 smp->data.type = SMP_T_SINT;
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001568 return 1;
1569}
1570
Willy Tarreau60a2ee72017-12-15 07:13:48 +01001571static int sample_conv_length(const struct arg *arg_p, struct sample *smp, void *private)
Etienne Carriereed0d24e2017-12-13 13:41:34 +01001572{
1573 int i = smp->data.u.str.len;
1574 smp->data.u.sint = i;
1575 smp->data.type = SMP_T_SINT;
1576 return 1;
1577}
1578
1579
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001580static int sample_conv_str2lower(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brun107ca302010-01-04 16:16:05 +01001581{
1582 int i;
1583
Willy Tarreauf0645dc2016-08-09 14:29:38 +02001584 if (!smp_make_rw(smp))
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01001585 return 0;
1586
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001587 for (i = 0; i < smp->data.u.str.len; i++) {
1588 if ((smp->data.u.str.str[i] >= 'A') && (smp->data.u.str.str[i] <= 'Z'))
1589 smp->data.u.str.str[i] += 'a' - 'A';
Emeric Brun107ca302010-01-04 16:16:05 +01001590 }
1591 return 1;
1592}
1593
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001594static int sample_conv_str2upper(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brun107ca302010-01-04 16:16:05 +01001595{
1596 int i;
1597
Willy Tarreauf0645dc2016-08-09 14:29:38 +02001598 if (!smp_make_rw(smp))
Emeric Brun485479d2010-09-23 18:02:19 +02001599 return 0;
1600
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001601 for (i = 0; i < smp->data.u.str.len; i++) {
1602 if ((smp->data.u.str.str[i] >= 'a') && (smp->data.u.str.str[i] <= 'z'))
1603 smp->data.u.str.str[i] += 'A' - 'a';
Emeric Brun107ca302010-01-04 16:16:05 +01001604 }
1605 return 1;
1606}
1607
Willy Tarreauf9954102012-04-20 14:03:29 +02001608/* takes the netmask in arg_p */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001609static int sample_conv_ipmask(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud31d6eb2010-01-26 18:01:41 +01001610{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001611 smp->data.u.ipv4.s_addr &= arg_p->data.ipv4.s_addr;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001612 smp->data.type = SMP_T_IPV4;
Willy Tarreaud31d6eb2010-01-26 18:01:41 +01001613 return 1;
1614}
1615
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001616/* takes an UINT value on input supposed to represent the time since EPOCH,
1617 * adds an optional offset found in args[1] and emits a string representing
1618 * the local time in the format specified in args[1] using strftime().
1619 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001620static int sample_conv_ltime(const struct arg *args, struct sample *smp, void *private)
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001621{
1622 struct chunk *temp;
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001623 /* With high numbers, the date returned can be negative, the 55 bits mask prevent this. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001624 time_t curr_date = smp->data.u.sint & 0x007fffffffffffffLL;
Thierry FOURNIERfac9ccf2015-07-08 00:15:20 +02001625 struct tm *tm;
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001626
1627 /* add offset */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001628 if (args[1].type == ARGT_SINT)
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001629 curr_date += args[1].data.sint;
1630
Thierry FOURNIERfac9ccf2015-07-08 00:15:20 +02001631 tm = localtime(&curr_date);
1632 if (!tm)
1633 return 0;
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001634 temp = get_trash_chunk();
Thierry FOURNIERfac9ccf2015-07-08 00:15:20 +02001635 temp->len = strftime(temp->str, temp->size, args[0].data.str.str, tm);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001636 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001637 smp->data.type = SMP_T_STR;
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001638 return 1;
1639}
1640
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001641/* hashes the binary input into a 32-bit unsigned int */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001642static int sample_conv_sdbm(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001643{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001644 smp->data.u.sint = hash_sdbm(smp->data.u.str.str, smp->data.u.str.len);
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001645 if (arg_p && arg_p->data.sint)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001646 smp->data.u.sint = full_hash(smp->data.u.sint);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001647 smp->data.type = SMP_T_SINT;
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001648 return 1;
1649}
1650
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001651/* takes an UINT value on input supposed to represent the time since EPOCH,
1652 * adds an optional offset found in args[1] and emits a string representing
1653 * the UTC date in the format specified in args[1] using strftime().
1654 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001655static int sample_conv_utime(const struct arg *args, struct sample *smp, void *private)
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001656{
1657 struct chunk *temp;
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001658 /* With high numbers, the date returned can be negative, the 55 bits mask prevent this. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001659 time_t curr_date = smp->data.u.sint & 0x007fffffffffffffLL;
Thierry FOURNIERfac9ccf2015-07-08 00:15:20 +02001660 struct tm *tm;
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001661
1662 /* add offset */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001663 if (args[1].type == ARGT_SINT)
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001664 curr_date += args[1].data.sint;
1665
Thierry FOURNIERfac9ccf2015-07-08 00:15:20 +02001666 tm = gmtime(&curr_date);
1667 if (!tm)
1668 return 0;
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001669 temp = get_trash_chunk();
Thierry FOURNIERfac9ccf2015-07-08 00:15:20 +02001670 temp->len = strftime(temp->str, temp->size, args[0].data.str.str, tm);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001671 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001672 smp->data.type = SMP_T_STR;
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001673 return 1;
1674}
1675
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001676/* hashes the binary input into a 32-bit unsigned int */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001677static int sample_conv_wt6(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001678{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001679 smp->data.u.sint = hash_wt6(smp->data.u.str.str, smp->data.u.str.len);
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001680 if (arg_p && arg_p->data.sint)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001681 smp->data.u.sint = full_hash(smp->data.u.sint);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001682 smp->data.type = SMP_T_SINT;
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001683 return 1;
1684}
1685
Thierry FOURNIER01e09742016-12-26 11:46:11 +01001686/* hashes the binary input into a 32-bit unsigned int using xxh.
1687 * The seed of the hash defaults to 0 but can be changd in argument 1.
1688 */
1689static int sample_conv_xxh32(const struct arg *arg_p, struct sample *smp, void *private)
1690{
1691 unsigned int seed;
1692
1693 if (arg_p && arg_p->data.sint)
1694 seed = arg_p->data.sint;
1695 else
1696 seed = 0;
1697 smp->data.u.sint = XXH32(smp->data.u.str.str, smp->data.u.str.len, seed);
1698 smp->data.type = SMP_T_SINT;
1699 return 1;
1700}
1701
1702/* hashes the binary input into a 64-bit unsigned int using xxh.
1703 * In fact, the function returns a 64 bit unsigned, but the sample
1704 * storage of haproxy only proposes 64-bits signed, so the value is
1705 * cast as signed. This cast doesn't impact the hash repartition.
1706 * The seed of the hash defaults to 0 but can be changd in argument 1.
1707 */
1708static int sample_conv_xxh64(const struct arg *arg_p, struct sample *smp, void *private)
1709{
1710 unsigned long long int seed;
1711
1712 if (arg_p && arg_p->data.sint)
1713 seed = (unsigned long long int)arg_p->data.sint;
1714 else
1715 seed = 0;
1716 smp->data.u.sint = (long long int)XXH64(smp->data.u.str.str, smp->data.u.str.len, seed);
1717 smp->data.type = SMP_T_SINT;
1718 return 1;
1719}
1720
Willy Tarreau80599772015-01-20 19:35:24 +01001721/* hashes the binary input into a 32-bit unsigned int */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001722static int sample_conv_crc32(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau80599772015-01-20 19:35:24 +01001723{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001724 smp->data.u.sint = hash_crc32(smp->data.u.str.str, smp->data.u.str.len);
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001725 if (arg_p && arg_p->data.sint)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001726 smp->data.u.sint = full_hash(smp->data.u.sint);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001727 smp->data.type = SMP_T_SINT;
Willy Tarreau80599772015-01-20 19:35:24 +01001728 return 1;
1729}
1730
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001731/* This function escape special json characters. The returned string can be
1732 * safely set between two '"' and used as json string. The json string is
1733 * defined like this:
1734 *
1735 * any Unicode character except '"' or '\' or control character
1736 * \", \\, \/, \b, \f, \n, \r, \t, \u + four-hex-digits
1737 *
1738 * The enum input_type contain all the allowed mode for decoding the input
1739 * string.
1740 */
1741enum input_type {
1742 IT_ASCII = 0,
1743 IT_UTF8,
1744 IT_UTF8S,
1745 IT_UTF8P,
1746 IT_UTF8PS,
1747};
1748static int sample_conv_json_check(struct arg *arg, struct sample_conv *conv,
1749 const char *file, int line, char **err)
1750{
1751 if (!arg) {
1752 memprintf(err, "Unexpected empty arg list");
1753 return 0;
1754 }
1755
1756 if (arg->type != ARGT_STR) {
1757 memprintf(err, "Unexpected arg type");
1758 return 0;
1759 }
1760
1761 if (strcmp(arg->data.str.str, "") == 0) {
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001762 arg->type = ARGT_SINT;
1763 arg->data.sint = IT_ASCII;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001764 return 1;
1765 }
1766
1767 else if (strcmp(arg->data.str.str, "ascii") == 0) {
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001768 arg->type = ARGT_SINT;
1769 arg->data.sint = IT_ASCII;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001770 return 1;
1771 }
1772
1773 else if (strcmp(arg->data.str.str, "utf8") == 0) {
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001774 arg->type = ARGT_SINT;
1775 arg->data.sint = IT_UTF8;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001776 return 1;
1777 }
1778
1779 else if (strcmp(arg->data.str.str, "utf8s") == 0) {
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001780 arg->type = ARGT_SINT;
1781 arg->data.sint = IT_UTF8S;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001782 return 1;
1783 }
1784
1785 else if (strcmp(arg->data.str.str, "utf8p") == 0) {
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001786 arg->type = ARGT_SINT;
1787 arg->data.sint = IT_UTF8P;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001788 return 1;
1789 }
1790
1791 else if (strcmp(arg->data.str.str, "utf8ps") == 0) {
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001792 arg->type = ARGT_SINT;
1793 arg->data.sint = IT_UTF8PS;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001794 return 1;
1795 }
1796
Thierry FOURNIER / OZON.IOa69c9122016-11-23 01:13:57 +01001797 memprintf(err, "Unexpected input code type. "
1798 "Allowed value are 'ascii', 'utf8', 'utf8s', 'utf8p' and 'utf8ps'");
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001799 return 0;
1800}
1801
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001802static int sample_conv_json(const struct arg *arg_p, struct sample *smp, void *private)
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001803{
1804 struct chunk *temp;
1805 char _str[7]; /* \u + 4 hex digit + null char for sprintf. */
1806 const char *str;
1807 int len;
1808 enum input_type input_type = IT_ASCII;
1809 unsigned int c;
1810 unsigned int ret;
1811 char *p;
1812
1813 if (arg_p)
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001814 input_type = arg_p->data.sint;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001815
1816 temp = get_trash_chunk();
1817 temp->len = 0;
1818
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001819 p = smp->data.u.str.str;
1820 while (p < smp->data.u.str.str + smp->data.u.str.len) {
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001821
1822 if (input_type == IT_ASCII) {
1823 /* Read input as ASCII. */
1824 c = *(unsigned char *)p;
1825 p++;
1826 }
1827 else {
1828 /* Read input as UTF8. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001829 ret = utf8_next(p, smp->data.u.str.len - ( p - smp->data.u.str.str ), &c);
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001830 p += utf8_return_length(ret);
1831
1832 if (input_type == IT_UTF8 && utf8_return_code(ret) != UTF8_CODE_OK)
1833 return 0;
1834 if (input_type == IT_UTF8S && utf8_return_code(ret) != UTF8_CODE_OK)
1835 continue;
1836 if (input_type == IT_UTF8P && utf8_return_code(ret) & (UTF8_CODE_INVRANGE|UTF8_CODE_BADSEQ))
1837 return 0;
1838 if (input_type == IT_UTF8PS && utf8_return_code(ret) & (UTF8_CODE_INVRANGE|UTF8_CODE_BADSEQ))
1839 continue;
1840
1841 /* Check too big values. */
1842 if ((unsigned int)c > 0xffff) {
1843 if (input_type == IT_UTF8 || input_type == IT_UTF8P)
1844 return 0;
1845 continue;
1846 }
1847 }
1848
1849 /* Convert character. */
1850 if (c == '"') {
1851 len = 2;
1852 str = "\\\"";
1853 }
1854 else if (c == '\\') {
1855 len = 2;
1856 str = "\\\\";
1857 }
1858 else if (c == '/') {
1859 len = 2;
1860 str = "\\/";
1861 }
1862 else if (c == '\b') {
1863 len = 2;
1864 str = "\\b";
1865 }
1866 else if (c == '\f') {
1867 len = 2;
1868 str = "\\f";
1869 }
1870 else if (c == '\r') {
1871 len = 2;
1872 str = "\\r";
1873 }
1874 else if (c == '\n') {
1875 len = 2;
1876 str = "\\n";
1877 }
1878 else if (c == '\t') {
1879 len = 2;
1880 str = "\\t";
1881 }
1882 else if (c > 0xff || !isprint(c)) {
1883 /* isprint generate a segfault if c is too big. The man says that
1884 * c must have the value of an unsigned char or EOF.
1885 */
1886 len = 6;
1887 _str[0] = '\\';
1888 _str[1] = 'u';
1889 snprintf(&_str[2], 5, "%04x", (unsigned short)c);
1890 str = _str;
1891 }
1892 else {
1893 len = 1;
1894 str = (char *)&c;
1895 }
1896
1897 /* Check length */
1898 if (temp->len + len > temp->size)
1899 return 0;
1900
1901 /* Copy string. */
1902 memcpy(temp->str + temp->len, str, len);
1903 temp->len += len;
1904 }
1905
1906 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001907 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001908 smp->data.type = SMP_T_STR;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001909
1910 return 1;
1911}
1912
Emeric Brun54c4ac82014-11-03 15:32:43 +01001913/* This sample function is designed to extract some bytes from an input buffer.
1914 * First arg is the offset.
1915 * Optional second arg is the length to truncate */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001916static int sample_conv_bytes(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brun54c4ac82014-11-03 15:32:43 +01001917{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001918 if (smp->data.u.str.len <= arg_p[0].data.sint) {
1919 smp->data.u.str.len = 0;
Emeric Brun54c4ac82014-11-03 15:32:43 +01001920 return 1;
1921 }
1922
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001923 if (smp->data.u.str.size)
1924 smp->data.u.str.size -= arg_p[0].data.sint;
1925 smp->data.u.str.len -= arg_p[0].data.sint;
1926 smp->data.u.str.str += arg_p[0].data.sint;
Emeric Brun54c4ac82014-11-03 15:32:43 +01001927
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001928 if ((arg_p[1].type == ARGT_SINT) && (arg_p[1].data.sint < smp->data.u.str.len))
1929 smp->data.u.str.len = arg_p[1].data.sint;
Emeric Brun54c4ac82014-11-03 15:32:43 +01001930
1931 return 1;
1932}
1933
Emeric Brunf399b0d2014-11-03 17:07:03 +01001934static int sample_conv_field_check(struct arg *args, struct sample_conv *conv,
1935 const char *file, int line, char **err)
1936{
1937 struct arg *arg = args;
1938
1939 if (!arg) {
1940 memprintf(err, "Unexpected empty arg list");
1941 return 0;
1942 }
1943
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001944 if (arg->type != ARGT_SINT) {
Emeric Brunf399b0d2014-11-03 17:07:03 +01001945 memprintf(err, "Unexpected arg type");
1946 return 0;
1947 }
1948
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001949 if (!arg->data.sint) {
Emeric Brunf399b0d2014-11-03 17:07:03 +01001950 memprintf(err, "Unexpected value 0 for index");
1951 return 0;
1952 }
1953
1954 arg++;
1955
1956 if (arg->type != ARGT_STR) {
1957 memprintf(err, "Unexpected arg type");
1958 return 0;
1959 }
1960
1961 if (!arg->data.str.len) {
1962 memprintf(err, "Empty separators list");
1963 return 0;
1964 }
1965
1966 return 1;
1967}
1968
1969/* This sample function is designed to a return selected part of a string (field).
1970 * First arg is the index of the field (start at 1)
1971 * Second arg is a char list of separators (type string)
1972 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001973static int sample_conv_field(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brunf399b0d2014-11-03 17:07:03 +01001974{
1975 unsigned int field;
1976 char *start, *end;
1977 int i;
1978
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001979 if (!arg_p[0].data.sint)
Emeric Brunf399b0d2014-11-03 17:07:03 +01001980 return 0;
1981
1982 field = 1;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001983 end = start = smp->data.u.str.str;
1984 while (end - smp->data.u.str.str < smp->data.u.str.len) {
Emeric Brunf399b0d2014-11-03 17:07:03 +01001985
1986 for (i = 0 ; i < arg_p[1].data.str.len ; i++) {
1987 if (*end == arg_p[1].data.str.str[i]) {
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001988 if (field == arg_p[0].data.sint)
Emeric Brunf399b0d2014-11-03 17:07:03 +01001989 goto found;
1990 start = end+1;
1991 field++;
1992 break;
1993 }
1994 }
1995 end++;
1996 }
1997
1998 /* Field not found */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001999 if (field != arg_p[0].data.sint) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002000 smp->data.u.str.len = 0;
Emeric Brunf399b0d2014-11-03 17:07:03 +01002001 return 1;
2002 }
2003found:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002004 smp->data.u.str.len = end - start;
Emeric Brunf399b0d2014-11-03 17:07:03 +01002005 /* If ret string is len 0, no need to
2006 change pointers or to update size */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002007 if (!smp->data.u.str.len)
Emeric Brunf399b0d2014-11-03 17:07:03 +01002008 return 1;
2009
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002010 smp->data.u.str.str = start;
Emeric Brunf399b0d2014-11-03 17:07:03 +01002011
2012 /* Compute remaining size if needed
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002013 Note: smp->data.u.str.size cannot be set to 0 */
2014 if (smp->data.u.str.size)
2015 smp->data.u.str.size -= start - smp->data.u.str.str;
Emeric Brunf399b0d2014-11-03 17:07:03 +01002016
2017 return 1;
2018}
2019
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002020/* This sample function is designed to return a word from a string.
2021 * First arg is the index of the word (start at 1)
2022 * Second arg is a char list of words separators (type string)
2023 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002024static int sample_conv_word(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002025{
2026 unsigned int word;
2027 char *start, *end;
2028 int i, issep, inword;
2029
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002030 if (!arg_p[0].data.sint)
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002031 return 0;
2032
2033 word = 0;
2034 inword = 0;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002035 end = start = smp->data.u.str.str;
2036 while (end - smp->data.u.str.str < smp->data.u.str.len) {
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002037 issep = 0;
2038 for (i = 0 ; i < arg_p[1].data.str.len ; i++) {
2039 if (*end == arg_p[1].data.str.str[i]) {
2040 issep = 1;
2041 break;
2042 }
2043 }
2044 if (!inword) {
2045 if (!issep) {
2046 word++;
2047 start = end;
2048 inword = 1;
2049 }
2050 }
2051 else if (issep) {
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002052 if (word == arg_p[0].data.sint)
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002053 goto found;
2054 inword = 0;
2055 }
2056 end++;
2057 }
2058
2059 /* Field not found */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002060 if (word != arg_p[0].data.sint) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002061 smp->data.u.str.len = 0;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002062 return 1;
2063 }
2064found:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002065 smp->data.u.str.len = end - start;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002066 /* If ret string is len 0, no need to
2067 change pointers or to update size */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002068 if (!smp->data.u.str.len)
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002069 return 1;
2070
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002071 smp->data.u.str.str = start;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002072
2073 /* Compute remaining size if needed
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002074 Note: smp->data.u.str.size cannot be set to 0 */
2075 if (smp->data.u.str.size)
2076 smp->data.u.str.size -= start - smp->data.u.str.str;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002077
2078 return 1;
2079}
2080
Willy Tarreau7eda8492015-01-20 19:47:06 +01002081static int sample_conv_regsub_check(struct arg *args, struct sample_conv *conv,
2082 const char *file, int line, char **err)
2083{
2084 struct arg *arg = args;
2085 char *p;
2086 int len;
2087
2088 /* arg0 is a regex, it uses type_flag for ICASE and global match */
2089 arg[0].type_flags = 0;
2090
2091 if (arg[2].type != ARGT_STR)
2092 return 1;
2093
2094 p = arg[2].data.str.str;
2095 len = arg[2].data.str.len;
2096 while (len) {
2097 if (*p == 'i') {
2098 arg[0].type_flags |= ARGF_REG_ICASE;
2099 }
2100 else if (*p == 'g') {
2101 arg[0].type_flags |= ARGF_REG_GLOB;
2102 }
2103 else {
2104 memprintf(err, "invalid regex flag '%c', only 'i' and 'g' are supported", *p);
2105 return 0;
2106 }
2107 p++;
2108 len--;
2109 }
2110 return 1;
2111}
2112
2113/* This sample function is designed to do the equivalent of s/match/replace/ on
2114 * the input string. It applies a regex and restarts from the last matched
2115 * location until nothing matches anymore. First arg is the regex to apply to
2116 * the input string, second arg is the replacement expression.
2117 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002118static int sample_conv_regsub(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau7eda8492015-01-20 19:47:06 +01002119{
2120 char *start, *end;
2121 struct my_regex *reg = arg_p[0].data.reg;
2122 regmatch_t pmatch[MAX_MATCH];
2123 struct chunk *trash = get_trash_chunk();
2124 int flag, max;
2125 int found;
2126
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002127 start = smp->data.u.str.str;
2128 end = start + smp->data.u.str.len;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002129
2130 flag = 0;
2131 while (1) {
2132 /* check for last round which is used to copy remaining parts
2133 * when not running in global replacement mode.
2134 */
2135 found = 0;
2136 if ((arg_p[0].type_flags & ARGF_REG_GLOB) || !(flag & REG_NOTBOL)) {
2137 /* Note: we can have start == end on empty strings or at the end */
2138 found = regex_exec_match2(reg, start, end - start, MAX_MATCH, pmatch, flag);
2139 }
2140
2141 if (!found)
2142 pmatch[0].rm_so = end - start;
2143
2144 /* copy the heading non-matching part (which may also be the tail if nothing matches) */
2145 max = trash->size - trash->len;
2146 if (max && pmatch[0].rm_so > 0) {
2147 if (max > pmatch[0].rm_so)
2148 max = pmatch[0].rm_so;
2149 memcpy(trash->str + trash->len, start, max);
2150 trash->len += max;
2151 }
2152
2153 if (!found)
2154 break;
2155
2156 /* replace the matching part */
2157 max = trash->size - trash->len;
2158 if (max) {
2159 if (max > arg_p[1].data.str.len)
2160 max = arg_p[1].data.str.len;
2161 memcpy(trash->str + trash->len, arg_p[1].data.str.str, max);
2162 trash->len += max;
2163 }
2164
2165 /* stop here if we're done with this string */
2166 if (start >= end)
2167 break;
2168
2169 /* We have a special case for matches of length 0 (eg: "x*y*").
2170 * These ones are considered to match in front of a character,
2171 * so we have to copy that character and skip to the next one.
2172 */
2173 if (!pmatch[0].rm_eo) {
2174 if (trash->len < trash->size)
2175 trash->str[trash->len++] = start[pmatch[0].rm_eo];
2176 pmatch[0].rm_eo++;
2177 }
2178
2179 start += pmatch[0].rm_eo;
2180 flag |= REG_NOTBOL;
2181 }
2182
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002183 smp->data.u.str = *trash;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002184 return 1;
2185}
2186
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002187/* This function check an operator entry. It expects a string.
2188 * The string can be an integer or a variable name.
2189 */
2190static int check_operator(struct arg *args, struct sample_conv *conv,
2191 const char *file, int line, char **err)
2192{
2193 const char *str;
2194 const char *end;
2195
2196 /* Try to decode a variable. */
2197 if (vars_check_arg(&args[0], NULL))
2198 return 1;
2199
2200 /* Try to convert an integer */
2201 str = args[0].data.str.str;
2202 end = str + strlen(str);
2203 args[0].data.sint = read_int64(&str, end);
2204 if (*str != '\0') {
2205 memprintf(err, "expects an integer or a variable name");
2206 return 0;
2207 }
2208 args[0].type = ARGT_SINT;
2209 return 1;
2210}
2211
Willy Tarreau6204cd92016-03-10 16:33:04 +01002212/* This function returns a sample struct filled with an arg content.
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002213 * If the arg contain an integer, the integer is returned in the
2214 * sample. If the arg contains a variable descriptor, it returns the
2215 * variable value.
2216 *
2217 * This function returns 0 if an error occurs, otherwise it returns 1.
2218 */
Willy Tarreau6204cd92016-03-10 16:33:04 +01002219static inline int sample_conv_var2smp(const struct arg *arg, struct sample *smp)
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002220{
2221 switch (arg->type) {
2222 case ARGT_SINT:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002223 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002224 smp->data.u.sint = arg->data.sint;
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002225 return 1;
2226 case ARGT_VAR:
Willy Tarreau6204cd92016-03-10 16:33:04 +01002227 if (!vars_get_by_desc(&arg->data.var, smp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002228 return 0;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002229 if (!sample_casts[smp->data.type][SMP_T_SINT])
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002230 return 0;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002231 if (!sample_casts[smp->data.type][SMP_T_SINT](smp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002232 return 0;
2233 return 1;
2234 default:
2235 return 0;
2236 }
2237}
2238
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002239/* Takes a SINT on input, applies a binary twos complement and returns the SINT
Willy Tarreau97707872015-01-27 15:12:13 +01002240 * result.
2241 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002242static int sample_conv_binary_cpl(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002243{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002244 smp->data.u.sint = ~smp->data.u.sint;
Willy Tarreau97707872015-01-27 15:12:13 +01002245 return 1;
2246}
2247
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002248/* Takes a SINT on input, applies a binary "and" with the SINT directly in
2249 * arg_p or in the varaible described in arg_p, and returns the SINT result.
Willy Tarreau97707872015-01-27 15:12:13 +01002250 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002251static int sample_conv_binary_and(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002252{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002253 struct sample tmp;
2254
Willy Tarreau7560dd42016-03-10 16:28:58 +01002255 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreau6204cd92016-03-10 16:33:04 +01002256 if (!sample_conv_var2smp(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002257 return 0;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002258 smp->data.u.sint &= tmp.data.u.sint;
Willy Tarreau97707872015-01-27 15:12:13 +01002259 return 1;
2260}
2261
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002262/* Takes a SINT on input, applies a binary "or" with the SINT directly in
2263 * arg_p or in the varaible described in arg_p, and returns the SINT result.
Willy Tarreau97707872015-01-27 15:12:13 +01002264 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002265static int sample_conv_binary_or(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002266{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002267 struct sample tmp;
2268
Willy Tarreau7560dd42016-03-10 16:28:58 +01002269 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreau6204cd92016-03-10 16:33:04 +01002270 if (!sample_conv_var2smp(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002271 return 0;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002272 smp->data.u.sint |= tmp.data.u.sint;
Willy Tarreau97707872015-01-27 15:12:13 +01002273 return 1;
2274}
2275
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002276/* Takes a SINT on input, applies a binary "xor" with the SINT directly in
2277 * arg_p or in the varaible described in arg_p, and returns the SINT result.
Willy Tarreau97707872015-01-27 15:12:13 +01002278 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002279static int sample_conv_binary_xor(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002280{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002281 struct sample tmp;
2282
Willy Tarreau7560dd42016-03-10 16:28:58 +01002283 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreau6204cd92016-03-10 16:33:04 +01002284 if (!sample_conv_var2smp(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002285 return 0;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002286 smp->data.u.sint ^= tmp.data.u.sint;
Willy Tarreau97707872015-01-27 15:12:13 +01002287 return 1;
2288}
2289
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002290static inline long long int arith_add(long long int a, long long int b)
2291{
2292 /* Prevent overflow and makes capped calculus.
2293 * We must ensure that the check calculus doesn't
2294 * exceed the signed 64 bits limits.
2295 *
2296 * +----------+----------+
2297 * | a<0 | a>=0 |
2298 * +------+----------+----------+
2299 * | b<0 | MIN-a>b | no check |
2300 * +------+----------+----------+
2301 * | b>=0 | no check | MAX-a<b |
2302 * +------+----------+----------+
2303 */
2304 if ((a ^ b) >= 0) {
2305 /* signs are differents. */
2306 if (a < 0) {
2307 if (LLONG_MIN - a > b)
2308 return LLONG_MIN;
2309 }
2310 if (LLONG_MAX - a < b)
2311 return LLONG_MAX;
2312 }
2313 return a + b;
2314}
2315
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002316/* Takes a SINT on input, applies an arithmetic "add" with the SINT directly in
2317 * arg_p or in the varaible described in arg_p, and returns the SINT result.
Willy Tarreau97707872015-01-27 15:12:13 +01002318 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002319static int sample_conv_arith_add(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002320{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002321 struct sample tmp;
2322
Willy Tarreau7560dd42016-03-10 16:28:58 +01002323 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreau6204cd92016-03-10 16:33:04 +01002324 if (!sample_conv_var2smp(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002325 return 0;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002326 smp->data.u.sint = arith_add(smp->data.u.sint, tmp.data.u.sint);
Willy Tarreau97707872015-01-27 15:12:13 +01002327 return 1;
2328}
2329
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002330/* Takes a SINT on input, applies an arithmetic "sub" with the SINT directly in
2331 * arg_p or in the varaible described in arg_p, and returns the SINT result.
Willy Tarreau97707872015-01-27 15:12:13 +01002332 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002333static int sample_conv_arith_sub(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002334 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002335{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002336 struct sample tmp;
2337
Willy Tarreau7560dd42016-03-10 16:28:58 +01002338 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreau6204cd92016-03-10 16:33:04 +01002339 if (!sample_conv_var2smp(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002340 return 0;
2341
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002342 /* We cannot represent -LLONG_MIN because abs(LLONG_MIN) is greater
2343 * than abs(LLONG_MAX). So, the following code use LLONG_MAX in place
2344 * of -LLONG_MIN and correct the result.
2345 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002346 if (tmp.data.u.sint == LLONG_MIN) {
2347 smp->data.u.sint = arith_add(smp->data.u.sint, LLONG_MAX);
2348 if (smp->data.u.sint < LLONG_MAX)
2349 smp->data.u.sint++;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002350 return 1;
2351 }
2352
2353 /* standard substraction: we use the "add" function and negate
2354 * the second operand.
2355 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002356 smp->data.u.sint = arith_add(smp->data.u.sint, -tmp.data.u.sint);
Willy Tarreau97707872015-01-27 15:12:13 +01002357 return 1;
2358}
2359
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002360/* Takes a SINT on input, applies an arithmetic "mul" with the SINT directly in
2361 * arg_p or in the varaible described in arg_p, and returns the SINT result.
2362 * If the result makes an overflow, then the largest possible quantity is
2363 * returned.
Willy Tarreau97707872015-01-27 15:12:13 +01002364 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002365static int sample_conv_arith_mul(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002366 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002367{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002368 struct sample tmp;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002369 long long int c;
2370
Willy Tarreau7560dd42016-03-10 16:28:58 +01002371 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreau6204cd92016-03-10 16:33:04 +01002372 if (!sample_conv_var2smp(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002373 return 0;
2374
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002375 /* prevent divide by 0 during the check */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002376 if (!smp->data.u.sint || !tmp.data.u.sint) {
2377 smp->data.u.sint = 0;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002378 return 1;
2379 }
2380
2381 /* The multiply between LLONG_MIN and -1 returns a
2382 * "floting point exception".
2383 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002384 if (smp->data.u.sint == LLONG_MIN && tmp.data.u.sint == -1) {
2385 smp->data.u.sint = LLONG_MAX;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002386 return 1;
2387 }
2388
2389 /* execute standard multiplication. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002390 c = smp->data.u.sint * tmp.data.u.sint;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002391
2392 /* check for overflow and makes capped multiply. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002393 if (smp->data.u.sint != c / tmp.data.u.sint) {
2394 if ((smp->data.u.sint < 0) == (tmp.data.u.sint < 0)) {
2395 smp->data.u.sint = LLONG_MAX;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002396 return 1;
2397 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002398 smp->data.u.sint = LLONG_MIN;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002399 return 1;
2400 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002401 smp->data.u.sint = c;
Willy Tarreau97707872015-01-27 15:12:13 +01002402 return 1;
2403}
2404
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002405/* Takes a SINT on input, applies an arithmetic "div" with the SINT directly in
2406 * arg_p or in the varaible described in arg_p, and returns the SINT result.
2407 * If arg_p makes the result overflow, then the largest possible quantity is
2408 * returned.
Willy Tarreau97707872015-01-27 15:12:13 +01002409 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002410static int sample_conv_arith_div(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002411 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002412{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002413 struct sample tmp;
2414
Willy Tarreau7560dd42016-03-10 16:28:58 +01002415 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreau6204cd92016-03-10 16:33:04 +01002416 if (!sample_conv_var2smp(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002417 return 0;
2418
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002419 if (tmp.data.u.sint) {
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002420 /* The divide between LLONG_MIN and -1 returns a
2421 * "floting point exception".
2422 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002423 if (smp->data.u.sint == LLONG_MIN && tmp.data.u.sint == -1) {
2424 smp->data.u.sint = LLONG_MAX;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002425 return 1;
2426 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002427 smp->data.u.sint /= tmp.data.u.sint;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002428 return 1;
2429 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002430 smp->data.u.sint = LLONG_MAX;
Willy Tarreau97707872015-01-27 15:12:13 +01002431 return 1;
2432}
2433
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002434/* Takes a SINT on input, applies an arithmetic "mod" with the SINT directly in
2435 * arg_p or in the varaible described in arg_p, and returns the SINT result.
2436 * If arg_p makes the result overflow, then 0 is returned.
Willy Tarreau97707872015-01-27 15:12:13 +01002437 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002438static int sample_conv_arith_mod(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002439 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002440{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002441 struct sample tmp;
2442
Willy Tarreau7560dd42016-03-10 16:28:58 +01002443 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreau6204cd92016-03-10 16:33:04 +01002444 if (!sample_conv_var2smp(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002445 return 0;
2446
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002447 if (tmp.data.u.sint) {
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002448 /* The divide between LLONG_MIN and -1 returns a
2449 * "floting point exception".
2450 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002451 if (smp->data.u.sint == LLONG_MIN && tmp.data.u.sint == -1) {
2452 smp->data.u.sint = 0;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002453 return 1;
2454 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002455 smp->data.u.sint %= tmp.data.u.sint;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002456 return 1;
2457 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002458 smp->data.u.sint = 0;
Willy Tarreau97707872015-01-27 15:12:13 +01002459 return 1;
2460}
2461
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002462/* Takes an SINT on input, applies an arithmetic "neg" and returns the SINT
Willy Tarreau97707872015-01-27 15:12:13 +01002463 * result.
2464 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002465static int sample_conv_arith_neg(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002466 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002467{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002468 if (smp->data.u.sint == LLONG_MIN)
2469 smp->data.u.sint = LLONG_MAX;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002470 else
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002471 smp->data.u.sint = -smp->data.u.sint;
Willy Tarreau97707872015-01-27 15:12:13 +01002472 return 1;
2473}
2474
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002475/* Takes a SINT on input, returns true is the value is non-null, otherwise
Willy Tarreau97707872015-01-27 15:12:13 +01002476 * false. The output is a BOOL.
2477 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002478static int sample_conv_arith_bool(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002479 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002480{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002481 smp->data.u.sint = !!smp->data.u.sint;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002482 smp->data.type = SMP_T_BOOL;
Willy Tarreau97707872015-01-27 15:12:13 +01002483 return 1;
2484}
2485
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002486/* Takes a SINT on input, returns false is the value is non-null, otherwise
Willy Tarreau97707872015-01-27 15:12:13 +01002487 * truee. The output is a BOOL.
2488 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002489static int sample_conv_arith_not(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002490 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002491{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002492 smp->data.u.sint = !smp->data.u.sint;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002493 smp->data.type = SMP_T_BOOL;
Willy Tarreau97707872015-01-27 15:12:13 +01002494 return 1;
2495}
2496
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002497/* Takes a SINT on input, returns true is the value is odd, otherwise false.
Willy Tarreau97707872015-01-27 15:12:13 +01002498 * The output is a BOOL.
2499 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002500static int sample_conv_arith_odd(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002501 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002502{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002503 smp->data.u.sint = smp->data.u.sint & 1;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002504 smp->data.type = SMP_T_BOOL;
Willy Tarreau97707872015-01-27 15:12:13 +01002505 return 1;
2506}
2507
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002508/* Takes a SINT on input, returns true is the value is even, otherwise false.
Willy Tarreau97707872015-01-27 15:12:13 +01002509 * The output is a BOOL.
2510 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002511static int sample_conv_arith_even(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002512 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002513{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002514 smp->data.u.sint = !(smp->data.u.sint & 1);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002515 smp->data.type = SMP_T_BOOL;
Willy Tarreau97707872015-01-27 15:12:13 +01002516 return 1;
2517}
2518
Willy Tarreau5b8ad222013-07-25 12:17:57 +02002519/************************************************************************/
2520/* All supported sample fetch functions must be declared here */
2521/************************************************************************/
2522
2523/* force TRUE to be returned at the fetch level */
2524static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002525smp_fetch_true(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau5b8ad222013-07-25 12:17:57 +02002526{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002527 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002528 smp->data.u.sint = 1;
Willy Tarreau5b8ad222013-07-25 12:17:57 +02002529 return 1;
2530}
2531
2532/* force FALSE to be returned at the fetch level */
2533static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002534smp_fetch_false(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau5b8ad222013-07-25 12:17:57 +02002535{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002536 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002537 smp->data.u.sint = 0;
Willy Tarreau5b8ad222013-07-25 12:17:57 +02002538 return 1;
2539}
2540
2541/* retrieve environment variable $1 as a string */
2542static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002543smp_fetch_env(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau5b8ad222013-07-25 12:17:57 +02002544{
2545 char *env;
2546
2547 if (!args || args[0].type != ARGT_STR)
2548 return 0;
2549
2550 env = getenv(args[0].data.str.str);
2551 if (!env)
2552 return 0;
2553
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002554 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01002555 smp->flags = SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002556 smp->data.u.str.str = env;
2557 smp->data.u.str.len = strlen(env);
Willy Tarreau5b8ad222013-07-25 12:17:57 +02002558 return 1;
2559}
2560
Willy Tarreau6236d3a2013-07-25 14:28:25 +02002561/* retrieve the current local date in epoch time, and applies an optional offset
2562 * of args[0] seconds.
2563 */
2564static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002565smp_fetch_date(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau6236d3a2013-07-25 14:28:25 +02002566{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002567 smp->data.u.sint = date.tv_sec;
Willy Tarreau6236d3a2013-07-25 14:28:25 +02002568
2569 /* add offset */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002570 if (args && args[0].type == ARGT_SINT)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002571 smp->data.u.sint += args[0].data.sint;
Willy Tarreau6236d3a2013-07-25 14:28:25 +02002572
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002573 smp->data.type = SMP_T_SINT;
Willy Tarreau6236d3a2013-07-25 14:28:25 +02002574 smp->flags |= SMP_F_VOL_TEST | SMP_F_MAY_CHANGE;
2575 return 1;
2576}
2577
Nenad Merdanovic807a6e72017-03-12 22:00:00 +01002578/* returns the hostname */
2579static int
2580smp_fetch_hostname(const struct arg *args, struct sample *smp, const char *kw, void *private)
2581{
2582 smp->data.type = SMP_T_STR;
2583 smp->flags = SMP_F_CONST;
2584 smp->data.u.str.str = hostname;
2585 smp->data.u.str.len = strlen(hostname);
2586 return 1;
2587}
2588
Willy Tarreau0f30d262014-11-24 16:02:05 +01002589/* returns the number of processes */
2590static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002591smp_fetch_nbproc(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau0f30d262014-11-24 16:02:05 +01002592{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002593 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002594 smp->data.u.sint = global.nbproc;
Willy Tarreau0f30d262014-11-24 16:02:05 +01002595 return 1;
2596}
2597
2598/* returns the number of the current process (between 1 and nbproc */
2599static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002600smp_fetch_proc(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau0f30d262014-11-24 16:02:05 +01002601{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002602 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002603 smp->data.u.sint = relative_pid;
Willy Tarreau0f30d262014-11-24 16:02:05 +01002604 return 1;
2605}
2606
Christopher Faulet34adb2a2017-11-21 21:45:38 +01002607/* returns the number of the current thread (between 1 and nbthread */
2608static int
2609smp_fetch_thread(const struct arg *args, struct sample *smp, const char *kw, void *private)
2610{
2611 smp->data.type = SMP_T_SINT;
2612 smp->data.u.sint = tid;
2613 return 1;
2614}
2615
Willy Tarreau84310e22014-02-14 11:59:04 +01002616/* generate a random 32-bit integer for whatever purpose, with an optional
2617 * range specified in argument.
2618 */
2619static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002620smp_fetch_rand(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau84310e22014-02-14 11:59:04 +01002621{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002622 smp->data.u.sint = random();
Willy Tarreau84310e22014-02-14 11:59:04 +01002623
2624 /* reduce if needed. Don't do a modulo, use all bits! */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002625 if (args && args[0].type == ARGT_SINT)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002626 smp->data.u.sint = (smp->data.u.sint * args[0].data.sint) / ((u64)RAND_MAX+1);
Willy Tarreau84310e22014-02-14 11:59:04 +01002627
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002628 smp->data.type = SMP_T_SINT;
Willy Tarreau84310e22014-02-14 11:59:04 +01002629 smp->flags |= SMP_F_VOL_TEST | SMP_F_MAY_CHANGE;
2630 return 1;
2631}
2632
Willy Tarreau0f30d262014-11-24 16:02:05 +01002633/* returns true if the current process is stopping */
2634static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002635smp_fetch_stopping(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau0f30d262014-11-24 16:02:05 +01002636{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002637 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002638 smp->data.u.sint = stopping;
Willy Tarreau0f30d262014-11-24 16:02:05 +01002639 return 1;
2640}
2641
Thierry FOURNIERcc103292015-06-06 19:30:17 +02002642static int smp_fetch_const_str(const struct arg *args, struct sample *smp, const char *kw, void *private)
2643{
2644 smp->flags |= SMP_F_CONST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002645 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002646 smp->data.u.str.str = args[0].data.str.str;
2647 smp->data.u.str.len = args[0].data.str.len;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02002648 return 1;
2649}
2650
2651static int smp_check_const_bool(struct arg *args, char **err)
2652{
2653 if (strcasecmp(args[0].data.str.str, "true") == 0 ||
2654 strcasecmp(args[0].data.str.str, "1") == 0) {
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002655 args[0].type = ARGT_SINT;
2656 args[0].data.sint = 1;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02002657 return 1;
2658 }
2659 if (strcasecmp(args[0].data.str.str, "false") == 0 ||
2660 strcasecmp(args[0].data.str.str, "0") == 0) {
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002661 args[0].type = ARGT_SINT;
2662 args[0].data.sint = 0;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02002663 return 1;
2664 }
2665 memprintf(err, "Expects 'true', 'false', '0' or '1'");
2666 return 0;
2667}
2668
2669static int smp_fetch_const_bool(const struct arg *args, struct sample *smp, const char *kw, void *private)
2670{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002671 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002672 smp->data.u.sint = args[0].data.sint;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02002673 return 1;
2674}
2675
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002676static int smp_fetch_const_int(const struct arg *args, struct sample *smp, const char *kw, void *private)
Thierry FOURNIERcc103292015-06-06 19:30:17 +02002677{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002678 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002679 smp->data.u.sint = args[0].data.sint;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02002680 return 1;
2681}
2682
2683static int smp_fetch_const_ipv4(const struct arg *args, struct sample *smp, const char *kw, void *private)
2684{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002685 smp->data.type = SMP_T_IPV4;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002686 smp->data.u.ipv4 = args[0].data.ipv4;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02002687 return 1;
2688}
2689
2690static int smp_fetch_const_ipv6(const struct arg *args, struct sample *smp, const char *kw, void *private)
2691{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002692 smp->data.type = SMP_T_IPV6;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002693 smp->data.u.ipv6 = args[0].data.ipv6;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02002694 return 1;
2695}
2696
2697static int smp_check_const_bin(struct arg *args, char **err)
2698{
David Carlier64a16ab2016-04-08 10:37:02 +01002699 char *binstr = NULL;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02002700 int binstrlen;
2701
2702 if (!parse_binary(args[0].data.str.str, &binstr, &binstrlen, err))
2703 return 0;
2704 args[0].type = ARGT_STR;
2705 args[0].data.str.str = binstr;
2706 args[0].data.str.len = binstrlen;
2707 return 1;
2708}
2709
2710static int smp_fetch_const_bin(const struct arg *args, struct sample *smp, const char *kw, void *private)
2711{
2712 smp->flags |= SMP_F_CONST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002713 smp->data.type = SMP_T_BIN;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002714 smp->data.u.str.str = args[0].data.str.str;
2715 smp->data.u.str.len = args[0].data.str.len;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02002716 return 1;
2717}
2718
2719static int smp_check_const_meth(struct arg *args, char **err)
2720{
2721 enum http_meth_t meth;
2722 int i;
2723
2724 meth = find_http_meth(args[0].data.str.str, args[0].data.str.len);
2725 if (meth != HTTP_METH_OTHER) {
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002726 args[0].type = ARGT_SINT;
2727 args[0].data.sint = meth;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02002728 } else {
2729 /* Check method avalaibility. A methos is a token defined as :
2730 * tchar = "!" / "#" / "$" / "%" / "&" / "'" / "*" / "+" / "-" / "." /
2731 * "^" / "_" / "`" / "|" / "~" / DIGIT / ALPHA
2732 * token = 1*tchar
2733 */
2734 for (i = 0; i < args[0].data.str.len; i++) {
Willy Tarreau2235b262016-11-05 15:50:20 +01002735 if (!HTTP_IS_TOKEN(args[0].data.str.str[i])) {
Thierry FOURNIERcc103292015-06-06 19:30:17 +02002736 memprintf(err, "expects valid method.");
2737 return 0;
2738 }
2739 }
2740 }
2741 return 1;
2742}
2743
2744static int smp_fetch_const_meth(const struct arg *args, struct sample *smp, const char *kw, void *private)
2745{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002746 smp->data.type = SMP_T_METH;
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002747 if (args[0].type == ARGT_SINT) {
Thierry FOURNIERcc103292015-06-06 19:30:17 +02002748 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002749 smp->data.u.meth.meth = args[0].data.sint;
2750 smp->data.u.meth.str.str = "";
2751 smp->data.u.meth.str.len = 0;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02002752 } else {
2753 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002754 smp->data.u.meth.meth = HTTP_METH_OTHER;
2755 smp->data.u.meth.str.str = args[0].data.str.str;
2756 smp->data.u.meth.str.len = args[0].data.str.len;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02002757 }
2758 return 1;
2759}
2760
Willy Tarreau5b8ad222013-07-25 12:17:57 +02002761/* Note: must not be declared <const> as its list will be overwritten.
2762 * Note: fetches that may return multiple types must be declared as the lowest
2763 * common denominator, the type that can be casted into all other ones. For
2764 * instance IPv4/IPv6 must be declared IPv4.
2765 */
2766static struct sample_fetch_kw_list smp_kws = {ILH, {
2767 { "always_false", smp_fetch_false, 0, NULL, SMP_T_BOOL, SMP_USE_INTRN },
2768 { "always_true", smp_fetch_true, 0, NULL, SMP_T_BOOL, SMP_USE_INTRN },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01002769 { "env", smp_fetch_env, ARG1(1,STR), NULL, SMP_T_STR, SMP_USE_INTRN },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002770 { "date", smp_fetch_date, ARG1(0,SINT), NULL, SMP_T_SINT, SMP_USE_INTRN },
Nenad Merdanovic807a6e72017-03-12 22:00:00 +01002771 { "hostname", smp_fetch_hostname, 0, NULL, SMP_T_STR, SMP_USE_INTRN },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002772 { "nbproc", smp_fetch_nbproc,0, NULL, SMP_T_SINT, SMP_USE_INTRN },
2773 { "proc", smp_fetch_proc, 0, NULL, SMP_T_SINT, SMP_USE_INTRN },
Christopher Faulet34adb2a2017-11-21 21:45:38 +01002774 { "thread", smp_fetch_thread, 0, NULL, SMP_T_SINT, SMP_USE_INTRN },
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002775 { "rand", smp_fetch_rand, ARG1(0,SINT), NULL, SMP_T_SINT, SMP_USE_INTRN },
Willy Tarreau0f30d262014-11-24 16:02:05 +01002776 { "stopping", smp_fetch_stopping, 0, NULL, SMP_T_BOOL, SMP_USE_INTRN },
Thierry FOURNIERcc103292015-06-06 19:30:17 +02002777
2778 { "str", smp_fetch_const_str, ARG1(1,STR), NULL , SMP_T_STR, SMP_USE_INTRN },
2779 { "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 +02002780 { "int", smp_fetch_const_int, ARG1(1,SINT), NULL , SMP_T_SINT, SMP_USE_INTRN },
Thierry FOURNIERcc103292015-06-06 19:30:17 +02002781 { "ipv4", smp_fetch_const_ipv4, ARG1(1,IPV4), NULL , SMP_T_IPV4, SMP_USE_INTRN },
2782 { "ipv6", smp_fetch_const_ipv6, ARG1(1,IPV6), NULL , SMP_T_IPV6, SMP_USE_INTRN },
2783 { "bin", smp_fetch_const_bin, ARG1(1,STR), smp_check_const_bin , SMP_T_BIN, SMP_USE_INTRN },
2784 { "meth", smp_fetch_const_meth, ARG1(1,STR), smp_check_const_meth, SMP_T_METH, SMP_USE_INTRN },
2785
Willy Tarreau5b8ad222013-07-25 12:17:57 +02002786 { /* END */ },
2787}};
2788
Emeric Brun107ca302010-01-04 16:16:05 +01002789/* Note: must not be declared <const> as its list will be overwritten */
Willy Tarreaudc13c112013-06-21 23:16:39 +02002790static struct sample_conv_kw_list sample_conv_kws = {ILH, {
Thierry FOURNIER9687c772015-05-07 15:46:29 +02002791#ifdef DEBUG_EXPR
2792 { "debug", sample_conv_debug, 0, NULL, SMP_T_ANY, SMP_T_ANY },
2793#endif
2794
Holger Just1bfc24b2017-05-06 00:56:53 +02002795 { "b64dec", sample_conv_base642bin,0, NULL, SMP_T_STR, SMP_T_BIN },
Emeric Brun53d1a982014-04-30 18:21:37 +02002796 { "base64", sample_conv_bin2base64,0, NULL, SMP_T_BIN, SMP_T_STR },
Willy Tarreau12785782012-04-27 21:37:17 +02002797 { "upper", sample_conv_str2upper, 0, NULL, SMP_T_STR, SMP_T_STR },
2798 { "lower", sample_conv_str2lower, 0, NULL, SMP_T_STR, SMP_T_STR },
Willy Tarreau60a2ee72017-12-15 07:13:48 +01002799 { "length", sample_conv_length, 0, NULL, SMP_T_STR, SMP_T_SINT },
Thierry FOURNIER2f49d6d2014-03-12 15:01:52 +01002800 { "hex", sample_conv_bin2hex, 0, NULL, SMP_T_BIN, SMP_T_STR },
Dragan Dosen3f957b22017-10-24 09:27:34 +02002801 { "hex2i", sample_conv_hex2int, 0, NULL, SMP_T_STR, SMP_T_SINT },
Willy Tarreau12785782012-04-27 21:37:17 +02002802 { "ipmask", sample_conv_ipmask, ARG1(1,MSK4), NULL, SMP_T_IPV4, SMP_T_IPV4 },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002803 { "ltime", sample_conv_ltime, ARG2(1,STR,SINT), NULL, SMP_T_SINT, SMP_T_STR },
2804 { "utime", sample_conv_utime, ARG2(1,STR,SINT), NULL, SMP_T_SINT, SMP_T_STR },
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002805 { "crc32", sample_conv_crc32, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
2806 { "djb2", sample_conv_djb2, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
2807 { "sdbm", sample_conv_sdbm, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
2808 { "wt6", sample_conv_wt6, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
Thierry FOURNIER01e09742016-12-26 11:46:11 +01002809 { "xxh32", sample_conv_xxh32, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
2810 { "xxh64", sample_conv_xxh64, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002811 { "json", sample_conv_json, ARG1(1,STR), sample_conv_json_check, SMP_T_STR, SMP_T_STR },
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002812 { "bytes", sample_conv_bytes, ARG2(1,SINT,SINT), NULL, SMP_T_BIN, SMP_T_BIN },
2813 { "field", sample_conv_field, ARG2(2,SINT,STR), sample_conv_field_check, SMP_T_STR, SMP_T_STR },
2814 { "word", sample_conv_word, ARG2(2,SINT,STR), sample_conv_field_check, SMP_T_STR, SMP_T_STR },
Willy Tarreau7eda8492015-01-20 19:47:06 +01002815 { "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 +02002816 { "sha1", sample_conv_sha1, 0, NULL, SMP_T_BIN, SMP_T_BIN },
Willy Tarreau97707872015-01-27 15:12:13 +01002817
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002818 { "and", sample_conv_binary_and, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
2819 { "or", sample_conv_binary_or, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
2820 { "xor", sample_conv_binary_xor, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
2821 { "cpl", sample_conv_binary_cpl, 0, NULL, SMP_T_SINT, SMP_T_SINT },
2822 { "bool", sample_conv_arith_bool, 0, NULL, SMP_T_SINT, SMP_T_BOOL },
2823 { "not", sample_conv_arith_not, 0, NULL, SMP_T_SINT, SMP_T_BOOL },
2824 { "odd", sample_conv_arith_odd, 0, NULL, SMP_T_SINT, SMP_T_BOOL },
2825 { "even", sample_conv_arith_even, 0, NULL, SMP_T_SINT, SMP_T_BOOL },
2826 { "add", sample_conv_arith_add, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
2827 { "sub", sample_conv_arith_sub, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
2828 { "mul", sample_conv_arith_mul, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
2829 { "div", sample_conv_arith_div, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
2830 { "mod", sample_conv_arith_mod, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
2831 { "neg", sample_conv_arith_neg, 0, NULL, SMP_T_SINT, SMP_T_SINT },
Willy Tarreau97707872015-01-27 15:12:13 +01002832
Willy Tarreau9fcb9842012-04-20 14:45:49 +02002833 { NULL, NULL, 0, 0, 0 },
Emeric Brun107ca302010-01-04 16:16:05 +01002834}};
2835
2836__attribute__((constructor))
Willy Tarreau12785782012-04-27 21:37:17 +02002837static void __sample_init(void)
Emeric Brun107ca302010-01-04 16:16:05 +01002838{
Willy Tarreau5b8ad222013-07-25 12:17:57 +02002839 /* register sample fetch and format conversion keywords */
2840 sample_register_fetches(&smp_kws);
Willy Tarreau12785782012-04-27 21:37:17 +02002841 sample_register_convs(&sample_conv_kws);
Emeric Brun107ca302010-01-04 16:16:05 +01002842}