blob: 0b16c51cb4c57b1dec4beaf262c0b6c49c55b4ba [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
Christopher Fauletb99a3b02021-04-15 09:28:02 +0200465 htx = smp_prefetch_htx(smp, chn, 1);
Willy Tarreau91d0b602020-08-12 14:04:52 +0200466 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
Amaury Denoyelle0ef3a712021-05-10 11:23:34 +02001076 memset(&addr, 0, sizeof(addr));
1077
Christopher Faulet46575cd2019-04-17 11:40:30 +02001078 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001079 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +02001080 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001081 struct htx_sl *sl;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001082
1083 if (!htx)
1084 return 0;
Christopher Faulet297fbb42019-05-13 14:41:27 +02001085 sl = http_get_stline(htx);
Amaury Denoyelle0ef3a712021-05-10 11:23:34 +02001086 if (url2sa(HTX_SL_REQ_UPTR(sl), HTX_SL_REQ_ULEN(sl), &addr, NULL) < 0)
1087 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001088 }
1089 else {
1090 /* LEGACY version */
Christopher Faulet89dc4992019-04-17 12:02:59 +02001091 CHECK_HTTP_MESSAGE_FIRST(chn);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001092
1093 txn = smp->strm->txn;
Amaury Denoyelle0ef3a712021-05-10 11:23:34 +02001094 if (url2sa(ci_head(chn) + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &addr, NULL) < 0)
1095 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001096 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001097
Willy Tarreau79e57332018-10-02 16:01:16 +02001098 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
1099 return 0;
1100
1101 smp->data.type = SMP_T_IPV4;
1102 smp->data.u.ipv4 = ((struct sockaddr_in *)&addr)->sin_addr;
1103 smp->flags = 0;
1104 return 1;
1105}
1106
1107static int smp_fetch_url_port(const struct arg *args, struct sample *smp, const char *kw, void *private)
1108{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001109 struct channel *chn = SMP_REQ_CHN(smp);
Willy Tarreau79e57332018-10-02 16:01:16 +02001110 struct http_txn *txn;
1111 struct sockaddr_storage addr;
1112
Amaury Denoyelle0ef3a712021-05-10 11:23:34 +02001113 memset(&addr, 0, sizeof(addr));
1114
Christopher Faulet46575cd2019-04-17 11:40:30 +02001115 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001116 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +02001117 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001118 struct htx_sl *sl;
Willy Tarreau79e57332018-10-02 16:01:16 +02001119
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001120 if (!htx)
1121 return 0;
Christopher Faulet297fbb42019-05-13 14:41:27 +02001122 sl = http_get_stline(htx);
Amaury Denoyelle0ef3a712021-05-10 11:23:34 +02001123 if (url2sa(HTX_SL_REQ_UPTR(sl), HTX_SL_REQ_ULEN(sl), &addr, NULL) < 0)
1124 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001125 }
1126 else {
1127 /* LEGACY version */
Christopher Faulet89dc4992019-04-17 12:02:59 +02001128 CHECK_HTTP_MESSAGE_FIRST(chn);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001129
1130 txn = smp->strm->txn;
Amaury Denoyelle0ef3a712021-05-10 11:23:34 +02001131 if (url2sa(ci_head(chn) + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &addr, NULL) < 0)
1132 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001133 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001134 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
1135 return 0;
1136
1137 smp->data.type = SMP_T_SINT;
1138 smp->data.u.sint = ntohs(((struct sockaddr_in *)&addr)->sin_port);
1139 smp->flags = 0;
1140 return 1;
1141}
1142
1143/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
1144 * Accepts an optional argument of type string containing the header field name,
1145 * and an optional argument of type signed or unsigned integer to request an
1146 * explicit occurrence of the header. Note that in the event of a missing name,
1147 * headers are considered from the first one. It does not stop on commas and
1148 * returns full lines instead (useful for User-Agent or Date for example).
1149 */
1150static int smp_fetch_fhdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
1151{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001152 /* possible keywords: req.fhdr, res.fhdr */
1153 struct channel *chn = ((kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Willy Tarreau79e57332018-10-02 16:01:16 +02001154 int occ = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001155
Christopher Faulet46575cd2019-04-17 11:40:30 +02001156 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001157 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +02001158 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001159 struct http_hdr_ctx *ctx = smp->ctx.a[0];
1160 struct ist name;
Willy Tarreau79e57332018-10-02 16:01:16 +02001161
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001162 if (!ctx) {
1163 /* first call */
1164 ctx = &static_http_hdr_ctx;
1165 ctx->blk = NULL;
1166 smp->ctx.a[0] = ctx;
1167 }
1168
1169 if (args) {
1170 if (args[0].type != ARGT_STR)
1171 return 0;
1172 name.ptr = args[0].data.str.area;
1173 name.len = args[0].data.str.data;
1174
1175 if (args[1].type == ARGT_SINT)
1176 occ = args[1].data.sint;
1177 }
1178
1179 if (!htx)
Willy Tarreau79e57332018-10-02 16:01:16 +02001180 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001181
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001182 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
1183 /* search for header from the beginning */
1184 ctx->blk = NULL;
1185
1186 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
1187 /* no explicit occurrence and single fetch => last header by default */
1188 occ = -1;
1189
1190 if (!occ)
1191 /* prepare to report multiple occurrences for ACL fetches */
1192 smp->flags |= SMP_F_NOT_LAST;
1193
1194 smp->data.type = SMP_T_STR;
1195 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
1196 if (http_get_htx_fhdr(htx, name, occ, ctx, &smp->data.u.str.area, &smp->data.u.str.data))
1197 return 1;
Willy Tarreau79e57332018-10-02 16:01:16 +02001198 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001199 else {
1200 /* LEGACY version */
1201 struct hdr_idx *idx;
1202 struct hdr_ctx *ctx = smp->ctx.a[0];
1203 const struct http_msg *msg;
1204 const char *name_str = NULL;
1205 int name_len = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001206
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001207 if (!ctx) {
1208 /* first call */
1209 ctx = &static_hdr_ctx;
1210 ctx->idx = 0;
1211 smp->ctx.a[0] = ctx;
1212 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001213
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001214 if (args) {
1215 if (args[0].type != ARGT_STR)
1216 return 0;
1217 name_str = args[0].data.str.area;
1218 name_len = args[0].data.str.data;
Willy Tarreau79e57332018-10-02 16:01:16 +02001219
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001220 if (args[1].type == ARGT_SINT)
1221 occ = args[1].data.sint;
1222 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001223
Christopher Faulet89dc4992019-04-17 12:02:59 +02001224 CHECK_HTTP_MESSAGE_FIRST(chn);
Willy Tarreau79e57332018-10-02 16:01:16 +02001225
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001226 idx = &smp->strm->txn->hdr_idx;
Christopher Faulet89dc4992019-04-17 12:02:59 +02001227 msg = (!(chn->flags & CF_ISRESP) ? &smp->strm->txn->req : &smp->strm->txn->rsp);
Willy Tarreau79e57332018-10-02 16:01:16 +02001228
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001229 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
1230 /* search for header from the beginning */
1231 ctx->idx = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001232
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001233 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
1234 /* no explicit occurrence and single fetch => last header by default */
1235 occ = -1;
1236
1237 if (!occ)
1238 /* prepare to report multiple occurrences for ACL fetches */
1239 smp->flags |= SMP_F_NOT_LAST;
1240
1241 smp->data.type = SMP_T_STR;
1242 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
1243 if (http_get_fhdr(msg, name_str, name_len, idx, occ, ctx, &smp->data.u.str.area, &smp->data.u.str.data))
1244 return 1;
1245 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001246 smp->flags &= ~SMP_F_NOT_LAST;
1247 return 0;
1248}
1249
1250/* 6. Check on HTTP header count. The number of occurrences is returned.
1251 * Accepts exactly 1 argument of type string. It does not stop on commas and
1252 * returns full lines instead (useful for User-Agent or Date for example).
1253 */
1254static int smp_fetch_fhdr_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
1255{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001256 /* possible keywords: req.fhdr_cnt, res.fhdr_cnt */
1257 struct channel *chn = ((kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Willy Tarreau79e57332018-10-02 16:01:16 +02001258 int cnt;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001259
Christopher Faulet46575cd2019-04-17 11:40:30 +02001260 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001261 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +02001262 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001263 struct http_hdr_ctx ctx;
1264 struct ist name;
1265
1266 if (!htx)
1267 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001268
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001269 if (args && args->type == ARGT_STR) {
1270 name.ptr = args->data.str.area;
1271 name.len = args->data.str.data;
Olivier Houcharde2c78cd2018-11-21 13:49:48 +01001272 } else {
1273 name.ptr = NULL;
1274 name.len = 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001275 }
1276
1277 ctx.blk = NULL;
1278 cnt = 0;
1279 while (http_find_header(htx, name, &ctx, 1))
1280 cnt++;
Willy Tarreau79e57332018-10-02 16:01:16 +02001281 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001282 else {
1283 /* LEGACY version */
1284 struct hdr_idx *idx;
1285 struct hdr_ctx ctx;
1286 const struct http_msg *msg;
1287 const char *name = NULL;
1288 int len = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001289
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001290 if (args && args->type == ARGT_STR) {
1291 name = args->data.str.area;
1292 len = args->data.str.data;
1293 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001294
Christopher Faulet89dc4992019-04-17 12:02:59 +02001295 CHECK_HTTP_MESSAGE_FIRST(chn);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001296
1297 idx = &smp->strm->txn->hdr_idx;
Christopher Faulet89dc4992019-04-17 12:02:59 +02001298 msg = (!(chn->flags & CF_ISRESP) ? &smp->strm->txn->req : &smp->strm->txn->rsp);
Willy Tarreau79e57332018-10-02 16:01:16 +02001299
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001300 ctx.idx = 0;
1301 cnt = 0;
1302 while (http_find_full_header2(name, len, ci_head(msg->chn), idx, &ctx))
1303 cnt++;
1304 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001305
1306 smp->data.type = SMP_T_SINT;
1307 smp->data.u.sint = cnt;
1308 smp->flags = SMP_F_VOL_HDR;
1309 return 1;
1310}
1311
1312static int smp_fetch_hdr_names(const struct arg *args, struct sample *smp, const char *kw, void *private)
1313{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001314 /* possible keywords: req.hdr_names, res.hdr_names */
1315 struct channel *chn = ((kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Willy Tarreau79e57332018-10-02 16:01:16 +02001316 struct buffer *temp;
1317 char del = ',';
1318
Christopher Faulet46575cd2019-04-17 11:40:30 +02001319 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001320 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +02001321 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001322 int32_t pos;
Willy Tarreau79e57332018-10-02 16:01:16 +02001323
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001324 if (!htx)
1325 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001326
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001327 if (args && args->type == ARGT_STR)
1328 del = *args[0].data.str.area;
Willy Tarreau79e57332018-10-02 16:01:16 +02001329
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001330 temp = get_trash_chunk();
Christopher Fauleta3f15502019-05-13 15:27:23 +02001331 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001332 struct htx_blk *blk = htx_get_blk(htx, pos);
1333 enum htx_blk_type type = htx_get_blk_type(blk);
1334 struct ist n;
Willy Tarreau79e57332018-10-02 16:01:16 +02001335
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001336 if (type == HTX_BLK_EOH)
1337 break;
1338 if (type != HTX_BLK_HDR)
1339 continue;
1340 n = htx_get_blk_name(htx, blk);
1341
1342 if (temp->data)
1343 temp->area[temp->data++] = del;
1344 chunk_memcat(temp, n.ptr, n.len);
1345 }
1346 }
1347 else {
1348 /* LEGACY version */
1349 struct hdr_idx *idx;
1350 struct hdr_ctx ctx;
1351 const struct http_msg *msg;
1352
1353 if (args && args->type == ARGT_STR)
1354 del = *args[0].data.str.area;
1355
Christopher Faulet89dc4992019-04-17 12:02:59 +02001356 CHECK_HTTP_MESSAGE_FIRST(chn);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001357
1358 idx = &smp->strm->txn->hdr_idx;
Christopher Faulet89dc4992019-04-17 12:02:59 +02001359 msg = (!(chn->flags & CF_ISRESP) ? &smp->strm->txn->req : &smp->strm->txn->rsp);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001360
1361 temp = get_trash_chunk();
1362
1363 ctx.idx = 0;
1364 while (http_find_next_header(ci_head(msg->chn), idx, &ctx)) {
1365 if (temp->data)
1366 temp->area[temp->data++] = del;
1367 memcpy(temp->area + temp->data, ctx.line, ctx.del);
1368 temp->data += ctx.del;
1369 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001370 }
1371
1372 smp->data.type = SMP_T_STR;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001373 smp->data.u.str = *temp;
Willy Tarreau79e57332018-10-02 16:01:16 +02001374 smp->flags = SMP_F_VOL_HDR;
1375 return 1;
1376}
1377
1378/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
1379 * Accepts an optional argument of type string containing the header field name,
1380 * and an optional argument of type signed or unsigned integer to request an
1381 * explicit occurrence of the header. Note that in the event of a missing name,
1382 * headers are considered from the first one.
1383 */
1384static int smp_fetch_hdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
1385{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001386 /* possible keywords: req.hdr / hdr, res.hdr / shdr */
1387 struct channel *chn = ((kw[0] == 'h' || kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Willy Tarreau79e57332018-10-02 16:01:16 +02001388 int occ = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001389
Christopher Faulet46575cd2019-04-17 11:40:30 +02001390 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001391 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +02001392 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001393 struct http_hdr_ctx *ctx = smp->ctx.a[0];
1394 struct ist name;
1395
1396 if (!ctx) {
1397 /* first call */
1398 ctx = &static_http_hdr_ctx;
1399 ctx->blk = NULL;
1400 smp->ctx.a[0] = ctx;
1401 }
1402
1403 if (args) {
1404 if (args[0].type != ARGT_STR)
1405 return 0;
1406 name.ptr = args[0].data.str.area;
1407 name.len = args[0].data.str.data;
1408
1409 if (args[1].type == ARGT_SINT)
1410 occ = args[1].data.sint;
1411 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001412
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001413 if (!htx)
Willy Tarreau79e57332018-10-02 16:01:16 +02001414 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001415
1416 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
1417 /* search for header from the beginning */
1418 ctx->blk = NULL;
1419
1420 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
1421 /* no explicit occurrence and single fetch => last header by default */
1422 occ = -1;
1423
1424 if (!occ)
1425 /* prepare to report multiple occurrences for ACL fetches */
1426 smp->flags |= SMP_F_NOT_LAST;
Willy Tarreau79e57332018-10-02 16:01:16 +02001427
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001428 smp->data.type = SMP_T_STR;
1429 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
1430 if (http_get_htx_hdr(htx, name, occ, ctx, &smp->data.u.str.area, &smp->data.u.str.data))
1431 return 1;
Willy Tarreau79e57332018-10-02 16:01:16 +02001432 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001433 else {
1434 /* LEGACY version */
1435 struct hdr_idx *idx;
1436 struct hdr_ctx *ctx = smp->ctx.a[0];
1437 const struct http_msg *msg;
1438 const char *name_str = NULL;
1439 int name_len = 0;
1440
1441 if (!ctx) {
1442 /* first call */
1443 ctx = &static_hdr_ctx;
1444 ctx->idx = 0;
1445 smp->ctx.a[0] = ctx;
1446 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001447
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001448 if (args) {
1449 if (args[0].type != ARGT_STR)
1450 return 0;
1451 name_str = args[0].data.str.area;
1452 name_len = args[0].data.str.data;
Willy Tarreau79e57332018-10-02 16:01:16 +02001453
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001454 if (args[1].type == ARGT_SINT)
1455 occ = args[1].data.sint;
1456 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001457
Christopher Faulet89dc4992019-04-17 12:02:59 +02001458 CHECK_HTTP_MESSAGE_FIRST(chn);
Willy Tarreau79e57332018-10-02 16:01:16 +02001459
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001460 idx = &smp->strm->txn->hdr_idx;
Christopher Faulet89dc4992019-04-17 12:02:59 +02001461 msg = (!(chn->flags & CF_ISRESP) ? &smp->strm->txn->req : &smp->strm->txn->rsp);
Willy Tarreau79e57332018-10-02 16:01:16 +02001462
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001463 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
1464 /* search for header from the beginning */
1465 ctx->idx = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001466
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001467 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
1468 /* no explicit occurrence and single fetch => last header by default */
1469 occ = -1;
1470
1471 if (!occ)
1472 /* prepare to report multiple occurrences for ACL fetches */
1473 smp->flags |= SMP_F_NOT_LAST;
1474
1475 smp->data.type = SMP_T_STR;
1476 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
1477 if (http_get_hdr(msg, name_str, name_len, idx, occ, ctx, &smp->data.u.str.area, &smp->data.u.str.data))
1478 return 1;
1479 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001480
1481 smp->flags &= ~SMP_F_NOT_LAST;
1482 return 0;
1483}
1484
Christopher Fauletc1f40dd2019-05-16 10:07:30 +02001485/* Same than smp_fetch_hdr() but only relies on the sample direction to choose
1486 * the right channel. So instead of duplicating the code, we just change the
1487 * keyword and then fallback on smp_fetch_hdr().
1488 */
1489static int smp_fetch_chn_hdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
1490{
1491 kw = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ ? "req.hdr" : "res.hdr");
1492 return smp_fetch_hdr(args, smp, kw, private);
1493}
1494
Willy Tarreau79e57332018-10-02 16:01:16 +02001495/* 6. Check on HTTP header count. The number of occurrences is returned.
1496 * Accepts exactly 1 argument of type string.
1497 */
1498static int smp_fetch_hdr_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
1499{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001500 /* possible keywords: req.hdr_cnt / hdr_cnt, res.hdr_cnt / shdr_cnt */
1501 struct channel *chn = ((kw[0] == 'h' || kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Willy Tarreau79e57332018-10-02 16:01:16 +02001502 int cnt;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001503
Christopher Faulet46575cd2019-04-17 11:40:30 +02001504 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001505 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +02001506 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001507 struct http_hdr_ctx ctx;
1508 struct ist name;
1509
1510 if (!htx)
1511 return 0;
1512
1513 if (args && args->type == ARGT_STR) {
1514 name.ptr = args->data.str.area;
1515 name.len = args->data.str.data;
Olivier Houcharde2c78cd2018-11-21 13:49:48 +01001516 } else {
1517 name.ptr = NULL;
1518 name.len = 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001519 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001520
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001521 ctx.blk = NULL;
1522 cnt = 0;
1523 while (http_find_header(htx, name, &ctx, 0))
1524 cnt++;
Willy Tarreau79e57332018-10-02 16:01:16 +02001525 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001526 else {
1527 /* LEGACY version */
1528 struct hdr_idx *idx;
1529 struct hdr_ctx ctx;
1530 const struct http_msg *msg;
1531 const char *name = NULL;
1532 int len = 0;
1533
1534 if (args && args->type == ARGT_STR) {
1535 name = args->data.str.area;
1536 len = args->data.str.data;
1537 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001538
Christopher Faulet89dc4992019-04-17 12:02:59 +02001539 CHECK_HTTP_MESSAGE_FIRST(chn);
Willy Tarreau79e57332018-10-02 16:01:16 +02001540
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001541 idx = &smp->strm->txn->hdr_idx;
Christopher Faulet89dc4992019-04-17 12:02:59 +02001542 msg = (!(chn->flags & CF_ISRESP) ? &smp->strm->txn->req : &smp->strm->txn->rsp);
Willy Tarreau79e57332018-10-02 16:01:16 +02001543
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001544 ctx.idx = 0;
1545 cnt = 0;
1546 while (http_find_header2(name, len, ci_head(msg->chn), idx, &ctx))
1547 cnt++;
1548 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001549
1550 smp->data.type = SMP_T_SINT;
1551 smp->data.u.sint = cnt;
1552 smp->flags = SMP_F_VOL_HDR;
1553 return 1;
1554}
1555
1556/* Fetch an HTTP header's integer value. The integer value is returned. It
1557 * takes a mandatory argument of type string and an optional one of type int
1558 * to designate a specific occurrence. It returns an unsigned integer, which
1559 * may or may not be appropriate for everything.
1560 */
1561static int smp_fetch_hdr_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
1562{
1563 int ret = smp_fetch_hdr(args, smp, kw, private);
1564
1565 if (ret > 0) {
1566 smp->data.type = SMP_T_SINT;
1567 smp->data.u.sint = strl2ic(smp->data.u.str.area,
1568 smp->data.u.str.data);
1569 }
1570
1571 return ret;
1572}
1573
1574/* Fetch an HTTP header's IP value. takes a mandatory argument of type string
1575 * and an optional one of type int to designate a specific occurrence.
Willy Tarreaud08feaf2021-03-25 14:12:29 +01001576 * It returns an IPv4 or IPv6 address. Addresses surrounded by invalid chars
1577 * are rejected. However IPv4 addresses may be followed with a colon and a
1578 * valid port number.
Willy Tarreau79e57332018-10-02 16:01:16 +02001579 */
1580static int smp_fetch_hdr_ip(const struct arg *args, struct sample *smp, const char *kw, void *private)
1581{
Tim Duesterhus8ba978b2020-06-26 15:44:48 +02001582 struct buffer *temp = get_trash_chunk();
Willy Tarreaud08feaf2021-03-25 14:12:29 +01001583 int ret, len;
1584 int port;
Willy Tarreau79e57332018-10-02 16:01:16 +02001585
1586 while ((ret = smp_fetch_hdr(args, smp, kw, private)) > 0) {
Tim Duesterhus8ba978b2020-06-26 15:44:48 +02001587 if (smp->data.u.str.data < temp->size - 1) {
1588 memcpy(temp->area, smp->data.u.str.area,
1589 smp->data.u.str.data);
1590 temp->area[smp->data.u.str.data] = '\0';
Willy Tarreaud08feaf2021-03-25 14:12:29 +01001591 len = url2ipv4((char *) temp->area, &smp->data.u.ipv4);
Willy Tarreau30df20d2021-03-31 11:41:36 +02001592 if (len > 0 && len == smp->data.u.str.data) {
Willy Tarreaud08feaf2021-03-25 14:12:29 +01001593 /* plain IPv4 address */
1594 smp->data.type = SMP_T_IPV4;
1595 break;
1596 } else if (len > 0 && temp->area[len] == ':' &&
1597 strl2irc(temp->area + len + 1, smp->data.u.str.data - len - 1, &port) == 0 &&
1598 port >= 0 && port <= 65535) {
1599 /* IPv4 address suffixed with ':' followed by a valid port number */
Tim Duesterhus8ba978b2020-06-26 15:44:48 +02001600 smp->data.type = SMP_T_IPV4;
1601 break;
1602 } else if (inet_pton(AF_INET6, temp->area, &smp->data.u.ipv6)) {
1603 smp->data.type = SMP_T_IPV6;
1604 break;
Willy Tarreau79e57332018-10-02 16:01:16 +02001605 }
1606 }
1607
1608 /* if the header doesn't match an IP address, fetch next one */
1609 if (!(smp->flags & SMP_F_NOT_LAST))
1610 return 0;
1611 }
1612 return ret;
1613}
1614
1615/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
1616 * the first '/' after the possible hostname, and ends before the possible '?'.
1617 */
1618static int smp_fetch_path(const struct arg *args, struct sample *smp, const char *kw, void *private)
1619{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001620 struct channel *chn = SMP_REQ_CHN(smp);
1621
Christopher Faulet46575cd2019-04-17 11:40:30 +02001622 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001623 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +02001624 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001625 struct htx_sl *sl;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001626 struct ist path;
1627 size_t len;
Willy Tarreau79e57332018-10-02 16:01:16 +02001628
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001629 if (!htx)
1630 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001631
Christopher Faulet297fbb42019-05-13 14:41:27 +02001632 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001633 path = http_get_path(htx_sl_req_uri(sl));
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001634 if (!path.ptr)
1635 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001636
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001637 for (len = 0; len < path.len && *(path.ptr + len) != '?'; len++)
Dragan Dosen8861e1c2019-02-12 19:50:31 +01001638 ;
Willy Tarreau79e57332018-10-02 16:01:16 +02001639
Dragan Dosen8861e1c2019-02-12 19:50:31 +01001640 /* OK, we got the '/' ! */
1641 smp->data.type = SMP_T_STR;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001642 smp->data.u.str.area = path.ptr;
1643 smp->data.u.str.data = len;
1644 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
1645 }
1646 else {
1647 struct http_txn *txn;
1648 char *ptr, *end;
Willy Tarreau79e57332018-10-02 16:01:16 +02001649
Christopher Faulet89dc4992019-04-17 12:02:59 +02001650 CHECK_HTTP_MESSAGE_FIRST(chn);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001651
1652 txn = smp->strm->txn;
Christopher Faulet89dc4992019-04-17 12:02:59 +02001653 end = ci_head(chn) + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001654 ptr = http_txn_get_path(txn);
1655 if (!ptr)
1656 return 0;
1657
1658 /* OK, we got the '/' ! */
1659 smp->data.type = SMP_T_STR;
1660 smp->data.u.str.area = ptr;
1661
1662 while (ptr < end && *ptr != '?')
1663 ptr++;
1664
1665 smp->data.u.str.data = ptr - smp->data.u.str.area;
1666 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
1667 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001668 return 1;
1669}
1670
1671/* This produces a concatenation of the first occurrence of the Host header
1672 * followed by the path component if it begins with a slash ('/'). This means
1673 * that '*' will not be added, resulting in exactly the first Host entry.
1674 * If no Host header is found, then the path is returned as-is. The returned
1675 * value is stored in the trash so it does not need to be marked constant.
1676 * The returned sample is of type string.
1677 */
1678static int smp_fetch_base(const struct arg *args, struct sample *smp, const char *kw, void *private)
1679{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001680 struct channel *chn = SMP_REQ_CHN(smp);
Willy Tarreau79e57332018-10-02 16:01:16 +02001681 struct buffer *temp;
1682
Christopher Faulet46575cd2019-04-17 11:40:30 +02001683 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001684 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +02001685 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001686 struct htx_sl *sl;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001687 struct http_hdr_ctx ctx;
1688 struct ist path;
Willy Tarreau79e57332018-10-02 16:01:16 +02001689
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001690 if (!htx)
1691 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001692
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001693 ctx.blk = NULL;
1694 if (!http_find_header(htx, ist("Host"), &ctx, 0) || !ctx.value.len)
1695 return smp_fetch_path(args, smp, kw, private);
Willy Tarreau79e57332018-10-02 16:01:16 +02001696
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001697 /* OK we have the header value in ctx.value */
1698 temp = get_trash_chunk();
1699 chunk_memcat(temp, ctx.value.ptr, ctx.value.len);
1700
1701 /* now retrieve the path */
Christopher Faulet297fbb42019-05-13 14:41:27 +02001702 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001703 path = http_get_path(htx_sl_req_uri(sl));
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001704 if (path.ptr) {
1705 size_t len;
1706
Dragan Dosen8861e1c2019-02-12 19:50:31 +01001707 for (len = 0; len < path.len && *(path.ptr + len) != '?'; len++)
1708 ;
1709
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001710 if (len && *(path.ptr) == '/')
1711 chunk_memcat(temp, path.ptr, len);
1712 }
1713
1714 smp->data.type = SMP_T_STR;
1715 smp->data.u.str = *temp;
1716 }
1717 else {
1718 /* LEGACY version */
1719 struct http_txn *txn;
1720 char *ptr, *end, *beg;
1721 struct hdr_ctx ctx;
1722
Christopher Faulet89dc4992019-04-17 12:02:59 +02001723 CHECK_HTTP_MESSAGE_FIRST(chn);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001724
1725 txn = smp->strm->txn;
1726 ctx.idx = 0;
Christopher Faulet89dc4992019-04-17 12:02:59 +02001727 if (!http_find_header2("Host", 4, ci_head(chn), &txn->hdr_idx, &ctx) || !ctx.vlen)
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001728 return smp_fetch_path(args, smp, kw, private);
1729
1730 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
1731 temp = get_trash_chunk();
1732 memcpy(temp->area, ctx.line + ctx.val, ctx.vlen);
1733 smp->data.type = SMP_T_STR;
1734 smp->data.u.str.area = temp->area;
1735 smp->data.u.str.data = ctx.vlen;
Willy Tarreau79e57332018-10-02 16:01:16 +02001736
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001737 /* now retrieve the path */
Christopher Faulet89dc4992019-04-17 12:02:59 +02001738 end = ci_head(chn) + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001739 beg = http_txn_get_path(txn);
1740 if (!beg)
1741 beg = end;
Willy Tarreau79e57332018-10-02 16:01:16 +02001742
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001743 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
1744
1745 if (beg < ptr && *beg == '/') {
1746 memcpy(smp->data.u.str.area + smp->data.u.str.data, beg,
1747 ptr - beg);
1748 smp->data.u.str.data += ptr - beg;
1749 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001750 }
1751
1752 smp->flags = SMP_F_VOL_1ST;
1753 return 1;
1754}
1755
1756/* This produces a 32-bit hash of the concatenation of the first occurrence of
1757 * the Host header followed by the path component if it begins with a slash ('/').
1758 * This means that '*' will not be added, resulting in exactly the first Host
1759 * entry. If no Host header is found, then the path is used. The resulting value
1760 * is hashed using the path hash followed by a full avalanche hash and provides a
1761 * 32-bit integer value. This fetch is useful for tracking per-path activity on
1762 * high-traffic sites without having to store whole paths.
1763 */
1764static int smp_fetch_base32(const struct arg *args, struct sample *smp, const char *kw, void *private)
1765{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001766 struct channel *chn = SMP_REQ_CHN(smp);
Willy Tarreau79e57332018-10-02 16:01:16 +02001767 unsigned int hash = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001768
Christopher Faulet46575cd2019-04-17 11:40:30 +02001769 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001770 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +02001771 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001772 struct htx_sl *sl;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001773 struct http_hdr_ctx ctx;
1774 struct ist path;
Willy Tarreau79e57332018-10-02 16:01:16 +02001775
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001776 if (!htx)
1777 return 0;
1778
1779 ctx.blk = NULL;
Dragan Dosen8861e1c2019-02-12 19:50:31 +01001780 if (http_find_header(htx, ist("Host"), &ctx, 0)) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001781 /* OK we have the header value in ctx.value */
1782 while (ctx.value.len--)
1783 hash = *(ctx.value.ptr++) + (hash << 6) + (hash << 16) - hash;
1784 }
1785
1786 /* now retrieve the path */
Christopher Faulet297fbb42019-05-13 14:41:27 +02001787 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001788 path = http_get_path(htx_sl_req_uri(sl));
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001789 if (path.ptr) {
1790 size_t len;
1791
Dragan Dosen8861e1c2019-02-12 19:50:31 +01001792 for (len = 0; len < path.len && *(path.ptr + len) != '?'; len++)
1793 ;
1794
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001795 if (len && *(path.ptr) == '/') {
1796 while (len--)
1797 hash = *(path.ptr++) + (hash << 6) + (hash << 16) - hash;
1798 }
1799 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001800 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001801 else {
1802 /* LEGACY version */
1803 struct http_txn *txn;
1804 struct hdr_ctx ctx;
1805 char *ptr, *beg, *end;
1806 int len;
1807
Christopher Faulet89dc4992019-04-17 12:02:59 +02001808 CHECK_HTTP_MESSAGE_FIRST(chn);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001809
1810 txn = smp->strm->txn;
1811 ctx.idx = 0;
Christopher Faulet89dc4992019-04-17 12:02:59 +02001812 if (http_find_header2("Host", 4, ci_head(chn), &txn->hdr_idx, &ctx)) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001813 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
1814 ptr = ctx.line + ctx.val;
1815 len = ctx.vlen;
1816 while (len--)
1817 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
1818 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001819
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001820 /* now retrieve the path */
Christopher Faulet89dc4992019-04-17 12:02:59 +02001821 end = ci_head(chn) + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001822 beg = http_txn_get_path(txn);
1823 if (!beg)
1824 beg = end;
Willy Tarreau79e57332018-10-02 16:01:16 +02001825
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001826 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
Willy Tarreau79e57332018-10-02 16:01:16 +02001827
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001828 if (beg < ptr && *beg == '/') {
1829 while (beg < ptr)
1830 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
1831 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001832 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001833
Willy Tarreau79e57332018-10-02 16:01:16 +02001834 hash = full_hash(hash);
1835
1836 smp->data.type = SMP_T_SINT;
1837 smp->data.u.sint = hash;
1838 smp->flags = SMP_F_VOL_1ST;
1839 return 1;
1840}
1841
1842/* This concatenates the source address with the 32-bit hash of the Host and
1843 * path as returned by smp_fetch_base32(). The idea is to have per-source and
1844 * per-path counters. The result is a binary block from 8 to 20 bytes depending
1845 * on the source address length. The path hash is stored before the address so
1846 * that in environments where IPv6 is insignificant, truncating the output to
1847 * 8 bytes would still work.
1848 */
1849static int smp_fetch_base32_src(const struct arg *args, struct sample *smp, const char *kw, void *private)
1850{
1851 struct buffer *temp;
1852 struct connection *cli_conn = objt_conn(smp->sess->origin);
1853
1854 if (!cli_conn)
1855 return 0;
1856
1857 if (!smp_fetch_base32(args, smp, kw, private))
1858 return 0;
1859
1860 temp = get_trash_chunk();
1861 *(unsigned int *) temp->area = htonl(smp->data.u.sint);
1862 temp->data += sizeof(unsigned int);
1863
1864 switch (cli_conn->addr.from.ss_family) {
1865 case AF_INET:
1866 memcpy(temp->area + temp->data,
1867 &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr,
1868 4);
1869 temp->data += 4;
1870 break;
1871 case AF_INET6:
1872 memcpy(temp->area + temp->data,
1873 &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr,
1874 16);
1875 temp->data += 16;
1876 break;
1877 default:
1878 return 0;
1879 }
1880
1881 smp->data.u.str = *temp;
1882 smp->data.type = SMP_T_BIN;
1883 return 1;
1884}
1885
1886/* Extracts the query string, which comes after the question mark '?'. If no
1887 * question mark is found, nothing is returned. Otherwise it returns a sample
1888 * of type string carrying the whole query string.
1889 */
1890static int smp_fetch_query(const struct arg *args, struct sample *smp, const char *kw, void *private)
1891{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001892 struct channel *chn = SMP_REQ_CHN(smp);
Willy Tarreau79e57332018-10-02 16:01:16 +02001893 char *ptr, *end;
1894
Christopher Faulet46575cd2019-04-17 11:40:30 +02001895 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001896 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +02001897 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001898 struct htx_sl *sl;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001899
1900 if (!htx)
1901 return 0;
1902
Christopher Faulet297fbb42019-05-13 14:41:27 +02001903 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001904 ptr = HTX_SL_REQ_UPTR(sl);
1905 end = HTX_SL_REQ_UPTR(sl) + HTX_SL_REQ_ULEN(sl);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001906 }
1907 else {
1908 /* LEGACY version */
1909 struct http_txn *txn;
1910
Christopher Faulet89dc4992019-04-17 12:02:59 +02001911 CHECK_HTTP_MESSAGE_FIRST(chn);
Willy Tarreau79e57332018-10-02 16:01:16 +02001912
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001913 txn = smp->strm->txn;
Christopher Faulet89dc4992019-04-17 12:02:59 +02001914 ptr = ci_head(chn) + txn->req.sl.rq.u;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001915 end = ptr + txn->req.sl.rq.u_l;
1916 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001917
1918 /* look up the '?' */
1919 do {
1920 if (ptr == end)
1921 return 0;
1922 } while (*ptr++ != '?');
1923
1924 smp->data.type = SMP_T_STR;
1925 smp->data.u.str.area = ptr;
1926 smp->data.u.str.data = end - ptr;
1927 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
1928 return 1;
1929}
1930
1931static int smp_fetch_proto_http(const struct arg *args, struct sample *smp, const char *kw, void *private)
1932{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001933 struct channel *chn = SMP_REQ_CHN(smp);
1934
Christopher Faulet46575cd2019-04-17 11:40:30 +02001935 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001936 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +02001937 struct htx *htx = smp_prefetch_htx(smp, chn, 0);
Willy Tarreau79e57332018-10-02 16:01:16 +02001938
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001939 if (!htx)
1940 return 0;
1941 }
1942 else {
1943 /* LEGACY version */
Willy Tarreau79e57332018-10-02 16:01:16 +02001944
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001945 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
1946 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
1947 */
Christopher Faulet89dc4992019-04-17 12:02:59 +02001948 CHECK_HTTP_MESSAGE_FIRST_PERM(chn);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001949 }
1950 smp->data.type = SMP_T_BOOL;
Willy Tarreau79e57332018-10-02 16:01:16 +02001951 smp->data.u.sint = 1;
1952 return 1;
1953}
1954
1955/* return a valid test if the current request is the first one on the connection */
1956static int smp_fetch_http_first_req(const struct arg *args, struct sample *smp, const char *kw, void *private)
1957{
Willy Tarreaud095c672020-04-29 11:52:13 +02001958 if (!smp->strm)
1959 return 0;
1960
Willy Tarreau79e57332018-10-02 16:01:16 +02001961 smp->data.type = SMP_T_BOOL;
1962 smp->data.u.sint = !(smp->strm->txn->flags & TX_NOT_FIRST);
1963 return 1;
1964}
1965
1966/* Accepts exactly 1 argument of type userlist */
1967static int smp_fetch_http_auth(const struct arg *args, struct sample *smp, const char *kw, void *private)
1968{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001969 struct channel *chn = SMP_REQ_CHN(smp);
Willy Tarreau79e57332018-10-02 16:01:16 +02001970
1971 if (!args || args->type != ARGT_USR)
1972 return 0;
1973
Christopher Faulet46575cd2019-04-17 11:40:30 +02001974 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001975 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +02001976 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Willy Tarreau79e57332018-10-02 16:01:16 +02001977
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001978 if (!htx)
1979 return 0;
Christopher Faulete98411b2019-07-15 13:58:29 +02001980 if (!get_http_auth(smp, htx))
1981 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001982 }
1983 else {
1984 /* LEGACY version */
Christopher Faulet89dc4992019-04-17 12:02:59 +02001985 CHECK_HTTP_MESSAGE_FIRST(chn);
Christopher Faulete98411b2019-07-15 13:58:29 +02001986 if (!get_http_auth(smp, NULL))
1987 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001988 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001989
1990 smp->data.type = SMP_T_BOOL;
1991 smp->data.u.sint = check_user(args->data.usr, smp->strm->txn->auth.user,
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001992 smp->strm->txn->auth.pass);
Willy Tarreau79e57332018-10-02 16:01:16 +02001993 return 1;
1994}
1995
1996/* Accepts exactly 1 argument of type userlist */
1997static int smp_fetch_http_auth_grp(const struct arg *args, struct sample *smp, const char *kw, void *private)
1998{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001999 struct channel *chn = SMP_REQ_CHN(smp);
2000
Willy Tarreau79e57332018-10-02 16:01:16 +02002001 if (!args || args->type != ARGT_USR)
2002 return 0;
2003
Christopher Faulet46575cd2019-04-17 11:40:30 +02002004 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002005 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +02002006 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002007
2008 if (!htx)
2009 return 0;
Christopher Faulete98411b2019-07-15 13:58:29 +02002010 if (!get_http_auth(smp, htx))
2011 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002012 }
2013 else {
2014 /* LEGACY version */
Christopher Faulet89dc4992019-04-17 12:02:59 +02002015 CHECK_HTTP_MESSAGE_FIRST(chn);
Christopher Faulete98411b2019-07-15 13:58:29 +02002016 if (!get_http_auth(smp, NULL))
2017 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002018 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002019
Willy Tarreau79e57332018-10-02 16:01:16 +02002020 /* if the user does not belong to the userlist or has a wrong password,
2021 * report that it unconditionally does not match. Otherwise we return
2022 * a string containing the username.
2023 */
2024 if (!check_user(args->data.usr, smp->strm->txn->auth.user,
2025 smp->strm->txn->auth.pass))
2026 return 0;
2027
2028 /* pat_match_auth() will need the user list */
2029 smp->ctx.a[0] = args->data.usr;
2030
2031 smp->data.type = SMP_T_STR;
2032 smp->flags = SMP_F_CONST;
2033 smp->data.u.str.area = smp->strm->txn->auth.user;
2034 smp->data.u.str.data = strlen(smp->strm->txn->auth.user);
2035
2036 return 1;
2037}
2038
2039/* Fetch a captured HTTP request header. The index is the position of
2040 * the "capture" option in the configuration file
2041 */
2042static int smp_fetch_capture_req_hdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
2043{
Willy Tarreau79ed4672020-04-29 11:44:54 +02002044 struct proxy *fe;
Willy Tarreau79e57332018-10-02 16:01:16 +02002045 int idx;
2046
2047 if (!args || args->type != ARGT_SINT)
2048 return 0;
2049
Willy Tarreau79ed4672020-04-29 11:44:54 +02002050 if (!smp->strm)
2051 return 0;
2052
2053 fe = strm_fe(smp->strm);
Willy Tarreau79e57332018-10-02 16:01:16 +02002054 idx = args->data.sint;
2055
2056 if (idx > (fe->nb_req_cap - 1) || smp->strm->req_cap == NULL || smp->strm->req_cap[idx] == NULL)
2057 return 0;
2058
2059 smp->data.type = SMP_T_STR;
2060 smp->flags |= SMP_F_CONST;
2061 smp->data.u.str.area = smp->strm->req_cap[idx];
2062 smp->data.u.str.data = strlen(smp->strm->req_cap[idx]);
2063
2064 return 1;
2065}
2066
2067/* Fetch a captured HTTP response header. The index is the position of
2068 * the "capture" option in the configuration file
2069 */
2070static int smp_fetch_capture_res_hdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
2071{
Willy Tarreau79ed4672020-04-29 11:44:54 +02002072 struct proxy *fe;
Willy Tarreau79e57332018-10-02 16:01:16 +02002073 int idx;
2074
2075 if (!args || args->type != ARGT_SINT)
2076 return 0;
2077
Willy Tarreau79ed4672020-04-29 11:44:54 +02002078 if (!smp->strm)
2079 return 0;
2080
2081 fe = strm_fe(smp->strm);
Willy Tarreau79e57332018-10-02 16:01:16 +02002082 idx = args->data.sint;
2083
2084 if (idx > (fe->nb_rsp_cap - 1) || smp->strm->res_cap == NULL || smp->strm->res_cap[idx] == NULL)
2085 return 0;
2086
2087 smp->data.type = SMP_T_STR;
2088 smp->flags |= SMP_F_CONST;
2089 smp->data.u.str.area = smp->strm->res_cap[idx];
2090 smp->data.u.str.data = strlen(smp->strm->res_cap[idx]);
2091
2092 return 1;
2093}
2094
2095/* Extracts the METHOD in the HTTP request, the txn->uri should be filled before the call */
2096static int smp_fetch_capture_req_method(const struct arg *args, struct sample *smp, const char *kw, void *private)
2097{
2098 struct buffer *temp;
Willy Tarreau79ed4672020-04-29 11:44:54 +02002099 struct http_txn *txn;
Willy Tarreau79e57332018-10-02 16:01:16 +02002100 char *ptr;
2101
Willy Tarreau79ed4672020-04-29 11:44:54 +02002102 if (!smp->strm)
2103 return 0;
2104
2105 txn = smp->strm->txn;
Willy Tarreau79e57332018-10-02 16:01:16 +02002106 if (!txn || !txn->uri)
2107 return 0;
2108
2109 ptr = txn->uri;
2110
2111 while (*ptr != ' ' && *ptr != '\0') /* find first space */
2112 ptr++;
2113
2114 temp = get_trash_chunk();
2115 temp->area = txn->uri;
2116 temp->data = ptr - txn->uri;
2117 smp->data.u.str = *temp;
2118 smp->data.type = SMP_T_STR;
2119 smp->flags = SMP_F_CONST;
2120
2121 return 1;
2122
2123}
2124
2125/* Extracts the path in the HTTP request, the txn->uri should be filled before the call */
2126static int smp_fetch_capture_req_uri(const struct arg *args, struct sample *smp, const char *kw, void *private)
2127{
Willy Tarreau79ed4672020-04-29 11:44:54 +02002128 struct http_txn *txn;
Willy Tarreau79e57332018-10-02 16:01:16 +02002129 struct ist path;
2130 const char *ptr;
2131
Willy Tarreau79ed4672020-04-29 11:44:54 +02002132 if (!smp->strm)
2133 return 0;
2134
2135 txn = smp->strm->txn;
Willy Tarreau79e57332018-10-02 16:01:16 +02002136 if (!txn || !txn->uri)
2137 return 0;
2138
2139 ptr = txn->uri;
2140
2141 while (*ptr != ' ' && *ptr != '\0') /* find first space */
2142 ptr++;
2143
2144 if (!*ptr)
2145 return 0;
2146
Christopher Faulet78337bb2018-11-15 14:35:18 +01002147 /* skip the first space and find space after URI */
2148 path = ist2(++ptr, 0);
2149 while (*ptr != ' ' && *ptr != '\0')
2150 ptr++;
2151 path.len = ptr - path.ptr;
Willy Tarreau79e57332018-10-02 16:01:16 +02002152
Christopher Faulet78337bb2018-11-15 14:35:18 +01002153 path = http_get_path(path);
Willy Tarreau79e57332018-10-02 16:01:16 +02002154 if (!path.ptr)
2155 return 0;
2156
2157 smp->data.u.str.area = path.ptr;
2158 smp->data.u.str.data = path.len;
2159 smp->data.type = SMP_T_STR;
2160 smp->flags = SMP_F_CONST;
2161
2162 return 1;
2163}
2164
2165/* Retrieves the HTTP version from the request (either 1.0 or 1.1) and emits it
2166 * as a string (either "HTTP/1.0" or "HTTP/1.1").
2167 */
2168static int smp_fetch_capture_req_ver(const struct arg *args, struct sample *smp, const char *kw, void *private)
2169{
Willy Tarreau79ed4672020-04-29 11:44:54 +02002170 struct http_txn *txn;
2171
2172 if (!smp->strm)
2173 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02002174
Willy Tarreau79ed4672020-04-29 11:44:54 +02002175 txn = smp->strm->txn;
Willy Tarreau79e57332018-10-02 16:01:16 +02002176 if (!txn || txn->req.msg_state < HTTP_MSG_HDR_FIRST)
2177 return 0;
2178
2179 if (txn->req.flags & HTTP_MSGF_VER_11)
2180 smp->data.u.str.area = "HTTP/1.1";
2181 else
2182 smp->data.u.str.area = "HTTP/1.0";
2183
2184 smp->data.u.str.data = 8;
2185 smp->data.type = SMP_T_STR;
2186 smp->flags = SMP_F_CONST;
2187 return 1;
2188
2189}
2190
2191/* Retrieves the HTTP version from the response (either 1.0 or 1.1) and emits it
2192 * as a string (either "HTTP/1.0" or "HTTP/1.1").
2193 */
2194static int smp_fetch_capture_res_ver(const struct arg *args, struct sample *smp, const char *kw, void *private)
2195{
Willy Tarreau79ed4672020-04-29 11:44:54 +02002196 struct http_txn *txn;
2197
2198 if (!smp->strm)
2199 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02002200
Willy Tarreau79ed4672020-04-29 11:44:54 +02002201 txn = smp->strm->txn;
Willy Tarreau79e57332018-10-02 16:01:16 +02002202 if (!txn || txn->rsp.msg_state < HTTP_MSG_HDR_FIRST)
2203 return 0;
2204
2205 if (txn->rsp.flags & HTTP_MSGF_VER_11)
2206 smp->data.u.str.area = "HTTP/1.1";
2207 else
2208 smp->data.u.str.area = "HTTP/1.0";
2209
2210 smp->data.u.str.data = 8;
2211 smp->data.type = SMP_T_STR;
2212 smp->flags = SMP_F_CONST;
2213 return 1;
2214
2215}
2216
2217/* Iterate over all cookies present in a message. The context is stored in
2218 * smp->ctx.a[0] for the in-header position, smp->ctx.a[1] for the
2219 * end-of-header-value, and smp->ctx.a[2] for the hdr_ctx. Depending on
2220 * the direction, multiple cookies may be parsed on the same line or not.
Maciej Zdeb22dc68b2020-11-13 09:38:06 +00002221 * If provided, the searched cookie name is in args, in args->data.str. If
2222 * the input options indicate that no iterating is desired, then only last
2223 * value is fetched if any. If no cookie name is provided, the first cookie
2224 * value found is fetched. The returned sample is of type CSTR. Can be used
2225 * to parse cookies in other files.
Willy Tarreau79e57332018-10-02 16:01:16 +02002226 */
2227static int smp_fetch_cookie(const struct arg *args, struct sample *smp, const char *kw, void *private)
2228{
Christopher Faulet89dc4992019-04-17 12:02:59 +02002229 /* possible keywords: req.cookie / cookie / cook, res.cookie / scook / set-cookie */
2230 struct channel *chn = ((kw[0] == 'c' || kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Christopher Faulet08f475b2020-11-13 13:41:04 +01002231 char *cook = NULL;
2232 size_t cook_l = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02002233 int found = 0;
2234
Christopher Faulet08f475b2020-11-13 13:41:04 +01002235 if (args && args->type == ARGT_STR) {
2236 cook = args->data.str.area;
2237 cook_l = args->data.str.data;
2238 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002239
Christopher Faulet46575cd2019-04-17 11:40:30 +02002240 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002241 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +02002242 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002243 struct http_hdr_ctx *ctx = smp->ctx.a[2];
2244 struct ist hdr;
Willy Tarreau79e57332018-10-02 16:01:16 +02002245
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002246 if (!ctx) {
2247 /* first call */
2248 ctx = &static_http_hdr_ctx;
2249 ctx->blk = NULL;
2250 smp->ctx.a[2] = ctx;
2251 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002252
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002253 if (!htx)
2254 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02002255
Christopher Faulet89dc4992019-04-17 12:02:59 +02002256 hdr = (!(chn->flags & CF_ISRESP) ? ist("Cookie") : ist("Set-Cookie"));
Willy Tarreau79e57332018-10-02 16:01:16 +02002257
Maciej Zdeb22dc68b2020-11-13 09:38:06 +00002258 /* OK so basically here, either we want only one value or we want to
2259 * iterate over all of them and we fetch the next one. In this last case
2260 * SMP_OPT_ITERATE option is set.
Willy Tarreau79e57332018-10-02 16:01:16 +02002261 */
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002262
2263 if (!(smp->flags & SMP_F_NOT_LAST)) {
2264 /* search for the header from the beginning, we must first initialize
2265 * the search parameters.
2266 */
2267 smp->ctx.a[0] = NULL;
2268 ctx->blk = NULL;
2269 }
2270
2271 smp->flags |= SMP_F_VOL_HDR;
2272 while (1) {
2273 /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */
2274 if (!smp->ctx.a[0]) {
2275 if (!http_find_header(htx, hdr, ctx, 0))
2276 goto out;
2277
Christopher Faulet08f475b2020-11-13 13:41:04 +01002278 if (ctx->value.len < cook_l + 1)
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002279 continue;
2280
2281 smp->ctx.a[0] = ctx->value.ptr;
2282 smp->ctx.a[1] = smp->ctx.a[0] + ctx->value.len;
2283 }
2284
2285 smp->data.type = SMP_T_STR;
2286 smp->flags |= SMP_F_CONST;
2287 smp->ctx.a[0] = http_extract_cookie_value(smp->ctx.a[0], smp->ctx.a[1],
Christopher Faulet08f475b2020-11-13 13:41:04 +01002288 cook, cook_l,
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002289 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
2290 &smp->data.u.str.area,
2291 &smp->data.u.str.data);
2292 if (smp->ctx.a[0]) {
2293 found = 1;
Maciej Zdeb22dc68b2020-11-13 09:38:06 +00002294 if (smp->opt & SMP_OPT_ITERATE) {
2295 /* iterate on cookie value */
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002296 smp->flags |= SMP_F_NOT_LAST;
2297 return 1;
2298 }
Maciej Zdeb22dc68b2020-11-13 09:38:06 +00002299 if (args->data.str.data == 0) {
2300 /* No cookie name, first occurrence returned */
2301 break;
2302 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002303 }
2304 /* if we're looking for last occurrence, let's loop */
2305 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002306 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002307 else {
2308 /* LEGACY version */
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002309 struct hdr_idx *idx;
2310 struct hdr_ctx *ctx = smp->ctx.a[2];
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002311 const char *hdr_name;
2312 int hdr_name_len;
2313 char *sol;
Willy Tarreau79e57332018-10-02 16:01:16 +02002314
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002315 if (!ctx) {
2316 /* first call */
2317 ctx = &static_hdr_ctx;
2318 ctx->idx = 0;
2319 smp->ctx.a[2] = ctx;
2320 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002321
Christopher Faulet89dc4992019-04-17 12:02:59 +02002322 CHECK_HTTP_MESSAGE_FIRST(chn);
Willy Tarreau79e57332018-10-02 16:01:16 +02002323
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002324 idx = &smp->strm->txn->hdr_idx;
Christopher Faulet89dc4992019-04-17 12:02:59 +02002325 if (!(chn->flags & CF_ISRESP)) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002326 hdr_name = "Cookie";
2327 hdr_name_len = 6;
2328 } else {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002329 hdr_name = "Set-Cookie";
2330 hdr_name_len = 10;
Willy Tarreau79e57332018-10-02 16:01:16 +02002331 }
2332
Maciej Zdeb22dc68b2020-11-13 09:38:06 +00002333 /* OK so basically here, either we want only one value or we want to
2334 * iterate over all of them and we fetch the next one. In this last case
2335 * SMP_OPT_ITERATE option is set.
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002336 */
2337
Christopher Faulet89dc4992019-04-17 12:02:59 +02002338 sol = ci_head(chn);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002339 if (!(smp->flags & SMP_F_NOT_LAST)) {
2340 /* search for the header from the beginning, we must first initialize
2341 * the search parameters.
2342 */
2343 smp->ctx.a[0] = NULL;
2344 ctx->idx = 0;
2345 }
2346
2347 smp->flags |= SMP_F_VOL_HDR;
2348
2349 while (1) {
2350 /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */
2351 if (!smp->ctx.a[0]) {
2352 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, ctx))
2353 goto out;
2354
Christopher Faulet08f475b2020-11-13 13:41:04 +01002355 if (ctx->vlen < cook_l + 1)
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002356 continue;
2357
2358 smp->ctx.a[0] = ctx->line + ctx->val;
2359 smp->ctx.a[1] = smp->ctx.a[0] + ctx->vlen;
2360 }
2361
2362 smp->data.type = SMP_T_STR;
2363 smp->flags |= SMP_F_CONST;
2364 smp->ctx.a[0] = http_extract_cookie_value(smp->ctx.a[0], smp->ctx.a[1],
Christopher Faulet08f475b2020-11-13 13:41:04 +01002365 cook, cook_l,
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002366 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
2367 &smp->data.u.str.area, &smp->data.u.str.data);
2368 if (smp->ctx.a[0]) {
2369 found = 1;
Maciej Zdeb22dc68b2020-11-13 09:38:06 +00002370 if (smp->opt & SMP_OPT_ITERATE) {
2371 /* iterate on cookie value */
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002372 smp->flags |= SMP_F_NOT_LAST;
2373 return 1;
2374 }
Maciej Zdeb22dc68b2020-11-13 09:38:06 +00002375 if (args->data.str.data == 0) {
2376 /* No cookie name, first occurrence returned */
2377 break;
2378 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002379 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002380 /* if we're looking for last occurrence, let's loop */
Willy Tarreau79e57332018-10-02 16:01:16 +02002381 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002382 }
2383 /* all cookie headers and values were scanned. If we're looking for the
2384 * last occurrence, we may return it now.
2385 */
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002386 out:
Willy Tarreau79e57332018-10-02 16:01:16 +02002387 smp->flags &= ~SMP_F_NOT_LAST;
2388 return found;
2389}
2390
Christopher Fauletc1f40dd2019-05-16 10:07:30 +02002391/* Same than smp_fetch_cookie() but only relies on the sample direction to
2392 * choose the right channel. So instead of duplicating the code, we just change
2393 * the keyword and then fallback on smp_fetch_cookie().
2394 */
2395static int smp_fetch_chn_cookie(const struct arg *args, struct sample *smp, const char *kw, void *private)
2396{
2397 kw = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ ? "req.cook" : "res.cook");
2398 return smp_fetch_cookie(args, smp, kw, private);
2399}
2400
Willy Tarreau79e57332018-10-02 16:01:16 +02002401/* Iterate over all cookies present in a request to count how many occurrences
2402 * match the name in args and args->data.str.len. If <multi> is non-null, then
2403 * multiple cookies may be parsed on the same line. The returned sample is of
2404 * type UINT. Accepts exactly 1 argument of type string.
2405 */
2406static int smp_fetch_cookie_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
2407{
Christopher Faulet89dc4992019-04-17 12:02:59 +02002408 /* possible keywords: req.cook_cnt / cook_cnt, res.cook_cnt / scook_cnt */
2409 struct channel *chn = ((kw[0] == 'c' || kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Willy Tarreau79e57332018-10-02 16:01:16 +02002410 char *val_beg, *val_end;
Christopher Faulet08f475b2020-11-13 13:41:04 +01002411 char *cook = NULL;
2412 size_t cook_l = 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002413 int cnt;
Willy Tarreau79e57332018-10-02 16:01:16 +02002414
Christopher Faulet08f475b2020-11-13 13:41:04 +01002415 if (args && args->type == ARGT_STR){
2416 cook = args->data.str.area;
2417 cook_l = args->data.str.data;
2418 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002419
Christopher Faulet46575cd2019-04-17 11:40:30 +02002420 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002421 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +02002422 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002423 struct http_hdr_ctx ctx;
2424 struct ist hdr;
2425
2426 if (!htx)
2427 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02002428
Christopher Faulet89dc4992019-04-17 12:02:59 +02002429 hdr = (!(chn->flags & CF_ISRESP) ? ist("Cookie") : ist("Set-Cookie"));
Willy Tarreau79e57332018-10-02 16:01:16 +02002430
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002431 val_end = val_beg = NULL;
2432 ctx.blk = NULL;
2433 cnt = 0;
2434 while (1) {
2435 /* Note: val_beg == NULL every time we need to fetch a new header */
2436 if (!val_beg) {
2437 if (!http_find_header(htx, hdr, &ctx, 0))
2438 break;
Willy Tarreau79e57332018-10-02 16:01:16 +02002439
Christopher Faulet08f475b2020-11-13 13:41:04 +01002440 if (ctx.value.len < cook_l + 1)
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002441 continue;
Willy Tarreau79e57332018-10-02 16:01:16 +02002442
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002443 val_beg = ctx.value.ptr;
2444 val_end = val_beg + ctx.value.len;
2445 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002446
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002447 smp->data.type = SMP_T_STR;
2448 smp->flags |= SMP_F_CONST;
2449 while ((val_beg = http_extract_cookie_value(val_beg, val_end,
Christopher Faulet08f475b2020-11-13 13:41:04 +01002450 cook, cook_l,
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002451 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
2452 &smp->data.u.str.area,
2453 &smp->data.u.str.data))) {
2454 cnt++;
2455 }
2456 }
2457 }
2458 else {
2459 /* LEGACY version */
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002460 struct hdr_idx *idx;
2461 struct hdr_ctx ctx;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002462 const char *hdr_name;
2463 int hdr_name_len;
2464 char *sol;
Willy Tarreau79e57332018-10-02 16:01:16 +02002465
Christopher Faulet89dc4992019-04-17 12:02:59 +02002466 CHECK_HTTP_MESSAGE_FIRST(chn);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002467
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002468 idx = &smp->strm->txn->hdr_idx;
Christopher Faulet89dc4992019-04-17 12:02:59 +02002469 if (!(chn->flags & CF_ISRESP)) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002470 hdr_name = "Cookie";
2471 hdr_name_len = 6;
2472 } else {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002473 hdr_name = "Set-Cookie";
2474 hdr_name_len = 10;
Willy Tarreau79e57332018-10-02 16:01:16 +02002475 }
2476
Christopher Faulet89dc4992019-04-17 12:02:59 +02002477 sol = ci_head(chn);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002478 val_end = val_beg = NULL;
2479 ctx.idx = 0;
2480 cnt = 0;
2481
2482 while (1) {
2483 /* Note: val_beg == NULL every time we need to fetch a new header */
2484 if (!val_beg) {
2485 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, &ctx))
2486 break;
2487
Christopher Faulet08f475b2020-11-13 13:41:04 +01002488 if (ctx.vlen < cook_l + 1)
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002489 continue;
2490
2491 val_beg = ctx.line + ctx.val;
2492 val_end = val_beg + ctx.vlen;
2493 }
2494
2495 smp->data.type = SMP_T_STR;
2496 smp->flags |= SMP_F_CONST;
2497 while ((val_beg = http_extract_cookie_value(val_beg, val_end,
Christopher Faulet08f475b2020-11-13 13:41:04 +01002498 cook, cook_l,
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002499 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
2500 &smp->data.u.str.area, &smp->data.u.str.data))) {
2501 cnt++;
2502 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002503 }
2504 }
2505
2506 smp->data.type = SMP_T_SINT;
2507 smp->data.u.sint = cnt;
2508 smp->flags |= SMP_F_VOL_HDR;
2509 return 1;
2510}
2511
2512/* Fetch an cookie's integer value. The integer value is returned. It
2513 * takes a mandatory argument of type string. It relies on smp_fetch_cookie().
2514 */
2515static int smp_fetch_cookie_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
2516{
2517 int ret = smp_fetch_cookie(args, smp, kw, private);
2518
2519 if (ret > 0) {
2520 smp->data.type = SMP_T_SINT;
2521 smp->data.u.sint = strl2ic(smp->data.u.str.area,
2522 smp->data.u.str.data);
2523 }
2524
2525 return ret;
2526}
2527
2528/************************************************************************/
2529/* The code below is dedicated to sample fetches */
2530/************************************************************************/
2531
2532/* This scans a URL-encoded query string. It takes an optionally wrapping
2533 * string whose first contigous chunk has its beginning in ctx->a[0] and end
2534 * in ctx->a[1], and the optional second part in (ctx->a[2]..ctx->a[3]). The
2535 * pointers are updated for next iteration before leaving.
2536 */
2537static int smp_fetch_param(char delim, const char *name, int name_len, const struct arg *args, struct sample *smp, const char *kw, void *private)
2538{
2539 const char *vstart, *vend;
2540 struct buffer *temp;
2541 const char **chunks = (const char **)smp->ctx.a;
2542
2543 if (!http_find_next_url_param(chunks, name, name_len,
2544 &vstart, &vend, delim))
2545 return 0;
2546
2547 /* Create sample. If the value is contiguous, return the pointer as CONST,
2548 * if the value is wrapped, copy-it in a buffer.
2549 */
2550 smp->data.type = SMP_T_STR;
2551 if (chunks[2] &&
2552 vstart >= chunks[0] && vstart <= chunks[1] &&
2553 vend >= chunks[2] && vend <= chunks[3]) {
2554 /* Wrapped case. */
2555 temp = get_trash_chunk();
2556 memcpy(temp->area, vstart, chunks[1] - vstart);
2557 memcpy(temp->area + ( chunks[1] - vstart ), chunks[2],
2558 vend - chunks[2]);
2559 smp->data.u.str.area = temp->area;
2560 smp->data.u.str.data = ( chunks[1] - vstart ) + ( vend - chunks[2] );
2561 } else {
2562 /* Contiguous case. */
2563 smp->data.u.str.area = (char *)vstart;
2564 smp->data.u.str.data = vend - vstart;
2565 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
2566 }
2567
2568 /* Update context, check wrapping. */
2569 chunks[0] = vend;
2570 if (chunks[2] && vend >= chunks[2] && vend <= chunks[3]) {
2571 chunks[1] = chunks[3];
2572 chunks[2] = NULL;
2573 }
2574
2575 if (chunks[0] < chunks[1])
2576 smp->flags |= SMP_F_NOT_LAST;
2577
2578 return 1;
2579}
2580
2581/* This function iterates over each parameter of the query string. It uses
2582 * ctx->a[0] and ctx->a[1] to store the beginning and end of the current
2583 * parameter. Since it uses smp_fetch_param(), ctx->a[2..3] are both NULL.
2584 * An optional parameter name is passed in args[0], otherwise any parameter is
2585 * considered. It supports an optional delimiter argument for the beginning of
2586 * the string in args[1], which defaults to "?".
2587 */
2588static int smp_fetch_url_param(const struct arg *args, struct sample *smp, const char *kw, void *private)
2589{
Christopher Faulet89dc4992019-04-17 12:02:59 +02002590 struct channel *chn = SMP_REQ_CHN(smp);
Willy Tarreau79e57332018-10-02 16:01:16 +02002591 char delim = '?';
2592 const char *name;
2593 int name_len;
2594
2595 if (!args ||
2596 (args[0].type && args[0].type != ARGT_STR) ||
2597 (args[1].type && args[1].type != ARGT_STR))
2598 return 0;
2599
2600 name = "";
2601 name_len = 0;
2602 if (args->type == ARGT_STR) {
2603 name = args->data.str.area;
2604 name_len = args->data.str.data;
2605 }
2606
2607 if (args[1].type)
2608 delim = *args[1].data.str.area;
2609
2610 if (!smp->ctx.a[0]) { // first call, find the query string
Christopher Faulet46575cd2019-04-17 11:40:30 +02002611 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002612 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +02002613 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01002614 struct htx_sl *sl;
Willy Tarreau79e57332018-10-02 16:01:16 +02002615
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002616 if (!htx)
2617 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02002618
Christopher Faulet297fbb42019-05-13 14:41:27 +02002619 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01002620 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 +02002621 if (!smp->ctx.a[0])
2622 return 0;
2623
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01002624 smp->ctx.a[1] = HTX_SL_REQ_UPTR(sl) + HTX_SL_REQ_ULEN(sl);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002625 }
2626 else {
2627 /* LEGACY version */
2628 struct http_msg *msg;
2629
Christopher Faulet89dc4992019-04-17 12:02:59 +02002630 CHECK_HTTP_MESSAGE_FIRST(chn);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002631
2632 msg = &smp->strm->txn->req;
2633
Christopher Faulet89dc4992019-04-17 12:02:59 +02002634 smp->ctx.a[0] = http_find_param_list(ci_head(chn) + msg->sl.rq.u,
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002635 msg->sl.rq.u_l, delim);
2636 if (!smp->ctx.a[0])
2637 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02002638
Christopher Faulet89dc4992019-04-17 12:02:59 +02002639 smp->ctx.a[1] = ci_head(chn) + msg->sl.rq.u + msg->sl.rq.u_l;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002640 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002641
2642 /* Assume that the context is filled with NULL pointer
2643 * before the first call.
2644 * smp->ctx.a[2] = NULL;
2645 * smp->ctx.a[3] = NULL;
2646 */
2647 }
2648
2649 return smp_fetch_param(delim, name, name_len, args, smp, kw, private);
2650}
2651
2652/* This function iterates over each parameter of the body. This requires
2653 * that the body has been waited for using http-buffer-request. It uses
2654 * ctx->a[0] and ctx->a[1] to store the beginning and end of the first
2655 * contigous part of the body, and optionally ctx->a[2..3] to reference the
2656 * optional second part if the body wraps at the end of the buffer. An optional
2657 * parameter name is passed in args[0], otherwise any parameter is considered.
2658 */
2659static int smp_fetch_body_param(const struct arg *args, struct sample *smp, const char *kw, void *private)
2660{
Christopher Faulet89dc4992019-04-17 12:02:59 +02002661 struct channel *chn = SMP_REQ_CHN(smp);
Willy Tarreau79e57332018-10-02 16:01:16 +02002662 const char *name;
2663 int name_len;
2664
2665 if (!args || (args[0].type && args[0].type != ARGT_STR))
2666 return 0;
2667
2668 name = "";
2669 name_len = 0;
2670 if (args[0].type == ARGT_STR) {
2671 name = args[0].data.str.area;
2672 name_len = args[0].data.str.data;
2673 }
2674
2675 if (!smp->ctx.a[0]) { // first call, find the query string
Christopher Faulet46575cd2019-04-17 11:40:30 +02002676 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002677 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +02002678 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002679 struct buffer *temp;
2680 int32_t pos;
Willy Tarreau79e57332018-10-02 16:01:16 +02002681
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002682 if (!htx)
2683 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02002684
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002685 temp = get_trash_chunk();
Christopher Fauleta3f15502019-05-13 15:27:23 +02002686 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002687 struct htx_blk *blk = htx_get_blk(htx, pos);
2688 enum htx_blk_type type = htx_get_blk_type(blk);
Willy Tarreau79e57332018-10-02 16:01:16 +02002689
Christopher Faulet54b5e212019-06-04 10:08:28 +02002690 if (type == HTX_BLK_EOM || type == HTX_BLK_TLR || type == HTX_BLK_EOT)
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002691 break;
2692 if (type == HTX_BLK_DATA) {
Christopher Fauletc59ff232018-12-03 13:58:44 +01002693 if (!htx_data_to_h1(htx_get_blk_value(htx, blk), temp, 0))
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002694 return 0;
2695 }
2696 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002697
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002698 smp->ctx.a[0] = temp->area;
2699 smp->ctx.a[1] = temp->area + temp->data;
Willy Tarreau79e57332018-10-02 16:01:16 +02002700
2701 /* Assume that the context is filled with NULL pointer
2702 * before the first call.
2703 * smp->ctx.a[2] = NULL;
2704 * smp->ctx.a[3] = NULL;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002705 */
Willy Tarreau79e57332018-10-02 16:01:16 +02002706 }
2707 else {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002708 /* LEGACY version */
2709 struct http_msg *msg;
2710 unsigned long len;
2711 unsigned long block1;
2712 char *body;
2713
Christopher Faulet89dc4992019-04-17 12:02:59 +02002714 CHECK_HTTP_MESSAGE_FIRST(chn);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002715
Christopher Faulet89dc4992019-04-17 12:02:59 +02002716 msg = &smp->strm->txn->req;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002717 len = http_body_bytes(msg);
Christopher Faulet89dc4992019-04-17 12:02:59 +02002718 body = c_ptr(chn, -http_data_rewind(msg));
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002719
2720 block1 = len;
Christopher Faulet89dc4992019-04-17 12:02:59 +02002721 if (block1 > b_wrap(&chn->buf) - body)
2722 block1 = b_wrap(&chn->buf) - body;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002723
2724 if (block1 == len) {
2725 /* buffer is not wrapped (or empty) */
2726 smp->ctx.a[0] = body;
2727 smp->ctx.a[1] = body + len;
2728
2729 /* Assume that the context is filled with NULL pointer
2730 * before the first call.
2731 * smp->ctx.a[2] = NULL;
2732 * smp->ctx.a[3] = NULL;
2733 */
2734 }
2735 else {
2736 /* buffer is wrapped, we need to defragment it */
2737 smp->ctx.a[0] = body;
2738 smp->ctx.a[1] = body + block1;
Christopher Faulet89dc4992019-04-17 12:02:59 +02002739 smp->ctx.a[2] = b_orig(&chn->buf);
2740 smp->ctx.a[3] = b_orig(&chn->buf) + ( len - block1 );
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002741 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002742 }
2743 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002744
Willy Tarreau79e57332018-10-02 16:01:16 +02002745 return smp_fetch_param('&', name, name_len, args, smp, kw, private);
2746}
2747
2748/* Return the signed integer value for the specified url parameter (see url_param
2749 * above).
2750 */
2751static int smp_fetch_url_param_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
2752{
2753 int ret = smp_fetch_url_param(args, smp, kw, private);
2754
2755 if (ret > 0) {
2756 smp->data.type = SMP_T_SINT;
2757 smp->data.u.sint = strl2ic(smp->data.u.str.area,
2758 smp->data.u.str.data);
2759 }
2760
2761 return ret;
2762}
2763
2764/* This produces a 32-bit hash of the concatenation of the first occurrence of
2765 * the Host header followed by the path component if it begins with a slash ('/').
2766 * This means that '*' will not be added, resulting in exactly the first Host
2767 * entry. If no Host header is found, then the path is used. The resulting value
2768 * is hashed using the url hash followed by a full avalanche hash and provides a
2769 * 32-bit integer value. This fetch is useful for tracking per-URL activity on
2770 * high-traffic sites without having to store whole paths.
2771 * this differs from the base32 functions in that it includes the url parameters
2772 * as well as the path
2773 */
2774static int smp_fetch_url32(const struct arg *args, struct sample *smp, const char *kw, void *private)
2775{
Christopher Faulet89dc4992019-04-17 12:02:59 +02002776 struct channel *chn = SMP_REQ_CHN(smp);
Willy Tarreau79e57332018-10-02 16:01:16 +02002777 unsigned int hash = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02002778
Christopher Faulet46575cd2019-04-17 11:40:30 +02002779 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002780 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +02002781 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002782 struct http_hdr_ctx ctx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01002783 struct htx_sl *sl;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002784 struct ist path;
Willy Tarreau79e57332018-10-02 16:01:16 +02002785
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002786 if (!htx)
2787 return 0;
2788
2789 ctx.blk = NULL;
2790 if (http_find_header(htx, ist("Host"), &ctx, 1)) {
2791 /* OK we have the header value in ctx.value */
2792 while (ctx.value.len--)
2793 hash = *(ctx.value.ptr++) + (hash << 6) + (hash << 16) - hash;
2794 }
2795
2796 /* now retrieve the path */
Christopher Faulet297fbb42019-05-13 14:41:27 +02002797 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01002798 path = http_get_path(htx_sl_req_uri(sl));
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002799 if (path.len && *(path.ptr) == '/') {
2800 while (path.len--)
2801 hash = *(path.ptr++) + (hash << 6) + (hash << 16) - hash;
2802 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002803 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002804 else {
2805 /* LEGACY version */
2806 struct http_txn *txn;
2807 struct hdr_ctx ctx;
2808 char *ptr, *beg, *end;
2809 int len;
2810
Christopher Faulet89dc4992019-04-17 12:02:59 +02002811 CHECK_HTTP_MESSAGE_FIRST(chn);
Willy Tarreau79e57332018-10-02 16:01:16 +02002812
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002813 txn = smp->strm->txn;
2814 ctx.idx = 0;
Christopher Faulet89dc4992019-04-17 12:02:59 +02002815 if (http_find_header2("Host", 4, ci_head(chn), &txn->hdr_idx, &ctx)) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002816 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
2817 ptr = ctx.line + ctx.val;
2818 len = ctx.vlen;
2819 while (len--)
2820 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
2821 }
2822
2823 /* now retrieve the path */
Christopher Faulet89dc4992019-04-17 12:02:59 +02002824 end = ci_head(chn) + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002825 beg = http_txn_get_path(txn);
2826 if (!beg)
2827 beg = end;
Willy Tarreau79e57332018-10-02 16:01:16 +02002828
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002829 for (ptr = beg; ptr < end ; ptr++);
Willy Tarreau79e57332018-10-02 16:01:16 +02002830
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002831 if (beg < ptr && *beg == '/') {
2832 while (beg < ptr)
2833 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
2834 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002835 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002836
Willy Tarreau79e57332018-10-02 16:01:16 +02002837 hash = full_hash(hash);
2838
2839 smp->data.type = SMP_T_SINT;
2840 smp->data.u.sint = hash;
2841 smp->flags = SMP_F_VOL_1ST;
2842 return 1;
2843}
2844
2845/* This concatenates the source address with the 32-bit hash of the Host and
2846 * URL as returned by smp_fetch_base32(). The idea is to have per-source and
2847 * per-url counters. The result is a binary block from 8 to 20 bytes depending
2848 * on the source address length. The URL hash is stored before the address so
2849 * that in environments where IPv6 is insignificant, truncating the output to
2850 * 8 bytes would still work.
2851 */
2852static int smp_fetch_url32_src(const struct arg *args, struct sample *smp, const char *kw, void *private)
2853{
2854 struct buffer *temp;
2855 struct connection *cli_conn = objt_conn(smp->sess->origin);
2856
2857 if (!cli_conn)
2858 return 0;
2859
2860 if (!smp_fetch_url32(args, smp, kw, private))
2861 return 0;
2862
2863 temp = get_trash_chunk();
2864 *(unsigned int *) temp->area = htonl(smp->data.u.sint);
2865 temp->data += sizeof(unsigned int);
2866
2867 switch (cli_conn->addr.from.ss_family) {
2868 case AF_INET:
2869 memcpy(temp->area + temp->data,
2870 &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr,
2871 4);
2872 temp->data += 4;
2873 break;
2874 case AF_INET6:
2875 memcpy(temp->area + temp->data,
2876 &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr,
2877 16);
2878 temp->data += 16;
2879 break;
2880 default:
2881 return 0;
2882 }
2883
2884 smp->data.u.str = *temp;
2885 smp->data.type = SMP_T_BIN;
2886 return 1;
2887}
2888
2889/************************************************************************/
2890/* Other utility functions */
2891/************************************************************************/
2892
2893/* This function is used to validate the arguments passed to any "hdr" fetch
2894 * keyword. These keywords support an optional positive or negative occurrence
2895 * number. We must ensure that the number is greater than -MAX_HDR_HISTORY. It
2896 * is assumed that the types are already the correct ones. Returns 0 on error,
2897 * non-zero if OK. If <err> is not NULL, it will be filled with a pointer to an
2898 * error message in case of error, that the caller is responsible for freeing.
2899 * The initial location must either be freeable or NULL.
2900 * Note: this function's pointer is checked from Lua.
2901 */
2902int val_hdr(struct arg *arg, char **err_msg)
2903{
2904 if (arg && arg[1].type == ARGT_SINT && arg[1].data.sint < -MAX_HDR_HISTORY) {
2905 memprintf(err_msg, "header occurrence must be >= %d", -MAX_HDR_HISTORY);
2906 return 0;
2907 }
2908 return 1;
2909}
2910
2911/************************************************************************/
2912/* All supported sample fetch keywords must be declared here. */
2913/************************************************************************/
2914
2915/* Note: must not be declared <const> as its list will be overwritten */
2916static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
2917 { "base", smp_fetch_base, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2918 { "base32", smp_fetch_base32, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
2919 { "base32+src", smp_fetch_base32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
2920
2921 /* capture are allocated and are permanent in the stream */
2922 { "capture.req.hdr", smp_fetch_capture_req_hdr, ARG1(1,SINT), NULL, SMP_T_STR, SMP_USE_HRQHP },
2923
2924 /* retrieve these captures from the HTTP logs */
2925 { "capture.req.method", smp_fetch_capture_req_method, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
2926 { "capture.req.uri", smp_fetch_capture_req_uri, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
2927 { "capture.req.ver", smp_fetch_capture_req_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
2928
2929 { "capture.res.hdr", smp_fetch_capture_res_hdr, ARG1(1,SINT), NULL, SMP_T_STR, SMP_USE_HRSHP },
2930 { "capture.res.ver", smp_fetch_capture_res_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
2931
2932 /* cookie is valid in both directions (eg: for "stick ...") but cook*
2933 * are only here to match the ACL's name, are request-only and are used
2934 * for ACL compatibility only.
2935 */
2936 { "cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Christopher Fauletc1f40dd2019-05-16 10:07:30 +02002937 { "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 +02002938 { "cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2939 { "cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2940
2941 /* hdr is valid in both directions (eg: for "stick ...") but hdr_* are
2942 * only here to match the ACL's name, are request-only and are used for
2943 * ACL compatibility only.
2944 */
Christopher Fauletc1f40dd2019-05-16 10:07:30 +02002945 { "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 +02002946 { "hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2947 { "hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV },
2948 { "hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_SINT, SMP_USE_HRQHV },
2949
2950 { "http_auth", smp_fetch_http_auth, ARG1(1,USR), NULL, SMP_T_BOOL, SMP_USE_HRQHV },
2951 { "http_auth_group", smp_fetch_http_auth_grp, ARG1(1,USR), NULL, SMP_T_STR, SMP_USE_HRQHV },
2952 { "http_first_req", smp_fetch_http_first_req, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
2953 { "method", smp_fetch_meth, 0, NULL, SMP_T_METH, SMP_USE_HRQHP },
2954 { "path", smp_fetch_path, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2955 { "query", smp_fetch_query, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2956
2957 /* HTTP protocol on the request path */
2958 { "req.proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
2959 { "req_proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
2960
2961 /* HTTP version on the request path */
2962 { "req.ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2963 { "req_ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2964
2965 { "req.body", smp_fetch_body, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
2966 { "req.body_len", smp_fetch_body_len, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
2967 { "req.body_size", smp_fetch_body_size, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
2968 { "req.body_param", smp_fetch_body_param, ARG1(0,STR), NULL, SMP_T_BIN, SMP_USE_HRQHV },
2969
2970 { "req.hdrs", smp_fetch_hdrs, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
2971 { "req.hdrs_bin", smp_fetch_hdrs_bin, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
2972
2973 /* HTTP version on the response path */
2974 { "res.ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
2975 { "resp_ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
2976
2977 /* explicit req.{cook,hdr} are used to force the fetch direction to be request-only */
2978 { "req.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
2979 { "req.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2980 { "req.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2981
2982 { "req.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
2983 { "req.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2984 { "req.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
2985 { "req.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2986 { "req.hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV },
2987 { "req.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
2988 { "req.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_SINT, SMP_USE_HRQHV },
2989
2990 /* explicit req.{cook,hdr} are used to force the fetch direction to be response-only */
2991 { "res.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
2992 { "res.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2993 { "res.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2994
2995 { "res.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
2996 { "res.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2997 { "res.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
2998 { "res.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2999 { "res.hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV },
3000 { "res.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
3001 { "res.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_SINT, SMP_USE_HRSHV },
3002
3003 /* scook is valid only on the response and is used for ACL compatibility */
3004 { "scook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
3005 { "scook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
3006 { "scook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
3007 { "set-cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV }, /* deprecated */
3008
3009 /* shdr is valid only on the response and is used for ACL compatibility */
3010 { "shdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
3011 { "shdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
3012 { "shdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV },
3013 { "shdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_SINT, SMP_USE_HRSHV },
3014
3015 { "status", smp_fetch_stcode, 0, NULL, SMP_T_SINT, SMP_USE_HRSHP },
3016 { "unique-id", smp_fetch_uniqueid, 0, NULL, SMP_T_STR, SMP_SRC_L4SRV },
3017 { "url", smp_fetch_url, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
3018 { "url32", smp_fetch_url32, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
3019 { "url32+src", smp_fetch_url32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
3020 { "url_ip", smp_fetch_url_ip, 0, NULL, SMP_T_IPV4, SMP_USE_HRQHV },
3021 { "url_port", smp_fetch_url_port, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
3022 { "url_param", smp_fetch_url_param, ARG2(0,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
3023 { "urlp" , smp_fetch_url_param, ARG2(0,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
3024 { "urlp_val", smp_fetch_url_param_val, ARG2(0,STR,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
3025 { /* END */ },
3026}};
3027
Willy Tarreau0108d902018-11-25 19:14:37 +01003028INITCALL1(STG_REGISTER, sample_register_fetches, &sample_fetch_keywords);
Willy Tarreau79e57332018-10-02 16:01:16 +02003029
3030/*
3031 * Local variables:
3032 * c-indent-level: 8
3033 * c-basic-offset: 8
3034 * End:
3035 */