blob: 9d53b810843e0d6e570d52396fdae155eb512546 [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
Willy Tarreau1cf8f082014-02-07 12:14:54 +010035/* sample type names */
36const char *smp_to_type[SMP_TYPES] = {
37 [SMP_T_BOOL] = "bool",
38 [SMP_T_UINT] = "uint",
39 [SMP_T_SINT] = "sint",
40 [SMP_T_ADDR] = "addr",
41 [SMP_T_IPV4] = "ipv4",
42 [SMP_T_IPV6] = "ipv6",
43 [SMP_T_STR] = "str",
44 [SMP_T_BIN] = "bin",
Willy Tarreau1cf8f082014-02-07 12:14:54 +010045};
46
Willy Tarreau12785782012-04-27 21:37:17 +020047/* static sample used in sample_process() when <p> is NULL */
Willy Tarreaub4a88f02012-04-23 21:35:11 +020048static struct sample temp_smp;
Emeric Brun107ca302010-01-04 16:16:05 +010049
Willy Tarreau12785782012-04-27 21:37:17 +020050/* list head of all known sample fetch keywords */
51static struct sample_fetch_kw_list sample_fetches = {
52 .list = LIST_HEAD_INIT(sample_fetches.list)
Emeric Brun107ca302010-01-04 16:16:05 +010053};
54
Willy Tarreau12785782012-04-27 21:37:17 +020055/* list head of all known sample format conversion keywords */
56static struct sample_conv_kw_list sample_convs = {
57 .list = LIST_HEAD_INIT(sample_convs.list)
Emeric Brun107ca302010-01-04 16:16:05 +010058};
59
Willy Tarreau80aca902013-01-07 15:42:20 +010060const unsigned int fetch_cap[SMP_SRC_ENTRIES] = {
61 [SMP_SRC_INTRN] = (SMP_VAL_FE_CON_ACC | SMP_VAL_FE_SES_ACC | SMP_VAL_FE_REQ_CNT |
62 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
63 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
64 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
65 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
66 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
67 SMP_VAL_FE_LOG_END),
68
69 [SMP_SRC_LISTN] = (SMP_VAL_FE_CON_ACC | SMP_VAL_FE_SES_ACC | SMP_VAL_FE_REQ_CNT |
70 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
71 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
72 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
73 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
74 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
75 SMP_VAL_FE_LOG_END),
76
77 [SMP_SRC_FTEND] = (SMP_VAL_FE_CON_ACC | SMP_VAL_FE_SES_ACC | SMP_VAL_FE_REQ_CNT |
78 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
79 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
80 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
81 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
82 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
83 SMP_VAL_FE_LOG_END),
84
85 [SMP_SRC_L4CLI] = (SMP_VAL_FE_CON_ACC | SMP_VAL_FE_SES_ACC | SMP_VAL_FE_REQ_CNT |
86 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
87 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
88 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
89 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
90 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
91 SMP_VAL_FE_LOG_END),
92
93 [SMP_SRC_L5CLI] = (SMP_VAL___________ | SMP_VAL_FE_SES_ACC | SMP_VAL_FE_REQ_CNT |
94 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
95 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
96 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
97 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
98 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
99 SMP_VAL_FE_LOG_END),
100
101 [SMP_SRC_TRACK] = (SMP_VAL_FE_CON_ACC | SMP_VAL_FE_SES_ACC | SMP_VAL_FE_REQ_CNT |
102 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
103 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
104 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
105 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
106 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
107 SMP_VAL_FE_LOG_END),
108
109 [SMP_SRC_L6REQ] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_FE_REQ_CNT |
110 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
111 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
112 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL___________ |
113 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
114 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
115 SMP_VAL___________),
116
117 [SMP_SRC_HRQHV] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_FE_REQ_CNT |
118 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
119 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
120 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL___________ |
121 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
122 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
123 SMP_VAL___________),
124
125 [SMP_SRC_HRQHP] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_FE_REQ_CNT |
126 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
127 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
128 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
129 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
130 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
131 SMP_VAL_FE_LOG_END),
132
133 [SMP_SRC_HRQBO] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
134 SMP_VAL___________ | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
135 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
136 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL___________ |
137 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
138 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
139 SMP_VAL___________),
140
141 [SMP_SRC_BKEND] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
142 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
143 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
144 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
145 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
146 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
147 SMP_VAL_FE_LOG_END),
148
149 [SMP_SRC_SERVR] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
150 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
151 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
152 SMP_VAL___________ | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
153 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
154 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
155 SMP_VAL_FE_LOG_END),
156
157 [SMP_SRC_L4SRV] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
158 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
159 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
160 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_BE_RES_CNT |
161 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
162 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
163 SMP_VAL_FE_LOG_END),
164
165 [SMP_SRC_L5SRV] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
166 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
167 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
168 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_BE_RES_CNT |
169 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
170 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
171 SMP_VAL_FE_LOG_END),
172
173 [SMP_SRC_L6RES] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
174 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
175 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
176 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_BE_RES_CNT |
177 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
178 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
179 SMP_VAL___________),
180
181 [SMP_SRC_HRSHV] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
182 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
183 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
184 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_BE_RES_CNT |
185 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
186 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
187 SMP_VAL___________),
188
189 [SMP_SRC_HRSHP] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
190 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
191 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
192 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_BE_RES_CNT |
193 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
194 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
195 SMP_VAL_FE_LOG_END),
196
197 [SMP_SRC_HRSBO] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
198 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
199 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
200 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
201 SMP_VAL___________ | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
202 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
203 SMP_VAL___________),
204
205 [SMP_SRC_RQFIN] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
206 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
207 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
208 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
209 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
210 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
211 SMP_VAL_FE_LOG_END),
212
213 [SMP_SRC_RSFIN] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
214 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
215 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
216 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
217 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
218 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
219 SMP_VAL_FE_LOG_END),
220
221 [SMP_SRC_TXFIN] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
222 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
223 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
224 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
225 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
226 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
227 SMP_VAL_FE_LOG_END),
228
229 [SMP_SRC_SSFIN] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
230 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
231 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
232 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
233 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
234 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
235 SMP_VAL_FE_LOG_END),
236};
237
238static const char *fetch_src_names[SMP_SRC_ENTRIES] = {
239 [SMP_SRC_INTRN] = "internal state",
240 [SMP_SRC_LISTN] = "listener",
241 [SMP_SRC_FTEND] = "frontend",
242 [SMP_SRC_L4CLI] = "client address",
243 [SMP_SRC_L5CLI] = "client-side connection",
244 [SMP_SRC_TRACK] = "track counters",
245 [SMP_SRC_L6REQ] = "request buffer",
246 [SMP_SRC_HRQHV] = "HTTP request headers",
247 [SMP_SRC_HRQHP] = "HTTP request",
248 [SMP_SRC_HRQBO] = "HTTP request body",
249 [SMP_SRC_BKEND] = "backend",
250 [SMP_SRC_SERVR] = "server",
251 [SMP_SRC_L4SRV] = "server address",
252 [SMP_SRC_L5SRV] = "server-side connection",
253 [SMP_SRC_L6RES] = "response buffer",
254 [SMP_SRC_HRSHV] = "HTTP response headers",
255 [SMP_SRC_HRSHP] = "HTTP response",
256 [SMP_SRC_HRSBO] = "HTTP response body",
257 [SMP_SRC_RQFIN] = "request buffer statistics",
258 [SMP_SRC_RSFIN] = "response buffer statistics",
259 [SMP_SRC_TXFIN] = "transaction statistics",
260 [SMP_SRC_SSFIN] = "session statistics",
261};
262
Willy Tarreaubf8e2512013-03-25 14:52:41 +0100263static const char *fetch_ckp_names[SMP_CKP_ENTRIES] = {
264 [SMP_CKP_FE_CON_ACC] = "frontend tcp-request connection rule",
265 [SMP_CKP_FE_SES_ACC] = "frontend tcp-request session rule",
266 [SMP_CKP_FE_REQ_CNT] = "frontend tcp-request content rule",
267 [SMP_CKP_FE_HRQ_HDR] = "frontend http-request header rule",
268 [SMP_CKP_FE_HRQ_BDY] = "frontend http-request body rule",
269 [SMP_CKP_FE_SET_BCK] = "frontend use-backend rule",
270 [SMP_CKP_BE_REQ_CNT] = "backend tcp-request content rule",
271 [SMP_CKP_BE_HRQ_HDR] = "backend http-request header rule",
272 [SMP_CKP_BE_HRQ_BDY] = "backend http-request body rule",
273 [SMP_CKP_BE_SET_SRV] = "backend use-server, balance or stick-match rule",
274 [SMP_CKP_BE_SRV_CON] = "server source selection",
275 [SMP_CKP_BE_RES_CNT] = "backend tcp-response content rule",
276 [SMP_CKP_BE_HRS_HDR] = "backend http-response header rule",
277 [SMP_CKP_BE_HRS_BDY] = "backend http-response body rule",
278 [SMP_CKP_BE_STO_RUL] = "backend stick-store rule",
279 [SMP_CKP_FE_RES_CNT] = "frontend tcp-response content rule",
280 [SMP_CKP_FE_HRS_HDR] = "frontend http-response header rule",
281 [SMP_CKP_FE_HRS_BDY] = "frontend http-response body rule",
282 [SMP_CKP_FE_LOG_END] = "logs",
283};
284
Thierry FOURNIER348971e2013-11-21 10:50:10 +0100285/* This function returns the type of the data returned by the sample_expr.
286 * It assumes that the <expr> and all of its converters are properly
287 * initialized.
288 */
289inline
290int smp_expr_output_type(struct sample_expr *expr)
291{
292 struct sample_conv_expr *smp_expr;
293
294 if (!LIST_ISEMPTY(&expr->conv_exprs)) {
295 smp_expr = LIST_PREV(&expr->conv_exprs, struct sample_conv_expr *, list);
296 return smp_expr->conv->out_type;
297 }
298 return expr->fetch->out_type;
299}
300
301
Willy Tarreau80aca902013-01-07 15:42:20 +0100302/* fill the trash with a comma-delimited list of source names for the <use> bit
303 * field which must be composed of a non-null set of SMP_USE_* flags. The return
304 * value is the pointer to the string in the trash buffer.
305 */
306const char *sample_src_names(unsigned int use)
307{
308 int bit;
309
310 trash.len = 0;
311 trash.str[0] = '\0';
312 for (bit = 0; bit < SMP_SRC_ENTRIES; bit++) {
313 if (!(use & ~((1 << bit) - 1)))
314 break; /* no more bits */
315
316 if (!(use & (1 << bit)))
317 continue; /* bit not set */
318
319 trash.len += snprintf(trash.str + trash.len, trash.size - trash.len, "%s%s",
320 (use & ((1 << bit) - 1)) ? "," : "",
321 fetch_src_names[bit]);
322 }
323 return trash.str;
324}
325
Willy Tarreaubf8e2512013-03-25 14:52:41 +0100326/* return a pointer to the correct sample checkpoint name, or "unknown" when
327 * the flags are invalid. Only the lowest bit is used, higher bits are ignored
328 * if set.
329 */
330const char *sample_ckp_names(unsigned int use)
331{
332 int bit;
333
334 for (bit = 0; bit < SMP_CKP_ENTRIES; bit++)
335 if (use & (1 << bit))
336 return fetch_ckp_names[bit];
337 return "unknown sample check place, please report this bug";
338}
339
Emeric Brun107ca302010-01-04 16:16:05 +0100340/*
Willy Tarreau80aca902013-01-07 15:42:20 +0100341 * Registers the sample fetch keyword list <kwl> as a list of valid keywords
342 * for next parsing sessions. The fetch keywords capabilities are also computed
343 * from their ->use field.
Emeric Brun107ca302010-01-04 16:16:05 +0100344 */
Willy Tarreau80aca902013-01-07 15:42:20 +0100345void sample_register_fetches(struct sample_fetch_kw_list *kwl)
Emeric Brun107ca302010-01-04 16:16:05 +0100346{
Willy Tarreau80aca902013-01-07 15:42:20 +0100347 struct sample_fetch *sf;
348 int bit;
349
350 for (sf = kwl->kw; sf->kw != NULL; sf++) {
351 for (bit = 0; bit < SMP_SRC_ENTRIES; bit++)
352 if (sf->use & (1 << bit))
353 sf->val |= fetch_cap[bit];
354 }
355 LIST_ADDQ(&sample_fetches.list, &kwl->list);
Emeric Brun107ca302010-01-04 16:16:05 +0100356}
357
358/*
Willy Tarreau12785782012-04-27 21:37:17 +0200359 * Registers the sample format coverstion keyword list <pckl> as a list of valid keywords for next
Emeric Brun107ca302010-01-04 16:16:05 +0100360 * parsing sessions.
361 */
Willy Tarreau12785782012-04-27 21:37:17 +0200362void sample_register_convs(struct sample_conv_kw_list *pckl)
Emeric Brun107ca302010-01-04 16:16:05 +0100363{
Willy Tarreau12785782012-04-27 21:37:17 +0200364 LIST_ADDQ(&sample_convs.list, &pckl->list);
Emeric Brun107ca302010-01-04 16:16:05 +0100365}
366
367/*
Willy Tarreau12785782012-04-27 21:37:17 +0200368 * Returns the pointer on sample fetch keyword structure identified by
Emeric Brun107ca302010-01-04 16:16:05 +0100369 * string of <len> in buffer <kw>.
370 *
371 */
Willy Tarreau12785782012-04-27 21:37:17 +0200372struct sample_fetch *find_sample_fetch(const char *kw, int len)
Emeric Brun107ca302010-01-04 16:16:05 +0100373{
374 int index;
Willy Tarreau12785782012-04-27 21:37:17 +0200375 struct sample_fetch_kw_list *kwl;
Emeric Brun107ca302010-01-04 16:16:05 +0100376
Willy Tarreau12785782012-04-27 21:37:17 +0200377 list_for_each_entry(kwl, &sample_fetches.list, list) {
Emeric Brun107ca302010-01-04 16:16:05 +0100378 for (index = 0; kwl->kw[index].kw != NULL; index++) {
379 if (strncmp(kwl->kw[index].kw, kw, len) == 0 &&
380 kwl->kw[index].kw[len] == '\0')
381 return &kwl->kw[index];
382 }
383 }
384 return NULL;
385}
386
Thierry FOURNIER4d9a1d12014-12-08 14:49:19 +0100387/* This fucntion browse the list of available saple fetch. <current> is
388 * the last used sample fetch. If it is the first call, it must set to NULL.
389 * <idx> is the index of the next sampleèfetch entry. It is used as private
390 * value. It is useles to initiate it.
391 *
392 * It returns always the newt fetch_sample entry, and NULL when the end of
393 * the list is reached.
394 */
395struct sample_fetch *sample_fetch_getnext(struct sample_fetch *current, int *idx)
396{
397 struct sample_fetch_kw_list *kwl;
398 struct sample_fetch *base;
399
400 if (!current) {
401 /* Get first kwl entry. */
402 kwl = LIST_NEXT(&sample_fetches.list, struct sample_fetch_kw_list *, list);
403 (*idx) = 0;
404 } else {
405 /* Get kwl corresponding to the curret entry. */
406 base = current + 1 - (*idx);
407 kwl = container_of(base, struct sample_fetch_kw_list, kw);
408 }
409
410 while (1) {
411
412 /* Check if kwl is the last entry. */
413 if (&kwl->list == &sample_fetches.list)
414 return NULL;
415
416 /* idx contain the next keyword. If it is available, return it. */
417 if (kwl->kw[*idx].kw) {
418 (*idx)++;
419 return &kwl->kw[(*idx)-1];
420 }
421
422 /* get next entry in the main list, and return NULL if the end is reached. */
423 kwl = LIST_NEXT(&kwl->list, struct sample_fetch_kw_list *, list);
424
425 /* Set index to 0, ans do one other loop. */
426 (*idx) = 0;
427 }
428}
429
Thierry FOURNIER8fd13762015-03-10 23:56:48 +0100430/* This function browses the list of available converters. <current> is
431 * the last used converter. If it is the first call, it must set to NULL.
432 * <idx> is the index of the next converter entry. It is used as private
433 * value. It is useless to initiate it.
434 *
435 * It returns always the next sample_conv entry, and NULL when the end of
436 * the list is reached.
437 */
438struct sample_conv *sample_conv_getnext(struct sample_conv *current, int *idx)
439{
440 struct sample_conv_kw_list *kwl;
441 struct sample_conv *base;
442
443 if (!current) {
444 /* Get first kwl entry. */
445 kwl = LIST_NEXT(&sample_convs.list, struct sample_conv_kw_list *, list);
446 (*idx) = 0;
447 } else {
448 /* Get kwl corresponding to the curret entry. */
449 base = current + 1 - (*idx);
450 kwl = container_of(base, struct sample_conv_kw_list, kw);
451 }
452
453 while (1) {
454 /* Check if kwl is the last entry. */
455 if (&kwl->list == &sample_convs.list)
456 return NULL;
457
458 /* idx contain the next keyword. If it is available, return it. */
459 if (kwl->kw[*idx].kw) {
460 (*idx)++;
461 return &kwl->kw[(*idx)-1];
462 }
463
464 /* get next entry in the main list, and return NULL if the end is reached. */
465 kwl = LIST_NEXT(&kwl->list, struct sample_conv_kw_list *, list);
466
467 /* Set index to 0, ans do one other loop. */
468 (*idx) = 0;
469 }
470}
471
Emeric Brun107ca302010-01-04 16:16:05 +0100472/*
Willy Tarreau12785782012-04-27 21:37:17 +0200473 * Returns the pointer on sample format conversion keyword structure identified by
Emeric Brun107ca302010-01-04 16:16:05 +0100474 * string of <len> in buffer <kw>.
475 *
476 */
Willy Tarreau12785782012-04-27 21:37:17 +0200477struct sample_conv *find_sample_conv(const char *kw, int len)
Emeric Brun107ca302010-01-04 16:16:05 +0100478{
479 int index;
Willy Tarreau12785782012-04-27 21:37:17 +0200480 struct sample_conv_kw_list *kwl;
Emeric Brun107ca302010-01-04 16:16:05 +0100481
Willy Tarreau12785782012-04-27 21:37:17 +0200482 list_for_each_entry(kwl, &sample_convs.list, list) {
Emeric Brun107ca302010-01-04 16:16:05 +0100483 for (index = 0; kwl->kw[index].kw != NULL; index++) {
484 if (strncmp(kwl->kw[index].kw, kw, len) == 0 &&
485 kwl->kw[index].kw[len] == '\0')
486 return &kwl->kw[index];
487 }
488 }
489 return NULL;
490}
491
Emeric Brun107ca302010-01-04 16:16:05 +0100492/******************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +0200493/* Sample casts functions */
Willy Tarreaub8c8f1f2012-04-23 22:38:26 +0200494/* Note: these functions do *NOT* set the output type on the */
495/* sample, the caller is responsible for doing this on return. */
Emeric Brun107ca302010-01-04 16:16:05 +0100496/******************************************************************/
497
Willy Tarreau342acb42012-04-23 22:03:39 +0200498static int c_ip2int(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100499{
Willy Tarreau342acb42012-04-23 22:03:39 +0200500 smp->data.uint = ntohl(smp->data.ipv4.s_addr);
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100501 smp->type = SMP_T_UINT;
Emeric Brun107ca302010-01-04 16:16:05 +0100502 return 1;
503}
504
Willy Tarreau342acb42012-04-23 22:03:39 +0200505static int c_ip2str(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100506{
Willy Tarreau47ca5452012-12-23 20:22:19 +0100507 struct chunk *trash = get_trash_chunk();
Emeric Brun107ca302010-01-04 16:16:05 +0100508
Willy Tarreau342acb42012-04-23 22:03:39 +0200509 if (!inet_ntop(AF_INET, (void *)&smp->data.ipv4, trash->str, trash->size))
Emeric Brun107ca302010-01-04 16:16:05 +0100510 return 0;
511
512 trash->len = strlen(trash->str);
Willy Tarreau342acb42012-04-23 22:03:39 +0200513 smp->data.str = *trash;
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100514 smp->type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100515 smp->flags &= ~SMP_F_CONST;
Emeric Brun107ca302010-01-04 16:16:05 +0100516
517 return 1;
518}
519
Willy Tarreau342acb42012-04-23 22:03:39 +0200520static int c_ip2ipv6(struct sample *smp)
David du Colombier4f92d322011-03-24 11:09:31 +0100521{
Willy Tarreau342acb42012-04-23 22:03:39 +0200522 v4tov6(&smp->data.ipv6, &smp->data.ipv4);
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100523 smp->type = SMP_T_IPV6;
David du Colombier4f92d322011-03-24 11:09:31 +0100524 return 1;
525}
526
Willy Tarreau342acb42012-04-23 22:03:39 +0200527static int c_ipv62str(struct sample *smp)
David du Colombier4f92d322011-03-24 11:09:31 +0100528{
Willy Tarreau47ca5452012-12-23 20:22:19 +0100529 struct chunk *trash = get_trash_chunk();
David du Colombier4f92d322011-03-24 11:09:31 +0100530
Willy Tarreau342acb42012-04-23 22:03:39 +0200531 if (!inet_ntop(AF_INET6, (void *)&smp->data.ipv6, trash->str, trash->size))
David du Colombier4f92d322011-03-24 11:09:31 +0100532 return 0;
533
534 trash->len = strlen(trash->str);
Willy Tarreau342acb42012-04-23 22:03:39 +0200535 smp->data.str = *trash;
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100536 smp->type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100537 smp->flags &= ~SMP_F_CONST;
David du Colombier4f92d322011-03-24 11:09:31 +0100538 return 1;
539}
540
541/*
Willy Tarreau342acb42012-04-23 22:03:39 +0200542static int c_ipv62ip(struct sample *smp)
David du Colombier4f92d322011-03-24 11:09:31 +0100543{
Willy Tarreau342acb42012-04-23 22:03:39 +0200544 return v6tov4(&smp->data.ipv4, &smp->data.ipv6);
David du Colombier4f92d322011-03-24 11:09:31 +0100545}
546*/
547
Willy Tarreau342acb42012-04-23 22:03:39 +0200548static int c_int2ip(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100549{
Willy Tarreau342acb42012-04-23 22:03:39 +0200550 smp->data.ipv4.s_addr = htonl(smp->data.uint);
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100551 smp->type = SMP_T_IPV4;
Emeric Brun107ca302010-01-04 16:16:05 +0100552 return 1;
553}
554
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100555static int c_str2addr(struct sample *smp)
556{
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100557 if (!buf2ip(smp->data.str.str, smp->data.str.len, &smp->data.ipv4)) {
Thierry FOURNIERfd139902013-12-11 12:38:57 +0100558 if (!buf2ip6(smp->data.str.str, smp->data.str.len, &smp->data.ipv6))
559 return 0;
560 smp->type = SMP_T_IPV6;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100561 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIERfd139902013-12-11 12:38:57 +0100562 return 1;
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100563 }
564 smp->type = SMP_T_IPV4;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100565 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100566 return 1;
567}
568
Willy Tarreau342acb42012-04-23 22:03:39 +0200569static int c_str2ip(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100570{
Willy Tarreau342acb42012-04-23 22:03:39 +0200571 if (!buf2ip(smp->data.str.str, smp->data.str.len, &smp->data.ipv4))
Emeric Brun107ca302010-01-04 16:16:05 +0100572 return 0;
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100573 smp->type = SMP_T_IPV4;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100574 smp->flags &= ~SMP_F_CONST;
Emeric Brun107ca302010-01-04 16:16:05 +0100575 return 1;
576}
577
Willy Tarreau342acb42012-04-23 22:03:39 +0200578static int c_str2ipv6(struct sample *smp)
David du Colombier4f92d322011-03-24 11:09:31 +0100579{
Thierry FOURNIERfd139902013-12-11 12:38:57 +0100580 if (!buf2ip6(smp->data.str.str, smp->data.str.len, &smp->data.ipv6))
581 return 0;
582 smp->type = SMP_T_IPV6;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100583 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIERfd139902013-12-11 12:38:57 +0100584 return 1;
David du Colombier4f92d322011-03-24 11:09:31 +0100585}
586
Emeric Brun4b9e8022014-11-03 18:17:10 +0100587/*
588 * The NULL char always enforces the end of string if it is met.
589 * Data is never changed, so we can ignore the CONST case
Thierry FOURNIERe87cac12014-03-12 15:07:59 +0100590 */
Emeric Brun8ac33d92012-10-17 13:36:06 +0200591static int c_bin2str(struct sample *smp)
592{
Emeric Brun4b9e8022014-11-03 18:17:10 +0100593 int i;
Emeric Brun8ac33d92012-10-17 13:36:06 +0200594
Emeric Brun4b9e8022014-11-03 18:17:10 +0100595 for (i = 0; i < smp->data.str.len; i++) {
596 if (!smp->data.str.str[i]) {
597 smp->data.str.len = i;
Thierry FOURNIERe87cac12014-03-12 15:07:59 +0100598 break;
Emeric Brun4b9e8022014-11-03 18:17:10 +0100599 }
Emeric Brun8ac33d92012-10-17 13:36:06 +0200600 }
Emeric Brun8ac33d92012-10-17 13:36:06 +0200601 return 1;
602}
603
Willy Tarreau342acb42012-04-23 22:03:39 +0200604static int c_int2str(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100605{
Willy Tarreau47ca5452012-12-23 20:22:19 +0100606 struct chunk *trash = get_trash_chunk();
Emeric Brun107ca302010-01-04 16:16:05 +0100607 char *pos;
608
Willy Tarreau342acb42012-04-23 22:03:39 +0200609 pos = ultoa_r(smp->data.uint, trash->str, trash->size);
Emeric Brun107ca302010-01-04 16:16:05 +0100610
611 if (!pos)
612 return 0;
613
Emeric Brun485479d2010-09-23 18:02:19 +0200614 trash->size = trash->size - (pos - trash->str);
Emeric Brun107ca302010-01-04 16:16:05 +0100615 trash->str = pos;
616 trash->len = strlen(pos);
Willy Tarreau342acb42012-04-23 22:03:39 +0200617 smp->data.str = *trash;
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100618 smp->type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100619 smp->flags &= ~SMP_F_CONST;
Emeric Brun107ca302010-01-04 16:16:05 +0100620 return 1;
621}
622
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100623/* This function duplicates data and removes the flag "const". */
624int smp_dup(struct sample *smp)
Emeric Brun485479d2010-09-23 18:02:19 +0200625{
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100626 struct chunk *trash;
Emeric Brun485479d2010-09-23 18:02:19 +0200627
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100628 /* If the const flag is not set, we don't need to duplicate the
629 * pattern as it can be modified in place.
630 */
631 if (!(smp->flags & SMP_F_CONST))
632 return 1;
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100633
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100634 switch (smp->type) {
635 case SMP_T_BOOL:
636 case SMP_T_UINT:
637 case SMP_T_SINT:
638 case SMP_T_ADDR:
639 case SMP_T_IPV4:
640 case SMP_T_IPV6:
641 /* These type are not const. */
642 break;
643 case SMP_T_STR:
644 case SMP_T_BIN:
645 /* Duplicate data. */
646 trash = get_trash_chunk();
647 trash->len = smp->data.str.len < trash->size ? smp->data.str.len : trash->size;
648 memcpy(trash->str, smp->data.str.str, trash->len);
649 smp->data.str = *trash;
650 break;
651 default:
652 /* Other cases are unexpected. */
653 return 0;
654 }
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100655
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100656 /* remove const flag */
657 smp->flags &= ~SMP_F_CONST;
Emeric Brun485479d2010-09-23 18:02:19 +0200658 return 1;
659}
660
Thierry FOURNIER0e9af552013-12-14 14:55:04 +0100661int c_none(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100662{
663 return 1;
664}
665
Willy Tarreau342acb42012-04-23 22:03:39 +0200666static int c_str2int(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100667{
668 int i;
669 uint32_t ret = 0;
670
Thierry FOURNIER60bb0202014-01-27 18:20:48 +0100671 if (smp->data.str.len == 0)
672 return 0;
673
Willy Tarreau342acb42012-04-23 22:03:39 +0200674 for (i = 0; i < smp->data.str.len; i++) {
675 uint32_t val = smp->data.str.str[i] - '0';
Emeric Brun107ca302010-01-04 16:16:05 +0100676
Thierry FOURNIER60bb0202014-01-27 18:20:48 +0100677 if (val > 9) {
678 if (i == 0)
679 return 0;
Emeric Brun107ca302010-01-04 16:16:05 +0100680 break;
Thierry FOURNIER60bb0202014-01-27 18:20:48 +0100681 }
Emeric Brun107ca302010-01-04 16:16:05 +0100682
683 ret = ret * 10 + val;
684 }
685
Willy Tarreau342acb42012-04-23 22:03:39 +0200686 smp->data.uint = ret;
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100687 smp->type = SMP_T_UINT;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100688 smp->flags &= ~SMP_F_CONST;
Emeric Brun107ca302010-01-04 16:16:05 +0100689 return 1;
690}
691
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100692static int c_str2meth(struct sample *smp)
693{
694 enum http_meth_t meth;
695 int len;
696
697 meth = find_http_meth(smp->data.str.str, smp->data.str.len);
698 if (meth == HTTP_METH_OTHER) {
699 len = smp->data.str.len;
700 smp->data.meth.str.str = smp->data.str.str;
701 smp->data.meth.str.len = len;
702 }
703 else
704 smp->flags &= ~SMP_F_CONST;
705 smp->data.meth.meth = meth;
706 smp->type = SMP_T_METH;
707 return 1;
708}
709
710static int c_meth2str(struct sample *smp)
711{
712 int len;
713 enum http_meth_t meth;
714
715 if (smp->data.meth.meth == HTTP_METH_OTHER) {
716 /* The method is unknown. Copy the original pointer. */
717 len = smp->data.meth.str.len;
718 smp->data.str.str = smp->data.meth.str.str;
719 smp->data.str.len = len;
720 smp->type = SMP_T_STR;
721 }
722 else if (smp->data.meth.meth < HTTP_METH_OTHER) {
723 /* The method is known, copy the pointer containing the string. */
724 meth = smp->data.meth.meth;
725 smp->data.str.str = http_known_methods[meth].name;
726 smp->data.str.len = http_known_methods[meth].len;
727 smp->flags |= SMP_F_CONST;
728 smp->type = SMP_T_STR;
729 }
730 else {
731 /* Unknown method */
732 return 0;
733 }
734 return 1;
735}
736
Willy Tarreau9700e5c2014-07-15 21:03:26 +0200737static int c_addr2bin(struct sample *smp)
738{
739 struct chunk *chk = get_trash_chunk();
740
741 if (smp->type == SMP_T_IPV4) {
742 chk->len = 4;
743 memcpy(chk->str, &smp->data.ipv4, chk->len);
744 }
745 else if (smp->type == SMP_T_IPV6) {
746 chk->len = 16;
747 memcpy(chk->str, &smp->data.ipv6, chk->len);
748 }
749 else
750 return 0;
751
752 smp->data.str = *chk;
753 smp->type = SMP_T_BIN;
754 return 1;
755}
756
Willy Tarreaubbfd1a22014-07-15 21:19:08 +0200757static int c_int2bin(struct sample *smp)
758{
759 struct chunk *chk = get_trash_chunk();
760
761 *(unsigned int *)chk->str = htonl(smp->data.uint);
762 chk->len = 4;
763
764 smp->data.str = *chk;
765 smp->type = SMP_T_BIN;
766 return 1;
767}
768
Willy Tarreau9700e5c2014-07-15 21:03:26 +0200769
Emeric Brun107ca302010-01-04 16:16:05 +0100770/*****************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +0200771/* Sample casts matrix: */
772/* sample_casts[from type][to type] */
773/* NULL pointer used for impossible sample casts */
Emeric Brun107ca302010-01-04 16:16:05 +0100774/*****************************************************************/
Emeric Brun107ca302010-01-04 16:16:05 +0100775
Thierry FOURNIER8af6ff12013-11-21 10:53:12 +0100776sample_cast_fct sample_casts[SMP_TYPES][SMP_TYPES] = {
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100777/* to: BOOL UINT SINT ADDR IPV4 IPV6 STR BIN METH */
778/* from: BOOL */ { c_none, c_none, c_none, NULL, NULL, NULL, c_int2str, NULL, NULL, },
Willy Tarreaubbfd1a22014-07-15 21:19:08 +0200779/* UINT */ { c_none, c_none, c_none, c_int2ip, c_int2ip, NULL, c_int2str, c_int2bin, NULL, },
780/* SINT */ { c_none, c_none, c_none, c_int2ip, c_int2ip, NULL, c_int2str, c_int2bin, NULL, },
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100781/* ADDR */ { NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, },
Willy Tarreau9700e5c2014-07-15 21:03:26 +0200782/* IPV4 */ { NULL, c_ip2int, c_ip2int, c_none, c_none, c_ip2ipv6, c_ip2str, c_addr2bin, NULL, },
783/* IPV6 */ { NULL, NULL, NULL, c_none, NULL, c_none, c_ipv62str, c_addr2bin, NULL, },
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100784/* STR */ { c_str2int, c_str2int, c_str2int, c_str2addr, c_str2ip, c_str2ipv6, c_none, c_none, c_str2meth, },
785/* BIN */ { NULL, NULL, NULL, NULL, NULL, NULL, c_bin2str, c_none, c_str2meth, },
786/* METH */ { NULL, NULL, NULL, NULL, NULL, NULL, c_meth2str, c_meth2str, c_none, },
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200787};
Emeric Brun107ca302010-01-04 16:16:05 +0100788
Emeric Brun107ca302010-01-04 16:16:05 +0100789/*
Willy Tarreau12785782012-04-27 21:37:17 +0200790 * Parse a sample expression configuration:
Emeric Brun107ca302010-01-04 16:16:05 +0100791 * fetch keyword followed by format conversion keywords.
Willy Tarreau12785782012-04-27 21:37:17 +0200792 * Returns a pointer on allocated sample expression structure.
Willy Tarreaua4312fa2013-04-02 16:34:32 +0200793 * The caller must have set al->ctx.
Emeric Brun107ca302010-01-04 16:16:05 +0100794 */
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +0100795struct 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 +0100796{
Willy Tarreau833cc792013-07-24 15:34:19 +0200797 const char *begw; /* beginning of word */
798 const char *endw; /* end of word */
799 const char *endt; /* end of term */
Willy Tarreau12785782012-04-27 21:37:17 +0200800 struct sample_expr *expr;
801 struct sample_fetch *fetch;
802 struct sample_conv *conv;
Emeric Brun107ca302010-01-04 16:16:05 +0100803 unsigned long prev_type;
Willy Tarreau833cc792013-07-24 15:34:19 +0200804 char *fkw = NULL;
805 char *ckw = NULL;
Willy Tarreau689a1df2013-12-13 00:40:11 +0100806 int err_arg;
Emeric Brun107ca302010-01-04 16:16:05 +0100807
Willy Tarreau833cc792013-07-24 15:34:19 +0200808 begw = str[*idx];
809 for (endw = begw; *endw && *endw != '(' && *endw != ','; endw++);
810
811 if (endw == begw) {
Willy Tarreau975c1782013-12-12 23:16:54 +0100812 memprintf(err_msg, "missing fetch method");
Emeric Brun107ca302010-01-04 16:16:05 +0100813 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200814 }
Emeric Brun107ca302010-01-04 16:16:05 +0100815
Willy Tarreau833cc792013-07-24 15:34:19 +0200816 /* keep a copy of the current fetch keyword for error reporting */
817 fkw = my_strndup(begw, endw - begw);
Emeric Brun107ca302010-01-04 16:16:05 +0100818
Willy Tarreau833cc792013-07-24 15:34:19 +0200819 fetch = find_sample_fetch(begw, endw - begw);
820 if (!fetch) {
Willy Tarreau975c1782013-12-12 23:16:54 +0100821 memprintf(err_msg, "unknown fetch method '%s'", fkw);
Emeric Brun107ca302010-01-04 16:16:05 +0100822 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200823 }
Emeric Brun107ca302010-01-04 16:16:05 +0100824
Willy Tarreau833cc792013-07-24 15:34:19 +0200825 endt = endw;
826 if (*endt == '(') {
Willy Tarreau689a1df2013-12-13 00:40:11 +0100827 /* look for the end of this term and skip the opening parenthesis */
828 endt = ++endw;
Willy Tarreau833cc792013-07-24 15:34:19 +0200829 while (*endt && *endt != ')')
830 endt++;
831 if (*endt != ')') {
Willy Tarreau975c1782013-12-12 23:16:54 +0100832 memprintf(err_msg, "missing closing ')' after arguments to fetch keyword '%s'", fkw);
Willy Tarreau833cc792013-07-24 15:34:19 +0200833 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200834 }
Emeric Brun485479d2010-09-23 18:02:19 +0200835 }
Emeric Brun107ca302010-01-04 16:16:05 +0100836
Willy Tarreau833cc792013-07-24 15:34:19 +0200837 /* At this point, we have :
838 * - begw : beginning of the keyword
Willy Tarreau689a1df2013-12-13 00:40:11 +0100839 * - endw : end of the keyword, first character not part of keyword
840 * nor the opening parenthesis (so first character of args
841 * if present).
Willy Tarreau833cc792013-07-24 15:34:19 +0200842 * - endt : end of the term (=endw or last parenthesis if args are present)
843 */
844
845 if (fetch->out_type >= SMP_TYPES) {
Willy Tarreau975c1782013-12-12 23:16:54 +0100846 memprintf(err_msg, "returns type of fetch method '%s' is unknown", fkw);
Emeric Brun107ca302010-01-04 16:16:05 +0100847 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200848 }
Emeric Brun107ca302010-01-04 16:16:05 +0100849 prev_type = fetch->out_type;
Willy Tarreau833cc792013-07-24 15:34:19 +0200850
Willy Tarreau12785782012-04-27 21:37:17 +0200851 expr = calloc(1, sizeof(struct sample_expr));
Emeric Brun485479d2010-09-23 18:02:19 +0200852 if (!expr)
853 goto out_error;
Emeric Brun107ca302010-01-04 16:16:05 +0100854
855 LIST_INIT(&(expr->conv_exprs));
856 expr->fetch = fetch;
Willy Tarreau2e845be2012-10-19 19:49:09 +0200857 expr->arg_p = empty_arg_list;
Emeric Brun107ca302010-01-04 16:16:05 +0100858
Willy Tarreau689a1df2013-12-13 00:40:11 +0100859 /* Note that we call the argument parser even with an empty string,
860 * this allows it to automatically create entries for mandatory
861 * implicit arguments (eg: local proxy name).
862 */
863 al->kw = expr->fetch->kw;
864 al->conv = NULL;
865 if (make_arg_list(endw, endt - endw, fetch->arg_mask, &expr->arg_p, err_msg, NULL, &err_arg, al) < 0) {
866 memprintf(err_msg, "fetch method '%s' : %s", fkw, *err_msg);
867 goto out_error;
868 }
Willy Tarreau2e845be2012-10-19 19:49:09 +0200869
Willy Tarreau689a1df2013-12-13 00:40:11 +0100870 if (!expr->arg_p) {
871 expr->arg_p = empty_arg_list;
Emeric Brun485479d2010-09-23 18:02:19 +0200872 }
Willy Tarreau689a1df2013-12-13 00:40:11 +0100873 else if (fetch->val_args && !fetch->val_args(expr->arg_p, err_msg)) {
874 memprintf(err_msg, "invalid args in fetch method '%s' : %s", fkw, *err_msg);
Emeric Brun485479d2010-09-23 18:02:19 +0200875 goto out_error;
Emeric Brun107ca302010-01-04 16:16:05 +0100876 }
877
Willy Tarreau833cc792013-07-24 15:34:19 +0200878 /* Now process the converters if any. We have two supported syntaxes
879 * for the converters, which can be combined :
880 * - comma-delimited list of converters just after the keyword and args ;
881 * - one converter per keyword
882 * The combination allows to have each keyword being a comma-delimited
883 * series of converters.
884 *
885 * We want to process the former first, then the latter. For this we start
886 * from the beginning of the supposed place in the exiting conv chain, which
887 * starts at the last comma (endt).
888 */
889
890 while (1) {
Willy Tarreau12785782012-04-27 21:37:17 +0200891 struct sample_conv_expr *conv_expr;
Emeric Brun107ca302010-01-04 16:16:05 +0100892
Willy Tarreau833cc792013-07-24 15:34:19 +0200893 if (*endt == ')') /* skip last closing parenthesis */
894 endt++;
Emeric Brun107ca302010-01-04 16:16:05 +0100895
Willy Tarreau833cc792013-07-24 15:34:19 +0200896 if (*endt && *endt != ',') {
897 if (ckw)
Willy Tarreau975c1782013-12-12 23:16:54 +0100898 memprintf(err_msg, "missing comma after conv keyword '%s'", ckw);
Willy Tarreau833cc792013-07-24 15:34:19 +0200899 else
Willy Tarreau975c1782013-12-12 23:16:54 +0100900 memprintf(err_msg, "missing comma after fetch keyword '%s'", fkw);
Emeric Brun107ca302010-01-04 16:16:05 +0100901 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200902 }
Emeric Brun107ca302010-01-04 16:16:05 +0100903
Willy Tarreau833cc792013-07-24 15:34:19 +0200904 while (*endt == ',') /* then trailing commas */
905 endt++;
906
907 begw = endt; /* start of conv keyword */
908
909 if (!*begw) {
910 /* none ? skip to next string */
911 (*idx)++;
912 begw = str[*idx];
913 if (!begw || !*begw)
914 break;
915 }
916
917 for (endw = begw; *endw && *endw != '(' && *endw != ','; endw++);
918
919 free(ckw);
920 ckw = my_strndup(begw, endw - begw);
921
922 conv = find_sample_conv(begw, endw - begw);
923 if (!conv) {
924 /* we found an isolated keyword that we don't know, it's not ours */
925 if (begw == str[*idx])
926 break;
Willy Tarreau975c1782013-12-12 23:16:54 +0100927 memprintf(err_msg, "unknown conv method '%s'", ckw);
Willy Tarreau833cc792013-07-24 15:34:19 +0200928 goto out_error;
929 }
Emeric Brun107ca302010-01-04 16:16:05 +0100930
Willy Tarreau833cc792013-07-24 15:34:19 +0200931 endt = endw;
932 if (*endt == '(') {
933 /* look for the end of this term */
934 while (*endt && *endt != ')')
935 endt++;
936 if (*endt != ')') {
Willy Tarreau975c1782013-12-12 23:16:54 +0100937 memprintf(err_msg, "syntax error: missing ')' after conv keyword '%s'", ckw);
Willy Tarreau833cc792013-07-24 15:34:19 +0200938 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200939 }
Willy Tarreau833cc792013-07-24 15:34:19 +0200940 }
941
942 if (conv->in_type >= SMP_TYPES || conv->out_type >= SMP_TYPES) {
Willy Tarreau975c1782013-12-12 23:16:54 +0100943 memprintf(err_msg, "returns type of conv method '%s' is unknown", ckw);
Emeric Brun107ca302010-01-04 16:16:05 +0100944 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200945 }
Emeric Brun107ca302010-01-04 16:16:05 +0100946
947 /* If impossible type conversion */
Willy Tarreau12785782012-04-27 21:37:17 +0200948 if (!sample_casts[prev_type][conv->in_type]) {
Willy Tarreau975c1782013-12-12 23:16:54 +0100949 memprintf(err_msg, "conv method '%s' cannot be applied", ckw);
Emeric Brun107ca302010-01-04 16:16:05 +0100950 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200951 }
Emeric Brun107ca302010-01-04 16:16:05 +0100952
953 prev_type = conv->out_type;
Willy Tarreau12785782012-04-27 21:37:17 +0200954 conv_expr = calloc(1, sizeof(struct sample_conv_expr));
Emeric Brun485479d2010-09-23 18:02:19 +0200955 if (!conv_expr)
956 goto out_error;
Emeric Brun107ca302010-01-04 16:16:05 +0100957
958 LIST_ADDQ(&(expr->conv_exprs), &(conv_expr->list));
959 conv_expr->conv = conv;
960
Willy Tarreau833cc792013-07-24 15:34:19 +0200961 if (endt != endw) {
Willy Tarreaub27c0d32012-04-20 16:04:47 +0200962 int err_arg;
Willy Tarreau21d68a62012-04-20 15:52:36 +0200963
Willy Tarreau9fcb9842012-04-20 14:45:49 +0200964 if (!conv->arg_mask) {
Willy Tarreau975c1782013-12-12 23:16:54 +0100965 memprintf(err_msg, "conv method '%s' does not support any args", ckw);
Emeric Brun485479d2010-09-23 18:02:19 +0200966 goto out_error;
967 }
Willy Tarreau9e92d322010-01-26 17:58:06 +0100968
Willy Tarreaua4312fa2013-04-02 16:34:32 +0200969 al->kw = expr->fetch->kw;
970 al->conv = conv_expr->conv->kw;
Willy Tarreau975c1782013-12-12 23:16:54 +0100971 if (make_arg_list(endw + 1, endt - endw - 1, conv->arg_mask, &conv_expr->arg_p, err_msg, NULL, &err_arg, al) < 0) {
972 memprintf(err_msg, "invalid arg %d in conv method '%s' : %s", err_arg+1, ckw, *err_msg);
Willy Tarreau21d68a62012-04-20 15:52:36 +0200973 goto out_error;
974 }
975
Willy Tarreau2e845be2012-10-19 19:49:09 +0200976 if (!conv_expr->arg_p)
977 conv_expr->arg_p = empty_arg_list;
978
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +0100979 if (conv->val_args && !conv->val_args(conv_expr->arg_p, conv, file, line, err_msg)) {
Willy Tarreau975c1782013-12-12 23:16:54 +0100980 memprintf(err_msg, "invalid args in conv method '%s' : %s", ckw, *err_msg);
Emeric Brun485479d2010-09-23 18:02:19 +0200981 goto out_error;
982 }
983 }
Willy Tarreau833cc792013-07-24 15:34:19 +0200984 else if (ARGM(conv->arg_mask)) {
Willy Tarreau975c1782013-12-12 23:16:54 +0100985 memprintf(err_msg, "missing args for conv method '%s'", ckw);
Emeric Brun485479d2010-09-23 18:02:19 +0200986 goto out_error;
Emeric Brun107ca302010-01-04 16:16:05 +0100987 }
988 }
Emeric Brun485479d2010-09-23 18:02:19 +0200989
Willy Tarreau833cc792013-07-24 15:34:19 +0200990 out:
991 free(fkw);
992 free(ckw);
Emeric Brun107ca302010-01-04 16:16:05 +0100993 return expr;
994
995out_error:
Willy Tarreau12785782012-04-27 21:37:17 +0200996 /* TODO: prune_sample_expr(expr); */
Willy Tarreau833cc792013-07-24 15:34:19 +0200997 expr = NULL;
998 goto out;
Emeric Brun107ca302010-01-04 16:16:05 +0100999}
1000
1001/*
Willy Tarreau12785782012-04-27 21:37:17 +02001002 * Process a fetch + format conversion of defined by the sample expression <expr>
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001003 * on request or response considering the <opt> parameter.
Willy Tarreau12785782012-04-27 21:37:17 +02001004 * Returns a pointer on a typed sample structure containing the result or NULL if
1005 * sample is not found or when format conversion failed.
Emeric Brun107ca302010-01-04 16:16:05 +01001006 * If <p> is not null, function returns results in structure pointed by <p>.
Willy Tarreau12785782012-04-27 21:37:17 +02001007 * If <p> is null, functions returns a pointer on a static sample structure.
Willy Tarreaub8c8f1f2012-04-23 22:38:26 +02001008 *
1009 * Note: the fetch functions are required to properly set the return type. The
1010 * conversion functions must do so too. However the cast functions do not need
1011 * to since they're made to cast mutiple types according to what is required.
Willy Tarreau6bcb0a82014-07-30 08:56:35 +02001012 *
1013 * The caller may indicate in <opt> if it considers the result final or not.
1014 * The caller needs to check the SMP_F_MAY_CHANGE flag in p->flags to verify
1015 * if the result is stable or not, according to the following table :
1016 *
1017 * return MAY_CHANGE FINAL Meaning for the sample
1018 * NULL 0 * Not present and will never be (eg: header)
1019 * NULL 1 0 Not present yet, could change (eg: POST param)
1020 * NULL 1 1 Not present yet, will not change anymore
1021 * smp 0 * Present and will not change (eg: header)
1022 * smp 1 0 Present, may change (eg: request length)
1023 * smp 1 1 Present, last known value (eg: request length)
Emeric Brun107ca302010-01-04 16:16:05 +01001024 */
Willy Tarreau15e91e12015-04-04 00:52:09 +02001025struct sample *sample_process(struct proxy *px, struct stream *strm, unsigned int opt,
Willy Tarreau12785782012-04-27 21:37:17 +02001026 struct sample_expr *expr, struct sample *p)
Emeric Brun107ca302010-01-04 16:16:05 +01001027{
Willy Tarreau12785782012-04-27 21:37:17 +02001028 struct sample_conv_expr *conv_expr;
Emeric Brun107ca302010-01-04 16:16:05 +01001029
Willy Tarreau18387e22013-07-25 12:02:38 +02001030 if (p == NULL) {
Willy Tarreaub4a88f02012-04-23 21:35:11 +02001031 p = &temp_smp;
Willy Tarreau6c616e02014-06-25 16:56:41 +02001032 memset(p, 0, sizeof(*p));
Willy Tarreau18387e22013-07-25 12:02:38 +02001033 }
Emeric Brun107ca302010-01-04 16:16:05 +01001034
Willy Tarreau15e91e12015-04-04 00:52:09 +02001035 if (!expr->fetch->process(px, strm, opt, expr->arg_p, p, expr->fetch->kw, expr->fetch->private))
Emeric Brun107ca302010-01-04 16:16:05 +01001036 return NULL;
1037
Emeric Brun107ca302010-01-04 16:16:05 +01001038 list_for_each_entry(conv_expr, &expr->conv_exprs, list) {
Willy Tarreau12e50112012-04-25 17:21:49 +02001039 /* we want to ensure that p->type can be casted into
1040 * conv_expr->conv->in_type. We have 3 possibilities :
1041 * - NULL => not castable.
1042 * - c_none => nothing to do (let's optimize it)
1043 * - other => apply cast and prepare to fail
1044 */
Willy Tarreau12785782012-04-27 21:37:17 +02001045 if (!sample_casts[p->type][conv_expr->conv->in_type])
Willy Tarreau12e50112012-04-25 17:21:49 +02001046 return NULL;
1047
Willy Tarreau12785782012-04-27 21:37:17 +02001048 if (sample_casts[p->type][conv_expr->conv->in_type] != c_none &&
1049 !sample_casts[p->type][conv_expr->conv->in_type](p))
Emeric Brun107ca302010-01-04 16:16:05 +01001050 return NULL;
1051
Willy Tarreau12e50112012-04-25 17:21:49 +02001052 /* OK cast succeeded */
1053
Willy Tarreau15e91e12015-04-04 00:52:09 +02001054 if (!conv_expr->conv->process(strm, conv_expr->arg_p, p, conv_expr->conv->private))
Emeric Brun107ca302010-01-04 16:16:05 +01001055 return NULL;
Emeric Brun107ca302010-01-04 16:16:05 +01001056 }
1057 return p;
1058}
1059
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001060/*
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001061 * Resolve all remaining arguments in proxy <p>. Returns the number of
1062 * errors or 0 if everything is fine.
1063 */
1064int smp_resolve_args(struct proxy *p)
1065{
1066 struct arg_list *cur, *bak;
1067 const char *ctx, *where;
1068 const char *conv_ctx, *conv_pre, *conv_pos;
1069 struct userlist *ul;
Willy Tarreau46947782015-01-19 19:00:58 +01001070 struct my_regex *reg;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001071 struct arg *arg;
1072 int cfgerr = 0;
Willy Tarreau46947782015-01-19 19:00:58 +01001073 int rflags;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001074
1075 list_for_each_entry_safe(cur, bak, &p->conf.args.list, list) {
1076 struct proxy *px;
1077 struct server *srv;
1078 char *pname, *sname;
Willy Tarreau46947782015-01-19 19:00:58 +01001079 char *err;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001080
1081 arg = cur->arg;
1082
1083 /* prepare output messages */
1084 conv_pre = conv_pos = conv_ctx = "";
1085 if (cur->conv) {
1086 conv_ctx = cur->conv;
1087 conv_pre = "conversion keyword '";
1088 conv_pos = "' for ";
1089 }
1090
1091 where = "in";
1092 ctx = "sample fetch keyword";
1093 switch (cur->ctx) {
Willy Tarreau46947782015-01-19 19:00:58 +01001094 case ARGC_STK: where = "in stick rule in"; break;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001095 case ARGC_TRK: where = "in tracking rule in"; break;
1096 case ARGC_LOG: where = "in log-format string in"; break;
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +01001097 case ARGC_HRQ: where = "in http-request header format string in"; break;
1098 case ARGC_HRS: where = "in http-response header format string in"; break;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001099 case ARGC_UIF: where = "in unique-id-format string in"; break;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01001100 case ARGC_RDR: where = "in redirect format string in"; break;
Willy Tarreau3a4ac422014-06-13 16:17:14 +02001101 case ARGC_CAP: where = "in capture rule in"; break;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001102 case ARGC_ACL: ctx = "ACL keyword"; break;
1103 }
1104
1105 /* set a few default settings */
1106 px = p;
1107 pname = p->id;
1108
1109 switch (arg->type) {
1110 case ARGT_SRV:
1111 if (!arg->data.str.len) {
1112 Alert("parsing [%s:%d] : missing server name in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1113 cur->file, cur->line,
1114 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
1115 cfgerr++;
1116 continue;
1117 }
1118
1119 /* we support two formats : "bck/srv" and "srv" */
1120 sname = strrchr(arg->data.str.str, '/');
1121
1122 if (sname) {
1123 *sname++ = '\0';
1124 pname = arg->data.str.str;
1125
1126 px = findproxy(pname, PR_CAP_BE);
1127 if (!px) {
1128 Alert("parsing [%s:%d] : unable to find proxy '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1129 cur->file, cur->line, pname,
1130 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
1131 cfgerr++;
1132 break;
1133 }
1134 }
1135 else
1136 sname = arg->data.str.str;
1137
1138 srv = findserver(px, sname);
1139 if (!srv) {
1140 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",
1141 cur->file, cur->line, sname, pname,
1142 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
1143 cfgerr++;
1144 break;
1145 }
1146
1147 free(arg->data.str.str);
1148 arg->data.str.str = NULL;
1149 arg->unresolved = 0;
1150 arg->data.srv = srv;
1151 break;
1152
1153 case ARGT_FE:
1154 if (arg->data.str.len) {
1155 pname = arg->data.str.str;
1156 px = findproxy(pname, PR_CAP_FE);
1157 }
1158
1159 if (!px) {
1160 Alert("parsing [%s:%d] : unable to find frontend '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1161 cur->file, cur->line, pname,
1162 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
1163 cfgerr++;
1164 break;
1165 }
1166
1167 if (!(px->cap & PR_CAP_FE)) {
1168 Alert("parsing [%s:%d] : proxy '%s', referenced in arg %d of %s%s%s%s '%s' %s proxy '%s', has not frontend capability.\n",
1169 cur->file, cur->line, pname,
1170 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
1171 cfgerr++;
1172 break;
1173 }
1174
1175 free(arg->data.str.str);
1176 arg->data.str.str = NULL;
1177 arg->unresolved = 0;
1178 arg->data.prx = px;
1179 break;
1180
1181 case ARGT_BE:
1182 if (arg->data.str.len) {
1183 pname = arg->data.str.str;
1184 px = findproxy(pname, PR_CAP_BE);
1185 }
1186
1187 if (!px) {
1188 Alert("parsing [%s:%d] : unable to find backend '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1189 cur->file, cur->line, pname,
1190 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
1191 cfgerr++;
1192 break;
1193 }
1194
1195 if (!(px->cap & PR_CAP_BE)) {
1196 Alert("parsing [%s:%d] : proxy '%s', referenced in arg %d of %s%s%s%s '%s' %s proxy '%s', has not backend capability.\n",
1197 cur->file, cur->line, pname,
1198 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
1199 cfgerr++;
1200 break;
1201 }
1202
1203 free(arg->data.str.str);
1204 arg->data.str.str = NULL;
1205 arg->unresolved = 0;
1206 arg->data.prx = px;
1207 break;
1208
1209 case ARGT_TAB:
1210 if (arg->data.str.len) {
1211 pname = arg->data.str.str;
1212 px = find_stktable(pname);
1213 }
1214
1215 if (!px) {
1216 Alert("parsing [%s:%d] : unable to find table '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1217 cur->file, cur->line, pname,
1218 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
1219 cfgerr++;
1220 break;
1221 }
1222
1223 if (!px->table.size) {
1224 Alert("parsing [%s:%d] : no table in proxy '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1225 cur->file, cur->line, pname,
1226 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
1227 cfgerr++;
1228 break;
1229 }
1230
1231 free(arg->data.str.str);
1232 arg->data.str.str = NULL;
1233 arg->unresolved = 0;
1234 arg->data.prx = px;
1235 break;
1236
1237 case ARGT_USR:
1238 if (!arg->data.str.len) {
1239 Alert("parsing [%s:%d] : missing userlist name in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1240 cur->file, cur->line,
1241 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
1242 cfgerr++;
1243 break;
1244 }
1245
1246 if (p->uri_auth && p->uri_auth->userlist &&
1247 !strcmp(p->uri_auth->userlist->name, arg->data.str.str))
1248 ul = p->uri_auth->userlist;
1249 else
1250 ul = auth_find_userlist(arg->data.str.str);
1251
1252 if (!ul) {
1253 Alert("parsing [%s:%d] : unable to find userlist '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1254 cur->file, cur->line, arg->data.str.str,
1255 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
1256 cfgerr++;
1257 break;
1258 }
1259
1260 free(arg->data.str.str);
1261 arg->data.str.str = NULL;
1262 arg->unresolved = 0;
1263 arg->data.usr = ul;
1264 break;
Willy Tarreau46947782015-01-19 19:00:58 +01001265
1266 case ARGT_REG:
1267 if (!arg->data.str.len) {
1268 Alert("parsing [%s:%d] : missing regex in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1269 cur->file, cur->line,
1270 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
1271 cfgerr++;
1272 continue;
1273 }
1274
1275 reg = calloc(1, sizeof(*reg));
1276 if (!reg) {
1277 Alert("parsing [%s:%d] : not enough memory to build regex in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1278 cur->file, cur->line,
1279 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
1280 cfgerr++;
1281 continue;
1282 }
1283
1284 rflags = 0;
1285 rflags |= (arg->type_flags & ARGF_REG_ICASE) ? REG_ICASE : 0;
1286 err = NULL;
1287
Willy Tarreaud817e462015-01-23 20:23:17 +01001288 if (!regex_comp(arg->data.str.str, reg, !(rflags & REG_ICASE), 1 /* capture substr */, &err)) {
Willy Tarreau46947782015-01-19 19:00:58 +01001289 Alert("parsing [%s:%d] : error in regex '%s' in arg %d of %s%s%s%s '%s' %s proxy '%s' : %s.\n",
1290 cur->file, cur->line,
1291 arg->data.str.str,
1292 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id, err);
1293 cfgerr++;
1294 continue;
1295 }
1296
1297 free(arg->data.str.str);
1298 arg->data.str.str = NULL;
1299 arg->unresolved = 0;
1300 arg->data.reg = reg;
1301 break;
1302
1303
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001304 }
1305
1306 LIST_DEL(&cur->list);
1307 free(cur);
1308 } /* end of args processing */
1309
1310 return cfgerr;
1311}
1312
1313/*
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001314 * Process a fetch + format conversion as defined by the sample expression
1315 * <expr> on request or response considering the <opt> parameter. The output is
1316 * always of type string. If a stable sample can be fetched, or an unstable one
1317 * when <opt> contains SMP_OPT_FINAL, the sample is converted to a string and
1318 * returned without the SMP_F_MAY_CHANGE flag. If an unstable sample is found
1319 * and <opt> does not contain SMP_OPT_FINAL, then the sample is returned as-is
1320 * with its SMP_F_MAY_CHANGE flag so that the caller can check it and decide to
1321 * take actions (eg: wait longer). If a sample could not be found or could not
Willy Tarreau6bcb0a82014-07-30 08:56:35 +02001322 * be converted, NULL is returned. The caller MUST NOT use the sample if the
1323 * SMP_F_MAY_CHANGE flag is present, as it is used only as a hint that there is
1324 * still hope to get it after waiting longer, and is not converted to string.
1325 * The possible output combinations are the following :
1326 *
1327 * return MAY_CHANGE FINAL Meaning for the sample
1328 * NULL * * Not present and will never be (eg: header)
1329 * smp 0 * Final value converted (eg: header)
1330 * smp 1 0 Not present yet, may appear later (eg: header)
1331 * smp 1 1 never happens (either flag is cleared on output)
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001332 */
Willy Tarreau15e91e12015-04-04 00:52:09 +02001333struct sample *sample_fetch_string(struct proxy *px, struct stream *strm,
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001334 unsigned int opt, struct sample_expr *expr)
1335{
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001336 struct sample *smp = &temp_smp;
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001337
Willy Tarreau6c616e02014-06-25 16:56:41 +02001338 memset(smp, 0, sizeof(*smp));
1339
Willy Tarreau15e91e12015-04-04 00:52:09 +02001340 if (!sample_process(px, strm, opt, expr, smp)) {
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001341 if ((smp->flags & SMP_F_MAY_CHANGE) && !(opt & SMP_OPT_FINAL))
1342 return smp;
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001343 return NULL;
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001344 }
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001345
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01001346 if (!sample_casts[smp->type][SMP_T_STR])
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001347 return NULL;
1348
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01001349 if (!sample_casts[smp->type][SMP_T_STR](smp))
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001350 return NULL;
1351
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01001352 smp->type = SMP_T_STR;
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001353 smp->flags &= ~SMP_F_MAY_CHANGE;
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001354 return smp;
1355}
1356
Emeric Brun107ca302010-01-04 16:16:05 +01001357/*****************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +02001358/* Sample format convert functions */
Willy Tarreaub8c8f1f2012-04-23 22:38:26 +02001359/* These functions set the data type on return. */
Emeric Brun107ca302010-01-04 16:16:05 +01001360/*****************************************************************/
1361
Willy Tarreau87b09662015-04-03 00:22:06 +02001362static int sample_conv_bin2base64(struct stream *stream, const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01001363 struct sample *smp, void *private)
Emeric Brun53d1a982014-04-30 18:21:37 +02001364{
1365 struct chunk *trash = get_trash_chunk();
1366 int b64_len;
1367
1368 trash->len = 0;
1369 b64_len = a2base64(smp->data.str.str, smp->data.str.len, trash->str, trash->size);
1370 if (b64_len < 0)
1371 return 0;
1372
1373 trash->len = b64_len;
1374 smp->data.str = *trash;
1375 smp->type = SMP_T_STR;
1376 smp->flags &= ~SMP_F_CONST;
1377 return 1;
1378}
1379
Willy Tarreau87b09662015-04-03 00:22:06 +02001380static int sample_conv_bin2hex(struct stream *stream, const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01001381 struct sample *smp, void *private)
Thierry FOURNIER2f49d6d2014-03-12 15:01:52 +01001382{
1383 struct chunk *trash = get_trash_chunk();
1384 unsigned char c;
1385 int ptr = 0;
1386
1387 trash->len = 0;
1388 while (ptr < smp->data.str.len && trash->len <= trash->size - 2) {
1389 c = smp->data.str.str[ptr++];
1390 trash->str[trash->len++] = hextab[(c >> 4) & 0xF];
1391 trash->str[trash->len++] = hextab[c & 0xF];
1392 }
1393 smp->data.str = *trash;
1394 smp->type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01001395 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIER2f49d6d2014-03-12 15:01:52 +01001396 return 1;
1397}
1398
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001399/* hashes the binary input into a 32-bit unsigned int */
Willy Tarreau87b09662015-04-03 00:22:06 +02001400static int sample_conv_djb2(struct stream *stream, const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01001401 struct sample *smp, void *private)
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001402{
1403 smp->data.uint = hash_djb2(smp->data.str.str, smp->data.str.len);
1404 if (arg_p && arg_p->data.uint)
1405 smp->data.uint = full_hash(smp->data.uint);
1406 smp->type = SMP_T_UINT;
1407 return 1;
1408}
1409
Willy Tarreau87b09662015-04-03 00:22:06 +02001410static int sample_conv_str2lower(struct stream *stream, const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01001411 struct sample *smp, void *private)
Emeric Brun107ca302010-01-04 16:16:05 +01001412{
1413 int i;
1414
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01001415 if (!smp_dup(smp))
1416 return 0;
1417
Willy Tarreau342acb42012-04-23 22:03:39 +02001418 if (!smp->data.str.size)
Emeric Brun485479d2010-09-23 18:02:19 +02001419 return 0;
1420
Willy Tarreau342acb42012-04-23 22:03:39 +02001421 for (i = 0; i < smp->data.str.len; i++) {
1422 if ((smp->data.str.str[i] >= 'A') && (smp->data.str.str[i] <= 'Z'))
1423 smp->data.str.str[i] += 'a' - 'A';
Emeric Brun107ca302010-01-04 16:16:05 +01001424 }
1425 return 1;
1426}
1427
Willy Tarreau87b09662015-04-03 00:22:06 +02001428static int sample_conv_str2upper(struct stream *stream, const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01001429 struct sample *smp, void *private)
Emeric Brun107ca302010-01-04 16:16:05 +01001430{
1431 int i;
1432
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01001433 if (!smp_dup(smp))
1434 return 0;
1435
Willy Tarreau342acb42012-04-23 22:03:39 +02001436 if (!smp->data.str.size)
Emeric Brun485479d2010-09-23 18:02:19 +02001437 return 0;
1438
Willy Tarreau342acb42012-04-23 22:03:39 +02001439 for (i = 0; i < smp->data.str.len; i++) {
1440 if ((smp->data.str.str[i] >= 'a') && (smp->data.str.str[i] <= 'z'))
1441 smp->data.str.str[i] += 'A' - 'a';
Emeric Brun107ca302010-01-04 16:16:05 +01001442 }
1443 return 1;
1444}
1445
Willy Tarreauf9954102012-04-20 14:03:29 +02001446/* takes the netmask in arg_p */
Willy Tarreau87b09662015-04-03 00:22:06 +02001447static int sample_conv_ipmask(struct stream *stream, const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01001448 struct sample *smp, void *private)
Willy Tarreaud31d6eb2010-01-26 18:01:41 +01001449{
Willy Tarreau342acb42012-04-23 22:03:39 +02001450 smp->data.ipv4.s_addr &= arg_p->data.ipv4.s_addr;
Willy Tarreaub8c8f1f2012-04-23 22:38:26 +02001451 smp->type = SMP_T_IPV4;
Willy Tarreaud31d6eb2010-01-26 18:01:41 +01001452 return 1;
1453}
1454
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001455/* takes an UINT value on input supposed to represent the time since EPOCH,
1456 * adds an optional offset found in args[1] and emits a string representing
1457 * the local time in the format specified in args[1] using strftime().
1458 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001459static int sample_conv_ltime(struct stream *stream, const struct arg *args,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01001460 struct sample *smp, void *private)
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001461{
1462 struct chunk *temp;
1463 time_t curr_date = smp->data.uint;
1464
1465 /* add offset */
1466 if (args[1].type == ARGT_SINT || args[1].type == ARGT_UINT)
1467 curr_date += args[1].data.sint;
1468
1469 temp = get_trash_chunk();
1470 temp->len = strftime(temp->str, temp->size, args[0].data.str.str, localtime(&curr_date));
1471 smp->data.str = *temp;
1472 smp->type = SMP_T_STR;
1473 return 1;
1474}
1475
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001476/* hashes the binary input into a 32-bit unsigned int */
Willy Tarreau87b09662015-04-03 00:22:06 +02001477static int sample_conv_sdbm(struct stream *stream, const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01001478 struct sample *smp, void *private)
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001479{
1480 smp->data.uint = hash_sdbm(smp->data.str.str, smp->data.str.len);
1481 if (arg_p && arg_p->data.uint)
1482 smp->data.uint = full_hash(smp->data.uint);
1483 smp->type = SMP_T_UINT;
1484 return 1;
1485}
1486
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001487/* takes an UINT value on input supposed to represent the time since EPOCH,
1488 * adds an optional offset found in args[1] and emits a string representing
1489 * the UTC date in the format specified in args[1] using strftime().
1490 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001491static int sample_conv_utime(struct stream *stream, const struct arg *args,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01001492 struct sample *smp, void *private)
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001493{
1494 struct chunk *temp;
1495 time_t curr_date = smp->data.uint;
1496
1497 /* add offset */
1498 if (args[1].type == ARGT_SINT || args[1].type == ARGT_UINT)
1499 curr_date += args[1].data.sint;
1500
1501 temp = get_trash_chunk();
1502 temp->len = strftime(temp->str, temp->size, args[0].data.str.str, gmtime(&curr_date));
1503 smp->data.str = *temp;
1504 smp->type = SMP_T_STR;
1505 return 1;
1506}
1507
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001508/* hashes the binary input into a 32-bit unsigned int */
Willy Tarreau87b09662015-04-03 00:22:06 +02001509static int sample_conv_wt6(struct stream *stream, const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01001510 struct sample *smp, void *private)
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001511{
1512 smp->data.uint = hash_wt6(smp->data.str.str, smp->data.str.len);
1513 if (arg_p && arg_p->data.uint)
1514 smp->data.uint = full_hash(smp->data.uint);
1515 smp->type = SMP_T_UINT;
1516 return 1;
1517}
1518
Willy Tarreau80599772015-01-20 19:35:24 +01001519/* hashes the binary input into a 32-bit unsigned int */
Willy Tarreau87b09662015-04-03 00:22:06 +02001520static int sample_conv_crc32(struct stream *stream, const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01001521 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
Willy Tarreau87b09662015-04-03 00:22:06 +02001601static int sample_conv_json(struct stream *stream, const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01001602 struct sample *smp, void *private)
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001603{
1604 struct chunk *temp;
1605 char _str[7]; /* \u + 4 hex digit + null char for sprintf. */
1606 const char *str;
1607 int len;
1608 enum input_type input_type = IT_ASCII;
1609 unsigned int c;
1610 unsigned int ret;
1611 char *p;
1612
1613 if (arg_p)
1614 input_type = arg_p->data.uint;
1615
1616 temp = get_trash_chunk();
1617 temp->len = 0;
1618
1619 p = smp->data.str.str;
1620 while (p < smp->data.str.str + smp->data.str.len) {
1621
1622 if (input_type == IT_ASCII) {
1623 /* Read input as ASCII. */
1624 c = *(unsigned char *)p;
1625 p++;
1626 }
1627 else {
1628 /* Read input as UTF8. */
1629 ret = utf8_next(p, smp->data.str.len - ( p - smp->data.str.str ), &c);
1630 p += utf8_return_length(ret);
1631
1632 if (input_type == IT_UTF8 && utf8_return_code(ret) != UTF8_CODE_OK)
1633 return 0;
1634 if (input_type == IT_UTF8S && utf8_return_code(ret) != UTF8_CODE_OK)
1635 continue;
1636 if (input_type == IT_UTF8P && utf8_return_code(ret) & (UTF8_CODE_INVRANGE|UTF8_CODE_BADSEQ))
1637 return 0;
1638 if (input_type == IT_UTF8PS && utf8_return_code(ret) & (UTF8_CODE_INVRANGE|UTF8_CODE_BADSEQ))
1639 continue;
1640
1641 /* Check too big values. */
1642 if ((unsigned int)c > 0xffff) {
1643 if (input_type == IT_UTF8 || input_type == IT_UTF8P)
1644 return 0;
1645 continue;
1646 }
1647 }
1648
1649 /* Convert character. */
1650 if (c == '"') {
1651 len = 2;
1652 str = "\\\"";
1653 }
1654 else if (c == '\\') {
1655 len = 2;
1656 str = "\\\\";
1657 }
1658 else if (c == '/') {
1659 len = 2;
1660 str = "\\/";
1661 }
1662 else if (c == '\b') {
1663 len = 2;
1664 str = "\\b";
1665 }
1666 else if (c == '\f') {
1667 len = 2;
1668 str = "\\f";
1669 }
1670 else if (c == '\r') {
1671 len = 2;
1672 str = "\\r";
1673 }
1674 else if (c == '\n') {
1675 len = 2;
1676 str = "\\n";
1677 }
1678 else if (c == '\t') {
1679 len = 2;
1680 str = "\\t";
1681 }
1682 else if (c > 0xff || !isprint(c)) {
1683 /* isprint generate a segfault if c is too big. The man says that
1684 * c must have the value of an unsigned char or EOF.
1685 */
1686 len = 6;
1687 _str[0] = '\\';
1688 _str[1] = 'u';
1689 snprintf(&_str[2], 5, "%04x", (unsigned short)c);
1690 str = _str;
1691 }
1692 else {
1693 len = 1;
1694 str = (char *)&c;
1695 }
1696
1697 /* Check length */
1698 if (temp->len + len > temp->size)
1699 return 0;
1700
1701 /* Copy string. */
1702 memcpy(temp->str + temp->len, str, len);
1703 temp->len += len;
1704 }
1705
1706 smp->flags &= ~SMP_F_CONST;
1707 smp->data.str = *temp;
1708 smp->type = SMP_T_STR;
1709
1710 return 1;
1711}
1712
Emeric Brun54c4ac82014-11-03 15:32:43 +01001713/* This sample function is designed to extract some bytes from an input buffer.
1714 * First arg is the offset.
1715 * Optional second arg is the length to truncate */
Willy Tarreau87b09662015-04-03 00:22:06 +02001716static int sample_conv_bytes(struct stream *stream, const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01001717 struct sample *smp, void *private)
Emeric Brun54c4ac82014-11-03 15:32:43 +01001718{
1719 if (smp->data.str.len <= arg_p[0].data.uint) {
1720 smp->data.str.len = 0;
1721 return 1;
1722 }
1723
1724 if (smp->data.str.size)
1725 smp->data.str.size -= arg_p[0].data.uint;
1726 smp->data.str.len -= arg_p[0].data.uint;
1727 smp->data.str.str += arg_p[0].data.uint;
1728
1729 if ((arg_p[1].type == ARGT_UINT) && (arg_p[1].data.uint < smp->data.str.len))
1730 smp->data.str.len = arg_p[1].data.uint;
1731
1732 return 1;
1733}
1734
Emeric Brunf399b0d2014-11-03 17:07:03 +01001735static int sample_conv_field_check(struct arg *args, struct sample_conv *conv,
1736 const char *file, int line, char **err)
1737{
1738 struct arg *arg = args;
1739
1740 if (!arg) {
1741 memprintf(err, "Unexpected empty arg list");
1742 return 0;
1743 }
1744
1745 if (arg->type != ARGT_UINT) {
1746 memprintf(err, "Unexpected arg type");
1747 return 0;
1748 }
1749
1750 if (!arg->data.uint) {
1751 memprintf(err, "Unexpected value 0 for index");
1752 return 0;
1753 }
1754
1755 arg++;
1756
1757 if (arg->type != ARGT_STR) {
1758 memprintf(err, "Unexpected arg type");
1759 return 0;
1760 }
1761
1762 if (!arg->data.str.len) {
1763 memprintf(err, "Empty separators list");
1764 return 0;
1765 }
1766
1767 return 1;
1768}
1769
1770/* This sample function is designed to a return selected part of a string (field).
1771 * First arg is the index of the field (start at 1)
1772 * Second arg is a char list of separators (type string)
1773 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001774static int sample_conv_field(struct stream *stream, const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01001775 struct sample *smp, void *private)
Emeric Brunf399b0d2014-11-03 17:07:03 +01001776{
1777 unsigned int field;
1778 char *start, *end;
1779 int i;
1780
1781 if (!arg_p[0].data.uint)
1782 return 0;
1783
1784 field = 1;
1785 end = start = smp->data.str.str;
1786 while (end - smp->data.str.str < smp->data.str.len) {
1787
1788 for (i = 0 ; i < arg_p[1].data.str.len ; i++) {
1789 if (*end == arg_p[1].data.str.str[i]) {
1790 if (field == arg_p[0].data.uint)
1791 goto found;
1792 start = end+1;
1793 field++;
1794 break;
1795 }
1796 }
1797 end++;
1798 }
1799
1800 /* Field not found */
1801 if (field != arg_p[0].data.uint) {
1802 smp->data.str.len = 0;
1803 return 1;
1804 }
1805found:
1806 smp->data.str.len = end - start;
1807 /* If ret string is len 0, no need to
1808 change pointers or to update size */
1809 if (!smp->data.str.len)
1810 return 1;
1811
1812 smp->data.str.str = start;
1813
1814 /* Compute remaining size if needed
1815 Note: smp->data.str.size cannot be set to 0 */
1816 if (smp->data.str.size)
1817 smp->data.str.size -= start - smp->data.str.str;
1818
1819 return 1;
1820}
1821
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01001822/* This sample function is designed to return a word from a string.
1823 * First arg is the index of the word (start at 1)
1824 * Second arg is a char list of words separators (type string)
1825 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001826static int sample_conv_word(struct stream *stream, const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01001827 struct sample *smp, void *private)
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01001828{
1829 unsigned int word;
1830 char *start, *end;
1831 int i, issep, inword;
1832
1833 if (!arg_p[0].data.uint)
1834 return 0;
1835
1836 word = 0;
1837 inword = 0;
1838 end = start = smp->data.str.str;
1839 while (end - smp->data.str.str < smp->data.str.len) {
1840 issep = 0;
1841 for (i = 0 ; i < arg_p[1].data.str.len ; i++) {
1842 if (*end == arg_p[1].data.str.str[i]) {
1843 issep = 1;
1844 break;
1845 }
1846 }
1847 if (!inword) {
1848 if (!issep) {
1849 word++;
1850 start = end;
1851 inword = 1;
1852 }
1853 }
1854 else if (issep) {
1855 if (word == arg_p[0].data.uint)
1856 goto found;
1857 inword = 0;
1858 }
1859 end++;
1860 }
1861
1862 /* Field not found */
1863 if (word != arg_p[0].data.uint) {
1864 smp->data.str.len = 0;
1865 return 1;
1866 }
1867found:
1868 smp->data.str.len = end - start;
1869 /* If ret string is len 0, no need to
1870 change pointers or to update size */
1871 if (!smp->data.str.len)
1872 return 1;
1873
1874 smp->data.str.str = start;
1875
1876 /* Compute remaining size if needed
1877 Note: smp->data.str.size cannot be set to 0 */
1878 if (smp->data.str.size)
1879 smp->data.str.size -= start - smp->data.str.str;
1880
1881 return 1;
1882}
1883
Willy Tarreau7eda8492015-01-20 19:47:06 +01001884static int sample_conv_regsub_check(struct arg *args, struct sample_conv *conv,
1885 const char *file, int line, char **err)
1886{
1887 struct arg *arg = args;
1888 char *p;
1889 int len;
1890
1891 /* arg0 is a regex, it uses type_flag for ICASE and global match */
1892 arg[0].type_flags = 0;
1893
1894 if (arg[2].type != ARGT_STR)
1895 return 1;
1896
1897 p = arg[2].data.str.str;
1898 len = arg[2].data.str.len;
1899 while (len) {
1900 if (*p == 'i') {
1901 arg[0].type_flags |= ARGF_REG_ICASE;
1902 }
1903 else if (*p == 'g') {
1904 arg[0].type_flags |= ARGF_REG_GLOB;
1905 }
1906 else {
1907 memprintf(err, "invalid regex flag '%c', only 'i' and 'g' are supported", *p);
1908 return 0;
1909 }
1910 p++;
1911 len--;
1912 }
1913 return 1;
1914}
1915
1916/* This sample function is designed to do the equivalent of s/match/replace/ on
1917 * the input string. It applies a regex and restarts from the last matched
1918 * location until nothing matches anymore. First arg is the regex to apply to
1919 * the input string, second arg is the replacement expression.
1920 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001921static int sample_conv_regsub(struct stream *stream, const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01001922 struct sample *smp, void *private)
Willy Tarreau7eda8492015-01-20 19:47:06 +01001923{
1924 char *start, *end;
1925 struct my_regex *reg = arg_p[0].data.reg;
1926 regmatch_t pmatch[MAX_MATCH];
1927 struct chunk *trash = get_trash_chunk();
1928 int flag, max;
1929 int found;
1930
1931 start = smp->data.str.str;
1932 end = start + smp->data.str.len;
1933
1934 flag = 0;
1935 while (1) {
1936 /* check for last round which is used to copy remaining parts
1937 * when not running in global replacement mode.
1938 */
1939 found = 0;
1940 if ((arg_p[0].type_flags & ARGF_REG_GLOB) || !(flag & REG_NOTBOL)) {
1941 /* Note: we can have start == end on empty strings or at the end */
1942 found = regex_exec_match2(reg, start, end - start, MAX_MATCH, pmatch, flag);
1943 }
1944
1945 if (!found)
1946 pmatch[0].rm_so = end - start;
1947
1948 /* copy the heading non-matching part (which may also be the tail if nothing matches) */
1949 max = trash->size - trash->len;
1950 if (max && pmatch[0].rm_so > 0) {
1951 if (max > pmatch[0].rm_so)
1952 max = pmatch[0].rm_so;
1953 memcpy(trash->str + trash->len, start, max);
1954 trash->len += max;
1955 }
1956
1957 if (!found)
1958 break;
1959
1960 /* replace the matching part */
1961 max = trash->size - trash->len;
1962 if (max) {
1963 if (max > arg_p[1].data.str.len)
1964 max = arg_p[1].data.str.len;
1965 memcpy(trash->str + trash->len, arg_p[1].data.str.str, max);
1966 trash->len += max;
1967 }
1968
1969 /* stop here if we're done with this string */
1970 if (start >= end)
1971 break;
1972
1973 /* We have a special case for matches of length 0 (eg: "x*y*").
1974 * These ones are considered to match in front of a character,
1975 * so we have to copy that character and skip to the next one.
1976 */
1977 if (!pmatch[0].rm_eo) {
1978 if (trash->len < trash->size)
1979 trash->str[trash->len++] = start[pmatch[0].rm_eo];
1980 pmatch[0].rm_eo++;
1981 }
1982
1983 start += pmatch[0].rm_eo;
1984 flag |= REG_NOTBOL;
1985 }
1986
1987 smp->data.str = *trash;
1988 return 1;
1989}
1990
Willy Tarreau97707872015-01-27 15:12:13 +01001991/* Takes a UINT on input, applies a binary twos complement and returns the UINT
1992 * result.
1993 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001994static int sample_conv_binary_cpl(struct stream *stream, const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01001995 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01001996{
1997 smp->data.uint = ~smp->data.uint;
1998 return 1;
1999}
2000
2001/* Takes a UINT on input, applies a binary "and" with the UINT in arg_p, and
2002 * returns the UINT result.
2003 */
Willy Tarreau87b09662015-04-03 00:22:06 +02002004static int sample_conv_binary_and(struct stream *stream, const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002005 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002006{
2007 smp->data.uint &= arg_p->data.uint;
2008 return 1;
2009}
2010
2011/* Takes a UINT on input, applies a binary "or" with the UINT in arg_p, and
2012 * returns the UINT result.
2013 */
Willy Tarreau87b09662015-04-03 00:22:06 +02002014static int sample_conv_binary_or(struct stream *stream, const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002015 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002016{
2017 smp->data.uint |= arg_p->data.uint;
2018 return 1;
2019}
2020
2021/* Takes a UINT on input, applies a binary "xor" with the UINT in arg_p, and
2022 * returns the UINT result.
2023 */
Willy Tarreau87b09662015-04-03 00:22:06 +02002024static int sample_conv_binary_xor(struct stream *stream, const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002025 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 "add" with the UINT in arg_p,
2032 * and returns the UINT result.
2033 */
Willy Tarreau87b09662015-04-03 00:22:06 +02002034static int sample_conv_arith_add(struct stream *stream, 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 "sub" with the UINT in arg_p,
2042 * and returns the UINT result.
2043 */
Willy Tarreau87b09662015-04-03 00:22:06 +02002044static int sample_conv_arith_sub(struct stream *stream, 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 "mul" with the UINT in arg_p,
2052 * and returns the UINT result.
2053 */
Willy Tarreau87b09662015-04-03 00:22:06 +02002054static int sample_conv_arith_mul(struct stream *stream, const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002055 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002056{
2057 smp->data.uint *= arg_p->data.uint;
2058 return 1;
2059}
2060
2061/* Takes a UINT on input, applies an arithmetic "div" with the UINT in arg_p,
2062 * and returns the UINT result. If arg_p makes the result overflow, then the
2063 * largest possible quantity is returned.
2064 */
Willy Tarreau87b09662015-04-03 00:22:06 +02002065static int sample_conv_arith_div(struct stream *stream, const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002066 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002067{
2068 if (arg_p->data.uint)
2069 smp->data.uint /= arg_p->data.uint;
2070 else
2071 smp->data.uint = ~0;
2072 return 1;
2073}
2074
2075/* Takes a UINT on input, applies an arithmetic "mod" with the UINT in arg_p,
2076 * and returns the UINT result. If arg_p makes the result overflow, then zero
2077 * is returned.
2078 */
Willy Tarreau87b09662015-04-03 00:22:06 +02002079static int sample_conv_arith_mod(struct stream *stream, const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002080 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002081{
2082 if (arg_p->data.uint)
2083 smp->data.uint %= arg_p->data.uint;
2084 else
2085 smp->data.uint = 0;
2086 return 1;
2087}
2088
2089/* Takes an UINT on input, applies an arithmetic "neg" and returns the UINT
2090 * result.
2091 */
Willy Tarreau87b09662015-04-03 00:22:06 +02002092static int sample_conv_arith_neg(struct stream *stream, 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 return 1;
2097}
2098
2099/* Takes a UINT on input, returns true is the value is non-null, otherwise
2100 * false. The output is a BOOL.
2101 */
Willy Tarreau87b09662015-04-03 00:22:06 +02002102static int sample_conv_arith_bool(struct stream *stream, const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002103 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002104{
2105 smp->data.uint = !!smp->data.uint;
2106 smp->type = SMP_T_BOOL;
2107 return 1;
2108}
2109
2110/* Takes a UINT on input, returns false is the value is non-null, otherwise
2111 * truee. The output is a BOOL.
2112 */
Willy Tarreau87b09662015-04-03 00:22:06 +02002113static int sample_conv_arith_not(struct stream *stream, const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002114 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002115{
2116 smp->data.uint = !smp->data.uint;
2117 smp->type = SMP_T_BOOL;
2118 return 1;
2119}
2120
2121/* Takes a UINT on input, returns true is the value is odd, otherwise false.
2122 * The output is a BOOL.
2123 */
Willy Tarreau87b09662015-04-03 00:22:06 +02002124static int sample_conv_arith_odd(struct stream *stream, const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002125 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002126{
2127 smp->data.uint = smp->data.uint & 1;
2128 smp->type = SMP_T_BOOL;
2129 return 1;
2130}
2131
2132/* Takes a UINT on input, returns true is the value is even, otherwise false.
2133 * The output is a BOOL.
2134 */
Willy Tarreau87b09662015-04-03 00:22:06 +02002135static int sample_conv_arith_even(struct stream *stream, const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002136 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002137{
2138 smp->data.uint = !(smp->data.uint & 1);
2139 smp->type = SMP_T_BOOL;
2140 return 1;
2141}
2142
Willy Tarreau5b8ad222013-07-25 12:17:57 +02002143/************************************************************************/
2144/* All supported sample fetch functions must be declared here */
2145/************************************************************************/
2146
2147/* force TRUE to be returned at the fetch level */
2148static int
Willy Tarreau15e91e12015-04-04 00:52:09 +02002149smp_fetch_true(struct proxy *px, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01002150 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau5b8ad222013-07-25 12:17:57 +02002151{
2152 smp->type = SMP_T_BOOL;
2153 smp->data.uint = 1;
2154 return 1;
2155}
2156
2157/* force FALSE to be returned at the fetch level */
2158static int
Willy Tarreau15e91e12015-04-04 00:52:09 +02002159smp_fetch_false(struct proxy *px, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01002160 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau5b8ad222013-07-25 12:17:57 +02002161{
2162 smp->type = SMP_T_BOOL;
2163 smp->data.uint = 0;
2164 return 1;
2165}
2166
2167/* retrieve environment variable $1 as a string */
2168static int
Willy Tarreau15e91e12015-04-04 00:52:09 +02002169smp_fetch_env(struct proxy *px, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01002170 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau5b8ad222013-07-25 12:17:57 +02002171{
2172 char *env;
2173
2174 if (!args || args[0].type != ARGT_STR)
2175 return 0;
2176
2177 env = getenv(args[0].data.str.str);
2178 if (!env)
2179 return 0;
2180
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01002181 smp->type = SMP_T_STR;
2182 smp->flags = SMP_F_CONST;
Willy Tarreau5b8ad222013-07-25 12:17:57 +02002183 smp->data.str.str = env;
2184 smp->data.str.len = strlen(env);
2185 return 1;
2186}
2187
Willy Tarreau6236d3a2013-07-25 14:28:25 +02002188/* retrieve the current local date in epoch time, and applies an optional offset
2189 * of args[0] seconds.
2190 */
2191static int
Willy Tarreau15e91e12015-04-04 00:52:09 +02002192smp_fetch_date(struct proxy *px, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01002193 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau6236d3a2013-07-25 14:28:25 +02002194{
2195 smp->data.uint = date.tv_sec;
2196
2197 /* add offset */
2198 if (args && (args[0].type == ARGT_SINT || args[0].type == ARGT_UINT))
2199 smp->data.uint += args[0].data.sint;
2200
2201 smp->type = SMP_T_UINT;
2202 smp->flags |= SMP_F_VOL_TEST | SMP_F_MAY_CHANGE;
2203 return 1;
2204}
2205
Willy Tarreau0f30d262014-11-24 16:02:05 +01002206/* returns the number of processes */
2207static int
Willy Tarreau15e91e12015-04-04 00:52:09 +02002208smp_fetch_nbproc(struct proxy *px, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01002209 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau0f30d262014-11-24 16:02:05 +01002210{
2211 smp->type = SMP_T_UINT;
2212 smp->data.uint = global.nbproc;
2213 return 1;
2214}
2215
2216/* returns the number of the current process (between 1 and nbproc */
2217static int
Willy Tarreau15e91e12015-04-04 00:52:09 +02002218smp_fetch_proc(struct proxy *px, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01002219 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau0f30d262014-11-24 16:02:05 +01002220{
2221 smp->type = SMP_T_UINT;
2222 smp->data.uint = relative_pid;
2223 return 1;
2224}
2225
Willy Tarreau84310e22014-02-14 11:59:04 +01002226/* generate a random 32-bit integer for whatever purpose, with an optional
2227 * range specified in argument.
2228 */
2229static int
Willy Tarreau15e91e12015-04-04 00:52:09 +02002230smp_fetch_rand(struct proxy *px, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01002231 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau84310e22014-02-14 11:59:04 +01002232{
2233 smp->data.uint = random();
2234
2235 /* reduce if needed. Don't do a modulo, use all bits! */
2236 if (args && args[0].type == ARGT_UINT)
Vincent Bernat1228dc02014-12-10 10:31:37 +01002237 smp->data.uint = ((uint64_t)smp->data.uint * args[0].data.uint) / ((u64)RAND_MAX+1);
Willy Tarreau84310e22014-02-14 11:59:04 +01002238
2239 smp->type = SMP_T_UINT;
2240 smp->flags |= SMP_F_VOL_TEST | SMP_F_MAY_CHANGE;
2241 return 1;
2242}
2243
Willy Tarreau0f30d262014-11-24 16:02:05 +01002244/* returns true if the current process is stopping */
2245static int
Willy Tarreau15e91e12015-04-04 00:52:09 +02002246smp_fetch_stopping(struct proxy *px, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01002247 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau0f30d262014-11-24 16:02:05 +01002248{
2249 smp->type = SMP_T_BOOL;
2250 smp->data.uint = stopping;
2251 return 1;
2252}
2253
Willy Tarreau5b8ad222013-07-25 12:17:57 +02002254/* Note: must not be declared <const> as its list will be overwritten.
2255 * Note: fetches that may return multiple types must be declared as the lowest
2256 * common denominator, the type that can be casted into all other ones. For
2257 * instance IPv4/IPv6 must be declared IPv4.
2258 */
2259static struct sample_fetch_kw_list smp_kws = {ILH, {
2260 { "always_false", smp_fetch_false, 0, NULL, SMP_T_BOOL, SMP_USE_INTRN },
2261 { "always_true", smp_fetch_true, 0, NULL, SMP_T_BOOL, SMP_USE_INTRN },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01002262 { "env", smp_fetch_env, ARG1(1,STR), NULL, SMP_T_STR, SMP_USE_INTRN },
Willy Tarreau6236d3a2013-07-25 14:28:25 +02002263 { "date", smp_fetch_date, ARG1(0,SINT), NULL, SMP_T_UINT, SMP_USE_INTRN },
Willy Tarreau0f30d262014-11-24 16:02:05 +01002264 { "nbproc", smp_fetch_nbproc,0, NULL, SMP_T_UINT, SMP_USE_INTRN },
2265 { "proc", smp_fetch_proc, 0, NULL, SMP_T_UINT, SMP_USE_INTRN },
Willy Tarreau84310e22014-02-14 11:59:04 +01002266 { "rand", smp_fetch_rand, ARG1(0,UINT), NULL, SMP_T_UINT, SMP_USE_INTRN },
Willy Tarreau0f30d262014-11-24 16:02:05 +01002267 { "stopping", smp_fetch_stopping, 0, NULL, SMP_T_BOOL, SMP_USE_INTRN },
Willy Tarreau5b8ad222013-07-25 12:17:57 +02002268 { /* END */ },
2269}};
2270
Emeric Brun107ca302010-01-04 16:16:05 +01002271/* Note: must not be declared <const> as its list will be overwritten */
Willy Tarreaudc13c112013-06-21 23:16:39 +02002272static struct sample_conv_kw_list sample_conv_kws = {ILH, {
Emeric Brun53d1a982014-04-30 18:21:37 +02002273 { "base64", sample_conv_bin2base64,0, NULL, SMP_T_BIN, SMP_T_STR },
Willy Tarreau12785782012-04-27 21:37:17 +02002274 { "upper", sample_conv_str2upper, 0, NULL, SMP_T_STR, SMP_T_STR },
2275 { "lower", sample_conv_str2lower, 0, NULL, SMP_T_STR, SMP_T_STR },
Thierry FOURNIER2f49d6d2014-03-12 15:01:52 +01002276 { "hex", sample_conv_bin2hex, 0, NULL, SMP_T_BIN, SMP_T_STR },
Willy Tarreau12785782012-04-27 21:37:17 +02002277 { "ipmask", sample_conv_ipmask, ARG1(1,MSK4), NULL, SMP_T_IPV4, SMP_T_IPV4 },
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02002278 { "ltime", sample_conv_ltime, ARG2(1,STR,SINT), NULL, SMP_T_UINT, SMP_T_STR },
2279 { "utime", sample_conv_utime, ARG2(1,STR,SINT), NULL, SMP_T_UINT, SMP_T_STR },
Willy Tarreau80599772015-01-20 19:35:24 +01002280 { "crc32", sample_conv_crc32, ARG1(0,UINT), NULL, SMP_T_BIN, SMP_T_UINT },
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02002281 { "djb2", sample_conv_djb2, ARG1(0,UINT), NULL, SMP_T_BIN, SMP_T_UINT },
2282 { "sdbm", sample_conv_sdbm, ARG1(0,UINT), NULL, SMP_T_BIN, SMP_T_UINT },
2283 { "wt6", sample_conv_wt6, ARG1(0,UINT), NULL, SMP_T_BIN, SMP_T_UINT },
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002284 { "json", sample_conv_json, ARG1(1,STR), sample_conv_json_check, SMP_T_STR, SMP_T_STR },
Emeric Brun54c4ac82014-11-03 15:32:43 +01002285 { "bytes", sample_conv_bytes, ARG2(1,UINT,UINT), NULL, SMP_T_BIN, SMP_T_BIN },
Emeric Brunf399b0d2014-11-03 17:07:03 +01002286 { "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 +01002287 { "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 +01002288 { "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 +01002289
2290 { "and", sample_conv_binary_and, ARG1(1,UINT), NULL, SMP_T_UINT, SMP_T_UINT },
2291 { "or", sample_conv_binary_or, ARG1(1,UINT), NULL, SMP_T_UINT, SMP_T_UINT },
2292 { "xor", sample_conv_binary_xor, ARG1(1,UINT), NULL, SMP_T_UINT, SMP_T_UINT },
2293 { "cpl", sample_conv_binary_cpl, 0, NULL, SMP_T_UINT, SMP_T_UINT },
2294 { "bool", sample_conv_arith_bool, 0, NULL, SMP_T_UINT, SMP_T_BOOL },
2295 { "not", sample_conv_arith_not, 0, NULL, SMP_T_UINT, SMP_T_BOOL },
2296 { "odd", sample_conv_arith_odd, 0, NULL, SMP_T_UINT, SMP_T_BOOL },
2297 { "even", sample_conv_arith_even, 0, NULL, SMP_T_UINT, SMP_T_BOOL },
2298 { "add", sample_conv_arith_add, ARG1(1,UINT), NULL, SMP_T_UINT, SMP_T_UINT },
2299 { "sub", sample_conv_arith_sub, ARG1(1,UINT), NULL, SMP_T_UINT, SMP_T_UINT },
2300 { "mul", sample_conv_arith_mul, ARG1(1,UINT), NULL, SMP_T_UINT, SMP_T_UINT },
2301 { "div", sample_conv_arith_div, ARG1(1,UINT), NULL, SMP_T_UINT, SMP_T_UINT },
2302 { "mod", sample_conv_arith_mod, ARG1(1,UINT), NULL, SMP_T_UINT, SMP_T_UINT },
2303 { "neg", sample_conv_arith_neg, 0, NULL, SMP_T_UINT, SMP_T_UINT },
2304
Willy Tarreau9fcb9842012-04-20 14:45:49 +02002305 { NULL, NULL, 0, 0, 0 },
Emeric Brun107ca302010-01-04 16:16:05 +01002306}};
2307
2308__attribute__((constructor))
Willy Tarreau12785782012-04-27 21:37:17 +02002309static void __sample_init(void)
Emeric Brun107ca302010-01-04 16:16:05 +01002310{
Willy Tarreau5b8ad222013-07-25 12:17:57 +02002311 /* register sample fetch and format conversion keywords */
2312 sample_register_fetches(&smp_kws);
Willy Tarreau12785782012-04-27 21:37:17 +02002313 sample_register_convs(&sample_conv_kws);
Emeric Brun107ca302010-01-04 16:16:05 +01002314}