blob: 7296787cffcc5f74997b94d3116572c118a5fbef [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);
Christopher Fauletef453ed2018-10-24 21:39:27 +0200231
Christopher Fauleteca88542019-04-03 10:12:42 +0200232 if (IS_HTX_STRM(s)) {
Christopher Faulet89dc4992019-04-17 12:02:59 +0200233 htx = htxbuf(&chn->buf);
Christopher Fauletef453ed2018-10-24 21:39:27 +0200234
Christopher Faulet89dc4992019-04-17 12:02:59 +0200235 if (msg->msg_state == HTTP_MSG_ERROR || (htx->flags & HTX_FL_PARSING_ERROR))
236 return NULL;
Christopher Fauletef453ed2018-10-24 21:39:27 +0200237
Christopher Faulet89dc4992019-04-17 12:02:59 +0200238 if (msg->msg_state < HTTP_MSG_BODY) {
239 /* Analyse not yet started */
Christopher Faulet29f17582019-05-23 11:03:26 +0200240 if (htx_is_empty(htx) || htx->first == -1) {
Christopher Fauletef453ed2018-10-24 21:39:27 +0200241 /* Parsing is done by the mux, just wait */
242 smp->flags |= SMP_F_MAY_CHANGE;
243 return NULL;
244 }
245 }
Christopher Faulet297fbb42019-05-13 14:41:27 +0200246 sl = http_get_stline(htx);
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +0200247 if (vol && !sl) {
Christopher Faulet89dc4992019-04-17 12:02:59 +0200248 /* The start-line was already forwarded, it is too late to fetch anything */
249 return NULL;
250 }
Christopher Fauletef453ed2018-10-24 21:39:27 +0200251 }
Christopher Fauleteca88542019-04-03 10:12:42 +0200252 else { /* RAW mode */
Christopher Faulet89dc4992019-04-17 12:02:59 +0200253 struct buffer *buf;
254 struct h1m h1m;
Christopher Faulete4ab11b2019-06-11 15:05:37 +0200255 struct http_hdr hdrs[global.tune.max_http_hdr];
Christopher Faulet89dc4992019-04-17 12:02:59 +0200256 union h1_sl h1sl;
257 unsigned int flags = HTX_FL_NONE;
258 int ret;
Christopher Fauletef453ed2018-10-24 21:39:27 +0200259
Christopher Faulet89dc4992019-04-17 12:02:59 +0200260 /* no HTTP fetch on the response in TCP mode */
261 if (chn->flags & CF_ISRESP)
262 return NULL;
Christopher Fauletef453ed2018-10-24 21:39:27 +0200263
Christopher Faulet89dc4992019-04-17 12:02:59 +0200264 /* Now we are working on the request only */
265 buf = &chn->buf;
266 if (b_head(buf) + b_data(buf) > b_wrap(buf))
267 b_slow_realign(buf, trash.area, 0);
Christopher Fauletef453ed2018-10-24 21:39:27 +0200268
Christopher Faulet89dc4992019-04-17 12:02:59 +0200269 h1m_init_req(&h1m);
270 ret = h1_headers_to_hdr_list(b_head(buf), b_stop(buf),
271 hdrs, sizeof(hdrs)/sizeof(hdrs[0]), &h1m, &h1sl);
272 if (ret <= 0) {
273 /* Invalid or too big*/
274 if (ret < 0 || channel_full(&s->req, global.tune.maxrewrite))
Christopher Fauletef453ed2018-10-24 21:39:27 +0200275 return NULL;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100276
Christopher Faulet89dc4992019-04-17 12:02:59 +0200277 /* wait for a full request */
278 smp->flags |= SMP_F_MAY_CHANGE;
279 return NULL;
280 }
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100281
Christopher Faulet89dc4992019-04-17 12:02:59 +0200282 /* OK we just got a valid HTTP mesage. We have to convert it
283 * into an HTX message.
284 */
285 if (unlikely(h1sl.rq.v.len == 0)) {
286 /* try to convert HTTP/0.9 requests to HTTP/1.0 */
287 if (h1sl.rq.meth != HTTP_METH_GET || !h1sl.rq.u.len)
Christopher Fauletef453ed2018-10-24 21:39:27 +0200288 return NULL;
Christopher Faulet89dc4992019-04-17 12:02:59 +0200289 h1sl.rq.v = ist("HTTP/1.0");
Christopher Fauletef453ed2018-10-24 21:39:27 +0200290 }
Christopher Faulet89dc4992019-04-17 12:02:59 +0200291
292 /* Set HTX start-line flags */
293 if (h1m.flags & H1_MF_VER_11)
294 flags |= HTX_SL_F_VER_11;
295 if (h1m.flags & H1_MF_XFER_ENC)
296 flags |= HTX_SL_F_XFER_ENC;
297 flags |= HTX_SL_F_XFER_LEN;
298 if (h1m.flags & H1_MF_CHNK)
299 flags |= HTX_SL_F_CHNK;
300 else if (h1m.flags & H1_MF_CLEN)
301 flags |= HTX_SL_F_CLEN;
302
Richard Russoc0968f52019-07-31 11:45:56 -0700303 htx = htx_from_buf(get_raw_htx_chunk());
Christopher Faulet89dc4992019-04-17 12:02:59 +0200304 sl = htx_add_stline(htx, HTX_BLK_REQ_SL, flags, h1sl.rq.m, h1sl.rq.u, h1sl.rq.v);
305 if (!sl || !htx_add_all_headers(htx, hdrs))
Christopher Fauletef453ed2018-10-24 21:39:27 +0200306 return NULL;
Willy Tarreauce9bbf52019-05-13 08:32:31 +0200307 sl->info.req.meth = h1sl.rq.meth;
Christopher Faulet89dc4992019-04-17 12:02:59 +0200308 }
309
310 /* OK we just got a valid HTTP message. If not already done by
311 * HTTP analyzers, we have some minor preparation to perform so
312 * that further checks can rely on HTTP tests.
313 */
314 if (sl && msg->msg_state < HTTP_MSG_BODY) {
315 if (!(chn->flags & CF_ISRESP)) {
316 txn->meth = sl->info.req.meth;
317 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
318 s->flags |= SF_REDIRECTABLE;
Christopher Fauletef453ed2018-10-24 21:39:27 +0200319 }
Christopher Faulet89dc4992019-04-17 12:02:59 +0200320 else
321 txn->status = sl->info.res.status;
322 if (sl->flags & HTX_SL_F_VER_11)
323 msg->flags |= HTTP_MSGF_VER_11;
Christopher Fauletef453ed2018-10-24 21:39:27 +0200324 }
325
326 /* everything's OK */
Christopher Fauletef453ed2018-10-24 21:39:27 +0200327 return htx;
328}
329
330/* This function ensures that the prerequisites for an L7 fetch are ready,
331 * which means that a request or response is ready. If some data is missing,
332 * a parsing attempt is made. This is useful in TCP-based ACLs which are able
Willy Tarreau79e57332018-10-02 16:01:16 +0200333 * to extract data from L7. If <req_vol> is non-null during a request prefetch,
334 * another test is made to ensure the required information is not gone.
335 *
336 * The function returns :
337 * 0 with SMP_F_MAY_CHANGE in the sample flags if some data is missing to
338 * decide whether or not an HTTP message is present ;
339 * 0 if the requested data cannot be fetched or if it is certain that
340 * we'll never have any HTTP message there ;
341 * 1 if an HTTP message is ready
342 */
343int smp_prefetch_http(struct proxy *px, struct stream *s, unsigned int opt,
Christopher Faulet89dc4992019-04-17 12:02:59 +0200344 struct channel *chn, struct sample *smp, int req_vol)
Willy Tarreau79e57332018-10-02 16:01:16 +0200345{
346 struct http_txn *txn;
347 struct http_msg *msg;
348
349 /* Note: it is possible that <s> is NULL when called before stream
350 * initialization (eg: tcp-request connection), so this function is the
351 * one responsible for guarding against this case for all HTTP users.
352 */
Christopher Faulet89dc4992019-04-17 12:02:59 +0200353 if (!s || !chn)
Willy Tarreau79e57332018-10-02 16:01:16 +0200354 return 0;
355
356 if (!s->txn) {
357 if (unlikely(!http_alloc_txn(s)))
358 return 0; /* not enough memory */
359 http_init_txn(s);
360 }
361 txn = s->txn;
Willy Tarreau79e57332018-10-02 16:01:16 +0200362 smp->data.type = SMP_T_BOOL;
363
Christopher Faulet89dc4992019-04-17 12:02:59 +0200364 if (chn->flags & CF_ISRESP) {
365 /* Check for a dependency on a response */
366 if (txn->rsp.msg_state < HTTP_MSG_BODY) {
367 smp->flags |= SMP_F_MAY_CHANGE;
368 return 0;
369 }
370 goto end;
371 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200372
Christopher Faulet89dc4992019-04-17 12:02:59 +0200373 /* Check for a dependency on a request */
374 msg = &txn->req;
Willy Tarreau79e57332018-10-02 16:01:16 +0200375
Christopher Faulet89dc4992019-04-17 12:02:59 +0200376 if (req_vol && (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) {
377 return 0; /* data might have moved and indexes changed */
378 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200379
Christopher Faulet89dc4992019-04-17 12:02:59 +0200380 /* If the buffer does not leave enough free space at the end, we must
381 * first realign it.
382 */
383 if (ci_head(chn) > b_orig(&chn->buf) &&
384 ci_head(chn) + ci_data(chn) > b_wrap(&chn->buf) - global.tune.maxrewrite)
385 channel_slow_realign(chn, trash.area);
Willy Tarreau79e57332018-10-02 16:01:16 +0200386
Christopher Faulet89dc4992019-04-17 12:02:59 +0200387 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
388 if (msg->msg_state == HTTP_MSG_ERROR)
389 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200390
Christopher Faulet89dc4992019-04-17 12:02:59 +0200391 /* Try to decode HTTP request */
392 if (likely(msg->next < ci_data(chn)))
393 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau79e57332018-10-02 16:01:16 +0200394
Christopher Faulet89dc4992019-04-17 12:02:59 +0200395 /* Still no valid request ? */
396 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
397 if ((msg->msg_state == HTTP_MSG_ERROR) ||
398 channel_full(chn, global.tune.maxrewrite)) {
Willy Tarreau79e57332018-10-02 16:01:16 +0200399 return 0;
Christopher Faulet89dc4992019-04-17 12:02:59 +0200400 }
401 /* wait for final state */
402 smp->flags |= SMP_F_MAY_CHANGE;
403 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200404 }
405
Christopher Faulet89dc4992019-04-17 12:02:59 +0200406 /* OK we just got a valid HTTP message. We have some minor
407 * preparation to perform so that further checks can rely
408 * on HTTP tests.
409 */
410
411 /* If the message was parsed but was too large, we must absolutely
412 * return an error so that it is not processed. At the moment this
413 * cannot happen, but if the parsers are to change in the future,
414 * we want this check to be maintained.
415 */
416 if (unlikely(ci_head(chn) + ci_data(chn) >
417 b_wrap(&chn->buf) - global.tune.maxrewrite)) {
418 msg->err_state = msg->msg_state;
419 msg->msg_state = HTTP_MSG_ERROR;
420 smp->data.u.sint = 1;
421 return 1;
Willy Tarreau79e57332018-10-02 16:01:16 +0200422 }
423
Christopher Faulet89dc4992019-04-17 12:02:59 +0200424 txn->meth = find_http_meth(ci_head(chn), msg->sl.rq.m_l);
425 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
426 s->flags |= SF_REDIRECTABLE;
427
428 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
Willy Tarreau79e57332018-10-02 16:01:16 +0200429 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200430 }
431
Christopher Faulet89dc4992019-04-17 12:02:59 +0200432 end:
Willy Tarreau79e57332018-10-02 16:01:16 +0200433 /* everything's OK */
434 smp->data.u.sint = 1;
435 return 1;
436}
437
438/* This function fetches the method of current HTTP request and stores
439 * it in the global pattern struct as a chunk. There are two possibilities :
440 * - if the method is known (not HTTP_METH_OTHER), its identifier is stored
441 * in <len> and <ptr> is NULL ;
442 * - if the method is unknown (HTTP_METH_OTHER), <ptr> points to the text and
443 * <len> to its length.
444 * This is intended to be used with pat_match_meth() only.
445 */
446static int smp_fetch_meth(const struct arg *args, struct sample *smp, const char *kw, void *private)
447{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200448 struct channel *chn = SMP_REQ_CHN(smp);
Willy Tarreau79e57332018-10-02 16:01:16 +0200449 int meth;
450 struct http_txn *txn;
451
Christopher Faulet46575cd2019-04-17 11:40:30 +0200452 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200453 /* HTX version */
Willy Tarreau91d0b602020-08-12 14:04:52 +0200454 txn = smp->strm->txn;
455 if (!txn)
Willy Tarreau79e57332018-10-02 16:01:16 +0200456 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200457
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200458 meth = txn->meth;
459 smp->data.type = SMP_T_METH;
460 smp->data.u.meth.meth = meth;
461 if (meth == HTTP_METH_OTHER) {
Willy Tarreau91d0b602020-08-12 14:04:52 +0200462 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100463 struct htx_sl *sl;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200464
Willy Tarreau91d0b602020-08-12 14:04:52 +0200465 htx = smp_prefetch_htx(smp, chn, 0);
466 if (!htx)
467 return 0;
468
Christopher Faulet89dc4992019-04-17 12:02:59 +0200469 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200470 /* ensure the indexes are not affected */
471 return 0;
Christopher Faulet89dc4992019-04-17 12:02:59 +0200472 }
Christopher Faulet297fbb42019-05-13 14:41:27 +0200473 sl = http_get_stline(htx);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200474 smp->flags |= SMP_F_CONST;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100475 smp->data.u.meth.str.area = HTX_SL_REQ_MPTR(sl);
476 smp->data.u.meth.str.data = HTX_SL_REQ_MLEN(sl);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200477 }
478 smp->flags |= SMP_F_VOL_1ST;
479 }
480 else {
481 /* LEGACY version */
Christopher Faulet89dc4992019-04-17 12:02:59 +0200482 CHECK_HTTP_MESSAGE_FIRST_PERM(chn);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200483
484 txn = smp->strm->txn;
485 meth = txn->meth;
486 smp->data.type = SMP_T_METH;
487 smp->data.u.meth.meth = meth;
488 if (meth == HTTP_METH_OTHER) {
Christopher Faulet89dc4992019-04-17 12:02:59 +0200489 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200490 /* ensure the indexes are not affected */
491 return 0;
Christopher Faulet89dc4992019-04-17 12:02:59 +0200492 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200493 smp->flags |= SMP_F_CONST;
494 smp->data.u.meth.str.data = txn->req.sl.rq.m_l;
495 smp->data.u.meth.str.area = ci_head(txn->req.chn);
496 }
497 smp->flags |= SMP_F_VOL_1ST;
Willy Tarreau79e57332018-10-02 16:01:16 +0200498 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200499 return 1;
500}
501
502static int smp_fetch_rqver(const struct arg *args, struct sample *smp, const char *kw, void *private)
503{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200504 struct channel *chn = SMP_REQ_CHN(smp);
Willy Tarreau79e57332018-10-02 16:01:16 +0200505 struct http_txn *txn;
506 char *ptr;
507 int len;
508
Christopher Faulet46575cd2019-04-17 11:40:30 +0200509 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200510 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +0200511 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100512 struct htx_sl *sl;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200513
514 if (!htx)
515 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200516
Christopher Faulet297fbb42019-05-13 14:41:27 +0200517 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100518 len = HTX_SL_REQ_VLEN(sl);
519 ptr = HTX_SL_REQ_VPTR(sl);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200520 }
521 else {
522 /* LEGACY version */
Christopher Faulet89dc4992019-04-17 12:02:59 +0200523 CHECK_HTTP_MESSAGE_FIRST(chn);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200524
525 txn = smp->strm->txn;
526 len = txn->req.sl.rq.v_l;
Christopher Faulet89dc4992019-04-17 12:02:59 +0200527 ptr = ci_head(chn) + txn->req.sl.rq.v;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200528 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200529
530 while ((len-- > 0) && (*ptr++ != '/'));
531 if (len <= 0)
532 return 0;
533
534 smp->data.type = SMP_T_STR;
535 smp->data.u.str.area = ptr;
536 smp->data.u.str.data = len;
537
538 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
539 return 1;
540}
541
542static int smp_fetch_stver(const struct arg *args, struct sample *smp, const char *kw, void *private)
543{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200544 struct channel *chn = SMP_RES_CHN(smp);
Willy Tarreau79e57332018-10-02 16:01:16 +0200545 struct http_txn *txn;
546 char *ptr;
547 int len;
548
Christopher Faulet46575cd2019-04-17 11:40:30 +0200549 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200550 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +0200551 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100552 struct htx_sl *sl;
Willy Tarreau79e57332018-10-02 16:01:16 +0200553
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200554 if (!htx)
555 return 0;
556
Christopher Faulet297fbb42019-05-13 14:41:27 +0200557 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100558 len = HTX_SL_RES_VLEN(sl);
559 ptr = HTX_SL_RES_VPTR(sl);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200560 }
561 else {
562 /* LEGACY version */
Christopher Faulet89dc4992019-04-17 12:02:59 +0200563 CHECK_HTTP_MESSAGE_FIRST(chn);
Willy Tarreau79e57332018-10-02 16:01:16 +0200564
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200565 txn = smp->strm->txn;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200566 len = txn->rsp.sl.st.v_l;
Christopher Faulet89dc4992019-04-17 12:02:59 +0200567 ptr = ci_head(chn);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200568 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200569
570 while ((len-- > 0) && (*ptr++ != '/'));
571 if (len <= 0)
572 return 0;
573
574 smp->data.type = SMP_T_STR;
575 smp->data.u.str.area = ptr;
576 smp->data.u.str.data = len;
577
578 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
579 return 1;
580}
581
582/* 3. Check on Status Code. We manipulate integers here. */
583static int smp_fetch_stcode(const struct arg *args, struct sample *smp, const char *kw, void *private)
584{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200585 struct channel *chn = SMP_RES_CHN(smp);
Willy Tarreau79e57332018-10-02 16:01:16 +0200586 struct http_txn *txn;
587 char *ptr;
588 int len;
589
Christopher Faulet46575cd2019-04-17 11:40:30 +0200590 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200591 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +0200592 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100593 struct htx_sl *sl;
Willy Tarreau79e57332018-10-02 16:01:16 +0200594
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200595 if (!htx)
596 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200597
Christopher Faulet297fbb42019-05-13 14:41:27 +0200598 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100599 len = HTX_SL_RES_CLEN(sl);
600 ptr = HTX_SL_RES_CPTR(sl);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200601 }
602 else {
603 /* LEGACY version */
Christopher Faulet89dc4992019-04-17 12:02:59 +0200604 CHECK_HTTP_MESSAGE_FIRST(chn);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200605
606 txn = smp->strm->txn;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200607 len = txn->rsp.sl.st.c_l;
Christopher Faulet89dc4992019-04-17 12:02:59 +0200608 ptr = ci_head(chn) + txn->rsp.sl.st.c;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200609 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200610
611 smp->data.type = SMP_T_SINT;
612 smp->data.u.sint = __strl2ui(ptr, len);
613 smp->flags = SMP_F_VOL_1ST;
614 return 1;
615}
616
617static int smp_fetch_uniqueid(const struct arg *args, struct sample *smp, const char *kw, void *private)
618{
619 if (LIST_ISEMPTY(&smp->sess->fe->format_unique_id))
620 return 0;
621
Willy Tarreauc1534622020-04-29 11:50:38 +0200622 if (!smp->strm)
623 return 0;
624
Willy Tarreau79e57332018-10-02 16:01:16 +0200625 if (!smp->strm->unique_id) {
626 if ((smp->strm->unique_id = pool_alloc(pool_head_uniqueid)) == NULL)
627 return 0;
628 smp->strm->unique_id[0] = '\0';
Tim Duesterhusea23a5c2020-02-26 16:20:49 +0100629 build_logline(smp->strm, smp->strm->unique_id,
630 UNIQUEID_LEN, &smp->sess->fe->format_unique_id);
Willy Tarreau79e57332018-10-02 16:01:16 +0200631 }
Tim Duesterhusea23a5c2020-02-26 16:20:49 +0100632 smp->data.u.str.data = strlen(smp->strm->unique_id);
Willy Tarreau79e57332018-10-02 16:01:16 +0200633 smp->data.type = SMP_T_STR;
634 smp->data.u.str.area = smp->strm->unique_id;
635 smp->flags = SMP_F_CONST;
636 return 1;
637}
638
639/* Returns a string block containing all headers including the
Joseph Herlant942eea32018-11-15 13:57:22 -0800640 * empty line which separes headers from the body. This is useful
641 * for some headers analysis.
Willy Tarreau79e57332018-10-02 16:01:16 +0200642 */
643static int smp_fetch_hdrs(const struct arg *args, struct sample *smp, const char *kw, void *private)
644{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200645 struct channel *chn = SMP_REQ_CHN(smp);
Willy Tarreau79e57332018-10-02 16:01:16 +0200646 struct http_txn *txn;
647
Christopher Faulet46575cd2019-04-17 11:40:30 +0200648 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200649 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +0200650 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200651 struct buffer *temp;
652 int32_t pos;
Willy Tarreau79e57332018-10-02 16:01:16 +0200653
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200654 if (!htx)
655 return 0;
656 temp = get_trash_chunk();
Christopher Fauleta3f15502019-05-13 15:27:23 +0200657 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200658 struct htx_blk *blk = htx_get_blk(htx, pos);
659 enum htx_blk_type type = htx_get_blk_type(blk);
Willy Tarreau79e57332018-10-02 16:01:16 +0200660
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200661 if (type == HTX_BLK_HDR) {
662 struct ist n = htx_get_blk_name(htx, blk);
663 struct ist v = htx_get_blk_value(htx, blk);
664
Christopher Fauletc59ff232018-12-03 13:58:44 +0100665 if (!htx_hdr_to_h1(n, v, temp))
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200666 return 0;
667 }
668 else if (type == HTX_BLK_EOH) {
669 if (!chunk_memcat(temp, "\r\n", 2))
670 return 0;
671 break;
672 }
673 }
674 smp->data.type = SMP_T_STR;
675 smp->data.u.str = *temp;
676
677 }
678 else {
679 /* LEGACY version */
680 struct http_msg *msg;
681 struct hdr_idx *idx;
682
Christopher Faulet89dc4992019-04-17 12:02:59 +0200683 CHECK_HTTP_MESSAGE_FIRST(chn);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200684
685 txn = smp->strm->txn;
686 idx = &txn->hdr_idx;
687 msg = &txn->req;
Willy Tarreau79e57332018-10-02 16:01:16 +0200688
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200689 smp->data.type = SMP_T_STR;
Christopher Faulet89dc4992019-04-17 12:02:59 +0200690 smp->data.u.str.area = ci_head(chn) + hdr_idx_first_pos(idx);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200691 smp->data.u.str.data = msg->eoh - hdr_idx_first_pos(idx) + 1 +
Christopher Faulet89dc4992019-04-17 12:02:59 +0200692 (ci_head(chn)[msg->eoh] == '\r');
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200693 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200694 return 1;
695}
696
697/* Returns the header request in a length/value encoded format.
698 * This is useful for exchanges with the SPOE.
699 *
700 * A "length value" is a multibyte code encoding numbers. It uses the
701 * SPOE format. The encoding is the following:
702 *
703 * Each couple "header name" / "header value" is composed
704 * like this:
705 * "length value" "header name bytes"
706 * "length value" "header value bytes"
707 * When the last header is reached, the header name and the header
708 * value are empty. Their length are 0
709 */
710static int smp_fetch_hdrs_bin(const struct arg *args, struct sample *smp, const char *kw, void *private)
711{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200712 struct channel *chn = SMP_REQ_CHN(smp);
Willy Tarreau79e57332018-10-02 16:01:16 +0200713 struct http_txn *txn;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200714 struct buffer *temp;
Willy Tarreau79e57332018-10-02 16:01:16 +0200715
Christopher Faulet46575cd2019-04-17 11:40:30 +0200716 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200717 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +0200718 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200719 struct buffer *temp;
720 char *p, *end;
721 int32_t pos;
722 int ret;
Willy Tarreau79e57332018-10-02 16:01:16 +0200723
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200724 if (!htx)
725 return 0;
726 temp = get_trash_chunk();
727 p = temp->area;
728 end = temp->area + temp->size;
Christopher Fauleta3f15502019-05-13 15:27:23 +0200729 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200730 struct htx_blk *blk = htx_get_blk(htx, pos);
731 enum htx_blk_type type = htx_get_blk_type(blk);
732 struct ist n, v;
Willy Tarreau79e57332018-10-02 16:01:16 +0200733
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200734 if (type == HTX_BLK_HDR) {
735 n = htx_get_blk_name(htx,blk);
736 v = htx_get_blk_value(htx, blk);
Willy Tarreau79e57332018-10-02 16:01:16 +0200737
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200738 /* encode the header name. */
739 ret = encode_varint(n.len, &p, end);
740 if (ret == -1)
741 return 0;
742 if (p + n.len > end)
743 return 0;
744 memcpy(p, n.ptr, n.len);
745 p += n.len;
Willy Tarreau79e57332018-10-02 16:01:16 +0200746
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200747 /* encode the header value. */
748 ret = encode_varint(v.len, &p, end);
749 if (ret == -1)
750 return 0;
751 if (p + v.len > end)
752 return 0;
753 memcpy(p, v.ptr, v.len);
754 p += v.len;
Willy Tarreau79e57332018-10-02 16:01:16 +0200755
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200756 }
757 else if (type == HTX_BLK_EOH) {
758 /* encode the end of the header list with empty
759 * header name and header value.
760 */
761 ret = encode_varint(0, &p, end);
762 if (ret == -1)
763 return 0;
764 ret = encode_varint(0, &p, end);
765 if (ret == -1)
766 return 0;
767 break;
768 }
769 }
770
771 /* Initialise sample data which will be filled. */
772 smp->data.type = SMP_T_BIN;
773 smp->data.u.str.area = temp->area;
774 smp->data.u.str.data = p - temp->area;
775 smp->data.u.str.size = temp->size;
776 }
777 else {
778 /* LEGACY version */
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200779 struct hdr_idx *idx;
780 const char *cur_ptr, *cur_next, *p;
781 int old_idx, cur_idx;
782 struct hdr_idx_elem *cur_hdr;
783 const char *hn, *hv;
784 int hnl, hvl;
785 int ret;
786 char *buf;
787 char *end;
788
Christopher Faulet89dc4992019-04-17 12:02:59 +0200789 CHECK_HTTP_MESSAGE_FIRST(chn);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200790
791 temp = get_trash_chunk();
792 buf = temp->area;
793 end = temp->area + temp->size;
794
795 txn = smp->strm->txn;
796 idx = &txn->hdr_idx;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200797
798 /* Build array of headers. */
799 old_idx = 0;
Christopher Faulet89dc4992019-04-17 12:02:59 +0200800 cur_next = ci_head(chn) + hdr_idx_first_pos(idx);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200801 while (1) {
802 cur_idx = idx->v[old_idx].next;
803 if (!cur_idx)
804 break;
805 old_idx = cur_idx;
Willy Tarreau79e57332018-10-02 16:01:16 +0200806
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200807 cur_hdr = &idx->v[cur_idx];
808 cur_ptr = cur_next;
809 cur_next = cur_ptr + cur_hdr->len + cur_hdr->cr + 1;
810
811 /* Now we have one full header at cur_ptr of len cur_hdr->len,
812 * and the next header starts at cur_next. We'll check
813 * this header in the list as well as against the default
814 * rule.
815 */
816
817 /* look for ': *'. */
818 hn = cur_ptr;
819 for (p = cur_ptr; p < cur_ptr + cur_hdr->len && *p != ':'; p++);
820 if (p >= cur_ptr+cur_hdr->len)
821 continue;
822 hnl = p - hn;
Willy Tarreau79e57332018-10-02 16:01:16 +0200823 p++;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200824 while (p < cur_ptr + cur_hdr->len && (*p == ' ' || *p == '\t'))
825 p++;
826 if (p >= cur_ptr + cur_hdr->len)
827 continue;
828 hv = p;
829 hvl = cur_ptr + cur_hdr->len-p;
Willy Tarreau79e57332018-10-02 16:01:16 +0200830
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200831 /* encode the header name. */
832 ret = encode_varint(hnl, &buf, end);
833 if (ret == -1)
834 return 0;
835 if (buf + hnl > end)
836 return 0;
837 memcpy(buf, hn, hnl);
838 buf += hnl;
839
840 /* encode and copy the value. */
841 ret = encode_varint(hvl, &buf, end);
842 if (ret == -1)
843 return 0;
844 if (buf + hvl > end)
845 return 0;
846 memcpy(buf, hv, hvl);
847 buf += hvl;
848 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200849
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200850 /* encode the end of the header list with empty
851 * header name and header value.
852 */
853 ret = encode_varint(0, &buf, end);
Willy Tarreau79e57332018-10-02 16:01:16 +0200854 if (ret == -1)
855 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200856 ret = encode_varint(0, &buf, end);
857 if (ret == -1)
Willy Tarreau79e57332018-10-02 16:01:16 +0200858 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200859
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200860 /* Initialise sample data which will be filled. */
861 smp->data.type = SMP_T_BIN;
862 smp->data.u.str.area = temp->area;
863 smp->data.u.str.data = buf - temp->area;
864 smp->data.u.str.size = temp->size;
865 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200866 return 1;
867}
868
869/* returns the longest available part of the body. This requires that the body
870 * has been waited for using http-buffer-request.
871 */
872static int smp_fetch_body(const struct arg *args, struct sample *smp, const char *kw, void *private)
873{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200874 struct channel *chn = SMP_REQ_CHN(smp);
Willy Tarreau79e57332018-10-02 16:01:16 +0200875 struct buffer *temp;
876
Christopher Faulet46575cd2019-04-17 11:40:30 +0200877 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200878 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +0200879 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200880 int32_t pos;
Willy Tarreau79e57332018-10-02 16:01:16 +0200881
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200882 if (!htx)
883 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200884
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200885 temp = get_trash_chunk();
Christopher Fauleta3f15502019-05-13 15:27:23 +0200886 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200887 struct htx_blk *blk = htx_get_blk(htx, pos);
888 enum htx_blk_type type = htx_get_blk_type(blk);
Willy Tarreau79e57332018-10-02 16:01:16 +0200889
Christopher Faulet54b5e212019-06-04 10:08:28 +0200890 if (type == HTX_BLK_EOM || type == HTX_BLK_TLR || type == HTX_BLK_EOT)
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200891 break;
892 if (type == HTX_BLK_DATA) {
Christopher Fauletc59ff232018-12-03 13:58:44 +0100893 if (!htx_data_to_h1(htx_get_blk_value(htx, blk), temp, 0))
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200894 return 0;
895 }
896 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200897
Willy Tarreau79e57332018-10-02 16:01:16 +0200898 smp->data.type = SMP_T_BIN;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200899 smp->data.u.str = *temp;
900 smp->flags = SMP_F_VOL_TEST;
Willy Tarreau79e57332018-10-02 16:01:16 +0200901 }
902 else {
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200903 /* LEGACY version */
904 struct http_msg *msg;
905 unsigned long len;
906 unsigned long block1;
907 char *body;
908
Christopher Faulet89dc4992019-04-17 12:02:59 +0200909 CHECK_HTTP_MESSAGE_FIRST(chn);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200910
Christopher Faulet89dc4992019-04-17 12:02:59 +0200911 msg = &smp->strm->txn->req;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200912 len = http_body_bytes(msg);
Christopher Faulet89dc4992019-04-17 12:02:59 +0200913 body = c_ptr(chn, -http_data_rewind(msg));
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200914
915 block1 = len;
Christopher Faulet89dc4992019-04-17 12:02:59 +0200916 if (block1 > b_wrap(&chn->buf) - body)
917 block1 = b_wrap(&chn->buf) - body;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200918
919 if (block1 == len) {
920 /* buffer is not wrapped (or empty) */
921 smp->data.type = SMP_T_BIN;
922 smp->data.u.str.area = body;
923 smp->data.u.str.data = len;
924 smp->flags = SMP_F_VOL_TEST | SMP_F_CONST;
925 }
926 else {
927 /* buffer is wrapped, we need to defragment it */
928 temp = get_trash_chunk();
929 memcpy(temp->area, body, block1);
Christopher Faulet89dc4992019-04-17 12:02:59 +0200930 memcpy(temp->area + block1, b_orig(&chn->buf), len - block1);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200931 smp->data.type = SMP_T_BIN;
932 smp->data.u.str.area = temp->area;
933 smp->data.u.str.data = len;
934 smp->flags = SMP_F_VOL_TEST;
935 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200936 }
937 return 1;
938}
939
940
941/* returns the available length of the body. This requires that the body
942 * has been waited for using http-buffer-request.
943 */
944static int smp_fetch_body_len(const struct arg *args, struct sample *smp, const char *kw, void *private)
945{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200946 struct channel *chn = SMP_REQ_CHN(smp);
947
Christopher Faulet46575cd2019-04-17 11:40:30 +0200948 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200949 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +0200950 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Dragan Dosen5a606682019-02-14 12:30:53 +0100951 int32_t pos;
Christopher Fauletc16317d2018-12-12 14:11:22 +0100952 unsigned long long len = 0;
953
954 if (!htx)
955 return 0;
956
Christopher Fauleta3f15502019-05-13 15:27:23 +0200957 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Dragan Dosen5a606682019-02-14 12:30:53 +0100958 struct htx_blk *blk = htx_get_blk(htx, pos);
959 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Fauletc16317d2018-12-12 14:11:22 +0100960
Christopher Faulet54b5e212019-06-04 10:08:28 +0200961 if (type == HTX_BLK_EOM || type == HTX_BLK_TLR || type == HTX_BLK_EOT)
Christopher Fauletc16317d2018-12-12 14:11:22 +0100962 break;
Dragan Dosen5a606682019-02-14 12:30:53 +0100963 if (type == HTX_BLK_DATA)
964 len += htx_get_blksz(blk);
Christopher Fauletc16317d2018-12-12 14:11:22 +0100965 }
966
967 smp->data.type = SMP_T_SINT;
968 smp->data.u.sint = len;
969
970 smp->flags = SMP_F_VOL_TEST;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200971 }
972 else {
973 /* LEGACY version */
974 struct http_msg *msg;
Willy Tarreau79e57332018-10-02 16:01:16 +0200975
Christopher Faulet89dc4992019-04-17 12:02:59 +0200976 CHECK_HTTP_MESSAGE_FIRST(chn);
Willy Tarreau79e57332018-10-02 16:01:16 +0200977
Christopher Faulet89dc4992019-04-17 12:02:59 +0200978 msg = &smp->strm->txn->req;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200979 smp->data.type = SMP_T_SINT;
980 smp->data.u.sint = http_body_bytes(msg);
Willy Tarreau79e57332018-10-02 16:01:16 +0200981
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200982 smp->flags = SMP_F_VOL_TEST;
983 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200984 return 1;
985}
986
987
988/* returns the advertised length of the body, or the advertised size of the
989 * chunks available in the buffer. This requires that the body has been waited
990 * for using http-buffer-request.
991 */
992static int smp_fetch_body_size(const struct arg *args, struct sample *smp, const char *kw, void *private)
993{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200994 struct channel *chn = SMP_REQ_CHN(smp);
995
Christopher Faulet46575cd2019-04-17 11:40:30 +0200996 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200997 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +0200998 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Dragan Dosen5a606682019-02-14 12:30:53 +0100999 int32_t pos;
Christopher Fauletc16317d2018-12-12 14:11:22 +01001000 unsigned long long len = 0;
1001
1002 if (!htx)
1003 return 0;
1004
Christopher Fauleta3f15502019-05-13 15:27:23 +02001005 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Dragan Dosen5a606682019-02-14 12:30:53 +01001006 struct htx_blk *blk = htx_get_blk(htx, pos);
1007 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Fauletc16317d2018-12-12 14:11:22 +01001008
Christopher Faulet54b5e212019-06-04 10:08:28 +02001009 if (type == HTX_BLK_EOM || type == HTX_BLK_TLR || type == HTX_BLK_EOT)
Christopher Fauletc16317d2018-12-12 14:11:22 +01001010 break;
Dragan Dosen5a606682019-02-14 12:30:53 +01001011 if (type == HTX_BLK_DATA)
1012 len += htx_get_blksz(blk);
Christopher Fauletc16317d2018-12-12 14:11:22 +01001013 }
1014 if (htx->extra != ULLONG_MAX)
1015 len += htx->extra;
1016
1017 smp->data.type = SMP_T_SINT;
1018 smp->data.u.sint = len;
1019
1020 smp->flags = SMP_F_VOL_TEST;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001021 }
1022 else {
1023 /* LEGACY version */
1024 struct http_msg *msg;
Willy Tarreau79e57332018-10-02 16:01:16 +02001025
Christopher Faulet89dc4992019-04-17 12:02:59 +02001026 CHECK_HTTP_MESSAGE_FIRST(chn);
Willy Tarreau79e57332018-10-02 16:01:16 +02001027
Christopher Faulet89dc4992019-04-17 12:02:59 +02001028 msg = &smp->strm->txn->req;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001029 smp->data.type = SMP_T_SINT;
1030 smp->data.u.sint = msg->body_len;
Willy Tarreau79e57332018-10-02 16:01:16 +02001031
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001032 smp->flags = SMP_F_VOL_TEST;
1033 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001034 return 1;
1035}
1036
1037
1038/* 4. Check on URL/URI. A pointer to the URI is stored. */
1039static int smp_fetch_url(const struct arg *args, struct sample *smp, const char *kw, void *private)
1040{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001041 struct channel *chn = SMP_REQ_CHN(smp);
Willy Tarreau79e57332018-10-02 16:01:16 +02001042 struct http_txn *txn;
1043
Christopher Faulet46575cd2019-04-17 11:40:30 +02001044 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001045 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +02001046 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001047 struct htx_sl *sl;
Willy Tarreau79e57332018-10-02 16:01:16 +02001048
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001049 if (!htx)
1050 return 0;
Christopher Faulet297fbb42019-05-13 14:41:27 +02001051 sl = http_get_stline(htx);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001052 smp->data.type = SMP_T_STR;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001053 smp->data.u.str.area = HTX_SL_REQ_UPTR(sl);
1054 smp->data.u.str.data = HTX_SL_REQ_ULEN(sl);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001055 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
1056 }
1057 else {
1058 /* LEGACY version */
Christopher Faulet89dc4992019-04-17 12:02:59 +02001059 CHECK_HTTP_MESSAGE_FIRST(chn);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001060
1061 txn = smp->strm->txn;
1062 smp->data.type = SMP_T_STR;
1063 smp->data.u.str.data = txn->req.sl.rq.u_l;
Christopher Faulet89dc4992019-04-17 12:02:59 +02001064 smp->data.u.str.area = ci_head(chn) + txn->req.sl.rq.u;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001065 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
1066 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001067 return 1;
1068}
1069
1070static int smp_fetch_url_ip(const struct arg *args, struct sample *smp, const char *kw, void *private)
1071{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001072 struct channel *chn = SMP_REQ_CHN(smp);
Willy Tarreau79e57332018-10-02 16:01:16 +02001073 struct http_txn *txn;
1074 struct sockaddr_storage addr;
1075
Christopher Faulet46575cd2019-04-17 11:40:30 +02001076 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001077 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +02001078 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001079 struct htx_sl *sl;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001080
1081 if (!htx)
1082 return 0;
Christopher Faulet297fbb42019-05-13 14:41:27 +02001083 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001084 url2sa(HTX_SL_REQ_UPTR(sl), HTX_SL_REQ_ULEN(sl), &addr, NULL);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001085 }
1086 else {
1087 /* LEGACY version */
Christopher Faulet89dc4992019-04-17 12:02:59 +02001088 CHECK_HTTP_MESSAGE_FIRST(chn);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001089
1090 txn = smp->strm->txn;
Christopher Faulet89dc4992019-04-17 12:02:59 +02001091 url2sa(ci_head(chn) + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &addr, NULL);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001092 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001093
Willy Tarreau79e57332018-10-02 16:01:16 +02001094 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
1095 return 0;
1096
1097 smp->data.type = SMP_T_IPV4;
1098 smp->data.u.ipv4 = ((struct sockaddr_in *)&addr)->sin_addr;
1099 smp->flags = 0;
1100 return 1;
1101}
1102
1103static int smp_fetch_url_port(const struct arg *args, struct sample *smp, const char *kw, void *private)
1104{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001105 struct channel *chn = SMP_REQ_CHN(smp);
Willy Tarreau79e57332018-10-02 16:01:16 +02001106 struct http_txn *txn;
1107 struct sockaddr_storage addr;
1108
Christopher Faulet46575cd2019-04-17 11:40:30 +02001109 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001110 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +02001111 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001112 struct htx_sl *sl;
Willy Tarreau79e57332018-10-02 16:01:16 +02001113
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001114 if (!htx)
1115 return 0;
Christopher Faulet297fbb42019-05-13 14:41:27 +02001116 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001117 url2sa(HTX_SL_REQ_UPTR(sl), HTX_SL_REQ_ULEN(sl), &addr, NULL);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001118 }
1119 else {
1120 /* LEGACY version */
Christopher Faulet89dc4992019-04-17 12:02:59 +02001121 CHECK_HTTP_MESSAGE_FIRST(chn);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001122
1123 txn = smp->strm->txn;
Christopher Faulet89dc4992019-04-17 12:02:59 +02001124 url2sa(ci_head(chn) + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &addr, NULL);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001125 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001126 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
1127 return 0;
1128
1129 smp->data.type = SMP_T_SINT;
1130 smp->data.u.sint = ntohs(((struct sockaddr_in *)&addr)->sin_port);
1131 smp->flags = 0;
1132 return 1;
1133}
1134
1135/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
1136 * Accepts an optional argument of type string containing the header field name,
1137 * and an optional argument of type signed or unsigned integer to request an
1138 * explicit occurrence of the header. Note that in the event of a missing name,
1139 * headers are considered from the first one. It does not stop on commas and
1140 * returns full lines instead (useful for User-Agent or Date for example).
1141 */
1142static int smp_fetch_fhdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
1143{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001144 /* possible keywords: req.fhdr, res.fhdr */
1145 struct channel *chn = ((kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Willy Tarreau79e57332018-10-02 16:01:16 +02001146 int occ = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001147
Christopher Faulet46575cd2019-04-17 11:40:30 +02001148 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001149 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +02001150 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001151 struct http_hdr_ctx *ctx = smp->ctx.a[0];
1152 struct ist name;
Willy Tarreau79e57332018-10-02 16:01:16 +02001153
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001154 if (!ctx) {
1155 /* first call */
1156 ctx = &static_http_hdr_ctx;
1157 ctx->blk = NULL;
1158 smp->ctx.a[0] = ctx;
1159 }
1160
1161 if (args) {
1162 if (args[0].type != ARGT_STR)
1163 return 0;
1164 name.ptr = args[0].data.str.area;
1165 name.len = args[0].data.str.data;
1166
1167 if (args[1].type == ARGT_SINT)
1168 occ = args[1].data.sint;
1169 }
1170
1171 if (!htx)
Willy Tarreau79e57332018-10-02 16:01:16 +02001172 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001173
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001174 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
1175 /* search for header from the beginning */
1176 ctx->blk = NULL;
1177
1178 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
1179 /* no explicit occurrence and single fetch => last header by default */
1180 occ = -1;
1181
1182 if (!occ)
1183 /* prepare to report multiple occurrences for ACL fetches */
1184 smp->flags |= SMP_F_NOT_LAST;
1185
1186 smp->data.type = SMP_T_STR;
1187 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
1188 if (http_get_htx_fhdr(htx, name, occ, ctx, &smp->data.u.str.area, &smp->data.u.str.data))
1189 return 1;
Willy Tarreau79e57332018-10-02 16:01:16 +02001190 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001191 else {
1192 /* LEGACY version */
1193 struct hdr_idx *idx;
1194 struct hdr_ctx *ctx = smp->ctx.a[0];
1195 const struct http_msg *msg;
1196 const char *name_str = NULL;
1197 int name_len = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001198
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001199 if (!ctx) {
1200 /* first call */
1201 ctx = &static_hdr_ctx;
1202 ctx->idx = 0;
1203 smp->ctx.a[0] = ctx;
1204 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001205
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001206 if (args) {
1207 if (args[0].type != ARGT_STR)
1208 return 0;
1209 name_str = args[0].data.str.area;
1210 name_len = args[0].data.str.data;
Willy Tarreau79e57332018-10-02 16:01:16 +02001211
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001212 if (args[1].type == ARGT_SINT)
1213 occ = args[1].data.sint;
1214 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001215
Christopher Faulet89dc4992019-04-17 12:02:59 +02001216 CHECK_HTTP_MESSAGE_FIRST(chn);
Willy Tarreau79e57332018-10-02 16:01:16 +02001217
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001218 idx = &smp->strm->txn->hdr_idx;
Christopher Faulet89dc4992019-04-17 12:02:59 +02001219 msg = (!(chn->flags & CF_ISRESP) ? &smp->strm->txn->req : &smp->strm->txn->rsp);
Willy Tarreau79e57332018-10-02 16:01:16 +02001220
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001221 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
1222 /* search for header from the beginning */
1223 ctx->idx = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001224
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001225 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
1226 /* no explicit occurrence and single fetch => last header by default */
1227 occ = -1;
1228
1229 if (!occ)
1230 /* prepare to report multiple occurrences for ACL fetches */
1231 smp->flags |= SMP_F_NOT_LAST;
1232
1233 smp->data.type = SMP_T_STR;
1234 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
1235 if (http_get_fhdr(msg, name_str, name_len, idx, occ, ctx, &smp->data.u.str.area, &smp->data.u.str.data))
1236 return 1;
1237 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001238 smp->flags &= ~SMP_F_NOT_LAST;
1239 return 0;
1240}
1241
1242/* 6. Check on HTTP header count. The number of occurrences is returned.
1243 * Accepts exactly 1 argument of type string. It does not stop on commas and
1244 * returns full lines instead (useful for User-Agent or Date for example).
1245 */
1246static int smp_fetch_fhdr_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
1247{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001248 /* possible keywords: req.fhdr_cnt, res.fhdr_cnt */
1249 struct channel *chn = ((kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Willy Tarreau79e57332018-10-02 16:01:16 +02001250 int cnt;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001251
Christopher Faulet46575cd2019-04-17 11:40:30 +02001252 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001253 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +02001254 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001255 struct http_hdr_ctx ctx;
1256 struct ist name;
1257
1258 if (!htx)
1259 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001260
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001261 if (args && args->type == ARGT_STR) {
1262 name.ptr = args->data.str.area;
1263 name.len = args->data.str.data;
Olivier Houcharde2c78cd2018-11-21 13:49:48 +01001264 } else {
1265 name.ptr = NULL;
1266 name.len = 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001267 }
1268
1269 ctx.blk = NULL;
1270 cnt = 0;
1271 while (http_find_header(htx, name, &ctx, 1))
1272 cnt++;
Willy Tarreau79e57332018-10-02 16:01:16 +02001273 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001274 else {
1275 /* LEGACY version */
1276 struct hdr_idx *idx;
1277 struct hdr_ctx ctx;
1278 const struct http_msg *msg;
1279 const char *name = NULL;
1280 int len = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001281
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001282 if (args && args->type == ARGT_STR) {
1283 name = args->data.str.area;
1284 len = args->data.str.data;
1285 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001286
Christopher Faulet89dc4992019-04-17 12:02:59 +02001287 CHECK_HTTP_MESSAGE_FIRST(chn);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001288
1289 idx = &smp->strm->txn->hdr_idx;
Christopher Faulet89dc4992019-04-17 12:02:59 +02001290 msg = (!(chn->flags & CF_ISRESP) ? &smp->strm->txn->req : &smp->strm->txn->rsp);
Willy Tarreau79e57332018-10-02 16:01:16 +02001291
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001292 ctx.idx = 0;
1293 cnt = 0;
1294 while (http_find_full_header2(name, len, ci_head(msg->chn), idx, &ctx))
1295 cnt++;
1296 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001297
1298 smp->data.type = SMP_T_SINT;
1299 smp->data.u.sint = cnt;
1300 smp->flags = SMP_F_VOL_HDR;
1301 return 1;
1302}
1303
1304static int smp_fetch_hdr_names(const struct arg *args, struct sample *smp, const char *kw, void *private)
1305{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001306 /* possible keywords: req.hdr_names, res.hdr_names */
1307 struct channel *chn = ((kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Willy Tarreau79e57332018-10-02 16:01:16 +02001308 struct buffer *temp;
1309 char del = ',';
1310
Christopher Faulet46575cd2019-04-17 11:40:30 +02001311 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001312 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +02001313 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001314 int32_t pos;
Willy Tarreau79e57332018-10-02 16:01:16 +02001315
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001316 if (!htx)
1317 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001318
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001319 if (args && args->type == ARGT_STR)
1320 del = *args[0].data.str.area;
Willy Tarreau79e57332018-10-02 16:01:16 +02001321
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001322 temp = get_trash_chunk();
Christopher Fauleta3f15502019-05-13 15:27:23 +02001323 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001324 struct htx_blk *blk = htx_get_blk(htx, pos);
1325 enum htx_blk_type type = htx_get_blk_type(blk);
1326 struct ist n;
Willy Tarreau79e57332018-10-02 16:01:16 +02001327
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001328 if (type == HTX_BLK_EOH)
1329 break;
1330 if (type != HTX_BLK_HDR)
1331 continue;
1332 n = htx_get_blk_name(htx, blk);
1333
1334 if (temp->data)
1335 temp->area[temp->data++] = del;
1336 chunk_memcat(temp, n.ptr, n.len);
1337 }
1338 }
1339 else {
1340 /* LEGACY version */
1341 struct hdr_idx *idx;
1342 struct hdr_ctx ctx;
1343 const struct http_msg *msg;
1344
1345 if (args && args->type == ARGT_STR)
1346 del = *args[0].data.str.area;
1347
Christopher Faulet89dc4992019-04-17 12:02:59 +02001348 CHECK_HTTP_MESSAGE_FIRST(chn);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001349
1350 idx = &smp->strm->txn->hdr_idx;
Christopher Faulet89dc4992019-04-17 12:02:59 +02001351 msg = (!(chn->flags & CF_ISRESP) ? &smp->strm->txn->req : &smp->strm->txn->rsp);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001352
1353 temp = get_trash_chunk();
1354
1355 ctx.idx = 0;
1356 while (http_find_next_header(ci_head(msg->chn), idx, &ctx)) {
1357 if (temp->data)
1358 temp->area[temp->data++] = del;
1359 memcpy(temp->area + temp->data, ctx.line, ctx.del);
1360 temp->data += ctx.del;
1361 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001362 }
1363
1364 smp->data.type = SMP_T_STR;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001365 smp->data.u.str = *temp;
Willy Tarreau79e57332018-10-02 16:01:16 +02001366 smp->flags = SMP_F_VOL_HDR;
1367 return 1;
1368}
1369
1370/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
1371 * Accepts an optional argument of type string containing the header field name,
1372 * and an optional argument of type signed or unsigned integer to request an
1373 * explicit occurrence of the header. Note that in the event of a missing name,
1374 * headers are considered from the first one.
1375 */
1376static int smp_fetch_hdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
1377{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001378 /* possible keywords: req.hdr / hdr, res.hdr / shdr */
1379 struct channel *chn = ((kw[0] == 'h' || kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Willy Tarreau79e57332018-10-02 16:01:16 +02001380 int occ = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001381
Christopher Faulet46575cd2019-04-17 11:40:30 +02001382 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001383 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +02001384 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001385 struct http_hdr_ctx *ctx = smp->ctx.a[0];
1386 struct ist name;
1387
1388 if (!ctx) {
1389 /* first call */
1390 ctx = &static_http_hdr_ctx;
1391 ctx->blk = NULL;
1392 smp->ctx.a[0] = ctx;
1393 }
1394
1395 if (args) {
1396 if (args[0].type != ARGT_STR)
1397 return 0;
1398 name.ptr = args[0].data.str.area;
1399 name.len = args[0].data.str.data;
1400
1401 if (args[1].type == ARGT_SINT)
1402 occ = args[1].data.sint;
1403 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001404
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001405 if (!htx)
Willy Tarreau79e57332018-10-02 16:01:16 +02001406 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001407
1408 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
1409 /* search for header from the beginning */
1410 ctx->blk = NULL;
1411
1412 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
1413 /* no explicit occurrence and single fetch => last header by default */
1414 occ = -1;
1415
1416 if (!occ)
1417 /* prepare to report multiple occurrences for ACL fetches */
1418 smp->flags |= SMP_F_NOT_LAST;
Willy Tarreau79e57332018-10-02 16:01:16 +02001419
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001420 smp->data.type = SMP_T_STR;
1421 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
1422 if (http_get_htx_hdr(htx, name, occ, ctx, &smp->data.u.str.area, &smp->data.u.str.data))
1423 return 1;
Willy Tarreau79e57332018-10-02 16:01:16 +02001424 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001425 else {
1426 /* LEGACY version */
1427 struct hdr_idx *idx;
1428 struct hdr_ctx *ctx = smp->ctx.a[0];
1429 const struct http_msg *msg;
1430 const char *name_str = NULL;
1431 int name_len = 0;
1432
1433 if (!ctx) {
1434 /* first call */
1435 ctx = &static_hdr_ctx;
1436 ctx->idx = 0;
1437 smp->ctx.a[0] = ctx;
1438 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001439
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001440 if (args) {
1441 if (args[0].type != ARGT_STR)
1442 return 0;
1443 name_str = args[0].data.str.area;
1444 name_len = args[0].data.str.data;
Willy Tarreau79e57332018-10-02 16:01:16 +02001445
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001446 if (args[1].type == ARGT_SINT)
1447 occ = args[1].data.sint;
1448 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001449
Christopher Faulet89dc4992019-04-17 12:02:59 +02001450 CHECK_HTTP_MESSAGE_FIRST(chn);
Willy Tarreau79e57332018-10-02 16:01:16 +02001451
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001452 idx = &smp->strm->txn->hdr_idx;
Christopher Faulet89dc4992019-04-17 12:02:59 +02001453 msg = (!(chn->flags & CF_ISRESP) ? &smp->strm->txn->req : &smp->strm->txn->rsp);
Willy Tarreau79e57332018-10-02 16:01:16 +02001454
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001455 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
1456 /* search for header from the beginning */
1457 ctx->idx = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001458
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001459 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
1460 /* no explicit occurrence and single fetch => last header by default */
1461 occ = -1;
1462
1463 if (!occ)
1464 /* prepare to report multiple occurrences for ACL fetches */
1465 smp->flags |= SMP_F_NOT_LAST;
1466
1467 smp->data.type = SMP_T_STR;
1468 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
1469 if (http_get_hdr(msg, name_str, name_len, idx, occ, ctx, &smp->data.u.str.area, &smp->data.u.str.data))
1470 return 1;
1471 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001472
1473 smp->flags &= ~SMP_F_NOT_LAST;
1474 return 0;
1475}
1476
Christopher Fauletc1f40dd2019-05-16 10:07:30 +02001477/* Same than smp_fetch_hdr() but only relies on the sample direction to choose
1478 * the right channel. So instead of duplicating the code, we just change the
1479 * keyword and then fallback on smp_fetch_hdr().
1480 */
1481static int smp_fetch_chn_hdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
1482{
1483 kw = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ ? "req.hdr" : "res.hdr");
1484 return smp_fetch_hdr(args, smp, kw, private);
1485}
1486
Willy Tarreau79e57332018-10-02 16:01:16 +02001487/* 6. Check on HTTP header count. The number of occurrences is returned.
1488 * Accepts exactly 1 argument of type string.
1489 */
1490static int smp_fetch_hdr_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
1491{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001492 /* possible keywords: req.hdr_cnt / hdr_cnt, res.hdr_cnt / shdr_cnt */
1493 struct channel *chn = ((kw[0] == 'h' || kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Willy Tarreau79e57332018-10-02 16:01:16 +02001494 int cnt;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001495
Christopher Faulet46575cd2019-04-17 11:40:30 +02001496 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001497 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +02001498 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001499 struct http_hdr_ctx ctx;
1500 struct ist name;
1501
1502 if (!htx)
1503 return 0;
1504
1505 if (args && args->type == ARGT_STR) {
1506 name.ptr = args->data.str.area;
1507 name.len = args->data.str.data;
Olivier Houcharde2c78cd2018-11-21 13:49:48 +01001508 } else {
1509 name.ptr = NULL;
1510 name.len = 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001511 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001512
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001513 ctx.blk = NULL;
1514 cnt = 0;
1515 while (http_find_header(htx, name, &ctx, 0))
1516 cnt++;
Willy Tarreau79e57332018-10-02 16:01:16 +02001517 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001518 else {
1519 /* LEGACY version */
1520 struct hdr_idx *idx;
1521 struct hdr_ctx ctx;
1522 const struct http_msg *msg;
1523 const char *name = NULL;
1524 int len = 0;
1525
1526 if (args && args->type == ARGT_STR) {
1527 name = args->data.str.area;
1528 len = args->data.str.data;
1529 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001530
Christopher Faulet89dc4992019-04-17 12:02:59 +02001531 CHECK_HTTP_MESSAGE_FIRST(chn);
Willy Tarreau79e57332018-10-02 16:01:16 +02001532
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001533 idx = &smp->strm->txn->hdr_idx;
Christopher Faulet89dc4992019-04-17 12:02:59 +02001534 msg = (!(chn->flags & CF_ISRESP) ? &smp->strm->txn->req : &smp->strm->txn->rsp);
Willy Tarreau79e57332018-10-02 16:01:16 +02001535
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001536 ctx.idx = 0;
1537 cnt = 0;
1538 while (http_find_header2(name, len, ci_head(msg->chn), idx, &ctx))
1539 cnt++;
1540 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001541
1542 smp->data.type = SMP_T_SINT;
1543 smp->data.u.sint = cnt;
1544 smp->flags = SMP_F_VOL_HDR;
1545 return 1;
1546}
1547
1548/* Fetch an HTTP header's integer value. The integer value is returned. It
1549 * takes a mandatory argument of type string and an optional one of type int
1550 * to designate a specific occurrence. It returns an unsigned integer, which
1551 * may or may not be appropriate for everything.
1552 */
1553static int smp_fetch_hdr_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
1554{
1555 int ret = smp_fetch_hdr(args, smp, kw, private);
1556
1557 if (ret > 0) {
1558 smp->data.type = SMP_T_SINT;
1559 smp->data.u.sint = strl2ic(smp->data.u.str.area,
1560 smp->data.u.str.data);
1561 }
1562
1563 return ret;
1564}
1565
1566/* Fetch an HTTP header's IP value. takes a mandatory argument of type string
1567 * and an optional one of type int to designate a specific occurrence.
1568 * It returns an IPv4 or IPv6 address.
1569 */
1570static int smp_fetch_hdr_ip(const struct arg *args, struct sample *smp, const char *kw, void *private)
1571{
1572 int ret;
Tim Duesterhus8ba978b2020-06-26 15:44:48 +02001573 struct buffer *temp = get_trash_chunk();
Willy Tarreau79e57332018-10-02 16:01:16 +02001574
1575 while ((ret = smp_fetch_hdr(args, smp, kw, private)) > 0) {
Tim Duesterhus8ba978b2020-06-26 15:44:48 +02001576 if (smp->data.u.str.data < temp->size - 1) {
1577 memcpy(temp->area, smp->data.u.str.area,
1578 smp->data.u.str.data);
1579 temp->area[smp->data.u.str.data] = '\0';
1580 if (url2ipv4((char *) temp->area, &smp->data.u.ipv4)) {
1581 smp->data.type = SMP_T_IPV4;
1582 break;
1583 } else if (inet_pton(AF_INET6, temp->area, &smp->data.u.ipv6)) {
1584 smp->data.type = SMP_T_IPV6;
1585 break;
Willy Tarreau79e57332018-10-02 16:01:16 +02001586 }
1587 }
1588
1589 /* if the header doesn't match an IP address, fetch next one */
1590 if (!(smp->flags & SMP_F_NOT_LAST))
1591 return 0;
1592 }
1593 return ret;
1594}
1595
1596/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
1597 * the first '/' after the possible hostname, and ends before the possible '?'.
1598 */
1599static int smp_fetch_path(const struct arg *args, struct sample *smp, const char *kw, void *private)
1600{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001601 struct channel *chn = SMP_REQ_CHN(smp);
1602
Christopher Faulet46575cd2019-04-17 11:40:30 +02001603 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001604 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +02001605 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001606 struct htx_sl *sl;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001607 struct ist path;
1608 size_t len;
Willy Tarreau79e57332018-10-02 16:01:16 +02001609
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001610 if (!htx)
1611 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001612
Christopher Faulet297fbb42019-05-13 14:41:27 +02001613 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001614 path = http_get_path(htx_sl_req_uri(sl));
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001615 if (!path.ptr)
1616 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001617
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001618 for (len = 0; len < path.len && *(path.ptr + len) != '?'; len++)
Dragan Dosen8861e1c2019-02-12 19:50:31 +01001619 ;
Willy Tarreau79e57332018-10-02 16:01:16 +02001620
Dragan Dosen8861e1c2019-02-12 19:50:31 +01001621 /* OK, we got the '/' ! */
1622 smp->data.type = SMP_T_STR;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001623 smp->data.u.str.area = path.ptr;
1624 smp->data.u.str.data = len;
1625 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
1626 }
1627 else {
1628 struct http_txn *txn;
1629 char *ptr, *end;
Willy Tarreau79e57332018-10-02 16:01:16 +02001630
Christopher Faulet89dc4992019-04-17 12:02:59 +02001631 CHECK_HTTP_MESSAGE_FIRST(chn);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001632
1633 txn = smp->strm->txn;
Christopher Faulet89dc4992019-04-17 12:02:59 +02001634 end = ci_head(chn) + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001635 ptr = http_txn_get_path(txn);
1636 if (!ptr)
1637 return 0;
1638
1639 /* OK, we got the '/' ! */
1640 smp->data.type = SMP_T_STR;
1641 smp->data.u.str.area = ptr;
1642
1643 while (ptr < end && *ptr != '?')
1644 ptr++;
1645
1646 smp->data.u.str.data = ptr - smp->data.u.str.area;
1647 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
1648 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001649 return 1;
1650}
1651
1652/* This produces a concatenation of the first occurrence of the Host header
1653 * followed by the path component if it begins with a slash ('/'). This means
1654 * that '*' will not be added, resulting in exactly the first Host entry.
1655 * If no Host header is found, then the path is returned as-is. The returned
1656 * value is stored in the trash so it does not need to be marked constant.
1657 * The returned sample is of type string.
1658 */
1659static int smp_fetch_base(const struct arg *args, struct sample *smp, const char *kw, void *private)
1660{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001661 struct channel *chn = SMP_REQ_CHN(smp);
Willy Tarreau79e57332018-10-02 16:01:16 +02001662 struct buffer *temp;
1663
Christopher Faulet46575cd2019-04-17 11:40:30 +02001664 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001665 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +02001666 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001667 struct htx_sl *sl;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001668 struct http_hdr_ctx ctx;
1669 struct ist path;
Willy Tarreau79e57332018-10-02 16:01:16 +02001670
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001671 if (!htx)
1672 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001673
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001674 ctx.blk = NULL;
1675 if (!http_find_header(htx, ist("Host"), &ctx, 0) || !ctx.value.len)
1676 return smp_fetch_path(args, smp, kw, private);
Willy Tarreau79e57332018-10-02 16:01:16 +02001677
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001678 /* OK we have the header value in ctx.value */
1679 temp = get_trash_chunk();
1680 chunk_memcat(temp, ctx.value.ptr, ctx.value.len);
1681
1682 /* now retrieve the path */
Christopher Faulet297fbb42019-05-13 14:41:27 +02001683 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001684 path = http_get_path(htx_sl_req_uri(sl));
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001685 if (path.ptr) {
1686 size_t len;
1687
Dragan Dosen8861e1c2019-02-12 19:50:31 +01001688 for (len = 0; len < path.len && *(path.ptr + len) != '?'; len++)
1689 ;
1690
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001691 if (len && *(path.ptr) == '/')
1692 chunk_memcat(temp, path.ptr, len);
1693 }
1694
1695 smp->data.type = SMP_T_STR;
1696 smp->data.u.str = *temp;
1697 }
1698 else {
1699 /* LEGACY version */
1700 struct http_txn *txn;
1701 char *ptr, *end, *beg;
1702 struct hdr_ctx ctx;
1703
Christopher Faulet89dc4992019-04-17 12:02:59 +02001704 CHECK_HTTP_MESSAGE_FIRST(chn);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001705
1706 txn = smp->strm->txn;
1707 ctx.idx = 0;
Christopher Faulet89dc4992019-04-17 12:02:59 +02001708 if (!http_find_header2("Host", 4, ci_head(chn), &txn->hdr_idx, &ctx) || !ctx.vlen)
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001709 return smp_fetch_path(args, smp, kw, private);
1710
1711 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
1712 temp = get_trash_chunk();
1713 memcpy(temp->area, ctx.line + ctx.val, ctx.vlen);
1714 smp->data.type = SMP_T_STR;
1715 smp->data.u.str.area = temp->area;
1716 smp->data.u.str.data = ctx.vlen;
Willy Tarreau79e57332018-10-02 16:01:16 +02001717
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001718 /* now retrieve the path */
Christopher Faulet89dc4992019-04-17 12:02:59 +02001719 end = ci_head(chn) + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001720 beg = http_txn_get_path(txn);
1721 if (!beg)
1722 beg = end;
Willy Tarreau79e57332018-10-02 16:01:16 +02001723
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001724 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
1725
1726 if (beg < ptr && *beg == '/') {
1727 memcpy(smp->data.u.str.area + smp->data.u.str.data, beg,
1728 ptr - beg);
1729 smp->data.u.str.data += ptr - beg;
1730 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001731 }
1732
1733 smp->flags = SMP_F_VOL_1ST;
1734 return 1;
1735}
1736
1737/* This produces a 32-bit hash of the concatenation of the first occurrence of
1738 * the Host header followed by the path component if it begins with a slash ('/').
1739 * This means that '*' will not be added, resulting in exactly the first Host
1740 * entry. If no Host header is found, then the path is used. The resulting value
1741 * is hashed using the path hash followed by a full avalanche hash and provides a
1742 * 32-bit integer value. This fetch is useful for tracking per-path activity on
1743 * high-traffic sites without having to store whole paths.
1744 */
1745static int smp_fetch_base32(const struct arg *args, struct sample *smp, const char *kw, void *private)
1746{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001747 struct channel *chn = SMP_REQ_CHN(smp);
Willy Tarreau79e57332018-10-02 16:01:16 +02001748 unsigned int hash = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001749
Christopher Faulet46575cd2019-04-17 11:40:30 +02001750 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001751 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +02001752 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001753 struct htx_sl *sl;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001754 struct http_hdr_ctx ctx;
1755 struct ist path;
Willy Tarreau79e57332018-10-02 16:01:16 +02001756
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001757 if (!htx)
1758 return 0;
1759
1760 ctx.blk = NULL;
Dragan Dosen8861e1c2019-02-12 19:50:31 +01001761 if (http_find_header(htx, ist("Host"), &ctx, 0)) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001762 /* OK we have the header value in ctx.value */
1763 while (ctx.value.len--)
1764 hash = *(ctx.value.ptr++) + (hash << 6) + (hash << 16) - hash;
1765 }
1766
1767 /* now retrieve the path */
Christopher Faulet297fbb42019-05-13 14:41:27 +02001768 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001769 path = http_get_path(htx_sl_req_uri(sl));
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001770 if (path.ptr) {
1771 size_t len;
1772
Dragan Dosen8861e1c2019-02-12 19:50:31 +01001773 for (len = 0; len < path.len && *(path.ptr + len) != '?'; len++)
1774 ;
1775
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001776 if (len && *(path.ptr) == '/') {
1777 while (len--)
1778 hash = *(path.ptr++) + (hash << 6) + (hash << 16) - hash;
1779 }
1780 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001781 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001782 else {
1783 /* LEGACY version */
1784 struct http_txn *txn;
1785 struct hdr_ctx ctx;
1786 char *ptr, *beg, *end;
1787 int len;
1788
Christopher Faulet89dc4992019-04-17 12:02:59 +02001789 CHECK_HTTP_MESSAGE_FIRST(chn);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001790
1791 txn = smp->strm->txn;
1792 ctx.idx = 0;
Christopher Faulet89dc4992019-04-17 12:02:59 +02001793 if (http_find_header2("Host", 4, ci_head(chn), &txn->hdr_idx, &ctx)) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001794 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
1795 ptr = ctx.line + ctx.val;
1796 len = ctx.vlen;
1797 while (len--)
1798 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
1799 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001800
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001801 /* now retrieve the path */
Christopher Faulet89dc4992019-04-17 12:02:59 +02001802 end = ci_head(chn) + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001803 beg = http_txn_get_path(txn);
1804 if (!beg)
1805 beg = end;
Willy Tarreau79e57332018-10-02 16:01:16 +02001806
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001807 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
Willy Tarreau79e57332018-10-02 16:01:16 +02001808
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001809 if (beg < ptr && *beg == '/') {
1810 while (beg < ptr)
1811 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
1812 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001813 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001814
Willy Tarreau79e57332018-10-02 16:01:16 +02001815 hash = full_hash(hash);
1816
1817 smp->data.type = SMP_T_SINT;
1818 smp->data.u.sint = hash;
1819 smp->flags = SMP_F_VOL_1ST;
1820 return 1;
1821}
1822
1823/* This concatenates the source address with the 32-bit hash of the Host and
1824 * path as returned by smp_fetch_base32(). The idea is to have per-source and
1825 * per-path counters. The result is a binary block from 8 to 20 bytes depending
1826 * on the source address length. The path hash is stored before the address so
1827 * that in environments where IPv6 is insignificant, truncating the output to
1828 * 8 bytes would still work.
1829 */
1830static int smp_fetch_base32_src(const struct arg *args, struct sample *smp, const char *kw, void *private)
1831{
1832 struct buffer *temp;
1833 struct connection *cli_conn = objt_conn(smp->sess->origin);
1834
1835 if (!cli_conn)
1836 return 0;
1837
1838 if (!smp_fetch_base32(args, smp, kw, private))
1839 return 0;
1840
1841 temp = get_trash_chunk();
1842 *(unsigned int *) temp->area = htonl(smp->data.u.sint);
1843 temp->data += sizeof(unsigned int);
1844
1845 switch (cli_conn->addr.from.ss_family) {
1846 case AF_INET:
1847 memcpy(temp->area + temp->data,
1848 &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr,
1849 4);
1850 temp->data += 4;
1851 break;
1852 case AF_INET6:
1853 memcpy(temp->area + temp->data,
1854 &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr,
1855 16);
1856 temp->data += 16;
1857 break;
1858 default:
1859 return 0;
1860 }
1861
1862 smp->data.u.str = *temp;
1863 smp->data.type = SMP_T_BIN;
1864 return 1;
1865}
1866
1867/* Extracts the query string, which comes after the question mark '?'. If no
1868 * question mark is found, nothing is returned. Otherwise it returns a sample
1869 * of type string carrying the whole query string.
1870 */
1871static int smp_fetch_query(const struct arg *args, struct sample *smp, const char *kw, void *private)
1872{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001873 struct channel *chn = SMP_REQ_CHN(smp);
Willy Tarreau79e57332018-10-02 16:01:16 +02001874 char *ptr, *end;
1875
Christopher Faulet46575cd2019-04-17 11:40:30 +02001876 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001877 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +02001878 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001879 struct htx_sl *sl;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001880
1881 if (!htx)
1882 return 0;
1883
Christopher Faulet297fbb42019-05-13 14:41:27 +02001884 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001885 ptr = HTX_SL_REQ_UPTR(sl);
1886 end = HTX_SL_REQ_UPTR(sl) + HTX_SL_REQ_ULEN(sl);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001887 }
1888 else {
1889 /* LEGACY version */
1890 struct http_txn *txn;
1891
Christopher Faulet89dc4992019-04-17 12:02:59 +02001892 CHECK_HTTP_MESSAGE_FIRST(chn);
Willy Tarreau79e57332018-10-02 16:01:16 +02001893
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001894 txn = smp->strm->txn;
Christopher Faulet89dc4992019-04-17 12:02:59 +02001895 ptr = ci_head(chn) + txn->req.sl.rq.u;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001896 end = ptr + txn->req.sl.rq.u_l;
1897 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001898
1899 /* look up the '?' */
1900 do {
1901 if (ptr == end)
1902 return 0;
1903 } while (*ptr++ != '?');
1904
1905 smp->data.type = SMP_T_STR;
1906 smp->data.u.str.area = ptr;
1907 smp->data.u.str.data = end - ptr;
1908 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
1909 return 1;
1910}
1911
1912static int smp_fetch_proto_http(const struct arg *args, struct sample *smp, const char *kw, void *private)
1913{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001914 struct channel *chn = SMP_REQ_CHN(smp);
1915
Christopher Faulet46575cd2019-04-17 11:40:30 +02001916 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001917 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +02001918 struct htx *htx = smp_prefetch_htx(smp, chn, 0);
Willy Tarreau79e57332018-10-02 16:01:16 +02001919
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001920 if (!htx)
1921 return 0;
1922 }
1923 else {
1924 /* LEGACY version */
Willy Tarreau79e57332018-10-02 16:01:16 +02001925
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001926 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
1927 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
1928 */
Christopher Faulet89dc4992019-04-17 12:02:59 +02001929 CHECK_HTTP_MESSAGE_FIRST_PERM(chn);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001930 }
1931 smp->data.type = SMP_T_BOOL;
Willy Tarreau79e57332018-10-02 16:01:16 +02001932 smp->data.u.sint = 1;
1933 return 1;
1934}
1935
1936/* return a valid test if the current request is the first one on the connection */
1937static int smp_fetch_http_first_req(const struct arg *args, struct sample *smp, const char *kw, void *private)
1938{
Willy Tarreaud095c672020-04-29 11:52:13 +02001939 if (!smp->strm)
1940 return 0;
1941
Willy Tarreau79e57332018-10-02 16:01:16 +02001942 smp->data.type = SMP_T_BOOL;
1943 smp->data.u.sint = !(smp->strm->txn->flags & TX_NOT_FIRST);
1944 return 1;
1945}
1946
1947/* Accepts exactly 1 argument of type userlist */
1948static int smp_fetch_http_auth(const struct arg *args, struct sample *smp, const char *kw, void *private)
1949{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001950 struct channel *chn = SMP_REQ_CHN(smp);
Willy Tarreau79e57332018-10-02 16:01:16 +02001951
1952 if (!args || args->type != ARGT_USR)
1953 return 0;
1954
Christopher Faulet46575cd2019-04-17 11:40:30 +02001955 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001956 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +02001957 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Willy Tarreau79e57332018-10-02 16:01:16 +02001958
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001959 if (!htx)
1960 return 0;
Christopher Faulete98411b2019-07-15 13:58:29 +02001961 if (!get_http_auth(smp, htx))
1962 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001963 }
1964 else {
1965 /* LEGACY version */
Christopher Faulet89dc4992019-04-17 12:02:59 +02001966 CHECK_HTTP_MESSAGE_FIRST(chn);
Christopher Faulete98411b2019-07-15 13:58:29 +02001967 if (!get_http_auth(smp, NULL))
1968 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001969 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001970
1971 smp->data.type = SMP_T_BOOL;
1972 smp->data.u.sint = check_user(args->data.usr, smp->strm->txn->auth.user,
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001973 smp->strm->txn->auth.pass);
Willy Tarreau79e57332018-10-02 16:01:16 +02001974 return 1;
1975}
1976
1977/* Accepts exactly 1 argument of type userlist */
1978static int smp_fetch_http_auth_grp(const struct arg *args, struct sample *smp, const char *kw, void *private)
1979{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001980 struct channel *chn = SMP_REQ_CHN(smp);
1981
Willy Tarreau79e57332018-10-02 16:01:16 +02001982 if (!args || args->type != ARGT_USR)
1983 return 0;
1984
Christopher Faulet46575cd2019-04-17 11:40:30 +02001985 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001986 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +02001987 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001988
1989 if (!htx)
1990 return 0;
Christopher Faulete98411b2019-07-15 13:58:29 +02001991 if (!get_http_auth(smp, htx))
1992 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001993 }
1994 else {
1995 /* LEGACY version */
Christopher Faulet89dc4992019-04-17 12:02:59 +02001996 CHECK_HTTP_MESSAGE_FIRST(chn);
Christopher Faulete98411b2019-07-15 13:58:29 +02001997 if (!get_http_auth(smp, NULL))
1998 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001999 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002000
Willy Tarreau79e57332018-10-02 16:01:16 +02002001 /* if the user does not belong to the userlist or has a wrong password,
2002 * report that it unconditionally does not match. Otherwise we return
2003 * a string containing the username.
2004 */
2005 if (!check_user(args->data.usr, smp->strm->txn->auth.user,
2006 smp->strm->txn->auth.pass))
2007 return 0;
2008
2009 /* pat_match_auth() will need the user list */
2010 smp->ctx.a[0] = args->data.usr;
2011
2012 smp->data.type = SMP_T_STR;
2013 smp->flags = SMP_F_CONST;
2014 smp->data.u.str.area = smp->strm->txn->auth.user;
2015 smp->data.u.str.data = strlen(smp->strm->txn->auth.user);
2016
2017 return 1;
2018}
2019
2020/* Fetch a captured HTTP request header. The index is the position of
2021 * the "capture" option in the configuration file
2022 */
2023static int smp_fetch_capture_req_hdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
2024{
Willy Tarreau79ed4672020-04-29 11:44:54 +02002025 struct proxy *fe;
Willy Tarreau79e57332018-10-02 16:01:16 +02002026 int idx;
2027
2028 if (!args || args->type != ARGT_SINT)
2029 return 0;
2030
Willy Tarreau79ed4672020-04-29 11:44:54 +02002031 if (!smp->strm)
2032 return 0;
2033
2034 fe = strm_fe(smp->strm);
Willy Tarreau79e57332018-10-02 16:01:16 +02002035 idx = args->data.sint;
2036
2037 if (idx > (fe->nb_req_cap - 1) || smp->strm->req_cap == NULL || smp->strm->req_cap[idx] == NULL)
2038 return 0;
2039
2040 smp->data.type = SMP_T_STR;
2041 smp->flags |= SMP_F_CONST;
2042 smp->data.u.str.area = smp->strm->req_cap[idx];
2043 smp->data.u.str.data = strlen(smp->strm->req_cap[idx]);
2044
2045 return 1;
2046}
2047
2048/* Fetch a captured HTTP response header. The index is the position of
2049 * the "capture" option in the configuration file
2050 */
2051static int smp_fetch_capture_res_hdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
2052{
Willy Tarreau79ed4672020-04-29 11:44:54 +02002053 struct proxy *fe;
Willy Tarreau79e57332018-10-02 16:01:16 +02002054 int idx;
2055
2056 if (!args || args->type != ARGT_SINT)
2057 return 0;
2058
Willy Tarreau79ed4672020-04-29 11:44:54 +02002059 if (!smp->strm)
2060 return 0;
2061
2062 fe = strm_fe(smp->strm);
Willy Tarreau79e57332018-10-02 16:01:16 +02002063 idx = args->data.sint;
2064
2065 if (idx > (fe->nb_rsp_cap - 1) || smp->strm->res_cap == NULL || smp->strm->res_cap[idx] == NULL)
2066 return 0;
2067
2068 smp->data.type = SMP_T_STR;
2069 smp->flags |= SMP_F_CONST;
2070 smp->data.u.str.area = smp->strm->res_cap[idx];
2071 smp->data.u.str.data = strlen(smp->strm->res_cap[idx]);
2072
2073 return 1;
2074}
2075
2076/* Extracts the METHOD in the HTTP request, the txn->uri should be filled before the call */
2077static int smp_fetch_capture_req_method(const struct arg *args, struct sample *smp, const char *kw, void *private)
2078{
2079 struct buffer *temp;
Willy Tarreau79ed4672020-04-29 11:44:54 +02002080 struct http_txn *txn;
Willy Tarreau79e57332018-10-02 16:01:16 +02002081 char *ptr;
2082
Willy Tarreau79ed4672020-04-29 11:44:54 +02002083 if (!smp->strm)
2084 return 0;
2085
2086 txn = smp->strm->txn;
Willy Tarreau79e57332018-10-02 16:01:16 +02002087 if (!txn || !txn->uri)
2088 return 0;
2089
2090 ptr = txn->uri;
2091
2092 while (*ptr != ' ' && *ptr != '\0') /* find first space */
2093 ptr++;
2094
2095 temp = get_trash_chunk();
2096 temp->area = txn->uri;
2097 temp->data = ptr - txn->uri;
2098 smp->data.u.str = *temp;
2099 smp->data.type = SMP_T_STR;
2100 smp->flags = SMP_F_CONST;
2101
2102 return 1;
2103
2104}
2105
2106/* Extracts the path in the HTTP request, the txn->uri should be filled before the call */
2107static int smp_fetch_capture_req_uri(const struct arg *args, struct sample *smp, const char *kw, void *private)
2108{
Willy Tarreau79ed4672020-04-29 11:44:54 +02002109 struct http_txn *txn;
Willy Tarreau79e57332018-10-02 16:01:16 +02002110 struct ist path;
2111 const char *ptr;
2112
Willy Tarreau79ed4672020-04-29 11:44:54 +02002113 if (!smp->strm)
2114 return 0;
2115
2116 txn = smp->strm->txn;
Willy Tarreau79e57332018-10-02 16:01:16 +02002117 if (!txn || !txn->uri)
2118 return 0;
2119
2120 ptr = txn->uri;
2121
2122 while (*ptr != ' ' && *ptr != '\0') /* find first space */
2123 ptr++;
2124
2125 if (!*ptr)
2126 return 0;
2127
Christopher Faulet78337bb2018-11-15 14:35:18 +01002128 /* skip the first space and find space after URI */
2129 path = ist2(++ptr, 0);
2130 while (*ptr != ' ' && *ptr != '\0')
2131 ptr++;
2132 path.len = ptr - path.ptr;
Willy Tarreau79e57332018-10-02 16:01:16 +02002133
Christopher Faulet78337bb2018-11-15 14:35:18 +01002134 path = http_get_path(path);
Willy Tarreau79e57332018-10-02 16:01:16 +02002135 if (!path.ptr)
2136 return 0;
2137
2138 smp->data.u.str.area = path.ptr;
2139 smp->data.u.str.data = path.len;
2140 smp->data.type = SMP_T_STR;
2141 smp->flags = SMP_F_CONST;
2142
2143 return 1;
2144}
2145
2146/* Retrieves the HTTP version from the request (either 1.0 or 1.1) and emits it
2147 * as a string (either "HTTP/1.0" or "HTTP/1.1").
2148 */
2149static int smp_fetch_capture_req_ver(const struct arg *args, struct sample *smp, const char *kw, void *private)
2150{
Willy Tarreau79ed4672020-04-29 11:44:54 +02002151 struct http_txn *txn;
2152
2153 if (!smp->strm)
2154 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02002155
Willy Tarreau79ed4672020-04-29 11:44:54 +02002156 txn = smp->strm->txn;
Willy Tarreau79e57332018-10-02 16:01:16 +02002157 if (!txn || txn->req.msg_state < HTTP_MSG_HDR_FIRST)
2158 return 0;
2159
2160 if (txn->req.flags & HTTP_MSGF_VER_11)
2161 smp->data.u.str.area = "HTTP/1.1";
2162 else
2163 smp->data.u.str.area = "HTTP/1.0";
2164
2165 smp->data.u.str.data = 8;
2166 smp->data.type = SMP_T_STR;
2167 smp->flags = SMP_F_CONST;
2168 return 1;
2169
2170}
2171
2172/* Retrieves the HTTP version from the response (either 1.0 or 1.1) and emits it
2173 * as a string (either "HTTP/1.0" or "HTTP/1.1").
2174 */
2175static int smp_fetch_capture_res_ver(const struct arg *args, struct sample *smp, const char *kw, void *private)
2176{
Willy Tarreau79ed4672020-04-29 11:44:54 +02002177 struct http_txn *txn;
2178
2179 if (!smp->strm)
2180 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02002181
Willy Tarreau79ed4672020-04-29 11:44:54 +02002182 txn = smp->strm->txn;
Willy Tarreau79e57332018-10-02 16:01:16 +02002183 if (!txn || txn->rsp.msg_state < HTTP_MSG_HDR_FIRST)
2184 return 0;
2185
2186 if (txn->rsp.flags & HTTP_MSGF_VER_11)
2187 smp->data.u.str.area = "HTTP/1.1";
2188 else
2189 smp->data.u.str.area = "HTTP/1.0";
2190
2191 smp->data.u.str.data = 8;
2192 smp->data.type = SMP_T_STR;
2193 smp->flags = SMP_F_CONST;
2194 return 1;
2195
2196}
2197
2198/* Iterate over all cookies present in a message. The context is stored in
2199 * smp->ctx.a[0] for the in-header position, smp->ctx.a[1] for the
2200 * end-of-header-value, and smp->ctx.a[2] for the hdr_ctx. Depending on
2201 * the direction, multiple cookies may be parsed on the same line or not.
Maciej Zdeb22dc68b2020-11-13 09:38:06 +00002202 * If provided, the searched cookie name is in args, in args->data.str. If
2203 * the input options indicate that no iterating is desired, then only last
2204 * value is fetched if any. If no cookie name is provided, the first cookie
2205 * value found is fetched. The returned sample is of type CSTR. Can be used
2206 * to parse cookies in other files.
Willy Tarreau79e57332018-10-02 16:01:16 +02002207 */
2208static int smp_fetch_cookie(const struct arg *args, struct sample *smp, const char *kw, void *private)
2209{
Christopher Faulet89dc4992019-04-17 12:02:59 +02002210 /* possible keywords: req.cookie / cookie / cook, res.cookie / scook / set-cookie */
2211 struct channel *chn = ((kw[0] == 'c' || kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Christopher Faulet08f475b2020-11-13 13:41:04 +01002212 char *cook = NULL;
2213 size_t cook_l = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02002214 int found = 0;
2215
Christopher Faulet08f475b2020-11-13 13:41:04 +01002216 if (args && args->type == ARGT_STR) {
2217 cook = args->data.str.area;
2218 cook_l = args->data.str.data;
2219 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002220
Christopher Faulet46575cd2019-04-17 11:40:30 +02002221 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002222 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +02002223 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002224 struct http_hdr_ctx *ctx = smp->ctx.a[2];
2225 struct ist hdr;
Willy Tarreau79e57332018-10-02 16:01:16 +02002226
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002227 if (!ctx) {
2228 /* first call */
2229 ctx = &static_http_hdr_ctx;
2230 ctx->blk = NULL;
2231 smp->ctx.a[2] = ctx;
2232 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002233
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002234 if (!htx)
2235 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02002236
Christopher Faulet89dc4992019-04-17 12:02:59 +02002237 hdr = (!(chn->flags & CF_ISRESP) ? ist("Cookie") : ist("Set-Cookie"));
Willy Tarreau79e57332018-10-02 16:01:16 +02002238
Maciej Zdeb22dc68b2020-11-13 09:38:06 +00002239 /* OK so basically here, either we want only one value or we want to
2240 * iterate over all of them and we fetch the next one. In this last case
2241 * SMP_OPT_ITERATE option is set.
Willy Tarreau79e57332018-10-02 16:01:16 +02002242 */
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002243
2244 if (!(smp->flags & SMP_F_NOT_LAST)) {
2245 /* search for the header from the beginning, we must first initialize
2246 * the search parameters.
2247 */
2248 smp->ctx.a[0] = NULL;
2249 ctx->blk = NULL;
2250 }
2251
2252 smp->flags |= SMP_F_VOL_HDR;
2253 while (1) {
2254 /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */
2255 if (!smp->ctx.a[0]) {
2256 if (!http_find_header(htx, hdr, ctx, 0))
2257 goto out;
2258
Christopher Faulet08f475b2020-11-13 13:41:04 +01002259 if (ctx->value.len < cook_l + 1)
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002260 continue;
2261
2262 smp->ctx.a[0] = ctx->value.ptr;
2263 smp->ctx.a[1] = smp->ctx.a[0] + ctx->value.len;
2264 }
2265
2266 smp->data.type = SMP_T_STR;
2267 smp->flags |= SMP_F_CONST;
2268 smp->ctx.a[0] = http_extract_cookie_value(smp->ctx.a[0], smp->ctx.a[1],
Christopher Faulet08f475b2020-11-13 13:41:04 +01002269 cook, cook_l,
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002270 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
2271 &smp->data.u.str.area,
2272 &smp->data.u.str.data);
2273 if (smp->ctx.a[0]) {
2274 found = 1;
Maciej Zdeb22dc68b2020-11-13 09:38:06 +00002275 if (smp->opt & SMP_OPT_ITERATE) {
2276 /* iterate on cookie value */
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002277 smp->flags |= SMP_F_NOT_LAST;
2278 return 1;
2279 }
Maciej Zdeb22dc68b2020-11-13 09:38:06 +00002280 if (args->data.str.data == 0) {
2281 /* No cookie name, first occurrence returned */
2282 break;
2283 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002284 }
2285 /* if we're looking for last occurrence, let's loop */
2286 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002287 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002288 else {
2289 /* LEGACY version */
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002290 struct hdr_idx *idx;
2291 struct hdr_ctx *ctx = smp->ctx.a[2];
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002292 const char *hdr_name;
2293 int hdr_name_len;
2294 char *sol;
Willy Tarreau79e57332018-10-02 16:01:16 +02002295
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002296 if (!ctx) {
2297 /* first call */
2298 ctx = &static_hdr_ctx;
2299 ctx->idx = 0;
2300 smp->ctx.a[2] = ctx;
2301 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002302
Christopher Faulet89dc4992019-04-17 12:02:59 +02002303 CHECK_HTTP_MESSAGE_FIRST(chn);
Willy Tarreau79e57332018-10-02 16:01:16 +02002304
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002305 idx = &smp->strm->txn->hdr_idx;
Christopher Faulet89dc4992019-04-17 12:02:59 +02002306 if (!(chn->flags & CF_ISRESP)) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002307 hdr_name = "Cookie";
2308 hdr_name_len = 6;
2309 } else {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002310 hdr_name = "Set-Cookie";
2311 hdr_name_len = 10;
Willy Tarreau79e57332018-10-02 16:01:16 +02002312 }
2313
Maciej Zdeb22dc68b2020-11-13 09:38:06 +00002314 /* OK so basically here, either we want only one value or we want to
2315 * iterate over all of them and we fetch the next one. In this last case
2316 * SMP_OPT_ITERATE option is set.
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002317 */
2318
Christopher Faulet89dc4992019-04-17 12:02:59 +02002319 sol = ci_head(chn);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002320 if (!(smp->flags & SMP_F_NOT_LAST)) {
2321 /* search for the header from the beginning, we must first initialize
2322 * the search parameters.
2323 */
2324 smp->ctx.a[0] = NULL;
2325 ctx->idx = 0;
2326 }
2327
2328 smp->flags |= SMP_F_VOL_HDR;
2329
2330 while (1) {
2331 /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */
2332 if (!smp->ctx.a[0]) {
2333 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, ctx))
2334 goto out;
2335
Christopher Faulet08f475b2020-11-13 13:41:04 +01002336 if (ctx->vlen < cook_l + 1)
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002337 continue;
2338
2339 smp->ctx.a[0] = ctx->line + ctx->val;
2340 smp->ctx.a[1] = smp->ctx.a[0] + ctx->vlen;
2341 }
2342
2343 smp->data.type = SMP_T_STR;
2344 smp->flags |= SMP_F_CONST;
2345 smp->ctx.a[0] = http_extract_cookie_value(smp->ctx.a[0], smp->ctx.a[1],
Christopher Faulet08f475b2020-11-13 13:41:04 +01002346 cook, cook_l,
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002347 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
2348 &smp->data.u.str.area, &smp->data.u.str.data);
2349 if (smp->ctx.a[0]) {
2350 found = 1;
Maciej Zdeb22dc68b2020-11-13 09:38:06 +00002351 if (smp->opt & SMP_OPT_ITERATE) {
2352 /* iterate on cookie value */
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002353 smp->flags |= SMP_F_NOT_LAST;
2354 return 1;
2355 }
Maciej Zdeb22dc68b2020-11-13 09:38:06 +00002356 if (args->data.str.data == 0) {
2357 /* No cookie name, first occurrence returned */
2358 break;
2359 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002360 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002361 /* if we're looking for last occurrence, let's loop */
Willy Tarreau79e57332018-10-02 16:01:16 +02002362 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002363 }
2364 /* all cookie headers and values were scanned. If we're looking for the
2365 * last occurrence, we may return it now.
2366 */
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002367 out:
Willy Tarreau79e57332018-10-02 16:01:16 +02002368 smp->flags &= ~SMP_F_NOT_LAST;
2369 return found;
2370}
2371
Christopher Fauletc1f40dd2019-05-16 10:07:30 +02002372/* Same than smp_fetch_cookie() but only relies on the sample direction to
2373 * choose the right channel. So instead of duplicating the code, we just change
2374 * the keyword and then fallback on smp_fetch_cookie().
2375 */
2376static int smp_fetch_chn_cookie(const struct arg *args, struct sample *smp, const char *kw, void *private)
2377{
2378 kw = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ ? "req.cook" : "res.cook");
2379 return smp_fetch_cookie(args, smp, kw, private);
2380}
2381
Willy Tarreau79e57332018-10-02 16:01:16 +02002382/* Iterate over all cookies present in a request to count how many occurrences
2383 * match the name in args and args->data.str.len. If <multi> is non-null, then
2384 * multiple cookies may be parsed on the same line. The returned sample is of
2385 * type UINT. Accepts exactly 1 argument of type string.
2386 */
2387static int smp_fetch_cookie_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
2388{
Christopher Faulet89dc4992019-04-17 12:02:59 +02002389 /* possible keywords: req.cook_cnt / cook_cnt, res.cook_cnt / scook_cnt */
2390 struct channel *chn = ((kw[0] == 'c' || kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Willy Tarreau79e57332018-10-02 16:01:16 +02002391 char *val_beg, *val_end;
Christopher Faulet08f475b2020-11-13 13:41:04 +01002392 char *cook = NULL;
2393 size_t cook_l = 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002394 int cnt;
Willy Tarreau79e57332018-10-02 16:01:16 +02002395
Christopher Faulet08f475b2020-11-13 13:41:04 +01002396 if (args && args->type == ARGT_STR){
2397 cook = args->data.str.area;
2398 cook_l = args->data.str.data;
2399 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002400
Christopher Faulet46575cd2019-04-17 11:40:30 +02002401 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002402 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +02002403 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002404 struct http_hdr_ctx ctx;
2405 struct ist hdr;
2406
2407 if (!htx)
2408 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02002409
Christopher Faulet89dc4992019-04-17 12:02:59 +02002410 hdr = (!(chn->flags & CF_ISRESP) ? ist("Cookie") : ist("Set-Cookie"));
Willy Tarreau79e57332018-10-02 16:01:16 +02002411
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002412 val_end = val_beg = NULL;
2413 ctx.blk = NULL;
2414 cnt = 0;
2415 while (1) {
2416 /* Note: val_beg == NULL every time we need to fetch a new header */
2417 if (!val_beg) {
2418 if (!http_find_header(htx, hdr, &ctx, 0))
2419 break;
Willy Tarreau79e57332018-10-02 16:01:16 +02002420
Christopher Faulet08f475b2020-11-13 13:41:04 +01002421 if (ctx.value.len < cook_l + 1)
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002422 continue;
Willy Tarreau79e57332018-10-02 16:01:16 +02002423
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002424 val_beg = ctx.value.ptr;
2425 val_end = val_beg + ctx.value.len;
2426 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002427
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002428 smp->data.type = SMP_T_STR;
2429 smp->flags |= SMP_F_CONST;
2430 while ((val_beg = http_extract_cookie_value(val_beg, val_end,
Christopher Faulet08f475b2020-11-13 13:41:04 +01002431 cook, cook_l,
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002432 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
2433 &smp->data.u.str.area,
2434 &smp->data.u.str.data))) {
2435 cnt++;
2436 }
2437 }
2438 }
2439 else {
2440 /* LEGACY version */
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002441 struct hdr_idx *idx;
2442 struct hdr_ctx ctx;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002443 const char *hdr_name;
2444 int hdr_name_len;
2445 char *sol;
Willy Tarreau79e57332018-10-02 16:01:16 +02002446
Christopher Faulet89dc4992019-04-17 12:02:59 +02002447 CHECK_HTTP_MESSAGE_FIRST(chn);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002448
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002449 idx = &smp->strm->txn->hdr_idx;
Christopher Faulet89dc4992019-04-17 12:02:59 +02002450 if (!(chn->flags & CF_ISRESP)) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002451 hdr_name = "Cookie";
2452 hdr_name_len = 6;
2453 } else {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002454 hdr_name = "Set-Cookie";
2455 hdr_name_len = 10;
Willy Tarreau79e57332018-10-02 16:01:16 +02002456 }
2457
Christopher Faulet89dc4992019-04-17 12:02:59 +02002458 sol = ci_head(chn);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002459 val_end = val_beg = NULL;
2460 ctx.idx = 0;
2461 cnt = 0;
2462
2463 while (1) {
2464 /* Note: val_beg == NULL every time we need to fetch a new header */
2465 if (!val_beg) {
2466 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, &ctx))
2467 break;
2468
Christopher Faulet08f475b2020-11-13 13:41:04 +01002469 if (ctx.vlen < cook_l + 1)
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002470 continue;
2471
2472 val_beg = ctx.line + ctx.val;
2473 val_end = val_beg + ctx.vlen;
2474 }
2475
2476 smp->data.type = SMP_T_STR;
2477 smp->flags |= SMP_F_CONST;
2478 while ((val_beg = http_extract_cookie_value(val_beg, val_end,
Christopher Faulet08f475b2020-11-13 13:41:04 +01002479 cook, cook_l,
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002480 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
2481 &smp->data.u.str.area, &smp->data.u.str.data))) {
2482 cnt++;
2483 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002484 }
2485 }
2486
2487 smp->data.type = SMP_T_SINT;
2488 smp->data.u.sint = cnt;
2489 smp->flags |= SMP_F_VOL_HDR;
2490 return 1;
2491}
2492
2493/* Fetch an cookie's integer value. The integer value is returned. It
2494 * takes a mandatory argument of type string. It relies on smp_fetch_cookie().
2495 */
2496static int smp_fetch_cookie_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
2497{
2498 int ret = smp_fetch_cookie(args, smp, kw, private);
2499
2500 if (ret > 0) {
2501 smp->data.type = SMP_T_SINT;
2502 smp->data.u.sint = strl2ic(smp->data.u.str.area,
2503 smp->data.u.str.data);
2504 }
2505
2506 return ret;
2507}
2508
2509/************************************************************************/
2510/* The code below is dedicated to sample fetches */
2511/************************************************************************/
2512
2513/* This scans a URL-encoded query string. It takes an optionally wrapping
2514 * string whose first contigous chunk has its beginning in ctx->a[0] and end
2515 * in ctx->a[1], and the optional second part in (ctx->a[2]..ctx->a[3]). The
2516 * pointers are updated for next iteration before leaving.
2517 */
2518static int smp_fetch_param(char delim, const char *name, int name_len, const struct arg *args, struct sample *smp, const char *kw, void *private)
2519{
2520 const char *vstart, *vend;
2521 struct buffer *temp;
2522 const char **chunks = (const char **)smp->ctx.a;
2523
2524 if (!http_find_next_url_param(chunks, name, name_len,
2525 &vstart, &vend, delim))
2526 return 0;
2527
2528 /* Create sample. If the value is contiguous, return the pointer as CONST,
2529 * if the value is wrapped, copy-it in a buffer.
2530 */
2531 smp->data.type = SMP_T_STR;
2532 if (chunks[2] &&
2533 vstart >= chunks[0] && vstart <= chunks[1] &&
2534 vend >= chunks[2] && vend <= chunks[3]) {
2535 /* Wrapped case. */
2536 temp = get_trash_chunk();
2537 memcpy(temp->area, vstart, chunks[1] - vstart);
2538 memcpy(temp->area + ( chunks[1] - vstart ), chunks[2],
2539 vend - chunks[2]);
2540 smp->data.u.str.area = temp->area;
2541 smp->data.u.str.data = ( chunks[1] - vstart ) + ( vend - chunks[2] );
2542 } else {
2543 /* Contiguous case. */
2544 smp->data.u.str.area = (char *)vstart;
2545 smp->data.u.str.data = vend - vstart;
2546 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
2547 }
2548
2549 /* Update context, check wrapping. */
2550 chunks[0] = vend;
2551 if (chunks[2] && vend >= chunks[2] && vend <= chunks[3]) {
2552 chunks[1] = chunks[3];
2553 chunks[2] = NULL;
2554 }
2555
2556 if (chunks[0] < chunks[1])
2557 smp->flags |= SMP_F_NOT_LAST;
2558
2559 return 1;
2560}
2561
2562/* This function iterates over each parameter of the query string. It uses
2563 * ctx->a[0] and ctx->a[1] to store the beginning and end of the current
2564 * parameter. Since it uses smp_fetch_param(), ctx->a[2..3] are both NULL.
2565 * An optional parameter name is passed in args[0], otherwise any parameter is
2566 * considered. It supports an optional delimiter argument for the beginning of
2567 * the string in args[1], which defaults to "?".
2568 */
2569static int smp_fetch_url_param(const struct arg *args, struct sample *smp, const char *kw, void *private)
2570{
Christopher Faulet89dc4992019-04-17 12:02:59 +02002571 struct channel *chn = SMP_REQ_CHN(smp);
Willy Tarreau79e57332018-10-02 16:01:16 +02002572 char delim = '?';
2573 const char *name;
2574 int name_len;
2575
2576 if (!args ||
2577 (args[0].type && args[0].type != ARGT_STR) ||
2578 (args[1].type && args[1].type != ARGT_STR))
2579 return 0;
2580
2581 name = "";
2582 name_len = 0;
2583 if (args->type == ARGT_STR) {
2584 name = args->data.str.area;
2585 name_len = args->data.str.data;
2586 }
2587
2588 if (args[1].type)
2589 delim = *args[1].data.str.area;
2590
2591 if (!smp->ctx.a[0]) { // first call, find the query string
Christopher Faulet46575cd2019-04-17 11:40:30 +02002592 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002593 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +02002594 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01002595 struct htx_sl *sl;
Willy Tarreau79e57332018-10-02 16:01:16 +02002596
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002597 if (!htx)
2598 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02002599
Christopher Faulet297fbb42019-05-13 14:41:27 +02002600 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01002601 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 +02002602 if (!smp->ctx.a[0])
2603 return 0;
2604
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01002605 smp->ctx.a[1] = HTX_SL_REQ_UPTR(sl) + HTX_SL_REQ_ULEN(sl);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002606 }
2607 else {
2608 /* LEGACY version */
2609 struct http_msg *msg;
2610
Christopher Faulet89dc4992019-04-17 12:02:59 +02002611 CHECK_HTTP_MESSAGE_FIRST(chn);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002612
2613 msg = &smp->strm->txn->req;
2614
Christopher Faulet89dc4992019-04-17 12:02:59 +02002615 smp->ctx.a[0] = http_find_param_list(ci_head(chn) + msg->sl.rq.u,
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002616 msg->sl.rq.u_l, delim);
2617 if (!smp->ctx.a[0])
2618 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02002619
Christopher Faulet89dc4992019-04-17 12:02:59 +02002620 smp->ctx.a[1] = ci_head(chn) + msg->sl.rq.u + msg->sl.rq.u_l;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002621 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002622
2623 /* Assume that the context is filled with NULL pointer
2624 * before the first call.
2625 * smp->ctx.a[2] = NULL;
2626 * smp->ctx.a[3] = NULL;
2627 */
2628 }
2629
2630 return smp_fetch_param(delim, name, name_len, args, smp, kw, private);
2631}
2632
2633/* This function iterates over each parameter of the body. This requires
2634 * that the body has been waited for using http-buffer-request. It uses
2635 * ctx->a[0] and ctx->a[1] to store the beginning and end of the first
2636 * contigous part of the body, and optionally ctx->a[2..3] to reference the
2637 * optional second part if the body wraps at the end of the buffer. An optional
2638 * parameter name is passed in args[0], otherwise any parameter is considered.
2639 */
2640static int smp_fetch_body_param(const struct arg *args, struct sample *smp, const char *kw, void *private)
2641{
Christopher Faulet89dc4992019-04-17 12:02:59 +02002642 struct channel *chn = SMP_REQ_CHN(smp);
Willy Tarreau79e57332018-10-02 16:01:16 +02002643 const char *name;
2644 int name_len;
2645
2646 if (!args || (args[0].type && args[0].type != ARGT_STR))
2647 return 0;
2648
2649 name = "";
2650 name_len = 0;
2651 if (args[0].type == ARGT_STR) {
2652 name = args[0].data.str.area;
2653 name_len = args[0].data.str.data;
2654 }
2655
2656 if (!smp->ctx.a[0]) { // first call, find the query string
Christopher Faulet46575cd2019-04-17 11:40:30 +02002657 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002658 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +02002659 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002660 struct buffer *temp;
2661 int32_t pos;
Willy Tarreau79e57332018-10-02 16:01:16 +02002662
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002663 if (!htx)
2664 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02002665
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002666 temp = get_trash_chunk();
Christopher Fauleta3f15502019-05-13 15:27:23 +02002667 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002668 struct htx_blk *blk = htx_get_blk(htx, pos);
2669 enum htx_blk_type type = htx_get_blk_type(blk);
Willy Tarreau79e57332018-10-02 16:01:16 +02002670
Christopher Faulet54b5e212019-06-04 10:08:28 +02002671 if (type == HTX_BLK_EOM || type == HTX_BLK_TLR || type == HTX_BLK_EOT)
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002672 break;
2673 if (type == HTX_BLK_DATA) {
Christopher Fauletc59ff232018-12-03 13:58:44 +01002674 if (!htx_data_to_h1(htx_get_blk_value(htx, blk), temp, 0))
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002675 return 0;
2676 }
2677 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002678
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002679 smp->ctx.a[0] = temp->area;
2680 smp->ctx.a[1] = temp->area + temp->data;
Willy Tarreau79e57332018-10-02 16:01:16 +02002681
2682 /* Assume that the context is filled with NULL pointer
2683 * before the first call.
2684 * smp->ctx.a[2] = NULL;
2685 * smp->ctx.a[3] = NULL;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002686 */
Willy Tarreau79e57332018-10-02 16:01:16 +02002687 }
2688 else {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002689 /* LEGACY version */
2690 struct http_msg *msg;
2691 unsigned long len;
2692 unsigned long block1;
2693 char *body;
2694
Christopher Faulet89dc4992019-04-17 12:02:59 +02002695 CHECK_HTTP_MESSAGE_FIRST(chn);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002696
Christopher Faulet89dc4992019-04-17 12:02:59 +02002697 msg = &smp->strm->txn->req;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002698 len = http_body_bytes(msg);
Christopher Faulet89dc4992019-04-17 12:02:59 +02002699 body = c_ptr(chn, -http_data_rewind(msg));
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002700
2701 block1 = len;
Christopher Faulet89dc4992019-04-17 12:02:59 +02002702 if (block1 > b_wrap(&chn->buf) - body)
2703 block1 = b_wrap(&chn->buf) - body;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002704
2705 if (block1 == len) {
2706 /* buffer is not wrapped (or empty) */
2707 smp->ctx.a[0] = body;
2708 smp->ctx.a[1] = body + len;
2709
2710 /* Assume that the context is filled with NULL pointer
2711 * before the first call.
2712 * smp->ctx.a[2] = NULL;
2713 * smp->ctx.a[3] = NULL;
2714 */
2715 }
2716 else {
2717 /* buffer is wrapped, we need to defragment it */
2718 smp->ctx.a[0] = body;
2719 smp->ctx.a[1] = body + block1;
Christopher Faulet89dc4992019-04-17 12:02:59 +02002720 smp->ctx.a[2] = b_orig(&chn->buf);
2721 smp->ctx.a[3] = b_orig(&chn->buf) + ( len - block1 );
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002722 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002723 }
2724 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002725
Willy Tarreau79e57332018-10-02 16:01:16 +02002726 return smp_fetch_param('&', name, name_len, args, smp, kw, private);
2727}
2728
2729/* Return the signed integer value for the specified url parameter (see url_param
2730 * above).
2731 */
2732static int smp_fetch_url_param_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
2733{
2734 int ret = smp_fetch_url_param(args, smp, kw, private);
2735
2736 if (ret > 0) {
2737 smp->data.type = SMP_T_SINT;
2738 smp->data.u.sint = strl2ic(smp->data.u.str.area,
2739 smp->data.u.str.data);
2740 }
2741
2742 return ret;
2743}
2744
2745/* This produces a 32-bit hash of the concatenation of the first occurrence of
2746 * the Host header followed by the path component if it begins with a slash ('/').
2747 * This means that '*' will not be added, resulting in exactly the first Host
2748 * entry. If no Host header is found, then the path is used. The resulting value
2749 * is hashed using the url hash followed by a full avalanche hash and provides a
2750 * 32-bit integer value. This fetch is useful for tracking per-URL activity on
2751 * high-traffic sites without having to store whole paths.
2752 * this differs from the base32 functions in that it includes the url parameters
2753 * as well as the path
2754 */
2755static int smp_fetch_url32(const struct arg *args, struct sample *smp, const char *kw, void *private)
2756{
Christopher Faulet89dc4992019-04-17 12:02:59 +02002757 struct channel *chn = SMP_REQ_CHN(smp);
Willy Tarreau79e57332018-10-02 16:01:16 +02002758 unsigned int hash = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02002759
Christopher Faulet46575cd2019-04-17 11:40:30 +02002760 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002761 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +02002762 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002763 struct http_hdr_ctx ctx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01002764 struct htx_sl *sl;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002765 struct ist path;
Willy Tarreau79e57332018-10-02 16:01:16 +02002766
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002767 if (!htx)
2768 return 0;
2769
2770 ctx.blk = NULL;
2771 if (http_find_header(htx, ist("Host"), &ctx, 1)) {
2772 /* OK we have the header value in ctx.value */
2773 while (ctx.value.len--)
2774 hash = *(ctx.value.ptr++) + (hash << 6) + (hash << 16) - hash;
2775 }
2776
2777 /* now retrieve the path */
Christopher Faulet297fbb42019-05-13 14:41:27 +02002778 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01002779 path = http_get_path(htx_sl_req_uri(sl));
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002780 if (path.len && *(path.ptr) == '/') {
2781 while (path.len--)
2782 hash = *(path.ptr++) + (hash << 6) + (hash << 16) - hash;
2783 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002784 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002785 else {
2786 /* LEGACY version */
2787 struct http_txn *txn;
2788 struct hdr_ctx ctx;
2789 char *ptr, *beg, *end;
2790 int len;
2791
Christopher Faulet89dc4992019-04-17 12:02:59 +02002792 CHECK_HTTP_MESSAGE_FIRST(chn);
Willy Tarreau79e57332018-10-02 16:01:16 +02002793
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002794 txn = smp->strm->txn;
2795 ctx.idx = 0;
Christopher Faulet89dc4992019-04-17 12:02:59 +02002796 if (http_find_header2("Host", 4, ci_head(chn), &txn->hdr_idx, &ctx)) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002797 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
2798 ptr = ctx.line + ctx.val;
2799 len = ctx.vlen;
2800 while (len--)
2801 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
2802 }
2803
2804 /* now retrieve the path */
Christopher Faulet89dc4992019-04-17 12:02:59 +02002805 end = ci_head(chn) + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002806 beg = http_txn_get_path(txn);
2807 if (!beg)
2808 beg = end;
Willy Tarreau79e57332018-10-02 16:01:16 +02002809
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002810 for (ptr = beg; ptr < end ; ptr++);
Willy Tarreau79e57332018-10-02 16:01:16 +02002811
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002812 if (beg < ptr && *beg == '/') {
2813 while (beg < ptr)
2814 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
2815 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002816 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002817
Willy Tarreau79e57332018-10-02 16:01:16 +02002818 hash = full_hash(hash);
2819
2820 smp->data.type = SMP_T_SINT;
2821 smp->data.u.sint = hash;
2822 smp->flags = SMP_F_VOL_1ST;
2823 return 1;
2824}
2825
2826/* This concatenates the source address with the 32-bit hash of the Host and
2827 * URL as returned by smp_fetch_base32(). The idea is to have per-source and
2828 * per-url counters. The result is a binary block from 8 to 20 bytes depending
2829 * on the source address length. The URL hash is stored before the address so
2830 * that in environments where IPv6 is insignificant, truncating the output to
2831 * 8 bytes would still work.
2832 */
2833static int smp_fetch_url32_src(const struct arg *args, struct sample *smp, const char *kw, void *private)
2834{
2835 struct buffer *temp;
2836 struct connection *cli_conn = objt_conn(smp->sess->origin);
2837
2838 if (!cli_conn)
2839 return 0;
2840
2841 if (!smp_fetch_url32(args, smp, kw, private))
2842 return 0;
2843
2844 temp = get_trash_chunk();
2845 *(unsigned int *) temp->area = htonl(smp->data.u.sint);
2846 temp->data += sizeof(unsigned int);
2847
2848 switch (cli_conn->addr.from.ss_family) {
2849 case AF_INET:
2850 memcpy(temp->area + temp->data,
2851 &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr,
2852 4);
2853 temp->data += 4;
2854 break;
2855 case AF_INET6:
2856 memcpy(temp->area + temp->data,
2857 &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr,
2858 16);
2859 temp->data += 16;
2860 break;
2861 default:
2862 return 0;
2863 }
2864
2865 smp->data.u.str = *temp;
2866 smp->data.type = SMP_T_BIN;
2867 return 1;
2868}
2869
2870/************************************************************************/
2871/* Other utility functions */
2872/************************************************************************/
2873
2874/* This function is used to validate the arguments passed to any "hdr" fetch
2875 * keyword. These keywords support an optional positive or negative occurrence
2876 * number. We must ensure that the number is greater than -MAX_HDR_HISTORY. It
2877 * is assumed that the types are already the correct ones. Returns 0 on error,
2878 * non-zero if OK. If <err> is not NULL, it will be filled with a pointer to an
2879 * error message in case of error, that the caller is responsible for freeing.
2880 * The initial location must either be freeable or NULL.
2881 * Note: this function's pointer is checked from Lua.
2882 */
2883int val_hdr(struct arg *arg, char **err_msg)
2884{
2885 if (arg && arg[1].type == ARGT_SINT && arg[1].data.sint < -MAX_HDR_HISTORY) {
2886 memprintf(err_msg, "header occurrence must be >= %d", -MAX_HDR_HISTORY);
2887 return 0;
2888 }
2889 return 1;
2890}
2891
2892/************************************************************************/
2893/* All supported sample fetch keywords must be declared here. */
2894/************************************************************************/
2895
2896/* Note: must not be declared <const> as its list will be overwritten */
2897static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
2898 { "base", smp_fetch_base, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2899 { "base32", smp_fetch_base32, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
2900 { "base32+src", smp_fetch_base32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
2901
2902 /* capture are allocated and are permanent in the stream */
2903 { "capture.req.hdr", smp_fetch_capture_req_hdr, ARG1(1,SINT), NULL, SMP_T_STR, SMP_USE_HRQHP },
2904
2905 /* retrieve these captures from the HTTP logs */
2906 { "capture.req.method", smp_fetch_capture_req_method, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
2907 { "capture.req.uri", smp_fetch_capture_req_uri, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
2908 { "capture.req.ver", smp_fetch_capture_req_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
2909
2910 { "capture.res.hdr", smp_fetch_capture_res_hdr, ARG1(1,SINT), NULL, SMP_T_STR, SMP_USE_HRSHP },
2911 { "capture.res.ver", smp_fetch_capture_res_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
2912
2913 /* cookie is valid in both directions (eg: for "stick ...") but cook*
2914 * are only here to match the ACL's name, are request-only and are used
2915 * for ACL compatibility only.
2916 */
2917 { "cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Christopher Fauletc1f40dd2019-05-16 10:07:30 +02002918 { "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 +02002919 { "cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2920 { "cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2921
2922 /* hdr is valid in both directions (eg: for "stick ...") but hdr_* are
2923 * only here to match the ACL's name, are request-only and are used for
2924 * ACL compatibility only.
2925 */
Christopher Fauletc1f40dd2019-05-16 10:07:30 +02002926 { "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 +02002927 { "hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2928 { "hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV },
2929 { "hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_SINT, SMP_USE_HRQHV },
2930
2931 { "http_auth", smp_fetch_http_auth, ARG1(1,USR), NULL, SMP_T_BOOL, SMP_USE_HRQHV },
2932 { "http_auth_group", smp_fetch_http_auth_grp, ARG1(1,USR), NULL, SMP_T_STR, SMP_USE_HRQHV },
2933 { "http_first_req", smp_fetch_http_first_req, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
2934 { "method", smp_fetch_meth, 0, NULL, SMP_T_METH, SMP_USE_HRQHP },
2935 { "path", smp_fetch_path, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2936 { "query", smp_fetch_query, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2937
2938 /* HTTP protocol on the request path */
2939 { "req.proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
2940 { "req_proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
2941
2942 /* HTTP version on the request path */
2943 { "req.ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2944 { "req_ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2945
2946 { "req.body", smp_fetch_body, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
2947 { "req.body_len", smp_fetch_body_len, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
2948 { "req.body_size", smp_fetch_body_size, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
2949 { "req.body_param", smp_fetch_body_param, ARG1(0,STR), NULL, SMP_T_BIN, SMP_USE_HRQHV },
2950
2951 { "req.hdrs", smp_fetch_hdrs, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
2952 { "req.hdrs_bin", smp_fetch_hdrs_bin, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
2953
2954 /* HTTP version on the response path */
2955 { "res.ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
2956 { "resp_ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
2957
2958 /* explicit req.{cook,hdr} are used to force the fetch direction to be request-only */
2959 { "req.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
2960 { "req.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2961 { "req.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2962
2963 { "req.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
2964 { "req.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2965 { "req.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
2966 { "req.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2967 { "req.hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV },
2968 { "req.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
2969 { "req.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_SINT, SMP_USE_HRQHV },
2970
2971 /* explicit req.{cook,hdr} are used to force the fetch direction to be response-only */
2972 { "res.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
2973 { "res.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2974 { "res.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2975
2976 { "res.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
2977 { "res.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2978 { "res.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
2979 { "res.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2980 { "res.hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV },
2981 { "res.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
2982 { "res.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_SINT, SMP_USE_HRSHV },
2983
2984 /* scook is valid only on the response and is used for ACL compatibility */
2985 { "scook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
2986 { "scook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2987 { "scook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2988 { "set-cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV }, /* deprecated */
2989
2990 /* shdr is valid only on the response and is used for ACL compatibility */
2991 { "shdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
2992 { "shdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2993 { "shdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV },
2994 { "shdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_SINT, SMP_USE_HRSHV },
2995
2996 { "status", smp_fetch_stcode, 0, NULL, SMP_T_SINT, SMP_USE_HRSHP },
2997 { "unique-id", smp_fetch_uniqueid, 0, NULL, SMP_T_STR, SMP_SRC_L4SRV },
2998 { "url", smp_fetch_url, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2999 { "url32", smp_fetch_url32, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
3000 { "url32+src", smp_fetch_url32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
3001 { "url_ip", smp_fetch_url_ip, 0, NULL, SMP_T_IPV4, SMP_USE_HRQHV },
3002 { "url_port", smp_fetch_url_port, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
3003 { "url_param", smp_fetch_url_param, ARG2(0,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
3004 { "urlp" , smp_fetch_url_param, ARG2(0,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
3005 { "urlp_val", smp_fetch_url_param_val, ARG2(0,STR,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
3006 { /* END */ },
3007}};
3008
Willy Tarreau0108d902018-11-25 19:14:37 +01003009INITCALL1(STG_REGISTER, sample_register_fetches, &sample_fetch_keywords);
Willy Tarreau79e57332018-10-02 16:01:16 +02003010
3011/*
3012 * Local variables:
3013 * c-indent-level: 8
3014 * c-basic-offset: 8
3015 * End:
3016 */