blob: 8f88646ea886aee17b6b1d4f102139fe16fc4438 [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>
Frédéric Lécaille1fceee82019-02-25 15:30:36 +010042#include <proto/protocol_buffers.h>
Willy Tarreau79e57332018-10-02 16:01:16 +020043#include <proto/sample.h>
44#include <proto/stream.h>
45
46
47/* this struct is used between calls to smp_fetch_hdr() or smp_fetch_cookie() */
48static THREAD_LOCAL struct hdr_ctx static_hdr_ctx;
Christopher Fauletef453ed2018-10-24 21:39:27 +020049static THREAD_LOCAL struct http_hdr_ctx static_http_hdr_ctx;
50
Willy Tarreau79e57332018-10-02 16:01:16 +020051
52/*
53 * Returns the data from Authorization header. Function may be called more
54 * than once so data is stored in txn->auth_data. When no header is found
55 * or auth method is unknown auth_method is set to HTTP_AUTH_WRONG to avoid
56 * searching again for something we are unable to find anyway. However, if
57 * the result if valid, the cache is not reused because we would risk to
58 * have the credentials overwritten by another stream in parallel.
59 */
60
Christopher Faulet311c7ea2018-10-24 21:41:55 +020061static int get_http_auth(struct sample *smp)
Willy Tarreau79e57332018-10-02 16:01:16 +020062{
Christopher Faulet311c7ea2018-10-24 21:41:55 +020063 struct stream *s = smp->strm;
Willy Tarreau79e57332018-10-02 16:01:16 +020064 struct http_txn *txn = s->txn;
65 struct buffer auth_method;
Willy Tarreau79e57332018-10-02 16:01:16 +020066 char *h, *p;
67 int len;
68
69#ifdef DEBUG_AUTH
70 printf("Auth for stream %p: %d\n", s, txn->auth.method);
71#endif
Willy Tarreau79e57332018-10-02 16:01:16 +020072 if (txn->auth.method == HTTP_AUTH_WRONG)
73 return 0;
74
75 txn->auth.method = HTTP_AUTH_WRONG;
76
Christopher Faulet311c7ea2018-10-24 21:41:55 +020077 if (IS_HTX_STRM(s) || (smp->px->mode == PR_MODE_TCP)) {
78 /* HTX version */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +010079 struct htx *htx = htxbuf(&s->req.buf);
Christopher Faulet311c7ea2018-10-24 21:41:55 +020080 struct http_hdr_ctx ctx = { .blk = NULL };
81 struct ist hdr;
Willy Tarreau79e57332018-10-02 16:01:16 +020082
Christopher Faulet311c7ea2018-10-24 21:41:55 +020083 if (txn->flags & TX_USE_PX_CONN)
84 hdr = ist("Proxy-Authorization");
85 else
86 hdr = ist("Authorization");
87
Christopher Faulet311c7ea2018-10-24 21:41:55 +020088 ctx.blk = NULL;
89 if (!http_find_header(htx, hdr, &ctx, 0))
90 return 0;
91
92 p = memchr(ctx.value.ptr, ' ', ctx.value.len);
93 len = p - ctx.value.ptr;
94 if (!p || len <= 0)
95 return 0;
96
97 if (chunk_initlen(&auth_method, ctx.value.ptr, 0, len) != 1)
98 return 0;
99
100 chunk_initlen(&txn->auth.method_data, p + 1, 0, ctx.value.len - len - 1);
Willy Tarreau79e57332018-10-02 16:01:16 +0200101 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200102 else {
103 /* LEGACY version */
104 struct hdr_ctx ctx = { .idx = 0 };
Willy Tarreau79e57332018-10-02 16:01:16 +0200105
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200106 if (txn->flags & TX_USE_PX_CONN) {
107 h = "Proxy-Authorization";
108 len = strlen(h);
109 } else {
110 h = "Authorization";
111 len = strlen(h);
112 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200113
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200114 if (!http_find_header2(h, len, ci_head(&s->req), &txn->hdr_idx, &ctx))
115 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200116
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200117 h = ctx.line + ctx.val;
Willy Tarreau79e57332018-10-02 16:01:16 +0200118
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200119 p = memchr(h, ' ', ctx.vlen);
120 len = p - h;
121 if (!p || len <= 0)
122 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200123
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200124 if (chunk_initlen(&auth_method, h, 0, len) != 1)
125 return 0;
126
127 chunk_initlen(&txn->auth.method_data, p + 1, 0, ctx.vlen - len - 1);
128 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200129
130 if (!strncasecmp("Basic", auth_method.area, auth_method.data)) {
131 struct buffer *http_auth = get_trash_chunk();
132
133 len = base64dec(txn->auth.method_data.area,
134 txn->auth.method_data.data,
135 http_auth->area, global.tune.bufsize - 1);
136
137 if (len < 0)
138 return 0;
139
140
141 http_auth->area[len] = '\0';
142
143 p = strchr(http_auth->area, ':');
144
145 if (!p)
146 return 0;
147
148 txn->auth.user = http_auth->area;
149 *p = '\0';
150 txn->auth.pass = p+1;
151
152 txn->auth.method = HTTP_AUTH_BASIC;
153 return 1;
154 }
155
156 return 0;
157}
158
159/* This function ensures that the prerequisites for an L7 fetch are ready,
160 * which means that a request or response is ready. If some data is missing,
161 * a parsing attempt is made. This is useful in TCP-based ACLs which are able
Christopher Fauletef453ed2018-10-24 21:39:27 +0200162 * to extract data from L7.
163 *
164 * The function returns :
165 * NULL with SMP_F_MAY_CHANGE in the sample flags if some data is missing to
166 * decide whether or not an HTTP message is present ;
167 * NULL if the requested data cannot be fetched or if it is certain that
168 * we'll never have any HTTP message there ;
169 * The HTX message if ready
170 */
171struct htx *smp_prefetch_htx(struct sample *smp, const struct arg *args)
172{
173 struct proxy *px = smp->px;
174 struct stream *s = smp->strm;
175 unsigned int opt = smp->opt;
176 struct http_txn *txn = NULL;
177 struct htx *htx = NULL;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100178 struct htx_sl *sl;
Christopher Fauletef453ed2018-10-24 21:39:27 +0200179
180 /* Note: it is possible that <s> is NULL when called before stream
181 * initialization (eg: tcp-request connection), so this function is the
182 * one responsible for guarding against this case for all HTTP users.
183 */
184 if (!s)
185 return NULL;
186
187 if (!s->txn) {
188 if (unlikely(!http_alloc_txn(s)))
189 return NULL; /* not enough memory */
190 http_init_txn(s);
191 txn = s->txn;
192 }
193
194 if (px->mode == PR_MODE_HTTP) {
195 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Christopher Faulet27ba2dc2018-12-05 11:53:24 +0100196 htx = htxbuf(&s->req.buf);
Christopher Fauletef453ed2018-10-24 21:39:27 +0200197 if (htx_is_empty(htx) || htx_get_tail_type(htx) < HTX_BLK_EOH) {
198 /* Parsing is done by the mux, just wait */
199 smp->flags |= SMP_F_MAY_CHANGE;
200 return NULL;
201 }
202
203 /* OK we just got a valid HTTP request. We have some
204 * minor preparation to perform so that further checks
205 * can rely on HTTP tests.
206 */
207 if (txn) {
208 sl = http_find_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100209 txn->meth = sl->info.req.meth;
Christopher Fauletef453ed2018-10-24 21:39:27 +0200210 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
211 s->flags |= SF_REDIRECTABLE;
212 }
213
214 /* otherwise everything's ready for the request */
215 }
216 else {
Christopher Faulet27ba2dc2018-12-05 11:53:24 +0100217 htx = htxbuf(&s->res.buf);
Christopher Fauletef453ed2018-10-24 21:39:27 +0200218 if (htx_is_empty(htx) || htx_get_tail_type(htx) < HTX_BLK_EOH) {
219 /* Parsing is done by the mux, just wait */
220 smp->flags |= SMP_F_MAY_CHANGE;
221 return NULL;
222 }
223 }
224 }
225 else { /* PR_MODE_TCP */
226 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
227 struct buffer *buf;
228 struct h1m h1m;
229 struct http_hdr hdrs[MAX_HTTP_HDR];
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100230 union h1_sl h1sl;
231 unsigned int flags = HTX_FL_NONE;
Christopher Fauletef453ed2018-10-24 21:39:27 +0200232 int ret;
233
234 buf = &s->req.buf;
235 if (b_head(buf) + b_data(buf) > b_wrap(buf))
236 b_slow_realign(buf, trash.area, 0);
237
238 h1m_init_req(&h1m);
239 ret = h1_headers_to_hdr_list(b_head(buf), b_stop(buf),
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100240 hdrs, sizeof(hdrs)/sizeof(hdrs[0]), &h1m, &h1sl);
Christopher Fauletef453ed2018-10-24 21:39:27 +0200241 if (ret <= 0) {
242 /* Invalid or too big*/
243 if (ret < 0 || channel_full(&s->req, global.tune.maxrewrite))
244 return NULL;
245
246 /* wait for a full request */
247 smp->flags |= SMP_F_MAY_CHANGE;
248 return NULL;
249 }
250
251 /* OK we just got a valid HTTP request. We have to
252 * convert it into an HTX message.
253 */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100254 if (unlikely(h1sl.rq.v.len == 0)) {
Christopher Fauletef453ed2018-10-24 21:39:27 +0200255 /* try to convert HTTP/0.9 requests to HTTP/1.0 */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100256 if (h1sl.rq.meth != HTTP_METH_GET || !h1sl.rq.u.len)
Christopher Fauletef453ed2018-10-24 21:39:27 +0200257 return NULL;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100258 h1sl.rq.v = ist("HTTP/1.0");
Christopher Fauletef453ed2018-10-24 21:39:27 +0200259 }
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100260 else if ((h1sl.rq.v.len == 8) &&
261 ((*(h1sl.rq.v.ptr + 5) > '1') ||
262 ((*(h1sl.rq.v.ptr + 5) == '1') && (*(h1sl.rq.v.ptr + 7) >= '1'))))
263 h1m.flags |= H1_MF_VER_11;
264
265
266 /* Set HTX start-line flags */
267 if (h1m.flags & H1_MF_VER_11)
268 flags |= HTX_SL_F_VER_11;
269 if (h1m.flags & H1_MF_XFER_ENC)
270 flags |= HTX_SL_F_XFER_ENC;
271 if (h1m.flags & H1_MF_XFER_LEN) {
272 flags |= HTX_SL_F_XFER_LEN;
273 if (h1m.flags & H1_MF_CHNK)
274 flags |= HTX_SL_F_CHNK;
275 else if (h1m.flags & H1_MF_CLEN)
276 flags |= HTX_SL_F_CLEN;
277 }
278
Christopher Fauletef453ed2018-10-24 21:39:27 +0200279 htx = htx_from_buf(get_trash_chunk());
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100280 sl = htx_add_stline(htx, HTX_BLK_REQ_SL, flags, h1sl.rq.m, h1sl.rq.u, h1sl.rq.v);
281 if (!sl || !htx_add_all_headers(htx, hdrs))
Christopher Fauletef453ed2018-10-24 21:39:27 +0200282 return NULL;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100283 sl->info.req.meth = h1sl.rq.meth;
Christopher Fauletef453ed2018-10-24 21:39:27 +0200284
285 if (txn) {
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100286 txn->meth = h1sl.rq.meth;
Christopher Fauletef453ed2018-10-24 21:39:27 +0200287 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
288 s->flags |= SF_REDIRECTABLE;
289 }
290 /* Ok, now everything's ready for the request */
291 }
292 else {
293 /* Impossible, no HTTP fetch on tcp-response */
294 return NULL;
295 }
296 }
297
298 /* everything's OK */
299 smp->data.u.sint = 1;
300 return htx;
301}
302
303/* This function ensures that the prerequisites for an L7 fetch are ready,
304 * which means that a request or response is ready. If some data is missing,
305 * a parsing attempt is made. This is useful in TCP-based ACLs which are able
Willy Tarreau79e57332018-10-02 16:01:16 +0200306 * to extract data from L7. If <req_vol> is non-null during a request prefetch,
307 * another test is made to ensure the required information is not gone.
308 *
309 * The function returns :
310 * 0 with SMP_F_MAY_CHANGE in the sample flags if some data is missing to
311 * decide whether or not an HTTP message is present ;
312 * 0 if the requested data cannot be fetched or if it is certain that
313 * we'll never have any HTTP message there ;
314 * 1 if an HTTP message is ready
315 */
316int smp_prefetch_http(struct proxy *px, struct stream *s, unsigned int opt,
317 const struct arg *args, struct sample *smp, int req_vol)
318{
319 struct http_txn *txn;
320 struct http_msg *msg;
321
322 /* Note: it is possible that <s> is NULL when called before stream
323 * initialization (eg: tcp-request connection), so this function is the
324 * one responsible for guarding against this case for all HTTP users.
325 */
326 if (!s)
327 return 0;
328
329 if (!s->txn) {
330 if (unlikely(!http_alloc_txn(s)))
331 return 0; /* not enough memory */
332 http_init_txn(s);
333 }
334 txn = s->txn;
335 msg = &txn->req;
336
337 /* Check for a dependency on a request */
338 smp->data.type = SMP_T_BOOL;
339
340 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
341 /* If the buffer does not leave enough free space at the end,
342 * we must first realign it.
343 */
344 if (ci_head(&s->req) > b_orig(&s->req.buf) &&
345 ci_head(&s->req) + ci_data(&s->req) > b_wrap(&s->req.buf) - global.tune.maxrewrite)
346 channel_slow_realign(&s->req, trash.area);
347
348 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY)) {
349 if (msg->msg_state == HTTP_MSG_ERROR)
350 return 0;
351
352 /* Try to decode HTTP request */
353 if (likely(msg->next < ci_data(&s->req)))
354 http_msg_analyzer(msg, &txn->hdr_idx);
355
356 /* Still no valid request ? */
357 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
358 if ((msg->msg_state == HTTP_MSG_ERROR) ||
359 channel_full(&s->req, global.tune.maxrewrite)) {
360 return 0;
361 }
362 /* wait for final state */
363 smp->flags |= SMP_F_MAY_CHANGE;
364 return 0;
365 }
366
367 /* OK we just got a valid HTTP request. We have some minor
368 * preparation to perform so that further checks can rely
369 * on HTTP tests.
370 */
371
372 /* If the request was parsed but was too large, we must absolutely
373 * return an error so that it is not processed. At the moment this
374 * cannot happen, but if the parsers are to change in the future,
375 * we want this check to be maintained.
376 */
377 if (unlikely(ci_head(&s->req) + ci_data(&s->req) >
378 b_wrap(&s->req.buf) - global.tune.maxrewrite)) {
379 msg->err_state = msg->msg_state;
380 msg->msg_state = HTTP_MSG_ERROR;
381 smp->data.u.sint = 1;
382 return 1;
383 }
384
385 txn->meth = find_http_meth(ci_head(msg->chn), msg->sl.rq.m_l);
386 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
387 s->flags |= SF_REDIRECTABLE;
388
389 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
390 return 0;
391 }
392
393 if (req_vol && txn->rsp.msg_state != HTTP_MSG_RPBEFORE) {
394 return 0; /* data might have moved and indexes changed */
395 }
396
397 /* otherwise everything's ready for the request */
398 }
399 else {
400 /* Check for a dependency on a response */
401 if (txn->rsp.msg_state < HTTP_MSG_BODY) {
402 smp->flags |= SMP_F_MAY_CHANGE;
403 return 0;
404 }
405 }
406
407 /* everything's OK */
408 smp->data.u.sint = 1;
409 return 1;
410}
411
412/* This function fetches the method of current HTTP request and stores
413 * it in the global pattern struct as a chunk. There are two possibilities :
414 * - if the method is known (not HTTP_METH_OTHER), its identifier is stored
415 * in <len> and <ptr> is NULL ;
416 * - if the method is unknown (HTTP_METH_OTHER), <ptr> points to the text and
417 * <len> to its length.
418 * This is intended to be used with pat_match_meth() only.
419 */
420static int smp_fetch_meth(const struct arg *args, struct sample *smp, const char *kw, void *private)
421{
422 int meth;
423 struct http_txn *txn;
424
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200425 if (IS_HTX_SMP(smp) || (smp->px->mode == PR_MODE_TCP)) {
426 /* HTX version */
427 struct htx *htx = smp_prefetch_htx(smp, args);
Willy Tarreau79e57332018-10-02 16:01:16 +0200428
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200429 if (!htx)
Willy Tarreau79e57332018-10-02 16:01:16 +0200430 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200431
432 txn = smp->strm->txn;
433 meth = txn->meth;
434 smp->data.type = SMP_T_METH;
435 smp->data.u.meth.meth = meth;
436 if (meth == HTTP_METH_OTHER) {
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100437 struct htx_sl *sl;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200438
439 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
440 /* ensure the indexes are not affected */
441 return 0;
442
443 sl = http_find_stline(htx);
444 smp->flags |= SMP_F_CONST;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100445 smp->data.u.meth.str.area = HTX_SL_REQ_MPTR(sl);
446 smp->data.u.meth.str.data = HTX_SL_REQ_MLEN(sl);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200447 }
448 smp->flags |= SMP_F_VOL_1ST;
449 }
450 else {
451 /* LEGACY version */
452 CHECK_HTTP_MESSAGE_FIRST_PERM();
453
454 txn = smp->strm->txn;
455 meth = txn->meth;
456 smp->data.type = SMP_T_METH;
457 smp->data.u.meth.meth = meth;
458 if (meth == HTTP_METH_OTHER) {
459 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
460 /* ensure the indexes are not affected */
461 return 0;
462 smp->flags |= SMP_F_CONST;
463 smp->data.u.meth.str.data = txn->req.sl.rq.m_l;
464 smp->data.u.meth.str.area = ci_head(txn->req.chn);
465 }
466 smp->flags |= SMP_F_VOL_1ST;
Willy Tarreau79e57332018-10-02 16:01:16 +0200467 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200468 return 1;
469}
470
471static int smp_fetch_rqver(const struct arg *args, struct sample *smp, const char *kw, void *private)
472{
473 struct http_txn *txn;
474 char *ptr;
475 int len;
476
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200477 if (IS_HTX_SMP(smp) || (smp->px->mode == PR_MODE_TCP)) {
478 /* HTX version */
479 struct htx *htx = smp_prefetch_htx(smp, args);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100480 struct htx_sl *sl;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200481
482 if (!htx)
483 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200484
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200485 sl = http_find_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100486 len = HTX_SL_REQ_VLEN(sl);
487 ptr = HTX_SL_REQ_VPTR(sl);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200488 }
489 else {
490 /* LEGACY version */
491 CHECK_HTTP_MESSAGE_FIRST();
492
493 txn = smp->strm->txn;
494 len = txn->req.sl.rq.v_l;
495 ptr = ci_head(txn->req.chn) + txn->req.sl.rq.v;
496 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200497
498 while ((len-- > 0) && (*ptr++ != '/'));
499 if (len <= 0)
500 return 0;
501
502 smp->data.type = SMP_T_STR;
503 smp->data.u.str.area = ptr;
504 smp->data.u.str.data = len;
505
506 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
507 return 1;
508}
509
510static int smp_fetch_stver(const struct arg *args, struct sample *smp, const char *kw, void *private)
511{
512 struct http_txn *txn;
513 char *ptr;
514 int len;
515
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200516 if (IS_HTX_SMP(smp) || (smp->px->mode == PR_MODE_TCP)) {
517 /* HTX version */
518 struct htx *htx = smp_prefetch_htx(smp, args);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100519 struct htx_sl *sl;
Willy Tarreau79e57332018-10-02 16:01:16 +0200520
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200521 if (!htx)
522 return 0;
523
524 sl = http_find_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100525 len = HTX_SL_RES_VLEN(sl);
526 ptr = HTX_SL_RES_VPTR(sl);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200527 }
528 else {
529 /* LEGACY version */
530 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau79e57332018-10-02 16:01:16 +0200531
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200532 txn = smp->strm->txn;
533 if (txn->rsp.msg_state < HTTP_MSG_BODY)
534 return 0;
535
536 len = txn->rsp.sl.st.v_l;
537 ptr = ci_head(txn->rsp.chn);
538 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200539
540 while ((len-- > 0) && (*ptr++ != '/'));
541 if (len <= 0)
542 return 0;
543
544 smp->data.type = SMP_T_STR;
545 smp->data.u.str.area = ptr;
546 smp->data.u.str.data = len;
547
548 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
549 return 1;
550}
551
552/* 3. Check on Status Code. We manipulate integers here. */
553static int smp_fetch_stcode(const struct arg *args, struct sample *smp, const char *kw, void *private)
554{
555 struct http_txn *txn;
556 char *ptr;
557 int len;
558
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200559 if (IS_HTX_SMP(smp) || (smp->px->mode == PR_MODE_TCP)) {
560 /* HTX version */
561 struct htx *htx = smp_prefetch_htx(smp, args);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100562 struct htx_sl *sl;
Willy Tarreau79e57332018-10-02 16:01:16 +0200563
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200564 if (!htx)
565 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200566
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200567 sl = http_find_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100568 len = HTX_SL_RES_CLEN(sl);
569 ptr = HTX_SL_RES_CPTR(sl);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200570 }
571 else {
572 /* LEGACY version */
573 CHECK_HTTP_MESSAGE_FIRST();
574
575 txn = smp->strm->txn;
576 if (txn->rsp.msg_state < HTTP_MSG_BODY)
577 return 0;
578
579 len = txn->rsp.sl.st.c_l;
580 ptr = ci_head(txn->rsp.chn) + txn->rsp.sl.st.c;
581 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200582
583 smp->data.type = SMP_T_SINT;
584 smp->data.u.sint = __strl2ui(ptr, len);
585 smp->flags = SMP_F_VOL_1ST;
586 return 1;
587}
588
589static int smp_fetch_uniqueid(const struct arg *args, struct sample *smp, const char *kw, void *private)
590{
591 if (LIST_ISEMPTY(&smp->sess->fe->format_unique_id))
592 return 0;
593
594 if (!smp->strm->unique_id) {
595 if ((smp->strm->unique_id = pool_alloc(pool_head_uniqueid)) == NULL)
596 return 0;
597 smp->strm->unique_id[0] = '\0';
598 }
599 smp->data.u.str.data = build_logline(smp->strm, smp->strm->unique_id,
600 UNIQUEID_LEN, &smp->sess->fe->format_unique_id);
601
602 smp->data.type = SMP_T_STR;
603 smp->data.u.str.area = smp->strm->unique_id;
604 smp->flags = SMP_F_CONST;
605 return 1;
606}
607
608/* Returns a string block containing all headers including the
Joseph Herlant942eea32018-11-15 13:57:22 -0800609 * empty line which separes headers from the body. This is useful
610 * for some headers analysis.
Willy Tarreau79e57332018-10-02 16:01:16 +0200611 */
612static int smp_fetch_hdrs(const struct arg *args, struct sample *smp, const char *kw, void *private)
613{
Willy Tarreau79e57332018-10-02 16:01:16 +0200614 struct http_txn *txn;
615
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200616 if (IS_HTX_SMP(smp) || (smp->px->mode == PR_MODE_TCP)) {
617 /* HTX version */
618 struct htx *htx = smp_prefetch_htx(smp, args);
619 struct buffer *temp;
620 int32_t pos;
Willy Tarreau79e57332018-10-02 16:01:16 +0200621
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200622 if (!htx)
623 return 0;
624 temp = get_trash_chunk();
625 for (pos = htx_get_head(htx); pos != -1; pos = htx_get_next(htx, pos)) {
626 struct htx_blk *blk = htx_get_blk(htx, pos);
627 enum htx_blk_type type = htx_get_blk_type(blk);
Willy Tarreau79e57332018-10-02 16:01:16 +0200628
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200629 if (type == HTX_BLK_HDR) {
630 struct ist n = htx_get_blk_name(htx, blk);
631 struct ist v = htx_get_blk_value(htx, blk);
632
Christopher Fauletc59ff232018-12-03 13:58:44 +0100633 if (!htx_hdr_to_h1(n, v, temp))
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200634 return 0;
635 }
636 else if (type == HTX_BLK_EOH) {
637 if (!chunk_memcat(temp, "\r\n", 2))
638 return 0;
639 break;
640 }
641 }
642 smp->data.type = SMP_T_STR;
643 smp->data.u.str = *temp;
644
645 }
646 else {
647 /* LEGACY version */
648 struct http_msg *msg;
649 struct hdr_idx *idx;
650
651 CHECK_HTTP_MESSAGE_FIRST();
652
653 txn = smp->strm->txn;
654 idx = &txn->hdr_idx;
655 msg = &txn->req;
Willy Tarreau79e57332018-10-02 16:01:16 +0200656
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200657 smp->data.type = SMP_T_STR;
658 smp->data.u.str.area = ci_head(msg->chn) + hdr_idx_first_pos(idx);
659 smp->data.u.str.data = msg->eoh - hdr_idx_first_pos(idx) + 1 +
660 (ci_head(msg->chn)[msg->eoh] == '\r');
661 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200662 return 1;
663}
664
665/* Returns the header request in a length/value encoded format.
666 * This is useful for exchanges with the SPOE.
667 *
668 * A "length value" is a multibyte code encoding numbers. It uses the
669 * SPOE format. The encoding is the following:
670 *
671 * Each couple "header name" / "header value" is composed
672 * like this:
673 * "length value" "header name bytes"
674 * "length value" "header value bytes"
675 * When the last header is reached, the header name and the header
676 * value are empty. Their length are 0
677 */
678static int smp_fetch_hdrs_bin(const struct arg *args, struct sample *smp, const char *kw, void *private)
679{
Willy Tarreau79e57332018-10-02 16:01:16 +0200680 struct http_txn *txn;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200681 struct buffer *temp;
Willy Tarreau79e57332018-10-02 16:01:16 +0200682
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200683 if (IS_HTX_SMP(smp) || (smp->px->mode == PR_MODE_TCP)) {
684 /* HTX version */
685 struct htx *htx = smp_prefetch_htx(smp, args);
686 struct buffer *temp;
687 char *p, *end;
688 int32_t pos;
689 int ret;
Willy Tarreau79e57332018-10-02 16:01:16 +0200690
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200691 if (!htx)
692 return 0;
693 temp = get_trash_chunk();
694 p = temp->area;
695 end = temp->area + temp->size;
696 for (pos = htx_get_head(htx); pos != -1; pos = htx_get_next(htx, pos)) {
697 struct htx_blk *blk = htx_get_blk(htx, pos);
698 enum htx_blk_type type = htx_get_blk_type(blk);
699 struct ist n, v;
Willy Tarreau79e57332018-10-02 16:01:16 +0200700
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200701 if (type == HTX_BLK_HDR) {
702 n = htx_get_blk_name(htx,blk);
703 v = htx_get_blk_value(htx, blk);
Willy Tarreau79e57332018-10-02 16:01:16 +0200704
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200705 /* encode the header name. */
706 ret = encode_varint(n.len, &p, end);
707 if (ret == -1)
708 return 0;
709 if (p + n.len > end)
710 return 0;
711 memcpy(p, n.ptr, n.len);
712 p += n.len;
Willy Tarreau79e57332018-10-02 16:01:16 +0200713
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200714 /* encode the header value. */
715 ret = encode_varint(v.len, &p, end);
716 if (ret == -1)
717 return 0;
718 if (p + v.len > end)
719 return 0;
720 memcpy(p, v.ptr, v.len);
721 p += v.len;
Willy Tarreau79e57332018-10-02 16:01:16 +0200722
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200723 }
724 else if (type == HTX_BLK_EOH) {
725 /* encode the end of the header list with empty
726 * header name and header value.
727 */
728 ret = encode_varint(0, &p, end);
729 if (ret == -1)
730 return 0;
731 ret = encode_varint(0, &p, end);
732 if (ret == -1)
733 return 0;
734 break;
735 }
736 }
737
738 /* Initialise sample data which will be filled. */
739 smp->data.type = SMP_T_BIN;
740 smp->data.u.str.area = temp->area;
741 smp->data.u.str.data = p - temp->area;
742 smp->data.u.str.size = temp->size;
743 }
744 else {
745 /* LEGACY version */
746 struct http_msg *msg;
747 struct hdr_idx *idx;
748 const char *cur_ptr, *cur_next, *p;
749 int old_idx, cur_idx;
750 struct hdr_idx_elem *cur_hdr;
751 const char *hn, *hv;
752 int hnl, hvl;
753 int ret;
754 char *buf;
755 char *end;
756
757 CHECK_HTTP_MESSAGE_FIRST();
758
759 temp = get_trash_chunk();
760 buf = temp->area;
761 end = temp->area + temp->size;
762
763 txn = smp->strm->txn;
764 idx = &txn->hdr_idx;
765 msg = &txn->req;
766
767 /* Build array of headers. */
768 old_idx = 0;
769 cur_next = ci_head(msg->chn) + hdr_idx_first_pos(idx);
770 while (1) {
771 cur_idx = idx->v[old_idx].next;
772 if (!cur_idx)
773 break;
774 old_idx = cur_idx;
Willy Tarreau79e57332018-10-02 16:01:16 +0200775
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200776 cur_hdr = &idx->v[cur_idx];
777 cur_ptr = cur_next;
778 cur_next = cur_ptr + cur_hdr->len + cur_hdr->cr + 1;
779
780 /* Now we have one full header at cur_ptr of len cur_hdr->len,
781 * and the next header starts at cur_next. We'll check
782 * this header in the list as well as against the default
783 * rule.
784 */
785
786 /* look for ': *'. */
787 hn = cur_ptr;
788 for (p = cur_ptr; p < cur_ptr + cur_hdr->len && *p != ':'; p++);
789 if (p >= cur_ptr+cur_hdr->len)
790 continue;
791 hnl = p - hn;
Willy Tarreau79e57332018-10-02 16:01:16 +0200792 p++;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200793 while (p < cur_ptr + cur_hdr->len && (*p == ' ' || *p == '\t'))
794 p++;
795 if (p >= cur_ptr + cur_hdr->len)
796 continue;
797 hv = p;
798 hvl = cur_ptr + cur_hdr->len-p;
Willy Tarreau79e57332018-10-02 16:01:16 +0200799
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200800 /* encode the header name. */
801 ret = encode_varint(hnl, &buf, end);
802 if (ret == -1)
803 return 0;
804 if (buf + hnl > end)
805 return 0;
806 memcpy(buf, hn, hnl);
807 buf += hnl;
808
809 /* encode and copy the value. */
810 ret = encode_varint(hvl, &buf, end);
811 if (ret == -1)
812 return 0;
813 if (buf + hvl > end)
814 return 0;
815 memcpy(buf, hv, hvl);
816 buf += hvl;
817 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200818
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200819 /* encode the end of the header list with empty
820 * header name and header value.
821 */
822 ret = encode_varint(0, &buf, end);
Willy Tarreau79e57332018-10-02 16:01:16 +0200823 if (ret == -1)
824 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200825 ret = encode_varint(0, &buf, end);
826 if (ret == -1)
Willy Tarreau79e57332018-10-02 16:01:16 +0200827 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200828
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200829 /* Initialise sample data which will be filled. */
830 smp->data.type = SMP_T_BIN;
831 smp->data.u.str.area = temp->area;
832 smp->data.u.str.data = buf - temp->area;
833 smp->data.u.str.size = temp->size;
834 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200835 return 1;
836}
837
838/* returns the longest available part of the body. This requires that the body
839 * has been waited for using http-buffer-request.
840 */
841static int smp_fetch_body(const struct arg *args, struct sample *smp, const char *kw, void *private)
842{
Willy Tarreau79e57332018-10-02 16:01:16 +0200843 struct buffer *temp;
844
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200845 if (IS_HTX_SMP(smp) || (smp->px->mode == PR_MODE_TCP)) {
846 /* HTX version */
847 struct htx *htx = smp_prefetch_htx(smp, args);
848 int32_t pos;
Willy Tarreau79e57332018-10-02 16:01:16 +0200849
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200850 if (!htx)
851 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200852
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200853 temp = get_trash_chunk();
854 for (pos = htx_get_head(htx); pos != -1; pos = htx_get_next(htx, pos)) {
855 struct htx_blk *blk = htx_get_blk(htx, pos);
856 enum htx_blk_type type = htx_get_blk_type(blk);
Willy Tarreau79e57332018-10-02 16:01:16 +0200857
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200858 if (type == HTX_BLK_EOM || type == HTX_BLK_EOD)
859 break;
860 if (type == HTX_BLK_DATA) {
Christopher Fauletc59ff232018-12-03 13:58:44 +0100861 if (!htx_data_to_h1(htx_get_blk_value(htx, blk), temp, 0))
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200862 return 0;
863 }
864 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200865
Willy Tarreau79e57332018-10-02 16:01:16 +0200866 smp->data.type = SMP_T_BIN;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200867 smp->data.u.str = *temp;
868 smp->flags = SMP_F_VOL_TEST;
Willy Tarreau79e57332018-10-02 16:01:16 +0200869 }
870 else {
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200871 /* LEGACY version */
872 struct http_msg *msg;
873 unsigned long len;
874 unsigned long block1;
875 char *body;
876
877 CHECK_HTTP_MESSAGE_FIRST();
878
879 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
880 msg = &smp->strm->txn->req;
881 else
882 msg = &smp->strm->txn->rsp;
883
884 len = http_body_bytes(msg);
885 body = c_ptr(msg->chn, -http_data_rewind(msg));
886
887 block1 = len;
888 if (block1 > b_wrap(&msg->chn->buf) - body)
889 block1 = b_wrap(&msg->chn->buf) - body;
890
891 if (block1 == len) {
892 /* buffer is not wrapped (or empty) */
893 smp->data.type = SMP_T_BIN;
894 smp->data.u.str.area = body;
895 smp->data.u.str.data = len;
896 smp->flags = SMP_F_VOL_TEST | SMP_F_CONST;
897 }
898 else {
899 /* buffer is wrapped, we need to defragment it */
900 temp = get_trash_chunk();
901 memcpy(temp->area, body, block1);
902 memcpy(temp->area + block1, b_orig(&msg->chn->buf),
903 len - block1);
904 smp->data.type = SMP_T_BIN;
905 smp->data.u.str.area = temp->area;
906 smp->data.u.str.data = len;
907 smp->flags = SMP_F_VOL_TEST;
908 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200909 }
910 return 1;
911}
912
913
914/* returns the available length of the body. This requires that the body
915 * has been waited for using http-buffer-request.
916 */
917static int smp_fetch_body_len(const struct arg *args, struct sample *smp, const char *kw, void *private)
918{
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200919 if (IS_HTX_SMP(smp) || (smp->px->mode == PR_MODE_TCP)) {
920 /* HTX version */
Christopher Fauletc16317d2018-12-12 14:11:22 +0100921 struct htx *htx = smp_prefetch_htx(smp, args);
Dragan Dosen5a606682019-02-14 12:30:53 +0100922 int32_t pos;
Christopher Fauletc16317d2018-12-12 14:11:22 +0100923 unsigned long long len = 0;
924
925 if (!htx)
926 return 0;
927
Dragan Dosen5a606682019-02-14 12:30:53 +0100928 for (pos = htx_get_head(htx); pos != -1; pos = htx_get_next(htx, pos)) {
929 struct htx_blk *blk = htx_get_blk(htx, pos);
930 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Fauletc16317d2018-12-12 14:11:22 +0100931
Dragan Dosen5a606682019-02-14 12:30:53 +0100932 if (type == HTX_BLK_EOM || type == HTX_BLK_EOD)
Christopher Fauletc16317d2018-12-12 14:11:22 +0100933 break;
Dragan Dosen5a606682019-02-14 12:30:53 +0100934 if (type == HTX_BLK_DATA)
935 len += htx_get_blksz(blk);
Christopher Fauletc16317d2018-12-12 14:11:22 +0100936 }
937
938 smp->data.type = SMP_T_SINT;
939 smp->data.u.sint = len;
940
941 smp->flags = SMP_F_VOL_TEST;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200942 }
943 else {
944 /* LEGACY version */
945 struct http_msg *msg;
Willy Tarreau79e57332018-10-02 16:01:16 +0200946
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200947 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau79e57332018-10-02 16:01:16 +0200948
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200949 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
950 msg = &smp->strm->txn->req;
951 else
952 msg = &smp->strm->txn->rsp;
Willy Tarreau79e57332018-10-02 16:01:16 +0200953
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200954 smp->data.type = SMP_T_SINT;
955 smp->data.u.sint = http_body_bytes(msg);
Willy Tarreau79e57332018-10-02 16:01:16 +0200956
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200957 smp->flags = SMP_F_VOL_TEST;
958 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200959 return 1;
960}
961
962
963/* returns the advertised length of the body, or the advertised size of the
964 * chunks available in the buffer. This requires that the body has been waited
965 * for using http-buffer-request.
966 */
967static int smp_fetch_body_size(const struct arg *args, struct sample *smp, const char *kw, void *private)
968{
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200969 if (IS_HTX_SMP(smp) || (smp->px->mode == PR_MODE_TCP)) {
970 /* HTX version */
Christopher Fauletc16317d2018-12-12 14:11:22 +0100971 struct htx *htx = smp_prefetch_htx(smp, args);
Dragan Dosen5a606682019-02-14 12:30:53 +0100972 int32_t pos;
Christopher Fauletc16317d2018-12-12 14:11:22 +0100973 unsigned long long len = 0;
974
975 if (!htx)
976 return 0;
977
Dragan Dosen5a606682019-02-14 12:30:53 +0100978 for (pos = htx_get_head(htx); pos != -1; pos = htx_get_next(htx, pos)) {
979 struct htx_blk *blk = htx_get_blk(htx, pos);
980 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Fauletc16317d2018-12-12 14:11:22 +0100981
Dragan Dosen5a606682019-02-14 12:30:53 +0100982 if (type == HTX_BLK_EOM || type == HTX_BLK_EOD)
Christopher Fauletc16317d2018-12-12 14:11:22 +0100983 break;
Dragan Dosen5a606682019-02-14 12:30:53 +0100984 if (type == HTX_BLK_DATA)
985 len += htx_get_blksz(blk);
Christopher Fauletc16317d2018-12-12 14:11:22 +0100986 }
987 if (htx->extra != ULLONG_MAX)
988 len += htx->extra;
989
990 smp->data.type = SMP_T_SINT;
991 smp->data.u.sint = len;
992
993 smp->flags = SMP_F_VOL_TEST;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200994 }
995 else {
996 /* LEGACY version */
997 struct http_msg *msg;
Willy Tarreau79e57332018-10-02 16:01:16 +0200998
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200999 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau79e57332018-10-02 16:01:16 +02001000
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001001 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
1002 msg = &smp->strm->txn->req;
1003 else
1004 msg = &smp->strm->txn->rsp;
Willy Tarreau79e57332018-10-02 16:01:16 +02001005
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001006 smp->data.type = SMP_T_SINT;
1007 smp->data.u.sint = msg->body_len;
Willy Tarreau79e57332018-10-02 16:01:16 +02001008
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001009 smp->flags = SMP_F_VOL_TEST;
1010 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001011 return 1;
1012}
1013
1014
1015/* 4. Check on URL/URI. A pointer to the URI is stored. */
1016static int smp_fetch_url(const struct arg *args, struct sample *smp, const char *kw, void *private)
1017{
1018 struct http_txn *txn;
1019
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001020 if (IS_HTX_SMP(smp) || (smp->px->mode == PR_MODE_TCP)) {
1021 /* HTX version */
1022 struct htx *htx = smp_prefetch_htx(smp, args);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001023 struct htx_sl *sl;
Willy Tarreau79e57332018-10-02 16:01:16 +02001024
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001025 if (!htx)
1026 return 0;
1027 sl = http_find_stline(htx);
1028 smp->data.type = SMP_T_STR;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001029 smp->data.u.str.area = HTX_SL_REQ_UPTR(sl);
1030 smp->data.u.str.data = HTX_SL_REQ_ULEN(sl);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001031 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
1032 }
1033 else {
1034 /* LEGACY version */
1035 CHECK_HTTP_MESSAGE_FIRST();
1036
1037 txn = smp->strm->txn;
1038 smp->data.type = SMP_T_STR;
1039 smp->data.u.str.data = txn->req.sl.rq.u_l;
1040 smp->data.u.str.area = ci_head(txn->req.chn) + txn->req.sl.rq.u;
1041 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
1042 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001043 return 1;
1044}
1045
1046static int smp_fetch_url_ip(const struct arg *args, struct sample *smp, const char *kw, void *private)
1047{
1048 struct http_txn *txn;
1049 struct sockaddr_storage addr;
1050
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001051 if (IS_HTX_SMP(smp) || (smp->px->mode == PR_MODE_TCP)) {
1052 /* HTX version */
1053 struct htx *htx = smp_prefetch_htx(smp, args);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001054 struct htx_sl *sl;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001055
1056 if (!htx)
1057 return 0;
1058 sl = http_find_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001059 url2sa(HTX_SL_REQ_UPTR(sl), HTX_SL_REQ_ULEN(sl), &addr, NULL);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001060 }
1061 else {
1062 /* LEGACY version */
1063 CHECK_HTTP_MESSAGE_FIRST();
1064
1065 txn = smp->strm->txn;
1066 url2sa(ci_head(txn->req.chn) + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &addr, NULL);
1067 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001068
Willy Tarreau79e57332018-10-02 16:01:16 +02001069 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
1070 return 0;
1071
1072 smp->data.type = SMP_T_IPV4;
1073 smp->data.u.ipv4 = ((struct sockaddr_in *)&addr)->sin_addr;
1074 smp->flags = 0;
1075 return 1;
1076}
1077
1078static int smp_fetch_url_port(const struct arg *args, struct sample *smp, const char *kw, void *private)
1079{
1080 struct http_txn *txn;
1081 struct sockaddr_storage addr;
1082
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001083 if (IS_HTX_SMP(smp) || (smp->px->mode == PR_MODE_TCP)) {
1084 /* HTX version */
1085 struct htx *htx = smp_prefetch_htx(smp, args);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001086 struct htx_sl *sl;
Willy Tarreau79e57332018-10-02 16:01:16 +02001087
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001088 if (!htx)
1089 return 0;
1090 sl = http_find_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001091 url2sa(HTX_SL_REQ_UPTR(sl), HTX_SL_REQ_ULEN(sl), &addr, NULL);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001092 }
1093 else {
1094 /* LEGACY version */
1095 CHECK_HTTP_MESSAGE_FIRST();
1096
1097 txn = smp->strm->txn;
1098 url2sa(ci_head(txn->req.chn) + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &addr, NULL);
1099 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001100 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
1101 return 0;
1102
1103 smp->data.type = SMP_T_SINT;
1104 smp->data.u.sint = ntohs(((struct sockaddr_in *)&addr)->sin_port);
1105 smp->flags = 0;
1106 return 1;
1107}
1108
1109/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
1110 * Accepts an optional argument of type string containing the header field name,
1111 * and an optional argument of type signed or unsigned integer to request an
1112 * explicit occurrence of the header. Note that in the event of a missing name,
1113 * headers are considered from the first one. It does not stop on commas and
1114 * returns full lines instead (useful for User-Agent or Date for example).
1115 */
1116static int smp_fetch_fhdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
1117{
Willy Tarreau79e57332018-10-02 16:01:16 +02001118 int occ = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001119
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001120 if (IS_HTX_SMP(smp) || (smp->px->mode == PR_MODE_TCP)) {
1121 /* HTX version */
1122 struct htx *htx = smp_prefetch_htx(smp, args);
1123 struct http_hdr_ctx *ctx = smp->ctx.a[0];
1124 struct ist name;
Willy Tarreau79e57332018-10-02 16:01:16 +02001125
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001126 if (!ctx) {
1127 /* first call */
1128 ctx = &static_http_hdr_ctx;
1129 ctx->blk = NULL;
1130 smp->ctx.a[0] = ctx;
1131 }
1132
1133 if (args) {
1134 if (args[0].type != ARGT_STR)
1135 return 0;
1136 name.ptr = args[0].data.str.area;
1137 name.len = args[0].data.str.data;
1138
1139 if (args[1].type == ARGT_SINT)
1140 occ = args[1].data.sint;
1141 }
1142
1143 if (!htx)
Willy Tarreau79e57332018-10-02 16:01:16 +02001144 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001145
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001146 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
1147 /* search for header from the beginning */
1148 ctx->blk = NULL;
1149
1150 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
1151 /* no explicit occurrence and single fetch => last header by default */
1152 occ = -1;
1153
1154 if (!occ)
1155 /* prepare to report multiple occurrences for ACL fetches */
1156 smp->flags |= SMP_F_NOT_LAST;
1157
1158 smp->data.type = SMP_T_STR;
1159 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
1160 if (http_get_htx_fhdr(htx, name, occ, ctx, &smp->data.u.str.area, &smp->data.u.str.data))
1161 return 1;
Willy Tarreau79e57332018-10-02 16:01:16 +02001162 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001163 else {
1164 /* LEGACY version */
1165 struct hdr_idx *idx;
1166 struct hdr_ctx *ctx = smp->ctx.a[0];
1167 const struct http_msg *msg;
1168 const char *name_str = NULL;
1169 int name_len = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001170
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001171 if (!ctx) {
1172 /* first call */
1173 ctx = &static_hdr_ctx;
1174 ctx->idx = 0;
1175 smp->ctx.a[0] = ctx;
1176 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001177
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001178 if (args) {
1179 if (args[0].type != ARGT_STR)
1180 return 0;
1181 name_str = args[0].data.str.area;
1182 name_len = args[0].data.str.data;
Willy Tarreau79e57332018-10-02 16:01:16 +02001183
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001184 if (args[1].type == ARGT_SINT)
1185 occ = args[1].data.sint;
1186 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001187
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001188 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau79e57332018-10-02 16:01:16 +02001189
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001190 idx = &smp->strm->txn->hdr_idx;
1191 msg = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &smp->strm->txn->req : &smp->strm->txn->rsp;
Willy Tarreau79e57332018-10-02 16:01:16 +02001192
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001193 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
1194 /* search for header from the beginning */
1195 ctx->idx = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001196
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001197 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
1198 /* no explicit occurrence and single fetch => last header by default */
1199 occ = -1;
1200
1201 if (!occ)
1202 /* prepare to report multiple occurrences for ACL fetches */
1203 smp->flags |= SMP_F_NOT_LAST;
1204
1205 smp->data.type = SMP_T_STR;
1206 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
1207 if (http_get_fhdr(msg, name_str, name_len, idx, occ, ctx, &smp->data.u.str.area, &smp->data.u.str.data))
1208 return 1;
1209 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001210 smp->flags &= ~SMP_F_NOT_LAST;
1211 return 0;
1212}
1213
1214/* 6. Check on HTTP header count. The number of occurrences is returned.
1215 * Accepts exactly 1 argument of type string. It does not stop on commas and
1216 * returns full lines instead (useful for User-Agent or Date for example).
1217 */
1218static int smp_fetch_fhdr_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
1219{
Willy Tarreau79e57332018-10-02 16:01:16 +02001220 int cnt;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001221
1222 if (IS_HTX_SMP(smp) || (smp->px->mode == PR_MODE_TCP)) {
1223 /* HTX version */
1224 struct htx *htx = smp_prefetch_htx(smp, args);
1225 struct http_hdr_ctx ctx;
1226 struct ist name;
1227
1228 if (!htx)
1229 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001230
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001231 if (args && args->type == ARGT_STR) {
1232 name.ptr = args->data.str.area;
1233 name.len = args->data.str.data;
Olivier Houcharde2c78cd2018-11-21 13:49:48 +01001234 } else {
1235 name.ptr = NULL;
1236 name.len = 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001237 }
1238
1239 ctx.blk = NULL;
1240 cnt = 0;
1241 while (http_find_header(htx, name, &ctx, 1))
1242 cnt++;
Willy Tarreau79e57332018-10-02 16:01:16 +02001243 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001244 else {
1245 /* LEGACY version */
1246 struct hdr_idx *idx;
1247 struct hdr_ctx ctx;
1248 const struct http_msg *msg;
1249 const char *name = NULL;
1250 int len = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001251
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001252 if (args && args->type == ARGT_STR) {
1253 name = args->data.str.area;
1254 len = args->data.str.data;
1255 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001256
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001257 CHECK_HTTP_MESSAGE_FIRST();
1258
1259 idx = &smp->strm->txn->hdr_idx;
1260 msg = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &smp->strm->txn->req : &smp->strm->txn->rsp;
Willy Tarreau79e57332018-10-02 16:01:16 +02001261
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001262 ctx.idx = 0;
1263 cnt = 0;
1264 while (http_find_full_header2(name, len, ci_head(msg->chn), idx, &ctx))
1265 cnt++;
1266 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001267
1268 smp->data.type = SMP_T_SINT;
1269 smp->data.u.sint = cnt;
1270 smp->flags = SMP_F_VOL_HDR;
1271 return 1;
1272}
1273
1274static int smp_fetch_hdr_names(const struct arg *args, struct sample *smp, const char *kw, void *private)
1275{
Willy Tarreau79e57332018-10-02 16:01:16 +02001276 struct buffer *temp;
1277 char del = ',';
1278
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001279 if (IS_HTX_SMP(smp) || (smp->px->mode == PR_MODE_TCP)) {
1280 /* HTX version */
1281 struct htx *htx = smp_prefetch_htx(smp, args);
1282 int32_t pos;
Willy Tarreau79e57332018-10-02 16:01:16 +02001283
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001284 if (!htx)
1285 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001286
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001287 if (args && args->type == ARGT_STR)
1288 del = *args[0].data.str.area;
Willy Tarreau79e57332018-10-02 16:01:16 +02001289
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001290 temp = get_trash_chunk();
1291 for (pos = htx_get_head(htx); pos != -1; pos = htx_get_next(htx, pos)) {
1292 struct htx_blk *blk = htx_get_blk(htx, pos);
1293 enum htx_blk_type type = htx_get_blk_type(blk);
1294 struct ist n;
Willy Tarreau79e57332018-10-02 16:01:16 +02001295
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001296 if (type == HTX_BLK_EOH)
1297 break;
1298 if (type != HTX_BLK_HDR)
1299 continue;
1300 n = htx_get_blk_name(htx, blk);
1301
1302 if (temp->data)
1303 temp->area[temp->data++] = del;
1304 chunk_memcat(temp, n.ptr, n.len);
1305 }
1306 }
1307 else {
1308 /* LEGACY version */
1309 struct hdr_idx *idx;
1310 struct hdr_ctx ctx;
1311 const struct http_msg *msg;
1312
1313 if (args && args->type == ARGT_STR)
1314 del = *args[0].data.str.area;
1315
1316 CHECK_HTTP_MESSAGE_FIRST();
1317
1318 idx = &smp->strm->txn->hdr_idx;
1319 msg = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &smp->strm->txn->req : &smp->strm->txn->rsp;
1320
1321 temp = get_trash_chunk();
1322
1323 ctx.idx = 0;
1324 while (http_find_next_header(ci_head(msg->chn), idx, &ctx)) {
1325 if (temp->data)
1326 temp->area[temp->data++] = del;
1327 memcpy(temp->area + temp->data, ctx.line, ctx.del);
1328 temp->data += ctx.del;
1329 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001330 }
1331
1332 smp->data.type = SMP_T_STR;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001333 smp->data.u.str = *temp;
Willy Tarreau79e57332018-10-02 16:01:16 +02001334 smp->flags = SMP_F_VOL_HDR;
1335 return 1;
1336}
1337
1338/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
1339 * Accepts an optional argument of type string containing the header field name,
1340 * and an optional argument of type signed or unsigned integer to request an
1341 * explicit occurrence of the header. Note that in the event of a missing name,
1342 * headers are considered from the first one.
1343 */
1344static int smp_fetch_hdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
1345{
Willy Tarreau79e57332018-10-02 16:01:16 +02001346 int occ = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001347
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001348 if (IS_HTX_SMP(smp) || (smp->px->mode == PR_MODE_TCP)) {
1349 /* HTX version */
1350 struct htx *htx = smp_prefetch_htx(smp, args);
1351 struct http_hdr_ctx *ctx = smp->ctx.a[0];
1352 struct ist name;
1353
1354 if (!ctx) {
1355 /* first call */
1356 ctx = &static_http_hdr_ctx;
1357 ctx->blk = NULL;
1358 smp->ctx.a[0] = ctx;
1359 }
1360
1361 if (args) {
1362 if (args[0].type != ARGT_STR)
1363 return 0;
1364 name.ptr = args[0].data.str.area;
1365 name.len = args[0].data.str.data;
1366
1367 if (args[1].type == ARGT_SINT)
1368 occ = args[1].data.sint;
1369 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001370
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001371 if (!htx)
Willy Tarreau79e57332018-10-02 16:01:16 +02001372 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001373
1374 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
1375 /* search for header from the beginning */
1376 ctx->blk = NULL;
1377
1378 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
1379 /* no explicit occurrence and single fetch => last header by default */
1380 occ = -1;
1381
1382 if (!occ)
1383 /* prepare to report multiple occurrences for ACL fetches */
1384 smp->flags |= SMP_F_NOT_LAST;
Willy Tarreau79e57332018-10-02 16:01:16 +02001385
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001386 smp->data.type = SMP_T_STR;
1387 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
1388 if (http_get_htx_hdr(htx, name, occ, ctx, &smp->data.u.str.area, &smp->data.u.str.data))
1389 return 1;
Willy Tarreau79e57332018-10-02 16:01:16 +02001390 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001391 else {
1392 /* LEGACY version */
1393 struct hdr_idx *idx;
1394 struct hdr_ctx *ctx = smp->ctx.a[0];
1395 const struct http_msg *msg;
1396 const char *name_str = NULL;
1397 int name_len = 0;
1398
1399 if (!ctx) {
1400 /* first call */
1401 ctx = &static_hdr_ctx;
1402 ctx->idx = 0;
1403 smp->ctx.a[0] = ctx;
1404 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001405
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001406 if (args) {
1407 if (args[0].type != ARGT_STR)
1408 return 0;
1409 name_str = args[0].data.str.area;
1410 name_len = args[0].data.str.data;
Willy Tarreau79e57332018-10-02 16:01:16 +02001411
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001412 if (args[1].type == ARGT_SINT)
1413 occ = args[1].data.sint;
1414 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001415
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001416 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau79e57332018-10-02 16:01:16 +02001417
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001418 idx = &smp->strm->txn->hdr_idx;
1419 msg = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &smp->strm->txn->req : &smp->strm->txn->rsp;
Willy Tarreau79e57332018-10-02 16:01:16 +02001420
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001421 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
1422 /* search for header from the beginning */
1423 ctx->idx = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001424
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001425 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
1426 /* no explicit occurrence and single fetch => last header by default */
1427 occ = -1;
1428
1429 if (!occ)
1430 /* prepare to report multiple occurrences for ACL fetches */
1431 smp->flags |= SMP_F_NOT_LAST;
1432
1433 smp->data.type = SMP_T_STR;
1434 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
1435 if (http_get_hdr(msg, name_str, name_len, idx, occ, ctx, &smp->data.u.str.area, &smp->data.u.str.data))
1436 return 1;
1437 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001438
1439 smp->flags &= ~SMP_F_NOT_LAST;
1440 return 0;
1441}
1442
1443/* 6. Check on HTTP header count. The number of occurrences is returned.
1444 * Accepts exactly 1 argument of type string.
1445 */
1446static int smp_fetch_hdr_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
1447{
Willy Tarreau79e57332018-10-02 16:01:16 +02001448 int cnt;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001449
1450 if (IS_HTX_SMP(smp) || (smp->px->mode == PR_MODE_TCP)) {
1451 /* HTX version */
1452 struct htx *htx = smp_prefetch_htx(smp, args);
1453 struct http_hdr_ctx ctx;
1454 struct ist name;
1455
1456 if (!htx)
1457 return 0;
1458
1459 if (args && args->type == ARGT_STR) {
1460 name.ptr = args->data.str.area;
1461 name.len = args->data.str.data;
Olivier Houcharde2c78cd2018-11-21 13:49:48 +01001462 } else {
1463 name.ptr = NULL;
1464 name.len = 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001465 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001466
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001467 ctx.blk = NULL;
1468 cnt = 0;
1469 while (http_find_header(htx, name, &ctx, 0))
1470 cnt++;
Willy Tarreau79e57332018-10-02 16:01:16 +02001471 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001472 else {
1473 /* LEGACY version */
1474 struct hdr_idx *idx;
1475 struct hdr_ctx ctx;
1476 const struct http_msg *msg;
1477 const char *name = NULL;
1478 int len = 0;
1479
1480 if (args && args->type == ARGT_STR) {
1481 name = args->data.str.area;
1482 len = args->data.str.data;
1483 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001484
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001485 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau79e57332018-10-02 16:01:16 +02001486
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001487 idx = &smp->strm->txn->hdr_idx;
1488 msg = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &smp->strm->txn->req : &smp->strm->txn->rsp;
Willy Tarreau79e57332018-10-02 16:01:16 +02001489
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001490 ctx.idx = 0;
1491 cnt = 0;
1492 while (http_find_header2(name, len, ci_head(msg->chn), idx, &ctx))
1493 cnt++;
1494 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001495
1496 smp->data.type = SMP_T_SINT;
1497 smp->data.u.sint = cnt;
1498 smp->flags = SMP_F_VOL_HDR;
1499 return 1;
1500}
1501
1502/* Fetch an HTTP header's integer value. The integer value is returned. It
1503 * takes a mandatory argument of type string and an optional one of type int
1504 * to designate a specific occurrence. It returns an unsigned integer, which
1505 * may or may not be appropriate for everything.
1506 */
1507static int smp_fetch_hdr_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
1508{
1509 int ret = smp_fetch_hdr(args, smp, kw, private);
1510
1511 if (ret > 0) {
1512 smp->data.type = SMP_T_SINT;
1513 smp->data.u.sint = strl2ic(smp->data.u.str.area,
1514 smp->data.u.str.data);
1515 }
1516
1517 return ret;
1518}
1519
Frédéric Lécaille1fceee82019-02-25 15:30:36 +01001520static inline struct buffer *
1521smp_fetch_body_buf(const struct arg *args, struct sample *smp)
1522{
1523 struct buffer *buf;
1524
1525 if (IS_HTX_SMP(smp) || (smp->px->mode == PR_MODE_TCP)) {
1526 /* HTX version */
1527 struct htx *htx = smp_prefetch_htx(smp, args);
1528 int32_t pos;
1529
1530 if (!htx)
1531 return NULL;
1532
1533 buf = get_trash_chunk();
1534 for (pos = htx_get_head(htx); pos != -1; pos = htx_get_next(htx, pos)) {
1535 struct htx_blk *blk = htx_get_blk(htx, pos);
1536 enum htx_blk_type type = htx_get_blk_type(blk);
1537
1538 if (type == HTX_BLK_EOM || type == HTX_BLK_EOD)
1539 break;
1540 if (type == HTX_BLK_DATA) {
1541 if (!htx_data_to_h1(htx_get_blk_value(htx, blk), buf, 0))
1542 return NULL;
1543 }
1544 }
1545 }
1546 else {
1547 /* LEGACY version */
1548 struct http_msg *msg;
1549 unsigned long len;
1550 unsigned long block1;
1551 char *body;
1552
1553 if (smp_prefetch_http(smp->px, smp->strm, smp->opt, args, smp, 1) <= 0)
1554 return NULL;
1555
1556 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
1557 msg = &smp->strm->txn->req;
1558 else
1559 msg = &smp->strm->txn->rsp;
1560
1561 len = http_body_bytes(msg);
1562 body = c_ptr(msg->chn, -http_data_rewind(msg));
1563
1564 block1 = len;
1565 if (block1 > b_wrap(&msg->chn->buf) - body)
1566 block1 = b_wrap(&msg->chn->buf) - body;
1567
1568 buf = get_trash_chunk();
1569 if (block1 == len) {
1570 /* buffer is not wrapped (or empty) */
1571 memcpy(buf->area, body, len);
1572 }
1573 else {
1574 /* buffer is wrapped, we need to defragment it */
1575 memcpy(buf->area, body, block1);
1576 memcpy(buf->area + block1, b_orig(&msg->chn->buf),
1577 len - block1);
1578 }
1579 buf->data = len;
1580 }
1581
1582 return buf;
1583}
1584
1585#define GRPC_MSG_COMPRESS_FLAG_SZ 1 /* 1 byte */
1586#define GRPC_MSG_LENGTH_SZ 4 /* 4 bytes */
1587#define GRPC_MSG_HEADER_SZ (GRPC_MSG_COMPRESS_FLAG_SZ + GRPC_MSG_LENGTH_SZ)
1588
1589/*
1590 * Fetch a gRPC field value. Takes a mandatory argument: the field identifier
1591 * (dotted notation) internally represented as an array of unsigned integers
1592 * and its size.
1593 * Return 1 if the field was found, 0 if not.
1594 */
1595static int smp_fetch_req_ungrpc(const struct arg *args, struct sample *smp, const char *kw, void *private)
1596{
1597 struct buffer *body;
1598 unsigned char *pos;
1599 size_t grpc_left;
1600 unsigned int *fid;
1601 size_t fid_sz;
1602
1603 if (!smp->strm)
1604 return 0;
1605
1606 fid = args[0].data.fid.ids;
1607 fid_sz = args[0].data.fid.sz;
1608
1609 body = smp_fetch_body_buf(args, smp);
1610 if (!body)
1611 return 0;
1612
1613 pos = (unsigned char *)body->area;
1614 /* Remaining bytes in the body to be parsed. */
1615 grpc_left = body->data;
1616
1617 while (grpc_left > GRPC_MSG_COMPRESS_FLAG_SZ + GRPC_MSG_LENGTH_SZ) {
1618 int next_field, found;
1619 size_t grpc_msg_len, left;
1620 unsigned int wire_type, field_number;
1621 uint64_t key, elen;
1622
1623 grpc_msg_len = left = ntohl(*(uint32_t *)(pos + GRPC_MSG_COMPRESS_FLAG_SZ));
1624
1625 pos += GRPC_MSG_HEADER_SZ;
1626 grpc_left -= GRPC_MSG_HEADER_SZ;
1627
1628 if (grpc_left < left)
1629 return 0;
1630
1631 found = 1;
1632 /* Length of the length-delimited messages if any. */
1633 elen = 0;
1634
1635 /* Message decoding: there may be serveral key+value protobuf pairs by
1636 * gRPC message.
1637 */
1638 next_field = 0;
1639 while (next_field < fid_sz) {
1640 uint64_t sleft;
1641
1642 if ((ssize_t)left <= 0)
1643 return 0;
1644
1645 /* Remaining bytes saving. */
1646 sleft = left;
1647
1648 /* Key decoding */
1649 if (!protobuf_decode_varint(&key, &pos, &left))
1650 return 0;
1651
1652 wire_type = key & 0x7;
1653 field_number = key >> 3;
1654 found = field_number == fid[next_field];
1655
1656 if (found && field_number != fid[next_field])
1657 found = 0;
1658
1659 switch (wire_type) {
1660 case PBUF_TYPE_VARINT:
1661 {
1662 if (!found) {
1663 protobuf_skip_varint(&pos, &left);
1664 } else if (next_field == fid_sz - 1) {
1665 int varint_len;
1666 unsigned char *spos = pos;
1667
1668 varint_len = protobuf_varint_getlen(&pos, &left);
1669 if (varint_len == -1)
1670 return 0;
1671
1672 smp->data.type = SMP_T_BIN;
1673 smp->data.u.str.area = (char *)spos;
1674 smp->data.u.str.data = varint_len;
1675 smp->flags = SMP_F_VOL_TEST;
1676 return 1;
1677 }
1678 break;
1679 }
1680
1681 case PBUF_TYPE_64BIT:
1682 {
1683 if (!found) {
1684 pos += sizeof(uint64_t);
1685 left -= sizeof(uint64_t);
1686 } else if (next_field == fid_sz - 1) {
1687 smp->data.type = SMP_T_BIN;
1688 smp->data.u.str.area = (char *)pos;
1689 smp->data.u.str.data = sizeof(uint64_t);
1690 smp->flags = SMP_F_VOL_TEST;
1691 return 1;
1692 }
1693 break;
1694 }
1695
1696 case PBUF_TYPE_LENGTH_DELIMITED:
1697 {
1698 /* Decode the length of this length-delimited field. */
1699 if (!protobuf_decode_varint(&elen, &pos, &left))
1700 return 0;
1701
1702 if (elen > left)
1703 return 0;
1704
1705 /* The size of the current field is computed from here do skip
1706 * the bytes to encode the previous lenght.*
1707 */
1708 sleft = left;
1709 if (!found) {
1710 /* Skip the current length-delimited field. */
1711 pos += elen;
1712 left -= elen;
1713 break;
1714 } else if (next_field == fid_sz - 1) {
1715 smp->data.type = SMP_T_BIN;
1716 smp->data.u.str.area = (char *)pos;
1717 smp->data.u.str.data = elen;
1718 smp->flags = SMP_F_VOL_TEST;
1719 return 1;
1720 }
1721 break;
1722 }
1723
1724 case PBUF_TYPE_32BIT:
1725 {
1726 if (!found) {
1727 pos += sizeof(uint32_t);
1728 left -= sizeof(uint32_t);
1729 } else if (next_field == fid_sz - 1) {
1730 smp->data.type = SMP_T_BIN;
1731 smp->data.u.str.area = (char *)pos;
1732 smp->data.u.str.data = sizeof(uint32_t);
1733 smp->flags = SMP_F_VOL_TEST;
1734 return 1;
1735 }
1736 break;
1737 }
1738
1739 default:
1740 return 0;
1741 }
1742
1743 if ((ssize_t)(elen) > 0)
1744 elen -= sleft - left;
1745
1746 if (found) {
1747 next_field++;
1748 }
1749 else if ((ssize_t)elen <= 0) {
1750 next_field = 0;
1751 }
1752 }
1753 grpc_left -= grpc_msg_len;
1754 }
1755
1756 return 0;
1757}
1758
Willy Tarreau79e57332018-10-02 16:01:16 +02001759/* Fetch an HTTP header's IP value. takes a mandatory argument of type string
1760 * and an optional one of type int to designate a specific occurrence.
1761 * It returns an IPv4 or IPv6 address.
1762 */
1763static int smp_fetch_hdr_ip(const struct arg *args, struct sample *smp, const char *kw, void *private)
1764{
1765 int ret;
1766
1767 while ((ret = smp_fetch_hdr(args, smp, kw, private)) > 0) {
1768 if (url2ipv4((char *) smp->data.u.str.area, &smp->data.u.ipv4)) {
1769 smp->data.type = SMP_T_IPV4;
1770 break;
1771 } else {
1772 struct buffer *temp = get_trash_chunk();
1773 if (smp->data.u.str.data < temp->size - 1) {
1774 memcpy(temp->area, smp->data.u.str.area,
1775 smp->data.u.str.data);
1776 temp->area[smp->data.u.str.data] = '\0';
1777 if (inet_pton(AF_INET6, temp->area, &smp->data.u.ipv6)) {
1778 smp->data.type = SMP_T_IPV6;
1779 break;
1780 }
1781 }
1782 }
1783
1784 /* if the header doesn't match an IP address, fetch next one */
1785 if (!(smp->flags & SMP_F_NOT_LAST))
1786 return 0;
1787 }
1788 return ret;
1789}
1790
1791/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
1792 * the first '/' after the possible hostname, and ends before the possible '?'.
1793 */
1794static int smp_fetch_path(const struct arg *args, struct sample *smp, const char *kw, void *private)
1795{
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001796 if (IS_HTX_SMP(smp) || (smp->px->mode == PR_MODE_TCP)) {
1797 /* HTX version */
1798 struct htx *htx = smp_prefetch_htx(smp, args);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001799 struct htx_sl *sl;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001800 struct ist path;
1801 size_t len;
Willy Tarreau79e57332018-10-02 16:01:16 +02001802
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001803 if (!htx)
1804 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001805
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001806 sl = http_find_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001807 path = http_get_path(htx_sl_req_uri(sl));
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001808 if (!path.ptr)
1809 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001810
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001811 for (len = 0; len < path.len && *(path.ptr + len) != '?'; len++)
Dragan Dosen8861e1c2019-02-12 19:50:31 +01001812 ;
Willy Tarreau79e57332018-10-02 16:01:16 +02001813
Dragan Dosen8861e1c2019-02-12 19:50:31 +01001814 /* OK, we got the '/' ! */
1815 smp->data.type = SMP_T_STR;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001816 smp->data.u.str.area = path.ptr;
1817 smp->data.u.str.data = len;
1818 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
1819 }
1820 else {
1821 struct http_txn *txn;
1822 char *ptr, *end;
Willy Tarreau79e57332018-10-02 16:01:16 +02001823
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001824 CHECK_HTTP_MESSAGE_FIRST();
1825
1826 txn = smp->strm->txn;
1827 end = ci_head(txn->req.chn) + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
1828 ptr = http_txn_get_path(txn);
1829 if (!ptr)
1830 return 0;
1831
1832 /* OK, we got the '/' ! */
1833 smp->data.type = SMP_T_STR;
1834 smp->data.u.str.area = ptr;
1835
1836 while (ptr < end && *ptr != '?')
1837 ptr++;
1838
1839 smp->data.u.str.data = ptr - smp->data.u.str.area;
1840 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
1841 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001842 return 1;
1843}
1844
1845/* This produces a concatenation of the first occurrence of the Host header
1846 * followed by the path component if it begins with a slash ('/'). This means
1847 * that '*' will not be added, resulting in exactly the first Host entry.
1848 * If no Host header is found, then the path is returned as-is. The returned
1849 * value is stored in the trash so it does not need to be marked constant.
1850 * The returned sample is of type string.
1851 */
1852static int smp_fetch_base(const struct arg *args, struct sample *smp, const char *kw, void *private)
1853{
Willy Tarreau79e57332018-10-02 16:01:16 +02001854 struct buffer *temp;
1855
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001856 if (IS_HTX_SMP(smp) || (smp->px->mode == PR_MODE_TCP)) {
1857 /* HTX version */
1858 struct htx *htx = smp_prefetch_htx(smp, args);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001859 struct htx_sl *sl;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001860 struct http_hdr_ctx ctx;
1861 struct ist path;
Willy Tarreau79e57332018-10-02 16:01:16 +02001862
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001863 if (!htx)
1864 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001865
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001866 ctx.blk = NULL;
1867 if (!http_find_header(htx, ist("Host"), &ctx, 0) || !ctx.value.len)
1868 return smp_fetch_path(args, smp, kw, private);
Willy Tarreau79e57332018-10-02 16:01:16 +02001869
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001870 /* OK we have the header value in ctx.value */
1871 temp = get_trash_chunk();
1872 chunk_memcat(temp, ctx.value.ptr, ctx.value.len);
1873
1874 /* now retrieve the path */
1875 sl = http_find_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001876 path = http_get_path(htx_sl_req_uri(sl));
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001877 if (path.ptr) {
1878 size_t len;
1879
Dragan Dosen8861e1c2019-02-12 19:50:31 +01001880 for (len = 0; len < path.len && *(path.ptr + len) != '?'; len++)
1881 ;
1882
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001883 if (len && *(path.ptr) == '/')
1884 chunk_memcat(temp, path.ptr, len);
1885 }
1886
1887 smp->data.type = SMP_T_STR;
1888 smp->data.u.str = *temp;
1889 }
1890 else {
1891 /* LEGACY version */
1892 struct http_txn *txn;
1893 char *ptr, *end, *beg;
1894 struct hdr_ctx ctx;
1895
1896 CHECK_HTTP_MESSAGE_FIRST();
1897
1898 txn = smp->strm->txn;
1899 ctx.idx = 0;
1900 if (!http_find_header2("Host", 4, ci_head(txn->req.chn), &txn->hdr_idx, &ctx) || !ctx.vlen)
1901 return smp_fetch_path(args, smp, kw, private);
1902
1903 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
1904 temp = get_trash_chunk();
1905 memcpy(temp->area, ctx.line + ctx.val, ctx.vlen);
1906 smp->data.type = SMP_T_STR;
1907 smp->data.u.str.area = temp->area;
1908 smp->data.u.str.data = ctx.vlen;
Willy Tarreau79e57332018-10-02 16:01:16 +02001909
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001910 /* now retrieve the path */
1911 end = ci_head(txn->req.chn) + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
1912 beg = http_txn_get_path(txn);
1913 if (!beg)
1914 beg = end;
Willy Tarreau79e57332018-10-02 16:01:16 +02001915
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001916 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
1917
1918 if (beg < ptr && *beg == '/') {
1919 memcpy(smp->data.u.str.area + smp->data.u.str.data, beg,
1920 ptr - beg);
1921 smp->data.u.str.data += ptr - beg;
1922 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001923 }
1924
1925 smp->flags = SMP_F_VOL_1ST;
1926 return 1;
1927}
1928
1929/* This produces a 32-bit hash of the concatenation of the first occurrence of
1930 * the Host header followed by the path component if it begins with a slash ('/').
1931 * This means that '*' will not be added, resulting in exactly the first Host
1932 * entry. If no Host header is found, then the path is used. The resulting value
1933 * is hashed using the path hash followed by a full avalanche hash and provides a
1934 * 32-bit integer value. This fetch is useful for tracking per-path activity on
1935 * high-traffic sites without having to store whole paths.
1936 */
1937static int smp_fetch_base32(const struct arg *args, struct sample *smp, const char *kw, void *private)
1938{
Willy Tarreau79e57332018-10-02 16:01:16 +02001939 unsigned int hash = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001940
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001941 if (IS_HTX_SMP(smp) || (smp->px->mode == PR_MODE_TCP)) {
1942 /* HTX version */
1943 struct htx *htx = smp_prefetch_htx(smp, args);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001944 struct htx_sl *sl;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001945 struct http_hdr_ctx ctx;
1946 struct ist path;
Willy Tarreau79e57332018-10-02 16:01:16 +02001947
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001948 if (!htx)
1949 return 0;
1950
1951 ctx.blk = NULL;
Dragan Dosen8861e1c2019-02-12 19:50:31 +01001952 if (http_find_header(htx, ist("Host"), &ctx, 0)) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001953 /* OK we have the header value in ctx.value */
1954 while (ctx.value.len--)
1955 hash = *(ctx.value.ptr++) + (hash << 6) + (hash << 16) - hash;
1956 }
1957
1958 /* now retrieve the path */
1959 sl = http_find_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001960 path = http_get_path(htx_sl_req_uri(sl));
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001961 if (path.ptr) {
1962 size_t len;
1963
Dragan Dosen8861e1c2019-02-12 19:50:31 +01001964 for (len = 0; len < path.len && *(path.ptr + len) != '?'; len++)
1965 ;
1966
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001967 if (len && *(path.ptr) == '/') {
1968 while (len--)
1969 hash = *(path.ptr++) + (hash << 6) + (hash << 16) - hash;
1970 }
1971 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001972 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001973 else {
1974 /* LEGACY version */
1975 struct http_txn *txn;
1976 struct hdr_ctx ctx;
1977 char *ptr, *beg, *end;
1978 int len;
1979
1980 CHECK_HTTP_MESSAGE_FIRST();
1981
1982 txn = smp->strm->txn;
1983 ctx.idx = 0;
1984 if (http_find_header2("Host", 4, ci_head(txn->req.chn), &txn->hdr_idx, &ctx)) {
1985 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
1986 ptr = ctx.line + ctx.val;
1987 len = ctx.vlen;
1988 while (len--)
1989 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
1990 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001991
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001992 /* now retrieve the path */
1993 end = ci_head(txn->req.chn) + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
1994 beg = http_txn_get_path(txn);
1995 if (!beg)
1996 beg = end;
Willy Tarreau79e57332018-10-02 16:01:16 +02001997
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001998 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
Willy Tarreau79e57332018-10-02 16:01:16 +02001999
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002000 if (beg < ptr && *beg == '/') {
2001 while (beg < ptr)
2002 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
2003 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002004 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002005
Willy Tarreau79e57332018-10-02 16:01:16 +02002006 hash = full_hash(hash);
2007
2008 smp->data.type = SMP_T_SINT;
2009 smp->data.u.sint = hash;
2010 smp->flags = SMP_F_VOL_1ST;
2011 return 1;
2012}
2013
2014/* This concatenates the source address with the 32-bit hash of the Host and
2015 * path as returned by smp_fetch_base32(). The idea is to have per-source and
2016 * per-path counters. The result is a binary block from 8 to 20 bytes depending
2017 * on the source address length. The path hash is stored before the address so
2018 * that in environments where IPv6 is insignificant, truncating the output to
2019 * 8 bytes would still work.
2020 */
2021static int smp_fetch_base32_src(const struct arg *args, struct sample *smp, const char *kw, void *private)
2022{
2023 struct buffer *temp;
2024 struct connection *cli_conn = objt_conn(smp->sess->origin);
2025
2026 if (!cli_conn)
2027 return 0;
2028
2029 if (!smp_fetch_base32(args, smp, kw, private))
2030 return 0;
2031
2032 temp = get_trash_chunk();
2033 *(unsigned int *) temp->area = htonl(smp->data.u.sint);
2034 temp->data += sizeof(unsigned int);
2035
2036 switch (cli_conn->addr.from.ss_family) {
2037 case AF_INET:
2038 memcpy(temp->area + temp->data,
2039 &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr,
2040 4);
2041 temp->data += 4;
2042 break;
2043 case AF_INET6:
2044 memcpy(temp->area + temp->data,
2045 &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr,
2046 16);
2047 temp->data += 16;
2048 break;
2049 default:
2050 return 0;
2051 }
2052
2053 smp->data.u.str = *temp;
2054 smp->data.type = SMP_T_BIN;
2055 return 1;
2056}
2057
2058/* Extracts the query string, which comes after the question mark '?'. If no
2059 * question mark is found, nothing is returned. Otherwise it returns a sample
2060 * of type string carrying the whole query string.
2061 */
2062static int smp_fetch_query(const struct arg *args, struct sample *smp, const char *kw, void *private)
2063{
Willy Tarreau79e57332018-10-02 16:01:16 +02002064 char *ptr, *end;
2065
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002066 if (IS_HTX_SMP(smp) || (smp->px->mode == PR_MODE_TCP)) {
2067 /* HTX version */
2068 struct htx *htx = smp_prefetch_htx(smp, args);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01002069 struct htx_sl *sl;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002070
2071 if (!htx)
2072 return 0;
2073
2074 sl = http_find_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01002075 ptr = HTX_SL_REQ_UPTR(sl);
2076 end = HTX_SL_REQ_UPTR(sl) + HTX_SL_REQ_ULEN(sl);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002077 }
2078 else {
2079 /* LEGACY version */
2080 struct http_txn *txn;
2081
2082 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau79e57332018-10-02 16:01:16 +02002083
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002084 txn = smp->strm->txn;
2085 ptr = ci_head(txn->req.chn) + txn->req.sl.rq.u;
2086 end = ptr + txn->req.sl.rq.u_l;
2087 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002088
2089 /* look up the '?' */
2090 do {
2091 if (ptr == end)
2092 return 0;
2093 } while (*ptr++ != '?');
2094
2095 smp->data.type = SMP_T_STR;
2096 smp->data.u.str.area = ptr;
2097 smp->data.u.str.data = end - ptr;
2098 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
2099 return 1;
2100}
2101
2102static int smp_fetch_proto_http(const struct arg *args, struct sample *smp, const char *kw, void *private)
2103{
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002104 if (IS_HTX_SMP(smp) || (smp->px->mode == PR_MODE_TCP)) {
2105 /* HTX version */
2106 struct htx *htx = smp_prefetch_htx(smp, args);
Willy Tarreau79e57332018-10-02 16:01:16 +02002107
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002108 if (!htx)
2109 return 0;
2110 }
2111 else {
2112 /* LEGACY version */
Willy Tarreau79e57332018-10-02 16:01:16 +02002113
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002114 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
2115 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
2116 */
2117 CHECK_HTTP_MESSAGE_FIRST_PERM();
2118 }
2119 smp->data.type = SMP_T_BOOL;
Willy Tarreau79e57332018-10-02 16:01:16 +02002120 smp->data.u.sint = 1;
2121 return 1;
2122}
2123
2124/* return a valid test if the current request is the first one on the connection */
2125static int smp_fetch_http_first_req(const struct arg *args, struct sample *smp, const char *kw, void *private)
2126{
2127 smp->data.type = SMP_T_BOOL;
2128 smp->data.u.sint = !(smp->strm->txn->flags & TX_NOT_FIRST);
2129 return 1;
2130}
2131
2132/* Accepts exactly 1 argument of type userlist */
2133static int smp_fetch_http_auth(const struct arg *args, struct sample *smp, const char *kw, void *private)
2134{
2135
2136 if (!args || args->type != ARGT_USR)
2137 return 0;
2138
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002139 if (IS_HTX_SMP(smp) || (smp->px->mode == PR_MODE_TCP)) {
2140 /* HTX version */
2141 struct htx *htx = smp_prefetch_htx(smp, args);
Willy Tarreau79e57332018-10-02 16:01:16 +02002142
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002143 if (!htx)
2144 return 0;
2145 }
2146 else {
2147 /* LEGACY version */
2148 CHECK_HTTP_MESSAGE_FIRST();
2149 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002150
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002151 if (!get_http_auth(smp))
2152 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02002153 smp->data.type = SMP_T_BOOL;
2154 smp->data.u.sint = check_user(args->data.usr, smp->strm->txn->auth.user,
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002155 smp->strm->txn->auth.pass);
Willy Tarreau79e57332018-10-02 16:01:16 +02002156 return 1;
2157}
2158
2159/* Accepts exactly 1 argument of type userlist */
2160static int smp_fetch_http_auth_grp(const struct arg *args, struct sample *smp, const char *kw, void *private)
2161{
2162 if (!args || args->type != ARGT_USR)
2163 return 0;
2164
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002165 if (IS_HTX_SMP(smp) || (smp->px->mode == PR_MODE_TCP)) {
2166 /* HTX version */
2167 struct htx *htx = smp_prefetch_htx(smp, args);
2168
2169 if (!htx)
2170 return 0;
2171 }
2172 else {
2173 /* LEGACY version */
2174 CHECK_HTTP_MESSAGE_FIRST();
2175 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002176
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002177 if (!get_http_auth(smp))
Willy Tarreau79e57332018-10-02 16:01:16 +02002178 return 0;
2179
2180 /* if the user does not belong to the userlist or has a wrong password,
2181 * report that it unconditionally does not match. Otherwise we return
2182 * a string containing the username.
2183 */
2184 if (!check_user(args->data.usr, smp->strm->txn->auth.user,
2185 smp->strm->txn->auth.pass))
2186 return 0;
2187
2188 /* pat_match_auth() will need the user list */
2189 smp->ctx.a[0] = args->data.usr;
2190
2191 smp->data.type = SMP_T_STR;
2192 smp->flags = SMP_F_CONST;
2193 smp->data.u.str.area = smp->strm->txn->auth.user;
2194 smp->data.u.str.data = strlen(smp->strm->txn->auth.user);
2195
2196 return 1;
2197}
2198
2199/* Fetch a captured HTTP request header. The index is the position of
2200 * the "capture" option in the configuration file
2201 */
2202static int smp_fetch_capture_req_hdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
2203{
2204 struct proxy *fe = strm_fe(smp->strm);
2205 int idx;
2206
2207 if (!args || args->type != ARGT_SINT)
2208 return 0;
2209
2210 idx = args->data.sint;
2211
2212 if (idx > (fe->nb_req_cap - 1) || smp->strm->req_cap == NULL || smp->strm->req_cap[idx] == NULL)
2213 return 0;
2214
2215 smp->data.type = SMP_T_STR;
2216 smp->flags |= SMP_F_CONST;
2217 smp->data.u.str.area = smp->strm->req_cap[idx];
2218 smp->data.u.str.data = strlen(smp->strm->req_cap[idx]);
2219
2220 return 1;
2221}
2222
2223/* Fetch a captured HTTP response header. The index is the position of
2224 * the "capture" option in the configuration file
2225 */
2226static int smp_fetch_capture_res_hdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
2227{
2228 struct proxy *fe = strm_fe(smp->strm);
2229 int idx;
2230
2231 if (!args || args->type != ARGT_SINT)
2232 return 0;
2233
2234 idx = args->data.sint;
2235
2236 if (idx > (fe->nb_rsp_cap - 1) || smp->strm->res_cap == NULL || smp->strm->res_cap[idx] == NULL)
2237 return 0;
2238
2239 smp->data.type = SMP_T_STR;
2240 smp->flags |= SMP_F_CONST;
2241 smp->data.u.str.area = smp->strm->res_cap[idx];
2242 smp->data.u.str.data = strlen(smp->strm->res_cap[idx]);
2243
2244 return 1;
2245}
2246
2247/* Extracts the METHOD in the HTTP request, the txn->uri should be filled before the call */
2248static int smp_fetch_capture_req_method(const struct arg *args, struct sample *smp, const char *kw, void *private)
2249{
2250 struct buffer *temp;
2251 struct http_txn *txn = smp->strm->txn;
2252 char *ptr;
2253
2254 if (!txn || !txn->uri)
2255 return 0;
2256
2257 ptr = txn->uri;
2258
2259 while (*ptr != ' ' && *ptr != '\0') /* find first space */
2260 ptr++;
2261
2262 temp = get_trash_chunk();
2263 temp->area = txn->uri;
2264 temp->data = ptr - txn->uri;
2265 smp->data.u.str = *temp;
2266 smp->data.type = SMP_T_STR;
2267 smp->flags = SMP_F_CONST;
2268
2269 return 1;
2270
2271}
2272
2273/* Extracts the path in the HTTP request, the txn->uri should be filled before the call */
2274static int smp_fetch_capture_req_uri(const struct arg *args, struct sample *smp, const char *kw, void *private)
2275{
2276 struct http_txn *txn = smp->strm->txn;
2277 struct ist path;
2278 const char *ptr;
2279
2280 if (!txn || !txn->uri)
2281 return 0;
2282
2283 ptr = txn->uri;
2284
2285 while (*ptr != ' ' && *ptr != '\0') /* find first space */
2286 ptr++;
2287
2288 if (!*ptr)
2289 return 0;
2290
Christopher Faulet78337bb2018-11-15 14:35:18 +01002291 /* skip the first space and find space after URI */
2292 path = ist2(++ptr, 0);
2293 while (*ptr != ' ' && *ptr != '\0')
2294 ptr++;
2295 path.len = ptr - path.ptr;
Willy Tarreau79e57332018-10-02 16:01:16 +02002296
Christopher Faulet78337bb2018-11-15 14:35:18 +01002297 path = http_get_path(path);
Willy Tarreau79e57332018-10-02 16:01:16 +02002298 if (!path.ptr)
2299 return 0;
2300
2301 smp->data.u.str.area = path.ptr;
2302 smp->data.u.str.data = path.len;
2303 smp->data.type = SMP_T_STR;
2304 smp->flags = SMP_F_CONST;
2305
2306 return 1;
2307}
2308
2309/* Retrieves the HTTP version from the request (either 1.0 or 1.1) and emits it
2310 * as a string (either "HTTP/1.0" or "HTTP/1.1").
2311 */
2312static int smp_fetch_capture_req_ver(const struct arg *args, struct sample *smp, const char *kw, void *private)
2313{
2314 struct http_txn *txn = smp->strm->txn;
2315
2316 if (!txn || txn->req.msg_state < HTTP_MSG_HDR_FIRST)
2317 return 0;
2318
2319 if (txn->req.flags & HTTP_MSGF_VER_11)
2320 smp->data.u.str.area = "HTTP/1.1";
2321 else
2322 smp->data.u.str.area = "HTTP/1.0";
2323
2324 smp->data.u.str.data = 8;
2325 smp->data.type = SMP_T_STR;
2326 smp->flags = SMP_F_CONST;
2327 return 1;
2328
2329}
2330
2331/* Retrieves the HTTP version from the response (either 1.0 or 1.1) and emits it
2332 * as a string (either "HTTP/1.0" or "HTTP/1.1").
2333 */
2334static int smp_fetch_capture_res_ver(const struct arg *args, struct sample *smp, const char *kw, void *private)
2335{
2336 struct http_txn *txn = smp->strm->txn;
2337
2338 if (!txn || txn->rsp.msg_state < HTTP_MSG_HDR_FIRST)
2339 return 0;
2340
2341 if (txn->rsp.flags & HTTP_MSGF_VER_11)
2342 smp->data.u.str.area = "HTTP/1.1";
2343 else
2344 smp->data.u.str.area = "HTTP/1.0";
2345
2346 smp->data.u.str.data = 8;
2347 smp->data.type = SMP_T_STR;
2348 smp->flags = SMP_F_CONST;
2349 return 1;
2350
2351}
2352
2353/* Iterate over all cookies present in a message. The context is stored in
2354 * smp->ctx.a[0] for the in-header position, smp->ctx.a[1] for the
2355 * end-of-header-value, and smp->ctx.a[2] for the hdr_ctx. Depending on
2356 * the direction, multiple cookies may be parsed on the same line or not.
2357 * The cookie name is in args and the name length in args->data.str.len.
2358 * Accepts exactly 1 argument of type string. If the input options indicate
2359 * that no iterating is desired, then only last value is fetched if any.
2360 * The returned sample is of type CSTR. Can be used to parse cookies in other
2361 * files.
2362 */
2363static int smp_fetch_cookie(const struct arg *args, struct sample *smp, const char *kw, void *private)
2364{
Willy Tarreau79e57332018-10-02 16:01:16 +02002365 int occ = 0;
2366 int found = 0;
2367
2368 if (!args || args->type != ARGT_STR)
2369 return 0;
2370
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002371 if (IS_HTX_SMP(smp) || (smp->px->mode == PR_MODE_TCP)) {
2372 /* HTX version */
2373 struct htx *htx = smp_prefetch_htx(smp, args);
2374 struct http_hdr_ctx *ctx = smp->ctx.a[2];
2375 struct ist hdr;
Willy Tarreau79e57332018-10-02 16:01:16 +02002376
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002377 if (!ctx) {
2378 /* first call */
2379 ctx = &static_http_hdr_ctx;
2380 ctx->blk = NULL;
2381 smp->ctx.a[2] = ctx;
2382 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002383
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002384 if (!htx)
2385 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02002386
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002387 hdr = (((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
2388 ? ist("Cookie")
2389 : ist("Set-Cookie"));
Willy Tarreau79e57332018-10-02 16:01:16 +02002390
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002391 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
2392 /* no explicit occurrence and single fetch => last cookie by default */
2393 occ = -1;
Willy Tarreau79e57332018-10-02 16:01:16 +02002394
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002395 /* OK so basically here, either we want only one value and it's the
2396 * last one, or we want to iterate over all of them and we fetch the
2397 * next one.
Willy Tarreau79e57332018-10-02 16:01:16 +02002398 */
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002399
2400 if (!(smp->flags & SMP_F_NOT_LAST)) {
2401 /* search for the header from the beginning, we must first initialize
2402 * the search parameters.
2403 */
2404 smp->ctx.a[0] = NULL;
2405 ctx->blk = NULL;
2406 }
2407
2408 smp->flags |= SMP_F_VOL_HDR;
2409 while (1) {
2410 /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */
2411 if (!smp->ctx.a[0]) {
2412 if (!http_find_header(htx, hdr, ctx, 0))
2413 goto out;
2414
2415 if (ctx->value.len < args->data.str.data + 1)
2416 continue;
2417
2418 smp->ctx.a[0] = ctx->value.ptr;
2419 smp->ctx.a[1] = smp->ctx.a[0] + ctx->value.len;
2420 }
2421
2422 smp->data.type = SMP_T_STR;
2423 smp->flags |= SMP_F_CONST;
2424 smp->ctx.a[0] = http_extract_cookie_value(smp->ctx.a[0], smp->ctx.a[1],
2425 args->data.str.area, args->data.str.data,
2426 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
2427 &smp->data.u.str.area,
2428 &smp->data.u.str.data);
2429 if (smp->ctx.a[0]) {
2430 found = 1;
2431 if (occ >= 0) {
2432 /* one value was returned into smp->data.u.str.{str,len} */
2433 smp->flags |= SMP_F_NOT_LAST;
2434 return 1;
2435 }
2436 }
2437 /* if we're looking for last occurrence, let's loop */
2438 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002439 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002440 else {
2441 /* LEGACY version */
2442 struct http_txn *txn;
2443 struct hdr_idx *idx;
2444 struct hdr_ctx *ctx = smp->ctx.a[2];
2445 const struct http_msg *msg;
2446 const char *hdr_name;
2447 int hdr_name_len;
2448 char *sol;
Willy Tarreau79e57332018-10-02 16:01:16 +02002449
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002450 if (!ctx) {
2451 /* first call */
2452 ctx = &static_hdr_ctx;
2453 ctx->idx = 0;
2454 smp->ctx.a[2] = ctx;
2455 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002456
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002457 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau79e57332018-10-02 16:01:16 +02002458
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002459 txn = smp->strm->txn;
2460 idx = &smp->strm->txn->hdr_idx;
Willy Tarreau79e57332018-10-02 16:01:16 +02002461
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002462 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
2463 msg = &txn->req;
2464 hdr_name = "Cookie";
2465 hdr_name_len = 6;
2466 } else {
2467 msg = &txn->rsp;
2468 hdr_name = "Set-Cookie";
2469 hdr_name_len = 10;
Willy Tarreau79e57332018-10-02 16:01:16 +02002470 }
2471
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002472 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
2473 /* no explicit occurrence and single fetch => last cookie by default */
2474 occ = -1;
2475
2476 /* OK so basically here, either we want only one value and it's the
2477 * last one, or we want to iterate over all of them and we fetch the
2478 * next one.
2479 */
2480
2481 sol = ci_head(msg->chn);
2482 if (!(smp->flags & SMP_F_NOT_LAST)) {
2483 /* search for the header from the beginning, we must first initialize
2484 * the search parameters.
2485 */
2486 smp->ctx.a[0] = NULL;
2487 ctx->idx = 0;
2488 }
2489
2490 smp->flags |= SMP_F_VOL_HDR;
2491
2492 while (1) {
2493 /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */
2494 if (!smp->ctx.a[0]) {
2495 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, ctx))
2496 goto out;
2497
2498 if (ctx->vlen < args->data.str.data + 1)
2499 continue;
2500
2501 smp->ctx.a[0] = ctx->line + ctx->val;
2502 smp->ctx.a[1] = smp->ctx.a[0] + ctx->vlen;
2503 }
2504
2505 smp->data.type = SMP_T_STR;
2506 smp->flags |= SMP_F_CONST;
2507 smp->ctx.a[0] = http_extract_cookie_value(smp->ctx.a[0], smp->ctx.a[1],
2508 args->data.str.area, args->data.str.data,
2509 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
2510 &smp->data.u.str.area, &smp->data.u.str.data);
2511 if (smp->ctx.a[0]) {
2512 found = 1;
2513 if (occ >= 0) {
2514 /* one value was returned into smp->data.u.str.{str,len} */
2515 smp->flags |= SMP_F_NOT_LAST;
2516 return 1;
2517 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002518 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002519 /* if we're looking for last occurrence, let's loop */
Willy Tarreau79e57332018-10-02 16:01:16 +02002520 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002521 }
2522 /* all cookie headers and values were scanned. If we're looking for the
2523 * last occurrence, we may return it now.
2524 */
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002525 out:
Willy Tarreau79e57332018-10-02 16:01:16 +02002526 smp->flags &= ~SMP_F_NOT_LAST;
2527 return found;
2528}
2529
2530/* Iterate over all cookies present in a request to count how many occurrences
2531 * match the name in args and args->data.str.len. If <multi> is non-null, then
2532 * multiple cookies may be parsed on the same line. The returned sample is of
2533 * type UINT. Accepts exactly 1 argument of type string.
2534 */
2535static int smp_fetch_cookie_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
2536{
Willy Tarreau79e57332018-10-02 16:01:16 +02002537 char *val_beg, *val_end;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002538 int cnt;
Willy Tarreau79e57332018-10-02 16:01:16 +02002539
2540 if (!args || args->type != ARGT_STR)
2541 return 0;
2542
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002543 if (IS_HTX_SMP(smp) || (smp->px->mode == PR_MODE_TCP)) {
2544 /* HTX version */
2545 struct htx *htx = smp_prefetch_htx(smp, args);
2546 struct http_hdr_ctx ctx;
2547 struct ist hdr;
2548
2549 if (!htx)
2550 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02002551
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002552 hdr = (((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
2553 ? ist("Cookie")
2554 : ist("Set-Cookie"));
Willy Tarreau79e57332018-10-02 16:01:16 +02002555
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002556 val_end = val_beg = NULL;
2557 ctx.blk = NULL;
2558 cnt = 0;
2559 while (1) {
2560 /* Note: val_beg == NULL every time we need to fetch a new header */
2561 if (!val_beg) {
2562 if (!http_find_header(htx, hdr, &ctx, 0))
2563 break;
Willy Tarreau79e57332018-10-02 16:01:16 +02002564
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002565 if (ctx.value.len < args->data.str.data + 1)
2566 continue;
Willy Tarreau79e57332018-10-02 16:01:16 +02002567
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002568 val_beg = ctx.value.ptr;
2569 val_end = val_beg + ctx.value.len;
2570 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002571
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002572 smp->data.type = SMP_T_STR;
2573 smp->flags |= SMP_F_CONST;
2574 while ((val_beg = http_extract_cookie_value(val_beg, val_end,
2575 args->data.str.area, args->data.str.data,
2576 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
2577 &smp->data.u.str.area,
2578 &smp->data.u.str.data))) {
2579 cnt++;
2580 }
2581 }
2582 }
2583 else {
2584 /* LEGACY version */
2585 struct http_txn *txn;
2586 struct hdr_idx *idx;
2587 struct hdr_ctx ctx;
2588 const struct http_msg *msg;
2589 const char *hdr_name;
2590 int hdr_name_len;
2591 char *sol;
Willy Tarreau79e57332018-10-02 16:01:16 +02002592
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002593 CHECK_HTTP_MESSAGE_FIRST();
2594
2595 txn = smp->strm->txn;
2596 idx = &smp->strm->txn->hdr_idx;
2597
2598 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
2599 msg = &txn->req;
2600 hdr_name = "Cookie";
2601 hdr_name_len = 6;
2602 } else {
2603 msg = &txn->rsp;
2604 hdr_name = "Set-Cookie";
2605 hdr_name_len = 10;
Willy Tarreau79e57332018-10-02 16:01:16 +02002606 }
2607
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002608 sol = ci_head(msg->chn);
2609 val_end = val_beg = NULL;
2610 ctx.idx = 0;
2611 cnt = 0;
2612
2613 while (1) {
2614 /* Note: val_beg == NULL every time we need to fetch a new header */
2615 if (!val_beg) {
2616 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, &ctx))
2617 break;
2618
2619 if (ctx.vlen < args->data.str.data + 1)
2620 continue;
2621
2622 val_beg = ctx.line + ctx.val;
2623 val_end = val_beg + ctx.vlen;
2624 }
2625
2626 smp->data.type = SMP_T_STR;
2627 smp->flags |= SMP_F_CONST;
2628 while ((val_beg = http_extract_cookie_value(val_beg, val_end,
2629 args->data.str.area, args->data.str.data,
2630 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
2631 &smp->data.u.str.area, &smp->data.u.str.data))) {
2632 cnt++;
2633 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002634 }
2635 }
2636
2637 smp->data.type = SMP_T_SINT;
2638 smp->data.u.sint = cnt;
2639 smp->flags |= SMP_F_VOL_HDR;
2640 return 1;
2641}
2642
2643/* Fetch an cookie's integer value. The integer value is returned. It
2644 * takes a mandatory argument of type string. It relies on smp_fetch_cookie().
2645 */
2646static int smp_fetch_cookie_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
2647{
2648 int ret = smp_fetch_cookie(args, smp, kw, private);
2649
2650 if (ret > 0) {
2651 smp->data.type = SMP_T_SINT;
2652 smp->data.u.sint = strl2ic(smp->data.u.str.area,
2653 smp->data.u.str.data);
2654 }
2655
2656 return ret;
2657}
2658
2659/************************************************************************/
2660/* The code below is dedicated to sample fetches */
2661/************************************************************************/
2662
2663/* This scans a URL-encoded query string. It takes an optionally wrapping
2664 * string whose first contigous chunk has its beginning in ctx->a[0] and end
2665 * in ctx->a[1], and the optional second part in (ctx->a[2]..ctx->a[3]). The
2666 * pointers are updated for next iteration before leaving.
2667 */
2668static int smp_fetch_param(char delim, const char *name, int name_len, const struct arg *args, struct sample *smp, const char *kw, void *private)
2669{
2670 const char *vstart, *vend;
2671 struct buffer *temp;
2672 const char **chunks = (const char **)smp->ctx.a;
2673
2674 if (!http_find_next_url_param(chunks, name, name_len,
2675 &vstart, &vend, delim))
2676 return 0;
2677
2678 /* Create sample. If the value is contiguous, return the pointer as CONST,
2679 * if the value is wrapped, copy-it in a buffer.
2680 */
2681 smp->data.type = SMP_T_STR;
2682 if (chunks[2] &&
2683 vstart >= chunks[0] && vstart <= chunks[1] &&
2684 vend >= chunks[2] && vend <= chunks[3]) {
2685 /* Wrapped case. */
2686 temp = get_trash_chunk();
2687 memcpy(temp->area, vstart, chunks[1] - vstart);
2688 memcpy(temp->area + ( chunks[1] - vstart ), chunks[2],
2689 vend - chunks[2]);
2690 smp->data.u.str.area = temp->area;
2691 smp->data.u.str.data = ( chunks[1] - vstart ) + ( vend - chunks[2] );
2692 } else {
2693 /* Contiguous case. */
2694 smp->data.u.str.area = (char *)vstart;
2695 smp->data.u.str.data = vend - vstart;
2696 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
2697 }
2698
2699 /* Update context, check wrapping. */
2700 chunks[0] = vend;
2701 if (chunks[2] && vend >= chunks[2] && vend <= chunks[3]) {
2702 chunks[1] = chunks[3];
2703 chunks[2] = NULL;
2704 }
2705
2706 if (chunks[0] < chunks[1])
2707 smp->flags |= SMP_F_NOT_LAST;
2708
2709 return 1;
2710}
2711
2712/* This function iterates over each parameter of the query string. It uses
2713 * ctx->a[0] and ctx->a[1] to store the beginning and end of the current
2714 * parameter. Since it uses smp_fetch_param(), ctx->a[2..3] are both NULL.
2715 * An optional parameter name is passed in args[0], otherwise any parameter is
2716 * considered. It supports an optional delimiter argument for the beginning of
2717 * the string in args[1], which defaults to "?".
2718 */
2719static int smp_fetch_url_param(const struct arg *args, struct sample *smp, const char *kw, void *private)
2720{
Willy Tarreau79e57332018-10-02 16:01:16 +02002721 char delim = '?';
2722 const char *name;
2723 int name_len;
2724
2725 if (!args ||
2726 (args[0].type && args[0].type != ARGT_STR) ||
2727 (args[1].type && args[1].type != ARGT_STR))
2728 return 0;
2729
2730 name = "";
2731 name_len = 0;
2732 if (args->type == ARGT_STR) {
2733 name = args->data.str.area;
2734 name_len = args->data.str.data;
2735 }
2736
2737 if (args[1].type)
2738 delim = *args[1].data.str.area;
2739
2740 if (!smp->ctx.a[0]) { // first call, find the query string
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002741 if (IS_HTX_SMP(smp) || (smp->px->mode == PR_MODE_TCP)) {
2742 /* HTX version */
2743 struct htx *htx = smp_prefetch_htx(smp, args);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01002744 struct htx_sl *sl;
Willy Tarreau79e57332018-10-02 16:01:16 +02002745
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002746 if (!htx)
2747 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02002748
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002749 sl = http_find_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01002750 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 +02002751 if (!smp->ctx.a[0])
2752 return 0;
2753
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01002754 smp->ctx.a[1] = HTX_SL_REQ_UPTR(sl) + HTX_SL_REQ_ULEN(sl);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002755 }
2756 else {
2757 /* LEGACY version */
2758 struct http_msg *msg;
2759
2760 CHECK_HTTP_MESSAGE_FIRST();
2761
2762 msg = &smp->strm->txn->req;
2763
2764 smp->ctx.a[0] = http_find_param_list(ci_head(msg->chn) + msg->sl.rq.u,
2765 msg->sl.rq.u_l, delim);
2766 if (!smp->ctx.a[0])
2767 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02002768
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002769 smp->ctx.a[1] = ci_head(msg->chn) + msg->sl.rq.u + msg->sl.rq.u_l;
2770 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002771
2772 /* Assume that the context is filled with NULL pointer
2773 * before the first call.
2774 * smp->ctx.a[2] = NULL;
2775 * smp->ctx.a[3] = NULL;
2776 */
2777 }
2778
2779 return smp_fetch_param(delim, name, name_len, args, smp, kw, private);
2780}
2781
2782/* This function iterates over each parameter of the body. This requires
2783 * that the body has been waited for using http-buffer-request. It uses
2784 * ctx->a[0] and ctx->a[1] to store the beginning and end of the first
2785 * contigous part of the body, and optionally ctx->a[2..3] to reference the
2786 * optional second part if the body wraps at the end of the buffer. An optional
2787 * parameter name is passed in args[0], otherwise any parameter is considered.
2788 */
2789static int smp_fetch_body_param(const struct arg *args, struct sample *smp, const char *kw, void *private)
2790{
Willy Tarreau79e57332018-10-02 16:01:16 +02002791 const char *name;
2792 int name_len;
2793
2794 if (!args || (args[0].type && args[0].type != ARGT_STR))
2795 return 0;
2796
2797 name = "";
2798 name_len = 0;
2799 if (args[0].type == ARGT_STR) {
2800 name = args[0].data.str.area;
2801 name_len = args[0].data.str.data;
2802 }
2803
2804 if (!smp->ctx.a[0]) { // first call, find the query string
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002805 if (IS_HTX_SMP(smp) || (smp->px->mode == PR_MODE_TCP)) {
2806 /* HTX version */
2807 struct htx *htx = smp_prefetch_htx(smp, args);
2808 struct buffer *temp;
2809 int32_t pos;
Willy Tarreau79e57332018-10-02 16:01:16 +02002810
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002811 if (!htx)
2812 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02002813
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002814 temp = get_trash_chunk();
2815 for (pos = htx_get_head(htx); pos != -1; pos = htx_get_next(htx, pos)) {
2816 struct htx_blk *blk = htx_get_blk(htx, pos);
2817 enum htx_blk_type type = htx_get_blk_type(blk);
Willy Tarreau79e57332018-10-02 16:01:16 +02002818
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002819 if (type == HTX_BLK_EOM || type == HTX_BLK_EOD)
2820 break;
2821 if (type == HTX_BLK_DATA) {
Christopher Fauletc59ff232018-12-03 13:58:44 +01002822 if (!htx_data_to_h1(htx_get_blk_value(htx, blk), temp, 0))
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002823 return 0;
2824 }
2825 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002826
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002827 smp->ctx.a[0] = temp->area;
2828 smp->ctx.a[1] = temp->area + temp->data;
Willy Tarreau79e57332018-10-02 16:01:16 +02002829
2830 /* Assume that the context is filled with NULL pointer
2831 * before the first call.
2832 * smp->ctx.a[2] = NULL;
2833 * smp->ctx.a[3] = NULL;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002834 */
Willy Tarreau79e57332018-10-02 16:01:16 +02002835 }
2836 else {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002837 /* LEGACY version */
2838 struct http_msg *msg;
2839 unsigned long len;
2840 unsigned long block1;
2841 char *body;
2842
2843 CHECK_HTTP_MESSAGE_FIRST();
2844
2845 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
2846 msg = &smp->strm->txn->req;
2847 else
2848 msg = &smp->strm->txn->rsp;
2849
2850 len = http_body_bytes(msg);
2851 body = c_ptr(msg->chn, -http_data_rewind(msg));
2852
2853 block1 = len;
2854 if (block1 > b_wrap(&msg->chn->buf) - body)
2855 block1 = b_wrap(&msg->chn->buf) - body;
2856
2857 if (block1 == len) {
2858 /* buffer is not wrapped (or empty) */
2859 smp->ctx.a[0] = body;
2860 smp->ctx.a[1] = body + len;
2861
2862 /* Assume that the context is filled with NULL pointer
2863 * before the first call.
2864 * smp->ctx.a[2] = NULL;
2865 * smp->ctx.a[3] = NULL;
2866 */
2867 }
2868 else {
2869 /* buffer is wrapped, we need to defragment it */
2870 smp->ctx.a[0] = body;
2871 smp->ctx.a[1] = body + block1;
2872 smp->ctx.a[2] = b_orig(&msg->chn->buf);
2873 smp->ctx.a[3] = b_orig(&msg->chn->buf) + ( len - block1 );
2874 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002875 }
2876 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002877
Willy Tarreau79e57332018-10-02 16:01:16 +02002878 return smp_fetch_param('&', name, name_len, args, smp, kw, private);
2879}
2880
2881/* Return the signed integer value for the specified url parameter (see url_param
2882 * above).
2883 */
2884static int smp_fetch_url_param_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
2885{
2886 int ret = smp_fetch_url_param(args, smp, kw, private);
2887
2888 if (ret > 0) {
2889 smp->data.type = SMP_T_SINT;
2890 smp->data.u.sint = strl2ic(smp->data.u.str.area,
2891 smp->data.u.str.data);
2892 }
2893
2894 return ret;
2895}
2896
2897/* This produces a 32-bit hash of the concatenation of the first occurrence of
2898 * the Host header followed by the path component if it begins with a slash ('/').
2899 * This means that '*' will not be added, resulting in exactly the first Host
2900 * entry. If no Host header is found, then the path is used. The resulting value
2901 * is hashed using the url hash followed by a full avalanche hash and provides a
2902 * 32-bit integer value. This fetch is useful for tracking per-URL activity on
2903 * high-traffic sites without having to store whole paths.
2904 * this differs from the base32 functions in that it includes the url parameters
2905 * as well as the path
2906 */
2907static int smp_fetch_url32(const struct arg *args, struct sample *smp, const char *kw, void *private)
2908{
Willy Tarreau79e57332018-10-02 16:01:16 +02002909 unsigned int hash = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02002910
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002911 if (IS_HTX_SMP(smp) || (smp->px->mode == PR_MODE_TCP)) {
2912 /* HTX version */
2913 struct htx *htx = smp_prefetch_htx(smp, args);
2914 struct http_hdr_ctx ctx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01002915 struct htx_sl *sl;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002916 struct ist path;
Willy Tarreau79e57332018-10-02 16:01:16 +02002917
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002918 if (!htx)
2919 return 0;
2920
2921 ctx.blk = NULL;
2922 if (http_find_header(htx, ist("Host"), &ctx, 1)) {
2923 /* OK we have the header value in ctx.value */
2924 while (ctx.value.len--)
2925 hash = *(ctx.value.ptr++) + (hash << 6) + (hash << 16) - hash;
2926 }
2927
2928 /* now retrieve the path */
2929 sl = http_find_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01002930 path = http_get_path(htx_sl_req_uri(sl));
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002931 while (path.len > 0 && *(path.ptr) != '?') {
2932 path.ptr++;
2933 path.len--;
2934 }
2935 if (path.len && *(path.ptr) == '/') {
2936 while (path.len--)
2937 hash = *(path.ptr++) + (hash << 6) + (hash << 16) - hash;
2938 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002939 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002940 else {
2941 /* LEGACY version */
2942 struct http_txn *txn;
2943 struct hdr_ctx ctx;
2944 char *ptr, *beg, *end;
2945 int len;
2946
2947 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau79e57332018-10-02 16:01:16 +02002948
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002949 txn = smp->strm->txn;
2950 ctx.idx = 0;
2951 if (http_find_header2("Host", 4, ci_head(txn->req.chn), &txn->hdr_idx, &ctx)) {
2952 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
2953 ptr = ctx.line + ctx.val;
2954 len = ctx.vlen;
2955 while (len--)
2956 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
2957 }
2958
2959 /* now retrieve the path */
2960 end = ci_head(txn->req.chn) + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
2961 beg = http_txn_get_path(txn);
2962 if (!beg)
2963 beg = end;
Willy Tarreau79e57332018-10-02 16:01:16 +02002964
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002965 for (ptr = beg; ptr < end ; ptr++);
Willy Tarreau79e57332018-10-02 16:01:16 +02002966
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002967 if (beg < ptr && *beg == '/') {
2968 while (beg < ptr)
2969 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
2970 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002971 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002972
Willy Tarreau79e57332018-10-02 16:01:16 +02002973 hash = full_hash(hash);
2974
2975 smp->data.type = SMP_T_SINT;
2976 smp->data.u.sint = hash;
2977 smp->flags = SMP_F_VOL_1ST;
2978 return 1;
2979}
2980
2981/* This concatenates the source address with the 32-bit hash of the Host and
2982 * URL as returned by smp_fetch_base32(). The idea is to have per-source and
2983 * per-url counters. The result is a binary block from 8 to 20 bytes depending
2984 * on the source address length. The URL hash is stored before the address so
2985 * that in environments where IPv6 is insignificant, truncating the output to
2986 * 8 bytes would still work.
2987 */
2988static int smp_fetch_url32_src(const struct arg *args, struct sample *smp, const char *kw, void *private)
2989{
2990 struct buffer *temp;
2991 struct connection *cli_conn = objt_conn(smp->sess->origin);
2992
2993 if (!cli_conn)
2994 return 0;
2995
2996 if (!smp_fetch_url32(args, smp, kw, private))
2997 return 0;
2998
2999 temp = get_trash_chunk();
3000 *(unsigned int *) temp->area = htonl(smp->data.u.sint);
3001 temp->data += sizeof(unsigned int);
3002
3003 switch (cli_conn->addr.from.ss_family) {
3004 case AF_INET:
3005 memcpy(temp->area + temp->data,
3006 &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr,
3007 4);
3008 temp->data += 4;
3009 break;
3010 case AF_INET6:
3011 memcpy(temp->area + temp->data,
3012 &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr,
3013 16);
3014 temp->data += 16;
3015 break;
3016 default:
3017 return 0;
3018 }
3019
3020 smp->data.u.str = *temp;
3021 smp->data.type = SMP_T_BIN;
3022 return 1;
3023}
3024
3025/************************************************************************/
3026/* Other utility functions */
3027/************************************************************************/
3028
3029/* This function is used to validate the arguments passed to any "hdr" fetch
3030 * keyword. These keywords support an optional positive or negative occurrence
3031 * number. We must ensure that the number is greater than -MAX_HDR_HISTORY. It
3032 * is assumed that the types are already the correct ones. Returns 0 on error,
3033 * non-zero if OK. If <err> is not NULL, it will be filled with a pointer to an
3034 * error message in case of error, that the caller is responsible for freeing.
3035 * The initial location must either be freeable or NULL.
3036 * Note: this function's pointer is checked from Lua.
3037 */
3038int val_hdr(struct arg *arg, char **err_msg)
3039{
3040 if (arg && arg[1].type == ARGT_SINT && arg[1].data.sint < -MAX_HDR_HISTORY) {
3041 memprintf(err_msg, "header occurrence must be >= %d", -MAX_HDR_HISTORY);
3042 return 0;
3043 }
3044 return 1;
3045}
3046
3047/************************************************************************/
3048/* All supported sample fetch keywords must be declared here. */
3049/************************************************************************/
3050
3051/* Note: must not be declared <const> as its list will be overwritten */
3052static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
3053 { "base", smp_fetch_base, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
3054 { "base32", smp_fetch_base32, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
3055 { "base32+src", smp_fetch_base32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
3056
3057 /* capture are allocated and are permanent in the stream */
3058 { "capture.req.hdr", smp_fetch_capture_req_hdr, ARG1(1,SINT), NULL, SMP_T_STR, SMP_USE_HRQHP },
3059
3060 /* retrieve these captures from the HTTP logs */
3061 { "capture.req.method", smp_fetch_capture_req_method, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
3062 { "capture.req.uri", smp_fetch_capture_req_uri, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
3063 { "capture.req.ver", smp_fetch_capture_req_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
3064
3065 { "capture.res.hdr", smp_fetch_capture_res_hdr, ARG1(1,SINT), NULL, SMP_T_STR, SMP_USE_HRSHP },
3066 { "capture.res.ver", smp_fetch_capture_res_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
3067
3068 /* cookie is valid in both directions (eg: for "stick ...") but cook*
3069 * are only here to match the ACL's name, are request-only and are used
3070 * for ACL compatibility only.
3071 */
3072 { "cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
3073 { "cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV|SMP_USE_HRSHV },
3074 { "cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
3075 { "cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
3076
3077 /* hdr is valid in both directions (eg: for "stick ...") but hdr_* are
3078 * only here to match the ACL's name, are request-only and are used for
3079 * ACL compatibility only.
3080 */
3081 { "hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV|SMP_USE_HRSHV },
3082 { "hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
3083 { "hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV },
3084 { "hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_SINT, SMP_USE_HRQHV },
3085
3086 { "http_auth", smp_fetch_http_auth, ARG1(1,USR), NULL, SMP_T_BOOL, SMP_USE_HRQHV },
3087 { "http_auth_group", smp_fetch_http_auth_grp, ARG1(1,USR), NULL, SMP_T_STR, SMP_USE_HRQHV },
3088 { "http_first_req", smp_fetch_http_first_req, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
3089 { "method", smp_fetch_meth, 0, NULL, SMP_T_METH, SMP_USE_HRQHP },
3090 { "path", smp_fetch_path, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
3091 { "query", smp_fetch_query, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
3092
3093 /* HTTP protocol on the request path */
3094 { "req.proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
3095 { "req_proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
3096
3097 /* HTTP version on the request path */
3098 { "req.ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
3099 { "req_ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
3100
3101 { "req.body", smp_fetch_body, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
3102 { "req.body_len", smp_fetch_body_len, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
3103 { "req.body_size", smp_fetch_body_size, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
3104 { "req.body_param", smp_fetch_body_param, ARG1(0,STR), NULL, SMP_T_BIN, SMP_USE_HRQHV },
3105
3106 { "req.hdrs", smp_fetch_hdrs, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
3107 { "req.hdrs_bin", smp_fetch_hdrs_bin, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
3108
3109 /* HTTP version on the response path */
3110 { "res.ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
3111 { "resp_ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
3112
3113 /* explicit req.{cook,hdr} are used to force the fetch direction to be request-only */
3114 { "req.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
3115 { "req.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
3116 { "req.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
3117
3118 { "req.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
3119 { "req.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
3120 { "req.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
3121 { "req.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
3122 { "req.hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV },
3123 { "req.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
3124 { "req.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_SINT, SMP_USE_HRQHV },
Frédéric Lécaille1fceee82019-02-25 15:30:36 +01003125 { "req.ungrpc", smp_fetch_req_ungrpc, ARG1(1, PBUF_FNUM), NULL, SMP_T_BIN, SMP_USE_HRQHV },
Willy Tarreau79e57332018-10-02 16:01:16 +02003126
3127 /* explicit req.{cook,hdr} are used to force the fetch direction to be response-only */
3128 { "res.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
3129 { "res.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
3130 { "res.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
3131
3132 { "res.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
3133 { "res.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
3134 { "res.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
3135 { "res.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
3136 { "res.hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV },
3137 { "res.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
3138 { "res.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_SINT, SMP_USE_HRSHV },
3139
3140 /* scook is valid only on the response and is used for ACL compatibility */
3141 { "scook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
3142 { "scook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
3143 { "scook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
3144 { "set-cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV }, /* deprecated */
3145
3146 /* shdr is valid only on the response and is used for ACL compatibility */
3147 { "shdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
3148 { "shdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
3149 { "shdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV },
3150 { "shdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_SINT, SMP_USE_HRSHV },
3151
3152 { "status", smp_fetch_stcode, 0, NULL, SMP_T_SINT, SMP_USE_HRSHP },
3153 { "unique-id", smp_fetch_uniqueid, 0, NULL, SMP_T_STR, SMP_SRC_L4SRV },
3154 { "url", smp_fetch_url, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
3155 { "url32", smp_fetch_url32, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
3156 { "url32+src", smp_fetch_url32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
3157 { "url_ip", smp_fetch_url_ip, 0, NULL, SMP_T_IPV4, SMP_USE_HRQHV },
3158 { "url_port", smp_fetch_url_port, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
3159 { "url_param", smp_fetch_url_param, ARG2(0,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
3160 { "urlp" , smp_fetch_url_param, ARG2(0,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
3161 { "urlp_val", smp_fetch_url_param_val, ARG2(0,STR,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
3162 { /* END */ },
3163}};
3164
Willy Tarreau0108d902018-11-25 19:14:37 +01003165INITCALL1(STG_REGISTER, sample_register_fetches, &sample_fetch_keywords);
Willy Tarreau79e57332018-10-02 16:01:16 +02003166
3167/*
3168 * Local variables:
3169 * c-indent-level: 8
3170 * c-basic-offset: 8
3171 * End:
3172 */