blob: 9e6113bca2d105d218e2b606a4fb12f86f7a906d [file] [log] [blame]
Willy Tarreau79e57332018-10-02 16:01:16 +02001/*
2 * HTTP samples fetching
3 *
4 * Copyright 2000-2018 Willy Tarreau <w@1wt.eu>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
13#include <sys/types.h>
14
15#include <ctype.h>
16#include <string.h>
17#include <time.h>
18
19#include <common/base64.h>
20#include <common/chunk.h>
21#include <common/compat.h>
22#include <common/config.h>
23#include <common/debug.h>
Willy Tarreauafba57a2018-12-11 13:44:24 +010024#include <common/h1.h>
Willy Tarreau79e57332018-10-02 16:01:16 +020025#include <common/http.h>
Willy Tarreaub96b77e2018-12-11 10:22:41 +010026#include <common/htx.h>
Willy Tarreau0108d902018-11-25 19:14:37 +010027#include <common/initcall.h>
Willy Tarreau79e57332018-10-02 16:01:16 +020028#include <common/memory.h>
29#include <common/standard.h>
30#include <common/version.h>
31
32#include <types/global.h>
33
34#include <proto/arg.h>
35#include <proto/auth.h>
Willy Tarreau538746a2018-12-11 10:59:20 +010036#include <proto/hdr_idx.h>
Willy Tarreau79e57332018-10-02 16:01:16 +020037#include <proto/http_fetch.h>
Christopher Fauletef453ed2018-10-24 21:39:27 +020038#include <proto/http_htx.h>
Willy Tarreau79e57332018-10-02 16:01:16 +020039#include <proto/log.h>
40#include <proto/obj_type.h>
41#include <proto/proto_http.h>
42#include <proto/sample.h>
43#include <proto/stream.h>
44
45
46/* this struct is used between calls to smp_fetch_hdr() or smp_fetch_cookie() */
47static THREAD_LOCAL struct hdr_ctx static_hdr_ctx;
Christopher Fauletef453ed2018-10-24 21:39:27 +020048static THREAD_LOCAL struct http_hdr_ctx static_http_hdr_ctx;
Richard Russoc0968f52019-07-31 11:45:56 -070049/* this is used to convert raw connection buffers to htx */
50static THREAD_LOCAL struct buffer static_raw_htx_chunk;
51static THREAD_LOCAL char *static_raw_htx_buf;
Christopher Fauletef453ed2018-10-24 21:39:27 +020052
Christopher Faulet89dc4992019-04-17 12:02:59 +020053#define SMP_REQ_CHN(smp) (smp->strm ? &smp->strm->req : NULL)
54#define SMP_RES_CHN(smp) (smp->strm ? &smp->strm->res : NULL)
Willy Tarreau79e57332018-10-02 16:01:16 +020055
Richard Russoc0968f52019-07-31 11:45:56 -070056/* This function returns the static htx chunk, where raw connections get
57 * converted to HTX as needed for samplxsing.
58 */
59struct buffer *get_raw_htx_chunk(void)
60{
61 chunk_reset(&static_raw_htx_chunk);
62 return &static_raw_htx_chunk;
63}
64
65static int alloc_raw_htx_chunk_per_thread()
66{
67 static_raw_htx_buf = malloc(global.tune.bufsize);
68 if (!static_raw_htx_buf)
69 return 0;
70 chunk_init(&static_raw_htx_chunk, static_raw_htx_buf, global.tune.bufsize);
71 return 1;
72}
73
74static void free_raw_htx_chunk_per_thread()
75{
76 free(static_raw_htx_buf);
77 static_raw_htx_buf = NULL;
78}
79
80REGISTER_PER_THREAD_ALLOC(alloc_raw_htx_chunk_per_thread);
81REGISTER_PER_THREAD_FREE(free_raw_htx_chunk_per_thread);
82
Willy Tarreau79e57332018-10-02 16:01:16 +020083/*
84 * Returns the data from Authorization header. Function may be called more
85 * than once so data is stored in txn->auth_data. When no header is found
86 * or auth method is unknown auth_method is set to HTTP_AUTH_WRONG to avoid
87 * searching again for something we are unable to find anyway. However, if
88 * the result if valid, the cache is not reused because we would risk to
89 * have the credentials overwritten by another stream in parallel.
90 */
91
Christopher Faulete98411b2019-07-15 13:58:29 +020092static int get_http_auth(struct sample *smp, struct htx *htx)
Willy Tarreau79e57332018-10-02 16:01:16 +020093{
Christopher Faulet311c7ea2018-10-24 21:41:55 +020094 struct stream *s = smp->strm;
Willy Tarreau79e57332018-10-02 16:01:16 +020095 struct http_txn *txn = s->txn;
96 struct buffer auth_method;
Willy Tarreau79e57332018-10-02 16:01:16 +020097 char *h, *p;
98 int len;
99
100#ifdef DEBUG_AUTH
101 printf("Auth for stream %p: %d\n", s, txn->auth.method);
102#endif
Willy Tarreau79e57332018-10-02 16:01:16 +0200103 if (txn->auth.method == HTTP_AUTH_WRONG)
104 return 0;
105
106 txn->auth.method = HTTP_AUTH_WRONG;
107
Christopher Faulete98411b2019-07-15 13:58:29 +0200108 if (htx) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200109 /* HTX version */
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200110 struct http_hdr_ctx ctx = { .blk = NULL };
111 struct ist hdr;
Willy Tarreau79e57332018-10-02 16:01:16 +0200112
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200113 if (txn->flags & TX_USE_PX_CONN)
114 hdr = ist("Proxy-Authorization");
115 else
116 hdr = ist("Authorization");
117
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200118 ctx.blk = NULL;
119 if (!http_find_header(htx, hdr, &ctx, 0))
120 return 0;
121
122 p = memchr(ctx.value.ptr, ' ', ctx.value.len);
123 len = p - ctx.value.ptr;
124 if (!p || len <= 0)
125 return 0;
126
127 if (chunk_initlen(&auth_method, ctx.value.ptr, 0, len) != 1)
128 return 0;
129
130 chunk_initlen(&txn->auth.method_data, p + 1, 0, ctx.value.len - len - 1);
Willy Tarreau79e57332018-10-02 16:01:16 +0200131 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200132 else {
133 /* LEGACY version */
134 struct hdr_ctx ctx = { .idx = 0 };
Willy Tarreau79e57332018-10-02 16:01:16 +0200135
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200136 if (txn->flags & TX_USE_PX_CONN) {
137 h = "Proxy-Authorization";
138 len = strlen(h);
139 } else {
140 h = "Authorization";
141 len = strlen(h);
142 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200143
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200144 if (!http_find_header2(h, len, ci_head(&s->req), &txn->hdr_idx, &ctx))
145 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200146
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200147 h = ctx.line + ctx.val;
Willy Tarreau79e57332018-10-02 16:01:16 +0200148
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200149 p = memchr(h, ' ', ctx.vlen);
150 len = p - h;
151 if (!p || len <= 0)
152 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200153
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200154 if (chunk_initlen(&auth_method, h, 0, len) != 1)
155 return 0;
156
157 chunk_initlen(&txn->auth.method_data, p + 1, 0, ctx.vlen - len - 1);
158 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200159
160 if (!strncasecmp("Basic", auth_method.area, auth_method.data)) {
161 struct buffer *http_auth = get_trash_chunk();
162
163 len = base64dec(txn->auth.method_data.area,
164 txn->auth.method_data.data,
165 http_auth->area, global.tune.bufsize - 1);
166
167 if (len < 0)
168 return 0;
169
170
171 http_auth->area[len] = '\0';
172
173 p = strchr(http_auth->area, ':');
174
175 if (!p)
176 return 0;
177
178 txn->auth.user = http_auth->area;
179 *p = '\0';
180 txn->auth.pass = p+1;
181
182 txn->auth.method = HTTP_AUTH_BASIC;
183 return 1;
184 }
185
186 return 0;
187}
188
189/* This function ensures that the prerequisites for an L7 fetch are ready,
190 * which means that a request or response is ready. If some data is missing,
191 * a parsing attempt is made. This is useful in TCP-based ACLs which are able
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +0200192 * to extract data from L7. If <vol> is non-null during a prefetch, another
193 * test is made to ensure the required information is not gone.
Christopher Fauletef453ed2018-10-24 21:39:27 +0200194 *
195 * The function returns :
196 * NULL with SMP_F_MAY_CHANGE in the sample flags if some data is missing to
197 * decide whether or not an HTTP message is present ;
198 * NULL if the requested data cannot be fetched or if it is certain that
199 * we'll never have any HTTP message there ;
200 * The HTX message if ready
201 */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +0200202struct htx *smp_prefetch_htx(struct sample *smp, struct channel *chn, int vol)
Christopher Fauletef453ed2018-10-24 21:39:27 +0200203{
Christopher Fauletef453ed2018-10-24 21:39:27 +0200204 struct stream *s = smp->strm;
Christopher Fauletef453ed2018-10-24 21:39:27 +0200205 struct http_txn *txn = NULL;
206 struct htx *htx = NULL;
Christopher Faulet89dc4992019-04-17 12:02:59 +0200207 struct http_msg *msg;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100208 struct htx_sl *sl;
Christopher Fauletef453ed2018-10-24 21:39:27 +0200209
210 /* Note: it is possible that <s> is NULL when called before stream
211 * initialization (eg: tcp-request connection), so this function is the
212 * one responsible for guarding against this case for all HTTP users.
213 */
Christopher Faulet89dc4992019-04-17 12:02:59 +0200214 if (!s || !chn)
Christopher Fauletef453ed2018-10-24 21:39:27 +0200215 return NULL;
216
217 if (!s->txn) {
218 if (unlikely(!http_alloc_txn(s)))
219 return NULL; /* not enough memory */
220 http_init_txn(s);
221 txn = s->txn;
222 }
Christopher Faulet89dc4992019-04-17 12:02:59 +0200223 txn = s->txn;
224 msg = (!(chn->flags & CF_ISRESP) ? &txn->req : &txn->rsp);
225 smp->data.type = SMP_T_BOOL;
Christopher Fauletef453ed2018-10-24 21:39:27 +0200226
Christopher Fauleteca88542019-04-03 10:12:42 +0200227 if (IS_HTX_STRM(s)) {
Christopher Faulet89dc4992019-04-17 12:02:59 +0200228 htx = htxbuf(&chn->buf);
Christopher Fauletef453ed2018-10-24 21:39:27 +0200229
Christopher Faulet89dc4992019-04-17 12:02:59 +0200230 if (msg->msg_state == HTTP_MSG_ERROR || (htx->flags & HTX_FL_PARSING_ERROR))
231 return NULL;
Christopher Fauletef453ed2018-10-24 21:39:27 +0200232
Christopher Faulet89dc4992019-04-17 12:02:59 +0200233 if (msg->msg_state < HTTP_MSG_BODY) {
234 /* Analyse not yet started */
Christopher Faulet29f17582019-05-23 11:03:26 +0200235 if (htx_is_empty(htx) || htx->first == -1) {
Christopher Fauletef453ed2018-10-24 21:39:27 +0200236 /* Parsing is done by the mux, just wait */
237 smp->flags |= SMP_F_MAY_CHANGE;
238 return NULL;
239 }
240 }
Christopher Faulet297fbb42019-05-13 14:41:27 +0200241 sl = http_get_stline(htx);
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +0200242 if (vol && !sl) {
Christopher Faulet89dc4992019-04-17 12:02:59 +0200243 /* The start-line was already forwarded, it is too late to fetch anything */
244 return NULL;
245 }
Christopher Fauletef453ed2018-10-24 21:39:27 +0200246 }
Christopher Fauleteca88542019-04-03 10:12:42 +0200247 else { /* RAW mode */
Christopher Faulet89dc4992019-04-17 12:02:59 +0200248 struct buffer *buf;
249 struct h1m h1m;
Christopher Faulete4ab11b2019-06-11 15:05:37 +0200250 struct http_hdr hdrs[global.tune.max_http_hdr];
Christopher Faulet89dc4992019-04-17 12:02:59 +0200251 union h1_sl h1sl;
252 unsigned int flags = HTX_FL_NONE;
253 int ret;
Christopher Fauletef453ed2018-10-24 21:39:27 +0200254
Christopher Faulet89dc4992019-04-17 12:02:59 +0200255 /* no HTTP fetch on the response in TCP mode */
256 if (chn->flags & CF_ISRESP)
257 return NULL;
Christopher Fauletef453ed2018-10-24 21:39:27 +0200258
Christopher Faulet89dc4992019-04-17 12:02:59 +0200259 /* Now we are working on the request only */
260 buf = &chn->buf;
261 if (b_head(buf) + b_data(buf) > b_wrap(buf))
262 b_slow_realign(buf, trash.area, 0);
Christopher Fauletef453ed2018-10-24 21:39:27 +0200263
Christopher Faulet89dc4992019-04-17 12:02:59 +0200264 h1m_init_req(&h1m);
265 ret = h1_headers_to_hdr_list(b_head(buf), b_stop(buf),
266 hdrs, sizeof(hdrs)/sizeof(hdrs[0]), &h1m, &h1sl);
267 if (ret <= 0) {
268 /* Invalid or too big*/
269 if (ret < 0 || channel_full(&s->req, global.tune.maxrewrite))
Christopher Fauletef453ed2018-10-24 21:39:27 +0200270 return NULL;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100271
Christopher Faulet89dc4992019-04-17 12:02:59 +0200272 /* wait for a full request */
273 smp->flags |= SMP_F_MAY_CHANGE;
274 return NULL;
275 }
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100276
Christopher Faulet89dc4992019-04-17 12:02:59 +0200277 /* OK we just got a valid HTTP mesage. We have to convert it
278 * into an HTX message.
279 */
280 if (unlikely(h1sl.rq.v.len == 0)) {
281 /* try to convert HTTP/0.9 requests to HTTP/1.0 */
282 if (h1sl.rq.meth != HTTP_METH_GET || !h1sl.rq.u.len)
Christopher Fauletef453ed2018-10-24 21:39:27 +0200283 return NULL;
Christopher Faulet89dc4992019-04-17 12:02:59 +0200284 h1sl.rq.v = ist("HTTP/1.0");
Christopher Fauletef453ed2018-10-24 21:39:27 +0200285 }
Christopher Faulet89dc4992019-04-17 12:02:59 +0200286
287 /* Set HTX start-line flags */
288 if (h1m.flags & H1_MF_VER_11)
289 flags |= HTX_SL_F_VER_11;
290 if (h1m.flags & H1_MF_XFER_ENC)
291 flags |= HTX_SL_F_XFER_ENC;
292 flags |= HTX_SL_F_XFER_LEN;
293 if (h1m.flags & H1_MF_CHNK)
294 flags |= HTX_SL_F_CHNK;
295 else if (h1m.flags & H1_MF_CLEN)
296 flags |= HTX_SL_F_CLEN;
297
Richard Russoc0968f52019-07-31 11:45:56 -0700298 htx = htx_from_buf(get_raw_htx_chunk());
Christopher Faulet89dc4992019-04-17 12:02:59 +0200299 sl = htx_add_stline(htx, HTX_BLK_REQ_SL, flags, h1sl.rq.m, h1sl.rq.u, h1sl.rq.v);
300 if (!sl || !htx_add_all_headers(htx, hdrs))
Christopher Fauletef453ed2018-10-24 21:39:27 +0200301 return NULL;
Willy Tarreauce9bbf52019-05-13 08:32:31 +0200302 sl->info.req.meth = h1sl.rq.meth;
Christopher Faulet89dc4992019-04-17 12:02:59 +0200303 }
304
305 /* OK we just got a valid HTTP message. If not already done by
306 * HTTP analyzers, we have some minor preparation to perform so
307 * that further checks can rely on HTTP tests.
308 */
309 if (sl && msg->msg_state < HTTP_MSG_BODY) {
310 if (!(chn->flags & CF_ISRESP)) {
311 txn->meth = sl->info.req.meth;
312 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
313 s->flags |= SF_REDIRECTABLE;
Christopher Fauletef453ed2018-10-24 21:39:27 +0200314 }
Christopher Faulet89dc4992019-04-17 12:02:59 +0200315 else
316 txn->status = sl->info.res.status;
317 if (sl->flags & HTX_SL_F_VER_11)
318 msg->flags |= HTTP_MSGF_VER_11;
Christopher Fauletef453ed2018-10-24 21:39:27 +0200319 }
320
321 /* everything's OK */
322 smp->data.u.sint = 1;
323 return htx;
324}
325
326/* This function ensures that the prerequisites for an L7 fetch are ready,
327 * which means that a request or response is ready. If some data is missing,
328 * a parsing attempt is made. This is useful in TCP-based ACLs which are able
Willy Tarreau79e57332018-10-02 16:01:16 +0200329 * to extract data from L7. If <req_vol> is non-null during a request prefetch,
330 * another test is made to ensure the required information is not gone.
331 *
332 * The function returns :
333 * 0 with SMP_F_MAY_CHANGE in the sample flags if some data is missing to
334 * decide whether or not an HTTP message is present ;
335 * 0 if the requested data cannot be fetched or if it is certain that
336 * we'll never have any HTTP message there ;
337 * 1 if an HTTP message is ready
338 */
339int smp_prefetch_http(struct proxy *px, struct stream *s, unsigned int opt,
Christopher Faulet89dc4992019-04-17 12:02:59 +0200340 struct channel *chn, struct sample *smp, int req_vol)
Willy Tarreau79e57332018-10-02 16:01:16 +0200341{
342 struct http_txn *txn;
343 struct http_msg *msg;
344
345 /* Note: it is possible that <s> is NULL when called before stream
346 * initialization (eg: tcp-request connection), so this function is the
347 * one responsible for guarding against this case for all HTTP users.
348 */
Christopher Faulet89dc4992019-04-17 12:02:59 +0200349 if (!s || !chn)
Willy Tarreau79e57332018-10-02 16:01:16 +0200350 return 0;
351
352 if (!s->txn) {
353 if (unlikely(!http_alloc_txn(s)))
354 return 0; /* not enough memory */
355 http_init_txn(s);
356 }
357 txn = s->txn;
Willy Tarreau79e57332018-10-02 16:01:16 +0200358 smp->data.type = SMP_T_BOOL;
359
Christopher Faulet89dc4992019-04-17 12:02:59 +0200360 if (chn->flags & CF_ISRESP) {
361 /* Check for a dependency on a response */
362 if (txn->rsp.msg_state < HTTP_MSG_BODY) {
363 smp->flags |= SMP_F_MAY_CHANGE;
364 return 0;
365 }
366 goto end;
367 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200368
Christopher Faulet89dc4992019-04-17 12:02:59 +0200369 /* Check for a dependency on a request */
370 msg = &txn->req;
Willy Tarreau79e57332018-10-02 16:01:16 +0200371
Christopher Faulet89dc4992019-04-17 12:02:59 +0200372 if (req_vol && (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) {
373 return 0; /* data might have moved and indexes changed */
374 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200375
Christopher Faulet89dc4992019-04-17 12:02:59 +0200376 /* If the buffer does not leave enough free space at the end, we must
377 * first realign it.
378 */
379 if (ci_head(chn) > b_orig(&chn->buf) &&
380 ci_head(chn) + ci_data(chn) > b_wrap(&chn->buf) - global.tune.maxrewrite)
381 channel_slow_realign(chn, trash.area);
Willy Tarreau79e57332018-10-02 16:01:16 +0200382
Christopher Faulet89dc4992019-04-17 12:02:59 +0200383 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
384 if (msg->msg_state == HTTP_MSG_ERROR)
385 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200386
Christopher Faulet89dc4992019-04-17 12:02:59 +0200387 /* Try to decode HTTP request */
388 if (likely(msg->next < ci_data(chn)))
389 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau79e57332018-10-02 16:01:16 +0200390
Christopher Faulet89dc4992019-04-17 12:02:59 +0200391 /* Still no valid request ? */
392 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
393 if ((msg->msg_state == HTTP_MSG_ERROR) ||
394 channel_full(chn, global.tune.maxrewrite)) {
Willy Tarreau79e57332018-10-02 16:01:16 +0200395 return 0;
Christopher Faulet89dc4992019-04-17 12:02:59 +0200396 }
397 /* wait for final state */
398 smp->flags |= SMP_F_MAY_CHANGE;
399 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200400 }
401
Christopher Faulet89dc4992019-04-17 12:02:59 +0200402 /* OK we just got a valid HTTP message. We have some minor
403 * preparation to perform so that further checks can rely
404 * on HTTP tests.
405 */
406
407 /* If the message was parsed but was too large, we must absolutely
408 * return an error so that it is not processed. At the moment this
409 * cannot happen, but if the parsers are to change in the future,
410 * we want this check to be maintained.
411 */
412 if (unlikely(ci_head(chn) + ci_data(chn) >
413 b_wrap(&chn->buf) - global.tune.maxrewrite)) {
414 msg->err_state = msg->msg_state;
415 msg->msg_state = HTTP_MSG_ERROR;
416 smp->data.u.sint = 1;
417 return 1;
Willy Tarreau79e57332018-10-02 16:01:16 +0200418 }
419
Christopher Faulet89dc4992019-04-17 12:02:59 +0200420 txn->meth = find_http_meth(ci_head(chn), msg->sl.rq.m_l);
421 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
422 s->flags |= SF_REDIRECTABLE;
423
424 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
Willy Tarreau79e57332018-10-02 16:01:16 +0200425 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200426 }
427
Christopher Faulet89dc4992019-04-17 12:02:59 +0200428 end:
Willy Tarreau79e57332018-10-02 16:01:16 +0200429 /* everything's OK */
430 smp->data.u.sint = 1;
431 return 1;
432}
433
434/* This function fetches the method of current HTTP request and stores
435 * it in the global pattern struct as a chunk. There are two possibilities :
436 * - if the method is known (not HTTP_METH_OTHER), its identifier is stored
437 * in <len> and <ptr> is NULL ;
438 * - if the method is unknown (HTTP_METH_OTHER), <ptr> points to the text and
439 * <len> to its length.
440 * This is intended to be used with pat_match_meth() only.
441 */
442static int smp_fetch_meth(const struct arg *args, struct sample *smp, const char *kw, void *private)
443{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200444 struct channel *chn = SMP_REQ_CHN(smp);
Willy Tarreau79e57332018-10-02 16:01:16 +0200445 int meth;
446 struct http_txn *txn;
447
Christopher Faulet46575cd2019-04-17 11:40:30 +0200448 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200449 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +0200450 struct htx *htx = smp_prefetch_htx(smp, chn, 0);
Willy Tarreau79e57332018-10-02 16:01:16 +0200451
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200452 if (!htx)
Willy Tarreau79e57332018-10-02 16:01:16 +0200453 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200454
455 txn = smp->strm->txn;
456 meth = txn->meth;
457 smp->data.type = SMP_T_METH;
458 smp->data.u.meth.meth = meth;
459 if (meth == HTTP_METH_OTHER) {
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100460 struct htx_sl *sl;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200461
Christopher Faulet89dc4992019-04-17 12:02:59 +0200462 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200463 /* ensure the indexes are not affected */
464 return 0;
Christopher Faulet89dc4992019-04-17 12:02:59 +0200465 }
Christopher Faulet297fbb42019-05-13 14:41:27 +0200466 sl = http_get_stline(htx);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200467 smp->flags |= SMP_F_CONST;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100468 smp->data.u.meth.str.area = HTX_SL_REQ_MPTR(sl);
469 smp->data.u.meth.str.data = HTX_SL_REQ_MLEN(sl);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200470 }
471 smp->flags |= SMP_F_VOL_1ST;
472 }
473 else {
474 /* LEGACY version */
Christopher Faulet89dc4992019-04-17 12:02:59 +0200475 CHECK_HTTP_MESSAGE_FIRST_PERM(chn);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200476
477 txn = smp->strm->txn;
478 meth = txn->meth;
479 smp->data.type = SMP_T_METH;
480 smp->data.u.meth.meth = meth;
481 if (meth == HTTP_METH_OTHER) {
Christopher Faulet89dc4992019-04-17 12:02:59 +0200482 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200483 /* ensure the indexes are not affected */
484 return 0;
Christopher Faulet89dc4992019-04-17 12:02:59 +0200485 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200486 smp->flags |= SMP_F_CONST;
487 smp->data.u.meth.str.data = txn->req.sl.rq.m_l;
488 smp->data.u.meth.str.area = ci_head(txn->req.chn);
489 }
490 smp->flags |= SMP_F_VOL_1ST;
Willy Tarreau79e57332018-10-02 16:01:16 +0200491 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200492 return 1;
493}
494
495static int smp_fetch_rqver(const struct arg *args, struct sample *smp, const char *kw, void *private)
496{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200497 struct channel *chn = SMP_REQ_CHN(smp);
Willy Tarreau79e57332018-10-02 16:01:16 +0200498 struct http_txn *txn;
499 char *ptr;
500 int len;
501
Christopher Faulet46575cd2019-04-17 11:40:30 +0200502 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200503 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +0200504 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100505 struct htx_sl *sl;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200506
507 if (!htx)
508 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200509
Christopher Faulet297fbb42019-05-13 14:41:27 +0200510 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100511 len = HTX_SL_REQ_VLEN(sl);
512 ptr = HTX_SL_REQ_VPTR(sl);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200513 }
514 else {
515 /* LEGACY version */
Christopher Faulet89dc4992019-04-17 12:02:59 +0200516 CHECK_HTTP_MESSAGE_FIRST(chn);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200517
518 txn = smp->strm->txn;
519 len = txn->req.sl.rq.v_l;
Christopher Faulet89dc4992019-04-17 12:02:59 +0200520 ptr = ci_head(chn) + txn->req.sl.rq.v;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200521 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200522
523 while ((len-- > 0) && (*ptr++ != '/'));
524 if (len <= 0)
525 return 0;
526
527 smp->data.type = SMP_T_STR;
528 smp->data.u.str.area = ptr;
529 smp->data.u.str.data = len;
530
531 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
532 return 1;
533}
534
535static int smp_fetch_stver(const struct arg *args, struct sample *smp, const char *kw, void *private)
536{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200537 struct channel *chn = SMP_RES_CHN(smp);
Willy Tarreau79e57332018-10-02 16:01:16 +0200538 struct http_txn *txn;
539 char *ptr;
540 int len;
541
Christopher Faulet46575cd2019-04-17 11:40:30 +0200542 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200543 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +0200544 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100545 struct htx_sl *sl;
Willy Tarreau79e57332018-10-02 16:01:16 +0200546
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200547 if (!htx)
548 return 0;
549
Christopher Faulet297fbb42019-05-13 14:41:27 +0200550 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100551 len = HTX_SL_RES_VLEN(sl);
552 ptr = HTX_SL_RES_VPTR(sl);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200553 }
554 else {
555 /* LEGACY version */
Christopher Faulet89dc4992019-04-17 12:02:59 +0200556 CHECK_HTTP_MESSAGE_FIRST(chn);
Willy Tarreau79e57332018-10-02 16:01:16 +0200557
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200558 txn = smp->strm->txn;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200559 len = txn->rsp.sl.st.v_l;
Christopher Faulet89dc4992019-04-17 12:02:59 +0200560 ptr = ci_head(chn);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200561 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200562
563 while ((len-- > 0) && (*ptr++ != '/'));
564 if (len <= 0)
565 return 0;
566
567 smp->data.type = SMP_T_STR;
568 smp->data.u.str.area = ptr;
569 smp->data.u.str.data = len;
570
571 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
572 return 1;
573}
574
575/* 3. Check on Status Code. We manipulate integers here. */
576static int smp_fetch_stcode(const struct arg *args, struct sample *smp, const char *kw, void *private)
577{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200578 struct channel *chn = SMP_RES_CHN(smp);
Willy Tarreau79e57332018-10-02 16:01:16 +0200579 struct http_txn *txn;
580 char *ptr;
581 int len;
582
Christopher Faulet46575cd2019-04-17 11:40:30 +0200583 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200584 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +0200585 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100586 struct htx_sl *sl;
Willy Tarreau79e57332018-10-02 16:01:16 +0200587
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200588 if (!htx)
589 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200590
Christopher Faulet297fbb42019-05-13 14:41:27 +0200591 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100592 len = HTX_SL_RES_CLEN(sl);
593 ptr = HTX_SL_RES_CPTR(sl);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200594 }
595 else {
596 /* LEGACY version */
Christopher Faulet89dc4992019-04-17 12:02:59 +0200597 CHECK_HTTP_MESSAGE_FIRST(chn);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200598
599 txn = smp->strm->txn;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200600 len = txn->rsp.sl.st.c_l;
Christopher Faulet89dc4992019-04-17 12:02:59 +0200601 ptr = ci_head(chn) + txn->rsp.sl.st.c;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200602 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200603
604 smp->data.type = SMP_T_SINT;
605 smp->data.u.sint = __strl2ui(ptr, len);
606 smp->flags = SMP_F_VOL_1ST;
607 return 1;
608}
609
610static int smp_fetch_uniqueid(const struct arg *args, struct sample *smp, const char *kw, void *private)
611{
612 if (LIST_ISEMPTY(&smp->sess->fe->format_unique_id))
613 return 0;
614
Willy Tarreauc1534622020-04-29 11:50:38 +0200615 if (!smp->strm)
616 return 0;
617
Willy Tarreau79e57332018-10-02 16:01:16 +0200618 if (!smp->strm->unique_id) {
619 if ((smp->strm->unique_id = pool_alloc(pool_head_uniqueid)) == NULL)
620 return 0;
621 smp->strm->unique_id[0] = '\0';
Tim Duesterhusea23a5c2020-02-26 16:20:49 +0100622 build_logline(smp->strm, smp->strm->unique_id,
623 UNIQUEID_LEN, &smp->sess->fe->format_unique_id);
Willy Tarreau79e57332018-10-02 16:01:16 +0200624 }
Tim Duesterhusea23a5c2020-02-26 16:20:49 +0100625 smp->data.u.str.data = strlen(smp->strm->unique_id);
Willy Tarreau79e57332018-10-02 16:01:16 +0200626 smp->data.type = SMP_T_STR;
627 smp->data.u.str.area = smp->strm->unique_id;
628 smp->flags = SMP_F_CONST;
629 return 1;
630}
631
632/* Returns a string block containing all headers including the
Joseph Herlant942eea32018-11-15 13:57:22 -0800633 * empty line which separes headers from the body. This is useful
634 * for some headers analysis.
Willy Tarreau79e57332018-10-02 16:01:16 +0200635 */
636static int smp_fetch_hdrs(const struct arg *args, struct sample *smp, const char *kw, void *private)
637{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200638 struct channel *chn = SMP_REQ_CHN(smp);
Willy Tarreau79e57332018-10-02 16:01:16 +0200639 struct http_txn *txn;
640
Christopher Faulet46575cd2019-04-17 11:40:30 +0200641 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200642 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +0200643 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200644 struct buffer *temp;
645 int32_t pos;
Willy Tarreau79e57332018-10-02 16:01:16 +0200646
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200647 if (!htx)
648 return 0;
649 temp = get_trash_chunk();
Christopher Fauleta3f15502019-05-13 15:27:23 +0200650 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200651 struct htx_blk *blk = htx_get_blk(htx, pos);
652 enum htx_blk_type type = htx_get_blk_type(blk);
Willy Tarreau79e57332018-10-02 16:01:16 +0200653
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200654 if (type == HTX_BLK_HDR) {
655 struct ist n = htx_get_blk_name(htx, blk);
656 struct ist v = htx_get_blk_value(htx, blk);
657
Christopher Fauletc59ff232018-12-03 13:58:44 +0100658 if (!htx_hdr_to_h1(n, v, temp))
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200659 return 0;
660 }
661 else if (type == HTX_BLK_EOH) {
662 if (!chunk_memcat(temp, "\r\n", 2))
663 return 0;
664 break;
665 }
666 }
667 smp->data.type = SMP_T_STR;
668 smp->data.u.str = *temp;
669
670 }
671 else {
672 /* LEGACY version */
673 struct http_msg *msg;
674 struct hdr_idx *idx;
675
Christopher Faulet89dc4992019-04-17 12:02:59 +0200676 CHECK_HTTP_MESSAGE_FIRST(chn);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200677
678 txn = smp->strm->txn;
679 idx = &txn->hdr_idx;
680 msg = &txn->req;
Willy Tarreau79e57332018-10-02 16:01:16 +0200681
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200682 smp->data.type = SMP_T_STR;
Christopher Faulet89dc4992019-04-17 12:02:59 +0200683 smp->data.u.str.area = ci_head(chn) + hdr_idx_first_pos(idx);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200684 smp->data.u.str.data = msg->eoh - hdr_idx_first_pos(idx) + 1 +
Christopher Faulet89dc4992019-04-17 12:02:59 +0200685 (ci_head(chn)[msg->eoh] == '\r');
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200686 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200687 return 1;
688}
689
690/* Returns the header request in a length/value encoded format.
691 * This is useful for exchanges with the SPOE.
692 *
693 * A "length value" is a multibyte code encoding numbers. It uses the
694 * SPOE format. The encoding is the following:
695 *
696 * Each couple "header name" / "header value" is composed
697 * like this:
698 * "length value" "header name bytes"
699 * "length value" "header value bytes"
700 * When the last header is reached, the header name and the header
701 * value are empty. Their length are 0
702 */
703static int smp_fetch_hdrs_bin(const struct arg *args, struct sample *smp, const char *kw, void *private)
704{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200705 struct channel *chn = SMP_REQ_CHN(smp);
Willy Tarreau79e57332018-10-02 16:01:16 +0200706 struct http_txn *txn;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200707 struct buffer *temp;
Willy Tarreau79e57332018-10-02 16:01:16 +0200708
Christopher Faulet46575cd2019-04-17 11:40:30 +0200709 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200710 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +0200711 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200712 struct buffer *temp;
713 char *p, *end;
714 int32_t pos;
715 int ret;
Willy Tarreau79e57332018-10-02 16:01:16 +0200716
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200717 if (!htx)
718 return 0;
719 temp = get_trash_chunk();
720 p = temp->area;
721 end = temp->area + temp->size;
Christopher Fauleta3f15502019-05-13 15:27:23 +0200722 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200723 struct htx_blk *blk = htx_get_blk(htx, pos);
724 enum htx_blk_type type = htx_get_blk_type(blk);
725 struct ist n, v;
Willy Tarreau79e57332018-10-02 16:01:16 +0200726
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200727 if (type == HTX_BLK_HDR) {
728 n = htx_get_blk_name(htx,blk);
729 v = htx_get_blk_value(htx, blk);
Willy Tarreau79e57332018-10-02 16:01:16 +0200730
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200731 /* encode the header name. */
732 ret = encode_varint(n.len, &p, end);
733 if (ret == -1)
734 return 0;
735 if (p + n.len > end)
736 return 0;
737 memcpy(p, n.ptr, n.len);
738 p += n.len;
Willy Tarreau79e57332018-10-02 16:01:16 +0200739
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200740 /* encode the header value. */
741 ret = encode_varint(v.len, &p, end);
742 if (ret == -1)
743 return 0;
744 if (p + v.len > end)
745 return 0;
746 memcpy(p, v.ptr, v.len);
747 p += v.len;
Willy Tarreau79e57332018-10-02 16:01:16 +0200748
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200749 }
750 else if (type == HTX_BLK_EOH) {
751 /* encode the end of the header list with empty
752 * header name and header value.
753 */
754 ret = encode_varint(0, &p, end);
755 if (ret == -1)
756 return 0;
757 ret = encode_varint(0, &p, end);
758 if (ret == -1)
759 return 0;
760 break;
761 }
762 }
763
764 /* Initialise sample data which will be filled. */
765 smp->data.type = SMP_T_BIN;
766 smp->data.u.str.area = temp->area;
767 smp->data.u.str.data = p - temp->area;
768 smp->data.u.str.size = temp->size;
769 }
770 else {
771 /* LEGACY version */
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200772 struct hdr_idx *idx;
773 const char *cur_ptr, *cur_next, *p;
774 int old_idx, cur_idx;
775 struct hdr_idx_elem *cur_hdr;
776 const char *hn, *hv;
777 int hnl, hvl;
778 int ret;
779 char *buf;
780 char *end;
781
Christopher Faulet89dc4992019-04-17 12:02:59 +0200782 CHECK_HTTP_MESSAGE_FIRST(chn);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200783
784 temp = get_trash_chunk();
785 buf = temp->area;
786 end = temp->area + temp->size;
787
788 txn = smp->strm->txn;
789 idx = &txn->hdr_idx;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200790
791 /* Build array of headers. */
792 old_idx = 0;
Christopher Faulet89dc4992019-04-17 12:02:59 +0200793 cur_next = ci_head(chn) + hdr_idx_first_pos(idx);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200794 while (1) {
795 cur_idx = idx->v[old_idx].next;
796 if (!cur_idx)
797 break;
798 old_idx = cur_idx;
Willy Tarreau79e57332018-10-02 16:01:16 +0200799
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200800 cur_hdr = &idx->v[cur_idx];
801 cur_ptr = cur_next;
802 cur_next = cur_ptr + cur_hdr->len + cur_hdr->cr + 1;
803
804 /* Now we have one full header at cur_ptr of len cur_hdr->len,
805 * and the next header starts at cur_next. We'll check
806 * this header in the list as well as against the default
807 * rule.
808 */
809
810 /* look for ': *'. */
811 hn = cur_ptr;
812 for (p = cur_ptr; p < cur_ptr + cur_hdr->len && *p != ':'; p++);
813 if (p >= cur_ptr+cur_hdr->len)
814 continue;
815 hnl = p - hn;
Willy Tarreau79e57332018-10-02 16:01:16 +0200816 p++;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200817 while (p < cur_ptr + cur_hdr->len && (*p == ' ' || *p == '\t'))
818 p++;
819 if (p >= cur_ptr + cur_hdr->len)
820 continue;
821 hv = p;
822 hvl = cur_ptr + cur_hdr->len-p;
Willy Tarreau79e57332018-10-02 16:01:16 +0200823
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200824 /* encode the header name. */
825 ret = encode_varint(hnl, &buf, end);
826 if (ret == -1)
827 return 0;
828 if (buf + hnl > end)
829 return 0;
830 memcpy(buf, hn, hnl);
831 buf += hnl;
832
833 /* encode and copy the value. */
834 ret = encode_varint(hvl, &buf, end);
835 if (ret == -1)
836 return 0;
837 if (buf + hvl > end)
838 return 0;
839 memcpy(buf, hv, hvl);
840 buf += hvl;
841 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200842
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200843 /* encode the end of the header list with empty
844 * header name and header value.
845 */
846 ret = encode_varint(0, &buf, end);
Willy Tarreau79e57332018-10-02 16:01:16 +0200847 if (ret == -1)
848 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200849 ret = encode_varint(0, &buf, end);
850 if (ret == -1)
Willy Tarreau79e57332018-10-02 16:01:16 +0200851 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200852
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200853 /* Initialise sample data which will be filled. */
854 smp->data.type = SMP_T_BIN;
855 smp->data.u.str.area = temp->area;
856 smp->data.u.str.data = buf - temp->area;
857 smp->data.u.str.size = temp->size;
858 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200859 return 1;
860}
861
862/* returns the longest available part of the body. This requires that the body
863 * has been waited for using http-buffer-request.
864 */
865static int smp_fetch_body(const struct arg *args, struct sample *smp, const char *kw, void *private)
866{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200867 struct channel *chn = SMP_REQ_CHN(smp);
Willy Tarreau79e57332018-10-02 16:01:16 +0200868 struct buffer *temp;
869
Christopher Faulet46575cd2019-04-17 11:40:30 +0200870 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200871 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +0200872 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200873 int32_t pos;
Willy Tarreau79e57332018-10-02 16:01:16 +0200874
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200875 if (!htx)
876 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200877
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200878 temp = get_trash_chunk();
Christopher Fauleta3f15502019-05-13 15:27:23 +0200879 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200880 struct htx_blk *blk = htx_get_blk(htx, pos);
881 enum htx_blk_type type = htx_get_blk_type(blk);
Willy Tarreau79e57332018-10-02 16:01:16 +0200882
Christopher Faulet54b5e212019-06-04 10:08:28 +0200883 if (type == HTX_BLK_EOM || type == HTX_BLK_TLR || type == HTX_BLK_EOT)
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200884 break;
885 if (type == HTX_BLK_DATA) {
Christopher Fauletc59ff232018-12-03 13:58:44 +0100886 if (!htx_data_to_h1(htx_get_blk_value(htx, blk), temp, 0))
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200887 return 0;
888 }
889 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200890
Willy Tarreau79e57332018-10-02 16:01:16 +0200891 smp->data.type = SMP_T_BIN;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200892 smp->data.u.str = *temp;
893 smp->flags = SMP_F_VOL_TEST;
Willy Tarreau79e57332018-10-02 16:01:16 +0200894 }
895 else {
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200896 /* LEGACY version */
897 struct http_msg *msg;
898 unsigned long len;
899 unsigned long block1;
900 char *body;
901
Christopher Faulet89dc4992019-04-17 12:02:59 +0200902 CHECK_HTTP_MESSAGE_FIRST(chn);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200903
Christopher Faulet89dc4992019-04-17 12:02:59 +0200904 msg = &smp->strm->txn->req;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200905 len = http_body_bytes(msg);
Christopher Faulet89dc4992019-04-17 12:02:59 +0200906 body = c_ptr(chn, -http_data_rewind(msg));
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200907
908 block1 = len;
Christopher Faulet89dc4992019-04-17 12:02:59 +0200909 if (block1 > b_wrap(&chn->buf) - body)
910 block1 = b_wrap(&chn->buf) - body;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200911
912 if (block1 == len) {
913 /* buffer is not wrapped (or empty) */
914 smp->data.type = SMP_T_BIN;
915 smp->data.u.str.area = body;
916 smp->data.u.str.data = len;
917 smp->flags = SMP_F_VOL_TEST | SMP_F_CONST;
918 }
919 else {
920 /* buffer is wrapped, we need to defragment it */
921 temp = get_trash_chunk();
922 memcpy(temp->area, body, block1);
Christopher Faulet89dc4992019-04-17 12:02:59 +0200923 memcpy(temp->area + block1, b_orig(&chn->buf), len - block1);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200924 smp->data.type = SMP_T_BIN;
925 smp->data.u.str.area = temp->area;
926 smp->data.u.str.data = len;
927 smp->flags = SMP_F_VOL_TEST;
928 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200929 }
930 return 1;
931}
932
933
934/* returns the available length of the body. This requires that the body
935 * has been waited for using http-buffer-request.
936 */
937static int smp_fetch_body_len(const struct arg *args, struct sample *smp, const char *kw, void *private)
938{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200939 struct channel *chn = SMP_REQ_CHN(smp);
940
Christopher Faulet46575cd2019-04-17 11:40:30 +0200941 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200942 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +0200943 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Dragan Dosen5a606682019-02-14 12:30:53 +0100944 int32_t pos;
Christopher Fauletc16317d2018-12-12 14:11:22 +0100945 unsigned long long len = 0;
946
947 if (!htx)
948 return 0;
949
Christopher Fauleta3f15502019-05-13 15:27:23 +0200950 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Dragan Dosen5a606682019-02-14 12:30:53 +0100951 struct htx_blk *blk = htx_get_blk(htx, pos);
952 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Fauletc16317d2018-12-12 14:11:22 +0100953
Christopher Faulet54b5e212019-06-04 10:08:28 +0200954 if (type == HTX_BLK_EOM || type == HTX_BLK_TLR || type == HTX_BLK_EOT)
Christopher Fauletc16317d2018-12-12 14:11:22 +0100955 break;
Dragan Dosen5a606682019-02-14 12:30:53 +0100956 if (type == HTX_BLK_DATA)
957 len += htx_get_blksz(blk);
Christopher Fauletc16317d2018-12-12 14:11:22 +0100958 }
959
960 smp->data.type = SMP_T_SINT;
961 smp->data.u.sint = len;
962
963 smp->flags = SMP_F_VOL_TEST;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200964 }
965 else {
966 /* LEGACY version */
967 struct http_msg *msg;
Willy Tarreau79e57332018-10-02 16:01:16 +0200968
Christopher Faulet89dc4992019-04-17 12:02:59 +0200969 CHECK_HTTP_MESSAGE_FIRST(chn);
Willy Tarreau79e57332018-10-02 16:01:16 +0200970
Christopher Faulet89dc4992019-04-17 12:02:59 +0200971 msg = &smp->strm->txn->req;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200972 smp->data.type = SMP_T_SINT;
973 smp->data.u.sint = http_body_bytes(msg);
Willy Tarreau79e57332018-10-02 16:01:16 +0200974
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200975 smp->flags = SMP_F_VOL_TEST;
976 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200977 return 1;
978}
979
980
981/* returns the advertised length of the body, or the advertised size of the
982 * chunks available in the buffer. This requires that the body has been waited
983 * for using http-buffer-request.
984 */
985static int smp_fetch_body_size(const struct arg *args, struct sample *smp, const char *kw, void *private)
986{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200987 struct channel *chn = SMP_REQ_CHN(smp);
988
Christopher Faulet46575cd2019-04-17 11:40:30 +0200989 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200990 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +0200991 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Dragan Dosen5a606682019-02-14 12:30:53 +0100992 int32_t pos;
Christopher Fauletc16317d2018-12-12 14:11:22 +0100993 unsigned long long len = 0;
994
995 if (!htx)
996 return 0;
997
Christopher Fauleta3f15502019-05-13 15:27:23 +0200998 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Dragan Dosen5a606682019-02-14 12:30:53 +0100999 struct htx_blk *blk = htx_get_blk(htx, pos);
1000 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Fauletc16317d2018-12-12 14:11:22 +01001001
Christopher Faulet54b5e212019-06-04 10:08:28 +02001002 if (type == HTX_BLK_EOM || type == HTX_BLK_TLR || type == HTX_BLK_EOT)
Christopher Fauletc16317d2018-12-12 14:11:22 +01001003 break;
Dragan Dosen5a606682019-02-14 12:30:53 +01001004 if (type == HTX_BLK_DATA)
1005 len += htx_get_blksz(blk);
Christopher Fauletc16317d2018-12-12 14:11:22 +01001006 }
1007 if (htx->extra != ULLONG_MAX)
1008 len += htx->extra;
1009
1010 smp->data.type = SMP_T_SINT;
1011 smp->data.u.sint = len;
1012
1013 smp->flags = SMP_F_VOL_TEST;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001014 }
1015 else {
1016 /* LEGACY version */
1017 struct http_msg *msg;
Willy Tarreau79e57332018-10-02 16:01:16 +02001018
Christopher Faulet89dc4992019-04-17 12:02:59 +02001019 CHECK_HTTP_MESSAGE_FIRST(chn);
Willy Tarreau79e57332018-10-02 16:01:16 +02001020
Christopher Faulet89dc4992019-04-17 12:02:59 +02001021 msg = &smp->strm->txn->req;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001022 smp->data.type = SMP_T_SINT;
1023 smp->data.u.sint = msg->body_len;
Willy Tarreau79e57332018-10-02 16:01:16 +02001024
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001025 smp->flags = SMP_F_VOL_TEST;
1026 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001027 return 1;
1028}
1029
1030
1031/* 4. Check on URL/URI. A pointer to the URI is stored. */
1032static int smp_fetch_url(const struct arg *args, struct sample *smp, const char *kw, void *private)
1033{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001034 struct channel *chn = SMP_REQ_CHN(smp);
Willy Tarreau79e57332018-10-02 16:01:16 +02001035 struct http_txn *txn;
1036
Christopher Faulet46575cd2019-04-17 11:40:30 +02001037 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001038 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +02001039 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001040 struct htx_sl *sl;
Willy Tarreau79e57332018-10-02 16:01:16 +02001041
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001042 if (!htx)
1043 return 0;
Christopher Faulet297fbb42019-05-13 14:41:27 +02001044 sl = http_get_stline(htx);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001045 smp->data.type = SMP_T_STR;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001046 smp->data.u.str.area = HTX_SL_REQ_UPTR(sl);
1047 smp->data.u.str.data = HTX_SL_REQ_ULEN(sl);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001048 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
1049 }
1050 else {
1051 /* LEGACY version */
Christopher Faulet89dc4992019-04-17 12:02:59 +02001052 CHECK_HTTP_MESSAGE_FIRST(chn);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001053
1054 txn = smp->strm->txn;
1055 smp->data.type = SMP_T_STR;
1056 smp->data.u.str.data = txn->req.sl.rq.u_l;
Christopher Faulet89dc4992019-04-17 12:02:59 +02001057 smp->data.u.str.area = ci_head(chn) + txn->req.sl.rq.u;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001058 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
1059 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001060 return 1;
1061}
1062
1063static int smp_fetch_url_ip(const struct arg *args, struct sample *smp, const char *kw, void *private)
1064{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001065 struct channel *chn = SMP_REQ_CHN(smp);
Willy Tarreau79e57332018-10-02 16:01:16 +02001066 struct http_txn *txn;
1067 struct sockaddr_storage addr;
1068
Christopher Faulet46575cd2019-04-17 11:40:30 +02001069 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001070 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +02001071 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001072 struct htx_sl *sl;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001073
1074 if (!htx)
1075 return 0;
Christopher Faulet297fbb42019-05-13 14:41:27 +02001076 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001077 url2sa(HTX_SL_REQ_UPTR(sl), HTX_SL_REQ_ULEN(sl), &addr, NULL);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001078 }
1079 else {
1080 /* LEGACY version */
Christopher Faulet89dc4992019-04-17 12:02:59 +02001081 CHECK_HTTP_MESSAGE_FIRST(chn);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001082
1083 txn = smp->strm->txn;
Christopher Faulet89dc4992019-04-17 12:02:59 +02001084 url2sa(ci_head(chn) + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &addr, NULL);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001085 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001086
Willy Tarreau79e57332018-10-02 16:01:16 +02001087 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
1088 return 0;
1089
1090 smp->data.type = SMP_T_IPV4;
1091 smp->data.u.ipv4 = ((struct sockaddr_in *)&addr)->sin_addr;
1092 smp->flags = 0;
1093 return 1;
1094}
1095
1096static int smp_fetch_url_port(const struct arg *args, struct sample *smp, const char *kw, void *private)
1097{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001098 struct channel *chn = SMP_REQ_CHN(smp);
Willy Tarreau79e57332018-10-02 16:01:16 +02001099 struct http_txn *txn;
1100 struct sockaddr_storage addr;
1101
Christopher Faulet46575cd2019-04-17 11:40:30 +02001102 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001103 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +02001104 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001105 struct htx_sl *sl;
Willy Tarreau79e57332018-10-02 16:01:16 +02001106
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001107 if (!htx)
1108 return 0;
Christopher Faulet297fbb42019-05-13 14:41:27 +02001109 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001110 url2sa(HTX_SL_REQ_UPTR(sl), HTX_SL_REQ_ULEN(sl), &addr, NULL);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001111 }
1112 else {
1113 /* LEGACY version */
Christopher Faulet89dc4992019-04-17 12:02:59 +02001114 CHECK_HTTP_MESSAGE_FIRST(chn);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001115
1116 txn = smp->strm->txn;
Christopher Faulet89dc4992019-04-17 12:02:59 +02001117 url2sa(ci_head(chn) + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &addr, NULL);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001118 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001119 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
1120 return 0;
1121
1122 smp->data.type = SMP_T_SINT;
1123 smp->data.u.sint = ntohs(((struct sockaddr_in *)&addr)->sin_port);
1124 smp->flags = 0;
1125 return 1;
1126}
1127
1128/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
1129 * Accepts an optional argument of type string containing the header field name,
1130 * and an optional argument of type signed or unsigned integer to request an
1131 * explicit occurrence of the header. Note that in the event of a missing name,
1132 * headers are considered from the first one. It does not stop on commas and
1133 * returns full lines instead (useful for User-Agent or Date for example).
1134 */
1135static int smp_fetch_fhdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
1136{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001137 /* possible keywords: req.fhdr, res.fhdr */
1138 struct channel *chn = ((kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Willy Tarreau79e57332018-10-02 16:01:16 +02001139 int occ = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001140
Christopher Faulet46575cd2019-04-17 11:40:30 +02001141 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001142 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +02001143 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001144 struct http_hdr_ctx *ctx = smp->ctx.a[0];
1145 struct ist name;
Willy Tarreau79e57332018-10-02 16:01:16 +02001146
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001147 if (!ctx) {
1148 /* first call */
1149 ctx = &static_http_hdr_ctx;
1150 ctx->blk = NULL;
1151 smp->ctx.a[0] = ctx;
1152 }
1153
1154 if (args) {
1155 if (args[0].type != ARGT_STR)
1156 return 0;
1157 name.ptr = args[0].data.str.area;
1158 name.len = args[0].data.str.data;
1159
1160 if (args[1].type == ARGT_SINT)
1161 occ = args[1].data.sint;
1162 }
1163
1164 if (!htx)
Willy Tarreau79e57332018-10-02 16:01:16 +02001165 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001166
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001167 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
1168 /* search for header from the beginning */
1169 ctx->blk = NULL;
1170
1171 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
1172 /* no explicit occurrence and single fetch => last header by default */
1173 occ = -1;
1174
1175 if (!occ)
1176 /* prepare to report multiple occurrences for ACL fetches */
1177 smp->flags |= SMP_F_NOT_LAST;
1178
1179 smp->data.type = SMP_T_STR;
1180 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
1181 if (http_get_htx_fhdr(htx, name, occ, ctx, &smp->data.u.str.area, &smp->data.u.str.data))
1182 return 1;
Willy Tarreau79e57332018-10-02 16:01:16 +02001183 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001184 else {
1185 /* LEGACY version */
1186 struct hdr_idx *idx;
1187 struct hdr_ctx *ctx = smp->ctx.a[0];
1188 const struct http_msg *msg;
1189 const char *name_str = NULL;
1190 int name_len = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001191
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001192 if (!ctx) {
1193 /* first call */
1194 ctx = &static_hdr_ctx;
1195 ctx->idx = 0;
1196 smp->ctx.a[0] = ctx;
1197 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001198
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001199 if (args) {
1200 if (args[0].type != ARGT_STR)
1201 return 0;
1202 name_str = args[0].data.str.area;
1203 name_len = args[0].data.str.data;
Willy Tarreau79e57332018-10-02 16:01:16 +02001204
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001205 if (args[1].type == ARGT_SINT)
1206 occ = args[1].data.sint;
1207 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001208
Christopher Faulet89dc4992019-04-17 12:02:59 +02001209 CHECK_HTTP_MESSAGE_FIRST(chn);
Willy Tarreau79e57332018-10-02 16:01:16 +02001210
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001211 idx = &smp->strm->txn->hdr_idx;
Christopher Faulet89dc4992019-04-17 12:02:59 +02001212 msg = (!(chn->flags & CF_ISRESP) ? &smp->strm->txn->req : &smp->strm->txn->rsp);
Willy Tarreau79e57332018-10-02 16:01:16 +02001213
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001214 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
1215 /* search for header from the beginning */
1216 ctx->idx = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001217
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001218 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
1219 /* no explicit occurrence and single fetch => last header by default */
1220 occ = -1;
1221
1222 if (!occ)
1223 /* prepare to report multiple occurrences for ACL fetches */
1224 smp->flags |= SMP_F_NOT_LAST;
1225
1226 smp->data.type = SMP_T_STR;
1227 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
1228 if (http_get_fhdr(msg, name_str, name_len, idx, occ, ctx, &smp->data.u.str.area, &smp->data.u.str.data))
1229 return 1;
1230 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001231 smp->flags &= ~SMP_F_NOT_LAST;
1232 return 0;
1233}
1234
1235/* 6. Check on HTTP header count. The number of occurrences is returned.
1236 * Accepts exactly 1 argument of type string. It does not stop on commas and
1237 * returns full lines instead (useful for User-Agent or Date for example).
1238 */
1239static int smp_fetch_fhdr_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
1240{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001241 /* possible keywords: req.fhdr_cnt, res.fhdr_cnt */
1242 struct channel *chn = ((kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Willy Tarreau79e57332018-10-02 16:01:16 +02001243 int cnt;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001244
Christopher Faulet46575cd2019-04-17 11:40:30 +02001245 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001246 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +02001247 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001248 struct http_hdr_ctx ctx;
1249 struct ist name;
1250
1251 if (!htx)
1252 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001253
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001254 if (args && args->type == ARGT_STR) {
1255 name.ptr = args->data.str.area;
1256 name.len = args->data.str.data;
Olivier Houcharde2c78cd2018-11-21 13:49:48 +01001257 } else {
1258 name.ptr = NULL;
1259 name.len = 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001260 }
1261
1262 ctx.blk = NULL;
1263 cnt = 0;
1264 while (http_find_header(htx, name, &ctx, 1))
1265 cnt++;
Willy Tarreau79e57332018-10-02 16:01:16 +02001266 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001267 else {
1268 /* LEGACY version */
1269 struct hdr_idx *idx;
1270 struct hdr_ctx ctx;
1271 const struct http_msg *msg;
1272 const char *name = NULL;
1273 int len = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001274
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001275 if (args && args->type == ARGT_STR) {
1276 name = args->data.str.area;
1277 len = args->data.str.data;
1278 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001279
Christopher Faulet89dc4992019-04-17 12:02:59 +02001280 CHECK_HTTP_MESSAGE_FIRST(chn);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001281
1282 idx = &smp->strm->txn->hdr_idx;
Christopher Faulet89dc4992019-04-17 12:02:59 +02001283 msg = (!(chn->flags & CF_ISRESP) ? &smp->strm->txn->req : &smp->strm->txn->rsp);
Willy Tarreau79e57332018-10-02 16:01:16 +02001284
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001285 ctx.idx = 0;
1286 cnt = 0;
1287 while (http_find_full_header2(name, len, ci_head(msg->chn), idx, &ctx))
1288 cnt++;
1289 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001290
1291 smp->data.type = SMP_T_SINT;
1292 smp->data.u.sint = cnt;
1293 smp->flags = SMP_F_VOL_HDR;
1294 return 1;
1295}
1296
1297static int smp_fetch_hdr_names(const struct arg *args, struct sample *smp, const char *kw, void *private)
1298{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001299 /* possible keywords: req.hdr_names, res.hdr_names */
1300 struct channel *chn = ((kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Willy Tarreau79e57332018-10-02 16:01:16 +02001301 struct buffer *temp;
1302 char del = ',';
1303
Christopher Faulet46575cd2019-04-17 11:40:30 +02001304 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001305 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +02001306 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001307 int32_t pos;
Willy Tarreau79e57332018-10-02 16:01:16 +02001308
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001309 if (!htx)
1310 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001311
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001312 if (args && args->type == ARGT_STR)
1313 del = *args[0].data.str.area;
Willy Tarreau79e57332018-10-02 16:01:16 +02001314
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001315 temp = get_trash_chunk();
Christopher Fauleta3f15502019-05-13 15:27:23 +02001316 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001317 struct htx_blk *blk = htx_get_blk(htx, pos);
1318 enum htx_blk_type type = htx_get_blk_type(blk);
1319 struct ist n;
Willy Tarreau79e57332018-10-02 16:01:16 +02001320
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001321 if (type == HTX_BLK_EOH)
1322 break;
1323 if (type != HTX_BLK_HDR)
1324 continue;
1325 n = htx_get_blk_name(htx, blk);
1326
1327 if (temp->data)
1328 temp->area[temp->data++] = del;
1329 chunk_memcat(temp, n.ptr, n.len);
1330 }
1331 }
1332 else {
1333 /* LEGACY version */
1334 struct hdr_idx *idx;
1335 struct hdr_ctx ctx;
1336 const struct http_msg *msg;
1337
1338 if (args && args->type == ARGT_STR)
1339 del = *args[0].data.str.area;
1340
Christopher Faulet89dc4992019-04-17 12:02:59 +02001341 CHECK_HTTP_MESSAGE_FIRST(chn);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001342
1343 idx = &smp->strm->txn->hdr_idx;
Christopher Faulet89dc4992019-04-17 12:02:59 +02001344 msg = (!(chn->flags & CF_ISRESP) ? &smp->strm->txn->req : &smp->strm->txn->rsp);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001345
1346 temp = get_trash_chunk();
1347
1348 ctx.idx = 0;
1349 while (http_find_next_header(ci_head(msg->chn), idx, &ctx)) {
1350 if (temp->data)
1351 temp->area[temp->data++] = del;
1352 memcpy(temp->area + temp->data, ctx.line, ctx.del);
1353 temp->data += ctx.del;
1354 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001355 }
1356
1357 smp->data.type = SMP_T_STR;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001358 smp->data.u.str = *temp;
Willy Tarreau79e57332018-10-02 16:01:16 +02001359 smp->flags = SMP_F_VOL_HDR;
1360 return 1;
1361}
1362
1363/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
1364 * Accepts an optional argument of type string containing the header field name,
1365 * and an optional argument of type signed or unsigned integer to request an
1366 * explicit occurrence of the header. Note that in the event of a missing name,
1367 * headers are considered from the first one.
1368 */
1369static int smp_fetch_hdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
1370{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001371 /* possible keywords: req.hdr / hdr, res.hdr / shdr */
1372 struct channel *chn = ((kw[0] == 'h' || kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Willy Tarreau79e57332018-10-02 16:01:16 +02001373 int occ = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001374
Christopher Faulet46575cd2019-04-17 11:40:30 +02001375 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001376 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +02001377 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001378 struct http_hdr_ctx *ctx = smp->ctx.a[0];
1379 struct ist name;
1380
1381 if (!ctx) {
1382 /* first call */
1383 ctx = &static_http_hdr_ctx;
1384 ctx->blk = NULL;
1385 smp->ctx.a[0] = ctx;
1386 }
1387
1388 if (args) {
1389 if (args[0].type != ARGT_STR)
1390 return 0;
1391 name.ptr = args[0].data.str.area;
1392 name.len = args[0].data.str.data;
1393
1394 if (args[1].type == ARGT_SINT)
1395 occ = args[1].data.sint;
1396 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001397
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001398 if (!htx)
Willy Tarreau79e57332018-10-02 16:01:16 +02001399 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001400
1401 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
1402 /* search for header from the beginning */
1403 ctx->blk = NULL;
1404
1405 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
1406 /* no explicit occurrence and single fetch => last header by default */
1407 occ = -1;
1408
1409 if (!occ)
1410 /* prepare to report multiple occurrences for ACL fetches */
1411 smp->flags |= SMP_F_NOT_LAST;
Willy Tarreau79e57332018-10-02 16:01:16 +02001412
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001413 smp->data.type = SMP_T_STR;
1414 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
1415 if (http_get_htx_hdr(htx, name, occ, ctx, &smp->data.u.str.area, &smp->data.u.str.data))
1416 return 1;
Willy Tarreau79e57332018-10-02 16:01:16 +02001417 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001418 else {
1419 /* LEGACY version */
1420 struct hdr_idx *idx;
1421 struct hdr_ctx *ctx = smp->ctx.a[0];
1422 const struct http_msg *msg;
1423 const char *name_str = NULL;
1424 int name_len = 0;
1425
1426 if (!ctx) {
1427 /* first call */
1428 ctx = &static_hdr_ctx;
1429 ctx->idx = 0;
1430 smp->ctx.a[0] = ctx;
1431 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001432
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001433 if (args) {
1434 if (args[0].type != ARGT_STR)
1435 return 0;
1436 name_str = args[0].data.str.area;
1437 name_len = args[0].data.str.data;
Willy Tarreau79e57332018-10-02 16:01:16 +02001438
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001439 if (args[1].type == ARGT_SINT)
1440 occ = args[1].data.sint;
1441 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001442
Christopher Faulet89dc4992019-04-17 12:02:59 +02001443 CHECK_HTTP_MESSAGE_FIRST(chn);
Willy Tarreau79e57332018-10-02 16:01:16 +02001444
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001445 idx = &smp->strm->txn->hdr_idx;
Christopher Faulet89dc4992019-04-17 12:02:59 +02001446 msg = (!(chn->flags & CF_ISRESP) ? &smp->strm->txn->req : &smp->strm->txn->rsp);
Willy Tarreau79e57332018-10-02 16:01:16 +02001447
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001448 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
1449 /* search for header from the beginning */
1450 ctx->idx = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001451
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001452 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
1453 /* no explicit occurrence and single fetch => last header by default */
1454 occ = -1;
1455
1456 if (!occ)
1457 /* prepare to report multiple occurrences for ACL fetches */
1458 smp->flags |= SMP_F_NOT_LAST;
1459
1460 smp->data.type = SMP_T_STR;
1461 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
1462 if (http_get_hdr(msg, name_str, name_len, idx, occ, ctx, &smp->data.u.str.area, &smp->data.u.str.data))
1463 return 1;
1464 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001465
1466 smp->flags &= ~SMP_F_NOT_LAST;
1467 return 0;
1468}
1469
Christopher Fauletc1f40dd2019-05-16 10:07:30 +02001470/* Same than smp_fetch_hdr() but only relies on the sample direction to choose
1471 * the right channel. So instead of duplicating the code, we just change the
1472 * keyword and then fallback on smp_fetch_hdr().
1473 */
1474static int smp_fetch_chn_hdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
1475{
1476 kw = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ ? "req.hdr" : "res.hdr");
1477 return smp_fetch_hdr(args, smp, kw, private);
1478}
1479
Willy Tarreau79e57332018-10-02 16:01:16 +02001480/* 6. Check on HTTP header count. The number of occurrences is returned.
1481 * Accepts exactly 1 argument of type string.
1482 */
1483static int smp_fetch_hdr_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
1484{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001485 /* possible keywords: req.hdr_cnt / hdr_cnt, res.hdr_cnt / shdr_cnt */
1486 struct channel *chn = ((kw[0] == 'h' || kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Willy Tarreau79e57332018-10-02 16:01:16 +02001487 int cnt;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001488
Christopher Faulet46575cd2019-04-17 11:40:30 +02001489 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001490 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +02001491 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001492 struct http_hdr_ctx ctx;
1493 struct ist name;
1494
1495 if (!htx)
1496 return 0;
1497
1498 if (args && args->type == ARGT_STR) {
1499 name.ptr = args->data.str.area;
1500 name.len = args->data.str.data;
Olivier Houcharde2c78cd2018-11-21 13:49:48 +01001501 } else {
1502 name.ptr = NULL;
1503 name.len = 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001504 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001505
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001506 ctx.blk = NULL;
1507 cnt = 0;
1508 while (http_find_header(htx, name, &ctx, 0))
1509 cnt++;
Willy Tarreau79e57332018-10-02 16:01:16 +02001510 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001511 else {
1512 /* LEGACY version */
1513 struct hdr_idx *idx;
1514 struct hdr_ctx ctx;
1515 const struct http_msg *msg;
1516 const char *name = NULL;
1517 int len = 0;
1518
1519 if (args && args->type == ARGT_STR) {
1520 name = args->data.str.area;
1521 len = args->data.str.data;
1522 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001523
Christopher Faulet89dc4992019-04-17 12:02:59 +02001524 CHECK_HTTP_MESSAGE_FIRST(chn);
Willy Tarreau79e57332018-10-02 16:01:16 +02001525
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001526 idx = &smp->strm->txn->hdr_idx;
Christopher Faulet89dc4992019-04-17 12:02:59 +02001527 msg = (!(chn->flags & CF_ISRESP) ? &smp->strm->txn->req : &smp->strm->txn->rsp);
Willy Tarreau79e57332018-10-02 16:01:16 +02001528
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001529 ctx.idx = 0;
1530 cnt = 0;
1531 while (http_find_header2(name, len, ci_head(msg->chn), idx, &ctx))
1532 cnt++;
1533 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001534
1535 smp->data.type = SMP_T_SINT;
1536 smp->data.u.sint = cnt;
1537 smp->flags = SMP_F_VOL_HDR;
1538 return 1;
1539}
1540
1541/* Fetch an HTTP header's integer value. The integer value is returned. It
1542 * takes a mandatory argument of type string and an optional one of type int
1543 * to designate a specific occurrence. It returns an unsigned integer, which
1544 * may or may not be appropriate for everything.
1545 */
1546static int smp_fetch_hdr_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
1547{
1548 int ret = smp_fetch_hdr(args, smp, kw, private);
1549
1550 if (ret > 0) {
1551 smp->data.type = SMP_T_SINT;
1552 smp->data.u.sint = strl2ic(smp->data.u.str.area,
1553 smp->data.u.str.data);
1554 }
1555
1556 return ret;
1557}
1558
1559/* Fetch an HTTP header's IP value. takes a mandatory argument of type string
1560 * and an optional one of type int to designate a specific occurrence.
1561 * It returns an IPv4 or IPv6 address.
1562 */
1563static int smp_fetch_hdr_ip(const struct arg *args, struct sample *smp, const char *kw, void *private)
1564{
1565 int ret;
Tim Duesterhus8ba978b2020-06-26 15:44:48 +02001566 struct buffer *temp = get_trash_chunk();
Willy Tarreau79e57332018-10-02 16:01:16 +02001567
1568 while ((ret = smp_fetch_hdr(args, smp, kw, private)) > 0) {
Tim Duesterhus8ba978b2020-06-26 15:44:48 +02001569 if (smp->data.u.str.data < temp->size - 1) {
1570 memcpy(temp->area, smp->data.u.str.area,
1571 smp->data.u.str.data);
1572 temp->area[smp->data.u.str.data] = '\0';
1573 if (url2ipv4((char *) temp->area, &smp->data.u.ipv4)) {
1574 smp->data.type = SMP_T_IPV4;
1575 break;
1576 } else if (inet_pton(AF_INET6, temp->area, &smp->data.u.ipv6)) {
1577 smp->data.type = SMP_T_IPV6;
1578 break;
Willy Tarreau79e57332018-10-02 16:01:16 +02001579 }
1580 }
1581
1582 /* if the header doesn't match an IP address, fetch next one */
1583 if (!(smp->flags & SMP_F_NOT_LAST))
1584 return 0;
1585 }
1586 return ret;
1587}
1588
1589/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
1590 * the first '/' after the possible hostname, and ends before the possible '?'.
1591 */
1592static int smp_fetch_path(const struct arg *args, struct sample *smp, const char *kw, void *private)
1593{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001594 struct channel *chn = SMP_REQ_CHN(smp);
1595
Christopher Faulet46575cd2019-04-17 11:40:30 +02001596 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001597 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +02001598 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001599 struct htx_sl *sl;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001600 struct ist path;
1601 size_t len;
Willy Tarreau79e57332018-10-02 16:01:16 +02001602
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001603 if (!htx)
1604 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001605
Christopher Faulet297fbb42019-05-13 14:41:27 +02001606 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001607 path = http_get_path(htx_sl_req_uri(sl));
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001608 if (!path.ptr)
1609 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001610
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001611 for (len = 0; len < path.len && *(path.ptr + len) != '?'; len++)
Dragan Dosen8861e1c2019-02-12 19:50:31 +01001612 ;
Willy Tarreau79e57332018-10-02 16:01:16 +02001613
Dragan Dosen8861e1c2019-02-12 19:50:31 +01001614 /* OK, we got the '/' ! */
1615 smp->data.type = SMP_T_STR;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001616 smp->data.u.str.area = path.ptr;
1617 smp->data.u.str.data = len;
1618 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
1619 }
1620 else {
1621 struct http_txn *txn;
1622 char *ptr, *end;
Willy Tarreau79e57332018-10-02 16:01:16 +02001623
Christopher Faulet89dc4992019-04-17 12:02:59 +02001624 CHECK_HTTP_MESSAGE_FIRST(chn);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001625
1626 txn = smp->strm->txn;
Christopher Faulet89dc4992019-04-17 12:02:59 +02001627 end = ci_head(chn) + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001628 ptr = http_txn_get_path(txn);
1629 if (!ptr)
1630 return 0;
1631
1632 /* OK, we got the '/' ! */
1633 smp->data.type = SMP_T_STR;
1634 smp->data.u.str.area = ptr;
1635
1636 while (ptr < end && *ptr != '?')
1637 ptr++;
1638
1639 smp->data.u.str.data = ptr - smp->data.u.str.area;
1640 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
1641 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001642 return 1;
1643}
1644
1645/* This produces a concatenation of the first occurrence of the Host header
1646 * followed by the path component if it begins with a slash ('/'). This means
1647 * that '*' will not be added, resulting in exactly the first Host entry.
1648 * If no Host header is found, then the path is returned as-is. The returned
1649 * value is stored in the trash so it does not need to be marked constant.
1650 * The returned sample is of type string.
1651 */
1652static int smp_fetch_base(const struct arg *args, struct sample *smp, const char *kw, void *private)
1653{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001654 struct channel *chn = SMP_REQ_CHN(smp);
Willy Tarreau79e57332018-10-02 16:01:16 +02001655 struct buffer *temp;
1656
Christopher Faulet46575cd2019-04-17 11:40:30 +02001657 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001658 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +02001659 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001660 struct htx_sl *sl;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001661 struct http_hdr_ctx ctx;
1662 struct ist path;
Willy Tarreau79e57332018-10-02 16:01:16 +02001663
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001664 if (!htx)
1665 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001666
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001667 ctx.blk = NULL;
1668 if (!http_find_header(htx, ist("Host"), &ctx, 0) || !ctx.value.len)
1669 return smp_fetch_path(args, smp, kw, private);
Willy Tarreau79e57332018-10-02 16:01:16 +02001670
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001671 /* OK we have the header value in ctx.value */
1672 temp = get_trash_chunk();
1673 chunk_memcat(temp, ctx.value.ptr, ctx.value.len);
1674
1675 /* now retrieve the path */
Christopher Faulet297fbb42019-05-13 14:41:27 +02001676 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001677 path = http_get_path(htx_sl_req_uri(sl));
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001678 if (path.ptr) {
1679 size_t len;
1680
Dragan Dosen8861e1c2019-02-12 19:50:31 +01001681 for (len = 0; len < path.len && *(path.ptr + len) != '?'; len++)
1682 ;
1683
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001684 if (len && *(path.ptr) == '/')
1685 chunk_memcat(temp, path.ptr, len);
1686 }
1687
1688 smp->data.type = SMP_T_STR;
1689 smp->data.u.str = *temp;
1690 }
1691 else {
1692 /* LEGACY version */
1693 struct http_txn *txn;
1694 char *ptr, *end, *beg;
1695 struct hdr_ctx ctx;
1696
Christopher Faulet89dc4992019-04-17 12:02:59 +02001697 CHECK_HTTP_MESSAGE_FIRST(chn);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001698
1699 txn = smp->strm->txn;
1700 ctx.idx = 0;
Christopher Faulet89dc4992019-04-17 12:02:59 +02001701 if (!http_find_header2("Host", 4, ci_head(chn), &txn->hdr_idx, &ctx) || !ctx.vlen)
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001702 return smp_fetch_path(args, smp, kw, private);
1703
1704 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
1705 temp = get_trash_chunk();
1706 memcpy(temp->area, ctx.line + ctx.val, ctx.vlen);
1707 smp->data.type = SMP_T_STR;
1708 smp->data.u.str.area = temp->area;
1709 smp->data.u.str.data = ctx.vlen;
Willy Tarreau79e57332018-10-02 16:01:16 +02001710
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001711 /* now retrieve the path */
Christopher Faulet89dc4992019-04-17 12:02:59 +02001712 end = ci_head(chn) + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001713 beg = http_txn_get_path(txn);
1714 if (!beg)
1715 beg = end;
Willy Tarreau79e57332018-10-02 16:01:16 +02001716
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001717 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
1718
1719 if (beg < ptr && *beg == '/') {
1720 memcpy(smp->data.u.str.area + smp->data.u.str.data, beg,
1721 ptr - beg);
1722 smp->data.u.str.data += ptr - beg;
1723 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001724 }
1725
1726 smp->flags = SMP_F_VOL_1ST;
1727 return 1;
1728}
1729
1730/* This produces a 32-bit hash of the concatenation of the first occurrence of
1731 * the Host header followed by the path component if it begins with a slash ('/').
1732 * This means that '*' will not be added, resulting in exactly the first Host
1733 * entry. If no Host header is found, then the path is used. The resulting value
1734 * is hashed using the path hash followed by a full avalanche hash and provides a
1735 * 32-bit integer value. This fetch is useful for tracking per-path activity on
1736 * high-traffic sites without having to store whole paths.
1737 */
1738static int smp_fetch_base32(const struct arg *args, struct sample *smp, const char *kw, void *private)
1739{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001740 struct channel *chn = SMP_REQ_CHN(smp);
Willy Tarreau79e57332018-10-02 16:01:16 +02001741 unsigned int hash = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001742
Christopher Faulet46575cd2019-04-17 11:40:30 +02001743 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001744 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +02001745 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001746 struct htx_sl *sl;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001747 struct http_hdr_ctx ctx;
1748 struct ist path;
Willy Tarreau79e57332018-10-02 16:01:16 +02001749
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001750 if (!htx)
1751 return 0;
1752
1753 ctx.blk = NULL;
Dragan Dosen8861e1c2019-02-12 19:50:31 +01001754 if (http_find_header(htx, ist("Host"), &ctx, 0)) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001755 /* OK we have the header value in ctx.value */
1756 while (ctx.value.len--)
1757 hash = *(ctx.value.ptr++) + (hash << 6) + (hash << 16) - hash;
1758 }
1759
1760 /* now retrieve the path */
Christopher Faulet297fbb42019-05-13 14:41:27 +02001761 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001762 path = http_get_path(htx_sl_req_uri(sl));
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001763 if (path.ptr) {
1764 size_t len;
1765
Dragan Dosen8861e1c2019-02-12 19:50:31 +01001766 for (len = 0; len < path.len && *(path.ptr + len) != '?'; len++)
1767 ;
1768
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001769 if (len && *(path.ptr) == '/') {
1770 while (len--)
1771 hash = *(path.ptr++) + (hash << 6) + (hash << 16) - hash;
1772 }
1773 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001774 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001775 else {
1776 /* LEGACY version */
1777 struct http_txn *txn;
1778 struct hdr_ctx ctx;
1779 char *ptr, *beg, *end;
1780 int len;
1781
Christopher Faulet89dc4992019-04-17 12:02:59 +02001782 CHECK_HTTP_MESSAGE_FIRST(chn);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001783
1784 txn = smp->strm->txn;
1785 ctx.idx = 0;
Christopher Faulet89dc4992019-04-17 12:02:59 +02001786 if (http_find_header2("Host", 4, ci_head(chn), &txn->hdr_idx, &ctx)) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001787 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
1788 ptr = ctx.line + ctx.val;
1789 len = ctx.vlen;
1790 while (len--)
1791 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
1792 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001793
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001794 /* now retrieve the path */
Christopher Faulet89dc4992019-04-17 12:02:59 +02001795 end = ci_head(chn) + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001796 beg = http_txn_get_path(txn);
1797 if (!beg)
1798 beg = end;
Willy Tarreau79e57332018-10-02 16:01:16 +02001799
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001800 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
Willy Tarreau79e57332018-10-02 16:01:16 +02001801
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001802 if (beg < ptr && *beg == '/') {
1803 while (beg < ptr)
1804 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
1805 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001806 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001807
Willy Tarreau79e57332018-10-02 16:01:16 +02001808 hash = full_hash(hash);
1809
1810 smp->data.type = SMP_T_SINT;
1811 smp->data.u.sint = hash;
1812 smp->flags = SMP_F_VOL_1ST;
1813 return 1;
1814}
1815
1816/* This concatenates the source address with the 32-bit hash of the Host and
1817 * path as returned by smp_fetch_base32(). The idea is to have per-source and
1818 * per-path counters. The result is a binary block from 8 to 20 bytes depending
1819 * on the source address length. The path hash is stored before the address so
1820 * that in environments where IPv6 is insignificant, truncating the output to
1821 * 8 bytes would still work.
1822 */
1823static int smp_fetch_base32_src(const struct arg *args, struct sample *smp, const char *kw, void *private)
1824{
1825 struct buffer *temp;
1826 struct connection *cli_conn = objt_conn(smp->sess->origin);
1827
1828 if (!cli_conn)
1829 return 0;
1830
1831 if (!smp_fetch_base32(args, smp, kw, private))
1832 return 0;
1833
1834 temp = get_trash_chunk();
1835 *(unsigned int *) temp->area = htonl(smp->data.u.sint);
1836 temp->data += sizeof(unsigned int);
1837
1838 switch (cli_conn->addr.from.ss_family) {
1839 case AF_INET:
1840 memcpy(temp->area + temp->data,
1841 &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr,
1842 4);
1843 temp->data += 4;
1844 break;
1845 case AF_INET6:
1846 memcpy(temp->area + temp->data,
1847 &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr,
1848 16);
1849 temp->data += 16;
1850 break;
1851 default:
1852 return 0;
1853 }
1854
1855 smp->data.u.str = *temp;
1856 smp->data.type = SMP_T_BIN;
1857 return 1;
1858}
1859
1860/* Extracts the query string, which comes after the question mark '?'. If no
1861 * question mark is found, nothing is returned. Otherwise it returns a sample
1862 * of type string carrying the whole query string.
1863 */
1864static int smp_fetch_query(const struct arg *args, struct sample *smp, const char *kw, void *private)
1865{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001866 struct channel *chn = SMP_REQ_CHN(smp);
Willy Tarreau79e57332018-10-02 16:01:16 +02001867 char *ptr, *end;
1868
Christopher Faulet46575cd2019-04-17 11:40:30 +02001869 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001870 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +02001871 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001872 struct htx_sl *sl;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001873
1874 if (!htx)
1875 return 0;
1876
Christopher Faulet297fbb42019-05-13 14:41:27 +02001877 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001878 ptr = HTX_SL_REQ_UPTR(sl);
1879 end = HTX_SL_REQ_UPTR(sl) + HTX_SL_REQ_ULEN(sl);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001880 }
1881 else {
1882 /* LEGACY version */
1883 struct http_txn *txn;
1884
Christopher Faulet89dc4992019-04-17 12:02:59 +02001885 CHECK_HTTP_MESSAGE_FIRST(chn);
Willy Tarreau79e57332018-10-02 16:01:16 +02001886
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001887 txn = smp->strm->txn;
Christopher Faulet89dc4992019-04-17 12:02:59 +02001888 ptr = ci_head(chn) + txn->req.sl.rq.u;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001889 end = ptr + txn->req.sl.rq.u_l;
1890 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001891
1892 /* look up the '?' */
1893 do {
1894 if (ptr == end)
1895 return 0;
1896 } while (*ptr++ != '?');
1897
1898 smp->data.type = SMP_T_STR;
1899 smp->data.u.str.area = ptr;
1900 smp->data.u.str.data = end - ptr;
1901 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
1902 return 1;
1903}
1904
1905static int smp_fetch_proto_http(const struct arg *args, struct sample *smp, const char *kw, void *private)
1906{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001907 struct channel *chn = SMP_REQ_CHN(smp);
1908
Christopher Faulet46575cd2019-04-17 11:40:30 +02001909 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001910 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +02001911 struct htx *htx = smp_prefetch_htx(smp, chn, 0);
Willy Tarreau79e57332018-10-02 16:01:16 +02001912
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001913 if (!htx)
1914 return 0;
1915 }
1916 else {
1917 /* LEGACY version */
Willy Tarreau79e57332018-10-02 16:01:16 +02001918
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001919 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
1920 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
1921 */
Christopher Faulet89dc4992019-04-17 12:02:59 +02001922 CHECK_HTTP_MESSAGE_FIRST_PERM(chn);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001923 }
1924 smp->data.type = SMP_T_BOOL;
Willy Tarreau79e57332018-10-02 16:01:16 +02001925 smp->data.u.sint = 1;
1926 return 1;
1927}
1928
1929/* return a valid test if the current request is the first one on the connection */
1930static int smp_fetch_http_first_req(const struct arg *args, struct sample *smp, const char *kw, void *private)
1931{
Willy Tarreaud095c672020-04-29 11:52:13 +02001932 if (!smp->strm)
1933 return 0;
1934
Willy Tarreau79e57332018-10-02 16:01:16 +02001935 smp->data.type = SMP_T_BOOL;
1936 smp->data.u.sint = !(smp->strm->txn->flags & TX_NOT_FIRST);
1937 return 1;
1938}
1939
1940/* Accepts exactly 1 argument of type userlist */
1941static int smp_fetch_http_auth(const struct arg *args, struct sample *smp, const char *kw, void *private)
1942{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001943 struct channel *chn = SMP_REQ_CHN(smp);
Willy Tarreau79e57332018-10-02 16:01:16 +02001944
1945 if (!args || args->type != ARGT_USR)
1946 return 0;
1947
Christopher Faulet46575cd2019-04-17 11:40:30 +02001948 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001949 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +02001950 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Willy Tarreau79e57332018-10-02 16:01:16 +02001951
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001952 if (!htx)
1953 return 0;
Christopher Faulete98411b2019-07-15 13:58:29 +02001954 if (!get_http_auth(smp, htx))
1955 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001956 }
1957 else {
1958 /* LEGACY version */
Christopher Faulet89dc4992019-04-17 12:02:59 +02001959 CHECK_HTTP_MESSAGE_FIRST(chn);
Christopher Faulete98411b2019-07-15 13:58:29 +02001960 if (!get_http_auth(smp, NULL))
1961 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001962 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001963
1964 smp->data.type = SMP_T_BOOL;
1965 smp->data.u.sint = check_user(args->data.usr, smp->strm->txn->auth.user,
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001966 smp->strm->txn->auth.pass);
Willy Tarreau79e57332018-10-02 16:01:16 +02001967 return 1;
1968}
1969
1970/* Accepts exactly 1 argument of type userlist */
1971static int smp_fetch_http_auth_grp(const struct arg *args, struct sample *smp, const char *kw, void *private)
1972{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001973 struct channel *chn = SMP_REQ_CHN(smp);
1974
Willy Tarreau79e57332018-10-02 16:01:16 +02001975 if (!args || args->type != ARGT_USR)
1976 return 0;
1977
Christopher Faulet46575cd2019-04-17 11:40:30 +02001978 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001979 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +02001980 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001981
1982 if (!htx)
1983 return 0;
Christopher Faulete98411b2019-07-15 13:58:29 +02001984 if (!get_http_auth(smp, htx))
1985 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001986 }
1987 else {
1988 /* LEGACY version */
Christopher Faulet89dc4992019-04-17 12:02:59 +02001989 CHECK_HTTP_MESSAGE_FIRST(chn);
Christopher Faulete98411b2019-07-15 13:58:29 +02001990 if (!get_http_auth(smp, NULL))
1991 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001992 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001993
Willy Tarreau79e57332018-10-02 16:01:16 +02001994 /* if the user does not belong to the userlist or has a wrong password,
1995 * report that it unconditionally does not match. Otherwise we return
1996 * a string containing the username.
1997 */
1998 if (!check_user(args->data.usr, smp->strm->txn->auth.user,
1999 smp->strm->txn->auth.pass))
2000 return 0;
2001
2002 /* pat_match_auth() will need the user list */
2003 smp->ctx.a[0] = args->data.usr;
2004
2005 smp->data.type = SMP_T_STR;
2006 smp->flags = SMP_F_CONST;
2007 smp->data.u.str.area = smp->strm->txn->auth.user;
2008 smp->data.u.str.data = strlen(smp->strm->txn->auth.user);
2009
2010 return 1;
2011}
2012
2013/* Fetch a captured HTTP request header. The index is the position of
2014 * the "capture" option in the configuration file
2015 */
2016static int smp_fetch_capture_req_hdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
2017{
Willy Tarreau79ed4672020-04-29 11:44:54 +02002018 struct proxy *fe;
Willy Tarreau79e57332018-10-02 16:01:16 +02002019 int idx;
2020
2021 if (!args || args->type != ARGT_SINT)
2022 return 0;
2023
Willy Tarreau79ed4672020-04-29 11:44:54 +02002024 if (!smp->strm)
2025 return 0;
2026
2027 fe = strm_fe(smp->strm);
Willy Tarreau79e57332018-10-02 16:01:16 +02002028 idx = args->data.sint;
2029
2030 if (idx > (fe->nb_req_cap - 1) || smp->strm->req_cap == NULL || smp->strm->req_cap[idx] == NULL)
2031 return 0;
2032
2033 smp->data.type = SMP_T_STR;
2034 smp->flags |= SMP_F_CONST;
2035 smp->data.u.str.area = smp->strm->req_cap[idx];
2036 smp->data.u.str.data = strlen(smp->strm->req_cap[idx]);
2037
2038 return 1;
2039}
2040
2041/* Fetch a captured HTTP response header. The index is the position of
2042 * the "capture" option in the configuration file
2043 */
2044static int smp_fetch_capture_res_hdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
2045{
Willy Tarreau79ed4672020-04-29 11:44:54 +02002046 struct proxy *fe;
Willy Tarreau79e57332018-10-02 16:01:16 +02002047 int idx;
2048
2049 if (!args || args->type != ARGT_SINT)
2050 return 0;
2051
Willy Tarreau79ed4672020-04-29 11:44:54 +02002052 if (!smp->strm)
2053 return 0;
2054
2055 fe = strm_fe(smp->strm);
Willy Tarreau79e57332018-10-02 16:01:16 +02002056 idx = args->data.sint;
2057
2058 if (idx > (fe->nb_rsp_cap - 1) || smp->strm->res_cap == NULL || smp->strm->res_cap[idx] == NULL)
2059 return 0;
2060
2061 smp->data.type = SMP_T_STR;
2062 smp->flags |= SMP_F_CONST;
2063 smp->data.u.str.area = smp->strm->res_cap[idx];
2064 smp->data.u.str.data = strlen(smp->strm->res_cap[idx]);
2065
2066 return 1;
2067}
2068
2069/* Extracts the METHOD in the HTTP request, the txn->uri should be filled before the call */
2070static int smp_fetch_capture_req_method(const struct arg *args, struct sample *smp, const char *kw, void *private)
2071{
2072 struct buffer *temp;
Willy Tarreau79ed4672020-04-29 11:44:54 +02002073 struct http_txn *txn;
Willy Tarreau79e57332018-10-02 16:01:16 +02002074 char *ptr;
2075
Willy Tarreau79ed4672020-04-29 11:44:54 +02002076 if (!smp->strm)
2077 return 0;
2078
2079 txn = smp->strm->txn;
Willy Tarreau79e57332018-10-02 16:01:16 +02002080 if (!txn || !txn->uri)
2081 return 0;
2082
2083 ptr = txn->uri;
2084
2085 while (*ptr != ' ' && *ptr != '\0') /* find first space */
2086 ptr++;
2087
2088 temp = get_trash_chunk();
2089 temp->area = txn->uri;
2090 temp->data = ptr - txn->uri;
2091 smp->data.u.str = *temp;
2092 smp->data.type = SMP_T_STR;
2093 smp->flags = SMP_F_CONST;
2094
2095 return 1;
2096
2097}
2098
2099/* Extracts the path in the HTTP request, the txn->uri should be filled before the call */
2100static int smp_fetch_capture_req_uri(const struct arg *args, struct sample *smp, const char *kw, void *private)
2101{
Willy Tarreau79ed4672020-04-29 11:44:54 +02002102 struct http_txn *txn;
Willy Tarreau79e57332018-10-02 16:01:16 +02002103 struct ist path;
2104 const char *ptr;
2105
Willy Tarreau79ed4672020-04-29 11:44:54 +02002106 if (!smp->strm)
2107 return 0;
2108
2109 txn = smp->strm->txn;
Willy Tarreau79e57332018-10-02 16:01:16 +02002110 if (!txn || !txn->uri)
2111 return 0;
2112
2113 ptr = txn->uri;
2114
2115 while (*ptr != ' ' && *ptr != '\0') /* find first space */
2116 ptr++;
2117
2118 if (!*ptr)
2119 return 0;
2120
Christopher Faulet78337bb2018-11-15 14:35:18 +01002121 /* skip the first space and find space after URI */
2122 path = ist2(++ptr, 0);
2123 while (*ptr != ' ' && *ptr != '\0')
2124 ptr++;
2125 path.len = ptr - path.ptr;
Willy Tarreau79e57332018-10-02 16:01:16 +02002126
Christopher Faulet78337bb2018-11-15 14:35:18 +01002127 path = http_get_path(path);
Willy Tarreau79e57332018-10-02 16:01:16 +02002128 if (!path.ptr)
2129 return 0;
2130
2131 smp->data.u.str.area = path.ptr;
2132 smp->data.u.str.data = path.len;
2133 smp->data.type = SMP_T_STR;
2134 smp->flags = SMP_F_CONST;
2135
2136 return 1;
2137}
2138
2139/* Retrieves the HTTP version from the request (either 1.0 or 1.1) and emits it
2140 * as a string (either "HTTP/1.0" or "HTTP/1.1").
2141 */
2142static int smp_fetch_capture_req_ver(const struct arg *args, struct sample *smp, const char *kw, void *private)
2143{
Willy Tarreau79ed4672020-04-29 11:44:54 +02002144 struct http_txn *txn;
2145
2146 if (!smp->strm)
2147 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02002148
Willy Tarreau79ed4672020-04-29 11:44:54 +02002149 txn = smp->strm->txn;
Willy Tarreau79e57332018-10-02 16:01:16 +02002150 if (!txn || txn->req.msg_state < HTTP_MSG_HDR_FIRST)
2151 return 0;
2152
2153 if (txn->req.flags & HTTP_MSGF_VER_11)
2154 smp->data.u.str.area = "HTTP/1.1";
2155 else
2156 smp->data.u.str.area = "HTTP/1.0";
2157
2158 smp->data.u.str.data = 8;
2159 smp->data.type = SMP_T_STR;
2160 smp->flags = SMP_F_CONST;
2161 return 1;
2162
2163}
2164
2165/* Retrieves the HTTP version from the response (either 1.0 or 1.1) and emits it
2166 * as a string (either "HTTP/1.0" or "HTTP/1.1").
2167 */
2168static int smp_fetch_capture_res_ver(const struct arg *args, struct sample *smp, const char *kw, void *private)
2169{
Willy Tarreau79ed4672020-04-29 11:44:54 +02002170 struct http_txn *txn;
2171
2172 if (!smp->strm)
2173 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02002174
Willy Tarreau79ed4672020-04-29 11:44:54 +02002175 txn = smp->strm->txn;
Willy Tarreau79e57332018-10-02 16:01:16 +02002176 if (!txn || txn->rsp.msg_state < HTTP_MSG_HDR_FIRST)
2177 return 0;
2178
2179 if (txn->rsp.flags & HTTP_MSGF_VER_11)
2180 smp->data.u.str.area = "HTTP/1.1";
2181 else
2182 smp->data.u.str.area = "HTTP/1.0";
2183
2184 smp->data.u.str.data = 8;
2185 smp->data.type = SMP_T_STR;
2186 smp->flags = SMP_F_CONST;
2187 return 1;
2188
2189}
2190
2191/* Iterate over all cookies present in a message. The context is stored in
2192 * smp->ctx.a[0] for the in-header position, smp->ctx.a[1] for the
2193 * end-of-header-value, and smp->ctx.a[2] for the hdr_ctx. Depending on
2194 * the direction, multiple cookies may be parsed on the same line or not.
2195 * The cookie name is in args and the name length in args->data.str.len.
2196 * Accepts exactly 1 argument of type string. If the input options indicate
2197 * that no iterating is desired, then only last value is fetched if any.
2198 * The returned sample is of type CSTR. Can be used to parse cookies in other
2199 * files.
2200 */
2201static int smp_fetch_cookie(const struct arg *args, struct sample *smp, const char *kw, void *private)
2202{
Christopher Faulet89dc4992019-04-17 12:02:59 +02002203 /* possible keywords: req.cookie / cookie / cook, res.cookie / scook / set-cookie */
2204 struct channel *chn = ((kw[0] == 'c' || kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Willy Tarreau79e57332018-10-02 16:01:16 +02002205 int occ = 0;
2206 int found = 0;
2207
2208 if (!args || args->type != ARGT_STR)
2209 return 0;
2210
Christopher Faulet46575cd2019-04-17 11:40:30 +02002211 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002212 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +02002213 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002214 struct http_hdr_ctx *ctx = smp->ctx.a[2];
2215 struct ist hdr;
Willy Tarreau79e57332018-10-02 16:01:16 +02002216
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002217 if (!ctx) {
2218 /* first call */
2219 ctx = &static_http_hdr_ctx;
2220 ctx->blk = NULL;
2221 smp->ctx.a[2] = ctx;
2222 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002223
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002224 if (!htx)
2225 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02002226
Christopher Faulet89dc4992019-04-17 12:02:59 +02002227 hdr = (!(chn->flags & CF_ISRESP) ? ist("Cookie") : ist("Set-Cookie"));
Willy Tarreau79e57332018-10-02 16:01:16 +02002228
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002229 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
2230 /* no explicit occurrence and single fetch => last cookie by default */
2231 occ = -1;
Willy Tarreau79e57332018-10-02 16:01:16 +02002232
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002233 /* OK so basically here, either we want only one value and it's the
2234 * last one, or we want to iterate over all of them and we fetch the
2235 * next one.
Willy Tarreau79e57332018-10-02 16:01:16 +02002236 */
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002237
2238 if (!(smp->flags & SMP_F_NOT_LAST)) {
2239 /* search for the header from the beginning, we must first initialize
2240 * the search parameters.
2241 */
2242 smp->ctx.a[0] = NULL;
2243 ctx->blk = NULL;
2244 }
2245
2246 smp->flags |= SMP_F_VOL_HDR;
2247 while (1) {
2248 /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */
2249 if (!smp->ctx.a[0]) {
2250 if (!http_find_header(htx, hdr, ctx, 0))
2251 goto out;
2252
2253 if (ctx->value.len < args->data.str.data + 1)
2254 continue;
2255
2256 smp->ctx.a[0] = ctx->value.ptr;
2257 smp->ctx.a[1] = smp->ctx.a[0] + ctx->value.len;
2258 }
2259
2260 smp->data.type = SMP_T_STR;
2261 smp->flags |= SMP_F_CONST;
2262 smp->ctx.a[0] = http_extract_cookie_value(smp->ctx.a[0], smp->ctx.a[1],
2263 args->data.str.area, args->data.str.data,
2264 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
2265 &smp->data.u.str.area,
2266 &smp->data.u.str.data);
2267 if (smp->ctx.a[0]) {
2268 found = 1;
2269 if (occ >= 0) {
2270 /* one value was returned into smp->data.u.str.{str,len} */
2271 smp->flags |= SMP_F_NOT_LAST;
2272 return 1;
2273 }
2274 }
2275 /* if we're looking for last occurrence, let's loop */
2276 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002277 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002278 else {
2279 /* LEGACY version */
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002280 struct hdr_idx *idx;
2281 struct hdr_ctx *ctx = smp->ctx.a[2];
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002282 const char *hdr_name;
2283 int hdr_name_len;
2284 char *sol;
Willy Tarreau79e57332018-10-02 16:01:16 +02002285
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002286 if (!ctx) {
2287 /* first call */
2288 ctx = &static_hdr_ctx;
2289 ctx->idx = 0;
2290 smp->ctx.a[2] = ctx;
2291 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002292
Christopher Faulet89dc4992019-04-17 12:02:59 +02002293 CHECK_HTTP_MESSAGE_FIRST(chn);
Willy Tarreau79e57332018-10-02 16:01:16 +02002294
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002295 idx = &smp->strm->txn->hdr_idx;
Christopher Faulet89dc4992019-04-17 12:02:59 +02002296 if (!(chn->flags & CF_ISRESP)) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002297 hdr_name = "Cookie";
2298 hdr_name_len = 6;
2299 } else {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002300 hdr_name = "Set-Cookie";
2301 hdr_name_len = 10;
Willy Tarreau79e57332018-10-02 16:01:16 +02002302 }
2303
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002304 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
2305 /* no explicit occurrence and single fetch => last cookie by default */
2306 occ = -1;
2307
2308 /* OK so basically here, either we want only one value and it's the
2309 * last one, or we want to iterate over all of them and we fetch the
2310 * next one.
2311 */
2312
Christopher Faulet89dc4992019-04-17 12:02:59 +02002313 sol = ci_head(chn);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002314 if (!(smp->flags & SMP_F_NOT_LAST)) {
2315 /* search for the header from the beginning, we must first initialize
2316 * the search parameters.
2317 */
2318 smp->ctx.a[0] = NULL;
2319 ctx->idx = 0;
2320 }
2321
2322 smp->flags |= SMP_F_VOL_HDR;
2323
2324 while (1) {
2325 /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */
2326 if (!smp->ctx.a[0]) {
2327 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, ctx))
2328 goto out;
2329
2330 if (ctx->vlen < args->data.str.data + 1)
2331 continue;
2332
2333 smp->ctx.a[0] = ctx->line + ctx->val;
2334 smp->ctx.a[1] = smp->ctx.a[0] + ctx->vlen;
2335 }
2336
2337 smp->data.type = SMP_T_STR;
2338 smp->flags |= SMP_F_CONST;
2339 smp->ctx.a[0] = http_extract_cookie_value(smp->ctx.a[0], smp->ctx.a[1],
2340 args->data.str.area, args->data.str.data,
2341 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
2342 &smp->data.u.str.area, &smp->data.u.str.data);
2343 if (smp->ctx.a[0]) {
2344 found = 1;
2345 if (occ >= 0) {
2346 /* one value was returned into smp->data.u.str.{str,len} */
2347 smp->flags |= SMP_F_NOT_LAST;
2348 return 1;
2349 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002350 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002351 /* if we're looking for last occurrence, let's loop */
Willy Tarreau79e57332018-10-02 16:01:16 +02002352 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002353 }
2354 /* all cookie headers and values were scanned. If we're looking for the
2355 * last occurrence, we may return it now.
2356 */
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002357 out:
Willy Tarreau79e57332018-10-02 16:01:16 +02002358 smp->flags &= ~SMP_F_NOT_LAST;
2359 return found;
2360}
2361
Christopher Fauletc1f40dd2019-05-16 10:07:30 +02002362/* Same than smp_fetch_cookie() but only relies on the sample direction to
2363 * choose the right channel. So instead of duplicating the code, we just change
2364 * the keyword and then fallback on smp_fetch_cookie().
2365 */
2366static int smp_fetch_chn_cookie(const struct arg *args, struct sample *smp, const char *kw, void *private)
2367{
2368 kw = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ ? "req.cook" : "res.cook");
2369 return smp_fetch_cookie(args, smp, kw, private);
2370}
2371
Willy Tarreau79e57332018-10-02 16:01:16 +02002372/* Iterate over all cookies present in a request to count how many occurrences
2373 * match the name in args and args->data.str.len. If <multi> is non-null, then
2374 * multiple cookies may be parsed on the same line. The returned sample is of
2375 * type UINT. Accepts exactly 1 argument of type string.
2376 */
2377static int smp_fetch_cookie_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
2378{
Christopher Faulet89dc4992019-04-17 12:02:59 +02002379 /* possible keywords: req.cook_cnt / cook_cnt, res.cook_cnt / scook_cnt */
2380 struct channel *chn = ((kw[0] == 'c' || kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Willy Tarreau79e57332018-10-02 16:01:16 +02002381 char *val_beg, *val_end;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002382 int cnt;
Willy Tarreau79e57332018-10-02 16:01:16 +02002383
2384 if (!args || args->type != ARGT_STR)
2385 return 0;
2386
Christopher Faulet46575cd2019-04-17 11:40:30 +02002387 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002388 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +02002389 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002390 struct http_hdr_ctx ctx;
2391 struct ist hdr;
2392
2393 if (!htx)
2394 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02002395
Christopher Faulet89dc4992019-04-17 12:02:59 +02002396 hdr = (!(chn->flags & CF_ISRESP) ? ist("Cookie") : ist("Set-Cookie"));
Willy Tarreau79e57332018-10-02 16:01:16 +02002397
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002398 val_end = val_beg = NULL;
2399 ctx.blk = NULL;
2400 cnt = 0;
2401 while (1) {
2402 /* Note: val_beg == NULL every time we need to fetch a new header */
2403 if (!val_beg) {
2404 if (!http_find_header(htx, hdr, &ctx, 0))
2405 break;
Willy Tarreau79e57332018-10-02 16:01:16 +02002406
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002407 if (ctx.value.len < args->data.str.data + 1)
2408 continue;
Willy Tarreau79e57332018-10-02 16:01:16 +02002409
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002410 val_beg = ctx.value.ptr;
2411 val_end = val_beg + ctx.value.len;
2412 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002413
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002414 smp->data.type = SMP_T_STR;
2415 smp->flags |= SMP_F_CONST;
2416 while ((val_beg = http_extract_cookie_value(val_beg, val_end,
2417 args->data.str.area, args->data.str.data,
2418 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
2419 &smp->data.u.str.area,
2420 &smp->data.u.str.data))) {
2421 cnt++;
2422 }
2423 }
2424 }
2425 else {
2426 /* LEGACY version */
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002427 struct hdr_idx *idx;
2428 struct hdr_ctx ctx;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002429 const char *hdr_name;
2430 int hdr_name_len;
2431 char *sol;
Willy Tarreau79e57332018-10-02 16:01:16 +02002432
Christopher Faulet89dc4992019-04-17 12:02:59 +02002433 CHECK_HTTP_MESSAGE_FIRST(chn);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002434
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002435 idx = &smp->strm->txn->hdr_idx;
Christopher Faulet89dc4992019-04-17 12:02:59 +02002436 if (!(chn->flags & CF_ISRESP)) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002437 hdr_name = "Cookie";
2438 hdr_name_len = 6;
2439 } else {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002440 hdr_name = "Set-Cookie";
2441 hdr_name_len = 10;
Willy Tarreau79e57332018-10-02 16:01:16 +02002442 }
2443
Christopher Faulet89dc4992019-04-17 12:02:59 +02002444 sol = ci_head(chn);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002445 val_end = val_beg = NULL;
2446 ctx.idx = 0;
2447 cnt = 0;
2448
2449 while (1) {
2450 /* Note: val_beg == NULL every time we need to fetch a new header */
2451 if (!val_beg) {
2452 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, &ctx))
2453 break;
2454
2455 if (ctx.vlen < args->data.str.data + 1)
2456 continue;
2457
2458 val_beg = ctx.line + ctx.val;
2459 val_end = val_beg + ctx.vlen;
2460 }
2461
2462 smp->data.type = SMP_T_STR;
2463 smp->flags |= SMP_F_CONST;
2464 while ((val_beg = http_extract_cookie_value(val_beg, val_end,
2465 args->data.str.area, args->data.str.data,
2466 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
2467 &smp->data.u.str.area, &smp->data.u.str.data))) {
2468 cnt++;
2469 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002470 }
2471 }
2472
2473 smp->data.type = SMP_T_SINT;
2474 smp->data.u.sint = cnt;
2475 smp->flags |= SMP_F_VOL_HDR;
2476 return 1;
2477}
2478
2479/* Fetch an cookie's integer value. The integer value is returned. It
2480 * takes a mandatory argument of type string. It relies on smp_fetch_cookie().
2481 */
2482static int smp_fetch_cookie_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
2483{
2484 int ret = smp_fetch_cookie(args, smp, kw, private);
2485
2486 if (ret > 0) {
2487 smp->data.type = SMP_T_SINT;
2488 smp->data.u.sint = strl2ic(smp->data.u.str.area,
2489 smp->data.u.str.data);
2490 }
2491
2492 return ret;
2493}
2494
2495/************************************************************************/
2496/* The code below is dedicated to sample fetches */
2497/************************************************************************/
2498
2499/* This scans a URL-encoded query string. It takes an optionally wrapping
2500 * string whose first contigous chunk has its beginning in ctx->a[0] and end
2501 * in ctx->a[1], and the optional second part in (ctx->a[2]..ctx->a[3]). The
2502 * pointers are updated for next iteration before leaving.
2503 */
2504static int smp_fetch_param(char delim, const char *name, int name_len, const struct arg *args, struct sample *smp, const char *kw, void *private)
2505{
2506 const char *vstart, *vend;
2507 struct buffer *temp;
2508 const char **chunks = (const char **)smp->ctx.a;
2509
2510 if (!http_find_next_url_param(chunks, name, name_len,
2511 &vstart, &vend, delim))
2512 return 0;
2513
2514 /* Create sample. If the value is contiguous, return the pointer as CONST,
2515 * if the value is wrapped, copy-it in a buffer.
2516 */
2517 smp->data.type = SMP_T_STR;
2518 if (chunks[2] &&
2519 vstart >= chunks[0] && vstart <= chunks[1] &&
2520 vend >= chunks[2] && vend <= chunks[3]) {
2521 /* Wrapped case. */
2522 temp = get_trash_chunk();
2523 memcpy(temp->area, vstart, chunks[1] - vstart);
2524 memcpy(temp->area + ( chunks[1] - vstart ), chunks[2],
2525 vend - chunks[2]);
2526 smp->data.u.str.area = temp->area;
2527 smp->data.u.str.data = ( chunks[1] - vstart ) + ( vend - chunks[2] );
2528 } else {
2529 /* Contiguous case. */
2530 smp->data.u.str.area = (char *)vstart;
2531 smp->data.u.str.data = vend - vstart;
2532 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
2533 }
2534
2535 /* Update context, check wrapping. */
2536 chunks[0] = vend;
2537 if (chunks[2] && vend >= chunks[2] && vend <= chunks[3]) {
2538 chunks[1] = chunks[3];
2539 chunks[2] = NULL;
2540 }
2541
2542 if (chunks[0] < chunks[1])
2543 smp->flags |= SMP_F_NOT_LAST;
2544
2545 return 1;
2546}
2547
2548/* This function iterates over each parameter of the query string. It uses
2549 * ctx->a[0] and ctx->a[1] to store the beginning and end of the current
2550 * parameter. Since it uses smp_fetch_param(), ctx->a[2..3] are both NULL.
2551 * An optional parameter name is passed in args[0], otherwise any parameter is
2552 * considered. It supports an optional delimiter argument for the beginning of
2553 * the string in args[1], which defaults to "?".
2554 */
2555static int smp_fetch_url_param(const struct arg *args, struct sample *smp, const char *kw, void *private)
2556{
Christopher Faulet89dc4992019-04-17 12:02:59 +02002557 struct channel *chn = SMP_REQ_CHN(smp);
Willy Tarreau79e57332018-10-02 16:01:16 +02002558 char delim = '?';
2559 const char *name;
2560 int name_len;
2561
2562 if (!args ||
2563 (args[0].type && args[0].type != ARGT_STR) ||
2564 (args[1].type && args[1].type != ARGT_STR))
2565 return 0;
2566
2567 name = "";
2568 name_len = 0;
2569 if (args->type == ARGT_STR) {
2570 name = args->data.str.area;
2571 name_len = args->data.str.data;
2572 }
2573
2574 if (args[1].type)
2575 delim = *args[1].data.str.area;
2576
2577 if (!smp->ctx.a[0]) { // first call, find the query string
Christopher Faulet46575cd2019-04-17 11:40:30 +02002578 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002579 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +02002580 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01002581 struct htx_sl *sl;
Willy Tarreau79e57332018-10-02 16:01:16 +02002582
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002583 if (!htx)
2584 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02002585
Christopher Faulet297fbb42019-05-13 14:41:27 +02002586 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01002587 smp->ctx.a[0] = http_find_param_list(HTX_SL_REQ_UPTR(sl), HTX_SL_REQ_ULEN(sl), delim);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002588 if (!smp->ctx.a[0])
2589 return 0;
2590
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01002591 smp->ctx.a[1] = HTX_SL_REQ_UPTR(sl) + HTX_SL_REQ_ULEN(sl);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002592 }
2593 else {
2594 /* LEGACY version */
2595 struct http_msg *msg;
2596
Christopher Faulet89dc4992019-04-17 12:02:59 +02002597 CHECK_HTTP_MESSAGE_FIRST(chn);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002598
2599 msg = &smp->strm->txn->req;
2600
Christopher Faulet89dc4992019-04-17 12:02:59 +02002601 smp->ctx.a[0] = http_find_param_list(ci_head(chn) + msg->sl.rq.u,
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002602 msg->sl.rq.u_l, delim);
2603 if (!smp->ctx.a[0])
2604 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02002605
Christopher Faulet89dc4992019-04-17 12:02:59 +02002606 smp->ctx.a[1] = ci_head(chn) + msg->sl.rq.u + msg->sl.rq.u_l;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002607 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002608
2609 /* Assume that the context is filled with NULL pointer
2610 * before the first call.
2611 * smp->ctx.a[2] = NULL;
2612 * smp->ctx.a[3] = NULL;
2613 */
2614 }
2615
2616 return smp_fetch_param(delim, name, name_len, args, smp, kw, private);
2617}
2618
2619/* This function iterates over each parameter of the body. This requires
2620 * that the body has been waited for using http-buffer-request. It uses
2621 * ctx->a[0] and ctx->a[1] to store the beginning and end of the first
2622 * contigous part of the body, and optionally ctx->a[2..3] to reference the
2623 * optional second part if the body wraps at the end of the buffer. An optional
2624 * parameter name is passed in args[0], otherwise any parameter is considered.
2625 */
2626static int smp_fetch_body_param(const struct arg *args, struct sample *smp, const char *kw, void *private)
2627{
Christopher Faulet89dc4992019-04-17 12:02:59 +02002628 struct channel *chn = SMP_REQ_CHN(smp);
Willy Tarreau79e57332018-10-02 16:01:16 +02002629 const char *name;
2630 int name_len;
2631
2632 if (!args || (args[0].type && args[0].type != ARGT_STR))
2633 return 0;
2634
2635 name = "";
2636 name_len = 0;
2637 if (args[0].type == ARGT_STR) {
2638 name = args[0].data.str.area;
2639 name_len = args[0].data.str.data;
2640 }
2641
2642 if (!smp->ctx.a[0]) { // first call, find the query string
Christopher Faulet46575cd2019-04-17 11:40:30 +02002643 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002644 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +02002645 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002646 struct buffer *temp;
2647 int32_t pos;
Willy Tarreau79e57332018-10-02 16:01:16 +02002648
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002649 if (!htx)
2650 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02002651
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002652 temp = get_trash_chunk();
Christopher Fauleta3f15502019-05-13 15:27:23 +02002653 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002654 struct htx_blk *blk = htx_get_blk(htx, pos);
2655 enum htx_blk_type type = htx_get_blk_type(blk);
Willy Tarreau79e57332018-10-02 16:01:16 +02002656
Christopher Faulet54b5e212019-06-04 10:08:28 +02002657 if (type == HTX_BLK_EOM || type == HTX_BLK_TLR || type == HTX_BLK_EOT)
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002658 break;
2659 if (type == HTX_BLK_DATA) {
Christopher Fauletc59ff232018-12-03 13:58:44 +01002660 if (!htx_data_to_h1(htx_get_blk_value(htx, blk), temp, 0))
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002661 return 0;
2662 }
2663 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002664
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002665 smp->ctx.a[0] = temp->area;
2666 smp->ctx.a[1] = temp->area + temp->data;
Willy Tarreau79e57332018-10-02 16:01:16 +02002667
2668 /* Assume that the context is filled with NULL pointer
2669 * before the first call.
2670 * smp->ctx.a[2] = NULL;
2671 * smp->ctx.a[3] = NULL;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002672 */
Willy Tarreau79e57332018-10-02 16:01:16 +02002673 }
2674 else {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002675 /* LEGACY version */
2676 struct http_msg *msg;
2677 unsigned long len;
2678 unsigned long block1;
2679 char *body;
2680
Christopher Faulet89dc4992019-04-17 12:02:59 +02002681 CHECK_HTTP_MESSAGE_FIRST(chn);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002682
Christopher Faulet89dc4992019-04-17 12:02:59 +02002683 msg = &smp->strm->txn->req;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002684 len = http_body_bytes(msg);
Christopher Faulet89dc4992019-04-17 12:02:59 +02002685 body = c_ptr(chn, -http_data_rewind(msg));
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002686
2687 block1 = len;
Christopher Faulet89dc4992019-04-17 12:02:59 +02002688 if (block1 > b_wrap(&chn->buf) - body)
2689 block1 = b_wrap(&chn->buf) - body;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002690
2691 if (block1 == len) {
2692 /* buffer is not wrapped (or empty) */
2693 smp->ctx.a[0] = body;
2694 smp->ctx.a[1] = body + len;
2695
2696 /* Assume that the context is filled with NULL pointer
2697 * before the first call.
2698 * smp->ctx.a[2] = NULL;
2699 * smp->ctx.a[3] = NULL;
2700 */
2701 }
2702 else {
2703 /* buffer is wrapped, we need to defragment it */
2704 smp->ctx.a[0] = body;
2705 smp->ctx.a[1] = body + block1;
Christopher Faulet89dc4992019-04-17 12:02:59 +02002706 smp->ctx.a[2] = b_orig(&chn->buf);
2707 smp->ctx.a[3] = b_orig(&chn->buf) + ( len - block1 );
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002708 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002709 }
2710 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002711
Willy Tarreau79e57332018-10-02 16:01:16 +02002712 return smp_fetch_param('&', name, name_len, args, smp, kw, private);
2713}
2714
2715/* Return the signed integer value for the specified url parameter (see url_param
2716 * above).
2717 */
2718static int smp_fetch_url_param_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
2719{
2720 int ret = smp_fetch_url_param(args, smp, kw, private);
2721
2722 if (ret > 0) {
2723 smp->data.type = SMP_T_SINT;
2724 smp->data.u.sint = strl2ic(smp->data.u.str.area,
2725 smp->data.u.str.data);
2726 }
2727
2728 return ret;
2729}
2730
2731/* This produces a 32-bit hash of the concatenation of the first occurrence of
2732 * the Host header followed by the path component if it begins with a slash ('/').
2733 * This means that '*' will not be added, resulting in exactly the first Host
2734 * entry. If no Host header is found, then the path is used. The resulting value
2735 * is hashed using the url hash followed by a full avalanche hash and provides a
2736 * 32-bit integer value. This fetch is useful for tracking per-URL activity on
2737 * high-traffic sites without having to store whole paths.
2738 * this differs from the base32 functions in that it includes the url parameters
2739 * as well as the path
2740 */
2741static int smp_fetch_url32(const struct arg *args, struct sample *smp, const char *kw, void *private)
2742{
Christopher Faulet89dc4992019-04-17 12:02:59 +02002743 struct channel *chn = SMP_REQ_CHN(smp);
Willy Tarreau79e57332018-10-02 16:01:16 +02002744 unsigned int hash = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02002745
Christopher Faulet46575cd2019-04-17 11:40:30 +02002746 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002747 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +02002748 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002749 struct http_hdr_ctx ctx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01002750 struct htx_sl *sl;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002751 struct ist path;
Willy Tarreau79e57332018-10-02 16:01:16 +02002752
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002753 if (!htx)
2754 return 0;
2755
2756 ctx.blk = NULL;
2757 if (http_find_header(htx, ist("Host"), &ctx, 1)) {
2758 /* OK we have the header value in ctx.value */
2759 while (ctx.value.len--)
2760 hash = *(ctx.value.ptr++) + (hash << 6) + (hash << 16) - hash;
2761 }
2762
2763 /* now retrieve the path */
Christopher Faulet297fbb42019-05-13 14:41:27 +02002764 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01002765 path = http_get_path(htx_sl_req_uri(sl));
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002766 if (path.len && *(path.ptr) == '/') {
2767 while (path.len--)
2768 hash = *(path.ptr++) + (hash << 6) + (hash << 16) - hash;
2769 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002770 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002771 else {
2772 /* LEGACY version */
2773 struct http_txn *txn;
2774 struct hdr_ctx ctx;
2775 char *ptr, *beg, *end;
2776 int len;
2777
Christopher Faulet89dc4992019-04-17 12:02:59 +02002778 CHECK_HTTP_MESSAGE_FIRST(chn);
Willy Tarreau79e57332018-10-02 16:01:16 +02002779
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002780 txn = smp->strm->txn;
2781 ctx.idx = 0;
Christopher Faulet89dc4992019-04-17 12:02:59 +02002782 if (http_find_header2("Host", 4, ci_head(chn), &txn->hdr_idx, &ctx)) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002783 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
2784 ptr = ctx.line + ctx.val;
2785 len = ctx.vlen;
2786 while (len--)
2787 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
2788 }
2789
2790 /* now retrieve the path */
Christopher Faulet89dc4992019-04-17 12:02:59 +02002791 end = ci_head(chn) + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002792 beg = http_txn_get_path(txn);
2793 if (!beg)
2794 beg = end;
Willy Tarreau79e57332018-10-02 16:01:16 +02002795
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002796 for (ptr = beg; ptr < end ; ptr++);
Willy Tarreau79e57332018-10-02 16:01:16 +02002797
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002798 if (beg < ptr && *beg == '/') {
2799 while (beg < ptr)
2800 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
2801 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002802 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002803
Willy Tarreau79e57332018-10-02 16:01:16 +02002804 hash = full_hash(hash);
2805
2806 smp->data.type = SMP_T_SINT;
2807 smp->data.u.sint = hash;
2808 smp->flags = SMP_F_VOL_1ST;
2809 return 1;
2810}
2811
2812/* This concatenates the source address with the 32-bit hash of the Host and
2813 * URL as returned by smp_fetch_base32(). The idea is to have per-source and
2814 * per-url counters. The result is a binary block from 8 to 20 bytes depending
2815 * on the source address length. The URL hash is stored before the address so
2816 * that in environments where IPv6 is insignificant, truncating the output to
2817 * 8 bytes would still work.
2818 */
2819static int smp_fetch_url32_src(const struct arg *args, struct sample *smp, const char *kw, void *private)
2820{
2821 struct buffer *temp;
2822 struct connection *cli_conn = objt_conn(smp->sess->origin);
2823
2824 if (!cli_conn)
2825 return 0;
2826
2827 if (!smp_fetch_url32(args, smp, kw, private))
2828 return 0;
2829
2830 temp = get_trash_chunk();
2831 *(unsigned int *) temp->area = htonl(smp->data.u.sint);
2832 temp->data += sizeof(unsigned int);
2833
2834 switch (cli_conn->addr.from.ss_family) {
2835 case AF_INET:
2836 memcpy(temp->area + temp->data,
2837 &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr,
2838 4);
2839 temp->data += 4;
2840 break;
2841 case AF_INET6:
2842 memcpy(temp->area + temp->data,
2843 &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr,
2844 16);
2845 temp->data += 16;
2846 break;
2847 default:
2848 return 0;
2849 }
2850
2851 smp->data.u.str = *temp;
2852 smp->data.type = SMP_T_BIN;
2853 return 1;
2854}
2855
2856/************************************************************************/
2857/* Other utility functions */
2858/************************************************************************/
2859
2860/* This function is used to validate the arguments passed to any "hdr" fetch
2861 * keyword. These keywords support an optional positive or negative occurrence
2862 * number. We must ensure that the number is greater than -MAX_HDR_HISTORY. It
2863 * is assumed that the types are already the correct ones. Returns 0 on error,
2864 * non-zero if OK. If <err> is not NULL, it will be filled with a pointer to an
2865 * error message in case of error, that the caller is responsible for freeing.
2866 * The initial location must either be freeable or NULL.
2867 * Note: this function's pointer is checked from Lua.
2868 */
2869int val_hdr(struct arg *arg, char **err_msg)
2870{
2871 if (arg && arg[1].type == ARGT_SINT && arg[1].data.sint < -MAX_HDR_HISTORY) {
2872 memprintf(err_msg, "header occurrence must be >= %d", -MAX_HDR_HISTORY);
2873 return 0;
2874 }
2875 return 1;
2876}
2877
2878/************************************************************************/
2879/* All supported sample fetch keywords must be declared here. */
2880/************************************************************************/
2881
2882/* Note: must not be declared <const> as its list will be overwritten */
2883static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
2884 { "base", smp_fetch_base, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2885 { "base32", smp_fetch_base32, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
2886 { "base32+src", smp_fetch_base32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
2887
2888 /* capture are allocated and are permanent in the stream */
2889 { "capture.req.hdr", smp_fetch_capture_req_hdr, ARG1(1,SINT), NULL, SMP_T_STR, SMP_USE_HRQHP },
2890
2891 /* retrieve these captures from the HTTP logs */
2892 { "capture.req.method", smp_fetch_capture_req_method, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
2893 { "capture.req.uri", smp_fetch_capture_req_uri, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
2894 { "capture.req.ver", smp_fetch_capture_req_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
2895
2896 { "capture.res.hdr", smp_fetch_capture_res_hdr, ARG1(1,SINT), NULL, SMP_T_STR, SMP_USE_HRSHP },
2897 { "capture.res.ver", smp_fetch_capture_res_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
2898
2899 /* cookie is valid in both directions (eg: for "stick ...") but cook*
2900 * are only here to match the ACL's name, are request-only and are used
2901 * for ACL compatibility only.
2902 */
2903 { "cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Christopher Fauletc1f40dd2019-05-16 10:07:30 +02002904 { "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 +02002905 { "cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2906 { "cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2907
2908 /* hdr is valid in both directions (eg: for "stick ...") but hdr_* are
2909 * only here to match the ACL's name, are request-only and are used for
2910 * ACL compatibility only.
2911 */
Christopher Fauletc1f40dd2019-05-16 10:07:30 +02002912 { "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 +02002913 { "hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2914 { "hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV },
2915 { "hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_SINT, SMP_USE_HRQHV },
2916
2917 { "http_auth", smp_fetch_http_auth, ARG1(1,USR), NULL, SMP_T_BOOL, SMP_USE_HRQHV },
2918 { "http_auth_group", smp_fetch_http_auth_grp, ARG1(1,USR), NULL, SMP_T_STR, SMP_USE_HRQHV },
2919 { "http_first_req", smp_fetch_http_first_req, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
2920 { "method", smp_fetch_meth, 0, NULL, SMP_T_METH, SMP_USE_HRQHP },
2921 { "path", smp_fetch_path, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2922 { "query", smp_fetch_query, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2923
2924 /* HTTP protocol on the request path */
2925 { "req.proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
2926 { "req_proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
2927
2928 /* HTTP version on the request path */
2929 { "req.ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2930 { "req_ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2931
2932 { "req.body", smp_fetch_body, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
2933 { "req.body_len", smp_fetch_body_len, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
2934 { "req.body_size", smp_fetch_body_size, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
2935 { "req.body_param", smp_fetch_body_param, ARG1(0,STR), NULL, SMP_T_BIN, SMP_USE_HRQHV },
2936
2937 { "req.hdrs", smp_fetch_hdrs, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
2938 { "req.hdrs_bin", smp_fetch_hdrs_bin, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
2939
2940 /* HTTP version on the response path */
2941 { "res.ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
2942 { "resp_ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
2943
2944 /* explicit req.{cook,hdr} are used to force the fetch direction to be request-only */
2945 { "req.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
2946 { "req.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2947 { "req.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2948
2949 { "req.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
2950 { "req.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2951 { "req.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
2952 { "req.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2953 { "req.hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV },
2954 { "req.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
2955 { "req.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_SINT, SMP_USE_HRQHV },
2956
2957 /* explicit req.{cook,hdr} are used to force the fetch direction to be response-only */
2958 { "res.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
2959 { "res.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2960 { "res.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2961
2962 { "res.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
2963 { "res.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2964 { "res.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
2965 { "res.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2966 { "res.hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV },
2967 { "res.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
2968 { "res.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_SINT, SMP_USE_HRSHV },
2969
2970 /* scook is valid only on the response and is used for ACL compatibility */
2971 { "scook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
2972 { "scook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2973 { "scook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2974 { "set-cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV }, /* deprecated */
2975
2976 /* shdr is valid only on the response and is used for ACL compatibility */
2977 { "shdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
2978 { "shdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2979 { "shdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV },
2980 { "shdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_SINT, SMP_USE_HRSHV },
2981
2982 { "status", smp_fetch_stcode, 0, NULL, SMP_T_SINT, SMP_USE_HRSHP },
2983 { "unique-id", smp_fetch_uniqueid, 0, NULL, SMP_T_STR, SMP_SRC_L4SRV },
2984 { "url", smp_fetch_url, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2985 { "url32", smp_fetch_url32, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
2986 { "url32+src", smp_fetch_url32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
2987 { "url_ip", smp_fetch_url_ip, 0, NULL, SMP_T_IPV4, SMP_USE_HRQHV },
2988 { "url_port", smp_fetch_url_port, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
2989 { "url_param", smp_fetch_url_param, ARG2(0,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
2990 { "urlp" , smp_fetch_url_param, ARG2(0,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
2991 { "urlp_val", smp_fetch_url_param_val, ARG2(0,STR,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2992 { /* END */ },
2993}};
2994
Willy Tarreau0108d902018-11-25 19:14:37 +01002995INITCALL1(STG_REGISTER, sample_register_fetches, &sample_fetch_keywords);
Willy Tarreau79e57332018-10-02 16:01:16 +02002996
2997/*
2998 * Local variables:
2999 * c-indent-level: 8
3000 * c-basic-offset: 8
3001 * End:
3002 */