blob: 96d44ab6072f0968a0aeeb0a6e80c3e0b4df6348 [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
Willy Tarreau4c7e4b72020-05-27 12:58:42 +020019#include <haproxy/api.h>
Willy Tarreau8d366972020-05-27 16:10:29 +020020#include <haproxy/base64.h>
Willy Tarreau79e57332018-10-02 16:01:16 +020021#include <common/chunk.h>
Willy Tarreauafba57a2018-12-11 13:44:24 +010022#include <common/h1.h>
Willy Tarreau79e57332018-10-02 16:01:16 +020023#include <common/http.h>
Willy Tarreaub96b77e2018-12-11 10:22:41 +010024#include <common/htx.h>
Willy Tarreau79e57332018-10-02 16:01:16 +020025#include <common/memory.h>
26#include <common/standard.h>
Willy Tarreaud6788052020-05-27 15:59:00 +020027#include <haproxy/version.h>
Willy Tarreau79e57332018-10-02 16:01:16 +020028
29#include <types/global.h>
30
31#include <proto/arg.h>
32#include <proto/auth.h>
Christopher Fauleteb2754b2019-07-16 14:49:01 +020033#include <proto/channel.h>
Willy Tarreau9a1efe12019-07-17 17:13:50 +020034#include <proto/connection.h>
Willy Tarreau79e57332018-10-02 16:01:16 +020035#include <proto/http_fetch.h>
Christopher Faulet53a899b2019-10-08 16:38:42 +020036#include <proto/h1_htx.h>
Christopher Fauletef453ed2018-10-24 21:39:27 +020037#include <proto/http_htx.h>
Willy Tarreau79e57332018-10-02 16:01:16 +020038#include <proto/log.h>
39#include <proto/obj_type.h>
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020040#include <proto/http_ana.h>
Willy Tarreau79e57332018-10-02 16:01:16 +020041#include <proto/sample.h>
42#include <proto/stream.h>
43
44
45/* this struct is used between calls to smp_fetch_hdr() or smp_fetch_cookie() */
Christopher Fauletef453ed2018-10-24 21:39:27 +020046static THREAD_LOCAL struct http_hdr_ctx static_http_hdr_ctx;
Richard Russo458eafb2019-07-31 11:45:56 -070047/* this is used to convert raw connection buffers to htx */
48static THREAD_LOCAL struct buffer static_raw_htx_chunk;
49static THREAD_LOCAL char *static_raw_htx_buf;
Christopher Fauletef453ed2018-10-24 21:39:27 +020050
Christopher Faulet89dc4992019-04-17 12:02:59 +020051#define SMP_REQ_CHN(smp) (smp->strm ? &smp->strm->req : NULL)
52#define SMP_RES_CHN(smp) (smp->strm ? &smp->strm->res : NULL)
Willy Tarreau79e57332018-10-02 16:01:16 +020053
Richard Russo458eafb2019-07-31 11:45:56 -070054/* This function returns the static htx chunk, where raw connections get
55 * converted to HTX as needed for samplxsing.
56 */
57struct buffer *get_raw_htx_chunk(void)
58{
59 chunk_reset(&static_raw_htx_chunk);
60 return &static_raw_htx_chunk;
61}
62
63static int alloc_raw_htx_chunk_per_thread()
64{
65 static_raw_htx_buf = malloc(global.tune.bufsize);
66 if (!static_raw_htx_buf)
67 return 0;
68 chunk_init(&static_raw_htx_chunk, static_raw_htx_buf, global.tune.bufsize);
69 return 1;
70}
71
72static void free_raw_htx_chunk_per_thread()
73{
74 free(static_raw_htx_buf);
75 static_raw_htx_buf = NULL;
76}
77
78REGISTER_PER_THREAD_ALLOC(alloc_raw_htx_chunk_per_thread);
79REGISTER_PER_THREAD_FREE(free_raw_htx_chunk_per_thread);
80
Willy Tarreau79e57332018-10-02 16:01:16 +020081/*
82 * Returns the data from Authorization header. Function may be called more
83 * than once so data is stored in txn->auth_data. When no header is found
84 * or auth method is unknown auth_method is set to HTTP_AUTH_WRONG to avoid
85 * searching again for something we are unable to find anyway. However, if
86 * the result if valid, the cache is not reused because we would risk to
87 * have the credentials overwritten by another stream in parallel.
Willy Tarreaueae83722020-04-29 11:52:51 +020088 * The caller is responsible for passing a sample with a valid stream/txn,
89 * and a valid htx.
Willy Tarreau79e57332018-10-02 16:01:16 +020090 */
91
Christopher Fauletcd761952019-07-15 13:58:29 +020092static int get_http_auth(struct sample *smp, struct htx *htx)
Willy Tarreau79e57332018-10-02 16:01:16 +020093{
Christopher Faulet311c7ea2018-10-24 21:41:55 +020094 struct stream *s = smp->strm;
Willy Tarreau79e57332018-10-02 16:01:16 +020095 struct http_txn *txn = s->txn;
Christopher Faulet6d1dd462019-07-15 14:36:03 +020096 struct http_hdr_ctx ctx = { .blk = NULL };
97 struct ist hdr;
Willy Tarreau79e57332018-10-02 16:01:16 +020098 struct buffer auth_method;
Christopher Faulet6d1dd462019-07-15 14:36:03 +020099 char *p;
Willy Tarreau79e57332018-10-02 16:01:16 +0200100 int len;
101
102#ifdef DEBUG_AUTH
103 printf("Auth for stream %p: %d\n", s, txn->auth.method);
104#endif
Willy Tarreau79e57332018-10-02 16:01:16 +0200105 if (txn->auth.method == HTTP_AUTH_WRONG)
106 return 0;
107
108 txn->auth.method = HTTP_AUTH_WRONG;
109
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200110 if (txn->flags & TX_USE_PX_CONN)
111 hdr = ist("Proxy-Authorization");
112 else
113 hdr = ist("Authorization");
Willy Tarreau79e57332018-10-02 16:01:16 +0200114
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200115 ctx.blk = NULL;
116 if (!http_find_header(htx, hdr, &ctx, 0))
117 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200118
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200119 p = memchr(ctx.value.ptr, ' ', ctx.value.len);
120 len = p - ctx.value.ptr;
121 if (!p || len <= 0)
122 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200123
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200124 if (chunk_initlen(&auth_method, ctx.value.ptr, 0, len) != 1)
125 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200126
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200127 chunk_initlen(&txn->auth.method_data, p + 1, 0, ctx.value.len - len - 1);
Willy Tarreau79e57332018-10-02 16:01:16 +0200128
129 if (!strncasecmp("Basic", auth_method.area, auth_method.data)) {
130 struct buffer *http_auth = get_trash_chunk();
131
132 len = base64dec(txn->auth.method_data.area,
133 txn->auth.method_data.data,
134 http_auth->area, global.tune.bufsize - 1);
135
136 if (len < 0)
137 return 0;
138
139
140 http_auth->area[len] = '\0';
141
142 p = strchr(http_auth->area, ':');
143
144 if (!p)
145 return 0;
146
147 txn->auth.user = http_auth->area;
148 *p = '\0';
149 txn->auth.pass = p+1;
150
151 txn->auth.method = HTTP_AUTH_BASIC;
152 return 1;
153 }
154
155 return 0;
156}
157
158/* This function ensures that the prerequisites for an L7 fetch are ready,
159 * which means that a request or response is ready. If some data is missing,
160 * a parsing attempt is made. This is useful in TCP-based ACLs which are able
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +0200161 * to extract data from L7. If <vol> is non-null during a prefetch, another
162 * test is made to ensure the required information is not gone.
Christopher Fauletef453ed2018-10-24 21:39:27 +0200163 *
164 * The function returns :
165 * NULL with SMP_F_MAY_CHANGE in the sample flags if some data is missing to
166 * decide whether or not an HTTP message is present ;
167 * NULL if the requested data cannot be fetched or if it is certain that
Willy Tarreaueae83722020-04-29 11:52:51 +0200168 * we'll never have any HTTP message there; this includes null strm or chn.
Christopher Fauletef453ed2018-10-24 21:39:27 +0200169 * The HTX message if ready
170 */
Christopher Faulet778f5ed2020-04-29 15:51:55 +0200171struct htx *smp_prefetch_htx(struct sample *smp, struct channel *chn, struct check *check, int vol)
Christopher Fauletef453ed2018-10-24 21:39:27 +0200172{
Christopher Fauletef453ed2018-10-24 21:39:27 +0200173 struct stream *s = smp->strm;
Christopher Fauletef453ed2018-10-24 21:39:27 +0200174 struct http_txn *txn = NULL;
175 struct htx *htx = NULL;
Christopher Faulet89dc4992019-04-17 12:02:59 +0200176 struct http_msg *msg;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100177 struct htx_sl *sl;
Christopher Fauletef453ed2018-10-24 21:39:27 +0200178
179 /* Note: it is possible that <s> is NULL when called before stream
180 * initialization (eg: tcp-request connection), so this function is the
181 * one responsible for guarding against this case for all HTTP users.
Christopher Faulet778f5ed2020-04-29 15:51:55 +0200182 *
183 * In the health check context, the stream and the channel must be NULL
184 * and <check> must be set. In this case, only the input buffer,
185 * corresponding to the response, is considered. It is the caller
186 * responsibility to provide <check>.
Christopher Fauletef453ed2018-10-24 21:39:27 +0200187 */
Christopher Faulet778f5ed2020-04-29 15:51:55 +0200188 BUG_ON(check && (s || chn));
189 if (!s || !chn) {
190 if (check) {
191 htx = htxbuf(&check->bi);
192
193 /* Analyse not yet started */
194 if (htx_is_empty(htx) || htx->first == -1)
195 return NULL;
196
197 sl = http_get_stline(htx);
198 if (vol && !sl) {
199 /* The start-line was already forwarded, it is too late to fetch anything */
200 return NULL;
201 }
202 goto end;
203 }
204
Christopher Fauletef453ed2018-10-24 21:39:27 +0200205 return NULL;
Christopher Faulet778f5ed2020-04-29 15:51:55 +0200206 }
Christopher Fauletef453ed2018-10-24 21:39:27 +0200207
208 if (!s->txn) {
209 if (unlikely(!http_alloc_txn(s)))
210 return NULL; /* not enough memory */
211 http_init_txn(s);
212 txn = s->txn;
213 }
Christopher Faulet89dc4992019-04-17 12:02:59 +0200214 txn = s->txn;
215 msg = (!(chn->flags & CF_ISRESP) ? &txn->req : &txn->rsp);
216 smp->data.type = SMP_T_BOOL;
Christopher Fauletef453ed2018-10-24 21:39:27 +0200217
Christopher Fauleteca88542019-04-03 10:12:42 +0200218 if (IS_HTX_STRM(s)) {
Christopher Faulet89dc4992019-04-17 12:02:59 +0200219 htx = htxbuf(&chn->buf);
Christopher Fauletef453ed2018-10-24 21:39:27 +0200220
Christopher Faulet89dc4992019-04-17 12:02:59 +0200221 if (msg->msg_state == HTTP_MSG_ERROR || (htx->flags & HTX_FL_PARSING_ERROR))
222 return NULL;
Christopher Fauletef453ed2018-10-24 21:39:27 +0200223
Christopher Faulet89dc4992019-04-17 12:02:59 +0200224 if (msg->msg_state < HTTP_MSG_BODY) {
225 /* Analyse not yet started */
Christopher Faulet29f17582019-05-23 11:03:26 +0200226 if (htx_is_empty(htx) || htx->first == -1) {
Christopher Fauletef453ed2018-10-24 21:39:27 +0200227 /* Parsing is done by the mux, just wait */
228 smp->flags |= SMP_F_MAY_CHANGE;
229 return NULL;
230 }
231 }
Christopher Faulet297fbb42019-05-13 14:41:27 +0200232 sl = http_get_stline(htx);
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +0200233 if (vol && !sl) {
Christopher Faulet89dc4992019-04-17 12:02:59 +0200234 /* The start-line was already forwarded, it is too late to fetch anything */
235 return NULL;
236 }
Christopher Fauletef453ed2018-10-24 21:39:27 +0200237 }
Christopher Fauleteca88542019-04-03 10:12:42 +0200238 else { /* RAW mode */
Christopher Faulet89dc4992019-04-17 12:02:59 +0200239 struct buffer *buf;
240 struct h1m h1m;
Christopher Faulete4ab11b2019-06-11 15:05:37 +0200241 struct http_hdr hdrs[global.tune.max_http_hdr];
Christopher Faulet89dc4992019-04-17 12:02:59 +0200242 union h1_sl h1sl;
243 unsigned int flags = HTX_FL_NONE;
244 int ret;
Christopher Fauletef453ed2018-10-24 21:39:27 +0200245
Christopher Faulet89dc4992019-04-17 12:02:59 +0200246 /* no HTTP fetch on the response in TCP mode */
247 if (chn->flags & CF_ISRESP)
248 return NULL;
Christopher Fauletef453ed2018-10-24 21:39:27 +0200249
Christopher Faulet89dc4992019-04-17 12:02:59 +0200250 /* Now we are working on the request only */
251 buf = &chn->buf;
252 if (b_head(buf) + b_data(buf) > b_wrap(buf))
253 b_slow_realign(buf, trash.area, 0);
Christopher Fauletef453ed2018-10-24 21:39:27 +0200254
Christopher Faulet89dc4992019-04-17 12:02:59 +0200255 h1m_init_req(&h1m);
256 ret = h1_headers_to_hdr_list(b_head(buf), b_stop(buf),
257 hdrs, sizeof(hdrs)/sizeof(hdrs[0]), &h1m, &h1sl);
258 if (ret <= 0) {
259 /* Invalid or too big*/
260 if (ret < 0 || channel_full(&s->req, global.tune.maxrewrite))
Christopher Fauletef453ed2018-10-24 21:39:27 +0200261 return NULL;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100262
Christopher Faulet89dc4992019-04-17 12:02:59 +0200263 /* wait for a full request */
264 smp->flags |= SMP_F_MAY_CHANGE;
265 return NULL;
266 }
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100267
Christopher Faulet89dc4992019-04-17 12:02:59 +0200268 /* OK we just got a valid HTTP mesage. We have to convert it
269 * into an HTX message.
270 */
271 if (unlikely(h1sl.rq.v.len == 0)) {
272 /* try to convert HTTP/0.9 requests to HTTP/1.0 */
273 if (h1sl.rq.meth != HTTP_METH_GET || !h1sl.rq.u.len)
Christopher Fauletef453ed2018-10-24 21:39:27 +0200274 return NULL;
Christopher Faulet89dc4992019-04-17 12:02:59 +0200275 h1sl.rq.v = ist("HTTP/1.0");
Christopher Fauletef453ed2018-10-24 21:39:27 +0200276 }
Christopher Faulet89dc4992019-04-17 12:02:59 +0200277
278 /* Set HTX start-line flags */
279 if (h1m.flags & H1_MF_VER_11)
280 flags |= HTX_SL_F_VER_11;
281 if (h1m.flags & H1_MF_XFER_ENC)
282 flags |= HTX_SL_F_XFER_ENC;
283 flags |= HTX_SL_F_XFER_LEN;
284 if (h1m.flags & H1_MF_CHNK)
285 flags |= HTX_SL_F_CHNK;
286 else if (h1m.flags & H1_MF_CLEN)
287 flags |= HTX_SL_F_CLEN;
288
Richard Russo458eafb2019-07-31 11:45:56 -0700289 htx = htx_from_buf(get_raw_htx_chunk());
Christopher Faulet89dc4992019-04-17 12:02:59 +0200290 sl = htx_add_stline(htx, HTX_BLK_REQ_SL, flags, h1sl.rq.m, h1sl.rq.u, h1sl.rq.v);
291 if (!sl || !htx_add_all_headers(htx, hdrs))
Christopher Fauletef453ed2018-10-24 21:39:27 +0200292 return NULL;
Willy Tarreauce9bbf52019-05-13 08:32:31 +0200293 sl->info.req.meth = h1sl.rq.meth;
Christopher Faulet89dc4992019-04-17 12:02:59 +0200294 }
295
296 /* OK we just got a valid HTTP message. If not already done by
297 * HTTP analyzers, we have some minor preparation to perform so
298 * that further checks can rely on HTTP tests.
299 */
300 if (sl && msg->msg_state < HTTP_MSG_BODY) {
301 if (!(chn->flags & CF_ISRESP)) {
302 txn->meth = sl->info.req.meth;
303 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
304 s->flags |= SF_REDIRECTABLE;
Christopher Fauletef453ed2018-10-24 21:39:27 +0200305 }
Christopher Faulet89dc4992019-04-17 12:02:59 +0200306 else
307 txn->status = sl->info.res.status;
308 if (sl->flags & HTX_SL_F_VER_11)
309 msg->flags |= HTTP_MSGF_VER_11;
Christopher Fauletef453ed2018-10-24 21:39:27 +0200310 }
311
312 /* everything's OK */
Christopher Faulet778f5ed2020-04-29 15:51:55 +0200313 end:
Christopher Fauletef453ed2018-10-24 21:39:27 +0200314 smp->data.u.sint = 1;
315 return htx;
316}
317
Willy Tarreau79e57332018-10-02 16:01:16 +0200318/* This function fetches the method of current HTTP request and stores
319 * it in the global pattern struct as a chunk. There are two possibilities :
320 * - if the method is known (not HTTP_METH_OTHER), its identifier is stored
321 * in <len> and <ptr> is NULL ;
322 * - if the method is unknown (HTTP_METH_OTHER), <ptr> points to the text and
323 * <len> to its length.
324 * This is intended to be used with pat_match_meth() only.
325 */
326static int smp_fetch_meth(const struct arg *args, struct sample *smp, const char *kw, void *private)
327{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200328 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +0200329 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 0);
Willy Tarreau79e57332018-10-02 16:01:16 +0200330 struct http_txn *txn;
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200331 int meth;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200332
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200333 if (!htx)
334 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200335
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200336 txn = smp->strm->txn;
337 meth = txn->meth;
338 smp->data.type = SMP_T_METH;
339 smp->data.u.meth.meth = meth;
340 if (meth == HTTP_METH_OTHER) {
341 struct htx_sl *sl;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200342
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200343 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) {
344 /* ensure the indexes are not affected */
345 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200346 }
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200347 sl = http_get_stline(htx);
348 smp->flags |= SMP_F_CONST;
349 smp->data.u.meth.str.area = HTX_SL_REQ_MPTR(sl);
350 smp->data.u.meth.str.data = HTX_SL_REQ_MLEN(sl);
Willy Tarreau79e57332018-10-02 16:01:16 +0200351 }
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200352 smp->flags |= SMP_F_VOL_1ST;
Willy Tarreau79e57332018-10-02 16:01:16 +0200353 return 1;
354}
355
356static int smp_fetch_rqver(const struct arg *args, struct sample *smp, const char *kw, void *private)
357{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200358 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +0200359 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200360 struct htx_sl *sl;
Willy Tarreau79e57332018-10-02 16:01:16 +0200361 char *ptr;
362 int len;
363
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200364 if (!htx)
365 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200366
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200367 sl = http_get_stline(htx);
368 len = HTX_SL_REQ_VLEN(sl);
369 ptr = HTX_SL_REQ_VPTR(sl);
Willy Tarreau79e57332018-10-02 16:01:16 +0200370
371 while ((len-- > 0) && (*ptr++ != '/'));
372 if (len <= 0)
373 return 0;
374
375 smp->data.type = SMP_T_STR;
376 smp->data.u.str.area = ptr;
377 smp->data.u.str.data = len;
378
379 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
380 return 1;
381}
382
383static int smp_fetch_stver(const struct arg *args, struct sample *smp, const char *kw, void *private)
384{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200385 struct channel *chn = SMP_RES_CHN(smp);
Christopher Fauletf98e6262020-05-06 09:42:04 +0200386 struct check *check = objt_check(smp->sess->origin);
Christopher Faulet16032ab2020-04-30 11:30:00 +0200387 struct htx *htx = smp_prefetch_htx(smp, chn, check, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200388 struct htx_sl *sl;
Willy Tarreau79e57332018-10-02 16:01:16 +0200389 char *ptr;
390 int len;
391
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200392 if (!htx)
393 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200394
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200395 sl = http_get_stline(htx);
396 len = HTX_SL_RES_VLEN(sl);
397 ptr = HTX_SL_RES_VPTR(sl);
Willy Tarreau79e57332018-10-02 16:01:16 +0200398
399 while ((len-- > 0) && (*ptr++ != '/'));
400 if (len <= 0)
401 return 0;
402
403 smp->data.type = SMP_T_STR;
404 smp->data.u.str.area = ptr;
405 smp->data.u.str.data = len;
406
407 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
408 return 1;
409}
410
411/* 3. Check on Status Code. We manipulate integers here. */
412static int smp_fetch_stcode(const struct arg *args, struct sample *smp, const char *kw, void *private)
413{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200414 struct channel *chn = SMP_RES_CHN(smp);
Christopher Fauletf98e6262020-05-06 09:42:04 +0200415 struct check *check = objt_check(smp->sess->origin);
Christopher Faulet16032ab2020-04-30 11:30:00 +0200416 struct htx *htx = smp_prefetch_htx(smp, chn, check, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200417 struct htx_sl *sl;
Willy Tarreau79e57332018-10-02 16:01:16 +0200418 char *ptr;
419 int len;
420
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200421 if (!htx)
422 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200423
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200424 sl = http_get_stline(htx);
425 len = HTX_SL_RES_CLEN(sl);
426 ptr = HTX_SL_RES_CPTR(sl);
Willy Tarreau79e57332018-10-02 16:01:16 +0200427
428 smp->data.type = SMP_T_SINT;
429 smp->data.u.sint = __strl2ui(ptr, len);
430 smp->flags = SMP_F_VOL_1ST;
431 return 1;
432}
433
434static int smp_fetch_uniqueid(const struct arg *args, struct sample *smp, const char *kw, void *private)
435{
Tim Duesterhusa17e6622020-03-05 20:19:02 +0100436 struct ist unique_id;
Tim Duesterhus2825b4b2020-02-28 15:13:34 +0100437
Willy Tarreau79e57332018-10-02 16:01:16 +0200438 if (LIST_ISEMPTY(&smp->sess->fe->format_unique_id))
439 return 0;
440
Willy Tarreaua1062a42020-04-29 11:50:38 +0200441 if (!smp->strm)
442 return 0;
443
Tim Duesterhusa17e6622020-03-05 20:19:02 +0100444 unique_id = stream_generate_unique_id(smp->strm, &smp->sess->fe->format_unique_id);
445 if (!isttest(unique_id))
Tim Duesterhus2825b4b2020-02-28 15:13:34 +0100446 return 0;
447
Tim Duesterhusa17e6622020-03-05 20:19:02 +0100448 smp->data.u.str.area = smp->strm->unique_id.ptr;
449 smp->data.u.str.data = smp->strm->unique_id.len;
Tim Duesterhus2825b4b2020-02-28 15:13:34 +0100450 smp->data.type = SMP_T_STR;
Willy Tarreau79e57332018-10-02 16:01:16 +0200451 smp->flags = SMP_F_CONST;
452 return 1;
453}
454
455/* Returns a string block containing all headers including the
Joseph Herlant942eea32018-11-15 13:57:22 -0800456 * empty line which separes headers from the body. This is useful
457 * for some headers analysis.
Willy Tarreau79e57332018-10-02 16:01:16 +0200458 */
459static int smp_fetch_hdrs(const struct arg *args, struct sample *smp, const char *kw, void *private)
460{
Christopher Faulete596d182020-05-05 17:46:34 +0200461 /* possible keywords: req.hdrs, res.hdrs */
462 struct channel *chn = ((kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Christopher Fauletf98e6262020-05-06 09:42:04 +0200463 struct check *check = ((kw[2] == 's') ? objt_check(smp->sess->origin) : NULL);
Christopher Faulet16032ab2020-04-30 11:30:00 +0200464 struct htx *htx = smp_prefetch_htx(smp, chn, check, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200465 struct buffer *temp;
466 int32_t pos;
Willy Tarreau79e57332018-10-02 16:01:16 +0200467
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200468 if (!htx)
469 return 0;
470 temp = get_trash_chunk();
471 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
472 struct htx_blk *blk = htx_get_blk(htx, pos);
473 enum htx_blk_type type = htx_get_blk_type(blk);
Willy Tarreau79e57332018-10-02 16:01:16 +0200474
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200475 if (type == HTX_BLK_HDR) {
476 struct ist n = htx_get_blk_name(htx, blk);
477 struct ist v = htx_get_blk_value(htx, blk);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200478
Christopher Faulet53a899b2019-10-08 16:38:42 +0200479 if (!h1_format_htx_hdr(n, v, temp))
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200480 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200481 }
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200482 else if (type == HTX_BLK_EOH) {
483 if (!chunk_memcat(temp, "\r\n", 2))
484 return 0;
485 break;
486 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200487 }
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200488 smp->data.type = SMP_T_STR;
489 smp->data.u.str = *temp;
Willy Tarreau79e57332018-10-02 16:01:16 +0200490 return 1;
491}
492
493/* Returns the header request in a length/value encoded format.
494 * This is useful for exchanges with the SPOE.
495 *
496 * A "length value" is a multibyte code encoding numbers. It uses the
497 * SPOE format. The encoding is the following:
498 *
499 * Each couple "header name" / "header value" is composed
500 * like this:
501 * "length value" "header name bytes"
502 * "length value" "header value bytes"
503 * When the last header is reached, the header name and the header
504 * value are empty. Their length are 0
505 */
506static int smp_fetch_hdrs_bin(const struct arg *args, struct sample *smp, const char *kw, void *private)
507{
Christopher Faulete596d182020-05-05 17:46:34 +0200508 /* possible keywords: req.hdrs_bin, res.hdrs_bin */
509 struct channel *chn = ((kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Christopher Fauletf98e6262020-05-06 09:42:04 +0200510 struct check *check = ((kw[2] == 's') ? objt_check(smp->sess->origin) : NULL);
Christopher Faulet16032ab2020-04-30 11:30:00 +0200511 struct htx *htx = smp_prefetch_htx(smp, chn, check, 1);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200512 struct buffer *temp;
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200513 char *p, *end;
514 int32_t pos;
515 int ret;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200516
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200517 if (!htx)
518 return 0;
519 temp = get_trash_chunk();
520 p = temp->area;
521 end = temp->area + temp->size;
522 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
523 struct htx_blk *blk = htx_get_blk(htx, pos);
524 enum htx_blk_type type = htx_get_blk_type(blk);
525 struct ist n, v;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200526
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200527 if (type == HTX_BLK_HDR) {
528 n = htx_get_blk_name(htx,blk);
529 v = htx_get_blk_value(htx, blk);
Willy Tarreau79e57332018-10-02 16:01:16 +0200530
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200531 /* encode the header name. */
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200532 ret = encode_varint(n.len, &p, end);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200533 if (ret == -1)
534 return 0;
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200535 if (p + n.len > end)
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200536 return 0;
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200537 memcpy(p, n.ptr, n.len);
538 p += n.len;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200539
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200540 /* encode the header value. */
541 ret = encode_varint(v.len, &p, end);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200542 if (ret == -1)
543 return 0;
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200544 if (p + v.len > end)
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200545 return 0;
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200546 memcpy(p, v.ptr, v.len);
547 p += v.len;
Willy Tarreau79e57332018-10-02 16:01:16 +0200548
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200549 }
550 else if (type == HTX_BLK_EOH) {
551 /* encode the end of the header list with empty
552 * header name and header value.
553 */
554 ret = encode_varint(0, &p, end);
555 if (ret == -1)
556 return 0;
557 ret = encode_varint(0, &p, end);
558 if (ret == -1)
559 return 0;
560 break;
561 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200562 }
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200563
564 /* Initialise sample data which will be filled. */
565 smp->data.type = SMP_T_BIN;
566 smp->data.u.str.area = temp->area;
567 smp->data.u.str.data = p - temp->area;
568 smp->data.u.str.size = temp->size;
Willy Tarreau79e57332018-10-02 16:01:16 +0200569 return 1;
570}
571
572/* returns the longest available part of the body. This requires that the body
573 * has been waited for using http-buffer-request.
574 */
575static int smp_fetch_body(const struct arg *args, struct sample *smp, const char *kw, void *private)
576{
Christopher Faulete596d182020-05-05 17:46:34 +0200577 /* possible keywords: req.body, res.body */
578 struct channel *chn = ((kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Christopher Fauletf98e6262020-05-06 09:42:04 +0200579 struct check *check = ((kw[2] == 's') ? objt_check(smp->sess->origin) : NULL);
Christopher Faulet16032ab2020-04-30 11:30:00 +0200580 struct htx *htx = smp_prefetch_htx(smp, chn, check, 1);
Willy Tarreau79e57332018-10-02 16:01:16 +0200581 struct buffer *temp;
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200582 int32_t pos;
Willy Tarreau79e57332018-10-02 16:01:16 +0200583
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200584 if (!htx)
585 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200586
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200587 temp = get_trash_chunk();
588 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
589 struct htx_blk *blk = htx_get_blk(htx, pos);
590 enum htx_blk_type type = htx_get_blk_type(blk);
Willy Tarreau79e57332018-10-02 16:01:16 +0200591
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200592 if (type == HTX_BLK_EOM || type == HTX_BLK_TLR || type == HTX_BLK_EOT)
593 break;
594 if (type == HTX_BLK_DATA) {
Christopher Faulet53a899b2019-10-08 16:38:42 +0200595 if (!h1_format_htx_data(htx_get_blk_value(htx, blk), temp, 0))
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200596 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200597 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200598 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200599
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200600 smp->data.type = SMP_T_BIN;
601 smp->data.u.str = *temp;
602 smp->flags = SMP_F_VOL_TEST;
Willy Tarreau79e57332018-10-02 16:01:16 +0200603 return 1;
604}
605
606
607/* returns the available length of the body. This requires that the body
608 * has been waited for using http-buffer-request.
609 */
610static int smp_fetch_body_len(const struct arg *args, struct sample *smp, const char *kw, void *private)
611{
Christopher Faulete596d182020-05-05 17:46:34 +0200612 /* possible keywords: req.body_len, res.body_len */
613 struct channel *chn = ((kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Christopher Fauletf98e6262020-05-06 09:42:04 +0200614 struct check *check = ((kw[2] == 's') ? objt_check(smp->sess->origin) : NULL);
Christopher Faulet16032ab2020-04-30 11:30:00 +0200615 struct htx *htx = smp_prefetch_htx(smp, chn, check, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200616 int32_t pos;
617 unsigned long long len = 0;
Christopher Fauletc16317d2018-12-12 14:11:22 +0100618
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200619 if (!htx)
620 return 0;
Christopher Fauletc16317d2018-12-12 14:11:22 +0100621
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200622 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
623 struct htx_blk *blk = htx_get_blk(htx, pos);
624 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Fauletc16317d2018-12-12 14:11:22 +0100625
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200626 if (type == HTX_BLK_EOM || type == HTX_BLK_TLR || type == HTX_BLK_EOT)
627 break;
628 if (type == HTX_BLK_DATA)
629 len += htx_get_blksz(blk);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200630 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200631
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200632 smp->data.type = SMP_T_SINT;
633 smp->data.u.sint = len;
634 smp->flags = SMP_F_VOL_TEST;
Willy Tarreau79e57332018-10-02 16:01:16 +0200635 return 1;
636}
637
638
639/* returns the advertised length of the body, or the advertised size of the
640 * chunks available in the buffer. This requires that the body has been waited
641 * for using http-buffer-request.
642 */
643static int smp_fetch_body_size(const struct arg *args, struct sample *smp, const char *kw, void *private)
644{
Christopher Faulete596d182020-05-05 17:46:34 +0200645 /* possible keywords: req.body_size, res.body_size */
646 struct channel *chn = ((kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Christopher Fauletf98e6262020-05-06 09:42:04 +0200647 struct check *check = ((kw[2] == 's') ? objt_check(smp->sess->origin) : NULL);
Christopher Faulet16032ab2020-04-30 11:30:00 +0200648 struct htx *htx = smp_prefetch_htx(smp, chn, check, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200649 int32_t pos;
650 unsigned long long len = 0;
Christopher Faulet89dc4992019-04-17 12:02:59 +0200651
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200652 if (!htx)
653 return 0;
Christopher Fauletc16317d2018-12-12 14:11:22 +0100654
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200655 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
656 struct htx_blk *blk = htx_get_blk(htx, pos);
657 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Fauletc16317d2018-12-12 14:11:22 +0100658
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200659 if (type == HTX_BLK_EOM || type == HTX_BLK_TLR || type == HTX_BLK_EOT)
660 break;
661 if (type == HTX_BLK_DATA)
662 len += htx_get_blksz(blk);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200663 }
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200664 if (htx->extra != ULLONG_MAX)
665 len += htx->extra;
Willy Tarreau79e57332018-10-02 16:01:16 +0200666
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200667 smp->data.type = SMP_T_SINT;
668 smp->data.u.sint = len;
669 smp->flags = SMP_F_VOL_TEST;
Willy Tarreau79e57332018-10-02 16:01:16 +0200670 return 1;
671}
672
673
674/* 4. Check on URL/URI. A pointer to the URI is stored. */
675static int smp_fetch_url(const struct arg *args, struct sample *smp, const char *kw, void *private)
676{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200677 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +0200678 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200679 struct htx_sl *sl;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200680
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200681 if (!htx)
682 return 0;
683 sl = http_get_stline(htx);
684 smp->data.type = SMP_T_STR;
685 smp->data.u.str.area = HTX_SL_REQ_UPTR(sl);
686 smp->data.u.str.data = HTX_SL_REQ_ULEN(sl);
687 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau79e57332018-10-02 16:01:16 +0200688 return 1;
689}
690
691static int smp_fetch_url_ip(const struct arg *args, struct sample *smp, const char *kw, void *private)
692{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200693 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +0200694 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200695 struct htx_sl *sl;
Willy Tarreau79e57332018-10-02 16:01:16 +0200696 struct sockaddr_storage addr;
697
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200698 if (!htx)
699 return 0;
700 sl = http_get_stline(htx);
701 url2sa(HTX_SL_REQ_UPTR(sl), HTX_SL_REQ_ULEN(sl), &addr, NULL);
Willy Tarreau79e57332018-10-02 16:01:16 +0200702
Willy Tarreau79e57332018-10-02 16:01:16 +0200703 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
704 return 0;
705
706 smp->data.type = SMP_T_IPV4;
707 smp->data.u.ipv4 = ((struct sockaddr_in *)&addr)->sin_addr;
708 smp->flags = 0;
709 return 1;
710}
711
712static int smp_fetch_url_port(const struct arg *args, struct sample *smp, const char *kw, void *private)
713{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200714 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +0200715 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200716 struct htx_sl *sl;
Willy Tarreau79e57332018-10-02 16:01:16 +0200717 struct sockaddr_storage addr;
718
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200719 if (!htx)
720 return 0;
721 sl = http_get_stline(htx);
722 url2sa(HTX_SL_REQ_UPTR(sl), HTX_SL_REQ_ULEN(sl), &addr, NULL);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200723
Willy Tarreau79e57332018-10-02 16:01:16 +0200724 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
725 return 0;
726
727 smp->data.type = SMP_T_SINT;
728 smp->data.u.sint = ntohs(((struct sockaddr_in *)&addr)->sin_port);
729 smp->flags = 0;
730 return 1;
731}
732
733/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
734 * Accepts an optional argument of type string containing the header field name,
735 * and an optional argument of type signed or unsigned integer to request an
736 * explicit occurrence of the header. Note that in the event of a missing name,
737 * headers are considered from the first one. It does not stop on commas and
738 * returns full lines instead (useful for User-Agent or Date for example).
739 */
740static int smp_fetch_fhdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
741{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200742 /* possible keywords: req.fhdr, res.fhdr */
743 struct channel *chn = ((kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Christopher Fauletf98e6262020-05-06 09:42:04 +0200744 struct check *check = ((kw[2] == 's') ? objt_check(smp->sess->origin) : NULL);
Christopher Faulet16032ab2020-04-30 11:30:00 +0200745 struct htx *htx = smp_prefetch_htx(smp, chn, check, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200746 struct http_hdr_ctx *ctx = smp->ctx.a[0];
747 struct ist name;
Willy Tarreau79e57332018-10-02 16:01:16 +0200748 int occ = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200749
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200750 if (!ctx) {
751 /* first call */
752 ctx = &static_http_hdr_ctx;
753 ctx->blk = NULL;
754 smp->ctx.a[0] = ctx;
755 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200756
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200757 if (args) {
758 if (args[0].type != ARGT_STR)
Willy Tarreau79e57332018-10-02 16:01:16 +0200759 return 0;
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200760 name.ptr = args[0].data.str.area;
761 name.len = args[0].data.str.data;
Willy Tarreau79e57332018-10-02 16:01:16 +0200762
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200763 if (args[1].type == ARGT_SINT)
764 occ = args[1].data.sint;
Willy Tarreau79e57332018-10-02 16:01:16 +0200765 }
766
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200767 if (!htx)
768 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200769
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200770 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
771 /* search for header from the beginning */
772 ctx->blk = NULL;
Willy Tarreau79e57332018-10-02 16:01:16 +0200773
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200774 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
775 /* no explicit occurrence and single fetch => last header by default */
776 occ = -1;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200777
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200778 if (!occ)
779 /* prepare to report multiple occurrences for ACL fetches */
780 smp->flags |= SMP_F_NOT_LAST;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200781
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200782 smp->data.type = SMP_T_STR;
783 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
784 if (http_get_htx_fhdr(htx, name, occ, ctx, &smp->data.u.str.area, &smp->data.u.str.data))
785 return 1;
Willy Tarreau79e57332018-10-02 16:01:16 +0200786 smp->flags &= ~SMP_F_NOT_LAST;
787 return 0;
788}
789
790/* 6. Check on HTTP header count. The number of occurrences is returned.
791 * Accepts exactly 1 argument of type string. It does not stop on commas and
792 * returns full lines instead (useful for User-Agent or Date for example).
793 */
794static int smp_fetch_fhdr_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
795{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200796 /* possible keywords: req.fhdr_cnt, res.fhdr_cnt */
797 struct channel *chn = ((kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Christopher Fauletf98e6262020-05-06 09:42:04 +0200798 struct check *check = ((kw[2] == 's') ? objt_check(smp->sess->origin) : NULL);
Christopher Faulet16032ab2020-04-30 11:30:00 +0200799 struct htx *htx = smp_prefetch_htx(smp, chn, check, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200800 struct http_hdr_ctx ctx;
801 struct ist name;
Willy Tarreau79e57332018-10-02 16:01:16 +0200802 int cnt;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200803
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200804 if (!htx)
805 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200806
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200807 if (args && args->type == ARGT_STR) {
808 name.ptr = args->data.str.area;
809 name.len = args->data.str.data;
810 } else {
811 name.ptr = NULL;
812 name.len = 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200813 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200814
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200815 ctx.blk = NULL;
816 cnt = 0;
817 while (http_find_header(htx, name, &ctx, 1))
818 cnt++;
Willy Tarreau79e57332018-10-02 16:01:16 +0200819 smp->data.type = SMP_T_SINT;
820 smp->data.u.sint = cnt;
821 smp->flags = SMP_F_VOL_HDR;
822 return 1;
823}
824
825static int smp_fetch_hdr_names(const struct arg *args, struct sample *smp, const char *kw, void *private)
826{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200827 /* possible keywords: req.hdr_names, res.hdr_names */
828 struct channel *chn = ((kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Christopher Fauletf98e6262020-05-06 09:42:04 +0200829 struct check *check = ((kw[2] == 's') ? objt_check(smp->sess->origin) : NULL);
Christopher Faulet16032ab2020-04-30 11:30:00 +0200830 struct htx *htx = smp_prefetch_htx(smp, chn, check, 1);
Willy Tarreau79e57332018-10-02 16:01:16 +0200831 struct buffer *temp;
832 char del = ',';
833
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200834 int32_t pos;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200835
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200836 if (!htx)
837 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200838
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200839 if (args && args->type == ARGT_STR)
840 del = *args[0].data.str.area;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200841
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200842 temp = get_trash_chunk();
843 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
844 struct htx_blk *blk = htx_get_blk(htx, pos);
845 enum htx_blk_type type = htx_get_blk_type(blk);
846 struct ist n;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200847
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200848 if (type == HTX_BLK_EOH)
849 break;
850 if (type != HTX_BLK_HDR)
851 continue;
852 n = htx_get_blk_name(htx, blk);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200853
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200854 if (temp->data)
855 temp->area[temp->data++] = del;
856 chunk_memcat(temp, n.ptr, n.len);
Willy Tarreau79e57332018-10-02 16:01:16 +0200857 }
858
859 smp->data.type = SMP_T_STR;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200860 smp->data.u.str = *temp;
Willy Tarreau79e57332018-10-02 16:01:16 +0200861 smp->flags = SMP_F_VOL_HDR;
862 return 1;
863}
864
865/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
866 * Accepts an optional argument of type string containing the header field name,
867 * and an optional argument of type signed or unsigned integer to request an
868 * explicit occurrence of the header. Note that in the event of a missing name,
869 * headers are considered from the first one.
870 */
871static int smp_fetch_hdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
872{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200873 /* possible keywords: req.hdr / hdr, res.hdr / shdr */
874 struct channel *chn = ((kw[0] == 'h' || kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Christopher Fauletf98e6262020-05-06 09:42:04 +0200875 struct check *check = ((kw[0] == 's' || kw[2] == 's') ? objt_check(smp->sess->origin) : NULL);
Christopher Faulet16032ab2020-04-30 11:30:00 +0200876 struct htx *htx = smp_prefetch_htx(smp, chn, check, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200877 struct http_hdr_ctx *ctx = smp->ctx.a[0];
878 struct ist name;
Willy Tarreau79e57332018-10-02 16:01:16 +0200879 int occ = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200880
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200881 if (!ctx) {
882 /* first call */
883 ctx = &static_http_hdr_ctx;
884 ctx->blk = NULL;
885 smp->ctx.a[0] = ctx;
886 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200887
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200888 if (args) {
889 if (args[0].type != ARGT_STR)
Willy Tarreau79e57332018-10-02 16:01:16 +0200890 return 0;
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200891 name.ptr = args[0].data.str.area;
892 name.len = args[0].data.str.data;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200893
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200894 if (args[1].type == ARGT_SINT)
895 occ = args[1].data.sint;
Willy Tarreau79e57332018-10-02 16:01:16 +0200896 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200897
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200898 if (!htx)
899 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200900
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200901 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
902 /* search for header from the beginning */
903 ctx->blk = NULL;
Willy Tarreau79e57332018-10-02 16:01:16 +0200904
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200905 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
906 /* no explicit occurrence and single fetch => last header by default */
907 occ = -1;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200908
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200909 if (!occ)
910 /* prepare to report multiple occurrences for ACL fetches */
911 smp->flags |= SMP_F_NOT_LAST;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200912
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200913 smp->data.type = SMP_T_STR;
914 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
915 if (http_get_htx_hdr(htx, name, occ, ctx, &smp->data.u.str.area, &smp->data.u.str.data))
916 return 1;
Willy Tarreau79e57332018-10-02 16:01:16 +0200917
918 smp->flags &= ~SMP_F_NOT_LAST;
919 return 0;
920}
921
Christopher Fauletc1f40dd2019-05-16 10:07:30 +0200922/* Same than smp_fetch_hdr() but only relies on the sample direction to choose
923 * the right channel. So instead of duplicating the code, we just change the
924 * keyword and then fallback on smp_fetch_hdr().
925 */
926static int smp_fetch_chn_hdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
927{
928 kw = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ ? "req.hdr" : "res.hdr");
929 return smp_fetch_hdr(args, smp, kw, private);
930}
931
Willy Tarreau79e57332018-10-02 16:01:16 +0200932/* 6. Check on HTTP header count. The number of occurrences is returned.
933 * Accepts exactly 1 argument of type string.
934 */
935static int smp_fetch_hdr_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
936{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200937 /* possible keywords: req.hdr_cnt / hdr_cnt, res.hdr_cnt / shdr_cnt */
938 struct channel *chn = ((kw[0] == 'h' || kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Christopher Fauletf98e6262020-05-06 09:42:04 +0200939 struct check *check = ((kw[0] == 's' || kw[2] == 's') ? objt_check(smp->sess->origin) : NULL);
Christopher Faulet16032ab2020-04-30 11:30:00 +0200940 struct htx *htx = smp_prefetch_htx(smp, chn, check, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200941 struct http_hdr_ctx ctx;
942 struct ist name;
Willy Tarreau79e57332018-10-02 16:01:16 +0200943 int cnt;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200944
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200945 if (!htx)
946 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200947
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200948 if (args && args->type == ARGT_STR) {
949 name.ptr = args->data.str.area;
950 name.len = args->data.str.data;
951 } else {
952 name.ptr = NULL;
953 name.len = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200954 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200955
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200956 ctx.blk = NULL;
957 cnt = 0;
958 while (http_find_header(htx, name, &ctx, 0))
959 cnt++;
Willy Tarreau79e57332018-10-02 16:01:16 +0200960
961 smp->data.type = SMP_T_SINT;
962 smp->data.u.sint = cnt;
963 smp->flags = SMP_F_VOL_HDR;
964 return 1;
965}
966
967/* Fetch an HTTP header's integer value. The integer value is returned. It
968 * takes a mandatory argument of type string and an optional one of type int
969 * to designate a specific occurrence. It returns an unsigned integer, which
970 * may or may not be appropriate for everything.
971 */
972static int smp_fetch_hdr_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
973{
974 int ret = smp_fetch_hdr(args, smp, kw, private);
975
976 if (ret > 0) {
977 smp->data.type = SMP_T_SINT;
978 smp->data.u.sint = strl2ic(smp->data.u.str.area,
979 smp->data.u.str.data);
980 }
981
982 return ret;
983}
984
985/* Fetch an HTTP header's IP value. takes a mandatory argument of type string
986 * and an optional one of type int to designate a specific occurrence.
987 * It returns an IPv4 or IPv6 address.
988 */
989static int smp_fetch_hdr_ip(const struct arg *args, struct sample *smp, const char *kw, void *private)
990{
991 int ret;
992
993 while ((ret = smp_fetch_hdr(args, smp, kw, private)) > 0) {
994 if (url2ipv4((char *) smp->data.u.str.area, &smp->data.u.ipv4)) {
995 smp->data.type = SMP_T_IPV4;
996 break;
997 } else {
998 struct buffer *temp = get_trash_chunk();
999 if (smp->data.u.str.data < temp->size - 1) {
1000 memcpy(temp->area, smp->data.u.str.area,
1001 smp->data.u.str.data);
1002 temp->area[smp->data.u.str.data] = '\0';
1003 if (inet_pton(AF_INET6, temp->area, &smp->data.u.ipv6)) {
1004 smp->data.type = SMP_T_IPV6;
1005 break;
1006 }
1007 }
1008 }
1009
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001010 /* if the header doesn't match an IP address, fetch next one */
1011 if (!(smp->flags & SMP_F_NOT_LAST))
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001012 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001013 }
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001014 return ret;
1015}
Willy Tarreau79e57332018-10-02 16:01:16 +02001016
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001017/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
1018 * the first '/' after the possible hostname, and ends before the possible '?'.
1019 */
1020static int smp_fetch_path(const struct arg *args, struct sample *smp, const char *kw, void *private)
1021{
1022 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +02001023 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001024 struct htx_sl *sl;
1025 struct ist path;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001026
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001027 if (!htx)
1028 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001029
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001030 sl = http_get_stline(htx);
Jerome Magnin4fb196c2020-02-21 10:49:12 +01001031 path = iststop(http_get_path(htx_sl_req_uri(sl)), '?');
Tim Duesterhused526372020-03-05 17:56:33 +01001032 if (!isttest(path))
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001033 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001034
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001035 /* OK, we got the '/' ! */
1036 smp->data.type = SMP_T_STR;
1037 smp->data.u.str.area = path.ptr;
Jerome Magnin4fb196c2020-02-21 10:49:12 +01001038 smp->data.u.str.data = path.len;
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001039 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau79e57332018-10-02 16:01:16 +02001040 return 1;
1041}
1042
1043/* This produces a concatenation of the first occurrence of the Host header
1044 * followed by the path component if it begins with a slash ('/'). This means
1045 * that '*' will not be added, resulting in exactly the first Host entry.
1046 * If no Host header is found, then the path is returned as-is. The returned
1047 * value is stored in the trash so it does not need to be marked constant.
1048 * The returned sample is of type string.
1049 */
1050static int smp_fetch_base(const struct arg *args, struct sample *smp, const char *kw, void *private)
1051{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001052 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +02001053 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001054 struct htx_sl *sl;
Willy Tarreau79e57332018-10-02 16:01:16 +02001055 struct buffer *temp;
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001056 struct http_hdr_ctx ctx;
1057 struct ist path;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001058
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001059 if (!htx)
1060 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001061
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001062 ctx.blk = NULL;
1063 if (!http_find_header(htx, ist("Host"), &ctx, 0) || !ctx.value.len)
1064 return smp_fetch_path(args, smp, kw, private);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001065
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001066 /* OK we have the header value in ctx.value */
1067 temp = get_trash_chunk();
1068 chunk_memcat(temp, ctx.value.ptr, ctx.value.len);
Willy Tarreau79e57332018-10-02 16:01:16 +02001069
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001070 /* now retrieve the path */
1071 sl = http_get_stline(htx);
1072 path = http_get_path(htx_sl_req_uri(sl));
Tim Duesterhused526372020-03-05 17:56:33 +01001073 if (isttest(path)) {
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001074 size_t len;
Willy Tarreau79e57332018-10-02 16:01:16 +02001075
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001076 for (len = 0; len < path.len && *(path.ptr + len) != '?'; len++)
1077 ;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001078
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001079 if (len && *(path.ptr) == '/')
1080 chunk_memcat(temp, path.ptr, len);
Willy Tarreau79e57332018-10-02 16:01:16 +02001081 }
1082
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001083 smp->data.type = SMP_T_STR;
1084 smp->data.u.str = *temp;
Willy Tarreau79e57332018-10-02 16:01:16 +02001085 smp->flags = SMP_F_VOL_1ST;
1086 return 1;
1087}
1088
1089/* This produces a 32-bit hash of the concatenation of the first occurrence of
1090 * the Host header followed by the path component if it begins with a slash ('/').
1091 * This means that '*' will not be added, resulting in exactly the first Host
1092 * entry. If no Host header is found, then the path is used. The resulting value
1093 * is hashed using the path hash followed by a full avalanche hash and provides a
1094 * 32-bit integer value. This fetch is useful for tracking per-path activity on
1095 * high-traffic sites without having to store whole paths.
1096 */
1097static int smp_fetch_base32(const struct arg *args, struct sample *smp, const char *kw, void *private)
1098{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001099 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +02001100 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001101 struct htx_sl *sl;
1102 struct http_hdr_ctx ctx;
1103 struct ist path;
Willy Tarreau79e57332018-10-02 16:01:16 +02001104 unsigned int hash = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001105
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001106 if (!htx)
1107 return 0;
Dragan Dosen8861e1c2019-02-12 19:50:31 +01001108
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001109 ctx.blk = NULL;
1110 if (http_find_header(htx, ist("Host"), &ctx, 0)) {
1111 /* OK we have the header value in ctx.value */
1112 while (ctx.value.len--)
1113 hash = *(ctx.value.ptr++) + (hash << 6) + (hash << 16) - hash;
Willy Tarreau79e57332018-10-02 16:01:16 +02001114 }
1115
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001116 /* now retrieve the path */
1117 sl = http_get_stline(htx);
1118 path = http_get_path(htx_sl_req_uri(sl));
Tim Duesterhused526372020-03-05 17:56:33 +01001119 if (isttest(path)) {
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001120 size_t len;
Willy Tarreau79e57332018-10-02 16:01:16 +02001121
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001122 for (len = 0; len < path.len && *(path.ptr + len) != '?'; len++)
1123 ;
Willy Tarreau79e57332018-10-02 16:01:16 +02001124
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001125 if (len && *(path.ptr) == '/') {
1126 while (len--)
1127 hash = *(path.ptr++) + (hash << 6) + (hash << 16) - hash;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001128 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001129 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001130
Willy Tarreau79e57332018-10-02 16:01:16 +02001131 hash = full_hash(hash);
1132
1133 smp->data.type = SMP_T_SINT;
1134 smp->data.u.sint = hash;
1135 smp->flags = SMP_F_VOL_1ST;
1136 return 1;
1137}
1138
1139/* This concatenates the source address with the 32-bit hash of the Host and
1140 * path as returned by smp_fetch_base32(). The idea is to have per-source and
1141 * per-path counters. The result is a binary block from 8 to 20 bytes depending
1142 * on the source address length. The path hash is stored before the address so
1143 * that in environments where IPv6 is insignificant, truncating the output to
1144 * 8 bytes would still work.
1145 */
1146static int smp_fetch_base32_src(const struct arg *args, struct sample *smp, const char *kw, void *private)
1147{
1148 struct buffer *temp;
1149 struct connection *cli_conn = objt_conn(smp->sess->origin);
1150
Willy Tarreaucd7ca792019-07-17 16:57:03 +02001151 if (!cli_conn || !conn_get_src(cli_conn))
Willy Tarreau79e57332018-10-02 16:01:16 +02001152 return 0;
1153
1154 if (!smp_fetch_base32(args, smp, kw, private))
1155 return 0;
1156
1157 temp = get_trash_chunk();
1158 *(unsigned int *) temp->area = htonl(smp->data.u.sint);
1159 temp->data += sizeof(unsigned int);
1160
Willy Tarreau9a1efe12019-07-17 17:13:50 +02001161 switch (cli_conn->src->ss_family) {
Willy Tarreau79e57332018-10-02 16:01:16 +02001162 case AF_INET:
1163 memcpy(temp->area + temp->data,
Willy Tarreau9a1efe12019-07-17 17:13:50 +02001164 &((struct sockaddr_in *)cli_conn->src)->sin_addr,
Willy Tarreau79e57332018-10-02 16:01:16 +02001165 4);
1166 temp->data += 4;
1167 break;
1168 case AF_INET6:
1169 memcpy(temp->area + temp->data,
Willy Tarreau9a1efe12019-07-17 17:13:50 +02001170 &((struct sockaddr_in6 *)cli_conn->src)->sin6_addr,
Willy Tarreau79e57332018-10-02 16:01:16 +02001171 16);
1172 temp->data += 16;
1173 break;
1174 default:
1175 return 0;
1176 }
1177
1178 smp->data.u.str = *temp;
1179 smp->data.type = SMP_T_BIN;
1180 return 1;
1181}
1182
1183/* Extracts the query string, which comes after the question mark '?'. If no
1184 * question mark is found, nothing is returned. Otherwise it returns a sample
1185 * of type string carrying the whole query string.
1186 */
1187static int smp_fetch_query(const struct arg *args, struct sample *smp, const char *kw, void *private)
1188{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001189 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +02001190 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001191 struct htx_sl *sl;
Willy Tarreau79e57332018-10-02 16:01:16 +02001192 char *ptr, *end;
1193
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001194 if (!htx)
1195 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001196
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001197 sl = http_get_stline(htx);
1198 ptr = HTX_SL_REQ_UPTR(sl);
1199 end = HTX_SL_REQ_UPTR(sl) + HTX_SL_REQ_ULEN(sl);
Willy Tarreau79e57332018-10-02 16:01:16 +02001200
1201 /* look up the '?' */
1202 do {
1203 if (ptr == end)
1204 return 0;
1205 } while (*ptr++ != '?');
1206
1207 smp->data.type = SMP_T_STR;
1208 smp->data.u.str.area = ptr;
1209 smp->data.u.str.data = end - ptr;
1210 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
1211 return 1;
1212}
1213
1214static int smp_fetch_proto_http(const struct arg *args, struct sample *smp, const char *kw, void *private)
1215{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001216 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +02001217 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 0);
Willy Tarreau79e57332018-10-02 16:01:16 +02001218
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001219 if (!htx)
1220 return 0;
1221 smp->data.type = SMP_T_BOOL;
Willy Tarreau79e57332018-10-02 16:01:16 +02001222 smp->data.u.sint = 1;
1223 return 1;
1224}
1225
1226/* return a valid test if the current request is the first one on the connection */
1227static int smp_fetch_http_first_req(const struct arg *args, struct sample *smp, const char *kw, void *private)
1228{
Willy Tarreau79512b62020-04-29 11:52:13 +02001229 if (!smp->strm)
1230 return 0;
1231
Willy Tarreau79e57332018-10-02 16:01:16 +02001232 smp->data.type = SMP_T_BOOL;
1233 smp->data.u.sint = !(smp->strm->txn->flags & TX_NOT_FIRST);
1234 return 1;
1235}
1236
Christopher Fauleta4063562019-08-02 11:51:37 +02001237/* Fetch the authentication method if there is an Authorization header. It
1238 * relies on get_http_auth()
1239 */
1240static int smp_fetch_http_auth_type(const struct arg *args, struct sample *smp, const char *kw, void *private)
1241{
1242 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +02001243 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Fauleta4063562019-08-02 11:51:37 +02001244 struct http_txn *txn;
1245
1246 if (!htx)
1247 return 0;
1248
1249 txn = smp->strm->txn;
1250 if (!get_http_auth(smp, htx))
1251 return 0;
1252
1253 switch (txn->auth.method) {
1254 case HTTP_AUTH_BASIC:
1255 smp->data.u.str.area = "Basic";
1256 smp->data.u.str.data = 5;
1257 break;
1258 case HTTP_AUTH_DIGEST:
1259 /* Unexpected because not supported */
1260 smp->data.u.str.area = "Digest";
1261 smp->data.u.str.data = 6;
1262 break;
1263 default:
1264 return 0;
1265 }
1266
1267 smp->data.type = SMP_T_STR;
1268 smp->flags = SMP_F_CONST;
1269 return 1;
1270}
1271
1272/* Fetch the user supplied if there is an Authorization header. It relies on
1273 * get_http_auth()
1274 */
1275static int smp_fetch_http_auth_user(const struct arg *args, struct sample *smp, const char *kw, void *private)
1276{
1277 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +02001278 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Fauleta4063562019-08-02 11:51:37 +02001279 struct http_txn *txn;
1280
1281 if (!htx)
1282 return 0;
1283
1284 txn = smp->strm->txn;
1285 if (!get_http_auth(smp, htx))
1286 return 0;
1287
1288 smp->data.type = SMP_T_STR;
1289 smp->data.u.str.area = txn->auth.user;
1290 smp->data.u.str.data = strlen(txn->auth.user);
1291 smp->flags = SMP_F_CONST;
1292 return 1;
1293}
1294
1295/* Fetch the password supplied if there is an Authorization header. It relies on
1296 * get_http_auth()
1297 */
1298static int smp_fetch_http_auth_pass(const struct arg *args, struct sample *smp, const char *kw, void *private)
1299{
1300 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +02001301 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Fauleta4063562019-08-02 11:51:37 +02001302 struct http_txn *txn;
1303
1304 if (!htx)
1305 return 0;
1306
1307 txn = smp->strm->txn;
1308 if (!get_http_auth(smp, htx))
1309 return 0;
1310
1311 smp->data.type = SMP_T_STR;
1312 smp->data.u.str.area = txn->auth.pass;
1313 smp->data.u.str.data = strlen(txn->auth.pass);
1314 smp->flags = SMP_F_CONST;
1315 return 1;
1316}
1317
Willy Tarreau79e57332018-10-02 16:01:16 +02001318/* Accepts exactly 1 argument of type userlist */
1319static int smp_fetch_http_auth(const struct arg *args, struct sample *smp, const char *kw, void *private)
1320{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001321 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +02001322 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Willy Tarreau79e57332018-10-02 16:01:16 +02001323
1324 if (!args || args->type != ARGT_USR)
1325 return 0;
1326
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001327 if (!htx)
1328 return 0;
1329 if (!get_http_auth(smp, htx))
1330 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001331
1332 smp->data.type = SMP_T_BOOL;
1333 smp->data.u.sint = check_user(args->data.usr, smp->strm->txn->auth.user,
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001334 smp->strm->txn->auth.pass);
Willy Tarreau79e57332018-10-02 16:01:16 +02001335 return 1;
1336}
1337
1338/* Accepts exactly 1 argument of type userlist */
1339static int smp_fetch_http_auth_grp(const struct arg *args, struct sample *smp, const char *kw, void *private)
1340{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001341 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +02001342 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Faulet89dc4992019-04-17 12:02:59 +02001343
Willy Tarreau79e57332018-10-02 16:01:16 +02001344 if (!args || args->type != ARGT_USR)
1345 return 0;
1346
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001347 if (!htx)
1348 return 0;
1349 if (!get_http_auth(smp, htx))
1350 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001351
Willy Tarreau79e57332018-10-02 16:01:16 +02001352 /* if the user does not belong to the userlist or has a wrong password,
1353 * report that it unconditionally does not match. Otherwise we return
1354 * a string containing the username.
1355 */
1356 if (!check_user(args->data.usr, smp->strm->txn->auth.user,
1357 smp->strm->txn->auth.pass))
1358 return 0;
1359
1360 /* pat_match_auth() will need the user list */
1361 smp->ctx.a[0] = args->data.usr;
1362
1363 smp->data.type = SMP_T_STR;
1364 smp->flags = SMP_F_CONST;
1365 smp->data.u.str.area = smp->strm->txn->auth.user;
1366 smp->data.u.str.data = strlen(smp->strm->txn->auth.user);
1367
1368 return 1;
1369}
1370
1371/* Fetch a captured HTTP request header. The index is the position of
1372 * the "capture" option in the configuration file
1373 */
1374static int smp_fetch_capture_req_hdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
1375{
Willy Tarreau0898c2d2020-04-29 11:44:54 +02001376 struct proxy *fe;
Willy Tarreau79e57332018-10-02 16:01:16 +02001377 int idx;
1378
1379 if (!args || args->type != ARGT_SINT)
1380 return 0;
1381
Willy Tarreau0898c2d2020-04-29 11:44:54 +02001382 if (!smp->strm)
1383 return 0;
1384
1385 fe = strm_fe(smp->strm);
Willy Tarreau79e57332018-10-02 16:01:16 +02001386 idx = args->data.sint;
1387
1388 if (idx > (fe->nb_req_cap - 1) || smp->strm->req_cap == NULL || smp->strm->req_cap[idx] == NULL)
1389 return 0;
1390
1391 smp->data.type = SMP_T_STR;
1392 smp->flags |= SMP_F_CONST;
1393 smp->data.u.str.area = smp->strm->req_cap[idx];
1394 smp->data.u.str.data = strlen(smp->strm->req_cap[idx]);
1395
1396 return 1;
1397}
1398
1399/* Fetch a captured HTTP response header. The index is the position of
1400 * the "capture" option in the configuration file
1401 */
1402static int smp_fetch_capture_res_hdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
1403{
Willy Tarreau0898c2d2020-04-29 11:44:54 +02001404 struct proxy *fe;
Willy Tarreau79e57332018-10-02 16:01:16 +02001405 int idx;
1406
1407 if (!args || args->type != ARGT_SINT)
1408 return 0;
1409
Willy Tarreau0898c2d2020-04-29 11:44:54 +02001410 if (!smp->strm)
1411 return 0;
1412
1413 fe = strm_fe(smp->strm);
Willy Tarreau79e57332018-10-02 16:01:16 +02001414 idx = args->data.sint;
1415
1416 if (idx > (fe->nb_rsp_cap - 1) || smp->strm->res_cap == NULL || smp->strm->res_cap[idx] == NULL)
1417 return 0;
1418
1419 smp->data.type = SMP_T_STR;
1420 smp->flags |= SMP_F_CONST;
1421 smp->data.u.str.area = smp->strm->res_cap[idx];
1422 smp->data.u.str.data = strlen(smp->strm->res_cap[idx]);
1423
1424 return 1;
1425}
1426
1427/* Extracts the METHOD in the HTTP request, the txn->uri should be filled before the call */
1428static int smp_fetch_capture_req_method(const struct arg *args, struct sample *smp, const char *kw, void *private)
1429{
1430 struct buffer *temp;
Willy Tarreau0898c2d2020-04-29 11:44:54 +02001431 struct http_txn *txn;
Willy Tarreau79e57332018-10-02 16:01:16 +02001432 char *ptr;
1433
Willy Tarreau0898c2d2020-04-29 11:44:54 +02001434 if (!smp->strm)
1435 return 0;
1436
1437 txn = smp->strm->txn;
Willy Tarreau79e57332018-10-02 16:01:16 +02001438 if (!txn || !txn->uri)
1439 return 0;
1440
1441 ptr = txn->uri;
1442
1443 while (*ptr != ' ' && *ptr != '\0') /* find first space */
1444 ptr++;
1445
1446 temp = get_trash_chunk();
1447 temp->area = txn->uri;
1448 temp->data = ptr - txn->uri;
1449 smp->data.u.str = *temp;
1450 smp->data.type = SMP_T_STR;
1451 smp->flags = SMP_F_CONST;
1452
1453 return 1;
1454
1455}
1456
1457/* Extracts the path in the HTTP request, the txn->uri should be filled before the call */
1458static int smp_fetch_capture_req_uri(const struct arg *args, struct sample *smp, const char *kw, void *private)
1459{
Willy Tarreau0898c2d2020-04-29 11:44:54 +02001460 struct http_txn *txn;
Willy Tarreau79e57332018-10-02 16:01:16 +02001461 struct ist path;
1462 const char *ptr;
1463
Willy Tarreau0898c2d2020-04-29 11:44:54 +02001464 if (!smp->strm)
1465 return 0;
1466
1467 txn = smp->strm->txn;
Willy Tarreau79e57332018-10-02 16:01:16 +02001468 if (!txn || !txn->uri)
1469 return 0;
1470
1471 ptr = txn->uri;
1472
1473 while (*ptr != ' ' && *ptr != '\0') /* find first space */
1474 ptr++;
1475
1476 if (!*ptr)
1477 return 0;
1478
Christopher Faulet78337bb2018-11-15 14:35:18 +01001479 /* skip the first space and find space after URI */
1480 path = ist2(++ptr, 0);
1481 while (*ptr != ' ' && *ptr != '\0')
1482 ptr++;
1483 path.len = ptr - path.ptr;
Willy Tarreau79e57332018-10-02 16:01:16 +02001484
Christopher Faulet78337bb2018-11-15 14:35:18 +01001485 path = http_get_path(path);
Tim Duesterhused526372020-03-05 17:56:33 +01001486 if (!isttest(path))
Willy Tarreau79e57332018-10-02 16:01:16 +02001487 return 0;
1488
1489 smp->data.u.str.area = path.ptr;
1490 smp->data.u.str.data = path.len;
1491 smp->data.type = SMP_T_STR;
1492 smp->flags = SMP_F_CONST;
1493
1494 return 1;
1495}
1496
1497/* Retrieves the HTTP version from the request (either 1.0 or 1.1) and emits it
1498 * as a string (either "HTTP/1.0" or "HTTP/1.1").
1499 */
1500static int smp_fetch_capture_req_ver(const struct arg *args, struct sample *smp, const char *kw, void *private)
1501{
Willy Tarreau0898c2d2020-04-29 11:44:54 +02001502 struct http_txn *txn;
1503
1504 if (!smp->strm)
1505 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001506
Willy Tarreau0898c2d2020-04-29 11:44:54 +02001507 txn = smp->strm->txn;
Christopher Faulet711ed6a2019-07-16 14:16:10 +02001508 if (!txn || txn->req.msg_state >= HTTP_MSG_BODY)
Willy Tarreau79e57332018-10-02 16:01:16 +02001509 return 0;
1510
1511 if (txn->req.flags & HTTP_MSGF_VER_11)
1512 smp->data.u.str.area = "HTTP/1.1";
1513 else
1514 smp->data.u.str.area = "HTTP/1.0";
1515
1516 smp->data.u.str.data = 8;
1517 smp->data.type = SMP_T_STR;
1518 smp->flags = SMP_F_CONST;
1519 return 1;
1520
1521}
1522
1523/* Retrieves the HTTP version from the response (either 1.0 or 1.1) and emits it
1524 * as a string (either "HTTP/1.0" or "HTTP/1.1").
1525 */
1526static int smp_fetch_capture_res_ver(const struct arg *args, struct sample *smp, const char *kw, void *private)
1527{
Willy Tarreau0898c2d2020-04-29 11:44:54 +02001528 struct http_txn *txn;
1529
1530 if (!smp->strm)
1531 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001532
Willy Tarreau0898c2d2020-04-29 11:44:54 +02001533 txn = smp->strm->txn;
Christopher Faulet711ed6a2019-07-16 14:16:10 +02001534 if (!txn || txn->rsp.msg_state >= HTTP_MSG_BODY)
Willy Tarreau79e57332018-10-02 16:01:16 +02001535 return 0;
1536
1537 if (txn->rsp.flags & HTTP_MSGF_VER_11)
1538 smp->data.u.str.area = "HTTP/1.1";
1539 else
1540 smp->data.u.str.area = "HTTP/1.0";
1541
1542 smp->data.u.str.data = 8;
1543 smp->data.type = SMP_T_STR;
1544 smp->flags = SMP_F_CONST;
1545 return 1;
1546
1547}
1548
1549/* Iterate over all cookies present in a message. The context is stored in
1550 * smp->ctx.a[0] for the in-header position, smp->ctx.a[1] for the
1551 * end-of-header-value, and smp->ctx.a[2] for the hdr_ctx. Depending on
1552 * the direction, multiple cookies may be parsed on the same line or not.
1553 * The cookie name is in args and the name length in args->data.str.len.
1554 * Accepts exactly 1 argument of type string. If the input options indicate
1555 * that no iterating is desired, then only last value is fetched if any.
1556 * The returned sample is of type CSTR. Can be used to parse cookies in other
1557 * files.
1558 */
1559static int smp_fetch_cookie(const struct arg *args, struct sample *smp, const char *kw, void *private)
1560{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001561 /* possible keywords: req.cookie / cookie / cook, res.cookie / scook / set-cookie */
1562 struct channel *chn = ((kw[0] == 'c' || kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Christopher Fauletf98e6262020-05-06 09:42:04 +02001563 struct check *check = ((kw[0] == 's' || kw[2] == 's') ? objt_check(smp->sess->origin) : NULL);
Christopher Faulet16032ab2020-04-30 11:30:00 +02001564 struct htx *htx = smp_prefetch_htx(smp, chn, check, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001565 struct http_hdr_ctx *ctx = smp->ctx.a[2];
1566 struct ist hdr;
Willy Tarreau79e57332018-10-02 16:01:16 +02001567 int occ = 0;
1568 int found = 0;
1569
1570 if (!args || args->type != ARGT_STR)
1571 return 0;
1572
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001573 if (!ctx) {
1574 /* first call */
1575 ctx = &static_http_hdr_ctx;
1576 ctx->blk = NULL;
1577 smp->ctx.a[2] = ctx;
1578 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001579
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001580 if (!htx)
1581 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001582
Christopher Faulet16032ab2020-04-30 11:30:00 +02001583 hdr = (!(check || (chn && chn->flags & CF_ISRESP)) ? ist("Cookie") : ist("Set-Cookie"));
Willy Tarreau79e57332018-10-02 16:01:16 +02001584
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001585 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
1586 /* no explicit occurrence and single fetch => last cookie by default */
1587 occ = -1;
Willy Tarreau79e57332018-10-02 16:01:16 +02001588
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001589 /* OK so basically here, either we want only one value and it's the
1590 * last one, or we want to iterate over all of them and we fetch the
1591 * next one.
1592 */
Willy Tarreau79e57332018-10-02 16:01:16 +02001593
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001594 if (!(smp->flags & SMP_F_NOT_LAST)) {
1595 /* search for the header from the beginning, we must first initialize
1596 * the search parameters.
Willy Tarreau79e57332018-10-02 16:01:16 +02001597 */
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001598 smp->ctx.a[0] = NULL;
1599 ctx->blk = NULL;
Willy Tarreau79e57332018-10-02 16:01:16 +02001600 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001601
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001602 smp->flags |= SMP_F_VOL_HDR;
1603 while (1) {
1604 /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */
1605 if (!smp->ctx.a[0]) {
1606 if (!http_find_header(htx, hdr, ctx, 0))
1607 goto out;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001608
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001609 if (ctx->value.len < args->data.str.data + 1)
1610 continue;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001611
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001612 smp->ctx.a[0] = ctx->value.ptr;
1613 smp->ctx.a[1] = smp->ctx.a[0] + ctx->value.len;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001614 }
1615
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001616 smp->data.type = SMP_T_STR;
1617 smp->flags |= SMP_F_CONST;
1618 smp->ctx.a[0] = http_extract_cookie_value(smp->ctx.a[0], smp->ctx.a[1],
1619 args->data.str.area, args->data.str.data,
1620 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
1621 &smp->data.u.str.area,
1622 &smp->data.u.str.data);
1623 if (smp->ctx.a[0]) {
1624 found = 1;
1625 if (occ >= 0) {
1626 /* one value was returned into smp->data.u.str.{str,len} */
1627 smp->flags |= SMP_F_NOT_LAST;
1628 return 1;
Willy Tarreau79e57332018-10-02 16:01:16 +02001629 }
1630 }
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001631 /* if we're looking for last occurrence, let's loop */
Willy Tarreau79e57332018-10-02 16:01:16 +02001632 }
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001633
Willy Tarreau79e57332018-10-02 16:01:16 +02001634 /* all cookie headers and values were scanned. If we're looking for the
1635 * last occurrence, we may return it now.
1636 */
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001637 out:
Willy Tarreau79e57332018-10-02 16:01:16 +02001638 smp->flags &= ~SMP_F_NOT_LAST;
1639 return found;
1640}
1641
Christopher Fauletc1f40dd2019-05-16 10:07:30 +02001642/* Same than smp_fetch_cookie() but only relies on the sample direction to
1643 * choose the right channel. So instead of duplicating the code, we just change
1644 * the keyword and then fallback on smp_fetch_cookie().
1645 */
1646static int smp_fetch_chn_cookie(const struct arg *args, struct sample *smp, const char *kw, void *private)
1647{
1648 kw = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ ? "req.cook" : "res.cook");
1649 return smp_fetch_cookie(args, smp, kw, private);
1650}
1651
Willy Tarreau79e57332018-10-02 16:01:16 +02001652/* Iterate over all cookies present in a request to count how many occurrences
1653 * match the name in args and args->data.str.len. If <multi> is non-null, then
1654 * multiple cookies may be parsed on the same line. The returned sample is of
1655 * type UINT. Accepts exactly 1 argument of type string.
1656 */
1657static int smp_fetch_cookie_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
1658{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001659 /* possible keywords: req.cook_cnt / cook_cnt, res.cook_cnt / scook_cnt */
1660 struct channel *chn = ((kw[0] == 'c' || kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Christopher Fauletf98e6262020-05-06 09:42:04 +02001661 struct check *check = ((kw[0] == 's' || kw[2] == 's') ? objt_check(smp->sess->origin) : NULL);
Christopher Faulet16032ab2020-04-30 11:30:00 +02001662 struct htx *htx = smp_prefetch_htx(smp, chn, check, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001663 struct http_hdr_ctx ctx;
1664 struct ist hdr;
Willy Tarreau79e57332018-10-02 16:01:16 +02001665 char *val_beg, *val_end;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001666 int cnt;
Willy Tarreau79e57332018-10-02 16:01:16 +02001667
1668 if (!args || args->type != ARGT_STR)
1669 return 0;
1670
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001671 if (!htx)
1672 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001673
Christopher Faulet16032ab2020-04-30 11:30:00 +02001674 hdr = (!(check || (chn && chn->flags & CF_ISRESP)) ? ist("Cookie") : ist("Set-Cookie"));
Willy Tarreau79e57332018-10-02 16:01:16 +02001675
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001676 val_end = val_beg = NULL;
1677 ctx.blk = NULL;
1678 cnt = 0;
1679 while (1) {
1680 /* Note: val_beg == NULL every time we need to fetch a new header */
1681 if (!val_beg) {
1682 if (!http_find_header(htx, hdr, &ctx, 0))
1683 break;
Willy Tarreau79e57332018-10-02 16:01:16 +02001684
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001685 if (ctx.value.len < args->data.str.data + 1)
1686 continue;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001687
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001688 val_beg = ctx.value.ptr;
1689 val_end = val_beg + ctx.value.len;
Willy Tarreau79e57332018-10-02 16:01:16 +02001690 }
1691
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001692 smp->data.type = SMP_T_STR;
1693 smp->flags |= SMP_F_CONST;
1694 while ((val_beg = http_extract_cookie_value(val_beg, val_end,
1695 args->data.str.area, args->data.str.data,
1696 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
1697 &smp->data.u.str.area,
1698 &smp->data.u.str.data))) {
1699 cnt++;
Willy Tarreau79e57332018-10-02 16:01:16 +02001700 }
1701 }
1702
1703 smp->data.type = SMP_T_SINT;
1704 smp->data.u.sint = cnt;
1705 smp->flags |= SMP_F_VOL_HDR;
1706 return 1;
1707}
1708
1709/* Fetch an cookie's integer value. The integer value is returned. It
1710 * takes a mandatory argument of type string. It relies on smp_fetch_cookie().
1711 */
1712static int smp_fetch_cookie_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
1713{
1714 int ret = smp_fetch_cookie(args, smp, kw, private);
1715
1716 if (ret > 0) {
1717 smp->data.type = SMP_T_SINT;
1718 smp->data.u.sint = strl2ic(smp->data.u.str.area,
1719 smp->data.u.str.data);
1720 }
1721
1722 return ret;
1723}
1724
1725/************************************************************************/
1726/* The code below is dedicated to sample fetches */
1727/************************************************************************/
1728
1729/* This scans a URL-encoded query string. It takes an optionally wrapping
1730 * string whose first contigous chunk has its beginning in ctx->a[0] and end
1731 * in ctx->a[1], and the optional second part in (ctx->a[2]..ctx->a[3]). The
1732 * pointers are updated for next iteration before leaving.
1733 */
1734static int smp_fetch_param(char delim, const char *name, int name_len, const struct arg *args, struct sample *smp, const char *kw, void *private)
1735{
1736 const char *vstart, *vend;
1737 struct buffer *temp;
1738 const char **chunks = (const char **)smp->ctx.a;
1739
1740 if (!http_find_next_url_param(chunks, name, name_len,
1741 &vstart, &vend, delim))
1742 return 0;
1743
1744 /* Create sample. If the value is contiguous, return the pointer as CONST,
1745 * if the value is wrapped, copy-it in a buffer.
1746 */
1747 smp->data.type = SMP_T_STR;
1748 if (chunks[2] &&
1749 vstart >= chunks[0] && vstart <= chunks[1] &&
1750 vend >= chunks[2] && vend <= chunks[3]) {
1751 /* Wrapped case. */
1752 temp = get_trash_chunk();
1753 memcpy(temp->area, vstart, chunks[1] - vstart);
1754 memcpy(temp->area + ( chunks[1] - vstart ), chunks[2],
1755 vend - chunks[2]);
1756 smp->data.u.str.area = temp->area;
1757 smp->data.u.str.data = ( chunks[1] - vstart ) + ( vend - chunks[2] );
1758 } else {
1759 /* Contiguous case. */
1760 smp->data.u.str.area = (char *)vstart;
1761 smp->data.u.str.data = vend - vstart;
1762 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
1763 }
1764
1765 /* Update context, check wrapping. */
1766 chunks[0] = vend;
1767 if (chunks[2] && vend >= chunks[2] && vend <= chunks[3]) {
1768 chunks[1] = chunks[3];
1769 chunks[2] = NULL;
1770 }
1771
1772 if (chunks[0] < chunks[1])
1773 smp->flags |= SMP_F_NOT_LAST;
1774
1775 return 1;
1776}
1777
1778/* This function iterates over each parameter of the query string. It uses
1779 * ctx->a[0] and ctx->a[1] to store the beginning and end of the current
1780 * parameter. Since it uses smp_fetch_param(), ctx->a[2..3] are both NULL.
1781 * An optional parameter name is passed in args[0], otherwise any parameter is
1782 * considered. It supports an optional delimiter argument for the beginning of
1783 * the string in args[1], which defaults to "?".
1784 */
1785static int smp_fetch_url_param(const struct arg *args, struct sample *smp, const char *kw, void *private)
1786{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001787 struct channel *chn = SMP_REQ_CHN(smp);
Willy Tarreau79e57332018-10-02 16:01:16 +02001788 char delim = '?';
1789 const char *name;
1790 int name_len;
1791
1792 if (!args ||
1793 (args[0].type && args[0].type != ARGT_STR) ||
1794 (args[1].type && args[1].type != ARGT_STR))
1795 return 0;
1796
1797 name = "";
1798 name_len = 0;
1799 if (args->type == ARGT_STR) {
1800 name = args->data.str.area;
1801 name_len = args->data.str.data;
1802 }
1803
1804 if (args[1].type)
1805 delim = *args[1].data.str.area;
1806
1807 if (!smp->ctx.a[0]) { // first call, find the query string
Christopher Faulet778f5ed2020-04-29 15:51:55 +02001808 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001809 struct htx_sl *sl;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001810
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001811 if (!htx)
1812 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001813
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001814 sl = http_get_stline(htx);
1815 smp->ctx.a[0] = http_find_param_list(HTX_SL_REQ_UPTR(sl), HTX_SL_REQ_ULEN(sl), delim);
1816 if (!smp->ctx.a[0])
1817 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001818
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001819 smp->ctx.a[1] = HTX_SL_REQ_UPTR(sl) + HTX_SL_REQ_ULEN(sl);
Willy Tarreau79e57332018-10-02 16:01:16 +02001820
1821 /* Assume that the context is filled with NULL pointer
1822 * before the first call.
1823 * smp->ctx.a[2] = NULL;
1824 * smp->ctx.a[3] = NULL;
1825 */
1826 }
1827
1828 return smp_fetch_param(delim, name, name_len, args, smp, kw, private);
1829}
1830
1831/* This function iterates over each parameter of the body. This requires
1832 * that the body has been waited for using http-buffer-request. It uses
1833 * ctx->a[0] and ctx->a[1] to store the beginning and end of the first
1834 * contigous part of the body, and optionally ctx->a[2..3] to reference the
1835 * optional second part if the body wraps at the end of the buffer. An optional
1836 * parameter name is passed in args[0], otherwise any parameter is considered.
1837 */
1838static int smp_fetch_body_param(const struct arg *args, struct sample *smp, const char *kw, void *private)
1839{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001840 struct channel *chn = SMP_REQ_CHN(smp);
Willy Tarreau79e57332018-10-02 16:01:16 +02001841 const char *name;
1842 int name_len;
1843
1844 if (!args || (args[0].type && args[0].type != ARGT_STR))
1845 return 0;
1846
1847 name = "";
1848 name_len = 0;
1849 if (args[0].type == ARGT_STR) {
1850 name = args[0].data.str.area;
1851 name_len = args[0].data.str.data;
1852 }
1853
1854 if (!smp->ctx.a[0]) { // first call, find the query string
Christopher Faulete596d182020-05-05 17:46:34 +02001855 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001856 struct buffer *temp;
1857 int32_t pos;
Willy Tarreau79e57332018-10-02 16:01:16 +02001858
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001859 if (!htx)
1860 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001861
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001862 temp = get_trash_chunk();
1863 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
1864 struct htx_blk *blk = htx_get_blk(htx, pos);
1865 enum htx_blk_type type = htx_get_blk_type(blk);
Willy Tarreau79e57332018-10-02 16:01:16 +02001866
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001867 if (type == HTX_BLK_EOM || type == HTX_BLK_TLR || type == HTX_BLK_EOT)
1868 break;
1869 if (type == HTX_BLK_DATA) {
Christopher Faulet53a899b2019-10-08 16:38:42 +02001870 if (!h1_format_htx_data(htx_get_blk_value(htx, blk), temp, 0))
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001871 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001872 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001873 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001874
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001875 smp->ctx.a[0] = temp->area;
1876 smp->ctx.a[1] = temp->area + temp->data;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001877
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001878 /* Assume that the context is filled with NULL pointer
1879 * before the first call.
1880 * smp->ctx.a[2] = NULL;
1881 * smp->ctx.a[3] = NULL;
1882 */
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001883
Willy Tarreau79e57332018-10-02 16:01:16 +02001884 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001885
Willy Tarreau79e57332018-10-02 16:01:16 +02001886 return smp_fetch_param('&', name, name_len, args, smp, kw, private);
1887}
1888
1889/* Return the signed integer value for the specified url parameter (see url_param
1890 * above).
1891 */
1892static int smp_fetch_url_param_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
1893{
1894 int ret = smp_fetch_url_param(args, smp, kw, private);
1895
1896 if (ret > 0) {
1897 smp->data.type = SMP_T_SINT;
1898 smp->data.u.sint = strl2ic(smp->data.u.str.area,
1899 smp->data.u.str.data);
1900 }
1901
1902 return ret;
1903}
1904
1905/* This produces a 32-bit hash of the concatenation of the first occurrence of
1906 * the Host header followed by the path component if it begins with a slash ('/').
1907 * This means that '*' will not be added, resulting in exactly the first Host
1908 * entry. If no Host header is found, then the path is used. The resulting value
1909 * is hashed using the url hash followed by a full avalanche hash and provides a
1910 * 32-bit integer value. This fetch is useful for tracking per-URL activity on
1911 * high-traffic sites without having to store whole paths.
1912 * this differs from the base32 functions in that it includes the url parameters
1913 * as well as the path
1914 */
1915static int smp_fetch_url32(const struct arg *args, struct sample *smp, const char *kw, void *private)
1916{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001917 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +02001918 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001919 struct http_hdr_ctx ctx;
1920 struct htx_sl *sl;
1921 struct ist path;
Willy Tarreau79e57332018-10-02 16:01:16 +02001922 unsigned int hash = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001923
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001924 if (!htx)
1925 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001926
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001927 ctx.blk = NULL;
1928 if (http_find_header(htx, ist("Host"), &ctx, 1)) {
1929 /* OK we have the header value in ctx.value */
1930 while (ctx.value.len--)
1931 hash = *(ctx.value.ptr++) + (hash << 6) + (hash << 16) - hash;
Willy Tarreau79e57332018-10-02 16:01:16 +02001932 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001933
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001934 /* now retrieve the path */
1935 sl = http_get_stline(htx);
1936 path = http_get_path(htx_sl_req_uri(sl));
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001937 if (path.len && *(path.ptr) == '/') {
1938 while (path.len--)
1939 hash = *(path.ptr++) + (hash << 6) + (hash << 16) - hash;
Willy Tarreau79e57332018-10-02 16:01:16 +02001940 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001941
Willy Tarreau79e57332018-10-02 16:01:16 +02001942 hash = full_hash(hash);
1943
1944 smp->data.type = SMP_T_SINT;
1945 smp->data.u.sint = hash;
1946 smp->flags = SMP_F_VOL_1ST;
1947 return 1;
1948}
1949
1950/* This concatenates the source address with the 32-bit hash of the Host and
1951 * URL as returned by smp_fetch_base32(). The idea is to have per-source and
1952 * per-url counters. The result is a binary block from 8 to 20 bytes depending
1953 * on the source address length. The URL hash is stored before the address so
1954 * that in environments where IPv6 is insignificant, truncating the output to
1955 * 8 bytes would still work.
1956 */
1957static int smp_fetch_url32_src(const struct arg *args, struct sample *smp, const char *kw, void *private)
1958{
1959 struct buffer *temp;
1960 struct connection *cli_conn = objt_conn(smp->sess->origin);
1961
Willy Tarreaucd7ca792019-07-17 16:57:03 +02001962 if (!cli_conn || !conn_get_src(cli_conn))
Willy Tarreau79e57332018-10-02 16:01:16 +02001963 return 0;
1964
1965 if (!smp_fetch_url32(args, smp, kw, private))
1966 return 0;
1967
1968 temp = get_trash_chunk();
1969 *(unsigned int *) temp->area = htonl(smp->data.u.sint);
1970 temp->data += sizeof(unsigned int);
1971
Willy Tarreau9a1efe12019-07-17 17:13:50 +02001972 switch (cli_conn->src->ss_family) {
Willy Tarreau79e57332018-10-02 16:01:16 +02001973 case AF_INET:
1974 memcpy(temp->area + temp->data,
Willy Tarreau9a1efe12019-07-17 17:13:50 +02001975 &((struct sockaddr_in *)cli_conn->src)->sin_addr,
Willy Tarreau79e57332018-10-02 16:01:16 +02001976 4);
1977 temp->data += 4;
1978 break;
1979 case AF_INET6:
1980 memcpy(temp->area + temp->data,
Willy Tarreau9a1efe12019-07-17 17:13:50 +02001981 &((struct sockaddr_in6 *)cli_conn->src)->sin6_addr,
Willy Tarreau79e57332018-10-02 16:01:16 +02001982 16);
1983 temp->data += 16;
1984 break;
1985 default:
1986 return 0;
1987 }
1988
1989 smp->data.u.str = *temp;
1990 smp->data.type = SMP_T_BIN;
1991 return 1;
1992}
1993
1994/************************************************************************/
1995/* Other utility functions */
1996/************************************************************************/
1997
1998/* This function is used to validate the arguments passed to any "hdr" fetch
1999 * keyword. These keywords support an optional positive or negative occurrence
2000 * number. We must ensure that the number is greater than -MAX_HDR_HISTORY. It
2001 * is assumed that the types are already the correct ones. Returns 0 on error,
2002 * non-zero if OK. If <err> is not NULL, it will be filled with a pointer to an
2003 * error message in case of error, that the caller is responsible for freeing.
2004 * The initial location must either be freeable or NULL.
2005 * Note: this function's pointer is checked from Lua.
2006 */
2007int val_hdr(struct arg *arg, char **err_msg)
2008{
2009 if (arg && arg[1].type == ARGT_SINT && arg[1].data.sint < -MAX_HDR_HISTORY) {
2010 memprintf(err_msg, "header occurrence must be >= %d", -MAX_HDR_HISTORY);
2011 return 0;
2012 }
2013 return 1;
2014}
2015
2016/************************************************************************/
2017/* All supported sample fetch keywords must be declared here. */
2018/************************************************************************/
2019
2020/* Note: must not be declared <const> as its list will be overwritten */
2021static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
2022 { "base", smp_fetch_base, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2023 { "base32", smp_fetch_base32, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
2024 { "base32+src", smp_fetch_base32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
2025
2026 /* capture are allocated and are permanent in the stream */
2027 { "capture.req.hdr", smp_fetch_capture_req_hdr, ARG1(1,SINT), NULL, SMP_T_STR, SMP_USE_HRQHP },
2028
2029 /* retrieve these captures from the HTTP logs */
2030 { "capture.req.method", smp_fetch_capture_req_method, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
2031 { "capture.req.uri", smp_fetch_capture_req_uri, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
2032 { "capture.req.ver", smp_fetch_capture_req_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
2033
2034 { "capture.res.hdr", smp_fetch_capture_res_hdr, ARG1(1,SINT), NULL, SMP_T_STR, SMP_USE_HRSHP },
2035 { "capture.res.ver", smp_fetch_capture_res_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
2036
2037 /* cookie is valid in both directions (eg: for "stick ...") but cook*
2038 * are only here to match the ACL's name, are request-only and are used
2039 * for ACL compatibility only.
2040 */
2041 { "cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Christopher Fauletc1f40dd2019-05-16 10:07:30 +02002042 { "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 +02002043 { "cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2044 { "cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2045
2046 /* hdr is valid in both directions (eg: for "stick ...") but hdr_* are
2047 * only here to match the ACL's name, are request-only and are used for
2048 * ACL compatibility only.
2049 */
Christopher Fauletc1f40dd2019-05-16 10:07:30 +02002050 { "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 +02002051 { "hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2052 { "hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV },
2053 { "hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_SINT, SMP_USE_HRQHV },
2054
Christopher Fauleta4063562019-08-02 11:51:37 +02002055 { "http_auth_type", smp_fetch_http_auth_type, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2056 { "http_auth_user", smp_fetch_http_auth_user, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2057 { "http_auth_pass", smp_fetch_http_auth_pass, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau79e57332018-10-02 16:01:16 +02002058 { "http_auth", smp_fetch_http_auth, ARG1(1,USR), NULL, SMP_T_BOOL, SMP_USE_HRQHV },
2059 { "http_auth_group", smp_fetch_http_auth_grp, ARG1(1,USR), NULL, SMP_T_STR, SMP_USE_HRQHV },
2060 { "http_first_req", smp_fetch_http_first_req, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
2061 { "method", smp_fetch_meth, 0, NULL, SMP_T_METH, SMP_USE_HRQHP },
2062 { "path", smp_fetch_path, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2063 { "query", smp_fetch_query, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2064
2065 /* HTTP protocol on the request path */
2066 { "req.proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
2067 { "req_proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
2068
2069 /* HTTP version on the request path */
2070 { "req.ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2071 { "req_ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2072
2073 { "req.body", smp_fetch_body, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
2074 { "req.body_len", smp_fetch_body_len, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
2075 { "req.body_size", smp_fetch_body_size, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
2076 { "req.body_param", smp_fetch_body_param, ARG1(0,STR), NULL, SMP_T_BIN, SMP_USE_HRQHV },
2077
2078 { "req.hdrs", smp_fetch_hdrs, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
2079 { "req.hdrs_bin", smp_fetch_hdrs_bin, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
2080
2081 /* HTTP version on the response path */
2082 { "res.ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
2083 { "resp_ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
2084
Christopher Faulete596d182020-05-05 17:46:34 +02002085 { "res.body", smp_fetch_body, 0, NULL, SMP_T_BIN, SMP_USE_HRSHV },
2086 { "res.body_len", smp_fetch_body_len, 0, NULL, SMP_T_SINT, SMP_USE_HRSHV },
2087 { "res.body_size", smp_fetch_body_size, 0, NULL, SMP_T_SINT, SMP_USE_HRSHV },
2088
2089 { "res.hdrs", smp_fetch_hdrs, 0, NULL, SMP_T_BIN, SMP_USE_HRSHV },
2090 { "res.hdrs_bin", smp_fetch_hdrs_bin, 0, NULL, SMP_T_BIN, SMP_USE_HRSHV },
2091
Willy Tarreau79e57332018-10-02 16:01:16 +02002092 /* explicit req.{cook,hdr} are used to force the fetch direction to be request-only */
2093 { "req.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
2094 { "req.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2095 { "req.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2096
2097 { "req.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
2098 { "req.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2099 { "req.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
2100 { "req.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2101 { "req.hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV },
2102 { "req.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
2103 { "req.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_SINT, SMP_USE_HRQHV },
2104
2105 /* explicit req.{cook,hdr} are used to force the fetch direction to be response-only */
2106 { "res.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
2107 { "res.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2108 { "res.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2109
2110 { "res.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
2111 { "res.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2112 { "res.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
2113 { "res.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2114 { "res.hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV },
2115 { "res.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
2116 { "res.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_SINT, SMP_USE_HRSHV },
2117
2118 /* scook is valid only on the response and is used for ACL compatibility */
2119 { "scook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
2120 { "scook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2121 { "scook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2122 { "set-cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV }, /* deprecated */
2123
2124 /* shdr is valid only on the response and is used for ACL compatibility */
2125 { "shdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
2126 { "shdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2127 { "shdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV },
2128 { "shdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_SINT, SMP_USE_HRSHV },
2129
2130 { "status", smp_fetch_stcode, 0, NULL, SMP_T_SINT, SMP_USE_HRSHP },
2131 { "unique-id", smp_fetch_uniqueid, 0, NULL, SMP_T_STR, SMP_SRC_L4SRV },
2132 { "url", smp_fetch_url, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2133 { "url32", smp_fetch_url32, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
2134 { "url32+src", smp_fetch_url32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
2135 { "url_ip", smp_fetch_url_ip, 0, NULL, SMP_T_IPV4, SMP_USE_HRQHV },
2136 { "url_port", smp_fetch_url_port, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
2137 { "url_param", smp_fetch_url_param, ARG2(0,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
2138 { "urlp" , smp_fetch_url_param, ARG2(0,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
2139 { "urlp_val", smp_fetch_url_param_val, ARG2(0,STR,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Christopher Faulet16032ab2020-04-30 11:30:00 +02002140
Willy Tarreau79e57332018-10-02 16:01:16 +02002141 { /* END */ },
2142}};
2143
Willy Tarreau0108d902018-11-25 19:14:37 +01002144INITCALL1(STG_REGISTER, sample_register_fetches, &sample_fetch_keywords);
Willy Tarreau79e57332018-10-02 16:01:16 +02002145
2146/*
2147 * Local variables:
2148 * c-indent-level: 8
2149 * c-basic-offset: 8
2150 * End:
2151 */