blob: 20a59bea9876945957515befa50d05a1063ce1b3 [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
Thierry FOURNIER01e09742016-12-26 11:46:11 +010036#include <import/xxhash.h>
37
Willy Tarreau1cf8f082014-02-07 12:14:54 +010038/* sample type names */
39const char *smp_to_type[SMP_TYPES] = {
Thierry FOURNIER9c627e82015-06-03 20:12:35 +020040 [SMP_T_ANY] = "any",
Willy Tarreau1cf8f082014-02-07 12:14:54 +010041 [SMP_T_BOOL] = "bool",
Willy Tarreau1cf8f082014-02-07 12:14:54 +010042 [SMP_T_SINT] = "sint",
43 [SMP_T_ADDR] = "addr",
44 [SMP_T_IPV4] = "ipv4",
45 [SMP_T_IPV6] = "ipv6",
46 [SMP_T_STR] = "str",
47 [SMP_T_BIN] = "bin",
Thierry FOURNIER4c2479e2015-06-03 20:12:04 +020048 [SMP_T_METH] = "meth",
Willy Tarreau1cf8f082014-02-07 12:14:54 +010049};
50
Willy Tarreau12785782012-04-27 21:37:17 +020051/* static sample used in sample_process() when <p> is NULL */
Willy Tarreaub4a88f02012-04-23 21:35:11 +020052static struct sample temp_smp;
Emeric Brun107ca302010-01-04 16:16:05 +010053
Willy Tarreau12785782012-04-27 21:37:17 +020054/* list head of all known sample fetch keywords */
55static struct sample_fetch_kw_list sample_fetches = {
56 .list = LIST_HEAD_INIT(sample_fetches.list)
Emeric Brun107ca302010-01-04 16:16:05 +010057};
58
Willy Tarreau12785782012-04-27 21:37:17 +020059/* list head of all known sample format conversion keywords */
60static struct sample_conv_kw_list sample_convs = {
61 .list = LIST_HEAD_INIT(sample_convs.list)
Emeric Brun107ca302010-01-04 16:16:05 +010062};
63
Willy Tarreau80aca902013-01-07 15:42:20 +010064const unsigned int fetch_cap[SMP_SRC_ENTRIES] = {
65 [SMP_SRC_INTRN] = (SMP_VAL_FE_CON_ACC | SMP_VAL_FE_SES_ACC | SMP_VAL_FE_REQ_CNT |
66 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
67 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
68 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
69 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
70 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
71 SMP_VAL_FE_LOG_END),
72
73 [SMP_SRC_LISTN] = (SMP_VAL_FE_CON_ACC | SMP_VAL_FE_SES_ACC | SMP_VAL_FE_REQ_CNT |
74 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
75 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
76 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
77 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
78 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
79 SMP_VAL_FE_LOG_END),
80
81 [SMP_SRC_FTEND] = (SMP_VAL_FE_CON_ACC | SMP_VAL_FE_SES_ACC | SMP_VAL_FE_REQ_CNT |
82 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
83 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
84 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
85 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
86 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
87 SMP_VAL_FE_LOG_END),
88
89 [SMP_SRC_L4CLI] = (SMP_VAL_FE_CON_ACC | SMP_VAL_FE_SES_ACC | SMP_VAL_FE_REQ_CNT |
90 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
91 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
92 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
93 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
94 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
95 SMP_VAL_FE_LOG_END),
96
97 [SMP_SRC_L5CLI] = (SMP_VAL___________ | SMP_VAL_FE_SES_ACC | SMP_VAL_FE_REQ_CNT |
98 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
99 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
100 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
101 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
102 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
103 SMP_VAL_FE_LOG_END),
104
105 [SMP_SRC_TRACK] = (SMP_VAL_FE_CON_ACC | SMP_VAL_FE_SES_ACC | SMP_VAL_FE_REQ_CNT |
106 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
107 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
108 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
109 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
110 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
111 SMP_VAL_FE_LOG_END),
112
113 [SMP_SRC_L6REQ] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_FE_REQ_CNT |
114 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
115 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
116 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL___________ |
117 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
118 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
119 SMP_VAL___________),
120
121 [SMP_SRC_HRQHV] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_FE_REQ_CNT |
122 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
123 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
124 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL___________ |
125 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
126 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
127 SMP_VAL___________),
128
129 [SMP_SRC_HRQHP] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_FE_REQ_CNT |
130 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
131 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
132 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
133 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
134 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
135 SMP_VAL_FE_LOG_END),
136
137 [SMP_SRC_HRQBO] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
138 SMP_VAL___________ | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
139 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
140 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL___________ |
141 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
142 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
143 SMP_VAL___________),
144
145 [SMP_SRC_BKEND] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
146 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
147 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
148 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
149 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
150 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
151 SMP_VAL_FE_LOG_END),
152
153 [SMP_SRC_SERVR] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
154 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
155 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
156 SMP_VAL___________ | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
157 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
158 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
159 SMP_VAL_FE_LOG_END),
160
161 [SMP_SRC_L4SRV] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
162 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
163 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
164 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_BE_RES_CNT |
165 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
166 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
167 SMP_VAL_FE_LOG_END),
168
169 [SMP_SRC_L5SRV] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
170 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
171 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
172 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_BE_RES_CNT |
173 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
174 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
175 SMP_VAL_FE_LOG_END),
176
177 [SMP_SRC_L6RES] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
178 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
179 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
180 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_BE_RES_CNT |
181 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
182 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
183 SMP_VAL___________),
184
185 [SMP_SRC_HRSHV] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
186 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
187 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
188 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_BE_RES_CNT |
189 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
190 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
191 SMP_VAL___________),
192
193 [SMP_SRC_HRSHP] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
194 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
195 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
196 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_BE_RES_CNT |
197 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
198 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
199 SMP_VAL_FE_LOG_END),
200
201 [SMP_SRC_HRSBO] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
202 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_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
206 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
207 SMP_VAL___________),
208
209 [SMP_SRC_RQFIN] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
210 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_FE_LOG_END),
216
217 [SMP_SRC_RSFIN] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
218 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_FE_LOG_END),
224
225 [SMP_SRC_TXFIN] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
226 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_FE_LOG_END),
232
233 [SMP_SRC_SSFIN] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
234 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_FE_LOG_END),
240};
241
242static const char *fetch_src_names[SMP_SRC_ENTRIES] = {
243 [SMP_SRC_INTRN] = "internal state",
244 [SMP_SRC_LISTN] = "listener",
245 [SMP_SRC_FTEND] = "frontend",
246 [SMP_SRC_L4CLI] = "client address",
247 [SMP_SRC_L5CLI] = "client-side connection",
248 [SMP_SRC_TRACK] = "track counters",
249 [SMP_SRC_L6REQ] = "request buffer",
250 [SMP_SRC_HRQHV] = "HTTP request headers",
251 [SMP_SRC_HRQHP] = "HTTP request",
252 [SMP_SRC_HRQBO] = "HTTP request body",
253 [SMP_SRC_BKEND] = "backend",
254 [SMP_SRC_SERVR] = "server",
255 [SMP_SRC_L4SRV] = "server address",
256 [SMP_SRC_L5SRV] = "server-side connection",
257 [SMP_SRC_L6RES] = "response buffer",
258 [SMP_SRC_HRSHV] = "HTTP response headers",
259 [SMP_SRC_HRSHP] = "HTTP response",
260 [SMP_SRC_HRSBO] = "HTTP response body",
261 [SMP_SRC_RQFIN] = "request buffer statistics",
262 [SMP_SRC_RSFIN] = "response buffer statistics",
263 [SMP_SRC_TXFIN] = "transaction statistics",
264 [SMP_SRC_SSFIN] = "session statistics",
265};
266
Willy Tarreaubf8e2512013-03-25 14:52:41 +0100267static const char *fetch_ckp_names[SMP_CKP_ENTRIES] = {
268 [SMP_CKP_FE_CON_ACC] = "frontend tcp-request connection rule",
269 [SMP_CKP_FE_SES_ACC] = "frontend tcp-request session rule",
270 [SMP_CKP_FE_REQ_CNT] = "frontend tcp-request content rule",
271 [SMP_CKP_FE_HRQ_HDR] = "frontend http-request header rule",
272 [SMP_CKP_FE_HRQ_BDY] = "frontend http-request body rule",
273 [SMP_CKP_FE_SET_BCK] = "frontend use-backend rule",
274 [SMP_CKP_BE_REQ_CNT] = "backend tcp-request content rule",
275 [SMP_CKP_BE_HRQ_HDR] = "backend http-request header rule",
276 [SMP_CKP_BE_HRQ_BDY] = "backend http-request body rule",
277 [SMP_CKP_BE_SET_SRV] = "backend use-server, balance or stick-match rule",
278 [SMP_CKP_BE_SRV_CON] = "server source selection",
279 [SMP_CKP_BE_RES_CNT] = "backend tcp-response content rule",
280 [SMP_CKP_BE_HRS_HDR] = "backend http-response header rule",
281 [SMP_CKP_BE_HRS_BDY] = "backend http-response body rule",
282 [SMP_CKP_BE_STO_RUL] = "backend stick-store rule",
283 [SMP_CKP_FE_RES_CNT] = "frontend tcp-response content rule",
284 [SMP_CKP_FE_HRS_HDR] = "frontend http-response header rule",
285 [SMP_CKP_FE_HRS_BDY] = "frontend http-response body rule",
286 [SMP_CKP_FE_LOG_END] = "logs",
287};
288
Thierry FOURNIER348971e2013-11-21 10:50:10 +0100289/* This function returns the type of the data returned by the sample_expr.
290 * It assumes that the <expr> and all of its converters are properly
291 * initialized.
292 */
293inline
294int smp_expr_output_type(struct sample_expr *expr)
295{
296 struct sample_conv_expr *smp_expr;
297
298 if (!LIST_ISEMPTY(&expr->conv_exprs)) {
299 smp_expr = LIST_PREV(&expr->conv_exprs, struct sample_conv_expr *, list);
300 return smp_expr->conv->out_type;
301 }
302 return expr->fetch->out_type;
303}
304
305
Willy Tarreau80aca902013-01-07 15:42:20 +0100306/* fill the trash with a comma-delimited list of source names for the <use> bit
307 * field which must be composed of a non-null set of SMP_USE_* flags. The return
308 * value is the pointer to the string in the trash buffer.
309 */
310const char *sample_src_names(unsigned int use)
311{
312 int bit;
313
314 trash.len = 0;
315 trash.str[0] = '\0';
316 for (bit = 0; bit < SMP_SRC_ENTRIES; bit++) {
317 if (!(use & ~((1 << bit) - 1)))
318 break; /* no more bits */
319
320 if (!(use & (1 << bit)))
321 continue; /* bit not set */
322
323 trash.len += snprintf(trash.str + trash.len, trash.size - trash.len, "%s%s",
324 (use & ((1 << bit) - 1)) ? "," : "",
325 fetch_src_names[bit]);
326 }
327 return trash.str;
328}
329
Willy Tarreaubf8e2512013-03-25 14:52:41 +0100330/* return a pointer to the correct sample checkpoint name, or "unknown" when
331 * the flags are invalid. Only the lowest bit is used, higher bits are ignored
332 * if set.
333 */
334const char *sample_ckp_names(unsigned int use)
335{
336 int bit;
337
338 for (bit = 0; bit < SMP_CKP_ENTRIES; bit++)
339 if (use & (1 << bit))
340 return fetch_ckp_names[bit];
341 return "unknown sample check place, please report this bug";
342}
343
Emeric Brun107ca302010-01-04 16:16:05 +0100344/*
Willy Tarreau80aca902013-01-07 15:42:20 +0100345 * Registers the sample fetch keyword list <kwl> as a list of valid keywords
346 * for next parsing sessions. The fetch keywords capabilities are also computed
347 * from their ->use field.
Emeric Brun107ca302010-01-04 16:16:05 +0100348 */
Willy Tarreau80aca902013-01-07 15:42:20 +0100349void sample_register_fetches(struct sample_fetch_kw_list *kwl)
Emeric Brun107ca302010-01-04 16:16:05 +0100350{
Willy Tarreau80aca902013-01-07 15:42:20 +0100351 struct sample_fetch *sf;
352 int bit;
353
354 for (sf = kwl->kw; sf->kw != NULL; sf++) {
355 for (bit = 0; bit < SMP_SRC_ENTRIES; bit++)
356 if (sf->use & (1 << bit))
357 sf->val |= fetch_cap[bit];
358 }
359 LIST_ADDQ(&sample_fetches.list, &kwl->list);
Emeric Brun107ca302010-01-04 16:16:05 +0100360}
361
362/*
Willy Tarreau12785782012-04-27 21:37:17 +0200363 * Registers the sample format coverstion keyword list <pckl> as a list of valid keywords for next
Emeric Brun107ca302010-01-04 16:16:05 +0100364 * parsing sessions.
365 */
Willy Tarreau12785782012-04-27 21:37:17 +0200366void sample_register_convs(struct sample_conv_kw_list *pckl)
Emeric Brun107ca302010-01-04 16:16:05 +0100367{
Willy Tarreau12785782012-04-27 21:37:17 +0200368 LIST_ADDQ(&sample_convs.list, &pckl->list);
Emeric Brun107ca302010-01-04 16:16:05 +0100369}
370
371/*
Willy Tarreau12785782012-04-27 21:37:17 +0200372 * Returns the pointer on sample fetch keyword structure identified by
Emeric Brun107ca302010-01-04 16:16:05 +0100373 * string of <len> in buffer <kw>.
374 *
375 */
Willy Tarreau12785782012-04-27 21:37:17 +0200376struct sample_fetch *find_sample_fetch(const char *kw, int len)
Emeric Brun107ca302010-01-04 16:16:05 +0100377{
378 int index;
Willy Tarreau12785782012-04-27 21:37:17 +0200379 struct sample_fetch_kw_list *kwl;
Emeric Brun107ca302010-01-04 16:16:05 +0100380
Willy Tarreau12785782012-04-27 21:37:17 +0200381 list_for_each_entry(kwl, &sample_fetches.list, list) {
Emeric Brun107ca302010-01-04 16:16:05 +0100382 for (index = 0; kwl->kw[index].kw != NULL; index++) {
383 if (strncmp(kwl->kw[index].kw, kw, len) == 0 &&
384 kwl->kw[index].kw[len] == '\0')
385 return &kwl->kw[index];
386 }
387 }
388 return NULL;
389}
390
Thierry FOURNIER4d9a1d12014-12-08 14:49:19 +0100391/* This fucntion browse the list of available saple fetch. <current> is
392 * the last used sample fetch. If it is the first call, it must set to NULL.
393 * <idx> is the index of the next sampleèfetch entry. It is used as private
394 * value. It is useles to initiate it.
395 *
396 * It returns always the newt fetch_sample entry, and NULL when the end of
397 * the list is reached.
398 */
399struct sample_fetch *sample_fetch_getnext(struct sample_fetch *current, int *idx)
400{
401 struct sample_fetch_kw_list *kwl;
402 struct sample_fetch *base;
403
404 if (!current) {
405 /* Get first kwl entry. */
406 kwl = LIST_NEXT(&sample_fetches.list, struct sample_fetch_kw_list *, list);
407 (*idx) = 0;
408 } else {
409 /* Get kwl corresponding to the curret entry. */
410 base = current + 1 - (*idx);
411 kwl = container_of(base, struct sample_fetch_kw_list, kw);
412 }
413
414 while (1) {
415
416 /* Check if kwl is the last entry. */
417 if (&kwl->list == &sample_fetches.list)
418 return NULL;
419
420 /* idx contain the next keyword. If it is available, return it. */
421 if (kwl->kw[*idx].kw) {
422 (*idx)++;
423 return &kwl->kw[(*idx)-1];
424 }
425
426 /* get next entry in the main list, and return NULL if the end is reached. */
427 kwl = LIST_NEXT(&kwl->list, struct sample_fetch_kw_list *, list);
428
429 /* Set index to 0, ans do one other loop. */
430 (*idx) = 0;
431 }
432}
433
Thierry FOURNIER8fd13762015-03-10 23:56:48 +0100434/* This function browses the list of available converters. <current> is
435 * the last used converter. If it is the first call, it must set to NULL.
436 * <idx> is the index of the next converter entry. It is used as private
437 * value. It is useless to initiate it.
438 *
439 * It returns always the next sample_conv entry, and NULL when the end of
440 * the list is reached.
441 */
442struct sample_conv *sample_conv_getnext(struct sample_conv *current, int *idx)
443{
444 struct sample_conv_kw_list *kwl;
445 struct sample_conv *base;
446
447 if (!current) {
448 /* Get first kwl entry. */
449 kwl = LIST_NEXT(&sample_convs.list, struct sample_conv_kw_list *, list);
450 (*idx) = 0;
451 } else {
452 /* Get kwl corresponding to the curret entry. */
453 base = current + 1 - (*idx);
454 kwl = container_of(base, struct sample_conv_kw_list, kw);
455 }
456
457 while (1) {
458 /* Check if kwl is the last entry. */
459 if (&kwl->list == &sample_convs.list)
460 return NULL;
461
462 /* idx contain the next keyword. If it is available, return it. */
463 if (kwl->kw[*idx].kw) {
464 (*idx)++;
465 return &kwl->kw[(*idx)-1];
466 }
467
468 /* get next entry in the main list, and return NULL if the end is reached. */
469 kwl = LIST_NEXT(&kwl->list, struct sample_conv_kw_list *, list);
470
471 /* Set index to 0, ans do one other loop. */
472 (*idx) = 0;
473 }
474}
475
Emeric Brun107ca302010-01-04 16:16:05 +0100476/*
Willy Tarreau12785782012-04-27 21:37:17 +0200477 * Returns the pointer on sample format conversion keyword structure identified by
Emeric Brun107ca302010-01-04 16:16:05 +0100478 * string of <len> in buffer <kw>.
479 *
480 */
Willy Tarreau12785782012-04-27 21:37:17 +0200481struct sample_conv *find_sample_conv(const char *kw, int len)
Emeric Brun107ca302010-01-04 16:16:05 +0100482{
483 int index;
Willy Tarreau12785782012-04-27 21:37:17 +0200484 struct sample_conv_kw_list *kwl;
Emeric Brun107ca302010-01-04 16:16:05 +0100485
Willy Tarreau12785782012-04-27 21:37:17 +0200486 list_for_each_entry(kwl, &sample_convs.list, list) {
Emeric Brun107ca302010-01-04 16:16:05 +0100487 for (index = 0; kwl->kw[index].kw != NULL; index++) {
488 if (strncmp(kwl->kw[index].kw, kw, len) == 0 &&
489 kwl->kw[index].kw[len] == '\0')
490 return &kwl->kw[index];
491 }
492 }
493 return NULL;
494}
495
Emeric Brun107ca302010-01-04 16:16:05 +0100496/******************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +0200497/* Sample casts functions */
Willy Tarreaub8c8f1f2012-04-23 22:38:26 +0200498/* Note: these functions do *NOT* set the output type on the */
499/* sample, the caller is responsible for doing this on return. */
Emeric Brun107ca302010-01-04 16:16:05 +0100500/******************************************************************/
501
Willy Tarreau342acb42012-04-23 22:03:39 +0200502static int c_ip2int(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100503{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200504 smp->data.u.sint = ntohl(smp->data.u.ipv4.s_addr);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200505 smp->data.type = SMP_T_SINT;
Emeric Brun107ca302010-01-04 16:16:05 +0100506 return 1;
507}
508
Willy Tarreau342acb42012-04-23 22:03:39 +0200509static int c_ip2str(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100510{
Willy Tarreau47ca5452012-12-23 20:22:19 +0100511 struct chunk *trash = get_trash_chunk();
Emeric Brun107ca302010-01-04 16:16:05 +0100512
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200513 if (!inet_ntop(AF_INET, (void *)&smp->data.u.ipv4, trash->str, trash->size))
Emeric Brun107ca302010-01-04 16:16:05 +0100514 return 0;
515
516 trash->len = strlen(trash->str);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200517 smp->data.u.str = *trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200518 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100519 smp->flags &= ~SMP_F_CONST;
Emeric Brun107ca302010-01-04 16:16:05 +0100520
521 return 1;
522}
523
Willy Tarreau342acb42012-04-23 22:03:39 +0200524static int c_ip2ipv6(struct sample *smp)
David du Colombier4f92d322011-03-24 11:09:31 +0100525{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200526 v4tov6(&smp->data.u.ipv6, &smp->data.u.ipv4);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200527 smp->data.type = SMP_T_IPV6;
David du Colombier4f92d322011-03-24 11:09:31 +0100528 return 1;
529}
530
Thierry FOURNIERcc4d1712015-07-24 09:04:56 +0200531static int c_ipv62ip(struct sample *smp)
532{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200533 if (!v6tov4(&smp->data.u.ipv4, &smp->data.u.ipv6))
Thierry FOURNIERcc4d1712015-07-24 09:04:56 +0200534 return 0;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200535 smp->data.type = SMP_T_IPV6;
Thierry FOURNIERcc4d1712015-07-24 09:04:56 +0200536 return 1;
537}
538
Willy Tarreau342acb42012-04-23 22:03:39 +0200539static int c_ipv62str(struct sample *smp)
David du Colombier4f92d322011-03-24 11:09:31 +0100540{
Willy Tarreau47ca5452012-12-23 20:22:19 +0100541 struct chunk *trash = get_trash_chunk();
David du Colombier4f92d322011-03-24 11:09:31 +0100542
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200543 if (!inet_ntop(AF_INET6, (void *)&smp->data.u.ipv6, trash->str, trash->size))
David du Colombier4f92d322011-03-24 11:09:31 +0100544 return 0;
545
546 trash->len = strlen(trash->str);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200547 smp->data.u.str = *trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200548 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100549 smp->flags &= ~SMP_F_CONST;
David du Colombier4f92d322011-03-24 11:09:31 +0100550 return 1;
551}
552
553/*
Willy Tarreau342acb42012-04-23 22:03:39 +0200554static int c_ipv62ip(struct sample *smp)
David du Colombier4f92d322011-03-24 11:09:31 +0100555{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200556 return v6tov4(&smp->data.u.ipv4, &smp->data.u.ipv6);
David du Colombier4f92d322011-03-24 11:09:31 +0100557}
558*/
559
Willy Tarreau342acb42012-04-23 22:03:39 +0200560static int c_int2ip(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100561{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200562 smp->data.u.ipv4.s_addr = htonl((unsigned int)smp->data.u.sint);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200563 smp->data.type = SMP_T_IPV4;
Emeric Brun107ca302010-01-04 16:16:05 +0100564 return 1;
565}
566
Thierry FOURNIERcc4d1712015-07-24 09:04:56 +0200567static int c_int2ipv6(struct sample *smp)
568{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200569 smp->data.u.ipv4.s_addr = htonl((unsigned int)smp->data.u.sint);
570 v4tov6(&smp->data.u.ipv6, &smp->data.u.ipv4);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200571 smp->data.type = SMP_T_IPV6;
Thierry FOURNIERcc4d1712015-07-24 09:04:56 +0200572 return 1;
573}
574
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100575static int c_str2addr(struct sample *smp)
576{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200577 if (!buf2ip(smp->data.u.str.str, smp->data.u.str.len, &smp->data.u.ipv4)) {
578 if (!buf2ip6(smp->data.u.str.str, smp->data.u.str.len, &smp->data.u.ipv6))
Thierry FOURNIERfd139902013-12-11 12:38:57 +0100579 return 0;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200580 smp->data.type = SMP_T_IPV6;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100581 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIERfd139902013-12-11 12:38:57 +0100582 return 1;
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100583 }
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200584 smp->data.type = SMP_T_IPV4;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100585 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100586 return 1;
587}
588
Willy Tarreau342acb42012-04-23 22:03:39 +0200589static int c_str2ip(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100590{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200591 if (!buf2ip(smp->data.u.str.str, smp->data.u.str.len, &smp->data.u.ipv4))
Emeric Brun107ca302010-01-04 16:16:05 +0100592 return 0;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200593 smp->data.type = SMP_T_IPV4;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100594 smp->flags &= ~SMP_F_CONST;
Emeric Brun107ca302010-01-04 16:16:05 +0100595 return 1;
596}
597
Willy Tarreau342acb42012-04-23 22:03:39 +0200598static int c_str2ipv6(struct sample *smp)
David du Colombier4f92d322011-03-24 11:09:31 +0100599{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200600 if (!buf2ip6(smp->data.u.str.str, smp->data.u.str.len, &smp->data.u.ipv6))
Thierry FOURNIERfd139902013-12-11 12:38:57 +0100601 return 0;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200602 smp->data.type = SMP_T_IPV6;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100603 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIERfd139902013-12-11 12:38:57 +0100604 return 1;
David du Colombier4f92d322011-03-24 11:09:31 +0100605}
606
Emeric Brun4b9e8022014-11-03 18:17:10 +0100607/*
608 * The NULL char always enforces the end of string if it is met.
609 * Data is never changed, so we can ignore the CONST case
Thierry FOURNIERe87cac12014-03-12 15:07:59 +0100610 */
Emeric Brun8ac33d92012-10-17 13:36:06 +0200611static int c_bin2str(struct sample *smp)
612{
Emeric Brun4b9e8022014-11-03 18:17:10 +0100613 int i;
Emeric Brun8ac33d92012-10-17 13:36:06 +0200614
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200615 for (i = 0; i < smp->data.u.str.len; i++) {
616 if (!smp->data.u.str.str[i]) {
617 smp->data.u.str.len = i;
Thierry FOURNIERe87cac12014-03-12 15:07:59 +0100618 break;
Emeric Brun4b9e8022014-11-03 18:17:10 +0100619 }
Emeric Brun8ac33d92012-10-17 13:36:06 +0200620 }
Emeric Brun8ac33d92012-10-17 13:36:06 +0200621 return 1;
622}
623
Willy Tarreau342acb42012-04-23 22:03:39 +0200624static int c_int2str(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100625{
Willy Tarreau47ca5452012-12-23 20:22:19 +0100626 struct chunk *trash = get_trash_chunk();
Emeric Brun107ca302010-01-04 16:16:05 +0100627 char *pos;
628
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200629 pos = lltoa_r(smp->data.u.sint, trash->str, trash->size);
Emeric Brun107ca302010-01-04 16:16:05 +0100630 if (!pos)
631 return 0;
632
Emeric Brun485479d2010-09-23 18:02:19 +0200633 trash->size = trash->size - (pos - trash->str);
Emeric Brun107ca302010-01-04 16:16:05 +0100634 trash->str = pos;
635 trash->len = strlen(pos);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200636 smp->data.u.str = *trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200637 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100638 smp->flags &= ~SMP_F_CONST;
Emeric Brun107ca302010-01-04 16:16:05 +0100639 return 1;
640}
641
Willy Tarreauad635822016-08-08 19:21:09 +0200642/* This function inconditionally duplicates data and removes the "const" flag.
643 * For strings and binary blocks, it also provides a known allocated size with
644 * a length that is capped to the size, and ensures a trailing zero is always
645 * appended for strings. This is necessary for some operations which may
646 * require to extend the length. It returns 0 if it fails, 1 on success.
647 */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100648int smp_dup(struct sample *smp)
Emeric Brun485479d2010-09-23 18:02:19 +0200649{
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100650 struct chunk *trash;
Emeric Brun485479d2010-09-23 18:02:19 +0200651
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200652 switch (smp->data.type) {
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100653 case SMP_T_BOOL:
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100654 case SMP_T_SINT:
655 case SMP_T_ADDR:
656 case SMP_T_IPV4:
657 case SMP_T_IPV6:
658 /* These type are not const. */
659 break;
Willy Tarreauad635822016-08-08 19:21:09 +0200660
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100661 case SMP_T_STR:
Willy Tarreauad635822016-08-08 19:21:09 +0200662 trash = get_trash_chunk();
663 trash->len = smp->data.u.str.len;
664 if (trash->len > trash->size - 1)
665 trash->len = trash->size - 1;
666
667 memcpy(trash->str, smp->data.u.str.str, trash->len);
668 trash->str[trash->len] = 0;
669 smp->data.u.str = *trash;
670 break;
671
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100672 case SMP_T_BIN:
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100673 trash = get_trash_chunk();
Willy Tarreauad635822016-08-08 19:21:09 +0200674 trash->len = smp->data.u.str.len;
675 if (trash->len > trash->size)
676 trash->len = trash->size;
677
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200678 memcpy(trash->str, smp->data.u.str.str, trash->len);
679 smp->data.u.str = *trash;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100680 break;
681 default:
682 /* Other cases are unexpected. */
683 return 0;
684 }
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100685
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100686 /* remove const flag */
687 smp->flags &= ~SMP_F_CONST;
Emeric Brun485479d2010-09-23 18:02:19 +0200688 return 1;
689}
690
Thierry FOURNIER0e9af552013-12-14 14:55:04 +0100691int c_none(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100692{
693 return 1;
694}
695
Willy Tarreau342acb42012-04-23 22:03:39 +0200696static int c_str2int(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100697{
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +0200698 const char *str;
699 const char *end;
Emeric Brun107ca302010-01-04 16:16:05 +0100700
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200701 if (smp->data.u.str.len == 0)
Thierry FOURNIER60bb0202014-01-27 18:20:48 +0100702 return 0;
703
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200704 str = smp->data.u.str.str;
705 end = smp->data.u.str.str + smp->data.u.str.len;
Emeric Brun107ca302010-01-04 16:16:05 +0100706
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200707 smp->data.u.sint = read_int64(&str, end);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200708 smp->data.type = SMP_T_SINT;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100709 smp->flags &= ~SMP_F_CONST;
Emeric Brun107ca302010-01-04 16:16:05 +0100710 return 1;
711}
712
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100713static int c_str2meth(struct sample *smp)
714{
715 enum http_meth_t meth;
716 int len;
717
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200718 meth = find_http_meth(smp->data.u.str.str, smp->data.u.str.len);
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100719 if (meth == HTTP_METH_OTHER) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200720 len = smp->data.u.str.len;
721 smp->data.u.meth.str.str = smp->data.u.str.str;
722 smp->data.u.meth.str.len = len;
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100723 }
724 else
725 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200726 smp->data.u.meth.meth = meth;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200727 smp->data.type = SMP_T_METH;
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100728 return 1;
729}
730
731static int c_meth2str(struct sample *smp)
732{
733 int len;
734 enum http_meth_t meth;
735
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200736 if (smp->data.u.meth.meth == HTTP_METH_OTHER) {
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100737 /* The method is unknown. Copy the original pointer. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200738 len = smp->data.u.meth.str.len;
739 smp->data.u.str.str = smp->data.u.meth.str.str;
740 smp->data.u.str.len = len;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200741 smp->data.type = SMP_T_STR;
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100742 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200743 else if (smp->data.u.meth.meth < HTTP_METH_OTHER) {
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100744 /* The method is known, copy the pointer containing the string. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200745 meth = smp->data.u.meth.meth;
746 smp->data.u.str.str = http_known_methods[meth].name;
747 smp->data.u.str.len = http_known_methods[meth].len;
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100748 smp->flags |= SMP_F_CONST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200749 smp->data.type = SMP_T_STR;
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100750 }
751 else {
752 /* Unknown method */
753 return 0;
754 }
755 return 1;
756}
757
Willy Tarreau9700e5c2014-07-15 21:03:26 +0200758static int c_addr2bin(struct sample *smp)
759{
760 struct chunk *chk = get_trash_chunk();
761
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200762 if (smp->data.type == SMP_T_IPV4) {
Willy Tarreau9700e5c2014-07-15 21:03:26 +0200763 chk->len = 4;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200764 memcpy(chk->str, &smp->data.u.ipv4, chk->len);
Willy Tarreau9700e5c2014-07-15 21:03:26 +0200765 }
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200766 else if (smp->data.type == SMP_T_IPV6) {
Willy Tarreau9700e5c2014-07-15 21:03:26 +0200767 chk->len = 16;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200768 memcpy(chk->str, &smp->data.u.ipv6, chk->len);
Willy Tarreau9700e5c2014-07-15 21:03:26 +0200769 }
770 else
771 return 0;
772
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200773 smp->data.u.str = *chk;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200774 smp->data.type = SMP_T_BIN;
Willy Tarreau9700e5c2014-07-15 21:03:26 +0200775 return 1;
776}
777
Willy Tarreaubbfd1a22014-07-15 21:19:08 +0200778static int c_int2bin(struct sample *smp)
779{
780 struct chunk *chk = get_trash_chunk();
781
Willy Tarreau5f6e9052016-05-20 06:29:59 +0200782 *(unsigned long long int *)chk->str = my_htonll(smp->data.u.sint);
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +0200783 chk->len = 8;
Willy Tarreaubbfd1a22014-07-15 21:19:08 +0200784
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200785 smp->data.u.str = *chk;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200786 smp->data.type = SMP_T_BIN;
Willy Tarreaubbfd1a22014-07-15 21:19:08 +0200787 return 1;
788}
789
Willy Tarreau9700e5c2014-07-15 21:03:26 +0200790
Emeric Brun107ca302010-01-04 16:16:05 +0100791/*****************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +0200792/* Sample casts matrix: */
793/* sample_casts[from type][to type] */
794/* NULL pointer used for impossible sample casts */
Emeric Brun107ca302010-01-04 16:16:05 +0100795/*****************************************************************/
Emeric Brun107ca302010-01-04 16:16:05 +0100796
Thierry FOURNIER8af6ff12013-11-21 10:53:12 +0100797sample_cast_fct sample_casts[SMP_TYPES][SMP_TYPES] = {
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +0200798/* to: ANY BOOL SINT ADDR IPV4 IPV6 STR BIN METH */
799/* from: ANY */ { c_none, c_none, c_none, c_none, c_none, c_none, c_none, c_none, c_none, },
800/* BOOL */ { c_none, c_none, c_none, NULL, NULL, NULL, c_int2str, NULL, NULL, },
Thierry FOURNIERcc4d1712015-07-24 09:04:56 +0200801/* 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 +0200802/* ADDR */ { c_none, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, },
803/* IPV4 */ { c_none, NULL, c_ip2int, c_none, c_none, c_ip2ipv6, c_ip2str, c_addr2bin, NULL, },
Thierry FOURNIERcc4d1712015-07-24 09:04:56 +0200804/* IPV6 */ { c_none, NULL, NULL, c_none, c_ipv62ip,c_none, c_ipv62str, c_addr2bin, NULL, },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +0200805/* STR */ { c_none, c_str2int, c_str2int, c_str2addr, c_str2ip, c_str2ipv6, c_none, c_none, c_str2meth, },
806/* BIN */ { c_none, NULL, NULL, NULL, NULL, NULL, c_bin2str, c_none, c_str2meth, },
807/* METH */ { c_none, NULL, NULL, NULL, NULL, NULL, c_meth2str, c_meth2str, c_none, }
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200808};
Emeric Brun107ca302010-01-04 16:16:05 +0100809
Emeric Brun107ca302010-01-04 16:16:05 +0100810/*
Willy Tarreau12785782012-04-27 21:37:17 +0200811 * Parse a sample expression configuration:
Emeric Brun107ca302010-01-04 16:16:05 +0100812 * fetch keyword followed by format conversion keywords.
Willy Tarreau12785782012-04-27 21:37:17 +0200813 * Returns a pointer on allocated sample expression structure.
Willy Tarreaua4312fa2013-04-02 16:34:32 +0200814 * The caller must have set al->ctx.
Emeric Brun107ca302010-01-04 16:16:05 +0100815 */
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +0100816struct 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 +0100817{
Willy Tarreau833cc792013-07-24 15:34:19 +0200818 const char *begw; /* beginning of word */
819 const char *endw; /* end of word */
820 const char *endt; /* end of term */
Willy Tarreau12785782012-04-27 21:37:17 +0200821 struct sample_expr *expr;
822 struct sample_fetch *fetch;
823 struct sample_conv *conv;
Emeric Brun107ca302010-01-04 16:16:05 +0100824 unsigned long prev_type;
Willy Tarreau833cc792013-07-24 15:34:19 +0200825 char *fkw = NULL;
826 char *ckw = NULL;
Willy Tarreau689a1df2013-12-13 00:40:11 +0100827 int err_arg;
Emeric Brun107ca302010-01-04 16:16:05 +0100828
Willy Tarreau833cc792013-07-24 15:34:19 +0200829 begw = str[*idx];
830 for (endw = begw; *endw && *endw != '(' && *endw != ','; endw++);
831
832 if (endw == begw) {
Willy Tarreau975c1782013-12-12 23:16:54 +0100833 memprintf(err_msg, "missing fetch method");
Emeric Brun107ca302010-01-04 16:16:05 +0100834 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200835 }
Emeric Brun107ca302010-01-04 16:16:05 +0100836
Willy Tarreau833cc792013-07-24 15:34:19 +0200837 /* keep a copy of the current fetch keyword for error reporting */
838 fkw = my_strndup(begw, endw - begw);
Emeric Brun107ca302010-01-04 16:16:05 +0100839
Willy Tarreau833cc792013-07-24 15:34:19 +0200840 fetch = find_sample_fetch(begw, endw - begw);
841 if (!fetch) {
Willy Tarreau975c1782013-12-12 23:16:54 +0100842 memprintf(err_msg, "unknown fetch method '%s'", fkw);
Emeric Brun107ca302010-01-04 16:16:05 +0100843 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200844 }
Emeric Brun107ca302010-01-04 16:16:05 +0100845
Willy Tarreau833cc792013-07-24 15:34:19 +0200846 endt = endw;
847 if (*endt == '(') {
Willy Tarreau689a1df2013-12-13 00:40:11 +0100848 /* look for the end of this term and skip the opening parenthesis */
849 endt = ++endw;
Willy Tarreau833cc792013-07-24 15:34:19 +0200850 while (*endt && *endt != ')')
851 endt++;
852 if (*endt != ')') {
Willy Tarreau975c1782013-12-12 23:16:54 +0100853 memprintf(err_msg, "missing closing ')' after arguments to fetch keyword '%s'", fkw);
Willy Tarreau833cc792013-07-24 15:34:19 +0200854 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200855 }
Emeric Brun485479d2010-09-23 18:02:19 +0200856 }
Emeric Brun107ca302010-01-04 16:16:05 +0100857
Willy Tarreau833cc792013-07-24 15:34:19 +0200858 /* At this point, we have :
859 * - begw : beginning of the keyword
Willy Tarreau689a1df2013-12-13 00:40:11 +0100860 * - endw : end of the keyword, first character not part of keyword
861 * nor the opening parenthesis (so first character of args
862 * if present).
Willy Tarreau833cc792013-07-24 15:34:19 +0200863 * - endt : end of the term (=endw or last parenthesis if args are present)
864 */
865
866 if (fetch->out_type >= SMP_TYPES) {
Willy Tarreau975c1782013-12-12 23:16:54 +0100867 memprintf(err_msg, "returns type of fetch method '%s' is unknown", fkw);
Emeric Brun107ca302010-01-04 16:16:05 +0100868 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200869 }
Emeric Brun107ca302010-01-04 16:16:05 +0100870 prev_type = fetch->out_type;
Willy Tarreau833cc792013-07-24 15:34:19 +0200871
Vincent Bernat02779b62016-04-03 13:48:43 +0200872 expr = calloc(1, sizeof(*expr));
Emeric Brun485479d2010-09-23 18:02:19 +0200873 if (!expr)
874 goto out_error;
Emeric Brun107ca302010-01-04 16:16:05 +0100875
876 LIST_INIT(&(expr->conv_exprs));
877 expr->fetch = fetch;
Willy Tarreau2e845be2012-10-19 19:49:09 +0200878 expr->arg_p = empty_arg_list;
Emeric Brun107ca302010-01-04 16:16:05 +0100879
Willy Tarreau689a1df2013-12-13 00:40:11 +0100880 /* Note that we call the argument parser even with an empty string,
881 * this allows it to automatically create entries for mandatory
882 * implicit arguments (eg: local proxy name).
883 */
884 al->kw = expr->fetch->kw;
885 al->conv = NULL;
886 if (make_arg_list(endw, endt - endw, fetch->arg_mask, &expr->arg_p, err_msg, NULL, &err_arg, al) < 0) {
887 memprintf(err_msg, "fetch method '%s' : %s", fkw, *err_msg);
888 goto out_error;
889 }
Willy Tarreau2e845be2012-10-19 19:49:09 +0200890
Willy Tarreau689a1df2013-12-13 00:40:11 +0100891 if (!expr->arg_p) {
892 expr->arg_p = empty_arg_list;
Emeric Brun485479d2010-09-23 18:02:19 +0200893 }
Willy Tarreau689a1df2013-12-13 00:40:11 +0100894 else if (fetch->val_args && !fetch->val_args(expr->arg_p, err_msg)) {
895 memprintf(err_msg, "invalid args in fetch method '%s' : %s", fkw, *err_msg);
Emeric Brun485479d2010-09-23 18:02:19 +0200896 goto out_error;
Emeric Brun107ca302010-01-04 16:16:05 +0100897 }
898
Willy Tarreau833cc792013-07-24 15:34:19 +0200899 /* Now process the converters if any. We have two supported syntaxes
900 * for the converters, which can be combined :
901 * - comma-delimited list of converters just after the keyword and args ;
902 * - one converter per keyword
903 * The combination allows to have each keyword being a comma-delimited
904 * series of converters.
905 *
906 * We want to process the former first, then the latter. For this we start
907 * from the beginning of the supposed place in the exiting conv chain, which
908 * starts at the last comma (endt).
909 */
910
911 while (1) {
Willy Tarreau12785782012-04-27 21:37:17 +0200912 struct sample_conv_expr *conv_expr;
Emeric Brun107ca302010-01-04 16:16:05 +0100913
Willy Tarreau833cc792013-07-24 15:34:19 +0200914 if (*endt == ')') /* skip last closing parenthesis */
915 endt++;
Emeric Brun107ca302010-01-04 16:16:05 +0100916
Willy Tarreau833cc792013-07-24 15:34:19 +0200917 if (*endt && *endt != ',') {
918 if (ckw)
Willy Tarreau97108e02016-11-25 07:33:24 +0100919 memprintf(err_msg, "missing comma after converter '%s'", ckw);
Willy Tarreau833cc792013-07-24 15:34:19 +0200920 else
Willy Tarreau975c1782013-12-12 23:16:54 +0100921 memprintf(err_msg, "missing comma after fetch keyword '%s'", fkw);
Emeric Brun107ca302010-01-04 16:16:05 +0100922 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200923 }
Emeric Brun107ca302010-01-04 16:16:05 +0100924
Willy Tarreau833cc792013-07-24 15:34:19 +0200925 while (*endt == ',') /* then trailing commas */
926 endt++;
927
Willy Tarreau97108e02016-11-25 07:33:24 +0100928 begw = endt; /* start of converter */
Willy Tarreau833cc792013-07-24 15:34:19 +0200929
930 if (!*begw) {
931 /* none ? skip to next string */
932 (*idx)++;
933 begw = str[*idx];
934 if (!begw || !*begw)
935 break;
936 }
937
938 for (endw = begw; *endw && *endw != '(' && *endw != ','; endw++);
939
940 free(ckw);
941 ckw = my_strndup(begw, endw - begw);
942
943 conv = find_sample_conv(begw, endw - begw);
944 if (!conv) {
945 /* we found an isolated keyword that we don't know, it's not ours */
946 if (begw == str[*idx])
947 break;
Willy Tarreau97108e02016-11-25 07:33:24 +0100948 memprintf(err_msg, "unknown converter '%s'", ckw);
Willy Tarreau833cc792013-07-24 15:34:19 +0200949 goto out_error;
950 }
Emeric Brun107ca302010-01-04 16:16:05 +0100951
Willy Tarreau833cc792013-07-24 15:34:19 +0200952 endt = endw;
953 if (*endt == '(') {
954 /* look for the end of this term */
955 while (*endt && *endt != ')')
956 endt++;
957 if (*endt != ')') {
Willy Tarreau97108e02016-11-25 07:33:24 +0100958 memprintf(err_msg, "syntax error: missing ')' after converter '%s'", ckw);
Willy Tarreau833cc792013-07-24 15:34:19 +0200959 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200960 }
Willy Tarreau833cc792013-07-24 15:34:19 +0200961 }
962
963 if (conv->in_type >= SMP_TYPES || conv->out_type >= SMP_TYPES) {
Willy Tarreau97108e02016-11-25 07:33:24 +0100964 memprintf(err_msg, "returns type of converter '%s' is unknown", ckw);
Emeric Brun107ca302010-01-04 16:16:05 +0100965 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200966 }
Emeric Brun107ca302010-01-04 16:16:05 +0100967
968 /* If impossible type conversion */
Willy Tarreau12785782012-04-27 21:37:17 +0200969 if (!sample_casts[prev_type][conv->in_type]) {
Willy Tarreau97108e02016-11-25 07:33:24 +0100970 memprintf(err_msg, "converter '%s' cannot be applied", ckw);
Emeric Brun107ca302010-01-04 16:16:05 +0100971 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200972 }
Emeric Brun107ca302010-01-04 16:16:05 +0100973
974 prev_type = conv->out_type;
Vincent Bernat02779b62016-04-03 13:48:43 +0200975 conv_expr = calloc(1, sizeof(*conv_expr));
Emeric Brun485479d2010-09-23 18:02:19 +0200976 if (!conv_expr)
977 goto out_error;
Emeric Brun107ca302010-01-04 16:16:05 +0100978
979 LIST_ADDQ(&(expr->conv_exprs), &(conv_expr->list));
980 conv_expr->conv = conv;
981
Willy Tarreau833cc792013-07-24 15:34:19 +0200982 if (endt != endw) {
Willy Tarreaub27c0d32012-04-20 16:04:47 +0200983 int err_arg;
Willy Tarreau21d68a62012-04-20 15:52:36 +0200984
Willy Tarreau9fcb9842012-04-20 14:45:49 +0200985 if (!conv->arg_mask) {
Willy Tarreau97108e02016-11-25 07:33:24 +0100986 memprintf(err_msg, "converter '%s' does not support any args", ckw);
Emeric Brun485479d2010-09-23 18:02:19 +0200987 goto out_error;
988 }
Willy Tarreau9e92d322010-01-26 17:58:06 +0100989
Willy Tarreaua4312fa2013-04-02 16:34:32 +0200990 al->kw = expr->fetch->kw;
991 al->conv = conv_expr->conv->kw;
Willy Tarreau975c1782013-12-12 23:16:54 +0100992 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 +0100993 memprintf(err_msg, "invalid arg %d in converter '%s' : %s", err_arg+1, ckw, *err_msg);
Willy Tarreau21d68a62012-04-20 15:52:36 +0200994 goto out_error;
995 }
996
Willy Tarreau2e845be2012-10-19 19:49:09 +0200997 if (!conv_expr->arg_p)
998 conv_expr->arg_p = empty_arg_list;
999
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +01001000 if (conv->val_args && !conv->val_args(conv_expr->arg_p, conv, file, line, err_msg)) {
Willy Tarreau97108e02016-11-25 07:33:24 +01001001 memprintf(err_msg, "invalid args in converter '%s' : %s", ckw, *err_msg);
Emeric Brun485479d2010-09-23 18:02:19 +02001002 goto out_error;
1003 }
1004 }
Willy Tarreau833cc792013-07-24 15:34:19 +02001005 else if (ARGM(conv->arg_mask)) {
Willy Tarreau97108e02016-11-25 07:33:24 +01001006 memprintf(err_msg, "missing args for converter '%s'", ckw);
Emeric Brun485479d2010-09-23 18:02:19 +02001007 goto out_error;
Emeric Brun107ca302010-01-04 16:16:05 +01001008 }
1009 }
Emeric Brun485479d2010-09-23 18:02:19 +02001010
Willy Tarreau833cc792013-07-24 15:34:19 +02001011 out:
1012 free(fkw);
1013 free(ckw);
Emeric Brun107ca302010-01-04 16:16:05 +01001014 return expr;
1015
1016out_error:
Willy Tarreau12785782012-04-27 21:37:17 +02001017 /* TODO: prune_sample_expr(expr); */
Willy Tarreau833cc792013-07-24 15:34:19 +02001018 expr = NULL;
1019 goto out;
Emeric Brun107ca302010-01-04 16:16:05 +01001020}
1021
1022/*
Willy Tarreau12785782012-04-27 21:37:17 +02001023 * Process a fetch + format conversion of defined by the sample expression <expr>
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001024 * on request or response considering the <opt> parameter.
Willy Tarreau12785782012-04-27 21:37:17 +02001025 * Returns a pointer on a typed sample structure containing the result or NULL if
1026 * sample is not found or when format conversion failed.
Emeric Brun107ca302010-01-04 16:16:05 +01001027 * If <p> is not null, function returns results in structure pointed by <p>.
Willy Tarreau12785782012-04-27 21:37:17 +02001028 * If <p> is null, functions returns a pointer on a static sample structure.
Willy Tarreaub8c8f1f2012-04-23 22:38:26 +02001029 *
1030 * Note: the fetch functions are required to properly set the return type. The
1031 * conversion functions must do so too. However the cast functions do not need
1032 * to since they're made to cast mutiple types according to what is required.
Willy Tarreau6bcb0a82014-07-30 08:56:35 +02001033 *
1034 * The caller may indicate in <opt> if it considers the result final or not.
1035 * The caller needs to check the SMP_F_MAY_CHANGE flag in p->flags to verify
1036 * if the result is stable or not, according to the following table :
1037 *
1038 * return MAY_CHANGE FINAL Meaning for the sample
1039 * NULL 0 * Not present and will never be (eg: header)
1040 * NULL 1 0 Not present yet, could change (eg: POST param)
1041 * NULL 1 1 Not present yet, will not change anymore
1042 * smp 0 * Present and will not change (eg: header)
1043 * smp 1 0 Present, may change (eg: request length)
1044 * smp 1 1 Present, last known value (eg: request length)
Emeric Brun107ca302010-01-04 16:16:05 +01001045 */
Willy Tarreau192252e2015-04-04 01:47:55 +02001046struct sample *sample_process(struct proxy *px, struct session *sess,
1047 struct stream *strm, unsigned int opt,
Willy Tarreau12785782012-04-27 21:37:17 +02001048 struct sample_expr *expr, struct sample *p)
Emeric Brun107ca302010-01-04 16:16:05 +01001049{
Willy Tarreau12785782012-04-27 21:37:17 +02001050 struct sample_conv_expr *conv_expr;
Emeric Brun107ca302010-01-04 16:16:05 +01001051
Willy Tarreau18387e22013-07-25 12:02:38 +02001052 if (p == NULL) {
Willy Tarreaub4a88f02012-04-23 21:35:11 +02001053 p = &temp_smp;
Willy Tarreau6c616e02014-06-25 16:56:41 +02001054 memset(p, 0, sizeof(*p));
Willy Tarreau18387e22013-07-25 12:02:38 +02001055 }
Emeric Brun107ca302010-01-04 16:16:05 +01001056
Willy Tarreau1777ea62016-03-10 16:15:46 +01001057 smp_set_owner(p, px, sess, strm, opt);
Thierry FOURNIER0786d052015-05-11 15:42:45 +02001058 if (!expr->fetch->process(expr->arg_p, p, expr->fetch->kw, expr->fetch->private))
Emeric Brun107ca302010-01-04 16:16:05 +01001059 return NULL;
1060
Emeric Brun107ca302010-01-04 16:16:05 +01001061 list_for_each_entry(conv_expr, &expr->conv_exprs, list) {
Willy Tarreau12e50112012-04-25 17:21:49 +02001062 /* we want to ensure that p->type can be casted into
1063 * conv_expr->conv->in_type. We have 3 possibilities :
1064 * - NULL => not castable.
1065 * - c_none => nothing to do (let's optimize it)
1066 * - other => apply cast and prepare to fail
1067 */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001068 if (!sample_casts[p->data.type][conv_expr->conv->in_type])
Willy Tarreau12e50112012-04-25 17:21:49 +02001069 return NULL;
1070
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001071 if (sample_casts[p->data.type][conv_expr->conv->in_type] != c_none &&
1072 !sample_casts[p->data.type][conv_expr->conv->in_type](p))
Emeric Brun107ca302010-01-04 16:16:05 +01001073 return NULL;
1074
Willy Tarreau12e50112012-04-25 17:21:49 +02001075 /* OK cast succeeded */
1076
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001077 if (!conv_expr->conv->process(conv_expr->arg_p, p, conv_expr->conv->private))
Emeric Brun107ca302010-01-04 16:16:05 +01001078 return NULL;
Emeric Brun107ca302010-01-04 16:16:05 +01001079 }
1080 return p;
1081}
1082
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001083/*
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001084 * Resolve all remaining arguments in proxy <p>. Returns the number of
1085 * errors or 0 if everything is fine.
1086 */
1087int smp_resolve_args(struct proxy *p)
1088{
1089 struct arg_list *cur, *bak;
1090 const char *ctx, *where;
1091 const char *conv_ctx, *conv_pre, *conv_pos;
1092 struct userlist *ul;
Willy Tarreau46947782015-01-19 19:00:58 +01001093 struct my_regex *reg;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001094 struct arg *arg;
1095 int cfgerr = 0;
Willy Tarreau46947782015-01-19 19:00:58 +01001096 int rflags;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001097
1098 list_for_each_entry_safe(cur, bak, &p->conf.args.list, list) {
1099 struct proxy *px;
1100 struct server *srv;
1101 char *pname, *sname;
Willy Tarreau46947782015-01-19 19:00:58 +01001102 char *err;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001103
1104 arg = cur->arg;
1105
1106 /* prepare output messages */
1107 conv_pre = conv_pos = conv_ctx = "";
1108 if (cur->conv) {
1109 conv_ctx = cur->conv;
1110 conv_pre = "conversion keyword '";
1111 conv_pos = "' for ";
1112 }
1113
1114 where = "in";
1115 ctx = "sample fetch keyword";
1116 switch (cur->ctx) {
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001117 case ARGC_STK: where = "in stick rule in"; break;
1118 case ARGC_TRK: where = "in tracking rule in"; break;
1119 case ARGC_LOG: where = "in log-format string in"; break;
1120 case ARGC_LOGSD: where = "in log-format-sd string in"; break;
1121 case ARGC_HRQ: where = "in http-request header format string in"; break;
1122 case ARGC_HRS: where = "in http-response header format string in"; break;
1123 case ARGC_UIF: where = "in unique-id-format string in"; break;
1124 case ARGC_RDR: where = "in redirect format string in"; break;
1125 case ARGC_CAP: where = "in capture rule in"; break;
1126 case ARGC_ACL: ctx = "ACL keyword"; break;
1127 case ARGC_SRV: where = "in server directive in"; break;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001128 case ARGC_SPOE: where = "in spoe-message directive in"; break;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001129 }
1130
1131 /* set a few default settings */
1132 px = p;
1133 pname = p->id;
1134
1135 switch (arg->type) {
1136 case ARGT_SRV:
1137 if (!arg->data.str.len) {
1138 Alert("parsing [%s:%d] : missing server name in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1139 cur->file, cur->line,
1140 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
1141 cfgerr++;
1142 continue;
1143 }
1144
1145 /* we support two formats : "bck/srv" and "srv" */
1146 sname = strrchr(arg->data.str.str, '/');
1147
1148 if (sname) {
1149 *sname++ = '\0';
1150 pname = arg->data.str.str;
1151
Willy Tarreau9e0bb102015-05-26 11:24:42 +02001152 px = proxy_be_by_name(pname);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001153 if (!px) {
1154 Alert("parsing [%s:%d] : unable to find proxy '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1155 cur->file, cur->line, pname,
1156 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
1157 cfgerr++;
1158 break;
1159 }
1160 }
1161 else
1162 sname = arg->data.str.str;
1163
1164 srv = findserver(px, sname);
1165 if (!srv) {
1166 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",
1167 cur->file, cur->line, sname, pname,
1168 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
1169 cfgerr++;
1170 break;
1171 }
1172
1173 free(arg->data.str.str);
1174 arg->data.str.str = NULL;
1175 arg->unresolved = 0;
1176 arg->data.srv = srv;
1177 break;
1178
1179 case ARGT_FE:
1180 if (arg->data.str.len) {
1181 pname = arg->data.str.str;
Willy Tarreau9e0bb102015-05-26 11:24:42 +02001182 px = proxy_fe_by_name(pname);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001183 }
1184
1185 if (!px) {
1186 Alert("parsing [%s:%d] : unable to find frontend '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1187 cur->file, cur->line, pname,
1188 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
1189 cfgerr++;
1190 break;
1191 }
1192
1193 if (!(px->cap & PR_CAP_FE)) {
1194 Alert("parsing [%s:%d] : proxy '%s', referenced in arg %d of %s%s%s%s '%s' %s proxy '%s', has not frontend capability.\n",
1195 cur->file, cur->line, pname,
1196 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
1197 cfgerr++;
1198 break;
1199 }
1200
1201 free(arg->data.str.str);
1202 arg->data.str.str = NULL;
1203 arg->unresolved = 0;
1204 arg->data.prx = px;
1205 break;
1206
1207 case ARGT_BE:
1208 if (arg->data.str.len) {
1209 pname = arg->data.str.str;
Willy Tarreau9e0bb102015-05-26 11:24:42 +02001210 px = proxy_be_by_name(pname);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001211 }
1212
1213 if (!px) {
1214 Alert("parsing [%s:%d] : unable to find backend '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1215 cur->file, cur->line, pname,
1216 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
1217 cfgerr++;
1218 break;
1219 }
1220
1221 if (!(px->cap & PR_CAP_BE)) {
1222 Alert("parsing [%s:%d] : proxy '%s', referenced in arg %d of %s%s%s%s '%s' %s proxy '%s', has not backend capability.\n",
1223 cur->file, cur->line, pname,
1224 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
1225 cfgerr++;
1226 break;
1227 }
1228
1229 free(arg->data.str.str);
1230 arg->data.str.str = NULL;
1231 arg->unresolved = 0;
1232 arg->data.prx = px;
1233 break;
1234
1235 case ARGT_TAB:
1236 if (arg->data.str.len) {
1237 pname = arg->data.str.str;
Willy Tarreaue2dc1fa2015-05-26 12:08:07 +02001238 px = proxy_tbl_by_name(pname);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001239 }
1240
1241 if (!px) {
1242 Alert("parsing [%s:%d] : unable to find table '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1243 cur->file, cur->line, pname,
1244 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
1245 cfgerr++;
1246 break;
1247 }
1248
1249 if (!px->table.size) {
1250 Alert("parsing [%s:%d] : no table in proxy '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1251 cur->file, cur->line, pname,
1252 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
1253 cfgerr++;
1254 break;
1255 }
1256
1257 free(arg->data.str.str);
1258 arg->data.str.str = NULL;
1259 arg->unresolved = 0;
1260 arg->data.prx = px;
1261 break;
1262
1263 case ARGT_USR:
1264 if (!arg->data.str.len) {
1265 Alert("parsing [%s:%d] : missing userlist name in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1266 cur->file, cur->line,
1267 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
1268 cfgerr++;
1269 break;
1270 }
1271
1272 if (p->uri_auth && p->uri_auth->userlist &&
1273 !strcmp(p->uri_auth->userlist->name, arg->data.str.str))
1274 ul = p->uri_auth->userlist;
1275 else
1276 ul = auth_find_userlist(arg->data.str.str);
1277
1278 if (!ul) {
1279 Alert("parsing [%s:%d] : unable to find userlist '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1280 cur->file, cur->line, arg->data.str.str,
1281 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
1282 cfgerr++;
1283 break;
1284 }
1285
1286 free(arg->data.str.str);
1287 arg->data.str.str = NULL;
1288 arg->unresolved = 0;
1289 arg->data.usr = ul;
1290 break;
Willy Tarreau46947782015-01-19 19:00:58 +01001291
1292 case ARGT_REG:
1293 if (!arg->data.str.len) {
1294 Alert("parsing [%s:%d] : missing regex in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1295 cur->file, cur->line,
1296 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
1297 cfgerr++;
1298 continue;
1299 }
1300
1301 reg = calloc(1, sizeof(*reg));
1302 if (!reg) {
1303 Alert("parsing [%s:%d] : not enough memory to build regex in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1304 cur->file, cur->line,
1305 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
1306 cfgerr++;
1307 continue;
1308 }
1309
1310 rflags = 0;
1311 rflags |= (arg->type_flags & ARGF_REG_ICASE) ? REG_ICASE : 0;
1312 err = NULL;
1313
Willy Tarreaud817e462015-01-23 20:23:17 +01001314 if (!regex_comp(arg->data.str.str, reg, !(rflags & REG_ICASE), 1 /* capture substr */, &err)) {
Willy Tarreau46947782015-01-19 19:00:58 +01001315 Alert("parsing [%s:%d] : error in regex '%s' in arg %d of %s%s%s%s '%s' %s proxy '%s' : %s.\n",
1316 cur->file, cur->line,
1317 arg->data.str.str,
1318 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id, err);
1319 cfgerr++;
1320 continue;
1321 }
1322
1323 free(arg->data.str.str);
1324 arg->data.str.str = NULL;
1325 arg->unresolved = 0;
1326 arg->data.reg = reg;
1327 break;
1328
1329
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001330 }
1331
1332 LIST_DEL(&cur->list);
1333 free(cur);
1334 } /* end of args processing */
1335
1336 return cfgerr;
1337}
1338
1339/*
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001340 * Process a fetch + format conversion as defined by the sample expression
1341 * <expr> on request or response considering the <opt> parameter. The output is
Adis Nezirovic79beb242015-07-06 15:41:02 +02001342 * not explicitly set to <smp_type>, but shall be compatible with it as
1343 * specified by 'sample_casts' table. If a stable sample can be fetched, or an
1344 * unstable one when <opt> contains SMP_OPT_FINAL, the sample is converted and
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001345 * returned without the SMP_F_MAY_CHANGE flag. If an unstable sample is found
1346 * and <opt> does not contain SMP_OPT_FINAL, then the sample is returned as-is
1347 * with its SMP_F_MAY_CHANGE flag so that the caller can check it and decide to
1348 * take actions (eg: wait longer). If a sample could not be found or could not
Willy Tarreau6bcb0a82014-07-30 08:56:35 +02001349 * be converted, NULL is returned. The caller MUST NOT use the sample if the
1350 * SMP_F_MAY_CHANGE flag is present, as it is used only as a hint that there is
1351 * still hope to get it after waiting longer, and is not converted to string.
1352 * The possible output combinations are the following :
1353 *
1354 * return MAY_CHANGE FINAL Meaning for the sample
1355 * NULL * * Not present and will never be (eg: header)
1356 * smp 0 * Final value converted (eg: header)
1357 * smp 1 0 Not present yet, may appear later (eg: header)
1358 * smp 1 1 never happens (either flag is cleared on output)
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001359 */
Adis Nezirovic79beb242015-07-06 15:41:02 +02001360struct sample *sample_fetch_as_type(struct proxy *px, struct session *sess,
Willy Tarreau192252e2015-04-04 01:47:55 +02001361 struct stream *strm, unsigned int opt,
Adis Nezirovic79beb242015-07-06 15:41:02 +02001362 struct sample_expr *expr, int smp_type)
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001363{
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001364 struct sample *smp = &temp_smp;
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001365
Willy Tarreau6c616e02014-06-25 16:56:41 +02001366 memset(smp, 0, sizeof(*smp));
1367
Willy Tarreau192252e2015-04-04 01:47:55 +02001368 if (!sample_process(px, sess, strm, opt, expr, smp)) {
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001369 if ((smp->flags & SMP_F_MAY_CHANGE) && !(opt & SMP_OPT_FINAL))
1370 return smp;
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001371 return NULL;
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001372 }
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001373
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001374 if (!sample_casts[smp->data.type][smp_type])
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001375 return NULL;
1376
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001377 if (!sample_casts[smp->data.type][smp_type](smp))
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001378 return NULL;
1379
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001380 smp->flags &= ~SMP_F_MAY_CHANGE;
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001381 return smp;
1382}
1383
Christopher Faulet476e5d02016-10-26 11:34:47 +02001384static void release_sample_arg(struct arg *p)
1385{
1386 struct arg *p_back = p;
1387
1388 if (!p)
1389 return;
1390
1391 while (p->type != ARGT_STOP) {
1392 if (p->type == ARGT_STR || p->unresolved) {
1393 free(p->data.str.str);
1394 p->data.str.str = NULL;
1395 p->unresolved = 0;
1396 }
1397 else if (p->type == ARGT_REG) {
1398 if (p->data.reg) {
1399 regex_free(p->data.reg);
1400 free(p->data.reg);
1401 p->data.reg = NULL;
1402 }
1403 }
1404 p++;
1405 }
1406
1407 if (p_back != empty_arg_list)
1408 free(p_back);
1409}
1410
1411void release_sample_expr(struct sample_expr *expr)
1412{
1413 struct sample_conv_expr *conv_expr, *conv_exprb;
1414
1415 if (!expr)
1416 return;
1417
1418 list_for_each_entry_safe(conv_expr, conv_exprb, &expr->conv_exprs, list)
1419 release_sample_arg(conv_expr->arg_p);
1420 release_sample_arg(expr->arg_p);
1421 free(expr);
1422}
1423
Emeric Brun107ca302010-01-04 16:16:05 +01001424/*****************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +02001425/* Sample format convert functions */
Willy Tarreaub8c8f1f2012-04-23 22:38:26 +02001426/* These functions set the data type on return. */
Emeric Brun107ca302010-01-04 16:16:05 +01001427/*****************************************************************/
1428
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001429#ifdef DEBUG_EXPR
1430static int sample_conv_debug(const struct arg *arg_p, struct sample *smp, void *private)
1431{
1432 int i;
1433 struct sample tmp;
1434
1435 if (!(global.mode & MODE_QUIET) || (global.mode & (MODE_VERBOSE | MODE_STARTING))) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001436 fprintf(stderr, "[debug converter] type: %s ", smp_to_type[smp->data.type]);
1437 if (!sample_casts[smp->data.type][SMP_T_STR]) {
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001438 fprintf(stderr, "(undisplayable)");
1439 } else {
1440
1441 /* Copy sample fetch. This put the sample as const, the
1442 * cast will copy data if a transformation is required.
1443 */
1444 memcpy(&tmp, smp, sizeof(struct sample));
1445 tmp.flags = SMP_F_CONST;
1446
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001447 if (!sample_casts[smp->data.type][SMP_T_STR](&tmp))
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001448 fprintf(stderr, "(undisplayable)");
1449
1450 else {
1451 /* Display the displayable chars*. */
1452 fprintf(stderr, "<");
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001453 for (i = 0; i < tmp.data.u.str.len; i++) {
1454 if (isprint(tmp.data.u.str.str[i]))
1455 fputc(tmp.data.u.str.str[i], stderr);
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001456 else
1457 fputc('.', stderr);
1458 }
1459 }
1460 fprintf(stderr, ">\n");
1461 }
1462 }
1463 return 1;
1464}
1465#endif
1466
Holger Just1bfc24b2017-05-06 00:56:53 +02001467static int sample_conv_base642bin(const struct arg *arg_p, struct sample *smp, void *private)
1468{
1469 struct chunk *trash = get_trash_chunk();
1470 int bin_len;
1471
1472 trash->len = 0;
1473 bin_len = base64dec(smp->data.u.str.str, smp->data.u.str.len, trash->str, trash->size);
1474 if (bin_len < 0)
1475 return 0;
1476
1477 trash->len = bin_len;
1478 smp->data.u.str = *trash;
1479 smp->data.type = SMP_T_BIN;
1480 smp->flags &= ~SMP_F_CONST;
1481 return 1;
1482}
1483
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001484static int sample_conv_bin2base64(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brun53d1a982014-04-30 18:21:37 +02001485{
1486 struct chunk *trash = get_trash_chunk();
1487 int b64_len;
1488
1489 trash->len = 0;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001490 b64_len = a2base64(smp->data.u.str.str, smp->data.u.str.len, trash->str, trash->size);
Emeric Brun53d1a982014-04-30 18:21:37 +02001491 if (b64_len < 0)
1492 return 0;
1493
1494 trash->len = b64_len;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001495 smp->data.u.str = *trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001496 smp->data.type = SMP_T_STR;
Emeric Brun53d1a982014-04-30 18:21:37 +02001497 smp->flags &= ~SMP_F_CONST;
1498 return 1;
1499}
1500
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001501static int sample_conv_bin2hex(const struct arg *arg_p, struct sample *smp, void *private)
Thierry FOURNIER2f49d6d2014-03-12 15:01:52 +01001502{
1503 struct chunk *trash = get_trash_chunk();
1504 unsigned char c;
1505 int ptr = 0;
1506
1507 trash->len = 0;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001508 while (ptr < smp->data.u.str.len && trash->len <= trash->size - 2) {
1509 c = smp->data.u.str.str[ptr++];
Thierry FOURNIER2f49d6d2014-03-12 15:01:52 +01001510 trash->str[trash->len++] = hextab[(c >> 4) & 0xF];
1511 trash->str[trash->len++] = hextab[c & 0xF];
1512 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001513 smp->data.u.str = *trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001514 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01001515 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIER2f49d6d2014-03-12 15:01:52 +01001516 return 1;
1517}
1518
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001519/* hashes the binary input into a 32-bit unsigned int */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001520static int sample_conv_djb2(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001521{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001522 smp->data.u.sint = hash_djb2(smp->data.u.str.str, smp->data.u.str.len);
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001523 if (arg_p && arg_p->data.sint)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001524 smp->data.u.sint = full_hash(smp->data.u.sint);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001525 smp->data.type = SMP_T_SINT;
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001526 return 1;
1527}
1528
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001529static int sample_conv_str2lower(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brun107ca302010-01-04 16:16:05 +01001530{
1531 int i;
1532
Willy Tarreauf0645dc2016-08-09 14:29:38 +02001533 if (!smp_make_rw(smp))
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01001534 return 0;
1535
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001536 for (i = 0; i < smp->data.u.str.len; i++) {
1537 if ((smp->data.u.str.str[i] >= 'A') && (smp->data.u.str.str[i] <= 'Z'))
1538 smp->data.u.str.str[i] += 'a' - 'A';
Emeric Brun107ca302010-01-04 16:16:05 +01001539 }
1540 return 1;
1541}
1542
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001543static int sample_conv_str2upper(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brun107ca302010-01-04 16:16:05 +01001544{
1545 int i;
1546
Willy Tarreauf0645dc2016-08-09 14:29:38 +02001547 if (!smp_make_rw(smp))
Emeric Brun485479d2010-09-23 18:02:19 +02001548 return 0;
1549
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001550 for (i = 0; i < smp->data.u.str.len; i++) {
1551 if ((smp->data.u.str.str[i] >= 'a') && (smp->data.u.str.str[i] <= 'z'))
1552 smp->data.u.str.str[i] += 'A' - 'a';
Emeric Brun107ca302010-01-04 16:16:05 +01001553 }
1554 return 1;
1555}
1556
Willy Tarreauf9954102012-04-20 14:03:29 +02001557/* takes the netmask in arg_p */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001558static int sample_conv_ipmask(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud31d6eb2010-01-26 18:01:41 +01001559{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001560 smp->data.u.ipv4.s_addr &= arg_p->data.ipv4.s_addr;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001561 smp->data.type = SMP_T_IPV4;
Willy Tarreaud31d6eb2010-01-26 18:01:41 +01001562 return 1;
1563}
1564
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001565/* takes an UINT value on input supposed to represent the time since EPOCH,
1566 * adds an optional offset found in args[1] and emits a string representing
1567 * the local time in the format specified in args[1] using strftime().
1568 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001569static int sample_conv_ltime(const struct arg *args, struct sample *smp, void *private)
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001570{
1571 struct chunk *temp;
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001572 /* With high numbers, the date returned can be negative, the 55 bits mask prevent this. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001573 time_t curr_date = smp->data.u.sint & 0x007fffffffffffffLL;
Thierry FOURNIERfac9ccf2015-07-08 00:15:20 +02001574 struct tm *tm;
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001575
1576 /* add offset */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001577 if (args[1].type == ARGT_SINT)
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001578 curr_date += args[1].data.sint;
1579
Thierry FOURNIERfac9ccf2015-07-08 00:15:20 +02001580 tm = localtime(&curr_date);
1581 if (!tm)
1582 return 0;
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001583 temp = get_trash_chunk();
Thierry FOURNIERfac9ccf2015-07-08 00:15:20 +02001584 temp->len = strftime(temp->str, temp->size, args[0].data.str.str, tm);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001585 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001586 smp->data.type = SMP_T_STR;
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001587 return 1;
1588}
1589
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001590/* hashes the binary input into a 32-bit unsigned int */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001591static int sample_conv_sdbm(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001592{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001593 smp->data.u.sint = hash_sdbm(smp->data.u.str.str, smp->data.u.str.len);
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001594 if (arg_p && arg_p->data.sint)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001595 smp->data.u.sint = full_hash(smp->data.u.sint);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001596 smp->data.type = SMP_T_SINT;
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001597 return 1;
1598}
1599
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001600/* takes an UINT value on input supposed to represent the time since EPOCH,
1601 * adds an optional offset found in args[1] and emits a string representing
1602 * the UTC date in the format specified in args[1] using strftime().
1603 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001604static int sample_conv_utime(const struct arg *args, struct sample *smp, void *private)
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001605{
1606 struct chunk *temp;
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001607 /* With high numbers, the date returned can be negative, the 55 bits mask prevent this. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001608 time_t curr_date = smp->data.u.sint & 0x007fffffffffffffLL;
Thierry FOURNIERfac9ccf2015-07-08 00:15:20 +02001609 struct tm *tm;
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001610
1611 /* add offset */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001612 if (args[1].type == ARGT_SINT)
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001613 curr_date += args[1].data.sint;
1614
Thierry FOURNIERfac9ccf2015-07-08 00:15:20 +02001615 tm = gmtime(&curr_date);
1616 if (!tm)
1617 return 0;
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001618 temp = get_trash_chunk();
Thierry FOURNIERfac9ccf2015-07-08 00:15:20 +02001619 temp->len = strftime(temp->str, temp->size, args[0].data.str.str, tm);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001620 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001621 smp->data.type = SMP_T_STR;
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001622 return 1;
1623}
1624
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001625/* hashes the binary input into a 32-bit unsigned int */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001626static int sample_conv_wt6(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001627{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001628 smp->data.u.sint = hash_wt6(smp->data.u.str.str, smp->data.u.str.len);
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001629 if (arg_p && arg_p->data.sint)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001630 smp->data.u.sint = full_hash(smp->data.u.sint);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001631 smp->data.type = SMP_T_SINT;
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001632 return 1;
1633}
1634
Thierry FOURNIER01e09742016-12-26 11:46:11 +01001635/* hashes the binary input into a 32-bit unsigned int using xxh.
1636 * The seed of the hash defaults to 0 but can be changd in argument 1.
1637 */
1638static int sample_conv_xxh32(const struct arg *arg_p, struct sample *smp, void *private)
1639{
1640 unsigned int seed;
1641
1642 if (arg_p && arg_p->data.sint)
1643 seed = arg_p->data.sint;
1644 else
1645 seed = 0;
1646 smp->data.u.sint = XXH32(smp->data.u.str.str, smp->data.u.str.len, seed);
1647 smp->data.type = SMP_T_SINT;
1648 return 1;
1649}
1650
1651/* hashes the binary input into a 64-bit unsigned int using xxh.
1652 * In fact, the function returns a 64 bit unsigned, but the sample
1653 * storage of haproxy only proposes 64-bits signed, so the value is
1654 * cast as signed. This cast doesn't impact the hash repartition.
1655 * The seed of the hash defaults to 0 but can be changd in argument 1.
1656 */
1657static int sample_conv_xxh64(const struct arg *arg_p, struct sample *smp, void *private)
1658{
1659 unsigned long long int seed;
1660
1661 if (arg_p && arg_p->data.sint)
1662 seed = (unsigned long long int)arg_p->data.sint;
1663 else
1664 seed = 0;
1665 smp->data.u.sint = (long long int)XXH64(smp->data.u.str.str, smp->data.u.str.len, seed);
1666 smp->data.type = SMP_T_SINT;
1667 return 1;
1668}
1669
Willy Tarreau80599772015-01-20 19:35:24 +01001670/* hashes the binary input into a 32-bit unsigned int */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001671static int sample_conv_crc32(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau80599772015-01-20 19:35:24 +01001672{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001673 smp->data.u.sint = hash_crc32(smp->data.u.str.str, smp->data.u.str.len);
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001674 if (arg_p && arg_p->data.sint)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001675 smp->data.u.sint = full_hash(smp->data.u.sint);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001676 smp->data.type = SMP_T_SINT;
Willy Tarreau80599772015-01-20 19:35:24 +01001677 return 1;
1678}
1679
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001680/* This function escape special json characters. The returned string can be
1681 * safely set between two '"' and used as json string. The json string is
1682 * defined like this:
1683 *
1684 * any Unicode character except '"' or '\' or control character
1685 * \", \\, \/, \b, \f, \n, \r, \t, \u + four-hex-digits
1686 *
1687 * The enum input_type contain all the allowed mode for decoding the input
1688 * string.
1689 */
1690enum input_type {
1691 IT_ASCII = 0,
1692 IT_UTF8,
1693 IT_UTF8S,
1694 IT_UTF8P,
1695 IT_UTF8PS,
1696};
1697static int sample_conv_json_check(struct arg *arg, struct sample_conv *conv,
1698 const char *file, int line, char **err)
1699{
1700 if (!arg) {
1701 memprintf(err, "Unexpected empty arg list");
1702 return 0;
1703 }
1704
1705 if (arg->type != ARGT_STR) {
1706 memprintf(err, "Unexpected arg type");
1707 return 0;
1708 }
1709
1710 if (strcmp(arg->data.str.str, "") == 0) {
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001711 arg->type = ARGT_SINT;
1712 arg->data.sint = IT_ASCII;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001713 return 1;
1714 }
1715
1716 else if (strcmp(arg->data.str.str, "ascii") == 0) {
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001717 arg->type = ARGT_SINT;
1718 arg->data.sint = IT_ASCII;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001719 return 1;
1720 }
1721
1722 else if (strcmp(arg->data.str.str, "utf8") == 0) {
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001723 arg->type = ARGT_SINT;
1724 arg->data.sint = IT_UTF8;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001725 return 1;
1726 }
1727
1728 else if (strcmp(arg->data.str.str, "utf8s") == 0) {
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001729 arg->type = ARGT_SINT;
1730 arg->data.sint = IT_UTF8S;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001731 return 1;
1732 }
1733
1734 else if (strcmp(arg->data.str.str, "utf8p") == 0) {
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001735 arg->type = ARGT_SINT;
1736 arg->data.sint = IT_UTF8P;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001737 return 1;
1738 }
1739
1740 else if (strcmp(arg->data.str.str, "utf8ps") == 0) {
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001741 arg->type = ARGT_SINT;
1742 arg->data.sint = IT_UTF8PS;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001743 return 1;
1744 }
1745
Thierry FOURNIER / OZON.IOa69c9122016-11-23 01:13:57 +01001746 memprintf(err, "Unexpected input code type. "
1747 "Allowed value are 'ascii', 'utf8', 'utf8s', 'utf8p' and 'utf8ps'");
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001748 return 0;
1749}
1750
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001751static int sample_conv_json(const struct arg *arg_p, struct sample *smp, void *private)
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001752{
1753 struct chunk *temp;
1754 char _str[7]; /* \u + 4 hex digit + null char for sprintf. */
1755 const char *str;
1756 int len;
1757 enum input_type input_type = IT_ASCII;
1758 unsigned int c;
1759 unsigned int ret;
1760 char *p;
1761
1762 if (arg_p)
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001763 input_type = arg_p->data.sint;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001764
1765 temp = get_trash_chunk();
1766 temp->len = 0;
1767
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001768 p = smp->data.u.str.str;
1769 while (p < smp->data.u.str.str + smp->data.u.str.len) {
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001770
1771 if (input_type == IT_ASCII) {
1772 /* Read input as ASCII. */
1773 c = *(unsigned char *)p;
1774 p++;
1775 }
1776 else {
1777 /* Read input as UTF8. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001778 ret = utf8_next(p, smp->data.u.str.len - ( p - smp->data.u.str.str ), &c);
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001779 p += utf8_return_length(ret);
1780
1781 if (input_type == IT_UTF8 && utf8_return_code(ret) != UTF8_CODE_OK)
1782 return 0;
1783 if (input_type == IT_UTF8S && utf8_return_code(ret) != UTF8_CODE_OK)
1784 continue;
1785 if (input_type == IT_UTF8P && utf8_return_code(ret) & (UTF8_CODE_INVRANGE|UTF8_CODE_BADSEQ))
1786 return 0;
1787 if (input_type == IT_UTF8PS && utf8_return_code(ret) & (UTF8_CODE_INVRANGE|UTF8_CODE_BADSEQ))
1788 continue;
1789
1790 /* Check too big values. */
1791 if ((unsigned int)c > 0xffff) {
1792 if (input_type == IT_UTF8 || input_type == IT_UTF8P)
1793 return 0;
1794 continue;
1795 }
1796 }
1797
1798 /* Convert character. */
1799 if (c == '"') {
1800 len = 2;
1801 str = "\\\"";
1802 }
1803 else if (c == '\\') {
1804 len = 2;
1805 str = "\\\\";
1806 }
1807 else if (c == '/') {
1808 len = 2;
1809 str = "\\/";
1810 }
1811 else if (c == '\b') {
1812 len = 2;
1813 str = "\\b";
1814 }
1815 else if (c == '\f') {
1816 len = 2;
1817 str = "\\f";
1818 }
1819 else if (c == '\r') {
1820 len = 2;
1821 str = "\\r";
1822 }
1823 else if (c == '\n') {
1824 len = 2;
1825 str = "\\n";
1826 }
1827 else if (c == '\t') {
1828 len = 2;
1829 str = "\\t";
1830 }
1831 else if (c > 0xff || !isprint(c)) {
1832 /* isprint generate a segfault if c is too big. The man says that
1833 * c must have the value of an unsigned char or EOF.
1834 */
1835 len = 6;
1836 _str[0] = '\\';
1837 _str[1] = 'u';
1838 snprintf(&_str[2], 5, "%04x", (unsigned short)c);
1839 str = _str;
1840 }
1841 else {
1842 len = 1;
1843 str = (char *)&c;
1844 }
1845
1846 /* Check length */
1847 if (temp->len + len > temp->size)
1848 return 0;
1849
1850 /* Copy string. */
1851 memcpy(temp->str + temp->len, str, len);
1852 temp->len += len;
1853 }
1854
1855 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001856 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001857 smp->data.type = SMP_T_STR;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001858
1859 return 1;
1860}
1861
Emeric Brun54c4ac82014-11-03 15:32:43 +01001862/* This sample function is designed to extract some bytes from an input buffer.
1863 * First arg is the offset.
1864 * Optional second arg is the length to truncate */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001865static int sample_conv_bytes(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brun54c4ac82014-11-03 15:32:43 +01001866{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001867 if (smp->data.u.str.len <= arg_p[0].data.sint) {
1868 smp->data.u.str.len = 0;
Emeric Brun54c4ac82014-11-03 15:32:43 +01001869 return 1;
1870 }
1871
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001872 if (smp->data.u.str.size)
1873 smp->data.u.str.size -= arg_p[0].data.sint;
1874 smp->data.u.str.len -= arg_p[0].data.sint;
1875 smp->data.u.str.str += arg_p[0].data.sint;
Emeric Brun54c4ac82014-11-03 15:32:43 +01001876
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001877 if ((arg_p[1].type == ARGT_SINT) && (arg_p[1].data.sint < smp->data.u.str.len))
1878 smp->data.u.str.len = arg_p[1].data.sint;
Emeric Brun54c4ac82014-11-03 15:32:43 +01001879
1880 return 1;
1881}
1882
Emeric Brunf399b0d2014-11-03 17:07:03 +01001883static int sample_conv_field_check(struct arg *args, struct sample_conv *conv,
1884 const char *file, int line, char **err)
1885{
1886 struct arg *arg = args;
1887
1888 if (!arg) {
1889 memprintf(err, "Unexpected empty arg list");
1890 return 0;
1891 }
1892
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001893 if (arg->type != ARGT_SINT) {
Emeric Brunf399b0d2014-11-03 17:07:03 +01001894 memprintf(err, "Unexpected arg type");
1895 return 0;
1896 }
1897
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001898 if (!arg->data.sint) {
Emeric Brunf399b0d2014-11-03 17:07:03 +01001899 memprintf(err, "Unexpected value 0 for index");
1900 return 0;
1901 }
1902
1903 arg++;
1904
1905 if (arg->type != ARGT_STR) {
1906 memprintf(err, "Unexpected arg type");
1907 return 0;
1908 }
1909
1910 if (!arg->data.str.len) {
1911 memprintf(err, "Empty separators list");
1912 return 0;
1913 }
1914
1915 return 1;
1916}
1917
1918/* This sample function is designed to a return selected part of a string (field).
1919 * First arg is the index of the field (start at 1)
1920 * Second arg is a char list of separators (type string)
1921 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001922static int sample_conv_field(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brunf399b0d2014-11-03 17:07:03 +01001923{
1924 unsigned int field;
1925 char *start, *end;
1926 int i;
1927
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001928 if (!arg_p[0].data.sint)
Emeric Brunf399b0d2014-11-03 17:07:03 +01001929 return 0;
1930
1931 field = 1;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001932 end = start = smp->data.u.str.str;
1933 while (end - smp->data.u.str.str < smp->data.u.str.len) {
Emeric Brunf399b0d2014-11-03 17:07:03 +01001934
1935 for (i = 0 ; i < arg_p[1].data.str.len ; i++) {
1936 if (*end == arg_p[1].data.str.str[i]) {
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001937 if (field == arg_p[0].data.sint)
Emeric Brunf399b0d2014-11-03 17:07:03 +01001938 goto found;
1939 start = end+1;
1940 field++;
1941 break;
1942 }
1943 }
1944 end++;
1945 }
1946
1947 /* Field not found */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001948 if (field != arg_p[0].data.sint) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001949 smp->data.u.str.len = 0;
Emeric Brunf399b0d2014-11-03 17:07:03 +01001950 return 1;
1951 }
1952found:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001953 smp->data.u.str.len = end - start;
Emeric Brunf399b0d2014-11-03 17:07:03 +01001954 /* If ret string is len 0, no need to
1955 change pointers or to update size */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001956 if (!smp->data.u.str.len)
Emeric Brunf399b0d2014-11-03 17:07:03 +01001957 return 1;
1958
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001959 smp->data.u.str.str = start;
Emeric Brunf399b0d2014-11-03 17:07:03 +01001960
1961 /* Compute remaining size if needed
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001962 Note: smp->data.u.str.size cannot be set to 0 */
1963 if (smp->data.u.str.size)
1964 smp->data.u.str.size -= start - smp->data.u.str.str;
Emeric Brunf399b0d2014-11-03 17:07:03 +01001965
1966 return 1;
1967}
1968
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01001969/* This sample function is designed to return a word from a string.
1970 * First arg is the index of the word (start at 1)
1971 * Second arg is a char list of words separators (type string)
1972 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001973static int sample_conv_word(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01001974{
1975 unsigned int word;
1976 char *start, *end;
1977 int i, issep, inword;
1978
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001979 if (!arg_p[0].data.sint)
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01001980 return 0;
1981
1982 word = 0;
1983 inword = 0;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001984 end = start = smp->data.u.str.str;
1985 while (end - smp->data.u.str.str < smp->data.u.str.len) {
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01001986 issep = 0;
1987 for (i = 0 ; i < arg_p[1].data.str.len ; i++) {
1988 if (*end == arg_p[1].data.str.str[i]) {
1989 issep = 1;
1990 break;
1991 }
1992 }
1993 if (!inword) {
1994 if (!issep) {
1995 word++;
1996 start = end;
1997 inword = 1;
1998 }
1999 }
2000 else if (issep) {
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002001 if (word == arg_p[0].data.sint)
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002002 goto found;
2003 inword = 0;
2004 }
2005 end++;
2006 }
2007
2008 /* Field not found */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002009 if (word != arg_p[0].data.sint) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002010 smp->data.u.str.len = 0;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002011 return 1;
2012 }
2013found:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002014 smp->data.u.str.len = end - start;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002015 /* If ret string is len 0, no need to
2016 change pointers or to update size */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002017 if (!smp->data.u.str.len)
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002018 return 1;
2019
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002020 smp->data.u.str.str = start;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002021
2022 /* Compute remaining size if needed
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002023 Note: smp->data.u.str.size cannot be set to 0 */
2024 if (smp->data.u.str.size)
2025 smp->data.u.str.size -= start - smp->data.u.str.str;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002026
2027 return 1;
2028}
2029
Willy Tarreau7eda8492015-01-20 19:47:06 +01002030static int sample_conv_regsub_check(struct arg *args, struct sample_conv *conv,
2031 const char *file, int line, char **err)
2032{
2033 struct arg *arg = args;
2034 char *p;
2035 int len;
2036
2037 /* arg0 is a regex, it uses type_flag for ICASE and global match */
2038 arg[0].type_flags = 0;
2039
2040 if (arg[2].type != ARGT_STR)
2041 return 1;
2042
2043 p = arg[2].data.str.str;
2044 len = arg[2].data.str.len;
2045 while (len) {
2046 if (*p == 'i') {
2047 arg[0].type_flags |= ARGF_REG_ICASE;
2048 }
2049 else if (*p == 'g') {
2050 arg[0].type_flags |= ARGF_REG_GLOB;
2051 }
2052 else {
2053 memprintf(err, "invalid regex flag '%c', only 'i' and 'g' are supported", *p);
2054 return 0;
2055 }
2056 p++;
2057 len--;
2058 }
2059 return 1;
2060}
2061
2062/* This sample function is designed to do the equivalent of s/match/replace/ on
2063 * the input string. It applies a regex and restarts from the last matched
2064 * location until nothing matches anymore. First arg is the regex to apply to
2065 * the input string, second arg is the replacement expression.
2066 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002067static int sample_conv_regsub(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau7eda8492015-01-20 19:47:06 +01002068{
2069 char *start, *end;
2070 struct my_regex *reg = arg_p[0].data.reg;
2071 regmatch_t pmatch[MAX_MATCH];
2072 struct chunk *trash = get_trash_chunk();
2073 int flag, max;
2074 int found;
2075
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002076 start = smp->data.u.str.str;
2077 end = start + smp->data.u.str.len;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002078
2079 flag = 0;
2080 while (1) {
2081 /* check for last round which is used to copy remaining parts
2082 * when not running in global replacement mode.
2083 */
2084 found = 0;
2085 if ((arg_p[0].type_flags & ARGF_REG_GLOB) || !(flag & REG_NOTBOL)) {
2086 /* Note: we can have start == end on empty strings or at the end */
2087 found = regex_exec_match2(reg, start, end - start, MAX_MATCH, pmatch, flag);
2088 }
2089
2090 if (!found)
2091 pmatch[0].rm_so = end - start;
2092
2093 /* copy the heading non-matching part (which may also be the tail if nothing matches) */
2094 max = trash->size - trash->len;
2095 if (max && pmatch[0].rm_so > 0) {
2096 if (max > pmatch[0].rm_so)
2097 max = pmatch[0].rm_so;
2098 memcpy(trash->str + trash->len, start, max);
2099 trash->len += max;
2100 }
2101
2102 if (!found)
2103 break;
2104
2105 /* replace the matching part */
2106 max = trash->size - trash->len;
2107 if (max) {
2108 if (max > arg_p[1].data.str.len)
2109 max = arg_p[1].data.str.len;
2110 memcpy(trash->str + trash->len, arg_p[1].data.str.str, max);
2111 trash->len += max;
2112 }
2113
2114 /* stop here if we're done with this string */
2115 if (start >= end)
2116 break;
2117
2118 /* We have a special case for matches of length 0 (eg: "x*y*").
2119 * These ones are considered to match in front of a character,
2120 * so we have to copy that character and skip to the next one.
2121 */
2122 if (!pmatch[0].rm_eo) {
2123 if (trash->len < trash->size)
2124 trash->str[trash->len++] = start[pmatch[0].rm_eo];
2125 pmatch[0].rm_eo++;
2126 }
2127
2128 start += pmatch[0].rm_eo;
2129 flag |= REG_NOTBOL;
2130 }
2131
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002132 smp->data.u.str = *trash;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002133 return 1;
2134}
2135
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002136/* This function check an operator entry. It expects a string.
2137 * The string can be an integer or a variable name.
2138 */
2139static int check_operator(struct arg *args, struct sample_conv *conv,
2140 const char *file, int line, char **err)
2141{
2142 const char *str;
2143 const char *end;
2144
2145 /* Try to decode a variable. */
2146 if (vars_check_arg(&args[0], NULL))
2147 return 1;
2148
2149 /* Try to convert an integer */
2150 str = args[0].data.str.str;
2151 end = str + strlen(str);
2152 args[0].data.sint = read_int64(&str, end);
2153 if (*str != '\0') {
2154 memprintf(err, "expects an integer or a variable name");
2155 return 0;
2156 }
2157 args[0].type = ARGT_SINT;
2158 return 1;
2159}
2160
Willy Tarreau6204cd92016-03-10 16:33:04 +01002161/* This function returns a sample struct filled with an arg content.
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002162 * If the arg contain an integer, the integer is returned in the
2163 * sample. If the arg contains a variable descriptor, it returns the
2164 * variable value.
2165 *
2166 * This function returns 0 if an error occurs, otherwise it returns 1.
2167 */
Willy Tarreau6204cd92016-03-10 16:33:04 +01002168static inline int sample_conv_var2smp(const struct arg *arg, struct sample *smp)
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002169{
2170 switch (arg->type) {
2171 case ARGT_SINT:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002172 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002173 smp->data.u.sint = arg->data.sint;
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002174 return 1;
2175 case ARGT_VAR:
Willy Tarreau6204cd92016-03-10 16:33:04 +01002176 if (!vars_get_by_desc(&arg->data.var, smp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002177 return 0;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002178 if (!sample_casts[smp->data.type][SMP_T_SINT])
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002179 return 0;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002180 if (!sample_casts[smp->data.type][SMP_T_SINT](smp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002181 return 0;
2182 return 1;
2183 default:
2184 return 0;
2185 }
2186}
2187
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002188/* Takes a SINT on input, applies a binary twos complement and returns the SINT
Willy Tarreau97707872015-01-27 15:12:13 +01002189 * result.
2190 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002191static int sample_conv_binary_cpl(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002192{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002193 smp->data.u.sint = ~smp->data.u.sint;
Willy Tarreau97707872015-01-27 15:12:13 +01002194 return 1;
2195}
2196
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002197/* Takes a SINT on input, applies a binary "and" with the SINT directly in
2198 * arg_p or in the varaible described in arg_p, and returns the SINT result.
Willy Tarreau97707872015-01-27 15:12:13 +01002199 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002200static int sample_conv_binary_and(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002201{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002202 struct sample tmp;
2203
Willy Tarreau7560dd42016-03-10 16:28:58 +01002204 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreau6204cd92016-03-10 16:33:04 +01002205 if (!sample_conv_var2smp(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002206 return 0;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002207 smp->data.u.sint &= tmp.data.u.sint;
Willy Tarreau97707872015-01-27 15:12:13 +01002208 return 1;
2209}
2210
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002211/* Takes a SINT on input, applies a binary "or" with the SINT directly in
2212 * arg_p or in the varaible described in arg_p, and returns the SINT result.
Willy Tarreau97707872015-01-27 15:12:13 +01002213 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002214static int sample_conv_binary_or(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002215{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002216 struct sample tmp;
2217
Willy Tarreau7560dd42016-03-10 16:28:58 +01002218 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreau6204cd92016-03-10 16:33:04 +01002219 if (!sample_conv_var2smp(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002220 return 0;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002221 smp->data.u.sint |= tmp.data.u.sint;
Willy Tarreau97707872015-01-27 15:12:13 +01002222 return 1;
2223}
2224
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002225/* Takes a SINT on input, applies a binary "xor" with the SINT directly in
2226 * arg_p or in the varaible described in arg_p, and returns the SINT result.
Willy Tarreau97707872015-01-27 15:12:13 +01002227 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002228static int sample_conv_binary_xor(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002229{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002230 struct sample tmp;
2231
Willy Tarreau7560dd42016-03-10 16:28:58 +01002232 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreau6204cd92016-03-10 16:33:04 +01002233 if (!sample_conv_var2smp(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002234 return 0;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002235 smp->data.u.sint ^= tmp.data.u.sint;
Willy Tarreau97707872015-01-27 15:12:13 +01002236 return 1;
2237}
2238
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002239static inline long long int arith_add(long long int a, long long int b)
2240{
2241 /* Prevent overflow and makes capped calculus.
2242 * We must ensure that the check calculus doesn't
2243 * exceed the signed 64 bits limits.
2244 *
2245 * +----------+----------+
2246 * | a<0 | a>=0 |
2247 * +------+----------+----------+
2248 * | b<0 | MIN-a>b | no check |
2249 * +------+----------+----------+
2250 * | b>=0 | no check | MAX-a<b |
2251 * +------+----------+----------+
2252 */
2253 if ((a ^ b) >= 0) {
2254 /* signs are differents. */
2255 if (a < 0) {
2256 if (LLONG_MIN - a > b)
2257 return LLONG_MIN;
2258 }
2259 if (LLONG_MAX - a < b)
2260 return LLONG_MAX;
2261 }
2262 return a + b;
2263}
2264
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002265/* Takes a SINT on input, applies an arithmetic "add" with the SINT directly in
2266 * arg_p or in the varaible described in arg_p, and returns the SINT result.
Willy Tarreau97707872015-01-27 15:12:13 +01002267 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002268static int sample_conv_arith_add(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002269{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002270 struct sample tmp;
2271
Willy Tarreau7560dd42016-03-10 16:28:58 +01002272 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreau6204cd92016-03-10 16:33:04 +01002273 if (!sample_conv_var2smp(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002274 return 0;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002275 smp->data.u.sint = arith_add(smp->data.u.sint, tmp.data.u.sint);
Willy Tarreau97707872015-01-27 15:12:13 +01002276 return 1;
2277}
2278
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002279/* Takes a SINT on input, applies an arithmetic "sub" with the SINT directly in
2280 * arg_p or in the varaible described in arg_p, and returns the SINT result.
Willy Tarreau97707872015-01-27 15:12:13 +01002281 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002282static int sample_conv_arith_sub(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002283 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002284{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002285 struct sample tmp;
2286
Willy Tarreau7560dd42016-03-10 16:28:58 +01002287 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreau6204cd92016-03-10 16:33:04 +01002288 if (!sample_conv_var2smp(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002289 return 0;
2290
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002291 /* We cannot represent -LLONG_MIN because abs(LLONG_MIN) is greater
2292 * than abs(LLONG_MAX). So, the following code use LLONG_MAX in place
2293 * of -LLONG_MIN and correct the result.
2294 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002295 if (tmp.data.u.sint == LLONG_MIN) {
2296 smp->data.u.sint = arith_add(smp->data.u.sint, LLONG_MAX);
2297 if (smp->data.u.sint < LLONG_MAX)
2298 smp->data.u.sint++;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002299 return 1;
2300 }
2301
2302 /* standard substraction: we use the "add" function and negate
2303 * the second operand.
2304 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002305 smp->data.u.sint = arith_add(smp->data.u.sint, -tmp.data.u.sint);
Willy Tarreau97707872015-01-27 15:12:13 +01002306 return 1;
2307}
2308
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002309/* Takes a SINT on input, applies an arithmetic "mul" with the SINT directly in
2310 * arg_p or in the varaible described in arg_p, and returns the SINT result.
2311 * If the result makes an overflow, then the largest possible quantity is
2312 * returned.
Willy Tarreau97707872015-01-27 15:12:13 +01002313 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002314static int sample_conv_arith_mul(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002315 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002316{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002317 struct sample tmp;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002318 long long int c;
2319
Willy Tarreau7560dd42016-03-10 16:28:58 +01002320 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreau6204cd92016-03-10 16:33:04 +01002321 if (!sample_conv_var2smp(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002322 return 0;
2323
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002324 /* prevent divide by 0 during the check */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002325 if (!smp->data.u.sint || !tmp.data.u.sint) {
2326 smp->data.u.sint = 0;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002327 return 1;
2328 }
2329
2330 /* The multiply between LLONG_MIN and -1 returns a
2331 * "floting point exception".
2332 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002333 if (smp->data.u.sint == LLONG_MIN && tmp.data.u.sint == -1) {
2334 smp->data.u.sint = LLONG_MAX;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002335 return 1;
2336 }
2337
2338 /* execute standard multiplication. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002339 c = smp->data.u.sint * tmp.data.u.sint;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002340
2341 /* check for overflow and makes capped multiply. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002342 if (smp->data.u.sint != c / tmp.data.u.sint) {
2343 if ((smp->data.u.sint < 0) == (tmp.data.u.sint < 0)) {
2344 smp->data.u.sint = LLONG_MAX;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002345 return 1;
2346 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002347 smp->data.u.sint = LLONG_MIN;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002348 return 1;
2349 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002350 smp->data.u.sint = c;
Willy Tarreau97707872015-01-27 15:12:13 +01002351 return 1;
2352}
2353
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002354/* Takes a SINT on input, applies an arithmetic "div" with the SINT directly in
2355 * arg_p or in the varaible described in arg_p, and returns the SINT result.
2356 * If arg_p makes the result overflow, then the largest possible quantity is
2357 * returned.
Willy Tarreau97707872015-01-27 15:12:13 +01002358 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002359static int sample_conv_arith_div(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002360 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002361{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002362 struct sample tmp;
2363
Willy Tarreau7560dd42016-03-10 16:28:58 +01002364 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreau6204cd92016-03-10 16:33:04 +01002365 if (!sample_conv_var2smp(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002366 return 0;
2367
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002368 if (tmp.data.u.sint) {
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002369 /* The divide between LLONG_MIN and -1 returns a
2370 * "floting point exception".
2371 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002372 if (smp->data.u.sint == LLONG_MIN && tmp.data.u.sint == -1) {
2373 smp->data.u.sint = LLONG_MAX;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002374 return 1;
2375 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002376 smp->data.u.sint /= tmp.data.u.sint;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002377 return 1;
2378 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002379 smp->data.u.sint = LLONG_MAX;
Willy Tarreau97707872015-01-27 15:12:13 +01002380 return 1;
2381}
2382
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002383/* Takes a SINT on input, applies an arithmetic "mod" with the SINT directly in
2384 * arg_p or in the varaible described in arg_p, and returns the SINT result.
2385 * If arg_p makes the result overflow, then 0 is returned.
Willy Tarreau97707872015-01-27 15:12:13 +01002386 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002387static int sample_conv_arith_mod(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002388 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002389{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002390 struct sample tmp;
2391
Willy Tarreau7560dd42016-03-10 16:28:58 +01002392 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreau6204cd92016-03-10 16:33:04 +01002393 if (!sample_conv_var2smp(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002394 return 0;
2395
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002396 if (tmp.data.u.sint) {
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002397 /* The divide between LLONG_MIN and -1 returns a
2398 * "floting point exception".
2399 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002400 if (smp->data.u.sint == LLONG_MIN && tmp.data.u.sint == -1) {
2401 smp->data.u.sint = 0;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002402 return 1;
2403 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002404 smp->data.u.sint %= tmp.data.u.sint;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002405 return 1;
2406 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002407 smp->data.u.sint = 0;
Willy Tarreau97707872015-01-27 15:12:13 +01002408 return 1;
2409}
2410
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002411/* Takes an SINT on input, applies an arithmetic "neg" and returns the SINT
Willy Tarreau97707872015-01-27 15:12:13 +01002412 * result.
2413 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002414static int sample_conv_arith_neg(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002415 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002416{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002417 if (smp->data.u.sint == LLONG_MIN)
2418 smp->data.u.sint = LLONG_MAX;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002419 else
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002420 smp->data.u.sint = -smp->data.u.sint;
Willy Tarreau97707872015-01-27 15:12:13 +01002421 return 1;
2422}
2423
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002424/* Takes a SINT on input, returns true is the value is non-null, otherwise
Willy Tarreau97707872015-01-27 15:12:13 +01002425 * false. The output is a BOOL.
2426 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002427static int sample_conv_arith_bool(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002428 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002429{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002430 smp->data.u.sint = !!smp->data.u.sint;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002431 smp->data.type = SMP_T_BOOL;
Willy Tarreau97707872015-01-27 15:12:13 +01002432 return 1;
2433}
2434
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002435/* Takes a SINT on input, returns false is the value is non-null, otherwise
Willy Tarreau97707872015-01-27 15:12:13 +01002436 * truee. The output is a BOOL.
2437 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002438static int sample_conv_arith_not(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 FOURNIER136f9d32015-08-19 09:07:19 +02002441 smp->data.u.sint = !smp->data.u.sint;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002442 smp->data.type = SMP_T_BOOL;
Willy Tarreau97707872015-01-27 15:12:13 +01002443 return 1;
2444}
2445
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002446/* Takes a SINT on input, returns true is the value is odd, otherwise false.
Willy Tarreau97707872015-01-27 15:12:13 +01002447 * The output is a BOOL.
2448 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002449static int sample_conv_arith_odd(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002450 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002451{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002452 smp->data.u.sint = smp->data.u.sint & 1;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002453 smp->data.type = SMP_T_BOOL;
Willy Tarreau97707872015-01-27 15:12:13 +01002454 return 1;
2455}
2456
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002457/* Takes a SINT on input, returns true is the value is even, otherwise false.
Willy Tarreau97707872015-01-27 15:12:13 +01002458 * The output is a BOOL.
2459 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002460static int sample_conv_arith_even(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002461 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002462{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002463 smp->data.u.sint = !(smp->data.u.sint & 1);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002464 smp->data.type = SMP_T_BOOL;
Willy Tarreau97707872015-01-27 15:12:13 +01002465 return 1;
2466}
2467
Willy Tarreau5b8ad222013-07-25 12:17:57 +02002468/************************************************************************/
2469/* All supported sample fetch functions must be declared here */
2470/************************************************************************/
2471
2472/* force TRUE to be returned at the fetch level */
2473static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002474smp_fetch_true(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau5b8ad222013-07-25 12:17:57 +02002475{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002476 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002477 smp->data.u.sint = 1;
Willy Tarreau5b8ad222013-07-25 12:17:57 +02002478 return 1;
2479}
2480
2481/* force FALSE to be returned at the fetch level */
2482static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002483smp_fetch_false(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau5b8ad222013-07-25 12:17:57 +02002484{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002485 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002486 smp->data.u.sint = 0;
Willy Tarreau5b8ad222013-07-25 12:17:57 +02002487 return 1;
2488}
2489
2490/* retrieve environment variable $1 as a string */
2491static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002492smp_fetch_env(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau5b8ad222013-07-25 12:17:57 +02002493{
2494 char *env;
2495
2496 if (!args || args[0].type != ARGT_STR)
2497 return 0;
2498
2499 env = getenv(args[0].data.str.str);
2500 if (!env)
2501 return 0;
2502
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002503 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01002504 smp->flags = SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002505 smp->data.u.str.str = env;
2506 smp->data.u.str.len = strlen(env);
Willy Tarreau5b8ad222013-07-25 12:17:57 +02002507 return 1;
2508}
2509
Willy Tarreau6236d3a2013-07-25 14:28:25 +02002510/* retrieve the current local date in epoch time, and applies an optional offset
2511 * of args[0] seconds.
2512 */
2513static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002514smp_fetch_date(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau6236d3a2013-07-25 14:28:25 +02002515{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002516 smp->data.u.sint = date.tv_sec;
Willy Tarreau6236d3a2013-07-25 14:28:25 +02002517
2518 /* add offset */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002519 if (args && args[0].type == ARGT_SINT)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002520 smp->data.u.sint += args[0].data.sint;
Willy Tarreau6236d3a2013-07-25 14:28:25 +02002521
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002522 smp->data.type = SMP_T_SINT;
Willy Tarreau6236d3a2013-07-25 14:28:25 +02002523 smp->flags |= SMP_F_VOL_TEST | SMP_F_MAY_CHANGE;
2524 return 1;
2525}
2526
Nenad Merdanovic807a6e72017-03-12 22:00:00 +01002527/* returns the hostname */
2528static int
2529smp_fetch_hostname(const struct arg *args, struct sample *smp, const char *kw, void *private)
2530{
2531 smp->data.type = SMP_T_STR;
2532 smp->flags = SMP_F_CONST;
2533 smp->data.u.str.str = hostname;
2534 smp->data.u.str.len = strlen(hostname);
2535 return 1;
2536}
2537
Willy Tarreau0f30d262014-11-24 16:02:05 +01002538/* returns the number of processes */
2539static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002540smp_fetch_nbproc(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau0f30d262014-11-24 16:02:05 +01002541{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002542 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002543 smp->data.u.sint = global.nbproc;
Willy Tarreau0f30d262014-11-24 16:02:05 +01002544 return 1;
2545}
2546
2547/* returns the number of the current process (between 1 and nbproc */
2548static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002549smp_fetch_proc(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau0f30d262014-11-24 16:02:05 +01002550{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002551 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002552 smp->data.u.sint = relative_pid;
Willy Tarreau0f30d262014-11-24 16:02:05 +01002553 return 1;
2554}
2555
Willy Tarreau84310e22014-02-14 11:59:04 +01002556/* generate a random 32-bit integer for whatever purpose, with an optional
2557 * range specified in argument.
2558 */
2559static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002560smp_fetch_rand(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau84310e22014-02-14 11:59:04 +01002561{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002562 smp->data.u.sint = random();
Willy Tarreau84310e22014-02-14 11:59:04 +01002563
2564 /* reduce if needed. Don't do a modulo, use all bits! */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002565 if (args && args[0].type == ARGT_SINT)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002566 smp->data.u.sint = (smp->data.u.sint * args[0].data.sint) / ((u64)RAND_MAX+1);
Willy Tarreau84310e22014-02-14 11:59:04 +01002567
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002568 smp->data.type = SMP_T_SINT;
Willy Tarreau84310e22014-02-14 11:59:04 +01002569 smp->flags |= SMP_F_VOL_TEST | SMP_F_MAY_CHANGE;
2570 return 1;
2571}
2572
Willy Tarreau0f30d262014-11-24 16:02:05 +01002573/* returns true if the current process is stopping */
2574static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002575smp_fetch_stopping(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau0f30d262014-11-24 16:02:05 +01002576{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002577 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002578 smp->data.u.sint = stopping;
Willy Tarreau0f30d262014-11-24 16:02:05 +01002579 return 1;
2580}
2581
Thierry FOURNIERcc103292015-06-06 19:30:17 +02002582static int smp_fetch_const_str(const struct arg *args, struct sample *smp, const char *kw, void *private)
2583{
2584 smp->flags |= SMP_F_CONST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002585 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002586 smp->data.u.str.str = args[0].data.str.str;
2587 smp->data.u.str.len = args[0].data.str.len;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02002588 return 1;
2589}
2590
2591static int smp_check_const_bool(struct arg *args, char **err)
2592{
2593 if (strcasecmp(args[0].data.str.str, "true") == 0 ||
2594 strcasecmp(args[0].data.str.str, "1") == 0) {
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002595 args[0].type = ARGT_SINT;
2596 args[0].data.sint = 1;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02002597 return 1;
2598 }
2599 if (strcasecmp(args[0].data.str.str, "false") == 0 ||
2600 strcasecmp(args[0].data.str.str, "0") == 0) {
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002601 args[0].type = ARGT_SINT;
2602 args[0].data.sint = 0;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02002603 return 1;
2604 }
2605 memprintf(err, "Expects 'true', 'false', '0' or '1'");
2606 return 0;
2607}
2608
2609static int smp_fetch_const_bool(const struct arg *args, struct sample *smp, const char *kw, void *private)
2610{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002611 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002612 smp->data.u.sint = args[0].data.sint;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02002613 return 1;
2614}
2615
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002616static int smp_fetch_const_int(const struct arg *args, struct sample *smp, const char *kw, void *private)
Thierry FOURNIERcc103292015-06-06 19:30:17 +02002617{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002618 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002619 smp->data.u.sint = args[0].data.sint;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02002620 return 1;
2621}
2622
2623static int smp_fetch_const_ipv4(const struct arg *args, struct sample *smp, const char *kw, void *private)
2624{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002625 smp->data.type = SMP_T_IPV4;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002626 smp->data.u.ipv4 = args[0].data.ipv4;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02002627 return 1;
2628}
2629
2630static int smp_fetch_const_ipv6(const struct arg *args, struct sample *smp, const char *kw, void *private)
2631{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002632 smp->data.type = SMP_T_IPV6;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002633 smp->data.u.ipv6 = args[0].data.ipv6;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02002634 return 1;
2635}
2636
2637static int smp_check_const_bin(struct arg *args, char **err)
2638{
David Carlier64a16ab2016-04-08 10:37:02 +01002639 char *binstr = NULL;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02002640 int binstrlen;
2641
2642 if (!parse_binary(args[0].data.str.str, &binstr, &binstrlen, err))
2643 return 0;
2644 args[0].type = ARGT_STR;
2645 args[0].data.str.str = binstr;
2646 args[0].data.str.len = binstrlen;
2647 return 1;
2648}
2649
2650static int smp_fetch_const_bin(const struct arg *args, struct sample *smp, const char *kw, void *private)
2651{
2652 smp->flags |= SMP_F_CONST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002653 smp->data.type = SMP_T_BIN;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002654 smp->data.u.str.str = args[0].data.str.str;
2655 smp->data.u.str.len = args[0].data.str.len;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02002656 return 1;
2657}
2658
2659static int smp_check_const_meth(struct arg *args, char **err)
2660{
2661 enum http_meth_t meth;
2662 int i;
2663
2664 meth = find_http_meth(args[0].data.str.str, args[0].data.str.len);
2665 if (meth != HTTP_METH_OTHER) {
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002666 args[0].type = ARGT_SINT;
2667 args[0].data.sint = meth;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02002668 } else {
2669 /* Check method avalaibility. A methos is a token defined as :
2670 * tchar = "!" / "#" / "$" / "%" / "&" / "'" / "*" / "+" / "-" / "." /
2671 * "^" / "_" / "`" / "|" / "~" / DIGIT / ALPHA
2672 * token = 1*tchar
2673 */
2674 for (i = 0; i < args[0].data.str.len; i++) {
Willy Tarreau2235b262016-11-05 15:50:20 +01002675 if (!HTTP_IS_TOKEN(args[0].data.str.str[i])) {
Thierry FOURNIERcc103292015-06-06 19:30:17 +02002676 memprintf(err, "expects valid method.");
2677 return 0;
2678 }
2679 }
2680 }
2681 return 1;
2682}
2683
2684static int smp_fetch_const_meth(const struct arg *args, struct sample *smp, const char *kw, void *private)
2685{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002686 smp->data.type = SMP_T_METH;
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002687 if (args[0].type == ARGT_SINT) {
Thierry FOURNIERcc103292015-06-06 19:30:17 +02002688 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002689 smp->data.u.meth.meth = args[0].data.sint;
2690 smp->data.u.meth.str.str = "";
2691 smp->data.u.meth.str.len = 0;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02002692 } else {
2693 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002694 smp->data.u.meth.meth = HTTP_METH_OTHER;
2695 smp->data.u.meth.str.str = args[0].data.str.str;
2696 smp->data.u.meth.str.len = args[0].data.str.len;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02002697 }
2698 return 1;
2699}
2700
Willy Tarreau5b8ad222013-07-25 12:17:57 +02002701/* Note: must not be declared <const> as its list will be overwritten.
2702 * Note: fetches that may return multiple types must be declared as the lowest
2703 * common denominator, the type that can be casted into all other ones. For
2704 * instance IPv4/IPv6 must be declared IPv4.
2705 */
2706static struct sample_fetch_kw_list smp_kws = {ILH, {
2707 { "always_false", smp_fetch_false, 0, NULL, SMP_T_BOOL, SMP_USE_INTRN },
2708 { "always_true", smp_fetch_true, 0, NULL, SMP_T_BOOL, SMP_USE_INTRN },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01002709 { "env", smp_fetch_env, ARG1(1,STR), NULL, SMP_T_STR, SMP_USE_INTRN },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002710 { "date", smp_fetch_date, ARG1(0,SINT), NULL, SMP_T_SINT, SMP_USE_INTRN },
Nenad Merdanovic807a6e72017-03-12 22:00:00 +01002711 { "hostname", smp_fetch_hostname, 0, NULL, SMP_T_STR, SMP_USE_INTRN },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002712 { "nbproc", smp_fetch_nbproc,0, NULL, SMP_T_SINT, SMP_USE_INTRN },
2713 { "proc", smp_fetch_proc, 0, NULL, SMP_T_SINT, SMP_USE_INTRN },
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002714 { "rand", smp_fetch_rand, ARG1(0,SINT), NULL, SMP_T_SINT, SMP_USE_INTRN },
Willy Tarreau0f30d262014-11-24 16:02:05 +01002715 { "stopping", smp_fetch_stopping, 0, NULL, SMP_T_BOOL, SMP_USE_INTRN },
Thierry FOURNIERcc103292015-06-06 19:30:17 +02002716
2717 { "str", smp_fetch_const_str, ARG1(1,STR), NULL , SMP_T_STR, SMP_USE_INTRN },
2718 { "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 +02002719 { "int", smp_fetch_const_int, ARG1(1,SINT), NULL , SMP_T_SINT, SMP_USE_INTRN },
Thierry FOURNIERcc103292015-06-06 19:30:17 +02002720 { "ipv4", smp_fetch_const_ipv4, ARG1(1,IPV4), NULL , SMP_T_IPV4, SMP_USE_INTRN },
2721 { "ipv6", smp_fetch_const_ipv6, ARG1(1,IPV6), NULL , SMP_T_IPV6, SMP_USE_INTRN },
2722 { "bin", smp_fetch_const_bin, ARG1(1,STR), smp_check_const_bin , SMP_T_BIN, SMP_USE_INTRN },
2723 { "meth", smp_fetch_const_meth, ARG1(1,STR), smp_check_const_meth, SMP_T_METH, SMP_USE_INTRN },
2724
Willy Tarreau5b8ad222013-07-25 12:17:57 +02002725 { /* END */ },
2726}};
2727
Emeric Brun107ca302010-01-04 16:16:05 +01002728/* Note: must not be declared <const> as its list will be overwritten */
Willy Tarreaudc13c112013-06-21 23:16:39 +02002729static struct sample_conv_kw_list sample_conv_kws = {ILH, {
Thierry FOURNIER9687c772015-05-07 15:46:29 +02002730#ifdef DEBUG_EXPR
2731 { "debug", sample_conv_debug, 0, NULL, SMP_T_ANY, SMP_T_ANY },
2732#endif
2733
Holger Just1bfc24b2017-05-06 00:56:53 +02002734 { "b64dec", sample_conv_base642bin,0, NULL, SMP_T_STR, SMP_T_BIN },
Emeric Brun53d1a982014-04-30 18:21:37 +02002735 { "base64", sample_conv_bin2base64,0, NULL, SMP_T_BIN, SMP_T_STR },
Willy Tarreau12785782012-04-27 21:37:17 +02002736 { "upper", sample_conv_str2upper, 0, NULL, SMP_T_STR, SMP_T_STR },
2737 { "lower", sample_conv_str2lower, 0, NULL, SMP_T_STR, SMP_T_STR },
Thierry FOURNIER2f49d6d2014-03-12 15:01:52 +01002738 { "hex", sample_conv_bin2hex, 0, NULL, SMP_T_BIN, SMP_T_STR },
Willy Tarreau12785782012-04-27 21:37:17 +02002739 { "ipmask", sample_conv_ipmask, ARG1(1,MSK4), NULL, SMP_T_IPV4, SMP_T_IPV4 },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002740 { "ltime", sample_conv_ltime, ARG2(1,STR,SINT), NULL, SMP_T_SINT, SMP_T_STR },
2741 { "utime", sample_conv_utime, ARG2(1,STR,SINT), NULL, SMP_T_SINT, SMP_T_STR },
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002742 { "crc32", sample_conv_crc32, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
2743 { "djb2", sample_conv_djb2, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
2744 { "sdbm", sample_conv_sdbm, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
2745 { "wt6", sample_conv_wt6, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
Thierry FOURNIER01e09742016-12-26 11:46:11 +01002746 { "xxh32", sample_conv_xxh32, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
2747 { "xxh64", sample_conv_xxh64, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002748 { "json", sample_conv_json, ARG1(1,STR), sample_conv_json_check, SMP_T_STR, SMP_T_STR },
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002749 { "bytes", sample_conv_bytes, ARG2(1,SINT,SINT), NULL, SMP_T_BIN, SMP_T_BIN },
2750 { "field", sample_conv_field, ARG2(2,SINT,STR), sample_conv_field_check, SMP_T_STR, SMP_T_STR },
2751 { "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 +01002752 { "regsub", sample_conv_regsub, ARG3(2,REG,STR,STR), sample_conv_regsub_check, SMP_T_STR, SMP_T_STR },
Willy Tarreau97707872015-01-27 15:12:13 +01002753
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002754 { "and", sample_conv_binary_and, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
2755 { "or", sample_conv_binary_or, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
2756 { "xor", sample_conv_binary_xor, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
2757 { "cpl", sample_conv_binary_cpl, 0, NULL, SMP_T_SINT, SMP_T_SINT },
2758 { "bool", sample_conv_arith_bool, 0, NULL, SMP_T_SINT, SMP_T_BOOL },
2759 { "not", sample_conv_arith_not, 0, NULL, SMP_T_SINT, SMP_T_BOOL },
2760 { "odd", sample_conv_arith_odd, 0, NULL, SMP_T_SINT, SMP_T_BOOL },
2761 { "even", sample_conv_arith_even, 0, NULL, SMP_T_SINT, SMP_T_BOOL },
2762 { "add", sample_conv_arith_add, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
2763 { "sub", sample_conv_arith_sub, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
2764 { "mul", sample_conv_arith_mul, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
2765 { "div", sample_conv_arith_div, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
2766 { "mod", sample_conv_arith_mod, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
2767 { "neg", sample_conv_arith_neg, 0, NULL, SMP_T_SINT, SMP_T_SINT },
Willy Tarreau97707872015-01-27 15:12:13 +01002768
Willy Tarreau9fcb9842012-04-20 14:45:49 +02002769 { NULL, NULL, 0, 0, 0 },
Emeric Brun107ca302010-01-04 16:16:05 +01002770}};
2771
2772__attribute__((constructor))
Willy Tarreau12785782012-04-27 21:37:17 +02002773static void __sample_init(void)
Emeric Brun107ca302010-01-04 16:16:05 +01002774{
Willy Tarreau5b8ad222013-07-25 12:17:57 +02002775 /* register sample fetch and format conversion keywords */
2776 sample_register_fetches(&smp_kws);
Willy Tarreau12785782012-04-27 21:37:17 +02002777 sample_register_convs(&sample_conv_kws);
Emeric Brun107ca302010-01-04 16:16:05 +01002778}