blob: 487333d2c4b9dcebb543324a2ddfa24178d065fd [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;
1566
1567 while ((ret = smp_fetch_hdr(args, smp, kw, private)) > 0) {
1568 if (url2ipv4((char *) smp->data.u.str.area, &smp->data.u.ipv4)) {
1569 smp->data.type = SMP_T_IPV4;
1570 break;
1571 } else {
1572 struct buffer *temp = get_trash_chunk();
1573 if (smp->data.u.str.data < temp->size - 1) {
1574 memcpy(temp->area, smp->data.u.str.area,
1575 smp->data.u.str.data);
1576 temp->area[smp->data.u.str.data] = '\0';
1577 if (inet_pton(AF_INET6, temp->area, &smp->data.u.ipv6)) {
1578 smp->data.type = SMP_T_IPV6;
1579 break;
1580 }
1581 }
1582 }
1583
1584 /* if the header doesn't match an IP address, fetch next one */
1585 if (!(smp->flags & SMP_F_NOT_LAST))
1586 return 0;
1587 }
1588 return ret;
1589}
1590
1591/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
1592 * the first '/' after the possible hostname, and ends before the possible '?'.
1593 */
1594static int smp_fetch_path(const struct arg *args, struct sample *smp, const char *kw, void *private)
1595{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001596 struct channel *chn = SMP_REQ_CHN(smp);
1597
Christopher Faulet46575cd2019-04-17 11:40:30 +02001598 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001599 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +02001600 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001601 struct htx_sl *sl;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001602 struct ist path;
1603 size_t len;
Willy Tarreau79e57332018-10-02 16:01:16 +02001604
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001605 if (!htx)
1606 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001607
Christopher Faulet297fbb42019-05-13 14:41:27 +02001608 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001609 path = http_get_path(htx_sl_req_uri(sl));
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001610 if (!path.ptr)
1611 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001612
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001613 for (len = 0; len < path.len && *(path.ptr + len) != '?'; len++)
Dragan Dosen8861e1c2019-02-12 19:50:31 +01001614 ;
Willy Tarreau79e57332018-10-02 16:01:16 +02001615
Dragan Dosen8861e1c2019-02-12 19:50:31 +01001616 /* OK, we got the '/' ! */
1617 smp->data.type = SMP_T_STR;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001618 smp->data.u.str.area = path.ptr;
1619 smp->data.u.str.data = len;
1620 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
1621 }
1622 else {
1623 struct http_txn *txn;
1624 char *ptr, *end;
Willy Tarreau79e57332018-10-02 16:01:16 +02001625
Christopher Faulet89dc4992019-04-17 12:02:59 +02001626 CHECK_HTTP_MESSAGE_FIRST(chn);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001627
1628 txn = smp->strm->txn;
Christopher Faulet89dc4992019-04-17 12:02:59 +02001629 end = ci_head(chn) + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001630 ptr = http_txn_get_path(txn);
1631 if (!ptr)
1632 return 0;
1633
1634 /* OK, we got the '/' ! */
1635 smp->data.type = SMP_T_STR;
1636 smp->data.u.str.area = ptr;
1637
1638 while (ptr < end && *ptr != '?')
1639 ptr++;
1640
1641 smp->data.u.str.data = ptr - smp->data.u.str.area;
1642 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
1643 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001644 return 1;
1645}
1646
1647/* This produces a concatenation of the first occurrence of the Host header
1648 * followed by the path component if it begins with a slash ('/'). This means
1649 * that '*' will not be added, resulting in exactly the first Host entry.
1650 * If no Host header is found, then the path is returned as-is. The returned
1651 * value is stored in the trash so it does not need to be marked constant.
1652 * The returned sample is of type string.
1653 */
1654static int smp_fetch_base(const struct arg *args, struct sample *smp, const char *kw, void *private)
1655{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001656 struct channel *chn = SMP_REQ_CHN(smp);
Willy Tarreau79e57332018-10-02 16:01:16 +02001657 struct buffer *temp;
1658
Christopher Faulet46575cd2019-04-17 11:40:30 +02001659 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001660 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +02001661 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001662 struct htx_sl *sl;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001663 struct http_hdr_ctx ctx;
1664 struct ist path;
Willy Tarreau79e57332018-10-02 16:01:16 +02001665
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001666 if (!htx)
1667 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001668
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001669 ctx.blk = NULL;
1670 if (!http_find_header(htx, ist("Host"), &ctx, 0) || !ctx.value.len)
1671 return smp_fetch_path(args, smp, kw, private);
Willy Tarreau79e57332018-10-02 16:01:16 +02001672
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001673 /* OK we have the header value in ctx.value */
1674 temp = get_trash_chunk();
1675 chunk_memcat(temp, ctx.value.ptr, ctx.value.len);
1676
1677 /* now retrieve the path */
Christopher Faulet297fbb42019-05-13 14:41:27 +02001678 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001679 path = http_get_path(htx_sl_req_uri(sl));
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001680 if (path.ptr) {
1681 size_t len;
1682
Dragan Dosen8861e1c2019-02-12 19:50:31 +01001683 for (len = 0; len < path.len && *(path.ptr + len) != '?'; len++)
1684 ;
1685
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001686 if (len && *(path.ptr) == '/')
1687 chunk_memcat(temp, path.ptr, len);
1688 }
1689
1690 smp->data.type = SMP_T_STR;
1691 smp->data.u.str = *temp;
1692 }
1693 else {
1694 /* LEGACY version */
1695 struct http_txn *txn;
1696 char *ptr, *end, *beg;
1697 struct hdr_ctx ctx;
1698
Christopher Faulet89dc4992019-04-17 12:02:59 +02001699 CHECK_HTTP_MESSAGE_FIRST(chn);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001700
1701 txn = smp->strm->txn;
1702 ctx.idx = 0;
Christopher Faulet89dc4992019-04-17 12:02:59 +02001703 if (!http_find_header2("Host", 4, ci_head(chn), &txn->hdr_idx, &ctx) || !ctx.vlen)
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001704 return smp_fetch_path(args, smp, kw, private);
1705
1706 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
1707 temp = get_trash_chunk();
1708 memcpy(temp->area, ctx.line + ctx.val, ctx.vlen);
1709 smp->data.type = SMP_T_STR;
1710 smp->data.u.str.area = temp->area;
1711 smp->data.u.str.data = ctx.vlen;
Willy Tarreau79e57332018-10-02 16:01:16 +02001712
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001713 /* now retrieve the path */
Christopher Faulet89dc4992019-04-17 12:02:59 +02001714 end = ci_head(chn) + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001715 beg = http_txn_get_path(txn);
1716 if (!beg)
1717 beg = end;
Willy Tarreau79e57332018-10-02 16:01:16 +02001718
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001719 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
1720
1721 if (beg < ptr && *beg == '/') {
1722 memcpy(smp->data.u.str.area + smp->data.u.str.data, beg,
1723 ptr - beg);
1724 smp->data.u.str.data += ptr - beg;
1725 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001726 }
1727
1728 smp->flags = SMP_F_VOL_1ST;
1729 return 1;
1730}
1731
1732/* This produces a 32-bit hash of the concatenation of the first occurrence of
1733 * the Host header followed by the path component if it begins with a slash ('/').
1734 * This means that '*' will not be added, resulting in exactly the first Host
1735 * entry. If no Host header is found, then the path is used. The resulting value
1736 * is hashed using the path hash followed by a full avalanche hash and provides a
1737 * 32-bit integer value. This fetch is useful for tracking per-path activity on
1738 * high-traffic sites without having to store whole paths.
1739 */
1740static int smp_fetch_base32(const struct arg *args, struct sample *smp, const char *kw, void *private)
1741{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001742 struct channel *chn = SMP_REQ_CHN(smp);
Willy Tarreau79e57332018-10-02 16:01:16 +02001743 unsigned int hash = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001744
Christopher Faulet46575cd2019-04-17 11:40:30 +02001745 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001746 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +02001747 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001748 struct htx_sl *sl;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001749 struct http_hdr_ctx ctx;
1750 struct ist path;
Willy Tarreau79e57332018-10-02 16:01:16 +02001751
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001752 if (!htx)
1753 return 0;
1754
1755 ctx.blk = NULL;
Dragan Dosen8861e1c2019-02-12 19:50:31 +01001756 if (http_find_header(htx, ist("Host"), &ctx, 0)) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001757 /* OK we have the header value in ctx.value */
1758 while (ctx.value.len--)
1759 hash = *(ctx.value.ptr++) + (hash << 6) + (hash << 16) - hash;
1760 }
1761
1762 /* now retrieve the path */
Christopher Faulet297fbb42019-05-13 14:41:27 +02001763 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001764 path = http_get_path(htx_sl_req_uri(sl));
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001765 if (path.ptr) {
1766 size_t len;
1767
Dragan Dosen8861e1c2019-02-12 19:50:31 +01001768 for (len = 0; len < path.len && *(path.ptr + len) != '?'; len++)
1769 ;
1770
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001771 if (len && *(path.ptr) == '/') {
1772 while (len--)
1773 hash = *(path.ptr++) + (hash << 6) + (hash << 16) - hash;
1774 }
1775 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001776 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001777 else {
1778 /* LEGACY version */
1779 struct http_txn *txn;
1780 struct hdr_ctx ctx;
1781 char *ptr, *beg, *end;
1782 int len;
1783
Christopher Faulet89dc4992019-04-17 12:02:59 +02001784 CHECK_HTTP_MESSAGE_FIRST(chn);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001785
1786 txn = smp->strm->txn;
1787 ctx.idx = 0;
Christopher Faulet89dc4992019-04-17 12:02:59 +02001788 if (http_find_header2("Host", 4, ci_head(chn), &txn->hdr_idx, &ctx)) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001789 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
1790 ptr = ctx.line + ctx.val;
1791 len = ctx.vlen;
1792 while (len--)
1793 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
1794 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001795
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001796 /* now retrieve the path */
Christopher Faulet89dc4992019-04-17 12:02:59 +02001797 end = ci_head(chn) + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001798 beg = http_txn_get_path(txn);
1799 if (!beg)
1800 beg = end;
Willy Tarreau79e57332018-10-02 16:01:16 +02001801
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001802 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
Willy Tarreau79e57332018-10-02 16:01:16 +02001803
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001804 if (beg < ptr && *beg == '/') {
1805 while (beg < ptr)
1806 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
1807 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001808 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001809
Willy Tarreau79e57332018-10-02 16:01:16 +02001810 hash = full_hash(hash);
1811
1812 smp->data.type = SMP_T_SINT;
1813 smp->data.u.sint = hash;
1814 smp->flags = SMP_F_VOL_1ST;
1815 return 1;
1816}
1817
1818/* This concatenates the source address with the 32-bit hash of the Host and
1819 * path as returned by smp_fetch_base32(). The idea is to have per-source and
1820 * per-path counters. The result is a binary block from 8 to 20 bytes depending
1821 * on the source address length. The path hash is stored before the address so
1822 * that in environments where IPv6 is insignificant, truncating the output to
1823 * 8 bytes would still work.
1824 */
1825static int smp_fetch_base32_src(const struct arg *args, struct sample *smp, const char *kw, void *private)
1826{
1827 struct buffer *temp;
1828 struct connection *cli_conn = objt_conn(smp->sess->origin);
1829
1830 if (!cli_conn)
1831 return 0;
1832
1833 if (!smp_fetch_base32(args, smp, kw, private))
1834 return 0;
1835
1836 temp = get_trash_chunk();
1837 *(unsigned int *) temp->area = htonl(smp->data.u.sint);
1838 temp->data += sizeof(unsigned int);
1839
1840 switch (cli_conn->addr.from.ss_family) {
1841 case AF_INET:
1842 memcpy(temp->area + temp->data,
1843 &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr,
1844 4);
1845 temp->data += 4;
1846 break;
1847 case AF_INET6:
1848 memcpy(temp->area + temp->data,
1849 &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr,
1850 16);
1851 temp->data += 16;
1852 break;
1853 default:
1854 return 0;
1855 }
1856
1857 smp->data.u.str = *temp;
1858 smp->data.type = SMP_T_BIN;
1859 return 1;
1860}
1861
1862/* Extracts the query string, which comes after the question mark '?'. If no
1863 * question mark is found, nothing is returned. Otherwise it returns a sample
1864 * of type string carrying the whole query string.
1865 */
1866static int smp_fetch_query(const struct arg *args, struct sample *smp, const char *kw, void *private)
1867{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001868 struct channel *chn = SMP_REQ_CHN(smp);
Willy Tarreau79e57332018-10-02 16:01:16 +02001869 char *ptr, *end;
1870
Christopher Faulet46575cd2019-04-17 11:40:30 +02001871 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001872 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +02001873 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001874 struct htx_sl *sl;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001875
1876 if (!htx)
1877 return 0;
1878
Christopher Faulet297fbb42019-05-13 14:41:27 +02001879 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001880 ptr = HTX_SL_REQ_UPTR(sl);
1881 end = HTX_SL_REQ_UPTR(sl) + HTX_SL_REQ_ULEN(sl);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001882 }
1883 else {
1884 /* LEGACY version */
1885 struct http_txn *txn;
1886
Christopher Faulet89dc4992019-04-17 12:02:59 +02001887 CHECK_HTTP_MESSAGE_FIRST(chn);
Willy Tarreau79e57332018-10-02 16:01:16 +02001888
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001889 txn = smp->strm->txn;
Christopher Faulet89dc4992019-04-17 12:02:59 +02001890 ptr = ci_head(chn) + txn->req.sl.rq.u;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001891 end = ptr + txn->req.sl.rq.u_l;
1892 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001893
1894 /* look up the '?' */
1895 do {
1896 if (ptr == end)
1897 return 0;
1898 } while (*ptr++ != '?');
1899
1900 smp->data.type = SMP_T_STR;
1901 smp->data.u.str.area = ptr;
1902 smp->data.u.str.data = end - ptr;
1903 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
1904 return 1;
1905}
1906
1907static int smp_fetch_proto_http(const struct arg *args, struct sample *smp, const char *kw, void *private)
1908{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001909 struct channel *chn = SMP_REQ_CHN(smp);
1910
Christopher Faulet46575cd2019-04-17 11:40:30 +02001911 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001912 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +02001913 struct htx *htx = smp_prefetch_htx(smp, chn, 0);
Willy Tarreau79e57332018-10-02 16:01:16 +02001914
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001915 if (!htx)
1916 return 0;
1917 }
1918 else {
1919 /* LEGACY version */
Willy Tarreau79e57332018-10-02 16:01:16 +02001920
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001921 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
1922 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
1923 */
Christopher Faulet89dc4992019-04-17 12:02:59 +02001924 CHECK_HTTP_MESSAGE_FIRST_PERM(chn);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001925 }
1926 smp->data.type = SMP_T_BOOL;
Willy Tarreau79e57332018-10-02 16:01:16 +02001927 smp->data.u.sint = 1;
1928 return 1;
1929}
1930
1931/* return a valid test if the current request is the first one on the connection */
1932static int smp_fetch_http_first_req(const struct arg *args, struct sample *smp, const char *kw, void *private)
1933{
Willy Tarreaud095c672020-04-29 11:52:13 +02001934 if (!smp->strm)
1935 return 0;
1936
Willy Tarreau79e57332018-10-02 16:01:16 +02001937 smp->data.type = SMP_T_BOOL;
1938 smp->data.u.sint = !(smp->strm->txn->flags & TX_NOT_FIRST);
1939 return 1;
1940}
1941
1942/* Accepts exactly 1 argument of type userlist */
1943static int smp_fetch_http_auth(const struct arg *args, struct sample *smp, const char *kw, void *private)
1944{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001945 struct channel *chn = SMP_REQ_CHN(smp);
Willy Tarreau79e57332018-10-02 16:01:16 +02001946
1947 if (!args || args->type != ARGT_USR)
1948 return 0;
1949
Christopher Faulet46575cd2019-04-17 11:40:30 +02001950 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001951 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +02001952 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Willy Tarreau79e57332018-10-02 16:01:16 +02001953
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001954 if (!htx)
1955 return 0;
Christopher Faulete98411b2019-07-15 13:58:29 +02001956 if (!get_http_auth(smp, htx))
1957 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001958 }
1959 else {
1960 /* LEGACY version */
Christopher Faulet89dc4992019-04-17 12:02:59 +02001961 CHECK_HTTP_MESSAGE_FIRST(chn);
Christopher Faulete98411b2019-07-15 13:58:29 +02001962 if (!get_http_auth(smp, NULL))
1963 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001964 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001965
1966 smp->data.type = SMP_T_BOOL;
1967 smp->data.u.sint = check_user(args->data.usr, smp->strm->txn->auth.user,
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001968 smp->strm->txn->auth.pass);
Willy Tarreau79e57332018-10-02 16:01:16 +02001969 return 1;
1970}
1971
1972/* Accepts exactly 1 argument of type userlist */
1973static int smp_fetch_http_auth_grp(const struct arg *args, struct sample *smp, const char *kw, void *private)
1974{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001975 struct channel *chn = SMP_REQ_CHN(smp);
1976
Willy Tarreau79e57332018-10-02 16:01:16 +02001977 if (!args || args->type != ARGT_USR)
1978 return 0;
1979
Christopher Faulet46575cd2019-04-17 11:40:30 +02001980 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001981 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +02001982 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001983
1984 if (!htx)
1985 return 0;
Christopher Faulete98411b2019-07-15 13:58:29 +02001986 if (!get_http_auth(smp, htx))
1987 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001988 }
1989 else {
1990 /* LEGACY version */
Christopher Faulet89dc4992019-04-17 12:02:59 +02001991 CHECK_HTTP_MESSAGE_FIRST(chn);
Christopher Faulete98411b2019-07-15 13:58:29 +02001992 if (!get_http_auth(smp, NULL))
1993 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001994 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001995
Willy Tarreau79e57332018-10-02 16:01:16 +02001996 /* if the user does not belong to the userlist or has a wrong password,
1997 * report that it unconditionally does not match. Otherwise we return
1998 * a string containing the username.
1999 */
2000 if (!check_user(args->data.usr, smp->strm->txn->auth.user,
2001 smp->strm->txn->auth.pass))
2002 return 0;
2003
2004 /* pat_match_auth() will need the user list */
2005 smp->ctx.a[0] = args->data.usr;
2006
2007 smp->data.type = SMP_T_STR;
2008 smp->flags = SMP_F_CONST;
2009 smp->data.u.str.area = smp->strm->txn->auth.user;
2010 smp->data.u.str.data = strlen(smp->strm->txn->auth.user);
2011
2012 return 1;
2013}
2014
2015/* Fetch a captured HTTP request header. The index is the position of
2016 * the "capture" option in the configuration file
2017 */
2018static int smp_fetch_capture_req_hdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
2019{
Willy Tarreau79ed4672020-04-29 11:44:54 +02002020 struct proxy *fe;
Willy Tarreau79e57332018-10-02 16:01:16 +02002021 int idx;
2022
2023 if (!args || args->type != ARGT_SINT)
2024 return 0;
2025
Willy Tarreau79ed4672020-04-29 11:44:54 +02002026 if (!smp->strm)
2027 return 0;
2028
2029 fe = strm_fe(smp->strm);
Willy Tarreau79e57332018-10-02 16:01:16 +02002030 idx = args->data.sint;
2031
2032 if (idx > (fe->nb_req_cap - 1) || smp->strm->req_cap == NULL || smp->strm->req_cap[idx] == NULL)
2033 return 0;
2034
2035 smp->data.type = SMP_T_STR;
2036 smp->flags |= SMP_F_CONST;
2037 smp->data.u.str.area = smp->strm->req_cap[idx];
2038 smp->data.u.str.data = strlen(smp->strm->req_cap[idx]);
2039
2040 return 1;
2041}
2042
2043/* Fetch a captured HTTP response header. The index is the position of
2044 * the "capture" option in the configuration file
2045 */
2046static int smp_fetch_capture_res_hdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
2047{
Willy Tarreau79ed4672020-04-29 11:44:54 +02002048 struct proxy *fe;
Willy Tarreau79e57332018-10-02 16:01:16 +02002049 int idx;
2050
2051 if (!args || args->type != ARGT_SINT)
2052 return 0;
2053
Willy Tarreau79ed4672020-04-29 11:44:54 +02002054 if (!smp->strm)
2055 return 0;
2056
2057 fe = strm_fe(smp->strm);
Willy Tarreau79e57332018-10-02 16:01:16 +02002058 idx = args->data.sint;
2059
2060 if (idx > (fe->nb_rsp_cap - 1) || smp->strm->res_cap == NULL || smp->strm->res_cap[idx] == NULL)
2061 return 0;
2062
2063 smp->data.type = SMP_T_STR;
2064 smp->flags |= SMP_F_CONST;
2065 smp->data.u.str.area = smp->strm->res_cap[idx];
2066 smp->data.u.str.data = strlen(smp->strm->res_cap[idx]);
2067
2068 return 1;
2069}
2070
2071/* Extracts the METHOD in the HTTP request, the txn->uri should be filled before the call */
2072static int smp_fetch_capture_req_method(const struct arg *args, struct sample *smp, const char *kw, void *private)
2073{
2074 struct buffer *temp;
Willy Tarreau79ed4672020-04-29 11:44:54 +02002075 struct http_txn *txn;
Willy Tarreau79e57332018-10-02 16:01:16 +02002076 char *ptr;
2077
Willy Tarreau79ed4672020-04-29 11:44:54 +02002078 if (!smp->strm)
2079 return 0;
2080
2081 txn = smp->strm->txn;
Willy Tarreau79e57332018-10-02 16:01:16 +02002082 if (!txn || !txn->uri)
2083 return 0;
2084
2085 ptr = txn->uri;
2086
2087 while (*ptr != ' ' && *ptr != '\0') /* find first space */
2088 ptr++;
2089
2090 temp = get_trash_chunk();
2091 temp->area = txn->uri;
2092 temp->data = ptr - txn->uri;
2093 smp->data.u.str = *temp;
2094 smp->data.type = SMP_T_STR;
2095 smp->flags = SMP_F_CONST;
2096
2097 return 1;
2098
2099}
2100
2101/* Extracts the path in the HTTP request, the txn->uri should be filled before the call */
2102static int smp_fetch_capture_req_uri(const struct arg *args, struct sample *smp, const char *kw, void *private)
2103{
Willy Tarreau79ed4672020-04-29 11:44:54 +02002104 struct http_txn *txn;
Willy Tarreau79e57332018-10-02 16:01:16 +02002105 struct ist path;
2106 const char *ptr;
2107
Willy Tarreau79ed4672020-04-29 11:44:54 +02002108 if (!smp->strm)
2109 return 0;
2110
2111 txn = smp->strm->txn;
Willy Tarreau79e57332018-10-02 16:01:16 +02002112 if (!txn || !txn->uri)
2113 return 0;
2114
2115 ptr = txn->uri;
2116
2117 while (*ptr != ' ' && *ptr != '\0') /* find first space */
2118 ptr++;
2119
2120 if (!*ptr)
2121 return 0;
2122
Christopher Faulet78337bb2018-11-15 14:35:18 +01002123 /* skip the first space and find space after URI */
2124 path = ist2(++ptr, 0);
2125 while (*ptr != ' ' && *ptr != '\0')
2126 ptr++;
2127 path.len = ptr - path.ptr;
Willy Tarreau79e57332018-10-02 16:01:16 +02002128
Christopher Faulet78337bb2018-11-15 14:35:18 +01002129 path = http_get_path(path);
Willy Tarreau79e57332018-10-02 16:01:16 +02002130 if (!path.ptr)
2131 return 0;
2132
2133 smp->data.u.str.area = path.ptr;
2134 smp->data.u.str.data = path.len;
2135 smp->data.type = SMP_T_STR;
2136 smp->flags = SMP_F_CONST;
2137
2138 return 1;
2139}
2140
2141/* Retrieves the HTTP version from the request (either 1.0 or 1.1) and emits it
2142 * as a string (either "HTTP/1.0" or "HTTP/1.1").
2143 */
2144static int smp_fetch_capture_req_ver(const struct arg *args, struct sample *smp, const char *kw, void *private)
2145{
Willy Tarreau79ed4672020-04-29 11:44:54 +02002146 struct http_txn *txn;
2147
2148 if (!smp->strm)
2149 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02002150
Willy Tarreau79ed4672020-04-29 11:44:54 +02002151 txn = smp->strm->txn;
Willy Tarreau79e57332018-10-02 16:01:16 +02002152 if (!txn || txn->req.msg_state < HTTP_MSG_HDR_FIRST)
2153 return 0;
2154
2155 if (txn->req.flags & HTTP_MSGF_VER_11)
2156 smp->data.u.str.area = "HTTP/1.1";
2157 else
2158 smp->data.u.str.area = "HTTP/1.0";
2159
2160 smp->data.u.str.data = 8;
2161 smp->data.type = SMP_T_STR;
2162 smp->flags = SMP_F_CONST;
2163 return 1;
2164
2165}
2166
2167/* Retrieves the HTTP version from the response (either 1.0 or 1.1) and emits it
2168 * as a string (either "HTTP/1.0" or "HTTP/1.1").
2169 */
2170static int smp_fetch_capture_res_ver(const struct arg *args, struct sample *smp, const char *kw, void *private)
2171{
Willy Tarreau79ed4672020-04-29 11:44:54 +02002172 struct http_txn *txn;
2173
2174 if (!smp->strm)
2175 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02002176
Willy Tarreau79ed4672020-04-29 11:44:54 +02002177 txn = smp->strm->txn;
Willy Tarreau79e57332018-10-02 16:01:16 +02002178 if (!txn || txn->rsp.msg_state < HTTP_MSG_HDR_FIRST)
2179 return 0;
2180
2181 if (txn->rsp.flags & HTTP_MSGF_VER_11)
2182 smp->data.u.str.area = "HTTP/1.1";
2183 else
2184 smp->data.u.str.area = "HTTP/1.0";
2185
2186 smp->data.u.str.data = 8;
2187 smp->data.type = SMP_T_STR;
2188 smp->flags = SMP_F_CONST;
2189 return 1;
2190
2191}
2192
2193/* Iterate over all cookies present in a message. The context is stored in
2194 * smp->ctx.a[0] for the in-header position, smp->ctx.a[1] for the
2195 * end-of-header-value, and smp->ctx.a[2] for the hdr_ctx. Depending on
2196 * the direction, multiple cookies may be parsed on the same line or not.
2197 * The cookie name is in args and the name length in args->data.str.len.
2198 * Accepts exactly 1 argument of type string. If the input options indicate
2199 * that no iterating is desired, then only last value is fetched if any.
2200 * The returned sample is of type CSTR. Can be used to parse cookies in other
2201 * files.
2202 */
2203static int smp_fetch_cookie(const struct arg *args, struct sample *smp, const char *kw, void *private)
2204{
Christopher Faulet89dc4992019-04-17 12:02:59 +02002205 /* possible keywords: req.cookie / cookie / cook, res.cookie / scook / set-cookie */
2206 struct channel *chn = ((kw[0] == 'c' || kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Willy Tarreau79e57332018-10-02 16:01:16 +02002207 int occ = 0;
2208 int found = 0;
2209
2210 if (!args || args->type != ARGT_STR)
2211 return 0;
2212
Christopher Faulet46575cd2019-04-17 11:40:30 +02002213 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002214 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +02002215 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002216 struct http_hdr_ctx *ctx = smp->ctx.a[2];
2217 struct ist hdr;
Willy Tarreau79e57332018-10-02 16:01:16 +02002218
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002219 if (!ctx) {
2220 /* first call */
2221 ctx = &static_http_hdr_ctx;
2222 ctx->blk = NULL;
2223 smp->ctx.a[2] = ctx;
2224 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002225
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002226 if (!htx)
2227 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02002228
Christopher Faulet89dc4992019-04-17 12:02:59 +02002229 hdr = (!(chn->flags & CF_ISRESP) ? ist("Cookie") : ist("Set-Cookie"));
Willy Tarreau79e57332018-10-02 16:01:16 +02002230
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002231 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
2232 /* no explicit occurrence and single fetch => last cookie by default */
2233 occ = -1;
Willy Tarreau79e57332018-10-02 16:01:16 +02002234
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002235 /* OK so basically here, either we want only one value and it's the
2236 * last one, or we want to iterate over all of them and we fetch the
2237 * next one.
Willy Tarreau79e57332018-10-02 16:01:16 +02002238 */
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002239
2240 if (!(smp->flags & SMP_F_NOT_LAST)) {
2241 /* search for the header from the beginning, we must first initialize
2242 * the search parameters.
2243 */
2244 smp->ctx.a[0] = NULL;
2245 ctx->blk = NULL;
2246 }
2247
2248 smp->flags |= SMP_F_VOL_HDR;
2249 while (1) {
2250 /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */
2251 if (!smp->ctx.a[0]) {
2252 if (!http_find_header(htx, hdr, ctx, 0))
2253 goto out;
2254
2255 if (ctx->value.len < args->data.str.data + 1)
2256 continue;
2257
2258 smp->ctx.a[0] = ctx->value.ptr;
2259 smp->ctx.a[1] = smp->ctx.a[0] + ctx->value.len;
2260 }
2261
2262 smp->data.type = SMP_T_STR;
2263 smp->flags |= SMP_F_CONST;
2264 smp->ctx.a[0] = http_extract_cookie_value(smp->ctx.a[0], smp->ctx.a[1],
2265 args->data.str.area, args->data.str.data,
2266 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
2267 &smp->data.u.str.area,
2268 &smp->data.u.str.data);
2269 if (smp->ctx.a[0]) {
2270 found = 1;
2271 if (occ >= 0) {
2272 /* one value was returned into smp->data.u.str.{str,len} */
2273 smp->flags |= SMP_F_NOT_LAST;
2274 return 1;
2275 }
2276 }
2277 /* if we're looking for last occurrence, let's loop */
2278 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002279 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002280 else {
2281 /* LEGACY version */
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002282 struct hdr_idx *idx;
2283 struct hdr_ctx *ctx = smp->ctx.a[2];
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002284 const char *hdr_name;
2285 int hdr_name_len;
2286 char *sol;
Willy Tarreau79e57332018-10-02 16:01:16 +02002287
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002288 if (!ctx) {
2289 /* first call */
2290 ctx = &static_hdr_ctx;
2291 ctx->idx = 0;
2292 smp->ctx.a[2] = ctx;
2293 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002294
Christopher Faulet89dc4992019-04-17 12:02:59 +02002295 CHECK_HTTP_MESSAGE_FIRST(chn);
Willy Tarreau79e57332018-10-02 16:01:16 +02002296
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002297 idx = &smp->strm->txn->hdr_idx;
Christopher Faulet89dc4992019-04-17 12:02:59 +02002298 if (!(chn->flags & CF_ISRESP)) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002299 hdr_name = "Cookie";
2300 hdr_name_len = 6;
2301 } else {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002302 hdr_name = "Set-Cookie";
2303 hdr_name_len = 10;
Willy Tarreau79e57332018-10-02 16:01:16 +02002304 }
2305
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002306 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
2307 /* no explicit occurrence and single fetch => last cookie by default */
2308 occ = -1;
2309
2310 /* OK so basically here, either we want only one value and it's the
2311 * last one, or we want to iterate over all of them and we fetch the
2312 * next one.
2313 */
2314
Christopher Faulet89dc4992019-04-17 12:02:59 +02002315 sol = ci_head(chn);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002316 if (!(smp->flags & SMP_F_NOT_LAST)) {
2317 /* search for the header from the beginning, we must first initialize
2318 * the search parameters.
2319 */
2320 smp->ctx.a[0] = NULL;
2321 ctx->idx = 0;
2322 }
2323
2324 smp->flags |= SMP_F_VOL_HDR;
2325
2326 while (1) {
2327 /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */
2328 if (!smp->ctx.a[0]) {
2329 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, ctx))
2330 goto out;
2331
2332 if (ctx->vlen < args->data.str.data + 1)
2333 continue;
2334
2335 smp->ctx.a[0] = ctx->line + ctx->val;
2336 smp->ctx.a[1] = smp->ctx.a[0] + ctx->vlen;
2337 }
2338
2339 smp->data.type = SMP_T_STR;
2340 smp->flags |= SMP_F_CONST;
2341 smp->ctx.a[0] = http_extract_cookie_value(smp->ctx.a[0], smp->ctx.a[1],
2342 args->data.str.area, args->data.str.data,
2343 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
2344 &smp->data.u.str.area, &smp->data.u.str.data);
2345 if (smp->ctx.a[0]) {
2346 found = 1;
2347 if (occ >= 0) {
2348 /* one value was returned into smp->data.u.str.{str,len} */
2349 smp->flags |= SMP_F_NOT_LAST;
2350 return 1;
2351 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002352 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002353 /* if we're looking for last occurrence, let's loop */
Willy Tarreau79e57332018-10-02 16:01:16 +02002354 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002355 }
2356 /* all cookie headers and values were scanned. If we're looking for the
2357 * last occurrence, we may return it now.
2358 */
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002359 out:
Willy Tarreau79e57332018-10-02 16:01:16 +02002360 smp->flags &= ~SMP_F_NOT_LAST;
2361 return found;
2362}
2363
Christopher Fauletc1f40dd2019-05-16 10:07:30 +02002364/* Same than smp_fetch_cookie() but only relies on the sample direction to
2365 * choose the right channel. So instead of duplicating the code, we just change
2366 * the keyword and then fallback on smp_fetch_cookie().
2367 */
2368static int smp_fetch_chn_cookie(const struct arg *args, struct sample *smp, const char *kw, void *private)
2369{
2370 kw = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ ? "req.cook" : "res.cook");
2371 return smp_fetch_cookie(args, smp, kw, private);
2372}
2373
Willy Tarreau79e57332018-10-02 16:01:16 +02002374/* Iterate over all cookies present in a request to count how many occurrences
2375 * match the name in args and args->data.str.len. If <multi> is non-null, then
2376 * multiple cookies may be parsed on the same line. The returned sample is of
2377 * type UINT. Accepts exactly 1 argument of type string.
2378 */
2379static int smp_fetch_cookie_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
2380{
Christopher Faulet89dc4992019-04-17 12:02:59 +02002381 /* possible keywords: req.cook_cnt / cook_cnt, res.cook_cnt / scook_cnt */
2382 struct channel *chn = ((kw[0] == 'c' || kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Willy Tarreau79e57332018-10-02 16:01:16 +02002383 char *val_beg, *val_end;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002384 int cnt;
Willy Tarreau79e57332018-10-02 16:01:16 +02002385
2386 if (!args || args->type != ARGT_STR)
2387 return 0;
2388
Christopher Faulet46575cd2019-04-17 11:40:30 +02002389 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002390 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +02002391 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002392 struct http_hdr_ctx ctx;
2393 struct ist hdr;
2394
2395 if (!htx)
2396 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02002397
Christopher Faulet89dc4992019-04-17 12:02:59 +02002398 hdr = (!(chn->flags & CF_ISRESP) ? ist("Cookie") : ist("Set-Cookie"));
Willy Tarreau79e57332018-10-02 16:01:16 +02002399
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002400 val_end = val_beg = NULL;
2401 ctx.blk = NULL;
2402 cnt = 0;
2403 while (1) {
2404 /* Note: val_beg == NULL every time we need to fetch a new header */
2405 if (!val_beg) {
2406 if (!http_find_header(htx, hdr, &ctx, 0))
2407 break;
Willy Tarreau79e57332018-10-02 16:01:16 +02002408
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002409 if (ctx.value.len < args->data.str.data + 1)
2410 continue;
Willy Tarreau79e57332018-10-02 16:01:16 +02002411
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002412 val_beg = ctx.value.ptr;
2413 val_end = val_beg + ctx.value.len;
2414 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002415
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002416 smp->data.type = SMP_T_STR;
2417 smp->flags |= SMP_F_CONST;
2418 while ((val_beg = http_extract_cookie_value(val_beg, val_end,
2419 args->data.str.area, args->data.str.data,
2420 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
2421 &smp->data.u.str.area,
2422 &smp->data.u.str.data))) {
2423 cnt++;
2424 }
2425 }
2426 }
2427 else {
2428 /* LEGACY version */
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002429 struct hdr_idx *idx;
2430 struct hdr_ctx ctx;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002431 const char *hdr_name;
2432 int hdr_name_len;
2433 char *sol;
Willy Tarreau79e57332018-10-02 16:01:16 +02002434
Christopher Faulet89dc4992019-04-17 12:02:59 +02002435 CHECK_HTTP_MESSAGE_FIRST(chn);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002436
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002437 idx = &smp->strm->txn->hdr_idx;
Christopher Faulet89dc4992019-04-17 12:02:59 +02002438 if (!(chn->flags & CF_ISRESP)) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002439 hdr_name = "Cookie";
2440 hdr_name_len = 6;
2441 } else {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002442 hdr_name = "Set-Cookie";
2443 hdr_name_len = 10;
Willy Tarreau79e57332018-10-02 16:01:16 +02002444 }
2445
Christopher Faulet89dc4992019-04-17 12:02:59 +02002446 sol = ci_head(chn);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002447 val_end = val_beg = NULL;
2448 ctx.idx = 0;
2449 cnt = 0;
2450
2451 while (1) {
2452 /* Note: val_beg == NULL every time we need to fetch a new header */
2453 if (!val_beg) {
2454 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, &ctx))
2455 break;
2456
2457 if (ctx.vlen < args->data.str.data + 1)
2458 continue;
2459
2460 val_beg = ctx.line + ctx.val;
2461 val_end = val_beg + ctx.vlen;
2462 }
2463
2464 smp->data.type = SMP_T_STR;
2465 smp->flags |= SMP_F_CONST;
2466 while ((val_beg = http_extract_cookie_value(val_beg, val_end,
2467 args->data.str.area, args->data.str.data,
2468 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
2469 &smp->data.u.str.area, &smp->data.u.str.data))) {
2470 cnt++;
2471 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002472 }
2473 }
2474
2475 smp->data.type = SMP_T_SINT;
2476 smp->data.u.sint = cnt;
2477 smp->flags |= SMP_F_VOL_HDR;
2478 return 1;
2479}
2480
2481/* Fetch an cookie's integer value. The integer value is returned. It
2482 * takes a mandatory argument of type string. It relies on smp_fetch_cookie().
2483 */
2484static int smp_fetch_cookie_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
2485{
2486 int ret = smp_fetch_cookie(args, smp, kw, private);
2487
2488 if (ret > 0) {
2489 smp->data.type = SMP_T_SINT;
2490 smp->data.u.sint = strl2ic(smp->data.u.str.area,
2491 smp->data.u.str.data);
2492 }
2493
2494 return ret;
2495}
2496
2497/************************************************************************/
2498/* The code below is dedicated to sample fetches */
2499/************************************************************************/
2500
2501/* This scans a URL-encoded query string. It takes an optionally wrapping
2502 * string whose first contigous chunk has its beginning in ctx->a[0] and end
2503 * in ctx->a[1], and the optional second part in (ctx->a[2]..ctx->a[3]). The
2504 * pointers are updated for next iteration before leaving.
2505 */
2506static int smp_fetch_param(char delim, const char *name, int name_len, const struct arg *args, struct sample *smp, const char *kw, void *private)
2507{
2508 const char *vstart, *vend;
2509 struct buffer *temp;
2510 const char **chunks = (const char **)smp->ctx.a;
2511
2512 if (!http_find_next_url_param(chunks, name, name_len,
2513 &vstart, &vend, delim))
2514 return 0;
2515
2516 /* Create sample. If the value is contiguous, return the pointer as CONST,
2517 * if the value is wrapped, copy-it in a buffer.
2518 */
2519 smp->data.type = SMP_T_STR;
2520 if (chunks[2] &&
2521 vstart >= chunks[0] && vstart <= chunks[1] &&
2522 vend >= chunks[2] && vend <= chunks[3]) {
2523 /* Wrapped case. */
2524 temp = get_trash_chunk();
2525 memcpy(temp->area, vstart, chunks[1] - vstart);
2526 memcpy(temp->area + ( chunks[1] - vstart ), chunks[2],
2527 vend - chunks[2]);
2528 smp->data.u.str.area = temp->area;
2529 smp->data.u.str.data = ( chunks[1] - vstart ) + ( vend - chunks[2] );
2530 } else {
2531 /* Contiguous case. */
2532 smp->data.u.str.area = (char *)vstart;
2533 smp->data.u.str.data = vend - vstart;
2534 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
2535 }
2536
2537 /* Update context, check wrapping. */
2538 chunks[0] = vend;
2539 if (chunks[2] && vend >= chunks[2] && vend <= chunks[3]) {
2540 chunks[1] = chunks[3];
2541 chunks[2] = NULL;
2542 }
2543
2544 if (chunks[0] < chunks[1])
2545 smp->flags |= SMP_F_NOT_LAST;
2546
2547 return 1;
2548}
2549
2550/* This function iterates over each parameter of the query string. It uses
2551 * ctx->a[0] and ctx->a[1] to store the beginning and end of the current
2552 * parameter. Since it uses smp_fetch_param(), ctx->a[2..3] are both NULL.
2553 * An optional parameter name is passed in args[0], otherwise any parameter is
2554 * considered. It supports an optional delimiter argument for the beginning of
2555 * the string in args[1], which defaults to "?".
2556 */
2557static int smp_fetch_url_param(const struct arg *args, struct sample *smp, const char *kw, void *private)
2558{
Christopher Faulet89dc4992019-04-17 12:02:59 +02002559 struct channel *chn = SMP_REQ_CHN(smp);
Willy Tarreau79e57332018-10-02 16:01:16 +02002560 char delim = '?';
2561 const char *name;
2562 int name_len;
2563
2564 if (!args ||
2565 (args[0].type && args[0].type != ARGT_STR) ||
2566 (args[1].type && args[1].type != ARGT_STR))
2567 return 0;
2568
2569 name = "";
2570 name_len = 0;
2571 if (args->type == ARGT_STR) {
2572 name = args->data.str.area;
2573 name_len = args->data.str.data;
2574 }
2575
2576 if (args[1].type)
2577 delim = *args[1].data.str.area;
2578
2579 if (!smp->ctx.a[0]) { // first call, find the query string
Christopher Faulet46575cd2019-04-17 11:40:30 +02002580 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002581 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +02002582 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01002583 struct htx_sl *sl;
Willy Tarreau79e57332018-10-02 16:01:16 +02002584
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002585 if (!htx)
2586 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02002587
Christopher Faulet297fbb42019-05-13 14:41:27 +02002588 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01002589 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 +02002590 if (!smp->ctx.a[0])
2591 return 0;
2592
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01002593 smp->ctx.a[1] = HTX_SL_REQ_UPTR(sl) + HTX_SL_REQ_ULEN(sl);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002594 }
2595 else {
2596 /* LEGACY version */
2597 struct http_msg *msg;
2598
Christopher Faulet89dc4992019-04-17 12:02:59 +02002599 CHECK_HTTP_MESSAGE_FIRST(chn);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002600
2601 msg = &smp->strm->txn->req;
2602
Christopher Faulet89dc4992019-04-17 12:02:59 +02002603 smp->ctx.a[0] = http_find_param_list(ci_head(chn) + msg->sl.rq.u,
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002604 msg->sl.rq.u_l, delim);
2605 if (!smp->ctx.a[0])
2606 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02002607
Christopher Faulet89dc4992019-04-17 12:02:59 +02002608 smp->ctx.a[1] = ci_head(chn) + msg->sl.rq.u + msg->sl.rq.u_l;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002609 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002610
2611 /* Assume that the context is filled with NULL pointer
2612 * before the first call.
2613 * smp->ctx.a[2] = NULL;
2614 * smp->ctx.a[3] = NULL;
2615 */
2616 }
2617
2618 return smp_fetch_param(delim, name, name_len, args, smp, kw, private);
2619}
2620
2621/* This function iterates over each parameter of the body. This requires
2622 * that the body has been waited for using http-buffer-request. It uses
2623 * ctx->a[0] and ctx->a[1] to store the beginning and end of the first
2624 * contigous part of the body, and optionally ctx->a[2..3] to reference the
2625 * optional second part if the body wraps at the end of the buffer. An optional
2626 * parameter name is passed in args[0], otherwise any parameter is considered.
2627 */
2628static int smp_fetch_body_param(const struct arg *args, struct sample *smp, const char *kw, void *private)
2629{
Christopher Faulet89dc4992019-04-17 12:02:59 +02002630 struct channel *chn = SMP_REQ_CHN(smp);
Willy Tarreau79e57332018-10-02 16:01:16 +02002631 const char *name;
2632 int name_len;
2633
2634 if (!args || (args[0].type && args[0].type != ARGT_STR))
2635 return 0;
2636
2637 name = "";
2638 name_len = 0;
2639 if (args[0].type == ARGT_STR) {
2640 name = args[0].data.str.area;
2641 name_len = args[0].data.str.data;
2642 }
2643
2644 if (!smp->ctx.a[0]) { // first call, find the query string
Christopher Faulet46575cd2019-04-17 11:40:30 +02002645 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002646 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +02002647 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002648 struct buffer *temp;
2649 int32_t pos;
Willy Tarreau79e57332018-10-02 16:01:16 +02002650
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002651 if (!htx)
2652 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02002653
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002654 temp = get_trash_chunk();
Christopher Fauleta3f15502019-05-13 15:27:23 +02002655 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002656 struct htx_blk *blk = htx_get_blk(htx, pos);
2657 enum htx_blk_type type = htx_get_blk_type(blk);
Willy Tarreau79e57332018-10-02 16:01:16 +02002658
Christopher Faulet54b5e212019-06-04 10:08:28 +02002659 if (type == HTX_BLK_EOM || type == HTX_BLK_TLR || type == HTX_BLK_EOT)
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002660 break;
2661 if (type == HTX_BLK_DATA) {
Christopher Fauletc59ff232018-12-03 13:58:44 +01002662 if (!htx_data_to_h1(htx_get_blk_value(htx, blk), temp, 0))
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002663 return 0;
2664 }
2665 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002666
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002667 smp->ctx.a[0] = temp->area;
2668 smp->ctx.a[1] = temp->area + temp->data;
Willy Tarreau79e57332018-10-02 16:01:16 +02002669
2670 /* Assume that the context is filled with NULL pointer
2671 * before the first call.
2672 * smp->ctx.a[2] = NULL;
2673 * smp->ctx.a[3] = NULL;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002674 */
Willy Tarreau79e57332018-10-02 16:01:16 +02002675 }
2676 else {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002677 /* LEGACY version */
2678 struct http_msg *msg;
2679 unsigned long len;
2680 unsigned long block1;
2681 char *body;
2682
Christopher Faulet89dc4992019-04-17 12:02:59 +02002683 CHECK_HTTP_MESSAGE_FIRST(chn);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002684
Christopher Faulet89dc4992019-04-17 12:02:59 +02002685 msg = &smp->strm->txn->req;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002686 len = http_body_bytes(msg);
Christopher Faulet89dc4992019-04-17 12:02:59 +02002687 body = c_ptr(chn, -http_data_rewind(msg));
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002688
2689 block1 = len;
Christopher Faulet89dc4992019-04-17 12:02:59 +02002690 if (block1 > b_wrap(&chn->buf) - body)
2691 block1 = b_wrap(&chn->buf) - body;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002692
2693 if (block1 == len) {
2694 /* buffer is not wrapped (or empty) */
2695 smp->ctx.a[0] = body;
2696 smp->ctx.a[1] = body + len;
2697
2698 /* Assume that the context is filled with NULL pointer
2699 * before the first call.
2700 * smp->ctx.a[2] = NULL;
2701 * smp->ctx.a[3] = NULL;
2702 */
2703 }
2704 else {
2705 /* buffer is wrapped, we need to defragment it */
2706 smp->ctx.a[0] = body;
2707 smp->ctx.a[1] = body + block1;
Christopher Faulet89dc4992019-04-17 12:02:59 +02002708 smp->ctx.a[2] = b_orig(&chn->buf);
2709 smp->ctx.a[3] = b_orig(&chn->buf) + ( len - block1 );
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002710 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002711 }
2712 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002713
Willy Tarreau79e57332018-10-02 16:01:16 +02002714 return smp_fetch_param('&', name, name_len, args, smp, kw, private);
2715}
2716
2717/* Return the signed integer value for the specified url parameter (see url_param
2718 * above).
2719 */
2720static int smp_fetch_url_param_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
2721{
2722 int ret = smp_fetch_url_param(args, smp, kw, private);
2723
2724 if (ret > 0) {
2725 smp->data.type = SMP_T_SINT;
2726 smp->data.u.sint = strl2ic(smp->data.u.str.area,
2727 smp->data.u.str.data);
2728 }
2729
2730 return ret;
2731}
2732
2733/* This produces a 32-bit hash of the concatenation of the first occurrence of
2734 * the Host header followed by the path component if it begins with a slash ('/').
2735 * This means that '*' will not be added, resulting in exactly the first Host
2736 * entry. If no Host header is found, then the path is used. The resulting value
2737 * is hashed using the url hash followed by a full avalanche hash and provides a
2738 * 32-bit integer value. This fetch is useful for tracking per-URL activity on
2739 * high-traffic sites without having to store whole paths.
2740 * this differs from the base32 functions in that it includes the url parameters
2741 * as well as the path
2742 */
2743static int smp_fetch_url32(const struct arg *args, struct sample *smp, const char *kw, void *private)
2744{
Christopher Faulet89dc4992019-04-17 12:02:59 +02002745 struct channel *chn = SMP_REQ_CHN(smp);
Willy Tarreau79e57332018-10-02 16:01:16 +02002746 unsigned int hash = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02002747
Christopher Faulet46575cd2019-04-17 11:40:30 +02002748 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002749 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +02002750 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002751 struct http_hdr_ctx ctx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01002752 struct htx_sl *sl;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002753 struct ist path;
Willy Tarreau79e57332018-10-02 16:01:16 +02002754
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002755 if (!htx)
2756 return 0;
2757
2758 ctx.blk = NULL;
2759 if (http_find_header(htx, ist("Host"), &ctx, 1)) {
2760 /* OK we have the header value in ctx.value */
2761 while (ctx.value.len--)
2762 hash = *(ctx.value.ptr++) + (hash << 6) + (hash << 16) - hash;
2763 }
2764
2765 /* now retrieve the path */
Christopher Faulet297fbb42019-05-13 14:41:27 +02002766 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01002767 path = http_get_path(htx_sl_req_uri(sl));
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002768 if (path.len && *(path.ptr) == '/') {
2769 while (path.len--)
2770 hash = *(path.ptr++) + (hash << 6) + (hash << 16) - hash;
2771 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002772 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002773 else {
2774 /* LEGACY version */
2775 struct http_txn *txn;
2776 struct hdr_ctx ctx;
2777 char *ptr, *beg, *end;
2778 int len;
2779
Christopher Faulet89dc4992019-04-17 12:02:59 +02002780 CHECK_HTTP_MESSAGE_FIRST(chn);
Willy Tarreau79e57332018-10-02 16:01:16 +02002781
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002782 txn = smp->strm->txn;
2783 ctx.idx = 0;
Christopher Faulet89dc4992019-04-17 12:02:59 +02002784 if (http_find_header2("Host", 4, ci_head(chn), &txn->hdr_idx, &ctx)) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002785 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
2786 ptr = ctx.line + ctx.val;
2787 len = ctx.vlen;
2788 while (len--)
2789 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
2790 }
2791
2792 /* now retrieve the path */
Christopher Faulet89dc4992019-04-17 12:02:59 +02002793 end = ci_head(chn) + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002794 beg = http_txn_get_path(txn);
2795 if (!beg)
2796 beg = end;
Willy Tarreau79e57332018-10-02 16:01:16 +02002797
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002798 for (ptr = beg; ptr < end ; ptr++);
Willy Tarreau79e57332018-10-02 16:01:16 +02002799
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002800 if (beg < ptr && *beg == '/') {
2801 while (beg < ptr)
2802 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
2803 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002804 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002805
Willy Tarreau79e57332018-10-02 16:01:16 +02002806 hash = full_hash(hash);
2807
2808 smp->data.type = SMP_T_SINT;
2809 smp->data.u.sint = hash;
2810 smp->flags = SMP_F_VOL_1ST;
2811 return 1;
2812}
2813
2814/* This concatenates the source address with the 32-bit hash of the Host and
2815 * URL as returned by smp_fetch_base32(). The idea is to have per-source and
2816 * per-url counters. The result is a binary block from 8 to 20 bytes depending
2817 * on the source address length. The URL hash is stored before the address so
2818 * that in environments where IPv6 is insignificant, truncating the output to
2819 * 8 bytes would still work.
2820 */
2821static int smp_fetch_url32_src(const struct arg *args, struct sample *smp, const char *kw, void *private)
2822{
2823 struct buffer *temp;
2824 struct connection *cli_conn = objt_conn(smp->sess->origin);
2825
2826 if (!cli_conn)
2827 return 0;
2828
2829 if (!smp_fetch_url32(args, smp, kw, private))
2830 return 0;
2831
2832 temp = get_trash_chunk();
2833 *(unsigned int *) temp->area = htonl(smp->data.u.sint);
2834 temp->data += sizeof(unsigned int);
2835
2836 switch (cli_conn->addr.from.ss_family) {
2837 case AF_INET:
2838 memcpy(temp->area + temp->data,
2839 &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr,
2840 4);
2841 temp->data += 4;
2842 break;
2843 case AF_INET6:
2844 memcpy(temp->area + temp->data,
2845 &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr,
2846 16);
2847 temp->data += 16;
2848 break;
2849 default:
2850 return 0;
2851 }
2852
2853 smp->data.u.str = *temp;
2854 smp->data.type = SMP_T_BIN;
2855 return 1;
2856}
2857
2858/************************************************************************/
2859/* Other utility functions */
2860/************************************************************************/
2861
2862/* This function is used to validate the arguments passed to any "hdr" fetch
2863 * keyword. These keywords support an optional positive or negative occurrence
2864 * number. We must ensure that the number is greater than -MAX_HDR_HISTORY. It
2865 * is assumed that the types are already the correct ones. Returns 0 on error,
2866 * non-zero if OK. If <err> is not NULL, it will be filled with a pointer to an
2867 * error message in case of error, that the caller is responsible for freeing.
2868 * The initial location must either be freeable or NULL.
2869 * Note: this function's pointer is checked from Lua.
2870 */
2871int val_hdr(struct arg *arg, char **err_msg)
2872{
2873 if (arg && arg[1].type == ARGT_SINT && arg[1].data.sint < -MAX_HDR_HISTORY) {
2874 memprintf(err_msg, "header occurrence must be >= %d", -MAX_HDR_HISTORY);
2875 return 0;
2876 }
2877 return 1;
2878}
2879
2880/************************************************************************/
2881/* All supported sample fetch keywords must be declared here. */
2882/************************************************************************/
2883
2884/* Note: must not be declared <const> as its list will be overwritten */
2885static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
2886 { "base", smp_fetch_base, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2887 { "base32", smp_fetch_base32, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
2888 { "base32+src", smp_fetch_base32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
2889
2890 /* capture are allocated and are permanent in the stream */
2891 { "capture.req.hdr", smp_fetch_capture_req_hdr, ARG1(1,SINT), NULL, SMP_T_STR, SMP_USE_HRQHP },
2892
2893 /* retrieve these captures from the HTTP logs */
2894 { "capture.req.method", smp_fetch_capture_req_method, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
2895 { "capture.req.uri", smp_fetch_capture_req_uri, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
2896 { "capture.req.ver", smp_fetch_capture_req_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
2897
2898 { "capture.res.hdr", smp_fetch_capture_res_hdr, ARG1(1,SINT), NULL, SMP_T_STR, SMP_USE_HRSHP },
2899 { "capture.res.ver", smp_fetch_capture_res_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
2900
2901 /* cookie is valid in both directions (eg: for "stick ...") but cook*
2902 * are only here to match the ACL's name, are request-only and are used
2903 * for ACL compatibility only.
2904 */
2905 { "cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Christopher Fauletc1f40dd2019-05-16 10:07:30 +02002906 { "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 +02002907 { "cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2908 { "cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2909
2910 /* hdr is valid in both directions (eg: for "stick ...") but hdr_* are
2911 * only here to match the ACL's name, are request-only and are used for
2912 * ACL compatibility only.
2913 */
Christopher Fauletc1f40dd2019-05-16 10:07:30 +02002914 { "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 +02002915 { "hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2916 { "hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV },
2917 { "hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_SINT, SMP_USE_HRQHV },
2918
2919 { "http_auth", smp_fetch_http_auth, ARG1(1,USR), NULL, SMP_T_BOOL, SMP_USE_HRQHV },
2920 { "http_auth_group", smp_fetch_http_auth_grp, ARG1(1,USR), NULL, SMP_T_STR, SMP_USE_HRQHV },
2921 { "http_first_req", smp_fetch_http_first_req, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
2922 { "method", smp_fetch_meth, 0, NULL, SMP_T_METH, SMP_USE_HRQHP },
2923 { "path", smp_fetch_path, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2924 { "query", smp_fetch_query, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2925
2926 /* HTTP protocol on the request path */
2927 { "req.proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
2928 { "req_proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
2929
2930 /* HTTP version on the request path */
2931 { "req.ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2932 { "req_ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2933
2934 { "req.body", smp_fetch_body, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
2935 { "req.body_len", smp_fetch_body_len, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
2936 { "req.body_size", smp_fetch_body_size, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
2937 { "req.body_param", smp_fetch_body_param, ARG1(0,STR), NULL, SMP_T_BIN, SMP_USE_HRQHV },
2938
2939 { "req.hdrs", smp_fetch_hdrs, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
2940 { "req.hdrs_bin", smp_fetch_hdrs_bin, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
2941
2942 /* HTTP version on the response path */
2943 { "res.ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
2944 { "resp_ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
2945
2946 /* explicit req.{cook,hdr} are used to force the fetch direction to be request-only */
2947 { "req.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
2948 { "req.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2949 { "req.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2950
2951 { "req.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
2952 { "req.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2953 { "req.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
2954 { "req.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2955 { "req.hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV },
2956 { "req.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
2957 { "req.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_SINT, SMP_USE_HRQHV },
2958
2959 /* explicit req.{cook,hdr} are used to force the fetch direction to be response-only */
2960 { "res.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
2961 { "res.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2962 { "res.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2963
2964 { "res.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
2965 { "res.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2966 { "res.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
2967 { "res.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2968 { "res.hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV },
2969 { "res.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
2970 { "res.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_SINT, SMP_USE_HRSHV },
2971
2972 /* scook is valid only on the response and is used for ACL compatibility */
2973 { "scook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
2974 { "scook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2975 { "scook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2976 { "set-cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV }, /* deprecated */
2977
2978 /* shdr is valid only on the response and is used for ACL compatibility */
2979 { "shdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
2980 { "shdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2981 { "shdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV },
2982 { "shdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_SINT, SMP_USE_HRSHV },
2983
2984 { "status", smp_fetch_stcode, 0, NULL, SMP_T_SINT, SMP_USE_HRSHP },
2985 { "unique-id", smp_fetch_uniqueid, 0, NULL, SMP_T_STR, SMP_SRC_L4SRV },
2986 { "url", smp_fetch_url, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2987 { "url32", smp_fetch_url32, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
2988 { "url32+src", smp_fetch_url32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
2989 { "url_ip", smp_fetch_url_ip, 0, NULL, SMP_T_IPV4, SMP_USE_HRQHV },
2990 { "url_port", smp_fetch_url_port, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
2991 { "url_param", smp_fetch_url_param, ARG2(0,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
2992 { "urlp" , smp_fetch_url_param, ARG2(0,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
2993 { "urlp_val", smp_fetch_url_param_val, ARG2(0,STR,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2994 { /* END */ },
2995}};
2996
Willy Tarreau0108d902018-11-25 19:14:37 +01002997INITCALL1(STG_REGISTER, sample_register_fetches, &sample_fetch_keywords);
Willy Tarreau79e57332018-10-02 16:01:16 +02002998
2999/*
3000 * Local variables:
3001 * c-indent-level: 8
3002 * c-basic-offset: 8
3003 * End:
3004 */