blob: 1ecc266b7c2da5952fba5c2347a2fcc5bbb126a1 [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>
Emeric Brun107ca302010-01-04 16:16:05 +010034
Thomas Holmes951d44d2015-05-13 11:27:35 +010035#ifdef USE_51DEGREES
36#include <51Degrees.h>
37#endif
38
Willy Tarreau1cf8f082014-02-07 12:14:54 +010039/* sample type names */
40const char *smp_to_type[SMP_TYPES] = {
Thierry FOURNIER9c627e82015-06-03 20:12:35 +020041 [SMP_T_ANY] = "any",
Willy Tarreau1cf8f082014-02-07 12:14:54 +010042 [SMP_T_BOOL] = "bool",
43 [SMP_T_UINT] = "uint",
44 [SMP_T_SINT] = "sint",
45 [SMP_T_ADDR] = "addr",
46 [SMP_T_IPV4] = "ipv4",
47 [SMP_T_IPV6] = "ipv6",
48 [SMP_T_STR] = "str",
49 [SMP_T_BIN] = "bin",
Thierry FOURNIER4c2479e2015-06-03 20:12:04 +020050 [SMP_T_METH] = "meth",
Willy Tarreau1cf8f082014-02-07 12:14:54 +010051};
52
Willy Tarreau12785782012-04-27 21:37:17 +020053/* static sample used in sample_process() when <p> is NULL */
Willy Tarreaub4a88f02012-04-23 21:35:11 +020054static struct sample temp_smp;
Emeric Brun107ca302010-01-04 16:16:05 +010055
Willy Tarreau12785782012-04-27 21:37:17 +020056/* list head of all known sample fetch keywords */
57static struct sample_fetch_kw_list sample_fetches = {
58 .list = LIST_HEAD_INIT(sample_fetches.list)
Emeric Brun107ca302010-01-04 16:16:05 +010059};
60
Willy Tarreau12785782012-04-27 21:37:17 +020061/* list head of all known sample format conversion keywords */
62static struct sample_conv_kw_list sample_convs = {
63 .list = LIST_HEAD_INIT(sample_convs.list)
Emeric Brun107ca302010-01-04 16:16:05 +010064};
65
Willy Tarreau80aca902013-01-07 15:42:20 +010066const unsigned int fetch_cap[SMP_SRC_ENTRIES] = {
67 [SMP_SRC_INTRN] = (SMP_VAL_FE_CON_ACC | SMP_VAL_FE_SES_ACC | SMP_VAL_FE_REQ_CNT |
68 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
69 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
70 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
71 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
72 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
73 SMP_VAL_FE_LOG_END),
74
75 [SMP_SRC_LISTN] = (SMP_VAL_FE_CON_ACC | SMP_VAL_FE_SES_ACC | SMP_VAL_FE_REQ_CNT |
76 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
77 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
78 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
79 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
80 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
81 SMP_VAL_FE_LOG_END),
82
83 [SMP_SRC_FTEND] = (SMP_VAL_FE_CON_ACC | SMP_VAL_FE_SES_ACC | SMP_VAL_FE_REQ_CNT |
84 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
85 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
86 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
87 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
88 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
89 SMP_VAL_FE_LOG_END),
90
91 [SMP_SRC_L4CLI] = (SMP_VAL_FE_CON_ACC | SMP_VAL_FE_SES_ACC | SMP_VAL_FE_REQ_CNT |
92 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
93 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
94 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
95 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
96 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
97 SMP_VAL_FE_LOG_END),
98
99 [SMP_SRC_L5CLI] = (SMP_VAL___________ | SMP_VAL_FE_SES_ACC | SMP_VAL_FE_REQ_CNT |
100 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
101 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
102 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
103 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
104 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
105 SMP_VAL_FE_LOG_END),
106
107 [SMP_SRC_TRACK] = (SMP_VAL_FE_CON_ACC | SMP_VAL_FE_SES_ACC | SMP_VAL_FE_REQ_CNT |
108 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
109 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
110 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
111 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
112 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
113 SMP_VAL_FE_LOG_END),
114
115 [SMP_SRC_L6REQ] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_FE_REQ_CNT |
116 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
117 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
118 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL___________ |
119 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
120 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
121 SMP_VAL___________),
122
123 [SMP_SRC_HRQHV] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_FE_REQ_CNT |
124 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
125 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
126 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL___________ |
127 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
128 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
129 SMP_VAL___________),
130
131 [SMP_SRC_HRQHP] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_FE_REQ_CNT |
132 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
133 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
134 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
135 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
136 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
137 SMP_VAL_FE_LOG_END),
138
139 [SMP_SRC_HRQBO] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
140 SMP_VAL___________ | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
141 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
142 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL___________ |
143 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
144 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
145 SMP_VAL___________),
146
147 [SMP_SRC_BKEND] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
148 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
149 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
150 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
151 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
152 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
153 SMP_VAL_FE_LOG_END),
154
155 [SMP_SRC_SERVR] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
156 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
157 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
158 SMP_VAL___________ | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
159 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
160 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
161 SMP_VAL_FE_LOG_END),
162
163 [SMP_SRC_L4SRV] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
164 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
165 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
166 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_BE_RES_CNT |
167 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
168 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
169 SMP_VAL_FE_LOG_END),
170
171 [SMP_SRC_L5SRV] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
172 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
173 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
174 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_BE_RES_CNT |
175 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
176 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
177 SMP_VAL_FE_LOG_END),
178
179 [SMP_SRC_L6RES] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
180 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
181 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
182 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_BE_RES_CNT |
183 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
184 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
185 SMP_VAL___________),
186
187 [SMP_SRC_HRSHV] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
188 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
189 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
190 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_BE_RES_CNT |
191 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
192 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
193 SMP_VAL___________),
194
195 [SMP_SRC_HRSHP] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
196 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
197 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
198 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_BE_RES_CNT |
199 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
200 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
201 SMP_VAL_FE_LOG_END),
202
203 [SMP_SRC_HRSBO] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
204 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
205 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
206 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
207 SMP_VAL___________ | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
208 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
209 SMP_VAL___________),
210
211 [SMP_SRC_RQFIN] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
212 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
213 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
214 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
215 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
216 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
217 SMP_VAL_FE_LOG_END),
218
219 [SMP_SRC_RSFIN] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
220 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
221 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
222 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
223 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
224 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
225 SMP_VAL_FE_LOG_END),
226
227 [SMP_SRC_TXFIN] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
228 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
229 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
230 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
231 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
232 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
233 SMP_VAL_FE_LOG_END),
234
235 [SMP_SRC_SSFIN] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
236 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
237 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
238 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
239 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
240 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
241 SMP_VAL_FE_LOG_END),
242};
243
244static const char *fetch_src_names[SMP_SRC_ENTRIES] = {
245 [SMP_SRC_INTRN] = "internal state",
246 [SMP_SRC_LISTN] = "listener",
247 [SMP_SRC_FTEND] = "frontend",
248 [SMP_SRC_L4CLI] = "client address",
249 [SMP_SRC_L5CLI] = "client-side connection",
250 [SMP_SRC_TRACK] = "track counters",
251 [SMP_SRC_L6REQ] = "request buffer",
252 [SMP_SRC_HRQHV] = "HTTP request headers",
253 [SMP_SRC_HRQHP] = "HTTP request",
254 [SMP_SRC_HRQBO] = "HTTP request body",
255 [SMP_SRC_BKEND] = "backend",
256 [SMP_SRC_SERVR] = "server",
257 [SMP_SRC_L4SRV] = "server address",
258 [SMP_SRC_L5SRV] = "server-side connection",
259 [SMP_SRC_L6RES] = "response buffer",
260 [SMP_SRC_HRSHV] = "HTTP response headers",
261 [SMP_SRC_HRSHP] = "HTTP response",
262 [SMP_SRC_HRSBO] = "HTTP response body",
263 [SMP_SRC_RQFIN] = "request buffer statistics",
264 [SMP_SRC_RSFIN] = "response buffer statistics",
265 [SMP_SRC_TXFIN] = "transaction statistics",
266 [SMP_SRC_SSFIN] = "session statistics",
267};
268
Willy Tarreaubf8e2512013-03-25 14:52:41 +0100269static const char *fetch_ckp_names[SMP_CKP_ENTRIES] = {
270 [SMP_CKP_FE_CON_ACC] = "frontend tcp-request connection rule",
271 [SMP_CKP_FE_SES_ACC] = "frontend tcp-request session rule",
272 [SMP_CKP_FE_REQ_CNT] = "frontend tcp-request content rule",
273 [SMP_CKP_FE_HRQ_HDR] = "frontend http-request header rule",
274 [SMP_CKP_FE_HRQ_BDY] = "frontend http-request body rule",
275 [SMP_CKP_FE_SET_BCK] = "frontend use-backend rule",
276 [SMP_CKP_BE_REQ_CNT] = "backend tcp-request content rule",
277 [SMP_CKP_BE_HRQ_HDR] = "backend http-request header rule",
278 [SMP_CKP_BE_HRQ_BDY] = "backend http-request body rule",
279 [SMP_CKP_BE_SET_SRV] = "backend use-server, balance or stick-match rule",
280 [SMP_CKP_BE_SRV_CON] = "server source selection",
281 [SMP_CKP_BE_RES_CNT] = "backend tcp-response content rule",
282 [SMP_CKP_BE_HRS_HDR] = "backend http-response header rule",
283 [SMP_CKP_BE_HRS_BDY] = "backend http-response body rule",
284 [SMP_CKP_BE_STO_RUL] = "backend stick-store rule",
285 [SMP_CKP_FE_RES_CNT] = "frontend tcp-response content rule",
286 [SMP_CKP_FE_HRS_HDR] = "frontend http-response header rule",
287 [SMP_CKP_FE_HRS_BDY] = "frontend http-response body rule",
288 [SMP_CKP_FE_LOG_END] = "logs",
289};
290
Thierry FOURNIER348971e2013-11-21 10:50:10 +0100291/* This function returns the type of the data returned by the sample_expr.
292 * It assumes that the <expr> and all of its converters are properly
293 * initialized.
294 */
295inline
296int smp_expr_output_type(struct sample_expr *expr)
297{
298 struct sample_conv_expr *smp_expr;
299
300 if (!LIST_ISEMPTY(&expr->conv_exprs)) {
301 smp_expr = LIST_PREV(&expr->conv_exprs, struct sample_conv_expr *, list);
302 return smp_expr->conv->out_type;
303 }
304 return expr->fetch->out_type;
305}
306
307
Willy Tarreau80aca902013-01-07 15:42:20 +0100308/* fill the trash with a comma-delimited list of source names for the <use> bit
309 * field which must be composed of a non-null set of SMP_USE_* flags. The return
310 * value is the pointer to the string in the trash buffer.
311 */
312const char *sample_src_names(unsigned int use)
313{
314 int bit;
315
316 trash.len = 0;
317 trash.str[0] = '\0';
318 for (bit = 0; bit < SMP_SRC_ENTRIES; bit++) {
319 if (!(use & ~((1 << bit) - 1)))
320 break; /* no more bits */
321
322 if (!(use & (1 << bit)))
323 continue; /* bit not set */
324
325 trash.len += snprintf(trash.str + trash.len, trash.size - trash.len, "%s%s",
326 (use & ((1 << bit) - 1)) ? "," : "",
327 fetch_src_names[bit]);
328 }
329 return trash.str;
330}
331
Willy Tarreaubf8e2512013-03-25 14:52:41 +0100332/* return a pointer to the correct sample checkpoint name, or "unknown" when
333 * the flags are invalid. Only the lowest bit is used, higher bits are ignored
334 * if set.
335 */
336const char *sample_ckp_names(unsigned int use)
337{
338 int bit;
339
340 for (bit = 0; bit < SMP_CKP_ENTRIES; bit++)
341 if (use & (1 << bit))
342 return fetch_ckp_names[bit];
343 return "unknown sample check place, please report this bug";
344}
345
Emeric Brun107ca302010-01-04 16:16:05 +0100346/*
Willy Tarreau80aca902013-01-07 15:42:20 +0100347 * Registers the sample fetch keyword list <kwl> as a list of valid keywords
348 * for next parsing sessions. The fetch keywords capabilities are also computed
349 * from their ->use field.
Emeric Brun107ca302010-01-04 16:16:05 +0100350 */
Willy Tarreau80aca902013-01-07 15:42:20 +0100351void sample_register_fetches(struct sample_fetch_kw_list *kwl)
Emeric Brun107ca302010-01-04 16:16:05 +0100352{
Willy Tarreau80aca902013-01-07 15:42:20 +0100353 struct sample_fetch *sf;
354 int bit;
355
356 for (sf = kwl->kw; sf->kw != NULL; sf++) {
357 for (bit = 0; bit < SMP_SRC_ENTRIES; bit++)
358 if (sf->use & (1 << bit))
359 sf->val |= fetch_cap[bit];
360 }
361 LIST_ADDQ(&sample_fetches.list, &kwl->list);
Emeric Brun107ca302010-01-04 16:16:05 +0100362}
363
364/*
Willy Tarreau12785782012-04-27 21:37:17 +0200365 * Registers the sample format coverstion keyword list <pckl> as a list of valid keywords for next
Emeric Brun107ca302010-01-04 16:16:05 +0100366 * parsing sessions.
367 */
Willy Tarreau12785782012-04-27 21:37:17 +0200368void sample_register_convs(struct sample_conv_kw_list *pckl)
Emeric Brun107ca302010-01-04 16:16:05 +0100369{
Willy Tarreau12785782012-04-27 21:37:17 +0200370 LIST_ADDQ(&sample_convs.list, &pckl->list);
Emeric Brun107ca302010-01-04 16:16:05 +0100371}
372
373/*
Willy Tarreau12785782012-04-27 21:37:17 +0200374 * Returns the pointer on sample fetch keyword structure identified by
Emeric Brun107ca302010-01-04 16:16:05 +0100375 * string of <len> in buffer <kw>.
376 *
377 */
Willy Tarreau12785782012-04-27 21:37:17 +0200378struct sample_fetch *find_sample_fetch(const char *kw, int len)
Emeric Brun107ca302010-01-04 16:16:05 +0100379{
380 int index;
Willy Tarreau12785782012-04-27 21:37:17 +0200381 struct sample_fetch_kw_list *kwl;
Emeric Brun107ca302010-01-04 16:16:05 +0100382
Willy Tarreau12785782012-04-27 21:37:17 +0200383 list_for_each_entry(kwl, &sample_fetches.list, list) {
Emeric Brun107ca302010-01-04 16:16:05 +0100384 for (index = 0; kwl->kw[index].kw != NULL; index++) {
385 if (strncmp(kwl->kw[index].kw, kw, len) == 0 &&
386 kwl->kw[index].kw[len] == '\0')
387 return &kwl->kw[index];
388 }
389 }
390 return NULL;
391}
392
Thierry FOURNIER4d9a1d12014-12-08 14:49:19 +0100393/* This fucntion browse the list of available saple fetch. <current> is
394 * the last used sample fetch. If it is the first call, it must set to NULL.
395 * <idx> is the index of the next sampleèfetch entry. It is used as private
396 * value. It is useles to initiate it.
397 *
398 * It returns always the newt fetch_sample entry, and NULL when the end of
399 * the list is reached.
400 */
401struct sample_fetch *sample_fetch_getnext(struct sample_fetch *current, int *idx)
402{
403 struct sample_fetch_kw_list *kwl;
404 struct sample_fetch *base;
405
406 if (!current) {
407 /* Get first kwl entry. */
408 kwl = LIST_NEXT(&sample_fetches.list, struct sample_fetch_kw_list *, list);
409 (*idx) = 0;
410 } else {
411 /* Get kwl corresponding to the curret entry. */
412 base = current + 1 - (*idx);
413 kwl = container_of(base, struct sample_fetch_kw_list, kw);
414 }
415
416 while (1) {
417
418 /* Check if kwl is the last entry. */
419 if (&kwl->list == &sample_fetches.list)
420 return NULL;
421
422 /* idx contain the next keyword. If it is available, return it. */
423 if (kwl->kw[*idx].kw) {
424 (*idx)++;
425 return &kwl->kw[(*idx)-1];
426 }
427
428 /* get next entry in the main list, and return NULL if the end is reached. */
429 kwl = LIST_NEXT(&kwl->list, struct sample_fetch_kw_list *, list);
430
431 /* Set index to 0, ans do one other loop. */
432 (*idx) = 0;
433 }
434}
435
Thierry FOURNIER8fd13762015-03-10 23:56:48 +0100436/* This function browses the list of available converters. <current> is
437 * the last used converter. If it is the first call, it must set to NULL.
438 * <idx> is the index of the next converter entry. It is used as private
439 * value. It is useless to initiate it.
440 *
441 * It returns always the next sample_conv entry, and NULL when the end of
442 * the list is reached.
443 */
444struct sample_conv *sample_conv_getnext(struct sample_conv *current, int *idx)
445{
446 struct sample_conv_kw_list *kwl;
447 struct sample_conv *base;
448
449 if (!current) {
450 /* Get first kwl entry. */
451 kwl = LIST_NEXT(&sample_convs.list, struct sample_conv_kw_list *, list);
452 (*idx) = 0;
453 } else {
454 /* Get kwl corresponding to the curret entry. */
455 base = current + 1 - (*idx);
456 kwl = container_of(base, struct sample_conv_kw_list, kw);
457 }
458
459 while (1) {
460 /* Check if kwl is the last entry. */
461 if (&kwl->list == &sample_convs.list)
462 return NULL;
463
464 /* idx contain the next keyword. If it is available, return it. */
465 if (kwl->kw[*idx].kw) {
466 (*idx)++;
467 return &kwl->kw[(*idx)-1];
468 }
469
470 /* get next entry in the main list, and return NULL if the end is reached. */
471 kwl = LIST_NEXT(&kwl->list, struct sample_conv_kw_list *, list);
472
473 /* Set index to 0, ans do one other loop. */
474 (*idx) = 0;
475 }
476}
477
Emeric Brun107ca302010-01-04 16:16:05 +0100478/*
Willy Tarreau12785782012-04-27 21:37:17 +0200479 * Returns the pointer on sample format conversion keyword structure identified by
Emeric Brun107ca302010-01-04 16:16:05 +0100480 * string of <len> in buffer <kw>.
481 *
482 */
Willy Tarreau12785782012-04-27 21:37:17 +0200483struct sample_conv *find_sample_conv(const char *kw, int len)
Emeric Brun107ca302010-01-04 16:16:05 +0100484{
485 int index;
Willy Tarreau12785782012-04-27 21:37:17 +0200486 struct sample_conv_kw_list *kwl;
Emeric Brun107ca302010-01-04 16:16:05 +0100487
Willy Tarreau12785782012-04-27 21:37:17 +0200488 list_for_each_entry(kwl, &sample_convs.list, list) {
Emeric Brun107ca302010-01-04 16:16:05 +0100489 for (index = 0; kwl->kw[index].kw != NULL; index++) {
490 if (strncmp(kwl->kw[index].kw, kw, len) == 0 &&
491 kwl->kw[index].kw[len] == '\0')
492 return &kwl->kw[index];
493 }
494 }
495 return NULL;
496}
497
Emeric Brun107ca302010-01-04 16:16:05 +0100498/******************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +0200499/* Sample casts functions */
Willy Tarreaub8c8f1f2012-04-23 22:38:26 +0200500/* Note: these functions do *NOT* set the output type on the */
501/* sample, the caller is responsible for doing this on return. */
Emeric Brun107ca302010-01-04 16:16:05 +0100502/******************************************************************/
503
Willy Tarreau342acb42012-04-23 22:03:39 +0200504static int c_ip2int(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100505{
Willy Tarreau342acb42012-04-23 22:03:39 +0200506 smp->data.uint = ntohl(smp->data.ipv4.s_addr);
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100507 smp->type = SMP_T_UINT;
Emeric Brun107ca302010-01-04 16:16:05 +0100508 return 1;
509}
510
Willy Tarreau342acb42012-04-23 22:03:39 +0200511static int c_ip2str(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100512{
Willy Tarreau47ca5452012-12-23 20:22:19 +0100513 struct chunk *trash = get_trash_chunk();
Emeric Brun107ca302010-01-04 16:16:05 +0100514
Willy Tarreau342acb42012-04-23 22:03:39 +0200515 if (!inet_ntop(AF_INET, (void *)&smp->data.ipv4, trash->str, trash->size))
Emeric Brun107ca302010-01-04 16:16:05 +0100516 return 0;
517
518 trash->len = strlen(trash->str);
Willy Tarreau342acb42012-04-23 22:03:39 +0200519 smp->data.str = *trash;
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100520 smp->type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100521 smp->flags &= ~SMP_F_CONST;
Emeric Brun107ca302010-01-04 16:16:05 +0100522
523 return 1;
524}
525
Willy Tarreau342acb42012-04-23 22:03:39 +0200526static int c_ip2ipv6(struct sample *smp)
David du Colombier4f92d322011-03-24 11:09:31 +0100527{
Willy Tarreau342acb42012-04-23 22:03:39 +0200528 v4tov6(&smp->data.ipv6, &smp->data.ipv4);
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100529 smp->type = SMP_T_IPV6;
David du Colombier4f92d322011-03-24 11:09:31 +0100530 return 1;
531}
532
Willy Tarreau342acb42012-04-23 22:03:39 +0200533static int c_ipv62str(struct sample *smp)
David du Colombier4f92d322011-03-24 11:09:31 +0100534{
Willy Tarreau47ca5452012-12-23 20:22:19 +0100535 struct chunk *trash = get_trash_chunk();
David du Colombier4f92d322011-03-24 11:09:31 +0100536
Willy Tarreau342acb42012-04-23 22:03:39 +0200537 if (!inet_ntop(AF_INET6, (void *)&smp->data.ipv6, trash->str, trash->size))
David du Colombier4f92d322011-03-24 11:09:31 +0100538 return 0;
539
540 trash->len = strlen(trash->str);
Willy Tarreau342acb42012-04-23 22:03:39 +0200541 smp->data.str = *trash;
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100542 smp->type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100543 smp->flags &= ~SMP_F_CONST;
David du Colombier4f92d322011-03-24 11:09:31 +0100544 return 1;
545}
546
547/*
Willy Tarreau342acb42012-04-23 22:03:39 +0200548static int c_ipv62ip(struct sample *smp)
David du Colombier4f92d322011-03-24 11:09:31 +0100549{
Willy Tarreau342acb42012-04-23 22:03:39 +0200550 return v6tov4(&smp->data.ipv4, &smp->data.ipv6);
David du Colombier4f92d322011-03-24 11:09:31 +0100551}
552*/
553
Willy Tarreau342acb42012-04-23 22:03:39 +0200554static int c_int2ip(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100555{
Willy Tarreau342acb42012-04-23 22:03:39 +0200556 smp->data.ipv4.s_addr = htonl(smp->data.uint);
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100557 smp->type = SMP_T_IPV4;
Emeric Brun107ca302010-01-04 16:16:05 +0100558 return 1;
559}
560
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100561static int c_str2addr(struct sample *smp)
562{
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100563 if (!buf2ip(smp->data.str.str, smp->data.str.len, &smp->data.ipv4)) {
Thierry FOURNIERfd139902013-12-11 12:38:57 +0100564 if (!buf2ip6(smp->data.str.str, smp->data.str.len, &smp->data.ipv6))
565 return 0;
566 smp->type = SMP_T_IPV6;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100567 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIERfd139902013-12-11 12:38:57 +0100568 return 1;
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100569 }
570 smp->type = SMP_T_IPV4;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100571 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100572 return 1;
573}
574
Willy Tarreau342acb42012-04-23 22:03:39 +0200575static int c_str2ip(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100576{
Willy Tarreau342acb42012-04-23 22:03:39 +0200577 if (!buf2ip(smp->data.str.str, smp->data.str.len, &smp->data.ipv4))
Emeric Brun107ca302010-01-04 16:16:05 +0100578 return 0;
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100579 smp->type = SMP_T_IPV4;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100580 smp->flags &= ~SMP_F_CONST;
Emeric Brun107ca302010-01-04 16:16:05 +0100581 return 1;
582}
583
Willy Tarreau342acb42012-04-23 22:03:39 +0200584static int c_str2ipv6(struct sample *smp)
David du Colombier4f92d322011-03-24 11:09:31 +0100585{
Thierry FOURNIERfd139902013-12-11 12:38:57 +0100586 if (!buf2ip6(smp->data.str.str, smp->data.str.len, &smp->data.ipv6))
587 return 0;
588 smp->type = SMP_T_IPV6;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100589 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIERfd139902013-12-11 12:38:57 +0100590 return 1;
David du Colombier4f92d322011-03-24 11:09:31 +0100591}
592
Emeric Brun4b9e8022014-11-03 18:17:10 +0100593/*
594 * The NULL char always enforces the end of string if it is met.
595 * Data is never changed, so we can ignore the CONST case
Thierry FOURNIERe87cac12014-03-12 15:07:59 +0100596 */
Emeric Brun8ac33d92012-10-17 13:36:06 +0200597static int c_bin2str(struct sample *smp)
598{
Emeric Brun4b9e8022014-11-03 18:17:10 +0100599 int i;
Emeric Brun8ac33d92012-10-17 13:36:06 +0200600
Emeric Brun4b9e8022014-11-03 18:17:10 +0100601 for (i = 0; i < smp->data.str.len; i++) {
602 if (!smp->data.str.str[i]) {
603 smp->data.str.len = i;
Thierry FOURNIERe87cac12014-03-12 15:07:59 +0100604 break;
Emeric Brun4b9e8022014-11-03 18:17:10 +0100605 }
Emeric Brun8ac33d92012-10-17 13:36:06 +0200606 }
Emeric Brun8ac33d92012-10-17 13:36:06 +0200607 return 1;
608}
609
Willy Tarreau342acb42012-04-23 22:03:39 +0200610static int c_int2str(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100611{
Willy Tarreau47ca5452012-12-23 20:22:19 +0100612 struct chunk *trash = get_trash_chunk();
Emeric Brun107ca302010-01-04 16:16:05 +0100613 char *pos;
614
Willy Tarreau342acb42012-04-23 22:03:39 +0200615 pos = ultoa_r(smp->data.uint, trash->str, trash->size);
Thierry FOURNIER0f811442015-06-06 19:16:52 +0200616 if (!pos)
617 return 0;
618
619 trash->size = trash->size - (pos - trash->str);
620 trash->str = pos;
621 trash->len = strlen(pos);
622 smp->data.str = *trash;
623 smp->type = SMP_T_STR;
624 smp->flags &= ~SMP_F_CONST;
625 return 1;
626}
627
628static int c_sint2str(struct sample *smp)
629{
630 struct chunk *trash = get_trash_chunk();
631 char *pos;
Emeric Brun107ca302010-01-04 16:16:05 +0100632
Thierry FOURNIER0f811442015-06-06 19:16:52 +0200633 pos = sltoa_r(smp->data.sint, trash->str, trash->size);
Emeric Brun107ca302010-01-04 16:16:05 +0100634 if (!pos)
635 return 0;
636
Emeric Brun485479d2010-09-23 18:02:19 +0200637 trash->size = trash->size - (pos - trash->str);
Emeric Brun107ca302010-01-04 16:16:05 +0100638 trash->str = pos;
639 trash->len = strlen(pos);
Willy Tarreau342acb42012-04-23 22:03:39 +0200640 smp->data.str = *trash;
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100641 smp->type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100642 smp->flags &= ~SMP_F_CONST;
Emeric Brun107ca302010-01-04 16:16:05 +0100643 return 1;
644}
645
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100646/* This function duplicates data and removes the flag "const". */
647int smp_dup(struct sample *smp)
Emeric Brun485479d2010-09-23 18:02:19 +0200648{
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100649 struct chunk *trash;
Emeric Brun485479d2010-09-23 18:02:19 +0200650
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100651 /* If the const flag is not set, we don't need to duplicate the
652 * pattern as it can be modified in place.
653 */
654 if (!(smp->flags & SMP_F_CONST))
655 return 1;
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100656
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100657 switch (smp->type) {
658 case SMP_T_BOOL:
659 case SMP_T_UINT:
660 case SMP_T_SINT:
661 case SMP_T_ADDR:
662 case SMP_T_IPV4:
663 case SMP_T_IPV6:
664 /* These type are not const. */
665 break;
666 case SMP_T_STR:
667 case SMP_T_BIN:
668 /* Duplicate data. */
669 trash = get_trash_chunk();
670 trash->len = smp->data.str.len < trash->size ? smp->data.str.len : trash->size;
671 memcpy(trash->str, smp->data.str.str, trash->len);
672 smp->data.str = *trash;
673 break;
674 default:
675 /* Other cases are unexpected. */
676 return 0;
677 }
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100678
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100679 /* remove const flag */
680 smp->flags &= ~SMP_F_CONST;
Emeric Brun485479d2010-09-23 18:02:19 +0200681 return 1;
682}
683
Thierry FOURNIER0e9af552013-12-14 14:55:04 +0100684int c_none(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100685{
686 return 1;
687}
688
Willy Tarreau342acb42012-04-23 22:03:39 +0200689static int c_str2int(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100690{
691 int i;
692 uint32_t ret = 0;
693
Thierry FOURNIER60bb0202014-01-27 18:20:48 +0100694 if (smp->data.str.len == 0)
695 return 0;
696
Willy Tarreau342acb42012-04-23 22:03:39 +0200697 for (i = 0; i < smp->data.str.len; i++) {
698 uint32_t val = smp->data.str.str[i] - '0';
Emeric Brun107ca302010-01-04 16:16:05 +0100699
Thierry FOURNIER60bb0202014-01-27 18:20:48 +0100700 if (val > 9) {
701 if (i == 0)
702 return 0;
Emeric Brun107ca302010-01-04 16:16:05 +0100703 break;
Thierry FOURNIER60bb0202014-01-27 18:20:48 +0100704 }
Emeric Brun107ca302010-01-04 16:16:05 +0100705
706 ret = ret * 10 + val;
707 }
708
Willy Tarreau342acb42012-04-23 22:03:39 +0200709 smp->data.uint = ret;
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100710 smp->type = SMP_T_UINT;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100711 smp->flags &= ~SMP_F_CONST;
Emeric Brun107ca302010-01-04 16:16:05 +0100712 return 1;
713}
714
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100715static int c_str2meth(struct sample *smp)
716{
717 enum http_meth_t meth;
718 int len;
719
720 meth = find_http_meth(smp->data.str.str, smp->data.str.len);
721 if (meth == HTTP_METH_OTHER) {
722 len = smp->data.str.len;
723 smp->data.meth.str.str = smp->data.str.str;
724 smp->data.meth.str.len = len;
725 }
726 else
727 smp->flags &= ~SMP_F_CONST;
728 smp->data.meth.meth = meth;
729 smp->type = SMP_T_METH;
730 return 1;
731}
732
733static int c_meth2str(struct sample *smp)
734{
735 int len;
736 enum http_meth_t meth;
737
738 if (smp->data.meth.meth == HTTP_METH_OTHER) {
739 /* The method is unknown. Copy the original pointer. */
740 len = smp->data.meth.str.len;
741 smp->data.str.str = smp->data.meth.str.str;
742 smp->data.str.len = len;
743 smp->type = SMP_T_STR;
744 }
745 else if (smp->data.meth.meth < HTTP_METH_OTHER) {
746 /* The method is known, copy the pointer containing the string. */
747 meth = smp->data.meth.meth;
748 smp->data.str.str = http_known_methods[meth].name;
749 smp->data.str.len = http_known_methods[meth].len;
750 smp->flags |= SMP_F_CONST;
751 smp->type = SMP_T_STR;
752 }
753 else {
754 /* Unknown method */
755 return 0;
756 }
757 return 1;
758}
759
Willy Tarreau9700e5c2014-07-15 21:03:26 +0200760static int c_addr2bin(struct sample *smp)
761{
762 struct chunk *chk = get_trash_chunk();
763
764 if (smp->type == SMP_T_IPV4) {
765 chk->len = 4;
766 memcpy(chk->str, &smp->data.ipv4, chk->len);
767 }
768 else if (smp->type == SMP_T_IPV6) {
769 chk->len = 16;
770 memcpy(chk->str, &smp->data.ipv6, chk->len);
771 }
772 else
773 return 0;
774
775 smp->data.str = *chk;
776 smp->type = SMP_T_BIN;
777 return 1;
778}
779
Willy Tarreaubbfd1a22014-07-15 21:19:08 +0200780static int c_int2bin(struct sample *smp)
781{
782 struct chunk *chk = get_trash_chunk();
783
784 *(unsigned int *)chk->str = htonl(smp->data.uint);
785 chk->len = 4;
786
787 smp->data.str = *chk;
788 smp->type = SMP_T_BIN;
789 return 1;
790}
791
Willy Tarreau9700e5c2014-07-15 21:03:26 +0200792
Emeric Brun107ca302010-01-04 16:16:05 +0100793/*****************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +0200794/* Sample casts matrix: */
795/* sample_casts[from type][to type] */
796/* NULL pointer used for impossible sample casts */
Emeric Brun107ca302010-01-04 16:16:05 +0100797/*****************************************************************/
Emeric Brun107ca302010-01-04 16:16:05 +0100798
Thierry FOURNIER8af6ff12013-11-21 10:53:12 +0100799sample_cast_fct sample_casts[SMP_TYPES][SMP_TYPES] = {
Thierry FOURNIER9c627e82015-06-03 20:12:35 +0200800/* to: ANY BOOL UINT SINT ADDR IPV4 IPV6 STR BIN METH */
801/* from: ANY */ { c_none, c_none, c_none, c_none, c_none, c_none, c_none, c_none, c_none, c_none, },
802/* BOOL */ { c_none, c_none, c_none, c_none, NULL, NULL, NULL, c_int2str, NULL, NULL, },
803/* UINT */ { c_none, c_none, c_none, c_none, c_int2ip, c_int2ip, NULL, c_int2str, c_int2bin, NULL, },
804/* SINT */ { c_none, c_none, c_none, c_none, c_int2ip, c_int2ip, NULL, c_sint2str, c_int2bin, NULL, },
805/* ADDR */ { c_none, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, },
806/* IPV4 */ { c_none, NULL, c_ip2int, c_ip2int, c_none, c_none, c_ip2ipv6, c_ip2str, c_addr2bin, NULL, },
807/* IPV6 */ { c_none, NULL, NULL, NULL, c_none, NULL, c_none, c_ipv62str, c_addr2bin, NULL, },
808/* STR */ { c_none, c_str2int, c_str2int, c_str2int, c_str2addr, c_str2ip, c_str2ipv6, c_none, c_none, c_str2meth, },
809/* BIN */ { c_none, NULL, NULL, NULL, NULL, NULL, NULL, c_bin2str, c_none, c_str2meth, },
810/* METH */ { c_none, NULL, NULL, NULL, NULL, NULL, NULL, c_meth2str, c_meth2str, c_none, }
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200811};
Emeric Brun107ca302010-01-04 16:16:05 +0100812
Emeric Brun107ca302010-01-04 16:16:05 +0100813/*
Willy Tarreau12785782012-04-27 21:37:17 +0200814 * Parse a sample expression configuration:
Emeric Brun107ca302010-01-04 16:16:05 +0100815 * fetch keyword followed by format conversion keywords.
Willy Tarreau12785782012-04-27 21:37:17 +0200816 * Returns a pointer on allocated sample expression structure.
Willy Tarreaua4312fa2013-04-02 16:34:32 +0200817 * The caller must have set al->ctx.
Emeric Brun107ca302010-01-04 16:16:05 +0100818 */
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +0100819struct 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 +0100820{
Willy Tarreau833cc792013-07-24 15:34:19 +0200821 const char *begw; /* beginning of word */
822 const char *endw; /* end of word */
823 const char *endt; /* end of term */
Willy Tarreau12785782012-04-27 21:37:17 +0200824 struct sample_expr *expr;
825 struct sample_fetch *fetch;
826 struct sample_conv *conv;
Emeric Brun107ca302010-01-04 16:16:05 +0100827 unsigned long prev_type;
Willy Tarreau833cc792013-07-24 15:34:19 +0200828 char *fkw = NULL;
829 char *ckw = NULL;
Willy Tarreau689a1df2013-12-13 00:40:11 +0100830 int err_arg;
Emeric Brun107ca302010-01-04 16:16:05 +0100831
Willy Tarreau833cc792013-07-24 15:34:19 +0200832 begw = str[*idx];
833 for (endw = begw; *endw && *endw != '(' && *endw != ','; endw++);
834
835 if (endw == begw) {
Willy Tarreau975c1782013-12-12 23:16:54 +0100836 memprintf(err_msg, "missing fetch method");
Emeric Brun107ca302010-01-04 16:16:05 +0100837 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200838 }
Emeric Brun107ca302010-01-04 16:16:05 +0100839
Willy Tarreau833cc792013-07-24 15:34:19 +0200840 /* keep a copy of the current fetch keyword for error reporting */
841 fkw = my_strndup(begw, endw - begw);
Emeric Brun107ca302010-01-04 16:16:05 +0100842
Willy Tarreau833cc792013-07-24 15:34:19 +0200843 fetch = find_sample_fetch(begw, endw - begw);
844 if (!fetch) {
Willy Tarreau975c1782013-12-12 23:16:54 +0100845 memprintf(err_msg, "unknown fetch method '%s'", fkw);
Emeric Brun107ca302010-01-04 16:16:05 +0100846 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200847 }
Emeric Brun107ca302010-01-04 16:16:05 +0100848
Willy Tarreau833cc792013-07-24 15:34:19 +0200849 endt = endw;
850 if (*endt == '(') {
Willy Tarreau689a1df2013-12-13 00:40:11 +0100851 /* look for the end of this term and skip the opening parenthesis */
852 endt = ++endw;
Willy Tarreau833cc792013-07-24 15:34:19 +0200853 while (*endt && *endt != ')')
854 endt++;
855 if (*endt != ')') {
Willy Tarreau975c1782013-12-12 23:16:54 +0100856 memprintf(err_msg, "missing closing ')' after arguments to fetch keyword '%s'", fkw);
Willy Tarreau833cc792013-07-24 15:34:19 +0200857 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200858 }
Emeric Brun485479d2010-09-23 18:02:19 +0200859 }
Emeric Brun107ca302010-01-04 16:16:05 +0100860
Willy Tarreau833cc792013-07-24 15:34:19 +0200861 /* At this point, we have :
862 * - begw : beginning of the keyword
Willy Tarreau689a1df2013-12-13 00:40:11 +0100863 * - endw : end of the keyword, first character not part of keyword
864 * nor the opening parenthesis (so first character of args
865 * if present).
Willy Tarreau833cc792013-07-24 15:34:19 +0200866 * - endt : end of the term (=endw or last parenthesis if args are present)
867 */
868
869 if (fetch->out_type >= SMP_TYPES) {
Willy Tarreau975c1782013-12-12 23:16:54 +0100870 memprintf(err_msg, "returns type of fetch method '%s' is unknown", fkw);
Emeric Brun107ca302010-01-04 16:16:05 +0100871 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200872 }
Emeric Brun107ca302010-01-04 16:16:05 +0100873 prev_type = fetch->out_type;
Willy Tarreau833cc792013-07-24 15:34:19 +0200874
Willy Tarreau12785782012-04-27 21:37:17 +0200875 expr = calloc(1, sizeof(struct sample_expr));
Emeric Brun485479d2010-09-23 18:02:19 +0200876 if (!expr)
877 goto out_error;
Emeric Brun107ca302010-01-04 16:16:05 +0100878
879 LIST_INIT(&(expr->conv_exprs));
880 expr->fetch = fetch;
Willy Tarreau2e845be2012-10-19 19:49:09 +0200881 expr->arg_p = empty_arg_list;
Emeric Brun107ca302010-01-04 16:16:05 +0100882
Willy Tarreau689a1df2013-12-13 00:40:11 +0100883 /* Note that we call the argument parser even with an empty string,
884 * this allows it to automatically create entries for mandatory
885 * implicit arguments (eg: local proxy name).
886 */
887 al->kw = expr->fetch->kw;
888 al->conv = NULL;
889 if (make_arg_list(endw, endt - endw, fetch->arg_mask, &expr->arg_p, err_msg, NULL, &err_arg, al) < 0) {
890 memprintf(err_msg, "fetch method '%s' : %s", fkw, *err_msg);
891 goto out_error;
892 }
Willy Tarreau2e845be2012-10-19 19:49:09 +0200893
Willy Tarreau689a1df2013-12-13 00:40:11 +0100894 if (!expr->arg_p) {
895 expr->arg_p = empty_arg_list;
Emeric Brun485479d2010-09-23 18:02:19 +0200896 }
Willy Tarreau689a1df2013-12-13 00:40:11 +0100897 else if (fetch->val_args && !fetch->val_args(expr->arg_p, err_msg)) {
898 memprintf(err_msg, "invalid args in fetch method '%s' : %s", fkw, *err_msg);
Emeric Brun485479d2010-09-23 18:02:19 +0200899 goto out_error;
Emeric Brun107ca302010-01-04 16:16:05 +0100900 }
901
Willy Tarreau833cc792013-07-24 15:34:19 +0200902 /* Now process the converters if any. We have two supported syntaxes
903 * for the converters, which can be combined :
904 * - comma-delimited list of converters just after the keyword and args ;
905 * - one converter per keyword
906 * The combination allows to have each keyword being a comma-delimited
907 * series of converters.
908 *
909 * We want to process the former first, then the latter. For this we start
910 * from the beginning of the supposed place in the exiting conv chain, which
911 * starts at the last comma (endt).
912 */
913
914 while (1) {
Willy Tarreau12785782012-04-27 21:37:17 +0200915 struct sample_conv_expr *conv_expr;
Emeric Brun107ca302010-01-04 16:16:05 +0100916
Willy Tarreau833cc792013-07-24 15:34:19 +0200917 if (*endt == ')') /* skip last closing parenthesis */
918 endt++;
Emeric Brun107ca302010-01-04 16:16:05 +0100919
Willy Tarreau833cc792013-07-24 15:34:19 +0200920 if (*endt && *endt != ',') {
921 if (ckw)
Willy Tarreau975c1782013-12-12 23:16:54 +0100922 memprintf(err_msg, "missing comma after conv keyword '%s'", ckw);
Willy Tarreau833cc792013-07-24 15:34:19 +0200923 else
Willy Tarreau975c1782013-12-12 23:16:54 +0100924 memprintf(err_msg, "missing comma after fetch keyword '%s'", fkw);
Emeric Brun107ca302010-01-04 16:16:05 +0100925 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200926 }
Emeric Brun107ca302010-01-04 16:16:05 +0100927
Willy Tarreau833cc792013-07-24 15:34:19 +0200928 while (*endt == ',') /* then trailing commas */
929 endt++;
930
931 begw = endt; /* start of conv keyword */
932
933 if (!*begw) {
934 /* none ? skip to next string */
935 (*idx)++;
936 begw = str[*idx];
937 if (!begw || !*begw)
938 break;
939 }
940
941 for (endw = begw; *endw && *endw != '(' && *endw != ','; endw++);
942
943 free(ckw);
944 ckw = my_strndup(begw, endw - begw);
945
946 conv = find_sample_conv(begw, endw - begw);
947 if (!conv) {
948 /* we found an isolated keyword that we don't know, it's not ours */
949 if (begw == str[*idx])
950 break;
Willy Tarreau975c1782013-12-12 23:16:54 +0100951 memprintf(err_msg, "unknown conv method '%s'", ckw);
Willy Tarreau833cc792013-07-24 15:34:19 +0200952 goto out_error;
953 }
Emeric Brun107ca302010-01-04 16:16:05 +0100954
Willy Tarreau833cc792013-07-24 15:34:19 +0200955 endt = endw;
956 if (*endt == '(') {
957 /* look for the end of this term */
958 while (*endt && *endt != ')')
959 endt++;
960 if (*endt != ')') {
Willy Tarreau975c1782013-12-12 23:16:54 +0100961 memprintf(err_msg, "syntax error: missing ')' after conv keyword '%s'", ckw);
Willy Tarreau833cc792013-07-24 15:34:19 +0200962 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200963 }
Willy Tarreau833cc792013-07-24 15:34:19 +0200964 }
965
966 if (conv->in_type >= SMP_TYPES || conv->out_type >= SMP_TYPES) {
Willy Tarreau975c1782013-12-12 23:16:54 +0100967 memprintf(err_msg, "returns type of conv method '%s' is unknown", ckw);
Emeric Brun107ca302010-01-04 16:16:05 +0100968 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200969 }
Emeric Brun107ca302010-01-04 16:16:05 +0100970
971 /* If impossible type conversion */
Willy Tarreau12785782012-04-27 21:37:17 +0200972 if (!sample_casts[prev_type][conv->in_type]) {
Willy Tarreau975c1782013-12-12 23:16:54 +0100973 memprintf(err_msg, "conv method '%s' cannot be applied", ckw);
Emeric Brun107ca302010-01-04 16:16:05 +0100974 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200975 }
Emeric Brun107ca302010-01-04 16:16:05 +0100976
977 prev_type = conv->out_type;
Willy Tarreau12785782012-04-27 21:37:17 +0200978 conv_expr = calloc(1, sizeof(struct sample_conv_expr));
Emeric Brun485479d2010-09-23 18:02:19 +0200979 if (!conv_expr)
980 goto out_error;
Emeric Brun107ca302010-01-04 16:16:05 +0100981
982 LIST_ADDQ(&(expr->conv_exprs), &(conv_expr->list));
983 conv_expr->conv = conv;
984
Willy Tarreau833cc792013-07-24 15:34:19 +0200985 if (endt != endw) {
Willy Tarreaub27c0d32012-04-20 16:04:47 +0200986 int err_arg;
Willy Tarreau21d68a62012-04-20 15:52:36 +0200987
Willy Tarreau9fcb9842012-04-20 14:45:49 +0200988 if (!conv->arg_mask) {
Willy Tarreau975c1782013-12-12 23:16:54 +0100989 memprintf(err_msg, "conv method '%s' does not support any args", ckw);
Emeric Brun485479d2010-09-23 18:02:19 +0200990 goto out_error;
991 }
Willy Tarreau9e92d322010-01-26 17:58:06 +0100992
Willy Tarreaua4312fa2013-04-02 16:34:32 +0200993 al->kw = expr->fetch->kw;
994 al->conv = conv_expr->conv->kw;
Willy Tarreau975c1782013-12-12 23:16:54 +0100995 if (make_arg_list(endw + 1, endt - endw - 1, conv->arg_mask, &conv_expr->arg_p, err_msg, NULL, &err_arg, al) < 0) {
996 memprintf(err_msg, "invalid arg %d in conv method '%s' : %s", err_arg+1, ckw, *err_msg);
Willy Tarreau21d68a62012-04-20 15:52:36 +0200997 goto out_error;
998 }
999
Willy Tarreau2e845be2012-10-19 19:49:09 +02001000 if (!conv_expr->arg_p)
1001 conv_expr->arg_p = empty_arg_list;
1002
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +01001003 if (conv->val_args && !conv->val_args(conv_expr->arg_p, conv, file, line, err_msg)) {
Willy Tarreau975c1782013-12-12 23:16:54 +01001004 memprintf(err_msg, "invalid args in conv method '%s' : %s", ckw, *err_msg);
Emeric Brun485479d2010-09-23 18:02:19 +02001005 goto out_error;
1006 }
1007 }
Willy Tarreau833cc792013-07-24 15:34:19 +02001008 else if (ARGM(conv->arg_mask)) {
Willy Tarreau975c1782013-12-12 23:16:54 +01001009 memprintf(err_msg, "missing args for conv method '%s'", ckw);
Emeric Brun485479d2010-09-23 18:02:19 +02001010 goto out_error;
Emeric Brun107ca302010-01-04 16:16:05 +01001011 }
1012 }
Emeric Brun485479d2010-09-23 18:02:19 +02001013
Willy Tarreau833cc792013-07-24 15:34:19 +02001014 out:
1015 free(fkw);
1016 free(ckw);
Emeric Brun107ca302010-01-04 16:16:05 +01001017 return expr;
1018
1019out_error:
Willy Tarreau12785782012-04-27 21:37:17 +02001020 /* TODO: prune_sample_expr(expr); */
Willy Tarreau833cc792013-07-24 15:34:19 +02001021 expr = NULL;
1022 goto out;
Emeric Brun107ca302010-01-04 16:16:05 +01001023}
1024
1025/*
Willy Tarreau12785782012-04-27 21:37:17 +02001026 * Process a fetch + format conversion of defined by the sample expression <expr>
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001027 * on request or response considering the <opt> parameter.
Willy Tarreau12785782012-04-27 21:37:17 +02001028 * Returns a pointer on a typed sample structure containing the result or NULL if
1029 * sample is not found or when format conversion failed.
Emeric Brun107ca302010-01-04 16:16:05 +01001030 * If <p> is not null, function returns results in structure pointed by <p>.
Willy Tarreau12785782012-04-27 21:37:17 +02001031 * If <p> is null, functions returns a pointer on a static sample structure.
Willy Tarreaub8c8f1f2012-04-23 22:38:26 +02001032 *
1033 * Note: the fetch functions are required to properly set the return type. The
1034 * conversion functions must do so too. However the cast functions do not need
1035 * to since they're made to cast mutiple types according to what is required.
Willy Tarreau6bcb0a82014-07-30 08:56:35 +02001036 *
1037 * The caller may indicate in <opt> if it considers the result final or not.
1038 * The caller needs to check the SMP_F_MAY_CHANGE flag in p->flags to verify
1039 * if the result is stable or not, according to the following table :
1040 *
1041 * return MAY_CHANGE FINAL Meaning for the sample
1042 * NULL 0 * Not present and will never be (eg: header)
1043 * NULL 1 0 Not present yet, could change (eg: POST param)
1044 * NULL 1 1 Not present yet, will not change anymore
1045 * smp 0 * Present and will not change (eg: header)
1046 * smp 1 0 Present, may change (eg: request length)
1047 * smp 1 1 Present, last known value (eg: request length)
Emeric Brun107ca302010-01-04 16:16:05 +01001048 */
Willy Tarreau192252e2015-04-04 01:47:55 +02001049struct sample *sample_process(struct proxy *px, struct session *sess,
1050 struct stream *strm, unsigned int opt,
Willy Tarreau12785782012-04-27 21:37:17 +02001051 struct sample_expr *expr, struct sample *p)
Emeric Brun107ca302010-01-04 16:16:05 +01001052{
Willy Tarreau12785782012-04-27 21:37:17 +02001053 struct sample_conv_expr *conv_expr;
Emeric Brun107ca302010-01-04 16:16:05 +01001054
Willy Tarreau18387e22013-07-25 12:02:38 +02001055 if (p == NULL) {
Willy Tarreaub4a88f02012-04-23 21:35:11 +02001056 p = &temp_smp;
Willy Tarreau6c616e02014-06-25 16:56:41 +02001057 memset(p, 0, sizeof(*p));
Willy Tarreau18387e22013-07-25 12:02:38 +02001058 }
Emeric Brun107ca302010-01-04 16:16:05 +01001059
Thierry FOURNIER6879ad32015-05-11 11:54:58 +02001060 p->px = px;
1061 p->sess = sess;
1062 p->strm = strm;
Thierry FOURNIER1d33b882015-05-11 15:25:29 +02001063 p->opt = opt;
Thierry FOURNIER0786d052015-05-11 15:42:45 +02001064 if (!expr->fetch->process(expr->arg_p, p, expr->fetch->kw, expr->fetch->private))
Emeric Brun107ca302010-01-04 16:16:05 +01001065 return NULL;
1066
Emeric Brun107ca302010-01-04 16:16:05 +01001067 list_for_each_entry(conv_expr, &expr->conv_exprs, list) {
Willy Tarreau12e50112012-04-25 17:21:49 +02001068 /* we want to ensure that p->type can be casted into
1069 * conv_expr->conv->in_type. We have 3 possibilities :
1070 * - NULL => not castable.
1071 * - c_none => nothing to do (let's optimize it)
1072 * - other => apply cast and prepare to fail
1073 */
Willy Tarreau12785782012-04-27 21:37:17 +02001074 if (!sample_casts[p->type][conv_expr->conv->in_type])
Willy Tarreau12e50112012-04-25 17:21:49 +02001075 return NULL;
1076
Willy Tarreau12785782012-04-27 21:37:17 +02001077 if (sample_casts[p->type][conv_expr->conv->in_type] != c_none &&
1078 !sample_casts[p->type][conv_expr->conv->in_type](p))
Emeric Brun107ca302010-01-04 16:16:05 +01001079 return NULL;
1080
Willy Tarreau12e50112012-04-25 17:21:49 +02001081 /* OK cast succeeded */
1082
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001083 if (!conv_expr->conv->process(conv_expr->arg_p, p, conv_expr->conv->private))
Emeric Brun107ca302010-01-04 16:16:05 +01001084 return NULL;
Emeric Brun107ca302010-01-04 16:16:05 +01001085 }
1086 return p;
1087}
1088
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001089/*
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001090 * Resolve all remaining arguments in proxy <p>. Returns the number of
1091 * errors or 0 if everything is fine.
1092 */
1093int smp_resolve_args(struct proxy *p)
1094{
1095 struct arg_list *cur, *bak;
1096 const char *ctx, *where;
1097 const char *conv_ctx, *conv_pre, *conv_pos;
1098 struct userlist *ul;
Willy Tarreau46947782015-01-19 19:00:58 +01001099 struct my_regex *reg;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001100 struct arg *arg;
1101 int cfgerr = 0;
Willy Tarreau46947782015-01-19 19:00:58 +01001102 int rflags;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001103
1104 list_for_each_entry_safe(cur, bak, &p->conf.args.list, list) {
1105 struct proxy *px;
1106 struct server *srv;
1107 char *pname, *sname;
Willy Tarreau46947782015-01-19 19:00:58 +01001108 char *err;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001109
1110 arg = cur->arg;
1111
1112 /* prepare output messages */
1113 conv_pre = conv_pos = conv_ctx = "";
1114 if (cur->conv) {
1115 conv_ctx = cur->conv;
1116 conv_pre = "conversion keyword '";
1117 conv_pos = "' for ";
1118 }
1119
1120 where = "in";
1121 ctx = "sample fetch keyword";
1122 switch (cur->ctx) {
Willy Tarreau46947782015-01-19 19:00:58 +01001123 case ARGC_STK: where = "in stick rule in"; break;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001124 case ARGC_TRK: where = "in tracking rule in"; break;
1125 case ARGC_LOG: where = "in log-format string in"; break;
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +01001126 case ARGC_HRQ: where = "in http-request header format string in"; break;
1127 case ARGC_HRS: where = "in http-response header format string in"; break;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001128 case ARGC_UIF: where = "in unique-id-format string in"; break;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01001129 case ARGC_RDR: where = "in redirect format string in"; break;
Willy Tarreau3a4ac422014-06-13 16:17:14 +02001130 case ARGC_CAP: where = "in capture rule in"; break;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001131 case ARGC_ACL: ctx = "ACL keyword"; break;
1132 }
1133
1134 /* set a few default settings */
1135 px = p;
1136 pname = p->id;
1137
1138 switch (arg->type) {
1139 case ARGT_SRV:
1140 if (!arg->data.str.len) {
1141 Alert("parsing [%s:%d] : missing server name in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1142 cur->file, cur->line,
1143 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
1144 cfgerr++;
1145 continue;
1146 }
1147
1148 /* we support two formats : "bck/srv" and "srv" */
1149 sname = strrchr(arg->data.str.str, '/');
1150
1151 if (sname) {
1152 *sname++ = '\0';
1153 pname = arg->data.str.str;
1154
Willy Tarreau9e0bb102015-05-26 11:24:42 +02001155 px = proxy_be_by_name(pname);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001156 if (!px) {
1157 Alert("parsing [%s:%d] : unable to find proxy '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1158 cur->file, cur->line, pname,
1159 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
1160 cfgerr++;
1161 break;
1162 }
1163 }
1164 else
1165 sname = arg->data.str.str;
1166
1167 srv = findserver(px, sname);
1168 if (!srv) {
1169 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",
1170 cur->file, cur->line, sname, pname,
1171 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
1172 cfgerr++;
1173 break;
1174 }
1175
1176 free(arg->data.str.str);
1177 arg->data.str.str = NULL;
1178 arg->unresolved = 0;
1179 arg->data.srv = srv;
1180 break;
1181
1182 case ARGT_FE:
1183 if (arg->data.str.len) {
1184 pname = arg->data.str.str;
Willy Tarreau9e0bb102015-05-26 11:24:42 +02001185 px = proxy_fe_by_name(pname);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001186 }
1187
1188 if (!px) {
1189 Alert("parsing [%s:%d] : unable to find frontend '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1190 cur->file, cur->line, pname,
1191 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
1192 cfgerr++;
1193 break;
1194 }
1195
1196 if (!(px->cap & PR_CAP_FE)) {
1197 Alert("parsing [%s:%d] : proxy '%s', referenced in arg %d of %s%s%s%s '%s' %s proxy '%s', has not frontend capability.\n",
1198 cur->file, cur->line, pname,
1199 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
1200 cfgerr++;
1201 break;
1202 }
1203
1204 free(arg->data.str.str);
1205 arg->data.str.str = NULL;
1206 arg->unresolved = 0;
1207 arg->data.prx = px;
1208 break;
1209
1210 case ARGT_BE:
1211 if (arg->data.str.len) {
1212 pname = arg->data.str.str;
Willy Tarreau9e0bb102015-05-26 11:24:42 +02001213 px = proxy_be_by_name(pname);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001214 }
1215
1216 if (!px) {
1217 Alert("parsing [%s:%d] : unable to find backend '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1218 cur->file, cur->line, pname,
1219 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
1220 cfgerr++;
1221 break;
1222 }
1223
1224 if (!(px->cap & PR_CAP_BE)) {
1225 Alert("parsing [%s:%d] : proxy '%s', referenced in arg %d of %s%s%s%s '%s' %s proxy '%s', has not backend capability.\n",
1226 cur->file, cur->line, pname,
1227 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
1228 cfgerr++;
1229 break;
1230 }
1231
1232 free(arg->data.str.str);
1233 arg->data.str.str = NULL;
1234 arg->unresolved = 0;
1235 arg->data.prx = px;
1236 break;
1237
1238 case ARGT_TAB:
1239 if (arg->data.str.len) {
1240 pname = arg->data.str.str;
Willy Tarreaue2dc1fa2015-05-26 12:08:07 +02001241 px = proxy_tbl_by_name(pname);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001242 }
1243
1244 if (!px) {
1245 Alert("parsing [%s:%d] : unable to find table '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1246 cur->file, cur->line, pname,
1247 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
1248 cfgerr++;
1249 break;
1250 }
1251
1252 if (!px->table.size) {
1253 Alert("parsing [%s:%d] : no table in proxy '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1254 cur->file, cur->line, pname,
1255 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
1256 cfgerr++;
1257 break;
1258 }
1259
1260 free(arg->data.str.str);
1261 arg->data.str.str = NULL;
1262 arg->unresolved = 0;
1263 arg->data.prx = px;
1264 break;
1265
1266 case ARGT_USR:
1267 if (!arg->data.str.len) {
1268 Alert("parsing [%s:%d] : missing userlist name in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1269 cur->file, cur->line,
1270 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
1271 cfgerr++;
1272 break;
1273 }
1274
1275 if (p->uri_auth && p->uri_auth->userlist &&
1276 !strcmp(p->uri_auth->userlist->name, arg->data.str.str))
1277 ul = p->uri_auth->userlist;
1278 else
1279 ul = auth_find_userlist(arg->data.str.str);
1280
1281 if (!ul) {
1282 Alert("parsing [%s:%d] : unable to find userlist '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1283 cur->file, cur->line, arg->data.str.str,
1284 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
1285 cfgerr++;
1286 break;
1287 }
1288
1289 free(arg->data.str.str);
1290 arg->data.str.str = NULL;
1291 arg->unresolved = 0;
1292 arg->data.usr = ul;
1293 break;
Willy Tarreau46947782015-01-19 19:00:58 +01001294
1295 case ARGT_REG:
1296 if (!arg->data.str.len) {
1297 Alert("parsing [%s:%d] : missing regex in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1298 cur->file, cur->line,
1299 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
1300 cfgerr++;
1301 continue;
1302 }
1303
1304 reg = calloc(1, sizeof(*reg));
1305 if (!reg) {
1306 Alert("parsing [%s:%d] : not enough memory to build regex in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1307 cur->file, cur->line,
1308 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
1309 cfgerr++;
1310 continue;
1311 }
1312
1313 rflags = 0;
1314 rflags |= (arg->type_flags & ARGF_REG_ICASE) ? REG_ICASE : 0;
1315 err = NULL;
1316
Willy Tarreaud817e462015-01-23 20:23:17 +01001317 if (!regex_comp(arg->data.str.str, reg, !(rflags & REG_ICASE), 1 /* capture substr */, &err)) {
Willy Tarreau46947782015-01-19 19:00:58 +01001318 Alert("parsing [%s:%d] : error in regex '%s' in arg %d of %s%s%s%s '%s' %s proxy '%s' : %s.\n",
1319 cur->file, cur->line,
1320 arg->data.str.str,
1321 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id, err);
1322 cfgerr++;
1323 continue;
1324 }
1325
1326 free(arg->data.str.str);
1327 arg->data.str.str = NULL;
1328 arg->unresolved = 0;
1329 arg->data.reg = reg;
1330 break;
1331
1332
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001333 }
1334
1335 LIST_DEL(&cur->list);
1336 free(cur);
1337 } /* end of args processing */
1338
1339 return cfgerr;
1340}
1341
1342/*
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001343 * Process a fetch + format conversion as defined by the sample expression
1344 * <expr> on request or response considering the <opt> parameter. The output is
1345 * always of type string. If a stable sample can be fetched, or an unstable one
1346 * when <opt> contains SMP_OPT_FINAL, the sample is converted to a string and
1347 * returned without the SMP_F_MAY_CHANGE flag. If an unstable sample is found
1348 * and <opt> does not contain SMP_OPT_FINAL, then the sample is returned as-is
1349 * with its SMP_F_MAY_CHANGE flag so that the caller can check it and decide to
1350 * take actions (eg: wait longer). If a sample could not be found or could not
Willy Tarreau6bcb0a82014-07-30 08:56:35 +02001351 * be converted, NULL is returned. The caller MUST NOT use the sample if the
1352 * SMP_F_MAY_CHANGE flag is present, as it is used only as a hint that there is
1353 * still hope to get it after waiting longer, and is not converted to string.
1354 * The possible output combinations are the following :
1355 *
1356 * return MAY_CHANGE FINAL Meaning for the sample
1357 * NULL * * Not present and will never be (eg: header)
1358 * smp 0 * Final value converted (eg: header)
1359 * smp 1 0 Not present yet, may appear later (eg: header)
1360 * smp 1 1 never happens (either flag is cleared on output)
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001361 */
Willy Tarreau192252e2015-04-04 01:47:55 +02001362struct sample *sample_fetch_string(struct proxy *px, struct session *sess,
1363 struct stream *strm, unsigned int opt,
1364 struct sample_expr *expr)
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001365{
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001366 struct sample *smp = &temp_smp;
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001367
Willy Tarreau6c616e02014-06-25 16:56:41 +02001368 memset(smp, 0, sizeof(*smp));
1369
Willy Tarreau192252e2015-04-04 01:47:55 +02001370 if (!sample_process(px, sess, strm, opt, expr, smp)) {
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001371 if ((smp->flags & SMP_F_MAY_CHANGE) && !(opt & SMP_OPT_FINAL))
1372 return smp;
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001373 return NULL;
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001374 }
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001375
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01001376 if (!sample_casts[smp->type][SMP_T_STR])
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001377 return NULL;
1378
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01001379 if (!sample_casts[smp->type][SMP_T_STR](smp))
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001380 return NULL;
1381
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01001382 smp->type = SMP_T_STR;
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001383 smp->flags &= ~SMP_F_MAY_CHANGE;
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001384 return smp;
1385}
1386
Emeric Brun107ca302010-01-04 16:16:05 +01001387/*****************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +02001388/* Sample format convert functions */
Willy Tarreaub8c8f1f2012-04-23 22:38:26 +02001389/* These functions set the data type on return. */
Emeric Brun107ca302010-01-04 16:16:05 +01001390/*****************************************************************/
1391
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001392#ifdef DEBUG_EXPR
1393static int sample_conv_debug(const struct arg *arg_p, struct sample *smp, void *private)
1394{
1395 int i;
1396 struct sample tmp;
1397
1398 if (!(global.mode & MODE_QUIET) || (global.mode & (MODE_VERBOSE | MODE_STARTING))) {
1399 fprintf(stderr, "[debug converter] type: %s ", smp_to_type[smp->type]);
1400 if (!sample_casts[smp->type][SMP_T_STR]) {
1401 fprintf(stderr, "(undisplayable)");
1402 } else {
1403
1404 /* Copy sample fetch. This put the sample as const, the
1405 * cast will copy data if a transformation is required.
1406 */
1407 memcpy(&tmp, smp, sizeof(struct sample));
1408 tmp.flags = SMP_F_CONST;
1409
1410 if (!sample_casts[smp->type][SMP_T_STR](&tmp))
1411 fprintf(stderr, "(undisplayable)");
1412
1413 else {
1414 /* Display the displayable chars*. */
1415 fprintf(stderr, "<");
1416 for (i = 0; i < tmp.data.str.len; i++) {
1417 if (isprint(tmp.data.str.str[i]))
1418 fputc(tmp.data.str.str[i], stderr);
1419 else
1420 fputc('.', stderr);
1421 }
1422 }
1423 fprintf(stderr, ">\n");
1424 }
1425 }
1426 return 1;
1427}
1428#endif
1429
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001430static int sample_conv_bin2base64(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brun53d1a982014-04-30 18:21:37 +02001431{
1432 struct chunk *trash = get_trash_chunk();
1433 int b64_len;
1434
1435 trash->len = 0;
1436 b64_len = a2base64(smp->data.str.str, smp->data.str.len, trash->str, trash->size);
1437 if (b64_len < 0)
1438 return 0;
1439
1440 trash->len = b64_len;
1441 smp->data.str = *trash;
1442 smp->type = SMP_T_STR;
1443 smp->flags &= ~SMP_F_CONST;
1444 return 1;
1445}
1446
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001447static int sample_conv_bin2hex(const struct arg *arg_p, struct sample *smp, void *private)
Thierry FOURNIER2f49d6d2014-03-12 15:01:52 +01001448{
1449 struct chunk *trash = get_trash_chunk();
1450 unsigned char c;
1451 int ptr = 0;
1452
1453 trash->len = 0;
1454 while (ptr < smp->data.str.len && trash->len <= trash->size - 2) {
1455 c = smp->data.str.str[ptr++];
1456 trash->str[trash->len++] = hextab[(c >> 4) & 0xF];
1457 trash->str[trash->len++] = hextab[c & 0xF];
1458 }
1459 smp->data.str = *trash;
1460 smp->type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01001461 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIER2f49d6d2014-03-12 15:01:52 +01001462 return 1;
1463}
1464
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001465/* hashes the binary input into a 32-bit unsigned int */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001466static int sample_conv_djb2(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001467{
1468 smp->data.uint = hash_djb2(smp->data.str.str, smp->data.str.len);
1469 if (arg_p && arg_p->data.uint)
1470 smp->data.uint = full_hash(smp->data.uint);
1471 smp->type = SMP_T_UINT;
1472 return 1;
1473}
1474
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001475static int sample_conv_str2lower(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brun107ca302010-01-04 16:16:05 +01001476{
1477 int i;
1478
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01001479 if (!smp_dup(smp))
1480 return 0;
1481
Willy Tarreau342acb42012-04-23 22:03:39 +02001482 if (!smp->data.str.size)
Emeric Brun485479d2010-09-23 18:02:19 +02001483 return 0;
1484
Willy Tarreau342acb42012-04-23 22:03:39 +02001485 for (i = 0; i < smp->data.str.len; i++) {
1486 if ((smp->data.str.str[i] >= 'A') && (smp->data.str.str[i] <= 'Z'))
1487 smp->data.str.str[i] += 'a' - 'A';
Emeric Brun107ca302010-01-04 16:16:05 +01001488 }
1489 return 1;
1490}
1491
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001492static int sample_conv_str2upper(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brun107ca302010-01-04 16:16:05 +01001493{
1494 int i;
1495
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01001496 if (!smp_dup(smp))
1497 return 0;
1498
Willy Tarreau342acb42012-04-23 22:03:39 +02001499 if (!smp->data.str.size)
Emeric Brun485479d2010-09-23 18:02:19 +02001500 return 0;
1501
Willy Tarreau342acb42012-04-23 22:03:39 +02001502 for (i = 0; i < smp->data.str.len; i++) {
1503 if ((smp->data.str.str[i] >= 'a') && (smp->data.str.str[i] <= 'z'))
1504 smp->data.str.str[i] += 'A' - 'a';
Emeric Brun107ca302010-01-04 16:16:05 +01001505 }
1506 return 1;
1507}
1508
Willy Tarreauf9954102012-04-20 14:03:29 +02001509/* takes the netmask in arg_p */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001510static int sample_conv_ipmask(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud31d6eb2010-01-26 18:01:41 +01001511{
Willy Tarreau342acb42012-04-23 22:03:39 +02001512 smp->data.ipv4.s_addr &= arg_p->data.ipv4.s_addr;
Willy Tarreaub8c8f1f2012-04-23 22:38:26 +02001513 smp->type = SMP_T_IPV4;
Willy Tarreaud31d6eb2010-01-26 18:01:41 +01001514 return 1;
1515}
1516
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001517/* takes an UINT value on input supposed to represent the time since EPOCH,
1518 * adds an optional offset found in args[1] and emits a string representing
1519 * the local time in the format specified in args[1] using strftime().
1520 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001521static int sample_conv_ltime(const struct arg *args, struct sample *smp, void *private)
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001522{
1523 struct chunk *temp;
1524 time_t curr_date = smp->data.uint;
1525
1526 /* add offset */
1527 if (args[1].type == ARGT_SINT || args[1].type == ARGT_UINT)
1528 curr_date += args[1].data.sint;
1529
1530 temp = get_trash_chunk();
1531 temp->len = strftime(temp->str, temp->size, args[0].data.str.str, localtime(&curr_date));
1532 smp->data.str = *temp;
1533 smp->type = SMP_T_STR;
1534 return 1;
1535}
1536
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001537/* hashes the binary input into a 32-bit unsigned int */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001538static int sample_conv_sdbm(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001539{
1540 smp->data.uint = hash_sdbm(smp->data.str.str, smp->data.str.len);
1541 if (arg_p && arg_p->data.uint)
1542 smp->data.uint = full_hash(smp->data.uint);
1543 smp->type = SMP_T_UINT;
1544 return 1;
1545}
1546
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001547/* takes an UINT value on input supposed to represent the time since EPOCH,
1548 * adds an optional offset found in args[1] and emits a string representing
1549 * the UTC date in the format specified in args[1] using strftime().
1550 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001551static int sample_conv_utime(const struct arg *args, struct sample *smp, void *private)
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001552{
1553 struct chunk *temp;
1554 time_t curr_date = smp->data.uint;
1555
1556 /* add offset */
1557 if (args[1].type == ARGT_SINT || args[1].type == ARGT_UINT)
1558 curr_date += args[1].data.sint;
1559
1560 temp = get_trash_chunk();
1561 temp->len = strftime(temp->str, temp->size, args[0].data.str.str, gmtime(&curr_date));
1562 smp->data.str = *temp;
1563 smp->type = SMP_T_STR;
1564 return 1;
1565}
1566
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001567/* hashes the binary input into a 32-bit unsigned int */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001568static int sample_conv_wt6(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001569{
1570 smp->data.uint = hash_wt6(smp->data.str.str, smp->data.str.len);
1571 if (arg_p && arg_p->data.uint)
1572 smp->data.uint = full_hash(smp->data.uint);
1573 smp->type = SMP_T_UINT;
1574 return 1;
1575}
1576
Willy Tarreau80599772015-01-20 19:35:24 +01001577/* hashes the binary input into a 32-bit unsigned int */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001578static int sample_conv_crc32(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau80599772015-01-20 19:35:24 +01001579{
1580 smp->data.uint = hash_crc32(smp->data.str.str, smp->data.str.len);
1581 if (arg_p && arg_p->data.uint)
1582 smp->data.uint = full_hash(smp->data.uint);
1583 smp->type = SMP_T_UINT;
1584 return 1;
1585}
1586
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001587/* This function escape special json characters. The returned string can be
1588 * safely set between two '"' and used as json string. The json string is
1589 * defined like this:
1590 *
1591 * any Unicode character except '"' or '\' or control character
1592 * \", \\, \/, \b, \f, \n, \r, \t, \u + four-hex-digits
1593 *
1594 * The enum input_type contain all the allowed mode for decoding the input
1595 * string.
1596 */
1597enum input_type {
1598 IT_ASCII = 0,
1599 IT_UTF8,
1600 IT_UTF8S,
1601 IT_UTF8P,
1602 IT_UTF8PS,
1603};
1604static int sample_conv_json_check(struct arg *arg, struct sample_conv *conv,
1605 const char *file, int line, char **err)
1606{
1607 if (!arg) {
1608 memprintf(err, "Unexpected empty arg list");
1609 return 0;
1610 }
1611
1612 if (arg->type != ARGT_STR) {
1613 memprintf(err, "Unexpected arg type");
1614 return 0;
1615 }
1616
1617 if (strcmp(arg->data.str.str, "") == 0) {
1618 arg->type = ARGT_UINT;
1619 arg->data.uint = IT_ASCII;
1620 return 1;
1621 }
1622
1623 else if (strcmp(arg->data.str.str, "ascii") == 0) {
1624 arg->type = ARGT_UINT;
1625 arg->data.uint = IT_ASCII;
1626 return 1;
1627 }
1628
1629 else if (strcmp(arg->data.str.str, "utf8") == 0) {
1630 arg->type = ARGT_UINT;
1631 arg->data.uint = IT_UTF8;
1632 return 1;
1633 }
1634
1635 else if (strcmp(arg->data.str.str, "utf8s") == 0) {
1636 arg->type = ARGT_UINT;
1637 arg->data.uint = IT_UTF8S;
1638 return 1;
1639 }
1640
1641 else if (strcmp(arg->data.str.str, "utf8p") == 0) {
1642 arg->type = ARGT_UINT;
1643 arg->data.uint = IT_UTF8P;
1644 return 1;
1645 }
1646
1647 else if (strcmp(arg->data.str.str, "utf8ps") == 0) {
1648 arg->type = ARGT_UINT;
1649 arg->data.uint = IT_UTF8PS;
1650 return 1;
1651 }
1652
1653 memprintf(err, "Unexpected input code type at file '%s', line %d. "
1654 "Allowed value are 'ascii', 'utf8', 'utf8p' and 'utf8pp'", file, line);
1655 return 0;
1656}
1657
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001658static int sample_conv_json(const struct arg *arg_p, struct sample *smp, void *private)
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001659{
1660 struct chunk *temp;
1661 char _str[7]; /* \u + 4 hex digit + null char for sprintf. */
1662 const char *str;
1663 int len;
1664 enum input_type input_type = IT_ASCII;
1665 unsigned int c;
1666 unsigned int ret;
1667 char *p;
1668
1669 if (arg_p)
1670 input_type = arg_p->data.uint;
1671
1672 temp = get_trash_chunk();
1673 temp->len = 0;
1674
1675 p = smp->data.str.str;
1676 while (p < smp->data.str.str + smp->data.str.len) {
1677
1678 if (input_type == IT_ASCII) {
1679 /* Read input as ASCII. */
1680 c = *(unsigned char *)p;
1681 p++;
1682 }
1683 else {
1684 /* Read input as UTF8. */
1685 ret = utf8_next(p, smp->data.str.len - ( p - smp->data.str.str ), &c);
1686 p += utf8_return_length(ret);
1687
1688 if (input_type == IT_UTF8 && utf8_return_code(ret) != UTF8_CODE_OK)
1689 return 0;
1690 if (input_type == IT_UTF8S && utf8_return_code(ret) != UTF8_CODE_OK)
1691 continue;
1692 if (input_type == IT_UTF8P && utf8_return_code(ret) & (UTF8_CODE_INVRANGE|UTF8_CODE_BADSEQ))
1693 return 0;
1694 if (input_type == IT_UTF8PS && utf8_return_code(ret) & (UTF8_CODE_INVRANGE|UTF8_CODE_BADSEQ))
1695 continue;
1696
1697 /* Check too big values. */
1698 if ((unsigned int)c > 0xffff) {
1699 if (input_type == IT_UTF8 || input_type == IT_UTF8P)
1700 return 0;
1701 continue;
1702 }
1703 }
1704
1705 /* Convert character. */
1706 if (c == '"') {
1707 len = 2;
1708 str = "\\\"";
1709 }
1710 else if (c == '\\') {
1711 len = 2;
1712 str = "\\\\";
1713 }
1714 else if (c == '/') {
1715 len = 2;
1716 str = "\\/";
1717 }
1718 else if (c == '\b') {
1719 len = 2;
1720 str = "\\b";
1721 }
1722 else if (c == '\f') {
1723 len = 2;
1724 str = "\\f";
1725 }
1726 else if (c == '\r') {
1727 len = 2;
1728 str = "\\r";
1729 }
1730 else if (c == '\n') {
1731 len = 2;
1732 str = "\\n";
1733 }
1734 else if (c == '\t') {
1735 len = 2;
1736 str = "\\t";
1737 }
1738 else if (c > 0xff || !isprint(c)) {
1739 /* isprint generate a segfault if c is too big. The man says that
1740 * c must have the value of an unsigned char or EOF.
1741 */
1742 len = 6;
1743 _str[0] = '\\';
1744 _str[1] = 'u';
1745 snprintf(&_str[2], 5, "%04x", (unsigned short)c);
1746 str = _str;
1747 }
1748 else {
1749 len = 1;
1750 str = (char *)&c;
1751 }
1752
1753 /* Check length */
1754 if (temp->len + len > temp->size)
1755 return 0;
1756
1757 /* Copy string. */
1758 memcpy(temp->str + temp->len, str, len);
1759 temp->len += len;
1760 }
1761
1762 smp->flags &= ~SMP_F_CONST;
1763 smp->data.str = *temp;
1764 smp->type = SMP_T_STR;
1765
1766 return 1;
1767}
1768
Emeric Brun54c4ac82014-11-03 15:32:43 +01001769/* This sample function is designed to extract some bytes from an input buffer.
1770 * First arg is the offset.
1771 * Optional second arg is the length to truncate */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001772static int sample_conv_bytes(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brun54c4ac82014-11-03 15:32:43 +01001773{
1774 if (smp->data.str.len <= arg_p[0].data.uint) {
1775 smp->data.str.len = 0;
1776 return 1;
1777 }
1778
1779 if (smp->data.str.size)
1780 smp->data.str.size -= arg_p[0].data.uint;
1781 smp->data.str.len -= arg_p[0].data.uint;
1782 smp->data.str.str += arg_p[0].data.uint;
1783
1784 if ((arg_p[1].type == ARGT_UINT) && (arg_p[1].data.uint < smp->data.str.len))
1785 smp->data.str.len = arg_p[1].data.uint;
1786
1787 return 1;
1788}
1789
Emeric Brunf399b0d2014-11-03 17:07:03 +01001790static int sample_conv_field_check(struct arg *args, struct sample_conv *conv,
1791 const char *file, int line, char **err)
1792{
1793 struct arg *arg = args;
1794
1795 if (!arg) {
1796 memprintf(err, "Unexpected empty arg list");
1797 return 0;
1798 }
1799
1800 if (arg->type != ARGT_UINT) {
1801 memprintf(err, "Unexpected arg type");
1802 return 0;
1803 }
1804
1805 if (!arg->data.uint) {
1806 memprintf(err, "Unexpected value 0 for index");
1807 return 0;
1808 }
1809
1810 arg++;
1811
1812 if (arg->type != ARGT_STR) {
1813 memprintf(err, "Unexpected arg type");
1814 return 0;
1815 }
1816
1817 if (!arg->data.str.len) {
1818 memprintf(err, "Empty separators list");
1819 return 0;
1820 }
1821
1822 return 1;
1823}
1824
1825/* This sample function is designed to a return selected part of a string (field).
1826 * First arg is the index of the field (start at 1)
1827 * Second arg is a char list of separators (type string)
1828 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001829static int sample_conv_field(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brunf399b0d2014-11-03 17:07:03 +01001830{
1831 unsigned int field;
1832 char *start, *end;
1833 int i;
1834
1835 if (!arg_p[0].data.uint)
1836 return 0;
1837
1838 field = 1;
1839 end = start = smp->data.str.str;
1840 while (end - smp->data.str.str < smp->data.str.len) {
1841
1842 for (i = 0 ; i < arg_p[1].data.str.len ; i++) {
1843 if (*end == arg_p[1].data.str.str[i]) {
1844 if (field == arg_p[0].data.uint)
1845 goto found;
1846 start = end+1;
1847 field++;
1848 break;
1849 }
1850 }
1851 end++;
1852 }
1853
1854 /* Field not found */
1855 if (field != arg_p[0].data.uint) {
1856 smp->data.str.len = 0;
1857 return 1;
1858 }
1859found:
1860 smp->data.str.len = end - start;
1861 /* If ret string is len 0, no need to
1862 change pointers or to update size */
1863 if (!smp->data.str.len)
1864 return 1;
1865
1866 smp->data.str.str = start;
1867
1868 /* Compute remaining size if needed
1869 Note: smp->data.str.size cannot be set to 0 */
1870 if (smp->data.str.size)
1871 smp->data.str.size -= start - smp->data.str.str;
1872
1873 return 1;
1874}
1875
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01001876/* This sample function is designed to return a word from a string.
1877 * First arg is the index of the word (start at 1)
1878 * Second arg is a char list of words separators (type string)
1879 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001880static int sample_conv_word(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01001881{
1882 unsigned int word;
1883 char *start, *end;
1884 int i, issep, inword;
1885
1886 if (!arg_p[0].data.uint)
1887 return 0;
1888
1889 word = 0;
1890 inword = 0;
1891 end = start = smp->data.str.str;
1892 while (end - smp->data.str.str < smp->data.str.len) {
1893 issep = 0;
1894 for (i = 0 ; i < arg_p[1].data.str.len ; i++) {
1895 if (*end == arg_p[1].data.str.str[i]) {
1896 issep = 1;
1897 break;
1898 }
1899 }
1900 if (!inword) {
1901 if (!issep) {
1902 word++;
1903 start = end;
1904 inword = 1;
1905 }
1906 }
1907 else if (issep) {
1908 if (word == arg_p[0].data.uint)
1909 goto found;
1910 inword = 0;
1911 }
1912 end++;
1913 }
1914
1915 /* Field not found */
1916 if (word != arg_p[0].data.uint) {
1917 smp->data.str.len = 0;
1918 return 1;
1919 }
1920found:
1921 smp->data.str.len = end - start;
1922 /* If ret string is len 0, no need to
1923 change pointers or to update size */
1924 if (!smp->data.str.len)
1925 return 1;
1926
1927 smp->data.str.str = start;
1928
1929 /* Compute remaining size if needed
1930 Note: smp->data.str.size cannot be set to 0 */
1931 if (smp->data.str.size)
1932 smp->data.str.size -= start - smp->data.str.str;
1933
1934 return 1;
1935}
1936
Willy Tarreau7eda8492015-01-20 19:47:06 +01001937static int sample_conv_regsub_check(struct arg *args, struct sample_conv *conv,
1938 const char *file, int line, char **err)
1939{
1940 struct arg *arg = args;
1941 char *p;
1942 int len;
1943
1944 /* arg0 is a regex, it uses type_flag for ICASE and global match */
1945 arg[0].type_flags = 0;
1946
1947 if (arg[2].type != ARGT_STR)
1948 return 1;
1949
1950 p = arg[2].data.str.str;
1951 len = arg[2].data.str.len;
1952 while (len) {
1953 if (*p == 'i') {
1954 arg[0].type_flags |= ARGF_REG_ICASE;
1955 }
1956 else if (*p == 'g') {
1957 arg[0].type_flags |= ARGF_REG_GLOB;
1958 }
1959 else {
1960 memprintf(err, "invalid regex flag '%c', only 'i' and 'g' are supported", *p);
1961 return 0;
1962 }
1963 p++;
1964 len--;
1965 }
1966 return 1;
1967}
1968
1969/* This sample function is designed to do the equivalent of s/match/replace/ on
1970 * the input string. It applies a regex and restarts from the last matched
1971 * location until nothing matches anymore. First arg is the regex to apply to
1972 * the input string, second arg is the replacement expression.
1973 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001974static int sample_conv_regsub(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau7eda8492015-01-20 19:47:06 +01001975{
1976 char *start, *end;
1977 struct my_regex *reg = arg_p[0].data.reg;
1978 regmatch_t pmatch[MAX_MATCH];
1979 struct chunk *trash = get_trash_chunk();
1980 int flag, max;
1981 int found;
1982
1983 start = smp->data.str.str;
1984 end = start + smp->data.str.len;
1985
1986 flag = 0;
1987 while (1) {
1988 /* check for last round which is used to copy remaining parts
1989 * when not running in global replacement mode.
1990 */
1991 found = 0;
1992 if ((arg_p[0].type_flags & ARGF_REG_GLOB) || !(flag & REG_NOTBOL)) {
1993 /* Note: we can have start == end on empty strings or at the end */
1994 found = regex_exec_match2(reg, start, end - start, MAX_MATCH, pmatch, flag);
1995 }
1996
1997 if (!found)
1998 pmatch[0].rm_so = end - start;
1999
2000 /* copy the heading non-matching part (which may also be the tail if nothing matches) */
2001 max = trash->size - trash->len;
2002 if (max && pmatch[0].rm_so > 0) {
2003 if (max > pmatch[0].rm_so)
2004 max = pmatch[0].rm_so;
2005 memcpy(trash->str + trash->len, start, max);
2006 trash->len += max;
2007 }
2008
2009 if (!found)
2010 break;
2011
2012 /* replace the matching part */
2013 max = trash->size - trash->len;
2014 if (max) {
2015 if (max > arg_p[1].data.str.len)
2016 max = arg_p[1].data.str.len;
2017 memcpy(trash->str + trash->len, arg_p[1].data.str.str, max);
2018 trash->len += max;
2019 }
2020
2021 /* stop here if we're done with this string */
2022 if (start >= end)
2023 break;
2024
2025 /* We have a special case for matches of length 0 (eg: "x*y*").
2026 * These ones are considered to match in front of a character,
2027 * so we have to copy that character and skip to the next one.
2028 */
2029 if (!pmatch[0].rm_eo) {
2030 if (trash->len < trash->size)
2031 trash->str[trash->len++] = start[pmatch[0].rm_eo];
2032 pmatch[0].rm_eo++;
2033 }
2034
2035 start += pmatch[0].rm_eo;
2036 flag |= REG_NOTBOL;
2037 }
2038
2039 smp->data.str = *trash;
2040 return 1;
2041}
2042
Willy Tarreau97707872015-01-27 15:12:13 +01002043/* Takes a UINT on input, applies a binary twos complement and returns the UINT
2044 * result.
2045 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002046static int sample_conv_binary_cpl(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002047{
2048 smp->data.uint = ~smp->data.uint;
2049 return 1;
2050}
2051
2052/* Takes a UINT on input, applies a binary "and" with the UINT in arg_p, and
2053 * returns the UINT result.
2054 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002055static int sample_conv_binary_and(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002056{
2057 smp->data.uint &= arg_p->data.uint;
2058 return 1;
2059}
2060
2061/* Takes a UINT on input, applies a binary "or" with the UINT in arg_p, and
2062 * returns the UINT result.
2063 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002064static int sample_conv_binary_or(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002065{
2066 smp->data.uint |= arg_p->data.uint;
2067 return 1;
2068}
2069
2070/* Takes a UINT on input, applies a binary "xor" with the UINT in arg_p, and
2071 * returns the UINT result.
2072 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002073static int sample_conv_binary_xor(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002074{
2075 smp->data.uint ^= arg_p->data.uint;
2076 return 1;
2077}
2078
2079/* Takes a UINT on input, applies an arithmetic "add" with the UINT in arg_p,
2080 * and returns the UINT result.
2081 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002082static int sample_conv_arith_add(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002083{
2084 smp->data.uint += arg_p->data.uint;
2085 return 1;
2086}
2087
2088/* Takes a UINT on input, applies an arithmetic "sub" with the UINT in arg_p,
2089 * and returns the UINT result.
2090 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002091static int sample_conv_arith_sub(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002092 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002093{
2094 smp->data.uint -= arg_p->data.uint;
2095 return 1;
2096}
2097
2098/* Takes a UINT on input, applies an arithmetic "mul" with the UINT in arg_p,
2099 * and returns the UINT result.
2100 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002101static int sample_conv_arith_mul(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002102 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002103{
2104 smp->data.uint *= arg_p->data.uint;
2105 return 1;
2106}
2107
2108/* Takes a UINT on input, applies an arithmetic "div" with the UINT in arg_p,
2109 * and returns the UINT result. If arg_p makes the result overflow, then the
2110 * largest possible quantity is returned.
2111 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002112static int sample_conv_arith_div(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002113 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002114{
2115 if (arg_p->data.uint)
2116 smp->data.uint /= arg_p->data.uint;
2117 else
2118 smp->data.uint = ~0;
2119 return 1;
2120}
2121
2122/* Takes a UINT on input, applies an arithmetic "mod" with the UINT in arg_p,
2123 * and returns the UINT result. If arg_p makes the result overflow, then zero
2124 * is returned.
2125 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002126static int sample_conv_arith_mod(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002127 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002128{
2129 if (arg_p->data.uint)
2130 smp->data.uint %= arg_p->data.uint;
2131 else
2132 smp->data.uint = 0;
2133 return 1;
2134}
2135
2136/* Takes an UINT on input, applies an arithmetic "neg" and returns the UINT
2137 * result.
2138 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002139static int sample_conv_arith_neg(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002140 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002141{
2142 smp->data.uint = -smp->data.uint;
2143 return 1;
2144}
2145
2146/* Takes a UINT on input, returns true is the value is non-null, otherwise
2147 * false. The output is a BOOL.
2148 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002149static int sample_conv_arith_bool(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002150 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002151{
2152 smp->data.uint = !!smp->data.uint;
2153 smp->type = SMP_T_BOOL;
2154 return 1;
2155}
2156
2157/* Takes a UINT on input, returns false is the value is non-null, otherwise
2158 * truee. The output is a BOOL.
2159 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002160static int sample_conv_arith_not(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002161 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002162{
2163 smp->data.uint = !smp->data.uint;
2164 smp->type = SMP_T_BOOL;
2165 return 1;
2166}
2167
2168/* Takes a UINT on input, returns true is the value is odd, otherwise false.
2169 * The output is a BOOL.
2170 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002171static int sample_conv_arith_odd(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002172 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002173{
2174 smp->data.uint = smp->data.uint & 1;
2175 smp->type = SMP_T_BOOL;
2176 return 1;
2177}
2178
2179/* Takes a UINT on input, returns true is the value is even, otherwise false.
2180 * The output is a BOOL.
2181 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002182static int sample_conv_arith_even(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002183 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002184{
2185 smp->data.uint = !(smp->data.uint & 1);
2186 smp->type = SMP_T_BOOL;
2187 return 1;
2188}
Thomas Holmes951d44d2015-05-13 11:27:35 +01002189
2190#ifdef USE_51DEGREES
Thomas Holmes4d441a72015-05-29 12:54:25 +01002191#ifdef FIFTYONEDEGREES_H_TRIE_INCLUDED
2192static int
2193sample_fiftyone_degrees(const struct arg *args, struct sample *smp, void *private)
2194{
2195 int i; // used in loops.
2196 int device_offset;
2197 int property_index;
2198 char no_data[] = "NoData"; // response when no data could be found.
2199 struct chunk *tmp;
2200
2201 // use a temporary trash buffer and copy data in it
2202 smp->data.str.str[smp->data.str.len] = '\0';
2203
2204 // perform detection.
2205 device_offset = fiftyoneDegreesGetDeviceOffset(smp->data.str.str);
2206
2207 i = 0;
2208 tmp = get_trash_chunk();
2209 chunk_reset(tmp);
2210
2211 // loop through property names passed to the filter and fetch them from the dataset.
2212 while (args[i].data.str.str) {
2213 // try to find request property in dataset.
2214 property_index = fiftyoneDegreesGetPropertyIndex(args[i].data.str.str);
2215 if (property_index > 0) {
2216 chunk_appendf(tmp, "%s", fiftyoneDegreesGetValue(device_offset, property_index));
2217 }
2218 else {
2219 chunk_appendf(tmp, "%s", no_data);
2220 }
2221 // add seperator
2222 if (global._51d_property_seperator)
2223 chunk_appendf(tmp, "%c", global._51d_property_seperator);
2224 else
2225 chunk_appendf(tmp, ",");
2226 ++i;
2227 }
2228
2229 if (tmp->len) {
2230 --tmp->len;
2231 tmp->str[tmp->len] = '\0';
2232 }
2233
2234 smp->data.str.str = tmp->str;
2235 smp->data.str.len = strlen(smp->data.str.str);
2236
2237 return 1;
2238}
2239#endif // FIFTYONEDEGREES_H_TRIE_INCLUDED
2240
2241#ifdef FIFTYONEDEGREES_H_PATTERN_INCLUDED
2242static int
2243sample_fiftyone_degrees(const struct arg *args, struct sample *smp, void *private)
Thomas Holmes951d44d2015-05-13 11:27:35 +01002244{
2245 int i, j, found; // used in loops.
2246 fiftyoneDegreesWorkset* ws; // workset for detection.
2247 char no_data[] = "NoData"; // response when no data could be found.
Thomas Holmes4d441a72015-05-29 12:54:25 +01002248 const char* property_name;
Thomas Holmes951d44d2015-05-13 11:27:35 +01002249 struct chunk *tmp;
2250
2251 // use a temporary trash buffer and copy data in it
2252 smp->data.str.str[smp->data.str.len] = '\0';
2253
2254 // create workset. this will later contain detection results.
2255 ws = fiftyoneDegreesCreateWorkset(&global._51d_data_set);
2256 if (!ws)
2257 return 0;
2258
2259 // perform detection.
2260 fiftyoneDegreesMatch(ws, smp->data.str.str);
2261
2262 i = 0;
2263 tmp = get_trash_chunk();
2264 chunk_reset(tmp);
2265 // loop through property names passed to the filter and fetch them from the dataset.
2266 while (args[i].data.str.str) {
2267 found = j = 0;
2268 // try to find request property in dataset.
Thomas Holmes4d441a72015-05-29 12:54:25 +01002269 for (j = 0; j < ws->dataSet->requiredPropertyCount; j++) {
2270 property_name = fiftyoneDegreesGetPropertyName(ws->dataSet, ws->dataSet->requiredProperties[j]);
2271 if (strcmp(property_name, args[i].data.str.str) == 0) {
Thomas Holmes951d44d2015-05-13 11:27:35 +01002272 found = 1;
2273 fiftyoneDegreesSetValues(ws, j);
2274 chunk_appendf(tmp, "%s", fiftyoneDegreesGetValueName(ws->dataSet, *ws->values));
2275 break;
2276 }
Thomas Holmes951d44d2015-05-13 11:27:35 +01002277 }
Thomas Holmes4d441a72015-05-29 12:54:25 +01002278 if (!found) {
Thomas Holmes951d44d2015-05-13 11:27:35 +01002279 chunk_appendf(tmp, "%s", no_data);
Thomas Holmes4d441a72015-05-29 12:54:25 +01002280 }
Thomas Holmes951d44d2015-05-13 11:27:35 +01002281
2282 // add seperator
2283 if (global._51d_property_seperator)
2284 chunk_appendf(tmp, "%c", global._51d_property_seperator);
2285 else
2286 chunk_appendf(tmp, ",");
2287 ++i;
2288 }
2289
2290 if (tmp->len) {
2291 --tmp->len;
2292 tmp->str[tmp->len] = '\0';
2293 }
2294
2295 smp->data.str.str = tmp->str;
2296 smp->data.str.len = strlen(smp->data.str.str);
2297
2298 fiftyoneDegreesFreeWorkset(ws);
2299
2300 return 1;
2301}
Thomas Holmes4d441a72015-05-29 12:54:25 +01002302#endif // FIFTYONEDEGREES_H_PATTERN_INCLUDED
2303#endif // USE_51DEGREES
Thomas Holmes951d44d2015-05-13 11:27:35 +01002304
Willy Tarreau97707872015-01-27 15:12:13 +01002305
Willy Tarreau5b8ad222013-07-25 12:17:57 +02002306/************************************************************************/
2307/* All supported sample fetch functions must be declared here */
2308/************************************************************************/
2309
2310/* force TRUE to be returned at the fetch level */
2311static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002312smp_fetch_true(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau5b8ad222013-07-25 12:17:57 +02002313{
2314 smp->type = SMP_T_BOOL;
2315 smp->data.uint = 1;
2316 return 1;
2317}
2318
2319/* force FALSE to be returned at the fetch level */
2320static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002321smp_fetch_false(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau5b8ad222013-07-25 12:17:57 +02002322{
2323 smp->type = SMP_T_BOOL;
2324 smp->data.uint = 0;
2325 return 1;
2326}
2327
2328/* retrieve environment variable $1 as a string */
2329static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002330smp_fetch_env(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau5b8ad222013-07-25 12:17:57 +02002331{
2332 char *env;
2333
2334 if (!args || args[0].type != ARGT_STR)
2335 return 0;
2336
2337 env = getenv(args[0].data.str.str);
2338 if (!env)
2339 return 0;
2340
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01002341 smp->type = SMP_T_STR;
2342 smp->flags = SMP_F_CONST;
Willy Tarreau5b8ad222013-07-25 12:17:57 +02002343 smp->data.str.str = env;
2344 smp->data.str.len = strlen(env);
2345 return 1;
2346}
2347
Willy Tarreau6236d3a2013-07-25 14:28:25 +02002348/* retrieve the current local date in epoch time, and applies an optional offset
2349 * of args[0] seconds.
2350 */
2351static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002352smp_fetch_date(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau6236d3a2013-07-25 14:28:25 +02002353{
2354 smp->data.uint = date.tv_sec;
2355
2356 /* add offset */
2357 if (args && (args[0].type == ARGT_SINT || args[0].type == ARGT_UINT))
2358 smp->data.uint += args[0].data.sint;
2359
2360 smp->type = SMP_T_UINT;
2361 smp->flags |= SMP_F_VOL_TEST | SMP_F_MAY_CHANGE;
2362 return 1;
2363}
2364
Willy Tarreau0f30d262014-11-24 16:02:05 +01002365/* returns the number of processes */
2366static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002367smp_fetch_nbproc(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau0f30d262014-11-24 16:02:05 +01002368{
2369 smp->type = SMP_T_UINT;
2370 smp->data.uint = global.nbproc;
2371 return 1;
2372}
2373
2374/* returns the number of the current process (between 1 and nbproc */
2375static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002376smp_fetch_proc(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau0f30d262014-11-24 16:02:05 +01002377{
2378 smp->type = SMP_T_UINT;
2379 smp->data.uint = relative_pid;
2380 return 1;
2381}
2382
Willy Tarreau84310e22014-02-14 11:59:04 +01002383/* generate a random 32-bit integer for whatever purpose, with an optional
2384 * range specified in argument.
2385 */
2386static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002387smp_fetch_rand(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau84310e22014-02-14 11:59:04 +01002388{
2389 smp->data.uint = random();
2390
2391 /* reduce if needed. Don't do a modulo, use all bits! */
2392 if (args && args[0].type == ARGT_UINT)
Vincent Bernat1228dc02014-12-10 10:31:37 +01002393 smp->data.uint = ((uint64_t)smp->data.uint * args[0].data.uint) / ((u64)RAND_MAX+1);
Willy Tarreau84310e22014-02-14 11:59:04 +01002394
2395 smp->type = SMP_T_UINT;
2396 smp->flags |= SMP_F_VOL_TEST | SMP_F_MAY_CHANGE;
2397 return 1;
2398}
2399
Willy Tarreau0f30d262014-11-24 16:02:05 +01002400/* returns true if the current process is stopping */
2401static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002402smp_fetch_stopping(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau0f30d262014-11-24 16:02:05 +01002403{
2404 smp->type = SMP_T_BOOL;
2405 smp->data.uint = stopping;
2406 return 1;
2407}
2408
Willy Tarreau5b8ad222013-07-25 12:17:57 +02002409/* Note: must not be declared <const> as its list will be overwritten.
2410 * Note: fetches that may return multiple types must be declared as the lowest
2411 * common denominator, the type that can be casted into all other ones. For
2412 * instance IPv4/IPv6 must be declared IPv4.
2413 */
2414static struct sample_fetch_kw_list smp_kws = {ILH, {
2415 { "always_false", smp_fetch_false, 0, NULL, SMP_T_BOOL, SMP_USE_INTRN },
2416 { "always_true", smp_fetch_true, 0, NULL, SMP_T_BOOL, SMP_USE_INTRN },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01002417 { "env", smp_fetch_env, ARG1(1,STR), NULL, SMP_T_STR, SMP_USE_INTRN },
Willy Tarreau6236d3a2013-07-25 14:28:25 +02002418 { "date", smp_fetch_date, ARG1(0,SINT), NULL, SMP_T_UINT, SMP_USE_INTRN },
Willy Tarreau0f30d262014-11-24 16:02:05 +01002419 { "nbproc", smp_fetch_nbproc,0, NULL, SMP_T_UINT, SMP_USE_INTRN },
2420 { "proc", smp_fetch_proc, 0, NULL, SMP_T_UINT, SMP_USE_INTRN },
Willy Tarreau84310e22014-02-14 11:59:04 +01002421 { "rand", smp_fetch_rand, ARG1(0,UINT), NULL, SMP_T_UINT, SMP_USE_INTRN },
Willy Tarreau0f30d262014-11-24 16:02:05 +01002422 { "stopping", smp_fetch_stopping, 0, NULL, SMP_T_BOOL, SMP_USE_INTRN },
Willy Tarreau5b8ad222013-07-25 12:17:57 +02002423 { /* END */ },
2424}};
2425
Emeric Brun107ca302010-01-04 16:16:05 +01002426/* Note: must not be declared <const> as its list will be overwritten */
Willy Tarreaudc13c112013-06-21 23:16:39 +02002427static struct sample_conv_kw_list sample_conv_kws = {ILH, {
Thierry FOURNIER9687c772015-05-07 15:46:29 +02002428#ifdef DEBUG_EXPR
2429 { "debug", sample_conv_debug, 0, NULL, SMP_T_ANY, SMP_T_ANY },
2430#endif
2431
Emeric Brun53d1a982014-04-30 18:21:37 +02002432 { "base64", sample_conv_bin2base64,0, NULL, SMP_T_BIN, SMP_T_STR },
Willy Tarreau12785782012-04-27 21:37:17 +02002433 { "upper", sample_conv_str2upper, 0, NULL, SMP_T_STR, SMP_T_STR },
2434 { "lower", sample_conv_str2lower, 0, NULL, SMP_T_STR, SMP_T_STR },
Thierry FOURNIER2f49d6d2014-03-12 15:01:52 +01002435 { "hex", sample_conv_bin2hex, 0, NULL, SMP_T_BIN, SMP_T_STR },
Willy Tarreau12785782012-04-27 21:37:17 +02002436 { "ipmask", sample_conv_ipmask, ARG1(1,MSK4), NULL, SMP_T_IPV4, SMP_T_IPV4 },
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02002437 { "ltime", sample_conv_ltime, ARG2(1,STR,SINT), NULL, SMP_T_UINT, SMP_T_STR },
2438 { "utime", sample_conv_utime, ARG2(1,STR,SINT), NULL, SMP_T_UINT, SMP_T_STR },
Willy Tarreau80599772015-01-20 19:35:24 +01002439 { "crc32", sample_conv_crc32, ARG1(0,UINT), NULL, SMP_T_BIN, SMP_T_UINT },
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02002440 { "djb2", sample_conv_djb2, ARG1(0,UINT), NULL, SMP_T_BIN, SMP_T_UINT },
2441 { "sdbm", sample_conv_sdbm, ARG1(0,UINT), NULL, SMP_T_BIN, SMP_T_UINT },
2442 { "wt6", sample_conv_wt6, ARG1(0,UINT), NULL, SMP_T_BIN, SMP_T_UINT },
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002443 { "json", sample_conv_json, ARG1(1,STR), sample_conv_json_check, SMP_T_STR, SMP_T_STR },
Emeric Brun54c4ac82014-11-03 15:32:43 +01002444 { "bytes", sample_conv_bytes, ARG2(1,UINT,UINT), NULL, SMP_T_BIN, SMP_T_BIN },
Emeric Brunf399b0d2014-11-03 17:07:03 +01002445 { "field", sample_conv_field, ARG2(2,UINT,STR), sample_conv_field_check, SMP_T_STR, SMP_T_STR },
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002446 { "word", sample_conv_word, ARG2(2,UINT,STR), sample_conv_field_check, SMP_T_STR, SMP_T_STR },
Willy Tarreau7eda8492015-01-20 19:47:06 +01002447 { "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 +01002448
2449 { "and", sample_conv_binary_and, ARG1(1,UINT), NULL, SMP_T_UINT, SMP_T_UINT },
2450 { "or", sample_conv_binary_or, ARG1(1,UINT), NULL, SMP_T_UINT, SMP_T_UINT },
2451 { "xor", sample_conv_binary_xor, ARG1(1,UINT), NULL, SMP_T_UINT, SMP_T_UINT },
2452 { "cpl", sample_conv_binary_cpl, 0, NULL, SMP_T_UINT, SMP_T_UINT },
2453 { "bool", sample_conv_arith_bool, 0, NULL, SMP_T_UINT, SMP_T_BOOL },
2454 { "not", sample_conv_arith_not, 0, NULL, SMP_T_UINT, SMP_T_BOOL },
2455 { "odd", sample_conv_arith_odd, 0, NULL, SMP_T_UINT, SMP_T_BOOL },
2456 { "even", sample_conv_arith_even, 0, NULL, SMP_T_UINT, SMP_T_BOOL },
2457 { "add", sample_conv_arith_add, ARG1(1,UINT), NULL, SMP_T_UINT, SMP_T_UINT },
2458 { "sub", sample_conv_arith_sub, ARG1(1,UINT), NULL, SMP_T_UINT, SMP_T_UINT },
2459 { "mul", sample_conv_arith_mul, ARG1(1,UINT), NULL, SMP_T_UINT, SMP_T_UINT },
2460 { "div", sample_conv_arith_div, ARG1(1,UINT), NULL, SMP_T_UINT, SMP_T_UINT },
2461 { "mod", sample_conv_arith_mod, ARG1(1,UINT), NULL, SMP_T_UINT, SMP_T_UINT },
2462 { "neg", sample_conv_arith_neg, 0, NULL, SMP_T_UINT, SMP_T_UINT },
Thomas Holmes951d44d2015-05-13 11:27:35 +01002463#ifdef USE_51DEGREES
Thomas Holmes4d441a72015-05-29 12:54:25 +01002464 { "51d", sample_fiftyone_degrees,ARG5(1,STR,STR,STR,STR,STR), NULL, SMP_T_STR, SMP_T_STR },
Thomas Holmes951d44d2015-05-13 11:27:35 +01002465#endif
Willy Tarreau97707872015-01-27 15:12:13 +01002466
Willy Tarreau9fcb9842012-04-20 14:45:49 +02002467 { NULL, NULL, 0, 0, 0 },
Emeric Brun107ca302010-01-04 16:16:05 +01002468}};
2469
2470__attribute__((constructor))
Willy Tarreau12785782012-04-27 21:37:17 +02002471static void __sample_init(void)
Emeric Brun107ca302010-01-04 16:16:05 +01002472{
Willy Tarreau5b8ad222013-07-25 12:17:57 +02002473 /* register sample fetch and format conversion keywords */
2474 sample_register_fetches(&smp_kws);
Willy Tarreau12785782012-04-27 21:37:17 +02002475 sample_register_convs(&sample_conv_kws);
Emeric Brun107ca302010-01-04 16:16:05 +01002476}