blob: ba2ea04a1eaa91be5d1c06d5bf89f8a41907dfa7 [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
615 if (!smp->strm->unique_id) {
616 if ((smp->strm->unique_id = pool_alloc(pool_head_uniqueid)) == NULL)
617 return 0;
618 smp->strm->unique_id[0] = '\0';
Tim Duesterhusea23a5c2020-02-26 16:20:49 +0100619 build_logline(smp->strm, smp->strm->unique_id,
620 UNIQUEID_LEN, &smp->sess->fe->format_unique_id);
Willy Tarreau79e57332018-10-02 16:01:16 +0200621 }
Tim Duesterhusea23a5c2020-02-26 16:20:49 +0100622 smp->data.u.str.data = strlen(smp->strm->unique_id);
Willy Tarreau79e57332018-10-02 16:01:16 +0200623 smp->data.type = SMP_T_STR;
624 smp->data.u.str.area = smp->strm->unique_id;
625 smp->flags = SMP_F_CONST;
626 return 1;
627}
628
629/* Returns a string block containing all headers including the
Joseph Herlant942eea32018-11-15 13:57:22 -0800630 * empty line which separes headers from the body. This is useful
631 * for some headers analysis.
Willy Tarreau79e57332018-10-02 16:01:16 +0200632 */
633static int smp_fetch_hdrs(const struct arg *args, struct sample *smp, const char *kw, void *private)
634{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200635 struct channel *chn = SMP_REQ_CHN(smp);
Willy Tarreau79e57332018-10-02 16:01:16 +0200636 struct http_txn *txn;
637
Christopher Faulet46575cd2019-04-17 11:40:30 +0200638 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200639 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +0200640 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200641 struct buffer *temp;
642 int32_t pos;
Willy Tarreau79e57332018-10-02 16:01:16 +0200643
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200644 if (!htx)
645 return 0;
646 temp = get_trash_chunk();
Christopher Fauleta3f15502019-05-13 15:27:23 +0200647 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200648 struct htx_blk *blk = htx_get_blk(htx, pos);
649 enum htx_blk_type type = htx_get_blk_type(blk);
Willy Tarreau79e57332018-10-02 16:01:16 +0200650
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200651 if (type == HTX_BLK_HDR) {
652 struct ist n = htx_get_blk_name(htx, blk);
653 struct ist v = htx_get_blk_value(htx, blk);
654
Christopher Fauletc59ff232018-12-03 13:58:44 +0100655 if (!htx_hdr_to_h1(n, v, temp))
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200656 return 0;
657 }
658 else if (type == HTX_BLK_EOH) {
659 if (!chunk_memcat(temp, "\r\n", 2))
660 return 0;
661 break;
662 }
663 }
664 smp->data.type = SMP_T_STR;
665 smp->data.u.str = *temp;
666
667 }
668 else {
669 /* LEGACY version */
670 struct http_msg *msg;
671 struct hdr_idx *idx;
672
Christopher Faulet89dc4992019-04-17 12:02:59 +0200673 CHECK_HTTP_MESSAGE_FIRST(chn);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200674
675 txn = smp->strm->txn;
676 idx = &txn->hdr_idx;
677 msg = &txn->req;
Willy Tarreau79e57332018-10-02 16:01:16 +0200678
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200679 smp->data.type = SMP_T_STR;
Christopher Faulet89dc4992019-04-17 12:02:59 +0200680 smp->data.u.str.area = ci_head(chn) + hdr_idx_first_pos(idx);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200681 smp->data.u.str.data = msg->eoh - hdr_idx_first_pos(idx) + 1 +
Christopher Faulet89dc4992019-04-17 12:02:59 +0200682 (ci_head(chn)[msg->eoh] == '\r');
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200683 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200684 return 1;
685}
686
687/* Returns the header request in a length/value encoded format.
688 * This is useful for exchanges with the SPOE.
689 *
690 * A "length value" is a multibyte code encoding numbers. It uses the
691 * SPOE format. The encoding is the following:
692 *
693 * Each couple "header name" / "header value" is composed
694 * like this:
695 * "length value" "header name bytes"
696 * "length value" "header value bytes"
697 * When the last header is reached, the header name and the header
698 * value are empty. Their length are 0
699 */
700static int smp_fetch_hdrs_bin(const struct arg *args, struct sample *smp, const char *kw, void *private)
701{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200702 struct channel *chn = SMP_REQ_CHN(smp);
Willy Tarreau79e57332018-10-02 16:01:16 +0200703 struct http_txn *txn;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200704 struct buffer *temp;
Willy Tarreau79e57332018-10-02 16:01:16 +0200705
Christopher Faulet46575cd2019-04-17 11:40:30 +0200706 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200707 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +0200708 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200709 struct buffer *temp;
710 char *p, *end;
711 int32_t pos;
712 int ret;
Willy Tarreau79e57332018-10-02 16:01:16 +0200713
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200714 if (!htx)
715 return 0;
716 temp = get_trash_chunk();
717 p = temp->area;
718 end = temp->area + temp->size;
Christopher Fauleta3f15502019-05-13 15:27:23 +0200719 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200720 struct htx_blk *blk = htx_get_blk(htx, pos);
721 enum htx_blk_type type = htx_get_blk_type(blk);
722 struct ist n, v;
Willy Tarreau79e57332018-10-02 16:01:16 +0200723
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200724 if (type == HTX_BLK_HDR) {
725 n = htx_get_blk_name(htx,blk);
726 v = htx_get_blk_value(htx, blk);
Willy Tarreau79e57332018-10-02 16:01:16 +0200727
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200728 /* encode the header name. */
729 ret = encode_varint(n.len, &p, end);
730 if (ret == -1)
731 return 0;
732 if (p + n.len > end)
733 return 0;
734 memcpy(p, n.ptr, n.len);
735 p += n.len;
Willy Tarreau79e57332018-10-02 16:01:16 +0200736
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200737 /* encode the header value. */
738 ret = encode_varint(v.len, &p, end);
739 if (ret == -1)
740 return 0;
741 if (p + v.len > end)
742 return 0;
743 memcpy(p, v.ptr, v.len);
744 p += v.len;
Willy Tarreau79e57332018-10-02 16:01:16 +0200745
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200746 }
747 else if (type == HTX_BLK_EOH) {
748 /* encode the end of the header list with empty
749 * header name and header value.
750 */
751 ret = encode_varint(0, &p, end);
752 if (ret == -1)
753 return 0;
754 ret = encode_varint(0, &p, end);
755 if (ret == -1)
756 return 0;
757 break;
758 }
759 }
760
761 /* Initialise sample data which will be filled. */
762 smp->data.type = SMP_T_BIN;
763 smp->data.u.str.area = temp->area;
764 smp->data.u.str.data = p - temp->area;
765 smp->data.u.str.size = temp->size;
766 }
767 else {
768 /* LEGACY version */
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200769 struct hdr_idx *idx;
770 const char *cur_ptr, *cur_next, *p;
771 int old_idx, cur_idx;
772 struct hdr_idx_elem *cur_hdr;
773 const char *hn, *hv;
774 int hnl, hvl;
775 int ret;
776 char *buf;
777 char *end;
778
Christopher Faulet89dc4992019-04-17 12:02:59 +0200779 CHECK_HTTP_MESSAGE_FIRST(chn);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200780
781 temp = get_trash_chunk();
782 buf = temp->area;
783 end = temp->area + temp->size;
784
785 txn = smp->strm->txn;
786 idx = &txn->hdr_idx;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200787
788 /* Build array of headers. */
789 old_idx = 0;
Christopher Faulet89dc4992019-04-17 12:02:59 +0200790 cur_next = ci_head(chn) + hdr_idx_first_pos(idx);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200791 while (1) {
792 cur_idx = idx->v[old_idx].next;
793 if (!cur_idx)
794 break;
795 old_idx = cur_idx;
Willy Tarreau79e57332018-10-02 16:01:16 +0200796
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200797 cur_hdr = &idx->v[cur_idx];
798 cur_ptr = cur_next;
799 cur_next = cur_ptr + cur_hdr->len + cur_hdr->cr + 1;
800
801 /* Now we have one full header at cur_ptr of len cur_hdr->len,
802 * and the next header starts at cur_next. We'll check
803 * this header in the list as well as against the default
804 * rule.
805 */
806
807 /* look for ': *'. */
808 hn = cur_ptr;
809 for (p = cur_ptr; p < cur_ptr + cur_hdr->len && *p != ':'; p++);
810 if (p >= cur_ptr+cur_hdr->len)
811 continue;
812 hnl = p - hn;
Willy Tarreau79e57332018-10-02 16:01:16 +0200813 p++;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200814 while (p < cur_ptr + cur_hdr->len && (*p == ' ' || *p == '\t'))
815 p++;
816 if (p >= cur_ptr + cur_hdr->len)
817 continue;
818 hv = p;
819 hvl = cur_ptr + cur_hdr->len-p;
Willy Tarreau79e57332018-10-02 16:01:16 +0200820
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200821 /* encode the header name. */
822 ret = encode_varint(hnl, &buf, end);
823 if (ret == -1)
824 return 0;
825 if (buf + hnl > end)
826 return 0;
827 memcpy(buf, hn, hnl);
828 buf += hnl;
829
830 /* encode and copy the value. */
831 ret = encode_varint(hvl, &buf, end);
832 if (ret == -1)
833 return 0;
834 if (buf + hvl > end)
835 return 0;
836 memcpy(buf, hv, hvl);
837 buf += hvl;
838 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200839
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200840 /* encode the end of the header list with empty
841 * header name and header value.
842 */
843 ret = encode_varint(0, &buf, end);
Willy Tarreau79e57332018-10-02 16:01:16 +0200844 if (ret == -1)
845 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200846 ret = encode_varint(0, &buf, end);
847 if (ret == -1)
Willy Tarreau79e57332018-10-02 16:01:16 +0200848 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200849
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200850 /* Initialise sample data which will be filled. */
851 smp->data.type = SMP_T_BIN;
852 smp->data.u.str.area = temp->area;
853 smp->data.u.str.data = buf - temp->area;
854 smp->data.u.str.size = temp->size;
855 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200856 return 1;
857}
858
859/* returns the longest available part of the body. This requires that the body
860 * has been waited for using http-buffer-request.
861 */
862static int smp_fetch_body(const struct arg *args, struct sample *smp, const char *kw, void *private)
863{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200864 struct channel *chn = SMP_REQ_CHN(smp);
Willy Tarreau79e57332018-10-02 16:01:16 +0200865 struct buffer *temp;
866
Christopher Faulet46575cd2019-04-17 11:40:30 +0200867 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200868 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +0200869 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200870 int32_t pos;
Willy Tarreau79e57332018-10-02 16:01:16 +0200871
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200872 if (!htx)
873 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200874
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200875 temp = get_trash_chunk();
Christopher Fauleta3f15502019-05-13 15:27:23 +0200876 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200877 struct htx_blk *blk = htx_get_blk(htx, pos);
878 enum htx_blk_type type = htx_get_blk_type(blk);
Willy Tarreau79e57332018-10-02 16:01:16 +0200879
Christopher Faulet54b5e212019-06-04 10:08:28 +0200880 if (type == HTX_BLK_EOM || type == HTX_BLK_TLR || type == HTX_BLK_EOT)
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200881 break;
882 if (type == HTX_BLK_DATA) {
Christopher Fauletc59ff232018-12-03 13:58:44 +0100883 if (!htx_data_to_h1(htx_get_blk_value(htx, blk), temp, 0))
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200884 return 0;
885 }
886 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200887
Willy Tarreau79e57332018-10-02 16:01:16 +0200888 smp->data.type = SMP_T_BIN;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200889 smp->data.u.str = *temp;
890 smp->flags = SMP_F_VOL_TEST;
Willy Tarreau79e57332018-10-02 16:01:16 +0200891 }
892 else {
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200893 /* LEGACY version */
894 struct http_msg *msg;
895 unsigned long len;
896 unsigned long block1;
897 char *body;
898
Christopher Faulet89dc4992019-04-17 12:02:59 +0200899 CHECK_HTTP_MESSAGE_FIRST(chn);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200900
Christopher Faulet89dc4992019-04-17 12:02:59 +0200901 msg = &smp->strm->txn->req;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200902 len = http_body_bytes(msg);
Christopher Faulet89dc4992019-04-17 12:02:59 +0200903 body = c_ptr(chn, -http_data_rewind(msg));
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200904
905 block1 = len;
Christopher Faulet89dc4992019-04-17 12:02:59 +0200906 if (block1 > b_wrap(&chn->buf) - body)
907 block1 = b_wrap(&chn->buf) - body;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200908
909 if (block1 == len) {
910 /* buffer is not wrapped (or empty) */
911 smp->data.type = SMP_T_BIN;
912 smp->data.u.str.area = body;
913 smp->data.u.str.data = len;
914 smp->flags = SMP_F_VOL_TEST | SMP_F_CONST;
915 }
916 else {
917 /* buffer is wrapped, we need to defragment it */
918 temp = get_trash_chunk();
919 memcpy(temp->area, body, block1);
Christopher Faulet89dc4992019-04-17 12:02:59 +0200920 memcpy(temp->area + block1, b_orig(&chn->buf), len - block1);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200921 smp->data.type = SMP_T_BIN;
922 smp->data.u.str.area = temp->area;
923 smp->data.u.str.data = len;
924 smp->flags = SMP_F_VOL_TEST;
925 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200926 }
927 return 1;
928}
929
930
931/* returns the available length of the body. This requires that the body
932 * has been waited for using http-buffer-request.
933 */
934static int smp_fetch_body_len(const struct arg *args, struct sample *smp, const char *kw, void *private)
935{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200936 struct channel *chn = SMP_REQ_CHN(smp);
937
Christopher Faulet46575cd2019-04-17 11:40:30 +0200938 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200939 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +0200940 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Dragan Dosen5a606682019-02-14 12:30:53 +0100941 int32_t pos;
Christopher Fauletc16317d2018-12-12 14:11:22 +0100942 unsigned long long len = 0;
943
944 if (!htx)
945 return 0;
946
Christopher Fauleta3f15502019-05-13 15:27:23 +0200947 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Dragan Dosen5a606682019-02-14 12:30:53 +0100948 struct htx_blk *blk = htx_get_blk(htx, pos);
949 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Fauletc16317d2018-12-12 14:11:22 +0100950
Christopher Faulet54b5e212019-06-04 10:08:28 +0200951 if (type == HTX_BLK_EOM || type == HTX_BLK_TLR || type == HTX_BLK_EOT)
Christopher Fauletc16317d2018-12-12 14:11:22 +0100952 break;
Dragan Dosen5a606682019-02-14 12:30:53 +0100953 if (type == HTX_BLK_DATA)
954 len += htx_get_blksz(blk);
Christopher Fauletc16317d2018-12-12 14:11:22 +0100955 }
956
957 smp->data.type = SMP_T_SINT;
958 smp->data.u.sint = len;
959
960 smp->flags = SMP_F_VOL_TEST;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200961 }
962 else {
963 /* LEGACY version */
964 struct http_msg *msg;
Willy Tarreau79e57332018-10-02 16:01:16 +0200965
Christopher Faulet89dc4992019-04-17 12:02:59 +0200966 CHECK_HTTP_MESSAGE_FIRST(chn);
Willy Tarreau79e57332018-10-02 16:01:16 +0200967
Christopher Faulet89dc4992019-04-17 12:02:59 +0200968 msg = &smp->strm->txn->req;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200969 smp->data.type = SMP_T_SINT;
970 smp->data.u.sint = http_body_bytes(msg);
Willy Tarreau79e57332018-10-02 16:01:16 +0200971
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200972 smp->flags = SMP_F_VOL_TEST;
973 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200974 return 1;
975}
976
977
978/* returns the advertised length of the body, or the advertised size of the
979 * chunks available in the buffer. This requires that the body has been waited
980 * for using http-buffer-request.
981 */
982static int smp_fetch_body_size(const struct arg *args, struct sample *smp, const char *kw, void *private)
983{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200984 struct channel *chn = SMP_REQ_CHN(smp);
985
Christopher Faulet46575cd2019-04-17 11:40:30 +0200986 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200987 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +0200988 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Dragan Dosen5a606682019-02-14 12:30:53 +0100989 int32_t pos;
Christopher Fauletc16317d2018-12-12 14:11:22 +0100990 unsigned long long len = 0;
991
992 if (!htx)
993 return 0;
994
Christopher Fauleta3f15502019-05-13 15:27:23 +0200995 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Dragan Dosen5a606682019-02-14 12:30:53 +0100996 struct htx_blk *blk = htx_get_blk(htx, pos);
997 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Fauletc16317d2018-12-12 14:11:22 +0100998
Christopher Faulet54b5e212019-06-04 10:08:28 +0200999 if (type == HTX_BLK_EOM || type == HTX_BLK_TLR || type == HTX_BLK_EOT)
Christopher Fauletc16317d2018-12-12 14:11:22 +01001000 break;
Dragan Dosen5a606682019-02-14 12:30:53 +01001001 if (type == HTX_BLK_DATA)
1002 len += htx_get_blksz(blk);
Christopher Fauletc16317d2018-12-12 14:11:22 +01001003 }
1004 if (htx->extra != ULLONG_MAX)
1005 len += htx->extra;
1006
1007 smp->data.type = SMP_T_SINT;
1008 smp->data.u.sint = len;
1009
1010 smp->flags = SMP_F_VOL_TEST;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001011 }
1012 else {
1013 /* LEGACY version */
1014 struct http_msg *msg;
Willy Tarreau79e57332018-10-02 16:01:16 +02001015
Christopher Faulet89dc4992019-04-17 12:02:59 +02001016 CHECK_HTTP_MESSAGE_FIRST(chn);
Willy Tarreau79e57332018-10-02 16:01:16 +02001017
Christopher Faulet89dc4992019-04-17 12:02:59 +02001018 msg = &smp->strm->txn->req;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001019 smp->data.type = SMP_T_SINT;
1020 smp->data.u.sint = msg->body_len;
Willy Tarreau79e57332018-10-02 16:01:16 +02001021
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001022 smp->flags = SMP_F_VOL_TEST;
1023 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001024 return 1;
1025}
1026
1027
1028/* 4. Check on URL/URI. A pointer to the URI is stored. */
1029static int smp_fetch_url(const struct arg *args, struct sample *smp, const char *kw, void *private)
1030{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001031 struct channel *chn = SMP_REQ_CHN(smp);
Willy Tarreau79e57332018-10-02 16:01:16 +02001032 struct http_txn *txn;
1033
Christopher Faulet46575cd2019-04-17 11:40:30 +02001034 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001035 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +02001036 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001037 struct htx_sl *sl;
Willy Tarreau79e57332018-10-02 16:01:16 +02001038
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001039 if (!htx)
1040 return 0;
Christopher Faulet297fbb42019-05-13 14:41:27 +02001041 sl = http_get_stline(htx);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001042 smp->data.type = SMP_T_STR;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001043 smp->data.u.str.area = HTX_SL_REQ_UPTR(sl);
1044 smp->data.u.str.data = HTX_SL_REQ_ULEN(sl);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001045 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
1046 }
1047 else {
1048 /* LEGACY version */
Christopher Faulet89dc4992019-04-17 12:02:59 +02001049 CHECK_HTTP_MESSAGE_FIRST(chn);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001050
1051 txn = smp->strm->txn;
1052 smp->data.type = SMP_T_STR;
1053 smp->data.u.str.data = txn->req.sl.rq.u_l;
Christopher Faulet89dc4992019-04-17 12:02:59 +02001054 smp->data.u.str.area = ci_head(chn) + txn->req.sl.rq.u;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001055 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
1056 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001057 return 1;
1058}
1059
1060static int smp_fetch_url_ip(const struct arg *args, struct sample *smp, const char *kw, void *private)
1061{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001062 struct channel *chn = SMP_REQ_CHN(smp);
Willy Tarreau79e57332018-10-02 16:01:16 +02001063 struct http_txn *txn;
1064 struct sockaddr_storage addr;
1065
Christopher Faulet46575cd2019-04-17 11:40:30 +02001066 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001067 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +02001068 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001069 struct htx_sl *sl;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001070
1071 if (!htx)
1072 return 0;
Christopher Faulet297fbb42019-05-13 14:41:27 +02001073 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001074 url2sa(HTX_SL_REQ_UPTR(sl), HTX_SL_REQ_ULEN(sl), &addr, NULL);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001075 }
1076 else {
1077 /* LEGACY version */
Christopher Faulet89dc4992019-04-17 12:02:59 +02001078 CHECK_HTTP_MESSAGE_FIRST(chn);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001079
1080 txn = smp->strm->txn;
Christopher Faulet89dc4992019-04-17 12:02:59 +02001081 url2sa(ci_head(chn) + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &addr, NULL);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001082 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001083
Willy Tarreau79e57332018-10-02 16:01:16 +02001084 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
1085 return 0;
1086
1087 smp->data.type = SMP_T_IPV4;
1088 smp->data.u.ipv4 = ((struct sockaddr_in *)&addr)->sin_addr;
1089 smp->flags = 0;
1090 return 1;
1091}
1092
1093static int smp_fetch_url_port(const struct arg *args, struct sample *smp, const char *kw, void *private)
1094{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001095 struct channel *chn = SMP_REQ_CHN(smp);
Willy Tarreau79e57332018-10-02 16:01:16 +02001096 struct http_txn *txn;
1097 struct sockaddr_storage addr;
1098
Christopher Faulet46575cd2019-04-17 11:40:30 +02001099 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001100 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +02001101 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001102 struct htx_sl *sl;
Willy Tarreau79e57332018-10-02 16:01:16 +02001103
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001104 if (!htx)
1105 return 0;
Christopher Faulet297fbb42019-05-13 14:41:27 +02001106 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001107 url2sa(HTX_SL_REQ_UPTR(sl), HTX_SL_REQ_ULEN(sl), &addr, NULL);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001108 }
1109 else {
1110 /* LEGACY version */
Christopher Faulet89dc4992019-04-17 12:02:59 +02001111 CHECK_HTTP_MESSAGE_FIRST(chn);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001112
1113 txn = smp->strm->txn;
Christopher Faulet89dc4992019-04-17 12:02:59 +02001114 url2sa(ci_head(chn) + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &addr, NULL);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001115 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001116 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
1117 return 0;
1118
1119 smp->data.type = SMP_T_SINT;
1120 smp->data.u.sint = ntohs(((struct sockaddr_in *)&addr)->sin_port);
1121 smp->flags = 0;
1122 return 1;
1123}
1124
1125/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
1126 * Accepts an optional argument of type string containing the header field name,
1127 * and an optional argument of type signed or unsigned integer to request an
1128 * explicit occurrence of the header. Note that in the event of a missing name,
1129 * headers are considered from the first one. It does not stop on commas and
1130 * returns full lines instead (useful for User-Agent or Date for example).
1131 */
1132static int smp_fetch_fhdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
1133{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001134 /* possible keywords: req.fhdr, res.fhdr */
1135 struct channel *chn = ((kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Willy Tarreau79e57332018-10-02 16:01:16 +02001136 int occ = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001137
Christopher Faulet46575cd2019-04-17 11:40:30 +02001138 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001139 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +02001140 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001141 struct http_hdr_ctx *ctx = smp->ctx.a[0];
1142 struct ist name;
Willy Tarreau79e57332018-10-02 16:01:16 +02001143
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001144 if (!ctx) {
1145 /* first call */
1146 ctx = &static_http_hdr_ctx;
1147 ctx->blk = NULL;
1148 smp->ctx.a[0] = ctx;
1149 }
1150
1151 if (args) {
1152 if (args[0].type != ARGT_STR)
1153 return 0;
1154 name.ptr = args[0].data.str.area;
1155 name.len = args[0].data.str.data;
1156
1157 if (args[1].type == ARGT_SINT)
1158 occ = args[1].data.sint;
1159 }
1160
1161 if (!htx)
Willy Tarreau79e57332018-10-02 16:01:16 +02001162 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001163
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001164 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
1165 /* search for header from the beginning */
1166 ctx->blk = NULL;
1167
1168 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
1169 /* no explicit occurrence and single fetch => last header by default */
1170 occ = -1;
1171
1172 if (!occ)
1173 /* prepare to report multiple occurrences for ACL fetches */
1174 smp->flags |= SMP_F_NOT_LAST;
1175
1176 smp->data.type = SMP_T_STR;
1177 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
1178 if (http_get_htx_fhdr(htx, name, occ, ctx, &smp->data.u.str.area, &smp->data.u.str.data))
1179 return 1;
Willy Tarreau79e57332018-10-02 16:01:16 +02001180 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001181 else {
1182 /* LEGACY version */
1183 struct hdr_idx *idx;
1184 struct hdr_ctx *ctx = smp->ctx.a[0];
1185 const struct http_msg *msg;
1186 const char *name_str = NULL;
1187 int name_len = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001188
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001189 if (!ctx) {
1190 /* first call */
1191 ctx = &static_hdr_ctx;
1192 ctx->idx = 0;
1193 smp->ctx.a[0] = ctx;
1194 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001195
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001196 if (args) {
1197 if (args[0].type != ARGT_STR)
1198 return 0;
1199 name_str = args[0].data.str.area;
1200 name_len = args[0].data.str.data;
Willy Tarreau79e57332018-10-02 16:01:16 +02001201
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001202 if (args[1].type == ARGT_SINT)
1203 occ = args[1].data.sint;
1204 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001205
Christopher Faulet89dc4992019-04-17 12:02:59 +02001206 CHECK_HTTP_MESSAGE_FIRST(chn);
Willy Tarreau79e57332018-10-02 16:01:16 +02001207
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001208 idx = &smp->strm->txn->hdr_idx;
Christopher Faulet89dc4992019-04-17 12:02:59 +02001209 msg = (!(chn->flags & CF_ISRESP) ? &smp->strm->txn->req : &smp->strm->txn->rsp);
Willy Tarreau79e57332018-10-02 16:01:16 +02001210
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001211 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
1212 /* search for header from the beginning */
1213 ctx->idx = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001214
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001215 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
1216 /* no explicit occurrence and single fetch => last header by default */
1217 occ = -1;
1218
1219 if (!occ)
1220 /* prepare to report multiple occurrences for ACL fetches */
1221 smp->flags |= SMP_F_NOT_LAST;
1222
1223 smp->data.type = SMP_T_STR;
1224 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
1225 if (http_get_fhdr(msg, name_str, name_len, idx, occ, ctx, &smp->data.u.str.area, &smp->data.u.str.data))
1226 return 1;
1227 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001228 smp->flags &= ~SMP_F_NOT_LAST;
1229 return 0;
1230}
1231
1232/* 6. Check on HTTP header count. The number of occurrences is returned.
1233 * Accepts exactly 1 argument of type string. It does not stop on commas and
1234 * returns full lines instead (useful for User-Agent or Date for example).
1235 */
1236static int smp_fetch_fhdr_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
1237{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001238 /* possible keywords: req.fhdr_cnt, res.fhdr_cnt */
1239 struct channel *chn = ((kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Willy Tarreau79e57332018-10-02 16:01:16 +02001240 int cnt;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001241
Christopher Faulet46575cd2019-04-17 11:40:30 +02001242 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001243 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +02001244 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001245 struct http_hdr_ctx ctx;
1246 struct ist name;
1247
1248 if (!htx)
1249 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001250
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001251 if (args && args->type == ARGT_STR) {
1252 name.ptr = args->data.str.area;
1253 name.len = args->data.str.data;
Olivier Houcharde2c78cd2018-11-21 13:49:48 +01001254 } else {
1255 name.ptr = NULL;
1256 name.len = 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001257 }
1258
1259 ctx.blk = NULL;
1260 cnt = 0;
1261 while (http_find_header(htx, name, &ctx, 1))
1262 cnt++;
Willy Tarreau79e57332018-10-02 16:01:16 +02001263 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001264 else {
1265 /* LEGACY version */
1266 struct hdr_idx *idx;
1267 struct hdr_ctx ctx;
1268 const struct http_msg *msg;
1269 const char *name = NULL;
1270 int len = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001271
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001272 if (args && args->type == ARGT_STR) {
1273 name = args->data.str.area;
1274 len = args->data.str.data;
1275 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001276
Christopher Faulet89dc4992019-04-17 12:02:59 +02001277 CHECK_HTTP_MESSAGE_FIRST(chn);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001278
1279 idx = &smp->strm->txn->hdr_idx;
Christopher Faulet89dc4992019-04-17 12:02:59 +02001280 msg = (!(chn->flags & CF_ISRESP) ? &smp->strm->txn->req : &smp->strm->txn->rsp);
Willy Tarreau79e57332018-10-02 16:01:16 +02001281
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001282 ctx.idx = 0;
1283 cnt = 0;
1284 while (http_find_full_header2(name, len, ci_head(msg->chn), idx, &ctx))
1285 cnt++;
1286 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001287
1288 smp->data.type = SMP_T_SINT;
1289 smp->data.u.sint = cnt;
1290 smp->flags = SMP_F_VOL_HDR;
1291 return 1;
1292}
1293
1294static int smp_fetch_hdr_names(const struct arg *args, struct sample *smp, const char *kw, void *private)
1295{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001296 /* possible keywords: req.hdr_names, res.hdr_names */
1297 struct channel *chn = ((kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Willy Tarreau79e57332018-10-02 16:01:16 +02001298 struct buffer *temp;
1299 char del = ',';
1300
Christopher Faulet46575cd2019-04-17 11:40:30 +02001301 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001302 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +02001303 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001304 int32_t pos;
Willy Tarreau79e57332018-10-02 16:01:16 +02001305
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001306 if (!htx)
1307 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001308
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001309 if (args && args->type == ARGT_STR)
1310 del = *args[0].data.str.area;
Willy Tarreau79e57332018-10-02 16:01:16 +02001311
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001312 temp = get_trash_chunk();
Christopher Fauleta3f15502019-05-13 15:27:23 +02001313 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001314 struct htx_blk *blk = htx_get_blk(htx, pos);
1315 enum htx_blk_type type = htx_get_blk_type(blk);
1316 struct ist n;
Willy Tarreau79e57332018-10-02 16:01:16 +02001317
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001318 if (type == HTX_BLK_EOH)
1319 break;
1320 if (type != HTX_BLK_HDR)
1321 continue;
1322 n = htx_get_blk_name(htx, blk);
1323
1324 if (temp->data)
1325 temp->area[temp->data++] = del;
1326 chunk_memcat(temp, n.ptr, n.len);
1327 }
1328 }
1329 else {
1330 /* LEGACY version */
1331 struct hdr_idx *idx;
1332 struct hdr_ctx ctx;
1333 const struct http_msg *msg;
1334
1335 if (args && args->type == ARGT_STR)
1336 del = *args[0].data.str.area;
1337
Christopher Faulet89dc4992019-04-17 12:02:59 +02001338 CHECK_HTTP_MESSAGE_FIRST(chn);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001339
1340 idx = &smp->strm->txn->hdr_idx;
Christopher Faulet89dc4992019-04-17 12:02:59 +02001341 msg = (!(chn->flags & CF_ISRESP) ? &smp->strm->txn->req : &smp->strm->txn->rsp);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001342
1343 temp = get_trash_chunk();
1344
1345 ctx.idx = 0;
1346 while (http_find_next_header(ci_head(msg->chn), idx, &ctx)) {
1347 if (temp->data)
1348 temp->area[temp->data++] = del;
1349 memcpy(temp->area + temp->data, ctx.line, ctx.del);
1350 temp->data += ctx.del;
1351 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001352 }
1353
1354 smp->data.type = SMP_T_STR;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001355 smp->data.u.str = *temp;
Willy Tarreau79e57332018-10-02 16:01:16 +02001356 smp->flags = SMP_F_VOL_HDR;
1357 return 1;
1358}
1359
1360/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
1361 * Accepts an optional argument of type string containing the header field name,
1362 * and an optional argument of type signed or unsigned integer to request an
1363 * explicit occurrence of the header. Note that in the event of a missing name,
1364 * headers are considered from the first one.
1365 */
1366static int smp_fetch_hdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
1367{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001368 /* possible keywords: req.hdr / hdr, res.hdr / shdr */
1369 struct channel *chn = ((kw[0] == 'h' || kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Willy Tarreau79e57332018-10-02 16:01:16 +02001370 int occ = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001371
Christopher Faulet46575cd2019-04-17 11:40:30 +02001372 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001373 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +02001374 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001375 struct http_hdr_ctx *ctx = smp->ctx.a[0];
1376 struct ist name;
1377
1378 if (!ctx) {
1379 /* first call */
1380 ctx = &static_http_hdr_ctx;
1381 ctx->blk = NULL;
1382 smp->ctx.a[0] = ctx;
1383 }
1384
1385 if (args) {
1386 if (args[0].type != ARGT_STR)
1387 return 0;
1388 name.ptr = args[0].data.str.area;
1389 name.len = args[0].data.str.data;
1390
1391 if (args[1].type == ARGT_SINT)
1392 occ = args[1].data.sint;
1393 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001394
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001395 if (!htx)
Willy Tarreau79e57332018-10-02 16:01:16 +02001396 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001397
1398 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
1399 /* search for header from the beginning */
1400 ctx->blk = NULL;
1401
1402 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
1403 /* no explicit occurrence and single fetch => last header by default */
1404 occ = -1;
1405
1406 if (!occ)
1407 /* prepare to report multiple occurrences for ACL fetches */
1408 smp->flags |= SMP_F_NOT_LAST;
Willy Tarreau79e57332018-10-02 16:01:16 +02001409
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001410 smp->data.type = SMP_T_STR;
1411 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
1412 if (http_get_htx_hdr(htx, name, occ, ctx, &smp->data.u.str.area, &smp->data.u.str.data))
1413 return 1;
Willy Tarreau79e57332018-10-02 16:01:16 +02001414 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001415 else {
1416 /* LEGACY version */
1417 struct hdr_idx *idx;
1418 struct hdr_ctx *ctx = smp->ctx.a[0];
1419 const struct http_msg *msg;
1420 const char *name_str = NULL;
1421 int name_len = 0;
1422
1423 if (!ctx) {
1424 /* first call */
1425 ctx = &static_hdr_ctx;
1426 ctx->idx = 0;
1427 smp->ctx.a[0] = ctx;
1428 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001429
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001430 if (args) {
1431 if (args[0].type != ARGT_STR)
1432 return 0;
1433 name_str = args[0].data.str.area;
1434 name_len = args[0].data.str.data;
Willy Tarreau79e57332018-10-02 16:01:16 +02001435
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001436 if (args[1].type == ARGT_SINT)
1437 occ = args[1].data.sint;
1438 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001439
Christopher Faulet89dc4992019-04-17 12:02:59 +02001440 CHECK_HTTP_MESSAGE_FIRST(chn);
Willy Tarreau79e57332018-10-02 16:01:16 +02001441
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001442 idx = &smp->strm->txn->hdr_idx;
Christopher Faulet89dc4992019-04-17 12:02:59 +02001443 msg = (!(chn->flags & CF_ISRESP) ? &smp->strm->txn->req : &smp->strm->txn->rsp);
Willy Tarreau79e57332018-10-02 16:01:16 +02001444
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001445 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
1446 /* search for header from the beginning */
1447 ctx->idx = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001448
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001449 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
1450 /* no explicit occurrence and single fetch => last header by default */
1451 occ = -1;
1452
1453 if (!occ)
1454 /* prepare to report multiple occurrences for ACL fetches */
1455 smp->flags |= SMP_F_NOT_LAST;
1456
1457 smp->data.type = SMP_T_STR;
1458 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
1459 if (http_get_hdr(msg, name_str, name_len, idx, occ, ctx, &smp->data.u.str.area, &smp->data.u.str.data))
1460 return 1;
1461 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001462
1463 smp->flags &= ~SMP_F_NOT_LAST;
1464 return 0;
1465}
1466
Christopher Fauletc1f40dd2019-05-16 10:07:30 +02001467/* Same than smp_fetch_hdr() but only relies on the sample direction to choose
1468 * the right channel. So instead of duplicating the code, we just change the
1469 * keyword and then fallback on smp_fetch_hdr().
1470 */
1471static int smp_fetch_chn_hdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
1472{
1473 kw = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ ? "req.hdr" : "res.hdr");
1474 return smp_fetch_hdr(args, smp, kw, private);
1475}
1476
Willy Tarreau79e57332018-10-02 16:01:16 +02001477/* 6. Check on HTTP header count. The number of occurrences is returned.
1478 * Accepts exactly 1 argument of type string.
1479 */
1480static int smp_fetch_hdr_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
1481{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001482 /* possible keywords: req.hdr_cnt / hdr_cnt, res.hdr_cnt / shdr_cnt */
1483 struct channel *chn = ((kw[0] == 'h' || kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Willy Tarreau79e57332018-10-02 16:01:16 +02001484 int cnt;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001485
Christopher Faulet46575cd2019-04-17 11:40:30 +02001486 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001487 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +02001488 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001489 struct http_hdr_ctx ctx;
1490 struct ist name;
1491
1492 if (!htx)
1493 return 0;
1494
1495 if (args && args->type == ARGT_STR) {
1496 name.ptr = args->data.str.area;
1497 name.len = args->data.str.data;
Olivier Houcharde2c78cd2018-11-21 13:49:48 +01001498 } else {
1499 name.ptr = NULL;
1500 name.len = 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001501 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001502
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001503 ctx.blk = NULL;
1504 cnt = 0;
1505 while (http_find_header(htx, name, &ctx, 0))
1506 cnt++;
Willy Tarreau79e57332018-10-02 16:01:16 +02001507 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001508 else {
1509 /* LEGACY version */
1510 struct hdr_idx *idx;
1511 struct hdr_ctx ctx;
1512 const struct http_msg *msg;
1513 const char *name = NULL;
1514 int len = 0;
1515
1516 if (args && args->type == ARGT_STR) {
1517 name = args->data.str.area;
1518 len = args->data.str.data;
1519 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001520
Christopher Faulet89dc4992019-04-17 12:02:59 +02001521 CHECK_HTTP_MESSAGE_FIRST(chn);
Willy Tarreau79e57332018-10-02 16:01:16 +02001522
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001523 idx = &smp->strm->txn->hdr_idx;
Christopher Faulet89dc4992019-04-17 12:02:59 +02001524 msg = (!(chn->flags & CF_ISRESP) ? &smp->strm->txn->req : &smp->strm->txn->rsp);
Willy Tarreau79e57332018-10-02 16:01:16 +02001525
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001526 ctx.idx = 0;
1527 cnt = 0;
1528 while (http_find_header2(name, len, ci_head(msg->chn), idx, &ctx))
1529 cnt++;
1530 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001531
1532 smp->data.type = SMP_T_SINT;
1533 smp->data.u.sint = cnt;
1534 smp->flags = SMP_F_VOL_HDR;
1535 return 1;
1536}
1537
1538/* Fetch an HTTP header's integer value. The integer value is returned. It
1539 * takes a mandatory argument of type string and an optional one of type int
1540 * to designate a specific occurrence. It returns an unsigned integer, which
1541 * may or may not be appropriate for everything.
1542 */
1543static int smp_fetch_hdr_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
1544{
1545 int ret = smp_fetch_hdr(args, smp, kw, private);
1546
1547 if (ret > 0) {
1548 smp->data.type = SMP_T_SINT;
1549 smp->data.u.sint = strl2ic(smp->data.u.str.area,
1550 smp->data.u.str.data);
1551 }
1552
1553 return ret;
1554}
1555
1556/* Fetch an HTTP header's IP value. takes a mandatory argument of type string
1557 * and an optional one of type int to designate a specific occurrence.
1558 * It returns an IPv4 or IPv6 address.
1559 */
1560static int smp_fetch_hdr_ip(const struct arg *args, struct sample *smp, const char *kw, void *private)
1561{
1562 int ret;
1563
1564 while ((ret = smp_fetch_hdr(args, smp, kw, private)) > 0) {
1565 if (url2ipv4((char *) smp->data.u.str.area, &smp->data.u.ipv4)) {
1566 smp->data.type = SMP_T_IPV4;
1567 break;
1568 } else {
1569 struct buffer *temp = get_trash_chunk();
1570 if (smp->data.u.str.data < temp->size - 1) {
1571 memcpy(temp->area, smp->data.u.str.area,
1572 smp->data.u.str.data);
1573 temp->area[smp->data.u.str.data] = '\0';
1574 if (inet_pton(AF_INET6, temp->area, &smp->data.u.ipv6)) {
1575 smp->data.type = SMP_T_IPV6;
1576 break;
1577 }
1578 }
1579 }
1580
1581 /* if the header doesn't match an IP address, fetch next one */
1582 if (!(smp->flags & SMP_F_NOT_LAST))
1583 return 0;
1584 }
1585 return ret;
1586}
1587
1588/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
1589 * the first '/' after the possible hostname, and ends before the possible '?'.
1590 */
1591static int smp_fetch_path(const struct arg *args, struct sample *smp, const char *kw, void *private)
1592{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001593 struct channel *chn = SMP_REQ_CHN(smp);
1594
Christopher Faulet46575cd2019-04-17 11:40:30 +02001595 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001596 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +02001597 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001598 struct htx_sl *sl;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001599 struct ist path;
1600 size_t len;
Willy Tarreau79e57332018-10-02 16:01:16 +02001601
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001602 if (!htx)
1603 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001604
Christopher Faulet297fbb42019-05-13 14:41:27 +02001605 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001606 path = http_get_path(htx_sl_req_uri(sl));
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001607 if (!path.ptr)
1608 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001609
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001610 for (len = 0; len < path.len && *(path.ptr + len) != '?'; len++)
Dragan Dosen8861e1c2019-02-12 19:50:31 +01001611 ;
Willy Tarreau79e57332018-10-02 16:01:16 +02001612
Dragan Dosen8861e1c2019-02-12 19:50:31 +01001613 /* OK, we got the '/' ! */
1614 smp->data.type = SMP_T_STR;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001615 smp->data.u.str.area = path.ptr;
1616 smp->data.u.str.data = len;
1617 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
1618 }
1619 else {
1620 struct http_txn *txn;
1621 char *ptr, *end;
Willy Tarreau79e57332018-10-02 16:01:16 +02001622
Christopher Faulet89dc4992019-04-17 12:02:59 +02001623 CHECK_HTTP_MESSAGE_FIRST(chn);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001624
1625 txn = smp->strm->txn;
Christopher Faulet89dc4992019-04-17 12:02:59 +02001626 end = ci_head(chn) + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001627 ptr = http_txn_get_path(txn);
1628 if (!ptr)
1629 return 0;
1630
1631 /* OK, we got the '/' ! */
1632 smp->data.type = SMP_T_STR;
1633 smp->data.u.str.area = ptr;
1634
1635 while (ptr < end && *ptr != '?')
1636 ptr++;
1637
1638 smp->data.u.str.data = ptr - smp->data.u.str.area;
1639 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
1640 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001641 return 1;
1642}
1643
1644/* This produces a concatenation of the first occurrence of the Host header
1645 * followed by the path component if it begins with a slash ('/'). This means
1646 * that '*' will not be added, resulting in exactly the first Host entry.
1647 * If no Host header is found, then the path is returned as-is. The returned
1648 * value is stored in the trash so it does not need to be marked constant.
1649 * The returned sample is of type string.
1650 */
1651static int smp_fetch_base(const struct arg *args, struct sample *smp, const char *kw, void *private)
1652{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001653 struct channel *chn = SMP_REQ_CHN(smp);
Willy Tarreau79e57332018-10-02 16:01:16 +02001654 struct buffer *temp;
1655
Christopher Faulet46575cd2019-04-17 11:40:30 +02001656 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001657 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +02001658 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001659 struct htx_sl *sl;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001660 struct http_hdr_ctx ctx;
1661 struct ist path;
Willy Tarreau79e57332018-10-02 16:01:16 +02001662
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001663 if (!htx)
1664 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001665
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001666 ctx.blk = NULL;
1667 if (!http_find_header(htx, ist("Host"), &ctx, 0) || !ctx.value.len)
1668 return smp_fetch_path(args, smp, kw, private);
Willy Tarreau79e57332018-10-02 16:01:16 +02001669
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001670 /* OK we have the header value in ctx.value */
1671 temp = get_trash_chunk();
1672 chunk_memcat(temp, ctx.value.ptr, ctx.value.len);
1673
1674 /* now retrieve the path */
Christopher Faulet297fbb42019-05-13 14:41:27 +02001675 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001676 path = http_get_path(htx_sl_req_uri(sl));
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001677 if (path.ptr) {
1678 size_t len;
1679
Dragan Dosen8861e1c2019-02-12 19:50:31 +01001680 for (len = 0; len < path.len && *(path.ptr + len) != '?'; len++)
1681 ;
1682
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001683 if (len && *(path.ptr) == '/')
1684 chunk_memcat(temp, path.ptr, len);
1685 }
1686
1687 smp->data.type = SMP_T_STR;
1688 smp->data.u.str = *temp;
1689 }
1690 else {
1691 /* LEGACY version */
1692 struct http_txn *txn;
1693 char *ptr, *end, *beg;
1694 struct hdr_ctx ctx;
1695
Christopher Faulet89dc4992019-04-17 12:02:59 +02001696 CHECK_HTTP_MESSAGE_FIRST(chn);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001697
1698 txn = smp->strm->txn;
1699 ctx.idx = 0;
Christopher Faulet89dc4992019-04-17 12:02:59 +02001700 if (!http_find_header2("Host", 4, ci_head(chn), &txn->hdr_idx, &ctx) || !ctx.vlen)
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001701 return smp_fetch_path(args, smp, kw, private);
1702
1703 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
1704 temp = get_trash_chunk();
1705 memcpy(temp->area, ctx.line + ctx.val, ctx.vlen);
1706 smp->data.type = SMP_T_STR;
1707 smp->data.u.str.area = temp->area;
1708 smp->data.u.str.data = ctx.vlen;
Willy Tarreau79e57332018-10-02 16:01:16 +02001709
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001710 /* now retrieve the path */
Christopher Faulet89dc4992019-04-17 12:02:59 +02001711 end = ci_head(chn) + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001712 beg = http_txn_get_path(txn);
1713 if (!beg)
1714 beg = end;
Willy Tarreau79e57332018-10-02 16:01:16 +02001715
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001716 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
1717
1718 if (beg < ptr && *beg == '/') {
1719 memcpy(smp->data.u.str.area + smp->data.u.str.data, beg,
1720 ptr - beg);
1721 smp->data.u.str.data += ptr - beg;
1722 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001723 }
1724
1725 smp->flags = SMP_F_VOL_1ST;
1726 return 1;
1727}
1728
1729/* This produces a 32-bit hash of the concatenation of the first occurrence of
1730 * the Host header followed by the path component if it begins with a slash ('/').
1731 * This means that '*' will not be added, resulting in exactly the first Host
1732 * entry. If no Host header is found, then the path is used. The resulting value
1733 * is hashed using the path hash followed by a full avalanche hash and provides a
1734 * 32-bit integer value. This fetch is useful for tracking per-path activity on
1735 * high-traffic sites without having to store whole paths.
1736 */
1737static int smp_fetch_base32(const struct arg *args, struct sample *smp, const char *kw, void *private)
1738{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001739 struct channel *chn = SMP_REQ_CHN(smp);
Willy Tarreau79e57332018-10-02 16:01:16 +02001740 unsigned int hash = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001741
Christopher Faulet46575cd2019-04-17 11:40:30 +02001742 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001743 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +02001744 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001745 struct htx_sl *sl;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001746 struct http_hdr_ctx ctx;
1747 struct ist path;
Willy Tarreau79e57332018-10-02 16:01:16 +02001748
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001749 if (!htx)
1750 return 0;
1751
1752 ctx.blk = NULL;
Dragan Dosen8861e1c2019-02-12 19:50:31 +01001753 if (http_find_header(htx, ist("Host"), &ctx, 0)) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001754 /* OK we have the header value in ctx.value */
1755 while (ctx.value.len--)
1756 hash = *(ctx.value.ptr++) + (hash << 6) + (hash << 16) - hash;
1757 }
1758
1759 /* now retrieve the path */
Christopher Faulet297fbb42019-05-13 14:41:27 +02001760 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001761 path = http_get_path(htx_sl_req_uri(sl));
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001762 if (path.ptr) {
1763 size_t len;
1764
Dragan Dosen8861e1c2019-02-12 19:50:31 +01001765 for (len = 0; len < path.len && *(path.ptr + len) != '?'; len++)
1766 ;
1767
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001768 if (len && *(path.ptr) == '/') {
1769 while (len--)
1770 hash = *(path.ptr++) + (hash << 6) + (hash << 16) - hash;
1771 }
1772 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001773 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001774 else {
1775 /* LEGACY version */
1776 struct http_txn *txn;
1777 struct hdr_ctx ctx;
1778 char *ptr, *beg, *end;
1779 int len;
1780
Christopher Faulet89dc4992019-04-17 12:02:59 +02001781 CHECK_HTTP_MESSAGE_FIRST(chn);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001782
1783 txn = smp->strm->txn;
1784 ctx.idx = 0;
Christopher Faulet89dc4992019-04-17 12:02:59 +02001785 if (http_find_header2("Host", 4, ci_head(chn), &txn->hdr_idx, &ctx)) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001786 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
1787 ptr = ctx.line + ctx.val;
1788 len = ctx.vlen;
1789 while (len--)
1790 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
1791 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001792
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001793 /* now retrieve the path */
Christopher Faulet89dc4992019-04-17 12:02:59 +02001794 end = ci_head(chn) + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001795 beg = http_txn_get_path(txn);
1796 if (!beg)
1797 beg = end;
Willy Tarreau79e57332018-10-02 16:01:16 +02001798
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001799 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
Willy Tarreau79e57332018-10-02 16:01:16 +02001800
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001801 if (beg < ptr && *beg == '/') {
1802 while (beg < ptr)
1803 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
1804 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001805 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001806
Willy Tarreau79e57332018-10-02 16:01:16 +02001807 hash = full_hash(hash);
1808
1809 smp->data.type = SMP_T_SINT;
1810 smp->data.u.sint = hash;
1811 smp->flags = SMP_F_VOL_1ST;
1812 return 1;
1813}
1814
1815/* This concatenates the source address with the 32-bit hash of the Host and
1816 * path as returned by smp_fetch_base32(). The idea is to have per-source and
1817 * per-path counters. The result is a binary block from 8 to 20 bytes depending
1818 * on the source address length. The path hash is stored before the address so
1819 * that in environments where IPv6 is insignificant, truncating the output to
1820 * 8 bytes would still work.
1821 */
1822static int smp_fetch_base32_src(const struct arg *args, struct sample *smp, const char *kw, void *private)
1823{
1824 struct buffer *temp;
1825 struct connection *cli_conn = objt_conn(smp->sess->origin);
1826
1827 if (!cli_conn)
1828 return 0;
1829
1830 if (!smp_fetch_base32(args, smp, kw, private))
1831 return 0;
1832
1833 temp = get_trash_chunk();
1834 *(unsigned int *) temp->area = htonl(smp->data.u.sint);
1835 temp->data += sizeof(unsigned int);
1836
1837 switch (cli_conn->addr.from.ss_family) {
1838 case AF_INET:
1839 memcpy(temp->area + temp->data,
1840 &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr,
1841 4);
1842 temp->data += 4;
1843 break;
1844 case AF_INET6:
1845 memcpy(temp->area + temp->data,
1846 &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr,
1847 16);
1848 temp->data += 16;
1849 break;
1850 default:
1851 return 0;
1852 }
1853
1854 smp->data.u.str = *temp;
1855 smp->data.type = SMP_T_BIN;
1856 return 1;
1857}
1858
1859/* Extracts the query string, which comes after the question mark '?'. If no
1860 * question mark is found, nothing is returned. Otherwise it returns a sample
1861 * of type string carrying the whole query string.
1862 */
1863static int smp_fetch_query(const struct arg *args, struct sample *smp, const char *kw, void *private)
1864{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001865 struct channel *chn = SMP_REQ_CHN(smp);
Willy Tarreau79e57332018-10-02 16:01:16 +02001866 char *ptr, *end;
1867
Christopher Faulet46575cd2019-04-17 11:40:30 +02001868 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001869 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +02001870 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001871 struct htx_sl *sl;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001872
1873 if (!htx)
1874 return 0;
1875
Christopher Faulet297fbb42019-05-13 14:41:27 +02001876 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001877 ptr = HTX_SL_REQ_UPTR(sl);
1878 end = HTX_SL_REQ_UPTR(sl) + HTX_SL_REQ_ULEN(sl);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001879 }
1880 else {
1881 /* LEGACY version */
1882 struct http_txn *txn;
1883
Christopher Faulet89dc4992019-04-17 12:02:59 +02001884 CHECK_HTTP_MESSAGE_FIRST(chn);
Willy Tarreau79e57332018-10-02 16:01:16 +02001885
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001886 txn = smp->strm->txn;
Christopher Faulet89dc4992019-04-17 12:02:59 +02001887 ptr = ci_head(chn) + txn->req.sl.rq.u;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001888 end = ptr + txn->req.sl.rq.u_l;
1889 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001890
1891 /* look up the '?' */
1892 do {
1893 if (ptr == end)
1894 return 0;
1895 } while (*ptr++ != '?');
1896
1897 smp->data.type = SMP_T_STR;
1898 smp->data.u.str.area = ptr;
1899 smp->data.u.str.data = end - ptr;
1900 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
1901 return 1;
1902}
1903
1904static int smp_fetch_proto_http(const struct arg *args, struct sample *smp, const char *kw, void *private)
1905{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001906 struct channel *chn = SMP_REQ_CHN(smp);
1907
Christopher Faulet46575cd2019-04-17 11:40:30 +02001908 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001909 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +02001910 struct htx *htx = smp_prefetch_htx(smp, chn, 0);
Willy Tarreau79e57332018-10-02 16:01:16 +02001911
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001912 if (!htx)
1913 return 0;
1914 }
1915 else {
1916 /* LEGACY version */
Willy Tarreau79e57332018-10-02 16:01:16 +02001917
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001918 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
1919 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
1920 */
Christopher Faulet89dc4992019-04-17 12:02:59 +02001921 CHECK_HTTP_MESSAGE_FIRST_PERM(chn);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001922 }
1923 smp->data.type = SMP_T_BOOL;
Willy Tarreau79e57332018-10-02 16:01:16 +02001924 smp->data.u.sint = 1;
1925 return 1;
1926}
1927
1928/* return a valid test if the current request is the first one on the connection */
1929static int smp_fetch_http_first_req(const struct arg *args, struct sample *smp, const char *kw, void *private)
1930{
1931 smp->data.type = SMP_T_BOOL;
1932 smp->data.u.sint = !(smp->strm->txn->flags & TX_NOT_FIRST);
1933 return 1;
1934}
1935
1936/* Accepts exactly 1 argument of type userlist */
1937static int smp_fetch_http_auth(const struct arg *args, struct sample *smp, const char *kw, void *private)
1938{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001939 struct channel *chn = SMP_REQ_CHN(smp);
Willy Tarreau79e57332018-10-02 16:01:16 +02001940
1941 if (!args || args->type != ARGT_USR)
1942 return 0;
1943
Christopher Faulet46575cd2019-04-17 11:40:30 +02001944 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001945 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +02001946 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Willy Tarreau79e57332018-10-02 16:01:16 +02001947
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001948 if (!htx)
1949 return 0;
Christopher Faulete98411b2019-07-15 13:58:29 +02001950 if (!get_http_auth(smp, htx))
1951 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001952 }
1953 else {
1954 /* LEGACY version */
Christopher Faulet89dc4992019-04-17 12:02:59 +02001955 CHECK_HTTP_MESSAGE_FIRST(chn);
Christopher Faulete98411b2019-07-15 13:58:29 +02001956 if (!get_http_auth(smp, NULL))
1957 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001958 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001959
1960 smp->data.type = SMP_T_BOOL;
1961 smp->data.u.sint = check_user(args->data.usr, smp->strm->txn->auth.user,
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001962 smp->strm->txn->auth.pass);
Willy Tarreau79e57332018-10-02 16:01:16 +02001963 return 1;
1964}
1965
1966/* Accepts exactly 1 argument of type userlist */
1967static int smp_fetch_http_auth_grp(const struct arg *args, struct sample *smp, const char *kw, void *private)
1968{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001969 struct channel *chn = SMP_REQ_CHN(smp);
1970
Willy Tarreau79e57332018-10-02 16:01:16 +02001971 if (!args || args->type != ARGT_USR)
1972 return 0;
1973
Christopher Faulet46575cd2019-04-17 11:40:30 +02001974 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001975 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +02001976 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001977
1978 if (!htx)
1979 return 0;
Christopher Faulete98411b2019-07-15 13:58:29 +02001980 if (!get_http_auth(smp, htx))
1981 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001982 }
1983 else {
1984 /* LEGACY version */
Christopher Faulet89dc4992019-04-17 12:02:59 +02001985 CHECK_HTTP_MESSAGE_FIRST(chn);
Christopher Faulete98411b2019-07-15 13:58:29 +02001986 if (!get_http_auth(smp, NULL))
1987 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001988 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001989
Willy Tarreau79e57332018-10-02 16:01:16 +02001990 /* if the user does not belong to the userlist or has a wrong password,
1991 * report that it unconditionally does not match. Otherwise we return
1992 * a string containing the username.
1993 */
1994 if (!check_user(args->data.usr, smp->strm->txn->auth.user,
1995 smp->strm->txn->auth.pass))
1996 return 0;
1997
1998 /* pat_match_auth() will need the user list */
1999 smp->ctx.a[0] = args->data.usr;
2000
2001 smp->data.type = SMP_T_STR;
2002 smp->flags = SMP_F_CONST;
2003 smp->data.u.str.area = smp->strm->txn->auth.user;
2004 smp->data.u.str.data = strlen(smp->strm->txn->auth.user);
2005
2006 return 1;
2007}
2008
2009/* Fetch a captured HTTP request header. The index is the position of
2010 * the "capture" option in the configuration file
2011 */
2012static int smp_fetch_capture_req_hdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
2013{
Willy Tarreau79ed4672020-04-29 11:44:54 +02002014 struct proxy *fe;
Willy Tarreau79e57332018-10-02 16:01:16 +02002015 int idx;
2016
2017 if (!args || args->type != ARGT_SINT)
2018 return 0;
2019
Willy Tarreau79ed4672020-04-29 11:44:54 +02002020 if (!smp->strm)
2021 return 0;
2022
2023 fe = strm_fe(smp->strm);
Willy Tarreau79e57332018-10-02 16:01:16 +02002024 idx = args->data.sint;
2025
2026 if (idx > (fe->nb_req_cap - 1) || smp->strm->req_cap == NULL || smp->strm->req_cap[idx] == NULL)
2027 return 0;
2028
2029 smp->data.type = SMP_T_STR;
2030 smp->flags |= SMP_F_CONST;
2031 smp->data.u.str.area = smp->strm->req_cap[idx];
2032 smp->data.u.str.data = strlen(smp->strm->req_cap[idx]);
2033
2034 return 1;
2035}
2036
2037/* Fetch a captured HTTP response header. The index is the position of
2038 * the "capture" option in the configuration file
2039 */
2040static int smp_fetch_capture_res_hdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
2041{
Willy Tarreau79ed4672020-04-29 11:44:54 +02002042 struct proxy *fe;
Willy Tarreau79e57332018-10-02 16:01:16 +02002043 int idx;
2044
2045 if (!args || args->type != ARGT_SINT)
2046 return 0;
2047
Willy Tarreau79ed4672020-04-29 11:44:54 +02002048 if (!smp->strm)
2049 return 0;
2050
2051 fe = strm_fe(smp->strm);
Willy Tarreau79e57332018-10-02 16:01:16 +02002052 idx = args->data.sint;
2053
2054 if (idx > (fe->nb_rsp_cap - 1) || smp->strm->res_cap == NULL || smp->strm->res_cap[idx] == NULL)
2055 return 0;
2056
2057 smp->data.type = SMP_T_STR;
2058 smp->flags |= SMP_F_CONST;
2059 smp->data.u.str.area = smp->strm->res_cap[idx];
2060 smp->data.u.str.data = strlen(smp->strm->res_cap[idx]);
2061
2062 return 1;
2063}
2064
2065/* Extracts the METHOD in the HTTP request, the txn->uri should be filled before the call */
2066static int smp_fetch_capture_req_method(const struct arg *args, struct sample *smp, const char *kw, void *private)
2067{
2068 struct buffer *temp;
Willy Tarreau79ed4672020-04-29 11:44:54 +02002069 struct http_txn *txn;
Willy Tarreau79e57332018-10-02 16:01:16 +02002070 char *ptr;
2071
Willy Tarreau79ed4672020-04-29 11:44:54 +02002072 if (!smp->strm)
2073 return 0;
2074
2075 txn = smp->strm->txn;
Willy Tarreau79e57332018-10-02 16:01:16 +02002076 if (!txn || !txn->uri)
2077 return 0;
2078
2079 ptr = txn->uri;
2080
2081 while (*ptr != ' ' && *ptr != '\0') /* find first space */
2082 ptr++;
2083
2084 temp = get_trash_chunk();
2085 temp->area = txn->uri;
2086 temp->data = ptr - txn->uri;
2087 smp->data.u.str = *temp;
2088 smp->data.type = SMP_T_STR;
2089 smp->flags = SMP_F_CONST;
2090
2091 return 1;
2092
2093}
2094
2095/* Extracts the path in the HTTP request, the txn->uri should be filled before the call */
2096static int smp_fetch_capture_req_uri(const struct arg *args, struct sample *smp, const char *kw, void *private)
2097{
Willy Tarreau79ed4672020-04-29 11:44:54 +02002098 struct http_txn *txn;
Willy Tarreau79e57332018-10-02 16:01:16 +02002099 struct ist path;
2100 const char *ptr;
2101
Willy Tarreau79ed4672020-04-29 11:44:54 +02002102 if (!smp->strm)
2103 return 0;
2104
2105 txn = smp->strm->txn;
Willy Tarreau79e57332018-10-02 16:01:16 +02002106 if (!txn || !txn->uri)
2107 return 0;
2108
2109 ptr = txn->uri;
2110
2111 while (*ptr != ' ' && *ptr != '\0') /* find first space */
2112 ptr++;
2113
2114 if (!*ptr)
2115 return 0;
2116
Christopher Faulet78337bb2018-11-15 14:35:18 +01002117 /* skip the first space and find space after URI */
2118 path = ist2(++ptr, 0);
2119 while (*ptr != ' ' && *ptr != '\0')
2120 ptr++;
2121 path.len = ptr - path.ptr;
Willy Tarreau79e57332018-10-02 16:01:16 +02002122
Christopher Faulet78337bb2018-11-15 14:35:18 +01002123 path = http_get_path(path);
Willy Tarreau79e57332018-10-02 16:01:16 +02002124 if (!path.ptr)
2125 return 0;
2126
2127 smp->data.u.str.area = path.ptr;
2128 smp->data.u.str.data = path.len;
2129 smp->data.type = SMP_T_STR;
2130 smp->flags = SMP_F_CONST;
2131
2132 return 1;
2133}
2134
2135/* Retrieves the HTTP version from the request (either 1.0 or 1.1) and emits it
2136 * as a string (either "HTTP/1.0" or "HTTP/1.1").
2137 */
2138static int smp_fetch_capture_req_ver(const struct arg *args, struct sample *smp, const char *kw, void *private)
2139{
Willy Tarreau79ed4672020-04-29 11:44:54 +02002140 struct http_txn *txn;
2141
2142 if (!smp->strm)
2143 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02002144
Willy Tarreau79ed4672020-04-29 11:44:54 +02002145 txn = smp->strm->txn;
Willy Tarreau79e57332018-10-02 16:01:16 +02002146 if (!txn || txn->req.msg_state < HTTP_MSG_HDR_FIRST)
2147 return 0;
2148
2149 if (txn->req.flags & HTTP_MSGF_VER_11)
2150 smp->data.u.str.area = "HTTP/1.1";
2151 else
2152 smp->data.u.str.area = "HTTP/1.0";
2153
2154 smp->data.u.str.data = 8;
2155 smp->data.type = SMP_T_STR;
2156 smp->flags = SMP_F_CONST;
2157 return 1;
2158
2159}
2160
2161/* Retrieves the HTTP version from the response (either 1.0 or 1.1) and emits it
2162 * as a string (either "HTTP/1.0" or "HTTP/1.1").
2163 */
2164static int smp_fetch_capture_res_ver(const struct arg *args, struct sample *smp, const char *kw, void *private)
2165{
Willy Tarreau79ed4672020-04-29 11:44:54 +02002166 struct http_txn *txn;
2167
2168 if (!smp->strm)
2169 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02002170
Willy Tarreau79ed4672020-04-29 11:44:54 +02002171 txn = smp->strm->txn;
Willy Tarreau79e57332018-10-02 16:01:16 +02002172 if (!txn || txn->rsp.msg_state < HTTP_MSG_HDR_FIRST)
2173 return 0;
2174
2175 if (txn->rsp.flags & HTTP_MSGF_VER_11)
2176 smp->data.u.str.area = "HTTP/1.1";
2177 else
2178 smp->data.u.str.area = "HTTP/1.0";
2179
2180 smp->data.u.str.data = 8;
2181 smp->data.type = SMP_T_STR;
2182 smp->flags = SMP_F_CONST;
2183 return 1;
2184
2185}
2186
2187/* Iterate over all cookies present in a message. The context is stored in
2188 * smp->ctx.a[0] for the in-header position, smp->ctx.a[1] for the
2189 * end-of-header-value, and smp->ctx.a[2] for the hdr_ctx. Depending on
2190 * the direction, multiple cookies may be parsed on the same line or not.
2191 * The cookie name is in args and the name length in args->data.str.len.
2192 * Accepts exactly 1 argument of type string. If the input options indicate
2193 * that no iterating is desired, then only last value is fetched if any.
2194 * The returned sample is of type CSTR. Can be used to parse cookies in other
2195 * files.
2196 */
2197static int smp_fetch_cookie(const struct arg *args, struct sample *smp, const char *kw, void *private)
2198{
Christopher Faulet89dc4992019-04-17 12:02:59 +02002199 /* possible keywords: req.cookie / cookie / cook, res.cookie / scook / set-cookie */
2200 struct channel *chn = ((kw[0] == 'c' || kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Willy Tarreau79e57332018-10-02 16:01:16 +02002201 int occ = 0;
2202 int found = 0;
2203
2204 if (!args || args->type != ARGT_STR)
2205 return 0;
2206
Christopher Faulet46575cd2019-04-17 11:40:30 +02002207 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002208 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +02002209 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002210 struct http_hdr_ctx *ctx = smp->ctx.a[2];
2211 struct ist hdr;
Willy Tarreau79e57332018-10-02 16:01:16 +02002212
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002213 if (!ctx) {
2214 /* first call */
2215 ctx = &static_http_hdr_ctx;
2216 ctx->blk = NULL;
2217 smp->ctx.a[2] = ctx;
2218 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002219
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002220 if (!htx)
2221 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02002222
Christopher Faulet89dc4992019-04-17 12:02:59 +02002223 hdr = (!(chn->flags & CF_ISRESP) ? ist("Cookie") : ist("Set-Cookie"));
Willy Tarreau79e57332018-10-02 16:01:16 +02002224
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002225 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
2226 /* no explicit occurrence and single fetch => last cookie by default */
2227 occ = -1;
Willy Tarreau79e57332018-10-02 16:01:16 +02002228
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002229 /* OK so basically here, either we want only one value and it's the
2230 * last one, or we want to iterate over all of them and we fetch the
2231 * next one.
Willy Tarreau79e57332018-10-02 16:01:16 +02002232 */
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002233
2234 if (!(smp->flags & SMP_F_NOT_LAST)) {
2235 /* search for the header from the beginning, we must first initialize
2236 * the search parameters.
2237 */
2238 smp->ctx.a[0] = NULL;
2239 ctx->blk = NULL;
2240 }
2241
2242 smp->flags |= SMP_F_VOL_HDR;
2243 while (1) {
2244 /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */
2245 if (!smp->ctx.a[0]) {
2246 if (!http_find_header(htx, hdr, ctx, 0))
2247 goto out;
2248
2249 if (ctx->value.len < args->data.str.data + 1)
2250 continue;
2251
2252 smp->ctx.a[0] = ctx->value.ptr;
2253 smp->ctx.a[1] = smp->ctx.a[0] + ctx->value.len;
2254 }
2255
2256 smp->data.type = SMP_T_STR;
2257 smp->flags |= SMP_F_CONST;
2258 smp->ctx.a[0] = http_extract_cookie_value(smp->ctx.a[0], smp->ctx.a[1],
2259 args->data.str.area, args->data.str.data,
2260 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
2261 &smp->data.u.str.area,
2262 &smp->data.u.str.data);
2263 if (smp->ctx.a[0]) {
2264 found = 1;
2265 if (occ >= 0) {
2266 /* one value was returned into smp->data.u.str.{str,len} */
2267 smp->flags |= SMP_F_NOT_LAST;
2268 return 1;
2269 }
2270 }
2271 /* if we're looking for last occurrence, let's loop */
2272 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002273 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002274 else {
2275 /* LEGACY version */
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002276 struct hdr_idx *idx;
2277 struct hdr_ctx *ctx = smp->ctx.a[2];
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002278 const char *hdr_name;
2279 int hdr_name_len;
2280 char *sol;
Willy Tarreau79e57332018-10-02 16:01:16 +02002281
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002282 if (!ctx) {
2283 /* first call */
2284 ctx = &static_hdr_ctx;
2285 ctx->idx = 0;
2286 smp->ctx.a[2] = ctx;
2287 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002288
Christopher Faulet89dc4992019-04-17 12:02:59 +02002289 CHECK_HTTP_MESSAGE_FIRST(chn);
Willy Tarreau79e57332018-10-02 16:01:16 +02002290
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002291 idx = &smp->strm->txn->hdr_idx;
Christopher Faulet89dc4992019-04-17 12:02:59 +02002292 if (!(chn->flags & CF_ISRESP)) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002293 hdr_name = "Cookie";
2294 hdr_name_len = 6;
2295 } else {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002296 hdr_name = "Set-Cookie";
2297 hdr_name_len = 10;
Willy Tarreau79e57332018-10-02 16:01:16 +02002298 }
2299
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002300 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
2301 /* no explicit occurrence and single fetch => last cookie by default */
2302 occ = -1;
2303
2304 /* OK so basically here, either we want only one value and it's the
2305 * last one, or we want to iterate over all of them and we fetch the
2306 * next one.
2307 */
2308
Christopher Faulet89dc4992019-04-17 12:02:59 +02002309 sol = ci_head(chn);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002310 if (!(smp->flags & SMP_F_NOT_LAST)) {
2311 /* search for the header from the beginning, we must first initialize
2312 * the search parameters.
2313 */
2314 smp->ctx.a[0] = NULL;
2315 ctx->idx = 0;
2316 }
2317
2318 smp->flags |= SMP_F_VOL_HDR;
2319
2320 while (1) {
2321 /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */
2322 if (!smp->ctx.a[0]) {
2323 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, ctx))
2324 goto out;
2325
2326 if (ctx->vlen < args->data.str.data + 1)
2327 continue;
2328
2329 smp->ctx.a[0] = ctx->line + ctx->val;
2330 smp->ctx.a[1] = smp->ctx.a[0] + ctx->vlen;
2331 }
2332
2333 smp->data.type = SMP_T_STR;
2334 smp->flags |= SMP_F_CONST;
2335 smp->ctx.a[0] = http_extract_cookie_value(smp->ctx.a[0], smp->ctx.a[1],
2336 args->data.str.area, args->data.str.data,
2337 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
2338 &smp->data.u.str.area, &smp->data.u.str.data);
2339 if (smp->ctx.a[0]) {
2340 found = 1;
2341 if (occ >= 0) {
2342 /* one value was returned into smp->data.u.str.{str,len} */
2343 smp->flags |= SMP_F_NOT_LAST;
2344 return 1;
2345 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002346 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002347 /* if we're looking for last occurrence, let's loop */
Willy Tarreau79e57332018-10-02 16:01:16 +02002348 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002349 }
2350 /* all cookie headers and values were scanned. If we're looking for the
2351 * last occurrence, we may return it now.
2352 */
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002353 out:
Willy Tarreau79e57332018-10-02 16:01:16 +02002354 smp->flags &= ~SMP_F_NOT_LAST;
2355 return found;
2356}
2357
Christopher Fauletc1f40dd2019-05-16 10:07:30 +02002358/* Same than smp_fetch_cookie() but only relies on the sample direction to
2359 * choose the right channel. So instead of duplicating the code, we just change
2360 * the keyword and then fallback on smp_fetch_cookie().
2361 */
2362static int smp_fetch_chn_cookie(const struct arg *args, struct sample *smp, const char *kw, void *private)
2363{
2364 kw = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ ? "req.cook" : "res.cook");
2365 return smp_fetch_cookie(args, smp, kw, private);
2366}
2367
Willy Tarreau79e57332018-10-02 16:01:16 +02002368/* Iterate over all cookies present in a request to count how many occurrences
2369 * match the name in args and args->data.str.len. If <multi> is non-null, then
2370 * multiple cookies may be parsed on the same line. The returned sample is of
2371 * type UINT. Accepts exactly 1 argument of type string.
2372 */
2373static int smp_fetch_cookie_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
2374{
Christopher Faulet89dc4992019-04-17 12:02:59 +02002375 /* possible keywords: req.cook_cnt / cook_cnt, res.cook_cnt / scook_cnt */
2376 struct channel *chn = ((kw[0] == 'c' || kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Willy Tarreau79e57332018-10-02 16:01:16 +02002377 char *val_beg, *val_end;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002378 int cnt;
Willy Tarreau79e57332018-10-02 16:01:16 +02002379
2380 if (!args || args->type != ARGT_STR)
2381 return 0;
2382
Christopher Faulet46575cd2019-04-17 11:40:30 +02002383 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002384 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +02002385 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002386 struct http_hdr_ctx ctx;
2387 struct ist hdr;
2388
2389 if (!htx)
2390 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02002391
Christopher Faulet89dc4992019-04-17 12:02:59 +02002392 hdr = (!(chn->flags & CF_ISRESP) ? ist("Cookie") : ist("Set-Cookie"));
Willy Tarreau79e57332018-10-02 16:01:16 +02002393
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002394 val_end = val_beg = NULL;
2395 ctx.blk = NULL;
2396 cnt = 0;
2397 while (1) {
2398 /* Note: val_beg == NULL every time we need to fetch a new header */
2399 if (!val_beg) {
2400 if (!http_find_header(htx, hdr, &ctx, 0))
2401 break;
Willy Tarreau79e57332018-10-02 16:01:16 +02002402
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002403 if (ctx.value.len < args->data.str.data + 1)
2404 continue;
Willy Tarreau79e57332018-10-02 16:01:16 +02002405
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002406 val_beg = ctx.value.ptr;
2407 val_end = val_beg + ctx.value.len;
2408 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002409
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002410 smp->data.type = SMP_T_STR;
2411 smp->flags |= SMP_F_CONST;
2412 while ((val_beg = http_extract_cookie_value(val_beg, val_end,
2413 args->data.str.area, args->data.str.data,
2414 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
2415 &smp->data.u.str.area,
2416 &smp->data.u.str.data))) {
2417 cnt++;
2418 }
2419 }
2420 }
2421 else {
2422 /* LEGACY version */
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002423 struct hdr_idx *idx;
2424 struct hdr_ctx ctx;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002425 const char *hdr_name;
2426 int hdr_name_len;
2427 char *sol;
Willy Tarreau79e57332018-10-02 16:01:16 +02002428
Christopher Faulet89dc4992019-04-17 12:02:59 +02002429 CHECK_HTTP_MESSAGE_FIRST(chn);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002430
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002431 idx = &smp->strm->txn->hdr_idx;
Christopher Faulet89dc4992019-04-17 12:02:59 +02002432 if (!(chn->flags & CF_ISRESP)) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002433 hdr_name = "Cookie";
2434 hdr_name_len = 6;
2435 } else {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002436 hdr_name = "Set-Cookie";
2437 hdr_name_len = 10;
Willy Tarreau79e57332018-10-02 16:01:16 +02002438 }
2439
Christopher Faulet89dc4992019-04-17 12:02:59 +02002440 sol = ci_head(chn);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002441 val_end = val_beg = NULL;
2442 ctx.idx = 0;
2443 cnt = 0;
2444
2445 while (1) {
2446 /* Note: val_beg == NULL every time we need to fetch a new header */
2447 if (!val_beg) {
2448 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, &ctx))
2449 break;
2450
2451 if (ctx.vlen < args->data.str.data + 1)
2452 continue;
2453
2454 val_beg = ctx.line + ctx.val;
2455 val_end = val_beg + ctx.vlen;
2456 }
2457
2458 smp->data.type = SMP_T_STR;
2459 smp->flags |= SMP_F_CONST;
2460 while ((val_beg = http_extract_cookie_value(val_beg, val_end,
2461 args->data.str.area, args->data.str.data,
2462 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
2463 &smp->data.u.str.area, &smp->data.u.str.data))) {
2464 cnt++;
2465 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002466 }
2467 }
2468
2469 smp->data.type = SMP_T_SINT;
2470 smp->data.u.sint = cnt;
2471 smp->flags |= SMP_F_VOL_HDR;
2472 return 1;
2473}
2474
2475/* Fetch an cookie's integer value. The integer value is returned. It
2476 * takes a mandatory argument of type string. It relies on smp_fetch_cookie().
2477 */
2478static int smp_fetch_cookie_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
2479{
2480 int ret = smp_fetch_cookie(args, smp, kw, private);
2481
2482 if (ret > 0) {
2483 smp->data.type = SMP_T_SINT;
2484 smp->data.u.sint = strl2ic(smp->data.u.str.area,
2485 smp->data.u.str.data);
2486 }
2487
2488 return ret;
2489}
2490
2491/************************************************************************/
2492/* The code below is dedicated to sample fetches */
2493/************************************************************************/
2494
2495/* This scans a URL-encoded query string. It takes an optionally wrapping
2496 * string whose first contigous chunk has its beginning in ctx->a[0] and end
2497 * in ctx->a[1], and the optional second part in (ctx->a[2]..ctx->a[3]). The
2498 * pointers are updated for next iteration before leaving.
2499 */
2500static int smp_fetch_param(char delim, const char *name, int name_len, const struct arg *args, struct sample *smp, const char *kw, void *private)
2501{
2502 const char *vstart, *vend;
2503 struct buffer *temp;
2504 const char **chunks = (const char **)smp->ctx.a;
2505
2506 if (!http_find_next_url_param(chunks, name, name_len,
2507 &vstart, &vend, delim))
2508 return 0;
2509
2510 /* Create sample. If the value is contiguous, return the pointer as CONST,
2511 * if the value is wrapped, copy-it in a buffer.
2512 */
2513 smp->data.type = SMP_T_STR;
2514 if (chunks[2] &&
2515 vstart >= chunks[0] && vstart <= chunks[1] &&
2516 vend >= chunks[2] && vend <= chunks[3]) {
2517 /* Wrapped case. */
2518 temp = get_trash_chunk();
2519 memcpy(temp->area, vstart, chunks[1] - vstart);
2520 memcpy(temp->area + ( chunks[1] - vstart ), chunks[2],
2521 vend - chunks[2]);
2522 smp->data.u.str.area = temp->area;
2523 smp->data.u.str.data = ( chunks[1] - vstart ) + ( vend - chunks[2] );
2524 } else {
2525 /* Contiguous case. */
2526 smp->data.u.str.area = (char *)vstart;
2527 smp->data.u.str.data = vend - vstart;
2528 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
2529 }
2530
2531 /* Update context, check wrapping. */
2532 chunks[0] = vend;
2533 if (chunks[2] && vend >= chunks[2] && vend <= chunks[3]) {
2534 chunks[1] = chunks[3];
2535 chunks[2] = NULL;
2536 }
2537
2538 if (chunks[0] < chunks[1])
2539 smp->flags |= SMP_F_NOT_LAST;
2540
2541 return 1;
2542}
2543
2544/* This function iterates over each parameter of the query string. It uses
2545 * ctx->a[0] and ctx->a[1] to store the beginning and end of the current
2546 * parameter. Since it uses smp_fetch_param(), ctx->a[2..3] are both NULL.
2547 * An optional parameter name is passed in args[0], otherwise any parameter is
2548 * considered. It supports an optional delimiter argument for the beginning of
2549 * the string in args[1], which defaults to "?".
2550 */
2551static int smp_fetch_url_param(const struct arg *args, struct sample *smp, const char *kw, void *private)
2552{
Christopher Faulet89dc4992019-04-17 12:02:59 +02002553 struct channel *chn = SMP_REQ_CHN(smp);
Willy Tarreau79e57332018-10-02 16:01:16 +02002554 char delim = '?';
2555 const char *name;
2556 int name_len;
2557
2558 if (!args ||
2559 (args[0].type && args[0].type != ARGT_STR) ||
2560 (args[1].type && args[1].type != ARGT_STR))
2561 return 0;
2562
2563 name = "";
2564 name_len = 0;
2565 if (args->type == ARGT_STR) {
2566 name = args->data.str.area;
2567 name_len = args->data.str.data;
2568 }
2569
2570 if (args[1].type)
2571 delim = *args[1].data.str.area;
2572
2573 if (!smp->ctx.a[0]) { // first call, find the query string
Christopher Faulet46575cd2019-04-17 11:40:30 +02002574 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002575 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +02002576 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01002577 struct htx_sl *sl;
Willy Tarreau79e57332018-10-02 16:01:16 +02002578
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002579 if (!htx)
2580 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02002581
Christopher Faulet297fbb42019-05-13 14:41:27 +02002582 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01002583 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 +02002584 if (!smp->ctx.a[0])
2585 return 0;
2586
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01002587 smp->ctx.a[1] = HTX_SL_REQ_UPTR(sl) + HTX_SL_REQ_ULEN(sl);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002588 }
2589 else {
2590 /* LEGACY version */
2591 struct http_msg *msg;
2592
Christopher Faulet89dc4992019-04-17 12:02:59 +02002593 CHECK_HTTP_MESSAGE_FIRST(chn);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002594
2595 msg = &smp->strm->txn->req;
2596
Christopher Faulet89dc4992019-04-17 12:02:59 +02002597 smp->ctx.a[0] = http_find_param_list(ci_head(chn) + msg->sl.rq.u,
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002598 msg->sl.rq.u_l, delim);
2599 if (!smp->ctx.a[0])
2600 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02002601
Christopher Faulet89dc4992019-04-17 12:02:59 +02002602 smp->ctx.a[1] = ci_head(chn) + msg->sl.rq.u + msg->sl.rq.u_l;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002603 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002604
2605 /* Assume that the context is filled with NULL pointer
2606 * before the first call.
2607 * smp->ctx.a[2] = NULL;
2608 * smp->ctx.a[3] = NULL;
2609 */
2610 }
2611
2612 return smp_fetch_param(delim, name, name_len, args, smp, kw, private);
2613}
2614
2615/* This function iterates over each parameter of the body. This requires
2616 * that the body has been waited for using http-buffer-request. It uses
2617 * ctx->a[0] and ctx->a[1] to store the beginning and end of the first
2618 * contigous part of the body, and optionally ctx->a[2..3] to reference the
2619 * optional second part if the body wraps at the end of the buffer. An optional
2620 * parameter name is passed in args[0], otherwise any parameter is considered.
2621 */
2622static int smp_fetch_body_param(const struct arg *args, struct sample *smp, const char *kw, void *private)
2623{
Christopher Faulet89dc4992019-04-17 12:02:59 +02002624 struct channel *chn = SMP_REQ_CHN(smp);
Willy Tarreau79e57332018-10-02 16:01:16 +02002625 const char *name;
2626 int name_len;
2627
2628 if (!args || (args[0].type && args[0].type != ARGT_STR))
2629 return 0;
2630
2631 name = "";
2632 name_len = 0;
2633 if (args[0].type == ARGT_STR) {
2634 name = args[0].data.str.area;
2635 name_len = args[0].data.str.data;
2636 }
2637
2638 if (!smp->ctx.a[0]) { // first call, find the query string
Christopher Faulet46575cd2019-04-17 11:40:30 +02002639 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002640 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +02002641 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002642 struct buffer *temp;
2643 int32_t pos;
Willy Tarreau79e57332018-10-02 16:01:16 +02002644
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002645 if (!htx)
2646 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02002647
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002648 temp = get_trash_chunk();
Christopher Fauleta3f15502019-05-13 15:27:23 +02002649 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002650 struct htx_blk *blk = htx_get_blk(htx, pos);
2651 enum htx_blk_type type = htx_get_blk_type(blk);
Willy Tarreau79e57332018-10-02 16:01:16 +02002652
Christopher Faulet54b5e212019-06-04 10:08:28 +02002653 if (type == HTX_BLK_EOM || type == HTX_BLK_TLR || type == HTX_BLK_EOT)
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002654 break;
2655 if (type == HTX_BLK_DATA) {
Christopher Fauletc59ff232018-12-03 13:58:44 +01002656 if (!htx_data_to_h1(htx_get_blk_value(htx, blk), temp, 0))
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002657 return 0;
2658 }
2659 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002660
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002661 smp->ctx.a[0] = temp->area;
2662 smp->ctx.a[1] = temp->area + temp->data;
Willy Tarreau79e57332018-10-02 16:01:16 +02002663
2664 /* Assume that the context is filled with NULL pointer
2665 * before the first call.
2666 * smp->ctx.a[2] = NULL;
2667 * smp->ctx.a[3] = NULL;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002668 */
Willy Tarreau79e57332018-10-02 16:01:16 +02002669 }
2670 else {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002671 /* LEGACY version */
2672 struct http_msg *msg;
2673 unsigned long len;
2674 unsigned long block1;
2675 char *body;
2676
Christopher Faulet89dc4992019-04-17 12:02:59 +02002677 CHECK_HTTP_MESSAGE_FIRST(chn);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002678
Christopher Faulet89dc4992019-04-17 12:02:59 +02002679 msg = &smp->strm->txn->req;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002680 len = http_body_bytes(msg);
Christopher Faulet89dc4992019-04-17 12:02:59 +02002681 body = c_ptr(chn, -http_data_rewind(msg));
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002682
2683 block1 = len;
Christopher Faulet89dc4992019-04-17 12:02:59 +02002684 if (block1 > b_wrap(&chn->buf) - body)
2685 block1 = b_wrap(&chn->buf) - body;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002686
2687 if (block1 == len) {
2688 /* buffer is not wrapped (or empty) */
2689 smp->ctx.a[0] = body;
2690 smp->ctx.a[1] = body + len;
2691
2692 /* Assume that the context is filled with NULL pointer
2693 * before the first call.
2694 * smp->ctx.a[2] = NULL;
2695 * smp->ctx.a[3] = NULL;
2696 */
2697 }
2698 else {
2699 /* buffer is wrapped, we need to defragment it */
2700 smp->ctx.a[0] = body;
2701 smp->ctx.a[1] = body + block1;
Christopher Faulet89dc4992019-04-17 12:02:59 +02002702 smp->ctx.a[2] = b_orig(&chn->buf);
2703 smp->ctx.a[3] = b_orig(&chn->buf) + ( len - block1 );
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002704 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002705 }
2706 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002707
Willy Tarreau79e57332018-10-02 16:01:16 +02002708 return smp_fetch_param('&', name, name_len, args, smp, kw, private);
2709}
2710
2711/* Return the signed integer value for the specified url parameter (see url_param
2712 * above).
2713 */
2714static int smp_fetch_url_param_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
2715{
2716 int ret = smp_fetch_url_param(args, smp, kw, private);
2717
2718 if (ret > 0) {
2719 smp->data.type = SMP_T_SINT;
2720 smp->data.u.sint = strl2ic(smp->data.u.str.area,
2721 smp->data.u.str.data);
2722 }
2723
2724 return ret;
2725}
2726
2727/* This produces a 32-bit hash of the concatenation of the first occurrence of
2728 * the Host header followed by the path component if it begins with a slash ('/').
2729 * This means that '*' will not be added, resulting in exactly the first Host
2730 * entry. If no Host header is found, then the path is used. The resulting value
2731 * is hashed using the url hash followed by a full avalanche hash and provides a
2732 * 32-bit integer value. This fetch is useful for tracking per-URL activity on
2733 * high-traffic sites without having to store whole paths.
2734 * this differs from the base32 functions in that it includes the url parameters
2735 * as well as the path
2736 */
2737static int smp_fetch_url32(const struct arg *args, struct sample *smp, const char *kw, void *private)
2738{
Christopher Faulet89dc4992019-04-17 12:02:59 +02002739 struct channel *chn = SMP_REQ_CHN(smp);
Willy Tarreau79e57332018-10-02 16:01:16 +02002740 unsigned int hash = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02002741
Christopher Faulet46575cd2019-04-17 11:40:30 +02002742 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002743 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +02002744 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002745 struct http_hdr_ctx ctx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01002746 struct htx_sl *sl;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002747 struct ist path;
Willy Tarreau79e57332018-10-02 16:01:16 +02002748
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002749 if (!htx)
2750 return 0;
2751
2752 ctx.blk = NULL;
2753 if (http_find_header(htx, ist("Host"), &ctx, 1)) {
2754 /* OK we have the header value in ctx.value */
2755 while (ctx.value.len--)
2756 hash = *(ctx.value.ptr++) + (hash << 6) + (hash << 16) - hash;
2757 }
2758
2759 /* now retrieve the path */
Christopher Faulet297fbb42019-05-13 14:41:27 +02002760 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01002761 path = http_get_path(htx_sl_req_uri(sl));
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002762 if (path.len && *(path.ptr) == '/') {
2763 while (path.len--)
2764 hash = *(path.ptr++) + (hash << 6) + (hash << 16) - hash;
2765 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002766 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002767 else {
2768 /* LEGACY version */
2769 struct http_txn *txn;
2770 struct hdr_ctx ctx;
2771 char *ptr, *beg, *end;
2772 int len;
2773
Christopher Faulet89dc4992019-04-17 12:02:59 +02002774 CHECK_HTTP_MESSAGE_FIRST(chn);
Willy Tarreau79e57332018-10-02 16:01:16 +02002775
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002776 txn = smp->strm->txn;
2777 ctx.idx = 0;
Christopher Faulet89dc4992019-04-17 12:02:59 +02002778 if (http_find_header2("Host", 4, ci_head(chn), &txn->hdr_idx, &ctx)) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002779 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
2780 ptr = ctx.line + ctx.val;
2781 len = ctx.vlen;
2782 while (len--)
2783 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
2784 }
2785
2786 /* now retrieve the path */
Christopher Faulet89dc4992019-04-17 12:02:59 +02002787 end = ci_head(chn) + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002788 beg = http_txn_get_path(txn);
2789 if (!beg)
2790 beg = end;
Willy Tarreau79e57332018-10-02 16:01:16 +02002791
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002792 for (ptr = beg; ptr < end ; ptr++);
Willy Tarreau79e57332018-10-02 16:01:16 +02002793
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002794 if (beg < ptr && *beg == '/') {
2795 while (beg < ptr)
2796 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
2797 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002798 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002799
Willy Tarreau79e57332018-10-02 16:01:16 +02002800 hash = full_hash(hash);
2801
2802 smp->data.type = SMP_T_SINT;
2803 smp->data.u.sint = hash;
2804 smp->flags = SMP_F_VOL_1ST;
2805 return 1;
2806}
2807
2808/* This concatenates the source address with the 32-bit hash of the Host and
2809 * URL as returned by smp_fetch_base32(). The idea is to have per-source and
2810 * per-url counters. The result is a binary block from 8 to 20 bytes depending
2811 * on the source address length. The URL hash is stored before the address so
2812 * that in environments where IPv6 is insignificant, truncating the output to
2813 * 8 bytes would still work.
2814 */
2815static int smp_fetch_url32_src(const struct arg *args, struct sample *smp, const char *kw, void *private)
2816{
2817 struct buffer *temp;
2818 struct connection *cli_conn = objt_conn(smp->sess->origin);
2819
2820 if (!cli_conn)
2821 return 0;
2822
2823 if (!smp_fetch_url32(args, smp, kw, private))
2824 return 0;
2825
2826 temp = get_trash_chunk();
2827 *(unsigned int *) temp->area = htonl(smp->data.u.sint);
2828 temp->data += sizeof(unsigned int);
2829
2830 switch (cli_conn->addr.from.ss_family) {
2831 case AF_INET:
2832 memcpy(temp->area + temp->data,
2833 &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr,
2834 4);
2835 temp->data += 4;
2836 break;
2837 case AF_INET6:
2838 memcpy(temp->area + temp->data,
2839 &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr,
2840 16);
2841 temp->data += 16;
2842 break;
2843 default:
2844 return 0;
2845 }
2846
2847 smp->data.u.str = *temp;
2848 smp->data.type = SMP_T_BIN;
2849 return 1;
2850}
2851
2852/************************************************************************/
2853/* Other utility functions */
2854/************************************************************************/
2855
2856/* This function is used to validate the arguments passed to any "hdr" fetch
2857 * keyword. These keywords support an optional positive or negative occurrence
2858 * number. We must ensure that the number is greater than -MAX_HDR_HISTORY. It
2859 * is assumed that the types are already the correct ones. Returns 0 on error,
2860 * non-zero if OK. If <err> is not NULL, it will be filled with a pointer to an
2861 * error message in case of error, that the caller is responsible for freeing.
2862 * The initial location must either be freeable or NULL.
2863 * Note: this function's pointer is checked from Lua.
2864 */
2865int val_hdr(struct arg *arg, char **err_msg)
2866{
2867 if (arg && arg[1].type == ARGT_SINT && arg[1].data.sint < -MAX_HDR_HISTORY) {
2868 memprintf(err_msg, "header occurrence must be >= %d", -MAX_HDR_HISTORY);
2869 return 0;
2870 }
2871 return 1;
2872}
2873
2874/************************************************************************/
2875/* All supported sample fetch keywords must be declared here. */
2876/************************************************************************/
2877
2878/* Note: must not be declared <const> as its list will be overwritten */
2879static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
2880 { "base", smp_fetch_base, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2881 { "base32", smp_fetch_base32, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
2882 { "base32+src", smp_fetch_base32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
2883
2884 /* capture are allocated and are permanent in the stream */
2885 { "capture.req.hdr", smp_fetch_capture_req_hdr, ARG1(1,SINT), NULL, SMP_T_STR, SMP_USE_HRQHP },
2886
2887 /* retrieve these captures from the HTTP logs */
2888 { "capture.req.method", smp_fetch_capture_req_method, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
2889 { "capture.req.uri", smp_fetch_capture_req_uri, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
2890 { "capture.req.ver", smp_fetch_capture_req_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
2891
2892 { "capture.res.hdr", smp_fetch_capture_res_hdr, ARG1(1,SINT), NULL, SMP_T_STR, SMP_USE_HRSHP },
2893 { "capture.res.ver", smp_fetch_capture_res_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
2894
2895 /* cookie is valid in both directions (eg: for "stick ...") but cook*
2896 * are only here to match the ACL's name, are request-only and are used
2897 * for ACL compatibility only.
2898 */
2899 { "cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Christopher Fauletc1f40dd2019-05-16 10:07:30 +02002900 { "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 +02002901 { "cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2902 { "cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2903
2904 /* hdr is valid in both directions (eg: for "stick ...") but hdr_* are
2905 * only here to match the ACL's name, are request-only and are used for
2906 * ACL compatibility only.
2907 */
Christopher Fauletc1f40dd2019-05-16 10:07:30 +02002908 { "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 +02002909 { "hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2910 { "hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV },
2911 { "hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_SINT, SMP_USE_HRQHV },
2912
2913 { "http_auth", smp_fetch_http_auth, ARG1(1,USR), NULL, SMP_T_BOOL, SMP_USE_HRQHV },
2914 { "http_auth_group", smp_fetch_http_auth_grp, ARG1(1,USR), NULL, SMP_T_STR, SMP_USE_HRQHV },
2915 { "http_first_req", smp_fetch_http_first_req, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
2916 { "method", smp_fetch_meth, 0, NULL, SMP_T_METH, SMP_USE_HRQHP },
2917 { "path", smp_fetch_path, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2918 { "query", smp_fetch_query, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2919
2920 /* HTTP protocol on the request path */
2921 { "req.proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
2922 { "req_proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
2923
2924 /* HTTP version on the request path */
2925 { "req.ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2926 { "req_ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2927
2928 { "req.body", smp_fetch_body, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
2929 { "req.body_len", smp_fetch_body_len, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
2930 { "req.body_size", smp_fetch_body_size, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
2931 { "req.body_param", smp_fetch_body_param, ARG1(0,STR), NULL, SMP_T_BIN, SMP_USE_HRQHV },
2932
2933 { "req.hdrs", smp_fetch_hdrs, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
2934 { "req.hdrs_bin", smp_fetch_hdrs_bin, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
2935
2936 /* HTTP version on the response path */
2937 { "res.ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
2938 { "resp_ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
2939
2940 /* explicit req.{cook,hdr} are used to force the fetch direction to be request-only */
2941 { "req.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
2942 { "req.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2943 { "req.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2944
2945 { "req.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
2946 { "req.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2947 { "req.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
2948 { "req.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2949 { "req.hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV },
2950 { "req.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
2951 { "req.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_SINT, SMP_USE_HRQHV },
2952
2953 /* explicit req.{cook,hdr} are used to force the fetch direction to be response-only */
2954 { "res.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
2955 { "res.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2956 { "res.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2957
2958 { "res.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
2959 { "res.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2960 { "res.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
2961 { "res.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2962 { "res.hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV },
2963 { "res.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
2964 { "res.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_SINT, SMP_USE_HRSHV },
2965
2966 /* scook is valid only on the response and is used for ACL compatibility */
2967 { "scook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
2968 { "scook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2969 { "scook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2970 { "set-cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV }, /* deprecated */
2971
2972 /* shdr is valid only on the response and is used for ACL compatibility */
2973 { "shdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
2974 { "shdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2975 { "shdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV },
2976 { "shdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_SINT, SMP_USE_HRSHV },
2977
2978 { "status", smp_fetch_stcode, 0, NULL, SMP_T_SINT, SMP_USE_HRSHP },
2979 { "unique-id", smp_fetch_uniqueid, 0, NULL, SMP_T_STR, SMP_SRC_L4SRV },
2980 { "url", smp_fetch_url, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2981 { "url32", smp_fetch_url32, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
2982 { "url32+src", smp_fetch_url32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
2983 { "url_ip", smp_fetch_url_ip, 0, NULL, SMP_T_IPV4, SMP_USE_HRQHV },
2984 { "url_port", smp_fetch_url_port, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
2985 { "url_param", smp_fetch_url_param, ARG2(0,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
2986 { "urlp" , smp_fetch_url_param, ARG2(0,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
2987 { "urlp_val", smp_fetch_url_param_val, ARG2(0,STR,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2988 { /* END */ },
2989}};
2990
Willy Tarreau0108d902018-11-25 19:14:37 +01002991INITCALL1(STG_REGISTER, sample_register_fetches, &sample_fetch_keywords);
Willy Tarreau79e57332018-10-02 16:01:16 +02002992
2993/*
2994 * Local variables:
2995 * c-indent-level: 8
2996 * c-basic-offset: 8
2997 * End:
2998 */