blob: 6448bde9d1bc887d1870f3a2e1455e4ef95e9a4f [file] [log] [blame]
Willy Tarreau79e57332018-10-02 16:01:16 +02001/*
2 * HTTP samples fetching
3 *
4 * Copyright 2000-2018 Willy Tarreau <w@1wt.eu>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
13#include <sys/types.h>
14
15#include <ctype.h>
16#include <string.h>
17#include <time.h>
18
19#include <common/base64.h>
20#include <common/chunk.h>
21#include <common/compat.h>
22#include <common/config.h>
23#include <common/debug.h>
Willy Tarreauafba57a2018-12-11 13:44:24 +010024#include <common/h1.h>
Willy Tarreau79e57332018-10-02 16:01:16 +020025#include <common/http.h>
Willy Tarreaub96b77e2018-12-11 10:22:41 +010026#include <common/htx.h>
Willy Tarreau0108d902018-11-25 19:14:37 +010027#include <common/initcall.h>
Willy Tarreau79e57332018-10-02 16:01:16 +020028#include <common/memory.h>
29#include <common/standard.h>
30#include <common/version.h>
31
32#include <types/global.h>
33
34#include <proto/arg.h>
35#include <proto/auth.h>
Willy Tarreau538746a2018-12-11 10:59:20 +010036#include <proto/hdr_idx.h>
Willy Tarreau79e57332018-10-02 16:01:16 +020037#include <proto/http_fetch.h>
Christopher Fauletef453ed2018-10-24 21:39:27 +020038#include <proto/http_htx.h>
Willy Tarreau79e57332018-10-02 16:01:16 +020039#include <proto/log.h>
40#include <proto/obj_type.h>
41#include <proto/proto_http.h>
42#include <proto/sample.h>
43#include <proto/stream.h>
44
45
46/* this struct is used between calls to smp_fetch_hdr() or smp_fetch_cookie() */
47static THREAD_LOCAL struct hdr_ctx static_hdr_ctx;
Christopher Fauletef453ed2018-10-24 21:39:27 +020048static THREAD_LOCAL struct http_hdr_ctx static_http_hdr_ctx;
Richard Russoc0968f52019-07-31 11:45:56 -070049/* this is used to convert raw connection buffers to htx */
50static THREAD_LOCAL struct buffer static_raw_htx_chunk;
51static THREAD_LOCAL char *static_raw_htx_buf;
Christopher Fauletef453ed2018-10-24 21:39:27 +020052
Christopher Faulet89dc4992019-04-17 12:02:59 +020053#define SMP_REQ_CHN(smp) (smp->strm ? &smp->strm->req : NULL)
54#define SMP_RES_CHN(smp) (smp->strm ? &smp->strm->res : NULL)
Willy Tarreau79e57332018-10-02 16:01:16 +020055
Richard Russoc0968f52019-07-31 11:45:56 -070056/* This function returns the static htx chunk, where raw connections get
57 * converted to HTX as needed for samplxsing.
58 */
59struct buffer *get_raw_htx_chunk(void)
60{
61 chunk_reset(&static_raw_htx_chunk);
62 return &static_raw_htx_chunk;
63}
64
65static int alloc_raw_htx_chunk_per_thread()
66{
67 static_raw_htx_buf = malloc(global.tune.bufsize);
68 if (!static_raw_htx_buf)
69 return 0;
70 chunk_init(&static_raw_htx_chunk, static_raw_htx_buf, global.tune.bufsize);
71 return 1;
72}
73
74static void free_raw_htx_chunk_per_thread()
75{
76 free(static_raw_htx_buf);
77 static_raw_htx_buf = NULL;
78}
79
80REGISTER_PER_THREAD_ALLOC(alloc_raw_htx_chunk_per_thread);
81REGISTER_PER_THREAD_FREE(free_raw_htx_chunk_per_thread);
82
Willy Tarreau79e57332018-10-02 16:01:16 +020083/*
84 * Returns the data from Authorization header. Function may be called more
85 * than once so data is stored in txn->auth_data. When no header is found
86 * or auth method is unknown auth_method is set to HTTP_AUTH_WRONG to avoid
87 * searching again for something we are unable to find anyway. However, if
88 * the result if valid, the cache is not reused because we would risk to
89 * have the credentials overwritten by another stream in parallel.
90 */
91
Christopher Faulete98411b2019-07-15 13:58:29 +020092static int get_http_auth(struct sample *smp, struct htx *htx)
Willy Tarreau79e57332018-10-02 16:01:16 +020093{
Christopher Faulet311c7ea2018-10-24 21:41:55 +020094 struct stream *s = smp->strm;
Willy Tarreau79e57332018-10-02 16:01:16 +020095 struct http_txn *txn = s->txn;
96 struct buffer auth_method;
Willy Tarreau79e57332018-10-02 16:01:16 +020097 char *h, *p;
98 int len;
99
100#ifdef DEBUG_AUTH
101 printf("Auth for stream %p: %d\n", s, txn->auth.method);
102#endif
Willy Tarreau79e57332018-10-02 16:01:16 +0200103 if (txn->auth.method == HTTP_AUTH_WRONG)
104 return 0;
105
106 txn->auth.method = HTTP_AUTH_WRONG;
107
Christopher Faulete98411b2019-07-15 13:58:29 +0200108 if (htx) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200109 /* HTX version */
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200110 struct http_hdr_ctx ctx = { .blk = NULL };
111 struct ist hdr;
Willy Tarreau79e57332018-10-02 16:01:16 +0200112
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200113 if (txn->flags & TX_USE_PX_CONN)
114 hdr = ist("Proxy-Authorization");
115 else
116 hdr = ist("Authorization");
117
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200118 ctx.blk = NULL;
119 if (!http_find_header(htx, hdr, &ctx, 0))
120 return 0;
121
122 p = memchr(ctx.value.ptr, ' ', ctx.value.len);
123 len = p - ctx.value.ptr;
124 if (!p || len <= 0)
125 return 0;
126
127 if (chunk_initlen(&auth_method, ctx.value.ptr, 0, len) != 1)
128 return 0;
129
130 chunk_initlen(&txn->auth.method_data, p + 1, 0, ctx.value.len - len - 1);
Willy Tarreau79e57332018-10-02 16:01:16 +0200131 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200132 else {
133 /* LEGACY version */
134 struct hdr_ctx ctx = { .idx = 0 };
Willy Tarreau79e57332018-10-02 16:01:16 +0200135
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200136 if (txn->flags & TX_USE_PX_CONN) {
137 h = "Proxy-Authorization";
138 len = strlen(h);
139 } else {
140 h = "Authorization";
141 len = strlen(h);
142 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200143
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200144 if (!http_find_header2(h, len, ci_head(&s->req), &txn->hdr_idx, &ctx))
145 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200146
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200147 h = ctx.line + ctx.val;
Willy Tarreau79e57332018-10-02 16:01:16 +0200148
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200149 p = memchr(h, ' ', ctx.vlen);
150 len = p - h;
151 if (!p || len <= 0)
152 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200153
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200154 if (chunk_initlen(&auth_method, h, 0, len) != 1)
155 return 0;
156
157 chunk_initlen(&txn->auth.method_data, p + 1, 0, ctx.vlen - len - 1);
158 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200159
160 if (!strncasecmp("Basic", auth_method.area, auth_method.data)) {
161 struct buffer *http_auth = get_trash_chunk();
162
163 len = base64dec(txn->auth.method_data.area,
164 txn->auth.method_data.data,
165 http_auth->area, global.tune.bufsize - 1);
166
167 if (len < 0)
168 return 0;
169
170
171 http_auth->area[len] = '\0';
172
173 p = strchr(http_auth->area, ':');
174
175 if (!p)
176 return 0;
177
178 txn->auth.user = http_auth->area;
179 *p = '\0';
180 txn->auth.pass = p+1;
181
182 txn->auth.method = HTTP_AUTH_BASIC;
183 return 1;
184 }
185
186 return 0;
187}
188
189/* This function ensures that the prerequisites for an L7 fetch are ready,
190 * which means that a request or response is ready. If some data is missing,
191 * a parsing attempt is made. This is useful in TCP-based ACLs which are able
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +0200192 * to extract data from L7. If <vol> is non-null during a prefetch, another
193 * test is made to ensure the required information is not gone.
Christopher Fauletef453ed2018-10-24 21:39:27 +0200194 *
195 * The function returns :
196 * NULL with SMP_F_MAY_CHANGE in the sample flags if some data is missing to
197 * decide whether or not an HTTP message is present ;
198 * NULL if the requested data cannot be fetched or if it is certain that
199 * we'll never have any HTTP message there ;
200 * The HTX message if ready
201 */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +0200202struct htx *smp_prefetch_htx(struct sample *smp, struct channel *chn, int vol)
Christopher Fauletef453ed2018-10-24 21:39:27 +0200203{
Christopher Fauletef453ed2018-10-24 21:39:27 +0200204 struct stream *s = smp->strm;
Christopher Fauletef453ed2018-10-24 21:39:27 +0200205 struct http_txn *txn = NULL;
206 struct htx *htx = NULL;
Christopher Faulet89dc4992019-04-17 12:02:59 +0200207 struct http_msg *msg;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100208 struct htx_sl *sl;
Christopher Fauletef453ed2018-10-24 21:39:27 +0200209
210 /* Note: it is possible that <s> is NULL when called before stream
211 * initialization (eg: tcp-request connection), so this function is the
212 * one responsible for guarding against this case for all HTTP users.
213 */
Christopher Faulet89dc4992019-04-17 12:02:59 +0200214 if (!s || !chn)
Christopher Fauletef453ed2018-10-24 21:39:27 +0200215 return NULL;
216
217 if (!s->txn) {
218 if (unlikely(!http_alloc_txn(s)))
219 return NULL; /* not enough memory */
220 http_init_txn(s);
221 txn = s->txn;
222 }
Christopher Faulet89dc4992019-04-17 12:02:59 +0200223 txn = s->txn;
224 msg = (!(chn->flags & CF_ISRESP) ? &txn->req : &txn->rsp);
225 smp->data.type = SMP_T_BOOL;
Christopher Fauletef453ed2018-10-24 21:39:27 +0200226
Christopher Fauleteca88542019-04-03 10:12:42 +0200227 if (IS_HTX_STRM(s)) {
Christopher Faulet89dc4992019-04-17 12:02:59 +0200228 htx = htxbuf(&chn->buf);
Christopher Fauletef453ed2018-10-24 21:39:27 +0200229
Christopher Faulet89dc4992019-04-17 12:02:59 +0200230 if (msg->msg_state == HTTP_MSG_ERROR || (htx->flags & HTX_FL_PARSING_ERROR))
231 return NULL;
Christopher Fauletef453ed2018-10-24 21:39:27 +0200232
Christopher Faulet89dc4992019-04-17 12:02:59 +0200233 if (msg->msg_state < HTTP_MSG_BODY) {
234 /* Analyse not yet started */
Christopher Faulet29f17582019-05-23 11:03:26 +0200235 if (htx_is_empty(htx) || htx->first == -1) {
Christopher Fauletef453ed2018-10-24 21:39:27 +0200236 /* Parsing is done by the mux, just wait */
237 smp->flags |= SMP_F_MAY_CHANGE;
238 return NULL;
239 }
240 }
Christopher Faulet297fbb42019-05-13 14:41:27 +0200241 sl = http_get_stline(htx);
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +0200242 if (vol && !sl) {
Christopher Faulet89dc4992019-04-17 12:02:59 +0200243 /* The start-line was already forwarded, it is too late to fetch anything */
244 return NULL;
245 }
Christopher Fauletef453ed2018-10-24 21:39:27 +0200246 }
Christopher Fauleteca88542019-04-03 10:12:42 +0200247 else { /* RAW mode */
Christopher Faulet89dc4992019-04-17 12:02:59 +0200248 struct buffer *buf;
249 struct h1m h1m;
Christopher Faulete4ab11b2019-06-11 15:05:37 +0200250 struct http_hdr hdrs[global.tune.max_http_hdr];
Christopher Faulet89dc4992019-04-17 12:02:59 +0200251 union h1_sl h1sl;
252 unsigned int flags = HTX_FL_NONE;
253 int ret;
Christopher Fauletef453ed2018-10-24 21:39:27 +0200254
Christopher Faulet89dc4992019-04-17 12:02:59 +0200255 /* no HTTP fetch on the response in TCP mode */
256 if (chn->flags & CF_ISRESP)
257 return NULL;
Christopher Fauletef453ed2018-10-24 21:39:27 +0200258
Christopher Faulet89dc4992019-04-17 12:02:59 +0200259 /* Now we are working on the request only */
260 buf = &chn->buf;
261 if (b_head(buf) + b_data(buf) > b_wrap(buf))
262 b_slow_realign(buf, trash.area, 0);
Christopher Fauletef453ed2018-10-24 21:39:27 +0200263
Christopher Faulet89dc4992019-04-17 12:02:59 +0200264 h1m_init_req(&h1m);
265 ret = h1_headers_to_hdr_list(b_head(buf), b_stop(buf),
266 hdrs, sizeof(hdrs)/sizeof(hdrs[0]), &h1m, &h1sl);
267 if (ret <= 0) {
268 /* Invalid or too big*/
269 if (ret < 0 || channel_full(&s->req, global.tune.maxrewrite))
Christopher Fauletef453ed2018-10-24 21:39:27 +0200270 return NULL;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100271
Christopher Faulet89dc4992019-04-17 12:02:59 +0200272 /* wait for a full request */
273 smp->flags |= SMP_F_MAY_CHANGE;
274 return NULL;
275 }
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100276
Christopher Faulet89dc4992019-04-17 12:02:59 +0200277 /* OK we just got a valid HTTP mesage. We have to convert it
278 * into an HTX message.
279 */
280 if (unlikely(h1sl.rq.v.len == 0)) {
281 /* try to convert HTTP/0.9 requests to HTTP/1.0 */
282 if (h1sl.rq.meth != HTTP_METH_GET || !h1sl.rq.u.len)
Christopher Fauletef453ed2018-10-24 21:39:27 +0200283 return NULL;
Christopher Faulet89dc4992019-04-17 12:02:59 +0200284 h1sl.rq.v = ist("HTTP/1.0");
Christopher Fauletef453ed2018-10-24 21:39:27 +0200285 }
Christopher Faulet89dc4992019-04-17 12:02:59 +0200286
287 /* Set HTX start-line flags */
288 if (h1m.flags & H1_MF_VER_11)
289 flags |= HTX_SL_F_VER_11;
290 if (h1m.flags & H1_MF_XFER_ENC)
291 flags |= HTX_SL_F_XFER_ENC;
292 flags |= HTX_SL_F_XFER_LEN;
293 if (h1m.flags & H1_MF_CHNK)
294 flags |= HTX_SL_F_CHNK;
295 else if (h1m.flags & H1_MF_CLEN)
296 flags |= HTX_SL_F_CLEN;
297
Richard Russoc0968f52019-07-31 11:45:56 -0700298 htx = htx_from_buf(get_raw_htx_chunk());
Christopher Faulet89dc4992019-04-17 12:02:59 +0200299 sl = htx_add_stline(htx, HTX_BLK_REQ_SL, flags, h1sl.rq.m, h1sl.rq.u, h1sl.rq.v);
300 if (!sl || !htx_add_all_headers(htx, hdrs))
Christopher Fauletef453ed2018-10-24 21:39:27 +0200301 return NULL;
Willy Tarreauce9bbf52019-05-13 08:32:31 +0200302 sl->info.req.meth = h1sl.rq.meth;
Christopher Faulet89dc4992019-04-17 12:02:59 +0200303 }
304
305 /* OK we just got a valid HTTP message. If not already done by
306 * HTTP analyzers, we have some minor preparation to perform so
307 * that further checks can rely on HTTP tests.
308 */
309 if (sl && msg->msg_state < HTTP_MSG_BODY) {
310 if (!(chn->flags & CF_ISRESP)) {
311 txn->meth = sl->info.req.meth;
312 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
313 s->flags |= SF_REDIRECTABLE;
Christopher Fauletef453ed2018-10-24 21:39:27 +0200314 }
Christopher Faulet89dc4992019-04-17 12:02:59 +0200315 else
316 txn->status = sl->info.res.status;
317 if (sl->flags & HTX_SL_F_VER_11)
318 msg->flags |= HTTP_MSGF_VER_11;
Christopher Fauletef453ed2018-10-24 21:39:27 +0200319 }
320
321 /* everything's OK */
322 smp->data.u.sint = 1;
323 return htx;
324}
325
326/* This function ensures that the prerequisites for an L7 fetch are ready,
327 * which means that a request or response is ready. If some data is missing,
328 * a parsing attempt is made. This is useful in TCP-based ACLs which are able
Willy Tarreau79e57332018-10-02 16:01:16 +0200329 * to extract data from L7. If <req_vol> is non-null during a request prefetch,
330 * another test is made to ensure the required information is not gone.
331 *
332 * The function returns :
333 * 0 with SMP_F_MAY_CHANGE in the sample flags if some data is missing to
334 * decide whether or not an HTTP message is present ;
335 * 0 if the requested data cannot be fetched or if it is certain that
336 * we'll never have any HTTP message there ;
337 * 1 if an HTTP message is ready
338 */
339int smp_prefetch_http(struct proxy *px, struct stream *s, unsigned int opt,
Christopher Faulet89dc4992019-04-17 12:02:59 +0200340 struct channel *chn, struct sample *smp, int req_vol)
Willy Tarreau79e57332018-10-02 16:01:16 +0200341{
342 struct http_txn *txn;
343 struct http_msg *msg;
344
345 /* Note: it is possible that <s> is NULL when called before stream
346 * initialization (eg: tcp-request connection), so this function is the
347 * one responsible for guarding against this case for all HTTP users.
348 */
Christopher Faulet89dc4992019-04-17 12:02:59 +0200349 if (!s || !chn)
Willy Tarreau79e57332018-10-02 16:01:16 +0200350 return 0;
351
352 if (!s->txn) {
353 if (unlikely(!http_alloc_txn(s)))
354 return 0; /* not enough memory */
355 http_init_txn(s);
356 }
357 txn = s->txn;
Willy Tarreau79e57332018-10-02 16:01:16 +0200358 smp->data.type = SMP_T_BOOL;
359
Christopher Faulet89dc4992019-04-17 12:02:59 +0200360 if (chn->flags & CF_ISRESP) {
361 /* Check for a dependency on a response */
362 if (txn->rsp.msg_state < HTTP_MSG_BODY) {
363 smp->flags |= SMP_F_MAY_CHANGE;
364 return 0;
365 }
366 goto end;
367 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200368
Christopher Faulet89dc4992019-04-17 12:02:59 +0200369 /* Check for a dependency on a request */
370 msg = &txn->req;
Willy Tarreau79e57332018-10-02 16:01:16 +0200371
Christopher Faulet89dc4992019-04-17 12:02:59 +0200372 if (req_vol && (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) {
373 return 0; /* data might have moved and indexes changed */
374 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200375
Christopher Faulet89dc4992019-04-17 12:02:59 +0200376 /* If the buffer does not leave enough free space at the end, we must
377 * first realign it.
378 */
379 if (ci_head(chn) > b_orig(&chn->buf) &&
380 ci_head(chn) + ci_data(chn) > b_wrap(&chn->buf) - global.tune.maxrewrite)
381 channel_slow_realign(chn, trash.area);
Willy Tarreau79e57332018-10-02 16:01:16 +0200382
Christopher Faulet89dc4992019-04-17 12:02:59 +0200383 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
384 if (msg->msg_state == HTTP_MSG_ERROR)
385 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200386
Christopher Faulet89dc4992019-04-17 12:02:59 +0200387 /* Try to decode HTTP request */
388 if (likely(msg->next < ci_data(chn)))
389 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau79e57332018-10-02 16:01:16 +0200390
Christopher Faulet89dc4992019-04-17 12:02:59 +0200391 /* Still no valid request ? */
392 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
393 if ((msg->msg_state == HTTP_MSG_ERROR) ||
394 channel_full(chn, global.tune.maxrewrite)) {
Willy Tarreau79e57332018-10-02 16:01:16 +0200395 return 0;
Christopher Faulet89dc4992019-04-17 12:02:59 +0200396 }
397 /* wait for final state */
398 smp->flags |= SMP_F_MAY_CHANGE;
399 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200400 }
401
Christopher Faulet89dc4992019-04-17 12:02:59 +0200402 /* OK we just got a valid HTTP message. We have some minor
403 * preparation to perform so that further checks can rely
404 * on HTTP tests.
405 */
406
407 /* If the message was parsed but was too large, we must absolutely
408 * return an error so that it is not processed. At the moment this
409 * cannot happen, but if the parsers are to change in the future,
410 * we want this check to be maintained.
411 */
412 if (unlikely(ci_head(chn) + ci_data(chn) >
413 b_wrap(&chn->buf) - global.tune.maxrewrite)) {
414 msg->err_state = msg->msg_state;
415 msg->msg_state = HTTP_MSG_ERROR;
416 smp->data.u.sint = 1;
417 return 1;
Willy Tarreau79e57332018-10-02 16:01:16 +0200418 }
419
Christopher Faulet89dc4992019-04-17 12:02:59 +0200420 txn->meth = find_http_meth(ci_head(chn), msg->sl.rq.m_l);
421 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
422 s->flags |= SF_REDIRECTABLE;
423
424 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
Willy Tarreau79e57332018-10-02 16:01:16 +0200425 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200426 }
427
Christopher Faulet89dc4992019-04-17 12:02:59 +0200428 end:
Willy Tarreau79e57332018-10-02 16:01:16 +0200429 /* everything's OK */
430 smp->data.u.sint = 1;
431 return 1;
432}
433
434/* This function fetches the method of current HTTP request and stores
435 * it in the global pattern struct as a chunk. There are two possibilities :
436 * - if the method is known (not HTTP_METH_OTHER), its identifier is stored
437 * in <len> and <ptr> is NULL ;
438 * - if the method is unknown (HTTP_METH_OTHER), <ptr> points to the text and
439 * <len> to its length.
440 * This is intended to be used with pat_match_meth() only.
441 */
442static int smp_fetch_meth(const struct arg *args, struct sample *smp, const char *kw, void *private)
443{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200444 struct channel *chn = SMP_REQ_CHN(smp);
Willy Tarreau79e57332018-10-02 16:01:16 +0200445 int meth;
446 struct http_txn *txn;
447
Christopher Faulet46575cd2019-04-17 11:40:30 +0200448 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200449 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +0200450 struct htx *htx = smp_prefetch_htx(smp, chn, 0);
Willy Tarreau79e57332018-10-02 16:01:16 +0200451
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200452 if (!htx)
Willy Tarreau79e57332018-10-02 16:01:16 +0200453 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200454
455 txn = smp->strm->txn;
456 meth = txn->meth;
457 smp->data.type = SMP_T_METH;
458 smp->data.u.meth.meth = meth;
459 if (meth == HTTP_METH_OTHER) {
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100460 struct htx_sl *sl;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200461
Christopher Faulet89dc4992019-04-17 12:02:59 +0200462 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200463 /* ensure the indexes are not affected */
464 return 0;
Christopher Faulet89dc4992019-04-17 12:02:59 +0200465 }
Christopher Faulet297fbb42019-05-13 14:41:27 +0200466 sl = http_get_stline(htx);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200467 smp->flags |= SMP_F_CONST;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100468 smp->data.u.meth.str.area = HTX_SL_REQ_MPTR(sl);
469 smp->data.u.meth.str.data = HTX_SL_REQ_MLEN(sl);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200470 }
471 smp->flags |= SMP_F_VOL_1ST;
472 }
473 else {
474 /* LEGACY version */
Christopher Faulet89dc4992019-04-17 12:02:59 +0200475 CHECK_HTTP_MESSAGE_FIRST_PERM(chn);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200476
477 txn = smp->strm->txn;
478 meth = txn->meth;
479 smp->data.type = SMP_T_METH;
480 smp->data.u.meth.meth = meth;
481 if (meth == HTTP_METH_OTHER) {
Christopher Faulet89dc4992019-04-17 12:02:59 +0200482 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200483 /* ensure the indexes are not affected */
484 return 0;
Christopher Faulet89dc4992019-04-17 12:02:59 +0200485 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200486 smp->flags |= SMP_F_CONST;
487 smp->data.u.meth.str.data = txn->req.sl.rq.m_l;
488 smp->data.u.meth.str.area = ci_head(txn->req.chn);
489 }
490 smp->flags |= SMP_F_VOL_1ST;
Willy Tarreau79e57332018-10-02 16:01:16 +0200491 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200492 return 1;
493}
494
495static int smp_fetch_rqver(const struct arg *args, struct sample *smp, const char *kw, void *private)
496{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200497 struct channel *chn = SMP_REQ_CHN(smp);
Willy Tarreau79e57332018-10-02 16:01:16 +0200498 struct http_txn *txn;
499 char *ptr;
500 int len;
501
Christopher Faulet46575cd2019-04-17 11:40:30 +0200502 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200503 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +0200504 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100505 struct htx_sl *sl;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200506
507 if (!htx)
508 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200509
Christopher Faulet297fbb42019-05-13 14:41:27 +0200510 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100511 len = HTX_SL_REQ_VLEN(sl);
512 ptr = HTX_SL_REQ_VPTR(sl);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200513 }
514 else {
515 /* LEGACY version */
Christopher Faulet89dc4992019-04-17 12:02:59 +0200516 CHECK_HTTP_MESSAGE_FIRST(chn);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200517
518 txn = smp->strm->txn;
519 len = txn->req.sl.rq.v_l;
Christopher Faulet89dc4992019-04-17 12:02:59 +0200520 ptr = ci_head(chn) + txn->req.sl.rq.v;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200521 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200522
523 while ((len-- > 0) && (*ptr++ != '/'));
524 if (len <= 0)
525 return 0;
526
527 smp->data.type = SMP_T_STR;
528 smp->data.u.str.area = ptr;
529 smp->data.u.str.data = len;
530
531 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
532 return 1;
533}
534
535static int smp_fetch_stver(const struct arg *args, struct sample *smp, const char *kw, void *private)
536{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200537 struct channel *chn = SMP_RES_CHN(smp);
Willy Tarreau79e57332018-10-02 16:01:16 +0200538 struct http_txn *txn;
539 char *ptr;
540 int len;
541
Christopher Faulet46575cd2019-04-17 11:40:30 +0200542 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200543 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +0200544 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100545 struct htx_sl *sl;
Willy Tarreau79e57332018-10-02 16:01:16 +0200546
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200547 if (!htx)
548 return 0;
549
Christopher Faulet297fbb42019-05-13 14:41:27 +0200550 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100551 len = HTX_SL_RES_VLEN(sl);
552 ptr = HTX_SL_RES_VPTR(sl);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200553 }
554 else {
555 /* LEGACY version */
Christopher Faulet89dc4992019-04-17 12:02:59 +0200556 CHECK_HTTP_MESSAGE_FIRST(chn);
Willy Tarreau79e57332018-10-02 16:01:16 +0200557
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200558 txn = smp->strm->txn;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200559 len = txn->rsp.sl.st.v_l;
Christopher Faulet89dc4992019-04-17 12:02:59 +0200560 ptr = ci_head(chn);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200561 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200562
563 while ((len-- > 0) && (*ptr++ != '/'));
564 if (len <= 0)
565 return 0;
566
567 smp->data.type = SMP_T_STR;
568 smp->data.u.str.area = ptr;
569 smp->data.u.str.data = len;
570
571 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
572 return 1;
573}
574
575/* 3. Check on Status Code. We manipulate integers here. */
576static int smp_fetch_stcode(const struct arg *args, struct sample *smp, const char *kw, void *private)
577{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200578 struct channel *chn = SMP_RES_CHN(smp);
Willy Tarreau79e57332018-10-02 16:01:16 +0200579 struct http_txn *txn;
580 char *ptr;
581 int len;
582
Christopher Faulet46575cd2019-04-17 11:40:30 +0200583 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200584 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +0200585 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100586 struct htx_sl *sl;
Willy Tarreau79e57332018-10-02 16:01:16 +0200587
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200588 if (!htx)
589 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200590
Christopher Faulet297fbb42019-05-13 14:41:27 +0200591 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100592 len = HTX_SL_RES_CLEN(sl);
593 ptr = HTX_SL_RES_CPTR(sl);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200594 }
595 else {
596 /* LEGACY version */
Christopher Faulet89dc4992019-04-17 12:02:59 +0200597 CHECK_HTTP_MESSAGE_FIRST(chn);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200598
599 txn = smp->strm->txn;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200600 len = txn->rsp.sl.st.c_l;
Christopher Faulet89dc4992019-04-17 12:02:59 +0200601 ptr = ci_head(chn) + txn->rsp.sl.st.c;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200602 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200603
604 smp->data.type = SMP_T_SINT;
605 smp->data.u.sint = __strl2ui(ptr, len);
606 smp->flags = SMP_F_VOL_1ST;
607 return 1;
608}
609
610static int smp_fetch_uniqueid(const struct arg *args, struct sample *smp, const char *kw, void *private)
611{
612 if (LIST_ISEMPTY(&smp->sess->fe->format_unique_id))
613 return 0;
614
615 if (!smp->strm->unique_id) {
616 if ((smp->strm->unique_id = pool_alloc(pool_head_uniqueid)) == NULL)
617 return 0;
618 smp->strm->unique_id[0] = '\0';
619 }
620 smp->data.u.str.data = build_logline(smp->strm, smp->strm->unique_id,
621 UNIQUEID_LEN, &smp->sess->fe->format_unique_id);
622
623 smp->data.type = SMP_T_STR;
624 smp->data.u.str.area = smp->strm->unique_id;
625 smp->flags = SMP_F_CONST;
626 return 1;
627}
628
629/* Returns a string block containing all headers including the
Joseph Herlant942eea32018-11-15 13:57:22 -0800630 * empty line which separes headers from the body. This is useful
631 * for some headers analysis.
Willy Tarreau79e57332018-10-02 16:01:16 +0200632 */
633static int smp_fetch_hdrs(const struct arg *args, struct sample *smp, const char *kw, void *private)
634{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200635 struct channel *chn = SMP_REQ_CHN(smp);
Willy Tarreau79e57332018-10-02 16:01:16 +0200636 struct http_txn *txn;
637
Christopher Faulet46575cd2019-04-17 11:40:30 +0200638 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200639 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +0200640 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200641 struct buffer *temp;
642 int32_t pos;
Willy Tarreau79e57332018-10-02 16:01:16 +0200643
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200644 if (!htx)
645 return 0;
646 temp = get_trash_chunk();
Christopher Fauleta3f15502019-05-13 15:27:23 +0200647 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200648 struct htx_blk *blk = htx_get_blk(htx, pos);
649 enum htx_blk_type type = htx_get_blk_type(blk);
Willy Tarreau79e57332018-10-02 16:01:16 +0200650
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200651 if (type == HTX_BLK_HDR) {
652 struct ist n = htx_get_blk_name(htx, blk);
653 struct ist v = htx_get_blk_value(htx, blk);
654
Christopher Fauletc59ff232018-12-03 13:58:44 +0100655 if (!htx_hdr_to_h1(n, v, temp))
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200656 return 0;
657 }
658 else if (type == HTX_BLK_EOH) {
659 if (!chunk_memcat(temp, "\r\n", 2))
660 return 0;
661 break;
662 }
663 }
664 smp->data.type = SMP_T_STR;
665 smp->data.u.str = *temp;
666
667 }
668 else {
669 /* LEGACY version */
670 struct http_msg *msg;
671 struct hdr_idx *idx;
672
Christopher Faulet89dc4992019-04-17 12:02:59 +0200673 CHECK_HTTP_MESSAGE_FIRST(chn);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200674
675 txn = smp->strm->txn;
676 idx = &txn->hdr_idx;
677 msg = &txn->req;
Willy Tarreau79e57332018-10-02 16:01:16 +0200678
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200679 smp->data.type = SMP_T_STR;
Christopher Faulet89dc4992019-04-17 12:02:59 +0200680 smp->data.u.str.area = ci_head(chn) + hdr_idx_first_pos(idx);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200681 smp->data.u.str.data = msg->eoh - hdr_idx_first_pos(idx) + 1 +
Christopher Faulet89dc4992019-04-17 12:02:59 +0200682 (ci_head(chn)[msg->eoh] == '\r');
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200683 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200684 return 1;
685}
686
687/* Returns the header request in a length/value encoded format.
688 * This is useful for exchanges with the SPOE.
689 *
690 * A "length value" is a multibyte code encoding numbers. It uses the
691 * SPOE format. The encoding is the following:
692 *
693 * Each couple "header name" / "header value" is composed
694 * like this:
695 * "length value" "header name bytes"
696 * "length value" "header value bytes"
697 * When the last header is reached, the header name and the header
698 * value are empty. Their length are 0
699 */
700static int smp_fetch_hdrs_bin(const struct arg *args, struct sample *smp, const char *kw, void *private)
701{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200702 struct channel *chn = SMP_REQ_CHN(smp);
Willy Tarreau79e57332018-10-02 16:01:16 +0200703 struct http_txn *txn;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200704 struct buffer *temp;
Willy Tarreau79e57332018-10-02 16:01:16 +0200705
Christopher Faulet46575cd2019-04-17 11:40:30 +0200706 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200707 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +0200708 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200709 struct buffer *temp;
710 char *p, *end;
711 int32_t pos;
712 int ret;
Willy Tarreau79e57332018-10-02 16:01:16 +0200713
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200714 if (!htx)
715 return 0;
716 temp = get_trash_chunk();
717 p = temp->area;
718 end = temp->area + temp->size;
Christopher Fauleta3f15502019-05-13 15:27:23 +0200719 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200720 struct htx_blk *blk = htx_get_blk(htx, pos);
721 enum htx_blk_type type = htx_get_blk_type(blk);
722 struct ist n, v;
Willy Tarreau79e57332018-10-02 16:01:16 +0200723
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200724 if (type == HTX_BLK_HDR) {
725 n = htx_get_blk_name(htx,blk);
726 v = htx_get_blk_value(htx, blk);
Willy Tarreau79e57332018-10-02 16:01:16 +0200727
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200728 /* encode the header name. */
729 ret = encode_varint(n.len, &p, end);
730 if (ret == -1)
731 return 0;
732 if (p + n.len > end)
733 return 0;
734 memcpy(p, n.ptr, n.len);
735 p += n.len;
Willy Tarreau79e57332018-10-02 16:01:16 +0200736
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200737 /* encode the header value. */
738 ret = encode_varint(v.len, &p, end);
739 if (ret == -1)
740 return 0;
741 if (p + v.len > end)
742 return 0;
743 memcpy(p, v.ptr, v.len);
744 p += v.len;
Willy Tarreau79e57332018-10-02 16:01:16 +0200745
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200746 }
747 else if (type == HTX_BLK_EOH) {
748 /* encode the end of the header list with empty
749 * header name and header value.
750 */
751 ret = encode_varint(0, &p, end);
752 if (ret == -1)
753 return 0;
754 ret = encode_varint(0, &p, end);
755 if (ret == -1)
756 return 0;
757 break;
758 }
759 }
760
761 /* Initialise sample data which will be filled. */
762 smp->data.type = SMP_T_BIN;
763 smp->data.u.str.area = temp->area;
764 smp->data.u.str.data = p - temp->area;
765 smp->data.u.str.size = temp->size;
766 }
767 else {
768 /* LEGACY version */
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200769 struct hdr_idx *idx;
770 const char *cur_ptr, *cur_next, *p;
771 int old_idx, cur_idx;
772 struct hdr_idx_elem *cur_hdr;
773 const char *hn, *hv;
774 int hnl, hvl;
775 int ret;
776 char *buf;
777 char *end;
778
Christopher Faulet89dc4992019-04-17 12:02:59 +0200779 CHECK_HTTP_MESSAGE_FIRST(chn);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200780
781 temp = get_trash_chunk();
782 buf = temp->area;
783 end = temp->area + temp->size;
784
785 txn = smp->strm->txn;
786 idx = &txn->hdr_idx;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200787
788 /* Build array of headers. */
789 old_idx = 0;
Christopher Faulet89dc4992019-04-17 12:02:59 +0200790 cur_next = ci_head(chn) + hdr_idx_first_pos(idx);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200791 while (1) {
792 cur_idx = idx->v[old_idx].next;
793 if (!cur_idx)
794 break;
795 old_idx = cur_idx;
Willy Tarreau79e57332018-10-02 16:01:16 +0200796
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200797 cur_hdr = &idx->v[cur_idx];
798 cur_ptr = cur_next;
799 cur_next = cur_ptr + cur_hdr->len + cur_hdr->cr + 1;
800
801 /* Now we have one full header at cur_ptr of len cur_hdr->len,
802 * and the next header starts at cur_next. We'll check
803 * this header in the list as well as against the default
804 * rule.
805 */
806
807 /* look for ': *'. */
808 hn = cur_ptr;
809 for (p = cur_ptr; p < cur_ptr + cur_hdr->len && *p != ':'; p++);
810 if (p >= cur_ptr+cur_hdr->len)
811 continue;
812 hnl = p - hn;
Willy Tarreau79e57332018-10-02 16:01:16 +0200813 p++;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200814 while (p < cur_ptr + cur_hdr->len && (*p == ' ' || *p == '\t'))
815 p++;
816 if (p >= cur_ptr + cur_hdr->len)
817 continue;
818 hv = p;
819 hvl = cur_ptr + cur_hdr->len-p;
Willy Tarreau79e57332018-10-02 16:01:16 +0200820
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200821 /* encode the header name. */
822 ret = encode_varint(hnl, &buf, end);
823 if (ret == -1)
824 return 0;
825 if (buf + hnl > end)
826 return 0;
827 memcpy(buf, hn, hnl);
828 buf += hnl;
829
830 /* encode and copy the value. */
831 ret = encode_varint(hvl, &buf, end);
832 if (ret == -1)
833 return 0;
834 if (buf + hvl > end)
835 return 0;
836 memcpy(buf, hv, hvl);
837 buf += hvl;
838 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200839
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200840 /* encode the end of the header list with empty
841 * header name and header value.
842 */
843 ret = encode_varint(0, &buf, end);
Willy Tarreau79e57332018-10-02 16:01:16 +0200844 if (ret == -1)
845 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200846 ret = encode_varint(0, &buf, end);
847 if (ret == -1)
Willy Tarreau79e57332018-10-02 16:01:16 +0200848 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200849
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200850 /* Initialise sample data which will be filled. */
851 smp->data.type = SMP_T_BIN;
852 smp->data.u.str.area = temp->area;
853 smp->data.u.str.data = buf - temp->area;
854 smp->data.u.str.size = temp->size;
855 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200856 return 1;
857}
858
859/* returns the longest available part of the body. This requires that the body
860 * has been waited for using http-buffer-request.
861 */
862static int smp_fetch_body(const struct arg *args, struct sample *smp, const char *kw, void *private)
863{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200864 struct channel *chn = SMP_REQ_CHN(smp);
Willy Tarreau79e57332018-10-02 16:01:16 +0200865 struct buffer *temp;
866
Christopher Faulet46575cd2019-04-17 11:40:30 +0200867 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200868 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +0200869 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200870 int32_t pos;
Willy Tarreau79e57332018-10-02 16:01:16 +0200871
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200872 if (!htx)
873 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200874
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200875 temp = get_trash_chunk();
Christopher Fauleta3f15502019-05-13 15:27:23 +0200876 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200877 struct htx_blk *blk = htx_get_blk(htx, pos);
878 enum htx_blk_type type = htx_get_blk_type(blk);
Willy Tarreau79e57332018-10-02 16:01:16 +0200879
Christopher Faulet54b5e212019-06-04 10:08:28 +0200880 if (type == HTX_BLK_EOM || type == HTX_BLK_TLR || type == HTX_BLK_EOT)
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200881 break;
882 if (type == HTX_BLK_DATA) {
Christopher Fauletc59ff232018-12-03 13:58:44 +0100883 if (!htx_data_to_h1(htx_get_blk_value(htx, blk), temp, 0))
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200884 return 0;
885 }
886 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200887
Willy Tarreau79e57332018-10-02 16:01:16 +0200888 smp->data.type = SMP_T_BIN;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200889 smp->data.u.str = *temp;
890 smp->flags = SMP_F_VOL_TEST;
Willy Tarreau79e57332018-10-02 16:01:16 +0200891 }
892 else {
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200893 /* LEGACY version */
894 struct http_msg *msg;
895 unsigned long len;
896 unsigned long block1;
897 char *body;
898
Christopher Faulet89dc4992019-04-17 12:02:59 +0200899 CHECK_HTTP_MESSAGE_FIRST(chn);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200900
Christopher Faulet89dc4992019-04-17 12:02:59 +0200901 msg = &smp->strm->txn->req;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200902 len = http_body_bytes(msg);
Christopher Faulet89dc4992019-04-17 12:02:59 +0200903 body = c_ptr(chn, -http_data_rewind(msg));
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200904
905 block1 = len;
Christopher Faulet89dc4992019-04-17 12:02:59 +0200906 if (block1 > b_wrap(&chn->buf) - body)
907 block1 = b_wrap(&chn->buf) - body;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200908
909 if (block1 == len) {
910 /* buffer is not wrapped (or empty) */
911 smp->data.type = SMP_T_BIN;
912 smp->data.u.str.area = body;
913 smp->data.u.str.data = len;
914 smp->flags = SMP_F_VOL_TEST | SMP_F_CONST;
915 }
916 else {
917 /* buffer is wrapped, we need to defragment it */
918 temp = get_trash_chunk();
919 memcpy(temp->area, body, block1);
Christopher Faulet89dc4992019-04-17 12:02:59 +0200920 memcpy(temp->area + block1, b_orig(&chn->buf), len - block1);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200921 smp->data.type = SMP_T_BIN;
922 smp->data.u.str.area = temp->area;
923 smp->data.u.str.data = len;
924 smp->flags = SMP_F_VOL_TEST;
925 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200926 }
927 return 1;
928}
929
930
931/* returns the available length of the body. This requires that the body
932 * has been waited for using http-buffer-request.
933 */
934static int smp_fetch_body_len(const struct arg *args, struct sample *smp, const char *kw, void *private)
935{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200936 struct channel *chn = SMP_REQ_CHN(smp);
937
Christopher Faulet46575cd2019-04-17 11:40:30 +0200938 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200939 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +0200940 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Dragan Dosen5a606682019-02-14 12:30:53 +0100941 int32_t pos;
Christopher Fauletc16317d2018-12-12 14:11:22 +0100942 unsigned long long len = 0;
943
944 if (!htx)
945 return 0;
946
Christopher Fauleta3f15502019-05-13 15:27:23 +0200947 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Dragan Dosen5a606682019-02-14 12:30:53 +0100948 struct htx_blk *blk = htx_get_blk(htx, pos);
949 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Fauletc16317d2018-12-12 14:11:22 +0100950
Christopher Faulet54b5e212019-06-04 10:08:28 +0200951 if (type == HTX_BLK_EOM || type == HTX_BLK_TLR || type == HTX_BLK_EOT)
Christopher Fauletc16317d2018-12-12 14:11:22 +0100952 break;
Dragan Dosen5a606682019-02-14 12:30:53 +0100953 if (type == HTX_BLK_DATA)
954 len += htx_get_blksz(blk);
Christopher Fauletc16317d2018-12-12 14:11:22 +0100955 }
956
957 smp->data.type = SMP_T_SINT;
958 smp->data.u.sint = len;
959
960 smp->flags = SMP_F_VOL_TEST;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200961 }
962 else {
963 /* LEGACY version */
964 struct http_msg *msg;
Willy Tarreau79e57332018-10-02 16:01:16 +0200965
Christopher Faulet89dc4992019-04-17 12:02:59 +0200966 CHECK_HTTP_MESSAGE_FIRST(chn);
Willy Tarreau79e57332018-10-02 16:01:16 +0200967
Christopher Faulet89dc4992019-04-17 12:02:59 +0200968 msg = &smp->strm->txn->req;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200969 smp->data.type = SMP_T_SINT;
970 smp->data.u.sint = http_body_bytes(msg);
Willy Tarreau79e57332018-10-02 16:01:16 +0200971
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200972 smp->flags = SMP_F_VOL_TEST;
973 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200974 return 1;
975}
976
977
978/* returns the advertised length of the body, or the advertised size of the
979 * chunks available in the buffer. This requires that the body has been waited
980 * for using http-buffer-request.
981 */
982static int smp_fetch_body_size(const struct arg *args, struct sample *smp, const char *kw, void *private)
983{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200984 struct channel *chn = SMP_REQ_CHN(smp);
985
Christopher Faulet46575cd2019-04-17 11:40:30 +0200986 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200987 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +0200988 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Dragan Dosen5a606682019-02-14 12:30:53 +0100989 int32_t pos;
Christopher Fauletc16317d2018-12-12 14:11:22 +0100990 unsigned long long len = 0;
991
992 if (!htx)
993 return 0;
994
Christopher Fauleta3f15502019-05-13 15:27:23 +0200995 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Dragan Dosen5a606682019-02-14 12:30:53 +0100996 struct htx_blk *blk = htx_get_blk(htx, pos);
997 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Fauletc16317d2018-12-12 14:11:22 +0100998
Christopher Faulet54b5e212019-06-04 10:08:28 +0200999 if (type == HTX_BLK_EOM || type == HTX_BLK_TLR || type == HTX_BLK_EOT)
Christopher Fauletc16317d2018-12-12 14:11:22 +01001000 break;
Dragan Dosen5a606682019-02-14 12:30:53 +01001001 if (type == HTX_BLK_DATA)
1002 len += htx_get_blksz(blk);
Christopher Fauletc16317d2018-12-12 14:11:22 +01001003 }
1004 if (htx->extra != ULLONG_MAX)
1005 len += htx->extra;
1006
1007 smp->data.type = SMP_T_SINT;
1008 smp->data.u.sint = len;
1009
1010 smp->flags = SMP_F_VOL_TEST;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001011 }
1012 else {
1013 /* LEGACY version */
1014 struct http_msg *msg;
Willy Tarreau79e57332018-10-02 16:01:16 +02001015
Christopher Faulet89dc4992019-04-17 12:02:59 +02001016 CHECK_HTTP_MESSAGE_FIRST(chn);
Willy Tarreau79e57332018-10-02 16:01:16 +02001017
Christopher Faulet89dc4992019-04-17 12:02:59 +02001018 msg = &smp->strm->txn->req;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001019 smp->data.type = SMP_T_SINT;
1020 smp->data.u.sint = msg->body_len;
Willy Tarreau79e57332018-10-02 16:01:16 +02001021
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001022 smp->flags = SMP_F_VOL_TEST;
1023 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001024 return 1;
1025}
1026
1027
1028/* 4. Check on URL/URI. A pointer to the URI is stored. */
1029static int smp_fetch_url(const struct arg *args, struct sample *smp, const char *kw, void *private)
1030{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001031 struct channel *chn = SMP_REQ_CHN(smp);
Willy Tarreau79e57332018-10-02 16:01:16 +02001032 struct http_txn *txn;
1033
Christopher Faulet46575cd2019-04-17 11:40:30 +02001034 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001035 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +02001036 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001037 struct htx_sl *sl;
Willy Tarreau79e57332018-10-02 16:01:16 +02001038
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001039 if (!htx)
1040 return 0;
Christopher Faulet297fbb42019-05-13 14:41:27 +02001041 sl = http_get_stline(htx);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001042 smp->data.type = SMP_T_STR;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001043 smp->data.u.str.area = HTX_SL_REQ_UPTR(sl);
1044 smp->data.u.str.data = HTX_SL_REQ_ULEN(sl);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001045 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
1046 }
1047 else {
1048 /* LEGACY version */
Christopher Faulet89dc4992019-04-17 12:02:59 +02001049 CHECK_HTTP_MESSAGE_FIRST(chn);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001050
1051 txn = smp->strm->txn;
1052 smp->data.type = SMP_T_STR;
1053 smp->data.u.str.data = txn->req.sl.rq.u_l;
Christopher Faulet89dc4992019-04-17 12:02:59 +02001054 smp->data.u.str.area = ci_head(chn) + txn->req.sl.rq.u;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001055 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
1056 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001057 return 1;
1058}
1059
1060static int smp_fetch_url_ip(const struct arg *args, struct sample *smp, const char *kw, void *private)
1061{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001062 struct channel *chn = SMP_REQ_CHN(smp);
Willy Tarreau79e57332018-10-02 16:01:16 +02001063 struct http_txn *txn;
1064 struct sockaddr_storage addr;
1065
Christopher Faulet46575cd2019-04-17 11:40:30 +02001066 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001067 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +02001068 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001069 struct htx_sl *sl;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001070
1071 if (!htx)
1072 return 0;
Christopher Faulet297fbb42019-05-13 14:41:27 +02001073 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001074 url2sa(HTX_SL_REQ_UPTR(sl), HTX_SL_REQ_ULEN(sl), &addr, NULL);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001075 }
1076 else {
1077 /* LEGACY version */
Christopher Faulet89dc4992019-04-17 12:02:59 +02001078 CHECK_HTTP_MESSAGE_FIRST(chn);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001079
1080 txn = smp->strm->txn;
Christopher Faulet89dc4992019-04-17 12:02:59 +02001081 url2sa(ci_head(chn) + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &addr, NULL);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001082 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001083
Willy Tarreau79e57332018-10-02 16:01:16 +02001084 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
1085 return 0;
1086
1087 smp->data.type = SMP_T_IPV4;
1088 smp->data.u.ipv4 = ((struct sockaddr_in *)&addr)->sin_addr;
1089 smp->flags = 0;
1090 return 1;
1091}
1092
1093static int smp_fetch_url_port(const struct arg *args, struct sample *smp, const char *kw, void *private)
1094{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001095 struct channel *chn = SMP_REQ_CHN(smp);
Willy Tarreau79e57332018-10-02 16:01:16 +02001096 struct http_txn *txn;
1097 struct sockaddr_storage addr;
1098
Christopher Faulet46575cd2019-04-17 11:40:30 +02001099 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001100 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +02001101 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001102 struct htx_sl *sl;
Willy Tarreau79e57332018-10-02 16:01:16 +02001103
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001104 if (!htx)
1105 return 0;
Christopher Faulet297fbb42019-05-13 14:41:27 +02001106 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001107 url2sa(HTX_SL_REQ_UPTR(sl), HTX_SL_REQ_ULEN(sl), &addr, NULL);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001108 }
1109 else {
1110 /* LEGACY version */
Christopher Faulet89dc4992019-04-17 12:02:59 +02001111 CHECK_HTTP_MESSAGE_FIRST(chn);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001112
1113 txn = smp->strm->txn;
Christopher Faulet89dc4992019-04-17 12:02:59 +02001114 url2sa(ci_head(chn) + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &addr, NULL);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001115 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001116 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
1117 return 0;
1118
1119 smp->data.type = SMP_T_SINT;
1120 smp->data.u.sint = ntohs(((struct sockaddr_in *)&addr)->sin_port);
1121 smp->flags = 0;
1122 return 1;
1123}
1124
1125/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
1126 * Accepts an optional argument of type string containing the header field name,
1127 * and an optional argument of type signed or unsigned integer to request an
1128 * explicit occurrence of the header. Note that in the event of a missing name,
1129 * headers are considered from the first one. It does not stop on commas and
1130 * returns full lines instead (useful for User-Agent or Date for example).
1131 */
1132static int smp_fetch_fhdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
1133{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001134 /* possible keywords: req.fhdr, res.fhdr */
1135 struct channel *chn = ((kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Willy Tarreau79e57332018-10-02 16:01:16 +02001136 int occ = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001137
Christopher Faulet46575cd2019-04-17 11:40:30 +02001138 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001139 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +02001140 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001141 struct http_hdr_ctx *ctx = smp->ctx.a[0];
1142 struct ist name;
Willy Tarreau79e57332018-10-02 16:01:16 +02001143
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001144 if (!ctx) {
1145 /* first call */
1146 ctx = &static_http_hdr_ctx;
1147 ctx->blk = NULL;
1148 smp->ctx.a[0] = ctx;
1149 }
1150
1151 if (args) {
1152 if (args[0].type != ARGT_STR)
1153 return 0;
1154 name.ptr = args[0].data.str.area;
1155 name.len = args[0].data.str.data;
1156
1157 if (args[1].type == ARGT_SINT)
1158 occ = args[1].data.sint;
1159 }
1160
1161 if (!htx)
Willy Tarreau79e57332018-10-02 16:01:16 +02001162 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001163
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001164 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
1165 /* search for header from the beginning */
1166 ctx->blk = NULL;
1167
1168 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
1169 /* no explicit occurrence and single fetch => last header by default */
1170 occ = -1;
1171
1172 if (!occ)
1173 /* prepare to report multiple occurrences for ACL fetches */
1174 smp->flags |= SMP_F_NOT_LAST;
1175
1176 smp->data.type = SMP_T_STR;
1177 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
1178 if (http_get_htx_fhdr(htx, name, occ, ctx, &smp->data.u.str.area, &smp->data.u.str.data))
1179 return 1;
Willy Tarreau79e57332018-10-02 16:01:16 +02001180 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001181 else {
1182 /* LEGACY version */
1183 struct hdr_idx *idx;
1184 struct hdr_ctx *ctx = smp->ctx.a[0];
1185 const struct http_msg *msg;
1186 const char *name_str = NULL;
1187 int name_len = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001188
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001189 if (!ctx) {
1190 /* first call */
1191 ctx = &static_hdr_ctx;
1192 ctx->idx = 0;
1193 smp->ctx.a[0] = ctx;
1194 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001195
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001196 if (args) {
1197 if (args[0].type != ARGT_STR)
1198 return 0;
1199 name_str = args[0].data.str.area;
1200 name_len = args[0].data.str.data;
Willy Tarreau79e57332018-10-02 16:01:16 +02001201
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001202 if (args[1].type == ARGT_SINT)
1203 occ = args[1].data.sint;
1204 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001205
Christopher Faulet89dc4992019-04-17 12:02:59 +02001206 CHECK_HTTP_MESSAGE_FIRST(chn);
Willy Tarreau79e57332018-10-02 16:01:16 +02001207
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001208 idx = &smp->strm->txn->hdr_idx;
Christopher Faulet89dc4992019-04-17 12:02:59 +02001209 msg = (!(chn->flags & CF_ISRESP) ? &smp->strm->txn->req : &smp->strm->txn->rsp);
Willy Tarreau79e57332018-10-02 16:01:16 +02001210
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001211 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
1212 /* search for header from the beginning */
1213 ctx->idx = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001214
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001215 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
1216 /* no explicit occurrence and single fetch => last header by default */
1217 occ = -1;
1218
1219 if (!occ)
1220 /* prepare to report multiple occurrences for ACL fetches */
1221 smp->flags |= SMP_F_NOT_LAST;
1222
1223 smp->data.type = SMP_T_STR;
1224 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
1225 if (http_get_fhdr(msg, name_str, name_len, idx, occ, ctx, &smp->data.u.str.area, &smp->data.u.str.data))
1226 return 1;
1227 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001228 smp->flags &= ~SMP_F_NOT_LAST;
1229 return 0;
1230}
1231
1232/* 6. Check on HTTP header count. The number of occurrences is returned.
1233 * Accepts exactly 1 argument of type string. It does not stop on commas and
1234 * returns full lines instead (useful for User-Agent or Date for example).
1235 */
1236static int smp_fetch_fhdr_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
1237{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001238 /* possible keywords: req.fhdr_cnt, res.fhdr_cnt */
1239 struct channel *chn = ((kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Willy Tarreau79e57332018-10-02 16:01:16 +02001240 int cnt;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001241
Christopher Faulet46575cd2019-04-17 11:40:30 +02001242 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001243 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +02001244 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001245 struct http_hdr_ctx ctx;
1246 struct ist name;
1247
1248 if (!htx)
1249 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001250
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001251 if (args && args->type == ARGT_STR) {
1252 name.ptr = args->data.str.area;
1253 name.len = args->data.str.data;
Olivier Houcharde2c78cd2018-11-21 13:49:48 +01001254 } else {
1255 name.ptr = NULL;
1256 name.len = 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001257 }
1258
1259 ctx.blk = NULL;
1260 cnt = 0;
1261 while (http_find_header(htx, name, &ctx, 1))
1262 cnt++;
Willy Tarreau79e57332018-10-02 16:01:16 +02001263 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001264 else {
1265 /* LEGACY version */
1266 struct hdr_idx *idx;
1267 struct hdr_ctx ctx;
1268 const struct http_msg *msg;
1269 const char *name = NULL;
1270 int len = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001271
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001272 if (args && args->type == ARGT_STR) {
1273 name = args->data.str.area;
1274 len = args->data.str.data;
1275 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001276
Christopher Faulet89dc4992019-04-17 12:02:59 +02001277 CHECK_HTTP_MESSAGE_FIRST(chn);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001278
1279 idx = &smp->strm->txn->hdr_idx;
Christopher Faulet89dc4992019-04-17 12:02:59 +02001280 msg = (!(chn->flags & CF_ISRESP) ? &smp->strm->txn->req : &smp->strm->txn->rsp);
Willy Tarreau79e57332018-10-02 16:01:16 +02001281
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001282 ctx.idx = 0;
1283 cnt = 0;
1284 while (http_find_full_header2(name, len, ci_head(msg->chn), idx, &ctx))
1285 cnt++;
1286 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001287
1288 smp->data.type = SMP_T_SINT;
1289 smp->data.u.sint = cnt;
1290 smp->flags = SMP_F_VOL_HDR;
1291 return 1;
1292}
1293
1294static int smp_fetch_hdr_names(const struct arg *args, struct sample *smp, const char *kw, void *private)
1295{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001296 /* possible keywords: req.hdr_names, res.hdr_names */
1297 struct channel *chn = ((kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Willy Tarreau79e57332018-10-02 16:01:16 +02001298 struct buffer *temp;
1299 char del = ',';
1300
Christopher Faulet46575cd2019-04-17 11:40:30 +02001301 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001302 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +02001303 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001304 int32_t pos;
Willy Tarreau79e57332018-10-02 16:01:16 +02001305
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001306 if (!htx)
1307 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001308
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001309 if (args && args->type == ARGT_STR)
1310 del = *args[0].data.str.area;
Willy Tarreau79e57332018-10-02 16:01:16 +02001311
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001312 temp = get_trash_chunk();
Christopher Fauleta3f15502019-05-13 15:27:23 +02001313 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001314 struct htx_blk *blk = htx_get_blk(htx, pos);
1315 enum htx_blk_type type = htx_get_blk_type(blk);
1316 struct ist n;
Willy Tarreau79e57332018-10-02 16:01:16 +02001317
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001318 if (type == HTX_BLK_EOH)
1319 break;
1320 if (type != HTX_BLK_HDR)
1321 continue;
1322 n = htx_get_blk_name(htx, blk);
1323
1324 if (temp->data)
1325 temp->area[temp->data++] = del;
1326 chunk_memcat(temp, n.ptr, n.len);
1327 }
1328 }
1329 else {
1330 /* LEGACY version */
1331 struct hdr_idx *idx;
1332 struct hdr_ctx ctx;
1333 const struct http_msg *msg;
1334
1335 if (args && args->type == ARGT_STR)
1336 del = *args[0].data.str.area;
1337
Christopher Faulet89dc4992019-04-17 12:02:59 +02001338 CHECK_HTTP_MESSAGE_FIRST(chn);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001339
1340 idx = &smp->strm->txn->hdr_idx;
Christopher Faulet89dc4992019-04-17 12:02:59 +02001341 msg = (!(chn->flags & CF_ISRESP) ? &smp->strm->txn->req : &smp->strm->txn->rsp);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001342
1343 temp = get_trash_chunk();
1344
1345 ctx.idx = 0;
1346 while (http_find_next_header(ci_head(msg->chn), idx, &ctx)) {
1347 if (temp->data)
1348 temp->area[temp->data++] = del;
1349 memcpy(temp->area + temp->data, ctx.line, ctx.del);
1350 temp->data += ctx.del;
1351 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001352 }
1353
1354 smp->data.type = SMP_T_STR;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001355 smp->data.u.str = *temp;
Willy Tarreau79e57332018-10-02 16:01:16 +02001356 smp->flags = SMP_F_VOL_HDR;
1357 return 1;
1358}
1359
1360/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
1361 * Accepts an optional argument of type string containing the header field name,
1362 * and an optional argument of type signed or unsigned integer to request an
1363 * explicit occurrence of the header. Note that in the event of a missing name,
1364 * headers are considered from the first one.
1365 */
1366static int smp_fetch_hdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
1367{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001368 /* possible keywords: req.hdr / hdr, res.hdr / shdr */
1369 struct channel *chn = ((kw[0] == 'h' || kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Willy Tarreau79e57332018-10-02 16:01:16 +02001370 int occ = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001371
Christopher Faulet46575cd2019-04-17 11:40:30 +02001372 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001373 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +02001374 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001375 struct http_hdr_ctx *ctx = smp->ctx.a[0];
1376 struct ist name;
1377
1378 if (!ctx) {
1379 /* first call */
1380 ctx = &static_http_hdr_ctx;
1381 ctx->blk = NULL;
1382 smp->ctx.a[0] = ctx;
1383 }
1384
1385 if (args) {
1386 if (args[0].type != ARGT_STR)
1387 return 0;
1388 name.ptr = args[0].data.str.area;
1389 name.len = args[0].data.str.data;
1390
1391 if (args[1].type == ARGT_SINT)
1392 occ = args[1].data.sint;
1393 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001394
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001395 if (!htx)
Willy Tarreau79e57332018-10-02 16:01:16 +02001396 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001397
1398 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
1399 /* search for header from the beginning */
1400 ctx->blk = NULL;
1401
1402 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
1403 /* no explicit occurrence and single fetch => last header by default */
1404 occ = -1;
1405
1406 if (!occ)
1407 /* prepare to report multiple occurrences for ACL fetches */
1408 smp->flags |= SMP_F_NOT_LAST;
Willy Tarreau79e57332018-10-02 16:01:16 +02001409
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001410 smp->data.type = SMP_T_STR;
1411 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
1412 if (http_get_htx_hdr(htx, name, occ, ctx, &smp->data.u.str.area, &smp->data.u.str.data))
1413 return 1;
Willy Tarreau79e57332018-10-02 16:01:16 +02001414 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001415 else {
1416 /* LEGACY version */
1417 struct hdr_idx *idx;
1418 struct hdr_ctx *ctx = smp->ctx.a[0];
1419 const struct http_msg *msg;
1420 const char *name_str = NULL;
1421 int name_len = 0;
1422
1423 if (!ctx) {
1424 /* first call */
1425 ctx = &static_hdr_ctx;
1426 ctx->idx = 0;
1427 smp->ctx.a[0] = ctx;
1428 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001429
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001430 if (args) {
1431 if (args[0].type != ARGT_STR)
1432 return 0;
1433 name_str = args[0].data.str.area;
1434 name_len = args[0].data.str.data;
Willy Tarreau79e57332018-10-02 16:01:16 +02001435
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001436 if (args[1].type == ARGT_SINT)
1437 occ = args[1].data.sint;
1438 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001439
Christopher Faulet89dc4992019-04-17 12:02:59 +02001440 CHECK_HTTP_MESSAGE_FIRST(chn);
Willy Tarreau79e57332018-10-02 16:01:16 +02001441
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001442 idx = &smp->strm->txn->hdr_idx;
Christopher Faulet89dc4992019-04-17 12:02:59 +02001443 msg = (!(chn->flags & CF_ISRESP) ? &smp->strm->txn->req : &smp->strm->txn->rsp);
Willy Tarreau79e57332018-10-02 16:01:16 +02001444
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001445 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
1446 /* search for header from the beginning */
1447 ctx->idx = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001448
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001449 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
1450 /* no explicit occurrence and single fetch => last header by default */
1451 occ = -1;
1452
1453 if (!occ)
1454 /* prepare to report multiple occurrences for ACL fetches */
1455 smp->flags |= SMP_F_NOT_LAST;
1456
1457 smp->data.type = SMP_T_STR;
1458 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
1459 if (http_get_hdr(msg, name_str, name_len, idx, occ, ctx, &smp->data.u.str.area, &smp->data.u.str.data))
1460 return 1;
1461 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001462
1463 smp->flags &= ~SMP_F_NOT_LAST;
1464 return 0;
1465}
1466
Christopher Fauletc1f40dd2019-05-16 10:07:30 +02001467/* Same than smp_fetch_hdr() but only relies on the sample direction to choose
1468 * the right channel. So instead of duplicating the code, we just change the
1469 * keyword and then fallback on smp_fetch_hdr().
1470 */
1471static int smp_fetch_chn_hdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
1472{
1473 kw = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ ? "req.hdr" : "res.hdr");
1474 return smp_fetch_hdr(args, smp, kw, private);
1475}
1476
Willy Tarreau79e57332018-10-02 16:01:16 +02001477/* 6. Check on HTTP header count. The number of occurrences is returned.
1478 * Accepts exactly 1 argument of type string.
1479 */
1480static int smp_fetch_hdr_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
1481{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001482 /* possible keywords: req.hdr_cnt / hdr_cnt, res.hdr_cnt / shdr_cnt */
1483 struct channel *chn = ((kw[0] == 'h' || kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Willy Tarreau79e57332018-10-02 16:01:16 +02001484 int cnt;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001485
Christopher Faulet46575cd2019-04-17 11:40:30 +02001486 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001487 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +02001488 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001489 struct http_hdr_ctx ctx;
1490 struct ist name;
1491
1492 if (!htx)
1493 return 0;
1494
1495 if (args && args->type == ARGT_STR) {
1496 name.ptr = args->data.str.area;
1497 name.len = args->data.str.data;
Olivier Houcharde2c78cd2018-11-21 13:49:48 +01001498 } else {
1499 name.ptr = NULL;
1500 name.len = 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001501 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001502
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001503 ctx.blk = NULL;
1504 cnt = 0;
1505 while (http_find_header(htx, name, &ctx, 0))
1506 cnt++;
Willy Tarreau79e57332018-10-02 16:01:16 +02001507 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001508 else {
1509 /* LEGACY version */
1510 struct hdr_idx *idx;
1511 struct hdr_ctx ctx;
1512 const struct http_msg *msg;
1513 const char *name = NULL;
1514 int len = 0;
1515
1516 if (args && args->type == ARGT_STR) {
1517 name = args->data.str.area;
1518 len = args->data.str.data;
1519 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001520
Christopher Faulet89dc4992019-04-17 12:02:59 +02001521 CHECK_HTTP_MESSAGE_FIRST(chn);
Willy Tarreau79e57332018-10-02 16:01:16 +02001522
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001523 idx = &smp->strm->txn->hdr_idx;
Christopher Faulet89dc4992019-04-17 12:02:59 +02001524 msg = (!(chn->flags & CF_ISRESP) ? &smp->strm->txn->req : &smp->strm->txn->rsp);
Willy Tarreau79e57332018-10-02 16:01:16 +02001525
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001526 ctx.idx = 0;
1527 cnt = 0;
1528 while (http_find_header2(name, len, ci_head(msg->chn), idx, &ctx))
1529 cnt++;
1530 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001531
1532 smp->data.type = SMP_T_SINT;
1533 smp->data.u.sint = cnt;
1534 smp->flags = SMP_F_VOL_HDR;
1535 return 1;
1536}
1537
1538/* Fetch an HTTP header's integer value. The integer value is returned. It
1539 * takes a mandatory argument of type string and an optional one of type int
1540 * to designate a specific occurrence. It returns an unsigned integer, which
1541 * may or may not be appropriate for everything.
1542 */
1543static int smp_fetch_hdr_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
1544{
1545 int ret = smp_fetch_hdr(args, smp, kw, private);
1546
1547 if (ret > 0) {
1548 smp->data.type = SMP_T_SINT;
1549 smp->data.u.sint = strl2ic(smp->data.u.str.area,
1550 smp->data.u.str.data);
1551 }
1552
1553 return ret;
1554}
1555
1556/* Fetch an HTTP header's IP value. takes a mandatory argument of type string
1557 * and an optional one of type int to designate a specific occurrence.
1558 * It returns an IPv4 or IPv6 address.
1559 */
1560static int smp_fetch_hdr_ip(const struct arg *args, struct sample *smp, const char *kw, void *private)
1561{
1562 int ret;
1563
1564 while ((ret = smp_fetch_hdr(args, smp, kw, private)) > 0) {
1565 if (url2ipv4((char *) smp->data.u.str.area, &smp->data.u.ipv4)) {
1566 smp->data.type = SMP_T_IPV4;
1567 break;
1568 } else {
1569 struct buffer *temp = get_trash_chunk();
1570 if (smp->data.u.str.data < temp->size - 1) {
1571 memcpy(temp->area, smp->data.u.str.area,
1572 smp->data.u.str.data);
1573 temp->area[smp->data.u.str.data] = '\0';
1574 if (inet_pton(AF_INET6, temp->area, &smp->data.u.ipv6)) {
1575 smp->data.type = SMP_T_IPV6;
1576 break;
1577 }
1578 }
1579 }
1580
1581 /* if the header doesn't match an IP address, fetch next one */
1582 if (!(smp->flags & SMP_F_NOT_LAST))
1583 return 0;
1584 }
1585 return ret;
1586}
1587
1588/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
1589 * the first '/' after the possible hostname, and ends before the possible '?'.
1590 */
1591static int smp_fetch_path(const struct arg *args, struct sample *smp, const char *kw, void *private)
1592{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001593 struct channel *chn = SMP_REQ_CHN(smp);
1594
Christopher Faulet46575cd2019-04-17 11:40:30 +02001595 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001596 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +02001597 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001598 struct htx_sl *sl;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001599 struct ist path;
1600 size_t len;
Willy Tarreau79e57332018-10-02 16:01:16 +02001601
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001602 if (!htx)
1603 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001604
Christopher Faulet297fbb42019-05-13 14:41:27 +02001605 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001606 path = http_get_path(htx_sl_req_uri(sl));
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001607 if (!path.ptr)
1608 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001609
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001610 for (len = 0; len < path.len && *(path.ptr + len) != '?'; len++)
Dragan Dosen8861e1c2019-02-12 19:50:31 +01001611 ;
Willy Tarreau79e57332018-10-02 16:01:16 +02001612
Dragan Dosen8861e1c2019-02-12 19:50:31 +01001613 /* OK, we got the '/' ! */
1614 smp->data.type = SMP_T_STR;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001615 smp->data.u.str.area = path.ptr;
1616 smp->data.u.str.data = len;
1617 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
1618 }
1619 else {
1620 struct http_txn *txn;
1621 char *ptr, *end;
Willy Tarreau79e57332018-10-02 16:01:16 +02001622
Christopher Faulet89dc4992019-04-17 12:02:59 +02001623 CHECK_HTTP_MESSAGE_FIRST(chn);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001624
1625 txn = smp->strm->txn;
Christopher Faulet89dc4992019-04-17 12:02:59 +02001626 end = ci_head(chn) + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001627 ptr = http_txn_get_path(txn);
1628 if (!ptr)
1629 return 0;
1630
1631 /* OK, we got the '/' ! */
1632 smp->data.type = SMP_T_STR;
1633 smp->data.u.str.area = ptr;
1634
1635 while (ptr < end && *ptr != '?')
1636 ptr++;
1637
1638 smp->data.u.str.data = ptr - smp->data.u.str.area;
1639 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
1640 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001641 return 1;
1642}
1643
1644/* This produces a concatenation of the first occurrence of the Host header
1645 * followed by the path component if it begins with a slash ('/'). This means
1646 * that '*' will not be added, resulting in exactly the first Host entry.
1647 * If no Host header is found, then the path is returned as-is. The returned
1648 * value is stored in the trash so it does not need to be marked constant.
1649 * The returned sample is of type string.
1650 */
1651static int smp_fetch_base(const struct arg *args, struct sample *smp, const char *kw, void *private)
1652{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001653 struct channel *chn = SMP_REQ_CHN(smp);
Willy Tarreau79e57332018-10-02 16:01:16 +02001654 struct buffer *temp;
1655
Christopher Faulet46575cd2019-04-17 11:40:30 +02001656 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001657 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +02001658 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001659 struct htx_sl *sl;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001660 struct http_hdr_ctx ctx;
1661 struct ist path;
Willy Tarreau79e57332018-10-02 16:01:16 +02001662
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001663 if (!htx)
1664 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001665
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001666 ctx.blk = NULL;
1667 if (!http_find_header(htx, ist("Host"), &ctx, 0) || !ctx.value.len)
1668 return smp_fetch_path(args, smp, kw, private);
Willy Tarreau79e57332018-10-02 16:01:16 +02001669
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001670 /* OK we have the header value in ctx.value */
1671 temp = get_trash_chunk();
1672 chunk_memcat(temp, ctx.value.ptr, ctx.value.len);
1673
1674 /* now retrieve the path */
Christopher Faulet297fbb42019-05-13 14:41:27 +02001675 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001676 path = http_get_path(htx_sl_req_uri(sl));
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001677 if (path.ptr) {
1678 size_t len;
1679
Dragan Dosen8861e1c2019-02-12 19:50:31 +01001680 for (len = 0; len < path.len && *(path.ptr + len) != '?'; len++)
1681 ;
1682
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001683 if (len && *(path.ptr) == '/')
1684 chunk_memcat(temp, path.ptr, len);
1685 }
1686
1687 smp->data.type = SMP_T_STR;
1688 smp->data.u.str = *temp;
1689 }
1690 else {
1691 /* LEGACY version */
1692 struct http_txn *txn;
1693 char *ptr, *end, *beg;
1694 struct hdr_ctx ctx;
1695
Christopher Faulet89dc4992019-04-17 12:02:59 +02001696 CHECK_HTTP_MESSAGE_FIRST(chn);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001697
1698 txn = smp->strm->txn;
1699 ctx.idx = 0;
Christopher Faulet89dc4992019-04-17 12:02:59 +02001700 if (!http_find_header2("Host", 4, ci_head(chn), &txn->hdr_idx, &ctx) || !ctx.vlen)
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001701 return smp_fetch_path(args, smp, kw, private);
1702
1703 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
1704 temp = get_trash_chunk();
1705 memcpy(temp->area, ctx.line + ctx.val, ctx.vlen);
1706 smp->data.type = SMP_T_STR;
1707 smp->data.u.str.area = temp->area;
1708 smp->data.u.str.data = ctx.vlen;
Willy Tarreau79e57332018-10-02 16:01:16 +02001709
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001710 /* now retrieve the path */
Christopher Faulet89dc4992019-04-17 12:02:59 +02001711 end = ci_head(chn) + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001712 beg = http_txn_get_path(txn);
1713 if (!beg)
1714 beg = end;
Willy Tarreau79e57332018-10-02 16:01:16 +02001715
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001716 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
1717
1718 if (beg < ptr && *beg == '/') {
1719 memcpy(smp->data.u.str.area + smp->data.u.str.data, beg,
1720 ptr - beg);
1721 smp->data.u.str.data += ptr - beg;
1722 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001723 }
1724
1725 smp->flags = SMP_F_VOL_1ST;
1726 return 1;
1727}
1728
1729/* This produces a 32-bit hash of the concatenation of the first occurrence of
1730 * the Host header followed by the path component if it begins with a slash ('/').
1731 * This means that '*' will not be added, resulting in exactly the first Host
1732 * entry. If no Host header is found, then the path is used. The resulting value
1733 * is hashed using the path hash followed by a full avalanche hash and provides a
1734 * 32-bit integer value. This fetch is useful for tracking per-path activity on
1735 * high-traffic sites without having to store whole paths.
1736 */
1737static int smp_fetch_base32(const struct arg *args, struct sample *smp, const char *kw, void *private)
1738{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001739 struct channel *chn = SMP_REQ_CHN(smp);
Willy Tarreau79e57332018-10-02 16:01:16 +02001740 unsigned int hash = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001741
Christopher Faulet46575cd2019-04-17 11:40:30 +02001742 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001743 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +02001744 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001745 struct htx_sl *sl;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001746 struct http_hdr_ctx ctx;
1747 struct ist path;
Willy Tarreau79e57332018-10-02 16:01:16 +02001748
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001749 if (!htx)
1750 return 0;
1751
1752 ctx.blk = NULL;
Dragan Dosen8861e1c2019-02-12 19:50:31 +01001753 if (http_find_header(htx, ist("Host"), &ctx, 0)) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001754 /* OK we have the header value in ctx.value */
1755 while (ctx.value.len--)
1756 hash = *(ctx.value.ptr++) + (hash << 6) + (hash << 16) - hash;
1757 }
1758
1759 /* now retrieve the path */
Christopher Faulet297fbb42019-05-13 14:41:27 +02001760 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001761 path = http_get_path(htx_sl_req_uri(sl));
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001762 if (path.ptr) {
1763 size_t len;
1764
Dragan Dosen8861e1c2019-02-12 19:50:31 +01001765 for (len = 0; len < path.len && *(path.ptr + len) != '?'; len++)
1766 ;
1767
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001768 if (len && *(path.ptr) == '/') {
1769 while (len--)
1770 hash = *(path.ptr++) + (hash << 6) + (hash << 16) - hash;
1771 }
1772 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001773 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001774 else {
1775 /* LEGACY version */
1776 struct http_txn *txn;
1777 struct hdr_ctx ctx;
1778 char *ptr, *beg, *end;
1779 int len;
1780
Christopher Faulet89dc4992019-04-17 12:02:59 +02001781 CHECK_HTTP_MESSAGE_FIRST(chn);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001782
1783 txn = smp->strm->txn;
1784 ctx.idx = 0;
Christopher Faulet89dc4992019-04-17 12:02:59 +02001785 if (http_find_header2("Host", 4, ci_head(chn), &txn->hdr_idx, &ctx)) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001786 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
1787 ptr = ctx.line + ctx.val;
1788 len = ctx.vlen;
1789 while (len--)
1790 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
1791 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001792
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001793 /* now retrieve the path */
Christopher Faulet89dc4992019-04-17 12:02:59 +02001794 end = ci_head(chn) + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001795 beg = http_txn_get_path(txn);
1796 if (!beg)
1797 beg = end;
Willy Tarreau79e57332018-10-02 16:01:16 +02001798
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001799 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
Willy Tarreau79e57332018-10-02 16:01:16 +02001800
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001801 if (beg < ptr && *beg == '/') {
1802 while (beg < ptr)
1803 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
1804 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001805 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001806
Willy Tarreau79e57332018-10-02 16:01:16 +02001807 hash = full_hash(hash);
1808
1809 smp->data.type = SMP_T_SINT;
1810 smp->data.u.sint = hash;
1811 smp->flags = SMP_F_VOL_1ST;
1812 return 1;
1813}
1814
1815/* This concatenates the source address with the 32-bit hash of the Host and
1816 * path as returned by smp_fetch_base32(). The idea is to have per-source and
1817 * per-path counters. The result is a binary block from 8 to 20 bytes depending
1818 * on the source address length. The path hash is stored before the address so
1819 * that in environments where IPv6 is insignificant, truncating the output to
1820 * 8 bytes would still work.
1821 */
1822static int smp_fetch_base32_src(const struct arg *args, struct sample *smp, const char *kw, void *private)
1823{
1824 struct buffer *temp;
1825 struct connection *cli_conn = objt_conn(smp->sess->origin);
1826
1827 if (!cli_conn)
1828 return 0;
1829
1830 if (!smp_fetch_base32(args, smp, kw, private))
1831 return 0;
1832
1833 temp = get_trash_chunk();
1834 *(unsigned int *) temp->area = htonl(smp->data.u.sint);
1835 temp->data += sizeof(unsigned int);
1836
1837 switch (cli_conn->addr.from.ss_family) {
1838 case AF_INET:
1839 memcpy(temp->area + temp->data,
1840 &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr,
1841 4);
1842 temp->data += 4;
1843 break;
1844 case AF_INET6:
1845 memcpy(temp->area + temp->data,
1846 &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr,
1847 16);
1848 temp->data += 16;
1849 break;
1850 default:
1851 return 0;
1852 }
1853
1854 smp->data.u.str = *temp;
1855 smp->data.type = SMP_T_BIN;
1856 return 1;
1857}
1858
1859/* Extracts the query string, which comes after the question mark '?'. If no
1860 * question mark is found, nothing is returned. Otherwise it returns a sample
1861 * of type string carrying the whole query string.
1862 */
1863static int smp_fetch_query(const struct arg *args, struct sample *smp, const char *kw, void *private)
1864{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001865 struct channel *chn = SMP_REQ_CHN(smp);
Willy Tarreau79e57332018-10-02 16:01:16 +02001866 char *ptr, *end;
1867
Christopher Faulet46575cd2019-04-17 11:40:30 +02001868 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001869 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +02001870 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001871 struct htx_sl *sl;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001872
1873 if (!htx)
1874 return 0;
1875
Christopher Faulet297fbb42019-05-13 14:41:27 +02001876 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001877 ptr = HTX_SL_REQ_UPTR(sl);
1878 end = HTX_SL_REQ_UPTR(sl) + HTX_SL_REQ_ULEN(sl);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001879 }
1880 else {
1881 /* LEGACY version */
1882 struct http_txn *txn;
1883
Christopher Faulet89dc4992019-04-17 12:02:59 +02001884 CHECK_HTTP_MESSAGE_FIRST(chn);
Willy Tarreau79e57332018-10-02 16:01:16 +02001885
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001886 txn = smp->strm->txn;
Christopher Faulet89dc4992019-04-17 12:02:59 +02001887 ptr = ci_head(chn) + txn->req.sl.rq.u;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001888 end = ptr + txn->req.sl.rq.u_l;
1889 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001890
1891 /* look up the '?' */
1892 do {
1893 if (ptr == end)
1894 return 0;
1895 } while (*ptr++ != '?');
1896
1897 smp->data.type = SMP_T_STR;
1898 smp->data.u.str.area = ptr;
1899 smp->data.u.str.data = end - ptr;
1900 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
1901 return 1;
1902}
1903
1904static int smp_fetch_proto_http(const struct arg *args, struct sample *smp, const char *kw, void *private)
1905{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001906 struct channel *chn = SMP_REQ_CHN(smp);
1907
Christopher Faulet46575cd2019-04-17 11:40:30 +02001908 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001909 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +02001910 struct htx *htx = smp_prefetch_htx(smp, chn, 0);
Willy Tarreau79e57332018-10-02 16:01:16 +02001911
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001912 if (!htx)
1913 return 0;
1914 }
1915 else {
1916 /* LEGACY version */
Willy Tarreau79e57332018-10-02 16:01:16 +02001917
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001918 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
1919 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
1920 */
Christopher Faulet89dc4992019-04-17 12:02:59 +02001921 CHECK_HTTP_MESSAGE_FIRST_PERM(chn);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001922 }
1923 smp->data.type = SMP_T_BOOL;
Willy Tarreau79e57332018-10-02 16:01:16 +02001924 smp->data.u.sint = 1;
1925 return 1;
1926}
1927
1928/* return a valid test if the current request is the first one on the connection */
1929static int smp_fetch_http_first_req(const struct arg *args, struct sample *smp, const char *kw, void *private)
1930{
1931 smp->data.type = SMP_T_BOOL;
1932 smp->data.u.sint = !(smp->strm->txn->flags & TX_NOT_FIRST);
1933 return 1;
1934}
1935
1936/* Accepts exactly 1 argument of type userlist */
1937static int smp_fetch_http_auth(const struct arg *args, struct sample *smp, const char *kw, void *private)
1938{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001939 struct channel *chn = SMP_REQ_CHN(smp);
Willy Tarreau79e57332018-10-02 16:01:16 +02001940
1941 if (!args || args->type != ARGT_USR)
1942 return 0;
1943
Christopher Faulet46575cd2019-04-17 11:40:30 +02001944 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001945 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +02001946 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Willy Tarreau79e57332018-10-02 16:01:16 +02001947
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001948 if (!htx)
1949 return 0;
Christopher Faulete98411b2019-07-15 13:58:29 +02001950 if (!get_http_auth(smp, htx))
1951 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001952 }
1953 else {
1954 /* LEGACY version */
Christopher Faulet89dc4992019-04-17 12:02:59 +02001955 CHECK_HTTP_MESSAGE_FIRST(chn);
Christopher Faulete98411b2019-07-15 13:58:29 +02001956 if (!get_http_auth(smp, NULL))
1957 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001958 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001959
1960 smp->data.type = SMP_T_BOOL;
1961 smp->data.u.sint = check_user(args->data.usr, smp->strm->txn->auth.user,
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001962 smp->strm->txn->auth.pass);
Willy Tarreau79e57332018-10-02 16:01:16 +02001963 return 1;
1964}
1965
1966/* Accepts exactly 1 argument of type userlist */
1967static int smp_fetch_http_auth_grp(const struct arg *args, struct sample *smp, const char *kw, void *private)
1968{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001969 struct channel *chn = SMP_REQ_CHN(smp);
1970
Willy Tarreau79e57332018-10-02 16:01:16 +02001971 if (!args || args->type != ARGT_USR)
1972 return 0;
1973
Christopher Faulet46575cd2019-04-17 11:40:30 +02001974 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001975 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +02001976 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001977
1978 if (!htx)
1979 return 0;
Christopher Faulete98411b2019-07-15 13:58:29 +02001980 if (!get_http_auth(smp, htx))
1981 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001982 }
1983 else {
1984 /* LEGACY version */
Christopher Faulet89dc4992019-04-17 12:02:59 +02001985 CHECK_HTTP_MESSAGE_FIRST(chn);
Christopher Faulete98411b2019-07-15 13:58:29 +02001986 if (!get_http_auth(smp, NULL))
1987 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001988 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001989
Willy Tarreau79e57332018-10-02 16:01:16 +02001990 /* if the user does not belong to the userlist or has a wrong password,
1991 * report that it unconditionally does not match. Otherwise we return
1992 * a string containing the username.
1993 */
1994 if (!check_user(args->data.usr, smp->strm->txn->auth.user,
1995 smp->strm->txn->auth.pass))
1996 return 0;
1997
1998 /* pat_match_auth() will need the user list */
1999 smp->ctx.a[0] = args->data.usr;
2000
2001 smp->data.type = SMP_T_STR;
2002 smp->flags = SMP_F_CONST;
2003 smp->data.u.str.area = smp->strm->txn->auth.user;
2004 smp->data.u.str.data = strlen(smp->strm->txn->auth.user);
2005
2006 return 1;
2007}
2008
2009/* Fetch a captured HTTP request header. The index is the position of
2010 * the "capture" option in the configuration file
2011 */
2012static int smp_fetch_capture_req_hdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
2013{
2014 struct proxy *fe = strm_fe(smp->strm);
2015 int idx;
2016
2017 if (!args || args->type != ARGT_SINT)
2018 return 0;
2019
2020 idx = args->data.sint;
2021
2022 if (idx > (fe->nb_req_cap - 1) || smp->strm->req_cap == NULL || smp->strm->req_cap[idx] == NULL)
2023 return 0;
2024
2025 smp->data.type = SMP_T_STR;
2026 smp->flags |= SMP_F_CONST;
2027 smp->data.u.str.area = smp->strm->req_cap[idx];
2028 smp->data.u.str.data = strlen(smp->strm->req_cap[idx]);
2029
2030 return 1;
2031}
2032
2033/* Fetch a captured HTTP response header. The index is the position of
2034 * the "capture" option in the configuration file
2035 */
2036static int smp_fetch_capture_res_hdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
2037{
2038 struct proxy *fe = strm_fe(smp->strm);
2039 int idx;
2040
2041 if (!args || args->type != ARGT_SINT)
2042 return 0;
2043
2044 idx = args->data.sint;
2045
2046 if (idx > (fe->nb_rsp_cap - 1) || smp->strm->res_cap == NULL || smp->strm->res_cap[idx] == NULL)
2047 return 0;
2048
2049 smp->data.type = SMP_T_STR;
2050 smp->flags |= SMP_F_CONST;
2051 smp->data.u.str.area = smp->strm->res_cap[idx];
2052 smp->data.u.str.data = strlen(smp->strm->res_cap[idx]);
2053
2054 return 1;
2055}
2056
2057/* Extracts the METHOD in the HTTP request, the txn->uri should be filled before the call */
2058static int smp_fetch_capture_req_method(const struct arg *args, struct sample *smp, const char *kw, void *private)
2059{
2060 struct buffer *temp;
2061 struct http_txn *txn = smp->strm->txn;
2062 char *ptr;
2063
2064 if (!txn || !txn->uri)
2065 return 0;
2066
2067 ptr = txn->uri;
2068
2069 while (*ptr != ' ' && *ptr != '\0') /* find first space */
2070 ptr++;
2071
2072 temp = get_trash_chunk();
2073 temp->area = txn->uri;
2074 temp->data = ptr - txn->uri;
2075 smp->data.u.str = *temp;
2076 smp->data.type = SMP_T_STR;
2077 smp->flags = SMP_F_CONST;
2078
2079 return 1;
2080
2081}
2082
2083/* Extracts the path in the HTTP request, the txn->uri should be filled before the call */
2084static int smp_fetch_capture_req_uri(const struct arg *args, struct sample *smp, const char *kw, void *private)
2085{
2086 struct http_txn *txn = smp->strm->txn;
2087 struct ist path;
2088 const char *ptr;
2089
2090 if (!txn || !txn->uri)
2091 return 0;
2092
2093 ptr = txn->uri;
2094
2095 while (*ptr != ' ' && *ptr != '\0') /* find first space */
2096 ptr++;
2097
2098 if (!*ptr)
2099 return 0;
2100
Christopher Faulet78337bb2018-11-15 14:35:18 +01002101 /* skip the first space and find space after URI */
2102 path = ist2(++ptr, 0);
2103 while (*ptr != ' ' && *ptr != '\0')
2104 ptr++;
2105 path.len = ptr - path.ptr;
Willy Tarreau79e57332018-10-02 16:01:16 +02002106
Christopher Faulet78337bb2018-11-15 14:35:18 +01002107 path = http_get_path(path);
Willy Tarreau79e57332018-10-02 16:01:16 +02002108 if (!path.ptr)
2109 return 0;
2110
2111 smp->data.u.str.area = path.ptr;
2112 smp->data.u.str.data = path.len;
2113 smp->data.type = SMP_T_STR;
2114 smp->flags = SMP_F_CONST;
2115
2116 return 1;
2117}
2118
2119/* Retrieves the HTTP version from the request (either 1.0 or 1.1) and emits it
2120 * as a string (either "HTTP/1.0" or "HTTP/1.1").
2121 */
2122static int smp_fetch_capture_req_ver(const struct arg *args, struct sample *smp, const char *kw, void *private)
2123{
2124 struct http_txn *txn = smp->strm->txn;
2125
2126 if (!txn || txn->req.msg_state < HTTP_MSG_HDR_FIRST)
2127 return 0;
2128
2129 if (txn->req.flags & HTTP_MSGF_VER_11)
2130 smp->data.u.str.area = "HTTP/1.1";
2131 else
2132 smp->data.u.str.area = "HTTP/1.0";
2133
2134 smp->data.u.str.data = 8;
2135 smp->data.type = SMP_T_STR;
2136 smp->flags = SMP_F_CONST;
2137 return 1;
2138
2139}
2140
2141/* Retrieves the HTTP version from the response (either 1.0 or 1.1) and emits it
2142 * as a string (either "HTTP/1.0" or "HTTP/1.1").
2143 */
2144static int smp_fetch_capture_res_ver(const struct arg *args, struct sample *smp, const char *kw, void *private)
2145{
2146 struct http_txn *txn = smp->strm->txn;
2147
2148 if (!txn || txn->rsp.msg_state < HTTP_MSG_HDR_FIRST)
2149 return 0;
2150
2151 if (txn->rsp.flags & HTTP_MSGF_VER_11)
2152 smp->data.u.str.area = "HTTP/1.1";
2153 else
2154 smp->data.u.str.area = "HTTP/1.0";
2155
2156 smp->data.u.str.data = 8;
2157 smp->data.type = SMP_T_STR;
2158 smp->flags = SMP_F_CONST;
2159 return 1;
2160
2161}
2162
2163/* Iterate over all cookies present in a message. The context is stored in
2164 * smp->ctx.a[0] for the in-header position, smp->ctx.a[1] for the
2165 * end-of-header-value, and smp->ctx.a[2] for the hdr_ctx. Depending on
2166 * the direction, multiple cookies may be parsed on the same line or not.
2167 * The cookie name is in args and the name length in args->data.str.len.
2168 * Accepts exactly 1 argument of type string. If the input options indicate
2169 * that no iterating is desired, then only last value is fetched if any.
2170 * The returned sample is of type CSTR. Can be used to parse cookies in other
2171 * files.
2172 */
2173static int smp_fetch_cookie(const struct arg *args, struct sample *smp, const char *kw, void *private)
2174{
Christopher Faulet89dc4992019-04-17 12:02:59 +02002175 /* possible keywords: req.cookie / cookie / cook, res.cookie / scook / set-cookie */
2176 struct channel *chn = ((kw[0] == 'c' || kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Willy Tarreau79e57332018-10-02 16:01:16 +02002177 int occ = 0;
2178 int found = 0;
2179
2180 if (!args || args->type != ARGT_STR)
2181 return 0;
2182
Christopher Faulet46575cd2019-04-17 11:40:30 +02002183 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002184 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +02002185 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002186 struct http_hdr_ctx *ctx = smp->ctx.a[2];
2187 struct ist hdr;
Willy Tarreau79e57332018-10-02 16:01:16 +02002188
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002189 if (!ctx) {
2190 /* first call */
2191 ctx = &static_http_hdr_ctx;
2192 ctx->blk = NULL;
2193 smp->ctx.a[2] = ctx;
2194 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002195
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002196 if (!htx)
2197 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02002198
Christopher Faulet89dc4992019-04-17 12:02:59 +02002199 hdr = (!(chn->flags & CF_ISRESP) ? ist("Cookie") : ist("Set-Cookie"));
Willy Tarreau79e57332018-10-02 16:01:16 +02002200
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002201 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
2202 /* no explicit occurrence and single fetch => last cookie by default */
2203 occ = -1;
Willy Tarreau79e57332018-10-02 16:01:16 +02002204
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002205 /* OK so basically here, either we want only one value and it's the
2206 * last one, or we want to iterate over all of them and we fetch the
2207 * next one.
Willy Tarreau79e57332018-10-02 16:01:16 +02002208 */
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002209
2210 if (!(smp->flags & SMP_F_NOT_LAST)) {
2211 /* search for the header from the beginning, we must first initialize
2212 * the search parameters.
2213 */
2214 smp->ctx.a[0] = NULL;
2215 ctx->blk = NULL;
2216 }
2217
2218 smp->flags |= SMP_F_VOL_HDR;
2219 while (1) {
2220 /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */
2221 if (!smp->ctx.a[0]) {
2222 if (!http_find_header(htx, hdr, ctx, 0))
2223 goto out;
2224
2225 if (ctx->value.len < args->data.str.data + 1)
2226 continue;
2227
2228 smp->ctx.a[0] = ctx->value.ptr;
2229 smp->ctx.a[1] = smp->ctx.a[0] + ctx->value.len;
2230 }
2231
2232 smp->data.type = SMP_T_STR;
2233 smp->flags |= SMP_F_CONST;
2234 smp->ctx.a[0] = http_extract_cookie_value(smp->ctx.a[0], smp->ctx.a[1],
2235 args->data.str.area, args->data.str.data,
2236 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
2237 &smp->data.u.str.area,
2238 &smp->data.u.str.data);
2239 if (smp->ctx.a[0]) {
2240 found = 1;
2241 if (occ >= 0) {
2242 /* one value was returned into smp->data.u.str.{str,len} */
2243 smp->flags |= SMP_F_NOT_LAST;
2244 return 1;
2245 }
2246 }
2247 /* if we're looking for last occurrence, let's loop */
2248 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002249 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002250 else {
2251 /* LEGACY version */
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002252 struct hdr_idx *idx;
2253 struct hdr_ctx *ctx = smp->ctx.a[2];
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002254 const char *hdr_name;
2255 int hdr_name_len;
2256 char *sol;
Willy Tarreau79e57332018-10-02 16:01:16 +02002257
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002258 if (!ctx) {
2259 /* first call */
2260 ctx = &static_hdr_ctx;
2261 ctx->idx = 0;
2262 smp->ctx.a[2] = ctx;
2263 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002264
Christopher Faulet89dc4992019-04-17 12:02:59 +02002265 CHECK_HTTP_MESSAGE_FIRST(chn);
Willy Tarreau79e57332018-10-02 16:01:16 +02002266
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002267 idx = &smp->strm->txn->hdr_idx;
Christopher Faulet89dc4992019-04-17 12:02:59 +02002268 if (!(chn->flags & CF_ISRESP)) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002269 hdr_name = "Cookie";
2270 hdr_name_len = 6;
2271 } else {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002272 hdr_name = "Set-Cookie";
2273 hdr_name_len = 10;
Willy Tarreau79e57332018-10-02 16:01:16 +02002274 }
2275
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002276 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
2277 /* no explicit occurrence and single fetch => last cookie by default */
2278 occ = -1;
2279
2280 /* OK so basically here, either we want only one value and it's the
2281 * last one, or we want to iterate over all of them and we fetch the
2282 * next one.
2283 */
2284
Christopher Faulet89dc4992019-04-17 12:02:59 +02002285 sol = ci_head(chn);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002286 if (!(smp->flags & SMP_F_NOT_LAST)) {
2287 /* search for the header from the beginning, we must first initialize
2288 * the search parameters.
2289 */
2290 smp->ctx.a[0] = NULL;
2291 ctx->idx = 0;
2292 }
2293
2294 smp->flags |= SMP_F_VOL_HDR;
2295
2296 while (1) {
2297 /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */
2298 if (!smp->ctx.a[0]) {
2299 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, ctx))
2300 goto out;
2301
2302 if (ctx->vlen < args->data.str.data + 1)
2303 continue;
2304
2305 smp->ctx.a[0] = ctx->line + ctx->val;
2306 smp->ctx.a[1] = smp->ctx.a[0] + ctx->vlen;
2307 }
2308
2309 smp->data.type = SMP_T_STR;
2310 smp->flags |= SMP_F_CONST;
2311 smp->ctx.a[0] = http_extract_cookie_value(smp->ctx.a[0], smp->ctx.a[1],
2312 args->data.str.area, args->data.str.data,
2313 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
2314 &smp->data.u.str.area, &smp->data.u.str.data);
2315 if (smp->ctx.a[0]) {
2316 found = 1;
2317 if (occ >= 0) {
2318 /* one value was returned into smp->data.u.str.{str,len} */
2319 smp->flags |= SMP_F_NOT_LAST;
2320 return 1;
2321 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002322 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002323 /* if we're looking for last occurrence, let's loop */
Willy Tarreau79e57332018-10-02 16:01:16 +02002324 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002325 }
2326 /* all cookie headers and values were scanned. If we're looking for the
2327 * last occurrence, we may return it now.
2328 */
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002329 out:
Willy Tarreau79e57332018-10-02 16:01:16 +02002330 smp->flags &= ~SMP_F_NOT_LAST;
2331 return found;
2332}
2333
Christopher Fauletc1f40dd2019-05-16 10:07:30 +02002334/* Same than smp_fetch_cookie() but only relies on the sample direction to
2335 * choose the right channel. So instead of duplicating the code, we just change
2336 * the keyword and then fallback on smp_fetch_cookie().
2337 */
2338static int smp_fetch_chn_cookie(const struct arg *args, struct sample *smp, const char *kw, void *private)
2339{
2340 kw = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ ? "req.cook" : "res.cook");
2341 return smp_fetch_cookie(args, smp, kw, private);
2342}
2343
Willy Tarreau79e57332018-10-02 16:01:16 +02002344/* Iterate over all cookies present in a request to count how many occurrences
2345 * match the name in args and args->data.str.len. If <multi> is non-null, then
2346 * multiple cookies may be parsed on the same line. The returned sample is of
2347 * type UINT. Accepts exactly 1 argument of type string.
2348 */
2349static int smp_fetch_cookie_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
2350{
Christopher Faulet89dc4992019-04-17 12:02:59 +02002351 /* possible keywords: req.cook_cnt / cook_cnt, res.cook_cnt / scook_cnt */
2352 struct channel *chn = ((kw[0] == 'c' || kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Willy Tarreau79e57332018-10-02 16:01:16 +02002353 char *val_beg, *val_end;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002354 int cnt;
Willy Tarreau79e57332018-10-02 16:01:16 +02002355
2356 if (!args || args->type != ARGT_STR)
2357 return 0;
2358
Christopher Faulet46575cd2019-04-17 11:40:30 +02002359 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002360 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +02002361 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002362 struct http_hdr_ctx ctx;
2363 struct ist hdr;
2364
2365 if (!htx)
2366 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02002367
Christopher Faulet89dc4992019-04-17 12:02:59 +02002368 hdr = (!(chn->flags & CF_ISRESP) ? ist("Cookie") : ist("Set-Cookie"));
Willy Tarreau79e57332018-10-02 16:01:16 +02002369
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002370 val_end = val_beg = NULL;
2371 ctx.blk = NULL;
2372 cnt = 0;
2373 while (1) {
2374 /* Note: val_beg == NULL every time we need to fetch a new header */
2375 if (!val_beg) {
2376 if (!http_find_header(htx, hdr, &ctx, 0))
2377 break;
Willy Tarreau79e57332018-10-02 16:01:16 +02002378
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002379 if (ctx.value.len < args->data.str.data + 1)
2380 continue;
Willy Tarreau79e57332018-10-02 16:01:16 +02002381
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002382 val_beg = ctx.value.ptr;
2383 val_end = val_beg + ctx.value.len;
2384 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002385
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002386 smp->data.type = SMP_T_STR;
2387 smp->flags |= SMP_F_CONST;
2388 while ((val_beg = http_extract_cookie_value(val_beg, val_end,
2389 args->data.str.area, args->data.str.data,
2390 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
2391 &smp->data.u.str.area,
2392 &smp->data.u.str.data))) {
2393 cnt++;
2394 }
2395 }
2396 }
2397 else {
2398 /* LEGACY version */
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002399 struct hdr_idx *idx;
2400 struct hdr_ctx ctx;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002401 const char *hdr_name;
2402 int hdr_name_len;
2403 char *sol;
Willy Tarreau79e57332018-10-02 16:01:16 +02002404
Christopher Faulet89dc4992019-04-17 12:02:59 +02002405 CHECK_HTTP_MESSAGE_FIRST(chn);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002406
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002407 idx = &smp->strm->txn->hdr_idx;
Christopher Faulet89dc4992019-04-17 12:02:59 +02002408 if (!(chn->flags & CF_ISRESP)) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002409 hdr_name = "Cookie";
2410 hdr_name_len = 6;
2411 } else {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002412 hdr_name = "Set-Cookie";
2413 hdr_name_len = 10;
Willy Tarreau79e57332018-10-02 16:01:16 +02002414 }
2415
Christopher Faulet89dc4992019-04-17 12:02:59 +02002416 sol = ci_head(chn);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002417 val_end = val_beg = NULL;
2418 ctx.idx = 0;
2419 cnt = 0;
2420
2421 while (1) {
2422 /* Note: val_beg == NULL every time we need to fetch a new header */
2423 if (!val_beg) {
2424 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, &ctx))
2425 break;
2426
2427 if (ctx.vlen < args->data.str.data + 1)
2428 continue;
2429
2430 val_beg = ctx.line + ctx.val;
2431 val_end = val_beg + ctx.vlen;
2432 }
2433
2434 smp->data.type = SMP_T_STR;
2435 smp->flags |= SMP_F_CONST;
2436 while ((val_beg = http_extract_cookie_value(val_beg, val_end,
2437 args->data.str.area, args->data.str.data,
2438 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
2439 &smp->data.u.str.area, &smp->data.u.str.data))) {
2440 cnt++;
2441 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002442 }
2443 }
2444
2445 smp->data.type = SMP_T_SINT;
2446 smp->data.u.sint = cnt;
2447 smp->flags |= SMP_F_VOL_HDR;
2448 return 1;
2449}
2450
2451/* Fetch an cookie's integer value. The integer value is returned. It
2452 * takes a mandatory argument of type string. It relies on smp_fetch_cookie().
2453 */
2454static int smp_fetch_cookie_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
2455{
2456 int ret = smp_fetch_cookie(args, smp, kw, private);
2457
2458 if (ret > 0) {
2459 smp->data.type = SMP_T_SINT;
2460 smp->data.u.sint = strl2ic(smp->data.u.str.area,
2461 smp->data.u.str.data);
2462 }
2463
2464 return ret;
2465}
2466
2467/************************************************************************/
2468/* The code below is dedicated to sample fetches */
2469/************************************************************************/
2470
2471/* This scans a URL-encoded query string. It takes an optionally wrapping
2472 * string whose first contigous chunk has its beginning in ctx->a[0] and end
2473 * in ctx->a[1], and the optional second part in (ctx->a[2]..ctx->a[3]). The
2474 * pointers are updated for next iteration before leaving.
2475 */
2476static int smp_fetch_param(char delim, const char *name, int name_len, const struct arg *args, struct sample *smp, const char *kw, void *private)
2477{
2478 const char *vstart, *vend;
2479 struct buffer *temp;
2480 const char **chunks = (const char **)smp->ctx.a;
2481
2482 if (!http_find_next_url_param(chunks, name, name_len,
2483 &vstart, &vend, delim))
2484 return 0;
2485
2486 /* Create sample. If the value is contiguous, return the pointer as CONST,
2487 * if the value is wrapped, copy-it in a buffer.
2488 */
2489 smp->data.type = SMP_T_STR;
2490 if (chunks[2] &&
2491 vstart >= chunks[0] && vstart <= chunks[1] &&
2492 vend >= chunks[2] && vend <= chunks[3]) {
2493 /* Wrapped case. */
2494 temp = get_trash_chunk();
2495 memcpy(temp->area, vstart, chunks[1] - vstart);
2496 memcpy(temp->area + ( chunks[1] - vstart ), chunks[2],
2497 vend - chunks[2]);
2498 smp->data.u.str.area = temp->area;
2499 smp->data.u.str.data = ( chunks[1] - vstart ) + ( vend - chunks[2] );
2500 } else {
2501 /* Contiguous case. */
2502 smp->data.u.str.area = (char *)vstart;
2503 smp->data.u.str.data = vend - vstart;
2504 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
2505 }
2506
2507 /* Update context, check wrapping. */
2508 chunks[0] = vend;
2509 if (chunks[2] && vend >= chunks[2] && vend <= chunks[3]) {
2510 chunks[1] = chunks[3];
2511 chunks[2] = NULL;
2512 }
2513
2514 if (chunks[0] < chunks[1])
2515 smp->flags |= SMP_F_NOT_LAST;
2516
2517 return 1;
2518}
2519
2520/* This function iterates over each parameter of the query string. It uses
2521 * ctx->a[0] and ctx->a[1] to store the beginning and end of the current
2522 * parameter. Since it uses smp_fetch_param(), ctx->a[2..3] are both NULL.
2523 * An optional parameter name is passed in args[0], otherwise any parameter is
2524 * considered. It supports an optional delimiter argument for the beginning of
2525 * the string in args[1], which defaults to "?".
2526 */
2527static int smp_fetch_url_param(const struct arg *args, struct sample *smp, const char *kw, void *private)
2528{
Christopher Faulet89dc4992019-04-17 12:02:59 +02002529 struct channel *chn = SMP_REQ_CHN(smp);
Willy Tarreau79e57332018-10-02 16:01:16 +02002530 char delim = '?';
2531 const char *name;
2532 int name_len;
2533
2534 if (!args ||
2535 (args[0].type && args[0].type != ARGT_STR) ||
2536 (args[1].type && args[1].type != ARGT_STR))
2537 return 0;
2538
2539 name = "";
2540 name_len = 0;
2541 if (args->type == ARGT_STR) {
2542 name = args->data.str.area;
2543 name_len = args->data.str.data;
2544 }
2545
2546 if (args[1].type)
2547 delim = *args[1].data.str.area;
2548
2549 if (!smp->ctx.a[0]) { // first call, find the query string
Christopher Faulet46575cd2019-04-17 11:40:30 +02002550 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002551 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +02002552 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01002553 struct htx_sl *sl;
Willy Tarreau79e57332018-10-02 16:01:16 +02002554
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002555 if (!htx)
2556 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02002557
Christopher Faulet297fbb42019-05-13 14:41:27 +02002558 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01002559 smp->ctx.a[0] = http_find_param_list(HTX_SL_REQ_UPTR(sl), HTX_SL_REQ_ULEN(sl), delim);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002560 if (!smp->ctx.a[0])
2561 return 0;
2562
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01002563 smp->ctx.a[1] = HTX_SL_REQ_UPTR(sl) + HTX_SL_REQ_ULEN(sl);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002564 }
2565 else {
2566 /* LEGACY version */
2567 struct http_msg *msg;
2568
Christopher Faulet89dc4992019-04-17 12:02:59 +02002569 CHECK_HTTP_MESSAGE_FIRST(chn);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002570
2571 msg = &smp->strm->txn->req;
2572
Christopher Faulet89dc4992019-04-17 12:02:59 +02002573 smp->ctx.a[0] = http_find_param_list(ci_head(chn) + msg->sl.rq.u,
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002574 msg->sl.rq.u_l, delim);
2575 if (!smp->ctx.a[0])
2576 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02002577
Christopher Faulet89dc4992019-04-17 12:02:59 +02002578 smp->ctx.a[1] = ci_head(chn) + msg->sl.rq.u + msg->sl.rq.u_l;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002579 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002580
2581 /* Assume that the context is filled with NULL pointer
2582 * before the first call.
2583 * smp->ctx.a[2] = NULL;
2584 * smp->ctx.a[3] = NULL;
2585 */
2586 }
2587
2588 return smp_fetch_param(delim, name, name_len, args, smp, kw, private);
2589}
2590
2591/* This function iterates over each parameter of the body. This requires
2592 * that the body has been waited for using http-buffer-request. It uses
2593 * ctx->a[0] and ctx->a[1] to store the beginning and end of the first
2594 * contigous part of the body, and optionally ctx->a[2..3] to reference the
2595 * optional second part if the body wraps at the end of the buffer. An optional
2596 * parameter name is passed in args[0], otherwise any parameter is considered.
2597 */
2598static int smp_fetch_body_param(const struct arg *args, struct sample *smp, const char *kw, void *private)
2599{
Christopher Faulet89dc4992019-04-17 12:02:59 +02002600 struct channel *chn = SMP_REQ_CHN(smp);
Willy Tarreau79e57332018-10-02 16:01:16 +02002601 const char *name;
2602 int name_len;
2603
2604 if (!args || (args[0].type && args[0].type != ARGT_STR))
2605 return 0;
2606
2607 name = "";
2608 name_len = 0;
2609 if (args[0].type == ARGT_STR) {
2610 name = args[0].data.str.area;
2611 name_len = args[0].data.str.data;
2612 }
2613
2614 if (!smp->ctx.a[0]) { // first call, find the query string
Christopher Faulet46575cd2019-04-17 11:40:30 +02002615 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002616 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +02002617 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002618 struct buffer *temp;
2619 int32_t pos;
Willy Tarreau79e57332018-10-02 16:01:16 +02002620
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002621 if (!htx)
2622 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02002623
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002624 temp = get_trash_chunk();
Christopher Fauleta3f15502019-05-13 15:27:23 +02002625 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002626 struct htx_blk *blk = htx_get_blk(htx, pos);
2627 enum htx_blk_type type = htx_get_blk_type(blk);
Willy Tarreau79e57332018-10-02 16:01:16 +02002628
Christopher Faulet54b5e212019-06-04 10:08:28 +02002629 if (type == HTX_BLK_EOM || type == HTX_BLK_TLR || type == HTX_BLK_EOT)
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002630 break;
2631 if (type == HTX_BLK_DATA) {
Christopher Fauletc59ff232018-12-03 13:58:44 +01002632 if (!htx_data_to_h1(htx_get_blk_value(htx, blk), temp, 0))
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002633 return 0;
2634 }
2635 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002636
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002637 smp->ctx.a[0] = temp->area;
2638 smp->ctx.a[1] = temp->area + temp->data;
Willy Tarreau79e57332018-10-02 16:01:16 +02002639
2640 /* Assume that the context is filled with NULL pointer
2641 * before the first call.
2642 * smp->ctx.a[2] = NULL;
2643 * smp->ctx.a[3] = NULL;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002644 */
Willy Tarreau79e57332018-10-02 16:01:16 +02002645 }
2646 else {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002647 /* LEGACY version */
2648 struct http_msg *msg;
2649 unsigned long len;
2650 unsigned long block1;
2651 char *body;
2652
Christopher Faulet89dc4992019-04-17 12:02:59 +02002653 CHECK_HTTP_MESSAGE_FIRST(chn);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002654
Christopher Faulet89dc4992019-04-17 12:02:59 +02002655 msg = &smp->strm->txn->req;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002656 len = http_body_bytes(msg);
Christopher Faulet89dc4992019-04-17 12:02:59 +02002657 body = c_ptr(chn, -http_data_rewind(msg));
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002658
2659 block1 = len;
Christopher Faulet89dc4992019-04-17 12:02:59 +02002660 if (block1 > b_wrap(&chn->buf) - body)
2661 block1 = b_wrap(&chn->buf) - body;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002662
2663 if (block1 == len) {
2664 /* buffer is not wrapped (or empty) */
2665 smp->ctx.a[0] = body;
2666 smp->ctx.a[1] = body + len;
2667
2668 /* Assume that the context is filled with NULL pointer
2669 * before the first call.
2670 * smp->ctx.a[2] = NULL;
2671 * smp->ctx.a[3] = NULL;
2672 */
2673 }
2674 else {
2675 /* buffer is wrapped, we need to defragment it */
2676 smp->ctx.a[0] = body;
2677 smp->ctx.a[1] = body + block1;
Christopher Faulet89dc4992019-04-17 12:02:59 +02002678 smp->ctx.a[2] = b_orig(&chn->buf);
2679 smp->ctx.a[3] = b_orig(&chn->buf) + ( len - block1 );
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002680 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002681 }
2682 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002683
Willy Tarreau79e57332018-10-02 16:01:16 +02002684 return smp_fetch_param('&', name, name_len, args, smp, kw, private);
2685}
2686
2687/* Return the signed integer value for the specified url parameter (see url_param
2688 * above).
2689 */
2690static int smp_fetch_url_param_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
2691{
2692 int ret = smp_fetch_url_param(args, smp, kw, private);
2693
2694 if (ret > 0) {
2695 smp->data.type = SMP_T_SINT;
2696 smp->data.u.sint = strl2ic(smp->data.u.str.area,
2697 smp->data.u.str.data);
2698 }
2699
2700 return ret;
2701}
2702
2703/* This produces a 32-bit hash of the concatenation of the first occurrence of
2704 * the Host header followed by the path component if it begins with a slash ('/').
2705 * This means that '*' will not be added, resulting in exactly the first Host
2706 * entry. If no Host header is found, then the path is used. The resulting value
2707 * is hashed using the url hash followed by a full avalanche hash and provides a
2708 * 32-bit integer value. This fetch is useful for tracking per-URL activity on
2709 * high-traffic sites without having to store whole paths.
2710 * this differs from the base32 functions in that it includes the url parameters
2711 * as well as the path
2712 */
2713static int smp_fetch_url32(const struct arg *args, struct sample *smp, const char *kw, void *private)
2714{
Christopher Faulet89dc4992019-04-17 12:02:59 +02002715 struct channel *chn = SMP_REQ_CHN(smp);
Willy Tarreau79e57332018-10-02 16:01:16 +02002716 unsigned int hash = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02002717
Christopher Faulet46575cd2019-04-17 11:40:30 +02002718 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002719 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +02002720 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002721 struct http_hdr_ctx ctx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01002722 struct htx_sl *sl;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002723 struct ist path;
Willy Tarreau79e57332018-10-02 16:01:16 +02002724
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002725 if (!htx)
2726 return 0;
2727
2728 ctx.blk = NULL;
2729 if (http_find_header(htx, ist("Host"), &ctx, 1)) {
2730 /* OK we have the header value in ctx.value */
2731 while (ctx.value.len--)
2732 hash = *(ctx.value.ptr++) + (hash << 6) + (hash << 16) - hash;
2733 }
2734
2735 /* now retrieve the path */
Christopher Faulet297fbb42019-05-13 14:41:27 +02002736 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01002737 path = http_get_path(htx_sl_req_uri(sl));
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002738 if (path.len && *(path.ptr) == '/') {
2739 while (path.len--)
2740 hash = *(path.ptr++) + (hash << 6) + (hash << 16) - hash;
2741 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002742 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002743 else {
2744 /* LEGACY version */
2745 struct http_txn *txn;
2746 struct hdr_ctx ctx;
2747 char *ptr, *beg, *end;
2748 int len;
2749
Christopher Faulet89dc4992019-04-17 12:02:59 +02002750 CHECK_HTTP_MESSAGE_FIRST(chn);
Willy Tarreau79e57332018-10-02 16:01:16 +02002751
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002752 txn = smp->strm->txn;
2753 ctx.idx = 0;
Christopher Faulet89dc4992019-04-17 12:02:59 +02002754 if (http_find_header2("Host", 4, ci_head(chn), &txn->hdr_idx, &ctx)) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002755 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
2756 ptr = ctx.line + ctx.val;
2757 len = ctx.vlen;
2758 while (len--)
2759 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
2760 }
2761
2762 /* now retrieve the path */
Christopher Faulet89dc4992019-04-17 12:02:59 +02002763 end = ci_head(chn) + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002764 beg = http_txn_get_path(txn);
2765 if (!beg)
2766 beg = end;
Willy Tarreau79e57332018-10-02 16:01:16 +02002767
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002768 for (ptr = beg; ptr < end ; ptr++);
Willy Tarreau79e57332018-10-02 16:01:16 +02002769
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002770 if (beg < ptr && *beg == '/') {
2771 while (beg < ptr)
2772 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
2773 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002774 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002775
Willy Tarreau79e57332018-10-02 16:01:16 +02002776 hash = full_hash(hash);
2777
2778 smp->data.type = SMP_T_SINT;
2779 smp->data.u.sint = hash;
2780 smp->flags = SMP_F_VOL_1ST;
2781 return 1;
2782}
2783
2784/* This concatenates the source address with the 32-bit hash of the Host and
2785 * URL as returned by smp_fetch_base32(). The idea is to have per-source and
2786 * per-url counters. The result is a binary block from 8 to 20 bytes depending
2787 * on the source address length. The URL hash is stored before the address so
2788 * that in environments where IPv6 is insignificant, truncating the output to
2789 * 8 bytes would still work.
2790 */
2791static int smp_fetch_url32_src(const struct arg *args, struct sample *smp, const char *kw, void *private)
2792{
2793 struct buffer *temp;
2794 struct connection *cli_conn = objt_conn(smp->sess->origin);
2795
2796 if (!cli_conn)
2797 return 0;
2798
2799 if (!smp_fetch_url32(args, smp, kw, private))
2800 return 0;
2801
2802 temp = get_trash_chunk();
2803 *(unsigned int *) temp->area = htonl(smp->data.u.sint);
2804 temp->data += sizeof(unsigned int);
2805
2806 switch (cli_conn->addr.from.ss_family) {
2807 case AF_INET:
2808 memcpy(temp->area + temp->data,
2809 &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr,
2810 4);
2811 temp->data += 4;
2812 break;
2813 case AF_INET6:
2814 memcpy(temp->area + temp->data,
2815 &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr,
2816 16);
2817 temp->data += 16;
2818 break;
2819 default:
2820 return 0;
2821 }
2822
2823 smp->data.u.str = *temp;
2824 smp->data.type = SMP_T_BIN;
2825 return 1;
2826}
2827
2828/************************************************************************/
2829/* Other utility functions */
2830/************************************************************************/
2831
2832/* This function is used to validate the arguments passed to any "hdr" fetch
2833 * keyword. These keywords support an optional positive or negative occurrence
2834 * number. We must ensure that the number is greater than -MAX_HDR_HISTORY. It
2835 * is assumed that the types are already the correct ones. Returns 0 on error,
2836 * non-zero if OK. If <err> is not NULL, it will be filled with a pointer to an
2837 * error message in case of error, that the caller is responsible for freeing.
2838 * The initial location must either be freeable or NULL.
2839 * Note: this function's pointer is checked from Lua.
2840 */
2841int val_hdr(struct arg *arg, char **err_msg)
2842{
2843 if (arg && arg[1].type == ARGT_SINT && arg[1].data.sint < -MAX_HDR_HISTORY) {
2844 memprintf(err_msg, "header occurrence must be >= %d", -MAX_HDR_HISTORY);
2845 return 0;
2846 }
2847 return 1;
2848}
2849
2850/************************************************************************/
2851/* All supported sample fetch keywords must be declared here. */
2852/************************************************************************/
2853
2854/* Note: must not be declared <const> as its list will be overwritten */
2855static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
2856 { "base", smp_fetch_base, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2857 { "base32", smp_fetch_base32, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
2858 { "base32+src", smp_fetch_base32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
2859
2860 /* capture are allocated and are permanent in the stream */
2861 { "capture.req.hdr", smp_fetch_capture_req_hdr, ARG1(1,SINT), NULL, SMP_T_STR, SMP_USE_HRQHP },
2862
2863 /* retrieve these captures from the HTTP logs */
2864 { "capture.req.method", smp_fetch_capture_req_method, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
2865 { "capture.req.uri", smp_fetch_capture_req_uri, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
2866 { "capture.req.ver", smp_fetch_capture_req_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
2867
2868 { "capture.res.hdr", smp_fetch_capture_res_hdr, ARG1(1,SINT), NULL, SMP_T_STR, SMP_USE_HRSHP },
2869 { "capture.res.ver", smp_fetch_capture_res_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
2870
2871 /* cookie is valid in both directions (eg: for "stick ...") but cook*
2872 * are only here to match the ACL's name, are request-only and are used
2873 * for ACL compatibility only.
2874 */
2875 { "cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Christopher Fauletc1f40dd2019-05-16 10:07:30 +02002876 { "cookie", smp_fetch_chn_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV|SMP_USE_HRSHV },
Willy Tarreau79e57332018-10-02 16:01:16 +02002877 { "cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2878 { "cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2879
2880 /* hdr is valid in both directions (eg: for "stick ...") but hdr_* are
2881 * only here to match the ACL's name, are request-only and are used for
2882 * ACL compatibility only.
2883 */
Christopher Fauletc1f40dd2019-05-16 10:07:30 +02002884 { "hdr", smp_fetch_chn_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV|SMP_USE_HRSHV },
Willy Tarreau79e57332018-10-02 16:01:16 +02002885 { "hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2886 { "hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV },
2887 { "hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_SINT, SMP_USE_HRQHV },
2888
2889 { "http_auth", smp_fetch_http_auth, ARG1(1,USR), NULL, SMP_T_BOOL, SMP_USE_HRQHV },
2890 { "http_auth_group", smp_fetch_http_auth_grp, ARG1(1,USR), NULL, SMP_T_STR, SMP_USE_HRQHV },
2891 { "http_first_req", smp_fetch_http_first_req, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
2892 { "method", smp_fetch_meth, 0, NULL, SMP_T_METH, SMP_USE_HRQHP },
2893 { "path", smp_fetch_path, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2894 { "query", smp_fetch_query, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2895
2896 /* HTTP protocol on the request path */
2897 { "req.proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
2898 { "req_proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
2899
2900 /* HTTP version on the request path */
2901 { "req.ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2902 { "req_ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2903
2904 { "req.body", smp_fetch_body, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
2905 { "req.body_len", smp_fetch_body_len, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
2906 { "req.body_size", smp_fetch_body_size, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
2907 { "req.body_param", smp_fetch_body_param, ARG1(0,STR), NULL, SMP_T_BIN, SMP_USE_HRQHV },
2908
2909 { "req.hdrs", smp_fetch_hdrs, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
2910 { "req.hdrs_bin", smp_fetch_hdrs_bin, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
2911
2912 /* HTTP version on the response path */
2913 { "res.ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
2914 { "resp_ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
2915
2916 /* explicit req.{cook,hdr} are used to force the fetch direction to be request-only */
2917 { "req.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
2918 { "req.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2919 { "req.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2920
2921 { "req.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
2922 { "req.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2923 { "req.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
2924 { "req.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2925 { "req.hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV },
2926 { "req.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
2927 { "req.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_SINT, SMP_USE_HRQHV },
2928
2929 /* explicit req.{cook,hdr} are used to force the fetch direction to be response-only */
2930 { "res.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
2931 { "res.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2932 { "res.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2933
2934 { "res.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
2935 { "res.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2936 { "res.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
2937 { "res.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2938 { "res.hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV },
2939 { "res.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
2940 { "res.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_SINT, SMP_USE_HRSHV },
2941
2942 /* scook is valid only on the response and is used for ACL compatibility */
2943 { "scook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
2944 { "scook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2945 { "scook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2946 { "set-cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV }, /* deprecated */
2947
2948 /* shdr is valid only on the response and is used for ACL compatibility */
2949 { "shdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
2950 { "shdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2951 { "shdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV },
2952 { "shdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_SINT, SMP_USE_HRSHV },
2953
2954 { "status", smp_fetch_stcode, 0, NULL, SMP_T_SINT, SMP_USE_HRSHP },
2955 { "unique-id", smp_fetch_uniqueid, 0, NULL, SMP_T_STR, SMP_SRC_L4SRV },
2956 { "url", smp_fetch_url, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2957 { "url32", smp_fetch_url32, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
2958 { "url32+src", smp_fetch_url32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
2959 { "url_ip", smp_fetch_url_ip, 0, NULL, SMP_T_IPV4, SMP_USE_HRQHV },
2960 { "url_port", smp_fetch_url_port, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
2961 { "url_param", smp_fetch_url_param, ARG2(0,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
2962 { "urlp" , smp_fetch_url_param, ARG2(0,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
2963 { "urlp_val", smp_fetch_url_param_val, ARG2(0,STR,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2964 { /* END */ },
2965}};
2966
Willy Tarreau0108d902018-11-25 19:14:37 +01002967INITCALL1(STG_REGISTER, sample_register_fetches, &sample_fetch_keywords);
Willy Tarreau79e57332018-10-02 16:01:16 +02002968
2969/*
2970 * Local variables:
2971 * c-indent-level: 8
2972 * c-basic-offset: 8
2973 * End:
2974 */