blob: 0a09012adca09fd20aae8c3d63e8377df308a1fa [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] = {
Thierry FOURNIER9c627e82015-06-03 20:12:35 +020037 [SMP_T_ANY] = "any",
Willy Tarreau1cf8f082014-02-07 12:14:54 +010038 [SMP_T_BOOL] = "bool",
39 [SMP_T_UINT] = "uint",
40 [SMP_T_SINT] = "sint",
41 [SMP_T_ADDR] = "addr",
42 [SMP_T_IPV4] = "ipv4",
43 [SMP_T_IPV6] = "ipv6",
44 [SMP_T_STR] = "str",
45 [SMP_T_BIN] = "bin",
Thierry FOURNIER4c2479e2015-06-03 20:12:04 +020046 [SMP_T_METH] = "meth",
Willy Tarreau1cf8f082014-02-07 12:14:54 +010047};
48
Willy Tarreau12785782012-04-27 21:37:17 +020049/* static sample used in sample_process() when <p> is NULL */
Willy Tarreaub4a88f02012-04-23 21:35:11 +020050static struct sample temp_smp;
Emeric Brun107ca302010-01-04 16:16:05 +010051
Willy Tarreau12785782012-04-27 21:37:17 +020052/* list head of all known sample fetch keywords */
53static struct sample_fetch_kw_list sample_fetches = {
54 .list = LIST_HEAD_INIT(sample_fetches.list)
Emeric Brun107ca302010-01-04 16:16:05 +010055};
56
Willy Tarreau12785782012-04-27 21:37:17 +020057/* list head of all known sample format conversion keywords */
58static struct sample_conv_kw_list sample_convs = {
59 .list = LIST_HEAD_INIT(sample_convs.list)
Emeric Brun107ca302010-01-04 16:16:05 +010060};
61
Willy Tarreau80aca902013-01-07 15:42:20 +010062const unsigned int fetch_cap[SMP_SRC_ENTRIES] = {
63 [SMP_SRC_INTRN] = (SMP_VAL_FE_CON_ACC | SMP_VAL_FE_SES_ACC | SMP_VAL_FE_REQ_CNT |
64 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
65 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
66 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
67 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
68 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
69 SMP_VAL_FE_LOG_END),
70
71 [SMP_SRC_LISTN] = (SMP_VAL_FE_CON_ACC | SMP_VAL_FE_SES_ACC | SMP_VAL_FE_REQ_CNT |
72 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
73 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
74 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
75 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
76 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
77 SMP_VAL_FE_LOG_END),
78
79 [SMP_SRC_FTEND] = (SMP_VAL_FE_CON_ACC | SMP_VAL_FE_SES_ACC | SMP_VAL_FE_REQ_CNT |
80 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
81 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
82 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
83 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
84 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
85 SMP_VAL_FE_LOG_END),
86
87 [SMP_SRC_L4CLI] = (SMP_VAL_FE_CON_ACC | SMP_VAL_FE_SES_ACC | SMP_VAL_FE_REQ_CNT |
88 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
89 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
90 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
91 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
92 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
93 SMP_VAL_FE_LOG_END),
94
95 [SMP_SRC_L5CLI] = (SMP_VAL___________ | SMP_VAL_FE_SES_ACC | SMP_VAL_FE_REQ_CNT |
96 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
97 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
98 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
99 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
100 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
101 SMP_VAL_FE_LOG_END),
102
103 [SMP_SRC_TRACK] = (SMP_VAL_FE_CON_ACC | SMP_VAL_FE_SES_ACC | SMP_VAL_FE_REQ_CNT |
104 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
105 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
106 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
107 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
108 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
109 SMP_VAL_FE_LOG_END),
110
111 [SMP_SRC_L6REQ] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_FE_REQ_CNT |
112 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
113 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
114 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL___________ |
115 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
116 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
117 SMP_VAL___________),
118
119 [SMP_SRC_HRQHV] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_FE_REQ_CNT |
120 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
121 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
122 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL___________ |
123 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
124 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
125 SMP_VAL___________),
126
127 [SMP_SRC_HRQHP] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_FE_REQ_CNT |
128 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
129 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
130 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
131 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
132 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
133 SMP_VAL_FE_LOG_END),
134
135 [SMP_SRC_HRQBO] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
136 SMP_VAL___________ | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
137 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
138 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL___________ |
139 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
140 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
141 SMP_VAL___________),
142
143 [SMP_SRC_BKEND] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
144 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
145 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
146 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
147 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
148 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
149 SMP_VAL_FE_LOG_END),
150
151 [SMP_SRC_SERVR] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
152 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
153 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
154 SMP_VAL___________ | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
155 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
156 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
157 SMP_VAL_FE_LOG_END),
158
159 [SMP_SRC_L4SRV] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
160 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
161 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
162 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_BE_RES_CNT |
163 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
164 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
165 SMP_VAL_FE_LOG_END),
166
167 [SMP_SRC_L5SRV] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
168 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
169 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
170 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_BE_RES_CNT |
171 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
172 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
173 SMP_VAL_FE_LOG_END),
174
175 [SMP_SRC_L6RES] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
176 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
177 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
178 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_BE_RES_CNT |
179 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
180 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
181 SMP_VAL___________),
182
183 [SMP_SRC_HRSHV] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
184 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
185 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
186 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_BE_RES_CNT |
187 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
188 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
189 SMP_VAL___________),
190
191 [SMP_SRC_HRSHP] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
192 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
193 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
194 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_BE_RES_CNT |
195 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
196 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
197 SMP_VAL_FE_LOG_END),
198
199 [SMP_SRC_HRSBO] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
200 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
201 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
202 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
203 SMP_VAL___________ | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
204 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
205 SMP_VAL___________),
206
207 [SMP_SRC_RQFIN] = (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___________ | SMP_VAL___________ | SMP_VAL___________ |
212 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
213 SMP_VAL_FE_LOG_END),
214
215 [SMP_SRC_RSFIN] = (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___________ | SMP_VAL___________ | SMP_VAL___________ |
220 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
221 SMP_VAL_FE_LOG_END),
222
223 [SMP_SRC_TXFIN] = (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___________ | SMP_VAL___________ | SMP_VAL___________ |
228 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
229 SMP_VAL_FE_LOG_END),
230
231 [SMP_SRC_SSFIN] = (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___________ | SMP_VAL___________ | SMP_VAL___________ |
236 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
237 SMP_VAL_FE_LOG_END),
238};
239
240static const char *fetch_src_names[SMP_SRC_ENTRIES] = {
241 [SMP_SRC_INTRN] = "internal state",
242 [SMP_SRC_LISTN] = "listener",
243 [SMP_SRC_FTEND] = "frontend",
244 [SMP_SRC_L4CLI] = "client address",
245 [SMP_SRC_L5CLI] = "client-side connection",
246 [SMP_SRC_TRACK] = "track counters",
247 [SMP_SRC_L6REQ] = "request buffer",
248 [SMP_SRC_HRQHV] = "HTTP request headers",
249 [SMP_SRC_HRQHP] = "HTTP request",
250 [SMP_SRC_HRQBO] = "HTTP request body",
251 [SMP_SRC_BKEND] = "backend",
252 [SMP_SRC_SERVR] = "server",
253 [SMP_SRC_L4SRV] = "server address",
254 [SMP_SRC_L5SRV] = "server-side connection",
255 [SMP_SRC_L6RES] = "response buffer",
256 [SMP_SRC_HRSHV] = "HTTP response headers",
257 [SMP_SRC_HRSHP] = "HTTP response",
258 [SMP_SRC_HRSBO] = "HTTP response body",
259 [SMP_SRC_RQFIN] = "request buffer statistics",
260 [SMP_SRC_RSFIN] = "response buffer statistics",
261 [SMP_SRC_TXFIN] = "transaction statistics",
262 [SMP_SRC_SSFIN] = "session statistics",
263};
264
Willy Tarreaubf8e2512013-03-25 14:52:41 +0100265static const char *fetch_ckp_names[SMP_CKP_ENTRIES] = {
266 [SMP_CKP_FE_CON_ACC] = "frontend tcp-request connection rule",
267 [SMP_CKP_FE_SES_ACC] = "frontend tcp-request session rule",
268 [SMP_CKP_FE_REQ_CNT] = "frontend tcp-request content rule",
269 [SMP_CKP_FE_HRQ_HDR] = "frontend http-request header rule",
270 [SMP_CKP_FE_HRQ_BDY] = "frontend http-request body rule",
271 [SMP_CKP_FE_SET_BCK] = "frontend use-backend rule",
272 [SMP_CKP_BE_REQ_CNT] = "backend tcp-request content rule",
273 [SMP_CKP_BE_HRQ_HDR] = "backend http-request header rule",
274 [SMP_CKP_BE_HRQ_BDY] = "backend http-request body rule",
275 [SMP_CKP_BE_SET_SRV] = "backend use-server, balance or stick-match rule",
276 [SMP_CKP_BE_SRV_CON] = "server source selection",
277 [SMP_CKP_BE_RES_CNT] = "backend tcp-response content rule",
278 [SMP_CKP_BE_HRS_HDR] = "backend http-response header rule",
279 [SMP_CKP_BE_HRS_BDY] = "backend http-response body rule",
280 [SMP_CKP_BE_STO_RUL] = "backend stick-store rule",
281 [SMP_CKP_FE_RES_CNT] = "frontend tcp-response content rule",
282 [SMP_CKP_FE_HRS_HDR] = "frontend http-response header rule",
283 [SMP_CKP_FE_HRS_BDY] = "frontend http-response body rule",
284 [SMP_CKP_FE_LOG_END] = "logs",
285};
286
Thierry FOURNIER348971e2013-11-21 10:50:10 +0100287/* This function returns the type of the data returned by the sample_expr.
288 * It assumes that the <expr> and all of its converters are properly
289 * initialized.
290 */
291inline
292int smp_expr_output_type(struct sample_expr *expr)
293{
294 struct sample_conv_expr *smp_expr;
295
296 if (!LIST_ISEMPTY(&expr->conv_exprs)) {
297 smp_expr = LIST_PREV(&expr->conv_exprs, struct sample_conv_expr *, list);
298 return smp_expr->conv->out_type;
299 }
300 return expr->fetch->out_type;
301}
302
303
Willy Tarreau80aca902013-01-07 15:42:20 +0100304/* fill the trash with a comma-delimited list of source names for the <use> bit
305 * field which must be composed of a non-null set of SMP_USE_* flags. The return
306 * value is the pointer to the string in the trash buffer.
307 */
308const char *sample_src_names(unsigned int use)
309{
310 int bit;
311
312 trash.len = 0;
313 trash.str[0] = '\0';
314 for (bit = 0; bit < SMP_SRC_ENTRIES; bit++) {
315 if (!(use & ~((1 << bit) - 1)))
316 break; /* no more bits */
317
318 if (!(use & (1 << bit)))
319 continue; /* bit not set */
320
321 trash.len += snprintf(trash.str + trash.len, trash.size - trash.len, "%s%s",
322 (use & ((1 << bit) - 1)) ? "," : "",
323 fetch_src_names[bit]);
324 }
325 return trash.str;
326}
327
Willy Tarreaubf8e2512013-03-25 14:52:41 +0100328/* return a pointer to the correct sample checkpoint name, or "unknown" when
329 * the flags are invalid. Only the lowest bit is used, higher bits are ignored
330 * if set.
331 */
332const char *sample_ckp_names(unsigned int use)
333{
334 int bit;
335
336 for (bit = 0; bit < SMP_CKP_ENTRIES; bit++)
337 if (use & (1 << bit))
338 return fetch_ckp_names[bit];
339 return "unknown sample check place, please report this bug";
340}
341
Emeric Brun107ca302010-01-04 16:16:05 +0100342/*
Willy Tarreau80aca902013-01-07 15:42:20 +0100343 * Registers the sample fetch keyword list <kwl> as a list of valid keywords
344 * for next parsing sessions. The fetch keywords capabilities are also computed
345 * from their ->use field.
Emeric Brun107ca302010-01-04 16:16:05 +0100346 */
Willy Tarreau80aca902013-01-07 15:42:20 +0100347void sample_register_fetches(struct sample_fetch_kw_list *kwl)
Emeric Brun107ca302010-01-04 16:16:05 +0100348{
Willy Tarreau80aca902013-01-07 15:42:20 +0100349 struct sample_fetch *sf;
350 int bit;
351
352 for (sf = kwl->kw; sf->kw != NULL; sf++) {
353 for (bit = 0; bit < SMP_SRC_ENTRIES; bit++)
354 if (sf->use & (1 << bit))
355 sf->val |= fetch_cap[bit];
356 }
357 LIST_ADDQ(&sample_fetches.list, &kwl->list);
Emeric Brun107ca302010-01-04 16:16:05 +0100358}
359
360/*
Willy Tarreau12785782012-04-27 21:37:17 +0200361 * Registers the sample format coverstion keyword list <pckl> as a list of valid keywords for next
Emeric Brun107ca302010-01-04 16:16:05 +0100362 * parsing sessions.
363 */
Willy Tarreau12785782012-04-27 21:37:17 +0200364void sample_register_convs(struct sample_conv_kw_list *pckl)
Emeric Brun107ca302010-01-04 16:16:05 +0100365{
Willy Tarreau12785782012-04-27 21:37:17 +0200366 LIST_ADDQ(&sample_convs.list, &pckl->list);
Emeric Brun107ca302010-01-04 16:16:05 +0100367}
368
369/*
Willy Tarreau12785782012-04-27 21:37:17 +0200370 * Returns the pointer on sample fetch keyword structure identified by
Emeric Brun107ca302010-01-04 16:16:05 +0100371 * string of <len> in buffer <kw>.
372 *
373 */
Willy Tarreau12785782012-04-27 21:37:17 +0200374struct sample_fetch *find_sample_fetch(const char *kw, int len)
Emeric Brun107ca302010-01-04 16:16:05 +0100375{
376 int index;
Willy Tarreau12785782012-04-27 21:37:17 +0200377 struct sample_fetch_kw_list *kwl;
Emeric Brun107ca302010-01-04 16:16:05 +0100378
Willy Tarreau12785782012-04-27 21:37:17 +0200379 list_for_each_entry(kwl, &sample_fetches.list, list) {
Emeric Brun107ca302010-01-04 16:16:05 +0100380 for (index = 0; kwl->kw[index].kw != NULL; index++) {
381 if (strncmp(kwl->kw[index].kw, kw, len) == 0 &&
382 kwl->kw[index].kw[len] == '\0')
383 return &kwl->kw[index];
384 }
385 }
386 return NULL;
387}
388
Thierry FOURNIER4d9a1d12014-12-08 14:49:19 +0100389/* This fucntion browse the list of available saple fetch. <current> is
390 * the last used sample fetch. If it is the first call, it must set to NULL.
391 * <idx> is the index of the next sampleèfetch entry. It is used as private
392 * value. It is useles to initiate it.
393 *
394 * It returns always the newt fetch_sample entry, and NULL when the end of
395 * the list is reached.
396 */
397struct sample_fetch *sample_fetch_getnext(struct sample_fetch *current, int *idx)
398{
399 struct sample_fetch_kw_list *kwl;
400 struct sample_fetch *base;
401
402 if (!current) {
403 /* Get first kwl entry. */
404 kwl = LIST_NEXT(&sample_fetches.list, struct sample_fetch_kw_list *, list);
405 (*idx) = 0;
406 } else {
407 /* Get kwl corresponding to the curret entry. */
408 base = current + 1 - (*idx);
409 kwl = container_of(base, struct sample_fetch_kw_list, kw);
410 }
411
412 while (1) {
413
414 /* Check if kwl is the last entry. */
415 if (&kwl->list == &sample_fetches.list)
416 return NULL;
417
418 /* idx contain the next keyword. If it is available, return it. */
419 if (kwl->kw[*idx].kw) {
420 (*idx)++;
421 return &kwl->kw[(*idx)-1];
422 }
423
424 /* get next entry in the main list, and return NULL if the end is reached. */
425 kwl = LIST_NEXT(&kwl->list, struct sample_fetch_kw_list *, list);
426
427 /* Set index to 0, ans do one other loop. */
428 (*idx) = 0;
429 }
430}
431
Thierry FOURNIER8fd13762015-03-10 23:56:48 +0100432/* This function browses the list of available converters. <current> is
433 * the last used converter. If it is the first call, it must set to NULL.
434 * <idx> is the index of the next converter entry. It is used as private
435 * value. It is useless to initiate it.
436 *
437 * It returns always the next sample_conv entry, and NULL when the end of
438 * the list is reached.
439 */
440struct sample_conv *sample_conv_getnext(struct sample_conv *current, int *idx)
441{
442 struct sample_conv_kw_list *kwl;
443 struct sample_conv *base;
444
445 if (!current) {
446 /* Get first kwl entry. */
447 kwl = LIST_NEXT(&sample_convs.list, struct sample_conv_kw_list *, list);
448 (*idx) = 0;
449 } else {
450 /* Get kwl corresponding to the curret entry. */
451 base = current + 1 - (*idx);
452 kwl = container_of(base, struct sample_conv_kw_list, kw);
453 }
454
455 while (1) {
456 /* Check if kwl is the last entry. */
457 if (&kwl->list == &sample_convs.list)
458 return NULL;
459
460 /* idx contain the next keyword. If it is available, return it. */
461 if (kwl->kw[*idx].kw) {
462 (*idx)++;
463 return &kwl->kw[(*idx)-1];
464 }
465
466 /* get next entry in the main list, and return NULL if the end is reached. */
467 kwl = LIST_NEXT(&kwl->list, struct sample_conv_kw_list *, list);
468
469 /* Set index to 0, ans do one other loop. */
470 (*idx) = 0;
471 }
472}
473
Emeric Brun107ca302010-01-04 16:16:05 +0100474/*
Willy Tarreau12785782012-04-27 21:37:17 +0200475 * Returns the pointer on sample format conversion keyword structure identified by
Emeric Brun107ca302010-01-04 16:16:05 +0100476 * string of <len> in buffer <kw>.
477 *
478 */
Willy Tarreau12785782012-04-27 21:37:17 +0200479struct sample_conv *find_sample_conv(const char *kw, int len)
Emeric Brun107ca302010-01-04 16:16:05 +0100480{
481 int index;
Willy Tarreau12785782012-04-27 21:37:17 +0200482 struct sample_conv_kw_list *kwl;
Emeric Brun107ca302010-01-04 16:16:05 +0100483
Willy Tarreau12785782012-04-27 21:37:17 +0200484 list_for_each_entry(kwl, &sample_convs.list, list) {
Emeric Brun107ca302010-01-04 16:16:05 +0100485 for (index = 0; kwl->kw[index].kw != NULL; index++) {
486 if (strncmp(kwl->kw[index].kw, kw, len) == 0 &&
487 kwl->kw[index].kw[len] == '\0')
488 return &kwl->kw[index];
489 }
490 }
491 return NULL;
492}
493
Emeric Brun107ca302010-01-04 16:16:05 +0100494/******************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +0200495/* Sample casts functions */
Willy Tarreaub8c8f1f2012-04-23 22:38:26 +0200496/* Note: these functions do *NOT* set the output type on the */
497/* sample, the caller is responsible for doing this on return. */
Emeric Brun107ca302010-01-04 16:16:05 +0100498/******************************************************************/
499
Willy Tarreau342acb42012-04-23 22:03:39 +0200500static int c_ip2int(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100501{
Willy Tarreau342acb42012-04-23 22:03:39 +0200502 smp->data.uint = ntohl(smp->data.ipv4.s_addr);
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100503 smp->type = SMP_T_UINT;
Emeric Brun107ca302010-01-04 16:16:05 +0100504 return 1;
505}
506
Willy Tarreau342acb42012-04-23 22:03:39 +0200507static int c_ip2str(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100508{
Willy Tarreau47ca5452012-12-23 20:22:19 +0100509 struct chunk *trash = get_trash_chunk();
Emeric Brun107ca302010-01-04 16:16:05 +0100510
Willy Tarreau342acb42012-04-23 22:03:39 +0200511 if (!inet_ntop(AF_INET, (void *)&smp->data.ipv4, trash->str, trash->size))
Emeric Brun107ca302010-01-04 16:16:05 +0100512 return 0;
513
514 trash->len = strlen(trash->str);
Willy Tarreau342acb42012-04-23 22:03:39 +0200515 smp->data.str = *trash;
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100516 smp->type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100517 smp->flags &= ~SMP_F_CONST;
Emeric Brun107ca302010-01-04 16:16:05 +0100518
519 return 1;
520}
521
Willy Tarreau342acb42012-04-23 22:03:39 +0200522static int c_ip2ipv6(struct sample *smp)
David du Colombier4f92d322011-03-24 11:09:31 +0100523{
Willy Tarreau342acb42012-04-23 22:03:39 +0200524 v4tov6(&smp->data.ipv6, &smp->data.ipv4);
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100525 smp->type = SMP_T_IPV6;
David du Colombier4f92d322011-03-24 11:09:31 +0100526 return 1;
527}
528
Willy Tarreau342acb42012-04-23 22:03:39 +0200529static int c_ipv62str(struct sample *smp)
David du Colombier4f92d322011-03-24 11:09:31 +0100530{
Willy Tarreau47ca5452012-12-23 20:22:19 +0100531 struct chunk *trash = get_trash_chunk();
David du Colombier4f92d322011-03-24 11:09:31 +0100532
Willy Tarreau342acb42012-04-23 22:03:39 +0200533 if (!inet_ntop(AF_INET6, (void *)&smp->data.ipv6, trash->str, trash->size))
David du Colombier4f92d322011-03-24 11:09:31 +0100534 return 0;
535
536 trash->len = strlen(trash->str);
Willy Tarreau342acb42012-04-23 22:03:39 +0200537 smp->data.str = *trash;
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100538 smp->type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100539 smp->flags &= ~SMP_F_CONST;
David du Colombier4f92d322011-03-24 11:09:31 +0100540 return 1;
541}
542
543/*
Willy Tarreau342acb42012-04-23 22:03:39 +0200544static int c_ipv62ip(struct sample *smp)
David du Colombier4f92d322011-03-24 11:09:31 +0100545{
Willy Tarreau342acb42012-04-23 22:03:39 +0200546 return v6tov4(&smp->data.ipv4, &smp->data.ipv6);
David du Colombier4f92d322011-03-24 11:09:31 +0100547}
548*/
549
Willy Tarreau342acb42012-04-23 22:03:39 +0200550static int c_int2ip(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100551{
Willy Tarreau342acb42012-04-23 22:03:39 +0200552 smp->data.ipv4.s_addr = htonl(smp->data.uint);
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100553 smp->type = SMP_T_IPV4;
Emeric Brun107ca302010-01-04 16:16:05 +0100554 return 1;
555}
556
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100557static int c_str2addr(struct sample *smp)
558{
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100559 if (!buf2ip(smp->data.str.str, smp->data.str.len, &smp->data.ipv4)) {
Thierry FOURNIERfd139902013-12-11 12:38:57 +0100560 if (!buf2ip6(smp->data.str.str, smp->data.str.len, &smp->data.ipv6))
561 return 0;
562 smp->type = SMP_T_IPV6;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100563 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIERfd139902013-12-11 12:38:57 +0100564 return 1;
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100565 }
566 smp->type = SMP_T_IPV4;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100567 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100568 return 1;
569}
570
Willy Tarreau342acb42012-04-23 22:03:39 +0200571static int c_str2ip(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100572{
Willy Tarreau342acb42012-04-23 22:03:39 +0200573 if (!buf2ip(smp->data.str.str, smp->data.str.len, &smp->data.ipv4))
Emeric Brun107ca302010-01-04 16:16:05 +0100574 return 0;
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100575 smp->type = SMP_T_IPV4;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100576 smp->flags &= ~SMP_F_CONST;
Emeric Brun107ca302010-01-04 16:16:05 +0100577 return 1;
578}
579
Willy Tarreau342acb42012-04-23 22:03:39 +0200580static int c_str2ipv6(struct sample *smp)
David du Colombier4f92d322011-03-24 11:09:31 +0100581{
Thierry FOURNIERfd139902013-12-11 12:38:57 +0100582 if (!buf2ip6(smp->data.str.str, smp->data.str.len, &smp->data.ipv6))
583 return 0;
584 smp->type = SMP_T_IPV6;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100585 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIERfd139902013-12-11 12:38:57 +0100586 return 1;
David du Colombier4f92d322011-03-24 11:09:31 +0100587}
588
Emeric Brun4b9e8022014-11-03 18:17:10 +0100589/*
590 * The NULL char always enforces the end of string if it is met.
591 * Data is never changed, so we can ignore the CONST case
Thierry FOURNIERe87cac12014-03-12 15:07:59 +0100592 */
Emeric Brun8ac33d92012-10-17 13:36:06 +0200593static int c_bin2str(struct sample *smp)
594{
Emeric Brun4b9e8022014-11-03 18:17:10 +0100595 int i;
Emeric Brun8ac33d92012-10-17 13:36:06 +0200596
Emeric Brun4b9e8022014-11-03 18:17:10 +0100597 for (i = 0; i < smp->data.str.len; i++) {
598 if (!smp->data.str.str[i]) {
599 smp->data.str.len = i;
Thierry FOURNIERe87cac12014-03-12 15:07:59 +0100600 break;
Emeric Brun4b9e8022014-11-03 18:17:10 +0100601 }
Emeric Brun8ac33d92012-10-17 13:36:06 +0200602 }
Emeric Brun8ac33d92012-10-17 13:36:06 +0200603 return 1;
604}
605
Willy Tarreau342acb42012-04-23 22:03:39 +0200606static int c_int2str(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100607{
Willy Tarreau47ca5452012-12-23 20:22:19 +0100608 struct chunk *trash = get_trash_chunk();
Emeric Brun107ca302010-01-04 16:16:05 +0100609 char *pos;
610
Willy Tarreau342acb42012-04-23 22:03:39 +0200611 pos = ultoa_r(smp->data.uint, trash->str, trash->size);
Thierry FOURNIER0f811442015-06-06 19:16:52 +0200612 if (!pos)
613 return 0;
614
615 trash->size = trash->size - (pos - trash->str);
616 trash->str = pos;
617 trash->len = strlen(pos);
618 smp->data.str = *trash;
619 smp->type = SMP_T_STR;
620 smp->flags &= ~SMP_F_CONST;
621 return 1;
622}
623
624static int c_sint2str(struct sample *smp)
625{
626 struct chunk *trash = get_trash_chunk();
627 char *pos;
Emeric Brun107ca302010-01-04 16:16:05 +0100628
Thierry FOURNIER0f811442015-06-06 19:16:52 +0200629 pos = sltoa_r(smp->data.sint, trash->str, trash->size);
Emeric Brun107ca302010-01-04 16:16:05 +0100630 if (!pos)
631 return 0;
632
Emeric Brun485479d2010-09-23 18:02:19 +0200633 trash->size = trash->size - (pos - trash->str);
Emeric Brun107ca302010-01-04 16:16:05 +0100634 trash->str = pos;
635 trash->len = strlen(pos);
Willy Tarreau342acb42012-04-23 22:03:39 +0200636 smp->data.str = *trash;
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100637 smp->type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100638 smp->flags &= ~SMP_F_CONST;
Emeric Brun107ca302010-01-04 16:16:05 +0100639 return 1;
640}
641
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100642/* This function duplicates data and removes the flag "const". */
643int smp_dup(struct sample *smp)
Emeric Brun485479d2010-09-23 18:02:19 +0200644{
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100645 struct chunk *trash;
Emeric Brun485479d2010-09-23 18:02:19 +0200646
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100647 /* If the const flag is not set, we don't need to duplicate the
648 * pattern as it can be modified in place.
649 */
650 if (!(smp->flags & SMP_F_CONST))
651 return 1;
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100652
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100653 switch (smp->type) {
654 case SMP_T_BOOL:
655 case SMP_T_UINT:
656 case SMP_T_SINT:
657 case SMP_T_ADDR:
658 case SMP_T_IPV4:
659 case SMP_T_IPV6:
660 /* These type are not const. */
661 break;
662 case SMP_T_STR:
663 case SMP_T_BIN:
664 /* Duplicate data. */
665 trash = get_trash_chunk();
666 trash->len = smp->data.str.len < trash->size ? smp->data.str.len : trash->size;
667 memcpy(trash->str, smp->data.str.str, trash->len);
668 smp->data.str = *trash;
669 break;
670 default:
671 /* Other cases are unexpected. */
672 return 0;
673 }
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100674
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100675 /* remove const flag */
676 smp->flags &= ~SMP_F_CONST;
Emeric Brun485479d2010-09-23 18:02:19 +0200677 return 1;
678}
679
Thierry FOURNIER0e9af552013-12-14 14:55:04 +0100680int c_none(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100681{
682 return 1;
683}
684
Willy Tarreau342acb42012-04-23 22:03:39 +0200685static int c_str2int(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100686{
687 int i;
688 uint32_t ret = 0;
689
Thierry FOURNIER60bb0202014-01-27 18:20:48 +0100690 if (smp->data.str.len == 0)
691 return 0;
692
Willy Tarreau342acb42012-04-23 22:03:39 +0200693 for (i = 0; i < smp->data.str.len; i++) {
694 uint32_t val = smp->data.str.str[i] - '0';
Emeric Brun107ca302010-01-04 16:16:05 +0100695
Thierry FOURNIER60bb0202014-01-27 18:20:48 +0100696 if (val > 9) {
697 if (i == 0)
698 return 0;
Emeric Brun107ca302010-01-04 16:16:05 +0100699 break;
Thierry FOURNIER60bb0202014-01-27 18:20:48 +0100700 }
Emeric Brun107ca302010-01-04 16:16:05 +0100701
702 ret = ret * 10 + val;
703 }
704
Willy Tarreau342acb42012-04-23 22:03:39 +0200705 smp->data.uint = ret;
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100706 smp->type = SMP_T_UINT;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100707 smp->flags &= ~SMP_F_CONST;
Emeric Brun107ca302010-01-04 16:16:05 +0100708 return 1;
709}
710
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100711static int c_str2meth(struct sample *smp)
712{
713 enum http_meth_t meth;
714 int len;
715
716 meth = find_http_meth(smp->data.str.str, smp->data.str.len);
717 if (meth == HTTP_METH_OTHER) {
718 len = smp->data.str.len;
719 smp->data.meth.str.str = smp->data.str.str;
720 smp->data.meth.str.len = len;
721 }
722 else
723 smp->flags &= ~SMP_F_CONST;
724 smp->data.meth.meth = meth;
725 smp->type = SMP_T_METH;
726 return 1;
727}
728
729static int c_meth2str(struct sample *smp)
730{
731 int len;
732 enum http_meth_t meth;
733
734 if (smp->data.meth.meth == HTTP_METH_OTHER) {
735 /* The method is unknown. Copy the original pointer. */
736 len = smp->data.meth.str.len;
737 smp->data.str.str = smp->data.meth.str.str;
738 smp->data.str.len = len;
739 smp->type = SMP_T_STR;
740 }
741 else if (smp->data.meth.meth < HTTP_METH_OTHER) {
742 /* The method is known, copy the pointer containing the string. */
743 meth = smp->data.meth.meth;
744 smp->data.str.str = http_known_methods[meth].name;
745 smp->data.str.len = http_known_methods[meth].len;
746 smp->flags |= SMP_F_CONST;
747 smp->type = SMP_T_STR;
748 }
749 else {
750 /* Unknown method */
751 return 0;
752 }
753 return 1;
754}
755
Willy Tarreau9700e5c2014-07-15 21:03:26 +0200756static int c_addr2bin(struct sample *smp)
757{
758 struct chunk *chk = get_trash_chunk();
759
760 if (smp->type == SMP_T_IPV4) {
761 chk->len = 4;
762 memcpy(chk->str, &smp->data.ipv4, chk->len);
763 }
764 else if (smp->type == SMP_T_IPV6) {
765 chk->len = 16;
766 memcpy(chk->str, &smp->data.ipv6, chk->len);
767 }
768 else
769 return 0;
770
771 smp->data.str = *chk;
772 smp->type = SMP_T_BIN;
773 return 1;
774}
775
Willy Tarreaubbfd1a22014-07-15 21:19:08 +0200776static int c_int2bin(struct sample *smp)
777{
778 struct chunk *chk = get_trash_chunk();
779
780 *(unsigned int *)chk->str = htonl(smp->data.uint);
781 chk->len = 4;
782
783 smp->data.str = *chk;
784 smp->type = SMP_T_BIN;
785 return 1;
786}
787
Willy Tarreau9700e5c2014-07-15 21:03:26 +0200788
Emeric Brun107ca302010-01-04 16:16:05 +0100789/*****************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +0200790/* Sample casts matrix: */
791/* sample_casts[from type][to type] */
792/* NULL pointer used for impossible sample casts */
Emeric Brun107ca302010-01-04 16:16:05 +0100793/*****************************************************************/
Emeric Brun107ca302010-01-04 16:16:05 +0100794
Thierry FOURNIER8af6ff12013-11-21 10:53:12 +0100795sample_cast_fct sample_casts[SMP_TYPES][SMP_TYPES] = {
Thierry FOURNIER9c627e82015-06-03 20:12:35 +0200796/* to: ANY BOOL UINT SINT ADDR IPV4 IPV6 STR BIN METH */
797/* from: ANY */ { c_none, c_none, c_none, c_none, c_none, c_none, c_none, c_none, c_none, c_none, },
798/* BOOL */ { c_none, c_none, c_none, c_none, NULL, NULL, NULL, c_int2str, NULL, NULL, },
799/* UINT */ { c_none, c_none, c_none, c_none, c_int2ip, c_int2ip, NULL, c_int2str, c_int2bin, NULL, },
800/* SINT */ { c_none, c_none, c_none, c_none, c_int2ip, c_int2ip, NULL, c_sint2str, c_int2bin, NULL, },
801/* ADDR */ { c_none, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, },
802/* IPV4 */ { c_none, NULL, c_ip2int, c_ip2int, c_none, c_none, c_ip2ipv6, c_ip2str, c_addr2bin, NULL, },
803/* IPV6 */ { c_none, NULL, NULL, NULL, c_none, NULL, c_none, c_ipv62str, c_addr2bin, NULL, },
804/* STR */ { c_none, c_str2int, c_str2int, c_str2int, c_str2addr, c_str2ip, c_str2ipv6, c_none, c_none, c_str2meth, },
805/* BIN */ { c_none, NULL, NULL, NULL, NULL, NULL, NULL, c_bin2str, c_none, c_str2meth, },
806/* METH */ { c_none, NULL, NULL, NULL, NULL, NULL, NULL, c_meth2str, c_meth2str, c_none, }
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200807};
Emeric Brun107ca302010-01-04 16:16:05 +0100808
Emeric Brun107ca302010-01-04 16:16:05 +0100809/*
Willy Tarreau12785782012-04-27 21:37:17 +0200810 * Parse a sample expression configuration:
Emeric Brun107ca302010-01-04 16:16:05 +0100811 * fetch keyword followed by format conversion keywords.
Willy Tarreau12785782012-04-27 21:37:17 +0200812 * Returns a pointer on allocated sample expression structure.
Willy Tarreaua4312fa2013-04-02 16:34:32 +0200813 * The caller must have set al->ctx.
Emeric Brun107ca302010-01-04 16:16:05 +0100814 */
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +0100815struct 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 +0100816{
Willy Tarreau833cc792013-07-24 15:34:19 +0200817 const char *begw; /* beginning of word */
818 const char *endw; /* end of word */
819 const char *endt; /* end of term */
Willy Tarreau12785782012-04-27 21:37:17 +0200820 struct sample_expr *expr;
821 struct sample_fetch *fetch;
822 struct sample_conv *conv;
Emeric Brun107ca302010-01-04 16:16:05 +0100823 unsigned long prev_type;
Willy Tarreau833cc792013-07-24 15:34:19 +0200824 char *fkw = NULL;
825 char *ckw = NULL;
Willy Tarreau689a1df2013-12-13 00:40:11 +0100826 int err_arg;
Emeric Brun107ca302010-01-04 16:16:05 +0100827
Willy Tarreau833cc792013-07-24 15:34:19 +0200828 begw = str[*idx];
829 for (endw = begw; *endw && *endw != '(' && *endw != ','; endw++);
830
831 if (endw == begw) {
Willy Tarreau975c1782013-12-12 23:16:54 +0100832 memprintf(err_msg, "missing fetch method");
Emeric Brun107ca302010-01-04 16:16:05 +0100833 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200834 }
Emeric Brun107ca302010-01-04 16:16:05 +0100835
Willy Tarreau833cc792013-07-24 15:34:19 +0200836 /* keep a copy of the current fetch keyword for error reporting */
837 fkw = my_strndup(begw, endw - begw);
Emeric Brun107ca302010-01-04 16:16:05 +0100838
Willy Tarreau833cc792013-07-24 15:34:19 +0200839 fetch = find_sample_fetch(begw, endw - begw);
840 if (!fetch) {
Willy Tarreau975c1782013-12-12 23:16:54 +0100841 memprintf(err_msg, "unknown fetch method '%s'", fkw);
Emeric Brun107ca302010-01-04 16:16:05 +0100842 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200843 }
Emeric Brun107ca302010-01-04 16:16:05 +0100844
Willy Tarreau833cc792013-07-24 15:34:19 +0200845 endt = endw;
846 if (*endt == '(') {
Willy Tarreau689a1df2013-12-13 00:40:11 +0100847 /* look for the end of this term and skip the opening parenthesis */
848 endt = ++endw;
Willy Tarreau833cc792013-07-24 15:34:19 +0200849 while (*endt && *endt != ')')
850 endt++;
851 if (*endt != ')') {
Willy Tarreau975c1782013-12-12 23:16:54 +0100852 memprintf(err_msg, "missing closing ')' after arguments to fetch keyword '%s'", fkw);
Willy Tarreau833cc792013-07-24 15:34:19 +0200853 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200854 }
Emeric Brun485479d2010-09-23 18:02:19 +0200855 }
Emeric Brun107ca302010-01-04 16:16:05 +0100856
Willy Tarreau833cc792013-07-24 15:34:19 +0200857 /* At this point, we have :
858 * - begw : beginning of the keyword
Willy Tarreau689a1df2013-12-13 00:40:11 +0100859 * - endw : end of the keyword, first character not part of keyword
860 * nor the opening parenthesis (so first character of args
861 * if present).
Willy Tarreau833cc792013-07-24 15:34:19 +0200862 * - endt : end of the term (=endw or last parenthesis if args are present)
863 */
864
865 if (fetch->out_type >= SMP_TYPES) {
Willy Tarreau975c1782013-12-12 23:16:54 +0100866 memprintf(err_msg, "returns type of fetch method '%s' is unknown", fkw);
Emeric Brun107ca302010-01-04 16:16:05 +0100867 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200868 }
Emeric Brun107ca302010-01-04 16:16:05 +0100869 prev_type = fetch->out_type;
Willy Tarreau833cc792013-07-24 15:34:19 +0200870
Willy Tarreau12785782012-04-27 21:37:17 +0200871 expr = calloc(1, sizeof(struct sample_expr));
Emeric Brun485479d2010-09-23 18:02:19 +0200872 if (!expr)
873 goto out_error;
Emeric Brun107ca302010-01-04 16:16:05 +0100874
875 LIST_INIT(&(expr->conv_exprs));
876 expr->fetch = fetch;
Willy Tarreau2e845be2012-10-19 19:49:09 +0200877 expr->arg_p = empty_arg_list;
Emeric Brun107ca302010-01-04 16:16:05 +0100878
Willy Tarreau689a1df2013-12-13 00:40:11 +0100879 /* Note that we call the argument parser even with an empty string,
880 * this allows it to automatically create entries for mandatory
881 * implicit arguments (eg: local proxy name).
882 */
883 al->kw = expr->fetch->kw;
884 al->conv = NULL;
885 if (make_arg_list(endw, endt - endw, fetch->arg_mask, &expr->arg_p, err_msg, NULL, &err_arg, al) < 0) {
886 memprintf(err_msg, "fetch method '%s' : %s", fkw, *err_msg);
887 goto out_error;
888 }
Willy Tarreau2e845be2012-10-19 19:49:09 +0200889
Willy Tarreau689a1df2013-12-13 00:40:11 +0100890 if (!expr->arg_p) {
891 expr->arg_p = empty_arg_list;
Emeric Brun485479d2010-09-23 18:02:19 +0200892 }
Willy Tarreau689a1df2013-12-13 00:40:11 +0100893 else if (fetch->val_args && !fetch->val_args(expr->arg_p, err_msg)) {
894 memprintf(err_msg, "invalid args in fetch method '%s' : %s", fkw, *err_msg);
Emeric Brun485479d2010-09-23 18:02:19 +0200895 goto out_error;
Emeric Brun107ca302010-01-04 16:16:05 +0100896 }
897
Willy Tarreau833cc792013-07-24 15:34:19 +0200898 /* Now process the converters if any. We have two supported syntaxes
899 * for the converters, which can be combined :
900 * - comma-delimited list of converters just after the keyword and args ;
901 * - one converter per keyword
902 * The combination allows to have each keyword being a comma-delimited
903 * series of converters.
904 *
905 * We want to process the former first, then the latter. For this we start
906 * from the beginning of the supposed place in the exiting conv chain, which
907 * starts at the last comma (endt).
908 */
909
910 while (1) {
Willy Tarreau12785782012-04-27 21:37:17 +0200911 struct sample_conv_expr *conv_expr;
Emeric Brun107ca302010-01-04 16:16:05 +0100912
Willy Tarreau833cc792013-07-24 15:34:19 +0200913 if (*endt == ')') /* skip last closing parenthesis */
914 endt++;
Emeric Brun107ca302010-01-04 16:16:05 +0100915
Willy Tarreau833cc792013-07-24 15:34:19 +0200916 if (*endt && *endt != ',') {
917 if (ckw)
Willy Tarreau975c1782013-12-12 23:16:54 +0100918 memprintf(err_msg, "missing comma after conv keyword '%s'", ckw);
Willy Tarreau833cc792013-07-24 15:34:19 +0200919 else
Willy Tarreau975c1782013-12-12 23:16:54 +0100920 memprintf(err_msg, "missing comma after fetch keyword '%s'", fkw);
Emeric Brun107ca302010-01-04 16:16:05 +0100921 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200922 }
Emeric Brun107ca302010-01-04 16:16:05 +0100923
Willy Tarreau833cc792013-07-24 15:34:19 +0200924 while (*endt == ',') /* then trailing commas */
925 endt++;
926
927 begw = endt; /* start of conv keyword */
928
929 if (!*begw) {
930 /* none ? skip to next string */
931 (*idx)++;
932 begw = str[*idx];
933 if (!begw || !*begw)
934 break;
935 }
936
937 for (endw = begw; *endw && *endw != '(' && *endw != ','; endw++);
938
939 free(ckw);
940 ckw = my_strndup(begw, endw - begw);
941
942 conv = find_sample_conv(begw, endw - begw);
943 if (!conv) {
944 /* we found an isolated keyword that we don't know, it's not ours */
945 if (begw == str[*idx])
946 break;
Willy Tarreau975c1782013-12-12 23:16:54 +0100947 memprintf(err_msg, "unknown conv method '%s'", ckw);
Willy Tarreau833cc792013-07-24 15:34:19 +0200948 goto out_error;
949 }
Emeric Brun107ca302010-01-04 16:16:05 +0100950
Willy Tarreau833cc792013-07-24 15:34:19 +0200951 endt = endw;
952 if (*endt == '(') {
953 /* look for the end of this term */
954 while (*endt && *endt != ')')
955 endt++;
956 if (*endt != ')') {
Willy Tarreau975c1782013-12-12 23:16:54 +0100957 memprintf(err_msg, "syntax error: missing ')' after conv keyword '%s'", ckw);
Willy Tarreau833cc792013-07-24 15:34:19 +0200958 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200959 }
Willy Tarreau833cc792013-07-24 15:34:19 +0200960 }
961
962 if (conv->in_type >= SMP_TYPES || conv->out_type >= SMP_TYPES) {
Willy Tarreau975c1782013-12-12 23:16:54 +0100963 memprintf(err_msg, "returns type of conv method '%s' is unknown", ckw);
Emeric Brun107ca302010-01-04 16:16:05 +0100964 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200965 }
Emeric Brun107ca302010-01-04 16:16:05 +0100966
967 /* If impossible type conversion */
Willy Tarreau12785782012-04-27 21:37:17 +0200968 if (!sample_casts[prev_type][conv->in_type]) {
Willy Tarreau975c1782013-12-12 23:16:54 +0100969 memprintf(err_msg, "conv method '%s' cannot be applied", ckw);
Emeric Brun107ca302010-01-04 16:16:05 +0100970 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200971 }
Emeric Brun107ca302010-01-04 16:16:05 +0100972
973 prev_type = conv->out_type;
Willy Tarreau12785782012-04-27 21:37:17 +0200974 conv_expr = calloc(1, sizeof(struct sample_conv_expr));
Emeric Brun485479d2010-09-23 18:02:19 +0200975 if (!conv_expr)
976 goto out_error;
Emeric Brun107ca302010-01-04 16:16:05 +0100977
978 LIST_ADDQ(&(expr->conv_exprs), &(conv_expr->list));
979 conv_expr->conv = conv;
980
Willy Tarreau833cc792013-07-24 15:34:19 +0200981 if (endt != endw) {
Willy Tarreaub27c0d32012-04-20 16:04:47 +0200982 int err_arg;
Willy Tarreau21d68a62012-04-20 15:52:36 +0200983
Willy Tarreau9fcb9842012-04-20 14:45:49 +0200984 if (!conv->arg_mask) {
Willy Tarreau975c1782013-12-12 23:16:54 +0100985 memprintf(err_msg, "conv method '%s' does not support any args", ckw);
Emeric Brun485479d2010-09-23 18:02:19 +0200986 goto out_error;
987 }
Willy Tarreau9e92d322010-01-26 17:58:06 +0100988
Willy Tarreaua4312fa2013-04-02 16:34:32 +0200989 al->kw = expr->fetch->kw;
990 al->conv = conv_expr->conv->kw;
Willy Tarreau975c1782013-12-12 23:16:54 +0100991 if (make_arg_list(endw + 1, endt - endw - 1, conv->arg_mask, &conv_expr->arg_p, err_msg, NULL, &err_arg, al) < 0) {
992 memprintf(err_msg, "invalid arg %d in conv method '%s' : %s", err_arg+1, ckw, *err_msg);
Willy Tarreau21d68a62012-04-20 15:52:36 +0200993 goto out_error;
994 }
995
Willy Tarreau2e845be2012-10-19 19:49:09 +0200996 if (!conv_expr->arg_p)
997 conv_expr->arg_p = empty_arg_list;
998
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +0100999 if (conv->val_args && !conv->val_args(conv_expr->arg_p, conv, file, line, err_msg)) {
Willy Tarreau975c1782013-12-12 23:16:54 +01001000 memprintf(err_msg, "invalid args in conv method '%s' : %s", ckw, *err_msg);
Emeric Brun485479d2010-09-23 18:02:19 +02001001 goto out_error;
1002 }
1003 }
Willy Tarreau833cc792013-07-24 15:34:19 +02001004 else if (ARGM(conv->arg_mask)) {
Willy Tarreau975c1782013-12-12 23:16:54 +01001005 memprintf(err_msg, "missing args for conv method '%s'", ckw);
Emeric Brun485479d2010-09-23 18:02:19 +02001006 goto out_error;
Emeric Brun107ca302010-01-04 16:16:05 +01001007 }
1008 }
Emeric Brun485479d2010-09-23 18:02:19 +02001009
Willy Tarreau833cc792013-07-24 15:34:19 +02001010 out:
1011 free(fkw);
1012 free(ckw);
Emeric Brun107ca302010-01-04 16:16:05 +01001013 return expr;
1014
1015out_error:
Willy Tarreau12785782012-04-27 21:37:17 +02001016 /* TODO: prune_sample_expr(expr); */
Willy Tarreau833cc792013-07-24 15:34:19 +02001017 expr = NULL;
1018 goto out;
Emeric Brun107ca302010-01-04 16:16:05 +01001019}
1020
1021/*
Willy Tarreau12785782012-04-27 21:37:17 +02001022 * Process a fetch + format conversion of defined by the sample expression <expr>
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001023 * on request or response considering the <opt> parameter.
Willy Tarreau12785782012-04-27 21:37:17 +02001024 * Returns a pointer on a typed sample structure containing the result or NULL if
1025 * sample is not found or when format conversion failed.
Emeric Brun107ca302010-01-04 16:16:05 +01001026 * If <p> is not null, function returns results in structure pointed by <p>.
Willy Tarreau12785782012-04-27 21:37:17 +02001027 * If <p> is null, functions returns a pointer on a static sample structure.
Willy Tarreaub8c8f1f2012-04-23 22:38:26 +02001028 *
1029 * Note: the fetch functions are required to properly set the return type. The
1030 * conversion functions must do so too. However the cast functions do not need
1031 * to since they're made to cast mutiple types according to what is required.
Willy Tarreau6bcb0a82014-07-30 08:56:35 +02001032 *
1033 * The caller may indicate in <opt> if it considers the result final or not.
1034 * The caller needs to check the SMP_F_MAY_CHANGE flag in p->flags to verify
1035 * if the result is stable or not, according to the following table :
1036 *
1037 * return MAY_CHANGE FINAL Meaning for the sample
1038 * NULL 0 * Not present and will never be (eg: header)
1039 * NULL 1 0 Not present yet, could change (eg: POST param)
1040 * NULL 1 1 Not present yet, will not change anymore
1041 * smp 0 * Present and will not change (eg: header)
1042 * smp 1 0 Present, may change (eg: request length)
1043 * smp 1 1 Present, last known value (eg: request length)
Emeric Brun107ca302010-01-04 16:16:05 +01001044 */
Willy Tarreau192252e2015-04-04 01:47:55 +02001045struct sample *sample_process(struct proxy *px, struct session *sess,
1046 struct stream *strm, unsigned int opt,
Willy Tarreau12785782012-04-27 21:37:17 +02001047 struct sample_expr *expr, struct sample *p)
Emeric Brun107ca302010-01-04 16:16:05 +01001048{
Willy Tarreau12785782012-04-27 21:37:17 +02001049 struct sample_conv_expr *conv_expr;
Emeric Brun107ca302010-01-04 16:16:05 +01001050
Willy Tarreau18387e22013-07-25 12:02:38 +02001051 if (p == NULL) {
Willy Tarreaub4a88f02012-04-23 21:35:11 +02001052 p = &temp_smp;
Willy Tarreau6c616e02014-06-25 16:56:41 +02001053 memset(p, 0, sizeof(*p));
Willy Tarreau18387e22013-07-25 12:02:38 +02001054 }
Emeric Brun107ca302010-01-04 16:16:05 +01001055
Thierry FOURNIER6879ad32015-05-11 11:54:58 +02001056 p->px = px;
1057 p->sess = sess;
1058 p->strm = strm;
Thierry FOURNIER1d33b882015-05-11 15:25:29 +02001059 p->opt = opt;
Thierry FOURNIER0786d052015-05-11 15:42:45 +02001060 if (!expr->fetch->process(expr->arg_p, p, expr->fetch->kw, expr->fetch->private))
Emeric Brun107ca302010-01-04 16:16:05 +01001061 return NULL;
1062
Emeric Brun107ca302010-01-04 16:16:05 +01001063 list_for_each_entry(conv_expr, &expr->conv_exprs, list) {
Willy Tarreau12e50112012-04-25 17:21:49 +02001064 /* we want to ensure that p->type can be casted into
1065 * conv_expr->conv->in_type. We have 3 possibilities :
1066 * - NULL => not castable.
1067 * - c_none => nothing to do (let's optimize it)
1068 * - other => apply cast and prepare to fail
1069 */
Willy Tarreau12785782012-04-27 21:37:17 +02001070 if (!sample_casts[p->type][conv_expr->conv->in_type])
Willy Tarreau12e50112012-04-25 17:21:49 +02001071 return NULL;
1072
Willy Tarreau12785782012-04-27 21:37:17 +02001073 if (sample_casts[p->type][conv_expr->conv->in_type] != c_none &&
1074 !sample_casts[p->type][conv_expr->conv->in_type](p))
Emeric Brun107ca302010-01-04 16:16:05 +01001075 return NULL;
1076
Willy Tarreau12e50112012-04-25 17:21:49 +02001077 /* OK cast succeeded */
1078
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001079 if (!conv_expr->conv->process(conv_expr->arg_p, p, conv_expr->conv->private))
Emeric Brun107ca302010-01-04 16:16:05 +01001080 return NULL;
Emeric Brun107ca302010-01-04 16:16:05 +01001081 }
1082 return p;
1083}
1084
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001085/*
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001086 * Resolve all remaining arguments in proxy <p>. Returns the number of
1087 * errors or 0 if everything is fine.
1088 */
1089int smp_resolve_args(struct proxy *p)
1090{
1091 struct arg_list *cur, *bak;
1092 const char *ctx, *where;
1093 const char *conv_ctx, *conv_pre, *conv_pos;
1094 struct userlist *ul;
Willy Tarreau46947782015-01-19 19:00:58 +01001095 struct my_regex *reg;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001096 struct arg *arg;
1097 int cfgerr = 0;
Willy Tarreau46947782015-01-19 19:00:58 +01001098 int rflags;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001099
1100 list_for_each_entry_safe(cur, bak, &p->conf.args.list, list) {
1101 struct proxy *px;
1102 struct server *srv;
1103 char *pname, *sname;
Willy Tarreau46947782015-01-19 19:00:58 +01001104 char *err;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001105
1106 arg = cur->arg;
1107
1108 /* prepare output messages */
1109 conv_pre = conv_pos = conv_ctx = "";
1110 if (cur->conv) {
1111 conv_ctx = cur->conv;
1112 conv_pre = "conversion keyword '";
1113 conv_pos = "' for ";
1114 }
1115
1116 where = "in";
1117 ctx = "sample fetch keyword";
1118 switch (cur->ctx) {
Willy Tarreau46947782015-01-19 19:00:58 +01001119 case ARGC_STK: where = "in stick rule in"; break;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001120 case ARGC_TRK: where = "in tracking rule in"; break;
1121 case ARGC_LOG: where = "in log-format string in"; break;
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +01001122 case ARGC_HRQ: where = "in http-request header format string in"; break;
1123 case ARGC_HRS: where = "in http-response header format string in"; break;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001124 case ARGC_UIF: where = "in unique-id-format string in"; break;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01001125 case ARGC_RDR: where = "in redirect format string in"; break;
Willy Tarreau3a4ac422014-06-13 16:17:14 +02001126 case ARGC_CAP: where = "in capture rule in"; break;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001127 case ARGC_ACL: ctx = "ACL keyword"; break;
Willy Tarreau28d976d2015-07-09 11:39:33 +02001128 case ARGC_SRV: where = "in server directive in"; break;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001129 }
1130
1131 /* set a few default settings */
1132 px = p;
1133 pname = p->id;
1134
1135 switch (arg->type) {
1136 case ARGT_SRV:
1137 if (!arg->data.str.len) {
1138 Alert("parsing [%s:%d] : missing server name in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1139 cur->file, cur->line,
1140 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
1141 cfgerr++;
1142 continue;
1143 }
1144
1145 /* we support two formats : "bck/srv" and "srv" */
1146 sname = strrchr(arg->data.str.str, '/');
1147
1148 if (sname) {
1149 *sname++ = '\0';
1150 pname = arg->data.str.str;
1151
Willy Tarreau9e0bb102015-05-26 11:24:42 +02001152 px = proxy_be_by_name(pname);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001153 if (!px) {
1154 Alert("parsing [%s:%d] : unable to find proxy '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1155 cur->file, cur->line, pname,
1156 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
1157 cfgerr++;
1158 break;
1159 }
1160 }
1161 else
1162 sname = arg->data.str.str;
1163
1164 srv = findserver(px, sname);
1165 if (!srv) {
1166 Alert("parsing [%s:%d] : unable to find server '%s' in proxy '%s', referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1167 cur->file, cur->line, sname, pname,
1168 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
1169 cfgerr++;
1170 break;
1171 }
1172
1173 free(arg->data.str.str);
1174 arg->data.str.str = NULL;
1175 arg->unresolved = 0;
1176 arg->data.srv = srv;
1177 break;
1178
1179 case ARGT_FE:
1180 if (arg->data.str.len) {
1181 pname = arg->data.str.str;
Willy Tarreau9e0bb102015-05-26 11:24:42 +02001182 px = proxy_fe_by_name(pname);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001183 }
1184
1185 if (!px) {
1186 Alert("parsing [%s:%d] : unable to find frontend '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1187 cur->file, cur->line, pname,
1188 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
1189 cfgerr++;
1190 break;
1191 }
1192
1193 if (!(px->cap & PR_CAP_FE)) {
1194 Alert("parsing [%s:%d] : proxy '%s', referenced in arg %d of %s%s%s%s '%s' %s proxy '%s', has not frontend capability.\n",
1195 cur->file, cur->line, pname,
1196 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
1197 cfgerr++;
1198 break;
1199 }
1200
1201 free(arg->data.str.str);
1202 arg->data.str.str = NULL;
1203 arg->unresolved = 0;
1204 arg->data.prx = px;
1205 break;
1206
1207 case ARGT_BE:
1208 if (arg->data.str.len) {
1209 pname = arg->data.str.str;
Willy Tarreau9e0bb102015-05-26 11:24:42 +02001210 px = proxy_be_by_name(pname);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001211 }
1212
1213 if (!px) {
1214 Alert("parsing [%s:%d] : unable to find backend '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1215 cur->file, cur->line, pname,
1216 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
1217 cfgerr++;
1218 break;
1219 }
1220
1221 if (!(px->cap & PR_CAP_BE)) {
1222 Alert("parsing [%s:%d] : proxy '%s', referenced in arg %d of %s%s%s%s '%s' %s proxy '%s', has not backend capability.\n",
1223 cur->file, cur->line, pname,
1224 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
1225 cfgerr++;
1226 break;
1227 }
1228
1229 free(arg->data.str.str);
1230 arg->data.str.str = NULL;
1231 arg->unresolved = 0;
1232 arg->data.prx = px;
1233 break;
1234
1235 case ARGT_TAB:
1236 if (arg->data.str.len) {
1237 pname = arg->data.str.str;
Willy Tarreaue2dc1fa2015-05-26 12:08:07 +02001238 px = proxy_tbl_by_name(pname);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001239 }
1240
1241 if (!px) {
1242 Alert("parsing [%s:%d] : unable to find table '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1243 cur->file, cur->line, pname,
1244 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
1245 cfgerr++;
1246 break;
1247 }
1248
1249 if (!px->table.size) {
1250 Alert("parsing [%s:%d] : no table in proxy '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1251 cur->file, cur->line, pname,
1252 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
1253 cfgerr++;
1254 break;
1255 }
1256
1257 free(arg->data.str.str);
1258 arg->data.str.str = NULL;
1259 arg->unresolved = 0;
1260 arg->data.prx = px;
1261 break;
1262
1263 case ARGT_USR:
1264 if (!arg->data.str.len) {
1265 Alert("parsing [%s:%d] : missing userlist name in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1266 cur->file, cur->line,
1267 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
1268 cfgerr++;
1269 break;
1270 }
1271
1272 if (p->uri_auth && p->uri_auth->userlist &&
1273 !strcmp(p->uri_auth->userlist->name, arg->data.str.str))
1274 ul = p->uri_auth->userlist;
1275 else
1276 ul = auth_find_userlist(arg->data.str.str);
1277
1278 if (!ul) {
1279 Alert("parsing [%s:%d] : unable to find userlist '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1280 cur->file, cur->line, arg->data.str.str,
1281 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
1282 cfgerr++;
1283 break;
1284 }
1285
1286 free(arg->data.str.str);
1287 arg->data.str.str = NULL;
1288 arg->unresolved = 0;
1289 arg->data.usr = ul;
1290 break;
Willy Tarreau46947782015-01-19 19:00:58 +01001291
1292 case ARGT_REG:
1293 if (!arg->data.str.len) {
1294 Alert("parsing [%s:%d] : missing regex in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1295 cur->file, cur->line,
1296 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
1297 cfgerr++;
1298 continue;
1299 }
1300
1301 reg = calloc(1, sizeof(*reg));
1302 if (!reg) {
1303 Alert("parsing [%s:%d] : not enough memory to build regex in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1304 cur->file, cur->line,
1305 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
1306 cfgerr++;
1307 continue;
1308 }
1309
1310 rflags = 0;
1311 rflags |= (arg->type_flags & ARGF_REG_ICASE) ? REG_ICASE : 0;
1312 err = NULL;
1313
Willy Tarreaud817e462015-01-23 20:23:17 +01001314 if (!regex_comp(arg->data.str.str, reg, !(rflags & REG_ICASE), 1 /* capture substr */, &err)) {
Willy Tarreau46947782015-01-19 19:00:58 +01001315 Alert("parsing [%s:%d] : error in regex '%s' in arg %d of %s%s%s%s '%s' %s proxy '%s' : %s.\n",
1316 cur->file, cur->line,
1317 arg->data.str.str,
1318 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id, err);
1319 cfgerr++;
1320 continue;
1321 }
1322
1323 free(arg->data.str.str);
1324 arg->data.str.str = NULL;
1325 arg->unresolved = 0;
1326 arg->data.reg = reg;
1327 break;
1328
1329
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001330 }
1331
1332 LIST_DEL(&cur->list);
1333 free(cur);
1334 } /* end of args processing */
1335
1336 return cfgerr;
1337}
1338
1339/*
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001340 * Process a fetch + format conversion as defined by the sample expression
1341 * <expr> on request or response considering the <opt> parameter. The output is
Adis Nezirovic79beb242015-07-06 15:41:02 +02001342 * not explicitly set to <smp_type>, but shall be compatible with it as
1343 * specified by 'sample_casts' table. If a stable sample can be fetched, or an
1344 * unstable one when <opt> contains SMP_OPT_FINAL, the sample is converted and
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001345 * returned without the SMP_F_MAY_CHANGE flag. If an unstable sample is found
1346 * and <opt> does not contain SMP_OPT_FINAL, then the sample is returned as-is
1347 * with its SMP_F_MAY_CHANGE flag so that the caller can check it and decide to
1348 * take actions (eg: wait longer). If a sample could not be found or could not
Willy Tarreau6bcb0a82014-07-30 08:56:35 +02001349 * be converted, NULL is returned. The caller MUST NOT use the sample if the
1350 * SMP_F_MAY_CHANGE flag is present, as it is used only as a hint that there is
1351 * still hope to get it after waiting longer, and is not converted to string.
1352 * The possible output combinations are the following :
1353 *
1354 * return MAY_CHANGE FINAL Meaning for the sample
1355 * NULL * * Not present and will never be (eg: header)
1356 * smp 0 * Final value converted (eg: header)
1357 * smp 1 0 Not present yet, may appear later (eg: header)
1358 * smp 1 1 never happens (either flag is cleared on output)
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001359 */
Adis Nezirovic79beb242015-07-06 15:41:02 +02001360struct sample *sample_fetch_as_type(struct proxy *px, struct session *sess,
Willy Tarreau192252e2015-04-04 01:47:55 +02001361 struct stream *strm, unsigned int opt,
Adis Nezirovic79beb242015-07-06 15:41:02 +02001362 struct sample_expr *expr, int smp_type)
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001363{
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001364 struct sample *smp = &temp_smp;
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001365
Willy Tarreau6c616e02014-06-25 16:56:41 +02001366 memset(smp, 0, sizeof(*smp));
1367
Willy Tarreau192252e2015-04-04 01:47:55 +02001368 if (!sample_process(px, sess, strm, opt, expr, smp)) {
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001369 if ((smp->flags & SMP_F_MAY_CHANGE) && !(opt & SMP_OPT_FINAL))
1370 return smp;
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001371 return NULL;
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001372 }
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001373
Adis Nezirovic79beb242015-07-06 15:41:02 +02001374 if (!sample_casts[smp->type][smp_type])
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001375 return NULL;
1376
Adis Nezirovic79beb242015-07-06 15:41:02 +02001377 if (!sample_casts[smp->type][smp_type](smp))
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001378 return NULL;
1379
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001380 smp->flags &= ~SMP_F_MAY_CHANGE;
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001381 return smp;
1382}
1383
Emeric Brun107ca302010-01-04 16:16:05 +01001384/*****************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +02001385/* Sample format convert functions */
Willy Tarreaub8c8f1f2012-04-23 22:38:26 +02001386/* These functions set the data type on return. */
Emeric Brun107ca302010-01-04 16:16:05 +01001387/*****************************************************************/
1388
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001389#ifdef DEBUG_EXPR
1390static int sample_conv_debug(const struct arg *arg_p, struct sample *smp, void *private)
1391{
1392 int i;
1393 struct sample tmp;
1394
1395 if (!(global.mode & MODE_QUIET) || (global.mode & (MODE_VERBOSE | MODE_STARTING))) {
1396 fprintf(stderr, "[debug converter] type: %s ", smp_to_type[smp->type]);
1397 if (!sample_casts[smp->type][SMP_T_STR]) {
1398 fprintf(stderr, "(undisplayable)");
1399 } else {
1400
1401 /* Copy sample fetch. This put the sample as const, the
1402 * cast will copy data if a transformation is required.
1403 */
1404 memcpy(&tmp, smp, sizeof(struct sample));
1405 tmp.flags = SMP_F_CONST;
1406
1407 if (!sample_casts[smp->type][SMP_T_STR](&tmp))
1408 fprintf(stderr, "(undisplayable)");
1409
1410 else {
1411 /* Display the displayable chars*. */
1412 fprintf(stderr, "<");
1413 for (i = 0; i < tmp.data.str.len; i++) {
1414 if (isprint(tmp.data.str.str[i]))
1415 fputc(tmp.data.str.str[i], stderr);
1416 else
1417 fputc('.', stderr);
1418 }
1419 }
1420 fprintf(stderr, ">\n");
1421 }
1422 }
1423 return 1;
1424}
1425#endif
1426
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001427static int sample_conv_bin2base64(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brun53d1a982014-04-30 18:21:37 +02001428{
1429 struct chunk *trash = get_trash_chunk();
1430 int b64_len;
1431
1432 trash->len = 0;
1433 b64_len = a2base64(smp->data.str.str, smp->data.str.len, trash->str, trash->size);
1434 if (b64_len < 0)
1435 return 0;
1436
1437 trash->len = b64_len;
1438 smp->data.str = *trash;
1439 smp->type = SMP_T_STR;
1440 smp->flags &= ~SMP_F_CONST;
1441 return 1;
1442}
1443
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001444static int sample_conv_bin2hex(const struct arg *arg_p, struct sample *smp, void *private)
Thierry FOURNIER2f49d6d2014-03-12 15:01:52 +01001445{
1446 struct chunk *trash = get_trash_chunk();
1447 unsigned char c;
1448 int ptr = 0;
1449
1450 trash->len = 0;
1451 while (ptr < smp->data.str.len && trash->len <= trash->size - 2) {
1452 c = smp->data.str.str[ptr++];
1453 trash->str[trash->len++] = hextab[(c >> 4) & 0xF];
1454 trash->str[trash->len++] = hextab[c & 0xF];
1455 }
1456 smp->data.str = *trash;
1457 smp->type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01001458 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIER2f49d6d2014-03-12 15:01:52 +01001459 return 1;
1460}
1461
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001462/* hashes the binary input into a 32-bit unsigned int */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001463static int sample_conv_djb2(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001464{
1465 smp->data.uint = hash_djb2(smp->data.str.str, smp->data.str.len);
1466 if (arg_p && arg_p->data.uint)
1467 smp->data.uint = full_hash(smp->data.uint);
1468 smp->type = SMP_T_UINT;
1469 return 1;
1470}
1471
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001472static int sample_conv_str2lower(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brun107ca302010-01-04 16:16:05 +01001473{
1474 int i;
1475
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01001476 if (!smp_dup(smp))
1477 return 0;
1478
Willy Tarreau342acb42012-04-23 22:03:39 +02001479 if (!smp->data.str.size)
Emeric Brun485479d2010-09-23 18:02:19 +02001480 return 0;
1481
Willy Tarreau342acb42012-04-23 22:03:39 +02001482 for (i = 0; i < smp->data.str.len; i++) {
1483 if ((smp->data.str.str[i] >= 'A') && (smp->data.str.str[i] <= 'Z'))
1484 smp->data.str.str[i] += 'a' - 'A';
Emeric Brun107ca302010-01-04 16:16:05 +01001485 }
1486 return 1;
1487}
1488
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001489static int sample_conv_str2upper(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brun107ca302010-01-04 16:16:05 +01001490{
1491 int i;
1492
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01001493 if (!smp_dup(smp))
1494 return 0;
1495
Willy Tarreau342acb42012-04-23 22:03:39 +02001496 if (!smp->data.str.size)
Emeric Brun485479d2010-09-23 18:02:19 +02001497 return 0;
1498
Willy Tarreau342acb42012-04-23 22:03:39 +02001499 for (i = 0; i < smp->data.str.len; i++) {
1500 if ((smp->data.str.str[i] >= 'a') && (smp->data.str.str[i] <= 'z'))
1501 smp->data.str.str[i] += 'A' - 'a';
Emeric Brun107ca302010-01-04 16:16:05 +01001502 }
1503 return 1;
1504}
1505
Willy Tarreauf9954102012-04-20 14:03:29 +02001506/* takes the netmask in arg_p */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001507static int sample_conv_ipmask(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud31d6eb2010-01-26 18:01:41 +01001508{
Willy Tarreau342acb42012-04-23 22:03:39 +02001509 smp->data.ipv4.s_addr &= arg_p->data.ipv4.s_addr;
Willy Tarreaub8c8f1f2012-04-23 22:38:26 +02001510 smp->type = SMP_T_IPV4;
Willy Tarreaud31d6eb2010-01-26 18:01:41 +01001511 return 1;
1512}
1513
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001514/* takes an UINT value on input supposed to represent the time since EPOCH,
1515 * adds an optional offset found in args[1] and emits a string representing
1516 * the local time in the format specified in args[1] using strftime().
1517 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001518static int sample_conv_ltime(const struct arg *args, struct sample *smp, void *private)
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001519{
1520 struct chunk *temp;
1521 time_t curr_date = smp->data.uint;
Thierry FOURNIERfac9ccf2015-07-08 00:15:20 +02001522 struct tm *tm;
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001523
1524 /* add offset */
1525 if (args[1].type == ARGT_SINT || args[1].type == ARGT_UINT)
1526 curr_date += args[1].data.sint;
1527
Thierry FOURNIERfac9ccf2015-07-08 00:15:20 +02001528 tm = localtime(&curr_date);
1529 if (!tm)
1530 return 0;
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001531 temp = get_trash_chunk();
Thierry FOURNIERfac9ccf2015-07-08 00:15:20 +02001532 temp->len = strftime(temp->str, temp->size, args[0].data.str.str, tm);
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001533 smp->data.str = *temp;
1534 smp->type = SMP_T_STR;
1535 return 1;
1536}
1537
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001538/* hashes the binary input into a 32-bit unsigned int */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001539static int sample_conv_sdbm(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001540{
1541 smp->data.uint = hash_sdbm(smp->data.str.str, smp->data.str.len);
1542 if (arg_p && arg_p->data.uint)
1543 smp->data.uint = full_hash(smp->data.uint);
1544 smp->type = SMP_T_UINT;
1545 return 1;
1546}
1547
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001548/* takes an UINT value on input supposed to represent the time since EPOCH,
1549 * adds an optional offset found in args[1] and emits a string representing
1550 * the UTC date in the format specified in args[1] using strftime().
1551 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001552static int sample_conv_utime(const struct arg *args, struct sample *smp, void *private)
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001553{
1554 struct chunk *temp;
1555 time_t curr_date = smp->data.uint;
Thierry FOURNIERfac9ccf2015-07-08 00:15:20 +02001556 struct tm *tm;
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001557
1558 /* add offset */
1559 if (args[1].type == ARGT_SINT || args[1].type == ARGT_UINT)
1560 curr_date += args[1].data.sint;
1561
Thierry FOURNIERfac9ccf2015-07-08 00:15:20 +02001562 tm = gmtime(&curr_date);
1563 if (!tm)
1564 return 0;
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001565 temp = get_trash_chunk();
Thierry FOURNIERfac9ccf2015-07-08 00:15:20 +02001566 temp->len = strftime(temp->str, temp->size, args[0].data.str.str, tm);
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001567 smp->data.str = *temp;
1568 smp->type = SMP_T_STR;
1569 return 1;
1570}
1571
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001572/* hashes the binary input into a 32-bit unsigned int */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001573static int sample_conv_wt6(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001574{
1575 smp->data.uint = hash_wt6(smp->data.str.str, smp->data.str.len);
1576 if (arg_p && arg_p->data.uint)
1577 smp->data.uint = full_hash(smp->data.uint);
1578 smp->type = SMP_T_UINT;
1579 return 1;
1580}
1581
Willy Tarreau80599772015-01-20 19:35:24 +01001582/* hashes the binary input into a 32-bit unsigned int */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001583static int sample_conv_crc32(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau80599772015-01-20 19:35:24 +01001584{
1585 smp->data.uint = hash_crc32(smp->data.str.str, smp->data.str.len);
1586 if (arg_p && arg_p->data.uint)
1587 smp->data.uint = full_hash(smp->data.uint);
1588 smp->type = SMP_T_UINT;
1589 return 1;
1590}
1591
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001592/* This function escape special json characters. The returned string can be
1593 * safely set between two '"' and used as json string. The json string is
1594 * defined like this:
1595 *
1596 * any Unicode character except '"' or '\' or control character
1597 * \", \\, \/, \b, \f, \n, \r, \t, \u + four-hex-digits
1598 *
1599 * The enum input_type contain all the allowed mode for decoding the input
1600 * string.
1601 */
1602enum input_type {
1603 IT_ASCII = 0,
1604 IT_UTF8,
1605 IT_UTF8S,
1606 IT_UTF8P,
1607 IT_UTF8PS,
1608};
1609static int sample_conv_json_check(struct arg *arg, struct sample_conv *conv,
1610 const char *file, int line, char **err)
1611{
1612 if (!arg) {
1613 memprintf(err, "Unexpected empty arg list");
1614 return 0;
1615 }
1616
1617 if (arg->type != ARGT_STR) {
1618 memprintf(err, "Unexpected arg type");
1619 return 0;
1620 }
1621
1622 if (strcmp(arg->data.str.str, "") == 0) {
1623 arg->type = ARGT_UINT;
1624 arg->data.uint = IT_ASCII;
1625 return 1;
1626 }
1627
1628 else if (strcmp(arg->data.str.str, "ascii") == 0) {
1629 arg->type = ARGT_UINT;
1630 arg->data.uint = IT_ASCII;
1631 return 1;
1632 }
1633
1634 else if (strcmp(arg->data.str.str, "utf8") == 0) {
1635 arg->type = ARGT_UINT;
1636 arg->data.uint = IT_UTF8;
1637 return 1;
1638 }
1639
1640 else if (strcmp(arg->data.str.str, "utf8s") == 0) {
1641 arg->type = ARGT_UINT;
1642 arg->data.uint = IT_UTF8S;
1643 return 1;
1644 }
1645
1646 else if (strcmp(arg->data.str.str, "utf8p") == 0) {
1647 arg->type = ARGT_UINT;
1648 arg->data.uint = IT_UTF8P;
1649 return 1;
1650 }
1651
1652 else if (strcmp(arg->data.str.str, "utf8ps") == 0) {
1653 arg->type = ARGT_UINT;
1654 arg->data.uint = IT_UTF8PS;
1655 return 1;
1656 }
1657
1658 memprintf(err, "Unexpected input code type at file '%s', line %d. "
1659 "Allowed value are 'ascii', 'utf8', 'utf8p' and 'utf8pp'", file, line);
1660 return 0;
1661}
1662
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001663static int sample_conv_json(const struct arg *arg_p, struct sample *smp, void *private)
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001664{
1665 struct chunk *temp;
1666 char _str[7]; /* \u + 4 hex digit + null char for sprintf. */
1667 const char *str;
1668 int len;
1669 enum input_type input_type = IT_ASCII;
1670 unsigned int c;
1671 unsigned int ret;
1672 char *p;
1673
1674 if (arg_p)
1675 input_type = arg_p->data.uint;
1676
1677 temp = get_trash_chunk();
1678 temp->len = 0;
1679
1680 p = smp->data.str.str;
1681 while (p < smp->data.str.str + smp->data.str.len) {
1682
1683 if (input_type == IT_ASCII) {
1684 /* Read input as ASCII. */
1685 c = *(unsigned char *)p;
1686 p++;
1687 }
1688 else {
1689 /* Read input as UTF8. */
1690 ret = utf8_next(p, smp->data.str.len - ( p - smp->data.str.str ), &c);
1691 p += utf8_return_length(ret);
1692
1693 if (input_type == IT_UTF8 && utf8_return_code(ret) != UTF8_CODE_OK)
1694 return 0;
1695 if (input_type == IT_UTF8S && utf8_return_code(ret) != UTF8_CODE_OK)
1696 continue;
1697 if (input_type == IT_UTF8P && utf8_return_code(ret) & (UTF8_CODE_INVRANGE|UTF8_CODE_BADSEQ))
1698 return 0;
1699 if (input_type == IT_UTF8PS && utf8_return_code(ret) & (UTF8_CODE_INVRANGE|UTF8_CODE_BADSEQ))
1700 continue;
1701
1702 /* Check too big values. */
1703 if ((unsigned int)c > 0xffff) {
1704 if (input_type == IT_UTF8 || input_type == IT_UTF8P)
1705 return 0;
1706 continue;
1707 }
1708 }
1709
1710 /* Convert character. */
1711 if (c == '"') {
1712 len = 2;
1713 str = "\\\"";
1714 }
1715 else if (c == '\\') {
1716 len = 2;
1717 str = "\\\\";
1718 }
1719 else if (c == '/') {
1720 len = 2;
1721 str = "\\/";
1722 }
1723 else if (c == '\b') {
1724 len = 2;
1725 str = "\\b";
1726 }
1727 else if (c == '\f') {
1728 len = 2;
1729 str = "\\f";
1730 }
1731 else if (c == '\r') {
1732 len = 2;
1733 str = "\\r";
1734 }
1735 else if (c == '\n') {
1736 len = 2;
1737 str = "\\n";
1738 }
1739 else if (c == '\t') {
1740 len = 2;
1741 str = "\\t";
1742 }
1743 else if (c > 0xff || !isprint(c)) {
1744 /* isprint generate a segfault if c is too big. The man says that
1745 * c must have the value of an unsigned char or EOF.
1746 */
1747 len = 6;
1748 _str[0] = '\\';
1749 _str[1] = 'u';
1750 snprintf(&_str[2], 5, "%04x", (unsigned short)c);
1751 str = _str;
1752 }
1753 else {
1754 len = 1;
1755 str = (char *)&c;
1756 }
1757
1758 /* Check length */
1759 if (temp->len + len > temp->size)
1760 return 0;
1761
1762 /* Copy string. */
1763 memcpy(temp->str + temp->len, str, len);
1764 temp->len += len;
1765 }
1766
1767 smp->flags &= ~SMP_F_CONST;
1768 smp->data.str = *temp;
1769 smp->type = SMP_T_STR;
1770
1771 return 1;
1772}
1773
Emeric Brun54c4ac82014-11-03 15:32:43 +01001774/* This sample function is designed to extract some bytes from an input buffer.
1775 * First arg is the offset.
1776 * Optional second arg is the length to truncate */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001777static int sample_conv_bytes(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brun54c4ac82014-11-03 15:32:43 +01001778{
1779 if (smp->data.str.len <= arg_p[0].data.uint) {
1780 smp->data.str.len = 0;
1781 return 1;
1782 }
1783
1784 if (smp->data.str.size)
1785 smp->data.str.size -= arg_p[0].data.uint;
1786 smp->data.str.len -= arg_p[0].data.uint;
1787 smp->data.str.str += arg_p[0].data.uint;
1788
1789 if ((arg_p[1].type == ARGT_UINT) && (arg_p[1].data.uint < smp->data.str.len))
1790 smp->data.str.len = arg_p[1].data.uint;
1791
1792 return 1;
1793}
1794
Emeric Brunf399b0d2014-11-03 17:07:03 +01001795static int sample_conv_field_check(struct arg *args, struct sample_conv *conv,
1796 const char *file, int line, char **err)
1797{
1798 struct arg *arg = args;
1799
1800 if (!arg) {
1801 memprintf(err, "Unexpected empty arg list");
1802 return 0;
1803 }
1804
1805 if (arg->type != ARGT_UINT) {
1806 memprintf(err, "Unexpected arg type");
1807 return 0;
1808 }
1809
1810 if (!arg->data.uint) {
1811 memprintf(err, "Unexpected value 0 for index");
1812 return 0;
1813 }
1814
1815 arg++;
1816
1817 if (arg->type != ARGT_STR) {
1818 memprintf(err, "Unexpected arg type");
1819 return 0;
1820 }
1821
1822 if (!arg->data.str.len) {
1823 memprintf(err, "Empty separators list");
1824 return 0;
1825 }
1826
1827 return 1;
1828}
1829
1830/* This sample function is designed to a return selected part of a string (field).
1831 * First arg is the index of the field (start at 1)
1832 * Second arg is a char list of separators (type string)
1833 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001834static int sample_conv_field(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brunf399b0d2014-11-03 17:07:03 +01001835{
1836 unsigned int field;
1837 char *start, *end;
1838 int i;
1839
1840 if (!arg_p[0].data.uint)
1841 return 0;
1842
1843 field = 1;
1844 end = start = smp->data.str.str;
1845 while (end - smp->data.str.str < smp->data.str.len) {
1846
1847 for (i = 0 ; i < arg_p[1].data.str.len ; i++) {
1848 if (*end == arg_p[1].data.str.str[i]) {
1849 if (field == arg_p[0].data.uint)
1850 goto found;
1851 start = end+1;
1852 field++;
1853 break;
1854 }
1855 }
1856 end++;
1857 }
1858
1859 /* Field not found */
1860 if (field != arg_p[0].data.uint) {
1861 smp->data.str.len = 0;
1862 return 1;
1863 }
1864found:
1865 smp->data.str.len = end - start;
1866 /* If ret string is len 0, no need to
1867 change pointers or to update size */
1868 if (!smp->data.str.len)
1869 return 1;
1870
1871 smp->data.str.str = start;
1872
1873 /* Compute remaining size if needed
1874 Note: smp->data.str.size cannot be set to 0 */
1875 if (smp->data.str.size)
1876 smp->data.str.size -= start - smp->data.str.str;
1877
1878 return 1;
1879}
1880
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01001881/* This sample function is designed to return a word from a string.
1882 * First arg is the index of the word (start at 1)
1883 * Second arg is a char list of words separators (type string)
1884 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001885static int sample_conv_word(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01001886{
1887 unsigned int word;
1888 char *start, *end;
1889 int i, issep, inword;
1890
1891 if (!arg_p[0].data.uint)
1892 return 0;
1893
1894 word = 0;
1895 inword = 0;
1896 end = start = smp->data.str.str;
1897 while (end - smp->data.str.str < smp->data.str.len) {
1898 issep = 0;
1899 for (i = 0 ; i < arg_p[1].data.str.len ; i++) {
1900 if (*end == arg_p[1].data.str.str[i]) {
1901 issep = 1;
1902 break;
1903 }
1904 }
1905 if (!inword) {
1906 if (!issep) {
1907 word++;
1908 start = end;
1909 inword = 1;
1910 }
1911 }
1912 else if (issep) {
1913 if (word == arg_p[0].data.uint)
1914 goto found;
1915 inword = 0;
1916 }
1917 end++;
1918 }
1919
1920 /* Field not found */
1921 if (word != arg_p[0].data.uint) {
1922 smp->data.str.len = 0;
1923 return 1;
1924 }
1925found:
1926 smp->data.str.len = end - start;
1927 /* If ret string is len 0, no need to
1928 change pointers or to update size */
1929 if (!smp->data.str.len)
1930 return 1;
1931
1932 smp->data.str.str = start;
1933
1934 /* Compute remaining size if needed
1935 Note: smp->data.str.size cannot be set to 0 */
1936 if (smp->data.str.size)
1937 smp->data.str.size -= start - smp->data.str.str;
1938
1939 return 1;
1940}
1941
Willy Tarreau7eda8492015-01-20 19:47:06 +01001942static int sample_conv_regsub_check(struct arg *args, struct sample_conv *conv,
1943 const char *file, int line, char **err)
1944{
1945 struct arg *arg = args;
1946 char *p;
1947 int len;
1948
1949 /* arg0 is a regex, it uses type_flag for ICASE and global match */
1950 arg[0].type_flags = 0;
1951
1952 if (arg[2].type != ARGT_STR)
1953 return 1;
1954
1955 p = arg[2].data.str.str;
1956 len = arg[2].data.str.len;
1957 while (len) {
1958 if (*p == 'i') {
1959 arg[0].type_flags |= ARGF_REG_ICASE;
1960 }
1961 else if (*p == 'g') {
1962 arg[0].type_flags |= ARGF_REG_GLOB;
1963 }
1964 else {
1965 memprintf(err, "invalid regex flag '%c', only 'i' and 'g' are supported", *p);
1966 return 0;
1967 }
1968 p++;
1969 len--;
1970 }
1971 return 1;
1972}
1973
1974/* This sample function is designed to do the equivalent of s/match/replace/ on
1975 * the input string. It applies a regex and restarts from the last matched
1976 * location until nothing matches anymore. First arg is the regex to apply to
1977 * the input string, second arg is the replacement expression.
1978 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001979static int sample_conv_regsub(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau7eda8492015-01-20 19:47:06 +01001980{
1981 char *start, *end;
1982 struct my_regex *reg = arg_p[0].data.reg;
1983 regmatch_t pmatch[MAX_MATCH];
1984 struct chunk *trash = get_trash_chunk();
1985 int flag, max;
1986 int found;
1987
1988 start = smp->data.str.str;
1989 end = start + smp->data.str.len;
1990
1991 flag = 0;
1992 while (1) {
1993 /* check for last round which is used to copy remaining parts
1994 * when not running in global replacement mode.
1995 */
1996 found = 0;
1997 if ((arg_p[0].type_flags & ARGF_REG_GLOB) || !(flag & REG_NOTBOL)) {
1998 /* Note: we can have start == end on empty strings or at the end */
1999 found = regex_exec_match2(reg, start, end - start, MAX_MATCH, pmatch, flag);
2000 }
2001
2002 if (!found)
2003 pmatch[0].rm_so = end - start;
2004
2005 /* copy the heading non-matching part (which may also be the tail if nothing matches) */
2006 max = trash->size - trash->len;
2007 if (max && pmatch[0].rm_so > 0) {
2008 if (max > pmatch[0].rm_so)
2009 max = pmatch[0].rm_so;
2010 memcpy(trash->str + trash->len, start, max);
2011 trash->len += max;
2012 }
2013
2014 if (!found)
2015 break;
2016
2017 /* replace the matching part */
2018 max = trash->size - trash->len;
2019 if (max) {
2020 if (max > arg_p[1].data.str.len)
2021 max = arg_p[1].data.str.len;
2022 memcpy(trash->str + trash->len, arg_p[1].data.str.str, max);
2023 trash->len += max;
2024 }
2025
2026 /* stop here if we're done with this string */
2027 if (start >= end)
2028 break;
2029
2030 /* We have a special case for matches of length 0 (eg: "x*y*").
2031 * These ones are considered to match in front of a character,
2032 * so we have to copy that character and skip to the next one.
2033 */
2034 if (!pmatch[0].rm_eo) {
2035 if (trash->len < trash->size)
2036 trash->str[trash->len++] = start[pmatch[0].rm_eo];
2037 pmatch[0].rm_eo++;
2038 }
2039
2040 start += pmatch[0].rm_eo;
2041 flag |= REG_NOTBOL;
2042 }
2043
2044 smp->data.str = *trash;
2045 return 1;
2046}
2047
Willy Tarreau97707872015-01-27 15:12:13 +01002048/* Takes a UINT on input, applies a binary twos complement and returns the UINT
2049 * result.
2050 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002051static int sample_conv_binary_cpl(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002052{
2053 smp->data.uint = ~smp->data.uint;
2054 return 1;
2055}
2056
2057/* Takes a UINT on input, applies a binary "and" with the UINT in arg_p, and
2058 * returns the UINT result.
2059 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002060static int sample_conv_binary_and(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002061{
2062 smp->data.uint &= arg_p->data.uint;
2063 return 1;
2064}
2065
2066/* Takes a UINT on input, applies a binary "or" with the UINT in arg_p, and
2067 * returns the UINT result.
2068 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002069static int sample_conv_binary_or(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002070{
2071 smp->data.uint |= arg_p->data.uint;
2072 return 1;
2073}
2074
2075/* Takes a UINT on input, applies a binary "xor" with the UINT in arg_p, and
2076 * returns the UINT result.
2077 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002078static int sample_conv_binary_xor(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002079{
2080 smp->data.uint ^= arg_p->data.uint;
2081 return 1;
2082}
2083
2084/* Takes a UINT on input, applies an arithmetic "add" with the UINT in arg_p,
2085 * and returns the UINT result.
2086 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002087static int sample_conv_arith_add(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002088{
2089 smp->data.uint += arg_p->data.uint;
2090 return 1;
2091}
2092
2093/* Takes a UINT on input, applies an arithmetic "sub" with the UINT in arg_p,
2094 * and returns the UINT result.
2095 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002096static int sample_conv_arith_sub(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002097 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002098{
2099 smp->data.uint -= arg_p->data.uint;
2100 return 1;
2101}
2102
2103/* Takes a UINT on input, applies an arithmetic "mul" with the UINT in arg_p,
2104 * and returns the UINT result.
2105 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002106static int sample_conv_arith_mul(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002107 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002108{
2109 smp->data.uint *= arg_p->data.uint;
2110 return 1;
2111}
2112
2113/* Takes a UINT on input, applies an arithmetic "div" with the UINT in arg_p,
2114 * and returns the UINT result. If arg_p makes the result overflow, then the
2115 * largest possible quantity is returned.
2116 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002117static int sample_conv_arith_div(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002118 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002119{
2120 if (arg_p->data.uint)
2121 smp->data.uint /= arg_p->data.uint;
2122 else
2123 smp->data.uint = ~0;
2124 return 1;
2125}
2126
2127/* Takes a UINT on input, applies an arithmetic "mod" with the UINT in arg_p,
2128 * and returns the UINT result. If arg_p makes the result overflow, then zero
2129 * is returned.
2130 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002131static int sample_conv_arith_mod(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002132 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002133{
2134 if (arg_p->data.uint)
2135 smp->data.uint %= arg_p->data.uint;
2136 else
2137 smp->data.uint = 0;
2138 return 1;
2139}
2140
2141/* Takes an UINT on input, applies an arithmetic "neg" and returns the UINT
2142 * result.
2143 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002144static int sample_conv_arith_neg(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002145 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002146{
2147 smp->data.uint = -smp->data.uint;
2148 return 1;
2149}
2150
2151/* Takes a UINT on input, returns true is the value is non-null, otherwise
2152 * false. The output is a BOOL.
2153 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002154static int sample_conv_arith_bool(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002155 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002156{
2157 smp->data.uint = !!smp->data.uint;
2158 smp->type = SMP_T_BOOL;
2159 return 1;
2160}
2161
2162/* Takes a UINT on input, returns false is the value is non-null, otherwise
2163 * truee. The output is a BOOL.
2164 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002165static int sample_conv_arith_not(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002166 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002167{
2168 smp->data.uint = !smp->data.uint;
2169 smp->type = SMP_T_BOOL;
2170 return 1;
2171}
2172
2173/* Takes a UINT on input, returns true is the value is odd, otherwise false.
2174 * The output is a BOOL.
2175 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002176static int sample_conv_arith_odd(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002177 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002178{
2179 smp->data.uint = smp->data.uint & 1;
2180 smp->type = SMP_T_BOOL;
2181 return 1;
2182}
2183
2184/* Takes a UINT on input, returns true is the value is even, otherwise false.
2185 * The output is a BOOL.
2186 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002187static int sample_conv_arith_even(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002188 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002189{
2190 smp->data.uint = !(smp->data.uint & 1);
2191 smp->type = SMP_T_BOOL;
2192 return 1;
2193}
2194
Willy Tarreau5b8ad222013-07-25 12:17:57 +02002195/************************************************************************/
2196/* All supported sample fetch functions must be declared here */
2197/************************************************************************/
2198
2199/* force TRUE to be returned at the fetch level */
2200static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002201smp_fetch_true(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau5b8ad222013-07-25 12:17:57 +02002202{
2203 smp->type = SMP_T_BOOL;
2204 smp->data.uint = 1;
2205 return 1;
2206}
2207
2208/* force FALSE to be returned at the fetch level */
2209static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002210smp_fetch_false(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau5b8ad222013-07-25 12:17:57 +02002211{
2212 smp->type = SMP_T_BOOL;
2213 smp->data.uint = 0;
2214 return 1;
2215}
2216
2217/* retrieve environment variable $1 as a string */
2218static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002219smp_fetch_env(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau5b8ad222013-07-25 12:17:57 +02002220{
2221 char *env;
2222
2223 if (!args || args[0].type != ARGT_STR)
2224 return 0;
2225
2226 env = getenv(args[0].data.str.str);
2227 if (!env)
2228 return 0;
2229
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01002230 smp->type = SMP_T_STR;
2231 smp->flags = SMP_F_CONST;
Willy Tarreau5b8ad222013-07-25 12:17:57 +02002232 smp->data.str.str = env;
2233 smp->data.str.len = strlen(env);
2234 return 1;
2235}
2236
Willy Tarreau6236d3a2013-07-25 14:28:25 +02002237/* retrieve the current local date in epoch time, and applies an optional offset
2238 * of args[0] seconds.
2239 */
2240static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002241smp_fetch_date(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau6236d3a2013-07-25 14:28:25 +02002242{
2243 smp->data.uint = date.tv_sec;
2244
2245 /* add offset */
2246 if (args && (args[0].type == ARGT_SINT || args[0].type == ARGT_UINT))
2247 smp->data.uint += args[0].data.sint;
2248
2249 smp->type = SMP_T_UINT;
2250 smp->flags |= SMP_F_VOL_TEST | SMP_F_MAY_CHANGE;
2251 return 1;
2252}
2253
Willy Tarreau0f30d262014-11-24 16:02:05 +01002254/* returns the number of processes */
2255static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002256smp_fetch_nbproc(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau0f30d262014-11-24 16:02:05 +01002257{
2258 smp->type = SMP_T_UINT;
2259 smp->data.uint = global.nbproc;
2260 return 1;
2261}
2262
2263/* returns the number of the current process (between 1 and nbproc */
2264static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002265smp_fetch_proc(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau0f30d262014-11-24 16:02:05 +01002266{
2267 smp->type = SMP_T_UINT;
2268 smp->data.uint = relative_pid;
2269 return 1;
2270}
2271
Willy Tarreau84310e22014-02-14 11:59:04 +01002272/* generate a random 32-bit integer for whatever purpose, with an optional
2273 * range specified in argument.
2274 */
2275static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002276smp_fetch_rand(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau84310e22014-02-14 11:59:04 +01002277{
2278 smp->data.uint = random();
2279
2280 /* reduce if needed. Don't do a modulo, use all bits! */
2281 if (args && args[0].type == ARGT_UINT)
Vincent Bernat1228dc02014-12-10 10:31:37 +01002282 smp->data.uint = ((uint64_t)smp->data.uint * args[0].data.uint) / ((u64)RAND_MAX+1);
Willy Tarreau84310e22014-02-14 11:59:04 +01002283
2284 smp->type = SMP_T_UINT;
2285 smp->flags |= SMP_F_VOL_TEST | SMP_F_MAY_CHANGE;
2286 return 1;
2287}
2288
Willy Tarreau0f30d262014-11-24 16:02:05 +01002289/* returns true if the current process is stopping */
2290static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002291smp_fetch_stopping(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau0f30d262014-11-24 16:02:05 +01002292{
2293 smp->type = SMP_T_BOOL;
2294 smp->data.uint = stopping;
2295 return 1;
2296}
2297
Thierry FOURNIERcc103292015-06-06 19:30:17 +02002298static int smp_fetch_const_str(const struct arg *args, struct sample *smp, const char *kw, void *private)
2299{
2300 smp->flags |= SMP_F_CONST;
2301 smp->type = SMP_T_STR;
2302 smp->data.str.str = args[0].data.str.str;
2303 smp->data.str.len = args[0].data.str.len;
2304 return 1;
2305}
2306
2307static int smp_check_const_bool(struct arg *args, char **err)
2308{
2309 if (strcasecmp(args[0].data.str.str, "true") == 0 ||
2310 strcasecmp(args[0].data.str.str, "1") == 0) {
2311 args[0].type = ARGT_UINT;
2312 args[0].data.uint = 1;
2313 return 1;
2314 }
2315 if (strcasecmp(args[0].data.str.str, "false") == 0 ||
2316 strcasecmp(args[0].data.str.str, "0") == 0) {
2317 args[0].type = ARGT_UINT;
2318 args[0].data.uint = 0;
2319 return 1;
2320 }
2321 memprintf(err, "Expects 'true', 'false', '0' or '1'");
2322 return 0;
2323}
2324
2325static int smp_fetch_const_bool(const struct arg *args, struct sample *smp, const char *kw, void *private)
2326{
2327 smp->type = SMP_T_BOOL;
2328 smp->data.uint = args[0].data.uint;
2329 return 1;
2330}
2331
2332static int smp_fetch_const_uint(const struct arg *args, struct sample *smp, const char *kw, void *private)
2333{
2334 smp->type = SMP_T_UINT;
2335 smp->data.uint = args[0].data.uint;
2336 return 1;
2337}
2338
2339static int smp_fetch_const_sint(const struct arg *args, struct sample *smp, const char *kw, void *private)
2340{
2341 smp->type = SMP_T_SINT;
2342 smp->data.sint = args[0].data.sint;
2343 return 1;
2344}
2345
2346static int smp_fetch_const_ipv4(const struct arg *args, struct sample *smp, const char *kw, void *private)
2347{
2348 smp->type = SMP_T_IPV4;
2349 smp->data.ipv4 = args[0].data.ipv4;
2350 return 1;
2351}
2352
2353static int smp_fetch_const_ipv6(const struct arg *args, struct sample *smp, const char *kw, void *private)
2354{
2355 smp->type = SMP_T_IPV6;
2356 smp->data.ipv6 = args[0].data.ipv6;
2357 return 1;
2358}
2359
2360static int smp_check_const_bin(struct arg *args, char **err)
2361{
2362 char *binstr;
2363 int binstrlen;
2364
2365 if (!parse_binary(args[0].data.str.str, &binstr, &binstrlen, err))
2366 return 0;
2367 args[0].type = ARGT_STR;
2368 args[0].data.str.str = binstr;
2369 args[0].data.str.len = binstrlen;
2370 return 1;
2371}
2372
2373static int smp_fetch_const_bin(const struct arg *args, struct sample *smp, const char *kw, void *private)
2374{
2375 smp->flags |= SMP_F_CONST;
2376 smp->type = SMP_T_BIN;
2377 smp->data.str.str = args[0].data.str.str;
2378 smp->data.str.len = args[0].data.str.len;
2379 return 1;
2380}
2381
2382static int smp_check_const_meth(struct arg *args, char **err)
2383{
2384 enum http_meth_t meth;
2385 int i;
2386
2387 meth = find_http_meth(args[0].data.str.str, args[0].data.str.len);
2388 if (meth != HTTP_METH_OTHER) {
2389 args[0].type = ARGT_UINT;
2390 args[0].data.uint = meth;
2391 } else {
2392 /* Check method avalaibility. A methos is a token defined as :
2393 * tchar = "!" / "#" / "$" / "%" / "&" / "'" / "*" / "+" / "-" / "." /
2394 * "^" / "_" / "`" / "|" / "~" / DIGIT / ALPHA
2395 * token = 1*tchar
2396 */
2397 for (i = 0; i < args[0].data.str.len; i++) {
2398 if (!http_is_token[(unsigned char)args[0].data.str.str[i]]) {
2399 memprintf(err, "expects valid method.");
2400 return 0;
2401 }
2402 }
2403 }
2404 return 1;
2405}
2406
2407static int smp_fetch_const_meth(const struct arg *args, struct sample *smp, const char *kw, void *private)
2408{
2409 smp->type = SMP_T_METH;
2410 if (args[0].type == ARGT_UINT) {
2411 smp->flags &= ~SMP_F_CONST;
2412 smp->data.meth.meth = args[0].data.uint;
2413 smp->data.meth.str.str = "";
2414 smp->data.meth.str.len = 0;
2415 } else {
2416 smp->flags |= SMP_F_CONST;
2417 smp->data.meth.meth = HTTP_METH_OTHER;
2418 smp->data.meth.str.str = args[0].data.str.str;
2419 smp->data.meth.str.len = args[0].data.str.len;
2420 }
2421 return 1;
2422}
2423
Willy Tarreau5b8ad222013-07-25 12:17:57 +02002424/* Note: must not be declared <const> as its list will be overwritten.
2425 * Note: fetches that may return multiple types must be declared as the lowest
2426 * common denominator, the type that can be casted into all other ones. For
2427 * instance IPv4/IPv6 must be declared IPv4.
2428 */
2429static struct sample_fetch_kw_list smp_kws = {ILH, {
2430 { "always_false", smp_fetch_false, 0, NULL, SMP_T_BOOL, SMP_USE_INTRN },
2431 { "always_true", smp_fetch_true, 0, NULL, SMP_T_BOOL, SMP_USE_INTRN },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01002432 { "env", smp_fetch_env, ARG1(1,STR), NULL, SMP_T_STR, SMP_USE_INTRN },
Willy Tarreau6236d3a2013-07-25 14:28:25 +02002433 { "date", smp_fetch_date, ARG1(0,SINT), NULL, SMP_T_UINT, SMP_USE_INTRN },
Willy Tarreau0f30d262014-11-24 16:02:05 +01002434 { "nbproc", smp_fetch_nbproc,0, NULL, SMP_T_UINT, SMP_USE_INTRN },
2435 { "proc", smp_fetch_proc, 0, NULL, SMP_T_UINT, SMP_USE_INTRN },
Willy Tarreau84310e22014-02-14 11:59:04 +01002436 { "rand", smp_fetch_rand, ARG1(0,UINT), NULL, SMP_T_UINT, SMP_USE_INTRN },
Willy Tarreau0f30d262014-11-24 16:02:05 +01002437 { "stopping", smp_fetch_stopping, 0, NULL, SMP_T_BOOL, SMP_USE_INTRN },
Thierry FOURNIERcc103292015-06-06 19:30:17 +02002438
2439 { "str", smp_fetch_const_str, ARG1(1,STR), NULL , SMP_T_STR, SMP_USE_INTRN },
2440 { "bool", smp_fetch_const_bool, ARG1(1,STR), smp_check_const_bool, SMP_T_BOOL, SMP_USE_INTRN },
2441 { "uint", smp_fetch_const_uint, ARG1(1,UINT), NULL , SMP_T_UINT, SMP_USE_INTRN },
2442 { "sint", smp_fetch_const_sint, ARG1(1,SINT), NULL , SMP_T_SINT, SMP_USE_INTRN },
2443 { "ipv4", smp_fetch_const_ipv4, ARG1(1,IPV4), NULL , SMP_T_IPV4, SMP_USE_INTRN },
2444 { "ipv6", smp_fetch_const_ipv6, ARG1(1,IPV6), NULL , SMP_T_IPV6, SMP_USE_INTRN },
2445 { "bin", smp_fetch_const_bin, ARG1(1,STR), smp_check_const_bin , SMP_T_BIN, SMP_USE_INTRN },
2446 { "meth", smp_fetch_const_meth, ARG1(1,STR), smp_check_const_meth, SMP_T_METH, SMP_USE_INTRN },
2447
Willy Tarreau5b8ad222013-07-25 12:17:57 +02002448 { /* END */ },
2449}};
2450
Emeric Brun107ca302010-01-04 16:16:05 +01002451/* Note: must not be declared <const> as its list will be overwritten */
Willy Tarreaudc13c112013-06-21 23:16:39 +02002452static struct sample_conv_kw_list sample_conv_kws = {ILH, {
Thierry FOURNIER9687c772015-05-07 15:46:29 +02002453#ifdef DEBUG_EXPR
2454 { "debug", sample_conv_debug, 0, NULL, SMP_T_ANY, SMP_T_ANY },
2455#endif
2456
Emeric Brun53d1a982014-04-30 18:21:37 +02002457 { "base64", sample_conv_bin2base64,0, NULL, SMP_T_BIN, SMP_T_STR },
Willy Tarreau12785782012-04-27 21:37:17 +02002458 { "upper", sample_conv_str2upper, 0, NULL, SMP_T_STR, SMP_T_STR },
2459 { "lower", sample_conv_str2lower, 0, NULL, SMP_T_STR, SMP_T_STR },
Thierry FOURNIER2f49d6d2014-03-12 15:01:52 +01002460 { "hex", sample_conv_bin2hex, 0, NULL, SMP_T_BIN, SMP_T_STR },
Willy Tarreau12785782012-04-27 21:37:17 +02002461 { "ipmask", sample_conv_ipmask, ARG1(1,MSK4), NULL, SMP_T_IPV4, SMP_T_IPV4 },
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02002462 { "ltime", sample_conv_ltime, ARG2(1,STR,SINT), NULL, SMP_T_UINT, SMP_T_STR },
2463 { "utime", sample_conv_utime, ARG2(1,STR,SINT), NULL, SMP_T_UINT, SMP_T_STR },
Willy Tarreau80599772015-01-20 19:35:24 +01002464 { "crc32", sample_conv_crc32, ARG1(0,UINT), NULL, SMP_T_BIN, SMP_T_UINT },
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02002465 { "djb2", sample_conv_djb2, ARG1(0,UINT), NULL, SMP_T_BIN, SMP_T_UINT },
2466 { "sdbm", sample_conv_sdbm, ARG1(0,UINT), NULL, SMP_T_BIN, SMP_T_UINT },
2467 { "wt6", sample_conv_wt6, ARG1(0,UINT), NULL, SMP_T_BIN, SMP_T_UINT },
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002468 { "json", sample_conv_json, ARG1(1,STR), sample_conv_json_check, SMP_T_STR, SMP_T_STR },
Emeric Brun54c4ac82014-11-03 15:32:43 +01002469 { "bytes", sample_conv_bytes, ARG2(1,UINT,UINT), NULL, SMP_T_BIN, SMP_T_BIN },
Emeric Brunf399b0d2014-11-03 17:07:03 +01002470 { "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 +01002471 { "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 +01002472 { "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 +01002473
2474 { "and", sample_conv_binary_and, ARG1(1,UINT), NULL, SMP_T_UINT, SMP_T_UINT },
2475 { "or", sample_conv_binary_or, ARG1(1,UINT), NULL, SMP_T_UINT, SMP_T_UINT },
2476 { "xor", sample_conv_binary_xor, ARG1(1,UINT), NULL, SMP_T_UINT, SMP_T_UINT },
2477 { "cpl", sample_conv_binary_cpl, 0, NULL, SMP_T_UINT, SMP_T_UINT },
2478 { "bool", sample_conv_arith_bool, 0, NULL, SMP_T_UINT, SMP_T_BOOL },
2479 { "not", sample_conv_arith_not, 0, NULL, SMP_T_UINT, SMP_T_BOOL },
2480 { "odd", sample_conv_arith_odd, 0, NULL, SMP_T_UINT, SMP_T_BOOL },
2481 { "even", sample_conv_arith_even, 0, NULL, SMP_T_UINT, SMP_T_BOOL },
2482 { "add", sample_conv_arith_add, ARG1(1,UINT), NULL, SMP_T_UINT, SMP_T_UINT },
2483 { "sub", sample_conv_arith_sub, ARG1(1,UINT), NULL, SMP_T_UINT, SMP_T_UINT },
2484 { "mul", sample_conv_arith_mul, ARG1(1,UINT), NULL, SMP_T_UINT, SMP_T_UINT },
2485 { "div", sample_conv_arith_div, ARG1(1,UINT), NULL, SMP_T_UINT, SMP_T_UINT },
2486 { "mod", sample_conv_arith_mod, ARG1(1,UINT), NULL, SMP_T_UINT, SMP_T_UINT },
2487 { "neg", sample_conv_arith_neg, 0, NULL, SMP_T_UINT, SMP_T_UINT },
2488
Willy Tarreau9fcb9842012-04-20 14:45:49 +02002489 { NULL, NULL, 0, 0, 0 },
Emeric Brun107ca302010-01-04 16:16:05 +01002490}};
2491
2492__attribute__((constructor))
Willy Tarreau12785782012-04-27 21:37:17 +02002493static void __sample_init(void)
Emeric Brun107ca302010-01-04 16:16:05 +01002494{
Willy Tarreau5b8ad222013-07-25 12:17:57 +02002495 /* register sample fetch and format conversion keywords */
2496 sample_register_fetches(&smp_kws);
Willy Tarreau12785782012-04-27 21:37:17 +02002497 sample_register_convs(&sample_conv_kws);
Emeric Brun107ca302010-01-04 16:16:05 +01002498}