blob: 1912508000f2168f34ac1a108fd40609860f2545 [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 ;
Willy Tarreau91d0b602020-08-12 14:04:52 +0200200 * NULL if the sample's direction does not match the channel's (i.e. the
201 * function was asked to work on the wrong channel)
Christopher Fauletef453ed2018-10-24 21:39:27 +0200202 * The HTX message if ready
203 */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +0200204struct htx *smp_prefetch_htx(struct sample *smp, struct channel *chn, int vol)
Christopher Fauletef453ed2018-10-24 21:39:27 +0200205{
Christopher Fauletef453ed2018-10-24 21:39:27 +0200206 struct stream *s = smp->strm;
Christopher Fauletef453ed2018-10-24 21:39:27 +0200207 struct http_txn *txn = NULL;
208 struct htx *htx = NULL;
Christopher Faulet89dc4992019-04-17 12:02:59 +0200209 struct http_msg *msg;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100210 struct htx_sl *sl;
Christopher Fauletef453ed2018-10-24 21:39:27 +0200211
212 /* Note: it is possible that <s> is NULL when called before stream
213 * initialization (eg: tcp-request connection), so this function is the
214 * one responsible for guarding against this case for all HTTP users.
215 */
Christopher Faulet89dc4992019-04-17 12:02:59 +0200216 if (!s || !chn)
Christopher Fauletef453ed2018-10-24 21:39:27 +0200217 return NULL;
218
Willy Tarreau91d0b602020-08-12 14:04:52 +0200219 if (((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ && (chn->flags & CF_ISRESP)) ||
220 ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES && !(chn->flags & CF_ISRESP)))
221 return 0;
222
Christopher Fauletef453ed2018-10-24 21:39:27 +0200223 if (!s->txn) {
224 if (unlikely(!http_alloc_txn(s)))
225 return NULL; /* not enough memory */
226 http_init_txn(s);
227 txn = s->txn;
228 }
Christopher Faulet89dc4992019-04-17 12:02:59 +0200229 txn = s->txn;
230 msg = (!(chn->flags & CF_ISRESP) ? &txn->req : &txn->rsp);
231 smp->data.type = SMP_T_BOOL;
Christopher Fauletef453ed2018-10-24 21:39:27 +0200232
Christopher Fauleteca88542019-04-03 10:12:42 +0200233 if (IS_HTX_STRM(s)) {
Christopher Faulet89dc4992019-04-17 12:02:59 +0200234 htx = htxbuf(&chn->buf);
Christopher Fauletef453ed2018-10-24 21:39:27 +0200235
Christopher Faulet89dc4992019-04-17 12:02:59 +0200236 if (msg->msg_state == HTTP_MSG_ERROR || (htx->flags & HTX_FL_PARSING_ERROR))
237 return NULL;
Christopher Fauletef453ed2018-10-24 21:39:27 +0200238
Christopher Faulet89dc4992019-04-17 12:02:59 +0200239 if (msg->msg_state < HTTP_MSG_BODY) {
240 /* Analyse not yet started */
Christopher Faulet29f17582019-05-23 11:03:26 +0200241 if (htx_is_empty(htx) || htx->first == -1) {
Christopher Fauletef453ed2018-10-24 21:39:27 +0200242 /* Parsing is done by the mux, just wait */
243 smp->flags |= SMP_F_MAY_CHANGE;
244 return NULL;
245 }
246 }
Christopher Faulet297fbb42019-05-13 14:41:27 +0200247 sl = http_get_stline(htx);
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +0200248 if (vol && !sl) {
Christopher Faulet89dc4992019-04-17 12:02:59 +0200249 /* The start-line was already forwarded, it is too late to fetch anything */
250 return NULL;
251 }
Christopher Fauletef453ed2018-10-24 21:39:27 +0200252 }
Christopher Fauleteca88542019-04-03 10:12:42 +0200253 else { /* RAW mode */
Christopher Faulet89dc4992019-04-17 12:02:59 +0200254 struct buffer *buf;
255 struct h1m h1m;
Christopher Faulete4ab11b2019-06-11 15:05:37 +0200256 struct http_hdr hdrs[global.tune.max_http_hdr];
Christopher Faulet89dc4992019-04-17 12:02:59 +0200257 union h1_sl h1sl;
258 unsigned int flags = HTX_FL_NONE;
259 int ret;
Christopher Fauletef453ed2018-10-24 21:39:27 +0200260
Christopher Faulet89dc4992019-04-17 12:02:59 +0200261 /* no HTTP fetch on the response in TCP mode */
262 if (chn->flags & CF_ISRESP)
263 return NULL;
Christopher Fauletef453ed2018-10-24 21:39:27 +0200264
Christopher Faulet89dc4992019-04-17 12:02:59 +0200265 /* Now we are working on the request only */
266 buf = &chn->buf;
267 if (b_head(buf) + b_data(buf) > b_wrap(buf))
268 b_slow_realign(buf, trash.area, 0);
Christopher Fauletef453ed2018-10-24 21:39:27 +0200269
Christopher Faulet89dc4992019-04-17 12:02:59 +0200270 h1m_init_req(&h1m);
271 ret = h1_headers_to_hdr_list(b_head(buf), b_stop(buf),
272 hdrs, sizeof(hdrs)/sizeof(hdrs[0]), &h1m, &h1sl);
273 if (ret <= 0) {
274 /* Invalid or too big*/
275 if (ret < 0 || channel_full(&s->req, global.tune.maxrewrite))
Christopher Fauletef453ed2018-10-24 21:39:27 +0200276 return NULL;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100277
Christopher Faulet89dc4992019-04-17 12:02:59 +0200278 /* wait for a full request */
279 smp->flags |= SMP_F_MAY_CHANGE;
280 return NULL;
281 }
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100282
Christopher Faulet89dc4992019-04-17 12:02:59 +0200283 /* OK we just got a valid HTTP mesage. We have to convert it
284 * into an HTX message.
285 */
286 if (unlikely(h1sl.rq.v.len == 0)) {
287 /* try to convert HTTP/0.9 requests to HTTP/1.0 */
288 if (h1sl.rq.meth != HTTP_METH_GET || !h1sl.rq.u.len)
Christopher Fauletef453ed2018-10-24 21:39:27 +0200289 return NULL;
Christopher Faulet89dc4992019-04-17 12:02:59 +0200290 h1sl.rq.v = ist("HTTP/1.0");
Christopher Fauletef453ed2018-10-24 21:39:27 +0200291 }
Christopher Faulet89dc4992019-04-17 12:02:59 +0200292
293 /* Set HTX start-line flags */
294 if (h1m.flags & H1_MF_VER_11)
295 flags |= HTX_SL_F_VER_11;
296 if (h1m.flags & H1_MF_XFER_ENC)
297 flags |= HTX_SL_F_XFER_ENC;
298 flags |= HTX_SL_F_XFER_LEN;
299 if (h1m.flags & H1_MF_CHNK)
300 flags |= HTX_SL_F_CHNK;
301 else if (h1m.flags & H1_MF_CLEN)
302 flags |= HTX_SL_F_CLEN;
303
Richard Russoc0968f52019-07-31 11:45:56 -0700304 htx = htx_from_buf(get_raw_htx_chunk());
Christopher Faulet89dc4992019-04-17 12:02:59 +0200305 sl = htx_add_stline(htx, HTX_BLK_REQ_SL, flags, h1sl.rq.m, h1sl.rq.u, h1sl.rq.v);
306 if (!sl || !htx_add_all_headers(htx, hdrs))
Christopher Fauletef453ed2018-10-24 21:39:27 +0200307 return NULL;
Willy Tarreauce9bbf52019-05-13 08:32:31 +0200308 sl->info.req.meth = h1sl.rq.meth;
Christopher Faulet89dc4992019-04-17 12:02:59 +0200309 }
310
311 /* OK we just got a valid HTTP message. If not already done by
312 * HTTP analyzers, we have some minor preparation to perform so
313 * that further checks can rely on HTTP tests.
314 */
315 if (sl && msg->msg_state < HTTP_MSG_BODY) {
316 if (!(chn->flags & CF_ISRESP)) {
317 txn->meth = sl->info.req.meth;
318 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
319 s->flags |= SF_REDIRECTABLE;
Christopher Fauletef453ed2018-10-24 21:39:27 +0200320 }
Christopher Faulet89dc4992019-04-17 12:02:59 +0200321 else
322 txn->status = sl->info.res.status;
323 if (sl->flags & HTX_SL_F_VER_11)
324 msg->flags |= HTTP_MSGF_VER_11;
Christopher Fauletef453ed2018-10-24 21:39:27 +0200325 }
326
327 /* everything's OK */
328 smp->data.u.sint = 1;
329 return htx;
330}
331
332/* This function ensures that the prerequisites for an L7 fetch are ready,
333 * which means that a request or response is ready. If some data is missing,
334 * a parsing attempt is made. This is useful in TCP-based ACLs which are able
Willy Tarreau79e57332018-10-02 16:01:16 +0200335 * to extract data from L7. If <req_vol> is non-null during a request prefetch,
336 * another test is made to ensure the required information is not gone.
337 *
338 * The function returns :
339 * 0 with SMP_F_MAY_CHANGE in the sample flags if some data is missing to
340 * decide whether or not an HTTP message is present ;
341 * 0 if the requested data cannot be fetched or if it is certain that
342 * we'll never have any HTTP message there ;
343 * 1 if an HTTP message is ready
344 */
345int smp_prefetch_http(struct proxy *px, struct stream *s, unsigned int opt,
Christopher Faulet89dc4992019-04-17 12:02:59 +0200346 struct channel *chn, struct sample *smp, int req_vol)
Willy Tarreau79e57332018-10-02 16:01:16 +0200347{
348 struct http_txn *txn;
349 struct http_msg *msg;
350
351 /* Note: it is possible that <s> is NULL when called before stream
352 * initialization (eg: tcp-request connection), so this function is the
353 * one responsible for guarding against this case for all HTTP users.
354 */
Christopher Faulet89dc4992019-04-17 12:02:59 +0200355 if (!s || !chn)
Willy Tarreau79e57332018-10-02 16:01:16 +0200356 return 0;
357
358 if (!s->txn) {
359 if (unlikely(!http_alloc_txn(s)))
360 return 0; /* not enough memory */
361 http_init_txn(s);
362 }
363 txn = s->txn;
Willy Tarreau79e57332018-10-02 16:01:16 +0200364 smp->data.type = SMP_T_BOOL;
365
Christopher Faulet89dc4992019-04-17 12:02:59 +0200366 if (chn->flags & CF_ISRESP) {
367 /* Check for a dependency on a response */
368 if (txn->rsp.msg_state < HTTP_MSG_BODY) {
369 smp->flags |= SMP_F_MAY_CHANGE;
370 return 0;
371 }
372 goto end;
373 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200374
Christopher Faulet89dc4992019-04-17 12:02:59 +0200375 /* Check for a dependency on a request */
376 msg = &txn->req;
Willy Tarreau79e57332018-10-02 16:01:16 +0200377
Christopher Faulet89dc4992019-04-17 12:02:59 +0200378 if (req_vol && (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) {
379 return 0; /* data might have moved and indexes changed */
380 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200381
Christopher Faulet89dc4992019-04-17 12:02:59 +0200382 /* If the buffer does not leave enough free space at the end, we must
383 * first realign it.
384 */
385 if (ci_head(chn) > b_orig(&chn->buf) &&
386 ci_head(chn) + ci_data(chn) > b_wrap(&chn->buf) - global.tune.maxrewrite)
387 channel_slow_realign(chn, trash.area);
Willy Tarreau79e57332018-10-02 16:01:16 +0200388
Christopher Faulet89dc4992019-04-17 12:02:59 +0200389 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
390 if (msg->msg_state == HTTP_MSG_ERROR)
391 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200392
Christopher Faulet89dc4992019-04-17 12:02:59 +0200393 /* Try to decode HTTP request */
394 if (likely(msg->next < ci_data(chn)))
395 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau79e57332018-10-02 16:01:16 +0200396
Christopher Faulet89dc4992019-04-17 12:02:59 +0200397 /* Still no valid request ? */
398 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
399 if ((msg->msg_state == HTTP_MSG_ERROR) ||
400 channel_full(chn, global.tune.maxrewrite)) {
Willy Tarreau79e57332018-10-02 16:01:16 +0200401 return 0;
Christopher Faulet89dc4992019-04-17 12:02:59 +0200402 }
403 /* wait for final state */
404 smp->flags |= SMP_F_MAY_CHANGE;
405 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200406 }
407
Christopher Faulet89dc4992019-04-17 12:02:59 +0200408 /* OK we just got a valid HTTP message. We have some minor
409 * preparation to perform so that further checks can rely
410 * on HTTP tests.
411 */
412
413 /* If the message was parsed but was too large, we must absolutely
414 * return an error so that it is not processed. At the moment this
415 * cannot happen, but if the parsers are to change in the future,
416 * we want this check to be maintained.
417 */
418 if (unlikely(ci_head(chn) + ci_data(chn) >
419 b_wrap(&chn->buf) - global.tune.maxrewrite)) {
420 msg->err_state = msg->msg_state;
421 msg->msg_state = HTTP_MSG_ERROR;
422 smp->data.u.sint = 1;
423 return 1;
Willy Tarreau79e57332018-10-02 16:01:16 +0200424 }
425
Christopher Faulet89dc4992019-04-17 12:02:59 +0200426 txn->meth = find_http_meth(ci_head(chn), msg->sl.rq.m_l);
427 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
428 s->flags |= SF_REDIRECTABLE;
429
430 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
Willy Tarreau79e57332018-10-02 16:01:16 +0200431 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200432 }
433
Christopher Faulet89dc4992019-04-17 12:02:59 +0200434 end:
Willy Tarreau79e57332018-10-02 16:01:16 +0200435 /* everything's OK */
436 smp->data.u.sint = 1;
437 return 1;
438}
439
440/* This function fetches the method of current HTTP request and stores
441 * it in the global pattern struct as a chunk. There are two possibilities :
442 * - if the method is known (not HTTP_METH_OTHER), its identifier is stored
443 * in <len> and <ptr> is NULL ;
444 * - if the method is unknown (HTTP_METH_OTHER), <ptr> points to the text and
445 * <len> to its length.
446 * This is intended to be used with pat_match_meth() only.
447 */
448static int smp_fetch_meth(const struct arg *args, struct sample *smp, const char *kw, void *private)
449{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200450 struct channel *chn = SMP_REQ_CHN(smp);
Willy Tarreau79e57332018-10-02 16:01:16 +0200451 int meth;
452 struct http_txn *txn;
453
Christopher Faulet46575cd2019-04-17 11:40:30 +0200454 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200455 /* HTX version */
Willy Tarreau91d0b602020-08-12 14:04:52 +0200456 txn = smp->strm->txn;
457 if (!txn)
Willy Tarreau79e57332018-10-02 16:01:16 +0200458 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200459
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200460 meth = txn->meth;
461 smp->data.type = SMP_T_METH;
462 smp->data.u.meth.meth = meth;
463 if (meth == HTTP_METH_OTHER) {
Willy Tarreau91d0b602020-08-12 14:04:52 +0200464 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100465 struct htx_sl *sl;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200466
Willy Tarreau91d0b602020-08-12 14:04:52 +0200467 htx = smp_prefetch_htx(smp, chn, 0);
468 if (!htx)
469 return 0;
470
Christopher Faulet89dc4992019-04-17 12:02:59 +0200471 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200472 /* ensure the indexes are not affected */
473 return 0;
Christopher Faulet89dc4992019-04-17 12:02:59 +0200474 }
Christopher Faulet297fbb42019-05-13 14:41:27 +0200475 sl = http_get_stline(htx);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200476 smp->flags |= SMP_F_CONST;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100477 smp->data.u.meth.str.area = HTX_SL_REQ_MPTR(sl);
478 smp->data.u.meth.str.data = HTX_SL_REQ_MLEN(sl);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200479 }
480 smp->flags |= SMP_F_VOL_1ST;
481 }
482 else {
483 /* LEGACY version */
Christopher Faulet89dc4992019-04-17 12:02:59 +0200484 CHECK_HTTP_MESSAGE_FIRST_PERM(chn);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200485
486 txn = smp->strm->txn;
487 meth = txn->meth;
488 smp->data.type = SMP_T_METH;
489 smp->data.u.meth.meth = meth;
490 if (meth == HTTP_METH_OTHER) {
Christopher Faulet89dc4992019-04-17 12:02:59 +0200491 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200492 /* ensure the indexes are not affected */
493 return 0;
Christopher Faulet89dc4992019-04-17 12:02:59 +0200494 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200495 smp->flags |= SMP_F_CONST;
496 smp->data.u.meth.str.data = txn->req.sl.rq.m_l;
497 smp->data.u.meth.str.area = ci_head(txn->req.chn);
498 }
499 smp->flags |= SMP_F_VOL_1ST;
Willy Tarreau79e57332018-10-02 16:01:16 +0200500 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200501 return 1;
502}
503
504static int smp_fetch_rqver(const struct arg *args, struct sample *smp, const char *kw, void *private)
505{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200506 struct channel *chn = SMP_REQ_CHN(smp);
Willy Tarreau79e57332018-10-02 16:01:16 +0200507 struct http_txn *txn;
508 char *ptr;
509 int len;
510
Christopher Faulet46575cd2019-04-17 11:40:30 +0200511 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200512 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +0200513 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100514 struct htx_sl *sl;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200515
516 if (!htx)
517 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200518
Christopher Faulet297fbb42019-05-13 14:41:27 +0200519 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100520 len = HTX_SL_REQ_VLEN(sl);
521 ptr = HTX_SL_REQ_VPTR(sl);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200522 }
523 else {
524 /* LEGACY version */
Christopher Faulet89dc4992019-04-17 12:02:59 +0200525 CHECK_HTTP_MESSAGE_FIRST(chn);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200526
527 txn = smp->strm->txn;
528 len = txn->req.sl.rq.v_l;
Christopher Faulet89dc4992019-04-17 12:02:59 +0200529 ptr = ci_head(chn) + txn->req.sl.rq.v;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200530 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200531
532 while ((len-- > 0) && (*ptr++ != '/'));
533 if (len <= 0)
534 return 0;
535
536 smp->data.type = SMP_T_STR;
537 smp->data.u.str.area = ptr;
538 smp->data.u.str.data = len;
539
540 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
541 return 1;
542}
543
544static int smp_fetch_stver(const struct arg *args, struct sample *smp, const char *kw, void *private)
545{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200546 struct channel *chn = SMP_RES_CHN(smp);
Willy Tarreau79e57332018-10-02 16:01:16 +0200547 struct http_txn *txn;
548 char *ptr;
549 int len;
550
Christopher Faulet46575cd2019-04-17 11:40:30 +0200551 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200552 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +0200553 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100554 struct htx_sl *sl;
Willy Tarreau79e57332018-10-02 16:01:16 +0200555
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200556 if (!htx)
557 return 0;
558
Christopher Faulet297fbb42019-05-13 14:41:27 +0200559 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100560 len = HTX_SL_RES_VLEN(sl);
561 ptr = HTX_SL_RES_VPTR(sl);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200562 }
563 else {
564 /* LEGACY version */
Christopher Faulet89dc4992019-04-17 12:02:59 +0200565 CHECK_HTTP_MESSAGE_FIRST(chn);
Willy Tarreau79e57332018-10-02 16:01:16 +0200566
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200567 txn = smp->strm->txn;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200568 len = txn->rsp.sl.st.v_l;
Christopher Faulet89dc4992019-04-17 12:02:59 +0200569 ptr = ci_head(chn);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200570 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200571
572 while ((len-- > 0) && (*ptr++ != '/'));
573 if (len <= 0)
574 return 0;
575
576 smp->data.type = SMP_T_STR;
577 smp->data.u.str.area = ptr;
578 smp->data.u.str.data = len;
579
580 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
581 return 1;
582}
583
584/* 3. Check on Status Code. We manipulate integers here. */
585static int smp_fetch_stcode(const struct arg *args, struct sample *smp, const char *kw, void *private)
586{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200587 struct channel *chn = SMP_RES_CHN(smp);
Willy Tarreau79e57332018-10-02 16:01:16 +0200588 struct http_txn *txn;
589 char *ptr;
590 int len;
591
Christopher Faulet46575cd2019-04-17 11:40:30 +0200592 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200593 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +0200594 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100595 struct htx_sl *sl;
Willy Tarreau79e57332018-10-02 16:01:16 +0200596
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200597 if (!htx)
598 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200599
Christopher Faulet297fbb42019-05-13 14:41:27 +0200600 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100601 len = HTX_SL_RES_CLEN(sl);
602 ptr = HTX_SL_RES_CPTR(sl);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200603 }
604 else {
605 /* LEGACY version */
Christopher Faulet89dc4992019-04-17 12:02:59 +0200606 CHECK_HTTP_MESSAGE_FIRST(chn);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200607
608 txn = smp->strm->txn;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200609 len = txn->rsp.sl.st.c_l;
Christopher Faulet89dc4992019-04-17 12:02:59 +0200610 ptr = ci_head(chn) + txn->rsp.sl.st.c;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200611 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200612
613 smp->data.type = SMP_T_SINT;
614 smp->data.u.sint = __strl2ui(ptr, len);
615 smp->flags = SMP_F_VOL_1ST;
616 return 1;
617}
618
619static int smp_fetch_uniqueid(const struct arg *args, struct sample *smp, const char *kw, void *private)
620{
621 if (LIST_ISEMPTY(&smp->sess->fe->format_unique_id))
622 return 0;
623
Willy Tarreauc1534622020-04-29 11:50:38 +0200624 if (!smp->strm)
625 return 0;
626
Willy Tarreau79e57332018-10-02 16:01:16 +0200627 if (!smp->strm->unique_id) {
628 if ((smp->strm->unique_id = pool_alloc(pool_head_uniqueid)) == NULL)
629 return 0;
630 smp->strm->unique_id[0] = '\0';
Tim Duesterhusea23a5c2020-02-26 16:20:49 +0100631 build_logline(smp->strm, smp->strm->unique_id,
632 UNIQUEID_LEN, &smp->sess->fe->format_unique_id);
Willy Tarreau79e57332018-10-02 16:01:16 +0200633 }
Tim Duesterhusea23a5c2020-02-26 16:20:49 +0100634 smp->data.u.str.data = strlen(smp->strm->unique_id);
Willy Tarreau79e57332018-10-02 16:01:16 +0200635 smp->data.type = SMP_T_STR;
636 smp->data.u.str.area = smp->strm->unique_id;
637 smp->flags = SMP_F_CONST;
638 return 1;
639}
640
641/* Returns a string block containing all headers including the
Joseph Herlant942eea32018-11-15 13:57:22 -0800642 * empty line which separes headers from the body. This is useful
643 * for some headers analysis.
Willy Tarreau79e57332018-10-02 16:01:16 +0200644 */
645static int smp_fetch_hdrs(const struct arg *args, struct sample *smp, const char *kw, void *private)
646{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200647 struct channel *chn = SMP_REQ_CHN(smp);
Willy Tarreau79e57332018-10-02 16:01:16 +0200648 struct http_txn *txn;
649
Christopher Faulet46575cd2019-04-17 11:40:30 +0200650 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200651 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +0200652 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200653 struct buffer *temp;
654 int32_t pos;
Willy Tarreau79e57332018-10-02 16:01:16 +0200655
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200656 if (!htx)
657 return 0;
658 temp = get_trash_chunk();
Christopher Fauleta3f15502019-05-13 15:27:23 +0200659 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200660 struct htx_blk *blk = htx_get_blk(htx, pos);
661 enum htx_blk_type type = htx_get_blk_type(blk);
Willy Tarreau79e57332018-10-02 16:01:16 +0200662
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200663 if (type == HTX_BLK_HDR) {
664 struct ist n = htx_get_blk_name(htx, blk);
665 struct ist v = htx_get_blk_value(htx, blk);
666
Christopher Fauletc59ff232018-12-03 13:58:44 +0100667 if (!htx_hdr_to_h1(n, v, temp))
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200668 return 0;
669 }
670 else if (type == HTX_BLK_EOH) {
671 if (!chunk_memcat(temp, "\r\n", 2))
672 return 0;
673 break;
674 }
675 }
676 smp->data.type = SMP_T_STR;
677 smp->data.u.str = *temp;
678
679 }
680 else {
681 /* LEGACY version */
682 struct http_msg *msg;
683 struct hdr_idx *idx;
684
Christopher Faulet89dc4992019-04-17 12:02:59 +0200685 CHECK_HTTP_MESSAGE_FIRST(chn);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200686
687 txn = smp->strm->txn;
688 idx = &txn->hdr_idx;
689 msg = &txn->req;
Willy Tarreau79e57332018-10-02 16:01:16 +0200690
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200691 smp->data.type = SMP_T_STR;
Christopher Faulet89dc4992019-04-17 12:02:59 +0200692 smp->data.u.str.area = ci_head(chn) + hdr_idx_first_pos(idx);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200693 smp->data.u.str.data = msg->eoh - hdr_idx_first_pos(idx) + 1 +
Christopher Faulet89dc4992019-04-17 12:02:59 +0200694 (ci_head(chn)[msg->eoh] == '\r');
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200695 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200696 return 1;
697}
698
699/* Returns the header request in a length/value encoded format.
700 * This is useful for exchanges with the SPOE.
701 *
702 * A "length value" is a multibyte code encoding numbers. It uses the
703 * SPOE format. The encoding is the following:
704 *
705 * Each couple "header name" / "header value" is composed
706 * like this:
707 * "length value" "header name bytes"
708 * "length value" "header value bytes"
709 * When the last header is reached, the header name and the header
710 * value are empty. Their length are 0
711 */
712static int smp_fetch_hdrs_bin(const struct arg *args, struct sample *smp, const char *kw, void *private)
713{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200714 struct channel *chn = SMP_REQ_CHN(smp);
Willy Tarreau79e57332018-10-02 16:01:16 +0200715 struct http_txn *txn;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200716 struct buffer *temp;
Willy Tarreau79e57332018-10-02 16:01:16 +0200717
Christopher Faulet46575cd2019-04-17 11:40:30 +0200718 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200719 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +0200720 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200721 struct buffer *temp;
722 char *p, *end;
723 int32_t pos;
724 int ret;
Willy Tarreau79e57332018-10-02 16:01:16 +0200725
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200726 if (!htx)
727 return 0;
728 temp = get_trash_chunk();
729 p = temp->area;
730 end = temp->area + temp->size;
Christopher Fauleta3f15502019-05-13 15:27:23 +0200731 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200732 struct htx_blk *blk = htx_get_blk(htx, pos);
733 enum htx_blk_type type = htx_get_blk_type(blk);
734 struct ist n, v;
Willy Tarreau79e57332018-10-02 16:01:16 +0200735
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200736 if (type == HTX_BLK_HDR) {
737 n = htx_get_blk_name(htx,blk);
738 v = htx_get_blk_value(htx, blk);
Willy Tarreau79e57332018-10-02 16:01:16 +0200739
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200740 /* encode the header name. */
741 ret = encode_varint(n.len, &p, end);
742 if (ret == -1)
743 return 0;
744 if (p + n.len > end)
745 return 0;
746 memcpy(p, n.ptr, n.len);
747 p += n.len;
Willy Tarreau79e57332018-10-02 16:01:16 +0200748
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200749 /* encode the header value. */
750 ret = encode_varint(v.len, &p, end);
751 if (ret == -1)
752 return 0;
753 if (p + v.len > end)
754 return 0;
755 memcpy(p, v.ptr, v.len);
756 p += v.len;
Willy Tarreau79e57332018-10-02 16:01:16 +0200757
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200758 }
759 else if (type == HTX_BLK_EOH) {
760 /* encode the end of the header list with empty
761 * header name and header value.
762 */
763 ret = encode_varint(0, &p, end);
764 if (ret == -1)
765 return 0;
766 ret = encode_varint(0, &p, end);
767 if (ret == -1)
768 return 0;
769 break;
770 }
771 }
772
773 /* Initialise sample data which will be filled. */
774 smp->data.type = SMP_T_BIN;
775 smp->data.u.str.area = temp->area;
776 smp->data.u.str.data = p - temp->area;
777 smp->data.u.str.size = temp->size;
778 }
779 else {
780 /* LEGACY version */
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200781 struct hdr_idx *idx;
782 const char *cur_ptr, *cur_next, *p;
783 int old_idx, cur_idx;
784 struct hdr_idx_elem *cur_hdr;
785 const char *hn, *hv;
786 int hnl, hvl;
787 int ret;
788 char *buf;
789 char *end;
790
Christopher Faulet89dc4992019-04-17 12:02:59 +0200791 CHECK_HTTP_MESSAGE_FIRST(chn);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200792
793 temp = get_trash_chunk();
794 buf = temp->area;
795 end = temp->area + temp->size;
796
797 txn = smp->strm->txn;
798 idx = &txn->hdr_idx;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200799
800 /* Build array of headers. */
801 old_idx = 0;
Christopher Faulet89dc4992019-04-17 12:02:59 +0200802 cur_next = ci_head(chn) + hdr_idx_first_pos(idx);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200803 while (1) {
804 cur_idx = idx->v[old_idx].next;
805 if (!cur_idx)
806 break;
807 old_idx = cur_idx;
Willy Tarreau79e57332018-10-02 16:01:16 +0200808
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200809 cur_hdr = &idx->v[cur_idx];
810 cur_ptr = cur_next;
811 cur_next = cur_ptr + cur_hdr->len + cur_hdr->cr + 1;
812
813 /* Now we have one full header at cur_ptr of len cur_hdr->len,
814 * and the next header starts at cur_next. We'll check
815 * this header in the list as well as against the default
816 * rule.
817 */
818
819 /* look for ': *'. */
820 hn = cur_ptr;
821 for (p = cur_ptr; p < cur_ptr + cur_hdr->len && *p != ':'; p++);
822 if (p >= cur_ptr+cur_hdr->len)
823 continue;
824 hnl = p - hn;
Willy Tarreau79e57332018-10-02 16:01:16 +0200825 p++;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200826 while (p < cur_ptr + cur_hdr->len && (*p == ' ' || *p == '\t'))
827 p++;
828 if (p >= cur_ptr + cur_hdr->len)
829 continue;
830 hv = p;
831 hvl = cur_ptr + cur_hdr->len-p;
Willy Tarreau79e57332018-10-02 16:01:16 +0200832
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200833 /* encode the header name. */
834 ret = encode_varint(hnl, &buf, end);
835 if (ret == -1)
836 return 0;
837 if (buf + hnl > end)
838 return 0;
839 memcpy(buf, hn, hnl);
840 buf += hnl;
841
842 /* encode and copy the value. */
843 ret = encode_varint(hvl, &buf, end);
844 if (ret == -1)
845 return 0;
846 if (buf + hvl > end)
847 return 0;
848 memcpy(buf, hv, hvl);
849 buf += hvl;
850 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200851
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200852 /* encode the end of the header list with empty
853 * header name and header value.
854 */
855 ret = encode_varint(0, &buf, end);
Willy Tarreau79e57332018-10-02 16:01:16 +0200856 if (ret == -1)
857 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200858 ret = encode_varint(0, &buf, end);
859 if (ret == -1)
Willy Tarreau79e57332018-10-02 16:01:16 +0200860 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200861
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200862 /* Initialise sample data which will be filled. */
863 smp->data.type = SMP_T_BIN;
864 smp->data.u.str.area = temp->area;
865 smp->data.u.str.data = buf - temp->area;
866 smp->data.u.str.size = temp->size;
867 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200868 return 1;
869}
870
871/* returns the longest available part of the body. This requires that the body
872 * has been waited for using http-buffer-request.
873 */
874static int smp_fetch_body(const struct arg *args, struct sample *smp, const char *kw, void *private)
875{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200876 struct channel *chn = SMP_REQ_CHN(smp);
Willy Tarreau79e57332018-10-02 16:01:16 +0200877 struct buffer *temp;
878
Christopher Faulet46575cd2019-04-17 11:40:30 +0200879 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200880 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +0200881 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200882 int32_t pos;
Willy Tarreau79e57332018-10-02 16:01:16 +0200883
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200884 if (!htx)
885 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200886
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200887 temp = get_trash_chunk();
Christopher Fauleta3f15502019-05-13 15:27:23 +0200888 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200889 struct htx_blk *blk = htx_get_blk(htx, pos);
890 enum htx_blk_type type = htx_get_blk_type(blk);
Willy Tarreau79e57332018-10-02 16:01:16 +0200891
Christopher Faulet54b5e212019-06-04 10:08:28 +0200892 if (type == HTX_BLK_EOM || type == HTX_BLK_TLR || type == HTX_BLK_EOT)
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200893 break;
894 if (type == HTX_BLK_DATA) {
Christopher Fauletc59ff232018-12-03 13:58:44 +0100895 if (!htx_data_to_h1(htx_get_blk_value(htx, blk), temp, 0))
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200896 return 0;
897 }
898 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200899
Willy Tarreau79e57332018-10-02 16:01:16 +0200900 smp->data.type = SMP_T_BIN;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200901 smp->data.u.str = *temp;
902 smp->flags = SMP_F_VOL_TEST;
Willy Tarreau79e57332018-10-02 16:01:16 +0200903 }
904 else {
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200905 /* LEGACY version */
906 struct http_msg *msg;
907 unsigned long len;
908 unsigned long block1;
909 char *body;
910
Christopher Faulet89dc4992019-04-17 12:02:59 +0200911 CHECK_HTTP_MESSAGE_FIRST(chn);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200912
Christopher Faulet89dc4992019-04-17 12:02:59 +0200913 msg = &smp->strm->txn->req;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200914 len = http_body_bytes(msg);
Christopher Faulet89dc4992019-04-17 12:02:59 +0200915 body = c_ptr(chn, -http_data_rewind(msg));
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200916
917 block1 = len;
Christopher Faulet89dc4992019-04-17 12:02:59 +0200918 if (block1 > b_wrap(&chn->buf) - body)
919 block1 = b_wrap(&chn->buf) - body;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200920
921 if (block1 == len) {
922 /* buffer is not wrapped (or empty) */
923 smp->data.type = SMP_T_BIN;
924 smp->data.u.str.area = body;
925 smp->data.u.str.data = len;
926 smp->flags = SMP_F_VOL_TEST | SMP_F_CONST;
927 }
928 else {
929 /* buffer is wrapped, we need to defragment it */
930 temp = get_trash_chunk();
931 memcpy(temp->area, body, block1);
Christopher Faulet89dc4992019-04-17 12:02:59 +0200932 memcpy(temp->area + block1, b_orig(&chn->buf), len - block1);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200933 smp->data.type = SMP_T_BIN;
934 smp->data.u.str.area = temp->area;
935 smp->data.u.str.data = len;
936 smp->flags = SMP_F_VOL_TEST;
937 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200938 }
939 return 1;
940}
941
942
943/* returns the available length of the body. This requires that the body
944 * has been waited for using http-buffer-request.
945 */
946static int smp_fetch_body_len(const struct arg *args, struct sample *smp, const char *kw, void *private)
947{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200948 struct channel *chn = SMP_REQ_CHN(smp);
949
Christopher Faulet46575cd2019-04-17 11:40:30 +0200950 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200951 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +0200952 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Dragan Dosen5a606682019-02-14 12:30:53 +0100953 int32_t pos;
Christopher Fauletc16317d2018-12-12 14:11:22 +0100954 unsigned long long len = 0;
955
956 if (!htx)
957 return 0;
958
Christopher Fauleta3f15502019-05-13 15:27:23 +0200959 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Dragan Dosen5a606682019-02-14 12:30:53 +0100960 struct htx_blk *blk = htx_get_blk(htx, pos);
961 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Fauletc16317d2018-12-12 14:11:22 +0100962
Christopher Faulet54b5e212019-06-04 10:08:28 +0200963 if (type == HTX_BLK_EOM || type == HTX_BLK_TLR || type == HTX_BLK_EOT)
Christopher Fauletc16317d2018-12-12 14:11:22 +0100964 break;
Dragan Dosen5a606682019-02-14 12:30:53 +0100965 if (type == HTX_BLK_DATA)
966 len += htx_get_blksz(blk);
Christopher Fauletc16317d2018-12-12 14:11:22 +0100967 }
968
969 smp->data.type = SMP_T_SINT;
970 smp->data.u.sint = len;
971
972 smp->flags = SMP_F_VOL_TEST;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200973 }
974 else {
975 /* LEGACY version */
976 struct http_msg *msg;
Willy Tarreau79e57332018-10-02 16:01:16 +0200977
Christopher Faulet89dc4992019-04-17 12:02:59 +0200978 CHECK_HTTP_MESSAGE_FIRST(chn);
Willy Tarreau79e57332018-10-02 16:01:16 +0200979
Christopher Faulet89dc4992019-04-17 12:02:59 +0200980 msg = &smp->strm->txn->req;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200981 smp->data.type = SMP_T_SINT;
982 smp->data.u.sint = http_body_bytes(msg);
Willy Tarreau79e57332018-10-02 16:01:16 +0200983
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200984 smp->flags = SMP_F_VOL_TEST;
985 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200986 return 1;
987}
988
989
990/* returns the advertised length of the body, or the advertised size of the
991 * chunks available in the buffer. This requires that the body has been waited
992 * for using http-buffer-request.
993 */
994static int smp_fetch_body_size(const struct arg *args, struct sample *smp, const char *kw, void *private)
995{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200996 struct channel *chn = SMP_REQ_CHN(smp);
997
Christopher Faulet46575cd2019-04-17 11:40:30 +0200998 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200999 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +02001000 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Dragan Dosen5a606682019-02-14 12:30:53 +01001001 int32_t pos;
Christopher Fauletc16317d2018-12-12 14:11:22 +01001002 unsigned long long len = 0;
1003
1004 if (!htx)
1005 return 0;
1006
Christopher Fauleta3f15502019-05-13 15:27:23 +02001007 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Dragan Dosen5a606682019-02-14 12:30:53 +01001008 struct htx_blk *blk = htx_get_blk(htx, pos);
1009 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Fauletc16317d2018-12-12 14:11:22 +01001010
Christopher Faulet54b5e212019-06-04 10:08:28 +02001011 if (type == HTX_BLK_EOM || type == HTX_BLK_TLR || type == HTX_BLK_EOT)
Christopher Fauletc16317d2018-12-12 14:11:22 +01001012 break;
Dragan Dosen5a606682019-02-14 12:30:53 +01001013 if (type == HTX_BLK_DATA)
1014 len += htx_get_blksz(blk);
Christopher Fauletc16317d2018-12-12 14:11:22 +01001015 }
1016 if (htx->extra != ULLONG_MAX)
1017 len += htx->extra;
1018
1019 smp->data.type = SMP_T_SINT;
1020 smp->data.u.sint = len;
1021
1022 smp->flags = SMP_F_VOL_TEST;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001023 }
1024 else {
1025 /* LEGACY version */
1026 struct http_msg *msg;
Willy Tarreau79e57332018-10-02 16:01:16 +02001027
Christopher Faulet89dc4992019-04-17 12:02:59 +02001028 CHECK_HTTP_MESSAGE_FIRST(chn);
Willy Tarreau79e57332018-10-02 16:01:16 +02001029
Christopher Faulet89dc4992019-04-17 12:02:59 +02001030 msg = &smp->strm->txn->req;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001031 smp->data.type = SMP_T_SINT;
1032 smp->data.u.sint = msg->body_len;
Willy Tarreau79e57332018-10-02 16:01:16 +02001033
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001034 smp->flags = SMP_F_VOL_TEST;
1035 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001036 return 1;
1037}
1038
1039
1040/* 4. Check on URL/URI. A pointer to the URI is stored. */
1041static int smp_fetch_url(const struct arg *args, struct sample *smp, const char *kw, void *private)
1042{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001043 struct channel *chn = SMP_REQ_CHN(smp);
Willy Tarreau79e57332018-10-02 16:01:16 +02001044 struct http_txn *txn;
1045
Christopher Faulet46575cd2019-04-17 11:40:30 +02001046 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001047 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +02001048 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001049 struct htx_sl *sl;
Willy Tarreau79e57332018-10-02 16:01:16 +02001050
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001051 if (!htx)
1052 return 0;
Christopher Faulet297fbb42019-05-13 14:41:27 +02001053 sl = http_get_stline(htx);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001054 smp->data.type = SMP_T_STR;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001055 smp->data.u.str.area = HTX_SL_REQ_UPTR(sl);
1056 smp->data.u.str.data = HTX_SL_REQ_ULEN(sl);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001057 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
1058 }
1059 else {
1060 /* LEGACY version */
Christopher Faulet89dc4992019-04-17 12:02:59 +02001061 CHECK_HTTP_MESSAGE_FIRST(chn);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001062
1063 txn = smp->strm->txn;
1064 smp->data.type = SMP_T_STR;
1065 smp->data.u.str.data = txn->req.sl.rq.u_l;
Christopher Faulet89dc4992019-04-17 12:02:59 +02001066 smp->data.u.str.area = ci_head(chn) + txn->req.sl.rq.u;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001067 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
1068 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001069 return 1;
1070}
1071
1072static int smp_fetch_url_ip(const struct arg *args, struct sample *smp, const char *kw, void *private)
1073{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001074 struct channel *chn = SMP_REQ_CHN(smp);
Willy Tarreau79e57332018-10-02 16:01:16 +02001075 struct http_txn *txn;
1076 struct sockaddr_storage addr;
1077
Christopher Faulet46575cd2019-04-17 11:40:30 +02001078 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001079 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +02001080 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001081 struct htx_sl *sl;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001082
1083 if (!htx)
1084 return 0;
Christopher Faulet297fbb42019-05-13 14:41:27 +02001085 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001086 url2sa(HTX_SL_REQ_UPTR(sl), HTX_SL_REQ_ULEN(sl), &addr, NULL);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001087 }
1088 else {
1089 /* LEGACY version */
Christopher Faulet89dc4992019-04-17 12:02:59 +02001090 CHECK_HTTP_MESSAGE_FIRST(chn);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001091
1092 txn = smp->strm->txn;
Christopher Faulet89dc4992019-04-17 12:02:59 +02001093 url2sa(ci_head(chn) + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &addr, NULL);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001094 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001095
Willy Tarreau79e57332018-10-02 16:01:16 +02001096 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
1097 return 0;
1098
1099 smp->data.type = SMP_T_IPV4;
1100 smp->data.u.ipv4 = ((struct sockaddr_in *)&addr)->sin_addr;
1101 smp->flags = 0;
1102 return 1;
1103}
1104
1105static int smp_fetch_url_port(const struct arg *args, struct sample *smp, const char *kw, void *private)
1106{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001107 struct channel *chn = SMP_REQ_CHN(smp);
Willy Tarreau79e57332018-10-02 16:01:16 +02001108 struct http_txn *txn;
1109 struct sockaddr_storage addr;
1110
Christopher Faulet46575cd2019-04-17 11:40:30 +02001111 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001112 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +02001113 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001114 struct htx_sl *sl;
Willy Tarreau79e57332018-10-02 16:01:16 +02001115
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001116 if (!htx)
1117 return 0;
Christopher Faulet297fbb42019-05-13 14:41:27 +02001118 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001119 url2sa(HTX_SL_REQ_UPTR(sl), HTX_SL_REQ_ULEN(sl), &addr, NULL);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001120 }
1121 else {
1122 /* LEGACY version */
Christopher Faulet89dc4992019-04-17 12:02:59 +02001123 CHECK_HTTP_MESSAGE_FIRST(chn);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001124
1125 txn = smp->strm->txn;
Christopher Faulet89dc4992019-04-17 12:02:59 +02001126 url2sa(ci_head(chn) + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &addr, NULL);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001127 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001128 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
1129 return 0;
1130
1131 smp->data.type = SMP_T_SINT;
1132 smp->data.u.sint = ntohs(((struct sockaddr_in *)&addr)->sin_port);
1133 smp->flags = 0;
1134 return 1;
1135}
1136
1137/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
1138 * Accepts an optional argument of type string containing the header field name,
1139 * and an optional argument of type signed or unsigned integer to request an
1140 * explicit occurrence of the header. Note that in the event of a missing name,
1141 * headers are considered from the first one. It does not stop on commas and
1142 * returns full lines instead (useful for User-Agent or Date for example).
1143 */
1144static int smp_fetch_fhdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
1145{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001146 /* possible keywords: req.fhdr, res.fhdr */
1147 struct channel *chn = ((kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Willy Tarreau79e57332018-10-02 16:01:16 +02001148 int occ = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001149
Christopher Faulet46575cd2019-04-17 11:40:30 +02001150 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001151 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +02001152 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001153 struct http_hdr_ctx *ctx = smp->ctx.a[0];
1154 struct ist name;
Willy Tarreau79e57332018-10-02 16:01:16 +02001155
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001156 if (!ctx) {
1157 /* first call */
1158 ctx = &static_http_hdr_ctx;
1159 ctx->blk = NULL;
1160 smp->ctx.a[0] = ctx;
1161 }
1162
1163 if (args) {
1164 if (args[0].type != ARGT_STR)
1165 return 0;
1166 name.ptr = args[0].data.str.area;
1167 name.len = args[0].data.str.data;
1168
1169 if (args[1].type == ARGT_SINT)
1170 occ = args[1].data.sint;
1171 }
1172
1173 if (!htx)
Willy Tarreau79e57332018-10-02 16:01:16 +02001174 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001175
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001176 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
1177 /* search for header from the beginning */
1178 ctx->blk = NULL;
1179
1180 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
1181 /* no explicit occurrence and single fetch => last header by default */
1182 occ = -1;
1183
1184 if (!occ)
1185 /* prepare to report multiple occurrences for ACL fetches */
1186 smp->flags |= SMP_F_NOT_LAST;
1187
1188 smp->data.type = SMP_T_STR;
1189 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
1190 if (http_get_htx_fhdr(htx, name, occ, ctx, &smp->data.u.str.area, &smp->data.u.str.data))
1191 return 1;
Willy Tarreau79e57332018-10-02 16:01:16 +02001192 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001193 else {
1194 /* LEGACY version */
1195 struct hdr_idx *idx;
1196 struct hdr_ctx *ctx = smp->ctx.a[0];
1197 const struct http_msg *msg;
1198 const char *name_str = NULL;
1199 int name_len = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001200
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001201 if (!ctx) {
1202 /* first call */
1203 ctx = &static_hdr_ctx;
1204 ctx->idx = 0;
1205 smp->ctx.a[0] = ctx;
1206 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001207
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001208 if (args) {
1209 if (args[0].type != ARGT_STR)
1210 return 0;
1211 name_str = args[0].data.str.area;
1212 name_len = args[0].data.str.data;
Willy Tarreau79e57332018-10-02 16:01:16 +02001213
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001214 if (args[1].type == ARGT_SINT)
1215 occ = args[1].data.sint;
1216 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001217
Christopher Faulet89dc4992019-04-17 12:02:59 +02001218 CHECK_HTTP_MESSAGE_FIRST(chn);
Willy Tarreau79e57332018-10-02 16:01:16 +02001219
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001220 idx = &smp->strm->txn->hdr_idx;
Christopher Faulet89dc4992019-04-17 12:02:59 +02001221 msg = (!(chn->flags & CF_ISRESP) ? &smp->strm->txn->req : &smp->strm->txn->rsp);
Willy Tarreau79e57332018-10-02 16:01:16 +02001222
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001223 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
1224 /* search for header from the beginning */
1225 ctx->idx = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001226
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001227 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
1228 /* no explicit occurrence and single fetch => last header by default */
1229 occ = -1;
1230
1231 if (!occ)
1232 /* prepare to report multiple occurrences for ACL fetches */
1233 smp->flags |= SMP_F_NOT_LAST;
1234
1235 smp->data.type = SMP_T_STR;
1236 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
1237 if (http_get_fhdr(msg, name_str, name_len, idx, occ, ctx, &smp->data.u.str.area, &smp->data.u.str.data))
1238 return 1;
1239 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001240 smp->flags &= ~SMP_F_NOT_LAST;
1241 return 0;
1242}
1243
1244/* 6. Check on HTTP header count. The number of occurrences is returned.
1245 * Accepts exactly 1 argument of type string. It does not stop on commas and
1246 * returns full lines instead (useful for User-Agent or Date for example).
1247 */
1248static int smp_fetch_fhdr_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
1249{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001250 /* possible keywords: req.fhdr_cnt, res.fhdr_cnt */
1251 struct channel *chn = ((kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Willy Tarreau79e57332018-10-02 16:01:16 +02001252 int cnt;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001253
Christopher Faulet46575cd2019-04-17 11:40:30 +02001254 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001255 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +02001256 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001257 struct http_hdr_ctx ctx;
1258 struct ist name;
1259
1260 if (!htx)
1261 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001262
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001263 if (args && args->type == ARGT_STR) {
1264 name.ptr = args->data.str.area;
1265 name.len = args->data.str.data;
Olivier Houcharde2c78cd2018-11-21 13:49:48 +01001266 } else {
1267 name.ptr = NULL;
1268 name.len = 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001269 }
1270
1271 ctx.blk = NULL;
1272 cnt = 0;
1273 while (http_find_header(htx, name, &ctx, 1))
1274 cnt++;
Willy Tarreau79e57332018-10-02 16:01:16 +02001275 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001276 else {
1277 /* LEGACY version */
1278 struct hdr_idx *idx;
1279 struct hdr_ctx ctx;
1280 const struct http_msg *msg;
1281 const char *name = NULL;
1282 int len = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001283
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001284 if (args && args->type == ARGT_STR) {
1285 name = args->data.str.area;
1286 len = args->data.str.data;
1287 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001288
Christopher Faulet89dc4992019-04-17 12:02:59 +02001289 CHECK_HTTP_MESSAGE_FIRST(chn);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001290
1291 idx = &smp->strm->txn->hdr_idx;
Christopher Faulet89dc4992019-04-17 12:02:59 +02001292 msg = (!(chn->flags & CF_ISRESP) ? &smp->strm->txn->req : &smp->strm->txn->rsp);
Willy Tarreau79e57332018-10-02 16:01:16 +02001293
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001294 ctx.idx = 0;
1295 cnt = 0;
1296 while (http_find_full_header2(name, len, ci_head(msg->chn), idx, &ctx))
1297 cnt++;
1298 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001299
1300 smp->data.type = SMP_T_SINT;
1301 smp->data.u.sint = cnt;
1302 smp->flags = SMP_F_VOL_HDR;
1303 return 1;
1304}
1305
1306static int smp_fetch_hdr_names(const struct arg *args, struct sample *smp, const char *kw, void *private)
1307{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001308 /* possible keywords: req.hdr_names, res.hdr_names */
1309 struct channel *chn = ((kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Willy Tarreau79e57332018-10-02 16:01:16 +02001310 struct buffer *temp;
1311 char del = ',';
1312
Christopher Faulet46575cd2019-04-17 11:40:30 +02001313 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001314 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +02001315 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001316 int32_t pos;
Willy Tarreau79e57332018-10-02 16:01:16 +02001317
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001318 if (!htx)
1319 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001320
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001321 if (args && args->type == ARGT_STR)
1322 del = *args[0].data.str.area;
Willy Tarreau79e57332018-10-02 16:01:16 +02001323
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001324 temp = get_trash_chunk();
Christopher Fauleta3f15502019-05-13 15:27:23 +02001325 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001326 struct htx_blk *blk = htx_get_blk(htx, pos);
1327 enum htx_blk_type type = htx_get_blk_type(blk);
1328 struct ist n;
Willy Tarreau79e57332018-10-02 16:01:16 +02001329
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001330 if (type == HTX_BLK_EOH)
1331 break;
1332 if (type != HTX_BLK_HDR)
1333 continue;
1334 n = htx_get_blk_name(htx, blk);
1335
1336 if (temp->data)
1337 temp->area[temp->data++] = del;
1338 chunk_memcat(temp, n.ptr, n.len);
1339 }
1340 }
1341 else {
1342 /* LEGACY version */
1343 struct hdr_idx *idx;
1344 struct hdr_ctx ctx;
1345 const struct http_msg *msg;
1346
1347 if (args && args->type == ARGT_STR)
1348 del = *args[0].data.str.area;
1349
Christopher Faulet89dc4992019-04-17 12:02:59 +02001350 CHECK_HTTP_MESSAGE_FIRST(chn);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001351
1352 idx = &smp->strm->txn->hdr_idx;
Christopher Faulet89dc4992019-04-17 12:02:59 +02001353 msg = (!(chn->flags & CF_ISRESP) ? &smp->strm->txn->req : &smp->strm->txn->rsp);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001354
1355 temp = get_trash_chunk();
1356
1357 ctx.idx = 0;
1358 while (http_find_next_header(ci_head(msg->chn), idx, &ctx)) {
1359 if (temp->data)
1360 temp->area[temp->data++] = del;
1361 memcpy(temp->area + temp->data, ctx.line, ctx.del);
1362 temp->data += ctx.del;
1363 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001364 }
1365
1366 smp->data.type = SMP_T_STR;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001367 smp->data.u.str = *temp;
Willy Tarreau79e57332018-10-02 16:01:16 +02001368 smp->flags = SMP_F_VOL_HDR;
1369 return 1;
1370}
1371
1372/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
1373 * Accepts an optional argument of type string containing the header field name,
1374 * and an optional argument of type signed or unsigned integer to request an
1375 * explicit occurrence of the header. Note that in the event of a missing name,
1376 * headers are considered from the first one.
1377 */
1378static int smp_fetch_hdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
1379{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001380 /* possible keywords: req.hdr / hdr, res.hdr / shdr */
1381 struct channel *chn = ((kw[0] == 'h' || kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Willy Tarreau79e57332018-10-02 16:01:16 +02001382 int occ = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001383
Christopher Faulet46575cd2019-04-17 11:40:30 +02001384 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001385 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +02001386 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001387 struct http_hdr_ctx *ctx = smp->ctx.a[0];
1388 struct ist name;
1389
1390 if (!ctx) {
1391 /* first call */
1392 ctx = &static_http_hdr_ctx;
1393 ctx->blk = NULL;
1394 smp->ctx.a[0] = ctx;
1395 }
1396
1397 if (args) {
1398 if (args[0].type != ARGT_STR)
1399 return 0;
1400 name.ptr = args[0].data.str.area;
1401 name.len = args[0].data.str.data;
1402
1403 if (args[1].type == ARGT_SINT)
1404 occ = args[1].data.sint;
1405 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001406
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001407 if (!htx)
Willy Tarreau79e57332018-10-02 16:01:16 +02001408 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001409
1410 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
1411 /* search for header from the beginning */
1412 ctx->blk = NULL;
1413
1414 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
1415 /* no explicit occurrence and single fetch => last header by default */
1416 occ = -1;
1417
1418 if (!occ)
1419 /* prepare to report multiple occurrences for ACL fetches */
1420 smp->flags |= SMP_F_NOT_LAST;
Willy Tarreau79e57332018-10-02 16:01:16 +02001421
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001422 smp->data.type = SMP_T_STR;
1423 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
1424 if (http_get_htx_hdr(htx, name, occ, ctx, &smp->data.u.str.area, &smp->data.u.str.data))
1425 return 1;
Willy Tarreau79e57332018-10-02 16:01:16 +02001426 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001427 else {
1428 /* LEGACY version */
1429 struct hdr_idx *idx;
1430 struct hdr_ctx *ctx = smp->ctx.a[0];
1431 const struct http_msg *msg;
1432 const char *name_str = NULL;
1433 int name_len = 0;
1434
1435 if (!ctx) {
1436 /* first call */
1437 ctx = &static_hdr_ctx;
1438 ctx->idx = 0;
1439 smp->ctx.a[0] = ctx;
1440 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001441
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001442 if (args) {
1443 if (args[0].type != ARGT_STR)
1444 return 0;
1445 name_str = args[0].data.str.area;
1446 name_len = args[0].data.str.data;
Willy Tarreau79e57332018-10-02 16:01:16 +02001447
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001448 if (args[1].type == ARGT_SINT)
1449 occ = args[1].data.sint;
1450 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001451
Christopher Faulet89dc4992019-04-17 12:02:59 +02001452 CHECK_HTTP_MESSAGE_FIRST(chn);
Willy Tarreau79e57332018-10-02 16:01:16 +02001453
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001454 idx = &smp->strm->txn->hdr_idx;
Christopher Faulet89dc4992019-04-17 12:02:59 +02001455 msg = (!(chn->flags & CF_ISRESP) ? &smp->strm->txn->req : &smp->strm->txn->rsp);
Willy Tarreau79e57332018-10-02 16:01:16 +02001456
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001457 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
1458 /* search for header from the beginning */
1459 ctx->idx = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001460
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001461 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
1462 /* no explicit occurrence and single fetch => last header by default */
1463 occ = -1;
1464
1465 if (!occ)
1466 /* prepare to report multiple occurrences for ACL fetches */
1467 smp->flags |= SMP_F_NOT_LAST;
1468
1469 smp->data.type = SMP_T_STR;
1470 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
1471 if (http_get_hdr(msg, name_str, name_len, idx, occ, ctx, &smp->data.u.str.area, &smp->data.u.str.data))
1472 return 1;
1473 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001474
1475 smp->flags &= ~SMP_F_NOT_LAST;
1476 return 0;
1477}
1478
Christopher Fauletc1f40dd2019-05-16 10:07:30 +02001479/* Same than smp_fetch_hdr() but only relies on the sample direction to choose
1480 * the right channel. So instead of duplicating the code, we just change the
1481 * keyword and then fallback on smp_fetch_hdr().
1482 */
1483static int smp_fetch_chn_hdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
1484{
1485 kw = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ ? "req.hdr" : "res.hdr");
1486 return smp_fetch_hdr(args, smp, kw, private);
1487}
1488
Willy Tarreau79e57332018-10-02 16:01:16 +02001489/* 6. Check on HTTP header count. The number of occurrences is returned.
1490 * Accepts exactly 1 argument of type string.
1491 */
1492static int smp_fetch_hdr_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
1493{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001494 /* possible keywords: req.hdr_cnt / hdr_cnt, res.hdr_cnt / shdr_cnt */
1495 struct channel *chn = ((kw[0] == 'h' || kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Willy Tarreau79e57332018-10-02 16:01:16 +02001496 int cnt;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001497
Christopher Faulet46575cd2019-04-17 11:40:30 +02001498 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001499 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +02001500 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001501 struct http_hdr_ctx ctx;
1502 struct ist name;
1503
1504 if (!htx)
1505 return 0;
1506
1507 if (args && args->type == ARGT_STR) {
1508 name.ptr = args->data.str.area;
1509 name.len = args->data.str.data;
Olivier Houcharde2c78cd2018-11-21 13:49:48 +01001510 } else {
1511 name.ptr = NULL;
1512 name.len = 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001513 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001514
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001515 ctx.blk = NULL;
1516 cnt = 0;
1517 while (http_find_header(htx, name, &ctx, 0))
1518 cnt++;
Willy Tarreau79e57332018-10-02 16:01:16 +02001519 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001520 else {
1521 /* LEGACY version */
1522 struct hdr_idx *idx;
1523 struct hdr_ctx ctx;
1524 const struct http_msg *msg;
1525 const char *name = NULL;
1526 int len = 0;
1527
1528 if (args && args->type == ARGT_STR) {
1529 name = args->data.str.area;
1530 len = args->data.str.data;
1531 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001532
Christopher Faulet89dc4992019-04-17 12:02:59 +02001533 CHECK_HTTP_MESSAGE_FIRST(chn);
Willy Tarreau79e57332018-10-02 16:01:16 +02001534
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001535 idx = &smp->strm->txn->hdr_idx;
Christopher Faulet89dc4992019-04-17 12:02:59 +02001536 msg = (!(chn->flags & CF_ISRESP) ? &smp->strm->txn->req : &smp->strm->txn->rsp);
Willy Tarreau79e57332018-10-02 16:01:16 +02001537
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001538 ctx.idx = 0;
1539 cnt = 0;
1540 while (http_find_header2(name, len, ci_head(msg->chn), idx, &ctx))
1541 cnt++;
1542 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001543
1544 smp->data.type = SMP_T_SINT;
1545 smp->data.u.sint = cnt;
1546 smp->flags = SMP_F_VOL_HDR;
1547 return 1;
1548}
1549
1550/* Fetch an HTTP header's integer value. The integer value is returned. It
1551 * takes a mandatory argument of type string and an optional one of type int
1552 * to designate a specific occurrence. It returns an unsigned integer, which
1553 * may or may not be appropriate for everything.
1554 */
1555static int smp_fetch_hdr_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
1556{
1557 int ret = smp_fetch_hdr(args, smp, kw, private);
1558
1559 if (ret > 0) {
1560 smp->data.type = SMP_T_SINT;
1561 smp->data.u.sint = strl2ic(smp->data.u.str.area,
1562 smp->data.u.str.data);
1563 }
1564
1565 return ret;
1566}
1567
1568/* Fetch an HTTP header's IP value. takes a mandatory argument of type string
1569 * and an optional one of type int to designate a specific occurrence.
1570 * It returns an IPv4 or IPv6 address.
1571 */
1572static int smp_fetch_hdr_ip(const struct arg *args, struct sample *smp, const char *kw, void *private)
1573{
1574 int ret;
Tim Duesterhus8ba978b2020-06-26 15:44:48 +02001575 struct buffer *temp = get_trash_chunk();
Willy Tarreau79e57332018-10-02 16:01:16 +02001576
1577 while ((ret = smp_fetch_hdr(args, smp, kw, private)) > 0) {
Tim Duesterhus8ba978b2020-06-26 15:44:48 +02001578 if (smp->data.u.str.data < temp->size - 1) {
1579 memcpy(temp->area, smp->data.u.str.area,
1580 smp->data.u.str.data);
1581 temp->area[smp->data.u.str.data] = '\0';
1582 if (url2ipv4((char *) temp->area, &smp->data.u.ipv4)) {
1583 smp->data.type = SMP_T_IPV4;
1584 break;
1585 } else if (inet_pton(AF_INET6, temp->area, &smp->data.u.ipv6)) {
1586 smp->data.type = SMP_T_IPV6;
1587 break;
Willy Tarreau79e57332018-10-02 16:01:16 +02001588 }
1589 }
1590
1591 /* if the header doesn't match an IP address, fetch next one */
1592 if (!(smp->flags & SMP_F_NOT_LAST))
1593 return 0;
1594 }
1595 return ret;
1596}
1597
1598/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
1599 * the first '/' after the possible hostname, and ends before the possible '?'.
1600 */
1601static int smp_fetch_path(const struct arg *args, struct sample *smp, const char *kw, void *private)
1602{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001603 struct channel *chn = SMP_REQ_CHN(smp);
1604
Christopher Faulet46575cd2019-04-17 11:40:30 +02001605 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001606 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +02001607 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001608 struct htx_sl *sl;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001609 struct ist path;
1610 size_t len;
Willy Tarreau79e57332018-10-02 16:01:16 +02001611
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001612 if (!htx)
1613 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001614
Christopher Faulet297fbb42019-05-13 14:41:27 +02001615 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001616 path = http_get_path(htx_sl_req_uri(sl));
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001617 if (!path.ptr)
1618 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001619
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001620 for (len = 0; len < path.len && *(path.ptr + len) != '?'; len++)
Dragan Dosen8861e1c2019-02-12 19:50:31 +01001621 ;
Willy Tarreau79e57332018-10-02 16:01:16 +02001622
Dragan Dosen8861e1c2019-02-12 19:50:31 +01001623 /* OK, we got the '/' ! */
1624 smp->data.type = SMP_T_STR;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001625 smp->data.u.str.area = path.ptr;
1626 smp->data.u.str.data = len;
1627 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
1628 }
1629 else {
1630 struct http_txn *txn;
1631 char *ptr, *end;
Willy Tarreau79e57332018-10-02 16:01:16 +02001632
Christopher Faulet89dc4992019-04-17 12:02:59 +02001633 CHECK_HTTP_MESSAGE_FIRST(chn);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001634
1635 txn = smp->strm->txn;
Christopher Faulet89dc4992019-04-17 12:02:59 +02001636 end = ci_head(chn) + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001637 ptr = http_txn_get_path(txn);
1638 if (!ptr)
1639 return 0;
1640
1641 /* OK, we got the '/' ! */
1642 smp->data.type = SMP_T_STR;
1643 smp->data.u.str.area = ptr;
1644
1645 while (ptr < end && *ptr != '?')
1646 ptr++;
1647
1648 smp->data.u.str.data = ptr - smp->data.u.str.area;
1649 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
1650 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001651 return 1;
1652}
1653
1654/* This produces a concatenation of the first occurrence of the Host header
1655 * followed by the path component if it begins with a slash ('/'). This means
1656 * that '*' will not be added, resulting in exactly the first Host entry.
1657 * If no Host header is found, then the path is returned as-is. The returned
1658 * value is stored in the trash so it does not need to be marked constant.
1659 * The returned sample is of type string.
1660 */
1661static int smp_fetch_base(const struct arg *args, struct sample *smp, const char *kw, void *private)
1662{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001663 struct channel *chn = SMP_REQ_CHN(smp);
Willy Tarreau79e57332018-10-02 16:01:16 +02001664 struct buffer *temp;
1665
Christopher Faulet46575cd2019-04-17 11:40:30 +02001666 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001667 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +02001668 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001669 struct htx_sl *sl;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001670 struct http_hdr_ctx ctx;
1671 struct ist path;
Willy Tarreau79e57332018-10-02 16:01:16 +02001672
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001673 if (!htx)
1674 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001675
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001676 ctx.blk = NULL;
1677 if (!http_find_header(htx, ist("Host"), &ctx, 0) || !ctx.value.len)
1678 return smp_fetch_path(args, smp, kw, private);
Willy Tarreau79e57332018-10-02 16:01:16 +02001679
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001680 /* OK we have the header value in ctx.value */
1681 temp = get_trash_chunk();
1682 chunk_memcat(temp, ctx.value.ptr, ctx.value.len);
1683
1684 /* now retrieve the path */
Christopher Faulet297fbb42019-05-13 14:41:27 +02001685 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001686 path = http_get_path(htx_sl_req_uri(sl));
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001687 if (path.ptr) {
1688 size_t len;
1689
Dragan Dosen8861e1c2019-02-12 19:50:31 +01001690 for (len = 0; len < path.len && *(path.ptr + len) != '?'; len++)
1691 ;
1692
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001693 if (len && *(path.ptr) == '/')
1694 chunk_memcat(temp, path.ptr, len);
1695 }
1696
1697 smp->data.type = SMP_T_STR;
1698 smp->data.u.str = *temp;
1699 }
1700 else {
1701 /* LEGACY version */
1702 struct http_txn *txn;
1703 char *ptr, *end, *beg;
1704 struct hdr_ctx ctx;
1705
Christopher Faulet89dc4992019-04-17 12:02:59 +02001706 CHECK_HTTP_MESSAGE_FIRST(chn);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001707
1708 txn = smp->strm->txn;
1709 ctx.idx = 0;
Christopher Faulet89dc4992019-04-17 12:02:59 +02001710 if (!http_find_header2("Host", 4, ci_head(chn), &txn->hdr_idx, &ctx) || !ctx.vlen)
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001711 return smp_fetch_path(args, smp, kw, private);
1712
1713 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
1714 temp = get_trash_chunk();
1715 memcpy(temp->area, ctx.line + ctx.val, ctx.vlen);
1716 smp->data.type = SMP_T_STR;
1717 smp->data.u.str.area = temp->area;
1718 smp->data.u.str.data = ctx.vlen;
Willy Tarreau79e57332018-10-02 16:01:16 +02001719
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001720 /* now retrieve the path */
Christopher Faulet89dc4992019-04-17 12:02:59 +02001721 end = ci_head(chn) + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001722 beg = http_txn_get_path(txn);
1723 if (!beg)
1724 beg = end;
Willy Tarreau79e57332018-10-02 16:01:16 +02001725
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001726 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
1727
1728 if (beg < ptr && *beg == '/') {
1729 memcpy(smp->data.u.str.area + smp->data.u.str.data, beg,
1730 ptr - beg);
1731 smp->data.u.str.data += ptr - beg;
1732 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001733 }
1734
1735 smp->flags = SMP_F_VOL_1ST;
1736 return 1;
1737}
1738
1739/* This produces a 32-bit hash of the concatenation of the first occurrence of
1740 * the Host header followed by the path component if it begins with a slash ('/').
1741 * This means that '*' will not be added, resulting in exactly the first Host
1742 * entry. If no Host header is found, then the path is used. The resulting value
1743 * is hashed using the path hash followed by a full avalanche hash and provides a
1744 * 32-bit integer value. This fetch is useful for tracking per-path activity on
1745 * high-traffic sites without having to store whole paths.
1746 */
1747static int smp_fetch_base32(const struct arg *args, struct sample *smp, const char *kw, void *private)
1748{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001749 struct channel *chn = SMP_REQ_CHN(smp);
Willy Tarreau79e57332018-10-02 16:01:16 +02001750 unsigned int hash = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001751
Christopher Faulet46575cd2019-04-17 11:40:30 +02001752 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001753 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +02001754 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001755 struct htx_sl *sl;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001756 struct http_hdr_ctx ctx;
1757 struct ist path;
Willy Tarreau79e57332018-10-02 16:01:16 +02001758
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001759 if (!htx)
1760 return 0;
1761
1762 ctx.blk = NULL;
Dragan Dosen8861e1c2019-02-12 19:50:31 +01001763 if (http_find_header(htx, ist("Host"), &ctx, 0)) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001764 /* OK we have the header value in ctx.value */
1765 while (ctx.value.len--)
1766 hash = *(ctx.value.ptr++) + (hash << 6) + (hash << 16) - hash;
1767 }
1768
1769 /* now retrieve the path */
Christopher Faulet297fbb42019-05-13 14:41:27 +02001770 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001771 path = http_get_path(htx_sl_req_uri(sl));
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001772 if (path.ptr) {
1773 size_t len;
1774
Dragan Dosen8861e1c2019-02-12 19:50:31 +01001775 for (len = 0; len < path.len && *(path.ptr + len) != '?'; len++)
1776 ;
1777
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001778 if (len && *(path.ptr) == '/') {
1779 while (len--)
1780 hash = *(path.ptr++) + (hash << 6) + (hash << 16) - hash;
1781 }
1782 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001783 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001784 else {
1785 /* LEGACY version */
1786 struct http_txn *txn;
1787 struct hdr_ctx ctx;
1788 char *ptr, *beg, *end;
1789 int len;
1790
Christopher Faulet89dc4992019-04-17 12:02:59 +02001791 CHECK_HTTP_MESSAGE_FIRST(chn);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001792
1793 txn = smp->strm->txn;
1794 ctx.idx = 0;
Christopher Faulet89dc4992019-04-17 12:02:59 +02001795 if (http_find_header2("Host", 4, ci_head(chn), &txn->hdr_idx, &ctx)) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001796 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
1797 ptr = ctx.line + ctx.val;
1798 len = ctx.vlen;
1799 while (len--)
1800 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
1801 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001802
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001803 /* now retrieve the path */
Christopher Faulet89dc4992019-04-17 12:02:59 +02001804 end = ci_head(chn) + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001805 beg = http_txn_get_path(txn);
1806 if (!beg)
1807 beg = end;
Willy Tarreau79e57332018-10-02 16:01:16 +02001808
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001809 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
Willy Tarreau79e57332018-10-02 16:01:16 +02001810
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001811 if (beg < ptr && *beg == '/') {
1812 while (beg < ptr)
1813 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
1814 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001815 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001816
Willy Tarreau79e57332018-10-02 16:01:16 +02001817 hash = full_hash(hash);
1818
1819 smp->data.type = SMP_T_SINT;
1820 smp->data.u.sint = hash;
1821 smp->flags = SMP_F_VOL_1ST;
1822 return 1;
1823}
1824
1825/* This concatenates the source address with the 32-bit hash of the Host and
1826 * path as returned by smp_fetch_base32(). The idea is to have per-source and
1827 * per-path counters. The result is a binary block from 8 to 20 bytes depending
1828 * on the source address length. The path hash is stored before the address so
1829 * that in environments where IPv6 is insignificant, truncating the output to
1830 * 8 bytes would still work.
1831 */
1832static int smp_fetch_base32_src(const struct arg *args, struct sample *smp, const char *kw, void *private)
1833{
1834 struct buffer *temp;
1835 struct connection *cli_conn = objt_conn(smp->sess->origin);
1836
1837 if (!cli_conn)
1838 return 0;
1839
1840 if (!smp_fetch_base32(args, smp, kw, private))
1841 return 0;
1842
1843 temp = get_trash_chunk();
1844 *(unsigned int *) temp->area = htonl(smp->data.u.sint);
1845 temp->data += sizeof(unsigned int);
1846
1847 switch (cli_conn->addr.from.ss_family) {
1848 case AF_INET:
1849 memcpy(temp->area + temp->data,
1850 &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr,
1851 4);
1852 temp->data += 4;
1853 break;
1854 case AF_INET6:
1855 memcpy(temp->area + temp->data,
1856 &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr,
1857 16);
1858 temp->data += 16;
1859 break;
1860 default:
1861 return 0;
1862 }
1863
1864 smp->data.u.str = *temp;
1865 smp->data.type = SMP_T_BIN;
1866 return 1;
1867}
1868
1869/* Extracts the query string, which comes after the question mark '?'. If no
1870 * question mark is found, nothing is returned. Otherwise it returns a sample
1871 * of type string carrying the whole query string.
1872 */
1873static int smp_fetch_query(const struct arg *args, struct sample *smp, const char *kw, void *private)
1874{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001875 struct channel *chn = SMP_REQ_CHN(smp);
Willy Tarreau79e57332018-10-02 16:01:16 +02001876 char *ptr, *end;
1877
Christopher Faulet46575cd2019-04-17 11:40:30 +02001878 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001879 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +02001880 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001881 struct htx_sl *sl;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001882
1883 if (!htx)
1884 return 0;
1885
Christopher Faulet297fbb42019-05-13 14:41:27 +02001886 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001887 ptr = HTX_SL_REQ_UPTR(sl);
1888 end = HTX_SL_REQ_UPTR(sl) + HTX_SL_REQ_ULEN(sl);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001889 }
1890 else {
1891 /* LEGACY version */
1892 struct http_txn *txn;
1893
Christopher Faulet89dc4992019-04-17 12:02:59 +02001894 CHECK_HTTP_MESSAGE_FIRST(chn);
Willy Tarreau79e57332018-10-02 16:01:16 +02001895
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001896 txn = smp->strm->txn;
Christopher Faulet89dc4992019-04-17 12:02:59 +02001897 ptr = ci_head(chn) + txn->req.sl.rq.u;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001898 end = ptr + txn->req.sl.rq.u_l;
1899 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001900
1901 /* look up the '?' */
1902 do {
1903 if (ptr == end)
1904 return 0;
1905 } while (*ptr++ != '?');
1906
1907 smp->data.type = SMP_T_STR;
1908 smp->data.u.str.area = ptr;
1909 smp->data.u.str.data = end - ptr;
1910 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
1911 return 1;
1912}
1913
1914static int smp_fetch_proto_http(const struct arg *args, struct sample *smp, const char *kw, void *private)
1915{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001916 struct channel *chn = SMP_REQ_CHN(smp);
1917
Christopher Faulet46575cd2019-04-17 11:40:30 +02001918 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001919 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +02001920 struct htx *htx = smp_prefetch_htx(smp, chn, 0);
Willy Tarreau79e57332018-10-02 16:01:16 +02001921
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001922 if (!htx)
1923 return 0;
1924 }
1925 else {
1926 /* LEGACY version */
Willy Tarreau79e57332018-10-02 16:01:16 +02001927
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001928 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
1929 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
1930 */
Christopher Faulet89dc4992019-04-17 12:02:59 +02001931 CHECK_HTTP_MESSAGE_FIRST_PERM(chn);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001932 }
1933 smp->data.type = SMP_T_BOOL;
Willy Tarreau79e57332018-10-02 16:01:16 +02001934 smp->data.u.sint = 1;
1935 return 1;
1936}
1937
1938/* return a valid test if the current request is the first one on the connection */
1939static int smp_fetch_http_first_req(const struct arg *args, struct sample *smp, const char *kw, void *private)
1940{
Willy Tarreaud095c672020-04-29 11:52:13 +02001941 if (!smp->strm)
1942 return 0;
1943
Willy Tarreau79e57332018-10-02 16:01:16 +02001944 smp->data.type = SMP_T_BOOL;
1945 smp->data.u.sint = !(smp->strm->txn->flags & TX_NOT_FIRST);
1946 return 1;
1947}
1948
1949/* Accepts exactly 1 argument of type userlist */
1950static int smp_fetch_http_auth(const struct arg *args, struct sample *smp, const char *kw, void *private)
1951{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001952 struct channel *chn = SMP_REQ_CHN(smp);
Willy Tarreau79e57332018-10-02 16:01:16 +02001953
1954 if (!args || args->type != ARGT_USR)
1955 return 0;
1956
Christopher Faulet46575cd2019-04-17 11:40:30 +02001957 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001958 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +02001959 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Willy Tarreau79e57332018-10-02 16:01:16 +02001960
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001961 if (!htx)
1962 return 0;
Christopher Faulete98411b2019-07-15 13:58:29 +02001963 if (!get_http_auth(smp, htx))
1964 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001965 }
1966 else {
1967 /* LEGACY version */
Christopher Faulet89dc4992019-04-17 12:02:59 +02001968 CHECK_HTTP_MESSAGE_FIRST(chn);
Christopher Faulete98411b2019-07-15 13:58:29 +02001969 if (!get_http_auth(smp, NULL))
1970 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001971 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001972
1973 smp->data.type = SMP_T_BOOL;
1974 smp->data.u.sint = check_user(args->data.usr, smp->strm->txn->auth.user,
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001975 smp->strm->txn->auth.pass);
Willy Tarreau79e57332018-10-02 16:01:16 +02001976 return 1;
1977}
1978
1979/* Accepts exactly 1 argument of type userlist */
1980static int smp_fetch_http_auth_grp(const struct arg *args, struct sample *smp, const char *kw, void *private)
1981{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001982 struct channel *chn = SMP_REQ_CHN(smp);
1983
Willy Tarreau79e57332018-10-02 16:01:16 +02001984 if (!args || args->type != ARGT_USR)
1985 return 0;
1986
Christopher Faulet46575cd2019-04-17 11:40:30 +02001987 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001988 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +02001989 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001990
1991 if (!htx)
1992 return 0;
Christopher Faulete98411b2019-07-15 13:58:29 +02001993 if (!get_http_auth(smp, htx))
1994 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001995 }
1996 else {
1997 /* LEGACY version */
Christopher Faulet89dc4992019-04-17 12:02:59 +02001998 CHECK_HTTP_MESSAGE_FIRST(chn);
Christopher Faulete98411b2019-07-15 13:58:29 +02001999 if (!get_http_auth(smp, NULL))
2000 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002001 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002002
Willy Tarreau79e57332018-10-02 16:01:16 +02002003 /* if the user does not belong to the userlist or has a wrong password,
2004 * report that it unconditionally does not match. Otherwise we return
2005 * a string containing the username.
2006 */
2007 if (!check_user(args->data.usr, smp->strm->txn->auth.user,
2008 smp->strm->txn->auth.pass))
2009 return 0;
2010
2011 /* pat_match_auth() will need the user list */
2012 smp->ctx.a[0] = args->data.usr;
2013
2014 smp->data.type = SMP_T_STR;
2015 smp->flags = SMP_F_CONST;
2016 smp->data.u.str.area = smp->strm->txn->auth.user;
2017 smp->data.u.str.data = strlen(smp->strm->txn->auth.user);
2018
2019 return 1;
2020}
2021
2022/* Fetch a captured HTTP request header. The index is the position of
2023 * the "capture" option in the configuration file
2024 */
2025static int smp_fetch_capture_req_hdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
2026{
Willy Tarreau79ed4672020-04-29 11:44:54 +02002027 struct proxy *fe;
Willy Tarreau79e57332018-10-02 16:01:16 +02002028 int idx;
2029
2030 if (!args || args->type != ARGT_SINT)
2031 return 0;
2032
Willy Tarreau79ed4672020-04-29 11:44:54 +02002033 if (!smp->strm)
2034 return 0;
2035
2036 fe = strm_fe(smp->strm);
Willy Tarreau79e57332018-10-02 16:01:16 +02002037 idx = args->data.sint;
2038
2039 if (idx > (fe->nb_req_cap - 1) || smp->strm->req_cap == NULL || smp->strm->req_cap[idx] == NULL)
2040 return 0;
2041
2042 smp->data.type = SMP_T_STR;
2043 smp->flags |= SMP_F_CONST;
2044 smp->data.u.str.area = smp->strm->req_cap[idx];
2045 smp->data.u.str.data = strlen(smp->strm->req_cap[idx]);
2046
2047 return 1;
2048}
2049
2050/* Fetch a captured HTTP response header. The index is the position of
2051 * the "capture" option in the configuration file
2052 */
2053static int smp_fetch_capture_res_hdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
2054{
Willy Tarreau79ed4672020-04-29 11:44:54 +02002055 struct proxy *fe;
Willy Tarreau79e57332018-10-02 16:01:16 +02002056 int idx;
2057
2058 if (!args || args->type != ARGT_SINT)
2059 return 0;
2060
Willy Tarreau79ed4672020-04-29 11:44:54 +02002061 if (!smp->strm)
2062 return 0;
2063
2064 fe = strm_fe(smp->strm);
Willy Tarreau79e57332018-10-02 16:01:16 +02002065 idx = args->data.sint;
2066
2067 if (idx > (fe->nb_rsp_cap - 1) || smp->strm->res_cap == NULL || smp->strm->res_cap[idx] == NULL)
2068 return 0;
2069
2070 smp->data.type = SMP_T_STR;
2071 smp->flags |= SMP_F_CONST;
2072 smp->data.u.str.area = smp->strm->res_cap[idx];
2073 smp->data.u.str.data = strlen(smp->strm->res_cap[idx]);
2074
2075 return 1;
2076}
2077
2078/* Extracts the METHOD in the HTTP request, the txn->uri should be filled before the call */
2079static int smp_fetch_capture_req_method(const struct arg *args, struct sample *smp, const char *kw, void *private)
2080{
2081 struct buffer *temp;
Willy Tarreau79ed4672020-04-29 11:44:54 +02002082 struct http_txn *txn;
Willy Tarreau79e57332018-10-02 16:01:16 +02002083 char *ptr;
2084
Willy Tarreau79ed4672020-04-29 11:44:54 +02002085 if (!smp->strm)
2086 return 0;
2087
2088 txn = smp->strm->txn;
Willy Tarreau79e57332018-10-02 16:01:16 +02002089 if (!txn || !txn->uri)
2090 return 0;
2091
2092 ptr = txn->uri;
2093
2094 while (*ptr != ' ' && *ptr != '\0') /* find first space */
2095 ptr++;
2096
2097 temp = get_trash_chunk();
2098 temp->area = txn->uri;
2099 temp->data = ptr - txn->uri;
2100 smp->data.u.str = *temp;
2101 smp->data.type = SMP_T_STR;
2102 smp->flags = SMP_F_CONST;
2103
2104 return 1;
2105
2106}
2107
2108/* Extracts the path in the HTTP request, the txn->uri should be filled before the call */
2109static int smp_fetch_capture_req_uri(const struct arg *args, struct sample *smp, const char *kw, void *private)
2110{
Willy Tarreau79ed4672020-04-29 11:44:54 +02002111 struct http_txn *txn;
Willy Tarreau79e57332018-10-02 16:01:16 +02002112 struct ist path;
2113 const char *ptr;
2114
Willy Tarreau79ed4672020-04-29 11:44:54 +02002115 if (!smp->strm)
2116 return 0;
2117
2118 txn = smp->strm->txn;
Willy Tarreau79e57332018-10-02 16:01:16 +02002119 if (!txn || !txn->uri)
2120 return 0;
2121
2122 ptr = txn->uri;
2123
2124 while (*ptr != ' ' && *ptr != '\0') /* find first space */
2125 ptr++;
2126
2127 if (!*ptr)
2128 return 0;
2129
Christopher Faulet78337bb2018-11-15 14:35:18 +01002130 /* skip the first space and find space after URI */
2131 path = ist2(++ptr, 0);
2132 while (*ptr != ' ' && *ptr != '\0')
2133 ptr++;
2134 path.len = ptr - path.ptr;
Willy Tarreau79e57332018-10-02 16:01:16 +02002135
Christopher Faulet78337bb2018-11-15 14:35:18 +01002136 path = http_get_path(path);
Willy Tarreau79e57332018-10-02 16:01:16 +02002137 if (!path.ptr)
2138 return 0;
2139
2140 smp->data.u.str.area = path.ptr;
2141 smp->data.u.str.data = path.len;
2142 smp->data.type = SMP_T_STR;
2143 smp->flags = SMP_F_CONST;
2144
2145 return 1;
2146}
2147
2148/* Retrieves the HTTP version from the request (either 1.0 or 1.1) and emits it
2149 * as a string (either "HTTP/1.0" or "HTTP/1.1").
2150 */
2151static int smp_fetch_capture_req_ver(const struct arg *args, struct sample *smp, const char *kw, void *private)
2152{
Willy Tarreau79ed4672020-04-29 11:44:54 +02002153 struct http_txn *txn;
2154
2155 if (!smp->strm)
2156 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02002157
Willy Tarreau79ed4672020-04-29 11:44:54 +02002158 txn = smp->strm->txn;
Willy Tarreau79e57332018-10-02 16:01:16 +02002159 if (!txn || txn->req.msg_state < HTTP_MSG_HDR_FIRST)
2160 return 0;
2161
2162 if (txn->req.flags & HTTP_MSGF_VER_11)
2163 smp->data.u.str.area = "HTTP/1.1";
2164 else
2165 smp->data.u.str.area = "HTTP/1.0";
2166
2167 smp->data.u.str.data = 8;
2168 smp->data.type = SMP_T_STR;
2169 smp->flags = SMP_F_CONST;
2170 return 1;
2171
2172}
2173
2174/* Retrieves the HTTP version from the response (either 1.0 or 1.1) and emits it
2175 * as a string (either "HTTP/1.0" or "HTTP/1.1").
2176 */
2177static int smp_fetch_capture_res_ver(const struct arg *args, struct sample *smp, const char *kw, void *private)
2178{
Willy Tarreau79ed4672020-04-29 11:44:54 +02002179 struct http_txn *txn;
2180
2181 if (!smp->strm)
2182 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02002183
Willy Tarreau79ed4672020-04-29 11:44:54 +02002184 txn = smp->strm->txn;
Willy Tarreau79e57332018-10-02 16:01:16 +02002185 if (!txn || txn->rsp.msg_state < HTTP_MSG_HDR_FIRST)
2186 return 0;
2187
2188 if (txn->rsp.flags & HTTP_MSGF_VER_11)
2189 smp->data.u.str.area = "HTTP/1.1";
2190 else
2191 smp->data.u.str.area = "HTTP/1.0";
2192
2193 smp->data.u.str.data = 8;
2194 smp->data.type = SMP_T_STR;
2195 smp->flags = SMP_F_CONST;
2196 return 1;
2197
2198}
2199
2200/* Iterate over all cookies present in a message. The context is stored in
2201 * smp->ctx.a[0] for the in-header position, smp->ctx.a[1] for the
2202 * end-of-header-value, and smp->ctx.a[2] for the hdr_ctx. Depending on
2203 * the direction, multiple cookies may be parsed on the same line or not.
2204 * The cookie name is in args and the name length in args->data.str.len.
2205 * Accepts exactly 1 argument of type string. If the input options indicate
2206 * that no iterating is desired, then only last value is fetched if any.
2207 * The returned sample is of type CSTR. Can be used to parse cookies in other
2208 * files.
2209 */
2210static int smp_fetch_cookie(const struct arg *args, struct sample *smp, const char *kw, void *private)
2211{
Christopher Faulet89dc4992019-04-17 12:02:59 +02002212 /* possible keywords: req.cookie / cookie / cook, res.cookie / scook / set-cookie */
2213 struct channel *chn = ((kw[0] == 'c' || kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Willy Tarreau79e57332018-10-02 16:01:16 +02002214 int occ = 0;
2215 int found = 0;
2216
2217 if (!args || args->type != ARGT_STR)
2218 return 0;
2219
Christopher Faulet46575cd2019-04-17 11:40:30 +02002220 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002221 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +02002222 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002223 struct http_hdr_ctx *ctx = smp->ctx.a[2];
2224 struct ist hdr;
Willy Tarreau79e57332018-10-02 16:01:16 +02002225
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002226 if (!ctx) {
2227 /* first call */
2228 ctx = &static_http_hdr_ctx;
2229 ctx->blk = NULL;
2230 smp->ctx.a[2] = ctx;
2231 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002232
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002233 if (!htx)
2234 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02002235
Christopher Faulet89dc4992019-04-17 12:02:59 +02002236 hdr = (!(chn->flags & CF_ISRESP) ? ist("Cookie") : ist("Set-Cookie"));
Willy Tarreau79e57332018-10-02 16:01:16 +02002237
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002238 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
2239 /* no explicit occurrence and single fetch => last cookie by default */
2240 occ = -1;
Willy Tarreau79e57332018-10-02 16:01:16 +02002241
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002242 /* OK so basically here, either we want only one value and it's the
2243 * last one, or we want to iterate over all of them and we fetch the
2244 * next one.
Willy Tarreau79e57332018-10-02 16:01:16 +02002245 */
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002246
2247 if (!(smp->flags & SMP_F_NOT_LAST)) {
2248 /* search for the header from the beginning, we must first initialize
2249 * the search parameters.
2250 */
2251 smp->ctx.a[0] = NULL;
2252 ctx->blk = NULL;
2253 }
2254
2255 smp->flags |= SMP_F_VOL_HDR;
2256 while (1) {
2257 /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */
2258 if (!smp->ctx.a[0]) {
2259 if (!http_find_header(htx, hdr, ctx, 0))
2260 goto out;
2261
2262 if (ctx->value.len < args->data.str.data + 1)
2263 continue;
2264
2265 smp->ctx.a[0] = ctx->value.ptr;
2266 smp->ctx.a[1] = smp->ctx.a[0] + ctx->value.len;
2267 }
2268
2269 smp->data.type = SMP_T_STR;
2270 smp->flags |= SMP_F_CONST;
2271 smp->ctx.a[0] = http_extract_cookie_value(smp->ctx.a[0], smp->ctx.a[1],
2272 args->data.str.area, args->data.str.data,
2273 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
2274 &smp->data.u.str.area,
2275 &smp->data.u.str.data);
2276 if (smp->ctx.a[0]) {
2277 found = 1;
2278 if (occ >= 0) {
2279 /* one value was returned into smp->data.u.str.{str,len} */
2280 smp->flags |= SMP_F_NOT_LAST;
2281 return 1;
2282 }
2283 }
2284 /* if we're looking for last occurrence, let's loop */
2285 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002286 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002287 else {
2288 /* LEGACY version */
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002289 struct hdr_idx *idx;
2290 struct hdr_ctx *ctx = smp->ctx.a[2];
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002291 const char *hdr_name;
2292 int hdr_name_len;
2293 char *sol;
Willy Tarreau79e57332018-10-02 16:01:16 +02002294
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002295 if (!ctx) {
2296 /* first call */
2297 ctx = &static_hdr_ctx;
2298 ctx->idx = 0;
2299 smp->ctx.a[2] = ctx;
2300 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002301
Christopher Faulet89dc4992019-04-17 12:02:59 +02002302 CHECK_HTTP_MESSAGE_FIRST(chn);
Willy Tarreau79e57332018-10-02 16:01:16 +02002303
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002304 idx = &smp->strm->txn->hdr_idx;
Christopher Faulet89dc4992019-04-17 12:02:59 +02002305 if (!(chn->flags & CF_ISRESP)) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002306 hdr_name = "Cookie";
2307 hdr_name_len = 6;
2308 } else {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002309 hdr_name = "Set-Cookie";
2310 hdr_name_len = 10;
Willy Tarreau79e57332018-10-02 16:01:16 +02002311 }
2312
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002313 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
2314 /* no explicit occurrence and single fetch => last cookie by default */
2315 occ = -1;
2316
2317 /* OK so basically here, either we want only one value and it's the
2318 * last one, or we want to iterate over all of them and we fetch the
2319 * next one.
2320 */
2321
Christopher Faulet89dc4992019-04-17 12:02:59 +02002322 sol = ci_head(chn);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002323 if (!(smp->flags & SMP_F_NOT_LAST)) {
2324 /* search for the header from the beginning, we must first initialize
2325 * the search parameters.
2326 */
2327 smp->ctx.a[0] = NULL;
2328 ctx->idx = 0;
2329 }
2330
2331 smp->flags |= SMP_F_VOL_HDR;
2332
2333 while (1) {
2334 /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */
2335 if (!smp->ctx.a[0]) {
2336 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, ctx))
2337 goto out;
2338
2339 if (ctx->vlen < args->data.str.data + 1)
2340 continue;
2341
2342 smp->ctx.a[0] = ctx->line + ctx->val;
2343 smp->ctx.a[1] = smp->ctx.a[0] + ctx->vlen;
2344 }
2345
2346 smp->data.type = SMP_T_STR;
2347 smp->flags |= SMP_F_CONST;
2348 smp->ctx.a[0] = http_extract_cookie_value(smp->ctx.a[0], smp->ctx.a[1],
2349 args->data.str.area, args->data.str.data,
2350 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
2351 &smp->data.u.str.area, &smp->data.u.str.data);
2352 if (smp->ctx.a[0]) {
2353 found = 1;
2354 if (occ >= 0) {
2355 /* one value was returned into smp->data.u.str.{str,len} */
2356 smp->flags |= SMP_F_NOT_LAST;
2357 return 1;
2358 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002359 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002360 /* if we're looking for last occurrence, let's loop */
Willy Tarreau79e57332018-10-02 16:01:16 +02002361 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002362 }
2363 /* all cookie headers and values were scanned. If we're looking for the
2364 * last occurrence, we may return it now.
2365 */
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002366 out:
Willy Tarreau79e57332018-10-02 16:01:16 +02002367 smp->flags &= ~SMP_F_NOT_LAST;
2368 return found;
2369}
2370
Christopher Fauletc1f40dd2019-05-16 10:07:30 +02002371/* Same than smp_fetch_cookie() but only relies on the sample direction to
2372 * choose the right channel. So instead of duplicating the code, we just change
2373 * the keyword and then fallback on smp_fetch_cookie().
2374 */
2375static int smp_fetch_chn_cookie(const struct arg *args, struct sample *smp, const char *kw, void *private)
2376{
2377 kw = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ ? "req.cook" : "res.cook");
2378 return smp_fetch_cookie(args, smp, kw, private);
2379}
2380
Willy Tarreau79e57332018-10-02 16:01:16 +02002381/* Iterate over all cookies present in a request to count how many occurrences
2382 * match the name in args and args->data.str.len. If <multi> is non-null, then
2383 * multiple cookies may be parsed on the same line. The returned sample is of
2384 * type UINT. Accepts exactly 1 argument of type string.
2385 */
2386static int smp_fetch_cookie_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
2387{
Christopher Faulet89dc4992019-04-17 12:02:59 +02002388 /* possible keywords: req.cook_cnt / cook_cnt, res.cook_cnt / scook_cnt */
2389 struct channel *chn = ((kw[0] == 'c' || kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Willy Tarreau79e57332018-10-02 16:01:16 +02002390 char *val_beg, *val_end;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002391 int cnt;
Willy Tarreau79e57332018-10-02 16:01:16 +02002392
2393 if (!args || args->type != ARGT_STR)
2394 return 0;
2395
Christopher Faulet46575cd2019-04-17 11:40:30 +02002396 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002397 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +02002398 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002399 struct http_hdr_ctx ctx;
2400 struct ist hdr;
2401
2402 if (!htx)
2403 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02002404
Christopher Faulet89dc4992019-04-17 12:02:59 +02002405 hdr = (!(chn->flags & CF_ISRESP) ? ist("Cookie") : ist("Set-Cookie"));
Willy Tarreau79e57332018-10-02 16:01:16 +02002406
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002407 val_end = val_beg = NULL;
2408 ctx.blk = NULL;
2409 cnt = 0;
2410 while (1) {
2411 /* Note: val_beg == NULL every time we need to fetch a new header */
2412 if (!val_beg) {
2413 if (!http_find_header(htx, hdr, &ctx, 0))
2414 break;
Willy Tarreau79e57332018-10-02 16:01:16 +02002415
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002416 if (ctx.value.len < args->data.str.data + 1)
2417 continue;
Willy Tarreau79e57332018-10-02 16:01:16 +02002418
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002419 val_beg = ctx.value.ptr;
2420 val_end = val_beg + ctx.value.len;
2421 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002422
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002423 smp->data.type = SMP_T_STR;
2424 smp->flags |= SMP_F_CONST;
2425 while ((val_beg = http_extract_cookie_value(val_beg, val_end,
2426 args->data.str.area, args->data.str.data,
2427 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
2428 &smp->data.u.str.area,
2429 &smp->data.u.str.data))) {
2430 cnt++;
2431 }
2432 }
2433 }
2434 else {
2435 /* LEGACY version */
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002436 struct hdr_idx *idx;
2437 struct hdr_ctx ctx;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002438 const char *hdr_name;
2439 int hdr_name_len;
2440 char *sol;
Willy Tarreau79e57332018-10-02 16:01:16 +02002441
Christopher Faulet89dc4992019-04-17 12:02:59 +02002442 CHECK_HTTP_MESSAGE_FIRST(chn);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002443
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002444 idx = &smp->strm->txn->hdr_idx;
Christopher Faulet89dc4992019-04-17 12:02:59 +02002445 if (!(chn->flags & CF_ISRESP)) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002446 hdr_name = "Cookie";
2447 hdr_name_len = 6;
2448 } else {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002449 hdr_name = "Set-Cookie";
2450 hdr_name_len = 10;
Willy Tarreau79e57332018-10-02 16:01:16 +02002451 }
2452
Christopher Faulet89dc4992019-04-17 12:02:59 +02002453 sol = ci_head(chn);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002454 val_end = val_beg = NULL;
2455 ctx.idx = 0;
2456 cnt = 0;
2457
2458 while (1) {
2459 /* Note: val_beg == NULL every time we need to fetch a new header */
2460 if (!val_beg) {
2461 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, &ctx))
2462 break;
2463
2464 if (ctx.vlen < args->data.str.data + 1)
2465 continue;
2466
2467 val_beg = ctx.line + ctx.val;
2468 val_end = val_beg + ctx.vlen;
2469 }
2470
2471 smp->data.type = SMP_T_STR;
2472 smp->flags |= SMP_F_CONST;
2473 while ((val_beg = http_extract_cookie_value(val_beg, val_end,
2474 args->data.str.area, args->data.str.data,
2475 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
2476 &smp->data.u.str.area, &smp->data.u.str.data))) {
2477 cnt++;
2478 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002479 }
2480 }
2481
2482 smp->data.type = SMP_T_SINT;
2483 smp->data.u.sint = cnt;
2484 smp->flags |= SMP_F_VOL_HDR;
2485 return 1;
2486}
2487
2488/* Fetch an cookie's integer value. The integer value is returned. It
2489 * takes a mandatory argument of type string. It relies on smp_fetch_cookie().
2490 */
2491static int smp_fetch_cookie_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
2492{
2493 int ret = smp_fetch_cookie(args, smp, kw, private);
2494
2495 if (ret > 0) {
2496 smp->data.type = SMP_T_SINT;
2497 smp->data.u.sint = strl2ic(smp->data.u.str.area,
2498 smp->data.u.str.data);
2499 }
2500
2501 return ret;
2502}
2503
2504/************************************************************************/
2505/* The code below is dedicated to sample fetches */
2506/************************************************************************/
2507
2508/* This scans a URL-encoded query string. It takes an optionally wrapping
2509 * string whose first contigous chunk has its beginning in ctx->a[0] and end
2510 * in ctx->a[1], and the optional second part in (ctx->a[2]..ctx->a[3]). The
2511 * pointers are updated for next iteration before leaving.
2512 */
2513static int smp_fetch_param(char delim, const char *name, int name_len, const struct arg *args, struct sample *smp, const char *kw, void *private)
2514{
2515 const char *vstart, *vend;
2516 struct buffer *temp;
2517 const char **chunks = (const char **)smp->ctx.a;
2518
2519 if (!http_find_next_url_param(chunks, name, name_len,
2520 &vstart, &vend, delim))
2521 return 0;
2522
2523 /* Create sample. If the value is contiguous, return the pointer as CONST,
2524 * if the value is wrapped, copy-it in a buffer.
2525 */
2526 smp->data.type = SMP_T_STR;
2527 if (chunks[2] &&
2528 vstart >= chunks[0] && vstart <= chunks[1] &&
2529 vend >= chunks[2] && vend <= chunks[3]) {
2530 /* Wrapped case. */
2531 temp = get_trash_chunk();
2532 memcpy(temp->area, vstart, chunks[1] - vstart);
2533 memcpy(temp->area + ( chunks[1] - vstart ), chunks[2],
2534 vend - chunks[2]);
2535 smp->data.u.str.area = temp->area;
2536 smp->data.u.str.data = ( chunks[1] - vstart ) + ( vend - chunks[2] );
2537 } else {
2538 /* Contiguous case. */
2539 smp->data.u.str.area = (char *)vstart;
2540 smp->data.u.str.data = vend - vstart;
2541 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
2542 }
2543
2544 /* Update context, check wrapping. */
2545 chunks[0] = vend;
2546 if (chunks[2] && vend >= chunks[2] && vend <= chunks[3]) {
2547 chunks[1] = chunks[3];
2548 chunks[2] = NULL;
2549 }
2550
2551 if (chunks[0] < chunks[1])
2552 smp->flags |= SMP_F_NOT_LAST;
2553
2554 return 1;
2555}
2556
2557/* This function iterates over each parameter of the query string. It uses
2558 * ctx->a[0] and ctx->a[1] to store the beginning and end of the current
2559 * parameter. Since it uses smp_fetch_param(), ctx->a[2..3] are both NULL.
2560 * An optional parameter name is passed in args[0], otherwise any parameter is
2561 * considered. It supports an optional delimiter argument for the beginning of
2562 * the string in args[1], which defaults to "?".
2563 */
2564static int smp_fetch_url_param(const struct arg *args, struct sample *smp, const char *kw, void *private)
2565{
Christopher Faulet89dc4992019-04-17 12:02:59 +02002566 struct channel *chn = SMP_REQ_CHN(smp);
Willy Tarreau79e57332018-10-02 16:01:16 +02002567 char delim = '?';
2568 const char *name;
2569 int name_len;
2570
2571 if (!args ||
2572 (args[0].type && args[0].type != ARGT_STR) ||
2573 (args[1].type && args[1].type != ARGT_STR))
2574 return 0;
2575
2576 name = "";
2577 name_len = 0;
2578 if (args->type == ARGT_STR) {
2579 name = args->data.str.area;
2580 name_len = args->data.str.data;
2581 }
2582
2583 if (args[1].type)
2584 delim = *args[1].data.str.area;
2585
2586 if (!smp->ctx.a[0]) { // first call, find the query string
Christopher Faulet46575cd2019-04-17 11:40:30 +02002587 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002588 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +02002589 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01002590 struct htx_sl *sl;
Willy Tarreau79e57332018-10-02 16:01:16 +02002591
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002592 if (!htx)
2593 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02002594
Christopher Faulet297fbb42019-05-13 14:41:27 +02002595 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01002596 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 +02002597 if (!smp->ctx.a[0])
2598 return 0;
2599
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01002600 smp->ctx.a[1] = HTX_SL_REQ_UPTR(sl) + HTX_SL_REQ_ULEN(sl);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002601 }
2602 else {
2603 /* LEGACY version */
2604 struct http_msg *msg;
2605
Christopher Faulet89dc4992019-04-17 12:02:59 +02002606 CHECK_HTTP_MESSAGE_FIRST(chn);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002607
2608 msg = &smp->strm->txn->req;
2609
Christopher Faulet89dc4992019-04-17 12:02:59 +02002610 smp->ctx.a[0] = http_find_param_list(ci_head(chn) + msg->sl.rq.u,
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002611 msg->sl.rq.u_l, delim);
2612 if (!smp->ctx.a[0])
2613 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02002614
Christopher Faulet89dc4992019-04-17 12:02:59 +02002615 smp->ctx.a[1] = ci_head(chn) + msg->sl.rq.u + msg->sl.rq.u_l;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002616 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002617
2618 /* Assume that the context is filled with NULL pointer
2619 * before the first call.
2620 * smp->ctx.a[2] = NULL;
2621 * smp->ctx.a[3] = NULL;
2622 */
2623 }
2624
2625 return smp_fetch_param(delim, name, name_len, args, smp, kw, private);
2626}
2627
2628/* This function iterates over each parameter of the body. This requires
2629 * that the body has been waited for using http-buffer-request. It uses
2630 * ctx->a[0] and ctx->a[1] to store the beginning and end of the first
2631 * contigous part of the body, and optionally ctx->a[2..3] to reference the
2632 * optional second part if the body wraps at the end of the buffer. An optional
2633 * parameter name is passed in args[0], otherwise any parameter is considered.
2634 */
2635static int smp_fetch_body_param(const struct arg *args, struct sample *smp, const char *kw, void *private)
2636{
Christopher Faulet89dc4992019-04-17 12:02:59 +02002637 struct channel *chn = SMP_REQ_CHN(smp);
Willy Tarreau79e57332018-10-02 16:01:16 +02002638 const char *name;
2639 int name_len;
2640
2641 if (!args || (args[0].type && args[0].type != ARGT_STR))
2642 return 0;
2643
2644 name = "";
2645 name_len = 0;
2646 if (args[0].type == ARGT_STR) {
2647 name = args[0].data.str.area;
2648 name_len = args[0].data.str.data;
2649 }
2650
2651 if (!smp->ctx.a[0]) { // first call, find the query string
Christopher Faulet46575cd2019-04-17 11:40:30 +02002652 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002653 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +02002654 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002655 struct buffer *temp;
2656 int32_t pos;
Willy Tarreau79e57332018-10-02 16:01:16 +02002657
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002658 if (!htx)
2659 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02002660
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002661 temp = get_trash_chunk();
Christopher Fauleta3f15502019-05-13 15:27:23 +02002662 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002663 struct htx_blk *blk = htx_get_blk(htx, pos);
2664 enum htx_blk_type type = htx_get_blk_type(blk);
Willy Tarreau79e57332018-10-02 16:01:16 +02002665
Christopher Faulet54b5e212019-06-04 10:08:28 +02002666 if (type == HTX_BLK_EOM || type == HTX_BLK_TLR || type == HTX_BLK_EOT)
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002667 break;
2668 if (type == HTX_BLK_DATA) {
Christopher Fauletc59ff232018-12-03 13:58:44 +01002669 if (!htx_data_to_h1(htx_get_blk_value(htx, blk), temp, 0))
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002670 return 0;
2671 }
2672 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002673
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002674 smp->ctx.a[0] = temp->area;
2675 smp->ctx.a[1] = temp->area + temp->data;
Willy Tarreau79e57332018-10-02 16:01:16 +02002676
2677 /* Assume that the context is filled with NULL pointer
2678 * before the first call.
2679 * smp->ctx.a[2] = NULL;
2680 * smp->ctx.a[3] = NULL;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002681 */
Willy Tarreau79e57332018-10-02 16:01:16 +02002682 }
2683 else {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002684 /* LEGACY version */
2685 struct http_msg *msg;
2686 unsigned long len;
2687 unsigned long block1;
2688 char *body;
2689
Christopher Faulet89dc4992019-04-17 12:02:59 +02002690 CHECK_HTTP_MESSAGE_FIRST(chn);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002691
Christopher Faulet89dc4992019-04-17 12:02:59 +02002692 msg = &smp->strm->txn->req;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002693 len = http_body_bytes(msg);
Christopher Faulet89dc4992019-04-17 12:02:59 +02002694 body = c_ptr(chn, -http_data_rewind(msg));
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002695
2696 block1 = len;
Christopher Faulet89dc4992019-04-17 12:02:59 +02002697 if (block1 > b_wrap(&chn->buf) - body)
2698 block1 = b_wrap(&chn->buf) - body;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002699
2700 if (block1 == len) {
2701 /* buffer is not wrapped (or empty) */
2702 smp->ctx.a[0] = body;
2703 smp->ctx.a[1] = body + len;
2704
2705 /* Assume that the context is filled with NULL pointer
2706 * before the first call.
2707 * smp->ctx.a[2] = NULL;
2708 * smp->ctx.a[3] = NULL;
2709 */
2710 }
2711 else {
2712 /* buffer is wrapped, we need to defragment it */
2713 smp->ctx.a[0] = body;
2714 smp->ctx.a[1] = body + block1;
Christopher Faulet89dc4992019-04-17 12:02:59 +02002715 smp->ctx.a[2] = b_orig(&chn->buf);
2716 smp->ctx.a[3] = b_orig(&chn->buf) + ( len - block1 );
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002717 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002718 }
2719 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002720
Willy Tarreau79e57332018-10-02 16:01:16 +02002721 return smp_fetch_param('&', name, name_len, args, smp, kw, private);
2722}
2723
2724/* Return the signed integer value for the specified url parameter (see url_param
2725 * above).
2726 */
2727static int smp_fetch_url_param_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
2728{
2729 int ret = smp_fetch_url_param(args, smp, kw, private);
2730
2731 if (ret > 0) {
2732 smp->data.type = SMP_T_SINT;
2733 smp->data.u.sint = strl2ic(smp->data.u.str.area,
2734 smp->data.u.str.data);
2735 }
2736
2737 return ret;
2738}
2739
2740/* This produces a 32-bit hash of the concatenation of the first occurrence of
2741 * the Host header followed by the path component if it begins with a slash ('/').
2742 * This means that '*' will not be added, resulting in exactly the first Host
2743 * entry. If no Host header is found, then the path is used. The resulting value
2744 * is hashed using the url hash followed by a full avalanche hash and provides a
2745 * 32-bit integer value. This fetch is useful for tracking per-URL activity on
2746 * high-traffic sites without having to store whole paths.
2747 * this differs from the base32 functions in that it includes the url parameters
2748 * as well as the path
2749 */
2750static int smp_fetch_url32(const struct arg *args, struct sample *smp, const char *kw, void *private)
2751{
Christopher Faulet89dc4992019-04-17 12:02:59 +02002752 struct channel *chn = SMP_REQ_CHN(smp);
Willy Tarreau79e57332018-10-02 16:01:16 +02002753 unsigned int hash = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02002754
Christopher Faulet46575cd2019-04-17 11:40:30 +02002755 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002756 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +02002757 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002758 struct http_hdr_ctx ctx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01002759 struct htx_sl *sl;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002760 struct ist path;
Willy Tarreau79e57332018-10-02 16:01:16 +02002761
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002762 if (!htx)
2763 return 0;
2764
2765 ctx.blk = NULL;
2766 if (http_find_header(htx, ist("Host"), &ctx, 1)) {
2767 /* OK we have the header value in ctx.value */
2768 while (ctx.value.len--)
2769 hash = *(ctx.value.ptr++) + (hash << 6) + (hash << 16) - hash;
2770 }
2771
2772 /* now retrieve the path */
Christopher Faulet297fbb42019-05-13 14:41:27 +02002773 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01002774 path = http_get_path(htx_sl_req_uri(sl));
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002775 if (path.len && *(path.ptr) == '/') {
2776 while (path.len--)
2777 hash = *(path.ptr++) + (hash << 6) + (hash << 16) - hash;
2778 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002779 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002780 else {
2781 /* LEGACY version */
2782 struct http_txn *txn;
2783 struct hdr_ctx ctx;
2784 char *ptr, *beg, *end;
2785 int len;
2786
Christopher Faulet89dc4992019-04-17 12:02:59 +02002787 CHECK_HTTP_MESSAGE_FIRST(chn);
Willy Tarreau79e57332018-10-02 16:01:16 +02002788
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002789 txn = smp->strm->txn;
2790 ctx.idx = 0;
Christopher Faulet89dc4992019-04-17 12:02:59 +02002791 if (http_find_header2("Host", 4, ci_head(chn), &txn->hdr_idx, &ctx)) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002792 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
2793 ptr = ctx.line + ctx.val;
2794 len = ctx.vlen;
2795 while (len--)
2796 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
2797 }
2798
2799 /* now retrieve the path */
Christopher Faulet89dc4992019-04-17 12:02:59 +02002800 end = ci_head(chn) + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002801 beg = http_txn_get_path(txn);
2802 if (!beg)
2803 beg = end;
Willy Tarreau79e57332018-10-02 16:01:16 +02002804
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002805 for (ptr = beg; ptr < end ; ptr++);
Willy Tarreau79e57332018-10-02 16:01:16 +02002806
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002807 if (beg < ptr && *beg == '/') {
2808 while (beg < ptr)
2809 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
2810 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002811 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002812
Willy Tarreau79e57332018-10-02 16:01:16 +02002813 hash = full_hash(hash);
2814
2815 smp->data.type = SMP_T_SINT;
2816 smp->data.u.sint = hash;
2817 smp->flags = SMP_F_VOL_1ST;
2818 return 1;
2819}
2820
2821/* This concatenates the source address with the 32-bit hash of the Host and
2822 * URL as returned by smp_fetch_base32(). The idea is to have per-source and
2823 * per-url counters. The result is a binary block from 8 to 20 bytes depending
2824 * on the source address length. The URL hash is stored before the address so
2825 * that in environments where IPv6 is insignificant, truncating the output to
2826 * 8 bytes would still work.
2827 */
2828static int smp_fetch_url32_src(const struct arg *args, struct sample *smp, const char *kw, void *private)
2829{
2830 struct buffer *temp;
2831 struct connection *cli_conn = objt_conn(smp->sess->origin);
2832
2833 if (!cli_conn)
2834 return 0;
2835
2836 if (!smp_fetch_url32(args, smp, kw, private))
2837 return 0;
2838
2839 temp = get_trash_chunk();
2840 *(unsigned int *) temp->area = htonl(smp->data.u.sint);
2841 temp->data += sizeof(unsigned int);
2842
2843 switch (cli_conn->addr.from.ss_family) {
2844 case AF_INET:
2845 memcpy(temp->area + temp->data,
2846 &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr,
2847 4);
2848 temp->data += 4;
2849 break;
2850 case AF_INET6:
2851 memcpy(temp->area + temp->data,
2852 &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr,
2853 16);
2854 temp->data += 16;
2855 break;
2856 default:
2857 return 0;
2858 }
2859
2860 smp->data.u.str = *temp;
2861 smp->data.type = SMP_T_BIN;
2862 return 1;
2863}
2864
2865/************************************************************************/
2866/* Other utility functions */
2867/************************************************************************/
2868
2869/* This function is used to validate the arguments passed to any "hdr" fetch
2870 * keyword. These keywords support an optional positive or negative occurrence
2871 * number. We must ensure that the number is greater than -MAX_HDR_HISTORY. It
2872 * is assumed that the types are already the correct ones. Returns 0 on error,
2873 * non-zero if OK. If <err> is not NULL, it will be filled with a pointer to an
2874 * error message in case of error, that the caller is responsible for freeing.
2875 * The initial location must either be freeable or NULL.
2876 * Note: this function's pointer is checked from Lua.
2877 */
2878int val_hdr(struct arg *arg, char **err_msg)
2879{
2880 if (arg && arg[1].type == ARGT_SINT && arg[1].data.sint < -MAX_HDR_HISTORY) {
2881 memprintf(err_msg, "header occurrence must be >= %d", -MAX_HDR_HISTORY);
2882 return 0;
2883 }
2884 return 1;
2885}
2886
2887/************************************************************************/
2888/* All supported sample fetch keywords must be declared here. */
2889/************************************************************************/
2890
2891/* Note: must not be declared <const> as its list will be overwritten */
2892static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
2893 { "base", smp_fetch_base, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2894 { "base32", smp_fetch_base32, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
2895 { "base32+src", smp_fetch_base32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
2896
2897 /* capture are allocated and are permanent in the stream */
2898 { "capture.req.hdr", smp_fetch_capture_req_hdr, ARG1(1,SINT), NULL, SMP_T_STR, SMP_USE_HRQHP },
2899
2900 /* retrieve these captures from the HTTP logs */
2901 { "capture.req.method", smp_fetch_capture_req_method, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
2902 { "capture.req.uri", smp_fetch_capture_req_uri, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
2903 { "capture.req.ver", smp_fetch_capture_req_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
2904
2905 { "capture.res.hdr", smp_fetch_capture_res_hdr, ARG1(1,SINT), NULL, SMP_T_STR, SMP_USE_HRSHP },
2906 { "capture.res.ver", smp_fetch_capture_res_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
2907
2908 /* cookie is valid in both directions (eg: for "stick ...") but cook*
2909 * are only here to match the ACL's name, are request-only and are used
2910 * for ACL compatibility only.
2911 */
2912 { "cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Christopher Fauletc1f40dd2019-05-16 10:07:30 +02002913 { "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 +02002914 { "cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2915 { "cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2916
2917 /* hdr is valid in both directions (eg: for "stick ...") but hdr_* are
2918 * only here to match the ACL's name, are request-only and are used for
2919 * ACL compatibility only.
2920 */
Christopher Fauletc1f40dd2019-05-16 10:07:30 +02002921 { "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 +02002922 { "hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2923 { "hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV },
2924 { "hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_SINT, SMP_USE_HRQHV },
2925
2926 { "http_auth", smp_fetch_http_auth, ARG1(1,USR), NULL, SMP_T_BOOL, SMP_USE_HRQHV },
2927 { "http_auth_group", smp_fetch_http_auth_grp, ARG1(1,USR), NULL, SMP_T_STR, SMP_USE_HRQHV },
2928 { "http_first_req", smp_fetch_http_first_req, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
2929 { "method", smp_fetch_meth, 0, NULL, SMP_T_METH, SMP_USE_HRQHP },
2930 { "path", smp_fetch_path, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2931 { "query", smp_fetch_query, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2932
2933 /* HTTP protocol on the request path */
2934 { "req.proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
2935 { "req_proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
2936
2937 /* HTTP version on the request path */
2938 { "req.ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2939 { "req_ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2940
2941 { "req.body", smp_fetch_body, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
2942 { "req.body_len", smp_fetch_body_len, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
2943 { "req.body_size", smp_fetch_body_size, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
2944 { "req.body_param", smp_fetch_body_param, ARG1(0,STR), NULL, SMP_T_BIN, SMP_USE_HRQHV },
2945
2946 { "req.hdrs", smp_fetch_hdrs, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
2947 { "req.hdrs_bin", smp_fetch_hdrs_bin, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
2948
2949 /* HTTP version on the response path */
2950 { "res.ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
2951 { "resp_ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
2952
2953 /* explicit req.{cook,hdr} are used to force the fetch direction to be request-only */
2954 { "req.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
2955 { "req.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2956 { "req.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2957
2958 { "req.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
2959 { "req.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2960 { "req.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
2961 { "req.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2962 { "req.hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV },
2963 { "req.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
2964 { "req.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_SINT, SMP_USE_HRQHV },
2965
2966 /* explicit req.{cook,hdr} are used to force the fetch direction to be response-only */
2967 { "res.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
2968 { "res.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2969 { "res.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2970
2971 { "res.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
2972 { "res.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2973 { "res.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
2974 { "res.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2975 { "res.hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV },
2976 { "res.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
2977 { "res.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_SINT, SMP_USE_HRSHV },
2978
2979 /* scook is valid only on the response and is used for ACL compatibility */
2980 { "scook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
2981 { "scook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2982 { "scook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2983 { "set-cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV }, /* deprecated */
2984
2985 /* shdr is valid only on the response and is used for ACL compatibility */
2986 { "shdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
2987 { "shdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2988 { "shdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV },
2989 { "shdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_SINT, SMP_USE_HRSHV },
2990
2991 { "status", smp_fetch_stcode, 0, NULL, SMP_T_SINT, SMP_USE_HRSHP },
2992 { "unique-id", smp_fetch_uniqueid, 0, NULL, SMP_T_STR, SMP_SRC_L4SRV },
2993 { "url", smp_fetch_url, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2994 { "url32", smp_fetch_url32, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
2995 { "url32+src", smp_fetch_url32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
2996 { "url_ip", smp_fetch_url_ip, 0, NULL, SMP_T_IPV4, SMP_USE_HRQHV },
2997 { "url_port", smp_fetch_url_port, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
2998 { "url_param", smp_fetch_url_param, ARG2(0,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
2999 { "urlp" , smp_fetch_url_param, ARG2(0,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
3000 { "urlp_val", smp_fetch_url_param_val, ARG2(0,STR,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
3001 { /* END */ },
3002}};
3003
Willy Tarreau0108d902018-11-25 19:14:37 +01003004INITCALL1(STG_REGISTER, sample_register_fetches, &sample_fetch_keywords);
Willy Tarreau79e57332018-10-02 16:01:16 +02003005
3006/*
3007 * Local variables:
3008 * c-indent-level: 8
3009 * c-basic-offset: 8
3010 * End:
3011 */