blob: 5fc68a9f227d2dec7eed9a7e9ba4c4b91f1da61c [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>
Christopher Fauleteb2754b2019-07-16 14:49:01 +020036#include <proto/channel.h>
Willy Tarreau9a1efe12019-07-17 17:13:50 +020037#include <proto/connection.h>
Willy Tarreau79e57332018-10-02 16:01:16 +020038#include <proto/http_fetch.h>
Christopher Faulet53a899b2019-10-08 16:38:42 +020039#include <proto/h1_htx.h>
Christopher Fauletef453ed2018-10-24 21:39:27 +020040#include <proto/http_htx.h>
Willy Tarreau79e57332018-10-02 16:01:16 +020041#include <proto/log.h>
42#include <proto/obj_type.h>
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020043#include <proto/http_ana.h>
Willy Tarreau79e57332018-10-02 16:01:16 +020044#include <proto/sample.h>
45#include <proto/stream.h>
46
47
48/* this struct is used between calls to smp_fetch_hdr() or smp_fetch_cookie() */
Christopher Fauletef453ed2018-10-24 21:39:27 +020049static THREAD_LOCAL struct http_hdr_ctx static_http_hdr_ctx;
Richard Russo458eafb2019-07-31 11:45:56 -070050/* this is used to convert raw connection buffers to htx */
51static THREAD_LOCAL struct buffer static_raw_htx_chunk;
52static THREAD_LOCAL char *static_raw_htx_buf;
Christopher Fauletef453ed2018-10-24 21:39:27 +020053
Christopher Faulet89dc4992019-04-17 12:02:59 +020054#define SMP_REQ_CHN(smp) (smp->strm ? &smp->strm->req : NULL)
55#define SMP_RES_CHN(smp) (smp->strm ? &smp->strm->res : NULL)
Willy Tarreau79e57332018-10-02 16:01:16 +020056
Richard Russo458eafb2019-07-31 11:45:56 -070057/* This function returns the static htx chunk, where raw connections get
58 * converted to HTX as needed for samplxsing.
59 */
60struct buffer *get_raw_htx_chunk(void)
61{
62 chunk_reset(&static_raw_htx_chunk);
63 return &static_raw_htx_chunk;
64}
65
66static int alloc_raw_htx_chunk_per_thread()
67{
68 static_raw_htx_buf = malloc(global.tune.bufsize);
69 if (!static_raw_htx_buf)
70 return 0;
71 chunk_init(&static_raw_htx_chunk, static_raw_htx_buf, global.tune.bufsize);
72 return 1;
73}
74
75static void free_raw_htx_chunk_per_thread()
76{
77 free(static_raw_htx_buf);
78 static_raw_htx_buf = NULL;
79}
80
81REGISTER_PER_THREAD_ALLOC(alloc_raw_htx_chunk_per_thread);
82REGISTER_PER_THREAD_FREE(free_raw_htx_chunk_per_thread);
83
Willy Tarreau79e57332018-10-02 16:01:16 +020084/*
85 * Returns the data from Authorization header. Function may be called more
86 * than once so data is stored in txn->auth_data. When no header is found
87 * or auth method is unknown auth_method is set to HTTP_AUTH_WRONG to avoid
88 * searching again for something we are unable to find anyway. However, if
89 * the result if valid, the cache is not reused because we would risk to
90 * have the credentials overwritten by another stream in parallel.
Willy Tarreaueae83722020-04-29 11:52:51 +020091 * The caller is responsible for passing a sample with a valid stream/txn,
92 * and a valid htx.
Willy Tarreau79e57332018-10-02 16:01:16 +020093 */
94
Christopher Fauletcd761952019-07-15 13:58:29 +020095static int get_http_auth(struct sample *smp, struct htx *htx)
Willy Tarreau79e57332018-10-02 16:01:16 +020096{
Christopher Faulet311c7ea2018-10-24 21:41:55 +020097 struct stream *s = smp->strm;
Willy Tarreau79e57332018-10-02 16:01:16 +020098 struct http_txn *txn = s->txn;
Christopher Faulet6d1dd462019-07-15 14:36:03 +020099 struct http_hdr_ctx ctx = { .blk = NULL };
100 struct ist hdr;
Willy Tarreau79e57332018-10-02 16:01:16 +0200101 struct buffer auth_method;
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200102 char *p;
Willy Tarreau79e57332018-10-02 16:01:16 +0200103 int len;
104
105#ifdef DEBUG_AUTH
106 printf("Auth for stream %p: %d\n", s, txn->auth.method);
107#endif
Willy Tarreau79e57332018-10-02 16:01:16 +0200108 if (txn->auth.method == HTTP_AUTH_WRONG)
109 return 0;
110
111 txn->auth.method = HTTP_AUTH_WRONG;
112
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200113 if (txn->flags & TX_USE_PX_CONN)
114 hdr = ist("Proxy-Authorization");
115 else
116 hdr = ist("Authorization");
Willy Tarreau79e57332018-10-02 16:01:16 +0200117
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200118 ctx.blk = NULL;
119 if (!http_find_header(htx, hdr, &ctx, 0))
120 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200121
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200122 p = memchr(ctx.value.ptr, ' ', ctx.value.len);
123 len = p - ctx.value.ptr;
124 if (!p || len <= 0)
125 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200126
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200127 if (chunk_initlen(&auth_method, ctx.value.ptr, 0, len) != 1)
128 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200129
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200130 chunk_initlen(&txn->auth.method_data, p + 1, 0, ctx.value.len - len - 1);
Willy Tarreau79e57332018-10-02 16:01:16 +0200131
132 if (!strncasecmp("Basic", auth_method.area, auth_method.data)) {
133 struct buffer *http_auth = get_trash_chunk();
134
135 len = base64dec(txn->auth.method_data.area,
136 txn->auth.method_data.data,
137 http_auth->area, global.tune.bufsize - 1);
138
139 if (len < 0)
140 return 0;
141
142
143 http_auth->area[len] = '\0';
144
145 p = strchr(http_auth->area, ':');
146
147 if (!p)
148 return 0;
149
150 txn->auth.user = http_auth->area;
151 *p = '\0';
152 txn->auth.pass = p+1;
153
154 txn->auth.method = HTTP_AUTH_BASIC;
155 return 1;
156 }
157
158 return 0;
159}
160
161/* This function ensures that the prerequisites for an L7 fetch are ready,
162 * which means that a request or response is ready. If some data is missing,
163 * a parsing attempt is made. This is useful in TCP-based ACLs which are able
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +0200164 * to extract data from L7. If <vol> is non-null during a prefetch, another
165 * test is made to ensure the required information is not gone.
Christopher Fauletef453ed2018-10-24 21:39:27 +0200166 *
167 * The function returns :
168 * NULL with SMP_F_MAY_CHANGE in the sample flags if some data is missing to
169 * decide whether or not an HTTP message is present ;
170 * NULL if the requested data cannot be fetched or if it is certain that
Willy Tarreaueae83722020-04-29 11:52:51 +0200171 * we'll never have any HTTP message there; this includes null strm or chn.
Christopher Fauletef453ed2018-10-24 21:39:27 +0200172 * The HTX message if ready
173 */
Christopher Faulet778f5ed2020-04-29 15:51:55 +0200174struct htx *smp_prefetch_htx(struct sample *smp, struct channel *chn, struct check *check, int vol)
Christopher Fauletef453ed2018-10-24 21:39:27 +0200175{
Christopher Fauletef453ed2018-10-24 21:39:27 +0200176 struct stream *s = smp->strm;
Christopher Fauletef453ed2018-10-24 21:39:27 +0200177 struct http_txn *txn = NULL;
178 struct htx *htx = NULL;
Christopher Faulet89dc4992019-04-17 12:02:59 +0200179 struct http_msg *msg;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100180 struct htx_sl *sl;
Christopher Fauletef453ed2018-10-24 21:39:27 +0200181
182 /* Note: it is possible that <s> is NULL when called before stream
183 * initialization (eg: tcp-request connection), so this function is the
184 * one responsible for guarding against this case for all HTTP users.
Christopher Faulet778f5ed2020-04-29 15:51:55 +0200185 *
186 * In the health check context, the stream and the channel must be NULL
187 * and <check> must be set. In this case, only the input buffer,
188 * corresponding to the response, is considered. It is the caller
189 * responsibility to provide <check>.
Christopher Fauletef453ed2018-10-24 21:39:27 +0200190 */
Christopher Faulet778f5ed2020-04-29 15:51:55 +0200191 BUG_ON(check && (s || chn));
192 if (!s || !chn) {
193 if (check) {
194 htx = htxbuf(&check->bi);
195
196 /* Analyse not yet started */
197 if (htx_is_empty(htx) || htx->first == -1)
198 return NULL;
199
200 sl = http_get_stline(htx);
201 if (vol && !sl) {
202 /* The start-line was already forwarded, it is too late to fetch anything */
203 return NULL;
204 }
205 goto end;
206 }
207
Christopher Fauletef453ed2018-10-24 21:39:27 +0200208 return NULL;
Christopher Faulet778f5ed2020-04-29 15:51:55 +0200209 }
Christopher Fauletef453ed2018-10-24 21:39:27 +0200210
211 if (!s->txn) {
212 if (unlikely(!http_alloc_txn(s)))
213 return NULL; /* not enough memory */
214 http_init_txn(s);
215 txn = s->txn;
216 }
Christopher Faulet89dc4992019-04-17 12:02:59 +0200217 txn = s->txn;
218 msg = (!(chn->flags & CF_ISRESP) ? &txn->req : &txn->rsp);
219 smp->data.type = SMP_T_BOOL;
Christopher Fauletef453ed2018-10-24 21:39:27 +0200220
Christopher Fauleteca88542019-04-03 10:12:42 +0200221 if (IS_HTX_STRM(s)) {
Christopher Faulet89dc4992019-04-17 12:02:59 +0200222 htx = htxbuf(&chn->buf);
Christopher Fauletef453ed2018-10-24 21:39:27 +0200223
Christopher Faulet89dc4992019-04-17 12:02:59 +0200224 if (msg->msg_state == HTTP_MSG_ERROR || (htx->flags & HTX_FL_PARSING_ERROR))
225 return NULL;
Christopher Fauletef453ed2018-10-24 21:39:27 +0200226
Christopher Faulet89dc4992019-04-17 12:02:59 +0200227 if (msg->msg_state < HTTP_MSG_BODY) {
228 /* Analyse not yet started */
Christopher Faulet29f17582019-05-23 11:03:26 +0200229 if (htx_is_empty(htx) || htx->first == -1) {
Christopher Fauletef453ed2018-10-24 21:39:27 +0200230 /* Parsing is done by the mux, just wait */
231 smp->flags |= SMP_F_MAY_CHANGE;
232 return NULL;
233 }
234 }
Christopher Faulet297fbb42019-05-13 14:41:27 +0200235 sl = http_get_stline(htx);
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +0200236 if (vol && !sl) {
Christopher Faulet89dc4992019-04-17 12:02:59 +0200237 /* The start-line was already forwarded, it is too late to fetch anything */
238 return NULL;
239 }
Christopher Fauletef453ed2018-10-24 21:39:27 +0200240 }
Christopher Fauleteca88542019-04-03 10:12:42 +0200241 else { /* RAW mode */
Christopher Faulet89dc4992019-04-17 12:02:59 +0200242 struct buffer *buf;
243 struct h1m h1m;
Christopher Faulete4ab11b2019-06-11 15:05:37 +0200244 struct http_hdr hdrs[global.tune.max_http_hdr];
Christopher Faulet89dc4992019-04-17 12:02:59 +0200245 union h1_sl h1sl;
246 unsigned int flags = HTX_FL_NONE;
247 int ret;
Christopher Fauletef453ed2018-10-24 21:39:27 +0200248
Christopher Faulet89dc4992019-04-17 12:02:59 +0200249 /* no HTTP fetch on the response in TCP mode */
250 if (chn->flags & CF_ISRESP)
251 return NULL;
Christopher Fauletef453ed2018-10-24 21:39:27 +0200252
Christopher Faulet89dc4992019-04-17 12:02:59 +0200253 /* Now we are working on the request only */
254 buf = &chn->buf;
255 if (b_head(buf) + b_data(buf) > b_wrap(buf))
256 b_slow_realign(buf, trash.area, 0);
Christopher Fauletef453ed2018-10-24 21:39:27 +0200257
Christopher Faulet89dc4992019-04-17 12:02:59 +0200258 h1m_init_req(&h1m);
259 ret = h1_headers_to_hdr_list(b_head(buf), b_stop(buf),
260 hdrs, sizeof(hdrs)/sizeof(hdrs[0]), &h1m, &h1sl);
261 if (ret <= 0) {
262 /* Invalid or too big*/
263 if (ret < 0 || channel_full(&s->req, global.tune.maxrewrite))
Christopher Fauletef453ed2018-10-24 21:39:27 +0200264 return NULL;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100265
Christopher Faulet89dc4992019-04-17 12:02:59 +0200266 /* wait for a full request */
267 smp->flags |= SMP_F_MAY_CHANGE;
268 return NULL;
269 }
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100270
Christopher Faulet89dc4992019-04-17 12:02:59 +0200271 /* OK we just got a valid HTTP mesage. We have to convert it
272 * into an HTX message.
273 */
274 if (unlikely(h1sl.rq.v.len == 0)) {
275 /* try to convert HTTP/0.9 requests to HTTP/1.0 */
276 if (h1sl.rq.meth != HTTP_METH_GET || !h1sl.rq.u.len)
Christopher Fauletef453ed2018-10-24 21:39:27 +0200277 return NULL;
Christopher Faulet89dc4992019-04-17 12:02:59 +0200278 h1sl.rq.v = ist("HTTP/1.0");
Christopher Fauletef453ed2018-10-24 21:39:27 +0200279 }
Christopher Faulet89dc4992019-04-17 12:02:59 +0200280
281 /* Set HTX start-line flags */
282 if (h1m.flags & H1_MF_VER_11)
283 flags |= HTX_SL_F_VER_11;
284 if (h1m.flags & H1_MF_XFER_ENC)
285 flags |= HTX_SL_F_XFER_ENC;
286 flags |= HTX_SL_F_XFER_LEN;
287 if (h1m.flags & H1_MF_CHNK)
288 flags |= HTX_SL_F_CHNK;
289 else if (h1m.flags & H1_MF_CLEN)
290 flags |= HTX_SL_F_CLEN;
291
Richard Russo458eafb2019-07-31 11:45:56 -0700292 htx = htx_from_buf(get_raw_htx_chunk());
Christopher Faulet89dc4992019-04-17 12:02:59 +0200293 sl = htx_add_stline(htx, HTX_BLK_REQ_SL, flags, h1sl.rq.m, h1sl.rq.u, h1sl.rq.v);
294 if (!sl || !htx_add_all_headers(htx, hdrs))
Christopher Fauletef453ed2018-10-24 21:39:27 +0200295 return NULL;
Willy Tarreauce9bbf52019-05-13 08:32:31 +0200296 sl->info.req.meth = h1sl.rq.meth;
Christopher Faulet89dc4992019-04-17 12:02:59 +0200297 }
298
299 /* OK we just got a valid HTTP message. If not already done by
300 * HTTP analyzers, we have some minor preparation to perform so
301 * that further checks can rely on HTTP tests.
302 */
303 if (sl && msg->msg_state < HTTP_MSG_BODY) {
304 if (!(chn->flags & CF_ISRESP)) {
305 txn->meth = sl->info.req.meth;
306 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
307 s->flags |= SF_REDIRECTABLE;
Christopher Fauletef453ed2018-10-24 21:39:27 +0200308 }
Christopher Faulet89dc4992019-04-17 12:02:59 +0200309 else
310 txn->status = sl->info.res.status;
311 if (sl->flags & HTX_SL_F_VER_11)
312 msg->flags |= HTTP_MSGF_VER_11;
Christopher Fauletef453ed2018-10-24 21:39:27 +0200313 }
314
315 /* everything's OK */
Christopher Faulet778f5ed2020-04-29 15:51:55 +0200316 end:
Christopher Fauletef453ed2018-10-24 21:39:27 +0200317 smp->data.u.sint = 1;
318 return htx;
319}
320
Willy Tarreau79e57332018-10-02 16:01:16 +0200321/* This function fetches the method of current HTTP request and stores
322 * it in the global pattern struct as a chunk. There are two possibilities :
323 * - if the method is known (not HTTP_METH_OTHER), its identifier is stored
324 * in <len> and <ptr> is NULL ;
325 * - if the method is unknown (HTTP_METH_OTHER), <ptr> points to the text and
326 * <len> to its length.
327 * This is intended to be used with pat_match_meth() only.
328 */
329static int smp_fetch_meth(const struct arg *args, struct sample *smp, const char *kw, void *private)
330{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200331 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +0200332 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 0);
Willy Tarreau79e57332018-10-02 16:01:16 +0200333 struct http_txn *txn;
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200334 int meth;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200335
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200336 if (!htx)
337 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200338
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200339 txn = smp->strm->txn;
340 meth = txn->meth;
341 smp->data.type = SMP_T_METH;
342 smp->data.u.meth.meth = meth;
343 if (meth == HTTP_METH_OTHER) {
344 struct htx_sl *sl;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200345
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200346 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) {
347 /* ensure the indexes are not affected */
348 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200349 }
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200350 sl = http_get_stline(htx);
351 smp->flags |= SMP_F_CONST;
352 smp->data.u.meth.str.area = HTX_SL_REQ_MPTR(sl);
353 smp->data.u.meth.str.data = HTX_SL_REQ_MLEN(sl);
Willy Tarreau79e57332018-10-02 16:01:16 +0200354 }
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200355 smp->flags |= SMP_F_VOL_1ST;
Willy Tarreau79e57332018-10-02 16:01:16 +0200356 return 1;
357}
358
359static int smp_fetch_rqver(const struct arg *args, struct sample *smp, const char *kw, void *private)
360{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200361 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +0200362 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200363 struct htx_sl *sl;
Willy Tarreau79e57332018-10-02 16:01:16 +0200364 char *ptr;
365 int len;
366
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200367 if (!htx)
368 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200369
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200370 sl = http_get_stline(htx);
371 len = HTX_SL_REQ_VLEN(sl);
372 ptr = HTX_SL_REQ_VPTR(sl);
Willy Tarreau79e57332018-10-02 16:01:16 +0200373
374 while ((len-- > 0) && (*ptr++ != '/'));
375 if (len <= 0)
376 return 0;
377
378 smp->data.type = SMP_T_STR;
379 smp->data.u.str.area = ptr;
380 smp->data.u.str.data = len;
381
382 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
383 return 1;
384}
385
386static int smp_fetch_stver(const struct arg *args, struct sample *smp, const char *kw, void *private)
387{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200388 struct channel *chn = SMP_RES_CHN(smp);
Christopher Fauletf98e6262020-05-06 09:42:04 +0200389 struct check *check = objt_check(smp->sess->origin);
Christopher Faulet16032ab2020-04-30 11:30:00 +0200390 struct htx *htx = smp_prefetch_htx(smp, chn, check, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200391 struct htx_sl *sl;
Willy Tarreau79e57332018-10-02 16:01:16 +0200392 char *ptr;
393 int len;
394
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200395 if (!htx)
396 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200397
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200398 sl = http_get_stline(htx);
399 len = HTX_SL_RES_VLEN(sl);
400 ptr = HTX_SL_RES_VPTR(sl);
Willy Tarreau79e57332018-10-02 16:01:16 +0200401
402 while ((len-- > 0) && (*ptr++ != '/'));
403 if (len <= 0)
404 return 0;
405
406 smp->data.type = SMP_T_STR;
407 smp->data.u.str.area = ptr;
408 smp->data.u.str.data = len;
409
410 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
411 return 1;
412}
413
414/* 3. Check on Status Code. We manipulate integers here. */
415static int smp_fetch_stcode(const struct arg *args, struct sample *smp, const char *kw, void *private)
416{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200417 struct channel *chn = SMP_RES_CHN(smp);
Christopher Fauletf98e6262020-05-06 09:42:04 +0200418 struct check *check = objt_check(smp->sess->origin);
Christopher Faulet16032ab2020-04-30 11:30:00 +0200419 struct htx *htx = smp_prefetch_htx(smp, chn, check, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200420 struct htx_sl *sl;
Willy Tarreau79e57332018-10-02 16:01:16 +0200421 char *ptr;
422 int len;
423
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200424 if (!htx)
425 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200426
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200427 sl = http_get_stline(htx);
428 len = HTX_SL_RES_CLEN(sl);
429 ptr = HTX_SL_RES_CPTR(sl);
Willy Tarreau79e57332018-10-02 16:01:16 +0200430
431 smp->data.type = SMP_T_SINT;
432 smp->data.u.sint = __strl2ui(ptr, len);
433 smp->flags = SMP_F_VOL_1ST;
434 return 1;
435}
436
437static int smp_fetch_uniqueid(const struct arg *args, struct sample *smp, const char *kw, void *private)
438{
Tim Duesterhusa17e6622020-03-05 20:19:02 +0100439 struct ist unique_id;
Tim Duesterhus2825b4b2020-02-28 15:13:34 +0100440
Willy Tarreau79e57332018-10-02 16:01:16 +0200441 if (LIST_ISEMPTY(&smp->sess->fe->format_unique_id))
442 return 0;
443
Willy Tarreaua1062a42020-04-29 11:50:38 +0200444 if (!smp->strm)
445 return 0;
446
Tim Duesterhusa17e6622020-03-05 20:19:02 +0100447 unique_id = stream_generate_unique_id(smp->strm, &smp->sess->fe->format_unique_id);
448 if (!isttest(unique_id))
Tim Duesterhus2825b4b2020-02-28 15:13:34 +0100449 return 0;
450
Tim Duesterhusa17e6622020-03-05 20:19:02 +0100451 smp->data.u.str.area = smp->strm->unique_id.ptr;
452 smp->data.u.str.data = smp->strm->unique_id.len;
Tim Duesterhus2825b4b2020-02-28 15:13:34 +0100453 smp->data.type = SMP_T_STR;
Willy Tarreau79e57332018-10-02 16:01:16 +0200454 smp->flags = SMP_F_CONST;
455 return 1;
456}
457
458/* Returns a string block containing all headers including the
Joseph Herlant942eea32018-11-15 13:57:22 -0800459 * empty line which separes headers from the body. This is useful
460 * for some headers analysis.
Willy Tarreau79e57332018-10-02 16:01:16 +0200461 */
462static int smp_fetch_hdrs(const struct arg *args, struct sample *smp, const char *kw, void *private)
463{
Christopher Faulete596d182020-05-05 17:46:34 +0200464 /* possible keywords: req.hdrs, res.hdrs */
465 struct channel *chn = ((kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Christopher Fauletf98e6262020-05-06 09:42:04 +0200466 struct check *check = ((kw[2] == 's') ? objt_check(smp->sess->origin) : NULL);
Christopher Faulet16032ab2020-04-30 11:30:00 +0200467 struct htx *htx = smp_prefetch_htx(smp, chn, check, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200468 struct buffer *temp;
469 int32_t pos;
Willy Tarreau79e57332018-10-02 16:01:16 +0200470
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200471 if (!htx)
472 return 0;
473 temp = get_trash_chunk();
474 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
475 struct htx_blk *blk = htx_get_blk(htx, pos);
476 enum htx_blk_type type = htx_get_blk_type(blk);
Willy Tarreau79e57332018-10-02 16:01:16 +0200477
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200478 if (type == HTX_BLK_HDR) {
479 struct ist n = htx_get_blk_name(htx, blk);
480 struct ist v = htx_get_blk_value(htx, blk);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200481
Christopher Faulet53a899b2019-10-08 16:38:42 +0200482 if (!h1_format_htx_hdr(n, v, temp))
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200483 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200484 }
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200485 else if (type == HTX_BLK_EOH) {
486 if (!chunk_memcat(temp, "\r\n", 2))
487 return 0;
488 break;
489 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200490 }
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200491 smp->data.type = SMP_T_STR;
492 smp->data.u.str = *temp;
Willy Tarreau79e57332018-10-02 16:01:16 +0200493 return 1;
494}
495
496/* Returns the header request in a length/value encoded format.
497 * This is useful for exchanges with the SPOE.
498 *
499 * A "length value" is a multibyte code encoding numbers. It uses the
500 * SPOE format. The encoding is the following:
501 *
502 * Each couple "header name" / "header value" is composed
503 * like this:
504 * "length value" "header name bytes"
505 * "length value" "header value bytes"
506 * When the last header is reached, the header name and the header
507 * value are empty. Their length are 0
508 */
509static int smp_fetch_hdrs_bin(const struct arg *args, struct sample *smp, const char *kw, void *private)
510{
Christopher Faulete596d182020-05-05 17:46:34 +0200511 /* possible keywords: req.hdrs_bin, res.hdrs_bin */
512 struct channel *chn = ((kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Christopher Fauletf98e6262020-05-06 09:42:04 +0200513 struct check *check = ((kw[2] == 's') ? objt_check(smp->sess->origin) : NULL);
Christopher Faulet16032ab2020-04-30 11:30:00 +0200514 struct htx *htx = smp_prefetch_htx(smp, chn, check, 1);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200515 struct buffer *temp;
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200516 char *p, *end;
517 int32_t pos;
518 int ret;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200519
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200520 if (!htx)
521 return 0;
522 temp = get_trash_chunk();
523 p = temp->area;
524 end = temp->area + temp->size;
525 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
526 struct htx_blk *blk = htx_get_blk(htx, pos);
527 enum htx_blk_type type = htx_get_blk_type(blk);
528 struct ist n, v;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200529
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200530 if (type == HTX_BLK_HDR) {
531 n = htx_get_blk_name(htx,blk);
532 v = htx_get_blk_value(htx, blk);
Willy Tarreau79e57332018-10-02 16:01:16 +0200533
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200534 /* encode the header name. */
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200535 ret = encode_varint(n.len, &p, end);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200536 if (ret == -1)
537 return 0;
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200538 if (p + n.len > end)
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200539 return 0;
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200540 memcpy(p, n.ptr, n.len);
541 p += n.len;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200542
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200543 /* encode the header value. */
544 ret = encode_varint(v.len, &p, end);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200545 if (ret == -1)
546 return 0;
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200547 if (p + v.len > end)
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200548 return 0;
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200549 memcpy(p, v.ptr, v.len);
550 p += v.len;
Willy Tarreau79e57332018-10-02 16:01:16 +0200551
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200552 }
553 else if (type == HTX_BLK_EOH) {
554 /* encode the end of the header list with empty
555 * header name and header value.
556 */
557 ret = encode_varint(0, &p, end);
558 if (ret == -1)
559 return 0;
560 ret = encode_varint(0, &p, end);
561 if (ret == -1)
562 return 0;
563 break;
564 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200565 }
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200566
567 /* Initialise sample data which will be filled. */
568 smp->data.type = SMP_T_BIN;
569 smp->data.u.str.area = temp->area;
570 smp->data.u.str.data = p - temp->area;
571 smp->data.u.str.size = temp->size;
Willy Tarreau79e57332018-10-02 16:01:16 +0200572 return 1;
573}
574
575/* returns the longest available part of the body. This requires that the body
576 * has been waited for using http-buffer-request.
577 */
578static int smp_fetch_body(const struct arg *args, struct sample *smp, const char *kw, void *private)
579{
Christopher Faulete596d182020-05-05 17:46:34 +0200580 /* possible keywords: req.body, res.body */
581 struct channel *chn = ((kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Christopher Fauletf98e6262020-05-06 09:42:04 +0200582 struct check *check = ((kw[2] == 's') ? objt_check(smp->sess->origin) : NULL);
Christopher Faulet16032ab2020-04-30 11:30:00 +0200583 struct htx *htx = smp_prefetch_htx(smp, chn, check, 1);
Willy Tarreau79e57332018-10-02 16:01:16 +0200584 struct buffer *temp;
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200585 int32_t pos;
Willy Tarreau79e57332018-10-02 16:01:16 +0200586
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200587 if (!htx)
588 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200589
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200590 temp = get_trash_chunk();
591 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
592 struct htx_blk *blk = htx_get_blk(htx, pos);
593 enum htx_blk_type type = htx_get_blk_type(blk);
Willy Tarreau79e57332018-10-02 16:01:16 +0200594
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200595 if (type == HTX_BLK_EOM || type == HTX_BLK_TLR || type == HTX_BLK_EOT)
596 break;
597 if (type == HTX_BLK_DATA) {
Christopher Faulet53a899b2019-10-08 16:38:42 +0200598 if (!h1_format_htx_data(htx_get_blk_value(htx, blk), temp, 0))
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200599 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200600 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200601 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200602
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200603 smp->data.type = SMP_T_BIN;
604 smp->data.u.str = *temp;
605 smp->flags = SMP_F_VOL_TEST;
Willy Tarreau79e57332018-10-02 16:01:16 +0200606 return 1;
607}
608
609
610/* returns the available length of the body. This requires that the body
611 * has been waited for using http-buffer-request.
612 */
613static int smp_fetch_body_len(const struct arg *args, struct sample *smp, const char *kw, void *private)
614{
Christopher Faulete596d182020-05-05 17:46:34 +0200615 /* possible keywords: req.body_len, res.body_len */
616 struct channel *chn = ((kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Christopher Fauletf98e6262020-05-06 09:42:04 +0200617 struct check *check = ((kw[2] == 's') ? objt_check(smp->sess->origin) : NULL);
Christopher Faulet16032ab2020-04-30 11:30:00 +0200618 struct htx *htx = smp_prefetch_htx(smp, chn, check, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200619 int32_t pos;
620 unsigned long long len = 0;
Christopher Fauletc16317d2018-12-12 14:11:22 +0100621
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200622 if (!htx)
623 return 0;
Christopher Fauletc16317d2018-12-12 14:11:22 +0100624
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200625 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
626 struct htx_blk *blk = htx_get_blk(htx, pos);
627 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Fauletc16317d2018-12-12 14:11:22 +0100628
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200629 if (type == HTX_BLK_EOM || type == HTX_BLK_TLR || type == HTX_BLK_EOT)
630 break;
631 if (type == HTX_BLK_DATA)
632 len += htx_get_blksz(blk);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200633 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200634
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200635 smp->data.type = SMP_T_SINT;
636 smp->data.u.sint = len;
637 smp->flags = SMP_F_VOL_TEST;
Willy Tarreau79e57332018-10-02 16:01:16 +0200638 return 1;
639}
640
641
642/* returns the advertised length of the body, or the advertised size of the
643 * chunks available in the buffer. This requires that the body has been waited
644 * for using http-buffer-request.
645 */
646static int smp_fetch_body_size(const struct arg *args, struct sample *smp, const char *kw, void *private)
647{
Christopher Faulete596d182020-05-05 17:46:34 +0200648 /* possible keywords: req.body_size, res.body_size */
649 struct channel *chn = ((kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Christopher Fauletf98e6262020-05-06 09:42:04 +0200650 struct check *check = ((kw[2] == 's') ? objt_check(smp->sess->origin) : NULL);
Christopher Faulet16032ab2020-04-30 11:30:00 +0200651 struct htx *htx = smp_prefetch_htx(smp, chn, check, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200652 int32_t pos;
653 unsigned long long len = 0;
Christopher Faulet89dc4992019-04-17 12:02:59 +0200654
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200655 if (!htx)
656 return 0;
Christopher Fauletc16317d2018-12-12 14:11:22 +0100657
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200658 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
659 struct htx_blk *blk = htx_get_blk(htx, pos);
660 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Fauletc16317d2018-12-12 14:11:22 +0100661
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200662 if (type == HTX_BLK_EOM || type == HTX_BLK_TLR || type == HTX_BLK_EOT)
663 break;
664 if (type == HTX_BLK_DATA)
665 len += htx_get_blksz(blk);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200666 }
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200667 if (htx->extra != ULLONG_MAX)
668 len += htx->extra;
Willy Tarreau79e57332018-10-02 16:01:16 +0200669
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200670 smp->data.type = SMP_T_SINT;
671 smp->data.u.sint = len;
672 smp->flags = SMP_F_VOL_TEST;
Willy Tarreau79e57332018-10-02 16:01:16 +0200673 return 1;
674}
675
676
677/* 4. Check on URL/URI. A pointer to the URI is stored. */
678static int smp_fetch_url(const struct arg *args, struct sample *smp, const char *kw, void *private)
679{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200680 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +0200681 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200682 struct htx_sl *sl;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200683
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200684 if (!htx)
685 return 0;
686 sl = http_get_stline(htx);
687 smp->data.type = SMP_T_STR;
688 smp->data.u.str.area = HTX_SL_REQ_UPTR(sl);
689 smp->data.u.str.data = HTX_SL_REQ_ULEN(sl);
690 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau79e57332018-10-02 16:01:16 +0200691 return 1;
692}
693
694static int smp_fetch_url_ip(const struct arg *args, struct sample *smp, const char *kw, void *private)
695{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200696 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +0200697 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200698 struct htx_sl *sl;
Willy Tarreau79e57332018-10-02 16:01:16 +0200699 struct sockaddr_storage addr;
700
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200701 if (!htx)
702 return 0;
703 sl = http_get_stline(htx);
704 url2sa(HTX_SL_REQ_UPTR(sl), HTX_SL_REQ_ULEN(sl), &addr, NULL);
Willy Tarreau79e57332018-10-02 16:01:16 +0200705
Willy Tarreau79e57332018-10-02 16:01:16 +0200706 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
707 return 0;
708
709 smp->data.type = SMP_T_IPV4;
710 smp->data.u.ipv4 = ((struct sockaddr_in *)&addr)->sin_addr;
711 smp->flags = 0;
712 return 1;
713}
714
715static int smp_fetch_url_port(const struct arg *args, struct sample *smp, const char *kw, void *private)
716{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200717 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +0200718 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200719 struct htx_sl *sl;
Willy Tarreau79e57332018-10-02 16:01:16 +0200720 struct sockaddr_storage addr;
721
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200722 if (!htx)
723 return 0;
724 sl = http_get_stline(htx);
725 url2sa(HTX_SL_REQ_UPTR(sl), HTX_SL_REQ_ULEN(sl), &addr, NULL);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200726
Willy Tarreau79e57332018-10-02 16:01:16 +0200727 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
728 return 0;
729
730 smp->data.type = SMP_T_SINT;
731 smp->data.u.sint = ntohs(((struct sockaddr_in *)&addr)->sin_port);
732 smp->flags = 0;
733 return 1;
734}
735
736/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
737 * Accepts an optional argument of type string containing the header field name,
738 * and an optional argument of type signed or unsigned integer to request an
739 * explicit occurrence of the header. Note that in the event of a missing name,
740 * headers are considered from the first one. It does not stop on commas and
741 * returns full lines instead (useful for User-Agent or Date for example).
742 */
743static int smp_fetch_fhdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
744{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200745 /* possible keywords: req.fhdr, res.fhdr */
746 struct channel *chn = ((kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Christopher Fauletf98e6262020-05-06 09:42:04 +0200747 struct check *check = ((kw[2] == 's') ? objt_check(smp->sess->origin) : NULL);
Christopher Faulet16032ab2020-04-30 11:30:00 +0200748 struct htx *htx = smp_prefetch_htx(smp, chn, check, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200749 struct http_hdr_ctx *ctx = smp->ctx.a[0];
750 struct ist name;
Willy Tarreau79e57332018-10-02 16:01:16 +0200751 int occ = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200752
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200753 if (!ctx) {
754 /* first call */
755 ctx = &static_http_hdr_ctx;
756 ctx->blk = NULL;
757 smp->ctx.a[0] = ctx;
758 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200759
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200760 if (args) {
761 if (args[0].type != ARGT_STR)
Willy Tarreau79e57332018-10-02 16:01:16 +0200762 return 0;
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200763 name.ptr = args[0].data.str.area;
764 name.len = args[0].data.str.data;
Willy Tarreau79e57332018-10-02 16:01:16 +0200765
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200766 if (args[1].type == ARGT_SINT)
767 occ = args[1].data.sint;
Willy Tarreau79e57332018-10-02 16:01:16 +0200768 }
769
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200770 if (!htx)
771 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200772
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200773 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
774 /* search for header from the beginning */
775 ctx->blk = NULL;
Willy Tarreau79e57332018-10-02 16:01:16 +0200776
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200777 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
778 /* no explicit occurrence and single fetch => last header by default */
779 occ = -1;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200780
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200781 if (!occ)
782 /* prepare to report multiple occurrences for ACL fetches */
783 smp->flags |= SMP_F_NOT_LAST;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200784
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200785 smp->data.type = SMP_T_STR;
786 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
787 if (http_get_htx_fhdr(htx, name, occ, ctx, &smp->data.u.str.area, &smp->data.u.str.data))
788 return 1;
Willy Tarreau79e57332018-10-02 16:01:16 +0200789 smp->flags &= ~SMP_F_NOT_LAST;
790 return 0;
791}
792
793/* 6. Check on HTTP header count. The number of occurrences is returned.
794 * Accepts exactly 1 argument of type string. It does not stop on commas and
795 * returns full lines instead (useful for User-Agent or Date for example).
796 */
797static int smp_fetch_fhdr_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
798{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200799 /* possible keywords: req.fhdr_cnt, res.fhdr_cnt */
800 struct channel *chn = ((kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Christopher Fauletf98e6262020-05-06 09:42:04 +0200801 struct check *check = ((kw[2] == 's') ? objt_check(smp->sess->origin) : NULL);
Christopher Faulet16032ab2020-04-30 11:30:00 +0200802 struct htx *htx = smp_prefetch_htx(smp, chn, check, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200803 struct http_hdr_ctx ctx;
804 struct ist name;
Willy Tarreau79e57332018-10-02 16:01:16 +0200805 int cnt;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200806
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200807 if (!htx)
808 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200809
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200810 if (args && args->type == ARGT_STR) {
811 name.ptr = args->data.str.area;
812 name.len = args->data.str.data;
813 } else {
814 name.ptr = NULL;
815 name.len = 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200816 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200817
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200818 ctx.blk = NULL;
819 cnt = 0;
820 while (http_find_header(htx, name, &ctx, 1))
821 cnt++;
Willy Tarreau79e57332018-10-02 16:01:16 +0200822 smp->data.type = SMP_T_SINT;
823 smp->data.u.sint = cnt;
824 smp->flags = SMP_F_VOL_HDR;
825 return 1;
826}
827
828static int smp_fetch_hdr_names(const struct arg *args, struct sample *smp, const char *kw, void *private)
829{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200830 /* possible keywords: req.hdr_names, res.hdr_names */
831 struct channel *chn = ((kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Christopher Fauletf98e6262020-05-06 09:42:04 +0200832 struct check *check = ((kw[2] == 's') ? objt_check(smp->sess->origin) : NULL);
Christopher Faulet16032ab2020-04-30 11:30:00 +0200833 struct htx *htx = smp_prefetch_htx(smp, chn, check, 1);
Willy Tarreau79e57332018-10-02 16:01:16 +0200834 struct buffer *temp;
835 char del = ',';
836
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200837 int32_t pos;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200838
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200839 if (!htx)
840 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200841
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200842 if (args && args->type == ARGT_STR)
843 del = *args[0].data.str.area;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200844
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200845 temp = get_trash_chunk();
846 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
847 struct htx_blk *blk = htx_get_blk(htx, pos);
848 enum htx_blk_type type = htx_get_blk_type(blk);
849 struct ist n;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200850
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200851 if (type == HTX_BLK_EOH)
852 break;
853 if (type != HTX_BLK_HDR)
854 continue;
855 n = htx_get_blk_name(htx, blk);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200856
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200857 if (temp->data)
858 temp->area[temp->data++] = del;
859 chunk_memcat(temp, n.ptr, n.len);
Willy Tarreau79e57332018-10-02 16:01:16 +0200860 }
861
862 smp->data.type = SMP_T_STR;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200863 smp->data.u.str = *temp;
Willy Tarreau79e57332018-10-02 16:01:16 +0200864 smp->flags = SMP_F_VOL_HDR;
865 return 1;
866}
867
868/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
869 * Accepts an optional argument of type string containing the header field name,
870 * and an optional argument of type signed or unsigned integer to request an
871 * explicit occurrence of the header. Note that in the event of a missing name,
872 * headers are considered from the first one.
873 */
874static int smp_fetch_hdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
875{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200876 /* possible keywords: req.hdr / hdr, res.hdr / shdr */
877 struct channel *chn = ((kw[0] == 'h' || kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Christopher Fauletf98e6262020-05-06 09:42:04 +0200878 struct check *check = ((kw[0] == 's' || kw[2] == 's') ? objt_check(smp->sess->origin) : NULL);
Christopher Faulet16032ab2020-04-30 11:30:00 +0200879 struct htx *htx = smp_prefetch_htx(smp, chn, check, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200880 struct http_hdr_ctx *ctx = smp->ctx.a[0];
881 struct ist name;
Willy Tarreau79e57332018-10-02 16:01:16 +0200882 int occ = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200883
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200884 if (!ctx) {
885 /* first call */
886 ctx = &static_http_hdr_ctx;
887 ctx->blk = NULL;
888 smp->ctx.a[0] = ctx;
889 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200890
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200891 if (args) {
892 if (args[0].type != ARGT_STR)
Willy Tarreau79e57332018-10-02 16:01:16 +0200893 return 0;
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200894 name.ptr = args[0].data.str.area;
895 name.len = args[0].data.str.data;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200896
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200897 if (args[1].type == ARGT_SINT)
898 occ = args[1].data.sint;
Willy Tarreau79e57332018-10-02 16:01:16 +0200899 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200900
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200901 if (!htx)
902 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200903
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200904 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
905 /* search for header from the beginning */
906 ctx->blk = NULL;
Willy Tarreau79e57332018-10-02 16:01:16 +0200907
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200908 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
909 /* no explicit occurrence and single fetch => last header by default */
910 occ = -1;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200911
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200912 if (!occ)
913 /* prepare to report multiple occurrences for ACL fetches */
914 smp->flags |= SMP_F_NOT_LAST;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200915
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200916 smp->data.type = SMP_T_STR;
917 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
918 if (http_get_htx_hdr(htx, name, occ, ctx, &smp->data.u.str.area, &smp->data.u.str.data))
919 return 1;
Willy Tarreau79e57332018-10-02 16:01:16 +0200920
921 smp->flags &= ~SMP_F_NOT_LAST;
922 return 0;
923}
924
Christopher Fauletc1f40dd2019-05-16 10:07:30 +0200925/* Same than smp_fetch_hdr() but only relies on the sample direction to choose
926 * the right channel. So instead of duplicating the code, we just change the
927 * keyword and then fallback on smp_fetch_hdr().
928 */
929static int smp_fetch_chn_hdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
930{
931 kw = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ ? "req.hdr" : "res.hdr");
932 return smp_fetch_hdr(args, smp, kw, private);
933}
934
Willy Tarreau79e57332018-10-02 16:01:16 +0200935/* 6. Check on HTTP header count. The number of occurrences is returned.
936 * Accepts exactly 1 argument of type string.
937 */
938static int smp_fetch_hdr_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
939{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200940 /* possible keywords: req.hdr_cnt / hdr_cnt, res.hdr_cnt / shdr_cnt */
941 struct channel *chn = ((kw[0] == 'h' || kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Christopher Fauletf98e6262020-05-06 09:42:04 +0200942 struct check *check = ((kw[0] == 's' || kw[2] == 's') ? objt_check(smp->sess->origin) : NULL);
Christopher Faulet16032ab2020-04-30 11:30:00 +0200943 struct htx *htx = smp_prefetch_htx(smp, chn, check, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200944 struct http_hdr_ctx ctx;
945 struct ist name;
Willy Tarreau79e57332018-10-02 16:01:16 +0200946 int cnt;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200947
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200948 if (!htx)
949 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200950
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200951 if (args && args->type == ARGT_STR) {
952 name.ptr = args->data.str.area;
953 name.len = args->data.str.data;
954 } else {
955 name.ptr = NULL;
956 name.len = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200957 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200958
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200959 ctx.blk = NULL;
960 cnt = 0;
961 while (http_find_header(htx, name, &ctx, 0))
962 cnt++;
Willy Tarreau79e57332018-10-02 16:01:16 +0200963
964 smp->data.type = SMP_T_SINT;
965 smp->data.u.sint = cnt;
966 smp->flags = SMP_F_VOL_HDR;
967 return 1;
968}
969
970/* Fetch an HTTP header's integer value. The integer value is returned. It
971 * takes a mandatory argument of type string and an optional one of type int
972 * to designate a specific occurrence. It returns an unsigned integer, which
973 * may or may not be appropriate for everything.
974 */
975static int smp_fetch_hdr_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
976{
977 int ret = smp_fetch_hdr(args, smp, kw, private);
978
979 if (ret > 0) {
980 smp->data.type = SMP_T_SINT;
981 smp->data.u.sint = strl2ic(smp->data.u.str.area,
982 smp->data.u.str.data);
983 }
984
985 return ret;
986}
987
988/* Fetch an HTTP header's IP value. takes a mandatory argument of type string
989 * and an optional one of type int to designate a specific occurrence.
990 * It returns an IPv4 or IPv6 address.
991 */
992static int smp_fetch_hdr_ip(const struct arg *args, struct sample *smp, const char *kw, void *private)
993{
994 int ret;
995
996 while ((ret = smp_fetch_hdr(args, smp, kw, private)) > 0) {
997 if (url2ipv4((char *) smp->data.u.str.area, &smp->data.u.ipv4)) {
998 smp->data.type = SMP_T_IPV4;
999 break;
1000 } else {
1001 struct buffer *temp = get_trash_chunk();
1002 if (smp->data.u.str.data < temp->size - 1) {
1003 memcpy(temp->area, smp->data.u.str.area,
1004 smp->data.u.str.data);
1005 temp->area[smp->data.u.str.data] = '\0';
1006 if (inet_pton(AF_INET6, temp->area, &smp->data.u.ipv6)) {
1007 smp->data.type = SMP_T_IPV6;
1008 break;
1009 }
1010 }
1011 }
1012
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001013 /* if the header doesn't match an IP address, fetch next one */
1014 if (!(smp->flags & SMP_F_NOT_LAST))
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001015 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001016 }
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001017 return ret;
1018}
Willy Tarreau79e57332018-10-02 16:01:16 +02001019
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001020/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
1021 * the first '/' after the possible hostname, and ends before the possible '?'.
1022 */
1023static int smp_fetch_path(const struct arg *args, struct sample *smp, const char *kw, void *private)
1024{
1025 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +02001026 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001027 struct htx_sl *sl;
1028 struct ist path;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001029
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001030 if (!htx)
1031 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001032
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001033 sl = http_get_stline(htx);
Jerome Magnin4fb196c2020-02-21 10:49:12 +01001034 path = iststop(http_get_path(htx_sl_req_uri(sl)), '?');
Tim Duesterhused526372020-03-05 17:56:33 +01001035 if (!isttest(path))
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001036 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001037
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001038 /* OK, we got the '/' ! */
1039 smp->data.type = SMP_T_STR;
1040 smp->data.u.str.area = path.ptr;
Jerome Magnin4fb196c2020-02-21 10:49:12 +01001041 smp->data.u.str.data = path.len;
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001042 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau79e57332018-10-02 16:01:16 +02001043 return 1;
1044}
1045
1046/* This produces a concatenation of the first occurrence of the Host header
1047 * followed by the path component if it begins with a slash ('/'). This means
1048 * that '*' will not be added, resulting in exactly the first Host entry.
1049 * If no Host header is found, then the path is returned as-is. The returned
1050 * value is stored in the trash so it does not need to be marked constant.
1051 * The returned sample is of type string.
1052 */
1053static int smp_fetch_base(const struct arg *args, struct sample *smp, const char *kw, void *private)
1054{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001055 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +02001056 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001057 struct htx_sl *sl;
Willy Tarreau79e57332018-10-02 16:01:16 +02001058 struct buffer *temp;
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001059 struct http_hdr_ctx ctx;
1060 struct ist path;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001061
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001062 if (!htx)
1063 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001064
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001065 ctx.blk = NULL;
1066 if (!http_find_header(htx, ist("Host"), &ctx, 0) || !ctx.value.len)
1067 return smp_fetch_path(args, smp, kw, private);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001068
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001069 /* OK we have the header value in ctx.value */
1070 temp = get_trash_chunk();
1071 chunk_memcat(temp, ctx.value.ptr, ctx.value.len);
Willy Tarreau79e57332018-10-02 16:01:16 +02001072
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001073 /* now retrieve the path */
1074 sl = http_get_stline(htx);
1075 path = http_get_path(htx_sl_req_uri(sl));
Tim Duesterhused526372020-03-05 17:56:33 +01001076 if (isttest(path)) {
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001077 size_t len;
Willy Tarreau79e57332018-10-02 16:01:16 +02001078
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001079 for (len = 0; len < path.len && *(path.ptr + len) != '?'; len++)
1080 ;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001081
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001082 if (len && *(path.ptr) == '/')
1083 chunk_memcat(temp, path.ptr, len);
Willy Tarreau79e57332018-10-02 16:01:16 +02001084 }
1085
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001086 smp->data.type = SMP_T_STR;
1087 smp->data.u.str = *temp;
Willy Tarreau79e57332018-10-02 16:01:16 +02001088 smp->flags = SMP_F_VOL_1ST;
1089 return 1;
1090}
1091
1092/* This produces a 32-bit hash of the concatenation of the first occurrence of
1093 * the Host header followed by the path component if it begins with a slash ('/').
1094 * This means that '*' will not be added, resulting in exactly the first Host
1095 * entry. If no Host header is found, then the path is used. The resulting value
1096 * is hashed using the path hash followed by a full avalanche hash and provides a
1097 * 32-bit integer value. This fetch is useful for tracking per-path activity on
1098 * high-traffic sites without having to store whole paths.
1099 */
1100static int smp_fetch_base32(const struct arg *args, struct sample *smp, const char *kw, void *private)
1101{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001102 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +02001103 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001104 struct htx_sl *sl;
1105 struct http_hdr_ctx ctx;
1106 struct ist path;
Willy Tarreau79e57332018-10-02 16:01:16 +02001107 unsigned int hash = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001108
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001109 if (!htx)
1110 return 0;
Dragan Dosen8861e1c2019-02-12 19:50:31 +01001111
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001112 ctx.blk = NULL;
1113 if (http_find_header(htx, ist("Host"), &ctx, 0)) {
1114 /* OK we have the header value in ctx.value */
1115 while (ctx.value.len--)
1116 hash = *(ctx.value.ptr++) + (hash << 6) + (hash << 16) - hash;
Willy Tarreau79e57332018-10-02 16:01:16 +02001117 }
1118
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001119 /* now retrieve the path */
1120 sl = http_get_stline(htx);
1121 path = http_get_path(htx_sl_req_uri(sl));
Tim Duesterhused526372020-03-05 17:56:33 +01001122 if (isttest(path)) {
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001123 size_t len;
Willy Tarreau79e57332018-10-02 16:01:16 +02001124
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001125 for (len = 0; len < path.len && *(path.ptr + len) != '?'; len++)
1126 ;
Willy Tarreau79e57332018-10-02 16:01:16 +02001127
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001128 if (len && *(path.ptr) == '/') {
1129 while (len--)
1130 hash = *(path.ptr++) + (hash << 6) + (hash << 16) - hash;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001131 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001132 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001133
Willy Tarreau79e57332018-10-02 16:01:16 +02001134 hash = full_hash(hash);
1135
1136 smp->data.type = SMP_T_SINT;
1137 smp->data.u.sint = hash;
1138 smp->flags = SMP_F_VOL_1ST;
1139 return 1;
1140}
1141
1142/* This concatenates the source address with the 32-bit hash of the Host and
1143 * path as returned by smp_fetch_base32(). The idea is to have per-source and
1144 * per-path counters. The result is a binary block from 8 to 20 bytes depending
1145 * on the source address length. The path hash is stored before the address so
1146 * that in environments where IPv6 is insignificant, truncating the output to
1147 * 8 bytes would still work.
1148 */
1149static int smp_fetch_base32_src(const struct arg *args, struct sample *smp, const char *kw, void *private)
1150{
1151 struct buffer *temp;
1152 struct connection *cli_conn = objt_conn(smp->sess->origin);
1153
Willy Tarreaucd7ca792019-07-17 16:57:03 +02001154 if (!cli_conn || !conn_get_src(cli_conn))
Willy Tarreau79e57332018-10-02 16:01:16 +02001155 return 0;
1156
1157 if (!smp_fetch_base32(args, smp, kw, private))
1158 return 0;
1159
1160 temp = get_trash_chunk();
1161 *(unsigned int *) temp->area = htonl(smp->data.u.sint);
1162 temp->data += sizeof(unsigned int);
1163
Willy Tarreau9a1efe12019-07-17 17:13:50 +02001164 switch (cli_conn->src->ss_family) {
Willy Tarreau79e57332018-10-02 16:01:16 +02001165 case AF_INET:
1166 memcpy(temp->area + temp->data,
Willy Tarreau9a1efe12019-07-17 17:13:50 +02001167 &((struct sockaddr_in *)cli_conn->src)->sin_addr,
Willy Tarreau79e57332018-10-02 16:01:16 +02001168 4);
1169 temp->data += 4;
1170 break;
1171 case AF_INET6:
1172 memcpy(temp->area + temp->data,
Willy Tarreau9a1efe12019-07-17 17:13:50 +02001173 &((struct sockaddr_in6 *)cli_conn->src)->sin6_addr,
Willy Tarreau79e57332018-10-02 16:01:16 +02001174 16);
1175 temp->data += 16;
1176 break;
1177 default:
1178 return 0;
1179 }
1180
1181 smp->data.u.str = *temp;
1182 smp->data.type = SMP_T_BIN;
1183 return 1;
1184}
1185
1186/* Extracts the query string, which comes after the question mark '?'. If no
1187 * question mark is found, nothing is returned. Otherwise it returns a sample
1188 * of type string carrying the whole query string.
1189 */
1190static int smp_fetch_query(const struct arg *args, struct sample *smp, const char *kw, void *private)
1191{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001192 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +02001193 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001194 struct htx_sl *sl;
Willy Tarreau79e57332018-10-02 16:01:16 +02001195 char *ptr, *end;
1196
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001197 if (!htx)
1198 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001199
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001200 sl = http_get_stline(htx);
1201 ptr = HTX_SL_REQ_UPTR(sl);
1202 end = HTX_SL_REQ_UPTR(sl) + HTX_SL_REQ_ULEN(sl);
Willy Tarreau79e57332018-10-02 16:01:16 +02001203
1204 /* look up the '?' */
1205 do {
1206 if (ptr == end)
1207 return 0;
1208 } while (*ptr++ != '?');
1209
1210 smp->data.type = SMP_T_STR;
1211 smp->data.u.str.area = ptr;
1212 smp->data.u.str.data = end - ptr;
1213 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
1214 return 1;
1215}
1216
1217static int smp_fetch_proto_http(const struct arg *args, struct sample *smp, const char *kw, void *private)
1218{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001219 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +02001220 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 0);
Willy Tarreau79e57332018-10-02 16:01:16 +02001221
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001222 if (!htx)
1223 return 0;
1224 smp->data.type = SMP_T_BOOL;
Willy Tarreau79e57332018-10-02 16:01:16 +02001225 smp->data.u.sint = 1;
1226 return 1;
1227}
1228
1229/* return a valid test if the current request is the first one on the connection */
1230static int smp_fetch_http_first_req(const struct arg *args, struct sample *smp, const char *kw, void *private)
1231{
Willy Tarreau79512b62020-04-29 11:52:13 +02001232 if (!smp->strm)
1233 return 0;
1234
Willy Tarreau79e57332018-10-02 16:01:16 +02001235 smp->data.type = SMP_T_BOOL;
1236 smp->data.u.sint = !(smp->strm->txn->flags & TX_NOT_FIRST);
1237 return 1;
1238}
1239
Christopher Fauleta4063562019-08-02 11:51:37 +02001240/* Fetch the authentication method if there is an Authorization header. It
1241 * relies on get_http_auth()
1242 */
1243static int smp_fetch_http_auth_type(const struct arg *args, struct sample *smp, const char *kw, void *private)
1244{
1245 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +02001246 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Fauleta4063562019-08-02 11:51:37 +02001247 struct http_txn *txn;
1248
1249 if (!htx)
1250 return 0;
1251
1252 txn = smp->strm->txn;
1253 if (!get_http_auth(smp, htx))
1254 return 0;
1255
1256 switch (txn->auth.method) {
1257 case HTTP_AUTH_BASIC:
1258 smp->data.u.str.area = "Basic";
1259 smp->data.u.str.data = 5;
1260 break;
1261 case HTTP_AUTH_DIGEST:
1262 /* Unexpected because not supported */
1263 smp->data.u.str.area = "Digest";
1264 smp->data.u.str.data = 6;
1265 break;
1266 default:
1267 return 0;
1268 }
1269
1270 smp->data.type = SMP_T_STR;
1271 smp->flags = SMP_F_CONST;
1272 return 1;
1273}
1274
1275/* Fetch the user supplied if there is an Authorization header. It relies on
1276 * get_http_auth()
1277 */
1278static int smp_fetch_http_auth_user(const struct arg *args, struct sample *smp, const char *kw, void *private)
1279{
1280 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +02001281 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Fauleta4063562019-08-02 11:51:37 +02001282 struct http_txn *txn;
1283
1284 if (!htx)
1285 return 0;
1286
1287 txn = smp->strm->txn;
1288 if (!get_http_auth(smp, htx))
1289 return 0;
1290
1291 smp->data.type = SMP_T_STR;
1292 smp->data.u.str.area = txn->auth.user;
1293 smp->data.u.str.data = strlen(txn->auth.user);
1294 smp->flags = SMP_F_CONST;
1295 return 1;
1296}
1297
1298/* Fetch the password supplied if there is an Authorization header. It relies on
1299 * get_http_auth()
1300 */
1301static int smp_fetch_http_auth_pass(const struct arg *args, struct sample *smp, const char *kw, void *private)
1302{
1303 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +02001304 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Fauleta4063562019-08-02 11:51:37 +02001305 struct http_txn *txn;
1306
1307 if (!htx)
1308 return 0;
1309
1310 txn = smp->strm->txn;
1311 if (!get_http_auth(smp, htx))
1312 return 0;
1313
1314 smp->data.type = SMP_T_STR;
1315 smp->data.u.str.area = txn->auth.pass;
1316 smp->data.u.str.data = strlen(txn->auth.pass);
1317 smp->flags = SMP_F_CONST;
1318 return 1;
1319}
1320
Willy Tarreau79e57332018-10-02 16:01:16 +02001321/* Accepts exactly 1 argument of type userlist */
1322static int smp_fetch_http_auth(const struct arg *args, struct sample *smp, const char *kw, void *private)
1323{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001324 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +02001325 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Willy Tarreau79e57332018-10-02 16:01:16 +02001326
1327 if (!args || args->type != ARGT_USR)
1328 return 0;
1329
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001330 if (!htx)
1331 return 0;
1332 if (!get_http_auth(smp, htx))
1333 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001334
1335 smp->data.type = SMP_T_BOOL;
1336 smp->data.u.sint = check_user(args->data.usr, smp->strm->txn->auth.user,
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001337 smp->strm->txn->auth.pass);
Willy Tarreau79e57332018-10-02 16:01:16 +02001338 return 1;
1339}
1340
1341/* Accepts exactly 1 argument of type userlist */
1342static int smp_fetch_http_auth_grp(const struct arg *args, struct sample *smp, const char *kw, void *private)
1343{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001344 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +02001345 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Faulet89dc4992019-04-17 12:02:59 +02001346
Willy Tarreau79e57332018-10-02 16:01:16 +02001347 if (!args || args->type != ARGT_USR)
1348 return 0;
1349
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001350 if (!htx)
1351 return 0;
1352 if (!get_http_auth(smp, htx))
1353 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001354
Willy Tarreau79e57332018-10-02 16:01:16 +02001355 /* if the user does not belong to the userlist or has a wrong password,
1356 * report that it unconditionally does not match. Otherwise we return
1357 * a string containing the username.
1358 */
1359 if (!check_user(args->data.usr, smp->strm->txn->auth.user,
1360 smp->strm->txn->auth.pass))
1361 return 0;
1362
1363 /* pat_match_auth() will need the user list */
1364 smp->ctx.a[0] = args->data.usr;
1365
1366 smp->data.type = SMP_T_STR;
1367 smp->flags = SMP_F_CONST;
1368 smp->data.u.str.area = smp->strm->txn->auth.user;
1369 smp->data.u.str.data = strlen(smp->strm->txn->auth.user);
1370
1371 return 1;
1372}
1373
1374/* Fetch a captured HTTP request header. The index is the position of
1375 * the "capture" option in the configuration file
1376 */
1377static int smp_fetch_capture_req_hdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
1378{
Willy Tarreau0898c2d2020-04-29 11:44:54 +02001379 struct proxy *fe;
Willy Tarreau79e57332018-10-02 16:01:16 +02001380 int idx;
1381
1382 if (!args || args->type != ARGT_SINT)
1383 return 0;
1384
Willy Tarreau0898c2d2020-04-29 11:44:54 +02001385 if (!smp->strm)
1386 return 0;
1387
1388 fe = strm_fe(smp->strm);
Willy Tarreau79e57332018-10-02 16:01:16 +02001389 idx = args->data.sint;
1390
1391 if (idx > (fe->nb_req_cap - 1) || smp->strm->req_cap == NULL || smp->strm->req_cap[idx] == NULL)
1392 return 0;
1393
1394 smp->data.type = SMP_T_STR;
1395 smp->flags |= SMP_F_CONST;
1396 smp->data.u.str.area = smp->strm->req_cap[idx];
1397 smp->data.u.str.data = strlen(smp->strm->req_cap[idx]);
1398
1399 return 1;
1400}
1401
1402/* Fetch a captured HTTP response header. The index is the position of
1403 * the "capture" option in the configuration file
1404 */
1405static int smp_fetch_capture_res_hdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
1406{
Willy Tarreau0898c2d2020-04-29 11:44:54 +02001407 struct proxy *fe;
Willy Tarreau79e57332018-10-02 16:01:16 +02001408 int idx;
1409
1410 if (!args || args->type != ARGT_SINT)
1411 return 0;
1412
Willy Tarreau0898c2d2020-04-29 11:44:54 +02001413 if (!smp->strm)
1414 return 0;
1415
1416 fe = strm_fe(smp->strm);
Willy Tarreau79e57332018-10-02 16:01:16 +02001417 idx = args->data.sint;
1418
1419 if (idx > (fe->nb_rsp_cap - 1) || smp->strm->res_cap == NULL || smp->strm->res_cap[idx] == NULL)
1420 return 0;
1421
1422 smp->data.type = SMP_T_STR;
1423 smp->flags |= SMP_F_CONST;
1424 smp->data.u.str.area = smp->strm->res_cap[idx];
1425 smp->data.u.str.data = strlen(smp->strm->res_cap[idx]);
1426
1427 return 1;
1428}
1429
1430/* Extracts the METHOD in the HTTP request, the txn->uri should be filled before the call */
1431static int smp_fetch_capture_req_method(const struct arg *args, struct sample *smp, const char *kw, void *private)
1432{
1433 struct buffer *temp;
Willy Tarreau0898c2d2020-04-29 11:44:54 +02001434 struct http_txn *txn;
Willy Tarreau79e57332018-10-02 16:01:16 +02001435 char *ptr;
1436
Willy Tarreau0898c2d2020-04-29 11:44:54 +02001437 if (!smp->strm)
1438 return 0;
1439
1440 txn = smp->strm->txn;
Willy Tarreau79e57332018-10-02 16:01:16 +02001441 if (!txn || !txn->uri)
1442 return 0;
1443
1444 ptr = txn->uri;
1445
1446 while (*ptr != ' ' && *ptr != '\0') /* find first space */
1447 ptr++;
1448
1449 temp = get_trash_chunk();
1450 temp->area = txn->uri;
1451 temp->data = ptr - txn->uri;
1452 smp->data.u.str = *temp;
1453 smp->data.type = SMP_T_STR;
1454 smp->flags = SMP_F_CONST;
1455
1456 return 1;
1457
1458}
1459
1460/* Extracts the path in the HTTP request, the txn->uri should be filled before the call */
1461static int smp_fetch_capture_req_uri(const struct arg *args, struct sample *smp, const char *kw, void *private)
1462{
Willy Tarreau0898c2d2020-04-29 11:44:54 +02001463 struct http_txn *txn;
Willy Tarreau79e57332018-10-02 16:01:16 +02001464 struct ist path;
1465 const char *ptr;
1466
Willy Tarreau0898c2d2020-04-29 11:44:54 +02001467 if (!smp->strm)
1468 return 0;
1469
1470 txn = smp->strm->txn;
Willy Tarreau79e57332018-10-02 16:01:16 +02001471 if (!txn || !txn->uri)
1472 return 0;
1473
1474 ptr = txn->uri;
1475
1476 while (*ptr != ' ' && *ptr != '\0') /* find first space */
1477 ptr++;
1478
1479 if (!*ptr)
1480 return 0;
1481
Christopher Faulet78337bb2018-11-15 14:35:18 +01001482 /* skip the first space and find space after URI */
1483 path = ist2(++ptr, 0);
1484 while (*ptr != ' ' && *ptr != '\0')
1485 ptr++;
1486 path.len = ptr - path.ptr;
Willy Tarreau79e57332018-10-02 16:01:16 +02001487
Christopher Faulet78337bb2018-11-15 14:35:18 +01001488 path = http_get_path(path);
Tim Duesterhused526372020-03-05 17:56:33 +01001489 if (!isttest(path))
Willy Tarreau79e57332018-10-02 16:01:16 +02001490 return 0;
1491
1492 smp->data.u.str.area = path.ptr;
1493 smp->data.u.str.data = path.len;
1494 smp->data.type = SMP_T_STR;
1495 smp->flags = SMP_F_CONST;
1496
1497 return 1;
1498}
1499
1500/* Retrieves the HTTP version from the request (either 1.0 or 1.1) and emits it
1501 * as a string (either "HTTP/1.0" or "HTTP/1.1").
1502 */
1503static int smp_fetch_capture_req_ver(const struct arg *args, struct sample *smp, const char *kw, void *private)
1504{
Willy Tarreau0898c2d2020-04-29 11:44:54 +02001505 struct http_txn *txn;
1506
1507 if (!smp->strm)
1508 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001509
Willy Tarreau0898c2d2020-04-29 11:44:54 +02001510 txn = smp->strm->txn;
Christopher Faulet711ed6a2019-07-16 14:16:10 +02001511 if (!txn || txn->req.msg_state >= HTTP_MSG_BODY)
Willy Tarreau79e57332018-10-02 16:01:16 +02001512 return 0;
1513
1514 if (txn->req.flags & HTTP_MSGF_VER_11)
1515 smp->data.u.str.area = "HTTP/1.1";
1516 else
1517 smp->data.u.str.area = "HTTP/1.0";
1518
1519 smp->data.u.str.data = 8;
1520 smp->data.type = SMP_T_STR;
1521 smp->flags = SMP_F_CONST;
1522 return 1;
1523
1524}
1525
1526/* Retrieves the HTTP version from the response (either 1.0 or 1.1) and emits it
1527 * as a string (either "HTTP/1.0" or "HTTP/1.1").
1528 */
1529static int smp_fetch_capture_res_ver(const struct arg *args, struct sample *smp, const char *kw, void *private)
1530{
Willy Tarreau0898c2d2020-04-29 11:44:54 +02001531 struct http_txn *txn;
1532
1533 if (!smp->strm)
1534 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001535
Willy Tarreau0898c2d2020-04-29 11:44:54 +02001536 txn = smp->strm->txn;
Christopher Faulet711ed6a2019-07-16 14:16:10 +02001537 if (!txn || txn->rsp.msg_state >= HTTP_MSG_BODY)
Willy Tarreau79e57332018-10-02 16:01:16 +02001538 return 0;
1539
1540 if (txn->rsp.flags & HTTP_MSGF_VER_11)
1541 smp->data.u.str.area = "HTTP/1.1";
1542 else
1543 smp->data.u.str.area = "HTTP/1.0";
1544
1545 smp->data.u.str.data = 8;
1546 smp->data.type = SMP_T_STR;
1547 smp->flags = SMP_F_CONST;
1548 return 1;
1549
1550}
1551
1552/* Iterate over all cookies present in a message. The context is stored in
1553 * smp->ctx.a[0] for the in-header position, smp->ctx.a[1] for the
1554 * end-of-header-value, and smp->ctx.a[2] for the hdr_ctx. Depending on
1555 * the direction, multiple cookies may be parsed on the same line or not.
1556 * The cookie name is in args and the name length in args->data.str.len.
1557 * Accepts exactly 1 argument of type string. If the input options indicate
1558 * that no iterating is desired, then only last value is fetched if any.
1559 * The returned sample is of type CSTR. Can be used to parse cookies in other
1560 * files.
1561 */
1562static int smp_fetch_cookie(const struct arg *args, struct sample *smp, const char *kw, void *private)
1563{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001564 /* possible keywords: req.cookie / cookie / cook, res.cookie / scook / set-cookie */
1565 struct channel *chn = ((kw[0] == 'c' || kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Christopher Fauletf98e6262020-05-06 09:42:04 +02001566 struct check *check = ((kw[0] == 's' || kw[2] == 's') ? objt_check(smp->sess->origin) : NULL);
Christopher Faulet16032ab2020-04-30 11:30:00 +02001567 struct htx *htx = smp_prefetch_htx(smp, chn, check, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001568 struct http_hdr_ctx *ctx = smp->ctx.a[2];
1569 struct ist hdr;
Willy Tarreau79e57332018-10-02 16:01:16 +02001570 int occ = 0;
1571 int found = 0;
1572
1573 if (!args || args->type != ARGT_STR)
1574 return 0;
1575
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001576 if (!ctx) {
1577 /* first call */
1578 ctx = &static_http_hdr_ctx;
1579 ctx->blk = NULL;
1580 smp->ctx.a[2] = ctx;
1581 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001582
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001583 if (!htx)
1584 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001585
Christopher Faulet16032ab2020-04-30 11:30:00 +02001586 hdr = (!(check || (chn && chn->flags & CF_ISRESP)) ? ist("Cookie") : ist("Set-Cookie"));
Willy Tarreau79e57332018-10-02 16:01:16 +02001587
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001588 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
1589 /* no explicit occurrence and single fetch => last cookie by default */
1590 occ = -1;
Willy Tarreau79e57332018-10-02 16:01:16 +02001591
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001592 /* OK so basically here, either we want only one value and it's the
1593 * last one, or we want to iterate over all of them and we fetch the
1594 * next one.
1595 */
Willy Tarreau79e57332018-10-02 16:01:16 +02001596
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001597 if (!(smp->flags & SMP_F_NOT_LAST)) {
1598 /* search for the header from the beginning, we must first initialize
1599 * the search parameters.
Willy Tarreau79e57332018-10-02 16:01:16 +02001600 */
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001601 smp->ctx.a[0] = NULL;
1602 ctx->blk = NULL;
Willy Tarreau79e57332018-10-02 16:01:16 +02001603 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001604
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001605 smp->flags |= SMP_F_VOL_HDR;
1606 while (1) {
1607 /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */
1608 if (!smp->ctx.a[0]) {
1609 if (!http_find_header(htx, hdr, ctx, 0))
1610 goto out;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001611
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001612 if (ctx->value.len < args->data.str.data + 1)
1613 continue;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001614
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001615 smp->ctx.a[0] = ctx->value.ptr;
1616 smp->ctx.a[1] = smp->ctx.a[0] + ctx->value.len;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001617 }
1618
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001619 smp->data.type = SMP_T_STR;
1620 smp->flags |= SMP_F_CONST;
1621 smp->ctx.a[0] = http_extract_cookie_value(smp->ctx.a[0], smp->ctx.a[1],
1622 args->data.str.area, args->data.str.data,
1623 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
1624 &smp->data.u.str.area,
1625 &smp->data.u.str.data);
1626 if (smp->ctx.a[0]) {
1627 found = 1;
1628 if (occ >= 0) {
1629 /* one value was returned into smp->data.u.str.{str,len} */
1630 smp->flags |= SMP_F_NOT_LAST;
1631 return 1;
Willy Tarreau79e57332018-10-02 16:01:16 +02001632 }
1633 }
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001634 /* if we're looking for last occurrence, let's loop */
Willy Tarreau79e57332018-10-02 16:01:16 +02001635 }
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001636
Willy Tarreau79e57332018-10-02 16:01:16 +02001637 /* all cookie headers and values were scanned. If we're looking for the
1638 * last occurrence, we may return it now.
1639 */
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001640 out:
Willy Tarreau79e57332018-10-02 16:01:16 +02001641 smp->flags &= ~SMP_F_NOT_LAST;
1642 return found;
1643}
1644
Christopher Fauletc1f40dd2019-05-16 10:07:30 +02001645/* Same than smp_fetch_cookie() but only relies on the sample direction to
1646 * choose the right channel. So instead of duplicating the code, we just change
1647 * the keyword and then fallback on smp_fetch_cookie().
1648 */
1649static int smp_fetch_chn_cookie(const struct arg *args, struct sample *smp, const char *kw, void *private)
1650{
1651 kw = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ ? "req.cook" : "res.cook");
1652 return smp_fetch_cookie(args, smp, kw, private);
1653}
1654
Willy Tarreau79e57332018-10-02 16:01:16 +02001655/* Iterate over all cookies present in a request to count how many occurrences
1656 * match the name in args and args->data.str.len. If <multi> is non-null, then
1657 * multiple cookies may be parsed on the same line. The returned sample is of
1658 * type UINT. Accepts exactly 1 argument of type string.
1659 */
1660static int smp_fetch_cookie_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
1661{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001662 /* possible keywords: req.cook_cnt / cook_cnt, res.cook_cnt / scook_cnt */
1663 struct channel *chn = ((kw[0] == 'c' || kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Christopher Fauletf98e6262020-05-06 09:42:04 +02001664 struct check *check = ((kw[0] == 's' || kw[2] == 's') ? objt_check(smp->sess->origin) : NULL);
Christopher Faulet16032ab2020-04-30 11:30:00 +02001665 struct htx *htx = smp_prefetch_htx(smp, chn, check, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001666 struct http_hdr_ctx ctx;
1667 struct ist hdr;
Willy Tarreau79e57332018-10-02 16:01:16 +02001668 char *val_beg, *val_end;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001669 int cnt;
Willy Tarreau79e57332018-10-02 16:01:16 +02001670
1671 if (!args || args->type != ARGT_STR)
1672 return 0;
1673
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001674 if (!htx)
1675 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001676
Christopher Faulet16032ab2020-04-30 11:30:00 +02001677 hdr = (!(check || (chn && chn->flags & CF_ISRESP)) ? ist("Cookie") : ist("Set-Cookie"));
Willy Tarreau79e57332018-10-02 16:01:16 +02001678
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001679 val_end = val_beg = NULL;
1680 ctx.blk = NULL;
1681 cnt = 0;
1682 while (1) {
1683 /* Note: val_beg == NULL every time we need to fetch a new header */
1684 if (!val_beg) {
1685 if (!http_find_header(htx, hdr, &ctx, 0))
1686 break;
Willy Tarreau79e57332018-10-02 16:01:16 +02001687
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001688 if (ctx.value.len < args->data.str.data + 1)
1689 continue;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001690
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001691 val_beg = ctx.value.ptr;
1692 val_end = val_beg + ctx.value.len;
Willy Tarreau79e57332018-10-02 16:01:16 +02001693 }
1694
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001695 smp->data.type = SMP_T_STR;
1696 smp->flags |= SMP_F_CONST;
1697 while ((val_beg = http_extract_cookie_value(val_beg, val_end,
1698 args->data.str.area, args->data.str.data,
1699 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
1700 &smp->data.u.str.area,
1701 &smp->data.u.str.data))) {
1702 cnt++;
Willy Tarreau79e57332018-10-02 16:01:16 +02001703 }
1704 }
1705
1706 smp->data.type = SMP_T_SINT;
1707 smp->data.u.sint = cnt;
1708 smp->flags |= SMP_F_VOL_HDR;
1709 return 1;
1710}
1711
1712/* Fetch an cookie's integer value. The integer value is returned. It
1713 * takes a mandatory argument of type string. It relies on smp_fetch_cookie().
1714 */
1715static int smp_fetch_cookie_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
1716{
1717 int ret = smp_fetch_cookie(args, smp, kw, private);
1718
1719 if (ret > 0) {
1720 smp->data.type = SMP_T_SINT;
1721 smp->data.u.sint = strl2ic(smp->data.u.str.area,
1722 smp->data.u.str.data);
1723 }
1724
1725 return ret;
1726}
1727
1728/************************************************************************/
1729/* The code below is dedicated to sample fetches */
1730/************************************************************************/
1731
1732/* This scans a URL-encoded query string. It takes an optionally wrapping
1733 * string whose first contigous chunk has its beginning in ctx->a[0] and end
1734 * in ctx->a[1], and the optional second part in (ctx->a[2]..ctx->a[3]). The
1735 * pointers are updated for next iteration before leaving.
1736 */
1737static int smp_fetch_param(char delim, const char *name, int name_len, const struct arg *args, struct sample *smp, const char *kw, void *private)
1738{
1739 const char *vstart, *vend;
1740 struct buffer *temp;
1741 const char **chunks = (const char **)smp->ctx.a;
1742
1743 if (!http_find_next_url_param(chunks, name, name_len,
1744 &vstart, &vend, delim))
1745 return 0;
1746
1747 /* Create sample. If the value is contiguous, return the pointer as CONST,
1748 * if the value is wrapped, copy-it in a buffer.
1749 */
1750 smp->data.type = SMP_T_STR;
1751 if (chunks[2] &&
1752 vstart >= chunks[0] && vstart <= chunks[1] &&
1753 vend >= chunks[2] && vend <= chunks[3]) {
1754 /* Wrapped case. */
1755 temp = get_trash_chunk();
1756 memcpy(temp->area, vstart, chunks[1] - vstart);
1757 memcpy(temp->area + ( chunks[1] - vstart ), chunks[2],
1758 vend - chunks[2]);
1759 smp->data.u.str.area = temp->area;
1760 smp->data.u.str.data = ( chunks[1] - vstart ) + ( vend - chunks[2] );
1761 } else {
1762 /* Contiguous case. */
1763 smp->data.u.str.area = (char *)vstart;
1764 smp->data.u.str.data = vend - vstart;
1765 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
1766 }
1767
1768 /* Update context, check wrapping. */
1769 chunks[0] = vend;
1770 if (chunks[2] && vend >= chunks[2] && vend <= chunks[3]) {
1771 chunks[1] = chunks[3];
1772 chunks[2] = NULL;
1773 }
1774
1775 if (chunks[0] < chunks[1])
1776 smp->flags |= SMP_F_NOT_LAST;
1777
1778 return 1;
1779}
1780
1781/* This function iterates over each parameter of the query string. It uses
1782 * ctx->a[0] and ctx->a[1] to store the beginning and end of the current
1783 * parameter. Since it uses smp_fetch_param(), ctx->a[2..3] are both NULL.
1784 * An optional parameter name is passed in args[0], otherwise any parameter is
1785 * considered. It supports an optional delimiter argument for the beginning of
1786 * the string in args[1], which defaults to "?".
1787 */
1788static int smp_fetch_url_param(const struct arg *args, struct sample *smp, const char *kw, void *private)
1789{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001790 struct channel *chn = SMP_REQ_CHN(smp);
Willy Tarreau79e57332018-10-02 16:01:16 +02001791 char delim = '?';
1792 const char *name;
1793 int name_len;
1794
1795 if (!args ||
1796 (args[0].type && args[0].type != ARGT_STR) ||
1797 (args[1].type && args[1].type != ARGT_STR))
1798 return 0;
1799
1800 name = "";
1801 name_len = 0;
1802 if (args->type == ARGT_STR) {
1803 name = args->data.str.area;
1804 name_len = args->data.str.data;
1805 }
1806
1807 if (args[1].type)
1808 delim = *args[1].data.str.area;
1809
1810 if (!smp->ctx.a[0]) { // first call, find the query string
Christopher Faulet778f5ed2020-04-29 15:51:55 +02001811 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001812 struct htx_sl *sl;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001813
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001814 if (!htx)
1815 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001816
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001817 sl = http_get_stline(htx);
1818 smp->ctx.a[0] = http_find_param_list(HTX_SL_REQ_UPTR(sl), HTX_SL_REQ_ULEN(sl), delim);
1819 if (!smp->ctx.a[0])
1820 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001821
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001822 smp->ctx.a[1] = HTX_SL_REQ_UPTR(sl) + HTX_SL_REQ_ULEN(sl);
Willy Tarreau79e57332018-10-02 16:01:16 +02001823
1824 /* Assume that the context is filled with NULL pointer
1825 * before the first call.
1826 * smp->ctx.a[2] = NULL;
1827 * smp->ctx.a[3] = NULL;
1828 */
1829 }
1830
1831 return smp_fetch_param(delim, name, name_len, args, smp, kw, private);
1832}
1833
1834/* This function iterates over each parameter of the body. This requires
1835 * that the body has been waited for using http-buffer-request. It uses
1836 * ctx->a[0] and ctx->a[1] to store the beginning and end of the first
1837 * contigous part of the body, and optionally ctx->a[2..3] to reference the
1838 * optional second part if the body wraps at the end of the buffer. An optional
1839 * parameter name is passed in args[0], otherwise any parameter is considered.
1840 */
1841static int smp_fetch_body_param(const struct arg *args, struct sample *smp, const char *kw, void *private)
1842{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001843 struct channel *chn = SMP_REQ_CHN(smp);
Willy Tarreau79e57332018-10-02 16:01:16 +02001844 const char *name;
1845 int name_len;
1846
1847 if (!args || (args[0].type && args[0].type != ARGT_STR))
1848 return 0;
1849
1850 name = "";
1851 name_len = 0;
1852 if (args[0].type == ARGT_STR) {
1853 name = args[0].data.str.area;
1854 name_len = args[0].data.str.data;
1855 }
1856
1857 if (!smp->ctx.a[0]) { // first call, find the query string
Christopher Faulete596d182020-05-05 17:46:34 +02001858 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001859 struct buffer *temp;
1860 int32_t pos;
Willy Tarreau79e57332018-10-02 16:01:16 +02001861
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001862 if (!htx)
1863 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001864
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001865 temp = get_trash_chunk();
1866 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
1867 struct htx_blk *blk = htx_get_blk(htx, pos);
1868 enum htx_blk_type type = htx_get_blk_type(blk);
Willy Tarreau79e57332018-10-02 16:01:16 +02001869
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001870 if (type == HTX_BLK_EOM || type == HTX_BLK_TLR || type == HTX_BLK_EOT)
1871 break;
1872 if (type == HTX_BLK_DATA) {
Christopher Faulet53a899b2019-10-08 16:38:42 +02001873 if (!h1_format_htx_data(htx_get_blk_value(htx, blk), temp, 0))
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001874 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001875 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001876 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001877
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001878 smp->ctx.a[0] = temp->area;
1879 smp->ctx.a[1] = temp->area + temp->data;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001880
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001881 /* Assume that the context is filled with NULL pointer
1882 * before the first call.
1883 * smp->ctx.a[2] = NULL;
1884 * smp->ctx.a[3] = NULL;
1885 */
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001886
Willy Tarreau79e57332018-10-02 16:01:16 +02001887 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001888
Willy Tarreau79e57332018-10-02 16:01:16 +02001889 return smp_fetch_param('&', name, name_len, args, smp, kw, private);
1890}
1891
1892/* Return the signed integer value for the specified url parameter (see url_param
1893 * above).
1894 */
1895static int smp_fetch_url_param_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
1896{
1897 int ret = smp_fetch_url_param(args, smp, kw, private);
1898
1899 if (ret > 0) {
1900 smp->data.type = SMP_T_SINT;
1901 smp->data.u.sint = strl2ic(smp->data.u.str.area,
1902 smp->data.u.str.data);
1903 }
1904
1905 return ret;
1906}
1907
1908/* This produces a 32-bit hash of the concatenation of the first occurrence of
1909 * the Host header followed by the path component if it begins with a slash ('/').
1910 * This means that '*' will not be added, resulting in exactly the first Host
1911 * entry. If no Host header is found, then the path is used. The resulting value
1912 * is hashed using the url hash followed by a full avalanche hash and provides a
1913 * 32-bit integer value. This fetch is useful for tracking per-URL activity on
1914 * high-traffic sites without having to store whole paths.
1915 * this differs from the base32 functions in that it includes the url parameters
1916 * as well as the path
1917 */
1918static int smp_fetch_url32(const struct arg *args, struct sample *smp, const char *kw, void *private)
1919{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001920 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +02001921 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001922 struct http_hdr_ctx ctx;
1923 struct htx_sl *sl;
1924 struct ist path;
Willy Tarreau79e57332018-10-02 16:01:16 +02001925 unsigned int hash = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001926
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001927 if (!htx)
1928 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001929
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001930 ctx.blk = NULL;
1931 if (http_find_header(htx, ist("Host"), &ctx, 1)) {
1932 /* OK we have the header value in ctx.value */
1933 while (ctx.value.len--)
1934 hash = *(ctx.value.ptr++) + (hash << 6) + (hash << 16) - hash;
Willy Tarreau79e57332018-10-02 16:01:16 +02001935 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001936
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001937 /* now retrieve the path */
1938 sl = http_get_stline(htx);
1939 path = http_get_path(htx_sl_req_uri(sl));
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001940 if (path.len && *(path.ptr) == '/') {
1941 while (path.len--)
1942 hash = *(path.ptr++) + (hash << 6) + (hash << 16) - hash;
Willy Tarreau79e57332018-10-02 16:01:16 +02001943 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001944
Willy Tarreau79e57332018-10-02 16:01:16 +02001945 hash = full_hash(hash);
1946
1947 smp->data.type = SMP_T_SINT;
1948 smp->data.u.sint = hash;
1949 smp->flags = SMP_F_VOL_1ST;
1950 return 1;
1951}
1952
1953/* This concatenates the source address with the 32-bit hash of the Host and
1954 * URL as returned by smp_fetch_base32(). The idea is to have per-source and
1955 * per-url counters. The result is a binary block from 8 to 20 bytes depending
1956 * on the source address length. The URL hash is stored before the address so
1957 * that in environments where IPv6 is insignificant, truncating the output to
1958 * 8 bytes would still work.
1959 */
1960static int smp_fetch_url32_src(const struct arg *args, struct sample *smp, const char *kw, void *private)
1961{
1962 struct buffer *temp;
1963 struct connection *cli_conn = objt_conn(smp->sess->origin);
1964
Willy Tarreaucd7ca792019-07-17 16:57:03 +02001965 if (!cli_conn || !conn_get_src(cli_conn))
Willy Tarreau79e57332018-10-02 16:01:16 +02001966 return 0;
1967
1968 if (!smp_fetch_url32(args, smp, kw, private))
1969 return 0;
1970
1971 temp = get_trash_chunk();
1972 *(unsigned int *) temp->area = htonl(smp->data.u.sint);
1973 temp->data += sizeof(unsigned int);
1974
Willy Tarreau9a1efe12019-07-17 17:13:50 +02001975 switch (cli_conn->src->ss_family) {
Willy Tarreau79e57332018-10-02 16:01:16 +02001976 case AF_INET:
1977 memcpy(temp->area + temp->data,
Willy Tarreau9a1efe12019-07-17 17:13:50 +02001978 &((struct sockaddr_in *)cli_conn->src)->sin_addr,
Willy Tarreau79e57332018-10-02 16:01:16 +02001979 4);
1980 temp->data += 4;
1981 break;
1982 case AF_INET6:
1983 memcpy(temp->area + temp->data,
Willy Tarreau9a1efe12019-07-17 17:13:50 +02001984 &((struct sockaddr_in6 *)cli_conn->src)->sin6_addr,
Willy Tarreau79e57332018-10-02 16:01:16 +02001985 16);
1986 temp->data += 16;
1987 break;
1988 default:
1989 return 0;
1990 }
1991
1992 smp->data.u.str = *temp;
1993 smp->data.type = SMP_T_BIN;
1994 return 1;
1995}
1996
1997/************************************************************************/
1998/* Other utility functions */
1999/************************************************************************/
2000
2001/* This function is used to validate the arguments passed to any "hdr" fetch
2002 * keyword. These keywords support an optional positive or negative occurrence
2003 * number. We must ensure that the number is greater than -MAX_HDR_HISTORY. It
2004 * is assumed that the types are already the correct ones. Returns 0 on error,
2005 * non-zero if OK. If <err> is not NULL, it will be filled with a pointer to an
2006 * error message in case of error, that the caller is responsible for freeing.
2007 * The initial location must either be freeable or NULL.
2008 * Note: this function's pointer is checked from Lua.
2009 */
2010int val_hdr(struct arg *arg, char **err_msg)
2011{
2012 if (arg && arg[1].type == ARGT_SINT && arg[1].data.sint < -MAX_HDR_HISTORY) {
2013 memprintf(err_msg, "header occurrence must be >= %d", -MAX_HDR_HISTORY);
2014 return 0;
2015 }
2016 return 1;
2017}
2018
2019/************************************************************************/
2020/* All supported sample fetch keywords must be declared here. */
2021/************************************************************************/
2022
2023/* Note: must not be declared <const> as its list will be overwritten */
2024static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
2025 { "base", smp_fetch_base, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2026 { "base32", smp_fetch_base32, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
2027 { "base32+src", smp_fetch_base32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
2028
2029 /* capture are allocated and are permanent in the stream */
2030 { "capture.req.hdr", smp_fetch_capture_req_hdr, ARG1(1,SINT), NULL, SMP_T_STR, SMP_USE_HRQHP },
2031
2032 /* retrieve these captures from the HTTP logs */
2033 { "capture.req.method", smp_fetch_capture_req_method, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
2034 { "capture.req.uri", smp_fetch_capture_req_uri, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
2035 { "capture.req.ver", smp_fetch_capture_req_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
2036
2037 { "capture.res.hdr", smp_fetch_capture_res_hdr, ARG1(1,SINT), NULL, SMP_T_STR, SMP_USE_HRSHP },
2038 { "capture.res.ver", smp_fetch_capture_res_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
2039
2040 /* cookie is valid in both directions (eg: for "stick ...") but cook*
2041 * are only here to match the ACL's name, are request-only and are used
2042 * for ACL compatibility only.
2043 */
2044 { "cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Christopher Fauletc1f40dd2019-05-16 10:07:30 +02002045 { "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 +02002046 { "cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2047 { "cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2048
2049 /* hdr is valid in both directions (eg: for "stick ...") but hdr_* are
2050 * only here to match the ACL's name, are request-only and are used for
2051 * ACL compatibility only.
2052 */
Christopher Fauletc1f40dd2019-05-16 10:07:30 +02002053 { "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 +02002054 { "hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2055 { "hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV },
2056 { "hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_SINT, SMP_USE_HRQHV },
2057
Christopher Fauleta4063562019-08-02 11:51:37 +02002058 { "http_auth_type", smp_fetch_http_auth_type, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2059 { "http_auth_user", smp_fetch_http_auth_user, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2060 { "http_auth_pass", smp_fetch_http_auth_pass, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau79e57332018-10-02 16:01:16 +02002061 { "http_auth", smp_fetch_http_auth, ARG1(1,USR), NULL, SMP_T_BOOL, SMP_USE_HRQHV },
2062 { "http_auth_group", smp_fetch_http_auth_grp, ARG1(1,USR), NULL, SMP_T_STR, SMP_USE_HRQHV },
2063 { "http_first_req", smp_fetch_http_first_req, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
2064 { "method", smp_fetch_meth, 0, NULL, SMP_T_METH, SMP_USE_HRQHP },
2065 { "path", smp_fetch_path, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2066 { "query", smp_fetch_query, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2067
2068 /* HTTP protocol on the request path */
2069 { "req.proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
2070 { "req_proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
2071
2072 /* HTTP version on the request path */
2073 { "req.ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2074 { "req_ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2075
2076 { "req.body", smp_fetch_body, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
2077 { "req.body_len", smp_fetch_body_len, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
2078 { "req.body_size", smp_fetch_body_size, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
2079 { "req.body_param", smp_fetch_body_param, ARG1(0,STR), NULL, SMP_T_BIN, SMP_USE_HRQHV },
2080
2081 { "req.hdrs", smp_fetch_hdrs, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
2082 { "req.hdrs_bin", smp_fetch_hdrs_bin, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
2083
2084 /* HTTP version on the response path */
2085 { "res.ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
2086 { "resp_ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
2087
Christopher Faulete596d182020-05-05 17:46:34 +02002088 { "res.body", smp_fetch_body, 0, NULL, SMP_T_BIN, SMP_USE_HRSHV },
2089 { "res.body_len", smp_fetch_body_len, 0, NULL, SMP_T_SINT, SMP_USE_HRSHV },
2090 { "res.body_size", smp_fetch_body_size, 0, NULL, SMP_T_SINT, SMP_USE_HRSHV },
2091
2092 { "res.hdrs", smp_fetch_hdrs, 0, NULL, SMP_T_BIN, SMP_USE_HRSHV },
2093 { "res.hdrs_bin", smp_fetch_hdrs_bin, 0, NULL, SMP_T_BIN, SMP_USE_HRSHV },
2094
Willy Tarreau79e57332018-10-02 16:01:16 +02002095 /* explicit req.{cook,hdr} are used to force the fetch direction to be request-only */
2096 { "req.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
2097 { "req.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2098 { "req.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2099
2100 { "req.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
2101 { "req.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2102 { "req.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
2103 { "req.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2104 { "req.hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV },
2105 { "req.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
2106 { "req.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_SINT, SMP_USE_HRQHV },
2107
2108 /* explicit req.{cook,hdr} are used to force the fetch direction to be response-only */
2109 { "res.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
2110 { "res.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2111 { "res.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2112
2113 { "res.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
2114 { "res.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2115 { "res.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
2116 { "res.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2117 { "res.hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV },
2118 { "res.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
2119 { "res.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_SINT, SMP_USE_HRSHV },
2120
2121 /* scook is valid only on the response and is used for ACL compatibility */
2122 { "scook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
2123 { "scook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2124 { "scook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2125 { "set-cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV }, /* deprecated */
2126
2127 /* shdr is valid only on the response and is used for ACL compatibility */
2128 { "shdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
2129 { "shdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2130 { "shdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV },
2131 { "shdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_SINT, SMP_USE_HRSHV },
2132
2133 { "status", smp_fetch_stcode, 0, NULL, SMP_T_SINT, SMP_USE_HRSHP },
2134 { "unique-id", smp_fetch_uniqueid, 0, NULL, SMP_T_STR, SMP_SRC_L4SRV },
2135 { "url", smp_fetch_url, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2136 { "url32", smp_fetch_url32, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
2137 { "url32+src", smp_fetch_url32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
2138 { "url_ip", smp_fetch_url_ip, 0, NULL, SMP_T_IPV4, SMP_USE_HRQHV },
2139 { "url_port", smp_fetch_url_port, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
2140 { "url_param", smp_fetch_url_param, ARG2(0,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
2141 { "urlp" , smp_fetch_url_param, ARG2(0,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
2142 { "urlp_val", smp_fetch_url_param_val, ARG2(0,STR,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Christopher Faulet16032ab2020-04-30 11:30:00 +02002143
Willy Tarreau79e57332018-10-02 16:01:16 +02002144 { /* END */ },
2145}};
2146
Willy Tarreau0108d902018-11-25 19:14:37 +01002147INITCALL1(STG_REGISTER, sample_register_fetches, &sample_fetch_keywords);
Willy Tarreau79e57332018-10-02 16:01:16 +02002148
2149/*
2150 * Local variables:
2151 * c-indent-level: 8
2152 * c-basic-offset: 8
2153 * End:
2154 */