blob: b3a5ea54b22514cae3877e7031a99b4057e4a01b [file] [log] [blame]
Willy Tarreau79e57332018-10-02 16:01:16 +02001/*
2 * HTTP samples fetching
3 *
4 * Copyright 2000-2018 Willy Tarreau <w@1wt.eu>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
13#include <sys/types.h>
14
15#include <ctype.h>
16#include <string.h>
17#include <time.h>
18
19#include <common/base64.h>
20#include <common/chunk.h>
21#include <common/compat.h>
22#include <common/config.h>
23#include <common/debug.h>
Willy Tarreauafba57a2018-12-11 13:44:24 +010024#include <common/h1.h>
Willy Tarreau79e57332018-10-02 16:01:16 +020025#include <common/http.h>
Willy Tarreaub96b77e2018-12-11 10:22:41 +010026#include <common/htx.h>
Willy Tarreau0108d902018-11-25 19:14:37 +010027#include <common/initcall.h>
Willy Tarreau79e57332018-10-02 16:01:16 +020028#include <common/memory.h>
29#include <common/standard.h>
30#include <common/version.h>
31
32#include <types/global.h>
33
34#include <proto/arg.h>
35#include <proto/auth.h>
Willy Tarreau538746a2018-12-11 10:59:20 +010036#include <proto/hdr_idx.h>
Willy Tarreau79e57332018-10-02 16:01:16 +020037#include <proto/http_fetch.h>
Christopher Fauletef453ed2018-10-24 21:39:27 +020038#include <proto/http_htx.h>
Willy Tarreau79e57332018-10-02 16:01:16 +020039#include <proto/log.h>
40#include <proto/obj_type.h>
41#include <proto/proto_http.h>
42#include <proto/sample.h>
43#include <proto/stream.h>
44
45
46/* this struct is used between calls to smp_fetch_hdr() or smp_fetch_cookie() */
47static THREAD_LOCAL struct hdr_ctx static_hdr_ctx;
Christopher Fauletef453ed2018-10-24 21:39:27 +020048static THREAD_LOCAL struct http_hdr_ctx static_http_hdr_ctx;
Richard Russoc0968f52019-07-31 11:45:56 -070049/* this is used to convert raw connection buffers to htx */
50static THREAD_LOCAL struct buffer static_raw_htx_chunk;
51static THREAD_LOCAL char *static_raw_htx_buf;
Christopher Fauletef453ed2018-10-24 21:39:27 +020052
Christopher Faulet89dc4992019-04-17 12:02:59 +020053#define SMP_REQ_CHN(smp) (smp->strm ? &smp->strm->req : NULL)
54#define SMP_RES_CHN(smp) (smp->strm ? &smp->strm->res : NULL)
Willy Tarreau79e57332018-10-02 16:01:16 +020055
Richard Russoc0968f52019-07-31 11:45:56 -070056/* This function returns the static htx chunk, where raw connections get
57 * converted to HTX as needed for samplxsing.
58 */
59struct buffer *get_raw_htx_chunk(void)
60{
61 chunk_reset(&static_raw_htx_chunk);
62 return &static_raw_htx_chunk;
63}
64
65static int alloc_raw_htx_chunk_per_thread()
66{
67 static_raw_htx_buf = malloc(global.tune.bufsize);
68 if (!static_raw_htx_buf)
69 return 0;
70 chunk_init(&static_raw_htx_chunk, static_raw_htx_buf, global.tune.bufsize);
71 return 1;
72}
73
74static void free_raw_htx_chunk_per_thread()
75{
76 free(static_raw_htx_buf);
77 static_raw_htx_buf = NULL;
78}
79
80REGISTER_PER_THREAD_ALLOC(alloc_raw_htx_chunk_per_thread);
81REGISTER_PER_THREAD_FREE(free_raw_htx_chunk_per_thread);
82
Willy Tarreau79e57332018-10-02 16:01:16 +020083/*
84 * Returns the data from Authorization header. Function may be called more
85 * than once so data is stored in txn->auth_data. When no header is found
86 * or auth method is unknown auth_method is set to HTTP_AUTH_WRONG to avoid
87 * searching again for something we are unable to find anyway. However, if
88 * the result if valid, the cache is not reused because we would risk to
89 * have the credentials overwritten by another stream in parallel.
90 */
91
Christopher Faulete98411b2019-07-15 13:58:29 +020092static int get_http_auth(struct sample *smp, struct htx *htx)
Willy Tarreau79e57332018-10-02 16:01:16 +020093{
Christopher Faulet311c7ea2018-10-24 21:41:55 +020094 struct stream *s = smp->strm;
Willy Tarreau79e57332018-10-02 16:01:16 +020095 struct http_txn *txn = s->txn;
96 struct buffer auth_method;
Willy Tarreau79e57332018-10-02 16:01:16 +020097 char *h, *p;
98 int len;
99
100#ifdef DEBUG_AUTH
101 printf("Auth for stream %p: %d\n", s, txn->auth.method);
102#endif
Willy Tarreau79e57332018-10-02 16:01:16 +0200103 if (txn->auth.method == HTTP_AUTH_WRONG)
104 return 0;
105
106 txn->auth.method = HTTP_AUTH_WRONG;
107
Christopher Faulete98411b2019-07-15 13:58:29 +0200108 if (htx) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200109 /* HTX version */
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200110 struct http_hdr_ctx ctx = { .blk = NULL };
111 struct ist hdr;
Willy Tarreau79e57332018-10-02 16:01:16 +0200112
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200113 if (txn->flags & TX_USE_PX_CONN)
114 hdr = ist("Proxy-Authorization");
115 else
116 hdr = ist("Authorization");
117
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200118 ctx.blk = NULL;
119 if (!http_find_header(htx, hdr, &ctx, 0))
120 return 0;
121
122 p = memchr(ctx.value.ptr, ' ', ctx.value.len);
123 len = p - ctx.value.ptr;
124 if (!p || len <= 0)
125 return 0;
126
127 if (chunk_initlen(&auth_method, ctx.value.ptr, 0, len) != 1)
128 return 0;
129
130 chunk_initlen(&txn->auth.method_data, p + 1, 0, ctx.value.len - len - 1);
Willy Tarreau79e57332018-10-02 16:01:16 +0200131 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200132 else {
133 /* LEGACY version */
134 struct hdr_ctx ctx = { .idx = 0 };
Willy Tarreau79e57332018-10-02 16:01:16 +0200135
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200136 if (txn->flags & TX_USE_PX_CONN) {
137 h = "Proxy-Authorization";
138 len = strlen(h);
139 } else {
140 h = "Authorization";
141 len = strlen(h);
142 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200143
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200144 if (!http_find_header2(h, len, ci_head(&s->req), &txn->hdr_idx, &ctx))
145 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200146
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200147 h = ctx.line + ctx.val;
Willy Tarreau79e57332018-10-02 16:01:16 +0200148
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200149 p = memchr(h, ' ', ctx.vlen);
150 len = p - h;
151 if (!p || len <= 0)
152 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200153
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200154 if (chunk_initlen(&auth_method, h, 0, len) != 1)
155 return 0;
156
157 chunk_initlen(&txn->auth.method_data, p + 1, 0, ctx.vlen - len - 1);
158 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200159
160 if (!strncasecmp("Basic", auth_method.area, auth_method.data)) {
161 struct buffer *http_auth = get_trash_chunk();
162
163 len = base64dec(txn->auth.method_data.area,
164 txn->auth.method_data.data,
165 http_auth->area, global.tune.bufsize - 1);
166
167 if (len < 0)
168 return 0;
169
170
171 http_auth->area[len] = '\0';
172
173 p = strchr(http_auth->area, ':');
174
175 if (!p)
176 return 0;
177
178 txn->auth.user = http_auth->area;
179 *p = '\0';
180 txn->auth.pass = p+1;
181
182 txn->auth.method = HTTP_AUTH_BASIC;
183 return 1;
184 }
185
186 return 0;
187}
188
189/* This function ensures that the prerequisites for an L7 fetch are ready,
190 * which means that a request or response is ready. If some data is missing,
191 * a parsing attempt is made. This is useful in TCP-based ACLs which are able
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +0200192 * to extract data from L7. If <vol> is non-null during a prefetch, another
193 * test is made to ensure the required information is not gone.
Christopher Fauletef453ed2018-10-24 21:39:27 +0200194 *
195 * The function returns :
196 * NULL with SMP_F_MAY_CHANGE in the sample flags if some data is missing to
197 * decide whether or not an HTTP message is present ;
198 * NULL if the requested data cannot be fetched or if it is certain that
199 * we'll never have any HTTP message there ;
Willy Tarreau91d0b602020-08-12 14:04:52 +0200200 * NULL if the sample's direction does not match the channel's (i.e. the
201 * function was asked to work on the wrong channel)
Christopher Fauletef453ed2018-10-24 21:39:27 +0200202 * The HTX message if ready
203 */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +0200204struct htx *smp_prefetch_htx(struct sample *smp, struct channel *chn, int vol)
Christopher Fauletef453ed2018-10-24 21:39:27 +0200205{
Christopher Fauletef453ed2018-10-24 21:39:27 +0200206 struct stream *s = smp->strm;
Christopher Fauletef453ed2018-10-24 21:39:27 +0200207 struct http_txn *txn = NULL;
208 struct htx *htx = NULL;
Christopher Faulet89dc4992019-04-17 12:02:59 +0200209 struct http_msg *msg;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100210 struct htx_sl *sl;
Christopher Fauletef453ed2018-10-24 21:39:27 +0200211
212 /* Note: it is possible that <s> is NULL when called before stream
213 * initialization (eg: tcp-request connection), so this function is the
214 * one responsible for guarding against this case for all HTTP users.
215 */
Christopher Faulet89dc4992019-04-17 12:02:59 +0200216 if (!s || !chn)
Christopher Fauletef453ed2018-10-24 21:39:27 +0200217 return NULL;
218
Willy Tarreau91d0b602020-08-12 14:04:52 +0200219 if (((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ && (chn->flags & CF_ISRESP)) ||
220 ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES && !(chn->flags & CF_ISRESP)))
221 return 0;
222
Christopher Fauletef453ed2018-10-24 21:39:27 +0200223 if (!s->txn) {
224 if (unlikely(!http_alloc_txn(s)))
225 return NULL; /* not enough memory */
226 http_init_txn(s);
227 txn = s->txn;
228 }
Christopher Faulet89dc4992019-04-17 12:02:59 +0200229 txn = s->txn;
230 msg = (!(chn->flags & CF_ISRESP) ? &txn->req : &txn->rsp);
Christopher Fauletef453ed2018-10-24 21:39:27 +0200231
Christopher Fauleteca88542019-04-03 10:12:42 +0200232 if (IS_HTX_STRM(s)) {
Christopher Faulet89dc4992019-04-17 12:02:59 +0200233 htx = htxbuf(&chn->buf);
Christopher Fauletef453ed2018-10-24 21:39:27 +0200234
Christopher Faulet89dc4992019-04-17 12:02:59 +0200235 if (msg->msg_state == HTTP_MSG_ERROR || (htx->flags & HTX_FL_PARSING_ERROR))
236 return NULL;
Christopher Fauletef453ed2018-10-24 21:39:27 +0200237
Christopher Faulet89dc4992019-04-17 12:02:59 +0200238 if (msg->msg_state < HTTP_MSG_BODY) {
239 /* Analyse not yet started */
Christopher Faulet29f17582019-05-23 11:03:26 +0200240 if (htx_is_empty(htx) || htx->first == -1) {
Christopher Fauletef453ed2018-10-24 21:39:27 +0200241 /* Parsing is done by the mux, just wait */
242 smp->flags |= SMP_F_MAY_CHANGE;
243 return NULL;
244 }
245 }
Christopher Faulet297fbb42019-05-13 14:41:27 +0200246 sl = http_get_stline(htx);
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +0200247 if (vol && !sl) {
Christopher Faulet89dc4992019-04-17 12:02:59 +0200248 /* The start-line was already forwarded, it is too late to fetch anything */
249 return NULL;
250 }
Christopher Fauletef453ed2018-10-24 21:39:27 +0200251 }
Christopher Fauleteca88542019-04-03 10:12:42 +0200252 else { /* RAW mode */
Christopher Faulet89dc4992019-04-17 12:02:59 +0200253 struct buffer *buf;
254 struct h1m h1m;
Christopher Faulete4ab11b2019-06-11 15:05:37 +0200255 struct http_hdr hdrs[global.tune.max_http_hdr];
Christopher Faulet89dc4992019-04-17 12:02:59 +0200256 union h1_sl h1sl;
257 unsigned int flags = HTX_FL_NONE;
258 int ret;
Christopher Fauletef453ed2018-10-24 21:39:27 +0200259
Christopher Faulet89dc4992019-04-17 12:02:59 +0200260 /* no HTTP fetch on the response in TCP mode */
261 if (chn->flags & CF_ISRESP)
262 return NULL;
Christopher Fauletef453ed2018-10-24 21:39:27 +0200263
Christopher Faulet89dc4992019-04-17 12:02:59 +0200264 /* Now we are working on the request only */
265 buf = &chn->buf;
266 if (b_head(buf) + b_data(buf) > b_wrap(buf))
267 b_slow_realign(buf, trash.area, 0);
Christopher Fauletef453ed2018-10-24 21:39:27 +0200268
Christopher Faulet89dc4992019-04-17 12:02:59 +0200269 h1m_init_req(&h1m);
270 ret = h1_headers_to_hdr_list(b_head(buf), b_stop(buf),
271 hdrs, sizeof(hdrs)/sizeof(hdrs[0]), &h1m, &h1sl);
272 if (ret <= 0) {
273 /* Invalid or too big*/
274 if (ret < 0 || channel_full(&s->req, global.tune.maxrewrite))
Christopher Fauletef453ed2018-10-24 21:39:27 +0200275 return NULL;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100276
Christopher Faulet89dc4992019-04-17 12:02:59 +0200277 /* wait for a full request */
278 smp->flags |= SMP_F_MAY_CHANGE;
279 return NULL;
280 }
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100281
Christopher Faulet89dc4992019-04-17 12:02:59 +0200282 /* OK we just got a valid HTTP mesage. We have to convert it
283 * into an HTX message.
284 */
285 if (unlikely(h1sl.rq.v.len == 0)) {
286 /* try to convert HTTP/0.9 requests to HTTP/1.0 */
287 if (h1sl.rq.meth != HTTP_METH_GET || !h1sl.rq.u.len)
Christopher Fauletef453ed2018-10-24 21:39:27 +0200288 return NULL;
Christopher Faulet89dc4992019-04-17 12:02:59 +0200289 h1sl.rq.v = ist("HTTP/1.0");
Christopher Fauletef453ed2018-10-24 21:39:27 +0200290 }
Christopher Faulet89dc4992019-04-17 12:02:59 +0200291
292 /* Set HTX start-line flags */
293 if (h1m.flags & H1_MF_VER_11)
294 flags |= HTX_SL_F_VER_11;
295 if (h1m.flags & H1_MF_XFER_ENC)
296 flags |= HTX_SL_F_XFER_ENC;
297 flags |= HTX_SL_F_XFER_LEN;
298 if (h1m.flags & H1_MF_CHNK)
299 flags |= HTX_SL_F_CHNK;
300 else if (h1m.flags & H1_MF_CLEN)
301 flags |= HTX_SL_F_CLEN;
302
Richard Russoc0968f52019-07-31 11:45:56 -0700303 htx = htx_from_buf(get_raw_htx_chunk());
Christopher Faulet89dc4992019-04-17 12:02:59 +0200304 sl = htx_add_stline(htx, HTX_BLK_REQ_SL, flags, h1sl.rq.m, h1sl.rq.u, h1sl.rq.v);
305 if (!sl || !htx_add_all_headers(htx, hdrs))
Christopher Fauletef453ed2018-10-24 21:39:27 +0200306 return NULL;
Willy Tarreauce9bbf52019-05-13 08:32:31 +0200307 sl->info.req.meth = h1sl.rq.meth;
Christopher Faulet89dc4992019-04-17 12:02:59 +0200308 }
309
310 /* OK we just got a valid HTTP message. If not already done by
311 * HTTP analyzers, we have some minor preparation to perform so
312 * that further checks can rely on HTTP tests.
313 */
314 if (sl && msg->msg_state < HTTP_MSG_BODY) {
315 if (!(chn->flags & CF_ISRESP)) {
316 txn->meth = sl->info.req.meth;
317 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
318 s->flags |= SF_REDIRECTABLE;
Christopher Fauletef453ed2018-10-24 21:39:27 +0200319 }
Christopher Faulet89dc4992019-04-17 12:02:59 +0200320 else
321 txn->status = sl->info.res.status;
322 if (sl->flags & HTX_SL_F_VER_11)
323 msg->flags |= HTTP_MSGF_VER_11;
Christopher Fauletef453ed2018-10-24 21:39:27 +0200324 }
325
326 /* everything's OK */
Christopher Fauletef453ed2018-10-24 21:39:27 +0200327 return htx;
328}
329
330/* This function ensures that the prerequisites for an L7 fetch are ready,
331 * which means that a request or response is ready. If some data is missing,
332 * a parsing attempt is made. This is useful in TCP-based ACLs which are able
Willy Tarreau79e57332018-10-02 16:01:16 +0200333 * to extract data from L7. If <req_vol> is non-null during a request prefetch,
334 * another test is made to ensure the required information is not gone.
335 *
336 * The function returns :
337 * 0 with SMP_F_MAY_CHANGE in the sample flags if some data is missing to
338 * decide whether or not an HTTP message is present ;
339 * 0 if the requested data cannot be fetched or if it is certain that
340 * we'll never have any HTTP message there ;
341 * 1 if an HTTP message is ready
342 */
343int smp_prefetch_http(struct proxy *px, struct stream *s, unsigned int opt,
Christopher Faulet89dc4992019-04-17 12:02:59 +0200344 struct channel *chn, struct sample *smp, int req_vol)
Willy Tarreau79e57332018-10-02 16:01:16 +0200345{
346 struct http_txn *txn;
347 struct http_msg *msg;
348
349 /* Note: it is possible that <s> is NULL when called before stream
350 * initialization (eg: tcp-request connection), so this function is the
351 * one responsible for guarding against this case for all HTTP users.
352 */
Christopher Faulet89dc4992019-04-17 12:02:59 +0200353 if (!s || !chn)
Willy Tarreau79e57332018-10-02 16:01:16 +0200354 return 0;
355
356 if (!s->txn) {
357 if (unlikely(!http_alloc_txn(s)))
358 return 0; /* not enough memory */
359 http_init_txn(s);
360 }
361 txn = s->txn;
Willy Tarreau79e57332018-10-02 16:01:16 +0200362 smp->data.type = SMP_T_BOOL;
363
Christopher Faulet89dc4992019-04-17 12:02:59 +0200364 if (chn->flags & CF_ISRESP) {
365 /* Check for a dependency on a response */
366 if (txn->rsp.msg_state < HTTP_MSG_BODY) {
367 smp->flags |= SMP_F_MAY_CHANGE;
368 return 0;
369 }
370 goto end;
371 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200372
Christopher Faulet89dc4992019-04-17 12:02:59 +0200373 /* Check for a dependency on a request */
374 msg = &txn->req;
Willy Tarreau79e57332018-10-02 16:01:16 +0200375
Christopher Faulet89dc4992019-04-17 12:02:59 +0200376 if (req_vol && (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) {
377 return 0; /* data might have moved and indexes changed */
378 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200379
Christopher Faulet89dc4992019-04-17 12:02:59 +0200380 /* If the buffer does not leave enough free space at the end, we must
381 * first realign it.
382 */
383 if (ci_head(chn) > b_orig(&chn->buf) &&
384 ci_head(chn) + ci_data(chn) > b_wrap(&chn->buf) - global.tune.maxrewrite)
385 channel_slow_realign(chn, trash.area);
Willy Tarreau79e57332018-10-02 16:01:16 +0200386
Christopher Faulet89dc4992019-04-17 12:02:59 +0200387 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
388 if (msg->msg_state == HTTP_MSG_ERROR)
389 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200390
Christopher Faulet89dc4992019-04-17 12:02:59 +0200391 /* Try to decode HTTP request */
392 if (likely(msg->next < ci_data(chn)))
393 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau79e57332018-10-02 16:01:16 +0200394
Christopher Faulet89dc4992019-04-17 12:02:59 +0200395 /* Still no valid request ? */
396 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
397 if ((msg->msg_state == HTTP_MSG_ERROR) ||
398 channel_full(chn, global.tune.maxrewrite)) {
Willy Tarreau79e57332018-10-02 16:01:16 +0200399 return 0;
Christopher Faulet89dc4992019-04-17 12:02:59 +0200400 }
401 /* wait for final state */
402 smp->flags |= SMP_F_MAY_CHANGE;
403 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200404 }
405
Christopher Faulet89dc4992019-04-17 12:02:59 +0200406 /* OK we just got a valid HTTP message. We have some minor
407 * preparation to perform so that further checks can rely
408 * on HTTP tests.
409 */
410
411 /* If the message was parsed but was too large, we must absolutely
412 * return an error so that it is not processed. At the moment this
413 * cannot happen, but if the parsers are to change in the future,
414 * we want this check to be maintained.
415 */
416 if (unlikely(ci_head(chn) + ci_data(chn) >
417 b_wrap(&chn->buf) - global.tune.maxrewrite)) {
418 msg->err_state = msg->msg_state;
419 msg->msg_state = HTTP_MSG_ERROR;
420 smp->data.u.sint = 1;
421 return 1;
Willy Tarreau79e57332018-10-02 16:01:16 +0200422 }
423
Christopher Faulet89dc4992019-04-17 12:02:59 +0200424 txn->meth = find_http_meth(ci_head(chn), msg->sl.rq.m_l);
425 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
426 s->flags |= SF_REDIRECTABLE;
427
428 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
Willy Tarreau79e57332018-10-02 16:01:16 +0200429 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200430 }
431
Christopher Faulet89dc4992019-04-17 12:02:59 +0200432 end:
Willy Tarreau79e57332018-10-02 16:01:16 +0200433 /* everything's OK */
434 smp->data.u.sint = 1;
435 return 1;
436}
437
438/* This function fetches the method of current HTTP request and stores
439 * it in the global pattern struct as a chunk. There are two possibilities :
440 * - if the method is known (not HTTP_METH_OTHER), its identifier is stored
441 * in <len> and <ptr> is NULL ;
442 * - if the method is unknown (HTTP_METH_OTHER), <ptr> points to the text and
443 * <len> to its length.
444 * This is intended to be used with pat_match_meth() only.
445 */
446static int smp_fetch_meth(const struct arg *args, struct sample *smp, const char *kw, void *private)
447{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200448 struct channel *chn = SMP_REQ_CHN(smp);
Willy Tarreau79e57332018-10-02 16:01:16 +0200449 int meth;
450 struct http_txn *txn;
451
Christopher Faulet46575cd2019-04-17 11:40:30 +0200452 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200453 /* HTX version */
Willy Tarreau91d0b602020-08-12 14:04:52 +0200454 txn = smp->strm->txn;
455 if (!txn)
Willy Tarreau79e57332018-10-02 16:01:16 +0200456 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200457
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200458 meth = txn->meth;
459 smp->data.type = SMP_T_METH;
460 smp->data.u.meth.meth = meth;
461 if (meth == HTTP_METH_OTHER) {
Willy Tarreau91d0b602020-08-12 14:04:52 +0200462 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100463 struct htx_sl *sl;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200464
Willy Tarreau91d0b602020-08-12 14:04:52 +0200465 htx = smp_prefetch_htx(smp, chn, 0);
466 if (!htx)
467 return 0;
468
Christopher Faulet89dc4992019-04-17 12:02:59 +0200469 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200470 /* ensure the indexes are not affected */
471 return 0;
Christopher Faulet89dc4992019-04-17 12:02:59 +0200472 }
Christopher Faulet297fbb42019-05-13 14:41:27 +0200473 sl = http_get_stline(htx);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200474 smp->flags |= SMP_F_CONST;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100475 smp->data.u.meth.str.area = HTX_SL_REQ_MPTR(sl);
476 smp->data.u.meth.str.data = HTX_SL_REQ_MLEN(sl);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200477 }
478 smp->flags |= SMP_F_VOL_1ST;
479 }
480 else {
481 /* LEGACY version */
Christopher Faulet89dc4992019-04-17 12:02:59 +0200482 CHECK_HTTP_MESSAGE_FIRST_PERM(chn);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200483
484 txn = smp->strm->txn;
485 meth = txn->meth;
486 smp->data.type = SMP_T_METH;
487 smp->data.u.meth.meth = meth;
488 if (meth == HTTP_METH_OTHER) {
Christopher Faulet89dc4992019-04-17 12:02:59 +0200489 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200490 /* ensure the indexes are not affected */
491 return 0;
Christopher Faulet89dc4992019-04-17 12:02:59 +0200492 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200493 smp->flags |= SMP_F_CONST;
494 smp->data.u.meth.str.data = txn->req.sl.rq.m_l;
495 smp->data.u.meth.str.area = ci_head(txn->req.chn);
496 }
497 smp->flags |= SMP_F_VOL_1ST;
Willy Tarreau79e57332018-10-02 16:01:16 +0200498 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200499 return 1;
500}
501
502static int smp_fetch_rqver(const struct arg *args, struct sample *smp, const char *kw, void *private)
503{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200504 struct channel *chn = SMP_REQ_CHN(smp);
Willy Tarreau79e57332018-10-02 16:01:16 +0200505 struct http_txn *txn;
506 char *ptr;
507 int len;
508
Christopher Faulet46575cd2019-04-17 11:40:30 +0200509 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200510 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +0200511 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100512 struct htx_sl *sl;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200513
514 if (!htx)
515 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200516
Christopher Faulet297fbb42019-05-13 14:41:27 +0200517 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100518 len = HTX_SL_REQ_VLEN(sl);
519 ptr = HTX_SL_REQ_VPTR(sl);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200520 }
521 else {
522 /* LEGACY version */
Christopher Faulet89dc4992019-04-17 12:02:59 +0200523 CHECK_HTTP_MESSAGE_FIRST(chn);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200524
525 txn = smp->strm->txn;
526 len = txn->req.sl.rq.v_l;
Christopher Faulet89dc4992019-04-17 12:02:59 +0200527 ptr = ci_head(chn) + txn->req.sl.rq.v;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200528 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200529
530 while ((len-- > 0) && (*ptr++ != '/'));
531 if (len <= 0)
532 return 0;
533
534 smp->data.type = SMP_T_STR;
535 smp->data.u.str.area = ptr;
536 smp->data.u.str.data = len;
537
538 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
539 return 1;
540}
541
542static int smp_fetch_stver(const struct arg *args, struct sample *smp, const char *kw, void *private)
543{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200544 struct channel *chn = SMP_RES_CHN(smp);
Willy Tarreau79e57332018-10-02 16:01:16 +0200545 struct http_txn *txn;
546 char *ptr;
547 int len;
548
Christopher Faulet46575cd2019-04-17 11:40:30 +0200549 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200550 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +0200551 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100552 struct htx_sl *sl;
Willy Tarreau79e57332018-10-02 16:01:16 +0200553
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200554 if (!htx)
555 return 0;
556
Christopher Faulet297fbb42019-05-13 14:41:27 +0200557 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100558 len = HTX_SL_RES_VLEN(sl);
559 ptr = HTX_SL_RES_VPTR(sl);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200560 }
561 else {
562 /* LEGACY version */
Christopher Faulet89dc4992019-04-17 12:02:59 +0200563 CHECK_HTTP_MESSAGE_FIRST(chn);
Willy Tarreau79e57332018-10-02 16:01:16 +0200564
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200565 txn = smp->strm->txn;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200566 len = txn->rsp.sl.st.v_l;
Christopher Faulet89dc4992019-04-17 12:02:59 +0200567 ptr = ci_head(chn);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200568 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200569
570 while ((len-- > 0) && (*ptr++ != '/'));
571 if (len <= 0)
572 return 0;
573
574 smp->data.type = SMP_T_STR;
575 smp->data.u.str.area = ptr;
576 smp->data.u.str.data = len;
577
578 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
579 return 1;
580}
581
582/* 3. Check on Status Code. We manipulate integers here. */
583static int smp_fetch_stcode(const struct arg *args, struct sample *smp, const char *kw, void *private)
584{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200585 struct channel *chn = SMP_RES_CHN(smp);
Willy Tarreau79e57332018-10-02 16:01:16 +0200586 struct http_txn *txn;
587 char *ptr;
588 int len;
589
Christopher Faulet46575cd2019-04-17 11:40:30 +0200590 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200591 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +0200592 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100593 struct htx_sl *sl;
Willy Tarreau79e57332018-10-02 16:01:16 +0200594
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200595 if (!htx)
596 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200597
Christopher Faulet297fbb42019-05-13 14:41:27 +0200598 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100599 len = HTX_SL_RES_CLEN(sl);
600 ptr = HTX_SL_RES_CPTR(sl);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200601 }
602 else {
603 /* LEGACY version */
Christopher Faulet89dc4992019-04-17 12:02:59 +0200604 CHECK_HTTP_MESSAGE_FIRST(chn);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200605
606 txn = smp->strm->txn;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200607 len = txn->rsp.sl.st.c_l;
Christopher Faulet89dc4992019-04-17 12:02:59 +0200608 ptr = ci_head(chn) + txn->rsp.sl.st.c;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200609 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200610
611 smp->data.type = SMP_T_SINT;
612 smp->data.u.sint = __strl2ui(ptr, len);
613 smp->flags = SMP_F_VOL_1ST;
614 return 1;
615}
616
617static int smp_fetch_uniqueid(const struct arg *args, struct sample *smp, const char *kw, void *private)
618{
619 if (LIST_ISEMPTY(&smp->sess->fe->format_unique_id))
620 return 0;
621
Willy Tarreauc1534622020-04-29 11:50:38 +0200622 if (!smp->strm)
623 return 0;
624
Willy Tarreau79e57332018-10-02 16:01:16 +0200625 if (!smp->strm->unique_id) {
626 if ((smp->strm->unique_id = pool_alloc(pool_head_uniqueid)) == NULL)
627 return 0;
628 smp->strm->unique_id[0] = '\0';
Tim Duesterhusea23a5c2020-02-26 16:20:49 +0100629 build_logline(smp->strm, smp->strm->unique_id,
630 UNIQUEID_LEN, &smp->sess->fe->format_unique_id);
Willy Tarreau79e57332018-10-02 16:01:16 +0200631 }
Tim Duesterhusea23a5c2020-02-26 16:20:49 +0100632 smp->data.u.str.data = strlen(smp->strm->unique_id);
Willy Tarreau79e57332018-10-02 16:01:16 +0200633 smp->data.type = SMP_T_STR;
634 smp->data.u.str.area = smp->strm->unique_id;
635 smp->flags = SMP_F_CONST;
636 return 1;
637}
638
639/* Returns a string block containing all headers including the
Joseph Herlant942eea32018-11-15 13:57:22 -0800640 * empty line which separes headers from the body. This is useful
641 * for some headers analysis.
Willy Tarreau79e57332018-10-02 16:01:16 +0200642 */
643static int smp_fetch_hdrs(const struct arg *args, struct sample *smp, const char *kw, void *private)
644{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200645 struct channel *chn = SMP_REQ_CHN(smp);
Willy Tarreau79e57332018-10-02 16:01:16 +0200646 struct http_txn *txn;
647
Christopher Faulet46575cd2019-04-17 11:40:30 +0200648 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200649 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +0200650 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200651 struct buffer *temp;
652 int32_t pos;
Willy Tarreau79e57332018-10-02 16:01:16 +0200653
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200654 if (!htx)
655 return 0;
656 temp = get_trash_chunk();
Christopher Fauleta3f15502019-05-13 15:27:23 +0200657 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200658 struct htx_blk *blk = htx_get_blk(htx, pos);
659 enum htx_blk_type type = htx_get_blk_type(blk);
Willy Tarreau79e57332018-10-02 16:01:16 +0200660
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200661 if (type == HTX_BLK_HDR) {
662 struct ist n = htx_get_blk_name(htx, blk);
663 struct ist v = htx_get_blk_value(htx, blk);
664
Christopher Fauletc59ff232018-12-03 13:58:44 +0100665 if (!htx_hdr_to_h1(n, v, temp))
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200666 return 0;
667 }
668 else if (type == HTX_BLK_EOH) {
669 if (!chunk_memcat(temp, "\r\n", 2))
670 return 0;
671 break;
672 }
673 }
674 smp->data.type = SMP_T_STR;
675 smp->data.u.str = *temp;
676
677 }
678 else {
679 /* LEGACY version */
680 struct http_msg *msg;
681 struct hdr_idx *idx;
682
Christopher Faulet89dc4992019-04-17 12:02:59 +0200683 CHECK_HTTP_MESSAGE_FIRST(chn);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200684
685 txn = smp->strm->txn;
686 idx = &txn->hdr_idx;
687 msg = &txn->req;
Willy Tarreau79e57332018-10-02 16:01:16 +0200688
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200689 smp->data.type = SMP_T_STR;
Christopher Faulet89dc4992019-04-17 12:02:59 +0200690 smp->data.u.str.area = ci_head(chn) + hdr_idx_first_pos(idx);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200691 smp->data.u.str.data = msg->eoh - hdr_idx_first_pos(idx) + 1 +
Christopher Faulet89dc4992019-04-17 12:02:59 +0200692 (ci_head(chn)[msg->eoh] == '\r');
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200693 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200694 return 1;
695}
696
697/* Returns the header request in a length/value encoded format.
698 * This is useful for exchanges with the SPOE.
699 *
700 * A "length value" is a multibyte code encoding numbers. It uses the
701 * SPOE format. The encoding is the following:
702 *
703 * Each couple "header name" / "header value" is composed
704 * like this:
705 * "length value" "header name bytes"
706 * "length value" "header value bytes"
707 * When the last header is reached, the header name and the header
708 * value are empty. Their length are 0
709 */
710static int smp_fetch_hdrs_bin(const struct arg *args, struct sample *smp, const char *kw, void *private)
711{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200712 struct channel *chn = SMP_REQ_CHN(smp);
Willy Tarreau79e57332018-10-02 16:01:16 +0200713 struct http_txn *txn;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200714 struct buffer *temp;
Willy Tarreau79e57332018-10-02 16:01:16 +0200715
Christopher Faulet46575cd2019-04-17 11:40:30 +0200716 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200717 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +0200718 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200719 struct buffer *temp;
720 char *p, *end;
721 int32_t pos;
722 int ret;
Willy Tarreau79e57332018-10-02 16:01:16 +0200723
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200724 if (!htx)
725 return 0;
726 temp = get_trash_chunk();
727 p = temp->area;
728 end = temp->area + temp->size;
Christopher Fauleta3f15502019-05-13 15:27:23 +0200729 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200730 struct htx_blk *blk = htx_get_blk(htx, pos);
731 enum htx_blk_type type = htx_get_blk_type(blk);
732 struct ist n, v;
Willy Tarreau79e57332018-10-02 16:01:16 +0200733
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200734 if (type == HTX_BLK_HDR) {
735 n = htx_get_blk_name(htx,blk);
736 v = htx_get_blk_value(htx, blk);
Willy Tarreau79e57332018-10-02 16:01:16 +0200737
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200738 /* encode the header name. */
739 ret = encode_varint(n.len, &p, end);
740 if (ret == -1)
741 return 0;
742 if (p + n.len > end)
743 return 0;
744 memcpy(p, n.ptr, n.len);
745 p += n.len;
Willy Tarreau79e57332018-10-02 16:01:16 +0200746
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200747 /* encode the header value. */
748 ret = encode_varint(v.len, &p, end);
749 if (ret == -1)
750 return 0;
751 if (p + v.len > end)
752 return 0;
753 memcpy(p, v.ptr, v.len);
754 p += v.len;
Willy Tarreau79e57332018-10-02 16:01:16 +0200755
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200756 }
757 else if (type == HTX_BLK_EOH) {
758 /* encode the end of the header list with empty
759 * header name and header value.
760 */
761 ret = encode_varint(0, &p, end);
762 if (ret == -1)
763 return 0;
764 ret = encode_varint(0, &p, end);
765 if (ret == -1)
766 return 0;
767 break;
768 }
769 }
770
771 /* Initialise sample data which will be filled. */
772 smp->data.type = SMP_T_BIN;
773 smp->data.u.str.area = temp->area;
774 smp->data.u.str.data = p - temp->area;
775 smp->data.u.str.size = temp->size;
776 }
777 else {
778 /* LEGACY version */
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200779 struct hdr_idx *idx;
780 const char *cur_ptr, *cur_next, *p;
781 int old_idx, cur_idx;
782 struct hdr_idx_elem *cur_hdr;
783 const char *hn, *hv;
784 int hnl, hvl;
785 int ret;
786 char *buf;
787 char *end;
788
Christopher Faulet89dc4992019-04-17 12:02:59 +0200789 CHECK_HTTP_MESSAGE_FIRST(chn);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200790
791 temp = get_trash_chunk();
792 buf = temp->area;
793 end = temp->area + temp->size;
794
795 txn = smp->strm->txn;
796 idx = &txn->hdr_idx;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200797
798 /* Build array of headers. */
799 old_idx = 0;
Christopher Faulet89dc4992019-04-17 12:02:59 +0200800 cur_next = ci_head(chn) + hdr_idx_first_pos(idx);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200801 while (1) {
802 cur_idx = idx->v[old_idx].next;
803 if (!cur_idx)
804 break;
805 old_idx = cur_idx;
Willy Tarreau79e57332018-10-02 16:01:16 +0200806
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200807 cur_hdr = &idx->v[cur_idx];
808 cur_ptr = cur_next;
809 cur_next = cur_ptr + cur_hdr->len + cur_hdr->cr + 1;
810
811 /* Now we have one full header at cur_ptr of len cur_hdr->len,
812 * and the next header starts at cur_next. We'll check
813 * this header in the list as well as against the default
814 * rule.
815 */
816
817 /* look for ': *'. */
818 hn = cur_ptr;
819 for (p = cur_ptr; p < cur_ptr + cur_hdr->len && *p != ':'; p++);
820 if (p >= cur_ptr+cur_hdr->len)
821 continue;
822 hnl = p - hn;
Willy Tarreau79e57332018-10-02 16:01:16 +0200823 p++;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200824 while (p < cur_ptr + cur_hdr->len && (*p == ' ' || *p == '\t'))
825 p++;
826 if (p >= cur_ptr + cur_hdr->len)
827 continue;
828 hv = p;
829 hvl = cur_ptr + cur_hdr->len-p;
Willy Tarreau79e57332018-10-02 16:01:16 +0200830
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200831 /* encode the header name. */
832 ret = encode_varint(hnl, &buf, end);
833 if (ret == -1)
834 return 0;
835 if (buf + hnl > end)
836 return 0;
837 memcpy(buf, hn, hnl);
838 buf += hnl;
839
840 /* encode and copy the value. */
841 ret = encode_varint(hvl, &buf, end);
842 if (ret == -1)
843 return 0;
844 if (buf + hvl > end)
845 return 0;
846 memcpy(buf, hv, hvl);
847 buf += hvl;
848 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200849
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200850 /* encode the end of the header list with empty
851 * header name and header value.
852 */
853 ret = encode_varint(0, &buf, end);
Willy Tarreau79e57332018-10-02 16:01:16 +0200854 if (ret == -1)
855 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200856 ret = encode_varint(0, &buf, end);
857 if (ret == -1)
Willy Tarreau79e57332018-10-02 16:01:16 +0200858 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200859
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200860 /* Initialise sample data which will be filled. */
861 smp->data.type = SMP_T_BIN;
862 smp->data.u.str.area = temp->area;
863 smp->data.u.str.data = buf - temp->area;
864 smp->data.u.str.size = temp->size;
865 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200866 return 1;
867}
868
869/* returns the longest available part of the body. This requires that the body
870 * has been waited for using http-buffer-request.
871 */
872static int smp_fetch_body(const struct arg *args, struct sample *smp, const char *kw, void *private)
873{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200874 struct channel *chn = SMP_REQ_CHN(smp);
Willy Tarreau79e57332018-10-02 16:01:16 +0200875 struct buffer *temp;
876
Christopher Faulet46575cd2019-04-17 11:40:30 +0200877 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200878 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +0200879 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200880 int32_t pos;
Willy Tarreau79e57332018-10-02 16:01:16 +0200881
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200882 if (!htx)
883 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200884
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200885 temp = get_trash_chunk();
Christopher Fauleta3f15502019-05-13 15:27:23 +0200886 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200887 struct htx_blk *blk = htx_get_blk(htx, pos);
888 enum htx_blk_type type = htx_get_blk_type(blk);
Willy Tarreau79e57332018-10-02 16:01:16 +0200889
Christopher Faulet54b5e212019-06-04 10:08:28 +0200890 if (type == HTX_BLK_EOM || type == HTX_BLK_TLR || type == HTX_BLK_EOT)
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200891 break;
892 if (type == HTX_BLK_DATA) {
Christopher Fauletc59ff232018-12-03 13:58:44 +0100893 if (!htx_data_to_h1(htx_get_blk_value(htx, blk), temp, 0))
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200894 return 0;
895 }
896 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200897
Willy Tarreau79e57332018-10-02 16:01:16 +0200898 smp->data.type = SMP_T_BIN;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200899 smp->data.u.str = *temp;
900 smp->flags = SMP_F_VOL_TEST;
Willy Tarreau79e57332018-10-02 16:01:16 +0200901 }
902 else {
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200903 /* LEGACY version */
904 struct http_msg *msg;
905 unsigned long len;
906 unsigned long block1;
907 char *body;
908
Christopher Faulet89dc4992019-04-17 12:02:59 +0200909 CHECK_HTTP_MESSAGE_FIRST(chn);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200910
Christopher Faulet89dc4992019-04-17 12:02:59 +0200911 msg = &smp->strm->txn->req;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200912 len = http_body_bytes(msg);
Christopher Faulet89dc4992019-04-17 12:02:59 +0200913 body = c_ptr(chn, -http_data_rewind(msg));
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200914
915 block1 = len;
Christopher Faulet89dc4992019-04-17 12:02:59 +0200916 if (block1 > b_wrap(&chn->buf) - body)
917 block1 = b_wrap(&chn->buf) - body;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200918
919 if (block1 == len) {
920 /* buffer is not wrapped (or empty) */
921 smp->data.type = SMP_T_BIN;
922 smp->data.u.str.area = body;
923 smp->data.u.str.data = len;
924 smp->flags = SMP_F_VOL_TEST | SMP_F_CONST;
925 }
926 else {
927 /* buffer is wrapped, we need to defragment it */
928 temp = get_trash_chunk();
929 memcpy(temp->area, body, block1);
Christopher Faulet89dc4992019-04-17 12:02:59 +0200930 memcpy(temp->area + block1, b_orig(&chn->buf), len - block1);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200931 smp->data.type = SMP_T_BIN;
932 smp->data.u.str.area = temp->area;
933 smp->data.u.str.data = len;
934 smp->flags = SMP_F_VOL_TEST;
935 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200936 }
937 return 1;
938}
939
940
941/* returns the available length of the body. This requires that the body
942 * has been waited for using http-buffer-request.
943 */
944static int smp_fetch_body_len(const struct arg *args, struct sample *smp, const char *kw, void *private)
945{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200946 struct channel *chn = SMP_REQ_CHN(smp);
947
Christopher Faulet46575cd2019-04-17 11:40:30 +0200948 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200949 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +0200950 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Dragan Dosen5a606682019-02-14 12:30:53 +0100951 int32_t pos;
Christopher Fauletc16317d2018-12-12 14:11:22 +0100952 unsigned long long len = 0;
953
954 if (!htx)
955 return 0;
956
Christopher Fauleta3f15502019-05-13 15:27:23 +0200957 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Dragan Dosen5a606682019-02-14 12:30:53 +0100958 struct htx_blk *blk = htx_get_blk(htx, pos);
959 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Fauletc16317d2018-12-12 14:11:22 +0100960
Christopher Faulet54b5e212019-06-04 10:08:28 +0200961 if (type == HTX_BLK_EOM || type == HTX_BLK_TLR || type == HTX_BLK_EOT)
Christopher Fauletc16317d2018-12-12 14:11:22 +0100962 break;
Dragan Dosen5a606682019-02-14 12:30:53 +0100963 if (type == HTX_BLK_DATA)
964 len += htx_get_blksz(blk);
Christopher Fauletc16317d2018-12-12 14:11:22 +0100965 }
966
967 smp->data.type = SMP_T_SINT;
968 smp->data.u.sint = len;
969
970 smp->flags = SMP_F_VOL_TEST;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200971 }
972 else {
973 /* LEGACY version */
974 struct http_msg *msg;
Willy Tarreau79e57332018-10-02 16:01:16 +0200975
Christopher Faulet89dc4992019-04-17 12:02:59 +0200976 CHECK_HTTP_MESSAGE_FIRST(chn);
Willy Tarreau79e57332018-10-02 16:01:16 +0200977
Christopher Faulet89dc4992019-04-17 12:02:59 +0200978 msg = &smp->strm->txn->req;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200979 smp->data.type = SMP_T_SINT;
980 smp->data.u.sint = http_body_bytes(msg);
Willy Tarreau79e57332018-10-02 16:01:16 +0200981
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200982 smp->flags = SMP_F_VOL_TEST;
983 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200984 return 1;
985}
986
987
988/* returns the advertised length of the body, or the advertised size of the
989 * chunks available in the buffer. This requires that the body has been waited
990 * for using http-buffer-request.
991 */
992static int smp_fetch_body_size(const struct arg *args, struct sample *smp, const char *kw, void *private)
993{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200994 struct channel *chn = SMP_REQ_CHN(smp);
995
Christopher Faulet46575cd2019-04-17 11:40:30 +0200996 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200997 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +0200998 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Dragan Dosen5a606682019-02-14 12:30:53 +0100999 int32_t pos;
Christopher Fauletc16317d2018-12-12 14:11:22 +01001000 unsigned long long len = 0;
1001
1002 if (!htx)
1003 return 0;
1004
Christopher Fauleta3f15502019-05-13 15:27:23 +02001005 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Dragan Dosen5a606682019-02-14 12:30:53 +01001006 struct htx_blk *blk = htx_get_blk(htx, pos);
1007 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Fauletc16317d2018-12-12 14:11:22 +01001008
Christopher Faulet54b5e212019-06-04 10:08:28 +02001009 if (type == HTX_BLK_EOM || type == HTX_BLK_TLR || type == HTX_BLK_EOT)
Christopher Fauletc16317d2018-12-12 14:11:22 +01001010 break;
Dragan Dosen5a606682019-02-14 12:30:53 +01001011 if (type == HTX_BLK_DATA)
1012 len += htx_get_blksz(blk);
Christopher Fauletc16317d2018-12-12 14:11:22 +01001013 }
1014 if (htx->extra != ULLONG_MAX)
1015 len += htx->extra;
1016
1017 smp->data.type = SMP_T_SINT;
1018 smp->data.u.sint = len;
1019
1020 smp->flags = SMP_F_VOL_TEST;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001021 }
1022 else {
1023 /* LEGACY version */
1024 struct http_msg *msg;
Willy Tarreau79e57332018-10-02 16:01:16 +02001025
Christopher Faulet89dc4992019-04-17 12:02:59 +02001026 CHECK_HTTP_MESSAGE_FIRST(chn);
Willy Tarreau79e57332018-10-02 16:01:16 +02001027
Christopher Faulet89dc4992019-04-17 12:02:59 +02001028 msg = &smp->strm->txn->req;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001029 smp->data.type = SMP_T_SINT;
1030 smp->data.u.sint = msg->body_len;
Willy Tarreau79e57332018-10-02 16:01:16 +02001031
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001032 smp->flags = SMP_F_VOL_TEST;
1033 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001034 return 1;
1035}
1036
1037
1038/* 4. Check on URL/URI. A pointer to the URI is stored. */
1039static int smp_fetch_url(const struct arg *args, struct sample *smp, const char *kw, void *private)
1040{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001041 struct channel *chn = SMP_REQ_CHN(smp);
Willy Tarreau79e57332018-10-02 16:01:16 +02001042 struct http_txn *txn;
1043
Christopher Faulet46575cd2019-04-17 11:40:30 +02001044 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001045 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +02001046 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001047 struct htx_sl *sl;
Willy Tarreau79e57332018-10-02 16:01:16 +02001048
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001049 if (!htx)
1050 return 0;
Christopher Faulet297fbb42019-05-13 14:41:27 +02001051 sl = http_get_stline(htx);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001052 smp->data.type = SMP_T_STR;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001053 smp->data.u.str.area = HTX_SL_REQ_UPTR(sl);
1054 smp->data.u.str.data = HTX_SL_REQ_ULEN(sl);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001055 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
1056 }
1057 else {
1058 /* LEGACY version */
Christopher Faulet89dc4992019-04-17 12:02:59 +02001059 CHECK_HTTP_MESSAGE_FIRST(chn);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001060
1061 txn = smp->strm->txn;
1062 smp->data.type = SMP_T_STR;
1063 smp->data.u.str.data = txn->req.sl.rq.u_l;
Christopher Faulet89dc4992019-04-17 12:02:59 +02001064 smp->data.u.str.area = ci_head(chn) + txn->req.sl.rq.u;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001065 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
1066 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001067 return 1;
1068}
1069
1070static int smp_fetch_url_ip(const struct arg *args, struct sample *smp, const char *kw, void *private)
1071{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001072 struct channel *chn = SMP_REQ_CHN(smp);
Willy Tarreau79e57332018-10-02 16:01:16 +02001073 struct http_txn *txn;
1074 struct sockaddr_storage addr;
1075
Christopher Faulet46575cd2019-04-17 11:40:30 +02001076 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001077 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +02001078 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001079 struct htx_sl *sl;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001080
1081 if (!htx)
1082 return 0;
Christopher Faulet297fbb42019-05-13 14:41:27 +02001083 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001084 url2sa(HTX_SL_REQ_UPTR(sl), HTX_SL_REQ_ULEN(sl), &addr, NULL);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001085 }
1086 else {
1087 /* LEGACY version */
Christopher Faulet89dc4992019-04-17 12:02:59 +02001088 CHECK_HTTP_MESSAGE_FIRST(chn);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001089
1090 txn = smp->strm->txn;
Christopher Faulet89dc4992019-04-17 12:02:59 +02001091 url2sa(ci_head(chn) + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &addr, NULL);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001092 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001093
Willy Tarreau79e57332018-10-02 16:01:16 +02001094 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
1095 return 0;
1096
1097 smp->data.type = SMP_T_IPV4;
1098 smp->data.u.ipv4 = ((struct sockaddr_in *)&addr)->sin_addr;
1099 smp->flags = 0;
1100 return 1;
1101}
1102
1103static int smp_fetch_url_port(const struct arg *args, struct sample *smp, const char *kw, void *private)
1104{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001105 struct channel *chn = SMP_REQ_CHN(smp);
Willy Tarreau79e57332018-10-02 16:01:16 +02001106 struct http_txn *txn;
1107 struct sockaddr_storage addr;
1108
Christopher Faulet46575cd2019-04-17 11:40:30 +02001109 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001110 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +02001111 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001112 struct htx_sl *sl;
Willy Tarreau79e57332018-10-02 16:01:16 +02001113
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001114 if (!htx)
1115 return 0;
Christopher Faulet297fbb42019-05-13 14:41:27 +02001116 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001117 url2sa(HTX_SL_REQ_UPTR(sl), HTX_SL_REQ_ULEN(sl), &addr, NULL);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001118 }
1119 else {
1120 /* LEGACY version */
Christopher Faulet89dc4992019-04-17 12:02:59 +02001121 CHECK_HTTP_MESSAGE_FIRST(chn);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001122
1123 txn = smp->strm->txn;
Christopher Faulet89dc4992019-04-17 12:02:59 +02001124 url2sa(ci_head(chn) + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &addr, NULL);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001125 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001126 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
1127 return 0;
1128
1129 smp->data.type = SMP_T_SINT;
1130 smp->data.u.sint = ntohs(((struct sockaddr_in *)&addr)->sin_port);
1131 smp->flags = 0;
1132 return 1;
1133}
1134
1135/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
1136 * Accepts an optional argument of type string containing the header field name,
1137 * and an optional argument of type signed or unsigned integer to request an
1138 * explicit occurrence of the header. Note that in the event of a missing name,
1139 * headers are considered from the first one. It does not stop on commas and
1140 * returns full lines instead (useful for User-Agent or Date for example).
1141 */
1142static int smp_fetch_fhdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
1143{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001144 /* possible keywords: req.fhdr, res.fhdr */
1145 struct channel *chn = ((kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Willy Tarreau79e57332018-10-02 16:01:16 +02001146 int occ = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001147
Christopher Faulet46575cd2019-04-17 11:40:30 +02001148 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001149 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +02001150 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001151 struct http_hdr_ctx *ctx = smp->ctx.a[0];
1152 struct ist name;
Willy Tarreau79e57332018-10-02 16:01:16 +02001153
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001154 if (!ctx) {
1155 /* first call */
1156 ctx = &static_http_hdr_ctx;
1157 ctx->blk = NULL;
1158 smp->ctx.a[0] = ctx;
1159 }
1160
1161 if (args) {
1162 if (args[0].type != ARGT_STR)
1163 return 0;
1164 name.ptr = args[0].data.str.area;
1165 name.len = args[0].data.str.data;
1166
1167 if (args[1].type == ARGT_SINT)
1168 occ = args[1].data.sint;
1169 }
1170
1171 if (!htx)
Willy Tarreau79e57332018-10-02 16:01:16 +02001172 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001173
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001174 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
1175 /* search for header from the beginning */
1176 ctx->blk = NULL;
1177
1178 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
1179 /* no explicit occurrence and single fetch => last header by default */
1180 occ = -1;
1181
1182 if (!occ)
1183 /* prepare to report multiple occurrences for ACL fetches */
1184 smp->flags |= SMP_F_NOT_LAST;
1185
1186 smp->data.type = SMP_T_STR;
1187 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
1188 if (http_get_htx_fhdr(htx, name, occ, ctx, &smp->data.u.str.area, &smp->data.u.str.data))
1189 return 1;
Willy Tarreau79e57332018-10-02 16:01:16 +02001190 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001191 else {
1192 /* LEGACY version */
1193 struct hdr_idx *idx;
1194 struct hdr_ctx *ctx = smp->ctx.a[0];
1195 const struct http_msg *msg;
1196 const char *name_str = NULL;
1197 int name_len = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001198
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001199 if (!ctx) {
1200 /* first call */
1201 ctx = &static_hdr_ctx;
1202 ctx->idx = 0;
1203 smp->ctx.a[0] = ctx;
1204 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001205
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001206 if (args) {
1207 if (args[0].type != ARGT_STR)
1208 return 0;
1209 name_str = args[0].data.str.area;
1210 name_len = args[0].data.str.data;
Willy Tarreau79e57332018-10-02 16:01:16 +02001211
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001212 if (args[1].type == ARGT_SINT)
1213 occ = args[1].data.sint;
1214 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001215
Christopher Faulet89dc4992019-04-17 12:02:59 +02001216 CHECK_HTTP_MESSAGE_FIRST(chn);
Willy Tarreau79e57332018-10-02 16:01:16 +02001217
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001218 idx = &smp->strm->txn->hdr_idx;
Christopher Faulet89dc4992019-04-17 12:02:59 +02001219 msg = (!(chn->flags & CF_ISRESP) ? &smp->strm->txn->req : &smp->strm->txn->rsp);
Willy Tarreau79e57332018-10-02 16:01:16 +02001220
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001221 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
1222 /* search for header from the beginning */
1223 ctx->idx = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001224
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001225 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
1226 /* no explicit occurrence and single fetch => last header by default */
1227 occ = -1;
1228
1229 if (!occ)
1230 /* prepare to report multiple occurrences for ACL fetches */
1231 smp->flags |= SMP_F_NOT_LAST;
1232
1233 smp->data.type = SMP_T_STR;
1234 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
1235 if (http_get_fhdr(msg, name_str, name_len, idx, occ, ctx, &smp->data.u.str.area, &smp->data.u.str.data))
1236 return 1;
1237 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001238 smp->flags &= ~SMP_F_NOT_LAST;
1239 return 0;
1240}
1241
1242/* 6. Check on HTTP header count. The number of occurrences is returned.
1243 * Accepts exactly 1 argument of type string. It does not stop on commas and
1244 * returns full lines instead (useful for User-Agent or Date for example).
1245 */
1246static int smp_fetch_fhdr_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
1247{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001248 /* possible keywords: req.fhdr_cnt, res.fhdr_cnt */
1249 struct channel *chn = ((kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Willy Tarreau79e57332018-10-02 16:01:16 +02001250 int cnt;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001251
Christopher Faulet46575cd2019-04-17 11:40:30 +02001252 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001253 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +02001254 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001255 struct http_hdr_ctx ctx;
1256 struct ist name;
1257
1258 if (!htx)
1259 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001260
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001261 if (args && args->type == ARGT_STR) {
1262 name.ptr = args->data.str.area;
1263 name.len = args->data.str.data;
Olivier Houcharde2c78cd2018-11-21 13:49:48 +01001264 } else {
1265 name.ptr = NULL;
1266 name.len = 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001267 }
1268
1269 ctx.blk = NULL;
1270 cnt = 0;
1271 while (http_find_header(htx, name, &ctx, 1))
1272 cnt++;
Willy Tarreau79e57332018-10-02 16:01:16 +02001273 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001274 else {
1275 /* LEGACY version */
1276 struct hdr_idx *idx;
1277 struct hdr_ctx ctx;
1278 const struct http_msg *msg;
1279 const char *name = NULL;
1280 int len = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001281
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001282 if (args && args->type == ARGT_STR) {
1283 name = args->data.str.area;
1284 len = args->data.str.data;
1285 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001286
Christopher Faulet89dc4992019-04-17 12:02:59 +02001287 CHECK_HTTP_MESSAGE_FIRST(chn);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001288
1289 idx = &smp->strm->txn->hdr_idx;
Christopher Faulet89dc4992019-04-17 12:02:59 +02001290 msg = (!(chn->flags & CF_ISRESP) ? &smp->strm->txn->req : &smp->strm->txn->rsp);
Willy Tarreau79e57332018-10-02 16:01:16 +02001291
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001292 ctx.idx = 0;
1293 cnt = 0;
1294 while (http_find_full_header2(name, len, ci_head(msg->chn), idx, &ctx))
1295 cnt++;
1296 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001297
1298 smp->data.type = SMP_T_SINT;
1299 smp->data.u.sint = cnt;
1300 smp->flags = SMP_F_VOL_HDR;
1301 return 1;
1302}
1303
1304static int smp_fetch_hdr_names(const struct arg *args, struct sample *smp, const char *kw, void *private)
1305{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001306 /* possible keywords: req.hdr_names, res.hdr_names */
1307 struct channel *chn = ((kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Willy Tarreau79e57332018-10-02 16:01:16 +02001308 struct buffer *temp;
1309 char del = ',';
1310
Christopher Faulet46575cd2019-04-17 11:40:30 +02001311 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001312 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +02001313 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001314 int32_t pos;
Willy Tarreau79e57332018-10-02 16:01:16 +02001315
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001316 if (!htx)
1317 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001318
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001319 if (args && args->type == ARGT_STR)
1320 del = *args[0].data.str.area;
Willy Tarreau79e57332018-10-02 16:01:16 +02001321
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001322 temp = get_trash_chunk();
Christopher Fauleta3f15502019-05-13 15:27:23 +02001323 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001324 struct htx_blk *blk = htx_get_blk(htx, pos);
1325 enum htx_blk_type type = htx_get_blk_type(blk);
1326 struct ist n;
Willy Tarreau79e57332018-10-02 16:01:16 +02001327
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001328 if (type == HTX_BLK_EOH)
1329 break;
1330 if (type != HTX_BLK_HDR)
1331 continue;
1332 n = htx_get_blk_name(htx, blk);
1333
1334 if (temp->data)
1335 temp->area[temp->data++] = del;
1336 chunk_memcat(temp, n.ptr, n.len);
1337 }
1338 }
1339 else {
1340 /* LEGACY version */
1341 struct hdr_idx *idx;
1342 struct hdr_ctx ctx;
1343 const struct http_msg *msg;
1344
1345 if (args && args->type == ARGT_STR)
1346 del = *args[0].data.str.area;
1347
Christopher Faulet89dc4992019-04-17 12:02:59 +02001348 CHECK_HTTP_MESSAGE_FIRST(chn);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001349
1350 idx = &smp->strm->txn->hdr_idx;
Christopher Faulet89dc4992019-04-17 12:02:59 +02001351 msg = (!(chn->flags & CF_ISRESP) ? &smp->strm->txn->req : &smp->strm->txn->rsp);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001352
1353 temp = get_trash_chunk();
1354
1355 ctx.idx = 0;
1356 while (http_find_next_header(ci_head(msg->chn), idx, &ctx)) {
1357 if (temp->data)
1358 temp->area[temp->data++] = del;
1359 memcpy(temp->area + temp->data, ctx.line, ctx.del);
1360 temp->data += ctx.del;
1361 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001362 }
1363
1364 smp->data.type = SMP_T_STR;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001365 smp->data.u.str = *temp;
Willy Tarreau79e57332018-10-02 16:01:16 +02001366 smp->flags = SMP_F_VOL_HDR;
1367 return 1;
1368}
1369
1370/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
1371 * Accepts an optional argument of type string containing the header field name,
1372 * and an optional argument of type signed or unsigned integer to request an
1373 * explicit occurrence of the header. Note that in the event of a missing name,
1374 * headers are considered from the first one.
1375 */
1376static int smp_fetch_hdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
1377{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001378 /* possible keywords: req.hdr / hdr, res.hdr / shdr */
1379 struct channel *chn = ((kw[0] == 'h' || kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Willy Tarreau79e57332018-10-02 16:01:16 +02001380 int occ = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001381
Christopher Faulet46575cd2019-04-17 11:40:30 +02001382 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001383 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +02001384 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001385 struct http_hdr_ctx *ctx = smp->ctx.a[0];
1386 struct ist name;
1387
1388 if (!ctx) {
1389 /* first call */
1390 ctx = &static_http_hdr_ctx;
1391 ctx->blk = NULL;
1392 smp->ctx.a[0] = ctx;
1393 }
1394
1395 if (args) {
1396 if (args[0].type != ARGT_STR)
1397 return 0;
1398 name.ptr = args[0].data.str.area;
1399 name.len = args[0].data.str.data;
1400
1401 if (args[1].type == ARGT_SINT)
1402 occ = args[1].data.sint;
1403 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001404
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001405 if (!htx)
Willy Tarreau79e57332018-10-02 16:01:16 +02001406 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001407
1408 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
1409 /* search for header from the beginning */
1410 ctx->blk = NULL;
1411
1412 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
1413 /* no explicit occurrence and single fetch => last header by default */
1414 occ = -1;
1415
1416 if (!occ)
1417 /* prepare to report multiple occurrences for ACL fetches */
1418 smp->flags |= SMP_F_NOT_LAST;
Willy Tarreau79e57332018-10-02 16:01:16 +02001419
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001420 smp->data.type = SMP_T_STR;
1421 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
1422 if (http_get_htx_hdr(htx, name, occ, ctx, &smp->data.u.str.area, &smp->data.u.str.data))
1423 return 1;
Willy Tarreau79e57332018-10-02 16:01:16 +02001424 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001425 else {
1426 /* LEGACY version */
1427 struct hdr_idx *idx;
1428 struct hdr_ctx *ctx = smp->ctx.a[0];
1429 const struct http_msg *msg;
1430 const char *name_str = NULL;
1431 int name_len = 0;
1432
1433 if (!ctx) {
1434 /* first call */
1435 ctx = &static_hdr_ctx;
1436 ctx->idx = 0;
1437 smp->ctx.a[0] = ctx;
1438 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001439
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001440 if (args) {
1441 if (args[0].type != ARGT_STR)
1442 return 0;
1443 name_str = args[0].data.str.area;
1444 name_len = args[0].data.str.data;
Willy Tarreau79e57332018-10-02 16:01:16 +02001445
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001446 if (args[1].type == ARGT_SINT)
1447 occ = args[1].data.sint;
1448 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001449
Christopher Faulet89dc4992019-04-17 12:02:59 +02001450 CHECK_HTTP_MESSAGE_FIRST(chn);
Willy Tarreau79e57332018-10-02 16:01:16 +02001451
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001452 idx = &smp->strm->txn->hdr_idx;
Christopher Faulet89dc4992019-04-17 12:02:59 +02001453 msg = (!(chn->flags & CF_ISRESP) ? &smp->strm->txn->req : &smp->strm->txn->rsp);
Willy Tarreau79e57332018-10-02 16:01:16 +02001454
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001455 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
1456 /* search for header from the beginning */
1457 ctx->idx = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001458
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001459 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
1460 /* no explicit occurrence and single fetch => last header by default */
1461 occ = -1;
1462
1463 if (!occ)
1464 /* prepare to report multiple occurrences for ACL fetches */
1465 smp->flags |= SMP_F_NOT_LAST;
1466
1467 smp->data.type = SMP_T_STR;
1468 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
1469 if (http_get_hdr(msg, name_str, name_len, idx, occ, ctx, &smp->data.u.str.area, &smp->data.u.str.data))
1470 return 1;
1471 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001472
1473 smp->flags &= ~SMP_F_NOT_LAST;
1474 return 0;
1475}
1476
Christopher Fauletc1f40dd2019-05-16 10:07:30 +02001477/* Same than smp_fetch_hdr() but only relies on the sample direction to choose
1478 * the right channel. So instead of duplicating the code, we just change the
1479 * keyword and then fallback on smp_fetch_hdr().
1480 */
1481static int smp_fetch_chn_hdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
1482{
1483 kw = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ ? "req.hdr" : "res.hdr");
1484 return smp_fetch_hdr(args, smp, kw, private);
1485}
1486
Willy Tarreau79e57332018-10-02 16:01:16 +02001487/* 6. Check on HTTP header count. The number of occurrences is returned.
1488 * Accepts exactly 1 argument of type string.
1489 */
1490static int smp_fetch_hdr_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
1491{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001492 /* possible keywords: req.hdr_cnt / hdr_cnt, res.hdr_cnt / shdr_cnt */
1493 struct channel *chn = ((kw[0] == 'h' || kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Willy Tarreau79e57332018-10-02 16:01:16 +02001494 int cnt;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001495
Christopher Faulet46575cd2019-04-17 11:40:30 +02001496 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001497 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +02001498 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001499 struct http_hdr_ctx ctx;
1500 struct ist name;
1501
1502 if (!htx)
1503 return 0;
1504
1505 if (args && args->type == ARGT_STR) {
1506 name.ptr = args->data.str.area;
1507 name.len = args->data.str.data;
Olivier Houcharde2c78cd2018-11-21 13:49:48 +01001508 } else {
1509 name.ptr = NULL;
1510 name.len = 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001511 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001512
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001513 ctx.blk = NULL;
1514 cnt = 0;
1515 while (http_find_header(htx, name, &ctx, 0))
1516 cnt++;
Willy Tarreau79e57332018-10-02 16:01:16 +02001517 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001518 else {
1519 /* LEGACY version */
1520 struct hdr_idx *idx;
1521 struct hdr_ctx ctx;
1522 const struct http_msg *msg;
1523 const char *name = NULL;
1524 int len = 0;
1525
1526 if (args && args->type == ARGT_STR) {
1527 name = args->data.str.area;
1528 len = args->data.str.data;
1529 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001530
Christopher Faulet89dc4992019-04-17 12:02:59 +02001531 CHECK_HTTP_MESSAGE_FIRST(chn);
Willy Tarreau79e57332018-10-02 16:01:16 +02001532
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001533 idx = &smp->strm->txn->hdr_idx;
Christopher Faulet89dc4992019-04-17 12:02:59 +02001534 msg = (!(chn->flags & CF_ISRESP) ? &smp->strm->txn->req : &smp->strm->txn->rsp);
Willy Tarreau79e57332018-10-02 16:01:16 +02001535
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001536 ctx.idx = 0;
1537 cnt = 0;
1538 while (http_find_header2(name, len, ci_head(msg->chn), idx, &ctx))
1539 cnt++;
1540 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001541
1542 smp->data.type = SMP_T_SINT;
1543 smp->data.u.sint = cnt;
1544 smp->flags = SMP_F_VOL_HDR;
1545 return 1;
1546}
1547
1548/* Fetch an HTTP header's integer value. The integer value is returned. It
1549 * takes a mandatory argument of type string and an optional one of type int
1550 * to designate a specific occurrence. It returns an unsigned integer, which
1551 * may or may not be appropriate for everything.
1552 */
1553static int smp_fetch_hdr_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
1554{
1555 int ret = smp_fetch_hdr(args, smp, kw, private);
1556
1557 if (ret > 0) {
1558 smp->data.type = SMP_T_SINT;
1559 smp->data.u.sint = strl2ic(smp->data.u.str.area,
1560 smp->data.u.str.data);
1561 }
1562
1563 return ret;
1564}
1565
1566/* Fetch an HTTP header's IP value. takes a mandatory argument of type string
1567 * and an optional one of type int to designate a specific occurrence.
Willy Tarreaud08feaf2021-03-25 14:12:29 +01001568 * It returns an IPv4 or IPv6 address. Addresses surrounded by invalid chars
1569 * are rejected. However IPv4 addresses may be followed with a colon and a
1570 * valid port number.
Willy Tarreau79e57332018-10-02 16:01:16 +02001571 */
1572static int smp_fetch_hdr_ip(const struct arg *args, struct sample *smp, const char *kw, void *private)
1573{
Tim Duesterhus8ba978b2020-06-26 15:44:48 +02001574 struct buffer *temp = get_trash_chunk();
Willy Tarreaud08feaf2021-03-25 14:12:29 +01001575 int ret, len;
1576 int port;
Willy Tarreau79e57332018-10-02 16:01:16 +02001577
1578 while ((ret = smp_fetch_hdr(args, smp, kw, private)) > 0) {
Tim Duesterhus8ba978b2020-06-26 15:44:48 +02001579 if (smp->data.u.str.data < temp->size - 1) {
1580 memcpy(temp->area, smp->data.u.str.area,
1581 smp->data.u.str.data);
1582 temp->area[smp->data.u.str.data] = '\0';
Willy Tarreaud08feaf2021-03-25 14:12:29 +01001583 len = url2ipv4((char *) temp->area, &smp->data.u.ipv4);
1584 if (len == smp->data.u.str.data) {
1585 /* plain IPv4 address */
1586 smp->data.type = SMP_T_IPV4;
1587 break;
1588 } else if (len > 0 && temp->area[len] == ':' &&
1589 strl2irc(temp->area + len + 1, smp->data.u.str.data - len - 1, &port) == 0 &&
1590 port >= 0 && port <= 65535) {
1591 /* IPv4 address suffixed with ':' followed by a valid port number */
Tim Duesterhus8ba978b2020-06-26 15:44:48 +02001592 smp->data.type = SMP_T_IPV4;
1593 break;
1594 } else if (inet_pton(AF_INET6, temp->area, &smp->data.u.ipv6)) {
1595 smp->data.type = SMP_T_IPV6;
1596 break;
Willy Tarreau79e57332018-10-02 16:01:16 +02001597 }
1598 }
1599
1600 /* if the header doesn't match an IP address, fetch next one */
1601 if (!(smp->flags & SMP_F_NOT_LAST))
1602 return 0;
1603 }
1604 return ret;
1605}
1606
1607/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
1608 * the first '/' after the possible hostname, and ends before the possible '?'.
1609 */
1610static int smp_fetch_path(const struct arg *args, struct sample *smp, const char *kw, void *private)
1611{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001612 struct channel *chn = SMP_REQ_CHN(smp);
1613
Christopher Faulet46575cd2019-04-17 11:40:30 +02001614 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001615 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +02001616 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001617 struct htx_sl *sl;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001618 struct ist path;
1619 size_t len;
Willy Tarreau79e57332018-10-02 16:01:16 +02001620
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001621 if (!htx)
1622 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001623
Christopher Faulet297fbb42019-05-13 14:41:27 +02001624 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001625 path = http_get_path(htx_sl_req_uri(sl));
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001626 if (!path.ptr)
1627 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001628
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001629 for (len = 0; len < path.len && *(path.ptr + len) != '?'; len++)
Dragan Dosen8861e1c2019-02-12 19:50:31 +01001630 ;
Willy Tarreau79e57332018-10-02 16:01:16 +02001631
Dragan Dosen8861e1c2019-02-12 19:50:31 +01001632 /* OK, we got the '/' ! */
1633 smp->data.type = SMP_T_STR;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001634 smp->data.u.str.area = path.ptr;
1635 smp->data.u.str.data = len;
1636 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
1637 }
1638 else {
1639 struct http_txn *txn;
1640 char *ptr, *end;
Willy Tarreau79e57332018-10-02 16:01:16 +02001641
Christopher Faulet89dc4992019-04-17 12:02:59 +02001642 CHECK_HTTP_MESSAGE_FIRST(chn);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001643
1644 txn = smp->strm->txn;
Christopher Faulet89dc4992019-04-17 12:02:59 +02001645 end = ci_head(chn) + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001646 ptr = http_txn_get_path(txn);
1647 if (!ptr)
1648 return 0;
1649
1650 /* OK, we got the '/' ! */
1651 smp->data.type = SMP_T_STR;
1652 smp->data.u.str.area = ptr;
1653
1654 while (ptr < end && *ptr != '?')
1655 ptr++;
1656
1657 smp->data.u.str.data = ptr - smp->data.u.str.area;
1658 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
1659 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001660 return 1;
1661}
1662
1663/* This produces a concatenation of the first occurrence of the Host header
1664 * followed by the path component if it begins with a slash ('/'). This means
1665 * that '*' will not be added, resulting in exactly the first Host entry.
1666 * If no Host header is found, then the path is returned as-is. The returned
1667 * value is stored in the trash so it does not need to be marked constant.
1668 * The returned sample is of type string.
1669 */
1670static int smp_fetch_base(const struct arg *args, struct sample *smp, const char *kw, void *private)
1671{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001672 struct channel *chn = SMP_REQ_CHN(smp);
Willy Tarreau79e57332018-10-02 16:01:16 +02001673 struct buffer *temp;
1674
Christopher Faulet46575cd2019-04-17 11:40:30 +02001675 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001676 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +02001677 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001678 struct htx_sl *sl;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001679 struct http_hdr_ctx ctx;
1680 struct ist path;
Willy Tarreau79e57332018-10-02 16:01:16 +02001681
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001682 if (!htx)
1683 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001684
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001685 ctx.blk = NULL;
1686 if (!http_find_header(htx, ist("Host"), &ctx, 0) || !ctx.value.len)
1687 return smp_fetch_path(args, smp, kw, private);
Willy Tarreau79e57332018-10-02 16:01:16 +02001688
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001689 /* OK we have the header value in ctx.value */
1690 temp = get_trash_chunk();
1691 chunk_memcat(temp, ctx.value.ptr, ctx.value.len);
1692
1693 /* now retrieve the path */
Christopher Faulet297fbb42019-05-13 14:41:27 +02001694 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001695 path = http_get_path(htx_sl_req_uri(sl));
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001696 if (path.ptr) {
1697 size_t len;
1698
Dragan Dosen8861e1c2019-02-12 19:50:31 +01001699 for (len = 0; len < path.len && *(path.ptr + len) != '?'; len++)
1700 ;
1701
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001702 if (len && *(path.ptr) == '/')
1703 chunk_memcat(temp, path.ptr, len);
1704 }
1705
1706 smp->data.type = SMP_T_STR;
1707 smp->data.u.str = *temp;
1708 }
1709 else {
1710 /* LEGACY version */
1711 struct http_txn *txn;
1712 char *ptr, *end, *beg;
1713 struct hdr_ctx ctx;
1714
Christopher Faulet89dc4992019-04-17 12:02:59 +02001715 CHECK_HTTP_MESSAGE_FIRST(chn);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001716
1717 txn = smp->strm->txn;
1718 ctx.idx = 0;
Christopher Faulet89dc4992019-04-17 12:02:59 +02001719 if (!http_find_header2("Host", 4, ci_head(chn), &txn->hdr_idx, &ctx) || !ctx.vlen)
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001720 return smp_fetch_path(args, smp, kw, private);
1721
1722 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
1723 temp = get_trash_chunk();
1724 memcpy(temp->area, ctx.line + ctx.val, ctx.vlen);
1725 smp->data.type = SMP_T_STR;
1726 smp->data.u.str.area = temp->area;
1727 smp->data.u.str.data = ctx.vlen;
Willy Tarreau79e57332018-10-02 16:01:16 +02001728
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001729 /* now retrieve the path */
Christopher Faulet89dc4992019-04-17 12:02:59 +02001730 end = ci_head(chn) + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001731 beg = http_txn_get_path(txn);
1732 if (!beg)
1733 beg = end;
Willy Tarreau79e57332018-10-02 16:01:16 +02001734
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001735 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
1736
1737 if (beg < ptr && *beg == '/') {
1738 memcpy(smp->data.u.str.area + smp->data.u.str.data, beg,
1739 ptr - beg);
1740 smp->data.u.str.data += ptr - beg;
1741 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001742 }
1743
1744 smp->flags = SMP_F_VOL_1ST;
1745 return 1;
1746}
1747
1748/* This produces a 32-bit hash of the concatenation of the first occurrence of
1749 * the Host header followed by the path component if it begins with a slash ('/').
1750 * This means that '*' will not be added, resulting in exactly the first Host
1751 * entry. If no Host header is found, then the path is used. The resulting value
1752 * is hashed using the path hash followed by a full avalanche hash and provides a
1753 * 32-bit integer value. This fetch is useful for tracking per-path activity on
1754 * high-traffic sites without having to store whole paths.
1755 */
1756static int smp_fetch_base32(const struct arg *args, struct sample *smp, const char *kw, void *private)
1757{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001758 struct channel *chn = SMP_REQ_CHN(smp);
Willy Tarreau79e57332018-10-02 16:01:16 +02001759 unsigned int hash = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001760
Christopher Faulet46575cd2019-04-17 11:40:30 +02001761 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001762 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +02001763 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001764 struct htx_sl *sl;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001765 struct http_hdr_ctx ctx;
1766 struct ist path;
Willy Tarreau79e57332018-10-02 16:01:16 +02001767
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001768 if (!htx)
1769 return 0;
1770
1771 ctx.blk = NULL;
Dragan Dosen8861e1c2019-02-12 19:50:31 +01001772 if (http_find_header(htx, ist("Host"), &ctx, 0)) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001773 /* OK we have the header value in ctx.value */
1774 while (ctx.value.len--)
1775 hash = *(ctx.value.ptr++) + (hash << 6) + (hash << 16) - hash;
1776 }
1777
1778 /* now retrieve the path */
Christopher Faulet297fbb42019-05-13 14:41:27 +02001779 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001780 path = http_get_path(htx_sl_req_uri(sl));
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001781 if (path.ptr) {
1782 size_t len;
1783
Dragan Dosen8861e1c2019-02-12 19:50:31 +01001784 for (len = 0; len < path.len && *(path.ptr + len) != '?'; len++)
1785 ;
1786
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001787 if (len && *(path.ptr) == '/') {
1788 while (len--)
1789 hash = *(path.ptr++) + (hash << 6) + (hash << 16) - hash;
1790 }
1791 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001792 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001793 else {
1794 /* LEGACY version */
1795 struct http_txn *txn;
1796 struct hdr_ctx ctx;
1797 char *ptr, *beg, *end;
1798 int len;
1799
Christopher Faulet89dc4992019-04-17 12:02:59 +02001800 CHECK_HTTP_MESSAGE_FIRST(chn);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001801
1802 txn = smp->strm->txn;
1803 ctx.idx = 0;
Christopher Faulet89dc4992019-04-17 12:02:59 +02001804 if (http_find_header2("Host", 4, ci_head(chn), &txn->hdr_idx, &ctx)) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001805 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
1806 ptr = ctx.line + ctx.val;
1807 len = ctx.vlen;
1808 while (len--)
1809 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
1810 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001811
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001812 /* now retrieve the path */
Christopher Faulet89dc4992019-04-17 12:02:59 +02001813 end = ci_head(chn) + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001814 beg = http_txn_get_path(txn);
1815 if (!beg)
1816 beg = end;
Willy Tarreau79e57332018-10-02 16:01:16 +02001817
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001818 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
Willy Tarreau79e57332018-10-02 16:01:16 +02001819
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001820 if (beg < ptr && *beg == '/') {
1821 while (beg < ptr)
1822 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
1823 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001824 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001825
Willy Tarreau79e57332018-10-02 16:01:16 +02001826 hash = full_hash(hash);
1827
1828 smp->data.type = SMP_T_SINT;
1829 smp->data.u.sint = hash;
1830 smp->flags = SMP_F_VOL_1ST;
1831 return 1;
1832}
1833
1834/* This concatenates the source address with the 32-bit hash of the Host and
1835 * path as returned by smp_fetch_base32(). The idea is to have per-source and
1836 * per-path counters. The result is a binary block from 8 to 20 bytes depending
1837 * on the source address length. The path hash is stored before the address so
1838 * that in environments where IPv6 is insignificant, truncating the output to
1839 * 8 bytes would still work.
1840 */
1841static int smp_fetch_base32_src(const struct arg *args, struct sample *smp, const char *kw, void *private)
1842{
1843 struct buffer *temp;
1844 struct connection *cli_conn = objt_conn(smp->sess->origin);
1845
1846 if (!cli_conn)
1847 return 0;
1848
1849 if (!smp_fetch_base32(args, smp, kw, private))
1850 return 0;
1851
1852 temp = get_trash_chunk();
1853 *(unsigned int *) temp->area = htonl(smp->data.u.sint);
1854 temp->data += sizeof(unsigned int);
1855
1856 switch (cli_conn->addr.from.ss_family) {
1857 case AF_INET:
1858 memcpy(temp->area + temp->data,
1859 &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr,
1860 4);
1861 temp->data += 4;
1862 break;
1863 case AF_INET6:
1864 memcpy(temp->area + temp->data,
1865 &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr,
1866 16);
1867 temp->data += 16;
1868 break;
1869 default:
1870 return 0;
1871 }
1872
1873 smp->data.u.str = *temp;
1874 smp->data.type = SMP_T_BIN;
1875 return 1;
1876}
1877
1878/* Extracts the query string, which comes after the question mark '?'. If no
1879 * question mark is found, nothing is returned. Otherwise it returns a sample
1880 * of type string carrying the whole query string.
1881 */
1882static int smp_fetch_query(const struct arg *args, struct sample *smp, const char *kw, void *private)
1883{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001884 struct channel *chn = SMP_REQ_CHN(smp);
Willy Tarreau79e57332018-10-02 16:01:16 +02001885 char *ptr, *end;
1886
Christopher Faulet46575cd2019-04-17 11:40:30 +02001887 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001888 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +02001889 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001890 struct htx_sl *sl;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001891
1892 if (!htx)
1893 return 0;
1894
Christopher Faulet297fbb42019-05-13 14:41:27 +02001895 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001896 ptr = HTX_SL_REQ_UPTR(sl);
1897 end = HTX_SL_REQ_UPTR(sl) + HTX_SL_REQ_ULEN(sl);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001898 }
1899 else {
1900 /* LEGACY version */
1901 struct http_txn *txn;
1902
Christopher Faulet89dc4992019-04-17 12:02:59 +02001903 CHECK_HTTP_MESSAGE_FIRST(chn);
Willy Tarreau79e57332018-10-02 16:01:16 +02001904
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001905 txn = smp->strm->txn;
Christopher Faulet89dc4992019-04-17 12:02:59 +02001906 ptr = ci_head(chn) + txn->req.sl.rq.u;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001907 end = ptr + txn->req.sl.rq.u_l;
1908 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001909
1910 /* look up the '?' */
1911 do {
1912 if (ptr == end)
1913 return 0;
1914 } while (*ptr++ != '?');
1915
1916 smp->data.type = SMP_T_STR;
1917 smp->data.u.str.area = ptr;
1918 smp->data.u.str.data = end - ptr;
1919 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
1920 return 1;
1921}
1922
1923static int smp_fetch_proto_http(const struct arg *args, struct sample *smp, const char *kw, void *private)
1924{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001925 struct channel *chn = SMP_REQ_CHN(smp);
1926
Christopher Faulet46575cd2019-04-17 11:40:30 +02001927 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001928 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +02001929 struct htx *htx = smp_prefetch_htx(smp, chn, 0);
Willy Tarreau79e57332018-10-02 16:01:16 +02001930
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001931 if (!htx)
1932 return 0;
1933 }
1934 else {
1935 /* LEGACY version */
Willy Tarreau79e57332018-10-02 16:01:16 +02001936
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001937 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
1938 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
1939 */
Christopher Faulet89dc4992019-04-17 12:02:59 +02001940 CHECK_HTTP_MESSAGE_FIRST_PERM(chn);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001941 }
1942 smp->data.type = SMP_T_BOOL;
Willy Tarreau79e57332018-10-02 16:01:16 +02001943 smp->data.u.sint = 1;
1944 return 1;
1945}
1946
1947/* return a valid test if the current request is the first one on the connection */
1948static int smp_fetch_http_first_req(const struct arg *args, struct sample *smp, const char *kw, void *private)
1949{
Willy Tarreaud095c672020-04-29 11:52:13 +02001950 if (!smp->strm)
1951 return 0;
1952
Willy Tarreau79e57332018-10-02 16:01:16 +02001953 smp->data.type = SMP_T_BOOL;
1954 smp->data.u.sint = !(smp->strm->txn->flags & TX_NOT_FIRST);
1955 return 1;
1956}
1957
1958/* Accepts exactly 1 argument of type userlist */
1959static int smp_fetch_http_auth(const struct arg *args, struct sample *smp, const char *kw, void *private)
1960{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001961 struct channel *chn = SMP_REQ_CHN(smp);
Willy Tarreau79e57332018-10-02 16:01:16 +02001962
1963 if (!args || args->type != ARGT_USR)
1964 return 0;
1965
Christopher Faulet46575cd2019-04-17 11:40:30 +02001966 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001967 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +02001968 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Willy Tarreau79e57332018-10-02 16:01:16 +02001969
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001970 if (!htx)
1971 return 0;
Christopher Faulete98411b2019-07-15 13:58:29 +02001972 if (!get_http_auth(smp, htx))
1973 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001974 }
1975 else {
1976 /* LEGACY version */
Christopher Faulet89dc4992019-04-17 12:02:59 +02001977 CHECK_HTTP_MESSAGE_FIRST(chn);
Christopher Faulete98411b2019-07-15 13:58:29 +02001978 if (!get_http_auth(smp, NULL))
1979 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001980 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001981
1982 smp->data.type = SMP_T_BOOL;
1983 smp->data.u.sint = check_user(args->data.usr, smp->strm->txn->auth.user,
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001984 smp->strm->txn->auth.pass);
Willy Tarreau79e57332018-10-02 16:01:16 +02001985 return 1;
1986}
1987
1988/* Accepts exactly 1 argument of type userlist */
1989static int smp_fetch_http_auth_grp(const struct arg *args, struct sample *smp, const char *kw, void *private)
1990{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001991 struct channel *chn = SMP_REQ_CHN(smp);
1992
Willy Tarreau79e57332018-10-02 16:01:16 +02001993 if (!args || args->type != ARGT_USR)
1994 return 0;
1995
Christopher Faulet46575cd2019-04-17 11:40:30 +02001996 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001997 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +02001998 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001999
2000 if (!htx)
2001 return 0;
Christopher Faulete98411b2019-07-15 13:58:29 +02002002 if (!get_http_auth(smp, htx))
2003 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002004 }
2005 else {
2006 /* LEGACY version */
Christopher Faulet89dc4992019-04-17 12:02:59 +02002007 CHECK_HTTP_MESSAGE_FIRST(chn);
Christopher Faulete98411b2019-07-15 13:58:29 +02002008 if (!get_http_auth(smp, NULL))
2009 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002010 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002011
Willy Tarreau79e57332018-10-02 16:01:16 +02002012 /* if the user does not belong to the userlist or has a wrong password,
2013 * report that it unconditionally does not match. Otherwise we return
2014 * a string containing the username.
2015 */
2016 if (!check_user(args->data.usr, smp->strm->txn->auth.user,
2017 smp->strm->txn->auth.pass))
2018 return 0;
2019
2020 /* pat_match_auth() will need the user list */
2021 smp->ctx.a[0] = args->data.usr;
2022
2023 smp->data.type = SMP_T_STR;
2024 smp->flags = SMP_F_CONST;
2025 smp->data.u.str.area = smp->strm->txn->auth.user;
2026 smp->data.u.str.data = strlen(smp->strm->txn->auth.user);
2027
2028 return 1;
2029}
2030
2031/* Fetch a captured HTTP request header. The index is the position of
2032 * the "capture" option in the configuration file
2033 */
2034static int smp_fetch_capture_req_hdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
2035{
Willy Tarreau79ed4672020-04-29 11:44:54 +02002036 struct proxy *fe;
Willy Tarreau79e57332018-10-02 16:01:16 +02002037 int idx;
2038
2039 if (!args || args->type != ARGT_SINT)
2040 return 0;
2041
Willy Tarreau79ed4672020-04-29 11:44:54 +02002042 if (!smp->strm)
2043 return 0;
2044
2045 fe = strm_fe(smp->strm);
Willy Tarreau79e57332018-10-02 16:01:16 +02002046 idx = args->data.sint;
2047
2048 if (idx > (fe->nb_req_cap - 1) || smp->strm->req_cap == NULL || smp->strm->req_cap[idx] == NULL)
2049 return 0;
2050
2051 smp->data.type = SMP_T_STR;
2052 smp->flags |= SMP_F_CONST;
2053 smp->data.u.str.area = smp->strm->req_cap[idx];
2054 smp->data.u.str.data = strlen(smp->strm->req_cap[idx]);
2055
2056 return 1;
2057}
2058
2059/* Fetch a captured HTTP response header. The index is the position of
2060 * the "capture" option in the configuration file
2061 */
2062static int smp_fetch_capture_res_hdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
2063{
Willy Tarreau79ed4672020-04-29 11:44:54 +02002064 struct proxy *fe;
Willy Tarreau79e57332018-10-02 16:01:16 +02002065 int idx;
2066
2067 if (!args || args->type != ARGT_SINT)
2068 return 0;
2069
Willy Tarreau79ed4672020-04-29 11:44:54 +02002070 if (!smp->strm)
2071 return 0;
2072
2073 fe = strm_fe(smp->strm);
Willy Tarreau79e57332018-10-02 16:01:16 +02002074 idx = args->data.sint;
2075
2076 if (idx > (fe->nb_rsp_cap - 1) || smp->strm->res_cap == NULL || smp->strm->res_cap[idx] == NULL)
2077 return 0;
2078
2079 smp->data.type = SMP_T_STR;
2080 smp->flags |= SMP_F_CONST;
2081 smp->data.u.str.area = smp->strm->res_cap[idx];
2082 smp->data.u.str.data = strlen(smp->strm->res_cap[idx]);
2083
2084 return 1;
2085}
2086
2087/* Extracts the METHOD in the HTTP request, the txn->uri should be filled before the call */
2088static int smp_fetch_capture_req_method(const struct arg *args, struct sample *smp, const char *kw, void *private)
2089{
2090 struct buffer *temp;
Willy Tarreau79ed4672020-04-29 11:44:54 +02002091 struct http_txn *txn;
Willy Tarreau79e57332018-10-02 16:01:16 +02002092 char *ptr;
2093
Willy Tarreau79ed4672020-04-29 11:44:54 +02002094 if (!smp->strm)
2095 return 0;
2096
2097 txn = smp->strm->txn;
Willy Tarreau79e57332018-10-02 16:01:16 +02002098 if (!txn || !txn->uri)
2099 return 0;
2100
2101 ptr = txn->uri;
2102
2103 while (*ptr != ' ' && *ptr != '\0') /* find first space */
2104 ptr++;
2105
2106 temp = get_trash_chunk();
2107 temp->area = txn->uri;
2108 temp->data = ptr - txn->uri;
2109 smp->data.u.str = *temp;
2110 smp->data.type = SMP_T_STR;
2111 smp->flags = SMP_F_CONST;
2112
2113 return 1;
2114
2115}
2116
2117/* Extracts the path in the HTTP request, the txn->uri should be filled before the call */
2118static int smp_fetch_capture_req_uri(const struct arg *args, struct sample *smp, const char *kw, void *private)
2119{
Willy Tarreau79ed4672020-04-29 11:44:54 +02002120 struct http_txn *txn;
Willy Tarreau79e57332018-10-02 16:01:16 +02002121 struct ist path;
2122 const char *ptr;
2123
Willy Tarreau79ed4672020-04-29 11:44:54 +02002124 if (!smp->strm)
2125 return 0;
2126
2127 txn = smp->strm->txn;
Willy Tarreau79e57332018-10-02 16:01:16 +02002128 if (!txn || !txn->uri)
2129 return 0;
2130
2131 ptr = txn->uri;
2132
2133 while (*ptr != ' ' && *ptr != '\0') /* find first space */
2134 ptr++;
2135
2136 if (!*ptr)
2137 return 0;
2138
Christopher Faulet78337bb2018-11-15 14:35:18 +01002139 /* skip the first space and find space after URI */
2140 path = ist2(++ptr, 0);
2141 while (*ptr != ' ' && *ptr != '\0')
2142 ptr++;
2143 path.len = ptr - path.ptr;
Willy Tarreau79e57332018-10-02 16:01:16 +02002144
Christopher Faulet78337bb2018-11-15 14:35:18 +01002145 path = http_get_path(path);
Willy Tarreau79e57332018-10-02 16:01:16 +02002146 if (!path.ptr)
2147 return 0;
2148
2149 smp->data.u.str.area = path.ptr;
2150 smp->data.u.str.data = path.len;
2151 smp->data.type = SMP_T_STR;
2152 smp->flags = SMP_F_CONST;
2153
2154 return 1;
2155}
2156
2157/* Retrieves the HTTP version from the request (either 1.0 or 1.1) and emits it
2158 * as a string (either "HTTP/1.0" or "HTTP/1.1").
2159 */
2160static int smp_fetch_capture_req_ver(const struct arg *args, struct sample *smp, const char *kw, void *private)
2161{
Willy Tarreau79ed4672020-04-29 11:44:54 +02002162 struct http_txn *txn;
2163
2164 if (!smp->strm)
2165 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02002166
Willy Tarreau79ed4672020-04-29 11:44:54 +02002167 txn = smp->strm->txn;
Willy Tarreau79e57332018-10-02 16:01:16 +02002168 if (!txn || txn->req.msg_state < HTTP_MSG_HDR_FIRST)
2169 return 0;
2170
2171 if (txn->req.flags & HTTP_MSGF_VER_11)
2172 smp->data.u.str.area = "HTTP/1.1";
2173 else
2174 smp->data.u.str.area = "HTTP/1.0";
2175
2176 smp->data.u.str.data = 8;
2177 smp->data.type = SMP_T_STR;
2178 smp->flags = SMP_F_CONST;
2179 return 1;
2180
2181}
2182
2183/* Retrieves the HTTP version from the response (either 1.0 or 1.1) and emits it
2184 * as a string (either "HTTP/1.0" or "HTTP/1.1").
2185 */
2186static int smp_fetch_capture_res_ver(const struct arg *args, struct sample *smp, const char *kw, void *private)
2187{
Willy Tarreau79ed4672020-04-29 11:44:54 +02002188 struct http_txn *txn;
2189
2190 if (!smp->strm)
2191 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02002192
Willy Tarreau79ed4672020-04-29 11:44:54 +02002193 txn = smp->strm->txn;
Willy Tarreau79e57332018-10-02 16:01:16 +02002194 if (!txn || txn->rsp.msg_state < HTTP_MSG_HDR_FIRST)
2195 return 0;
2196
2197 if (txn->rsp.flags & HTTP_MSGF_VER_11)
2198 smp->data.u.str.area = "HTTP/1.1";
2199 else
2200 smp->data.u.str.area = "HTTP/1.0";
2201
2202 smp->data.u.str.data = 8;
2203 smp->data.type = SMP_T_STR;
2204 smp->flags = SMP_F_CONST;
2205 return 1;
2206
2207}
2208
2209/* Iterate over all cookies present in a message. The context is stored in
2210 * smp->ctx.a[0] for the in-header position, smp->ctx.a[1] for the
2211 * end-of-header-value, and smp->ctx.a[2] for the hdr_ctx. Depending on
2212 * the direction, multiple cookies may be parsed on the same line or not.
Maciej Zdeb22dc68b2020-11-13 09:38:06 +00002213 * If provided, the searched cookie name is in args, in args->data.str. If
2214 * the input options indicate that no iterating is desired, then only last
2215 * value is fetched if any. If no cookie name is provided, the first cookie
2216 * value found is fetched. The returned sample is of type CSTR. Can be used
2217 * to parse cookies in other files.
Willy Tarreau79e57332018-10-02 16:01:16 +02002218 */
2219static int smp_fetch_cookie(const struct arg *args, struct sample *smp, const char *kw, void *private)
2220{
Christopher Faulet89dc4992019-04-17 12:02:59 +02002221 /* possible keywords: req.cookie / cookie / cook, res.cookie / scook / set-cookie */
2222 struct channel *chn = ((kw[0] == 'c' || kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Christopher Faulet08f475b2020-11-13 13:41:04 +01002223 char *cook = NULL;
2224 size_t cook_l = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02002225 int found = 0;
2226
Christopher Faulet08f475b2020-11-13 13:41:04 +01002227 if (args && args->type == ARGT_STR) {
2228 cook = args->data.str.area;
2229 cook_l = args->data.str.data;
2230 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002231
Christopher Faulet46575cd2019-04-17 11:40:30 +02002232 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002233 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +02002234 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002235 struct http_hdr_ctx *ctx = smp->ctx.a[2];
2236 struct ist hdr;
Willy Tarreau79e57332018-10-02 16:01:16 +02002237
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002238 if (!ctx) {
2239 /* first call */
2240 ctx = &static_http_hdr_ctx;
2241 ctx->blk = NULL;
2242 smp->ctx.a[2] = ctx;
2243 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002244
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002245 if (!htx)
2246 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02002247
Christopher Faulet89dc4992019-04-17 12:02:59 +02002248 hdr = (!(chn->flags & CF_ISRESP) ? ist("Cookie") : ist("Set-Cookie"));
Willy Tarreau79e57332018-10-02 16:01:16 +02002249
Maciej Zdeb22dc68b2020-11-13 09:38:06 +00002250 /* OK so basically here, either we want only one value or we want to
2251 * iterate over all of them and we fetch the next one. In this last case
2252 * SMP_OPT_ITERATE option is set.
Willy Tarreau79e57332018-10-02 16:01:16 +02002253 */
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002254
2255 if (!(smp->flags & SMP_F_NOT_LAST)) {
2256 /* search for the header from the beginning, we must first initialize
2257 * the search parameters.
2258 */
2259 smp->ctx.a[0] = NULL;
2260 ctx->blk = NULL;
2261 }
2262
2263 smp->flags |= SMP_F_VOL_HDR;
2264 while (1) {
2265 /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */
2266 if (!smp->ctx.a[0]) {
2267 if (!http_find_header(htx, hdr, ctx, 0))
2268 goto out;
2269
Christopher Faulet08f475b2020-11-13 13:41:04 +01002270 if (ctx->value.len < cook_l + 1)
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002271 continue;
2272
2273 smp->ctx.a[0] = ctx->value.ptr;
2274 smp->ctx.a[1] = smp->ctx.a[0] + ctx->value.len;
2275 }
2276
2277 smp->data.type = SMP_T_STR;
2278 smp->flags |= SMP_F_CONST;
2279 smp->ctx.a[0] = http_extract_cookie_value(smp->ctx.a[0], smp->ctx.a[1],
Christopher Faulet08f475b2020-11-13 13:41:04 +01002280 cook, cook_l,
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002281 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
2282 &smp->data.u.str.area,
2283 &smp->data.u.str.data);
2284 if (smp->ctx.a[0]) {
2285 found = 1;
Maciej Zdeb22dc68b2020-11-13 09:38:06 +00002286 if (smp->opt & SMP_OPT_ITERATE) {
2287 /* iterate on cookie value */
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002288 smp->flags |= SMP_F_NOT_LAST;
2289 return 1;
2290 }
Maciej Zdeb22dc68b2020-11-13 09:38:06 +00002291 if (args->data.str.data == 0) {
2292 /* No cookie name, first occurrence returned */
2293 break;
2294 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002295 }
2296 /* if we're looking for last occurrence, let's loop */
2297 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002298 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002299 else {
2300 /* LEGACY version */
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002301 struct hdr_idx *idx;
2302 struct hdr_ctx *ctx = smp->ctx.a[2];
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002303 const char *hdr_name;
2304 int hdr_name_len;
2305 char *sol;
Willy Tarreau79e57332018-10-02 16:01:16 +02002306
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002307 if (!ctx) {
2308 /* first call */
2309 ctx = &static_hdr_ctx;
2310 ctx->idx = 0;
2311 smp->ctx.a[2] = ctx;
2312 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002313
Christopher Faulet89dc4992019-04-17 12:02:59 +02002314 CHECK_HTTP_MESSAGE_FIRST(chn);
Willy Tarreau79e57332018-10-02 16:01:16 +02002315
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002316 idx = &smp->strm->txn->hdr_idx;
Christopher Faulet89dc4992019-04-17 12:02:59 +02002317 if (!(chn->flags & CF_ISRESP)) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002318 hdr_name = "Cookie";
2319 hdr_name_len = 6;
2320 } else {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002321 hdr_name = "Set-Cookie";
2322 hdr_name_len = 10;
Willy Tarreau79e57332018-10-02 16:01:16 +02002323 }
2324
Maciej Zdeb22dc68b2020-11-13 09:38:06 +00002325 /* OK so basically here, either we want only one value or we want to
2326 * iterate over all of them and we fetch the next one. In this last case
2327 * SMP_OPT_ITERATE option is set.
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002328 */
2329
Christopher Faulet89dc4992019-04-17 12:02:59 +02002330 sol = ci_head(chn);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002331 if (!(smp->flags & SMP_F_NOT_LAST)) {
2332 /* search for the header from the beginning, we must first initialize
2333 * the search parameters.
2334 */
2335 smp->ctx.a[0] = NULL;
2336 ctx->idx = 0;
2337 }
2338
2339 smp->flags |= SMP_F_VOL_HDR;
2340
2341 while (1) {
2342 /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */
2343 if (!smp->ctx.a[0]) {
2344 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, ctx))
2345 goto out;
2346
Christopher Faulet08f475b2020-11-13 13:41:04 +01002347 if (ctx->vlen < cook_l + 1)
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002348 continue;
2349
2350 smp->ctx.a[0] = ctx->line + ctx->val;
2351 smp->ctx.a[1] = smp->ctx.a[0] + ctx->vlen;
2352 }
2353
2354 smp->data.type = SMP_T_STR;
2355 smp->flags |= SMP_F_CONST;
2356 smp->ctx.a[0] = http_extract_cookie_value(smp->ctx.a[0], smp->ctx.a[1],
Christopher Faulet08f475b2020-11-13 13:41:04 +01002357 cook, cook_l,
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002358 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
2359 &smp->data.u.str.area, &smp->data.u.str.data);
2360 if (smp->ctx.a[0]) {
2361 found = 1;
Maciej Zdeb22dc68b2020-11-13 09:38:06 +00002362 if (smp->opt & SMP_OPT_ITERATE) {
2363 /* iterate on cookie value */
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002364 smp->flags |= SMP_F_NOT_LAST;
2365 return 1;
2366 }
Maciej Zdeb22dc68b2020-11-13 09:38:06 +00002367 if (args->data.str.data == 0) {
2368 /* No cookie name, first occurrence returned */
2369 break;
2370 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002371 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002372 /* if we're looking for last occurrence, let's loop */
Willy Tarreau79e57332018-10-02 16:01:16 +02002373 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002374 }
2375 /* all cookie headers and values were scanned. If we're looking for the
2376 * last occurrence, we may return it now.
2377 */
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002378 out:
Willy Tarreau79e57332018-10-02 16:01:16 +02002379 smp->flags &= ~SMP_F_NOT_LAST;
2380 return found;
2381}
2382
Christopher Fauletc1f40dd2019-05-16 10:07:30 +02002383/* Same than smp_fetch_cookie() but only relies on the sample direction to
2384 * choose the right channel. So instead of duplicating the code, we just change
2385 * the keyword and then fallback on smp_fetch_cookie().
2386 */
2387static int smp_fetch_chn_cookie(const struct arg *args, struct sample *smp, const char *kw, void *private)
2388{
2389 kw = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ ? "req.cook" : "res.cook");
2390 return smp_fetch_cookie(args, smp, kw, private);
2391}
2392
Willy Tarreau79e57332018-10-02 16:01:16 +02002393/* Iterate over all cookies present in a request to count how many occurrences
2394 * match the name in args and args->data.str.len. If <multi> is non-null, then
2395 * multiple cookies may be parsed on the same line. The returned sample is of
2396 * type UINT. Accepts exactly 1 argument of type string.
2397 */
2398static int smp_fetch_cookie_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
2399{
Christopher Faulet89dc4992019-04-17 12:02:59 +02002400 /* possible keywords: req.cook_cnt / cook_cnt, res.cook_cnt / scook_cnt */
2401 struct channel *chn = ((kw[0] == 'c' || kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Willy Tarreau79e57332018-10-02 16:01:16 +02002402 char *val_beg, *val_end;
Christopher Faulet08f475b2020-11-13 13:41:04 +01002403 char *cook = NULL;
2404 size_t cook_l = 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002405 int cnt;
Willy Tarreau79e57332018-10-02 16:01:16 +02002406
Christopher Faulet08f475b2020-11-13 13:41:04 +01002407 if (args && args->type == ARGT_STR){
2408 cook = args->data.str.area;
2409 cook_l = args->data.str.data;
2410 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002411
Christopher Faulet46575cd2019-04-17 11:40:30 +02002412 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002413 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +02002414 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002415 struct http_hdr_ctx ctx;
2416 struct ist hdr;
2417
2418 if (!htx)
2419 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02002420
Christopher Faulet89dc4992019-04-17 12:02:59 +02002421 hdr = (!(chn->flags & CF_ISRESP) ? ist("Cookie") : ist("Set-Cookie"));
Willy Tarreau79e57332018-10-02 16:01:16 +02002422
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002423 val_end = val_beg = NULL;
2424 ctx.blk = NULL;
2425 cnt = 0;
2426 while (1) {
2427 /* Note: val_beg == NULL every time we need to fetch a new header */
2428 if (!val_beg) {
2429 if (!http_find_header(htx, hdr, &ctx, 0))
2430 break;
Willy Tarreau79e57332018-10-02 16:01:16 +02002431
Christopher Faulet08f475b2020-11-13 13:41:04 +01002432 if (ctx.value.len < cook_l + 1)
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002433 continue;
Willy Tarreau79e57332018-10-02 16:01:16 +02002434
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002435 val_beg = ctx.value.ptr;
2436 val_end = val_beg + ctx.value.len;
2437 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002438
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002439 smp->data.type = SMP_T_STR;
2440 smp->flags |= SMP_F_CONST;
2441 while ((val_beg = http_extract_cookie_value(val_beg, val_end,
Christopher Faulet08f475b2020-11-13 13:41:04 +01002442 cook, cook_l,
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002443 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
2444 &smp->data.u.str.area,
2445 &smp->data.u.str.data))) {
2446 cnt++;
2447 }
2448 }
2449 }
2450 else {
2451 /* LEGACY version */
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002452 struct hdr_idx *idx;
2453 struct hdr_ctx ctx;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002454 const char *hdr_name;
2455 int hdr_name_len;
2456 char *sol;
Willy Tarreau79e57332018-10-02 16:01:16 +02002457
Christopher Faulet89dc4992019-04-17 12:02:59 +02002458 CHECK_HTTP_MESSAGE_FIRST(chn);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002459
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002460 idx = &smp->strm->txn->hdr_idx;
Christopher Faulet89dc4992019-04-17 12:02:59 +02002461 if (!(chn->flags & CF_ISRESP)) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002462 hdr_name = "Cookie";
2463 hdr_name_len = 6;
2464 } else {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002465 hdr_name = "Set-Cookie";
2466 hdr_name_len = 10;
Willy Tarreau79e57332018-10-02 16:01:16 +02002467 }
2468
Christopher Faulet89dc4992019-04-17 12:02:59 +02002469 sol = ci_head(chn);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002470 val_end = val_beg = NULL;
2471 ctx.idx = 0;
2472 cnt = 0;
2473
2474 while (1) {
2475 /* Note: val_beg == NULL every time we need to fetch a new header */
2476 if (!val_beg) {
2477 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, &ctx))
2478 break;
2479
Christopher Faulet08f475b2020-11-13 13:41:04 +01002480 if (ctx.vlen < cook_l + 1)
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002481 continue;
2482
2483 val_beg = ctx.line + ctx.val;
2484 val_end = val_beg + ctx.vlen;
2485 }
2486
2487 smp->data.type = SMP_T_STR;
2488 smp->flags |= SMP_F_CONST;
2489 while ((val_beg = http_extract_cookie_value(val_beg, val_end,
Christopher Faulet08f475b2020-11-13 13:41:04 +01002490 cook, cook_l,
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002491 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
2492 &smp->data.u.str.area, &smp->data.u.str.data))) {
2493 cnt++;
2494 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002495 }
2496 }
2497
2498 smp->data.type = SMP_T_SINT;
2499 smp->data.u.sint = cnt;
2500 smp->flags |= SMP_F_VOL_HDR;
2501 return 1;
2502}
2503
2504/* Fetch an cookie's integer value. The integer value is returned. It
2505 * takes a mandatory argument of type string. It relies on smp_fetch_cookie().
2506 */
2507static int smp_fetch_cookie_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
2508{
2509 int ret = smp_fetch_cookie(args, smp, kw, private);
2510
2511 if (ret > 0) {
2512 smp->data.type = SMP_T_SINT;
2513 smp->data.u.sint = strl2ic(smp->data.u.str.area,
2514 smp->data.u.str.data);
2515 }
2516
2517 return ret;
2518}
2519
2520/************************************************************************/
2521/* The code below is dedicated to sample fetches */
2522/************************************************************************/
2523
2524/* This scans a URL-encoded query string. It takes an optionally wrapping
2525 * string whose first contigous chunk has its beginning in ctx->a[0] and end
2526 * in ctx->a[1], and the optional second part in (ctx->a[2]..ctx->a[3]). The
2527 * pointers are updated for next iteration before leaving.
2528 */
2529static int smp_fetch_param(char delim, const char *name, int name_len, const struct arg *args, struct sample *smp, const char *kw, void *private)
2530{
2531 const char *vstart, *vend;
2532 struct buffer *temp;
2533 const char **chunks = (const char **)smp->ctx.a;
2534
2535 if (!http_find_next_url_param(chunks, name, name_len,
2536 &vstart, &vend, delim))
2537 return 0;
2538
2539 /* Create sample. If the value is contiguous, return the pointer as CONST,
2540 * if the value is wrapped, copy-it in a buffer.
2541 */
2542 smp->data.type = SMP_T_STR;
2543 if (chunks[2] &&
2544 vstart >= chunks[0] && vstart <= chunks[1] &&
2545 vend >= chunks[2] && vend <= chunks[3]) {
2546 /* Wrapped case. */
2547 temp = get_trash_chunk();
2548 memcpy(temp->area, vstart, chunks[1] - vstart);
2549 memcpy(temp->area + ( chunks[1] - vstart ), chunks[2],
2550 vend - chunks[2]);
2551 smp->data.u.str.area = temp->area;
2552 smp->data.u.str.data = ( chunks[1] - vstart ) + ( vend - chunks[2] );
2553 } else {
2554 /* Contiguous case. */
2555 smp->data.u.str.area = (char *)vstart;
2556 smp->data.u.str.data = vend - vstart;
2557 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
2558 }
2559
2560 /* Update context, check wrapping. */
2561 chunks[0] = vend;
2562 if (chunks[2] && vend >= chunks[2] && vend <= chunks[3]) {
2563 chunks[1] = chunks[3];
2564 chunks[2] = NULL;
2565 }
2566
2567 if (chunks[0] < chunks[1])
2568 smp->flags |= SMP_F_NOT_LAST;
2569
2570 return 1;
2571}
2572
2573/* This function iterates over each parameter of the query string. It uses
2574 * ctx->a[0] and ctx->a[1] to store the beginning and end of the current
2575 * parameter. Since it uses smp_fetch_param(), ctx->a[2..3] are both NULL.
2576 * An optional parameter name is passed in args[0], otherwise any parameter is
2577 * considered. It supports an optional delimiter argument for the beginning of
2578 * the string in args[1], which defaults to "?".
2579 */
2580static int smp_fetch_url_param(const struct arg *args, struct sample *smp, const char *kw, void *private)
2581{
Christopher Faulet89dc4992019-04-17 12:02:59 +02002582 struct channel *chn = SMP_REQ_CHN(smp);
Willy Tarreau79e57332018-10-02 16:01:16 +02002583 char delim = '?';
2584 const char *name;
2585 int name_len;
2586
2587 if (!args ||
2588 (args[0].type && args[0].type != ARGT_STR) ||
2589 (args[1].type && args[1].type != ARGT_STR))
2590 return 0;
2591
2592 name = "";
2593 name_len = 0;
2594 if (args->type == ARGT_STR) {
2595 name = args->data.str.area;
2596 name_len = args->data.str.data;
2597 }
2598
2599 if (args[1].type)
2600 delim = *args[1].data.str.area;
2601
2602 if (!smp->ctx.a[0]) { // first call, find the query string
Christopher Faulet46575cd2019-04-17 11:40:30 +02002603 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002604 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +02002605 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01002606 struct htx_sl *sl;
Willy Tarreau79e57332018-10-02 16:01:16 +02002607
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002608 if (!htx)
2609 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02002610
Christopher Faulet297fbb42019-05-13 14:41:27 +02002611 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01002612 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 +02002613 if (!smp->ctx.a[0])
2614 return 0;
2615
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01002616 smp->ctx.a[1] = HTX_SL_REQ_UPTR(sl) + HTX_SL_REQ_ULEN(sl);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002617 }
2618 else {
2619 /* LEGACY version */
2620 struct http_msg *msg;
2621
Christopher Faulet89dc4992019-04-17 12:02:59 +02002622 CHECK_HTTP_MESSAGE_FIRST(chn);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002623
2624 msg = &smp->strm->txn->req;
2625
Christopher Faulet89dc4992019-04-17 12:02:59 +02002626 smp->ctx.a[0] = http_find_param_list(ci_head(chn) + msg->sl.rq.u,
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002627 msg->sl.rq.u_l, delim);
2628 if (!smp->ctx.a[0])
2629 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02002630
Christopher Faulet89dc4992019-04-17 12:02:59 +02002631 smp->ctx.a[1] = ci_head(chn) + msg->sl.rq.u + msg->sl.rq.u_l;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002632 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002633
2634 /* Assume that the context is filled with NULL pointer
2635 * before the first call.
2636 * smp->ctx.a[2] = NULL;
2637 * smp->ctx.a[3] = NULL;
2638 */
2639 }
2640
2641 return smp_fetch_param(delim, name, name_len, args, smp, kw, private);
2642}
2643
2644/* This function iterates over each parameter of the body. This requires
2645 * that the body has been waited for using http-buffer-request. It uses
2646 * ctx->a[0] and ctx->a[1] to store the beginning and end of the first
2647 * contigous part of the body, and optionally ctx->a[2..3] to reference the
2648 * optional second part if the body wraps at the end of the buffer. An optional
2649 * parameter name is passed in args[0], otherwise any parameter is considered.
2650 */
2651static int smp_fetch_body_param(const struct arg *args, struct sample *smp, const char *kw, void *private)
2652{
Christopher Faulet89dc4992019-04-17 12:02:59 +02002653 struct channel *chn = SMP_REQ_CHN(smp);
Willy Tarreau79e57332018-10-02 16:01:16 +02002654 const char *name;
2655 int name_len;
2656
2657 if (!args || (args[0].type && args[0].type != ARGT_STR))
2658 return 0;
2659
2660 name = "";
2661 name_len = 0;
2662 if (args[0].type == ARGT_STR) {
2663 name = args[0].data.str.area;
2664 name_len = args[0].data.str.data;
2665 }
2666
2667 if (!smp->ctx.a[0]) { // first call, find the query string
Christopher Faulet46575cd2019-04-17 11:40:30 +02002668 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002669 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +02002670 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002671 struct buffer *temp;
2672 int32_t pos;
Willy Tarreau79e57332018-10-02 16:01:16 +02002673
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002674 if (!htx)
2675 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02002676
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002677 temp = get_trash_chunk();
Christopher Fauleta3f15502019-05-13 15:27:23 +02002678 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002679 struct htx_blk *blk = htx_get_blk(htx, pos);
2680 enum htx_blk_type type = htx_get_blk_type(blk);
Willy Tarreau79e57332018-10-02 16:01:16 +02002681
Christopher Faulet54b5e212019-06-04 10:08:28 +02002682 if (type == HTX_BLK_EOM || type == HTX_BLK_TLR || type == HTX_BLK_EOT)
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002683 break;
2684 if (type == HTX_BLK_DATA) {
Christopher Fauletc59ff232018-12-03 13:58:44 +01002685 if (!htx_data_to_h1(htx_get_blk_value(htx, blk), temp, 0))
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002686 return 0;
2687 }
2688 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002689
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002690 smp->ctx.a[0] = temp->area;
2691 smp->ctx.a[1] = temp->area + temp->data;
Willy Tarreau79e57332018-10-02 16:01:16 +02002692
2693 /* Assume that the context is filled with NULL pointer
2694 * before the first call.
2695 * smp->ctx.a[2] = NULL;
2696 * smp->ctx.a[3] = NULL;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002697 */
Willy Tarreau79e57332018-10-02 16:01:16 +02002698 }
2699 else {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002700 /* LEGACY version */
2701 struct http_msg *msg;
2702 unsigned long len;
2703 unsigned long block1;
2704 char *body;
2705
Christopher Faulet89dc4992019-04-17 12:02:59 +02002706 CHECK_HTTP_MESSAGE_FIRST(chn);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002707
Christopher Faulet89dc4992019-04-17 12:02:59 +02002708 msg = &smp->strm->txn->req;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002709 len = http_body_bytes(msg);
Christopher Faulet89dc4992019-04-17 12:02:59 +02002710 body = c_ptr(chn, -http_data_rewind(msg));
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002711
2712 block1 = len;
Christopher Faulet89dc4992019-04-17 12:02:59 +02002713 if (block1 > b_wrap(&chn->buf) - body)
2714 block1 = b_wrap(&chn->buf) - body;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002715
2716 if (block1 == len) {
2717 /* buffer is not wrapped (or empty) */
2718 smp->ctx.a[0] = body;
2719 smp->ctx.a[1] = body + len;
2720
2721 /* Assume that the context is filled with NULL pointer
2722 * before the first call.
2723 * smp->ctx.a[2] = NULL;
2724 * smp->ctx.a[3] = NULL;
2725 */
2726 }
2727 else {
2728 /* buffer is wrapped, we need to defragment it */
2729 smp->ctx.a[0] = body;
2730 smp->ctx.a[1] = body + block1;
Christopher Faulet89dc4992019-04-17 12:02:59 +02002731 smp->ctx.a[2] = b_orig(&chn->buf);
2732 smp->ctx.a[3] = b_orig(&chn->buf) + ( len - block1 );
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002733 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002734 }
2735 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002736
Willy Tarreau79e57332018-10-02 16:01:16 +02002737 return smp_fetch_param('&', name, name_len, args, smp, kw, private);
2738}
2739
2740/* Return the signed integer value for the specified url parameter (see url_param
2741 * above).
2742 */
2743static int smp_fetch_url_param_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
2744{
2745 int ret = smp_fetch_url_param(args, smp, kw, private);
2746
2747 if (ret > 0) {
2748 smp->data.type = SMP_T_SINT;
2749 smp->data.u.sint = strl2ic(smp->data.u.str.area,
2750 smp->data.u.str.data);
2751 }
2752
2753 return ret;
2754}
2755
2756/* This produces a 32-bit hash of the concatenation of the first occurrence of
2757 * the Host header followed by the path component if it begins with a slash ('/').
2758 * This means that '*' will not be added, resulting in exactly the first Host
2759 * entry. If no Host header is found, then the path is used. The resulting value
2760 * is hashed using the url hash followed by a full avalanche hash and provides a
2761 * 32-bit integer value. This fetch is useful for tracking per-URL activity on
2762 * high-traffic sites without having to store whole paths.
2763 * this differs from the base32 functions in that it includes the url parameters
2764 * as well as the path
2765 */
2766static int smp_fetch_url32(const struct arg *args, struct sample *smp, const char *kw, void *private)
2767{
Christopher Faulet89dc4992019-04-17 12:02:59 +02002768 struct channel *chn = SMP_REQ_CHN(smp);
Willy Tarreau79e57332018-10-02 16:01:16 +02002769 unsigned int hash = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02002770
Christopher Faulet46575cd2019-04-17 11:40:30 +02002771 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002772 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +02002773 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002774 struct http_hdr_ctx ctx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01002775 struct htx_sl *sl;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002776 struct ist path;
Willy Tarreau79e57332018-10-02 16:01:16 +02002777
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002778 if (!htx)
2779 return 0;
2780
2781 ctx.blk = NULL;
2782 if (http_find_header(htx, ist("Host"), &ctx, 1)) {
2783 /* OK we have the header value in ctx.value */
2784 while (ctx.value.len--)
2785 hash = *(ctx.value.ptr++) + (hash << 6) + (hash << 16) - hash;
2786 }
2787
2788 /* now retrieve the path */
Christopher Faulet297fbb42019-05-13 14:41:27 +02002789 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01002790 path = http_get_path(htx_sl_req_uri(sl));
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002791 if (path.len && *(path.ptr) == '/') {
2792 while (path.len--)
2793 hash = *(path.ptr++) + (hash << 6) + (hash << 16) - hash;
2794 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002795 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002796 else {
2797 /* LEGACY version */
2798 struct http_txn *txn;
2799 struct hdr_ctx ctx;
2800 char *ptr, *beg, *end;
2801 int len;
2802
Christopher Faulet89dc4992019-04-17 12:02:59 +02002803 CHECK_HTTP_MESSAGE_FIRST(chn);
Willy Tarreau79e57332018-10-02 16:01:16 +02002804
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002805 txn = smp->strm->txn;
2806 ctx.idx = 0;
Christopher Faulet89dc4992019-04-17 12:02:59 +02002807 if (http_find_header2("Host", 4, ci_head(chn), &txn->hdr_idx, &ctx)) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002808 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
2809 ptr = ctx.line + ctx.val;
2810 len = ctx.vlen;
2811 while (len--)
2812 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
2813 }
2814
2815 /* now retrieve the path */
Christopher Faulet89dc4992019-04-17 12:02:59 +02002816 end = ci_head(chn) + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002817 beg = http_txn_get_path(txn);
2818 if (!beg)
2819 beg = end;
Willy Tarreau79e57332018-10-02 16:01:16 +02002820
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002821 for (ptr = beg; ptr < end ; ptr++);
Willy Tarreau79e57332018-10-02 16:01:16 +02002822
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002823 if (beg < ptr && *beg == '/') {
2824 while (beg < ptr)
2825 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
2826 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002827 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002828
Willy Tarreau79e57332018-10-02 16:01:16 +02002829 hash = full_hash(hash);
2830
2831 smp->data.type = SMP_T_SINT;
2832 smp->data.u.sint = hash;
2833 smp->flags = SMP_F_VOL_1ST;
2834 return 1;
2835}
2836
2837/* This concatenates the source address with the 32-bit hash of the Host and
2838 * URL as returned by smp_fetch_base32(). The idea is to have per-source and
2839 * per-url counters. The result is a binary block from 8 to 20 bytes depending
2840 * on the source address length. The URL hash is stored before the address so
2841 * that in environments where IPv6 is insignificant, truncating the output to
2842 * 8 bytes would still work.
2843 */
2844static int smp_fetch_url32_src(const struct arg *args, struct sample *smp, const char *kw, void *private)
2845{
2846 struct buffer *temp;
2847 struct connection *cli_conn = objt_conn(smp->sess->origin);
2848
2849 if (!cli_conn)
2850 return 0;
2851
2852 if (!smp_fetch_url32(args, smp, kw, private))
2853 return 0;
2854
2855 temp = get_trash_chunk();
2856 *(unsigned int *) temp->area = htonl(smp->data.u.sint);
2857 temp->data += sizeof(unsigned int);
2858
2859 switch (cli_conn->addr.from.ss_family) {
2860 case AF_INET:
2861 memcpy(temp->area + temp->data,
2862 &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr,
2863 4);
2864 temp->data += 4;
2865 break;
2866 case AF_INET6:
2867 memcpy(temp->area + temp->data,
2868 &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr,
2869 16);
2870 temp->data += 16;
2871 break;
2872 default:
2873 return 0;
2874 }
2875
2876 smp->data.u.str = *temp;
2877 smp->data.type = SMP_T_BIN;
2878 return 1;
2879}
2880
2881/************************************************************************/
2882/* Other utility functions */
2883/************************************************************************/
2884
2885/* This function is used to validate the arguments passed to any "hdr" fetch
2886 * keyword. These keywords support an optional positive or negative occurrence
2887 * number. We must ensure that the number is greater than -MAX_HDR_HISTORY. It
2888 * is assumed that the types are already the correct ones. Returns 0 on error,
2889 * non-zero if OK. If <err> is not NULL, it will be filled with a pointer to an
2890 * error message in case of error, that the caller is responsible for freeing.
2891 * The initial location must either be freeable or NULL.
2892 * Note: this function's pointer is checked from Lua.
2893 */
2894int val_hdr(struct arg *arg, char **err_msg)
2895{
2896 if (arg && arg[1].type == ARGT_SINT && arg[1].data.sint < -MAX_HDR_HISTORY) {
2897 memprintf(err_msg, "header occurrence must be >= %d", -MAX_HDR_HISTORY);
2898 return 0;
2899 }
2900 return 1;
2901}
2902
2903/************************************************************************/
2904/* All supported sample fetch keywords must be declared here. */
2905/************************************************************************/
2906
2907/* Note: must not be declared <const> as its list will be overwritten */
2908static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
2909 { "base", smp_fetch_base, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2910 { "base32", smp_fetch_base32, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
2911 { "base32+src", smp_fetch_base32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
2912
2913 /* capture are allocated and are permanent in the stream */
2914 { "capture.req.hdr", smp_fetch_capture_req_hdr, ARG1(1,SINT), NULL, SMP_T_STR, SMP_USE_HRQHP },
2915
2916 /* retrieve these captures from the HTTP logs */
2917 { "capture.req.method", smp_fetch_capture_req_method, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
2918 { "capture.req.uri", smp_fetch_capture_req_uri, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
2919 { "capture.req.ver", smp_fetch_capture_req_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
2920
2921 { "capture.res.hdr", smp_fetch_capture_res_hdr, ARG1(1,SINT), NULL, SMP_T_STR, SMP_USE_HRSHP },
2922 { "capture.res.ver", smp_fetch_capture_res_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
2923
2924 /* cookie is valid in both directions (eg: for "stick ...") but cook*
2925 * are only here to match the ACL's name, are request-only and are used
2926 * for ACL compatibility only.
2927 */
2928 { "cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Christopher Fauletc1f40dd2019-05-16 10:07:30 +02002929 { "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 +02002930 { "cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2931 { "cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2932
2933 /* hdr is valid in both directions (eg: for "stick ...") but hdr_* are
2934 * only here to match the ACL's name, are request-only and are used for
2935 * ACL compatibility only.
2936 */
Christopher Fauletc1f40dd2019-05-16 10:07:30 +02002937 { "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 +02002938 { "hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2939 { "hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV },
2940 { "hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_SINT, SMP_USE_HRQHV },
2941
2942 { "http_auth", smp_fetch_http_auth, ARG1(1,USR), NULL, SMP_T_BOOL, SMP_USE_HRQHV },
2943 { "http_auth_group", smp_fetch_http_auth_grp, ARG1(1,USR), NULL, SMP_T_STR, SMP_USE_HRQHV },
2944 { "http_first_req", smp_fetch_http_first_req, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
2945 { "method", smp_fetch_meth, 0, NULL, SMP_T_METH, SMP_USE_HRQHP },
2946 { "path", smp_fetch_path, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2947 { "query", smp_fetch_query, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2948
2949 /* HTTP protocol on the request path */
2950 { "req.proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
2951 { "req_proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
2952
2953 /* HTTP version on the request path */
2954 { "req.ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2955 { "req_ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2956
2957 { "req.body", smp_fetch_body, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
2958 { "req.body_len", smp_fetch_body_len, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
2959 { "req.body_size", smp_fetch_body_size, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
2960 { "req.body_param", smp_fetch_body_param, ARG1(0,STR), NULL, SMP_T_BIN, SMP_USE_HRQHV },
2961
2962 { "req.hdrs", smp_fetch_hdrs, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
2963 { "req.hdrs_bin", smp_fetch_hdrs_bin, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
2964
2965 /* HTTP version on the response path */
2966 { "res.ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
2967 { "resp_ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
2968
2969 /* explicit req.{cook,hdr} are used to force the fetch direction to be request-only */
2970 { "req.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
2971 { "req.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2972 { "req.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2973
2974 { "req.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
2975 { "req.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2976 { "req.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
2977 { "req.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2978 { "req.hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV },
2979 { "req.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
2980 { "req.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_SINT, SMP_USE_HRQHV },
2981
2982 /* explicit req.{cook,hdr} are used to force the fetch direction to be response-only */
2983 { "res.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
2984 { "res.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2985 { "res.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2986
2987 { "res.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
2988 { "res.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2989 { "res.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
2990 { "res.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2991 { "res.hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV },
2992 { "res.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
2993 { "res.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_SINT, SMP_USE_HRSHV },
2994
2995 /* scook is valid only on the response and is used for ACL compatibility */
2996 { "scook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
2997 { "scook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2998 { "scook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2999 { "set-cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV }, /* deprecated */
3000
3001 /* shdr is valid only on the response and is used for ACL compatibility */
3002 { "shdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
3003 { "shdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
3004 { "shdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV },
3005 { "shdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_SINT, SMP_USE_HRSHV },
3006
3007 { "status", smp_fetch_stcode, 0, NULL, SMP_T_SINT, SMP_USE_HRSHP },
3008 { "unique-id", smp_fetch_uniqueid, 0, NULL, SMP_T_STR, SMP_SRC_L4SRV },
3009 { "url", smp_fetch_url, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
3010 { "url32", smp_fetch_url32, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
3011 { "url32+src", smp_fetch_url32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
3012 { "url_ip", smp_fetch_url_ip, 0, NULL, SMP_T_IPV4, SMP_USE_HRQHV },
3013 { "url_port", smp_fetch_url_port, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
3014 { "url_param", smp_fetch_url_param, ARG2(0,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
3015 { "urlp" , smp_fetch_url_param, ARG2(0,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
3016 { "urlp_val", smp_fetch_url_param_val, ARG2(0,STR,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
3017 { /* END */ },
3018}};
3019
Willy Tarreau0108d902018-11-25 19:14:37 +01003020INITCALL1(STG_REGISTER, sample_register_fetches, &sample_fetch_keywords);
Willy Tarreau79e57332018-10-02 16:01:16 +02003021
3022/*
3023 * Local variables:
3024 * c-indent-level: 8
3025 * c-basic-offset: 8
3026 * End:
3027 */