blob: 2fd97bbb5815d19b3f99b3ed00f8cb7579ea6baf [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] = {
41 [SMP_T_BOOL] = "bool",
42 [SMP_T_UINT] = "uint",
43 [SMP_T_SINT] = "sint",
44 [SMP_T_ADDR] = "addr",
45 [SMP_T_IPV4] = "ipv4",
46 [SMP_T_IPV6] = "ipv6",
47 [SMP_T_STR] = "str",
48 [SMP_T_BIN] = "bin",
Thierry FOURNIER4c2479e2015-06-03 20:12:04 +020049 [SMP_T_METH] = "meth",
Willy Tarreau1cf8f082014-02-07 12:14:54 +010050};
51
Willy Tarreau12785782012-04-27 21:37:17 +020052/* static sample used in sample_process() when <p> is NULL */
Willy Tarreaub4a88f02012-04-23 21:35:11 +020053static struct sample temp_smp;
Emeric Brun107ca302010-01-04 16:16:05 +010054
Willy Tarreau12785782012-04-27 21:37:17 +020055/* list head of all known sample fetch keywords */
56static struct sample_fetch_kw_list sample_fetches = {
57 .list = LIST_HEAD_INIT(sample_fetches.list)
Emeric Brun107ca302010-01-04 16:16:05 +010058};
59
Willy Tarreau12785782012-04-27 21:37:17 +020060/* list head of all known sample format conversion keywords */
61static struct sample_conv_kw_list sample_convs = {
62 .list = LIST_HEAD_INIT(sample_convs.list)
Emeric Brun107ca302010-01-04 16:16:05 +010063};
64
Willy Tarreau80aca902013-01-07 15:42:20 +010065const unsigned int fetch_cap[SMP_SRC_ENTRIES] = {
66 [SMP_SRC_INTRN] = (SMP_VAL_FE_CON_ACC | SMP_VAL_FE_SES_ACC | SMP_VAL_FE_REQ_CNT |
67 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
68 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
69 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
70 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
71 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
72 SMP_VAL_FE_LOG_END),
73
74 [SMP_SRC_LISTN] = (SMP_VAL_FE_CON_ACC | SMP_VAL_FE_SES_ACC | SMP_VAL_FE_REQ_CNT |
75 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
76 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
77 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
78 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
79 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
80 SMP_VAL_FE_LOG_END),
81
82 [SMP_SRC_FTEND] = (SMP_VAL_FE_CON_ACC | SMP_VAL_FE_SES_ACC | SMP_VAL_FE_REQ_CNT |
83 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
84 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
85 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
86 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
87 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
88 SMP_VAL_FE_LOG_END),
89
90 [SMP_SRC_L4CLI] = (SMP_VAL_FE_CON_ACC | SMP_VAL_FE_SES_ACC | SMP_VAL_FE_REQ_CNT |
91 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
92 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
93 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
94 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
95 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
96 SMP_VAL_FE_LOG_END),
97
98 [SMP_SRC_L5CLI] = (SMP_VAL___________ | SMP_VAL_FE_SES_ACC | SMP_VAL_FE_REQ_CNT |
99 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
100 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
101 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
102 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
103 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
104 SMP_VAL_FE_LOG_END),
105
106 [SMP_SRC_TRACK] = (SMP_VAL_FE_CON_ACC | SMP_VAL_FE_SES_ACC | SMP_VAL_FE_REQ_CNT |
107 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
108 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
109 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
110 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
111 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
112 SMP_VAL_FE_LOG_END),
113
114 [SMP_SRC_L6REQ] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_FE_REQ_CNT |
115 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
116 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
117 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL___________ |
118 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
119 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
120 SMP_VAL___________),
121
122 [SMP_SRC_HRQHV] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_FE_REQ_CNT |
123 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
124 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
125 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL___________ |
126 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
127 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
128 SMP_VAL___________),
129
130 [SMP_SRC_HRQHP] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_FE_REQ_CNT |
131 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
132 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
133 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
134 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
135 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
136 SMP_VAL_FE_LOG_END),
137
138 [SMP_SRC_HRQBO] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
139 SMP_VAL___________ | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
140 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
141 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL___________ |
142 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
143 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
144 SMP_VAL___________),
145
146 [SMP_SRC_BKEND] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
147 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
148 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
149 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
150 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
151 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
152 SMP_VAL_FE_LOG_END),
153
154 [SMP_SRC_SERVR] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
155 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
156 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
157 SMP_VAL___________ | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
158 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
159 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
160 SMP_VAL_FE_LOG_END),
161
162 [SMP_SRC_L4SRV] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
163 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
164 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
165 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_BE_RES_CNT |
166 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
167 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
168 SMP_VAL_FE_LOG_END),
169
170 [SMP_SRC_L5SRV] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
171 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
172 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
173 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_BE_RES_CNT |
174 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
175 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
176 SMP_VAL_FE_LOG_END),
177
178 [SMP_SRC_L6RES] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
179 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
180 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
181 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_BE_RES_CNT |
182 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
183 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
184 SMP_VAL___________),
185
186 [SMP_SRC_HRSHV] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
187 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
188 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
189 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_BE_RES_CNT |
190 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
191 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
192 SMP_VAL___________),
193
194 [SMP_SRC_HRSHP] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
195 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
196 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
197 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_BE_RES_CNT |
198 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
199 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
200 SMP_VAL_FE_LOG_END),
201
202 [SMP_SRC_HRSBO] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
203 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
204 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
205 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
206 SMP_VAL___________ | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
207 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
208 SMP_VAL___________),
209
210 [SMP_SRC_RQFIN] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
211 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
212 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
213 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
214 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
215 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
216 SMP_VAL_FE_LOG_END),
217
218 [SMP_SRC_RSFIN] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
219 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
220 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
221 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
222 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
223 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
224 SMP_VAL_FE_LOG_END),
225
226 [SMP_SRC_TXFIN] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
227 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
228 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
229 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
230 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
231 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
232 SMP_VAL_FE_LOG_END),
233
234 [SMP_SRC_SSFIN] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
235 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
236 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
237 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
238 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
239 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
240 SMP_VAL_FE_LOG_END),
241};
242
243static const char *fetch_src_names[SMP_SRC_ENTRIES] = {
244 [SMP_SRC_INTRN] = "internal state",
245 [SMP_SRC_LISTN] = "listener",
246 [SMP_SRC_FTEND] = "frontend",
247 [SMP_SRC_L4CLI] = "client address",
248 [SMP_SRC_L5CLI] = "client-side connection",
249 [SMP_SRC_TRACK] = "track counters",
250 [SMP_SRC_L6REQ] = "request buffer",
251 [SMP_SRC_HRQHV] = "HTTP request headers",
252 [SMP_SRC_HRQHP] = "HTTP request",
253 [SMP_SRC_HRQBO] = "HTTP request body",
254 [SMP_SRC_BKEND] = "backend",
255 [SMP_SRC_SERVR] = "server",
256 [SMP_SRC_L4SRV] = "server address",
257 [SMP_SRC_L5SRV] = "server-side connection",
258 [SMP_SRC_L6RES] = "response buffer",
259 [SMP_SRC_HRSHV] = "HTTP response headers",
260 [SMP_SRC_HRSHP] = "HTTP response",
261 [SMP_SRC_HRSBO] = "HTTP response body",
262 [SMP_SRC_RQFIN] = "request buffer statistics",
263 [SMP_SRC_RSFIN] = "response buffer statistics",
264 [SMP_SRC_TXFIN] = "transaction statistics",
265 [SMP_SRC_SSFIN] = "session statistics",
266};
267
Willy Tarreaubf8e2512013-03-25 14:52:41 +0100268static const char *fetch_ckp_names[SMP_CKP_ENTRIES] = {
269 [SMP_CKP_FE_CON_ACC] = "frontend tcp-request connection rule",
270 [SMP_CKP_FE_SES_ACC] = "frontend tcp-request session rule",
271 [SMP_CKP_FE_REQ_CNT] = "frontend tcp-request content rule",
272 [SMP_CKP_FE_HRQ_HDR] = "frontend http-request header rule",
273 [SMP_CKP_FE_HRQ_BDY] = "frontend http-request body rule",
274 [SMP_CKP_FE_SET_BCK] = "frontend use-backend rule",
275 [SMP_CKP_BE_REQ_CNT] = "backend tcp-request content rule",
276 [SMP_CKP_BE_HRQ_HDR] = "backend http-request header rule",
277 [SMP_CKP_BE_HRQ_BDY] = "backend http-request body rule",
278 [SMP_CKP_BE_SET_SRV] = "backend use-server, balance or stick-match rule",
279 [SMP_CKP_BE_SRV_CON] = "server source selection",
280 [SMP_CKP_BE_RES_CNT] = "backend tcp-response content rule",
281 [SMP_CKP_BE_HRS_HDR] = "backend http-response header rule",
282 [SMP_CKP_BE_HRS_BDY] = "backend http-response body rule",
283 [SMP_CKP_BE_STO_RUL] = "backend stick-store rule",
284 [SMP_CKP_FE_RES_CNT] = "frontend tcp-response content rule",
285 [SMP_CKP_FE_HRS_HDR] = "frontend http-response header rule",
286 [SMP_CKP_FE_HRS_BDY] = "frontend http-response body rule",
287 [SMP_CKP_FE_LOG_END] = "logs",
288};
289
Thierry FOURNIER348971e2013-11-21 10:50:10 +0100290/* This function returns the type of the data returned by the sample_expr.
291 * It assumes that the <expr> and all of its converters are properly
292 * initialized.
293 */
294inline
295int smp_expr_output_type(struct sample_expr *expr)
296{
297 struct sample_conv_expr *smp_expr;
298
299 if (!LIST_ISEMPTY(&expr->conv_exprs)) {
300 smp_expr = LIST_PREV(&expr->conv_exprs, struct sample_conv_expr *, list);
301 return smp_expr->conv->out_type;
302 }
303 return expr->fetch->out_type;
304}
305
306
Willy Tarreau80aca902013-01-07 15:42:20 +0100307/* fill the trash with a comma-delimited list of source names for the <use> bit
308 * field which must be composed of a non-null set of SMP_USE_* flags. The return
309 * value is the pointer to the string in the trash buffer.
310 */
311const char *sample_src_names(unsigned int use)
312{
313 int bit;
314
315 trash.len = 0;
316 trash.str[0] = '\0';
317 for (bit = 0; bit < SMP_SRC_ENTRIES; bit++) {
318 if (!(use & ~((1 << bit) - 1)))
319 break; /* no more bits */
320
321 if (!(use & (1 << bit)))
322 continue; /* bit not set */
323
324 trash.len += snprintf(trash.str + trash.len, trash.size - trash.len, "%s%s",
325 (use & ((1 << bit) - 1)) ? "," : "",
326 fetch_src_names[bit]);
327 }
328 return trash.str;
329}
330
Willy Tarreaubf8e2512013-03-25 14:52:41 +0100331/* return a pointer to the correct sample checkpoint name, or "unknown" when
332 * the flags are invalid. Only the lowest bit is used, higher bits are ignored
333 * if set.
334 */
335const char *sample_ckp_names(unsigned int use)
336{
337 int bit;
338
339 for (bit = 0; bit < SMP_CKP_ENTRIES; bit++)
340 if (use & (1 << bit))
341 return fetch_ckp_names[bit];
342 return "unknown sample check place, please report this bug";
343}
344
Emeric Brun107ca302010-01-04 16:16:05 +0100345/*
Willy Tarreau80aca902013-01-07 15:42:20 +0100346 * Registers the sample fetch keyword list <kwl> as a list of valid keywords
347 * for next parsing sessions. The fetch keywords capabilities are also computed
348 * from their ->use field.
Emeric Brun107ca302010-01-04 16:16:05 +0100349 */
Willy Tarreau80aca902013-01-07 15:42:20 +0100350void sample_register_fetches(struct sample_fetch_kw_list *kwl)
Emeric Brun107ca302010-01-04 16:16:05 +0100351{
Willy Tarreau80aca902013-01-07 15:42:20 +0100352 struct sample_fetch *sf;
353 int bit;
354
355 for (sf = kwl->kw; sf->kw != NULL; sf++) {
356 for (bit = 0; bit < SMP_SRC_ENTRIES; bit++)
357 if (sf->use & (1 << bit))
358 sf->val |= fetch_cap[bit];
359 }
360 LIST_ADDQ(&sample_fetches.list, &kwl->list);
Emeric Brun107ca302010-01-04 16:16:05 +0100361}
362
363/*
Willy Tarreau12785782012-04-27 21:37:17 +0200364 * Registers the sample format coverstion keyword list <pckl> as a list of valid keywords for next
Emeric Brun107ca302010-01-04 16:16:05 +0100365 * parsing sessions.
366 */
Willy Tarreau12785782012-04-27 21:37:17 +0200367void sample_register_convs(struct sample_conv_kw_list *pckl)
Emeric Brun107ca302010-01-04 16:16:05 +0100368{
Willy Tarreau12785782012-04-27 21:37:17 +0200369 LIST_ADDQ(&sample_convs.list, &pckl->list);
Emeric Brun107ca302010-01-04 16:16:05 +0100370}
371
372/*
Willy Tarreau12785782012-04-27 21:37:17 +0200373 * Returns the pointer on sample fetch keyword structure identified by
Emeric Brun107ca302010-01-04 16:16:05 +0100374 * string of <len> in buffer <kw>.
375 *
376 */
Willy Tarreau12785782012-04-27 21:37:17 +0200377struct sample_fetch *find_sample_fetch(const char *kw, int len)
Emeric Brun107ca302010-01-04 16:16:05 +0100378{
379 int index;
Willy Tarreau12785782012-04-27 21:37:17 +0200380 struct sample_fetch_kw_list *kwl;
Emeric Brun107ca302010-01-04 16:16:05 +0100381
Willy Tarreau12785782012-04-27 21:37:17 +0200382 list_for_each_entry(kwl, &sample_fetches.list, list) {
Emeric Brun107ca302010-01-04 16:16:05 +0100383 for (index = 0; kwl->kw[index].kw != NULL; index++) {
384 if (strncmp(kwl->kw[index].kw, kw, len) == 0 &&
385 kwl->kw[index].kw[len] == '\0')
386 return &kwl->kw[index];
387 }
388 }
389 return NULL;
390}
391
Thierry FOURNIER4d9a1d12014-12-08 14:49:19 +0100392/* This fucntion browse the list of available saple fetch. <current> is
393 * the last used sample fetch. If it is the first call, it must set to NULL.
394 * <idx> is the index of the next sampleèfetch entry. It is used as private
395 * value. It is useles to initiate it.
396 *
397 * It returns always the newt fetch_sample entry, and NULL when the end of
398 * the list is reached.
399 */
400struct sample_fetch *sample_fetch_getnext(struct sample_fetch *current, int *idx)
401{
402 struct sample_fetch_kw_list *kwl;
403 struct sample_fetch *base;
404
405 if (!current) {
406 /* Get first kwl entry. */
407 kwl = LIST_NEXT(&sample_fetches.list, struct sample_fetch_kw_list *, list);
408 (*idx) = 0;
409 } else {
410 /* Get kwl corresponding to the curret entry. */
411 base = current + 1 - (*idx);
412 kwl = container_of(base, struct sample_fetch_kw_list, kw);
413 }
414
415 while (1) {
416
417 /* Check if kwl is the last entry. */
418 if (&kwl->list == &sample_fetches.list)
419 return NULL;
420
421 /* idx contain the next keyword. If it is available, return it. */
422 if (kwl->kw[*idx].kw) {
423 (*idx)++;
424 return &kwl->kw[(*idx)-1];
425 }
426
427 /* get next entry in the main list, and return NULL if the end is reached. */
428 kwl = LIST_NEXT(&kwl->list, struct sample_fetch_kw_list *, list);
429
430 /* Set index to 0, ans do one other loop. */
431 (*idx) = 0;
432 }
433}
434
Thierry FOURNIER8fd13762015-03-10 23:56:48 +0100435/* This function browses the list of available converters. <current> is
436 * the last used converter. If it is the first call, it must set to NULL.
437 * <idx> is the index of the next converter entry. It is used as private
438 * value. It is useless to initiate it.
439 *
440 * It returns always the next sample_conv entry, and NULL when the end of
441 * the list is reached.
442 */
443struct sample_conv *sample_conv_getnext(struct sample_conv *current, int *idx)
444{
445 struct sample_conv_kw_list *kwl;
446 struct sample_conv *base;
447
448 if (!current) {
449 /* Get first kwl entry. */
450 kwl = LIST_NEXT(&sample_convs.list, struct sample_conv_kw_list *, list);
451 (*idx) = 0;
452 } else {
453 /* Get kwl corresponding to the curret entry. */
454 base = current + 1 - (*idx);
455 kwl = container_of(base, struct sample_conv_kw_list, kw);
456 }
457
458 while (1) {
459 /* Check if kwl is the last entry. */
460 if (&kwl->list == &sample_convs.list)
461 return NULL;
462
463 /* idx contain the next keyword. If it is available, return it. */
464 if (kwl->kw[*idx].kw) {
465 (*idx)++;
466 return &kwl->kw[(*idx)-1];
467 }
468
469 /* get next entry in the main list, and return NULL if the end is reached. */
470 kwl = LIST_NEXT(&kwl->list, struct sample_conv_kw_list *, list);
471
472 /* Set index to 0, ans do one other loop. */
473 (*idx) = 0;
474 }
475}
476
Emeric Brun107ca302010-01-04 16:16:05 +0100477/*
Willy Tarreau12785782012-04-27 21:37:17 +0200478 * Returns the pointer on sample format conversion keyword structure identified by
Emeric Brun107ca302010-01-04 16:16:05 +0100479 * string of <len> in buffer <kw>.
480 *
481 */
Willy Tarreau12785782012-04-27 21:37:17 +0200482struct sample_conv *find_sample_conv(const char *kw, int len)
Emeric Brun107ca302010-01-04 16:16:05 +0100483{
484 int index;
Willy Tarreau12785782012-04-27 21:37:17 +0200485 struct sample_conv_kw_list *kwl;
Emeric Brun107ca302010-01-04 16:16:05 +0100486
Willy Tarreau12785782012-04-27 21:37:17 +0200487 list_for_each_entry(kwl, &sample_convs.list, list) {
Emeric Brun107ca302010-01-04 16:16:05 +0100488 for (index = 0; kwl->kw[index].kw != NULL; index++) {
489 if (strncmp(kwl->kw[index].kw, kw, len) == 0 &&
490 kwl->kw[index].kw[len] == '\0')
491 return &kwl->kw[index];
492 }
493 }
494 return NULL;
495}
496
Emeric Brun107ca302010-01-04 16:16:05 +0100497/******************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +0200498/* Sample casts functions */
Willy Tarreaub8c8f1f2012-04-23 22:38:26 +0200499/* Note: these functions do *NOT* set the output type on the */
500/* sample, the caller is responsible for doing this on return. */
Emeric Brun107ca302010-01-04 16:16:05 +0100501/******************************************************************/
502
Willy Tarreau342acb42012-04-23 22:03:39 +0200503static int c_ip2int(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100504{
Willy Tarreau342acb42012-04-23 22:03:39 +0200505 smp->data.uint = ntohl(smp->data.ipv4.s_addr);
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100506 smp->type = SMP_T_UINT;
Emeric Brun107ca302010-01-04 16:16:05 +0100507 return 1;
508}
509
Willy Tarreau342acb42012-04-23 22:03:39 +0200510static int c_ip2str(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100511{
Willy Tarreau47ca5452012-12-23 20:22:19 +0100512 struct chunk *trash = get_trash_chunk();
Emeric Brun107ca302010-01-04 16:16:05 +0100513
Willy Tarreau342acb42012-04-23 22:03:39 +0200514 if (!inet_ntop(AF_INET, (void *)&smp->data.ipv4, trash->str, trash->size))
Emeric Brun107ca302010-01-04 16:16:05 +0100515 return 0;
516
517 trash->len = strlen(trash->str);
Willy Tarreau342acb42012-04-23 22:03:39 +0200518 smp->data.str = *trash;
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100519 smp->type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100520 smp->flags &= ~SMP_F_CONST;
Emeric Brun107ca302010-01-04 16:16:05 +0100521
522 return 1;
523}
524
Willy Tarreau342acb42012-04-23 22:03:39 +0200525static int c_ip2ipv6(struct sample *smp)
David du Colombier4f92d322011-03-24 11:09:31 +0100526{
Willy Tarreau342acb42012-04-23 22:03:39 +0200527 v4tov6(&smp->data.ipv6, &smp->data.ipv4);
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100528 smp->type = SMP_T_IPV6;
David du Colombier4f92d322011-03-24 11:09:31 +0100529 return 1;
530}
531
Willy Tarreau342acb42012-04-23 22:03:39 +0200532static int c_ipv62str(struct sample *smp)
David du Colombier4f92d322011-03-24 11:09:31 +0100533{
Willy Tarreau47ca5452012-12-23 20:22:19 +0100534 struct chunk *trash = get_trash_chunk();
David du Colombier4f92d322011-03-24 11:09:31 +0100535
Willy Tarreau342acb42012-04-23 22:03:39 +0200536 if (!inet_ntop(AF_INET6, (void *)&smp->data.ipv6, trash->str, trash->size))
David du Colombier4f92d322011-03-24 11:09:31 +0100537 return 0;
538
539 trash->len = strlen(trash->str);
Willy Tarreau342acb42012-04-23 22:03:39 +0200540 smp->data.str = *trash;
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100541 smp->type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100542 smp->flags &= ~SMP_F_CONST;
David du Colombier4f92d322011-03-24 11:09:31 +0100543 return 1;
544}
545
546/*
Willy Tarreau342acb42012-04-23 22:03:39 +0200547static int c_ipv62ip(struct sample *smp)
David du Colombier4f92d322011-03-24 11:09:31 +0100548{
Willy Tarreau342acb42012-04-23 22:03:39 +0200549 return v6tov4(&smp->data.ipv4, &smp->data.ipv6);
David du Colombier4f92d322011-03-24 11:09:31 +0100550}
551*/
552
Willy Tarreau342acb42012-04-23 22:03:39 +0200553static int c_int2ip(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100554{
Willy Tarreau342acb42012-04-23 22:03:39 +0200555 smp->data.ipv4.s_addr = htonl(smp->data.uint);
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100556 smp->type = SMP_T_IPV4;
Emeric Brun107ca302010-01-04 16:16:05 +0100557 return 1;
558}
559
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100560static int c_str2addr(struct sample *smp)
561{
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100562 if (!buf2ip(smp->data.str.str, smp->data.str.len, &smp->data.ipv4)) {
Thierry FOURNIERfd139902013-12-11 12:38:57 +0100563 if (!buf2ip6(smp->data.str.str, smp->data.str.len, &smp->data.ipv6))
564 return 0;
565 smp->type = SMP_T_IPV6;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100566 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIERfd139902013-12-11 12:38:57 +0100567 return 1;
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100568 }
569 smp->type = SMP_T_IPV4;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100570 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100571 return 1;
572}
573
Willy Tarreau342acb42012-04-23 22:03:39 +0200574static int c_str2ip(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100575{
Willy Tarreau342acb42012-04-23 22:03:39 +0200576 if (!buf2ip(smp->data.str.str, smp->data.str.len, &smp->data.ipv4))
Emeric Brun107ca302010-01-04 16:16:05 +0100577 return 0;
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100578 smp->type = SMP_T_IPV4;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100579 smp->flags &= ~SMP_F_CONST;
Emeric Brun107ca302010-01-04 16:16:05 +0100580 return 1;
581}
582
Willy Tarreau342acb42012-04-23 22:03:39 +0200583static int c_str2ipv6(struct sample *smp)
David du Colombier4f92d322011-03-24 11:09:31 +0100584{
Thierry FOURNIERfd139902013-12-11 12:38:57 +0100585 if (!buf2ip6(smp->data.str.str, smp->data.str.len, &smp->data.ipv6))
586 return 0;
587 smp->type = SMP_T_IPV6;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100588 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIERfd139902013-12-11 12:38:57 +0100589 return 1;
David du Colombier4f92d322011-03-24 11:09:31 +0100590}
591
Emeric Brun4b9e8022014-11-03 18:17:10 +0100592/*
593 * The NULL char always enforces the end of string if it is met.
594 * Data is never changed, so we can ignore the CONST case
Thierry FOURNIERe87cac12014-03-12 15:07:59 +0100595 */
Emeric Brun8ac33d92012-10-17 13:36:06 +0200596static int c_bin2str(struct sample *smp)
597{
Emeric Brun4b9e8022014-11-03 18:17:10 +0100598 int i;
Emeric Brun8ac33d92012-10-17 13:36:06 +0200599
Emeric Brun4b9e8022014-11-03 18:17:10 +0100600 for (i = 0; i < smp->data.str.len; i++) {
601 if (!smp->data.str.str[i]) {
602 smp->data.str.len = i;
Thierry FOURNIERe87cac12014-03-12 15:07:59 +0100603 break;
Emeric Brun4b9e8022014-11-03 18:17:10 +0100604 }
Emeric Brun8ac33d92012-10-17 13:36:06 +0200605 }
Emeric Brun8ac33d92012-10-17 13:36:06 +0200606 return 1;
607}
608
Willy Tarreau342acb42012-04-23 22:03:39 +0200609static int c_int2str(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100610{
Willy Tarreau47ca5452012-12-23 20:22:19 +0100611 struct chunk *trash = get_trash_chunk();
Emeric Brun107ca302010-01-04 16:16:05 +0100612 char *pos;
613
Willy Tarreau342acb42012-04-23 22:03:39 +0200614 pos = ultoa_r(smp->data.uint, trash->str, trash->size);
Emeric Brun107ca302010-01-04 16:16:05 +0100615
616 if (!pos)
617 return 0;
618
Emeric Brun485479d2010-09-23 18:02:19 +0200619 trash->size = trash->size - (pos - trash->str);
Emeric Brun107ca302010-01-04 16:16:05 +0100620 trash->str = pos;
621 trash->len = strlen(pos);
Willy Tarreau342acb42012-04-23 22:03:39 +0200622 smp->data.str = *trash;
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100623 smp->type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100624 smp->flags &= ~SMP_F_CONST;
Emeric Brun107ca302010-01-04 16:16:05 +0100625 return 1;
626}
627
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100628/* This function duplicates data and removes the flag "const". */
629int smp_dup(struct sample *smp)
Emeric Brun485479d2010-09-23 18:02:19 +0200630{
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100631 struct chunk *trash;
Emeric Brun485479d2010-09-23 18:02:19 +0200632
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100633 /* If the const flag is not set, we don't need to duplicate the
634 * pattern as it can be modified in place.
635 */
636 if (!(smp->flags & SMP_F_CONST))
637 return 1;
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100638
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100639 switch (smp->type) {
640 case SMP_T_BOOL:
641 case SMP_T_UINT:
642 case SMP_T_SINT:
643 case SMP_T_ADDR:
644 case SMP_T_IPV4:
645 case SMP_T_IPV6:
646 /* These type are not const. */
647 break;
648 case SMP_T_STR:
649 case SMP_T_BIN:
650 /* Duplicate data. */
651 trash = get_trash_chunk();
652 trash->len = smp->data.str.len < trash->size ? smp->data.str.len : trash->size;
653 memcpy(trash->str, smp->data.str.str, trash->len);
654 smp->data.str = *trash;
655 break;
656 default:
657 /* Other cases are unexpected. */
658 return 0;
659 }
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100660
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100661 /* remove const flag */
662 smp->flags &= ~SMP_F_CONST;
Emeric Brun485479d2010-09-23 18:02:19 +0200663 return 1;
664}
665
Thierry FOURNIER0e9af552013-12-14 14:55:04 +0100666int c_none(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100667{
668 return 1;
669}
670
Willy Tarreau342acb42012-04-23 22:03:39 +0200671static int c_str2int(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100672{
673 int i;
674 uint32_t ret = 0;
675
Thierry FOURNIER60bb0202014-01-27 18:20:48 +0100676 if (smp->data.str.len == 0)
677 return 0;
678
Willy Tarreau342acb42012-04-23 22:03:39 +0200679 for (i = 0; i < smp->data.str.len; i++) {
680 uint32_t val = smp->data.str.str[i] - '0';
Emeric Brun107ca302010-01-04 16:16:05 +0100681
Thierry FOURNIER60bb0202014-01-27 18:20:48 +0100682 if (val > 9) {
683 if (i == 0)
684 return 0;
Emeric Brun107ca302010-01-04 16:16:05 +0100685 break;
Thierry FOURNIER60bb0202014-01-27 18:20:48 +0100686 }
Emeric Brun107ca302010-01-04 16:16:05 +0100687
688 ret = ret * 10 + val;
689 }
690
Willy Tarreau342acb42012-04-23 22:03:39 +0200691 smp->data.uint = ret;
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100692 smp->type = SMP_T_UINT;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100693 smp->flags &= ~SMP_F_CONST;
Emeric Brun107ca302010-01-04 16:16:05 +0100694 return 1;
695}
696
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100697static int c_str2meth(struct sample *smp)
698{
699 enum http_meth_t meth;
700 int len;
701
702 meth = find_http_meth(smp->data.str.str, smp->data.str.len);
703 if (meth == HTTP_METH_OTHER) {
704 len = smp->data.str.len;
705 smp->data.meth.str.str = smp->data.str.str;
706 smp->data.meth.str.len = len;
707 }
708 else
709 smp->flags &= ~SMP_F_CONST;
710 smp->data.meth.meth = meth;
711 smp->type = SMP_T_METH;
712 return 1;
713}
714
715static int c_meth2str(struct sample *smp)
716{
717 int len;
718 enum http_meth_t meth;
719
720 if (smp->data.meth.meth == HTTP_METH_OTHER) {
721 /* The method is unknown. Copy the original pointer. */
722 len = smp->data.meth.str.len;
723 smp->data.str.str = smp->data.meth.str.str;
724 smp->data.str.len = len;
725 smp->type = SMP_T_STR;
726 }
727 else if (smp->data.meth.meth < HTTP_METH_OTHER) {
728 /* The method is known, copy the pointer containing the string. */
729 meth = smp->data.meth.meth;
730 smp->data.str.str = http_known_methods[meth].name;
731 smp->data.str.len = http_known_methods[meth].len;
732 smp->flags |= SMP_F_CONST;
733 smp->type = SMP_T_STR;
734 }
735 else {
736 /* Unknown method */
737 return 0;
738 }
739 return 1;
740}
741
Willy Tarreau9700e5c2014-07-15 21:03:26 +0200742static int c_addr2bin(struct sample *smp)
743{
744 struct chunk *chk = get_trash_chunk();
745
746 if (smp->type == SMP_T_IPV4) {
747 chk->len = 4;
748 memcpy(chk->str, &smp->data.ipv4, chk->len);
749 }
750 else if (smp->type == SMP_T_IPV6) {
751 chk->len = 16;
752 memcpy(chk->str, &smp->data.ipv6, chk->len);
753 }
754 else
755 return 0;
756
757 smp->data.str = *chk;
758 smp->type = SMP_T_BIN;
759 return 1;
760}
761
Willy Tarreaubbfd1a22014-07-15 21:19:08 +0200762static int c_int2bin(struct sample *smp)
763{
764 struct chunk *chk = get_trash_chunk();
765
766 *(unsigned int *)chk->str = htonl(smp->data.uint);
767 chk->len = 4;
768
769 smp->data.str = *chk;
770 smp->type = SMP_T_BIN;
771 return 1;
772}
773
Willy Tarreau9700e5c2014-07-15 21:03:26 +0200774
Emeric Brun107ca302010-01-04 16:16:05 +0100775/*****************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +0200776/* Sample casts matrix: */
777/* sample_casts[from type][to type] */
778/* NULL pointer used for impossible sample casts */
Emeric Brun107ca302010-01-04 16:16:05 +0100779/*****************************************************************/
Emeric Brun107ca302010-01-04 16:16:05 +0100780
Thierry FOURNIER8af6ff12013-11-21 10:53:12 +0100781sample_cast_fct sample_casts[SMP_TYPES][SMP_TYPES] = {
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100782/* to: BOOL UINT SINT ADDR IPV4 IPV6 STR BIN METH */
783/* from: BOOL */ { c_none, c_none, c_none, NULL, NULL, NULL, c_int2str, NULL, NULL, },
Willy Tarreaubbfd1a22014-07-15 21:19:08 +0200784/* UINT */ { c_none, c_none, c_none, c_int2ip, c_int2ip, NULL, c_int2str, c_int2bin, NULL, },
785/* SINT */ { c_none, c_none, c_none, c_int2ip, c_int2ip, NULL, c_int2str, c_int2bin, NULL, },
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100786/* ADDR */ { NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, },
Willy Tarreau9700e5c2014-07-15 21:03:26 +0200787/* IPV4 */ { NULL, c_ip2int, c_ip2int, c_none, c_none, c_ip2ipv6, c_ip2str, c_addr2bin, NULL, },
788/* IPV6 */ { NULL, NULL, NULL, c_none, NULL, c_none, c_ipv62str, c_addr2bin, NULL, },
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100789/* STR */ { c_str2int, c_str2int, c_str2int, c_str2addr, c_str2ip, c_str2ipv6, c_none, c_none, c_str2meth, },
790/* BIN */ { NULL, NULL, NULL, NULL, NULL, NULL, c_bin2str, c_none, c_str2meth, },
791/* METH */ { NULL, NULL, NULL, NULL, NULL, NULL, c_meth2str, c_meth2str, c_none, },
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200792};
Emeric Brun107ca302010-01-04 16:16:05 +0100793
Emeric Brun107ca302010-01-04 16:16:05 +0100794/*
Willy Tarreau12785782012-04-27 21:37:17 +0200795 * Parse a sample expression configuration:
Emeric Brun107ca302010-01-04 16:16:05 +0100796 * fetch keyword followed by format conversion keywords.
Willy Tarreau12785782012-04-27 21:37:17 +0200797 * Returns a pointer on allocated sample expression structure.
Willy Tarreaua4312fa2013-04-02 16:34:32 +0200798 * The caller must have set al->ctx.
Emeric Brun107ca302010-01-04 16:16:05 +0100799 */
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +0100800struct 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 +0100801{
Willy Tarreau833cc792013-07-24 15:34:19 +0200802 const char *begw; /* beginning of word */
803 const char *endw; /* end of word */
804 const char *endt; /* end of term */
Willy Tarreau12785782012-04-27 21:37:17 +0200805 struct sample_expr *expr;
806 struct sample_fetch *fetch;
807 struct sample_conv *conv;
Emeric Brun107ca302010-01-04 16:16:05 +0100808 unsigned long prev_type;
Willy Tarreau833cc792013-07-24 15:34:19 +0200809 char *fkw = NULL;
810 char *ckw = NULL;
Willy Tarreau689a1df2013-12-13 00:40:11 +0100811 int err_arg;
Emeric Brun107ca302010-01-04 16:16:05 +0100812
Willy Tarreau833cc792013-07-24 15:34:19 +0200813 begw = str[*idx];
814 for (endw = begw; *endw && *endw != '(' && *endw != ','; endw++);
815
816 if (endw == begw) {
Willy Tarreau975c1782013-12-12 23:16:54 +0100817 memprintf(err_msg, "missing fetch method");
Emeric Brun107ca302010-01-04 16:16:05 +0100818 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200819 }
Emeric Brun107ca302010-01-04 16:16:05 +0100820
Willy Tarreau833cc792013-07-24 15:34:19 +0200821 /* keep a copy of the current fetch keyword for error reporting */
822 fkw = my_strndup(begw, endw - begw);
Emeric Brun107ca302010-01-04 16:16:05 +0100823
Willy Tarreau833cc792013-07-24 15:34:19 +0200824 fetch = find_sample_fetch(begw, endw - begw);
825 if (!fetch) {
Willy Tarreau975c1782013-12-12 23:16:54 +0100826 memprintf(err_msg, "unknown fetch method '%s'", fkw);
Emeric Brun107ca302010-01-04 16:16:05 +0100827 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200828 }
Emeric Brun107ca302010-01-04 16:16:05 +0100829
Willy Tarreau833cc792013-07-24 15:34:19 +0200830 endt = endw;
831 if (*endt == '(') {
Willy Tarreau689a1df2013-12-13 00:40:11 +0100832 /* look for the end of this term and skip the opening parenthesis */
833 endt = ++endw;
Willy Tarreau833cc792013-07-24 15:34:19 +0200834 while (*endt && *endt != ')')
835 endt++;
836 if (*endt != ')') {
Willy Tarreau975c1782013-12-12 23:16:54 +0100837 memprintf(err_msg, "missing closing ')' after arguments to fetch keyword '%s'", fkw);
Willy Tarreau833cc792013-07-24 15:34:19 +0200838 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200839 }
Emeric Brun485479d2010-09-23 18:02:19 +0200840 }
Emeric Brun107ca302010-01-04 16:16:05 +0100841
Willy Tarreau833cc792013-07-24 15:34:19 +0200842 /* At this point, we have :
843 * - begw : beginning of the keyword
Willy Tarreau689a1df2013-12-13 00:40:11 +0100844 * - endw : end of the keyword, first character not part of keyword
845 * nor the opening parenthesis (so first character of args
846 * if present).
Willy Tarreau833cc792013-07-24 15:34:19 +0200847 * - endt : end of the term (=endw or last parenthesis if args are present)
848 */
849
850 if (fetch->out_type >= SMP_TYPES) {
Willy Tarreau975c1782013-12-12 23:16:54 +0100851 memprintf(err_msg, "returns type of fetch method '%s' is unknown", fkw);
Emeric Brun107ca302010-01-04 16:16:05 +0100852 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200853 }
Emeric Brun107ca302010-01-04 16:16:05 +0100854 prev_type = fetch->out_type;
Willy Tarreau833cc792013-07-24 15:34:19 +0200855
Willy Tarreau12785782012-04-27 21:37:17 +0200856 expr = calloc(1, sizeof(struct sample_expr));
Emeric Brun485479d2010-09-23 18:02:19 +0200857 if (!expr)
858 goto out_error;
Emeric Brun107ca302010-01-04 16:16:05 +0100859
860 LIST_INIT(&(expr->conv_exprs));
861 expr->fetch = fetch;
Willy Tarreau2e845be2012-10-19 19:49:09 +0200862 expr->arg_p = empty_arg_list;
Emeric Brun107ca302010-01-04 16:16:05 +0100863
Willy Tarreau689a1df2013-12-13 00:40:11 +0100864 /* Note that we call the argument parser even with an empty string,
865 * this allows it to automatically create entries for mandatory
866 * implicit arguments (eg: local proxy name).
867 */
868 al->kw = expr->fetch->kw;
869 al->conv = NULL;
870 if (make_arg_list(endw, endt - endw, fetch->arg_mask, &expr->arg_p, err_msg, NULL, &err_arg, al) < 0) {
871 memprintf(err_msg, "fetch method '%s' : %s", fkw, *err_msg);
872 goto out_error;
873 }
Willy Tarreau2e845be2012-10-19 19:49:09 +0200874
Willy Tarreau689a1df2013-12-13 00:40:11 +0100875 if (!expr->arg_p) {
876 expr->arg_p = empty_arg_list;
Emeric Brun485479d2010-09-23 18:02:19 +0200877 }
Willy Tarreau689a1df2013-12-13 00:40:11 +0100878 else if (fetch->val_args && !fetch->val_args(expr->arg_p, err_msg)) {
879 memprintf(err_msg, "invalid args in fetch method '%s' : %s", fkw, *err_msg);
Emeric Brun485479d2010-09-23 18:02:19 +0200880 goto out_error;
Emeric Brun107ca302010-01-04 16:16:05 +0100881 }
882
Willy Tarreau833cc792013-07-24 15:34:19 +0200883 /* Now process the converters if any. We have two supported syntaxes
884 * for the converters, which can be combined :
885 * - comma-delimited list of converters just after the keyword and args ;
886 * - one converter per keyword
887 * The combination allows to have each keyword being a comma-delimited
888 * series of converters.
889 *
890 * We want to process the former first, then the latter. For this we start
891 * from the beginning of the supposed place in the exiting conv chain, which
892 * starts at the last comma (endt).
893 */
894
895 while (1) {
Willy Tarreau12785782012-04-27 21:37:17 +0200896 struct sample_conv_expr *conv_expr;
Emeric Brun107ca302010-01-04 16:16:05 +0100897
Willy Tarreau833cc792013-07-24 15:34:19 +0200898 if (*endt == ')') /* skip last closing parenthesis */
899 endt++;
Emeric Brun107ca302010-01-04 16:16:05 +0100900
Willy Tarreau833cc792013-07-24 15:34:19 +0200901 if (*endt && *endt != ',') {
902 if (ckw)
Willy Tarreau975c1782013-12-12 23:16:54 +0100903 memprintf(err_msg, "missing comma after conv keyword '%s'", ckw);
Willy Tarreau833cc792013-07-24 15:34:19 +0200904 else
Willy Tarreau975c1782013-12-12 23:16:54 +0100905 memprintf(err_msg, "missing comma after fetch keyword '%s'", fkw);
Emeric Brun107ca302010-01-04 16:16:05 +0100906 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200907 }
Emeric Brun107ca302010-01-04 16:16:05 +0100908
Willy Tarreau833cc792013-07-24 15:34:19 +0200909 while (*endt == ',') /* then trailing commas */
910 endt++;
911
912 begw = endt; /* start of conv keyword */
913
914 if (!*begw) {
915 /* none ? skip to next string */
916 (*idx)++;
917 begw = str[*idx];
918 if (!begw || !*begw)
919 break;
920 }
921
922 for (endw = begw; *endw && *endw != '(' && *endw != ','; endw++);
923
924 free(ckw);
925 ckw = my_strndup(begw, endw - begw);
926
927 conv = find_sample_conv(begw, endw - begw);
928 if (!conv) {
929 /* we found an isolated keyword that we don't know, it's not ours */
930 if (begw == str[*idx])
931 break;
Willy Tarreau975c1782013-12-12 23:16:54 +0100932 memprintf(err_msg, "unknown conv method '%s'", ckw);
Willy Tarreau833cc792013-07-24 15:34:19 +0200933 goto out_error;
934 }
Emeric Brun107ca302010-01-04 16:16:05 +0100935
Willy Tarreau833cc792013-07-24 15:34:19 +0200936 endt = endw;
937 if (*endt == '(') {
938 /* look for the end of this term */
939 while (*endt && *endt != ')')
940 endt++;
941 if (*endt != ')') {
Willy Tarreau975c1782013-12-12 23:16:54 +0100942 memprintf(err_msg, "syntax error: missing ')' after conv keyword '%s'", ckw);
Willy Tarreau833cc792013-07-24 15:34:19 +0200943 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200944 }
Willy Tarreau833cc792013-07-24 15:34:19 +0200945 }
946
947 if (conv->in_type >= SMP_TYPES || conv->out_type >= SMP_TYPES) {
Willy Tarreau975c1782013-12-12 23:16:54 +0100948 memprintf(err_msg, "returns type of conv method '%s' is unknown", ckw);
Emeric Brun107ca302010-01-04 16:16:05 +0100949 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200950 }
Emeric Brun107ca302010-01-04 16:16:05 +0100951
952 /* If impossible type conversion */
Willy Tarreau12785782012-04-27 21:37:17 +0200953 if (!sample_casts[prev_type][conv->in_type]) {
Willy Tarreau975c1782013-12-12 23:16:54 +0100954 memprintf(err_msg, "conv method '%s' cannot be applied", ckw);
Emeric Brun107ca302010-01-04 16:16:05 +0100955 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200956 }
Emeric Brun107ca302010-01-04 16:16:05 +0100957
958 prev_type = conv->out_type;
Willy Tarreau12785782012-04-27 21:37:17 +0200959 conv_expr = calloc(1, sizeof(struct sample_conv_expr));
Emeric Brun485479d2010-09-23 18:02:19 +0200960 if (!conv_expr)
961 goto out_error;
Emeric Brun107ca302010-01-04 16:16:05 +0100962
963 LIST_ADDQ(&(expr->conv_exprs), &(conv_expr->list));
964 conv_expr->conv = conv;
965
Willy Tarreau833cc792013-07-24 15:34:19 +0200966 if (endt != endw) {
Willy Tarreaub27c0d32012-04-20 16:04:47 +0200967 int err_arg;
Willy Tarreau21d68a62012-04-20 15:52:36 +0200968
Willy Tarreau9fcb9842012-04-20 14:45:49 +0200969 if (!conv->arg_mask) {
Willy Tarreau975c1782013-12-12 23:16:54 +0100970 memprintf(err_msg, "conv method '%s' does not support any args", ckw);
Emeric Brun485479d2010-09-23 18:02:19 +0200971 goto out_error;
972 }
Willy Tarreau9e92d322010-01-26 17:58:06 +0100973
Willy Tarreaua4312fa2013-04-02 16:34:32 +0200974 al->kw = expr->fetch->kw;
975 al->conv = conv_expr->conv->kw;
Willy Tarreau975c1782013-12-12 23:16:54 +0100976 if (make_arg_list(endw + 1, endt - endw - 1, conv->arg_mask, &conv_expr->arg_p, err_msg, NULL, &err_arg, al) < 0) {
977 memprintf(err_msg, "invalid arg %d in conv method '%s' : %s", err_arg+1, ckw, *err_msg);
Willy Tarreau21d68a62012-04-20 15:52:36 +0200978 goto out_error;
979 }
980
Willy Tarreau2e845be2012-10-19 19:49:09 +0200981 if (!conv_expr->arg_p)
982 conv_expr->arg_p = empty_arg_list;
983
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +0100984 if (conv->val_args && !conv->val_args(conv_expr->arg_p, conv, file, line, err_msg)) {
Willy Tarreau975c1782013-12-12 23:16:54 +0100985 memprintf(err_msg, "invalid args in conv method '%s' : %s", ckw, *err_msg);
Emeric Brun485479d2010-09-23 18:02:19 +0200986 goto out_error;
987 }
988 }
Willy Tarreau833cc792013-07-24 15:34:19 +0200989 else if (ARGM(conv->arg_mask)) {
Willy Tarreau975c1782013-12-12 23:16:54 +0100990 memprintf(err_msg, "missing args for conv method '%s'", ckw);
Emeric Brun485479d2010-09-23 18:02:19 +0200991 goto out_error;
Emeric Brun107ca302010-01-04 16:16:05 +0100992 }
993 }
Emeric Brun485479d2010-09-23 18:02:19 +0200994
Willy Tarreau833cc792013-07-24 15:34:19 +0200995 out:
996 free(fkw);
997 free(ckw);
Emeric Brun107ca302010-01-04 16:16:05 +0100998 return expr;
999
1000out_error:
Willy Tarreau12785782012-04-27 21:37:17 +02001001 /* TODO: prune_sample_expr(expr); */
Willy Tarreau833cc792013-07-24 15:34:19 +02001002 expr = NULL;
1003 goto out;
Emeric Brun107ca302010-01-04 16:16:05 +01001004}
1005
1006/*
Willy Tarreau12785782012-04-27 21:37:17 +02001007 * Process a fetch + format conversion of defined by the sample expression <expr>
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001008 * on request or response considering the <opt> parameter.
Willy Tarreau12785782012-04-27 21:37:17 +02001009 * Returns a pointer on a typed sample structure containing the result or NULL if
1010 * sample is not found or when format conversion failed.
Emeric Brun107ca302010-01-04 16:16:05 +01001011 * If <p> is not null, function returns results in structure pointed by <p>.
Willy Tarreau12785782012-04-27 21:37:17 +02001012 * If <p> is null, functions returns a pointer on a static sample structure.
Willy Tarreaub8c8f1f2012-04-23 22:38:26 +02001013 *
1014 * Note: the fetch functions are required to properly set the return type. The
1015 * conversion functions must do so too. However the cast functions do not need
1016 * to since they're made to cast mutiple types according to what is required.
Willy Tarreau6bcb0a82014-07-30 08:56:35 +02001017 *
1018 * The caller may indicate in <opt> if it considers the result final or not.
1019 * The caller needs to check the SMP_F_MAY_CHANGE flag in p->flags to verify
1020 * if the result is stable or not, according to the following table :
1021 *
1022 * return MAY_CHANGE FINAL Meaning for the sample
1023 * NULL 0 * Not present and will never be (eg: header)
1024 * NULL 1 0 Not present yet, could change (eg: POST param)
1025 * NULL 1 1 Not present yet, will not change anymore
1026 * smp 0 * Present and will not change (eg: header)
1027 * smp 1 0 Present, may change (eg: request length)
1028 * smp 1 1 Present, last known value (eg: request length)
Emeric Brun107ca302010-01-04 16:16:05 +01001029 */
Willy Tarreau192252e2015-04-04 01:47:55 +02001030struct sample *sample_process(struct proxy *px, struct session *sess,
1031 struct stream *strm, unsigned int opt,
Willy Tarreau12785782012-04-27 21:37:17 +02001032 struct sample_expr *expr, struct sample *p)
Emeric Brun107ca302010-01-04 16:16:05 +01001033{
Willy Tarreau12785782012-04-27 21:37:17 +02001034 struct sample_conv_expr *conv_expr;
Emeric Brun107ca302010-01-04 16:16:05 +01001035
Willy Tarreau18387e22013-07-25 12:02:38 +02001036 if (p == NULL) {
Willy Tarreaub4a88f02012-04-23 21:35:11 +02001037 p = &temp_smp;
Willy Tarreau6c616e02014-06-25 16:56:41 +02001038 memset(p, 0, sizeof(*p));
Willy Tarreau18387e22013-07-25 12:02:38 +02001039 }
Emeric Brun107ca302010-01-04 16:16:05 +01001040
Thierry FOURNIER6879ad32015-05-11 11:54:58 +02001041 p->px = px;
1042 p->sess = sess;
1043 p->strm = strm;
Thierry FOURNIER1d33b882015-05-11 15:25:29 +02001044 p->opt = opt;
Thierry FOURNIER0786d052015-05-11 15:42:45 +02001045 if (!expr->fetch->process(expr->arg_p, p, expr->fetch->kw, expr->fetch->private))
Emeric Brun107ca302010-01-04 16:16:05 +01001046 return NULL;
1047
Emeric Brun107ca302010-01-04 16:16:05 +01001048 list_for_each_entry(conv_expr, &expr->conv_exprs, list) {
Willy Tarreau12e50112012-04-25 17:21:49 +02001049 /* we want to ensure that p->type can be casted into
1050 * conv_expr->conv->in_type. We have 3 possibilities :
1051 * - NULL => not castable.
1052 * - c_none => nothing to do (let's optimize it)
1053 * - other => apply cast and prepare to fail
1054 */
Willy Tarreau12785782012-04-27 21:37:17 +02001055 if (!sample_casts[p->type][conv_expr->conv->in_type])
Willy Tarreau12e50112012-04-25 17:21:49 +02001056 return NULL;
1057
Willy Tarreau12785782012-04-27 21:37:17 +02001058 if (sample_casts[p->type][conv_expr->conv->in_type] != c_none &&
1059 !sample_casts[p->type][conv_expr->conv->in_type](p))
Emeric Brun107ca302010-01-04 16:16:05 +01001060 return NULL;
1061
Willy Tarreau12e50112012-04-25 17:21:49 +02001062 /* OK cast succeeded */
1063
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001064 if (!conv_expr->conv->process(conv_expr->arg_p, p, conv_expr->conv->private))
Emeric Brun107ca302010-01-04 16:16:05 +01001065 return NULL;
Emeric Brun107ca302010-01-04 16:16:05 +01001066 }
1067 return p;
1068}
1069
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001070/*
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001071 * Resolve all remaining arguments in proxy <p>. Returns the number of
1072 * errors or 0 if everything is fine.
1073 */
1074int smp_resolve_args(struct proxy *p)
1075{
1076 struct arg_list *cur, *bak;
1077 const char *ctx, *where;
1078 const char *conv_ctx, *conv_pre, *conv_pos;
1079 struct userlist *ul;
Willy Tarreau46947782015-01-19 19:00:58 +01001080 struct my_regex *reg;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001081 struct arg *arg;
1082 int cfgerr = 0;
Willy Tarreau46947782015-01-19 19:00:58 +01001083 int rflags;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001084
1085 list_for_each_entry_safe(cur, bak, &p->conf.args.list, list) {
1086 struct proxy *px;
1087 struct server *srv;
1088 char *pname, *sname;
Willy Tarreau46947782015-01-19 19:00:58 +01001089 char *err;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001090
1091 arg = cur->arg;
1092
1093 /* prepare output messages */
1094 conv_pre = conv_pos = conv_ctx = "";
1095 if (cur->conv) {
1096 conv_ctx = cur->conv;
1097 conv_pre = "conversion keyword '";
1098 conv_pos = "' for ";
1099 }
1100
1101 where = "in";
1102 ctx = "sample fetch keyword";
1103 switch (cur->ctx) {
Willy Tarreau46947782015-01-19 19:00:58 +01001104 case ARGC_STK: where = "in stick rule in"; break;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001105 case ARGC_TRK: where = "in tracking rule in"; break;
1106 case ARGC_LOG: where = "in log-format string in"; break;
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +01001107 case ARGC_HRQ: where = "in http-request header format string in"; break;
1108 case ARGC_HRS: where = "in http-response header format string in"; break;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001109 case ARGC_UIF: where = "in unique-id-format string in"; break;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01001110 case ARGC_RDR: where = "in redirect format string in"; break;
Willy Tarreau3a4ac422014-06-13 16:17:14 +02001111 case ARGC_CAP: where = "in capture rule in"; break;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001112 case ARGC_ACL: ctx = "ACL keyword"; break;
1113 }
1114
1115 /* set a few default settings */
1116 px = p;
1117 pname = p->id;
1118
1119 switch (arg->type) {
1120 case ARGT_SRV:
1121 if (!arg->data.str.len) {
1122 Alert("parsing [%s:%d] : missing server name in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1123 cur->file, cur->line,
1124 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
1125 cfgerr++;
1126 continue;
1127 }
1128
1129 /* we support two formats : "bck/srv" and "srv" */
1130 sname = strrchr(arg->data.str.str, '/');
1131
1132 if (sname) {
1133 *sname++ = '\0';
1134 pname = arg->data.str.str;
1135
Willy Tarreau9e0bb102015-05-26 11:24:42 +02001136 px = proxy_be_by_name(pname);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001137 if (!px) {
1138 Alert("parsing [%s:%d] : unable to find proxy '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1139 cur->file, cur->line, pname,
1140 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
1141 cfgerr++;
1142 break;
1143 }
1144 }
1145 else
1146 sname = arg->data.str.str;
1147
1148 srv = findserver(px, sname);
1149 if (!srv) {
1150 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",
1151 cur->file, cur->line, sname, pname,
1152 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
1153 cfgerr++;
1154 break;
1155 }
1156
1157 free(arg->data.str.str);
1158 arg->data.str.str = NULL;
1159 arg->unresolved = 0;
1160 arg->data.srv = srv;
1161 break;
1162
1163 case ARGT_FE:
1164 if (arg->data.str.len) {
1165 pname = arg->data.str.str;
Willy Tarreau9e0bb102015-05-26 11:24:42 +02001166 px = proxy_fe_by_name(pname);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001167 }
1168
1169 if (!px) {
1170 Alert("parsing [%s:%d] : unable to find frontend '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1171 cur->file, cur->line, pname,
1172 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
1173 cfgerr++;
1174 break;
1175 }
1176
1177 if (!(px->cap & PR_CAP_FE)) {
1178 Alert("parsing [%s:%d] : proxy '%s', referenced in arg %d of %s%s%s%s '%s' %s proxy '%s', has not frontend capability.\n",
1179 cur->file, cur->line, pname,
1180 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
1181 cfgerr++;
1182 break;
1183 }
1184
1185 free(arg->data.str.str);
1186 arg->data.str.str = NULL;
1187 arg->unresolved = 0;
1188 arg->data.prx = px;
1189 break;
1190
1191 case ARGT_BE:
1192 if (arg->data.str.len) {
1193 pname = arg->data.str.str;
Willy Tarreau9e0bb102015-05-26 11:24:42 +02001194 px = proxy_be_by_name(pname);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001195 }
1196
1197 if (!px) {
1198 Alert("parsing [%s:%d] : unable to find backend '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1199 cur->file, cur->line, pname,
1200 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
1201 cfgerr++;
1202 break;
1203 }
1204
1205 if (!(px->cap & PR_CAP_BE)) {
1206 Alert("parsing [%s:%d] : proxy '%s', referenced in arg %d of %s%s%s%s '%s' %s proxy '%s', has not backend capability.\n",
1207 cur->file, cur->line, pname,
1208 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
1209 cfgerr++;
1210 break;
1211 }
1212
1213 free(arg->data.str.str);
1214 arg->data.str.str = NULL;
1215 arg->unresolved = 0;
1216 arg->data.prx = px;
1217 break;
1218
1219 case ARGT_TAB:
1220 if (arg->data.str.len) {
1221 pname = arg->data.str.str;
Willy Tarreaue2dc1fa2015-05-26 12:08:07 +02001222 px = proxy_tbl_by_name(pname);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001223 }
1224
1225 if (!px) {
1226 Alert("parsing [%s:%d] : unable to find table '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1227 cur->file, cur->line, pname,
1228 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
1229 cfgerr++;
1230 break;
1231 }
1232
1233 if (!px->table.size) {
1234 Alert("parsing [%s:%d] : no table in proxy '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1235 cur->file, cur->line, pname,
1236 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
1237 cfgerr++;
1238 break;
1239 }
1240
1241 free(arg->data.str.str);
1242 arg->data.str.str = NULL;
1243 arg->unresolved = 0;
1244 arg->data.prx = px;
1245 break;
1246
1247 case ARGT_USR:
1248 if (!arg->data.str.len) {
1249 Alert("parsing [%s:%d] : missing userlist name in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1250 cur->file, cur->line,
1251 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
1252 cfgerr++;
1253 break;
1254 }
1255
1256 if (p->uri_auth && p->uri_auth->userlist &&
1257 !strcmp(p->uri_auth->userlist->name, arg->data.str.str))
1258 ul = p->uri_auth->userlist;
1259 else
1260 ul = auth_find_userlist(arg->data.str.str);
1261
1262 if (!ul) {
1263 Alert("parsing [%s:%d] : unable to find userlist '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1264 cur->file, cur->line, arg->data.str.str,
1265 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
1266 cfgerr++;
1267 break;
1268 }
1269
1270 free(arg->data.str.str);
1271 arg->data.str.str = NULL;
1272 arg->unresolved = 0;
1273 arg->data.usr = ul;
1274 break;
Willy Tarreau46947782015-01-19 19:00:58 +01001275
1276 case ARGT_REG:
1277 if (!arg->data.str.len) {
1278 Alert("parsing [%s:%d] : missing regex in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1279 cur->file, cur->line,
1280 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
1281 cfgerr++;
1282 continue;
1283 }
1284
1285 reg = calloc(1, sizeof(*reg));
1286 if (!reg) {
1287 Alert("parsing [%s:%d] : not enough memory to build regex in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1288 cur->file, cur->line,
1289 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
1290 cfgerr++;
1291 continue;
1292 }
1293
1294 rflags = 0;
1295 rflags |= (arg->type_flags & ARGF_REG_ICASE) ? REG_ICASE : 0;
1296 err = NULL;
1297
Willy Tarreaud817e462015-01-23 20:23:17 +01001298 if (!regex_comp(arg->data.str.str, reg, !(rflags & REG_ICASE), 1 /* capture substr */, &err)) {
Willy Tarreau46947782015-01-19 19:00:58 +01001299 Alert("parsing [%s:%d] : error in regex '%s' in arg %d of %s%s%s%s '%s' %s proxy '%s' : %s.\n",
1300 cur->file, cur->line,
1301 arg->data.str.str,
1302 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id, err);
1303 cfgerr++;
1304 continue;
1305 }
1306
1307 free(arg->data.str.str);
1308 arg->data.str.str = NULL;
1309 arg->unresolved = 0;
1310 arg->data.reg = reg;
1311 break;
1312
1313
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001314 }
1315
1316 LIST_DEL(&cur->list);
1317 free(cur);
1318 } /* end of args processing */
1319
1320 return cfgerr;
1321}
1322
1323/*
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001324 * Process a fetch + format conversion as defined by the sample expression
1325 * <expr> on request or response considering the <opt> parameter. The output is
1326 * always of type string. If a stable sample can be fetched, or an unstable one
1327 * when <opt> contains SMP_OPT_FINAL, the sample is converted to a string and
1328 * returned without the SMP_F_MAY_CHANGE flag. If an unstable sample is found
1329 * and <opt> does not contain SMP_OPT_FINAL, then the sample is returned as-is
1330 * with its SMP_F_MAY_CHANGE flag so that the caller can check it and decide to
1331 * take actions (eg: wait longer). If a sample could not be found or could not
Willy Tarreau6bcb0a82014-07-30 08:56:35 +02001332 * be converted, NULL is returned. The caller MUST NOT use the sample if the
1333 * SMP_F_MAY_CHANGE flag is present, as it is used only as a hint that there is
1334 * still hope to get it after waiting longer, and is not converted to string.
1335 * The possible output combinations are the following :
1336 *
1337 * return MAY_CHANGE FINAL Meaning for the sample
1338 * NULL * * Not present and will never be (eg: header)
1339 * smp 0 * Final value converted (eg: header)
1340 * smp 1 0 Not present yet, may appear later (eg: header)
1341 * smp 1 1 never happens (either flag is cleared on output)
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001342 */
Willy Tarreau192252e2015-04-04 01:47:55 +02001343struct sample *sample_fetch_string(struct proxy *px, struct session *sess,
1344 struct stream *strm, unsigned int opt,
1345 struct sample_expr *expr)
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001346{
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001347 struct sample *smp = &temp_smp;
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001348
Willy Tarreau6c616e02014-06-25 16:56:41 +02001349 memset(smp, 0, sizeof(*smp));
1350
Willy Tarreau192252e2015-04-04 01:47:55 +02001351 if (!sample_process(px, sess, strm, opt, expr, smp)) {
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001352 if ((smp->flags & SMP_F_MAY_CHANGE) && !(opt & SMP_OPT_FINAL))
1353 return smp;
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001354 return NULL;
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001355 }
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001356
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01001357 if (!sample_casts[smp->type][SMP_T_STR])
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001358 return NULL;
1359
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01001360 if (!sample_casts[smp->type][SMP_T_STR](smp))
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001361 return NULL;
1362
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01001363 smp->type = SMP_T_STR;
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001364 smp->flags &= ~SMP_F_MAY_CHANGE;
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001365 return smp;
1366}
1367
Emeric Brun107ca302010-01-04 16:16:05 +01001368/*****************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +02001369/* Sample format convert functions */
Willy Tarreaub8c8f1f2012-04-23 22:38:26 +02001370/* These functions set the data type on return. */
Emeric Brun107ca302010-01-04 16:16:05 +01001371/*****************************************************************/
1372
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001373static int sample_conv_bin2base64(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brun53d1a982014-04-30 18:21:37 +02001374{
1375 struct chunk *trash = get_trash_chunk();
1376 int b64_len;
1377
1378 trash->len = 0;
1379 b64_len = a2base64(smp->data.str.str, smp->data.str.len, trash->str, trash->size);
1380 if (b64_len < 0)
1381 return 0;
1382
1383 trash->len = b64_len;
1384 smp->data.str = *trash;
1385 smp->type = SMP_T_STR;
1386 smp->flags &= ~SMP_F_CONST;
1387 return 1;
1388}
1389
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001390static int sample_conv_bin2hex(const struct arg *arg_p, struct sample *smp, void *private)
Thierry FOURNIER2f49d6d2014-03-12 15:01:52 +01001391{
1392 struct chunk *trash = get_trash_chunk();
1393 unsigned char c;
1394 int ptr = 0;
1395
1396 trash->len = 0;
1397 while (ptr < smp->data.str.len && trash->len <= trash->size - 2) {
1398 c = smp->data.str.str[ptr++];
1399 trash->str[trash->len++] = hextab[(c >> 4) & 0xF];
1400 trash->str[trash->len++] = hextab[c & 0xF];
1401 }
1402 smp->data.str = *trash;
1403 smp->type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01001404 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIER2f49d6d2014-03-12 15:01:52 +01001405 return 1;
1406}
1407
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001408/* hashes the binary input into a 32-bit unsigned int */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001409static int sample_conv_djb2(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001410{
1411 smp->data.uint = hash_djb2(smp->data.str.str, smp->data.str.len);
1412 if (arg_p && arg_p->data.uint)
1413 smp->data.uint = full_hash(smp->data.uint);
1414 smp->type = SMP_T_UINT;
1415 return 1;
1416}
1417
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001418static int sample_conv_str2lower(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brun107ca302010-01-04 16:16:05 +01001419{
1420 int i;
1421
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01001422 if (!smp_dup(smp))
1423 return 0;
1424
Willy Tarreau342acb42012-04-23 22:03:39 +02001425 if (!smp->data.str.size)
Emeric Brun485479d2010-09-23 18:02:19 +02001426 return 0;
1427
Willy Tarreau342acb42012-04-23 22:03:39 +02001428 for (i = 0; i < smp->data.str.len; i++) {
1429 if ((smp->data.str.str[i] >= 'A') && (smp->data.str.str[i] <= 'Z'))
1430 smp->data.str.str[i] += 'a' - 'A';
Emeric Brun107ca302010-01-04 16:16:05 +01001431 }
1432 return 1;
1433}
1434
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001435static int sample_conv_str2upper(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brun107ca302010-01-04 16:16:05 +01001436{
1437 int i;
1438
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01001439 if (!smp_dup(smp))
1440 return 0;
1441
Willy Tarreau342acb42012-04-23 22:03:39 +02001442 if (!smp->data.str.size)
Emeric Brun485479d2010-09-23 18:02:19 +02001443 return 0;
1444
Willy Tarreau342acb42012-04-23 22:03:39 +02001445 for (i = 0; i < smp->data.str.len; i++) {
1446 if ((smp->data.str.str[i] >= 'a') && (smp->data.str.str[i] <= 'z'))
1447 smp->data.str.str[i] += 'A' - 'a';
Emeric Brun107ca302010-01-04 16:16:05 +01001448 }
1449 return 1;
1450}
1451
Willy Tarreauf9954102012-04-20 14:03:29 +02001452/* takes the netmask in arg_p */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001453static int sample_conv_ipmask(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud31d6eb2010-01-26 18:01:41 +01001454{
Willy Tarreau342acb42012-04-23 22:03:39 +02001455 smp->data.ipv4.s_addr &= arg_p->data.ipv4.s_addr;
Willy Tarreaub8c8f1f2012-04-23 22:38:26 +02001456 smp->type = SMP_T_IPV4;
Willy Tarreaud31d6eb2010-01-26 18:01:41 +01001457 return 1;
1458}
1459
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001460/* takes an UINT value on input supposed to represent the time since EPOCH,
1461 * adds an optional offset found in args[1] and emits a string representing
1462 * the local time in the format specified in args[1] using strftime().
1463 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001464static int sample_conv_ltime(const struct arg *args, struct sample *smp, void *private)
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001465{
1466 struct chunk *temp;
1467 time_t curr_date = smp->data.uint;
1468
1469 /* add offset */
1470 if (args[1].type == ARGT_SINT || args[1].type == ARGT_UINT)
1471 curr_date += args[1].data.sint;
1472
1473 temp = get_trash_chunk();
1474 temp->len = strftime(temp->str, temp->size, args[0].data.str.str, localtime(&curr_date));
1475 smp->data.str = *temp;
1476 smp->type = SMP_T_STR;
1477 return 1;
1478}
1479
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001480/* hashes the binary input into a 32-bit unsigned int */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001481static int sample_conv_sdbm(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001482{
1483 smp->data.uint = hash_sdbm(smp->data.str.str, smp->data.str.len);
1484 if (arg_p && arg_p->data.uint)
1485 smp->data.uint = full_hash(smp->data.uint);
1486 smp->type = SMP_T_UINT;
1487 return 1;
1488}
1489
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001490/* takes an UINT value on input supposed to represent the time since EPOCH,
1491 * adds an optional offset found in args[1] and emits a string representing
1492 * the UTC date in the format specified in args[1] using strftime().
1493 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001494static int sample_conv_utime(const struct arg *args, struct sample *smp, void *private)
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001495{
1496 struct chunk *temp;
1497 time_t curr_date = smp->data.uint;
1498
1499 /* add offset */
1500 if (args[1].type == ARGT_SINT || args[1].type == ARGT_UINT)
1501 curr_date += args[1].data.sint;
1502
1503 temp = get_trash_chunk();
1504 temp->len = strftime(temp->str, temp->size, args[0].data.str.str, gmtime(&curr_date));
1505 smp->data.str = *temp;
1506 smp->type = SMP_T_STR;
1507 return 1;
1508}
1509
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001510/* hashes the binary input into a 32-bit unsigned int */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001511static int sample_conv_wt6(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001512{
1513 smp->data.uint = hash_wt6(smp->data.str.str, smp->data.str.len);
1514 if (arg_p && arg_p->data.uint)
1515 smp->data.uint = full_hash(smp->data.uint);
1516 smp->type = SMP_T_UINT;
1517 return 1;
1518}
1519
Willy Tarreau80599772015-01-20 19:35:24 +01001520/* hashes the binary input into a 32-bit unsigned int */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001521static int sample_conv_crc32(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau80599772015-01-20 19:35:24 +01001522{
1523 smp->data.uint = hash_crc32(smp->data.str.str, smp->data.str.len);
1524 if (arg_p && arg_p->data.uint)
1525 smp->data.uint = full_hash(smp->data.uint);
1526 smp->type = SMP_T_UINT;
1527 return 1;
1528}
1529
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001530/* This function escape special json characters. The returned string can be
1531 * safely set between two '"' and used as json string. The json string is
1532 * defined like this:
1533 *
1534 * any Unicode character except '"' or '\' or control character
1535 * \", \\, \/, \b, \f, \n, \r, \t, \u + four-hex-digits
1536 *
1537 * The enum input_type contain all the allowed mode for decoding the input
1538 * string.
1539 */
1540enum input_type {
1541 IT_ASCII = 0,
1542 IT_UTF8,
1543 IT_UTF8S,
1544 IT_UTF8P,
1545 IT_UTF8PS,
1546};
1547static int sample_conv_json_check(struct arg *arg, struct sample_conv *conv,
1548 const char *file, int line, char **err)
1549{
1550 if (!arg) {
1551 memprintf(err, "Unexpected empty arg list");
1552 return 0;
1553 }
1554
1555 if (arg->type != ARGT_STR) {
1556 memprintf(err, "Unexpected arg type");
1557 return 0;
1558 }
1559
1560 if (strcmp(arg->data.str.str, "") == 0) {
1561 arg->type = ARGT_UINT;
1562 arg->data.uint = IT_ASCII;
1563 return 1;
1564 }
1565
1566 else if (strcmp(arg->data.str.str, "ascii") == 0) {
1567 arg->type = ARGT_UINT;
1568 arg->data.uint = IT_ASCII;
1569 return 1;
1570 }
1571
1572 else if (strcmp(arg->data.str.str, "utf8") == 0) {
1573 arg->type = ARGT_UINT;
1574 arg->data.uint = IT_UTF8;
1575 return 1;
1576 }
1577
1578 else if (strcmp(arg->data.str.str, "utf8s") == 0) {
1579 arg->type = ARGT_UINT;
1580 arg->data.uint = IT_UTF8S;
1581 return 1;
1582 }
1583
1584 else if (strcmp(arg->data.str.str, "utf8p") == 0) {
1585 arg->type = ARGT_UINT;
1586 arg->data.uint = IT_UTF8P;
1587 return 1;
1588 }
1589
1590 else if (strcmp(arg->data.str.str, "utf8ps") == 0) {
1591 arg->type = ARGT_UINT;
1592 arg->data.uint = IT_UTF8PS;
1593 return 1;
1594 }
1595
1596 memprintf(err, "Unexpected input code type at file '%s', line %d. "
1597 "Allowed value are 'ascii', 'utf8', 'utf8p' and 'utf8pp'", file, line);
1598 return 0;
1599}
1600
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001601static int sample_conv_json(const struct arg *arg_p, struct sample *smp, void *private)
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001602{
1603 struct chunk *temp;
1604 char _str[7]; /* \u + 4 hex digit + null char for sprintf. */
1605 const char *str;
1606 int len;
1607 enum input_type input_type = IT_ASCII;
1608 unsigned int c;
1609 unsigned int ret;
1610 char *p;
1611
1612 if (arg_p)
1613 input_type = arg_p->data.uint;
1614
1615 temp = get_trash_chunk();
1616 temp->len = 0;
1617
1618 p = smp->data.str.str;
1619 while (p < smp->data.str.str + smp->data.str.len) {
1620
1621 if (input_type == IT_ASCII) {
1622 /* Read input as ASCII. */
1623 c = *(unsigned char *)p;
1624 p++;
1625 }
1626 else {
1627 /* Read input as UTF8. */
1628 ret = utf8_next(p, smp->data.str.len - ( p - smp->data.str.str ), &c);
1629 p += utf8_return_length(ret);
1630
1631 if (input_type == IT_UTF8 && utf8_return_code(ret) != UTF8_CODE_OK)
1632 return 0;
1633 if (input_type == IT_UTF8S && utf8_return_code(ret) != UTF8_CODE_OK)
1634 continue;
1635 if (input_type == IT_UTF8P && utf8_return_code(ret) & (UTF8_CODE_INVRANGE|UTF8_CODE_BADSEQ))
1636 return 0;
1637 if (input_type == IT_UTF8PS && utf8_return_code(ret) & (UTF8_CODE_INVRANGE|UTF8_CODE_BADSEQ))
1638 continue;
1639
1640 /* Check too big values. */
1641 if ((unsigned int)c > 0xffff) {
1642 if (input_type == IT_UTF8 || input_type == IT_UTF8P)
1643 return 0;
1644 continue;
1645 }
1646 }
1647
1648 /* Convert character. */
1649 if (c == '"') {
1650 len = 2;
1651 str = "\\\"";
1652 }
1653 else if (c == '\\') {
1654 len = 2;
1655 str = "\\\\";
1656 }
1657 else if (c == '/') {
1658 len = 2;
1659 str = "\\/";
1660 }
1661 else if (c == '\b') {
1662 len = 2;
1663 str = "\\b";
1664 }
1665 else if (c == '\f') {
1666 len = 2;
1667 str = "\\f";
1668 }
1669 else if (c == '\r') {
1670 len = 2;
1671 str = "\\r";
1672 }
1673 else if (c == '\n') {
1674 len = 2;
1675 str = "\\n";
1676 }
1677 else if (c == '\t') {
1678 len = 2;
1679 str = "\\t";
1680 }
1681 else if (c > 0xff || !isprint(c)) {
1682 /* isprint generate a segfault if c is too big. The man says that
1683 * c must have the value of an unsigned char or EOF.
1684 */
1685 len = 6;
1686 _str[0] = '\\';
1687 _str[1] = 'u';
1688 snprintf(&_str[2], 5, "%04x", (unsigned short)c);
1689 str = _str;
1690 }
1691 else {
1692 len = 1;
1693 str = (char *)&c;
1694 }
1695
1696 /* Check length */
1697 if (temp->len + len > temp->size)
1698 return 0;
1699
1700 /* Copy string. */
1701 memcpy(temp->str + temp->len, str, len);
1702 temp->len += len;
1703 }
1704
1705 smp->flags &= ~SMP_F_CONST;
1706 smp->data.str = *temp;
1707 smp->type = SMP_T_STR;
1708
1709 return 1;
1710}
1711
Emeric Brun54c4ac82014-11-03 15:32:43 +01001712/* This sample function is designed to extract some bytes from an input buffer.
1713 * First arg is the offset.
1714 * Optional second arg is the length to truncate */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001715static int sample_conv_bytes(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brun54c4ac82014-11-03 15:32:43 +01001716{
1717 if (smp->data.str.len <= arg_p[0].data.uint) {
1718 smp->data.str.len = 0;
1719 return 1;
1720 }
1721
1722 if (smp->data.str.size)
1723 smp->data.str.size -= arg_p[0].data.uint;
1724 smp->data.str.len -= arg_p[0].data.uint;
1725 smp->data.str.str += arg_p[0].data.uint;
1726
1727 if ((arg_p[1].type == ARGT_UINT) && (arg_p[1].data.uint < smp->data.str.len))
1728 smp->data.str.len = arg_p[1].data.uint;
1729
1730 return 1;
1731}
1732
Emeric Brunf399b0d2014-11-03 17:07:03 +01001733static int sample_conv_field_check(struct arg *args, struct sample_conv *conv,
1734 const char *file, int line, char **err)
1735{
1736 struct arg *arg = args;
1737
1738 if (!arg) {
1739 memprintf(err, "Unexpected empty arg list");
1740 return 0;
1741 }
1742
1743 if (arg->type != ARGT_UINT) {
1744 memprintf(err, "Unexpected arg type");
1745 return 0;
1746 }
1747
1748 if (!arg->data.uint) {
1749 memprintf(err, "Unexpected value 0 for index");
1750 return 0;
1751 }
1752
1753 arg++;
1754
1755 if (arg->type != ARGT_STR) {
1756 memprintf(err, "Unexpected arg type");
1757 return 0;
1758 }
1759
1760 if (!arg->data.str.len) {
1761 memprintf(err, "Empty separators list");
1762 return 0;
1763 }
1764
1765 return 1;
1766}
1767
1768/* This sample function is designed to a return selected part of a string (field).
1769 * First arg is the index of the field (start at 1)
1770 * Second arg is a char list of separators (type string)
1771 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001772static int sample_conv_field(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brunf399b0d2014-11-03 17:07:03 +01001773{
1774 unsigned int field;
1775 char *start, *end;
1776 int i;
1777
1778 if (!arg_p[0].data.uint)
1779 return 0;
1780
1781 field = 1;
1782 end = start = smp->data.str.str;
1783 while (end - smp->data.str.str < smp->data.str.len) {
1784
1785 for (i = 0 ; i < arg_p[1].data.str.len ; i++) {
1786 if (*end == arg_p[1].data.str.str[i]) {
1787 if (field == arg_p[0].data.uint)
1788 goto found;
1789 start = end+1;
1790 field++;
1791 break;
1792 }
1793 }
1794 end++;
1795 }
1796
1797 /* Field not found */
1798 if (field != arg_p[0].data.uint) {
1799 smp->data.str.len = 0;
1800 return 1;
1801 }
1802found:
1803 smp->data.str.len = end - start;
1804 /* If ret string is len 0, no need to
1805 change pointers or to update size */
1806 if (!smp->data.str.len)
1807 return 1;
1808
1809 smp->data.str.str = start;
1810
1811 /* Compute remaining size if needed
1812 Note: smp->data.str.size cannot be set to 0 */
1813 if (smp->data.str.size)
1814 smp->data.str.size -= start - smp->data.str.str;
1815
1816 return 1;
1817}
1818
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01001819/* This sample function is designed to return a word from a string.
1820 * First arg is the index of the word (start at 1)
1821 * Second arg is a char list of words separators (type string)
1822 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001823static int sample_conv_word(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01001824{
1825 unsigned int word;
1826 char *start, *end;
1827 int i, issep, inword;
1828
1829 if (!arg_p[0].data.uint)
1830 return 0;
1831
1832 word = 0;
1833 inword = 0;
1834 end = start = smp->data.str.str;
1835 while (end - smp->data.str.str < smp->data.str.len) {
1836 issep = 0;
1837 for (i = 0 ; i < arg_p[1].data.str.len ; i++) {
1838 if (*end == arg_p[1].data.str.str[i]) {
1839 issep = 1;
1840 break;
1841 }
1842 }
1843 if (!inword) {
1844 if (!issep) {
1845 word++;
1846 start = end;
1847 inword = 1;
1848 }
1849 }
1850 else if (issep) {
1851 if (word == arg_p[0].data.uint)
1852 goto found;
1853 inword = 0;
1854 }
1855 end++;
1856 }
1857
1858 /* Field not found */
1859 if (word != arg_p[0].data.uint) {
1860 smp->data.str.len = 0;
1861 return 1;
1862 }
1863found:
1864 smp->data.str.len = end - start;
1865 /* If ret string is len 0, no need to
1866 change pointers or to update size */
1867 if (!smp->data.str.len)
1868 return 1;
1869
1870 smp->data.str.str = start;
1871
1872 /* Compute remaining size if needed
1873 Note: smp->data.str.size cannot be set to 0 */
1874 if (smp->data.str.size)
1875 smp->data.str.size -= start - smp->data.str.str;
1876
1877 return 1;
1878}
1879
Willy Tarreau7eda8492015-01-20 19:47:06 +01001880static int sample_conv_regsub_check(struct arg *args, struct sample_conv *conv,
1881 const char *file, int line, char **err)
1882{
1883 struct arg *arg = args;
1884 char *p;
1885 int len;
1886
1887 /* arg0 is a regex, it uses type_flag for ICASE and global match */
1888 arg[0].type_flags = 0;
1889
1890 if (arg[2].type != ARGT_STR)
1891 return 1;
1892
1893 p = arg[2].data.str.str;
1894 len = arg[2].data.str.len;
1895 while (len) {
1896 if (*p == 'i') {
1897 arg[0].type_flags |= ARGF_REG_ICASE;
1898 }
1899 else if (*p == 'g') {
1900 arg[0].type_flags |= ARGF_REG_GLOB;
1901 }
1902 else {
1903 memprintf(err, "invalid regex flag '%c', only 'i' and 'g' are supported", *p);
1904 return 0;
1905 }
1906 p++;
1907 len--;
1908 }
1909 return 1;
1910}
1911
1912/* This sample function is designed to do the equivalent of s/match/replace/ on
1913 * the input string. It applies a regex and restarts from the last matched
1914 * location until nothing matches anymore. First arg is the regex to apply to
1915 * the input string, second arg is the replacement expression.
1916 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001917static int sample_conv_regsub(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau7eda8492015-01-20 19:47:06 +01001918{
1919 char *start, *end;
1920 struct my_regex *reg = arg_p[0].data.reg;
1921 regmatch_t pmatch[MAX_MATCH];
1922 struct chunk *trash = get_trash_chunk();
1923 int flag, max;
1924 int found;
1925
1926 start = smp->data.str.str;
1927 end = start + smp->data.str.len;
1928
1929 flag = 0;
1930 while (1) {
1931 /* check for last round which is used to copy remaining parts
1932 * when not running in global replacement mode.
1933 */
1934 found = 0;
1935 if ((arg_p[0].type_flags & ARGF_REG_GLOB) || !(flag & REG_NOTBOL)) {
1936 /* Note: we can have start == end on empty strings or at the end */
1937 found = regex_exec_match2(reg, start, end - start, MAX_MATCH, pmatch, flag);
1938 }
1939
1940 if (!found)
1941 pmatch[0].rm_so = end - start;
1942
1943 /* copy the heading non-matching part (which may also be the tail if nothing matches) */
1944 max = trash->size - trash->len;
1945 if (max && pmatch[0].rm_so > 0) {
1946 if (max > pmatch[0].rm_so)
1947 max = pmatch[0].rm_so;
1948 memcpy(trash->str + trash->len, start, max);
1949 trash->len += max;
1950 }
1951
1952 if (!found)
1953 break;
1954
1955 /* replace the matching part */
1956 max = trash->size - trash->len;
1957 if (max) {
1958 if (max > arg_p[1].data.str.len)
1959 max = arg_p[1].data.str.len;
1960 memcpy(trash->str + trash->len, arg_p[1].data.str.str, max);
1961 trash->len += max;
1962 }
1963
1964 /* stop here if we're done with this string */
1965 if (start >= end)
1966 break;
1967
1968 /* We have a special case for matches of length 0 (eg: "x*y*").
1969 * These ones are considered to match in front of a character,
1970 * so we have to copy that character and skip to the next one.
1971 */
1972 if (!pmatch[0].rm_eo) {
1973 if (trash->len < trash->size)
1974 trash->str[trash->len++] = start[pmatch[0].rm_eo];
1975 pmatch[0].rm_eo++;
1976 }
1977
1978 start += pmatch[0].rm_eo;
1979 flag |= REG_NOTBOL;
1980 }
1981
1982 smp->data.str = *trash;
1983 return 1;
1984}
1985
Willy Tarreau97707872015-01-27 15:12:13 +01001986/* Takes a UINT on input, applies a binary twos complement and returns the UINT
1987 * result.
1988 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001989static int sample_conv_binary_cpl(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01001990{
1991 smp->data.uint = ~smp->data.uint;
1992 return 1;
1993}
1994
1995/* Takes a UINT on input, applies a binary "and" with the UINT in arg_p, and
1996 * returns the UINT result.
1997 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001998static int sample_conv_binary_and(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01001999{
2000 smp->data.uint &= arg_p->data.uint;
2001 return 1;
2002}
2003
2004/* Takes a UINT on input, applies a binary "or" with the UINT in arg_p, and
2005 * returns the UINT result.
2006 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002007static int sample_conv_binary_or(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002008{
2009 smp->data.uint |= arg_p->data.uint;
2010 return 1;
2011}
2012
2013/* Takes a UINT on input, applies a binary "xor" with the UINT in arg_p, and
2014 * returns the UINT result.
2015 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002016static int sample_conv_binary_xor(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002017{
2018 smp->data.uint ^= arg_p->data.uint;
2019 return 1;
2020}
2021
2022/* Takes a UINT on input, applies an arithmetic "add" with the UINT in arg_p,
2023 * and returns the UINT result.
2024 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002025static int sample_conv_arith_add(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002026{
2027 smp->data.uint += arg_p->data.uint;
2028 return 1;
2029}
2030
2031/* Takes a UINT on input, applies an arithmetic "sub" with the UINT in arg_p,
2032 * and returns the UINT result.
2033 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002034static int sample_conv_arith_sub(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002035 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002036{
2037 smp->data.uint -= arg_p->data.uint;
2038 return 1;
2039}
2040
2041/* Takes a UINT on input, applies an arithmetic "mul" with the UINT in arg_p,
2042 * and returns the UINT result.
2043 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002044static int sample_conv_arith_mul(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002045 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002046{
2047 smp->data.uint *= arg_p->data.uint;
2048 return 1;
2049}
2050
2051/* Takes a UINT on input, applies an arithmetic "div" with the UINT in arg_p,
2052 * and returns the UINT result. If arg_p makes the result overflow, then the
2053 * largest possible quantity is returned.
2054 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002055static int sample_conv_arith_div(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002056 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002057{
2058 if (arg_p->data.uint)
2059 smp->data.uint /= arg_p->data.uint;
2060 else
2061 smp->data.uint = ~0;
2062 return 1;
2063}
2064
2065/* Takes a UINT on input, applies an arithmetic "mod" with the UINT in arg_p,
2066 * and returns the UINT result. If arg_p makes the result overflow, then zero
2067 * is returned.
2068 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002069static int sample_conv_arith_mod(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002070 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002071{
2072 if (arg_p->data.uint)
2073 smp->data.uint %= arg_p->data.uint;
2074 else
2075 smp->data.uint = 0;
2076 return 1;
2077}
2078
2079/* Takes an UINT on input, applies an arithmetic "neg" and returns the UINT
2080 * result.
2081 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002082static int sample_conv_arith_neg(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002083 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002084{
2085 smp->data.uint = -smp->data.uint;
2086 return 1;
2087}
2088
2089/* Takes a UINT on input, returns true is the value is non-null, otherwise
2090 * false. The output is a BOOL.
2091 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002092static int sample_conv_arith_bool(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002093 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002094{
2095 smp->data.uint = !!smp->data.uint;
2096 smp->type = SMP_T_BOOL;
2097 return 1;
2098}
2099
2100/* Takes a UINT on input, returns false is the value is non-null, otherwise
2101 * truee. The output is a BOOL.
2102 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002103static int sample_conv_arith_not(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002104 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002105{
2106 smp->data.uint = !smp->data.uint;
2107 smp->type = SMP_T_BOOL;
2108 return 1;
2109}
2110
2111/* Takes a UINT on input, returns true is the value is odd, otherwise false.
2112 * The output is a BOOL.
2113 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002114static int sample_conv_arith_odd(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002115 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002116{
2117 smp->data.uint = smp->data.uint & 1;
2118 smp->type = SMP_T_BOOL;
2119 return 1;
2120}
2121
2122/* Takes a UINT on input, returns true is the value is even, otherwise false.
2123 * The output is a BOOL.
2124 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002125static int sample_conv_arith_even(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002126 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002127{
2128 smp->data.uint = !(smp->data.uint & 1);
2129 smp->type = SMP_T_BOOL;
2130 return 1;
2131}
Thomas Holmes951d44d2015-05-13 11:27:35 +01002132
2133#ifdef USE_51DEGREES
Thomas Holmes4d441a72015-05-29 12:54:25 +01002134#ifdef FIFTYONEDEGREES_H_TRIE_INCLUDED
2135static int
2136sample_fiftyone_degrees(const struct arg *args, struct sample *smp, void *private)
2137{
2138 int i; // used in loops.
2139 int device_offset;
2140 int property_index;
2141 char no_data[] = "NoData"; // response when no data could be found.
2142 struct chunk *tmp;
2143
2144 // use a temporary trash buffer and copy data in it
2145 smp->data.str.str[smp->data.str.len] = '\0';
2146
2147 // perform detection.
2148 device_offset = fiftyoneDegreesGetDeviceOffset(smp->data.str.str);
2149
2150 i = 0;
2151 tmp = get_trash_chunk();
2152 chunk_reset(tmp);
2153
2154 // loop through property names passed to the filter and fetch them from the dataset.
2155 while (args[i].data.str.str) {
2156 // try to find request property in dataset.
2157 property_index = fiftyoneDegreesGetPropertyIndex(args[i].data.str.str);
2158 if (property_index > 0) {
2159 chunk_appendf(tmp, "%s", fiftyoneDegreesGetValue(device_offset, property_index));
2160 }
2161 else {
2162 chunk_appendf(tmp, "%s", no_data);
2163 }
2164 // add seperator
2165 if (global._51d_property_seperator)
2166 chunk_appendf(tmp, "%c", global._51d_property_seperator);
2167 else
2168 chunk_appendf(tmp, ",");
2169 ++i;
2170 }
2171
2172 if (tmp->len) {
2173 --tmp->len;
2174 tmp->str[tmp->len] = '\0';
2175 }
2176
2177 smp->data.str.str = tmp->str;
2178 smp->data.str.len = strlen(smp->data.str.str);
2179
2180 return 1;
2181}
2182#endif // FIFTYONEDEGREES_H_TRIE_INCLUDED
2183
2184#ifdef FIFTYONEDEGREES_H_PATTERN_INCLUDED
2185static int
2186sample_fiftyone_degrees(const struct arg *args, struct sample *smp, void *private)
Thomas Holmes951d44d2015-05-13 11:27:35 +01002187{
2188 int i, j, found; // used in loops.
2189 fiftyoneDegreesWorkset* ws; // workset for detection.
2190 char no_data[] = "NoData"; // response when no data could be found.
Thomas Holmes4d441a72015-05-29 12:54:25 +01002191 const char* property_name;
Thomas Holmes951d44d2015-05-13 11:27:35 +01002192 struct chunk *tmp;
2193
2194 // use a temporary trash buffer and copy data in it
2195 smp->data.str.str[smp->data.str.len] = '\0';
2196
2197 // create workset. this will later contain detection results.
2198 ws = fiftyoneDegreesCreateWorkset(&global._51d_data_set);
2199 if (!ws)
2200 return 0;
2201
2202 // perform detection.
2203 fiftyoneDegreesMatch(ws, smp->data.str.str);
2204
2205 i = 0;
2206 tmp = get_trash_chunk();
2207 chunk_reset(tmp);
2208 // loop through property names passed to the filter and fetch them from the dataset.
2209 while (args[i].data.str.str) {
2210 found = j = 0;
2211 // try to find request property in dataset.
Thomas Holmes4d441a72015-05-29 12:54:25 +01002212 for (j = 0; j < ws->dataSet->requiredPropertyCount; j++) {
2213 property_name = fiftyoneDegreesGetPropertyName(ws->dataSet, ws->dataSet->requiredProperties[j]);
2214 if (strcmp(property_name, args[i].data.str.str) == 0) {
Thomas Holmes951d44d2015-05-13 11:27:35 +01002215 found = 1;
2216 fiftyoneDegreesSetValues(ws, j);
2217 chunk_appendf(tmp, "%s", fiftyoneDegreesGetValueName(ws->dataSet, *ws->values));
2218 break;
2219 }
Thomas Holmes951d44d2015-05-13 11:27:35 +01002220 }
Thomas Holmes4d441a72015-05-29 12:54:25 +01002221 if (!found) {
Thomas Holmes951d44d2015-05-13 11:27:35 +01002222 chunk_appendf(tmp, "%s", no_data);
Thomas Holmes4d441a72015-05-29 12:54:25 +01002223 }
Thomas Holmes951d44d2015-05-13 11:27:35 +01002224
2225 // add seperator
2226 if (global._51d_property_seperator)
2227 chunk_appendf(tmp, "%c", global._51d_property_seperator);
2228 else
2229 chunk_appendf(tmp, ",");
2230 ++i;
2231 }
2232
2233 if (tmp->len) {
2234 --tmp->len;
2235 tmp->str[tmp->len] = '\0';
2236 }
2237
2238 smp->data.str.str = tmp->str;
2239 smp->data.str.len = strlen(smp->data.str.str);
2240
2241 fiftyoneDegreesFreeWorkset(ws);
2242
2243 return 1;
2244}
Thomas Holmes4d441a72015-05-29 12:54:25 +01002245#endif // FIFTYONEDEGREES_H_PATTERN_INCLUDED
2246#endif // USE_51DEGREES
Thomas Holmes951d44d2015-05-13 11:27:35 +01002247
Willy Tarreau97707872015-01-27 15:12:13 +01002248
Willy Tarreau5b8ad222013-07-25 12:17:57 +02002249/************************************************************************/
2250/* All supported sample fetch functions must be declared here */
2251/************************************************************************/
2252
2253/* force TRUE to be returned at the fetch level */
2254static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002255smp_fetch_true(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau5b8ad222013-07-25 12:17:57 +02002256{
2257 smp->type = SMP_T_BOOL;
2258 smp->data.uint = 1;
2259 return 1;
2260}
2261
2262/* force FALSE to be returned at the fetch level */
2263static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002264smp_fetch_false(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau5b8ad222013-07-25 12:17:57 +02002265{
2266 smp->type = SMP_T_BOOL;
2267 smp->data.uint = 0;
2268 return 1;
2269}
2270
2271/* retrieve environment variable $1 as a string */
2272static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002273smp_fetch_env(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau5b8ad222013-07-25 12:17:57 +02002274{
2275 char *env;
2276
2277 if (!args || args[0].type != ARGT_STR)
2278 return 0;
2279
2280 env = getenv(args[0].data.str.str);
2281 if (!env)
2282 return 0;
2283
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01002284 smp->type = SMP_T_STR;
2285 smp->flags = SMP_F_CONST;
Willy Tarreau5b8ad222013-07-25 12:17:57 +02002286 smp->data.str.str = env;
2287 smp->data.str.len = strlen(env);
2288 return 1;
2289}
2290
Willy Tarreau6236d3a2013-07-25 14:28:25 +02002291/* retrieve the current local date in epoch time, and applies an optional offset
2292 * of args[0] seconds.
2293 */
2294static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002295smp_fetch_date(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau6236d3a2013-07-25 14:28:25 +02002296{
2297 smp->data.uint = date.tv_sec;
2298
2299 /* add offset */
2300 if (args && (args[0].type == ARGT_SINT || args[0].type == ARGT_UINT))
2301 smp->data.uint += args[0].data.sint;
2302
2303 smp->type = SMP_T_UINT;
2304 smp->flags |= SMP_F_VOL_TEST | SMP_F_MAY_CHANGE;
2305 return 1;
2306}
2307
Willy Tarreau0f30d262014-11-24 16:02:05 +01002308/* returns the number of processes */
2309static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002310smp_fetch_nbproc(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau0f30d262014-11-24 16:02:05 +01002311{
2312 smp->type = SMP_T_UINT;
2313 smp->data.uint = global.nbproc;
2314 return 1;
2315}
2316
2317/* returns the number of the current process (between 1 and nbproc */
2318static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002319smp_fetch_proc(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau0f30d262014-11-24 16:02:05 +01002320{
2321 smp->type = SMP_T_UINT;
2322 smp->data.uint = relative_pid;
2323 return 1;
2324}
2325
Willy Tarreau84310e22014-02-14 11:59:04 +01002326/* generate a random 32-bit integer for whatever purpose, with an optional
2327 * range specified in argument.
2328 */
2329static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002330smp_fetch_rand(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau84310e22014-02-14 11:59:04 +01002331{
2332 smp->data.uint = random();
2333
2334 /* reduce if needed. Don't do a modulo, use all bits! */
2335 if (args && args[0].type == ARGT_UINT)
Vincent Bernat1228dc02014-12-10 10:31:37 +01002336 smp->data.uint = ((uint64_t)smp->data.uint * args[0].data.uint) / ((u64)RAND_MAX+1);
Willy Tarreau84310e22014-02-14 11:59:04 +01002337
2338 smp->type = SMP_T_UINT;
2339 smp->flags |= SMP_F_VOL_TEST | SMP_F_MAY_CHANGE;
2340 return 1;
2341}
2342
Willy Tarreau0f30d262014-11-24 16:02:05 +01002343/* returns true if the current process is stopping */
2344static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002345smp_fetch_stopping(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau0f30d262014-11-24 16:02:05 +01002346{
2347 smp->type = SMP_T_BOOL;
2348 smp->data.uint = stopping;
2349 return 1;
2350}
2351
Willy Tarreau5b8ad222013-07-25 12:17:57 +02002352/* Note: must not be declared <const> as its list will be overwritten.
2353 * Note: fetches that may return multiple types must be declared as the lowest
2354 * common denominator, the type that can be casted into all other ones. For
2355 * instance IPv4/IPv6 must be declared IPv4.
2356 */
2357static struct sample_fetch_kw_list smp_kws = {ILH, {
2358 { "always_false", smp_fetch_false, 0, NULL, SMP_T_BOOL, SMP_USE_INTRN },
2359 { "always_true", smp_fetch_true, 0, NULL, SMP_T_BOOL, SMP_USE_INTRN },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01002360 { "env", smp_fetch_env, ARG1(1,STR), NULL, SMP_T_STR, SMP_USE_INTRN },
Willy Tarreau6236d3a2013-07-25 14:28:25 +02002361 { "date", smp_fetch_date, ARG1(0,SINT), NULL, SMP_T_UINT, SMP_USE_INTRN },
Willy Tarreau0f30d262014-11-24 16:02:05 +01002362 { "nbproc", smp_fetch_nbproc,0, NULL, SMP_T_UINT, SMP_USE_INTRN },
2363 { "proc", smp_fetch_proc, 0, NULL, SMP_T_UINT, SMP_USE_INTRN },
Willy Tarreau84310e22014-02-14 11:59:04 +01002364 { "rand", smp_fetch_rand, ARG1(0,UINT), NULL, SMP_T_UINT, SMP_USE_INTRN },
Willy Tarreau0f30d262014-11-24 16:02:05 +01002365 { "stopping", smp_fetch_stopping, 0, NULL, SMP_T_BOOL, SMP_USE_INTRN },
Willy Tarreau5b8ad222013-07-25 12:17:57 +02002366 { /* END */ },
2367}};
2368
Emeric Brun107ca302010-01-04 16:16:05 +01002369/* Note: must not be declared <const> as its list will be overwritten */
Willy Tarreaudc13c112013-06-21 23:16:39 +02002370static struct sample_conv_kw_list sample_conv_kws = {ILH, {
Emeric Brun53d1a982014-04-30 18:21:37 +02002371 { "base64", sample_conv_bin2base64,0, NULL, SMP_T_BIN, SMP_T_STR },
Willy Tarreau12785782012-04-27 21:37:17 +02002372 { "upper", sample_conv_str2upper, 0, NULL, SMP_T_STR, SMP_T_STR },
2373 { "lower", sample_conv_str2lower, 0, NULL, SMP_T_STR, SMP_T_STR },
Thierry FOURNIER2f49d6d2014-03-12 15:01:52 +01002374 { "hex", sample_conv_bin2hex, 0, NULL, SMP_T_BIN, SMP_T_STR },
Willy Tarreau12785782012-04-27 21:37:17 +02002375 { "ipmask", sample_conv_ipmask, ARG1(1,MSK4), NULL, SMP_T_IPV4, SMP_T_IPV4 },
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02002376 { "ltime", sample_conv_ltime, ARG2(1,STR,SINT), NULL, SMP_T_UINT, SMP_T_STR },
2377 { "utime", sample_conv_utime, ARG2(1,STR,SINT), NULL, SMP_T_UINT, SMP_T_STR },
Willy Tarreau80599772015-01-20 19:35:24 +01002378 { "crc32", sample_conv_crc32, ARG1(0,UINT), NULL, SMP_T_BIN, SMP_T_UINT },
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02002379 { "djb2", sample_conv_djb2, ARG1(0,UINT), NULL, SMP_T_BIN, SMP_T_UINT },
2380 { "sdbm", sample_conv_sdbm, ARG1(0,UINT), NULL, SMP_T_BIN, SMP_T_UINT },
2381 { "wt6", sample_conv_wt6, ARG1(0,UINT), NULL, SMP_T_BIN, SMP_T_UINT },
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002382 { "json", sample_conv_json, ARG1(1,STR), sample_conv_json_check, SMP_T_STR, SMP_T_STR },
Emeric Brun54c4ac82014-11-03 15:32:43 +01002383 { "bytes", sample_conv_bytes, ARG2(1,UINT,UINT), NULL, SMP_T_BIN, SMP_T_BIN },
Emeric Brunf399b0d2014-11-03 17:07:03 +01002384 { "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 +01002385 { "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 +01002386 { "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 +01002387
2388 { "and", sample_conv_binary_and, ARG1(1,UINT), NULL, SMP_T_UINT, SMP_T_UINT },
2389 { "or", sample_conv_binary_or, ARG1(1,UINT), NULL, SMP_T_UINT, SMP_T_UINT },
2390 { "xor", sample_conv_binary_xor, ARG1(1,UINT), NULL, SMP_T_UINT, SMP_T_UINT },
2391 { "cpl", sample_conv_binary_cpl, 0, NULL, SMP_T_UINT, SMP_T_UINT },
2392 { "bool", sample_conv_arith_bool, 0, NULL, SMP_T_UINT, SMP_T_BOOL },
2393 { "not", sample_conv_arith_not, 0, NULL, SMP_T_UINT, SMP_T_BOOL },
2394 { "odd", sample_conv_arith_odd, 0, NULL, SMP_T_UINT, SMP_T_BOOL },
2395 { "even", sample_conv_arith_even, 0, NULL, SMP_T_UINT, SMP_T_BOOL },
2396 { "add", sample_conv_arith_add, ARG1(1,UINT), NULL, SMP_T_UINT, SMP_T_UINT },
2397 { "sub", sample_conv_arith_sub, ARG1(1,UINT), NULL, SMP_T_UINT, SMP_T_UINT },
2398 { "mul", sample_conv_arith_mul, ARG1(1,UINT), NULL, SMP_T_UINT, SMP_T_UINT },
2399 { "div", sample_conv_arith_div, ARG1(1,UINT), NULL, SMP_T_UINT, SMP_T_UINT },
2400 { "mod", sample_conv_arith_mod, ARG1(1,UINT), NULL, SMP_T_UINT, SMP_T_UINT },
2401 { "neg", sample_conv_arith_neg, 0, NULL, SMP_T_UINT, SMP_T_UINT },
Thomas Holmes951d44d2015-05-13 11:27:35 +01002402#ifdef USE_51DEGREES
Thomas Holmes4d441a72015-05-29 12:54:25 +01002403 { "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 +01002404#endif
Willy Tarreau97707872015-01-27 15:12:13 +01002405
Willy Tarreau9fcb9842012-04-20 14:45:49 +02002406 { NULL, NULL, 0, 0, 0 },
Emeric Brun107ca302010-01-04 16:16:05 +01002407}};
2408
2409__attribute__((constructor))
Willy Tarreau12785782012-04-27 21:37:17 +02002410static void __sample_init(void)
Emeric Brun107ca302010-01-04 16:16:05 +01002411{
Willy Tarreau5b8ad222013-07-25 12:17:57 +02002412 /* register sample fetch and format conversion keywords */
2413 sample_register_fetches(&smp_kws);
Willy Tarreau12785782012-04-27 21:37:17 +02002414 sample_register_convs(&sample_conv_kws);
Emeric Brun107ca302010-01-04 16:16:05 +01002415}