blob: 3cbe76279f38eaf0b186dc9929d9597bd09e918a [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 FOURNIER7654c9f2013-12-17 00:20:33 +0100652 /* If the const flag is not set, we don't need to duplicate the
653 * pattern as it can be modified in place.
654 */
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100655
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200656 switch (smp->data.type) {
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100657 case SMP_T_BOOL:
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100658 case SMP_T_SINT:
659 case SMP_T_ADDR:
660 case SMP_T_IPV4:
661 case SMP_T_IPV6:
662 /* These type are not const. */
663 break;
Willy Tarreauad635822016-08-08 19:21:09 +0200664
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100665 case SMP_T_STR:
Willy Tarreauad635822016-08-08 19:21:09 +0200666 trash = get_trash_chunk();
667 trash->len = smp->data.u.str.len;
668 if (trash->len > trash->size - 1)
669 trash->len = trash->size - 1;
670
671 memcpy(trash->str, smp->data.u.str.str, trash->len);
672 trash->str[trash->len] = 0;
673 smp->data.u.str = *trash;
674 break;
675
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100676 case SMP_T_BIN:
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100677 trash = get_trash_chunk();
Willy Tarreauad635822016-08-08 19:21:09 +0200678 trash->len = smp->data.u.str.len;
679 if (trash->len > trash->size)
680 trash->len = trash->size;
681
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200682 memcpy(trash->str, smp->data.u.str.str, trash->len);
683 smp->data.u.str = *trash;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100684 break;
685 default:
686 /* Other cases are unexpected. */
687 return 0;
688 }
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100689
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100690 /* remove const flag */
691 smp->flags &= ~SMP_F_CONST;
Emeric Brun485479d2010-09-23 18:02:19 +0200692 return 1;
693}
694
Thierry FOURNIER0e9af552013-12-14 14:55:04 +0100695int c_none(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100696{
697 return 1;
698}
699
Willy Tarreau342acb42012-04-23 22:03:39 +0200700static int c_str2int(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100701{
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +0200702 const char *str;
703 const char *end;
Emeric Brun107ca302010-01-04 16:16:05 +0100704
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200705 if (smp->data.u.str.len == 0)
Thierry FOURNIER60bb0202014-01-27 18:20:48 +0100706 return 0;
707
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200708 str = smp->data.u.str.str;
709 end = smp->data.u.str.str + smp->data.u.str.len;
Emeric Brun107ca302010-01-04 16:16:05 +0100710
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200711 smp->data.u.sint = read_int64(&str, end);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200712 smp->data.type = SMP_T_SINT;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100713 smp->flags &= ~SMP_F_CONST;
Emeric Brun107ca302010-01-04 16:16:05 +0100714 return 1;
715}
716
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100717static int c_str2meth(struct sample *smp)
718{
719 enum http_meth_t meth;
720 int len;
721
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200722 meth = find_http_meth(smp->data.u.str.str, smp->data.u.str.len);
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100723 if (meth == HTTP_METH_OTHER) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200724 len = smp->data.u.str.len;
725 smp->data.u.meth.str.str = smp->data.u.str.str;
726 smp->data.u.meth.str.len = len;
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100727 }
728 else
729 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200730 smp->data.u.meth.meth = meth;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200731 smp->data.type = SMP_T_METH;
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100732 return 1;
733}
734
735static int c_meth2str(struct sample *smp)
736{
737 int len;
738 enum http_meth_t meth;
739
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200740 if (smp->data.u.meth.meth == HTTP_METH_OTHER) {
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100741 /* The method is unknown. Copy the original pointer. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200742 len = smp->data.u.meth.str.len;
743 smp->data.u.str.str = smp->data.u.meth.str.str;
744 smp->data.u.str.len = len;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200745 smp->data.type = SMP_T_STR;
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100746 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200747 else if (smp->data.u.meth.meth < HTTP_METH_OTHER) {
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100748 /* The method is known, copy the pointer containing the string. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200749 meth = smp->data.u.meth.meth;
750 smp->data.u.str.str = http_known_methods[meth].name;
751 smp->data.u.str.len = http_known_methods[meth].len;
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100752 smp->flags |= SMP_F_CONST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200753 smp->data.type = SMP_T_STR;
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100754 }
755 else {
756 /* Unknown method */
757 return 0;
758 }
759 return 1;
760}
761
Willy Tarreau9700e5c2014-07-15 21:03:26 +0200762static int c_addr2bin(struct sample *smp)
763{
764 struct chunk *chk = get_trash_chunk();
765
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200766 if (smp->data.type == SMP_T_IPV4) {
Willy Tarreau9700e5c2014-07-15 21:03:26 +0200767 chk->len = 4;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200768 memcpy(chk->str, &smp->data.u.ipv4, chk->len);
Willy Tarreau9700e5c2014-07-15 21:03:26 +0200769 }
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200770 else if (smp->data.type == SMP_T_IPV6) {
Willy Tarreau9700e5c2014-07-15 21:03:26 +0200771 chk->len = 16;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200772 memcpy(chk->str, &smp->data.u.ipv6, chk->len);
Willy Tarreau9700e5c2014-07-15 21:03:26 +0200773 }
774 else
775 return 0;
776
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200777 smp->data.u.str = *chk;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200778 smp->data.type = SMP_T_BIN;
Willy Tarreau9700e5c2014-07-15 21:03:26 +0200779 return 1;
780}
781
Willy Tarreaubbfd1a22014-07-15 21:19:08 +0200782static int c_int2bin(struct sample *smp)
783{
784 struct chunk *chk = get_trash_chunk();
785
Willy Tarreau5f6e9052016-05-20 06:29:59 +0200786 *(unsigned long long int *)chk->str = my_htonll(smp->data.u.sint);
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +0200787 chk->len = 8;
Willy Tarreaubbfd1a22014-07-15 21:19:08 +0200788
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200789 smp->data.u.str = *chk;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200790 smp->data.type = SMP_T_BIN;
Willy Tarreaubbfd1a22014-07-15 21:19:08 +0200791 return 1;
792}
793
Willy Tarreau9700e5c2014-07-15 21:03:26 +0200794
Emeric Brun107ca302010-01-04 16:16:05 +0100795/*****************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +0200796/* Sample casts matrix: */
797/* sample_casts[from type][to type] */
798/* NULL pointer used for impossible sample casts */
Emeric Brun107ca302010-01-04 16:16:05 +0100799/*****************************************************************/
Emeric Brun107ca302010-01-04 16:16:05 +0100800
Thierry FOURNIER8af6ff12013-11-21 10:53:12 +0100801sample_cast_fct sample_casts[SMP_TYPES][SMP_TYPES] = {
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +0200802/* to: ANY BOOL SINT ADDR IPV4 IPV6 STR BIN METH */
803/* from: ANY */ { c_none, c_none, c_none, c_none, c_none, c_none, c_none, c_none, c_none, },
804/* BOOL */ { c_none, c_none, c_none, NULL, NULL, NULL, c_int2str, NULL, NULL, },
Thierry FOURNIERcc4d1712015-07-24 09:04:56 +0200805/* 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 +0200806/* ADDR */ { c_none, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, },
807/* IPV4 */ { c_none, NULL, c_ip2int, c_none, c_none, c_ip2ipv6, c_ip2str, c_addr2bin, NULL, },
Thierry FOURNIERcc4d1712015-07-24 09:04:56 +0200808/* IPV6 */ { c_none, NULL, NULL, c_none, c_ipv62ip,c_none, c_ipv62str, c_addr2bin, NULL, },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +0200809/* STR */ { c_none, c_str2int, c_str2int, c_str2addr, c_str2ip, c_str2ipv6, c_none, c_none, c_str2meth, },
810/* BIN */ { c_none, NULL, NULL, NULL, NULL, NULL, c_bin2str, c_none, c_str2meth, },
811/* METH */ { c_none, NULL, NULL, NULL, NULL, NULL, c_meth2str, c_meth2str, c_none, }
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200812};
Emeric Brun107ca302010-01-04 16:16:05 +0100813
Emeric Brun107ca302010-01-04 16:16:05 +0100814/*
Willy Tarreau12785782012-04-27 21:37:17 +0200815 * Parse a sample expression configuration:
Emeric Brun107ca302010-01-04 16:16:05 +0100816 * fetch keyword followed by format conversion keywords.
Willy Tarreau12785782012-04-27 21:37:17 +0200817 * Returns a pointer on allocated sample expression structure.
Willy Tarreaua4312fa2013-04-02 16:34:32 +0200818 * The caller must have set al->ctx.
Emeric Brun107ca302010-01-04 16:16:05 +0100819 */
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +0100820struct 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 +0100821{
Willy Tarreau833cc792013-07-24 15:34:19 +0200822 const char *begw; /* beginning of word */
823 const char *endw; /* end of word */
824 const char *endt; /* end of term */
Willy Tarreau12785782012-04-27 21:37:17 +0200825 struct sample_expr *expr;
826 struct sample_fetch *fetch;
827 struct sample_conv *conv;
Emeric Brun107ca302010-01-04 16:16:05 +0100828 unsigned long prev_type;
Willy Tarreau833cc792013-07-24 15:34:19 +0200829 char *fkw = NULL;
830 char *ckw = NULL;
Willy Tarreau689a1df2013-12-13 00:40:11 +0100831 int err_arg;
Emeric Brun107ca302010-01-04 16:16:05 +0100832
Willy Tarreau833cc792013-07-24 15:34:19 +0200833 begw = str[*idx];
834 for (endw = begw; *endw && *endw != '(' && *endw != ','; endw++);
835
836 if (endw == begw) {
Willy Tarreau975c1782013-12-12 23:16:54 +0100837 memprintf(err_msg, "missing fetch method");
Emeric Brun107ca302010-01-04 16:16:05 +0100838 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200839 }
Emeric Brun107ca302010-01-04 16:16:05 +0100840
Willy Tarreau833cc792013-07-24 15:34:19 +0200841 /* keep a copy of the current fetch keyword for error reporting */
842 fkw = my_strndup(begw, endw - begw);
Emeric Brun107ca302010-01-04 16:16:05 +0100843
Willy Tarreau833cc792013-07-24 15:34:19 +0200844 fetch = find_sample_fetch(begw, endw - begw);
845 if (!fetch) {
Willy Tarreau975c1782013-12-12 23:16:54 +0100846 memprintf(err_msg, "unknown fetch method '%s'", fkw);
Emeric Brun107ca302010-01-04 16:16:05 +0100847 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200848 }
Emeric Brun107ca302010-01-04 16:16:05 +0100849
Willy Tarreau833cc792013-07-24 15:34:19 +0200850 endt = endw;
851 if (*endt == '(') {
Willy Tarreau689a1df2013-12-13 00:40:11 +0100852 /* look for the end of this term and skip the opening parenthesis */
853 endt = ++endw;
Willy Tarreau833cc792013-07-24 15:34:19 +0200854 while (*endt && *endt != ')')
855 endt++;
856 if (*endt != ')') {
Willy Tarreau975c1782013-12-12 23:16:54 +0100857 memprintf(err_msg, "missing closing ')' after arguments to fetch keyword '%s'", fkw);
Willy Tarreau833cc792013-07-24 15:34:19 +0200858 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200859 }
Emeric Brun485479d2010-09-23 18:02:19 +0200860 }
Emeric Brun107ca302010-01-04 16:16:05 +0100861
Willy Tarreau833cc792013-07-24 15:34:19 +0200862 /* At this point, we have :
863 * - begw : beginning of the keyword
Willy Tarreau689a1df2013-12-13 00:40:11 +0100864 * - endw : end of the keyword, first character not part of keyword
865 * nor the opening parenthesis (so first character of args
866 * if present).
Willy Tarreau833cc792013-07-24 15:34:19 +0200867 * - endt : end of the term (=endw or last parenthesis if args are present)
868 */
869
870 if (fetch->out_type >= SMP_TYPES) {
Willy Tarreau975c1782013-12-12 23:16:54 +0100871 memprintf(err_msg, "returns type of fetch method '%s' is unknown", fkw);
Emeric Brun107ca302010-01-04 16:16:05 +0100872 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200873 }
Emeric Brun107ca302010-01-04 16:16:05 +0100874 prev_type = fetch->out_type;
Willy Tarreau833cc792013-07-24 15:34:19 +0200875
Vincent Bernat02779b62016-04-03 13:48:43 +0200876 expr = calloc(1, sizeof(*expr));
Emeric Brun485479d2010-09-23 18:02:19 +0200877 if (!expr)
878 goto out_error;
Emeric Brun107ca302010-01-04 16:16:05 +0100879
880 LIST_INIT(&(expr->conv_exprs));
881 expr->fetch = fetch;
Willy Tarreau2e845be2012-10-19 19:49:09 +0200882 expr->arg_p = empty_arg_list;
Emeric Brun107ca302010-01-04 16:16:05 +0100883
Willy Tarreau689a1df2013-12-13 00:40:11 +0100884 /* Note that we call the argument parser even with an empty string,
885 * this allows it to automatically create entries for mandatory
886 * implicit arguments (eg: local proxy name).
887 */
888 al->kw = expr->fetch->kw;
889 al->conv = NULL;
890 if (make_arg_list(endw, endt - endw, fetch->arg_mask, &expr->arg_p, err_msg, NULL, &err_arg, al) < 0) {
891 memprintf(err_msg, "fetch method '%s' : %s", fkw, *err_msg);
892 goto out_error;
893 }
Willy Tarreau2e845be2012-10-19 19:49:09 +0200894
Willy Tarreau689a1df2013-12-13 00:40:11 +0100895 if (!expr->arg_p) {
896 expr->arg_p = empty_arg_list;
Emeric Brun485479d2010-09-23 18:02:19 +0200897 }
Willy Tarreau689a1df2013-12-13 00:40:11 +0100898 else if (fetch->val_args && !fetch->val_args(expr->arg_p, err_msg)) {
899 memprintf(err_msg, "invalid args in fetch method '%s' : %s", fkw, *err_msg);
Emeric Brun485479d2010-09-23 18:02:19 +0200900 goto out_error;
Emeric Brun107ca302010-01-04 16:16:05 +0100901 }
902
Willy Tarreau833cc792013-07-24 15:34:19 +0200903 /* Now process the converters if any. We have two supported syntaxes
904 * for the converters, which can be combined :
905 * - comma-delimited list of converters just after the keyword and args ;
906 * - one converter per keyword
907 * The combination allows to have each keyword being a comma-delimited
908 * series of converters.
909 *
910 * We want to process the former first, then the latter. For this we start
911 * from the beginning of the supposed place in the exiting conv chain, which
912 * starts at the last comma (endt).
913 */
914
915 while (1) {
Willy Tarreau12785782012-04-27 21:37:17 +0200916 struct sample_conv_expr *conv_expr;
Emeric Brun107ca302010-01-04 16:16:05 +0100917
Willy Tarreau833cc792013-07-24 15:34:19 +0200918 if (*endt == ')') /* skip last closing parenthesis */
919 endt++;
Emeric Brun107ca302010-01-04 16:16:05 +0100920
Willy Tarreau833cc792013-07-24 15:34:19 +0200921 if (*endt && *endt != ',') {
922 if (ckw)
Willy Tarreau97108e02016-11-25 07:33:24 +0100923 memprintf(err_msg, "missing comma after converter '%s'", ckw);
Willy Tarreau833cc792013-07-24 15:34:19 +0200924 else
Willy Tarreau975c1782013-12-12 23:16:54 +0100925 memprintf(err_msg, "missing comma after fetch keyword '%s'", fkw);
Emeric Brun107ca302010-01-04 16:16:05 +0100926 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200927 }
Emeric Brun107ca302010-01-04 16:16:05 +0100928
Willy Tarreau833cc792013-07-24 15:34:19 +0200929 while (*endt == ',') /* then trailing commas */
930 endt++;
931
Willy Tarreau97108e02016-11-25 07:33:24 +0100932 begw = endt; /* start of converter */
Willy Tarreau833cc792013-07-24 15:34:19 +0200933
934 if (!*begw) {
935 /* none ? skip to next string */
936 (*idx)++;
937 begw = str[*idx];
938 if (!begw || !*begw)
939 break;
940 }
941
942 for (endw = begw; *endw && *endw != '(' && *endw != ','; endw++);
943
944 free(ckw);
945 ckw = my_strndup(begw, endw - begw);
946
947 conv = find_sample_conv(begw, endw - begw);
948 if (!conv) {
949 /* we found an isolated keyword that we don't know, it's not ours */
950 if (begw == str[*idx])
951 break;
Willy Tarreau97108e02016-11-25 07:33:24 +0100952 memprintf(err_msg, "unknown converter '%s'", ckw);
Willy Tarreau833cc792013-07-24 15:34:19 +0200953 goto out_error;
954 }
Emeric Brun107ca302010-01-04 16:16:05 +0100955
Willy Tarreau833cc792013-07-24 15:34:19 +0200956 endt = endw;
957 if (*endt == '(') {
958 /* look for the end of this term */
959 while (*endt && *endt != ')')
960 endt++;
961 if (*endt != ')') {
Willy Tarreau97108e02016-11-25 07:33:24 +0100962 memprintf(err_msg, "syntax error: missing ')' after converter '%s'", ckw);
Willy Tarreau833cc792013-07-24 15:34:19 +0200963 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200964 }
Willy Tarreau833cc792013-07-24 15:34:19 +0200965 }
966
967 if (conv->in_type >= SMP_TYPES || conv->out_type >= SMP_TYPES) {
Willy Tarreau97108e02016-11-25 07:33:24 +0100968 memprintf(err_msg, "returns type of converter '%s' is unknown", ckw);
Emeric Brun107ca302010-01-04 16:16:05 +0100969 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200970 }
Emeric Brun107ca302010-01-04 16:16:05 +0100971
972 /* If impossible type conversion */
Willy Tarreau12785782012-04-27 21:37:17 +0200973 if (!sample_casts[prev_type][conv->in_type]) {
Willy Tarreau97108e02016-11-25 07:33:24 +0100974 memprintf(err_msg, "converter '%s' cannot be applied", ckw);
Emeric Brun107ca302010-01-04 16:16:05 +0100975 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200976 }
Emeric Brun107ca302010-01-04 16:16:05 +0100977
978 prev_type = conv->out_type;
Vincent Bernat02779b62016-04-03 13:48:43 +0200979 conv_expr = calloc(1, sizeof(*conv_expr));
Emeric Brun485479d2010-09-23 18:02:19 +0200980 if (!conv_expr)
981 goto out_error;
Emeric Brun107ca302010-01-04 16:16:05 +0100982
983 LIST_ADDQ(&(expr->conv_exprs), &(conv_expr->list));
984 conv_expr->conv = conv;
985
Willy Tarreau833cc792013-07-24 15:34:19 +0200986 if (endt != endw) {
Willy Tarreaub27c0d32012-04-20 16:04:47 +0200987 int err_arg;
Willy Tarreau21d68a62012-04-20 15:52:36 +0200988
Willy Tarreau9fcb9842012-04-20 14:45:49 +0200989 if (!conv->arg_mask) {
Willy Tarreau97108e02016-11-25 07:33:24 +0100990 memprintf(err_msg, "converter '%s' does not support any args", ckw);
Emeric Brun485479d2010-09-23 18:02:19 +0200991 goto out_error;
992 }
Willy Tarreau9e92d322010-01-26 17:58:06 +0100993
Willy Tarreaua4312fa2013-04-02 16:34:32 +0200994 al->kw = expr->fetch->kw;
995 al->conv = conv_expr->conv->kw;
Willy Tarreau975c1782013-12-12 23:16:54 +0100996 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 +0100997 memprintf(err_msg, "invalid arg %d in converter '%s' : %s", err_arg+1, ckw, *err_msg);
Willy Tarreau21d68a62012-04-20 15:52:36 +0200998 goto out_error;
999 }
1000
Willy Tarreau2e845be2012-10-19 19:49:09 +02001001 if (!conv_expr->arg_p)
1002 conv_expr->arg_p = empty_arg_list;
1003
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +01001004 if (conv->val_args && !conv->val_args(conv_expr->arg_p, conv, file, line, err_msg)) {
Willy Tarreau97108e02016-11-25 07:33:24 +01001005 memprintf(err_msg, "invalid args in converter '%s' : %s", ckw, *err_msg);
Emeric Brun485479d2010-09-23 18:02:19 +02001006 goto out_error;
1007 }
1008 }
Willy Tarreau833cc792013-07-24 15:34:19 +02001009 else if (ARGM(conv->arg_mask)) {
Willy Tarreau97108e02016-11-25 07:33:24 +01001010 memprintf(err_msg, "missing args for converter '%s'", ckw);
Emeric Brun485479d2010-09-23 18:02:19 +02001011 goto out_error;
Emeric Brun107ca302010-01-04 16:16:05 +01001012 }
1013 }
Emeric Brun485479d2010-09-23 18:02:19 +02001014
Willy Tarreau833cc792013-07-24 15:34:19 +02001015 out:
1016 free(fkw);
1017 free(ckw);
Emeric Brun107ca302010-01-04 16:16:05 +01001018 return expr;
1019
1020out_error:
Willy Tarreau12785782012-04-27 21:37:17 +02001021 /* TODO: prune_sample_expr(expr); */
Willy Tarreau833cc792013-07-24 15:34:19 +02001022 expr = NULL;
1023 goto out;
Emeric Brun107ca302010-01-04 16:16:05 +01001024}
1025
1026/*
Willy Tarreau12785782012-04-27 21:37:17 +02001027 * Process a fetch + format conversion of defined by the sample expression <expr>
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001028 * on request or response considering the <opt> parameter.
Willy Tarreau12785782012-04-27 21:37:17 +02001029 * Returns a pointer on a typed sample structure containing the result or NULL if
1030 * sample is not found or when format conversion failed.
Emeric Brun107ca302010-01-04 16:16:05 +01001031 * If <p> is not null, function returns results in structure pointed by <p>.
Willy Tarreau12785782012-04-27 21:37:17 +02001032 * If <p> is null, functions returns a pointer on a static sample structure.
Willy Tarreaub8c8f1f2012-04-23 22:38:26 +02001033 *
1034 * Note: the fetch functions are required to properly set the return type. The
1035 * conversion functions must do so too. However the cast functions do not need
1036 * to since they're made to cast mutiple types according to what is required.
Willy Tarreau6bcb0a82014-07-30 08:56:35 +02001037 *
1038 * The caller may indicate in <opt> if it considers the result final or not.
1039 * The caller needs to check the SMP_F_MAY_CHANGE flag in p->flags to verify
1040 * if the result is stable or not, according to the following table :
1041 *
1042 * return MAY_CHANGE FINAL Meaning for the sample
1043 * NULL 0 * Not present and will never be (eg: header)
1044 * NULL 1 0 Not present yet, could change (eg: POST param)
1045 * NULL 1 1 Not present yet, will not change anymore
1046 * smp 0 * Present and will not change (eg: header)
1047 * smp 1 0 Present, may change (eg: request length)
1048 * smp 1 1 Present, last known value (eg: request length)
Emeric Brun107ca302010-01-04 16:16:05 +01001049 */
Willy Tarreau192252e2015-04-04 01:47:55 +02001050struct sample *sample_process(struct proxy *px, struct session *sess,
1051 struct stream *strm, unsigned int opt,
Willy Tarreau12785782012-04-27 21:37:17 +02001052 struct sample_expr *expr, struct sample *p)
Emeric Brun107ca302010-01-04 16:16:05 +01001053{
Willy Tarreau12785782012-04-27 21:37:17 +02001054 struct sample_conv_expr *conv_expr;
Emeric Brun107ca302010-01-04 16:16:05 +01001055
Willy Tarreau18387e22013-07-25 12:02:38 +02001056 if (p == NULL) {
Willy Tarreaub4a88f02012-04-23 21:35:11 +02001057 p = &temp_smp;
Willy Tarreau6c616e02014-06-25 16:56:41 +02001058 memset(p, 0, sizeof(*p));
Willy Tarreau18387e22013-07-25 12:02:38 +02001059 }
Emeric Brun107ca302010-01-04 16:16:05 +01001060
Willy Tarreau1777ea62016-03-10 16:15:46 +01001061 smp_set_owner(p, px, sess, strm, opt);
Thierry FOURNIER0786d052015-05-11 15:42:45 +02001062 if (!expr->fetch->process(expr->arg_p, p, expr->fetch->kw, expr->fetch->private))
Emeric Brun107ca302010-01-04 16:16:05 +01001063 return NULL;
1064
Emeric Brun107ca302010-01-04 16:16:05 +01001065 list_for_each_entry(conv_expr, &expr->conv_exprs, list) {
Willy Tarreau12e50112012-04-25 17:21:49 +02001066 /* we want to ensure that p->type can be casted into
1067 * conv_expr->conv->in_type. We have 3 possibilities :
1068 * - NULL => not castable.
1069 * - c_none => nothing to do (let's optimize it)
1070 * - other => apply cast and prepare to fail
1071 */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001072 if (!sample_casts[p->data.type][conv_expr->conv->in_type])
Willy Tarreau12e50112012-04-25 17:21:49 +02001073 return NULL;
1074
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001075 if (sample_casts[p->data.type][conv_expr->conv->in_type] != c_none &&
1076 !sample_casts[p->data.type][conv_expr->conv->in_type](p))
Emeric Brun107ca302010-01-04 16:16:05 +01001077 return NULL;
1078
Willy Tarreau12e50112012-04-25 17:21:49 +02001079 /* OK cast succeeded */
1080
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001081 if (!conv_expr->conv->process(conv_expr->arg_p, p, conv_expr->conv->private))
Emeric Brun107ca302010-01-04 16:16:05 +01001082 return NULL;
Emeric Brun107ca302010-01-04 16:16:05 +01001083 }
1084 return p;
1085}
1086
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001087/*
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001088 * Resolve all remaining arguments in proxy <p>. Returns the number of
1089 * errors or 0 if everything is fine.
1090 */
1091int smp_resolve_args(struct proxy *p)
1092{
1093 struct arg_list *cur, *bak;
1094 const char *ctx, *where;
1095 const char *conv_ctx, *conv_pre, *conv_pos;
1096 struct userlist *ul;
Willy Tarreau46947782015-01-19 19:00:58 +01001097 struct my_regex *reg;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001098 struct arg *arg;
1099 int cfgerr = 0;
Willy Tarreau46947782015-01-19 19:00:58 +01001100 int rflags;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001101
1102 list_for_each_entry_safe(cur, bak, &p->conf.args.list, list) {
1103 struct proxy *px;
1104 struct server *srv;
1105 char *pname, *sname;
Willy Tarreau46947782015-01-19 19:00:58 +01001106 char *err;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001107
1108 arg = cur->arg;
1109
1110 /* prepare output messages */
1111 conv_pre = conv_pos = conv_ctx = "";
1112 if (cur->conv) {
1113 conv_ctx = cur->conv;
1114 conv_pre = "conversion keyword '";
1115 conv_pos = "' for ";
1116 }
1117
1118 where = "in";
1119 ctx = "sample fetch keyword";
1120 switch (cur->ctx) {
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001121 case ARGC_STK: where = "in stick rule in"; break;
1122 case ARGC_TRK: where = "in tracking rule in"; break;
1123 case ARGC_LOG: where = "in log-format string in"; break;
1124 case ARGC_LOGSD: where = "in log-format-sd string in"; break;
1125 case ARGC_HRQ: where = "in http-request header format string in"; break;
1126 case ARGC_HRS: where = "in http-response header format string in"; break;
1127 case ARGC_UIF: where = "in unique-id-format string in"; break;
1128 case ARGC_RDR: where = "in redirect format string in"; break;
1129 case ARGC_CAP: where = "in capture rule in"; break;
1130 case ARGC_ACL: ctx = "ACL keyword"; break;
1131 case ARGC_SRV: where = "in server directive in"; break;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001132 case ARGC_SPOE: where = "in spoe-message directive in"; break;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001133 }
1134
1135 /* set a few default settings */
1136 px = p;
1137 pname = p->id;
1138
1139 switch (arg->type) {
1140 case ARGT_SRV:
1141 if (!arg->data.str.len) {
1142 Alert("parsing [%s:%d] : missing server name in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1143 cur->file, cur->line,
1144 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
1145 cfgerr++;
1146 continue;
1147 }
1148
1149 /* we support two formats : "bck/srv" and "srv" */
1150 sname = strrchr(arg->data.str.str, '/');
1151
1152 if (sname) {
1153 *sname++ = '\0';
1154 pname = arg->data.str.str;
1155
Willy Tarreau9e0bb102015-05-26 11:24:42 +02001156 px = proxy_be_by_name(pname);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001157 if (!px) {
1158 Alert("parsing [%s:%d] : unable to find proxy '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1159 cur->file, cur->line, pname,
1160 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
1161 cfgerr++;
1162 break;
1163 }
1164 }
1165 else
1166 sname = arg->data.str.str;
1167
1168 srv = findserver(px, sname);
1169 if (!srv) {
1170 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",
1171 cur->file, cur->line, sname, pname,
1172 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
1173 cfgerr++;
1174 break;
1175 }
1176
1177 free(arg->data.str.str);
1178 arg->data.str.str = NULL;
1179 arg->unresolved = 0;
1180 arg->data.srv = srv;
1181 break;
1182
1183 case ARGT_FE:
1184 if (arg->data.str.len) {
1185 pname = arg->data.str.str;
Willy Tarreau9e0bb102015-05-26 11:24:42 +02001186 px = proxy_fe_by_name(pname);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001187 }
1188
1189 if (!px) {
1190 Alert("parsing [%s:%d] : unable to find frontend '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1191 cur->file, cur->line, pname,
1192 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
1193 cfgerr++;
1194 break;
1195 }
1196
1197 if (!(px->cap & PR_CAP_FE)) {
1198 Alert("parsing [%s:%d] : proxy '%s', referenced in arg %d of %s%s%s%s '%s' %s proxy '%s', has not frontend capability.\n",
1199 cur->file, cur->line, pname,
1200 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
1201 cfgerr++;
1202 break;
1203 }
1204
1205 free(arg->data.str.str);
1206 arg->data.str.str = NULL;
1207 arg->unresolved = 0;
1208 arg->data.prx = px;
1209 break;
1210
1211 case ARGT_BE:
1212 if (arg->data.str.len) {
1213 pname = arg->data.str.str;
Willy Tarreau9e0bb102015-05-26 11:24:42 +02001214 px = proxy_be_by_name(pname);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001215 }
1216
1217 if (!px) {
1218 Alert("parsing [%s:%d] : unable to find backend '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1219 cur->file, cur->line, pname,
1220 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
1221 cfgerr++;
1222 break;
1223 }
1224
1225 if (!(px->cap & PR_CAP_BE)) {
1226 Alert("parsing [%s:%d] : proxy '%s', referenced in arg %d of %s%s%s%s '%s' %s proxy '%s', has not backend capability.\n",
1227 cur->file, cur->line, pname,
1228 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
1229 cfgerr++;
1230 break;
1231 }
1232
1233 free(arg->data.str.str);
1234 arg->data.str.str = NULL;
1235 arg->unresolved = 0;
1236 arg->data.prx = px;
1237 break;
1238
1239 case ARGT_TAB:
1240 if (arg->data.str.len) {
1241 pname = arg->data.str.str;
Willy Tarreaue2dc1fa2015-05-26 12:08:07 +02001242 px = proxy_tbl_by_name(pname);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001243 }
1244
1245 if (!px) {
1246 Alert("parsing [%s:%d] : unable to find table '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1247 cur->file, cur->line, pname,
1248 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
1249 cfgerr++;
1250 break;
1251 }
1252
1253 if (!px->table.size) {
1254 Alert("parsing [%s:%d] : no table in proxy '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1255 cur->file, cur->line, pname,
1256 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
1257 cfgerr++;
1258 break;
1259 }
1260
1261 free(arg->data.str.str);
1262 arg->data.str.str = NULL;
1263 arg->unresolved = 0;
1264 arg->data.prx = px;
1265 break;
1266
1267 case ARGT_USR:
1268 if (!arg->data.str.len) {
1269 Alert("parsing [%s:%d] : missing userlist name in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1270 cur->file, cur->line,
1271 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
1272 cfgerr++;
1273 break;
1274 }
1275
1276 if (p->uri_auth && p->uri_auth->userlist &&
1277 !strcmp(p->uri_auth->userlist->name, arg->data.str.str))
1278 ul = p->uri_auth->userlist;
1279 else
1280 ul = auth_find_userlist(arg->data.str.str);
1281
1282 if (!ul) {
1283 Alert("parsing [%s:%d] : unable to find userlist '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1284 cur->file, cur->line, arg->data.str.str,
1285 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
1286 cfgerr++;
1287 break;
1288 }
1289
1290 free(arg->data.str.str);
1291 arg->data.str.str = NULL;
1292 arg->unresolved = 0;
1293 arg->data.usr = ul;
1294 break;
Willy Tarreau46947782015-01-19 19:00:58 +01001295
1296 case ARGT_REG:
1297 if (!arg->data.str.len) {
1298 Alert("parsing [%s:%d] : missing regex in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1299 cur->file, cur->line,
1300 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
1301 cfgerr++;
1302 continue;
1303 }
1304
1305 reg = calloc(1, sizeof(*reg));
1306 if (!reg) {
1307 Alert("parsing [%s:%d] : not enough memory to build regex in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1308 cur->file, cur->line,
1309 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
1310 cfgerr++;
1311 continue;
1312 }
1313
1314 rflags = 0;
1315 rflags |= (arg->type_flags & ARGF_REG_ICASE) ? REG_ICASE : 0;
1316 err = NULL;
1317
Willy Tarreaud817e462015-01-23 20:23:17 +01001318 if (!regex_comp(arg->data.str.str, reg, !(rflags & REG_ICASE), 1 /* capture substr */, &err)) {
Willy Tarreau46947782015-01-19 19:00:58 +01001319 Alert("parsing [%s:%d] : error in regex '%s' in arg %d of %s%s%s%s '%s' %s proxy '%s' : %s.\n",
1320 cur->file, cur->line,
1321 arg->data.str.str,
1322 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id, err);
1323 cfgerr++;
1324 continue;
1325 }
1326
1327 free(arg->data.str.str);
1328 arg->data.str.str = NULL;
1329 arg->unresolved = 0;
1330 arg->data.reg = reg;
1331 break;
1332
1333
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001334 }
1335
1336 LIST_DEL(&cur->list);
1337 free(cur);
1338 } /* end of args processing */
1339
1340 return cfgerr;
1341}
1342
1343/*
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001344 * Process a fetch + format conversion as defined by the sample expression
1345 * <expr> on request or response considering the <opt> parameter. The output is
Adis Nezirovic79beb242015-07-06 15:41:02 +02001346 * not explicitly set to <smp_type>, but shall be compatible with it as
1347 * specified by 'sample_casts' table. If a stable sample can be fetched, or an
1348 * unstable one when <opt> contains SMP_OPT_FINAL, the sample is converted and
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001349 * returned without the SMP_F_MAY_CHANGE flag. If an unstable sample is found
1350 * and <opt> does not contain SMP_OPT_FINAL, then the sample is returned as-is
1351 * with its SMP_F_MAY_CHANGE flag so that the caller can check it and decide to
1352 * take actions (eg: wait longer). If a sample could not be found or could not
Willy Tarreau6bcb0a82014-07-30 08:56:35 +02001353 * be converted, NULL is returned. The caller MUST NOT use the sample if the
1354 * SMP_F_MAY_CHANGE flag is present, as it is used only as a hint that there is
1355 * still hope to get it after waiting longer, and is not converted to string.
1356 * The possible output combinations are the following :
1357 *
1358 * return MAY_CHANGE FINAL Meaning for the sample
1359 * NULL * * Not present and will never be (eg: header)
1360 * smp 0 * Final value converted (eg: header)
1361 * smp 1 0 Not present yet, may appear later (eg: header)
1362 * smp 1 1 never happens (either flag is cleared on output)
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001363 */
Adis Nezirovic79beb242015-07-06 15:41:02 +02001364struct sample *sample_fetch_as_type(struct proxy *px, struct session *sess,
Willy Tarreau192252e2015-04-04 01:47:55 +02001365 struct stream *strm, unsigned int opt,
Adis Nezirovic79beb242015-07-06 15:41:02 +02001366 struct sample_expr *expr, int smp_type)
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001367{
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001368 struct sample *smp = &temp_smp;
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001369
Willy Tarreau6c616e02014-06-25 16:56:41 +02001370 memset(smp, 0, sizeof(*smp));
1371
Willy Tarreau192252e2015-04-04 01:47:55 +02001372 if (!sample_process(px, sess, strm, opt, expr, smp)) {
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001373 if ((smp->flags & SMP_F_MAY_CHANGE) && !(opt & SMP_OPT_FINAL))
1374 return smp;
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001375 return NULL;
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001376 }
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001377
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001378 if (!sample_casts[smp->data.type][smp_type])
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001379 return NULL;
1380
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001381 if (!sample_casts[smp->data.type][smp_type](smp))
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001382 return NULL;
1383
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001384 smp->flags &= ~SMP_F_MAY_CHANGE;
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001385 return smp;
1386}
1387
Christopher Faulet476e5d02016-10-26 11:34:47 +02001388static void release_sample_arg(struct arg *p)
1389{
1390 struct arg *p_back = p;
1391
1392 if (!p)
1393 return;
1394
1395 while (p->type != ARGT_STOP) {
1396 if (p->type == ARGT_STR || p->unresolved) {
1397 free(p->data.str.str);
1398 p->data.str.str = NULL;
1399 p->unresolved = 0;
1400 }
1401 else if (p->type == ARGT_REG) {
1402 if (p->data.reg) {
1403 regex_free(p->data.reg);
1404 free(p->data.reg);
1405 p->data.reg = NULL;
1406 }
1407 }
1408 p++;
1409 }
1410
1411 if (p_back != empty_arg_list)
1412 free(p_back);
1413}
1414
1415void release_sample_expr(struct sample_expr *expr)
1416{
1417 struct sample_conv_expr *conv_expr, *conv_exprb;
1418
1419 if (!expr)
1420 return;
1421
1422 list_for_each_entry_safe(conv_expr, conv_exprb, &expr->conv_exprs, list)
1423 release_sample_arg(conv_expr->arg_p);
1424 release_sample_arg(expr->arg_p);
1425 free(expr);
1426}
1427
Emeric Brun107ca302010-01-04 16:16:05 +01001428/*****************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +02001429/* Sample format convert functions */
Willy Tarreaub8c8f1f2012-04-23 22:38:26 +02001430/* These functions set the data type on return. */
Emeric Brun107ca302010-01-04 16:16:05 +01001431/*****************************************************************/
1432
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001433#ifdef DEBUG_EXPR
1434static int sample_conv_debug(const struct arg *arg_p, struct sample *smp, void *private)
1435{
1436 int i;
1437 struct sample tmp;
1438
1439 if (!(global.mode & MODE_QUIET) || (global.mode & (MODE_VERBOSE | MODE_STARTING))) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001440 fprintf(stderr, "[debug converter] type: %s ", smp_to_type[smp->data.type]);
1441 if (!sample_casts[smp->data.type][SMP_T_STR]) {
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001442 fprintf(stderr, "(undisplayable)");
1443 } else {
1444
1445 /* Copy sample fetch. This put the sample as const, the
1446 * cast will copy data if a transformation is required.
1447 */
1448 memcpy(&tmp, smp, sizeof(struct sample));
1449 tmp.flags = SMP_F_CONST;
1450
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001451 if (!sample_casts[smp->data.type][SMP_T_STR](&tmp))
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001452 fprintf(stderr, "(undisplayable)");
1453
1454 else {
1455 /* Display the displayable chars*. */
1456 fprintf(stderr, "<");
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001457 for (i = 0; i < tmp.data.u.str.len; i++) {
1458 if (isprint(tmp.data.u.str.str[i]))
1459 fputc(tmp.data.u.str.str[i], stderr);
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001460 else
1461 fputc('.', stderr);
1462 }
1463 }
1464 fprintf(stderr, ">\n");
1465 }
1466 }
1467 return 1;
1468}
1469#endif
1470
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001471static int sample_conv_bin2base64(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brun53d1a982014-04-30 18:21:37 +02001472{
1473 struct chunk *trash = get_trash_chunk();
1474 int b64_len;
1475
1476 trash->len = 0;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001477 b64_len = a2base64(smp->data.u.str.str, smp->data.u.str.len, trash->str, trash->size);
Emeric Brun53d1a982014-04-30 18:21:37 +02001478 if (b64_len < 0)
1479 return 0;
1480
1481 trash->len = b64_len;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001482 smp->data.u.str = *trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001483 smp->data.type = SMP_T_STR;
Emeric Brun53d1a982014-04-30 18:21:37 +02001484 smp->flags &= ~SMP_F_CONST;
1485 return 1;
1486}
1487
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001488static int sample_conv_bin2hex(const struct arg *arg_p, struct sample *smp, void *private)
Thierry FOURNIER2f49d6d2014-03-12 15:01:52 +01001489{
1490 struct chunk *trash = get_trash_chunk();
1491 unsigned char c;
1492 int ptr = 0;
1493
1494 trash->len = 0;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001495 while (ptr < smp->data.u.str.len && trash->len <= trash->size - 2) {
1496 c = smp->data.u.str.str[ptr++];
Thierry FOURNIER2f49d6d2014-03-12 15:01:52 +01001497 trash->str[trash->len++] = hextab[(c >> 4) & 0xF];
1498 trash->str[trash->len++] = hextab[c & 0xF];
1499 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001500 smp->data.u.str = *trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001501 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01001502 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIER2f49d6d2014-03-12 15:01:52 +01001503 return 1;
1504}
1505
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001506/* hashes the binary input into a 32-bit unsigned int */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001507static int sample_conv_djb2(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001508{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001509 smp->data.u.sint = hash_djb2(smp->data.u.str.str, smp->data.u.str.len);
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001510 if (arg_p && arg_p->data.sint)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001511 smp->data.u.sint = full_hash(smp->data.u.sint);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001512 smp->data.type = SMP_T_SINT;
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001513 return 1;
1514}
1515
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001516static int sample_conv_str2lower(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brun107ca302010-01-04 16:16:05 +01001517{
1518 int i;
1519
Willy Tarreauf0645dc2016-08-09 14:29:38 +02001520 if (!smp_make_rw(smp))
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01001521 return 0;
1522
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001523 for (i = 0; i < smp->data.u.str.len; i++) {
1524 if ((smp->data.u.str.str[i] >= 'A') && (smp->data.u.str.str[i] <= 'Z'))
1525 smp->data.u.str.str[i] += 'a' - 'A';
Emeric Brun107ca302010-01-04 16:16:05 +01001526 }
1527 return 1;
1528}
1529
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001530static int sample_conv_str2upper(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brun107ca302010-01-04 16:16:05 +01001531{
1532 int i;
1533
Willy Tarreauf0645dc2016-08-09 14:29:38 +02001534 if (!smp_make_rw(smp))
Emeric Brun485479d2010-09-23 18:02:19 +02001535 return 0;
1536
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001537 for (i = 0; i < smp->data.u.str.len; i++) {
1538 if ((smp->data.u.str.str[i] >= 'a') && (smp->data.u.str.str[i] <= 'z'))
1539 smp->data.u.str.str[i] += 'A' - 'a';
Emeric Brun107ca302010-01-04 16:16:05 +01001540 }
1541 return 1;
1542}
1543
Willy Tarreauf9954102012-04-20 14:03:29 +02001544/* takes the netmask in arg_p */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001545static int sample_conv_ipmask(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud31d6eb2010-01-26 18:01:41 +01001546{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001547 smp->data.u.ipv4.s_addr &= arg_p->data.ipv4.s_addr;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001548 smp->data.type = SMP_T_IPV4;
Willy Tarreaud31d6eb2010-01-26 18:01:41 +01001549 return 1;
1550}
1551
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001552/* takes an UINT value on input supposed to represent the time since EPOCH,
1553 * adds an optional offset found in args[1] and emits a string representing
1554 * the local time in the format specified in args[1] using strftime().
1555 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001556static int sample_conv_ltime(const struct arg *args, struct sample *smp, void *private)
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001557{
1558 struct chunk *temp;
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001559 /* With high numbers, the date returned can be negative, the 55 bits mask prevent this. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001560 time_t curr_date = smp->data.u.sint & 0x007fffffffffffffLL;
Thierry FOURNIERfac9ccf2015-07-08 00:15:20 +02001561 struct tm *tm;
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001562
1563 /* add offset */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001564 if (args[1].type == ARGT_SINT)
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001565 curr_date += args[1].data.sint;
1566
Thierry FOURNIERfac9ccf2015-07-08 00:15:20 +02001567 tm = localtime(&curr_date);
1568 if (!tm)
1569 return 0;
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001570 temp = get_trash_chunk();
Thierry FOURNIERfac9ccf2015-07-08 00:15:20 +02001571 temp->len = strftime(temp->str, temp->size, args[0].data.str.str, tm);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001572 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001573 smp->data.type = SMP_T_STR;
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001574 return 1;
1575}
1576
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001577/* hashes the binary input into a 32-bit unsigned int */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001578static int sample_conv_sdbm(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001579{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001580 smp->data.u.sint = hash_sdbm(smp->data.u.str.str, smp->data.u.str.len);
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001581 if (arg_p && arg_p->data.sint)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001582 smp->data.u.sint = full_hash(smp->data.u.sint);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001583 smp->data.type = SMP_T_SINT;
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001584 return 1;
1585}
1586
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001587/* takes an UINT value on input supposed to represent the time since EPOCH,
1588 * adds an optional offset found in args[1] and emits a string representing
1589 * the UTC date in the format specified in args[1] using strftime().
1590 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001591static int sample_conv_utime(const struct arg *args, struct sample *smp, void *private)
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001592{
1593 struct chunk *temp;
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001594 /* With high numbers, the date returned can be negative, the 55 bits mask prevent this. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001595 time_t curr_date = smp->data.u.sint & 0x007fffffffffffffLL;
Thierry FOURNIERfac9ccf2015-07-08 00:15:20 +02001596 struct tm *tm;
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001597
1598 /* add offset */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001599 if (args[1].type == ARGT_SINT)
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001600 curr_date += args[1].data.sint;
1601
Thierry FOURNIERfac9ccf2015-07-08 00:15:20 +02001602 tm = gmtime(&curr_date);
1603 if (!tm)
1604 return 0;
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001605 temp = get_trash_chunk();
Thierry FOURNIERfac9ccf2015-07-08 00:15:20 +02001606 temp->len = strftime(temp->str, temp->size, args[0].data.str.str, tm);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001607 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001608 smp->data.type = SMP_T_STR;
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001609 return 1;
1610}
1611
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001612/* hashes the binary input into a 32-bit unsigned int */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001613static int sample_conv_wt6(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001614{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001615 smp->data.u.sint = hash_wt6(smp->data.u.str.str, smp->data.u.str.len);
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001616 if (arg_p && arg_p->data.sint)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001617 smp->data.u.sint = full_hash(smp->data.u.sint);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001618 smp->data.type = SMP_T_SINT;
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001619 return 1;
1620}
1621
Thierry FOURNIER01e09742016-12-26 11:46:11 +01001622/* hashes the binary input into a 32-bit unsigned int using xxh.
1623 * The seed of the hash defaults to 0 but can be changd in argument 1.
1624 */
1625static int sample_conv_xxh32(const struct arg *arg_p, struct sample *smp, void *private)
1626{
1627 unsigned int seed;
1628
1629 if (arg_p && arg_p->data.sint)
1630 seed = arg_p->data.sint;
1631 else
1632 seed = 0;
1633 smp->data.u.sint = XXH32(smp->data.u.str.str, smp->data.u.str.len, seed);
1634 smp->data.type = SMP_T_SINT;
1635 return 1;
1636}
1637
1638/* hashes the binary input into a 64-bit unsigned int using xxh.
1639 * In fact, the function returns a 64 bit unsigned, but the sample
1640 * storage of haproxy only proposes 64-bits signed, so the value is
1641 * cast as signed. This cast doesn't impact the hash repartition.
1642 * The seed of the hash defaults to 0 but can be changd in argument 1.
1643 */
1644static int sample_conv_xxh64(const struct arg *arg_p, struct sample *smp, void *private)
1645{
1646 unsigned long long int seed;
1647
1648 if (arg_p && arg_p->data.sint)
1649 seed = (unsigned long long int)arg_p->data.sint;
1650 else
1651 seed = 0;
1652 smp->data.u.sint = (long long int)XXH64(smp->data.u.str.str, smp->data.u.str.len, seed);
1653 smp->data.type = SMP_T_SINT;
1654 return 1;
1655}
1656
Willy Tarreau80599772015-01-20 19:35:24 +01001657/* hashes the binary input into a 32-bit unsigned int */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001658static int sample_conv_crc32(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau80599772015-01-20 19:35:24 +01001659{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001660 smp->data.u.sint = hash_crc32(smp->data.u.str.str, smp->data.u.str.len);
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001661 if (arg_p && arg_p->data.sint)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001662 smp->data.u.sint = full_hash(smp->data.u.sint);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001663 smp->data.type = SMP_T_SINT;
Willy Tarreau80599772015-01-20 19:35:24 +01001664 return 1;
1665}
1666
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001667/* This function escape special json characters. The returned string can be
1668 * safely set between two '"' and used as json string. The json string is
1669 * defined like this:
1670 *
1671 * any Unicode character except '"' or '\' or control character
1672 * \", \\, \/, \b, \f, \n, \r, \t, \u + four-hex-digits
1673 *
1674 * The enum input_type contain all the allowed mode for decoding the input
1675 * string.
1676 */
1677enum input_type {
1678 IT_ASCII = 0,
1679 IT_UTF8,
1680 IT_UTF8S,
1681 IT_UTF8P,
1682 IT_UTF8PS,
1683};
1684static int sample_conv_json_check(struct arg *arg, struct sample_conv *conv,
1685 const char *file, int line, char **err)
1686{
1687 if (!arg) {
1688 memprintf(err, "Unexpected empty arg list");
1689 return 0;
1690 }
1691
1692 if (arg->type != ARGT_STR) {
1693 memprintf(err, "Unexpected arg type");
1694 return 0;
1695 }
1696
1697 if (strcmp(arg->data.str.str, "") == 0) {
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001698 arg->type = ARGT_SINT;
1699 arg->data.sint = IT_ASCII;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001700 return 1;
1701 }
1702
1703 else if (strcmp(arg->data.str.str, "ascii") == 0) {
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001704 arg->type = ARGT_SINT;
1705 arg->data.sint = IT_ASCII;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001706 return 1;
1707 }
1708
1709 else if (strcmp(arg->data.str.str, "utf8") == 0) {
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001710 arg->type = ARGT_SINT;
1711 arg->data.sint = IT_UTF8;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001712 return 1;
1713 }
1714
1715 else if (strcmp(arg->data.str.str, "utf8s") == 0) {
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001716 arg->type = ARGT_SINT;
1717 arg->data.sint = IT_UTF8S;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001718 return 1;
1719 }
1720
1721 else if (strcmp(arg->data.str.str, "utf8p") == 0) {
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001722 arg->type = ARGT_SINT;
1723 arg->data.sint = IT_UTF8P;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001724 return 1;
1725 }
1726
1727 else if (strcmp(arg->data.str.str, "utf8ps") == 0) {
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001728 arg->type = ARGT_SINT;
1729 arg->data.sint = IT_UTF8PS;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001730 return 1;
1731 }
1732
Thierry FOURNIER / OZON.IOa69c9122016-11-23 01:13:57 +01001733 memprintf(err, "Unexpected input code type. "
1734 "Allowed value are 'ascii', 'utf8', 'utf8s', 'utf8p' and 'utf8ps'");
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001735 return 0;
1736}
1737
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001738static int sample_conv_json(const struct arg *arg_p, struct sample *smp, void *private)
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001739{
1740 struct chunk *temp;
1741 char _str[7]; /* \u + 4 hex digit + null char for sprintf. */
1742 const char *str;
1743 int len;
1744 enum input_type input_type = IT_ASCII;
1745 unsigned int c;
1746 unsigned int ret;
1747 char *p;
1748
1749 if (arg_p)
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001750 input_type = arg_p->data.sint;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001751
1752 temp = get_trash_chunk();
1753 temp->len = 0;
1754
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001755 p = smp->data.u.str.str;
1756 while (p < smp->data.u.str.str + smp->data.u.str.len) {
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001757
1758 if (input_type == IT_ASCII) {
1759 /* Read input as ASCII. */
1760 c = *(unsigned char *)p;
1761 p++;
1762 }
1763 else {
1764 /* Read input as UTF8. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001765 ret = utf8_next(p, smp->data.u.str.len - ( p - smp->data.u.str.str ), &c);
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001766 p += utf8_return_length(ret);
1767
1768 if (input_type == IT_UTF8 && utf8_return_code(ret) != UTF8_CODE_OK)
1769 return 0;
1770 if (input_type == IT_UTF8S && utf8_return_code(ret) != UTF8_CODE_OK)
1771 continue;
1772 if (input_type == IT_UTF8P && utf8_return_code(ret) & (UTF8_CODE_INVRANGE|UTF8_CODE_BADSEQ))
1773 return 0;
1774 if (input_type == IT_UTF8PS && utf8_return_code(ret) & (UTF8_CODE_INVRANGE|UTF8_CODE_BADSEQ))
1775 continue;
1776
1777 /* Check too big values. */
1778 if ((unsigned int)c > 0xffff) {
1779 if (input_type == IT_UTF8 || input_type == IT_UTF8P)
1780 return 0;
1781 continue;
1782 }
1783 }
1784
1785 /* Convert character. */
1786 if (c == '"') {
1787 len = 2;
1788 str = "\\\"";
1789 }
1790 else if (c == '\\') {
1791 len = 2;
1792 str = "\\\\";
1793 }
1794 else if (c == '/') {
1795 len = 2;
1796 str = "\\/";
1797 }
1798 else if (c == '\b') {
1799 len = 2;
1800 str = "\\b";
1801 }
1802 else if (c == '\f') {
1803 len = 2;
1804 str = "\\f";
1805 }
1806 else if (c == '\r') {
1807 len = 2;
1808 str = "\\r";
1809 }
1810 else if (c == '\n') {
1811 len = 2;
1812 str = "\\n";
1813 }
1814 else if (c == '\t') {
1815 len = 2;
1816 str = "\\t";
1817 }
1818 else if (c > 0xff || !isprint(c)) {
1819 /* isprint generate a segfault if c is too big. The man says that
1820 * c must have the value of an unsigned char or EOF.
1821 */
1822 len = 6;
1823 _str[0] = '\\';
1824 _str[1] = 'u';
1825 snprintf(&_str[2], 5, "%04x", (unsigned short)c);
1826 str = _str;
1827 }
1828 else {
1829 len = 1;
1830 str = (char *)&c;
1831 }
1832
1833 /* Check length */
1834 if (temp->len + len > temp->size)
1835 return 0;
1836
1837 /* Copy string. */
1838 memcpy(temp->str + temp->len, str, len);
1839 temp->len += len;
1840 }
1841
1842 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001843 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001844 smp->data.type = SMP_T_STR;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001845
1846 return 1;
1847}
1848
Emeric Brun54c4ac82014-11-03 15:32:43 +01001849/* This sample function is designed to extract some bytes from an input buffer.
1850 * First arg is the offset.
1851 * Optional second arg is the length to truncate */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001852static int sample_conv_bytes(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brun54c4ac82014-11-03 15:32:43 +01001853{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001854 if (smp->data.u.str.len <= arg_p[0].data.sint) {
1855 smp->data.u.str.len = 0;
Emeric Brun54c4ac82014-11-03 15:32:43 +01001856 return 1;
1857 }
1858
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001859 if (smp->data.u.str.size)
1860 smp->data.u.str.size -= arg_p[0].data.sint;
1861 smp->data.u.str.len -= arg_p[0].data.sint;
1862 smp->data.u.str.str += arg_p[0].data.sint;
Emeric Brun54c4ac82014-11-03 15:32:43 +01001863
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001864 if ((arg_p[1].type == ARGT_SINT) && (arg_p[1].data.sint < smp->data.u.str.len))
1865 smp->data.u.str.len = arg_p[1].data.sint;
Emeric Brun54c4ac82014-11-03 15:32:43 +01001866
1867 return 1;
1868}
1869
Emeric Brunf399b0d2014-11-03 17:07:03 +01001870static int sample_conv_field_check(struct arg *args, struct sample_conv *conv,
1871 const char *file, int line, char **err)
1872{
1873 struct arg *arg = args;
1874
1875 if (!arg) {
1876 memprintf(err, "Unexpected empty arg list");
1877 return 0;
1878 }
1879
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001880 if (arg->type != ARGT_SINT) {
Emeric Brunf399b0d2014-11-03 17:07:03 +01001881 memprintf(err, "Unexpected arg type");
1882 return 0;
1883 }
1884
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001885 if (!arg->data.sint) {
Emeric Brunf399b0d2014-11-03 17:07:03 +01001886 memprintf(err, "Unexpected value 0 for index");
1887 return 0;
1888 }
1889
1890 arg++;
1891
1892 if (arg->type != ARGT_STR) {
1893 memprintf(err, "Unexpected arg type");
1894 return 0;
1895 }
1896
1897 if (!arg->data.str.len) {
1898 memprintf(err, "Empty separators list");
1899 return 0;
1900 }
1901
1902 return 1;
1903}
1904
1905/* This sample function is designed to a return selected part of a string (field).
1906 * First arg is the index of the field (start at 1)
1907 * Second arg is a char list of separators (type string)
1908 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001909static int sample_conv_field(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brunf399b0d2014-11-03 17:07:03 +01001910{
1911 unsigned int field;
1912 char *start, *end;
1913 int i;
1914
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001915 if (!arg_p[0].data.sint)
Emeric Brunf399b0d2014-11-03 17:07:03 +01001916 return 0;
1917
1918 field = 1;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001919 end = start = smp->data.u.str.str;
1920 while (end - smp->data.u.str.str < smp->data.u.str.len) {
Emeric Brunf399b0d2014-11-03 17:07:03 +01001921
1922 for (i = 0 ; i < arg_p[1].data.str.len ; i++) {
1923 if (*end == arg_p[1].data.str.str[i]) {
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001924 if (field == arg_p[0].data.sint)
Emeric Brunf399b0d2014-11-03 17:07:03 +01001925 goto found;
1926 start = end+1;
1927 field++;
1928 break;
1929 }
1930 }
1931 end++;
1932 }
1933
1934 /* Field not found */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001935 if (field != arg_p[0].data.sint) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001936 smp->data.u.str.len = 0;
Emeric Brunf399b0d2014-11-03 17:07:03 +01001937 return 1;
1938 }
1939found:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001940 smp->data.u.str.len = end - start;
Emeric Brunf399b0d2014-11-03 17:07:03 +01001941 /* If ret string is len 0, no need to
1942 change pointers or to update size */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001943 if (!smp->data.u.str.len)
Emeric Brunf399b0d2014-11-03 17:07:03 +01001944 return 1;
1945
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001946 smp->data.u.str.str = start;
Emeric Brunf399b0d2014-11-03 17:07:03 +01001947
1948 /* Compute remaining size if needed
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001949 Note: smp->data.u.str.size cannot be set to 0 */
1950 if (smp->data.u.str.size)
1951 smp->data.u.str.size -= start - smp->data.u.str.str;
Emeric Brunf399b0d2014-11-03 17:07:03 +01001952
1953 return 1;
1954}
1955
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01001956/* This sample function is designed to return a word from a string.
1957 * First arg is the index of the word (start at 1)
1958 * Second arg is a char list of words separators (type string)
1959 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001960static int sample_conv_word(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01001961{
1962 unsigned int word;
1963 char *start, *end;
1964 int i, issep, inword;
1965
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001966 if (!arg_p[0].data.sint)
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01001967 return 0;
1968
1969 word = 0;
1970 inword = 0;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001971 end = start = smp->data.u.str.str;
1972 while (end - smp->data.u.str.str < smp->data.u.str.len) {
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01001973 issep = 0;
1974 for (i = 0 ; i < arg_p[1].data.str.len ; i++) {
1975 if (*end == arg_p[1].data.str.str[i]) {
1976 issep = 1;
1977 break;
1978 }
1979 }
1980 if (!inword) {
1981 if (!issep) {
1982 word++;
1983 start = end;
1984 inword = 1;
1985 }
1986 }
1987 else if (issep) {
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001988 if (word == arg_p[0].data.sint)
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01001989 goto found;
1990 inword = 0;
1991 }
1992 end++;
1993 }
1994
1995 /* Field not found */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001996 if (word != arg_p[0].data.sint) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001997 smp->data.u.str.len = 0;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01001998 return 1;
1999 }
2000found:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002001 smp->data.u.str.len = end - start;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002002 /* If ret string is len 0, no need to
2003 change pointers or to update size */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002004 if (!smp->data.u.str.len)
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002005 return 1;
2006
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002007 smp->data.u.str.str = start;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002008
2009 /* Compute remaining size if needed
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002010 Note: smp->data.u.str.size cannot be set to 0 */
2011 if (smp->data.u.str.size)
2012 smp->data.u.str.size -= start - smp->data.u.str.str;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002013
2014 return 1;
2015}
2016
Willy Tarreau7eda8492015-01-20 19:47:06 +01002017static int sample_conv_regsub_check(struct arg *args, struct sample_conv *conv,
2018 const char *file, int line, char **err)
2019{
2020 struct arg *arg = args;
2021 char *p;
2022 int len;
2023
2024 /* arg0 is a regex, it uses type_flag for ICASE and global match */
2025 arg[0].type_flags = 0;
2026
2027 if (arg[2].type != ARGT_STR)
2028 return 1;
2029
2030 p = arg[2].data.str.str;
2031 len = arg[2].data.str.len;
2032 while (len) {
2033 if (*p == 'i') {
2034 arg[0].type_flags |= ARGF_REG_ICASE;
2035 }
2036 else if (*p == 'g') {
2037 arg[0].type_flags |= ARGF_REG_GLOB;
2038 }
2039 else {
2040 memprintf(err, "invalid regex flag '%c', only 'i' and 'g' are supported", *p);
2041 return 0;
2042 }
2043 p++;
2044 len--;
2045 }
2046 return 1;
2047}
2048
2049/* This sample function is designed to do the equivalent of s/match/replace/ on
2050 * the input string. It applies a regex and restarts from the last matched
2051 * location until nothing matches anymore. First arg is the regex to apply to
2052 * the input string, second arg is the replacement expression.
2053 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002054static int sample_conv_regsub(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau7eda8492015-01-20 19:47:06 +01002055{
2056 char *start, *end;
2057 struct my_regex *reg = arg_p[0].data.reg;
2058 regmatch_t pmatch[MAX_MATCH];
2059 struct chunk *trash = get_trash_chunk();
2060 int flag, max;
2061 int found;
2062
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002063 start = smp->data.u.str.str;
2064 end = start + smp->data.u.str.len;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002065
2066 flag = 0;
2067 while (1) {
2068 /* check for last round which is used to copy remaining parts
2069 * when not running in global replacement mode.
2070 */
2071 found = 0;
2072 if ((arg_p[0].type_flags & ARGF_REG_GLOB) || !(flag & REG_NOTBOL)) {
2073 /* Note: we can have start == end on empty strings or at the end */
2074 found = regex_exec_match2(reg, start, end - start, MAX_MATCH, pmatch, flag);
2075 }
2076
2077 if (!found)
2078 pmatch[0].rm_so = end - start;
2079
2080 /* copy the heading non-matching part (which may also be the tail if nothing matches) */
2081 max = trash->size - trash->len;
2082 if (max && pmatch[0].rm_so > 0) {
2083 if (max > pmatch[0].rm_so)
2084 max = pmatch[0].rm_so;
2085 memcpy(trash->str + trash->len, start, max);
2086 trash->len += max;
2087 }
2088
2089 if (!found)
2090 break;
2091
2092 /* replace the matching part */
2093 max = trash->size - trash->len;
2094 if (max) {
2095 if (max > arg_p[1].data.str.len)
2096 max = arg_p[1].data.str.len;
2097 memcpy(trash->str + trash->len, arg_p[1].data.str.str, max);
2098 trash->len += max;
2099 }
2100
2101 /* stop here if we're done with this string */
2102 if (start >= end)
2103 break;
2104
2105 /* We have a special case for matches of length 0 (eg: "x*y*").
2106 * These ones are considered to match in front of a character,
2107 * so we have to copy that character and skip to the next one.
2108 */
2109 if (!pmatch[0].rm_eo) {
2110 if (trash->len < trash->size)
2111 trash->str[trash->len++] = start[pmatch[0].rm_eo];
2112 pmatch[0].rm_eo++;
2113 }
2114
2115 start += pmatch[0].rm_eo;
2116 flag |= REG_NOTBOL;
2117 }
2118
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002119 smp->data.u.str = *trash;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002120 return 1;
2121}
2122
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002123/* This function check an operator entry. It expects a string.
2124 * The string can be an integer or a variable name.
2125 */
2126static int check_operator(struct arg *args, struct sample_conv *conv,
2127 const char *file, int line, char **err)
2128{
2129 const char *str;
2130 const char *end;
2131
2132 /* Try to decode a variable. */
2133 if (vars_check_arg(&args[0], NULL))
2134 return 1;
2135
2136 /* Try to convert an integer */
2137 str = args[0].data.str.str;
2138 end = str + strlen(str);
2139 args[0].data.sint = read_int64(&str, end);
2140 if (*str != '\0') {
2141 memprintf(err, "expects an integer or a variable name");
2142 return 0;
2143 }
2144 args[0].type = ARGT_SINT;
2145 return 1;
2146}
2147
Willy Tarreau6204cd92016-03-10 16:33:04 +01002148/* This function returns a sample struct filled with an arg content.
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002149 * If the arg contain an integer, the integer is returned in the
2150 * sample. If the arg contains a variable descriptor, it returns the
2151 * variable value.
2152 *
2153 * This function returns 0 if an error occurs, otherwise it returns 1.
2154 */
Willy Tarreau6204cd92016-03-10 16:33:04 +01002155static inline int sample_conv_var2smp(const struct arg *arg, struct sample *smp)
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002156{
2157 switch (arg->type) {
2158 case ARGT_SINT:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002159 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002160 smp->data.u.sint = arg->data.sint;
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002161 return 1;
2162 case ARGT_VAR:
Willy Tarreau6204cd92016-03-10 16:33:04 +01002163 if (!vars_get_by_desc(&arg->data.var, smp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002164 return 0;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002165 if (!sample_casts[smp->data.type][SMP_T_SINT])
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002166 return 0;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002167 if (!sample_casts[smp->data.type][SMP_T_SINT](smp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002168 return 0;
2169 return 1;
2170 default:
2171 return 0;
2172 }
2173}
2174
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002175/* Takes a SINT on input, applies a binary twos complement and returns the SINT
Willy Tarreau97707872015-01-27 15:12:13 +01002176 * result.
2177 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002178static int sample_conv_binary_cpl(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002179{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002180 smp->data.u.sint = ~smp->data.u.sint;
Willy Tarreau97707872015-01-27 15:12:13 +01002181 return 1;
2182}
2183
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002184/* Takes a SINT on input, applies a binary "and" with the SINT directly in
2185 * arg_p or in the varaible described in arg_p, and returns the SINT result.
Willy Tarreau97707872015-01-27 15:12:13 +01002186 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002187static int sample_conv_binary_and(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002188{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002189 struct sample tmp;
2190
Willy Tarreau7560dd42016-03-10 16:28:58 +01002191 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreau6204cd92016-03-10 16:33:04 +01002192 if (!sample_conv_var2smp(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002193 return 0;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002194 smp->data.u.sint &= tmp.data.u.sint;
Willy Tarreau97707872015-01-27 15:12:13 +01002195 return 1;
2196}
2197
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002198/* Takes a SINT on input, applies a binary "or" with the SINT directly in
2199 * arg_p or in the varaible described in arg_p, and returns the SINT result.
Willy Tarreau97707872015-01-27 15:12:13 +01002200 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002201static int sample_conv_binary_or(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002202{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002203 struct sample tmp;
2204
Willy Tarreau7560dd42016-03-10 16:28:58 +01002205 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreau6204cd92016-03-10 16:33:04 +01002206 if (!sample_conv_var2smp(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002207 return 0;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002208 smp->data.u.sint |= tmp.data.u.sint;
Willy Tarreau97707872015-01-27 15:12:13 +01002209 return 1;
2210}
2211
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002212/* Takes a SINT on input, applies a binary "xor" with the SINT directly in
2213 * arg_p or in the varaible described in arg_p, and returns the SINT result.
Willy Tarreau97707872015-01-27 15:12:13 +01002214 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002215static int sample_conv_binary_xor(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002216{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002217 struct sample tmp;
2218
Willy Tarreau7560dd42016-03-10 16:28:58 +01002219 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreau6204cd92016-03-10 16:33:04 +01002220 if (!sample_conv_var2smp(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002221 return 0;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002222 smp->data.u.sint ^= tmp.data.u.sint;
Willy Tarreau97707872015-01-27 15:12:13 +01002223 return 1;
2224}
2225
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002226static inline long long int arith_add(long long int a, long long int b)
2227{
2228 /* Prevent overflow and makes capped calculus.
2229 * We must ensure that the check calculus doesn't
2230 * exceed the signed 64 bits limits.
2231 *
2232 * +----------+----------+
2233 * | a<0 | a>=0 |
2234 * +------+----------+----------+
2235 * | b<0 | MIN-a>b | no check |
2236 * +------+----------+----------+
2237 * | b>=0 | no check | MAX-a<b |
2238 * +------+----------+----------+
2239 */
2240 if ((a ^ b) >= 0) {
2241 /* signs are differents. */
2242 if (a < 0) {
2243 if (LLONG_MIN - a > b)
2244 return LLONG_MIN;
2245 }
2246 if (LLONG_MAX - a < b)
2247 return LLONG_MAX;
2248 }
2249 return a + b;
2250}
2251
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002252/* Takes a SINT on input, applies an arithmetic "add" with the SINT directly in
2253 * arg_p or in the varaible described in arg_p, and returns the SINT result.
Willy Tarreau97707872015-01-27 15:12:13 +01002254 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002255static int sample_conv_arith_add(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002256{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002257 struct sample tmp;
2258
Willy Tarreau7560dd42016-03-10 16:28:58 +01002259 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreau6204cd92016-03-10 16:33:04 +01002260 if (!sample_conv_var2smp(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002261 return 0;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002262 smp->data.u.sint = arith_add(smp->data.u.sint, tmp.data.u.sint);
Willy Tarreau97707872015-01-27 15:12:13 +01002263 return 1;
2264}
2265
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002266/* Takes a SINT on input, applies an arithmetic "sub" with the SINT directly in
2267 * arg_p or in the varaible described in arg_p, and returns the SINT result.
Willy Tarreau97707872015-01-27 15:12:13 +01002268 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002269static int sample_conv_arith_sub(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002270 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002271{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002272 struct sample tmp;
2273
Willy Tarreau7560dd42016-03-10 16:28:58 +01002274 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreau6204cd92016-03-10 16:33:04 +01002275 if (!sample_conv_var2smp(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002276 return 0;
2277
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002278 /* We cannot represent -LLONG_MIN because abs(LLONG_MIN) is greater
2279 * than abs(LLONG_MAX). So, the following code use LLONG_MAX in place
2280 * of -LLONG_MIN and correct the result.
2281 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002282 if (tmp.data.u.sint == LLONG_MIN) {
2283 smp->data.u.sint = arith_add(smp->data.u.sint, LLONG_MAX);
2284 if (smp->data.u.sint < LLONG_MAX)
2285 smp->data.u.sint++;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002286 return 1;
2287 }
2288
2289 /* standard substraction: we use the "add" function and negate
2290 * the second operand.
2291 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002292 smp->data.u.sint = arith_add(smp->data.u.sint, -tmp.data.u.sint);
Willy Tarreau97707872015-01-27 15:12:13 +01002293 return 1;
2294}
2295
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002296/* Takes a SINT on input, applies an arithmetic "mul" with the SINT directly in
2297 * arg_p or in the varaible described in arg_p, and returns the SINT result.
2298 * If the result makes an overflow, then the largest possible quantity is
2299 * returned.
Willy Tarreau97707872015-01-27 15:12:13 +01002300 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002301static int sample_conv_arith_mul(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002302 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002303{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002304 struct sample tmp;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002305 long long int c;
2306
Willy Tarreau7560dd42016-03-10 16:28:58 +01002307 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreau6204cd92016-03-10 16:33:04 +01002308 if (!sample_conv_var2smp(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002309 return 0;
2310
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002311 /* prevent divide by 0 during the check */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002312 if (!smp->data.u.sint || !tmp.data.u.sint) {
2313 smp->data.u.sint = 0;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002314 return 1;
2315 }
2316
2317 /* The multiply between LLONG_MIN and -1 returns a
2318 * "floting point exception".
2319 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002320 if (smp->data.u.sint == LLONG_MIN && tmp.data.u.sint == -1) {
2321 smp->data.u.sint = LLONG_MAX;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002322 return 1;
2323 }
2324
2325 /* execute standard multiplication. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002326 c = smp->data.u.sint * tmp.data.u.sint;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002327
2328 /* check for overflow and makes capped multiply. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002329 if (smp->data.u.sint != c / tmp.data.u.sint) {
2330 if ((smp->data.u.sint < 0) == (tmp.data.u.sint < 0)) {
2331 smp->data.u.sint = LLONG_MAX;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002332 return 1;
2333 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002334 smp->data.u.sint = LLONG_MIN;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002335 return 1;
2336 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002337 smp->data.u.sint = c;
Willy Tarreau97707872015-01-27 15:12:13 +01002338 return 1;
2339}
2340
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002341/* Takes a SINT on input, applies an arithmetic "div" with the SINT directly in
2342 * arg_p or in the varaible described in arg_p, and returns the SINT result.
2343 * If arg_p makes the result overflow, then the largest possible quantity is
2344 * returned.
Willy Tarreau97707872015-01-27 15:12:13 +01002345 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002346static int sample_conv_arith_div(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002347 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002348{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002349 struct sample tmp;
2350
Willy Tarreau7560dd42016-03-10 16:28:58 +01002351 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreau6204cd92016-03-10 16:33:04 +01002352 if (!sample_conv_var2smp(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002353 return 0;
2354
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002355 if (tmp.data.u.sint) {
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002356 /* The divide between LLONG_MIN and -1 returns a
2357 * "floting point exception".
2358 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002359 if (smp->data.u.sint == LLONG_MIN && tmp.data.u.sint == -1) {
2360 smp->data.u.sint = LLONG_MAX;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002361 return 1;
2362 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002363 smp->data.u.sint /= tmp.data.u.sint;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002364 return 1;
2365 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002366 smp->data.u.sint = LLONG_MAX;
Willy Tarreau97707872015-01-27 15:12:13 +01002367 return 1;
2368}
2369
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002370/* Takes a SINT on input, applies an arithmetic "mod" with the SINT directly in
2371 * arg_p or in the varaible described in arg_p, and returns the SINT result.
2372 * If arg_p makes the result overflow, then 0 is returned.
Willy Tarreau97707872015-01-27 15:12:13 +01002373 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002374static int sample_conv_arith_mod(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002375 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002376{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002377 struct sample tmp;
2378
Willy Tarreau7560dd42016-03-10 16:28:58 +01002379 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreau6204cd92016-03-10 16:33:04 +01002380 if (!sample_conv_var2smp(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002381 return 0;
2382
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002383 if (tmp.data.u.sint) {
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002384 /* The divide between LLONG_MIN and -1 returns a
2385 * "floting point exception".
2386 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002387 if (smp->data.u.sint == LLONG_MIN && tmp.data.u.sint == -1) {
2388 smp->data.u.sint = 0;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002389 return 1;
2390 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002391 smp->data.u.sint %= tmp.data.u.sint;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002392 return 1;
2393 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002394 smp->data.u.sint = 0;
Willy Tarreau97707872015-01-27 15:12:13 +01002395 return 1;
2396}
2397
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002398/* Takes an SINT on input, applies an arithmetic "neg" and returns the SINT
Willy Tarreau97707872015-01-27 15:12:13 +01002399 * result.
2400 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002401static int sample_conv_arith_neg(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002402 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002403{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002404 if (smp->data.u.sint == LLONG_MIN)
2405 smp->data.u.sint = LLONG_MAX;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002406 else
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002407 smp->data.u.sint = -smp->data.u.sint;
Willy Tarreau97707872015-01-27 15:12:13 +01002408 return 1;
2409}
2410
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002411/* Takes a SINT on input, returns true is the value is non-null, otherwise
Willy Tarreau97707872015-01-27 15:12:13 +01002412 * false. The output is a BOOL.
2413 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002414static int sample_conv_arith_bool(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 smp->data.u.sint = !!smp->data.u.sint;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002418 smp->data.type = SMP_T_BOOL;
Willy Tarreau97707872015-01-27 15:12:13 +01002419 return 1;
2420}
2421
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002422/* Takes a SINT on input, returns false is the value is non-null, otherwise
Willy Tarreau97707872015-01-27 15:12:13 +01002423 * truee. The output is a BOOL.
2424 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002425static int sample_conv_arith_not(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002426 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002427{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002428 smp->data.u.sint = !smp->data.u.sint;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002429 smp->data.type = SMP_T_BOOL;
Willy Tarreau97707872015-01-27 15:12:13 +01002430 return 1;
2431}
2432
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002433/* Takes a SINT on input, returns true is the value is odd, otherwise false.
Willy Tarreau97707872015-01-27 15:12:13 +01002434 * The output is a BOOL.
2435 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002436static int sample_conv_arith_odd(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002437 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002438{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002439 smp->data.u.sint = smp->data.u.sint & 1;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002440 smp->data.type = SMP_T_BOOL;
Willy Tarreau97707872015-01-27 15:12:13 +01002441 return 1;
2442}
2443
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002444/* Takes a SINT on input, returns true is the value is even, otherwise false.
Willy Tarreau97707872015-01-27 15:12:13 +01002445 * The output is a BOOL.
2446 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002447static int sample_conv_arith_even(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002448 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002449{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002450 smp->data.u.sint = !(smp->data.u.sint & 1);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002451 smp->data.type = SMP_T_BOOL;
Willy Tarreau97707872015-01-27 15:12:13 +01002452 return 1;
2453}
2454
Willy Tarreau5b8ad222013-07-25 12:17:57 +02002455/************************************************************************/
2456/* All supported sample fetch functions must be declared here */
2457/************************************************************************/
2458
2459/* force TRUE to be returned at the fetch level */
2460static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002461smp_fetch_true(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau5b8ad222013-07-25 12:17:57 +02002462{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002463 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002464 smp->data.u.sint = 1;
Willy Tarreau5b8ad222013-07-25 12:17:57 +02002465 return 1;
2466}
2467
2468/* force FALSE to be returned at the fetch level */
2469static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002470smp_fetch_false(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau5b8ad222013-07-25 12:17:57 +02002471{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002472 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002473 smp->data.u.sint = 0;
Willy Tarreau5b8ad222013-07-25 12:17:57 +02002474 return 1;
2475}
2476
2477/* retrieve environment variable $1 as a string */
2478static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002479smp_fetch_env(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau5b8ad222013-07-25 12:17:57 +02002480{
2481 char *env;
2482
2483 if (!args || args[0].type != ARGT_STR)
2484 return 0;
2485
2486 env = getenv(args[0].data.str.str);
2487 if (!env)
2488 return 0;
2489
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002490 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01002491 smp->flags = SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002492 smp->data.u.str.str = env;
2493 smp->data.u.str.len = strlen(env);
Willy Tarreau5b8ad222013-07-25 12:17:57 +02002494 return 1;
2495}
2496
Willy Tarreau6236d3a2013-07-25 14:28:25 +02002497/* retrieve the current local date in epoch time, and applies an optional offset
2498 * of args[0] seconds.
2499 */
2500static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002501smp_fetch_date(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau6236d3a2013-07-25 14:28:25 +02002502{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002503 smp->data.u.sint = date.tv_sec;
Willy Tarreau6236d3a2013-07-25 14:28:25 +02002504
2505 /* add offset */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002506 if (args && args[0].type == ARGT_SINT)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002507 smp->data.u.sint += args[0].data.sint;
Willy Tarreau6236d3a2013-07-25 14:28:25 +02002508
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002509 smp->data.type = SMP_T_SINT;
Willy Tarreau6236d3a2013-07-25 14:28:25 +02002510 smp->flags |= SMP_F_VOL_TEST | SMP_F_MAY_CHANGE;
2511 return 1;
2512}
2513
Willy Tarreau0f30d262014-11-24 16:02:05 +01002514/* returns the number of processes */
2515static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002516smp_fetch_nbproc(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau0f30d262014-11-24 16:02:05 +01002517{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002518 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002519 smp->data.u.sint = global.nbproc;
Willy Tarreau0f30d262014-11-24 16:02:05 +01002520 return 1;
2521}
2522
2523/* returns the number of the current process (between 1 and nbproc */
2524static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002525smp_fetch_proc(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau0f30d262014-11-24 16:02:05 +01002526{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002527 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002528 smp->data.u.sint = relative_pid;
Willy Tarreau0f30d262014-11-24 16:02:05 +01002529 return 1;
2530}
2531
Willy Tarreau84310e22014-02-14 11:59:04 +01002532/* generate a random 32-bit integer for whatever purpose, with an optional
2533 * range specified in argument.
2534 */
2535static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002536smp_fetch_rand(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau84310e22014-02-14 11:59:04 +01002537{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002538 smp->data.u.sint = random();
Willy Tarreau84310e22014-02-14 11:59:04 +01002539
2540 /* reduce if needed. Don't do a modulo, use all bits! */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002541 if (args && args[0].type == ARGT_SINT)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002542 smp->data.u.sint = (smp->data.u.sint * args[0].data.sint) / ((u64)RAND_MAX+1);
Willy Tarreau84310e22014-02-14 11:59:04 +01002543
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002544 smp->data.type = SMP_T_SINT;
Willy Tarreau84310e22014-02-14 11:59:04 +01002545 smp->flags |= SMP_F_VOL_TEST | SMP_F_MAY_CHANGE;
2546 return 1;
2547}
2548
Willy Tarreau0f30d262014-11-24 16:02:05 +01002549/* returns true if the current process is stopping */
2550static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002551smp_fetch_stopping(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau0f30d262014-11-24 16:02:05 +01002552{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002553 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002554 smp->data.u.sint = stopping;
Willy Tarreau0f30d262014-11-24 16:02:05 +01002555 return 1;
2556}
2557
Thierry FOURNIERcc103292015-06-06 19:30:17 +02002558static int smp_fetch_const_str(const struct arg *args, struct sample *smp, const char *kw, void *private)
2559{
2560 smp->flags |= SMP_F_CONST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002561 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002562 smp->data.u.str.str = args[0].data.str.str;
2563 smp->data.u.str.len = args[0].data.str.len;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02002564 return 1;
2565}
2566
2567static int smp_check_const_bool(struct arg *args, char **err)
2568{
2569 if (strcasecmp(args[0].data.str.str, "true") == 0 ||
2570 strcasecmp(args[0].data.str.str, "1") == 0) {
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002571 args[0].type = ARGT_SINT;
2572 args[0].data.sint = 1;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02002573 return 1;
2574 }
2575 if (strcasecmp(args[0].data.str.str, "false") == 0 ||
2576 strcasecmp(args[0].data.str.str, "0") == 0) {
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002577 args[0].type = ARGT_SINT;
2578 args[0].data.sint = 0;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02002579 return 1;
2580 }
2581 memprintf(err, "Expects 'true', 'false', '0' or '1'");
2582 return 0;
2583}
2584
2585static int smp_fetch_const_bool(const struct arg *args, struct sample *smp, const char *kw, void *private)
2586{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002587 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002588 smp->data.u.sint = args[0].data.sint;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02002589 return 1;
2590}
2591
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002592static int smp_fetch_const_int(const struct arg *args, struct sample *smp, const char *kw, void *private)
Thierry FOURNIERcc103292015-06-06 19:30:17 +02002593{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002594 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002595 smp->data.u.sint = args[0].data.sint;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02002596 return 1;
2597}
2598
2599static int smp_fetch_const_ipv4(const struct arg *args, struct sample *smp, const char *kw, void *private)
2600{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002601 smp->data.type = SMP_T_IPV4;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002602 smp->data.u.ipv4 = args[0].data.ipv4;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02002603 return 1;
2604}
2605
2606static int smp_fetch_const_ipv6(const struct arg *args, struct sample *smp, const char *kw, void *private)
2607{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002608 smp->data.type = SMP_T_IPV6;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002609 smp->data.u.ipv6 = args[0].data.ipv6;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02002610 return 1;
2611}
2612
2613static int smp_check_const_bin(struct arg *args, char **err)
2614{
David Carlier64a16ab2016-04-08 10:37:02 +01002615 char *binstr = NULL;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02002616 int binstrlen;
2617
2618 if (!parse_binary(args[0].data.str.str, &binstr, &binstrlen, err))
2619 return 0;
2620 args[0].type = ARGT_STR;
2621 args[0].data.str.str = binstr;
2622 args[0].data.str.len = binstrlen;
2623 return 1;
2624}
2625
2626static int smp_fetch_const_bin(const struct arg *args, struct sample *smp, const char *kw, void *private)
2627{
2628 smp->flags |= SMP_F_CONST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002629 smp->data.type = SMP_T_BIN;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002630 smp->data.u.str.str = args[0].data.str.str;
2631 smp->data.u.str.len = args[0].data.str.len;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02002632 return 1;
2633}
2634
2635static int smp_check_const_meth(struct arg *args, char **err)
2636{
2637 enum http_meth_t meth;
2638 int i;
2639
2640 meth = find_http_meth(args[0].data.str.str, args[0].data.str.len);
2641 if (meth != HTTP_METH_OTHER) {
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002642 args[0].type = ARGT_SINT;
2643 args[0].data.sint = meth;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02002644 } else {
2645 /* Check method avalaibility. A methos is a token defined as :
2646 * tchar = "!" / "#" / "$" / "%" / "&" / "'" / "*" / "+" / "-" / "." /
2647 * "^" / "_" / "`" / "|" / "~" / DIGIT / ALPHA
2648 * token = 1*tchar
2649 */
2650 for (i = 0; i < args[0].data.str.len; i++) {
Willy Tarreau2235b262016-11-05 15:50:20 +01002651 if (!HTTP_IS_TOKEN(args[0].data.str.str[i])) {
Thierry FOURNIERcc103292015-06-06 19:30:17 +02002652 memprintf(err, "expects valid method.");
2653 return 0;
2654 }
2655 }
2656 }
2657 return 1;
2658}
2659
2660static int smp_fetch_const_meth(const struct arg *args, struct sample *smp, const char *kw, void *private)
2661{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002662 smp->data.type = SMP_T_METH;
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002663 if (args[0].type == ARGT_SINT) {
Thierry FOURNIERcc103292015-06-06 19:30:17 +02002664 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002665 smp->data.u.meth.meth = args[0].data.sint;
2666 smp->data.u.meth.str.str = "";
2667 smp->data.u.meth.str.len = 0;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02002668 } else {
2669 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002670 smp->data.u.meth.meth = HTTP_METH_OTHER;
2671 smp->data.u.meth.str.str = args[0].data.str.str;
2672 smp->data.u.meth.str.len = args[0].data.str.len;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02002673 }
2674 return 1;
2675}
2676
Willy Tarreau5b8ad222013-07-25 12:17:57 +02002677/* Note: must not be declared <const> as its list will be overwritten.
2678 * Note: fetches that may return multiple types must be declared as the lowest
2679 * common denominator, the type that can be casted into all other ones. For
2680 * instance IPv4/IPv6 must be declared IPv4.
2681 */
2682static struct sample_fetch_kw_list smp_kws = {ILH, {
2683 { "always_false", smp_fetch_false, 0, NULL, SMP_T_BOOL, SMP_USE_INTRN },
2684 { "always_true", smp_fetch_true, 0, NULL, SMP_T_BOOL, SMP_USE_INTRN },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01002685 { "env", smp_fetch_env, ARG1(1,STR), NULL, SMP_T_STR, SMP_USE_INTRN },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002686 { "date", smp_fetch_date, ARG1(0,SINT), NULL, SMP_T_SINT, SMP_USE_INTRN },
2687 { "nbproc", smp_fetch_nbproc,0, NULL, SMP_T_SINT, SMP_USE_INTRN },
2688 { "proc", smp_fetch_proc, 0, NULL, SMP_T_SINT, SMP_USE_INTRN },
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002689 { "rand", smp_fetch_rand, ARG1(0,SINT), NULL, SMP_T_SINT, SMP_USE_INTRN },
Willy Tarreau0f30d262014-11-24 16:02:05 +01002690 { "stopping", smp_fetch_stopping, 0, NULL, SMP_T_BOOL, SMP_USE_INTRN },
Thierry FOURNIERcc103292015-06-06 19:30:17 +02002691
2692 { "str", smp_fetch_const_str, ARG1(1,STR), NULL , SMP_T_STR, SMP_USE_INTRN },
2693 { "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 +02002694 { "int", smp_fetch_const_int, ARG1(1,SINT), NULL , SMP_T_SINT, SMP_USE_INTRN },
Thierry FOURNIERcc103292015-06-06 19:30:17 +02002695 { "ipv4", smp_fetch_const_ipv4, ARG1(1,IPV4), NULL , SMP_T_IPV4, SMP_USE_INTRN },
2696 { "ipv6", smp_fetch_const_ipv6, ARG1(1,IPV6), NULL , SMP_T_IPV6, SMP_USE_INTRN },
2697 { "bin", smp_fetch_const_bin, ARG1(1,STR), smp_check_const_bin , SMP_T_BIN, SMP_USE_INTRN },
2698 { "meth", smp_fetch_const_meth, ARG1(1,STR), smp_check_const_meth, SMP_T_METH, SMP_USE_INTRN },
2699
Willy Tarreau5b8ad222013-07-25 12:17:57 +02002700 { /* END */ },
2701}};
2702
Emeric Brun107ca302010-01-04 16:16:05 +01002703/* Note: must not be declared <const> as its list will be overwritten */
Willy Tarreaudc13c112013-06-21 23:16:39 +02002704static struct sample_conv_kw_list sample_conv_kws = {ILH, {
Thierry FOURNIER9687c772015-05-07 15:46:29 +02002705#ifdef DEBUG_EXPR
2706 { "debug", sample_conv_debug, 0, NULL, SMP_T_ANY, SMP_T_ANY },
2707#endif
2708
Emeric Brun53d1a982014-04-30 18:21:37 +02002709 { "base64", sample_conv_bin2base64,0, NULL, SMP_T_BIN, SMP_T_STR },
Willy Tarreau12785782012-04-27 21:37:17 +02002710 { "upper", sample_conv_str2upper, 0, NULL, SMP_T_STR, SMP_T_STR },
2711 { "lower", sample_conv_str2lower, 0, NULL, SMP_T_STR, SMP_T_STR },
Thierry FOURNIER2f49d6d2014-03-12 15:01:52 +01002712 { "hex", sample_conv_bin2hex, 0, NULL, SMP_T_BIN, SMP_T_STR },
Willy Tarreau12785782012-04-27 21:37:17 +02002713 { "ipmask", sample_conv_ipmask, ARG1(1,MSK4), NULL, SMP_T_IPV4, SMP_T_IPV4 },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002714 { "ltime", sample_conv_ltime, ARG2(1,STR,SINT), NULL, SMP_T_SINT, SMP_T_STR },
2715 { "utime", sample_conv_utime, ARG2(1,STR,SINT), NULL, SMP_T_SINT, SMP_T_STR },
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002716 { "crc32", sample_conv_crc32, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
2717 { "djb2", sample_conv_djb2, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
2718 { "sdbm", sample_conv_sdbm, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
2719 { "wt6", sample_conv_wt6, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
Thierry FOURNIER01e09742016-12-26 11:46:11 +01002720 { "xxh32", sample_conv_xxh32, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
2721 { "xxh64", sample_conv_xxh64, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002722 { "json", sample_conv_json, ARG1(1,STR), sample_conv_json_check, SMP_T_STR, SMP_T_STR },
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002723 { "bytes", sample_conv_bytes, ARG2(1,SINT,SINT), NULL, SMP_T_BIN, SMP_T_BIN },
2724 { "field", sample_conv_field, ARG2(2,SINT,STR), sample_conv_field_check, SMP_T_STR, SMP_T_STR },
2725 { "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 +01002726 { "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 +01002727
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002728 { "and", sample_conv_binary_and, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
2729 { "or", sample_conv_binary_or, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
2730 { "xor", sample_conv_binary_xor, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
2731 { "cpl", sample_conv_binary_cpl, 0, NULL, SMP_T_SINT, SMP_T_SINT },
2732 { "bool", sample_conv_arith_bool, 0, NULL, SMP_T_SINT, SMP_T_BOOL },
2733 { "not", sample_conv_arith_not, 0, NULL, SMP_T_SINT, SMP_T_BOOL },
2734 { "odd", sample_conv_arith_odd, 0, NULL, SMP_T_SINT, SMP_T_BOOL },
2735 { "even", sample_conv_arith_even, 0, NULL, SMP_T_SINT, SMP_T_BOOL },
2736 { "add", sample_conv_arith_add, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
2737 { "sub", sample_conv_arith_sub, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
2738 { "mul", sample_conv_arith_mul, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
2739 { "div", sample_conv_arith_div, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
2740 { "mod", sample_conv_arith_mod, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
2741 { "neg", sample_conv_arith_neg, 0, NULL, SMP_T_SINT, SMP_T_SINT },
Willy Tarreau97707872015-01-27 15:12:13 +01002742
Willy Tarreau9fcb9842012-04-20 14:45:49 +02002743 { NULL, NULL, 0, 0, 0 },
Emeric Brun107ca302010-01-04 16:16:05 +01002744}};
2745
2746__attribute__((constructor))
Willy Tarreau12785782012-04-27 21:37:17 +02002747static void __sample_init(void)
Emeric Brun107ca302010-01-04 16:16:05 +01002748{
Willy Tarreau5b8ad222013-07-25 12:17:57 +02002749 /* register sample fetch and format conversion keywords */
2750 sample_register_fetches(&smp_kws);
Willy Tarreau12785782012-04-27 21:37:17 +02002751 sample_register_convs(&sample_conv_kws);
Emeric Brun107ca302010-01-04 16:16:05 +01002752}