blob: bf1d3e9c3a201e8aebfc037b81b597feab616070 [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 Faulet778f5ed2020-04-29 15:51:55 +0200389 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200390 struct htx_sl *sl;
Willy Tarreau79e57332018-10-02 16:01:16 +0200391 char *ptr;
392 int len;
393
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200394 if (!htx)
395 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200396
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200397 sl = http_get_stline(htx);
398 len = HTX_SL_RES_VLEN(sl);
399 ptr = HTX_SL_RES_VPTR(sl);
Willy Tarreau79e57332018-10-02 16:01:16 +0200400
401 while ((len-- > 0) && (*ptr++ != '/'));
402 if (len <= 0)
403 return 0;
404
405 smp->data.type = SMP_T_STR;
406 smp->data.u.str.area = ptr;
407 smp->data.u.str.data = len;
408
409 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
410 return 1;
411}
412
413/* 3. Check on Status Code. We manipulate integers here. */
414static int smp_fetch_stcode(const struct arg *args, struct sample *smp, const char *kw, void *private)
415{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200416 struct channel *chn = SMP_RES_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +0200417 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200418 struct htx_sl *sl;
Willy Tarreau79e57332018-10-02 16:01:16 +0200419 char *ptr;
420 int len;
421
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200422 if (!htx)
423 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200424
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200425 sl = http_get_stline(htx);
426 len = HTX_SL_RES_CLEN(sl);
427 ptr = HTX_SL_RES_CPTR(sl);
Willy Tarreau79e57332018-10-02 16:01:16 +0200428
429 smp->data.type = SMP_T_SINT;
430 smp->data.u.sint = __strl2ui(ptr, len);
431 smp->flags = SMP_F_VOL_1ST;
432 return 1;
433}
434
435static int smp_fetch_uniqueid(const struct arg *args, struct sample *smp, const char *kw, void *private)
436{
Tim Duesterhusa17e6622020-03-05 20:19:02 +0100437 struct ist unique_id;
Tim Duesterhus2825b4b2020-02-28 15:13:34 +0100438
Willy Tarreau79e57332018-10-02 16:01:16 +0200439 if (LIST_ISEMPTY(&smp->sess->fe->format_unique_id))
440 return 0;
441
Willy Tarreaua1062a42020-04-29 11:50:38 +0200442 if (!smp->strm)
443 return 0;
444
Tim Duesterhusa17e6622020-03-05 20:19:02 +0100445 unique_id = stream_generate_unique_id(smp->strm, &smp->sess->fe->format_unique_id);
446 if (!isttest(unique_id))
Tim Duesterhus2825b4b2020-02-28 15:13:34 +0100447 return 0;
448
Tim Duesterhusa17e6622020-03-05 20:19:02 +0100449 smp->data.u.str.area = smp->strm->unique_id.ptr;
450 smp->data.u.str.data = smp->strm->unique_id.len;
Tim Duesterhus2825b4b2020-02-28 15:13:34 +0100451 smp->data.type = SMP_T_STR;
Willy Tarreau79e57332018-10-02 16:01:16 +0200452 smp->flags = SMP_F_CONST;
453 return 1;
454}
455
456/* Returns a string block containing all headers including the
Joseph Herlant942eea32018-11-15 13:57:22 -0800457 * empty line which separes headers from the body. This is useful
458 * for some headers analysis.
Willy Tarreau79e57332018-10-02 16:01:16 +0200459 */
460static int smp_fetch_hdrs(const struct arg *args, struct sample *smp, const char *kw, void *private)
461{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200462 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +0200463 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200464 struct buffer *temp;
465 int32_t pos;
Willy Tarreau79e57332018-10-02 16:01:16 +0200466
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200467 if (!htx)
468 return 0;
469 temp = get_trash_chunk();
470 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
471 struct htx_blk *blk = htx_get_blk(htx, pos);
472 enum htx_blk_type type = htx_get_blk_type(blk);
Willy Tarreau79e57332018-10-02 16:01:16 +0200473
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200474 if (type == HTX_BLK_HDR) {
475 struct ist n = htx_get_blk_name(htx, blk);
476 struct ist v = htx_get_blk_value(htx, blk);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200477
Christopher Faulet53a899b2019-10-08 16:38:42 +0200478 if (!h1_format_htx_hdr(n, v, temp))
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200479 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200480 }
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200481 else if (type == HTX_BLK_EOH) {
482 if (!chunk_memcat(temp, "\r\n", 2))
483 return 0;
484 break;
485 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200486 }
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200487 smp->data.type = SMP_T_STR;
488 smp->data.u.str = *temp;
Willy Tarreau79e57332018-10-02 16:01:16 +0200489 return 1;
490}
491
492/* Returns the header request in a length/value encoded format.
493 * This is useful for exchanges with the SPOE.
494 *
495 * A "length value" is a multibyte code encoding numbers. It uses the
496 * SPOE format. The encoding is the following:
497 *
498 * Each couple "header name" / "header value" is composed
499 * like this:
500 * "length value" "header name bytes"
501 * "length value" "header value bytes"
502 * When the last header is reached, the header name and the header
503 * value are empty. Their length are 0
504 */
505static int smp_fetch_hdrs_bin(const struct arg *args, struct sample *smp, const char *kw, void *private)
506{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200507 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +0200508 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200509 struct buffer *temp;
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200510 char *p, *end;
511 int32_t pos;
512 int ret;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200513
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200514 if (!htx)
515 return 0;
516 temp = get_trash_chunk();
517 p = temp->area;
518 end = temp->area + temp->size;
519 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
520 struct htx_blk *blk = htx_get_blk(htx, pos);
521 enum htx_blk_type type = htx_get_blk_type(blk);
522 struct ist n, v;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200523
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200524 if (type == HTX_BLK_HDR) {
525 n = htx_get_blk_name(htx,blk);
526 v = htx_get_blk_value(htx, blk);
Willy Tarreau79e57332018-10-02 16:01:16 +0200527
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200528 /* encode the header name. */
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200529 ret = encode_varint(n.len, &p, end);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200530 if (ret == -1)
531 return 0;
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200532 if (p + n.len > end)
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200533 return 0;
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200534 memcpy(p, n.ptr, n.len);
535 p += n.len;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200536
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200537 /* encode the header value. */
538 ret = encode_varint(v.len, &p, end);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200539 if (ret == -1)
540 return 0;
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200541 if (p + v.len > end)
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200542 return 0;
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200543 memcpy(p, v.ptr, v.len);
544 p += v.len;
Willy Tarreau79e57332018-10-02 16:01:16 +0200545
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200546 }
547 else if (type == HTX_BLK_EOH) {
548 /* encode the end of the header list with empty
549 * header name and header value.
550 */
551 ret = encode_varint(0, &p, end);
552 if (ret == -1)
553 return 0;
554 ret = encode_varint(0, &p, end);
555 if (ret == -1)
556 return 0;
557 break;
558 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200559 }
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200560
561 /* Initialise sample data which will be filled. */
562 smp->data.type = SMP_T_BIN;
563 smp->data.u.str.area = temp->area;
564 smp->data.u.str.data = p - temp->area;
565 smp->data.u.str.size = temp->size;
Willy Tarreau79e57332018-10-02 16:01:16 +0200566 return 1;
567}
568
569/* returns the longest available part of the body. This requires that the body
570 * has been waited for using http-buffer-request.
571 */
572static int smp_fetch_body(const struct arg *args, struct sample *smp, const char *kw, void *private)
573{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200574 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +0200575 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Willy Tarreau79e57332018-10-02 16:01:16 +0200576 struct buffer *temp;
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200577 int32_t pos;
Willy Tarreau79e57332018-10-02 16:01:16 +0200578
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200579 if (!htx)
580 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200581
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200582 temp = get_trash_chunk();
583 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
584 struct htx_blk *blk = htx_get_blk(htx, pos);
585 enum htx_blk_type type = htx_get_blk_type(blk);
Willy Tarreau79e57332018-10-02 16:01:16 +0200586
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200587 if (type == HTX_BLK_EOM || type == HTX_BLK_TLR || type == HTX_BLK_EOT)
588 break;
589 if (type == HTX_BLK_DATA) {
Christopher Faulet53a899b2019-10-08 16:38:42 +0200590 if (!h1_format_htx_data(htx_get_blk_value(htx, blk), temp, 0))
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200591 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200592 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200593 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200594
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200595 smp->data.type = SMP_T_BIN;
596 smp->data.u.str = *temp;
597 smp->flags = SMP_F_VOL_TEST;
Willy Tarreau79e57332018-10-02 16:01:16 +0200598 return 1;
599}
600
601
602/* returns the available length of the body. This requires that the body
603 * has been waited for using http-buffer-request.
604 */
605static int smp_fetch_body_len(const struct arg *args, struct sample *smp, const char *kw, void *private)
606{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200607 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +0200608 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200609 int32_t pos;
610 unsigned long long len = 0;
Christopher Fauletc16317d2018-12-12 14:11:22 +0100611
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200612 if (!htx)
613 return 0;
Christopher Fauletc16317d2018-12-12 14:11:22 +0100614
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200615 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
616 struct htx_blk *blk = htx_get_blk(htx, pos);
617 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Fauletc16317d2018-12-12 14:11:22 +0100618
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200619 if (type == HTX_BLK_EOM || type == HTX_BLK_TLR || type == HTX_BLK_EOT)
620 break;
621 if (type == HTX_BLK_DATA)
622 len += htx_get_blksz(blk);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200623 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200624
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200625 smp->data.type = SMP_T_SINT;
626 smp->data.u.sint = len;
627 smp->flags = SMP_F_VOL_TEST;
Willy Tarreau79e57332018-10-02 16:01:16 +0200628 return 1;
629}
630
631
632/* returns the advertised length of the body, or the advertised size of the
633 * chunks available in the buffer. This requires that the body has been waited
634 * for using http-buffer-request.
635 */
636static int smp_fetch_body_size(const struct arg *args, struct sample *smp, const char *kw, void *private)
637{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200638 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +0200639 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200640 int32_t pos;
641 unsigned long long len = 0;
Christopher Faulet89dc4992019-04-17 12:02:59 +0200642
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200643 if (!htx)
644 return 0;
Christopher Fauletc16317d2018-12-12 14:11:22 +0100645
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200646 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
647 struct htx_blk *blk = htx_get_blk(htx, pos);
648 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Fauletc16317d2018-12-12 14:11:22 +0100649
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200650 if (type == HTX_BLK_EOM || type == HTX_BLK_TLR || type == HTX_BLK_EOT)
651 break;
652 if (type == HTX_BLK_DATA)
653 len += htx_get_blksz(blk);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200654 }
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200655 if (htx->extra != ULLONG_MAX)
656 len += htx->extra;
Willy Tarreau79e57332018-10-02 16:01:16 +0200657
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200658 smp->data.type = SMP_T_SINT;
659 smp->data.u.sint = len;
660 smp->flags = SMP_F_VOL_TEST;
Willy Tarreau79e57332018-10-02 16:01:16 +0200661 return 1;
662}
663
664
665/* 4. Check on URL/URI. A pointer to the URI is stored. */
666static int smp_fetch_url(const struct arg *args, struct sample *smp, const char *kw, void *private)
667{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200668 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +0200669 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200670 struct htx_sl *sl;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200671
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200672 if (!htx)
673 return 0;
674 sl = http_get_stline(htx);
675 smp->data.type = SMP_T_STR;
676 smp->data.u.str.area = HTX_SL_REQ_UPTR(sl);
677 smp->data.u.str.data = HTX_SL_REQ_ULEN(sl);
678 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau79e57332018-10-02 16:01:16 +0200679 return 1;
680}
681
682static int smp_fetch_url_ip(const struct arg *args, struct sample *smp, const char *kw, void *private)
683{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200684 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +0200685 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200686 struct htx_sl *sl;
Willy Tarreau79e57332018-10-02 16:01:16 +0200687 struct sockaddr_storage addr;
688
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200689 if (!htx)
690 return 0;
691 sl = http_get_stline(htx);
692 url2sa(HTX_SL_REQ_UPTR(sl), HTX_SL_REQ_ULEN(sl), &addr, NULL);
Willy Tarreau79e57332018-10-02 16:01:16 +0200693
Willy Tarreau79e57332018-10-02 16:01:16 +0200694 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
695 return 0;
696
697 smp->data.type = SMP_T_IPV4;
698 smp->data.u.ipv4 = ((struct sockaddr_in *)&addr)->sin_addr;
699 smp->flags = 0;
700 return 1;
701}
702
703static int smp_fetch_url_port(const struct arg *args, struct sample *smp, const char *kw, void *private)
704{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200705 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +0200706 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200707 struct htx_sl *sl;
Willy Tarreau79e57332018-10-02 16:01:16 +0200708 struct sockaddr_storage addr;
709
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200710 if (!htx)
711 return 0;
712 sl = http_get_stline(htx);
713 url2sa(HTX_SL_REQ_UPTR(sl), HTX_SL_REQ_ULEN(sl), &addr, NULL);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200714
Willy Tarreau79e57332018-10-02 16:01:16 +0200715 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
716 return 0;
717
718 smp->data.type = SMP_T_SINT;
719 smp->data.u.sint = ntohs(((struct sockaddr_in *)&addr)->sin_port);
720 smp->flags = 0;
721 return 1;
722}
723
724/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
725 * Accepts an optional argument of type string containing the header field name,
726 * and an optional argument of type signed or unsigned integer to request an
727 * explicit occurrence of the header. Note that in the event of a missing name,
728 * headers are considered from the first one. It does not stop on commas and
729 * returns full lines instead (useful for User-Agent or Date for example).
730 */
731static int smp_fetch_fhdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
732{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200733 /* possible keywords: req.fhdr, res.fhdr */
734 struct channel *chn = ((kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Christopher Faulet778f5ed2020-04-29 15:51:55 +0200735 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200736 struct http_hdr_ctx *ctx = smp->ctx.a[0];
737 struct ist name;
Willy Tarreau79e57332018-10-02 16:01:16 +0200738 int occ = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200739
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200740 if (!ctx) {
741 /* first call */
742 ctx = &static_http_hdr_ctx;
743 ctx->blk = NULL;
744 smp->ctx.a[0] = ctx;
745 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200746
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200747 if (args) {
748 if (args[0].type != ARGT_STR)
Willy Tarreau79e57332018-10-02 16:01:16 +0200749 return 0;
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200750 name.ptr = args[0].data.str.area;
751 name.len = args[0].data.str.data;
Willy Tarreau79e57332018-10-02 16:01:16 +0200752
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200753 if (args[1].type == ARGT_SINT)
754 occ = args[1].data.sint;
Willy Tarreau79e57332018-10-02 16:01:16 +0200755 }
756
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200757 if (!htx)
758 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200759
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200760 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
761 /* search for header from the beginning */
762 ctx->blk = NULL;
Willy Tarreau79e57332018-10-02 16:01:16 +0200763
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200764 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
765 /* no explicit occurrence and single fetch => last header by default */
766 occ = -1;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200767
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200768 if (!occ)
769 /* prepare to report multiple occurrences for ACL fetches */
770 smp->flags |= SMP_F_NOT_LAST;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200771
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200772 smp->data.type = SMP_T_STR;
773 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
774 if (http_get_htx_fhdr(htx, name, occ, ctx, &smp->data.u.str.area, &smp->data.u.str.data))
775 return 1;
Willy Tarreau79e57332018-10-02 16:01:16 +0200776 smp->flags &= ~SMP_F_NOT_LAST;
777 return 0;
778}
779
780/* 6. Check on HTTP header count. The number of occurrences is returned.
781 * Accepts exactly 1 argument of type string. It does not stop on commas and
782 * returns full lines instead (useful for User-Agent or Date for example).
783 */
784static int smp_fetch_fhdr_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
785{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200786 /* possible keywords: req.fhdr_cnt, res.fhdr_cnt */
787 struct channel *chn = ((kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Christopher Faulet778f5ed2020-04-29 15:51:55 +0200788 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200789 struct http_hdr_ctx ctx;
790 struct ist name;
Willy Tarreau79e57332018-10-02 16:01:16 +0200791 int cnt;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200792
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200793 if (!htx)
794 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200795
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200796 if (args && args->type == ARGT_STR) {
797 name.ptr = args->data.str.area;
798 name.len = args->data.str.data;
799 } else {
800 name.ptr = NULL;
801 name.len = 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200802 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200803
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200804 ctx.blk = NULL;
805 cnt = 0;
806 while (http_find_header(htx, name, &ctx, 1))
807 cnt++;
Willy Tarreau79e57332018-10-02 16:01:16 +0200808 smp->data.type = SMP_T_SINT;
809 smp->data.u.sint = cnt;
810 smp->flags = SMP_F_VOL_HDR;
811 return 1;
812}
813
814static int smp_fetch_hdr_names(const struct arg *args, struct sample *smp, const char *kw, void *private)
815{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200816 /* possible keywords: req.hdr_names, res.hdr_names */
817 struct channel *chn = ((kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Christopher Faulet778f5ed2020-04-29 15:51:55 +0200818 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Willy Tarreau79e57332018-10-02 16:01:16 +0200819 struct buffer *temp;
820 char del = ',';
821
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200822 int32_t pos;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200823
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200824 if (!htx)
825 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200826
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200827 if (args && args->type == ARGT_STR)
828 del = *args[0].data.str.area;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200829
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200830 temp = get_trash_chunk();
831 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
832 struct htx_blk *blk = htx_get_blk(htx, pos);
833 enum htx_blk_type type = htx_get_blk_type(blk);
834 struct ist n;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200835
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200836 if (type == HTX_BLK_EOH)
837 break;
838 if (type != HTX_BLK_HDR)
839 continue;
840 n = htx_get_blk_name(htx, blk);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200841
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200842 if (temp->data)
843 temp->area[temp->data++] = del;
844 chunk_memcat(temp, n.ptr, n.len);
Willy Tarreau79e57332018-10-02 16:01:16 +0200845 }
846
847 smp->data.type = SMP_T_STR;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200848 smp->data.u.str = *temp;
Willy Tarreau79e57332018-10-02 16:01:16 +0200849 smp->flags = SMP_F_VOL_HDR;
850 return 1;
851}
852
853/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
854 * Accepts an optional argument of type string containing the header field name,
855 * and an optional argument of type signed or unsigned integer to request an
856 * explicit occurrence of the header. Note that in the event of a missing name,
857 * headers are considered from the first one.
858 */
859static int smp_fetch_hdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
860{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200861 /* possible keywords: req.hdr / hdr, res.hdr / shdr */
862 struct channel *chn = ((kw[0] == 'h' || kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Christopher Faulet778f5ed2020-04-29 15:51:55 +0200863 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200864 struct http_hdr_ctx *ctx = smp->ctx.a[0];
865 struct ist name;
Willy Tarreau79e57332018-10-02 16:01:16 +0200866 int occ = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200867
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200868 if (!ctx) {
869 /* first call */
870 ctx = &static_http_hdr_ctx;
871 ctx->blk = NULL;
872 smp->ctx.a[0] = ctx;
873 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200874
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200875 if (args) {
876 if (args[0].type != ARGT_STR)
Willy Tarreau79e57332018-10-02 16:01:16 +0200877 return 0;
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200878 name.ptr = args[0].data.str.area;
879 name.len = args[0].data.str.data;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200880
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200881 if (args[1].type == ARGT_SINT)
882 occ = args[1].data.sint;
Willy Tarreau79e57332018-10-02 16:01:16 +0200883 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200884
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200885 if (!htx)
886 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200887
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200888 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
889 /* search for header from the beginning */
890 ctx->blk = NULL;
Willy Tarreau79e57332018-10-02 16:01:16 +0200891
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200892 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
893 /* no explicit occurrence and single fetch => last header by default */
894 occ = -1;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200895
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200896 if (!occ)
897 /* prepare to report multiple occurrences for ACL fetches */
898 smp->flags |= SMP_F_NOT_LAST;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200899
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200900 smp->data.type = SMP_T_STR;
901 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
902 if (http_get_htx_hdr(htx, name, occ, ctx, &smp->data.u.str.area, &smp->data.u.str.data))
903 return 1;
Willy Tarreau79e57332018-10-02 16:01:16 +0200904
905 smp->flags &= ~SMP_F_NOT_LAST;
906 return 0;
907}
908
Christopher Fauletc1f40dd2019-05-16 10:07:30 +0200909/* Same than smp_fetch_hdr() but only relies on the sample direction to choose
910 * the right channel. So instead of duplicating the code, we just change the
911 * keyword and then fallback on smp_fetch_hdr().
912 */
913static int smp_fetch_chn_hdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
914{
915 kw = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ ? "req.hdr" : "res.hdr");
916 return smp_fetch_hdr(args, smp, kw, private);
917}
918
Willy Tarreau79e57332018-10-02 16:01:16 +0200919/* 6. Check on HTTP header count. The number of occurrences is returned.
920 * Accepts exactly 1 argument of type string.
921 */
922static int smp_fetch_hdr_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
923{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200924 /* possible keywords: req.hdr_cnt / hdr_cnt, res.hdr_cnt / shdr_cnt */
925 struct channel *chn = ((kw[0] == 'h' || kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Christopher Faulet778f5ed2020-04-29 15:51:55 +0200926 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200927 struct http_hdr_ctx ctx;
928 struct ist name;
Willy Tarreau79e57332018-10-02 16:01:16 +0200929 int cnt;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200930
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200931 if (!htx)
932 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200933
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200934 if (args && args->type == ARGT_STR) {
935 name.ptr = args->data.str.area;
936 name.len = args->data.str.data;
937 } else {
938 name.ptr = NULL;
939 name.len = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200940 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200941
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200942 ctx.blk = NULL;
943 cnt = 0;
944 while (http_find_header(htx, name, &ctx, 0))
945 cnt++;
Willy Tarreau79e57332018-10-02 16:01:16 +0200946
947 smp->data.type = SMP_T_SINT;
948 smp->data.u.sint = cnt;
949 smp->flags = SMP_F_VOL_HDR;
950 return 1;
951}
952
953/* Fetch an HTTP header's integer value. The integer value is returned. It
954 * takes a mandatory argument of type string and an optional one of type int
955 * to designate a specific occurrence. It returns an unsigned integer, which
956 * may or may not be appropriate for everything.
957 */
958static int smp_fetch_hdr_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
959{
960 int ret = smp_fetch_hdr(args, smp, kw, private);
961
962 if (ret > 0) {
963 smp->data.type = SMP_T_SINT;
964 smp->data.u.sint = strl2ic(smp->data.u.str.area,
965 smp->data.u.str.data);
966 }
967
968 return ret;
969}
970
971/* Fetch an HTTP header's IP value. takes a mandatory argument of type string
972 * and an optional one of type int to designate a specific occurrence.
973 * It returns an IPv4 or IPv6 address.
974 */
975static int smp_fetch_hdr_ip(const struct arg *args, struct sample *smp, const char *kw, void *private)
976{
977 int ret;
978
979 while ((ret = smp_fetch_hdr(args, smp, kw, private)) > 0) {
980 if (url2ipv4((char *) smp->data.u.str.area, &smp->data.u.ipv4)) {
981 smp->data.type = SMP_T_IPV4;
982 break;
983 } else {
984 struct buffer *temp = get_trash_chunk();
985 if (smp->data.u.str.data < temp->size - 1) {
986 memcpy(temp->area, smp->data.u.str.area,
987 smp->data.u.str.data);
988 temp->area[smp->data.u.str.data] = '\0';
989 if (inet_pton(AF_INET6, temp->area, &smp->data.u.ipv6)) {
990 smp->data.type = SMP_T_IPV6;
991 break;
992 }
993 }
994 }
995
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200996 /* if the header doesn't match an IP address, fetch next one */
997 if (!(smp->flags & SMP_F_NOT_LAST))
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200998 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200999 }
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001000 return ret;
1001}
Willy Tarreau79e57332018-10-02 16:01:16 +02001002
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001003/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
1004 * the first '/' after the possible hostname, and ends before the possible '?'.
1005 */
1006static int smp_fetch_path(const struct arg *args, struct sample *smp, const char *kw, void *private)
1007{
1008 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +02001009 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001010 struct htx_sl *sl;
1011 struct ist path;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001012
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001013 if (!htx)
1014 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001015
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001016 sl = http_get_stline(htx);
Jerome Magnin4fb196c2020-02-21 10:49:12 +01001017 path = iststop(http_get_path(htx_sl_req_uri(sl)), '?');
Tim Duesterhused526372020-03-05 17:56:33 +01001018 if (!isttest(path))
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001019 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001020
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001021 /* OK, we got the '/' ! */
1022 smp->data.type = SMP_T_STR;
1023 smp->data.u.str.area = path.ptr;
Jerome Magnin4fb196c2020-02-21 10:49:12 +01001024 smp->data.u.str.data = path.len;
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001025 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau79e57332018-10-02 16:01:16 +02001026 return 1;
1027}
1028
1029/* This produces a concatenation of the first occurrence of the Host header
1030 * followed by the path component if it begins with a slash ('/'). This means
1031 * that '*' will not be added, resulting in exactly the first Host entry.
1032 * If no Host header is found, then the path is returned as-is. The returned
1033 * value is stored in the trash so it does not need to be marked constant.
1034 * The returned sample is of type string.
1035 */
1036static int smp_fetch_base(const struct arg *args, struct sample *smp, const char *kw, void *private)
1037{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001038 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +02001039 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001040 struct htx_sl *sl;
Willy Tarreau79e57332018-10-02 16:01:16 +02001041 struct buffer *temp;
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001042 struct http_hdr_ctx ctx;
1043 struct ist path;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001044
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001045 if (!htx)
1046 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001047
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001048 ctx.blk = NULL;
1049 if (!http_find_header(htx, ist("Host"), &ctx, 0) || !ctx.value.len)
1050 return smp_fetch_path(args, smp, kw, private);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001051
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001052 /* OK we have the header value in ctx.value */
1053 temp = get_trash_chunk();
1054 chunk_memcat(temp, ctx.value.ptr, ctx.value.len);
Willy Tarreau79e57332018-10-02 16:01:16 +02001055
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001056 /* now retrieve the path */
1057 sl = http_get_stline(htx);
1058 path = http_get_path(htx_sl_req_uri(sl));
Tim Duesterhused526372020-03-05 17:56:33 +01001059 if (isttest(path)) {
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001060 size_t len;
Willy Tarreau79e57332018-10-02 16:01:16 +02001061
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001062 for (len = 0; len < path.len && *(path.ptr + len) != '?'; len++)
1063 ;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001064
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001065 if (len && *(path.ptr) == '/')
1066 chunk_memcat(temp, path.ptr, len);
Willy Tarreau79e57332018-10-02 16:01:16 +02001067 }
1068
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001069 smp->data.type = SMP_T_STR;
1070 smp->data.u.str = *temp;
Willy Tarreau79e57332018-10-02 16:01:16 +02001071 smp->flags = SMP_F_VOL_1ST;
1072 return 1;
1073}
1074
1075/* This produces a 32-bit hash of the concatenation of the first occurrence of
1076 * the Host header followed by the path component if it begins with a slash ('/').
1077 * This means that '*' will not be added, resulting in exactly the first Host
1078 * entry. If no Host header is found, then the path is used. The resulting value
1079 * is hashed using the path hash followed by a full avalanche hash and provides a
1080 * 32-bit integer value. This fetch is useful for tracking per-path activity on
1081 * high-traffic sites without having to store whole paths.
1082 */
1083static int smp_fetch_base32(const struct arg *args, struct sample *smp, const char *kw, void *private)
1084{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001085 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +02001086 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001087 struct htx_sl *sl;
1088 struct http_hdr_ctx ctx;
1089 struct ist path;
Willy Tarreau79e57332018-10-02 16:01:16 +02001090 unsigned int hash = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001091
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001092 if (!htx)
1093 return 0;
Dragan Dosen8861e1c2019-02-12 19:50:31 +01001094
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001095 ctx.blk = NULL;
1096 if (http_find_header(htx, ist("Host"), &ctx, 0)) {
1097 /* OK we have the header value in ctx.value */
1098 while (ctx.value.len--)
1099 hash = *(ctx.value.ptr++) + (hash << 6) + (hash << 16) - hash;
Willy Tarreau79e57332018-10-02 16:01:16 +02001100 }
1101
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001102 /* now retrieve the path */
1103 sl = http_get_stline(htx);
1104 path = http_get_path(htx_sl_req_uri(sl));
Tim Duesterhused526372020-03-05 17:56:33 +01001105 if (isttest(path)) {
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001106 size_t len;
Willy Tarreau79e57332018-10-02 16:01:16 +02001107
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001108 for (len = 0; len < path.len && *(path.ptr + len) != '?'; len++)
1109 ;
Willy Tarreau79e57332018-10-02 16:01:16 +02001110
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001111 if (len && *(path.ptr) == '/') {
1112 while (len--)
1113 hash = *(path.ptr++) + (hash << 6) + (hash << 16) - hash;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001114 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001115 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001116
Willy Tarreau79e57332018-10-02 16:01:16 +02001117 hash = full_hash(hash);
1118
1119 smp->data.type = SMP_T_SINT;
1120 smp->data.u.sint = hash;
1121 smp->flags = SMP_F_VOL_1ST;
1122 return 1;
1123}
1124
1125/* This concatenates the source address with the 32-bit hash of the Host and
1126 * path as returned by smp_fetch_base32(). The idea is to have per-source and
1127 * per-path counters. The result is a binary block from 8 to 20 bytes depending
1128 * on the source address length. The path hash is stored before the address so
1129 * that in environments where IPv6 is insignificant, truncating the output to
1130 * 8 bytes would still work.
1131 */
1132static int smp_fetch_base32_src(const struct arg *args, struct sample *smp, const char *kw, void *private)
1133{
1134 struct buffer *temp;
1135 struct connection *cli_conn = objt_conn(smp->sess->origin);
1136
Willy Tarreaucd7ca792019-07-17 16:57:03 +02001137 if (!cli_conn || !conn_get_src(cli_conn))
Willy Tarreau79e57332018-10-02 16:01:16 +02001138 return 0;
1139
1140 if (!smp_fetch_base32(args, smp, kw, private))
1141 return 0;
1142
1143 temp = get_trash_chunk();
1144 *(unsigned int *) temp->area = htonl(smp->data.u.sint);
1145 temp->data += sizeof(unsigned int);
1146
Willy Tarreau9a1efe12019-07-17 17:13:50 +02001147 switch (cli_conn->src->ss_family) {
Willy Tarreau79e57332018-10-02 16:01:16 +02001148 case AF_INET:
1149 memcpy(temp->area + temp->data,
Willy Tarreau9a1efe12019-07-17 17:13:50 +02001150 &((struct sockaddr_in *)cli_conn->src)->sin_addr,
Willy Tarreau79e57332018-10-02 16:01:16 +02001151 4);
1152 temp->data += 4;
1153 break;
1154 case AF_INET6:
1155 memcpy(temp->area + temp->data,
Willy Tarreau9a1efe12019-07-17 17:13:50 +02001156 &((struct sockaddr_in6 *)cli_conn->src)->sin6_addr,
Willy Tarreau79e57332018-10-02 16:01:16 +02001157 16);
1158 temp->data += 16;
1159 break;
1160 default:
1161 return 0;
1162 }
1163
1164 smp->data.u.str = *temp;
1165 smp->data.type = SMP_T_BIN;
1166 return 1;
1167}
1168
1169/* Extracts the query string, which comes after the question mark '?'. If no
1170 * question mark is found, nothing is returned. Otherwise it returns a sample
1171 * of type string carrying the whole query string.
1172 */
1173static int smp_fetch_query(const struct arg *args, struct sample *smp, const char *kw, void *private)
1174{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001175 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +02001176 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001177 struct htx_sl *sl;
Willy Tarreau79e57332018-10-02 16:01:16 +02001178 char *ptr, *end;
1179
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001180 if (!htx)
1181 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001182
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001183 sl = http_get_stline(htx);
1184 ptr = HTX_SL_REQ_UPTR(sl);
1185 end = HTX_SL_REQ_UPTR(sl) + HTX_SL_REQ_ULEN(sl);
Willy Tarreau79e57332018-10-02 16:01:16 +02001186
1187 /* look up the '?' */
1188 do {
1189 if (ptr == end)
1190 return 0;
1191 } while (*ptr++ != '?');
1192
1193 smp->data.type = SMP_T_STR;
1194 smp->data.u.str.area = ptr;
1195 smp->data.u.str.data = end - ptr;
1196 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
1197 return 1;
1198}
1199
1200static int smp_fetch_proto_http(const struct arg *args, struct sample *smp, const char *kw, void *private)
1201{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001202 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +02001203 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 0);
Willy Tarreau79e57332018-10-02 16:01:16 +02001204
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001205 if (!htx)
1206 return 0;
1207 smp->data.type = SMP_T_BOOL;
Willy Tarreau79e57332018-10-02 16:01:16 +02001208 smp->data.u.sint = 1;
1209 return 1;
1210}
1211
1212/* return a valid test if the current request is the first one on the connection */
1213static int smp_fetch_http_first_req(const struct arg *args, struct sample *smp, const char *kw, void *private)
1214{
Willy Tarreau79512b62020-04-29 11:52:13 +02001215 if (!smp->strm)
1216 return 0;
1217
Willy Tarreau79e57332018-10-02 16:01:16 +02001218 smp->data.type = SMP_T_BOOL;
1219 smp->data.u.sint = !(smp->strm->txn->flags & TX_NOT_FIRST);
1220 return 1;
1221}
1222
Christopher Fauleta4063562019-08-02 11:51:37 +02001223/* Fetch the authentication method if there is an Authorization header. It
1224 * relies on get_http_auth()
1225 */
1226static int smp_fetch_http_auth_type(const struct arg *args, struct sample *smp, const char *kw, void *private)
1227{
1228 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +02001229 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Fauleta4063562019-08-02 11:51:37 +02001230 struct http_txn *txn;
1231
1232 if (!htx)
1233 return 0;
1234
1235 txn = smp->strm->txn;
1236 if (!get_http_auth(smp, htx))
1237 return 0;
1238
1239 switch (txn->auth.method) {
1240 case HTTP_AUTH_BASIC:
1241 smp->data.u.str.area = "Basic";
1242 smp->data.u.str.data = 5;
1243 break;
1244 case HTTP_AUTH_DIGEST:
1245 /* Unexpected because not supported */
1246 smp->data.u.str.area = "Digest";
1247 smp->data.u.str.data = 6;
1248 break;
1249 default:
1250 return 0;
1251 }
1252
1253 smp->data.type = SMP_T_STR;
1254 smp->flags = SMP_F_CONST;
1255 return 1;
1256}
1257
1258/* Fetch the user supplied if there is an Authorization header. It relies on
1259 * get_http_auth()
1260 */
1261static int smp_fetch_http_auth_user(const struct arg *args, struct sample *smp, const char *kw, void *private)
1262{
1263 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +02001264 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Fauleta4063562019-08-02 11:51:37 +02001265 struct http_txn *txn;
1266
1267 if (!htx)
1268 return 0;
1269
1270 txn = smp->strm->txn;
1271 if (!get_http_auth(smp, htx))
1272 return 0;
1273
1274 smp->data.type = SMP_T_STR;
1275 smp->data.u.str.area = txn->auth.user;
1276 smp->data.u.str.data = strlen(txn->auth.user);
1277 smp->flags = SMP_F_CONST;
1278 return 1;
1279}
1280
1281/* Fetch the password supplied if there is an Authorization header. It relies on
1282 * get_http_auth()
1283 */
1284static int smp_fetch_http_auth_pass(const struct arg *args, struct sample *smp, const char *kw, void *private)
1285{
1286 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +02001287 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Fauleta4063562019-08-02 11:51:37 +02001288 struct http_txn *txn;
1289
1290 if (!htx)
1291 return 0;
1292
1293 txn = smp->strm->txn;
1294 if (!get_http_auth(smp, htx))
1295 return 0;
1296
1297 smp->data.type = SMP_T_STR;
1298 smp->data.u.str.area = txn->auth.pass;
1299 smp->data.u.str.data = strlen(txn->auth.pass);
1300 smp->flags = SMP_F_CONST;
1301 return 1;
1302}
1303
Willy Tarreau79e57332018-10-02 16:01:16 +02001304/* Accepts exactly 1 argument of type userlist */
1305static int smp_fetch_http_auth(const struct arg *args, struct sample *smp, const char *kw, void *private)
1306{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001307 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +02001308 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Willy Tarreau79e57332018-10-02 16:01:16 +02001309
1310 if (!args || args->type != ARGT_USR)
1311 return 0;
1312
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001313 if (!htx)
1314 return 0;
1315 if (!get_http_auth(smp, htx))
1316 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001317
1318 smp->data.type = SMP_T_BOOL;
1319 smp->data.u.sint = check_user(args->data.usr, smp->strm->txn->auth.user,
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001320 smp->strm->txn->auth.pass);
Willy Tarreau79e57332018-10-02 16:01:16 +02001321 return 1;
1322}
1323
1324/* Accepts exactly 1 argument of type userlist */
1325static int smp_fetch_http_auth_grp(const struct arg *args, struct sample *smp, const char *kw, void *private)
1326{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001327 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +02001328 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Faulet89dc4992019-04-17 12:02:59 +02001329
Willy Tarreau79e57332018-10-02 16:01:16 +02001330 if (!args || args->type != ARGT_USR)
1331 return 0;
1332
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001333 if (!htx)
1334 return 0;
1335 if (!get_http_auth(smp, htx))
1336 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001337
Willy Tarreau79e57332018-10-02 16:01:16 +02001338 /* if the user does not belong to the userlist or has a wrong password,
1339 * report that it unconditionally does not match. Otherwise we return
1340 * a string containing the username.
1341 */
1342 if (!check_user(args->data.usr, smp->strm->txn->auth.user,
1343 smp->strm->txn->auth.pass))
1344 return 0;
1345
1346 /* pat_match_auth() will need the user list */
1347 smp->ctx.a[0] = args->data.usr;
1348
1349 smp->data.type = SMP_T_STR;
1350 smp->flags = SMP_F_CONST;
1351 smp->data.u.str.area = smp->strm->txn->auth.user;
1352 smp->data.u.str.data = strlen(smp->strm->txn->auth.user);
1353
1354 return 1;
1355}
1356
1357/* Fetch a captured HTTP request header. The index is the position of
1358 * the "capture" option in the configuration file
1359 */
1360static int smp_fetch_capture_req_hdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
1361{
Willy Tarreau0898c2d2020-04-29 11:44:54 +02001362 struct proxy *fe;
Willy Tarreau79e57332018-10-02 16:01:16 +02001363 int idx;
1364
1365 if (!args || args->type != ARGT_SINT)
1366 return 0;
1367
Willy Tarreau0898c2d2020-04-29 11:44:54 +02001368 if (!smp->strm)
1369 return 0;
1370
1371 fe = strm_fe(smp->strm);
Willy Tarreau79e57332018-10-02 16:01:16 +02001372 idx = args->data.sint;
1373
1374 if (idx > (fe->nb_req_cap - 1) || smp->strm->req_cap == NULL || smp->strm->req_cap[idx] == NULL)
1375 return 0;
1376
1377 smp->data.type = SMP_T_STR;
1378 smp->flags |= SMP_F_CONST;
1379 smp->data.u.str.area = smp->strm->req_cap[idx];
1380 smp->data.u.str.data = strlen(smp->strm->req_cap[idx]);
1381
1382 return 1;
1383}
1384
1385/* Fetch a captured HTTP response header. The index is the position of
1386 * the "capture" option in the configuration file
1387 */
1388static int smp_fetch_capture_res_hdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
1389{
Willy Tarreau0898c2d2020-04-29 11:44:54 +02001390 struct proxy *fe;
Willy Tarreau79e57332018-10-02 16:01:16 +02001391 int idx;
1392
1393 if (!args || args->type != ARGT_SINT)
1394 return 0;
1395
Willy Tarreau0898c2d2020-04-29 11:44:54 +02001396 if (!smp->strm)
1397 return 0;
1398
1399 fe = strm_fe(smp->strm);
Willy Tarreau79e57332018-10-02 16:01:16 +02001400 idx = args->data.sint;
1401
1402 if (idx > (fe->nb_rsp_cap - 1) || smp->strm->res_cap == NULL || smp->strm->res_cap[idx] == NULL)
1403 return 0;
1404
1405 smp->data.type = SMP_T_STR;
1406 smp->flags |= SMP_F_CONST;
1407 smp->data.u.str.area = smp->strm->res_cap[idx];
1408 smp->data.u.str.data = strlen(smp->strm->res_cap[idx]);
1409
1410 return 1;
1411}
1412
1413/* Extracts the METHOD in the HTTP request, the txn->uri should be filled before the call */
1414static int smp_fetch_capture_req_method(const struct arg *args, struct sample *smp, const char *kw, void *private)
1415{
1416 struct buffer *temp;
Willy Tarreau0898c2d2020-04-29 11:44:54 +02001417 struct http_txn *txn;
Willy Tarreau79e57332018-10-02 16:01:16 +02001418 char *ptr;
1419
Willy Tarreau0898c2d2020-04-29 11:44:54 +02001420 if (!smp->strm)
1421 return 0;
1422
1423 txn = smp->strm->txn;
Willy Tarreau79e57332018-10-02 16:01:16 +02001424 if (!txn || !txn->uri)
1425 return 0;
1426
1427 ptr = txn->uri;
1428
1429 while (*ptr != ' ' && *ptr != '\0') /* find first space */
1430 ptr++;
1431
1432 temp = get_trash_chunk();
1433 temp->area = txn->uri;
1434 temp->data = ptr - txn->uri;
1435 smp->data.u.str = *temp;
1436 smp->data.type = SMP_T_STR;
1437 smp->flags = SMP_F_CONST;
1438
1439 return 1;
1440
1441}
1442
1443/* Extracts the path in the HTTP request, the txn->uri should be filled before the call */
1444static int smp_fetch_capture_req_uri(const struct arg *args, struct sample *smp, const char *kw, void *private)
1445{
Willy Tarreau0898c2d2020-04-29 11:44:54 +02001446 struct http_txn *txn;
Willy Tarreau79e57332018-10-02 16:01:16 +02001447 struct ist path;
1448 const char *ptr;
1449
Willy Tarreau0898c2d2020-04-29 11:44:54 +02001450 if (!smp->strm)
1451 return 0;
1452
1453 txn = smp->strm->txn;
Willy Tarreau79e57332018-10-02 16:01:16 +02001454 if (!txn || !txn->uri)
1455 return 0;
1456
1457 ptr = txn->uri;
1458
1459 while (*ptr != ' ' && *ptr != '\0') /* find first space */
1460 ptr++;
1461
1462 if (!*ptr)
1463 return 0;
1464
Christopher Faulet78337bb2018-11-15 14:35:18 +01001465 /* skip the first space and find space after URI */
1466 path = ist2(++ptr, 0);
1467 while (*ptr != ' ' && *ptr != '\0')
1468 ptr++;
1469 path.len = ptr - path.ptr;
Willy Tarreau79e57332018-10-02 16:01:16 +02001470
Christopher Faulet78337bb2018-11-15 14:35:18 +01001471 path = http_get_path(path);
Tim Duesterhused526372020-03-05 17:56:33 +01001472 if (!isttest(path))
Willy Tarreau79e57332018-10-02 16:01:16 +02001473 return 0;
1474
1475 smp->data.u.str.area = path.ptr;
1476 smp->data.u.str.data = path.len;
1477 smp->data.type = SMP_T_STR;
1478 smp->flags = SMP_F_CONST;
1479
1480 return 1;
1481}
1482
1483/* Retrieves the HTTP version from the request (either 1.0 or 1.1) and emits it
1484 * as a string (either "HTTP/1.0" or "HTTP/1.1").
1485 */
1486static int smp_fetch_capture_req_ver(const struct arg *args, struct sample *smp, const char *kw, void *private)
1487{
Willy Tarreau0898c2d2020-04-29 11:44:54 +02001488 struct http_txn *txn;
1489
1490 if (!smp->strm)
1491 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001492
Willy Tarreau0898c2d2020-04-29 11:44:54 +02001493 txn = smp->strm->txn;
Christopher Faulet711ed6a2019-07-16 14:16:10 +02001494 if (!txn || txn->req.msg_state >= HTTP_MSG_BODY)
Willy Tarreau79e57332018-10-02 16:01:16 +02001495 return 0;
1496
1497 if (txn->req.flags & HTTP_MSGF_VER_11)
1498 smp->data.u.str.area = "HTTP/1.1";
1499 else
1500 smp->data.u.str.area = "HTTP/1.0";
1501
1502 smp->data.u.str.data = 8;
1503 smp->data.type = SMP_T_STR;
1504 smp->flags = SMP_F_CONST;
1505 return 1;
1506
1507}
1508
1509/* Retrieves the HTTP version from the response (either 1.0 or 1.1) and emits it
1510 * as a string (either "HTTP/1.0" or "HTTP/1.1").
1511 */
1512static int smp_fetch_capture_res_ver(const struct arg *args, struct sample *smp, const char *kw, void *private)
1513{
Willy Tarreau0898c2d2020-04-29 11:44:54 +02001514 struct http_txn *txn;
1515
1516 if (!smp->strm)
1517 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001518
Willy Tarreau0898c2d2020-04-29 11:44:54 +02001519 txn = smp->strm->txn;
Christopher Faulet711ed6a2019-07-16 14:16:10 +02001520 if (!txn || txn->rsp.msg_state >= HTTP_MSG_BODY)
Willy Tarreau79e57332018-10-02 16:01:16 +02001521 return 0;
1522
1523 if (txn->rsp.flags & HTTP_MSGF_VER_11)
1524 smp->data.u.str.area = "HTTP/1.1";
1525 else
1526 smp->data.u.str.area = "HTTP/1.0";
1527
1528 smp->data.u.str.data = 8;
1529 smp->data.type = SMP_T_STR;
1530 smp->flags = SMP_F_CONST;
1531 return 1;
1532
1533}
1534
1535/* Iterate over all cookies present in a message. The context is stored in
1536 * smp->ctx.a[0] for the in-header position, smp->ctx.a[1] for the
1537 * end-of-header-value, and smp->ctx.a[2] for the hdr_ctx. Depending on
1538 * the direction, multiple cookies may be parsed on the same line or not.
1539 * The cookie name is in args and the name length in args->data.str.len.
1540 * Accepts exactly 1 argument of type string. If the input options indicate
1541 * that no iterating is desired, then only last value is fetched if any.
1542 * The returned sample is of type CSTR. Can be used to parse cookies in other
1543 * files.
1544 */
1545static int smp_fetch_cookie(const struct arg *args, struct sample *smp, const char *kw, void *private)
1546{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001547 /* possible keywords: req.cookie / cookie / cook, res.cookie / scook / set-cookie */
1548 struct channel *chn = ((kw[0] == 'c' || kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Christopher Faulet778f5ed2020-04-29 15:51:55 +02001549 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001550 struct http_hdr_ctx *ctx = smp->ctx.a[2];
1551 struct ist hdr;
Willy Tarreau79e57332018-10-02 16:01:16 +02001552 int occ = 0;
1553 int found = 0;
1554
1555 if (!args || args->type != ARGT_STR)
1556 return 0;
1557
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001558 if (!ctx) {
1559 /* first call */
1560 ctx = &static_http_hdr_ctx;
1561 ctx->blk = NULL;
1562 smp->ctx.a[2] = ctx;
1563 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001564
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001565 if (!htx)
1566 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001567
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001568 hdr = (!(chn->flags & CF_ISRESP) ? ist("Cookie") : ist("Set-Cookie"));
Willy Tarreau79e57332018-10-02 16:01:16 +02001569
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001570 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
1571 /* no explicit occurrence and single fetch => last cookie by default */
1572 occ = -1;
Willy Tarreau79e57332018-10-02 16:01:16 +02001573
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001574 /* OK so basically here, either we want only one value and it's the
1575 * last one, or we want to iterate over all of them and we fetch the
1576 * next one.
1577 */
Willy Tarreau79e57332018-10-02 16:01:16 +02001578
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001579 if (!(smp->flags & SMP_F_NOT_LAST)) {
1580 /* search for the header from the beginning, we must first initialize
1581 * the search parameters.
Willy Tarreau79e57332018-10-02 16:01:16 +02001582 */
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001583 smp->ctx.a[0] = NULL;
1584 ctx->blk = NULL;
Willy Tarreau79e57332018-10-02 16:01:16 +02001585 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001586
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001587 smp->flags |= SMP_F_VOL_HDR;
1588 while (1) {
1589 /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */
1590 if (!smp->ctx.a[0]) {
1591 if (!http_find_header(htx, hdr, ctx, 0))
1592 goto out;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001593
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001594 if (ctx->value.len < args->data.str.data + 1)
1595 continue;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001596
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001597 smp->ctx.a[0] = ctx->value.ptr;
1598 smp->ctx.a[1] = smp->ctx.a[0] + ctx->value.len;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001599 }
1600
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001601 smp->data.type = SMP_T_STR;
1602 smp->flags |= SMP_F_CONST;
1603 smp->ctx.a[0] = http_extract_cookie_value(smp->ctx.a[0], smp->ctx.a[1],
1604 args->data.str.area, args->data.str.data,
1605 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
1606 &smp->data.u.str.area,
1607 &smp->data.u.str.data);
1608 if (smp->ctx.a[0]) {
1609 found = 1;
1610 if (occ >= 0) {
1611 /* one value was returned into smp->data.u.str.{str,len} */
1612 smp->flags |= SMP_F_NOT_LAST;
1613 return 1;
Willy Tarreau79e57332018-10-02 16:01:16 +02001614 }
1615 }
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001616 /* if we're looking for last occurrence, let's loop */
Willy Tarreau79e57332018-10-02 16:01:16 +02001617 }
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001618
Willy Tarreau79e57332018-10-02 16:01:16 +02001619 /* all cookie headers and values were scanned. If we're looking for the
1620 * last occurrence, we may return it now.
1621 */
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001622 out:
Willy Tarreau79e57332018-10-02 16:01:16 +02001623 smp->flags &= ~SMP_F_NOT_LAST;
1624 return found;
1625}
1626
Christopher Fauletc1f40dd2019-05-16 10:07:30 +02001627/* Same than smp_fetch_cookie() but only relies on the sample direction to
1628 * choose the right channel. So instead of duplicating the code, we just change
1629 * the keyword and then fallback on smp_fetch_cookie().
1630 */
1631static int smp_fetch_chn_cookie(const struct arg *args, struct sample *smp, const char *kw, void *private)
1632{
1633 kw = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ ? "req.cook" : "res.cook");
1634 return smp_fetch_cookie(args, smp, kw, private);
1635}
1636
Willy Tarreau79e57332018-10-02 16:01:16 +02001637/* Iterate over all cookies present in a request to count how many occurrences
1638 * match the name in args and args->data.str.len. If <multi> is non-null, then
1639 * multiple cookies may be parsed on the same line. The returned sample is of
1640 * type UINT. Accepts exactly 1 argument of type string.
1641 */
1642static int smp_fetch_cookie_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
1643{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001644 /* possible keywords: req.cook_cnt / cook_cnt, res.cook_cnt / scook_cnt */
1645 struct channel *chn = ((kw[0] == 'c' || kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Christopher Faulet778f5ed2020-04-29 15:51:55 +02001646 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001647 struct http_hdr_ctx ctx;
1648 struct ist hdr;
Willy Tarreau79e57332018-10-02 16:01:16 +02001649 char *val_beg, *val_end;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001650 int cnt;
Willy Tarreau79e57332018-10-02 16:01:16 +02001651
1652 if (!args || args->type != ARGT_STR)
1653 return 0;
1654
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001655 if (!htx)
1656 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001657
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001658 hdr = (!(chn->flags & CF_ISRESP) ? ist("Cookie") : ist("Set-Cookie"));
Willy Tarreau79e57332018-10-02 16:01:16 +02001659
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001660 val_end = val_beg = NULL;
1661 ctx.blk = NULL;
1662 cnt = 0;
1663 while (1) {
1664 /* Note: val_beg == NULL every time we need to fetch a new header */
1665 if (!val_beg) {
1666 if (!http_find_header(htx, hdr, &ctx, 0))
1667 break;
Willy Tarreau79e57332018-10-02 16:01:16 +02001668
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001669 if (ctx.value.len < args->data.str.data + 1)
1670 continue;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001671
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001672 val_beg = ctx.value.ptr;
1673 val_end = val_beg + ctx.value.len;
Willy Tarreau79e57332018-10-02 16:01:16 +02001674 }
1675
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001676 smp->data.type = SMP_T_STR;
1677 smp->flags |= SMP_F_CONST;
1678 while ((val_beg = http_extract_cookie_value(val_beg, val_end,
1679 args->data.str.area, args->data.str.data,
1680 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
1681 &smp->data.u.str.area,
1682 &smp->data.u.str.data))) {
1683 cnt++;
Willy Tarreau79e57332018-10-02 16:01:16 +02001684 }
1685 }
1686
1687 smp->data.type = SMP_T_SINT;
1688 smp->data.u.sint = cnt;
1689 smp->flags |= SMP_F_VOL_HDR;
1690 return 1;
1691}
1692
1693/* Fetch an cookie's integer value. The integer value is returned. It
1694 * takes a mandatory argument of type string. It relies on smp_fetch_cookie().
1695 */
1696static int smp_fetch_cookie_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
1697{
1698 int ret = smp_fetch_cookie(args, smp, kw, private);
1699
1700 if (ret > 0) {
1701 smp->data.type = SMP_T_SINT;
1702 smp->data.u.sint = strl2ic(smp->data.u.str.area,
1703 smp->data.u.str.data);
1704 }
1705
1706 return ret;
1707}
1708
1709/************************************************************************/
1710/* The code below is dedicated to sample fetches */
1711/************************************************************************/
1712
1713/* This scans a URL-encoded query string. It takes an optionally wrapping
1714 * string whose first contigous chunk has its beginning in ctx->a[0] and end
1715 * in ctx->a[1], and the optional second part in (ctx->a[2]..ctx->a[3]). The
1716 * pointers are updated for next iteration before leaving.
1717 */
1718static int smp_fetch_param(char delim, const char *name, int name_len, const struct arg *args, struct sample *smp, const char *kw, void *private)
1719{
1720 const char *vstart, *vend;
1721 struct buffer *temp;
1722 const char **chunks = (const char **)smp->ctx.a;
1723
1724 if (!http_find_next_url_param(chunks, name, name_len,
1725 &vstart, &vend, delim))
1726 return 0;
1727
1728 /* Create sample. If the value is contiguous, return the pointer as CONST,
1729 * if the value is wrapped, copy-it in a buffer.
1730 */
1731 smp->data.type = SMP_T_STR;
1732 if (chunks[2] &&
1733 vstart >= chunks[0] && vstart <= chunks[1] &&
1734 vend >= chunks[2] && vend <= chunks[3]) {
1735 /* Wrapped case. */
1736 temp = get_trash_chunk();
1737 memcpy(temp->area, vstart, chunks[1] - vstart);
1738 memcpy(temp->area + ( chunks[1] - vstart ), chunks[2],
1739 vend - chunks[2]);
1740 smp->data.u.str.area = temp->area;
1741 smp->data.u.str.data = ( chunks[1] - vstart ) + ( vend - chunks[2] );
1742 } else {
1743 /* Contiguous case. */
1744 smp->data.u.str.area = (char *)vstart;
1745 smp->data.u.str.data = vend - vstart;
1746 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
1747 }
1748
1749 /* Update context, check wrapping. */
1750 chunks[0] = vend;
1751 if (chunks[2] && vend >= chunks[2] && vend <= chunks[3]) {
1752 chunks[1] = chunks[3];
1753 chunks[2] = NULL;
1754 }
1755
1756 if (chunks[0] < chunks[1])
1757 smp->flags |= SMP_F_NOT_LAST;
1758
1759 return 1;
1760}
1761
1762/* This function iterates over each parameter of the query string. It uses
1763 * ctx->a[0] and ctx->a[1] to store the beginning and end of the current
1764 * parameter. Since it uses smp_fetch_param(), ctx->a[2..3] are both NULL.
1765 * An optional parameter name is passed in args[0], otherwise any parameter is
1766 * considered. It supports an optional delimiter argument for the beginning of
1767 * the string in args[1], which defaults to "?".
1768 */
1769static int smp_fetch_url_param(const struct arg *args, struct sample *smp, const char *kw, void *private)
1770{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001771 struct channel *chn = SMP_REQ_CHN(smp);
Willy Tarreau79e57332018-10-02 16:01:16 +02001772 char delim = '?';
1773 const char *name;
1774 int name_len;
1775
1776 if (!args ||
1777 (args[0].type && args[0].type != ARGT_STR) ||
1778 (args[1].type && args[1].type != ARGT_STR))
1779 return 0;
1780
1781 name = "";
1782 name_len = 0;
1783 if (args->type == ARGT_STR) {
1784 name = args->data.str.area;
1785 name_len = args->data.str.data;
1786 }
1787
1788 if (args[1].type)
1789 delim = *args[1].data.str.area;
1790
1791 if (!smp->ctx.a[0]) { // first call, find the query string
Christopher Faulet778f5ed2020-04-29 15:51:55 +02001792 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001793 struct htx_sl *sl;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001794
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001795 if (!htx)
1796 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001797
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001798 sl = http_get_stline(htx);
1799 smp->ctx.a[0] = http_find_param_list(HTX_SL_REQ_UPTR(sl), HTX_SL_REQ_ULEN(sl), delim);
1800 if (!smp->ctx.a[0])
1801 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001802
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001803 smp->ctx.a[1] = HTX_SL_REQ_UPTR(sl) + HTX_SL_REQ_ULEN(sl);
Willy Tarreau79e57332018-10-02 16:01:16 +02001804
1805 /* Assume that the context is filled with NULL pointer
1806 * before the first call.
1807 * smp->ctx.a[2] = NULL;
1808 * smp->ctx.a[3] = NULL;
1809 */
1810 }
1811
1812 return smp_fetch_param(delim, name, name_len, args, smp, kw, private);
1813}
1814
1815/* This function iterates over each parameter of the body. This requires
1816 * that the body has been waited for using http-buffer-request. It uses
1817 * ctx->a[0] and ctx->a[1] to store the beginning and end of the first
1818 * contigous part of the body, and optionally ctx->a[2..3] to reference the
1819 * optional second part if the body wraps at the end of the buffer. An optional
1820 * parameter name is passed in args[0], otherwise any parameter is considered.
1821 */
1822static int smp_fetch_body_param(const struct arg *args, struct sample *smp, const char *kw, void *private)
1823{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001824 struct channel *chn = SMP_REQ_CHN(smp);
Willy Tarreau79e57332018-10-02 16:01:16 +02001825 const char *name;
1826 int name_len;
1827
1828 if (!args || (args[0].type && args[0].type != ARGT_STR))
1829 return 0;
1830
1831 name = "";
1832 name_len = 0;
1833 if (args[0].type == ARGT_STR) {
1834 name = args[0].data.str.area;
1835 name_len = args[0].data.str.data;
1836 }
1837
1838 if (!smp->ctx.a[0]) { // first call, find the query string
Christopher Faulet778f5ed2020-04-29 15:51:55 +02001839 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001840 struct buffer *temp;
1841 int32_t pos;
Willy Tarreau79e57332018-10-02 16:01:16 +02001842
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001843 if (!htx)
1844 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001845
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001846 temp = get_trash_chunk();
1847 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
1848 struct htx_blk *blk = htx_get_blk(htx, pos);
1849 enum htx_blk_type type = htx_get_blk_type(blk);
Willy Tarreau79e57332018-10-02 16:01:16 +02001850
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001851 if (type == HTX_BLK_EOM || type == HTX_BLK_TLR || type == HTX_BLK_EOT)
1852 break;
1853 if (type == HTX_BLK_DATA) {
Christopher Faulet53a899b2019-10-08 16:38:42 +02001854 if (!h1_format_htx_data(htx_get_blk_value(htx, blk), temp, 0))
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001855 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001856 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001857 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001858
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001859 smp->ctx.a[0] = temp->area;
1860 smp->ctx.a[1] = temp->area + temp->data;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001861
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001862 /* Assume that the context is filled with NULL pointer
1863 * before the first call.
1864 * smp->ctx.a[2] = NULL;
1865 * smp->ctx.a[3] = NULL;
1866 */
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001867
Willy Tarreau79e57332018-10-02 16:01:16 +02001868 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001869
Willy Tarreau79e57332018-10-02 16:01:16 +02001870 return smp_fetch_param('&', name, name_len, args, smp, kw, private);
1871}
1872
1873/* Return the signed integer value for the specified url parameter (see url_param
1874 * above).
1875 */
1876static int smp_fetch_url_param_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
1877{
1878 int ret = smp_fetch_url_param(args, smp, kw, private);
1879
1880 if (ret > 0) {
1881 smp->data.type = SMP_T_SINT;
1882 smp->data.u.sint = strl2ic(smp->data.u.str.area,
1883 smp->data.u.str.data);
1884 }
1885
1886 return ret;
1887}
1888
1889/* This produces a 32-bit hash of the concatenation of the first occurrence of
1890 * the Host header followed by the path component if it begins with a slash ('/').
1891 * This means that '*' will not be added, resulting in exactly the first Host
1892 * entry. If no Host header is found, then the path is used. The resulting value
1893 * is hashed using the url hash followed by a full avalanche hash and provides a
1894 * 32-bit integer value. This fetch is useful for tracking per-URL activity on
1895 * high-traffic sites without having to store whole paths.
1896 * this differs from the base32 functions in that it includes the url parameters
1897 * as well as the path
1898 */
1899static int smp_fetch_url32(const struct arg *args, struct sample *smp, const char *kw, void *private)
1900{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001901 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +02001902 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001903 struct http_hdr_ctx ctx;
1904 struct htx_sl *sl;
1905 struct ist path;
Willy Tarreau79e57332018-10-02 16:01:16 +02001906 unsigned int hash = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001907
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001908 if (!htx)
1909 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001910
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001911 ctx.blk = NULL;
1912 if (http_find_header(htx, ist("Host"), &ctx, 1)) {
1913 /* OK we have the header value in ctx.value */
1914 while (ctx.value.len--)
1915 hash = *(ctx.value.ptr++) + (hash << 6) + (hash << 16) - hash;
Willy Tarreau79e57332018-10-02 16:01:16 +02001916 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001917
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001918 /* now retrieve the path */
1919 sl = http_get_stline(htx);
1920 path = http_get_path(htx_sl_req_uri(sl));
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001921 if (path.len && *(path.ptr) == '/') {
1922 while (path.len--)
1923 hash = *(path.ptr++) + (hash << 6) + (hash << 16) - hash;
Willy Tarreau79e57332018-10-02 16:01:16 +02001924 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001925
Willy Tarreau79e57332018-10-02 16:01:16 +02001926 hash = full_hash(hash);
1927
1928 smp->data.type = SMP_T_SINT;
1929 smp->data.u.sint = hash;
1930 smp->flags = SMP_F_VOL_1ST;
1931 return 1;
1932}
1933
1934/* This concatenates the source address with the 32-bit hash of the Host and
1935 * URL as returned by smp_fetch_base32(). The idea is to have per-source and
1936 * per-url counters. The result is a binary block from 8 to 20 bytes depending
1937 * on the source address length. The URL hash is stored before the address so
1938 * that in environments where IPv6 is insignificant, truncating the output to
1939 * 8 bytes would still work.
1940 */
1941static int smp_fetch_url32_src(const struct arg *args, struct sample *smp, const char *kw, void *private)
1942{
1943 struct buffer *temp;
1944 struct connection *cli_conn = objt_conn(smp->sess->origin);
1945
Willy Tarreaucd7ca792019-07-17 16:57:03 +02001946 if (!cli_conn || !conn_get_src(cli_conn))
Willy Tarreau79e57332018-10-02 16:01:16 +02001947 return 0;
1948
1949 if (!smp_fetch_url32(args, smp, kw, private))
1950 return 0;
1951
1952 temp = get_trash_chunk();
1953 *(unsigned int *) temp->area = htonl(smp->data.u.sint);
1954 temp->data += sizeof(unsigned int);
1955
Willy Tarreau9a1efe12019-07-17 17:13:50 +02001956 switch (cli_conn->src->ss_family) {
Willy Tarreau79e57332018-10-02 16:01:16 +02001957 case AF_INET:
1958 memcpy(temp->area + temp->data,
Willy Tarreau9a1efe12019-07-17 17:13:50 +02001959 &((struct sockaddr_in *)cli_conn->src)->sin_addr,
Willy Tarreau79e57332018-10-02 16:01:16 +02001960 4);
1961 temp->data += 4;
1962 break;
1963 case AF_INET6:
1964 memcpy(temp->area + temp->data,
Willy Tarreau9a1efe12019-07-17 17:13:50 +02001965 &((struct sockaddr_in6 *)cli_conn->src)->sin6_addr,
Willy Tarreau79e57332018-10-02 16:01:16 +02001966 16);
1967 temp->data += 16;
1968 break;
1969 default:
1970 return 0;
1971 }
1972
1973 smp->data.u.str = *temp;
1974 smp->data.type = SMP_T_BIN;
1975 return 1;
1976}
1977
1978/************************************************************************/
1979/* Other utility functions */
1980/************************************************************************/
1981
1982/* This function is used to validate the arguments passed to any "hdr" fetch
1983 * keyword. These keywords support an optional positive or negative occurrence
1984 * number. We must ensure that the number is greater than -MAX_HDR_HISTORY. It
1985 * is assumed that the types are already the correct ones. Returns 0 on error,
1986 * non-zero if OK. If <err> is not NULL, it will be filled with a pointer to an
1987 * error message in case of error, that the caller is responsible for freeing.
1988 * The initial location must either be freeable or NULL.
1989 * Note: this function's pointer is checked from Lua.
1990 */
1991int val_hdr(struct arg *arg, char **err_msg)
1992{
1993 if (arg && arg[1].type == ARGT_SINT && arg[1].data.sint < -MAX_HDR_HISTORY) {
1994 memprintf(err_msg, "header occurrence must be >= %d", -MAX_HDR_HISTORY);
1995 return 0;
1996 }
1997 return 1;
1998}
1999
2000/************************************************************************/
2001/* All supported sample fetch keywords must be declared here. */
2002/************************************************************************/
2003
2004/* Note: must not be declared <const> as its list will be overwritten */
2005static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
2006 { "base", smp_fetch_base, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2007 { "base32", smp_fetch_base32, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
2008 { "base32+src", smp_fetch_base32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
2009
2010 /* capture are allocated and are permanent in the stream */
2011 { "capture.req.hdr", smp_fetch_capture_req_hdr, ARG1(1,SINT), NULL, SMP_T_STR, SMP_USE_HRQHP },
2012
2013 /* retrieve these captures from the HTTP logs */
2014 { "capture.req.method", smp_fetch_capture_req_method, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
2015 { "capture.req.uri", smp_fetch_capture_req_uri, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
2016 { "capture.req.ver", smp_fetch_capture_req_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
2017
2018 { "capture.res.hdr", smp_fetch_capture_res_hdr, ARG1(1,SINT), NULL, SMP_T_STR, SMP_USE_HRSHP },
2019 { "capture.res.ver", smp_fetch_capture_res_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
2020
2021 /* cookie is valid in both directions (eg: for "stick ...") but cook*
2022 * are only here to match the ACL's name, are request-only and are used
2023 * for ACL compatibility only.
2024 */
2025 { "cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Christopher Fauletc1f40dd2019-05-16 10:07:30 +02002026 { "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 +02002027 { "cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2028 { "cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2029
2030 /* hdr is valid in both directions (eg: for "stick ...") but hdr_* are
2031 * only here to match the ACL's name, are request-only and are used for
2032 * ACL compatibility only.
2033 */
Christopher Fauletc1f40dd2019-05-16 10:07:30 +02002034 { "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 +02002035 { "hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2036 { "hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV },
2037 { "hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_SINT, SMP_USE_HRQHV },
2038
Christopher Fauleta4063562019-08-02 11:51:37 +02002039 { "http_auth_type", smp_fetch_http_auth_type, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2040 { "http_auth_user", smp_fetch_http_auth_user, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2041 { "http_auth_pass", smp_fetch_http_auth_pass, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau79e57332018-10-02 16:01:16 +02002042 { "http_auth", smp_fetch_http_auth, ARG1(1,USR), NULL, SMP_T_BOOL, SMP_USE_HRQHV },
2043 { "http_auth_group", smp_fetch_http_auth_grp, ARG1(1,USR), NULL, SMP_T_STR, SMP_USE_HRQHV },
2044 { "http_first_req", smp_fetch_http_first_req, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
2045 { "method", smp_fetch_meth, 0, NULL, SMP_T_METH, SMP_USE_HRQHP },
2046 { "path", smp_fetch_path, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2047 { "query", smp_fetch_query, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2048
2049 /* HTTP protocol on the request path */
2050 { "req.proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
2051 { "req_proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
2052
2053 /* HTTP version on the request path */
2054 { "req.ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2055 { "req_ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2056
2057 { "req.body", smp_fetch_body, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
2058 { "req.body_len", smp_fetch_body_len, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
2059 { "req.body_size", smp_fetch_body_size, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
2060 { "req.body_param", smp_fetch_body_param, ARG1(0,STR), NULL, SMP_T_BIN, SMP_USE_HRQHV },
2061
2062 { "req.hdrs", smp_fetch_hdrs, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
2063 { "req.hdrs_bin", smp_fetch_hdrs_bin, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
2064
2065 /* HTTP version on the response path */
2066 { "res.ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
2067 { "resp_ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
2068
2069 /* explicit req.{cook,hdr} are used to force the fetch direction to be request-only */
2070 { "req.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
2071 { "req.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2072 { "req.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2073
2074 { "req.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
2075 { "req.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2076 { "req.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
2077 { "req.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2078 { "req.hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV },
2079 { "req.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
2080 { "req.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_SINT, SMP_USE_HRQHV },
2081
2082 /* explicit req.{cook,hdr} are used to force the fetch direction to be response-only */
2083 { "res.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
2084 { "res.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2085 { "res.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2086
2087 { "res.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
2088 { "res.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2089 { "res.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
2090 { "res.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2091 { "res.hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV },
2092 { "res.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
2093 { "res.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_SINT, SMP_USE_HRSHV },
2094
2095 /* scook is valid only on the response and is used for ACL compatibility */
2096 { "scook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
2097 { "scook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2098 { "scook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2099 { "set-cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV }, /* deprecated */
2100
2101 /* shdr is valid only on the response and is used for ACL compatibility */
2102 { "shdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
2103 { "shdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2104 { "shdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV },
2105 { "shdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_SINT, SMP_USE_HRSHV },
2106
2107 { "status", smp_fetch_stcode, 0, NULL, SMP_T_SINT, SMP_USE_HRSHP },
2108 { "unique-id", smp_fetch_uniqueid, 0, NULL, SMP_T_STR, SMP_SRC_L4SRV },
2109 { "url", smp_fetch_url, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2110 { "url32", smp_fetch_url32, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
2111 { "url32+src", smp_fetch_url32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
2112 { "url_ip", smp_fetch_url_ip, 0, NULL, SMP_T_IPV4, SMP_USE_HRQHV },
2113 { "url_port", smp_fetch_url_port, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
2114 { "url_param", smp_fetch_url_param, ARG2(0,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
2115 { "urlp" , smp_fetch_url_param, ARG2(0,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
2116 { "urlp_val", smp_fetch_url_param_val, ARG2(0,STR,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2117 { /* END */ },
2118}};
2119
Willy Tarreau0108d902018-11-25 19:14:37 +01002120INITCALL1(STG_REGISTER, sample_register_fetches, &sample_fetch_keywords);
Willy Tarreau79e57332018-10-02 16:01:16 +02002121
2122/*
2123 * Local variables:
2124 * c-indent-level: 8
2125 * c-basic-offset: 8
2126 * End:
2127 */