blob: 38905095962b931cc1694b042d67cbeed28cc030 [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>
24#include <common/http.h>
Willy Tarreau0108d902018-11-25 19:14:37 +010025#include <common/initcall.h>
Willy Tarreau79e57332018-10-02 16:01:16 +020026#include <common/memory.h>
27#include <common/standard.h>
28#include <common/version.h>
29
30#include <types/global.h>
31
32#include <proto/arg.h>
33#include <proto/auth.h>
34#include <proto/http_fetch.h>
Christopher Fauletef453ed2018-10-24 21:39:27 +020035#include <proto/http_htx.h>
36#include <proto/htx.h>
Willy Tarreau79e57332018-10-02 16:01:16 +020037#include <proto/log.h>
38#include <proto/obj_type.h>
39#include <proto/proto_http.h>
40#include <proto/sample.h>
41#include <proto/stream.h>
42
43
44/* this struct is used between calls to smp_fetch_hdr() or smp_fetch_cookie() */
45static THREAD_LOCAL struct hdr_ctx static_hdr_ctx;
Christopher Fauletef453ed2018-10-24 21:39:27 +020046static THREAD_LOCAL struct http_hdr_ctx static_http_hdr_ctx;
47
Willy Tarreau79e57332018-10-02 16:01:16 +020048
49/*
50 * Returns the data from Authorization header. Function may be called more
51 * than once so data is stored in txn->auth_data. When no header is found
52 * or auth method is unknown auth_method is set to HTTP_AUTH_WRONG to avoid
53 * searching again for something we are unable to find anyway. However, if
54 * the result if valid, the cache is not reused because we would risk to
55 * have the credentials overwritten by another stream in parallel.
56 */
57
Christopher Faulet311c7ea2018-10-24 21:41:55 +020058static int get_http_auth(struct sample *smp)
Willy Tarreau79e57332018-10-02 16:01:16 +020059{
Christopher Faulet311c7ea2018-10-24 21:41:55 +020060 struct stream *s = smp->strm;
Willy Tarreau79e57332018-10-02 16:01:16 +020061 struct http_txn *txn = s->txn;
62 struct buffer auth_method;
Willy Tarreau79e57332018-10-02 16:01:16 +020063 char *h, *p;
64 int len;
65
66#ifdef DEBUG_AUTH
67 printf("Auth for stream %p: %d\n", s, txn->auth.method);
68#endif
Willy Tarreau79e57332018-10-02 16:01:16 +020069 if (txn->auth.method == HTTP_AUTH_WRONG)
70 return 0;
71
72 txn->auth.method = HTTP_AUTH_WRONG;
73
Christopher Faulet311c7ea2018-10-24 21:41:55 +020074 if (IS_HTX_STRM(s) || (smp->px->mode == PR_MODE_TCP)) {
75 /* HTX version */
76 struct htx *htx = htx_from_buf(&s->req.buf);
77 struct http_hdr_ctx ctx = { .blk = NULL };
78 struct ist hdr;
Willy Tarreau79e57332018-10-02 16:01:16 +020079
Christopher Faulet311c7ea2018-10-24 21:41:55 +020080 if (txn->flags & TX_USE_PX_CONN)
81 hdr = ist("Proxy-Authorization");
82 else
83 hdr = ist("Authorization");
84
85 htx = htx_from_buf(&s->req.buf);
86 ctx.blk = NULL;
87 if (!http_find_header(htx, hdr, &ctx, 0))
88 return 0;
89
90 p = memchr(ctx.value.ptr, ' ', ctx.value.len);
91 len = p - ctx.value.ptr;
92 if (!p || len <= 0)
93 return 0;
94
95 if (chunk_initlen(&auth_method, ctx.value.ptr, 0, len) != 1)
96 return 0;
97
98 chunk_initlen(&txn->auth.method_data, p + 1, 0, ctx.value.len - len - 1);
Willy Tarreau79e57332018-10-02 16:01:16 +020099 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200100 else {
101 /* LEGACY version */
102 struct hdr_ctx ctx = { .idx = 0 };
Willy Tarreau79e57332018-10-02 16:01:16 +0200103
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200104 if (txn->flags & TX_USE_PX_CONN) {
105 h = "Proxy-Authorization";
106 len = strlen(h);
107 } else {
108 h = "Authorization";
109 len = strlen(h);
110 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200111
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200112 if (!http_find_header2(h, len, ci_head(&s->req), &txn->hdr_idx, &ctx))
113 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200114
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200115 h = ctx.line + ctx.val;
Willy Tarreau79e57332018-10-02 16:01:16 +0200116
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200117 p = memchr(h, ' ', ctx.vlen);
118 len = p - h;
119 if (!p || len <= 0)
120 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200121
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200122 if (chunk_initlen(&auth_method, h, 0, len) != 1)
123 return 0;
124
125 chunk_initlen(&txn->auth.method_data, p + 1, 0, ctx.vlen - len - 1);
126 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200127
128 if (!strncasecmp("Basic", auth_method.area, auth_method.data)) {
129 struct buffer *http_auth = get_trash_chunk();
130
131 len = base64dec(txn->auth.method_data.area,
132 txn->auth.method_data.data,
133 http_auth->area, global.tune.bufsize - 1);
134
135 if (len < 0)
136 return 0;
137
138
139 http_auth->area[len] = '\0';
140
141 p = strchr(http_auth->area, ':');
142
143 if (!p)
144 return 0;
145
146 txn->auth.user = http_auth->area;
147 *p = '\0';
148 txn->auth.pass = p+1;
149
150 txn->auth.method = HTTP_AUTH_BASIC;
151 return 1;
152 }
153
154 return 0;
155}
156
157/* This function ensures that the prerequisites for an L7 fetch are ready,
158 * which means that a request or response is ready. If some data is missing,
159 * a parsing attempt is made. This is useful in TCP-based ACLs which are able
Christopher Fauletef453ed2018-10-24 21:39:27 +0200160 * to extract data from L7.
161 *
162 * The function returns :
163 * NULL with SMP_F_MAY_CHANGE in the sample flags if some data is missing to
164 * decide whether or not an HTTP message is present ;
165 * NULL if the requested data cannot be fetched or if it is certain that
166 * we'll never have any HTTP message there ;
167 * The HTX message if ready
168 */
169struct htx *smp_prefetch_htx(struct sample *smp, const struct arg *args)
170{
171 struct proxy *px = smp->px;
172 struct stream *s = smp->strm;
173 unsigned int opt = smp->opt;
174 struct http_txn *txn = NULL;
175 struct htx *htx = NULL;
176
177 /* Note: it is possible that <s> is NULL when called before stream
178 * initialization (eg: tcp-request connection), so this function is the
179 * one responsible for guarding against this case for all HTTP users.
180 */
181 if (!s)
182 return NULL;
183
184 if (!s->txn) {
185 if (unlikely(!http_alloc_txn(s)))
186 return NULL; /* not enough memory */
187 http_init_txn(s);
188 txn = s->txn;
189 }
190
191 if (px->mode == PR_MODE_HTTP) {
192 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
193 union h1_sl sl;
194
195 htx = htx_from_buf(&s->req.buf);
196 if (htx_is_empty(htx) || htx_get_tail_type(htx) < HTX_BLK_EOH) {
197 /* Parsing is done by the mux, just wait */
198 smp->flags |= SMP_F_MAY_CHANGE;
199 return NULL;
200 }
201
202 /* OK we just got a valid HTTP request. We have some
203 * minor preparation to perform so that further checks
204 * can rely on HTTP tests.
205 */
206 if (txn) {
207 sl = http_find_stline(htx);
208 txn->meth = sl.rq.meth;
209 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
210 s->flags |= SF_REDIRECTABLE;
211 }
212
213 /* otherwise everything's ready for the request */
214 }
215 else {
216 htx = htx_from_buf(&s->res.buf);
217 if (htx_is_empty(htx) || htx_get_tail_type(htx) < HTX_BLK_EOH) {
218 /* Parsing is done by the mux, just wait */
219 smp->flags |= SMP_F_MAY_CHANGE;
220 return NULL;
221 }
222 }
223 }
224 else { /* PR_MODE_TCP */
225 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
226 struct buffer *buf;
227 struct h1m h1m;
228 struct http_hdr hdrs[MAX_HTTP_HDR];
229 union h1_sl sl;
230 int ret;
231
232 buf = &s->req.buf;
233 if (b_head(buf) + b_data(buf) > b_wrap(buf))
234 b_slow_realign(buf, trash.area, 0);
235
236 h1m_init_req(&h1m);
237 ret = h1_headers_to_hdr_list(b_head(buf), b_stop(buf),
238 hdrs, sizeof(hdrs)/sizeof(hdrs[0]), &h1m, &sl);
239 if (ret <= 0) {
240 /* Invalid or too big*/
241 if (ret < 0 || channel_full(&s->req, global.tune.maxrewrite))
242 return NULL;
243
244 /* wait for a full request */
245 smp->flags |= SMP_F_MAY_CHANGE;
246 return NULL;
247 }
248
249 /* OK we just got a valid HTTP request. We have to
250 * convert it into an HTX message.
251 */
252 if (unlikely(sl.rq.v.len == 0)) {
253 /* try to convert HTTP/0.9 requests to HTTP/1.0 */
254 if (sl.rq.meth != HTTP_METH_GET || !sl.rq.u.len)
255 return NULL;
256 sl.rq.v = ist("HTTP/1.0");
257 }
258 htx = htx_from_buf(get_trash_chunk());
259 if (!htx_add_reqline(htx, sl) || !htx_add_all_headers(htx, hdrs))
260 return NULL;
261
262 if (txn) {
263 txn->meth = sl.rq.meth;
264 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
265 s->flags |= SF_REDIRECTABLE;
266 }
267 /* Ok, now everything's ready for the request */
268 }
269 else {
270 /* Impossible, no HTTP fetch on tcp-response */
271 return NULL;
272 }
273 }
274
275 /* everything's OK */
276 smp->data.u.sint = 1;
277 return htx;
278}
279
280/* This function ensures that the prerequisites for an L7 fetch are ready,
281 * which means that a request or response is ready. If some data is missing,
282 * a parsing attempt is made. This is useful in TCP-based ACLs which are able
Willy Tarreau79e57332018-10-02 16:01:16 +0200283 * to extract data from L7. If <req_vol> is non-null during a request prefetch,
284 * another test is made to ensure the required information is not gone.
285 *
286 * The function returns :
287 * 0 with SMP_F_MAY_CHANGE in the sample flags if some data is missing to
288 * decide whether or not an HTTP message is present ;
289 * 0 if the requested data cannot be fetched or if it is certain that
290 * we'll never have any HTTP message there ;
291 * 1 if an HTTP message is ready
292 */
293int smp_prefetch_http(struct proxy *px, struct stream *s, unsigned int opt,
294 const struct arg *args, struct sample *smp, int req_vol)
295{
296 struct http_txn *txn;
297 struct http_msg *msg;
298
299 /* Note: it is possible that <s> is NULL when called before stream
300 * initialization (eg: tcp-request connection), so this function is the
301 * one responsible for guarding against this case for all HTTP users.
302 */
303 if (!s)
304 return 0;
305
306 if (!s->txn) {
307 if (unlikely(!http_alloc_txn(s)))
308 return 0; /* not enough memory */
309 http_init_txn(s);
310 }
311 txn = s->txn;
312 msg = &txn->req;
313
314 /* Check for a dependency on a request */
315 smp->data.type = SMP_T_BOOL;
316
317 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
318 /* If the buffer does not leave enough free space at the end,
319 * we must first realign it.
320 */
321 if (ci_head(&s->req) > b_orig(&s->req.buf) &&
322 ci_head(&s->req) + ci_data(&s->req) > b_wrap(&s->req.buf) - global.tune.maxrewrite)
323 channel_slow_realign(&s->req, trash.area);
324
325 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY)) {
326 if (msg->msg_state == HTTP_MSG_ERROR)
327 return 0;
328
329 /* Try to decode HTTP request */
330 if (likely(msg->next < ci_data(&s->req)))
331 http_msg_analyzer(msg, &txn->hdr_idx);
332
333 /* Still no valid request ? */
334 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
335 if ((msg->msg_state == HTTP_MSG_ERROR) ||
336 channel_full(&s->req, global.tune.maxrewrite)) {
337 return 0;
338 }
339 /* wait for final state */
340 smp->flags |= SMP_F_MAY_CHANGE;
341 return 0;
342 }
343
344 /* OK we just got a valid HTTP request. We have some minor
345 * preparation to perform so that further checks can rely
346 * on HTTP tests.
347 */
348
349 /* If the request was parsed but was too large, we must absolutely
350 * return an error so that it is not processed. At the moment this
351 * cannot happen, but if the parsers are to change in the future,
352 * we want this check to be maintained.
353 */
354 if (unlikely(ci_head(&s->req) + ci_data(&s->req) >
355 b_wrap(&s->req.buf) - global.tune.maxrewrite)) {
356 msg->err_state = msg->msg_state;
357 msg->msg_state = HTTP_MSG_ERROR;
358 smp->data.u.sint = 1;
359 return 1;
360 }
361
362 txn->meth = find_http_meth(ci_head(msg->chn), msg->sl.rq.m_l);
363 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
364 s->flags |= SF_REDIRECTABLE;
365
366 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
367 return 0;
368 }
369
370 if (req_vol && txn->rsp.msg_state != HTTP_MSG_RPBEFORE) {
371 return 0; /* data might have moved and indexes changed */
372 }
373
374 /* otherwise everything's ready for the request */
375 }
376 else {
377 /* Check for a dependency on a response */
378 if (txn->rsp.msg_state < HTTP_MSG_BODY) {
379 smp->flags |= SMP_F_MAY_CHANGE;
380 return 0;
381 }
382 }
383
384 /* everything's OK */
385 smp->data.u.sint = 1;
386 return 1;
387}
388
389/* This function fetches the method of current HTTP request and stores
390 * it in the global pattern struct as a chunk. There are two possibilities :
391 * - if the method is known (not HTTP_METH_OTHER), its identifier is stored
392 * in <len> and <ptr> is NULL ;
393 * - if the method is unknown (HTTP_METH_OTHER), <ptr> points to the text and
394 * <len> to its length.
395 * This is intended to be used with pat_match_meth() only.
396 */
397static int smp_fetch_meth(const struct arg *args, struct sample *smp, const char *kw, void *private)
398{
399 int meth;
400 struct http_txn *txn;
401
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200402 if (IS_HTX_SMP(smp) || (smp->px->mode == PR_MODE_TCP)) {
403 /* HTX version */
404 struct htx *htx = smp_prefetch_htx(smp, args);
Willy Tarreau79e57332018-10-02 16:01:16 +0200405
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200406 if (!htx)
Willy Tarreau79e57332018-10-02 16:01:16 +0200407 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200408
409 txn = smp->strm->txn;
410 meth = txn->meth;
411 smp->data.type = SMP_T_METH;
412 smp->data.u.meth.meth = meth;
413 if (meth == HTTP_METH_OTHER) {
414 union h1_sl sl;
415
416 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
417 /* ensure the indexes are not affected */
418 return 0;
419
420 sl = http_find_stline(htx);
421 smp->flags |= SMP_F_CONST;
422 smp->data.u.meth.str.area = sl.rq.m.ptr;
423 smp->data.u.meth.str.data = sl.rq.m.len;
424 }
425 smp->flags |= SMP_F_VOL_1ST;
426 }
427 else {
428 /* LEGACY version */
429 CHECK_HTTP_MESSAGE_FIRST_PERM();
430
431 txn = smp->strm->txn;
432 meth = txn->meth;
433 smp->data.type = SMP_T_METH;
434 smp->data.u.meth.meth = meth;
435 if (meth == HTTP_METH_OTHER) {
436 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
437 /* ensure the indexes are not affected */
438 return 0;
439 smp->flags |= SMP_F_CONST;
440 smp->data.u.meth.str.data = txn->req.sl.rq.m_l;
441 smp->data.u.meth.str.area = ci_head(txn->req.chn);
442 }
443 smp->flags |= SMP_F_VOL_1ST;
Willy Tarreau79e57332018-10-02 16:01:16 +0200444 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200445 return 1;
446}
447
448static int smp_fetch_rqver(const struct arg *args, struct sample *smp, const char *kw, void *private)
449{
450 struct http_txn *txn;
451 char *ptr;
452 int len;
453
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200454 if (IS_HTX_SMP(smp) || (smp->px->mode == PR_MODE_TCP)) {
455 /* HTX version */
456 struct htx *htx = smp_prefetch_htx(smp, args);
457 union h1_sl sl;
458
459 if (!htx)
460 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200461
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200462 sl = http_find_stline(htx);
463 len = sl.rq.v.len;
464 ptr = sl.rq.v.ptr;
465 }
466 else {
467 /* LEGACY version */
468 CHECK_HTTP_MESSAGE_FIRST();
469
470 txn = smp->strm->txn;
471 len = txn->req.sl.rq.v_l;
472 ptr = ci_head(txn->req.chn) + txn->req.sl.rq.v;
473 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200474
475 while ((len-- > 0) && (*ptr++ != '/'));
476 if (len <= 0)
477 return 0;
478
479 smp->data.type = SMP_T_STR;
480 smp->data.u.str.area = ptr;
481 smp->data.u.str.data = len;
482
483 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
484 return 1;
485}
486
487static int smp_fetch_stver(const struct arg *args, struct sample *smp, const char *kw, void *private)
488{
489 struct http_txn *txn;
490 char *ptr;
491 int len;
492
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200493 if (IS_HTX_SMP(smp) || (smp->px->mode == PR_MODE_TCP)) {
494 /* HTX version */
495 struct htx *htx = smp_prefetch_htx(smp, args);
496 union h1_sl sl;
Willy Tarreau79e57332018-10-02 16:01:16 +0200497
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200498 if (!htx)
499 return 0;
500
501 sl = http_find_stline(htx);
502 len = sl.st.v.len;
503 ptr = sl.st.v.ptr;
504 }
505 else {
506 /* LEGACY version */
507 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau79e57332018-10-02 16:01:16 +0200508
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200509 txn = smp->strm->txn;
510 if (txn->rsp.msg_state < HTTP_MSG_BODY)
511 return 0;
512
513 len = txn->rsp.sl.st.v_l;
514 ptr = ci_head(txn->rsp.chn);
515 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200516
517 while ((len-- > 0) && (*ptr++ != '/'));
518 if (len <= 0)
519 return 0;
520
521 smp->data.type = SMP_T_STR;
522 smp->data.u.str.area = ptr;
523 smp->data.u.str.data = len;
524
525 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
526 return 1;
527}
528
529/* 3. Check on Status Code. We manipulate integers here. */
530static int smp_fetch_stcode(const struct arg *args, struct sample *smp, const char *kw, void *private)
531{
532 struct http_txn *txn;
533 char *ptr;
534 int len;
535
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200536 if (IS_HTX_SMP(smp) || (smp->px->mode == PR_MODE_TCP)) {
537 /* HTX version */
538 struct htx *htx = smp_prefetch_htx(smp, args);
539 union h1_sl sl;
Willy Tarreau79e57332018-10-02 16:01:16 +0200540
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200541 if (!htx)
542 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200543
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200544 sl = http_find_stline(htx);
545 len = sl.st.c.len;
546 ptr = sl.st.c.ptr;
547 }
548 else {
549 /* LEGACY version */
550 CHECK_HTTP_MESSAGE_FIRST();
551
552 txn = smp->strm->txn;
553 if (txn->rsp.msg_state < HTTP_MSG_BODY)
554 return 0;
555
556 len = txn->rsp.sl.st.c_l;
557 ptr = ci_head(txn->rsp.chn) + txn->rsp.sl.st.c;
558 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200559
560 smp->data.type = SMP_T_SINT;
561 smp->data.u.sint = __strl2ui(ptr, len);
562 smp->flags = SMP_F_VOL_1ST;
563 return 1;
564}
565
566static int smp_fetch_uniqueid(const struct arg *args, struct sample *smp, const char *kw, void *private)
567{
568 if (LIST_ISEMPTY(&smp->sess->fe->format_unique_id))
569 return 0;
570
571 if (!smp->strm->unique_id) {
572 if ((smp->strm->unique_id = pool_alloc(pool_head_uniqueid)) == NULL)
573 return 0;
574 smp->strm->unique_id[0] = '\0';
575 }
576 smp->data.u.str.data = build_logline(smp->strm, smp->strm->unique_id,
577 UNIQUEID_LEN, &smp->sess->fe->format_unique_id);
578
579 smp->data.type = SMP_T_STR;
580 smp->data.u.str.area = smp->strm->unique_id;
581 smp->flags = SMP_F_CONST;
582 return 1;
583}
584
585/* Returns a string block containing all headers including the
Joseph Herlant942eea32018-11-15 13:57:22 -0800586 * empty line which separes headers from the body. This is useful
587 * for some headers analysis.
Willy Tarreau79e57332018-10-02 16:01:16 +0200588 */
589static int smp_fetch_hdrs(const struct arg *args, struct sample *smp, const char *kw, void *private)
590{
Willy Tarreau79e57332018-10-02 16:01:16 +0200591 struct http_txn *txn;
592
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200593 if (IS_HTX_SMP(smp) || (smp->px->mode == PR_MODE_TCP)) {
594 /* HTX version */
595 struct htx *htx = smp_prefetch_htx(smp, args);
596 struct buffer *temp;
597 int32_t pos;
Willy Tarreau79e57332018-10-02 16:01:16 +0200598
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200599 if (!htx)
600 return 0;
601 temp = get_trash_chunk();
602 for (pos = htx_get_head(htx); pos != -1; pos = htx_get_next(htx, pos)) {
603 struct htx_blk *blk = htx_get_blk(htx, pos);
604 enum htx_blk_type type = htx_get_blk_type(blk);
Willy Tarreau79e57332018-10-02 16:01:16 +0200605
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200606 if (type == HTX_BLK_HDR) {
607 struct ist n = htx_get_blk_name(htx, blk);
608 struct ist v = htx_get_blk_value(htx, blk);
609
610 if (!htx_hdr_to_str(n, v, temp))
611 return 0;
612 }
613 else if (type == HTX_BLK_EOH) {
614 if (!chunk_memcat(temp, "\r\n", 2))
615 return 0;
616 break;
617 }
618 }
619 smp->data.type = SMP_T_STR;
620 smp->data.u.str = *temp;
621
622 }
623 else {
624 /* LEGACY version */
625 struct http_msg *msg;
626 struct hdr_idx *idx;
627
628 CHECK_HTTP_MESSAGE_FIRST();
629
630 txn = smp->strm->txn;
631 idx = &txn->hdr_idx;
632 msg = &txn->req;
Willy Tarreau79e57332018-10-02 16:01:16 +0200633
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200634 smp->data.type = SMP_T_STR;
635 smp->data.u.str.area = ci_head(msg->chn) + hdr_idx_first_pos(idx);
636 smp->data.u.str.data = msg->eoh - hdr_idx_first_pos(idx) + 1 +
637 (ci_head(msg->chn)[msg->eoh] == '\r');
638 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200639 return 1;
640}
641
642/* Returns the header request in a length/value encoded format.
643 * This is useful for exchanges with the SPOE.
644 *
645 * A "length value" is a multibyte code encoding numbers. It uses the
646 * SPOE format. The encoding is the following:
647 *
648 * Each couple "header name" / "header value" is composed
649 * like this:
650 * "length value" "header name bytes"
651 * "length value" "header value bytes"
652 * When the last header is reached, the header name and the header
653 * value are empty. Their length are 0
654 */
655static int smp_fetch_hdrs_bin(const struct arg *args, struct sample *smp, const char *kw, void *private)
656{
Willy Tarreau79e57332018-10-02 16:01:16 +0200657 struct http_txn *txn;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200658 struct buffer *temp;
Willy Tarreau79e57332018-10-02 16:01:16 +0200659
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200660 if (IS_HTX_SMP(smp) || (smp->px->mode == PR_MODE_TCP)) {
661 /* HTX version */
662 struct htx *htx = smp_prefetch_htx(smp, args);
663 struct buffer *temp;
664 char *p, *end;
665 int32_t pos;
666 int ret;
Willy Tarreau79e57332018-10-02 16:01:16 +0200667
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200668 if (!htx)
669 return 0;
670 temp = get_trash_chunk();
671 p = temp->area;
672 end = temp->area + temp->size;
673 for (pos = htx_get_head(htx); pos != -1; pos = htx_get_next(htx, pos)) {
674 struct htx_blk *blk = htx_get_blk(htx, pos);
675 enum htx_blk_type type = htx_get_blk_type(blk);
676 struct ist n, v;
Willy Tarreau79e57332018-10-02 16:01:16 +0200677
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200678 if (type == HTX_BLK_HDR) {
679 n = htx_get_blk_name(htx,blk);
680 v = htx_get_blk_value(htx, blk);
Willy Tarreau79e57332018-10-02 16:01:16 +0200681
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200682 /* encode the header name. */
683 ret = encode_varint(n.len, &p, end);
684 if (ret == -1)
685 return 0;
686 if (p + n.len > end)
687 return 0;
688 memcpy(p, n.ptr, n.len);
689 p += n.len;
Willy Tarreau79e57332018-10-02 16:01:16 +0200690
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200691 /* encode the header value. */
692 ret = encode_varint(v.len, &p, end);
693 if (ret == -1)
694 return 0;
695 if (p + v.len > end)
696 return 0;
697 memcpy(p, v.ptr, v.len);
698 p += v.len;
Willy Tarreau79e57332018-10-02 16:01:16 +0200699
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200700 }
701 else if (type == HTX_BLK_EOH) {
702 /* encode the end of the header list with empty
703 * header name and header value.
704 */
705 ret = encode_varint(0, &p, end);
706 if (ret == -1)
707 return 0;
708 ret = encode_varint(0, &p, end);
709 if (ret == -1)
710 return 0;
711 break;
712 }
713 }
714
715 /* Initialise sample data which will be filled. */
716 smp->data.type = SMP_T_BIN;
717 smp->data.u.str.area = temp->area;
718 smp->data.u.str.data = p - temp->area;
719 smp->data.u.str.size = temp->size;
720 }
721 else {
722 /* LEGACY version */
723 struct http_msg *msg;
724 struct hdr_idx *idx;
725 const char *cur_ptr, *cur_next, *p;
726 int old_idx, cur_idx;
727 struct hdr_idx_elem *cur_hdr;
728 const char *hn, *hv;
729 int hnl, hvl;
730 int ret;
731 char *buf;
732 char *end;
733
734 CHECK_HTTP_MESSAGE_FIRST();
735
736 temp = get_trash_chunk();
737 buf = temp->area;
738 end = temp->area + temp->size;
739
740 txn = smp->strm->txn;
741 idx = &txn->hdr_idx;
742 msg = &txn->req;
743
744 /* Build array of headers. */
745 old_idx = 0;
746 cur_next = ci_head(msg->chn) + hdr_idx_first_pos(idx);
747 while (1) {
748 cur_idx = idx->v[old_idx].next;
749 if (!cur_idx)
750 break;
751 old_idx = cur_idx;
Willy Tarreau79e57332018-10-02 16:01:16 +0200752
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200753 cur_hdr = &idx->v[cur_idx];
754 cur_ptr = cur_next;
755 cur_next = cur_ptr + cur_hdr->len + cur_hdr->cr + 1;
756
757 /* Now we have one full header at cur_ptr of len cur_hdr->len,
758 * and the next header starts at cur_next. We'll check
759 * this header in the list as well as against the default
760 * rule.
761 */
762
763 /* look for ': *'. */
764 hn = cur_ptr;
765 for (p = cur_ptr; p < cur_ptr + cur_hdr->len && *p != ':'; p++);
766 if (p >= cur_ptr+cur_hdr->len)
767 continue;
768 hnl = p - hn;
Willy Tarreau79e57332018-10-02 16:01:16 +0200769 p++;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200770 while (p < cur_ptr + cur_hdr->len && (*p == ' ' || *p == '\t'))
771 p++;
772 if (p >= cur_ptr + cur_hdr->len)
773 continue;
774 hv = p;
775 hvl = cur_ptr + cur_hdr->len-p;
Willy Tarreau79e57332018-10-02 16:01:16 +0200776
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200777 /* encode the header name. */
778 ret = encode_varint(hnl, &buf, end);
779 if (ret == -1)
780 return 0;
781 if (buf + hnl > end)
782 return 0;
783 memcpy(buf, hn, hnl);
784 buf += hnl;
785
786 /* encode and copy the value. */
787 ret = encode_varint(hvl, &buf, end);
788 if (ret == -1)
789 return 0;
790 if (buf + hvl > end)
791 return 0;
792 memcpy(buf, hv, hvl);
793 buf += hvl;
794 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200795
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200796 /* encode the end of the header list with empty
797 * header name and header value.
798 */
799 ret = encode_varint(0, &buf, end);
Willy Tarreau79e57332018-10-02 16:01:16 +0200800 if (ret == -1)
801 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200802 ret = encode_varint(0, &buf, end);
803 if (ret == -1)
Willy Tarreau79e57332018-10-02 16:01:16 +0200804 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200805
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200806 /* Initialise sample data which will be filled. */
807 smp->data.type = SMP_T_BIN;
808 smp->data.u.str.area = temp->area;
809 smp->data.u.str.data = buf - temp->area;
810 smp->data.u.str.size = temp->size;
811 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200812 return 1;
813}
814
815/* returns the longest available part of the body. This requires that the body
816 * has been waited for using http-buffer-request.
817 */
818static int smp_fetch_body(const struct arg *args, struct sample *smp, const char *kw, void *private)
819{
Willy Tarreau79e57332018-10-02 16:01:16 +0200820 struct buffer *temp;
821
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200822 if (IS_HTX_SMP(smp) || (smp->px->mode == PR_MODE_TCP)) {
823 /* HTX version */
824 struct htx *htx = smp_prefetch_htx(smp, args);
825 int32_t pos;
Willy Tarreau79e57332018-10-02 16:01:16 +0200826
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200827 if (!htx)
828 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200829
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200830 temp = get_trash_chunk();
831 for (pos = htx_get_head(htx); pos != -1; pos = htx_get_next(htx, pos)) {
832 struct htx_blk *blk = htx_get_blk(htx, pos);
833 enum htx_blk_type type = htx_get_blk_type(blk);
Willy Tarreau79e57332018-10-02 16:01:16 +0200834
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200835 if (type == HTX_BLK_EOM || type == HTX_BLK_EOD)
836 break;
837 if (type == HTX_BLK_DATA) {
838 if (!htx_data_to_str(htx_get_blk_value(htx, blk), temp, 0))
839 return 0;
840 }
841 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200842
Willy Tarreau79e57332018-10-02 16:01:16 +0200843 smp->data.type = SMP_T_BIN;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200844 smp->data.u.str = *temp;
845 smp->flags = SMP_F_VOL_TEST;
Willy Tarreau79e57332018-10-02 16:01:16 +0200846 }
847 else {
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200848 /* LEGACY version */
849 struct http_msg *msg;
850 unsigned long len;
851 unsigned long block1;
852 char *body;
853
854 CHECK_HTTP_MESSAGE_FIRST();
855
856 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
857 msg = &smp->strm->txn->req;
858 else
859 msg = &smp->strm->txn->rsp;
860
861 len = http_body_bytes(msg);
862 body = c_ptr(msg->chn, -http_data_rewind(msg));
863
864 block1 = len;
865 if (block1 > b_wrap(&msg->chn->buf) - body)
866 block1 = b_wrap(&msg->chn->buf) - body;
867
868 if (block1 == len) {
869 /* buffer is not wrapped (or empty) */
870 smp->data.type = SMP_T_BIN;
871 smp->data.u.str.area = body;
872 smp->data.u.str.data = len;
873 smp->flags = SMP_F_VOL_TEST | SMP_F_CONST;
874 }
875 else {
876 /* buffer is wrapped, we need to defragment it */
877 temp = get_trash_chunk();
878 memcpy(temp->area, body, block1);
879 memcpy(temp->area + block1, b_orig(&msg->chn->buf),
880 len - block1);
881 smp->data.type = SMP_T_BIN;
882 smp->data.u.str.area = temp->area;
883 smp->data.u.str.data = len;
884 smp->flags = SMP_F_VOL_TEST;
885 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200886 }
887 return 1;
888}
889
890
891/* returns the available length of the body. This requires that the body
892 * has been waited for using http-buffer-request.
893 */
894static int smp_fetch_body_len(const struct arg *args, struct sample *smp, const char *kw, void *private)
895{
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200896 if (IS_HTX_SMP(smp) || (smp->px->mode == PR_MODE_TCP)) {
897 /* HTX version */
898 return 0; /* TODO: to be implemented */
899 }
900 else {
901 /* LEGACY version */
902 struct http_msg *msg;
Willy Tarreau79e57332018-10-02 16:01:16 +0200903
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200904 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau79e57332018-10-02 16:01:16 +0200905
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200906 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
907 msg = &smp->strm->txn->req;
908 else
909 msg = &smp->strm->txn->rsp;
Willy Tarreau79e57332018-10-02 16:01:16 +0200910
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200911 smp->data.type = SMP_T_SINT;
912 smp->data.u.sint = http_body_bytes(msg);
Willy Tarreau79e57332018-10-02 16:01:16 +0200913
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200914 smp->flags = SMP_F_VOL_TEST;
915 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200916 return 1;
917}
918
919
920/* returns the advertised length of the body, or the advertised size of the
921 * chunks available in the buffer. This requires that the body has been waited
922 * for using http-buffer-request.
923 */
924static int smp_fetch_body_size(const struct arg *args, struct sample *smp, const char *kw, void *private)
925{
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200926 if (IS_HTX_SMP(smp) || (smp->px->mode == PR_MODE_TCP)) {
927 /* HTX version */
928 return 0; /* TODO: to be implemented */
929 }
930 else {
931 /* LEGACY version */
932 struct http_msg *msg;
Willy Tarreau79e57332018-10-02 16:01:16 +0200933
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200934 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau79e57332018-10-02 16:01:16 +0200935
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200936 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
937 msg = &smp->strm->txn->req;
938 else
939 msg = &smp->strm->txn->rsp;
Willy Tarreau79e57332018-10-02 16:01:16 +0200940
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200941 smp->data.type = SMP_T_SINT;
942 smp->data.u.sint = msg->body_len;
Willy Tarreau79e57332018-10-02 16:01:16 +0200943
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200944 smp->flags = SMP_F_VOL_TEST;
945 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200946 return 1;
947}
948
949
950/* 4. Check on URL/URI. A pointer to the URI is stored. */
951static int smp_fetch_url(const struct arg *args, struct sample *smp, const char *kw, void *private)
952{
953 struct http_txn *txn;
954
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200955 if (IS_HTX_SMP(smp) || (smp->px->mode == PR_MODE_TCP)) {
956 /* HTX version */
957 struct htx *htx = smp_prefetch_htx(smp, args);
958 union h1_sl sl;
Willy Tarreau79e57332018-10-02 16:01:16 +0200959
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200960 if (!htx)
961 return 0;
962 sl = http_find_stline(htx);
963 smp->data.type = SMP_T_STR;
964 smp->data.u.str.area = sl.rq.u.ptr;
965 smp->data.u.str.data = sl.rq.u.len;
966 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
967 }
968 else {
969 /* LEGACY version */
970 CHECK_HTTP_MESSAGE_FIRST();
971
972 txn = smp->strm->txn;
973 smp->data.type = SMP_T_STR;
974 smp->data.u.str.data = txn->req.sl.rq.u_l;
975 smp->data.u.str.area = ci_head(txn->req.chn) + txn->req.sl.rq.u;
976 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
977 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200978 return 1;
979}
980
981static int smp_fetch_url_ip(const struct arg *args, struct sample *smp, const char *kw, void *private)
982{
983 struct http_txn *txn;
984 struct sockaddr_storage addr;
985
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200986 if (IS_HTX_SMP(smp) || (smp->px->mode == PR_MODE_TCP)) {
987 /* HTX version */
988 struct htx *htx = smp_prefetch_htx(smp, args);
989 union h1_sl sl;
990
991 if (!htx)
992 return 0;
993 sl = http_find_stline(htx);
994 url2sa(sl.rq.u.ptr, sl.rq.u.len, &addr, NULL);
995 }
996 else {
997 /* LEGACY version */
998 CHECK_HTTP_MESSAGE_FIRST();
999
1000 txn = smp->strm->txn;
1001 url2sa(ci_head(txn->req.chn) + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &addr, NULL);
1002 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001003
Willy Tarreau79e57332018-10-02 16:01:16 +02001004 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
1005 return 0;
1006
1007 smp->data.type = SMP_T_IPV4;
1008 smp->data.u.ipv4 = ((struct sockaddr_in *)&addr)->sin_addr;
1009 smp->flags = 0;
1010 return 1;
1011}
1012
1013static int smp_fetch_url_port(const struct arg *args, struct sample *smp, const char *kw, void *private)
1014{
1015 struct http_txn *txn;
1016 struct sockaddr_storage addr;
1017
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001018 if (IS_HTX_SMP(smp) || (smp->px->mode == PR_MODE_TCP)) {
1019 /* HTX version */
1020 struct htx *htx = smp_prefetch_htx(smp, args);
1021 union h1_sl sl;
Willy Tarreau79e57332018-10-02 16:01:16 +02001022
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001023 if (!htx)
1024 return 0;
1025 sl = http_find_stline(htx);
1026 url2sa(sl.rq.u.ptr, sl.rq.u.len, &addr, NULL);
1027 }
1028 else {
1029 /* LEGACY version */
1030 CHECK_HTTP_MESSAGE_FIRST();
1031
1032 txn = smp->strm->txn;
1033 url2sa(ci_head(txn->req.chn) + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &addr, NULL);
1034 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001035 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
1036 return 0;
1037
1038 smp->data.type = SMP_T_SINT;
1039 smp->data.u.sint = ntohs(((struct sockaddr_in *)&addr)->sin_port);
1040 smp->flags = 0;
1041 return 1;
1042}
1043
1044/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
1045 * Accepts an optional argument of type string containing the header field name,
1046 * and an optional argument of type signed or unsigned integer to request an
1047 * explicit occurrence of the header. Note that in the event of a missing name,
1048 * headers are considered from the first one. It does not stop on commas and
1049 * returns full lines instead (useful for User-Agent or Date for example).
1050 */
1051static int smp_fetch_fhdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
1052{
Willy Tarreau79e57332018-10-02 16:01:16 +02001053 int occ = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001054
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001055 if (IS_HTX_SMP(smp) || (smp->px->mode == PR_MODE_TCP)) {
1056 /* HTX version */
1057 struct htx *htx = smp_prefetch_htx(smp, args);
1058 struct http_hdr_ctx *ctx = smp->ctx.a[0];
1059 struct ist name;
Willy Tarreau79e57332018-10-02 16:01:16 +02001060
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001061 if (!ctx) {
1062 /* first call */
1063 ctx = &static_http_hdr_ctx;
1064 ctx->blk = NULL;
1065 smp->ctx.a[0] = ctx;
1066 }
1067
1068 if (args) {
1069 if (args[0].type != ARGT_STR)
1070 return 0;
1071 name.ptr = args[0].data.str.area;
1072 name.len = args[0].data.str.data;
1073
1074 if (args[1].type == ARGT_SINT)
1075 occ = args[1].data.sint;
1076 }
1077
1078 if (!htx)
Willy Tarreau79e57332018-10-02 16:01:16 +02001079 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001080
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001081 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
1082 /* search for header from the beginning */
1083 ctx->blk = NULL;
1084
1085 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
1086 /* no explicit occurrence and single fetch => last header by default */
1087 occ = -1;
1088
1089 if (!occ)
1090 /* prepare to report multiple occurrences for ACL fetches */
1091 smp->flags |= SMP_F_NOT_LAST;
1092
1093 smp->data.type = SMP_T_STR;
1094 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
1095 if (http_get_htx_fhdr(htx, name, occ, ctx, &smp->data.u.str.area, &smp->data.u.str.data))
1096 return 1;
Willy Tarreau79e57332018-10-02 16:01:16 +02001097 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001098 else {
1099 /* LEGACY version */
1100 struct hdr_idx *idx;
1101 struct hdr_ctx *ctx = smp->ctx.a[0];
1102 const struct http_msg *msg;
1103 const char *name_str = NULL;
1104 int name_len = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001105
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001106 if (!ctx) {
1107 /* first call */
1108 ctx = &static_hdr_ctx;
1109 ctx->idx = 0;
1110 smp->ctx.a[0] = ctx;
1111 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001112
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001113 if (args) {
1114 if (args[0].type != ARGT_STR)
1115 return 0;
1116 name_str = args[0].data.str.area;
1117 name_len = args[0].data.str.data;
Willy Tarreau79e57332018-10-02 16:01:16 +02001118
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001119 if (args[1].type == ARGT_SINT)
1120 occ = args[1].data.sint;
1121 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001122
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001123 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau79e57332018-10-02 16:01:16 +02001124
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001125 idx = &smp->strm->txn->hdr_idx;
1126 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 +02001127
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001128 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
1129 /* search for header from the beginning */
1130 ctx->idx = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001131
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001132 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
1133 /* no explicit occurrence and single fetch => last header by default */
1134 occ = -1;
1135
1136 if (!occ)
1137 /* prepare to report multiple occurrences for ACL fetches */
1138 smp->flags |= SMP_F_NOT_LAST;
1139
1140 smp->data.type = SMP_T_STR;
1141 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
1142 if (http_get_fhdr(msg, name_str, name_len, idx, occ, ctx, &smp->data.u.str.area, &smp->data.u.str.data))
1143 return 1;
1144 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001145 smp->flags &= ~SMP_F_NOT_LAST;
1146 return 0;
1147}
1148
1149/* 6. Check on HTTP header count. The number of occurrences is returned.
1150 * Accepts exactly 1 argument of type string. It does not stop on commas and
1151 * returns full lines instead (useful for User-Agent or Date for example).
1152 */
1153static int smp_fetch_fhdr_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
1154{
Willy Tarreau79e57332018-10-02 16:01:16 +02001155 int cnt;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001156
1157 if (IS_HTX_SMP(smp) || (smp->px->mode == PR_MODE_TCP)) {
1158 /* HTX version */
1159 struct htx *htx = smp_prefetch_htx(smp, args);
1160 struct http_hdr_ctx ctx;
1161 struct ist name;
1162
1163 if (!htx)
1164 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001165
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001166 if (args && args->type == ARGT_STR) {
1167 name.ptr = args->data.str.area;
1168 name.len = args->data.str.data;
Olivier Houcharde2c78cd2018-11-21 13:49:48 +01001169 } else {
1170 name.ptr = NULL;
1171 name.len = 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001172 }
1173
1174 ctx.blk = NULL;
1175 cnt = 0;
1176 while (http_find_header(htx, name, &ctx, 1))
1177 cnt++;
Willy Tarreau79e57332018-10-02 16:01:16 +02001178 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001179 else {
1180 /* LEGACY version */
1181 struct hdr_idx *idx;
1182 struct hdr_ctx ctx;
1183 const struct http_msg *msg;
1184 const char *name = NULL;
1185 int len = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001186
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001187 if (args && args->type == ARGT_STR) {
1188 name = args->data.str.area;
1189 len = args->data.str.data;
1190 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001191
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001192 CHECK_HTTP_MESSAGE_FIRST();
1193
1194 idx = &smp->strm->txn->hdr_idx;
1195 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 +02001196
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001197 ctx.idx = 0;
1198 cnt = 0;
1199 while (http_find_full_header2(name, len, ci_head(msg->chn), idx, &ctx))
1200 cnt++;
1201 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001202
1203 smp->data.type = SMP_T_SINT;
1204 smp->data.u.sint = cnt;
1205 smp->flags = SMP_F_VOL_HDR;
1206 return 1;
1207}
1208
1209static int smp_fetch_hdr_names(const struct arg *args, struct sample *smp, const char *kw, void *private)
1210{
Willy Tarreau79e57332018-10-02 16:01:16 +02001211 struct buffer *temp;
1212 char del = ',';
1213
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001214 if (IS_HTX_SMP(smp) || (smp->px->mode == PR_MODE_TCP)) {
1215 /* HTX version */
1216 struct htx *htx = smp_prefetch_htx(smp, args);
1217 int32_t pos;
Willy Tarreau79e57332018-10-02 16:01:16 +02001218
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001219 if (!htx)
1220 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001221
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001222 if (args && args->type == ARGT_STR)
1223 del = *args[0].data.str.area;
Willy Tarreau79e57332018-10-02 16:01:16 +02001224
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001225 temp = get_trash_chunk();
1226 for (pos = htx_get_head(htx); pos != -1; pos = htx_get_next(htx, pos)) {
1227 struct htx_blk *blk = htx_get_blk(htx, pos);
1228 enum htx_blk_type type = htx_get_blk_type(blk);
1229 struct ist n;
Willy Tarreau79e57332018-10-02 16:01:16 +02001230
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001231 if (type == HTX_BLK_EOH)
1232 break;
1233 if (type != HTX_BLK_HDR)
1234 continue;
1235 n = htx_get_blk_name(htx, blk);
1236
1237 if (temp->data)
1238 temp->area[temp->data++] = del;
1239 chunk_memcat(temp, n.ptr, n.len);
1240 }
1241 }
1242 else {
1243 /* LEGACY version */
1244 struct hdr_idx *idx;
1245 struct hdr_ctx ctx;
1246 const struct http_msg *msg;
1247
1248 if (args && args->type == ARGT_STR)
1249 del = *args[0].data.str.area;
1250
1251 CHECK_HTTP_MESSAGE_FIRST();
1252
1253 idx = &smp->strm->txn->hdr_idx;
1254 msg = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &smp->strm->txn->req : &smp->strm->txn->rsp;
1255
1256 temp = get_trash_chunk();
1257
1258 ctx.idx = 0;
1259 while (http_find_next_header(ci_head(msg->chn), idx, &ctx)) {
1260 if (temp->data)
1261 temp->area[temp->data++] = del;
1262 memcpy(temp->area + temp->data, ctx.line, ctx.del);
1263 temp->data += ctx.del;
1264 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001265 }
1266
1267 smp->data.type = SMP_T_STR;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001268 smp->data.u.str = *temp;
Willy Tarreau79e57332018-10-02 16:01:16 +02001269 smp->flags = SMP_F_VOL_HDR;
1270 return 1;
1271}
1272
1273/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
1274 * Accepts an optional argument of type string containing the header field name,
1275 * and an optional argument of type signed or unsigned integer to request an
1276 * explicit occurrence of the header. Note that in the event of a missing name,
1277 * headers are considered from the first one.
1278 */
1279static int smp_fetch_hdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
1280{
Willy Tarreau79e57332018-10-02 16:01:16 +02001281 int occ = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001282
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001283 if (IS_HTX_SMP(smp) || (smp->px->mode == PR_MODE_TCP)) {
1284 /* HTX version */
1285 struct htx *htx = smp_prefetch_htx(smp, args);
1286 struct http_hdr_ctx *ctx = smp->ctx.a[0];
1287 struct ist name;
1288
1289 if (!ctx) {
1290 /* first call */
1291 ctx = &static_http_hdr_ctx;
1292 ctx->blk = NULL;
1293 smp->ctx.a[0] = ctx;
1294 }
1295
1296 if (args) {
1297 if (args[0].type != ARGT_STR)
1298 return 0;
1299 name.ptr = args[0].data.str.area;
1300 name.len = args[0].data.str.data;
1301
1302 if (args[1].type == ARGT_SINT)
1303 occ = args[1].data.sint;
1304 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001305
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001306 if (!htx)
Willy Tarreau79e57332018-10-02 16:01:16 +02001307 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001308
1309 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
1310 /* search for header from the beginning */
1311 ctx->blk = NULL;
1312
1313 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
1314 /* no explicit occurrence and single fetch => last header by default */
1315 occ = -1;
1316
1317 if (!occ)
1318 /* prepare to report multiple occurrences for ACL fetches */
1319 smp->flags |= SMP_F_NOT_LAST;
Willy Tarreau79e57332018-10-02 16:01:16 +02001320
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001321 smp->data.type = SMP_T_STR;
1322 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
1323 if (http_get_htx_hdr(htx, name, occ, ctx, &smp->data.u.str.area, &smp->data.u.str.data))
1324 return 1;
Willy Tarreau79e57332018-10-02 16:01:16 +02001325 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001326 else {
1327 /* LEGACY version */
1328 struct hdr_idx *idx;
1329 struct hdr_ctx *ctx = smp->ctx.a[0];
1330 const struct http_msg *msg;
1331 const char *name_str = NULL;
1332 int name_len = 0;
1333
1334 if (!ctx) {
1335 /* first call */
1336 ctx = &static_hdr_ctx;
1337 ctx->idx = 0;
1338 smp->ctx.a[0] = ctx;
1339 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001340
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001341 if (args) {
1342 if (args[0].type != ARGT_STR)
1343 return 0;
1344 name_str = args[0].data.str.area;
1345 name_len = args[0].data.str.data;
Willy Tarreau79e57332018-10-02 16:01:16 +02001346
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001347 if (args[1].type == ARGT_SINT)
1348 occ = args[1].data.sint;
1349 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001350
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001351 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau79e57332018-10-02 16:01:16 +02001352
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001353 idx = &smp->strm->txn->hdr_idx;
1354 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 +02001355
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001356 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
1357 /* search for header from the beginning */
1358 ctx->idx = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001359
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001360 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
1361 /* no explicit occurrence and single fetch => last header by default */
1362 occ = -1;
1363
1364 if (!occ)
1365 /* prepare to report multiple occurrences for ACL fetches */
1366 smp->flags |= SMP_F_NOT_LAST;
1367
1368 smp->data.type = SMP_T_STR;
1369 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
1370 if (http_get_hdr(msg, name_str, name_len, idx, occ, ctx, &smp->data.u.str.area, &smp->data.u.str.data))
1371 return 1;
1372 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001373
1374 smp->flags &= ~SMP_F_NOT_LAST;
1375 return 0;
1376}
1377
1378/* 6. Check on HTTP header count. The number of occurrences is returned.
1379 * Accepts exactly 1 argument of type string.
1380 */
1381static int smp_fetch_hdr_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
1382{
Willy Tarreau79e57332018-10-02 16:01:16 +02001383 int cnt;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001384
1385 if (IS_HTX_SMP(smp) || (smp->px->mode == PR_MODE_TCP)) {
1386 /* HTX version */
1387 struct htx *htx = smp_prefetch_htx(smp, args);
1388 struct http_hdr_ctx ctx;
1389 struct ist name;
1390
1391 if (!htx)
1392 return 0;
1393
1394 if (args && args->type == ARGT_STR) {
1395 name.ptr = args->data.str.area;
1396 name.len = args->data.str.data;
Olivier Houcharde2c78cd2018-11-21 13:49:48 +01001397 } else {
1398 name.ptr = NULL;
1399 name.len = 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001400 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001401
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001402 ctx.blk = NULL;
1403 cnt = 0;
1404 while (http_find_header(htx, name, &ctx, 0))
1405 cnt++;
Willy Tarreau79e57332018-10-02 16:01:16 +02001406 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001407 else {
1408 /* LEGACY version */
1409 struct hdr_idx *idx;
1410 struct hdr_ctx ctx;
1411 const struct http_msg *msg;
1412 const char *name = NULL;
1413 int len = 0;
1414
1415 if (args && args->type == ARGT_STR) {
1416 name = args->data.str.area;
1417 len = args->data.str.data;
1418 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001419
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001420 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau79e57332018-10-02 16:01:16 +02001421
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001422 idx = &smp->strm->txn->hdr_idx;
1423 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 +02001424
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001425 ctx.idx = 0;
1426 cnt = 0;
1427 while (http_find_header2(name, len, ci_head(msg->chn), idx, &ctx))
1428 cnt++;
1429 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001430
1431 smp->data.type = SMP_T_SINT;
1432 smp->data.u.sint = cnt;
1433 smp->flags = SMP_F_VOL_HDR;
1434 return 1;
1435}
1436
1437/* Fetch an HTTP header's integer value. The integer value is returned. It
1438 * takes a mandatory argument of type string and an optional one of type int
1439 * to designate a specific occurrence. It returns an unsigned integer, which
1440 * may or may not be appropriate for everything.
1441 */
1442static int smp_fetch_hdr_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
1443{
1444 int ret = smp_fetch_hdr(args, smp, kw, private);
1445
1446 if (ret > 0) {
1447 smp->data.type = SMP_T_SINT;
1448 smp->data.u.sint = strl2ic(smp->data.u.str.area,
1449 smp->data.u.str.data);
1450 }
1451
1452 return ret;
1453}
1454
1455/* Fetch an HTTP header's IP value. takes a mandatory argument of type string
1456 * and an optional one of type int to designate a specific occurrence.
1457 * It returns an IPv4 or IPv6 address.
1458 */
1459static int smp_fetch_hdr_ip(const struct arg *args, struct sample *smp, const char *kw, void *private)
1460{
1461 int ret;
1462
1463 while ((ret = smp_fetch_hdr(args, smp, kw, private)) > 0) {
1464 if (url2ipv4((char *) smp->data.u.str.area, &smp->data.u.ipv4)) {
1465 smp->data.type = SMP_T_IPV4;
1466 break;
1467 } else {
1468 struct buffer *temp = get_trash_chunk();
1469 if (smp->data.u.str.data < temp->size - 1) {
1470 memcpy(temp->area, smp->data.u.str.area,
1471 smp->data.u.str.data);
1472 temp->area[smp->data.u.str.data] = '\0';
1473 if (inet_pton(AF_INET6, temp->area, &smp->data.u.ipv6)) {
1474 smp->data.type = SMP_T_IPV6;
1475 break;
1476 }
1477 }
1478 }
1479
1480 /* if the header doesn't match an IP address, fetch next one */
1481 if (!(smp->flags & SMP_F_NOT_LAST))
1482 return 0;
1483 }
1484 return ret;
1485}
1486
1487/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
1488 * the first '/' after the possible hostname, and ends before the possible '?'.
1489 */
1490static int smp_fetch_path(const struct arg *args, struct sample *smp, const char *kw, void *private)
1491{
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001492 if (IS_HTX_SMP(smp) || (smp->px->mode == PR_MODE_TCP)) {
1493 /* HTX version */
1494 struct htx *htx = smp_prefetch_htx(smp, args);
1495 union h1_sl sl;
1496 struct ist path;
1497 size_t len;
Willy Tarreau79e57332018-10-02 16:01:16 +02001498
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001499 if (!htx)
1500 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001501
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001502 sl = http_find_stline(htx);
1503 path = http_get_path(sl.rq.u);
1504 if (!path.ptr)
1505 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001506
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001507 for (len = 0; len < path.len && *(path.ptr + len) != '?'; len++)
Willy Tarreau79e57332018-10-02 16:01:16 +02001508
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001509 /* OK, we got the '/' ! */
1510 smp->data.type = SMP_T_STR;
1511 smp->data.u.str.area = path.ptr;
1512 smp->data.u.str.data = len;
1513 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
1514 }
1515 else {
1516 struct http_txn *txn;
1517 char *ptr, *end;
Willy Tarreau79e57332018-10-02 16:01:16 +02001518
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001519 CHECK_HTTP_MESSAGE_FIRST();
1520
1521 txn = smp->strm->txn;
1522 end = ci_head(txn->req.chn) + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
1523 ptr = http_txn_get_path(txn);
1524 if (!ptr)
1525 return 0;
1526
1527 /* OK, we got the '/' ! */
1528 smp->data.type = SMP_T_STR;
1529 smp->data.u.str.area = ptr;
1530
1531 while (ptr < end && *ptr != '?')
1532 ptr++;
1533
1534 smp->data.u.str.data = ptr - smp->data.u.str.area;
1535 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
1536 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001537 return 1;
1538}
1539
1540/* This produces a concatenation of the first occurrence of the Host header
1541 * followed by the path component if it begins with a slash ('/'). This means
1542 * that '*' will not be added, resulting in exactly the first Host entry.
1543 * If no Host header is found, then the path is returned as-is. The returned
1544 * value is stored in the trash so it does not need to be marked constant.
1545 * The returned sample is of type string.
1546 */
1547static int smp_fetch_base(const struct arg *args, struct sample *smp, const char *kw, void *private)
1548{
Willy Tarreau79e57332018-10-02 16:01:16 +02001549 struct buffer *temp;
1550
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001551 if (IS_HTX_SMP(smp) || (smp->px->mode == PR_MODE_TCP)) {
1552 /* HTX version */
1553 struct htx *htx = smp_prefetch_htx(smp, args);
1554 union h1_sl sl;
1555 struct http_hdr_ctx ctx;
1556 struct ist path;
Willy Tarreau79e57332018-10-02 16:01:16 +02001557
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001558 if (!htx)
1559 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001560
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001561 ctx.blk = NULL;
1562 if (!http_find_header(htx, ist("Host"), &ctx, 0) || !ctx.value.len)
1563 return smp_fetch_path(args, smp, kw, private);
Willy Tarreau79e57332018-10-02 16:01:16 +02001564
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001565 /* OK we have the header value in ctx.value */
1566 temp = get_trash_chunk();
1567 chunk_memcat(temp, ctx.value.ptr, ctx.value.len);
1568
1569 /* now retrieve the path */
1570 sl = http_find_stline(htx);
1571 path = http_get_path(sl.rq.u);
1572 if (path.ptr) {
1573 size_t len;
1574
1575 for (len = 0; len < path.len && *(path.ptr) != '?'; len++);
1576 if (len && *(path.ptr) == '/')
1577 chunk_memcat(temp, path.ptr, len);
1578 }
1579
1580 smp->data.type = SMP_T_STR;
1581 smp->data.u.str = *temp;
1582 }
1583 else {
1584 /* LEGACY version */
1585 struct http_txn *txn;
1586 char *ptr, *end, *beg;
1587 struct hdr_ctx ctx;
1588
1589 CHECK_HTTP_MESSAGE_FIRST();
1590
1591 txn = smp->strm->txn;
1592 ctx.idx = 0;
1593 if (!http_find_header2("Host", 4, ci_head(txn->req.chn), &txn->hdr_idx, &ctx) || !ctx.vlen)
1594 return smp_fetch_path(args, smp, kw, private);
1595
1596 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
1597 temp = get_trash_chunk();
1598 memcpy(temp->area, ctx.line + ctx.val, ctx.vlen);
1599 smp->data.type = SMP_T_STR;
1600 smp->data.u.str.area = temp->area;
1601 smp->data.u.str.data = ctx.vlen;
Willy Tarreau79e57332018-10-02 16:01:16 +02001602
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001603 /* now retrieve the path */
1604 end = ci_head(txn->req.chn) + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
1605 beg = http_txn_get_path(txn);
1606 if (!beg)
1607 beg = end;
Willy Tarreau79e57332018-10-02 16:01:16 +02001608
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001609 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
1610
1611 if (beg < ptr && *beg == '/') {
1612 memcpy(smp->data.u.str.area + smp->data.u.str.data, beg,
1613 ptr - beg);
1614 smp->data.u.str.data += ptr - beg;
1615 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001616 }
1617
1618 smp->flags = SMP_F_VOL_1ST;
1619 return 1;
1620}
1621
1622/* This produces a 32-bit hash of the concatenation of the first occurrence of
1623 * the Host header followed by the path component if it begins with a slash ('/').
1624 * This means that '*' will not be added, resulting in exactly the first Host
1625 * entry. If no Host header is found, then the path is used. The resulting value
1626 * is hashed using the path hash followed by a full avalanche hash and provides a
1627 * 32-bit integer value. This fetch is useful for tracking per-path activity on
1628 * high-traffic sites without having to store whole paths.
1629 */
1630static int smp_fetch_base32(const struct arg *args, struct sample *smp, const char *kw, void *private)
1631{
Willy Tarreau79e57332018-10-02 16:01:16 +02001632 unsigned int hash = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001633
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001634 if (IS_HTX_SMP(smp) || (smp->px->mode == PR_MODE_TCP)) {
1635 /* HTX version */
1636 struct htx *htx = smp_prefetch_htx(smp, args);
1637 union h1_sl sl;
1638 struct http_hdr_ctx ctx;
1639 struct ist path;
Willy Tarreau79e57332018-10-02 16:01:16 +02001640
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001641 if (!htx)
1642 return 0;
1643
1644 ctx.blk = NULL;
1645 if (!http_find_header(htx, ist("Host"), &ctx, 0)) {
1646 /* OK we have the header value in ctx.value */
1647 while (ctx.value.len--)
1648 hash = *(ctx.value.ptr++) + (hash << 6) + (hash << 16) - hash;
1649 }
1650
1651 /* now retrieve the path */
1652 sl = http_find_stline(htx);
1653 path = http_get_path(sl.rq.u);
1654 if (path.ptr) {
1655 size_t len;
1656
1657 for (len = 0; len < path.len && *(path.ptr) != '?'; len++);
1658 if (len && *(path.ptr) == '/') {
1659 while (len--)
1660 hash = *(path.ptr++) + (hash << 6) + (hash << 16) - hash;
1661 }
1662 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001663 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001664 else {
1665 /* LEGACY version */
1666 struct http_txn *txn;
1667 struct hdr_ctx ctx;
1668 char *ptr, *beg, *end;
1669 int len;
1670
1671 CHECK_HTTP_MESSAGE_FIRST();
1672
1673 txn = smp->strm->txn;
1674 ctx.idx = 0;
1675 if (http_find_header2("Host", 4, ci_head(txn->req.chn), &txn->hdr_idx, &ctx)) {
1676 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
1677 ptr = ctx.line + ctx.val;
1678 len = ctx.vlen;
1679 while (len--)
1680 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
1681 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001682
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001683 /* now retrieve the path */
1684 end = ci_head(txn->req.chn) + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
1685 beg = http_txn_get_path(txn);
1686 if (!beg)
1687 beg = end;
Willy Tarreau79e57332018-10-02 16:01:16 +02001688
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001689 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
Willy Tarreau79e57332018-10-02 16:01:16 +02001690
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001691 if (beg < ptr && *beg == '/') {
1692 while (beg < ptr)
1693 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
1694 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001695 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001696
Willy Tarreau79e57332018-10-02 16:01:16 +02001697 hash = full_hash(hash);
1698
1699 smp->data.type = SMP_T_SINT;
1700 smp->data.u.sint = hash;
1701 smp->flags = SMP_F_VOL_1ST;
1702 return 1;
1703}
1704
1705/* This concatenates the source address with the 32-bit hash of the Host and
1706 * path as returned by smp_fetch_base32(). The idea is to have per-source and
1707 * per-path counters. The result is a binary block from 8 to 20 bytes depending
1708 * on the source address length. The path hash is stored before the address so
1709 * that in environments where IPv6 is insignificant, truncating the output to
1710 * 8 bytes would still work.
1711 */
1712static int smp_fetch_base32_src(const struct arg *args, struct sample *smp, const char *kw, void *private)
1713{
1714 struct buffer *temp;
1715 struct connection *cli_conn = objt_conn(smp->sess->origin);
1716
1717 if (!cli_conn)
1718 return 0;
1719
1720 if (!smp_fetch_base32(args, smp, kw, private))
1721 return 0;
1722
1723 temp = get_trash_chunk();
1724 *(unsigned int *) temp->area = htonl(smp->data.u.sint);
1725 temp->data += sizeof(unsigned int);
1726
1727 switch (cli_conn->addr.from.ss_family) {
1728 case AF_INET:
1729 memcpy(temp->area + temp->data,
1730 &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr,
1731 4);
1732 temp->data += 4;
1733 break;
1734 case AF_INET6:
1735 memcpy(temp->area + temp->data,
1736 &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr,
1737 16);
1738 temp->data += 16;
1739 break;
1740 default:
1741 return 0;
1742 }
1743
1744 smp->data.u.str = *temp;
1745 smp->data.type = SMP_T_BIN;
1746 return 1;
1747}
1748
1749/* Extracts the query string, which comes after the question mark '?'. If no
1750 * question mark is found, nothing is returned. Otherwise it returns a sample
1751 * of type string carrying the whole query string.
1752 */
1753static int smp_fetch_query(const struct arg *args, struct sample *smp, const char *kw, void *private)
1754{
Willy Tarreau79e57332018-10-02 16:01:16 +02001755 char *ptr, *end;
1756
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001757 if (IS_HTX_SMP(smp) || (smp->px->mode == PR_MODE_TCP)) {
1758 /* HTX version */
1759 struct htx *htx = smp_prefetch_htx(smp, args);
1760 union h1_sl sl;
1761
1762 if (!htx)
1763 return 0;
1764
1765 sl = http_find_stline(htx);
1766 ptr = sl.rq.u.ptr;
1767 end = sl.rq.u.ptr + sl.rq.u.len;
1768 }
1769 else {
1770 /* LEGACY version */
1771 struct http_txn *txn;
1772
1773 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau79e57332018-10-02 16:01:16 +02001774
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001775 txn = smp->strm->txn;
1776 ptr = ci_head(txn->req.chn) + txn->req.sl.rq.u;
1777 end = ptr + txn->req.sl.rq.u_l;
1778 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001779
1780 /* look up the '?' */
1781 do {
1782 if (ptr == end)
1783 return 0;
1784 } while (*ptr++ != '?');
1785
1786 smp->data.type = SMP_T_STR;
1787 smp->data.u.str.area = ptr;
1788 smp->data.u.str.data = end - ptr;
1789 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
1790 return 1;
1791}
1792
1793static int smp_fetch_proto_http(const struct arg *args, struct sample *smp, const char *kw, void *private)
1794{
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001795 if (IS_HTX_SMP(smp) || (smp->px->mode == PR_MODE_TCP)) {
1796 /* HTX version */
1797 struct htx *htx = smp_prefetch_htx(smp, args);
Willy Tarreau79e57332018-10-02 16:01:16 +02001798
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001799 if (!htx)
1800 return 0;
1801 }
1802 else {
1803 /* LEGACY version */
Willy Tarreau79e57332018-10-02 16:01:16 +02001804
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001805 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
1806 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
1807 */
1808 CHECK_HTTP_MESSAGE_FIRST_PERM();
1809 }
1810 smp->data.type = SMP_T_BOOL;
Willy Tarreau79e57332018-10-02 16:01:16 +02001811 smp->data.u.sint = 1;
1812 return 1;
1813}
1814
1815/* return a valid test if the current request is the first one on the connection */
1816static int smp_fetch_http_first_req(const struct arg *args, struct sample *smp, const char *kw, void *private)
1817{
1818 smp->data.type = SMP_T_BOOL;
1819 smp->data.u.sint = !(smp->strm->txn->flags & TX_NOT_FIRST);
1820 return 1;
1821}
1822
1823/* Accepts exactly 1 argument of type userlist */
1824static int smp_fetch_http_auth(const struct arg *args, struct sample *smp, const char *kw, void *private)
1825{
1826
1827 if (!args || args->type != ARGT_USR)
1828 return 0;
1829
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001830 if (IS_HTX_SMP(smp) || (smp->px->mode == PR_MODE_TCP)) {
1831 /* HTX version */
1832 struct htx *htx = smp_prefetch_htx(smp, args);
Willy Tarreau79e57332018-10-02 16:01:16 +02001833
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001834 if (!htx)
1835 return 0;
1836 }
1837 else {
1838 /* LEGACY version */
1839 CHECK_HTTP_MESSAGE_FIRST();
1840 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001841
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001842 if (!get_http_auth(smp))
1843 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001844 smp->data.type = SMP_T_BOOL;
1845 smp->data.u.sint = check_user(args->data.usr, smp->strm->txn->auth.user,
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001846 smp->strm->txn->auth.pass);
Willy Tarreau79e57332018-10-02 16:01:16 +02001847 return 1;
1848}
1849
1850/* Accepts exactly 1 argument of type userlist */
1851static int smp_fetch_http_auth_grp(const struct arg *args, struct sample *smp, const char *kw, void *private)
1852{
1853 if (!args || args->type != ARGT_USR)
1854 return 0;
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);
1859
1860 if (!htx)
1861 return 0;
1862 }
1863 else {
1864 /* LEGACY version */
1865 CHECK_HTTP_MESSAGE_FIRST();
1866 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001867
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001868 if (!get_http_auth(smp))
Willy Tarreau79e57332018-10-02 16:01:16 +02001869 return 0;
1870
1871 /* if the user does not belong to the userlist or has a wrong password,
1872 * report that it unconditionally does not match. Otherwise we return
1873 * a string containing the username.
1874 */
1875 if (!check_user(args->data.usr, smp->strm->txn->auth.user,
1876 smp->strm->txn->auth.pass))
1877 return 0;
1878
1879 /* pat_match_auth() will need the user list */
1880 smp->ctx.a[0] = args->data.usr;
1881
1882 smp->data.type = SMP_T_STR;
1883 smp->flags = SMP_F_CONST;
1884 smp->data.u.str.area = smp->strm->txn->auth.user;
1885 smp->data.u.str.data = strlen(smp->strm->txn->auth.user);
1886
1887 return 1;
1888}
1889
1890/* Fetch a captured HTTP request header. The index is the position of
1891 * the "capture" option in the configuration file
1892 */
1893static int smp_fetch_capture_req_hdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
1894{
1895 struct proxy *fe = strm_fe(smp->strm);
1896 int idx;
1897
1898 if (!args || args->type != ARGT_SINT)
1899 return 0;
1900
1901 idx = args->data.sint;
1902
1903 if (idx > (fe->nb_req_cap - 1) || smp->strm->req_cap == NULL || smp->strm->req_cap[idx] == NULL)
1904 return 0;
1905
1906 smp->data.type = SMP_T_STR;
1907 smp->flags |= SMP_F_CONST;
1908 smp->data.u.str.area = smp->strm->req_cap[idx];
1909 smp->data.u.str.data = strlen(smp->strm->req_cap[idx]);
1910
1911 return 1;
1912}
1913
1914/* Fetch a captured HTTP response header. The index is the position of
1915 * the "capture" option in the configuration file
1916 */
1917static int smp_fetch_capture_res_hdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
1918{
1919 struct proxy *fe = strm_fe(smp->strm);
1920 int idx;
1921
1922 if (!args || args->type != ARGT_SINT)
1923 return 0;
1924
1925 idx = args->data.sint;
1926
1927 if (idx > (fe->nb_rsp_cap - 1) || smp->strm->res_cap == NULL || smp->strm->res_cap[idx] == NULL)
1928 return 0;
1929
1930 smp->data.type = SMP_T_STR;
1931 smp->flags |= SMP_F_CONST;
1932 smp->data.u.str.area = smp->strm->res_cap[idx];
1933 smp->data.u.str.data = strlen(smp->strm->res_cap[idx]);
1934
1935 return 1;
1936}
1937
1938/* Extracts the METHOD in the HTTP request, the txn->uri should be filled before the call */
1939static int smp_fetch_capture_req_method(const struct arg *args, struct sample *smp, const char *kw, void *private)
1940{
1941 struct buffer *temp;
1942 struct http_txn *txn = smp->strm->txn;
1943 char *ptr;
1944
1945 if (!txn || !txn->uri)
1946 return 0;
1947
1948 ptr = txn->uri;
1949
1950 while (*ptr != ' ' && *ptr != '\0') /* find first space */
1951 ptr++;
1952
1953 temp = get_trash_chunk();
1954 temp->area = txn->uri;
1955 temp->data = ptr - txn->uri;
1956 smp->data.u.str = *temp;
1957 smp->data.type = SMP_T_STR;
1958 smp->flags = SMP_F_CONST;
1959
1960 return 1;
1961
1962}
1963
1964/* Extracts the path in the HTTP request, the txn->uri should be filled before the call */
1965static int smp_fetch_capture_req_uri(const struct arg *args, struct sample *smp, const char *kw, void *private)
1966{
1967 struct http_txn *txn = smp->strm->txn;
1968 struct ist path;
1969 const char *ptr;
1970
1971 if (!txn || !txn->uri)
1972 return 0;
1973
1974 ptr = txn->uri;
1975
1976 while (*ptr != ' ' && *ptr != '\0') /* find first space */
1977 ptr++;
1978
1979 if (!*ptr)
1980 return 0;
1981
Christopher Faulet78337bb2018-11-15 14:35:18 +01001982 /* skip the first space and find space after URI */
1983 path = ist2(++ptr, 0);
1984 while (*ptr != ' ' && *ptr != '\0')
1985 ptr++;
1986 path.len = ptr - path.ptr;
Willy Tarreau79e57332018-10-02 16:01:16 +02001987
Christopher Faulet78337bb2018-11-15 14:35:18 +01001988 path = http_get_path(path);
Willy Tarreau79e57332018-10-02 16:01:16 +02001989 if (!path.ptr)
1990 return 0;
1991
1992 smp->data.u.str.area = path.ptr;
1993 smp->data.u.str.data = path.len;
1994 smp->data.type = SMP_T_STR;
1995 smp->flags = SMP_F_CONST;
1996
1997 return 1;
1998}
1999
2000/* Retrieves the HTTP version from the request (either 1.0 or 1.1) and emits it
2001 * as a string (either "HTTP/1.0" or "HTTP/1.1").
2002 */
2003static int smp_fetch_capture_req_ver(const struct arg *args, struct sample *smp, const char *kw, void *private)
2004{
2005 struct http_txn *txn = smp->strm->txn;
2006
2007 if (!txn || txn->req.msg_state < HTTP_MSG_HDR_FIRST)
2008 return 0;
2009
2010 if (txn->req.flags & HTTP_MSGF_VER_11)
2011 smp->data.u.str.area = "HTTP/1.1";
2012 else
2013 smp->data.u.str.area = "HTTP/1.0";
2014
2015 smp->data.u.str.data = 8;
2016 smp->data.type = SMP_T_STR;
2017 smp->flags = SMP_F_CONST;
2018 return 1;
2019
2020}
2021
2022/* Retrieves the HTTP version from the response (either 1.0 or 1.1) and emits it
2023 * as a string (either "HTTP/1.0" or "HTTP/1.1").
2024 */
2025static int smp_fetch_capture_res_ver(const struct arg *args, struct sample *smp, const char *kw, void *private)
2026{
2027 struct http_txn *txn = smp->strm->txn;
2028
2029 if (!txn || txn->rsp.msg_state < HTTP_MSG_HDR_FIRST)
2030 return 0;
2031
2032 if (txn->rsp.flags & HTTP_MSGF_VER_11)
2033 smp->data.u.str.area = "HTTP/1.1";
2034 else
2035 smp->data.u.str.area = "HTTP/1.0";
2036
2037 smp->data.u.str.data = 8;
2038 smp->data.type = SMP_T_STR;
2039 smp->flags = SMP_F_CONST;
2040 return 1;
2041
2042}
2043
2044/* Iterate over all cookies present in a message. The context is stored in
2045 * smp->ctx.a[0] for the in-header position, smp->ctx.a[1] for the
2046 * end-of-header-value, and smp->ctx.a[2] for the hdr_ctx. Depending on
2047 * the direction, multiple cookies may be parsed on the same line or not.
2048 * The cookie name is in args and the name length in args->data.str.len.
2049 * Accepts exactly 1 argument of type string. If the input options indicate
2050 * that no iterating is desired, then only last value is fetched if any.
2051 * The returned sample is of type CSTR. Can be used to parse cookies in other
2052 * files.
2053 */
2054static int smp_fetch_cookie(const struct arg *args, struct sample *smp, const char *kw, void *private)
2055{
Willy Tarreau79e57332018-10-02 16:01:16 +02002056 int occ = 0;
2057 int found = 0;
2058
2059 if (!args || args->type != ARGT_STR)
2060 return 0;
2061
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002062 if (IS_HTX_SMP(smp) || (smp->px->mode == PR_MODE_TCP)) {
2063 /* HTX version */
2064 struct htx *htx = smp_prefetch_htx(smp, args);
2065 struct http_hdr_ctx *ctx = smp->ctx.a[2];
2066 struct ist hdr;
Willy Tarreau79e57332018-10-02 16:01:16 +02002067
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002068 if (!ctx) {
2069 /* first call */
2070 ctx = &static_http_hdr_ctx;
2071 ctx->blk = NULL;
2072 smp->ctx.a[2] = ctx;
2073 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002074
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002075 if (!htx)
2076 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02002077
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002078 hdr = (((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
2079 ? ist("Cookie")
2080 : ist("Set-Cookie"));
Willy Tarreau79e57332018-10-02 16:01:16 +02002081
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002082 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
2083 /* no explicit occurrence and single fetch => last cookie by default */
2084 occ = -1;
Willy Tarreau79e57332018-10-02 16:01:16 +02002085
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002086 /* OK so basically here, either we want only one value and it's the
2087 * last one, or we want to iterate over all of them and we fetch the
2088 * next one.
Willy Tarreau79e57332018-10-02 16:01:16 +02002089 */
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002090
2091 if (!(smp->flags & SMP_F_NOT_LAST)) {
2092 /* search for the header from the beginning, we must first initialize
2093 * the search parameters.
2094 */
2095 smp->ctx.a[0] = NULL;
2096 ctx->blk = NULL;
2097 }
2098
2099 smp->flags |= SMP_F_VOL_HDR;
2100 while (1) {
2101 /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */
2102 if (!smp->ctx.a[0]) {
2103 if (!http_find_header(htx, hdr, ctx, 0))
2104 goto out;
2105
2106 if (ctx->value.len < args->data.str.data + 1)
2107 continue;
2108
2109 smp->ctx.a[0] = ctx->value.ptr;
2110 smp->ctx.a[1] = smp->ctx.a[0] + ctx->value.len;
2111 }
2112
2113 smp->data.type = SMP_T_STR;
2114 smp->flags |= SMP_F_CONST;
2115 smp->ctx.a[0] = http_extract_cookie_value(smp->ctx.a[0], smp->ctx.a[1],
2116 args->data.str.area, args->data.str.data,
2117 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
2118 &smp->data.u.str.area,
2119 &smp->data.u.str.data);
2120 if (smp->ctx.a[0]) {
2121 found = 1;
2122 if (occ >= 0) {
2123 /* one value was returned into smp->data.u.str.{str,len} */
2124 smp->flags |= SMP_F_NOT_LAST;
2125 return 1;
2126 }
2127 }
2128 /* if we're looking for last occurrence, let's loop */
2129 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002130 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002131 else {
2132 /* LEGACY version */
2133 struct http_txn *txn;
2134 struct hdr_idx *idx;
2135 struct hdr_ctx *ctx = smp->ctx.a[2];
2136 const struct http_msg *msg;
2137 const char *hdr_name;
2138 int hdr_name_len;
2139 char *sol;
Willy Tarreau79e57332018-10-02 16:01:16 +02002140
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002141 if (!ctx) {
2142 /* first call */
2143 ctx = &static_hdr_ctx;
2144 ctx->idx = 0;
2145 smp->ctx.a[2] = ctx;
2146 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002147
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002148 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau79e57332018-10-02 16:01:16 +02002149
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002150 txn = smp->strm->txn;
2151 idx = &smp->strm->txn->hdr_idx;
Willy Tarreau79e57332018-10-02 16:01:16 +02002152
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002153 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
2154 msg = &txn->req;
2155 hdr_name = "Cookie";
2156 hdr_name_len = 6;
2157 } else {
2158 msg = &txn->rsp;
2159 hdr_name = "Set-Cookie";
2160 hdr_name_len = 10;
Willy Tarreau79e57332018-10-02 16:01:16 +02002161 }
2162
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002163 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
2164 /* no explicit occurrence and single fetch => last cookie by default */
2165 occ = -1;
2166
2167 /* OK so basically here, either we want only one value and it's the
2168 * last one, or we want to iterate over all of them and we fetch the
2169 * next one.
2170 */
2171
2172 sol = ci_head(msg->chn);
2173 if (!(smp->flags & SMP_F_NOT_LAST)) {
2174 /* search for the header from the beginning, we must first initialize
2175 * the search parameters.
2176 */
2177 smp->ctx.a[0] = NULL;
2178 ctx->idx = 0;
2179 }
2180
2181 smp->flags |= SMP_F_VOL_HDR;
2182
2183 while (1) {
2184 /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */
2185 if (!smp->ctx.a[0]) {
2186 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, ctx))
2187 goto out;
2188
2189 if (ctx->vlen < args->data.str.data + 1)
2190 continue;
2191
2192 smp->ctx.a[0] = ctx->line + ctx->val;
2193 smp->ctx.a[1] = smp->ctx.a[0] + ctx->vlen;
2194 }
2195
2196 smp->data.type = SMP_T_STR;
2197 smp->flags |= SMP_F_CONST;
2198 smp->ctx.a[0] = http_extract_cookie_value(smp->ctx.a[0], smp->ctx.a[1],
2199 args->data.str.area, args->data.str.data,
2200 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
2201 &smp->data.u.str.area, &smp->data.u.str.data);
2202 if (smp->ctx.a[0]) {
2203 found = 1;
2204 if (occ >= 0) {
2205 /* one value was returned into smp->data.u.str.{str,len} */
2206 smp->flags |= SMP_F_NOT_LAST;
2207 return 1;
2208 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002209 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002210 /* if we're looking for last occurrence, let's loop */
Willy Tarreau79e57332018-10-02 16:01:16 +02002211 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002212 }
2213 /* all cookie headers and values were scanned. If we're looking for the
2214 * last occurrence, we may return it now.
2215 */
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002216 out:
Willy Tarreau79e57332018-10-02 16:01:16 +02002217 smp->flags &= ~SMP_F_NOT_LAST;
2218 return found;
2219}
2220
2221/* Iterate over all cookies present in a request to count how many occurrences
2222 * match the name in args and args->data.str.len. If <multi> is non-null, then
2223 * multiple cookies may be parsed on the same line. The returned sample is of
2224 * type UINT. Accepts exactly 1 argument of type string.
2225 */
2226static int smp_fetch_cookie_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
2227{
Willy Tarreau79e57332018-10-02 16:01:16 +02002228 char *val_beg, *val_end;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002229 int cnt;
Willy Tarreau79e57332018-10-02 16:01:16 +02002230
2231 if (!args || args->type != ARGT_STR)
2232 return 0;
2233
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002234 if (IS_HTX_SMP(smp) || (smp->px->mode == PR_MODE_TCP)) {
2235 /* HTX version */
2236 struct htx *htx = smp_prefetch_htx(smp, args);
2237 struct http_hdr_ctx ctx;
2238 struct ist hdr;
2239
2240 if (!htx)
2241 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02002242
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002243 hdr = (((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
2244 ? ist("Cookie")
2245 : ist("Set-Cookie"));
Willy Tarreau79e57332018-10-02 16:01:16 +02002246
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002247 val_end = val_beg = NULL;
2248 ctx.blk = NULL;
2249 cnt = 0;
2250 while (1) {
2251 /* Note: val_beg == NULL every time we need to fetch a new header */
2252 if (!val_beg) {
2253 if (!http_find_header(htx, hdr, &ctx, 0))
2254 break;
Willy Tarreau79e57332018-10-02 16:01:16 +02002255
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002256 if (ctx.value.len < args->data.str.data + 1)
2257 continue;
Willy Tarreau79e57332018-10-02 16:01:16 +02002258
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002259 val_beg = ctx.value.ptr;
2260 val_end = val_beg + ctx.value.len;
2261 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002262
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002263 smp->data.type = SMP_T_STR;
2264 smp->flags |= SMP_F_CONST;
2265 while ((val_beg = http_extract_cookie_value(val_beg, val_end,
2266 args->data.str.area, args->data.str.data,
2267 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
2268 &smp->data.u.str.area,
2269 &smp->data.u.str.data))) {
2270 cnt++;
2271 }
2272 }
2273 }
2274 else {
2275 /* LEGACY version */
2276 struct http_txn *txn;
2277 struct hdr_idx *idx;
2278 struct hdr_ctx ctx;
2279 const struct http_msg *msg;
2280 const char *hdr_name;
2281 int hdr_name_len;
2282 char *sol;
Willy Tarreau79e57332018-10-02 16:01:16 +02002283
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002284 CHECK_HTTP_MESSAGE_FIRST();
2285
2286 txn = smp->strm->txn;
2287 idx = &smp->strm->txn->hdr_idx;
2288
2289 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
2290 msg = &txn->req;
2291 hdr_name = "Cookie";
2292 hdr_name_len = 6;
2293 } else {
2294 msg = &txn->rsp;
2295 hdr_name = "Set-Cookie";
2296 hdr_name_len = 10;
Willy Tarreau79e57332018-10-02 16:01:16 +02002297 }
2298
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002299 sol = ci_head(msg->chn);
2300 val_end = val_beg = NULL;
2301 ctx.idx = 0;
2302 cnt = 0;
2303
2304 while (1) {
2305 /* Note: val_beg == NULL every time we need to fetch a new header */
2306 if (!val_beg) {
2307 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, &ctx))
2308 break;
2309
2310 if (ctx.vlen < args->data.str.data + 1)
2311 continue;
2312
2313 val_beg = ctx.line + ctx.val;
2314 val_end = val_beg + ctx.vlen;
2315 }
2316
2317 smp->data.type = SMP_T_STR;
2318 smp->flags |= SMP_F_CONST;
2319 while ((val_beg = http_extract_cookie_value(val_beg, val_end,
2320 args->data.str.area, args->data.str.data,
2321 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
2322 &smp->data.u.str.area, &smp->data.u.str.data))) {
2323 cnt++;
2324 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002325 }
2326 }
2327
2328 smp->data.type = SMP_T_SINT;
2329 smp->data.u.sint = cnt;
2330 smp->flags |= SMP_F_VOL_HDR;
2331 return 1;
2332}
2333
2334/* Fetch an cookie's integer value. The integer value is returned. It
2335 * takes a mandatory argument of type string. It relies on smp_fetch_cookie().
2336 */
2337static int smp_fetch_cookie_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
2338{
2339 int ret = smp_fetch_cookie(args, smp, kw, private);
2340
2341 if (ret > 0) {
2342 smp->data.type = SMP_T_SINT;
2343 smp->data.u.sint = strl2ic(smp->data.u.str.area,
2344 smp->data.u.str.data);
2345 }
2346
2347 return ret;
2348}
2349
2350/************************************************************************/
2351/* The code below is dedicated to sample fetches */
2352/************************************************************************/
2353
2354/* This scans a URL-encoded query string. It takes an optionally wrapping
2355 * string whose first contigous chunk has its beginning in ctx->a[0] and end
2356 * in ctx->a[1], and the optional second part in (ctx->a[2]..ctx->a[3]). The
2357 * pointers are updated for next iteration before leaving.
2358 */
2359static int smp_fetch_param(char delim, const char *name, int name_len, const struct arg *args, struct sample *smp, const char *kw, void *private)
2360{
2361 const char *vstart, *vend;
2362 struct buffer *temp;
2363 const char **chunks = (const char **)smp->ctx.a;
2364
2365 if (!http_find_next_url_param(chunks, name, name_len,
2366 &vstart, &vend, delim))
2367 return 0;
2368
2369 /* Create sample. If the value is contiguous, return the pointer as CONST,
2370 * if the value is wrapped, copy-it in a buffer.
2371 */
2372 smp->data.type = SMP_T_STR;
2373 if (chunks[2] &&
2374 vstart >= chunks[0] && vstart <= chunks[1] &&
2375 vend >= chunks[2] && vend <= chunks[3]) {
2376 /* Wrapped case. */
2377 temp = get_trash_chunk();
2378 memcpy(temp->area, vstart, chunks[1] - vstart);
2379 memcpy(temp->area + ( chunks[1] - vstart ), chunks[2],
2380 vend - chunks[2]);
2381 smp->data.u.str.area = temp->area;
2382 smp->data.u.str.data = ( chunks[1] - vstart ) + ( vend - chunks[2] );
2383 } else {
2384 /* Contiguous case. */
2385 smp->data.u.str.area = (char *)vstart;
2386 smp->data.u.str.data = vend - vstart;
2387 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
2388 }
2389
2390 /* Update context, check wrapping. */
2391 chunks[0] = vend;
2392 if (chunks[2] && vend >= chunks[2] && vend <= chunks[3]) {
2393 chunks[1] = chunks[3];
2394 chunks[2] = NULL;
2395 }
2396
2397 if (chunks[0] < chunks[1])
2398 smp->flags |= SMP_F_NOT_LAST;
2399
2400 return 1;
2401}
2402
2403/* This function iterates over each parameter of the query string. It uses
2404 * ctx->a[0] and ctx->a[1] to store the beginning and end of the current
2405 * parameter. Since it uses smp_fetch_param(), ctx->a[2..3] are both NULL.
2406 * An optional parameter name is passed in args[0], otherwise any parameter is
2407 * considered. It supports an optional delimiter argument for the beginning of
2408 * the string in args[1], which defaults to "?".
2409 */
2410static int smp_fetch_url_param(const struct arg *args, struct sample *smp, const char *kw, void *private)
2411{
Willy Tarreau79e57332018-10-02 16:01:16 +02002412 char delim = '?';
2413 const char *name;
2414 int name_len;
2415
2416 if (!args ||
2417 (args[0].type && args[0].type != ARGT_STR) ||
2418 (args[1].type && args[1].type != ARGT_STR))
2419 return 0;
2420
2421 name = "";
2422 name_len = 0;
2423 if (args->type == ARGT_STR) {
2424 name = args->data.str.area;
2425 name_len = args->data.str.data;
2426 }
2427
2428 if (args[1].type)
2429 delim = *args[1].data.str.area;
2430
2431 if (!smp->ctx.a[0]) { // first call, find the query string
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002432 if (IS_HTX_SMP(smp) || (smp->px->mode == PR_MODE_TCP)) {
2433 /* HTX version */
2434 struct htx *htx = smp_prefetch_htx(smp, args);
2435 union h1_sl sl;
Willy Tarreau79e57332018-10-02 16:01:16 +02002436
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002437 if (!htx)
2438 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02002439
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002440 sl = http_find_stline(htx);
2441 smp->ctx.a[0] = http_find_param_list(sl.rq.u.ptr, sl.rq.u.len, delim);
2442 if (!smp->ctx.a[0])
2443 return 0;
2444
2445 smp->ctx.a[1] = sl.rq.u.ptr + sl.rq.u.len;
2446 }
2447 else {
2448 /* LEGACY version */
2449 struct http_msg *msg;
2450
2451 CHECK_HTTP_MESSAGE_FIRST();
2452
2453 msg = &smp->strm->txn->req;
2454
2455 smp->ctx.a[0] = http_find_param_list(ci_head(msg->chn) + msg->sl.rq.u,
2456 msg->sl.rq.u_l, delim);
2457 if (!smp->ctx.a[0])
2458 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02002459
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002460 smp->ctx.a[1] = ci_head(msg->chn) + msg->sl.rq.u + msg->sl.rq.u_l;
2461 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002462
2463 /* Assume that the context is filled with NULL pointer
2464 * before the first call.
2465 * smp->ctx.a[2] = NULL;
2466 * smp->ctx.a[3] = NULL;
2467 */
2468 }
2469
2470 return smp_fetch_param(delim, name, name_len, args, smp, kw, private);
2471}
2472
2473/* This function iterates over each parameter of the body. This requires
2474 * that the body has been waited for using http-buffer-request. It uses
2475 * ctx->a[0] and ctx->a[1] to store the beginning and end of the first
2476 * contigous part of the body, and optionally ctx->a[2..3] to reference the
2477 * optional second part if the body wraps at the end of the buffer. An optional
2478 * parameter name is passed in args[0], otherwise any parameter is considered.
2479 */
2480static int smp_fetch_body_param(const struct arg *args, struct sample *smp, const char *kw, void *private)
2481{
Willy Tarreau79e57332018-10-02 16:01:16 +02002482 const char *name;
2483 int name_len;
2484
2485 if (!args || (args[0].type && args[0].type != ARGT_STR))
2486 return 0;
2487
2488 name = "";
2489 name_len = 0;
2490 if (args[0].type == ARGT_STR) {
2491 name = args[0].data.str.area;
2492 name_len = args[0].data.str.data;
2493 }
2494
2495 if (!smp->ctx.a[0]) { // first call, find the query string
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002496 if (IS_HTX_SMP(smp) || (smp->px->mode == PR_MODE_TCP)) {
2497 /* HTX version */
2498 struct htx *htx = smp_prefetch_htx(smp, args);
2499 struct buffer *temp;
2500 int32_t pos;
Willy Tarreau79e57332018-10-02 16:01:16 +02002501
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002502 if (!htx)
2503 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02002504
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002505 temp = get_trash_chunk();
2506 for (pos = htx_get_head(htx); pos != -1; pos = htx_get_next(htx, pos)) {
2507 struct htx_blk *blk = htx_get_blk(htx, pos);
2508 enum htx_blk_type type = htx_get_blk_type(blk);
Willy Tarreau79e57332018-10-02 16:01:16 +02002509
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002510 if (type == HTX_BLK_EOM || type == HTX_BLK_EOD)
2511 break;
2512 if (type == HTX_BLK_DATA) {
2513 if (!htx_data_to_str(htx_get_blk_value(htx, blk), temp, 0))
2514 return 0;
2515 }
2516 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002517
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002518 smp->ctx.a[0] = temp->area;
2519 smp->ctx.a[1] = temp->area + temp->data;
Willy Tarreau79e57332018-10-02 16:01:16 +02002520
2521 /* Assume that the context is filled with NULL pointer
2522 * before the first call.
2523 * smp->ctx.a[2] = NULL;
2524 * smp->ctx.a[3] = NULL;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002525 */
Willy Tarreau79e57332018-10-02 16:01:16 +02002526 }
2527 else {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002528 /* LEGACY version */
2529 struct http_msg *msg;
2530 unsigned long len;
2531 unsigned long block1;
2532 char *body;
2533
2534 CHECK_HTTP_MESSAGE_FIRST();
2535
2536 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
2537 msg = &smp->strm->txn->req;
2538 else
2539 msg = &smp->strm->txn->rsp;
2540
2541 len = http_body_bytes(msg);
2542 body = c_ptr(msg->chn, -http_data_rewind(msg));
2543
2544 block1 = len;
2545 if (block1 > b_wrap(&msg->chn->buf) - body)
2546 block1 = b_wrap(&msg->chn->buf) - body;
2547
2548 if (block1 == len) {
2549 /* buffer is not wrapped (or empty) */
2550 smp->ctx.a[0] = body;
2551 smp->ctx.a[1] = body + len;
2552
2553 /* Assume that the context is filled with NULL pointer
2554 * before the first call.
2555 * smp->ctx.a[2] = NULL;
2556 * smp->ctx.a[3] = NULL;
2557 */
2558 }
2559 else {
2560 /* buffer is wrapped, we need to defragment it */
2561 smp->ctx.a[0] = body;
2562 smp->ctx.a[1] = body + block1;
2563 smp->ctx.a[2] = b_orig(&msg->chn->buf);
2564 smp->ctx.a[3] = b_orig(&msg->chn->buf) + ( len - block1 );
2565 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002566 }
2567 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002568
Willy Tarreau79e57332018-10-02 16:01:16 +02002569 return smp_fetch_param('&', name, name_len, args, smp, kw, private);
2570}
2571
2572/* Return the signed integer value for the specified url parameter (see url_param
2573 * above).
2574 */
2575static int smp_fetch_url_param_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
2576{
2577 int ret = smp_fetch_url_param(args, smp, kw, private);
2578
2579 if (ret > 0) {
2580 smp->data.type = SMP_T_SINT;
2581 smp->data.u.sint = strl2ic(smp->data.u.str.area,
2582 smp->data.u.str.data);
2583 }
2584
2585 return ret;
2586}
2587
2588/* This produces a 32-bit hash of the concatenation of the first occurrence of
2589 * the Host header followed by the path component if it begins with a slash ('/').
2590 * This means that '*' will not be added, resulting in exactly the first Host
2591 * entry. If no Host header is found, then the path is used. The resulting value
2592 * is hashed using the url hash followed by a full avalanche hash and provides a
2593 * 32-bit integer value. This fetch is useful for tracking per-URL activity on
2594 * high-traffic sites without having to store whole paths.
2595 * this differs from the base32 functions in that it includes the url parameters
2596 * as well as the path
2597 */
2598static int smp_fetch_url32(const struct arg *args, struct sample *smp, const char *kw, void *private)
2599{
Willy Tarreau79e57332018-10-02 16:01:16 +02002600 unsigned int hash = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02002601
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002602 if (IS_HTX_SMP(smp) || (smp->px->mode == PR_MODE_TCP)) {
2603 /* HTX version */
2604 struct htx *htx = smp_prefetch_htx(smp, args);
2605 struct http_hdr_ctx ctx;
2606 union h1_sl sl;
2607 struct ist path;
Willy Tarreau79e57332018-10-02 16:01:16 +02002608
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002609 if (!htx)
2610 return 0;
2611
2612 ctx.blk = NULL;
2613 if (http_find_header(htx, ist("Host"), &ctx, 1)) {
2614 /* OK we have the header value in ctx.value */
2615 while (ctx.value.len--)
2616 hash = *(ctx.value.ptr++) + (hash << 6) + (hash << 16) - hash;
2617 }
2618
2619 /* now retrieve the path */
2620 sl = http_find_stline(htx);
2621 path = http_get_path(sl.rq.u);
2622 while (path.len > 0 && *(path.ptr) != '?') {
2623 path.ptr++;
2624 path.len--;
2625 }
2626 if (path.len && *(path.ptr) == '/') {
2627 while (path.len--)
2628 hash = *(path.ptr++) + (hash << 6) + (hash << 16) - hash;
2629 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002630 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002631 else {
2632 /* LEGACY version */
2633 struct http_txn *txn;
2634 struct hdr_ctx ctx;
2635 char *ptr, *beg, *end;
2636 int len;
2637
2638 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau79e57332018-10-02 16:01:16 +02002639
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002640 txn = smp->strm->txn;
2641 ctx.idx = 0;
2642 if (http_find_header2("Host", 4, ci_head(txn->req.chn), &txn->hdr_idx, &ctx)) {
2643 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
2644 ptr = ctx.line + ctx.val;
2645 len = ctx.vlen;
2646 while (len--)
2647 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
2648 }
2649
2650 /* now retrieve the path */
2651 end = ci_head(txn->req.chn) + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
2652 beg = http_txn_get_path(txn);
2653 if (!beg)
2654 beg = end;
Willy Tarreau79e57332018-10-02 16:01:16 +02002655
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002656 for (ptr = beg; ptr < end ; ptr++);
Willy Tarreau79e57332018-10-02 16:01:16 +02002657
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002658 if (beg < ptr && *beg == '/') {
2659 while (beg < ptr)
2660 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
2661 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002662 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002663
Willy Tarreau79e57332018-10-02 16:01:16 +02002664 hash = full_hash(hash);
2665
2666 smp->data.type = SMP_T_SINT;
2667 smp->data.u.sint = hash;
2668 smp->flags = SMP_F_VOL_1ST;
2669 return 1;
2670}
2671
2672/* This concatenates the source address with the 32-bit hash of the Host and
2673 * URL as returned by smp_fetch_base32(). The idea is to have per-source and
2674 * per-url counters. The result is a binary block from 8 to 20 bytes depending
2675 * on the source address length. The URL hash is stored before the address so
2676 * that in environments where IPv6 is insignificant, truncating the output to
2677 * 8 bytes would still work.
2678 */
2679static int smp_fetch_url32_src(const struct arg *args, struct sample *smp, const char *kw, void *private)
2680{
2681 struct buffer *temp;
2682 struct connection *cli_conn = objt_conn(smp->sess->origin);
2683
2684 if (!cli_conn)
2685 return 0;
2686
2687 if (!smp_fetch_url32(args, smp, kw, private))
2688 return 0;
2689
2690 temp = get_trash_chunk();
2691 *(unsigned int *) temp->area = htonl(smp->data.u.sint);
2692 temp->data += sizeof(unsigned int);
2693
2694 switch (cli_conn->addr.from.ss_family) {
2695 case AF_INET:
2696 memcpy(temp->area + temp->data,
2697 &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr,
2698 4);
2699 temp->data += 4;
2700 break;
2701 case AF_INET6:
2702 memcpy(temp->area + temp->data,
2703 &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr,
2704 16);
2705 temp->data += 16;
2706 break;
2707 default:
2708 return 0;
2709 }
2710
2711 smp->data.u.str = *temp;
2712 smp->data.type = SMP_T_BIN;
2713 return 1;
2714}
2715
2716/************************************************************************/
2717/* Other utility functions */
2718/************************************************************************/
2719
2720/* This function is used to validate the arguments passed to any "hdr" fetch
2721 * keyword. These keywords support an optional positive or negative occurrence
2722 * number. We must ensure that the number is greater than -MAX_HDR_HISTORY. It
2723 * is assumed that the types are already the correct ones. Returns 0 on error,
2724 * non-zero if OK. If <err> is not NULL, it will be filled with a pointer to an
2725 * error message in case of error, that the caller is responsible for freeing.
2726 * The initial location must either be freeable or NULL.
2727 * Note: this function's pointer is checked from Lua.
2728 */
2729int val_hdr(struct arg *arg, char **err_msg)
2730{
2731 if (arg && arg[1].type == ARGT_SINT && arg[1].data.sint < -MAX_HDR_HISTORY) {
2732 memprintf(err_msg, "header occurrence must be >= %d", -MAX_HDR_HISTORY);
2733 return 0;
2734 }
2735 return 1;
2736}
2737
2738/************************************************************************/
2739/* All supported sample fetch keywords must be declared here. */
2740/************************************************************************/
2741
2742/* Note: must not be declared <const> as its list will be overwritten */
2743static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
2744 { "base", smp_fetch_base, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2745 { "base32", smp_fetch_base32, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
2746 { "base32+src", smp_fetch_base32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
2747
2748 /* capture are allocated and are permanent in the stream */
2749 { "capture.req.hdr", smp_fetch_capture_req_hdr, ARG1(1,SINT), NULL, SMP_T_STR, SMP_USE_HRQHP },
2750
2751 /* retrieve these captures from the HTTP logs */
2752 { "capture.req.method", smp_fetch_capture_req_method, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
2753 { "capture.req.uri", smp_fetch_capture_req_uri, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
2754 { "capture.req.ver", smp_fetch_capture_req_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
2755
2756 { "capture.res.hdr", smp_fetch_capture_res_hdr, ARG1(1,SINT), NULL, SMP_T_STR, SMP_USE_HRSHP },
2757 { "capture.res.ver", smp_fetch_capture_res_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
2758
2759 /* cookie is valid in both directions (eg: for "stick ...") but cook*
2760 * are only here to match the ACL's name, are request-only and are used
2761 * for ACL compatibility only.
2762 */
2763 { "cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
2764 { "cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV|SMP_USE_HRSHV },
2765 { "cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2766 { "cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2767
2768 /* hdr is valid in both directions (eg: for "stick ...") but hdr_* are
2769 * only here to match the ACL's name, are request-only and are used for
2770 * ACL compatibility only.
2771 */
2772 { "hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV|SMP_USE_HRSHV },
2773 { "hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2774 { "hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV },
2775 { "hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_SINT, SMP_USE_HRQHV },
2776
2777 { "http_auth", smp_fetch_http_auth, ARG1(1,USR), NULL, SMP_T_BOOL, SMP_USE_HRQHV },
2778 { "http_auth_group", smp_fetch_http_auth_grp, ARG1(1,USR), NULL, SMP_T_STR, SMP_USE_HRQHV },
2779 { "http_first_req", smp_fetch_http_first_req, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
2780 { "method", smp_fetch_meth, 0, NULL, SMP_T_METH, SMP_USE_HRQHP },
2781 { "path", smp_fetch_path, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2782 { "query", smp_fetch_query, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2783
2784 /* HTTP protocol on the request path */
2785 { "req.proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
2786 { "req_proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
2787
2788 /* HTTP version on the request path */
2789 { "req.ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2790 { "req_ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2791
2792 { "req.body", smp_fetch_body, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
2793 { "req.body_len", smp_fetch_body_len, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
2794 { "req.body_size", smp_fetch_body_size, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
2795 { "req.body_param", smp_fetch_body_param, ARG1(0,STR), NULL, SMP_T_BIN, SMP_USE_HRQHV },
2796
2797 { "req.hdrs", smp_fetch_hdrs, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
2798 { "req.hdrs_bin", smp_fetch_hdrs_bin, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
2799
2800 /* HTTP version on the response path */
2801 { "res.ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
2802 { "resp_ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
2803
2804 /* explicit req.{cook,hdr} are used to force the fetch direction to be request-only */
2805 { "req.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
2806 { "req.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2807 { "req.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2808
2809 { "req.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
2810 { "req.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2811 { "req.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
2812 { "req.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2813 { "req.hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV },
2814 { "req.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
2815 { "req.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_SINT, SMP_USE_HRQHV },
2816
2817 /* explicit req.{cook,hdr} are used to force the fetch direction to be response-only */
2818 { "res.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
2819 { "res.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2820 { "res.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2821
2822 { "res.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
2823 { "res.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2824 { "res.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
2825 { "res.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2826 { "res.hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV },
2827 { "res.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
2828 { "res.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_SINT, SMP_USE_HRSHV },
2829
2830 /* scook is valid only on the response and is used for ACL compatibility */
2831 { "scook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
2832 { "scook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2833 { "scook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2834 { "set-cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV }, /* deprecated */
2835
2836 /* shdr is valid only on the response and is used for ACL compatibility */
2837 { "shdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
2838 { "shdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2839 { "shdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV },
2840 { "shdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_SINT, SMP_USE_HRSHV },
2841
2842 { "status", smp_fetch_stcode, 0, NULL, SMP_T_SINT, SMP_USE_HRSHP },
2843 { "unique-id", smp_fetch_uniqueid, 0, NULL, SMP_T_STR, SMP_SRC_L4SRV },
2844 { "url", smp_fetch_url, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2845 { "url32", smp_fetch_url32, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
2846 { "url32+src", smp_fetch_url32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
2847 { "url_ip", smp_fetch_url_ip, 0, NULL, SMP_T_IPV4, SMP_USE_HRQHV },
2848 { "url_port", smp_fetch_url_port, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
2849 { "url_param", smp_fetch_url_param, ARG2(0,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
2850 { "urlp" , smp_fetch_url_param, ARG2(0,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
2851 { "urlp_val", smp_fetch_url_param_val, ARG2(0,STR,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2852 { /* END */ },
2853}};
2854
Willy Tarreau0108d902018-11-25 19:14:37 +01002855INITCALL1(STG_REGISTER, sample_register_fetches, &sample_fetch_keywords);
Willy Tarreau79e57332018-10-02 16:01:16 +02002856
2857/*
2858 * Local variables:
2859 * c-indent-level: 8
2860 * c-basic-offset: 8
2861 * End:
2862 */